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