home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_869 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.9 KB  |  102 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
  6. from cStringIO import StringIO
  7. from struct import pack
  8. from calibre.constants import plugins
  9. cPalmdoc = plugins['cPalmdoc'][0]
  10. if not cPalmdoc:
  11.     raise RuntimeError('Failed to load required cPalmdoc module: %s' % plugins['cPalmdoc'][1])
  12. cPalmdoc
  13.  
  14. def decompress_doc(data):
  15.     return cPalmdoc.decompress(data)
  16.  
  17.  
  18. def compress_doc(data):
  19.     return cPalmdoc.compress(data)
  20.  
  21.  
  22. def test():
  23.     TESTS = [
  24.         'abc\x03\x04\x05\x06ms',
  25.         'a b c \xfed ',
  26.         '0123456789axyz2bxyz2cdfgfo9iuyerh',
  27.         '0123456789asd0123456789asd|yyzzxxffhhjjkk',
  28.         'ciewacnaq eiu743 r787q 0w%  ; sa fd\xef\x0cfdxosac wocjp acoiecowei owaic jociowapjcivcjpoivjporeivjpoavca; p9aw8743y6r74%$^$^%8 ']
  29.     for test in TESTS:
  30.         print 'Test:', repr(test)
  31.         print '\tTesting compression...'
  32.         good = py_compress_doc(test)
  33.         x = compress_doc(test)
  34.         print '\t\tgood:', repr(good)
  35.         print '\t\tx   :', repr(x)
  36.         print '\tTesting decompression...'
  37.         print '\t\t', repr(decompress_doc(x))
  38.         print 
  39.     
  40.  
  41.  
  42. def py_compress_doc(data):
  43.     out = StringIO()
  44.     i = 0
  45.     ldata = len(data)
  46.     while i < ldata:
  47.         if i > 10 and ldata - i > 10:
  48.             chunk = ''
  49.             match = -1
  50.             for j in xrange(10, 2, -1):
  51.                 chunk = data[i:i + j]
  52.                 
  53.                 try:
  54.                     match = data.rindex(chunk, 0, i)
  55.                 except ValueError:
  56.                     continue
  57.  
  58.                 if i - match <= 2047:
  59.                     break
  60.                 
  61.                 match = -1
  62.             
  63.             if match >= 0:
  64.                 n = len(chunk)
  65.                 m = i - match
  66.                 code = 32768 + (m << 3 & 16376) + (n - 3)
  67.                 out.write(pack('>H', code))
  68.                 i += n
  69.                 continue
  70.             
  71.         
  72.         ch = data[i]
  73.         och = ord(ch)
  74.         i += 1
  75.         if ch == ' ' and i + 1 < ldata:
  76.             onch = ord(data[i])
  77.             if onch >= 64 and onch < 128:
  78.                 out.write(pack('>B', onch ^ 128))
  79.                 i += 1
  80.                 continue
  81.             
  82.         
  83.         if (och == 0 or och > 8) and och < 128:
  84.             out.write(ch)
  85.             continue
  86.         j = i
  87.         binseq = [
  88.             ch]
  89.         while j < ldata and len(binseq) < 8:
  90.             ch = data[j]
  91.             och = ord(ch)
  92.             if (och == 0 or och > 8) and och < 128:
  93.                 break
  94.             
  95.             binseq.append(ch)
  96.             j += 1
  97.         out.write(pack('>B', len(binseq)))
  98.         out.write(''.join(binseq))
  99.         i += len(binseq) - 1
  100.     return out.getvalue()
  101.  
  102.