]> xmof Git - DeDRM.git/commitdiff
More Python2 bugfixes
authorNoDRM <no_drm123@protonmail.com>
Thu, 3 Aug 2023 18:01:38 +0000 (20:01 +0200)
committerNoDRM <no_drm123@protonmail.com>
Thu, 3 Aug 2023 18:01:38 +0000 (20:01 +0200)
CHANGELOG.md
DeDRM_plugin/__init__.py
DeDRM_plugin/alfcrypto.py
DeDRM_plugin/erdr2pml.py
DeDRM_plugin/kgenpids.py
DeDRM_plugin/kindlekey.py
DeDRM_plugin/mobidedrm.py

index b5537cdfa88ff46a208b15e539cb47d505fc749d..c10dc1965e0a5e922fcb900513b89a8a8228e3dd 100644 (file)
@@ -101,4 +101,5 @@ This is v10.0.9, a release candidate for v10.1.0. I don't expect there to be maj
 
 - Fix a bug where decrypting a 40-bit RC4 pdf with R=2 didn't work.
 - Fix a bug where decrypting a 256-bit AES pdf with V=5 didn't work.
-- Fix bugs in kgenpids.py and kindlekey.py that caused it to fail on Python 2 (#380).
+- Fix bugs in kgenpids.py, alfcrypto.py, mobidedrm.py and kindlekey.py that caused it to fail on Python 2 (#380).
+- Fix some bugs (Python 2 and Python 3) in erdr2pml.py (untested).
index 9098817ab9c2c6e21b0e7c5352b48cc8f5b55bb8..983ecc807fadac1020147bd738a50ce00aae49ab 100644 (file)
@@ -96,7 +96,10 @@ import traceback
 #@@CALIBRE_COMPAT_CODE@@
 
 try: 
-    import __version
+    try: 
+        from . import __version
+    except:
+        import __version
 except: 
     print("#############################")
     print("Failed to load the DeDRM plugin")
@@ -134,8 +137,10 @@ try:
 except:
     config_dir = ""
 
-
-import utilities
+try: 
+    from . import utilities
+except: 
+    import utilities
 
 
 PLUGIN_NAME = __version.PLUGIN_NAME
@@ -915,6 +920,9 @@ class DeDRM(FileTypePlugin):
             # perhaps we need to get a new default Kindle for Mac/PC key
             defaultkeys = []
             print("{0} v{1}: Failed to decrypt with error: {2}".format(PLUGIN_NAME, PLUGIN_VERSION,e.args[0]))
+
+            traceback.print_exc()
+
             print("{0} v{1}: Looking for new default Kindle Key after {2:.1f} seconds".format(PLUGIN_NAME, PLUGIN_VERSION, time.time()-self.starttime))
 
             try:
index ecb79166a6c52aa42a886c6b44c05de62da9e900..3b9f319c56ba89e9fb21da7da31a758decc092ef 100644 (file)
@@ -8,6 +8,7 @@
 # pbkdf2.py Copyright © 2009 Daniel Holth <dholth@fastmail.fm>
 # pbkdf2.py This code may be freely used and modified for any purpose.
 
+import sys
 import hmac
 from struct import pack
 import hashlib
@@ -25,7 +26,10 @@ class Pukall_Cipher(object):
             raise Exception("PC1: Bad key length")
         wkey = []
         for i in range(8):
-            wkey.append(key[i*2]<<8 | key[i*2+1])
+            if sys.version_info[0] == 2:
+                wkey.append(ord(key[i*2])<<8 | ord(key[i*2+1]))
+            else: 
+                wkey.append(key[i*2]<<8 | key[i*2+1])
         dst = bytearray(len(src))
         for i in range(len(src)):
             temp1 = 0;
@@ -37,7 +41,12 @@ class Pukall_Cipher(object):
                 sum2  = (sum2+sum1)&0xFFFF
                 temp1 = (temp1*20021+1)&0xFFFF
                 byteXorVal ^= temp1 ^ sum2
-            curByte = src[i]
+
+            if sys.version_info[0] == 2:
+                curByte = ord(src[i])
+            else:
+                curByte = src[i]
+
             if not decryption:
                 keyXorVal = curByte * 257;
             curByte = ((curByte ^ (byteXorVal >> 8)) ^ byteXorVal) & 0xFF
@@ -45,7 +54,12 @@ class Pukall_Cipher(object):
                 keyXorVal = curByte * 257;
             for j in range(8):
                 wkey[j] ^= keyXorVal;
-            dst[i] = curByte
+            
+            if sys.version_info[0] == 2:
+                dst[i] = chr(curByte)
+            else: 
+                dst[i] = curByte
+                
         return bytes(dst)
 
 class Topaz_Cipher(object):
@@ -103,7 +117,7 @@ class KeyIVGen(object):
         def xorbytes( a, b ):
             if len(a) != len(b):
                 raise Exception("xorbytes(): lengths differ")
-            return bytes([x ^ y for x, y in zip(a, b)])
+            return bytes(bytearray([x ^ y for x, y in zip(a, b)]))
 
         def prf( h, data ):
             hm = h.copy()
index 1ec999329882e58d49cd1e19c3b65d091a3c076d..7d87aa759cbef87b68518e5b349c08ce6efb2583 100755 (executable)
@@ -79,12 +79,16 @@ except ImportError:
 
 #@@CALIBRE_COMPAT_CODE@@
 
-from utilities import SafeUnbuffered
+try: 
+    from utilities import SafeUnbuffered
+    from argv_utils import unicode_argv
+except: 
+    from . import utilities, argv_utils
 
 iswindows = sys.platform.startswith('win')
 isosx = sys.platform.startswith('darwin')
 
-from argv_utils import unicode_argv
+
 
 import cgi
 import logging
@@ -141,14 +145,20 @@ def sanitizeFileName(name):
 
 def fixKey(key):
     def fixByte(b):
+        if sys.version_info[0] == 2:
+            b = ord(b)
+
         return b ^ ((b ^ (b<<1) ^ (b<<2) ^ (b<<3) ^ (b<<4) ^ (b<<5) ^ (b<<6) ^ (b<<7) ^ 0x80) & 0x80)
-    return bytes([fixByte(a) for a in key])
+    return bytes(bytearray([fixByte(a) for a in key]))
 
 def deXOR(text, sp, table):
-    r=''
+    r=b''
     j = sp
     for i in range(len(text)):
-        r += chr(ord(table[j]) ^ ord(text[i]))
+        if sys.version_info[0] == 2:
+            r += chr(ord(table[j]) ^ ord(text[i]))
+        else: 
+            r += bytes(bytearray([table[j] ^ text[i]]))
         j = j + 1
         if j == len(table):
             j = 0
index d11c3dabbca32bb404231d7121c5597ce1ec76e7..670ed9358c91b1fd74c6dd6df51d2c1667507c44 100644 (file)
@@ -190,6 +190,10 @@ def getKindlePids(rec209, token, serialnum):
     if isinstance(serialnum,str):
         serialnum = serialnum.encode('utf-8')
 
+    if sys.version_info[0] == 2:
+        if isinstance(serialnum,unicode):
+            serialnum = serialnum.encode('utf-8')
+
     if rec209 is None:
         return [serialnum]
 
index e8a0e65b5b2d54c9d84d3d2fe197be89c8be25cb..6297959d24200c28e721a170728c6e0ec0fbfc38 100644 (file)
@@ -62,7 +62,12 @@ except NameError:
 
 # Routines common to Mac and PC
 
-from utilities import SafeUnbuffered
+try: 
+    from utilities import SafeUnbuffered
+    from argv_utils import unicode_argv
+except:
+    from . import utilities, argv_utils
+    
 
 try:
     from calibre.constants import iswindows, isosx
@@ -70,7 +75,7 @@ except:
     iswindows = sys.platform.startswith('win')
     isosx = sys.platform.startswith('darwin')
 
-from argv_utils import unicode_argv
+
 
 class DrmException(Exception):
     pass
index 03748362f021f62fd81a74b2b24e95c6ea3e36b6..36d30c466a1dbba0ed236c26378caf245cf88b76 100755 (executable)
@@ -124,7 +124,11 @@ def getSizeOfTrailingDataEntries(ptr, size, flags):
         if size <= 0:
             return result
         while True:
-            v = ptr[size-1]
+            if sys.version_info[0] == 2:
+                v = ord(ptr[size-1])
+            else:
+                v = ptr[size-1]
+
             result |= (v & 0x7F) << bitpos
             bitpos += 7
             size -= 1
@@ -140,7 +144,10 @@ def getSizeOfTrailingDataEntries(ptr, size, flags):
     # if multibyte data is included in the encryped data, we'll
     # have already cleared this flag.
     if flags & 1:
-        num += (ptr[size - num - 1] & 0x3) + 1
+        if sys.version_info[0] == 2:
+            num += (ord(ptr[size - num - 1]) & 0x3) + 1
+        else: 
+            num += (ptr[size - num - 1] & 0x3) + 1
     return num
 
 
@@ -299,7 +306,10 @@ class MobiBook:
         for pid in pidlist:
             bigpid = pid.encode('utf-8').ljust(16,b'\0')
             temp_key = PC1(keyvec1, bigpid, False)
-            temp_key_sum = sum(temp_key) & 0xff
+            if sys.version_info[0] == 2:
+                temp_key_sum = sum(map(ord,temp_key)) & 0xff
+            else:
+                temp_key_sum = sum(temp_key) & 0xff
             found_key = None
             for i in range(count):
                 verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
@@ -315,7 +325,11 @@ class MobiBook:
             # Then try the default encoding that doesn't require a PID
             pid = '00000000'
             temp_key = keyvec1
-            temp_key_sum = sum(temp_key) & 0xff
+            if sys.version_info[0] == 2:
+                temp_key_sum = sum(map(ord,temp_key)) & 0xff
+            else:
+                temp_key_sum = sum(temp_key) & 0xff
+            
             for i in range(count):
                 verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
                 if cksum == temp_key_sum: