Skip to content

Commit

Permalink
Add support for empty arrays (<>) in PDF objects. Fixes apprenticehar…
Browse files Browse the repository at this point in the history
  • Loading branch information
noDRM authored and shadyproject committed Feb 15, 2023
1 parent 08bceb2 commit 3119f20
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ List of changes since the fork of Apprentice Harper's repository:
- Re-enable Xrefs in exported PDF files since the file corruption bug is hopefully fixed. Please open bug reports if you encounter new issues with PDF files.
- Fix a bug that would sometimes cause corrupted keys to be added when adding them through the config dialog (fixes #145, #134, #119, #116, #115, #109).
- Update the README (fixes #136) to indicate that Apprentice Harper's version is no longer being updated.
- Fix a bug where PDFs with empty arrays (`<>`) in a PDF object failed to decrypt, fixes #183.
25 changes: 19 additions & 6 deletions DeDRM_plugin/ineptpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ def keyword_name(x):
OCT_STRING = re.compile(br'[0-7]')
ESC_STRING = { b'b':8, b't':9, b'n':10, b'f':12, b'r':13, b'(':40, b')':41, b'\\':92 }

class EmptyArrayValue(object):
def __str__(self):
return "<>"


class PSBaseParser(object):

'''
Expand Down Expand Up @@ -519,6 +524,13 @@ def parse_wopen(self, s, i):
if c == b'<':
self.add_token(KEYWORD_DICT_BEGIN)
i += 1
if c == b'>':
# Empty array without any contents. Why though?
# We need to add some dummy python object that will serialize to
# nothing, otherwise the code removes the whole array.
self.add_token(EmptyArrayValue())
i += 1

return (self.parse_main, i)

def parse_wclose(self, s, i):
Expand All @@ -544,7 +556,6 @@ def parse_hexstring(self, s, i):
else:
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)

Expand Down Expand Up @@ -1591,7 +1602,13 @@ def initialize_standard(self, password, docid, param):

def initialize_ebx_ignoble(self, keyb64, docid, param):
self.is_printable = self.is_modifiable = self.is_extractable = True
key = keyb64.decode('base64')[:16]

try:
key = keyb64.decode('base64')[:16]
# This will probably always error, but I'm not 100% sure, so lets leave the old code in.
except AttributeError:
key = codecs.decode(keyb64.encode("ascii"), 'base64')[:16]


length = int_value(param.get('Length', 0)) / 8
rights = codecs.decode(str_value(param.get('ADEPT_LICENSE')), "base64")
Expand Down Expand Up @@ -2225,11 +2242,7 @@ def serialize_object(self, obj):
elif isinstance(obj, bytearray):
self.write(b'(%s)' % self.escape_string(obj))
elif isinstance(obj, bytes):
# I'm not 100% sure if this is correct, but it seems to fix some PDFs ...
# If needed, revert that change.
self.write(b'<%s>' % binascii.hexlify(obj).upper())
print("ineptpdf.py: Unknown bytes element found - guessing.")
print("If this PDF is corrupted and/or doesn't work, please open a bug report.")
elif isinstance(obj, str):
self.write(b'(%s)' % self.escape_string(obj.encode('utf-8')))
elif isinstance(obj, bool):
Expand Down

0 comments on commit 3119f20

Please sign in to comment.