Coverage Report

Created: 2024-09-08 06:05

/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
    own_memory(own_memory),
10
    description(description),
11
    buf(buf),
12
    cur_offset(0),
13
    max_offset(buf ? QIntC::to_offset(buf->getSize()) : 0)
14
29.7k
{
15
29.7k
}
16
17
BufferInputSource::BufferInputSource(std::string const& description, std::string const& contents) :
18
    own_memory(true),
19
    description(description),
20
    buf(new Buffer(contents.length())),
21
    cur_offset(0),
22
    max_offset(QIntC::to_offset(buf->getSize()))
23
28.8k
{
24
28.8k
    memcpy(buf->getBuffer(), contents.c_str(), contents.length());
25
28.8k
}
26
27
BufferInputSource::~BufferInputSource()
28
58.5k
{
29
58.5k
    if (this->own_memory) {
30
32.5k
        delete this->buf;
31
32.5k
    }
32
58.5k
}
33
34
qpdf_offset_t
35
BufferInputSource::findAndSkipNextEOL()
36
4.83M
{
37
4.83M
    if (this->cur_offset < 0) {
38
0
        throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
39
0
    }
40
4.83M
    qpdf_offset_t end_pos = this->max_offset;
41
4.83M
    if (this->cur_offset >= end_pos) {
42
4.28k
        this->last_offset = end_pos;
43
4.28k
        this->cur_offset = end_pos;
44
4.28k
        return end_pos;
45
4.28k
    }
46
47
4.82M
    qpdf_offset_t result = 0;
48
4.82M
    unsigned char const* buffer = this->buf->getBuffer();
49
4.82M
    unsigned char const* end = buffer + end_pos;
50
4.82M
    unsigned char const* p = buffer + this->cur_offset;
51
52
260M
    while ((p < end) && !((*p == '\r') || (*p == '\n'))) {
53
255M
        ++p;
54
255M
    }
55
4.82M
    if (p < end) {
56
4.82M
        result = p - buffer;
57
4.82M
        this->cur_offset = result + 1;
58
4.82M
        ++p;
59
6.74M
        while ((this->cur_offset < end_pos) && ((*p == '\r') || (*p == '\n'))) {
60
1.92M
            ++p;
61
1.92M
            ++this->cur_offset;
62
1.92M
        }
63
4.82M
    } else {
64
3.26k
        this->cur_offset = end_pos;
65
3.26k
        result = end_pos;
66
3.26k
    }
67
4.82M
    return result;
68
4.83M
}
69
70
std::string const&
71
BufferInputSource::getName() const
72
2.71M
{
73
2.71M
    return this->description;
74
2.71M
}
75
76
qpdf_offset_t
77
BufferInputSource::tell()
78
53.3M
{
79
53.3M
    return this->cur_offset;
80
53.3M
}
81
82
void
83
BufferInputSource::seek(qpdf_offset_t offset, int whence)
84
34.2M
{
85
34.2M
    switch (whence) {
86
34.0M
    case SEEK_SET:
87
34.0M
        this->cur_offset = offset;
88
34.0M
        break;
89
90
15.2k
    case SEEK_END:
91
15.2k
        QIntC::range_check(this->max_offset, offset);
92
15.2k
        this->cur_offset = this->max_offset + offset;
93
15.2k
        break;
94
95
164k
    case SEEK_CUR:
96
164k
        QIntC::range_check(this->cur_offset, offset);
97
164k
        this->cur_offset += offset;
98
164k
        break;
99
100
0
    default:
101
0
        throw std::logic_error("INTERNAL ERROR: invalid argument to BufferInputSource::seek");
102
0
        break;
103
34.2M
    }
104
105
34.2M
    if (this->cur_offset < 0) {
106
97
        throw std::runtime_error(this->description + ": seek before beginning of buffer");
107
97
    }
108
34.2M
}
109
110
void
111
BufferInputSource::rewind()
112
0
{
113
0
    this->cur_offset = 0;
114
0
}
115
116
size_t
117
BufferInputSource::read(char* buffer, size_t length)
118
8.93M
{
119
8.93M
    if (this->cur_offset < 0) {
120
0
        throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
121
0
    }
122
8.93M
    qpdf_offset_t end_pos = this->max_offset;
123
8.93M
    if (this->cur_offset >= end_pos) {
124
82.5k
        this->last_offset = end_pos;
125
82.5k
        return 0;
126
82.5k
    }
127
128
8.84M
    this->last_offset = this->cur_offset;
129
8.84M
    size_t len = std::min(QIntC::to_size(end_pos - this->cur_offset), length);
130
8.84M
    memcpy(buffer, this->buf->getBuffer() + this->cur_offset, len);
131
8.84M
    this->cur_offset += QIntC::to_offset(len);
132
8.84M
    return len;
133
8.93M
}
134
135
void
136
BufferInputSource::unreadCh(char ch)
137
3.88k
{
138
3.88k
    if (this->cur_offset > 0) {
139
3.88k
        --this->cur_offset;
140
3.88k
    }
141
3.88k
}