5 * Reading and writing buffers of stuff
7 * (c) 2005 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Python interface to Catacomb.
14 * Catacomb/Python is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Catacomb/Python is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Catacomb/Python; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
31 #include "catacomb-python.h"
33 /*----- Data structures ---------------------------------------------------*/
35 typedef struct buf_pyobj {
41 static PyTypeObject *rbuf_pytype, *wbuf_pytype;
42 #define RBUF_PYCHECK(o) PyObject_TypeCheck((o), rbuf_pytype)
43 #define WBUF_PYCHECK(o) PyObject_TypeCheck((o), wbuf_pytype)
44 #define BUF_B(o) (&((buf_pyobj *)(o))->b)
45 #define BUF_SUB(o) (((buf_pyobj *)(o))->sub)
47 /*----- Exceptions --------------------------------------------------------*/
49 static PyObject *buferr;
51 #define BUFERR() do { PyErr_SetNone(buferr); goto end; } while (0)
53 /*----- Read buffers ------------------------------------------------------*/
55 static PyObject *rbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
60 static char *kwlist[] = { "data", 0 };
62 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &p, &n))
66 me = (buf_pyobj *)ty->tp_alloc(ty, 0);
68 buf_init(&me->b, q, n);
70 return ((PyObject *)me);
73 static void buf_pydealloc(PyObject *me)
76 Py_DECREF(BUF_SUB(me));
78 xfree(BBASE(BUF_B(me)));
82 static int rbuf_pysegcount(PyObject *me, int *nn)
83 { if (nn) *nn = BSZ(BUF_B(me)); return (1); }
85 static int rbuf_pyreadbuf(PyObject *me, int seg, void **q)
86 { assert(seg == 0); *q = BCUR(BUF_B(me)); return (BLEFT(BUF_B(me))); }
88 static PyObject *rbmeth_skip(PyObject *me, PyObject *arg)
92 if (!PyArg_ParseTuple(arg, "O&:skip", convszt, &n)) goto end;
93 if (!buf_get(BUF_B(me), n)) BUFERR();
99 static PyObject *rbmeth_get(PyObject *me, PyObject *arg)
104 if (!PyArg_ParseTuple(arg, "O&:get", convszt, &n)) goto end;
105 if ((p = buf_get(BUF_B(me), n)) == 0) BUFERR();
106 return (bytestring_pywrap(p, n));
111 #define RBMETH_GETU_(n, W, w) \
112 static PyObject *rbmeth_getu##w(PyObject *me, PyObject *arg) \
115 if (!PyArg_ParseTuple(arg, ":getu" #w)) goto end; \
116 if (buf_getu##w(BUF_B(me), &x)) BUFERR(); \
117 return (getu32(x)); \
121 DOUINTCONV(RBMETH_GETU_)
123 #define RBMETH_GETBLK_(n, W, w) \
124 static PyObject *rbmeth_getblk##w(PyObject *me, PyObject *arg) \
128 if (!PyArg_ParseTuple(arg, ":getblk" #w)) goto end; \
129 if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) BUFERR(); \
130 return (bytestring_pywrap(q, sz)); \
134 BUF_DOSUFFIXES(RBMETH_GETBLK_)
136 #define RBMETH_GETBUF_(n, W, w) \
137 static PyObject *rbmeth_getbuf##w(PyObject *me, PyObject *arg) \
141 if (!PyArg_ParseTuple(arg, ":getbuf" #w)) goto end; \
142 if (buf_getbuf##w(BUF_B(me), &bb)) BUFERR(); \
143 b = PyObject_NEW(buf_pyobj, rbuf_pytype); \
147 return ((PyObject *)b); \
151 BUF_DOSUFFIXES(RBMETH_GETBUF_)
153 static PyObject *rbmeth_getmp(PyObject *me, PyObject *arg)
156 if (!PyArg_ParseTuple(arg, ":getmp")) goto end;
157 if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR();
158 return (mp_pywrap(x));
163 static PyObject *rbmeth_getgf(PyObject *me, PyObject *arg)
166 if (!PyArg_ParseTuple(arg, ":getgf")) goto end;
167 if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR();
168 return (gf_pywrap(x));
173 static PyObject *rbmeth_getecpt(PyObject *me, PyObject *arg, PyObject *kw)
175 PyObject *cobj = Py_None;
176 static char *kwlist[] = { "curve", 0 };
178 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:getecpt", kwlist, &cobj))
180 if (cobj == Py_None) cobj = (PyObject *)ecpt_pytype;
181 if (!PyType_Check(cobj) ||
182 !PyType_IsSubtype((PyTypeObject *)cobj, ecpt_pytype))
183 TYERR("expected elliptic curve type");
184 if (buf_getec(BUF_B(me), &pt)) BUFERR();
185 return (ecpt_pywrapout(cobj, &pt));
190 static PyObject *rbmeth_getecptraw(PyObject *me, PyObject *arg)
192 PyTypeObject *cobj = ecpt_pytype;
194 if (!PyArg_ParseTuple(arg, "O!:getecptraw", eccurve_pytype, &cobj))
196 if (ec_getraw(ECCURVE_C(cobj), BUF_B(me), &pt)) BUFERR();
197 return (ecpt_pywrapout(cobj, &pt));
202 static PyObject *rbmeth_getge(PyObject *me, PyObject *arg)
206 if (!PyArg_ParseTuple(arg, "O!:getge", group_pytype, &gobj)) goto end;
207 x = G_CREATE(GROUP_G(gobj));
208 if (G_FROMBUF(GROUP_G(gobj), BUF_B(me), x)) BUFERR();
209 return (ge_pywrap(gobj, x));
211 if (x) G_DESTROY(GROUP_G(gobj), x);
215 static PyObject *rbmeth_getgeraw(PyObject *me, PyObject *arg)
219 if (!PyArg_ParseTuple(arg, "O!:getgeraw", group_pytype, &gobj)) goto end;
220 x = G_CREATE(GROUP_G(gobj));
221 if (G_FROMRAW(GROUP_G(gobj), BUF_B(me), x)) BUFERR();
222 return (ge_pywrap(gobj, x));
224 if (x) G_DESTROY(GROUP_G(gobj), x);
228 static PyObject *rbget_size(PyObject *me, void *hunoz)
229 { return (PyInt_FromLong(BSZ(BUF_B(me)))); }
230 static PyObject *rbget_left(PyObject *me, void *hunoz)
231 { return (PyInt_FromLong(BLEFT(BUF_B(me)))); }
232 static PyObject *rbget_endp(PyObject *me, void *hunoz)
233 { return (getbool(!BLEFT(BUF_B(me)))); }
234 static PyObject *rbget_offset(PyObject *me, void *hunoz)
235 { return (PyInt_FromLong(BLEN(BUF_B(me)))); }
236 static int rbset_offset(PyObject *me, PyObject *x, void *hunoz)
239 if (!x) NIERR("__del__");
240 if (!convszt(x, &n)) goto end;
241 if (n > BSZ(BUF_B(me))) VALERR("out of range");
242 BCUR(BUF_B(me)) = BBASE(BUF_B(me)) + n;
248 static PyGetSetDef rbuf_pygetset[] = {
249 #define GETSETNAME(op, name) rb##op##_##name
250 GET (size, "RBUF.size -> SIZE")
251 GET (left, "RBUF.left -> REMAINDER")
252 GET (endp, "RBUF.endp -> BOOL")
253 GETSET(offset, "RBUF.offset -> OFFSET")
258 static PyMethodDef rbuf_pymethods[] = {
259 #define METHNAME(func) rbmeth_##func
260 METH (skip, "RBUF.skip(N)")
261 METH (get, "RBUF.get(N) -> BYTES")
262 #define RBMETH_DECL_GETU_(n, W, w) \
263 METH(getu##w, "RBUF.getu" #w "() -> INT")
264 DOUINTCONV(RBMETH_DECL_GETU_)
265 #define RBMETH_DECL_GETBLK_(n, W, w) \
266 METH(getblk##w, "RBUF.getblk" #w "() -> INT")
267 BUF_DOSUFFIXES(RBMETH_DECL_GETBLK_)
268 #define RBMETH_DECL_GETBUF_(n, W, w) \
269 METH(getbuf##w, "RBUF.getbuf" #w "() -> INT")
270 BUF_DOSUFFIXES(RBMETH_DECL_GETBUF_)
271 METH (getmp, "RBUF.getmp() -> X")
272 METH (getgf, "RBUF.getgf() -> X")
273 KWMETH(getecpt, "RBUF.getecpt(curve = None) -> P")
274 METH (getecptraw, "RBUF.getecptraw(CURVE) -> P")
275 METH (getge, "RBUF.getge(GROUP) -> X")
276 METH (getgeraw, "RBUF.getgeraw(GROUP) -> X")
281 static PyBufferProcs rbuf_pybuffer = {
282 rbuf_pyreadbuf, /* @bf_getreadbuffer@ */
283 0, /* @bf_getwritebuffer@ */
284 rbuf_pysegcount, /* @bf_getsegcount@ */
285 0 /* @bf_getcharbuffer@ */
288 static PyTypeObject rbuf_pytype_skel = {
289 PyObject_HEAD_INIT(0) 0, /* Header */
290 "catacomb.ReadBuffer", /* @tp_name@ */
291 sizeof(buf_pyobj), /* @tp_basicsize@ */
292 0, /* @tp_itemsize@ */
294 buf_pydealloc, /* @tp_dealloc@ */
296 0, /* @tp_getattr@ */
297 0, /* @tp_setattr@ */
298 0, /* @tp_compare@ */
300 0, /* @tp_as_number@ */
301 0, /* @tp_as_sequence@ */
302 0, /* @tp_as_mapping@ */
306 0, /* @tp_getattro@ */
307 0, /* @tp_setattro@ */
308 &rbuf_pybuffer, /* @tp_as_buffer@ */
309 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
315 0, /* @tp_traverse@ */
317 0, /* @tp_richcompare@ */
318 0, /* @tp_weaklistoffset@ */
320 0, /* @tp_iternext@ */
321 rbuf_pymethods, /* @tp_methods@ */
322 0, /* @tp_members@ */
323 rbuf_pygetset, /* @tp_getset@ */
326 0, /* @tp_descr_get@ */
327 0, /* @tp_descr_set@ */
328 0, /* @tp_dictoffset@ */
330 PyType_GenericAlloc, /* @tp_alloc@ */
331 rbuf_pynew, /* @tp_new@ */
336 /*----- Write buffers -----------------------------------------------------*/
338 static void ensure(PyObject *me, size_t n)
345 size_t want = BLEFT(b) + n;
346 while (nn < want) nn <<= 1;
347 p = xrealloc(BBASE(b), nn, BSZ(b));
348 BCUR(b) = p + BLEN(b);
354 static PyObject *wbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
359 static char *kwlist[] = { "size", 0 };
361 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", kwlist,
364 me = (buf_pyobj *)ty->tp_alloc(ty, 0);
367 buf_init(&me->b, p, n);
369 return ((PyObject *)me);
372 static int wbuf_pysegcount(PyObject *me, int *nn)
373 { if (nn) *nn = BLEN(BUF_B(me)); return (1); }
375 static int wbuf_pyreadbuf(PyObject *me, int seg, void **q)
376 { assert(seg == 0); *q = BBASE(BUF_B(me)); return (BLEN(BUF_B(me))); }
378 static PyObject *wbmeth_zero(PyObject *me, PyObject *arg)
382 if (!PyArg_ParseTuple(arg, "O&:zero", convszt, &n)) return (0);
384 p = buf_get(BUF_B(me), n); assert(p && BOK(BUF_B(me)));
389 static PyObject *wbmeth_put(PyObject *me, PyObject *arg)
393 if (!PyArg_ParseTuple(arg, "s#:put", &p, &n)) return (0);
395 buf_put(BUF_B(me), p, n); assert(BOK(BUF_B(m)));
399 #define WBMETH_PUTU_(n, W, w) \
400 static PyObject *wbmeth_putu##w(PyObject *me, PyObject *arg) \
403 if (!PyArg_ParseTuple(arg, "O&:putu" #w, convu##n, &i)) return (0); \
404 ensure(me, SZ_##n); \
405 buf_putu##w(BUF_B(me), i); assert(BOK(BUF_B(me))); \
408 DOUINTCONV(WBMETH_PUTU_)
411 #define WBMETH_PUTBLK_(n, W, w) \
412 static PyObject *wbmeth_putblk##w(PyObject *me, PyObject *arg) \
416 if (!PyArg_ParseTuple(arg, "s#:putblk" #w, &p, &sz)) return (0); \
417 ensure(me, sz + SZ_##n); \
418 buf_putmem##w(BUF_B(me), p, sz); assert(BOK(BUF_B(me))); \
421 BUF_DOSUFFIXES(WBMETH_PUTBLK_)
423 static PyObject *wbmeth_putmp(PyObject *me, PyObject *arg)
426 if (!PyArg_ParseTuple(arg, "O&:putmp", convmp, &x)) return (0);
427 ensure(me, mp_octets(x) + 2);
428 buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me)));
432 static PyObject *wbmeth_putgf(PyObject *me, PyObject *arg)
435 if (!PyArg_ParseTuple(arg, "O&:putgf", convgf, &x)) return (0);
436 ensure(me, mp_octets(x) + 2);
437 buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me)));
442 static PyObject *wbmeth_putecpt(PyObject *me, PyObject *arg)
445 if (!PyArg_ParseTuple(arg, "O&:putecpt", convecpt, &pt)) return (0);
446 if (EC_ATINF(&pt)) ensure(me, 2);
447 else ensure(me, 4 + mp_octets(pt.x) + mp_octets(pt.y));
448 buf_putec(BUF_B(me), &pt); assert(BOK(BUF_B(me)));
453 static PyObject *wbmeth_putecptraw(PyObject *me, PyObject *arg)
457 if (!PyArg_ParseTuple(arg, "O!:putecptraw", ecptcurve_pytype, &ptobj))
459 EC_OUT(ECPT_C(ptobj), &pt, ECPT_P(ptobj));
460 ensure(me, ECPT_C(ptobj)->f->noctets * 2 + 1);
461 ec_putraw(ECPT_C(ptobj), BUF_B(me), &pt); assert(BOK(BUF_B(me)));
466 static PyObject *wbmeth_putge(PyObject *me, PyObject *arg)
469 if (!PyArg_ParseTuple(arg, "O!:putge", ge_pytype, &geobj)) return (0);
470 ensure(me, GE_G(geobj)->noctets);
471 G_TOBUF(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me)));
475 static PyObject *wbmeth_putgeraw(PyObject *me, PyObject *arg)
478 if (!PyArg_ParseTuple(arg, "O!:putgeraw", ge_pytype, &geobj)) return (0);
479 ensure(me, GE_G(geobj)->noctets);
480 G_TORAW(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me)));
484 static PyObject *wbget_size(PyObject *me, void *hunoz)
485 { return (PyInt_FromLong(BLEN(BUF_B(me)))); }
487 static PyGetSetDef wbuf_pygetset[] = {
488 #define GETSETNAME(op, name) wb##op##_##name
489 GET (size, "WBUF.size -> SIZE")
494 static PyMethodDef wbuf_pymethods[] = {
495 #define METHNAME(func) wbmeth_##func
496 METH (zero, "WBUF.skip(N)")
497 METH (put, "WBUF.put(BYTES)")
498 #define WBMETH_DECL_PUTU_(n, W, w) \
499 METH(putu##w, "WBUF.putu" #w "(INT)")
500 DOUINTCONV(WBMETH_DECL_PUTU_)
501 #define WBMETH_DECL_PUTBLK_(n, W, w) \
502 METH(putblk##w, "WBUF.putblk" #w "(BYTES)")
503 BUF_DOSUFFIXES(WBMETH_DECL_PUTBLK_)
504 METH (putmp, "WBUF.putmp(X)")
505 METH (putgf, "WBUF.putgf(X)")
506 KWMETH(putecpt, "WBUF.putecpt(P)")
507 METH (putecptraw, "WBUF.putecptraw(P)")
508 METH (putge, "WBUF.putge(X)")
509 METH (putgeraw, "WBUF.putgeraw(X)")
514 static PyBufferProcs wbuf_pybuffer = {
515 wbuf_pyreadbuf, /* @bf_getreadbuffer@ */
516 0, /* @bf_getwritebuffer@ */
517 wbuf_pysegcount, /* @bf_getsegcount@ */
518 0 /* @bf_getcharbuffer@ */
521 static PyTypeObject wbuf_pytype_skel = {
522 PyObject_HEAD_INIT(0) 0, /* Header */
523 "catacomb.WriteBuffer", /* @tp_name@ */
524 sizeof(buf_pyobj), /* @tp_basicsize@ */
525 0, /* @tp_itemsize@ */
527 buf_pydealloc, /* @tp_dealloc@ */
529 0, /* @tp_getattr@ */
530 0, /* @tp_setattr@ */
531 0, /* @tp_compare@ */
533 0, /* @tp_as_number@ */
534 0, /* @tp_as_sequence@ */
535 0, /* @tp_as_mapping@ */
539 0, /* @tp_getattro@ */
540 0, /* @tp_setattro@ */
541 &wbuf_pybuffer, /* @tp_as_buffer@ */
542 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
548 0, /* @tp_traverse@ */
550 0, /* @tp_richcompare@ */
551 0, /* @tp_weaklistoffset@ */
553 0, /* @tp_iternext@ */
554 wbuf_pymethods, /* @tp_methods@ */
555 0, /* @tp_members@ */
556 wbuf_pygetset, /* @tp_getset@ */
559 0, /* @tp_descr_get@ */
560 0, /* @tp_descr_set@ */
561 0, /* @tp_dictoffset@ */
563 PyType_GenericAlloc, /* @tp_alloc@ */
564 wbuf_pynew, /* @tp_new@ */
569 /*----- Initialization ----------------------------------------------------*/
571 void buffer_pyinit(void)
573 INITTYPE(rbuf, root);
574 INITTYPE(wbuf, root);
577 void buffer_pyinsert(PyObject *mod)
579 INSEXC("BufferError", buferr, PyExc_Exception, 0);
580 INSERT("ReadBuffer", rbuf_pytype);
581 INSERT("WriteBuffer", wbuf_pytype);
584 /*----- That's all, folks -------------------------------------------------*/