Coverage Report

Created: 2026-08-25 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
104
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
104
    zip_buffer_fragment_t fragment;
104
105
104
    if (data == NULL) {
106
104
        if (len > 0) {
107
0
            zip_error_set(error, ZIP_ER_INVAL, 0);
108
0
            return NULL;
109
0
        }
110
111
104
        return zip_source_buffer_fragment_with_attributes_create(NULL, 0, freep, attributes, error);
112
104
    }
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
104
}
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
104
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
104
    struct read_data *ctx;
136
104
    zip_source_t *zs;
137
104
    buffer_t *buffer;
138
139
104
    if (fragments == NULL && nfragments > 0) {
140
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
141
0
        return NULL;
142
0
    }
143
144
104
    if ((buffer = buffer_new(fragments, nfragments, freep, error)) == NULL) {
145
0
        return NULL;
146
0
    }
147
148
104
    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
104
    ctx->in = buffer;
155
104
    ctx->out = NULL;
156
104
    ctx->mtime = time(NULL);
157
104
    if (attributes) {
158
104
        (void)memcpy_s(&ctx->attributes, sizeof(ctx->attributes), attributes, sizeof(ctx->attributes));
159
104
    }
160
0
    else {
161
0
        zip_file_attributes_init(&ctx->attributes);
162
0
    }
163
104
    zip_error_init(&ctx->error);
164
165
104
    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
104
    return zs;
172
104
}
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
678
static zip_int64_t read_data(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
180
678
    struct read_data *ctx = (struct read_data *)state;
181
182
678
    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
94
    case ZIP_SOURCE_CLOSE:
203
94
        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
104
    case ZIP_SOURCE_FREE:
215
104
        buffer_free(ctx->in);
216
104
        buffer_free(ctx->out);
217
104
        free(ctx);
218
104
        return 0;
219
220
72
    case ZIP_SOURCE_GET_FILE_ATTRIBUTES: {
221
72
        if (len < sizeof(ctx->attributes)) {
222
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
223
0
            return -1;
224
0
        }
225
226
72
        (void)memcpy_s(data, sizeof(ctx->attributes), &ctx->attributes, sizeof(ctx->attributes));
227
228
72
        return sizeof(ctx->attributes);
229
72
    }
230
231
94
    case ZIP_SOURCE_OPEN:
232
94
        ctx->in->offset = 0;
233
94
        ctx->in->current_fragment = 0;
234
94
        return 0;
235
236
44
    case ZIP_SOURCE_READ:
237
44
        if (len > ZIP_INT64_MAX) {
238
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
239
0
            return -1;
240
0
        }
241
44
        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
44
    case ZIP_SOURCE_SEEK:
260
44
        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
122
    case ZIP_SOURCE_STAT: {
266
122
        zip_stat_t *st;
267
268
122
        if (len < sizeof(*st)) {
269
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
270
0
            return -1;
271
0
        }
272
273
122
        st = (zip_stat_t *)data;
274
275
122
        zip_stat_init(st);
276
122
        st->mtime = ctx->mtime;
277
122
        st->size = ctx->in->size;
278
122
        st->comp_size = st->size;
279
122
        st->comp_method = ZIP_CM_STORE;
280
122
        st->encryption_method = ZIP_EM_NONE;
281
122
        st->valid = ZIP_STAT_MTIME | ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD;
282
283
122
        return sizeof(*st);
284
122
    }
285
286
104
    case ZIP_SOURCE_SUPPORTS:
287
104
        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
678
    }
