Coverage Report

Created: 2026-04-01 07:49

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