CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/libzip/zip_extra_field.c
Views: 1401
/*1zip_extra_field.c -- manipulate extra fields2Copyright (C) 2012-2019 Dieter Baron and Thomas Klausner34This file is part of libzip, a library to manipulate ZIP archives.5The authors can be contacted at <[email protected]>67Redistribution and use in source and binary forms, with or without8modification, are permitted provided that the following conditions9are met:101. Redistributions of source code must retain the above copyright11notice, this list of conditions and the following disclaimer.122. Redistributions in binary form must reproduce the above copyright13notice, this list of conditions and the following disclaimer in14the documentation and/or other materials provided with the15distribution.163. The names of the authors may not be used to endorse or promote17products derived from this software without specific prior18written permission.1920THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS21OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY24DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE26GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS27INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER28IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR29OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/3233#include <stdlib.h>34#include <string.h>3536#include "zipint.h"373839zip_extra_field_t *40_zip_ef_clone(const zip_extra_field_t *ef, zip_error_t *error) {41zip_extra_field_t *head, *prev, *def;4243head = prev = NULL;4445while (ef) {46if ((def = _zip_ef_new(ef->id, ef->size, ef->data, ef->flags)) == NULL) {47zip_error_set(error, ZIP_ER_MEMORY, 0);48_zip_ef_free(head);49return NULL;50}5152if (head == NULL)53head = def;54if (prev)55prev->next = def;56prev = def;5758ef = ef->next;59}6061return head;62}636465zip_extra_field_t *66_zip_ef_delete_by_id(zip_extra_field_t *ef, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags) {67zip_extra_field_t *head, *prev;68int i;6970i = 0;71head = ef;72prev = NULL;73for (; ef; ef = (prev ? prev->next : head)) {74if ((ef->flags & flags & ZIP_EF_BOTH) && ((ef->id == id) || (id == ZIP_EXTRA_FIELD_ALL))) {75if (id_idx == ZIP_EXTRA_FIELD_ALL || i == id_idx) {76ef->flags &= ~(flags & ZIP_EF_BOTH);77if ((ef->flags & ZIP_EF_BOTH) == 0) {78if (prev)79prev->next = ef->next;80else81head = ef->next;82ef->next = NULL;83_zip_ef_free(ef);8485if (id_idx == ZIP_EXTRA_FIELD_ALL)86continue;87}88}8990i++;91if (i > id_idx)92break;93}94prev = ef;95}9697return head;98}99100101void102_zip_ef_free(zip_extra_field_t *ef) {103zip_extra_field_t *ef2;104105while (ef) {106ef2 = ef->next;107free(ef->data);108free(ef);109ef = ef2;110}111}112113114const zip_uint8_t *115_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) {116static const zip_uint8_t empty[1] = {'\0'};117118int i;119120i = 0;121for (; ef; ef = ef->next) {122if (ef->id == id && (ef->flags & flags & ZIP_EF_BOTH)) {123if (i < id_idx) {124i++;125continue;126}127128if (lenp)129*lenp = ef->size;130if (ef->size > 0)131return ef->data;132else133return empty;134}135}136137zip_error_set(error, ZIP_ER_NOENT, 0);138return NULL;139}140141142zip_extra_field_t *143_zip_ef_merge(zip_extra_field_t *to, zip_extra_field_t *from) {144zip_extra_field_t *ef2, *tt, *tail;145int duplicate;146147if (to == NULL)148return from;149150for (tail = to; tail->next; tail = tail->next)151;152153for (; from; from = ef2) {154ef2 = from->next;155156duplicate = 0;157for (tt = to; tt; tt = tt->next) {158if (tt->id == from->id && tt->size == from->size && (tt->size == 0 || memcmp(tt->data, from->data, tt->size) == 0)) {159tt->flags |= (from->flags & ZIP_EF_BOTH);160duplicate = 1;161break;162}163}164165from->next = NULL;166if (duplicate)167_zip_ef_free(from);168else169tail = tail->next = from;170}171172return to;173}174175176zip_extra_field_t *177_zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags) {178zip_extra_field_t *ef;179180if ((ef = (zip_extra_field_t *)malloc(sizeof(*ef))) == NULL)181return NULL;182183ef->next = NULL;184ef->flags = flags;185ef->id = id;186ef->size = size;187if (size > 0) {188if ((ef->data = (zip_uint8_t *)_zip_memdup(data, size, NULL)) == NULL) {189free(ef);190return NULL;191}192}193else194ef->data = NULL;195196return ef;197}198199200bool201_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) {202zip_buffer_t *buffer;203zip_extra_field_t *ef, *ef2, *ef_head;204205if ((buffer = _zip_buffer_new((zip_uint8_t *)data, len)) == NULL) {206zip_error_set(error, ZIP_ER_MEMORY, 0);207return false;208}209210ef_head = ef = NULL;211212while (_zip_buffer_ok(buffer) && _zip_buffer_left(buffer) >= 4) {213zip_uint16_t fid, flen;214zip_uint8_t *ef_data;215216fid = _zip_buffer_get_16(buffer);217flen = _zip_buffer_get_16(buffer);218ef_data = _zip_buffer_get(buffer, flen);219220if (ef_data == NULL) {221zip_error_set(error, ZIP_ER_INCONS, 0);222_zip_buffer_free(buffer);223_zip_ef_free(ef_head);224return false;225}226227if ((ef2 = _zip_ef_new(fid, flen, ef_data, flags)) == NULL) {228zip_error_set(error, ZIP_ER_MEMORY, 0);229_zip_buffer_free(buffer);230_zip_ef_free(ef_head);231return false;232}233234if (ef_head) {235ef->next = ef2;236ef = ef2;237}238else239ef_head = ef = ef2;240}241242if (!_zip_buffer_eof(buffer)) {243/* Android APK files align stored file data with padding in extra fields; ignore. */244/* see https://android.googlesource.com/platform/build/+/master/tools/zipalign/ZipAlign.cpp */245size_t glen = _zip_buffer_left(buffer);246zip_uint8_t *garbage;247garbage = _zip_buffer_get(buffer, glen);248if (glen >= 4 || garbage == NULL || memcmp(garbage, "\0\0\0", glen) != 0) {249zip_error_set(error, ZIP_ER_INCONS, 0);250_zip_buffer_free(buffer);251_zip_ef_free(ef_head);252return false;253}254}255256_zip_buffer_free(buffer);257258if (ef_head_p) {259*ef_head_p = ef_head;260}261else {262_zip_ef_free(ef_head);263}264265return true;266}267268269zip_extra_field_t *270_zip_ef_remove_internal(zip_extra_field_t *ef) {271zip_extra_field_t *ef_head;272zip_extra_field_t *prev, *next;273274ef_head = ef;275prev = NULL;276277while (ef) {278if (ZIP_EF_IS_INTERNAL(ef->id)) {279next = ef->next;280if (ef_head == ef)281ef_head = next;282ef->next = NULL;283_zip_ef_free(ef);284if (prev)285prev->next = next;286ef = next;287}288else {289prev = ef;290ef = ef->next;291}292}293294return ef_head;295}296297298zip_uint16_t299_zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags) {300zip_uint16_t size;301302size = 0;303for (; ef; ef = ef->next) {304if (ef->flags & flags & ZIP_EF_BOTH)305size = (zip_uint16_t)(size + 4 + ef->size);306}307308return size;309}310311312int313_zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) {314zip_uint8_t b[4];315zip_buffer_t *buffer = _zip_buffer_new(b, sizeof(b));316317if (buffer == NULL) {318return -1;319}320321for (; ef; ef = ef->next) {322if (ef->flags & flags & ZIP_EF_BOTH) {323_zip_buffer_set_offset(buffer, 0);324_zip_buffer_put_16(buffer, ef->id);325_zip_buffer_put_16(buffer, ef->size);326if (!_zip_buffer_ok(buffer)) {327zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);328_zip_buffer_free(buffer);329return -1;330}331if (_zip_write(za, b, 4) < 0) {332_zip_buffer_free(buffer);333return -1;334}335if (ef->size > 0) {336if (_zip_write(za, ef->data, ef->size) < 0) {337_zip_buffer_free(buffer);338return -1;339}340}341}342}343344_zip_buffer_free(buffer);345return 0;346}347348349int350_zip_read_local_ef(zip_t *za, zip_uint64_t idx) {351zip_entry_t *e;352unsigned char b[4];353zip_buffer_t *buffer;354zip_uint16_t fname_len, ef_len;355356if (idx >= za->nentry) {357zip_error_set(&za->error, ZIP_ER_INVAL, 0);358return -1;359}360361e = za->entry + idx;362363if (e->orig == NULL || e->orig->local_extra_fields_read)364return 0;365366if (e->orig->offset + 26 > ZIP_INT64_MAX) {367zip_error_set(&za->error, ZIP_ER_SEEK, EFBIG);368return -1;369}370371if (zip_source_seek(za->src, (zip_int64_t)(e->orig->offset + 26), SEEK_SET) < 0) {372_zip_error_set_from_source(&za->error, za->src);373return -1;374}375376if ((buffer = _zip_buffer_new_from_source(za->src, sizeof(b), b, &za->error)) == NULL) {377return -1;378}379380fname_len = _zip_buffer_get_16(buffer);381ef_len = _zip_buffer_get_16(buffer);382383if (!_zip_buffer_eof(buffer)) {384_zip_buffer_free(buffer);385zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);386return -1;387}388389_zip_buffer_free(buffer);390391if (ef_len > 0) {392zip_extra_field_t *ef;393zip_uint8_t *ef_raw;394395if (zip_source_seek(za->src, fname_len, SEEK_CUR) < 0) {396zip_error_set(&za->error, ZIP_ER_SEEK, errno);397return -1;398}399400ef_raw = _zip_read_data(NULL, za->src, ef_len, 0, &za->error);401402if (ef_raw == NULL)403return -1;404405if (!_zip_ef_parse(ef_raw, ef_len, ZIP_EF_LOCAL, &ef, &za->error)) {406free(ef_raw);407return -1;408}409free(ef_raw);410411if (ef) {412ef = _zip_ef_remove_internal(ef);413e->orig->extra_fields = _zip_ef_merge(e->orig->extra_fields, ef);414}415}416417e->orig->local_extra_fields_read = 1;418419if (e->changes && e->changes->local_extra_fields_read == 0) {420e->changes->extra_fields = e->orig->extra_fields;421e->changes->local_extra_fields_read = 1;422}423424return 0;425}426427428