315
678
}
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
44
static zip_uint64_t buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset) {
374
44
    zip_uint64_t low, high, mid;
375
376
44
    if (buffer->nfragments == 0) {
377
44
        return 0;
378
44
    }
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
208
static void buffer_free(buffer_t *buffer) {
401
208
    zip_uint64_t i;
402
403
208
    if (buffer == NULL) {
404
104
        return;
405
104
    }
406
407
104
    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
104
    for (i = buffer->first_owned_fragment; i < buffer->nfragments; i++) {
415
0
        free(buffer->fragments[i].data);
416
0
    }
417
104
    free(buffer->fragments);
418
104
    free(buffer->fragment_offsets);
419
104
    free(buffer);
420
104
}
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
104
static buffer_t *buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int free_data, zip_error_t *error) {
447
104
    buffer_t *buffer;
448
104
    bool have_empty_fragment = false;
449
450
104
    if ((buffer = malloc(sizeof(*buffer))) == NULL) {
451
0
        return NULL;
452
0
    }
453
454
104
    buffer->offset = 0;
455
104
    buffer->first_owned_fragment = 0;
456
104
    buffer->size = 0;
457
104
    buffer->fragments = NULL;
458
104
    buffer->fragment_offsets = NULL;
459
104
    buffer->nfragments = 0;
460
104
    buffer->fragments_capacity = 0;
461
104
    buffer->shared_buffer = NULL;
462
104
    buffer->shared_fragments = 0;
463
464
104
    if (nfragments == 0) {
465
104
        if ((buffer->fragment_offsets = malloc(sizeof(buffer->fragment_offsets[0]))) == NULL) {
466
0
            free(buffer);
467
0
            zip_error_set(error, ZIP_ER_MEMORY, 0);
468
0
            return NULL;
469
0
        }
470
104
        buffer->fragment_offsets[0] = 0;
471
104
    }
472
0
    else {
473
0
        zip_uint64_t i, j, offset;
474
475
0
        if (!buffer_grow_fragments(buffer, nfragments, NULL)) {
476
0
            zip_error_set(error, ZIP_ER_MEMORY, 0);
477
0
            buffer_free(buffer);
478
0
            return NULL;
479
0
        }
480
481
0
        offset = 0;
482
0
        for (i = 0, j = 0; i < nfragments; i++) {
483
0
            if (fragments[i].length == 0) {
484
0
                if (fragments[i].data != NULL && free_data) {
485
0
                    have_empty_fragment = true;
486
0
                }
487
0
                continue;
488
0
            }
489
0
            if (fragments[i].data == NULL) {
490
0
                zip_error_set(error, ZIP_ER_INVAL, 0);
491
0
                buffer_free(buffer);
492
0
                return NULL;
493
0
            }
494
0
            buffer->fragments[j].data = fragments[i].data;
495
0
            buffer->fragments[j].length = fragments[i].length;
496
0
            buffer->fragment_offsets[j] = offset;
497
0
            if (offset + fragments[i].length < offset) {
498
0
                zip_error_set(error, ZIP_ER_INVAL, 0);
499
0
                buffer_free(buffer);
500
0
                return NULL;
501
0
            }
502
0
            offset += fragments[i].length;
503
0
            j++;
504
0
        }
505
0
        buffer->nfragments = j;
506
0
        buffer->first_owned_fragment = free_data ? 0 : buffer->nfragments;
507
0
        buffer->fragment_offsets[buffer->nfragments] = offset;
508
0
        buffer->size = offset;
509
510
0
        if (have_empty_fragment) {
511
0
            for (i = 0; i < nfragments; i++) {
512
0
                if (fragments[i].length == 0 && fragments[i].data != NULL) {
513
0
                    free(fragments[i].data);
514
0
                }
515
0
            }
516
0
        }
517
0
    }
518
519
104
    return buffer;
520
104
}
521
522
0
static zip_int64_t buffer_at_eof(const buffer_t *buffer) {
523
0
    return buffer->offset == buffer->size;
524
0
}
525
526
44
static zip_int64_t buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) {
527
44
    zip_uint64_t n, i, fragment_offset;
528
529
44
    length = ZIP_MIN(length, buffer->size - buffer->offset);
530
531
44
    if (length == 0) {
532
44
        return 0;
533
44
    }
534
0
    if (length > ZIP_INT64_MAX) {
535
0
        return -1;
536
0
    }
537
538
0
    i = buffer->current_fragment;
539
0
    fragment_offset = buffer->offset - buffer->fragment_offsets[i];
540
0
    n = 0;
541
0
    while (n < length) {
542
0
        zip_uint64_t left = ZIP_MIN(length - n, buffer->fragments[i].length - fragment_offset);
543
#if ZIP_UINT64_MAX > SIZE_MAX
544
        left = ZIP_MIN(left, SIZE_MAX);
545
#endif
546
547
0
        (void)memcpy_s(data + n, (size_t)left, buffer->fragments[i].data + fragment_offset, (size_t)left);
548
549
0
        if (left == buffer->fragments[i].length - fragment_offset) {
550
0
            i++;
551
0
        }
552
0
        n += left;
553
0
        fragment_offset = 0;
554
0
    }
555
556
0
    buffer->offset += n;
557
0
    buffer->current_fragment = i;
558
0
    return (zip_int64_t)n;
559
0
}
560
561
562
44
static int buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error) {
563
44
    zip_int64_t new_offset = zip_source_seek_compute_offset(buffer->offset, buffer->size, data, len, error);
564
565
44
    if (new_offset < 0) {
566
0
        return -1;
567
0
    }
568
569
44
    buffer->offset = (zip_uint64_t)new_offset;
570
44
    buffer->current_fragment = buffer_find_fragment(buffer, buffer->offset);
571
44
    return 0;
572
44
}
573
574
575
0
static zip_int64_t buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *error) {
576
0
    zip_uint64_t copied, i, fragment_offset, capacity;
577
578
0
    if (buffer->offset + length + WRITE_FRAGMENT_SIZE - 1 < length) {
579
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
580
0
        return -1;
581
0
    }
582
583
    /* grow buffer if needed */
584
0
    capacity = buffer_capacity(buffer);
585
0
    if (buffer->offset + length > capacity) {
586
0
        zip_uint64_t needed_fragments = buffer->nfragments + (length - (capacity - buffer->offset) + WRITE_FRAGMENT_SIZE - 1) / WRITE_FRAGMENT_SIZE;
587
588
0
        if (needed_fragments > buffer->fragments_capacity) {
589
0
            zip_uint64_t new_capacity = buffer->fragments_capacity;
590
591
0
            if (new_capacity == 0) {
592
0
                new_capacity = 16;
593
0
            }
594
0
            while (new_capacity < needed_fragments) {
595
0
                new_capacity *= 2;
596
0
            }
597
598
0
            if (!buffer_grow_fragments(buffer, new_capacity, error)) {
599
0
                zip_error_set(error, ZIP_ER_MEMORY, 0);
600
0
                return -1;
601
0
            }
602
0
        }
603
604
0
        while (buffer->nfragments < needed_fragments) {
605
0
            if ((buffer->fragments[buffer->nfragments].data = malloc(WRITE_FRAGMENT_SIZE)) == NULL) {
606
0
                zip_error_set(error, ZIP_ER_MEMORY, 0);
607
0
                return -1;
608
0
            }
609
0
            buffer->fragments[buffer->nfragments].length = WRITE_FRAGMENT_SIZE;
610
0
            buffer->nfragments++;
611
0
            capacity += WRITE_FRAGMENT_SIZE;
612
0
            buffer->fragment_offsets[buffer->nfragments] = capacity;
613
0
        }
614
0
    }
615
616
0
    i = buffer->current_fragment;
617
0
    fragment_offset = buffer->offset - buffer->fragment_offsets[i];
618
0
    copied = 0;
619
0
    while (copied < length) {
620
0
        zip_uint64_t n = ZIP_MIN(ZIP_MIN(length - copied, buffer->fragments[i].length - fragment_offset), SIZE_MAX);
621
#if ZIP_UINT64_MAX > SIZE_MAX
622
        n = ZIP_MIN(n, SIZE_MAX);
623
#endif
624
625
0
        (void)memcpy_s(buffer->fragments[i].data + fragment_offset, (size_t)n, data + copied, (size_t)n);
626
627
0
        if (n == buffer->fragments[i].length - fragment_offset) {
628
0
            i++;
629
0
            fragment_offset = 0;
630
0
        }
631
0
        else {
632
0
            fragment_offset += n;
633
0
        }
634
0
        copied += n;
635
0
    }
636
637
0
    buffer->offset += copied;
638
0
    buffer->current_fragment = i;
639
0
    if (buffer->offset > buffer->size) {
640
0
        buffer->size = buffer->offset;
641
0
    }
642
643
0
    return (zip_int64_t)copied;
644
0
}