Coverage Report

Created: 2025-07-23 08:18

/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
0
_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
                    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
0
    return head;
99
0
}
100
101
102
void
103
0
_zip_ef_free(zip_extra_field_t *ef) {
104
0
    zip_extra_field_t *ef2;
105
106
0
    while (ef) {
107
0
        ef2 = ef->next;
108
0
        free(ef->data);
109
0
        free(ef);
110
0
        ef = ef2;
111
0
    }
112
0
}
113
114
115
const zip_uint8_t *
116
0
_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
0
    static const zip_uint8_t empty[1] = {'\0'};
118
119
0
    int i;
120
121
0
    i = 0;
122
0
    for (; ef; ef = ef->next) {
123
0
        if (ef->id == id && (ef->flags & flags & ZIP_EF_BOTH)) {
124
0
            if (i < id_idx) {
125
0
                i++;
126
0
                continue;
127
0
            }
128
129
0
            if (lenp)
130
0
                *lenp = ef->size;
131
0
            if (ef->size > 0)
132
0
                return ef->data;
133
0
            else
134
0
                return empty;
135
0
        }
136
0
    }
137
138
0
    zip_error_set(error, ZIP_ER_NOENT, 0);
139
0
    return NULL;
140
0
}
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
0
_zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags) {
179
0
    zip_extra_field_t *ef;
180
181
0
    if ((ef = (zip_extra_field_t *)malloc(sizeof(*ef))) == NULL)
182
0
        return NULL;
183
184
0
    ef->next = NULL;
185
0
    ef->flags = flags;
186
0
    ef->id = id;
187
0
    ef->size = size;
188
0
    if (size > 0) {
189
0
        if ((ef->data = (zip_uint8_t *)_zip_memdup(data, size, NULL)) == NULL) {
190
0
            free(ef);
191
0
            return NULL;
192
0
        }
193
0
    }
194
0
    else
195
0
        ef->data = NULL;
196
197
0
    return ef;
198
0
}
199
200
201
bool
202
0
_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
0
    zip_buffer_t *buffer;
204
0
    zip_extra_field_t *ef, *ef2, *ef_head;
205
206
0
    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
0
    ef_head = ef = NULL;
212
213
0
    while (_zip_buffer_ok(buffer) && _zip_buffer_left(buffer) >= 4) {
214
0
        zip_uint16_t fid, flen;
215
0
        zip_uint8_t *ef_data;
216
217
0
        fid = _zip_buffer_get_16(buffer);
218
0
        flen = _zip_buffer_get_16(buffer);
219
0
        ef_data = _zip_buffer_get(buffer, flen);
220
221
0
        if (ef_data == NULL) {
222
0
            zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_INVALID_EF_LENGTH);
223
0
            _zip_buffer_free(buffer);
224
0
            _zip_ef_free(ef_head);
225
0
            return false;
226
0
        }
227
228
0
        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
0
        if (ef_head) {
236
0
            ef->next = ef2;
237
0
            ef = ef2;
238
0
        }
239
0
        else
240
0
            ef_head = ef = ef2;
241
0
    }
242
243
0
    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
0
        size_t glen = _zip_buffer_left(buffer);
248
0
        zip_uint8_t *garbage;
249
0
        garbage = _zip_buffer_get(buffer, glen);
250
0
        if (glen >= 4 || garbage == NULL || memcmp(garbage, "\0\0\0", (size_t)glen) != 0) {
251
0
            zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_EF_TRAILING_GARBAGE);
252
0
            _zip_buffer_free(buffer);
253
0
            _zip_ef_free(ef_head);
254
0
            return false;
255
0
        }
256
0
    }
257
258
0
    _zip_buffer_free(buffer);
259
260
0
    if (ef_head_p) {
261
0
        *ef_head_p = ef_head;
262
0
    }
263
0
    else {
264
0
        _zip_ef_free(ef_head);
265
0
    }
266
267
0
    return true;
268
0
}
269
270
271
zip_extra_field_t *
272
0
_zip_ef_remove_internal(zip_extra_field_t *ef) {
273
0
    zip_extra_field_t *ef_head;
274
0
    zip_extra_field_t *prev, *next;
275
276
0
    ef_head = ef;
277
0
    prev = NULL;
278
279
0
    while (ef) {
280
0
        if (ZIP_EF_IS_INTERNAL(ef->id)) {
281
0
            next = ef->next;
282
0
            if (ef_head == ef)
283
0
                ef_head = next;
284
0
            ef->next = NULL;
285
0
            _zip_ef_free(ef);
286
0
            if (prev)
287
0
                prev->next = next;
288
0
            ef = next;
289
0
        }
290
0
        else {
291
0
            prev = ef;
292
0
            ef = ef->next;
293
0
        }
294
0
    }
295
296
0
    return ef_head;
297
0
}
298
299
300
zip_uint16_t
301
0
_zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags) {
302
0
    zip_uint16_t size;
303
304
0
    size = 0;
305
0
    for (; ef; ef = ef->next) {
306
0
        if (ef->flags & flags & ZIP_EF_BOTH)
307
0
            size = (zip_uint16_t)(size + 4 + ef->size);
308
0
    }
309
310
0
    return size;
311
0
}
312
313
314
int
315
0
_zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags) {
316
0
    zip_uint8_t b[4];
317
0
    zip_buffer_t *buffer = _zip_buffer_new(b, sizeof(b));
318
319
0
    if (buffer == NULL) {
320
0
        return -1;
321
0
    }
322
323
0
    for (; ef; ef = ef->next) {
324
0
        if (ef->flags & flags & ZIP_EF_BOTH) {
325
0
            _zip_buffer_set_offset(buffer, 0);
326
0
            _zip_buffer_put_16(buffer, ef->id);
327
0
            _zip_buffer_put_16(buffer, ef->size);
328
0
            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
0
            if (_zip_write(za, b, 4) < 0) {
334
0
                _zip_buffer_free(buffer);
335
0
                return -1;
336
0
            }
337
0
            if (ef->size > 0) {
338
0
                if (_zip_write(za, ef->data, ef->size) < 0) {
339
0
                    _zip_buffer_free(buffer);
340
0
                    return -1;
341
0
                }
342
0
            }
343
0
        }
344
0
    }
345
346
0
    _zip_buffer_free(buffer);
347
0
    return 0;
348
0
}
349
350
351
int
352
0
_zip_read_local_ef(zip_t *za, zip_uint64_t idx) {
353
0
    zip_entry_t *e;
354
0
    unsigned char b[4];
355
0
    zip_buffer_t *buffer;
356
0
    zip_uint16_t fname_len, ef_len;
357
358
0
    if (idx >= za->nentry) {
359
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
360
0
        return -1;
361
0
    }
362
363
0
    e = za->entry + idx;
364
365
0
    if (e->orig == NULL || e->orig->local_extra_fields_read)
366
0
        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
}