Coverage Report

Created: 2026-08-11 07:29

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
28.0k
void _zip_ef_free(zip_extra_field_t *ef) {
107
28.0k
    zip_extra_field_t *ef2;
108
109
33.2k
    while (ef) {
110
5.19k
        ef2 = ef->next;
111
5.19k
        free(ef->data);
112
5.19k
        free(ef);
113
5.19k
        ef = ef2;
114
5.19k
    }
115
28.0k
}
116
117
118
54.9k
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
54.9k
    static const zip_uint8_t empty[1] = {'\0'};
120
121
54.9k
    int i;
122
123
54.9k
    i = 0;
124
63.2k
    for (; ef; ef = ef->next) {
125
8.46k
        if (ef->id == id && (ef->flags & flags & ZIP_EF_BOTH)) {
126
147
            if (i < id_idx) {
127
0
                i++;
128
0
                continue;
129
0
            }
130
131
147
            if (lenp) {
132
147
                *lenp = ef->size;
133
147
            }
134
147
            if (ef->size > 0) {
135
111
                return ef->data;
136
111
            }
137
36
            else {
138
36
                return empty;
139
36
            }
140
147
        }
141
8.46k
    }
142
143
54.7k
    zip_error_set(error, ZIP_ER_NOENT, 0);
144
54.7k
    return NULL;
145
54.9k
}
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
5.19k
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
5.19k
    zip_extra_field_t *ef;
186
187
5.19k
    if ((ef = (zip_extra_field_t *)malloc(sizeof(*ef))) == NULL) {
188
0
        return NULL;
189
0
    }
190
191
5.19k
    ef->next = NULL;
192
5.19k
    ef->flags = flags;
193
5.19k
    ef->id = id;
194
5.19k
    ef->size = size;
195
5.19k
    if (size > 0) {
196
3.06k
        if ((ef->data = (zip_uint8_t *)_zip_memdup(data, size, NULL)) == NULL) {
197
0
            free(ef);
198
0
            return NULL;
199
0
        }
200
3.06k
    }
201
2.12k
    else {
202
2.12k
        ef->data = NULL;
203
2.12k
    }
204
205
5.19k
    return ef;
206
5.19k
}
207
208
209
1.93k
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
1.93k
    zip_buffer_t *buffer;
211
1.93k
    zip_extra_field_t *ef, *ef2, *ef_head;
212
213
1.93k
    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
1.93k
    ef_head = ef = NULL;
219
220
7.12k
    while (_zip_buffer_ok(buffer) && _zip_buffer_left(buffer) >= 4) {
221
5.30k
        zip_uint16_t fid, flen;
222
5.30k
        zip_uint8_t *ef_data;
223
224
5.30k
        fid = _zip_buffer_get_16(buffer);
225
5.30k
        flen = _zip_buffer_get_16(buffer);
226
5.30k
        ef_data = _zip_buffer_get(buffer, flen);
227
228
5.30k
        if (ef_data == NULL) {
229
115
            zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_INVALID_EF_LENGTH);
230
115
            _zip_buffer_free(buffer);
231
115
            _zip_ef_free(ef_head);
232
115
            return false;
233
115
        }
234
235
5.19k
        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
5.19k
        if (ef_head) {
243
3.39k
            ef->next = ef2;
244
3.39k
            ef = ef2;
245
3.39k
        }
246
1.79k
        else {
247
1.79k
            ef_head = ef = ef2;
248
1.79k
        }
249
5.19k
    }
250
251
1.82k
    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
323
        size_t glen = _zip_buffer_left(buffer);
256
323
        zip_uint8_t *garbage;
257
323
        garbage = _zip_buffer_get(buffer, glen);
258
323
        if (glen >= 4 || garbage == NULL || memcmp(garbage, "\0\0\0", (size_t)glen) != 0) {
259
82
            zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_EF_TRAILING_GARBAGE);
260
82
            _zip_buffer_free(buffer);
261
82
            _zip_ef_free(ef_head);
262
82
            return false;
263
82
        }
264
323
    }
265
266
1.73k
    _zip_buffer_free(buffer);
267
268
1.73k
    if (ef_head_p) {
269
1.73k
        *ef_head_p = ef_head;
270
1.73k
    }
271
0
    else {
272
0
        _zip_ef_free(ef_head);
273
0
    }
274
275
1.73k
    return true;
276
1.82k
}
277
278
279
27.1k
zip_extra_field_t *_zip_ef_remove_internal(zip_extra_field_t *ef) {
280
27.1k
    zip_extra_field_t *ef_head;
281
27.1k
    zip_extra_field_t *prev, *next;
282
283
27.1k
    ef_head = ef;
284
27.1k
    prev = NULL;
285
286
30.8k
    while (ef) {
287
3.72k
        if (ZIP_EF_IS_INTERNAL(ef->id)) {
288
67
            next = ef->next;
289
67
            if (ef_head == ef) {
290
20
                ef_head = next;
291
20
            }
292
67
            ef->next = NULL;
293
67
            _zip_ef_free(ef);
294
67
            if (prev) {
295
47
                prev->next = next;
296
47
            }
297
67
            ef = next;
298
67
        }
299
3.65k
        else {
300
3.65k
            prev = ef;
301
3.65k
            ef = ef->next;
302
3.65k
        }
303
3.72k
    }
304
305
27.1k
    return ef_head;
306
27.1k
}
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
}