home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __license__ = 'GPL v3'
- __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
- from cStringIO import StringIO
- from struct import pack
- from calibre.constants import plugins
- cPalmdoc = plugins['cPalmdoc'][0]
- if not cPalmdoc:
- raise RuntimeError('Failed to load required cPalmdoc module: %s' % plugins['cPalmdoc'][1])
- cPalmdoc
-
- def decompress_doc(data):
- return cPalmdoc.decompress(data)
-
-
- def compress_doc(data):
- return cPalmdoc.compress(data)
-
-
- def test():
- TESTS = [
- 'abc\x03\x04\x05\x06ms',
- 'a b c \xfed ',
- '0123456789axyz2bxyz2cdfgfo9iuyerh',
- '0123456789asd0123456789asd|yyzzxxffhhjjkk',
- 'ciewacnaq eiu743 r787q 0w% ; sa fd\xef\x0cfdxosac wocjp acoiecowei owaic jociowapjcivcjpoivjporeivjpoavca; p9aw8743y6r74%$^$^%8 ']
- for test in TESTS:
- print 'Test:', repr(test)
- print '\tTesting compression...'
- good = py_compress_doc(test)
- x = compress_doc(test)
- print '\t\tgood:', repr(good)
- print '\t\tx :', repr(x)
- print '\tTesting decompression...'
- print '\t\t', repr(decompress_doc(x))
- print
-
-
-
- def py_compress_doc(data):
- out = StringIO()
- i = 0
- ldata = len(data)
- while i < ldata:
- if i > 10 and ldata - i > 10:
- chunk = ''
- match = -1
- for j in xrange(10, 2, -1):
- chunk = data[i:i + j]
-
- try:
- match = data.rindex(chunk, 0, i)
- except ValueError:
- continue
-
- if i - match <= 2047:
- break
-
- match = -1
-
- if match >= 0:
- n = len(chunk)
- m = i - match
- code = 32768 + (m << 3 & 16376) + (n - 3)
- out.write(pack('>H', code))
- i += n
- continue
-
-
- ch = data[i]
- och = ord(ch)
- i += 1
- if ch == ' ' and i + 1 < ldata:
- onch = ord(data[i])
- if onch >= 64 and onch < 128:
- out.write(pack('>B', onch ^ 128))
- i += 1
- continue
-
-
- if (och == 0 or och > 8) and och < 128:
- out.write(ch)
- continue
- j = i
- binseq = [
- ch]
- while j < ldata and len(binseq) < 8:
- ch = data[j]
- och = ord(ch)
- if (och == 0 or och > 8) and och < 128:
- break
-
- binseq.append(ch)
- j += 1
- out.write(pack('>B', len(binseq)))
- out.write(''.join(binseq))
- i += len(binseq) - 1
- return out.getvalue()
-
-