Coverage Report

Created: 2025-08-29 06:53

/src/qpdf/libqpdf/OffsetInputSource.cc
Line
Count
Source (jump to first uncovered line)
1
#include <qpdf/OffsetInputSource.hh>
2
3
#include <limits>
4
#include <sstream>
5
#include <stdexcept>
6
7
OffsetInputSource::OffsetInputSource(
8
    std::shared_ptr<InputSource> proxied, qpdf_offset_t global_offset) :
9
844
    proxied(proxied),
10
844
    global_offset(global_offset)
11
844
{
12
844
    if (global_offset < 0) {
13
0
        throw std::logic_error("OffsetInputSource constructed with negative offset");
14
0
    }
15
844
    this->max_safe_offset = std::numeric_limits<qpdf_offset_t>::max() - global_offset;
16
844
}
17
18
qpdf_offset_t
19
OffsetInputSource::findAndSkipNextEOL()
20
141k
{
21
141k
    return this->proxied->findAndSkipNextEOL() - this->global_offset;
22
141k
}
23
24
std::string const&
25
OffsetInputSource::getName() const
26
59.2k
{
27
59.2k
    return this->proxied->getName();
28
59.2k
}
29
30
qpdf_offset_t
31
OffsetInputSource::tell()
32
2.15M
{
33
2.15M
    return this->proxied->tell() - this->global_offset;
34
2.15M
}
35
36
void
37
OffsetInputSource::seek(qpdf_offset_t offset, int whence)
38
919k
{
39
919k
    if (whence == SEEK_SET) {
40
909k
        if (offset > this->max_safe_offset) {
41
2
            std::ostringstream msg;
42
2
            msg.imbue(std::locale::classic());
43
2
            msg << "seeking to " << offset << " offset by " << global_offset
44
2
                << " would cause an overflow of the offset type";
45
2
            throw std::range_error(msg.str());
46
2
        }
47
909k
        this->proxied->seek(offset + global_offset, whence);
48
909k
    } else {
49
9.95k
        this->proxied->seek(offset, whence);
50
9.95k
    }
51
919k
    if (tell() < 0) {
52
15
        throw std::runtime_error("offset input source: seek before beginning of file");
53
15
    }
54
919k
}
55
56
void
57
OffsetInputSource::rewind()
58
0
{
59
0
    seek(0, SEEK_SET);
60
0
}
61
62
size_t
63
OffsetInputSource::read(char* buffer, size_t length)
64
335k
{
65
335k
    size_t result = this->proxied->read(buffer, length);
66
335k
    this->setLastOffset(this->proxied->getLastOffset() - global_offset);
67
335k
    return result;
68
335k
}
69
70
void
71
OffsetInputSource::unreadCh(char ch)
72
513
{
73
513
    this->proxied->unreadCh(ch);
74
513
}