VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Common/libzip/zip_source_buffer.c
diff options
context:
space:
mode:
authorDLL125 <134442578+DLL125@users.noreply.github.com>2023-05-25 12:52:53 +0200
committerGitHub <noreply@github.com>2023-05-25 12:52:53 +0200
commit1fc4168b81f565feab409b92ccb61c57a1c550eb (patch)
treef5b54c9699aabb120f4cfa3240164fc17fd1d075 /src/Common/libzip/zip_source_buffer.c
parentb872702309b20f2467d58652c3b00493ac4058bd (diff)
downloadVeraCrypt-1fc4168b81f565feab409b92ccb61c57a1c550eb.tar.gz
VeraCrypt-1fc4168b81f565feab409b92ccb61c57a1c550eb.zip
Update Libzip to latest 1.9.2 (#1071)
* Libzip 1.9.2 Updated Libzip to latest version 1.9.2 and changed version number in the config.h from 1.7.3 to 1.9.2. Not sure if anything else needs to be tweaked :) * Modified Libzip to work with Visual studio * Update README.md Update libzip copyright. * Added the missing files. I've added the missing files zipconf.h and config.h, I've missed those sorry for that!
Diffstat (limited to 'src/Common/libzip/zip_source_buffer.c')
-rw-r--r--src/Common/libzip/zip_source_buffer.c434
1 files changed, 219 insertions, 215 deletions
diff --git a/src/Common/libzip/zip_source_buffer.c b/src/Common/libzip/zip_source_buffer.c
index 519d2c30..04cd4f0a 100644
--- a/src/Common/libzip/zip_source_buffer.c
+++ b/src/Common/libzip/zip_source_buffer.c
@@ -1,9 +1,9 @@
/*
zip_source_buffer.c -- create zip data source from buffer
- Copyright (C) 1999-2019 Dieter Baron and Thomas Klausner
+ Copyright (C) 1999-2022 Dieter Baron and Thomas Klausner
This file is part of libzip, a library to manipulate ZIP archives.
- The authors can be contacted at <libzip@nih.at>
+ The authors can be contacted at <info@libzip.org>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@@ -87,7 +87,7 @@ zip_source_t *zip_source_buffer_fragment_with_attributes_create(const zip_buffer
ZIP_EXTERN zip_source_t *
zip_source_buffer(zip_t *za, const void *data, zip_uint64_t len, int freep) {
if (za == NULL)
- return NULL;
+ return NULL;
return zip_source_buffer_with_attributes_create(data, len, freep, NULL, &za->error);
}
@@ -104,12 +104,12 @@ zip_source_buffer_with_attributes_create(const void *data, zip_uint64_t len, int
zip_buffer_fragment_t fragment;
if (data == NULL) {
- if (len > 0) {
- zip_error_set(error, ZIP_ER_INVAL, 0);
- return NULL;
- }
+ if (len > 0) {
+ zip_error_set(error, ZIP_ER_INVAL, 0);
+ return NULL;
+ }
- return zip_source_buffer_fragment_with_attributes_create(NULL, 0, freep, attributes, error);
+ return zip_source_buffer_fragment_with_attributes_create(NULL, 0, freep, attributes, error);
}
fragment.data = (zip_uint8_t *)data;
@@ -122,7 +122,7 @@ zip_source_buffer_with_attributes_create(const void *data, zip_uint64_t len, int
ZIP_EXTERN zip_source_t *
zip_source_buffer_fragment(zip_t *za, const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep) {
if (za == NULL) {
- return NULL;
+ return NULL;
}
return zip_source_buffer_fragment_with_attributes_create(fragments, nfragments, freep, NULL, &za->error);
@@ -141,35 +141,35 @@ zip_source_buffer_fragment_with_attributes_create(const zip_buffer_fragment_t *f
buffer_t *buffer;
if (fragments == NULL && nfragments > 0) {
- zip_error_set(error, ZIP_ER_INVAL, 0);
- return NULL;
+ zip_error_set(error, ZIP_ER_INVAL, 0);
+ return NULL;
}
if ((buffer = buffer_new(fragments, nfragments, freep, error)) == NULL) {
- return NULL;
+ return NULL;
}
if ((ctx = (struct read_data *)malloc(sizeof(*ctx))) == NULL) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- buffer_free(buffer);
- return NULL;
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ buffer_free(buffer);
+ return NULL;
}
ctx->in = buffer;
ctx->out = NULL;
ctx->mtime = time(NULL);
if (attributes) {
- memcpy(&ctx->attributes, attributes, sizeof(ctx->attributes));
+ memcpy(&ctx->attributes, attributes, sizeof(ctx->attributes));
}
else {
- zip_file_attributes_init(&ctx->attributes);
+ zip_file_attributes_init(&ctx->attributes);
}
zip_error_init(&ctx->error);
if ((zs = zip_source_function_create(read_data, ctx, error)) == NULL) {
- buffer_free(ctx->in);
- free(ctx);
- return NULL;
+ buffer_free(ctx->in);
+ free(ctx);
+ return NULL;
}
return zs;
@@ -187,133 +187,133 @@ read_data(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
switch (cmd) {
case ZIP_SOURCE_BEGIN_WRITE:
- if ((ctx->out = buffer_new(NULL, 0, 0, &ctx->error)) == NULL) {
- return -1;
- }
- ctx->out->offset = 0;
- ctx->out->current_fragment = 0;
- return 0;
+ if ((ctx->out = buffer_new(NULL, 0, 0, &ctx->error)) == NULL) {
+ return -1;
+ }
+ ctx->out->offset = 0;
+ ctx->out->current_fragment = 0;
+ return 0;
case ZIP_SOURCE_BEGIN_WRITE_CLONING:
- if ((ctx->out = buffer_clone(ctx->in, len, &ctx->error)) == NULL) {
- return -1;
- }
- ctx->out->offset = len;
- ctx->out->current_fragment = ctx->out->nfragments;
- return 0;
+ if ((ctx->out = buffer_clone(ctx->in, len, &ctx->error)) == NULL) {
+ return -1;
+ }
+ ctx->out->offset = len;
+ ctx->out->current_fragment = ctx->out->nfragments;
+ return 0;
case ZIP_SOURCE_CLOSE:
- return 0;
+ return 0;
case ZIP_SOURCE_COMMIT_WRITE:
- buffer_free(ctx->in);
- ctx->in = ctx->out;
- ctx->out = NULL;
- return 0;
+ buffer_free(ctx->in);
+ ctx->in = ctx->out;
+ ctx->out = NULL;
+ return 0;
case ZIP_SOURCE_ERROR:
- return zip_error_to_data(&ctx->error, data, len);
+ return zip_error_to_data(&ctx->error, data, len);
case ZIP_SOURCE_FREE:
- buffer_free(ctx->in);
- buffer_free(ctx->out);
- free(ctx);
- return 0;
+ buffer_free(ctx->in);
+ buffer_free(ctx->out);
+ free(ctx);
+ return 0;
case ZIP_SOURCE_GET_FILE_ATTRIBUTES: {
- if (len < sizeof(ctx->attributes)) {
- zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
- return -1;
- }
+ if (len < sizeof(ctx->attributes)) {
+ zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
+ return -1;
+ }
- memcpy(data, &ctx->attributes, sizeof(ctx->attributes));
+ memcpy(data, &ctx->attributes, sizeof(ctx->attributes));
- return sizeof(ctx->attributes);
+ return sizeof(ctx->attributes);
}
case ZIP_SOURCE_OPEN:
- ctx->in->offset = 0;
- ctx->in->current_fragment = 0;
- return 0;
+ ctx->in->offset = 0;
+ ctx->in->current_fragment = 0;
+ return 0;
case ZIP_SOURCE_READ:
- if (len > ZIP_INT64_MAX) {
- zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
- return -1;
- }
- return buffer_read(ctx->in, data, len);
+ if (len > ZIP_INT64_MAX) {
+ zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
+ return -1;
+ }
+ return buffer_read(ctx->in, data, len);
case ZIP_SOURCE_REMOVE: {
- buffer_t *empty = buffer_new(NULL, 0, 0, &ctx->error);
- if (empty == NULL) {
- return -1;
- }
+ buffer_t *empty = buffer_new(NULL, 0, 0, &ctx->error);
+ if (empty == NULL) {
+ return -1;
+ }
- buffer_free(ctx->in);
- ctx->in = empty;
- return 0;
+ buffer_free(ctx->in);
+ ctx->in = empty;
+ return 0;
}
case ZIP_SOURCE_ROLLBACK_WRITE:
- buffer_free(ctx->out);
- ctx->out = NULL;
- return 0;
+ buffer_free(ctx->out);
+ ctx->out = NULL;
+ return 0;
case ZIP_SOURCE_SEEK:
- return buffer_seek(ctx->in, data, len, &ctx->error);
+ return buffer_seek(ctx->in, data, len, &ctx->error);
case ZIP_SOURCE_SEEK_WRITE:
- return buffer_seek(ctx->out, data, len, &ctx->error);
+ return buffer_seek(ctx->out, data, len, &ctx->error);
case ZIP_SOURCE_STAT: {
- zip_stat_t *st;
+ zip_stat_t *st;
- if (len < sizeof(*st)) {
- zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
- return -1;
- }
+ if (len < sizeof(*st)) {
+ zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
+ return -1;
+ }
- st = (zip_stat_t *)data;
+ st = (zip_stat_t *)data;
- zip_stat_init(st);
- st->mtime = ctx->mtime;
- st->size = ctx->in->size;
- st->comp_size = st->size;
- st->comp_method = ZIP_CM_STORE;
- st->encryption_method = ZIP_EM_NONE;
- st->valid = ZIP_STAT_MTIME | ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD;
+ zip_stat_init(st);
+ st->mtime = ctx->mtime;
+ st->size = ctx->in->size;
+ st->comp_size = st->size;
+ st->comp_method = ZIP_CM_STORE;
+ st->encryption_method = ZIP_EM_NONE;
+ st->valid = ZIP_STAT_MTIME | ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD;
- return sizeof(*st);
+ return sizeof(*st);
}
case ZIP_SOURCE_SUPPORTS:
- 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, -1);
+ 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, -1);
case ZIP_SOURCE_TELL:
- if (ctx->in->offset > ZIP_INT64_MAX) {
- zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW);
- return -1;
- }
- return (zip_int64_t)ctx->in->offset;
+ if (ctx->in->offset > ZIP_INT64_MAX) {
+ zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW);
+ return -1;
+ }
+ return (zip_int64_t)ctx->in->offset;
case ZIP_SOURCE_TELL_WRITE:
- if (ctx->out->offset > ZIP_INT64_MAX) {
- zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW);
- return -1;
- }
- return (zip_int64_t)ctx->out->offset;
+ if (ctx->out->offset > ZIP_INT64_MAX) {
+ zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW);
+ return -1;
+ }
+ return (zip_int64_t)ctx->out->offset;
case ZIP_SOURCE_WRITE:
- if (len > ZIP_INT64_MAX) {
- zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
- return -1;
- }
- return buffer_write(ctx->out, data, len, &ctx->error);
+ if (len > ZIP_INT64_MAX) {
+ zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
+ return -1;
+ }
+ return buffer_write(ctx->out, data, len, &ctx->error);
default:
- zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
- return -1;
+ zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
+ return -1;
}
}
@@ -324,34 +324,34 @@ buffer_clone(buffer_t *buffer, zip_uint64_t offset, zip_error_t *error) {
buffer_t *clone;
if (offset == 0) {
- return buffer_new(NULL, 0, 1, error);
+ return buffer_new(NULL, 0, 1, error);
}
if (offset > buffer->size) {
- zip_error_set(error, ZIP_ER_INVAL, 0);
- return NULL;
+ zip_error_set(error, ZIP_ER_INVAL, 0);
+ return NULL;
}
if (buffer->shared_buffer != NULL) {
- zip_error_set(error, ZIP_ER_INUSE, 0);
- return NULL;
+ zip_error_set(error, ZIP_ER_INUSE, 0);
+ return NULL;
}
fragment = buffer_find_fragment(buffer, offset);
fragment_offset = offset - buffer->fragment_offsets[fragment];
if (fragment_offset == 0) {
- fragment--;
- fragment_offset = buffer->fragments[fragment].length;
+ fragment--;
+ fragment_offset = buffer->fragments[fragment].length;
}
waste = buffer->fragments[fragment].length - fragment_offset;
if (waste > offset) {
- zip_error_set(error, ZIP_ER_OPNOTSUPP, 0);
- return NULL;
+ zip_error_set(error, ZIP_ER_OPNOTSUPP, 0);
+ return NULL;
}
if ((clone = buffer_new(buffer->fragments, fragment + 1, 0, error)) == NULL) {
- return NULL;
+ return NULL;
}
#ifndef __clang_analyzer__
@@ -361,13 +361,13 @@ buffer_clone(buffer_t *buffer, zip_uint64_t offset, zip_error_t *error) {
clone->fragment_offsets[clone->nfragments] = offset;
clone->size = offset;
- clone->first_owned_fragment = ZIP_MIN(buffer->first_owned_fragment, clone->nfragments - 1);
+ clone->first_owned_fragment = ZIP_MIN(buffer->first_owned_fragment, clone->nfragments);
buffer->shared_buffer = clone;
clone->shared_buffer = buffer;
buffer->shared_fragments = clone->nfragments;
clone->shared_fragments = fragment + 1;
-
+
return clone;
}
@@ -376,20 +376,24 @@ static zip_uint64_t
buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset) {
zip_uint64_t low, high, mid;
+ if (buffer->nfragments == 0) {
+ return 0;
+ }
+
low = 0;
high = buffer->nfragments - 1;
while (low < high) {
- mid = (high - low) / 2 + low;
- if (buffer->fragment_offsets[mid] > offset) {
- high = mid - 1;
- }
- else if (mid == buffer->nfragments || buffer->fragment_offsets[mid + 1] > offset) {
- return mid;
- }
- else {
- low = mid + 1;
- }
+ mid = (high - low) / 2 + low;
+ if (buffer->fragment_offsets[mid] > offset) {
+ high = mid - 1;
+ }
+ else if (mid == buffer->nfragments || buffer->fragment_offsets[mid + 1] > offset) {
+ return mid;
+ }
+ else {
+ low = mid + 1;
+ }
}
return low;
@@ -401,18 +405,18 @@ buffer_free(buffer_t *buffer) {
zip_uint64_t i;
if (buffer == NULL) {
- return;
+ return;
}
if (buffer->shared_buffer != NULL) {
- buffer->shared_buffer->shared_buffer = NULL;
- buffer->shared_buffer->shared_fragments = 0;
+ buffer->shared_buffer->shared_buffer = NULL;
+ buffer->shared_buffer->shared_fragments = 0;
- buffer->first_owned_fragment = ZIP_MAX(buffer->first_owned_fragment, buffer->shared_fragments);
+ buffer->first_owned_fragment = ZIP_MAX(buffer->first_owned_fragment, buffer->shared_fragments);
}
for (i = buffer->first_owned_fragment; i < buffer->nfragments; i++) {
- free(buffer->fragments[i].data);
+ free(buffer->fragments[i].data);
}
free(buffer->fragments);
free(buffer->fragment_offsets);
@@ -426,17 +430,17 @@ buffer_grow_fragments(buffer_t *buffer, zip_uint64_t capacity, zip_error_t *erro
zip_uint64_t *offsets;
if (capacity < buffer->fragments_capacity) {
- return true;
+ return true;
}
if ((fragments = realloc(buffer->fragments, sizeof(buffer->fragments[0]) * capacity)) == NULL) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return false;
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return false;
}
buffer->fragments = fragments;
if ((offsets = realloc(buffer->fragment_offsets, sizeof(buffer->fragment_offsets[0]) * (capacity + 1))) == NULL) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return false;
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return false;
}
buffer->fragment_offsets = offsets;
buffer->fragments_capacity = capacity;
@@ -450,7 +454,7 @@ buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int
buffer_t *buffer;
if ((buffer = malloc(sizeof(*buffer))) == NULL) {
- return NULL;
+ return NULL;
}
buffer->offset = 0;
@@ -464,42 +468,42 @@ buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int
buffer->shared_fragments = 0;
if (nfragments == 0) {
- if ((buffer->fragment_offsets = malloc(sizeof(buffer->fragment_offsets[0]))) == NULL) {
- free(buffer);
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return NULL;
- }
- buffer->fragment_offsets[0] = 0;
+ if ((buffer->fragment_offsets = malloc(sizeof(buffer->fragment_offsets[0]))) == NULL) {
+ free(buffer);
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return NULL;
+ }
+ buffer->fragment_offsets[0] = 0;
}
else {
- zip_uint64_t i, j, offset;
-
- if (!buffer_grow_fragments(buffer, nfragments, NULL)) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- buffer_free(buffer);
- return NULL;
- }
-
- offset = 0;
- for (i = 0, j = 0; i < nfragments; i++) {
- if (fragments[i].length == 0) {
- continue;
- }
- if (fragments[i].data == NULL) {
- zip_error_set(error, ZIP_ER_INVAL, 0);
- buffer_free(buffer);
- return NULL;
- }
- buffer->fragments[j].data = fragments[i].data;
- buffer->fragments[j].length = fragments[i].length;
- buffer->fragment_offsets[i] = offset;
- offset += fragments[i].length;
- j++;
- }
- buffer->nfragments = j;
- buffer->first_owned_fragment = free_data ? 0 : buffer->nfragments;
- buffer->fragment_offsets[buffer->nfragments] = offset;
- buffer->size = offset;
+ zip_uint64_t i, j, offset;
+
+ if (!buffer_grow_fragments(buffer, nfragments, NULL)) {
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ buffer_free(buffer);
+ return NULL;
+ }
+
+ offset = 0;
+ for (i = 0, j = 0; i < nfragments; i++) {
+ if (fragments[i].length == 0) {
+ continue;
+ }
+ if (fragments[i].data == NULL) {
+ zip_error_set(error, ZIP_ER_INVAL, 0);
+ buffer_free(buffer);
+ return NULL;
+ }
+ buffer->fragments[j].data = fragments[i].data;
+ buffer->fragments[j].length = fragments[i].length;
+ buffer->fragment_offsets[i] = offset;
+ offset += fragments[i].length;
+ j++;
+ }
+ buffer->nfragments = j;
+ buffer->first_owned_fragment = free_data ? 0 : buffer->nfragments;
+ buffer->fragment_offsets[buffer->nfragments] = offset;
+ buffer->size = offset;
}
return buffer;
@@ -512,25 +516,25 @@ buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) {
length = ZIP_MIN(length, buffer->size - buffer->offset);
if (length == 0) {
- return 0;
+ return 0;
}
if (length > ZIP_INT64_MAX) {
- return -1;
+ return -1;
}
i = buffer->current_fragment;
fragment_offset = buffer->offset - buffer->fragment_offsets[i];
n = 0;
while (n < length) {
- zip_uint64_t left = ZIP_MIN(length - n, buffer->fragments[i].length - fragment_offset);
+ zip_uint64_t left = ZIP_MIN(length - n, buffer->fragments[i].length - fragment_offset);
- memcpy(data + n, buffer->fragments[i].data + fragment_offset, left);
+ memcpy(data + n, buffer->fragments[i].data + fragment_offset, left);
- if (left == buffer->fragments[i].length - fragment_offset) {
- i++;
- }
- n += left;
- fragment_offset = 0;
+ if (left == buffer->fragments[i].length - fragment_offset) {
+ i++;
+ }
+ n += left;
+ fragment_offset = 0;
}
buffer->offset += n;
@@ -544,7 +548,7 @@ buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error)
zip_int64_t new_offset = zip_source_seek_compute_offset(buffer->offset, buffer->size, data, len, error);
if (new_offset < 0) {
- return -1;
+ return -1;
}
buffer->offset = (zip_uint64_t)new_offset;
@@ -558,62 +562,62 @@ buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip
zip_uint64_t n, i, fragment_offset, capacity;
if (buffer->offset + length + WRITE_FRAGMENT_SIZE - 1 < length) {
- zip_error_set(error, ZIP_ER_INVAL, 0);
- return -1;
+ zip_error_set(error, ZIP_ER_INVAL, 0);
+ return -1;
}
/* grow buffer if needed */
capacity = buffer_capacity(buffer);
if (buffer->offset + length > capacity) {
- zip_uint64_t needed_fragments = buffer->nfragments + (length - (capacity - buffer->offset) + WRITE_FRAGMENT_SIZE - 1) / WRITE_FRAGMENT_SIZE;
-
- if (needed_fragments > buffer->fragments_capacity) {
- zip_uint64_t new_capacity = buffer->fragments_capacity;
-
- if (new_capacity == 0) {
- new_capacity = 16;
- }
- while (new_capacity < needed_fragments) {
- new_capacity *= 2;
- }
-
- if (!buffer_grow_fragments(buffer, new_capacity, error)) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return -1;
- }
- }
-
- while (buffer->nfragments < needed_fragments) {
- if ((buffer->fragments[buffer->nfragments].data = malloc(WRITE_FRAGMENT_SIZE)) == NULL) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return -1;
- }
- buffer->fragments[buffer->nfragments].length = WRITE_FRAGMENT_SIZE;
- buffer->nfragments++;
- capacity += WRITE_FRAGMENT_SIZE;
- buffer->fragment_offsets[buffer->nfragments] = capacity;
- }
+ zip_uint64_t needed_fragments = buffer->nfragments + (length - (capacity - buffer->offset) + WRITE_FRAGMENT_SIZE - 1) / WRITE_FRAGMENT_SIZE;
+
+ if (needed_fragments > buffer->fragments_capacity) {
+ zip_uint64_t new_capacity = buffer->fragments_capacity;
+
+ if (new_capacity == 0) {
+ new_capacity = 16;
+ }
+ while (new_capacity < needed_fragments) {
+ new_capacity *= 2;
+ }
+
+ if (!buffer_grow_fragments(buffer, new_capacity, error)) {
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return -1;
+ }
+ }
+
+ while (buffer->nfragments < needed_fragments) {
+ if ((buffer->fragments[buffer->nfragments].data = malloc(WRITE_FRAGMENT_SIZE)) == NULL) {
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return -1;
+ }
+ buffer->fragments[buffer->nfragments].length = WRITE_FRAGMENT_SIZE;
+ buffer->nfragments++;
+ capacity += WRITE_FRAGMENT_SIZE;
+ buffer->fragment_offsets[buffer->nfragments] = capacity;
+ }
}
i = buffer->current_fragment;
fragment_offset = buffer->offset - buffer->fragment_offsets[i];
n = 0;
while (n < length) {
- zip_uint64_t left = ZIP_MIN(length - n, buffer->fragments[i].length - fragment_offset);
+ zip_uint64_t left = ZIP_MIN(length - n, buffer->fragments[i].length - fragment_offset);
- memcpy(buffer->fragments[i].data + fragment_offset, data + n, left);
+ memcpy(buffer->fragments[i].data + fragment_offset, data + n, left);
- if (left == buffer->fragments[i].length - fragment_offset) {
- i++;
- }
- n += left;
- fragment_offset = 0;
+ if (left == buffer->fragments[i].length - fragment_offset) {
+ i++;
+ }
+ n += left;
+ fragment_offset = 0;
}
buffer->offset += n;
buffer->current_fragment = i;
if (buffer->offset > buffer->size) {
- buffer->size = buffer->offset;
+ buffer->size = buffer->offset;
}
return (zip_int64_t)n;