2023-02-24 19:11:15 +06:00
|
|
|
'''
|
|
|
|
Copyright (c) 2021-2023 Leseratte10
|
|
|
|
This file is part of the ACSM Input Plugin by Leseratte10
|
|
|
|
ACSM Input Plugin for Calibre / acsm-calibre-plugin
|
|
|
|
|
|
|
|
For more information, see:
|
|
|
|
https://github.com/Leseratte10/acsm-calibre-plugin
|
|
|
|
'''
|
|
|
|
|
2022-01-16 23:34:52 +06:00
|
|
|
import sys, os, zlib, base64, time
|
2021-10-03 14:30:45 +06:00
|
|
|
|
|
|
|
class BackwardReader:
|
|
|
|
|
|
|
|
def __init__(self, file):
|
|
|
|
self.file = file
|
|
|
|
|
2022-01-16 23:34:52 +06:00
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
def readlines(self):
|
|
|
|
BLKSIZE = 4096
|
|
|
|
# Move reader to the end of file
|
|
|
|
self.file.seek(0, os.SEEK_END)
|
2022-01-16 23:34:52 +06:00
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
buffer = bytearray()
|
|
|
|
else:
|
|
|
|
buffer = ""
|
2021-10-03 14:30:45 +06:00
|
|
|
|
|
|
|
while True:
|
2022-01-16 23:34:52 +06:00
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
pos_newline = buffer.rfind(bytes([0x0a]))
|
|
|
|
else:
|
|
|
|
pos_newline = buffer.rfind("\n")
|
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
# 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]
|
2022-01-16 23:34:52 +06:00
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
yield line.decode("latin-1")
|
|
|
|
else:
|
|
|
|
yield line
|
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
elif current_pos:
|
|
|
|
# Need to fill the buffer
|
|
|
|
to_read = min(BLKSIZE, current_pos)
|
|
|
|
self.file.seek(current_pos-to_read, 0)
|
|
|
|
buffer = self.file.read(to_read) + buffer
|
|
|
|
self.file.seek(current_pos-to_read, 0)
|
|
|
|
if current_pos is to_read:
|
2022-01-16 23:34:52 +06:00
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
buffer = bytes([0x0a]) + buffer
|
|
|
|
else:
|
|
|
|
buffer = "\n" + buffer
|
2021-09-28 22:43:14 +06:00
|
|
|
else:
|
2021-10-03 14:30:45 +06:00
|
|
|
# Start of file
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-16 23:34:52 +06:00
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
def trim_encrypt_string(encrypt):
|
|
|
|
|
|
|
|
string_list = list(encrypt)
|
|
|
|
strlen = len(encrypt)
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
bracket_count = 0
|
|
|
|
while (i < strlen):
|
|
|
|
if string_list[i] == "<" and string_list[i+1] == "<":
|
|
|
|
bracket_count += 1
|
|
|
|
|
|
|
|
if string_list[i] == ">" and string_list[i+1] == ">":
|
|
|
|
bracket_count -= 1
|
|
|
|
|
|
|
|
if bracket_count == 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
len_to_use = i+2
|
|
|
|
|
|
|
|
return encrypt[0:len_to_use]
|
|
|
|
|
|
|
|
def cleanup_encrypt_element(element):
|
|
|
|
|
|
|
|
if element.startswith("ID[<"):
|
|
|
|
element = element.replace("><", "> <")
|
|
|
|
|
|
|
|
element = ' '.join(element.split())
|
|
|
|
element = element.replace("[ ", "[").replace("] ", "]")
|
|
|
|
|
|
|
|
return element
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-09-28 22:43:14 +06:00
|
|
|
|
|
|
|
def deflate_and_base64_encode( string_val ):
|
|
|
|
zlibbed_str = zlib.compress( string_val )
|
|
|
|
compressed_string = zlibbed_str[2:-4]
|
|
|
|
return base64.b64encode( compressed_string )
|
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
def update_ebx_with_keys(ebx_data, adept_license, ebx_bookid):
|
|
|
|
|
|
|
|
b64data = deflate_and_base64_encode(adept_license.encode("utf-8")).decode("utf-8")
|
|
|
|
|
|
|
|
ebx_new = ebx_data[:-2]
|
|
|
|
ebx_new += "/EBX_BOOKID(%s)/ADEPT_LICENSE(%s)>>" % (ebx_bookid, b64data)
|
|
|
|
|
|
|
|
return ebx_new
|
|
|
|
|
|
|
|
|
|
|
|
def find_ebx(filename_in):
|
|
|
|
find_ebx_start = int(time.time() * 1000)
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
fl = open(filename_in, "rb")
|
|
|
|
br = BackwardReader(fl)
|
|
|
|
|
|
|
|
for line in br.readlines():
|
|
|
|
i = i + 1
|
|
|
|
if "/EBX_HANDLER/" in line:
|
|
|
|
find_ebx_end = int(time.time() * 1000)
|
|
|
|
print("Found EBX after %d attempts - took %d ms" % (i, find_ebx_end - find_ebx_start))
|
|
|
|
return line
|
2021-09-28 22:43:14 +06:00
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
find_ebx_end = int(time.time() * 1000)
|
|
|
|
print("Error: Did not find EBX_HANDLER - took %d ms" % (find_ebx_end - find_ebx_start))
|
|
|
|
return None
|
2021-09-28 22:43:14 +06:00
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
def find_enc(filename_in):
|
|
|
|
find_enc_start = int(time.time() * 1000)
|
|
|
|
i = 0
|
2021-09-28 22:43:14 +06:00
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
fl = open(filename_in, "rb")
|
|
|
|
br = BackwardReader(fl)
|
2021-09-28 22:43:14 +06:00
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
for line in br.readlines():
|
|
|
|
i = i + 1
|
2022-12-14 03:16:51 +06:00
|
|
|
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:
|
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
find_enc_end = int(time.time() * 1000)
|
|
|
|
print("Found ENC after %d attempts - took %d ms" % (i, find_enc_end - find_enc_start))
|
2022-12-14 03:16:51 +06:00
|
|
|
if is_encrypt_odd:
|
|
|
|
print("Odd formatting of encryption blob?")
|
|
|
|
print("If this doesn't work correctly please open a bug report.")
|
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
return line
|
|
|
|
|
|
|
|
find_enc_end = int(time.time() * 1000)
|
|
|
|
print("Error: Did not find ENC - took %d ms" % (find_enc_end - find_enc_start))
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def patch_drm_into_pdf(filename_in, adept_license_string, filename_out, ebx_bookid):
|
|
|
|
|
|
|
|
drm_start_time = int(time.time() * 1000)
|
2021-09-28 22:43:14 +06:00
|
|
|
|
|
|
|
trailer = ""
|
|
|
|
trailer_idx = 0
|
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
startxref_offset = 0
|
|
|
|
prevline = ""
|
2021-10-01 18:32:46 +06:00
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
|
|
|
|
fl = open(filename_in, "rb")
|
|
|
|
br = BackwardReader(fl)
|
|
|
|
|
|
|
|
print("Searching for startxref ...")
|
|
|
|
for line in br.readlines():
|
2021-09-28 22:43:14 +06:00
|
|
|
trailer_idx += 1
|
|
|
|
trailer = line + "\n" + trailer
|
2021-10-03 14:30:45 +06:00
|
|
|
|
2021-10-04 19:36:25 +06:00
|
|
|
#print ("LINE: " + line)
|
2021-10-03 14:30:45 +06:00
|
|
|
|
|
|
|
if (trailer_idx > 10):
|
|
|
|
print("Took more than 10 attempts to find startxref ...")
|
|
|
|
return False
|
|
|
|
|
|
|
|
if (line == "startxref"):
|
|
|
|
startxref_offset = int(prevline)
|
|
|
|
print("Got startxref: %d" % (startxref_offset))
|
2021-09-28 22:43:14 +06:00
|
|
|
break
|
2021-10-03 14:30:45 +06:00
|
|
|
prevline = line
|
|
|
|
|
|
|
|
|
2021-09-28 22:43:14 +06:00
|
|
|
|
|
|
|
r_encrypt_offs1 = 0
|
|
|
|
r_encrypt_offs2 = 0
|
2021-10-03 14:30:45 +06:00
|
|
|
|
|
|
|
encrypt = None
|
|
|
|
|
|
|
|
|
|
|
|
encrypt = find_enc(filename_in)
|
|
|
|
if encrypt is None:
|
|
|
|
print("Error, enc not found")
|
|
|
|
return False
|
|
|
|
|
|
|
|
line_split = encrypt.split(' ')
|
|
|
|
next = 0
|
|
|
|
for element in line_split:
|
2022-12-14 03:16:51 +06:00
|
|
|
if element == "R/Encrypt" or element == "/Encrypt":
|
2021-10-03 14:30:45 +06:00
|
|
|
next = 2
|
2021-09-28 22:43:14 +06:00
|
|
|
continue
|
2021-10-03 14:30:45 +06:00
|
|
|
if next == 2:
|
|
|
|
r_encrypt_offs1 = element
|
|
|
|
next = 1
|
|
|
|
continue
|
|
|
|
if next == 1:
|
|
|
|
r_encrypt_offs2 = element
|
|
|
|
next = 0
|
2021-09-28 22:43:14 +06:00
|
|
|
continue
|
2021-10-03 14:30:45 +06:00
|
|
|
|
|
|
|
|
|
|
|
# read EBX element:
|
|
|
|
ebx_elem = find_ebx(filename_in)
|
|
|
|
|
|
|
|
if (ebx_elem is None):
|
|
|
|
print("Err: EBX is None")
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
print("")
|
|
|
|
print("")
|
|
|
|
print("Encryption handler:")
|
|
|
|
print(encrypt)
|
|
|
|
print("EBX handler:")
|
|
|
|
print(ebx_elem)
|
|
|
|
|
|
|
|
encrypt = trim_encrypt_string(encrypt)
|
|
|
|
|
|
|
|
print("Trimmed encryption handler:")
|
|
|
|
print(encrypt)
|
|
|
|
|
|
|
|
ebx_elem = update_ebx_with_keys(ebx_elem, adept_license_string, ebx_bookid)
|
|
|
|
|
|
|
|
print("Updated EBX handler not logged due to sensitive data")
|
|
|
|
#print(ebx_elem)
|
2021-09-28 22:43:14 +06:00
|
|
|
|
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
filesize_str = str(os.path.getsize(filename_in))
|
2021-09-28 22:43:14 +06:00
|
|
|
filesize_pad = filesize_str.zfill(10)
|
|
|
|
|
|
|
|
|
|
|
|
additional_data = "\r"
|
|
|
|
additional_data += r_encrypt_offs1 + " " + r_encrypt_offs2 + " " + "obj" + "\r"
|
2021-10-03 14:30:45 +06:00
|
|
|
additional_data += ebx_elem
|
2021-09-28 22:43:14 +06:00
|
|
|
additional_data += "\r"
|
|
|
|
additional_data += "endobj"
|
|
|
|
|
|
|
|
ptr = int(filesize_str) + len(additional_data)
|
|
|
|
|
|
|
|
additional_data += "\rxref\r" + r_encrypt_offs1 + " " + str((int(r_encrypt_offs2) + 1)) + "\r"
|
|
|
|
additional_data += filesize_pad + " 00000 n" + "\r\n"
|
|
|
|
additional_data += "trailer"
|
|
|
|
additional_data += "\r"
|
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
arr_root_str = encrypt.split('/')
|
2021-09-28 22:43:14 +06:00
|
|
|
did_prev = False
|
|
|
|
for elem in arr_root_str:
|
|
|
|
if elem.startswith("Prev"):
|
|
|
|
did_prev = True
|
2021-10-03 14:30:45 +06:00
|
|
|
additional_data += "Prev " + str(startxref_offset)
|
2021-09-28 22:43:14 +06:00
|
|
|
#print("Replacing prev from '%s' to '%s'" % (elem, "Prev " + startxref))
|
|
|
|
else:
|
2021-10-03 14:30:45 +06:00
|
|
|
additional_data += cleanup_encrypt_element(elem)
|
2021-09-28 22:43:14 +06:00
|
|
|
additional_data += "/"
|
|
|
|
|
|
|
|
if not did_prev:
|
|
|
|
# remove two >> at end
|
|
|
|
additional_data = additional_data[:-3]
|
2021-10-03 14:30:45 +06:00
|
|
|
additional_data += "/Prev " + str(startxref_offset) + ">>" + "/"
|
2021-09-28 22:43:14 +06:00
|
|
|
#print("Faking Prev %s" % startxref)
|
|
|
|
|
|
|
|
additional_data = additional_data[:-1]
|
|
|
|
|
|
|
|
additional_data += "\r" + "startxref\r" + str(ptr) + "\r" + "%%EOF"
|
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
#print("Appending DRM data: %s" % (additional_data))
|
2021-10-01 18:32:46 +06:00
|
|
|
|
2021-09-28 22:43:14 +06:00
|
|
|
|
2021-10-03 14:30:45 +06:00
|
|
|
inp = open(filename_in, "rb")
|
2021-09-28 22:43:14 +06:00
|
|
|
|
|
|
|
out = open(filename_out, "wb")
|
|
|
|
out.write(inp.read())
|
|
|
|
out.write(additional_data.encode("latin-1"))
|
|
|
|
inp.close()
|
|
|
|
out.close()
|
2021-10-03 14:30:45 +06:00
|
|
|
|
|
|
|
drm_end_time = int(time.time() * 1000)
|
|
|
|
|
|
|
|
print("Whole DRM patching took %d milliseconds." % (drm_end_time - drm_start_time))
|
|
|
|
|
|
|
|
return True
|