Advertisement
Guest User

Untitled

a guest
Feb 4th, 2010
1,605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 27.20 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
  3. #
  4. # erdr2pml.py
  5. #
  6. # This is a python script. You need a Python interpreter to run it.
  7. # For example, ActiveState Python, which exists for windows.
  8. # Changelog
  9. #
  10. #  Based on ereader2html version 0.08 plus some later small fixes
  11. #
  12. #  0.01 - Initial version
  13. #  0.02 - Support more eReader files. Support bold text and links. Fix PML decoder parsing bug.
  14. #  0.03 - Fix incorrect variable usage at one place.
  15. #  0.03b - enhancement by DeBockle (version 259 support)
  16. # Custom version 0.03 - no change to eReader support, only usability changes
  17. #   - start of pep-8 indentation (spaces not tab), fix trailing blanks
  18. #   - version variable, only one place to change
  19. #   - added main routine, now callable as a library/module,
  20. #     means tools can add optional support for ereader2html
  21. #   - outdir is no longer a mandatory parameter (defaults based on input name if missing)
  22. #   - time taken output to stdout
  23. #   - Psyco support - reduces runtime by a factor of (over) 3!
  24. #     E.g. (~600Kb file) 90 secs down to 24 secs
  25. #       - newstyle classes
  26. #       - changed map call to list comprehension
  27. #         may not work with python 2.3
  28. #         without Psyco this reduces runtime to 90%
  29. #         E.g. 90 secs down to 77 secs
  30. #         Psyco with map calls takes longer, do not run with map in Psyco JIT!
  31. #       - izip calls used instead of zip (if available), further reduction
  32. #         in run time (factor of 4.5).
  33. #         E.g. (~600Kb file) 90 secs down to 20 secs
  34. #   - Python 2.6+ support, avoid DeprecationWarning with sha/sha1
  35. #  0.04 - Footnote support, PML output, correct charset in html, support more PML tags
  36. #   - Feature change, dump out PML file
  37. #   - Added supprt for footnote tags. NOTE footnote ids appear to be bad (not usable)
  38. #       in some pdb files :-( due to the same id being used multiple times
  39. #   - Added correct charset encoding (pml is based on cp1252)
  40. #   - Added logging support.
  41. #  0.05 - Improved type 272 support for sidebars, links, chapters, metainfo, etc
  42. #  0.06 - Merge of 0.04 and 0.05. Improved HTML output
  43. #         Placed images in subfolder, so that it's possible to just
  44. #         drop the book.pml file onto DropBook to make an unencrypted
  45. #         copy of the eReader file.
  46. #         Using that with Calibre works a lot better than the HTML
  47. #         conversion in this code.
  48. #  0.07 - Further Improved type 272 support for sidebars with all earlier fixes
  49. #  0.08 - fixed typos, removed extraneous things
  50. #  0.09 - fixed typos in first_pages to first_page to again support older formats
  51. #  0.10 - minor cleanups
  52. #  0.11 - fixups for using correct xml for footnotes and sidebars for use with Dropbook
  53. #  0.12 - Fix added to prevent lowercasing of image names when the pml code itself uses a different case in the link name.
  54. #  0.13 - change to unbuffered stdout for use with gui front ends
  55.  
  56. __version__='0.13'
  57.  
  58. # Import Psyco if available
  59. try:
  60.     # Dumb speed hack 1
  61.     # http://psyco.sourceforge.net
  62.     import psyco
  63.     psyco.full()
  64.     pass
  65. except ImportError:
  66.     pass
  67. try:
  68.     # Dumb speed hack 2
  69.     # All map() calls converted to list comprehension (some use zip)
  70.     # override zip with izip - saves memory and in rough testing
  71.     # appears to be faster zip() is only used in the converted map() calls
  72.     from itertools import izip as zip
  73. except ImportError:
  74.     pass
  75.  
  76. class Unbuffered:
  77.     def __init__(self, stream):
  78.         self.stream = stream
  79.     def write(self, data):
  80.         self.stream.write(data)
  81.         self.stream.flush()
  82.     def __getattr__(self, attr):
  83.         return getattr(self.stream, attr)
  84.  
  85. import sys
  86. sys.stdout=Unbuffered(sys.stdout)
  87.  
  88. import struct, binascii, zlib, os, os.path, urllib
  89.  
  90. try:
  91.     from hashlib import sha1
  92. except ImportError:
  93.     # older Python release
  94.     import sha
  95.     sha1 = lambda s: sha.new(s)
  96. import cgi
  97. import logging
  98.  
  99. logging.basicConfig()
  100. #logging.basicConfig(level=logging.DEBUG)
  101.  
  102. ECB =   0
  103. CBC =   1
  104. class Des(object):
  105.     __pc1 = [56, 48, 40, 32, 24, 16,  8,  0, 57, 49, 41, 33, 25, 17,
  106.           9,  1, 58, 50, 42, 34, 26, 18, 10,  2, 59, 51, 43, 35,
  107.          62, 54, 46, 38, 30, 22, 14,  6, 61, 53, 45, 37, 29, 21,
  108.          13,  5, 60, 52, 44, 36, 28, 20, 12,  4, 27, 19, 11,  3]
  109.     __left_rotations = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]
  110.     __pc2 = [13, 16, 10, 23,  0,  4,2, 27, 14,  5, 20,  9,
  111.         22, 18, 11,  3, 25,  7, 15,  6, 26, 19, 12,  1,
  112.         40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
  113.         43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31]
  114.     __ip = [57, 49, 41, 33, 25, 17, 9,  1,  59, 51, 43, 35, 27, 19, 11, 3,
  115.         61, 53, 45, 37, 29, 21, 13, 5,  63, 55, 47, 39, 31, 23, 15, 7,
  116.         56, 48, 40, 32, 24, 16, 8,  0,  58, 50, 42, 34, 26, 18, 10, 2,
  117.         60, 52, 44, 36, 28, 20, 12, 4,  62, 54, 46, 38, 30, 22, 14, 6]
  118.     __expansion_table = [31,  0,  1,  2,  3,  4, 3,  4,  5,  6,  7,  8,
  119.          7,  8,  9, 10, 11, 12,11, 12, 13, 14, 15, 16,
  120.         15, 16, 17, 18, 19, 20,19, 20, 21, 22, 23, 24,
  121.         23, 24, 25, 26, 27, 28,27, 28, 29, 30, 31,  0]
  122.     __sbox = [[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
  123.          0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
  124.          4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
  125.          15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13],
  126.         [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
  127.          3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
  128.          0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
  129.          13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9],
  130.         [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
  131.          13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
  132.          13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
  133.          1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12],
  134.         [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
  135.          13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
  136.          10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
  137.          3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14],
  138.         [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
  139.          14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
  140.          4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
  141.          11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3],
  142.         [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
  143.          10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
  144.          9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
  145.          4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13],
  146.         [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
  147.          13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
  148.          1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
  149.          6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12],
  150.         [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
  151.          1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
  152.          7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
  153.          2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11],]
  154.     __p = [15, 6, 19, 20, 28, 11,27, 16, 0, 14, 22, 25,
  155.         4, 17, 30, 9, 1, 7,23,13, 31, 26, 2, 8,18, 12, 29, 5, 21, 10,3, 24]
  156.     __fp = [39,  7, 47, 15, 55, 23, 63, 31,38,  6, 46, 14, 54, 22, 62, 30,
  157.         37,  5, 45, 13, 53, 21, 61, 29,36,  4, 44, 12, 52, 20, 60, 28,
  158.         35,  3, 43, 11, 51, 19, 59, 27,34,  2, 42, 10, 50, 18, 58, 26,
  159.         33,  1, 41,  9, 49, 17, 57, 25,32,  0, 40,  8, 48, 16, 56, 24]
  160.     # Type of crypting being done
  161.     ENCRYPT =   0x00
  162.     DECRYPT =   0x01
  163.     def __init__(self, key, mode=ECB, IV=None):
  164.         if len(key) != 8:
  165.             raise ValueError("Invalid DES key size. Key must be exactly 8 bytes long.")
  166.         self.block_size = 8
  167.         self.key_size = 8
  168.         self.__padding = ''
  169.         self.setMode(mode)
  170.         if IV:
  171.             self.setIV(IV)
  172.         self.L = []
  173.         self.R = []
  174.         self.Kn = [ [0] * 48 ] * 16 # 16 48-bit keys (K1 - K16)
  175.         self.final = []
  176.         self.setKey(key)
  177.     def getKey(self):
  178.         return self.__key
  179.     def setKey(self, key):
  180.         self.__key = key
  181.         self.__create_sub_keys()
  182.     def getMode(self):
  183.         return self.__mode
  184.     def setMode(self, mode):
  185.         self.__mode = mode
  186.     def getIV(self):
  187.         return self.__iv
  188.     def setIV(self, IV):
  189.         if not IV or len(IV) != self.block_size:
  190.             raise ValueError("Invalid Initial Value (IV), must be a multiple of " + str(self.block_size) + " bytes")
  191.         self.__iv = IV
  192.     def getPadding(self):
  193.         return self.__padding
  194.     def __String_to_BitList(self, data):
  195.         l = len(data) * 8
  196.         result = [0] * l
  197.         pos = 0
  198.         for c in data:
  199.             i = 7
  200.             ch = ord(c)
  201.             while i >= 0:
  202.                 if ch & (1 << i) != 0:
  203.                     result[pos] = 1
  204.                 else:
  205.                     result[pos] = 0
  206.                 pos += 1
  207.                 i -= 1
  208.         return result
  209.     def __BitList_to_String(self, data):
  210.         result = ''
  211.         pos = 0
  212.         c = 0
  213.         while pos < len(data):
  214.             c += data[pos] << (7 - (pos % 8))
  215.             if (pos % 8) == 7:
  216.                 result += chr(c)
  217.                 c = 0
  218.             pos += 1
  219.         return result
  220.     def __permutate(self, table, block):
  221.         return [block[x] for x in table]
  222.     def __create_sub_keys(self):
  223.         key = self.__permutate(Des.__pc1, self.__String_to_BitList(self.getKey()))
  224.         i = 0
  225.         self.L = key[:28]
  226.         self.R = key[28:]
  227.         while i < 16:
  228.             j = 0
  229.             while j < Des.__left_rotations[i]:
  230.                 self.L.append(self.L[0])
  231.                 del self.L[0]
  232.                 self.R.append(self.R[0])
  233.                 del self.R[0]
  234.                 j += 1
  235.             self.Kn[i] = self.__permutate(Des.__pc2, self.L + self.R)
  236.             i += 1
  237.     def __des_crypt(self, block, crypt_type):
  238.         block = self.__permutate(Des.__ip, block)
  239.         self.L = block[:32]
  240.         self.R = block[32:]
  241.         if crypt_type == Des.ENCRYPT:
  242.             iteration = 0
  243.             iteration_adjustment = 1
  244.         else:
  245.             iteration = 15
  246.             iteration_adjustment = -1
  247.         i = 0
  248.         while i < 16:
  249.             tempR = self.R[:]
  250.             self.R = self.__permutate(Des.__expansion_table, self.R)
  251.             self.R = [x ^ y for x,y in zip(self.R, self.Kn[iteration])]
  252.             B = [self.R[:6], self.R[6:12], self.R[12:18], self.R[18:24], self.R[24:30], self.R[30:36], self.R[36:42], self.R[42:]]
  253.             j = 0
  254.             Bn = [0] * 32
  255.             pos = 0
  256.             while j < 8:
  257.                 m = (B[j][0] << 1) + B[j][5]
  258.                 n = (B[j][1] << 3) + (B[j][2] << 2) + (B[j][3] << 1) + B[j][4]
  259.                 v = Des.__sbox[j][(m << 4) + n]
  260.                 Bn[pos] = (v & 8) >> 3
  261.                 Bn[pos + 1] = (v & 4) >> 2
  262.                 Bn[pos + 2] = (v & 2) >> 1
  263.                 Bn[pos + 3] = v & 1
  264.                 pos += 4
  265.                 j += 1
  266.             self.R = self.__permutate(Des.__p, Bn)
  267.             self.R = [x ^ y for x, y in zip(self.R, self.L)]
  268.             self.L = tempR
  269.             i += 1
  270.             iteration += iteration_adjustment
  271.         self.final = self.__permutate(Des.__fp, self.R + self.L)
  272.         return self.final
  273.     def crypt(self, data, crypt_type):
  274.         if not data:
  275.             return ''
  276.         if len(data) % self.block_size != 0:
  277.             if crypt_type == Des.DECRYPT: # Decryption must work on 8 byte blocks
  278.                 raise ValueError("Invalid data length, data must be a multiple of " + str(self.block_size) + " bytes\n.")
  279.             if not self.getPadding():
  280.                 raise ValueError("Invalid data length, data must be a multiple of " + str(self.block_size) + " bytes\n. Try setting the optional padding character")
  281.             else:
  282.                 data += (self.block_size - (len(data) % self.block_size)) * self.getPadding()
  283.         if self.getMode() == CBC:
  284.             if self.getIV():
  285.                 iv = self.__String_to_BitList(self.getIV())
  286.             else:
  287.                 raise ValueError("For CBC mode, you must supply the Initial Value (IV) for ciphering")
  288.         i = 0
  289.         dict = {}
  290.         result = []
  291.         while i < len(data):
  292.             block = self.__String_to_BitList(data[i:i+8])
  293.             if self.getMode() == CBC:
  294.                 if crypt_type == Des.ENCRYPT:
  295.                     block = [x ^ y for x, y in zip(block, iv)]
  296.                 processed_block = self.__des_crypt(block, crypt_type)
  297.                 if crypt_type == Des.DECRYPT:
  298.                     processed_block = [x ^ y for x, y in zip(processed_block, iv)]
  299.                     iv = block
  300.                 else:
  301.                     iv = processed_block
  302.             else:
  303.                 processed_block = self.__des_crypt(block, crypt_type)
  304.             result.append(self.__BitList_to_String(processed_block))
  305.             i += 8
  306.         if crypt_type == Des.DECRYPT and self.getPadding():
  307.             s = result[-1]
  308.             while s[-1] == self.getPadding():
  309.                 s = s[:-1]
  310.             result[-1] = s
  311.         return ''.join(result)
  312.     def encrypt(self, data, pad=''):
  313.         self.__padding = pad
  314.         return self.crypt(data, Des.ENCRYPT)
  315.     def decrypt(self, data, pad=''):
  316.         self.__padding = pad
  317.         return self.crypt(data, Des.DECRYPT)
  318.  
  319. class Sectionizer(object):
  320.     def __init__(self, filename, ident):
  321.         self.contents = file(filename, 'rb').read()
  322.         self.header = self.contents[0:72]
  323.         self.num_sections, = struct.unpack('>H', self.contents[76:78])
  324.         if self.header[0x3C:0x3C+8] != ident:
  325.             raise ValueError('Invalid file format')
  326.         self.sections = []
  327.         for i in xrange(self.num_sections):
  328.             offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', self.contents[78+i*8:78+i*8+8])
  329.             flags, val = a1, a2<<16|a3<<8|a4
  330.             self.sections.append( (offset, flags, val) )
  331.     def loadSection(self, section):
  332.         if section + 1 == self.num_sections:
  333.             end_off = len(self.contents)
  334.         else:
  335.             end_off = self.sections[section + 1][0]
  336.         off = self.sections[section][0]
  337.         return self.contents[off:end_off]
  338.  
  339. def sanitizeFileName(s):
  340.     r = ''
  341.     for c in s:
  342.         if c in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-":
  343.             r += c
  344.     return r
  345.  
  346. def fixKey(key):
  347.     def fixByte(b):
  348.         return b ^ ((b ^ (b<<1) ^ (b<<2) ^ (b<<3) ^ (b<<4) ^ (b<<5) ^ (b<<6) ^ (b<<7) ^ 0x80) & 0x80)
  349.     return  "".join([chr(fixByte(ord(a))) for a in key])
  350.  
  351. def deXOR(text, sp, table):
  352.     r=''
  353.     j = sp
  354.     for i in xrange(len(text)):
  355.         r += chr(ord(table[j]) ^ ord(text[i]))
  356.         j = j + 1
  357.         if j == len(table):
  358.             j = 0
  359.     return r
  360.  
  361. class EreaderProcessor(object):
  362.     def __init__(self, section_reader, username, creditcard):
  363.         self.section_reader = section_reader
  364.         data = section_reader(0)
  365.         version,  = struct.unpack('>H', data[0:2])
  366.         self.version = version
  367.         logging.info('eReader file format version %s', version)
  368.         if version != 272 and version != 260 and version != 259:
  369.             raise ValueError('incorrect eReader version %d (error 1)' % version)
  370.         data = section_reader(1)
  371.         self.data = data
  372.         des = Des(fixKey(data[0:8]))
  373.         cookie_shuf, cookie_size = struct.unpack('>LL', des.decrypt(data[-8:]))
  374.         if cookie_shuf < 3 or cookie_shuf > 0x14 or cookie_size < 0xf0 or cookie_size > 0x200:
  375.             raise ValueError('incorrect eReader version (error 2)')
  376.         input = des.decrypt(data[-cookie_size:])
  377.         def unshuff(data, shuf):
  378.             r = [''] * len(data)
  379.             j = 0
  380.             for i in xrange(len(data)):
  381.                 j = (j + shuf) % len(data)
  382.                 r[j] = data[i]
  383.             assert  len("".join(r)) == len(data)
  384.             return "".join(r)
  385.         r = unshuff(input[0:-8], cookie_shuf)
  386.  
  387.         def fixUsername(s):
  388.             r = ''
  389.             for c in s.lower():
  390.                 if (c >= 'a' and c <= 'z' or c >= '0' and c <= '9'):
  391.                     r += c
  392.             return r
  393.  
  394.         user_key = struct.pack('>LL', binascii.crc32(fixUsername(username)) & 0xffffffff, binascii.crc32(creditcard[-8:])& 0xffffffff)
  395.         drm_sub_version = struct.unpack('>H', r[0:2])[0]
  396.         self.num_text_pages = struct.unpack('>H', r[2:4])[0] - 1
  397.         self.num_image_pages = struct.unpack('>H', r[26:26+2])[0]
  398.         self.first_image_page = struct.unpack('>H', r[24:24+2])[0]
  399.         if self.version == 272:
  400.             self.num_footnote_pages = struct.unpack('>H', r[46:46+2])[0]
  401.             self.first_footnote_page = struct.unpack('>H', r[44:44+2])[0]
  402.             self.num_sidebar_pages = struct.unpack('>H', r[38:38+2])[0]
  403.             self.first_sidebar_page = struct.unpack('>H', r[36:36+2])[0]
  404.             # self.num_bookinfo_pages = struct.unpack('>H', r[34:34+2])[0]
  405.             # self.first_bookinfo_page = struct.unpack('>H', r[32:32+2])[0]
  406.             # self.num_chapter_pages = struct.unpack('>H', r[22:22+2])[0]
  407.             # self.first_chapter_page = struct.unpack('>H', r[20:20+2])[0]
  408.             # self.num_link_pages = struct.unpack('>H', r[30:30+2])[0]
  409.             # self.first_link_page = struct.unpack('>H', r[28:28+2])[0]
  410.             # self.num_xtextsize_pages = struct.unpack('>H', r[54:54+2])[0]
  411.             # self.first_xtextsize_page = struct.unpack('>H', r[52:52+2])[0]
  412.  
  413.             # **before** data record 1 was decrypted and unshuffled, it contained data
  414.             # to create an XOR table and which is used to fix footnote record 0, link records, chapter records, etc
  415.             self.xortable_offset  = struct.unpack('>H', r[40:40+2])[0]
  416.             self.xortable_size = struct.unpack('>H', r[42:42+2])[0]
  417.             self.xortable = self.data[self.xortable_offset:self.xortable_offset + self.xortable_size]
  418.         else:
  419.             self.num_footnote_pages = 0
  420.             self.num_sidebar_pages = 0
  421.             self.first_footnote_page = -1
  422.             self.first_sidebar_page = -1
  423.             # self.num_bookinfo_pages = 0
  424.             # self.num_chapter_pages = 0
  425.             # self.num_link_pages = 0
  426.             # self.num_xtextsize_pages = 0
  427.             # self.first_bookinfo_page = -1
  428.             # self.first_chapter_page = -1
  429.             # self.first_link_page = -1
  430.             # self.first_xtextsize_page = -1
  431.  
  432.         logging.debug('self.num_text_pages %d', self.num_text_pages)
  433.         logging.debug('self.num_footnote_pages %d, self.first_footnote_page %d', self.num_footnote_pages , self.first_footnote_page)
  434.         logging.debug('self.num_sidebar_pages %d, self.first_sidebar_page %d', self.num_sidebar_pages , self.first_sidebar_page)
  435.         self.flags = struct.unpack('>L', r[4:8])[0]
  436.         reqd_flags = (1<<9) | (1<<7) | (1<<10)
  437.         if (self.flags & reqd_flags) != reqd_flags:
  438.             print "Flags: 0x%X" % self.flags
  439.             raise ValueError('incompatible eReader file')
  440.         des = Des(fixKey(user_key))
  441.         if version == 259:
  442.             if drm_sub_version != 7:
  443.                 raise ValueError('incorrect eReader version %d (error 3)' % drm_sub_version)
  444.             encrypted_key_sha = r[44:44+20]
  445.             encrypted_key = r[64:64+8]
  446.         elif version == 260:
  447.             if drm_sub_version != 13:
  448.                 raise ValueError('incorrect eReader version %d (error 3)' % drm_sub_version)
  449.             encrypted_key = r[44:44+8]
  450.             encrypted_key_sha = r[52:52+20]
  451.         elif version == 272:
  452.             encrypted_key = r[172:172+8]
  453.             encrypted_key_sha = r[56:56+20]
  454.         self.content_key = des.decrypt(encrypted_key)
  455.         if sha1(self.content_key).digest() != encrypted_key_sha:
  456.             raise ValueError('Incorrect Name and/or Credit Card')
  457.  
  458.     def getNumImages(self):
  459.         return self.num_image_pages
  460.  
  461.     def getImage(self, i):
  462.         sect = self.section_reader(self.first_image_page + i)
  463.         name = sect[4:4+32].strip('\0')
  464.         data = sect[62:]
  465.         return sanitizeFileName(name), data
  466.  
  467.     def cleanPML(self,pml):
  468.         # Update old \b font tag with correct \B bold font tag
  469.         pml2 = pml.replace('\\b', '\\B')
  470.         # Convert special characters to proper PML code.  High ASCII start at (\x82, \a130) and go up to (\xff, \a255)
  471.         for k in xrange(130,256):
  472.             # a2b_hex takes in a hexidecimal as a string and converts it
  473.             # to a binary ascii code that we search and replace for
  474.             badChar=binascii.a2b_hex('%02x' % k)
  475.             pml2 = pml2.replace(badChar, '\\a%03d' % k)
  476.             #end for k
  477.         return pml2
  478.  
  479.     # def getChapterNamePMLOffsetData(self):
  480.     #     cv = ''
  481.     #     if self.num_chapter_pages > 0:
  482.     #         for i in xrange(self.num_chapter_pages):
  483.     #             chaps = self.section_reader(self.first_chapter_page + i)
  484.     #             j = i % self.xortable_size
  485.     #             offname = deXOR(chaps, j, self.xortable)
  486.     #             offset = struct.unpack('>L', offname[0:4])[0]
  487.     #             name = offname[4:].strip('\0')
  488.     #             cv += '%d|%s\n' % (offset, name)
  489.     #     return cv
  490.  
  491.     # def getLinkNamePMLOffsetData(self):
  492.     #     lv = ''
  493.     #     if self.num_link_pages > 0:
  494.     #         for i in xrange(self.num_link_pages):
  495.     #             links = self.section_reader(self.first_link_page + i)
  496.     #             j = i % self.xortable_size
  497.     #             offname = deXOR(links, j, self.xortable)
  498.     #             offset = struct.unpack('>L', offname[0:4])[0]
  499.     #             name = offname[4:].strip('\0')
  500.     #             lv += '%d|%s\n' % (offset, name)
  501.     #     return lv
  502.  
  503.     # def getExpandedTextSizesData(self):
  504.     #      ts = ''
  505.     #      if self.num_xtextsize_pages > 0:
  506.     #          tsize = deXOR(self.section_reader(self.first_xtextsize_page), 0, self.xortable)
  507.     #          for i in xrange(self.num_text_pages):
  508.     #              xsize = struct.unpack('>H', tsize[0:2])[0]
  509.     #              ts += "%d\n" % xsize
  510.     #              tsize = tsize[2:]
  511.     #      return ts
  512.  
  513.     # def getBookInfo(self):
  514.     #     bkinfo = ''
  515.     #     if self.num_bookinfo_pages > 0:
  516.     #         info = self.section_reader(self.first_bookinfo_page)
  517.     #         bkinfo = deXOR(info, 0, self.xortable)
  518.     #         bkinfo = bkinfo.replace('\0','|')
  519.     #         bkinfo += '\n'
  520.     #     return bkinfo
  521.  
  522.     def getText(self):
  523.         des = Des(fixKey(self.content_key))
  524.         r = ''
  525.         for i in xrange(self.num_text_pages):
  526.             logging.debug('get page %d', i)
  527.             r += zlib.decompress(des.decrypt(self.section_reader(1 + i)))
  528.              
  529.         # now handle footnotes pages
  530.         if self.num_footnote_pages > 0:
  531.             r += '\n'
  532.             # the record 0 of the footnote section must pass through the Xor Table to make it useful
  533.             sect = self.section_reader(self.first_footnote_page)
  534.             fnote_ids = deXOR(sect, 0, self.xortable)
  535.             # the remaining records of the footnote sections need to be decoded with the content_key and zlib inflated
  536.             des = Des(fixKey(self.content_key))
  537.             for i in xrange(1,self.num_footnote_pages):
  538.                 logging.debug('get footnotepage %d', i)
  539.                 id_len = ord(fnote_ids[2])
  540.                 id = fnote_ids[3:3+id_len]
  541.                 fmarker = '<footnote id="%s">\n' % id
  542.                 fmarker += zlib.decompress(des.decrypt(self.section_reader(self.first_footnote_page + i)))
  543.                 fmarker += '\n</footnote>\n'
  544.                 r += fmarker
  545.                 fnote_ids = fnote_ids[id_len+4:]
  546.  
  547.         # now handle sidebar pages
  548.         if self.num_sidebar_pages > 0:
  549.             r += '\n'
  550.             # the record 0 of the sidebar section must pass through the Xor Table to make it useful
  551.             sect = self.section_reader(self.first_sidebar_page)
  552.             sbar_ids = deXOR(sect, 0, self.xortable)
  553.             # the remaining records of the sidebar sections need to be decoded with the content_key and zlib inflated
  554.             des = Des(fixKey(self.content_key))
  555.             for i in xrange(1,self.num_sidebar_pages):
  556.                 id_len = ord(sbar_ids[2])
  557.                 id = sbar_ids[3:3+id_len]
  558.                 smarker = '<sidebar id="%s">\n' % id
  559.                 smarker += zlib.decompress(des.decrypt(self.section_reader(self.first_footnote_page + i)))
  560.                 smarker += '\n</sidebar>\n'
  561.                 r += smarker
  562.                 sbar_ids = sbar_ids[id_len+4:]
  563.  
  564.         return r
  565.  
  566. def convertEreaderToPml(infile, name, cc, outdir):
  567.     if not os.path.exists(outdir):
  568.         os.makedirs(outdir)
  569.  
  570.     print "   Decoding File"
  571.     sect = Sectionizer(infile, 'PNRdPPrs')
  572.     er = EreaderProcessor(sect.loadSection, name, cc)
  573.  
  574.     if er.getNumImages() > 0:
  575.         print "   Extracting images"
  576.         imagedir = bookname + '_img/'
  577.         imagedirpath = os.path.join(outdir,imagedir)
  578.         if not os.path.exists(imagedirpath):
  579.             os.makedirs(imagedirpath)
  580.         for i in xrange(er.getNumImages()):
  581.             name, contents = er.getImage(i)
  582.             file(os.path.join(imagedirpath, name), 'wb').write(contents)
  583.  
  584.     print "   Extracting pml"
  585.     pml_string = er.getText()
  586.     pmlfilename = bookname + ".pml"
  587.     file(os.path.join(outdir, pmlfilename),'wb').write(pml_string)
  588.  
  589.     # bkinfo = er.getBookInfo()
  590.     # if bkinfo != '':
  591.     #     print "   Extracting book meta information"
  592.     #     file(os.path.join(outdir, 'bookinfo.txt'),'wb').write(bkinfo)
  593.  
  594.  
  595. def main(argv=None):
  596.     global bookname
  597.     if argv is None:
  598.         argv = sys.argv
  599.    
  600.     print "eRdr2Pml v%s. Copyright (c) 2009 The Dark Reverser" % __version__
  601.  
  602.     if len(argv)!=4 and len(argv)!=5:
  603.         print "Converts DRMed eReader books to PML Source"
  604.         print "Usage:"
  605.         print "  erdr2pml infile.pdb [outdir] \"your name\" credit_card_number "
  606.         print "Note:"
  607.         print "  if ommitted, outdir defaults based on 'infile.pdb'"
  608.         print "  It's enough to enter the last 8 digits of the credit card number"
  609.         return 1
  610.     else:
  611.         if len(argv)==4:
  612.             infile, name, cc = argv[1], argv[2], argv[3]
  613.             outdir = infile[:-4] + '_Source'
  614.         elif len(argv)==5:
  615.             infile, outdir, name, cc = argv[1], argv[2], argv[3], argv[4]
  616.         bookname = os.path.splitext(os.path.basename(infile))[0]
  617.  
  618.         try:
  619.             print "Processing..."
  620.             import time
  621.             start_time = time.time()
  622.             convertEreaderToPml(infile, name, cc, outdir)
  623.             end_time = time.time()
  624.             search_time = end_time - start_time
  625.             print 'elapsed time: %.2f seconds' % (search_time, )
  626.             print 'output in %s' % outdir
  627.             print "done"
  628.         except ValueError, e:
  629.             print "Error: %s" % e
  630.             return 1
  631.     return 0
  632.  
  633. if __name__ == "__main__":
  634.     #import cProfile
  635.     #command = """sys.exit(main())"""
  636.     #cProfile.runctx( command, globals(), locals(), filename="cprofile.profile" )
  637.    
  638.     sys.exit(main())
  639.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement