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_api.c
Views: 1401
/*1zip_extra_field_api.c -- public extra fields API functions2Copyright (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*/323334#include "zipint.h"353637ZIP_EXTERN int38zip_file_extra_field_delete(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_flags_t flags) {39zip_dirent_t *de;4041if ((flags & ZIP_EF_BOTH) == 0) {42zip_error_set(&za->error, ZIP_ER_INVAL, 0);43return -1;44}4546if (((flags & ZIP_EF_BOTH) == ZIP_EF_BOTH) && (ef_idx != ZIP_EXTRA_FIELD_ALL)) {47zip_error_set(&za->error, ZIP_ER_INVAL, 0);48return -1;49}5051if (_zip_get_dirent(za, idx, 0, NULL) == NULL)52return -1;5354if (ZIP_IS_RDONLY(za)) {55zip_error_set(&za->error, ZIP_ER_RDONLY, 0);56return -1;57}5859if (_zip_file_extra_field_prepare_for_change(za, idx) < 0)60return -1;6162de = za->entry[idx].changes;6364de->extra_fields = _zip_ef_delete_by_id(de->extra_fields, ZIP_EXTRA_FIELD_ALL, ef_idx, flags);65return 0;66}676869ZIP_EXTERN int70zip_file_extra_field_delete_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_uint16_t ef_idx, zip_flags_t flags) {71zip_dirent_t *de;7273if ((flags & ZIP_EF_BOTH) == 0) {74zip_error_set(&za->error, ZIP_ER_INVAL, 0);75return -1;76}7778if (((flags & ZIP_EF_BOTH) == ZIP_EF_BOTH) && (ef_idx != ZIP_EXTRA_FIELD_ALL)) {79zip_error_set(&za->error, ZIP_ER_INVAL, 0);80return -1;81}8283if (_zip_get_dirent(za, idx, 0, NULL) == NULL)84return -1;8586if (ZIP_IS_RDONLY(za)) {87zip_error_set(&za->error, ZIP_ER_RDONLY, 0);88return -1;89}9091if (_zip_file_extra_field_prepare_for_change(za, idx) < 0)92return -1;9394de = za->entry[idx].changes;9596de->extra_fields = _zip_ef_delete_by_id(de->extra_fields, ef_id, ef_idx, flags);97return 0;98}99100101ZIP_EXTERN const zip_uint8_t *102zip_file_extra_field_get(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_uint16_t *idp, zip_uint16_t *lenp, zip_flags_t flags) {103static const zip_uint8_t empty[1] = {'\0'};104105zip_dirent_t *de;106zip_extra_field_t *ef;107int i;108109if ((flags & ZIP_EF_BOTH) == 0) {110zip_error_set(&za->error, ZIP_ER_INVAL, 0);111return NULL;112}113114if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)115return NULL;116117if (flags & ZIP_FL_LOCAL)118if (_zip_read_local_ef(za, idx) < 0)119return NULL;120121i = 0;122for (ef = de->extra_fields; ef; ef = ef->next) {123if (ef->flags & flags & ZIP_EF_BOTH) {124if (i < ef_idx) {125i++;126continue;127}128129if (idp)130*idp = ef->id;131if (lenp)132*lenp = ef->size;133if (ef->size > 0)134return ef->data;135else136return empty;137}138}139140zip_error_set(&za->error, ZIP_ER_NOENT, 0);141return NULL;142}143144145ZIP_EXTERN const zip_uint8_t *146zip_file_extra_field_get_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_uint16_t ef_idx, zip_uint16_t *lenp, zip_flags_t flags) {147zip_dirent_t *de;148149if ((flags & ZIP_EF_BOTH) == 0) {150zip_error_set(&za->error, ZIP_ER_INVAL, 0);151return NULL;152}153154if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)155return NULL;156157if (flags & ZIP_FL_LOCAL)158if (_zip_read_local_ef(za, idx) < 0)159return NULL;160161return _zip_ef_get_by_id(de->extra_fields, lenp, ef_id, ef_idx, flags, &za->error);162}163164165ZIP_EXTERN zip_int16_t166zip_file_extra_fields_count(zip_t *za, zip_uint64_t idx, zip_flags_t flags) {167zip_dirent_t *de;168zip_extra_field_t *ef;169zip_uint16_t n;170171if ((flags & ZIP_EF_BOTH) == 0) {172zip_error_set(&za->error, ZIP_ER_INVAL, 0);173return -1;174}175176if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)177return -1;178179if (flags & ZIP_FL_LOCAL)180if (_zip_read_local_ef(za, idx) < 0)181return -1;182183n = 0;184for (ef = de->extra_fields; ef; ef = ef->next)185if (ef->flags & flags & ZIP_EF_BOTH)186n++;187188return (zip_int16_t)n;189}190191192ZIP_EXTERN zip_int16_t193zip_file_extra_fields_count_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_flags_t flags) {194zip_dirent_t *de;195zip_extra_field_t *ef;196zip_uint16_t n;197198if ((flags & ZIP_EF_BOTH) == 0) {199zip_error_set(&za->error, ZIP_ER_INVAL, 0);200return -1;201}202203if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)204return -1;205206if (flags & ZIP_FL_LOCAL)207if (_zip_read_local_ef(za, idx) < 0)208return -1;209210n = 0;211for (ef = de->extra_fields; ef; ef = ef->next)212if (ef->id == ef_id && (ef->flags & flags & ZIP_EF_BOTH))213n++;214215return (zip_int16_t)n;216}217218219ZIP_EXTERN int220zip_file_extra_field_set(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_uint16_t ef_idx, const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags) {221zip_dirent_t *de;222zip_uint16_t ls, cs;223zip_extra_field_t *ef, *ef_prev, *ef_new;224int i, found, new_len;225226if ((flags & ZIP_EF_BOTH) == 0) {227zip_error_set(&za->error, ZIP_ER_INVAL, 0);228return -1;229}230231if (_zip_get_dirent(za, idx, 0, NULL) == NULL)232return -1;233234if (ZIP_IS_RDONLY(za)) {235zip_error_set(&za->error, ZIP_ER_RDONLY, 0);236return -1;237}238239if (ZIP_EF_IS_INTERNAL(ef_id)) {240zip_error_set(&za->error, ZIP_ER_INVAL, 0);241return -1;242}243244if (_zip_file_extra_field_prepare_for_change(za, idx) < 0)245return -1;246247de = za->entry[idx].changes;248249ef = de->extra_fields;250ef_prev = NULL;251i = 0;252found = 0;253254for (; ef; ef = ef->next) {255if (ef->id == ef_id && (ef->flags & flags & ZIP_EF_BOTH)) {256if (i == ef_idx) {257found = 1;258break;259}260i++;261}262ef_prev = ef;263}264265if (i < ef_idx && ef_idx != ZIP_EXTRA_FIELD_NEW) {266zip_error_set(&za->error, ZIP_ER_INVAL, 0);267return -1;268}269270if (flags & ZIP_EF_LOCAL)271ls = _zip_ef_size(de->extra_fields, ZIP_EF_LOCAL);272else273ls = 0;274if (flags & ZIP_EF_CENTRAL)275cs = _zip_ef_size(de->extra_fields, ZIP_EF_CENTRAL);276else277cs = 0;278279new_len = ls > cs ? ls : cs;280if (found)281new_len -= ef->size + 4;282new_len += len + 4;283284if (new_len > ZIP_UINT16_MAX) {285zip_error_set(&za->error, ZIP_ER_INVAL, 0);286return -1;287}288289if ((ef_new = _zip_ef_new(ef_id, len, data, flags)) == NULL) {290zip_error_set(&za->error, ZIP_ER_MEMORY, 0);291return -1;292}293294if (found) {295if ((ef->flags & ZIP_EF_BOTH) == (flags & ZIP_EF_BOTH)) {296ef_new->next = ef->next;297ef->next = NULL;298_zip_ef_free(ef);299if (ef_prev)300ef_prev->next = ef_new;301else302de->extra_fields = ef_new;303}304else {305ef->flags &= ~(flags & ZIP_EF_BOTH);306ef_new->next = ef->next;307ef->next = ef_new;308}309}310else if (ef_prev) {311ef_new->next = ef_prev->next;312ef_prev->next = ef_new;313}314else315de->extra_fields = ef_new;316317return 0;318}319320321int322_zip_file_extra_field_prepare_for_change(zip_t *za, zip_uint64_t idx) {323zip_entry_t *e;324325if (idx >= za->nentry) {326zip_error_set(&za->error, ZIP_ER_INVAL, 0);327return -1;328}329330e = za->entry + idx;331332if (e->changes && (e->changes->changed & ZIP_DIRENT_EXTRA_FIELD))333return 0;334335if (e->orig) {336if (_zip_read_local_ef(za, idx) < 0)337return -1;338}339340if (e->changes == NULL) {341if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) {342zip_error_set(&za->error, ZIP_ER_MEMORY, 0);343return -1;344}345}346347if (e->orig && e->orig->extra_fields) {348if ((e->changes->extra_fields = _zip_ef_clone(e->orig->extra_fields, &za->error)) == NULL)349return -1;350}351e->changes->changed |= ZIP_DIRENT_EXTRA_FIELD;352353return 0;354}355356357