/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 | 27.1M | objects(qpdf ? &qpdf->m->objects : nullptr) |
27 | 27.1M | { |
28 | 27.1M | if (objects) { |
29 | 27.1M | objects->inParse(true); |
30 | 27.1M | } |
31 | 27.1M | } |
32 | | |
33 | | static std::shared_ptr<QPDFObject> |
34 | | getObject(QPDF* qpdf, int id, int gen, bool parse_pdf) |
35 | 5.17M | { |
36 | 5.17M | return qpdf->m->objects.getObjectForParser(id, gen, parse_pdf); |
37 | 5.17M | } |
38 | | |
39 | | ~ParseGuard() |
40 | 27.1M | { |
41 | 27.1M | if (objects) { |
42 | 27.1M | objects->inParse(false); |
43 | 27.1M | } |
44 | 27.1M | } |
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 | 54.6k | { |
54 | 54.6k | qpdf::Tokenizer tokenizer; |
55 | 54.6k | if (auto result = Parser( |
56 | 54.6k | input, |
57 | 54.6k | make_description(input.getName(), object_description), |
58 | 54.6k | object_description, |
59 | 54.6k | tokenizer, |
60 | 54.6k | nullptr, |
61 | 54.6k | context, |
62 | 54.6k | false) |
63 | 54.6k | .parse()) { |
64 | 54.0k | return result; |
65 | 54.0k | } |
66 | 636 | return {QPDFObject::create<QPDF_Null>()}; |
67 | 54.6k | } |
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 | 24.8M | { |
76 | 24.8M | static const std::string content("content"); // GCC12 - make constexpr |
77 | 24.8M | auto p = Parser( |
78 | 24.8M | input, |
79 | 24.8M | std::move(sp_description), |
80 | 24.8M | content, |
81 | 24.8M | tokenizer, |
82 | 24.8M | nullptr, |
83 | 24.8M | context, |
84 | 24.8M | true, |
85 | 24.8M | 0, |
86 | 24.8M | 0, |
87 | 24.8M | context && context->doc().reconstructed_xref()); |
88 | 24.8M | if (auto result = p.parse(true)) { |
89 | 24.8M | return {result, false}; |
90 | 24.8M | } |
91 | 22.5k | return {{}, p.empty_}; |
92 | 24.8M | } |
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.84M | { |
130 | 1.84M | return Parser( |
131 | 1.84M | input, |
132 | 1.84M | make_description(input.getName(), object_description), |
133 | 1.84M | object_description, |
134 | 1.84M | tokenizer, |
135 | 1.84M | decrypter, |
136 | 1.84M | &context, |
137 | 1.84M | true, |
138 | 1.84M | 0, |
139 | 1.84M | 0, |
140 | 1.84M | sanity_checks) |
141 | 1.84M | .parse(); |
142 | 1.84M | } |
143 | | |
144 | | QPDFObjectHandle |
145 | | Parser::parse( |
146 | | is::OffsetBuffer& input, int stream_id, int obj_id, qpdf::Tokenizer& tokenizer, QPDF& context) |
147 | 436k | { |
148 | 436k | return Parser( |
149 | 436k | input, |
150 | 436k | std::make_shared<QPDFObject::Description>( |
151 | 436k | QPDFObject::ObjStreamDescr(stream_id, obj_id)), |
152 | 436k | "", |
153 | 436k | tokenizer, |
154 | 436k | nullptr, |
155 | 436k | &context, |
156 | 436k | true, |
157 | 436k | stream_id, |
158 | 436k | obj_id) |
159 | 436k | .parse(); |
160 | 436k | } |
161 | | |
162 | | QPDFObjectHandle |
163 | | Parser::parse(bool content_stream) |
164 | 27.1M | { |
165 | 27.1M | try { |
166 | 27.1M | return parse_first(content_stream); |
167 | 27.1M | } catch (Error&) { |
168 | 114k | return {}; |
169 | 114k | } catch (QPDFExc& e) { |
170 | 50.7k | throw e; |
171 | 50.7k | } catch (std::logic_error& e) { |
172 | 30 | 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 | 27.1M | } |
178 | | |
179 | | QPDFObjectHandle |
180 | | Parser::parse_first(bool content_stream) |
181 | 27.1M | { |
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 | 27.1M | QPDF::Doc::ParseGuard pg(context_); |
188 | 27.1M | start_ = input_.tell(); |
189 | 27.1M | if (!tokenizer_.nextToken(input_, object_description_)) { |
190 | 30.2k | warn(tokenizer_.getErrorMessage()); |
191 | 30.2k | } |
192 | | |
193 | 27.1M | switch (tokenizer_.getType()) { |
194 | 17.0k | case QPDFTokenizer::tt_eof: |
195 | 17.0k | if (content_stream) { |
196 | | // In content stream mode, leave object uninitialized to indicate EOF |
197 | 11.1k | empty_ = true; |
198 | 11.1k | return {}; |
199 | 11.1k | } |
200 | 5.91k | warn("unexpected EOF"); |
201 | 5.91k | return {}; |
202 | | |
203 | 27.9k | case QPDFTokenizer::tt_bad: |
204 | 27.9k | return {}; |
205 | | |
206 | 2.43k | case QPDFTokenizer::tt_brace_open: |
207 | 4.50k | case QPDFTokenizer::tt_brace_close: |
208 | 4.50k | warn("treating unexpected brace token as null"); |
209 | 4.50k | return {}; |
210 | | |
211 | 7.89k | case QPDFTokenizer::tt_array_close: |
212 | 7.89k | warn("treating unexpected array close token as null"); |
213 | 7.89k | return {}; |
214 | | |
215 | 6.93k | case QPDFTokenizer::tt_dict_close: |
216 | 6.93k | warn("unexpected dictionary close token"); |
217 | 6.93k | return {}; |
218 | | |
219 | 198k | case QPDFTokenizer::tt_array_open: |
220 | 2.05M | case QPDFTokenizer::tt_dict_open: |
221 | 2.05M | stack_.clear(); |
222 | 2.05M | stack_.emplace_back( |
223 | 2.05M | input_, |
224 | 2.05M | (tokenizer_.getType() == QPDFTokenizer::tt_array_open) ? st_array : st_dictionary_key); |
225 | 2.05M | frame_ = &stack_.back(); |
226 | 2.05M | return parse_remainder(content_stream); |
227 | | |
228 | 10.6k | case QPDFTokenizer::tt_bool: |
229 | 10.6k | return with_description<QPDF_Bool>(tokenizer_.getValue() == "true"); |
230 | | |
231 | 3.23k | case QPDFTokenizer::tt_null: |
232 | 3.23k | return {QPDFObject::create<QPDF_Null>()}; |
233 | | |
234 | 948k | case QPDFTokenizer::tt_integer: |
235 | 948k | return with_description<QPDF_Integer>(QUtil::string_to_ll(tokenizer_.getValue().c_str())); |
236 | | |
237 | 336k | case QPDFTokenizer::tt_real: |
238 | 336k | return with_description<QPDF_Real>(tokenizer_.getValue()); |
239 | | |
240 | 357k | case QPDFTokenizer::tt_name: |
241 | 357k | return with_description<QPDF_Name>(tokenizer_.getValue()); |
242 | | |
243 | 23.3M | case QPDFTokenizer::tt_word: |
244 | 23.3M | { |
245 | 23.3M | auto const& value = tokenizer_.getValue(); |
246 | 23.3M | if (content_stream) { |
247 | 23.2M | return with_description<QPDF_Operator>(value); |
248 | 23.2M | } 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.76k | empty_ = true; |
254 | 2.76k | input_.seek(input_.getLastOffset(), SEEK_SET); |
255 | 2.76k | if (!content_stream) { |
256 | 2.76k | warn("empty object treated as null"); |
257 | 2.76k | } |
258 | 2.76k | return {}; |
259 | 99.5k | } else { |
260 | 99.5k | warn("unknown token while reading object; treating as string"); |
261 | 99.5k | return with_description<QPDF_String>(value); |
262 | 99.5k | } |
263 | 23.3M | } |
264 | | |
265 | 69.2k | case QPDFTokenizer::tt_string: |
266 | 69.2k | if (decrypter_) { |
267 | 2.21k | std::string s{tokenizer_.getValue()}; |
268 | 2.21k | decrypter_->decryptString(s); |
269 | 2.21k | return with_description<QPDF_String>(s); |
270 | 67.0k | } else { |
271 | 67.0k | return with_description<QPDF_String>(tokenizer_.getValue()); |
272 | 67.0k | } |
273 | | |
274 | 0 | default: |
275 | 0 | warn("treating unknown token type as null while reading object"); |
276 | 0 | return {}; |
277 | 27.1M | } |
278 | 27.1M | } |
279 | | |
280 | | QPDFObjectHandle |
281 | | Parser::parse_remainder(bool content_stream) |
282 | 2.05M | { |
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 | 2.05M | bad_count_ = 0; |
289 | 2.05M | bool b_contents = false; |
290 | | |
291 | 136M | while (true) { |
292 | 136M | if (!tokenizer_.nextToken(input_, object_description_)) { |
293 | 522k | warn(tokenizer_.getErrorMessage()); |
294 | 522k | } |
295 | 136M | ++good_count_; // optimistically |
296 | | |
297 | 136M | if (int_count_ != 0) { |
298 | | // Special handling of indirect references. Treat integer tokens as part of an indirect |
299 | | // reference until proven otherwise. |
300 | 42.8M | if (tokenizer_.getType() == QPDFTokenizer::tt_integer) { |
301 | 34.2M | if (++int_count_ > 2) { |
302 | | // Process the oldest buffered integer. |
303 | 28.1M | add_int(int_count_); |
304 | 28.1M | } |
305 | 34.2M | last_offset_buffer_[int_count_ % 2] = input_.getLastOffset(); |
306 | 34.2M | int_buffer_[int_count_ % 2] = QUtil::string_to_ll(tokenizer_.getValue().c_str()); |
307 | 34.2M | continue; |
308 | | |
309 | 34.2M | } else if ( |
310 | 8.59M | int_count_ >= 2 && tokenizer_.getType() == QPDFTokenizer::tt_word && |
311 | 5.49M | tokenizer_.getValue() == "R") { |
312 | 5.21M | 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.21M | auto id = QIntC::to_int(int_buffer_[(int_count_ - 1) % 2]); |
318 | 5.21M | auto gen = QIntC::to_int(int_buffer_[(int_count_) % 2]); |
319 | 5.21M | if (!(id < 1 || gen < 0 || gen >= 65535)) { |
320 | 5.17M | add(ParseGuard::getObject(context_, id, gen, parse_pdf_)); |
321 | 5.17M | } else { |
322 | 37.0k | add_bad_null( |
323 | 37.0k | "treating bad indirect reference (" + std::to_string(id) + " " + |
324 | 37.0k | std::to_string(gen) + " R) as null"); |
325 | 37.0k | } |
326 | 5.21M | int_count_ = 0; |
327 | 5.21M | continue; |
328 | | |
329 | 5.21M | } else if (int_count_ > 0) { |
330 | | // Process the buffered integers before processing the current token. |
331 | 3.38M | if (int_count_ > 1) { |
332 | 915k | add_int(int_count_ - 1); |
333 | 915k | } |
334 | 3.38M | add_int(int_count_); |
335 | 3.38M | int_count_ = 0; |
336 | 3.38M | } |
337 | 42.8M | } |
338 | | |
339 | 97.3M | switch (tokenizer_.getType()) { |
340 | 93.9k | case QPDFTokenizer::tt_eof: |
341 | 93.9k | warn("parse error while reading object"); |
342 | 93.9k | if (content_stream) { |
343 | | // In content stream mode, leave object uninitialized to indicate EOF |
344 | 559 | return {}; |
345 | 559 | } |
346 | 93.3k | warn("unexpected EOF"); |
347 | 93.3k | return {}; |
348 | | |
349 | 425k | case QPDFTokenizer::tt_bad: |
350 | 425k | check_too_many_bad_tokens(); |
351 | 425k | add_null(); |
352 | 425k | continue; |
353 | | |
354 | 31.3k | case QPDFTokenizer::tt_brace_open: |
355 | 58.2k | case QPDFTokenizer::tt_brace_close: |
356 | 58.2k | add_bad_null("treating unexpected brace token as null"); |
357 | 58.2k | continue; |
358 | | |
359 | 1.59M | case QPDFTokenizer::tt_array_close: |
360 | 1.59M | if (frame_->state == st_array) { |
361 | 1.56M | auto object = frame_->null_count > 100 |
362 | 1.56M | ? QPDFObject::create<QPDF_Array>(std::move(frame_->olist), true) |
363 | 1.56M | : QPDFObject::create<QPDF_Array>(std::move(frame_->olist)); |
364 | 1.56M | 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.56M | if (stack_.size() <= 1) { |
370 | 91.7k | return object; |
371 | 91.7k | } |
372 | 1.47M | stack_.pop_back(); |
373 | 1.47M | frame_ = &stack_.back(); |
374 | 1.47M | add(std::move(object)); |
375 | 1.47M | } else { |
376 | 28.6k | if (sanity_checks_) { |
377 | | // During sanity checks, assume nesting of containers is corrupt and object is |
378 | | // unusable. |
379 | 18.5k | warn("unexpected array close token; giving up on reading object"); |
380 | 18.5k | return {}; |
381 | 18.5k | } |
382 | 10.1k | add_bad_null("treating unexpected array close token as null"); |
383 | 10.1k | } |
384 | 1.48M | continue; |
385 | | |
386 | 2.50M | case QPDFTokenizer::tt_dict_close: |
387 | 2.50M | if (frame_->state <= st_dictionary_value) { |
388 | | // Attempt to recover more or less gracefully from invalid dictionaries. |
389 | 2.47M | auto& dict = frame_->dict; |
390 | | |
391 | 2.47M | if (frame_->state == st_dictionary_value) { |
392 | 137k | warn( |
393 | 137k | frame_->offset, |
394 | 137k | "dictionary ended prematurely; using null as value for last key"); |
395 | 137k | dict[frame_->key] = QPDFObject::create<QPDF_Null>(); |
396 | 137k | } |
397 | 2.47M | if (!frame_->olist.empty()) { |
398 | 644k | if (sanity_checks_) { |
399 | 604k | warn( |
400 | 604k | frame_->offset, |
401 | 604k | "expected dictionary keys but found non-name objects; ignoring"); |
402 | 604k | } else { |
403 | 40.0k | fix_missing_keys(); |
404 | 40.0k | } |
405 | 644k | } |
406 | | |
407 | 2.47M | if (!frame_->contents_string.empty() && dict.contains("/Type") && |
408 | 1.53k | dict["/Type"].isNameAndEquals("/Sig") && dict.contains("/ByteRange") && |
409 | 541 | dict.contains("/Contents") && dict["/Contents"].isString()) { |
410 | 453 | dict["/Contents"] = QPDFObjectHandle::newString(frame_->contents_string); |
411 | 453 | dict["/Contents"].setParsedOffset(frame_->contents_offset); |
412 | 453 | } |
413 | 2.47M | auto object = QPDFObject::create<QPDF_Dictionary>(std::move(dict)); |
414 | 2.47M | 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.47M | if (stack_.size() <= 1) { |
420 | 1.64M | return object; |
421 | 1.64M | } |
422 | 834k | stack_.pop_back(); |
423 | 834k | frame_ = &stack_.back(); |
424 | 834k | add(std::move(object)); |
425 | 834k | } else { |
426 | 23.5k | if (sanity_checks_) { |
427 | | // During sanity checks, assume nesting of containers is corrupt and object is |
428 | | // unusable. |
429 | 15.4k | warn("unexpected dictionary close token; giving up on reading object"); |
430 | 15.4k | return {}; |
431 | 15.4k | } |
432 | 8.09k | add_bad_null("unexpected dictionary close token"); |
433 | 8.09k | } |
434 | 843k | continue; |
435 | | |
436 | 2.52M | case QPDFTokenizer::tt_array_open: |
437 | 3.77M | case QPDFTokenizer::tt_dict_open: |
438 | 3.77M | if (stack_.size() > max_nesting) { |
439 | 1.10k | limits_error( |
440 | 1.10k | "parser-max-nesting", "ignoring excessively deeply nested data structure"); |
441 | 1.10k | } |
442 | 3.77M | b_contents = false; |
443 | 3.77M | stack_.emplace_back( |
444 | 3.77M | input_, |
445 | 3.77M | (tokenizer_.getType() == QPDFTokenizer::tt_array_open) ? st_array |
446 | 3.77M | : st_dictionary_key); |
447 | 3.77M | frame_ = &stack_.back(); |
448 | 3.77M | continue; |
449 | | |
450 | 147k | case QPDFTokenizer::tt_bool: |
451 | 147k | add_scalar<QPDF_Bool>(tokenizer_.getValue() == "true"); |
452 | 147k | continue; |
453 | | |
454 | 1.05M | case QPDFTokenizer::tt_null: |
455 | 1.05M | add_null(); |
456 | 1.05M | continue; |
457 | | |
458 | 9.30M | case QPDFTokenizer::tt_integer: |
459 | 9.30M | if (!content_stream) { |
460 | | // Buffer token in case it is part of an indirect reference. |
461 | 8.59M | last_offset_buffer_[1] = input_.getLastOffset(); |
462 | 8.59M | int_buffer_[1] = QUtil::string_to_ll(tokenizer_.getValue().c_str()); |
463 | 8.59M | int_count_ = 1; |
464 | 8.59M | } else { |
465 | 710k | add_scalar<QPDF_Integer>(QUtil::string_to_ll(tokenizer_.getValue().c_str())); |
466 | 710k | } |
467 | 9.30M | continue; |
468 | | |
469 | 1.13M | case QPDFTokenizer::tt_real: |
470 | 1.13M | add_scalar<QPDF_Real>(tokenizer_.getValue()); |
471 | 1.13M | continue; |
472 | | |
473 | 71.2M | case QPDFTokenizer::tt_name: |
474 | 71.2M | if (frame_->state == st_dictionary_key) { |
475 | 10.1M | frame_->key = tokenizer_.getValue(); |
476 | 10.1M | frame_->state = st_dictionary_value; |
477 | 10.1M | b_contents = decrypter_ && frame_->key == "/Contents"; |
478 | 10.1M | continue; |
479 | 61.0M | } else { |
480 | 61.0M | add_scalar<QPDF_Name>(tokenizer_.getValue()); |
481 | 61.0M | } |
482 | 61.0M | continue; |
483 | | |
484 | 61.0M | case QPDFTokenizer::tt_word: |
485 | 3.74M | if (content_stream) { |
486 | 1.04M | add_scalar<QPDF_Operator>(tokenizer_.getValue()); |
487 | 1.04M | continue; |
488 | 1.04M | } |
489 | | |
490 | 2.70M | if (sanity_checks_) { |
491 | 2.59M | 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 | 29.7k | warn( |
495 | 29.7k | "unexpected 'endobj' or 'endstream' while reading object; giving up on " |
496 | 29.7k | "reading object"); |
497 | 29.7k | return {}; |
498 | 29.7k | } |
499 | | |
500 | 2.56M | add_bad_null("unknown token while reading object; treating as null"); |
501 | 2.56M | continue; |
502 | 2.59M | } |
503 | | |
504 | 110k | warn("unknown token while reading object; treating as string"); |
505 | 110k | check_too_many_bad_tokens(); |
506 | 110k | add_scalar<QPDF_String>(tokenizer_.getValue()); |
507 | | |
508 | 110k | continue; |
509 | | |
510 | 2.23M | case QPDFTokenizer::tt_string: |
511 | 2.23M | { |
512 | 2.23M | auto const& val = tokenizer_.getValue(); |
513 | 2.23M | if (decrypter_) { |
514 | 293k | if (b_contents) { |
515 | 7.86k | frame_->contents_string = val; |
516 | 7.86k | frame_->contents_offset = input_.getLastOffset(); |
517 | 7.86k | b_contents = false; |
518 | 7.86k | } |
519 | 293k | std::string s{val}; |
520 | 293k | decrypter_->decryptString(s); |
521 | 293k | add_scalar<QPDF_String>(s); |
522 | 1.94M | } else { |
523 | 1.94M | add_scalar<QPDF_String>(val); |
524 | 1.94M | } |
525 | 2.23M | } |
526 | 2.23M | continue; |
527 | | |
528 | 0 | default: |
529 | 0 | add_bad_null("treating unknown token type as null while reading object"); |
530 | 97.3M | } |
531 | 97.3M | } |
532 | 2.05M | } |
533 | | |
534 | | void |
535 | | Parser::add(std::shared_ptr<QPDFObject>&& obj) |
536 | 106M | { |
537 | 106M | 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 | 96.9M | frame_->olist.emplace_back(std::move(obj)); |
541 | 96.9M | } else { |
542 | 9.46M | if (auto res = frame_->dict.insert_or_assign(frame_->key, std::move(obj)); !res.second) { |
543 | 96.5k | warn_duplicate_key(); |
544 | 96.5k | } |
545 | 9.46M | frame_->state = st_dictionary_key; |
546 | 9.46M | } |
547 | 106M | } |
548 | | |
549 | | void |
550 | | Parser::add_null() |
551 | 4.02M | { |
552 | 4.02M | const static ObjectPtr null_obj = QPDFObject::create<QPDF_Null>(); |
553 | | |
554 | 4.02M | 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.60M | frame_->olist.emplace_back(null_obj); |
558 | 3.60M | } else { |
559 | 419k | if (auto res = frame_->dict.insert_or_assign(frame_->key, null_obj); !res.second) { |
560 | 18.2k | warn_duplicate_key(); |
561 | 18.2k | } |
562 | 419k | frame_->state = st_dictionary_key; |
563 | 419k | } |
564 | 4.02M | ++frame_->null_count; |
565 | 4.02M | } |
566 | | |
567 | | void |
568 | | Parser::add_bad_null(std::string const& msg) |
569 | 2.66M | { |
570 | 2.66M | warn(msg); |
571 | 2.66M | check_too_many_bad_tokens(); |
572 | 2.66M | add_null(); |
573 | 2.66M | } |
574 | | |
575 | | void |
576 | | Parser::add_int(int count) |
577 | 32.4M | { |
578 | 32.4M | auto obj = QPDFObject::create<QPDF_Integer>(int_buffer_[count % 2]); |
579 | 32.4M | obj->setDescription(context_, description_, last_offset_buffer_[count % 2]); |
580 | 32.4M | add(std::move(obj)); |
581 | 32.4M | } |
582 | | |
583 | | template <typename T, typename... Args> |
584 | | void |
585 | | Parser::add_scalar(Args&&... args) |
586 | 66.4M | { |
587 | 66.4M | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); |
588 | 66.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 | 4.80k | max_bad_count_ = 1; |
592 | 4.80k | check_too_many_bad_tokens(); // always throws Error() |
593 | 4.80k | } |
594 | 66.4M | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); |
595 | 66.4M | obj->setDescription(context_, description_, input_.getLastOffset()); |
596 | 66.4M | add(std::move(obj)); |
597 | 66.4M | } void qpdf::impl::Parser::add_scalar<QPDF_Bool, bool>(bool&&) Line | Count | Source | 586 | 147k | { | 587 | 147k | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); | 588 | 147k | 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 | 528 | max_bad_count_ = 1; | 592 | 528 | check_too_many_bad_tokens(); // always throws Error() | 593 | 528 | } | 594 | 147k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 595 | 147k | obj->setDescription(context_, description_, input_.getLastOffset()); | 596 | 147k | add(std::move(obj)); | 597 | 147k | } |
void qpdf::impl::Parser::add_scalar<QPDF_Integer, long long>(long long&&) Line | Count | Source | 586 | 710k | { | 587 | 710k | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); | 588 | 710k | 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 | 36 | max_bad_count_ = 1; | 592 | 36 | check_too_many_bad_tokens(); // always throws Error() | 593 | 36 | } | 594 | 710k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 595 | 710k | obj->setDescription(context_, description_, input_.getLastOffset()); | 596 | 710k | add(std::move(obj)); | 597 | 710k | } |
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.13M | { | 587 | 1.13M | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); | 588 | 1.13M | 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 | 810 | max_bad_count_ = 1; | 592 | 810 | check_too_many_bad_tokens(); // always throws Error() | 593 | 810 | } | 594 | 1.13M | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 595 | 1.13M | obj->setDescription(context_, description_, input_.getLastOffset()); | 596 | 1.13M | add(std::move(obj)); | 597 | 1.13M | } |
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 | 61.0M | { | 587 | 61.0M | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); | 588 | 61.0M | 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.39k | max_bad_count_ = 1; | 592 | 2.39k | check_too_many_bad_tokens(); // always throws Error() | 593 | 2.39k | } | 594 | 61.0M | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 595 | 61.0M | obj->setDescription(context_, description_, input_.getLastOffset()); | 596 | 61.0M | add(std::move(obj)); | 597 | 61.0M | } |
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 | 1.04M | { | 587 | 1.04M | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); | 588 | 1.04M | 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 | 131 | max_bad_count_ = 1; | 592 | 131 | check_too_many_bad_tokens(); // always throws Error() | 593 | 131 | } | 594 | 1.04M | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 595 | 1.04M | obj->setDescription(context_, description_, input_.getLastOffset()); | 596 | 1.04M | add(std::move(obj)); | 597 | 1.04M | } |
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 | 2.05M | { | 587 | 2.05M | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); | 588 | 2.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 | 770 | max_bad_count_ = 1; | 592 | 770 | check_too_many_bad_tokens(); // always throws Error() | 593 | 770 | } | 594 | 2.05M | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 595 | 2.05M | obj->setDescription(context_, description_, input_.getLastOffset()); | 596 | 2.05M | add(std::move(obj)); | 597 | 2.05M | } |
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 | 292k | { | 587 | 292k | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); | 588 | 292k | 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 | 131 | max_bad_count_ = 1; | 592 | 131 | check_too_many_bad_tokens(); // always throws Error() | 593 | 131 | } | 594 | 292k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 595 | 292k | obj->setDescription(context_, description_, input_.getLastOffset()); | 596 | 292k | add(std::move(obj)); | 597 | 292k | } |
|
598 | | |
599 | | template <typename T, typename... Args> |
600 | | QPDFObjectHandle |
601 | | Parser::with_description(Args&&... args) |
602 | 25.0M | { |
603 | 25.0M | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); |
604 | 25.0M | obj->setDescription(context_, description_, start_); |
605 | 25.0M | return {obj}; |
606 | 25.0M | } QPDFObjectHandle qpdf::impl::Parser::with_description<QPDF_Bool, bool>(bool&&) Line | Count | Source | 602 | 10.6k | { | 603 | 10.6k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 604 | 10.6k | obj->setDescription(context_, description_, start_); | 605 | 10.6k | return {obj}; | 606 | 10.6k | } |
QPDFObjectHandle qpdf::impl::Parser::with_description<QPDF_Integer, long long>(long long&&) Line | Count | Source | 602 | 940k | { | 603 | 940k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 604 | 940k | obj->setDescription(context_, description_, start_); | 605 | 940k | return {obj}; | 606 | 940k | } |
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 | 336k | { | 603 | 336k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 604 | 336k | obj->setDescription(context_, description_, start_); | 605 | 336k | return {obj}; | 606 | 336k | } |
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 | 357k | { | 603 | 357k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 604 | 357k | obj->setDescription(context_, description_, start_); | 605 | 357k | return {obj}; | 606 | 357k | } |
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 | 23.2M | { | 603 | 23.2M | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 604 | 23.2M | obj->setDescription(context_, description_, start_); | 605 | 23.2M | return {obj}; | 606 | 23.2M | } |
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 | 161k | { | 603 | 161k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 604 | 161k | obj->setDescription(context_, description_, start_); | 605 | 161k | return {obj}; | 606 | 161k | } |
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 | 2.11k | { | 603 | 2.11k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 604 | 2.11k | obj->setDescription(context_, description_, start_); | 605 | 2.11k | return {obj}; | 606 | 2.11k | } |
|
607 | | |
608 | | void |
609 | | Parser::set_description(ObjectPtr& obj, qpdf_offset_t parsed_offset) |
610 | 4.03M | { |
611 | 4.03M | if (obj) { |
612 | 4.03M | obj->setDescription(context_, description_, parsed_offset); |
613 | 4.03M | } |
614 | 4.03M | } |
615 | | |
616 | | void |
617 | | Parser::fix_missing_keys() |
618 | 40.0k | { |
619 | 40.0k | std::set<std::string> names; |
620 | 282k | for (auto& obj: frame_->olist) { |
621 | 282k | if (obj.raw_type_code() == ::ot_name) { |
622 | 7.77k | names.insert(obj.getName()); |
623 | 7.77k | } |
624 | 282k | } |
625 | 40.0k | int next_fake_key = 1; |
626 | 239k | for (auto const& item: frame_->olist) { |
627 | 240k | while (true) { |
628 | 240k | const std::string key = "/QPDFFake" + std::to_string(next_fake_key++); |
629 | 240k | const bool found_fake = !frame_->dict.contains(key) && !names.contains(key); |
630 | 240k | QTC::TC("qpdf", "QPDFParser found fake", (found_fake ? 0 : 1)); |
631 | 240k | if (found_fake) { |
632 | 239k | warn( |
633 | 239k | frame_->offset, |
634 | 239k | "expected dictionary key but found non-name object; inserting key " + key); |
635 | 239k | frame_->dict[key] = item; |
636 | 239k | break; |
637 | 239k | } |
638 | 240k | } |
639 | 239k | } |
640 | 40.0k | } |
641 | | |
642 | | void |
643 | | Parser::check_too_many_bad_tokens() |
644 | 3.29M | { |
645 | 3.29M | auto limit = Limits::parser_max_container_size(bad_count_ || sanity_checks_); |
646 | 3.29M | if (frame_->olist.size() >= limit || frame_->dict.size() >= limit) { |
647 | 4.98k | 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 | 4.98k | limits_error( |
654 | 4.98k | "parser-max-container-size", |
655 | 4.98k | "encountered an array or dictionary with more than " + std::to_string(limit) + |
656 | 4.98k | " elements during xref recovery; giving up on reading object"); |
657 | 4.98k | } |
658 | 3.29M | if (max_bad_count_ && --max_bad_count_ == 0) { |
659 | 21.3k | limits_error( |
660 | 21.3k | "parser-max-errors", "too many errors during parsing; treating object as null"); |
661 | 21.3k | } |
662 | 3.29M | if (good_count_ > 4) { |
663 | 1.17M | good_count_ = 0; |
664 | 1.17M | bad_count_ = 1; |
665 | 1.17M | return; |
666 | 1.17M | } |
667 | 2.12M | if (++bad_count_ > 5 || |
668 | 2.03M | (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 | 87.5k | warn("too many errors; giving up on reading object"); |
672 | 87.5k | throw Error(); |
673 | 87.5k | } |
674 | 2.03M | good_count_ = 0; |
675 | 2.03M | } |
676 | | |
677 | | void |
678 | | Parser::limits_error(std::string const& limit, std::string const& msg) |
679 | 27.4k | { |
680 | 27.4k | Limits::error(); |
681 | 27.4k | warn("limits error("s + limit + "): " + msg); |
682 | 27.4k | throw Error(); |
683 | 27.4k | } |
684 | | |
685 | | void |
686 | | Parser::warn(QPDFExc const& e) const |
687 | 4.93M | { |
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.93M | if (context_) { |
692 | 4.93M | context_->warn(e); |
693 | 4.93M | } else { |
694 | 636 | throw e; |
695 | 636 | } |
696 | 4.93M | } |
697 | | |
698 | | void |
699 | | Parser::warn_duplicate_key() |
700 | 114k | { |
701 | 114k | warn( |
702 | 114k | frame_->offset, |
703 | 114k | "dictionary has duplicated key " + frame_->key + |
704 | 114k | "; last occurrence overrides earlier ones"); |
705 | 114k | check_too_many_bad_tokens(); |
706 | 114k | } |
707 | | |
708 | | void |
709 | | Parser::warn(qpdf_offset_t offset, std::string const& msg) const |
710 | 4.93M | { |
711 | 4.93M | if (stream_id_) { |
712 | 376k | std::string descr = "object "s + std::to_string(obj_id_) + " 0"; |
713 | 376k | std::string name = context_->getFilename() + " object stream " + std::to_string(stream_id_); |
714 | 376k | warn(QPDFExc(qpdf_e_damaged_pdf, name, descr, offset, msg)); |
715 | 4.56M | } else { |
716 | 4.56M | warn(QPDFExc(qpdf_e_damaged_pdf, input_.getName(), object_description_, offset, msg)); |
717 | 4.56M | } |
718 | 4.93M | } |
719 | | |
720 | | void |
721 | | Parser::warn(std::string const& msg) const |
722 | 3.84M | { |
723 | 3.84M | warn(input_.getLastOffset(), msg); |
724 | 3.84M | } |