X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/f52568b48a26bce7f62c3c03402e9f705f16332b..d96c882ed6f1852a266fd1a2b61bbc906cc805bb:/buffer.c diff --git a/buffer.c b/buffer.c index ac3e56b..43ed43d 100644 --- a/buffer.c +++ b/buffer.c @@ -46,7 +46,7 @@ static PyTypeObject *rbuf_pytype, *wbuf_pytype; static PyObject *buferr; -#define BUFERR() do { PyErr_SetNone(buferr); goto end; } while (0) +#define BUFERR(str) do { PyErr_SetString(buferr, str); goto end; } while (0) /*----- Read buffers ------------------------------------------------------*/ @@ -55,9 +55,9 @@ static PyObject *rbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) char *p, *q; Py_ssize_t n; buf_pyobj *me = 0; - static char *kwlist[] = { "data", 0 }; + static const char *const kwlist[] = { "data", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &p, &n)) + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &p, &n)) goto end; q = xmalloc(n); memcpy(q, p, n); @@ -88,7 +88,7 @@ static PyObject *rbmeth_skip(PyObject *me, PyObject *arg) size_t n; if (!PyArg_ParseTuple(arg, "O&:skip", convszt, &n)) goto end; - if (!buf_get(BUF_B(me), n)) BUFERR(); + if (!buf_get(BUF_B(me), n)) BUFERR("buffer exhausted"); RETURN_ME; end: return (0); @@ -100,7 +100,7 @@ static PyObject *rbmeth_get(PyObject *me, PyObject *arg) size_t n; if (!PyArg_ParseTuple(arg, "O&:get", convszt, &n)) goto end; - if ((p = buf_get(BUF_B(me), n)) == 0) BUFERR(); + if ((p = buf_get(BUF_B(me), n)) == 0) BUFERR("buffer exhausted"); return (bytestring_pywrap(p, n)); end: return (0); @@ -111,7 +111,7 @@ end: { \ uint##n x; \ if (!PyArg_ParseTuple(arg, ":getu" #w)) goto end; \ - if (buf_getu##w(BUF_B(me), &x)) BUFERR(); \ + if (buf_getu##w(BUF_B(me), &x)) BUFERR("buffer exhausted"); \ if (MASK##W <= ULONG_MAX) return (getulong(x)); \ else { kludge64 y; ASSIGN64(y, x); return (getk64(y)); } \ end: \ @@ -125,7 +125,8 @@ DOUINTCONV(RBMETH_GETU_) size_t sz; \ char *q; \ if (!PyArg_ParseTuple(arg, ":getblk" #w)) goto end; \ - if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) BUFERR(); \ + if ((q = buf_getmem##w(BUF_B(me), &sz)) == 0) \ + BUFERR("buffer exhausted"); \ return (bytestring_pywrap(q, sz)); \ end: \ return (0); \ @@ -138,7 +139,7 @@ BUF_DOSUFFIXES(RBMETH_GETBLK_) buf_pyobj *b; \ buf bb; \ if (!PyArg_ParseTuple(arg, ":getbuf" #w)) goto end; \ - if (buf_getbuf##w(BUF_B(me), &bb)) BUFERR(); \ + if (buf_getbuf##w(BUF_B(me), &bb)) BUFERR("buffer exhausted"); \ b = PyObject_NEW(buf_pyobj, rbuf_pytype); \ b->b = bb; \ b->sub = me; \ @@ -153,7 +154,7 @@ static PyObject *rbmeth_getmp(PyObject *me, PyObject *arg) { mp *x; if (!PyArg_ParseTuple(arg, ":getmp")) goto end; - if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR(); + if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR("buffer exhausted"); return (mp_pywrap(x)); end: return (0); @@ -163,7 +164,7 @@ static PyObject *rbmeth_getgf(PyObject *me, PyObject *arg) { mp *x; if (!PyArg_ParseTuple(arg, ":getgf")) goto end; - if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR(); + if ((x = buf_getmp(BUF_B(me))) == 0) BUFERR("buffer exhausted"); return (gf_pywrap(x)); end: return (0); @@ -172,15 +173,15 @@ end: static PyObject *rbmeth_getecpt(PyObject *me, PyObject *arg, PyObject *kw) { PyObject *cobj = Py_None; - static char *kwlist[] = { "curve", 0 }; + static const char *const kwlist[] = { "curve", 0 }; ec pt = EC_INIT; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:getecpt", kwlist, &cobj)) + if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:getecpt", KWLIST, &cobj)) goto end; if (cobj == Py_None) cobj = (PyObject *)ecpt_pytype; if (!PyType_Check(cobj) || !PyType_IsSubtype((PyTypeObject *)cobj, ecpt_pytype)) TYERR("expected elliptic curve type"); - if (buf_getec(BUF_B(me), &pt)) BUFERR(); + if (buf_getec(BUF_B(me), &pt)) BUFERR("buffer exhausted"); return (ecpt_pywrapout(cobj, &pt)); end: return (0); @@ -192,7 +193,7 @@ static PyObject *rbmeth_getecptraw(PyObject *me, PyObject *arg) ec pt = EC_INIT; if (!PyArg_ParseTuple(arg, "O!:getecptraw", eccurve_pytype, &cobj)) goto end; - if (ec_getraw(ECCURVE_C(cobj), BUF_B(me), &pt)) BUFERR(); + if (ec_getraw(ECCURVE_C(cobj), BUF_B(me), &pt)) BUFERR("buffer exhausted"); return (ecpt_pywrapout(cobj, &pt)); end: return (0); @@ -204,7 +205,7 @@ static PyObject *rbmeth_getge(PyObject *me, PyObject *arg) ge *x = 0; if (!PyArg_ParseTuple(arg, "O!:getge", group_pytype, &gobj)) goto end; x = G_CREATE(GROUP_G(gobj)); - if (G_FROMBUF(GROUP_G(gobj), BUF_B(me), x)) BUFERR(); + if (G_FROMBUF(GROUP_G(gobj), BUF_B(me), x)) BUFERR("buffer exhausted"); return (ge_pywrap(gobj, x)); end: if (x) G_DESTROY(GROUP_G(gobj), x); @@ -217,7 +218,7 @@ static PyObject *rbmeth_getgeraw(PyObject *me, PyObject *arg) ge *x = 0; if (!PyArg_ParseTuple(arg, "O!:getgeraw", group_pytype, &gobj)) goto end; x = G_CREATE(GROUP_G(gobj)); - if (G_FROMRAW(GROUP_G(gobj), BUF_B(me), x)) BUFERR(); + if (G_FROMRAW(GROUP_G(gobj), BUF_B(me), x)) BUFERR("buffer exhausted"); return (ge_pywrap(gobj, x)); end: if (x) G_DESTROY(GROUP_G(gobj), x); @@ -246,33 +247,33 @@ end: static PyGetSetDef rbuf_pygetset[] = { #define GETSETNAME(op, name) rb##op##_##name - GET (size, "RBUF.size -> SIZE") - GET (left, "RBUF.left -> REMAINDER") - GET (endp, "RBUF.endp -> BOOL") - GETSET(offset, "RBUF.offset -> OFFSET") + GET (size, "RBUF.size -> SIZE") + GET (left, "RBUF.left -> REMAINDER") + GET (endp, "RBUF.endp -> BOOL") + GETSET(offset, "RBUF.offset -> OFFSET") #undef GETSETNAME { 0 } }; static PyMethodDef rbuf_pymethods[] = { #define METHNAME(func) rbmeth_##func - METH (skip, "RBUF.skip(N)") - METH (get, "RBUF.get(N) -> BYTES") + METH (skip, "RBUF.skip(N)") + METH (get, "RBUF.get(N) -> BYTES") #define RBMETH_DECL_GETU_(n, W, w) \ - METH(getu##w, "RBUF.getu" #w "() -> INT") + METH(getu##w, "RBUF.getu" #w "() -> INT") DOUINTCONV(RBMETH_DECL_GETU_) #define RBMETH_DECL_GETBLK_(n, W, w) \ - METH(getblk##w, "RBUF.getblk" #w "() -> BYTES") + METH(getblk##w, "RBUF.getblk" #w "() -> BYTES") BUF_DOSUFFIXES(RBMETH_DECL_GETBLK_) #define RBMETH_DECL_GETBUF_(n, W, w) \ - METH(getbuf##w, "RBUF.getbuf" #w "() -> RBUF'") + METH(getbuf##w, "RBUF.getbuf" #w "() -> RBUF'") BUF_DOSUFFIXES(RBMETH_DECL_GETBUF_) - METH (getmp, "RBUF.getmp() -> X") - METH (getgf, "RBUF.getgf() -> X") - KWMETH(getecpt, "RBUF.getecpt([curve = None]) -> P") - METH (getecptraw, "RBUF.getecptraw(CURVE) -> P") - METH (getge, "RBUF.getge(GROUP) -> X") - METH (getgeraw, "RBUF.getgeraw(GROUP) -> X") + METH (getmp, "RBUF.getmp() -> X") + METH (getgf, "RBUF.getgf() -> X") + KWMETH(getecpt, "RBUF.getecpt([curve = None]) -> P") + METH (getecptraw, "RBUF.getecptraw(CURVE) -> P") + METH (getge, "RBUF.getge(GROUP) -> X") + METH (getgeraw, "RBUF.getgeraw(GROUP) -> X") #undef METHNAME { 0 } }; @@ -309,7 +310,7 @@ static PyTypeObject rbuf_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ - "A read buffer.", + "ReadBuffer(STR): a read buffer.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -355,9 +356,9 @@ static PyObject *wbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) char *p; size_t n = 64; buf_pyobj *me = 0; - static char *kwlist[] = { "size", 0 }; + static const char *const kwlist[] = { "size", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST, convszt, &n)) goto end; me = (buf_pyobj *)ty->tp_alloc(ty, 0); @@ -486,29 +487,33 @@ static PyObject *wbmeth_putgeraw(PyObject *me, PyObject *arg) static PyObject *wbget_size(PyObject *me, void *hunoz) { return (PyInt_FromLong(BLEN(BUF_B(me)))); } +static PyObject *wbget_contents(PyObject *me, void *hunoz) + { return (bytestring_pywrap(BBASE(BUF_B(me)), BLEN(BUF_B(me)))); } + static PyGetSetDef wbuf_pygetset[] = { #define GETSETNAME(op, name) wb##op##_##name - GET (size, "WBUF.size -> SIZE") + GET (size, "WBUF.size -> SIZE") + GET (contents, "WBUF.contents -> STR") #undef GETSETNAME { 0 } }; static PyMethodDef wbuf_pymethods[] = { #define METHNAME(func) wbmeth_##func - METH (zero, "WBUF.zero(N)") - METH (put, "WBUF.put(BYTES)") + METH (zero, "WBUF.zero(N)") + METH (put, "WBUF.put(BYTES)") #define WBMETH_DECL_PUTU_(n, W, w) \ - METH(putu##w, "WBUF.putu" #w "(INT)") + METH(putu##w, "WBUF.putu" #w "(INT)") DOUINTCONV(WBMETH_DECL_PUTU_) #define WBMETH_DECL_PUTBLK_(n, W, w) \ - METH(putblk##w, "WBUF.putblk" #w "(BYTES)") + METH(putblk##w, "WBUF.putblk" #w "(BYTES)") BUF_DOSUFFIXES(WBMETH_DECL_PUTBLK_) - METH (putmp, "WBUF.putmp(X)") - METH (putgf, "WBUF.putgf(X)") - METH (putecpt, "WBUF.putecpt(P)") - METH (putecptraw, "WBUF.putecptraw(P)") - METH (putge, "WBUF.putge(X)") - METH (putgeraw, "WBUF.putgeraw(X)") + METH (putmp, "WBUF.putmp(X)") + METH (putgf, "WBUF.putgf(X)") + METH (putecpt, "WBUF.putecpt(P)") + METH (putecptraw, "WBUF.putecptraw(P)") + METH (putge, "WBUF.putge(X)") + METH (putgeraw, "WBUF.putgeraw(X)") #undef METHNAME { 0 } }; @@ -545,7 +550,7 @@ static PyTypeObject wbuf_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ - "A write buffer.", + "WriteBuffer([size = ?]): a write buffer.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */