Coverage Report

Created: 2025-11-24 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/QPDF_objects.cc
Line
Count
Source
1
#include <qpdf/qpdf-config.h> // include first for large file support
2
3
#include <qpdf/QPDF_private.hh>
4
5
#include <qpdf/InputSource_private.hh>
6
#include <qpdf/OffsetInputSource.hh>
7
#include <qpdf/Pipeline.hh>
8
#include <qpdf/QPDFExc.hh>
9
#include <qpdf/QPDFLogger.hh>
10
#include <qpdf/QPDFObjectHandle_private.hh>
11
#include <qpdf/QPDFObject_private.hh>
12
#include <qpdf/QPDFParser.hh>
13
#include <qpdf/QTC.hh>
14
#include <qpdf/QUtil.hh>
15
#include <qpdf/Util.hh>
16
17
#include <array>
18
#include <atomic>
19
#include <cstring>
20
#include <limits>
21
#include <map>
22
#include <vector>
23
24
using namespace qpdf;
25
using namespace std::literals;
26
27
using Objects = QPDF::Doc::Objects;
28
29
namespace
30
{
31
    class InvalidInputSource: public InputSource
32
    {
33
      public:
34
        ~InvalidInputSource() override = default;
35
        qpdf_offset_t
36
        findAndSkipNextEOL() override
37
0
        {
38
0
            throwException();
39
0
            return 0;
40
0
        }
41
        std::string const&
42
        getName() const override
43
0
        {
44
0
            static std::string name("closed input source");
45
0
            return name;
46
0
        }
47
        qpdf_offset_t
48
        tell() override
49
0
        {
50
0
            throwException();
51
0
            return 0;
52
0
        }
53
        void
54
        seek(qpdf_offset_t offset, int whence) override
55
0
        {
56
0
            throwException();
57
0
        }
58
        void
59
        rewind() override
60
0
        {
61
0
            throwException();
62
0
        }
63
        size_t
64
        read(char* buffer, size_t length) override
65
0
        {
66
0
            throwException();
67
0
            return 0;
68
0
        }
69
        void
70
        unreadCh(char ch) override
71
0
        {
72
0
            throwException();
73
0
        }
74
75
      private:
76
        void
77
        throwException()
78
0
        {
79
0
            throw std::logic_error(
80
0
                "QPDF operation attempted on a QPDF object with no input "
81
0
                "source. QPDF operations are invalid before processFile (or "
82
0
                "another process method) or after closeInputSource");
83
0
        }
84
    };
85
} // namespace
86
87
class QPDF::ResolveRecorder final
88
{
89
  public:
90
    ResolveRecorder(QPDF& qpdf, QPDFObjGen const& og) :
91
487k
        qpdf(qpdf),
92
487k
        iter(qpdf.m->resolving.insert(og).first)
93
487k
    {
94
487k
    }
95
    ~ResolveRecorder()
96
487k
    {
97
487k
        qpdf.m->resolving.erase(iter);
98
487k
    }
99
100
  private:
101
    QPDF& qpdf;
102
    std::set<QPDFObjGen>::const_iterator iter;
103
};
104
105
class Objects::PatternFinder final: public InputSource::Finder
106
{
107
  public:
108
    PatternFinder(Objects& o, bool (Objects::*checker)()) :
109
65.7k
        o(o),
110
65.7k
        checker(checker)
111
65.7k
    {
112
65.7k
    }
113
    ~PatternFinder() final = default;
114
    bool
115
    check() final
116
46.7k
    {
117
46.7k
        return (this->o.*checker)();
118
46.7k
    }
119
120
  private:
121
    Objects& o;
122
    bool (Objects::*checker)();
123
};
124
125
bool
126
Objects::validatePDFVersion(char const*& p, std::string& version)
127
8.18k
{
128
8.18k
    if (!util::is_digit(*p)) {
129
2.35k
        return false;
130
2.35k
    }
131
14.5k
    while (util::is_digit(*p)) {
132
8.73k
        version.append(1, *p++);
133
8.73k
    }
134
5.83k
    if (!(*p == '.' && util::is_digit(*(p + 1)))) {
135
1.29k
        return false;
136
1.29k
    }
137
4.54k
    version.append(1, *p++);
138
12.2k
    while (util::is_digit(*p)) {
139
7.69k
        version.append(1, *p++);
140
7.69k
    }
141
4.54k
    return true;
142
5.83k
}
143
144
bool
145
Objects::findHeader()
146
8.19k
{
147
8.19k
    qpdf_offset_t global_offset = m->file->tell();
148
8.19k
    std::string line = m->file->readLine(1024);
149
8.19k
    char const* p = line.data();
150
8.19k
    util::assertion(strncmp(p, "%PDF-", 5) == 0, "findHeader is not looking at %PDF-");
151
8.19k
    p += 5;
152
8.19k
    std::string version;
153
    // Note: The string returned by line.data() is always null-terminated. The code below never
154
    // overruns the buffer because a null character always short-circuits further advancement.
155
8.19k
    if (!validatePDFVersion(p, version)) {
156
3.64k
        return false;
157
3.64k
    }
158
4.54k
    m->pdf_version = version;
159
4.54k
    if (global_offset != 0) {
160
        // Empirical evidence strongly suggests (codified in PDF 2.0 spec) that when there is
161
        // leading material prior to the PDF header, all explicit offsets in the file are such that
162
        // 0 points to the beginning of the header.
163
2.55k
        m->file = std::make_shared<OffsetInputSource>(m->file, global_offset);
164
2.55k
    }
165
4.54k
    return true;
166
8.19k
}
167
168
bool
169
Objects ::findStartxref()
170
7.92k
{
171
7.92k
    if (readToken(*m->file).isWord("startxref") && readToken(*m->file).isInteger()) {
172
        // Position in front of offset token
173
5.39k
        m->file->seek(m->file->getLastOffset(), SEEK_SET);
174
5.39k
        return true;
175
5.39k
    }
176
2.52k
    return false;
177
7.92k
}
178
179
void
180
Objects::parse(char const* password)
181
21.1k
{
182
21.1k
    if (password) {
183
0
        m->encp->provided_password = password;
184
0
    }
185
186
    // Find the header anywhere in the first 1024 bytes of the file.
187
21.1k
    PatternFinder hf(*this, &Objects::findHeader);
188
21.1k
    if (!m->file->findFirst("%PDF-", 0, 1024, hf)) {
189
16.6k
        warn(damagedPDF("", -1, "can't find PDF header"));
190
        // QPDFWriter writes files that usually require at least version 1.2 for /FlateDecode
191
16.6k
        m->pdf_version = "1.2";
192
16.6k
    }
193
194
    // PDF spec says %%EOF must be found within the last 1024 bytes of/ the file.  We add an extra
195
    // 30 characters to leave room for the startxref stuff.
196
21.1k
    m->file->seek(0, SEEK_END);
197
21.1k
    qpdf_offset_t end_offset = m->file->tell();
198
21.1k
    m->xref_table_max_offset = end_offset;
199
    // Sanity check on object ids. All objects must appear in xref table / stream. In all realistic
200
    // scenarios at least 3 bytes are required.
201
21.1k
    if (m->xref_table_max_id > m->xref_table_max_offset / 3) {
202
21.1k
        m->xref_table_max_id = static_cast<int>(m->xref_table_max_offset / 3);
203
21.1k
    }
204
21.1k
    qpdf_offset_t start_offset = (end_offset > 1054 ? end_offset - 1054 : 0);
205
21.1k
    PatternFinder sf(*this, &Objects::findStartxref);
206
21.1k
    qpdf_offset_t xref_offset = 0;
207
21.1k
    if (m->file->findLast("startxref", start_offset, 0, sf)) {
208
4.86k
        xref_offset = QUtil::string_to_ll(readToken(*m->file).getValue().c_str());
209
4.86k
    }
210
211
21.1k
    try {
212
21.1k
        if (xref_offset == 0) {
213
16.3k
            throw damagedPDF("", -1, "can't find startxref");
214
16.3k
        }
215
4.77k
        try {
216
4.77k
            read_xref(xref_offset);
217
4.77k
        } catch (QPDFExc&) {
218
3.83k
            throw;
219
3.83k
        } catch (std::exception& e) {
220
399
            throw damagedPDF("", -1, std::string("error reading xref: ") + e.what());
221
399
        }
222
20.6k
    } catch (QPDFExc& e) {
223
20.6k
        if (!cf.surpress_recovery()) {
224
20.6k
            reconstruct_xref(e, xref_offset > 0);
225
20.6k
        } else {
226
0
            throw;
227
0
        }
228
20.6k
    }
229
230
20.6k
    m->encp->initialize(qpdf);
231
9.66k
    m->parsed = true;
232
9.66k
    if (!m->xref_table.empty() && !qpdf.getRoot().getKey("/Pages").isDictionary()) {
233
        // QPDFs created from JSON have an empty xref table and no root object yet.
234
7
        throw damagedPDF("", -1, "unable to find page tree");
235
7
    }
236
9.66k
}
237
238
void
239
Objects::inParse(bool v)
240
364k
{
241
364k
    util::internal_error_if(
242
364k
        m->in_parse == v, "QPDF: re-entrant parsing detected"
243
        // This happens if QPDFParser::parse tries to resolve an indirect object while it is
244
        // parsing.
245
364k
    );
246
364k
    m->in_parse = v;
247
364k
}
248
249
void
250
Objects::setTrailer(QPDFObjectHandle obj)
251
6.29k
{
252
6.29k
    if (m->trailer) {
253
296
        return;
254
296
    }
255
6.00k
    m->trailer = obj;
256
6.00k
}
257
258
void
259
Objects::reconstruct_xref(QPDFExc& e, bool found_startxref)
260
20.8k
{
261
20.8k
    if (m->reconstructed_xref) {
262
        // Avoid xref reconstruction infinite loops. This is getting very hard to reproduce because
263
        // qpdf is throwing many fewer exceptions while parsing. Most situations are warnings now.
264
145
        throw e;
265
145
    }
266
267
    // If recovery generates more than 1000 warnings, the file is so severely damaged that there
268
    // probably is no point trying to continue.
269
20.7k
    const auto max_warnings = m->warnings.size() + 1000U;
270
2.06M
    auto check_warnings = [this, max_warnings]() {
271
2.06M
        if (m->warnings.size() > max_warnings) {
272
0
            throw damagedPDF("", -1, "too many errors while reconstructing cross-reference table");
273
0
        }
274
2.06M
    };
275
276
20.7k
    m->reconstructed_xref = true;
277
    // We may find more objects, which may contain dangling references.
278
20.7k
    m->fixed_dangling_refs = false;
279
280
20.7k
    warn(damagedPDF("", -1, "file is damaged"));
281
20.7k
    warn(e);
282
20.7k
    warn(damagedPDF("", -1, "Attempting to reconstruct cross-reference table"));
283
284
    // Delete all references to type 1 (uncompressed) objects
285
20.7k
    std::vector<QPDFObjGen> to_delete;
286
83.4k
    for (auto const& iter: m->xref_table) {
287
83.4k
        if (iter.second.getType() == 1) {
288
59.6k
            to_delete.emplace_back(iter.first);
289
59.6k
        }
290
83.4k
    }
291
59.6k
    for (auto const& iter: to_delete) {
292
59.6k
        m->xref_table.erase(iter);
293
59.6k
    }
294
295
20.7k
    std::vector<std::tuple<int, int, qpdf_offset_t>> found_objects;
296
20.7k
    std::vector<qpdf_offset_t> trailers;
297
20.7k
    std::vector<qpdf_offset_t> startxrefs;
298
299
20.7k
    m->file->seek(0, SEEK_END);
300
20.7k
    qpdf_offset_t eof = m->file->tell();
301
20.7k
    m->file->seek(0, SEEK_SET);
302
    // Don't allow very long tokens here during recovery. All the interesting tokens are covered.
303
20.7k
    static size_t const MAX_LEN = 10;
304
1.91M
    while (m->file->tell() < eof) {
305
1.89M
        QPDFTokenizer::Token t1 = m->objects.readToken(*m->file, MAX_LEN);
306
1.89M
        qpdf_offset_t token_start = m->file->tell() - toO(t1.getValue().length());
307
1.89M
        if (t1.isInteger()) {
308
366k
            auto pos = m->file->tell();
309
366k
            auto t2 = m->objects.readToken(*m->file, MAX_LEN);
310
366k
            if (t2.isInteger() && m->objects.readToken(*m->file, MAX_LEN).isWord("obj")) {
311
134k
                int obj = QUtil::string_to_int(t1.getValue().c_str());
312
134k
                int gen = QUtil::string_to_int(t2.getValue().c_str());
313
134k
                if (obj <= m->xref_table_max_id) {
314
134k
                    found_objects.emplace_back(obj, gen, token_start);
315
134k
                } else {
316
908
                    warn(damagedPDF(
317
908
                        "", -1, "ignoring object with impossibly large id " + std::to_string(obj)));
318
908
                }
319
134k
            }
320
366k
            m->file->seek(pos, SEEK_SET);
321
1.52M
        } else if (!m->trailer && t1.isWord("trailer")) {
322
36.9k
            trailers.emplace_back(m->file->tell());
323
1.48M
        } else if (!found_startxref && t1.isWord("startxref")) {
324
1.69k
            startxrefs.emplace_back(m->file->tell());
325
1.69k
        }
326
1.89M
        check_warnings();
327
1.89M
        m->file->findAndSkipNextEOL();
328
1.89M
    }
329
330
20.7k
    if (!found_startxref && !startxrefs.empty() && !found_objects.empty() &&
331
863
        startxrefs.back() > std::get<2>(found_objects.back())) {
332
388
        auto xref_backup{m->xref_table};
333
388
        try {
334
388
            m->file->seek(startxrefs.back(), SEEK_SET);
335
388
            if (auto offset = QUtil::string_to_ll(readToken(*m->file).getValue().data())) {
336
216
                read_xref(offset);
337
338
216
                if (qpdf.getRoot().getKey("/Pages").isDictionary()) {
339
4
                    warn(damagedPDF(
340
4
                        "", -1, "startxref was more than 1024 bytes before end of file"));
341
4
                    m->encp->initialize(qpdf);
342
4
                    m->parsed = true;
343
4
                    m->reconstructed_xref = false;
344
4
                    return;
345
4
                }
346
216
            }
347
388
        } catch (...) {
348
            // ok, bad luck. Do recovery.
349
212
        }
350
384
        m->xref_table = std::move(xref_backup);
351
384
    }
352
353
20.7k
    auto rend = found_objects.rend();
354
154k
    for (auto it = found_objects.rbegin(); it != rend; it++) {
355
133k
        auto [obj, gen, token_start] = *it;
356
133k
        insertXrefEntry(obj, 1, token_start, gen);
357
133k
        check_warnings();
358
133k
    }
359
20.7k
    m->deleted_objects.clear();
360
361
    // Search at most the last 100 trailer candidates. If none of them are valid, odds are this file
362
    // is deliberately broken.
363
20.7k
    int end_index = trailers.size() > 100 ? static_cast<int>(trailers.size()) - 100 : 0;
364
34.3k
    for (auto it = trailers.rbegin(); it != std::prev(trailers.rend(), end_index); it++) {
365
15.6k
        m->file->seek(*it, SEEK_SET);
366
15.6k
        auto t = readTrailer();
367
15.6k
        if (!t.isDictionary()) {
368
            // Oh well.  It was worth a try.
369
13.0k
        } else {
370
2.62k
            if (t.hasKey("/Root")) {
371
2.02k
                m->trailer = t;
372
2.02k
                break;
373
2.02k
            }
374
593
            warn(damagedPDF("trailer", *it, "recovered trailer has no /Root entry"));
375
593
        }
376
13.6k
        check_warnings();
377
13.6k
    }
378
379
20.7k
    if (!m->trailer) {
380
18.1k
        qpdf_offset_t max_offset{0};
381
18.1k
        size_t max_size{0};
382
        // If there are any xref streams, take the last one to appear.
383
77.4k
        for (auto const& iter: m->xref_table) {
384
77.4k
            auto entry = iter.second;
385
77.4k
            if (entry.getType() != 1) {
386
650
                continue;
387
650
            }
388
76.8k
            auto oh = qpdf.getObject(iter.first);
389
76.8k
            try {
390
76.8k
                if (!oh.isStreamOfType("/XRef")) {
391
69.1k
                    continue;
392
69.1k
                }
393
76.8k
            } catch (std::exception&) {
394
2.35k
                continue;
395
2.35k
            }
396
5.38k
            auto offset = entry.getOffset();
397
5.38k
            auto size = oh.getDict().getKey("/Size").getUIntValueAsUInt();
398
5.38k
            if (size > max_size || (size == max_size && offset > max_offset)) {
399
5.33k
                max_offset = offset;
400
5.33k
                setTrailer(oh.getDict());
401
5.33k
            }
402
5.38k
            check_warnings();
403
5.38k
        }
404
18.1k
        if (max_offset > 0) {
405
5.02k
            try {
406
5.02k
                read_xref(max_offset, true);
407
5.02k
            } catch (std::exception&) {
408
2.91k
                warn(damagedPDF(
409
2.91k
                    "", -1, "error decoding candidate xref stream while recovering damaged file"));
410
2.91k
            }
411
5.02k
            QTC::TC("qpdf", "QPDF recover xref stream");
412
4.99k
        }
413
18.1k
    }
414
415
20.7k
    if (!m->trailer || (!m->parsed && !m->trailer.getKey("/Root").isDictionary())) {
416
        // Try to find a Root dictionary. As a quick fix try the one with the highest object id.
417
18.3k
        QPDFObjectHandle root;
418
248k
        for (auto const& iter: m->obj_cache) {
419
248k
            try {
420
248k
                if (QPDFObjectHandle(iter.second.object).isDictionaryOfType("/Catalog")) {
421
8.13k
                    root = iter.second.object;
422
8.13k
                }
423
248k
            } catch (std::exception&) {
424
2.94k
                continue;
425
2.94k
            }
426
248k
        }
427
18.3k
        if (root) {
428
7.79k
            if (!m->trailer) {
429
6.25k
                warn(damagedPDF(
430
6.25k
                    "", -1, "unable to find trailer dictionary while recovering damaged file"));
431
6.25k
                m->trailer = QPDFObjectHandle::newDictionary();
432
6.25k
            }
433
7.79k
            m->trailer.replaceKey("/Root", root);
434
7.79k
        }
435
18.3k
    }
436
437
20.7k
    if (!m->trailer) {
438
        // We could check the last encountered object to see if it was an xref stream.  If so, we
439
        // could try to get the trailer from there.  This may make it possible to recover files with
440
        // bad startxref pointers even when they have object streams.
441
442
6.83k
        throw damagedPDF("", -1, "unable to find trailer dictionary while recovering damaged file");
443
6.83k
    }
444
13.8k
    if (m->xref_table.empty()) {
445
        // We cannot check for an empty xref table in parse because empty tables are valid when
446
        // creating QPDF objects from JSON.
447
255
        throw damagedPDF("", -1, "unable to find objects while recovering damaged file");
448
255
    }
449
13.6k
    check_warnings();
450
13.6k
    if (!m->parsed) {
451
13.4k
        m->parsed = !m->pages.empty();
452
13.4k
        if (!m->parsed) {
453
515
            throw damagedPDF("", -1, "unable to find any pages while recovering damaged file");
454
515
        }
455
12.9k
        check_warnings();
456
12.9k
    }
457
458
    // We could iterate through the objects looking for streams and try to find objects inside of
459
    // them, but it's probably not worth the trouble.  Acrobat can't recover files with any errors
460
    // in an xref stream, and this would be a real long shot anyway.  If we wanted to do anything
461
    // that involved looking at stream contents, we'd also have to call initializeEncryption() here.
462
    // It's safe to call it more than once.
463
13.6k
}
464
465
void
466
Objects::read_xref(qpdf_offset_t xref_offset, bool in_stream_recovery)
467
10.0k
{
468
10.0k
    std::map<int, int> free_table;
469
10.0k
    std::set<qpdf_offset_t> visited;
470
20.6k
    while (xref_offset) {
471
10.8k
        visited.insert(xref_offset);
472
10.8k
        char buf[7];
473
10.8k
        memset(buf, 0, sizeof(buf));
474
10.8k
        m->file->seek(xref_offset, SEEK_SET);
475
        // Some files miss the mark a little with startxref. We could do a better job of searching
476
        // in the neighborhood for something that looks like either an xref table or stream, but the
477
        // simple heuristic of skipping whitespace can help with the xref table case and is harmless
478
        // with the stream case.
479
10.8k
        bool done = false;
480
10.8k
        bool skipped_space = false;
481
26.3k
        while (!done) {
482
15.5k
            char ch;
483
15.5k
            if (1 == m->file->read(&ch, 1)) {
484
14.5k
                if (util::is_space(ch)) {
485
5.05k
                    skipped_space = true;
486
9.49k
                } else {
487
9.49k
                    m->file->unreadCh(ch);
488
9.49k
                    done = true;
489
9.49k
                }
490
14.5k
            } else {
491
1.01k
                QTC::TC("qpdf", "QPDF eof skipping spaces before xref", skipped_space ? 0 : 1);
492
1.01k
                done = true;
493
1.01k
            }
494
15.5k
        }
495
496
10.8k
        m->file->read(buf, sizeof(buf) - 1);
497
        // The PDF spec says xref must be followed by a line terminator, but files exist in the wild
498
        // where it is terminated by arbitrary whitespace.
499
10.8k
        if ((strncmp(buf, "xref", 4) == 0) && util::is_space(buf[4])) {
500
1.39k
            if (skipped_space) {
501
89
                warn(damagedPDF("", -1, "extraneous whitespace seen before xref"));
502
89
            }
503
1.39k
            QTC::TC(
504
1.39k
                "qpdf",
505
1.39k
                "QPDF xref space",
506
1.39k
                ((buf[4] == '\n')       ? 0
507
1.39k
                     : (buf[4] == '\r') ? 1
508
952
                     : (buf[4] == ' ')  ? 2
509
298
                                        : 9999));
510
1.39k
            int skip = 4;
511
            // buf is null-terminated, and util::is_space('\0') is false, so this won't overrun.
512
2.96k
            while (util::is_space(buf[skip])) {
513
1.56k
                ++skip;
514
1.56k
            }
515
1.39k
            xref_offset = read_xrefTable(xref_offset + skip);
516
9.41k
        } else {
517
9.41k
            xref_offset = read_xrefStream(xref_offset, in_stream_recovery);
518
9.41k
        }
519
10.8k
        if (visited.contains(xref_offset)) {
520
168
            throw damagedPDF("", -1, "loop detected following xref tables");
521
168
        }
522
10.8k
    }
523
524
9.83k
    if (!m->trailer) {
525
0
        throw damagedPDF("", -1, "unable to find trailer while reading xref");
526
0
    }
527
9.83k
    int size = m->trailer.getKey("/Size").getIntValueAsInt();
528
9.83k
    int max_obj = 0;
529
9.83k
    if (!m->xref_table.empty()) {
530
2.39k
        max_obj = m->xref_table.rbegin()->first.getObj();
531
2.39k
    }
532
9.83k
    if (!m->deleted_objects.empty()) {
533
1.23k
        max_obj = std::max(max_obj, *(m->deleted_objects.rbegin()));
534
1.23k
    }
535
9.83k
    if (size < 1 || (size - 1) != max_obj) {
536
1.87k
        if (size == (max_obj + 2) && qpdf.getObject(max_obj + 1, 0).isStreamOfType("/XRef")) {
537
0
            warn(damagedPDF(
538
0
                "",
539
0
                -1,
540
0
                "xref entry for the xref stream itself is missing - a common error handled "
541
0
                "correctly by qpdf and most other applications"));
542
1.87k
        } else {
543
1.87k
            warn(damagedPDF(
544
1.87k
                "",
545
1.87k
                -1,
546
1.87k
                ("reported number of objects (" + std::to_string(size) +
547
1.87k
                 ") is not one plus the highest object number (" + std::to_string(max_obj) + ")")));
548
1.87k
        }
549
1.87k
    }
550
551
    // We no longer need the deleted_objects table, so go ahead and clear it out to make sure we
552
    // never depend on its being set.
553
9.83k
    m->deleted_objects.clear();
554
555
    // Make sure we keep only the highest generation for any object.
556
9.83k
    QPDFObjGen last_og{-1, 0};
557
359k
    for (auto const& item: m->xref_table) {
558
359k
        auto id = item.first.getObj();
559
359k
        if (id == last_og.getObj() && id > 0) {
560
724
            qpdf.removeObject(last_og);
561
724
        }
562
359k
        last_og = item.first;
563
359k
    }
564
9.83k
}
565
566
bool
567
Objects::parse_xrefFirst(std::string const& line, int& obj, int& num, int& bytes)
568
7.14k
{
569
    // is_space and is_digit both return false on '\0', so this will not overrun the null-terminated
570
    // buffer.
571
7.14k
    char const* p = line.c_str();
572
7.14k
    char const* start = line.c_str();
573
574
    // Skip zero or more spaces
575
8.89k
    while (util::is_space(*p)) {
576
1.75k
        ++p;
577
1.75k
    }
578
    // Require digit
579
7.14k
    if (!util::is_digit(*p)) {
580
175
        return false;
581
175
    }
582
    // Gather digits
583
6.96k
    std::string obj_str;
584
22.0k
    while (util::is_digit(*p)) {
585
15.0k
        obj_str.append(1, *p++);
586
15.0k
    }
587
    // Require space
588
6.96k
    if (!util::is_space(*p)) {
589
99
        return false;
590
99
    }
591
    // Skip spaces
592
22.5k
    while (util::is_space(*p)) {
593
15.7k
        ++p;
594
15.7k
    }
595
    // Require digit
596
6.86k
    if (!util::is_digit(*p)) {
597
88
        return false;
598
88
    }
599
    // Gather digits
600
6.78k
    std::string num_str;
601
20.1k
    while (util::is_digit(*p)) {
602
13.3k
        num_str.append(1, *p++);
603
13.3k
    }
604
    // Skip any space including line terminators
605
19.3k
    while (util::is_space(*p)) {
606
12.5k
        ++p;
607
12.5k
    }
608
6.78k
    bytes = toI(p - start);
609
6.78k
    obj = QUtil::string_to_int(obj_str.c_str());
610
6.78k
    num = QUtil::string_to_int(num_str.c_str());
611
6.78k
    return true;
612
6.86k
}
613
614
bool
615
Objects::read_bad_xrefEntry(qpdf_offset_t& f1, int& f2, char& type)
616
6.81k
{
617
    // Reposition after initial read attempt and reread.
618
6.81k
    m->file->seek(m->file->getLastOffset(), SEEK_SET);
619
6.81k
    auto line = m->file->readLine(30);
620
621
    // is_space and is_digit both return false on '\0', so this will not overrun the null-terminated
622
    // buffer.
623
6.81k
    char const* p = line.data();
624
625
    // Skip zero or more spaces. There aren't supposed to be any.
626
6.81k
    bool invalid = false;
627
16.2k
    while (util::is_space(*p)) {
628
9.44k
        ++p;
629
9.44k
        invalid = true;
630
9.44k
    }
631
    // Require digit
632
6.81k
    if (!util::is_digit(*p)) {
633
15
        return false;
634
15
    }
635
    // Gather digits
636
6.79k
    std::string f1_str;
637
29.3k
    while (util::is_digit(*p)) {
638
22.5k
        f1_str.append(1, *p++);
639
22.5k
    }
640
    // Require space
641
6.79k
    if (!util::is_space(*p)) {
642
15
        return false;
643
15
    }
644
6.78k
    if (util::is_space(*(p + 1))) {
645
1.95k
        invalid = true;
646
1.95k
    }
647
    // Skip spaces
648
23.1k
    while (util::is_space(*p)) {
649
16.3k
        ++p;
650
16.3k
    }
651
    // Require digit
652
6.78k
    if (!util::is_digit(*p)) {
653
31
        return false;
654
31
    }
655
    // Gather digits
656
6.75k
    std::string f2_str;
657
28.9k
    while (util::is_digit(*p)) {
658
22.1k
        f2_str.append(1, *p++);
659
22.1k
    }
660
    // Require space
661
6.75k
    if (!util::is_space(*p)) {
662
37
        return false;
663
37
    }
664
6.71k
    if (util::is_space(*(p + 1))) {
665
1.41k
        invalid = true;
666
1.41k
    }
667
    // Skip spaces
668
16.2k
    while (util::is_space(*p)) {
669
9.54k
        ++p;
670
9.54k
    }
671
6.71k
    if ((*p == 'f') || (*p == 'n')) {
672
6.60k
        type = *p;
673
6.60k
    } else {
674
110
        return false;
675
110
    }
676
6.60k
    if ((f1_str.length() != 10) || (f2_str.length() != 5)) {
677
6.24k
        invalid = true;
678
6.24k
    }
679
680
6.60k
    if (invalid) {
681
6.25k
        warn(damagedPDF("xref table", "accepting invalid xref table entry"));
682
6.25k
    }
683
684
6.60k
    f1 = QUtil::string_to_ll(f1_str.c_str());
685
6.60k
    f2 = QUtil::string_to_int(f2_str.c_str());
686
687
6.60k
    return true;
688
6.71k
}
689
690
// Optimistically read and parse xref entry. If entry is bad, call read_bad_xrefEntry and return
691
// result.
692
bool
693
Objects::read_xrefEntry(qpdf_offset_t& f1, int& f2, char& type)
694
14.7k
{
695
14.7k
    std::array<char, 21> line;
696
14.7k
    if (m->file->read(line.data(), 20) != 20) {
697
        // C++20: [[unlikely]]
698
177
        return false;
699
177
    }
700
14.5k
    line[20] = '\0';
701
14.5k
    char const* p = line.data();
702
703
14.5k
    int f1_len = 0;
704
14.5k
    int f2_len = 0;
705
706
    // is_space and is_digit both return false on '\0', so this will not overrun the null-terminated
707
    // buffer.
708
709
    // Gather f1 digits. NB No risk of overflow as 9'999'999'999 < max long long.
710
70.4k
    while (*p == '0') {
711
55.8k
        ++f1_len;
712
55.8k
        ++p;
713
55.8k
    }
714
52.6k
    while (util::is_digit(*p) && f1_len++ < 10) {
715
38.0k
        f1 *= 10;
716
38.0k
        f1 += *p++ - '0';
717
38.0k
    }
718
    // Require space
719
14.5k
    if (!util::is_space(*p++)) {
720
        // Entry doesn't start with space or digit.
721
        // C++20: [[unlikely]]
722
56
        return false;
723
56
    }
724
    // Gather digits. NB No risk of overflow as 99'999 < max int.
725
43.7k
    while (*p == '0') {
726
29.2k
        ++f2_len;
727
29.2k
        ++p;
728
29.2k
    }
729
37.4k
    while (util::is_digit(*p) && f2_len++ < 5) {
730
22.9k
        f2 *= 10;
731
22.9k
        f2 += static_cast<int>(*p++ - '0');
732
22.9k
    }
733
14.5k
    if (util::is_space(*p++) && (*p == 'f' || *p == 'n')) {
734
        // C++20: [[likely]]
735
10.8k
        type = *p;
736
        // No test for valid line[19].
737
10.8k
        if (*(++p) && *(++p) && (*p == '\n' || *p == '\r') && f1_len == 10 && f2_len == 5) {
738
            // C++20: [[likely]]
739
7.72k
            return true;
740
7.72k
        }
741
10.8k
    }
742
6.81k
    return read_bad_xrefEntry(f1, f2, type);
743
14.5k
}
744
745
// Read a single cross-reference table section and associated trailer.
746
qpdf_offset_t
747
Objects::read_xrefTable(qpdf_offset_t xref_offset)
748
1.39k
{
749
1.39k
    m->file->seek(xref_offset, SEEK_SET);
750
1.39k
    std::string line;
751
7.17k
    while (true) {
752
7.14k
        line.assign(50, '\0');
753
7.14k
        m->file->read(line.data(), line.size());
754
7.14k
        int obj = 0;
755
7.14k
        int num = 0;
756
7.14k
        int bytes = 0;
757
7.14k
        if (!parse_xrefFirst(line, obj, num, bytes)) {
758
362
            throw damagedPDF("xref table", "xref syntax invalid");
759
362
        }
760
6.78k
        m->file->seek(m->file->getLastOffset() + bytes, SEEK_SET);
761
21.1k
        for (qpdf_offset_t i = obj; i - num < obj; ++i) {
762
14.7k
            if (i == 0) {
763
                // This is needed by checkLinearization()
764
506
                first_xref_item_offset_ = m->file->tell();
765
506
            }
766
            // For xref_table, these will always be small enough to be ints
767
14.7k
            qpdf_offset_t f1 = 0;
768
14.7k
            int f2 = 0;
769
14.7k
            char type = '\0';
770
14.7k
            if (!read_xrefEntry(f1, f2, type)) {
771
441
                throw damagedPDF(
772
441
                    "xref table", "invalid xref entry (obj=" + std::to_string(i) + ")");
773
441
            }
774
14.3k
            if (type == 'f') {
775
5.34k
                insertFreeXrefEntry(QPDFObjGen(toI(i), f2));
776
8.98k
            } else {
777
8.98k
                insertXrefEntry(toI(i), 1, f1, f2);
778
8.98k
            }
779
14.3k
        }
780
6.34k
        qpdf_offset_t pos = m->file->tell();
781
6.34k
        if (readToken(*m->file).isWord("trailer")) {
782
563
            break;
783
5.77k
        } else {
784
5.77k
            m->file->seek(pos, SEEK_SET);
785
5.77k
        }
786
6.34k
    }
787
788
    // Set offset to previous xref table if any
789
592
    QPDFObjectHandle cur_trailer = m->objects.readTrailer();
790
592
    if (!cur_trailer.isDictionary()) {
791
55
        throw damagedPDF("", "expected trailer dictionary");
792
55
    }
793
794
537
    if (!m->trailer) {
795
457
        setTrailer(cur_trailer);
796
797
457
        if (!m->trailer.hasKey("/Size")) {
798
141
            throw damagedPDF("trailer", "trailer dictionary lacks /Size key");
799
141
        }
800
316
        if (!m->trailer.getKey("/Size").isInteger()) {
801
1
            throw damagedPDF("trailer", "/Size key in trailer dictionary is not an integer");
802
1
        }
803
316
    }
804
805
395
    if (cur_trailer.hasKey("/XRefStm")) {
806
27
        if (cf.ignore_xref_streams()) {
807
0
            QTC::TC("qpdf", "QPDF ignoring XRefStm in trailer");
808
27
        } else {
809
27
            if (cur_trailer.getKey("/XRefStm").isInteger()) {
810
                // Read the xref stream but disregard any return value -- we'll use our trailer's
811
                // /Prev key instead of the xref stream's.
812
26
                (void)read_xrefStream(cur_trailer.getKey("/XRefStm").getIntValue());
813
26
            } else {
814
1
                throw damagedPDF("xref stream", xref_offset, "invalid /XRefStm");
815
1
            }
816
27
        }
817
27
    }
818
819
394
    if (cur_trailer.hasKey("/Prev")) {
820
135
        if (!cur_trailer.getKey("/Prev").isInteger()) {
821
1
            throw damagedPDF("trailer", "/Prev key in trailer dictionary is not an integer");
822
1
        }
823
134
        return cur_trailer.getKey("/Prev").getIntValue();
824
135
    }
825
826
259
    return 0;
827
394
}
828
829
// Read a single cross-reference stream.
830
qpdf_offset_t
831
Objects::read_xrefStream(qpdf_offset_t xref_offset, bool in_stream_recovery)
832
9.13k
{
833
9.13k
    if (!cf.ignore_xref_streams()) {
834
9.13k
        QPDFObjectHandle xref_obj;
835
9.13k
        try {
836
9.13k
            m->in_read_xref_stream = true;
837
9.13k
            xref_obj = readObjectAtOffset(xref_offset, "xref stream", true);
838
9.13k
        } catch (QPDFExc&) {
839
            // ignore -- report error below
840
1.92k
        }
841
9.13k
        m->in_read_xref_stream = false;
842
9.08k
        if (xref_obj.isStreamOfType("/XRef")) {
843
6.50k
            return processXRefStream(xref_offset, xref_obj, in_stream_recovery);
844
6.50k
        }
845
9.08k
    }
846
847
2.58k
    throw damagedPDF("", xref_offset, "xref not found");
848
0
    return 0; // unreachable
849
9.13k
}
850
851
// Return the entry size of the xref stream and the processed W array.
852
std::pair<int, std::array<int, 3>>
853
Objects::processXRefW(QPDFObjectHandle& dict, std::function<QPDFExc(std::string_view)> damaged)
854
6.50k
{
855
6.50k
    auto W_obj = dict.getKey("/W");
856
6.50k
    if (!(W_obj.size() >= 3 && W_obj.getArrayItem(0).isInteger() &&
857
6.25k
          W_obj.getArrayItem(1).isInteger() && W_obj.getArrayItem(2).isInteger())) {
858
300
        throw damaged("Cross-reference stream does not have a proper /W key");
859
300
    }
860
861
6.20k
    std::array<int, 3> W;
862
6.20k
    int entry_size = 0;
863
6.20k
    auto w_vector = W_obj.getArrayAsVector();
864
6.20k
    int max_bytes = sizeof(qpdf_offset_t);
865
24.6k
    for (size_t i = 0; i < 3; ++i) {
866
18.5k
        W[i] = w_vector[i].getIntValueAsInt();
867
18.5k
        if (W[i] > max_bytes) {
868
17
            throw damaged("Cross-reference stream's /W contains impossibly large values");
869
17
        }
870
18.5k
        if (W[i] < 0) {
871
36
            throw damaged("Cross-reference stream's /W contains negative values");
872
36
        }
873
18.4k
        entry_size += W[i];
874
18.4k
    }
875
6.14k
    if (entry_size == 0) {
876
4
        throw damaged("Cross-reference stream's /W indicates entry size of 0");
877
4
    }
878
6.14k
    return {entry_size, W};
879
6.14k
}
880
881
// Validate Size key and return the maximum number of entries that the xref stream can contain.
882
int
883
Objects::processXRefSize(
884
    QPDFObjectHandle& dict, int entry_size, std::function<QPDFExc(std::string_view)> damaged)
