pdfreader 0.1.14__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pdfreader/__init__.py +8 -0
- pdfreader/buffer.py +188 -0
- pdfreader/codecs/__init__.py +13 -0
- pdfreader/codecs/agl.py +4289 -0
- pdfreader/codecs/cmaps/Adobe-CNS1-0 +134 -0
- pdfreader/codecs/cmaps/Adobe-CNS1-3 +151 -0
- pdfreader/codecs/cmaps/Adobe-CNS1-4 +152 -0
- pdfreader/codecs/cmaps/Adobe-GB1-0 +109 -0
- pdfreader/codecs/cmaps/Adobe-GB1-2 +165 -0
- pdfreader/codecs/cmaps/Adobe-GB1-4 +195 -0
- pdfreader/codecs/cmaps/Adobe-Japan1-1 +111 -0
- pdfreader/codecs/cmaps/Adobe-Japan1-2 +113 -0
- pdfreader/codecs/cmaps/Adobe-Japan1-4 +138 -0
- pdfreader/codecs/cmaps/Adobe-Japan1-5 +157 -0
- pdfreader/codecs/cmaps/Adobe-Korea1-0 +114 -0
- pdfreader/codecs/cmaps/Adobe-Korea1-1 +149 -0
- pdfreader/codecs/cmaps/Adobe-Korea1-2 +149 -0
- pdfreader/codecs/cmaps/Identity-H +337 -0
- pdfreader/codecs/cmaps/README.md +1 -0
- pdfreader/codecs/codec.py +137 -0
- pdfreader/codecs/decoder.py +195 -0
- pdfreader/codecs/differences.py +75 -0
- pdfreader/codecs/macroman.py +69 -0
- pdfreader/codecs/pdfdoc.py +78 -0
- pdfreader/codecs/standard.py +50 -0
- pdfreader/codecs/tests.py +22 -0
- pdfreader/codecs/winansi.py +72 -0
- pdfreader/codecs/zapfdingbatsgl.py +205 -0
- pdfreader/constants.py +128 -0
- pdfreader/document.py +169 -0
- pdfreader/exceptions.py +12 -0
- pdfreader/filters/__init__.py +20 -0
- pdfreader/filters/ascii85.py +38 -0
- pdfreader/filters/asciihex.py +54 -0
- pdfreader/filters/ccittfax.py +554 -0
- pdfreader/filters/crypt.py +5 -0
- pdfreader/filters/dct.py +5 -0
- pdfreader/filters/flate.py +32 -0
- pdfreader/filters/jbig2.py +5 -0
- pdfreader/filters/jpx.py +5 -0
- pdfreader/filters/lzw.py +304 -0
- pdfreader/filters/predictors.py +33 -0
- pdfreader/filters/runlength.py +55 -0
- pdfreader/filters/test_ccittf_fax.py +144 -0
- pdfreader/filters/tests.py +22 -0
- pdfreader/parsers/__init__.py +4 -0
- pdfreader/parsers/base.py +741 -0
- pdfreader/parsers/cmap.py +268 -0
- pdfreader/parsers/content.py +88 -0
- pdfreader/parsers/document.py +627 -0
- pdfreader/parsers/inlineimage.py +73 -0
- pdfreader/parsers/objstm.py +16 -0
- pdfreader/parsers/tests.py +24 -0
- pdfreader/pillow.py +113 -0
- pdfreader/registry.py +48 -0
- pdfreader/securityhandler.py +333 -0
- pdfreader/tests.py +18 -0
- pdfreader/types/__init__.py +7 -0
- pdfreader/types/cmap.py +395 -0
- pdfreader/types/constants.py +235 -0
- pdfreader/types/content.py +92 -0
- pdfreader/types/filestructure.py +52 -0
- pdfreader/types/native.py +198 -0
- pdfreader/types/objects.py +523 -0
- pdfreader/types/tests.py +20 -0
- pdfreader/types/xref.py +165 -0
- pdfreader/utils.py +57 -0
- pdfreader/viewer/__init__.py +5 -0
- pdfreader/viewer/canvas.py +61 -0
- pdfreader/viewer/graphicsstate.py +99 -0
- pdfreader/viewer/pdfviewer.py +331 -0
- pdfreader/viewer/resources.py +64 -0
- pdfreader/viewer/simple.py +233 -0
- pdfreader/viewer/tests.py +20 -0
- pdfreader-0.1.14.dist-info/LICENSE +21 -0
- pdfreader-0.1.14.dist-info/METADATA +121 -0
- pdfreader-0.1.14.dist-info/RECORD +79 -0
- pdfreader-0.1.14.dist-info/WHEEL +5 -0
- pdfreader-0.1.14.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,741 @@
|
|
|
1
|
+
from ..buffer import Buffer
|
|
2
|
+
from ..constants import WHITESPACES, EOL, DELIMITERS, CR, LF, STRING_ESCAPED, DEFAULT_ENCODING
|
|
3
|
+
from ..types import *
|
|
4
|
+
from ..exceptions import ParserException
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class BasicTypesParser(object):
|
|
8
|
+
""" can parse basic PDF types """
|
|
9
|
+
|
|
10
|
+
exception_class = ParserException
|
|
11
|
+
indirect_references_allowed = True
|
|
12
|
+
empty_names_allowed = True
|
|
13
|
+
|
|
14
|
+
def __init__(self, fileobj_or_buffer, offset=0):
|
|
15
|
+
if isinstance(fileobj_or_buffer, Buffer):
|
|
16
|
+
self.buffer = fileobj_or_buffer
|
|
17
|
+
else:
|
|
18
|
+
self.buffer = Buffer(fileobj_or_buffer, offset)
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def current(self):
|
|
22
|
+
return self.buffer.current
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def is_eof(self):
|
|
26
|
+
return self.buffer.is_eof
|
|
27
|
+
|
|
28
|
+
def next(self):
|
|
29
|
+
return self.buffer.next()
|
|
30
|
+
|
|
31
|
+
def prev(self):
|
|
32
|
+
return self.buffer.prev()
|
|
33
|
+
|
|
34
|
+
def read(self, n):
|
|
35
|
+
return self.buffer.read(n)
|
|
36
|
+
|
|
37
|
+
def read_backward(self, n):
|
|
38
|
+
return self.buffer.read_backward(n)
|
|
39
|
+
|
|
40
|
+
def get_state(self):
|
|
41
|
+
return self.buffer.get_state()
|
|
42
|
+
|
|
43
|
+
def set_state(self, state):
|
|
44
|
+
return self.buffer.set_state(state)
|
|
45
|
+
|
|
46
|
+
def reset(self, n):
|
|
47
|
+
return self.buffer.reset(n)
|
|
48
|
+
|
|
49
|
+
def on_parser_error(self, message):
|
|
50
|
+
# ToDo: display parsing context here
|
|
51
|
+
raise self.exception_class(message)
|
|
52
|
+
|
|
53
|
+
def spaces(self):
|
|
54
|
+
if self.is_whitespace:
|
|
55
|
+
self.on_parser_error("Whitespace character expected")
|
|
56
|
+
self.maybe_spaces()
|
|
57
|
+
|
|
58
|
+
def maybe_spaces(self):
|
|
59
|
+
while self.is_whitespace:
|
|
60
|
+
self.next()
|
|
61
|
+
|
|
62
|
+
def maybe_spaces_or_comments(self):
|
|
63
|
+
self.maybe_spaces()
|
|
64
|
+
comments = []
|
|
65
|
+
res = ''
|
|
66
|
+
while self.current == b'%':
|
|
67
|
+
comments.append(self.comment())
|
|
68
|
+
self.maybe_spaces()
|
|
69
|
+
if comments:
|
|
70
|
+
# return multiline comment
|
|
71
|
+
res = Comment("\n".join(comments))
|
|
72
|
+
return res
|
|
73
|
+
|
|
74
|
+
def eol(self):
|
|
75
|
+
""" EOL is either CR or LF or the both """
|
|
76
|
+
if self.current not in EOL:
|
|
77
|
+
self.on_parser_error("EOL expected")
|
|
78
|
+
self.maybe_eol()
|
|
79
|
+
|
|
80
|
+
def maybe_eol(self):
|
|
81
|
+
""" EOL is either CR or LF or the both except for the streams """
|
|
82
|
+
if self.current == CR:
|
|
83
|
+
self.next()
|
|
84
|
+
if self.current == LF:
|
|
85
|
+
self.next()
|
|
86
|
+
elif self.current == LF:
|
|
87
|
+
self.next()
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def is_eol(self):
|
|
91
|
+
return self.current is not None and self.current in EOL
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def is_whitespace(self):
|
|
95
|
+
return self.current is not None and self.current in WHITESPACES
|
|
96
|
+
|
|
97
|
+
@property
|
|
98
|
+
def is_delimiter(self):
|
|
99
|
+
return self.current is not None and self.current in DELIMITERS
|
|
100
|
+
|
|
101
|
+
@property
|
|
102
|
+
def is_regular(self):
|
|
103
|
+
return self.current is not None and not (self.is_whitespace or self.is_delimiter)
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def is_digit(self):
|
|
107
|
+
return self.current is not None and self.current in b'0123456789'
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def is_hex_digit(self):
|
|
111
|
+
return self.current is not None and self.current in b'0123456789ABCDEFabcdef'
|
|
112
|
+
|
|
113
|
+
def null(self):
|
|
114
|
+
"""
|
|
115
|
+
null token
|
|
116
|
+
|
|
117
|
+
>>> s = b'null'
|
|
118
|
+
>>> BasicTypesParser(s, 0).null() is null
|
|
119
|
+
True
|
|
120
|
+
|
|
121
|
+
>>> s = b'none'
|
|
122
|
+
>>> BasicTypesParser(s, 0).null()
|
|
123
|
+
Traceback (most recent call last):
|
|
124
|
+
...
|
|
125
|
+
pdfreader.exceptions.ParserException: null token expected
|
|
126
|
+
"""
|
|
127
|
+
val = self.read(4)
|
|
128
|
+
if val != b'null':
|
|
129
|
+
self.on_parser_error("null token expected")
|
|
130
|
+
return null
|
|
131
|
+
|
|
132
|
+
def true(self):
|
|
133
|
+
"""
|
|
134
|
+
true token
|
|
135
|
+
|
|
136
|
+
>>> s = b'true'
|
|
137
|
+
>>> BasicTypesParser(s, 0).true()
|
|
138
|
+
True
|
|
139
|
+
|
|
140
|
+
>>> s = b'True'
|
|
141
|
+
>>> BasicTypesParser(s, 0).true()
|
|
142
|
+
Traceback (most recent call last):
|
|
143
|
+
...
|
|
144
|
+
pdfreader.exceptions.ParserException: true token expected
|
|
145
|
+
"""
|
|
146
|
+
val = self.read(4)
|
|
147
|
+
if val != b'true':
|
|
148
|
+
self.on_parser_error("true token expected")
|
|
149
|
+
return True
|
|
150
|
+
|
|
151
|
+
def false(self):
|
|
152
|
+
"""
|
|
153
|
+
false token
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
>>> s = b'false'
|
|
157
|
+
>>> BasicTypesParser(s, 0).false()
|
|
158
|
+
False
|
|
159
|
+
|
|
160
|
+
>>> s = b'False'
|
|
161
|
+
>>> BasicTypesParser(s, 0).false()
|
|
162
|
+
Traceback (most recent call last):
|
|
163
|
+
...
|
|
164
|
+
pdfreader.exceptions.ParserException: false token expected
|
|
165
|
+
"""
|
|
166
|
+
val = self.read(5)
|
|
167
|
+
if val != b'false':
|
|
168
|
+
self.on_parser_error("false token expected")
|
|
169
|
+
return False
|
|
170
|
+
|
|
171
|
+
def comment(self):
|
|
172
|
+
"""
|
|
173
|
+
|
|
174
|
+
>>> s = b'%any occurance of %-sign outside of string or stream until EOL (not including) is a comment\\n'
|
|
175
|
+
>>> BasicTypesParser(s, 0).comment()
|
|
176
|
+
'%any occurance of %-sign outside of string or stream until EOL (not including) is a comment'
|
|
177
|
+
|
|
178
|
+
Empty comment
|
|
179
|
+
>>> s = b'%\\n'
|
|
180
|
+
>>> BasicTypesParser(s, 0).comment()
|
|
181
|
+
'%'
|
|
182
|
+
"""
|
|
183
|
+
if self.current != b"%":
|
|
184
|
+
self.on_parser_error("% expected")
|
|
185
|
+
line = b""
|
|
186
|
+
while not self.is_eol:
|
|
187
|
+
line += self.next()
|
|
188
|
+
self.eol()
|
|
189
|
+
return Comment(line.decode(DEFAULT_ENCODING))
|
|
190
|
+
|
|
191
|
+
def numeric(self):
|
|
192
|
+
"""
|
|
193
|
+
|
|
194
|
+
>>> s = b'0'
|
|
195
|
+
>>> BasicTypesParser(s, 0).numeric()
|
|
196
|
+
0
|
|
197
|
+
|
|
198
|
+
>>> s = b'123'
|
|
199
|
+
>>> BasicTypesParser(s, 0).numeric()
|
|
200
|
+
123
|
|
201
|
+
|
|
202
|
+
>>> s = b'+123'
|
|
203
|
+
>>> BasicTypesParser(s, 0).numeric()
|
|
204
|
+
123
|
|
205
|
+
|
|
206
|
+
>>> s = b'-123'
|
|
207
|
+
>>> BasicTypesParser(s, 0).numeric()
|
|
208
|
+
-123
|
|
209
|
+
|
|
210
|
+
>>> s = b'-3.5'
|
|
211
|
+
>>> BasicTypesParser(s, 0).numeric()
|
|
212
|
+
Decimal('-3.5')
|
|
213
|
+
|
|
214
|
+
>>> s = b'+3.0'
|
|
215
|
+
>>> BasicTypesParser(s, 0).numeric()
|
|
216
|
+
Decimal('3.0')
|
|
217
|
+
|
|
218
|
+
>>> s = b'.01'
|
|
219
|
+
>>> BasicTypesParser(s, 0).numeric()
|
|
220
|
+
Decimal('0.01')
|
|
221
|
+
|
|
222
|
+
>>> s = b'+.01'
|
|
223
|
+
>>> BasicTypesParser(s, 0).numeric()
|
|
224
|
+
Decimal('0.01')
|
|
225
|
+
|
|
226
|
+
>>> s = b'-.01'
|
|
227
|
+
>>> BasicTypesParser(s, 0).numeric()
|
|
228
|
+
Decimal('-0.01')
|
|
229
|
+
|
|
230
|
+
"""
|
|
231
|
+
is_negative = False
|
|
232
|
+
is_integer = True
|
|
233
|
+
if self.current == b"+":
|
|
234
|
+
self.next()
|
|
235
|
+
elif self.current == b"-":
|
|
236
|
+
is_negative = True
|
|
237
|
+
self.next()
|
|
238
|
+
ipart, fpart = b'', b''
|
|
239
|
+
|
|
240
|
+
# read integer part
|
|
241
|
+
while self.is_digit:
|
|
242
|
+
ipart += self.current
|
|
243
|
+
self.next()
|
|
244
|
+
|
|
245
|
+
# read point if exists
|
|
246
|
+
if self.current == b'.':
|
|
247
|
+
is_integer = False
|
|
248
|
+
self.next()
|
|
249
|
+
while self.is_digit:
|
|
250
|
+
fpart += self.next()
|
|
251
|
+
|
|
252
|
+
if not ipart and not fpart:
|
|
253
|
+
self.on_parser_error("Invalid numeric token")
|
|
254
|
+
|
|
255
|
+
if not ipart:
|
|
256
|
+
ipart = b'0'
|
|
257
|
+
if not fpart:
|
|
258
|
+
fpart = b'0'
|
|
259
|
+
|
|
260
|
+
if is_integer:
|
|
261
|
+
val = int(ipart.decode(DEFAULT_ENCODING))
|
|
262
|
+
else:
|
|
263
|
+
val = Decimal("{}.{}".format(ipart.decode(DEFAULT_ENCODING), fpart.decode(DEFAULT_ENCODING)))
|
|
264
|
+
|
|
265
|
+
if is_negative:
|
|
266
|
+
val = -val
|
|
267
|
+
|
|
268
|
+
return val
|
|
269
|
+
|
|
270
|
+
def non_negative_int(self):
|
|
271
|
+
n = self.numeric()
|
|
272
|
+
if not isinstance(n, Integer) or n < 0:
|
|
273
|
+
self.on_parser_error("Non negative int expected")
|
|
274
|
+
return n
|
|
275
|
+
|
|
276
|
+
def name(self):
|
|
277
|
+
"""
|
|
278
|
+
>>> s = b'/Name'
|
|
279
|
+
>>> BasicTypesParser(s, 0).name()
|
|
280
|
+
'Name'
|
|
281
|
+
|
|
282
|
+
>>> s = b'/Name#20with#20spaces'
|
|
283
|
+
>>> BasicTypesParser(s, 0).name()
|
|
284
|
+
'Name with spaces'
|
|
285
|
+
|
|
286
|
+
>>> s = b'/Name#with!^speci_#0_als#'
|
|
287
|
+
>>> BasicTypesParser(s, 0).name()
|
|
288
|
+
'Name#with!^speci_#0_als#'
|
|
289
|
+
|
|
290
|
+
>>> s = b'/'
|
|
291
|
+
>>> BasicTypesParser(s, 0).name()
|
|
292
|
+
''
|
|
293
|
+
|
|
294
|
+
>>> s = b'/'
|
|
295
|
+
>>> p = BasicTypesParser(s, 0)
|
|
296
|
+
>>> p.empty_names_allowed = False
|
|
297
|
+
>>> p.name()
|
|
298
|
+
Traceback (most recent call last):
|
|
299
|
+
...
|
|
300
|
+
pdfreader.exceptions.ParserException: Empty /Name found
|
|
301
|
+
|
|
302
|
+
>>> s = b'Name'
|
|
303
|
+
>>> BasicTypesParser(s, 0).name()
|
|
304
|
+
Traceback (most recent call last):
|
|
305
|
+
...
|
|
306
|
+
pdfreader.exceptions.ParserException: Name token expected
|
|
307
|
+
"""
|
|
308
|
+
if self.current != b'/':
|
|
309
|
+
self.on_parser_error("Name token expected")
|
|
310
|
+
token = b''
|
|
311
|
+
self.next()
|
|
312
|
+
while self.is_regular:
|
|
313
|
+
if self.current == b'#':
|
|
314
|
+
self.next()
|
|
315
|
+
code = b''
|
|
316
|
+
for i in range(2):
|
|
317
|
+
if not self.is_hex_digit:
|
|
318
|
+
break
|
|
319
|
+
code += self.next()
|
|
320
|
+
if len(code) == 2:
|
|
321
|
+
# must be exactly 2 characters
|
|
322
|
+
token += chr(int(code.decode(DEFAULT_ENCODING), 16)).encode(DEFAULT_ENCODING)
|
|
323
|
+
else:
|
|
324
|
+
# leave as is
|
|
325
|
+
token += b'#' + code
|
|
326
|
+
else:
|
|
327
|
+
token += self.next()
|
|
328
|
+
if not self.empty_names_allowed and not token:
|
|
329
|
+
self.on_parser_error("Empty /Name found")
|
|
330
|
+
|
|
331
|
+
return Name(token.decode(DEFAULT_ENCODING))
|
|
332
|
+
|
|
333
|
+
def dictionary_or_stream_or_hexstring(self):
|
|
334
|
+
if self.current != b"<":
|
|
335
|
+
self.on_parser_error("Dict or hexstring expected")
|
|
336
|
+
self.next()
|
|
337
|
+
val = None
|
|
338
|
+
if self.current == b"<":
|
|
339
|
+
self.prev()
|
|
340
|
+
val = self.dictionary()
|
|
341
|
+
# stream may come after the dict
|
|
342
|
+
self.maybe_spaces_or_comments()
|
|
343
|
+
if self.current == b's':
|
|
344
|
+
val = self._stream(val)
|
|
345
|
+
elif self.is_hex_digit or self.is_whitespace or self.current == b'>':
|
|
346
|
+
# < 09FF> and <> are possible
|
|
347
|
+
self.prev()
|
|
348
|
+
val = self.hexstring()
|
|
349
|
+
else:
|
|
350
|
+
self.on_parser_error("Dict, stream or hexstring expected")
|
|
351
|
+
return val
|
|
352
|
+
|
|
353
|
+
def dictionary(self):
|
|
354
|
+
"""
|
|
355
|
+
|
|
356
|
+
>>> s = b'<<>>'
|
|
357
|
+
>>> BasicTypesParser(s, 0).dictionary()
|
|
358
|
+
{}
|
|
359
|
+
|
|
360
|
+
>>> s = b'''<< /Type /Example
|
|
361
|
+
... /Subtype /DictExample
|
|
362
|
+
... /Version 0.01
|
|
363
|
+
... /IntegerItem 12
|
|
364
|
+
... /StringItem (a string)
|
|
365
|
+
... /ArrayItem [1 2]
|
|
366
|
+
... /ObjRef 12 0 R
|
|
367
|
+
... /SubDictionary <<
|
|
368
|
+
... /Item1 true
|
|
369
|
+
... /Item2 false
|
|
370
|
+
... /Item3 null
|
|
371
|
+
... /Item4 (OK)
|
|
372
|
+
... >>
|
|
373
|
+
... >>'''
|
|
374
|
+
>>> expected = {'Type': 'Example', 'Subtype': 'DictExample', 'Version': Decimal('0.01'), 'IntegerItem': 12,
|
|
375
|
+
... 'StringItem': b'a string', 'ArrayItem': [1, 2], 'ObjRef': IndirectReference(12, 0),
|
|
376
|
+
... 'SubDictionary': {'Item1': True, 'Item2': False, 'Item3': None, 'Item4': b'OK'}}
|
|
377
|
+
>>> BasicTypesParser(s, 0).dictionary() == expected
|
|
378
|
+
True
|
|
379
|
+
|
|
380
|
+
"""
|
|
381
|
+
pfx = self.read(2)
|
|
382
|
+
if pfx != b'<<':
|
|
383
|
+
self.on_parser_error("Dictionary expected")
|
|
384
|
+
res = {}
|
|
385
|
+
self.maybe_spaces_or_comments()
|
|
386
|
+
while self.current != b'>':
|
|
387
|
+
key = self.name()
|
|
388
|
+
self.maybe_spaces_or_comments()
|
|
389
|
+
res[key] = self.object()
|
|
390
|
+
self.maybe_spaces_or_comments()
|
|
391
|
+
self.next()
|
|
392
|
+
if self.next() != b'>':
|
|
393
|
+
self.on_parser_error("End of dictionary >> expected ")
|
|
394
|
+
return res
|
|
395
|
+
|
|
396
|
+
def stream(self):
|
|
397
|
+
"""
|
|
398
|
+
Parses stream (dict + binary data)
|
|
399
|
+
|
|
400
|
+
>>> s = b'''<<
|
|
401
|
+
... /Length 10
|
|
402
|
+
... >>
|
|
403
|
+
... stream\\r\\n***data***\\nendstream'''
|
|
404
|
+
>>> BasicTypesParser(s, 0).stream()
|
|
405
|
+
<Stream:len=10,data=b'***data***'>
|
|
406
|
+
|
|
407
|
+
"""
|
|
408
|
+
d = self.dictionary()
|
|
409
|
+
# binary data comes after dict
|
|
410
|
+
self.maybe_spaces_or_comments()
|
|
411
|
+
return self._stream(d)
|
|
412
|
+
|
|
413
|
+
def _stream(self, d):
|
|
414
|
+
"""
|
|
415
|
+
Parses binary data which is part of Stream object itself
|
|
416
|
+
|
|
417
|
+
>>> d = dict(Length=10)
|
|
418
|
+
|
|
419
|
+
>>> s = b'stream\\r\\n***data***\\nendstream'
|
|
420
|
+
>>> BasicTypesParser(s, 0)._stream(d).stream
|
|
421
|
+
b'***data***'
|
|
422
|
+
|
|
423
|
+
>>> s = b'stream\\n***data***\\r\\nendstream'
|
|
424
|
+
>>> BasicTypesParser(s, 0)._stream(d).stream
|
|
425
|
+
b'***data***'
|
|
426
|
+
|
|
427
|
+
>>> s = b'stream\\n***data***\\rendstream'
|
|
428
|
+
>>> BasicTypesParser(s, 0)._stream(d).stream
|
|
429
|
+
b'***data***'
|
|
430
|
+
|
|
431
|
+
Work around wrong length. See https://github.com/maxpmaxp/pdfreader/issues/68
|
|
432
|
+
1. Length greater than real data length
|
|
433
|
+
>>> d = dict(Length=2)
|
|
434
|
+
>>> s = b'''stream\\n\\nendstream\\n\\n\\n\\n\\n\\n'''
|
|
435
|
+
>>> BasicTypesParser(s, 0)._stream(d).stream
|
|
436
|
+
b''
|
|
437
|
+
|
|
438
|
+
2. Length less than real data length
|
|
439
|
+
>>> d = dict(Length=1)
|
|
440
|
+
>>> s = b'''stream\\n***data***\\nendstream\\n\\n\\n\\n\\n'''
|
|
441
|
+
>>> BasicTypesParser(s, 0)._stream(d).stream
|
|
442
|
+
b'***data***'
|
|
443
|
+
"""
|
|
444
|
+
length = d['Length']
|
|
445
|
+
token = self.read(6)
|
|
446
|
+
if token != b'stream':
|
|
447
|
+
self.on_parser_error("stream expected")
|
|
448
|
+
# `stream` keyword must be followed by CR+LF or by LF, but NOT by CR alone
|
|
449
|
+
ch = self.next()
|
|
450
|
+
if ch == CR:
|
|
451
|
+
ch = self.next()
|
|
452
|
+
if ch != LF:
|
|
453
|
+
logging.warning("Missing LF after `stream` token - [CR]LF expected. Trying to proceed.")
|
|
454
|
+
self.prev()
|
|
455
|
+
|
|
456
|
+
state = self.get_state()
|
|
457
|
+
|
|
458
|
+
data = self.read(length)
|
|
459
|
+
# According to the spec EOL should be after the data and before endstream
|
|
460
|
+
# But some files do not follow this.
|
|
461
|
+
#
|
|
462
|
+
# See data/leesoil-cases-2.pdf
|
|
463
|
+
#
|
|
464
|
+
# self.eol()
|
|
465
|
+
self.maybe_spaces()
|
|
466
|
+
token = self.read(9)
|
|
467
|
+
if token != b'endstream':
|
|
468
|
+
# Work around wrong length. See https://github.com/maxpmaxp/pdfreader/issues/68
|
|
469
|
+
err_state = self.get_state()
|
|
470
|
+
logging.warning("Wrong stream length: {}. Trying to work around the issue.".format(length))
|
|
471
|
+
self.set_state(state)
|
|
472
|
+
data = self.read(9)
|
|
473
|
+
while not data.endswith(b'endstream'):
|
|
474
|
+
ch = self.next()
|
|
475
|
+
if ch is None:
|
|
476
|
+
self.set_state(err_state)
|
|
477
|
+
self.on_parser_error("endstream expected")
|
|
478
|
+
data += ch
|
|
479
|
+
|
|
480
|
+
data = data[:-9]
|
|
481
|
+
while data and data[-1:] in EOL:
|
|
482
|
+
data = data[:-1]
|
|
483
|
+
|
|
484
|
+
return Stream(d, data)
|
|
485
|
+
|
|
486
|
+
def hexstring(self):
|
|
487
|
+
"""
|
|
488
|
+
>>> s = b'<01020a0B>'
|
|
489
|
+
>>> BasicTypesParser(s, 0).hexstring()
|
|
490
|
+
'01020A0B'
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
>>> s = b'<0>'
|
|
494
|
+
>>> BasicTypesParser(s, 0).hexstring()
|
|
495
|
+
'00'
|
|
496
|
+
|
|
497
|
+
>>> s = b'<01 AA FF 1>'
|
|
498
|
+
>>> BasicTypesParser(s, 0).hexstring()
|
|
499
|
+
'01AAFF10'
|
|
500
|
+
|
|
501
|
+
>>> s = b'<>'
|
|
502
|
+
>>> BasicTypesParser(s, 0).hexstring()
|
|
503
|
+
''
|
|
504
|
+
|
|
505
|
+
>>> s = b'<0011XX>'
|
|
506
|
+
>>> BasicTypesParser(s, 0).hexstring()
|
|
507
|
+
Traceback (most recent call last):
|
|
508
|
+
...
|
|
509
|
+
pdfreader.exceptions.ParserException: Wrong hexadecimal string
|
|
510
|
+
|
|
511
|
+
"""
|
|
512
|
+
if self.current != b"<":
|
|
513
|
+
self.on_parser_error("Hexadecimal string expected")
|
|
514
|
+
self.next()
|
|
515
|
+
token = b''
|
|
516
|
+
self.maybe_spaces_or_comments()
|
|
517
|
+
while self.is_hex_digit:
|
|
518
|
+
token += self.next()
|
|
519
|
+
self.maybe_spaces_or_comments()
|
|
520
|
+
|
|
521
|
+
ch = self.next()
|
|
522
|
+
if ch != b'>':
|
|
523
|
+
self.on_parser_error("Wrong hexadecimal string")
|
|
524
|
+
if len(token) % 2:
|
|
525
|
+
# if there is an odd number of digits - the last one should be assumed 0
|
|
526
|
+
token += b'0'
|
|
527
|
+
return HexString(token.decode(DEFAULT_ENCODING).upper())
|
|
528
|
+
|
|
529
|
+
def array(self):
|
|
530
|
+
"""
|
|
531
|
+
|
|
532
|
+
>>> s = b'[]'
|
|
533
|
+
>>> BasicTypesParser(s, 0).array()
|
|
534
|
+
[]
|
|
535
|
+
|
|
536
|
+
>>> s = b'[-1.5 <AABBCC> (Regular string) <</Name /Value>> 0 10 5 R]'
|
|
537
|
+
>>> BasicTypesParser(s, 0).array()
|
|
538
|
+
[Decimal('-1.5'), 'AABBCC', b'Regular string', {'Name': 'Value'}, 0, <IndirectReference:n=10,g=5>]
|
|
539
|
+
|
|
540
|
+
"""
|
|
541
|
+
if self.current != b'[':
|
|
542
|
+
self.on_parser_error("Array expected")
|
|
543
|
+
self.next()
|
|
544
|
+
array = Array()
|
|
545
|
+
self.maybe_spaces_or_comments()
|
|
546
|
+
while self.current != b']':
|
|
547
|
+
array.append(self.object())
|
|
548
|
+
self.maybe_spaces_or_comments()
|
|
549
|
+
self.next() # skip enclosing bracket
|
|
550
|
+
return array
|
|
551
|
+
|
|
552
|
+
def string(self):
|
|
553
|
+
"""
|
|
554
|
+
The following are valid literal strings
|
|
555
|
+
|
|
556
|
+
>>> s = b'(This is a string)'
|
|
557
|
+
>>> BasicTypesParser(s, 0).string()
|
|
558
|
+
b'This is a string'
|
|
559
|
+
|
|
560
|
+
>>> s = b'''(Strings may contain newlines
|
|
561
|
+
... and such.)'''
|
|
562
|
+
>>> BasicTypesParser(s, 0).string()
|
|
563
|
+
b'Strings may contain newlines\\nand such.'
|
|
564
|
+
|
|
565
|
+
>>> s = b'''(Strings may contain balanced parenthesis () and special characters (*!&}^% and so on).)'''
|
|
566
|
+
>>> BasicTypesParser(s, 0).string()
|
|
567
|
+
b'Strings may contain balanced parenthesis () and special characters (*!&}^% and so on).'
|
|
568
|
+
|
|
569
|
+
Empty strings are allowed
|
|
570
|
+
>>> s = b'()'
|
|
571
|
+
>>> BasicTypesParser(s, 0).string()
|
|
572
|
+
b''
|
|
573
|
+
|
|
574
|
+
Multiline strings come with reverse solidus wollowed by CR, LF or the both.
|
|
575
|
+
>>> s = b'''(This is \\
|
|
576
|
+
... a multiline \\
|
|
577
|
+
... string)'''
|
|
578
|
+
>>> BasicTypesParser(s, 0).string()
|
|
579
|
+
b'This is a multiline string'
|
|
580
|
+
|
|
581
|
+
>>> s = b'(This string has escaped chars in it \\\\n\\\\r\\\\t\\\\b\\\\f\\\\(\\\\)\\\\\\\\)'
|
|
582
|
+
>>> BasicTypesParser(s, 0).string()
|
|
583
|
+
b'This string has escaped chars in it \\n\\r\\t\\x08\\x0c()\\\\'
|
|
584
|
+
|
|
585
|
+
>>> s = b'(This string contains 2 \\\\245octal characters\\\\307)'
|
|
586
|
+
>>> BasicTypesParser(s, 0).string()
|
|
587
|
+
b'This string contains 2 ¥octal charactersÇ'
|
|
588
|
+
|
|
589
|
+
>>> s = b'(The octal ddd may contain 1,2 or 3 octal digits: \\\\2,\\\\20,\\\\245)'
|
|
590
|
+
>>> BasicTypesParser(s, 0).string()
|
|
591
|
+
b'The octal ddd may contain 1,2 or 3 octal digits: \\x02,\\x10,¥'
|
|
592
|
+
|
|
593
|
+
>>> s = b'(\\\\0053 denotes 2 characters Ctl+E followed by the digit 3)'
|
|
594
|
+
>>> BasicTypesParser(s, 0).string()
|
|
595
|
+
b'\\x053 denotes 2 characters Ctl+E followed by the digit 3'
|
|
596
|
+
"""
|
|
597
|
+
|
|
598
|
+
if self.current != b'(':
|
|
599
|
+
self.on_parser_error("String expected")
|
|
600
|
+
val = b''
|
|
601
|
+
self.next()
|
|
602
|
+
while True:
|
|
603
|
+
ch = self.next()
|
|
604
|
+
if ch == b'(':
|
|
605
|
+
self.prev()
|
|
606
|
+
val += b"(" + self.string() + b")"
|
|
607
|
+
elif ch == b'\\':
|
|
608
|
+
ch = self.next()
|
|
609
|
+
if ch in b"01234567":
|
|
610
|
+
# octal code comes up to 3 characters
|
|
611
|
+
code = ch
|
|
612
|
+
for _ in range(2):
|
|
613
|
+
if self.current not in b"01234567":
|
|
614
|
+
break
|
|
615
|
+
code += self.next()
|
|
616
|
+
icode = int(code, 8)
|
|
617
|
+
# 8 bits values are allowed only
|
|
618
|
+
if icode <= 255:
|
|
619
|
+
val += bytes([icode])
|
|
620
|
+
else:
|
|
621
|
+
# leave as is
|
|
622
|
+
val += b"\\" + code
|
|
623
|
+
elif ch in EOL:
|
|
624
|
+
# multiline string - just skip
|
|
625
|
+
self.prev()
|
|
626
|
+
self.eol()
|
|
627
|
+
else:
|
|
628
|
+
# unescape or leave as is
|
|
629
|
+
val += STRING_ESCAPED.get(ch) or (b"\\" + ch)
|
|
630
|
+
elif ch == b')':
|
|
631
|
+
break
|
|
632
|
+
else:
|
|
633
|
+
val += ch
|
|
634
|
+
return String(val)
|
|
635
|
+
|
|
636
|
+
def _get_parser(self):
|
|
637
|
+
method = None
|
|
638
|
+
if self.current == b'<':
|
|
639
|
+
method = self.dictionary_or_stream_or_hexstring
|
|
640
|
+
elif self.current == b'[':
|
|
641
|
+
method = self.array
|
|
642
|
+
elif self.current == b'(':
|
|
643
|
+
method = self.string
|
|
644
|
+
elif self.current == b'n':
|
|
645
|
+
method = self.null
|
|
646
|
+
elif self.current == b'f':
|
|
647
|
+
method = self.false
|
|
648
|
+
elif self.current == b't':
|
|
649
|
+
method = self.true
|
|
650
|
+
elif self.current in b'+-.':
|
|
651
|
+
method = self.numeric
|
|
652
|
+
elif self.current in b'1234567890':
|
|
653
|
+
if self.indirect_references_allowed:
|
|
654
|
+
method = self.numeric_or_indirect_reference
|
|
655
|
+
else:
|
|
656
|
+
method = self.numeric
|
|
657
|
+
elif self.current == b"/":
|
|
658
|
+
method = self.name
|
|
659
|
+
return method
|
|
660
|
+
|
|
661
|
+
def object(self):
|
|
662
|
+
val = None
|
|
663
|
+
self.maybe_spaces_or_comments()
|
|
664
|
+
method = self._get_parser()
|
|
665
|
+
if method:
|
|
666
|
+
val = method()
|
|
667
|
+
else:
|
|
668
|
+
self.on_parser_error("Unexpected token")
|
|
669
|
+
return val
|
|
670
|
+
|
|
671
|
+
def indirect_reference(self):
|
|
672
|
+
"""
|
|
673
|
+
>>> s = b'10 5 R'
|
|
674
|
+
>>> BasicTypesParser(s, 0).indirect_reference()
|
|
675
|
+
<IndirectReference:n=10,g=5>
|
|
676
|
+
|
|
677
|
+
"""
|
|
678
|
+
num = self.non_negative_int()
|
|
679
|
+
self.maybe_spaces_or_comments()
|
|
680
|
+
gen = self.non_negative_int()
|
|
681
|
+
self.maybe_spaces_or_comments()
|
|
682
|
+
ch = self.next()
|
|
683
|
+
if ch != b'R':
|
|
684
|
+
self.on_parser_error('R keyword expected')
|
|
685
|
+
return IndirectReference(num, gen)
|
|
686
|
+
|
|
687
|
+
def numeric_or_indirect_reference(self):
|
|
688
|
+
state = self.get_state()
|
|
689
|
+
try:
|
|
690
|
+
val = self.indirect_reference()
|
|
691
|
+
except ParserException:
|
|
692
|
+
self.set_state(state)
|
|
693
|
+
val = self.numeric()
|
|
694
|
+
return val
|
|
695
|
+
|
|
696
|
+
def token(self):
|
|
697
|
+
""" just a token which does not belong to any of PDF types like: def, findresource, ET, BT
|
|
698
|
+
>>> s = b'def'
|
|
699
|
+
>>> BasicTypesParser(s, 0).token()
|
|
700
|
+
'def'
|
|
701
|
+
|
|
702
|
+
>>> s = b'T*'
|
|
703
|
+
>>> BasicTypesParser(s, 0).token()
|
|
704
|
+
'T*'
|
|
705
|
+
|
|
706
|
+
>>> s = b'10T*'
|
|
707
|
+
>>> BasicTypesParser(s, 0).token()
|
|
708
|
+
Traceback (most recent call last):
|
|
709
|
+
...
|
|
710
|
+
pdfreader.exceptions.ParserException: Regular non-digit character expected
|
|
711
|
+
"""
|
|
712
|
+
if not self.is_regular or self.is_digit:
|
|
713
|
+
self.on_parser_error("Regular non-digit character expected")
|
|
714
|
+
|
|
715
|
+
token = b''
|
|
716
|
+
while self.is_regular:
|
|
717
|
+
token += self.next()
|
|
718
|
+
return Token(token.decode(DEFAULT_ENCODING))
|
|
719
|
+
|
|
720
|
+
def expected_name(self, value):
|
|
721
|
+
name = self.name()
|
|
722
|
+
if name != value:
|
|
723
|
+
self.on_parser_error("%s expected".format(value))
|
|
724
|
+
return name
|
|
725
|
+
|
|
726
|
+
def expected_token(self, value):
|
|
727
|
+
token = self.token()
|
|
728
|
+
if token != value:
|
|
729
|
+
self.on_parser_error("%s expected".format(value))
|
|
730
|
+
return token
|
|
731
|
+
|
|
732
|
+
def expected_numeric(self, value):
|
|
733
|
+
n = self.numeric()
|
|
734
|
+
if n != value:
|
|
735
|
+
self.on_parser_error("%s expected".format(value))
|
|
736
|
+
return n
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
if __name__ == "__main__":
|
|
740
|
+
import doctest
|
|
741
|
+
doctest.testmod()
|