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
)));
61 static Py_ssize_t
rbuf_pysegcount(PyObject
*me
, Py_ssize_t
*nn
)
62 { if (nn
) *nn
= BSZ(BUF_B(me
)); return (1); }
64 static Py_ssize_t
rbuf_pyreadbuf(PyObject
*me
, Py_ssize_t seg
, void **q
)
65 { assert(seg
== 0); *q
= BCUR(BUF_B(me
)); return (BLEFT(BUF_B(me
))); }
67 static PyObject
*rbmeth_skip(PyObject
*me
, PyObject
*arg
)
71 if (!PyArg_ParseTuple(arg
, "O&:skip", convszt
, &n
)) goto end
;
72 if (!buf_get(BUF_B(me
), n
)) BUFERR("buffer exhausted");
78 static PyObject
*rbmeth_get(PyObject
*me
, PyObject
*arg
)
83 if (!PyArg_ParseTuple(arg
, "O&:get", convszt
, &n
)) goto end
;
84 if ((p
= buf_get(BUF_B(me
), n
)) == 0) BUFERR("buffer exhausted");
85 return (bytestring_pywrap(p
, n
));
90 #define RBMETH_GETU_(n, W, w) \
91 static PyObject *rbmeth_getu##w(PyObject *me) \
94 if (buf_getu##w(BUF_B(me), &x)) BUFERR("buffer exhausted"); \
95 if (MASK##W <= ULONG_MAX) return (getulong(x)); \
96 else { kludge64 y; ASSIGN64(y, x); return (getk64(y)); } \
100 DOUINTCONV(RBMETH_GETU_
)
102 #define RBMETH_GETBLK_(n, W, w) \
103 static PyObject *rbmeth_getblk##w(PyObject *me) \
107 if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) \
108 BUFERR("buffer exhausted"); \
109 return (bytestring_pywrap(q, sz)); \
113 BUF_DOSUFFIXES(RBMETH_GETBLK_
)
115 #define RBMETH_GETBUF_(n, W, w) \
116 static PyObject *rbmeth_getbuf##w(PyObject *me) \
120 if (buf_getbuf##w(BUF_B(me), &bb)) BUFERR("buffer exhausted"); \
121 b = PyObject_NEW(buf_pyobj, rbuf_pytype); \
126 return ((PyObject *)b); \
130 BUF_DOSUFFIXES(RBMETH_GETBUF_
)
132 static PyObject
*rbmeth_getmp(PyObject
*me
)
135 if ((x
= buf_getmp(BUF_B(me
))) == 0) BUFERR("buffer exhausted");
136 return (mp_pywrap(x
));
141 static PyObject
*rbmeth_getgf(PyObject
*me
)
144 if ((x
= buf_getmp(BUF_B(me
))) == 0) BUFERR("buffer exhausted");
145 return (gf_pywrap(x
));
150 static PyObject
*rbmeth_getecpt(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
152 PyObject
*cobj
= Py_None
;
153 static const char *const kwlist
[] = { "curve", 0 };
155 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:getecpt", KWLIST
, &cobj
))
157 if (cobj
== Py_None
) cobj
= (PyObject
*)ecpt_pytype
;
158 if (!PyType_Check(cobj
) ||
159 !PyType_IsSubtype((PyTypeObject
*)cobj
, ecpt_pytype
))
160 TYERR("expected elliptic curve type");
161 if (buf_getec(BUF_B(me
), &pt
)) BUFERR("buffer exhausted");
162 return (ecpt_pywrapout(cobj
, &pt
));
167 static PyObject
*rbmeth_getecptraw(PyObject
*me
, PyObject
*arg
)
172 if (!PyArg_ParseTuple(arg
, "O!:getecptraw", eccurve_pytype
, &cobj
))
174 if (ec_getraw(ECCURVE_C(cobj
), BUF_B(me
), &pt
)) BUFERR("buffer exhausted");
175 rc
= ecpt_pywrapout(cobj
, &pt
);
180 static PyObject
*rbmeth_getge(PyObject
*me
, PyObject
*arg
)
184 if (!PyArg_ParseTuple(arg
, "O!:getge", group_pytype
, &gobj
)) goto end
;
185 x
= G_CREATE(GROUP_G(gobj
));
186 if (G_FROMBUF(GROUP_G(gobj
), BUF_B(me
), x
)) BUFERR("buffer exhausted");
187 return (ge_pywrap(gobj
, x
));
189 if (x
) G_DESTROY(GROUP_G(gobj
), x
);
193 static PyObject
*rbmeth_getgeraw(PyObject
*me
, PyObject
*arg
)
197 if (!PyArg_ParseTuple(arg
, "O!:getgeraw", group_pytype
, &gobj
)) goto end
;
198 x
= G_CREATE(GROUP_G(gobj
));
199 if (G_FROMRAW(GROUP_G(gobj
), BUF_B(me
), x
)) BUFERR("buffer exhausted");
200 return (ge_pywrap(gobj
, x
));
202 if (x
) G_DESTROY(GROUP_G(gobj
), x
);
206 static PyObject
*rbget_size(PyObject
*me
, void *hunoz
)
207 { return (PyInt_FromLong(BSZ(BUF_B(me
)))); }
208 static PyObject
*rbget_left(PyObject
*me
, void *hunoz
)
209 { return (PyInt_FromLong(BLEFT(BUF_B(me
)))); }
210 static PyObject
*rbget_endp(PyObject
*me
, void *hunoz
)
211 { return (getbool(!BLEFT(BUF_B(me
)))); }
212 static PyObject
*rbget_offset(PyObject
*me
, void *hunoz
)
213 { return (PyInt_FromLong(BLEN(BUF_B(me
)))); }
214 static int rbset_offset(PyObject
*me
, PyObject
*x
, void *hunoz
)
217 if (!x
) NIERR("__del__");
218 if (!convszt(x
, &n
)) goto end
;
219 if (n
> BSZ(BUF_B(me
))) VALERR("out of range");
220 BCUR(BUF_B(me
)) = BBASE(BUF_B(me
)) + n
;
226 static const PyGetSetDef rbuf_pygetset
[] = {
227 #define GETSETNAME(op, name) rb##op##_##name
228 GET (size
, "RBUF.size -> SIZE")
229 GET (left
, "RBUF.left -> REMAINDER")
230 GET (endp
, "RBUF.endp -> BOOL")
231 GETSET(offset
, "RBUF.offset -> OFFSET")
236 static const PyMethodDef rbuf_pymethods
[] = {
237 #define METHNAME(func) rbmeth_##func
238 METH (skip
, "RBUF.skip(N)")
239 METH (get
, "RBUF.get(N) -> BYTES")
240 #define RBMETH_DECL_GETU_(n, W, w) \
241 NAMETH(getu##w, "RBUF.getu" #w "() -> INT")
242 DOUINTCONV(RBMETH_DECL_GETU_
)
243 #define RBMETH_DECL_GETBLK_(n, W, w) \
244 NAMETH(getblk##w, "RBUF.getblk" #w "() -> BYTES")
245 BUF_DOSUFFIXES(RBMETH_DECL_GETBLK_
)
246 #define RBMETH_DECL_GETBUF_(n, W, w) \
247 NAMETH(getbuf##w, "RBUF.getbuf" #w "() -> RBUF'")
248 BUF_DOSUFFIXES(RBMETH_DECL_GETBUF_
)
249 NAMETH(getmp
, "RBUF.getmp() -> X")
250 NAMETH(getgf
, "RBUF.getgf() -> X")
251 KWMETH(getecpt
, "RBUF.getecpt([curve = None]) -> P")
252 METH (getecptraw
, "RBUF.getecptraw(CURVE) -> P")
253 METH (getge
, "RBUF.getge(GROUP) -> X")
254 METH (getgeraw
, "RBUF.getgeraw(GROUP) -> X")
259 static const PyBufferProcs rbuf_pybuffer
= {
260 rbuf_pyreadbuf
, /* @bf_getreadbuffer@ */
261 0, /* @bf_getwritebuffer@ */
262 rbuf_pysegcount
, /* @bf_getsegcount@ */
263 0 /* @bf_getcharbuffer@ */
266 static const PyTypeObject rbuf_pytype_skel
= {
267 PyVarObject_HEAD_INIT(0, 0) /* Header */
268 "ReadBuffer", /* @tp_name@ */
269 sizeof(buf_pyobj
), /* @tp_basicsize@ */
270 0, /* @tp_itemsize@ */
272 buf_pydealloc
, /* @tp_dealloc@ */
274 0, /* @tp_getattr@ */
275 0, /* @tp_setattr@ */
276 0, /* @tp_compare@ */
278 0, /* @tp_as_number@ */
279 0, /* @tp_as_sequence@ */
280 0, /* @tp_as_mapping@ */
284 0, /* @tp_getattro@ */
285 0, /* @tp_setattro@ */
286 PYBUFFER(rbuf
), /* @tp_as_buffer@ */
287 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
291 "ReadBuffer(STR): a read buffer.",
293 0, /* @tp_traverse@ */
295 0, /* @tp_richcompare@ */
296 0, /* @tp_weaklistoffset@ */
298 0, /* @tp_iternext@ */
299 PYMETHODS(rbuf
), /* @tp_methods@ */
300 0, /* @tp_members@ */
301 PYGETSET(rbuf
), /* @tp_getset@ */
304 0, /* @tp_descr_get@ */
305 0, /* @tp_descr_set@ */
306 0, /* @tp_dictoffset@ */
308 PyType_GenericAlloc
, /* @tp_alloc@ */
309 rbuf_pynew
, /* @tp_new@ */
314 /*----- Write buffers -----------------------------------------------------*/
316 PyTypeObject
*wbuf_pytype
;
318 int ensurebuf(PyObject
*me
, size_t n
)
323 size_t want
= BLEN(b
) + n
;
328 BUFERR("buffer locked");
330 while (nn
< want
) nn
<<= 1;
331 p
= xrealloc(BBASE(b
), nn
, BSZ(b
));
332 BCUR(b
) = p
+ BLEN(b
);
341 static PyObject
*wbuf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
346 static const char *const kwlist
[] = { "size", 0 };
348 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", KWLIST
,
351 me
= (buf_pyobj
*)ty
->tp_alloc(ty
, 0);
353 me
->sub
= 0; me
->lk
= 0;
354 buf_init(&me
->b
, p
, n
);
356 return ((PyObject
*)me
);
359 static Py_ssize_t
wbuf_pysegcount(PyObject
*me
, Py_ssize_t
*nn
)
360 { if (nn
) *nn
= BLEN(BUF_B(me
)); return (1); }
362 static Py_ssize_t
wbuf_pyreadbuf(PyObject
*me
, Py_ssize_t seg
, void **q
)
363 { assert(seg
== 0); *q
= BBASE(BUF_B(me
)); return (BLEN(BUF_B(me
))); }
365 static PyObject
*wbmeth_zero(PyObject
*me
, PyObject
*arg
)
369 if (!PyArg_ParseTuple(arg
, "O&:zero", convszt
, &n
)) return (0);
370 if (ensurebuf(me
, n
)) return (0);
371 p
= buf_get(BUF_B(me
), n
); assert(p
&& BOK(BUF_B(me
)));
376 static PyObject
*wbmeth_put(PyObject
*me
, PyObject
*arg
)
379 if (!PyArg_ParseTuple(arg
, "O&:put", convbin
, &in
)) return (0);
380 if (ensurebuf(me
, in
.sz
)) return (0);
381 buf_put(BUF_B(me
), in
.p
, in
.sz
); assert(BOK(BUF_B(me
)));
385 #define WBMETH_PUTU_(n, W, w) \
386 static PyObject *wbmeth_putu##w(PyObject *me, PyObject *arg) \
389 if (!PyArg_ParseTuple(arg, "O&:putu" #w, convu##n, &i)) return (0); \
390 if (ensurebuf(me, SZ_##n)) return (0); \
391 buf_putu##w(BUF_B(me), i); assert(BOK(BUF_B(me))); \
394 DOUINTCONV(WBMETH_PUTU_
)
398 #define WBMETH_PUTBLK_(n, W, w) \
399 static PyObject *wbmeth_putblk##w(PyObject *me, PyObject *arg) \
402 if (!PyArg_ParseTuple(arg, "O&:putblk" #w, convbin, &in)) goto end; \
403 if (MASK##W && in.sz > MASK##W) VALERR("too large"); \
404 if (ensurebuf(me, in.sz + SZ_##n)) return (0); \
405 buf_putmem##w(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me))); \
410 BUF_DOSUFFIXES(WBMETH_PUTBLK_
)
412 static PyObject
*wbmeth_putmp(PyObject
*me
, PyObject
*arg
)
415 if (!PyArg_ParseTuple(arg
, "O&:putmp", convmp
, &x
)) return (0);
416 if (ensurebuf(me
, mp_octets(x
) + 2)) return (0);
417 buf_putmp(BUF_B(me
), x
); assert(BOK(BUF_B(me
)));
421 static PyObject
*wbmeth_putgf(PyObject
*me
, PyObject
*arg
)
424 if (!PyArg_ParseTuple(arg
, "O&:putgf", convgf
, &x
)) return (0);
425 if (ensurebuf(me
, mp_octets(x
) + 2)) return (0);
426 buf_putmp(BUF_B(me
), x
); assert(BOK(BUF_B(me
)));
431 static PyObject
*wbmeth_putecpt(PyObject
*me
, PyObject
*arg
)
434 if (!PyArg_ParseTuple(arg
, "O&:putecpt", convecpt
, &pt
)) return (0);
435 if (ensurebuf(me
, EC_ATINF(&pt
) ?
2 :
436 6 + mp_octets(pt
.x
) + mp_octets(pt
.y
)))
438 buf_putec(BUF_B(me
), &pt
); assert(BOK(BUF_B(me
)));
443 static PyObject
*wbmeth_putecptraw(PyObject
*me
, PyObject
*arg
)
447 if (!PyArg_ParseTuple(arg
, "O!:putecptraw", ecptcurve_pytype
, &ptobj
))
449 EC_OUT(ECPT_C(ptobj
), &pt
, ECPT_P(ptobj
));
450 if (ensurebuf(me
, ECPT_C(ptobj
)->f
->noctets
* 2 + 1)) return (0);
451 ec_putraw(ECPT_C(ptobj
), BUF_B(me
), &pt
); assert(BOK(BUF_B(me
)));
456 static PyObject
*wbmeth_putge(PyObject
*me
, PyObject
*arg
)
459 if (!PyArg_ParseTuple(arg
, "O!:putge", ge_pytype
, &geobj
)) return (0);
460 if (ensurebuf(me
, GE_G(geobj
)->noctets
)) return (0);
461 G_TOBUF(GE_G(geobj
), BUF_B(me
), GE_X(geobj
)); assert(BOK(BUF_B(me
)));
465 static PyObject
*wbmeth_putgeraw(PyObject
*me
, PyObject
*arg
)
468 if (!PyArg_ParseTuple(arg
, "O!:putgeraw", ge_pytype
, &geobj
)) return (0);
469 if (ensurebuf(me
, GE_G(geobj
)->noctets
)) return (0);
470 G_TORAW(GE_G(geobj
), BUF_B(me
), GE_X(geobj
)); assert(BOK(BUF_B(me
)));
474 static PyObject
*wbget_size(PyObject
*me
, void *hunoz
)
475 { return (PyInt_FromLong(BLEN(BUF_B(me
)))); }
477 static PyObject
*wbget_contents(PyObject
*me
, void *hunoz
)
478 { return (bytestring_pywrap(BBASE(BUF_B(me
)), BLEN(BUF_B(me
)))); }
480 static const PyGetSetDef wbuf_pygetset
[] = {
481 #define GETSETNAME(op, name) wb##op##_##name
482 GET (size
, "WBUF.size -> SIZE")
483 GET (contents
, "WBUF.contents -> STR")
488 static const PyMethodDef wbuf_pymethods
[] = {
489 #define METHNAME(func) wbmeth_##func
490 METH (zero
, "WBUF.zero(N)")
491 METH (put
, "WBUF.put(BYTES)")
492 #define WBMETH_DECL_PUTU_(n, W, w) \
493 METH(putu##w, "WBUF.putu" #w "(INT)")
494 DOUINTCONV(WBMETH_DECL_PUTU_
)
495 #define WBMETH_DECL_PUTBLK_(n, W, w) \
496 METH(putblk##w, "WBUF.putblk" #w "(BYTES)")
497 BUF_DOSUFFIXES(WBMETH_DECL_PUTBLK_
)
498 METH (putmp
, "WBUF.putmp(X)")
499 METH (putgf
, "WBUF.putgf(X)")
500 METH (putecpt
, "WBUF.putecpt(P)")
501 METH (putecptraw
, "WBUF.putecptraw(P)")
502 METH (putge
, "WBUF.putge(X)")
503 METH (putgeraw
, "WBUF.putgeraw(X)")
508 static const PyBufferProcs wbuf_pybuffer
= {
509 wbuf_pyreadbuf
, /* @bf_getreadbuffer@ */
510 0, /* @bf_getwritebuffer@ */
511 wbuf_pysegcount
, /* @bf_getsegcount@ */
512 0 /* @bf_getcharbuffer@ */
515 static const PyTypeObject wbuf_pytype_skel
= {
516 PyVarObject_HEAD_INIT(0, 0) /* Header */
517 "WriteBuffer", /* @tp_name@ */
518 sizeof(buf_pyobj
), /* @tp_basicsize@ */
519 0, /* @tp_itemsize@ */
521 buf_pydealloc
, /* @tp_dealloc@ */
523 0, /* @tp_getattr@ */
524 0, /* @tp_setattr@ */
525 0, /* @tp_compare@ */
527 0, /* @tp_as_number@ */
528 0, /* @tp_as_sequence@ */
529 0, /* @tp_as_mapping@ */
533 0, /* @tp_getattro@ */
534 0, /* @tp_setattro@ */
535 PYBUFFER(wbuf
), /* @tp_as_buffer@ */
536 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
540 "WriteBuffer([size = ?]): a write buffer.",
542 0, /* @tp_traverse@ */
544 0, /* @tp_richcompare@ */
545 0, /* @tp_weaklistoffset@ */
547 0, /* @tp_iternext@ */
548 PYMETHODS(wbuf
), /* @tp_methods@ */
549 0, /* @tp_members@ */
550 PYGETSET(wbuf
), /* @tp_getset@ */
553 0, /* @tp_descr_get@ */
554 0, /* @tp_descr_set@ */
555 0, /* @tp_dictoffset@ */
557 PyType_GenericAlloc
, /* @tp_alloc@ */
558 wbuf_pynew
, /* @tp_new@ */
563 /*----- Initialization ----------------------------------------------------*/
567 void buffer_pyinit(void)
569 INITTYPE(rbuf
, root
);
570 INITTYPE(wbuf
, root
);
573 void buffer_pyinsert(PyObject
*mod
)
575 INSEXC("BufferError", buferr
, PyExc_Exception
, 0);
576 INSERT("ReadBuffer", rbuf_pytype
);
577 INSERT("WriteBuffer", wbuf_pytype
);
580 /*----- That's all, folks -------------------------------------------------*/