1 /* t1code.c: Copyright 2005 Jörg Lehmann
2 * This program is distributed in the hope that it will be useful,
3 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5 * GNU General Public License for more details.
7 * You should have received a copy of the GNU General Public License
8 * along with this program; if not, write to the Free Software
9 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 def decoder(code, r, n):
24 data = array.array("B")
25 for x in array.array("B", code):
26 data.append(x ^ (r >> 8))
27 r = ((x + r) * c1 + c2) & 0xffff
28 return data.tobytes()[n:]
32 static PyObject
*py_decoder(PyObject
*self
, PyObject
*args
)
37 if (PyArg_ParseTuple(args
, "y#ii", (char **) &code
, &lcode
, &pr
, &n
)) {
44 if (! (data
= (unsigned char *) malloc(lcode
)) )
47 for (i
=0; i
<lcode
; i
++) {
49 data
[i
] = x
^ ( r
>> 8);
50 r
= (x
+ r
) * C1
+ C2
;
53 /* convert result to string stripping first n chars */
54 result
= PyBytes_FromStringAndSize((const char *)data
+ n
, lcode
- n
);
63 def encoder(data, r, random):
66 code = array.array("B")
67 for x in array.array("B", random+data):
68 code.append(x ^ (r >> 8))
69 r = ((code[-1] + r) * c1 + c2) & 0xffff;
73 static PyObject
*py_encoder(PyObject
*self
, PyObject
*args
)
76 unsigned char *random
;
77 int ldata
, pr
, lrandom
;
79 if (PyArg_ParseTuple(args
, "y#iy#", (char **) &data
, &ldata
, &pr
, (char **) &random
, &lrandom
)) {
85 if (! (code
= (unsigned char *) malloc(ldata
+ lrandom
)) )
88 for (i
=0; i
<lrandom
; i
++) {
89 code
[i
] = random
[i
] ^ ( r
>> 8);
90 r
= (code
[i
] + r
) * C1
+ C2
;
93 for (i
=0; i
<ldata
; i
++) {
94 code
[i
+lrandom
] = data
[i
] ^ ( r
>> 8);
95 r
= (code
[i
+lrandom
] + r
) * C1
+ C2
;
98 result
= PyBytes_FromStringAndSize((const char *)code
, ldata
+ lrandom
);
108 /* exported methods */
110 static PyMethodDef t1code_methods
[] = {
111 {"decoder", py_decoder
, METH_VARARGS
, NULL
},
112 {"encoder", py_encoder
, METH_VARARGS
, NULL
},
116 static struct PyModuleDef moduledef
= {
117 PyModuleDef_HEAD_INIT
,
131 PyObject
*module
= PyModule_Create(&moduledef
);