Python 3 fix for old ereader PDB DRM removal
This commit is contained in:
parent
45038cc77b
commit
c52e4db3df
|
@ -200,7 +200,7 @@ class Sectionizer(object):
|
||||||
self.num_sections, = struct.unpack('>H', self.contents[76:78])
|
self.num_sections, = struct.unpack('>H', self.contents[76:78])
|
||||||
# Dictionary or normal content (TODO: Not hard-coded)
|
# Dictionary or normal content (TODO: Not hard-coded)
|
||||||
if self.header[0x3C:0x3C+8] != ident:
|
if self.header[0x3C:0x3C+8] != ident:
|
||||||
if self.header[0x3C:0x3C+8] == "PDctPPrs":
|
if self.header[0x3C:0x3C+8] == b"PDctPPrs":
|
||||||
self.bkType = "Dict"
|
self.bkType = "Dict"
|
||||||
else:
|
else:
|
||||||
raise ValueError('Invalid file format')
|
raise ValueError('Invalid file format')
|
||||||
|
@ -240,7 +240,7 @@ def sanitizeFileName(name):
|
||||||
def fixKey(key):
|
def fixKey(key):
|
||||||
def fixByte(b):
|
def fixByte(b):
|
||||||
return b ^ ((b ^ (b<<1) ^ (b<<2) ^ (b<<3) ^ (b<<4) ^ (b<<5) ^ (b<<6) ^ (b<<7) ^ 0x80) & 0x80)
|
return b ^ ((b ^ (b<<1) ^ (b<<2) ^ (b<<3) ^ (b<<4) ^ (b<<5) ^ (b<<6) ^ (b<<7) ^ 0x80) & 0x80)
|
||||||
return "".join([chr(fixByte(ord(a))) for a in key])
|
return bytes([fixByte(a) for a in key])
|
||||||
|
|
||||||
def deXOR(text, sp, table):
|
def deXOR(text, sp, table):
|
||||||
r=''
|
r=''
|
||||||
|
@ -269,13 +269,13 @@ class EreaderProcessor(object):
|
||||||
raise ValueError('incorrect eReader version (error 2)')
|
raise ValueError('incorrect eReader version (error 2)')
|
||||||
input = des.decrypt(data[-cookie_size:])
|
input = des.decrypt(data[-cookie_size:])
|
||||||
def unshuff(data, shuf):
|
def unshuff(data, shuf):
|
||||||
r = [''] * len(data)
|
r = [0] * len(data)
|
||||||
j = 0
|
j = 0
|
||||||
for i in range(len(data)):
|
for i in range(len(data)):
|
||||||
j = (j + shuf) % len(data)
|
j = (j + shuf) % len(data)
|
||||||
r[j] = data[i]
|
r[j] = data[i]
|
||||||
assert len("".join(r)) == len(data)
|
assert len(bytes(r)) == len(data)
|
||||||
return "".join(r)
|
return bytes(r)
|
||||||
r = unshuff(input[0:-8], cookie_shuf)
|
r = unshuff(input[0:-8], cookie_shuf)
|
||||||
|
|
||||||
drm_sub_version = struct.unpack('>H', r[0:2])[0]
|
drm_sub_version = struct.unpack('>H', r[0:2])[0]
|
||||||
|
@ -354,7 +354,7 @@ class EreaderProcessor(object):
|
||||||
|
|
||||||
def getImage(self, i):
|
def getImage(self, i):
|
||||||
sect = self.section_reader(self.first_image_page + i)
|
sect = self.section_reader(self.first_image_page + i)
|
||||||
name = sect[4:4+32].strip('\0')
|
name = sect[4:4+32].strip(b'\0')
|
||||||
data = sect[62:]
|
data = sect[62:]
|
||||||
return sanitizeFileName(name.decode('windows-1252')), data
|
return sanitizeFileName(name.decode('windows-1252')), data
|
||||||
|
|
||||||
|
@ -404,7 +404,7 @@ class EreaderProcessor(object):
|
||||||
|
|
||||||
def getText(self):
|
def getText(self):
|
||||||
des = Des(fixKey(self.content_key))
|
des = Des(fixKey(self.content_key))
|
||||||
r = ''
|
r = b''
|
||||||
for i in range(self.num_text_pages):
|
for i in range(self.num_text_pages):
|
||||||
logging.debug('get page %d', i)
|
logging.debug('get page %d', i)
|
||||||
r += zlib.decompress(des.decrypt(self.section_reader(1 + i)))
|
r += zlib.decompress(des.decrypt(self.section_reader(1 + i)))
|
||||||
|
@ -456,8 +456,7 @@ def cleanPML(pml):
|
||||||
# Convert special characters to proper PML code. High ASCII start at (\x80, \a128) and go up to (\xff, \a255)
|
# Convert special characters to proper PML code. High ASCII start at (\x80, \a128) and go up to (\xff, \a255)
|
||||||
pml2 = pml
|
pml2 = pml
|
||||||
for k in range(128,256):
|
for k in range(128,256):
|
||||||
badChar = chr(k)
|
pml2 = pml2.replace(bytes([k]), b'\\a%03d' % k)
|
||||||
pml2 = pml2.replace(badChar, '\\a%03d' % k)
|
|
||||||
return pml2
|
return pml2
|
||||||
|
|
||||||
def decryptBook(infile, outpath, make_pmlz, user_key):
|
def decryptBook(infile, outpath, make_pmlz, user_key):
|
||||||
|
@ -476,7 +475,7 @@ def decryptBook(infile, outpath, make_pmlz, user_key):
|
||||||
if not os.path.exists(outdir):
|
if not os.path.exists(outdir):
|
||||||
os.makedirs(outdir)
|
os.makedirs(outdir)
|
||||||
print("Decoding File")
|
print("Decoding File")
|
||||||
sect =Sectionizer(infile, 'PNRdPPrs')
|
sect =Sectionizer(infile, b'PNRdPPrs')
|
||||||
er = EreaderProcessor(sect, user_key)
|
er = EreaderProcessor(sect, user_key)
|
||||||
|
|
||||||
if er.getNumImages() > 0:
|
if er.getNumImages() > 0:
|
||||||
|
|
|
@ -76,7 +76,7 @@ def load_libcrypto():
|
||||||
return ob.raw
|
return ob.raw
|
||||||
def decrypt(self, data):
|
def decrypt(self, data):
|
||||||
if not data:
|
if not data:
|
||||||
return ''
|
return b''
|
||||||
i = 0
|
i = 0
|
||||||
result = []
|
result = []
|
||||||
while i < len(data):
|
while i < len(data):
|
||||||
|
@ -84,6 +84,6 @@ def load_libcrypto():
|
||||||
processed_block = self.desdecrypt(block)
|
processed_block = self.desdecrypt(block)
|
||||||
result.append(processed_block)
|
result.append(processed_block)
|
||||||
i += 8
|
i += 8
|
||||||
return ''.join(result)
|
return b''.join(result)
|
||||||
|
|
||||||
return DES
|
return DES
|
||||||
|
|
Loading…
Reference in New Issue