Set ELF visibility to keep all of our external symbols to ourselves.
[pyke] / catacomb.c
index 0e1c711..affb5d3 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 void setconstants(PyObject *mod)
-{
-  static const struct { const char *name; unsigned long value; } consts[] = {
+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(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),
+  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
-    { 0 }
-  };
-  int i;
-  PyObject *x;
-
-  for (i = 0; consts[i].name; i++) {
-    if (consts[i].value > LONG_MAX)
-      x = PyLong_FromUnsignedLong(consts[i].value);
-    else
-      x = PyInt_FromLong(consts[i].value);
-    PyModule_AddObject(mod, (/*unconst*/ char *)consts[i].name, x);
-  }
-}
-
-PyObject *getu32(uint32 w)
-{
-  if (w <= 0x7fffffff)
-    return (PyInt_FromLong(w));
-  else
-    return (PyLong_FromUnsignedLong(w));
-}
-
-PyObject *getbool(int b)
-{
-  if (b) RETURN_TRUE;
-  else RETURN_FALSE;
-}
-
-PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
-{
-  PyErr_SetString(PyExc_TypeError, "can't instantiate this class");
-  return (0);
-}
-
-int convulong(PyObject *o, void *pp)
-{
-  long i;
-  unsigned long *p = pp;
-  PyObject *t;
-
-  if (PyInt_Check(o)) {
-    i = PyInt_AS_LONG(o);
-    if (i < 0) TYERR("must be nonnegative");
-    *p = i;
-  } else {
-    if ((t = PyNumber_Long(o)) == 0) goto end;
-    *p = PyLong_AsUnsignedLong(t);
-    Py_DECREF(t);
-    if (PyErr_Occurred()) goto end;
-  }
-  return (1);
-end:
-  return (0);
-}
-
-int convu32(PyObject *o, void *pp)
-{
-  unsigned long u;
-  uint32 *p = pp;
-
-  if (!convulong(o, &u)) goto end;
-  if (u > 0xffffffff) TYERR("out of range");
-  *p = u;
-  return (1);
-end:
-  return (0);
-}
-
-int convuint(PyObject *o, void *pp)
-{
-  unsigned long u;
-  unsigned *p = pp;
-
-  if (!convulong(o, &u)) goto end;
-  if (u > UINT_MAX) TYERR("out of range");
-  *p = u;
-  return (1);
-end:
-  return (0);
-}
-
-int convmpw(PyObject *o, void *pp)
-{
-  unsigned long u;
-  unsigned *p = pp;
-
-  if (!convulong(o, &u)) goto end;
-  if (u > MPW_MAX) TYERR("out of range");
-  *p = u;
-  return (1);
-end:
-  return (0);
-}
-
-int convszt(PyObject *o, void *pp)
-{
-  unsigned long u;
-  size_t *p = pp;
-
-  if (!convulong(o, &u)) goto end;
-  if (u > ~(size_t)0) TYERR("out of range");
-  *p = u;
-  return (1);
-end:
-  return (0);
-}
-
-int convbool(PyObject *o, void *pp)
-{
-  *(int *)pp = PyObject_IsTrue(o);
-  return (1);
-}
+  { 0 }
+};
 
 PyObject *mexp_common(PyObject *me, PyObject *arg,
                      size_t efsz,
@@ -221,54 +125,6 @@ end:
   return (z);
 }
 
-DA_DECL(method_v, PyMethodDef);
-static method_v global_pymethods = DA_INIT;
-void addmethods(const PyMethodDef *m)
-{
-  size_t n;
-
-  for (n = 0; m[n].ml_name; n++);
-  DA_ENSURE(&global_pymethods, n);
-  memcpy(DA(&global_pymethods) + DA_LEN(&global_pymethods),
-        m, n * sizeof(*m));
-  DA_EXTEND(&global_pymethods, n);
-}
-
-static const PyTypeObject emptytype = { 0 };
-
-void *newtype(PyTypeObject *metaty,
-             const PyTypeObject *skel,
-             const char *name)
-{
-  PyHeapTypeObject *ty =
-    (PyHeapTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0));
-  if (!skel) skel = &emptytype;
-  memcpy(ty, skel, sizeof(*skel));
-  if (ty->type.tp_base) Py_INCREF(ty->type.tp_base);
-#define COPY(blah) do {                                                        \
-    if (ty->type.tp_as_##blah) {                                       \
-      memcpy(&ty->as_##blah,                                           \
-            ty->type.tp_as_##blah,                                     \
-            sizeof(ty->as_##blah));                                    \
-      ty->type.tp_as_##blah = &ty->as_##blah;                          \
-    }                                                                  \
-  } while (0)
-  COPY(number);
-  COPY(sequence);
-  COPY(mapping);
-  COPY(buffer);
-#undef COPY
-  if (name)
-    ty->name = PyString_FromString(name);
-  else if (ty->type.tp_name)
-    ty->name = PyString_FromString(ty->type.tp_name);
-  if (ty->name)
-    ty->type.tp_name = PyString_AS_STRING(ty->name);
-  PyObject_INIT(&ty->type, metaty);
-  Py_INCREF(metaty);
-  return (ty);
-}
-
 static PyObject *smallprimes(void)
 {
   PyObject *v = PyList_New(NPRIME);
@@ -279,23 +135,48 @@ static PyObject *smallprimes(void)
   return (v);
 }
 
-PyTypeObject *inittype(PyTypeObject *tyskel)
+static PyObject *meth__ego(PyObject *me, PyObject *arg)
 {
-  PyTypeObject *ty = newtype(&PyType_Type, tyskel, 0);
-  ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
-  PyType_Ready(ty);
-  return (ty);
+  char *argv0;
+  if (!PyArg_ParseTuple(arg, "s:_ego", &argv0))
+    return (0);
+  if (strcmp(QUIS, "<UNNAMED>") == 0)
+    ego(argv0);
+  RETURN_NONE;
+}
+
+static PyMethodDef methods[] = {
+#define METHNAME(func) meth_##func
+  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)
+  char *seed;
+  uint32 r;
+
+  if (!Py_HashRandomizationFlag) return;
+  seed = getenv("PYTHONHASHSEED");
+  if (!seed || strcmp(seed, "random") == 0) 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
 }
 
-void init_base(void) {
-  static const PyMethodDef mzero = { 0 };
+EXPORT void init_base(void)
+{
   PyObject *mod;
+  addmethods(methods);
   INIT_MODULES;
-  DA_PUSH(&global_pymethods, mzero);
-  mod = Py_InitModule("catacomb._base", DA(&global_pymethods));
+  init_random();
+  mod = Py_InitModule("catacomb._base", donemethods());
   INSERT_MODULES;
   INSERT("smallprimes", smallprimes());
-  setconstants(mod);
+  setconstants(mod, consts);
 }
 
 /*----- That's all, folks -------------------------------------------------*/