Coverage Report

Created: 2025-11-11 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_extra_field_api.c
Line
Count
Source
1
/*
2
  zip_extra_field_api.c -- public extra fields API functions
3
  Copyright (C) 2012-2024 Dieter Baron and Thomas Klausner
4
5
  This file is part of libzip, a library to manipulate ZIP archives.
6
  The authors can be contacted at <info@libzip.org>
7
8
  Redistribution and use in source and binary forms, with or without
9
  modification, are permitted provided that the following conditions
10
  are met:
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
16
     distribution.
17
  3. The names of the authors may not be used to endorse or promote
18
     products derived from this software without specific prior
19
     written permission.
20
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.
32
*/
33
34
35
#include "zipint.h"
36
37
38
ZIP_EXTERN int
39
942
zip_file_extra_field_delete(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_flags_t flags) {
40
942
    zip_dirent_t *de;
41
42
942
    if ((flags & ZIP_EF_BOTH) == 0) {
43
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
44
0
        return -1;
45
0
    }
46
47
942
    if (((flags & ZIP_EF_BOTH) == ZIP_EF_BOTH) && (ef_idx != ZIP_EXTRA_FIELD_ALL)) {
48
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
49
0
        return -1;
50
0
    }
51
52
942
    if (_zip_get_dirent(za, idx, 0, NULL) == NULL)
53
0
        return -1;
54
55
942
    if (ZIP_IS_RDONLY(za)) {
56
0
        zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
57
0
        return -1;
58
0
    }
59
60
942
    if (_zip_file_extra_field_prepare_for_change(za, idx) < 0)
61
0
        return -1;
62
63
942
    de = za->entry[idx].changes;
64
65
942
    de->extra_fields = _zip_ef_delete_by_id(de->extra_fields, ZIP_EXTRA_FIELD_ALL, ef_idx, flags);
66
942
    return 0;
67
942
}
68
69
70
ZIP_EXTERN int
71
0
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) {
72
0
    zip_dirent_t *de;
73
74
0
    if ((flags & ZIP_EF_BOTH) == 0) {
75
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
76
0
        return -1;
77
0
    }
78
79
0
    if (((flags & ZIP_EF_BOTH) == ZIP_EF_BOTH) && (ef_idx != ZIP_EXTRA_FIELD_ALL)) {
80
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
81
0
        return -1;
82
0
    }
83
84
0
    if (_zip_get_dirent(za, idx, 0, NULL) == NULL)
85
0
        return -1;
86
87
0
    if (ZIP_IS_RDONLY(za)) {
88
0
        zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
89
0
        return -1;
90
0
    }
91
0
    if (ZIP_WANT_TORRENTZIP(za)) {
92
0
        zip_error_set(&za->error, ZIP_ER_NOT_ALLOWED, 0);
93
0
        return -1;
94
0
    }
95
96
0
    if (_zip_file_extra_field_prepare_for_change(za, idx) < 0)
97
0
        return -1;
98
99
0
    de = za->entry[idx].changes;
100
101
0
    de->extra_fields = _zip_ef_delete_by_id(de->extra_fields, ef_id, ef_idx, flags);
102
0
    return 0;
103
0
}
104
105
106
ZIP_EXTERN const zip_uint8_t *
107
0
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) {
108
0
    static const zip_uint8_t empty[1] = {'\0'};
109
110
0
    zip_dirent_t *de;
111
0
    zip_extra_field_t *ef;
112
0
    int i;
113
114
0
    if ((flags & ZIP_EF_BOTH) == 0) {
115
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
116
0
        return NULL;
117
0
    }
118
119
0
    if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)
120
0
        return NULL;
121
122
0
    if (flags & ZIP_FL_LOCAL)
123
0
        if (_zip_read_local_ef(za, idx) < 0)
124
0
            return NULL;
125
126
0
    i = 0;
