Coverage Report

Created: 2026-08-31 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_dirent.c
Line
Count
Source
1
/*
2
  zip_dirent.c -- read directory entry (local or central), clean dirent
3
  Copyright (C) 1999-2025 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 "zipint.h"
35
36
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <sys/types.h>
41
#include <time.h>
42
#include <zlib.h>
43
44
static zip_string_t *_zip_dirent_process_ef_utf_8(const zip_dirent_t *de, zip_uint16_t id, zip_string_t *str, bool check_consistency);
45
static zip_extra_field_t *_zip_ef_utf8(zip_uint16_t, zip_string_t *, zip_error_t *);
46
static bool _zip_dirent_process_winzip_aes(zip_dirent_t *de, zip_error_t *error);
47
48
49
43.0k
void _zip_cdir_free(zip_cdir_t *cd) {
50
43.0k
    zip_uint64_t i;
51
52
43.0k
    if (cd == NULL) {
53
0
        return;
54
0
    }
55
56
31.8M
    for (i = 0; i < cd->nentry; i++) {
57
31.8M
        _zip_entry_finalize(cd->entry + i);
58
31.8M
    }
59
43.0k
    free(cd->entry);
60
43.0k
    _zip_string_free(cd->comment);
61
43.0k
    free(cd);
62
43.0k
}
63
64
65
45.6k
zip_cdir_t *_zip_cdir_new(zip_error_t *error) {
66
45.6k
    zip_cdir_t *cd;
67
68
45.6k
    if ((cd = (zip_cdir_t *)malloc(sizeof(*cd))) == NULL) {
69
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
70
0
        return NULL;
71
0
    }
72
73
45.6k
    cd->entry = NULL;
74
45.6k
    cd->nentry = cd->nentry_alloc = 0;
75
45.6k
    cd->size = cd->offset = 0;
76
45.6k
    cd->comment = NULL;
77
45.6k
    cd->is_zip64 = false;
78
79
45.6k
    return cd;
80
45.6k
}
81
82
83
3.86k
bool _zip_cdir_grow(zip_cdir_t *cd, zip_uint64_t additional_entries, zip_error_t *error) {
84
3.86k
    zip_uint64_t i;
85
86
3.86k
    if (additional_entries == 0) {
87
360
        return true;
88
360
    }
89
90
3.50k
    if (!ZIP_REALLOC(cd->entry, cd->nentry_alloc, additional_entries, error)) {
91
0
        return false;
92
0
    }
93
94
31.8M
    for (i = cd->nentry; i < cd->nentry_alloc; i++) {
95
31.8M
        _zip_entry_init(cd->entry + i);
96
31.8M
    }
97
98
3.50k
    cd->nentry = cd->nentry_alloc;
99
100
3.50k
    return true;
101
3.50k
}
102
103
104
0
zip_int64_t _zip_cdir_write(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivors) {
105
0
    zip_uint64_t offset, size;
106
0
    zip_string_t *comment;
107
0
    zip_uint8_t buf[EOCDLEN + EOCD64LEN + EOCD64LOCLEN];
108
0
    zip_buffer_t *buffer;
109
0
    zip_int64_t off;
110
0
    zip_uint64_t i;
111
0
    zip_uint32_t cdir_crc;
112
113
0
    if ((off = zip_source_tell_write(za->src)) < 0) {
114
0
        zip_error_set_from_source(&za->error, za->src);
115
0
        return -1;
116
0
    }
117
0
    offset = (zip_uint64_t)off;
118
119
0
    if (ZIP_WANT_TORRENTZIP(za)) {
120
0
        cdir_crc = (zip_uint32_t)crc32(0, NULL, 0);
121
0
        za->write_crc = &cdir_crc;
122
0
    }
123
124
0
    for (i = 0; i < survivors; i++) {
125
0
        zip_entry_t *entry = za->entry + filelist[i].idx;
126
127
0
        if (_zip_dirent_write(za, entry->changes ? entry->changes : entry->orig, ZIP_FL_CENTRAL) < 0) {
128
0
            za->write_crc = NULL;
129
0
            return -1;
130
0
        }
131
0
    }
132
133
0
    za->write_crc = NULL;
134
135
0
    if ((off = zip_source_tell_write(za->src)) < 0) {
136
0
        zip_error_set_from_source(&za->error, za->src);
137
0
        return -1;
138
0
    }
139
0
    size = (zip_uint64_t)off - offset;
140
141
0
    if ((buffer = _zip_buffer_new(buf, sizeof(buf))) == NULL) {
142
0
        zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
143
0
        return -1;
144
0
    }
145
146
0
    if (survivors > ZIP_UINT16_MAX || offset > ZIP_UINT32_MAX || size > ZIP_UINT32_MAX) {
147
0
        _zip_buffer_put(buffer, EOCD64_MAGIC, 4);
148
0
        _zip_buffer_put_64(buffer, EOCD64LEN - 12);
149
0
        _zip_buffer_put_16(buffer, 45);
150
0
        _zip_buffer_put_16(buffer, 45);
151
0
        _zip_buffer_put_32(buffer, 0);
152
0
        _zip_buffer_put_32(buffer, 0);
153
0
        _zip_buffer_put_64(buffer, survivors);
154
0
        _zip_buffer_put_64(buffer, survivors);
155
0
        _zip_buffer_put_64(buffer, size);
156
0
        _zip_buffer_put_64(buffer, offset);
157
0
        _zip_buffer_put(buffer, EOCD64LOC_MAGIC, 4);
158
0
        _zip_buffer_put_32(buffer, 0);
159
0
        _zip_buffer_put_64(buffer, offset + size);
160
0
        _zip_buffer_put_32(buffer, 1);
161
0
    }
162
163
0
    _zip_buffer_put(buffer, EOCD_MAGIC, 4);
164
0
    _zip_buffer_put_32(buffer, 0);
165
0
    _zip_buffer_put_16(buffer, (zip_uint16_t)(survivors >= ZIP_UINT16_MAX ? ZIP_UINT16_MAX : survivors));
166
0
    _zip_buffer_put_16(buffer, (zip_uint16_t)(survivors >= ZIP_UINT16_MAX ? ZIP_UINT16_MAX : survivors));
167
0
    _zip_buffer_put_32(buffer, size >= ZIP_UINT32_MAX ? ZIP_UINT32_MAX : (zip_uint32_t)size);
168
0
    _zip_buffer_put_32(buffer, offset >= ZIP_UINT32_MAX ? ZIP_UINT32_MAX : (zip_uint32_t)offset);
169
170
0
    comment = za->comment_changed ? za->comment_changes : za->comment_orig;
171
172
0
    if (ZIP_WANT_TORRENTZIP(za)) {
173
0
        _zip_buffer_put_16(buffer, TORRENTZIP_SIGNATURE_LENGTH + TORRENTZIP_CRC_LENGTH);
174
0
    }
175
0
    else {
176
0
        _zip_buffer_put_16(buffer, (zip_uint16_t)(comment ? comment->length : 0));
177
0
    }
178
179
180
0
    if (!_zip_buffer_ok(buffer)) {
181
0
        zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
182
0
        _zip_buffer_free(buffer);
183
0
        return -1;
184
0
    }
185
186
0
    if (_zip_write(za, _zip_buffer_data(buffer), _zip_buffer_offset(buffer)) < 0) {
187
0
        _zip_buffer_free(buffer);
188
0
        return -1;
189
0
    }
190
191
0
    _zip_buffer_free(buffer);
192
193
0
    if (ZIP_WANT_TORRENTZIP(za)) {
194
0
        char torrentzip_comment[TORRENTZIP_SIGNATURE_LENGTH + TORRENTZIP_CRC_LENGTH + 1];
195
0
        snprintf(torrentzip_comment, sizeof(torrentzip_comment), TORRENTZIP_SIGNATURE "%08X", cdir_crc);
196
197
0
        if (_zip_write(za, torrentzip_comment, strlen(torrentzip_comment)) < 0) {
198
0
            return -1;
199
0
        }
200
0
    }
201
0
    else if (comment != NULL) {
202
0
        if (_zip_write(za, comment->raw, comment->length) < 0) {
203
0
            return -1;
204
0
        }
205
0
    }
206
207
0
    return (zip_int64_t)size;
208
0
}
209
210
211
0
zip_dirent_t *_zip_dirent_clone(const zip_dirent_t *sde) {
212
0
    zip_dirent_t *tde;
213
214
    /*
215
     This does a shallow copy. Allocated members are copied when they are modified.
216
     The changed flags keep track of which members need to be freed when a cloned entry is freed.
217
     */
