Coverage Report

Created: 2026-07-30 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_extra_field.c
Line
Count
Source
1
/*
2
  zip_extra_field.c -- manipulate extra fields
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
#include <stdlib.h>
35
#include <string.h>
36
37
#include "zipint.h"
38
39
40
0
zip_extra_field_t *_zip_ef_clone(const zip_extra_field_t *ef, zip_error_t *error) {
41
0
    zip_extra_field_t *head, *prev, *def;
42
43
0
    head = prev = NULL;
44
45
0
    while (ef) {
46
0
        if ((def = _zip_ef_new(ef->id, ef->size, ef->data, ef->flags)) == NULL) {
47
0
            zip_error_set(error, ZIP_ER_MEMORY, 0);
48
0
            _zip_ef_free(head);
49
0
            return NULL;
50
0
        }
51
52
0
        if (head == NULL) {
53
0
            head = def;
54
0
        }
55
0
        if (prev) {
56
0
            prev->next = def;
57
0
        }
58
0
        prev = def;
59
60
0
        ef = ef->next;
61
0
    }
62
63
0
    return head;
64
0
}
65
66
67
0
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) {
68
0
    zip_extra_field_t *head, *prev;
69
0
    int i;
70
71
0
    i = 0;
72
0
    head = ef;
73
0
    prev = NULL;
74
0
    for (; ef; ef = (prev ? prev->next : head)) {
75
0
        if ((ef->flags & flags & ZIP_EF_BOTH) && ((ef->id == id) || (id == ZIP_EXTRA_FIELD_ALL))) {
76
0
            if (id_idx == ZIP_EXTRA_FIELD_ALL || i == id_idx) {
77
0
                ef->flags &= ~(flags & ZIP_EF_BOTH);
78
0
                if ((ef->flags & ZIP_EF_BOTH) == 0) {
79
0
                    if (prev) {
80
0
                        prev->next = ef->next;
81
0
                    }
82
0
                    else {
83
0
                        head = ef->next;
84
0
                    }
85
0
                    ef->next = NULL;
86
0
                    _zip_ef_free(ef);
87
88
0
                    if (id_idx == ZIP_EXTRA_FIELD_ALL) {
89
0
                        continue;
90
0
                    }
91
0
                }
92
0
            }
93
94
0
            i++;
95
0
            if (i > id_idx) {
96
0
                break;
97
0
            }
98
0
        }
99
0
        prev = ef;
100
0
    }
101
102
0
    return head;
103
0
}
104
105
106
8.19k
void _zip_ef_free(zip_extra_field_t *ef) {
107
8.19k
    zip_extra_field_t *ef2;
108
109
9.95k
    while (ef) {
110
1.76k
        ef2 = ef->next;
111
1.76k
        free(ef->data);
112
1.76k
        free(ef);
113
1.76k
        ef = ef2;
114
1.76k
    }
115
8.19k
}
116
117
118
16.0k
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) {
119
16.0k
    static const zip_uint8_t empty[1] = {'\0'};
120
121
16.0k
    int i;
122
123
16.0k
    i = 0;
124
19.4k
    for (; ef; ef = ef->next) {
125
3.41k
        if (ef->id == id && (ef->flags & flags & ZIP_EF_BOTH)) {
126
6
            if (i < id_idx) {
127
0
                i++;
128
0
                continue;
129
0
            }
130
131
6
            if (lenp) {
132
6
                *lenp = ef->size;
133
6
            }
134
6
            if (ef->size > 0) {
135
6
                return ef->data;
136
6
            }
137
0
            else {
138
0
                return empty;
139
0
            }
140
6
        }
141
3.41k
    }
142
143
16.0k
    zip_error_set(error, ZIP_ER_NOENT, 0);
144
16.0k
    return NULL;
145
16.0k
}
146
147
148
0
zip_extra_field_t *_zip_ef_merge(zip_extra_field_t *to, zip_extra_field_t *from) {
149
0
    zip_extra_field_t *ef2, *tt, *tail;
150
0
    int duplicate;
151
152
0
    if (to == NULL) {
153
0
        return from;
154
0
    }
155
156
0
    for (tail = to; tail->next; tail = tail->next)
157
0
        ;
158
159
0
    for (; from; from = ef2) {
160
0
        ef2 = from->next;
161
162
0
        duplicate = 0;
163
0
        for (tt = to; tt; tt = tt->next) {
164
0
            if (tt->id == from->id && tt->size == from->size && (tt->size == 0 || memcmp(tt->data, from->data, tt->size) == 0)) {
165
0
                tt->flags |= (from->flags & ZIP_EF_BOTH);
166
0
                duplicate = 1;
167
0
                break;
168
0
            }
169
0
        }
170
171
0
        from->next = NULL;
172
0
        if (duplicate) {
173
0
            _zip_ef_free(from);
174
0
        }
175
0
        else {
176
0
            tail = tail->next = from;
177
0
        }
178
0
    }
179
180
0
    return to;
181
0
}
182
183
184
1.76k
zip_extra_field_t *_zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags) {
185
1.76k
    zip_extra_field_t *ef;
186
187
1.76k
    if ((ef = (zip_extra_field_t *)malloc(sizeof(*ef))) == NULL) {
188
0
        return NULL;
189
0
    }
190
191
1.76k
    ef->next = NULL;
192
1.76k
    ef->flags = flags;
193
1.76k
    ef->id = id;
194
1.76k
    ef->size = size;
195
1.76k
    if (size > 0) {
196
1.72k
        if ((ef->data = (zip_uint8_t *)_zip_memdup(data, size, NULL)) == NULL) {
197
0
            free(ef);
198
0
            return NULL;
199
0
        }
200
1.72k
    }
201
39
    else {
202
39
        ef->data = NULL;
203
39
    }
204
205
1.76k
    return ef;
206
1.76k
}
207
208
209
889
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) {
210
889
    zip_buffer_t *buffer;
211
889
    zip_extra_field_t *ef, *ef2, *ef_head;
212
213
889
    if ((buffer = _zip_buffer_new((zip_uint8_t *)data, len)) == NULL) {
214
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
215
0
        return false;
216
0
    }
217
218
889
    ef_head = ef = NULL;
219
220
2.65k
    while (_zip_buffer_ok(buffer) && _zip_buffer_left(buffer) >= 4) {
221
1.78k
        zip_uint16_t fid, flen;
222
1.78k
        zip_uint8_t *ef_data;
223
224
1.78k
        fid = _zip_buffer_get_16(buffer);
225
1.78k
        flen = _zip_buffer_get_16(buffer);
226
1.78k
        ef_data = _zip_buffer_get(buffer, flen);
227
228
1.78k
        if (ef_data == NULL) {
229
24
            zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_INVALID_EF_LENGTH);
230
24
            _zip_buffer_free(buffer);
231
24
            _zip_ef_free(ef_head);
232
24
            return false;
233
24
        }
234
235
1.76k
        if ((ef2 = _zip_ef_new(fid, flen, ef_data, flags)) == NULL) {
236
0
            zip_error_set(error, ZIP_ER_MEMORY, 0);
237
0
            _zip_buffer_free(buffer);
238
0
            _zip_ef_free(ef_head);
239
0
            return false;
240
0
        }
241
242
1.76k
        if (ef_head) {
243
889
            ef->next = ef2;
244
889
            ef = ef2;
245
889
        }
246
874
        else {
247
874
            ef_head = ef = ef2;
248
874
        }
249
1.76k
    }
250
251
865
    if (!_zip_buffer_eof(buffer)) {
252
        /* Android APK files align stored file data with padding in extra fields; ignore. */
