Coverage Report

Created: 2026-01-09 06:28

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