127
0
    for (ef = de->extra_fields; ef; ef = ef->next) {
128
0
        if (ef->flags & flags & ZIP_EF_BOTH) {
129
0
            if (i < ef_idx) {
130
0
                i++;
131
0
                continue;
132
0
            }
133
134
0
            if (idp)
135
0
                *idp = ef->id;
136
0
            if (lenp)
137
0
                *lenp = ef->size;
138
0
            if (ef->size > 0)
139
0
                return ef->data;
140
0
            else
141
0
                return empty;
142
0
        }
143
0
    }
144
145
0
    zip_error_set(&za->error, ZIP_ER_NOENT, 0);
146
0
    return NULL;
147
0
}
148
149
150
ZIP_EXTERN const zip_uint8_t *
151
0
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) {
152
0
    zip_dirent_t *de;
153
154
0
    if ((flags & ZIP_EF_BOTH) == 0) {
155
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
156
0
        return NULL;
157
0
    }
158
159
0
    if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)
160
0
        return NULL;
161
162
0
    if (flags & ZIP_FL_LOCAL)
163
0
        if (_zip_read_local_ef(za, idx) < 0)
164
0
            return NULL;
165
166
0
    return _zip_ef_get_by_id(de->extra_fields, lenp, ef_id, ef_idx, flags, &za->error);
167
0
}
168
169
170
ZIP_EXTERN zip_int16_t
171
0
zip_file_extra_fields_count(zip_t *za, zip_uint64_t idx, zip_flags_t flags) {
172
0
    zip_dirent_t *de;
173
0
    zip_extra_field_t *ef;
174
0
    zip_uint16_t n;
175
176
0
    if ((flags & ZIP_EF_BOTH) == 0) {
177
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
178
0
        return -1;
179
0
    }
180
181
0
    if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)
182
0
        return -1;
183
184
0
    if (flags & ZIP_FL_LOCAL)
185
0
        if (_zip_read_local_ef(za, idx) < 0)
186
0
            return -1;
187
188
0
    n = 0;
189
0
    for (ef = de->extra_fields; ef; ef = ef->next)
190
0
        if (ef->flags & flags & ZIP_EF_BOTH)
191
0
            n++;
192
193
0
    return (zip_int16_t)n;
194
0
}
195
196
197
ZIP_EXTERN zip_int16_t
198
0
zip_file_extra_fields_count_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_flags_t flags) {
199
0
    zip_dirent_t *de;
200
0
    zip_extra_field_t *ef;
201
0
    zip_uint16_t n;
202
203
0
    if ((flags & ZIP_EF_BOTH) == 0) {
204
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
205
0
        return -1;
206
0
    }
207
208
0
    if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)
209
0
        return -1;
210
211
0
    if (flags & ZIP_FL_LOCAL)
212
0
        if (_zip_read_local_ef(za, idx) < 0)
213
0
            return -1;
214
215
0
    n = 0;
216
0
    for (ef = de->extra_fields; ef; ef = ef->next)
217
0
        if (ef->id == ef_id && (ef->flags & flags & ZIP_EF_BOTH))
218
0
            n++;
219
220
0
    return (zip_int16_t)n;