885
6.14k
{
886
    // Number of entries is limited by the highest possible object id and stream size.
887
6.14k
    auto max_num_entries = std::numeric_limits<int>::max();
888
6.14k
    if (max_num_entries > (std::numeric_limits<qpdf_offset_t>::max() / entry_size)) {
889
0
        max_num_entries = toI(std::numeric_limits<qpdf_offset_t>::max() / entry_size);
890
0
    }
891
892
6.14k
    auto Size_obj = dict.getKey("/Size");
893
6.14k
    long long size;
894
6.14k
    if (!dict.getKey("/Size").getValueAsInt(size)) {
895
92
        throw damaged("Cross-reference stream does not have a proper /Size key");
896
6.05k
    } else if (size < 0) {
897
66
        throw damaged("Cross-reference stream has a negative /Size key");
898
5.98k
    } else if (size >= max_num_entries) {
899
80
        throw damaged("Cross-reference stream has an impossibly large /Size key");
900
80
    }
901
    // We are not validating that Size <= (Size key of parent xref / trailer).
902
5.90k
    return max_num_entries;
903
6.14k
}
904
905
// Return the number of entries of the xref stream and the processed Index array.
906
std::pair<int, std::vector<std::pair<int, int>>>
907
Objects::processXRefIndex(
908
    QPDFObjectHandle& dict, int max_num_entries, std::function<QPDFExc(std::string_view)> damaged)
