Coverage Report

Created: 2025-07-01 06:10

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