221
0
}
222
223
224
ZIP_EXTERN int
225
0
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) {
226
0
    zip_dirent_t *de;
227
0
    zip_uint16_t ls, cs;
228
0
    zip_extra_field_t *ef, *ef_prev, *ef_new;
229
0
    int i, found, new_len;
230
231
0
    if ((flags & ZIP_EF_BOTH) == 0) {
232
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
233
0
        return -1;
234
0
    }
235
236
0
    if (_zip_get_dirent(za, idx, 0, NULL) == NULL)
237
0
        return -1;
238
239
0
    if (ZIP_IS_RDONLY(za)) {
240
0
        zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
241
0
        return -1;
242
0
    }
243
0
    if (ZIP_WANT_TORRENTZIP(za)) {
244
0
        zip_error_set(&za->error, ZIP_ER_NOT_ALLOWED, 0);
245
0
        return -1;
246
0
    }
247
248
0
    if (ZIP_EF_IS_INTERNAL(ef_id)) {
249
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
250
0
        return -1;
251
0
    }
252
253
0
    if (_zip_file_extra_field_prepare_for_change(za, idx) < 0)
254
0
        return -1;
255
256
0
    de = za->entry[idx].changes;
257
258
0
    ef = de->extra_fields;
259
0
    ef_prev = NULL;
260
0
    i = 0;
261
0
    found = 0;
262
263
0
    for (; ef; ef = ef->next) {
264
0
        if (ef->id == ef_id && (ef->flags & flags & ZIP_EF_BOTH)) {
265
0
            if (i == ef_idx) {
266
0
                found = 1;
267
0
                break;
268
0
            }
269
0
            i++;
270
0
        }
271
0
        ef_prev = ef;
272
0
    }
273
274
0
    if (i < ef_idx && ef_idx != ZIP_EXTRA_FIELD_NEW) {
275
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
276
0
        return -1;
277
0
    }
278
279
0
    if (flags & ZIP_EF_LOCAL)
280
0
        ls = _zip_ef_size(de->extra_fields, ZIP_EF_LOCAL);
281
0
    else
282
0
        ls = 0;
283
0
    if (flags & ZIP_EF_CENTRAL)
284
0
        cs = _zip_ef_size(de->extra_fields, ZIP_EF_CENTRAL);
285
0
    else
286
0
        cs = 0;
287
288
0
    new_len = ls > cs ? ls : cs;
289
0
    if (found)
290
0
        new_len -= ef->size + 4;
291
0
    new_len += len + 4;
292
293
0
    if (new_len > ZIP_UINT16_MAX) {
294
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
295
0
        return -1;
296
0
    }
297
298
0
    if ((ef_new = _zip_ef_new(ef_id, len, data, flags)) == NULL) {
299
0
        zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
300
0
        return -1;
301
0
    }
302
303
0
    if (found) {
304
0
        if ((ef->flags & ZIP_EF_BOTH) == (flags & ZIP_EF_BOTH)) {
305
0
            ef_new->next = ef->next;
306
0
            ef->next = NULL;
307
0
            _zip_ef_free(ef);
308
0
            if (ef_prev)
309
0
                ef_prev->next = ef_new;
310
0
            else
311
0
                de->extra_fields = ef_new;
312
0
        }
313
0
        else {
314
0
            ef->flags &= ~(flags & ZIP_EF_BOTH);
315
0
            ef_new->next = ef->next;
316
0
            ef->next = ef_new;
317
0
        }
318
0
    }
319
0
    else if (ef_prev) {
320
0
        ef_new->next = ef_prev->next;
321
0
        ef_prev->next = ef_new;
322
0
    }
323
0
    else
324
0
        de->extra_fields = ef_new;
325
326
0
    return 0;
327
0
}
328
329
330
int
331
942
_zip_file_extra_field_prepare_for_change(zip_t *za, zip_uint64_t idx) {
332
942
    zip_entry_t *e;
333
334
942
    if (idx >= za->nentry) {
335
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
336
0
        return -1;
337
0
    }
338
339
942
    e = za->entry + idx;
340
341
942
    if (e->changes && (e->changes->changed & ZIP_DIRENT_EXTRA_FIELD))
342
0
        return 0;
343
344
942
    if (e->orig) {
345
0
        if (_zip_read_local_ef(za, idx) < 0)
346
0
            return -1;
347
0
    }
348
349
942
    if (e->changes == NULL) {
350
0
        if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) {
351
0
            zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
352
0
            return -1;
353
0
        }
354
0
    }
355
356
942
    if (e->orig && e->orig->extra_fields) {
357
0
        if ((e->changes->extra_fields = _zip_ef_clone(e->orig->extra_fields, &za->error)) == NULL)
358
0
            return -1;
359
0
    }
360
942
    e->changes->changed |= ZIP_DIRENT_EXTRA_FIELD;
361
362
942
    return 0;
363
942
}