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