Coverage Report

Created: 2025-07-11 06:45

/src/minizip-ng/mz_zip.c
Line
Count
Source (jump to first uncovered line)
1
/* zip.c -- Zip manipulation
2
   part of the minizip-ng project
3
4
   Copyright (C) Nathan Moinvaziri
5
     https://github.com/zlib-ng/minizip-ng
6
   Copyright (C) 2009-2010 Mathias Svensson
7
     Modifications for Zip64 support
8
     http://result42.com
9
   Copyright (C) 2007-2008 Even Rouault
10
     Modifications of Unzip for Zip64
11
   Copyright (C) 1998-2010 Gilles Vollant
12
     https://www.winimage.com/zLibDll/minizip.html
13
14
   This program is distributed under the terms of the same license as zlib.
15
   See the accompanying LICENSE file for the full text of the license.
16
*/
17
18
#include "mz.h"
19
#include "mz_crypt.h"
20
#include "mz_os.h"
21
#include "mz_strm.h"
22
#ifdef HAVE_BZIP2
23
#  include "mz_strm_bzip.h"
24
#endif
25
#ifdef HAVE_LIBCOMP
26
#  include "mz_strm_libcomp.h"
27
#endif
28
#ifdef HAVE_LZMA
29
#  include "mz_strm_lzma.h"
30
#endif
31
#include "mz_strm_mem.h"
32
#ifdef HAVE_PKCRYPT
33
#  include "mz_strm_pkcrypt.h"
34
#endif
35
#ifdef HAVE_WZAES
36
#  include "mz_strm_wzaes.h"
37
#endif
38
#ifdef HAVE_ZLIB
39
#  include "mz_strm_zlib.h"
40
#endif
41
#ifdef HAVE_ZSTD
42
#  include "mz_strm_zstd.h"
43
#endif
44
45
#include "mz_zip.h"
46
47
#include <ctype.h> /* tolower */
48
#include <stdio.h> /* snprintf */
49
50
#if defined(_MSC_VER) || defined(__MINGW32__)
51
#  define localtime_r(t1, t2) (localtime_s(t2, t1) == 0 ? t1 : NULL)
52
#endif
53
#if defined(_MSC_VER) && (_MSC_VER < 1900)
54
#  define snprintf _snprintf
55
#endif
56
57
/***************************************************************************/
58
59
348
#define MZ_ZIP_MAGIC_LOCALHEADER        (0x04034b50)
60
0
#define MZ_ZIP_MAGIC_LOCALHEADERU8      {0x50, 0x4b, 0x03, 0x04}
61
348
#define MZ_ZIP_MAGIC_CENTRALHEADER      (0x02014b50)
62
0
#define MZ_ZIP_MAGIC_CENTRALHEADERU8    {0x50, 0x4b, 0x01, 0x02}
63
437
#define MZ_ZIP_MAGIC_ENDHEADER          (0x06054b50)
64
0
#define MZ_ZIP_MAGIC_ENDHEADERU8        {0x50, 0x4b, 0x05, 0x06}
65
0
#define MZ_ZIP_MAGIC_ENDHEADER64        (0x06064b50)
66
0
#define MZ_ZIP_MAGIC_ENDLOCHEADER64     (0x07064b50)
67
348
#define MZ_ZIP_MAGIC_DATADESCRIPTOR     (0x08074b50)
68
0
#define MZ_ZIP_MAGIC_DATADESCRIPTORU8   {0x50, 0x4b, 0x07, 0x08}
69
70
0
#define MZ_ZIP_SIZE_LD_ITEM             (30)
71
0
#define MZ_ZIP_SIZE_CD_ITEM             (46)
72
0
#define MZ_ZIP_SIZE_CD_LOCATOR64        (20)
73
0
#define MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR (24)
74
75
0
#define MZ_ZIP_OFFSET_CRC_SIZES         (14)
76
696
#define MZ_ZIP_UNCOMPR_SIZE64_CUSHION   (2 * 1024 * 1024)
77
78
#ifndef MZ_ZIP_EOCD_MAX_BACK
79
0
#  define MZ_ZIP_EOCD_MAX_BACK (1 << 20)
80
#endif
81
82
/***************************************************************************/
83
84
typedef struct mz_zip_s {
85
    mz_zip_file file_info;
86
    mz_zip_file local_file_info;
87
88
    void *stream;                 /* main stream */
89
    void *cd_stream;              /* pointer to the stream with the cd */
90
    void *cd_mem_stream;          /* memory stream for central directory */
91
    void *compress_stream;        /* compression stream */
92
    void *crypt_stream;           /* encryption stream */
93
    void *file_info_stream;       /* memory stream for storing file info */
94
    void *local_file_info_stream; /* memory stream for storing local file info */
95
96
    int32_t open_mode;
97
    uint8_t recover;
98
    uint8_t data_descriptor;
99
100
    uint32_t disk_number_with_cd; /* number of the disk with the central dir */
101
    int64_t disk_offset_shift;    /* correction for zips that have wrong offset start of cd */
102
103
    int64_t cd_start_pos;   /* pos of the first file in the central dir stream */
104
    int64_t cd_current_pos; /* pos of the current file in the central dir */
105
    int64_t cd_offset;      /* offset of start of central directory */
106
    int64_t cd_size;        /* size of the central directory */
107
    uint32_t cd_signature;  /* signature of central directory */
108
109
    uint8_t entry_scanned; /* entry header information read ok */
110
    uint8_t entry_opened;  /* entry is open for read/write */
111
    uint8_t entry_raw;     /* entry opened with raw mode */
112
    uint32_t entry_crc32;  /* entry crc32  */
113
114
    uint64_t number_entry;
115
116
    uint16_t version_madeby;
117
    char *comment;
118
} mz_zip;
119
120
/***************************************************************************/
121
122
#if 0
123
#  define mz_zip_print printf
124
#else
125
#  define mz_zip_print(fmt, ...)
126
#endif
127
128
/***************************************************************************/
129
130
/* Locate the end of central directory */
131
0
static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos) {
132
0
    int64_t file_size = 0;
133
0
    int64_t max_back = MZ_ZIP_EOCD_MAX_BACK;
134
0
    uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8;
135
0
    int32_t err = MZ_OK;
136
137
0
    err = mz_stream_seek(stream, 0, MZ_SEEK_END);
138
0
    if (err != MZ_OK)
139
0
        return err;
140
141
0
    file_size = mz_stream_tell(stream);
142
143
0
    if (max_back <= 0 || max_back > file_size)
144
0
        max_back = file_size;
145
146
0
    return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos);
147
0
}
148
149
/* Locate the end of central directory 64 of a zip file */
150
0
static int32_t mz_zip_search_zip64_eocd(void *stream, const int64_t end_central_offset, int64_t *central_pos) {
151
0
    int64_t offset = 0;
152
0
    uint32_t value32 = 0;
153
0
    int32_t err = MZ_OK;
154
155
0
    *central_pos = 0;
156
157
    /* Zip64 end of central directory locator */
158
0
    err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
159
    /* Read locator signature */
160
0
    if (err == MZ_OK) {
161
0
        err = mz_stream_read_uint32(stream, &value32);
162
0
        if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
163
0
            err = MZ_FORMAT_ERROR;
164
0
    }
165
    /* Number of the disk with the start of the zip64 end of  central directory */
166
0
    if (err == MZ_OK)
167
0
        err = mz_stream_read_uint32(stream, &value32);
168
    /* Relative offset of the zip64 end of central directory record8 */
169
0
    if (err == MZ_OK)
170
0
        err = mz_stream_read_uint64(stream, (uint64_t *)&offset);
171
    /* Total number of disks */
172
0
    if (err == MZ_OK)
173
0
        err = mz_stream_read_uint32(stream, &value32);
174
    /* Goto end of central directory record */
175
0
    if (err == MZ_OK)
176
0
        err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET);
177
    /* The signature */
178
0
    if (err == MZ_OK) {
179
0
        err = mz_stream_read_uint32(stream, &value32);
180
0
        if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
181
0
            err = MZ_FORMAT_ERROR;
182
0
    }
183
184
0
    if (err == MZ_OK)
185
0
        *central_pos = offset;
186
187
0
    return err;
188
0
}
189
190
#ifdef HAVE_PKCRYPT
191
/* Get PKWARE traditional encryption verifier */
192
190
static uint16_t mz_zip_get_pk_verify(uint32_t dos_date, uint64_t crc, uint16_t flag) {
193
    /* Info-ZIP modification to ZipCrypto format: if bit 3 of the general
194
     * purpose bit flag is set, it uses high byte of 16-bit File Time. */
195
190
    if (flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
196
190
        return ((dos_date >> 16) & 0xff) << 8 | ((dos_date >> 8) & 0xff);
197
0
    return ((crc >> 16) & 0xff) << 8 | ((crc >> 24) & 0xff);
198
190
}
199
#endif
200
201
/* Get info about the current file in the zip file */
202
0
static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file *file_info, void *file_extra_stream) {
203
0
    uint64_t ntfs_time = 0;
204
0
    uint32_t reserved = 0;
205
0
    uint32_t magic = 0;
206
0
    uint32_t dos_date = 0;
207
0
    uint32_t field_pos = 0;
208
0
    uint16_t field_type = 0;
209
0
    uint16_t field_length = 0;
210
0
    uint32_t field_length_read = 0;
211
0
    uint16_t ntfs_attrib_id = 0;
212
0
    uint16_t ntfs_attrib_size = 0;
213
0
    uint16_t linkname_size;
214
0
    uint16_t value16 = 0;
215
0
    uint32_t value32 = 0;
216
0
    int64_t extrafield_pos = 0;
217
0
    int64_t comment_pos = 0;
218
0
    int64_t linkname_pos = 0;
219
0
    int64_t saved_pos = 0;
220
0
    int32_t err = MZ_OK;
221
0
    char *linkname = NULL;
222
223
0
    memset(file_info, 0, sizeof(mz_zip_file));
224
225
    /* Check the magic */
226
0
    err = mz_stream_read_uint32(stream, &magic);
227
0
    if (err == MZ_END_OF_STREAM)
228
0
        err = MZ_END_OF_LIST;
229
0
    else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
230
0
        err = MZ_END_OF_LIST;
231
0
    else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
232
0
        err = MZ_FORMAT_ERROR;
233
0
    else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
234
0
        err = MZ_FORMAT_ERROR;
235
236
    /* Read header fields */
237
0
    if (err == MZ_OK) {
238
0
        if (!local)
239
0
            err = mz_stream_read_uint16(stream, &file_info->version_madeby);
240
0
        if (err == MZ_OK)
241
0
            err = mz_stream_read_uint16(stream, &file_info->version_needed);
242
0
        if (err == MZ_OK)
243
0
            err = mz_stream_read_uint16(stream, &file_info->flag);
244
0
        if (err == MZ_OK)
245
0
            err = mz_stream_read_uint16(stream, &file_info->compression_method);
246
0
        if (err == MZ_OK) {
247
0
            err = mz_stream_read_uint32(stream, &dos_date);
248
0
            file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
249
0
        }
250
0
        if (err == MZ_OK)
251
0
            err = mz_stream_read_uint32(stream, &file_info->crc);
252
0
#ifdef HAVE_PKCRYPT
253
0
        if (err == MZ_OK && file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) {
254
            /* Use dos_date from header instead of derived from time in zip extensions */
255
0
            file_info->pk_verify = mz_zip_get_pk_verify(dos_date, file_info->crc, file_info->flag);
256
0
        }
257
0
#endif
258
0
        if (err == MZ_OK) {
259
0
            err = mz_stream_read_uint32(stream, &value32);
260
0
            file_info->compressed_size = value32;
261
0
        }
262
0
        if (err == MZ_OK) {
263
0
            err = mz_stream_read_uint32(stream, &value32);
264
0
            file_info->uncompressed_size = value32;
265
0
        }
266
0
        if (err == MZ_OK)
267
0
            err = mz_stream_read_uint16(stream, &file_info->filename_size);
268
0
        if (err == MZ_OK)
269
0
            err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
270
0
        if (!local) {
271
0
            if (err == MZ_OK)
272
0
                err = mz_stream_read_uint16(stream, &file_info->comment_size);
273
0
            if (err == MZ_OK) {
274
0
                err = mz_stream_read_uint16(stream, &value16);
275
0
                file_info->disk_number = value16;
276
0
            }
277
0
            if (err == MZ_OK)
278
0
                err = mz_stream_read_uint16(stream, &file_info->internal_fa);
279
0
            if (err == MZ_OK)
280
0
                err = mz_stream_read_uint32(stream, &file_info->external_fa);
281
0
            if (err == MZ_OK) {
282
0
                err = mz_stream_read_uint32(stream, &value32);
283
0
                file_info->disk_offset = value32;
284
0
            }
285
0
        }
286
0
    }
287
288
0
    if (err == MZ_OK)
289
0
        err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET);
290
291
    /* Copy variable length data to memory stream for later retrieval */
292
0
    if ((err == MZ_OK) && (file_info->filename_size > 0))
293
0
        err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size);
294
0
    mz_stream_write_uint8(file_extra_stream, 0);
295
0
    extrafield_pos = mz_stream_tell(file_extra_stream);
296
297
0
    if ((err == MZ_OK) && (file_info->extrafield_size > 0))
298
0
        err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size);
299
0
    mz_stream_write_uint8(file_extra_stream, 0);
300
301
0
    comment_pos = mz_stream_tell(file_extra_stream);
302
0
    if ((err == MZ_OK) && (file_info->comment_size > 0))
303
0
        err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size);
304
0
    mz_stream_write_uint8(file_extra_stream, 0);
305
306
0
    linkname_pos = mz_stream_tell(file_extra_stream);
307
    /* Overwrite if we encounter UNIX1 extra block */
308
0
    mz_stream_write_uint8(file_extra_stream, 0);
309
310
0
    if ((err == MZ_OK) && (file_info->extrafield_size > 0)) {
311
        /* Seek to and parse the extra field */
312
0
        err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET);
313
314
0
        while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size)) {
315
0
            err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length);
316
0
            if (err != MZ_OK)
317
0
                break;
318
0
            field_pos += 4;
319
320
            /* Don't allow field length to exceed size of remaining extrafield */
321
0
            if (field_length > (file_info->extrafield_size - field_pos))
322
0
                field_length = (uint16_t)(file_info->extrafield_size - field_pos);
323
324
            /* Read ZIP64 extra field */
325
0
            if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8)) {
326
0
                if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX)) {
327
0
                    err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size);
328
0
                    if (file_info->uncompressed_size < 0)
329
0
                        err = MZ_FORMAT_ERROR;
330
0
                }
331
0
                if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX)) {
332
0
                    err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size);
333
0
                    if (file_info->compressed_size < 0)
334
0
                        err = MZ_FORMAT_ERROR;
335
0
                }