909
5.90k
{
910
5.90k
    auto size = dict.getKey("/Size").getIntValueAsInt();
911
5.90k
    auto Index_obj = dict.getKey("/Index");
912
913
5.90k
    if (Index_obj.isArray()) {
914
1.12k
        std::vector<std::pair<int, int>> indx;
915
1.12k
        int num_entries = 0;
916
1.12k
        auto index_vec = Index_obj.getArrayAsVector();
917
1.12k
        if ((index_vec.size() % 2) || index_vec.size() < 2) {
918
24
            throw damaged("Cross-reference stream's /Index has an invalid number of values");
919
24
        }
920
921
1.10k
        int i = 0;
922
1.10k
        long long first = 0;
923
7.24k
        for (auto& val: index_vec) {
924
7.24k
            if (val.isInteger()) {
925
7.22k
                if (i % 2) {
926
3.57k
                    auto count = val.getIntValue();
927
3.57k
                    if (count <= 0) {
928
57
                        throw damaged(
929
57
                            "Cross-reference stream section claims to contain " +
930
57
                            std::to_string(count) + " entries");
931
57
                    }
932
                    // We are guarding against the possibility of num_entries * entry_size
933
                    // overflowing. We are not checking that entries are in ascending order as
934
                    // required by the spec, which probably should generate a warning. We are also
935
                    // not checking that for each subsection first object number + number of entries
936
                    // <= /Size. The spec requires us to ignore object number > /Size.
937
3.51k
                    if (first > (max_num_entries - count) ||
938
3.46k
                        count > (max_num_entries - num_entries)) {
939
60
                        throw damaged(
940
60
                            "Cross-reference stream claims to contain too many entries: " +
941
60
                            std::to_string(first) + " " + std::to_string(max_num_entries) + " " +
942
60
                            std::to_string(num_entries));
943
60
                    }
944
3.45k
                    indx.emplace_back(static_cast<int>(first), static_cast<int>(count));
945
3.45k
                    num_entries += static_cast<int>(count);
946
3.64k
                } else {
947
3.64k
                    first = val.getIntValue();
948
3.64k
                    if (first < 0) {
949
19
                        throw damaged(
950
19
                            "Cross-reference stream's /Index contains a negative object id");
951
3.63k
                    } else if (first > max_num_entries) {
952
51
                        throw damaged(
953
51
                            "Cross-reference stream's /Index contains an impossibly "
954
51
                            "large object id");
955
51
                    }
956
3.64k
                }
957
7.22k
            } else {
958
26
                throw damaged(
959
26
                    "Cross-reference stream's /Index's item " + std::to_string(i) +
960
26
                    " is not an integer");
961
26
            }
962
7.03k
            i++;
963
7.03k
        }
964
891
        QTC::TC("qpdf", "QPDF xref /Index is array", index_vec.size() == 2 ? 0 : 1);
965
891
        return {num_entries, indx};
966
4.77k
    } else if (Index_obj.null()) {
967
4.76k
        return {size, {{0, size}}};
968
4.76k
    } else {
969
9
        throw damaged("Cross-reference stream does not have a proper /Index key");
970
9
    }
971
5.90k
}
972
973
qpdf_offset_t
974
Objects::processXRefStream(
975
    qpdf_offset_t xref_offset, QPDFObjectHandle& xref_obj, bool in_stream_recovery)
