3 * Reading and writing buffers of stuff
5 * (c) 2005 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- Data structures ---------------------------------------------------*/
33 typedef struct buf_pyobj {
40 static PyTypeObject *rbuf_pytype, *wbuf_pytype;
41 #define RBUF_PYCHECK(o) PyObject_TypeCheck((o), rbuf_pytype)
42 #define WBUF_PYCHECK(o) PyObject_TypeCheck((o), wbuf_pytype)
43 #define BUF_B(o) (&((buf_pyobj *)(o))->b)
44 #define BUF_SUB(o) (((buf_pyobj *)(o))->sub)
45 #define BUF_LK(o) (((buf_pyobj *)(o))->lk)
47 /*----- Exceptions --------------------------------------------------------*/
49 static PyObject *buferr;
51 #define BUFERR(str) do { PyErr_SetString(buferr, str); goto end; } while (0)
53 /*----- Read buffers ------------------------------------------------------*/
55 static PyObject *rbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
60 static const char *const kwlist[] = { "data", 0 };
62 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &in))
65 memcpy(q, in.p, in.sz);
66 me = (buf_pyobj *)ty->tp_alloc(ty, 0);
67 me->sub = 0; me->lk = 0;
68 buf_init(&me->b, q, in.sz);
70 return ((PyObject *)me);
73 static void buf_pydealloc(PyObject *me)
76 if (BUF_SUB(me)) Py_DECREF(BUF_SUB(me));
77 else xfree(BBASE(BUF_B(me)));
81 static Py_ssize_t rbuf_pysegcount(PyObject *me, Py_ssize_t *nn)
82 { if (nn) *nn = BSZ(BUF_B(me)); return (1); }
84 static Py_ssize_t rbuf_pyreadbuf(PyObject *me, Py_ssize_t seg, void **q)
85 { assert(seg == 0); *q = BCUR(BUF_B(me)); return (BLEFT(BUF_B(me))); }
87 static PyObject *rbmeth_skip(PyObject *me, PyObject *arg)
91 if (!PyArg_ParseTuple(arg, "O&:skip", convszt, &n)) goto end;
92 if (!buf_get(BUF_B(me), n)) BUFERR("buffer exhausted");
98 static PyObject *rbmeth_get(PyObject *me, PyObject *arg)
103 if (!PyArg_ParseTuple(arg, "O&:get", convszt, &n)) goto end;
104 if ((p = buf_get(BUF_B(me), n)) == 0) BUFERR("buffer exhausted");
105 return (bytestring_pywrap(p, n));
110 #define RBMETH_GETU_(n, W, w) \
111 static PyObject *rbmeth_getu##w(PyObject *me) \
114 if (buf_getu##w(BUF_B(me), &x)) BUFERR("buffer exhausted"); \
115 if (MASK##W <= ULONG_MAX) return (getulong(x)); \
116 else { kludge64 y; ASSIGN64(y, x); return (getk64(y)); } \
120 DOUINTCONV(RBMETH_GETU_)
122 #define RBMETH_GETBLK_(n, W, w) \
123 static PyObject *rbmeth_getblk##w(PyObject *me) \
127 if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) \
128 BUFERR("buffer exhausted"); \
129 return (bytestring_pywrap(q, sz)); \
133 BUF_DOSUFFIXES(RBMETH_GETBLK_)
135 #define RBMETH_GETBUF_(n, W, w) \
136 static PyObject *rbmeth_getbuf##w(PyObject *me) \
140 if (buf_getbuf##w(BUF_B(me), &bb)) BUFERR("buffer exhausted"); \
141 b = PyObject_NEW(buf_pyobj, rbuf_pytype); \
146 return ((PyObject *)b); \
150 BUF_DOSUFFIXES(RBMETH_GETBUF_)
152 static PyObject *rbmeth_getmp(PyObject *me)
155 if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR("buffer exhausted");
156 return (mp_pywrap(x));
161 static PyObject *rbmeth_getgf(PyObject *me)
164 if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR("buffer exhausted");
165 return (gf_pywrap(x));
170 static PyObject *rbmeth_getecpt(PyObject *me, PyObject *arg, PyObject *kw)
172 PyObject *cobj = Py_None;
173 static const char *const kwlist[] = { "curve", 0 };
175 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:getecpt", KWLIST, &cobj))
177 if (cobj == Py_None) cobj = (PyObject *)ecpt_pytype;
178 if (!PyType_Check(cobj) ||
179 !PyType_IsSubtype((PyTypeObject *)cobj, ecpt_pytype))
180 TYERR("expected elliptic curve type");
181 if (buf_getec(BUF_B(me), &pt)) BUFERR("buffer exhausted");
182 return (ecpt_pywrapout(cobj, &pt));
187 static PyObject *rbmeth_getecptraw(PyObject *me, PyObject *arg)
192 if (!PyArg_ParseTuple(arg, "O!:getecptraw", eccurve_pytype, &cobj))
194 if (ec_getraw(ECCURVE_C(cobj), BUF_B(me), &pt)) BUFERR("buffer exhausted");
195 rc = ecpt_pywrapout(cobj, &pt);
200 static PyObject *rbmeth_getge(PyObject *me, PyObject *arg)
204 if (!PyArg_ParseTuple(arg, "O!:getge", group_pytype, &gobj)) goto end;
205 x = G_CREATE(GROUP_G(gobj));
206 if (G_FROMBUF(GROUP_G(gobj), BUF_B(me), x)) BUFERR("buffer exhausted");
207 return (ge_pywrap(gobj, x));
209 if (x) G_DESTROY(GROUP_G(gobj), x);
213 static PyObject *rbmeth_getgeraw(PyObject *me, PyObject *arg)
217 if (!PyArg_ParseTuple(arg, "O!:getgeraw", group_pytype, &gobj)) goto end;
218 x = G_CREATE(GROUP_G(gobj));
219 if (G_FROMRAW(GROUP_G(gobj), BUF_B(me), x)) BUFERR("buffer exhausted");
220 return (ge_pywrap(gobj, x));
222 if (x) G_DESTROY(GROUP_G(gobj), x);
226 static PyObject *rbget_size(PyObject *me, void *hunoz)
227 { return (PyInt_FromLong(BSZ(BUF_B(me)))); }
228 static PyObject *rbget_left(PyObject *me, void *hunoz)
229 { return (PyInt_FromLong(BLEFT(BUF_B(me)))); }
230 static PyObject *rbget_endp(PyObject *me, void *hunoz)
231 { return (getbool(!BLEFT(BUF_B(me)))); }
232 static PyObject *rbget_offset(PyObject *me, void *hunoz)
233 { return (PyInt_FromLong(BLEN(BUF_B(me)))); }
234 static int rbset_offset(PyObject *me, PyObject *x, void *hunoz)
237 if (!x) NIERR("__del__");
238 if (!convszt(x, &n)) goto end;
239 if (n > BSZ(BUF_B(me))) VALERR("out of range");
240 BCUR(BUF_B(me)) = BBASE(BUF_B(me)) + n;
246 static const PyGetSetDef rbuf_pygetset[] = {
247 #define GETSETNAME(op, name) rb##op##_##name
248 GET (size, "RBUF.size -> SIZE")
249 GET (left, "RBUF.left -> REMAINDER")
250 GET (endp, "RBUF.endp -> BOOL")
251 GETSET(offset, "RBUF.offset -> OFFSET")
256 static const PyMethodDef rbuf_pymethods[] = {
257 #define METHNAME(func) rbmeth_##func
258 METH (skip, "RBUF.skip(N)")
259 METH (get, "RBUF.get(N) -> BYTES")
260 #define RBMETH_DECL_GETU_(n, W, w) \
261 NAMETH(getu##w, "RBUF.getu" #w "() -> INT")
262 DOUINTCONV(RBMETH_DECL_GETU_)
263 #define RBMETH_DECL_GETBLK_(n, W, w) \
264 NAMETH(getblk##w, "RBUF.getblk" #w "() -> BYTES")
265 BUF_DOSUFFIXES(RBMETH_DECL_GETBLK_)
266 #define RBMETH_DECL_GETBUF_(n, W, w) \
267 NAMETH(getbuf##w, "RBUF.getbuf" #w "() -> RBUF'")
268 BUF_DOSUFFIXES(RBMETH_DECL_GETBUF_)
269 NAMETH(getmp, "RBUF.getmp() -> X")
270 NAMETH(getgf, "RBUF.getgf() -> X")
271 KWMETH(getecpt, "RBUF.getecpt([curve = None]) -> P")
272 METH (getecptraw, "RBUF.getecptraw(CURVE) -> P")
273 METH (getge, "RBUF.getge(GROUP) -> X")
274 METH (getgeraw, "RBUF.getgeraw(GROUP) -> X")
279 static const PyBufferProcs rbuf_pybuffer = {
280 rbuf_pyreadbuf, /* @bf_getreadbuffer@ */
281 0, /* @bf_getwritebuffer@ */
282 rbuf_pysegcount, /* @bf_getsegcount@ */
283 0 /* @bf_getcharbuffer@ */
286 static const PyTypeObject rbuf_pytype_skel = {
287 PyVarObject_HEAD_INIT(0, 0) /* Header */
288 "ReadBuffer", /* @tp_name@ */
289 sizeof(buf_pyobj), /* @tp_basicsize@ */
290 0, /* @tp_itemsize@ */
292 buf_pydealloc, /* @tp_dealloc@ */
294 0, /* @tp_getattr@ */
295 0, /* @tp_setattr@ */
296 0, /* @tp_compare@ */
298 0, /* @tp_as_number@ */
299 0, /* @tp_as_sequence@ */
300 0, /* @tp_as_mapping@ */
304 0, /* @tp_getattro@ */
305 0, /* @tp_setattro@ */
306 PYBUFFER(rbuf), /* @tp_as_buffer@ */
307 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
311 "ReadBuffer(STR): a read buffer.",
313 0, /* @tp_traverse@ */
315 0, /* @tp_richcompare@ */
316 0, /* @tp_weaklistoffset@ */
318 0, /* @tp_iternext@ */
319 PYMETHODS(rbuf), /* @tp_methods@ */
320 0, /* @tp_members@ */
321 PYGETSET(rbuf), /* @tp_getset@ */
324 0, /* @tp_descr_get@ */
325 0, /* @tp_descr_set@ */
326 0, /* @tp_dictoffset@ */
328 PyType_GenericAlloc, /* @tp_alloc@ */
329 rbuf_pynew, /* @tp_new@ */
334 /*----- Write buffers -----------------------------------------------------*/
336 static int ensure(PyObject *me, size_t n)
341 size_t want = BLEN(b) + n;
346 BUFERR("buffer locked");
348 while (nn < want) nn <<= 1;
349 p = xrealloc(BBASE(b), nn, BSZ(b));
350 BCUR(b) = p + BLEN(b);
359 static PyObject *wbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
364 static const char *const kwlist[] = { "size", 0 };
366 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST,
369 me = (buf_pyobj *)ty->tp_alloc(ty, 0);
371 me->sub = 0; me->lk = 0;
372 buf_init(&me->b, p, n);
374 return ((PyObject *)me);
377 static Py_ssize_t wbuf_pysegcount(PyObject *me, Py_ssize_t *nn)
378 { if (nn) *nn = BLEN(BUF_B(me)); return (1); }
380 static Py_ssize_t wbuf_pyreadbuf(PyObject *me, Py_ssize_t seg, void **q)
381 { assert(seg == 0); *q = BBASE(BUF_B(me)); return (BLEN(BUF_B(me))); }
383 static PyObject *wbmeth_zero(PyObject *me, PyObject *arg)
387 if (!PyArg_ParseTuple(arg, "O&:zero", convszt, &n)) return (0);
388 if (ensure(me, n)) return (0);
389 p = buf_get(BUF_B(me), n); assert(p && BOK(BUF_B(me)));
394 static PyObject *wbmeth_put(PyObject *me, PyObject *arg)
397 if (!PyArg_ParseTuple(arg, "O&:put", convbin, &in)) return (0);
398 if (ensure(me, in.sz)) return (0);
399 buf_put(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me)));
403 #define WBMETH_PUTU_(n, W, w) \
404 static PyObject *wbmeth_putu##w(PyObject *me, PyObject *arg) \
407 if (!PyArg_ParseTuple(arg, "O&:putu" #w, convu##n, &i)) return (0); \
408 if (ensure(me, SZ_##n)) return (0); \
409 buf_putu##w(BUF_B(me), i); assert(BOK(BUF_B(me))); \
412 DOUINTCONV(WBMETH_PUTU_)
416 #define WBMETH_PUTBLK_(n, W, w) \
417 static PyObject *wbmeth_putblk##w(PyObject *me, PyObject *arg) \
420 if (!PyArg_ParseTuple(arg, "O&:putblk" #w, convbin, &in)) goto end; \
421 if (MASK##W && in.sz > MASK##W) VALERR("too large"); \
422 if (ensure(me, in.sz + SZ_##n)) return (0); \
423 buf_putmem##w(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me))); \
428 BUF_DOSUFFIXES(WBMETH_PUTBLK_)
430 static PyObject *wbmeth_putmp(PyObject *me, PyObject *arg)
433 if (!PyArg_ParseTuple(arg, "O&:putmp", convmp, &x)) return (0);
434 if (ensure(me, mp_octets(x) + 2)) return (0);
435 buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me)));
439 static PyObject *wbmeth_putgf(PyObject *me, PyObject *arg)
442 if (!PyArg_ParseTuple(arg, "O&:putgf", convgf, &x)) return (0);
443 if (ensure(me, mp_octets(x) + 2)) return (0);
444 buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me)));
449 static PyObject *wbmeth_putecpt(PyObject *me, PyObject *arg)
452 if (!PyArg_ParseTuple(arg, "O&:putecpt", convecpt, &pt)) return (0);
453 if (ensure(me, EC_ATINF(&pt) ? 2 : 6 + mp_octets(pt.x) + mp_octets(pt.y)))
455 buf_putec(BUF_B(me), &pt); assert(BOK(BUF_B(me)));
460 static PyObject *wbmeth_putecptraw(PyObject *me, PyObject *arg)
464 if (!PyArg_ParseTuple(arg, "O!:putecptraw", ecptcurve_pytype, &ptobj))
466 EC_OUT(ECPT_C(ptobj), &pt, ECPT_P(ptobj));
467 if (ensure(me, ECPT_C(ptobj)->f->noctets * 2 + 1)) return (0);
468 ec_putraw(ECPT_C(ptobj), BUF_B(me), &pt); assert(BOK(BUF_B(me)));
473 static PyObject *wbmeth_putge(PyObject *me, PyObject *arg)
476 if (!PyArg_ParseTuple(arg, "O!:putge", ge_pytype, &geobj)) return (0);
477 if (ensure(me, GE_G(geobj)->noctets)) return (0);
478 G_TOBUF(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me)));
482 static PyObject *wbmeth_putgeraw(PyObject *me, PyObject *arg)
485 if (!PyArg_ParseTuple(arg, "O!:putgeraw", ge_pytype, &geobj)) return (0);
486 if (ensure(me, GE_G(geobj)->noctets)) return (0);
487 G_TORAW(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me)));
491 static PyObject *wbget_size(PyObject *me, void *hunoz)
492 { return (PyInt_FromLong(BLEN(BUF_B(me)))); }
494 static PyObject *wbget_contents(PyObject *me, void *hunoz)
495 { return (bytestring_pywrap(BBASE(BUF_B(me)), BLEN(BUF_B(me)))); }
497 static const PyGetSetDef wbuf_pygetset[] = {
498 #define GETSETNAME(op, name) wb##op##_##name
499 GET (size, "WBUF.size -> SIZE")
500 GET (contents, "WBUF.contents -> STR")
505 static const PyMethodDef wbuf_pymethods[] = {
506 #define METHNAME(func) wbmeth_##func
507 METH (zero, "WBUF.zero(N)")
508 METH (put, "WBUF.put(BYTES)")
509 #define WBMETH_DECL_PUTU_(n, W, w) \
510 METH(putu##w, "WBUF.putu" #w "(INT)")
511 DOUINTCONV(WBMETH_DECL_PUTU_)
512 #define WBMETH_DECL_PUTBLK_(n, W, w) \
513 METH(putblk##w, "WBUF.putblk" #w "(BYTES)")
514 BUF_DOSUFFIXES(WBMETH_DECL_PUTBLK_)
515 METH (putmp, "WBUF.putmp(X)")
516 METH (putgf, "WBUF.putgf(X)")
517 METH (putecpt, "WBUF.putecpt(P)")
518 METH (putecptraw, "WBUF.putecptraw(P)")
519 METH (putge, "WBUF.putge(X)")
520 METH (putgeraw, "WBUF.putgeraw(X)")
525 static const PyBufferProcs wbuf_pybuffer = {
526 wbuf_pyreadbuf, /* @bf_getreadbuffer@ */
527 0, /* @bf_getwritebuffer@ */
528 wbuf_pysegcount, /* @bf_getsegcount@ */
529 0 /* @bf_getcharbuffer@ */
532 static const PyTypeObject wbuf_pytype_skel = {
533 PyVarObject_HEAD_INIT(0, 0) /* Header */
534 "WriteBuffer", /* @tp_name@ */
535 sizeof(buf_pyobj), /* @tp_basicsize@ */
536 0, /* @tp_itemsize@ */
538 buf_pydealloc, /* @tp_dealloc@ */
540 0, /* @tp_getattr@ */
541 0, /* @tp_setattr@ */
542 0, /* @tp_compare@ */
544 0, /* @tp_as_number@ */
545 0, /* @tp_as_sequence@ */
546 0, /* @tp_as_mapping@ */
550 0, /* @tp_getattro@ */
551 0, /* @tp_setattro@ */
552 PYBUFFER(wbuf), /* @tp_as_buffer@ */
553 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
557 "WriteBuffer([size = ?]): a write buffer.",
559 0, /* @tp_traverse@ */
561 0, /* @tp_richcompare@ */
562 0, /* @tp_weaklistoffset@ */
564 0, /* @tp_iternext@ */
565 PYMETHODS(wbuf), /* @tp_methods@ */
566 0, /* @tp_members@ */
567 PYGETSET(wbuf), /* @tp_getset@ */
570 0, /* @tp_descr_get@ */
571 0, /* @tp_descr_set@ */
572 0, /* @tp_dictoffset@ */
574 PyType_GenericAlloc, /* @tp_alloc@ */
575 wbuf_pynew, /* @tp_new@ */
580 /*----- Initialization ----------------------------------------------------*/
582 void buffer_pyinit(void)
584 INITTYPE(rbuf, root);
585 INITTYPE(wbuf, root);
588 void buffer_pyinsert(PyObject *mod)
590 INSEXC("BufferError", buferr, PyExc_Exception, 0);
591 INSERT("ReadBuffer", rbuf_pytype);
592 INSERT("WriteBuffer", wbuf_pytype);
595 /*----- That's all, folks -------------------------------------------------*/