336
0
                if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX)) {
337
0
                    err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset);
338
0
                    if (file_info->disk_offset < 0)
339
0
                        err = MZ_FORMAT_ERROR;
340
0
                }
341
0
                if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
342
0
                    err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number);
343
0
            }
344
            /* Read NTFS extra field */
345
0
            else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4)) {
346
0
                if (err == MZ_OK)
347
0
                    err = mz_stream_read_uint32(file_extra_stream, &reserved);
348
0
                field_length_read = 4;
349
350
0
                while ((err == MZ_OK) && (field_length_read + 4 <= field_length)) {
351
0
                    err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id);
352
0
                    if (err == MZ_OK)
353
0
                        err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size);
354
0
                    field_length_read += 4;
355
356
0
                    if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24)) {
357
0
                        err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
358
0
                        mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
359
360
0
                        if (err == MZ_OK) {
361
0
                            err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
362
0
                            mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
363
0
                        }
364
0
                        if (err == MZ_OK) {
365
0
                            err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
366
0
                            mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
367
0
                        }
368
0
                    } else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length)) {
369
0
                        err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR);
370
0
                    }
371
372
0
                    field_length_read += ntfs_attrib_size;
373
0
                }
374
0
            }
375
            /* Read UNIX1 extra field */
376
0
            else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12)) {
377
0
                if (err == MZ_OK) {
378
0
                    err = mz_stream_read_uint32(file_extra_stream, &value32);
379
0
                    if (err == MZ_OK && file_info->accessed_date == 0)
380
0
                        file_info->accessed_date = value32;
381
0
                }
382
0
                if (err == MZ_OK) {
383
0
                    err = mz_stream_read_uint32(file_extra_stream, &value32);
384
0
                    if (err == MZ_OK && file_info->modified_date == 0)
385
0
                        file_info->modified_date = value32;
386
0
                }
387
0
                if (err == MZ_OK)
388
0
                    err = mz_stream_read_uint16(file_extra_stream, &value16); /* User id */
389
0
                if (err == MZ_OK)
390
0
                    err = mz_stream_read_uint16(file_extra_stream, &value16); /* Group id */
391
392
                /* Copy linkname to end of file extra stream so we can return null
393
                   terminated string */
394
0
                linkname_size = field_length - 12;
395
0
                if ((err == MZ_OK) && (linkname_size > 0)) {
396
0
                    linkname = (char *)malloc(linkname_size);
397
0
                    if (linkname) {
398
0
                        if (mz_stream_read(file_extra_stream, linkname, linkname_size) != linkname_size)
399
0
                            err = MZ_READ_ERROR;
400
0
                        if (err == MZ_OK) {
401
0
                            saved_pos = mz_stream_tell(file_extra_stream);
402
403
0
                            mz_stream_seek(file_extra_stream, linkname_pos, MZ_SEEK_SET);
404
0
                            mz_stream_write(file_extra_stream, linkname, linkname_size);
405
0
                            mz_stream_write_uint8(file_extra_stream, 0);
406
407
0
                            mz_stream_seek(file_extra_stream, saved_pos, MZ_SEEK_SET);
408
0
                        }
409
0
                        free(linkname);
410
0
                    }
411
0
                }
412
0
            }
413
0
#ifdef HAVE_WZAES
414
            /* Read AES extra field */
415
0
            else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7)) {
416
0
                uint8_t value8 = 0;
417
                /* Verify version info */
418
0
                err = mz_stream_read_uint16(file_extra_stream, &value16);
419
                /* Support AE-1 and AE-2 */
420
0
                if (value16 != 1 && value16 != 2)
421
0
                    err = MZ_FORMAT_ERROR;
422
0
                file_info->aes_version = value16;
423
0
                if (err == MZ_OK)
424
0
                    err = mz_stream_read_uint8(file_extra_stream, &value8);
425
0
                if ((char)value8 != 'A')
426
0
                    err = MZ_FORMAT_ERROR;
427
0
                if (err == MZ_OK)
428
0
                    err = mz_stream_read_uint8(file_extra_stream, &value8);
429
0
                if ((char)value8 != 'E')
430
0
                    err = MZ_FORMAT_ERROR;
431
                /* Get AES encryption strength and actual compression method */
432
0
                if (err == MZ_OK) {
433
0
                    err = mz_stream_read_uint8(file_extra_stream, &value8);
434
0
                    file_info->aes_strength = value8;
435
0
                }
436
0
                if (err == MZ_OK) {
437
0
                    err = mz_stream_read_uint16(file_extra_stream, &value16);
438
0
                    file_info->compression_method = value16;
439
0
                }
440
0
            }
441
0
#endif
442
0
            else if (field_length > 0) {
443
0
                err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
444
0
            }
445
446
0
            field_pos += field_length;
447
0
        }
448
0
    }
449
450
    /* Get pointers to variable length data */
451
0
    mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename);
452
0
    mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield);
453
0
    mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment);
454
0
    mz_stream_mem_get_buffer_at(file_extra_stream, linkname_pos, (const void **)&file_info->linkname);
455
456
    /* Set to empty string just in-case */
457
0
    if (!file_info->filename)
458
0
        file_info->filename = "";
459
0
    if (!file_info->extrafield)
460
0
        file_info->extrafield_size = 0;
461
0
    if (!file_info->comment)
462
0
        file_info->comment = "";
463
0
    if (!file_info->linkname)
464
0
        file_info->linkname = "";
465
466
0
    if (err == MZ_OK) {
467
0
        mz_zip_print("Zip - Entry - Read header - %s (local %" PRId8 ")\n", file_info->filename, local);
468
0
        mz_zip_print("Zip - Entry - Read header compress (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
469
0
                     file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
470
0
        if (!local) {
471
0
            mz_zip_print("Zip - Entry - Read header disk (disk %" PRIu32 " offset %" PRId64 ")\n",
472
0
                         file_info->disk_number, file_info->disk_offset);
473
0
        }
474
0
        mz_zip_print("Zip - Entry - Read header variable (fnl %" PRId32 " efs %" PRId32 " cms %" PRId32 ")\n",
475
0
                     file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
476
0
    }
477
478
0
    return err;
479
0
}
480
481
static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size,
482
0
                                            int64_t *uncompressed_size) {
483
0
    uint32_t value32 = 0;
484
0
    int64_t value64 = 0;
485
0
    int32_t err = MZ_OK;
486
487
0
    err = mz_stream_read_uint32(stream, &value32);
488
0
    if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR)
489
0
        err = MZ_FORMAT_ERROR;
490
0
    if (err == MZ_OK)
491
0
        err = mz_stream_read_uint32(stream, &value32);
492
0
    if (err == MZ_OK && crc32)
493
0
        *crc32 = value32;
494
0
    if (err == MZ_OK) {
495
        /* If zip 64 extension is enabled then read as 8 byte */
496
0
        if (!zip64) {
497
0
            err = mz_stream_read_uint32(stream, &value32);
498
0
            value64 = value32;
499
0
        } else {
500
0
            err = mz_stream_read_int64(stream, &value64);
501
0
            if (value64 < 0)
502
0
                err = MZ_FORMAT_ERROR;
503
0
        }
504
0
        if (err == MZ_OK && compressed_size)
505
0
            *compressed_size = value64;
506
0
    }
507
0
    if (err == MZ_OK) {
508
0
        if (!zip64) {
509
0
            err = mz_stream_read_uint32(stream, &value32);
510
0
            value64 = value32;
511
0
        } else {
512
0
            err = mz_stream_read_int64(stream, &value64);
513
0
            if (value64 < 0)
514
0
                err = MZ_FORMAT_ERROR;
515
0
        }
516
0
        if (err == MZ_OK && uncompressed_size)
517
0
            *uncompressed_size = value64;
518
0
    }
519
520
0
    return err;
521
0
}
522
523
696
static int32_t mz_zip_entry_write_crc_sizes(void *stream, uint8_t zip64, uint8_t mask, mz_zip_file *file_info) {
524
696
    int32_t err = MZ_OK;
525
526
696
    if (mask)
527
98
        err = mz_stream_write_uint32(stream, 0);
528
598
    else
529
598
        err = mz_stream_write_uint32(stream, file_info->crc); /* crc */
530
531
    /* For backwards-compatibility with older zip applications we set all sizes to UINT32_MAX
532
     * when zip64 is needed, instead of only setting sizes larger than UINT32_MAX. */
533
534
696
    if (err == MZ_OK) {
535
696
        if (zip64) /* compr size */
536
350
            err = mz_stream_write_uint32(stream, UINT32_MAX);
537
346
        else
538
346
            err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
539
696
    }
540
696
    if (err == MZ_OK) {
541
696
        if (mask) /* uncompr size */
542
98
            err = mz_stream_write_uint32(stream, 0);
543
598
        else if (zip64)
544
252
            err = mz_stream_write_uint32(stream, UINT32_MAX);
545
346
        else
546
346
            err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
547
696
    }
548
696
    return err;
549
696
}
550
551
1.04k
static int32_t mz_zip_entry_needs_zip64(mz_zip_file *file_info, uint8_t local, uint8_t *zip64) {
552
1.04k
    uint32_t max_uncompressed_size = UINT32_MAX;
553
1.04k
    uint8_t needs_zip64 = 0;
554
555
1.04k
    if (!zip64)
556
0
        return MZ_PARAM_ERROR;
557
558
1.04k
    *zip64 = 0;
559
560
1.04k
    if (local) {
561
        /* At local header we might not know yet whether compressed size will overflow unsigned
562
           32-bit integer which might happen for high entropy data so we give it some cushion */
563
564
696
        max_uncompressed_size -= MZ_ZIP_UNCOMPR_SIZE64_CUSHION;
565
696
    }
566
567
1.04k
    needs_zip64 = (file_info->uncompressed_size >= max_uncompressed_size) || (file_info->compressed_size >= UINT32_MAX);
568
569
1.04k
    if (!local) {
570
        /* Disk offset and number only used in central directory header */
571
348
        needs_zip64 |= (file_info->disk_offset >= UINT32_MAX) || (file_info->disk_number >= UINT16_MAX);
572
348
    }
573
574
1.04k
    if (file_info->zip64 == MZ_ZIP64_AUTO) {
575
        /* If uncompressed size is unknown, assume zip64 for 64-bit data descriptors */
576
798
        if (local && file_info->uncompressed_size == 0) {
577
            /* Don't use zip64 for local header directory entries */
578
532
            if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) != MZ_OK) {
579
532
                *zip64 = 1;
580
532
            }
581
532
        }
582
798
        *zip64 |= needs_zip64;
583
798
    } else if (file_info->zip64 == MZ_ZIP64_FORCE) {
584
126
        *zip64 = 1;
585
126
    } else if (file_info->zip64 == MZ_ZIP64_DISABLE) {
586
        /* Zip64 extension is required to zip file */
587
3
        if (needs_zip64)
588
0
            return MZ_PARAM_ERROR;
589
3
    }
590
591
1.04k
    return MZ_OK;
592
1.04k
}
593
594
696
static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info) {
595
696
    uint64_t ntfs_time = 0;
596
696
    uint32_t reserved = 0;
597
696
    uint32_t dos_date = 0;
598
696
    uint16_t extrafield_size = 0;
599
696
    uint16_t field_type = 0;
600
696
    uint16_t field_length = 0;
601
696
    uint16_t field_length_zip64 = 0;
602
696
    uint16_t field_length_ntfs = 0;
603
696
    uint16_t field_length_aes = 0;
604
696
    uint16_t field_length_unix1 = 0;
605
696
    uint16_t filename_size = 0;
606
696
    uint16_t filename_length = 0;
607
696
    uint16_t linkname_size = 0;
608
696
    uint16_t version_needed = 0;
609
696
    int32_t comment_size = 0;
610
696
    int32_t err = MZ_OK;
611
696
    int32_t err_mem = MZ_OK;
612
696
    uint8_t zip64 = 0;
613
696
    uint8_t skip_aes = 0;
614
696
    uint8_t mask = 0;
615
696
    uint8_t write_end_slash = 0;
616
696
    const char *filename = NULL;
617
696
    char masked_name[64];
618
696
    void *file_extra_stream = NULL;
619
620
696
    if (!file_info)
621
0
        return MZ_PARAM_ERROR;
622
623
696
    if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO))
624
98
        mask = 1;
625
626
    /* Determine if zip64 extra field is necessary */
627
696
    err = mz_zip_entry_needs_zip64(file_info, local, &zip64);
628
696
    if (err != MZ_OK)
629
0
        return err;
630
631
    /* Start calculating extra field sizes */
632
696
    if (zip64) {
633
        /* Both compressed and uncompressed sizes must be included (at least in local header) */
634
350
        field_length_zip64 = 8 + 8;
635
350
        if ((!local) && (file_info->disk_offset >= UINT32_MAX))
636
0
            field_length_zip64 += 8;
637
638
350
        extrafield_size += 4;
639
350
        extrafield_size += field_length_zip64;
640
350
    }
641
642
    /* Calculate extra field size and check for duplicates */
643
696
    if (file_info->extrafield_size > 0) {
644
0
        file_extra_stream = mz_stream_mem_create();
645
0
        if (!file_extra_stream)
646
0
            return MZ_MEM_ERROR;
647
0
        mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield, file_info->extrafield_size);
648
649
0
        do {
650
0
            err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
651
0
            if (err_mem == MZ_OK)
652
0
                err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
653
0
            if (err_mem != MZ_OK)
654
0
                break;
655
656
            /* Prefer incoming aes extensions over ours */
657
0
            if (field_type == MZ_ZIP_EXTENSION_AES)
658
0
                skip_aes = 1;
659
660
            /* Prefer our zip64, ntfs, unix1 extension over incoming */
661
0
            if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS &&
662
0
                field_type != MZ_ZIP_EXTENSION_UNIX1)
663
0
                extrafield_size += 4 + field_length;
664
665
0
            if (err_mem == MZ_OK)
666
0
                err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
667
0
        } while (err_mem == MZ_OK);
668
0
    }
669
670
696
#ifdef HAVE_WZAES
671
696
    if (!skip_aes) {
672
696
        if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version)) {
673
0
            field_length_aes = 1 + 1 + 1 + 2 + 2;
674
0
            extrafield_size += 4 + field_length_aes;
675
0
        }
676
696
    }
677
#else
678
    MZ_UNUSED(field_length_aes);
679
    MZ_UNUSED(skip_aes);
680
#endif
681
    /* NTFS timestamps */
682
696
    if ((file_info->modified_date != 0) && (file_info->accessed_date != 0) && (file_info->creation_date != 0) &&
683
696
        (!mask)) {
684
0
        field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
685
0
        extrafield_size += 4 + field_length_ntfs;
686
0
    }
687
688
    /* Unix1 symbolic links */
