diff options
Diffstat (limited to 'src/Common/libzip/zip_extra_field.c')
-rw-r--r-- | src/Common/libzip/zip_extra_field.c | 210 |
1 files changed, 99 insertions, 111 deletions
diff --git a/src/Common/libzip/zip_extra_field.c b/src/Common/libzip/zip_extra_field.c index a01ff790..42f97d0a 100644 --- a/src/Common/libzip/zip_extra_field.c +++ b/src/Common/libzip/zip_extra_field.c @@ -2,3 +2,3 @@ zip_extra_field.c -- manipulate extra fields - Copyright (C) 2012-2016 Dieter Baron and Thomas Klausner + Copyright (C) 2012-2017 Dieter Baron and Thomas Klausner @@ -19,3 +19,3 @@ written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS @@ -40,20 +40,19 @@ zip_extra_field_t * -_zip_ef_clone(const zip_extra_field_t *ef, zip_error_t *error) -{ +_zip_ef_clone(const zip_extra_field_t *ef, zip_error_t *error) { zip_extra_field_t *head, *prev, *def; - + head = prev = NULL; - + while (ef) { - if ((def=_zip_ef_new(ef->id, ef->size, ef->data, ef->flags)) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - _zip_ef_free(head); - return NULL; - } - - if (head == NULL) - head = def; - if (prev) - prev->next = def; - prev = def; + if ((def = _zip_ef_new(ef->id, ef->size, ef->data, ef->flags)) == NULL) { + zip_error_set(error, ZIP_ER_MEMORY, 0); + _zip_ef_free(head); + return NULL; + } + + if (head == NULL) + head = def; + if (prev) + prev->next = def; + prev = def; @@ -61,3 +60,3 @@ _zip_ef_clone(const zip_extra_field_t *ef, zip_error_t *error) } - + return head; @@ -67,4 +66,3 @@ _zip_ef_clone(const zip_extra_field_t *ef, zip_error_t *error) zip_extra_field_t * -_zip_ef_delete_by_id(zip_extra_field_t *ef, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags) -{ +_zip_ef_delete_by_id(zip_extra_field_t *ef, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags) { zip_extra_field_t *head, *prev; @@ -75,3 +73,3 @@ _zip_ef_delete_by_id(zip_extra_field_t *ef, zip_uint16_t id, zip_uint16_t id_idx prev = NULL; - for (; ef; ef=(prev ? prev->next : head)) { + for (; ef; ef = (prev ? prev->next : head)) { if ((ef->flags & flags & ZIP_EF_BOTH) && ((ef->id == id) || (id == ZIP_EXTRA_FIELD_ALL))) { @@ -91,3 +89,3 @@ _zip_ef_delete_by_id(zip_extra_field_t *ef, zip_uint16_t id, zip_uint16_t id_idx } - + i++; @@ -103,6 +101,4 @@ _zip_ef_delete_by_id(zip_extra_field_t *ef, zip_uint16_t id, zip_uint16_t id_idx - void -_zip_ef_free(zip_extra_field_t *ef) -{ +_zip_ef_free(zip_extra_field_t *ef) { zip_extra_field_t *ef2; @@ -119,6 +115,5 @@ _zip_ef_free(zip_extra_field_t *ef) const zip_uint8_t * -_zip_ef_get_by_id(const zip_extra_field_t *ef, zip_uint16_t *lenp, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags, zip_error_t *error) -{ - static const zip_uint8_t empty[1] = { '\0' }; - +_zip_ef_get_by_id(const zip_extra_field_t *ef, zip_uint16_t *lenp, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags, zip_error_t *error) { + static const zip_uint8_t empty[1] = {'\0'}; + int i; @@ -126,3 +121,3 @@ _zip_ef_get_by_id(const zip_extra_field_t *ef, zip_uint16_t *lenp, zip_uint16_t i = 0; - for (; ef; ef=ef->next) { + for (; ef; ef = ef->next) { if (ef->id == id && (ef->flags & flags & ZIP_EF_BOTH)) { @@ -148,4 +143,3 @@ _zip_ef_get_by_id(const zip_extra_field_t *ef, zip_uint16_t *lenp, zip_uint16_t zip_extra_field_t * -_zip_ef_merge(zip_extra_field_t *to, zip_extra_field_t *from) -{ +_zip_ef_merge(zip_extra_field_t *to, zip_extra_field_t *from) { zip_extra_field_t *ef2, *tt, *tail; @@ -156,6 +150,6 @@ _zip_ef_merge(zip_extra_field_t *to, zip_extra_field_t *from) - for (tail=to; tail->next; tail=tail->next) + for (tail = to; tail->next; tail = tail->next) ; - for (; from; from=ef2) { + for (; from; from = ef2) { ef2 = from->next; @@ -163,4 +157,4 @@ _zip_ef_merge(zip_extra_field_t *to, zip_extra_field_t *from) duplicate = 0; - for (tt=to; tt; tt=tt->next) { - if (tt->id == from->id && tt->size == from->size && memcmp(tt->data, from->data, tt->size) == 0) { + for (tt = to; tt; tt = tt->next) { + if (tt->id == from->id && tt->size == from->size && (tt->size == 0 || memcmp(tt->data, from->data, tt->size) == 0)) { tt->flags |= (from->flags & ZIP_EF_BOTH); @@ -183,7 +177,6 @@ _zip_ef_merge(zip_extra_field_t *to, zip_extra_field_t *from) zip_extra_field_t * -_zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags) -{ +_zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags) { zip_extra_field_t *ef; - if ((ef=(zip_extra_field_t *)malloc(sizeof(*ef))) == NULL) + if ((ef = (zip_extra_field_t *)malloc(sizeof(*ef))) == NULL) return NULL; @@ -195,3 +188,3 @@ _zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_fla if (size > 0) { - if ((ef->data=(zip_uint8_t *)_zip_memdup(data, size, NULL)) == NULL) { + if ((ef->data = (zip_uint8_t *)_zip_memdup(data, size, NULL)) == NULL) { free(ef); @@ -208,4 +201,3 @@ _zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_fla bool -_zip_ef_parse(const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags, zip_extra_field_t **ef_head_p, zip_error_t *error) -{ +_zip_ef_parse(const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags, zip_extra_field_t **ef_head_p, zip_error_t *error) { zip_buffer_t *buffer; @@ -214,26 +206,26 @@ _zip_ef_parse(const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags, zip_ if ((buffer = _zip_buffer_new((zip_uint8_t *)data, len)) == NULL) { - zip_error_set(error, ZIP_ER_MEMORY, 0); - return false; + zip_error_set(error, ZIP_ER_MEMORY, 0); + return false; } - + ef_head = ef = NULL; - + while (_zip_buffer_ok(buffer) && _zip_buffer_left(buffer) >= 4) { - zip_uint16_t fid, flen; - zip_uint8_t *ef_data; - - fid = _zip_buffer_get_16(buffer); + zip_uint16_t fid, flen; + zip_uint8_t *ef_data; + + fid = _zip_buffer_get_16(buffer); flen = _zip_buffer_get_16(buffer); - ef_data = _zip_buffer_get(buffer, flen); + ef_data = _zip_buffer_get(buffer, flen); - if (ef_data == NULL) { + if (ef_data == NULL) { zip_error_set(error, ZIP_ER_INCONS, 0); - _zip_buffer_free(buffer); + _zip_buffer_free(buffer); _zip_ef_free(ef_head); return false; - } - - if ((ef2=_zip_ef_new(fid, flen, ef_data, flags)) == NULL) { + } + + if ((ef2 = _zip_ef_new(fid, flen, ef_data, flags)) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); - _zip_buffer_free(buffer); + _zip_buffer_free(buffer); _zip_ef_free(ef_head); @@ -270,5 +262,5 @@ _zip_ef_parse(const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags, zip_ else { - _zip_ef_free(ef_head); + _zip_ef_free(ef_head); } - + return true; @@ -278,27 +270,26 @@ _zip_ef_parse(const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags, zip_ zip_extra_field_t * -_zip_ef_remove_internal(zip_extra_field_t *ef) -{ +_zip_ef_remove_internal(zip_extra_field_t *ef) { zip_extra_field_t *ef_head; zip_extra_field_t *prev, *next; - + ef_head = ef; prev = NULL; - + while (ef) { - if (ZIP_EF_IS_INTERNAL(ef->id)) { - next = ef->next; - if (ef_head == ef) - ef_head = next; - ef->next = NULL; - _zip_ef_free(ef); - if (prev) - prev->next = next; - ef = next; - } - else { - prev = ef; - ef = ef->next; - } + if (ZIP_EF_IS_INTERNAL(ef->id)) { + next = ef->next; + if (ef_head == ef) + ef_head = next; + ef->next = NULL; + _zip_ef_free(ef); + if (prev) + prev->next = next; + ef = next; + } + else { + prev = ef; + ef = ef->next; + } } - + return ef_head; @@ -308,4 +299,3 @@ _zip_ef_remove_internal(zip_extra_field_t *ef) zip_uint16_t -_zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags) -{ +_zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags) { zip_uint16_t size; @@ -313,5 +303,5 @@ _zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags) size = 0; - for (; ef; ef=ef->next) { + for (; ef; ef = ef->next) { if (ef->flags & flags & ZIP_EF_BOTH) - size = (zip_uint16_t)(size+4+ef->size); + size = (zip_uint16_t)(size + 4 + ef->size); } @@ -323,4 +313,3 @@ _zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags) int -_zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) -{ +_zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) { zip_uint8_t b[4]; @@ -332,15 +321,15 @@ _zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) - for (; ef; ef=ef->next) { + for (; ef; ef = ef->next) { if (ef->flags & flags & ZIP_EF_BOTH) { - _zip_buffer_set_offset(buffer, 0); - _zip_buffer_put_16(buffer, ef->id); + _zip_buffer_set_offset(buffer, 0); + _zip_buffer_put_16(buffer, ef->id); _zip_buffer_put_16(buffer, ef->size); - if (!_zip_buffer_ok(buffer)) { - zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); - _zip_buffer_free(buffer); - return -1; - } + if (!_zip_buffer_ok(buffer)) { + zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); + _zip_buffer_free(buffer); + return -1; + } if (_zip_write(za, b, 4) < 0) { - _zip_buffer_free(buffer); - return -1; + _zip_buffer_free(buffer); + return -1; } @@ -348,4 +337,4 @@ _zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) if (_zip_write(za, ef->data, ef->size) < 0) { - _zip_buffer_free(buffer); - return -1; + _zip_buffer_free(buffer); + return -1; } @@ -354,3 +343,3 @@ _zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) } - + _zip_buffer_free(buffer); @@ -361,4 +350,3 @@ _zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) int -_zip_read_local_ef(zip_t *za, zip_uint64_t idx) -{ +_zip_read_local_ef(zip_t *za, zip_uint64_t idx) { zip_entry_t *e; @@ -373,3 +361,3 @@ _zip_read_local_ef(zip_t *za, zip_uint64_t idx) - e = za->entry+idx; + e = za->entry + idx; @@ -381,3 +369,3 @@ _zip_read_local_ef(zip_t *za, zip_uint64_t idx) return -1; - } + } @@ -387,18 +375,18 @@ _zip_read_local_ef(zip_t *za, zip_uint64_t idx) } - + if ((buffer = _zip_buffer_new_from_source(za->src, sizeof(b), b, &za->error)) == NULL) { - return -1; + return -1; } - + fname_len = _zip_buffer_get_16(buffer); ef_len = _zip_buffer_get_16(buffer); - + if (!_zip_buffer_eof(buffer)) { - _zip_buffer_free(buffer); - zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); - return -1; + _zip_buffer_free(buffer); + zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); + return -1; } - + _zip_buffer_free(buffer); - + if (ef_len > 0) { @@ -430,3 +418,3 @@ _zip_read_local_ef(zip_t *za, zip_uint64_t idx) e->orig->local_extra_fields_read = 1; - + if (e->changes && e->changes->local_extra_fields_read == 0) { |