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); }
63 static Py_ssize_t
rbuf_pyreadbuf(PyObject
*me
, Py_ssize_t seg
, void **q
)
64 { assert(seg
== 0); *q
= BCUR(BUF_B(me
)); return (BLEFT(BUF_B(me
))); }
66 static PyObject
*rbmeth_skip(PyObject
*me
, PyObject
*arg
)
70 if (!PyArg_ParseTuple(arg
, "O&:skip", convszt
, &n
)) goto end
;
71 if (!buf_get(BUF_B(me
), n
)) BUFERR("buffer exhausted");
77 static PyObject
*rbmeth_get(PyObject
*me
, PyObject
*arg
)
82 if (!PyArg_ParseTuple(arg
, "O&:get", convszt
, &n
)) goto end
;
83 if ((p
= buf_get(BUF_B(me
), n
)) == 0) BUFERR("buffer exhausted");
84 return (bytestring_pywrap(p
, n
));
89 #define RBMETH_GETU_(n, W, w) \
90 static PyObject *rbmeth_getu##w(PyObject *me) \
93 if (buf_getu##w(BUF_B(me), &x)) BUFERR("buffer exhausted"); \
94 if (MASK##W <= ULONG_MAX) return (getulong(x)); \
95 else { kludge64 y; ASSIGN64(y, x); return (getk64(y)); } \
99 DOUINTCONV(RBMETH_GETU_
)
101 #define RBMETH_GETBLK_(n, W, w) \
102 static PyObject *rbmeth_getblk##w(PyObject *me) \
106 if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) \
107 BUFERR("buffer exhausted"); \
108 return (bytestring_pywrap(q, sz)); \
112 BUF_DOSUFFIXES(RBMETH_GETBLK_
)
114 #define RBMETH_GETBUF_(n, W, w) \
115 static PyObject *rbmeth_getbuf##w(PyObject *me) \
119 if (buf_getbuf##w(BUF_B(me), &bb)) BUFERR("buffer exhausted"); \
120 b = PyObject_NEW(buf_pyobj, rbuf_pytype); \
125 return ((PyObject *)b); \
129 BUF_DOSUFFIXES(RBMETH_GETBUF_
)
131 static PyObject
*rbmeth_getmp(PyObject
*me
)
134 if ((x
= buf_getmp(BUF_B(me
))) == 0) BUFERR("buffer exhausted");
135 return (mp_pywrap(x
));
140 static PyObject
*rbmeth_getgf(PyObject
*me
)
143 if ((x
= buf_getmp(BUF_B(me
))) == 0) BUFERR("buffer exhausted");
144 return (gf_pywrap(x
));
149 static PyObject
*rbmeth_getecpt(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
151 PyObject
*cobj
= Py_None
;
152 static const char *const kwlist
[] = { "curve", 0 };
154 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:getecpt", KWLIST
, &cobj
))
156 if (cobj
== Py_None
) cobj
= (PyObject
*)ecpt_pytype
;
157 if (!PyType_Check(cobj
) ||
158 !PyType_IsSubtype((PyTypeObject
*)cobj
, ecpt_pytype
))
159 TYERR("expected elliptic curve type");
160 if (buf_getec(BUF_B(me
), &pt
)) BUFERR("buffer exhausted");
161 return (ecpt_pywrapout(cobj
, &pt
));
166 static PyObject
*rbmeth_getecptraw(PyObject
*me
, PyObject
*arg
)
171 if (!PyArg_ParseTuple(arg
, "O!:getecptraw", eccurve_pytype
, &cobj
))
173 if (ec_getraw(ECCURVE_C(cobj
), BUF_B(me
), &pt
)) BUFERR("buffer exhausted");
174 rc
= ecpt_pywrapout(cobj
, &pt
);
179 static PyObject
*rbmeth_getge(PyObject
*me
, PyObject
*arg
)
183 if (!PyArg_ParseTuple(arg
, "O!:getge", group_pytype
, &gobj
)) goto end
;
184 x
= G_CREATE(GROUP_G(gobj
));
185 if (G_FROMBUF(GROUP_G(gobj
), BUF_B(me
), x
)) BUFERR("buffer exhausted");
186 return (ge_pywrap(gobj
, x
));
188 if (x
) G_DESTROY(GROUP_G(gobj
), x
);
192 static PyObject
*rbmeth_getgeraw(PyObject
*me
, PyObject
*arg
)
196 if (!PyArg_ParseTuple(arg
, "O!:getgeraw", group_pytype
, &gobj
)) goto end
;
197 x
= G_CREATE(GROUP_G(gobj
));
198 if (G_FROMRAW(GROUP_G(gobj
), BUF_B(me
), x
)) BUFERR("buffer exhausted");
199 return (ge_pywrap(gobj
, x
));
201 if (x
) G_DESTROY(GROUP_G(gobj
), x
);
205 static PyObject
*rbget_size(PyObject
*me
, void *hunoz
)
206 { return (PyInt_FromLong(BSZ(BUF_B(me
)))); }
207 static PyObject
*rbget_left(PyObject
*me
, void *hunoz
)
208 { return (PyInt_FromLong(BLEFT(BUF_B(me
)))); }
209 static PyObject
*rbget_endp(PyObject
*me
, void *hunoz
)
210 { return (getbool(!BLEFT(BUF_B(me
)))); }
211 static PyObject
*rbget_offset(PyObject
*me
, void *hunoz
)
212 { return (PyInt_FromLong(BLEN(BUF_B(me
)))); }
213 static int rbset_offset(PyObject
*me
, PyObject
*x
, void *hunoz
)
216 if (!x
) NIERR("__del__");
217 if (!convszt(x
, &n
)) goto end
;
218 if (n
> BSZ(BUF_B(me
))) VALERR("out of range");
219 BCUR(BUF_B(me
)) = BBASE(BUF_B(me
)) + n
;
225 static const PyGetSetDef rbuf_pygetset
[] = {
226 #define GETSETNAME(op, name) rb##op##_##name
227 GET (size
, "RBUF.size -> SIZE")
228 GET (left
, "RBUF.left -> REMAINDER")
229 GET (endp
, "RBUF.endp -> BOOL")
230 GETSET(offset
, "RBUF.offset -> OFFSET")
235 static const PyMethodDef rbuf_pymethods
[] = {
236 #define METHNAME(func) rbmeth_##func
237 METH (skip
, "RBUF.skip(N)")
238 METH (get
, "RBUF.get(N) -> BYTES")
239 #define RBMETH_DECL_GETU_(n, W, w) \
240 NAMETH(getu##w, "RBUF.getu" #w "() -> INT")
241 DOUINTCONV(RBMETH_DECL_GETU_
)
242 #define RBMETH_DECL_GETBLK_(n, W, w) \
243 NAMETH(getblk##w, "RBUF.getblk" #w "() -> BYTES")
244 BUF_DOSUFFIXES(RBMETH_DECL_GETBLK_
)
245 #define RBMETH_DECL_GETBUF_(n, W, w) \
246 NAMETH(getbuf##w, "RBUF.getbuf" #w "() -> RBUF'")
247 BUF_DOSUFFIXES(RBMETH_DECL_GETBUF_
)
248 NAMETH(getmp
, "RBUF.getmp() -> X")
249 NAMETH(getgf
, "RBUF.getgf() -> X")
250 KWMETH(getecpt
, "RBUF.getecpt([curve = None]) -> P")
251 METH (getecptraw
, "RBUF.getecptraw(CURVE) -> P")
252 METH (getge
, "RBUF.getge(GROUP) -> X")
253 METH (getgeraw
, "RBUF.getgeraw(GROUP) -> X")
258 static const PyBufferProcs rbuf_pybuffer
= {
259 rbuf_pyreadbuf
, /* @bf_getreadbuffer@ */
260 0, /* @bf_getwritebuffer@ */
261 rbuf_pysegcount
, /* @bf_getsegcount@ */
262 0 /* @bf_getcharbuffer@ */
265 static const PyTypeObject rbuf_pytype_skel
= {
266 PyVarObject_HEAD_INIT(0, 0) /* Header */
267 "ReadBuffer", /* @tp_name@ */
268 sizeof(buf_pyobj
), /* @tp_basicsize@ */
269 0, /* @tp_itemsize@ */
271 buf_pydealloc
, /* @tp_dealloc@ */
273 0, /* @tp_getattr@ */
274 0, /* @tp_setattr@ */
275 0, /* @tp_compare@ */
277 0, /* @tp_as_number@ */
278 0, /* @tp_as_sequence@ */
279 0, /* @tp_as_mapping@ */
283 0, /* @tp_getattro@ */
284 0, /* @tp_setattro@ */
285 PYBUFFER(rbuf
), /* @tp_as_buffer@ */
286 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
290 "ReadBuffer(STR): a read buffer.",
292 0, /* @tp_traverse@ */
294 0, /* @tp_richcompare@ */
295 0, /* @tp_weaklistoffset@ */
297 0, /* @tp_iternext@ */
298 PYMETHODS(rbuf
), /* @tp_methods@ */
299 0, /* @tp_members@ */
300 PYGETSET(rbuf
), /* @tp_getset@ */
303 0, /* @tp_descr_get@ */
304 0, /* @tp_descr_set@ */
305 0, /* @tp_dictoffset@ */
307 PyType_GenericAlloc
, /* @tp_alloc@ */
308 rbuf_pynew
, /* @tp_new@ */
313 /*----- Write buffers -----------------------------------------------------*/
315 PyTypeObject
*wbuf_pytype
;
317 int ensurebuf(PyObject
*me
, size_t n
)
322 size_t want
= BLEN(b
) + n
;
327 BUFERR("buffer locked");
329 while (nn
< want
) nn
<<= 1;
330 p
= xrealloc(BBASE(b
), nn
, BSZ(b
));
331 BCUR(b
) = p
+ BLEN(b
);
340 static PyObject
*wbuf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
345 static const char *const kwlist
[] = { "size", 0 };
347 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", KWLIST
,
350 me
= (buf_pyobj
*)ty
->tp_alloc(ty
, 0);
352 me
->sub
= 0; me
->lk
= 0;
353 buf_init(&me
->b
, p
, n
);
355 return ((PyObject
*)me
);
358 static Py_ssize_t
wbuf_pysegcount(PyObject
*me
, Py_ssize_t
*nn
)
359 { if (nn
) *nn
= BLEN(BUF_B(me
)); return (1); }
360 static Py_ssize_t
wbuf_pyreadbuf(PyObject
*me
, Py_ssize_t seg
, void **q
)
361 { assert(seg
== 0); *q
= BBASE(BUF_B(me
)); return (BLEN(BUF_B(me
))); }
363 static PyObject
*wbmeth_zero(PyObject
*me
, PyObject
*arg
)
367 if (!PyArg_ParseTuple(arg
, "O&:zero", convszt
, &n
)) return (0);
368 if (ensurebuf(me
, n
)) return (0);
369 p
= buf_get(BUF_B(me
), n
); assert(p
&& BOK(BUF_B(me
)));
374 static PyObject
*wbmeth_put(PyObject
*me
, PyObject
*arg
)
377 if (!PyArg_ParseTuple(arg
, "O&:put", convbin
, &in
)) return (0);
378 if (ensurebuf(me
, in
.sz
)) return (0);
379 buf_put(BUF_B(me
), in
.p
, in
.sz
); assert(BOK(BUF_B(me
)));
383 #define WBMETH_PUTU_(n, W, w) \
384 static PyObject *wbmeth_putu##w(PyObject *me, PyObject *arg) \
387 if (!PyArg_ParseTuple(arg, "O&:putu" #w, convu##n, &i)) return (0); \
388 if (ensurebuf(me, SZ_##n)) return (0); \
389 buf_putu##w(BUF_B(me), i); assert(BOK(BUF_B(me))); \
392 DOUINTCONV(WBMETH_PUTU_
)
396 #define WBMETH_PUTBLK_(n, W, w) \
397 static PyObject *wbmeth_putblk##w(PyObject *me, PyObject *arg) \
400 if (!PyArg_ParseTuple(arg, "O&:putblk" #w, convbin, &in)) goto end; \
401 if (MASK##W && in.sz > MASK##W) VALERR("too large"); \
402 if (ensurebuf(me, in.sz + SZ_##n)) return (0); \
403 buf_putmem##w(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me))); \
408 BUF_DOSUFFIXES(WBMETH_PUTBLK_
)
410 static PyObject
*wbmeth_putmp(PyObject
*me
, PyObject
*arg
)
413 if (!PyArg_ParseTuple(arg
, "O&:putmp", convmp
, &x
)) return (0);
414 if (ensurebuf(me
, mp_octets(x
) + 2)) return (0);
415 buf_putmp(BUF_B(me
), x
); assert(BOK(BUF_B(me
)));
419 static PyObject
*wbmeth_putgf(PyObject
*me
, PyObject
*arg
)
422 if (!PyArg_ParseTuple(arg
, "O&:putgf", convgf
, &x
)) return (0);
423 if (ensurebuf(me
, mp_octets(x
) + 2)) return (0);
424 buf_putmp(BUF_B(me
), x
); assert(BOK(BUF_B(me
)));
429 static PyObject
*wbmeth_putecpt(PyObject
*me
, PyObject
*arg
)
432 if (!PyArg_ParseTuple(arg
, "O&:putecpt", convecpt
, &pt
)) return (0);
433 if (ensurebuf(me
, EC_ATINF(&pt
) ?
2 :
434 6 + mp_octets(pt
.x
) + mp_octets(pt
.y
)))
436 buf_putec(BUF_B(me
), &pt
); assert(BOK(BUF_B(me
)));
441 static PyObject
*wbmeth_putecptraw(PyObject
*me
, PyObject
*arg
)
445 if (!PyArg_ParseTuple(arg
, "O!:putecptraw", ecptcurve_pytype
, &ptobj
))
447 EC_OUT(ECPT_C(ptobj
), &pt
, ECPT_P(ptobj
));
448 if (ensurebuf(me
, ECPT_C(ptobj
)->f
->noctets
* 2 + 1)) return (0);
449 ec_putraw(ECPT_C(ptobj
), BUF_B(me
), &pt
); assert(BOK(BUF_B(me
)));
454 static PyObject
*wbmeth_putge(PyObject
*me
, PyObject
*arg
)
457 if (!PyArg_ParseTuple(arg
, "O!:putge", ge_pytype
, &geobj
)) return (0);
458 if (ensurebuf(me
, GE_G(geobj
)->noctets
)) return (0);
459 G_TOBUF(GE_G(geobj
), BUF_B(me
), GE_X(geobj
)); assert(BOK(BUF_B(me
)));
463 static PyObject
*wbmeth_putgeraw(PyObject
*me
, PyObject
*arg
)
466 if (!PyArg_ParseTuple(arg
, "O!:putgeraw", ge_pytype
, &geobj
)) return (0);
467 if (ensurebuf(me
, GE_G(geobj
)->noctets
)) return (0);
468 G_TORAW(GE_G(geobj
), BUF_B(me
), GE_X(geobj
)); assert(BOK(BUF_B(me
)));
472 static PyObject
*wbget_size(PyObject
*me
, void *hunoz
)
473 { return (PyInt_FromLong(BLEN(BUF_B(me
)))); }
475 static PyObject
*wbget_contents(PyObject
*me
, void *hunoz
)
476 { return (bytestring_pywrap(BBASE(BUF_B(me
)), BLEN(BUF_B(me
)))); }
478 static const PyGetSetDef wbuf_pygetset
[] = {
479 #define GETSETNAME(op, name) wb##op##_##name
480 GET (size
, "WBUF.size -> SIZE")
481 GET (contents
, "WBUF.contents -> STR")
486 static const PyMethodDef wbuf_pymethods
[] = {
487 #define METHNAME(func) wbmeth_##func
488 METH (zero
, "WBUF.zero(N)")
489 METH (put
, "WBUF.put(BYTES)")
490 #define WBMETH_DECL_PUTU_(n, W, w) \
491 METH(putu##w, "WBUF.putu" #w "(INT)")
492 DOUINTCONV(WBMETH_DECL_PUTU_
)
493 #define WBMETH_DECL_PUTBLK_(n, W, w) \
494 METH(putblk##w, "WBUF.putblk" #w "(BYTES)")
495 BUF_DOSUFFIXES(WBMETH_DECL_PUTBLK_
)
496 METH (putmp
, "WBUF.putmp(X)")
497 METH (putgf
, "WBUF.putgf(X)")
498 METH (putecpt
, "WBUF.putecpt(P)")
499 METH (putecptraw
, "WBUF.putecptraw(P)")
500 METH (putge
, "WBUF.putge(X)")
501 METH (putgeraw
, "WBUF.putgeraw(X)")
506 static const PyBufferProcs wbuf_pybuffer
= {
507 wbuf_pyreadbuf
, /* @bf_getreadbuffer@ */
508 0, /* @bf_getwritebuffer@ */
509 wbuf_pysegcount
, /* @bf_getsegcount@ */
510 0 /* @bf_getcharbuffer@ */
513 static const PyTypeObject wbuf_pytype_skel
= {
514 PyVarObject_HEAD_INIT(0, 0) /* Header */
515 "WriteBuffer", /* @tp_name@ */
516 sizeof(buf_pyobj
), /* @tp_basicsize@ */
517 0, /* @tp_itemsize@ */
519 buf_pydealloc
, /* @tp_dealloc@ */
521 0, /* @tp_getattr@ */
522 0, /* @tp_setattr@ */
523 0, /* @tp_compare@ */
525 0, /* @tp_as_number@ */
526 0, /* @tp_as_sequence@ */
527 0, /* @tp_as_mapping@ */
531 0, /* @tp_getattro@ */
532 0, /* @tp_setattro@ */
533 PYBUFFER(wbuf
), /* @tp_as_buffer@ */
534 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
538 "WriteBuffer([size = ?]): a write buffer.",
540 0, /* @tp_traverse@ */
542 0, /* @tp_richcompare@ */
543 0, /* @tp_weaklistoffset@ */
545 0, /* @tp_iternext@ */
546 PYMETHODS(wbuf
), /* @tp_methods@ */
547 0, /* @tp_members@ */
548 PYGETSET(wbuf
), /* @tp_getset@ */
551 0, /* @tp_descr_get@ */
552 0, /* @tp_descr_set@ */
553 0, /* @tp_dictoffset@ */
555 PyType_GenericAlloc
, /* @tp_alloc@ */
556 wbuf_pynew
, /* @tp_new@ */
561 /*----- Initialization ----------------------------------------------------*/
565 void buffer_pyinit(void)
567 INITTYPE(rbuf
, root
);
568 INITTYPE(wbuf
, root
);
571 void buffer_pyinsert(PyObject
*mod
)
573 INSEXC("BufferError", buferr
, PyExc_Exception
, 0);
574 INSERT("ReadBuffer", rbuf_pytype
);
575 INSERT("WriteBuffer", wbuf_pytype
);
578 /*----- That's all, folks -------------------------------------------------*/