Coverage Report

Created: 2025-10-10 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/OffsetInputSource.cc
Line
Count
Source
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
2.79k
    proxied(proxied),
10
2.79k
    global_offset(global_offset)
11
2.79k
{
12
2.79k
    if (global_offset < 0) {
13
0
        throw std::logic_error("OffsetInputSource constructed with negative offset");
14
0
    }
15
2.79k
    this->max_safe_offset = std::numeric_limits<qpdf_offset_t>::max() - global_offset;
16
2.79k
}
17
18
qpdf_offset_t
19
OffsetInputSource::findAndSkipNextEOL()
20
1.20M
{
21
1.20M
    return this->proxied->findAndSkipNextEOL() - this->global_offset;
22
1.20M
}
23
24
std::string const&
25
OffsetInputSource::getName() const
26
188k
{
27
188k
    return this->proxied->getName();
28
188k
}
29
30
qpdf_offset_t
31
OffsetInputSource::tell()
32
9.21M
{
33
9.21M
    return this->proxied->tell() - this->global_offset;
34
9.21M
}
35
36
void
37
OffsetInputSource::seek(qpdf_offset_t offset, int whence)
38
3.37M
{
39
3.37M
    if (whence == SEEK_SET) {
40
3.32M
        if (offset > this->max_safe_offset) {
41
1
            std::ostringstream msg;
42
1
            msg.imbue(std::locale::classic());
43
1
            msg << "seeking to " << offset << " offset by " << global_offset
44
1
                << " would cause an overflow of the offset type";
45
1
            throw std::range_error(msg.str());
46
1
        }
47
3.32M
        this->proxied->seek(offset + global_offset, whence);
48
3.32M
    } else {
49
46.5k
        this->proxied->seek(offset, whence);
50
46.5k
    }
51
3.37M
    if (tell() < 0) {
52
16
        throw std::runtime_error("offset input source: seek before beginning of file");
53
16
    }
54
3.37M
}
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
661k
{
65
661k
    size_t result = this->proxied->read(buffer, length);
66
661k
    this->setLastOffset(this->proxied->getLastOffset() - global_offset);
67
661k
    return result;
68
661k
}
69
70
void
71
OffsetInputSource::unreadCh(char ch)
72
1.26k
{
73
1.26k
    this->proxied->unreadCh(ch);
74
1.26k
}