Update ineptpdf.py

Fix handling of metadata
This commit is contained in:
Apprentice Harper 2021-01-03 15:35:17 +00:00
parent c4581b4d72
commit 6cee615f26

View file

@ -592,7 +592,6 @@ class PSBaseParser(object):
if not pos: if not pos:
pos = self.bufpos+self.charpos pos = self.bufpos+self.charpos
self.fp.seek(pos) self.fp.seek(pos)
##print >>sys.stderr, 'poll(%d): %r' % (pos, self.fp.read(n))
self.fp.seek(pos0) self.fp.seek(pos0)
return return
@ -918,7 +917,6 @@ class PSStackParser(PSBaseParser):
''' '''
while not self.results: while not self.results:
(pos, token) = self.nexttoken() (pos, token) = self.nexttoken()
##print (pos,token), (self.curtype, self.curstack)
if (isinstance(token, int) or if (isinstance(token, int) or
isinstance(token, Decimal) or isinstance(token, Decimal) or
isinstance(token, bool) or isinstance(token, bool) or
@ -1031,7 +1029,7 @@ def decipher_all(decipher, objid, genno, x):
''' '''
Recursively decipher X. Recursively decipher X.
''' '''
if isinstance(x, str): if isinstance(x, bytearray) or isinstance(x,bytes):
return decipher(objid, genno, x) return decipher(objid, genno, x)
decf = lambda v: decipher_all(decipher, objid, genno, v) decf = lambda v: decipher_all(decipher, objid, genno, v)
if isinstance(x, list): if isinstance(x, list):
@ -1169,7 +1167,6 @@ class PDFStream(PDFObject):
if 'Filter' not in self.dic: if 'Filter' not in self.dic:
self.data = data self.data = data
self.rawdata = None self.rawdata = None
##print self.dict
return return
filters = self.dic['Filter'] filters = self.dic['Filter']
if not isinstance(filters, list): if not isinstance(filters, list):
@ -1669,7 +1666,6 @@ class PDFDocument(object):
plaintext = AES.new(key,AES.MODE_CBC,ivector).decrypt(data) plaintext = AES.new(key,AES.MODE_CBC,ivector).decrypt(data)
# remove pkcs#5 aes padding # remove pkcs#5 aes padding
cutter = -1 * plaintext[-1] cutter = -1 * plaintext[-1]
#print cutter
plaintext = plaintext[:cutter] plaintext = plaintext[:cutter]
return plaintext return plaintext
@ -1680,7 +1676,6 @@ class PDFDocument(object):
plaintext = AES.new(key,AES.MODE_CBC,ivector).decrypt(data) plaintext = AES.new(key,AES.MODE_CBC,ivector).decrypt(data)
# remove pkcs#5 aes padding # remove pkcs#5 aes padding
cutter = -1 * plaintext[-1] cutter = -1 * plaintext[-1]
#print cutter
plaintext = plaintext[:cutter] plaintext = plaintext[:cutter]
return plaintext return plaintext
@ -2115,7 +2110,7 @@ class PDFSerializer(object):
# end - hope this doesn't have bad effects # end - hope this doesn't have bad effects
self.write(b'<<') self.write(b'<<')
for key, val in obj.items(): for key, val in obj.items():
self.write(b'/%s' % key.encode('utf-8')) self.write(str(LIT(key.encode('utf-8'))).encode('utf-8'))
self.serialize_object(val) self.serialize_object(val)
self.write(b'>>') self.write(b'>>')
elif isinstance(obj, list): elif isinstance(obj, list):
@ -2175,14 +2170,9 @@ class PDFSerializer(object):
def decryptBook(userkey, inpath, outpath): def decryptBook(userkey, inpath, outpath):
if RSA is None: if RSA is None:
raise ADEPTError("PyCrypto or OpenSSL must be installed.") raise ADEPTError("PyCryptodome or OpenSSL must be installed.")
with open(inpath, 'rb') as inf: with open(inpath, 'rb') as inf:
#try:
serializer = PDFSerializer(inf, userkey) serializer = PDFSerializer(inf, userkey)
#except:
# print "Error serializing pdf {0}. Probably wrong key.".format(os.path.basename(inpath))
# return 2
# hope this will fix the 'bad file descriptor' problem
with open(outpath, 'wb') as outf: with open(outpath, 'wb') as outf:
# help construct to make sure the method runs to the end # help construct to make sure the method runs to the end
try: try: