5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- Main code ---------------------------------------------------------*/
33 static const struct nameval consts
[] = {
34 #define C(x) { #x, x }
35 C(FTY_PRIME
), C(FTY_BINARY
),
36 C(PGEN_PASS
), C(PGEN_FAIL
), C(PGEN_BEGIN
), C(PGEN_TRY
), C(PGEN_DONE
),
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
),
46 C(KF_BURN
), C(KF_OPT
),
47 C(EC_XONLY
), C(EC_YBIT
), C(EC_LSB
), C(EC_CMPR
), C(EC_EXPLY
), C(EC_SORT
),
48 #define ENTRY(tag, val, str) C(KERR_##tag),
55 PyObject
*mexp_common(PyObject
*me
, PyObject
*arg
,
57 PyObject
*(*id
)(PyObject
*),
58 int (*fill
)(void *, PyObject
*,
59 PyObject
*, PyObject
*),
60 PyObject
*(*exp
)(PyObject
*, void *, int),
63 int i
= 0, j
, n
, flat
;
64 PyObject
*qq
, *x
, *y
, *z
= 0;
67 if (PyTuple_Size(arg
) == 1)
68 arg
= PyTuple_GetItem(arg
, 0);
70 if (!PySequence_Check(arg
)) TYERR("not a sequence");
71 n
= PySequence_Size(arg
); if (!n
) { z
= id(me
); goto end
; }
72 x
= PySequence_GetItem(arg
, 0);
73 if (PySequence_Check(x
))
76 if (n
% 2) VALERR("must have even number of arguments");
82 v
= xmalloc(n
* efsz
);
83 for (i
= j
= 0, vv
= v
; i
< n
; i
++, vv
+= efsz
) {
85 x
= PySequence_GetItem(arg
, j
++);
86 y
= PySequence_GetItem(arg
, j
++);
88 qq
= PySequence_GetItem(arg
, j
++);
90 if (!PySequence_Check(qq
) || PySequence_Size(qq
) != 2) {
92 TYERR("want a sequence of pairs");
94 x
= PySequence_GetItem(qq
, 0);
95 y
= PySequence_GetItem(qq
, 1);
98 if (!x
|| !y
) goto end
;
99 if (fill(vv
, me
, x
, y
)) {
102 if (!PyErr_Occurred())
103 PyErr_SetString(PyExc_TypeError
, "type mismatch");
113 for (j
= 0, vv
= v
; j
< i
; j
++, vv
+= efsz
)
121 static PyObject
*smallprimes(void)
123 PyObject
*v
= PyList_New(NPRIME
);
126 for (i
= 0; i
< NPRIME
; i
++)
127 PyList_SetItem(v
, i
, PyInt_FromLong(primetab
[i
]));
131 static PyObject
*meth__ego(PyObject
*me
, PyObject
*arg
)
134 if (!PyArg_ParseTuple(arg
, "s:_ego", &argv0
))
136 if (strcmp(QUIS
, "<UNNAMED>") == 0)
141 static PyMethodDef methods
[] = {
142 #define METHNAME(func) meth_##func
143 METH (_ego
, "_ego(ARGV0)")
148 static void init_random(void)
150 #if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6)
154 if (!Py_HashRandomizationFlag
) return;
155 seed
= getenv("PYTHONHASHSEED");
156 if (!seed
|| strcmp(seed
, "random") == 0) r
= GR_WORD(&rand_global
);
157 else r
= strtoul(seed
, 0, 0);
158 if (!r
) r
= 0xe011f220; /* zero doesn't work well */
159 unihash_setkey(&unihash_global
, r
);
169 mod
= Py_InitModule("catacomb._base", donemethods());
171 INSERT("smallprimes", smallprimes());
172 setconstants(mod
, consts
);
175 /*----- That's all, folks -------------------------------------------------*/