Coverage Report

Created: 2025-08-03 06:16

/src/qpdf/libqpdf/BufferInputSource.cc
Line
Count
Source (jump to first uncovered line)
1
#include <qpdf/BufferInputSource.hh>
2
#include <qpdf/InputSource_private.hh>
3
4
#include <qpdf/Buffer.hh>
5
#include <qpdf/QIntC.hh>
6
7
#include <algorithm>
8
#include <cstring>
9
#include <sstream>
10
11
using namespace qpdf;
12
13
#ifndef QPDF_FUTURE
14
15
BufferInputSource::BufferInputSource(std::string const& description, Buffer* buf, bool own_memory) :
16
22.3k
    own_memory(own_memory),
17
22.3k
    description(description),
18
22.3k
    buf(buf),
19
22.3k
    cur_offset(0),
20
22.3k
    max_offset(buf ? QIntC::to_offset(buf->getSize()) : 0)
21
22.3k
{
22
22.3k
}
23
24
BufferInputSource::BufferInputSource(std::string const& description, std::string const& contents) :
25
0
    own_memory(true),
26
0
    description(description),
27
0
    buf(new Buffer(contents.length())),
28
0
    cur_offset(0),
29
0
    max_offset(QIntC::to_offset(buf->getSize()))
30
0
{
31
0
    memcpy(buf->getBuffer(), contents.c_str(), contents.length());
32
0
}
33
34
BufferInputSource::~BufferInputSource()
35
22.3k
{
36
22.3k
    if (own_memory) {
37
0
        delete buf;
38
0
    }
39
22.3k
}
40
41
qpdf_offset_t
42
BufferInputSource::findAndSkipNextEOL()
43
1.52M
{
44
1.52M
    if (cur_offset < 0) {
45
0
        throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
46
0
    }
47
1.52M
    qpdf_offset_t end_pos = max_offset;
48
1.52M
    if (cur_offset >= end_pos) {
49
7.53k
        last_offset = end_pos;
50
7.53k
        cur_offset = end_pos;
51
7.53k
        return end_pos;
52
7.53k
    }
53
54
1.52M
    qpdf_offset_t result = 0;
55
1.52M
    unsigned char const* buffer = buf->getBuffer();
56
1.52M
    unsigned char const* end = buffer + end_pos;
57
1.52M
    unsigned char const* p = buffer + cur_offset;
58
59
170M
    while ((p < end) && !((*p == '\r') || (*p == '\n'))) {
60
169M
        ++p;
61
169M
    }
62
1.52M
    if (p < end) {
63
1.50M
        result = p - buffer;
64
1.50M
        cur_offset = result + 1;
65
1.50M
        ++p;
66
1.73M
        while ((cur_offset < end_pos) && ((*p == '\r') || (*p == '\n'))) {
67
226k
            ++p;
68
226k
            ++cur_offset;
69
226k
        }
70
1.50M
    } else {
71
13.3k
        cur_offset = end_pos;
72
13.3k
        result = end_pos;
73
13.3k
    }
74
1.52M
    return result;
75
1.52M
}
76
77
std::string const&
78
BufferInputSource::getName() const
79
901k
{
80
901k
    return description;
81
901k
}
82
83
qpdf_offset_t
84
BufferInputSource::tell()
85
15.2M
{
86
15.2M
    return cur_offset;
87
15.2M
}
88
89
void
90
BufferInputSource::seek(qpdf_offset_t offset, int whence)
91
9.82M
{
92
9.82M
    switch (whence) {
93
9.63M
    case SEEK_SET:
94
9.63M
        cur_offset = offset;
95
9.63M
        break;
96
97
43.6k
    case SEEK_END:
98
43.6k
        QIntC::range_check(max_offset, offset);
99
43.6k
        cur_offset = max_offset + offset;
100
43.6k
        break;
101
102
149k
    case SEEK_CUR:
103
149k
        QIntC::range_check(cur_offset, offset);
104
149k
        cur_offset += offset;
105
149k
        break;
106
107
0
    default:
108
0
        throw std::logic_error("INTERNAL ERROR: invalid argument to BufferInputSource::seek");
109
0
        break;
110
9.82M
    }
111
112
9.82M
    if (cur_offset < 0) {
113
358
        throw std::runtime_error(description + ": seek before beginning of buffer");
114
358
    }
115
9.82M
}
116
117
void
118
BufferInputSource::rewind()
119
0
{
120
0
    cur_offset = 0;
121
0
}
122
123
size_t
124
BufferInputSource::read(char* buffer, size_t length)
125
1.94M
{
126
1.94M
    if (cur_offset < 0) {
127
0
        throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
128
0
    }
129
1.94M
    qpdf_offset_t end_pos = max_offset;
130
1.94M
    if (cur_offset >= end_pos) {
131
81.6k
        last_offset = end_pos;
132
81.6k
        return 0;
133
81.6k
    }
134
135
1.85M
    last_offset = cur_offset;
136
1.85M
    size_t len = std::min(QIntC::to_size(end_pos - cur_offset), length);
137
1.85M
    memcpy(buffer, buf->getBuffer() + cur_offset, len);
138
1.85M
    cur_offset += QIntC::to_offset(len);
139
1.85M
    return len;
140
1.94M
}
141
142
void
143
BufferInputSource::unreadCh(char ch)
144
14.7k
{
145
14.7k
    if (cur_offset > 0) {
146
14.7k
        --cur_offset;
147
14.7k
    }
148
14.7k
}
149
150
#else
151
152
class BufferInputSource::Members
153
{
154
  public:
155
    Members(std::string const& description, Buffer* buf, bool own_memory) :
156
        buf(own_memory ? buf : nullptr),
157
        is(description,
158
           buf && buf->getSize() > 0
159
               ? std::string_view(reinterpret_cast<const char*>(buf->getBuffer()), buf->getSize())
160
               : std::string_view())
161
    {
162
    }
163
164
    Members(std::string const& description, std::string const& str) :
165
        content(str),
166
        is(description, content)
167
    {
168
    }
169
170
    ~Members() = default;
171
172
    std::unique_ptr<Buffer> buf{nullptr};
173
    std::string content;
174
    is::OffsetBuffer is;
175
};
176
177
BufferInputSource::BufferInputSource(std::string const& description, Buffer* buf, bool own_memory) :
178
    m(std::make_unique<Members>(description, buf, own_memory))
