*.c: Introduce a new input conversion for binary strings.
[catacomb-python] / bytestring.c
index 8e1d19a..fdc6e50 100644 (file)
@@ -76,21 +76,19 @@ PyObject *bytestring_pywrapbuf(buf *b)
 static PyObject *bytestring_pynew(PyTypeObject *ty,
                                  PyObject *arg, PyObject *kw)
 {
-  const char *p;
-  Py_ssize_t n;
+  struct bin in;
   static const char *const kwlist[] = { "data", 0 };
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &p, &n))
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &in))
     return (0);
-  return (dowrap(ty, p, n));
+  return (dowrap(ty, in.p, in.sz));
 }
 
 static PyObject *meth_ctstreq(PyObject *me, PyObject *arg)
 {
-  char *p, *q;
-  Py_ssize_t psz, qsz;
-  if (!PyArg_ParseTuple(arg, "s#s#:ctstreq", &p, &psz, &q, &qsz))
+  struct bin s0, s1;
+  if (!PyArg_ParseTuple(arg, "O&O&:ctstreq", convbin, &s0 , convbin, &s1))
     goto end;
-  if (psz == qsz && ct_memeq(p, q, psz)) RETURN_TRUE;
+  if (s0.sz == s1.sz && ct_memeq(s0.p, s1.p, s0.sz)) RETURN_TRUE;
   else RETURN_FALSE;
 end:
   return (0);
@@ -110,25 +108,24 @@ end:
 static PyObject *bytestring_pyrichcompare(PyObject *me,
                                          PyObject *you, int op)
 {
+  struct bin s0, s1;
   int b;
-  void *mystr, *yourstr;
-  Py_ssize_t mylen, yourlen, minlen;
+  Py_ssize_t minlen;
 
-  if (!PyString_Check(me) || !PyString_Check(you)) RETURN_NOTIMPL;
-  mystr = PyString_AS_STRING(me); mylen = PyString_GET_SIZE(me);
-  yourstr = PyString_AS_STRING(you); yourlen = PyString_GET_SIZE(you);
+  s0.p = PyString_AS_STRING(me); s0.sz = PyString_GET_SIZE(me);
+  if (!convbin(you, &s1)) { PyErr_Clear(); RETURN_NOTIMPL; }
 
   switch (op) {
     case Py_EQ:
-      b = mylen == yourlen && ct_memeq(mystr, yourstr, mylen);
+      b = s0.sz == s1.sz && ct_memeq(s0.p, s1.p, s1.sz);
       break;
     case Py_NE:
-      b = mylen != yourlen || !ct_memeq(mystr, yourstr, mylen);
+      b = s0.sz != s1.sz || !ct_memeq(s0.p, s1.p, s1.sz);
       break;
     default:
-      minlen = mylen < yourlen ? mylen : yourlen;
-      b = memcmp(mystr, yourstr, minlen);
-      if (!b) b = mylen < yourlen ? -1 : mylen > yourlen ? +1 : 0;
+      minlen = s0.sz < s1.sz ? s0.sz : s1.sz;
+      b = memcmp(s0.p, s1.p, minlen);
+      if (!b) b = s0.sz < s1.sz ? -1 : s0.sz > s1.sz ? +1 : 0;
       switch (op) {
        case Py_LT: b = b <  0; break;
        case Py_LE: b = b <= 0; break;
@@ -143,17 +140,14 @@ static PyObject *bytestring_pyrichcompare(PyObject *me,
 
 static PyObject *bytestring_pyconcat(PyObject *x, PyObject *y)
 {
-  const void *xv; Py_ssize_t xsz;
-  const void *yv; Py_ssize_t ysz;
+  struct bin xx, yy;
   PyObject *z = 0; char *zp; size_t zsz;
 
-  if (PyObject_AsReadBuffer(x, &xv, &xsz) ||
-      PyObject_AsReadBuffer(y, &yv, &ysz))
-    goto end;
-  zsz = (size_t)xsz + (size_t)ysz;
-  if (xsz < 0 || ysz < 0 || zsz < xsz) VALERR("too long");
+  if (!convbin(x, &xx) || !convbin(y, &yy)) goto end;
+  zsz = (size_t)xx.sz + (size_t)yy.sz;
+  if (xx.sz < 0 || yy.sz < 0 || zsz < xx.sz) VALERR("too long");
   z = bytestring_pywrap(0, zsz); zp = PyString_AS_STRING(z);
-  memcpy(zp, xv, xsz); memcpy(zp + xsz, yv, ysz);
+  memcpy(zp, xx.p, xx.sz); memcpy(zp + xx.sz, yy.p, yy.sz);
 end:
   return (z);
 }
@@ -228,19 +222,16 @@ end:
 
 #define BINOP(name, op)                                                        \
   static PyObject *bytestring_py##name(PyObject *x, PyObject *y) {     \
-    const void *xv, *yv;                                               \
+    struct bin xx, yy;                                                 \
     const unsigned char *xp, *yp;                                      \
     unsigned char *zp;                                                 \
-    Py_ssize_t xsz, ysz;                                               \
     int i;                                                             \
     PyObject *rc = 0;                                                  \
-    if (PyObject_AsReadBuffer(x, &xv, &xsz) ||                         \
-       PyObject_AsReadBuffer(y, &yv, &ysz))                            \
-      goto end;                                                                \
-    if (xsz != ysz) VALERR("length mismatch");                         \
-    rc = bytestring_pywrap(0, xsz);                                    \
-    xp = xv; yp = yv; zp = (unsigned char *)PyString_AS_STRING(rc);    \
-    for (i = xsz; i > 0; i--) *zp++ = *xp++ op *yp++;                  \
+    if (!convbin(x, &xx) || !convbin(y, &yy)) goto end;                        \
+    if (xx.sz != yy.sz) VALERR("length mismatch");                     \
+    rc = bytestring_pywrap(0, xx.sz);                                  \
+    xp = xx.p; yp = yy.p; zp = (unsigned char *)PyString_AS_STRING(rc);        \
+    for (i = xx.sz; i > 0; i--) *zp++ = *xp++ op *yp++;                        \
   end:                                                                 \
     return (rc);                                                       \
   }
@@ -250,16 +241,15 @@ BINOP(xor, ^)
 
 #define UNOP(name, op)                                                 \
   static PyObject *bytestring_py##name(PyObject *x) {                  \
-    const void *xv;                                                    \
+    struct bin xx;                                                     \
     const unsigned char *xp;                                           \
     unsigned char *zp;                                                 \
-    Py_ssize_t xsz;                                                    \
     int i;                                                             \
     PyObject *rc = 0;                                                  \
-    if (PyObject_AsReadBuffer(x, &xv, &xsz)) goto end;                 \
-    rc = bytestring_pywrap(0, xsz);                                    \
-    xp = xv; zp = (unsigned char *)PyString_AS_STRING(rc);             \
-    for (i = xsz; i > 0; i--) *zp++ = op *xp++;                                \
+    if (!convbin(x, &xx)) goto end;                                    \
+    rc = bytestring_pywrap(0, xx.sz);                                  \
+    xp = xx.p; zp = (unsigned char *)PyString_AS_STRING(rc);           \
+    for (i = xx.sz; i > 0; i--) *zp++ = op *xp++;                      \
   end:                                                                 \
     return (rc);                                                       \
   }