Launchpad automatic translations update.
[odoo/odoo.git] / bin / report / pyPdf / utils.py
1 # vim: sw=4:expandtab:foldmethod=marker\r
2 #\r
3 # Copyright (c) 2006, Mathieu Fenniak\r
4 # All rights reserved.\r
5 #\r
6 # Redistribution and use in source and binary forms, with or without\r
7 # modification, are permitted provided that the following conditions are\r
8 # met:\r
9 #\r
10 # * Redistributions of source code must retain the above copyright notice,\r
11 # this list of conditions and the following disclaimer.\r
12 # * Redistributions in binary form must reproduce the above copyright notice,\r
13 # this list of conditions and the following disclaimer in the documentation\r
14 # and/or other materials provided with the distribution.\r
15 # * The name of the author may not be used to endorse or promote products\r
16 # derived from this software without specific prior written permission.\r
17 #\r
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
22 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
28 # POSSIBILITY OF SUCH DAMAGE.\r
29 \r
30 \r
31 """\r
32 Utility functions for PDF library.\r
33 """\r
34 __author__ = "Mathieu Fenniak"\r
35 __author_email__ = "biziqe@mathieu.fenniak.net"\r
36 \r
37 #ENABLE_PSYCO = False\r
38 #if ENABLE_PSYCO:\r
39 #    try:\r
40 #        import psyco\r
41 #    except ImportError:\r
42 #        ENABLE_PSYCO = False\r
43 #\r
44 #if not ENABLE_PSYCO:\r
45 #    class psyco:\r
46 #        def proxy(func):\r
47 #            return func\r
48 #        proxy = staticmethod(proxy)\r
49 \r
50 def readUntilWhitespace(stream, maxchars=None):\r
51     txt = ""\r
52     while True:\r
53         tok = stream.read(1)\r
54         if tok.isspace() or not tok:\r
55             break\r
56         txt += tok\r
57         if len(txt) == maxchars:\r
58             break\r
59     return txt\r
60 \r
61 def readNonWhitespace(stream):\r
62     tok = ' '\r
63     while tok == '\n' or tok == '\r' or tok == ' ' or tok == '\t':\r
64         tok = stream.read(1)\r
65     return tok\r
66 \r
67 class ConvertFunctionsToVirtualList(object):\r
68     def __init__(self, lengthFunction, getFunction):\r
69         self.lengthFunction = lengthFunction\r
70         self.getFunction = getFunction\r
71 \r
72     def __len__(self):\r
73         return self.lengthFunction()\r
74 \r
75     def __getitem__(self, index):\r
76         if not isinstance(index, int):\r
77             raise TypeError, "sequence indices must be integers"\r
78         len_self = len(self)\r
79         if index < 0:\r
80             # support negative indexes\r
81             index = len_self + index\r
82         if index < 0 or index >= len_self:\r
83             raise IndexError, "sequence index out of range"\r
84         return self.getFunction(index)\r
85 \r
86 def RC4_encrypt(key, plaintext):\r
87     S = [i for i in range(256)]\r
88     j = 0\r
89     for i in range(256):\r
90         j = (j + S[i] + ord(key[i % len(key)])) % 256\r
91         S[i], S[j] = S[j], S[i]\r
92     i, j = 0, 0\r
93     retval = ""\r
94     for x in range(len(plaintext)):\r
95         i = (i + 1) % 256\r
96         j = (j + S[i]) % 256\r
97         S[i], S[j] = S[j], S[i]\r
98         t = S[(S[i] + S[j]) % 256]\r
99         retval += chr(ord(plaintext[x]) ^ t)\r
100     return retval\r
101 \r
102 def matrixMultiply(a, b):\r
103     return [[sum([float(i)*float(j)\r
104                   for i, j in zip(row, col)]\r
105                 ) for col in zip(*b)]\r
106             for row in a]\r
107 \r
108 class PyPdfError(Exception):\r
109     pass\r
110 \r
111 class PdfReadError(PyPdfError):\r
112     pass\r
113 \r
114 class PageSizeNotDefinedError(PyPdfError):\r
115     pass\r
116 \r
117 if __name__ == "__main__":\r
118     # test RC4\r
119     out = RC4_encrypt("Key", "Plaintext")\r
120     print repr(out)\r
121     pt = RC4_encrypt("Key", out)\r
122     print repr(pt)\r