home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_2409 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  7.3 KB  |  236 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __author__ = 'Mathieu Fenniak'
  5. __author_email__ = 'biziqe@mathieu.fenniak.net'
  6. from utils import PdfReadError
  7.  
  8. try:
  9.     from cStringIO import StringIO
  10. except ImportError:
  11.     from StringIO import StringIO
  12.  
  13.  
  14. try:
  15.     import zlib
  16.     
  17.     def decompress(data):
  18.         return zlib.decompress(data)
  19.  
  20.     
  21.     def compress(data):
  22.         return zlib.compress(data)
  23.  
  24. except ImportError:
  25.     import System
  26.     from System import IO, Collections, Array
  27.     
  28.     def _string_to_bytearr(buf):
  29.         retval = Array.CreateInstance(System.Byte, len(buf))
  30.         for i in range(len(buf)):
  31.             retval[i] = ord(buf[i])
  32.         
  33.         return retval
  34.  
  35.     
  36.     def _bytearr_to_string(bytes):
  37.         retval = ''
  38.         for i in range(bytes.Length):
  39.             retval += chr(bytes[i])
  40.         
  41.         return retval
  42.  
  43.     
  44.     def _read_bytes(stream):
  45.         ms = IO.MemoryStream()
  46.         buf = Array.CreateInstance(System.Byte, 2048)
  47.         while True:
  48.             bytes = stream.Read(buf, 0, buf.Length)
  49.             if bytes == 0:
  50.                 break
  51.                 continue
  52.             ms.Write(buf, 0, bytes)
  53.         retval = ms.ToArray()
  54.         ms.Close()
  55.         return retval
  56.  
  57.     
  58.     def decompress(data):
  59.         bytes = _string_to_bytearr(data)
  60.         ms = IO.MemoryStream()
  61.         ms.Write(bytes, 0, bytes.Length)
  62.         ms.Position = 0
  63.         gz = IO.Compression.DeflateStream(ms, IO.Compression.CompressionMode.Decompress)
  64.         bytes = _read_bytes(gz)
  65.         retval = _bytearr_to_string(bytes)
  66.         gz.Close()
  67.         return retval
  68.  
  69.     
  70.     def compress(data):
  71.         bytes = _string_to_bytearr(data)
  72.         ms = IO.MemoryStream()
  73.         gz = IO.Compression.DeflateStream(ms, IO.Compression.CompressionMode.Compress, True)
  74.         gz.Write(bytes, 0, bytes.Length)
  75.         gz.Close()
  76.         ms.Position = 0
  77.         bytes = ms.ToArray()
  78.         retval = _bytearr_to_string(bytes)
  79.         ms.Close()
  80.         return retval
  81.  
  82.  
  83.  
  84. class FlateDecode(object):
  85.     
  86.     def decode(data, decodeParms):
  87.         data = decompress(data)
  88.         predictor = 1
  89.         if decodeParms:
  90.             predictor = decodeParms.get('/Predictor', 1)
  91.         
  92.         if predictor != 1:
  93.             columns = decodeParms['/Columns']
  94.             if predictor >= 10 and predictor <= 15:
  95.                 output = StringIO()
  96.                 rowlength = columns + 1
  97.                 prev_rowdata = (0,) * rowlength
  98.                 for row in xrange(len(data) / rowlength):
  99.                     rowdata = [ ord(x) for x in data[row * rowlength:(row + 1) * rowlength] ]
  100.                     filterByte = rowdata[0]
  101.                     if filterByte == 0:
  102.                         pass
  103.                     elif filterByte == 1:
  104.                         for i in range(2, rowlength):
  105.                             rowdata[i] = (rowdata[i] + rowdata[i - 1]) % 256
  106.                         
  107.                     elif filterByte == 2:
  108.                         for i in range(1, rowlength):
  109.                             rowdata[i] = (rowdata[i] + prev_rowdata[i]) % 256
  110.                         
  111.                     else:
  112.                         raise PdfReadError('Unsupported PNG filter %r' % filterByte)
  113.                     prev_rowdata = []
  114.                     []([]([ chr(x) for x in rowdata[1:] ]))
  115.                 
  116.                 data = output.getvalue()
  117.             else:
  118.                 raise PdfReadError('Unsupported flatedecode predictor %r' % predictor)
  119.         predictor <= 15
  120.         return data
  121.  
  122.     decode = staticmethod(decode)
  123.     
  124.     def encode(data):
  125.         return compress(data)
  126.  
  127.     encode = staticmethod(encode)
  128.  
  129.  
  130. class ASCIIHexDecode(object):
  131.     
  132.     def decode(data, decodeParms = None):
  133.         retval = ''
  134.         char = ''
  135.         x = 0
  136.         while True:
  137.             c = data[x]
  138.             if c == '>':
  139.                 break
  140.             elif c.isspace():
  141.                 x += 1
  142.                 continue
  143.             
  144.             char += c
  145.             if len(char) == 2:
  146.                 retval += chr(int(char, base = 16))
  147.                 char = ''
  148.             
  149.             x += 1
  150.         return retval
  151.  
  152.     decode = staticmethod(decode)
  153.  
  154.  
  155. class ASCII85Decode(object):
  156.     
  157.     def decode(data, decodeParms = None):
  158.         retval = ''
  159.         group = []
  160.         x = 0
  161.         hitEod = False
  162.         data = _[1]
  163.         while not hitEod:
  164.             c = data[x]
  165.             if len(retval) == 0 and c == '<' and data[x + 1] == '~':
  166.                 x += 2
  167.                 continue
  168.             elif c == 'z':
  169.                 retval += '\x00\x00\x00\x00'
  170.                 continue
  171.             elif c == '~' and data[x + 1] == '>':
  172.                 if len(group) != 0:
  173.                     cnt = len(group) - 1
  174.                     group += [
  175.                         85,
  176.                         85,
  177.                         85]
  178.                     hitEod = cnt
  179.                 else:
  180.                     break
  181.             else:
  182.                 c = ord(c) - 33
  183.                 group += [
  184.                     c]
  185.             if len(group) >= 5:
  186.                 b = group[0] * 52200625 + group[1] * 614125 + group[2] * 7225 + group[3] * 85 + group[4]
  187.                 c4 = chr((b >> 0) % 256)
  188.                 c3 = chr((b >> 8) % 256)
  189.                 c2 = chr((b >> 16) % 256)
  190.                 c1 = chr(b >> 24)
  191.                 retval += c1 + c2 + c3 + c4
  192.                 if hitEod:
  193.                     retval = retval[:-4 + hitEod]
  194.                 
  195.                 group = []
  196.             
  197.             x += 1
  198.         return retval
  199.  
  200.     decode = staticmethod(decode)
  201.  
  202.  
  203. def decodeStreamData(stream):
  204.     NameObject = NameObject
  205.     import generic
  206.     filters = stream.get('/Filter', ())
  207.     if len(filters) and not isinstance(filters[0], NameObject):
  208.         filters = (filters,)
  209.     
  210.     data = stream._data
  211.     for filterType in filters:
  212.         if filterType == '/FlateDecode':
  213.             data = FlateDecode.decode(data, stream.get('/DecodeParms'))
  214.             continue
  215.         if filterType == '/ASCIIHexDecode':
  216.             data = ASCIIHexDecode.decode(data)
  217.             continue
  218.         if filterType == '/ASCII85Decode':
  219.             data = ASCII85Decode.decode(data)
  220.             continue
  221.         if filterType == '/Crypt':
  222.             decodeParams = stream.get('/DecodeParams', { })
  223.             if '/Name' not in decodeParams and '/Type' not in decodeParams:
  224.                 pass
  225.             else:
  226.                 raise NotImplementedError('/Crypt filter with /Name or /Type not supported yet')
  227.         '/Type' not in decodeParams
  228.         raise NotImplementedError('unsupported filter %s' % filterType)
  229.     
  230.     return data
  231.  
  232. if __name__ == '__main__':
  233.     ascii85Test = '\n     <~9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,\n     O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKY\n     i(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIa\n     l(DId<j@<?3r@:F%a+D58\'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G\n     >uD.RTpAKYo\'+CT/5+Cei#DII?(E,9)oF*2M7/c~>\n    '
  234.     ascii85_originalText = 'Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.'
  235.  
  236.