976
6.50k
{
977
6.50k
    auto damaged = [this, xref_offset](std::string_view msg) -> QPDFExc {
978
4.20k
        return damagedPDF("xref stream", xref_offset, msg.data());
979
4.20k
    };
980
981
6.50k
    auto dict = xref_obj.getDict();
982
983
6.50k
    auto [entry_size, W] = processXRefW(dict, damaged);
984
6.50k
    int max_num_entries = processXRefSize(dict, entry_size, damaged);
985
6.50k
    auto [num_entries, indx] = processXRefIndex(dict, max_num_entries, damaged);
986
987
6.50k
    std::shared_ptr<Buffer> bp = xref_obj.getStreamData(qpdf_dl_specialized);
988
6.50k
    size_t actual_size = bp->getSize();
989
6.50k
    auto expected_size = toS(entry_size) * toS(num_entries);
990
991
6.50k
    if (expected_size != actual_size) {
992
3.36k
        QPDFExc x = damaged(
993
3.36k
            "Cross-reference stream data has the wrong size; expected = " +
994
3.36k
            std::to_string(expected_size) + "; actual = " + std::to_string(actual_size));
995
3.36k
        if (expected_size > actual_size) {
996
566
            throw x;
997
2.80k
        } else {
998
2.80k
            warn(x);
999
2.80k
        }
1000
3.36k
    }
1001
1002
5.93k
    bool saw_first_compressed_object = false;
1003
1004
    // Actual size vs. expected size check above ensures that we will not overflow any buffers here.
1005
    // We know that entry_size * num_entries is less or equal to the size of the buffer.
1006
5.93k
    auto p = bp->getBuffer();
1007
5.93k
    for (auto [obj, sec_entries]: indx) {
1008
        // Process a subsection.
1009
912k
        for (int i = 0; i < sec_entries; ++i) {
1010
            // Read this entry
1011
908k
            std::array<qpdf_offset_t, 3> fields{};
1012
908k
            if (W[0] == 0) {
1013
88.1k
                fields[0] = 1;
1014
88.1k
            }
1015
3.63M
            for (size_t j = 0; j < 3; ++j) {
1016
6.55M
                for (int k = 0; k < W[j]; ++k) {
1017
3.83M
                    fields[j] <<= 8;
1018
3.83M
                    fields[j] |= *p++;
1019
3.83M
                }
1020
2.72M
            }
1021
1022
            // Get the generation number.  The generation number is 0 unless this is an uncompressed
1023
            // object record, in which case the generation number appears as the third field.
1024
908k
            if (saw_first_compressed_object) {
1025
727k
                if (fields[0] != 2) {
1026
292k
                    uncompressed_after_compressed_ = true;
1027
292k
                }
1028
727k
            } else if (fields[0] == 2) {
1029
2.22k
                saw_first_compressed_object = true;
1030
2.22k
            }
1031
908k
            if (obj == 0) {
1032
                // This is needed by checkLinearization()
1033
2.86k
                first_xref_item_offset_ = xref_offset;
1034
905k
            } else if (fields[0] == 0) {
1035
                // Ignore fields[2], which we don't care about in this case. This works around the
1036
                // issue of some PDF files that put invalid values, like -1, here for deleted
1037
                // objects.
1038
139k
                insertFreeXrefEntry(QPDFObjGen(obj, 0));
1039
766k
            } else {
1040
766k
                auto typ = toI(fields[0]);
1041
766k
                if (!in_stream_recovery || typ == 2) {
1042
                    // If we are in xref stream recovery all actual uncompressed objects have
1043
                    // already been inserted into the xref table. Avoid adding junk data into the
1044
                    // xref table.
1045
586k
                    insertXrefEntry(obj, toI(fields[0]), fields[1], toI(fields[2]));
1046
586k
                }
1047
766k
            }
1048
908k
            ++obj;
1049
908k
        }
1050
4.07k
    }
1051
1052
5.93k
    if (!m->trailer) {
1053
509
        setTrailer(dict);
1054
509
    }
1055
1056
5.93k
    if (dict.hasKey("/Prev")) {
1057
915
        if (!dict.getKey("/Prev").isInteger()) {
1058
69
            throw damagedPDF(
1059
69
                "xref stream", "/Prev key in xref stream dictionary is not an integer");
1060
69
        }
1061
846
        return dict.getKey("/Prev").getIntValue();
1062
5.02k
    } else {
1063
5.02k
        return 0;
1064
5.02k
    }
1065
5.93k
}
1066
1067
void
1068
Objects::insertXrefEntry(int obj, int f0, qpdf_offset_t f1, int f2)
1069
729k
{
1070
    // Populate the xref table in such a way that the first reference to an object that we see,
1071
    // which is the one in the latest xref table in which it appears, is the one that gets stored.
1072
    // This works because we are reading more recent appends before older ones.
1073
1074
    // If there is already an entry for this object and generation in the table, it means that a
1075
    // later xref table has registered this object.  Disregard this one.
1076
729k
    int new_gen = f0 == 2 ? 0 : f2;
1077
1078
729k
    if (!(f0 == 1 || f0 == 2)) {
1079
13.4k
        return;
1080
13.4k
    }
1081
1082
715k
    if (!(obj > 0 && obj <= m->xref_table_max_id && 0 <= f2 && new_gen < 65535)) {
1083
        // We are ignoring invalid objgens. Most will arrive here from xref reconstruction. There
1084
        // is probably no point having another warning but we could count invalid items in order to
1085
        // decide when to give up.
1086
        // ignore impossibly large object ids or object ids > Size.
1087
136k
        return;
1088
136k
    }
1089
1090
579k
    if (m->deleted_objects.contains(obj)) {
1091
850
        return;
1092
850
    }
1093
1094
578k
    if (f0 == 2) {
1095
374k
        if (f1 == obj) {
1096
590
            warn(
1097
590
                damagedPDF("xref stream", "self-referential object stream " + std::to_string(obj)));
1098
590
            return;
1099
590
        }
1100
374k
        if (f1 > m->xref_table_max_id) {
1101
            // ignore impossibly large object stream ids
1102
9.72k
            warn(damagedPDF(
1103
9.72k
                "xref stream",
1104
9.72k
                "object stream id " + std::to_string(f1) + " for object " + std::to_string(obj) +
1105
9.72k
                    " is impossibly large"));
1106
9.72k
            return;
1107
9.72k
        }
1108
374k
    }
1109
1110
568k
    auto [iter, created] = m->xref_table.try_emplace(QPDFObjGen(obj, (f0 == 2 ? 0 : f2)));
1111
568k
    if (!created) {
1112
42.2k
        return;
1113
42.2k
    }
1114
1115
526k
    switch (f0) {
1116
168k
    case 1:
1117
        // f2 is generation
1118
168k
        QTC::TC("qpdf", "QPDF xref gen > 0", ((f2 > 0) ? 1 : 0));
1119
168k
        iter->second = QPDFXRefEntry(f1);
1120
168k
        break;
1121
1122
357k
    case 2:
1123
357k
        iter->second = QPDFXRefEntry(toI(f1), f2);
1124
357k
        break;
1125
1126
0
    default:
1127
0
        throw damagedPDF("xref stream", "unknown xref stream entry type " + std::to_string(f0));
1128
0
        break;
1129
526k
    }
1130
526k
}
1131
1132
void
1133
Objects::insertFreeXrefEntry(QPDFObjGen og)
1134
144k
{
1135
144k
    if (!m->xref_table.contains(og) && og.getObj() <= m->xref_table_max_id) {
1136
65.8k
        m->deleted_objects.insert(og.getObj());
1137
65.8k
    }
1138
144k
}
1139
1140
void
1141
QPDF::showXRefTable()
1142
0
{
1143
0
    auto& cout = *m->cf.log()->getInfo();
1144
0
    for (auto const& iter: m->xref_table) {
1145
0
        QPDFObjGen const& og = iter.first;
1146
0
        QPDFXRefEntry const& entry = iter.second;
1147
0
        cout << og.unparse('/') << ": ";
1148
0
        switch (entry.getType()) {
1149
0
        case 1:
1150
0
            cout << "uncompressed; offset = " << entry.getOffset();
1151
0
            break;
1152
1153
0
        case 2:
1154
0
            *m->cf.log()->getInfo() << "compressed; stream = " << entry.getObjStreamNumber()
1155
0
                                    << ", index = " << entry.getObjStreamIndex();
1156
0
            break;
1157
1158
0
        default:
1159
0
            throw std::logic_error("unknown cross-reference table type while showing xref_table");
1160
0
            break;
1161
0
        }
1162
0
        m->cf.log()->info("\n");
1163
0
    }
1164
0
}
1165
1166
// Resolve all objects in the xref table. If this triggers a xref table reconstruction abort and
1167
// return false. Otherwise return true.
1168
bool
1169
Objects::resolveXRefTable()
1170
9.31k
{
1171
9.31k
    bool may_change = !m->reconstructed_xref;
1172
347k
    for (auto& iter: m->xref_table) {
1173
347k
        if (isUnresolved(iter.first)) {
1174
221k
            resolve(iter.first);
1175
221k
            if (may_change && m->reconstructed_xref) {
1176
49
                return false;
1177
49
            }
1178
221k
        }
1179
347k
    }
1180
9.26k
    return true;
1181
9.31k
}
1182
1183
// Ensure all objects in the pdf file, including those in indirect references, appear in the object
1184
// cache.
1185
void
1186
QPDF::fixDanglingReferences(bool force)
1187
21.3k
{
1188
21.3k
    if (m->fixed_dangling_refs) {
1189
12.0k
        return;
1190
12.0k
    }
1191
9.26k
    if (!m->objects.resolveXRefTable()) {
1192
49
        m->objects.resolveXRefTable();
1193
49
    }
1194
9.26k
    m->fixed_dangling_refs = true;
1195
9.26k
}
1196
1197
size_t
1198
QPDF::getObjectCount()
1199
12.4k
{
1200
    // This method returns the next available indirect object number. makeIndirectObject uses it for
1201
    // this purpose. After fixDanglingReferences is called, all objects in the xref table will also
1202
    // be in obj_cache.
1203
12.4k
    fixDanglingReferences();
1204
12.4k
    QPDFObjGen og;
1205
12.4k
    if (!m->obj_cache.empty()) {
1206
12.3k
        og = (*(m->obj_cache.rbegin())).first;
1207
12.3k
    }
1208
12.4k
    return QIntC::to_size(og.getObj());
1209
12.4k
}
1210
1211
std::vector<QPDFObjectHandle>
1212
QPDF::getAllObjects()
1213
0
{
1214
    // After fixDanglingReferences is called, all objects are in the object cache.
1215
0
    fixDanglingReferences();
1216
0
    std::vector<QPDFObjectHandle> result;
1217
0
    for (auto const& iter: m->obj_cache) {
1218
0
        result.emplace_back(m->objects.newIndirect(iter.first, iter.second.object));
1219
0
    }
1220
0
    return result;
1221
0
}
1222
1223
void
1224
Objects::setLastObjectDescription(std::string const& description, QPDFObjGen og)
1225
234k
{
1226
234k
    m->last_object_description.clear();
1227
234k
    if (!description.empty()) {
1228
7.22k
        m->last_object_description += description;
1229
7.22k
        if (og.isIndirect()) {
1230
7.22k
            m->last_object_description += ": ";
1231
7.22k
        }
1232
7.22k
    }
1233
234k
    if (og.isIndirect()) {
1234
234k
        m->last_object_description += "object " + og.unparse(' ');
1235
234k
    }
1236
234k
}
1237
1238
QPDFObjectHandle
1239
Objects::readTrailer()
1240
16.2k
{
1241
16.2k
    qpdf_offset_t offset = m->file->tell();
1242
16.2k
    auto object =
1243
16.2k
        QPDFParser::parse(*m->file, "trailer", m->tokenizer, nullptr, qpdf, m->reconstructed_xref);
1244
16.2k
    if (object.isDictionary() && m->objects.readToken(*m->file).isWord("stream")) {
1245
149
        warn(damagedPDF("trailer", m->file->tell(), "stream keyword found in trailer"));
1246
149
    }
1247
    // Override last_offset so that it points to the beginning of the object we just read
1248
16.2k
    m->file->setLastOffset(offset);
1249
16.2k
    return object;
1250
16.2k
}
1251
1252
QPDFObjectHandle
1253
Objects::readObject(std::string const& description, QPDFObjGen og)
1254
120k
{
1255
120k
    setLastObjectDescription(description, og);
1256
120k
    qpdf_offset_t offset = m->file->tell();
1257
1258
120k
    StringDecrypter decrypter{&qpdf, og};
1259
120k
    StringDecrypter* decrypter_ptr = m->encp->encrypted ? &decrypter : nullptr;
1260
120k
    auto object = QPDFParser::parse(
1261
120k
        *m->file,
1262
120k
        m->last_object_description,
1263
120k
        m->tokenizer,
1264
120k
        decrypter_ptr,
1265
120k
        qpdf,
1266
120k
        m->reconstructed_xref || m->in_read_xref_stream);
1267
120k
    if (!object) {
1268
9.28k
        return {};
1269
9.28k
    }
1270
111k
    auto token = readToken(*m->file);
1271
111k
    if (object.isDictionary() && token.isWord("stream")) {
1272
48.3k
        readStream(object, og, offset);
1273
48.3k
        token = readToken(*m->file);
1274
48.3k
    }
1275
111k
    if (!token.isWord("endobj")) {
1276
26.3k
        warn(damagedPDF("expected endobj"));
1277
26.3k
    }
1278
111k
    return object;
1279
120k
}
1280
1281
// After reading stream dictionary and stream keyword, read rest of stream.
1282
void
1283
Objects::readStream(QPDFObjectHandle& object, QPDFObjGen og, qpdf_offset_t offset)
1284
48.3k
{
1285
48.3k
    validateStreamLineEnd(object, og, offset);
1286
1287
    // Must get offset before accessing any additional objects since resolving a previously
1288
    // unresolved indirect object will change file position.
1289
48.3k
    qpdf_offset_t stream_offset = m->file->tell();
1290
48.3k
    size_t length = 0;
1291
1292
48.3k
    try {
1293
48.3k
        auto length_obj = object.getKey("/Length");
1294
1295
48.3k
        if (!length_obj.isInteger()) {
1296
16.1k
            if (length_obj.null()) {
1297
15.8k
                throw damagedPDF(offset, "stream dictionary lacks /Length key");
1298
15.8k
            }
1299
241
            throw damagedPDF(offset, "/Length key in stream dictionary is not an integer");
1300
16.1k
        }
1301
1302
32.2k
        length = toS(length_obj.getUIntValue());
1303
        // Seek in two steps to avoid potential integer overflow
1304
32.2k
        m->file->seek(stream_offset, SEEK_SET);
1305
32.2k
        m->file->seek(toO(length), SEEK_CUR);
1306
32.2k
        if (!readToken(*m->file).isWord("endstream")) {
1307
8.42k
            throw damagedPDF("expected endstream");
1308
8.42k
        }
1309
32.2k
    } catch (QPDFExc& e) {
1310
32.1k
        if (!cf.surpress_recovery()) {
1311
32.1k
            warn(e);
1312
32.1k
            length = recoverStreamLength(m->file, og, stream_offset);
1313
32.1k
        } else {
1314
0
            throw;
1315
0
        }
1316
32.1k
    }
1317
39.3k
    object = QPDFObjectHandle(qpdf::Stream(qpdf, og, object, stream_offset, length));
1318
39.3k
}
1319
1320
void
1321
Objects::validateStreamLineEnd(QPDFObjectHandle& object, QPDFObjGen og, qpdf_offset_t offset)
1322
48.3k
{
1323
    // The PDF specification states that the word "stream" should be followed by either a carriage
1324
    // return and a newline or by a newline alone.  It specifically disallowed following it by a
1325
    // carriage return alone since, in that case, there would be no way to tell whether the NL in a
1326
    // CR NL sequence was part of the stream data.  However, some readers, including Adobe reader,
1327
    // accept a carriage return by itself when followed by a non-newline character, so that's what
1328
    // we do here. We have also seen files that have extraneous whitespace between the stream
1329
    // keyword and the newline.
1330
54.3k
    while (true) {
1331
54.3k
        char ch;
1332
54.3k
        if (m->file->read(&ch, 1) == 0) {
1333
            // A premature EOF here will result in some other problem that will get reported at
1334
            // another time.
1335
127
            return;
1336
127
        }
1337
54.1k
        if (ch == '\n') {
1338
            // ready to read stream data
1339
28.4k
            return;
1340
28.4k
        }
1341
25.7k
        if (ch == '\r') {
1342
            // Read another character
1343
15.6k
            if (m->file->read(&ch, 1) != 0) {
1344
15.6k
                if (ch == '\n') {
1345
                    // Ready to read stream data
1346
14.7k
                    QTC::TC("qpdf", "QPDF stream with CRNL");
1347
14.7k
                } else {
1348
                    // Treat the \r by itself as the whitespace after endstream and start reading
1349
                    // stream data in spite of not having seen a newline.
1350
929
                    m->file->unreadCh(ch);
1351
929
                    warn(damagedPDF(
1352
929
                        m->file->tell(), "stream keyword followed by carriage return only"));
1353
929
                }
1354
15.6k
            }
1355
15.6k
            return;
1356
15.6k
        }
1357
10.1k
        if (!util::is_space(ch)) {
1358
4.04k
            m->file->unreadCh(ch);
1359
4.04k
            warn(damagedPDF(
1360
4.04k
                m->file->tell(), "stream keyword not followed by proper line terminator"));
1361
4.04k
            return;
1362
4.04k
        }
1363
6.05k
        warn(damagedPDF(m->file->tell(), "stream keyword followed by extraneous whitespace"));
1364
6.05k
    }
1365
48.3k
}
1366
1367
bool
1368
Objects ::findEndstream()
1369
30.5k
{
1370
    // Find endstream or endobj. Position the input at that token.
1371
30.5k
    auto t = readToken(*m->file, 20);
1372
30.5k
    if (t.isWord("endobj") || t.isWord("endstream")) {
1373
22.7k
        m->file->seek(m->file->getLastOffset(), SEEK_SET);
1374
22.7k
        return true;
1375
22.7k
    }
1376
7.86k
    return false;
1377
30.5k
}
1378
1379
size_t
1380
Objects::recoverStreamLength(
1381
    std::shared_ptr<InputSource> input, QPDFObjGen og, qpdf_offset_t stream_offset)
