Coverage Report

Created: 2024-09-08 06:06

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