X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/68003d328074e78ede9e83f629a88a3639816930..54fd7594ee5df9dbc9745d98adaa01a5ed43b6e4:/catacomb-python.h diff --git a/catacomb-python.h b/catacomb-python.h index 4c7ced4..e74e048 100644 --- a/catacomb-python.h +++ b/catacomb-python.h @@ -70,6 +70,7 @@ PUBLIC_SYMBOLS; #include #include #include +#include #include #include @@ -81,6 +82,7 @@ PUBLIC_SYMBOLS; #include #include +#include #include #include #include @@ -136,31 +138,21 @@ MODULES(DECLARE_MODINIT) /* Conversions. */ extern int convmpw(PyObject *, void *); -/* Make a dictionary of generic-crypto classes. */ -#define GEN(func, base) \ - static PyObject *func(void) \ - { \ - PyObject *d = PyDict_New(); \ - PyObject *o; \ - int i; \ - \ - for (i = 0; g##base##tab[i]; i++) { \ - o = gc##base##_pywrap(CONVERT_CAREFULLY(gc##base *, const gc##base *, \ - g##base##tab[i])); \ - PyDict_SetItemString(d, \ - (/*unconst*/ char *)g##base##tab[i]->name, \ - o); \ - Py_DECREF(o); \ - } \ - return (d); \ - } +/* Building tables of things. */ +extern PyObject *make_algtab(const void *tab, size_t esz, + const char *(*namefn)(const void *), + PyObject *(*valfn)(const void *)); +extern PyObject *make_grouptab(const void *tab, size_t esz, + const char *(*namefn)(const void *), + int (*ixfn)(const void *), + PyObject *(*valfn)(int)); /* Common handling for simultaneous exponentiation. */ extern PyObject *mexp_common(PyObject *, PyObject *, size_t, PyObject *(*id)(PyObject *), int (*fill)(void *, PyObject *, PyObject *, PyObject *), - PyObject *(*exp)(PyObject *, void *, int), + PyObject *(*exp)(PyObject *, void *, size_t), void (*drop)(void *)); /*----- Bytestrings -------------------------------------------------------*/ @@ -168,6 +160,27 @@ extern PyObject *mexp_common(PyObject *, PyObject *, size_t, PyObject *bytestring_pywrap(const void *, size_t); PyObject *bytestring_pywrapbuf(buf *); +/*----- Buffers -----------------------------------------------------------*/ + +typedef struct buf_pyobj { + PyObject_HEAD + buf b; + PyObject *sub; + unsigned lk; +} buf_pyobj; + +extern PyTypeObject *rbuf_pytype, *wbuf_pytype; +#define RBUF_PYCHECK(o) PyObject_TypeCheck((o), rbuf_pytype) +#define WBUF_PYCHECK(o) PyObject_TypeCheck((o), wbuf_pytype) +#define BUF_B(o) (&((buf_pyobj *)(o))->b) +#define BUF_SUB(o) (((buf_pyobj *)(o))->sub) +#define BUF_LK(o) (((buf_pyobj *)(o))->lk) + +extern PyObject *buferr; +#define BUFERR(str) do { PyErr_SetString(buferr, str); goto end; } while (0) + +extern int ensurebuf(PyObject *, size_t); + /*----- Multiprecision arithmetic -----------------------------------------*/ typedef struct mp_pyobj { @@ -184,13 +197,15 @@ extern PyTypeObject *gf_pytype; extern mp *mp_frompylong(PyObject *); extern PyObject *mp_topylong(mp *); extern mp *tomp(PyObject *); +extern mp *implicitmp(PyObject *); extern mp *getmp(PyObject *); extern int convmp(PyObject *, void *); +extern mp *implicitgf(PyObject *); extern mp *getgf(PyObject *); extern int convgf(PyObject *, void *); extern PyObject *mp_pywrap(mp *); extern PyObject *gf_pywrap(mp *); -extern long mphash(mp *); +extern Py_hash_t mphash(mp *); extern mp *mp_frompyobject(PyObject *, int); extern PyObject *mp_topystring(mp *, int, const char *, const char *, const char *); @@ -223,7 +238,7 @@ typedef struct fe_pyobj { extern PyTypeObject *fe_pytype; #define FE_PYCHECK(o) PyObject_TypeCheck((o), fe_pytype) #define FE_F(o) (((fe_pyobj *)(o))->f) -#define FE_FOBJ(o) ((PyObject *)(o)->ob_type) +#define FE_FOBJ(o) ((PyObject *)Py_TYPE(o)) #define FE_X(o) (((fe_pyobj *)(o))->x) extern PyObject *fe_pywrap(PyObject *, mp *); @@ -256,7 +271,7 @@ extern PyTypeObject *ecpt_pytype, *ecptcurve_pytype; #define ECPT_PYCHECK(o) PyObject_TypeCheck((o), ecpt_pytype) #define ECPTCURVE_PYCHECK(o) PyObject_TypeCheck((o), ecptcurve_pytype) #define ECPT_C(o) (((ecpt_pyobj *)(o))->c) -#define ECPT_COBJ(o) ((PyObject *)(o)->ob_type) +#define ECPT_COBJ(o) ((PyObject *)Py_TYPE(o)) #define ECPT_FOBJ(o) ECCURVE_FOBJ(ECPT_COBJ((o))) #define ECPT_P(o) (&((ecpt_pyobj *)(o))->p) extern PyObject *ecpt_pywrap(PyObject *, ec *); @@ -291,7 +306,7 @@ extern PyTypeObject *ge_pytype; #define GE_PYCHECK(o) PyObject_TypeCheck((o), ge_pytype) #define GE_X(o) (((ge_pyobj *)(o))->x) #define GE_G(o) (((ge_pyobj *)(o))->g) -#define GE_GOBJ(o) ((PyObject *)(group_pyobj *)(o)->ob_type) +#define GE_GOBJ(o) (PyObject *)(Py_TYPE(o)) extern PyObject *ge_pywrap(PyObject *, ge *); typedef struct group_pyobj {