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