/src/libzip/lib/zip_source_buffer.c
Line | Count | Source |
1 | | /* |
2 | | zip_source_buffer.c -- create zip data source from buffer |
3 | | Copyright (C) 1999-2025 Dieter Baron and Thomas Klausner |
4 | | |
5 | | This file is part of libzip, a library to manipulate ZIP archives. |
6 | | The authors can be contacted at <info@libzip.org> |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | 1. Redistributions of source code must retain the above copyright |
12 | | notice, this list of conditions and the following disclaimer. |
13 | | 2. Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in |
15 | | the documentation and/or other materials provided with the |
16 | | distribution. |
17 | | 3. The names of the authors may not be used to endorse or promote |
18 | | products derived from this software without specific prior |
19 | | written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
22 | | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
25 | | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
27 | | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
29 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
30 | | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
31 | | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | #include "zipint.h" |
35 | | |
36 | | #include <stdlib.h> |
37 | | #include <string.h> |
38 | | |
39 | | #ifndef WRITE_FRAGMENT_SIZE |
40 | 0 | #define WRITE_FRAGMENT_SIZE (64 * 1024) |
41 | | #endif |
42 | | |
43 | | struct buffer { |
44 | | zip_buffer_fragment_t *fragments; /* fragments */ |
45 | | zip_uint64_t *fragment_offsets; /* offset of each fragment from start of buffer, nfragments+1 entries */ |
46 | | zip_uint64_t nfragments; /* number of allocated fragments */ |
47 | | zip_uint64_t fragments_capacity; /* size of fragments (number of pointers) */ |
48 | | |
49 | | zip_uint64_t first_owned_fragment; /* first fragment to free data from */ |
50 | | |
51 | | zip_uint64_t shared_fragments; /* number of shared fragments */ |
52 | | struct buffer *shared_buffer; /* buffer fragments are shared with */ |
53 | | |
54 | | zip_uint64_t size; /* size of buffer */ |
55 | | zip_uint64_t offset; /* current offset in buffer */ |
56 | | zip_uint64_t current_fragment; /* fragment current offset is in */ |
57 | | }; |
58 | | |
59 | | typedef struct buffer buffer_t; |
60 | | |
61 | | struct read_data { |
62 | | zip_error_t error; |
63 | | time_t mtime; |
64 | | zip_file_attributes_t attributes; |
65 | | buffer_t *in; |
66 | | buffer_t *out; |
67 | | }; |
68 | | |
69 | 0 | #define buffer_capacity(buffer) ((buffer)->fragment_offsets[(buffer)->nfragments]) |
70 | | #define buffer_size(buffer) ((buffer)->size) |
71 | | |
72 | | static zip_int64_t buffer_at_eof(const buffer_t *buffer); |
73 | | static buffer_t *buffer_clone(buffer_t *buffer, zip_uint64_t length, zip_error_t *error); |
74 | | static zip_uint64_t buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset); |
75 | | static void buffer_free(buffer_t *buffer); |
76 | | static bool buffer_grow_fragments(buffer_t *buffer, zip_uint64_t capacity, zip_error_t *error); |
77 | | static buffer_t *buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int free_data, zip_error_t *error); |
78 | | static zip_int64_t buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length); |
79 | | static int buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error); |
80 | | static zip_int64_t buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *); |
81 | | |
82 | | static zip_int64_t read_data(void *, void *, zip_uint64_t, zip_source_cmd_t); |
83 | | |
84 | | zip_source_t *zip_source_buffer_with_attributes_create(const void *data, zip_uint64_t len, int freep, zip_file_attributes_t *attributes, zip_error_t *error); |
85 | | zip_source_t *zip_source_buffer_fragment_with_attributes_create(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep, zip_file_attributes_t *attributes, zip_error_t *error); |
86 | | |
87 | | |
88 | 0 | ZIP_EXTERN zip_source_t *zip_source_buffer(zip_t *za, const void *data, zip_uint64_t len, int freep) { |
89 | 0 | if (za == NULL) { |
90 | 0 | return NULL; |
91 | 0 | } |
92 | | |
93 | 0 | return zip_source_buffer_with_attributes_create(data, len, freep, NULL, &za->error); |
94 | 0 | } |
95 | | |
96 | | |
97 | 0 | ZIP_EXTERN zip_source_t *zip_source_buffer_create(const void *data, zip_uint64_t len, int freep, zip_error_t *error) { |
98 | 0 | return zip_source_buffer_with_attributes_create(data, len, freep, NULL, error); |
99 | 0 | } |
100 | | |
101 | | |
102 | 0 | zip_source_t *zip_source_buffer_with_attributes_create(const void *data, zip_uint64_t len, int freep, zip_file_attributes_t *attributes, zip_error_t *error) { |
103 | 0 | zip_buffer_fragment_t fragment; |
104 | |
|
105 | 0 | if (data == NULL) { |
106 | 0 | if (len > 0) { |
107 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
108 | 0 | return NULL; |
109 | 0 | } |
110 | | |
111 | 0 | return zip_source_buffer_fragment_with_attributes_create(NULL, 0, freep, attributes, error); |
112 | 0 | } |
113 | | |
114 | 0 | fragment.data = (zip_uint8_t *)data; |
115 | 0 | fragment.length = len; |
116 | |
|
117 | 0 | return zip_source_buffer_fragment_with_attributes_create(&fragment, 1, freep, attributes, error); |
118 | 0 | } |
119 | | |
120 | | |
121 | 0 | ZIP_EXTERN zip_source_t *zip_source_buffer_fragment(zip_t *za, const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep) { |
122 | 0 | if (za == NULL) { |
123 | 0 | return NULL; |
124 | 0 | } |
125 | | |
126 | 0 | return zip_source_buffer_fragment_with_attributes_create(fragments, nfragments, freep, NULL, &za->error); |
127 | 0 | } |
128 | | |
129 | | |
130 | 0 | ZIP_EXTERN zip_source_t *zip_source_buffer_fragment_create(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep, zip_error_t *error) { |
131 | 0 | return zip_source_buffer_fragment_with_attributes_create(fragments, nfragments, freep, NULL, error); |
132 | 0 | } |
133 | | |
134 | 0 | zip_source_t *zip_source_buffer_fragment_with_attributes_create(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep, zip_file_attributes_t *attributes, zip_error_t *error) { |
135 | 0 | struct read_data *ctx; |
136 | 0 | zip_source_t *zs; |
137 | 0 | buffer_t *buffer; |
138 | |
|
139 | 0 | if (fragments == NULL && nfragments > 0) { |
140 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
141 | 0 | return NULL; |
142 | 0 | } |
143 | | |
144 | 0 | if ((buffer = buffer_new(fragments, nfragments, freep, error)) == NULL) { |
145 | 0 | return NULL; |
146 | 0 | } |
147 | | |
148 | 0 | if ((ctx = (struct read_data *)malloc(sizeof(*ctx))) == NULL) { |
149 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
150 | 0 | buffer_free(buffer); |
151 | 0 | return NULL; |
152 | 0 | } |
153 | | |
154 | 0 | ctx->in = buffer; |
155 | 0 | ctx->out = NULL; |
156 | 0 | ctx->mtime = time(NULL); |
157 | 0 | if (attributes) { |
158 | 0 | (void)memcpy_s(&ctx->attributes, sizeof(ctx->attributes), attributes, sizeof(ctx->attributes)); |
159 | 0 | } |
160 | 0 | else { |
161 | 0 | zip_file_attributes_init(&ctx->attributes); |
162 | 0 | } |
163 | 0 | zip_error_init(&ctx->error); |
164 | |
|
165 | 0 | if ((zs = zip_source_function_create(read_data, ctx, error)) == NULL) { |
166 | 0 | buffer_free(ctx->in); |
167 | 0 | free(ctx); |
168 | 0 | return NULL; |
169 | 0 | } |
170 | | |
171 | 0 | return zs; |
172 | 0 | } |
173 | | |
174 | | |
175 | 0 | zip_source_t *zip_source_buffer_with_attributes(zip_t *za, const void *data, zip_uint64_t len, int freep, zip_file_attributes_t *attributes) { |
176 | 0 | return zip_source_buffer_with_attributes_create(data, len, freep, attributes, &za->error); |
177 | 0 | } |
178 | | |
179 | 0 | static zip_int64_t read_data(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { |
180 | 0 | struct read_data *ctx = (struct read_data *)state; |
181 | |
|
182 | 0 | switch (cmd) { |
183 | 0 | case ZIP_SOURCE_AT_EOF: |
184 | 0 | return buffer_at_eof(ctx->in); |
185 | | |
186 | 0 | case ZIP_SOURCE_BEGIN_WRITE: |
187 | 0 | if ((ctx->out = buffer_new(NULL, 0, 0, &ctx->error)) == NULL) { |
188 | 0 | return -1; |
189 | 0 | } |
190 | 0 | ctx->out->offset = 0; |
191 | 0 | ctx->out->current_fragment = 0; |
192 | 0 | return 0; |
193 | | |
194 | 0 | case ZIP_SOURCE_BEGIN_WRITE_CLONING: |
195 | 0 | if ((ctx->out = buffer_clone(ctx->in, len, &ctx->error)) == NULL) { |
196 | 0 | return -1; |
197 | 0 | } |
198 | 0 | ctx->out->offset = len; |
199 | 0 | ctx->out->current_fragment = ctx->out->nfragments; |
200 | 0 | return 0; |
201 | | |
202 | 0 | case ZIP_SOURCE_CLOSE: |
203 | 0 | return 0; |
204 | | |
205 | 0 | case ZIP_SOURCE_COMMIT_WRITE: |
206 | 0 | buffer_free(ctx->in); |
207 | 0 | ctx->in = ctx->out; |
208 | 0 | ctx->out = NULL; |
209 | 0 | return 0; |
210 | | |
211 | 0 | case ZIP_SOURCE_ERROR: |
212 | 0 | return zip_error_to_data(&ctx->error, data, len); |
213 | | |
214 | 0 | case ZIP_SOURCE_FREE: |
215 | 0 | buffer_free(ctx->in); |
216 | 0 | buffer_free(ctx->out); |
217 | 0 | free(ctx); |
218 | 0 | return 0; |
219 | | |
220 | 0 | case ZIP_SOURCE_GET_FILE_ATTRIBUTES: { |
221 | 0 | if (len < sizeof(ctx->attributes)) { |
222 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
223 | 0 | return -1; |
224 | 0 | } |
225 | | |
226 | 0 | (void)memcpy_s(data, sizeof(ctx->attributes), &ctx->attributes, sizeof(ctx->attributes)); |
227 | |
|
228 | 0 | return sizeof(ctx->attributes); |
229 | 0 | } |
230 | | |
231 | 0 | case ZIP_SOURCE_OPEN: |
232 | 0 | ctx->in->offset = 0; |
233 | 0 | ctx->in->current_fragment = 0; |
234 | 0 | return 0; |
235 | | |
236 | 0 | case ZIP_SOURCE_READ: |
237 | 0 | if (len > ZIP_INT64_MAX) { |
238 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
239 | 0 | return -1; |
240 | 0 | } |
241 | 0 | return buffer_read(ctx->in, data, len); |
242 | | |
243 | 0 | case ZIP_SOURCE_REMOVE: { |
244 | 0 | buffer_t *empty = buffer_new(NULL, 0, 0, &ctx->error); |
245 | 0 | if (empty == NULL) { |
246 | 0 | return -1; |
247 | 0 | } |
248 | | |
249 | 0 | buffer_free(ctx->in); |
250 | 0 | ctx->in = empty; |
251 | 0 | return 0; |
252 | 0 | } |
253 | | |
254 | 0 | case ZIP_SOURCE_ROLLBACK_WRITE: |
255 | 0 | buffer_free(ctx->out); |
256 | 0 | ctx->out = NULL; |
257 | 0 | return 0; |
258 | | |
259 | 0 | case ZIP_SOURCE_SEEK: |
260 | 0 | return buffer_seek(ctx->in, data, len, &ctx->error); |
261 | | |
262 | 0 | case ZIP_SOURCE_SEEK_WRITE: |
263 | 0 | return buffer_seek(ctx->out, data, len, &ctx->error); |
264 | | |
265 | 0 | case ZIP_SOURCE_STAT: { |
266 | 0 | zip_stat_t *st; |
267 | |
|
268 | 0 | if (len < sizeof(*st)) { |
269 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
270 | 0 | return -1; |
271 | 0 | } |
272 | | |
273 | 0 | st = (zip_stat_t *)data; |
274 | |
|
275 | 0 | zip_stat_init(st); |
276 | 0 | st->mtime = ctx->mtime; |
277 | 0 | st->size = ctx->in->size; |
278 | 0 | st->comp_size = st->size; |
279 | 0 | st->comp_method = ZIP_CM_STORE; |
280 | 0 | st->encryption_method = ZIP_EM_NONE; |
281 | 0 | st->valid = ZIP_STAT_MTIME | ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD; |
282 | |
|
283 | 0 | return sizeof(*st); |
284 | 0 | } |
285 | | |
286 | 0 | case ZIP_SOURCE_SUPPORTS: |
287 | 0 | return zip_source_make_command_bitmap(ZIP_SOURCE_AT_EOF, ZIP_SOURCE_GET_FILE_ATTRIBUTES, ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_SEEK, ZIP_SOURCE_TELL, ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_BEGIN_WRITE_CLONING, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_REMOVE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_WRITE, ZIP_SOURCE_SUPPORTS_REOPEN, -1); |
288 | | |
289 | 0 | case ZIP_SOURCE_TELL: |
290 | 0 | if (ctx->in->offset > ZIP_INT64_MAX) { |
291 | 0 | zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW); |
292 | 0 | return -1; |
293 | 0 | } |
294 | 0 | return (zip_int64_t)ctx->in->offset; |
295 | | |
296 | | |
297 | 0 | case ZIP_SOURCE_TELL_WRITE: |
298 | 0 | if (ctx->out->offset > ZIP_INT64_MAX) { |
299 | 0 | zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW); |
300 | 0 | return -1; |
301 | 0 | } |
302 | 0 | return (zip_int64_t)ctx->out->offset; |
303 | | |
304 | 0 | case ZIP_SOURCE_WRITE: |
305 | 0 | if (len > ZIP_INT64_MAX) { |
306 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
307 | 0 | return -1; |
308 | 0 | } |
309 | 0 | return buffer_write(ctx->out, data, len, &ctx->error); |
310 | | |
311 | 0 | default: |
312 | 0 | zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); |
313 | 0 | return -1; |
314 | 0 | } |
315 | 0 | } |
316 | | |
317 | | |
318 | 0 | static buffer_t *buffer_clone(buffer_t *buffer, zip_uint64_t offset, zip_error_t *error) { |
319 | 0 | zip_uint64_t fragment, fragment_offset, waste; |
320 | 0 | buffer_t *clone; |
321 | |
|
322 | 0 | if (offset == 0) { |
323 | 0 | return buffer_new(NULL, 0, 1, error); |
324 | 0 | } |
325 | | |
326 | 0 | if (offset > buffer->size) { |
327 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
328 | 0 | return NULL; |
329 | 0 | } |
330 | 0 | if (buffer->shared_buffer != NULL) { |
331 | 0 | zip_error_set(error, ZIP_ER_INUSE, 0); |
332 | 0 | return NULL; |
333 | 0 | } |
334 | | |
335 | 0 | fragment = buffer_find_fragment(buffer, offset); |
336 | 0 | fragment_offset = offset - buffer->fragment_offsets[fragment]; |
337 | |
|
338 | 0 | if (fragment_offset == 0) { |
339 | | /* We can't be at beginning of fragment zero if offset > 0. */ |
340 | 0 | fragment--; |
341 | 0 | fragment_offset = buffer->fragments[fragment].length; |
342 | 0 | } |
343 | | |
344 | | /* TODO: This should also consider the length of the fully shared fragments */ |
345 | 0 | waste = buffer->fragments[fragment].length - fragment_offset; |
346 | 0 | if (waste > offset) { |
347 | 0 | zip_error_set(error, ZIP_ER_OPNOTSUPP, 0); |
348 | 0 | return NULL; |
349 | 0 | } |
350 | | |
351 | 0 | if ((clone = buffer_new(buffer->fragments, fragment + 1, 0, error)) == NULL) { |
352 | 0 | return NULL; |
353 | 0 | } |
354 | | |
355 | 0 | #ifndef __clang_analyzer__ |
356 | | /* clone->fragments can't be null, since it was created with at least one fragment */ |
357 | 0 | clone->fragments[fragment].length = fragment_offset; |
358 | 0 | #endif |
359 | 0 | clone->fragment_offsets[clone->nfragments] = offset; |
360 | 0 | clone->size = offset; |
361 | |
|
362 | 0 | clone->first_owned_fragment = ZIP_MIN(buffer->first_owned_fragment, clone->nfragments); |
363 | |
|
364 | 0 | buffer->shared_buffer = clone; |
365 | 0 | clone->shared_buffer = buffer; |
366 | 0 | buffer->shared_fragments = fragment + 1; |
367 | 0 | clone->shared_fragments = fragment + 1; |
368 | |
|
369 | 0 | return clone; |
370 | 0 | } |
371 | | |
372 | | |
373 | 0 | static zip_uint64_t buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset) { |
374 | 0 | zip_uint64_t low, high, mid; |
375 | |
|
376 | 0 | if (buffer->nfragments == 0) { |
377 | 0 | return 0; |
378 | 0 | } |
379 | | |
380 | 0 | low = 0; |
381 | 0 | high = buffer->nfragments - 1; |
382 | |
|
383 | 0 | while (low < high) { |
384 | 0 | mid = (high - low) / 2 + low; |
385 | 0 | if (buffer->fragment_offsets[mid] > offset) { |
386 | 0 | high = mid - 1; |
387 | 0 | } |
388 | 0 | else if (mid == buffer->nfragments || buffer->fragment_offsets[mid + 1] > offset) { |
389 | 0 | return mid; |
390 | 0 | } |
391 | 0 | else { |
392 | 0 | low = mid + 1; |
393 | 0 | } |
394 | 0 | } |
395 | | |
396 | 0 | return low; |
397 | 0 | } |
398 | | |
399 | | |
400 | 0 | static void buffer_free(buffer_t *buffer) { |
401 | 0 | zip_uint64_t i; |
402 | |
|
403 | 0 | if (buffer == NULL) { |
404 | 0 | return; |
405 | 0 | } |
406 | | |
407 | 0 | if (buffer->shared_buffer != NULL) { |
408 | 0 | buffer->shared_buffer->shared_buffer = NULL; |
409 | 0 | buffer->shared_buffer->shared_fragments = 0; |
410 | |
|
411 | 0 | buffer->first_owned_fragment = ZIP_MAX(buffer->first_owned_fragment, buffer->shared_fragments); |
412 | 0 | } |
413 | |
|
414 | 0 | for (i = buffer->first_owned_fragment; i < buffer->nfragments; i++) { |
415 | 0 | free(buffer->fragments[i].data); |
416 | 0 | } |
417 | 0 | free(buffer->fragments); |
418 | 0 | free(buffer->fragment_offsets); |
419 | 0 | free(buffer); |
420 | 0 | } |
421 | | |
422 | | |
423 | 0 | static bool buffer_grow_fragments(buffer_t *buffer, zip_uint64_t capacity, zip_error_t *error) { |
424 | 0 | zip_uint64_t additional_fragments; |
425 | 0 | zip_uint64_t offset_capacity = buffer->fragments_capacity + 1; |
426 | |
|
427 | 0 | if (capacity <= buffer->fragments_capacity) { |
428 | 0 | return true; |
429 | 0 | } |
430 | | |
431 | 0 | additional_fragments = capacity - buffer->fragments_capacity; |
432 | |
|
433 | 0 | if (!ZIP_REALLOC(buffer->fragments, buffer->fragments_capacity, additional_fragments, error)) { |
434 | 0 | return false; |
435 | 0 | } |
436 | | /* The size of both buffer->fragments and buffer->fragment_offsets is stored in buffer->fragments_capacity, so use a temporary capacity variable here for reallocating buffer->fragment_offsets. */ |
437 | 0 | if (!ZIP_REALLOC(buffer->fragment_offsets, offset_capacity, additional_fragments, error)) { |
438 | 0 | buffer->fragments_capacity -= additional_fragments; |
439 | 0 | return false; |
440 | 0 | } |
441 | | |
442 | 0 | return true; |
443 | 0 | } |
444 | | |
445 | | |
446 | 0 | static buffer_t *buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int free_data, zip_error_t *error) { |
447 | 0 | buffer_t *buffer; |
448 | |
|
449 | 0 | if ((buffer = malloc(sizeof(*buffer))) == NULL) { |
450 | 0 | return NULL; |
451 | 0 | } |
452 | | |
453 | 0 | buffer->offset = 0; |
454 | 0 | buffer->first_owned_fragment = 0; |
455 | 0 | buffer->size = 0; |
456 | 0 | buffer->fragments = NULL; |
457 | 0 | buffer->fragment_offsets = NULL; |
458 | 0 | buffer->nfragments = 0; |
459 | 0 | buffer->fragments_capacity = 0; |
460 | 0 | buffer->shared_buffer = NULL; |
461 | 0 | buffer->shared_fragments = 0; |
462 | |
|
463 | 0 | if (nfragments == 0) { |
464 | 0 | if ((buffer->fragment_offsets = malloc(sizeof(buffer->fragment_offsets[0]))) == NULL) { |
465 | 0 | free(buffer); |
466 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
467 | 0 | return NULL; |
468 | 0 | } |
469 | 0 | buffer->fragment_offsets[0] = 0; |
470 | 0 | } |
471 | 0 | else { |
472 | 0 | zip_uint64_t i, j, offset; |
473 | |
|
474 | 0 | if (!buffer_grow_fragments(buffer, nfragments, NULL)) { |
475 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
476 | 0 | buffer_free(buffer); |
477 | 0 | return NULL; |
478 | 0 | } |
479 | | |
480 | 0 | offset = 0; |
481 | 0 | for (i = 0, j = 0; i < nfragments; i++) { |
482 | 0 | if (fragments[i].length == 0) { |
483 | 0 | continue; |
484 | 0 | } |
485 | 0 | if (fragments[i].data == NULL) { |
486 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
487 | 0 | buffer_free(buffer); |
488 | 0 | return NULL; |
489 | 0 | } |
490 | 0 | buffer->fragments[j].data = fragments[i].data; |
491 | 0 | buffer->fragments[j].length = fragments[i].length; |
492 | 0 | buffer->fragment_offsets[j] = offset; |
493 | 0 | if (offset + fragments[i].length < offset) { |
494 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
495 | 0 | buffer_free(buffer); |
496 | 0 | return NULL; |
497 | 0 | } |
498 | 0 | offset += fragments[i].length; |
499 | 0 | j++; |
500 | 0 | } |
501 | 0 | buffer->nfragments = j; |
502 | 0 | buffer->first_owned_fragment = free_data ? 0 : buffer->nfragments; |
503 | 0 | buffer->fragment_offsets[buffer->nfragments] = offset; |
504 | 0 | buffer->size = offset; |
505 | 0 | } |
506 | | |
507 | 0 | return buffer; |
508 | 0 | } |
509 | | |
510 | 0 | static zip_int64_t buffer_at_eof(const buffer_t *buffer) { |
511 | 0 | return buffer->offset == buffer->size; |
512 | 0 | } |
513 | | |
514 | 0 | static zip_int64_t buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) { |
515 | 0 | zip_uint64_t n, i, fragment_offset; |
516 | |
|
517 | 0 | length = ZIP_MIN(length, buffer->size - buffer->offset); |
518 | |
|
519 | 0 | if (length == 0) { |
520 | 0 | return 0; |
521 | 0 | } |
522 | 0 | if (length > ZIP_INT64_MAX) { |
523 | 0 | return -1; |
524 | 0 | } |
525 | | |
526 | 0 | i = buffer->current_fragment; |
527 | 0 | fragment_offset = buffer->offset - buffer->fragment_offsets[i]; |
528 | 0 | n = 0; |
529 | 0 | while (n < length) { |
530 | 0 | zip_uint64_t left = ZIP_MIN(length - n, buffer->fragments[i].length - fragment_offset); |
531 | | #if ZIP_UINT64_MAX > SIZE_MAX |
532 | | left = ZIP_MIN(left, SIZE_MAX); |
533 | | #endif |
534 | |
|
535 | 0 | (void)memcpy_s(data + n, (size_t)left, buffer->fragments[i].data + fragment_offset, (size_t)left); |
536 | |
|
537 | 0 | if (left == buffer->fragments[i].length - fragment_offset) { |
538 | 0 | i++; |
539 | 0 | } |
540 | 0 | n += left; |
541 | 0 | fragment_offset = 0; |
542 | 0 | } |
543 | |
|
544 | 0 | buffer->offset += n; |
545 | 0 | buffer->current_fragment = i; |
546 | 0 | return (zip_int64_t)n; |
547 | 0 | } |
548 | | |
549 | | |
550 | 0 | static int buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error) { |
551 | 0 | zip_int64_t new_offset = zip_source_seek_compute_offset(buffer->offset, buffer->size, data, len, error); |
552 | |
|
553 | 0 | if (new_offset < 0) { |
554 | 0 | return -1; |
555 | 0 | } |
556 | | |
557 | 0 | buffer->offset = (zip_uint64_t)new_offset; |
558 | 0 | buffer->current_fragment = buffer_find_fragment(buffer, buffer->offset); |
559 | 0 | return 0; |
560 | 0 | } |
561 | | |
562 | | |
563 | 0 | static zip_int64_t buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *error) { |
564 | 0 | zip_uint64_t copied, i, fragment_offset, capacity; |
565 | |
|
566 | 0 | if (buffer->offset + length + WRITE_FRAGMENT_SIZE - 1 < length) { |
567 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
568 | 0 | return -1; |
569 | 0 | } |
570 | | |
571 | | /* grow buffer if needed */ |
572 | 0 | capacity = buffer_capacity(buffer); |
573 | 0 | if (buffer->offset + length > capacity) { |
574 | 0 | zip_uint64_t needed_fragments = buffer->nfragments + (length - (capacity - buffer->offset) + WRITE_FRAGMENT_SIZE - 1) / WRITE_FRAGMENT_SIZE; |
575 | |
|
576 | 0 | if (needed_fragments > buffer->fragments_capacity) { |
577 | 0 | zip_uint64_t new_capacity = buffer->fragments_capacity; |
578 | |
|
579 | 0 | if (new_capacity == 0) { |
580 | 0 | new_capacity = 16; |
581 | 0 | } |
582 | 0 | while (new_capacity < needed_fragments) { |
583 | 0 | new_capacity *= 2; |
584 | 0 | } |
585 | |
|
586 | 0 | if (!buffer_grow_fragments(buffer, new_capacity, error)) { |
587 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
588 | 0 | return -1; |
589 | 0 | } |
590 | 0 | } |
591 | | |
592 | 0 | while (buffer->nfragments < needed_fragments) { |
593 | 0 | if ((buffer->fragments[buffer->nfragments].data = malloc(WRITE_FRAGMENT_SIZE)) == NULL) { |
594 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
595 | 0 | return -1; |
596 | 0 | } |
597 | 0 | buffer->fragments[buffer->nfragments].length = WRITE_FRAGMENT_SIZE; |
598 | 0 | buffer->nfragments++; |
599 | 0 | capacity += WRITE_FRAGMENT_SIZE; |
600 | 0 | buffer->fragment_offsets[buffer->nfragments] = capacity; |
601 | 0 | } |
602 | 0 | } |
603 | | |
604 | 0 | i = buffer->current_fragment; |
605 | 0 | fragment_offset = buffer->offset - buffer->fragment_offsets[i]; |
606 | 0 | copied = 0; |
607 | 0 | while (copied < length) { |
608 | 0 | zip_uint64_t n = ZIP_MIN(ZIP_MIN(length - copied, buffer->fragments[i].length - fragment_offset), SIZE_MAX); |
609 | | #if ZIP_UINT64_MAX > SIZE_MAX |
610 | | n = ZIP_MIN(n, SIZE_MAX); |
611 | | #endif |
612 | |
|
613 | 0 | (void)memcpy_s(buffer->fragments[i].data + fragment_offset, (size_t)n, data + copied, (size_t)n); |
614 | |
|
615 | 0 | if (n == buffer->fragments[i].length - fragment_offset) { |
616 | 0 | i++; |
617 | 0 | fragment_offset = 0; |
618 | 0 | } |
619 | 0 | else { |
620 | 0 | fragment_offset += n; |
621 | 0 | } |
622 | 0 | copied += n; |
623 | 0 | } |
624 | |
|
625 | 0 | buffer->offset += copied; |
626 | 0 | buffer->current_fragment = i; |
627 | 0 | if (buffer->offset > buffer->size) { |
628 | 0 | buffer->size = buffer->offset; |
629 | 0 | } |
630 | |
|
631 | 0 | return (zip_int64_t)copied; |
632 | 0 | } |