Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 1 | /* |
jvr | 0d2a955 | 2001-08-16 16:34:37 +0000 | [diff] [blame] | 2 | ** Copyright 1996-2001 by Letterror: Just van Rossum, The Netherlands. |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 3 | ** |
| 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 | |
| 14 | static PyObject *ErrorObject; |
| 15 | |
| 16 | /* ----------------------------------------------------- */ |
| 17 | |
| 18 | static char eexec_decrypt__doc__[] = |
| 19 | "" |
| 20 | ; |
| 21 | |
| 22 | static PyObject * |
jvr | 93bde12 | 2001-08-09 19:36:36 +0000 | [diff] [blame] | 23 | eexec_decrypt(PyObject *self, PyObject *args) |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 24 | { |
| 25 | PyObject *_res = NULL; |
jvr | bad5dd2 | 2001-08-16 18:14:48 +0000 | [diff] [blame] | 26 | 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; |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 30 | unsigned char * inbuf; |
| 31 | unsigned char * outbuf; |
| 32 | unsigned long counter, insize; |
| 33 | |
jvr | bad5dd2 | 2001-08-16 18:14:48 +0000 | [diff] [blame] | 34 | if (!PyArg_ParseTuple(args, "s#i", &inbuf, &insize, &tempR)) |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 35 | return NULL; |
| 36 | |
jvr | bad5dd2 | 2001-08-16 18:14:48 +0000 | [diff] [blame] | 37 | R = (unsigned short)tempR; |
| 38 | |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 39 | 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 | |
| 54 | static char eexec_encrypt__doc__[] = |
| 55 | "" |
| 56 | ; |
| 57 | |
| 58 | static PyObject * |
jvr | 93bde12 | 2001-08-09 19:36:36 +0000 | [diff] [blame] | 59 | eexec_encrypt(PyObject *self, PyObject *args) |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 60 | { |
| 61 | PyObject *_res = NULL; |
jvr | bad5dd2 | 2001-08-16 18:14:48 +0000 | [diff] [blame] | 62 | 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; |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 66 | unsigned char * inbuf; |
| 67 | unsigned char * outbuf; |
| 68 | unsigned long counter, insize; |
| 69 | |
jvr | bad5dd2 | 2001-08-16 18:14:48 +0000 | [diff] [blame] | 70 | if (!PyArg_ParseTuple(args, "s#i", &inbuf, &insize, &tempR)) |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 71 | return NULL; |
| 72 | |
jvr | bad5dd2 | 2001-08-16 18:14:48 +0000 | [diff] [blame] | 73 | R = (unsigned short)tempR; |
| 74 | |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 75 | 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 | |
| 90 | static char eexec_hexString__doc__[] = |
| 91 | "" |
| 92 | ; |
| 93 | |
| 94 | static PyObject * |
jvr | 93bde12 | 2001-08-09 19:36:36 +0000 | [diff] [blame] | 95 | eexec_hexString(PyObject *self, PyObject *args) |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 96 | { |
| 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 | |
| 124 | static char eexec_deHexString__doc__[] = |
| 125 | "" |
| 126 | ; |
| 127 | |
| 128 | static PyObject * |
jvr | 93bde12 | 2001-08-09 19:36:36 +0000 | [diff] [blame] | 129 | eexec_deHexString(PyObject *self, PyObject *args) |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 130 | { |
| 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); |
| 161 | error: |
| 162 | free(outbuf); |
| 163 | return _res; |
| 164 | } |
| 165 | |
| 166 | /* List of methods defined in the module */ |
| 167 | |
| 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 */ |
| 174 | }; |
| 175 | |
| 176 | |
| 177 | /* Initialization function for the module (*must* be called initeexec) */ |
| 178 | |
| 179 | static char eexec_module_documentation[] = |
| 180 | "" |
| 181 | ; |
| 182 | |
jvr | c0cd826 | 2001-08-09 19:39:05 +0000 | [diff] [blame] | 183 | void initeexecOp(void); /* prototype to shut up the compiler */ |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 184 | |
jvr | c0cd826 | 2001-08-09 19:39:05 +0000 | [diff] [blame] | 185 | void initeexecOp(void) |
Just | 41ca640 | 2000-01-12 19:13:29 +0000 | [diff] [blame] | 186 | { |
| 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 | |