chiark / gitweb /
*.py: Use `str.replace' rather than `str.translate'.
[catacomb-python] / algorithms.c
index aeafad3c0336e6cc4dad12b93624b81dc6fb0ea3..3a618d02a49308eea449b95e6708f83cd6006962 100644 (file)
@@ -35,21 +35,31 @@ PyTypeObject *keysz_pytype;
 PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype;
 PyObject *sha_pyobj, *has160_pyobj;
 
+#ifndef KSZ_OPMASK
+#  define KSZ_OPMASK 0x1f
+#endif
+
+#ifndef KSZ_16BIT
+#  define KSZ_16BIT 0x20
+#endif
+
 PyObject *keysz_pywrap(const octet *k)
 {
-  switch (k[0]) {
+  unsigned op = *k++;
+#define ARG(i) (op&KSZ_16BIT ? LOAD16(k + 2*(i)) : k[i])
+  switch (op&KSZ_OPMASK) {
     case KSZ_ANY: {
       keysz_pyobj *o = PyObject_New(keysz_pyobj, keyszany_pytype);
-      o->dfl = k[1];
+      o->dfl = ARG(0);
       return ((PyObject *)o);
     } break;
     case KSZ_RANGE: {
       keyszrange_pyobj *o =
        PyObject_New(keyszrange_pyobj, keyszrange_pytype);
-      o->dfl = k[1];
-      o->min = k[2];
-      o->max = k[3];
-      o->mod = k[4];
+      o->dfl = ARG(0);
+      o->min = ARG(1);
+      o->max = ARG(2);
+      o->mod = ARG(3);
       if (!o->mod) o->mod = 1;
       return ((PyObject *)o);
     } break;
@@ -57,16 +67,17 @@ PyObject *keysz_pywrap(const octet *k)
       keyszset_pyobj *o =
        PyObject_New(keyszset_pyobj, keyszset_pytype);
       int i, n;
-      o->dfl = k[1];
-      for (i = 0; k[i + 1]; i++) ;
+      o->dfl = ARG(0);
+      for (i = 0; ARG(i); i++) ;
       n = i; o->set = PyTuple_New(n);
       for (i = 0; i < n; i++)
-       PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(k[i + 1]));
+       PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(ARG(i)));
       return ((PyObject *)o);
     } break;
     default:
       abort();
   }
+#undef ARG
 }
 
 static PyObject *keyszany_pynew(PyTypeObject *ty,
@@ -487,7 +498,7 @@ PyObject *gccipher_pywrap(gccipher *cc)
   g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
   g->ty.ht_type.tp_free = 0;
   g->ty.ht_type.tp_new = gcipher_pynew;
-  PyType_Ready(&g->ty.ht_type);
+  typeready(&g->ty.ht_type);
   return ((PyObject *)g);
 }
 
@@ -731,7 +742,7 @@ PyObject *gchash_pywrap(gchash *ch)
   g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
   g->ty.ht_type.tp_free = 0;
   g->ty.ht_type.tp_new = ghash_pynew;
-  PyType_Ready(&g->ty.ht_type);
+  typeready(&g->ty.ht_type);
   return ((PyObject *)g);
 }
 
@@ -986,7 +997,7 @@ PyObject *gcmac_pywrap(gcmac *cm)
   g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
   g->ty.ht_type.tp_free = 0;
   g->ty.ht_type.tp_new = gmac_pynew;
-  PyType_Ready(&g->ty.ht_type);
+  typeready(&g->ty.ht_type);
   return ((PyObject *)g);
 }
 
@@ -1006,7 +1017,7 @@ PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f)
   g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
   g->ty.ht_type.tp_free = 0;
   g->ty.ht_type.tp_new = gmhash_pynew;
-  PyType_Ready(&g->ty.ht_type);
+  typeready(&g->ty.ht_type);
   g->m = m;
   g->f = f;
   return ((PyObject *)g);
@@ -1275,7 +1286,7 @@ static PyObject *gcprp_pywrap(const prpinfo *prp)
   g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
   g->ty.ht_type.tp_free = 0;
   g->ty.ht_type.tp_new = gprp_pynew;
-  PyType_Ready(&g->ty.ht_type);
+  typeready(&g->ty.ht_type);
   return ((PyObject *)g);
 }