2
** Copyright 1996-2001 by Letterror: Just van Rossum, The Netherlands.
6
** Module implementing the eexec and charstring encryption algorithm as
7
** used by PostScript Type 1 fonts.
14
static PyObject *ErrorObject;
16
/* ----------------------------------------------------- */
18
static char eexec_decrypt__doc__[] =
23
eexec_decrypt(PyObject *self, PyObject *args)
25
PyObject *_res = NULL;
27
int tempR; /* can't portably use unsigned shorts between Python versions */
28
unsigned short c1 = 52845;
29
unsigned short c2 = 22719;
30
unsigned char * inbuf;
31
unsigned char * outbuf;
32
unsigned long counter, insize;
34
if (!PyArg_ParseTuple(args, "s#i", &inbuf, &insize, &tempR))
37
R = (unsigned short)tempR;
39
if ((outbuf = malloc(insize)) == NULL)
44
for(counter = 0;counter < insize; counter++) {
45
outbuf[counter] = (inbuf[counter] ^ (R>>8));
46
R = (inbuf[counter] + R) * c1 + c2;
49
_res = Py_BuildValue("s#l", outbuf, insize, (unsigned long)R);
54
static char eexec_encrypt__doc__[] =
59
eexec_encrypt(PyObject *self, PyObject *args)
61
PyObject *_res = NULL;
63
int tempR; /* can't portably use unsigned shorts between Python versions */
64
unsigned short c1 = 52845;
65
unsigned short c2 = 22719;
66
unsigned char * inbuf;
67
unsigned char * outbuf;
68
unsigned long counter, insize;
70
if (!PyArg_ParseTuple(args, "s#i", &inbuf, &insize, &tempR))
73
R = (unsigned short)tempR;
75
if ((outbuf = malloc(insize)) == NULL)
80
for(counter = 0;counter < insize; counter++) {
81
outbuf[counter] = (inbuf[counter] ^ (R>>8));
82
R = (outbuf[counter] + R) * c1 + c2;
85
_res = Py_BuildValue("s#l", outbuf, insize, (unsigned long)R);
90
static char eexec_hexString__doc__[] =
95
eexec_hexString(PyObject *self, PyObject *args)
97
PyObject *_res = NULL;
98
unsigned char * inbuf;
99
unsigned char * outbuf;
100
static const unsigned char hexchars[] = "0123456789ABCDEF";
101
unsigned long i, insize;
103
if (!PyArg_ParseTuple(args, "s#", &inbuf, &insize))
106
outbuf = malloc(2 * insize);
107
if (outbuf == NULL) {
112
for (i = 0; i < insize; i++) {
113
outbuf[2 * i] = hexchars[(inbuf[i] >> 4) & 0xF];
114
outbuf[2 * i + 1] = hexchars[inbuf[i] & 0xF];
116
_res = Py_BuildValue("s#", outbuf, 2 * insize);
122
#define HEX2DEC(c) ((c) >= 'A' ? ((c) - 'A' + 10) : ((c) - '0'))
124
static char eexec_deHexString__doc__[] =
129
eexec_deHexString(PyObject *self, PyObject *args)
131
PyObject *_res = NULL;
132
unsigned char * inbuf;
133
unsigned char * outbuf;
134
unsigned char c1, c2;
135
unsigned long insize, i;
137
if (!PyArg_ParseTuple(args, "s#", &inbuf, &insize))
141
PyErr_SetString(ErrorObject, "hex string must have even length");
145
outbuf = malloc(insize / 2);
146
if (outbuf == NULL) {
151
for ( i = 0; i < insize; i += 2) {
152
c1 = toupper(inbuf[i]);
153
c2 = toupper(inbuf[i+1]);
154
if (!isxdigit(c1) || !isxdigit(c1)) {
155
PyErr_SetString(ErrorObject, "non-hex character found");
158
outbuf[i/2] = (HEX2DEC(c2)) | (HEX2DEC(c1) << 4);
160
_res = Py_BuildValue("s#", outbuf, insize / 2);
166
/* List of methods defined in the module */
168
static struct PyMethodDef eexec_methods[] = {
169
{"decrypt", (PyCFunction)eexec_decrypt, METH_VARARGS, eexec_decrypt__doc__},
170
{"encrypt", (PyCFunction)eexec_encrypt, METH_VARARGS, eexec_encrypt__doc__},
171
{"hexString", (PyCFunction)eexec_hexString, METH_VARARGS, eexec_hexString__doc__},
172
{"deHexString", (PyCFunction)eexec_deHexString, METH_VARARGS, eexec_deHexString__doc__},
173
{NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
177
/* Initialization function for the module (*must* be called initeexec) */
179
static char eexec_module_documentation[] =
183
void initeexecOp(void); /* prototype to shut up the compiler */
185
void initeexecOp(void)
189
/* Create the module and add the functions */
190
m = Py_InitModule4("eexecOp", eexec_methods,
191
eexec_module_documentation,
192
(PyObject*)NULL,PYTHON_API_VERSION);
194
/* Add some symbolic constants to the module */
195
d = PyModule_GetDict(m);
196
ErrorObject = PyString_FromString("eexec.error");
197
PyDict_SetItemString(d, "error", ErrorObject);
199
/* Check for errors */
200
if (PyErr_Occurred())
201
Py_FatalError("can't initialize module eexec");