Commit | Line | Data |
---|---|---|
d7ab1bab | 1 | /* -*-c-*- |
2 | * | |
3 | * $Id$ | |
4 | * | |
5 | * Where the fun begins | |
6 | * | |
7 | * (c) 2004 Straylight/Edgeware | |
8 | */ | |
9 | ||
10 | /*----- Licensing notice --------------------------------------------------* | |
11 | * | |
12 | * This file is part of the Python interface to Catacomb. | |
13 | * | |
14 | * Catacomb/Python is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation; either version 2 of the License, or | |
17 | * (at your option) any later version. | |
18 | * | |
19 | * Catacomb/Python is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with Catacomb/Python; if not, write to the Free Software Foundation, | |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
27 | */ | |
28 | ||
29 | /*----- Header files ------------------------------------------------------*/ | |
30 | ||
31 | #include "catacomb-python.h" | |
32 | ||
33 | /*----- Main code ---------------------------------------------------------*/ | |
34 | ||
11cb3d97 | 35 | static const struct nameval consts[] = { |
d7ab1bab | 36 | #define C(x) { #x, x } |
11cb3d97 MW |
37 | C(FTY_PRIME), C(FTY_BINARY), |
38 | C(PGEN_PASS), C(PGEN_FAIL), C(PGEN_BEGIN), C(PGEN_TRY), C(PGEN_DONE), | |
39 | C(PGEN_ABORT), | |
40 | C(MPW_MAX), | |
41 | C(PMODE_READ), C(PMODE_VERIFY), | |
42 | C(KOPEN_READ), C(KOPEN_WRITE), C(KOPEN_NOFILE), | |
43 | C(KEXP_FOREVER), C(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), | |
46e6ad89 | 49 | #define ENTRY(tag, val, str) C(KERR_##tag), |
11cb3d97 | 50 | KEY_ERRORS(ENTRY) |
46e6ad89 | 51 | #undef ENTRY |
d7ab1bab | 52 | #undef C |
11cb3d97 MW |
53 | { 0 } |
54 | }; | |
d7ab1bab | 55 | |
56 | PyObject *mexp_common(PyObject *me, PyObject *arg, | |
57 | size_t efsz, | |
58 | PyObject *(*id)(PyObject *), | |
59 | int (*fill)(void *, PyObject *, | |
60 | PyObject *, PyObject *), | |
61 | PyObject *(*exp)(PyObject *, void *, int), | |
62 | void (*drop)(void *)) | |
63 | { | |
64 | int i = 0, j, n, flat; | |
65 | PyObject *qq, *x, *y, *z = 0; | |
66 | char *v = 0, *vv; | |
67 | ||
68 | if (PyTuple_Size(arg) == 1) | |
69 | arg = PyTuple_GetItem(arg, 0); | |
70 | Py_INCREF(arg); | |
71 | if (!PySequence_Check(arg)) TYERR("not a sequence"); | |
72 | n = PySequence_Size(arg); if (!n) { z = id(me); goto end; } | |
73 | x = PySequence_GetItem(arg, 0); | |
74 | if (PySequence_Check(x)) | |
75 | flat = 0; | |
76 | else { | |
77 | if (n % 2) VALERR("must have even number of arguments"); | |
78 | n /= 2; | |
79 | flat = 1; | |
80 | } | |
81 | Py_DECREF(x); | |
82 | ||
83 | v = xmalloc(n * efsz); | |
84 | for (i = j = 0, vv = v; i < n; i++, vv += efsz) { | |
85 | if (flat) { | |
86 | x = PySequence_GetItem(arg, j++); | |
87 | y = PySequence_GetItem(arg, j++); | |
88 | } else { | |
89 | qq = PySequence_GetItem(arg, j++); | |
90 | if (!qq) goto end; | |
91 | if (!PySequence_Check(qq) || PySequence_Size(qq) != 2) { | |
92 | Py_DECREF(qq); | |
93 | TYERR("want a sequence of pairs"); | |
94 | } | |
95 | x = PySequence_GetItem(qq, 0); | |
96 | y = PySequence_GetItem(qq, 1); | |
97 | Py_DECREF(qq); | |
98 | } | |
99 | if (!x || !y) goto end; | |
100 | if (fill(vv, me, x, y)) { | |
101 | Py_DECREF(x); | |
102 | Py_DECREF(y); | |
103 | if (!PyErr_Occurred()) | |
104 | PyErr_SetString(PyExc_TypeError, "type mismatch"); | |
105 | goto end; | |
106 | } | |
107 | Py_DECREF(x); | |
108 | Py_DECREF(y); | |
109 | } | |
110 | z = exp(me, v, n); | |
111 | ||
112 | end: | |
113 | if (v) { | |
114 | for (j = 0, vv = v; j < i; j++, vv += efsz) | |
115 | drop(vv); | |
116 | xfree(v); | |
117 | } | |
118 | Py_DECREF(arg); | |
119 | return (z); | |
120 | } | |
121 | ||
d7ab1bab | 122 | static PyObject *smallprimes(void) |
123 | { | |
124 | PyObject *v = PyList_New(NPRIME); | |
125 | int i; | |
126 | ||
127 | for (i = 0; i < NPRIME; i++) | |
128 | PyList_SetItem(v, i, PyInt_FromLong(primetab[i])); | |
129 | return (v); | |
130 | } | |
131 | ||
46e6ad89 | 132 | static PyObject *meth__ego(PyObject *me, PyObject *arg) |
133 | { | |
134 | char *argv0; | |
135 | if (!PyArg_ParseTuple(arg, "s:_ego", &argv0)) | |
136 | return (0); | |
137 | if (strcmp(QUIS, "<UNNAMED>") == 0) | |
138 | ego(argv0); | |
139 | RETURN_NONE; | |
140 | } | |
141 | ||
142 | static PyMethodDef methods[] = { | |
143 | #define METHNAME(func) meth_##func | |
144 | METH (_ego, "_ego(ARGV0)") | |
145 | #undef METHNAME | |
146 | { 0 } | |
147 | }; | |
148 | ||
11cb3d97 MW |
149 | void init_base(void) |
150 | { | |
d7ab1bab | 151 | PyObject *mod; |
46e6ad89 | 152 | addmethods(methods); |
d7ab1bab | 153 | INIT_MODULES; |
11cb3d97 | 154 | mod = Py_InitModule("catacomb._base", donemethods()); |
d7ab1bab | 155 | INSERT_MODULES; |
156 | INSERT("smallprimes", smallprimes()); | |
11cb3d97 | 157 | setconstants(mod, consts); |
d7ab1bab | 158 | } |
159 | ||
160 | /*----- That's all, folks -------------------------------------------------*/ |