Coverage Report

Created: 2026-06-09 07:01

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