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