/* -*-c-*-
- *
- * $Id$
*
* Definitions for Catacomb bindings
*
* (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.
#include <mLib/dstr.h>
#include <mLib/macros.h>
#include <mLib/quis.h>
+#include <mLib/unihash.h>
#include <catacomb/buf.h>
#include <catacomb/mpmont.h>
#include <catacomb/mpbarrett.h>
#include <catacomb/mpreduce.h>
+#include <catacomb/mp-fibonacci.h>
#include <catacomb/pgen.h>
#include <catacomb/pfilt.h>
#define VALERR(str) EXCERR(PyExc_ValueError, str)
#define TYERR(str) EXCERR(PyExc_TypeError, str)
#define ZDIVERR(str) EXCERR(PyExc_ZeroDivisionError, str)
-#define SYNERR(str) EXCERR(PyExc_SyntaxError, str)
#define SYSERR(str) EXCERR(PyExc_SystemError, str)
+#define NIERR(str) EXCERR(PyExc_NotImplementedError, str)
#define INDEXERR(idx) do { \
PyErr_SetObject(PyExc_KeyError, idx); \
goto end; \
return (0); \
}
+#if PY_VERSION_HEX < 0x02050000 /* Compatibility hack */
+# define ht_name name
+# define ht_type type
+#endif
+
#define root_pytype 0
#define type_pytype &PyType_Type
#define INITTYPE(ty, base) do { \
#define INSERT(name, ob) do { \
PyObject *_o = (PyObject *)(ob); \
- Py_INCREF(_o); \
+ Py_INCREF(_o); \
PyModule_AddObject(mod, name, _o); \
} while (0)
{ #name, ty, offsetof(MEMBERSTRUCT, name), f, doc },
#define MODULES(_) \
+ _(util) \
_(bytestring) _(buffer) \
_(rand) _(algorithms) _(pubkey) _(pgen) \
_(mp) _(field) _(ec) _(group) \
#define FREEOBJ(obj) \
(((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
+#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((/*unconst*/ gc##base *)g##base##tab[i]); \
+ PyDict_SetItemString(d, \
+ (/*unconst*/ char *)g##base##tab[i]->name, \
+ o); \
+ Py_DECREF(o); \
+ } \
+ return (d); \
+ }
+
+struct nameval { const char *name; unsigned long value; };
+extern void setconstants(PyObject *, const struct nameval *);
+
+extern PyObject *mexp_common(PyObject *, PyObject *, size_t,
+ PyObject *(*id)(PyObject *),
+ int (*fill)(void *, PyObject *,
+ PyObject *, PyObject *),
+ PyObject *(*exp)(PyObject *, void *, int),
+ void (*drop)(void *));
+
+extern int convulong(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 *getulong(unsigned long);
+extern void *newtype(PyTypeObject *, const PyTypeObject *, const char *);
+
+extern PyObject *mkexc(PyObject *, PyObject *, const char *, PyMethodDef *);
+extern void typeready(PyTypeObject *);
+extern PyTypeObject *inittype(PyTypeObject *);
+extern void addmethods(const PyMethodDef *);
+extern PyMethodDef *donemethods(void);
+
+/*----- Mapping methods ---------------------------------------------------*/
+
+#define GMAP_METH(func, doc) { #func, gmapmeth_##func, METH_VARARGS, doc },
+#define GMAP_KWMETH(func, doc) \
+ { #func, (PyCFunction)gmapmeth_##func, METH_VARARGS|METH_KEYWORDS, doc },
+#define GMAP_METHDECL(func, doc) \
+ extern PyObject *gmapmeth_##func(PyObject *, PyObject *);
+#define GMAP_KWMETHDECL(func, doc) \
+ extern PyObject *gmapmeth_##func(PyObject *, PyObject *, PyObject *);
+
+#define GMAP_DOROMETHODS(METH, KWMETH) \
+ METH (has_key, "D.has_key(KEY) -> BOOL") \
+ METH (keys, "D.keys() -> LIST") \
+ METH (values, "D.values() -> LIST") \
+ METH (items, "D.items() -> LIST") \
+ METH (iterkeys, "D.iterkeys() -> ITER") \
+ METH (itervalues, "D.itervalues() -> ITER") \
+ METH (iteritems, "D.iteritems() -> ITER") \
+ KWMETH(get, "D.get(KEY, [default = None]) -> VALUE") \
+
+#define GMAP_DOMETHODS(METH, KWMETH) \
+ GMAP_DOROMETHODS(METH, KWMETH) \
+ METH (clear, "D.clear()") \
+ KWMETH(setdefault, "D.setdefault(K, [default = None]) -> VALUE") \
+ KWMETH(pop, "D.pop(KEY, [default = <error>]) -> VALUE") \
+ METH (popitem, "D.popitem() -> (KEY, VALUE)") \
+ METH (update, "D.update(MAP)")
+
+GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL)
+#define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH)
+#define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH)
+extern Py_ssize_t gmap_pysize(PyObject *);
+extern PySequenceMethods gmap_pysequence;
+extern PyMethodDef gmap_pymethods[];
+
/*----- Bytestrings -------------------------------------------------------*/
PyTypeObject *bytestring_pyobj;
#define MP_PYCHECK(o) PyObject_TypeCheck((o), mp_pytype)
#define GF_PYCHECK(o) PyObject_TypeCheck((o), gf_pytype)
-extern mp *mp_frompylong(PyLongObject *);
-extern PyLongObject *mp_topylong(mp *);
+extern mp *mp_frompylong(PyObject *);
+extern PyObject *mp_topylong(mp *);
extern mp *tomp(PyObject *);
extern mp *getmp(PyObject *);
extern int convmp(PyObject *, void *);
extern mp *mp_frompyobject(PyObject *, int);
extern PyObject *mp_topystring(mp *, int,
const char *, const char *, const char *);
-extern int mp_tolong_checked(mp *, long *);
+extern int mp_tolong_checked(mp *, long *, int);
/*----- Abstract fields ---------------------------------------------------*/
field *f;
mp *x;
} fe_pyobj;
-
+
extern PyTypeObject *field_pytype;
extern PyTypeObject *primefield_pytype;
extern PyTypeObject *niceprimefield_pytype;
ec_info ei;
PyObject *cobj;
} ecinfo_pyobj;
-
+
extern PyTypeObject *ecinfo_pytype;
#define ECINFO_PYCHECK(o) PyObject_TypeCheck((o), ecinfo_pytype)
#define ECINFO_EI(o) (&((ecinfo_pyobj *)(o))->ei)
extern int convgmac(PyObject *, void *);
/*----- Key generation ----------------------------------------------------*/
-
+
typedef struct pfilt_pyobj {
PyObject_HEAD
pfilt f;
extern void droppgev(pgev *);
extern void pgenerr(void);
-/*----- Core utility functions --------------------------------------------*/
-
-extern PyObject *mexp_common(PyObject *, PyObject *, size_t,
- PyObject *(*id)(PyObject *),
- int (*fill)(void *, PyObject *,
- PyObject *, PyObject *),
- PyObject *(*exp)(PyObject *, void *, int),
- void (*drop)(void *));
-
-extern int convulong(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);
-#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 *);
-
/*----- That's all, folks -------------------------------------------------*/
#ifdef __cplusplus