689
696
    if (file_info->linkname && *file_info->linkname != 0) {
690
0
        linkname_size = (uint16_t)strlen(file_info->linkname);
691
0
        field_length_unix1 = 12 + linkname_size;
692
0
        extrafield_size += 4 + field_length_unix1;
693
0
    }
694
695
696
    if (local)
696
348
        err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
697
348
    else {
698
348
        err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
699
348
        if (err == MZ_OK)
700
348
            err = mz_stream_write_uint16(stream, file_info->version_madeby);
701
348
    }
702
703
    /* Calculate version needed to extract */
704
696
    if (err == MZ_OK) {
705
696
        version_needed = file_info->version_needed;
706
696
        if (version_needed == 0) {
707
696
            version_needed = 20;
708
696
            if (zip64)
709
350
                version_needed = 45;
710
#ifdef HAVE_BZIP2
711
            if (file_info->compression_method == MZ_COMPRESS_METHOD_BZIP2)
712
                version_needed = 46;
713
#endif
714
696
#ifdef HAVE_WZAES
715
696
            if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
716
0
                version_needed = 51;
717
696
#endif
718
#if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP)
719
            if ((file_info->compression_method == MZ_COMPRESS_METHOD_LZMA) ||
720
                (file_info->compression_method == MZ_COMPRESS_METHOD_XZ))
721
                version_needed = 63;
722
#endif
723
696
        }
724
696
        err = mz_stream_write_uint16(stream, version_needed);
725
696
    }
726
696
    if (err == MZ_OK)
727
696
        err = mz_stream_write_uint16(stream, file_info->flag);
728
696
    if (err == MZ_OK) {
729
696
#ifdef HAVE_WZAES
730
696
        if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
731
0
            err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
732
696
        else
733
696
#endif
734
696
            err = mz_stream_write_uint16(stream, file_info->compression_method);
735
696
    }
736
696
    if (err == MZ_OK) {
737
696
        if (file_info->modified_date != 0 && !mask)
738
0
            dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
739
696
        err = mz_stream_write_uint32(stream, dos_date);
740
696
    }
741
742
696
    if (err == MZ_OK)
743
696
        err = mz_zip_entry_write_crc_sizes(stream, zip64, mask, file_info);
744
745
696
    if (mask) {
746
98
        snprintf(masked_name, sizeof(masked_name), "%" PRIx32 "_%" PRIx64, file_info->disk_number,
747
98
                 file_info->disk_offset);
748
98
        filename = masked_name;
749
598
    } else {
750
598
        filename = file_info->filename;
751
598
    }
752
753
696
    filename_length = (uint16_t)strlen(filename);
754
696
    filename_size += filename_length;
755
756
696
    if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) &&
757
696
        ((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\'))) {
758
0
        filename_size += 1;
759
0
        write_end_slash = 1;
760
0
    }
761
762
696
    if (err == MZ_OK)
763
696
        err = mz_stream_write_uint16(stream, filename_size);
764
696
    if (err == MZ_OK)
765
696
        err = mz_stream_write_uint16(stream, extrafield_size);
766
767
696
    if (!local) {
768
348
        if (file_info->comment) {
769
348
            comment_size = (int32_t)strlen(file_info->comment);
770
348
            if (comment_size > UINT16_MAX)
771
0
                comment_size = UINT16_MAX;
772
348
        }
773
348
        if (err == MZ_OK)
774
348
            err = mz_stream_write_uint16(stream, (uint16_t)comment_size);
775
348
        if (err == MZ_OK)
776
348
            err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
777
348
        if (err == MZ_OK)
778
348
            err = mz_stream_write_uint16(stream, file_info->internal_fa);
779
348
        if (err == MZ_OK)
780
348
            err = mz_stream_write_uint32(stream, file_info->external_fa);
781
348
        if (err == MZ_OK) {
782
348
            if (file_info->disk_offset >= UINT32_MAX)
783
0
                err = mz_stream_write_uint32(stream, UINT32_MAX);
784
348
            else
785
348
                err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
786
348
        }
787
348
    }
788
789
696
    if (err == MZ_OK) {
790
696
        const char *next = filename;
791
696
        int32_t left = filename_length;
792
#if defined(_WIN32)
793
        const char *backslash = NULL;
794
795
        /* Ensure all slashes are written as forward slashes according to 4.4.17.1 */
796
        while ((err == MZ_OK) && (backslash = strchr(next, '\\'))) {
797
            int32_t part_length = (int32_t)(backslash - next);
798
799
            if (mz_stream_write(stream, next, part_length) != part_length || mz_stream_write(stream, "/", 1) != 1)
800
                err = MZ_WRITE_ERROR;
801
802
            left -= part_length + 1;
803
            next = backslash + 1;
804
        }
805
#endif
806
696
        if (err == MZ_OK && left > 0) {
807
696
            if (mz_stream_write(stream, next, left) != left)
808
0
                err = MZ_WRITE_ERROR;
809
696
        }
810
811
        /* Ensure that directories have a slash appended to them for compatibility */
812
696
        if (err == MZ_OK && write_end_slash)
813
0
            err = mz_stream_write_uint8(stream, '/');
814
696
    }
815
816
    /* Write ZIP64 extra field first so we can update sizes later if data descriptor not used */
817
696
    if ((err == MZ_OK) && (zip64)) {
818
350
        err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64);
819
350
        if (err == MZ_OK) {
820
350
            if (mask)
821
98
                err = mz_stream_write_int64(stream, 0);
822
252
            else
823
252
                err = mz_stream_write_int64(stream, file_info->uncompressed_size);
824
350
        }
825
350
        if (err == MZ_OK)
826
350
            err = mz_stream_write_int64(stream, file_info->compressed_size);
827
350
        if ((err == MZ_OK) && (!local) && (file_info->disk_offset >= UINT32_MAX))
828
0
            err = mz_stream_write_int64(stream, file_info->disk_offset);
829
350
        if ((err == MZ_OK) && (!local) && (file_info->disk_number >= UINT16_MAX))
830
0
            err = mz_stream_write_uint32(stream, file_info->disk_number);
831
350
    }
832
    /* Write NTFS extra field */
833
696
    if ((err == MZ_OK) && (field_length_ntfs > 0)) {
834
0
        err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs);
835
0
        if (err == MZ_OK)
836
0
            err = mz_stream_write_uint32(stream, reserved);
837
0
        if (err == MZ_OK)
838
0
            err = mz_stream_write_uint16(stream, 0x01);
839
0
        if (err == MZ_OK)
840
0
            err = mz_stream_write_uint16(stream, field_length_ntfs - 8);
841
0
        if (err == MZ_OK) {
842
0
            mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
843
0
            err = mz_stream_write_uint64(stream, ntfs_time);
844
0
        }
845
0
        if (err == MZ_OK) {
846
0
            mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
847
0
            err = mz_stream_write_uint64(stream, ntfs_time);
848
0
        }
849
0
        if (err == MZ_OK) {
850
0
            mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
851
0
            err = mz_stream_write_uint64(stream, ntfs_time);
852
0
        }
853
0
    }
854
    /* Write UNIX extra block extra field */
855
696
    if ((err == MZ_OK) && (field_length_unix1 > 0)) {
856
0
        err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_UNIX1, field_length_unix1);
857
0
        if (err == MZ_OK)
858
0
            err = mz_stream_write_uint32(stream, (uint32_t)file_info->accessed_date);
859
0
        if (err == MZ_OK)
860
0
            err = mz_stream_write_uint32(stream, (uint32_t)file_info->modified_date);
861
0
        if (err == MZ_OK) /* User id */
862
0
            err = mz_stream_write_uint16(stream, 0);
863
0
        if (err == MZ_OK) /* Group id */
864
0
            err = mz_stream_write_uint16(stream, 0);
865
0
        if (err == MZ_OK && linkname_size > 0) {
866
0
            if (mz_stream_write(stream, file_info->linkname, linkname_size) != linkname_size)
867
0
                err = MZ_WRITE_ERROR;
868
0
        }
869
0
    }
870
696
#ifdef HAVE_WZAES
871
    /* Write AES extra field */
872
696
    if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version)) {
873
0
        err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes);
874
0
        if (err == MZ_OK)
875
0
            err = mz_stream_write_uint16(stream, file_info->aes_version);
876
0
        if (err == MZ_OK)
877
0
            err = mz_stream_write_uint8(stream, 'A');
878
0
        if (err == MZ_OK)
879
0
            err = mz_stream_write_uint8(stream, 'E');
880
0
        if (err == MZ_OK)
881
0
            err = mz_stream_write_uint8(stream, file_info->aes_strength);
882
0
        if (err == MZ_OK)
883
0
            err = mz_stream_write_uint16(stream, file_info->compression_method);
884
0
    }
885
696
#endif
886
887
696
    if (file_info->extrafield_size > 0) {
888
0
        err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET);
889
0
        while (err == MZ_OK && err_mem == MZ_OK) {
890
0
            err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
891
0
            if (err_mem == MZ_OK)
892
0
                err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
893
0
            if (err_mem != MZ_OK)
894
0
                break;
895
896
            /* Prefer our zip 64, ntfs, unix1 extensions over incoming */
897
0
            if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS ||
898
0
                field_type == MZ_ZIP_EXTENSION_UNIX1) {
899
0
                err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
900
0
                continue;
901
0
            }
902
903
0
            err = mz_stream_write_uint16(stream, field_type);
904
0
            if (err == MZ_OK)
905
0
                err = mz_stream_write_uint16(stream, field_length);
906
0
            if (err == MZ_OK)
907
0
                err = mz_stream_copy(stream, file_extra_stream, field_length);
908
0
        }
909
910
0
        mz_stream_mem_delete(&file_extra_stream);
911
0
    }
912
913
696
    if (err == MZ_OK && !local && file_info->comment) {
914
348
        if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size)
915
0
            err = MZ_WRITE_ERROR;
916
348
    }
917
918
696
    return err;
919
696
}
920
921
static int32_t mz_zip_entry_write_descriptor(void *stream, uint8_t zip64, uint32_t crc32, int64_t compressed_size,
922
348
                                             int64_t uncompressed_size) {
923
348
    int32_t err = MZ_OK;
924
925
348
    err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
926
348
    if (err == MZ_OK)
927
348
        err = mz_stream_write_uint32(stream, crc32);
928
929
    /* Store data descriptor as 8 bytes if zip 64 extension enabled */
930
348
    if (err == MZ_OK) {
931
        /* Zip 64 extension is enabled when uncompressed size is > UINT32_MAX */
932
348
        if (!zip64)
933
40
            err = mz_stream_write_uint32(stream, (uint32_t)compressed_size);
934
308
        else
935
308
            err = mz_stream_write_int64(stream, compressed_size);
936
348
    }
937
348
    if (err == MZ_OK) {
938
348
        if (!zip64)
939
40
            err = mz_stream_write_uint32(stream, (uint32_t)uncompressed_size);
940
308
        else
941
308
            err = mz_stream_write_int64(stream, uncompressed_size);
942
348
    }
943
944
348
    return err;
945
348
}
946
947
0
static int32_t mz_zip_read_cd(void *handle) {
948
0
    mz_zip *zip = (mz_zip *)handle;
949
0
    uint64_t number_entry_cd64 = 0;
950
0
    uint64_t number_entry_cd = 0;
951
0
    int64_t eocd_pos = 0;
952
0
    int64_t eocd_pos64 = 0;
953
0
    int64_t value64i = 0;
954
0
    uint16_t value16 = 0;
955
0
    uint32_t value32 = 0;
956
0
    uint64_t value64 = 0;
957
0
    uint16_t comment_size = 0;
958
0
    int32_t comment_read = 0;
959
0
    int32_t err = MZ_OK;
960
961
0
    if (!zip)
962
0
        return MZ_PARAM_ERROR;
963
964
    /* Read and cache central directory records */
965
0
    err = mz_zip_search_eocd(zip->stream, &eocd_pos);
966
0
    if (err == MZ_OK) {
967
        /* The signature, already checked */
968
0
        err = mz_stream_read_uint32(zip->stream, &value32);
969
        /* Number of this disk */
970
0
        if (err == MZ_OK)
971
0
            err = mz_stream_read_uint16(zip->stream, &value16);
972
        /* Number of the disk with the start of the central directory */
973
0
        if (err == MZ_OK)
974
0
            err = mz_stream_read_uint16(zip->stream, &value16);
975
0
        zip->disk_number_with_cd = value16;
976
        /* Total number of entries in the central dir on this disk */
977
0
        if (err == MZ_OK)
978
0
            err = mz_stream_read_uint16(zip->stream, &value16);
979
0
        zip->number_entry = value16;
980
        /* Total number of entries in the central dir */
981
0
        if (err == MZ_OK)
982
0
            err = mz_stream_read_uint16(zip->stream, &value16);
983
0
        number_entry_cd = value16;
984
        /* When recover is enabled, we can ignore incorrect number of entries */
985
0
        if (number_entry_cd != zip->number_entry && !zip->recover)
986
0
            err = MZ_FORMAT_ERROR;
987
        /* Size of the central directory */
988
0
        if (err == MZ_OK)
989
0
            err = mz_stream_read_uint32(zip->stream, &value32);
990
0
        if (err == MZ_OK)
991
0
            zip->cd_size = value32;
992
        /* Offset of start of central directory with respect to the starting disk number */
993
0
        if (err == MZ_OK)
994
0
            err = mz_stream_read_uint32(zip->stream, &value32);
995
0
        if (err == MZ_OK)
996
0
            zip->cd_offset = value32;
997
        /* Zip file global comment length */
998
0
        if (err == MZ_OK)
999
0
            err = mz_stream_read_uint16(zip->stream, &comment_size);
1000
0
        if ((err == MZ_OK) && (comment_size > 0)) {
1001
0
            zip->comment = (char *)malloc(comment_size + 1);
1002
0
            if (zip->comment) {
1003
0
                comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
1004
                /* Don't fail if incorrect comment length read, not critical */
1005
0
                if (comment_read < 0)
1006
0
                    comment_read = 0;
1007
0
                zip->comment[comment_read] = 0;
1008
0
            }
1009
0
        }
1010
1011
0
        if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))) {
1012
            /* Format should be Zip64, as the central directory or file size is too large */
1013
0
            if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK) {
1014
0
                eocd_pos = eocd_pos64;
1015
1016
0
                err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
1017
                /* The signature, already checked */
1018
0
                if (err == MZ_OK)
1019
0
                    err = mz_stream_read_uint32(zip->stream, &value32);
1020
                /* Size of zip64 end of central directory record */
1021
0
                if (err == MZ_OK)
1022
0
                    err = mz_stream_read_uint64(zip->stream, &value64);
1023
                /* Version made by */
1024
0
                if (err == MZ_OK)
1025
0
                    err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
1026
                /* Version needed to extract */
1027
0
                if (err == MZ_OK)
1028
0
                    err = mz_stream_read_uint16(zip->stream, &value16);
1029
                /* Number of this disk */
1030
0
                if (err == MZ_OK)
1031
0
                    err = mz_stream_read_uint32(zip->stream, &value32);
1032
                /* Number of the disk with the start of the central directory */
1033
0
                if (err == MZ_OK)
1034
0
                    err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
1035
                /* Total number of entries in the central directory on this disk */
1036
0
                if (err == MZ_OK)
1037
0
                    err = mz_stream_read_uint64(zip->stream, &zip->number_entry);
1038
                /* Total number of entries in the central directory */
1039
0
                if (err == MZ_OK)
1040
0
                    err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
1041
0
                if (zip->number_entry != number_entry_cd64)
1042
0
                    err = MZ_FORMAT_ERROR;
1043
                /* Size of the central directory */
1044
0
                if (err == MZ_OK) {
1045
0
                    err = mz_stream_read_int64(zip->stream, &zip->cd_size);
1046
0
                    if (zip->cd_size < 0)
1047
0
                        err = MZ_FORMAT_ERROR;
1048
0
                }
1049
                /* Offset of start of central directory with respect to the starting disk number */
1050
0
                if (err == MZ_OK) {
1051
0
                    err = mz_stream_read_int64(zip->stream, &zip->cd_offset);
1052
0
                    if (zip->cd_offset < 0)
1053
0
                        err = MZ_FORMAT_ERROR;
1054
0
                }
1055
0
            } else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