218
219
0
    if ((tde = (zip_dirent_t *)malloc(sizeof(*tde))) == NULL) {
220
0
        return NULL;
221
0
    }
222
223
0
    if (sde) {
224
0
        (void)memcpy_s(tde, sizeof(*tde), sde, sizeof(*sde));
225
0
    }
226
0
    else {
227
0
        _zip_dirent_init(tde);
228
0
    }
229
230
0
    tde->changed = 0;
231
0
    tde->cloned = 1;
232
233
0
    return tde;
234
0
}
235
236
237
73.2k
void _zip_dirent_finalize(zip_dirent_t *zde) {
238
73.2k
    if (!zde->cloned || zde->changed & ZIP_DIRENT_FILENAME) {
239
73.2k
        _zip_string_free(zde->filename);
240
73.2k
        zde->filename = NULL;
241
73.2k
    }
242
73.2k
    if (!zde->cloned || zde->changed & ZIP_DIRENT_EXTRA_FIELD) {
243
73.2k
        _zip_extra_fields_fini(&zde->extra_fields);
244
73.2k
    }
245
73.2k
    if (!zde->cloned || zde->changed & ZIP_DIRENT_COMMENT) {
246
73.2k
        _zip_string_free(zde->comment);
247
73.2k
        zde->comment = NULL;
248
73.2k
    }
249
73.2k
    if (!zde->cloned || zde->changed & ZIP_DIRENT_PASSWORD) {
250
73.2k
        if (zde->password) {
251
0
            _zip_crypto_clear(zde->password, strlen(zde->password));
252
0
        }
253
73.2k
        free(zde->password);
254
73.2k
        zde->password = NULL;
255
73.2k
    }
256
73.2k
}
257
258
259
63.6M
void _zip_dirent_free(zip_dirent_t *zde) {
260
63.6M
    if (zde == NULL) {
261
63.5M
        return;
262
63.5M
    }
263
264
73.2k
    _zip_dirent_finalize(zde);
265
73.2k
    free(zde);
266
73.2k
}
267
268
269
0
bool _zip_dirent_merge(zip_dirent_t *de, zip_dirent_t *de_orig, bool replacing_data, zip_error_t *error) {
270
0
    if (!de->cloned) {
271
0
        zip_error_set(error, ZIP_ER_INTERNAL, 0);
272
0
        return false;
273
0
    }
274
275
0
    if (!(de->changed & ZIP_DIRENT_ATTRIBUTES)) {
276
0
        de->ext_attrib = de_orig->ext_attrib;
277
0
        de->int_attrib = de_orig->int_attrib;
278
0
    }
279
0
    if (!(de->changed & ZIP_DIRENT_COMMENT)) {
280
0
        de->comment = de_orig->comment;
281
0
    }
282
0
    if (!(de->changed & ZIP_DIRENT_COMP_METHOD)) {
283
0
        if (replacing_data) {
284
0
            de->comp_method = ZIP_CM_DEFAULT;
285
0
            de->compression_level = 0;
286
0
        }
287
0
        else {
288
0
            de->comp_method = de_orig->comp_method;
289
0
            de->compression_level = de_orig->compression_level;
290
0
        }
291
0
    }
292
0
    if (!(de->changed & ZIP_DIRENT_ENCRYPTION_METHOD)) {
293
0
        if (replacing_data) {
294
0
            de->encryption_method = ZIP_EM_NONE;
295
0
        }
296
0
        else {
297
0
            de->encryption_method = de_orig->encryption_method;
298
0
        }
299
0
    }
300
0
    if (!(de->changed & ZIP_DIRENT_EXTRA_FIELD)) {
301
0
        de->extra_fields = de_orig->extra_fields;
302
0
    }
303
0
    if (!(de->changed & ZIP_DIRENT_FILENAME)) {
304
0
        de->filename = de_orig->filename;
305
0
    }
306
0
    if (!(de->changed & ZIP_DIRENT_LAST_MOD)) {
307
0
        de->last_mod = de_orig->last_mod;
308
0
    }
309
0
    if (!(de->changed & ZIP_DIRENT_PASSWORD)) {
310
0
        de->password = de_orig->password;
311
0
    }
312
313
0
    return true;
314
0
}
315
316
317
146k
void _zip_dirent_init(zip_dirent_t *de) {
318
146k
    de->changed = 0;
319
146k
    de->local_extra_fields_read = 0;
320
146k
    de->cloned = 0;
321
322
146k
    de->crc_valid = true;
323
146k
    de->last_mod_mtime_valid = false;
324
146k
    de->version_madeby = 63 | (ZIP_OPSYS_DEFAULT << 8);
325
146k
    de->version_needed = 10; /* 1.0 */
326
146k
    de->bitflags = 0;
327
146k
    de->comp_method = ZIP_CM_DEFAULT;
328
146k
    de->last_mod.date = 0;
329
146k
    de->last_mod.time = 0;
330
146k
    de->crc = 0;
331
146k
    de->comp_size = 0;
332
146k
    de->uncomp_size = 0;
333
146k
    de->filename = NULL;
334
146k
    _zip_extra_fields_init(&de->extra_fields);
335
146k
    de->comment = NULL;
336
146k
    de->disk_number = 0;
337
146k
    de->int_attrib = 0;
338
146k
    de->ext_attrib = ZIP_EXT_ATTRIB_DEFAULT;
339
146k
    de->offset = 0;
340
146k
    de->compression_level = 0;
341
146k
    de->encryption_method = ZIP_EM_NONE;
342
146k
    de->password = NULL;
343
146k
}
344
345
346
0
bool _zip_dirent_needs_zip64(const zip_dirent_t *de, zip_flags_t flags) {
347
0
    if (de->uncomp_size >= ZIP_UINT32_MAX || de->comp_size >= ZIP_UINT32_MAX || ((flags & ZIP_FL_CENTRAL) && de->offset >= ZIP_UINT32_MAX)) {
348
0
        return true;
349
0
    }
350
351
0
    return false;
352
0
}
353
354
355
73.2k
zip_dirent_t *_zip_dirent_new(void) {
356
73.2k
    zip_dirent_t *de;
357
358
73.2k
    if ((de = (zip_dirent_t *)malloc(sizeof(*de))) == NULL) {
359
0
        return NULL;
360
0
    }
361
362
73.2k
    _zip_dirent_init(de);
363
73.2k
    return de;
364
73.2k
}
365
366
367
/*
368
   Fills the zip directory entry zde.
369
370
   If buffer is non-NULL, data is taken from there; otherwise data is read from fp as needed.
371
372
   If local is true, it reads a local header instead of a central directory entry.
373
374
   Returns size of dirent read if successful. On error, error is filled in and -1 is returned.
375
*/
376
377
73.2k
zip_int64_t _zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, bool local, bool is_zip64, zip_uint64_t central_compressed_size, bool check_consistency, zip_error_t *error) {
378
73.2k
    zip_uint8_t buf[CDENTRYSIZE];
379
73.2k
    zip_uint32_t size, variable_size;
380
73.2k
    zip_uint16_t filename_len, comment_len, ef_len;
381
73.2k
    zip_string_t *utf8_string;
382
73.2k
    zip_extra_field_t **efp;
383
384
73.2k
    bool from_buffer = (buffer != NULL);
385
386
73.2k
    size = local ? LENTRYSIZE : CDENTRYSIZE;
387
388
73.2k
    if (buffer) {
389
34.6k
        if (_zip_buffer_left(buffer) < size) {
390
106
            zip_error_set(error, ZIP_ER_NOZIP, 0);
391
106
            return -1;
392
106
        }
393
34.6k
    }
394
38.6k
    else {
395
38.6k
        if ((buffer = _zip_buffer_new_from_source(src, size, buf, error)) == NULL) {
396
16
            return -1;
397
16
        }
398
38.6k
    }
399
400
73.1k
    if (memcmp(_zip_buffer_get(buffer, 4), (local ? LOCAL_MAGIC : CENTRAL_MAGIC), 4) != 0) {
401
83
        zip_error_set(error, ZIP_ER_NOZIP, 0);
402
83
        if (!from_buffer) {
403
34
            _zip_buffer_free(buffer);
404
34
        }
405
83
        return -1;
406
83
    }
407
408
    /* convert buffercontents to zip_dirent */
409
410
73.0k
    _zip_dirent_init(zde);
411
73.0k
    if (!local) {
412
73.0k
        zde->version_madeby = _zip_buffer_get_16(buffer);
413
73.0k
    }
414
0
    else {
415
0
        zde->version_madeby = 0;
416
0
    }
417
73.0k
    zde->version_needed = _zip_buffer_get_16(buffer);
418
73.0k
    zde->bitflags = _zip_buffer_get_16(buffer);
419
73.0k
    zde->comp_method = _zip_buffer_get_16(buffer);
420
421
    /* convert to time_t */
422
73.0k
    zde->last_mod.time = _zip_buffer_get_16(buffer);
423
73.0k
    zde->last_mod.date = _zip_buffer_get_16(buffer);
424
425
73.0k
    zde->crc = _zip_buffer_get_32(buffer);
426
73.0k
    zde->comp_size = _zip_buffer_get_32(buffer);
427
73.0k
    zde->uncomp_size = _zip_buffer_get_32(buffer);
428
429
73.0k
    filename_len = _zip_buffer_get_16(buffer);
430
73.0k
    ef_len = _zip_buffer_get_16(buffer);
431
432
73.0k
    if (local) {
433
0
        comment_len = 0;
434
0
        zde->disk_number = 0;
435
0
        zde->int_attrib = 0;
436
0
        zde->ext_attrib = 0;
437
0
        zde->offset = 0;
438
0
    }
439
73.0k
    else {
440
73.0k
        comment_len = _zip_buffer_get_16(buffer);
441
73.0k
        zde->disk_number = _zip_buffer_get_16(buffer);
442
73.0k
        zde->int_attrib = _zip_buffer_get_16(buffer);
443
73.0k
        zde->ext_attrib = _zip_buffer_get_32(buffer);
444
73.0k
        zde->offset = _zip_buffer_get_32(buffer);
445
73.0k
    }
446
447
73.0k
    if (!_zip_buffer_ok(buffer)) {
448
0
        zip_error_set(error, ZIP_ER_INTERNAL, 0);
449
0
        if (!from_buffer) {
450
0
            _zip_buffer_free(buffer);
451
0
        }
452
0
        return -1;
453
0
    }
454
455
73.0k
    if (zde->bitflags & ZIP_GPBF_ENCRYPTED) {
456
18.1k
        if (zde->bitflags & ZIP_GPBF_STRONG_ENCRYPTION) {
457
            /* TODO */
458
6.09k
            zde->encryption_method = ZIP_EM_UNKNOWN;
459
6.09k
        }
460
12.0k
        else {
461
12.0k
            zde->encryption_method = ZIP_EM_TRAD_PKWARE;
462
12.0k
        }
463
18.1k
    }
464
54.9k
    else {
465
54.9k
        zde->encryption_method = ZIP_EM_NONE;
466
54.9k
    }
467
468
73.0k
    zde->filename = NULL;
469
73.0k
    _zip_extra_fields_init(&zde->extra_fields);
470
73.0k
    zde->comment = NULL;
471
472
73.0k
    variable_size = (zip_uint32_t)filename_len + (zip_uint32_t)ef_len + (zip_uint32_t)comment_len;
473
474
73.0k
    if (from_buffer) {
475
34.4k
        if (_zip_buffer_left(buffer) < variable_size) {
476
54
            zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_VARIABLE_SIZE_OVERFLOW);
477
54
            return -1;
478
54
        }
479
34.4k
    }
480
38.6k
    else {
481
38.6k
        _zip_buffer_free(buffer);
482
483
38.6k
        if ((buffer = _zip_buffer_new_from_source(src, variable_size, NULL, error)) == NULL) {
484
21
            return -1;
485
21
        }
486
38.6k
    }
487
488
73.0k
    if (filename_len) {
489
47.0k
        zde->filename = _zip_read_string(buffer, src, filename_len, 1, error);
490
47.0k
        if (zde->filename == NULL) {
491
0
            if (zip_error_code_zip(error) == ZIP_ER_EOF) {
492
0
                zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_VARIABLE_SIZE_OVERFLOW);
493
0
            }
494
0
            if (!from_buffer) {
495
0
                _zip_buffer_free(buffer);
496
0
            }
497
0
            return -1;
498
0
        }
499
500
47.0k
        if (zde->bitflags & ZIP_GPBF_ENCODING_UTF_8) {
501
7.01k
            if (_zip_guess_encoding(zde->filename, ZIP_ENCODING_UTF8_KNOWN) == ZIP_ENCODING_ERROR) {
502
36
                zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_INVALID_UTF8_IN_FILENAME);
503
36
                if (!from_buffer) {
504
7
                    _zip_buffer_free(buffer);
505
7
                }
506
36
                return -1;
507
36
            }
508
7.01k
        }
509
510
47.0k
        if (check_consistency) {
511
0
            zip_uint8_t *p;
512
513
0
            for (p = zde->filename->raw; p < zde->filename->raw + zde->filename->length; p++) {
514
0
                if (*p == 0) {
515
0
                    zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_NUL_IN_FILENAME);
516
0
                    if (!from_buffer) {
517
0
                        _zip_buffer_free(buffer);
518
0
                    }
519
0
                    return -1;
520
0
                }
521
0
            }
522
0
        }
523
47.0k
    }
524
525
72.9k
    if (ef_len) {
526
17.7k
        zip_uint8_t *ef = _zip_read_data(buffer, src, ef_len, 0, error);
527
528
17.7k
        if (ef == NULL) {
529
0
            if (!from_buffer) {
530
0
                _zip_buffer_free(buffer);
531
0
            }
532
0
            return -1;
533
0
        }
534
17.7k
        if (!_zip_ef_parse(ef, ef_len, local ? ZIP_EF_LOCAL : ZIP_EF_CENTRAL, (local ? &zde->extra_fields.local : &zde->extra_fields.central), error)) {
535
136
            free(ef);
536
136
            if (!from_buffer) {
537
22
                _zip_buffer_free(buffer);
538
22
            }
539
136
            return -1;
540
136
        }
541
17.6k
        free(ef);
542
17.6k
        if (local) {
543
0
            zde->local_extra_fields_read = 1;
544
0
        }
545
17.6k
    }
546
547
72.8k
    if (comment_len) {
548
3.36k
        zde->comment = _zip_read_string(buffer, src, comment_len, 0, error);
549
3.36k
        if (zde->comment == NULL) {
550
0
            if (!from_buffer) {
551
0
                _zip_buffer_free(buffer);
552
0
            }
553
0
            return -1;
554
0
        }
555
3.36k
        if (zde->bitflags & ZIP_GPBF_ENCODING_UTF_8) {
556
2.63k
            if (_zip_guess_encoding(zde->comment, ZIP_ENCODING_UTF8_KNOWN) == ZIP_ENCODING_ERROR) {
557
24
                zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_INVALID_UTF8_IN_COMMENT);
558
24
                if (!from_buffer) {
559
7
                    _zip_buffer_free(buffer);
560
7
                }
561
24
                return -1;
562
24
            }
563
2.63k
        }
564
3.36k
    }
565
566
72.8k
    if ((utf8_string = _zip_dirent_process_ef_utf_8(zde, ZIP_EF_UTF_8_NAME, zde->filename, check_consistency)) == NULL && zde->filename != NULL) {
567
0
        zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_UTF8_FILENAME_MISMATCH);
568
0
        if (!from_buffer) {
569
0
            _zip_buffer_free(buffer);
570
0
        }
571
0
        return -1;
572
0
    }
573
72.8k
    zde->filename = utf8_string;