1382
23.4k
{
1383
    // Try to reconstruct stream length by looking for endstream or endobj
1384
23.4k
    warn(damagedPDF(*input, stream_offset, "attempting to recover stream length"));
1385
1386
23.4k
    PatternFinder ef(*this, &Objects::findEndstream);
1387
23.4k
    size_t length = 0;
1388
23.4k
    if (m->file->findFirst("end", stream_offset, 0, ef)) {
1389
22.7k
        length = toS(m->file->tell() - stream_offset);
1390
        // Reread endstream but, if it was endobj, don't skip that.
1391
22.7k
        QPDFTokenizer::Token t = readToken(*m->file);
1392
22.7k
        if (t.getValue() == "endobj") {
1393
12.6k
            m->file->seek(m->file->getLastOffset(), SEEK_SET);
1394
12.6k
        }
1395
22.7k
    }
1396
1397
23.4k
    if (length) {
1398
22.4k
        auto end = stream_offset + toO(length);
1399
22.4k
        qpdf_offset_t found_offset = 0;
1400
22.4k
        QPDFObjGen found_og;
1401
1402
        // Make sure this is inside this object
1403
574k
        for (auto const& [current_og, entry]: m->xref_table) {
1404
574k
            if (entry.getType() == 1) {
1405
501k
                qpdf_offset_t obj_offset = entry.getOffset();
1406
501k
                if (found_offset < obj_offset && obj_offset < end) {
1407
127k
                    found_offset = obj_offset;
1408
127k
                    found_og = current_og;
1409
127k
                }
1410
501k
            }
1411
574k
        }
1412
22.4k
        if (!found_offset || found_og == og) {
1413
            // If we are trying to recover an XRef stream the xref table will not contain and
1414
            // won't contain any entries, therefore we cannot check the found length. Otherwise we
1415
            // found endstream\nendobj within the space allowed for this object, so we're probably
1416
            // in good shape.
1417
20.7k
        } else {
1418
1.68k
            length = 0;
1419
1.68k
        }
1420
22.4k
    }
1421
1422
23.4k
    if (length == 0) {
1423
2.63k
        warn(damagedPDF(
1424
2.63k
            *input, stream_offset, "unable to recover stream data; treating stream as empty"));
1425
20.7k
    } else {
1426
20.7k
        warn(damagedPDF(
1427
20.7k
            *input, stream_offset, "recovered stream length: " + std::to_string(length)));
1428
20.7k
    }
1429
1430
23.4k
    return length;
1431
23.4k
}
1432
1433
QPDFTokenizer::Token
1434
Objects::readToken(InputSource& input, size_t max_len)
1435
3.28M
{
1436
3.28M
    return m->tokenizer.readToken(input, m->last_object_description, true, max_len);
1437
3.28M
}
1438
1439
QPDFObjGen
1440
Objects::read_object_start(qpdf_offset_t offset)
1441
122k
{
1442
122k
    m->file->seek(offset, SEEK_SET);
1443
122k
    QPDFTokenizer::Token tobjid = readToken(*m->file);
1444
122k
    bool objidok = tobjid.isInteger();
1445
122k
    if (!objidok) {
1446
1.84k
        throw damagedPDF(offset, "expected n n obj");
1447
1.84k
    }
1448
120k
    QPDFTokenizer::Token tgen = readToken(*m->file);
1449
120k
    bool genok = tgen.isInteger();
1450
120k
    if (!genok) {
1451
88
        throw damagedPDF(offset, "expected n n obj");
1452
88
    }
1453
120k
    QPDFTokenizer::Token tobj = readToken(*m->file);
1454
1455
120k
    bool objok = tobj.isWord("obj");
1456
1457
120k
    if (!objok) {
1458
162
        throw damagedPDF(offset, "expected n n obj");
1459
162
    }
1460
120k
    int objid = QUtil::string_to_int(tobjid.getValue().c_str());
1461
120k
    int generation = QUtil::string_to_int(tgen.getValue().c_str());
1462
120k
    if (objid == 0) {
1463
12
        throw damagedPDF(offset, "object with ID 0");
1464
12
    }
1465
120k
    return {objid, generation};
1466
120k
}
1467
1468
void
1469
Objects::readObjectAtOffset(
1470
    bool try_recovery, qpdf_offset_t offset, std::string const& description, QPDFObjGen exp_og)
