chiark / gitweb /
algorithms.c: Add bindings for HSalsa20/r and HChaCha/r.
[catacomb-python] / bytestring.c
index c6491252419f1e8887451545323052b3fcbc89cb..b3b32de52bd6159416412277b838b758b520f8f0 100644 (file)
@@ -1,13 +1,11 @@
 /* -*-c-*-
- *
- * $Id$
  *
  * Byte strings
  *
  * (c) 2004 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.
 
 PyTypeObject *bytestring_pytype;
 
-PyObject *bytestring_pywrap(const void *p, size_t n)
+static PyObject *dowrap(PyTypeObject *ty, const void *p, size_t n)
 {
-  PyStringObject *x = PyObject_NewVar(PyStringObject, bytestring_pytype, n);
+  PyStringObject *x = (PyStringObject *)ty->tp_alloc(ty, n);
   if (p) memcpy(x->ob_sval, p, n);
   x->ob_sval[n] = 0;
 #ifdef CACHE_HASH
   x->ob_shash = -1;
 #endif
-#ifdef INTERN_STRINGS
-  x->ob_sinterned = NULL;
-#endif
+  x->ob_sstate = SSTATE_NOT_INTERNED;
   return ((PyObject *)x);
 }
 
+PyObject *bytestring_pywrap(const void *p, size_t n)
+  { return (dowrap(bytestring_pytype, p, n)); }
+
 PyObject *bytestring_pywrapbuf(buf *b)
-  { return bytestring_pywrap(BCUR(b), BLEFT(b)); }
+  { return (dowrap(bytestring_pytype, BCUR(b), BLEFT(b))); }
+
+static PyObject *bytestring_pynew(PyTypeObject *ty,
+                                 PyObject *arg, PyObject *kw)
+{
+  const char *p;
+  int n;
+  static char *kwlist[] = { "data", 0 };
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &p, &n))
+    return (0);
+  return (dowrap(ty, p, n));
+}
 
 #define BINOP(name, op)                                                        \
   static PyObject *bytestring_py##name(PyObject *x, PyObject *y) {     \
     const void *xv, *yv;                                               \
     const unsigned char *xp, *yp;                                      \
     unsigned char *zp;                                                 \
-    int xsz, ysz;                                                      \
+    Py_ssize_t xsz, ysz;                                               \
     int i;                                                             \
     PyObject *rc = 0;                                                  \
     if (PyObject_AsReadBuffer(x, &xv, &xsz) ||                         \
@@ -78,7 +88,7 @@ BINOP(xor, ^)
     const void *xv;                                                    \
     const unsigned char *xp;                                           \
     unsigned char *zp;                                                 \
-    int xsz;                                                           \
+    Py_ssize_t xsz;                                                    \
     int i;                                                             \
     PyObject *rc = 0;                                                  \
     if (PyObject_AsReadBuffer(x, &xv, &xsz)) goto end;                 \
@@ -119,8 +129,8 @@ static PyNumberMethods bytestring_pynumber = {
 static PyBufferProcs bytestring_pybuffer;
 
 static PyTypeObject bytestring_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
-  "catacomb.ByteString",               /* @tp_name@ */
+  PyObject_HEAD_INIT(0) 0,             /* Header */
+  "ByteString",                                /* @tp_name@ */
   0,                                   /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -151,7 +161,7 @@ static PyTypeObject bytestring_pytype_skel = {
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   0,                                   /* @tp_getset@ */
@@ -162,8 +172,8 @@ static PyTypeObject bytestring_pytype_skel = {
   0,                                   /* @tp_dictoffset@ */
   0,                                   /* @tp_init@ */
   PyType_GenericAlloc,                 /* @tp_alloc@ */
-  0,                                   /* @tp_new@ */
-  _PyObject_Del,                       /* @tp_free@ */
+  bytestring_pynew,                    /* @tp_new@ */
+  0,                                   /* @tp_free@ */
   0                                    /* @tp_is_gc@ */
 };