574
72.8k
    if (!local) {
575
72.8k
        if ((utf8_string = _zip_dirent_process_ef_utf_8(zde, ZIP_EF_UTF_8_COMMENT, zde->comment, check_consistency)) == NULL && zde->comment != NULL) {
576
0
            zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_UTF8_COMMENT_MISMATCH);
577
0
            if (!from_buffer) {
578
0
                _zip_buffer_free(buffer);
579
0
            }
580
0
            return -1;
581
0
        }
582
72.8k
        zde->comment = utf8_string;
583
72.8k
    }
584
585
    /* Zip64 */
586
587
72.8k
    if (zde->uncomp_size == ZIP_UINT32_MAX || zde->comp_size == ZIP_UINT32_MAX || zde->offset == ZIP_UINT32_MAX) {
588
4.49k
        zip_uint16_t got_len;
589
4.49k
        const zip_uint8_t *ef = _zip_extra_fields_get_by_id(&zde->extra_fields, &got_len, ZIP_EF_ZIP64, 0, local ? ZIP_EF_LOCAL : ZIP_EF_CENTRAL, error);
590
4.49k
        if (ef != NULL) {
591
3.01k
            if (!zip_dirent_process_ef_zip64(zde, ef, got_len, local, error)) {
592
344
                if (!from_buffer) {
593
3
                    _zip_buffer_free(buffer);
594
3
                }
595
344
                return -1;
596
344
            }
597
3.01k
        }
598
1.48k
        else if (is_zip64) {
599
4
            zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_MISSING_ZIP64_EF);
600
4
            if (!from_buffer) {
601
2
                _zip_buffer_free(buffer);
602
2
            }
603
4
            return -1;
604
4
        }
605
4.49k
    }
606
607
608
72.4k
    if (!_zip_buffer_ok(buffer)) {
609
0
        zip_error_set(error, ZIP_ER_INTERNAL, 0);
610
0
        if (!from_buffer) {
611
0
            _zip_buffer_free(buffer);
612
0
        }
613
0
        return -1;
614
0
    }
615
616
72.4k
    if (!from_buffer) {
617
38.5k
        _zip_buffer_free(buffer);
618
38.5k
    }
619
620
72.4k
    if (local && zde->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) {
621
0
        zip_uint32_t df_crc;
622
0
        zip_uint64_t df_comp_size, df_uncomp_size;
623
624
0
        if (zip_source_seek(src, central_compressed_size, SEEK_CUR) != 0 || (buffer = _zip_buffer_new_from_source(src, MAX_DATA_DESCRIPTOR_LENGTH, buf, error)) == NULL) {
625
0
            return -1;
626
0
        }
627
0
        if (memcmp(_zip_buffer_peek(buffer, MAGIC_LEN), DATADES_MAGIC, MAGIC_LEN) == 0) {
628
0
            _zip_buffer_skip(buffer, MAGIC_LEN);
629
0
        }
630
0
        df_crc = _zip_buffer_get_32(buffer);
631
0
        df_comp_size = is_zip64 ? _zip_buffer_get_64(buffer) : _zip_buffer_get_32(buffer);
632
0
        df_uncomp_size = is_zip64 ? _zip_buffer_get_64(buffer) : _zip_buffer_get_32(buffer);
633
634
0
        if (!_zip_buffer_ok(buffer)) {
635
0
            zip_error_set(error, ZIP_ER_INTERNAL, 0);
636
0
            _zip_buffer_free(buffer);
637
0
            return -1;
638
0
        }
639
0
        _zip_buffer_free(buffer);
640
641
        /* According to the appnote, it should be 0, but some implementations write the actual value. */
642
0
        if ((zde->crc != 0 && zde->crc != df_crc) || (zde->comp_size != 0 && zde->comp_size != df_comp_size) || (zde->uncomp_size != 0 && zde->uncomp_size != df_uncomp_size)) {
643
0
            zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_DATA_DESCRIPTOR_MISMATCH);
644
0
            return -1;
645
0
        }
646
0
        zde->crc = df_crc;
647
0
        zde->comp_size = df_comp_size;
648
0
        zde->uncomp_size = df_uncomp_size;
649
0
    }
650
651
    /* zip_source_seek / zip_source_tell don't support values > ZIP_INT64_MAX */
652
72.4k
    if (zde->offset > ZIP_INT64_MAX) {
653
62
        zip_error_set(error, ZIP_ER_SEEK, EFBIG);
654
62
        return -1;
655
62
    }
656
657
72.3k
    if (!_zip_dirent_process_winzip_aes(zde, error)) {
658
62
        return -1;
659
62
    }
660
661
72.3k
    efp = local ? &zde->extra_fields.local : &zde->extra_fields.central;
662
72.3k
    *efp = _zip_ef_remove_internal(*efp);
663
664
72.3k
    return (zip_int64_t)size + (zip_int64_t)variable_size;
665
72.3k
}
666
667
3.01k
bool zip_dirent_process_ef_zip64(zip_dirent_t *zde, const zip_uint8_t *ef, zip_uint64_t got_len, bool local, zip_error_t *error) {
668
3.01k
    zip_buffer_t *ef_buffer;
669
670
3.01k
    if ((ef_buffer = _zip_buffer_new((zip_uint8_t *)ef, got_len)) == NULL) {
671
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
672
0
        return false;
673
0
    }
674
675
3.01k
    if (zde->uncomp_size == ZIP_UINT32_MAX) {
676
1.67k
        zde->uncomp_size = _zip_buffer_get_64(ef_buffer);
677
1.67k
    }
678
1.34k
    else if (local) {
679
        /* From appnote.txt: This entry in the Local header MUST
680
           include BOTH original and compressed file size fields. */
681
0
        (void)_zip_buffer_skip(ef_buffer, 8); /* error is caught by _zip_buffer_eof() call */
682
0
    }
683
3.01k
    if (zde->comp_size == ZIP_UINT32_MAX) {
684
2.20k
        zde->comp_size = _zip_buffer_get_64(ef_buffer);
685
2.20k
    }
686
3.01k
    if (!local) {
687
3.01k
        if (zde->offset == ZIP_UINT32_MAX) {
688
490
            zde->offset = _zip_buffer_get_64(ef_buffer);
689
490
        }
690
3.01k
        if (zde->disk_number == ZIP_UINT16_MAX) {
691
402
            zde->disk_number = _zip_buffer_get_32(ef_buffer);
692
402
        }
693
3.01k
    }
694
695
3.01k
    if (!_zip_buffer_eof(ef_buffer)) {
696
        /* accept additional fields if values match */
697
1.27k
        bool ok = true;
698
1.27k
        switch (got_len) {
699
839
        case 28:
700
839
            _zip_buffer_set_offset(ef_buffer, 24);
701
839
            if (zde->disk_number != _zip_buffer_get_32(ef_buffer)) {
702
223
                ok = false;
703
223
            }
704
            /* fallthrough */
705
1.17k
        case 24:
706
1.17k
            _zip_buffer_set_offset(ef_buffer, 0);
707
1.17k
            if ((zde->uncomp_size != _zip_buffer_get_64(ef_buffer)) || (zde->comp_size != _zip_buffer_get_64(ef_buffer)) || (zde->offset != _zip_buffer_get_64(ef_buffer))) {
708
248
                ok = false;
709
248
            }
710
1.17k
            break;
711
712
94
        default:
713
94
            ok = false;
714
1.27k
        }
715
1.27k
        if (!ok) {
716
344
            zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_INVALID_ZIP64_EF);
717
344
            _zip_buffer_free(ef_buffer);
718
344
            return false;
719
344
        }
720
1.27k
    }
721
2.67k
    _zip_buffer_free(ef_buffer);
722
723
2.67k
    return true;
