pyke/mapping.c, key.c: Make the mapping code more intrusive and complete.
[catacomb-python] / catacomb.c
index e868f0f..60dae45 100644 (file)
 
 /*----- Main code ---------------------------------------------------------*/
 
-static const struct nameval consts[] = {
-#define CF(f, x) { #x, f, x }
-#define C(x) { #x, (x) >= 0 ? 0 : CF_SIGNED, x }
-  C(FTY_PRIME), C(FTY_BINARY),
-  C(PGEN_PASS), C(PGEN_FAIL), C(PGEN_BEGIN), C(PGEN_TRY), C(PGEN_DONE),
-  C(PGEN_ABORT),
-  C(MPW_MAX),
-  C(RAND_IBITS),
-  C(PMODE_READ), C(PMODE_VERIFY),
-  C(KOPEN_READ), C(KOPEN_WRITE), C(KOPEN_NOFILE),
-  CF(0, KEXP_FOREVER), CF(0, KEXP_EXPIRE),
-  C(KF_ENCMASK), C(KENC_BINARY), C(KENC_MP), C(KENC_STRUCT),
-    C(KENC_ENCRYPT), C(KENC_STRING), C(KENC_EC),
-  C(KF_CATMASK), C(KCAT_SYMM), C(KCAT_PRIV), C(KCAT_PUB), C(KCAT_SHARE),
-  C(KF_NONSECRET),
-  C(KF_BURN), C(KF_OPT),
-  C(EC_XONLY), C(EC_YBIT), C(EC_LSB), C(EC_CMPR), C(EC_EXPLY), C(EC_SORT),
-  C(X25519_KEYSZ), C(X25519_PUBSZ), C(X25519_OUTSZ),
-  C(X448_KEYSZ), C(X448_PUBSZ), C(X448_OUTSZ),
-  C(ED25519_KEYSZ), C(ED25519_PUBSZ), C(ED25519_SIGSZ),
-    C(ED25519_MAXPERSOSZ),
-  C(ED448_KEYSZ), C(ED448_PUBSZ), C(ED448_SIGSZ), C(ED448_MAXPERSOSZ),
-  C(AEADF_PCHSZ), C(AEADF_PCMSZ), C(AEADF_PCTSZ),
-  C(AEADF_AADNDEP), C(AEADF_AADFIRST), C(AEADF_NOAAD),
-#define ENTRY(tag, val, str) C(KERR_##tag),
-  KEY_ERRORS(ENTRY)
-#undef ENTRY
-#undef C
-#undef CF
-  { 0 }
-};
-
 PyObject *mexp_common(PyObject *me, PyObject *arg,
                      size_t efsz,
                      PyObject *(*id)(PyObject *),
@@ -74,8 +42,8 @@ PyObject *mexp_common(PyObject *me, PyObject *arg,
   PyObject *qq, *x, *y, *z = 0;
   char *v = 0, *vv;
 
-  if (PyTuple_Size(arg) == 1)
-    arg = PyTuple_GetItem(arg, 0);
+  if (PyTuple_GET_SIZE(arg) == 1)
+    arg = PyTuple_GET_ITEM(arg, 0);
   Py_INCREF(arg);
   if (!PySequence_Check(arg)) TYERR("not a sequence");
   n = PySequence_Size(arg); if (n < 0) goto end;
@@ -129,13 +97,26 @@ end:
   return (z);
 }
 
+int convmpw(PyObject *o, void *pp)
+{
+  unsigned long u;
+  unsigned *p = pp;
+
+  if (!convulong(o, &u)) goto end;
+  if (u > MPW_MAX) VALERR("out of range");
+  *p = u;
+  return (1);
+end:
+  return (0);
+}
+
 static PyObject *smallprimes(void)
 {
   PyObject *v = PyList_New(NPRIME);
   int i;
 
   for (i = 0; i < NPRIME; i++)
-    PyList_SetItem(v, i, PyInt_FromLong(primetab[i]));
+    PyList_SET_ITEM(v, i, PyInt_FromLong(primetab[i]));
   return (v);
 }
 
@@ -149,16 +130,16 @@ static PyObject *meth__ego(PyObject *me, PyObject *arg)
   RETURN_NONE;
 }
 
-static PyMethodDef methods[] = {
+static const PyMethodDef methods[] = {
 #define METHNAME(func) meth_##func
-  METH (_ego,                  "_ego(ARGV0)")
+  METH (_ego,          "_ego(ARGV0)")
 #undef METHNAME
   { 0 }
 };
 
 static void init_random(void)
 {
-#if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6)
+#if PY_VERSION_HEX >= 0x02060000
   char *seed;
   uint32 r;
 
@@ -174,13 +155,14 @@ static void init_random(void)
 EXPORT void init_base(void)
 {
   PyObject *mod;
+
+  modname = PyString_FromString("catacomb");
   addmethods(methods);
   INIT_MODULES;
   init_random();
   mod = Py_InitModule("catacomb._base", donemethods());
   INSERT_MODULES;
   INSERT("smallprimes", smallprimes());
-  setconstants(mod, consts);
 }
 
 /*----- That's all, folks -------------------------------------------------*/