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