2016-11-08 19:01:11 +00:00
|
|
|
|
2009-11-03 01:27:30 +00:00
|
|
|
import zlib
|
2016-05-20 19:12:05 +00:00
|
|
|
import logging
|
2014-06-26 09:12:39 +00:00
|
|
|
from .lzw import lzwdecode
|
|
|
|
from .ascii85 import ascii85decode
|
|
|
|
from .ascii85 import asciihexdecode
|
|
|
|
from .runlength import rldecode
|
|
|
|
from .ccitt import ccittfaxdecode
|
|
|
|
from .psparser import PSException
|
|
|
|
from .psparser import PSObject
|
|
|
|
from .psparser import LIT
|
2016-01-10 17:17:38 +00:00
|
|
|
from . import settings
|
2014-06-26 09:12:39 +00:00
|
|
|
from .utils import apply_png_predictor
|
|
|
|
from .utils import isnumber
|
|
|
|
|
2014-09-03 11:17:41 +00:00
|
|
|
import six #Python 2+3 compatibility
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2016-05-20 19:12:05 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2009-11-07 00:55:18 +00:00
|
|
|
LITERAL_CRYPT = LIT('Crypt')
|
2010-01-30 07:30:01 +00:00
|
|
|
|
|
|
|
# Abbreviation of Filter names in PDF 4.8.6. "Inline Images"
|
2009-11-07 00:55:18 +00:00
|
|
|
LITERALS_FLATE_DECODE = (LIT('FlateDecode'), LIT('Fl'))
|
|
|
|
LITERALS_LZW_DECODE = (LIT('LZWDecode'), LIT('LZW'))
|
|
|
|
LITERALS_ASCII85_DECODE = (LIT('ASCII85Decode'), LIT('A85'))
|
|
|
|
LITERALS_ASCIIHEX_DECODE = (LIT('ASCIIHexDecode'), LIT('AHx'))
|
2009-12-24 11:51:43 +00:00
|
|
|
LITERALS_RUNLENGTH_DECODE = (LIT('RunLengthDecode'), LIT('RL'))
|
2010-01-30 07:30:01 +00:00
|
|
|
LITERALS_CCITTFAX_DECODE = (LIT('CCITTFaxDecode'), LIT('CCF'))
|
|
|
|
LITERALS_DCT_DECODE = (LIT('DCTDecode'), LIT('DCT'))
|
2009-01-10 09:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
## PDF Objects
|
|
|
|
##
|
2013-11-07 08:35:04 +00:00
|
|
|
class PDFObject(PSObject):
|
|
|
|
pass
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
class PDFException(PSException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class PDFTypeError(PDFException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class PDFValueError(PDFException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class PDFObjectNotFound(PDFException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class PDFNotImplementedError(PDFException):
|
|
|
|
pass
|
2009-01-10 09:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
## PDFObjRef
|
|
|
|
##
|
|
|
|
class PDFObjRef(PDFObject):
|
|
|
|
|
2009-10-24 04:41:59 +00:00
|
|
|
def __init__(self, doc, objid, _):
|
|
|
|
if objid == 0:
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFValueError('PDF object id cannot be 0.')
|
|
|
|
self.doc = doc
|
|
|
|
self.objid = objid
|
|
|
|
#self.genno = genno # Never used.
|
|
|
|
return
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2009-10-24 04:41:59 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return '<PDFObjRef:%d>' % (self.objid)
|
|
|
|
|
2013-10-09 13:45:54 +00:00
|
|
|
def resolve(self, default=None):
|
|
|
|
try:
|
|
|
|
return self.doc.getobj(self.objid)
|
|
|
|
except PDFObjectNotFound:
|
|
|
|
return default
|
2009-01-10 09:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
# resolve
|
2013-10-09 13:45:54 +00:00
|
|
|
def resolve1(x, default=None):
|
2011-02-27 04:09:12 +00:00
|
|
|
"""Resolves an object.
|
|
|
|
|
|
|
|
If this is an array or dictionary, it may still contains
|
|
|
|
some indirect objects inside.
|
|
|
|
"""
|
2009-10-24 04:41:59 +00:00
|
|
|
while isinstance(x, PDFObjRef):
|
2013-10-09 13:45:54 +00:00
|
|
|
x = x.resolve(default=default)
|
2009-10-24 04:41:59 +00:00
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2013-10-09 13:45:54 +00:00
|
|
|
def resolve_all(x, default=None):
|
2011-02-27 04:09:12 +00:00
|
|
|
"""Recursively resolves the given object and all the internals.
|
2013-11-07 07:14:53 +00:00
|
|
|
|
2009-10-24 04:41:59 +00:00
|
|
|
Make sure there is no indirect reference within the nested object.
|
|
|
|
This procedure might be slow.
|
2011-02-27 04:09:12 +00:00
|
|
|
"""
|
2009-10-24 04:41:59 +00:00
|
|
|
while isinstance(x, PDFObjRef):
|
2013-10-09 13:45:54 +00:00
|
|
|
x = x.resolve(default=default)
|
2009-10-24 04:41:59 +00:00
|
|
|
if isinstance(x, list):
|
2013-11-07 08:35:04 +00:00
|
|
|
x = [resolve_all(v, default=default) for v in x]
|
2009-10-24 04:41:59 +00:00
|
|
|
elif isinstance(x, dict):
|
2013-11-07 08:35:04 +00:00
|
|
|
for (k, v) in x.iteritems():
|
2013-10-09 13:45:54 +00:00
|
|
|
x[k] = resolve_all(v, default=default)
|
2009-10-24 04:41:59 +00:00
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def decipher_all(decipher, objid, genno, x):
|
2011-02-27 04:09:12 +00:00
|
|
|
"""Recursively deciphers the given object.
|
|
|
|
"""
|
2014-09-11 21:33:14 +00:00
|
|
|
if isinstance(x, bytes):
|
2009-10-24 04:41:59 +00:00
|
|
|
return decipher(objid, genno, x)
|
|
|
|
if isinstance(x, list):
|
2013-11-07 08:35:04 +00:00
|
|
|
x = [decipher_all(decipher, objid, genno, v) for v in x]
|
2009-10-24 04:41:59 +00:00
|
|
|
elif isinstance(x, dict):
|
2014-09-03 11:17:41 +00:00
|
|
|
for (k, v) in six.iteritems(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x[k] = decipher_all(decipher, objid, genno, v)
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
# Type cheking
|
|
|
|
def int_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
|
|
|
if not isinstance(x, int):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('Integer required: %r' % x)
|
|
|
|
return 0
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def float_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
|
|
|
if not isinstance(x, float):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('Float required: %r' % x)
|
|
|
|
return 0.0
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def num_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
2014-03-28 08:50:59 +00:00
|
|
|
if not isnumber(x):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('Int or Float required: %r' % x)
|
|
|
|
return 0
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def str_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
2014-09-03 11:17:41 +00:00
|
|
|
if not isinstance(x, six.binary_type):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('String required: %r' % x)
|
|
|
|
return ''
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def list_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
2014-03-28 08:50:59 +00:00
|
|
|
if not isinstance(x, (list, tuple)):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('List required: %r' % x)
|
|
|
|
return []
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def dict_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
|
|
|
if not isinstance(x, dict):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2016-05-20 19:12:05 +00:00
|
|
|
log.error('PDFTypeError : Dict required: %r', x)
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('Dict required: %r' % x)
|
|
|
|
return {}
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2009-01-10 09:14:46 +00:00
|
|
|
def stream_value(x):
|
2009-10-24 04:41:59 +00:00
|
|
|
x = resolve1(x)
|
|
|
|
if not isinstance(x, PDFStream):
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFTypeError('PDFStream required: %r' % x)
|
|
|
|
return PDFStream({}, '')
|
|
|
|
return x
|
2009-01-10 09:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
## PDFStream type
|
|
|
|
##
|
|
|
|
class PDFStream(PDFObject):
|
2009-10-24 04:41:59 +00:00
|
|
|
|
2010-01-30 07:30:01 +00:00
|
|
|
def __init__(self, attrs, rawdata, decipher=None):
|
|
|
|
assert isinstance(attrs, dict)
|
|
|
|
self.attrs = attrs
|
2009-10-24 04:41:59 +00:00
|
|
|
self.rawdata = rawdata
|
|
|
|
self.decipher = decipher
|
|
|
|
self.data = None
|
|
|
|
self.objid = None
|
|
|
|
self.genno = None
|
|
|
|
return
|
|
|
|
|
|
|
|
def set_objid(self, objid, genno):
|
|
|
|
self.objid = objid
|
|
|
|
self.genno = genno
|
|
|
|
return
|
|
|
|
|
|
|
|
def __repr__(self):
|
2010-02-15 14:50:19 +00:00
|
|
|
if self.data is None:
|
|
|
|
assert self.rawdata is not None
|
|
|
|
return '<PDFStream(%r): raw=%d, %r>' % (self.objid, len(self.rawdata), self.attrs)
|
|
|
|
else:
|
|
|
|
assert self.data is not None
|
|
|
|
return '<PDFStream(%r): len=%d, %r>' % (self.objid, len(self.data), self.attrs)
|
2010-01-30 07:30:01 +00:00
|
|
|
|
|
|
|
def __contains__(self, name):
|
|
|
|
return name in self.attrs
|
2013-11-07 07:14:53 +00:00
|
|
|
|
2010-01-30 07:30:01 +00:00
|
|
|
def __getitem__(self, name):
|
|
|
|
return self.attrs[name]
|
2013-11-07 07:14:53 +00:00
|
|
|
|
2010-01-30 07:30:01 +00:00
|
|
|
def get(self, name, default=None):
|
|
|
|
return self.attrs.get(name, default)
|
2013-11-07 07:14:53 +00:00
|
|
|
|
2010-02-07 15:11:24 +00:00
|
|
|
def get_any(self, names, default=None):
|
|
|
|
for name in names:
|
|
|
|
if name in self.attrs:
|
|
|
|
return self.attrs[name]
|
|
|
|
return default
|
2009-10-24 04:41:59 +00:00
|
|
|
|
2010-04-10 11:05:02 +00:00
|
|
|
def get_filters(self):
|
|
|
|
filters = self.get_any(('F', 'Filter'))
|
2014-07-09 10:07:27 +00:00
|
|
|
params = self.get_any(('DP', 'DecodeParms', 'FDecodeParms'), {})
|
2013-11-07 08:35:04 +00:00
|
|
|
if not filters:
|
|
|
|
return []
|
2014-07-09 10:07:27 +00:00
|
|
|
if not isinstance(filters, list):
|
|
|
|
filters = [filters]
|
|
|
|
if not isinstance(params, list):
|
2016-09-23 12:57:28 +00:00
|
|
|
# Make sure the parameters list is the same as filters.
|
2016-09-23 12:31:31 +00:00
|
|
|
params = [params] * len(filters)
|
2016-09-24 10:03:22 +00:00
|
|
|
if settings.STRICT and len(params) != len(filters):
|
2016-09-23 12:57:28 +00:00
|
|
|
raise PDFException("Parameters len filter mismatch")
|
2014-07-09 10:07:27 +00:00
|
|
|
return zip(filters, params)
|
2010-04-10 11:05:02 +00:00
|
|
|
|
2009-10-24 04:41:59 +00:00
|
|
|
def decode(self):
|
2013-11-07 08:35:04 +00:00
|
|
|
assert self.data is None and self.rawdata is not None
|
2009-10-24 04:41:59 +00:00
|
|
|
data = self.rawdata
|
|
|
|
if self.decipher:
|
|
|
|
# Handle encryption
|
2014-05-19 14:27:43 +00:00
|
|
|
data = self.decipher(self.objid, self.genno, data, self.attrs)
|
2010-04-10 11:05:02 +00:00
|
|
|
filters = self.get_filters()
|
2010-02-07 15:11:24 +00:00
|
|
|
if not filters:
|
2010-02-15 14:50:19 +00:00
|
|
|
self.data = data
|
|
|
|
self.rawdata = None
|
2009-10-24 04:41:59 +00:00
|
|
|
return
|
2014-07-09 10:07:27 +00:00
|
|
|
for (f,params) in filters:
|
2009-10-24 04:41:59 +00:00
|
|
|
if f in LITERALS_FLATE_DECODE:
|
|
|
|
# will get errors if the document is encrypted.
|
2010-10-17 05:15:18 +00:00
|
|
|
try:
|
|
|
|
data = zlib.decompress(data)
|
2014-06-16 09:50:07 +00:00
|
|
|
except zlib.error as e:
|
2016-01-10 17:17:38 +00:00
|
|
|
if settings.STRICT:
|
2011-04-21 13:07:52 +00:00
|
|
|
raise PDFException('Invalid zlib bytes: %r, %r' % (e, data))
|
2014-06-30 10:05:56 +00:00
|
|
|
data = b''
|
2009-10-24 04:41:59 +00:00
|
|
|
elif f in LITERALS_LZW_DECODE:
|
2009-11-04 11:28:32 +00:00
|
|
|
data = lzwdecode(data)
|
2009-10-24 04:41:59 +00:00
|
|
|
elif f in LITERALS_ASCII85_DECODE:
|
2009-11-04 11:28:32 +00:00
|
|
|
data = ascii85decode(data)
|
2009-10-24 04:41:59 +00:00
|
|
|
elif f in LITERALS_ASCIIHEX_DECODE:
|
2009-11-04 11:28:32 +00:00
|
|
|
data = asciihexdecode(data)
|
2009-12-24 11:51:43 +00:00
|
|
|
elif f in LITERALS_RUNLENGTH_DECODE:
|
|
|
|
data = rldecode(data)
|
2010-02-13 15:00:43 +00:00
|
|
|
elif f in LITERALS_CCITTFAX_DECODE:
|
2011-07-18 11:07:00 +00:00
|
|
|
data = ccittfaxdecode(data, params)
|
2014-05-14 05:25:30 +00:00
|
|
|
elif f in LITERALS_DCT_DECODE:
|
|
|
|
# This is probably a JPG stream - it does not need to be decoded twice.
|
|
|
|
# Just return the stream to the user.
|
|
|
|
pass
|
2009-10-24 04:41:59 +00:00
|
|
|
elif f == LITERAL_CRYPT:
|
2009-12-24 11:51:43 +00:00
|
|
|
# not yet..
|
2009-10-24 04:41:59 +00:00
|
|
|
raise PDFNotImplementedError('/Crypt filter is unsupported')
|
|
|
|
else:
|
|
|
|
raise PDFNotImplementedError('Unsupported filter: %r' % f)
|
|
|
|
# apply predictors
|
2011-06-06 15:31:03 +00:00
|
|
|
if 'Predictor' in params:
|
2009-10-24 04:41:59 +00:00
|
|
|
pred = int_value(params['Predictor'])
|
2011-06-06 15:31:03 +00:00
|
|
|
if pred == 1:
|
|
|
|
# no predictor
|
|
|
|
pass
|
|
|
|
elif 10 <= pred:
|
|
|
|
# PNG predictor
|
|
|
|
colors = int_value(params.get('Colors', 1))
|
|
|
|
columns = int_value(params.get('Columns', 1))
|
|
|
|
bitspercomponent = int_value(params.get('BitsPerComponent', 8))
|
|
|
|
data = apply_png_predictor(pred, colors, columns, bitspercomponent, data)
|
|
|
|
else:
|
|
|
|
raise PDFNotImplementedError('Unsupported predictor: %r' % pred)
|
2009-10-24 04:41:59 +00:00
|
|
|
self.data = data
|
|
|
|
self.rawdata = None
|
|
|
|
return
|
|
|
|
|
|
|
|
def get_data(self):
|
2009-11-06 15:10:29 +00:00
|
|
|
if self.data is None:
|
2009-10-24 04:41:59 +00:00
|
|
|
self.decode()
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
def get_rawdata(self):
|
|
|
|
return self.rawdata
|