Coverage Report

Created: 2025-07-23 08:18

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