Guest User

DarkReverser

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