1056
0
                       (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)) {
1057
0
                err = MZ_FORMAT_ERROR;
1058
0
            }
1059
0
        }
1060
0
    }
1061
1062
0
    if (err == MZ_OK) {
1063
0
        mz_zip_print("Zip - Read cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
1064
0
                     zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1065
1066
        /* Verify central directory signature exists at offset */
1067
0
        err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1068
0
        if (err == MZ_OK)
1069
0
            err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
1070
0
        if ((err == MZ_OK) && (zip->cd_signature != MZ_ZIP_MAGIC_CENTRALHEADER)) {
1071
            /* If cd exists in large file and no zip-64 support, error for recover */
1072
0
            if (eocd_pos > UINT32_MAX && eocd_pos64 == 0)
1073
0
                err = MZ_FORMAT_ERROR;
1074
            /* If cd not found attempt to seek backward to find it */
1075
0
            if (err == MZ_OK)
1076
0
                err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
1077
0
            if (err == MZ_OK)
1078
0
                err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
1079
0
            if ((err == MZ_OK) && (zip->cd_signature == MZ_ZIP_MAGIC_CENTRALHEADER)) {
1080
                /* If found compensate for incorrect locations */
1081
0
                value64i = zip->cd_offset;
1082
0
                zip->cd_offset = eocd_pos - zip->cd_size;
1083
                /* Assume disk has prepended data */
1084
0
                zip->disk_offset_shift = zip->cd_offset - value64i;
1085
0
            }
1086
0
        }
1087
0
    }
1088
1089
0
    if (err == MZ_OK) {
1090
0
        if (eocd_pos < zip->cd_offset) {
1091
            /* End of central dir should always come after central dir */
1092
0
            err = MZ_FORMAT_ERROR;
1093
0
        } else if ((uint64_t)eocd_pos < (uint64_t)zip->cd_offset + zip->cd_size) {
1094
            /* Truncate size of cd if incorrect size or offset provided */
1095
0
            zip->cd_size = eocd_pos - zip->cd_offset;
1096
0
        }
1097
0
    }
1098
1099
0
    return err;
1100
0
}
1101
1102
437
static int32_t mz_zip_write_cd(void *handle) {
1103
437
    mz_zip *zip = (mz_zip *)handle;
1104
437
    int64_t zip64_eocd_pos_inzip = 0;
1105
437
    int64_t disk_number = 0;
1106
437
    int64_t disk_size = 0;
1107
437
    int32_t comment_size = 0;
1108
437
    int32_t err = MZ_OK;
1109
1110
437
    if (!zip)
1111
0
        return MZ_PARAM_ERROR;
1112
1113
437
    if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
1114
0
        zip->disk_number_with_cd = (uint32_t)disk_number;
1115
437
    if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
1116
0
        zip->disk_number_with_cd += 1;
1117
437
    mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1118
437
    if ((zip->disk_number_with_cd > 0) && (zip->open_mode & MZ_OPEN_MODE_APPEND)) {
1119
        /* Overwrite existing central directory if using split disks */
1120
0
        mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1121
0
    }
1122
1123
437
    zip->cd_offset = mz_stream_tell(zip->stream);
1124
437
    mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
1125
437
    zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
1126
437
    mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
1127
1128
437
    err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
1129
1130
437
    mz_zip_print("Zip - Write cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
1131
437
                 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1132
1133
437
    if (zip->cd_size == 0 && zip->number_entry > 0) {
1134
        /* Zip does not contain central directory, open with recovery option */
1135
0
        return MZ_FORMAT_ERROR;
1136
0
    }
1137
1138
    /* Write the ZIP64 central directory header */
1139
437
    if (zip->cd_offset >= UINT32_MAX || zip->number_entry >= UINT16_MAX) {
1140
0
        zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
1141
1142
0
        err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
1143
1144
        /* Size of this 'zip64 end of central directory' */
1145
0
        if (err == MZ_OK)
1146
0
            err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
1147
        /* Version made by */
1148
0
        if (err == MZ_OK)
1149
0
            err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
1150
        /* Version needed */
1151
0
        if (err == MZ_OK)
1152
0
            err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
1153
        /* Number of this disk */
1154
0
        if (err == MZ_OK)
1155
0
            err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
1156
        /* Number of the disk with the start of the central directory */
1157
0
        if (err == MZ_OK)
1158
0
            err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
1159
        /* Total number of entries in the central dir on this disk */
1160
0
        if (err == MZ_OK)
1161
0
            err = mz_stream_write_uint64(zip->stream, zip->number_entry);
1162
        /* Total number of entries in the central dir */
1163
0
        if (err == MZ_OK)
1164
0
            err = mz_stream_write_uint64(zip->stream, zip->number_entry);
1165
        /* Size of the central directory */
1166
0
        if (err == MZ_OK)
1167
0
            err = mz_stream_write_int64(zip->stream, zip->cd_size);
1168
        /* Offset of start of central directory with respect to the starting disk number */
1169
0
        if (err == MZ_OK)
1170
0
            err = mz_stream_write_int64(zip->stream, zip->cd_offset);
1171
0
        if (err == MZ_OK)
1172
0
            err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
1173
1174
        /* Number of the disk with the start of the central directory */
1175
0
        if (err == MZ_OK)
1176
0
            err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
1177
        /* Relative offset to the end of zip64 central directory */
1178
0
        if (err == MZ_OK)
1179
0
            err = mz_stream_write_int64(zip->stream, zip64_eocd_pos_inzip);
1180
        /* Number of the disk with the start of the central directory */
1181
0
        if (err == MZ_OK)
1182
0
            err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
1183
0
    }
1184
1185
    /* Write the central directory header */
1186
1187
    /* Signature */
1188
437
    if (err == MZ_OK)
1189
437
        err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
1190
    /* Number of this disk */
1191
437
    if (err == MZ_OK)
1192
437
        err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
1193
    /* Number of the disk with the start of the central directory */
1194
437
    if (err == MZ_OK)
1195
437
        err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
1196
    /* Total number of entries in the central dir on this disk */
1197
437
    if (err == MZ_OK) {
1198
437
        if (zip->number_entry >= UINT16_MAX)
1199
0
            err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1200
437
        else
1201
437
            err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1202
437
    }
1203
    /* Total number of entries in the central dir */
1204
437
    if (err == MZ_OK) {
1205
437
        if (zip->number_entry >= UINT16_MAX)
1206
0
            err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1207
437
        else
1208
437
            err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1209
437
    }
1210
    /* Size of the central directory */
1211
437
    if (err == MZ_OK)
1212
437
        err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
1213
    /* Offset of start of central directory with respect to the starting disk number */
1214
437
    if (err == MZ_OK) {
1215
437
        if (zip->cd_offset >= UINT32_MAX)
1216
0
            err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
1217
437
        else
1218
437
            err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
1219
437
    }
1220
1221
    /* Write global comment */
1222
437
    if (zip->comment) {
1223
0
        comment_size = (int32_t)strlen(zip->comment);
1224
0
        if (comment_size > UINT16_MAX)
1225
0
            comment_size = UINT16_MAX;
1226
0
    }
1227
437
    if (err == MZ_OK)
1228
437
        err = mz_stream_write_uint16(zip->stream, (uint16_t)comment_size);
1229
437
    if (err == MZ_OK) {
1230
437
        if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
1231
0
            err = MZ_READ_ERROR;
1232
437
    }
1233
437
    return err;
1234
437
}
1235
1236
0
static int32_t mz_zip_recover_cd(void *handle) {
1237
0
    mz_zip *zip = (mz_zip *)handle;
1238
0
    mz_zip_file local_file_info;
1239
0
    void *local_file_info_stream = NULL;
1240
0
    void *cd_mem_stream = NULL;
1241
0
    uint64_t number_entry = 0;
1242
0
    int64_t descriptor_pos = 0;
1243
0
    int64_t next_header_pos = 0;
1244
0
    int64_t disk_offset = 0;
1245
0
    int64_t disk_number = 0;
1246
0
    int64_t compressed_pos = 0;
1247
0
    int64_t compressed_end_pos = 0;
1248
0
    int64_t compressed_size = 0;
1249
0
    int64_t uncompressed_size = 0;
1250
0
    uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8;
1251
0
    uint8_t local_header_magic[4] = MZ_ZIP_MAGIC_LOCALHEADERU8;
1252
0
    uint8_t central_header_magic[4] = MZ_ZIP_MAGIC_CENTRALHEADERU8;
1253
0
    uint32_t crc32 = 0;
1254
0
    int32_t disk_number_with_cd = 0;
1255
0
    int32_t err = MZ_OK;
1256
0
    uint8_t zip64 = 0;
1257
0
    uint8_t eof = 0;
1258
1259
0
    mz_zip_print("Zip - Recover - Start\n");
1260
1261
0
    err = mz_zip_get_cd_mem_stream(zip, &cd_mem_stream);
1262
0
    if (err != MZ_OK)
1263
0
        return err;
1264
1265
    /* Determine if we are on a split disk or not */
1266
0
    mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0);
1267
0
    if (mz_stream_tell(zip->stream) < 0) {
1268
0
        mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1269
0
        mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1270
0
    } else
1271
0
        disk_number_with_cd = 1;
1272
1273
0
    local_file_info_stream = mz_stream_mem_create();
1274
0
    if (!local_file_info_stream)
1275
0
        return MZ_MEM_ERROR;
1276
1277
0
    if (mz_stream_is_open(cd_mem_stream) != MZ_OK)
1278
0
        err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1279
1280
0
    mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1281
1282
0
    if (err == MZ_OK) {
1283
0
        err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic), INT64_MAX,
1284
0
                             &next_header_pos);
1285
0
    }
1286
1287
0
    while (err == MZ_OK && !eof) {
1288
        /* Get current offset and disk number for central dir record */
1289
0
        disk_offset = mz_stream_tell(zip->stream);
1290
0
        mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1291
1292
        /* Read local headers */
1293
0
        memset(&local_file_info, 0, sizeof(local_file_info));
1294
0
        err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream);
1295
0
        if (err != MZ_OK)
1296
0
            break;
1297
1298
0
        local_file_info.disk_offset = disk_offset;
1299
0
        if (disk_number < 0)
1300
0
            disk_number = 0;
1301
0
        local_file_info.disk_number = (uint32_t)disk_number;
1302
1303
0
        compressed_pos = mz_stream_tell(zip->stream);
1304
1305
0
        if ((err == MZ_OK) && (local_file_info.compressed_size > 0)) {
1306
0
            mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR);
1307
0
        }
1308
1309
0
        for (;;) {
1310
            /* Search for the next local header */
1311
0
            err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic), INT64_MAX,
1312
0
                                 &next_header_pos);
1313
1314
0
            if (err == MZ_EXIST_ERROR) {
1315
0
                mz_stream_seek(zip->stream, compressed_pos, MZ_SEEK_SET);
1316
1317
                /* Search for central dir if no local header found */
1318
0
                err = mz_stream_find(zip->stream, (const void *)central_header_magic, sizeof(central_header_magic),
1319
0
                                     INT64_MAX, &next_header_pos);
1320
1321
0
                if (err == MZ_EXIST_ERROR) {
1322
                    /* Get end of stream if no central header found */
1323
0
                    mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1324
0
                    next_header_pos = mz_stream_tell(zip->stream);
1325
0
                }
1326
1327
0
                eof = 1;
1328
0
            }
1329
1330
0
            if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR || local_file_info.compressed_size == 0) {
1331
                /* Search backwards for the descriptor, seeking too far back will be incorrect if compressed size is
1332
                 * small */
1333
0
                err = mz_stream_find_reverse(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
1334
0
                                             MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR, &descriptor_pos);
1335
0
                if (err == MZ_OK) {
1336
0
                    if (mz_zip_extrafield_contains(local_file_info.extrafield, local_file_info.extrafield_size,
1337
0
                                                   MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
1338
0
                        zip64 = 1;
1339
1340
0
                    err =
1341
0
                        mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32, &compressed_size, &uncompressed_size);
1342
1343
0
                    if (err == MZ_OK) {
1344
0
                        if (local_file_info.crc == 0)
1345
0
                            local_file_info.crc = crc32;
1346
0
                        if (local_file_info.compressed_size == 0)
1347
0
                            local_file_info.compressed_size = compressed_size;
1348
0
                        if (local_file_info.uncompressed_size == 0)
1349
0
                            local_file_info.uncompressed_size = uncompressed_size;
1350
0
                    }
1351
1352
0
                    compressed_end_pos = descriptor_pos;
1353
0
                } else if (eof) {
1354
0
                    compressed_end_pos = next_header_pos;
1355
0
                } else if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) {
1356
                    /* Wrong local file entry found, keep searching */
1357
0
                    next_header_pos += 1;
1358
0
                    mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
1359
0
                    continue;
1360
0
                }
1361
0
            } else {
1362
0
                compressed_end_pos = next_header_pos;
1363
0
            }
1364
1365
0
            break;
1366
0
        }
1367
1368
0
        compressed_size = compressed_end_pos - compressed_pos;
1369
1370
0
        if (compressed_size > UINT32_MAX) {
1371
            /* Update sizes if 4GB file is written with no ZIP64 support */
1372
0
            if (local_file_info.uncompressed_size < UINT32_MAX) {
1373
0
                local_file_info.compressed_size = compressed_size;
1374
0
                local_file_info.uncompressed_size = 0;
1375
0
            }
1376
0
        }
