From 05a302424c477dbbc985bffeeac5b589d5f0259c Mon Sep 17 00:00:00 2001 From: Florian Bach Date: Sun, 16 Jan 2022 18:34:52 +0100 Subject: [PATCH] Update PDF code for Python2 --- bundle_calibre_plugin.sh | 1 + calibre-plugin/libadobe.py | 2 ++ calibre-plugin/libpdf.py | 26 +++++++++++++++++++++----- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/bundle_calibre_plugin.sh b/bundle_calibre_plugin.sh index 9c1a813..7eaa1aa 100755 --- a/bundle_calibre_plugin.sh +++ b/bundle_calibre_plugin.sh @@ -17,6 +17,7 @@ popd # Delete cache rm -r __pycache__ +rm *.pyc # Set module ID. This needs to be changed if any of the module ZIPs change. echo -n "2021-12-19-03" > module_id.txt diff --git a/calibre-plugin/libadobe.py b/calibre-plugin/libadobe.py index d49b24a..55c156e 100644 --- a/calibre-plugin/libadobe.py +++ b/calibre-plugin/libadobe.py @@ -392,6 +392,8 @@ def encrypt_with_device_key(data): for _ in range(remain): data.append(remain) + data = bytes(data) + iv = Random.get_random_bytes(16) cip = AES.new(devkey_bytes, AES.MODE_CBC, iv) diff --git a/calibre-plugin/libpdf.py b/calibre-plugin/libpdf.py index 3add510..0e527ee 100644 --- a/calibre-plugin/libpdf.py +++ b/calibre-plugin/libpdf.py @@ -1,25 +1,37 @@ -import os, zlib, base64, time +import sys, os, zlib, base64, time class BackwardReader: def __init__(self, file): self.file = file + def readlines(self): BLKSIZE = 4096 # Move reader to the end of file self.file.seek(0, os.SEEK_END) - buffer = bytearray() + if sys.version_info[0] >= 3: + buffer = bytearray() + else: + buffer = "" while True: - pos_newline = buffer.rfind(bytes([0x0a])) + if sys.version_info[0] >= 3: + pos_newline = buffer.rfind(bytes([0x0a])) + else: + pos_newline = buffer.rfind("\n") + # Get the current position of the reader current_pos = self.file.tell() if pos_newline != -1: # Newline is found line = buffer[pos_newline+1:] buffer = buffer[:pos_newline] - yield line.decode("latin-1") + if sys.version_info[0] >= 3: + yield line.decode("latin-1") + else: + yield line + elif current_pos: # Need to fill the buffer to_read = min(BLKSIZE, current_pos) @@ -27,13 +39,17 @@ class BackwardReader: buffer = self.file.read(to_read) + buffer self.file.seek(current_pos-to_read, 0) if current_pos is to_read: - buffer = bytes([0x0a]) + buffer + if sys.version_info[0] >= 3: + buffer = bytes([0x0a]) + buffer + else: + buffer = "\n" + buffer else: # Start of file return + def trim_encrypt_string(encrypt): string_list = list(encrypt)