Coverage Report

Created: 2025-07-14 06:14

/src/qpdf/libqpdf/BufferInputSource.cc
Line
Count
Source (jump to first uncovered line)
1
#include <qpdf/BufferInputSource.hh>
2
3
#include <qpdf/QIntC.hh>
4
#include <algorithm>
5
#include <cstring>
6
#include <sstream>
7
8
BufferInputSource::BufferInputSource(std::string const& description, Buffer* buf, bool own_memory) :
9
26.3k
    own_memory(own_memory),
10
26.3k
    description(description),
11
26.3k
    buf(buf),
12
26.3k
    cur_offset(0),
13
26.3k
    max_offset(buf ? QIntC::to_offset(buf->getSize()) : 0)
14
26.3k
{
15
26.3k
}
16
17
BufferInputSource::BufferInputSource(std::string const& description, std::string const& contents) :
18
7.37k
    own_memory(true),
19
7.37k
    description(description),
20
7.37k
    buf(new Buffer(contents.length())),
21
7.37k
    cur_offset(0),
22
7.37k
    max_offset(QIntC::to_offset(buf->getSize()))
23
7.37k
{
24
7.37k
    memcpy(buf->getBuffer(), contents.c_str(), contents.length());
25
7.37k
}
26
27
BufferInputSource::~BufferInputSource()
28
33.7k
{
29
33.7k
    if (own_memory) {
30
14.7k
        delete buf;
31
14.7k
    }
32
33.7k
}
33
34
qpdf_offset_t
35
BufferInputSource::findAndSkipNextEOL()
36
0
{
37
0
    if (cur_offset < 0) {
38
0
        throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
39
0
    }
40
0
    qpdf_offset_t end_pos = max_offset;
41
0
    if (cur_offset >= end_pos) {
42
0
        last_offset = end_pos;
43
0
        cur_offset = end_pos;
44
0
        return end_pos;
45
0
    }
46
47
0
    qpdf_offset_t result = 0;
48
0
    unsigned char const* buffer = buf->getBuffer();
49
0
    unsigned char const* end = buffer + end_pos;
50
0
    unsigned char const* p = buffer + cur_offset;
51
52
0
    while ((p < end) && !((*p == '\r') || (*p == '\n'))) {
53
0
        ++p;
54
0
    }
55
0
    if (p < end) {
56
0
        result = p - buffer;
57
0
        cur_offset = result + 1;
58
0
        ++p;
59
0
        while ((cur_offset < end_pos) && ((*p == '\r') || (*p == '\n'))) {
60
0
            ++p;
61
0
            ++cur_offset;
62
0
        }
63
0
    } else {
64
0
        cur_offset = end_pos;
65
0
        result = end_pos;
66
0
    }
67
0
    return result;
68
0
}
69
70
std::string const&
71
BufferInputSource::getName() const
72
294k
{
73
294k
    return description;
74
294k
}
75
76
qpdf_offset_t
77
BufferInputSource::tell()
78
168k
{
79
168k
    return cur_offset;
80
168k
}
81
82
void
83
BufferInputSource::seek(qpdf_offset_t offset, int whence)
84
194k
{
85
194k
    switch (whence) {
86
186k
    case SEEK_SET:
87
186k
        cur_offset = offset;
88
186k
        break;
89
90
7.37k
    case SEEK_END:
91
7.37k
        QIntC::range_check(max_offset, offset);
92
7.37k
        cur_offset = max_offset + offset;
93
7.37k
        break;
94
95
0
    case SEEK_CUR:
96
0
        QIntC::range_check(cur_offset, offset);
97
0
        cur_offset += offset;
98
0
        break;
99
100
0
    default:
101
0
        throw std::logic_error("INTERNAL ERROR: invalid argument to BufferInputSource::seek");
102
0
        break;
103
194k
    }
104
105
194k
    if (cur_offset < 0) {
106
0
        throw std::runtime_error(description + ": seek before beginning of buffer");
107
0
    }
108
194k
}
109
110
void
111
BufferInputSource::rewind()
112
0
{
113
0
    cur_offset = 0;
114
0
}
115
116
size_t
117
BufferInputSource::read(char* buffer, size_t length)
118
154k
{
119
154k
    if (cur_offset < 0) {
120
0
        throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
121
0
    }
122
154k
    qpdf_offset_t end_pos = max_offset;
123
154k
    if (cur_offset >= end_pos) {
124
19.6k
        last_offset = end_pos;
125
19.6k
        return 0;
126
19.6k
    }
127
128
134k
    last_offset = cur_offset;
129
134k
    size_t len = std::min(QIntC::to_size(end_pos - cur_offset), length);
130
134k
    memcpy(buffer, buf->getBuffer() + cur_offset, len);
131
134k
    cur_offset += QIntC::to_offset(len);
132
134k
    return len;
133
154k
}
134
135
void
136
BufferInputSource::unreadCh(char ch)
137
7.37k
{
138
7.37k
    if (cur_offset > 0) {
139
7.37k
        --cur_offset;
140
7.37k
    }
141
7.37k
}