1471
113k
{
1472
113k
    QPDFObjGen og;
1473
113k
    setLastObjectDescription(description, exp_og);
1474
1475
113k
    if (cf.surpress_recovery()) {
1476
0
        try_recovery = false;
1477
0
    }
1478
1479
    // Special case: if offset is 0, just return null.  Some PDF writers, in particular
1480
    // "Mac OS X 10.7.5 Quartz PDFContext", may store deleted objects in the xref table as
1481
    // "0000000000 00000 n", which is not correct, but it won't hurt anything for us to ignore
1482
    // these.
1483
113k
    if (offset == 0) {
1484
204
        warn(damagedPDF(
1485
204
            -1,
1486
204
            "object has offset 0 - a common error handled correctly by qpdf and most other "
1487
204
            "applications"));
1488
204
        return;
1489
204
    }
1490
1491
113k
    try {
1492
113k
        og = read_object_start(offset);
1493
113k
        if (exp_og != og) {
1494
30
            QPDFExc e = damagedPDF(offset, "expected " + exp_og.unparse(' ') + " obj");
1495
30
            if (try_recovery) {
1496
                // Will be retried below
1497
30
                throw e;
1498
30
            } else {
1499
                // We can try reading the object anyway even if the ID doesn't match.
1500
0
                warn(e);
1501
0
            }
1502
30
        }
1503
113k
    } catch (QPDFExc& e) {
1504
269
        if (!try_recovery) {
1505
0
            throw;
1506
0
        }
1507
        // Try again after reconstructing xref table
1508
269
        reconstruct_xref(e);
1509
269
        if (m->xref_table.contains(exp_og) && m->xref_table[exp_og].getType() == 1) {
1510
50
            qpdf_offset_t new_offset = m->xref_table[exp_og].getOffset();
1511
50
            readObjectAtOffset(false, new_offset, description, exp_og);
1512
50
            return;
1513
50
        }
1514
219
        warn(damagedPDF(
1515
219
            "",
1516
219
            -1,
1517
219
            ("object " + exp_og.unparse(' ') +
1518
219
             " not found in file after regenerating cross reference table")));
1519
219
        return;
1520
269
    }
1521
1522
113k
    if (auto oh = readObject(description, og)) {
1523
        // Determine the end offset of this object before and after white space.  We use these
1524
        // numbers to validate linearization hint tables.  Offsets and lengths of objects may imply
1525
        // the end of an object to be anywhere between these values.
1526
91.8k
        qpdf_offset_t end_before_space = m->file->tell();
1527
1528
        // skip over spaces
1529
206k
        while (true) {
1530
206k
            char ch;
1531
206k
            if (!m->file->read(&ch, 1)) {
1532
835
                throw damagedPDF(m->file->tell(), "EOF after endobj");
1533
835
            }
1534
205k
            if (!isspace(static_cast<unsigned char>(ch))) {
1535
91.0k
                m->file->seek(-1, SEEK_CUR);
1536
91.0k
                break;
1537
91.0k
            }
1538
205k
        }
1539
91.0k
        m->objects.updateCache(og, oh.obj_sp(), end_before_space, m->file->tell());
1540
91.0k
    }
1541
113k
}
1542
1543
QPDFObjectHandle
1544
Objects::readObjectAtOffset(
1545
    qpdf_offset_t offset, std::string const& description, bool skip_cache_if_in_xref)
