Guest User

Simon

a guest
Mar 20th, 2008
3,140
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. # This is a python script. You need a Python interpreter to run it.
  4. # For example, ActiveState Python, which exists for windows.
  5. # Changelog
  6. #  0.01 - Initial version
  7. #  0.02 - Support more eReader files. Support bold text and links. Fix PML decoder parsing bug.
  8. #  0.03 - Fix incorrect variable usage at one place.
  9.  
  10. import struct, binascii, zlib, os, sha, sys, os.path
  11.  
  12. ECB =   0
  13. CBC =   1
  14. class Des:
  15.     __pc1 = [56, 48, 40, 32, 24, 16,  8,  0, 57, 49, 41, 33, 25, 17,
  16.           9,  1, 58, 50, 42, 34, 26, 18, 10,  2, 59, 51, 43, 35,
  17.          62, 54, 46, 38, 30, 22, 14,  6, 61, 53, 45, 37, 29, 21,
  18.          13,  5, 60, 52, 44, 36, 28, 20, 12,  4, 27, 19, 11,  3]
  19.     __left_rotations = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]
  20.     __pc2 = [13, 16, 10, 23,  0,  4,2, 27, 14,  5, 20,  9,
  21.         22, 18, 11,  3, 25,  7, 15,  6, 26, 19, 12,  1,
  22.         40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
  23.         43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31]
  24.     __ip = [57, 49, 41, 33, 25, 17, 9,  1,  59, 51, 43, 35, 27, 19, 11, 3,
  25.         61, 53, 45, 37, 29, 21, 13, 5,  63, 55, 47, 39, 31, 23, 15, 7,
  26.         56, 48, 40, 32, 24, 16, 8,  0,  58, 50, 42, 34, 26, 18, 10, 2,
  27.         60, 52, 44, 36, 28, 20, 12, 4,  62, 54, 46, 38, 30, 22, 14, 6]
  28.     __expansion_table = [31,  0,  1,  2,  3,  4, 3,  4,  5,  6,  7,  8,
  29.          7,  8,  9, 10, 11, 12,11, 12, 13, 14, 15, 16,
  30.         15, 16, 17, 18, 19, 20,19, 20, 21, 22, 23, 24,
  31.         23, 24, 25, 26, 27, 28,27, 28, 29, 30, 31,  0]
  32.     __sbox = [[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
  33.          0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
  34.          4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
  35.          15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13],
  36.         [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
  37.          3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
  38.          0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
  39.          13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9],
  40.         [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
  41.          13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
  42.          13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
  43.          1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12],
  44.         [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
  45.          13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
  46.          10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
  47.          3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14],
  48.         [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
  49.          14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
  50.          4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
  51.          11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3],
  52.         [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
  53.          10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
  54.          9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
  55.          4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13],
  56.         [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
  57.          13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
  58.          1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
  59.          6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12],
  60.         [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
  61.          1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
  62.          7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
  63.          2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11],]
  64.     __p = [15, 6, 19, 20, 28, 11,27, 16, 0, 14, 22, 25,
  65.         4, 17, 30, 9, 1, 7,23,13, 31, 26, 2, 8,18, 12, 29, 5, 21, 10,3, 24]
  66.     __fp = [39,  7, 47, 15, 55, 23, 63, 31,38,  6, 46, 14, 54, 22, 62, 30,
  67.         37,  5, 45, 13, 53, 21, 61, 29,36,  4, 44, 12, 52, 20, 60, 28,
  68.         35,  3, 43, 11, 51, 19, 59, 27,34,  2, 42, 10, 50, 18, 58, 26,
  69.         33,  1, 41,  9, 49, 17, 57, 25,32,  0, 40,  8, 48, 16, 56, 24]
  70.     # Type of crypting being done
  71.     ENCRYPT =   0x00
  72.     DECRYPT =   0x01
  73.     def __init__(self, key, mode=ECB, IV=None):
  74.         if len(key) != 8:
  75.             raise ValueError("Invalid DES key size. Key must be exactly 8 bytes long.")
  76.         self.block_size = 8
  77.         self.key_size = 8
  78.         self.__padding = ''
  79.         self.setMode(mode)
  80.         if IV:
  81.             self.setIV(IV)
  82.         self.L = []
  83.         self.R = []
  84.         self.Kn = [ [0] * 48 ] * 16 # 16 48-bit keys (K1 - K16)
  85.         self.final = []
  86.         self.setKey(key)
  87.     def getKey(self):
  88.         return self.__key
  89.     def setKey(self, key):
  90.         self.__key = key
  91.         self.__create_sub_keys()
  92.     def getMode(self):
  93.         return self.__mode
  94.     def setMode(self, mode):
  95.         self.__mode = mode
  96.     def getIV(self):
  97.         return self.__iv
  98.     def setIV(self, IV):
  99.         if not IV or len(IV) != self.block_size:
  100.             raise ValueError("Invalid Initial Value (IV), must be a multiple of " + str(self.block_size) + " bytes")
  101.         self.__iv = IV
  102.     def getPadding(self):
  103.         return self.__padding
  104.     def __String_to_BitList(self, data):
  105.         l = len(data) * 8
  106.         result = [0] * l
  107.         pos = 0
  108.         for c in data:
  109.             i = 7
  110.             ch = ord(c)
  111.             while i >= 0:
  112.                 if ch & (1 << i) != 0:
  113.                     result[pos] = 1
  114.                 else:
  115.                     result[pos] = 0
  116.                 pos += 1
  117.                 i -= 1
  118.         return result
  119.     def __BitList_to_String(self, data):
  120.         result = ''
  121.         pos = 0
  122.         c = 0
  123.         while pos < len(data):
  124.             c += data[pos] << (7 - (pos % 8))
  125.             if (pos % 8) == 7:
  126.                 result += chr(c)
  127.                 c = 0
  128.             pos += 1
  129.         return result
  130.     def __permutate(self, table, block):
  131.         return map(lambda x: block[x], table)
  132.     def __create_sub_keys(self):
  133.         key = self.__permutate(Des.__pc1, self.__String_to_BitList(self.getKey()))
  134.         i = 0
  135.         self.L = key[:28]
  136.         self.R = key[28:]
  137.         while i < 16:
  138.             j = 0
  139.             while j < Des.__left_rotations[i]:
  140.                 self.L.append(self.L[0])
  141.                 del self.L[0]
  142.                 self.R.append(self.R[0])
  143.                 del self.R[0]
  144.                 j += 1
  145.             self.Kn[i] = self.__permutate(Des.__pc2, self.L + self.R)
  146.             i += 1
  147.     def __des_crypt(self, block, crypt_type):
  148.         block = self.__permutate(Des.__ip, block)
  149.         self.L = block[:32]
  150.         self.R = block[32:]
  151.         if crypt_type == Des.ENCRYPT:
  152.             iteration = 0
  153.             iteration_adjustment = 1
  154.         else:
  155.             iteration = 15
  156.             iteration_adjustment = -1
  157.         i = 0
  158.         while i < 16:
  159.             tempR = self.R[:]
  160.             self.R = self.__permutate(Des.__expansion_table, self.R)
  161.             self.R = map(lambda x, y: x ^ y, self.R, self.Kn[iteration])
  162.             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:]]
  163.             j = 0
  164.             Bn = [0] * 32
  165.             pos = 0
  166.             while j < 8:
  167.                 m = (B[j][0] << 1) + B[j][5]
  168.                 n = (B[j][1] << 3) + (B[j][2] << 2) + (B[j][3] << 1) + B[j][4]
  169.                 v = Des.__sbox[j][(m << 4) + n]
  170.                 Bn[pos] = (v & 8) >> 3
  171.                 Bn[pos + 1] = (v & 4) >> 2
  172.                 Bn[pos + 2] = (v & 2) >> 1
  173.                 Bn[pos + 3] = v & 1
  174.                 pos += 4
  175.                 j += 1
  176.             self.R = self.__permutate(Des.__p, Bn)
  177.             self.R = map(lambda x, y: x ^ y, self.R, self.L)
  178.             self.L = tempR
  179.             i += 1
  180.             iteration += iteration_adjustment
  181.         self.final = self.__permutate(Des.__fp, self.R + self.L)
  182.         return self.final
  183.     def crypt(self, data, crypt_type):
  184.         if not data:
  185.             return ''
  186.         if len(data) % self.block_size != 0:
  187.             if crypt_type == Des.DECRYPT: # Decryption must work on 8 byte blocks
  188.                 raise ValueError("Invalid data length, data must be a multiple of " + str(self.block_size) + " bytes\n.")
  189.             if not self.getPadding():
  190.                 raise ValueError("Invalid data length, data must be a multiple of " + str(self.block_size) + " bytes\n. Try setting the optional padding character")
  191.             else:
  192.                 data += (self.block_size - (len(data) % self.block_size)) * self.getPadding()
  193.         if self.getMode() == CBC:
  194.             if self.getIV():
  195.                 iv = self.__String_to_BitList(self.getIV())
  196.             else:
  197.                 raise ValueError("For CBC mode, you must supply the Initial Value (IV) for ciphering")
  198.         i = 0
  199.         dict = {}
  200.         result = []
  201.         while i < len(data):
  202.             block = self.__String_to_BitList(data[i:i+8])
  203.             if self.getMode() == CBC:
  204.                 if crypt_type == Des.ENCRYPT:
  205.                     block = map(lambda x, y: x ^ y, block, iv)
  206.                 processed_block = self.__des_crypt(block, crypt_type)
  207.                 if crypt_type == Des.DECRYPT:
  208.                     processed_block = map(lambda x, y: x ^ y, processed_block, iv)
  209.                     iv = block
  210.                 else:
  211.                     iv = processed_block
  212.             else:
  213.                 processed_block = self.__des_crypt(block, crypt_type)
  214.             result.append(self.__BitList_to_String(processed_block))
  215.             i += 8
  216.         if crypt_type == Des.DECRYPT and self.getPadding():
  217.             s = result[-1]
  218.             while s[-1] == self.getPadding():
  219.                 s = s[:-1]
  220.             result[-1] = s
  221.         return ''.join(result)
  222.     def encrypt(self, data, pad=''):
  223.         self.__padding = pad
  224.         return self.crypt(data, Des.ENCRYPT)
  225.     def decrypt(self, data, pad=''):
  226.         self.__padding = pad
  227.         return self.crypt(data, Des.DECRYPT)
  228.  
  229. class Sectionizer:
  230.     def __init__(self, filename, ident):
  231.         self.contents = file(filename, 'rb').read()
  232.         self.header = self.contents[0:72]
  233.         self.num_sections, = struct.unpack('>H', self.contents[76:78])
  234.         if self.header[0x3C:0x3C+8] != ident:
  235.             raise ValueError('Invalid file format')
  236.         self.sections = []
  237.         for i in xrange(self.num_sections):
  238.             offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', self.contents[78+i*8:78+i*8+8])
  239.             flags, val = a1, a2<<16|a3<<8|a4
  240.             self.sections.append( (offset, flags, val) )
  241.     def loadSection(self, section):
  242.         if section + 1 == self.num_sections:
  243.             end_off = len(self.contents)
  244.         else:
  245.             end_off = self.sections[section + 1][0]
  246.         off = self.sections[section][0]
  247.         return self.contents[off:end_off]
  248.  
  249. def sanitizeFileName(s):
  250.     r = ''
  251.     for c in s.lower():
  252.         if c in "abcdefghijklmnopqrstuvwxyz0123456789_.-":
  253.             r += c
  254.     return r
  255.  
  256. def fixKey(key):
  257.     def fixByte(b):
  258.         return b ^ ((b ^ (b<<1) ^ (b<<2) ^ (b<<3) ^ (b<<4) ^ (b<<5) ^ (b<<6) ^ (b<<7) ^ 0x80) & 0x80)
  259.     return  "".join([chr(fixByte(ord(a))) for a in key])
  260.  
  261. class EreaderProcessor:
  262.     def __init__(self, section_reader, username, creditcard):
  263.         self.section_reader = section_reader
  264.         data = section_reader(0)
  265.         version,  = struct.unpack('>H', data[0:2])
  266.         if version != 272 and version != 260:
  267.             raise ValueError('incorrect eReader version %d (error 1)' % version)
  268.         data = section_reader(1)
  269.         self.data = data
  270.         des = Des(fixKey(data[0:8]))
  271.         cookie_shuf, cookie_size = struct.unpack('>LL', des.decrypt(data[-8:]))
  272.         if cookie_shuf < 3 or cookie_shuf > 0x14 or cookie_size < 0xf0 or cookie_size > 0x200:
  273.             raise ValueError('incorrect eReader version (error 2)')
  274.         input = des.decrypt(data[-cookie_size:])
  275.         def unshuff(data, shuf):
  276.             r = [''] * len(data)
  277.             j = 0
  278.             for i in xrange(len(data)):
  279.                 j = (j + shuf) % len(data)
  280.                 r[j] = data[i]
  281.             assert  len("".join(r)) == len(data)
  282.             return "".join(r)
  283.         r = unshuff(input[0:-8], cookie_shuf)
  284.         def fixUsername(s):
  285.             r = ''
  286.             for c in s.lower():
  287.                 if (c >= 'a' and c <= 'z' or c >= '0' and c <= '9'):
  288.                     r += c
  289.             return r
  290.         user_key = struct.pack('>LL', binascii.crc32(fixUsername(username)) & 0xffffffff, binascii.crc32(creditcard[-8:])& 0xffffffff)
  291.         drm_sub_version = struct.unpack('>H', r[0:2])[0]
  292.         self.num_text_pages = struct.unpack('>H', r[2:4])[0] - 1
  293.         self.num_image_pages = struct.unpack('>H', r[26:26+2])[0]
  294.         self.first_image_page = struct.unpack('>H', r[24:24+2])[0]
  295.         self.flags = struct.unpack('>L', r[4:8])[0]
  296.         reqd_flags = (1<<9) | (1<<7) | (1<<10)
  297.         if (self.flags & reqd_flags) != reqd_flags:
  298.             print "Flags: 0x%X" % self.flags
  299.             raise ValueError('incompatible eReader file')
  300.         des = Des(fixKey(user_key))
  301.         if version == 260:
  302.             if drm_sub_version != 13:
  303.                 raise ValueError('incorrect eReader version %d (error 3)' % drm_sub_version)
  304.             encrypted_key = r[44:44+8]
  305.             encrypted_key_sha = r[52:52+20]
  306.         elif version == 272:
  307.             encrypted_key = r[172:172+8]
  308.             encrypted_key_sha = r[56:56+20]
  309.         self.content_key = des.decrypt(encrypted_key)
  310.         if sha.new(self.content_key).digest() != encrypted_key_sha:
  311.             raise ValueError('Incorrect Name and/or Credit Card')
  312.     def getNumImages(self):
  313.         return self.num_image_pages
  314.     def getImage(self, i):
  315.         sect = self.section_reader(self.first_image_page + i)
  316.         name = sect[4:4+32].strip('\0')
  317.         data = sect[62:]
  318.         return sanitizeFileName(name), data
  319.     def getText(self):
  320.         des = Des(fixKey(self.content_key))
  321.         r = ''
  322.         for i in xrange(self.num_text_pages):
  323.             r += zlib.decompress(des.decrypt(self.section_reader(1 + i)))
  324.         return r
  325.  
  326. class PmlConverter:
  327.     def __init__(self, s):
  328.         self.s = s
  329.         self.pos = 0
  330.     def nextOptAttr(self):
  331.         p = self.pos
  332.         if self.s[p:p+2] != '="':
  333.             return None
  334.         r = ''
  335.         p += 2
  336.         while self.s[p] != '"':
  337.             r += self.s[p]
  338.             p += 1
  339.         self.pos = p + 1
  340.         return r
  341.     def next(self):
  342.         p = self.pos
  343.         if p >= len(self.s):
  344.             return None
  345.         if self.s[p] != '\\':
  346.             res = self.s.find('\\', p)
  347.             if res == -1:
  348.                 res = len(self.s)
  349.             self.pos = res
  350.             return self.s[p : res], None, None
  351.         c = self.s[p+1]
  352.         if c in 'pxcriuovtnsblBk-lI\\d':
  353.             self.pos = p + 2
  354.             return None, c, None
  355.         if c in 'TwmqQ':
  356.             self.pos = p + 2
  357.             return None, c, self.nextOptAttr()
  358.         if c == 'a':
  359.             self.pos = p + 5
  360.             return None, c, int(self.s[p+2:p+5])
  361.         if c == 'U':
  362.             self.pos = p + 6
  363.             return None, c, int(self.s[p+2:p+6], 16)
  364.         c = self.s[p+1:p+1+2]
  365.         if c in ('X0','X1','X2','X3','X4','Sp','Sb'):
  366.             self.pos = p + 3
  367.             return None, c, None
  368.         if c in ('C0','C1','C2','C3','C4','Fn','Sd'):
  369.             self.pos = p + 3
  370.             return None, c, self.nextOptAttr()
  371.         print "unknown escape code %s" % c
  372.         self.pos = p + 1
  373.         return None, None, None
  374.     def linkPrinter(link):
  375.         return '<a href="%s">' % link
  376.    
  377.     html_tags = {
  378.         'c' : ('<p align="center">', '</p>'),
  379.         'r' : ('<p align="right">', '</p>'),
  380.         'i' : ('<i>', '</i>'),
  381.         'u' : ('<u>', '</u>'),
  382.         'b' : ('<strong>', '</strong>'),
  383.         'B' : ('<strong>', '</strong>'),
  384.         'o' : ('<strike>', '</strike>'),
  385.         'v' : ('<!-- ', ' -->'),
  386.         't' : ('', ''),
  387.         'Sb' : ('<sub>', '</sub>'),
  388.         'Sp' : ('<sup>', '</sup>'),
  389.         'X0' : ('<h1>', '</h1>'),
  390.         'X1' : ('<h2>', '</h2>'),
  391.         'X2' : ('<h3>', '</h3>'),
  392.         'X3' : ('<h4>', '</h4>'),
  393.         'X4' : ('<h5>', '</h5>'),
  394.         'l' : ('<font size="+2">', '</font>'),
  395.         'q' : (linkPrinter, '</a>'),
  396.     }
  397.     html_one_tags = {
  398.         'p' : '<br><br>'
  399.     }
  400.     pml_chars = {
  401.         160 : '&nbsp;',130 : '&#8212;',131: '&#402;',132: '&#8222;',
  402.         133: '&#8230;',134: '&#8224;',135: '&#8225;',138: '&#352;',
  403.         139: '&#8249;',140: '&#338;',145: '&#8216;',146: '&#8217;',
  404.         147: '&#8220;',148: '&#8221;',149: '&#8226;',150: '&#8211;',
  405.         151: '&#8212;',153: '&#8482;',154: '&#353;',155: '&#8250;',
  406.         156: '&#339;',159: '&#376;'
  407.     }
  408.     def process(self):
  409.         final = '<html><body>\n'
  410.         in_tags = []
  411.         def makeText(s):
  412.             s = s.replace('&', '&amp;')
  413.             #s = s.replace('"', '&quot;')
  414.             s = s.replace('<', '&lt;')
  415.             s = s.replace('>', '&gt;')
  416.             s = s.replace('\n', '<br>\n')
  417.             return s
  418.         while True:
  419.             r = self.next()
  420.             if not r:
  421.                 break
  422.             text, cmd, attr = r
  423.             if text:
  424.                 final += makeText(text)
  425.             if cmd:
  426.                 def getTag(ti, end):
  427.                     cmd, attr = ti
  428.                     r = self.html_tags[cmd][end]
  429.                     if type(r) != str:
  430.                         r = r(attr)
  431.                     return r
  432.                    
  433.                 if cmd in self.html_tags:
  434.                     pair = (cmd, attr)
  435.                     if cmd not in [a for (a,b) in in_tags]:
  436.                         final += getTag(pair, False)
  437.                         in_tags.append(pair)
  438.                     else:
  439.                         j = len(in_tags)
  440.                         while True:
  441.                             j = j - 1
  442.                             final += getTag(in_tags[j], True)
  443.                             if in_tags[j][0] == cmd:
  444.                                 break
  445.                         del in_tags[j]
  446.                         while j < len(in_tags):
  447.                             final += getTag(in_tags[j], False)
  448.                             j = j + 1
  449.                            
  450.                 if cmd in self.html_one_tags:
  451.                     final += self.html_one_tags[cmd]
  452.                 if cmd == 'm':
  453.                     final += '<img src="%s">' % attr
  454.                 if cmd == 'Q':
  455.                     final += '<a name="%s"> </a>' % attr
  456.                 if cmd == 'a':
  457.                     final += self.pml_chars.get(attr, '&#%d;' % attr)
  458.                 if cmd == 'U':
  459.                     final += '&#%d;' % attr
  460.         final += '</body></html>\n'
  461.         while True:
  462.             s = final.replace('<br>\n<br>\n<br>\n', '<br>\n<br>\n')
  463.             if s == final:
  464.                 break
  465.             final = s
  466.         return final
  467.  
  468. def convertEreaderToHtml(infile, name, cc, outdir):
  469.     if not os.path.exists(outdir):
  470.         os.makedirs(outdir)
  471.     sect = Sectionizer(infile, 'PNRdPPrs')
  472.     er = EreaderProcessor(sect.loadSection, name, cc)
  473.  
  474.     for i in xrange(er.getNumImages()):
  475.         name, contents = er.getImage(i)
  476.         file(os.path.join(outdir, name), 'wb').write(contents)
  477.  
  478.     pml = PmlConverter(er.getText())
  479.     file(os.path.join(outdir, 'book.html'),'wb').write(pml.process())
  480.  
  481. print "eReader2Html v0.03. Copyright (c) 2008 The Dark Reverser"
  482. if len(sys.argv)!=5:
  483.     print "Converts eReader books to HTML"
  484.     print "Usage:"
  485.     print "  ereader2html infile.pdb outdir \"your name\" credit_card_number "
  486.     print "Note:"
  487.     print "  It's enough to enter the last 8 digits of the credit card number"
  488. else:  
  489.     infile, outdir, name, cc = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
  490.     try:
  491.         print "Processing...",
  492.         convertEreaderToHtml(infile, name, cc, outdir)
  493.         print "done"
  494.     except ValueError, e:
  495.         print "Error: %s" % e
  496.  
RAW Paste Data