1377
1378
0
        mz_zip_print("Zip - Recover - Entry %s (csize %" PRId64 " usize %" PRId64 " flags 0x%" PRIx16 ")\n",
1379
0
                     local_file_info.filename, local_file_info.compressed_size, local_file_info.uncompressed_size,
1380
0
                     local_file_info.flag);
1381
1382
        /* Rewrite central dir with local headers and offsets */
1383
0
        err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
1384
0
        if (err == MZ_OK)
1385
0
            number_entry += 1;
1386
1387
0
        err = mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
1388
0
    }
1389
1390
0
    mz_stream_mem_delete(&local_file_info_stream);
1391
1392
0
    mz_zip_print("Zip - Recover - Complete (cddisk %" PRId32 " entries %" PRId64 ")\n", disk_number_with_cd,
1393
0
                 number_entry);
1394
1395
0
    if (number_entry == 0)
1396
0
        return err;
1397
1398
    /* Set new upper seek boundary for central dir mem stream */
1399
0
    disk_offset = mz_stream_tell(cd_mem_stream);
1400
0
    mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset);
1401
1402
    /* Set new central directory info */
1403
0
    mz_zip_set_cd_stream(zip, 0, cd_mem_stream);
1404
0
    mz_zip_set_number_entry(zip, number_entry);
1405
0
    mz_zip_set_disk_number_with_cd(zip, disk_number_with_cd);
1406
1407
0
    return MZ_OK;
1408
0
}
1409
1410
437
void *mz_zip_create(void) {
1411
437
    mz_zip *zip = (mz_zip *)calloc(1, sizeof(mz_zip));
1412
437
    if (zip)
1413
437
        zip->data_descriptor = 1;
1414
437
    return zip;
1415
437
}
1416
1417
437
void mz_zip_delete(void **handle) {
1418
437
    mz_zip *zip = NULL;
1419
437
    if (!handle)
1420
0
        return;
1421
437
    zip = (mz_zip *)*handle;
1422
437
    free(zip);
1423
437
    *handle = NULL;
1424
437
}
1425
1426
437
int32_t mz_zip_open(void *handle, void *stream, int32_t mode) {
1427
437
    mz_zip *zip = (mz_zip *)handle;
1428
437
    int32_t err = MZ_OK;
1429
1430
437
    if (!zip)
1431
0
        return MZ_PARAM_ERROR;
1432
1433
437
    mz_zip_print("Zip - Open\n");
1434
1435
437
    zip->stream = stream;
1436
437
    zip->cd_mem_stream = mz_stream_mem_create();
1437
437
    if (!zip->cd_mem_stream)
1438
0
        return MZ_MEM_ERROR;
1439
1440
437
    if (mode & MZ_OPEN_MODE_WRITE) {
1441
437
        mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1442
437
        zip->cd_stream = zip->cd_mem_stream;
1443
437
    } else {
1444
0
        zip->cd_stream = stream;
1445
0
    }
1446
1447
437
    if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND)) {
1448
0
        if ((mode & MZ_OPEN_MODE_CREATE) == 0) {
1449
0
            err = mz_zip_read_cd(zip);
1450
0
            if (err != MZ_OK) {
1451
0
                mz_zip_print("Zip - Error detected reading cd (%" PRId32 ")\n", err);
1452
0
                if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK)
1453
0
                    err = MZ_OK;
1454
0
            }
1455
0
        }
1456
1457
0
        if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND)) {
1458
0
            if (zip->cd_size > 0) {
1459
                /* Store central directory in memory */
1460
0
                err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1461
0
                if (err == MZ_OK)
1462
0
                    err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size);
1463
0
                if (err == MZ_OK)
1464
0
                    err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1465
0
            } else {
1466
0
                if (zip->cd_signature == MZ_ZIP_MAGIC_ENDHEADER) {
1467
                    /* If tiny zip then overwrite end header */
1468
0
                    err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1469
0
                } else {
1470
                    /* If no central directory, append new zip to end of file */
1471
0
                    err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1472
0
                }
1473
0
            }
1474
1475
0
            if (zip->disk_number_with_cd > 0) {
1476
                /* Move to last disk to begin appending */
1477
0
                mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->disk_number_with_cd - 1);
1478
0
            }
1479
0
        } else {
1480
0
            zip->cd_start_pos = zip->cd_offset;
1481
0
        }
1482
0
    }
1483
1484
437
    if (err != MZ_OK) {
1485
0
        mz_zip_close(zip);
1486
0
        return err;
1487
0
    }
1488
1489
    /* Memory streams used to store variable length file info data */
1490
437
    zip->file_info_stream = mz_stream_mem_create();
1491
437
    if (!zip->file_info_stream)
1492
0
        return MZ_MEM_ERROR;
1493
1494
437
    mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1495
1496
437
    zip->local_file_info_stream = mz_stream_mem_create();
1497
437
    if (!zip->local_file_info_stream) {
1498
0
        mz_stream_delete(&zip->file_info_stream);
1499
0
        return MZ_MEM_ERROR;
1500
0
    }
1501
1502
437
    mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1503
1504
437
    zip->open_mode = mode;
1505
1506
437
    return err;
1507
437
}
1508
1509
437
int32_t mz_zip_close(void *handle) {
1510
437
    mz_zip *zip = (mz_zip *)handle;
1511
437
    int32_t err = MZ_OK;
1512
1513
437
    if (!zip)
1514
0
        return MZ_PARAM_ERROR;
1515
1516
437
    mz_zip_print("Zip - Close\n");
1517
1518
437
    if (mz_zip_entry_is_open(zip) == MZ_OK)
1519
0
        err = mz_zip_entry_close(zip);
1520
1521
437
    if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE))
1522
437
        err = mz_zip_write_cd(zip);
1523
1524
437
    if (zip->cd_mem_stream) {
1525
437
        mz_stream_close(zip->cd_mem_stream);
1526
437
        mz_stream_delete(&zip->cd_mem_stream);
1527
437
    }
1528
1529
437
    if (zip->file_info_stream) {
1530
437
        mz_stream_mem_close(zip->file_info_stream);
1531
437
        mz_stream_mem_delete(&zip->file_info_stream);
1532
437
    }
1533
437
    if (zip->local_file_info_stream) {
1534
437
        mz_stream_mem_close(zip->local_file_info_stream);
1535
437
        mz_stream_mem_delete(&zip->local_file_info_stream);
1536
437
    }
1537
1538
437
    if (zip->comment) {
1539
0
        free(zip->comment);
1540
0
        zip->comment = NULL;
1541
0
    }
1542
1543
437
    zip->stream = NULL;
1544
437
    zip->cd_stream = NULL;
1545
1546
437
    return err;
1547
437
}
1548
1549
0
int32_t mz_zip_get_comment(void *handle, const char **comment) {
1550
0
    mz_zip *zip = (mz_zip *)handle;
1551
0
    if (!zip || !comment)
1552
0
        return MZ_PARAM_ERROR;
1553
0
    if (!zip->comment)
1554
0
        return MZ_EXIST_ERROR;
1555
0
    *comment = zip->comment;
1556
0
    return MZ_OK;
1557
0
}
1558
1559
0
int32_t mz_zip_set_comment(void *handle, const char *comment) {
1560
0
    mz_zip *zip = (mz_zip *)handle;
1561
0
    int32_t comment_size = 0;
1562
0
    if (!zip || !comment)
1563
0
        return MZ_PARAM_ERROR;
1564
0
    free(zip->comment);
1565
0
    comment_size = (int32_t)strlen(comment);
1566
0
    if (comment_size > UINT16_MAX)
1567
0
        return MZ_PARAM_ERROR;
1568
0
    zip->comment = (char *)calloc(comment_size + 1, sizeof(char));
1569
0
    if (!zip->comment)
1570
0
        return MZ_MEM_ERROR;
1571
0
    strncpy(zip->comment, comment, comment_size);
1572
0
    return MZ_OK;
1573
0
}
1574
1575
0
int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby) {
1576
0
    mz_zip *zip = (mz_zip *)handle;
1577
0
    if (!zip || !version_madeby)
1578
0
        return MZ_PARAM_ERROR;
1579
0
    *version_madeby = zip->version_madeby;
1580
0
    return MZ_OK;
1581
0
}
1582
1583
0
int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby) {
1584
0
    mz_zip *zip = (mz_zip *)handle;
1585
0
    if (!zip)
1586
0
        return MZ_PARAM_ERROR;
1587
0
    zip->version_madeby = version_madeby;
1588
0
    return MZ_OK;
1589
0
}
1590
1591
0
int32_t mz_zip_set_recover(void *handle, uint8_t recover) {
1592
0
    mz_zip *zip = (mz_zip *)handle;
1593
0
    if (!zip)
1594
0
        return MZ_PARAM_ERROR;
1595
0
    zip->recover = recover;
1596
0
    return MZ_OK;
1597
0
}
1598
1599
0
int32_t mz_zip_set_data_descriptor(void *handle, uint8_t data_descriptor) {
1600
0
    mz_zip *zip = (mz_zip *)handle;
1601
0
    if (!zip)
1602
0
        return MZ_PARAM_ERROR;
1603
0
    zip->data_descriptor = data_descriptor;
1604
0
    return MZ_OK;
1605
0
}
1606
1607
0
int32_t mz_zip_get_stream(void *handle, void **stream) {
1608
0
    mz_zip *zip = (mz_zip *)handle;
1609
0
    if (!zip || !stream)
1610
0
        return MZ_PARAM_ERROR;
1611
0
    *stream = zip->stream;
1612
0
    if (!*stream)
1613
0
        return MZ_EXIST_ERROR;
1614
0
    return MZ_OK;
1615
0
}
1616
1617
0
int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream) {
1618
0
    mz_zip *zip = (mz_zip *)handle;
1619
0
    if (!zip || !cd_stream)
1620
0
        return MZ_PARAM_ERROR;
1621
0
    zip->cd_offset = 0;
1622
0
    zip->cd_stream = cd_stream;
1623
0
    zip->cd_start_pos = cd_start_pos;
1624
0
    return MZ_OK;
1625
0
}
1626
1627
0
int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream) {
1628
0
    mz_zip *zip = (mz_zip *)handle;
1629
0
    if (!zip || !cd_mem_stream)
1630
0
        return MZ_PARAM_ERROR;
1631
0
    *cd_mem_stream = zip->cd_mem_stream;
1632
0
    if (!*cd_mem_stream)
1633
0
        return MZ_EXIST_ERROR;
1634
0
    return MZ_OK;
1635
0
}
1636
1637
0
int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry) {
1638
0
    mz_zip *zip = (mz_zip *)handle;
1639
0
    if (!zip)
1640
0
        return MZ_PARAM_ERROR;
1641
0
    zip->number_entry = number_entry;
1642
0
    return MZ_OK;
1643
0
}
1644
1645
0
int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry) {
1646
0
    mz_zip *zip = (mz_zip *)handle;
1647
0
    if (!zip || !number_entry)
1648
0
        return MZ_PARAM_ERROR;
1649
0
    *number_entry = zip->number_entry;
1650
0
    return MZ_OK;
1651
0
}
1652
1653
0
int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd) {
1654
0
    mz_zip *zip = (mz_zip *)handle;
1655
0
    if (!zip)
1656
0
        return MZ_PARAM_ERROR;
1657
0
    zip->disk_number_with_cd = disk_number_with_cd;
1658
0
    return MZ_OK;
1659
0
}
1660
1661
0
int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd) {
1662
0
    mz_zip *zip = (mz_zip *)handle;
1663
0
    if (!zip || !disk_number_with_cd)
1664
0
        return MZ_PARAM_ERROR;
1665
0
    *disk_number_with_cd = zip->disk_number_with_cd;
1666
0
    return MZ_OK;
1667
0
}
1668
1669
348
static int32_t mz_zip_entry_close_int(void *handle) {
1670
348
    mz_zip *zip = (mz_zip *)handle;
1671
1672
348
    if (zip->crypt_stream)
1673
348
        mz_stream_delete(&zip->crypt_stream);
1674
348
    zip->crypt_stream = NULL;
1675
348
    if (zip->compress_stream)
1676
348
        mz_stream_delete(&zip->compress_stream);
1677
348
    zip->compress_stream = NULL;
1678
1679
348
    zip->entry_opened = 0;
1680
1681
348
    return MZ_OK;
1682
348
}
1683
1684
348
static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress_level, const char *password) {
1685
348
    mz_zip *zip = (mz_zip *)handle;
1686
348
    int64_t max_total_in = 0;
1687
348
    int64_t header_size = 0;
1688
348
    int64_t footer_size = 0;
1689
348
    int32_t err = MZ_OK;
1690
348
    uint8_t use_crypt = 0;
1691
1692
348
    if (!zip)
1693
0
        return MZ_PARAM_ERROR;
1694
1695
348
    switch (zip->file_info.compression_method) {
1696
348
    case MZ_COMPRESS_METHOD_STORE:
1697
348
    case MZ_COMPRESS_METHOD_DEFLATE:
1698
#ifdef HAVE_BZIP2
1699
    case MZ_COMPRESS_METHOD_BZIP2:
1700
#endif
1701
#ifdef HAVE_LZMA
1702
    case MZ_COMPRESS_METHOD_LZMA:
1703
#endif
1704
#if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP)
1705
    case MZ_COMPRESS_METHOD_XZ:
1706
#endif
1707
#ifdef HAVE_ZSTD
1708
    case MZ_COMPRESS_METHOD_ZSTD:
1709
#endif
1710
348
        err = MZ_OK;
1711
348
        break;
1712
0
    default:
1713
0
        return MZ_SUPPORT_ERROR;
1714
348
    }
1715
1716
#ifndef HAVE_WZAES
1717
    if (zip->file_info.aes_version)
1718
        return MZ_SUPPORT_ERROR;
1719
#endif
1720
1721
348
    zip->entry_raw = raw;
1722
1723
348
    if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password)) {
1724
186
        if (zip->open_mode & MZ_OPEN_MODE_WRITE) {
1725
            /* Encrypt only when we are not trying to write raw and password is supplied. */
1726
186
            if (!zip->entry_raw)
1727
186
                use_crypt = 1;
1728
186
        } else if (zip->open_mode & MZ_OPEN_MODE_READ) {
1729
            /* Decrypt only when password is supplied. Don't error when password */
1730
            /* is not supplied as we may want to read the raw encrypted data. */
1731
0
            use_crypt = 1;
1732
0
        }
1733
186
    }