1546
9.13k
{
1547
9.13k
    auto og = read_object_start(offset);
1548
9.13k
    auto oh = readObject(description, og);
1549
1550
9.13k
    if (!oh || !m->objects.isUnresolved(og)) {
1551
5.27k
        return oh;
1552
5.27k
    }
1553
1554
3.86k
    if (skip_cache_if_in_xref && m->xref_table.contains(og)) {
1555
        // In the special case of the xref stream and linearization hint tables, the offset comes
1556
        // from another source. For the specific case of xref streams, the xref stream is read and
1557
        // loaded into the object cache very early in parsing. Ordinarily, when a file is updated by
1558
        // appending, items inserted into the xref table in later updates take precedence over
1559
        // earlier items. In the special case of reusing the object number previously used as the
1560
        // xref stream, we have the following order of events:
1561
        //
1562
        // * reused object gets loaded into the xref table
1563
        // * old object is read here while reading xref streams
1564
        // * original xref entry is ignored (since already in xref table)
1565
        //
1566
        // It is the second step that causes a problem. Even though the xref table is correct in
1567
        // this case, the old object is already in the cache and so effectively prevails over the
1568
        // reused object. To work around this issue, we have a special case for the xref stream (via
1569
        // the skip_cache_if_in_xref): if the object is already in the xref stream, don't cache what
1570
        // we read here.
1571
        //
1572
        // It is likely that the same bug may exist for linearization hint tables, but the existing
1573
        // code uses end_before_space and end_after_space from the cache, so fixing that would
1574
        // require more significant rework. The chances of a linearization hint stream being reused
1575
        // seems smaller because the xref stream is probably the highest object in the file and the
1576
        // linearization hint stream would be some random place in the middle, so I'm leaving that
1577
        // bug unfixed for now. If the bug were to be fixed, we could use !check_og in place of
1578
        // skip_cache_if_in_xref.
1579
12
        QTC::TC("qpdf", "QPDF skipping cache for known unchecked object");
1580
12
        return oh;
1581
12
    }
1582
1583
    // Determine the end offset of this object before and after white space.  We use these
1584
    // numbers to validate linearization hint tables.  Offsets and lengths of objects may imply
1585
    // the end of an object to be anywhere between these values.
1586
3.85k
    qpdf_offset_t end_before_space = m->file->tell();
1587
1588
    // skip over spaces
1589
6.17k
    while (true) {
1590
4.24k
        char ch;
1591
4.24k
        if (!m->file->read(&ch, 1)) {
1592
53
            throw damagedPDF(m->file->tell(), "EOF after endobj");
1593
53
        }
1594
4.19k
        if (!isspace(static_cast<unsigned char>(ch))) {
1595
1.87k
            m->file->seek(-1, SEEK_CUR);
1596
1.87k
            break;
1597
1.87k
        }
1598
4.19k
    }
1599
3.79k
    m->objects.updateCache(og, oh.obj_sp(), end_before_space, m->file->tell());
1600
1601
3.79k
    return oh;
1602
3.85k
}
1603
1604
std::shared_ptr<QPDFObject> const&
1605
Objects::resolve(QPDFObjGen og)
1606
487k
{
1607
487k
    if (!isUnresolved(og)) {
1608
0
        return m->obj_cache[og].object;
1609
0
    }
1610
1611
487k
    if (m->resolving.contains(og)) {
1612
        // This can happen if an object references itself directly or indirectly in some key that
1613
        // has to be resolved during object parsing, such as stream length.
1614
104
        warn(damagedPDF("", "loop detected resolving object " + og.unparse(' ')));
1615
104
        updateCache(og, QPDFObject::create<QPDF_Null>(), -1, -1);
1616
104
        return m->obj_cache[og].object;
1617
104
    }
1618
487k
    ResolveRecorder rr(qpdf, og);
1619
1620
487k
    if (m->xref_table.contains(og)) {
1621
360k
        QPDFXRefEntry const& entry = m->xref_table[og];
1622
360k
        try {
1623
360k
            switch (entry.getType()) {
1624
113k
            case 1:
1625
                // Object stored in cache by readObjectAtOffset
1626
113k
                readObjectAtOffset(true, entry.getOffset(), "", og);
1627
113k
                break;
1628
1629
247k
            case 2:
1630
247k
                resolveObjectsInStream(entry.getObjStreamNumber());
1631
247k
                break;
1632
1633
12
            default:
1634
12
                throw damagedPDF(
1635
12
                    "", -1, ("object " + og.unparse('/') + " has unexpected xref entry type"));
1636
360k
            }
1637
360k
        } catch (QPDFExc& e) {
1638
33.8k
            warn(e);
1639
33.8k
        } catch (std::exception& e) {
1640
132
            warn(damagedPDF(
1641
132
                "", -1, ("object " + og.unparse('/') + ": error reading object: " + e.what())));
1642
132
        }
1643
360k
    }
1644
1645
471k
    if (isUnresolved(og)) {
1646
        // PDF spec says unknown objects resolve to the null object.
1647
379k
        updateCache(og, QPDFObject::create<QPDF_Null>(), -1, -1);
1648
379k
    }
1649
1650
471k
    auto& result(m->obj_cache[og].object);
1651
471k
    result->setDefaultDescription(&qpdf, og);
1652
471k
    return result;
1653
487k
}
1654
1655
void
1656
Objects::resolveObjectsInStream(int obj_stream_number)
1657
247k
{
1658
247k
    auto damaged =
1659
247k
        [this, obj_stream_number](int id, qpdf_offset_t offset, std::string const& msg) -> QPDFExc {
1660
17.8k
        return {
1661
17.8k
            qpdf_e_damaged_pdf,
1662
17.8k
            m->file->getName() + " object stream " + std::to_string(obj_stream_number),
1663
17.8k
            +"object " + std::to_string(id) + " 0",
1664
17.8k
            offset,
1665
17.8k
            msg,
1666
17.8k
            true};
1667
17.8k
    };
1668
1669
247k
    if (m->resolved_object_streams.contains(obj_stream_number)) {
1670
225k
        return;
1671
225k
    }
1672
21.8k
    m->resolved_object_streams.insert(obj_stream_number);
1673
    // Force resolution of object stream
1674
21.8k
    Stream obj_stream = qpdf.getObject(obj_stream_number, 0);
1675
21.8k
    if (!obj_stream) {
1676
17.3k
        throw damagedPDF(
1677
17.3k
            "object " + std::to_string(obj_stream_number) + " 0",
1678
17.3k
            "supposed object stream " + std::to_string(obj_stream_number) + " is not a stream");
1679
17.3k
    }
1680
1681
    // For linearization data in the object, use the data from the object stream for the objects in
1682
    // the stream.
1683
4.57k
    QPDFObjGen stream_og(obj_stream_number, 0);
1684
4.57k
    qpdf_offset_t end_before_space = m->obj_cache[stream_og].end_before_space;
1685
4.57k
    qpdf_offset_t end_after_space = m->obj_cache[stream_og].end_after_space;
1686
1687
4.57k
    QPDFObjectHandle dict = obj_stream.getDict();
1688
4.57k
    if (!dict.isDictionaryOfType("/ObjStm")) {
1689
513
        warn(damagedPDF(
1690
513
            "object " + std::to_string(obj_stream_number) + " 0",
1691
513
            "supposed object stream " + std::to_string(obj_stream_number) + " has wrong type"));
1692
513
    }
1693
1694
4.57k
    unsigned int n{0};
1695
4.57k
    int first{0};
1696
4.57k
    if (!(dict.getKey("/N").getValueAsUInt(n) && dict.getKey("/First").getValueAsInt(first))) {
1697
198
        throw damagedPDF(
1698
198
            "object " + std::to_string(obj_stream_number) + " 0",
1699
198
            "object stream " + std::to_string(obj_stream_number) + " has incorrect keys");
1700
198
    }
1701
1702
    // id, offset, size
1703
4.37k
    std::vector<std::tuple<int, qpdf_offset_t, size_t>> offsets;
1704
1705
4.37k
    auto stream_data = obj_stream.getStreamData(qpdf_dl_specialized);
1706
1707
4.37k
    is::OffsetBuffer input("", stream_data);
1708
1709
4.37k
    const auto b_size = stream_data.size();
1710
4.37k
    const auto end_offset = static_cast<qpdf_offset_t>(b_size);
1711
4.37k
    auto b_start = stream_data.data();
1712
1713
4.37k
    if (first >= end_offset) {
1714
45
        throw damagedPDF(
1715
45
            "object " + std::to_string(obj_stream_number) + " 0",
1716
45
            "object stream " + std::to_string(obj_stream_number) + " has invalid /First entry");
1717
45
    }
1718
1719
4.33k
    int id = 0;
1720
4.33k
    long long last_offset = -1;
1721
4.33k
    bool is_first = true;
1722
75.8k
    for (unsigned int i = 0; i < n; ++i) {
1723
71.6k
        auto tnum = readToken(input);
1724
71.6k
        auto id_offset = input.getLastOffset();
1725
71.6k
        auto toffset = readToken(input);
1726
71.6k
        if (!(tnum.isInteger() && toffset.isInteger())) {
1727
126
            throw damaged(0, input.getLastOffset(), "expected integer in object stream header");
1728
126
        }
1729
1730
71.5k
        int num = QUtil::string_to_int(tnum.getValue().c_str());
1731
71.5k
        long long offset = QUtil::string_to_int(toffset.getValue().c_str());
1732
1733
71.5k
        if (num == obj_stream_number) {
1734
351
            warn(damaged(num, id_offset, "object stream claims to contain itself"));
1735
351
            continue;
1736
351
        }
1737
1738
71.1k
        if (num < 1) {
1739
743
            warn(damaged(num, id_offset, "object id is invalid"s));
1740
743
            continue;
1741
743
        }
1742
1743
70.4k
        if (offset <= last_offset) {
1744
7.41k
            warn(damaged(
1745
7.41k
                num,
1746
7.41k
                input.getLastOffset(),
1747
7.41k
                "offset " + std::to_string(offset) +
1748
7.41k
                    " is invalid (must be larger than previous offset " +
1749
7.41k
                    std::to_string(last_offset) + ")"));
1750
7.41k
            continue;
1751
7.41k
        }
1752
1753
63.0k
        if (num > m->xref_table_max_id) {
1754
1.61k
            continue;
1755
1.61k
        }
1756
1757
61.4k
        if (first + offset >= end_offset) {
1758
9.24k
            warn(damaged(
1759
9.24k
                num, input.getLastOffset(), "offset " + std::to_string(offset) + " is too large"));
1760
9.24k
            continue;
1761
9.24k
        }
1762
1763
52.1k
        if (is_first) {
1764
1.27k
            is_first = false;
1765
50.8k
        } else {
1766
50.8k
            offsets.emplace_back(
1767
50.8k
                id, last_offset + first, static_cast<size_t>(offset - last_offset));
1768
50.8k
        }
1769
1770
52.1k
        last_offset = offset;
1771
52.1k
        id = num;
1772
52.1k
    }
1773
1774
4.20k
    if (!is_first) {
1775
        // We found at least one valid entry.
1776
1.12k
        offsets.emplace_back(
1777
1.12k
            id, last_offset + first, b_size - static_cast<size_t>(last_offset + first));
1778
1.12k
    }
1779
1780
    // To avoid having to read the object stream multiple times, store all objects that would be
1781
    // found here in the cache.  Remember that some objects stored here might have been overridden
1782
    // by new objects appended to the file, so it is necessary to recheck the xref table and only
1783
    // cache what would actually be resolved here.
1784
47.9k
    for (auto const& [obj_id, obj_offset, obj_size]: offsets) {
1785
47.9k
        QPDFObjGen og(obj_id, 0);
1786
47.9k
        auto entry = m->xref_table.find(og);
1787
47.9k
        if (entry != m->xref_table.end() && entry->second.getType() == 2 &&
1788
46.0k
            entry->second.getObjStreamNumber() == obj_stream_number) {
1789
45.7k
            is::OffsetBuffer in("", {b_start + obj_offset, obj_size}, obj_offset);
1790
45.7k
            if (auto oh = QPDFParser::parse(in, obj_stream_number, obj_id, m->tokenizer, qpdf)) {
1791
42.3k
                updateCache(og, oh.obj_sp(), end_before_space, end_after_space);
1792
42.3k
            }
1793
45.7k
        } else {
1794
2.23k
            QTC::TC("qpdf", "QPDF not caching overridden objstm object");
1795
2.23k
        }
1796
47.9k
    }
1797
4.20k
}
1798
1799
QPDFObjectHandle
1800
Objects::newIndirect(QPDFObjGen og, std::shared_ptr<QPDFObject> const& obj)
1801
2.38k
{
1802
2.38k
    obj->setDefaultDescription(&qpdf, og);
1803
2.38k
    return {obj};
1804
2.38k
}
1805
1806
void
1807
Objects::updateCache(
1808
    QPDFObjGen og,
1809
    std::shared_ptr<QPDFObject> const& object,
1810
    qpdf_offset_t end_before_space,
1811
    qpdf_offset_t end_after_space,
1812
    bool destroy)
