blob: 3befda7c83e295145f4a856e792ff8e40db934c5 [file] [log] [blame]
Just41ca6402000-01-12 19:13:29 +00001/*
jvr0d2a9552001-08-16 16:34:37 +00002** Copyright 1996-2001 by Letterror: Just van Rossum, The Netherlands.
Just41ca6402000-01-12 19:13:29 +00003**
4** Open source.
5**
6** Module implementing the eexec and charstring encryption algorithm as
7** used by PostScript Type 1 fonts.
8**
9*/
10
11#include "Python.h"
12#include <ctype.h>
13
14static PyObject *ErrorObject;
15
16/* ----------------------------------------------------- */
17
18static char eexec_decrypt__doc__[] =
19""
20;
21
22static PyObject *
jvr93bde122001-08-09 19:36:36 +000023eexec_decrypt(PyObject *self, PyObject *args)
Just41ca6402000-01-12 19:13:29 +000024{
25 PyObject *_res = NULL;
jvrbad5dd22001-08-16 18:14:48 +000026 unsigned short R;
27 int tempR; /* can't portably use unsigned shorts between Python versions */
28 unsigned short c1 = 52845;
29 unsigned short c2 = 22719;
Just41ca6402000-01-12 19:13:29 +000030 unsigned char * inbuf;
31 unsigned char * outbuf;
32 unsigned long counter, insize;
33
jvrbad5dd22001-08-16 18:14:48 +000034 if (!PyArg_ParseTuple(args, "s#i", &inbuf, &insize, &tempR))
Just41ca6402000-01-12 19:13:29 +000035 return NULL;
36
jvrbad5dd22001-08-16 18:14:48 +000037 R = (unsigned short)tempR;
38
Just41ca6402000-01-12 19:13:29 +000039 if ((outbuf = malloc(insize)) == NULL)
40 {
41 PyErr_NoMemory();
42 return NULL;
43 }
44 for(counter = 0;counter < insize; counter++) {
45 outbuf[counter] = (inbuf[counter] ^ (R>>8));
46 R = (inbuf[counter] + R) * c1 + c2;
47 }
48
49 _res = Py_BuildValue("s#l", outbuf, insize, (unsigned long)R);
50 free(outbuf);
51 return _res;
52}
53
54static char eexec_encrypt__doc__[] =
55""
56;
57
58static PyObject *
jvr93bde122001-08-09 19:36:36 +000059eexec_encrypt(PyObject *self, PyObject *args)
Just41ca6402000-01-12 19:13:29 +000060{
61 PyObject *_res = NULL;
jvrbad5dd22001-08-16 18:14:48 +000062 unsigned short R;
63 int tempR; /* can't portably use unsigned shorts between Python versions */
64 unsigned short c1 = 52845;
65 unsigned short c2 = 22719;
Just41ca6402000-01-12 19:13:29 +000066 unsigned char * inbuf;
67 unsigned char * outbuf;
68 unsigned long counter, insize;
69
jvrbad5dd22001-08-16 18:14:48 +000070 if (!PyArg_ParseTuple(args, "s#i", &inbuf, &insize, &tempR))
Just41ca6402000-01-12 19:13:29 +000071 return NULL;
72
jvrbad5dd22001-08-16 18:14:48 +000073 R = (unsigned short)tempR;
74
Just41ca6402000-01-12 19:13:29 +000075 if ((outbuf = malloc(insize)) == NULL)
76 {
77 PyErr_NoMemory();
78 return NULL;
79 }
80 for(counter = 0;counter < insize; counter++) {
81 outbuf[counter] = (inbuf[counter] ^ (R>>8));
82 R = (outbuf[counter] + R) * c1 + c2;
83 }
84
85 _res = Py_BuildValue("s#l", outbuf, insize, (unsigned long)R);
86 free(outbuf);
87 return _res;
88}
89
90static char eexec_hexString__doc__[] =
91""
92;
93
94static PyObject *
jvr93bde122001-08-09 19:36:36 +000095eexec_hexString(PyObject *self, PyObject *args)
Just41ca6402000-01-12 19:13:29 +000096{
97 PyObject *_res = NULL;
98 unsigned char * inbuf;
99 unsigned char * outbuf;
100 static const unsigned char hexchars[] = "0123456789ABCDEF";
101 unsigned long i, insize;
102
103 if (!PyArg_ParseTuple(args, "s#", &inbuf, &insize))
104 return NULL;
105
106 outbuf = malloc(2 * insize);
107 if (outbuf == NULL) {
108 PyErr_NoMemory();
109 return NULL;
110 }
111
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];
115 }
116 _res = Py_BuildValue("s#", outbuf, 2 * insize);
117 free(outbuf);
118 return _res;
119}
120
121
122#define HEX2DEC(c) ((c) >= 'A' ? ((c) - 'A' + 10) : ((c) - '0'))
123
124static char eexec_deHexString__doc__[] =
125""
126;
127
128static PyObject *
jvr93bde122001-08-09 19:36:36 +0000129eexec_deHexString(PyObject *self, PyObject *args)
Just41ca6402000-01-12 19:13:29 +0000130{
131 PyObject *_res = NULL;
132 unsigned char * inbuf;
133 unsigned char * outbuf;
134 unsigned char c1, c2;
135 unsigned long insize, i;
136
137 if (!PyArg_ParseTuple(args, "s#", &inbuf, &insize))
138 return NULL;
139
140 if (insize % 2) {
141 PyErr_SetString(ErrorObject, "hex string must have even length");
142 return NULL;
143 }
144
145 outbuf = malloc(insize / 2);
146 if (outbuf == NULL) {
147 PyErr_NoMemory();
148 return NULL;
149 }
150
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");
156 goto error;
157 }
158 outbuf[i/2] = (HEX2DEC(c2)) | (HEX2DEC(c1) << 4);
159 }
160 _res = Py_BuildValue("s#", outbuf, insize / 2);
161error:
162 free(outbuf);
163 return _res;
164}
165
166/* List of methods defined in the module */
167
168static 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 */
174};
175
176
177/* Initialization function for the module (*must* be called initeexec) */
178
179static char eexec_module_documentation[] =
180""
181;
182
jvrc0cd8262001-08-09 19:39:05 +0000183void initeexecOp(void); /* prototype to shut up the compiler */
Just41ca6402000-01-12 19:13:29 +0000184
jvrc0cd8262001-08-09 19:39:05 +0000185void initeexecOp(void)
Just41ca6402000-01-12 19:13:29 +0000186{
187 PyObject *m, *d;
188
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);
193
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);
198
199 /* Check for errors */
200 if (PyErr_Occurred())
201 Py_FatalError("can't initialize module eexec");
202}
203