/usr/lib/python2.7/dist-packages/pdfrw/uncompress.py is in python-pdfrw 0.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | # A part of pdfrw (https://github.com/pmaupin/pdfrw)
# Copyright (C) 2006-2015 Patrick Maupin, Austin, Texas
# Copyright (C) 2012-2015 Nerijus Mika
# MIT license -- See LICENSE.txt for details
# Copyright (c) 2006, Mathieu Fenniak
# BSD license -- see LICENSE.txt for details
'''
A small subset of decompression filters. Should add more later.
I believe, after looking at the code, that portions of the flate
PNG predictor were originally transcribed from PyPDF2, which is
probably an excellent source of additional filters.
'''
import array
from .objects import PdfDict, PdfName
from .errors import log
from .py23_diffs import zlib, xrange, from_array, convert_load, convert_store
def streamobjects(mylist, isinstance=isinstance, PdfDict=PdfDict):
for obj in mylist:
if isinstance(obj, PdfDict) and obj.stream is not None:
yield obj
# Hack so we can import if zlib not available
decompressobj = zlib if zlib is None else zlib.decompressobj
def uncompress(mylist, leave_raw=False, warnings=set(),
flate=PdfName.FlateDecode, decompress=decompressobj,
isinstance=isinstance, list=list, len=len):
ok = True
for obj in streamobjects(mylist):
ftype = obj.Filter
if ftype is None:
continue
if isinstance(ftype, list) and len(ftype) == 1:
# todo: multiple filters
ftype = ftype[0]
parms = obj.DecodeParms
if ftype != flate:
msg = ('Not decompressing: cannot use filter %s'
' with parameters %s') % (repr(ftype), repr(parms))
if msg not in warnings:
warnings.add(msg)
log.warning(msg)
ok = False
else:
dco = decompress()
try:
data = dco.decompress(convert_store(obj.stream))
except Exception as s:
error = str(s)
else:
error = None
if parms:
predictor = int(parms.Predictor or 1)
if 10 <= predictor <= 15:
data, error = flate_png(data, parms)
elif predictor != 1:
error = ('Unsupported flatedecode predictor %s' %
repr(predictor))
if error is None:
assert not dco.unconsumed_tail
if dco.unused_data.strip():
error = ('Unconsumed compression data: %s' %
repr(dco.unused_data[:20]))
if error is None:
obj.Filter = None
obj.stream = data if leave_raw else convert_load(data)
else:
log.error('%s %s' % (error, repr(obj.indirect)))
ok = False
return ok
def flate_png(data, parms):
''' PNG prediction is used to make certain kinds of data
more compressible. Before the compression, each data
byte is either left the same, or is set to be a delta
from the previous byte, or is set to be a delta from
the previous row. This selection is done on a per-row
basis, and is indicated by a compression type byte
prepended to each row of data.
Within more recent PDF files, it is normal to use
this technique for Xref stream objects, which are
quite regular.
'''
columns = int(parms.Columns)
data = array.array('B', data)
rowlen = columns + 1
assert len(data) % rowlen == 0
rows = xrange(0, len(data), rowlen)
for row_index in rows:
offset = data[row_index]
if offset >= 2:
if offset > 2:
return None, 'Unsupported PNG filter %d' % offset
offset = rowlen if row_index else 0
if offset:
for index in xrange(row_index + 1, row_index + rowlen):
data[index] = (data[index] + data[index - offset]) % 256
for row_index in reversed(rows):
data.pop(row_index)
return from_array(data), None
|