Coverage Report

Created: 2025-10-12 07:06

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
1.21k
    proxied(proxied),
10
1.21k
    global_offset(global_offset)
11
1.21k
{
12
1.21k
    if (global_offset < 0) {
13
0
        throw std::logic_error("OffsetInputSource constructed with negative offset");
14
0
    }
15
1.21k
    this->max_safe_offset = std::numeric_limits<qpdf_offset_t>::max() - global_offset;
16
1.21k
}
17
18
qpdf_offset_t
19
OffsetInputSource::findAndSkipNextEOL()
20
205k
{
21
205k
    return this->proxied->findAndSkipNextEOL() - this->global_offset;
22
205k
}
23
24
std::string const&
25
OffsetInputSource::getName() const
26
93.0k
{
27
93.0k
    return this->proxied->getName();
28
93.0k
}
29
30
qpdf_offset_t
31
OffsetInputSource::tell()
32
3.77M
{
33
3.77M
    return this->proxied->tell() - this->global_offset;
34
3.77M
}
35
36
void
37
OffsetInputSource::seek(qpdf_offset_t offset, int whence)
38
1.67M
{
39
1.67M
    if (whence == SEEK_SET) {
40
1.65M
        if (offset > this->max_safe_offset) {
41
6
            std::ostringstream msg;
42
6
            msg.imbue(std::locale::classic());
43
6
            msg << "seeking to " << offset << " offset by " << global_offset
44
6
                << " would cause an overflow of the offset type";
45
6
            throw std::range_error(msg.str());
46
6
        }
47
1.65M
        this->proxied->seek(offset + global_offset, whence);
48
1.65M
    } else {
49
23.5k
        this->proxied->seek(offset, whence);
50
23.5k
    }
51
1.67M
    if (tell() < 0) {
52
10
        throw std::runtime_error("offset input source: seek before beginning of file");
53
10
    }
54
1.67M
}
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
742k
{
65
742k
    size_t result = this->proxied->read(buffer, length);
66
742k
    this->setLastOffset(this->proxied->getLastOffset() - global_offset);
67
742k
    return result;
68
742k
}
69
70
void
71
OffsetInputSource::unreadCh(char ch)
72
990
{
73
990
    this->proxied->unreadCh(ch);
74
990
}