2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2005 André Wobst <wobsta@users.sourceforge.net>
7 # This file is part of PyX (http://pyx.sourceforge.net/).
9 # PyX is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # PyX is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with PyX; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 c1_16
, c1_8
= divmod(c1
, 0x100) # to avoid overflow (or conversion to the slow long integers)
29 def decoder(code
, r
, n
):
30 plain
= array
.array("B")
31 for x
in array
.array("B", code
):
32 plain
.append(x ^
(r
>> 8))
33 # r = ((x + r) * c1 + c2) & 0xffff # this might overflow
34 r
= ((((x
+ r
) * c1_16
) & 0xff) * 0x100 + (x
+ r
) * c1_8
+ c2
) & 0xffff
35 return plain
.tostring()[n
:]
37 def encoder(data
, r
, random
):
38 code
= array
.array("B")
39 for x
in array
.array("B", random
+data
):
40 code
.append(x ^
(r
>>8))
41 # r = ((code[-1] + r) * c1 + c2) & 0xffff # this might overflow
42 r
= ((((code
[-1] + r
) * c1_16
) & 0xff) * 0x100 + (code
[-1] + r
) * c1_8
+ c2
) & 0xffff
43 return code
.tostring()