1813
514k
{
1814
514k
    object->setObjGen(&qpdf, og);
1815
514k
    if (isCached(og)) {
1816
291k
        auto& cache = m->obj_cache[og];
1817
291k
        object->move_to(cache.object, destroy);
1818
291k
        cache.end_before_space = end_before_space;
1819
291k
        cache.end_after_space = end_after_space;
1820
291k
    } else {
1821
223k
        m->obj_cache[og] = ObjCache(object, end_before_space, end_after_space);
1822
223k
    }
1823
514k
}
1824
1825
bool
1826
Objects::isCached(QPDFObjGen og)
1827
1.82M
{
1828
1.82M
    return m->obj_cache.contains(og);
1829
1.82M
}
1830
1831
bool
1832
Objects::isUnresolved(QPDFObjGen og)
1833
1.31M
{
1834
1.31M
    return !isCached(og) || m->obj_cache[og].object->isUnresolved();
1835
1.31M
}
1836
1837
QPDFObjGen
1838
Objects::nextObjGen()
1839
2.39k
{
1840
2.39k
    int max_objid = toI(qpdf.getObjectCount());
1841
2.39k
    if (max_objid == std::numeric_limits<int>::max()) {
1842
0
        throw std::range_error("max object id is too high to create new objects");
1843
0
    }
1844
2.39k
    return {max_objid + 1, 0};
1845
2.39k
}
1846
1847
QPDFObjectHandle
1848
Objects::makeIndirectFromQPDFObject(std::shared_ptr<QPDFObject> const& obj)
1849
2.39k
{
1850
2.39k
    QPDFObjGen next{nextObjGen()};
1851
2.39k
    m->obj_cache[next] = ObjCache(obj, -1, -1);
1852
2.39k
    return newIndirect(next, m->obj_cache[next].object);
1853
2.39k
}
1854
1855
QPDFObjectHandle
1856
QPDF::makeIndirectObject(QPDFObjectHandle oh)
1857
2.39k
{
1858
2.39k
    if (!oh) {
1859
0
        throw std::logic_error("attempted to make an uninitialized QPDFObjectHandle indirect");
1860
0
    }
1861
2.39k
    return m->objects.makeIndirectFromQPDFObject(oh.obj_sp());
1862
2.39k
}
1863
1864
std::shared_ptr<QPDFObject>
1865
Objects::getObjectForParser(int id, int gen, bool parse_pdf)
1866
434k
{
1867
    // This method is called by the parser and therefore must not resolve any objects.
1868
434k
    auto og = QPDFObjGen(id, gen);
1869
434k
    if (auto iter = m->obj_cache.find(og); iter != m->obj_cache.end()) {
1870
171k
        return iter->second.object;
1871
171k
    }
1872
262k
    if (m->xref_table.contains(og) || (!m->parsed && og.getObj() < m->xref_table_max_id)) {
1873
246k
        return m->obj_cache.insert({og, QPDFObject::create<QPDF_Unresolved>(&qpdf, og)})
1874
246k
            .first->second.object;
1875
246k
    }
1876
16.2k
    if (parse_pdf) {
1877
16.2k
        return QPDFObject::create<QPDF_Null>();
1878
16.2k
    }
1879
0
    return m->obj_cache.insert({og, QPDFObject::create<QPDF_Null>(&qpdf, og)}).first->second.object;
1880
16.2k
}
1881
1882
std::shared_ptr<QPDFObject>
1883
Objects::getObjectForJSON(int id, int gen)
1884
0
{
1885
0
    auto og = QPDFObjGen(id, gen);
1886
0
    auto [it, inserted] = m->obj_cache.try_emplace(og);
1887
0
    auto& obj = it->second.object;
1888
0
    if (inserted) {
1889
0
        obj = (m->parsed && !m->xref_table.contains(og))
1890
0
            ? QPDFObject::create<QPDF_Null>(&qpdf, og)
1891
0
            : QPDFObject::create<QPDF_Unresolved>(&qpdf, og);
1892
0
    }
1893
0
    return obj;
1894
0
}
1895
1896
QPDFObjectHandle
1897
QPDF::getObject(QPDFObjGen og)
1898
262k
{
1899
262k
    if (auto it = m->obj_cache.find(og); it != m->obj_cache.end()) {
1900
204k
        return {it->second.object};
1901
204k
    } else if (m->parsed && !m->xref_table.contains(og)) {
1902
5.02k
        return QPDFObject::create<QPDF_Null>();
1903
53.4k
    } else {
1904
53.4k
        auto result =
1905
53.4k
            m->obj_cache.try_emplace(og, QPDFObject::create<QPDF_Unresolved>(this, og), -1, -1);
1906
53.4k
        return {result.first->second.object};
1907
53.4k
    }
1908
262k
}
1909
1910
void
1911
QPDF::replaceObject(int objid, int generation, QPDFObjectHandle oh)
1912
0
{
1913
0
    replaceObject(QPDFObjGen(objid, generation), oh);
1914
0
}
1915
1916
void
1917
QPDF::replaceObject(QPDFObjGen og, QPDFObjectHandle oh)
1918
0
{
1919
0
    if (!oh || (oh.isIndirect() && !(oh.isStream() && oh.getObjGen() == og))) {
1920
0
        throw std::logic_error("QPDF::replaceObject called with indirect object handle");
1921
0
    }
1922
0
    m->objects.updateCache(og, oh.obj_sp(), -1, -1, false);
1923
0
}
1924
1925
void
1926
QPDF::removeObject(QPDFObjGen og)
1927
1.60k
{
1928
1.60k
    m->xref_table.erase(og);
1929
1.60k
    if (auto cached = m->obj_cache.find(og); cached != m->obj_cache.end()) {
1930
        // Take care of any object handles that may be floating around.
1931
1.30k
        cached->second.object->assign_null();
1932
1.30k
        cached->second.object->setObjGen(nullptr, QPDFObjGen());
1933
1.30k
        m->obj_cache.erase(cached);
1934
1.30k
    }
1935
1.60k
}
1936
1937
void
1938
QPDF::replaceReserved(QPDFObjectHandle reserved, QPDFObjectHandle replacement)
1939
0
{
1940
0
    QTC::TC("qpdf", "QPDF replaceReserved");
1941
0
    auto tc = reserved.getTypeCode();
1942
0
    if (!(tc == ::ot_reserved || tc == ::ot_null)) {
1943
0
        throw std::logic_error("replaceReserved called with non-reserved object");
1944
0
    }
1945
0
    replaceObject(reserved.getObjGen(), replacement);
1946
0
}
1947
1948
void
1949
QPDF::swapObjects(int objid1, int generation1, int objid2, int generation2)
1950
0
{
1951
0
    swapObjects(QPDFObjGen(objid1, generation1), QPDFObjGen(objid2, generation2));
1952
0
}
1953
1954
void
1955
QPDF::swapObjects(QPDFObjGen og1, QPDFObjGen og2)
1956
0
{
1957
    // Force objects to be read from the input source if needed, then swap them in the cache.
1958
0
    m->objects.resolve(og1);
1959
0
    m->objects.resolve(og2);
1960
0
    m->obj_cache[og1].object->swapWith(m->obj_cache[og2].object);
1961
0
}
1962
1963
size_t
1964
Objects::table_size()
1965
8.97k
{
1966
    // If obj_cache is dense, accommodate all object in tables,else accommodate only original
1967
    // objects.
1968
8.97k
    auto max_xref = !m->xref_table.empty() ? m->xref_table.crbegin()->first.getObj() : 0;
1969
8.97k
    auto max_obj = !m->obj_cache.empty() ? m->obj_cache.crbegin()->first.getObj() : 0;
1970
8.97k
    auto max_id = std::numeric_limits<int>::max() - 1;
1971
8.97k
    if (max_obj >= max_id || max_xref >= max_id) {
1972
        // Temporary fix. Long-term solution is
1973
        // - QPDFObjGen to enforce objgens are valid and sensible
1974
        // - xref table and obj cache to protect against insertion of impossibly large obj ids
1975
0
        stopOnError("Impossibly large object id encountered.");
1976
0
    }
1977
8.97k
    if (max_obj < 1.1 * std::max(toI(m->obj_cache.size()), max_xref)) {
1978
6.94k
        return toS(++max_obj);
1979
6.94k
    }
1980
2.02k
    return toS(++max_xref);
1981
8.97k
}
1982
1983
std::vector<QPDFObjGen>
1984
Objects::compressible_vector()
1985
0
{
1986
0
    return compressible<QPDFObjGen>();
1987
0
}
1988
1989
std::vector<bool>
1990
Objects::compressible_set()
1991
1.08k
{
1992
1.08k
    return compressible<bool>();
1993
1.08k
}
1994
1995
template <typename T>
1996
std::vector<T>
1997
Objects::compressible()
1998
1.08k
{
1999
    // Return a list of objects that are allowed to be in object streams.  Walk through the objects
2000
    // by traversing the document from the root, including a traversal of the pages tree.  This
2001
    // makes that objects that are on the same page are more likely to be in the same object stream,
2002
    // which is slightly more efficient, particularly with linearized files.  This is better than
2003
    // iterating through the xref table since it avoids preserving orphaned items.
2004
2005
    // Exclude encryption dictionary, if any
2006
1.08k
    QPDFObjectHandle encryption_dict = m->trailer.getKey("/Encrypt");
2007
1.08k
    QPDFObjGen encryption_dict_og = encryption_dict.getObjGen();
2008
2009
1.08k
    const size_t max_obj = qpdf.getObjectCount();
2010
1.08k
    std::vector<bool> visited(max_obj, false);
2011
1.08k
    std::vector<QPDFObjectHandle> queue;
2012
1.08k
    queue.reserve(512);
2013
1.08k
    queue.emplace_back(m->trailer);
2014
1.08k
    std::vector<T> result;
2015
1.08k
    if constexpr (std::is_same_v<T, QPDFObjGen>) {
2016
0
        result.reserve(m->obj_cache.size());
2017
1.08k
    } else {
2018
1.08k
        qpdf_static_expect(std::is_same_v<T, bool>);
2019
1.08k
        result.resize(max_obj + 1U, false);
2020
1.08k
    }
2021
497k
    while (!queue.empty()) {
2022
496k
        auto obj = queue.back();
2023
496k
        queue.pop_back();
2024
496k
        if (obj.getObjectID() > 0) {
2025
107k
            QPDFObjGen og = obj.getObjGen();
2026
107k
            const size_t id = toS(og.getObj() - 1);
2027
107k
            if (id >= max_obj) {
2028
0
                throw std::logic_error(
2029
0
                    "unexpected object id encountered in getCompressibleObjGens");
2030
0
            }
2031
107k
            if (visited[id]) {
2032
32.7k
                continue;
2033
32.7k
            }
2034
2035
            // Check whether this is the current object. If not, remove it (which changes it into a
2036
            // direct null and therefore stops us from revisiting it) and move on to the next object
2037
            // in the queue.
2038
74.3k
            auto upper = m->obj_cache.upper_bound(og);
2039
74.3k
            if (upper != m->obj_cache.end() && upper->first.getObj() == og.getObj()) {
2040
882
                qpdf.removeObject(og);
2041
882
                continue;
2042
882
            }
2043
2044
73.4k
            visited[id] = true;
2045
2046
73.4k
            if (og == encryption_dict_og) {
2047
112
                QTC::TC("qpdf", "QPDF exclude encryption dictionary");
2048
73.3k
            } else if (!(obj.isStream() ||
2049
69.2k
                         (obj.isDictionaryOfType("/Sig") && obj.hasKey("/ByteRange") &&
2050
69.2k
                          obj.hasKey("/Contents")))) {
2051
69.2k
                if constexpr (std::is_same_v<T, QPDFObjGen>) {
2052
0
                    result.push_back(og);
2053
69.2k
                } else if constexpr (std::is_same_v<T, bool>) {
2054
69.2k
                    result[id + 1U] = true;
2055
69.2k
                }
2056
69.2k
            }
2057
73.4k
        }
2058
462k
        if (obj.isStream()) {
2059
4.09k
            auto dict = obj.getDict().as_dictionary();
2060
4.09k
            auto end = dict.crend();
2061
31.0k
            for (auto iter = dict.crbegin(); iter != end; ++iter) {
2062
26.9k
                std::string const& key = iter->first;
2063
26.9k
                QPDFObjectHandle const& value = iter->second;
2064
26.9k
                if (!value.null()) {
2065
23.5k
                    if (key == "/Length") {
2066
                        // omit stream lengths
2067
3.70k
                        if (value.isIndirect()) {
2068
191
                            QTC::TC("qpdf", "QPDF exclude indirect length");
2069
191
                        }
2070
19.8k
                    } else {
2071
19.8k
                        queue.emplace_back(value);
2072
19.8k
                    }
2073
23.5k
                }
2074
26.9k
            }
2075
458k
        } else if (obj.isDictionary()) {
2076
42.5k
            auto dict = obj.as_dictionary();
2077
42.5k
            auto end = dict.crend();
2078
213k
            for (auto iter = dict.crbegin(); iter != end; ++iter) {
2079
171k
                if (!iter->second.null()) {
2080
143k
                    queue.emplace_back(iter->second);
2081
143k
                }
2082
171k
            }
2083
415k
        } else if (auto items = obj.as_array()) {
2084
415k
            queue.insert(queue.end(), items.crbegin(), items.crend());
2085
415k
        }
2086
462k
    }
2087
2088
1.08k
    return result;
2089
1.08k
}
Unexecuted instantiation: std::__1::vector<QPDFObjGen, std::__1::allocator<QPDFObjGen> > QPDF::Doc::Objects::compressible<QPDFObjGen>()
std::__1::vector<bool, std::__1::allocator<bool> > QPDF::Doc::Objects::compressible<bool>()
Line
Count
Source
1998
1.08k
{
1999
    // Return a list of objects that are allowed to be in object streams.  Walk through the objects
2000
    // by traversing the document from the root, including a traversal of the pages tree.  This
2001
    // makes that objects that are on the same page are more likely to be in the same object stream,
2002
    // which is slightly more efficient, particularly with linearized files.  This is better than
2003
    // iterating through the xref table since it avoids preserving orphaned items.
2004
2005
    // Exclude encryption dictionary, if any
2006
1.08k
    QPDFObjectHandle encryption_dict = m->trailer.getKey("/Encrypt");
2007
1.08k
    QPDFObjGen encryption_dict_og = encryption_dict.getObjGen();
2008
2009
1.08k
    const size_t max_obj = qpdf.getObjectCount();
2010
1.08k
    std::vector<bool> visited(max_obj, false);
2011
1.08k
    std::vector<QPDFObjectHandle> queue;
2012
1.08k
    queue.reserve(512);
2013
1.08k
    queue.emplace_back(m->trailer);
2014
1.08k
    std::vector<T> result;
2015
    if constexpr (std::is_same_v<T, QPDFObjGen>) {
2016
        result.reserve(m->obj_cache.size());
2017
1.08k
    } else {
2018
1.08k
        qpdf_static_expect(std::is_same_v<T, bool>);
2019
1.08k
        result.resize(max_obj + 1U, false);
2020
1.08k
    }
2021
497k
    while (!queue.empty()) {
2022
496k
        auto obj = queue.back();
2023
496k
        queue.pop_back();
2024
496k
        if (obj.getObjectID() > 0) {
2025
107k
            QPDFObjGen og = obj.getObjGen();
2026
107k
            const size_t id = toS(og.getObj() - 1);
2027
107k
            if (id >= max_obj) {
2028
0
                throw std::logic_error(
2029
0
                    "unexpected object id encountered in getCompressibleObjGens");
2030
0
            }
2031
107k
            if (visited[id]) {
2032
32.7k
                continue;
2033
32.7k
            }
2034
2035
            // Check whether this is the current object. If not, remove it (which changes it into a
2036
            // direct null and therefore stops us from revisiting it) and move on to the next object
2037
            // in the queue.
2038
74.3k
            auto upper = m->obj_cache.upper_bound(og);
2039
74.3k
            if (upper != m->obj_cache.end() && upper->first.getObj() == og.getObj()) {
2040
882
                qpdf.removeObject(og);
2041
882
                continue;
2042
882
            }
2043
2044
73.4k
            visited[id] = true;
2045
2046
73.4k
            if (og == encryption_dict_og) {
2047
112
                QTC::TC("qpdf", "QPDF exclude encryption dictionary");
2048
73.3k
            } else if (!(obj.isStream() ||
2049
69.2k
                         (obj.isDictionaryOfType("/Sig") && obj.hasKey("/ByteRange") &&
2050
69.2k
                          obj.hasKey("/Contents")))) {
2051
                if constexpr (std::is_same_v<T, QPDFObjGen>) {
2052
                    result.push_back(og);
2053
69.2k
                } else if constexpr (std::is_same_v<T, bool>) {
2054
69.2k
                    result[id + 1U] = true;
2055
69.2k
                }
2056
69.2k
            }
2057
73.4k
        }
2058
462k
        if (obj.isStream()) {
2059
4.09k
            auto dict = obj.getDict().as_dictionary();
2060
4.09k
            auto end = dict.crend();
2061
31.0k
            for (auto iter = dict.crbegin(); iter != end; ++iter) {
2062
26.9k
                std::string const& key = iter->first;
2063
26.9k
                QPDFObjectHandle const& value = iter->second;
2064
26.9k
                if (!value.null()) {
2065
23.5k
                    if (key == "/Length") {
2066
                        // omit stream lengths
2067
3.70k
                        if (value.isIndirect()) {
2068
191
                            QTC::TC("qpdf", "QPDF exclude indirect length");
2069
191
                        }
2070
19.8k
                    } else {
2071
19.8k
                        queue.emplace_back(value);
2072
19.8k
                    }
2073
23.5k
                }
2074
26.9k
            }
2075
458k
        } else if (obj.isDictionary()) {
2076
42.5k
            auto dict = obj.as_dictionary();
2077
42.5k
            auto end = dict.crend();
2078
213k
            for (auto iter = dict.crbegin(); iter != end; ++iter) {
2079
171k
                if (!iter->second.null()) {
2080
143k
                    queue.emplace_back(iter->second);
2081
143k
                }
2082
171k
            }
2083
415k
        } else if (auto items = obj.as_array()) {
2084
415k
            queue.insert(queue.end(), items.crbegin(), items.crend());
2085
415k
        }
2086
462k
    }
2087
2088
1.08k
    return result;
2089
1.08k
}