179
{
180
}
181
182
BufferInputSource::BufferInputSource(std::string const& description, std::string const& contents) :
183
    m(std::make_unique<Members>(description, contents))
184
{
185
}
186
BufferInputSource::~BufferInputSource() = default;
187
188
qpdf_offset_t
189
BufferInputSource::findAndSkipNextEOL()
190
{
191
    auto result = m->is.findAndSkipNextEOL();
192
    last_offset = m->is.getLastOffset();
193
    return result;
194
}
195
std::string const&
196
BufferInputSource::getName() const
197
{
198
    return m->is.getName();
199
}
200
qpdf_offset_t
201
BufferInputSource::tell()
202
{
203
    return m->is.tell();
204
}
205
void
206
BufferInputSource::seek(qpdf_offset_t offset, int whence)
207
{
208
    m->is.seek(offset, whence);
209
}
210
void
211
BufferInputSource::rewind()
212
{
213
    m->is.rewind();
214
}
215
size_t
216
BufferInputSource::read(char* buffer, size_t length)
217
{
218
    auto result = m->is.read(buffer, length);
219
    last_offset = m->is.getLastOffset();
220
    return result;
221
}
222
void
223
BufferInputSource::unreadCh(char ch)
224
{
225
    m->is.unreadCh(ch);
226
}
227
228
#endif // QPDF_FUTURE
229
230
qpdf_offset_t
231
is::OffsetBuffer::findAndSkipNextEOL()
232
0
{
233
0
    if (pos < 0) {
234
0
        throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0");
235
0
    }
236
0
    auto end_pos = static_cast<qpdf_offset_t>(view_.size());
237
0
    if (pos >= end_pos) {
238
0
        last_offset = end_pos + global_offset;
239
0
        pos = end_pos;
240
0
        return end_pos + global_offset;
241
0
    }
242
243
0
    qpdf_offset_t result = 0;
244
0
    auto buffer = view_.begin();
245
0
    auto end = view_.end();
246
0
    auto p = buffer + static_cast<std::ptrdiff_t>(pos);
247
248
0
    while (p < end && !(*p == '\r' || *p == '\n')) {
249
0
        ++p;
250
0
    }
251
0
    if (p < end) {
252
0
        result = p - buffer;
253
0
        pos = result + 1;
254
0
        ++p;
255
0
        while (pos < end_pos && (*p == '\r' || *p == '\n')) {
256
0
            ++p;
257
0
            ++pos;
258
0
        }
259
0
    } else {
260
0
        pos = end_pos;
261
0
        result = end_pos;
262
0
    }
263
0
    return result + global_offset;
264
0
}
265
266
void
267
is::OffsetBuffer::seek(qpdf_offset_t offset, int whence)
268
99.6M
{
269
99.6M
    switch (whence) {
270
99.6M
    case SEEK_SET:
271
99.6M
        pos = offset - global_offset;
272
99.6M
        break;
273
274
0
    case SEEK_END:
275
0
        QIntC::range_check(static_cast<qpdf_offset_t>(view_.size()), offset);
276
0
        pos = static_cast<qpdf_offset_t>(view_.size()) + offset;
277
0
        break;
278
279
0
    case SEEK_CUR:
280
0
        QIntC::range_check(pos, offset);
281
0
        pos += offset;
282
0
        break;
283
284
0
    default:
285
0
        throw std::logic_error("INTERNAL ERROR: invalid argument to BufferInputSource::seek");
286
0
        break;
287
99.6M
    }
288
289
99.6M
    if (pos < 0) {
290
0
        throw std::runtime_error(description + ": seek before beginning of buffer");
291
0
    }
292
99.6M
}
293
294
size_t
295
is::OffsetBuffer::read(char* buffer, size_t length)
296
10.9M
{
297
10.9M
    if (pos < 0) {
298
0
        throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0");
299
0
    }
300
10.9M
    auto end_pos = static_cast<qpdf_offset_t>(view_.size());
301
10.9M
    if (pos >= end_pos) {
302
26.1k
        last_offset = end_pos + global_offset;
303
26.1k
        return 0;
304
26.1k
    }
305
306
10.8M
    last_offset = pos + global_offset;
307
10.8M
    size_t len = std::min(QIntC::to_size(end_pos - pos), length);
308
10.8M
    memcpy(buffer, view_.data() + pos, len);
309
10.8M
    pos += QIntC::to_offset(len);
310
10.8M
    return len;
311
10.9M
}