diff --git a/DeDRM_plugin/__calibre_compat_code.py b/DeDRM_plugin/__calibre_compat_code.py index b44b8f3..8257c3e 100644 --- a/DeDRM_plugin/__calibre_compat_code.py +++ b/DeDRM_plugin/__calibre_compat_code.py @@ -8,12 +8,6 @@ if os.path.dirname(os.path.dirname(os.path.abspath(__file__))) not in sys.path: if os.path.dirname(os.path.abspath(__file__)) not in sys.path: sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) -# Bugfix for Calibre < 5: -if "calibre" in sys.modules and sys.version_info[0] == 2: - from calibre.utils.config import config_dir - if os.path.join(config_dir, "plugins", "DeDRM.zip") not in sys.path: - sys.path.insert(0, os.path.join(config_dir, "plugins", "DeDRM.zip")) - if "calibre" in sys.modules: # Explicitly set the package identifier so we are allowed to import stuff ... __package__ = "calibre_plugins.dedrm" diff --git a/DeDRM_plugin/__init__.py b/DeDRM_plugin/__init__.py index 8200122..29e585a 100644 --- a/DeDRM_plugin/__init__.py +++ b/DeDRM_plugin/__init__.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from __future__ import print_function - # __init__.py for DeDRM_plugin # Copyright © 2008-2020 Apprentice Harper et al. # Copyright © 2021-2023 NoDRM diff --git a/DeDRM_plugin/adobekey.py b/DeDRM_plugin/adobekey.py index 5ad771f..b212db9 100644 --- a/DeDRM_plugin/adobekey.py +++ b/DeDRM_plugin/adobekey.py @@ -68,10 +68,7 @@ if iswindows: c_long, c_ulong from ctypes.wintypes import LPVOID, DWORD, BOOL - try: - import winreg - except ImportError: - import _winreg as winreg + import winreg try: from Cryptodome.Cipher import AES @@ -79,10 +76,7 @@ if iswindows: from Crypto.Cipher import AES def unpad(data, padding=16): - if sys.version_info[0] == 2: - pad_len = ord(data[-1]) - else: - pad_len = data[-1] + pad_len = data[-1] return data[:-pad_len] @@ -135,19 +129,9 @@ if iswindows: GetUserName = GetUserName() def GetUserName2(): - try: - from winreg import OpenKey, QueryValueEx, HKEY_CURRENT_USER - except ImportError: - # We're on Python 2 - try: - # The default _winreg on Python2 isn't unicode-safe. - # Check if we have winreg_unicode, a unicode-safe alternative. - # Without winreg_unicode, this will fail with Unicode chars in the username. - from adobekey_winreg_unicode import OpenKey, QueryValueEx, HKEY_CURRENT_USER - except: - from _winreg import OpenKey, QueryValueEx, HKEY_CURRENT_USER + from winreg import OpenKey, QueryValueEx, HKEY_CURRENT_USER - try: + try: DEVICE_KEY_PATH = r'Software\Adobe\Adept\Device' regkey = OpenKey(HKEY_CURRENT_USER, DEVICE_KEY_PATH) userREG = QueryValueEx(regkey, 'username')[0].encode('utf-16-le')[::2] diff --git a/DeDRM_plugin/adobekey_get_passhash.py b/DeDRM_plugin/adobekey_get_passhash.py index 1e9b8e2..d894c6b 100644 --- a/DeDRM_plugin/adobekey_get_passhash.py +++ b/DeDRM_plugin/adobekey_get_passhash.py @@ -28,10 +28,7 @@ except ImportError: def unpad(data, padding=16): - if sys.version_info[0] == 2: - pad_len = ord(data[-1]) - else: - pad_len = data[-1] + pad_len = data[-1] return data[:-pad_len] @@ -60,10 +57,7 @@ def decrypt_passhash(passhash, fp): if iswindows: - try: - import winreg - except ImportError: - import _winreg as winreg + import winreg PRIVATE_LICENCE_KEY_PATH = r'Software\Adobe\Adept\Activation' diff --git a/DeDRM_plugin/adobekey_winreg_unicode.py b/DeDRM_plugin/adobekey_winreg_unicode.py deleted file mode 100644 index 6c719c4..0000000 --- a/DeDRM_plugin/adobekey_winreg_unicode.py +++ /dev/null @@ -1,271 +0,0 @@ -# This is based on https://github.com/DanielStutzbach/winreg_unicode -# The original _winreg in Python2 doesn't support unicode. -# This causes issues if there's unicode chars in the username needed to decrypt the key. - -''' -Copyright 2010 Stutzbach Enterprises, LLC (daniel@stutzbachenterprises.com) - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - 3. The name of the author may not be used to endorse or promote - products derived from this software without specific prior written - permission. - -THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR -IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, -INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING -IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -POSSIBILITY OF SUCH DAMAGE. -''' - -import ctypes, ctypes.wintypes - -ERROR_SUCCESS = 0 -ERROR_MORE_DATA = 234 - -KEY_READ = 0x20019 - -REG_NONE = 0 -REG_SZ = 1 -REG_EXPAND_SZ = 2 -REG_BINARY = 3 -REG_DWORD = 4 -REG_DWORD_BIG_ENDIAN = 5 -REG_DWORD_LITTLE_ENDIAN = 4 -REG_LINK = 6 -REG_MULTI_SZ = 7 -REG_RESOURCE_LIST = 8 -REG_FULL_RESOURCE_DESCRIPTOR = 9 -REG_RESOURCE_REQUIREMENTS_LIST = 10 - -c_HKEY = ctypes.c_void_p -DWORD = ctypes.wintypes.DWORD -BYTE = ctypes.wintypes.BYTE -LPDWORD = ctypes.POINTER(DWORD) -LPBYTE = ctypes.POINTER(BYTE) - -advapi32 = ctypes.windll.advapi32 - -class FILETIME(ctypes.Structure): - _fields_ = [("dwLowDateTime", DWORD), - ("dwHighDateTime", DWORD)] - -RegCloseKey = advapi32.RegCloseKey -RegCloseKey.restype = ctypes.c_long -RegCloseKey.argtypes = [c_HKEY] - -RegOpenKeyEx = advapi32.RegOpenKeyExW -RegOpenKeyEx.restype = ctypes.c_long -RegOpenKeyEx.argtypes = [c_HKEY, ctypes.c_wchar_p, ctypes.c_ulong, - ctypes.c_ulong, ctypes.POINTER(c_HKEY)] - -RegQueryInfoKey = advapi32.RegQueryInfoKeyW -RegQueryInfoKey.restype = ctypes.c_long -RegQueryInfoKey.argtypes = [c_HKEY, ctypes.c_wchar_p, LPDWORD, LPDWORD, - LPDWORD, LPDWORD, LPDWORD, LPDWORD, - LPDWORD, LPDWORD, LPDWORD, - ctypes.POINTER(FILETIME)] - -RegEnumValue = advapi32.RegEnumValueW -RegEnumValue.restype = ctypes.c_long -RegEnumValue.argtypes = [c_HKEY, DWORD, ctypes.c_wchar_p, LPDWORD, - LPDWORD, LPDWORD, LPBYTE, LPDWORD] - -RegEnumKeyEx = advapi32.RegEnumKeyExW -RegEnumKeyEx.restype = ctypes.c_long -RegEnumKeyEx.argtypes = [c_HKEY, DWORD, ctypes.c_wchar_p, LPDWORD, - LPDWORD, ctypes.c_wchar_p, LPDWORD, - ctypes.POINTER(FILETIME)] - -RegQueryValueEx = advapi32.RegQueryValueExW -RegQueryValueEx.restype = ctypes.c_long -RegQueryValueEx.argtypes = [c_HKEY, ctypes.c_wchar_p, LPDWORD, LPDWORD, - LPBYTE, LPDWORD] - -def check_code(code): - if code == ERROR_SUCCESS: - return - raise ctypes.WinError(2) - -class HKEY(object): - def __init__(self): - self.hkey = c_HKEY() - - def __enter__(self): - return self - - def __exit__(self, exc_type=None, exc_val=None, exc_tb=None): - self.Close() - return False - - def Detach(self): - rv = self.cast(self.hkey, self.c_ulong).value - self.hkey = c_HKEY() - return rv - - def __nonzero__(self): - return bool(self.hkey) - - def Close(self): - if not self.hkey: - return - if RegCloseKey is None or check_code is None or c_HKEY is None: - return # globals become None during exit - rc = RegCloseKey(self.hkey) - self.hkey = c_HKEY() - check_code(rc) - - def __del__(self): - self.Close() - -class RootHKEY(ctypes.Structure): - def __init__(self, value): - self.hkey = c_HKEY(value) - - def Close(self): - pass - -HKEY_CLASSES_ROOT = RootHKEY(0x80000000) -HKEY_CURRENT_USER = RootHKEY(0x80000001) -HKEY_LOCAL_MACHINE = RootHKEY(0x80000002) -HKEY_USERS = RootHKEY(0x80000003) -HKEY_PERFORMANCE_DATA = RootHKEY(0x80000004) -HKEY_CURRENT_CONFIG = RootHKEY(0x80000005) -HKEY_DYN_DATA = RootHKEY(0x80000006) - -def OpenKey(key, sub_key): - new_key = HKEY() - rc = RegOpenKeyEx(key.hkey, sub_key, 0, KEY_READ, - ctypes.cast(ctypes.byref(new_key.hkey), - ctypes.POINTER(c_HKEY))) - check_code(rc) - return new_key - -def QueryInfoKey(key): - null = LPDWORD() - num_sub_keys = DWORD() - num_values = DWORD() - ft = FILETIME() - rc = RegQueryInfoKey(key.hkey, ctypes.c_wchar_p(), null, null, - ctypes.byref(num_sub_keys), null, null, - ctypes.byref(num_values), null, null, null, - ctypes.byref(ft)) - check_code(rc) - return (num_sub_keys.value, num_values.value, - ft.dwLowDateTime | (ft.dwHighDateTime << 32)) - -def EnumValue(key, index): - null = LPDWORD() - value_size = DWORD() - data_size = DWORD() - rc = RegQueryInfoKey(key.hkey, ctypes.c_wchar_p(), null, null, null, - null, null, null, - ctypes.byref(value_size), ctypes.byref(data_size), - null, ctypes.POINTER(FILETIME)()) - check_code(rc) - value_size.value += 1 - data_size.value += 1 - - value = ctypes.create_unicode_buffer(value_size.value) - - while True: - data = ctypes.create_string_buffer(data_size.value) - - tmp_value_size = DWORD(value_size.value) - tmp_data_size = DWORD(data_size.value) - typ = DWORD() - rc = RegEnumValue(key.hkey, index, - ctypes.cast(value, ctypes.c_wchar_p), - ctypes.byref(tmp_value_size), null, - ctypes.byref(typ), - ctypes.cast(data, LPBYTE), - ctypes.byref(tmp_data_size)) - - if rc != ERROR_MORE_DATA: - break - - data_size.value *= 2 - - check_code(rc) - return (value.value, Reg2Py(data, tmp_data_size.value, typ.value), - typ.value) - -def split_multi_sz(data, size): - if size == 0: - return [] - Q = size - P = 0 - rv = [] - while P < Q and data[P].value != u'\0': - rv.append[P] - while P < Q and data[P].value != u'\0': - P += 1 - P += 1 - rv.append(size) - return [ctypes.wstring_at(ctypes.pointer(data[rv[i]]), - rv[i+1] - rv[i]).rstrip(u'\x00') - for i in range(len(rv)-1)] - -def Reg2Py(data, size, typ): - if typ == REG_DWORD: - if size == 0: - return 0 - return ctypes.cast(data, ctypes.POINTER(ctypes.c_int)).contents.value - elif typ == REG_SZ or typ == REG_EXPAND_SZ: - return ctypes.wstring_at(data, size // 2).rstrip(u'\x00') - elif typ == REG_MULTI_SZ: - return split_multi_sz(ctypes.cast(data, ctypes.c_wchar_p), size // 2) - else: - if size == 0: - return None - return ctypes.string_at(data, size) - -def EnumKey(key, index): - tmpbuf = ctypes.create_unicode_buffer(257) - length = DWORD(257) - rc = RegEnumKeyEx(key.hkey, index, - ctypes.cast(tmpbuf, ctypes.c_wchar_p), - ctypes.byref(length), - LPDWORD(), ctypes.c_wchar_p(), LPDWORD(), - ctypes.POINTER(FILETIME)()) - check_code(rc) - return ctypes.wstring_at(tmpbuf, length.value).rstrip(u'\x00') - -def QueryValueEx(key, value_name): - size = 256 - typ = DWORD() - while True: - tmp_size = DWORD(size) - buf = ctypes.create_string_buffer(size) - rc = RegQueryValueEx(key.hkey, value_name, LPDWORD(), - ctypes.byref(typ), - ctypes.cast(buf, LPBYTE), ctypes.byref(tmp_size)) - if rc != ERROR_MORE_DATA: - break - - size *= 2 - check_code(rc) - return (Reg2Py(buf, tmp_size.value, typ.value), typ.value) - -__all__ = ['OpenKey', 'QueryInfoKey', 'EnumValue', 'EnumKey', 'QueryValueEx', - 'HKEY_CLASSES_ROOT', 'HKEY_CURRENT_USER', 'HKEY_LOCAL_MACHINE', - 'HKEY_USERS', 'HKEY_PERFORMANCE_DATA', 'HKEY_CURRENT_CONFIG', - 'HKEY_DYN_DATA', 'REG_NONE', 'REG_SZ', 'REG_EXPAND_SZ', - 'REG_BINARY', 'REG_DWORD', 'REG_DWORD_BIG_ENDIAN', - 'REG_DWORD_LITTLE_ENDIAN', 'REG_LINK', 'REG_MULTI_SZ', - 'REG_RESOURCE_LIST', 'REG_FULL_RESOURCE_DESCRIPTOR', - 'REG_RESOURCE_REQUIREMENTS_LIST'] \ No newline at end of file diff --git a/DeDRM_plugin/alfcrypto.py b/DeDRM_plugin/alfcrypto.py index 3b9f319..6513fad 100644 --- a/DeDRM_plugin/alfcrypto.py +++ b/DeDRM_plugin/alfcrypto.py @@ -26,10 +26,7 @@ class Pukall_Cipher(object): raise Exception("PC1: Bad key length") wkey = [] for i in range(8): - 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]) + wkey.append(key[i*2]<<8 | key[i*2+1]) dst = bytearray(len(src)) for i in range(len(src)): temp1 = 0; @@ -42,10 +39,7 @@ class Pukall_Cipher(object): temp1 = (temp1*20021+1)&0xFFFF byteXorVal ^= temp1 ^ sum2 - if sys.version_info[0] == 2: - curByte = ord(src[i]) - else: - curByte = src[i] + curByte = src[i] if not decryption: keyXorVal = curByte * 257; @@ -55,11 +49,9 @@ class Pukall_Cipher(object): for j in range(8): wkey[j] ^= keyXorVal; - if sys.version_info[0] == 2: - dst[i] = chr(curByte) - else: - dst[i] = curByte - + dst[i] = curByte + + return bytes(dst) class Topaz_Cipher(object): diff --git a/DeDRM_plugin/androidkindlekey.py b/DeDRM_plugin/androidkindlekey.py index 27124bd..8ae8372 100755 --- a/DeDRM_plugin/androidkindlekey.py +++ b/DeDRM_plugin/androidkindlekey.py @@ -51,10 +51,7 @@ STORAGE2 = "map_data_storage.db" def unpad(data, padding=16): - if sys.version_info[0] == 2: - pad_len = ord(data[-1]) - else: - pad_len = data[-1] + pad_len = data[-1] return data[:-pad_len] diff --git a/DeDRM_plugin/argv_utils.py b/DeDRM_plugin/argv_utils.py index 2ca4776..2788643 100644 --- a/DeDRM_plugin/argv_utils.py +++ b/DeDRM_plugin/argv_utils.py @@ -3,46 +3,13 @@ import sys -# get sys.argv arguments and encode them into utf-8 +# get sys.argv arguments as a list of Unicode strings def unicode_argv(default_name): - - try: - from calibre.constants import iswindows - except: - iswindows = sys.platform.startswith('win') - - if iswindows: - # Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode - # strings. - - # Versions 2.x of Python don't support Unicode in sys.argv on - # Windows, with the underlying Windows API instead replacing multi-byte - # characters with '?'. - - - from ctypes import POINTER, byref, cdll, c_int, windll - from ctypes.wintypes import LPCWSTR, LPWSTR - - GetCommandLineW = cdll.kernel32.GetCommandLineW - GetCommandLineW.argtypes = [] - GetCommandLineW.restype = LPCWSTR - - CommandLineToArgvW = windll.shell32.CommandLineToArgvW - CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)] - CommandLineToArgvW.restype = POINTER(LPWSTR) - - cmd = GetCommandLineW() - argc = c_int(0) - argv = CommandLineToArgvW(cmd, byref(argc)) - if argc.value > 0: - # Remove Python executable and commands if present - start = argc.value - len(sys.argv) - return [argv[i] for i in - range(start, argc.value)] - # if we don't have any arguments at all, just pass back script name - # this should never happen - return [ default_name ] - else: - argvencoding = sys.stdin.encoding or "utf-8" - return [arg if (isinstance(arg, str) or isinstance(arg,unicode)) else str(arg, argvencoding) for arg in sys.argv] + # On Python 3 sys.argv is already a list of Unicode strings on every + # platform, so no special handling is needed. + if len(sys.argv) > 0: + return sys.argv + # if we don't have any arguments at all, just pass back the script name + # this should never happen + return [default_name] diff --git a/DeDRM_plugin/config.py b/DeDRM_plugin/config.py index 19b40fd..bd2b492 100755 --- a/DeDRM_plugin/config.py +++ b/DeDRM_plugin/config.py @@ -1143,13 +1143,7 @@ class AddAdeptDialog(): scriptpath = os.path.join(parent.parent.alfdir,"adobekey.py") defaultkeys, defaultnames = WineGetKeys(scriptpath, ".der",parent.getwineprefix()) - if sys.version_info[0] < 3: - # Python2 - import itertools - zip_function = itertools.izip - else: - # Python3 - zip_function = zip + zip_function = zip for key, name in zip_function(defaultkeys, defaultnames): key = codecs.encode(key,'hex').decode("latin-1") diff --git a/DeDRM_plugin/epubfontdecrypt.py b/DeDRM_plugin/epubfontdecrypt.py index c9a982f..ec10eab 100644 --- a/DeDRM_plugin/epubfontdecrypt.py +++ b/DeDRM_plugin/epubfontdecrypt.py @@ -16,8 +16,6 @@ Decrypts / deobfuscates font files in EPUB files """ -from __future__ import print_function - __license__ = 'GPL v3' __version__ = "2" diff --git a/DeDRM_plugin/erdr2pml.py b/DeDRM_plugin/erdr2pml.py index 2efe9bb..90cb429 100755 --- a/DeDRM_plugin/erdr2pml.py +++ b/DeDRM_plugin/erdr2pml.py @@ -142,9 +142,6 @@ 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(bytearray([fixByte(a) for a in key])) @@ -152,10 +149,7 @@ def deXOR(text, sp, table): r=b'' j = sp for i in range(len(text)): - if sys.version_info[0] == 2: - r += chr(ord(table[j]) ^ ord(text[i])) - else: - r += bytes(bytearray([table[j] ^ text[i]])) + r += bytes(bytearray([table[j] ^ text[i]])) j = j + 1 if j == len(table): j = 0 diff --git a/DeDRM_plugin/genbook.py b/DeDRM_plugin/genbook.py index 0d49f10..6df4ed4 100644 --- a/DeDRM_plugin/genbook.py +++ b/DeDRM_plugin/genbook.py @@ -2,7 +2,6 @@ # -*- coding: utf-8 -*- # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab # Python 3 for calibre 5.0 -from __future__ import print_function #@@CALIBRE_COMPAT_CODE@@ diff --git a/DeDRM_plugin/ignoblekeyAndroid.py b/DeDRM_plugin/ignoblekeyAndroid.py index e0b2f23..3e59e7f 100644 --- a/DeDRM_plugin/ignoblekeyAndroid.py +++ b/DeDRM_plugin/ignoblekeyAndroid.py @@ -16,10 +16,7 @@ import hashlib from lxml import etree def unpad(data, padding=16): - if sys.version_info[0] == 2: - pad_len = ord(data[-1]) - else: - pad_len = data[-1] + pad_len = data[-1] return data[:-pad_len] diff --git a/DeDRM_plugin/ignoblekeyNookStudy.py b/DeDRM_plugin/ignoblekeyNookStudy.py index 7916c39..2ffdadd 100644 --- a/DeDRM_plugin/ignoblekeyNookStudy.py +++ b/DeDRM_plugin/ignoblekeyNookStudy.py @@ -46,36 +46,22 @@ def getNookLogFiles(): logFiles = [] found = False if iswindows: - try: - import winreg - except ImportError: - import _winreg as winreg + import winreg # some 64 bit machines do not have the proper registry key for some reason # or the python interface to the 32 vs 64 bit registry is broken paths = set() if 'LOCALAPPDATA' in os.environ.keys(): - # Python 2.x does not return unicode env. Use Python 3.x - if sys.version_info[0] == 2: - path = winreg.ExpandEnvironmentStrings(u"%LOCALAPPDATA%") - else: - path = winreg.ExpandEnvironmentStrings("%LOCALAPPDATA%") + path = winreg.ExpandEnvironmentStrings("%LOCALAPPDATA%") if os.path.isdir(path): paths.add(path) if 'USERPROFILE' in os.environ.keys(): - # Python 2.x does not return unicode env. Use Python 3.x - if sys.version_info[0] == 2: - path = winreg.ExpandEnvironmentStrings(u"%USERPROFILE%")+u"\\AppData\\Local" - else: - path = winreg.ExpandEnvironmentStrings("%USERPROFILE%")+"\\AppData\\Local" + path = winreg.ExpandEnvironmentStrings("%USERPROFILE%")+"\\AppData\\Local" if os.path.isdir(path): paths.add(path) - if sys.version_info[0] == 2: - path = winreg.ExpandEnvironmentStrings(u"%USERPROFILE%")+u"\\AppData\\Roaming" - else: - path = winreg.ExpandEnvironmentStrings("%USERPROFILE%")+"\\AppData\\Roaming" + path = winreg.ExpandEnvironmentStrings("%USERPROFILE%")+"\\AppData\\Roaming" if os.path.isdir(path): paths.add(path) # User Shell Folders show take precedent over Shell Folders if present diff --git a/DeDRM_plugin/ignoblekeyWindowsStore.py b/DeDRM_plugin/ignoblekeyWindowsStore.py index 29df204..ab0b624 100644 --- a/DeDRM_plugin/ignoblekeyWindowsStore.py +++ b/DeDRM_plugin/ignoblekeyWindowsStore.py @@ -22,10 +22,7 @@ import hashlib from lxml import etree def unpad(data, padding=16): - if sys.version_info[0] == 2: - pad_len = ord(data[-1]) - else: - pad_len = data[-1] + pad_len = data[-1] return data[:-pad_len] diff --git a/DeDRM_plugin/ineptepub.py b/DeDRM_plugin/ineptepub.py index 3802f71..d9ffac2 100644 --- a/DeDRM_plugin/ineptepub.py +++ b/DeDRM_plugin/ineptepub.py @@ -63,10 +63,7 @@ except ImportError: def unpad(data, padding=16): - if sys.version_info[0] == 2: - pad_len = ord(data[-1]) - else: - pad_len = data[-1] + pad_len = data[-1] return data[:-pad_len] diff --git a/DeDRM_plugin/ineptpdf.py b/DeDRM_plugin/ineptpdf.py index 100dff7..3ab4524 100755 --- a/DeDRM_plugin/ineptpdf.py +++ b/DeDRM_plugin/ineptpdf.py @@ -85,10 +85,7 @@ except ImportError: def unpad(data, padding=16): - if sys.version_info[0] == 2: - pad_len = ord(data[-1]) - else: - pad_len = data[-1] + pad_len = data[-1] return data[:-pad_len] @@ -147,10 +144,7 @@ def nunpack(s, default=0): elif l == 2: return struct.unpack('>H', s)[0] elif l == 3: - if sys.version_info[0] == 2: - return struct.unpack('>L', '\x00'+s)[0] - else: - return struct.unpack('>L', bytes([0]) + s)[0] + return struct.unpack('>L', bytes([0]) + s)[0] elif l == 4: return struct.unpack('>L', s)[0] else: @@ -414,10 +408,7 @@ class PSBaseParser(object): self.hex += c return (self.parse_literal_hex, i+1) if self.hex: - if sys.version_info[0] == 2: - self.token += chr(int(self.hex, 16)) - else: - self.token += bytes([int(self.hex, 16)]) + self.token += bytes([int(self.hex, 16)]) return (self.parse_literal, i) def parse_number(self, s, i): @@ -501,18 +492,12 @@ class PSBaseParser(object): self.oct += c return (self.parse_string_1, i+1) if self.oct: - if sys.version_info[0] == 2: - self.token += chr(int(self.oct, 8)) - else: - self.token += bytes([int(self.oct, 8)]) + self.token += bytes([int(self.oct, 8)]) return (self.parse_string, i) if c in ESC_STRING: - if sys.version_info[0] == 2: - self.token += chr(ESC_STRING[c]) - else: - self.token += bytes([ESC_STRING[c]]) - + self.token += bytes([ESC_STRING[c]]) + return (self.parse_string, i+1) def parse_wopen(self, s, i): @@ -551,12 +536,8 @@ class PSBaseParser(object): return (self.parse_hexstring, len(s)) j = m.start(0) self.token += s[i:j] - if sys.version_info[0] == 2: - token = HEX_PAIR.sub(lambda m: chr(int(m.group(0), 16)), - SPC.sub('', self.token)) - else: - token = HEX_PAIR.sub(lambda m: bytes([int(m.group(0), 16)]), - SPC.sub(b'', self.token)) + token = HEX_PAIR.sub(lambda m: bytes([int(m.group(0), 16)]), + SPC.sub(b'', self.token)) self.add_token(token) return (self.parse_main, j) @@ -577,10 +558,7 @@ class PSBaseParser(object): while 1: self.fillbuf() if eol: - if sys.version_info[0] == 2: - c = self.buf[self.charpos] - else: - c = bytes([self.buf[self.charpos]]) + c = bytes([self.buf[self.charpos]]) # handle '\r\n' if c == b'\n': @@ -591,17 +569,11 @@ class PSBaseParser(object): if m: linebuf += self.buf[self.charpos:m.end(0)] self.charpos = m.end(0) - if sys.version_info[0] == 2: - if linebuf[-1] == b'\r': - eol = True - else: - break - else: - if bytes([linebuf[-1]]) == b'\r': - eol = True - else: - break - + if bytes([linebuf[-1]]) == b'\r': + eol = True + else: + break + else: linebuf += self.buf[self.charpos:] self.charpos = len(self.buf) @@ -977,14 +949,9 @@ class PDFStream(PDFObject): for i in range(0, len(data), columns+1): pred = data[i] ent1 = data[i+1:i+1+columns] - if sys.version_info[0] == 2: - if pred == '\x02': - ent1 = ''.join(chr((ord(a)+ord(b)) & 255) \ - for (a,b) in zip(ent0,ent1)) - else: - if pred == 2: - ent1 = b''.join(bytes([(a+b) & 255]) \ - for (a,b) in zip(ent0,ent1)) + if pred == 2: + ent1 = b''.join(bytes([(a+b) & 255]) \ + for (a,b) in zip(ent0,ent1)) buf += ent1 ent0 = ent1 data = buf @@ -1440,10 +1407,7 @@ class PDFDocument(object): x = ARC4.new(hash).decrypt(Odata) # 4 if R >= 3: for i in range(1,19+1): - if sys.version_info[0] == 2: - k = b''.join(chr(ord(c) ^ i) for c in hash ) - else: - k = b''.join(bytes([c ^ i]) for c in hash ) + k = b''.join(bytes([c ^ i]) for c in hash ) x = ARC4.new(k).decrypt(x) @@ -1510,10 +1474,7 @@ class PDFDocument(object): hash.update(docid[0]) # 3 x = ARC4.new(key).decrypt(hash.digest()[:16]) # 4 for i in range(1,19+1): - if sys.version_info[0] == 2: - k = b''.join(chr(ord(c) ^ i) for c in key ) - else: - k = b''.join(bytes([c ^ i]) for c in key ) + k = b''.join(bytes([c ^ i]) for c in key ) x = ARC4.new(k).decrypt(x) u1 = x+x # 32bytes total @@ -1785,10 +1746,7 @@ class PDFDocument(object): data = data[16:] plaintext = AES.new(key,AES.MODE_CBC,ivector).decrypt(data) # remove pkcs#5 aes padding - if sys.version_info[0] == 2: - cutter = -1 * ord(plaintext[-1]) - else: - cutter = -1 * plaintext[-1] + cutter = -1 * plaintext[-1] plaintext = plaintext[:cutter] return plaintext diff --git a/DeDRM_plugin/k4mobidedrm.py b/DeDRM_plugin/k4mobidedrm.py index 4a22318..268b22f 100644 --- a/DeDRM_plugin/k4mobidedrm.py +++ b/DeDRM_plugin/k4mobidedrm.py @@ -69,10 +69,7 @@ import getopt import re import traceback import time -try: - import html.entities as htmlentitydefs -except: - import htmlentitydefs +import html.entities as htmlentitydefs import json diff --git a/DeDRM_plugin/kgenpids.py b/DeDRM_plugin/kgenpids.py index 670ed93..af93403 100644 --- a/DeDRM_plugin/kgenpids.py +++ b/DeDRM_plugin/kgenpids.py @@ -53,10 +53,7 @@ def SHA1(message): def encode(data, map): result = b'' for char in data: - if sys.version_info[0] == 2: - value = ord(char) - else: - value = char + value = char Q = (value ^ 0x80) // len(map) R = value % len(map) @@ -90,11 +87,9 @@ def decode(data,map): def getTwoBitsFromBitField(bitField,offset): byteNumber = offset // 4 bitPosition = 6 - 2*(offset % 4) - if sys.version_info[0] == 2: - return ord(bitField[byteNumber]) >> bitPosition & 3 - else: - return bitField[byteNumber] >> bitPosition & 3 - + return bitField[byteNumber] >> bitPosition & 3 + + # Returns the six bits at offset from a bit field def getSixBitsFromBitField(bitField,offset): offset *= 3 @@ -171,10 +166,7 @@ def pidFromSerial(s, l): crc = crc32(s) arr1 = [0]*l for i in range(len(s)): - if sys.version_info[0] == 2: - arr1[i%l] ^= ord(s[i]) - else: - arr1[i%l] ^= s[i] + arr1[i%l] ^= s[i] crc_bytes = [crc >> 24 & 0xff, crc >> 16 & 0xff, crc >> 8 & 0xff, crc & 0xff] for i in range(l): arr1[i] ^= crc_bytes[i&3] @@ -190,10 +182,6 @@ 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] diff --git a/DeDRM_plugin/kindlekey.py b/DeDRM_plugin/kindlekey.py index 9dc5381..0da4f0f 100644 --- a/DeDRM_plugin/kindlekey.py +++ b/DeDRM_plugin/kindlekey.py @@ -119,10 +119,7 @@ def primes(n): def encode(data, map): result = b'' for char in data: - if sys.version_info[0] == 2: - value = ord(char) - else: - value = char + value = char Q = (value ^ 0x80) // len(map) R = value % len(map) @@ -160,10 +157,7 @@ if iswindows: create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \ string_at, Structure, c_void_p, cast - try: - import winreg - except ImportError: - import _winreg as winreg + import winreg MAX_PATH = 255 kernel32 = windll.kernel32 @@ -242,14 +236,9 @@ if iswindows: # replace any non-ASCII values with 0xfffd for i in range(0,len(buffer)): - if sys.version_info[0] == 2: - if buffer[i]>u"\u007f": - #print "swapping char "+str(i)+" ("+buffer[i]+")" - buffer[i] = u"\ufffd" - else: - if buffer[i]>"\u007f": - #print "swapping char "+str(i)+" ("+buffer[i]+")" - buffer[i] = "\ufffd" + if buffer[i]>"\u007f": + #print "swapping char "+str(i)+" ("+buffer[i]+")" + buffer[i] = "\ufffd" # return utf-8 encoding of modified username #print "modified username:"+buffer.value return buffer.value.encode('utf-8') @@ -293,11 +282,7 @@ if iswindows: # or the python interface to the 32 vs 64 bit registry is broken path = "" if 'LOCALAPPDATA' in os.environ.keys(): - # Python 2.x does not return unicode env. Use Python 3.x - if sys.version_info[0] == 2: - path = winreg.ExpandEnvironmentStrings(u"%LOCALAPPDATA%") - else: - path = winreg.ExpandEnvironmentStrings("%LOCALAPPDATA%") + path = winreg.ExpandEnvironmentStrings("%LOCALAPPDATA%") # this is just another alternative. # path = getEnvironmentVariable('LOCALAPPDATA') if not os.path.isdir(path): diff --git a/DeDRM_plugin/kindlepid.py b/DeDRM_plugin/kindlepid.py index a851ee5..3700fe9 100644 --- a/DeDRM_plugin/kindlepid.py +++ b/DeDRM_plugin/kindlepid.py @@ -44,10 +44,7 @@ def pidFromSerial(s, l): arr1 = [0]*l for i in range(len(s)): - if sys.version_info[0] == 2: - arr1[i%l] ^= ord(s[i]) - else: - arr1[i%l] ^= s[i] + arr1[i%l] ^= s[i] crc_bytes = [crc >> 24 & 0xff, crc >> 16 & 0xff, crc >> 8 & 0xff, crc & 0xff] for i in range(l): diff --git a/DeDRM_plugin/mobidedrm.py b/DeDRM_plugin/mobidedrm.py index 864c08f..96c20ba 100755 --- a/DeDRM_plugin/mobidedrm.py +++ b/DeDRM_plugin/mobidedrm.py @@ -5,7 +5,6 @@ # Copyright © 2008 The Dark Reverser # Portions © 2008–2020 Apprentice Harper et al. -from __future__ import print_function __license__ = 'GPL v3' __version__ = "1.1" @@ -134,10 +133,7 @@ def getSizeOfTrailingDataEntries(ptr, size, flags): if size <= 0: return result while True: - if sys.version_info[0] == 2: - v = ord(ptr[size-1]) - else: - v = ptr[size-1] + v = ptr[size-1] result |= (v & 0x7F) << bitpos bitpos += 7 @@ -154,10 +150,7 @@ def getSizeOfTrailingDataEntries(ptr, size, flags): # if multibyte data is included in the encryped data, we'll # have already cleared this flag. if flags & 1: - if sys.version_info[0] == 2: - num += (ord(ptr[size - num - 1]) & 0x3) + 1 - else: - num += (ptr[size - num - 1] & 0x3) + 1 + num += (ptr[size - num - 1] & 0x3) + 1 return num @@ -316,10 +309,7 @@ class MobiBook: for pid in pidlist: bigpid = pid.encode('utf-8').ljust(16,b'\0') temp_key = PC1(keyvec1, bigpid, False) - if sys.version_info[0] == 2: - temp_key_sum = sum(map(ord,temp_key)) & 0xff - else: - temp_key_sum = sum(temp_key) & 0xff + 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]) @@ -335,11 +325,8 @@ class MobiBook: # Then try the default encoding that doesn't require a PID pid = '00000000' temp_key = keyvec1 - if sys.version_info[0] == 2: - temp_key_sum = sum(map(ord,temp_key)) & 0xff - else: - temp_key_sum = sum(temp_key) & 0xff - + 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: diff --git a/DeDRM_plugin/standalone/__init__.py b/DeDRM_plugin/standalone/__init__.py index 60b615b..ec35021 100644 --- a/DeDRM_plugin/standalone/__init__.py +++ b/DeDRM_plugin/standalone/__init__.py @@ -3,8 +3,6 @@ # CLI interface for the DeDRM plugin (useable without Calibre, too) -from __future__ import absolute_import, print_function - # Copyright © 2021 NoDRM """ diff --git a/DeDRM_plugin/standalone/jsonconfig.py b/DeDRM_plugin/standalone/jsonconfig.py index b7603df..c239ce3 100644 --- a/DeDRM_plugin/standalone/jsonconfig.py +++ b/DeDRM_plugin/standalone/jsonconfig.py @@ -4,8 +4,6 @@ # CLI interface for the DeDRM plugin (useable without Calibre, too) # Config implementation -from __future__ import absolute_import, print_function - # Taken from Calibre code - Copyright © 2008, Kovid Goyal kovid@kovidgoyal.net, GPLv3 """ diff --git a/DeDRM_plugin/standalone/passhash.py b/DeDRM_plugin/standalone/passhash.py index 028828d..fd04611 100644 --- a/DeDRM_plugin/standalone/passhash.py +++ b/DeDRM_plugin/standalone/passhash.py @@ -4,8 +4,6 @@ # CLI interface for the DeDRM plugin (useable without Calibre, too) # Adobe PassHash implementation -from __future__ import absolute_import, print_function - # Copyright © 2021 NoDRM """ diff --git a/DeDRM_plugin/standalone/remove_drm.py b/DeDRM_plugin/standalone/remove_drm.py index a67bc6f..e1305c9 100644 --- a/DeDRM_plugin/standalone/remove_drm.py +++ b/DeDRM_plugin/standalone/remove_drm.py @@ -4,8 +4,6 @@ # CLI interface for the DeDRM plugin (useable without Calibre, too) # DRM removal -from __future__ import absolute_import, print_function - # Copyright © 2021 NoDRM """ diff --git a/DeDRM_plugin/topazextract.py b/DeDRM_plugin/topazextract.py index 4f2c057..59b892d 100644 --- a/DeDRM_plugin/topazextract.py +++ b/DeDRM_plugin/topazextract.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from __future__ import print_function # topazextract.py # Mostly written by some_updates based on code from many others diff --git a/DeDRM_plugin/utilities.py b/DeDRM_plugin/utilities.py index 50a7de8..c30c451 100644 --- a/DeDRM_plugin/utilities.py +++ b/DeDRM_plugin/utilities.py @@ -9,12 +9,8 @@ __license__ = 'GPL v3' def uStrCmp (s1, s2, caseless=False): import unicodedata as ud - if sys.version_info[0] == 2: - str1 = s1 if isinstance(s1, unicode) else unicode(s1) - str2 = s2 if isinstance(s2, unicode) else unicode(s2) - else: - str1 = s1 if isinstance(s1, str) else str(s1) - str2 = s2 if isinstance(s2, str) else str(s2) + str1 = s1 if isinstance(s1, str) else str(s1) + str2 = s2 if isinstance(s2, str) else str(s2) if caseless: return ud.normalize('NFC', str1.lower()) == ud.normalize('NFC', str2.lower()) diff --git a/Obok_plugin/__init__.py b/Obok_plugin/__init__.py index 864ded3..2f83437 100644 --- a/Obok_plugin/__init__.py +++ b/Obok_plugin/__init__.py @@ -1,7 +1,4 @@ # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai -from __future__ import (unicode_literals, division, absolute_import, - print_function) - __license__ = 'GPL v3' __version__ = '10.0.9' __docformat__ = 'restructuredtext en' diff --git a/Obok_plugin/common_utils.py b/Obok_plugin/common_utils.py index fe67f42..ec63c87 100644 --- a/Obok_plugin/common_utils.py +++ b/Obok_plugin/common_utils.py @@ -411,7 +411,7 @@ class KeyValueComboBox(QComboBox): def populate_combo(self, selected_key): self.clear() selected_idx = idx = -1 - for key, value in self.values.iteritems(): + for key, value in self.values.items(): idx = idx + 1 self.addItem(value) if key == selected_key: @@ -419,7 +419,7 @@ class KeyValueComboBox(QComboBox): self.setCurrentIndex(selected_idx) def selected_key(self): - for key, value in self.values.iteritems(): + for key, value in self.values.items(): if value == self.currentText().strip(): return key @@ -442,7 +442,7 @@ class KeyComboBox(QComboBox): self.setCurrentIndex(selected_idx) def selected_key(self): - for key, value in self.values.iteritems(): + for key, value in self.values.items(): if key == self.currentText().strip(): return key diff --git a/Obok_plugin/dialogs.py b/Obok_plugin/dialogs.py index 16bfcb0..ffb8625 100644 --- a/Obok_plugin/dialogs.py +++ b/Obok_plugin/dialogs.py @@ -1,7 +1,4 @@ # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai -from __future__ import (unicode_literals, division, absolute_import, - print_function) - __license__ = 'GPL v3' __docformat__ = 'restructuredtext en' diff --git a/Obok_plugin/obok/legacy_obok.py b/Obok_plugin/obok/legacy_obok.py index 9a9ae5d..caf24f2 100644 --- a/Obok_plugin/obok/legacy_obok.py +++ b/Obok_plugin/obok/legacy_obok.py @@ -42,10 +42,7 @@ class legacy_obok(object): pwsdid = '' try: if sys.platform.startswith('win'): - try: - import winreg - except ImportError: - import _winreg as winreg + import winreg regkey_browser = winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Software\\Kobo\\Kobo Desktop Edition\\Browser') cookies = winreg.QueryValueEx(regkey_browser, 'cookies') bytearrays = cookies[0] diff --git a/Obok_plugin/obok/obok.py b/Obok_plugin/obok/obok.py index a8379b1..b28be8f 100644 --- a/Obok_plugin/obok/obok.py +++ b/Obok_plugin/obok/obok.py @@ -166,7 +166,6 @@ # after all. # """Manage all Kobo books, either encrypted or DRM-free.""" -from __future__ import print_function __version__ = '10.0.9' __about__ = "Obok v{0}\nCopyright © 2012-2023 Physisticated et al.".format(__version__) @@ -192,10 +191,7 @@ except ImportError: from Crypto.Cipher import AES def unpad(data, padding=16): - if sys.version_info[0] == 2: - pad_len = ord(data[-1]) - else: - pad_len = data[-1] + pad_len = data[-1] return data[:-pad_len] @@ -224,8 +220,7 @@ class SafeUnbuffered: if self.encoding == None: self.encoding = "utf-8" def write(self, data): - if isinstance(data,str) or isinstance(data,unicode): - # str for Python3, unicode for Python2 + if isinstance(data,str): data = data.encode(self.encoding,"replace") try: buffer = getattr(self.stream, 'buffer', self.stream) @@ -305,24 +300,13 @@ class KoboLibrary(object): if (self.kobodir == u""): if sys.platform.startswith('win'): - try: - import winreg - except ImportError: - import _winreg as winreg + import winreg if sys.getwindowsversion().major > 5: if 'LOCALAPPDATA' in os.environ.keys(): - # Python 2.x does not return unicode env. Use Python 3.x - if sys.version_info[0] == 2: - self.kobodir = winreg.ExpandEnvironmentStrings(u"%LOCALAPPDATA%") - else: - self.kobodir = winreg.ExpandEnvironmentStrings("%LOCALAPPDATA%") + self.kobodir = winreg.ExpandEnvironmentStrings("%LOCALAPPDATA%") if (self.kobodir == u""): if 'USERPROFILE' in os.environ.keys(): - # Python 2.x does not return unicode env. Use Python 3.x - if sys.version_info[0] == 2: - self.kobodir = os.path.join(winreg.ExpandEnvironmentStrings(u"%USERPROFILE%"), "Local Settings", "Application Data") - else: - self.kobodir = os.path.join(winreg.ExpandEnvironmentStrings("%USERPROFILE%"), "Local Settings", "Application Data") + self.kobodir = os.path.join(winreg.ExpandEnvironmentStrings("%USERPROFILE%"), "Local Settings", "Application Data") self.kobodir = os.path.join(self.kobodir, "Kobo", "Kobo Desktop Edition") elif sys.platform.startswith('darwin'): self.kobodir = os.path.join(os.environ['HOME'], "Library", "Application Support", "Kobo", "Kobo Desktop Edition") diff --git a/Obok_plugin/utilities.py b/Obok_plugin/utilities.py index ba0d7fc..773e5d2 100644 --- a/Obok_plugin/utilities.py +++ b/Obok_plugin/utilities.py @@ -7,10 +7,7 @@ __docformat__ = 'restructuredtext en' import os, struct, time -try: - from StringIO import StringIO -except ImportError: - from io import StringIO +from io import StringIO from traceback import print_exc from PyQt5.Qt import (Qt, QDialog, QPixmap, QIcon, QLabel, QHBoxLayout, QFont, QTableWidgetItem)