724
3.01k
}
725
726
727
145k
static zip_string_t *_zip_dirent_process_ef_utf_8(const zip_dirent_t *de, zip_uint16_t id, zip_string_t *str, bool check_consistency) {
728
145k
    zip_uint16_t ef_len;
729
145k
    zip_uint32_t ef_crc;
730
145k
    zip_buffer_t *buffer;
731
732
145k
    const zip_uint8_t *ef = _zip_extra_fields_get_by_id(&de->extra_fields, &ef_len, id, 0, ZIP_EF_BOTH, NULL);
733
734
145k
    if (ef == NULL || ef_len < 5 || ef[0] != 1) {
735
136k
        return str;
736
136k
    }
737
738
8.75k
    if ((buffer = _zip_buffer_new((zip_uint8_t *)ef, ef_len)) == NULL) {
739
0
        return str;
740
0
    }
741
742
8.75k
    _zip_buffer_get_8(buffer);
743
8.75k
    ef_crc = _zip_buffer_get_32(buffer);
744
745
8.75k
    if (_zip_string_crc32(str) == ef_crc) {
746
7.07k
        zip_uint16_t len = (zip_uint16_t)_zip_buffer_left(buffer);
747
7.07k
        zip_string_t *ef_str = _zip_string_new(_zip_buffer_get(buffer, len), len, ZIP_FL_ENC_UTF_8, NULL);
748
749
7.07k
        if (ef_str != NULL) {
750
3.22k
            if (check_consistency) {
751
0
                if (!_zip_string_equal(str, ef_str) && _zip_string_is_ascii(ef_str)) {
752
0
                    _zip_string_free(ef_str);
753
0
                    _zip_buffer_free(buffer);
754
0
                    return NULL;
755
0
                }
756
0
            }
757
758
3.22k
            _zip_string_free(str);
759
3.22k
            str = ef_str;
760
3.22k
        }
761
7.07k
    }
762
763
8.75k
    _zip_buffer_free(buffer);
764
765
8.75k
    return str;
766
8.75k
}
767
768
769
72.3k
static bool _zip_dirent_process_winzip_aes(zip_dirent_t *de, zip_error_t *error) {
770
72.3k
    zip_uint16_t ef_len;
771
72.3k
    zip_buffer_t *buffer;
772
72.3k
    const zip_uint8_t *ef;
773
72.3k
    bool crc_valid;
774
72.3k
    zip_uint16_t enc_method;
775
776
777
72.3k
    if (de->comp_method != ZIP_CM_WINZIP_AES) {
778
70.8k
        return true;
779
70.8k
    }
780
781
1.52k
    ef = _zip_extra_fields_get_by_id(&de->extra_fields, &ef_len, ZIP_EF_WINZIP_AES, 0, ZIP_EF_BOTH, NULL);
782
783
1.52k
    if (ef == NULL || ef_len < 7) {
784
10
        zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_INVALID_WINZIPAES_EF);
785
10
        return false;
786
10
    }
787
788
1.51k
    if ((buffer = _zip_buffer_new((zip_uint8_t *)ef, ef_len)) == NULL) {
789
0
        zip_error_set(error, ZIP_ER_INTERNAL, 0);
790
0
        return false;
791
0
    }
792
793
    /* version */
794
795
1.51k
    crc_valid = true;
796
1.51k
    switch (_zip_buffer_get_16(buffer)) {
797
1.08k
    case 1:
798
1.08k
        break;
799
800
426
    case 2:
801
426
        crc_valid = false;
802
        /* TODO: When checking consistency, check that crc is 0. */
803
426
        break;
804
805
3
    default:
806
3
        zip_error_set(error, ZIP_ER_ENCRNOTSUPP, 0);
807
3
        _zip_buffer_free(buffer);
808
3
        return false;
809
1.51k
    }
810
811
    /* vendor */
812
1.51k
    if (memcmp(_zip_buffer_get(buffer, 2), "AE", 2) != 0) {
813
38
        zip_error_set(error, ZIP_ER_ENCRNOTSUPP, 0);
814
38
        _zip_buffer_free(buffer);
815
38
        return false;
816
38
    }
817
818
    /* mode */
819
1.47k
    switch (_zip_buffer_get_8(buffer)) {
820
242
    case 1:
821
242
        enc_method = ZIP_EM_AES_128;
822
242
        break;
823
107
    case 2:
824
107
        enc_method = ZIP_EM_AES_192;
825
107
        break;
826
1.12k
    case 3:
827
1.12k
        enc_method = ZIP_EM_AES_256;
828
1.12k
        break;
829
4
    default:
830
4
        zip_error_set(error, ZIP_ER_ENCRNOTSUPP, 0);
831
4
        _zip_buffer_free(buffer);
832
4
        return false;
833
1.47k
    }
834
835
1.47k
    if (ef_len != 7) {
836
7
        zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_INVALID_WINZIPAES_EF);
837
7
        _zip_buffer_free(buffer);
838
7
        return false;
839
7
    }
840
841
1.46k
    de->crc_valid = crc_valid;
842
1.46k
    de->encryption_method = enc_method;
843
1.46k
    de->comp_method = _zip_buffer_get_16(buffer);
844
845
1.46k
    _zip_buffer_free(buffer);
846
1.46k
    return true;
847
1.47k
}
848
849
850
19.2k
zip_int32_t _zip_dirent_size(zip_source_t *src, zip_uint16_t flags, zip_error_t *error) {
851
19.2k
    zip_int32_t size;
852
19.2k
    bool local = (flags & ZIP_EF_LOCAL) != 0;
853
19.2k
    int i;
854
19.2k
    zip_uint8_t b[CDENTRYSIZE];
855
19.2k
    zip_buffer_t *buffer;
856
857
19.2k
    size = local ? LENTRYSIZE : CDENTRYSIZE;
858
859
    /* Central header has fixed fields after size pointers. */
860
19.2k
    if ((buffer = _zip_buffer_new_from_source(src, local ? LENTRYSIZE : 34, b, error)) == NULL) {
861
314
        return -1;
862
314
    }
863
18.9k
    if (_zip_buffer_left(buffer) != (local ? LENTRYSIZE : 34)) {
864
0
        zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_CDIR_ENTRY_INVALID);
865
0
        _zip_buffer_free(buffer);
866
0
        return -1;
867
0
    }
868
869
18.9k
    if (memcmp(_zip_buffer_peek(buffer, 4), (local ? LOCAL_MAGIC : CENTRAL_MAGIC), 4) != 0) {
870
8.82k
        zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_CDIR_ENTRY_INVALID);
871
8.82k
        _zip_buffer_free(buffer);
872
8.82k
        return -1;
873
8.82k
    }
874
875
    /* Skip to size pointers. */
876
10.1k
    _zip_buffer_skip(buffer, local ? 26 : 28);
877
878
30.3k
    for (i = 0; i < (local ? 2 : 3); i++) {
879
20.2k
        size += _zip_buffer_get_16(buffer);
880
20.2k
    }
881
882
10.1k
    _zip_buffer_free(buffer);
883
10.1k
    return size;
