/src/qpdf/libqpdf/QPDFParser.cc
Line | Count | Source |
1 | | #include <qpdf/QPDFParser.hh> |
2 | | |
3 | | #include <qpdf/QPDF.hh> |
4 | | #include <qpdf/QPDFObjGen.hh> |
5 | | #include <qpdf/QPDFObjectHandle.hh> |
6 | | #include <qpdf/QPDFObject_private.hh> |
7 | | #include <qpdf/QPDFTokenizer_private.hh> |
8 | | #include <qpdf/QTC.hh> |
9 | | #include <qpdf/QUtil.hh> |
10 | | |
11 | | #include <memory> |
12 | | |
13 | | using namespace std::literals; |
14 | | using namespace qpdf; |
15 | | |
16 | | using ObjectPtr = std::shared_ptr<QPDFObject>; |
17 | | |
18 | | // The ParseGuard class allows QPDFParser to detect re-entrant parsing. It also provides |
19 | | // special access to allow the parser to create unresolved objects and dangling references. |
20 | | class QPDF::Doc::ParseGuard |
21 | | { |
22 | | public: |
23 | | ParseGuard(QPDF* qpdf) : |
24 | 19.8k | objects(qpdf ? &qpdf->m->objects : nullptr) |
25 | 19.8k | { |
26 | 19.8k | if (objects) { |
27 | 7.78k | objects->inParse(true); |
28 | 7.78k | } |
29 | 19.8k | } |
30 | | |
31 | | static std::shared_ptr<QPDFObject> |
32 | | getObject(QPDF* qpdf, int id, int gen, bool parse_pdf) |
33 | 0 | { |
34 | 0 | return qpdf->m->objects.getObjectForParser(id, gen, parse_pdf); |
35 | 0 | } |
36 | | |
37 | | ~ParseGuard() |
38 | 19.8k | { |
39 | 19.8k | if (objects) { |
40 | 7.78k | objects->inParse(false); |
41 | 7.78k | } |
42 | 19.8k | } |
43 | | QPDF::Doc::Objects* objects; |
44 | | }; |
45 | | |
46 | | using ParseGuard = QPDF::Doc::ParseGuard; |
47 | | |
48 | | QPDFObjectHandle |
49 | | QPDFParser::parse(InputSource& input, std::string const& object_description, QPDF* context) |
50 | 12.0k | { |
51 | 12.0k | qpdf::Tokenizer tokenizer; |
52 | 12.0k | bool empty = false; |
53 | 12.0k | return QPDFParser( |
54 | 12.0k | input, |
55 | 12.0k | make_description(input.getName(), object_description), |
56 | 12.0k | object_description, |
57 | 12.0k | tokenizer, |
58 | 12.0k | nullptr, |
59 | 12.0k | context, |
60 | 12.0k | false) |
61 | 12.0k | .parse(empty, false); |
62 | 12.0k | } |
63 | | |
64 | | QPDFObjectHandle |
65 | | QPDFParser::parse_content( |
66 | | InputSource& input, |
67 | | std::shared_ptr<QPDFObject::Description> sp_description, |
68 | | qpdf::Tokenizer& tokenizer, |
69 | | QPDF* context) |
70 | 0 | { |
71 | 0 | bool empty = false; |
72 | 0 | return QPDFParser( |
73 | 0 | input, |
74 | 0 | std::move(sp_description), |
75 | 0 | "content", |
76 | 0 | tokenizer, |
77 | 0 | nullptr, |
78 | 0 | context, |
79 | 0 | true, |
80 | 0 | 0, |
81 | 0 | 0, |
82 | 0 | context && context->doc().reconstructed_xref()) |
83 | 0 | .parse(empty, true); |
84 | 0 | } |
85 | | |
86 | | QPDFObjectHandle |
87 | | QPDFParser::parse( |
88 | | InputSource& input, |
89 | | std::string const& object_description, |
90 | | QPDFTokenizer& tokenizer, |
91 | | bool& empty, |
92 | | QPDFObjectHandle::StringDecrypter* decrypter, |
93 | | QPDF* context) |
94 | 0 | { |
95 | 0 | return QPDFParser( |
96 | 0 | input, |
97 | 0 | make_description(input.getName(), object_description), |
98 | 0 | object_description, |
99 | 0 | *tokenizer.m, |
100 | 0 | decrypter, |
101 | 0 | context, |
102 | 0 | false) |
103 | 0 | .parse(empty, false); |
104 | 0 | } |
105 | | |
106 | | std::pair<QPDFObjectHandle, bool> |
107 | | QPDFParser::parse( |
108 | | InputSource& input, |
109 | | std::string const& object_description, |
110 | | qpdf::Tokenizer& tokenizer, |
111 | | QPDFObjectHandle::StringDecrypter* decrypter, |
112 | | QPDF& context, |
113 | | bool sanity_checks) |
114 | 7.78k | { |
115 | 7.78k | bool empty{false}; |
116 | 7.78k | auto result = QPDFParser( |
117 | 7.78k | input, |
118 | 7.78k | make_description(input.getName(), object_description), |
119 | 7.78k | object_description, |
120 | 7.78k | tokenizer, |
121 | 7.78k | decrypter, |
122 | 7.78k | &context, |
123 | 7.78k | true, |
124 | 7.78k | 0, |
125 | 7.78k | 0, |
126 | 7.78k | sanity_checks) |
127 | 7.78k | .parse(empty, false); |
128 | 7.78k | return {result, empty}; |
129 | 7.78k | } |
130 | | |
131 | | std::pair<QPDFObjectHandle, bool> |
132 | | QPDFParser::parse( |
133 | | is::OffsetBuffer& input, int stream_id, int obj_id, qpdf::Tokenizer& tokenizer, QPDF& context) |
134 | 0 | { |
135 | 0 | bool empty{false}; |
136 | 0 | auto result = QPDFParser( |
137 | 0 | input, |
138 | 0 | std::make_shared<QPDFObject::Description>( |
139 | 0 | QPDFObject::ObjStreamDescr(stream_id, obj_id)), |
140 | 0 | "", |
141 | 0 | tokenizer, |
142 | 0 | nullptr, |
143 | 0 | &context, |
144 | 0 | true, |
145 | 0 | stream_id, |
146 | 0 | obj_id) |
147 | 0 | .parse(empty, false); |
148 | 0 | return {result, empty}; |
149 | 0 | } |
150 | | |
151 | | QPDFObjectHandle |
152 | | QPDFParser::parse(bool& empty, bool content_stream) |
153 | 19.8k | { |
154 | | // This method must take care not to resolve any objects. Don't check the type of any object |
155 | | // without first ensuring that it is a direct object. Otherwise, doing so may have the side |
156 | | // effect of reading the object and changing the file pointer. If you do this, it will cause a |
157 | | // logic error to be thrown from QPDF::inParse(). |
158 | | |
159 | 19.8k | ParseGuard pg(context); |
160 | 19.8k | empty = false; |
161 | 19.8k | start = input.tell(); |
162 | | |
163 | 19.8k | if (!tokenizer.nextToken(input, object_description)) { |
164 | 289 | warn(tokenizer.getErrorMessage()); |
165 | 289 | } |
166 | | |
167 | 19.8k | switch (tokenizer.getType()) { |
168 | 0 | case QPDFTokenizer::tt_eof: |
169 | 0 | if (content_stream) { |
170 | | // In content stream mode, leave object uninitialized to indicate EOF |
171 | 0 | return {}; |
172 | 0 | } |
173 | 0 | QTC::TC("qpdf", "QPDFParser eof in parse"); |
174 | 0 | warn("unexpected EOF"); |
175 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
176 | | |
177 | 0 | case QPDFTokenizer::tt_bad: |
178 | 0 | QTC::TC("qpdf", "QPDFParser bad token in parse"); |
179 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
180 | | |
181 | 0 | case QPDFTokenizer::tt_brace_open: |
182 | 0 | case QPDFTokenizer::tt_brace_close: |
183 | 0 | QTC::TC("qpdf", "QPDFParser bad brace"); |
184 | 0 | warn("treating unexpected brace token as null"); |
185 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
186 | | |
187 | 0 | case QPDFTokenizer::tt_array_close: |
188 | 0 | QTC::TC("qpdf", "QPDFParser bad array close"); |
189 | 0 | warn("treating unexpected array close token as null"); |
190 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
191 | | |
192 | 0 | case QPDFTokenizer::tt_dict_close: |
193 | 0 | QTC::TC("qpdf", "QPDFParser bad dictionary close"); |
194 | 0 | warn("unexpected dictionary close token"); |
195 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
196 | | |
197 | 0 | case QPDFTokenizer::tt_array_open: |
198 | 7.78k | case QPDFTokenizer::tt_dict_open: |
199 | 7.78k | stack.clear(); |
200 | 7.78k | stack.emplace_back( |
201 | 7.78k | input, |
202 | 7.78k | (tokenizer.getType() == QPDFTokenizer::tt_array_open) ? st_array : st_dictionary_key); |
203 | 7.78k | frame = &stack.back(); |
204 | 7.78k | return parseRemainder(content_stream); |
205 | | |
206 | 0 | case QPDFTokenizer::tt_bool: |
207 | 0 | return withDescription<QPDF_Bool>(tokenizer.getValue() == "true"); |
208 | | |
209 | 0 | case QPDFTokenizer::tt_null: |
210 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
211 | | |
212 | 0 | case QPDFTokenizer::tt_integer: |
213 | 0 | return withDescription<QPDF_Integer>(QUtil::string_to_ll(tokenizer.getValue().c_str())); |
214 | | |
215 | 0 | case QPDFTokenizer::tt_real: |
216 | 0 | return withDescription<QPDF_Real>(tokenizer.getValue()); |
217 | | |
218 | 11.7k | case QPDFTokenizer::tt_name: |
219 | 11.7k | return withDescription<QPDF_Name>(tokenizer.getValue()); |
220 | | |
221 | 0 | case QPDFTokenizer::tt_word: |
222 | 0 | { |
223 | 0 | auto const& value = tokenizer.getValue(); |
224 | 0 | if (content_stream) { |
225 | 0 | return withDescription<QPDF_Operator>(value); |
226 | 0 | } else if (value == "endobj") { |
227 | | // We just saw endobj without having read anything. Treat this as a null and do |
228 | | // not move the input source's offset. |
229 | 0 | input.seek(input.getLastOffset(), SEEK_SET); |
230 | 0 | empty = true; |
231 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
232 | 0 | } else { |
233 | 0 | QTC::TC("qpdf", "QPDFParser treat word as string"); |
234 | 0 | warn("unknown token while reading object; treating as string"); |
235 | 0 | return withDescription<QPDF_String>(value); |
236 | 0 | } |
237 | 0 | } |
238 | | |
239 | 0 | case QPDFTokenizer::tt_string: |
240 | 0 | if (decrypter) { |
241 | 0 | std::string s{tokenizer.getValue()}; |
242 | 0 | decrypter->decryptString(s); |
243 | 0 | return withDescription<QPDF_String>(s); |
244 | 0 | } else { |
245 | 0 | return withDescription<QPDF_String>(tokenizer.getValue()); |
246 | 0 | } |
247 | | |
248 | 0 | default: |
249 | 0 | warn("treating unknown token type as null while reading object"); |
250 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
251 | 19.8k | } |
252 | 19.8k | } |
253 | | |
254 | | QPDFObjectHandle |
255 | | QPDFParser::parseRemainder(bool content_stream) |
256 | 7.78k | { |
257 | | // This method must take care not to resolve any objects. Don't check the type of any object |
258 | | // without first ensuring that it is a direct object. Otherwise, doing so may have the side |
259 | | // effect of reading the object and changing the file pointer. If you do this, it will cause a |
260 | | // logic error to be thrown from QPDF::inParse(). |
261 | | |
262 | 7.78k | bad_count = 0; |
263 | 7.78k | bool b_contents = false; |
264 | | |
265 | 23.3k | while (true) { |
266 | 23.3k | if (!tokenizer.nextToken(input, object_description)) { |
267 | 0 | warn(tokenizer.getErrorMessage()); |
268 | 0 | } |
269 | 23.3k | ++good_count; // optimistically |
270 | | |
271 | 23.3k | if (int_count != 0) { |
272 | | // Special handling of indirect references. Treat integer tokens as part of an indirect |
273 | | // reference until proven otherwise. |
274 | 7.78k | if (tokenizer.getType() == QPDFTokenizer::tt_integer) { |
275 | 0 | if (++int_count > 2) { |
276 | | // Process the oldest buffered integer. |
277 | 0 | addInt(int_count); |
278 | 0 | } |
279 | 0 | last_offset_buffer[int_count % 2] = input.getLastOffset(); |
280 | 0 | int_buffer[int_count % 2] = QUtil::string_to_ll(tokenizer.getValue().c_str()); |
281 | 0 | continue; |
282 | |
|
283 | 7.78k | } else if ( |
284 | 7.78k | int_count >= 2 && tokenizer.getType() == QPDFTokenizer::tt_word && |
285 | 0 | tokenizer.getValue() == "R") { |
286 | 0 | if (context == nullptr) { |
287 | 0 | QTC::TC("qpdf", "QPDFParser indirect without context"); |
288 | 0 | throw std::logic_error( |
289 | 0 | "QPDFParser::parse called without context on an object " |
290 | 0 | "with indirect references"); |
291 | 0 | } |
292 | 0 | auto id = QIntC::to_int(int_buffer[(int_count - 1) % 2]); |
293 | 0 | auto gen = QIntC::to_int(int_buffer[(int_count) % 2]); |
294 | 0 | if (!(id < 1 || gen < 0 || gen >= 65535)) { |
295 | 0 | add(ParseGuard::getObject(context, id, gen, parse_pdf)); |
296 | 0 | } else { |
297 | 0 | QTC::TC("qpdf", "QPDFParser invalid objgen"); |
298 | 0 | addNull(); |
299 | 0 | } |
300 | 0 | int_count = 0; |
301 | 0 | continue; |
302 | |
|
303 | 7.78k | } else if (int_count > 0) { |
304 | | // Process the buffered integers before processing the current token. |
305 | 7.78k | if (int_count > 1) { |
306 | 0 | addInt(int_count - 1); |
307 | 0 | } |
308 | 7.78k | addInt(int_count); |
309 | 7.78k | int_count = 0; |
310 | 7.78k | } |
311 | 7.78k | } |
312 | | |
313 | 23.3k | switch (tokenizer.getType()) { |
314 | 0 | case QPDFTokenizer::tt_eof: |
315 | 0 | warn("parse error while reading object"); |
316 | 0 | if (content_stream) { |
317 | | // In content stream mode, leave object uninitialized to indicate EOF |
318 | 0 | return {}; |
319 | 0 | } |
320 | 0 | QTC::TC("qpdf", "QPDFParser eof in parseRemainder"); |
321 | 0 | warn("unexpected EOF"); |
322 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
323 | | |
324 | 0 | case QPDFTokenizer::tt_bad: |
325 | 0 | QTC::TC("qpdf", "QPDFParser bad token in parseRemainder"); |
326 | 0 | if (tooManyBadTokens()) { |
327 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
328 | 0 | } |
329 | 0 | addNull(); |
330 | 0 | continue; |
331 | | |
332 | 0 | case QPDFTokenizer::tt_brace_open: |
333 | 0 | case QPDFTokenizer::tt_brace_close: |
334 | 0 | QTC::TC("qpdf", "QPDFParser bad brace in parseRemainder"); |
335 | 0 | warn("treating unexpected brace token as null"); |
336 | 0 | if (tooManyBadTokens()) { |
337 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
338 | 0 | } |
339 | 0 | addNull(); |
340 | 0 | continue; |
341 | | |
342 | 0 | case QPDFTokenizer::tt_array_close: |
343 | 0 | if ((bad_count || sanity_checks) && !max_bad_count) { |
344 | | // Trigger warning. |
345 | 0 | (void)tooManyBadTokens(); |
346 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
347 | 0 | } |
348 | 0 | if (frame->state == st_array) { |
349 | 0 | auto object = frame->null_count > 100 |
350 | 0 | ? QPDFObject::create<QPDF_Array>(std::move(frame->olist), true) |
351 | 0 | : QPDFObject::create<QPDF_Array>(std::move(frame->olist)); |
352 | 0 | setDescription(object, frame->offset - 1); |
353 | | // The `offset` points to the next of "[". Set the rewind offset to point to the |
354 | | // beginning of "[". This has been explicitly tested with whitespace surrounding the |
355 | | // array start delimiter. getLastOffset points to the array end token and therefore |
356 | | // can't be used here. |
357 | 0 | if (stack.size() <= 1) { |
358 | 0 | return object; |
359 | 0 | } |
360 | 0 | stack.pop_back(); |
361 | 0 | frame = &stack.back(); |
362 | 0 | add(std::move(object)); |
363 | 0 | } else { |
364 | 0 | QTC::TC("qpdf", "QPDFParser bad array close in parseRemainder"); |
365 | 0 | if (sanity_checks) { |
366 | | // During sanity checks, assume nesting of containers is corrupt and object is |
367 | | // unusable. |
368 | 0 | warn("unexpected array close token; giving up on reading object"); |
369 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
370 | 0 | } |
371 | 0 | warn("treating unexpected array close token as null"); |
372 | 0 | if (tooManyBadTokens()) { |
373 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
374 | 0 | } |
375 | 0 | addNull(); |
376 | 0 | } |
377 | 0 | continue; |
378 | | |
379 | 7.78k | case QPDFTokenizer::tt_dict_close: |
380 | 7.78k | if ((bad_count || sanity_checks) && !max_bad_count) { |
381 | | // Trigger warning. |
382 | 0 | (void)tooManyBadTokens(); |
383 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
384 | 0 | } |
385 | 7.78k | if (frame->state <= st_dictionary_value) { |
386 | | // Attempt to recover more or less gracefully from invalid dictionaries. |
387 | 7.78k | auto& dict = frame->dict; |
388 | | |
389 | 7.78k | if (frame->state == st_dictionary_value) { |
390 | 0 | QTC::TC("qpdf", "QPDFParser no val for last key"); |
391 | 0 | warn( |
392 | 0 | frame->offset, |
393 | 0 | "dictionary ended prematurely; using null as value for last key"); |
394 | 0 | dict[frame->key] = QPDFObject::create<QPDF_Null>(); |
395 | 0 | } |
396 | 7.78k | if (!frame->olist.empty()) { |
397 | 0 | if (sanity_checks) { |
398 | 0 | warn( |
399 | 0 | frame->offset, |
400 | 0 | "expected dictionary keys but found non-name objects; ignoring"); |
401 | 0 | } else { |
402 | 0 | fixMissingKeys(); |
403 | 0 | } |
404 | 0 | } |
405 | | |
406 | 7.78k | if (!frame->contents_string.empty() && dict.contains("/Type") && |
407 | 0 | dict["/Type"].isNameAndEquals("/Sig") && dict.contains("/ByteRange") && |
408 | 0 | dict.contains("/Contents") && dict["/Contents"].isString()) { |
409 | 0 | dict["/Contents"] = QPDFObjectHandle::newString(frame->contents_string); |
410 | 0 | dict["/Contents"].setParsedOffset(frame->contents_offset); |
411 | 0 | } |
412 | 7.78k | auto object = QPDFObject::create<QPDF_Dictionary>(std::move(dict)); |
413 | 7.78k | setDescription(object, frame->offset - 2); |
414 | | // The `offset` points to the next of "<<". Set the rewind offset to point to the |
415 | | // beginning of "<<". This has been explicitly tested with whitespace surrounding |
416 | | // the dictionary start delimiter. getLastOffset points to the dictionary end token |
417 | | // and therefore can't be used here. |
418 | 7.78k | if (stack.size() <= 1) { |
419 | 7.78k | return object; |
420 | 7.78k | } |
421 | 0 | stack.pop_back(); |
422 | 0 | frame = &stack.back(); |
423 | 0 | add(std::move(object)); |
424 | 0 | } else { |
425 | 0 | if (sanity_checks) { |
426 | | // During sanity checks, assume nesting of containers is corrupt and object is |
427 | | // unusable. |
428 | 0 | warn("unexpected dictionary close token; giving up on reading object"); |
429 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
430 | 0 | } |
431 | 0 | warn("unexpected dictionary close token"); |
432 | 0 | if (tooManyBadTokens()) { |
433 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
434 | 0 | } |
435 | 0 | addNull(); |
436 | 0 | } |
437 | 0 | continue; |
438 | | |
439 | 0 | case QPDFTokenizer::tt_array_open: |
440 | 0 | case QPDFTokenizer::tt_dict_open: |
441 | 0 | if (stack.size() > 499) { |
442 | 0 | QTC::TC("qpdf", "QPDFParser too deep"); |
443 | 0 | warn("ignoring excessively deeply nested data structure"); |
444 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
445 | 0 | } else { |
446 | 0 | b_contents = false; |
447 | 0 | stack.emplace_back( |
448 | 0 | input, |
449 | 0 | (tokenizer.getType() == QPDFTokenizer::tt_array_open) ? st_array |
450 | 0 | : st_dictionary_key); |
451 | 0 | frame = &stack.back(); |
452 | 0 | continue; |
453 | 0 | } |
454 | | |
455 | 0 | case QPDFTokenizer::tt_bool: |
456 | 0 | addScalar<QPDF_Bool>(tokenizer.getValue() == "true"); |
457 | 0 | continue; |
458 | | |
459 | 0 | case QPDFTokenizer::tt_null: |
460 | 0 | addNull(); |
461 | 0 | continue; |
462 | | |
463 | 7.78k | case QPDFTokenizer::tt_integer: |
464 | 7.78k | if (!content_stream) { |
465 | | // Buffer token in case it is part of an indirect reference. |
466 | 7.78k | last_offset_buffer[1] = input.getLastOffset(); |
467 | 7.78k | int_buffer[1] = QUtil::string_to_ll(tokenizer.getValue().c_str()); |
468 | 7.78k | int_count = 1; |
469 | 7.78k | } else { |
470 | 0 | addScalar<QPDF_Integer>(QUtil::string_to_ll(tokenizer.getValue().c_str())); |
471 | 0 | } |
472 | 7.78k | continue; |
473 | | |
474 | 0 | case QPDFTokenizer::tt_real: |
475 | 0 | addScalar<QPDF_Real>(tokenizer.getValue()); |
476 | 0 | continue; |
477 | | |
478 | 7.78k | case QPDFTokenizer::tt_name: |
479 | 7.78k | if (frame->state == st_dictionary_key) { |
480 | 7.78k | frame->key = tokenizer.getValue(); |
481 | 7.78k | frame->state = st_dictionary_value; |
482 | 7.78k | b_contents = decrypter && frame->key == "/Contents"; |
483 | 7.78k | continue; |
484 | 7.78k | } else { |
485 | 0 | addScalar<QPDF_Name>(tokenizer.getValue()); |
486 | 0 | } |
487 | 0 | continue; |
488 | | |
489 | 0 | case QPDFTokenizer::tt_word: |
490 | 0 | if (content_stream) { |
491 | 0 | addScalar<QPDF_Operator>(tokenizer.getValue()); |
492 | 0 | continue; |
493 | 0 | } |
494 | | |
495 | 0 | if (sanity_checks) { |
496 | 0 | if (tokenizer.getValue() == "endobj" || tokenizer.getValue() == "endstream") { |
497 | | // During sanity checks, assume an unexpected endobj or endstream indicates that |
498 | | // we are parsing past the end of the object. |
499 | 0 | warn( |
500 | 0 | "unexpected 'endobj' or 'endstream' while reading object; giving up on " |
501 | 0 | "reading object"); |
502 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
503 | 0 | } |
504 | | |
505 | 0 | warn("unknown token while reading object; treating as null"); |
506 | 0 | if (tooManyBadTokens()) { |
507 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
508 | 0 | } |
509 | 0 | addNull(); |
510 | 0 | continue; |
511 | 0 | } |
512 | | |
513 | 0 | QTC::TC("qpdf", "QPDFParser treat word as string in parseRemainder"); |
514 | 0 | warn("unknown token while reading object; treating as string"); |
515 | 0 | if (tooManyBadTokens()) { |
516 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
517 | 0 | } |
518 | 0 | addScalar<QPDF_String>(tokenizer.getValue()); |
519 | |
|
520 | 0 | continue; |
521 | | |
522 | 0 | case QPDFTokenizer::tt_string: |
523 | 0 | { |
524 | 0 | auto const& val = tokenizer.getValue(); |
525 | 0 | if (decrypter) { |
526 | 0 | if (b_contents) { |
527 | 0 | frame->contents_string = val; |
528 | 0 | frame->contents_offset = input.getLastOffset(); |
529 | 0 | b_contents = false; |
530 | 0 | } |
531 | 0 | std::string s{val}; |
532 | 0 | decrypter->decryptString(s); |
533 | 0 | addScalar<QPDF_String>(s); |
534 | 0 | } else { |
535 | 0 | addScalar<QPDF_String>(val); |
536 | 0 | } |
537 | 0 | } |
538 | 0 | continue; |
539 | | |
540 | 0 | default: |
541 | 0 | warn("treating unknown token type as null while reading object"); |
542 | 0 | if (tooManyBadTokens()) { |
543 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
544 | 0 | } |
545 | 0 | addNull(); |
546 | 23.3k | } |
547 | 23.3k | } |
548 | 7.78k | } |
549 | | |
550 | | void |
551 | | QPDFParser::add(std::shared_ptr<QPDFObject>&& obj) |
552 | 7.78k | { |
553 | 7.78k | if (frame->state != st_dictionary_value) { |
554 | | // If state is st_dictionary_key then there is a missing key. Push onto olist for |
555 | | // processing once the tt_dict_close token has been found. |
556 | 0 | frame->olist.emplace_back(std::move(obj)); |
557 | 7.78k | } else { |
558 | 7.78k | if (auto res = frame->dict.insert_or_assign(frame->key, std::move(obj)); !res.second) { |
559 | 0 | warnDuplicateKey(); |
560 | 0 | } |
561 | 7.78k | frame->state = st_dictionary_key; |
562 | 7.78k | } |
563 | 7.78k | } |
564 | | |
565 | | void |
566 | | QPDFParser::addNull() |
567 | 0 | { |
568 | 0 | const static ObjectPtr null_obj = QPDFObject::create<QPDF_Null>(); |
569 | |
|
570 | 0 | if (frame->state != st_dictionary_value) { |
571 | | // If state is st_dictionary_key then there is a missing key. Push onto olist for |
572 | | // processing once the tt_dict_close token has been found. |
573 | 0 | frame->olist.emplace_back(null_obj); |
574 | 0 | } else { |
575 | 0 | if (auto res = frame->dict.insert_or_assign(frame->key, null_obj); !res.second) { |
576 | 0 | warnDuplicateKey(); |
577 | 0 | } |
578 | 0 | frame->state = st_dictionary_key; |
579 | 0 | } |
580 | 0 | ++frame->null_count; |
581 | 0 | } |
582 | | |
583 | | void |
584 | | QPDFParser::addInt(int count) |
585 | 7.78k | { |
586 | 7.78k | auto obj = QPDFObject::create<QPDF_Integer>(int_buffer[count % 2]); |
587 | 7.78k | obj->setDescription(context, description, last_offset_buffer[count % 2]); |
588 | 7.78k | add(std::move(obj)); |
589 | 7.78k | } |
590 | | |
591 | | template <typename T, typename... Args> |
592 | | void |
593 | | QPDFParser::addScalar(Args&&... args) |
594 | 0 | { |
595 | 0 | if ((bad_count || sanity_checks) && |
596 | 0 | (frame->olist.size() > 5'000 || frame->dict.size() > 5'000)) { |
597 | | // Stop adding scalars. We are going to abort when the close token or a bad token is |
598 | | // encountered. |
599 | 0 | max_bad_count = 0; |
600 | 0 | return; |
601 | 0 | } |
602 | 0 | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); |
603 | 0 | obj->setDescription(context, description, input.getLastOffset()); |
604 | 0 | add(std::move(obj)); |
605 | 0 | } Unexecuted instantiation: void QPDFParser::addScalar<QPDF_Bool, bool>(bool&&) Unexecuted instantiation: void QPDFParser::addScalar<QPDF_Integer, long long>(long long&&) Unexecuted instantiation: void QPDFParser::addScalar<QPDF_Real, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Unexecuted instantiation: void QPDFParser::addScalar<QPDF_Name, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Unexecuted instantiation: void QPDFParser::addScalar<QPDF_Operator, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Unexecuted instantiation: void QPDFParser::addScalar<QPDF_String, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Unexecuted instantiation: void QPDFParser::addScalar<QPDF_String, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) |
606 | | |
607 | | template <typename T, typename... Args> |
608 | | QPDFObjectHandle |
609 | | QPDFParser::withDescription(Args&&... args) |
610 | 11.7k | { |
611 | 11.7k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); |
612 | 11.7k | obj->setDescription(context, description, start); |
613 | 11.7k | return {obj}; |
614 | 11.7k | } Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_Bool, bool>(bool&&) Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_Integer, long long>(long long&&) Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_Real, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) QPDFObjectHandle QPDFParser::withDescription<QPDF_Name, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 610 | 11.7k | { | 611 | 11.7k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 612 | 11.7k | obj->setDescription(context, description, start); | 613 | 11.7k | return {obj}; | 614 | 11.7k | } |
Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_Operator, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_String, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_String, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) |
615 | | |
616 | | void |
617 | | QPDFParser::setDescription(ObjectPtr& obj, qpdf_offset_t parsed_offset) |
618 | 7.78k | { |
619 | 7.78k | if (obj) { |
620 | 7.78k | obj->setDescription(context, description, parsed_offset); |
621 | 7.78k | } |
622 | 7.78k | } |
623 | | |
624 | | void |
625 | | QPDFParser::fixMissingKeys() |
626 | 0 | { |
627 | 0 | std::set<std::string> names; |
628 | 0 | for (auto& obj: frame->olist) { |
629 | 0 | if (obj.getObj()->getTypeCode() == ::ot_name) { |
630 | 0 | names.insert(obj.getObj()->getStringValue()); |
631 | 0 | } |
632 | 0 | } |
633 | 0 | int next_fake_key = 1; |
634 | 0 | for (auto const& item: frame->olist) { |
635 | 0 | while (true) { |
636 | 0 | const std::string key = "/QPDFFake" + std::to_string(next_fake_key++); |
637 | 0 | const bool found_fake = !frame->dict.contains(key) && !names.contains(key); |
638 | 0 | QTC::TC("qpdf", "QPDFParser found fake", (found_fake ? 0 : 1)); |
639 | 0 | if (found_fake) { |
640 | 0 | warn( |
641 | 0 | frame->offset, |
642 | 0 | "expected dictionary key but found non-name object; inserting key " + key); |
643 | 0 | frame->dict[key] = item; |
644 | 0 | break; |
645 | 0 | } |
646 | 0 | } |
647 | 0 | } |
648 | 0 | } |
649 | | |
650 | | bool |
651 | | QPDFParser::tooManyBadTokens() |
652 | 0 | { |
653 | 0 | if (frame->olist.size() > 5'000 || frame->dict.size() > 5'000) { |
654 | 0 | if (bad_count) { |
655 | 0 | warn( |
656 | 0 | "encountered errors while parsing an array or dictionary with more than 5000 " |
657 | 0 | "elements; giving up on reading object"); |
658 | 0 | return true; |
659 | 0 | } |
660 | 0 | warn( |
661 | 0 | "encountered an array or dictionary with more than 5000 elements during xref recovery; " |
662 | 0 | "giving up on reading object"); |
663 | 0 | } |
664 | 0 | if (max_bad_count && --max_bad_count > 0 && good_count > 4) { |
665 | 0 | good_count = 0; |
666 | 0 | bad_count = 1; |
667 | 0 | return false; |
668 | 0 | } |
669 | 0 | if (++bad_count > 5 || |
670 | 0 | (frame->state != st_array && QIntC::to_size(max_bad_count) < frame->olist.size())) { |
671 | | // Give up after 5 errors in close proximity or if the number of missing dictionary keys |
672 | | // exceeds the remaining number of allowable total errors. |
673 | 0 | warn("too many errors; giving up on reading object"); |
674 | 0 | return true; |
675 | 0 | } |
676 | 0 | good_count = 0; |
677 | 0 | return false; |
678 | 0 | } |
679 | | |
680 | | void |
681 | | QPDFParser::warn(QPDFExc const& e) const |
682 | 289 | { |
683 | | // If parsing on behalf of a QPDF object and want to give a warning, we can warn through the |
684 | | // object. If parsing for some other reason, such as an explicit creation of an object from a |
685 | | // string, then just throw the exception. |
686 | 289 | if (context) { |
687 | 0 | context->warn(e); |
688 | 289 | } else { |
689 | 289 | throw e; |
690 | 289 | } |
691 | 289 | } |
692 | | |
693 | | void |
694 | | QPDFParser::warnDuplicateKey() |
695 | 0 | { |
696 | 0 | QTC::TC("qpdf", "QPDFParser duplicate dict key"); |
697 | 0 | warn( |
698 | 0 | frame->offset, |
699 | 0 | "dictionary has duplicated key " + frame->key + "; last occurrence overrides earlier ones"); |
700 | 0 | } |
701 | | |
702 | | void |
703 | | QPDFParser::warn(qpdf_offset_t offset, std::string const& msg) const |
704 | 289 | { |
705 | 289 | if (stream_id) { |
706 | 0 | std::string descr = "object "s + std::to_string(obj_id) + " 0"; |
707 | 0 | std::string name = context->getFilename() + " object stream " + std::to_string(stream_id); |
708 | 0 | warn(QPDFExc(qpdf_e_damaged_pdf, name, descr, offset, msg)); |
709 | 289 | } else { |
710 | 289 | warn(QPDFExc(qpdf_e_damaged_pdf, input.getName(), object_description, offset, msg)); |
711 | 289 | } |
712 | 289 | } |
713 | | |
714 | | void |
715 | | QPDFParser::warn(std::string const& msg) const |
716 | 289 | { |
717 | 289 | warn(input.getLastOffset(), msg); |
718 | 289 | } |