1734
1735
348
    if ((err == MZ_OK) && (use_crypt)) {
1736
186
#ifdef HAVE_WZAES
1737
186
        if (zip->file_info.aes_version) {
1738
0
            zip->crypt_stream = mz_stream_wzaes_create();
1739
0
            if (!zip->crypt_stream)
1740
0
                return MZ_MEM_ERROR;
1741
0
            mz_stream_wzaes_set_password(zip->crypt_stream, password);
1742
0
            mz_stream_wzaes_set_strength(zip->crypt_stream, zip->file_info.aes_strength);
1743
0
        } else
1744
186
#endif
1745
186
        {
1746
186
#ifdef HAVE_PKCRYPT
1747
186
            uint8_t verify1 = (uint8_t)((zip->file_info.pk_verify >> 8) & 0xff);
1748
186
            uint8_t verify2 = (uint8_t)((zip->file_info.pk_verify) & 0xff);
1749
1750
186
            zip->crypt_stream = mz_stream_pkcrypt_create();
1751
186
            if (!zip->crypt_stream)
1752
0
                return MZ_MEM_ERROR;
1753
186
            mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1754
186
            mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2, zip->file_info.version_needed);
1755
186
#endif
1756
186
        }
1757
186
    }
1758
1759
348
    if (err == MZ_OK) {
1760
348
        if (!zip->crypt_stream)
1761
162
            zip->crypt_stream = mz_stream_raw_create();
1762
348
        if (!zip->crypt_stream)
1763
0
            return MZ_MEM_ERROR;
1764
1765
348
        mz_stream_set_base(zip->crypt_stream, zip->stream);
1766
1767
348
        err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
1768
348
    }
1769
1770
348
    if (err == MZ_OK) {
1771
348
        if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
1772
348
            zip->compress_stream = mz_stream_raw_create();
1773
#ifdef HAVE_ZLIB
1774
        else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
1775
            zip->compress_stream = mz_stream_zlib_create();
1776
#endif
1777
#ifdef HAVE_BZIP2
1778
        else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
1779
            zip->compress_stream = mz_stream_bzip_create();
1780
#endif
1781
#ifdef HAVE_LIBCOMP
1782
        else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE ||
1783
                 zip->file_info.compression_method == MZ_COMPRESS_METHOD_XZ) {
1784
            zip->compress_stream = mz_stream_libcomp_create();
1785
            if (zip->compress_stream) {
1786
                mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_METHOD,
1787
                                         zip->file_info.compression_method);
1788
            }
1789
        }
1790
#endif
1791
#ifdef HAVE_LZMA
1792
        else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA ||
1793
                 zip->file_info.compression_method == MZ_COMPRESS_METHOD_XZ) {
1794
            zip->compress_stream = mz_stream_lzma_create();
1795
            if (zip->compress_stream) {
1796
                mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_METHOD,
1797
                                         zip->file_info.compression_method);
1798
            }
1799
        }
1800
#endif
1801
#ifdef HAVE_ZSTD
1802
        else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_ZSTD)
1803
            zip->compress_stream = mz_stream_zstd_create();
1804
#endif
1805
0
        else
1806
0
            err = MZ_PARAM_ERROR;
1807
348
    }
1808
1809
348
    if (err == MZ_OK && !zip->compress_stream)
1810
0
        err = MZ_MEM_ERROR;
1811
1812
348
    if (err == MZ_OK) {
1813
348
        if (zip->open_mode & MZ_OPEN_MODE_WRITE) {
1814
348
            mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
1815
348
        } else {
1816
0
            int32_t set_end_of_stream = 0;
1817
1818
0
#ifndef HAVE_LIBCOMP
1819
0
            if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE ||
1820
0
                zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
1821
0
#endif
1822
0
            {
1823
0
                max_total_in = zip->file_info.compressed_size;
1824
0
                mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1825
1826
0
                if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1827
0
                    max_total_in -= header_size;
1828
0
                if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1829
0
                    max_total_in -= footer_size;
1830
1831
0
                mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1832
0
            }
1833
1834
0
            switch (zip->file_info.compression_method) {
1835
0
            case MZ_COMPRESS_METHOD_LZMA:
1836
0
            case MZ_COMPRESS_METHOD_XZ:
1837
0
                set_end_of_stream = (zip->file_info.flag & MZ_ZIP_FLAG_LZMA_EOS_MARKER);
1838
0
                break;
1839
0
            case MZ_COMPRESS_METHOD_ZSTD:
1840
0
                set_end_of_stream = 1;
1841
0
                break;
1842
0
            }
1843
1844
0
            if (set_end_of_stream) {
1845
0
                mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX,
1846
0
                                         zip->file_info.compressed_size);
1847
0
                mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX,
1848
0
                                         zip->file_info.uncompressed_size);
1849
0
            }
1850
0
        }
1851
1852
348
        mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1853
1854
348
        err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1855
348
    }
1856
1857
348
    if (err == MZ_OK) {
1858
348
        zip->entry_opened = 1;
1859
348
        zip->entry_crc32 = 0;
1860
348
    } else {
1861
0
        mz_zip_entry_close_int(zip);
1862
0
    }
1863
1864
348
    return err;
1865
348
}
1866
1867
1.91k
int32_t mz_zip_entry_is_open(void *handle) {
1868
1.91k
    mz_zip *zip = (mz_zip *)handle;
1869
1.91k
    if (!zip)
1870
0
        return MZ_PARAM_ERROR;
1871
1.91k
    if (zip->entry_opened == 0)
1872
874
        return MZ_EXIST_ERROR;
1873
1.04k
    return MZ_OK;
1874
1.91k
}
1875
1876
0
int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password) {
1877
0
    mz_zip *zip = (mz_zip *)handle;
1878
0
    int32_t err = MZ_OK;
1879
0
    int32_t err_shift = MZ_OK;
1880
1881
#if defined(MZ_ZIP_NO_ENCRYPTION)
1882
    if (password)
1883
        return MZ_SUPPORT_ERROR;
1884
#endif
1885
0
    if (!zip || !zip->entry_scanned)
1886
0
        return MZ_PARAM_ERROR;
1887
0
    if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
1888
0
        return MZ_PARAM_ERROR;
1889
1890
0
    mz_zip_print("Zip - Entry - Read open (raw %" PRId32 ")\n", raw);
1891
1892
0
    err = mz_zip_entry_seek_local_header(zip);
1893
0
    if (err == MZ_OK)
1894
0
        err = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
1895
1896
0
    if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0) {
1897
        /* Perhaps we didn't compensated correctly for incorrect cd offset */
1898
0
        err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
1899
0
        if (err_shift == MZ_OK)
1900
0
            err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
1901
0
        if (err_shift == MZ_OK) {
1902
0
            zip->disk_offset_shift = 0;
1903
0
            err = err_shift;
1904
0
        }
1905
0
    }
1906
1907
0
#ifdef MZ_ZIP_NO_DECOMPRESSION
1908
0
    if (!raw && zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
1909
0
        err = MZ_SUPPORT_ERROR;
1910
0
#endif
1911
0
    if (err == MZ_OK)
1912
0
        err = mz_zip_entry_open_int(zip, raw, 0, password);
1913
1914
0
    return err;
1915
0
}
1916
1917
int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int16_t compress_level, uint8_t raw,
1918
437
                                const char *password) {
1919
437
    mz_zip *zip = (mz_zip *)handle;
1920
437
    int64_t filename_pos = -1;
1921
437
    int64_t extrafield_pos = 0;
1922
437
    int64_t comment_pos = 0;
1923
437
    int64_t linkname_pos = 0;
1924
437
    int64_t disk_number = 0;
1925
437
    uint8_t is_dir = 0;
1926
437
    int32_t err = MZ_OK;
1927
1928
#if defined(MZ_ZIP_NO_ENCRYPTION)
1929
    if (password)
1930
        return MZ_SUPPORT_ERROR;
1931
#endif
1932
437
    if (!zip || !file_info || !file_info->filename)
1933
0
        return MZ_PARAM_ERROR;
1934
1935
437
    if (mz_zip_entry_is_open(zip) == MZ_OK) {
1936
0
        err = mz_zip_entry_close(zip);
1937
0
        if (err != MZ_OK)
1938
0
            return err;
1939
0
    }
1940
1941
437
    memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
1942
1943
437
    mz_zip_print("Zip - Entry - Write open - %s (level %" PRId16 " raw %" PRId8 ")\n", zip->file_info.filename,
1944
437
                 compress_level, raw);
1945
1946
437
    mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
1947
437
    mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
1948
1949
    /* Copy filename, extrafield, and comment internally */
1950
437
    filename_pos = mz_stream_tell(zip->file_info_stream);
1951
437
    if (file_info->filename)
1952
437
        mz_stream_write(zip->file_info_stream, file_info->filename, (int32_t)strlen(file_info->filename));
1953
437
    mz_stream_write_uint8(zip->file_info_stream, 0);
1954
1955
437
    extrafield_pos = mz_stream_tell(zip->file_info_stream);
1956
437
    if (file_info->extrafield)
1957
0
        mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
1958
437
    mz_stream_write_uint8(zip->file_info_stream, 0);
1959
1960
437
    comment_pos = mz_stream_tell(zip->file_info_stream);
1961
437
    if (file_info->comment)
1962
0
        mz_stream_write(zip->file_info_stream, file_info->comment, file_info->comment_size);
1963
437
    mz_stream_write_uint8(zip->file_info_stream, 0);
1964
1965
437
    linkname_pos = mz_stream_tell(zip->file_info_stream);
1966
437
    if (file_info->linkname)
1967
0
        mz_stream_write(zip->file_info_stream, file_info->linkname, (int32_t)strlen(file_info->linkname));
1968
437
    mz_stream_write_uint8(zip->file_info_stream, 0);
1969
1970
437
    mz_stream_mem_get_buffer_at(zip->file_info_stream, filename_pos, (const void **)&zip->file_info.filename);
1971
437
    mz_stream_mem_get_buffer_at(zip->file_info_stream, extrafield_pos, (const void **)&zip->file_info.extrafield);
1972
437
    mz_stream_mem_get_buffer_at(zip->file_info_stream, comment_pos, (const void **)&zip->file_info.comment);
1973
437
    mz_stream_mem_get_buffer_at(zip->file_info_stream, linkname_pos, (const void **)&zip->file_info.linkname);
1974
1975
437
    if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE) {
1976
63
        if ((compress_level == 8) || (compress_level == 9))
1977
2
            zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
1978
63
        if (compress_level == 2)
1979
1
            zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
1980
63
        if (compress_level == 1)
1981
1
            zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1982
63
    }
1983
#if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP)
1984
    else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA ||
1985
             zip->file_info.compression_method == MZ_COMPRESS_METHOD_XZ)
1986
        zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
1987
#endif
1988
1989
437
    if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
1990
0
        is_dir = 1;
1991
1992
437
    if (!raw && !is_dir) {
1993
437
        if (zip->data_descriptor)
1994
437
            zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
1995
437
        if (password)
1996
190
            zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
1997
437
    }
1998
1999
437
    mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
2000
437
    zip->file_info.disk_number = (uint32_t)disk_number;
2001
437
    zip->file_info.disk_offset = mz_stream_tell(zip->stream);
2002
2003
437
    if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) {
2004
190
#ifdef HAVE_PKCRYPT
2005
        /* Pre-calculated CRC value is required for PKWARE traditional encryption */
2006
190
        uint32_t dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
2007
190
        zip->file_info.pk_verify = mz_zip_get_pk_verify(dos_date, zip->file_info.crc, zip->file_info.flag);
2008
190
#endif
2009
190
#ifdef HAVE_WZAES
2010
190
        if (zip->file_info.aes_version && zip->file_info.aes_strength == 0)
2011
0
            zip->file_info.aes_strength = MZ_AES_STRENGTH_256;
2012
190
#endif
2013
190
    }
2014
2015
437
    zip->file_info.crc = 0;
2016
437
    zip->file_info.compressed_size = 0;
2017
2018
437
    if ((compress_level == 0) || (is_dir))
2019
25
        zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
2020
2021
437
#ifdef MZ_ZIP_NO_COMPRESSION
2022
437
    if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
2023
89
        err = MZ_SUPPORT_ERROR;
2024
437
#endif
2025
437
    if (err == MZ_OK)
2026
348
        err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
2027
437
    if (err == MZ_OK)
2028
348
        err = mz_zip_entry_open_int(zip, raw, compress_level, password);
2029
2030
437
    return err;
2031
437
}
2032
2033
0
int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len) {
2034
0
    mz_zip *zip = (mz_zip *)handle;
2035
0
    int32_t read = 0;
2036
2037
0
    if (!zip || mz_zip_entry_is_open(zip) != MZ_OK)
2038
0
        return MZ_PARAM_ERROR;
2039
0
    if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) /* zlib limitation */
2040
0
        return MZ_PARAM_ERROR;
2041
0
    if (len == 0)
2042
0
        return MZ_PARAM_ERROR;
2043
2044
0
    if (zip->file_info.compressed_size == 0)
2045
0
        return 0;
2046
2047
    /* Read entire entry even if uncompressed_size = 0, otherwise */
2048
    /* aes encryption validation will fail if compressed_size > 0 */
2049
0
    read = mz_stream_read(zip->compress_stream, buf, len);
2050
0
    if (read > 0)
2051
0
        zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read);
2052
2053
0
    mz_zip_print("Zip - Entry - Read - %" PRId32 " (max %" PRId32 ")\n", read, len);
2054
2055
0
    return read;
2056
0
}
2057
2058
348
int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len) {
2059
348
    mz_zip *zip = (mz_zip *)handle;
2060
348
    int32_t written = 0;
2061
2062
348
    if (!zip || mz_zip_entry_is_open(zip) != MZ_OK)
2063
0
        return MZ_PARAM_ERROR;
2064
348
    written = mz_stream_write(zip->compress_stream, buf, len);
2065
348
    if (written > 0)
2066
270
        zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
2067
2068
348
    mz_zip_print("Zip - Entry - Write - %" PRId32 " (max %" PRId32 ")\n", written, len);
2069
348
    return written;
2070
348
}
2071
2072
0
int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size) {
2073
0
    mz_zip *zip = (mz_zip *)handle;
2074
0
    int64_t total_in = 0;
2075
0
    int32_t err = MZ_OK;
2076
0
    uint8_t zip64 = 0;
2077
2078
0
    if (!zip || mz_zip_entry_is_open(zip) != MZ_OK)
2079
0
        return MZ_PARAM_ERROR;
2080
2081
0
    mz_stream_close(zip->compress_stream);
2082
2083
0
    mz_zip_print("Zip - Entry - Read Close\n");
2084
2085
0
    if (crc32)
2086
0
        *crc32 = zip->file_info.crc;
2087
0
    if (compressed_size)
2088
0
        *compressed_size = zip->file_info.compressed_size;
2089
0
    if (uncompressed_size)
2090
0
        *uncompressed_size = zip->file_info.uncompressed_size;
2091
2092
0
    mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
2093
2094
0
    if ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) &&
