Coverage Report

Created: 2025-12-05 06:54

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 <qpdf/Util.hh>
4
5
#include <limits>
6
#include <sstream>
7
#include <stdexcept>
8
9
using namespace qpdf;
10
11
OffsetInputSource::OffsetInputSource(
12
    std::shared_ptr<InputSource> proxied, qpdf_offset_t global_offset) :
13
0
    proxied(proxied),
14
0
    global_offset(global_offset)
15
0
{
16
0
    util::assertion(global_offset >= 0, "OffsetInputSource constructed with negative offset");
17
0
    max_safe_offset = std::numeric_limits<qpdf_offset_t>::max() - global_offset;
18
0
}
19
20
qpdf_offset_t
21
OffsetInputSource::findAndSkipNextEOL()
22
0
{
23
0
    return proxied->findAndSkipNextEOL() - global_offset;
24
0
}
25
26
std::string const&
27
OffsetInputSource::getName() const
28
0
{
29
0
    return proxied->getName();
30
0
}
31
32
qpdf_offset_t
33
OffsetInputSource::tell()
34
0
{
35
0
    return proxied->tell() - global_offset;
36
0
}
37
38
void
39
OffsetInputSource::seek(qpdf_offset_t offset, int whence)
40
0
{
41
0
    if (whence == SEEK_SET) {
42
0
        if (offset > max_safe_offset) {
43
0
            std::ostringstream msg;
44
0
            msg.imbue(std::locale::classic());
45
0
            msg << "seeking to " << offset << " offset by " << global_offset
46
0
                << " would cause an overflow of the offset type";
47
0
            throw std::range_error(msg.str());
48
0
        }
49
0
        proxied->seek(offset + global_offset, whence);
50
0
    } else {
51
0
        proxied->seek(offset, whence);
52
0
    }
53
0
    util::no_ci_rt_error_if(tell() < 0, "offset input source: seek before beginning of file");
54
0
}
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
0
{
65
0
    size_t result = proxied->read(buffer, length);
66
0
    setLastOffset(proxied->getLastOffset() - global_offset);
67
0
    return result;
68
0
}
69
70
void
71
OffsetInputSource::unreadCh(char ch)
72
0
{
73
0
    proxied->unreadCh(ch);
74
0
}