253
        /* see https://android.googlesource.com/platform/build/+/master/tools/zipalign/ZipAlign.cpp */
254
        /* buffer is at most 64k long, so this can't overflow. */
255
23
        size_t glen = _zip_buffer_left(buffer);
256
23
        zip_uint8_t *garbage;
257
23
        garbage = _zip_buffer_get(buffer, glen);
258
23
        if (glen >= 4 || garbage == NULL || memcmp(garbage, "\0\0\0", (size_t)glen) != 0) {
259
16
            zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_EF_TRAILING_GARBAGE);
260
16
            _zip_buffer_free(buffer);
261
16
            _zip_ef_free(ef_head);
262
16
            return false;
263
16
        }
264
23
    }
265
266
849
    _zip_buffer_free(buffer);
267
268
849
    if (ef_head_p) {
269
849
        *ef_head_p = ef_head;
270
849
    }
271
0
    else {
272
0
        _zip_ef_free(ef_head);
273
0
    }
274
275
849
    return true;
276
865
}
277
278
279
8.00k
zip_extra_field_t *_zip_ef_remove_internal(zip_extra_field_t *ef) {
280
8.00k
    zip_extra_field_t *ef_head;
281
8.00k
    zip_extra_field_t *prev, *next;
282
283
8.00k
    ef_head = ef;
284
8.00k
    prev = NULL;
285
286
9.71k
    while (ef) {
287
1.70k
        if (ZIP_EF_IS_INTERNAL(ef->id)) {
288
7
            next = ef->next;
289
7
            if (ef_head == ef) {
290
1
                ef_head = next;
291
1
            }
292
7
            ef->next = NULL;
293
7
            _zip_ef_free(ef);
294
7
            if (prev) {
295
6
                prev->next = next;
296
6
            }
297
7
            ef = next;
298
7
        }
299
1.70k
        else {
300
1.70k
            prev = ef;
301
1.70k
            ef = ef->next;
302
1.70k
        }
303
1.70k
    }
304
305
8.00k
    return ef_head;
306
8.00k
}
307
308
309
0
zip_uint32_t _zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags) {
310
0
    zip_uint32_t size;
311
312
0
    size = 0;
313
0
    for (; ef; ef = ef->next) {
314
0
        if (ef->flags & flags & ZIP_EF_BOTH) {
315
0
            size = (zip_uint32_t)(size + 4 + ef->size);
316
0
        }
317
0
    }
318
319
0
    return size;
320
0
}
321
322
323
0
int _zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) {
324
0
    zip_uint8_t b[4];
325
0
    zip_buffer_t *buffer = _zip_buffer_new(b, sizeof(b));
326
327
0
    if (buffer == NULL) {
328
0
        return -1;
329
0
    }
330
331
0
    for (; ef; ef = ef->next) {
332
0
        if (ef->flags & flags & ZIP_EF_BOTH) {
333
0
            _zip_buffer_set_offset(buffer, 0);
334
0
            _zip_buffer_put_16(buffer, ef->id);
335
0
            _zip_buffer_put_16(buffer, ef->size);
336
0
            if (!_zip_buffer_ok(buffer)) {
337
0
                zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
338
0
                _zip_buffer_free(buffer);
339
0
                return -1;
340
0
            }
341
0
            if (_zip_write(za, b, 4) < 0) {
342
0
                _zip_buffer_free(buffer);
343
0
                return -1;
344
0
            }
345
0
            if (ef->size > 0) {
346
0
                if (_zip_write(za, ef->data, ef->size) < 0) {
347
0
                    _zip_buffer_free(buffer);
348
0
                    return -1;
349
0
                }
350
0
            }
351
0
        }
352
0
    }
353
354
0
    _zip_buffer_free(buffer);
355
0
    return 0;
356
0
}
357
358
359
0
int _zip_read_local_ef(zip_t *za, zip_uint64_t idx) {
360
0
    zip_entry_t *e;
361
0
    unsigned char b[4];
362
0
    zip_buffer_t *buffer;
363
0
    zip_uint16_t fname_len, ef_len;
364
365
0
    if (idx >= za->nentry) {
366
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
367
0
        return -1;
368
0
    }
369
370
0
    e = za->entry + idx;
371
372
0
    if (e->orig == NULL || e->orig->local_extra_fields_read) {
373
0
        return 0;
374
0
    }
375
376
0
    if (e->orig->offset + 26 > ZIP_INT64_MAX) {
377
0
        zip_error_set(&za->error, ZIP_ER_SEEK, EFBIG);
378
0
        return -1;
379
0
    }
380
381
0
    if (zip_source_seek(za->src, (zip_int64_t)(e->orig->offset + 26), SEEK_SET) < 0) {
382
0
        zip_error_set_from_source(&za->error, za->src);
383
0
        return -1;
384
0
    }
385
386
0
    if ((buffer = _zip_buffer_new_from_source(za->src, sizeof(b), b, &za->error)) == NULL) {
387
0
        return -1;
388
0
    }
389
390
0
    fname_len = _zip_buffer_get_16(buffer);
391
0
    ef_len = _zip_buffer_get_16(buffer);
392
393
0
    if (!_zip_buffer_eof(buffer)) {
394
0
        _zip_buffer_free(buffer);
395
0
        zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
396
0
        return -1;
397
0
    }
398
399
0
    _zip_buffer_free(buffer);
400
401
0
    if (ef_len > 0) {
402
0
        zip_extra_field_t *ef;
403
0
        zip_uint8_t *ef_raw;
404
405
0
        if (zip_source_seek(za->src, fname_len, SEEK_CUR) < 0) {
406
0
            zip_error_set(&za->error, ZIP_ER_SEEK, errno);
407
0
            return -1;
408
0
        }
409
410
0
        ef_raw = _zip_read_data(NULL, za->src, ef_len, 0, &za->error);
411
412
0
        if (ef_raw == NULL) {
413
0
            return -1;
414
0
        }
415
416
0
        if (!_zip_ef_parse(ef_raw, ef_len, ZIP_EF_LOCAL, &ef, &za->error)) {
417
0
            free(ef_raw);
418
0
            return -1;
419
0
        }
420
0
        free(ef_raw);
421
422
0
        if (ef) {
423
0
            ef = _zip_ef_remove_internal(ef);
424
0
            e->orig->extra_fields = _zip_ef_merge(e->orig->extra_fields, ef);
425
0
        }
426
0
    }
427
428
0
    e->orig->local_extra_fields_read = 1;
429
430
0
    if (e->changes && e->changes->local_extra_fields_read == 0) {
431
0
        e->changes->extra_fields = e->orig->extra_fields;
432
0
        e->changes->local_extra_fields_read = 1;
433
0
    }
434
435
0
    return 0;
436
0
}