Coverage Report

Created: 2025-11-11 07:02

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