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