Commit | Line | Data |
---|---|---|
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 | 33 | static const struct nameval consts[] = { |
b6a86d2e | 34 | #define C(x) { #x, x } |
68ec53f3 MW |
35 | C(FTY_PRIME), C(FTY_BINARY), |
36 | C(PGEN_PASS), C(PGEN_FAIL), C(PGEN_BEGIN), C(PGEN_TRY), C(PGEN_DONE), | |
37 | C(PGEN_ABORT), | |
38 | C(MPW_MAX), | |
39 | C(PMODE_READ), C(PMODE_VERIFY), | |
40 | C(KOPEN_READ), C(KOPEN_WRITE), C(KOPEN_NOFILE), | |
41 | C(KEXP_FOREVER), C(KEXP_EXPIRE), | |
42 | C(KF_ENCMASK), C(KENC_BINARY), C(KENC_MP), C(KENC_STRUCT), | |
43 | C(KENC_ENCRYPT), C(KENC_STRING), C(KENC_EC), | |
44 | C(KF_CATMASK), C(KCAT_SYMM), C(KCAT_PRIV), C(KCAT_PUB), C(KCAT_SHARE), | |
45 | C(KF_NONSECRET), | |
46 | C(KF_BURN), C(KF_OPT), | |
73a712fa | 47 | #define ENTRY(tag, val, str) C(KERR_##tag), |
68ec53f3 | 48 | KEY_ERRORS(ENTRY) |
73a712fa | 49 | #undef ENTRY |
b6a86d2e | 50 | #undef C |
68ec53f3 MW |
51 | { 0 } |
52 | }; | |
b6a86d2e | 53 | |
54 | PyObject *mexp_common(PyObject *me, PyObject *arg, | |
55 | size_t efsz, | |
56 | PyObject *(*id)(PyObject *), | |
57 | int (*fill)(void *, PyObject *, | |
58 | PyObject *, PyObject *), | |
59 | PyObject *(*exp)(PyObject *, void *, int), | |
60 | void (*drop)(void *)) | |
61 | { | |
62 | int i = 0, j, n, flat; | |
63 | PyObject *qq, *x, *y, *z = 0; | |
64 | char *v = 0, *vv; | |
65 | ||
66 | if (PyTuple_Size(arg) == 1) | |
67 | arg = PyTuple_GetItem(arg, 0); | |
68 | Py_INCREF(arg); | |
69 | if (!PySequence_Check(arg)) TYERR("not a sequence"); | |
70 | n = PySequence_Size(arg); if (!n) { z = id(me); goto end; } | |
71 | x = PySequence_GetItem(arg, 0); | |
72 | if (PySequence_Check(x)) | |
73 | flat = 0; | |
74 | else { | |
75 | if (n % 2) VALERR("must have even number of arguments"); | |
76 | n /= 2; | |
77 | flat = 1; | |
78 | } | |
79 | Py_DECREF(x); | |
80 | ||
81 | v = xmalloc(n * efsz); | |
82 | for (i = j = 0, vv = v; i < n; i++, vv += efsz) { | |
83 | if (flat) { | |
84 | x = PySequence_GetItem(arg, j++); | |
85 | y = PySequence_GetItem(arg, j++); | |
86 | } else { | |
87 | qq = PySequence_GetItem(arg, j++); | |
88 | if (!qq) goto end; | |
89 | if (!PySequence_Check(qq) || PySequence_Size(qq) != 2) { | |
90 | Py_DECREF(qq); | |
91 | TYERR("want a sequence of pairs"); | |
92 | } | |
93 | x = PySequence_GetItem(qq, 0); | |
94 | y = PySequence_GetItem(qq, 1); | |
95 | Py_DECREF(qq); | |
96 | } | |
97 | if (!x || !y) goto end; | |
98 | if (fill(vv, me, x, y)) { | |
99 | Py_DECREF(x); | |
100 | Py_DECREF(y); | |
101 | if (!PyErr_Occurred()) | |
102 | PyErr_SetString(PyExc_TypeError, "type mismatch"); | |
103 | goto end; | |
104 | } | |
105 | Py_DECREF(x); | |
106 | Py_DECREF(y); | |
107 | } | |
108 | z = exp(me, v, n); | |
109 | ||
110 | end: | |
111 | if (v) { | |
112 | for (j = 0, vv = v; j < i; j++, vv += efsz) | |
113 | drop(vv); | |
114 | xfree(v); | |
115 | } | |
116 | Py_DECREF(arg); | |
117 | return (z); | |
118 | } | |
119 | ||
b6a86d2e | 120 | static PyObject *smallprimes(void) |
121 | { | |
122 | PyObject *v = PyList_New(NPRIME); | |
123 | int i; | |
124 | ||
125 | for (i = 0; i < NPRIME; i++) | |
126 | PyList_SetItem(v, i, PyInt_FromLong(primetab[i])); | |
127 | return (v); | |
128 | } | |
129 | ||
73a712fa | 130 | static PyObject *meth__ego(PyObject *me, PyObject *arg) |
131 | { | |
132 | char *argv0; | |
133 | if (!PyArg_ParseTuple(arg, "s:_ego", &argv0)) | |
134 | return (0); | |
135 | if (strcmp(QUIS, "<UNNAMED>") == 0) | |
136 | ego(argv0); | |
137 | RETURN_NONE; | |
138 | } | |
139 | ||
140 | static PyMethodDef methods[] = { | |
141 | #define METHNAME(func) meth_##func | |
142 | METH (_ego, "_ego(ARGV0)") | |
143 | #undef METHNAME | |
144 | { 0 } | |
145 | }; | |
146 | ||
f7dee32a MW |
147 | static void init_random(void) |
148 | { | |
149 | #if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6) | |
150 | char *seed; | |
151 | uint32 r; | |
152 | ||
153 | if (!Py_HashRandomizationFlag) return; | |
154 | seed = getenv("PYTHONHASHSEED"); | |
155 | if (!seed || strcmp(seed, "random") == 0) r = GR_WORD(&rand_global); | |
156 | else r = strtoul(seed, 0, 0); | |
157 | if (!r) r = 0xe011f220; /* zero doesn't work well */ | |
158 | unihash_setkey(&unihash_global, r); | |
159 | #endif | |
160 | } | |
161 | ||
68ec53f3 MW |
162 | void init_base(void) |
163 | { | |
b6a86d2e | 164 | PyObject *mod; |
73a712fa | 165 | addmethods(methods); |
b6a86d2e | 166 | INIT_MODULES; |
f7dee32a | 167 | init_random(); |
68ec53f3 | 168 | mod = Py_InitModule("catacomb._base", donemethods()); |
b6a86d2e | 169 | INSERT_MODULES; |
170 | INSERT("smallprimes", smallprimes()); | |
68ec53f3 | 171 | setconstants(mod, consts); |
b6a86d2e | 172 | } |
173 | ||
174 | /*----- That's all, folks -------------------------------------------------*/ |