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
23.2k
{
15
23.2k
}
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
22.3k
{
24
22.3k
    memcpy(buf->getBuffer(), contents.c_str(), contents.length());
25
22.3k
}
26
27
BufferInputSource::~BufferInputSource()
28
45.6k
{
29
45.6k
    if (this->own_memory) {
30
25.3k
        delete this->buf;
31
25.3k
    }
32
45.6k
}
33
34
qpdf_offset_t
35
BufferInputSource::findAndSkipNextEOL()
36
3.57M
{
37
3.57M
    if (this->cur_offset < 0) {
38
0
        throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
39
0
    }
40
3.57M
    qpdf_offset_t end_pos = this->max_offset;
41
3.57M
    if (this->cur_offset >= end_pos) {
42
3.29k
        this->last_offset = end_pos;
43
3.29k
        this->cur_offset = end_pos;
44
3.29k
        return end_pos;
45
3.29k
    }
46
47
3.57M
    qpdf_offset_t result = 0;
48
3.57M
    unsigned char const* buffer = this->buf->getBuffer();
49
3.57M
    unsigned char const* end = buffer + end_pos;
50
3.57M
    unsigned char const* p = buffer + this->cur_offset;
51
52
95.7M
    while ((p < end) && !((*p == '\r') || (*p == '\n'))) {
53
92.2M
        ++p;
54
92.2M
    }
55
3.57M
    if (p < end) {
56
3.57M
        result = p - buffer;
57
3.57M
        this->cur_offset = result + 1;
58
3.57M
        ++p;
59
4.91M
        while ((this->cur_offset < end_pos) && ((*p == '\r') || (*p == '\n'))) {
60
1.34M
            ++p;
61
1.34M
            ++this->cur_offset;
62
1.34M
        }
63
3.57M
    } else {
64
2.50k
        this->cur_offset = end_pos;
65
2.50k
        result = end_pos;
66
2.50k
    }
67
3.57M
    return result;
68
3.57M
}
69
70
std::string const&
71
BufferInputSource::getName() const
72
1.28M
{
73
1.28M
    return this->description;
74
1.28M
}
75
76
qpdf_offset_t
77
BufferInputSource::tell()
78
35.3M
{
79
35.3M
    return this->cur_offset;
80
35.3M
}
81
82
void
83
BufferInputSource::seek(qpdf_offset_t offset, int whence)
84
23.3M
{
85
23.3M
    switch (whence) {
86
23.1M
    case SEEK_SET:
87
23.1M
        this->cur_offset = offset;
88
23.1M
        break;
89
90
11.7k
    case SEEK_END:
91
11.7k
        QIntC::range_check(this->max_offset, offset);
92
11.7k
        this->cur_offset = this->max_offset + offset;
93
11.7k
        break;
94
95
116k
    case SEEK_CUR:
96
116k
        QIntC::range_check(this->cur_offset, offset);
97
116k
        this->cur_offset += offset;
98
116k
        break;
99
100
0
    default:
101
0
        throw std::logic_error("INTERNAL ERROR: invalid argument to BufferInputSource::seek");
102
0
        break;
103
23.3M
    }
104
105
23.3M
    if (this->cur_offset < 0) {
106
31
        throw std::runtime_error(this->description + ": seek before beginning of buffer");
107
31
    }
108
23.3M
}
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
10.4M
{
119
10.4M
    if (this->cur_offset < 0) {
120
0
        throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
121
0
    }
122
10.4M
    qpdf_offset_t end_pos = this->max_offset;
123
10.4M
    if (this->cur_offset >= end_pos) {
124
72.1k
        this->last_offset = end_pos;
125
72.1k
        return 0;
126
72.1k
    }
127
128
10.3M
    this->last_offset = this->cur_offset;
129
10.3M
    size_t len = std::min(QIntC::to_size(end_pos - this->cur_offset), length);
130
10.3M
    memcpy(buffer, this->buf->getBuffer() + this->cur_offset, len);
131
10.3M
    this->cur_offset += QIntC::to_offset(len);
132
10.3M
    return len;
133
10.4M
}
134
135
void
136
BufferInputSource::unreadCh(char ch)
137
2.85k
{
138
2.85k
    if (this->cur_offset > 0) {
139
2.85k
        --this->cur_offset;
140
2.85k
    }
141
2.85k
}