2 zip_extra_field_api.c -- public extra fields API functions
3 Copyright (C) 2012-2014 Dieter Baron and Thomas Klausner
5 This file is part of libzip, a library to manipulate ZIP archives.
6 The authors can be contacted at <libzip@nih.at>
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
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
17 3. The names of the authors may not be used to endorse or promote
18 products derived from this software without specific prior
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.
39 zip_file_extra_field_delete(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_flags_t flags)
43 if ((flags & ZIP_EF_BOTH) == 0) {
44 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
48 if (((flags & ZIP_EF_BOTH) == ZIP_EF_BOTH) && (ef_idx != ZIP_EXTRA_FIELD_ALL)) {
49 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
53 if (_zip_get_dirent(za, idx, 0, NULL) == NULL)
56 if (ZIP_IS_RDONLY(za)) {
57 zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
61 if (_zip_file_extra_field_prepare_for_change(za, idx) < 0)
64 de = za->entry[idx].changes;
66 de->extra_fields = _zip_ef_delete_by_id(de->extra_fields, ZIP_EXTRA_FIELD_ALL, ef_idx, flags);
72 zip_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)
76 if ((flags & ZIP_EF_BOTH) == 0) {
77 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
81 if (((flags & ZIP_EF_BOTH) == ZIP_EF_BOTH) && (ef_idx != ZIP_EXTRA_FIELD_ALL)) {
82 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
86 if (_zip_get_dirent(za, idx, 0, NULL) == NULL)
89 if (ZIP_IS_RDONLY(za)) {
90 zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
94 if (_zip_file_extra_field_prepare_for_change(za, idx) < 0)
97 de = za->entry[idx].changes;
99 de->extra_fields = _zip_ef_delete_by_id(de->extra_fields, ef_id, ef_idx, flags);
104 ZIP_EXTERN const zip_uint8_t *
105 zip_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)
107 static const zip_uint8_t empty[1] = { '\0' };
110 zip_extra_field_t *ef;
113 if ((flags & ZIP_EF_BOTH) == 0) {
114 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
118 if ((de=_zip_get_dirent(za, idx, flags, &za->error)) == NULL)
121 if (flags & ZIP_FL_LOCAL)
122 if (_zip_read_local_ef(za, idx) < 0)
126 for (ef=de->extra_fields; ef; ef=ef->next) {
127 if (ef->flags & flags & ZIP_EF_BOTH) {
144 zip_error_set(&za->error, ZIP_ER_NOENT, 0);
150 ZIP_EXTERN const zip_uint8_t *
151 zip_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)
155 if ((flags & ZIP_EF_BOTH) == 0) {
156 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
160 if ((de=_zip_get_dirent(za, idx, flags, &za->error)) == NULL)
163 if (flags & ZIP_FL_LOCAL)
164 if (_zip_read_local_ef(za, idx) < 0)
167 return _zip_ef_get_by_id(de->extra_fields, lenp, ef_id, ef_idx, flags, &za->error);
171 ZIP_EXTERN zip_int16_t
172 zip_file_extra_fields_count(zip_t *za, zip_uint64_t idx, zip_flags_t flags)
175 zip_extra_field_t *ef;
178 if ((flags & ZIP_EF_BOTH) == 0) {
179 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
183 if ((de=_zip_get_dirent(za, idx, flags, &za->error)) == NULL)
186 if (flags & ZIP_FL_LOCAL)
187 if (_zip_read_local_ef(za, idx) < 0)
191 for (ef=de->extra_fields; ef; ef=ef->next)
192 if (ef->flags & flags & ZIP_EF_BOTH)
195 return (zip_int16_t)n;
199 ZIP_EXTERN zip_int16_t
200 zip_file_extra_fields_count_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_flags_t flags)
203 zip_extra_field_t *ef;
206 if ((flags & ZIP_EF_BOTH) == 0) {
207 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
211 if ((de=_zip_get_dirent(za, idx, flags, &za->error)) == NULL)
214 if (flags & ZIP_FL_LOCAL)
215 if (_zip_read_local_ef(za, idx) < 0)
219 for (ef=de->extra_fields; ef; ef=ef->next)
220 if (ef->id == ef_id && (ef->flags & flags & ZIP_EF_BOTH))
223 return (zip_int16_t)n;
228 zip_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)
232 zip_extra_field_t *ef, *ef_prev, *ef_new;
233 int i, found, new_len;
235 if ((flags & ZIP_EF_BOTH) == 0) {
236 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
240 if (_zip_get_dirent(za, idx, 0, NULL) == NULL)
243 if (ZIP_IS_RDONLY(za)) {
244 zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
248 if (ZIP_EF_IS_INTERNAL(ef_id)) {
249 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
253 if (_zip_file_extra_field_prepare_for_change(za, idx) < 0)
256 de = za->entry[idx].changes;
258 ef = de->extra_fields;
263 for (; ef; ef=ef->next) {
264 if (ef->id == ef_id && (ef->flags & flags & ZIP_EF_BOTH)) {
274 if (i < ef_idx && ef_idx != ZIP_EXTRA_FIELD_NEW) {
275 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
279 if (flags & ZIP_EF_LOCAL)
280 ls = _zip_ef_size(de->extra_fields, ZIP_EF_LOCAL);
283 if (flags & ZIP_EF_CENTRAL)
284 cs = _zip_ef_size(de->extra_fields, ZIP_EF_CENTRAL);
288 new_len = ls > cs ? ls : cs;
290 new_len -= ef->size + 4;
293 if (new_len > ZIP_UINT16_MAX) {
294 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
298 if ((ef_new=_zip_ef_new(ef_id, len, data, flags)) == NULL) {
299 zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
304 if ((ef->flags & ZIP_EF_BOTH) == (flags & ZIP_EF_BOTH)) {
305 ef_new->next = ef->next;
309 ef_prev->next = ef_new;
311 de->extra_fields = ef_new;
314 ef->flags &= ~(flags & ZIP_EF_BOTH);
315 ef_new->next = ef->next;
320 ef_new->next = ef_prev->next;
321 ef_prev->next = ef_new;
324 de->extra_fields = ef_new;
332 _zip_file_extra_field_prepare_for_change(zip_t *za, zip_uint64_t idx)
336 if (idx >= za->nentry) {
337 zip_error_set(&za->error, ZIP_ER_INVAL, 0);
343 if (e->changes && (e->changes->changed & ZIP_DIRENT_EXTRA_FIELD))
347 if (_zip_read_local_ef(za, idx) < 0)
351 if (e->changes == NULL) {
352 if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) {
353 zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
358 if (e->orig && e->orig->extra_fields) {
359 if ((e->changes->extra_fields=_zip_ef_clone(e->orig->extra_fields, &za->error)) == NULL)
362 e->changes->changed |= ZIP_DIRENT_EXTRA_FIELD;