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
|
/* t1code.c: Copyright 2005 Jörg Lehmann
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdlib.h>
#include <stdint.h>
#define C1 52845
#define C2 22719
/*
def decoder(code, r, n):
c1 = 52845
c2 = 22719
data = array.array("B")
for x in array.array("B", code):
data.append(x ^ (r >> 8))
r = ((x + r) * c1 + c2) & 0xffff
return data.tobytes()[n:]
*/
static PyObject *py_decoder(PyObject *self, PyObject *args)
{
unsigned char *code;
Py_ssize_t lcode;
int pr, n;
if (PyArg_ParseTuple(args, "y#ii", (char **) &code, &lcode, &pr, &n)) {
unsigned char *data;
Py_ssize_t i;
unsigned char x;
uint16_t r=pr;
PyObject *result;
if (! (data = (unsigned char *) malloc(lcode)) )
return NULL;
for (i=0; i<lcode; i++) {
x = code[i];
data[i] = x ^ ( r >> 8);
r = (x + r) * C1 + C2;
}
/* convert result to string stripping first n chars */
result = PyBytes_FromStringAndSize((const char *)data + n, lcode - n);
free(data);
return result;
}
else return NULL;
}
/*
def encoder(data, r, random):
c1 = 52845
c2 = 22719
code = array.array("B")
for x in array.array("B", random+data):
code.append(x ^ (r >> 8))
r = ((code[-1] + r) * c1 + c2) & 0xffff;
return code.tobytes()
*/
static PyObject *py_encoder(PyObject *self, PyObject *args)
{
unsigned char *data;
unsigned char *random;
Py_ssize_t ldata, lrandom;
int pr;
if (PyArg_ParseTuple(args, "y#iy#", (char **) &data, &ldata, &pr, (char **) &random, &lrandom)) {
unsigned char *code;
Py_ssize_t i;
uint16_t r=pr;
PyObject *result;
if (! (code = (unsigned char *) malloc(ldata + lrandom)) )
return NULL;
for (i=0; i<lrandom; i++) {
code[i] = random[i] ^ ( r >> 8);
r = (code[i] + r) * C1 + C2;
}
for (i=0; i<ldata; i++) {
code[i+lrandom] = data[i] ^ ( r >> 8);
r = (code[i+lrandom] + r) * C1 + C2;
}
result = PyBytes_FromStringAndSize((const char *)code, ldata + lrandom);
free(code);
return result;
}
else return NULL;
}
/* exported methods */
static PyMethodDef t1code_methods[] = {
{"decoder", py_decoder, METH_VARARGS, NULL},
{"encoder", py_encoder, METH_VARARGS, NULL},
{NULL, NULL}
};
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_t1code",
NULL,
-1,
t1code_methods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC
PyInit__t1code(void)
{
PyObject *module = PyModule_Create(&moduledef);
return module;
}
|