added eexecOp, C implementation of the new fontTools.misc.eexec module.
git-svn-id: svn://svn.code.sf.net/p/fonttools/code/trunk@55 4cde692c-a291-49d1-8350-778aa11640f8
This commit is contained in:
parent
599cc2fdf5
commit
41ca640f09
15
Src/eexecOp/README.txt
Normal file
15
Src/eexecOp/README.txt
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
eexecOp is imported by the fontTools.misc.eexec module, and the latter
|
||||||
|
provides a (slow) Python implementation in case eexecOp isn't there.
|
||||||
|
It is designed to be a shared library, to be placed in
|
||||||
|
FontTools/Lib/fontTools/misc/
|
||||||
|
but it should also work as a statically linked top level module.
|
||||||
|
|
||||||
|
Other files in this directory:
|
||||||
|
eexecOp.ppc.prj a Metrowerks CodeWarrior project file for MacOS
|
||||||
|
eexecOp.ppc.prj.exp an export file, exporting the initeexecOp symbol.
|
||||||
|
|
||||||
|
What else should be in this directory:
|
||||||
|
Makefiles for other compilers. Any help appreciated, since I'm still
|
||||||
|
a Mac-only guy!
|
||||||
|
|
||||||
|
Just
|
BIN
Src/eexecOp/eexecOp.ppc.prj
Normal file
BIN
Src/eexecOp/eexecOp.ppc.prj
Normal file
Binary file not shown.
1
Src/eexecOp/eexecOp.ppc.prj.exp
Normal file
1
Src/eexecOp/eexecOp.ppc.prj.exp
Normal file
@ -0,0 +1 @@
|
|||||||
|
initeexecOp
|
205
Src/eexecOp/eexecOpmodule.c
Normal file
205
Src/eexecOp/eexecOpmodule.c
Normal file
@ -0,0 +1,205 @@
|
|||||||
|
/*
|
||||||
|
** Copyright 1996-2000 by Letterror: Just van Rossum, Den Haag, 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(self, args)
|
||||||
|
PyObject *self; /* Not used */
|
||||||
|
PyObject *args;
|
||||||
|
{
|
||||||
|
PyObject *_res = NULL;
|
||||||
|
unsigned short int R;
|
||||||
|
unsigned short int c1 = 52845;
|
||||||
|
unsigned short int c2 = 22719;
|
||||||
|
unsigned char * inbuf;
|
||||||
|
unsigned char * outbuf;
|
||||||
|
unsigned long counter, insize;
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "s#h", &inbuf, &insize, &R))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
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(self, args)
|
||||||
|
PyObject *self; /* Not used */
|
||||||
|
PyObject *args;
|
||||||
|
{
|
||||||
|
PyObject *_res = NULL;
|
||||||
|
unsigned short int R;
|
||||||
|
unsigned short int c1 = 52845;
|
||||||
|
unsigned short int c2 = 22719;
|
||||||
|
unsigned char * inbuf;
|
||||||
|
unsigned char * outbuf;
|
||||||
|
unsigned long counter, insize;
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "s#h", &inbuf, &insize, &R))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
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(self, args)
|
||||||
|
PyObject *self; /* Not used */
|
||||||
|
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(self, args)
|
||||||
|
PyObject *self; /* Not used */
|
||||||
|
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(); /* prototype to shut up the compiler */
|
||||||
|
|
||||||
|
void initeexecOp()
|
||||||
|
{
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user