Coverage Report

Created: 2025-11-09 06:16

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