From c52e4db3df82c407ce67237cc10e13f62cacfe6c Mon Sep 17 00:00:00 2001 From: Apprentice Harper Date: Fri, 27 Nov 2020 15:51:33 +0000 Subject: [PATCH] Python 3 fix for old ereader PDB DRM removal --- DeDRM_plugin/erdr2pml.py | 19 +++++++++---------- DeDRM_plugin/openssl_des.py | 4 ++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/DeDRM_plugin/erdr2pml.py b/DeDRM_plugin/erdr2pml.py index 5cc395f..a7ad95e 100755 --- a/DeDRM_plugin/erdr2pml.py +++ b/DeDRM_plugin/erdr2pml.py @@ -200,7 +200,7 @@ class Sectionizer(object): self.num_sections, = struct.unpack('>H', self.contents[76:78]) # Dictionary or normal content (TODO: Not hard-coded) 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" else: raise ValueError('Invalid file format') @@ -240,7 +240,7 @@ def sanitizeFileName(name): def fixKey(key): def fixByte(b): 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): r='' @@ -269,13 +269,13 @@ class EreaderProcessor(object): raise ValueError('incorrect eReader version (error 2)') input = des.decrypt(data[-cookie_size:]) def unshuff(data, shuf): - r = [''] * len(data) + r = [0] * len(data) j = 0 for i in range(len(data)): j = (j + shuf) % len(data) r[j] = data[i] - assert len("".join(r)) == len(data) - return "".join(r) + assert len(bytes(r)) == len(data) + return bytes(r) r = unshuff(input[0:-8], cookie_shuf) drm_sub_version = struct.unpack('>H', r[0:2])[0] @@ -354,7 +354,7 @@ class EreaderProcessor(object): def getImage(self, 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:] return sanitizeFileName(name.decode('windows-1252')), data @@ -404,7 +404,7 @@ class EreaderProcessor(object): def getText(self): des = Des(fixKey(self.content_key)) - r = '' + r = b'' for i in range(self.num_text_pages): logging.debug('get page %d', 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) pml2 = pml for k in range(128,256): - badChar = chr(k) - pml2 = pml2.replace(badChar, '\\a%03d' % k) + pml2 = pml2.replace(bytes([k]), b'\\a%03d' % k) return pml2 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): os.makedirs(outdir) print("Decoding File") - sect =Sectionizer(infile, 'PNRdPPrs') + sect =Sectionizer(infile, b'PNRdPPrs') er = EreaderProcessor(sect, user_key) if er.getNumImages() > 0: diff --git a/DeDRM_plugin/openssl_des.py b/DeDRM_plugin/openssl_des.py index 9a84e58..9e455b4 100644 --- a/DeDRM_plugin/openssl_des.py +++ b/DeDRM_plugin/openssl_des.py @@ -76,7 +76,7 @@ def load_libcrypto(): return ob.raw def decrypt(self, data): if not data: - return '' + return b'' i = 0 result = [] while i < len(data): @@ -84,6 +84,6 @@ def load_libcrypto(): processed_block = self.desdecrypt(block) result.append(processed_block) i += 8 - return ''.join(result) + return b''.join(result) return DES