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