2095
0
        ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0) && (crc32 || compressed_size || uncompressed_size)) {
2096
        /* Check to see if data descriptor is zip64 bit format or not */
2097
0
        if (mz_zip_extrafield_contains(zip->local_file_info.extrafield, zip->local_file_info.extrafield_size,
2098
0
                                       MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK) {
2099
0
            zip64 = 1;
2100
0
        }
2101
2102
0
        err = mz_zip_entry_seek_local_header(zip);
2103
2104
        /* Seek to end of compressed stream since we might have over-read during decompression */
2105
0
        if (err == MZ_OK) {
2106
0
            err = mz_stream_seek(zip->stream,
2107
0
                                 MZ_ZIP_SIZE_LD_ITEM + (int64_t)zip->local_file_info.filename_size +
2108
0
                                     (int64_t)zip->local_file_info.extrafield_size + total_in,
2109
0
                                 MZ_SEEK_CUR);
2110
0
        }
2111
2112
        /* Read data descriptor */
2113
0
        if (err == MZ_OK)
2114
0
            err = mz_zip_entry_read_descriptor(zip->stream, zip64, crc32, compressed_size, uncompressed_size);
2115
0
    }
2116
2117
    /* If entire entry was not read verification will fail */
2118
0
    if ((err == MZ_OK) && (total_in == zip->file_info.compressed_size) && (!zip->entry_raw)) {
2119
0
#ifdef HAVE_WZAES
2120
        /* AES zip version AE-1 will expect a valid crc as well */
2121
0
        if (zip->file_info.aes_version <= 0x0001)
2122
0
#endif
2123
0
        {
2124
0
            if (zip->entry_crc32 != zip->file_info.crc) {
2125
0
                mz_zip_print("Zip - Entry - Crc failed (actual 0x%08" PRIx32 " expected 0x%08" PRIx32 ")\n",
2126
0
                             zip->entry_crc32, zip->file_info.crc);
2127
2128
0
                err = MZ_CRC_ERROR;
2129
0
            }
2130
0
        }
2131
0
    }
2132
2133
0
    mz_zip_entry_close_int(zip);
2134
2135
0
    return err;
2136
0
}
2137
2138
348
int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size) {
2139
348
    mz_zip *zip = (mz_zip *)handle;
2140
348
    int64_t end_disk_number = 0;
2141
348
    int32_t err = MZ_OK;
2142
348
    uint8_t zip64 = 0;
2143
2144
348
    if (!zip || mz_zip_entry_is_open(zip) != MZ_OK)
2145
0
        return MZ_PARAM_ERROR;
2146
2147
348
    mz_stream_close(zip->compress_stream);
2148
2149
348
    if (!zip->entry_raw)
2150
348
        crc32 = zip->entry_crc32;
2151
2152
348
    mz_zip_print("Zip - Entry - Write Close (crc 0x%08" PRIx32 " cs %" PRId64 " ucs %" PRId64 ")\n", crc32,
2153
348
                 compressed_size, uncompressed_size);
2154
2155
    /* If sizes are not set, then read them from the compression stream */
2156
348
    if (compressed_size < 0)
2157
348
        mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2158
348
    if (uncompressed_size < 0)
2159
348
        mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size);
2160
2161
348
    if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) {
2162
186
        mz_stream_set_base(zip->crypt_stream, zip->stream);
2163
186
        err = mz_stream_close(zip->crypt_stream);
2164
2165
186
        mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2166
186
    }
2167
2168
348
    mz_zip_entry_needs_zip64(&zip->file_info, 1, &zip64);
2169
2170
348
    if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)) {
2171
        /* Determine if we need to write data descriptor in zip64 format,
2172
           if local extrafield was saved with zip64 extrafield */
2173
2174
348
        if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
2175
98
            err = mz_zip_entry_write_descriptor(zip->stream, zip64, 0, compressed_size, 0);
2176
250
        else
2177
250
            err = mz_zip_entry_write_descriptor(zip->stream, zip64, crc32, compressed_size, uncompressed_size);
2178
348
    }
2179
2180
    /* Write file info to central directory */
2181
2182
348
    mz_zip_print("Zip - Entry - Write cd (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n", uncompressed_size,
2183
348
                 compressed_size, crc32);
2184
2185
348
    zip->file_info.crc = crc32;
2186
348
    zip->file_info.compressed_size = compressed_size;
2187
348
    zip->file_info.uncompressed_size = uncompressed_size;
2188
2189
348
    if (err == MZ_OK)
2190
348
        err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
2191
2192
    /* Update local header with crc32 and sizes */
2193
348
    if ((err == MZ_OK) && ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) == 0) &&
2194
348
        ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0)) {
2195
        /* Save the disk number and position we are to seek back after updating local header */
2196
0
        int64_t end_pos = mz_stream_tell(zip->stream);
2197
0
        mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &end_disk_number);
2198
2199
0
        err = mz_zip_entry_seek_local_header(zip);
2200
2201
0
        if (err == MZ_OK) {
2202
            /* Seek to crc32 and sizes offset in local header */
2203
0
            mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
2204
0
            err = mz_stream_seek(zip->stream, zip->file_info.disk_offset + MZ_ZIP_OFFSET_CRC_SIZES, MZ_SEEK_SET);
2205
0
        }
2206
2207
0
        if (err == MZ_OK)
2208
0
            err = mz_zip_entry_write_crc_sizes(zip->stream, zip64, 0, &zip->file_info);
2209
2210
        /* Seek to and update zip64 extension sizes */
2211
0
        if ((err == MZ_OK) && (zip64)) {
2212
0
            int64_t filename_size = zip->file_info.filename_size;
2213
2214
0
            if (filename_size == 0)
2215
0
                filename_size = strlen(zip->file_info.filename);
2216
2217
            /* Since we write zip64 extension first we know its offset */
2218
0
            err = mz_stream_seek(zip->stream, 2 + 2 + filename_size + 4, MZ_SEEK_CUR);
2219
2220
0
            if (err == MZ_OK)
2221
0
                err = mz_stream_write_uint64(zip->stream, zip->file_info.uncompressed_size);
2222
0
            if (err == MZ_OK)
2223
0
                err = mz_stream_write_uint64(zip->stream, zip->file_info.compressed_size);
2224
0
        }
2225
2226
0
        mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, end_disk_number);
2227
0
        mz_stream_seek(zip->stream, end_pos, MZ_SEEK_SET);
2228
0
    }
2229
2230
348
    zip->number_entry += 1;
2231
2232
348
    mz_zip_entry_close_int(zip);
2233
2234
348
    return err;
2235
348
}
2236
2237
0
int32_t mz_zip_entry_seek_local_header(void *handle) {
2238
0
    mz_zip *zip = (mz_zip *)handle;
2239
0
    int64_t disk_size = 0;
2240
0
    uint32_t disk_number = 0;
2241
2242
0
    if (!zip)
2243
0
        return MZ_PARAM_ERROR;
2244
0
    disk_number = zip->file_info.disk_number;
2245
0
    if (disk_number == zip->disk_number_with_cd) {
2246
0
        mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size);
2247
0
        if ((disk_size == 0) || ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0))
2248
0
            disk_number = (uint32_t)-1;
2249
0
    }
2250
2251
0
    mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, disk_number);
2252
2253
0
    mz_zip_print("Zip - Entry - Seek local (disk %" PRId32 " offset %" PRId64 ")\n", disk_number,
2254
0
                 zip->file_info.disk_offset);
2255
2256
    /* Guard against seek overflows */
2257
0
    if ((zip->disk_offset_shift > 0) && (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
2258
0
        return MZ_FORMAT_ERROR;
2259
2260
0
    return mz_stream_seek(zip->stream, zip->file_info.disk_offset + zip->disk_offset_shift, MZ_SEEK_SET);
2261
0
}
2262
2263
0
int32_t mz_zip_entry_get_compress_stream(void *handle, void **compress_stream) {
2264
0
    mz_zip *zip = (mz_zip *)handle;
2265
0
    if (!zip || !compress_stream)
2266
0
        return MZ_PARAM_ERROR;
2267
0
    *compress_stream = zip->compress_stream;
2268
0
    if (!*compress_stream)
2269
0
        return MZ_EXIST_ERROR;
2270
0
    return MZ_OK;
2271
0
}
2272
2273
348
int32_t mz_zip_entry_close(void *handle) {
2274
348
    return mz_zip_entry_close_raw(handle, UINT64_MAX, 0);
2275
348
}
2276
2277
348
int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32) {
2278
348
    mz_zip *zip = (mz_zip *)handle;
2279
348
    int32_t err = MZ_OK;
2280
2281
348
    if (!zip || mz_zip_entry_is_open(zip) != MZ_OK)
2282
0
        return MZ_PARAM_ERROR;
2283
2284
348
    if (zip->open_mode & MZ_OPEN_MODE_WRITE)
2285
348
        err = mz_zip_entry_write_close(zip, crc32, UINT64_MAX, uncompressed_size);
2286
0
    else
2287
0
        err = mz_zip_entry_read_close(zip, NULL, NULL, NULL);
2288
2289
348
    return err;
2290
348
}
2291
2292
0
int32_t mz_zip_entry_is_dir(void *handle) {
2293
0
    mz_zip *zip = (mz_zip *)handle;
2294
0
    int32_t filename_length = 0;
2295
2296
0
    if (!zip || !zip->entry_scanned)
2297
0
        return MZ_PARAM_ERROR;
2298
0
    if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2299
0
        return MZ_OK;
2300
2301
0
    filename_length = (int32_t)strlen(zip->file_info.filename);
2302
0
    if (filename_length > 0) {
2303
0
        if (mz_os_is_dir_separator(zip->file_info.filename[filename_length - 1]))
2304
0
            return MZ_OK;
2305
0
    }
2306
0
    return MZ_EXIST_ERROR;
2307
0
}
2308
2309
0
int32_t mz_zip_entry_is_symlink(void *handle) {
2310
0
    mz_zip *zip = (mz_zip *)handle;
2311
2312
0
    if (!zip || !zip->entry_scanned)
2313
0
        return MZ_PARAM_ERROR;
2314
0
    if (mz_zip_attrib_is_symlink(zip->file_info.external_fa, zip->file_info.version_madeby) != MZ_OK)
2315
0
        return MZ_EXIST_ERROR;
2316
2317
0
    return MZ_OK;
2318
0
}
2319
2320
0
int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info) {
2321
0
    mz_zip *zip = (mz_zip *)handle;
2322
2323
0
    if (!zip)
2324
0
        return MZ_PARAM_ERROR;
2325
2326
0
    if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0) {
2327
0
        if (!zip->entry_scanned)
2328
0
            return MZ_PARAM_ERROR;
2329
0
    }
2330
2331
0
    *file_info = &zip->file_info;
2332
0
    return MZ_OK;
2333
0
}
2334
2335
0
int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info) {
2336
0
    mz_zip *zip = (mz_zip *)handle;
2337
0
    if (!zip || mz_zip_entry_is_open(zip) != MZ_OK)
2338
0
        return MZ_PARAM_ERROR;
2339
0
    *local_file_info = &zip->local_file_info;
2340
0
    return MZ_OK;
2341
0
}
2342
2343
0
int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size) {
2344
0
    mz_zip *zip = (mz_zip *)handle;
2345
2346
0
    if (!zip || mz_zip_entry_is_open(zip) != MZ_OK)
2347
0
        return MZ_PARAM_ERROR;
2348
2349
0
    zip->file_info.extrafield = extrafield;
2350
0
    zip->file_info.extrafield_size = extrafield_size;
2351
0
    return MZ_OK;
2352
0
}
2353
2354
0
static int32_t mz_zip_goto_next_entry_int(void *handle) {
2355
0
    mz_zip *zip = (mz_zip *)handle;
2356
0
    int32_t err = MZ_OK;
2357
2358
0
    if (!zip)
2359
0
        return MZ_PARAM_ERROR;
2360
2361
0
    zip->entry_scanned = 0;
2362
2363
0
    mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
2364
2365
0
    err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
2366
0
    if (err == MZ_OK)
2367
0
        err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
2368
0
    if (err == MZ_OK)
2369
0
        zip->entry_scanned = 1;
2370
0
    return err;
2371
0
}
2372
2373
0
int64_t mz_zip_get_entry(void *handle) {
2374
0
    mz_zip *zip = (mz_zip *)handle;
2375
2376
0
    if (!zip)
2377
0
        return MZ_PARAM_ERROR;
2378
2379
0
    return zip->cd_current_pos;
2380
0
}
2381
2382
0
int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos) {
2383
0
    mz_zip *zip = (mz_zip *)handle;
2384
2385
0
    if (!zip)
2386
0
        return MZ_PARAM_ERROR;
2387
2388
0
    if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
2389
0
        return MZ_PARAM_ERROR;
2390
2391
0
    zip->cd_current_pos = cd_pos;
2392
2393
0
    return mz_zip_goto_next_entry_int(zip);
2394
0
}
2395
2396
0
int32_t mz_zip_goto_first_entry(void *handle) {
2397
0
    mz_zip *zip = (mz_zip *)handle;
2398
2399
0
    if (!zip)
2400
0
        return MZ_PARAM_ERROR;
2401
2402
0
    zip->cd_current_pos = zip->cd_start_pos;
2403
2404
0
    return mz_zip_goto_next_entry_int(zip);
2405
0
}
2406
2407
0
int32_t mz_zip_goto_next_entry(void *handle) {
2408
0
    mz_zip *zip = (mz_zip *)handle;
2409
2410
0
    if (!zip)
2411
0
        return MZ_PARAM_ERROR;
2412
2413
0
    zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
2414
0
        zip->file_info.extrafield_size + zip->file_info.comment_size;
2415
2416
0
    return mz_zip_goto_next_entry_int(zip);
2417
0
}
2418
2419
0
int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case) {
2420
0
    mz_zip *zip = (mz_zip *)handle;
2421
0
    int32_t err = MZ_OK;
2422
0
    int32_t result = 0;
2423
2424
0
    if (!zip || !filename)
2425
0
        return MZ_PARAM_ERROR;
2426
2427
    /* If we are already on the current entry, no need to search */
2428
0
    if (zip->entry_scanned && zip->file_info.filename) {
2429
0
        result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2430
0
        if (result == 0)
2431
0
            return MZ_OK;
2432
0
    }
2433
2434
    /* Search all entries starting at the first */
2435
0
    err = mz_zip_goto_first_entry(zip);
2436
0
    while (err == MZ_OK) {
2437
0
        result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2438
0
        if (result == 0)
2439
0
            return MZ_OK;
2440
2441
0
        err = mz_zip_goto_next_entry(zip);
2442
0
    }
2443
2444
0
    return err;
2445
0
}
2446
2447
0
int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb) {
2448
0
    mz_zip *zip = (mz_zip *)handle;
2449
0
    int32_t err = MZ_OK;
2450
0
    int32_t result = 0;
2451
2452
    /* Search first entry looking for match */
2453
0
    err = mz_zip_goto_first_entry(zip);
2454
0
    if (err != MZ_OK)
2455
0
        return err;
2456
2457
0
    result = cb(zip, userdata, &zip->file_info);
2458
0
    if (result == 0)
2459
0
        return MZ_OK;
2460
2461
0
    return mz_zip_locate_next_entry(zip, userdata, cb);
2462
0
}
2463
2464
0
int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb) {
2465
0
    mz_zip *zip = (mz_zip *)handle;
2466
0
    int32_t err = MZ_OK;
2467
0
    int32_t result = 0;
2468
2469
    /* Search next entries looking for match */
2470
0
    err = mz_zip_goto_next_entry(zip);
2471
0
    while (err == MZ_OK) {
2472
0
        result = cb(zip, userdata, &zip->file_info);
2473
0
        if (result == 0)
2474
0
            return MZ_OK;
2475
2476
0
        err = mz_zip_goto_next_entry(zip);
2477
0
    }
2478
2479
0
    return err;
2480
0
}
2481
2482
/***************************************************************************/
2483
2484
1.66k
int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby) {
2485
1.66k
    uint32_t posix_attrib = 0;
2486
1.66k
    uint8_t system = MZ_HOST_SYSTEM(version_madeby);
2487
1.66k
    int32_t err = MZ_OK;
2488
2489
1.66k
    err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2490
1.66k
    if (err == MZ_OK) {
2491
1.66k
        if ((posix_attrib & 0170000) == 0040000) /* S_ISDIR */
2492
0
            return MZ_OK;
2493
1.66k
    }
2494
2495
1.66k
    return MZ_EXIST_ERROR;
2496
1.66k
}
2497
2498
0
int32_t mz_zip_attrib_is_symlink(uint32_t attrib, int32_t version_madeby) {
2499
0
    uint32_t posix_attrib = 0;
2500
0
    uint8_t system = MZ_HOST_SYSTEM(version_madeby);
2501
0
    int32_t err = MZ_OK;
2502
2503
0
    err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2504
0
    if (err == MZ_OK) {
2505
0
        if ((posix_attrib & 0170000) == 0120000) /* S_ISLNK */
2506
0
            return MZ_OK;
2507
0
    }
2508
2509
0
    return MZ_EXIST_ERROR;
2510
0
}
2511
2512
1.66k
int32_t mz_zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys, uint32_t *target_attrib) {
2513
1.66k
    if (!target_attrib)
2514
0
        return MZ_PARAM_ERROR;
2515
2516
1.66k
    *target_attrib = 0;
2517
2518
1.66k
    if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS)) {
2519
1.66k
        if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS)) {
2520
0
            *target_attrib = src_attrib;
2521
0
            return MZ_OK;
2522
0
        }
