t/t-algorithms.py: Add tests for the new `KeySZ.pad' method.
[catacomb-python] / buffer.c
index 7220d84..5b7bb64 100644 (file)
--- a/buffer.c
+++ b/buffer.c
@@ -1,13 +1,11 @@
 /* -*-c-*-
  *
- * $Id$
- *
  * Reading and writing buffers of stuff
  *
  * (c) 2005 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of the Python interface to Catacomb.
  *
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * Catacomb/Python is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with Catacomb/Python; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
@@ -55,11 +53,11 @@ static PyObject *buferr;
 static PyObject *rbuf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
 {
   char *p, *q;
-  int n;
+  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);
@@ -79,10 +77,10 @@ static void buf_pydealloc(PyObject *me)
   FREEOBJ(me);
 }
 
-static int rbuf_pysegcount(PyObject *me, int *nn)
+static Py_ssize_t rbuf_pysegcount(PyObject *me, Py_ssize_t *nn)
   { if (nn) *nn = BSZ(BUF_B(me)); return (1); }
 
-static int rbuf_pyreadbuf(PyObject *me, int seg, void **q)
+static Py_ssize_t rbuf_pyreadbuf(PyObject *me, Py_ssize_t seg, void **q)
   { assert(seg == 0); *q = BCUR(BUF_B(me)); return (BLEFT(BUF_B(me))); }
 
 static PyObject *rbmeth_skip(PyObject *me, PyObject *arg)
@@ -114,7 +112,8 @@ end:
     uint##n x;                                                         \
     if (!PyArg_ParseTuple(arg, ":getu" #w)) goto end;                  \
     if (buf_getu##w(BUF_B(me), &x)) BUFERR();                          \
-    return (getulong(x));                                              \
+    if (MASK##W <= ULONG_MAX) return (getulong(x));                    \
+    else { kludge64 y; ASSIGN64(y, x); return (getk64(y)); }           \
   end:                                                                 \
     return (0);                                                                \
   }
@@ -173,9 +172,9 @@ 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) ||
@@ -186,7 +185,7 @@ static PyObject *rbmeth_getecpt(PyObject *me, PyObject *arg, PyObject *kw)
 end:
   return (0);
 }
-    
+
 static PyObject *rbmeth_getecptraw(PyObject *me, PyObject *arg)
 {
   PyTypeObject *cobj = ecpt_pytype;
@@ -263,14 +262,14 @@ static PyMethodDef rbuf_pymethods[] = {
     METH(getu##w, "RBUF.getu" #w "() -> INT")
   DOUINTCONV(RBMETH_DECL_GETU_)
 #define RBMETH_DECL_GETBLK_(n, W, w)                                   \
-    METH(getblk##w, "RBUF.getblk" #w "() -> INT")
+    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 "() -> INT")
+    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")
+  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")
@@ -287,7 +286,7 @@ static PyBufferProcs rbuf_pybuffer = {
 
 static PyTypeObject rbuf_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.ReadBuffer",               /* @tp_name@ */
+  "ReadBuffer",                                /* @tp_name@ */
   sizeof(buf_pyobj),                   /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -310,7 +309,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@ */
@@ -342,7 +341,7 @@ static void ensure(PyObject *me, size_t n)
   if (BLEFT(b) < n) {
     size_t nn = BSZ(b);
     octet *p;
-    size_t want = BLEFT(b) + n;
+    size_t want = BLEN(b) + n;
     while (nn < want) nn <<= 1;
     p = xrealloc(BBASE(b), nn, BSZ(b));
     BCUR(b) = p + BLEN(b);
@@ -356,9 +355,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);
@@ -369,10 +368,10 @@ end:
   return ((PyObject *)me);
 }
 
-static int wbuf_pysegcount(PyObject *me, int *nn)
+static Py_ssize_t wbuf_pysegcount(PyObject *me, Py_ssize_t *nn)
   { if (nn) *nn = BLEN(BUF_B(me)); return (1); }
 
-static int wbuf_pyreadbuf(PyObject *me, int seg, void **q)
+static Py_ssize_t wbuf_pyreadbuf(PyObject *me, Py_ssize_t seg, void **q)
   { assert(seg == 0); *q = BBASE(BUF_B(me)); return (BLEN(BUF_B(me))); }
 
 static PyObject *wbmeth_zero(PyObject *me, PyObject *arg)
@@ -389,10 +388,10 @@ static PyObject *wbmeth_zero(PyObject *me, PyObject *arg)
 static PyObject *wbmeth_put(PyObject *me, PyObject *arg)
 {
   void *p;
-  int n;
+  Py_ssize_t n;
   if (!PyArg_ParseTuple(arg, "s#:put", &p, &n)) return (0);
   ensure(me, n);
-  buf_put(BUF_B(me), p, n); assert(BOK(BUF_B(m)));
+  buf_put(BUF_B(me), p, n); assert(BOK(BUF_B(me)));
   RETURN_ME;
 }
 
@@ -407,16 +406,20 @@ static PyObject *wbmeth_put(PyObject *me, PyObject *arg)
   }
 DOUINTCONV(WBMETH_PUTU_)
 
+#define MASKz 0
 #define SZ_z 1
 #define WBMETH_PUTBLK_(n, W, w)                                                \
   static PyObject *wbmeth_putblk##w(PyObject *me, PyObject *arg)       \
   {                                                                    \
     char *p;                                                           \
-    int sz;                                                            \
-    if (!PyArg_ParseTuple(arg, "s#:putblk" #w, &p, &sz)) return (0);   \
+    Py_ssize_t sz;                                                     \
+    if (!PyArg_ParseTuple(arg, "s#:putblk" #w, &p, &sz)) goto end;     \
+    if (MASK##W && sz > MASK##W) VALERR("too large");                  \
     ensure(me, sz + SZ_##n);                                           \
     buf_putmem##w(BUF_B(me), p, sz); assert(BOK(BUF_B(me)));           \
     RETURN_ME;                                                         \
+  end:                                                                 \
+    return (0);                                                                \
   }
 BUF_DOSUFFIXES(WBMETH_PUTBLK_)
 
@@ -443,8 +446,7 @@ static PyObject *wbmeth_putecpt(PyObject *me, PyObject *arg)
 {
   ec pt = EC_INIT;
   if (!PyArg_ParseTuple(arg, "O&:putecpt", convecpt, &pt)) return (0);
-  if (EC_ATINF(&pt)) ensure(me, 2);
-  else ensure(me, 4 + mp_octets(pt.x) + mp_octets(pt.y));
+  ensure(me, EC_ATINF(&pt) ? 2 : 6 + mp_octets(pt.x) + mp_octets(pt.y));
   buf_putec(BUF_B(me), &pt); assert(BOK(BUF_B(me)));
   EC_DESTROY(&pt);
   RETURN_ME;
@@ -484,16 +486,20 @@ 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  (contents,              "WBUF.contents -> STR")
 #undef GETSETNAME
   { 0 }
 };
 
 static PyMethodDef wbuf_pymethods[] = {
 #define METHNAME(func) wbmeth_##func
-  METH (zero,                  "WBUF.skip(N)")
+  METH (zero,                  "WBUF.zero(N)")
   METH (put,                   "WBUF.put(BYTES)")
 #define WBMETH_DECL_PUTU_(n, W, w)                                     \
     METH(putu##w, "WBUF.putu" #w "(INT)")
@@ -503,7 +509,7 @@ static PyMethodDef wbuf_pymethods[] = {
   BUF_DOSUFFIXES(WBMETH_DECL_PUTBLK_)
   METH (putmp,                 "WBUF.putmp(X)")
   METH (putgf,                 "WBUF.putgf(X)")
-  KWMETH(putecpt,              "WBUF.putecpt(P)")
+  METH (putecpt,               "WBUF.putecpt(P)")
   METH (putecptraw,            "WBUF.putecptraw(P)")
   METH (putge,                 "WBUF.putge(X)")
   METH (putgeraw,              "WBUF.putgeraw(X)")
@@ -520,7 +526,7 @@ static PyBufferProcs wbuf_pybuffer = {
 
 static PyTypeObject wbuf_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.WriteBuffer",              /* @tp_name@ */
+  "WriteBuffer",                       /* @tp_name@ */
   sizeof(buf_pyobj),                   /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -543,7 +549,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@ */