/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 | 201k | objects(qpdf ? &qpdf->m->objects : nullptr) |
27 | 201k | { |
28 | 201k | if (objects) { |
29 | 188k | objects->inParse(true); |
30 | 188k | } |
31 | 201k | } |
32 | | |
33 | | static std::shared_ptr<QPDFObject> |
34 | | getObject(QPDF* qpdf, int id, int gen, bool parse_pdf) |
35 | 474k | { |
36 | 474k | return qpdf->m->objects.getObjectForParser(id, gen, parse_pdf); |
37 | 474k | } |
38 | | |
39 | | ~ParseGuard() |
40 | 201k | { |
41 | 201k | if (objects) { |
42 | 188k | objects->inParse(false); |
43 | 188k | } |
44 | 201k | } |
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 | 13.3k | { |
54 | 13.3k | qpdf::Tokenizer tokenizer; |
55 | 13.3k | if (auto result = Parser( |
56 | 13.3k | input, |
57 | 13.3k | make_description(input.getName(), object_description), |
58 | 13.3k | object_description, |
59 | 13.3k | tokenizer, |
60 | 13.3k | nullptr, |
61 | 13.3k | context, |
62 | 13.3k | false) |
63 | 13.3k | .parse()) { |
64 | 13.3k | return result; |
65 | 13.3k | } |
66 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
67 | 13.3k | } |
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 | 140k | { |
132 | 140k | return Parser( |
133 | 140k | input, |
134 | 140k | make_description(input.getName(), object_description), |
135 | 140k | object_description, |
136 | 140k | tokenizer, |
137 | 140k | decrypter, |
138 | 140k | &context, |
139 | 140k | true, |
140 | 140k | 0, |
141 | 140k | 0, |
142 | 140k | sanity_checks) |
143 | 140k | .parse(); |
144 | 140k | } |
145 | | |
146 | | QPDFObjectHandle |
147 | | Parser::parse( |
148 | | is::OffsetBuffer& input, int stream_id, int obj_id, qpdf::Tokenizer& tokenizer, QPDF& context) |
149 | 47.4k | { |
150 | 47.4k | return Parser( |
151 | 47.4k | input, |
152 | 47.4k | std::make_shared<QPDFObject::Description>( |
153 | 47.4k | QPDFObject::ObjStreamDescr(stream_id, obj_id)), |
154 | 47.4k | "", |
155 | 47.4k | tokenizer, |
156 | 47.4k | nullptr, |
157 | 47.4k | &context, |
158 | 47.4k | true, |
159 | 47.4k | stream_id, |
160 | 47.4k | obj_id) |
161 | 47.4k | .parse(); |
162 | 47.4k | } |
163 | | |
164 | | QPDFObjectHandle |
165 | | Parser::parse(bool content_stream) |
166 | 201k | { |
167 | 201k | try { |
168 | 201k | return parse_first(content_stream); |
169 | 201k | } catch (Error&) { |
170 | 7.42k | return {}; |
171 | 7.42k | } catch (QPDFExc& e) { |
172 | 3.49k | throw e; |
173 | 3.49k | } catch (std::logic_error& e) { |
174 | 4 | throw e; |
175 | 1.09k | } catch (std::exception& e) { |
176 | 1.09k | warn("treating object as null because of error during parsing: "s + e.what()); |
177 | 1.09k | return {}; |
178 | 1.09k | } |
179 | 201k | } |
180 | | |
181 | | QPDFObjectHandle |
182 | | Parser::parse_first(bool content_stream) |
183 | 201k | { |
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 | 201k | QPDF::Doc::ParseGuard pg(context_); |
190 | 201k | start_ = input_.tell(); |
191 | 201k | if (!tokenizer_.nextToken(input_, object_description_)) { |
192 | 2.03k | warn(tokenizer_.getErrorMessage()); |
193 | 2.03k | } |
194 | | |
195 | 201k | switch (tokenizer_.getType()) { |
196 | 577 | case QPDFTokenizer::tt_eof: |
197 | 577 | if (content_stream) { |
198 | | // In content stream mode, leave object uninitialized to indicate EOF |
199 | 0 | empty_ = true; |
200 | 0 | return {}; |
201 | 0 | } |
202 | 577 | warn("unexpected EOF"); |
203 | 577 | return {}; |
204 | | |
205 | 2.02k | case QPDFTokenizer::tt_bad: |
206 | 2.02k | return {}; |
207 | | |
208 | 94 | case QPDFTokenizer::tt_brace_open: |
209 | 248 | case QPDFTokenizer::tt_brace_close: |
210 | 248 | warn("treating unexpected brace token as null"); |
211 | 248 | return {}; |
212 | | |
213 | 623 | case QPDFTokenizer::tt_array_close: |
214 | 623 | warn("treating unexpected array close token as null"); |
215 | 623 | return {}; |
216 | | |
217 | 663 | case QPDFTokenizer::tt_dict_close: |
218 | 663 | warn("unexpected dictionary close token"); |
219 | 663 | return {}; |
220 | | |
221 | 10.9k | case QPDFTokenizer::tt_array_open: |
222 | 169k | case QPDFTokenizer::tt_dict_open: |
223 | 169k | stack_.clear(); |
224 | 169k | stack_.emplace_back( |
225 | 169k | input_, |
226 | 169k | (tokenizer_.getType() == QPDFTokenizer::tt_array_open) ? st_array : st_dictionary_key); |
227 | 169k | frame_ = &stack_.back(); |
228 | 169k | return parse_remainder(content_stream); |
229 | | |
230 | 442 | case QPDFTokenizer::tt_bool: |
231 | 442 | return with_description<QPDF_Bool>(tokenizer_.getValue() == "true"); |
232 | | |
233 | 208 | case QPDFTokenizer::tt_null: |
234 | 208 | return {QPDFObject::create<QPDF_Null>()}; |
235 | | |
236 | 12.8k | case QPDFTokenizer::tt_integer: |
237 | 12.8k | return with_description<QPDF_Integer>(QUtil::string_to_ll(tokenizer_.getValue().c_str())); |
238 | | |
239 | 980 | case QPDFTokenizer::tt_real: |
240 | 980 | return with_description<QPDF_Real>(tokenizer_.getValue()); |
241 | | |
242 | 1.55k | case QPDFTokenizer::tt_name: |
243 | 1.55k | return with_description<QPDF_Name>(tokenizer_.getValue()); |
244 | | |
245 | 11.0k | case QPDFTokenizer::tt_word: |
246 | 11.0k | { |
247 | 11.0k | auto const& value = tokenizer_.getValue(); |
248 | 11.0k | if (content_stream) { |
249 | 0 | return with_description<QPDF_Operator>(value); |
250 | 11.0k | } 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 | 189 | empty_ = true; |
256 | 189 | input_.seek(input_.getLastOffset(), SEEK_SET); |
257 | 189 | if (!content_stream) { |
258 | 189 | warn("empty object treated as null"); |
259 | 189 | } |
260 | 189 | return {}; |
261 | 10.8k | } else { |
262 | 10.8k | warn("unknown token while reading object; treating as string"); |
263 | 10.8k | return with_description<QPDF_String>(value); |
264 | 10.8k | } |
265 | 11.0k | } |
266 | | |
267 | 263 | case QPDFTokenizer::tt_string: |
268 | 263 | if (decrypter_) { |
269 | 38 | std::string s{tokenizer_.getValue()}; |
270 | 38 | decrypter_->decryptString(s); |
271 | 38 | return with_description<QPDF_String>(s); |
272 | 225 | } else { |
273 | 225 | return with_description<QPDF_String>(tokenizer_.getValue()); |
274 | 225 | } |
275 | | |
276 | 0 | default: |
277 | 0 | warn("treating unknown token type as null while reading object"); |
278 | 0 | return {}; |
279 | 201k | } |
280 | 201k | } |
281 | | |
282 | | QPDFObjectHandle |
283 | | Parser::parse_remainder(bool content_stream) |
284 | 169k | { |
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 | 169k | bad_count_ = 0; |
291 | 169k | bool b_contents = false; |
292 | | |
293 | 6.69M | while (true) { |
294 | 6.68M | if (!tokenizer_.nextToken(input_, object_description_)) { |
295 | 32.4k | warn(tokenizer_.getErrorMessage()); |
296 | 32.4k | } |
297 | 6.68M | ++good_count_; // optimistically |
298 | | |
299 | 6.68M | if (int_count_ != 0) { |
300 | | // Special handling of indirect references. Treat integer tokens as part of an indirect |
301 | | // reference until proven otherwise. |
302 | 2.57M | if (tokenizer_.getType() == QPDFTokenizer::tt_integer) { |
303 | 1.81M | if (++int_count_ > 2) { |
304 | | // Process the oldest buffered integer. |
305 | 1.26M | add_int(int_count_); |
306 | 1.26M | } |
307 | 1.81M | last_offset_buffer_[int_count_ % 2] = input_.getLastOffset(); |
308 | 1.81M | int_buffer_[int_count_ % 2] = QUtil::string_to_ll(tokenizer_.getValue().c_str()); |
309 | 1.81M | continue; |
310 | | |
311 | 1.81M | } else if ( |
312 | 762k | int_count_ >= 2 && tokenizer_.getType() == QPDFTokenizer::tt_word && |
313 | 496k | tokenizer_.getValue() == "R") { |
314 | 476k | 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 | 476k | auto id = QIntC::to_int(int_buffer_[(int_count_ - 1) % 2]); |
320 | 476k | auto gen = QIntC::to_int(int_buffer_[(int_count_) % 2]); |
321 | 476k | if (!(id < 1 || gen < 0 || gen >= 65535)) { |
322 | 474k | add(ParseGuard::getObject(context_, id, gen, parse_pdf_)); |
323 | 474k | } else { |
324 | 2.06k | add_bad_null( |
325 | 2.06k | "treating bad indirect reference (" + std::to_string(id) + " " + |
326 | 2.06k | std::to_string(gen) + " R) as null"); |
327 | 2.06k | } |
328 | 476k | int_count_ = 0; |
329 | 476k | continue; |
330 | | |
331 | 476k | } else if (int_count_ > 0) { |
332 | | // Process the buffered integers before processing the current token. |
333 | 285k | if (int_count_ > 1) { |
334 | 71.6k | add_int(int_count_ - 1); |
335 | 71.6k | } |
336 | 285k | add_int(int_count_); |
337 | 285k | int_count_ = 0; |
338 | 285k | } |
339 | 2.57M | } |
340 | | |
341 | 4.39M | switch (tokenizer_.getType()) { |
342 | 6.86k | case QPDFTokenizer::tt_eof: |
343 | 6.86k | warn("parse error while reading object"); |
344 | 6.86k | if (content_stream) { |
345 | | // In content stream mode, leave object uninitialized to indicate EOF |
346 | 0 | return {}; |
347 | 0 | } |
348 | 6.86k | warn("unexpected EOF"); |
349 | 6.86k | return {}; |
350 | | |
351 | 25.5k | case QPDFTokenizer::tt_bad: |
352 | 25.5k | check_too_many_bad_tokens(); |
353 | 25.5k | add_null(); |
354 | 25.5k | continue; |
355 | | |
356 | 1.92k | case QPDFTokenizer::tt_brace_open: |
357 | 3.80k | case QPDFTokenizer::tt_brace_close: |
358 | 3.80k | add_bad_null("treating unexpected brace token as null"); |
359 | 3.80k | continue; |
360 | | |
361 | 123k | case QPDFTokenizer::tt_array_close: |
362 | 123k | if (frame_->state == st_array) { |
363 | 122k | auto object = frame_->null_count > 100 |
364 | 122k | ? QPDFObject::create<QPDF_Array>(std::move(frame_->olist), true) |
365 | 122k | : QPDFObject::create<QPDF_Array>(std::move(frame_->olist)); |
366 | 122k | 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 | 122k | if (stack_.size() <= 1) { |
372 | 2.16k | return object; |
373 | 2.16k | } |
374 | 119k | stack_.pop_back(); |
375 | 119k | frame_ = &stack_.back(); |
376 | 119k | add(std::move(object)); |
377 | 119k | } else { |
378 | 1.58k | if (sanity_checks_) { |
379 | | // During sanity checks, assume nesting of containers is corrupt and object is |
380 | | // unusable. |
381 | 1.18k | warn("unexpected array close token; giving up on reading object"); |
382 | 1.18k | return {}; |
383 | 1.18k | } |
384 | 395 | add_bad_null("treating unexpected array close token as null"); |
385 | 395 | } |
386 | 120k | continue; |
387 | | |
388 | 221k | case QPDFTokenizer::tt_dict_close: |
389 | 221k | if (frame_->state <= st_dictionary_value) { |
390 | | // Attempt to recover more or less gracefully from invalid dictionaries. |
391 | 219k | auto& dict = frame_->dict; |
392 | | |
393 | 219k | if (frame_->state == st_dictionary_value) { |
394 | 8.68k | warn( |
395 | 8.68k | frame_->offset, |
396 | 8.68k | "dictionary ended prematurely; using null as value for last key"); |
397 | 8.68k | dict[frame_->key] = QPDFObject::create<QPDF_Null>(); |
398 | 8.68k | } |
399 | 219k | if (!frame_->olist.empty()) { |
400 | 40.1k | if (sanity_checks_) { |
401 | 38.3k | warn( |
402 | 38.3k | frame_->offset, |
403 | 38.3k | "expected dictionary keys but found non-name objects; ignoring"); |
404 | 38.3k | } else { |
405 | 1.75k | fix_missing_keys(); |
406 | 1.75k | } |
407 | 40.1k | } |
408 | | |
409 | 219k | if (!frame_->contents_string.empty() && dict.contains("/Type") && |
410 | 102 | dict["/Type"].isNameAndEquals("/Sig") && dict.contains("/ByteRange") && |
411 | 30 | dict.contains("/Contents") && dict["/Contents"].isString()) { |
412 | 30 | dict["/Contents"] = QPDFObjectHandle::newString(frame_->contents_string); |
413 | 30 | dict["/Contents"].setParsedOffset(frame_->contents_offset); |
414 | 30 | } |
415 | 219k | auto object = QPDFObject::create<QPDF_Dictionary>(std::move(dict)); |
416 | 219k | 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 | 219k | if (stack_.size() <= 1) { |
422 | 145k | return object; |
423 | 145k | } |
424 | 73.8k | stack_.pop_back(); |
425 | 73.8k | frame_ = &stack_.back(); |
426 | 73.8k | add(std::move(object)); |
427 | 73.8k | } else { |
428 | 1.99k | if (sanity_checks_) { |
429 | | // During sanity checks, assume nesting of containers is corrupt and object is |
430 | | // unusable. |
431 | 1.46k | warn("unexpected dictionary close token; giving up on reading object"); |
432 | 1.46k | return {}; |
433 | 1.46k | } |
434 | 534 | add_bad_null("unexpected dictionary close token"); |
435 | 534 | } |
436 | 74.3k | continue; |
437 | | |
438 | 170k | case QPDFTokenizer::tt_array_open: |
439 | 287k | case QPDFTokenizer::tt_dict_open: |
440 | 287k | if (stack_.size() > max_nesting) { |
441 | 68 | limits_error( |
442 | 68 | "parser-max-nesting", "ignoring excessively deeply nested data structure"); |
443 | 68 | } |
444 | 287k | b_contents = false; |
445 | 287k | stack_.emplace_back( |
446 | 287k | input_, |
447 | 287k | (tokenizer_.getType() == QPDFTokenizer::tt_array_open) ? st_array |
448 | 287k | : st_dictionary_key); |
449 | 287k | frame_ = &stack_.back(); |
450 | 287k | continue; |
451 | | |
452 | 13.2k | case QPDFTokenizer::tt_bool: |
453 | 13.2k | add_scalar<QPDF_Bool>(tokenizer_.getValue() == "true"); |
454 | 13.2k | continue; |
455 | | |
456 | 71.7k | case QPDFTokenizer::tt_null: |
457 | 71.7k | add_null(); |
458 | 71.7k | continue; |
459 | | |
460 | 762k | case QPDFTokenizer::tt_integer: |
461 | 762k | if (!content_stream) { |
462 | | // Buffer token in case it is part of an indirect reference. |
463 | 762k | last_offset_buffer_[1] = input_.getLastOffset(); |
464 | 762k | int_buffer_[1] = QUtil::string_to_ll(tokenizer_.getValue().c_str()); |
465 | 762k | int_count_ = 1; |
466 | 762k | } else { |
467 | 0 | add_scalar<QPDF_Integer>(QUtil::string_to_ll(tokenizer_.getValue().c_str())); |
468 | 0 | } |
469 | 762k | continue; |
470 | | |
471 | 99.0k | case QPDFTokenizer::tt_real: |
472 | 99.0k | add_scalar<QPDF_Real>(tokenizer_.getValue()); |
473 | 99.0k | continue; |
474 | | |
475 | 2.54M | case QPDFTokenizer::tt_name: |
476 | 2.54M | if (frame_->state == st_dictionary_key) { |
477 | 969k | frame_->key = tokenizer_.getValue(); |
478 | 969k | frame_->state = st_dictionary_value; |
479 | 969k | b_contents = decrypter_ && frame_->key == "/Contents"; |
480 | 969k | continue; |
481 | 1.57M | } else { |
482 | 1.57M | add_scalar<QPDF_Name>(tokenizer_.getValue()); |
483 | 1.57M | } |
484 | 1.57M | continue; |
485 | | |
486 | 1.57M | case QPDFTokenizer::tt_word: |
487 | 181k | if (content_stream) { |
488 | 0 | add_scalar<QPDF_Operator>(tokenizer_.getValue()); |
489 | 0 | continue; |
490 | 0 | } |
491 | | |
492 | 181k | if (sanity_checks_) { |
493 | 175k | 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 | 2.58k | warn( |
497 | 2.58k | "unexpected 'endobj' or 'endstream' while reading object; giving up on " |
498 | 2.58k | "reading object"); |
499 | 2.58k | return {}; |
500 | 2.58k | } |
501 | | |
502 | 172k | add_bad_null("unknown token while reading object; treating as null"); |
503 | 172k | continue; |
504 | 175k | } |
505 | | |
506 | 6.39k | warn("unknown token while reading object; treating as string"); |
507 | 6.39k | check_too_many_bad_tokens(); |
508 | 6.39k | add_scalar<QPDF_String>(tokenizer_.getValue()); |
509 | | |
510 | 6.39k | continue; |
511 | | |
512 | 51.5k | case QPDFTokenizer::tt_string: |
513 | 51.5k | { |
514 | 51.5k | auto const& val = tokenizer_.getValue(); |
515 | 51.5k | if (decrypter_) { |
516 | 7.51k | if (b_contents) { |
517 | 571 | frame_->contents_string = val; |
518 | 571 | frame_->contents_offset = input_.getLastOffset(); |
519 | 571 | b_contents = false; |
520 | 571 | } |
521 | 7.51k | std::string s{val}; |
522 | 7.51k | decrypter_->decryptString(s); |
523 | 7.51k | add_scalar<QPDF_String>(s); |
524 | 44.0k | } else { |
525 | 44.0k | add_scalar<QPDF_String>(val); |
526 | 44.0k | } |
527 | 51.5k | } |
528 | 51.5k | continue; |
529 | | |
530 | 0 | default: |
531 | 0 | add_bad_null("treating unknown token type as null while reading object"); |
532 | 4.39M | } |
533 | 4.39M | } |
534 | 169k | } |
535 | | |
536 | | void |
537 | | Parser::add(std::shared_ptr<QPDFObject>&& obj) |
538 | 4.03M | { |
539 | 4.03M | 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.11M | frame_->olist.emplace_back(std::move(obj)); |
543 | 3.11M | } else { |
544 | 919k | if (auto res = frame_->dict.insert_or_assign(frame_->key, std::move(obj)); !res.second) { |
545 | 33.4k | warn_duplicate_key(); |
546 | 33.4k | } |
547 | 919k | frame_->state = st_dictionary_key; |
548 | 919k | } |
549 | 4.03M | } |
550 | | |
551 | | void |
552 | | Parser::add_null() |
553 | 268k | { |
554 | 268k | const static ObjectPtr null_obj = QPDFObject::create<QPDF_Null>(); |
555 | | |
556 | 268k | 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 | 235k | frame_->olist.emplace_back(null_obj); |
560 | 235k | } else { |
561 | 32.6k | if (auto res = frame_->dict.insert_or_assign(frame_->key, null_obj); !res.second) { |
562 | 3.67k | warn_duplicate_key(); |
563 | 3.67k | } |
564 | 32.6k | frame_->state = st_dictionary_key; |
565 | 32.6k | } |
566 | 268k | ++frame_->null_count; |
567 | 268k | } |
568 | | |
569 | | void |
570 | | Parser::add_bad_null(std::string const& msg) |
571 | 179k | { |
572 | 179k | warn(msg); |
573 | 179k | check_too_many_bad_tokens(); |
574 | 179k | add_null(); |
575 | 179k | } |
576 | | |
577 | | void |
578 | | Parser::add_int(int count) |
579 | 1.62M | { |
580 | 1.62M | auto obj = QPDFObject::create<QPDF_Integer>(int_buffer_[count % 2]); |
581 | 1.62M | obj->setDescription(context_, description_, last_offset_buffer_[count % 2]); |
582 | 1.62M | add(std::move(obj)); |
583 | 1.62M | } |
584 | | |
585 | | template <typename T, typename... Args> |
586 | | void |
587 | | Parser::add_scalar(Args&&... args) |
588 | 1.74M | { |
589 | 1.74M | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); |
590 | 1.74M | 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 | 297 | max_bad_count_ = 1; |
594 | 297 | check_too_many_bad_tokens(); // always throws Error() |
595 | 297 | } |
596 | 1.74M | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); |
597 | 1.74M | obj->setDescription(context_, description_, input_.getLastOffset()); |
598 | 1.74M | add(std::move(obj)); |
599 | 1.74M | } void qpdf::impl::Parser::add_scalar<QPDF_Bool, bool>(bool&&) Line | Count | Source | 588 | 13.2k | { | 589 | 13.2k | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); | 590 | 13.2k | 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 | 40 | max_bad_count_ = 1; | 594 | 40 | check_too_many_bad_tokens(); // always throws Error() | 595 | 40 | } | 596 | 13.2k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 597 | 13.2k | obj->setDescription(context_, description_, input_.getLastOffset()); | 598 | 13.2k | add(std::move(obj)); | 599 | 13.2k | } |
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 | 99.0k | { | 589 | 99.0k | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); | 590 | 99.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 | 45 | max_bad_count_ = 1; | 594 | 45 | check_too_many_bad_tokens(); // always throws Error() | 595 | 45 | } | 596 | 99.0k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 597 | 99.0k | obj->setDescription(context_, description_, input_.getLastOffset()); | 598 | 99.0k | add(std::move(obj)); | 599 | 99.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 | 1.57M | { | 589 | 1.57M | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); | 590 | 1.57M | 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 | 159 | max_bad_count_ = 1; | 594 | 159 | check_too_many_bad_tokens(); // always throws Error() | 595 | 159 | } | 596 | 1.57M | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 597 | 1.57M | obj->setDescription(context_, description_, input_.getLastOffset()); | 598 | 1.57M | add(std::move(obj)); | 599 | 1.57M | } |
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 | 50.2k | { | 589 | 50.2k | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); | 590 | 50.2k | 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 | 44 | max_bad_count_ = 1; | 594 | 44 | check_too_many_bad_tokens(); // always throws Error() | 595 | 44 | } | 596 | 50.2k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 597 | 50.2k | obj->setDescription(context_, description_, input_.getLastOffset()); | 598 | 50.2k | add(std::move(obj)); | 599 | 50.2k | } |
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 | 7.51k | { | 589 | 7.51k | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); | 590 | 7.51k | 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 | 9 | max_bad_count_ = 1; | 594 | 9 | check_too_many_bad_tokens(); // always throws Error() | 595 | 9 | } | 596 | 7.51k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 597 | 7.51k | obj->setDescription(context_, description_, input_.getLastOffset()); | 598 | 7.51k | add(std::move(obj)); | 599 | 7.51k | } |
|
600 | | |
601 | | template <typename T, typename... Args> |
602 | | QPDFObjectHandle |
603 | | Parser::with_description(Args&&... args) |
604 | 25.9k | { |
605 | 25.9k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); |
606 | 25.9k | obj->setDescription(context_, description_, start_); |
607 | 25.9k | return {obj}; |
608 | 25.9k | } QPDFObjectHandle qpdf::impl::Parser::with_description<QPDF_Bool, bool>(bool&&) Line | Count | Source | 604 | 442 | { | 605 | 442 | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 606 | 442 | obj->setDescription(context_, description_, start_); | 607 | 442 | return {obj}; | 608 | 442 | } |
QPDFObjectHandle qpdf::impl::Parser::with_description<QPDF_Integer, long long>(long long&&) Line | Count | Source | 604 | 12.3k | { | 605 | 12.3k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 606 | 12.3k | obj->setDescription(context_, description_, start_); | 607 | 12.3k | return {obj}; | 608 | 12.3k | } |
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 | 980 | { | 605 | 980 | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 606 | 980 | obj->setDescription(context_, description_, start_); | 607 | 980 | return {obj}; | 608 | 980 | } |
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.55k | { | 605 | 1.55k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 606 | 1.55k | obj->setDescription(context_, description_, start_); | 607 | 1.55k | return {obj}; | 608 | 1.55k | } |
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 | 10.5k | { | 605 | 10.5k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 606 | 10.5k | obj->setDescription(context_, description_, start_); | 607 | 10.5k | return {obj}; | 608 | 10.5k | } |
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 | 38 | { | 605 | 38 | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 606 | 38 | obj->setDescription(context_, description_, start_); | 607 | 38 | return {obj}; | 608 | 38 | } |
|
609 | | |
610 | | void |
611 | | Parser::set_description(ObjectPtr& obj, qpdf_offset_t parsed_offset) |
612 | 341k | { |
613 | 341k | if (obj) { |
614 | 341k | obj->setDescription(context_, description_, parsed_offset); |
615 | 341k | } |
616 | 341k | } |
617 | | |
618 | | void |
619 | | Parser::fix_missing_keys() |
620 | 1.75k | { |
621 | 1.75k | std::set<std::string> names; |
622 | 6.27k | for (auto& obj: frame_->olist) { |
623 | 6.27k | if (obj.raw_type_code() == ::ot_name) { |
624 | 148 | names.insert(obj.obj_sp()->getStringValue()); |
625 | 148 | } |
626 | 6.27k | } |
627 | 1.75k | int next_fake_key = 1; |
628 | 6.26k | for (auto const& item: frame_->olist) { |
629 | 6.28k | while (true) { |
630 | 6.28k | const std::string key = "/QPDFFake" + std::to_string(next_fake_key++); |
631 | 6.28k | const bool found_fake = !frame_->dict.contains(key) && !names.contains(key); |
632 | 6.28k | QTC::TC("qpdf", "QPDFParser found fake", (found_fake ? 0 : 1)); |
633 | 6.28k | if (found_fake) { |
634 | 6.26k | warn( |
635 | 6.26k | frame_->offset, |
636 | 6.26k | "expected dictionary key but found non-name object; inserting key " + key); |
637 | 6.26k | frame_->dict[key] = item; |
638 | 6.26k | break; |
639 | 6.26k | } |
640 | 6.28k | } |
641 | 6.26k | } |
642 | 1.75k | } |
643 | | |
644 | | void |
645 | | Parser::check_too_many_bad_tokens() |
646 | 210k | { |
647 | 210k | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); |
648 | 210k | if (frame_->olist.size() >= limit || frame_->dict.size() >= limit) { |
649 | 302 | if (bad_count_) { |
650 | 260 | limits_error( |
651 | 260 | "parser-max-container-size-damaged", |
652 | 260 | "encountered errors while parsing an array or dictionary with more than " + |
653 | 260 | std::to_string(limit) + " elements; giving up on reading object"); |
654 | 260 | } |
655 | 302 | limits_error( |
656 | 302 | "parser-max-container-size", |
657 | 302 | "encountered an array or dictionary with more than " + std::to_string(limit) + |
658 | 302 | " elements during xref recovery; giving up on reading object"); |
659 | 302 | } |
660 | 210k | if (max_bad_count_ && --max_bad_count_ == 0) { |
661 | 899 | limits_error( |
662 | 899 | "parser-max-errors", "too many errors during parsing; treating object as null"); |
663 | 899 | } |
664 | 210k | if (good_count_ > 4) { |
665 | 77.6k | good_count_ = 0; |
666 | 77.6k | bad_count_ = 1; |
667 | 77.6k | return; |
668 | 77.6k | } |
669 | 132k | if (++bad_count_ > 5 || |
670 | 126k | (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 | 6.23k | warn("too many errors; giving up on reading object"); |
674 | 6.23k | throw Error(); |
675 | 6.23k | } |
676 | 126k | good_count_ = 0; |
677 | 126k | } |
678 | | |
679 | | void |
680 | | Parser::limits_error(std::string const& limit, std::string const& msg) |
681 | 1.26k | { |
682 | 1.26k | Limits::error(); |
683 | 1.26k | warn("limits error("s + limit + "): " + msg); |
684 | 1.26k | throw Error(); |
685 | 1.26k | } |
686 | | |
687 | | void |
688 | | Parser::warn(QPDFExc const& e) const |
689 | 351k | { |
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 | 351k | if (context_) { |
694 | 351k | context_->warn(e); |
695 | 351k | } else { |
696 | 0 | throw e; |
697 | 0 | } |
698 | 351k | } |
699 | | |
700 | | void |
701 | | Parser::warn_duplicate_key() |
702 | 37.1k | { |
703 | 37.1k | warn( |
704 | 37.1k | frame_->offset, |
705 | 37.1k | "dictionary has duplicated key " + frame_->key + |
706 | 37.1k | "; last occurrence overrides earlier ones"); |
707 | 37.1k | } |
708 | | |
709 | | void |
710 | | Parser::warn(qpdf_offset_t offset, std::string const& msg) const |
711 | 351k | { |
712 | 351k | if (stream_id_) { |
713 | 22.0k | std::string descr = "object "s + std::to_string(obj_id_) + " 0"; |
714 | 22.0k | std::string name = context_->getFilename() + " object stream " + std::to_string(stream_id_); |
715 | 22.0k | warn(QPDFExc(qpdf_e_damaged_pdf, name, descr, offset, msg)); |
716 | 329k | } else { |
717 | 329k | warn(QPDFExc(qpdf_e_damaged_pdf, input_.getName(), object_description_, offset, msg)); |
718 | 329k | } |
719 | 351k | } |
720 | | |
721 | | void |
722 | | Parser::warn(std::string const& msg) const |
723 | 260k | { |
724 | 260k | warn(input_.getLastOffset(), msg); |
725 | 260k | } |