chiark / gitweb /
Checkin, Debianized and more or less complete.
[catacomb-python] / buffer.c
1 /* -*-c-*-
2  *
3  * $Id$
4  *
5  * Reading and writing buffers of stuff
6  *
7  * (c) 2005 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the Python interface to Catacomb.
13  *
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.
18  * 
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.
23  * 
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.
27  */
28
29 /*----- Header files ------------------------------------------------------*/
30
31 #include "catacomb-python.h"
32
33 /*----- Data structures ---------------------------------------------------*/
34
35 typedef struct buf_pyobj {
36   PyObject_HEAD
37   buf b;
38 } buf_pyobj;
39
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)
44
45 /*----- Exceptions --------------------------------------------------------*/
46
47 static PyObject *buferr;
48
49 #define BUFERR() do { PyErr_SetNone(buferr); goto end; } while (0)
50
51 /*----- Read buffers ------------------------------------------------------*/
52
53 static PyObject *rbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
54 {
55   char *p, *q;
56   int n;
57   buf_pyobj *me = 0;
58   static char *kwlist[] = { "data", 0 };
59
60   if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &p, &n))
61     goto end;
62   me = (buf_pyobj *)ty->tp_alloc(ty, 0);
63   q = xmalloc(n);
64   memcpy(q, p, n);
65   buf_init(&me->b, q, n);
66 end:
67   return ((PyObject *)me);
68 }
69
70 static void buf_pydealloc(PyObject *me)
71   { xfree(BBASE(BUF_B(me))); FREEOBJ(me); }
72
73 static int rbuf_pysegcount(PyObject *me, int *nn)
74   { if (nn) *nn = BSZ(BUF_B(me)); return (1); }
75
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))); }
78
79 static PyObject *rbmeth_skip(PyObject *me, PyObject *arg)
80 {
81   size_t n;
82
83   if (!PyArg_ParseTuple(arg, "O&:skip", convszt, &n)) goto end;
84   if (!buf_get(BUF_B(me), n)) BUFERR();
85   RETURN_ME;
86 end:
87   return (0);
88 }
89
90 static PyObject *rbmeth_get(PyObject *me, PyObject *arg)
91 {
92   void *p;
93   size_t n;
94
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));
98 end:
99   return (0);
100 }
101
102 #define RBMETH_GETU_(n, W, w)                                           \
103   static PyObject *rbmeth_getu##w(PyObject *me, PyObject *arg)          \
104   {                                                                     \
105     uint##n x;                                                          \
106     if (!PyArg_ParseTuple(arg, ":getu" #w)) goto end;                   \
107     if (buf_getu##w(BUF_B(me), &x)) BUFERR();                           \
108     return (getu32(x));                                                 \
109   end:                                                                  \
110     return (0);                                                         \
111   }
112 DOUINTCONV(RBMETH_GETU_)
113
114 #define RBMETH_GETBLK_(n, W, w)                                         \
115   static PyObject *rbmeth_getblk##w(PyObject *me, PyObject *arg)        \
116   {                                                                     \
117     size_t sz;                                                          \
118     char *q;                                                            \
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));                                  \
122   end:                                                                  \
123     return (0);                                                         \
124   }
125 BUF_DOSUFFIXES(RBMETH_GETBLK_)
126
127 static PyObject *rbmeth_getmp(PyObject *me, PyObject *arg)
128 {
129   mp *x;
130   if (!PyArg_ParseTuple(arg, ":getmp")) goto end;
131   if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR();
132   return (mp_pywrap(x));
133 end:
134   return (0);
135 }
136
137 static PyObject *rbmeth_getgf(PyObject *me, PyObject *arg)
138 {
139   mp *x;
140   if (!PyArg_ParseTuple(arg, ":getgf")) goto end;
141   if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR();
142   return (gf_pywrap(x));
143 end:
144   return (0);
145 }
146
147 static PyObject *rbmeth_getecpt(PyObject *me, PyObject *arg, PyObject *kw)
148 {
149   PyObject *cobj = Py_None;
150   static char *kwlist[] = { "curve", 0 };
151   ec pt = EC_INIT;
152   if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:getecpt", kwlist, &cobj))
153     goto end;
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));
160 end:
161   return (0);
162 }
163     
164 static PyObject *rbmeth_getecptraw(PyObject *me, PyObject *arg)
165 {
166   PyTypeObject *cobj = ecpt_pytype;
167   ec pt = EC_INIT;
168   if (!PyArg_ParseTuple(arg, "O!:getecptraw", eccurve_pytype, &cobj))
169     goto end;
170   if (ec_getraw(ECCURVE_C(cobj), BUF_B(me), &pt)) BUFERR();
171   return (ecpt_pywrapout(cobj, &pt));
172 end:
173   return (0);
174 }
175
176 static PyObject *rbmeth_getge(PyObject *me, PyObject *arg)
177 {
178   PyObject *gobj;
179   ge *x = 0;
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));
184 end:
185   if (x) G_DESTROY(GROUP_G(gobj), x);
186   return (0);
187 }
188
189 static PyObject *rbmeth_getgeraw(PyObject *me, PyObject *arg)
190 {
191   PyObject *gobj;
192   ge *x = 0;
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));
197 end:
198   if (x) G_DESTROY(GROUP_G(gobj), x);
199   return (0);
200 }
201
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)))); }
208
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")
214 #undef GETSETNAME
215   { 0 }
216 };
217
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")
234 #undef METHNAME
235   { 0 }
236 };
237
238 static PyBufferProcs rbuf_pybuffer = {
239   rbuf_pyreadbuf,                       /* @bf_getreadbuffer@ */
240   0,                                    /* @bf_getwritebuffer@ */
241   rbuf_pysegcount,                      /* @bf_getsegcount@ */
242   0                                     /* @bf_getcharbuffer@ */
243 };
244
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@ */
250
251   buf_pydealloc,                        /* @tp_dealloc@ */
252   0,                                    /* @tp_print@ */
253   0,                                    /* @tp_getattr@ */
254   0,                                    /* @tp_setattr@ */
255   0,                                    /* @tp_compare@ */
256   0,                                    /* @tp_repr@ */
257   0,                                    /* @tp_as_number@ */
258   0,                                    /* @tp_as_sequence@ */
259   0,                                    /* @tp_as_mapping@ */
260   0,                                    /* @tp_hash@ */
261   0,                                    /* @tp_call@ */
262   0,                                    /* @tp_str@ */
263   0,                                    /* @tp_getattro@ */
264   0,                                    /* @tp_setattro@ */
265   &rbuf_pybuffer,                       /* @tp_as_buffer@ */
266   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
267     Py_TPFLAGS_BASETYPE,
268
269   /* @tp_doc@ */
270   "A read buffer.",
271
272   0,                                    /* @tp_traverse@ */
273   0,                                    /* @tp_clear@ */
274   0,                                    /* @tp_richcompare@ */
275   0,                                    /* @tp_weaklistoffset@ */
276   0,                                    /* @tp_iter@ */
277   0,                                    /* @tp_iternexr@ */
278   rbuf_pymethods,                       /* @tp_methods@ */
279   0,                                    /* @tp_members@ */
280   rbuf_pygetset,                        /* @tp_getset@ */
281   0,                                    /* @tp_base@ */
282   0,                                    /* @tp_dict@ */
283   0,                                    /* @tp_descr_get@ */
284   0,                                    /* @tp_descr_set@ */
285   0,                                    /* @tp_dictoffset@ */
286   0,                                    /* @tp_init@ */
287   PyType_GenericAlloc,                  /* @tp_alloc@ */
288   rbuf_pynew,                           /* @tp_new@ */
289   0,                                    /* @tp_free@ */
290   0                                     /* @tp_is_gc@ */
291 };
292
293 /*----- Write buffers -----------------------------------------------------*/
294
295 static void ensure(PyObject *me, size_t n)
296 {
297   buf *b = BUF_B(me);
298
299   if (BLEFT(b) < n) {
300     size_t nn = BSZ(b);
301     octet *p;
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);
306     BLIM(b) = p + nn;
307     BBASE(b) = p;
308   }
309 }
310
311 static PyObject *wbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
312 {
313   char *p;
314   size_t n = 64;
315   buf_pyobj *me = 0;
316   static char *kwlist[] = { "size", 0 };
317
318   if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", kwlist,
319                                    convszt, &n))
320     goto end;
321   me = (buf_pyobj *)ty->tp_alloc(ty, 0);
322   p = xmalloc(n);
323   buf_init(&me->b, p, n);
324 end:
325   return ((PyObject *)me);
326 }
327
328 static int wbuf_pysegcount(PyObject *me, int *nn)
329   { if (nn) *nn = BLEN(BUF_B(me)); return (1); }
330
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))); }
333
334 static PyObject *wbmeth_zero(PyObject *me, PyObject *arg)
335 {
336   void *p;
337   size_t n;
338   if (!PyArg_ParseTuple(arg, "O&:zero", convszt, &n)) return (0);
339   ensure(me, n);
340   p = buf_get(BUF_B(me), n); assert(p && BOK(BUF_B(me)));
341   memset(p, 0, n);
342   RETURN_ME;
343 }
344
345 static PyObject *wbmeth_put(PyObject *me, PyObject *arg)
346 {
347   void *p;
348   int n;
349   if (!PyArg_ParseTuple(arg, "s#:put", &p, &n)) return (0);
350   ensure(me, n);
351   buf_put(BUF_B(me), p, n); assert(BOK(BUF_B(m)));
352   RETURN_ME;
353 }
354
355 #define WBMETH_PUTU_(n, W, w)                                           \
356   static PyObject *wbmeth_putu##w(PyObject *me, PyObject *arg)          \
357   {                                                                     \
358     uint##n i;                                                          \
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)));                  \
362     RETURN_ME;                                                          \
363   }
364 DOUINTCONV(WBMETH_PUTU_)
365
366 #define SZ_z 1
367 #define WBMETH_PUTBLK_(n, W, w)                                         \
368   static PyObject *wbmeth_putblk##w(PyObject *me, PyObject *arg)        \
369   {                                                                     \
370     char *p;                                                            \
371     int sz;                                                             \
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)));            \
375     RETURN_ME;                                                          \
376   }
377 BUF_DOSUFFIXES(WBMETH_PUTBLK_)
378
379 static PyObject *wbmeth_putmp(PyObject *me, PyObject *arg)
380 {
381   mp *x = 0;
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)));
385   RETURN_ME;
386 }
387
388 static PyObject *wbmeth_putgf(PyObject *me, PyObject *arg)
389 {
390   mp *x = 0;
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)));
394   MP_DROP(x);
395   RETURN_ME;
396 }
397
398 static PyObject *wbmeth_putecpt(PyObject *me, PyObject *arg)
399 {
400   ec pt = EC_INIT;
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)));
405   EC_DESTROY(&pt);
406   RETURN_ME;
407 }
408
409 static PyObject *wbmeth_putecptraw(PyObject *me, PyObject *arg)
410 {
411   PyObject *ptobj;
412   ec pt = EC_INIT;
413   if (!PyArg_ParseTuple(arg, "O!:putecptraw", ecptcurve_pytype, &ptobj))
414     return (0);
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)));
418   EC_DESTROY(&pt);
419   RETURN_ME;
420 }
421
422 static PyObject *wbmeth_putge(PyObject *me, PyObject *arg)
423 {
424   PyObject *geobj;
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)));
428   RETURN_ME;
429 }
430
431 static PyObject *wbmeth_putgeraw(PyObject *me, PyObject *arg)
432 {
433   PyObject *geobj;
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)));
437   RETURN_ME;
438 }
439
440 static PyObject *wbget_size(PyObject *me, void *hunoz)
441   { return (PyInt_FromLong(BLEN(BUF_B(me)))); }
442
443 static PyGetSetDef wbuf_pygetset[] = {
444 #define GETSETNAME(op, name) wb##op##_##name
445   GET   (size,                  "WBUF.size -> SIZE")
446 #undef GETSETNAME
447   { 0 }
448 };
449
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)")
466 #undef METHNAME
467   { 0 }
468 };
469
470 static PyBufferProcs wbuf_pybuffer = {
471   wbuf_pyreadbuf,                       /* @bf_getreadbuffer@ */
472   0,                                    /* @bf_getwritebuffer@ */
473   wbuf_pysegcount,                      /* @bf_getsegcount@ */
474   0                                     /* @bf_getcharbuffer@ */
475 };
476
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@ */
482
483   buf_pydealloc,                        /* @tp_dealloc@ */
484   0,                                    /* @tp_print@ */
485   0,                                    /* @tp_getattr@ */
486   0,                                    /* @tp_setattr@ */
487   0,                                    /* @tp_compare@ */
488   0,                                    /* @tp_repr@ */
489   0,                                    /* @tp_as_number@ */
490   0,                                    /* @tp_as_sequence@ */
491   0,                                    /* @tp_as_mapping@ */
492   0,                                    /* @tp_hash@ */
493   0,                                    /* @tp_call@ */
494   0,                                    /* @tp_str@ */
495   0,                                    /* @tp_getattro@ */
496   0,                                    /* @tp_setattro@ */
497   &wbuf_pybuffer,                       /* @tp_as_buffer@ */
498   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
499     Py_TPFLAGS_BASETYPE,
500
501   /* @tp_doc@ */
502   "A write buffer.",
503
504   0,                                    /* @tp_traverse@ */
505   0,                                    /* @tp_clear@ */
506   0,                                    /* @tp_richcompare@ */
507   0,                                    /* @tp_weaklistoffset@ */
508   0,                                    /* @tp_iter@ */
509   0,                                    /* @tp_iternexr@ */
510   wbuf_pymethods,                       /* @tp_methods@ */
511   0,                                    /* @tp_members@ */
512   wbuf_pygetset,                        /* @tp_getset@ */
513   0,                                    /* @tp_base@ */
514   0,                                    /* @tp_dict@ */
515   0,                                    /* @tp_descr_get@ */
516   0,                                    /* @tp_descr_set@ */
517   0,                                    /* @tp_dictoffset@ */
518   0,                                    /* @tp_init@ */
519   PyType_GenericAlloc,                  /* @tp_alloc@ */
520   wbuf_pynew,                           /* @tp_new@ */
521   0,                                    /* @tp_free@ */
522   0                                     /* @tp_is_gc@ */
523 };
524
525 /*----- Initialization ----------------------------------------------------*/
526
527 void buffer_pyinit(void)
528 {
529   INITTYPE(rbuf, root);
530   INITTYPE(wbuf, root);
531 }
532
533 void buffer_pyinsert(PyObject *mod)
534 {
535   INSEXC("BufferError", buferr, PyExc_Exception, 0);
536   INSERT("ReadBuffer", rbuf_pytype);
537   INSERT("WriteBuffer", wbuf_pytype);
538 }
539
540 /*----- That's all, folks -------------------------------------------------*/