Coverage Report

Created: 2025-07-14 06:20

/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
93.3k
    own_memory(own_memory),
10
93.3k
    description(description),
11
93.3k
    buf(buf),
12
93.3k
    cur_offset(0),
13
93.3k
    max_offset(buf ? QIntC::to_offset(buf->getSize()) : 0)
14
93.3k
{
15
93.3k
}
16
17
BufferInputSource::BufferInputSource(std::string const& description, std::string const& contents) :
18
0
    own_memory(true),
19
0
    description(description),
20
0
    buf(new Buffer(contents.length())),
21
0
    cur_offset(0),
22
0
    max_offset(QIntC::to_offset(buf->getSize()))
23
0
{
24
0
    memcpy(buf->getBuffer(), contents.c_str(), contents.length());
25
0
}
26
27
BufferInputSource::~BufferInputSource()
28
93.3k
{
29
93.3k
    if (own_memory) {
30
0
        delete buf;
31
0
    }
32
93.3k
}
33
34
qpdf_offset_t
35
BufferInputSource::findAndSkipNextEOL()
36
2.31M
{
37
2.31M
    if (cur_offset < 0) {
38
0
        throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
39
0
    }
40
2.31M
    qpdf_offset_t end_pos = max_offset;
41
2.31M
    if (cur_offset >= end_pos) {
42
6.34k
        last_offset = end_pos;
43
6.34k
        cur_offset = end_pos;
44
6.34k
        return end_pos;
45
6.34k
    }
46
47
2.30M
    qpdf_offset_t result = 0;
48
2.30M
    unsigned char const* buffer = buf->getBuffer();
49
2.30M
    unsigned char const* end = buffer + end_pos;
50
2.30M
    unsigned char const* p = buffer + cur_offset;
51
52
144M
    while ((p < end) && !((*p == '\r') || (*p == '\n'))) {
53
142M
        ++p;
54
142M
    }
55
2.30M
    if (p < end) {
56
2.29M
        result = p - buffer;
57
2.29M
        cur_offset = result + 1;
58
2.29M
        ++p;
59
2.82M
        while ((cur_offset < end_pos) && ((*p == '\r') || (*p == '\n'))) {
60
525k
            ++p;
61
525k
            ++cur_offset;
62
525k
        }
63
2.29M
    } else {
64
11.9k
        cur_offset = end_pos;
65
11.9k
        result = end_pos;
66
11.9k
    }
67
2.30M
    return result;
68
2.31M
}
69
70
std::string const&
71
BufferInputSource::getName() const
72
811k
{
73
811k
    return description;
74
811k
}
75
76
qpdf_offset_t
77
BufferInputSource::tell()
78
20.2M
{
79
20.2M
    return cur_offset;
80
20.2M
}
81
82
void
83
BufferInputSource::seek(qpdf_offset_t offset, int whence)
84
12.0M
{
85
12.0M
    switch (whence) {
86
11.8M
    case SEEK_SET:
87
11.8M
        cur_offset = offset;
88
11.8M
        break;
89
90
37.4k
    case SEEK_END:
91
37.4k
        QIntC::range_check(max_offset, offset);
92
37.4k
        cur_offset = max_offset + offset;
93
37.4k
        break;
94
95
149k
    case SEEK_CUR:
96
149k
        QIntC::range_check(cur_offset, offset);
97
149k
        cur_offset += offset;
98
149k
        break;
99
100
0
    default:
101
0
        throw std::logic_error("INTERNAL ERROR: invalid argument to BufferInputSource::seek");
102
0
        break;
103
12.0M
    }
104
105
12.0M
    if (cur_offset < 0) {
106
251
        throw std::runtime_error(description + ": seek before beginning of buffer");
107
251
    }
108
12.0M
}
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
2.09M
{
119
2.09M
    if (cur_offset < 0) {
120
0
        throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
121
0
    }
122
2.09M
    qpdf_offset_t end_pos = max_offset;
123
2.09M
    if (cur_offset >= end_pos) {
124
81.1k
        last_offset = end_pos;
125
81.1k
        return 0;
126
81.1k
    }
127
128
2.01M
    last_offset = cur_offset;
129
2.01M
    size_t len = std::min(QIntC::to_size(end_pos - cur_offset), length);
130
2.01M
    memcpy(buffer, buf->getBuffer() + cur_offset, len);
131
2.01M
    cur_offset += QIntC::to_offset(len);
132
2.01M
    return len;
133
2.09M
}
134
135
void
136
BufferInputSource::unreadCh(char ch)
137
13.5k
{
138
13.5k
    if (cur_offset > 0) {
139
13.5k
        --cur_offset;
140
13.5k
    }
141
13.5k
}