/src/qpdf/libqpdf/BufferInputSource.cc
Line | Count | Source |
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 | 146k | own_memory(own_memory), |
17 | 146k | description(description), |
18 | 146k | buf(buf), |
19 | 146k | cur_offset(0), |
20 | 146k | max_offset(buf ? QIntC::to_offset(buf->getSize()) : 0) |
21 | 146k | { |
22 | 146k | } |
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 | 146k | { |
36 | 146k | if (own_memory) { |
37 | 0 | delete buf; |
38 | 0 | } |
39 | 146k | } |
40 | | |
41 | | qpdf_offset_t |
42 | | BufferInputSource::findAndSkipNextEOL() |
43 | 10.1M | { |
44 | 10.1M | if (cur_offset < 0) { |
45 | 0 | throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0"); |
46 | 0 | } |
47 | 10.1M | qpdf_offset_t end_pos = max_offset; |
48 | 10.1M | if (cur_offset >= end_pos) { |
49 | 44.1k | last_offset = end_pos; |
50 | 44.1k | cur_offset = end_pos; |
51 | 44.1k | return end_pos; |
52 | 44.1k | } |
53 | | |
54 | 10.1M | qpdf_offset_t result = 0; |
55 | 10.1M | unsigned char const* buffer = buf->getBuffer(); |
56 | 10.1M | unsigned char const* end = buffer + end_pos; |
57 | 10.1M | unsigned char const* p = buffer + cur_offset; |
58 | | |
59 | 974M | while ((p < end) && !((*p == '\r') || (*p == '\n'))) { |
60 | 964M | ++p; |
61 | 964M | } |
62 | 10.1M | if (p < end) { |
63 | 10.0M | result = p - buffer; |
64 | 10.0M | cur_offset = result + 1; |
65 | 10.0M | ++p; |
66 | 11.2M | while ((cur_offset < end_pos) && ((*p == '\r') || (*p == '\n'))) { |
67 | 1.17M | ++p; |
68 | 1.17M | ++cur_offset; |
69 | 1.17M | } |
70 | 10.0M | } else { |
71 | 89.7k | cur_offset = end_pos; |
72 | 89.7k | result = end_pos; |
73 | 89.7k | } |
74 | 10.1M | return result; |
75 | 10.1M | } |
76 | | |
77 | | std::string const& |
78 | | BufferInputSource::getName() const |
79 | 11.1M | { |
80 | 11.1M | return description; |
81 | 11.1M | } |
82 | | |
83 | | qpdf_offset_t |
84 | | BufferInputSource::tell() |
85 | 206M | { |
86 | 206M | return cur_offset; |
87 | 206M | } |
88 | | |
89 | | void |
90 | | BufferInputSource::seek(qpdf_offset_t offset, int whence) |
91 | 67.1M | { |
92 | 67.1M | switch (whence) { |
93 | 66.1M | case SEEK_SET: |
94 | 66.1M | cur_offset = offset; |
95 | 66.1M | break; |
96 | | |
97 | 273k | case SEEK_END: |
98 | 273k | QIntC::range_check(max_offset, offset); |
99 | 273k | cur_offset = max_offset + offset; |
100 | 273k | break; |
101 | | |
102 | 716k | case SEEK_CUR: |
103 | 716k | QIntC::range_check(cur_offset, offset); |
104 | 716k | cur_offset += offset; |
105 | 716k | break; |
106 | | |
107 | 0 | default: |
108 | 0 | throw std::logic_error("INTERNAL ERROR: invalid argument to BufferInputSource::seek"); |
109 | 0 | break; |
110 | 67.1M | } |
111 | | |
112 | 67.1M | if (cur_offset < 0) { |
113 | 2.65k | throw std::runtime_error(description + ": seek before beginning of buffer"); |
114 | 2.65k | } |
115 | 67.1M | } |
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 | 13.7M | { |
126 | 13.7M | if (cur_offset < 0) { |
127 | 0 | throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0"); |
128 | 0 | } |
129 | 13.7M | qpdf_offset_t end_pos = max_offset; |
130 | 13.7M | if (cur_offset >= end_pos) { |
131 | 500k | last_offset = end_pos; |
132 | 500k | return 0; |
133 | 500k | } |
134 | | |
135 | 13.2M | last_offset = cur_offset; |
136 | 13.2M | size_t len = std::min(QIntC::to_size(end_pos - cur_offset), length); |
137 | 13.2M | memcpy(buffer, buf->getBuffer() + cur_offset, len); |
138 | 13.2M | cur_offset += QIntC::to_offset(len); |
139 | 13.2M | return len; |
140 | 13.7M | } |
141 | | |
142 | | void |
143 | | BufferInputSource::unreadCh(char ch) |
144 | 106k | { |
145 | 106k | if (cur_offset > 0) { |
146 | 106k | --cur_offset; |
147 | 106k | } |
148 | 106k | } |
149 | | |
150 | | #else |
151 | | |
152 | | class BufferInputSource::Members |
153 | | { |
154 | | public: |
155 | | Members(std::string const& description, Buffer* buf, bool own_memory) : |
156 | 143k | buf(own_memory ? buf : nullptr), |
157 | 143k | is(description, |
158 | 143k | buf && buf->getSize() > 0 |
159 | 143k | ? std::string_view(reinterpret_cast<const char*>(buf->getBuffer()), buf->getSize()) |
160 | 143k | : std::string_view()) |
161 | 143k | { |
162 | 143k | } |
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 | 143k | ~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 | 9.97M | { |
233 | 9.97M | if (pos < 0) { |
234 | 0 | throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0"); |
235 | 0 | } |
236 | 9.97M | auto end_pos = static_cast<qpdf_offset_t>(view_.size()); |
237 | 9.97M | if (pos >= end_pos) { |
238 | 42.6k | last_offset = end_pos + global_offset; |
239 | 42.6k | pos = end_pos; |
240 | 42.6k | return end_pos + global_offset; |
241 | 42.6k | } |
242 | | |
243 | 9.92M | qpdf_offset_t result = 0; |
244 | 9.92M | auto buffer = view_.begin(); |
245 | 9.92M | auto end = view_.end(); |
246 | 9.92M | auto p = buffer + static_cast<std::ptrdiff_t>(pos); |
247 | | |
248 | 896M | while (p < end && !(*p == '\r' || *p == '\n')) { |
249 | 886M | ++p; |
250 | 886M | } |
251 | 9.92M | if (p < end) { |
252 | 9.84M | result = p - buffer; |
253 | 9.84M | pos = result + 1; |
254 | 9.84M | ++p; |
255 | 12.3M | while (pos < end_pos && (*p == '\r' || *p == '\n')) { |
256 | 2.52M | ++p; |
257 | 2.52M | ++pos; |
258 | 2.52M | } |
259 | 9.84M | } else { |
260 | 87.3k | pos = end_pos; |
261 | 87.3k | result = end_pos; |
262 | 87.3k | } |
263 | 9.92M | return result + global_offset; |
264 | 9.97M | } |
265 | | |
266 | | void |
267 | | is::OffsetBuffer::seek(qpdf_offset_t offset, int whence) |
268 | 221M | { |
269 | 221M | switch (whence) { |
270 | 220M | case SEEK_SET: |
271 | 220M | pos = offset - global_offset; |
272 | 220M | break; |
273 | | |
274 | 282k | case SEEK_END: |
275 | 282k | QIntC::range_check(static_cast<qpdf_offset_t>(view_.size()), offset); |
276 | 282k | pos = static_cast<qpdf_offset_t>(view_.size()) + offset; |
277 | 282k | break; |
278 | | |
279 | 691k | case SEEK_CUR: |
280 | 691k | QIntC::range_check(pos, offset); |
281 | 691k | pos += offset; |
282 | 691k | break; |
283 | | |
284 | 0 | default: |
285 | 0 | throw std::logic_error("INTERNAL ERROR: invalid argument to BufferInputSource::seek"); |
286 | 0 | break; |
287 | 221M | } |
288 | | |
289 | 221M | if (pos < 0) { |
290 | 2.00k | throw std::runtime_error(description + ": seek before beginning of buffer"); |
291 | 2.00k | } |
292 | 221M | } |
293 | | |
294 | | size_t |
295 | | is::OffsetBuffer::read(char* buffer, size_t length) |
296 | 28.6M | { |
297 | 28.6M | if (pos < 0) { |
298 | 0 | throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0"); |
299 | 0 | } |
300 | 28.6M | auto end_pos = static_cast<qpdf_offset_t>(view_.size()); |
301 | 28.6M | if (pos >= end_pos) { |
302 | 793k | last_offset = end_pos + global_offset; |
303 | 793k | return 0; |
304 | 793k | } |
305 | | |
306 | 27.8M | last_offset = pos + global_offset; |
307 | 27.8M | size_t len = std::min(QIntC::to_size(end_pos - pos), length); |
308 | 27.8M | memcpy(buffer, view_.data() + pos, len); |
309 | 27.8M | pos += QIntC::to_offset(len); |
310 | 27.8M | return len; |
311 | 28.6M | } |