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