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