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
{
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)
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 char *kwlist
[] = { "data", 0 };
60 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &p
, &n
))
62 me
= (buf_pyobj
*)ty
->tp_alloc(ty
, 0);
65 buf_init(&me
->b
, q
, n
);
67 return ((PyObject
*)me
);
70 static void buf_pydealloc(PyObject
*me
)
71 { xfree(BBASE(BUF_B(me
))); FREEOBJ(me
); }
73 static int rbuf_pysegcount(PyObject
*me
, int *nn
)
74 { if (nn
) *nn
= BSZ(BUF_B(me
)); return (1); }
76 static int rbuf_pyreadbuf(PyObject
*me
, int seg
, void **q
)
77 { assert(seg
== 0); *q
= BBASE(BUF_B(me
)); return (BSZ(BUF_B(me
))); }
79 static PyObject
*rbmeth_skip(PyObject
*me
, PyObject
*arg
)
83 if (!PyArg_ParseTuple(arg
, "O&:skip", convszt
, &n
)) goto end
;
84 if (!buf_get(BUF_B(me
), n
)) BUFERR();
90 static PyObject
*rbmeth_get(PyObject
*me
, PyObject
*arg
)
95 if (!PyArg_ParseTuple(arg
, "O&:get", convszt
, &n
)) goto end
;
96 if ((p
= buf_get(BUF_B(me
), n
)) == 0) BUFERR();
97 return (bytestring_pywrap(p
, n
));
102 #define RBMETH_GETU_(n, W, w) \
103 static PyObject *rbmeth_getu##w(PyObject *me, PyObject *arg) \
106 if (!PyArg_ParseTuple(arg, ":getu" #w)) goto end; \
107 if (buf_getu##w(BUF_B(me), &x)) BUFERR(); \
108 return (getu32(x)); \
112 DOUINTCONV(RBMETH_GETU_
)
114 #define RBMETH_GETBLK_(n, W, w) \
115 static PyObject *rbmeth_getblk##w(PyObject *me, PyObject *arg) \
119 if (!PyArg_ParseTuple(arg, ":getblk" #w)) goto end; \
120 if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) BUFERR(); \
121 return (bytestring_pywrap(q, sz)); \
125 BUF_DOSUFFIXES(RBMETH_GETBLK_
)
127 static PyObject
*rbmeth_getmp(PyObject
*me
, PyObject
*arg
)
130 if (!PyArg_ParseTuple(arg
, ":getmp")) goto end
;
131 if ((x
= buf_getmp(BUF_B(me
))) == 0) BUFERR();
132 return (mp_pywrap(x
));
137 static PyObject
*rbmeth_getgf(PyObject
*me
, PyObject
*arg
)
140 if (!PyArg_ParseTuple(arg
, ":getgf")) goto end
;
141 if ((x
= buf_getmp(BUF_B(me
))) == 0) BUFERR();
142 return (gf_pywrap(x
));
147 static PyObject
*rbmeth_getecpt(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
149 PyObject
*cobj
= Py_None
;
150 static char *kwlist
[] = { "curve", 0 };
152 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:getecpt", kwlist
, &cobj
))
154 if (cobj
== Py_None
) cobj
= (PyObject
*)ecpt_pytype
;
155 if (!PyType_Check(cobj
) ||
156 !PyType_IsSubtype((PyTypeObject
*)cobj
, ecpt_pytype
))
157 TYERR("expected elliptic curve type");
158 if (buf_getec(BUF_B(me
), &pt
)) BUFERR();
159 return (ecpt_pywrapout(cobj
, &pt
));
164 static PyObject
*rbmeth_getecptraw(PyObject
*me
, PyObject
*arg
)
166 PyTypeObject
*cobj
= ecpt_pytype
;
168 if (!PyArg_ParseTuple(arg
, "O!:getecptraw", eccurve_pytype
, &cobj
))
170 if (ec_getraw(ECCURVE_C(cobj
), BUF_B(me
), &pt
)) BUFERR();
171 return (ecpt_pywrapout(cobj
, &pt
));
176 static PyObject
*rbmeth_getge(PyObject
*me
, PyObject
*arg
)
180 if (!PyArg_ParseTuple(arg
, "O!:getge", group_pytype
, &gobj
)) goto end
;
181 x
= G_CREATE(GROUP_G(gobj
));
182 if (G_FROMBUF(GROUP_G(gobj
), BUF_B(me
), x
)) BUFERR();
183 return (ge_pywrap(gobj
, x
));
185 if (x
) G_DESTROY(GROUP_G(gobj
), x
);
189 static PyObject
*rbmeth_getgeraw(PyObject
*me
, PyObject
*arg
)
193 if (!PyArg_ParseTuple(arg
, "O!:getgeraw", group_pytype
, &gobj
)) goto end
;
194 x
= G_CREATE(GROUP_G(gobj
));
195 if (G_FROMRAW(GROUP_G(gobj
), BUF_B(me
), x
)) BUFERR();
196 return (ge_pywrap(gobj
, x
));
198 if (x
) G_DESTROY(GROUP_G(gobj
), x
);
202 static PyObject
*rbget_size(PyObject
*me
, void *hunoz
)
203 { return (PyInt_FromLong(BSZ(BUF_B(me
)))); }
204 static PyObject
*rbget_left(PyObject
*me
, void *hunoz
)
205 { return (PyInt_FromLong(BLEFT(BUF_B(me
)))); }
206 static PyObject
*rbget_endp(PyObject
*me
, void *hunoz
)
207 { return (getbool(!BLEFT(BUF_B(me
)))); }
209 static PyGetSetDef rbuf_pygetset
[] = {
210 #define GETSETNAME(op, name) rb##op##_##name
211 GET (size
, "RBUF.size -> SIZE")
212 GET (left
, "RBUF.left -> REMAINDER")
213 GET (endp
, "RBUF.endp -> BOOL")
218 static PyMethodDef rbuf_pymethods
[] = {
219 #define METHNAME(func) rbmeth_##func
220 METH (skip
, "RBUF.skip(N)")
221 METH (get
, "RBUF.get(N) -> BYTES")
222 #define RBMETH_DECL_GETU_(n, W, w) \
223 METH(getu##w, "RBUF.getu" #w "() -> INT")
224 DOUINTCONV(RBMETH_DECL_GETU_
)
225 #define RBMETH_DECL_GETBLK_(n, W, w) \
226 METH(getblk##w, "RBUF.getblk" #w "() -> INT")
227 BUF_DOSUFFIXES(RBMETH_DECL_GETBLK_
)
228 METH (getmp
, "RBUF.getmp() -> X")
229 METH (getgf
, "RBUF.getgf() -> X")
230 KWMETH(getecpt
, "RBUF.getecpt(curve = None) -> P")
231 METH (getecptraw
, "RBUF.getecptraw(CURVE) -> P")
232 METH (getge
, "RBUF.getge(GROUP) -> X")
233 METH (getgeraw
, "RBUF.getgeraw(GROUP) -> X")
238 static PyBufferProcs rbuf_pybuffer
= {
239 rbuf_pyreadbuf
, /* @bf_getreadbuffer@ */
240 0, /* @bf_getwritebuffer@ */
241 rbuf_pysegcount
, /* @bf_getsegcount@ */
242 0 /* @bf_getcharbuffer@ */
245 static PyTypeObject rbuf_pytype_skel
= {
246 PyObject_HEAD_INIT(0) 0, /* Header */
247 "catacomb.ReadBuffer", /* @tp_name@ */
248 sizeof(buf_pyobj
), /* @tp_basicsize@ */
249 0, /* @tp_itemsize@ */
251 buf_pydealloc
, /* @tp_dealloc@ */
253 0, /* @tp_getattr@ */
254 0, /* @tp_setattr@ */
255 0, /* @tp_compare@ */
257 0, /* @tp_as_number@ */
258 0, /* @tp_as_sequence@ */
259 0, /* @tp_as_mapping@ */
263 0, /* @tp_getattro@ */
264 0, /* @tp_setattro@ */
265 &rbuf_pybuffer
, /* @tp_as_buffer@ */
266 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
272 0, /* @tp_traverse@ */
274 0, /* @tp_richcompare@ */
275 0, /* @tp_weaklistoffset@ */
277 0, /* @tp_iternexr@ */
278 rbuf_pymethods
, /* @tp_methods@ */
279 0, /* @tp_members@ */
280 rbuf_pygetset
, /* @tp_getset@ */
283 0, /* @tp_descr_get@ */
284 0, /* @tp_descr_set@ */
285 0, /* @tp_dictoffset@ */
287 PyType_GenericAlloc
, /* @tp_alloc@ */
288 rbuf_pynew
, /* @tp_new@ */
293 /*----- Write buffers -----------------------------------------------------*/
295 static void ensure(PyObject
*me
, size_t n
)
302 size_t want
= BLEFT(b
) + n
;
303 while (nn
< want
) nn
<<= 1;
304 p
= xrealloc(BBASE(b
), nn
, BSZ(b
));
305 BCUR(b
) = p
+ BLEN(b
);
311 static PyObject
*wbuf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
316 static char *kwlist
[] = { "size", 0 };
318 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", kwlist
,
321 me
= (buf_pyobj
*)ty
->tp_alloc(ty
, 0);
323 buf_init(&me
->b
, p
, n
);
325 return ((PyObject
*)me
);
328 static int wbuf_pysegcount(PyObject
*me
, int *nn
)
329 { if (nn
) *nn
= BLEN(BUF_B(me
)); return (1); }
331 static int wbuf_pyreadbuf(PyObject
*me
, int seg
, void **q
)
332 { assert(seg
== 0); *q
= BBASE(BUF_B(me
)); return (BLEN(BUF_B(me
))); }
334 static PyObject
*wbmeth_zero(PyObject
*me
, PyObject
*arg
)
338 if (!PyArg_ParseTuple(arg
, "O&:zero", convszt
, &n
)) return (0);
340 p
= buf_get(BUF_B(me
), n
); assert(p
&& BOK(BUF_B(me
)));
345 static PyObject
*wbmeth_put(PyObject
*me
, PyObject
*arg
)
349 if (!PyArg_ParseTuple(arg
, "s#:put", &p
, &n
)) return (0);
351 buf_put(BUF_B(me
), p
, n
); assert(BOK(BUF_B(m
)));
355 #define WBMETH_PUTU_(n, W, w) \
356 static PyObject *wbmeth_putu##w(PyObject *me, PyObject *arg) \
359 if (!PyArg_ParseTuple(arg, "O&:putu" #w, convu##n, &i)) return (0); \
360 ensure(me, SZ_##n); \
361 buf_putu##w(BUF_B(me), i); assert(BOK(BUF_B(me))); \
364 DOUINTCONV(WBMETH_PUTU_
)
367 #define WBMETH_PUTBLK_(n, W, w) \
368 static PyObject *wbmeth_putblk##w(PyObject *me, PyObject *arg) \
372 if (!PyArg_ParseTuple(arg, "s#:putblk" #w, &p, &sz)) return (0); \
373 ensure(me, sz + SZ_##n); \
374 buf_putmem##w(BUF_B(me), p, sz); assert(BOK(BUF_B(me))); \
377 BUF_DOSUFFIXES(WBMETH_PUTBLK_
)
379 static PyObject
*wbmeth_putmp(PyObject
*me
, PyObject
*arg
)
382 if (!PyArg_ParseTuple(arg
, "O&:putmp", convmp
, &x
)) return (0);
383 ensure(me
, mp_octets(x
) + 2);
384 buf_putmp(BUF_B(me
), x
); assert(BOK(BUF_B(me
)));
388 static PyObject
*wbmeth_putgf(PyObject
*me
, PyObject
*arg
)
391 if (!PyArg_ParseTuple(arg
, "O&:putgf", convgf
, &x
)) return (0);
392 ensure(me
, mp_octets(x
) + 2);
393 buf_putmp(BUF_B(me
), x
); assert(BOK(BUF_B(me
)));
398 static PyObject
*wbmeth_putecpt(PyObject
*me
, PyObject
*arg
)
401 if (!PyArg_ParseTuple(arg
, "O&:putecpt", convecpt
, &pt
)) return (0);
402 if (EC_ATINF(&pt
)) ensure(me
, 2);
403 else ensure(me
, 4 + mp_octets(pt
.x
) + mp_octets(pt
.y
));
404 buf_putec(BUF_B(me
), &pt
); assert(BOK(BUF_B(me
)));
409 static PyObject
*wbmeth_putecptraw(PyObject
*me
, PyObject
*arg
)
413 if (!PyArg_ParseTuple(arg
, "O!:putecptraw", ecptcurve_pytype
, &ptobj
))
415 EC_OUT(ECPT_C(ptobj
), &pt
, ECPT_P(ptobj
));
416 ensure(me
, ECPT_C(ptobj
)->f
->noctets
* 2 + 1);
417 ec_putraw(ECPT_C(ptobj
), BUF_B(me
), &pt
); assert(BOK(BUF_B(me
)));
422 static PyObject
*wbmeth_putge(PyObject
*me
, PyObject
*arg
)
425 if (!PyArg_ParseTuple(arg
, "O!:putge", ge_pytype
, &geobj
)) return (0);
426 ensure(me
, GE_G(geobj
)->noctets
);
427 G_TOBUF(GE_G(geobj
), BUF_B(me
), GE_X(geobj
)); assert(BOK(BUF_B(me
)));
431 static PyObject
*wbmeth_putgeraw(PyObject
*me
, PyObject
*arg
)
434 if (!PyArg_ParseTuple(arg
, "O!:putgeraw", ge_pytype
, &geobj
)) return (0);
435 ensure(me
, GE_G(geobj
)->noctets
);
436 G_TORAW(GE_G(geobj
), BUF_B(me
), GE_X(geobj
)); assert(BOK(BUF_B(me
)));
440 static PyObject
*wbget_size(PyObject
*me
, void *hunoz
)
441 { return (PyInt_FromLong(BLEN(BUF_B(me
)))); }
443 static PyGetSetDef wbuf_pygetset
[] = {
444 #define GETSETNAME(op, name) wb##op##_##name
445 GET (size
, "WBUF.size -> SIZE")
450 static PyMethodDef wbuf_pymethods
[] = {
451 #define METHNAME(func) wbmeth_##func
452 METH (zero
, "WBUF.skip(N)")
453 METH (put
, "WBUF.put(BYTES)")
454 #define WBMETH_DECL_PUTU_(n, W, w) \
455 METH(putu##w, "WBUF.putu" #w "(INT)")
456 DOUINTCONV(WBMETH_DECL_PUTU_
)
457 #define WBMETH_DECL_PUTBLK_(n, W, w) \
458 METH(putblk##w, "WBUF.putblk" #w "(BYTES)")
459 BUF_DOSUFFIXES(WBMETH_DECL_PUTBLK_
)
460 METH (putmp
, "WBUF.putmp(X)")
461 METH (putgf
, "WBUF.putgf(X)")
462 KWMETH(putecpt
, "WBUF.putecpt(P)")
463 METH (putecptraw
, "WBUF.putecptraw(P)")
464 METH (putge
, "WBUF.putge(X)")
465 METH (putgeraw
, "WBUF.putgeraw(X)")
470 static PyBufferProcs wbuf_pybuffer
= {
471 wbuf_pyreadbuf
, /* @bf_getreadbuffer@ */
472 0, /* @bf_getwritebuffer@ */
473 wbuf_pysegcount
, /* @bf_getsegcount@ */
474 0 /* @bf_getcharbuffer@ */
477 static PyTypeObject wbuf_pytype_skel
= {
478 PyObject_HEAD_INIT(0) 0, /* Header */
479 "catacomb.WriteBuffer", /* @tp_name@ */
480 sizeof(buf_pyobj
), /* @tp_basicsize@ */
481 0, /* @tp_itemsize@ */
483 buf_pydealloc
, /* @tp_dealloc@ */
485 0, /* @tp_getattr@ */
486 0, /* @tp_setattr@ */
487 0, /* @tp_compare@ */
489 0, /* @tp_as_number@ */
490 0, /* @tp_as_sequence@ */
491 0, /* @tp_as_mapping@ */
495 0, /* @tp_getattro@ */
496 0, /* @tp_setattro@ */
497 &wbuf_pybuffer
, /* @tp_as_buffer@ */
498 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
504 0, /* @tp_traverse@ */
506 0, /* @tp_richcompare@ */
507 0, /* @tp_weaklistoffset@ */
509 0, /* @tp_iternexr@ */
510 wbuf_pymethods
, /* @tp_methods@ */
511 0, /* @tp_members@ */
512 wbuf_pygetset
, /* @tp_getset@ */
515 0, /* @tp_descr_get@ */
516 0, /* @tp_descr_set@ */
517 0, /* @tp_dictoffset@ */
519 PyType_GenericAlloc
, /* @tp_alloc@ */
520 wbuf_pynew
, /* @tp_new@ */
525 /*----- Initialization ----------------------------------------------------*/
527 void buffer_pyinit(void)
529 INITTYPE(rbuf
, root
);
530 INITTYPE(wbuf
, root
);
533 void buffer_pyinsert(PyObject
*mod
)
535 INSEXC("BufferError", buferr
, PyExc_Exception
, 0);
536 INSERT("ReadBuffer", rbuf_pytype
);
537 INSERT("WriteBuffer", wbuf_pytype
);
540 /*----- That's all, folks -------------------------------------------------*/