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 /*----- Read buffers ------------------------------------------------------*/
33 PyTypeObject *rbuf_pytype;
35 static PyObject *rbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
40 static const char *const kwlist[] = { "data", 0 };
42 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &in))
45 memcpy(q, in.p, in.sz);
46 me = (buf_pyobj *)ty->tp_alloc(ty, 0);
47 me->sub = 0; me->lk = 0;
48 buf_init(&me->b, q, in.sz);
50 return ((PyObject *)me);
53 static void buf_pydealloc(PyObject *me)
56 if (BUF_SUB(me)) Py_DECREF(BUF_SUB(me));
57 else xfree(BBASE(BUF_B(me)));
62 static int rbuf_pygetbuf(PyObject *me, Py_buffer *vw, int f)
65 return (PyBuffer_FillInfo(vw, me, BCUR(b), BLEFT(b), 1, f));
68 static Py_ssize_t rbuf_pysegcount(PyObject *me, Py_ssize_t *nn)
69 { if (nn) *nn = BSZ(BUF_B(me)); return (1); }
70 static Py_ssize_t rbuf_pyreadbuf(PyObject *me, Py_ssize_t seg, void **q)
71 { assert(seg == 0); *q = BCUR(BUF_B(me)); return (BLEFT(BUF_B(me))); }
74 static PyObject *rbmeth_skip(PyObject *me, PyObject *arg)
78 if (!PyArg_ParseTuple(arg, "O&:skip", convszt, &n)) goto end;
79 if (!buf_get(BUF_B(me), n)) BUFERR("buffer exhausted");
85 static PyObject *rbmeth_get(PyObject *me, PyObject *arg)
90 if (!PyArg_ParseTuple(arg, "O&:get", convszt, &n)) goto end;
91 if ((p = buf_get(BUF_B(me), n)) == 0) BUFERR("buffer exhausted");
92 return (bytestring_pywrap(p, n));
97 #define RBMETH_GETU_(n, W, w) \
98 static PyObject *rbmeth_getu##w(PyObject *me) \
101 if (buf_getu##w(BUF_B(me), &x)) BUFERR("buffer exhausted"); \
102 if (MASK##W <= ULONG_MAX) return (getulong(x)); \
103 else { kludge64 y; ASSIGN64(y, x); return (getk64(y)); } \
107 DOUINTCONV(RBMETH_GETU_)
109 #define RBMETH_GETBLK_(n, W, w) \
110 static PyObject *rbmeth_getblk##w(PyObject *me) \
114 if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) \
115 BUFERR("buffer exhausted"); \
116 return (bytestring_pywrap(q, sz)); \
120 BUF_DOSUFFIXES(RBMETH_GETBLK_)
122 #define RBMETH_GETBUF_(n, W, w) \
123 static PyObject *rbmeth_getbuf##w(PyObject *me) \
127 if (buf_getbuf##w(BUF_B(me), &bb)) BUFERR("buffer exhausted"); \
128 b = PyObject_NEW(buf_pyobj, rbuf_pytype); \
133 return ((PyObject *)b); \
137 BUF_DOSUFFIXES(RBMETH_GETBUF_)
139 static PyObject *rbmeth_getmp(PyObject *me)
142 if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR("buffer exhausted");
143 return (mp_pywrap(x));
148 static PyObject *rbmeth_getgf(PyObject *me)
151 if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR("buffer exhausted");
152 return (gf_pywrap(x));
157 static PyObject *rbmeth_getecpt(PyObject *me, PyObject *arg, PyObject *kw)
159 PyObject *cobj = Py_None;
160 static const char *const kwlist[] = { "curve", 0 };
162 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:getecpt", KWLIST, &cobj))
164 if (cobj == Py_None) cobj = (PyObject *)ecpt_pytype;
165 if (!PyType_Check(cobj) ||
166 !PyType_IsSubtype((PyTypeObject *)cobj, ecpt_pytype))
167 TYERR("expected elliptic curve type");
168 if (buf_getec(BUF_B(me), &pt)) BUFERR("buffer exhausted");
169 return (ecpt_pywrapout(cobj, &pt));
174 static PyObject *rbmeth_getecptraw(PyObject *me, PyObject *arg)
179 if (!PyArg_ParseTuple(arg, "O!:getecptraw", eccurve_pytype, &cobj))
181 if (ec_getraw(ECCURVE_C(cobj), BUF_B(me), &pt)) BUFERR("buffer exhausted");
182 rc = ecpt_pywrapout(cobj, &pt);
187 static PyObject *rbmeth_getge(PyObject *me, PyObject *arg)
191 if (!PyArg_ParseTuple(arg, "O!:getge", group_pytype, &gobj)) goto end;
192 x = G_CREATE(GROUP_G(gobj));
193 if (G_FROMBUF(GROUP_G(gobj), BUF_B(me), x)) BUFERR("buffer exhausted");
194 return (ge_pywrap(gobj, x));
196 if (x) G_DESTROY(GROUP_G(gobj), x);
200 static PyObject *rbmeth_getgeraw(PyObject *me, PyObject *arg)
204 if (!PyArg_ParseTuple(arg, "O!:getgeraw", group_pytype, &gobj)) goto end;
205 x = G_CREATE(GROUP_G(gobj));
206 if (G_FROMRAW(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 *rbget_size(PyObject *me, void *hunoz)
214 { return (PyInt_FromLong(BSZ(BUF_B(me)))); }
215 static PyObject *rbget_left(PyObject *me, void *hunoz)
216 { return (PyInt_FromLong(BLEFT(BUF_B(me)))); }
217 static PyObject *rbget_endp(PyObject *me, void *hunoz)
218 { return (getbool(!BLEFT(BUF_B(me)))); }
219 static PyObject *rbget_offset(PyObject *me, void *hunoz)
220 { return (PyInt_FromLong(BLEN(BUF_B(me)))); }
221 static int rbset_offset(PyObject *me, PyObject *x, void *hunoz)
224 if (!x) NIERR("__del__");
225 if (!convszt(x, &n)) goto end;
226 if (n > BSZ(BUF_B(me))) VALERR("out of range");
227 BCUR(BUF_B(me)) = BBASE(BUF_B(me)) + n;
233 static const PyGetSetDef rbuf_pygetset[] = {
234 #define GETSETNAME(op, name) rb##op##_##name
235 GET (size, "RBUF.size -> SIZE")
236 GET (left, "RBUF.left -> REMAINDER")
237 GET (endp, "RBUF.endp -> BOOL")
238 GETSET(offset, "RBUF.offset -> OFFSET")
243 static const PyMethodDef rbuf_pymethods[] = {
244 #define METHNAME(func) rbmeth_##func
245 METH (skip, "RBUF.skip(N)")
246 METH (get, "RBUF.get(N) -> BYTES")
247 #define RBMETH_DECL_GETU_(n, W, w) \
248 NAMETH(getu##w, "RBUF.getu" #w "() -> INT")
249 DOUINTCONV(RBMETH_DECL_GETU_)
250 #define RBMETH_DECL_GETBLK_(n, W, w) \
251 NAMETH(getblk##w, "RBUF.getblk" #w "() -> BYTES")
252 BUF_DOSUFFIXES(RBMETH_DECL_GETBLK_)
253 #define RBMETH_DECL_GETBUF_(n, W, w) \
254 NAMETH(getbuf##w, "RBUF.getbuf" #w "() -> RBUF'")
255 BUF_DOSUFFIXES(RBMETH_DECL_GETBUF_)
256 NAMETH(getmp, "RBUF.getmp() -> X")
257 NAMETH(getgf, "RBUF.getgf() -> X")
258 KWMETH(getecpt, "RBUF.getecpt([curve = None]) -> P")
259 METH (getecptraw, "RBUF.getecptraw(CURVE) -> P")
260 METH (getge, "RBUF.getge(GROUP) -> X")
261 METH (getgeraw, "RBUF.getgeraw(GROUP) -> X")
266 static const PyBufferProcs rbuf_pybuffer = {
268 rbuf_pygetbuf, /* @bf_getbuffer@ */
269 0, /* @bf_releasebuffer@ */
271 rbuf_pyreadbuf, /* @bf_getreadbuffer@ */
272 0, /* @bf_getwritebuffer@ */
273 rbuf_pysegcount, /* @bf_getsegcount@ */
274 0 /* @bf_getcharbuffer@ */
278 static const PyTypeObject rbuf_pytype_skel = {
279 PyVarObject_HEAD_INIT(0, 0) /* Header */
280 "ReadBuffer", /* @tp_name@ */
281 sizeof(buf_pyobj), /* @tp_basicsize@ */
282 0, /* @tp_itemsize@ */
284 buf_pydealloc, /* @tp_dealloc@ */
286 0, /* @tp_getattr@ */
287 0, /* @tp_setattr@ */
288 0, /* @tp_compare@ */
290 0, /* @tp_as_number@ */
291 0, /* @tp_as_sequence@ */
292 0, /* @tp_as_mapping@ */
296 0, /* @tp_getattro@ */
297 0, /* @tp_setattro@ */
298 PYBUFFER(rbuf), /* @tp_as_buffer@ */
299 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
303 "ReadBuffer(STR): a read buffer.",
305 0, /* @tp_traverse@ */
307 0, /* @tp_richcompare@ */
308 0, /* @tp_weaklistoffset@ */
310 0, /* @tp_iternext@ */
311 PYMETHODS(rbuf), /* @tp_methods@ */
312 0, /* @tp_members@ */
313 PYGETSET(rbuf), /* @tp_getset@ */
316 0, /* @tp_descr_get@ */
317 0, /* @tp_descr_set@ */
318 0, /* @tp_dictoffset@ */
320 PyType_GenericAlloc, /* @tp_alloc@ */
321 rbuf_pynew, /* @tp_new@ */
326 /*----- Write buffers -----------------------------------------------------*/
328 PyTypeObject *wbuf_pytype;
330 int ensurebuf(PyObject *me, size_t n)
335 size_t want = BLEN(b) + n;
340 BUFERR("buffer locked");
342 while (nn < want) nn <<= 1;
343 p = xrealloc(BBASE(b), nn, BSZ(b));
344 BCUR(b) = p + BLEN(b);
353 static PyObject *wbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
358 static const char *const kwlist[] = { "size", 0 };
360 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST,
363 me = (buf_pyobj *)ty->tp_alloc(ty, 0);
365 me->sub = 0; me->lk = 0;
366 buf_init(&me->b, p, n);
368 return ((PyObject *)me);
372 static int wbuf_pygetbuf(PyObject *me, Py_buffer *vw, int f)
375 if (PyBuffer_FillInfo(vw, me, BBASE(b), BLEN(b), 0, f)) return (-1);
376 BUF_LK(me)++; return (0);
378 static void wbuf_pyrlsbuf(PyObject *me, Py_buffer *vw)
381 static Py_ssize_t wbuf_pysegcount(PyObject *me, Py_ssize_t *nn)
382 { if (nn) *nn = BLEN(BUF_B(me)); return (1); }
383 static Py_ssize_t wbuf_pyreadbuf(PyObject *me, Py_ssize_t seg, void **q)
384 { assert(seg == 0); *q = BBASE(BUF_B(me)); return (BLEN(BUF_B(me))); }
387 static PyObject *wbmeth_zero(PyObject *me, PyObject *arg)
391 if (!PyArg_ParseTuple(arg, "O&:zero", convszt, &n)) return (0);
392 if (ensurebuf(me, n)) return (0);
393 p = buf_get(BUF_B(me), n); assert(p && BOK(BUF_B(me)));
398 static PyObject *wbmeth_put(PyObject *me, PyObject *arg)
401 if (!PyArg_ParseTuple(arg, "O&:put", convbin, &in)) return (0);
402 if (ensurebuf(me, in.sz)) return (0);
403 buf_put(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me)));
407 #define WBMETH_PUTU_(n, W, w) \
408 static PyObject *wbmeth_putu##w(PyObject *me, PyObject *arg) \
411 if (!PyArg_ParseTuple(arg, "O&:putu" #w, convu##n, &i)) return (0); \
412 if (ensurebuf(me, SZ_##n)) return (0); \
413 buf_putu##w(BUF_B(me), i); assert(BOK(BUF_B(me))); \
416 DOUINTCONV(WBMETH_PUTU_)
420 #define WBMETH_PUTBLK_(n, W, w) \
421 static PyObject *wbmeth_putblk##w(PyObject *me, PyObject *arg) \
424 if (!PyArg_ParseTuple(arg, "O&:putblk" #w, convbin, &in)) goto end; \
425 if (MASK##W && in.sz > MASK##W) VALERR("too large"); \
426 if (ensurebuf(me, in.sz + SZ_##n)) return (0); \
427 buf_putmem##w(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me))); \
432 BUF_DOSUFFIXES(WBMETH_PUTBLK_)
434 static PyObject *wbmeth_putmp(PyObject *me, PyObject *arg)
437 if (!PyArg_ParseTuple(arg, "O&:putmp", convmp, &x)) return (0);
438 if (ensurebuf(me, mp_octets(x) + 2)) return (0);
439 buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me)));
443 static PyObject *wbmeth_putgf(PyObject *me, PyObject *arg)
446 if (!PyArg_ParseTuple(arg, "O&:putgf", convgf, &x)) return (0);
447 if (ensurebuf(me, mp_octets(x) + 2)) return (0);
448 buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me)));
453 static PyObject *wbmeth_putecpt(PyObject *me, PyObject *arg)
456 if (!PyArg_ParseTuple(arg, "O&:putecpt", convecpt, &pt)) return (0);
457 if (ensurebuf(me, EC_ATINF(&pt) ? 2 :
458 6 + mp_octets(pt.x) + mp_octets(pt.y)))
460 buf_putec(BUF_B(me), &pt); assert(BOK(BUF_B(me)));
465 static PyObject *wbmeth_putecptraw(PyObject *me, PyObject *arg)
470 if (!PyArg_ParseTuple(arg, "O!:putecptraw", ecptcurve_pytype, &ptobj))
473 EC_OUT(cc, &pt, ECPT_P(ptobj));
474 if (ensurebuf(me, 2*cc->f->noctets + 1)) return (0);
475 ec_putraw(cc, BUF_B(me), &pt); assert(BOK(BUF_B(me)));
480 static PyObject *wbmeth_putge(PyObject *me, PyObject *arg)
483 if (!PyArg_ParseTuple(arg, "O!:putge", ge_pytype, &geobj)) return (0);
484 if (ensurebuf(me, GE_G(geobj)->noctets)) return (0);
485 G_TOBUF(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me)));
489 static PyObject *wbmeth_putgeraw(PyObject *me, PyObject *arg)
492 if (!PyArg_ParseTuple(arg, "O!:putgeraw", ge_pytype, &geobj)) return (0);
493 if (ensurebuf(me, GE_G(geobj)->noctets)) return (0);
494 G_TORAW(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me)));
498 static PyObject *wbget_size(PyObject *me, void *hunoz)
499 { return (PyInt_FromLong(BLEN(BUF_B(me)))); }
501 static PyObject *wbget_contents(PyObject *me, void *hunoz)
502 { return (bytestring_pywrap(BBASE(BUF_B(me)), BLEN(BUF_B(me)))); }
504 static const PyGetSetDef wbuf_pygetset[] = {
505 #define GETSETNAME(op, name) wb##op##_##name
506 GET (size, "WBUF.size -> SIZE")
507 GET (contents, "WBUF.contents -> STR")
512 static const PyMethodDef wbuf_pymethods[] = {
513 #define METHNAME(func) wbmeth_##func
514 METH (zero, "WBUF.zero(N)")
515 METH (put, "WBUF.put(BYTES)")
516 #define WBMETH_DECL_PUTU_(n, W, w) \
517 METH(putu##w, "WBUF.putu" #w "(INT)")
518 DOUINTCONV(WBMETH_DECL_PUTU_)
519 #define WBMETH_DECL_PUTBLK_(n, W, w) \
520 METH(putblk##w, "WBUF.putblk" #w "(BYTES)")
521 BUF_DOSUFFIXES(WBMETH_DECL_PUTBLK_)
522 METH (putmp, "WBUF.putmp(X)")
523 METH (putgf, "WBUF.putgf(X)")
524 METH (putecpt, "WBUF.putecpt(P)")
525 METH (putecptraw, "WBUF.putecptraw(P)")
526 METH (putge, "WBUF.putge(X)")
527 METH (putgeraw, "WBUF.putgeraw(X)")
532 static const PyBufferProcs wbuf_pybuffer = {
534 wbuf_pygetbuf, /* @bf_getbuffer@ */
535 wbuf_pyrlsbuf /* @bf_releasebuffer@ */
537 wbuf_pyreadbuf, /* @bf_getreadbuffer@ */
538 0, /* @bf_getwritebuffer@ */
539 wbuf_pysegcount, /* @bf_getsegcount@ */
540 0 /* @bf_getcharbuffer@ */
544 static const PyTypeObject wbuf_pytype_skel = {
545 PyVarObject_HEAD_INIT(0, 0) /* Header */
546 "WriteBuffer", /* @tp_name@ */
547 sizeof(buf_pyobj), /* @tp_basicsize@ */
548 0, /* @tp_itemsize@ */
550 buf_pydealloc, /* @tp_dealloc@ */
552 0, /* @tp_getattr@ */
553 0, /* @tp_setattr@ */
554 0, /* @tp_compare@ */
556 0, /* @tp_as_number@ */
557 0, /* @tp_as_sequence@ */
558 0, /* @tp_as_mapping@ */
562 0, /* @tp_getattro@ */
563 0, /* @tp_setattro@ */
564 PYBUFFER(wbuf), /* @tp_as_buffer@ */
565 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
569 "WriteBuffer([size = ?]): a write buffer.",
571 0, /* @tp_traverse@ */
573 0, /* @tp_richcompare@ */
574 0, /* @tp_weaklistoffset@ */
576 0, /* @tp_iternext@ */
577 PYMETHODS(wbuf), /* @tp_methods@ */
578 0, /* @tp_members@ */
579 PYGETSET(wbuf), /* @tp_getset@ */
582 0, /* @tp_descr_get@ */
583 0, /* @tp_descr_set@ */
584 0, /* @tp_dictoffset@ */
586 PyType_GenericAlloc, /* @tp_alloc@ */
587 wbuf_pynew, /* @tp_new@ */
592 /*----- Initialization ----------------------------------------------------*/
596 void buffer_pyinit(void)
598 INITTYPE(rbuf, root);
599 INITTYPE(wbuf, root);
602 void buffer_pyinsert(PyObject *mod)
604 INSEXC("BufferError", buferr, PyExc_Exception, 0);
605 INSERT("ReadBuffer", rbuf_pytype);
606 INSERT("WriteBuffer", wbuf_pytype);
609 /*----- That's all, folks -------------------------------------------------*/