chiark / gitweb /
*.c: Be more careful about `PySequence_Size'.
[catacomb-python] / catacomb.c
1 /* -*-c-*-
2  *
3  * Where the fun begins
4  *
5  * (c) 2004 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the Python interface to Catacomb.
11  *
12  * Catacomb/Python is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * Catacomb/Python is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Catacomb/Python; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "catacomb-python.h"
30
31 /*----- Main code ---------------------------------------------------------*/
32
33 static const struct nameval consts[] = {
34 #define CF(f, x) { #x, f, x }
35 #define C(x) { #x, (x) >= 0 ? 0 : CF_SIGNED, x }
36   C(FTY_PRIME), C(FTY_BINARY),
37   C(PGEN_PASS), C(PGEN_FAIL), C(PGEN_BEGIN), C(PGEN_TRY), C(PGEN_DONE),
38   C(PGEN_ABORT),
39   C(MPW_MAX),
40   C(RAND_IBITS),
41   C(PMODE_READ), C(PMODE_VERIFY),
42   C(KOPEN_READ), C(KOPEN_WRITE), C(KOPEN_NOFILE),
43   CF(0, KEXP_FOREVER), CF(0, KEXP_EXPIRE),
44   C(KF_ENCMASK), C(KENC_BINARY), C(KENC_MP), C(KENC_STRUCT),
45     C(KENC_ENCRYPT), C(KENC_STRING), C(KENC_EC),
46   C(KF_CATMASK), C(KCAT_SYMM), C(KCAT_PRIV), C(KCAT_PUB), C(KCAT_SHARE),
47   C(KF_NONSECRET),
48   C(KF_BURN), C(KF_OPT),
49   C(EC_XONLY), C(EC_YBIT), C(EC_LSB), C(EC_CMPR), C(EC_EXPLY), C(EC_SORT),
50   C(X25519_KEYSZ), C(X25519_PUBSZ), C(X25519_OUTSZ),
51   C(X448_KEYSZ), C(X448_PUBSZ), C(X448_OUTSZ),
52   C(ED25519_KEYSZ), C(ED25519_PUBSZ), C(ED25519_SIGSZ),
53     C(ED25519_MAXPERSOSZ),
54   C(ED448_KEYSZ), C(ED448_PUBSZ), C(ED448_SIGSZ), C(ED448_MAXPERSOSZ),
55 #define ENTRY(tag, val, str) C(KERR_##tag),
56   KEY_ERRORS(ENTRY)
57 #undef ENTRY
58 #undef C
59 #undef CF
60   { 0 }
61 };
62
63 PyObject *mexp_common(PyObject *me, PyObject *arg,
64                       size_t efsz,
65                       PyObject *(*id)(PyObject *),
66                       int (*fill)(void *, PyObject *,
67                                   PyObject *, PyObject *),
68                       PyObject *(*exp)(PyObject *, void *, int),
69                       void (*drop)(void *))
70 {
71   int i = 0, j, n, flat;
72   PyObject *qq, *x, *y, *z = 0;
73   char *v = 0, *vv;
74
75   if (PyTuple_Size(arg) == 1)
76     arg = PyTuple_GetItem(arg, 0);
77   Py_INCREF(arg);
78   if (!PySequence_Check(arg)) TYERR("not a sequence");
79   n = PySequence_Size(arg); if (n < 0) goto end;
80   if (!n) { z = id(me); goto end; }
81   x = PySequence_GetItem(arg, 0);
82   if (PySequence_Check(x))
83     flat = 0;
84   else {
85     if (n % 2) VALERR("must have even number of arguments");
86     n /= 2;
87     flat = 1;
88   }
89   Py_DECREF(x);
90
91   v = xmalloc(n * efsz);
92   for (i = j = 0, vv = v; i < n; i++, vv += efsz) {
93     if (flat) {
94       x = PySequence_GetItem(arg, j++);
95       y = PySequence_GetItem(arg, j++);
96     } else {
97       qq = PySequence_GetItem(arg, j++);
98       if (!qq) goto end;
99       if (!PySequence_Check(qq) || PySequence_Size(qq) != 2) {
100         Py_DECREF(qq);
101         TYERR("want a sequence of pairs");
102       }
103       x = PySequence_GetItem(qq, 0);
104       y = PySequence_GetItem(qq, 1);
105       Py_DECREF(qq);
106     }
107     if (!x || !y) goto end;
108     if (fill(vv, me, x, y)) {
109       Py_DECREF(x);
110       Py_DECREF(y);
111       if (!PyErr_Occurred())
112         PyErr_SetString(PyExc_TypeError, "type mismatch");
113       goto end;
114     }
115     Py_DECREF(x);
116     Py_DECREF(y);
117   }
118   z = exp(me, v, n);
119
120 end:
121   if (v) {
122     for (j = 0, vv = v; j < i; j++, vv += efsz)
123       drop(vv);
124     xfree(v);
125   }
126   Py_DECREF(arg);
127   return (z);
128 }
129
130 static PyObject *smallprimes(void)
131 {
132   PyObject *v = PyList_New(NPRIME);
133   int i;
134
135   for (i = 0; i < NPRIME; i++)
136     PyList_SetItem(v, i, PyInt_FromLong(primetab[i]));
137   return (v);
138 }
139
140 static PyObject *meth__ego(PyObject *me, PyObject *arg)
141 {
142   char *argv0;
143   if (!PyArg_ParseTuple(arg, "s:_ego", &argv0))
144     return (0);
145   if (strcmp(QUIS, "<UNNAMED>") == 0)
146     ego(argv0);
147   RETURN_NONE;
148 }
149
150 static PyMethodDef methods[] = {
151 #define METHNAME(func) meth_##func
152   METH  (_ego,                  "_ego(ARGV0)")
153 #undef METHNAME
154   { 0 }
155 };
156
157 static void init_random(void)
158 {
159 #if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6)
160   char *seed;
161   uint32 r;
162
163   if (!Py_HashRandomizationFlag) return;
164   seed = getenv("PYTHONHASHSEED");
165   if (!seed || strcmp(seed, "random") == 0) r = GR_WORD(&rand_global);
166   else r = strtoul(seed, 0, 0);
167   if (!r) r = 0xe011f220; /* zero doesn't work well */
168   unihash_setkey(&unihash_global, r);
169 #endif
170 }
171
172 void init_base(void)
173 {
174   PyObject *mod;
175   addmethods(methods);
176   INIT_MODULES;
177   init_random();
178   mod = Py_InitModule("catacomb._base", donemethods());
179   INSERT_MODULES;
180   INSERT("smallprimes", smallprimes());
181   setconstants(mod, consts);
182 }
183
184 /*----- That's all, folks -------------------------------------------------*/