Commit | Line | Data |
---|---|---|
46e6ad89 | 1 | /* -*-c-*- |
46e6ad89 | 2 | * |
3 | * Reading and writing buffers of stuff | |
4 | * | |
5 | * (c) 2005 Straylight/Edgeware | |
6 | */ | |
7 | ||
b2687a0a | 8 | /*----- Licensing notice --------------------------------------------------* |
46e6ad89 | 9 | * |
10 | * This file is part of the Python interface to Catacomb. | |
11 | * | |
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. | |
b2687a0a | 16 | * |
46e6ad89 | 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. | |
b2687a0a | 21 | * |
46e6ad89 | 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. | |
25 | */ | |
26 | ||
27 | /*----- Header files ------------------------------------------------------*/ | |
28 | ||
29 | #include "catacomb-python.h" | |
30 | ||
31 | /*----- Data structures ---------------------------------------------------*/ | |
32 | ||
33 | typedef struct buf_pyobj { | |
34 | PyObject_HEAD | |
35 | buf b; | |
0463996f | 36 | PyObject *sub; |
1d460442 | 37 | unsigned lk; |
46e6ad89 | 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) | |
0463996f | 44 | #define BUF_SUB(o) (((buf_pyobj *)(o))->sub) |
1d460442 | 45 | #define BUF_LK(o) (((buf_pyobj *)(o))->lk) |
46e6ad89 | 46 | |
47 | /*----- Exceptions --------------------------------------------------------*/ | |
48 | ||
49 | static PyObject *buferr; | |
50 | ||
326f6eb0 | 51 | #define BUFERR(str) do { PyErr_SetString(buferr, str); goto end; } while (0) |
46e6ad89 | 52 | |
53 | /*----- Read buffers ------------------------------------------------------*/ | |
54 | ||
55 | static PyObject *rbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
56 | { | |
be17c8c2 MW |
57 | struct bin in; |
58 | void *q; | |
46e6ad89 | 59 | buf_pyobj *me = 0; |
827f89d7 | 60 | static const char *const kwlist[] = { "data", 0 }; |
46e6ad89 | 61 | |
be17c8c2 | 62 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &in)) |
46e6ad89 | 63 | goto end; |
be17c8c2 MW |
64 | q = xmalloc(in.sz); |
65 | memcpy(q, in.p, in.sz); | |
0463996f | 66 | me = (buf_pyobj *)ty->tp_alloc(ty, 0); |
1d460442 | 67 | me->sub = 0; me->lk = 0; |
be17c8c2 | 68 | buf_init(&me->b, q, in.sz); |
46e6ad89 | 69 | end: |
70 | return ((PyObject *)me); | |
71 | } | |
72 | ||
73 | static void buf_pydealloc(PyObject *me) | |
0463996f | 74 | { |
1d460442 MW |
75 | assert(!BUF_LK(me)); |
76 | if (BUF_SUB(me)) Py_DECREF(BUF_SUB(me)); | |
77 | else xfree(BBASE(BUF_B(me))); | |
0463996f | 78 | FREEOBJ(me); |
79 | } | |
46e6ad89 | 80 | |
3d8f5f7c | 81 | static Py_ssize_t rbuf_pysegcount(PyObject *me, Py_ssize_t *nn) |
46e6ad89 | 82 | { if (nn) *nn = BSZ(BUF_B(me)); return (1); } |
83 | ||
3d8f5f7c | 84 | static Py_ssize_t rbuf_pyreadbuf(PyObject *me, Py_ssize_t seg, void **q) |
f368b46e | 85 | { assert(seg == 0); *q = BCUR(BUF_B(me)); return (BLEFT(BUF_B(me))); } |
46e6ad89 | 86 | |
87 | static PyObject *rbmeth_skip(PyObject *me, PyObject *arg) | |
88 | { | |
89 | size_t n; | |
90 | ||
91 | if (!PyArg_ParseTuple(arg, "O&:skip", convszt, &n)) goto end; | |
326f6eb0 | 92 | if (!buf_get(BUF_B(me), n)) BUFERR("buffer exhausted"); |
46e6ad89 | 93 | RETURN_ME; |
94 | end: | |
95 | return (0); | |
96 | } | |
97 | ||
98 | static PyObject *rbmeth_get(PyObject *me, PyObject *arg) | |
99 | { | |
100 | void *p; | |
101 | size_t n; | |
102 | ||
103 | if (!PyArg_ParseTuple(arg, "O&:get", convszt, &n)) goto end; | |
326f6eb0 | 104 | if ((p = buf_get(BUF_B(me), n)) == 0) BUFERR("buffer exhausted"); |
46e6ad89 | 105 | return (bytestring_pywrap(p, n)); |
106 | end: | |
107 | return (0); | |
108 | } | |
109 | ||
110 | #define RBMETH_GETU_(n, W, w) \ | |
7a75bb76 | 111 | static PyObject *rbmeth_getu##w(PyObject *me) \ |
46e6ad89 | 112 | { \ |
113 | uint##n x; \ | |
326f6eb0 | 114 | if (buf_getu##w(BUF_B(me), &x)) BUFERR("buffer exhausted"); \ |
2da7a9c0 MW |
115 | if (MASK##W <= ULONG_MAX) return (getulong(x)); \ |
116 | else { kludge64 y; ASSIGN64(y, x); return (getk64(y)); } \ | |
46e6ad89 | 117 | end: \ |
118 | return (0); \ | |
119 | } | |
120 | DOUINTCONV(RBMETH_GETU_) | |
121 | ||
122 | #define RBMETH_GETBLK_(n, W, w) \ | |
7a75bb76 | 123 | static PyObject *rbmeth_getblk##w(PyObject *me) \ |
46e6ad89 | 124 | { \ |
125 | size_t sz; \ | |
126 | char *q; \ | |
326f6eb0 MW |
127 | if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) \ |
128 | BUFERR("buffer exhausted"); \ | |
46e6ad89 | 129 | return (bytestring_pywrap(q, sz)); \ |
130 | end: \ | |
131 | return (0); \ | |
132 | } | |
133 | BUF_DOSUFFIXES(RBMETH_GETBLK_) | |
134 | ||
0463996f | 135 | #define RBMETH_GETBUF_(n, W, w) \ |
7a75bb76 | 136 | static PyObject *rbmeth_getbuf##w(PyObject *me) \ |
0463996f | 137 | { \ |
138 | buf_pyobj *b; \ | |
139 | buf bb; \ | |
326f6eb0 | 140 | if (buf_getbuf##w(BUF_B(me), &bb)) BUFERR("buffer exhausted"); \ |
0463996f | 141 | b = PyObject_NEW(buf_pyobj, rbuf_pytype); \ |
142 | b->b = bb; \ | |
143 | b->sub = me; \ | |
1d460442 | 144 | b->lk = 0; \ |
0463996f | 145 | Py_INCREF(me); \ |
146 | return ((PyObject *)b); \ | |
147 | end: \ | |
148 | return (0); \ | |
149 | } | |
150 | BUF_DOSUFFIXES(RBMETH_GETBUF_) | |
151 | ||
7a75bb76 | 152 | static PyObject *rbmeth_getmp(PyObject *me) |
46e6ad89 | 153 | { |
154 | mp *x; | |
326f6eb0 | 155 | if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR("buffer exhausted"); |
46e6ad89 | 156 | return (mp_pywrap(x)); |
157 | end: | |
158 | return (0); | |
159 | } | |
160 | ||
7a75bb76 | 161 | static PyObject *rbmeth_getgf(PyObject *me) |
46e6ad89 | 162 | { |
163 | mp *x; | |
326f6eb0 | 164 | if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR("buffer exhausted"); |
46e6ad89 | 165 | return (gf_pywrap(x)); |
166 | end: | |
167 | return (0); | |
168 | } | |
169 | ||
170 | static PyObject *rbmeth_getecpt(PyObject *me, PyObject *arg, PyObject *kw) | |
171 | { | |
172 | PyObject *cobj = Py_None; | |
827f89d7 | 173 | static const char *const kwlist[] = { "curve", 0 }; |
46e6ad89 | 174 | ec pt = EC_INIT; |
827f89d7 | 175 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:getecpt", KWLIST, &cobj)) |
46e6ad89 | 176 | goto end; |
177 | if (cobj == Py_None) cobj = (PyObject *)ecpt_pytype; | |
178 | if (!PyType_Check(cobj) || | |
179 | !PyType_IsSubtype((PyTypeObject *)cobj, ecpt_pytype)) | |
180 | TYERR("expected elliptic curve type"); | |
326f6eb0 | 181 | if (buf_getec(BUF_B(me), &pt)) BUFERR("buffer exhausted"); |
46e6ad89 | 182 | return (ecpt_pywrapout(cobj, &pt)); |
183 | end: | |
184 | return (0); | |
185 | } | |
b2687a0a | 186 | |
46e6ad89 | 187 | static PyObject *rbmeth_getecptraw(PyObject *me, PyObject *arg) |
188 | { | |
5b1ef02a | 189 | PyObject *cobj; |
46e6ad89 | 190 | ec pt = EC_INIT; |
45fb29bc | 191 | PyObject *rc = 0; |
46e6ad89 | 192 | if (!PyArg_ParseTuple(arg, "O!:getecptraw", eccurve_pytype, &cobj)) |
193 | goto end; | |
326f6eb0 | 194 | if (ec_getraw(ECCURVE_C(cobj), BUF_B(me), &pt)) BUFERR("buffer exhausted"); |
45fb29bc | 195 | rc = ecpt_pywrapout(cobj, &pt); |
46e6ad89 | 196 | end: |
45fb29bc | 197 | return (rc); |
46e6ad89 | 198 | } |
199 | ||
200 | static PyObject *rbmeth_getge(PyObject *me, PyObject *arg) | |
201 | { | |
202 | PyObject *gobj; | |
203 | ge *x = 0; | |
204 | if (!PyArg_ParseTuple(arg, "O!:getge", group_pytype, &gobj)) goto end; | |
205 | x = G_CREATE(GROUP_G(gobj)); | |
326f6eb0 | 206 | if (G_FROMBUF(GROUP_G(gobj), BUF_B(me), x)) BUFERR("buffer exhausted"); |
46e6ad89 | 207 | return (ge_pywrap(gobj, x)); |
208 | end: | |
209 | if (x) G_DESTROY(GROUP_G(gobj), x); | |
210 | return (0); | |
211 | } | |
212 | ||
213 | static PyObject *rbmeth_getgeraw(PyObject *me, PyObject *arg) | |
214 | { | |
215 | PyObject *gobj; | |
216 | ge *x = 0; | |
217 | if (!PyArg_ParseTuple(arg, "O!:getgeraw", group_pytype, &gobj)) goto end; | |
218 | x = G_CREATE(GROUP_G(gobj)); | |
326f6eb0 | 219 | if (G_FROMRAW(GROUP_G(gobj), BUF_B(me), x)) BUFERR("buffer exhausted"); |
46e6ad89 | 220 | return (ge_pywrap(gobj, x)); |
221 | end: | |
222 | if (x) G_DESTROY(GROUP_G(gobj), x); | |
223 | return (0); | |
224 | } | |
225 | ||
226 | static PyObject *rbget_size(PyObject *me, void *hunoz) | |
227 | { return (PyInt_FromLong(BSZ(BUF_B(me)))); } | |
228 | static PyObject *rbget_left(PyObject *me, void *hunoz) | |
229 | { return (PyInt_FromLong(BLEFT(BUF_B(me)))); } | |
230 | static PyObject *rbget_endp(PyObject *me, void *hunoz) | |
231 | { return (getbool(!BLEFT(BUF_B(me)))); } | |
0463996f | 232 | static PyObject *rbget_offset(PyObject *me, void *hunoz) |
233 | { return (PyInt_FromLong(BLEN(BUF_B(me)))); } | |
234 | static int rbset_offset(PyObject *me, PyObject *x, void *hunoz) | |
235 | { | |
236 | size_t n; | |
f368b46e | 237 | if (!x) NIERR("__del__"); |
0463996f | 238 | if (!convszt(x, &n)) goto end; |
239 | if (n > BSZ(BUF_B(me))) VALERR("out of range"); | |
240 | BCUR(BUF_B(me)) = BBASE(BUF_B(me)) + n; | |
241 | return (0); | |
242 | end: | |
243 | return (-1); | |
244 | } | |
46e6ad89 | 245 | |
637b9140 | 246 | static const PyGetSetDef rbuf_pygetset[] = { |
46e6ad89 | 247 | #define GETSETNAME(op, name) rb##op##_##name |
ef783f91 MW |
248 | GET (size, "RBUF.size -> SIZE") |
249 | GET (left, "RBUF.left -> REMAINDER") | |
250 | GET (endp, "RBUF.endp -> BOOL") | |
251 | GETSET(offset, "RBUF.offset -> OFFSET") | |
46e6ad89 | 252 | #undef GETSETNAME |
253 | { 0 } | |
254 | }; | |
255 | ||
637b9140 | 256 | static const PyMethodDef rbuf_pymethods[] = { |
46e6ad89 | 257 | #define METHNAME(func) rbmeth_##func |
ef783f91 MW |
258 | METH (skip, "RBUF.skip(N)") |
259 | METH (get, "RBUF.get(N) -> BYTES") | |
46e6ad89 | 260 | #define RBMETH_DECL_GETU_(n, W, w) \ |
7a75bb76 | 261 | NAMETH(getu##w, "RBUF.getu" #w "() -> INT") |
46e6ad89 | 262 | DOUINTCONV(RBMETH_DECL_GETU_) |
263 | #define RBMETH_DECL_GETBLK_(n, W, w) \ | |
7a75bb76 | 264 | NAMETH(getblk##w, "RBUF.getblk" #w "() -> BYTES") |
46e6ad89 | 265 | BUF_DOSUFFIXES(RBMETH_DECL_GETBLK_) |
0463996f | 266 | #define RBMETH_DECL_GETBUF_(n, W, w) \ |
7a75bb76 | 267 | NAMETH(getbuf##w, "RBUF.getbuf" #w "() -> RBUF'") |
0463996f | 268 | BUF_DOSUFFIXES(RBMETH_DECL_GETBUF_) |
7a75bb76 MW |
269 | NAMETH(getmp, "RBUF.getmp() -> X") |
270 | NAMETH(getgf, "RBUF.getgf() -> X") | |
ef783f91 MW |
271 | KWMETH(getecpt, "RBUF.getecpt([curve = None]) -> P") |
272 | METH (getecptraw, "RBUF.getecptraw(CURVE) -> P") | |
273 | METH (getge, "RBUF.getge(GROUP) -> X") | |
274 | METH (getgeraw, "RBUF.getgeraw(GROUP) -> X") | |
46e6ad89 | 275 | #undef METHNAME |
276 | { 0 } | |
277 | }; | |
278 | ||
637b9140 | 279 | static const PyBufferProcs rbuf_pybuffer = { |
46e6ad89 | 280 | rbuf_pyreadbuf, /* @bf_getreadbuffer@ */ |
281 | 0, /* @bf_getwritebuffer@ */ | |
282 | rbuf_pysegcount, /* @bf_getsegcount@ */ | |
283 | 0 /* @bf_getcharbuffer@ */ | |
284 | }; | |
285 | ||
ddd4720b | 286 | static const PyTypeObject rbuf_pytype_skel = { |
4648f560 | 287 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 288 | "ReadBuffer", /* @tp_name@ */ |
46e6ad89 | 289 | sizeof(buf_pyobj), /* @tp_basicsize@ */ |
290 | 0, /* @tp_itemsize@ */ | |
291 | ||
292 | buf_pydealloc, /* @tp_dealloc@ */ | |
293 | 0, /* @tp_print@ */ | |
294 | 0, /* @tp_getattr@ */ | |
295 | 0, /* @tp_setattr@ */ | |
296 | 0, /* @tp_compare@ */ | |
297 | 0, /* @tp_repr@ */ | |
298 | 0, /* @tp_as_number@ */ | |
299 | 0, /* @tp_as_sequence@ */ | |
300 | 0, /* @tp_as_mapping@ */ | |
301 | 0, /* @tp_hash@ */ | |
302 | 0, /* @tp_call@ */ | |
303 | 0, /* @tp_str@ */ | |
304 | 0, /* @tp_getattro@ */ | |
305 | 0, /* @tp_setattro@ */ | |
637b9140 | 306 | PYBUFFER(rbuf), /* @tp_as_buffer@ */ |
46e6ad89 | 307 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
308 | Py_TPFLAGS_BASETYPE, | |
309 | ||
310 | /* @tp_doc@ */ | |
ef783f91 | 311 | "ReadBuffer(STR): a read buffer.", |
46e6ad89 | 312 | |
313 | 0, /* @tp_traverse@ */ | |
314 | 0, /* @tp_clear@ */ | |
315 | 0, /* @tp_richcompare@ */ | |
316 | 0, /* @tp_weaklistoffset@ */ | |
317 | 0, /* @tp_iter@ */ | |
963a6148 | 318 | 0, /* @tp_iternext@ */ |
637b9140 | 319 | PYMETHODS(rbuf), /* @tp_methods@ */ |
46e6ad89 | 320 | 0, /* @tp_members@ */ |
637b9140 | 321 | PYGETSET(rbuf), /* @tp_getset@ */ |
46e6ad89 | 322 | 0, /* @tp_base@ */ |
323 | 0, /* @tp_dict@ */ | |
324 | 0, /* @tp_descr_get@ */ | |
325 | 0, /* @tp_descr_set@ */ | |
326 | 0, /* @tp_dictoffset@ */ | |
327 | 0, /* @tp_init@ */ | |
328 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
329 | rbuf_pynew, /* @tp_new@ */ | |
330 | 0, /* @tp_free@ */ | |
331 | 0 /* @tp_is_gc@ */ | |
332 | }; | |
333 | ||
334 | /*----- Write buffers -----------------------------------------------------*/ | |
335 | ||
1d460442 | 336 | static int ensure(PyObject *me, size_t n) |
46e6ad89 | 337 | { |
338 | buf *b = BUF_B(me); | |
1d460442 MW |
339 | size_t nn = BSZ(b); |
340 | octet *p; | |
341 | size_t want = BLEN(b) + n; | |
46e6ad89 | 342 | |
1d460442 MW |
343 | if (BLEFT(b) >= n) |
344 | return (0); | |
345 | else if (BUF_LK(me)) | |
346 | BUFERR("buffer locked"); | |
347 | else { | |
46e6ad89 | 348 | while (nn < want) nn <<= 1; |
349 | p = xrealloc(BBASE(b), nn, BSZ(b)); | |
350 | BCUR(b) = p + BLEN(b); | |
351 | BLIM(b) = p + nn; | |
352 | BBASE(b) = p; | |
1d460442 | 353 | return (0); |
46e6ad89 | 354 | } |
1d460442 MW |
355 | end: |
356 | return (-1); | |
46e6ad89 | 357 | } |
358 | ||
359 | static PyObject *wbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
360 | { | |
361 | char *p; | |
362 | size_t n = 64; | |
363 | buf_pyobj *me = 0; | |
827f89d7 | 364 | static const char *const kwlist[] = { "size", 0 }; |
46e6ad89 | 365 | |
827f89d7 | 366 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST, |
46e6ad89 | 367 | convszt, &n)) |
368 | goto end; | |
369 | me = (buf_pyobj *)ty->tp_alloc(ty, 0); | |
370 | p = xmalloc(n); | |
1d460442 | 371 | me->sub = 0; me->lk = 0; |
46e6ad89 | 372 | buf_init(&me->b, p, n); |
373 | end: | |
374 | return ((PyObject *)me); | |
375 | } | |
376 | ||
3d8f5f7c | 377 | static Py_ssize_t wbuf_pysegcount(PyObject *me, Py_ssize_t *nn) |
46e6ad89 | 378 | { if (nn) *nn = BLEN(BUF_B(me)); return (1); } |
379 | ||
3d8f5f7c | 380 | static Py_ssize_t wbuf_pyreadbuf(PyObject *me, Py_ssize_t seg, void **q) |
46e6ad89 | 381 | { assert(seg == 0); *q = BBASE(BUF_B(me)); return (BLEN(BUF_B(me))); } |
382 | ||
383 | static PyObject *wbmeth_zero(PyObject *me, PyObject *arg) | |
384 | { | |
385 | void *p; | |
386 | size_t n; | |
387 | if (!PyArg_ParseTuple(arg, "O&:zero", convszt, &n)) return (0); | |
1d460442 | 388 | if (ensure(me, n)) return (0); |
46e6ad89 | 389 | p = buf_get(BUF_B(me), n); assert(p && BOK(BUF_B(me))); |
390 | memset(p, 0, n); | |
391 | RETURN_ME; | |
392 | } | |
393 | ||
394 | static PyObject *wbmeth_put(PyObject *me, PyObject *arg) | |
395 | { | |
be17c8c2 MW |
396 | struct bin in; |
397 | if (!PyArg_ParseTuple(arg, "O&:put", convbin, &in)) return (0); | |
1d460442 | 398 | if (ensure(me, in.sz)) return (0); |
be17c8c2 | 399 | buf_put(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me))); |
46e6ad89 | 400 | RETURN_ME; |
401 | } | |
402 | ||
403 | #define WBMETH_PUTU_(n, W, w) \ | |
404 | static PyObject *wbmeth_putu##w(PyObject *me, PyObject *arg) \ | |
405 | { \ | |
406 | uint##n i; \ | |
407 | if (!PyArg_ParseTuple(arg, "O&:putu" #w, convu##n, &i)) return (0); \ | |
1d460442 | 408 | if (ensure(me, SZ_##n)) return (0); \ |
46e6ad89 | 409 | buf_putu##w(BUF_B(me), i); assert(BOK(BUF_B(me))); \ |
410 | RETURN_ME; \ | |
411 | } | |
412 | DOUINTCONV(WBMETH_PUTU_) | |
413 | ||
9fa70ff7 | 414 | #define MASKz 0 |
46e6ad89 | 415 | #define SZ_z 1 |
416 | #define WBMETH_PUTBLK_(n, W, w) \ | |
417 | static PyObject *wbmeth_putblk##w(PyObject *me, PyObject *arg) \ | |
418 | { \ | |
be17c8c2 MW |
419 | struct bin in; \ |
420 | if (!PyArg_ParseTuple(arg, "O&:putblk" #w, convbin, &in)) goto end; \ | |
421 | if (MASK##W && in.sz > MASK##W) VALERR("too large"); \ | |
1d460442 | 422 | if (ensure(me, in.sz + SZ_##n)) return (0); \ |
be17c8c2 | 423 | buf_putmem##w(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me))); \ |
46e6ad89 | 424 | RETURN_ME; \ |
9fa70ff7 MW |
425 | end: \ |
426 | return (0); \ | |
46e6ad89 | 427 | } |
428 | BUF_DOSUFFIXES(WBMETH_PUTBLK_) | |
429 | ||
430 | static PyObject *wbmeth_putmp(PyObject *me, PyObject *arg) | |
431 | { | |
432 | mp *x = 0; | |
433 | if (!PyArg_ParseTuple(arg, "O&:putmp", convmp, &x)) return (0); | |
1d460442 | 434 | if (ensure(me, mp_octets(x) + 2)) return (0); |
46e6ad89 | 435 | buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me))); |
436 | RETURN_ME; | |
437 | } | |
438 | ||
439 | static PyObject *wbmeth_putgf(PyObject *me, PyObject *arg) | |
440 | { | |
441 | mp *x = 0; | |
442 | if (!PyArg_ParseTuple(arg, "O&:putgf", convgf, &x)) return (0); | |
1d460442 | 443 | if (ensure(me, mp_octets(x) + 2)) return (0); |
46e6ad89 | 444 | buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me))); |
445 | MP_DROP(x); | |
446 | RETURN_ME; | |
447 | } | |
448 | ||
449 | static PyObject *wbmeth_putecpt(PyObject *me, PyObject *arg) | |
450 | { | |
451 | ec pt = EC_INIT; | |
452 | if (!PyArg_ParseTuple(arg, "O&:putecpt", convecpt, &pt)) return (0); | |
1d460442 MW |
453 | if (ensure(me, EC_ATINF(&pt) ? 2 : 6 + mp_octets(pt.x) + mp_octets(pt.y))) |
454 | return (0); | |
46e6ad89 | 455 | buf_putec(BUF_B(me), &pt); assert(BOK(BUF_B(me))); |
456 | EC_DESTROY(&pt); | |
457 | RETURN_ME; | |
458 | } | |
459 | ||
460 | static PyObject *wbmeth_putecptraw(PyObject *me, PyObject *arg) | |
461 | { | |
462 | PyObject *ptobj; | |
463 | ec pt = EC_INIT; | |
464 | if (!PyArg_ParseTuple(arg, "O!:putecptraw", ecptcurve_pytype, &ptobj)) | |
465 | return (0); | |
466 | EC_OUT(ECPT_C(ptobj), &pt, ECPT_P(ptobj)); | |
1d460442 | 467 | if (ensure(me, ECPT_C(ptobj)->f->noctets * 2 + 1)) return (0); |
46e6ad89 | 468 | ec_putraw(ECPT_C(ptobj), BUF_B(me), &pt); assert(BOK(BUF_B(me))); |
469 | EC_DESTROY(&pt); | |
470 | RETURN_ME; | |
471 | } | |
472 | ||
473 | static PyObject *wbmeth_putge(PyObject *me, PyObject *arg) | |
474 | { | |
475 | PyObject *geobj; | |
476 | if (!PyArg_ParseTuple(arg, "O!:putge", ge_pytype, &geobj)) return (0); | |
1d460442 | 477 | if (ensure(me, GE_G(geobj)->noctets)) return (0); |
46e6ad89 | 478 | G_TOBUF(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me))); |
479 | RETURN_ME; | |
480 | } | |
481 | ||
482 | static PyObject *wbmeth_putgeraw(PyObject *me, PyObject *arg) | |
483 | { | |
484 | PyObject *geobj; | |
485 | if (!PyArg_ParseTuple(arg, "O!:putgeraw", ge_pytype, &geobj)) return (0); | |
1d460442 | 486 | if (ensure(me, GE_G(geobj)->noctets)) return (0); |
46e6ad89 | 487 | G_TORAW(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me))); |
488 | RETURN_ME; | |
489 | } | |
490 | ||
491 | static PyObject *wbget_size(PyObject *me, void *hunoz) | |
492 | { return (PyInt_FromLong(BLEN(BUF_B(me)))); } | |
493 | ||
0051d10f MW |
494 | static PyObject *wbget_contents(PyObject *me, void *hunoz) |
495 | { return (bytestring_pywrap(BBASE(BUF_B(me)), BLEN(BUF_B(me)))); } | |
496 | ||
637b9140 | 497 | static const PyGetSetDef wbuf_pygetset[] = { |
46e6ad89 | 498 | #define GETSETNAME(op, name) wb##op##_##name |
ef783f91 MW |
499 | GET (size, "WBUF.size -> SIZE") |
500 | GET (contents, "WBUF.contents -> STR") | |
46e6ad89 | 501 | #undef GETSETNAME |
502 | { 0 } | |
503 | }; | |
504 | ||
637b9140 | 505 | static const PyMethodDef wbuf_pymethods[] = { |
46e6ad89 | 506 | #define METHNAME(func) wbmeth_##func |
ef783f91 MW |
507 | METH (zero, "WBUF.zero(N)") |
508 | METH (put, "WBUF.put(BYTES)") | |
46e6ad89 | 509 | #define WBMETH_DECL_PUTU_(n, W, w) \ |
ef783f91 | 510 | METH(putu##w, "WBUF.putu" #w "(INT)") |
46e6ad89 | 511 | DOUINTCONV(WBMETH_DECL_PUTU_) |
512 | #define WBMETH_DECL_PUTBLK_(n, W, w) \ | |
ef783f91 | 513 | METH(putblk##w, "WBUF.putblk" #w "(BYTES)") |
46e6ad89 | 514 | BUF_DOSUFFIXES(WBMETH_DECL_PUTBLK_) |
ef783f91 MW |
515 | METH (putmp, "WBUF.putmp(X)") |
516 | METH (putgf, "WBUF.putgf(X)") | |
517 | METH (putecpt, "WBUF.putecpt(P)") | |
518 | METH (putecptraw, "WBUF.putecptraw(P)") | |
519 | METH (putge, "WBUF.putge(X)") | |
520 | METH (putgeraw, "WBUF.putgeraw(X)") | |
46e6ad89 | 521 | #undef METHNAME |
522 | { 0 } | |
523 | }; | |
524 | ||
637b9140 | 525 | static const PyBufferProcs wbuf_pybuffer = { |
46e6ad89 | 526 | wbuf_pyreadbuf, /* @bf_getreadbuffer@ */ |
527 | 0, /* @bf_getwritebuffer@ */ | |
528 | wbuf_pysegcount, /* @bf_getsegcount@ */ | |
529 | 0 /* @bf_getcharbuffer@ */ | |
530 | }; | |
531 | ||
ddd4720b | 532 | static const PyTypeObject wbuf_pytype_skel = { |
4648f560 | 533 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 534 | "WriteBuffer", /* @tp_name@ */ |
46e6ad89 | 535 | sizeof(buf_pyobj), /* @tp_basicsize@ */ |
536 | 0, /* @tp_itemsize@ */ | |
537 | ||
538 | buf_pydealloc, /* @tp_dealloc@ */ | |
539 | 0, /* @tp_print@ */ | |
540 | 0, /* @tp_getattr@ */ | |
541 | 0, /* @tp_setattr@ */ | |
542 | 0, /* @tp_compare@ */ | |
543 | 0, /* @tp_repr@ */ | |
544 | 0, /* @tp_as_number@ */ | |
545 | 0, /* @tp_as_sequence@ */ | |
546 | 0, /* @tp_as_mapping@ */ | |
547 | 0, /* @tp_hash@ */ | |
548 | 0, /* @tp_call@ */ | |
549 | 0, /* @tp_str@ */ | |
550 | 0, /* @tp_getattro@ */ | |
551 | 0, /* @tp_setattro@ */ | |
637b9140 | 552 | PYBUFFER(wbuf), /* @tp_as_buffer@ */ |
46e6ad89 | 553 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
554 | Py_TPFLAGS_BASETYPE, | |
555 | ||
556 | /* @tp_doc@ */ | |
ef783f91 | 557 | "WriteBuffer([size = ?]): a write buffer.", |
46e6ad89 | 558 | |
559 | 0, /* @tp_traverse@ */ | |
560 | 0, /* @tp_clear@ */ | |
561 | 0, /* @tp_richcompare@ */ | |
562 | 0, /* @tp_weaklistoffset@ */ | |
563 | 0, /* @tp_iter@ */ | |
963a6148 | 564 | 0, /* @tp_iternext@ */ |
637b9140 | 565 | PYMETHODS(wbuf), /* @tp_methods@ */ |
46e6ad89 | 566 | 0, /* @tp_members@ */ |
637b9140 | 567 | PYGETSET(wbuf), /* @tp_getset@ */ |
46e6ad89 | 568 | 0, /* @tp_base@ */ |
569 | 0, /* @tp_dict@ */ | |
570 | 0, /* @tp_descr_get@ */ | |
571 | 0, /* @tp_descr_set@ */ | |
572 | 0, /* @tp_dictoffset@ */ | |
573 | 0, /* @tp_init@ */ | |
574 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
575 | wbuf_pynew, /* @tp_new@ */ | |
576 | 0, /* @tp_free@ */ | |
577 | 0 /* @tp_is_gc@ */ | |
578 | }; | |
579 | ||
580 | /*----- Initialization ----------------------------------------------------*/ | |
581 | ||
582 | void buffer_pyinit(void) | |
583 | { | |
584 | INITTYPE(rbuf, root); | |
585 | INITTYPE(wbuf, root); | |
586 | } | |
587 | ||
588 | void buffer_pyinsert(PyObject *mod) | |
589 | { | |
590 | INSEXC("BufferError", buferr, PyExc_Exception, 0); | |
591 | INSERT("ReadBuffer", rbuf_pytype); | |
592 | INSERT("WriteBuffer", wbuf_pytype); | |
593 | } | |
594 | ||
595 | /*----- That's all, folks -------------------------------------------------*/ |