From c50828031973d9b5a5aa0e97213b0d4a2c0fcf22 Mon Sep 17 00:00:00 2001 From: Florian Bach Date: Tue, 13 Dec 2022 22:16:51 +0100 Subject: [PATCH] Try to support PDFs with weird spacing in Encrypt blob Some PDFs apparently use nonstandard spacing in the /Encrypt blob which my parser didn't like. Hopefully this fixes it? Related to #44 --- calibre-plugin/libpdf.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/calibre-plugin/libpdf.py b/calibre-plugin/libpdf.py index 0e527ee..51b9402 100644 --- a/calibre-plugin/libpdf.py +++ b/calibre-plugin/libpdf.py @@ -128,9 +128,16 @@ def find_enc(filename_in): for line in br.readlines(): i = i + 1 - if "R/Encrypt" in line and "R/ID" in line: + is_encrypt_normal = "R/Encrypt" in line and "R/ID" in line + is_encrypt_odd = "R" in line and "/Encrypt" in line and "/ID" in line + if is_encrypt_normal or is_encrypt_odd: + find_enc_end = int(time.time() * 1000) print("Found ENC after %d attempts - took %d ms" % (i, find_enc_end - find_enc_start)) + if is_encrypt_odd: + print("Odd formatting of encryption blob?") + print("If this doesn't work correctly please open a bug report.") + return line find_enc_end = int(time.time() * 1000) @@ -186,7 +193,7 @@ def patch_drm_into_pdf(filename_in, adept_license_string, filename_out, ebx_book line_split = encrypt.split(' ') next = 0 for element in line_split: - if element == "R/Encrypt": + if element == "R/Encrypt" or element == "/Encrypt": next = 2 continue if next == 2: