Coverage Report

Created: 2026-12-01 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
26.5k
#define WRITE_FRAGMENT_SIZE (64 * 1024)
41
#endif
42
43
44
struct buffer_fragment {
45
    zip_uint8_t *data;
46
    zip_uint64_t length;
47
    bool free_data; /* whether to free data when freeing fragment */
48
};
49
typedef struct buffer_fragment buffer_fragment_t;
50
51
struct buffer {
52
    buffer_fragment_t *fragments;    /* fragments */
53
    zip_uint64_t *fragment_offsets;  /* offset of each fragment from start of buffer, nfragments+1 entries */
54
    zip_uint64_t nfragments;         /* number of allocated fragments */
55
    zip_uint64_t fragments_capacity; /* size of fragments (number of pointers) */
56
57
    struct buffer *shared_buffer; /* buffer fragments are shared with */
58
59
    zip_uint64_t size;             /* size of buffer */
60
    zip_uint64_t offset;           /* current offset in buffer */
61
    zip_uint64_t current_fragment; /* fragment current offset is in */
62
};
63
64
typedef struct buffer buffer_t;
65
66
struct read_data {
67
    zip_error_t error;
68
    time_t mtime;
69
    zip_file_attributes_t attributes;
70
    buffer_t *in;
71
    buffer_t *out;
72
};
73
74
typedef struct read_data read_data_t;
75
76
static read_data_t *read_data_new(void);
77
static void read_data_free(read_data_t *ctx, bool success);
78
79
/* TODO:
80
    buffer_write
81
*/
82
83
225k
#define buffer_capacity(buffer) ((buffer)->fragment_offsets[(buffer)->nfragments])
84
#define buffer_size(buffer) ((buffer)->size)
85
86
static zip_int64_t buffer_at_eof(const buffer_t *buffer);
87
static buffer_t *buffer_clone(buffer_t *buffer, zip_uint64_t length, zip_error_t *error);
88
static zip_uint64_t buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset);
89
static void buffer_free(buffer_t *buffer, bool success);
90
static bool buffer_grow_fragments(buffer_t *buffer, zip_uint64_t capacity, zip_error_t *error);
91
static bool buffer_is_fragment_shared(const buffer_t *buffer, zip_uint64_t fragment_index);
92
static bool buffer_make_fragment_writable(buffer_t *buffer, zip_uint64_t fragment_index, zip_error_t *error);
93
static buffer_t *buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int free_data, zip_error_t *error);
94
static zip_int64_t buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length);
95
static int buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error);
96
static zip_int64_t buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *);
97
98
static zip_int64_t read_data(void *, void *, zip_uint64_t, zip_source_cmd_t);
99
100
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);
101
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);
102
103
104
26.9k
ZIP_EXTERN zip_source_t *zip_source_buffer(zip_t *za, const void *data, zip_uint64_t len, int freep) {
105
26.9k
    if (za == NULL) {
106
0
        return NULL;
107
0
    }
108
109
26.9k
    return zip_source_buffer_with_attributes_create(data, len, freep, NULL, &za->error);
110
26.9k
}
111
112
113
14.4k
ZIP_EXTERN zip_source_t *zip_source_buffer_create(const void *data, zip_uint64_t len, int freep, zip_error_t *error) {
114
14.4k
    return zip_source_buffer_with_attributes_create(data, len, freep, NULL, error);
115
14.4k
}
116
117
118
53.9k
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) {
119
53.9k
    zip_buffer_fragment_t fragment;
120
121
53.9k
    if (data == NULL) {
122
24.1k
        if (len > 0) {
123
0
            zip_error_set(error, ZIP_ER_INVAL, 0);
124
0
            return NULL;
125
0
        }
126
127
24.1k
        return zip_source_buffer_fragment_with_attributes_create(NULL, 0, freep, attributes, error);
128
24.1k
    }
129
130
29.7k
    fragment.data = (zip_uint8_t *)data;
131
29.7k
    fragment.length = len;
132
133
29.7k
    return zip_source_buffer_fragment_with_attributes_create(&fragment, 1, freep, attributes, error);
134
53.9k
}
135
136
137
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) {
138
0
    if (za == NULL) {
139
0
        return NULL;
140
0
    }
141
142
0
    return zip_source_buffer_fragment_with_attributes_create(fragments, nfragments, freep, NULL, &za->error);
143
0
}
144
145
146
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) {
147
0
    return zip_source_buffer_fragment_with_attributes_create(fragments, nfragments, freep, NULL, error);
148
0
}
149
150
53.9k
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) {
151
53.9k
    read_data_t *ctx;
152
53.9k
    zip_source_t *zs;
153
53.9k
    buffer_t *buffer;
154
53.9k
    zip_uint64_t i;
155
156
53.9k
    if (fragments == NULL && nfragments > 0) {
157
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
158
0
        return NULL;
159
0
    }
160
161
53.9k
    if ((buffer = buffer_new(fragments, nfragments, freep, error)) == NULL) {
162
0
        return NULL;
163
0
    }
164
165
53.9k
    if ((ctx = read_data_new()) == NULL) {
166
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
167
0
        buffer_free(buffer, false);
168
0
        return NULL;
169
0
    }
170
171
53.9k
    ctx->in = buffer;
172
53.9k
    if (attributes) {
173
12.5k
        (void)memcpy_s(&ctx->attributes, sizeof(ctx->attributes), attributes, sizeof(ctx->attributes));
174
12.5k
    }
175
176
53.9k
    if ((zs = zip_source_function_create(read_data, ctx, error)) == NULL) {
177
0
        read_data_free(ctx, false);
178
0
        return NULL;
179
0
    }
180
181
53.9k
    if (freep) {
182
        /* We don't keep zero length fragments, so their data is no longer needed once the source is created. */
183
41.3k
        for (i = 0; i < nfragments; i++) {
184
20.6k
            if (fragments[i].length == 0 && fragments[i].data != NULL) {
185
7.64k
                free(fragments[i].data);
186
7.64k
            }
187
20.6k
        }
188
20.6k
    }
189
190
53.9k
    return zs;
191
53.9k
}
192
193
194
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) {
195
0
    return zip_source_buffer_with_attributes_create(data, len, freep, attributes, &za->error);
196
0
}
197
198
2.11M
static zip_int64_t read_data(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
199
2.11M
    read_data_t *ctx = (read_data_t *)state;
200
201
2.11M
    switch (cmd) {
202
0
    case ZIP_SOURCE_AT_EOF:
203
0
        return buffer_at_eof(ctx->in);
204
205
5.13k
    case ZIP_SOURCE_BEGIN_WRITE:
206
5.13k
        if ((ctx->out = buffer_new(NULL, 0, 0, &ctx->error)) == NULL) {
207
0
            return -1;
208
0
        }
209
5.13k
        ctx->out->offset = 0;
210
5.13k
        ctx->out->current_fragment = 0;
211
5.13k
        return 0;
212
213
0
    case ZIP_SOURCE_BEGIN_WRITE_CLONING:
214
0
        if ((ctx->out = buffer_clone(ctx->in, len, &ctx->error)) == NULL) {
215
0
            return -1;
216
0
        }
217
0
        ctx->out->offset = len;
218
0
        ctx->out->current_fragment = ctx->out->nfragments;
219
0
        return 0;
220
221
51.6k
    case ZIP_SOURCE_CLOSE:
222
51.6k
        return 0;
223
224
5.13k
    case ZIP_SOURCE_COMMIT_WRITE:
225
5.13k
        buffer_free(ctx->in, true);
226
5.13k
        ctx->in = ctx->out;
227
5.13k
        ctx->out = NULL;
228
5.13k
        return 0;
229
230
240k
    case ZIP_SOURCE_ERROR:
231
240k
        return zip_error_to_data(&ctx->error, data, len);
232
233
53.9k
    case ZIP_SOURCE_FREE:
234
53.9k
        read_data_free(ctx, true);
235
53.9k
        return 0;
236
237
70.5k
    case ZIP_SOURCE_GET_FILE_ATTRIBUTES: {
238
70.5k
        if (len < sizeof(ctx->attributes)) {
239
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
240
0
            return -1;
241
0
        }
242
243
70.5k
        (void)memcpy_s(data, sizeof(ctx->attributes), &ctx->attributes, sizeof(ctx->attributes));
244
245
70.5k
        return sizeof(ctx->attributes);
246
70.5k
    }
247
248
51.6k
    case ZIP_SOURCE_OPEN:
249
51.6k
        ctx->in->offset = 0;
250
51.6k
        ctx->in->current_fragment = 0;
251
51.6k
        return 0;
252
253
405k
    case ZIP_SOURCE_READ:
254
405k
        if (len > ZIP_INT64_MAX) {
255
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
256
0
            return -1;
257
0
        }
258
405k
        return buffer_read(ctx->in, data, len);
259
260
204
    case ZIP_SOURCE_REMOVE: {
261
204
        buffer_t *empty = buffer_new(NULL, 0, 0, &ctx->error);
262
204
        if (empty == NULL) {
263
0
            return -1;
264
0
        }
265
266
204
        buffer_free(ctx->in, true);
267
204
        ctx->in = empty;
268
204
        return 0;
269
204
    }
270
271
0
    case ZIP_SOURCE_ROLLBACK_WRITE:
272
0
        buffer_free(ctx->out, true);
273
0
        ctx->out = NULL;
274
0
        return 0;
275
276
538k
    case ZIP_SOURCE_SEEK:
277
538k
        return buffer_seek(ctx->in, data, len, &ctx->error);
278
279
43.1k
    case ZIP_SOURCE_SEEK_WRITE:
280
43.1k
        return buffer_seek(ctx->out, data, len, &ctx->error);
281
282
242k
    case ZIP_SOURCE_STAT: {
283
242k
        zip_stat_t *st;
284
285
242k
        if (len < sizeof(*st)) {
286
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
287
0
            return -1;
288
0
        }
289
290
242k
        st = (zip_stat_t *)data;
291
292
242k
        zip_stat_init(st);
293
242k
        st->mtime = ctx->mtime;
294
242k
        st->size = ctx->in->size;
295
242k
        st->comp_size = st->size;
296
242k
        st->comp_method = ZIP_CM_STORE;
297
242k
        st->encryption_method = ZIP_EM_NONE;
298
242k
        st->valid = ZIP_STAT_MTIME | ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD;
299
300
242k
        return sizeof(*st);
301
242k
    }
302
303
53.9k
    case ZIP_SOURCE_SUPPORTS:
304
53.9k
        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);
305
306
14.3k
    case ZIP_SOURCE_TELL:
307
14.3k
        if (ctx->in->offset > ZIP_INT64_MAX) {
308
0
            zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW);
309
0
            return -1;
310
0
        }
311
14.3k
        return (zip_int64_t)ctx->in->offset;
312
313
314
110k
    case ZIP_SOURCE_TELL_WRITE:
315
110k
        if (ctx->out->offset > ZIP_INT64_MAX) {
316
0
            zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW);
317
0
            return -1;
318
0
        }
319
110k
        return (zip_int64_t)ctx->out->offset;
320
321
225k
    case ZIP_SOURCE_WRITE:
322
225k
        if (len > ZIP_INT64_MAX) {
323
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
324
0
            return -1;
325
0
        }
326
225k
        return buffer_write(ctx->out, data, len, &ctx->error);
327
328
0
    default:
329
0
        zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
330
0
        return -1;
331
2.11M
    }
332
2.11M
}
333
334
335
0
static buffer_t *buffer_clone(buffer_t *buffer, zip_uint64_t offset, zip_error_t *error) {
336
0
    zip_uint64_t fragment, fragment_offset, waste;
337
0
    buffer_t *clone;
338
0
    size_t i;
339
340
0
    if (offset == 0) {
341
0
        return buffer_new(NULL, 0, 1, error);
342
0
    }
343
344
0
    if (offset > buffer->size) {
345
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
346
0
        return NULL;
347
0
    }
348
0
    if (buffer->shared_buffer != NULL) {
349
0
        zip_error_set(error, ZIP_ER_INUSE, 0);
350
0
        return NULL;
351
0
    }
352
353
0
    fragment = buffer_find_fragment(buffer, offset);
354
0
    fragment_offset = offset - buffer->fragment_offsets[fragment];
355
356
0
    if (fragment_offset == 0) {
357
        /* We can't be at beginning of fragment zero if offset > 0. */
358
0
        fragment--;
359
0
        fragment_offset = buffer->fragments[fragment].length;
360
0
    }
361
362
0
    waste = buffer->fragments[fragment].length - fragment_offset;
363
0
    if (waste > offset) {
364
0
        zip_error_set(error, ZIP_ER_OPNOTSUPP, 0);
365
0
        return NULL;
366
0
    }
367
368
0
    if ((clone = buffer_new(NULL, 0, 1, error)) == NULL) {
369
0
        return NULL;
370
0
    }
371
0
    if (!buffer_grow_fragments(clone, fragment + 1, error)) {
372
0
        buffer_free(clone, false);
373
0
        return NULL;
374
0
    }
375
376
0
    for (i = 0; i <= fragment; i++) {
377
0
        clone->fragments[i] = buffer->fragments[i];
378
0
        clone->fragment_offsets[i] = buffer->fragment_offsets[i];
379
0
    }
380
381
0
#ifndef __clang_analyzer__
382
    /* clone->fragments can't be null, since it was created with at least one fragment */
383
0
    clone->fragments[fragment].length = fragment_offset;
384
0
#endif
385
0
    clone->nfragments = fragment + 1;
386
0
    clone->fragment_offsets[clone->nfragments] = offset;
387
0
    clone->size = offset;
388
389
0
    buffer->shared_buffer = clone;
390
0
    clone->shared_buffer = buffer;
391
392
0
    return clone;
393
0
}
394
395
396
340k
static zip_uint64_t buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset) {
397
340k
    zip_uint64_t low, high, mid;
398
399
340k
    if (buffer->nfragments == 0) {
400
0
        return 0;
401
0
    }
402
403
340k
    low = 0;
404
340k
    high = buffer->nfragments - 1;
405
406
341k
    while (low < high) {
407
19.8k
        mid = (high - low) / 2 + low;
408
19.8k
        if (buffer->fragment_offsets[mid] > offset) {
409
0
            high = mid - 1;
410
0
        }
411
19.8k
        else if (mid == buffer->nfragments || buffer->fragment_offsets[mid + 1] > offset) {
412
18.6k
            return mid;
413
18.6k
        }
414
1.16k
        else {
415
1.16k
            low = mid + 1;
416
1.16k
        }
417
19.8k
    }
418
419
321k
    return low;
420
340k
}
421
422
423
113k
static void buffer_free(buffer_t *buffer, bool success) {
424
113k
    zip_uint64_t i;
425
426
113k
    if (buffer == NULL) {
427
53.9k
        return;
428
53.9k
    }
429
430
59.2k
    if (success) {
431
86.7k
        for (i = 0; i < buffer->nfragments; i++) {
432
27.4k
            if (buffer->fragments[i].free_data && !buffer_is_fragment_shared(buffer, i)) {
433
18.3k
                free(buffer->fragments[i].data);
434
18.3k
            }
435
27.4k
        }
436
59.2k
    }
437
59.2k
    if (buffer->shared_buffer != NULL) {
438
0
        buffer->shared_buffer->shared_buffer = NULL;
439
0
    }
440
59.2k
    free(buffer->fragments);
441
59.2k
    free(buffer->fragment_offsets);
442
59.2k
    free(buffer);
443
59.2k
}
444
445
446
34.9k
static bool buffer_grow_fragments(buffer_t *buffer, zip_uint64_t capacity, zip_error_t *error) {
447
34.9k
    zip_uint64_t additional_fragments;
448
34.9k
    zip_uint64_t offset_capacity = buffer->fragments_capacity + 1;
449
450
34.9k
    if (capacity <= buffer->fragments_capacity) {
451
0
        return true;
452
0
    }
453
454
34.9k
    additional_fragments = capacity - buffer->fragments_capacity;
455
456
34.9k
    if (!ZIP_REALLOC(buffer->fragments, buffer->fragments_capacity, additional_fragments, error)) {
457
0
        return false;
458
0
    }
459
    /* 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. */
460
34.9k
    if (!ZIP_REALLOC(buffer->fragment_offsets, offset_capacity, additional_fragments, error)) {
461
0
        buffer->fragments_capacity -= additional_fragments;
462
0
        return false;
463
0
    }
464
465
34.9k
    return true;
466
34.9k
}
467
468
469
59.2k
static buffer_t *buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int free_data, zip_error_t *error) {
470
59.2k
    buffer_t *buffer;
471
472
59.2k
    if ((buffer = malloc(sizeof(*buffer))) == NULL) {
473
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
474
0
        return NULL;
475
0
    }
476
477
59.2k
    buffer->offset = 0;
478
59.2k
    buffer->size = 0;
479
59.2k
    buffer->fragments = NULL;
480
59.2k
    buffer->fragment_offsets = NULL;
481
59.2k
    buffer->nfragments = 0;
482
59.2k
    buffer->fragments_capacity = 0;
483
59.2k
    buffer->shared_buffer = NULL;
484
485
59.2k
    if (nfragments == 0) {
486
29.4k
        if ((buffer->fragment_offsets = malloc(sizeof(buffer->fragment_offsets[0]))) == NULL) {
487
0
            free(buffer);
488
0
            zip_error_set(error, ZIP_ER_MEMORY, 0);
489
0
            return NULL;
490
0
        }
491
29.4k
        buffer->fragment_offsets[0] = 0;
492
29.4k
    }
493
29.7k
    else {
494
29.7k
        zip_uint64_t i, j, offset;
495
496
29.7k
        if (!buffer_grow_fragments(buffer, nfragments, NULL)) {
497
0
            zip_error_set(error, ZIP_ER_MEMORY, 0);
498
0
            buffer_free(buffer, false);
499
0
            return NULL;
500
0
        }
501
502
29.7k
        offset = 0;
503
59.5k
        for (i = 0, j = 0; i < nfragments; i++) {
504
29.7k
            if (fragments[i].length == 0) {
505
7.64k
                continue;
506
7.64k
            }
507
22.1k
            if (fragments[i].data == NULL) {
508
0
                zip_error_set(error, ZIP_ER_INVAL, 0);
509
0
                buffer_free(buffer, false);
510
0
                return NULL;
511
0
            }
512
22.1k
            buffer->fragments[j].data = fragments[i].data;
513
22.1k
            buffer->fragments[j].length = fragments[i].length;
514
22.1k
            buffer->fragments[j].free_data = free_data;
515
22.1k
            buffer->fragment_offsets[j] = offset;
516
22.1k
            if (!ASSIGN_UNSIGNED_ADD_FITS(offset, offset, fragments[i].length)) {
517
0
                zip_error_set(error, ZIP_ER_INVAL, 0);
518
0
                buffer_free(buffer, false);
519
0
                return NULL;
520
0
            }
521
22.1k
            j++;
522
22.1k
        }
523
29.7k
        buffer->nfragments = j;
524
29.7k
        buffer->fragment_offsets[buffer->nfragments] = offset;
525
29.7k
        buffer->size = offset;
526
29.7k
    }
527
528
59.2k
    return buffer;
529
59.2k
}
530
531
0
static zip_int64_t buffer_at_eof(const buffer_t *buffer) {
532
0
    return buffer->offset == buffer->size;
533
0
}
534
535
405k
static zip_int64_t buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) {
536
405k
    zip_uint64_t n, i, fragment_offset;
537
538
405k
    length = ZIP_MIN(length, buffer->size - buffer->offset);
539
540
405k
    if (length == 0) {
541
38.0k
        return 0;
542
38.0k
    }
543
366k
    if (length > ZIP_INT64_MAX) {
544
0
        return -1;
545
0
    }
546
547
366k
    i = buffer->current_fragment;
548
366k
    fragment_offset = buffer->offset - buffer->fragment_offsets[i];
549
366k
    n = 0;
550
734k
    while (n < length) {
551
367k
        zip_uint64_t left = ZIP_MIN(length - n, buffer->fragments[i].length - fragment_offset);
552
#if ZIP_UINT64_MAX > SIZE_MAX
553
        left = ZIP_MIN(left, SIZE_MAX);
554
#endif
555
556
367k
        (void)memcpy_s(data + n, (size_t)left, buffer->fragments[i].data + fragment_offset, (size_t)left);
557
558
367k
        if (left == buffer->fragments[i].length - fragment_offset) {
559
34.7k
            i++;
560
34.7k
        }
561
367k
        n += left;
562
367k
        fragment_offset = 0;
563
367k
    }
564
565
366k
    buffer->offset += n;
566
366k
    buffer->current_fragment = i;
567
366k
    return (zip_int64_t)n;
568
366k
}
569
570
571
581k
static int buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error) {
572
581k
    zip_int64_t new_offset = zip_source_seek_compute_offset(buffer->offset, buffer->size, data, len, error);
573
574
581k
    if (new_offset < 0) {
575
240k
        return -1;
576
240k
    }
577
578
340k
    buffer->offset = (zip_uint64_t)new_offset;
579
340k
    buffer->current_fragment = buffer_find_fragment(buffer, buffer->offset);
580
340k
    return 0;
581
581k
}
582
583
584
225k
static zip_int64_t buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *error) {
585
225k
    zip_uint64_t copied, i, fragment_offset, capacity;
586
587
225k
    if (!UNSIGNED_ADD_THREE_FITS(buffer->offset, length, WRITE_FRAGMENT_SIZE - 1)) {
588
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
589
0
        return -1;
590
0
    }
591
592
    /* grow buffer if needed */
593
225k
    capacity = buffer_capacity(buffer);
594
225k
    if (buffer->offset + length > capacity) {
595
5.31k
        zip_uint64_t needed_fragments = buffer->nfragments + (length - (capacity - buffer->offset) + WRITE_FRAGMENT_SIZE - 1) / WRITE_FRAGMENT_SIZE;
596
597
5.31k
        if (needed_fragments > buffer->fragments_capacity) {
598
5.13k
            zip_uint64_t new_capacity = buffer->fragments_capacity;
599
600
5.13k
            if (new_capacity == 0) {
601
5.13k
                new_capacity = 16;
602
5.13k
            }
603
5.13k
            while (new_capacity < needed_fragments) {
604
0
                new_capacity *= 2;
605
0
            }
606
607
5.13k
            if (!buffer_grow_fragments(buffer, new_capacity, error)) {
608
0
                zip_error_set(error, ZIP_ER_MEMORY, 0);
609
0
                return -1;
610
0
            }
611
5.13k
        }
612
613
10.6k
        while (buffer->nfragments < needed_fragments) {
614
5.31k
            if ((buffer->fragments[buffer->nfragments].data = malloc(WRITE_FRAGMENT_SIZE)) == NULL) {
615
0
                zip_error_set(error, ZIP_ER_MEMORY, 0);
616
0
                return -1;
617
0
            }
618
5.31k
            buffer->fragments[buffer->nfragments].length = WRITE_FRAGMENT_SIZE;
619
5.31k
            buffer->fragments[buffer->nfragments].free_data = true;
620
5.31k
            buffer->nfragments++;
621
5.31k
            capacity += WRITE_FRAGMENT_SIZE;
622
5.31k
            buffer->fragment_offsets[buffer->nfragments] = capacity;
623
5.31k
        }
624
5.31k
    }
625
626
225k
    i = buffer->current_fragment;
627
225k
    fragment_offset = buffer->offset - buffer->fragment_offsets[i];
628
225k
    copied = 0;
629
450k
    while (copied < length) {
630
225k
        zip_uint64_t n = ZIP_MIN(ZIP_MIN(length - copied, buffer->fragments[i].length - fragment_offset), SIZE_MAX);
631
#if ZIP_UINT64_MAX > SIZE_MAX
632
        n = ZIP_MIN(n, SIZE_MAX);
633
#endif
634
635
225k
        if (!buffer_make_fragment_writable(buffer, i, error)) {
636
0
            return -1;
637
0
        }
638
225k
        (void)memcpy_s(buffer->fragments[i].data + fragment_offset, (size_t)n, data + copied, (size_t)n);
639
640
225k
        if (n == buffer->fragments[i].length - fragment_offset) {
641
192
            i++;
642
192
            fragment_offset = 0;
643
192
        }
644
225k
        else {
645
225k
            fragment_offset += n;
646
225k
        }
647
225k
        copied += n;
648
225k
    }
649
650
225k
    buffer->offset += copied;
651
225k
    buffer->current_fragment = i;
652
225k
    if (buffer->offset > buffer->size) {
653
159k
        buffer->size = buffer->offset;
654
159k
    }
655
656
225k
    return (zip_int64_t)copied;
657
225k
}
658
659
660
243k
static bool buffer_is_fragment_shared(const buffer_t *buffer, zip_uint64_t fragment_index) {
661
243k
    if (buffer->shared_buffer == NULL) {
662
243k
        return false;
663
243k
    }
664
665
0
    if (fragment_index >= buffer->shared_buffer->nfragments) {
666
0
        return false;
667
0
    }
668
669
0
    return buffer->fragments[fragment_index].data == buffer->shared_buffer->fragments[fragment_index].data;
670
0
}
671
672
673
225k
static bool buffer_make_fragment_writable(buffer_t *buffer, zip_uint64_t fragment_index, zip_error_t *error) {
674
225k
    if (!buffer_is_fragment_shared(buffer, fragment_index)) {
675
225k
        return true;
676
225k
    }
677
678
#if ZIP_UINT64_MAX > SIZE_MAX
679
    if (buffer->fragments[fragment_index].length > SIZE_MAX) {
680
        zip_error_set(error, ZIP_ER_MEMORY, 0);
681
        return false;
682
    }
683
#endif
684
0
    zip_uint8_t *new_data = malloc(buffer->fragments[fragment_index].length);
685
0
    if (new_data == NULL) {
686
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
687
0
        return false;
688
0
    }
689
690
0
    (void)memcpy_s(new_data, buffer->fragments[fragment_index].length, buffer->fragments[fragment_index].data, buffer->fragments[fragment_index].length);
691
692
    /* Since the fragment was shared, we don't need to free the old data. */
693
0
    buffer->fragments[fragment_index].data = new_data;
694
0
    buffer->fragments[fragment_index].free_data = true;
695
696
0
    return true;
697
0
}
698
699
53.9k
static read_data_t *read_data_new(void) {
700
53.9k
    read_data_t *ctx = (read_data_t *)malloc(sizeof(read_data_t));
701
53.9k
    if (ctx == NULL) {
702
0
        return NULL;
703
0
    }
704
705
53.9k
    zip_error_init(&ctx->error);
706
53.9k
    ctx->in = NULL;
707
53.9k
    ctx->out = NULL;
708
53.9k
    ctx->mtime = time(NULL);
709
53.9k
    zip_file_attributes_init(&ctx->attributes);
710
53.9k
    return ctx;
711
53.9k
}
712
713
53.9k
static void read_data_free(read_data_t *ctx, bool success) {
714
53.9k
    if (ctx == NULL) {
715
0
        return;
716
0
    }
717
718
53.9k
    zip_error_fini(&ctx->error);
719
53.9k
    buffer_free(ctx->in, success);
720
53.9k
    buffer_free(ctx->out, true);
721
    /* TODO: attributes */
722
723
53.9k
    free(ctx);
724
53.9k
}