/src/qpdf/libqpdf/QPDFParser.cc
Line | Count | Source (jump to first uncovered line) |
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 | | QPDFObjectHandle |
19 | | QPDFParser::parse(InputSource& input, std::string const& object_description, QPDF* context) |
20 | 11.0k | { |
21 | 11.0k | qpdf::Tokenizer tokenizer; |
22 | 11.0k | bool empty = false; |
23 | 11.0k | return QPDFParser( |
24 | 11.0k | input, |
25 | 11.0k | make_description(input.getName(), object_description), |
26 | 11.0k | object_description, |
27 | 11.0k | tokenizer, |
28 | 11.0k | nullptr, |
29 | 11.0k | context, |
30 | 11.0k | false) |
31 | 11.0k | .parse(empty, false); |
32 | 11.0k | } |
33 | | |
34 | | QPDFObjectHandle |
35 | | QPDFParser::parse_content( |
36 | | InputSource& input, |
37 | | std::shared_ptr<QPDFObject::Description> sp_description, |
38 | | qpdf::Tokenizer& tokenizer, |
39 | | QPDF* context) |
40 | 0 | { |
41 | 0 | bool empty = false; |
42 | 0 | return QPDFParser( |
43 | 0 | input, |
44 | 0 | std::move(sp_description), |
45 | 0 | "content", |
46 | 0 | tokenizer, |
47 | 0 | nullptr, |
48 | 0 | context, |
49 | 0 | true, |
50 | 0 | 0, |
51 | 0 | 0, |
52 | 0 | context && context->reconstructed_xref()) |
53 | 0 | .parse(empty, true); |
54 | 0 | } |
55 | | |
56 | | QPDFObjectHandle |
57 | | QPDFParser::parse( |
58 | | InputSource& input, |
59 | | std::string const& object_description, |
60 | | QPDFTokenizer& tokenizer, |
61 | | bool& empty, |
62 | | QPDFObjectHandle::StringDecrypter* decrypter, |
63 | | QPDF* context) |
64 | 0 | { |
65 | 0 | return QPDFParser( |
66 | 0 | input, |
67 | 0 | make_description(input.getName(), object_description), |
68 | 0 | object_description, |
69 | 0 | *tokenizer.m, |
70 | 0 | decrypter, |
71 | 0 | context, |
72 | 0 | false) |
73 | 0 | .parse(empty, false); |
74 | 0 | } |
75 | | |
76 | | std::pair<QPDFObjectHandle, bool> |
77 | | QPDFParser::parse( |
78 | | InputSource& input, |
79 | | std::string const& object_description, |
80 | | qpdf::Tokenizer& tokenizer, |
81 | | QPDFObjectHandle::StringDecrypter* decrypter, |
82 | | QPDF& context, |
83 | | bool sanity_checks) |
84 | 7.52k | { |
85 | 7.52k | bool empty{false}; |
86 | 7.52k | auto result = QPDFParser( |
87 | 7.52k | input, |
88 | 7.52k | make_description(input.getName(), object_description), |
89 | 7.52k | object_description, |
90 | 7.52k | tokenizer, |
91 | 7.52k | decrypter, |
92 | 7.52k | &context, |
93 | 7.52k | true, |
94 | 7.52k | 0, |
95 | 7.52k | 0, |
96 | 7.52k | sanity_checks) |
97 | 7.52k | .parse(empty, false); |
98 | 7.52k | return {result, empty}; |
99 | 7.52k | } |
100 | | |
101 | | std::pair<QPDFObjectHandle, bool> |
102 | | QPDFParser::parse( |
103 | | is::OffsetBuffer& input, int stream_id, int obj_id, qpdf::Tokenizer& tokenizer, QPDF& context) |
104 | 0 | { |
105 | 0 | bool empty{false}; |
106 | 0 | auto result = QPDFParser( |
107 | 0 | input, |
108 | 0 | std::make_shared<QPDFObject::Description>( |
109 | 0 | QPDFObject::ObjStreamDescr(stream_id, obj_id)), |
110 | 0 | "", |
111 | 0 | tokenizer, |
112 | 0 | nullptr, |
113 | 0 | &context, |
114 | 0 | true, |
115 | 0 | stream_id, |
116 | 0 | obj_id) |
117 | 0 | .parse(empty, false); |
118 | 0 | return {result, empty}; |
119 | 0 | } |
120 | | |
121 | | QPDFObjectHandle |
122 | | QPDFParser::parse(bool& empty, bool content_stream) |
123 | 18.5k | { |
124 | | // This method must take care not to resolve any objects. Don't check the type of any object |
125 | | // without first ensuring that it is a direct object. Otherwise, doing so may have the side |
126 | | // effect of reading the object and changing the file pointer. If you do this, it will cause a |
127 | | // logic error to be thrown from QPDF::inParse(). |
128 | | |
129 | 18.5k | QPDF::ParseGuard pg(context); |
130 | 18.5k | empty = false; |
131 | 18.5k | start = input.tell(); |
132 | | |
133 | 18.5k | if (!tokenizer.nextToken(input, object_description)) { |
134 | 202 | warn(tokenizer.getErrorMessage()); |
135 | 202 | } |
136 | | |
137 | 18.5k | switch (tokenizer.getType()) { |
138 | 0 | case QPDFTokenizer::tt_eof: |
139 | 0 | if (content_stream) { |
140 | | // In content stream mode, leave object uninitialized to indicate EOF |
141 | 0 | return {}; |
142 | 0 | } |
143 | 0 | QTC::TC("qpdf", "QPDFParser eof in parse"); |
144 | 0 | warn("unexpected EOF"); |
145 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
146 | | |
147 | 0 | case QPDFTokenizer::tt_bad: |
148 | 0 | QTC::TC("qpdf", "QPDFParser bad token in parse"); |
149 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
150 | | |
151 | 0 | case QPDFTokenizer::tt_brace_open: |
152 | 0 | case QPDFTokenizer::tt_brace_close: |
153 | 0 | QTC::TC("qpdf", "QPDFParser bad brace"); |
154 | 0 | warn("treating unexpected brace token as null"); |
155 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
156 | | |
157 | 0 | case QPDFTokenizer::tt_array_close: |
158 | 0 | QTC::TC("qpdf", "QPDFParser bad array close"); |
159 | 0 | warn("treating unexpected array close token as null"); |
160 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
161 | | |
162 | 0 | case QPDFTokenizer::tt_dict_close: |
163 | 0 | QTC::TC("qpdf", "QPDFParser bad dictionary close"); |
164 | 0 | warn("unexpected dictionary close token"); |
165 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
166 | | |
167 | 0 | case QPDFTokenizer::tt_array_open: |
168 | 7.52k | case QPDFTokenizer::tt_dict_open: |
169 | 7.52k | stack.clear(); |
170 | 7.52k | stack.emplace_back( |
171 | 7.52k | input, |
172 | 7.52k | (tokenizer.getType() == QPDFTokenizer::tt_array_open) ? st_array : st_dictionary_key); |
173 | 7.52k | frame = &stack.back(); |
174 | 7.52k | return parseRemainder(content_stream); |
175 | | |
176 | 0 | case QPDFTokenizer::tt_bool: |
177 | 0 | return withDescription<QPDF_Bool>(tokenizer.getValue() == "true"); |
178 | | |
179 | 0 | case QPDFTokenizer::tt_null: |
180 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
181 | | |
182 | 0 | case QPDFTokenizer::tt_integer: |
183 | 0 | return withDescription<QPDF_Integer>(QUtil::string_to_ll(tokenizer.getValue().c_str())); |
184 | | |
185 | 0 | case QPDFTokenizer::tt_real: |
186 | 0 | return withDescription<QPDF_Real>(tokenizer.getValue()); |
187 | | |
188 | 10.8k | case QPDFTokenizer::tt_name: |
189 | 10.8k | return withDescription<QPDF_Name>(tokenizer.getValue()); |
190 | | |
191 | 0 | case QPDFTokenizer::tt_word: |
192 | 0 | { |
193 | 0 | auto const& value = tokenizer.getValue(); |
194 | 0 | if (content_stream) { |
195 | 0 | return withDescription<QPDF_Operator>(value); |
196 | 0 | } else if (value == "endobj") { |
197 | | // We just saw endobj without having read anything. Treat this as a null and do |
198 | | // not move the input source's offset. |
199 | 0 | input.seek(input.getLastOffset(), SEEK_SET); |
200 | 0 | empty = true; |
201 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
202 | 0 | } else { |
203 | 0 | QTC::TC("qpdf", "QPDFParser treat word as string"); |
204 | 0 | warn("unknown token while reading object; treating as string"); |
205 | 0 | return withDescription<QPDF_String>(value); |
206 | 0 | } |
207 | 0 | } |
208 | | |
209 | 0 | case QPDFTokenizer::tt_string: |
210 | 0 | if (decrypter) { |
211 | 0 | std::string s{tokenizer.getValue()}; |
212 | 0 | decrypter->decryptString(s); |
213 | 0 | return withDescription<QPDF_String>(s); |
214 | 0 | } else { |
215 | 0 | return withDescription<QPDF_String>(tokenizer.getValue()); |
216 | 0 | } |
217 | | |
218 | 0 | default: |
219 | 0 | warn("treating unknown token type as null while reading object"); |
220 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
221 | 18.5k | } |
222 | 18.5k | } |
223 | | |
224 | | QPDFObjectHandle |
225 | | QPDFParser::parseRemainder(bool content_stream) |
226 | 7.52k | { |
227 | | // This method must take care not to resolve any objects. Don't check the type of any object |
228 | | // without first ensuring that it is a direct object. Otherwise, doing so may have the side |
229 | | // effect of reading the object and changing the file pointer. If you do this, it will cause a |
230 | | // logic error to be thrown from QPDF::inParse(). |
231 | | |
232 | 7.52k | bad_count = 0; |
233 | 7.52k | bool b_contents = false; |
234 | | |
235 | 22.5k | while (true) { |
236 | 22.5k | if (!tokenizer.nextToken(input, object_description)) { |
237 | 0 | warn(tokenizer.getErrorMessage()); |
238 | 0 | } |
239 | 22.5k | ++good_count; // optimistically |
240 | | |
241 | 22.5k | if (int_count != 0) { |
242 | | // Special handling of indirect references. Treat integer tokens as part of an indirect |
243 | | // reference until proven otherwise. |
244 | 7.52k | if (tokenizer.getType() == QPDFTokenizer::tt_integer) { |
245 | 0 | if (++int_count > 2) { |
246 | | // Process the oldest buffered integer. |
247 | 0 | addInt(int_count); |
248 | 0 | } |
249 | 0 | last_offset_buffer[int_count % 2] = input.getLastOffset(); |
250 | 0 | int_buffer[int_count % 2] = QUtil::string_to_ll(tokenizer.getValue().c_str()); |
251 | 0 | continue; |
252 | |
|
253 | 7.52k | } else if ( |
254 | 7.52k | int_count >= 2 && tokenizer.getType() == QPDFTokenizer::tt_word && |
255 | 7.52k | tokenizer.getValue() == "R") { |
256 | 0 | if (context == nullptr) { |
257 | 0 | QTC::TC("qpdf", "QPDFParser indirect without context"); |
258 | 0 | throw std::logic_error( |
259 | 0 | "QPDFParser::parse called without context on an object " |
260 | 0 | "with indirect references"); |
261 | 0 | } |
262 | 0 | auto id = QIntC::to_int(int_buffer[(int_count - 1) % 2]); |
263 | 0 | auto gen = QIntC::to_int(int_buffer[(int_count) % 2]); |
264 | 0 | if (!(id < 1 || gen < 0 || gen >= 65535)) { |
265 | 0 | add(QPDF::ParseGuard::getObject(context, id, gen, parse_pdf)); |
266 | 0 | } else { |
267 | 0 | QTC::TC("qpdf", "QPDFParser invalid objgen"); |
268 | 0 | addNull(); |
269 | 0 | } |
270 | 0 | int_count = 0; |
271 | 0 | continue; |
272 | |
|
273 | 7.52k | } else if (int_count > 0) { |
274 | | // Process the buffered integers before processing the current token. |
275 | 7.52k | if (int_count > 1) { |
276 | 0 | addInt(int_count - 1); |
277 | 0 | } |
278 | 7.52k | addInt(int_count); |
279 | 7.52k | int_count = 0; |
280 | 7.52k | } |
281 | 7.52k | } |
282 | | |
283 | 22.5k | switch (tokenizer.getType()) { |
284 | 0 | case QPDFTokenizer::tt_eof: |
285 | 0 | warn("parse error while reading object"); |
286 | 0 | if (content_stream) { |
287 | | // In content stream mode, leave object uninitialized to indicate EOF |
288 | 0 | return {}; |
289 | 0 | } |
290 | 0 | QTC::TC("qpdf", "QPDFParser eof in parseRemainder"); |
291 | 0 | warn("unexpected EOF"); |
292 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
293 | | |
294 | 0 | case QPDFTokenizer::tt_bad: |
295 | 0 | QTC::TC("qpdf", "QPDFParser bad token in parseRemainder"); |
296 | 0 | if (tooManyBadTokens()) { |
297 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
298 | 0 | } |
299 | 0 | addNull(); |
300 | 0 | continue; |
301 | | |
302 | 0 | case QPDFTokenizer::tt_brace_open: |
303 | 0 | case QPDFTokenizer::tt_brace_close: |
304 | 0 | QTC::TC("qpdf", "QPDFParser bad brace in parseRemainder"); |
305 | 0 | warn("treating unexpected brace token as null"); |
306 | 0 | if (tooManyBadTokens()) { |
307 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
308 | 0 | } |
309 | 0 | addNull(); |
310 | 0 | continue; |
311 | | |
312 | 0 | case QPDFTokenizer::tt_array_close: |
313 | 0 | if ((bad_count || sanity_checks) && !max_bad_count) { |
314 | | // Trigger warning. |
315 | 0 | (void)tooManyBadTokens(); |
316 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
317 | 0 | } |
318 | 0 | if (frame->state == st_array) { |
319 | 0 | auto object = frame->null_count > 100 |
320 | 0 | ? QPDFObject::create<QPDF_Array>(std::move(frame->olist), true) |
321 | 0 | : QPDFObject::create<QPDF_Array>(std::move(frame->olist)); |
322 | 0 | setDescription(object, frame->offset - 1); |
323 | | // The `offset` points to the next of "[". Set the rewind offset to point to the |
324 | | // beginning of "[". This has been explicitly tested with whitespace surrounding the |
325 | | // array start delimiter. getLastOffset points to the array end token and therefore |
326 | | // can't be used here. |
327 | 0 | if (stack.size() <= 1) { |
328 | 0 | return object; |
329 | 0 | } |
330 | 0 | stack.pop_back(); |
331 | 0 | frame = &stack.back(); |
332 | 0 | add(std::move(object)); |
333 | 0 | } else { |
334 | 0 | QTC::TC("qpdf", "QPDFParser bad array close in parseRemainder"); |
335 | 0 | if (sanity_checks) { |
336 | | // During sanity checks, assume nesting of containers is corrupt and object is |
337 | | // unusable. |
338 | 0 | warn("unexpected array close token; giving up on reading object"); |
339 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
340 | 0 | } |
341 | 0 | warn("treating unexpected array close token as null"); |
342 | 0 | if (tooManyBadTokens()) { |
343 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
344 | 0 | } |
345 | 0 | addNull(); |
346 | 0 | } |
347 | 0 | continue; |
348 | | |
349 | 7.52k | case QPDFTokenizer::tt_dict_close: |
350 | 7.52k | if ((bad_count || sanity_checks) && !max_bad_count) { |
351 | | // Trigger warning. |
352 | 0 | (void)tooManyBadTokens(); |
353 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
354 | 0 | } |
355 | 7.52k | if (frame->state <= st_dictionary_value) { |
356 | | // Attempt to recover more or less gracefully from invalid dictionaries. |
357 | 7.52k | auto& dict = frame->dict; |
358 | | |
359 | 7.52k | if (frame->state == st_dictionary_value) { |
360 | 0 | QTC::TC("qpdf", "QPDFParser no val for last key"); |
361 | 0 | warn( |
362 | 0 | frame->offset, |
363 | 0 | "dictionary ended prematurely; using null as value for last key"); |
364 | 0 | dict[frame->key] = QPDFObject::create<QPDF_Null>(); |
365 | 0 | } |
366 | 7.52k | if (!frame->olist.empty()) { |
367 | 0 | if (sanity_checks) { |
368 | 0 | warn( |
369 | 0 | frame->offset, |
370 | 0 | "expected dictionary keys but found non-name objects; ignoring"); |
371 | 0 | } else { |
372 | 0 | fixMissingKeys(); |
373 | 0 | } |
374 | 0 | } |
375 | | |
376 | 7.52k | if (!frame->contents_string.empty() && dict.contains("/Type") && |
377 | 7.52k | dict["/Type"].isNameAndEquals("/Sig") && dict.contains("/ByteRange") && |
378 | 7.52k | dict.contains("/Contents") && dict["/Contents"].isString()) { |
379 | 0 | dict["/Contents"] = QPDFObjectHandle::newString(frame->contents_string); |
380 | 0 | dict["/Contents"].setParsedOffset(frame->contents_offset); |
381 | 0 | } |
382 | 7.52k | auto object = QPDFObject::create<QPDF_Dictionary>(std::move(dict)); |
383 | 7.52k | setDescription(object, frame->offset - 2); |
384 | | // The `offset` points to the next of "<<". Set the rewind offset to point to the |
385 | | // beginning of "<<". This has been explicitly tested with whitespace surrounding |
386 | | // the dictionary start delimiter. getLastOffset points to the dictionary end token |
387 | | // and therefore can't be used here. |
388 | 7.52k | if (stack.size() <= 1) { |
389 | 7.52k | return object; |
390 | 7.52k | } |
391 | 0 | stack.pop_back(); |
392 | 0 | frame = &stack.back(); |
393 | 0 | add(std::move(object)); |
394 | 0 | } else { |
395 | 0 | QTC::TC("qpdf", "QPDFParser bad dictionary close in parseRemainder"); |
396 | 0 | if (sanity_checks) { |
397 | | // During sanity checks, assume nesting of containers is corrupt and object is |
398 | | // unusable. |
399 | 0 | warn("unexpected dictionary close token; giving up on reading object"); |
400 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
401 | 0 | } |
402 | 0 | warn("unexpected dictionary close token"); |
403 | 0 | if (tooManyBadTokens()) { |
404 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
405 | 0 | } |
406 | 0 | addNull(); |
407 | 0 | } |
408 | 0 | continue; |
409 | | |
410 | 0 | case QPDFTokenizer::tt_array_open: |
411 | 0 | case QPDFTokenizer::tt_dict_open: |
412 | 0 | if (stack.size() > 499) { |
413 | 0 | QTC::TC("qpdf", "QPDFParser too deep"); |
414 | 0 | warn("ignoring excessively deeply nested data structure"); |
415 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
416 | 0 | } else { |
417 | 0 | b_contents = false; |
418 | 0 | stack.emplace_back( |
419 | 0 | input, |
420 | 0 | (tokenizer.getType() == QPDFTokenizer::tt_array_open) ? st_array |
421 | 0 | : st_dictionary_key); |
422 | 0 | frame = &stack.back(); |
423 | 0 | continue; |
424 | 0 | } |
425 | | |
426 | 0 | case QPDFTokenizer::tt_bool: |
427 | 0 | addScalar<QPDF_Bool>(tokenizer.getValue() == "true"); |
428 | 0 | continue; |
429 | | |
430 | 0 | case QPDFTokenizer::tt_null: |
431 | 0 | addNull(); |
432 | 0 | continue; |
433 | | |
434 | 7.52k | case QPDFTokenizer::tt_integer: |
435 | 7.52k | if (!content_stream) { |
436 | | // Buffer token in case it is part of an indirect reference. |
437 | 7.52k | last_offset_buffer[1] = input.getLastOffset(); |
438 | 7.52k | int_buffer[1] = QUtil::string_to_ll(tokenizer.getValue().c_str()); |
439 | 7.52k | int_count = 1; |
440 | 7.52k | } else { |
441 | 0 | addScalar<QPDF_Integer>(QUtil::string_to_ll(tokenizer.getValue().c_str())); |
442 | 0 | } |
443 | 7.52k | continue; |
444 | | |
445 | 0 | case QPDFTokenizer::tt_real: |
446 | 0 | addScalar<QPDF_Real>(tokenizer.getValue()); |
447 | 0 | continue; |
448 | | |
449 | 7.52k | case QPDFTokenizer::tt_name: |
450 | 7.52k | if (frame->state == st_dictionary_key) { |
451 | 7.52k | frame->key = tokenizer.getValue(); |
452 | 7.52k | frame->state = st_dictionary_value; |
453 | 7.52k | b_contents = decrypter && frame->key == "/Contents"; |
454 | 7.52k | continue; |
455 | 7.52k | } else { |
456 | 0 | addScalar<QPDF_Name>(tokenizer.getValue()); |
457 | 0 | } |
458 | 0 | continue; |
459 | | |
460 | 0 | case QPDFTokenizer::tt_word: |
461 | 0 | if (content_stream) { |
462 | 0 | addScalar<QPDF_Operator>(tokenizer.getValue()); |
463 | 0 | continue; |
464 | 0 | } |
465 | | |
466 | 0 | if (sanity_checks) { |
467 | 0 | if (tokenizer.getValue() == "endobj" || tokenizer.getValue() == "endstream") { |
468 | | // During sanity checks, assume an unexpected endobj or endstream indicates that |
469 | | // we are parsing past the end of the object. |
470 | 0 | warn( |
471 | 0 | "unexpected 'endobj' or 'endstream' while reading object; giving up on " |
472 | 0 | "reading object"); |
473 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
474 | 0 | } |
475 | | |
476 | 0 | warn("unknown token while reading object; treating as null"); |
477 | 0 | if (tooManyBadTokens()) { |
478 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
479 | 0 | } |
480 | 0 | addNull(); |
481 | 0 | continue; |
482 | 0 | } |
483 | | |
484 | 0 | QTC::TC("qpdf", "QPDFParser treat word as string in parseRemainder"); |
485 | 0 | warn("unknown token while reading object; treating as string"); |
486 | 0 | if (tooManyBadTokens()) { |
487 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
488 | 0 | } |
489 | 0 | addScalar<QPDF_String>(tokenizer.getValue()); |
490 | |
|
491 | 0 | continue; |
492 | | |
493 | 0 | case QPDFTokenizer::tt_string: |
494 | 0 | { |
495 | 0 | auto const& val = tokenizer.getValue(); |
496 | 0 | if (decrypter) { |
497 | 0 | if (b_contents) { |
498 | 0 | frame->contents_string = val; |
499 | 0 | frame->contents_offset = input.getLastOffset(); |
500 | 0 | b_contents = false; |
501 | 0 | } |
502 | 0 | std::string s{val}; |
503 | 0 | decrypter->decryptString(s); |
504 | 0 | addScalar<QPDF_String>(s); |
505 | 0 | } else { |
506 | 0 | addScalar<QPDF_String>(val); |
507 | 0 | } |
508 | 0 | } |
509 | 0 | continue; |
510 | | |
511 | 0 | default: |
512 | 0 | warn("treating unknown token type as null while reading object"); |
513 | 0 | if (tooManyBadTokens()) { |
514 | 0 | return {QPDFObject::create<QPDF_Null>()}; |
515 | 0 | } |
516 | 0 | addNull(); |
517 | 22.5k | } |
518 | 22.5k | } |
519 | 7.52k | } |
520 | | |
521 | | void |
522 | | QPDFParser::add(std::shared_ptr<QPDFObject>&& obj) |
523 | 7.52k | { |
524 | 7.52k | if (frame->state != st_dictionary_value) { |
525 | | // If state is st_dictionary_key then there is a missing key. Push onto olist for |
526 | | // processing once the tt_dict_close token has been found. |
527 | 0 | frame->olist.emplace_back(std::move(obj)); |
528 | 7.52k | } else { |
529 | 7.52k | if (auto res = frame->dict.insert_or_assign(frame->key, std::move(obj)); !res.second) { |
530 | 0 | warnDuplicateKey(); |
531 | 0 | } |
532 | 7.52k | frame->state = st_dictionary_key; |
533 | 7.52k | } |
534 | 7.52k | } |
535 | | |
536 | | void |
537 | | QPDFParser::addNull() |
538 | 0 | { |
539 | 0 | const static ObjectPtr null_obj = QPDFObject::create<QPDF_Null>(); |
540 | |
|
541 | 0 | if (frame->state != st_dictionary_value) { |
542 | | // If state is st_dictionary_key then there is a missing key. Push onto olist for |
543 | | // processing once the tt_dict_close token has been found. |
544 | 0 | frame->olist.emplace_back(null_obj); |
545 | 0 | } else { |
546 | 0 | if (auto res = frame->dict.insert_or_assign(frame->key, null_obj); !res.second) { |
547 | 0 | warnDuplicateKey(); |
548 | 0 | } |
549 | 0 | frame->state = st_dictionary_key; |
550 | 0 | } |
551 | 0 | ++frame->null_count; |
552 | 0 | } |
553 | | |
554 | | void |
555 | | QPDFParser::addInt(int count) |
556 | 7.52k | { |
557 | 7.52k | auto obj = QPDFObject::create<QPDF_Integer>(int_buffer[count % 2]); |
558 | 7.52k | obj->setDescription(context, description, last_offset_buffer[count % 2]); |
559 | 7.52k | add(std::move(obj)); |
560 | 7.52k | } |
561 | | |
562 | | template <typename T, typename... Args> |
563 | | void |
564 | | QPDFParser::addScalar(Args&&... args) |
565 | 0 | { |
566 | 0 | if ((bad_count || sanity_checks) && |
567 | 0 | (frame->olist.size() > 5'000 || frame->dict.size() > 5'000)) { |
568 | | // Stop adding scalars. We are going to abort when the close token or a bad token is |
569 | | // encountered. |
570 | 0 | max_bad_count = 0; |
571 | 0 | return; |
572 | 0 | } |
573 | 0 | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); |
574 | 0 | obj->setDescription(context, description, input.getLastOffset()); |
575 | 0 | add(std::move(obj)); |
576 | 0 | } Unexecuted instantiation: void QPDFParser::addScalar<QPDF_Bool, bool>(bool&&) Unexecuted instantiation: void QPDFParser::addScalar<QPDF_Integer, long long>(long long&&) Unexecuted instantiation: void QPDFParser::addScalar<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&) Unexecuted instantiation: void QPDFParser::addScalar<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&) Unexecuted instantiation: void QPDFParser::addScalar<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&) Unexecuted instantiation: void QPDFParser::addScalar<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&) Unexecuted instantiation: void QPDFParser::addScalar<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> >&) |
577 | | |
578 | | template <typename T, typename... Args> |
579 | | QPDFObjectHandle |
580 | | QPDFParser::withDescription(Args&&... args) |
581 | 10.8k | { |
582 | 10.8k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); |
583 | 10.8k | obj->setDescription(context, description, start); |
584 | 10.8k | return {obj}; |
585 | 10.8k | } Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_Bool, bool>(bool&&) Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_Integer, long long>(long long&&) Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<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&) QPDFObjectHandle QPDFParser::withDescription<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 | 581 | 10.8k | { | 582 | 10.8k | auto obj = QPDFObject::create<T>(std::forward<Args>(args)...); | 583 | 10.8k | obj->setDescription(context, description, start); | 584 | 10.8k | return {obj}; | 585 | 10.8k | } |
Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<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&) Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<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&) Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<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> >&) |
586 | | |
587 | | void |
588 | | QPDFParser::setDescription(ObjectPtr& obj, qpdf_offset_t parsed_offset) |
589 | 7.52k | { |
590 | 7.52k | if (obj) { |
591 | 7.52k | obj->setDescription(context, description, parsed_offset); |
592 | 7.52k | } |
593 | 7.52k | } |
594 | | |
595 | | void |
596 | | QPDFParser::fixMissingKeys() |
597 | 0 | { |
598 | 0 | std::set<std::string> names; |
599 | 0 | for (auto& obj: frame->olist) { |
600 | 0 | if (obj.getObj()->getTypeCode() == ::ot_name) { |
601 | 0 | names.insert(obj.getObj()->getStringValue()); |
602 | 0 | } |
603 | 0 | } |
604 | 0 | int next_fake_key = 1; |
605 | 0 | for (auto const& item: frame->olist) { |
606 | 0 | while (true) { |
607 | 0 | const std::string key = "/QPDFFake" + std::to_string(next_fake_key++); |
608 | 0 | const bool found_fake = !frame->dict.contains(key) && !names.contains(key); |
609 | 0 | QTC::TC("qpdf", "QPDFParser found fake", (found_fake ? 0 : 1)); |
610 | 0 | if (found_fake) { |
611 | 0 | warn( |
612 | 0 | frame->offset, |
613 | 0 | "expected dictionary key but found non-name object; inserting key " + key); |
614 | 0 | frame->dict[key] = item; |
615 | 0 | break; |
616 | 0 | } |
617 | 0 | } |
618 | 0 | } |
619 | 0 | } |
620 | | |
621 | | bool |
622 | | QPDFParser::tooManyBadTokens() |
623 | 0 | { |
624 | 0 | if (frame->olist.size() > 5'000 || frame->dict.size() > 5'000) { |
625 | 0 | if (bad_count) { |
626 | 0 | warn( |
627 | 0 | "encountered errors while parsing an array or dictionary with more than 5000 " |
628 | 0 | "elements; giving up on reading object"); |
629 | 0 | return true; |
630 | 0 | } |
631 | 0 | warn( |
632 | 0 | "encountered an array or dictionary with more than 5000 elements during xref recovery; " |
633 | 0 | "giving up on reading object"); |
634 | 0 | } |
635 | 0 | if (max_bad_count && --max_bad_count > 0 && good_count > 4) { |
636 | 0 | good_count = 0; |
637 | 0 | bad_count = 1; |
638 | 0 | return false; |
639 | 0 | } |
640 | 0 | if (++bad_count > 5 || |
641 | 0 | (frame->state != st_array && QIntC::to_size(max_bad_count) < frame->olist.size())) { |
642 | | // Give up after 5 errors in close proximity or if the number of missing dictionary keys |
643 | | // exceeds the remaining number of allowable total errors. |
644 | 0 | warn("too many errors; giving up on reading object"); |
645 | 0 | return true; |
646 | 0 | } |
647 | 0 | good_count = 0; |
648 | 0 | return false; |
649 | 0 | } |
650 | | |
651 | | void |
652 | | QPDFParser::warn(QPDFExc const& e) const |
653 | 202 | { |
654 | | // If parsing on behalf of a QPDF object and want to give a warning, we can warn through the |
655 | | // object. If parsing for some other reason, such as an explicit creation of an object from a |
656 | | // string, then just throw the exception. |
657 | 202 | if (context) { |
658 | 0 | context->warn(e); |
659 | 202 | } else { |
660 | 202 | throw e; |
661 | 202 | } |
662 | 202 | } |
663 | | |
664 | | void |
665 | | QPDFParser::warnDuplicateKey() |
666 | 0 | { |
667 | 0 | QTC::TC("qpdf", "QPDFParser duplicate dict key"); |
668 | 0 | warn( |
669 | 0 | frame->offset, |
670 | 0 | "dictionary has duplicated key " + frame->key + "; last occurrence overrides earlier ones"); |
671 | 0 | } |
672 | | |
673 | | void |
674 | | QPDFParser::warn(qpdf_offset_t offset, std::string const& msg) const |
675 | 202 | { |
676 | 202 | if (stream_id) { |
677 | 0 | std::string descr = "object "s + std::to_string(obj_id) + " 0"; |
678 | 0 | std::string name = context->getFilename() + " object stream " + std::to_string(stream_id); |
679 | 0 | warn(QPDFExc(qpdf_e_damaged_pdf, name, descr, offset, msg)); |
680 | 202 | } else { |
681 | 202 | warn(QPDFExc(qpdf_e_damaged_pdf, input.getName(), object_description, offset, msg)); |
682 | 202 | } |
683 | 202 | } |
684 | | |
685 | | void |
686 | | QPDFParser::warn(std::string const& msg) const |
687 | 202 | { |
688 | 202 | warn(input.getLastOffset(), msg); |
689 | 202 | } |