1 # vim: sw=4:expandtab:foldmethod=marker
\r
3 # Copyright (c) 2006, Mathieu Fenniak
\r
4 # All rights reserved.
\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
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
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
32 Utility functions for PDF library.
\r
34 __author__ = "Mathieu Fenniak"
\r
35 __author_email__ = "biziqe@mathieu.fenniak.net"
\r
37 #ENABLE_PSYCO = False
\r
41 # except ImportError:
\r
42 # ENABLE_PSYCO = False
\r
44 #if not ENABLE_PSYCO:
\r
48 # proxy = staticmethod(proxy)
\r
50 def readUntilWhitespace(stream, maxchars=None):
\r
53 tok = stream.read(1)
\r
54 if tok.isspace() or not tok:
\r
57 if len(txt) == maxchars:
\r
61 def readNonWhitespace(stream):
\r
63 while tok == '\n' or tok == '\r' or tok == ' ' or tok == '\t':
\r
64 tok = stream.read(1)
\r
67 class ConvertFunctionsToVirtualList(object):
\r
68 def __init__(self, lengthFunction, getFunction):
\r
69 self.lengthFunction = lengthFunction
\r
70 self.getFunction = getFunction
\r
73 return self.lengthFunction()
\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
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
86 def RC4_encrypt(key, plaintext):
\r
87 S = [i for i in range(256)]
\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
94 for x in range(len(plaintext)):
\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
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
108 class PyPdfError(Exception):
\r
111 class PdfReadError(PyPdfError):
\r
114 class PageSizeNotDefinedError(PyPdfError):
\r
117 if __name__ == "__main__":
\r
119 out = RC4_encrypt("Key", "Plaintext")
\r
121 pt = RC4_encrypt("Key", out)
\r