File: eexecOpmodule.c

package info (click to toggle)
fonttools 2.4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,876 kB
  • ctags: 1,817
  • sloc: python: 37,652; ansic: 149; makefile: 11
file content (203 lines) | stat: -rw-r--r-- 4,674 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
**  Copyright 1996-2001 by Letterror: Just van Rossum, The Netherlands.
**	
**  Open source.
**
**  Module implementing the eexec and charstring encryption algorithm as 
**  used by PostScript Type 1 fonts.
**	
*/

#include "Python.h"
#include <ctype.h>

static PyObject *ErrorObject;

/* ----------------------------------------------------- */

static char eexec_decrypt__doc__[] =
""
;

static PyObject *
eexec_decrypt(PyObject *self, PyObject *args)
{
	PyObject *_res = NULL;
	unsigned short R;
	int tempR;  /* can't portably use unsigned shorts between Python versions */
	unsigned short c1 = 52845;
	unsigned short c2 = 22719;
	unsigned char * inbuf;
	unsigned char * outbuf;
	unsigned long counter, insize;
	
	if (!PyArg_ParseTuple(args, "s#i", &inbuf, &insize, &tempR))
		return NULL;
	
	R = (unsigned short)tempR;
	
	if ((outbuf = malloc(insize)) == NULL)
	{
		PyErr_NoMemory();
		return NULL;
	}
	for(counter = 0;counter < insize; counter++) {
		outbuf[counter] = (inbuf[counter] ^ (R>>8));
		R = (inbuf[counter] + R) * c1 + c2;
	}
	
	_res = Py_BuildValue("s#l", outbuf, insize, (unsigned long)R);
	free(outbuf);
	return _res;
}

static char eexec_encrypt__doc__[] =
""
;

static PyObject *
eexec_encrypt(PyObject *self, PyObject *args)
{
	PyObject *_res = NULL;
	unsigned short R;
	int tempR;  /* can't portably use unsigned shorts between Python versions */
	unsigned short c1 = 52845;
	unsigned short c2 = 22719;
	unsigned char * inbuf;
	unsigned char * outbuf;
	unsigned long counter, insize;
	
	if (!PyArg_ParseTuple(args, "s#i", &inbuf, &insize, &tempR))
		return NULL;
	
	R = (unsigned short)tempR;
	
	if ((outbuf = malloc(insize)) == NULL)
	{
		PyErr_NoMemory();
		return NULL;
	}
	for(counter = 0;counter < insize; counter++) {
		outbuf[counter] = (inbuf[counter] ^ (R>>8));
		R = (outbuf[counter] + R) * c1 + c2;
	}
	
	_res = Py_BuildValue("s#l", outbuf, insize, (unsigned long)R);
	free(outbuf);
	return _res;
}

static char eexec_hexString__doc__[] =
""
;

static PyObject *
eexec_hexString(PyObject *self, PyObject *args)
{
	PyObject *_res = NULL;
	unsigned char * inbuf;
	unsigned char * outbuf;
	static const unsigned char hexchars[] = "0123456789ABCDEF";
	unsigned long i, insize;
	
	if (!PyArg_ParseTuple(args, "s#", &inbuf, &insize))
		return NULL;
	
	outbuf = malloc(2 * insize);
	if (outbuf == NULL) {
		PyErr_NoMemory();
		return NULL;
	}
	
	for (i = 0; i < insize; i++) {
		outbuf[2 * i] = hexchars[(inbuf[i] >> 4) & 0xF];
		outbuf[2 * i + 1] = hexchars[inbuf[i] & 0xF];
	}
	_res = Py_BuildValue("s#", outbuf, 2 * insize);
	free(outbuf);
	return _res;
}


#define HEX2DEC(c) ((c) >= 'A' ? ((c) - 'A' + 10) : ((c) - '0'))

static char eexec_deHexString__doc__[] =
""
;

static PyObject *
eexec_deHexString(PyObject *self, PyObject *args)
{
	PyObject *_res = NULL;
	unsigned char * inbuf;
	unsigned char * outbuf;
	unsigned char c1, c2;
	unsigned long insize, i;
	
	if (!PyArg_ParseTuple(args, "s#", &inbuf, &insize))
		return NULL;
	
	if (insize % 2) {
		PyErr_SetString(ErrorObject, "hex string must have even length");
		return NULL;
	}
	
	outbuf = malloc(insize / 2);
	if (outbuf == NULL) {
		PyErr_NoMemory();
		return NULL;
	}
	
	for ( i = 0; i < insize; i += 2) {
		c1 = toupper(inbuf[i]);
		c2 = toupper(inbuf[i+1]);
		if (!isxdigit(c1) || !isxdigit(c1)) {
			PyErr_SetString(ErrorObject, "non-hex character found");
			goto error;
		}
		outbuf[i/2] = (HEX2DEC(c2)) | (HEX2DEC(c1) << 4);
	}
	_res = Py_BuildValue("s#", outbuf, insize / 2);
error:
	free(outbuf);
	return _res;
}

/* List of methods defined in the module */

static struct PyMethodDef eexec_methods[] = {
	{"decrypt",	(PyCFunction)eexec_decrypt,		METH_VARARGS,	eexec_decrypt__doc__},
	{"encrypt",	(PyCFunction)eexec_encrypt,		METH_VARARGS,	eexec_encrypt__doc__},
 	{"hexString",	(PyCFunction)eexec_hexString,		METH_VARARGS,	eexec_hexString__doc__},
	{"deHexString",	(PyCFunction)eexec_deHexString,	METH_VARARGS,	eexec_deHexString__doc__},
	{NULL, (PyCFunction)NULL, 0, NULL}		/* sentinel */
};


/* Initialization function for the module (*must* be called initeexec) */

static char eexec_module_documentation[] = 
""
;

void initeexecOp(void); /* prototype to shut up the compiler */

void initeexecOp(void)
{
	PyObject *m, *d;

	/* Create the module and add the functions */
	m = Py_InitModule4("eexecOp", eexec_methods,
		eexec_module_documentation,
		(PyObject*)NULL,PYTHON_API_VERSION);

	/* Add some symbolic constants to the module */
	d = PyModule_GetDict(m);
	ErrorObject = PyString_FromString("eexec.error");
	PyDict_SetItemString(d, "error", ErrorObject);

	/* Check for errors */
	if (PyErr_Occurred())
		Py_FatalError("can't initialize module eexec");
}