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