From: Mark Wooding Date: Mon, 21 Oct 2019 11:01:48 +0000 (+0100) Subject: buffer.c: Publish the internal structure of buffer objects. X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/commitdiff_plain/128fdfb460f846e63811d5b1c14de89d30384793 buffer.c: Publish the internal structure of buffer objects. --- diff --git a/buffer.c b/buffer.c index 5731c53..7f535dc 100644 --- a/buffer.c +++ b/buffer.c @@ -28,30 +28,10 @@ #include "catacomb-python.h" -/*----- Data structures ---------------------------------------------------*/ - -typedef struct buf_pyobj { - PyObject_HEAD - buf b; - PyObject *sub; - unsigned lk; -} buf_pyobj; - -static PyTypeObject *rbuf_pytype, *wbuf_pytype; -#define RBUF_PYCHECK(o) PyObject_TypeCheck((o), rbuf_pytype) -#define WBUF_PYCHECK(o) PyObject_TypeCheck((o), wbuf_pytype) -#define BUF_B(o) (&((buf_pyobj *)(o))->b) -#define BUF_SUB(o) (((buf_pyobj *)(o))->sub) -#define BUF_LK(o) (((buf_pyobj *)(o))->lk) - -/*----- Exceptions --------------------------------------------------------*/ - -static PyObject *buferr; - -#define BUFERR(str) do { PyErr_SetString(buferr, str); goto end; } while (0) - /*----- Read buffers ------------------------------------------------------*/ +PyTypeObject *rbuf_pytype; + static PyObject *rbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { struct bin in; @@ -333,7 +313,9 @@ static const PyTypeObject rbuf_pytype_skel = { /*----- Write buffers -----------------------------------------------------*/ -static int ensure(PyObject *me, size_t n) +PyTypeObject *wbuf_pytype; + +int ensurebuf(PyObject *me, size_t n) { buf *b = BUF_B(me); size_t nn = BSZ(b); @@ -385,7 +367,7 @@ static PyObject *wbmeth_zero(PyObject *me, PyObject *arg) void *p; size_t n; if (!PyArg_ParseTuple(arg, "O&:zero", convszt, &n)) return (0); - if (ensure(me, n)) return (0); + if (ensurebuf(me, n)) return (0); p = buf_get(BUF_B(me), n); assert(p && BOK(BUF_B(me))); memset(p, 0, n); RETURN_ME; @@ -395,7 +377,7 @@ static PyObject *wbmeth_put(PyObject *me, PyObject *arg) { struct bin in; if (!PyArg_ParseTuple(arg, "O&:put", convbin, &in)) return (0); - if (ensure(me, in.sz)) return (0); + if (ensurebuf(me, in.sz)) return (0); buf_put(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me))); RETURN_ME; } @@ -405,7 +387,7 @@ static PyObject *wbmeth_put(PyObject *me, PyObject *arg) { \ uint##n i; \ if (!PyArg_ParseTuple(arg, "O&:putu" #w, convu##n, &i)) return (0); \ - if (ensure(me, SZ_##n)) return (0); \ + if (ensurebuf(me, SZ_##n)) return (0); \ buf_putu##w(BUF_B(me), i); assert(BOK(BUF_B(me))); \ RETURN_ME; \ } @@ -419,7 +401,7 @@ DOUINTCONV(WBMETH_PUTU_) struct bin in; \ if (!PyArg_ParseTuple(arg, "O&:putblk" #w, convbin, &in)) goto end; \ if (MASK##W && in.sz > MASK##W) VALERR("too large"); \ - if (ensure(me, in.sz + SZ_##n)) return (0); \ + if (ensurebuf(me, in.sz + SZ_##n)) return (0); \ buf_putmem##w(BUF_B(me), in.p, in.sz); assert(BOK(BUF_B(me))); \ RETURN_ME; \ end: \ @@ -431,7 +413,7 @@ static PyObject *wbmeth_putmp(PyObject *me, PyObject *arg) { mp *x = 0; if (!PyArg_ParseTuple(arg, "O&:putmp", convmp, &x)) return (0); - if (ensure(me, mp_octets(x) + 2)) return (0); + if (ensurebuf(me, mp_octets(x) + 2)) return (0); buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me))); RETURN_ME; } @@ -440,7 +422,7 @@ static PyObject *wbmeth_putgf(PyObject *me, PyObject *arg) { mp *x = 0; if (!PyArg_ParseTuple(arg, "O&:putgf", convgf, &x)) return (0); - if (ensure(me, mp_octets(x) + 2)) return (0); + if (ensurebuf(me, mp_octets(x) + 2)) return (0); buf_putmp(BUF_B(me), x); assert(BOK(BUF_B(me))); MP_DROP(x); RETURN_ME; @@ -450,7 +432,8 @@ static PyObject *wbmeth_putecpt(PyObject *me, PyObject *arg) { ec pt = EC_INIT; if (!PyArg_ParseTuple(arg, "O&:putecpt", convecpt, &pt)) return (0); - if (ensure(me, EC_ATINF(&pt) ? 2 : 6 + mp_octets(pt.x) + mp_octets(pt.y))) + if (ensurebuf(me, EC_ATINF(&pt) ? 2 : + 6 + mp_octets(pt.x) + mp_octets(pt.y))) return (0); buf_putec(BUF_B(me), &pt); assert(BOK(BUF_B(me))); EC_DESTROY(&pt); @@ -464,7 +447,7 @@ static PyObject *wbmeth_putecptraw(PyObject *me, PyObject *arg) if (!PyArg_ParseTuple(arg, "O!:putecptraw", ecptcurve_pytype, &ptobj)) return (0); EC_OUT(ECPT_C(ptobj), &pt, ECPT_P(ptobj)); - if (ensure(me, ECPT_C(ptobj)->f->noctets * 2 + 1)) return (0); + if (ensurebuf(me, ECPT_C(ptobj)->f->noctets * 2 + 1)) return (0); ec_putraw(ECPT_C(ptobj), BUF_B(me), &pt); assert(BOK(BUF_B(me))); EC_DESTROY(&pt); RETURN_ME; @@ -474,7 +457,7 @@ static PyObject *wbmeth_putge(PyObject *me, PyObject *arg) { PyObject *geobj; if (!PyArg_ParseTuple(arg, "O!:putge", ge_pytype, &geobj)) return (0); - if (ensure(me, GE_G(geobj)->noctets)) return (0); + if (ensurebuf(me, GE_G(geobj)->noctets)) return (0); G_TOBUF(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me))); RETURN_ME; } @@ -483,7 +466,7 @@ static PyObject *wbmeth_putgeraw(PyObject *me, PyObject *arg) { PyObject *geobj; if (!PyArg_ParseTuple(arg, "O!:putgeraw", ge_pytype, &geobj)) return (0); - if (ensure(me, GE_G(geobj)->noctets)) return (0); + if (ensurebuf(me, GE_G(geobj)->noctets)) return (0); G_TORAW(GE_G(geobj), BUF_B(me), GE_X(geobj)); assert(BOK(BUF_B(me))); RETURN_ME; } @@ -579,6 +562,8 @@ static const PyTypeObject wbuf_pytype_skel = { /*----- Initialization ----------------------------------------------------*/ +PyObject *buferr; + void buffer_pyinit(void) { INITTYPE(rbuf, root); diff --git a/catacomb-python.h b/catacomb-python.h index c3b120d..9b60ce4 100644 --- a/catacomb-python.h +++ b/catacomb-python.h @@ -158,6 +158,27 @@ extern PyObject *mexp_common(PyObject *, PyObject *, size_t, PyObject *bytestring_pywrap(const void *, size_t); PyObject *bytestring_pywrapbuf(buf *); +/*----- Buffers -----------------------------------------------------------*/ + +typedef struct buf_pyobj { + PyObject_HEAD + buf b; + PyObject *sub; + unsigned lk; +} buf_pyobj; + +extern PyTypeObject *rbuf_pytype, *wbuf_pytype; +#define RBUF_PYCHECK(o) PyObject_TypeCheck((o), rbuf_pytype) +#define WBUF_PYCHECK(o) PyObject_TypeCheck((o), wbuf_pytype) +#define BUF_B(o) (&((buf_pyobj *)(o))->b) +#define BUF_SUB(o) (((buf_pyobj *)(o))->sub) +#define BUF_LK(o) (((buf_pyobj *)(o))->lk) + +extern PyObject *buferr; +#define BUFERR(str) do { PyErr_SetString(buferr, str); goto end; } while (0) + +extern int ensurebuf(PyObject *, size_t); + /*----- Multiprecision arithmetic -----------------------------------------*/ typedef struct mp_pyobj {