2523
1.66k
        if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) ||
2524
1.66k
            (target_sys == MZ_HOST_SYSTEM_RISCOS))
2525
1.66k
            return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
2526
1.66k
    } else if ((src_sys == MZ_HOST_SYSTEM_UNIX) || (src_sys == MZ_HOST_SYSTEM_OSX_DARWIN) ||
2527
0
               (src_sys == MZ_HOST_SYSTEM_RISCOS)) {
2528
        /* If high bytes are set, it contains unix specific attributes */
2529
0
        if ((src_attrib >> 16) != 0)
2530
0
            src_attrib >>= 16;
2531
2532
0
        if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) ||
2533
0
            (target_sys == MZ_HOST_SYSTEM_RISCOS)) {
2534
0
            *target_attrib = src_attrib;
2535
0
            return MZ_OK;
2536
0
        }
2537
0
        if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
2538
0
            return mz_zip_attrib_posix_to_win32(src_attrib, target_attrib);
2539
0
    }
2540
2541
0
    return MZ_SUPPORT_ERROR;
2542
1.66k
}
2543
2544
0
int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib) {
2545
0
    if (!win32_attrib)
2546
0
        return MZ_PARAM_ERROR;
2547
2548
0
    *win32_attrib = 0;
2549
2550
    /* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */
2551
0
    if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
2552
0
        *win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
2553
    /* S_IFLNK */
2554
0
    if ((posix_attrib & 0170000) == 0120000)
2555
0
        *win32_attrib |= 0x400; /* FILE_ATTRIBUTE_REPARSE_POINT */
2556
    /* S_IFDIR */
2557
0
    else if ((posix_attrib & 0170000) == 0040000)
2558
0
        *win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
2559
    /* S_IFREG */
2560
0
    else
2561
0
        *win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
2562
2563
0
    return MZ_OK;
2564
0
}
2565
2566
1.66k
int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib) {
2567
1.66k
    if (!posix_attrib)
2568
0
        return MZ_PARAM_ERROR;
2569
2570
1.66k
    *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
2571
    /* FILE_ATTRIBUTE_READONLY */
2572
1.66k
    if ((win32_attrib & 0x01) == 0)
2573
1.66k
        *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
2574
    /* FILE_ATTRIBUTE_REPARSE_POINT */
2575
1.66k
    if ((win32_attrib & 0x400) == 0x400)
2576
0
        *posix_attrib |= 0120000; /* S_IFLNK */
2577
    /* FILE_ATTRIBUTE_DIRECTORY */
2578
1.66k
    else if ((win32_attrib & 0x10) == 0x10)
2579
0
        *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
2580
1.66k
    else
2581
1.66k
        *posix_attrib |= 0100000; /* S_IFREG */
2582
2583
1.66k
    return MZ_OK;
2584
1.66k
}
2585
2586
/***************************************************************************/
2587
2588
0
int32_t mz_zip_extrafield_find(void *stream, uint16_t type, int32_t max_seek, uint16_t *length) {
2589
0
    int32_t err = MZ_OK;
2590
0
    uint16_t field_type = 0;
2591
0
    uint16_t field_length = 0;
2592
2593
0
    if (max_seek < 4)
2594
0
        return MZ_EXIST_ERROR;
2595
2596
0
    do {
2597
0
        err = mz_stream_read_uint16(stream, &field_type);
2598
0
        if (err == MZ_OK)
2599
0
            err = mz_stream_read_uint16(stream, &field_length);
2600
0
        if (err != MZ_OK)
2601
0
            break;
2602
2603
0
        if (type == field_type) {
2604
0
            if (length)
2605
0
                *length = field_length;
2606
0
            return MZ_OK;
2607
0
        }
2608
2609
0
        max_seek -= field_length - 4;
2610
0
        if (max_seek < 0)
2611
0
            return MZ_EXIST_ERROR;
2612
2613
0
        err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR);
2614
0
    } while (err == MZ_OK);
2615
2616
0
    return MZ_EXIST_ERROR;
2617
0
}
2618
2619
int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size, uint16_t type,
2620
0
                                   uint16_t *length) {
2621
0
    void *file_extra_stream = NULL;
2622
0
    int32_t err = MZ_OK;
2623
2624
0
    if (!extrafield || !extrafield_size)
2625
0
        return MZ_PARAM_ERROR;
2626
2627
0
    file_extra_stream = mz_stream_mem_create();
2628
0
    if (!file_extra_stream)
2629
0
        return MZ_MEM_ERROR;
2630
2631
0
    mz_stream_mem_set_buffer(file_extra_stream, (void *)extrafield, extrafield_size);
2632
0
    err = mz_zip_extrafield_find(file_extra_stream, type, extrafield_size, length);
2633
0
    mz_stream_mem_delete(&file_extra_stream);
2634
0
    return err;
2635
0
}
2636
2637
0
int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length) {
2638
0
    int32_t err = MZ_OK;
2639
0
    if (!type || !length)
2640
0
        return MZ_PARAM_ERROR;
2641
0
    err = mz_stream_read_uint16(stream, type);
2642
0
    if (err == MZ_OK)
2643
0
        err = mz_stream_read_uint16(stream, length);
2644
0
    return err;
2645
0
}
2646
2647
350
int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length) {
2648
350
    int32_t err = MZ_OK;
2649
350
    err = mz_stream_write_uint16(stream, type);
2650
350
    if (err == MZ_OK)
2651
350
        err = mz_stream_write_uint16(stream, length);
2652
350
    return err;
2653
350
}
2654
2655
/***************************************************************************/
2656
2657
190
static int32_t mz_zip_invalid_date(const struct tm *ptm) {
2658
2.09k
#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
2659
190
    return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */
2660
190
            !datevalue_in_range(0, 11, ptm->tm_mon) || !datevalue_in_range(1, 31, ptm->tm_mday) ||
2661
190
            !datevalue_in_range(0, 23, ptm->tm_hour) || !datevalue_in_range(0, 59, ptm->tm_min) ||
2662
190
            !datevalue_in_range(0, 59, ptm->tm_sec));
2663
190
#undef datevalue_in_range
2664
190
}
2665
2666
0
static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm) {
2667
0
    uint64_t date = (uint64_t)(dos_date >> 16);
2668
2669
0
    ptm->tm_mday = (int16_t)(date & 0x1f);
2670
0
    ptm->tm_mon = (int16_t)(((date & 0x1E0) / 0x20) - 1);
2671
0
    ptm->tm_year = (int16_t)(((date & 0x0FE00) / 0x0200) + 80);
2672
0
    ptm->tm_hour = (int16_t)((dos_date & 0xF800) / 0x800);
2673
0
    ptm->tm_min = (int16_t)((dos_date & 0x7E0) / 0x20);
2674
0
    ptm->tm_sec = (int16_t)(2 * (dos_date & 0x1f));
2675
0
    ptm->tm_isdst = -1;
2676
0
}
2677
2678
0
int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm) {
2679
0
    if (!ptm)
2680
0
        return MZ_PARAM_ERROR;
2681
2682
0
    mz_zip_dosdate_to_raw_tm(dos_date, ptm);
2683
2684
0
    if (mz_zip_invalid_date(ptm)) {
2685
        /* Invalid date stored, so don't return it */
2686
0
        memset(ptm, 0, sizeof(struct tm));
2687
0
        return MZ_FORMAT_ERROR;
2688
0
    }
2689
0
    return MZ_OK;
2690
0
}
2691
2692
0
time_t mz_zip_dosdate_to_time_t(uint64_t dos_date) {
2693
0
    struct tm ptm;
2694
0
    mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
2695
0
    return mz_zip_tm_to_time_t(&ptm);
2696
0
}
2697
2698
0
time_t mz_zip_tm_to_time_t(struct tm *ptm) {
2699
0
    return mktime(ptm);
2700
0
}
2701
2702
190
int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm) {
2703
190
    struct tm ltm;
2704
190
    if (!ptm)
2705
0
        return MZ_PARAM_ERROR;
2706
190
    if (!localtime_r(&unix_time, &ltm)) { /* Returns a 1900-based year */
2707
        /* Invalid date stored, so don't return it */
2708
0
        memset(ptm, 0, sizeof(struct tm));
2709
0
        return MZ_INTERNAL_ERROR;
2710
0
    }
2711
190
    memcpy(ptm, &ltm, sizeof(struct tm));
2712
190
    return MZ_OK;
2713
190
}
2714
2715
190
uint32_t mz_zip_time_t_to_dos_date(time_t unix_time) {
2716
190
    struct tm ptm;
2717
190
    mz_zip_time_t_to_tm(unix_time, &ptm);
2718
190
    return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
2719
190
}
2720
2721
190
uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm) {
2722
190
    struct tm fixed_tm;
2723
2724
    /* Years supported: */
2725
2726
    /* [00, 79]      (assumed to be between 2000 and 2079) */
2727
    /* [80, 207]     (assumed to be between 1980 and 2107, typical output of old */
2728
    /*                software that does 'year-1900' to get a double digit year) */
2729
    /* [1980, 2107]  (due to format limitations, only years 1980-2107 can be stored.) */
2730
2731
190
    memcpy(&fixed_tm, ptm, sizeof(struct tm));
2732
190
    if (fixed_tm.tm_year >= 1980) /* range [1980, 2107] */
2733
0
        fixed_tm.tm_year -= 1980;
2734
190
    else if (fixed_tm.tm_year >= 80) /* range [80, 207] */
2735
0
        fixed_tm.tm_year -= 80;
2736
190
    else /* range [00, 79] */
2737
190
        fixed_tm.tm_year += 20;
2738
2739
190
    if (mz_zip_invalid_date(&fixed_tm))
2740
0
        return 0;
2741
2742
190
    return (((uint32_t)fixed_tm.tm_mday + (32 * ((uint32_t)fixed_tm.tm_mon + 1)) + (512 * (uint32_t)fixed_tm.tm_year))
2743
190
            << 16) |
2744
190
        (((uint32_t)fixed_tm.tm_sec / 2) + (32 * (uint32_t)fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
2745
190
}
2746
2747
0
int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time) {
2748
0
    *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
2749
0
    return MZ_OK;
2750
0
}
2751
2752
0
int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time) {
2753
0
    *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
2754
0
    return MZ_OK;
2755
0
}
2756
2757
/***************************************************************************/
2758
2759
0
int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case) {
2760
0
    do {
2761
0
        if ((*path1 == '\\' && *path2 == '/') || (*path2 == '\\' && *path1 == '/')) {
2762
            /* Ignore comparison of path slashes */
2763
0
        } else if (ignore_case) {
2764
0
            if (tolower(*path1) != tolower(*path2))
2765
0
                break;
2766
0
        } else if (*path1 != *path2) {
2767
0
            break;
2768
0
        }
2769
2770
0
        path1 += 1;
2771
0
        path2 += 1;
2772
0
    } while (*path1 != 0 && *path2 != 0);
2773
2774
0
    if (ignore_case)
2775
0
        return (int32_t)(tolower(*path1) - tolower(*path2));
2776
2777
0
    return (int32_t)(*path1 - *path2);
2778
0
}
2779
2780
/***************************************************************************/
2781
2782
0
const char *mz_zip_get_compression_method_string(int32_t compression_method) {
2783
0
    const char *method = "?";
2784
0
    switch (compression_method) {
2785
0
    case MZ_COMPRESS_METHOD_STORE:
2786
0
        method = "stored";
2787
0
        break;
2788
0
    case MZ_COMPRESS_METHOD_DEFLATE:
2789
0
        method = "deflate";
2790
0
        break;
2791
0
    case MZ_COMPRESS_METHOD_BZIP2:
2792
0
        method = "bzip2";
2793
0
        break;
2794
0
    case MZ_COMPRESS_METHOD_LZMA:
2795
0
        method = "lzma";
2796
0
        break;
2797
0
    case MZ_COMPRESS_METHOD_XZ:
2798
0
        method = "xz";
2799
0
        break;
2800
0
    case MZ_COMPRESS_METHOD_ZSTD:
2801
0
        method = "zstd";
2802
0
        break;
2803
0
    }
2804
0
    return method;
2805
0
}
2806
2807
/***************************************************************************/