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
{
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)
43 #define BUF_SUB(o) (((buf_pyobj *)(o))->sub)
45 /*----- Exceptions --------------------------------------------------------*/
47 static PyObject
*buferr
;
49 #define BUFERR(str) do { PyErr_SetString(buferr, str); goto end; } while (0)
51 /*----- Read buffers ------------------------------------------------------*/
53 static PyObject
*rbuf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
58 static const char *const kwlist
[] = { "data", 0 };
60 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convbin
, &in
))
63 memcpy(q
, in
.p
, in
.sz
);
64 me
= (buf_pyobj
*)ty
->tp_alloc(ty
, 0);
66 buf_init(&me
->b
, q
, in
.sz
);
68 return ((PyObject
*)me
);
71 static void buf_pydealloc(PyObject
*me
)
74 Py_DECREF(BUF_SUB(me
));
76 xfree(BBASE(BUF_B(me
)));
80 static Py_ssize_t
rbuf_pysegcount(PyObject
*me
, Py_ssize_t
*nn
)
81 { if (nn
) *nn
= BSZ(BUF_B(me
)); return (1); }
83 static Py_ssize_t
rbuf_pyreadbuf(PyObject
*me
, Py_ssize_t seg
, void **q
)
84 { assert(seg
== 0); *q
= BCUR(BUF_B(me
)); return (BLEFT(BUF_B(me
))); }
86 static PyObject
*rbmeth_skip(PyObject
*me
, PyObject
*arg
)
90 if (!PyArg_ParseTuple(arg
, "O&:skip", convszt
, &n
)) goto end
;
91 if (!buf_get(BUF_B(me
), n
)) BUFERR("buffer exhausted");
97 static PyObject
*rbmeth_get(PyObject
*me
, PyObject
*arg
)
102 if (!PyArg_ParseTuple(arg
, "O&:get", convszt
, &n
)) goto end
;
103 if ((p
= buf_get(BUF_B(me
), n
)) == 0) BUFERR("buffer exhausted");
104 return (bytestring_pywrap(p
, n
));
109 #define RBMETH_GETU_(n, W, w) \
110 static PyObject *rbmeth_getu##w(PyObject *me) \
113 if (buf_getu##w(BUF_B(me), &x)) BUFERR("buffer exhausted"); \
114 if (MASK##W <= ULONG_MAX) return (getulong(x)); \
115 else { kludge64 y; ASSIGN64(y, x); return (getk64(y)); } \
119 DOUINTCONV(RBMETH_GETU_
)
121 #define RBMETH_GETBLK_(n, W, w) \
122 static PyObject *rbmeth_getblk##w(PyObject *me) \
126 if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) \
127 BUFERR("buffer exhausted"); \
128 return (bytestring_pywrap(q, sz)); \
132 BUF_DOSUFFIXES(RBMETH_GETBLK_
)
134 #define RBMETH_GETBUF_(n, W, w) \
135 static PyObject *rbmeth_getbuf##w(PyObject *me) \
139 if (buf_getbuf##w(BUF_B(me), &bb)) BUFERR("buffer exhausted"); \
140 b = PyObject_NEW(buf_pyobj, rbuf_pytype); \
144 return ((PyObject *)b); \
148 BUF_DOSUFFIXES(RBMETH_GETBUF_
)
150 static PyObject
*rbmeth_getmp(PyObject
*me
)
153 if ((x
= buf_getmp(BUF_B(me
))) == 0) BUFERR("buffer exhausted");
154 return (mp_pywrap(x
));
159 static PyObject
*rbmeth_getgf(PyObject
*me
)
162 if ((x
= buf_getmp(BUF_B(me
))) == 0) BUFERR("buffer exhausted");
163 return (gf_pywrap(x
));
168 static PyObject
*rbmeth_getecpt(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
170 PyObject
*cobj
= Py_None
;
171 static const char *const kwlist
[] = { "curve", 0 };
173 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:getecpt", KWLIST
, &cobj
))
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");
179 if (buf_getec(BUF_B(me
), &pt
)) BUFERR("buffer exhausted");
180 return (ecpt_pywrapout(cobj
, &pt
));
185 static PyObject
*rbmeth_getecptraw(PyObject
*me
, PyObject
*arg
)
187 PyTypeObject
*cobj
= ecpt_pytype
;
189 if (!PyArg_ParseTuple(arg
, "O!:getecptraw", eccurve_pytype
, &cobj
))
191 if (ec_getraw(ECCURVE_C(cobj
), BUF_B(me
), &pt
)) BUFERR("buffer exhausted");
192 return (ecpt_pywrapout(cobj
, &pt
));
197 static PyObject
*rbmeth_getge(PyObject
*me
, PyObject
*arg
)
201 if (!PyArg_ParseTuple(arg
, "O!:getge", group_pytype
, &gobj
)) goto end
;
202 x
= G_CREATE(GROUP_G(gobj
));
203 if (G_FROMBUF(GROUP_G(gobj
), BUF_B(me
), x
)) BUFERR("buffer exhausted");
204 return (ge_pywrap(gobj
, x
));
206 if (x
) G_DESTROY(GROUP_G(gobj
), x
);
210 static PyObject
*rbmeth_getgeraw(PyObject
*me
, PyObject
*arg
)
214 if (!PyArg_ParseTuple(arg
, "O!:getgeraw", group_pytype
, &gobj
)) goto end
;
215 x
= G_CREATE(GROUP_G(gobj
));
216 if (G_FROMRAW(GROUP_G(gobj
), BUF_B(me
), x
)) BUFERR("buffer exhausted");
217 return (ge_pywrap(gobj
, x
));
219 if (x
) G_DESTROY(GROUP_G(gobj
), x
);
223 static PyObject
*rbget_size(PyObject
*me
, void *hunoz
)
224 { return (PyInt_FromLong(BSZ(BUF_B(me
)))); }
225 static PyObject
*rbget_left(PyObject
*me
, void *hunoz
)
226 { return (PyInt_FromLong(BLEFT(BUF_B(me
)))); }
227 static PyObject
*rbget_endp(PyObject
*me
, void *hunoz
)
228 { return (getbool(!BLEFT(BUF_B(me
)))); }
229 static PyObject
*rbget_offset(PyObject
*me
, void *hunoz
)
230 { return (PyInt_FromLong(BLEN(BUF_B(me
)))); }
231 static int rbset_offset(PyObject
*me
, PyObject
*x
, void *hunoz
)
234 if (!x
) NIERR("__del__");
235 if (!convszt(x
, &n
)) goto end
;
236 if (n
> BSZ(BUF_B(me
))) VALERR("out of range");
237 BCUR(BUF_B(me
)) = BBASE(BUF_B(me
)) + n
;
243 static const PyGetSetDef rbuf_pygetset
[] = {
244 #define GETSETNAME(op, name) rb##op##_##name
245 GET (size
, "RBUF.size -> SIZE")
246 GET (left
, "RBUF.left -> REMAINDER")
247 GET (endp
, "RBUF.endp -> BOOL")
248 GETSET(offset
, "RBUF.offset -> OFFSET")
253 static const PyMethodDef rbuf_pymethods
[] = {
254 #define METHNAME(func) rbmeth_##func
255 METH (skip
, "RBUF.skip(N)")
256 METH (get
, "RBUF.get(N) -> BYTES")
257 #define RBMETH_DECL_GETU_(n, W, w) \
258 NAMETH(getu##w, "RBUF.getu" #w "() -> INT")
259 DOUINTCONV(RBMETH_DECL_GETU_
)
260 #define RBMETH_DECL_GETBLK_(n, W, w) \
261 NAMETH(getblk##w, "RBUF.getblk" #w "() -> BYTES")
262 BUF_DOSUFFIXES(RBMETH_DECL_GETBLK_
)
263 #define RBMETH_DECL_GETBUF_(n, W, w) \
264 NAMETH(getbuf##w, "RBUF.getbuf" #w "() -> RBUF'")
265 BUF_DOSUFFIXES(RBMETH_DECL_GETBUF_
)
266 NAMETH(getmp
, "RBUF.getmp() -> X")
267 NAMETH(getgf
, "RBUF.getgf() -> X")
268 KWMETH(getecpt
, "RBUF.getecpt([curve = None]) -> P")
269 METH (getecptraw
, "RBUF.getecptraw(CURVE) -> P")
270 METH (getge
, "RBUF.getge(GROUP) -> X")
271 METH (getgeraw
, "RBUF.getgeraw(GROUP) -> X")
276 static const PyBufferProcs rbuf_pybuffer
= {
277 rbuf_pyreadbuf
, /* @bf_getreadbuffer@ */
278 0, /* @bf_getwritebuffer@ */
279 rbuf_pysegcount
, /* @bf_getsegcount@ */
280 0 /* @bf_getcharbuffer@ */
283 static const PyTypeObject rbuf_pytype_skel
= {
284 PyVarObject_HEAD_INIT(0, 0) /* Header */
285 "ReadBuffer", /* @tp_name@ */
286 sizeof(buf_pyobj
), /* @tp_basicsize@ */
287 0, /* @tp_itemsize@ */
289 buf_pydealloc
, /* @tp_dealloc@ */
291 0, /* @tp_getattr@ */
292 0, /* @tp_setattr@ */
293 0, /* @tp_compare@ */
295 0, /* @tp_as_number@ */
296 0, /* @tp_as_sequence@ */
297 0, /* @tp_as_mapping@ */
301 0, /* @tp_getattro@ */
302 0, /* @tp_setattro@ */
303 PYBUFFER(rbuf
), /* @tp_as_buffer@ */
304 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
308 "ReadBuffer(STR): a read buffer.",
310 0, /* @tp_traverse@ */
312 0, /* @tp_richcompare@ */
313 0, /* @tp_weaklistoffset@ */
315 0, /* @tp_iternext@ */
316 PYMETHODS(rbuf
), /* @tp_methods@ */
317 0, /* @tp_members@ */
318 PYGETSET(rbuf
), /* @tp_getset@ */
321 0, /* @tp_descr_get@ */
322 0, /* @tp_descr_set@ */
323 0, /* @tp_dictoffset@ */
325 PyType_GenericAlloc
, /* @tp_alloc@ */
326 rbuf_pynew
, /* @tp_new@ */
331 /*----- Write buffers -----------------------------------------------------*/
333 static void ensure(PyObject
*me
, size_t n
)
340 size_t want
= BLEN(b
) + n
;
341 while (nn
< want
) nn
<<= 1;
342 p
= xrealloc(BBASE(b
), nn
, BSZ(b
));
343 BCUR(b
) = p
+ BLEN(b
);
349 static PyObject
*wbuf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
354 static const char *const kwlist
[] = { "size", 0 };
356 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", KWLIST
,
359 me
= (buf_pyobj
*)ty
->tp_alloc(ty
, 0);
362 buf_init(&me
->b
, p
, n
);
364 return ((PyObject
*)me
);
367 static Py_ssize_t
wbuf_pysegcount(PyObject
*me
, Py_ssize_t
*nn
)
368 { if (nn
) *nn
= BLEN(BUF_B(me
)); return (1); }
370 static Py_ssize_t
wbuf_pyreadbuf(PyObject
*me
, Py_ssize_t seg
, void **q
)
371 { assert(seg
== 0); *q
= BBASE(BUF_B(me
)); return (BLEN(BUF_B(me
))); }
373 static PyObject
*wbmeth_zero(PyObject
*me
, PyObject
*arg
)
377 if (!PyArg_ParseTuple(arg
, "O&:zero", convszt
, &n
)) return (0);
379 p
= buf_get(BUF_B(me
), n
); assert(p
&& BOK(BUF_B(me
)));
384 static PyObject
*wbmeth_put(PyObject
*me
, PyObject
*arg
)
387 if (!PyArg_ParseTuple(arg
, "O&:put", convbin
, &in
)) return (0);
389 buf_put(BUF_B(me
), in
.p
, in
.sz
); assert(BOK(BUF_B(me
)));
393 #define WBMETH_PUTU_(n, W, w) \
394 static PyObject *wbmeth_putu##w(PyObject *me, PyObject *arg) \
397 if (!PyArg_ParseTuple(arg, "O&:putu" #w, convu##n, &i)) return (0); \
398 ensure(me, SZ_##n); \
399 buf_putu##w(BUF_B(me), i); assert(BOK(BUF_B(me))); \
402 DOUINTCONV(WBMETH_PUTU_
)
406 #define WBMETH_PUTBLK_(n, W, w) \
407 static PyObject *wbmeth_putblk##w(PyObject *me, PyObject *arg) \
410 if (!PyArg_ParseTuple(arg, "O&:putblk" #w, convbin, &in)) goto end; \
411 if (MASK##W && in.sz > MASK##W) VALERR("too large"); \
412 ensure(me, in.sz + SZ_##n); \
413 buf_putmem##w(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me))); \
418 BUF_DOSUFFIXES(WBMETH_PUTBLK_
)
420 static PyObject
*wbmeth_putmp(PyObject
*me
, PyObject
*arg
)
423 if (!PyArg_ParseTuple(arg
, "O&:putmp", convmp
, &x
)) return (0);
424 ensure(me
, mp_octets(x
) + 2);
425 buf_putmp(BUF_B(me
), x
); assert(BOK(BUF_B(me
)));
429 static PyObject
*wbmeth_putgf(PyObject
*me
, PyObject
*arg
)
432 if (!PyArg_ParseTuple(arg
, "O&:putgf", convgf
, &x
)) return (0);
433 ensure(me
, mp_octets(x
) + 2);
434 buf_putmp(BUF_B(me
), x
); assert(BOK(BUF_B(me
)));
439 static PyObject
*wbmeth_putecpt(PyObject
*me
, PyObject
*arg
)
442 if (!PyArg_ParseTuple(arg
, "O&:putecpt", convecpt
, &pt
)) return (0);
443 ensure(me
, EC_ATINF(&pt
) ?
2 : 6 + mp_octets(pt
.x
) + mp_octets(pt
.y
));
444 buf_putec(BUF_B(me
), &pt
); assert(BOK(BUF_B(me
)));
449 static PyObject
*wbmeth_putecptraw(PyObject
*me
, PyObject
*arg
)
453 if (!PyArg_ParseTuple(arg
, "O!:putecptraw", ecptcurve_pytype
, &ptobj
))
455 EC_OUT(ECPT_C(ptobj
), &pt
, ECPT_P(ptobj
));
456 ensure(me
, ECPT_C(ptobj
)->f
->noctets
* 2 + 1);
457 ec_putraw(ECPT_C(ptobj
), BUF_B(me
), &pt
); assert(BOK(BUF_B(me
)));
462 static PyObject
*wbmeth_putge(PyObject
*me
, PyObject
*arg
)
465 if (!PyArg_ParseTuple(arg
, "O!:putge", ge_pytype
, &geobj
)) return (0);
466 ensure(me
, GE_G(geobj
)->noctets
);
467 G_TOBUF(GE_G(geobj
), BUF_B(me
), GE_X(geobj
)); assert(BOK(BUF_B(me
)));
471 static PyObject
*wbmeth_putgeraw(PyObject
*me
, PyObject
*arg
)
474 if (!PyArg_ParseTuple(arg
, "O!:putgeraw", ge_pytype
, &geobj
)) return (0);
475 ensure(me
, GE_G(geobj
)->noctets
);
476 G_TORAW(GE_G(geobj
), BUF_B(me
), GE_X(geobj
)); assert(BOK(BUF_B(me
)));
480 static PyObject
*wbget_size(PyObject
*me
, void *hunoz
)
481 { return (PyInt_FromLong(BLEN(BUF_B(me
)))); }
483 static PyObject
*wbget_contents(PyObject
*me
, void *hunoz
)
484 { return (bytestring_pywrap(BBASE(BUF_B(me
)), BLEN(BUF_B(me
)))); }
486 static const PyGetSetDef wbuf_pygetset
[] = {
487 #define GETSETNAME(op, name) wb##op##_##name
488 GET (size
, "WBUF.size -> SIZE")
489 GET (contents
, "WBUF.contents -> STR")
494 static const PyMethodDef wbuf_pymethods
[] = {
495 #define METHNAME(func) wbmeth_##func
496 METH (zero
, "WBUF.zero(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 METH (putecpt
, "WBUF.putecpt(P)")
507 METH (putecptraw
, "WBUF.putecptraw(P)")
508 METH (putge
, "WBUF.putge(X)")
509 METH (putgeraw
, "WBUF.putgeraw(X)")
514 static const PyBufferProcs wbuf_pybuffer
= {
515 wbuf_pyreadbuf
, /* @bf_getreadbuffer@ */
516 0, /* @bf_getwritebuffer@ */
517 wbuf_pysegcount
, /* @bf_getsegcount@ */
518 0 /* @bf_getcharbuffer@ */
521 static const PyTypeObject wbuf_pytype_skel
= {
522 PyVarObject_HEAD_INIT(0, 0) /* Header */
523 "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 PYBUFFER(wbuf
), /* @tp_as_buffer@ */
542 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
546 "WriteBuffer([size = ?]): a write buffer.",
548 0, /* @tp_traverse@ */
550 0, /* @tp_richcompare@ */
551 0, /* @tp_weaklistoffset@ */
553 0, /* @tp_iternext@ */
554 PYMETHODS(wbuf
), /* @tp_methods@ */
555 0, /* @tp_members@ */
556 PYGETSET(wbuf
), /* @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 -------------------------------------------------*/