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() do { PyErr_SetNone(buferr); 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
, "s#:new", KWLIST
, &p
, &n
))
64 me
= (buf_pyobj
*)ty
->tp_alloc(ty
, 0);
66 buf_init(&me
->b
, q
, n
);
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();
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();
104 return (bytestring_pywrap(p
, n
));
109 #define RBMETH_GETU_(n, W, w) \
110 static PyObject *rbmeth_getu##w(PyObject *me, PyObject *arg) \
113 if (!PyArg_ParseTuple(arg, ":getu" #w)) goto end; \
114 if (buf_getu##w(BUF_B(me), &x)) BUFERR(); \
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, PyObject *arg) \
127 if (!PyArg_ParseTuple(arg, ":getblk" #w)) goto end; \
128 if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) BUFERR(); \
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, PyObject *arg) \
140 if (!PyArg_ParseTuple(arg, ":getbuf" #w)) goto end; \
141 if (buf_getbuf##w(BUF_B(me), &bb)) BUFERR(); \
142 b = PyObject_NEW(buf_pyobj, rbuf_pytype); \
146 return ((PyObject *)b); \
150 BUF_DOSUFFIXES(RBMETH_GETBUF_
)
152 static PyObject
*rbmeth_getmp(PyObject
*me
, PyObject
*arg
)
155 if (!PyArg_ParseTuple(arg
, ":getmp")) goto end
;
156 if ((x
= buf_getmp(BUF_B(me
))) == 0) BUFERR();
157 return (mp_pywrap(x
));
162 static PyObject
*rbmeth_getgf(PyObject
*me
, PyObject
*arg
)
165 if (!PyArg_ParseTuple(arg
, ":getgf")) goto end
;
166 if ((x
= buf_getmp(BUF_B(me
))) == 0) BUFERR();
167 return (gf_pywrap(x
));
172 static PyObject
*rbmeth_getecpt(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
174 PyObject
*cobj
= Py_None
;
175 static const char *const kwlist
[] = { "curve", 0 };
177 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:getecpt", KWLIST
, &cobj
))
179 if (cobj
== Py_None
) cobj
= (PyObject
*)ecpt_pytype
;
180 if (!PyType_Check(cobj
) ||
181 !PyType_IsSubtype((PyTypeObject
*)cobj
, ecpt_pytype
))
182 TYERR("expected elliptic curve type");
183 if (buf_getec(BUF_B(me
), &pt
)) BUFERR();
184 return (ecpt_pywrapout(cobj
, &pt
));
189 static PyObject
*rbmeth_getecptraw(PyObject
*me
, PyObject
*arg
)
191 PyTypeObject
*cobj
= ecpt_pytype
;
193 if (!PyArg_ParseTuple(arg
, "O!:getecptraw", eccurve_pytype
, &cobj
))
195 if (ec_getraw(ECCURVE_C(cobj
), BUF_B(me
), &pt
)) BUFERR();
196 return (ecpt_pywrapout(cobj
, &pt
));
201 static PyObject
*rbmeth_getge(PyObject
*me
, PyObject
*arg
)
205 if (!PyArg_ParseTuple(arg
, "O!:getge", group_pytype
, &gobj
)) goto end
;
206 x
= G_CREATE(GROUP_G(gobj
));
207 if (G_FROMBUF(GROUP_G(gobj
), BUF_B(me
), x
)) BUFERR();
208 return (ge_pywrap(gobj
, x
));
210 if (x
) G_DESTROY(GROUP_G(gobj
), x
);
214 static PyObject
*rbmeth_getgeraw(PyObject
*me
, PyObject
*arg
)
218 if (!PyArg_ParseTuple(arg
, "O!:getgeraw", group_pytype
, &gobj
)) goto end
;
219 x
= G_CREATE(GROUP_G(gobj
));
220 if (G_FROMRAW(GROUP_G(gobj
), BUF_B(me
), x
)) BUFERR();
221 return (ge_pywrap(gobj
, x
));
223 if (x
) G_DESTROY(GROUP_G(gobj
), x
);
227 static PyObject
*rbget_size(PyObject
*me
, void *hunoz
)
228 { return (PyInt_FromLong(BSZ(BUF_B(me
)))); }
229 static PyObject
*rbget_left(PyObject
*me
, void *hunoz
)
230 { return (PyInt_FromLong(BLEFT(BUF_B(me
)))); }
231 static PyObject
*rbget_endp(PyObject
*me
, void *hunoz
)
232 { return (getbool(!BLEFT(BUF_B(me
)))); }
233 static PyObject
*rbget_offset(PyObject
*me
, void *hunoz
)
234 { return (PyInt_FromLong(BLEN(BUF_B(me
)))); }
235 static int rbset_offset(PyObject
*me
, PyObject
*x
, void *hunoz
)
238 if (!x
) NIERR("__del__");
239 if (!convszt(x
, &n
)) goto end
;
240 if (n
> BSZ(BUF_B(me
))) VALERR("out of range");
241 BCUR(BUF_B(me
)) = BBASE(BUF_B(me
)) + n
;
247 static PyGetSetDef rbuf_pygetset
[] = {
248 #define GETSETNAME(op, name) rb##op##_##name
249 GET (size
, "RBUF.size -> SIZE")
250 GET (left
, "RBUF.left -> REMAINDER")
251 GET (endp
, "RBUF.endp -> BOOL")
252 GETSET(offset
, "RBUF.offset -> OFFSET")
257 static PyMethodDef rbuf_pymethods
[] = {
258 #define METHNAME(func) rbmeth_##func
259 METH (skip
, "RBUF.skip(N)")
260 METH (get
, "RBUF.get(N) -> BYTES")
261 #define RBMETH_DECL_GETU_(n, W, w) \
262 METH(getu##w, "RBUF.getu" #w "() -> INT")
263 DOUINTCONV(RBMETH_DECL_GETU_
)
264 #define RBMETH_DECL_GETBLK_(n, W, w) \
265 METH(getblk##w, "RBUF.getblk" #w "() -> BYTES")
266 BUF_DOSUFFIXES(RBMETH_DECL_GETBLK_
)
267 #define RBMETH_DECL_GETBUF_(n, W, w) \
268 METH(getbuf##w, "RBUF.getbuf" #w "() -> RBUF'")
269 BUF_DOSUFFIXES(RBMETH_DECL_GETBUF_
)
270 METH (getmp
, "RBUF.getmp() -> X")
271 METH (getgf
, "RBUF.getgf() -> X")
272 KWMETH(getecpt
, "RBUF.getecpt([curve = None]) -> P")
273 METH (getecptraw
, "RBUF.getecptraw(CURVE) -> P")
274 METH (getge
, "RBUF.getge(GROUP) -> X")
275 METH (getgeraw
, "RBUF.getgeraw(GROUP) -> X")
280 static PyBufferProcs rbuf_pybuffer
= {
281 rbuf_pyreadbuf
, /* @bf_getreadbuffer@ */
282 0, /* @bf_getwritebuffer@ */
283 rbuf_pysegcount
, /* @bf_getsegcount@ */
284 0 /* @bf_getcharbuffer@ */
287 static PyTypeObject rbuf_pytype_skel
= {
288 PyObject_HEAD_INIT(0) 0, /* Header */
289 "ReadBuffer", /* @tp_name@ */
290 sizeof(buf_pyobj
), /* @tp_basicsize@ */
291 0, /* @tp_itemsize@ */
293 buf_pydealloc
, /* @tp_dealloc@ */
295 0, /* @tp_getattr@ */
296 0, /* @tp_setattr@ */
297 0, /* @tp_compare@ */
299 0, /* @tp_as_number@ */
300 0, /* @tp_as_sequence@ */
301 0, /* @tp_as_mapping@ */
305 0, /* @tp_getattro@ */
306 0, /* @tp_setattro@ */
307 &rbuf_pybuffer
, /* @tp_as_buffer@ */
308 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
312 "ReadBuffer(STR): a read buffer.",
314 0, /* @tp_traverse@ */
316 0, /* @tp_richcompare@ */
317 0, /* @tp_weaklistoffset@ */
319 0, /* @tp_iternext@ */
320 rbuf_pymethods
, /* @tp_methods@ */
321 0, /* @tp_members@ */
322 rbuf_pygetset
, /* @tp_getset@ */
325 0, /* @tp_descr_get@ */
326 0, /* @tp_descr_set@ */
327 0, /* @tp_dictoffset@ */
329 PyType_GenericAlloc
, /* @tp_alloc@ */
330 rbuf_pynew
, /* @tp_new@ */
335 /*----- Write buffers -----------------------------------------------------*/
337 static void ensure(PyObject
*me
, size_t n
)
344 size_t want
= BLEN(b
) + n
;
345 while (nn
< want
) nn
<<= 1;
346 p
= xrealloc(BBASE(b
), nn
, BSZ(b
));
347 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);
366 buf_init(&me
->b
, p
, n
);
368 return ((PyObject
*)me
);
371 static Py_ssize_t
wbuf_pysegcount(PyObject
*me
, Py_ssize_t
*nn
)
372 { if (nn
) *nn
= BLEN(BUF_B(me
)); return (1); }
374 static Py_ssize_t
wbuf_pyreadbuf(PyObject
*me
, Py_ssize_t seg
, void **q
)
375 { assert(seg
== 0); *q
= BBASE(BUF_B(me
)); return (BLEN(BUF_B(me
))); }
377 static PyObject
*wbmeth_zero(PyObject
*me
, PyObject
*arg
)
381 if (!PyArg_ParseTuple(arg
, "O&:zero", convszt
, &n
)) return (0);
383 p
= buf_get(BUF_B(me
), n
); assert(p
&& BOK(BUF_B(me
)));
388 static PyObject
*wbmeth_put(PyObject
*me
, PyObject
*arg
)
392 if (!PyArg_ParseTuple(arg
, "s#:put", &p
, &n
)) return (0);
394 buf_put(BUF_B(me
), p
, n
); assert(BOK(BUF_B(me
)));
398 #define WBMETH_PUTU_(n, W, w) \
399 static PyObject *wbmeth_putu##w(PyObject *me, PyObject *arg) \
402 if (!PyArg_ParseTuple(arg, "O&:putu" #w, convu##n, &i)) return (0); \
403 ensure(me, SZ_##n); \
404 buf_putu##w(BUF_B(me), i); assert(BOK(BUF_B(me))); \
407 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)) goto end; \
417 if (MASK##W && sz > MASK##W) VALERR("too large"); \
418 ensure(me, sz + SZ_##n); \
419 buf_putmem##w(BUF_B(me), p, sz); assert(BOK(BUF_B(me))); \
424 BUF_DOSUFFIXES(WBMETH_PUTBLK_
)
426 static PyObject
*wbmeth_putmp(PyObject
*me
, PyObject
*arg
)
429 if (!PyArg_ParseTuple(arg
, "O&:putmp", convmp
, &x
)) return (0);
430 ensure(me
, mp_octets(x
) + 2);
431 buf_putmp(BUF_B(me
), x
); assert(BOK(BUF_B(me
)));
435 static PyObject
*wbmeth_putgf(PyObject
*me
, PyObject
*arg
)
438 if (!PyArg_ParseTuple(arg
, "O&:putgf", convgf
, &x
)) return (0);
439 ensure(me
, mp_octets(x
) + 2);
440 buf_putmp(BUF_B(me
), x
); assert(BOK(BUF_B(me
)));
445 static PyObject
*wbmeth_putecpt(PyObject
*me
, PyObject
*arg
)
448 if (!PyArg_ParseTuple(arg
, "O&:putecpt", convecpt
, &pt
)) return (0);
449 ensure(me
, EC_ATINF(&pt
) ?
2 : 6 + mp_octets(pt
.x
) + mp_octets(pt
.y
));
450 buf_putec(BUF_B(me
), &pt
); assert(BOK(BUF_B(me
)));
455 static PyObject
*wbmeth_putecptraw(PyObject
*me
, PyObject
*arg
)
459 if (!PyArg_ParseTuple(arg
, "O!:putecptraw", ecptcurve_pytype
, &ptobj
))
461 EC_OUT(ECPT_C(ptobj
), &pt
, ECPT_P(ptobj
));
462 ensure(me
, ECPT_C(ptobj
)->f
->noctets
* 2 + 1);
463 ec_putraw(ECPT_C(ptobj
), BUF_B(me
), &pt
); assert(BOK(BUF_B(me
)));
468 static PyObject
*wbmeth_putge(PyObject
*me
, PyObject
*arg
)
471 if (!PyArg_ParseTuple(arg
, "O!:putge", ge_pytype
, &geobj
)) return (0);
472 ensure(me
, GE_G(geobj
)->noctets
);
473 G_TOBUF(GE_G(geobj
), BUF_B(me
), GE_X(geobj
)); assert(BOK(BUF_B(me
)));
477 static PyObject
*wbmeth_putgeraw(PyObject
*me
, PyObject
*arg
)
480 if (!PyArg_ParseTuple(arg
, "O!:putgeraw", ge_pytype
, &geobj
)) return (0);
481 ensure(me
, GE_G(geobj
)->noctets
);
482 G_TORAW(GE_G(geobj
), BUF_B(me
), GE_X(geobj
)); assert(BOK(BUF_B(me
)));
486 static PyObject
*wbget_size(PyObject
*me
, void *hunoz
)
487 { return (PyInt_FromLong(BLEN(BUF_B(me
)))); }
489 static PyObject
*wbget_contents(PyObject
*me
, void *hunoz
)
490 { return (bytestring_pywrap(BBASE(BUF_B(me
)), BLEN(BUF_B(me
)))); }
492 static PyGetSetDef wbuf_pygetset
[] = {
493 #define GETSETNAME(op, name) wb##op##_##name
494 GET (size
, "WBUF.size -> SIZE")
495 GET (contents
, "WBUF.contents -> STR")
500 static PyMethodDef wbuf_pymethods
[] = {
501 #define METHNAME(func) wbmeth_##func
502 METH (zero
, "WBUF.zero(N)")
503 METH (put
, "WBUF.put(BYTES)")
504 #define WBMETH_DECL_PUTU_(n, W, w) \
505 METH(putu##w, "WBUF.putu" #w "(INT)")
506 DOUINTCONV(WBMETH_DECL_PUTU_
)
507 #define WBMETH_DECL_PUTBLK_(n, W, w) \
508 METH(putblk##w, "WBUF.putblk" #w "(BYTES)")
509 BUF_DOSUFFIXES(WBMETH_DECL_PUTBLK_
)
510 METH (putmp
, "WBUF.putmp(X)")
511 METH (putgf
, "WBUF.putgf(X)")
512 METH (putecpt
, "WBUF.putecpt(P)")
513 METH (putecptraw
, "WBUF.putecptraw(P)")
514 METH (putge
, "WBUF.putge(X)")
515 METH (putgeraw
, "WBUF.putgeraw(X)")
520 static PyBufferProcs wbuf_pybuffer
= {
521 wbuf_pyreadbuf
, /* @bf_getreadbuffer@ */
522 0, /* @bf_getwritebuffer@ */
523 wbuf_pysegcount
, /* @bf_getsegcount@ */
524 0 /* @bf_getcharbuffer@ */
527 static PyTypeObject wbuf_pytype_skel
= {
528 PyObject_HEAD_INIT(0) 0, /* Header */
529 "WriteBuffer", /* @tp_name@ */
530 sizeof(buf_pyobj
), /* @tp_basicsize@ */
531 0, /* @tp_itemsize@ */
533 buf_pydealloc
, /* @tp_dealloc@ */
535 0, /* @tp_getattr@ */
536 0, /* @tp_setattr@ */
537 0, /* @tp_compare@ */
539 0, /* @tp_as_number@ */
540 0, /* @tp_as_sequence@ */
541 0, /* @tp_as_mapping@ */
545 0, /* @tp_getattro@ */
546 0, /* @tp_setattro@ */
547 &wbuf_pybuffer
, /* @tp_as_buffer@ */
548 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
552 "WriteBuffer([size = ?]): a write buffer.",
554 0, /* @tp_traverse@ */
556 0, /* @tp_richcompare@ */
557 0, /* @tp_weaklistoffset@ */
559 0, /* @tp_iternext@ */
560 wbuf_pymethods
, /* @tp_methods@ */
561 0, /* @tp_members@ */
562 wbuf_pygetset
, /* @tp_getset@ */
565 0, /* @tp_descr_get@ */
566 0, /* @tp_descr_set@ */
567 0, /* @tp_dictoffset@ */
569 PyType_GenericAlloc
, /* @tp_alloc@ */
570 wbuf_pynew
, /* @tp_new@ */
575 /*----- Initialization ----------------------------------------------------*/
577 void buffer_pyinit(void)
579 INITTYPE(rbuf
, root
);
580 INITTYPE(wbuf
, root
);
583 void buffer_pyinsert(PyObject
*mod
)
585 INSEXC("BufferError", buferr
, PyExc_Exception
, 0);
586 INSERT("ReadBuffer", rbuf_pytype
);
587 INSERT("WriteBuffer", wbuf_pytype
);
590 /*----- That's all, folks -------------------------------------------------*/