Coverage Report

Created: 2025-07-23 06:33

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