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