/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 buffer_t *buffer_clone(buffer_t *buffer, zip_uint64_t length, zip_error_t *error); |
73 | | static zip_uint64_t buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset); |
74 | | static void buffer_free(buffer_t *buffer); |
75 | | static bool buffer_grow_fragments(buffer_t *buffer, zip_uint64_t capacity, zip_error_t *error); |
76 | | static buffer_t *buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int free_data, zip_error_t *error); |
77 | | static zip_int64_t buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length); |
78 | | static int buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error); |
79 | | static zip_int64_t buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *); |
80 | | |
81 | | static zip_int64_t read_data(void *, void *, zip_uint64_t, zip_source_cmd_t); |
82 | | |
83 | | 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); |
84 | | 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); |
85 | | |
86 | | |
87 | 0 | ZIP_EXTERN zip_source_t *zip_source_buffer(zip_t *za, const void *data, zip_uint64_t len, int freep) { |
88 | 0 | if (za == NULL) { |
89 | 0 | return NULL; |
90 | 0 | } |
91 | | |
92 | 0 | return zip_source_buffer_with_attributes_create(data, len, freep, NULL, &za->error); |
93 | 0 | } |
94 | | |
95 | | |
96 | 0 | ZIP_EXTERN zip_source_t *zip_source_buffer_create(const void *data, zip_uint64_t len, int freep, zip_error_t *error) { |
97 | 0 | return zip_source_buffer_with_attributes_create(data, len, freep, NULL, error); |
98 | 0 | } |
99 | | |
100 | | |
101 | 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) { |
102 | 0 | zip_buffer_fragment_t fragment; |
103 | |
|
104 | 0 | if (data == NULL) { |
105 | 0 | if (len > 0) { |
106 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
107 | 0 | return NULL; |
108 | 0 | } |
109 | | |
110 | 0 | return zip_source_buffer_fragment_with_attributes_create(NULL, 0, freep, attributes, error); |
111 | 0 | } |
112 | | |
113 | 0 | fragment.data = (zip_uint8_t *)data; |
114 | 0 | fragment.length = len; |
115 | |
|
116 | 0 | return zip_source_buffer_fragment_with_attributes_create(&fragment, 1, freep, attributes, error); |
117 | 0 | } |
118 | | |
119 | | |
120 | 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) { |
121 | 0 | if (za == NULL) { |
122 | 0 | return NULL; |
123 | 0 | } |
124 | | |
125 | 0 | return zip_source_buffer_fragment_with_attributes_create(fragments, nfragments, freep, NULL, &za->error); |
126 | 0 | } |
127 | | |
128 | | |
129 | 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) { |
130 | 0 | return zip_source_buffer_fragment_with_attributes_create(fragments, nfragments, freep, NULL, error); |
131 | 0 | } |
132 | | |
133 | 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) { |
134 | 0 | struct read_data *ctx; |
135 | 0 | zip_source_t *zs; |
136 | 0 | buffer_t *buffer; |
137 | |
|
138 | 0 | if (fragments == NULL && nfragments > 0) { |
139 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
140 | 0 | return NULL; |
141 | 0 | } |
142 | | |
143 | 0 | if ((buffer = buffer_new(fragments, nfragments, freep, error)) == NULL) { |
144 | 0 | return NULL; |
145 | 0 | } |
146 | | |
147 | 0 | if ((ctx = (struct read_data *)malloc(sizeof(*ctx))) == NULL) { |
148 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
149 | 0 | buffer_free(buffer); |
150 | 0 | return NULL; |
151 | 0 | } |
152 | | |
153 | 0 | ctx->in = buffer; |
154 | 0 | ctx->out = NULL; |
155 | 0 | ctx->mtime = time(NULL); |
156 | 0 | if (attributes) { |
157 | 0 | (void)memcpy_s(&ctx->attributes, sizeof(ctx->attributes), attributes, sizeof(ctx->attributes)); |
158 | 0 | } |
159 | 0 | else { |
160 | 0 | zip_file_attributes_init(&ctx->attributes); |
161 | 0 | } |
162 | 0 | zip_error_init(&ctx->error); |
163 | |
|
164 | 0 | if ((zs = zip_source_function_create(read_data, ctx, error)) == NULL) { |
165 | 0 | buffer_free(ctx->in); |
166 | 0 | free(ctx); |
167 | 0 | return NULL; |
168 | 0 | } |
169 | | |
170 | 0 | return zs; |
171 | 0 | } |
172 | | |
173 | | |
174 | 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) { |
175 | 0 | return zip_source_buffer_with_attributes_create(data, len, freep, attributes, &za->error); |
176 | 0 | } |
177 | | |
178 | 0 | static zip_int64_t read_data(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { |
179 | 0 | struct read_data *ctx = (struct read_data *)state; |
180 | |
|
181 | 0 | switch (cmd) { |
182 | 0 | case ZIP_SOURCE_BEGIN_WRITE: |
183 | 0 | if ((ctx->out = buffer_new(NULL, 0, 0, &ctx->error)) == NULL) { |
184 | 0 | return -1; |
185 | 0 | } |
186 | 0 | ctx->out->offset = 0; |
187 | 0 | ctx->out->current_fragment = 0; |
188 | 0 | return 0; |
189 | | |
190 | 0 | case ZIP_SOURCE_BEGIN_WRITE_CLONING: |
191 | 0 | if ((ctx->out = buffer_clone(ctx->in, len, &ctx->error)) == NULL) { |
192 | 0 | return -1; |
193 | 0 | } |
194 | 0 | ctx->out->offset = len; |
195 | 0 | ctx->out->current_fragment = ctx->out->nfragments; |
196 | 0 | return 0; |
197 | | |
198 | 0 | case ZIP_SOURCE_CLOSE: |
199 | 0 | return 0; |
200 | | |
201 | 0 | case ZIP_SOURCE_COMMIT_WRITE: |
202 | 0 | buffer_free(ctx->in); |
203 | 0 | ctx->in = ctx->out; |
204 | 0 | ctx->out = NULL; |
205 | 0 | return 0; |
206 | | |
207 | 0 | case ZIP_SOURCE_ERROR: |
208 | 0 | return zip_error_to_data(&ctx->error, data, len); |
209 | | |
210 | 0 | case ZIP_SOURCE_FREE: |
211 | 0 | buffer_free(ctx->in); |
212 | 0 | buffer_free(ctx->out); |
213 | 0 | free(ctx); |
214 | 0 | return 0; |
215 | | |
216 | 0 | case ZIP_SOURCE_GET_FILE_ATTRIBUTES: { |
217 | 0 | if (len < sizeof(ctx->attributes)) { |
218 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
219 | 0 | return -1; |
220 | 0 | } |
221 | | |
222 | 0 | (void)memcpy_s(data, sizeof(ctx->attributes), &ctx->attributes, sizeof(ctx->attributes)); |
223 | |
|
224 | 0 | return sizeof(ctx->attributes); |
225 | 0 | } |
226 | | |
227 | 0 | case ZIP_SOURCE_OPEN: |
228 | 0 | ctx->in->offset = 0; |
229 | 0 | ctx->in->current_fragment = 0; |
230 | 0 | return 0; |
231 | | |
232 | 0 | case ZIP_SOURCE_READ: |
233 | 0 | if (len > ZIP_INT64_MAX) { |
234 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
235 | 0 | return -1; |
236 | 0 | } |
237 | 0 | return buffer_read(ctx->in, data, len); |
238 | | |
239 | 0 | case ZIP_SOURCE_REMOVE: { |
240 | 0 | buffer_t *empty = buffer_new(NULL, 0, 0, &ctx->error); |
241 | 0 | if (empty == NULL) { |
242 | 0 | return -1; |
243 | 0 | } |
244 | | |
245 | 0 | buffer_free(ctx->in); |
246 | 0 | ctx->in = empty; |
247 | 0 | return 0; |
248 | 0 | } |
249 | | |
250 | 0 | case ZIP_SOURCE_ROLLBACK_WRITE: |
251 | 0 | buffer_free(ctx->out); |
252 | 0 | ctx->out = NULL; |
253 | 0 | return 0; |
254 | | |
255 | 0 | case ZIP_SOURCE_SEEK: |
256 | 0 | return buffer_seek(ctx->in, data, len, &ctx->error); |
257 | | |
258 | 0 | case ZIP_SOURCE_SEEK_WRITE: |
259 | 0 | return buffer_seek(ctx->out, data, len, &ctx->error); |
260 | | |
261 | 0 | case ZIP_SOURCE_STAT: { |
262 | 0 | zip_stat_t *st; |
263 | |
|
264 | 0 | if (len < sizeof(*st)) { |
265 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
266 | 0 | return -1; |
267 | 0 | } |
268 | | |
269 | 0 | st = (zip_stat_t *)data; |
270 | |
|
271 | 0 | zip_stat_init(st); |
272 | 0 | st->mtime = ctx->mtime; |
273 | 0 | st->size = ctx->in->size; |
274 | 0 | st->comp_size = st->size; |
275 | 0 | st->comp_method = ZIP_CM_STORE; |
276 | 0 | st->encryption_method = ZIP_EM_NONE; |
277 | 0 | st->valid = ZIP_STAT_MTIME | ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD; |
278 | |
|
279 | 0 | return sizeof(*st); |
280 | 0 | } |
281 | | |
282 | 0 | case ZIP_SOURCE_SUPPORTS: |
283 | 0 | return zip_source_make_command_bitmap(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); |
284 | | |
285 | 0 | case ZIP_SOURCE_TELL: |
286 | 0 | if (ctx->in->offset > ZIP_INT64_MAX) { |
287 | 0 | zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW); |
288 | 0 | return -1; |
289 | 0 | } |
290 | 0 | return (zip_int64_t)ctx->in->offset; |
291 | | |
292 | | |
293 | 0 | case ZIP_SOURCE_TELL_WRITE: |
294 | 0 | if (ctx->out->offset > ZIP_INT64_MAX) { |
295 | 0 | zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW); |
296 | 0 | return -1; |
297 | 0 | } |
298 | 0 | return (zip_int64_t)ctx->out->offset; |
299 | | |
300 | 0 | case ZIP_SOURCE_WRITE: |
301 | 0 | if (len > ZIP_INT64_MAX) { |
302 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
303 | 0 | return -1; |
304 | 0 | } |
305 | 0 | return buffer_write(ctx->out, data, len, &ctx->error); |
306 | | |
307 | 0 | default: |
308 | 0 | zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0); |
309 | 0 | return -1; |
310 | 0 | } |
311 | 0 | } |
312 | | |
313 | | |
314 | 0 | static buffer_t *buffer_clone(buffer_t *buffer, zip_uint64_t offset, zip_error_t *error) { |
315 | 0 | zip_uint64_t fragment, fragment_offset, waste; |
316 | 0 | buffer_t *clone; |
317 | |
|
318 | 0 | if (offset == 0) { |
319 | 0 | return buffer_new(NULL, 0, 1, error); |
320 | 0 | } |
321 | | |
322 | 0 | if (offset > buffer->size) { |
323 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
324 | 0 | return NULL; |
325 | 0 | } |
326 | 0 | if (buffer->shared_buffer != NULL) { |
327 | 0 | zip_error_set(error, ZIP_ER_INUSE, 0); |
328 | 0 | return NULL; |
329 | 0 | } |
330 | | |
331 | 0 | fragment = buffer_find_fragment(buffer, offset); |
332 | 0 | fragment_offset = offset - buffer->fragment_offsets[fragment]; |
333 | |
|
334 | 0 | if (fragment_offset == 0) { |
335 | | /* We can't be at beginning of fragment zero if offset > 0. */ |
336 | 0 | fragment--; |
337 | 0 | fragment_offset = buffer->fragments[fragment].length; |
338 | 0 | } |
339 | | |
340 | | /* TODO: This should also consider the length of the fully shared fragments */ |
341 | 0 | waste = buffer->fragments[fragment].length - fragment_offset; |
342 | 0 | if (waste > offset) { |
343 | 0 | zip_error_set(error, ZIP_ER_OPNOTSUPP, 0); |
344 | 0 | return NULL; |
345 | 0 | } |
346 | | |
347 | 0 | if ((clone = buffer_new(buffer->fragments, fragment + 1, 0, error)) == NULL) { |
348 | 0 | return NULL; |
349 | 0 | } |
350 | | |
351 | 0 | #ifndef __clang_analyzer__ |
352 | | /* clone->fragments can't be null, since it was created with at least one fragment */ |
353 | 0 | clone->fragments[fragment].length = fragment_offset; |
354 | 0 | #endif |
355 | 0 | clone->fragment_offsets[clone->nfragments] = offset; |
356 | 0 | clone->size = offset; |
357 | |
|
358 | 0 | clone->first_owned_fragment = ZIP_MIN(buffer->first_owned_fragment, clone->nfragments); |
359 | |
|
360 | 0 | buffer->shared_buffer = clone; |
361 | 0 | clone->shared_buffer = buffer; |
362 | 0 | buffer->shared_fragments = fragment + 1; |
363 | 0 | clone->shared_fragments = fragment + 1; |
364 | |
|
365 | 0 | return clone; |
366 | 0 | } |
367 | | |
368 | | |
369 | 0 | static zip_uint64_t buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset) { |
370 | 0 | zip_uint64_t low, high, mid; |
371 | |
|
372 | 0 | if (buffer->nfragments == 0) { |
373 | 0 | return 0; |
374 | 0 | } |
375 | | |
376 | 0 | low = 0; |
377 | 0 | high = buffer->nfragments - 1; |
378 | |
|
379 | 0 | while (low < high) { |
380 | 0 | mid = (high - low) / 2 + low; |
381 | 0 | if (buffer->fragment_offsets[mid] > offset) { |
382 | 0 | high = mid - 1; |
383 | 0 | } |
384 | 0 | else if (mid == buffer->nfragments || buffer->fragment_offsets[mid + 1] > offset) { |
385 | 0 | return mid; |
386 | 0 | } |
387 | 0 | else { |
388 | 0 | low = mid + 1; |
389 | 0 | } |
390 | 0 | } |
391 | | |
392 | 0 | return low; |
393 | 0 | } |
394 | | |
395 | | |
396 | 0 | static void buffer_free(buffer_t *buffer) { |
397 | 0 | zip_uint64_t i; |
398 | |
|
399 | 0 | if (buffer == NULL) { |
400 | 0 | return; |
401 | 0 | } |
402 | | |
403 | 0 | if (buffer->shared_buffer != NULL) { |
404 | 0 | buffer->shared_buffer->shared_buffer = NULL; |
405 | 0 | buffer->shared_buffer->shared_fragments = 0; |
406 | |
|
407 | 0 | buffer->first_owned_fragment = ZIP_MAX(buffer->first_owned_fragment, buffer->shared_fragments); |
408 | 0 | } |
409 | |
|
410 | 0 | for (i = buffer->first_owned_fragment; i < buffer->nfragments; i++) { |
411 | 0 | free(buffer->fragments[i].data); |
412 | 0 | } |
413 | 0 | free(buffer->fragments); |
414 | 0 | free(buffer->fragment_offsets); |
415 | 0 | free(buffer); |
416 | 0 | } |
417 | | |
418 | | |
419 | 0 | static bool buffer_grow_fragments(buffer_t *buffer, zip_uint64_t capacity, zip_error_t *error) { |
420 | 0 | zip_uint64_t additional_fragments; |
421 | 0 | zip_uint64_t offset_capacity = buffer->fragments_capacity + 1; |
422 | |
|
423 | 0 | if (capacity <= buffer->fragments_capacity) { |
424 | 0 | return true; |
425 | 0 | } |
426 | | |
427 | 0 | additional_fragments = capacity - buffer->fragments_capacity; |
428 | |
|
429 | 0 | if (!ZIP_REALLOC(buffer->fragments, buffer->fragments_capacity, additional_fragments, error)) { |
430 | 0 | return false; |
431 | 0 | } |
432 | | /* 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. */ |
433 | 0 | if (!ZIP_REALLOC(buffer->fragment_offsets, offset_capacity, additional_fragments, error)) { |
434 | 0 | buffer->fragments_capacity -= additional_fragments; |
435 | 0 | return false; |
436 | 0 | } |
437 | | |
438 | 0 | return true; |
439 | 0 | } |
440 | | |
441 | | |
442 | 0 | static buffer_t *buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int free_data, zip_error_t *error) { |
443 | 0 | buffer_t *buffer; |
444 | |
|
445 | 0 | if ((buffer = malloc(sizeof(*buffer))) == NULL) { |
446 | 0 | return NULL; |
447 | 0 | } |
448 | | |
449 | 0 | buffer->offset = 0; |
450 | 0 | buffer->first_owned_fragment = 0; |
451 | 0 | buffer->size = 0; |
452 | 0 | buffer->fragments = NULL; |
453 | 0 | buffer->fragment_offsets = NULL; |
454 | 0 | buffer->nfragments = 0; |
455 | 0 | buffer->fragments_capacity = 0; |
456 | 0 | buffer->shared_buffer = NULL; |
457 | 0 | buffer->shared_fragments = 0; |
458 | |
|
459 | 0 | if (nfragments == 0) { |
460 | 0 | if ((buffer->fragment_offsets = malloc(sizeof(buffer->fragment_offsets[0]))) == NULL) { |
461 | 0 | free(buffer); |
462 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
463 | 0 | return NULL; |
464 | 0 | } |
465 | 0 | buffer->fragment_offsets[0] = 0; |
466 | 0 | } |
467 | 0 | else { |
468 | 0 | zip_uint64_t i, j, offset; |
469 | |
|
470 | 0 | if (!buffer_grow_fragments(buffer, nfragments, NULL)) { |
471 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
472 | 0 | buffer_free(buffer); |
473 | 0 | return NULL; |
474 | 0 | } |
475 | | |
476 | 0 | offset = 0; |
477 | 0 | for (i = 0, j = 0; i < nfragments; i++) { |
478 | 0 | if (fragments[i].length == 0) { |
479 | 0 | continue; |
480 | 0 | } |
481 | 0 | if (fragments[i].data == NULL) { |
482 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
483 | 0 | buffer_free(buffer); |
484 | 0 | return NULL; |
485 | 0 | } |
486 | 0 | buffer->fragments[j].data = fragments[i].data; |
487 | 0 | buffer->fragments[j].length = fragments[i].length; |
488 | 0 | buffer->fragment_offsets[j] = offset; |
489 | 0 | if (offset + fragments[i].length < offset) { |
490 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
491 | 0 | buffer_free(buffer); |
492 | 0 | return NULL; |
493 | 0 | } |
494 | 0 | offset += fragments[i].length; |
495 | 0 | j++; |
496 | 0 | } |
497 | 0 | buffer->nfragments = j; |
498 | 0 | buffer->first_owned_fragment = free_data ? 0 : buffer->nfragments; |
499 | 0 | buffer->fragment_offsets[buffer->nfragments] = offset; |
500 | 0 | buffer->size = offset; |
501 | 0 | } |
502 | | |
503 | 0 | return buffer; |
504 | 0 | } |
505 | | |
506 | 0 | static zip_int64_t buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) { |
507 | 0 | zip_uint64_t n, i, fragment_offset; |
508 | |
|
509 | 0 | length = ZIP_MIN(length, buffer->size - buffer->offset); |
510 | |
|
511 | 0 | if (length == 0) { |
512 | 0 | return 0; |
513 | 0 | } |
514 | 0 | if (length > ZIP_INT64_MAX) { |
515 | 0 | return -1; |
516 | 0 | } |
517 | | |
518 | 0 | i = buffer->current_fragment; |
519 | 0 | fragment_offset = buffer->offset - buffer->fragment_offsets[i]; |
520 | 0 | n = 0; |
521 | 0 | while (n < length) { |
522 | 0 | zip_uint64_t left = ZIP_MIN(length - n, buffer->fragments[i].length - fragment_offset); |
523 | | #if ZIP_UINT64_MAX > SIZE_MAX |
524 | | left = ZIP_MIN(left, SIZE_MAX); |
525 | | #endif |
526 | |
|
527 | 0 | (void)memcpy_s(data + n, (size_t)left, buffer->fragments[i].data + fragment_offset, (size_t)left); |
528 | |
|
529 | 0 | if (left == buffer->fragments[i].length - fragment_offset) { |
530 | 0 | i++; |
531 | 0 | } |
532 | 0 | n += left; |
533 | 0 | fragment_offset = 0; |
534 | 0 | } |
535 | |
|
536 | 0 | buffer->offset += n; |
537 | 0 | buffer->current_fragment = i; |
538 | 0 | return (zip_int64_t)n; |
539 | 0 | } |
540 | | |
541 | | |
542 | 0 | static int buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error) { |
543 | 0 | zip_int64_t new_offset = zip_source_seek_compute_offset(buffer->offset, buffer->size, data, len, error); |
544 | |
|
545 | 0 | if (new_offset < 0) { |
546 | 0 | return -1; |
547 | 0 | } |
548 | | |
549 | 0 | buffer->offset = (zip_uint64_t)new_offset; |
550 | 0 | buffer->current_fragment = buffer_find_fragment(buffer, buffer->offset); |
551 | 0 | return 0; |
552 | 0 | } |
553 | | |
554 | | |
555 | 0 | static zip_int64_t buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *error) { |
556 | 0 | zip_uint64_t copied, i, fragment_offset, capacity; |
557 | |
|
558 | 0 | if (buffer->offset + length + WRITE_FRAGMENT_SIZE - 1 < length) { |
559 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
560 | 0 | return -1; |
561 | 0 | } |
562 | | |
563 | | /* grow buffer if needed */ |
564 | 0 | capacity = buffer_capacity(buffer); |
565 | 0 | if (buffer->offset + length > capacity) { |
566 | 0 | zip_uint64_t needed_fragments = buffer->nfragments + (length - (capacity - buffer->offset) + WRITE_FRAGMENT_SIZE - 1) / WRITE_FRAGMENT_SIZE; |
567 | |
|
568 | 0 | if (needed_fragments > buffer->fragments_capacity) { |
569 | 0 | zip_uint64_t new_capacity = buffer->fragments_capacity; |
570 | |
|
571 | 0 | if (new_capacity == 0) { |
572 | 0 | new_capacity = 16; |
573 | 0 | } |
574 | 0 | while (new_capacity < needed_fragments) { |
575 | 0 | new_capacity *= 2; |
576 | 0 | } |
577 | |
|
578 | 0 | if (!buffer_grow_fragments(buffer, new_capacity, error)) { |
579 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
580 | 0 | return -1; |
581 | 0 | } |
582 | 0 | } |
583 | | |
584 | 0 | while (buffer->nfragments < needed_fragments) { |
585 | 0 | if ((buffer->fragments[buffer->nfragments].data = malloc(WRITE_FRAGMENT_SIZE)) == NULL) { |
586 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
587 | 0 | return -1; |
588 | 0 | } |
589 | 0 | buffer->fragments[buffer->nfragments].length = WRITE_FRAGMENT_SIZE; |
590 | 0 | buffer->nfragments++; |
591 | 0 | capacity += WRITE_FRAGMENT_SIZE; |
592 | 0 | buffer->fragment_offsets[buffer->nfragments] = capacity; |
593 | 0 | } |
594 | 0 | } |
595 | | |
596 | 0 | i = buffer->current_fragment; |
597 | 0 | fragment_offset = buffer->offset - buffer->fragment_offsets[i]; |
598 | 0 | copied = 0; |
599 | 0 | while (copied < length) { |
600 | 0 | zip_uint64_t n = ZIP_MIN(ZIP_MIN(length - copied, buffer->fragments[i].length - fragment_offset), SIZE_MAX); |
601 | | #if ZIP_UINT64_MAX > SIZE_MAX |
602 | | n = ZIP_MIN(n, SIZE_MAX); |
603 | | #endif |
604 | |
|
605 | 0 | (void)memcpy_s(buffer->fragments[i].data + fragment_offset, (size_t)n, data + copied, (size_t)n); |
606 | |
|
607 | 0 | if (n == buffer->fragments[i].length - fragment_offset) { |
608 | 0 | i++; |
609 | 0 | fragment_offset = 0; |
610 | 0 | } |
611 | 0 | else { |
612 | 0 | fragment_offset += n; |
613 | 0 | } |
614 | 0 | copied += n; |
615 | 0 | } |
616 | |
|
617 | 0 | buffer->offset += copied; |
618 | 0 | buffer->current_fragment = i; |
619 | 0 | if (buffer->offset > buffer->size) { |
620 | 0 | buffer->size = buffer->offset; |
621 | 0 | } |
622 | |
|
623 | 0 | return (zip_int64_t)copied; |
624 | 0 | } |