X-Git-Url: https://git.distorted.org.uk/~mdw/pyke/blobdiff_plain/62058916b6c4b39764824d24be670525b7cdfd7a..68ec53f32a22b0c38e3b7b53e03edf747c49e416:/catacomb.c diff --git a/catacomb.c b/catacomb.c index 47a94dd..65d3985 100644 --- a/catacomb.c +++ b/catacomb.c @@ -32,142 +32,26 @@ /*----- 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(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(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) + 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); - } -} - -#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) -{ - 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); -} - -#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) -{ - 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, @@ -235,96 +119,6 @@ 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) -{ - 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); @@ -335,14 +129,6 @@ static PyObject *smallprimes(void) return (v); } -PyTypeObject *inittype(PyTypeObject *tyskel) -{ - PyTypeObject *ty = newtype(&PyType_Type, tyskel, 0); - ty->tp_flags |= Py_TPFLAGS_HEAPTYPE; - PyType_Ready(ty); - return (ty); -} - static PyObject *meth__ego(PyObject *me, PyObject *arg) { char *argv0; @@ -360,16 +146,15 @@ static PyMethodDef methods[] = { { 0 } }; -void init_base(void) { - static const PyMethodDef mzero = { 0 }; +void init_base(void) +{ PyObject *mod; addmethods(methods); INIT_MODULES; - DA_PUSH(&global_pymethods, mzero); - mod = Py_InitModule("catacomb._base", DA(&global_pymethods)); + mod = Py_InitModule("catacomb._base", donemethods()); INSERT_MODULES; INSERT("smallprimes", smallprimes()); - setconstants(mod); + setconstants(mod, consts); } /*----- That's all, folks -------------------------------------------------*/