Commit | Line | Data |
---|---|---|
46e6ad89 | 1 | /* -*-c-*- |
2 | * | |
46e6ad89 | 3 | * Reading and writing buffers of stuff |
4 | * | |
5 | * (c) 2005 Straylight/Edgeware | |
6 | */ | |
7 | ||
b2687a0a | 8 | /*----- Licensing notice --------------------------------------------------* |
46e6ad89 | 9 | * |
10 | * This file is part of the Python interface to Catacomb. | |
11 | * | |
12 | * Catacomb/Python is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
b2687a0a | 16 | * |
46e6ad89 | 17 | * Catacomb/Python is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
b2687a0a | 21 | * |
46e6ad89 | 22 | * You should have received a copy of the GNU General Public License |
23 | * along with Catacomb/Python; if not, write to the Free Software Foundation, | |
24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | */ | |
26 | ||
27 | /*----- Header files ------------------------------------------------------*/ | |
28 | ||
29 | #include "catacomb-python.h" | |
30 | ||
46e6ad89 | 31 | /*----- Read buffers ------------------------------------------------------*/ |
32 | ||
dc088b4d MW |
33 | PyTypeObject *rbuf_pytype; |
34 | ||
46e6ad89 | 35 | static PyObject *rbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
36 | { | |
67c75893 MW |
37 | struct bin in; |
38 | void *q; | |
46e6ad89 | 39 | buf_pyobj *me = 0; |
827f89d7 | 40 | static const char *const kwlist[] = { "data", 0 }; |
46e6ad89 | 41 | |
67c75893 | 42 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &in)) |
46e6ad89 | 43 | goto end; |
67c75893 MW |
44 | q = xmalloc(in.sz); |
45 | memcpy(q, in.p, in.sz); | |
0463996f | 46 | me = (buf_pyobj *)ty->tp_alloc(ty, 0); |
1e2e275e | 47 | me->sub = 0; me->lk = 0; |
67c75893 | 48 | buf_init(&me->b, q, in.sz); |
46e6ad89 | 49 | end: |
50 | return ((PyObject *)me); | |
51 | } | |
52 | ||
53 | static void buf_pydealloc(PyObject *me) | |
0463996f | 54 | { |
1e2e275e MW |
55 | assert(!BUF_LK(me)); |
56 | if (BUF_SUB(me)) Py_DECREF(BUF_SUB(me)); | |
57 | else xfree(BBASE(BUF_B(me))); | |
0463996f | 58 | FREEOBJ(me); |
59 | } | |
46e6ad89 | 60 | |
3d8f5f7c | 61 | static Py_ssize_t rbuf_pysegcount(PyObject *me, Py_ssize_t *nn) |
46e6ad89 | 62 | { if (nn) *nn = BSZ(BUF_B(me)); return (1); } |
63 | ||
3d8f5f7c | 64 | static Py_ssize_t rbuf_pyreadbuf(PyObject *me, Py_ssize_t seg, void **q) |
f368b46e | 65 | { assert(seg == 0); *q = BCUR(BUF_B(me)); return (BLEFT(BUF_B(me))); } |
46e6ad89 | 66 | |
67 | static PyObject *rbmeth_skip(PyObject *me, PyObject *arg) | |
68 | { | |
69 | size_t n; | |
70 | ||
71 | if (!PyArg_ParseTuple(arg, "O&:skip", convszt, &n)) goto end; | |
de2b80a2 | 72 | if (!buf_get(BUF_B(me), n)) BUFERR("buffer exhausted"); |
46e6ad89 | 73 | RETURN_ME; |
74 | end: | |
75 | return (0); | |
76 | } | |
77 | ||
78 | static PyObject *rbmeth_get(PyObject *me, PyObject *arg) | |
79 | { | |
80 | void *p; | |
81 | size_t n; | |
82 | ||
83 | if (!PyArg_ParseTuple(arg, "O&:get", convszt, &n)) goto end; | |
de2b80a2 | 84 | if ((p = buf_get(BUF_B(me), n)) == 0) BUFERR("buffer exhausted"); |
46e6ad89 | 85 | return (bytestring_pywrap(p, n)); |
86 | end: | |
87 | return (0); | |
88 | } | |
89 | ||
90 | #define RBMETH_GETU_(n, W, w) \ | |
91e56f06 | 91 | static PyObject *rbmeth_getu##w(PyObject *me) \ |
46e6ad89 | 92 | { \ |
93 | uint##n x; \ | |
de2b80a2 | 94 | if (buf_getu##w(BUF_B(me), &x)) BUFERR("buffer exhausted"); \ |
2da7a9c0 MW |
95 | if (MASK##W <= ULONG_MAX) return (getulong(x)); \ |
96 | else { kludge64 y; ASSIGN64(y, x); return (getk64(y)); } \ | |
46e6ad89 | 97 | end: \ |
98 | return (0); \ | |
99 | } | |
100 | DOUINTCONV(RBMETH_GETU_) | |
101 | ||
102 | #define RBMETH_GETBLK_(n, W, w) \ | |
91e56f06 | 103 | static PyObject *rbmeth_getblk##w(PyObject *me) \ |
46e6ad89 | 104 | { \ |
105 | size_t sz; \ | |
106 | char *q; \ | |
de2b80a2 MW |
107 | if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) \ |
108 | BUFERR("buffer exhausted"); \ | |
46e6ad89 | 109 | return (bytestring_pywrap(q, sz)); \ |
110 | end: \ | |
111 | return (0); \ | |
112 | } | |
113 | BUF_DOSUFFIXES(RBMETH_GETBLK_) | |
114 | ||
0463996f | 115 | #define RBMETH_GETBUF_(n, W, w) \ |
91e56f06 | 116 | static PyObject *rbmeth_getbuf##w(PyObject *me) \ |
0463996f | 117 | { \ |
118 | buf_pyobj *b; \ | |
119 | buf bb; \ | |
de2b80a2 | 120 | if (buf_getbuf##w(BUF_B(me), &bb)) BUFERR("buffer exhausted"); \ |
0463996f | 121 | b = PyObject_NEW(buf_pyobj, rbuf_pytype); \ |
122 | b->b = bb; \ | |
123 | b->sub = me; \ | |
1e2e275e | 124 | b->lk = 0; \ |
0463996f | 125 | Py_INCREF(me); \ |
126 | return ((PyObject *)b); \ | |
127 | end: \ | |
128 | return (0); \ | |
129 | } | |
130 | BUF_DOSUFFIXES(RBMETH_GETBUF_) | |
131 | ||
91e56f06 | 132 | static PyObject *rbmeth_getmp(PyObject *me) |
46e6ad89 | 133 | { |
134 | mp *x; | |
de2b80a2 | 135 | if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR("buffer exhausted"); |
46e6ad89 | 136 | return (mp_pywrap(x)); |
137 | end: | |
138 | return (0); | |
139 | } | |
140 | ||
91e56f06 | 141 | static PyObject *rbmeth_getgf(PyObject *me) |
46e6ad89 | 142 | { |
143 | mp *x; | |
de2b80a2 | 144 | if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR("buffer exhausted"); |
46e6ad89 | 145 | return (gf_pywrap(x)); |
146 | end: | |
147 | return (0); | |
148 | } | |
149 | ||
150 | static PyObject *rbmeth_getecpt(PyObject *me, PyObject *arg, PyObject *kw) | |
151 | { | |
152 | PyObject *cobj = Py_None; | |
827f89d7 | 153 | static const char *const kwlist[] = { "curve", 0 }; |
46e6ad89 | 154 | ec pt = EC_INIT; |
827f89d7 | 155 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:getecpt", KWLIST, &cobj)) |
46e6ad89 | 156 | goto end; |
157 | if (cobj == Py_None) cobj = (PyObject *)ecpt_pytype; | |
158 | if (!PyType_Check(cobj) || | |
159 | !PyType_IsSubtype((PyTypeObject *)cobj, ecpt_pytype)) | |
160 | TYERR("expected elliptic curve type"); | |
de2b80a2 | 161 | if (buf_getec(BUF_B(me), &pt)) BUFERR("buffer exhausted"); |
46e6ad89 | 162 | return (ecpt_pywrapout(cobj, &pt)); |
163 | end: | |
164 | return (0); | |
165 | } | |
b2687a0a | 166 | |
46e6ad89 | 167 | static PyObject *rbmeth_getecptraw(PyObject *me, PyObject *arg) |
168 | { | |
8a431988 | 169 | PyObject *cobj; |
46e6ad89 | 170 | ec pt = EC_INIT; |
7c7a6f8e | 171 | PyObject *rc = 0; |
46e6ad89 | 172 | if (!PyArg_ParseTuple(arg, "O!:getecptraw", eccurve_pytype, &cobj)) |
173 | goto end; | |
de2b80a2 | 174 | if (ec_getraw(ECCURVE_C(cobj), BUF_B(me), &pt)) BUFERR("buffer exhausted"); |
7c7a6f8e | 175 | rc = ecpt_pywrapout(cobj, &pt); |
46e6ad89 | 176 | end: |
7c7a6f8e | 177 | return (rc); |
46e6ad89 | 178 | } |
179 | ||
180 | static PyObject *rbmeth_getge(PyObject *me, PyObject *arg) | |
181 | { | |
182 | PyObject *gobj; | |
183 | ge *x = 0; | |
184 | if (!PyArg_ParseTuple(arg, "O!:getge", group_pytype, &gobj)) goto end; | |
185 | x = G_CREATE(GROUP_G(gobj)); | |
de2b80a2 | 186 | if (G_FROMBUF(GROUP_G(gobj), BUF_B(me), x)) BUFERR("buffer exhausted"); |
46e6ad89 | 187 | return (ge_pywrap(gobj, x)); |
188 | end: | |
189 | if (x) G_DESTROY(GROUP_G(gobj), x); | |
190 | return (0); | |
191 | } | |
192 | ||
193 | static PyObject *rbmeth_getgeraw(PyObject *me, PyObject *arg) | |
194 | { | |
195 | PyObject *gobj; | |
196 | ge *x = 0; | |
197 | if (!PyArg_ParseTuple(arg, "O!:getgeraw", group_pytype, &gobj)) goto end; | |
198 | x = G_CREATE(GROUP_G(gobj)); | |
de2b80a2 | 199 | if (G_FROMRAW(GROUP_G(gobj), BUF_B(me), x)) BUFERR("buffer exhausted"); |
46e6ad89 | 200 | return (ge_pywrap(gobj, x)); |
201 | end: | |
202 | if (x) G_DESTROY(GROUP_G(gobj), x); | |
203 | return (0); | |
204 | } | |
205 | ||
206 | static PyObject *rbget_size(PyObject *me, void *hunoz) | |
207 | { return (PyInt_FromLong(BSZ(BUF_B(me)))); } | |
208 | static PyObject *rbget_left(PyObject *me, void *hunoz) | |
209 | { return (PyInt_FromLong(BLEFT(BUF_B(me)))); } | |
210 | static PyObject *rbget_endp(PyObject *me, void *hunoz) | |
211 | { return (getbool(!BLEFT(BUF_B(me)))); } | |
0463996f | 212 | static PyObject *rbget_offset(PyObject *me, void *hunoz) |
213 | { return (PyInt_FromLong(BLEN(BUF_B(me)))); } | |
214 | static int rbset_offset(PyObject *me, PyObject *x, void *hunoz) | |
215 | { | |
216 | size_t n; | |
f368b46e | 217 | if (!x) NIERR("__del__"); |
0463996f | 218 | if (!convszt(x, &n)) goto end; |
219 | if (n > BSZ(BUF_B(me))) VALERR("out of range"); | |
220 | BCUR(BUF_B(me)) = BBASE(BUF_B(me)) + n; | |
221 | return (0); | |
222 | end: | |
223 | return (-1); | |
224 | } | |
46e6ad89 | 225 | |
c90f712e | 226 | static const PyGetSetDef rbuf_pygetset[] = { |
46e6ad89 | 227 | #define GETSETNAME(op, name) rb##op##_##name |
d96c882e MW |
228 | GET (size, "RBUF.size -> SIZE") |
229 | GET (left, "RBUF.left -> REMAINDER") | |
230 | GET (endp, "RBUF.endp -> BOOL") | |
231 | GETSET(offset, "RBUF.offset -> OFFSET") | |
46e6ad89 | 232 | #undef GETSETNAME |
233 | { 0 } | |
234 | }; | |
235 | ||
c90f712e | 236 | static const PyMethodDef rbuf_pymethods[] = { |
46e6ad89 | 237 | #define METHNAME(func) rbmeth_##func |
d96c882e MW |
238 | METH (skip, "RBUF.skip(N)") |
239 | METH (get, "RBUF.get(N) -> BYTES") | |
46e6ad89 | 240 | #define RBMETH_DECL_GETU_(n, W, w) \ |
91e56f06 | 241 | NAMETH(getu##w, "RBUF.getu" #w "() -> INT") |
46e6ad89 | 242 | DOUINTCONV(RBMETH_DECL_GETU_) |
243 | #define RBMETH_DECL_GETBLK_(n, W, w) \ | |
91e56f06 | 244 | NAMETH(getblk##w, "RBUF.getblk" #w "() -> BYTES") |
46e6ad89 | 245 | BUF_DOSUFFIXES(RBMETH_DECL_GETBLK_) |
0463996f | 246 | #define RBMETH_DECL_GETBUF_(n, W, w) \ |
91e56f06 | 247 | NAMETH(getbuf##w, "RBUF.getbuf" #w "() -> RBUF'") |
0463996f | 248 | BUF_DOSUFFIXES(RBMETH_DECL_GETBUF_) |
91e56f06 MW |
249 | NAMETH(getmp, "RBUF.getmp() -> X") |
250 | NAMETH(getgf, "RBUF.getgf() -> X") | |
d96c882e MW |
251 | KWMETH(getecpt, "RBUF.getecpt([curve = None]) -> P") |
252 | METH (getecptraw, "RBUF.getecptraw(CURVE) -> P") | |
253 | METH (getge, "RBUF.getge(GROUP) -> X") | |
254 | METH (getgeraw, "RBUF.getgeraw(GROUP) -> X") | |
46e6ad89 | 255 | #undef METHNAME |
256 | { 0 } | |
257 | }; | |
258 | ||
c90f712e | 259 | static const PyBufferProcs rbuf_pybuffer = { |
46e6ad89 | 260 | rbuf_pyreadbuf, /* @bf_getreadbuffer@ */ |
261 | 0, /* @bf_getwritebuffer@ */ | |
262 | rbuf_pysegcount, /* @bf_getsegcount@ */ | |
263 | 0 /* @bf_getcharbuffer@ */ | |
264 | }; | |
265 | ||
c263b05c | 266 | static const PyTypeObject rbuf_pytype_skel = { |
591bf41b | 267 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 268 | "ReadBuffer", /* @tp_name@ */ |
46e6ad89 | 269 | sizeof(buf_pyobj), /* @tp_basicsize@ */ |
270 | 0, /* @tp_itemsize@ */ | |
271 | ||
272 | buf_pydealloc, /* @tp_dealloc@ */ | |
273 | 0, /* @tp_print@ */ | |
274 | 0, /* @tp_getattr@ */ | |
275 | 0, /* @tp_setattr@ */ | |
276 | 0, /* @tp_compare@ */ | |
277 | 0, /* @tp_repr@ */ | |
278 | 0, /* @tp_as_number@ */ | |
279 | 0, /* @tp_as_sequence@ */ | |
280 | 0, /* @tp_as_mapping@ */ | |
281 | 0, /* @tp_hash@ */ | |
282 | 0, /* @tp_call@ */ | |
283 | 0, /* @tp_str@ */ | |
284 | 0, /* @tp_getattro@ */ | |
285 | 0, /* @tp_setattro@ */ | |
c90f712e | 286 | PYBUFFER(rbuf), /* @tp_as_buffer@ */ |
46e6ad89 | 287 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
288 | Py_TPFLAGS_BASETYPE, | |
289 | ||
290 | /* @tp_doc@ */ | |
d96c882e | 291 | "ReadBuffer(STR): a read buffer.", |
46e6ad89 | 292 | |
293 | 0, /* @tp_traverse@ */ | |
294 | 0, /* @tp_clear@ */ | |
295 | 0, /* @tp_richcompare@ */ | |
296 | 0, /* @tp_weaklistoffset@ */ | |
297 | 0, /* @tp_iter@ */ | |
963a6148 | 298 | 0, /* @tp_iternext@ */ |
c90f712e | 299 | PYMETHODS(rbuf), /* @tp_methods@ */ |
46e6ad89 | 300 | 0, /* @tp_members@ */ |
c90f712e | 301 | PYGETSET(rbuf), /* @tp_getset@ */ |
46e6ad89 | 302 | 0, /* @tp_base@ */ |
303 | 0, /* @tp_dict@ */ | |
304 | 0, /* @tp_descr_get@ */ | |
305 | 0, /* @tp_descr_set@ */ | |
306 | 0, /* @tp_dictoffset@ */ | |
307 | 0, /* @tp_init@ */ | |
308 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
309 | rbuf_pynew, /* @tp_new@ */ | |
310 | 0, /* @tp_free@ */ | |
311 | 0 /* @tp_is_gc@ */ | |
312 | }; | |
313 | ||
314 | /*----- Write buffers -----------------------------------------------------*/ | |
315 | ||
dc088b4d MW |
316 | PyTypeObject *wbuf_pytype; |
317 | ||
318 | int ensurebuf(PyObject *me, size_t n) | |
46e6ad89 | 319 | { |
320 | buf *b = BUF_B(me); | |
1e2e275e MW |
321 | size_t nn = BSZ(b); |
322 | octet *p; | |
323 | size_t want = BLEN(b) + n; | |
46e6ad89 | 324 | |
1e2e275e MW |
325 | if (BLEFT(b) >= n) |
326 | return (0); | |
327 | else if (BUF_LK(me)) | |
328 | BUFERR("buffer locked"); | |
329 | else { | |
46e6ad89 | 330 | while (nn < want) nn <<= 1; |
331 | p = xrealloc(BBASE(b), nn, BSZ(b)); | |
332 | BCUR(b) = p + BLEN(b); | |
333 | BLIM(b) = p + nn; | |
334 | BBASE(b) = p; | |
1e2e275e | 335 | return (0); |
46e6ad89 | 336 | } |
1e2e275e MW |
337 | end: |
338 | return (-1); | |
46e6ad89 | 339 | } |
340 | ||
341 | static PyObject *wbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
342 | { | |
343 | char *p; | |
344 | size_t n = 64; | |
345 | buf_pyobj *me = 0; | |
827f89d7 | 346 | static const char *const kwlist[] = { "size", 0 }; |
46e6ad89 | 347 | |
827f89d7 | 348 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST, |
46e6ad89 | 349 | convszt, &n)) |
350 | goto end; | |
351 | me = (buf_pyobj *)ty->tp_alloc(ty, 0); | |
352 | p = xmalloc(n); | |
1e2e275e | 353 | me->sub = 0; me->lk = 0; |
46e6ad89 | 354 | buf_init(&me->b, p, n); |
355 | end: | |
356 | return ((PyObject *)me); | |
357 | } | |
358 | ||
3d8f5f7c | 359 | static Py_ssize_t wbuf_pysegcount(PyObject *me, Py_ssize_t *nn) |
46e6ad89 | 360 | { if (nn) *nn = BLEN(BUF_B(me)); return (1); } |
361 | ||
3d8f5f7c | 362 | static Py_ssize_t wbuf_pyreadbuf(PyObject *me, Py_ssize_t seg, void **q) |
46e6ad89 | 363 | { assert(seg == 0); *q = BBASE(BUF_B(me)); return (BLEN(BUF_B(me))); } |
364 | ||
365 | static PyObject *wbmeth_zero(PyObject *me, PyObject *arg) | |
366 | { | |
367 | void *p; | |
368 | size_t n; | |
369 | if (!PyArg_ParseTuple(arg, "O&:zero", convszt, &n)) return (0); | |
dc088b4d | 370 | if (ensurebuf(me, n)) return (0); |
46e6ad89 | 371 | p = buf_get(BUF_B(me), n); assert(p && BOK(BUF_B(me))); |
372 | memset(p, 0, n); | |
373 | RETURN_ME; | |
374 | } | |
375 | ||
376 | static PyObject *wbmeth_put(PyObject *me, PyObject *arg) | |
377 | { | |
67c75893 MW |
378 | struct bin in; |
379 | if (!PyArg_ParseTuple(arg, "O&:put", convbin, &in)) return (0); | |
dc088b4d | 380 | if (ensurebuf(me, in.sz)) return (0); |
67c75893 | 381 | buf_put(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me))); |
46e6ad89 | 382 | RETURN_ME; |
383 | } | |
384 | ||
385 | #define WBMETH_PUTU_(n, W, w) \ | |
386 | static PyObject *wbmeth_putu##w(PyObject *me, PyObject *arg) \ | |
387 | { \ | |
388 | uint##n i; \ | |
389 | if (!PyArg_ParseTuple(arg, "O&:putu" #w, convu##n, &i)) return (0); \ | |
dc088b4d | 390 | if (ensurebuf(me, SZ_##n)) return (0); \ |
46e6ad89 | 391 | buf_putu##w(BUF_B(me), i); assert(BOK(BUF_B(me))); \ |
392 | RETURN_ME; \ | |
393 | } | |
394 | DOUINTCONV(WBMETH_PUTU_) | |
395 | ||
9fa70ff7 | 396 | #define MASKz 0 |
46e6ad89 | 397 | #define SZ_z 1 |
398 | #define WBMETH_PUTBLK_(n, W, w) \ | |
399 | static PyObject *wbmeth_putblk##w(PyObject *me, PyObject *arg) \ | |
400 | { \ | |
67c75893 MW |
401 | struct bin in; \ |
402 | if (!PyArg_ParseTuple(arg, "O&:putblk" #w, convbin, &in)) goto end; \ | |
403 | if (MASK##W && in.sz > MASK##W) VALERR("too large"); \ | |
dc088b4d | 404 | if (ensurebuf(me, in.sz + SZ_##n)) return (0); \ |
67c75893 | 405 | buf_putmem##w(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me))); \ |
46e6ad89 | 406 | RETURN_ME; \ |
9fa70ff7 MW |
407 | end: \ |
408 | return (0); \ | |
46e6ad89 | 409 | } |
410 | BUF_DOSUFFIXES(WBMETH_PUTBLK_) | |
411 | ||
412 | static PyObject *wbmeth_putmp(PyObject *me, PyObject *arg) | |
413 | { | |
414 | mp *x = 0; | |
415 | if (!PyArg_ParseTuple(arg, "O&:putmp", convmp, &x)) return (0); | |
dc088b4d | 416 | if (ensurebuf(me, mp_octets(x) + 2)) return (0); |
46e6ad89 | 417 | buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me))); |
418 | RETURN_ME; | |
419 | } | |
420 | ||
421 | static PyObject *wbmeth_putgf(PyObject *me, PyObject *arg) | |
422 | { | |
423 | mp *x = 0; | |
424 | if (!PyArg_ParseTuple(arg, "O&:putgf", convgf, &x)) return (0); | |
dc088b4d | 425 | if (ensurebuf(me, mp_octets(x) + 2)) return (0); |
46e6ad89 | 426 | buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me))); |
427 | MP_DROP(x); | |
428 | RETURN_ME; | |
429 | } | |
430 | ||
431 | static PyObject *wbmeth_putecpt(PyObject *me, PyObject *arg) | |
432 | { | |
433 | ec pt = EC_INIT; | |
434 | if (!PyArg_ParseTuple(arg, "O&:putecpt", convecpt, &pt)) return (0); | |
dc088b4d MW |
435 | if (ensurebuf(me, EC_ATINF(&pt) ? 2 : |
436 | 6 + mp_octets(pt.x) + mp_octets(pt.y))) | |
1e2e275e | 437 | return (0); |
46e6ad89 | 438 | buf_putec(BUF_B(me), &pt); assert(BOK(BUF_B(me))); |
439 | EC_DESTROY(&pt); | |
440 | RETURN_ME; | |
441 | } | |
442 | ||
443 | static PyObject *wbmeth_putecptraw(PyObject *me, PyObject *arg) | |
444 | { | |
445 | PyObject *ptobj; | |
446 | ec pt = EC_INIT; | |
447 | if (!PyArg_ParseTuple(arg, "O!:putecptraw", ecptcurve_pytype, &ptobj)) | |
448 | return (0); | |
449 | EC_OUT(ECPT_C(ptobj), &pt, ECPT_P(ptobj)); | |
dc088b4d | 450 | if (ensurebuf(me, ECPT_C(ptobj)->f->noctets * 2 + 1)) return (0); |
46e6ad89 | 451 | ec_putraw(ECPT_C(ptobj), BUF_B(me), &pt); assert(BOK(BUF_B(me))); |
452 | EC_DESTROY(&pt); | |
453 | RETURN_ME; | |
454 | } | |
455 | ||
456 | static PyObject *wbmeth_putge(PyObject *me, PyObject *arg) | |
457 | { | |
458 | PyObject *geobj; | |
459 | if (!PyArg_ParseTuple(arg, "O!:putge", ge_pytype, &geobj)) return (0); | |
dc088b4d | 460 | if (ensurebuf(me, GE_G(geobj)->noctets)) return (0); |
46e6ad89 | 461 | G_TOBUF(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me))); |
462 | RETURN_ME; | |
463 | } | |
464 | ||
465 | static PyObject *wbmeth_putgeraw(PyObject *me, PyObject *arg) | |
466 | { | |
467 | PyObject *geobj; | |
468 | if (!PyArg_ParseTuple(arg, "O!:putgeraw", ge_pytype, &geobj)) return (0); | |
dc088b4d | 469 | if (ensurebuf(me, GE_G(geobj)->noctets)) return (0); |
46e6ad89 | 470 | G_TORAW(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me))); |
471 | RETURN_ME; | |
472 | } | |
473 | ||
474 | static PyObject *wbget_size(PyObject *me, void *hunoz) | |
475 | { return (PyInt_FromLong(BLEN(BUF_B(me)))); } | |
476 | ||
0051d10f MW |
477 | static PyObject *wbget_contents(PyObject *me, void *hunoz) |
478 | { return (bytestring_pywrap(BBASE(BUF_B(me)), BLEN(BUF_B(me)))); } | |
479 | ||
c90f712e | 480 | static const PyGetSetDef wbuf_pygetset[] = { |
46e6ad89 | 481 | #define GETSETNAME(op, name) wb##op##_##name |
d96c882e MW |
482 | GET (size, "WBUF.size -> SIZE") |
483 | GET (contents, "WBUF.contents -> STR") | |
46e6ad89 | 484 | #undef GETSETNAME |
485 | { 0 } | |
486 | }; | |
487 | ||
c90f712e | 488 | static const PyMethodDef wbuf_pymethods[] = { |
46e6ad89 | 489 | #define METHNAME(func) wbmeth_##func |
d96c882e MW |
490 | METH (zero, "WBUF.zero(N)") |
491 | METH (put, "WBUF.put(BYTES)") | |
46e6ad89 | 492 | #define WBMETH_DECL_PUTU_(n, W, w) \ |
d96c882e | 493 | METH(putu##w, "WBUF.putu" #w "(INT)") |
46e6ad89 | 494 | DOUINTCONV(WBMETH_DECL_PUTU_) |
495 | #define WBMETH_DECL_PUTBLK_(n, W, w) \ | |
d96c882e | 496 | METH(putblk##w, "WBUF.putblk" #w "(BYTES)") |
46e6ad89 | 497 | BUF_DOSUFFIXES(WBMETH_DECL_PUTBLK_) |
d96c882e MW |
498 | METH (putmp, "WBUF.putmp(X)") |
499 | METH (putgf, "WBUF.putgf(X)") | |
500 | METH (putecpt, "WBUF.putecpt(P)") | |
501 | METH (putecptraw, "WBUF.putecptraw(P)") | |
502 | METH (putge, "WBUF.putge(X)") | |
503 | METH (putgeraw, "WBUF.putgeraw(X)") | |
46e6ad89 | 504 | #undef METHNAME |
505 | { 0 } | |
506 | }; | |
507 | ||
c90f712e | 508 | static const PyBufferProcs wbuf_pybuffer = { |
46e6ad89 | 509 | wbuf_pyreadbuf, /* @bf_getreadbuffer@ */ |
510 | 0, /* @bf_getwritebuffer@ */ | |
511 | wbuf_pysegcount, /* @bf_getsegcount@ */ | |
512 | 0 /* @bf_getcharbuffer@ */ | |
513 | }; | |
514 | ||
c263b05c | 515 | static const PyTypeObject wbuf_pytype_skel = { |
591bf41b | 516 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 517 | "WriteBuffer", /* @tp_name@ */ |
46e6ad89 | 518 | sizeof(buf_pyobj), /* @tp_basicsize@ */ |
519 | 0, /* @tp_itemsize@ */ | |
520 | ||
521 | buf_pydealloc, /* @tp_dealloc@ */ | |
522 | 0, /* @tp_print@ */ | |
523 | 0, /* @tp_getattr@ */ | |
524 | 0, /* @tp_setattr@ */ | |
525 | 0, /* @tp_compare@ */ | |
526 | 0, /* @tp_repr@ */ | |
527 | 0, /* @tp_as_number@ */ | |
528 | 0, /* @tp_as_sequence@ */ | |
529 | 0, /* @tp_as_mapping@ */ | |
530 | 0, /* @tp_hash@ */ | |
531 | 0, /* @tp_call@ */ | |
532 | 0, /* @tp_str@ */ | |
533 | 0, /* @tp_getattro@ */ | |
534 | 0, /* @tp_setattro@ */ | |
c90f712e | 535 | PYBUFFER(wbuf), /* @tp_as_buffer@ */ |
46e6ad89 | 536 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
537 | Py_TPFLAGS_BASETYPE, | |
538 | ||
539 | /* @tp_doc@ */ | |
d96c882e | 540 | "WriteBuffer([size = ?]): a write buffer.", |
46e6ad89 | 541 | |
542 | 0, /* @tp_traverse@ */ | |
543 | 0, /* @tp_clear@ */ | |
544 | 0, /* @tp_richcompare@ */ | |
545 | 0, /* @tp_weaklistoffset@ */ | |
546 | 0, /* @tp_iter@ */ | |
963a6148 | 547 | 0, /* @tp_iternext@ */ |
c90f712e | 548 | PYMETHODS(wbuf), /* @tp_methods@ */ |
46e6ad89 | 549 | 0, /* @tp_members@ */ |
c90f712e | 550 | PYGETSET(wbuf), /* @tp_getset@ */ |
46e6ad89 | 551 | 0, /* @tp_base@ */ |
552 | 0, /* @tp_dict@ */ | |
553 | 0, /* @tp_descr_get@ */ | |
554 | 0, /* @tp_descr_set@ */ | |
555 | 0, /* @tp_dictoffset@ */ | |
556 | 0, /* @tp_init@ */ | |
557 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
558 | wbuf_pynew, /* @tp_new@ */ | |
559 | 0, /* @tp_free@ */ | |
560 | 0 /* @tp_is_gc@ */ | |
561 | }; | |
562 | ||
563 | /*----- Initialization ----------------------------------------------------*/ | |
564 | ||
dc088b4d MW |
565 | PyObject *buferr; |
566 | ||
46e6ad89 | 567 | void buffer_pyinit(void) |
568 | { | |
569 | INITTYPE(rbuf, root); | |
570 | INITTYPE(wbuf, root); | |
571 | } | |
572 | ||
573 | void buffer_pyinsert(PyObject *mod) | |
574 | { | |
575 | INSEXC("BufferError", buferr, PyExc_Exception, 0); | |
576 | INSERT("ReadBuffer", rbuf_pytype); | |
577 | INSERT("WriteBuffer", wbuf_pytype); | |
578 | } | |
579 | ||
580 | /*----- That's all, folks -------------------------------------------------*/ |