From 73a712fa61e9b04ea6c1cbfc86e05cd4db1008b4 Mon Sep 17 00:00:00 2001 From: mdw Date: Mon, 26 Sep 2005 12:58:11 +0000 Subject: [PATCH] Checkin, Debianized and more or less complete. --- catacomb-python.h | 36 ++++++++++++------ catacomb.c | 112 +++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 118 insertions(+), 30 deletions(-) diff --git a/catacomb-python.h b/catacomb-python.h index ff99a78..b9b1fd5 100644 --- a/catacomb-python.h +++ b/catacomb-python.h @@ -39,9 +39,13 @@ #include #include +#undef ULLONG_MAX +#undef ULONG_LONG_MAX + #include #include #include +#include #include @@ -104,6 +108,9 @@ #include #include +#include +#include + /*----- Utility macros ----------------------------------------------------*/ #define RETURN_OBJ(obj) do { Py_INCREF(obj); return (obj); } while (0) @@ -122,6 +129,10 @@ #define ZDIVERR(str) EXCERR(PyExc_ZeroDivisionError, str) #define SYNERR(str) EXCERR(PyExc_SyntaxError, str) #define SYSERR(str) EXCERR(PyExc_SystemError, str) +#define INDEXERR(idx) do { \ + PyErr_SetObject(PyExc_KeyError, idx); \ + goto end; \ +} while (0) #define OSERR(name) do { \ PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); \ goto end; \ @@ -152,6 +163,9 @@ PyModule_AddObject(mod, name, _o); \ } while (0) +#define INSEXC(name, var, base, meth) \ + INSERT(name, var = mkexc(mod, base, name, meth)) + #define METH(func, doc) \ { #func, METHNAME(func), METH_VARARGS, doc }, #define KWMETH(func, doc) \ @@ -166,11 +180,11 @@ #define MEMBER(name, ty, f, doc) \ { #name, ty, offsetof(MEMBERSTRUCT, name), f, doc }, -#define MODULES(DO) \ - DO(bytestring) \ - DO(rand) DO(algorithms) DO(pubkey) DO(pgen) \ - DO(mp) DO(field) DO(ec) DO(group) \ - DO(passphrase) +#define MODULES(_) \ + _(bytestring) _(buffer) \ + _(rand) _(algorithms) _(pubkey) _(pgen) \ + _(mp) _(field) _(ec) _(group) \ + _(passphrase) _(share) _(key) #define DOMODINIT(m) m##_pyinit(); #define DOMODINSERT(m) m##_pyinsert(mod); #define INIT_MODULES do { MODULES(DOMODINIT) } while (0) @@ -236,7 +250,6 @@ extern mp *getfe(field *, PyObject *); typedef struct fe_pyobj { PyObject_HEAD field *f; - PyObject *fobj; /* to keep it alive */ mp *x; } fe_pyobj; @@ -271,7 +284,7 @@ extern PyObject *ecpt_pywrapout(void *, ec *); extern int toecpt(ec_curve *, ec *, PyObject *); extern int getecpt(ec_curve *, ec *, PyObject *); extern void getecptout(ec *, PyObject *); -extern int convec(PyObject *, void *); +extern int convecpt(PyObject *, void *); typedef struct eccurve_pyobj { PyHeapTypeObject ty; @@ -460,8 +473,6 @@ extern PyTypeObject *gmac_pytype; extern PyObject *gmac_pywrap(PyObject *, gmac *, unsigned); extern int convgmac(PyObject *, void *); -/*----- Public key crypto -------------------------------------------------*/ - /*----- Key generation ----------------------------------------------------*/ typedef struct pfilt_pyobj { @@ -500,14 +511,17 @@ extern PyObject *mexp_common(PyObject *, PyObject *, size_t, void (*drop)(void *)); extern int convulong(PyObject *, void *); -extern int convu32(PyObject *, void *); +#define DECL_CONVU_(n) extern int convu##n(PyObject *, void *); +DOUINTSZ(DECL_CONVU_) extern int convmpw(PyObject *, void *); extern int convuint(PyObject *, void *); extern int convszt(PyObject *, void *); extern int convbool(PyObject *, void *); extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *); extern PyObject *getbool(int); -extern PyObject *getu32(uint32); +#define DECL_GETU_(n) extern PyObject *getu##n(uint##n); +DOUINTSZ(DECL_GETU_) +extern PyObject * mkexc(PyObject *, PyObject *, const char *, PyMethodDef *); extern void *newtype(PyTypeObject *, const PyTypeObject *, const char *); extern PyTypeObject *inittype(PyTypeObject *); extern void addmethods(const PyMethodDef *); diff --git a/catacomb.c b/catacomb.c index 0e1c711..47a94dd 100644 --- a/catacomb.c +++ b/catacomb.c @@ -41,6 +41,16 @@ static void setconstants(PyObject *mod) 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 } }; @@ -56,13 +66,15 @@ static void setconstants(PyObject *mod) } } -PyObject *getu32(uint32 w) -{ - if (w <= 0x7fffffff) - return (PyInt_FromLong(w)); - else - return (PyLong_FromUnsignedLong(w)); -} +#define GETU_(n) \ + PyObject *getu##n(uint##n w) \ + { \ + if (w <= MASK##n) \ + return (PyInt_FromLong(w)); \ + else \ + return (PyLong_FromUnsignedLong(w)); \ + } +DOUINTSZ(GETU_) PyObject *getbool(int b) { @@ -97,18 +109,20 @@ 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); -} +#define CONVU_(n) \ + int convu##n(PyObject *o, void *pp) \ + { \ + unsigned long u; \ + uint##n *p = pp; \ + \ + if (!convulong(o, &u)) goto end; \ + if (u > MASK##n) TYERR("out of range"); \ + *p = u; \ + return (1); \ + end: \ + return (0); \ + } +DOUINTSZ(CONVU_) int convuint(PyObject *o, void *pp) { @@ -221,6 +235,48 @@ end: return (z); } +PyObject * mkexc(PyObject *mod, PyObject *base, + const char *name, PyMethodDef *mm) +{ + PyObject *nameobj = 0; + PyObject *dict = 0; + PyObject *exc = 0; + PyObject *func = 0; + PyObject *meth = 0; + + if ((nameobj = PyString_FromFormat("%s.%s", + PyModule_GetName(mod), + name)) == 0 || + (dict = PyDict_New()) == 0 || + (exc = PyErr_NewException(PyString_AS_STRING(nameobj), + base, dict)) == 0) + goto fail; + + if (mm) { + while (mm->ml_name) { + if ((func = PyCFunction_NewEx(mm, 0, mod)) == 0 || + (meth = PyMethod_New(func, 0, exc)) == 0 || + PyDict_SetItemString(dict, mm->ml_name, meth)) + goto fail; + Py_DECREF(func); func = 0; + Py_DECREF(meth); meth = 0; + mm++; + } + } + +done: + Py_XDECREF(nameobj); + Py_XDECREF(dict); + return (exc); + +fail: + Py_XDECREF(exc); + Py_XDECREF(func); + Py_XDECREF(meth); + exc = 0; + goto done; +} + DA_DECL(method_v, PyMethodDef); static method_v global_pymethods = DA_INIT; void addmethods(const PyMethodDef *m) @@ -287,9 +343,27 @@ PyTypeObject *inittype(PyTypeObject *tyskel) return (ty); } +static PyObject *meth__ego(PyObject *me, PyObject *arg) +{ + char *argv0; + if (!PyArg_ParseTuple(arg, "s:_ego", &argv0)) + return (0); + if (strcmp(QUIS, "") == 0) + ego(argv0); + RETURN_NONE; +} + +static PyMethodDef methods[] = { +#define METHNAME(func) meth_##func + METH (_ego, "_ego(ARGV0)") +#undef METHNAME + { 0 } +}; + void init_base(void) { static const PyMethodDef mzero = { 0 }; PyObject *mod; + addmethods(methods); INIT_MODULES; DA_PUSH(&global_pymethods, mzero); mod = Py_InitModule("catacomb._base", DA(&global_pymethods)); -- 2.11.0