884
18.9k
}
885
886
887
/* _zip_dirent_write
888
   Writes zip directory entry.
889
890
   If flags & ZIP_EF_LOCAL, it writes a local header instead of a central
891
   directory entry.  If flags & ZIP_EF_FORCE_ZIP64, a ZIP64 extra field is written, even if not needed.
892
893
   Returns 0 if successful, 1 if successful and wrote ZIP64 extra field. On error, error is filled in and -1 is
894
   returned.
895
*/
896
897
0
int _zip_dirent_write(zip_t *za, zip_dirent_t *de, zip_flags_t flags) {
898
0
    zip_dostime_t dostime;
899
0
    zip_encoding_type_t com_enc, name_enc;
900
0
    zip_extra_field_t *ef;
901
0
    zip_extra_field_t *ef64;
902
0
    zip_int32_t ef_size, de_ef_size;
903
0
    bool is_zip64;
904
0
    bool is_really_zip64;
905
0
    bool is_winzip_aes;
906
0
    zip_uint8_t buf[CDENTRYSIZE];
907
0
    zip_buffer_t *buffer;
908
909
0
    ef = NULL;
910
911
0
    name_enc = _zip_guess_encoding(de->filename, ZIP_ENCODING_UNKNOWN);
912
0
    com_enc = _zip_guess_encoding(de->comment, ZIP_ENCODING_UNKNOWN);
913
914
0
    if ((name_enc == ZIP_ENCODING_UTF8_KNOWN && com_enc == ZIP_ENCODING_ASCII) || (name_enc == ZIP_ENCODING_ASCII && com_enc == ZIP_ENCODING_UTF8_KNOWN) || (name_enc == ZIP_ENCODING_UTF8_KNOWN && com_enc == ZIP_ENCODING_UTF8_KNOWN)) {
915
0
        de->bitflags |= ZIP_GPBF_ENCODING_UTF_8;
916
0
    }
917
0
    else {
918
0
        de->bitflags &= (zip_uint16_t)~ZIP_GPBF_ENCODING_UTF_8;
919
0
        if (name_enc == ZIP_ENCODING_UTF8_KNOWN) {
920
0
            ef = _zip_ef_utf8(ZIP_EF_UTF_8_NAME, de->filename, &za->error);
921
0
            if (ef == NULL) {
922
0
                return -1;
923
0
            }
924
0
        }
925
0
        if ((flags & ZIP_FL_LOCAL) == 0 && com_enc == ZIP_ENCODING_UTF8_KNOWN) {
926
0
            zip_extra_field_t *ef2 = _zip_ef_utf8(ZIP_EF_UTF_8_COMMENT, de->comment, &za->error);
927
0
            if (ef2 == NULL) {
928
0
                _zip_ef_free(ef);
929
0
                return -1;
930
0
            }
931
0
            ef2->next = ef;
932
0
            ef = ef2;
933
0
        }
934
0
    }
935
936
0
    if (de->encryption_method == ZIP_EM_NONE) {
937
0
        de->bitflags &= (zip_uint16_t)~ZIP_GPBF_ENCRYPTED;
938
0
    }
939
0
    else {
940
0
        de->bitflags |= (zip_uint16_t)ZIP_GPBF_ENCRYPTED;
941
0
    }
942
943
0
    is_really_zip64 = _zip_dirent_needs_zip64(de, flags);
944
0
    is_zip64 = (flags & (ZIP_FL_LOCAL | ZIP_FL_FORCE_ZIP64)) == (ZIP_FL_LOCAL | ZIP_FL_FORCE_ZIP64) || is_really_zip64;
945
0
    is_winzip_aes = de->encryption_method == ZIP_EM_AES_128 || de->encryption_method == ZIP_EM_AES_192 || de->encryption_method == ZIP_EM_AES_256;
946
947
0
    if (is_zip64) {
948
0
        zip_uint8_t ef_zip64[EFZIP64SIZE];
949
0
        zip_buffer_t *ef_buffer = _zip_buffer_new(ef_zip64, sizeof(ef_zip64));
950
0
        if (ef_buffer == NULL) {
951
0
            zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
952
0
            _zip_ef_free(ef);
953
0
            return -1;
954
0
        }
955
956
0
        if (flags & ZIP_FL_LOCAL) {
957
0
            if ((flags & ZIP_FL_FORCE_ZIP64) || de->comp_size > ZIP_UINT32_MAX || de->uncomp_size > ZIP_UINT32_MAX) {
958
0
                _zip_buffer_put_64(ef_buffer, de->uncomp_size);
959
0
                _zip_buffer_put_64(ef_buffer, de->comp_size);
960
0
            }
961
0
        }
962
0
        else {
963
0
            if ((flags & ZIP_FL_FORCE_ZIP64) || de->comp_size > ZIP_UINT32_MAX || de->uncomp_size > ZIP_UINT32_MAX || de->offset > ZIP_UINT32_MAX) {
964
0
                if (de->uncomp_size >= ZIP_UINT32_MAX) {
965
0
                    _zip_buffer_put_64(ef_buffer, de->uncomp_size);
966
0
                }
967
0
                if (de->comp_size >= ZIP_UINT32_MAX) {
968
0
                    _zip_buffer_put_64(ef_buffer, de->comp_size);
969
0
                }
970
0
                if (de->offset >= ZIP_UINT32_MAX) {
971
0
                    _zip_buffer_put_64(ef_buffer, de->offset);
972
0
                }
973
0
            }
974
0
        }
975
976
0
        if (!_zip_buffer_ok(ef_buffer)) {
977
0
            zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
978
0
            _zip_buffer_free(ef_buffer);
979
0
            _zip_ef_free(ef);
980
0
            return -1;
981
0
        }
982
983
0
        ef64 = _zip_ef_new(ZIP_EF_ZIP64, (zip_uint16_t)(_zip_buffer_offset(ef_buffer)), ef_zip64);
984
0
        _zip_buffer_free(ef_buffer);
985
0
        if (ef64 == NULL) {
986
0
            zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
987
0
            _zip_ef_free(ef);
988
0
            return -1;
989
0
        }
990
0
        ef64->next = ef;
991
0
        ef = ef64;
992
0
    }
993
994
0
    if (is_winzip_aes) {
995
0
        zip_uint8_t data[EF_WINZIP_AES_SIZE];
996
0
        zip_buffer_t *ef_buffer = _zip_buffer_new(data, sizeof(data));
997
0
        zip_extra_field_t *ef_winzip;
998
999
0
        if (ef_buffer == NULL) {
1000
0
            zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
1001
0
            _zip_ef_free(ef);
1002
0
            return -1;
1003
0
        }
1004
1005
0
        _zip_buffer_put_16(ef_buffer, 2);
1006
0
        _zip_buffer_put(ef_buffer, "AE", 2);
1007
0
        _zip_buffer_put_8(ef_buffer, (zip_uint8_t)(de->encryption_method & 0xff));
1008
0
        _zip_buffer_put_16(ef_buffer, (zip_uint16_t)de->comp_method);
1009
1010
0
        if (!_zip_buffer_ok(ef_buffer)) {
1011
0
            zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
1012
0
            _zip_buffer_free(ef_buffer);
1013
0
            _zip_ef_free(ef);
1014
0
            return -1;
1015
0
        }
1016
1017
0
        ef_winzip = _zip_ef_new(ZIP_EF_WINZIP_AES, EF_WINZIP_AES_SIZE, data);
1018
0
        _zip_buffer_free(ef_buffer);
1019
0
        if (ef_winzip == NULL) {
1020
0
            zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
1021
0
            _zip_ef_free(ef);
1022
0
            return -1;
1023
0
        }
1024
0
        ef_winzip->next = ef;
1025
0
        ef = ef_winzip;
1026
0
    }
1027
1028
0
    if ((buffer = _zip_buffer_new(buf, sizeof(buf))) == NULL) {
1029
0
        zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
1030
0
        _zip_ef_free(ef);
1031
0
        return -1;
1032
0
    }
1033
1034
0
    _zip_buffer_put(buffer, (flags & ZIP_FL_LOCAL) ? LOCAL_MAGIC : CENTRAL_MAGIC, 4);
1035
1036
0
    if ((flags & ZIP_FL_LOCAL) == 0) {
1037
0
        _zip_buffer_put_16(buffer, de->version_madeby);
1038
0
    }
1039
0
    _zip_buffer_put_16(buffer, ZIP_MAX(is_really_zip64 ? 45 : 0, de->version_needed));
1040
0
    _zip_buffer_put_16(buffer, de->bitflags);
1041
0
    if (is_winzip_aes) {
1042
0
        _zip_buffer_put_16(buffer, ZIP_CM_WINZIP_AES);
1043
0
    }
1044
0
    else {
1045
0
        _zip_buffer_put_16(buffer, (zip_uint16_t)ZIP_CM_ACTUAL(de->comp_method));
1046
0
    }
1047
1048
0
    if (ZIP_WANT_TORRENTZIP(za)) {
1049
0
        dostime.time = 0xbc00;
1050
0
        dostime.date = 0x2198;
1051
0
    }
1052
0
    else {
1053
0
        dostime = de->last_mod;
1054
0
    }
1055
0
    _zip_buffer_put_16(buffer, dostime.time);
1056
0
    _zip_buffer_put_16(buffer, dostime.date);
1057
1058
0
    if (is_winzip_aes && de->uncomp_size < 20) {
1059
0
        _zip_buffer_put_32(buffer, 0);
1060
0
    }
1061
0
    else {
1062
0
        _zip_buffer_put_32(buffer, de->crc);
1063
0
    }
1064
1065
0
    if (((flags & ZIP_FL_LOCAL) == ZIP_FL_LOCAL) && ((de->comp_size >= ZIP_UINT32_MAX) || (de->uncomp_size >= ZIP_UINT32_MAX))) {
1066
        /* In local headers, if a ZIP64 EF is written, it MUST contain
1067
         * both compressed and uncompressed sizes (even if one of the
1068
         * two is smaller than 0xFFFFFFFF); on the other hand, those
1069
         * may only appear when the corresponding standard entry is
1070
         * 0xFFFFFFFF.  (appnote.txt 4.5.3) */
1071
0
        _zip_buffer_put_32(buffer, ZIP_UINT32_MAX);
1072
0
        _zip_buffer_put_32(buffer, ZIP_UINT32_MAX);
1073
0
    }
1074
0
    else {
1075
0
        if (de->comp_size < ZIP_UINT32_MAX) {
1076
0
            _zip_buffer_put_32(buffer, (zip_uint32_t)de->comp_size);
1077
0
        }
1078
0
        else {
1079
0
            _zip_buffer_put_32(buffer, ZIP_UINT32_MAX);
1080
0
        }
1081
0
        if (de->uncomp_size < ZIP_UINT32_MAX) {
1082
0
            _zip_buffer_put_32(buffer, (zip_uint32_t)de->uncomp_size);
1083
0
        }
1084
0
        else {
1085
0
            _zip_buffer_put_32(buffer, ZIP_UINT32_MAX);
1086
0
        }
1087
0
    }
1088
1089
0
    _zip_buffer_put_16(buffer, _zip_string_length(de->filename));
1090
1091
0
    ef_size = _zip_ef_size(ef);
1092
0
    if (!ZIP_WANT_TORRENTZIP(za)) {
1093
0
        de_ef_size = _zip_ef_size((flags & ZIP_FL_LOCAL) ? de->extra_fields.local : de->extra_fields.central);
1094
0
    }
1095
0
    else {
1096
0
        de_ef_size = 0;
1097
0
    }
1098
0
    if (ef_size < 0 || de_ef_size < 0 || ef_size + de_ef_size > ZIP_UINT16_MAX) {
1099
0
        zip_error_set(&za->error, ZIP_ER_EF_TOO_LARGE, 0);
1100
0
        _zip_buffer_free(buffer);
1101
0
        _zip_ef_free(ef);
1102
0
        return -1;
1103
0
    }
1104
0
    _zip_buffer_put_16(buffer, (zip_uint16_t)(ef_size + de_ef_size));
1105
1106
0
    if ((flags & ZIP_FL_LOCAL) == 0) {
1107
0
        _zip_buffer_put_16(buffer, ZIP_WANT_TORRENTZIP(za) ? 0 : _zip_string_length(de->comment));
1108
0
        _zip_buffer_put_16(buffer, (zip_uint16_t)de->disk_number);
1109
0
        _zip_buffer_put_16(buffer, de->int_attrib);
1110
0
        _zip_buffer_put_32(buffer, de->ext_attrib);
1111
0
        if (de->offset < ZIP_UINT32_MAX) {
1112
0
            _zip_buffer_put_32(buffer, (zip_uint32_t)de->offset);
1113
0
        }
1114
0
        else {
1115
0
            _zip_buffer_put_32(buffer, ZIP_UINT32_MAX);
1116
0
        }
1117
0
    }
1118
1119
0
    if (!_zip_buffer_ok(buffer)) {
1120
0
        zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
1121
0
        _zip_buffer_free(buffer);
1122
0
        _zip_ef_free(ef);
1123
0
        return -1;
1124
0
    }
1125
1126
0
    if (_zip_write(za, buf, _zip_buffer_offset(buffer)) < 0) {
1127
0
        _zip_buffer_free(buffer);
1128
0
        _zip_ef_free(ef);
1129
0
        return -1;
1130
0
    }
1131
1132
0
    _zip_buffer_free(buffer);
1133
1134
0
    if (de->filename) {
1135
0
        if (_zip_string_write(za, de->filename) < 0) {
1136
0
            _zip_ef_free(ef);
1137
0
            return -1;
1138
0
        }
1139
0
    }
1140
1141
0
    if (ef) {
1142
0
        if (_zip_ef_write(za, ef) < 0) {
1143
0
            _zip_ef_free(ef);
1144
0
            return -1;
1145
0
        }
1146
0
    }
1147
0
    _zip_ef_free(ef);
1148
0
    ef = (flags & ZIP_FL_LOCAL) ? de->extra_fields.local : de->extra_fields.central;
1149
0
    if (ef && !ZIP_WANT_TORRENTZIP(za)) {
1150
0
        if (_zip_ef_write(za, ef) < 0) {
1151
0
            return -1;
1152
0
        }
1153
0
    }
1154
1155
0
    if ((flags & ZIP_FL_LOCAL) == 0 && !ZIP_WANT_TORRENTZIP(za)) {
1156
0
        if (de->comment) {
1157
0
            if (_zip_string_write(za, de->comment) < 0) {
1158
0
                return -1;
1159
0
            }
1160
0
        }
1161
0
    }
1162
1163
1164
0
    return is_zip64;
1165
0
}
1166
1167
1168
29.3k
time_t _zip_d2u_time(const zip_dostime_t *dtime) {
1169
29.3k
    struct tm tm;
1170
1171
29.3k
    memset(&tm, 0, sizeof(tm));
1172
1173
    /* let mktime decide if DST is in effect */
1174
29.3k
    tm.tm_isdst = -1;
1175
1176
29.3k
    tm.tm_year = ((dtime->date >> 9) & 127) + 1980 - 1900;
1177
29.3k
    tm.tm_mon = ((dtime->date >> 5) & 15) - 1;
1178
29.3k
    tm.tm_mday = dtime->date & 31;
1179
1180
29.3k
    tm.tm_hour = (dtime->time >> 11) & 31;
1181
29.3k
    tm.tm_min = (dtime->time >> 5) & 63;
1182
29.3k
    tm.tm_sec = (dtime->time << 1) & 62;
1183
1184
29.3k
    return mktime(&tm);
1185
29.3k
}
1186
1187
1188
0
static zip_extra_field_t *_zip_ef_utf8(zip_uint16_t id, zip_string_t *str, zip_error_t *error) {
1189
0
    const zip_uint8_t *raw;
1190
0
    zip_uint32_t len;
1191
0
    zip_buffer_t *buffer;
1192
0
    zip_extra_field_t *ef;
1193
1194
0
    if ((raw = _zip_string_get(str, &len, ZIP_FL_ENC_RAW, NULL)) == NULL) {
1195
        /* error already set */
1196
0
        return NULL;
1197
0
    }
1198
1199
0
    if (len + 5 > ZIP_UINT16_MAX) {
1200
0
        zip_error_set(error, ZIP_ER_INVAL, 0); /* TODO: better error code? */
1201
0
        return NULL;
1202
0
    }
1203
1204
0
    if ((buffer = _zip_buffer_new(NULL, len + 5)) == NULL) {
1205
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
1206
0
        return NULL;
1207
0
    }
1208
1209
0
    _zip_buffer_put_8(buffer, 1);
1210
0
    _zip_buffer_put_32(buffer, _zip_string_crc32(str));
1211
0
    _zip_buffer_put(buffer, raw, len);
1212
1213
0
    if (!_zip_buffer_ok(buffer)) {
1214
0
        zip_error_set(error, ZIP_ER_INTERNAL, 0);
1215
0
        _zip_buffer_free(buffer);
1216
0
        return NULL;
1217
0
    }
1218
1219
0
    ef = _zip_ef_new(id, (zip_uint16_t)(_zip_buffer_offset(buffer)), _zip_buffer_data(buffer));
1220
0
    _zip_buffer_free(buffer);
1221
1222
0
    return ef;
1223
0
}
1224
1225
1226
88.0k
zip_dirent_t *_zip_get_dirent(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_error_t *error) {
1227
88.0k
    if (error == NULL) {
1228
29.3k
        error = &za->error;
1229
29.3k
    }
1230
1231
88.0k
    if (idx >= za->nentry) {
1232
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
1233
0
        return NULL;
1234
0
    }
1235
1236
88.0k
    if ((flags & ZIP_FL_UNCHANGED) || za->entry[idx].changes == NULL) {
1237
88.0k
        if (za->entry[idx].orig == NULL) {
1238
0
            zip_error_set(error, ZIP_ER_INVAL, 0);
1239
0
            return NULL;
1240
0
        }
1241
88.0k
        if (za->entry[idx].deleted && (flags & ZIP_FL_UNCHANGED) == 0) {
1242
0
            zip_error_set(error, ZIP_ER_DELETED, 0);
1243
0
            return NULL;
1244
0
        }
1245
88.0k
        return za->entry[idx].orig;
1246
88.0k
    }
1247
0
    else {
1248
0
        return za->entry[idx].changes;
1249
0
    }
1250
88.0k
}
1251
1252
1253
0
int _zip_u2d_time(time_t intime, zip_dostime_t *dtime, zip_error_t *ze) {
1254
0
    struct tm *tpm;
1255
0
    struct tm tm;
1256
0
    tpm = zip_localtime(&intime, &tm);
1257
0
    if (tpm == NULL) {
1258
        /* if localtime fails, return an arbitrary date (1980-01-01 00:00:00) */
1259
0
        dtime->date = (1 << 5) + 1;
1260
0
        dtime->time = 0;
1261
0
        if (ze) {
1262
0
            zip_error_set(ze, ZIP_ER_INVAL, errno);
1263
0
        }
1264
0
        return -1;
1265
0
    }
1266
0
    if (tpm->tm_year < 80) {
1267
0
        tpm->tm_year = 80;
1268
0
    }
1269
1270
0
    dtime->date = (zip_uint16_t)(((tpm->tm_year + 1900 - 1980) << 9) + ((tpm->tm_mon + 1) << 5) + tpm->tm_mday);
1271
0
    dtime->time = (zip_uint16_t)(((tpm->tm_hour) << 11) + ((tpm->tm_min) << 5) + ((tpm->tm_sec) >> 1));
1272
1273
0
    return 0;
1274
0
}
1275
1276
1277
0
bool _zip_dirent_apply_attributes(zip_dirent_t *de, zip_file_attributes_t *attributes, bool force_zip64) {
1278
0
    zip_uint16_t length;
1279
0
    bool has_changed = false;
1280
0
    zip_uint16_t version_madeby, version_needed;
1281
1282
0
    if (attributes->valid & ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS) {
1283
0
        zip_uint16_t mask = attributes->general_purpose_bit_mask & ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS_ALLOWED_MASK;
1284
0
        zip_uint16_t bitflags = (de->bitflags & ~mask) | (attributes->general_purpose_bit_flags & mask);
1285
0
        if (de->bitflags != bitflags) {
1286
0
            de->bitflags = bitflags;
1287
0
            has_changed = true;
1288
0
        }
1289
0
    }
1290
0
    if (attributes->valid & ZIP_FILE_ATTRIBUTES_ASCII) {
1291
0
        zip_uint16_t int_attrib = (de->int_attrib & ~0x1) | (attributes->ascii ? 1 : 0);
1292
0
        if (de->int_attrib != int_attrib) {
1293
0
            de->int_attrib = int_attrib;
1294
0
            has_changed = true;
1295
0
        }
1296
0
    }
1297
    /* manually set attributes are preferred over attributes provided by source */
1298
0
    if ((de->changed & ZIP_DIRENT_ATTRIBUTES) == 0 && (attributes->valid & ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES)) {
1299
0
        if (de->ext_attrib != attributes->external_file_attributes) {
1300
0
            de->ext_attrib = attributes->external_file_attributes;
1301
0
            has_changed = true;
1302
0
        }
1303
0
    }
1304
1305
0
    if (de->comp_method == ZIP_CM_LZMA) {
1306
0
        version_needed = 63;
1307
0
    }
1308
0
    else if (de->encryption_method == ZIP_EM_AES_128 || de->encryption_method == ZIP_EM_AES_192 || de->encryption_method == ZIP_EM_AES_256) {
1309
0
        version_needed = 51;
1310
0
    }
1311
0
    else if (de->comp_method == ZIP_CM_BZIP2) {
1312
0
        version_needed = 46;
1313
0
    }
1314
0
    else if (force_zip64 || _zip_dirent_needs_zip64(de, 0)) {
1315
0
        version_needed = 45;
1316
0
    }
1317
0
    else if (de->comp_method == ZIP_CM_DEFLATE || de->encryption_method == ZIP_EM_TRAD_PKWARE) {
1318
0
        version_needed = 20;
1319
0
    }
1320
0
    else if ((length = _zip_string_length(de->filename)) > 0 && de->filename->raw[length - 1] == '/') {
1321
0
        version_needed = 20;
1322
0
    }
1323
0
    else {
1324
0
        version_needed = 10;
1325
0
    }
1326
1327
0
    if (attributes->valid & ZIP_FILE_ATTRIBUTES_VERSION_NEEDED) {
1328
0
        version_needed = ZIP_MAX(version_needed, attributes->version_needed);
1329
0
    }
1330
1331
0
    if (de->version_needed != version_needed) {
1332
0
        de->version_needed = version_needed;
1333
0
        has_changed = true;
1334
0
    }
1335
1336
0
    version_madeby = 63 | (de->version_madeby & 0xff00);
1337
0
    if ((de->changed & ZIP_DIRENT_ATTRIBUTES) == 0 && (attributes->valid & ZIP_FILE_ATTRIBUTES_HOST_SYSTEM)) {
1338
0
        version_madeby = (version_madeby & 0xff) | (zip_uint16_t)(attributes->host_system << 8);
1339
0
    }
1340
0
    if (de->version_madeby != version_madeby) {
1341
0
        de->version_madeby = version_madeby;
1342
0
        has_changed = true;
1343
0
    }
1344
1345
0
    return has_changed;
1346
0
}
1347
1348
1349
/* _zip_dirent_torrent_normalize(de);
1350
   Set values suitable for torrentzip.
1351
*/
1352
1353
0
void zip_dirent_torrentzip_normalize(zip_dirent_t *de) {
1354
0
    de->version_madeby = 0;
1355
0
    de->version_needed = 20; /* 2.0 */
1356
0
    de->bitflags = 2;        /* maximum compression */
1357
0
    de->comp_method = ZIP_CM_DEFLATE;
1358
0
    de->compression_level = TORRENTZIP_COMPRESSION_FLAGS;
1359
0
    de->disk_number = 0;
1360
0
    de->int_attrib = 0;
1361
0
    de->ext_attrib = 0;
1362
1363
    /* last_mod, extra_fields, and comment are normalized in zip_dirent_write() directly */
1364
0
}
1365
1366
0
int zip_dirent_check_consistency(zip_dirent_t *dirent) {
1367
0
    if (dirent->comp_method == ZIP_CM_STORE) {
1368
0
        zip_uint64_t header_size = 0;
1369
0
        switch (dirent->encryption_method) {
1370
0
        case ZIP_EM_NONE:
1371
0
            break;
1372
0
        case ZIP_EM_TRAD_PKWARE:
1373
0
            header_size = 12;
1374
0
            break;
1375
0
        case ZIP_EM_AES_128:
1376
0
            header_size = 20;
1377
0
            break;
1378
0
        case ZIP_EM_AES_192:
1379
0
            header_size = 24;
1380
0
            break;
1381
0
        case ZIP_EM_AES_256:
1382
0
            header_size = 28;
1383
0
            break;
1384
1385
0
        default:
1386
0
            return 0;
1387
0
        }
1388
0
        if (dirent->uncomp_size + header_size < dirent->uncomp_size || dirent->comp_size != dirent->uncomp_size + header_size) {
1389
0
            return ZIP_ER_DETAIL_STORED_SIZE_MISMATCH;
1390
0
        }
1391
0
    }
1392
0
    return 0;
1393
0
}
1394
1395
29.3k
time_t zip_dirent_get_last_mod_mtime(zip_dirent_t *de) {
1396
29.3k
    if (!de->last_mod_mtime_valid) {
1397
29.3k
        de->last_mod_mtime = _zip_d2u_time(&de->last_mod);
1398
29.3k
        de->last_mod_mtime_valid = true;
1399
29.3k
    }
1400
1401
29.3k
    return de->last_mod_mtime;
1402
29.3k
}