util.c (mkexc): Populate dictionary before constructing exception class.
[pyke] / catacomb.c
CommitLineData
b6a86d2e 1/* -*-c-*-
2 *
b6a86d2e 3 * Where the fun begins
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
0b1eafbf 8/*----- Licensing notice --------------------------------------------------*
b6a86d2e 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.
0b1eafbf 16 *
b6a86d2e 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.
0b1eafbf 21 *
b6a86d2e 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
68ec53f3 33static const struct nameval consts[] = {
2ec562f1
MW
34#define CF(f, x) { #x, f, x }
35#define C(x) { #x, (x) >= 0 ? 0 : CF_SIGNED, x }
68ec53f3
MW
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),
862414c1 40 C(RAND_IBITS),
68ec53f3
MW
41 C(PMODE_READ), C(PMODE_VERIFY),
42 C(KOPEN_READ), C(KOPEN_WRITE), C(KOPEN_NOFILE),
2ec562f1 43 CF(0, KEXP_FOREVER), CF(0, KEXP_EXPIRE),
68ec53f3
MW
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),
d82643d8 49 C(EC_XONLY), C(EC_YBIT), C(EC_LSB), C(EC_CMPR), C(EC_EXPLY), C(EC_SORT),
d223dc6b 50 C(X25519_KEYSZ), C(X25519_PUBSZ), C(X25519_OUTSZ),
51f4de84 51 C(X448_KEYSZ), C(X448_PUBSZ), C(X448_OUTSZ),
70ed69c4 52 C(ED25519_KEYSZ), C(ED25519_PUBSZ), C(ED25519_SIGSZ),
0d7ae1be 53 C(ED25519_MAXPERSOSZ),
aa53a95f 54 C(ED448_KEYSZ), C(ED448_PUBSZ), C(ED448_SIGSZ), C(ED448_MAXPERSOSZ),
73a712fa 55#define ENTRY(tag, val, str) C(KERR_##tag),
68ec53f3 56 KEY_ERRORS(ENTRY)
73a712fa 57#undef ENTRY
b6a86d2e 58#undef C
2ec562f1 59#undef CF
68ec53f3
MW
60 { 0 }
61};
b6a86d2e 62
63PyObject *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) { z = id(me); goto end; }
80 x = PySequence_GetItem(arg, 0);
81 if (PySequence_Check(x))
82 flat = 0;
83 else {
84 if (n % 2) VALERR("must have even number of arguments");
85 n /= 2;
86 flat = 1;
87 }
88 Py_DECREF(x);
89
90 v = xmalloc(n * efsz);
91 for (i = j = 0, vv = v; i < n; i++, vv += efsz) {
92 if (flat) {
93 x = PySequence_GetItem(arg, j++);
94 y = PySequence_GetItem(arg, j++);
95 } else {
96 qq = PySequence_GetItem(arg, j++);
97 if (!qq) goto end;
98 if (!PySequence_Check(qq) || PySequence_Size(qq) != 2) {
99 Py_DECREF(qq);
100 TYERR("want a sequence of pairs");
101 }
102 x = PySequence_GetItem(qq, 0);
103 y = PySequence_GetItem(qq, 1);
104 Py_DECREF(qq);
105 }
106 if (!x || !y) goto end;
107 if (fill(vv, me, x, y)) {
108 Py_DECREF(x);
109 Py_DECREF(y);
110 if (!PyErr_Occurred())
111 PyErr_SetString(PyExc_TypeError, "type mismatch");
112 goto end;
113 }
114 Py_DECREF(x);
115 Py_DECREF(y);
116 }
117 z = exp(me, v, n);
118
119end:
120 if (v) {
121 for (j = 0, vv = v; j < i; j++, vv += efsz)
122 drop(vv);
123 xfree(v);
124 }
125 Py_DECREF(arg);
126 return (z);
127}
128
b6a86d2e 129static PyObject *smallprimes(void)
130{
131 PyObject *v = PyList_New(NPRIME);
132 int i;
133
134 for (i = 0; i < NPRIME; i++)
135 PyList_SetItem(v, i, PyInt_FromLong(primetab[i]));
136 return (v);
137}
138
73a712fa 139static PyObject *meth__ego(PyObject *me, PyObject *arg)
140{
141 char *argv0;
142 if (!PyArg_ParseTuple(arg, "s:_ego", &argv0))
143 return (0);
144 if (strcmp(QUIS, "<UNNAMED>") == 0)
145 ego(argv0);
146 RETURN_NONE;
147}
148
149static PyMethodDef methods[] = {
150#define METHNAME(func) meth_##func
151 METH (_ego, "_ego(ARGV0)")
152#undef METHNAME
153 { 0 }
154};
155
f7dee32a
MW
156static void init_random(void)
157{
158#if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6)
159 char *seed;
160 uint32 r;
161
162 if (!Py_HashRandomizationFlag) return;
163 seed = getenv("PYTHONHASHSEED");
164 if (!seed || strcmp(seed, "random") == 0) r = GR_WORD(&rand_global);
165 else r = strtoul(seed, 0, 0);
166 if (!r) r = 0xe011f220; /* zero doesn't work well */
167 unihash_setkey(&unihash_global, r);
168#endif
169}
170
68ec53f3
MW
171void init_base(void)
172{
b6a86d2e 173 PyObject *mod;
73a712fa 174 addmethods(methods);
b6a86d2e 175 INIT_MODULES;
f7dee32a 176 init_random();
68ec53f3 177 mod = Py_InitModule("catacomb._base", donemethods());
b6a86d2e 178 INSERT_MODULES;
179 INSERT("smallprimes", smallprimes());
68ec53f3 180 setconstants(mod, consts);
b6a86d2e 181}
182
183/*----- That's all, folks -------------------------------------------------*/