pyke/mapping.c, key.c: Make the mapping code more intrusive and complete.
[catacomb-python] / catacomb.c
index 65d3985..60dae45 100644 (file)
@@ -1,13 +1,11 @@
 /* -*-c-*-
  *
- * $Id$
- *
  * Where the fun begins
  *
  * (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.
 
 /*----- Main code ---------------------------------------------------------*/
 
-static const struct nameval consts[] = {
-#define C(x) { #x, 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(PMODE_READ), C(PMODE_VERIFY),
-  C(KOPEN_READ), C(KOPEN_WRITE), C(KOPEN_NOFILE),
-  C(KEXP_FOREVER), C(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),
-#define ENTRY(tag, val, str) C(KERR_##tag),
-  KEY_ERRORS(ENTRY)
-#undef ENTRY
-#undef C
-  { 0 }
-};
-
 PyObject *mexp_common(PyObject *me, PyObject *arg,
                      size_t efsz,
                      PyObject *(*id)(PyObject *),
@@ -65,11 +42,12 @@ 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) { z = id(me); goto end; }
+  n = PySequence_Size(arg); if (n < 0) goto end;
+  if (!n) { z = id(me); goto end; }
   x = PySequence_GetItem(arg, 0);
   if (PySequence_Check(x))
     flat = 0;
@@ -119,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);
 }
 
@@ -134,27 +125,44 @@ static PyObject *meth__ego(PyObject *me, PyObject *arg)
   char *argv0;
   if (!PyArg_ParseTuple(arg, "s:_ego", &argv0))
     return (0);
-  if (strcmp(QUIS, "<UNNAMED>") == 0)
+  if (STRCMP(QUIS, ==, "<UNNAMED>"))
     ego(argv0);
   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 }
 };
 
-void init_base(void)
+static void init_random(void)
+{
+#if PY_VERSION_HEX >= 0x02060000
+  char *seed;
+  uint32 r;
+
+  if (!Py_HashRandomizationFlag) return;
+  seed = getenv("PYTHONHASHSEED");
+  if (!seed || STRCMP(seed, ==, "random")) r = GR_WORD(&rand_global);
+  else r = strtoul(seed, 0, 0);
+  if (!r) r = 0xe011f220; /* zero doesn't work well */
+  unihash_setkey(&unihash_global, r);
+#endif
+}
+
+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 -------------------------------------------------*/