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
219k
#define MZ_ZIP_MAGIC_LOCALHEADER        (0x04034b50)
60
792
#define MZ_ZIP_MAGIC_LOCALHEADERU8      {0x50, 0x4b, 0x03, 0x04}
61
771k
#define MZ_ZIP_MAGIC_CENTRALHEADER      (0x02014b50)
62
792
#define MZ_ZIP_MAGIC_CENTRALHEADERU8    {0x50, 0x4b, 0x01, 0x02}
63
1.59M
#define MZ_ZIP_MAGIC_ENDHEADER          (0x06054b50)
64
3.39k
#define MZ_ZIP_MAGIC_ENDHEADERU8        {0x50, 0x4b, 0x05, 0x06}
65
795k
#define MZ_ZIP_MAGIC_ENDHEADER64        (0x06064b50)
66
444
#define MZ_ZIP_MAGIC_ENDLOCHEADER64     (0x07064b50)
67
12.8k
#define MZ_ZIP_MAGIC_DATADESCRIPTOR     (0x08074b50)
68
792
#define MZ_ZIP_MAGIC_DATADESCRIPTORU8   {0x50, 0x4b, 0x07, 0x08}
69
70
0
#define MZ_ZIP_SIZE_LD_ITEM             (30)
71
569k
#define MZ_ZIP_SIZE_CD_ITEM             (46)
72
494
#define MZ_ZIP_SIZE_CD_LOCATOR64        (20)
73
76.4k
#define MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR (24)
74
75
0
#define MZ_ZIP_OFFSET_CRC_SIZES         (14)
76
0
#define MZ_ZIP_UNCOMPR_SIZE64_CUSHION   (2 * 1024 * 1024)
77
78
#ifndef MZ_ZIP_EOCD_MAX_BACK
79
3.39k
#  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
3.39k
static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos) {
132
3.39k
    int64_t file_size = 0;
133
3.39k
    int64_t max_back = MZ_ZIP_EOCD_MAX_BACK;
134
3.39k
    uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8;
135
3.39k
    int32_t err = MZ_OK;
136
137
3.39k
    err = mz_stream_seek(stream, 0, MZ_SEEK_END);
138
3.39k
    if (err != MZ_OK)
139
0
        return err;
140
141
3.39k
    file_size = mz_stream_tell(stream);
142
143
3.39k
    if (max_back <= 0 || max_back > file_size)
144
3.39k
        max_back = file_size;
145
146
3.39k
    return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos);
147
3.39k
}
148
149
/* Locate the end of central directory 64 of a zip file */
150
494
static int32_t mz_zip_search_zip64_eocd(void *stream, const int64_t end_central_offset, int64_t *central_pos) {
151
494
    int64_t offset = 0;
152
494
    uint32_t value32 = 0;
153
494
    int32_t err = MZ_OK;
154
155
494
    *central_pos = 0;
156
157
    /* Zip64 end of central directory locator */
158
494
    err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
159
    /* Read locator signature */
160
494
    if (err == MZ_OK) {
161
444
        err = mz_stream_read_uint32(stream, &value32);
162
444
        if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
163
66
            err = MZ_FORMAT_ERROR;
164
444
    }
165
    /* Number of the disk with the start of the zip64 end of  central directory */
166
494
    if (err == MZ_OK)
167
378
        err = mz_stream_read_uint32(stream, &value32);
168
    /* Relative offset of the zip64 end of central directory record8 */
169
494
    if (err == MZ_OK)
170
378
        err = mz_stream_read_uint64(stream, (uint64_t *)&offset);
171
    /* Total number of disks */
172
494
    if (err == MZ_OK)
173
378
        err = mz_stream_read_uint32(stream, &value32);
174
    /* Goto end of central directory record */
175
494
    if (err == MZ_OK)
176
378
        err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET);
177
    /* The signature */
178
494
    if (err == MZ_OK) {
179
312
        err = mz_stream_read_uint32(stream, &value32);
180
312
        if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
181
66
            err = MZ_FORMAT_ERROR;
182
312
    }
183
184
494
    if (err == MZ_OK)
185
246
        *central_pos = offset;
186
187
494
    return err;
188
494
}
189
190
#ifdef HAVE_PKCRYPT
191
/* Get PKWARE traditional encryption verifier */
192
125k
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
125k
    if (flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
196
60.9k
        return ((dos_date >> 16) & 0xff) << 8 | ((dos_date >> 8) & 0xff);
197
64.8k
    return ((crc >> 16) & 0xff) << 8 | ((crc >> 24) & 0xff);
198
125k
}
199
#endif
200
201
/* Get info about the current file in the zip file */
202
802k
static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file *file_info, void *file_extra_stream) {
203
802k
    uint64_t ntfs_time = 0;
204
802k
    uint32_t reserved = 0;
205
802k
    uint32_t magic = 0;
206
802k
    uint32_t dos_date = 0;
207
802k
    uint32_t field_pos = 0;
208
802k
    uint16_t field_type = 0;
209
802k
    uint16_t field_length = 0;
210
802k
    uint32_t field_length_read = 0;
211
802k
    uint16_t ntfs_attrib_id = 0;
212
802k
    uint16_t ntfs_attrib_size = 0;
213
802k
    uint16_t linkname_size;
214
802k
    uint16_t value16 = 0;
215
802k
    uint32_t value32 = 0;
216
802k
    int64_t extrafield_pos = 0;
217
802k
    int64_t comment_pos = 0;
218
802k
    int64_t linkname_pos = 0;
219
802k
    int64_t saved_pos = 0;
220
802k
    int32_t err = MZ_OK;
221
802k
    char *linkname = NULL;
222
223
802k
    memset(file_info, 0, sizeof(mz_zip_file));
224
225
    /* Check the magic */
226
802k
    err = mz_stream_read_uint32(stream, &magic);
227
802k
    if (err == MZ_END_OF_STREAM)
228
6.40k
        err = MZ_END_OF_LIST;
229
795k
    else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
230
115
        err = MZ_END_OF_LIST;
231
795k
    else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
232
215
        err = MZ_FORMAT_ERROR;
233
795k
    else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
234
2.42k
        err = MZ_FORMAT_ERROR;
235
236
    /* Read header fields */
237
802k
    if (err == MZ_OK) {
238
793k
        if (!local)
239
574k
            err = mz_stream_read_uint16(stream, &file_info->version_madeby);
240
793k
        if (err == MZ_OK)
241
793k
            err = mz_stream_read_uint16(stream, &file_info->version_needed);
242
793k
        if (err == MZ_OK)
243
792k
            err = mz_stream_read_uint16(stream, &file_info->flag);
244
793k
        if (err == MZ_OK)
245
792k
            err = mz_stream_read_uint16(stream, &file_info->compression_method);
246
793k
        if (err == MZ_OK) {
247
792k
            err = mz_stream_read_uint32(stream, &dos_date);
248
792k
            file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
249
792k
        }
250
793k
        if (err == MZ_OK)
251
792k
            err = mz_stream_read_uint32(stream, &file_info->crc);
252
793k
#ifdef HAVE_PKCRYPT
253
793k
        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
125k
            file_info->pk_verify = mz_zip_get_pk_verify(dos_date, file_info->crc, file_info->flag);
256
125k
        }
257
793k
#endif
258
793k
        if (err == MZ_OK) {
259
792k
            err = mz_stream_read_uint32(stream, &value32);
260
792k
            file_info->compressed_size = value32;
261
792k
        }
262
793k
        if (err == MZ_OK) {
263
792k
            err = mz_stream_read_uint32(stream, &value32);
264
792k
            file_info->uncompressed_size = value32;
265
792k
        }
266
793k
        if (err == MZ_OK)
267
792k
            err = mz_stream_read_uint16(stream, &file_info->filename_size);
268
793k
        if (err == MZ_OK)
269
792k
            err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
270
793k
        if (!local) {
271
574k
            if (err == MZ_OK)
272
573k
                err = mz_stream_read_uint16(stream, &file_info->comment_size);
273
574k
            if (err == MZ_OK) {
274
573k
                err = mz_stream_read_uint16(stream, &value16);
275
573k
                file_info->disk_number = value16;
276
573k
            }
277
574k
            if (err == MZ_OK)
278
573k
                err = mz_stream_read_uint16(stream, &file_info->internal_fa);
279
574k
            if (err == MZ_OK)
280
573k
                err = mz_stream_read_uint32(stream, &file_info->external_fa);
281
574k
            if (err == MZ_OK) {
282
573k
                err = mz_stream_read_uint32(stream, &value32);
283
573k
                file_info->disk_offset = value32;
284
573k
            }
285
574k
        }
286
793k
    }
287
288
802k
    if (err == MZ_OK)
289
792k
        err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET);
290
291
    /* Copy variable length data to memory stream for later retrieval */
292
802k
    if ((err == MZ_OK) && (file_info->filename_size > 0))
293
621k
        err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size);
294
802k
    mz_stream_write_uint8(file_extra_stream, 0);
295
802k
    extrafield_pos = mz_stream_tell(file_extra_stream);
296
297
802k
    if ((err == MZ_OK) && (file_info->extrafield_size > 0))
298
123k
        err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size);
299
802k
    mz_stream_write_uint8(file_extra_stream, 0);
300
301
802k
    comment_pos = mz_stream_tell(file_extra_stream);
302
802k
    if ((err == MZ_OK) && (file_info->comment_size > 0))
303
1.56k
        err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size);
304
802k
    mz_stream_write_uint8(file_extra_stream, 0);
305
306
802k
    linkname_pos = mz_stream_tell(file_extra_stream);
307
    /* Overwrite if we encounter UNIX1 extra block */
308
802k
    mz_stream_write_uint8(file_extra_stream, 0);
309
310
802k
    if ((err == MZ_OK) && (file_info->extrafield_size > 0)) {
311
        /* Seek to and parse the extra field */
312
122k
        err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET);
313
314
398k
        while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size)) {
315
276k
            err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length);
316
276k
            if (err != MZ_OK)
317
80
                break;
318
276k
            field_pos += 4;
319
320
            /* Don't allow field length to exceed size of remaining extrafield */
321
276k
            if (field_length > (file_info->extrafield_size - field_pos))
322
94.2k
                field_length = (uint16_t)(file_info->extrafield_size - field_pos);
323
324
            /* Read ZIP64 extra field */
325
276k
            if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8)) {
326
50.6k
                if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX)) {
327
14.4k
                    err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size);
328
14.4k
                    if (file_info->uncompressed_size < 0)
329
331
                        err = MZ_FORMAT_ERROR;
330
14.4k
                }
331
50.6k
                if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX)) {
332
45.4k
                    err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size);
333
45.4k
                    if (file_info->compressed_size < 0)
334
230
                        err = MZ_FORMAT_ERROR;
335
45.4k
                }
336
50.6k
                if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX)) {
337
1.25k
                    err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset);
338
1.25k
                    if (file_info->disk_offset < 0)
339
326
                        err = MZ_FORMAT_ERROR;
340
1.25k
                }
341
50.6k
                if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
342
585
                    err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number);
343
50.6k
            }
344
            /* Read NTFS extra field */
345
225k
            else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4)) {
346
29.3k
                if (err == MZ_OK)
347
29.3k
                    err = mz_stream_read_uint32(file_extra_stream, &reserved);
348
29.3k
                field_length_read = 4;
349
350
76.1k
                while ((err == MZ_OK) && (field_length_read + 4 <= field_length)) {
351
46.8k
                    err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id);
352
46.8k
                    if (err == MZ_OK)
353
46.8k
                        err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size);
354
46.8k
                    field_length_read += 4;
355
356
46.8k
                    if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24)) {
357
27.8k
                        err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
358
27.8k
                        mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
359
360
27.8k
                        if (err == MZ_OK) {
361
27.8k
                            err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
362
27.8k
                            mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
363
27.8k
                        }
364
27.8k
                        if (err == MZ_OK) {
365
27.7k
                            err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
366
27.7k
                            mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
367
27.7k
                        }
368
27.8k
                    } else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length)) {
369
17.1k
                        err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR);
370
17.1k
                    }
371
372
46.8k
                    field_length_read += ntfs_attrib_size;
373
46.8k
                }
374
29.3k
            }
375
            /* Read UNIX1 extra field */
376
196k
            else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12)) {
377
5.38k
                if (err == MZ_OK) {
378
5.38k
                    err = mz_stream_read_uint32(file_extra_stream, &value32);
379
5.38k
                    if (err == MZ_OK && file_info->accessed_date == 0)
380
2.81k
                        file_info->accessed_date = value32;
381
5.38k
                }
382
5.38k
                if (err == MZ_OK) {
383
5.37k
                    err = mz_stream_read_uint32(file_extra_stream, &value32);
384
5.37k
                    if (err == MZ_OK && file_info->modified_date == 0)
385
0
                        file_info->modified_date = value32;
386
5.37k
                }
387
5.38k
                if (err == MZ_OK)
388
5.35k
                    err = mz_stream_read_uint16(file_extra_stream, &value16); /* User id */
389
5.38k
                if (err == MZ_OK)
390
5.34k
                    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
5.38k
                linkname_size = field_length - 12;
395
5.38k
                if ((err == MZ_OK) && (linkname_size > 0)) {
396
4.45k
                    linkname = (char *)malloc(linkname_size);
397
4.45k
                    if (linkname) {
398
4.45k
                        if (mz_stream_read(file_extra_stream, linkname, linkname_size) != linkname_size)
399
56
                            err = MZ_READ_ERROR;
400
4.45k
                        if (err == MZ_OK) {
401
4.39k
                            saved_pos = mz_stream_tell(file_extra_stream);
402
403
4.39k
                            mz_stream_seek(file_extra_stream, linkname_pos, MZ_SEEK_SET);
404
4.39k
                            mz_stream_write(file_extra_stream, linkname, linkname_size);
405
4.39k
                            mz_stream_write_uint8(file_extra_stream, 0);
406
407
4.39k
                            mz_stream_seek(file_extra_stream, saved_pos, MZ_SEEK_SET);
408
4.39k
                        }
409
4.45k
                        free(linkname);
410
4.45k
                    }
411
4.45k
                }
412
5.38k
            }
413
191k
#ifdef HAVE_WZAES
414
            /* Read AES extra field */
415
191k
            else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7)) {
416
79.6k
                uint8_t value8 = 0;
417
                /* Verify version info */
418
79.6k
                err = mz_stream_read_uint16(file_extra_stream, &value16);
419
                /* Support AE-1 and AE-2 */
420
79.6k
                if (value16 != 1 && value16 != 2)
421
57
                    err = MZ_FORMAT_ERROR;
422
79.6k
                file_info->aes_version = value16;
423
79.6k
                if (err == MZ_OK)
424
79.6k
                    err = mz_stream_read_uint8(file_extra_stream, &value8);
425
79.6k
                if ((char)value8 != 'A')
426
145
                    err = MZ_FORMAT_ERROR;
427
79.6k
                if (err == MZ_OK)
428
79.5k
                    err = mz_stream_read_uint8(file_extra_stream, &value8);
429
79.6k
                if ((char)value8 != 'E')
430
161
                    err = MZ_FORMAT_ERROR;
431
                /* Get AES encryption strength and actual compression method */
432
79.6k
                if (err == MZ_OK) {
433
79.5k
                    err = mz_stream_read_uint8(file_extra_stream, &value8);
434
79.5k
                    file_info->aes_strength = value8;
435
79.5k
                }
436
79.6k
                if (err == MZ_OK) {
437
79.5k
                    err = mz_stream_read_uint16(file_extra_stream, &value16);
438
79.5k
                    file_info->compression_method = value16;
439
79.5k
                }
440
79.6k
            }
441
111k
#endif
442
111k
            else if (field_length > 0) {
443
18.0k
                err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
444
18.0k
            }
445
446
276k
            field_pos += field_length;
447
276k
        }
448
122k
    }
449
450
    /* Get pointers to variable length data */
451
802k
    mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename);
452
802k
    mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield);
453
802k
    mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment);
454
802k
    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
802k
    if (!file_info->filename)
458
0
        file_info->filename = "";
459
802k
    if (!file_info->extrafield)
460
0
        file_info->extrafield_size = 0;
461
802k
    if (!file_info->comment)
462
0
        file_info->comment = "";
463
802k
    if (!file_info->linkname)
464
0
        file_info->linkname = "";
465
466
802k
    if (err == MZ_OK) {
467
789k
        mz_zip_print("Zip - Entry - Read header - %s (local %" PRId8 ")\n", file_info->filename, local);
468
789k
        mz_zip_print("Zip - Entry - Read header compress (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
469
789k
                     file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
470
789k
        if (!local) {
471
571k
            mz_zip_print("Zip - Entry - Read header disk (disk %" PRIu32 " offset %" PRId64 ")\n",
472
571k
                         file_info->disk_number, file_info->disk_offset);
473
571k
        }
474
789k
        mz_zip_print("Zip - Entry - Read header variable (fnl %" PRId32 " efs %" PRId32 " cms %" PRId32 ")\n",
475
789k
                     file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
476
789k
    }
477
478
802k
    return err;
479
802k
}
480
481
static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size,
482
12.8k
                                            int64_t *uncompressed_size) {
483
12.8k
    uint32_t value32 = 0;
484
12.8k
    int64_t value64 = 0;
485
12.8k
    int32_t err = MZ_OK;
486
487
12.8k
    err = mz_stream_read_uint32(stream, &value32);
488
12.8k
    if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR)
489
251
        err = MZ_FORMAT_ERROR;
490
12.8k
    if (err == MZ_OK)
491
12.6k
        err = mz_stream_read_uint32(stream, &value32);
492
12.8k
    if (err == MZ_OK && crc32)
493
12.6k
        *crc32 = value32;
494
12.8k
    if (err == MZ_OK) {
495
        /* If zip 64 extension is enabled then read as 8 byte */
496
12.6k
        if (!zip64) {
497
4.13k
            err = mz_stream_read_uint32(stream, &value32);
498
4.13k
            value64 = value32;
499
8.47k
        } else {
500
8.47k
            err = mz_stream_read_int64(stream, &value64);
501
8.47k
            if (value64 < 0)
502
1.16k
                err = MZ_FORMAT_ERROR;
503
8.47k
        }
504
12.6k
        if (err == MZ_OK && compressed_size)
505
11.4k
            *compressed_size = value64;
506
12.6k
    }
507
12.8k
    if (err == MZ_OK) {
508
11.4k
        if (!zip64) {
509
4.13k
            err = mz_stream_read_uint32(stream, &value32);
510
4.13k
            value64 = value32;
511
7.30k
        } else {
512
7.30k
            err = mz_stream_read_int64(stream, &value64);
513
7.30k
            if (value64 < 0)
514
1.38k
                err = MZ_FORMAT_ERROR;
515
7.30k
        }
516
11.4k
        if (err == MZ_OK && uncompressed_size)
517
10.0k
            *uncompressed_size = value64;
518
11.4k
    }
519
520
12.8k
    return err;
521
12.8k
}
522
523
191k
static int32_t mz_zip_entry_write_crc_sizes(void *stream, uint8_t zip64, uint8_t mask, mz_zip_file *file_info) {
524
191k
    int32_t err = MZ_OK;
525
526
191k
    if (mask)
527
0
        err = mz_stream_write_uint32(stream, 0);
528
191k
    else
529
191k
        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
191k
    if (err == MZ_OK) {
535
191k
        if (zip64) /* compr size */
536
14.5k
            err = mz_stream_write_uint32(stream, UINT32_MAX);
537
177k
        else
538
177k
            err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
539
191k
    }
540
191k
    if (err == MZ_OK) {
541
191k
        if (mask) /* uncompr size */
542
0
            err = mz_stream_write_uint32(stream, 0);
543
191k
        else if (zip64)
544
14.5k
            err = mz_stream_write_uint32(stream, UINT32_MAX);
545
177k
        else
546
177k
            err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
547
191k
    }
548
191k
    return err;
549
191k
}
550
551
191k
static int32_t mz_zip_entry_needs_zip64(mz_zip_file *file_info, uint8_t local, uint8_t *zip64) {
552
191k
    uint32_t max_uncompressed_size = UINT32_MAX;
553
191k
    uint8_t needs_zip64 = 0;
554
555
191k
    if (!zip64)
556
0
        return MZ_PARAM_ERROR;
557
558
191k
    *zip64 = 0;
559
560
191k
    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
0
        max_uncompressed_size -= MZ_ZIP_UNCOMPR_SIZE64_CUSHION;
565
0
    }
566
567
191k
    needs_zip64 = (file_info->uncompressed_size >= max_uncompressed_size) || (file_info->compressed_size >= UINT32_MAX);
568
569
191k
    if (!local) {
570
        /* Disk offset and number only used in central directory header */
571
191k
        needs_zip64 |= (file_info->disk_offset >= UINT32_MAX) || (file_info->disk_number >= UINT16_MAX);
572
191k
    }
573
574
191k
    if (file_info->zip64 == MZ_ZIP64_AUTO) {
575
        /* If uncompressed size is unknown, assume zip64 for 64-bit data descriptors */
576
191k
        if (local && file_info->uncompressed_size == 0) {
577
            /* Don't use zip64 for local header directory entries */
578
0
            if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) != MZ_OK) {
579
0
                *zip64 = 1;
580
0
            }
581
0
        }
582
191k
        *zip64 |= needs_zip64;
583
191k
    } else if (file_info->zip64 == MZ_ZIP64_FORCE) {
584
0
        *zip64 = 1;
585
0
    } else if (file_info->zip64 == MZ_ZIP64_DISABLE) {
586
        /* Zip64 extension is required to zip file */
587
0
        if (needs_zip64)
588
0
            return MZ_PARAM_ERROR;
589
0
    }
590
591
191k
    return MZ_OK;
592
191k
}
593
594
191k
static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info) {
595
191k
    uint64_t ntfs_time = 0;
596
191k
    uint32_t reserved = 0;
597
191k
    uint32_t dos_date = 0;
598
191k
    uint16_t extrafield_size = 0;
599
191k
    uint16_t field_type = 0;
600
191k
    uint16_t field_length = 0;
601
191k
    uint16_t field_length_zip64 = 0;
602
191k
    uint16_t field_length_ntfs = 0;
603
191k
    uint16_t field_length_aes = 0;
604
191k
    uint16_t field_length_unix1 = 0;
605
191k
    uint16_t filename_size = 0;
606
191k
    uint16_t filename_length = 0;
607
191k
    uint16_t linkname_size = 0;
608
191k
    uint16_t version_needed = 0;
609
191k
    int32_t comment_size = 0;
610
191k
    int32_t err = MZ_OK;
611
191k
    int32_t err_mem = MZ_OK;
612
191k
    uint8_t zip64 = 0;
613
191k
    uint8_t skip_aes = 0;
614
191k
    uint8_t mask = 0;
615
191k
    uint8_t write_end_slash = 0;
616
191k
    const char *filename = NULL;
617
191k
    char masked_name[64];
618
191k
    void *file_extra_stream = NULL;
619
620
191k
    if (!file_info)
621
0
        return MZ_PARAM_ERROR;
622
623
191k
    if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO))
624
0
        mask = 1;
625
626
    /* Determine if zip64 extra field is necessary */
627
191k
    err = mz_zip_entry_needs_zip64(file_info, local, &zip64);
628
191k
    if (err != MZ_OK)
629
0
        return err;
630
631
    /* Start calculating extra field sizes */
632
191k
    if (zip64) {
633
        /* Both compressed and uncompressed sizes must be included (at least in local header) */
634
14.5k
        field_length_zip64 = 8 + 8;
635
14.5k
        if ((!local) && (file_info->disk_offset >= UINT32_MAX))
636
0
            field_length_zip64 += 8;
637
638
14.5k
        extrafield_size += 4;
639
14.5k
        extrafield_size += field_length_zip64;
640
14.5k
    }
641
642
    /* Calculate extra field size and check for duplicates */
643
191k
    if (file_info->extrafield_size > 0) {
644
17.6k
        file_extra_stream = mz_stream_mem_create();
645
17.6k
        if (!file_extra_stream)
646
0
            return MZ_MEM_ERROR;
647
17.6k
        mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield, file_info->extrafield_size);
648
649
44.8k
        do {
650
44.8k
            err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
651
44.8k
            if (err_mem == MZ_OK)
652
41.4k
                err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
653
44.8k
            if (err_mem != MZ_OK)
654
6.14k
                break;
655
656
            /* Prefer incoming aes extensions over ours */
657
38.6k
            if (field_type == MZ_ZIP_EXTENSION_AES)
658
3.15k
                skip_aes = 1;
659
660
            /* Prefer our zip64, ntfs, unix1 extension over incoming */
661
38.6k
            if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS &&
662
38.6k
                field_type != MZ_ZIP_EXTENSION_UNIX1)
663
23.3k
                extrafield_size += 4 + field_length;
664
665
38.6k
            if (err_mem == MZ_OK)
666
38.6k
                err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
667
38.6k
        } while (err_mem == MZ_OK);
668
17.6k
    }
669
670
191k
#ifdef HAVE_WZAES
671
191k
    if (!skip_aes) {
672
189k
        if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version)) {
673
334
            field_length_aes = 1 + 1 + 1 + 2 + 2;
674
334
            extrafield_size += 4 + field_length_aes;
675
334
        }
676
189k
    }
677
#else
678
    MZ_UNUSED(field_length_aes);
679
    MZ_UNUSED(skip_aes);
680
#endif
681
    /* NTFS timestamps */
682
191k
    if ((file_info->modified_date != 0) && (file_info->accessed_date != 0) && (file_info->creation_date != 0) &&
683
191k
        (!mask)) {
684
1.69k
        field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
685
1.69k
        extrafield_size += 4 + field_length_ntfs;
686
1.69k
    }
687
688
    /* Unix1 symbolic links */
689
191k
    if (file_info->linkname && *file_info->linkname != 0) {
690
484
        linkname_size = (uint16_t)strlen(file_info->linkname);
691
484
        field_length_unix1 = 12 + linkname_size;
692
484
        extrafield_size += 4 + field_length_unix1;
693
484
    }
694
695
191k
    if (local)
696
0
        err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
697
191k
    else {
698
191k
        err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
699
191k
        if (err == MZ_OK)
700
191k
            err = mz_stream_write_uint16(stream, file_info->version_madeby);
701
191k
    }
702
703
    /* Calculate version needed to extract */
704
191k
    if (err == MZ_OK) {
705
191k
        version_needed = file_info->version_needed;
706
191k
        if (version_needed == 0) {
707
11.1k
            version_needed = 20;
708
11.1k
            if (zip64)
709
7.30k
                version_needed = 45;
710
#ifdef HAVE_BZIP2
711
            if (file_info->compression_method == MZ_COMPRESS_METHOD_BZIP2)
712
                version_needed = 46;
713
#endif
714
11.1k
#ifdef HAVE_WZAES
715
11.1k
            if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
716
1.57k
                version_needed = 51;
717
11.1k
#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
11.1k
        }
724
191k
        err = mz_stream_write_uint16(stream, version_needed);
725
191k
    }
726
191k
    if (err == MZ_OK)
727
191k
        err = mz_stream_write_uint16(stream, file_info->flag);
728
191k
    if (err == MZ_OK) {
729
191k
#ifdef HAVE_WZAES
730
191k
        if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
731
1.92k
            err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
732
189k
        else
733
189k
#endif
734
189k
            err = mz_stream_write_uint16(stream, file_info->compression_method);
735
191k
    }
736
191k
    if (err == MZ_OK) {
737
191k
        if (file_info->modified_date != 0 && !mask)
738
191k
            dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
739
191k
        err = mz_stream_write_uint32(stream, dos_date);
740
191k
    }
741
742
191k
    if (err == MZ_OK)
743
191k
        err = mz_zip_entry_write_crc_sizes(stream, zip64, mask, file_info);
744
745
191k
    if (mask) {
746
0
        snprintf(masked_name, sizeof(masked_name), "%" PRIx32 "_%" PRIx64, file_info->disk_number,
747
0
                 file_info->disk_offset);
748
0
        filename = masked_name;
749
191k
    } else {
750
191k
        filename = file_info->filename;
751
191k
    }
752
753
191k
    filename_length = (uint16_t)strlen(filename);
754
191k
    filename_size += filename_length;
755
756
191k
    if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) &&
757
191k
        ((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\'))) {
758
0
        filename_size += 1;
759
0
        write_end_slash = 1;
760
0
    }
761
762
191k
    if (err == MZ_OK)
763
191k
        err = mz_stream_write_uint16(stream, filename_size);
764
191k
    if (err == MZ_OK)
765
191k
        err = mz_stream_write_uint16(stream, extrafield_size);
766
767
191k
    if (!local) {
768
191k
        if (file_info->comment) {
769
191k
            comment_size = (int32_t)strlen(file_info->comment);
770
191k
            if (comment_size > UINT16_MAX)
771
0
                comment_size = UINT16_MAX;
772
191k
        }
773
191k
        if (err == MZ_OK)
774
191k
            err = mz_stream_write_uint16(stream, (uint16_t)comment_size);
775
191k
        if (err == MZ_OK)
776
191k
            err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
777
191k
        if (err == MZ_OK)
778
191k
            err = mz_stream_write_uint16(stream, file_info->internal_fa);
779
191k
        if (err == MZ_OK)
780
191k
            err = mz_stream_write_uint32(stream, file_info->external_fa);
781
191k
        if (err == MZ_OK) {
782
191k
            if (file_info->disk_offset >= UINT32_MAX)
783
0
                err = mz_stream_write_uint32(stream, UINT32_MAX);
784
191k
            else
785
191k
                err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
786
191k
        }
787
191k
    }
788
789
191k
    if (err == MZ_OK) {
790
191k
        const char *next = filename;
791
191k
        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
191k
        if (err == MZ_OK && left > 0) {
807
166k
            if (mz_stream_write(stream, next, left) != left)
808
0
                err = MZ_WRITE_ERROR;
809
166k
        }
810
811
        /* Ensure that directories have a slash appended to them for compatibility */
812
191k
        if (err == MZ_OK && write_end_slash)
813
0
            err = mz_stream_write_uint8(stream, '/');
814
191k
    }
815
816
    /* Write ZIP64 extra field first so we can update sizes later if data descriptor not used */
817
191k
    if ((err == MZ_OK) && (zip64)) {
818
14.5k
        err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64);
819
14.5k
        if (err == MZ_OK) {
820
14.5k
            if (mask)
821
0
                err = mz_stream_write_int64(stream, 0);
822
14.5k
            else
823
14.5k
                err = mz_stream_write_int64(stream, file_info->uncompressed_size);
824
14.5k
        }
825
14.5k
        if (err == MZ_OK)
826
14.5k
            err = mz_stream_write_int64(stream, file_info->compressed_size);
827
14.5k
        if ((err == MZ_OK) && (!local) && (file_info->disk_offset >= UINT32_MAX))
828
0
            err = mz_stream_write_int64(stream, file_info->disk_offset);
829
14.5k
        if ((err == MZ_OK) && (!local) && (file_info->disk_number >= UINT16_MAX))
830
0
            err = mz_stream_write_uint32(stream, file_info->disk_number);
831
14.5k
    }
832
    /* Write NTFS extra field */
833
191k
    if ((err == MZ_OK) && (field_length_ntfs > 0)) {
834
1.69k
        err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs);
835
1.69k
        if (err == MZ_OK)
836
1.69k
            err = mz_stream_write_uint32(stream, reserved);
837
1.69k
        if (err == MZ_OK)
838
1.69k
            err = mz_stream_write_uint16(stream, 0x01);
839
1.69k
        if (err == MZ_OK)
840
1.69k
            err = mz_stream_write_uint16(stream, field_length_ntfs - 8);
841
1.69k
        if (err == MZ_OK) {
842
1.69k
            mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
843
1.69k
            err = mz_stream_write_uint64(stream, ntfs_time);
844
1.69k
        }
845
1.69k
        if (err == MZ_OK) {
846
1.69k
            mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
847
1.69k
            err = mz_stream_write_uint64(stream, ntfs_time);
848
1.69k
        }
849
1.69k
        if (err == MZ_OK) {
850
1.69k
            mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
851
1.69k
            err = mz_stream_write_uint64(stream, ntfs_time);
852
1.69k
        }
853
1.69k
    }
854
    /* Write UNIX extra block extra field */
855
191k
    if ((err == MZ_OK) && (field_length_unix1 > 0)) {
856
484
        err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_UNIX1, field_length_unix1);
857
484
        if (err == MZ_OK)
858
484
            err = mz_stream_write_uint32(stream, (uint32_t)file_info->accessed_date);
859
484
        if (err == MZ_OK)
860
484
            err = mz_stream_write_uint32(stream, (uint32_t)file_info->modified_date);
861
484
        if (err == MZ_OK) /* User id */
862
484
            err = mz_stream_write_uint16(stream, 0);
863
484
        if (err == MZ_OK) /* Group id */
864
484
            err = mz_stream_write_uint16(stream, 0);
865
484
        if (err == MZ_OK && linkname_size > 0) {
866
484
            if (mz_stream_write(stream, file_info->linkname, linkname_size) != linkname_size)
867
0
                err = MZ_WRITE_ERROR;
868
484
        }
869
484
    }
870
191k
#ifdef HAVE_WZAES
871
    /* Write AES extra field */
872
191k
    if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version)) {
873
334
        err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes);
874
334
        if (err == MZ_OK)
875
334
            err = mz_stream_write_uint16(stream, file_info->aes_version);
876
334
        if (err == MZ_OK)
877
334
            err = mz_stream_write_uint8(stream, 'A');
878
334
        if (err == MZ_OK)
879
334
            err = mz_stream_write_uint8(stream, 'E');
880
334
        if (err == MZ_OK)
881
334
            err = mz_stream_write_uint8(stream, file_info->aes_strength);
882
334
        if (err == MZ_OK)
883
334
            err = mz_stream_write_uint16(stream, file_info->compression_method);
884
334
    }
885
191k
#endif
886
887
191k
    if (file_info->extrafield_size > 0) {
888
17.6k
        err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET);
889
56.3k
        while (err == MZ_OK && err_mem == MZ_OK) {
890
44.8k
            err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
891
44.8k
            if (err_mem == MZ_OK)
892
41.4k
                err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
893
44.8k
            if (err_mem != MZ_OK)
894
6.14k
                break;
895
896
            /* Prefer our zip 64, ntfs, unix1 extensions over incoming */
897
38.6k
            if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS ||
898
38.6k
                field_type == MZ_ZIP_EXTENSION_UNIX1) {
899
15.3k
                err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
900
15.3k
                continue;
901
15.3k
            }
902
903
23.3k
            err = mz_stream_write_uint16(stream, field_type);
904
23.3k
            if (err == MZ_OK)
905
23.3k
                err = mz_stream_write_uint16(stream, field_length);
906
23.3k
            if (err == MZ_OK)
907
23.3k
                err = mz_stream_copy(stream, file_extra_stream, field_length);
908
23.3k
        }
909
910
17.6k
        mz_stream_mem_delete(&file_extra_stream);
911
17.6k
    }
912
913
191k
    if (err == MZ_OK && !local && file_info->comment) {
914
186k
        if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size)
915
0
            err = MZ_WRITE_ERROR;
916
186k
    }
917
918
191k
    return err;
919
191k
}
920
921
static int32_t mz_zip_entry_write_descriptor(void *stream, uint8_t zip64, uint32_t crc32, int64_t compressed_size,
922
0
                                             int64_t uncompressed_size) {
923
0
    int32_t err = MZ_OK;
924
925
0
    err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
926
0
    if (err == MZ_OK)
927
0
        err = mz_stream_write_uint32(stream, crc32);
928
929
    /* Store data descriptor as 8 bytes if zip 64 extension enabled */
930
0
    if (err == MZ_OK) {
931
        /* Zip 64 extension is enabled when uncompressed size is > UINT32_MAX */
932
0
        if (!zip64)
933
0
            err = mz_stream_write_uint32(stream, (uint32_t)compressed_size);
934
0
        else
935
0
            err = mz_stream_write_int64(stream, compressed_size);
936
0
    }
937
0
    if (err == MZ_OK) {
938
0
        if (!zip64)
939
0
            err = mz_stream_write_uint32(stream, (uint32_t)uncompressed_size);
940
0
        else
941
0
            err = mz_stream_write_int64(stream, uncompressed_size);
942
0
    }
943
944
0
    return err;
945
0
}
946
947
3.39k
static int32_t mz_zip_read_cd(void *handle) {
948
3.39k
    mz_zip *zip = (mz_zip *)handle;
949
3.39k
    uint64_t number_entry_cd64 = 0;
950
3.39k
    uint64_t number_entry_cd = 0;
951
3.39k
    int64_t eocd_pos = 0;
952
3.39k
    int64_t eocd_pos64 = 0;
953
3.39k
    int64_t value64i = 0;
954
3.39k
    uint16_t value16 = 0;
955
3.39k
    uint32_t value32 = 0;
956
3.39k
    uint64_t value64 = 0;
957
3.39k
    uint16_t comment_size = 0;
958
3.39k
    int32_t comment_read = 0;
959
3.39k
    int32_t err = MZ_OK;
960
961
3.39k
    if (!zip)
962
0
        return MZ_PARAM_ERROR;
963
964
    /* Read and cache central directory records */
965
3.39k
    err = mz_zip_search_eocd(zip->stream, &eocd_pos);
966
3.39k
    if (err == MZ_OK) {
967
        /* The signature, already checked */
968
3.00k
        err = mz_stream_read_uint32(zip->stream, &value32);
969
        /* Number of this disk */
970
3.00k
        if (err == MZ_OK)
971
3.00k
            err = mz_stream_read_uint16(zip->stream, &value16);
972
        /* Number of the disk with the start of the central directory */
973
3.00k
        if (err == MZ_OK)
974
2.98k
            err = mz_stream_read_uint16(zip->stream, &value16);
975
3.00k
        zip->disk_number_with_cd = value16;
976
        /* Total number of entries in the central dir on this disk */
977
3.00k
        if (err == MZ_OK)
978
2.98k
            err = mz_stream_read_uint16(zip->stream, &value16);
979
3.00k
        zip->number_entry = value16;
980
        /* Total number of entries in the central dir */
981
3.00k
        if (err == MZ_OK)
982
2.98k
            err = mz_stream_read_uint16(zip->stream, &value16);
983
3.00k
        number_entry_cd = value16;
984
        /* When recover is enabled, we can ignore incorrect number of entries */
985
3.00k
        if (number_entry_cd != zip->number_entry && !zip->recover)
986
14
            err = MZ_FORMAT_ERROR;
987
        /* Size of the central directory */
988
3.00k
        if (err == MZ_OK)
989
2.97k
            err = mz_stream_read_uint32(zip->stream, &value32);
990
3.00k
        if (err == MZ_OK)
991
2.96k
            zip->cd_size = value32;
992
        /* Offset of start of central directory with respect to the starting disk number */
993
3.00k
        if (err == MZ_OK)
994
2.96k
            err = mz_stream_read_uint32(zip->stream, &value32);
995
3.00k
        if (err == MZ_OK)
996
2.96k
            zip->cd_offset = value32;
997
        /* Zip file global comment length */
998
3.00k
        if (err == MZ_OK)
999
2.96k
            err = mz_stream_read_uint16(zip->stream, &comment_size);
1000
3.00k
        if ((err == MZ_OK) && (comment_size > 0)) {
1001
2.28k
            zip->comment = (char *)malloc(comment_size + 1);
1002
2.28k
            if (zip->comment) {
1003
2.28k
                comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
1004
                /* Don't fail if incorrect comment length read, not critical */
1005
2.28k
                if (comment_read < 0)
1006
0
                    comment_read = 0;
1007
2.28k
                zip->comment[comment_read] = 0;
1008
2.28k
            }
1009
2.28k
        }
1010
1011
3.00k
        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
494
            if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK) {
1014
246
                eocd_pos = eocd_pos64;
1015
1016
246
                err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
1017
                /* The signature, already checked */
1018
246
                if (err == MZ_OK)
1019
246
                    err = mz_stream_read_uint32(zip->stream, &value32);
1020
                /* Size of zip64 end of central directory record */
1021
246
                if (err == MZ_OK)
1022
246
                    err = mz_stream_read_uint64(zip->stream, &value64);
1023
                /* Version made by */
1024
246
                if (err == MZ_OK)
1025
245
                    err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
1026
                /* Version needed to extract */
1027
246
                if (err == MZ_OK)
1028
245
                    err = mz_stream_read_uint16(zip->stream, &value16);
1029
                /* Number of this disk */
1030
246
                if (err == MZ_OK)
1031
245
                    err = mz_stream_read_uint32(zip->stream, &value32);
1032
                /* Number of the disk with the start of the central directory */
1033
246
                if (err == MZ_OK)
1034
245
                    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
246
                if (err == MZ_OK)
1037
245
                    err = mz_stream_read_uint64(zip->stream, &zip->number_entry);
1038
                /* Total number of entries in the central directory */
1039
246
                if (err == MZ_OK)
1040
244
                    err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
1041
246
                if (zip->number_entry != number_entry_cd64)
1042
84
                    err = MZ_FORMAT_ERROR;
1043
                /* Size of the central directory */
1044
246
                if (err == MZ_OK) {
1045
161
                    err = mz_stream_read_int64(zip->stream, &zip->cd_size);
1046
161
                    if (zip->cd_size < 0)
1047
60
                        err = MZ_FORMAT_ERROR;
1048
161
                }
1049
                /* Offset of start of central directory with respect to the starting disk number */
1050
246
                if (err == MZ_OK) {
1051
100
                    err = mz_stream_read_int64(zip->stream, &zip->cd_offset);
1052
100
                    if (zip->cd_offset < 0)
1053
53
                        err = MZ_FORMAT_ERROR;
1054
100
                }
1055
248
            } else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
1056
248
                       (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)) {
1057
248
                err = MZ_FORMAT_ERROR;
1058
248
            }
1059
494
        }
1060
3.00k
    }
1061
1062
3.39k
    if (err == MZ_OK) {
1063
2.50k
        mz_zip_print("Zip - Read cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
1064
2.50k
                     zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1065
1066
        /* Verify central directory signature exists at offset */
1067
2.50k
        err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1068
2.50k
        if (err == MZ_OK)
1069
2.38k
            err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
1070
2.50k
        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
755
            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
755
            if (err == MZ_OK)
1076
755
                err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
1077
755
            if (err == MZ_OK)
1078
391
                err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
1079
755
            if ((err == MZ_OK) && (zip->cd_signature == MZ_ZIP_MAGIC_CENTRALHEADER)) {
1080
                /* If found compensate for incorrect locations */
1081
248
                value64i = zip->cd_offset;
1082
248
                zip->cd_offset = eocd_pos - zip->cd_size;
1083
                /* Assume disk has prepended data */
1084
248
                zip->disk_offset_shift = zip->cd_offset - value64i;
1085
248
            }
1086
755
        }
1087
2.50k
    }
1088
1089
3.39k
    if (err == MZ_OK) {
1090
2.02k
        if (eocd_pos < zip->cd_offset) {
1091
            /* End of central dir should always come after central dir */
1092
11
            err = MZ_FORMAT_ERROR;
1093
2.01k
        } 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
1.40k
            zip->cd_size = eocd_pos - zip->cd_offset;
1096
1.40k
        }
1097
2.02k
    }
1098
1099
3.39k
    return err;
1100
3.39k
}
1101
1102
0
static int32_t mz_zip_write_cd(void *handle) {
1103
0
    mz_zip *zip = (mz_zip *)handle;
1104
0
    int64_t zip64_eocd_pos_inzip = 0;
1105
0
    int64_t disk_number = 0;
1106
0
    int64_t disk_size = 0;
1107
0
    int32_t comment_size = 0;
1108
0
    int32_t err = MZ_OK;
1109
1110
0
    if (!zip)
1111
0
        return MZ_PARAM_ERROR;
1112
1113
0
    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
0
    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
0
    mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1118
0
    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
0
    zip->cd_offset = mz_stream_tell(zip->stream);
1124
0
    mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
1125
0
    zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
1126
0
    mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
1127
1128
0
    err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
1129
1130
0
    mz_zip_print("Zip - Write cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
1131
0
                 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1132
1133
0
    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
0
    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
0
    if (err == MZ_OK)
1189
0
        err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
1190
    /* Number of this disk */
1191
0
    if (err == MZ_OK)
1192
0
        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
0
    if (err == MZ_OK)
1195
0
        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
0
    if (err == MZ_OK) {
1198
0
        if (zip->number_entry >= UINT16_MAX)
1199
0
            err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1200
0
        else
1201
0
            err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1202
0
    }
1203
    /* Total number of entries in the central dir */
1204
0
    if (err == MZ_OK) {
1205
0
        if (zip->number_entry >= UINT16_MAX)
1206
0
            err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1207
0
        else
1208
0
            err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1209
0
    }
1210
    /* Size of the central directory */
1211
0
    if (err == MZ_OK)
1212
0
        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
0
    if (err == MZ_OK) {
1215
0
        if (zip->cd_offset >= UINT32_MAX)
1216
0
            err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
1217
0
        else
1218
0
            err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
1219
0
    }
1220
1221
    /* Write global comment */
1222
0
    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
0
    if (err == MZ_OK)
1228
0
        err = mz_stream_write_uint16(zip->stream, (uint16_t)comment_size);
1229
0
    if (err == MZ_OK) {
1230
0
        if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
1231
0
            err = MZ_READ_ERROR;
1232
0
    }
1233
0
    return err;
1234
0
}
1235
1236
792
static int32_t mz_zip_recover_cd(void *handle) {
1237
792
    mz_zip *zip = (mz_zip *)handle;
1238
792
    mz_zip_file local_file_info;
1239
792
    void *local_file_info_stream = NULL;
1240
792
    void *cd_mem_stream = NULL;
1241
792
    uint64_t number_entry = 0;
1242
792
    int64_t descriptor_pos = 0;
1243
792
    int64_t next_header_pos = 0;
1244
792
    int64_t disk_offset = 0;
1245
792
    int64_t disk_number = 0;
1246
792
    int64_t compressed_pos = 0;
1247
792
    int64_t compressed_end_pos = 0;
1248
792
    int64_t compressed_size = 0;
1249
792
    int64_t uncompressed_size = 0;
1250
792
    uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8;
1251
792
    uint8_t local_header_magic[4] = MZ_ZIP_MAGIC_LOCALHEADERU8;
1252
792
    uint8_t central_header_magic[4] = MZ_ZIP_MAGIC_CENTRALHEADERU8;
1253
792
    uint32_t crc32 = 0;
1254
792
    int32_t disk_number_with_cd = 0;
1255
792
    int32_t err = MZ_OK;
1256
792
    uint8_t zip64 = 0;
1257
792
    uint8_t eof = 0;
1258
1259
792
    mz_zip_print("Zip - Recover - Start\n");
1260
1261
792
    err = mz_zip_get_cd_mem_stream(zip, &cd_mem_stream);
1262
792
    if (err != MZ_OK)
1263
0
        return err;
1264
1265
    /* Determine if we are on a split disk or not */
1266
792
    mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0);
1267
792
    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
792
        disk_number_with_cd = 1;
1272
1273
792
    local_file_info_stream = mz_stream_mem_create();
1274
792
    if (!local_file_info_stream)
1275
0
        return MZ_MEM_ERROR;
1276
1277
792
    if (mz_stream_is_open(cd_mem_stream) != MZ_OK)
1278
792
        err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1279
1280
792
    mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1281
1282
792
    if (err == MZ_OK) {
1283
792
        err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic), INT64_MAX,
1284
792
                             &next_header_pos);
1285
792
    }
1286
1287
192k
    while (err == MZ_OK && !eof) {
1288
        /* Get current offset and disk number for central dir record */
1289
192k
        disk_offset = mz_stream_tell(zip->stream);
1290
192k
        mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1291
1292
        /* Read local headers */
1293
192k
        memset(&local_file_info, 0, sizeof(local_file_info));
1294
192k
        err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream);
1295
192k
        if (err != MZ_OK)
1296
335
            break;
1297
1298
191k
        local_file_info.disk_offset = disk_offset;
1299
191k
        if (disk_number < 0)
1300
0
            disk_number = 0;
1301
191k
        local_file_info.disk_number = (uint32_t)disk_number;
1302
1303
191k
        compressed_pos = mz_stream_tell(zip->stream);
1304
1305
191k
        if ((err == MZ_OK) && (local_file_info.compressed_size > 0)) {
1306
180k
            mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR);
1307
180k
        }
1308
1309
249k
        for (;;) {
1310
            /* Search for the next local header */
1311
249k
            err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic), INT64_MAX,
1312
249k
                                 &next_header_pos);
1313
1314
249k
            if (err == MZ_EXIST_ERROR) {
1315
435
                mz_stream_seek(zip->stream, compressed_pos, MZ_SEEK_SET);
1316
1317
                /* Search for central dir if no local header found */
1318
435
                err = mz_stream_find(zip->stream, (const void *)central_header_magic, sizeof(central_header_magic),
1319
435
                                     INT64_MAX, &next_header_pos);
1320
1321
435
                if (err == MZ_EXIST_ERROR) {
1322
                    /* Get end of stream if no central header found */
1323
368
                    mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1324
368
                    next_header_pos = mz_stream_tell(zip->stream);
1325
368
                }
1326
1327
435
                eof = 1;
1328
435
            }
1329
1330
249k
            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
76.4k
                err = mz_stream_find_reverse(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
1334
76.4k
                                             MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR, &descriptor_pos);
1335
76.4k
                if (err == MZ_OK) {
1336
12.8k
                    if (mz_zip_extrafield_contains(local_file_info.extrafield, local_file_info.extrafield_size,
1337
12.8k
                                                   MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
1338
1.63k
                        zip64 = 1;
1339
1340
12.8k
                    err =
1341
12.8k
                        mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32, &compressed_size, &uncompressed_size);
1342
1343
12.8k
                    if (err == MZ_OK) {
1344
10.0k
                        if (local_file_info.crc == 0)
1345
2.81k
                            local_file_info.crc = crc32;
1346
10.0k
                        if (local_file_info.compressed_size == 0)
1347
5.05k
                            local_file_info.compressed_size = compressed_size;
1348
10.0k
                        if (local_file_info.uncompressed_size == 0)
1349
3.15k
                            local_file_info.uncompressed_size = uncompressed_size;
1350
10.0k
                    }
1351
1352
12.8k
                    compressed_end_pos = descriptor_pos;
1353
63.6k
                } else if (eof) {
1354
175
                    compressed_end_pos = next_header_pos;
1355
63.4k
                } else if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) {
1356
                    /* Wrong local file entry found, keep searching */
1357
57.9k
                    next_header_pos += 1;
1358
57.9k
                    mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
1359
57.9k
                    continue;
1360
57.9k
                }
1361
173k
            } else {
1362
173k
                compressed_end_pos = next_header_pos;
1363
173k
            }
1364
1365
191k
            break;
1366
249k
        }
1367
1368
191k
        compressed_size = compressed_end_pos - compressed_pos;
1369
1370
191k
        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
191k
        mz_zip_print("Zip - Recover - Entry %s (csize %" PRId64 " usize %" PRId64 " flags 0x%" PRIx16 ")\n",
1379
191k
                     local_file_info.filename, local_file_info.compressed_size, local_file_info.uncompressed_size,
1380
191k
                     local_file_info.flag);
1381
1382
        /* Rewrite central dir with local headers and offsets */
1383
191k
        err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
1384
191k
        if (err == MZ_OK)
1385
186k
            number_entry += 1;
1386
1387
191k
        err = mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
1388
191k
    }
1389
1390
792
    mz_stream_mem_delete(&local_file_info_stream);
1391
1392
792
    mz_zip_print("Zip - Recover - Complete (cddisk %" PRId32 " entries %" PRId64 ")\n", disk_number_with_cd,
1393
792
                 number_entry);
1394
1395
792
    if (number_entry == 0)
1396
122
        return err;
1397
1398
    /* Set new upper seek boundary for central dir mem stream */
1399
670
    disk_offset = mz_stream_tell(cd_mem_stream);
1400
670
    mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset);
1401
1402
    /* Set new central directory info */
1403
670
    mz_zip_set_cd_stream(zip, 0, cd_mem_stream);
1404
670
    mz_zip_set_number_entry(zip, number_entry);
1405
670
    mz_zip_set_disk_number_with_cd(zip, disk_number_with_cd);
1406
1407
670
    return MZ_OK;
1408
792
}
1409
1410
3.39k
void *mz_zip_create(void) {
1411
3.39k
    mz_zip *zip = (mz_zip *)calloc(1, sizeof(mz_zip));
1412
3.39k
    if (zip)
1413
3.39k
        zip->data_descriptor = 1;
1414
3.39k
    return zip;
1415
3.39k
}
1416
1417
3.39k
void mz_zip_delete(void **handle) {
1418
3.39k
    mz_zip *zip = NULL;
1419
3.39k
    if (!handle)
1420
0
        return;
1421
3.39k
    zip = (mz_zip *)*handle;
1422
3.39k
    free(zip);
1423
3.39k
    *handle = NULL;
1424
3.39k
}
1425
1426
3.39k
int32_t mz_zip_open(void *handle, void *stream, int32_t mode) {
1427
3.39k
    mz_zip *zip = (mz_zip *)handle;
1428
3.39k
    int32_t err = MZ_OK;
1429
1430
3.39k
    if (!zip)
1431
0
        return MZ_PARAM_ERROR;
1432
1433
3.39k
    mz_zip_print("Zip - Open\n");
1434
1435
3.39k
    zip->stream = stream;
1436
3.39k
    zip->cd_mem_stream = mz_stream_mem_create();
1437
3.39k
    if (!zip->cd_mem_stream)
1438
0
        return MZ_MEM_ERROR;
1439
1440
3.39k
    if (mode & MZ_OPEN_MODE_WRITE) {
1441
0
        mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1442
0
        zip->cd_stream = zip->cd_mem_stream;
1443
3.39k
    } else {
1444
3.39k
        zip->cd_stream = stream;
1445
3.39k
    }
1446
1447
3.39k
    if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND)) {
1448
3.39k
        if ((mode & MZ_OPEN_MODE_CREATE) == 0) {
1449
3.39k
            err = mz_zip_read_cd(zip);
1450
3.39k
            if (err != MZ_OK) {
1451
1.38k
                mz_zip_print("Zip - Error detected reading cd (%" PRId32 ")\n", err);
1452
1.38k
                if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK)
1453
729
                    err = MZ_OK;
1454
1.38k
            }
1455
3.39k
        }
1456
1457
3.39k
        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
3.39k
        } else {
1480
3.39k
            zip->cd_start_pos = zip->cd_offset;
1481
3.39k
        }
1482
3.39k
    }
1483
1484
3.39k
    if (err != MZ_OK) {
1485
658
        mz_zip_close(zip);
1486
658
        return err;
1487
658
    }
1488
1489
    /* Memory streams used to store variable length file info data */
1490
2.74k
    zip->file_info_stream = mz_stream_mem_create();
1491
2.74k
    if (!zip->file_info_stream)
1492
0
        return MZ_MEM_ERROR;
1493
1494
2.74k
    mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1495
1496
2.74k
    zip->local_file_info_stream = mz_stream_mem_create();
1497
2.74k
    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
2.74k
    mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1503
1504
2.74k
    zip->open_mode = mode;
1505
1506
2.74k
    return err;
1507
2.74k
}
1508
1509
3.39k
int32_t mz_zip_close(void *handle) {
1510
3.39k
    mz_zip *zip = (mz_zip *)handle;
1511
3.39k
    int32_t err = MZ_OK;
1512
1513
3.39k
    if (!zip)
1514
0
        return MZ_PARAM_ERROR;
1515
1516
3.39k
    mz_zip_print("Zip - Close\n");
1517
1518
3.39k
    if (mz_zip_entry_is_open(zip) == MZ_OK)
1519
0
        err = mz_zip_entry_close(zip);
1520
1521
3.39k
    if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE))
1522
0
        err = mz_zip_write_cd(zip);
1523
1524
3.39k
    if (zip->cd_mem_stream) {
1525
3.39k
        mz_stream_close(zip->cd_mem_stream);
1526
3.39k
        mz_stream_delete(&zip->cd_mem_stream);
1527
3.39k
    }
1528
1529
3.39k
    if (zip->file_info_stream) {
1530
2.74k
        mz_stream_mem_close(zip->file_info_stream);
1531
2.74k
        mz_stream_mem_delete(&zip->file_info_stream);
1532
2.74k
    }
1533
3.39k
    if (zip->local_file_info_stream) {
1534
2.74k
        mz_stream_mem_close(zip->local_file_info_stream);
1535
2.74k
        mz_stream_mem_delete(&zip->local_file_info_stream);
1536
2.74k
    }
1537
1538
3.39k
    if (zip->comment) {
1539
2.28k
        free(zip->comment);
1540
2.28k
        zip->comment = NULL;
1541
2.28k
    }
1542
1543
3.39k
    zip->stream = NULL;
1544
3.39k
    zip->cd_stream = NULL;
1545
1546
3.39k
    return err;
1547
3.39k
}
1548
1549
2.74k
int32_t mz_zip_get_comment(void *handle, const char **comment) {
1550
2.74k
    mz_zip *zip = (mz_zip *)handle;
1551
2.74k
    if (!zip || !comment)
1552
0
        return MZ_PARAM_ERROR;
1553
2.74k
    if (!zip->comment)
1554
968
        return MZ_EXIST_ERROR;
1555
1.77k
    *comment = zip->comment;
1556
1.77k
    return MZ_OK;
1557
2.74k
}
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
2.74k
int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby) {
1576
2.74k
    mz_zip *zip = (mz_zip *)handle;
1577
2.74k
    if (!zip || !version_madeby)
1578
0
        return MZ_PARAM_ERROR;
1579
2.74k
    *version_madeby = zip->version_madeby;
1580
2.74k
    return MZ_OK;
1581
2.74k
}
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
3.39k
int32_t mz_zip_set_recover(void *handle, uint8_t recover) {
1592
3.39k
    mz_zip *zip = (mz_zip *)handle;
1593
3.39k
    if (!zip)
1594
0
        return MZ_PARAM_ERROR;
1595
3.39k
    zip->recover = recover;
1596
3.39k
    return MZ_OK;
1597
3.39k
}
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
670
int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream) {
1618
670
    mz_zip *zip = (mz_zip *)handle;
1619
670
    if (!zip || !cd_stream)
1620
0
        return MZ_PARAM_ERROR;
1621
670
    zip->cd_offset = 0;
1622
670
    zip->cd_stream = cd_stream;
1623
670
    zip->cd_start_pos = cd_start_pos;
1624
670
    return MZ_OK;
1625
670
}
1626
1627
792
int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream) {
1628
792
    mz_zip *zip = (mz_zip *)handle;
1629
792
    if (!zip || !cd_mem_stream)
1630
0
        return MZ_PARAM_ERROR;
1631
792
    *cd_mem_stream = zip->cd_mem_stream;
1632
792
    if (!*cd_mem_stream)
1633
0
        return MZ_EXIST_ERROR;
1634
792
    return MZ_OK;
1635
792
}
1636
1637
670
int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry) {
1638
670
    mz_zip *zip = (mz_zip *)handle;
1639
670
    if (!zip)
1640
0
        return MZ_PARAM_ERROR;
1641
670
    zip->number_entry = number_entry;
1642
670
    return MZ_OK;
1643
670
}
1644
1645
2.74k
int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry) {
1646
2.74k
    mz_zip *zip = (mz_zip *)handle;
1647
2.74k
    if (!zip || !number_entry)
1648
0
        return MZ_PARAM_ERROR;
1649
2.74k
    *number_entry = zip->number_entry;
1650
2.74k
    return MZ_OK;
1651
2.74k
}
1652
1653
670
int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd) {
1654
670
    mz_zip *zip = (mz_zip *)handle;
1655
670
    if (!zip)
1656
0
        return MZ_PARAM_ERROR;
1657
670
    zip->disk_number_with_cd = disk_number_with_cd;
1658
670
    return MZ_OK;
1659
670
}
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
26.3k
static int32_t mz_zip_entry_close_int(void *handle) {
1670
26.3k
    mz_zip *zip = (mz_zip *)handle;
1671
1672
26.3k
    if (zip->crypt_stream)
1673
26.3k
        mz_stream_delete(&zip->crypt_stream);
1674
26.3k
    zip->crypt_stream = NULL;
1675
26.3k
    if (zip->compress_stream)
1676
26.1k
        mz_stream_delete(&zip->compress_stream);
1677
26.3k
    zip->compress_stream = NULL;
1678
1679
26.3k
    zip->entry_opened = 0;
1680
1681
26.3k
    return MZ_OK;
1682
26.3k
}
1683
1684
26.3k
static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress_level, const char *password) {
1685
26.3k
    mz_zip *zip = (mz_zip *)handle;
1686
26.3k
    int64_t max_total_in = 0;
1687
26.3k
    int64_t header_size = 0;
1688
26.3k
    int64_t footer_size = 0;
1689
26.3k
    int32_t err = MZ_OK;
1690
26.3k
    uint8_t use_crypt = 0;
1691
1692
26.3k
    if (!zip)
1693
0
        return MZ_PARAM_ERROR;
1694
1695
26.3k
    switch (zip->file_info.compression_method) {
1696
26.3k
    case MZ_COMPRESS_METHOD_STORE:
1697
26.3k
    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
26.3k
        err = MZ_OK;
1711
26.3k
        break;
1712
0
    default:
1713
0
        return MZ_SUPPORT_ERROR;
1714
26.3k
    }
1715
1716
#ifndef HAVE_WZAES
1717
    if (zip->file_info.aes_version)
1718
        return MZ_SUPPORT_ERROR;
1719
#endif
1720
1721
26.3k
    zip->entry_raw = raw;
1722
1723
26.3k
    if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password)) {
1724
16.0k
        if (zip->open_mode & MZ_OPEN_MODE_WRITE) {
1725
            /* Encrypt only when we are not trying to write raw and password is supplied. */
1726
0
            if (!zip->entry_raw)
1727
0
                use_crypt = 1;
1728
16.0k
        } 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
16.0k
            use_crypt = 1;
1732
16.0k
        }
1733
16.0k
    }
1734
1735
26.3k
    if ((err == MZ_OK) && (use_crypt)) {
1736
16.0k
#ifdef HAVE_WZAES
1737
16.0k
        if (zip->file_info.aes_version) {
1738
13.3k
            zip->crypt_stream = mz_stream_wzaes_create();
1739
13.3k
            if (!zip->crypt_stream)
1740
0
                return MZ_MEM_ERROR;
1741
13.3k
            mz_stream_wzaes_set_password(zip->crypt_stream, password);
1742
13.3k
            mz_stream_wzaes_set_strength(zip->crypt_stream, zip->file_info.aes_strength);
1743
13.3k
        } else
1744
2.73k
#endif
1745
2.73k
        {
1746
2.73k
#ifdef HAVE_PKCRYPT
1747
2.73k
            uint8_t verify1 = (uint8_t)((zip->file_info.pk_verify >> 8) & 0xff);
1748
2.73k
            uint8_t verify2 = (uint8_t)((zip->file_info.pk_verify) & 0xff);
1749
1750
2.73k
            zip->crypt_stream = mz_stream_pkcrypt_create();
1751
2.73k
            if (!zip->crypt_stream)
1752
0
                return MZ_MEM_ERROR;
1753
2.73k
            mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1754
2.73k
            mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2, zip->file_info.version_needed);
1755
2.73k
#endif
1756
2.73k
        }
1757
16.0k
    }
1758
1759
26.3k
    if (err == MZ_OK) {
1760
26.3k
        if (!zip->crypt_stream)
1761
10.2k
            zip->crypt_stream = mz_stream_raw_create();
1762
26.3k
        if (!zip->crypt_stream)
1763
0
            return MZ_MEM_ERROR;
1764
1765
26.3k
        mz_stream_set_base(zip->crypt_stream, zip->stream);
1766
1767
26.3k
        err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
1768
26.3k
    }
1769
1770
26.3k
    if (err == MZ_OK) {
1771
26.1k
        if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
1772
26.1k
            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
26.1k
    }
1808
1809
26.3k
    if (err == MZ_OK && !zip->compress_stream)
1810
0
        err = MZ_MEM_ERROR;
1811
1812
26.3k
    if (err == MZ_OK) {
1813
26.1k
        if (zip->open_mode & MZ_OPEN_MODE_WRITE) {
1814
0
            mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
1815
26.1k
        } else {
1816
26.1k
            int32_t set_end_of_stream = 0;
1817
1818
26.1k
#ifndef HAVE_LIBCOMP
1819
26.1k
            if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE ||
1820
26.1k
                zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
1821
26.1k
#endif
1822
26.1k
            {
1823
26.1k
                max_total_in = zip->file_info.compressed_size;
1824
26.1k
                mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1825
1826
26.1k
                if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1827
15.8k
                    max_total_in -= header_size;
1828
26.1k
                if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1829
15.8k
                    max_total_in -= footer_size;
1830
1831
26.1k
                mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1832
26.1k
            }
1833
1834
26.1k
            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
26.1k
            }
1843
1844
26.1k
            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
26.1k
        }
1851
1852
26.1k
        mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1853
1854
26.1k
        err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1855
26.1k
    }
1856
1857
26.3k
    if (err == MZ_OK) {
1858
26.1k
        zip->entry_opened = 1;
1859
26.1k
        zip->entry_crc32 = 0;
1860
26.1k
    } else {
1861
194
        mz_zip_entry_close_int(zip);
1862
194
    }
1863
1864
26.3k
    return err;
1865
26.3k
}
1866
1867
110k
int32_t mz_zip_entry_is_open(void *handle) {
1868
110k
    mz_zip *zip = (mz_zip *)handle;
1869
110k
    if (!zip)
1870
0
        return MZ_PARAM_ERROR;
1871
110k
    if (zip->entry_opened == 0)
1872
6.13k
        return MZ_EXIST_ERROR;
1873
104k
    return MZ_OK;
1874
110k
}
1875
1876
27.3k
int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password) {
1877
27.3k
    mz_zip *zip = (mz_zip *)handle;
1878
27.3k
    int32_t err = MZ_OK;
1879
27.3k
    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
27.3k
    if (!zip || !zip->entry_scanned)
1886
0
        return MZ_PARAM_ERROR;
1887
27.3k
    if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
1888
0
        return MZ_PARAM_ERROR;
1889
1890
27.3k
    mz_zip_print("Zip - Entry - Read open (raw %" PRId32 ")\n", raw);
1891
1892
27.3k
    err = mz_zip_entry_seek_local_header(zip);
1893
27.3k
    if (err == MZ_OK)
1894
26.9k
        err = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
1895
1896
27.3k
    if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0) {
1897
        /* Perhaps we didn't compensated correctly for incorrect cd offset */
1898
111
        err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
1899
111
        if (err_shift == MZ_OK)
1900
87
            err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
1901
111
        if (err_shift == MZ_OK) {
1902
65
            zip->disk_offset_shift = 0;
1903
65
            err = err_shift;
1904
65
        }
1905
111
    }
1906
1907
27.3k
#ifdef MZ_ZIP_NO_DECOMPRESSION
1908
27.3k
    if (!raw && zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
1909
846
        err = MZ_SUPPORT_ERROR;
1910
27.3k
#endif
1911
27.3k
    if (err == MZ_OK)
1912
26.3k
        err = mz_zip_entry_open_int(zip, raw, 0, password);
1913
1914
27.3k
    return err;
1915
27.3k
}
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
0
                                const char *password) {
1919
0
    mz_zip *zip = (mz_zip *)handle;
1920
0
    int64_t filename_pos = -1;
1921
0
    int64_t extrafield_pos = 0;
1922
0
    int64_t comment_pos = 0;
1923
0
    int64_t linkname_pos = 0;
1924
0
    int64_t disk_number = 0;
1925
0
    uint8_t is_dir = 0;
1926
0
    int32_t err = MZ_OK;
1927
1928
#if defined(MZ_ZIP_NO_ENCRYPTION)
1929
    if (password)
1930
        return MZ_SUPPORT_ERROR;
1931
#endif
1932
0
    if (!zip || !file_info || !file_info->filename)
1933
0
        return MZ_PARAM_ERROR;
1934
1935
0
    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
0
    memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
1942
1943
0
    mz_zip_print("Zip - Entry - Write open - %s (level %" PRId16 " raw %" PRId8 ")\n", zip->file_info.filename,
1944
0
                 compress_level, raw);
1945
1946
0
    mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
1947
0
    mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
1948
1949
    /* Copy filename, extrafield, and comment internally */
1950
0
    filename_pos = mz_stream_tell(zip->file_info_stream);
1951
0
    if (file_info->filename)
1952
0
        mz_stream_write(zip->file_info_stream, file_info->filename, (int32_t)strlen(file_info->filename));
1953
0
    mz_stream_write_uint8(zip->file_info_stream, 0);
1954
1955
0
    extrafield_pos = mz_stream_tell(zip->file_info_stream);
1956
0
    if (file_info->extrafield)
1957
0
        mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
1958
0
    mz_stream_write_uint8(zip->file_info_stream, 0);
1959
1960
0
    comment_pos = mz_stream_tell(zip->file_info_stream);
1961
0
    if (file_info->comment)
1962
0
        mz_stream_write(zip->file_info_stream, file_info->comment, file_info->comment_size);
1963
0
    mz_stream_write_uint8(zip->file_info_stream, 0);
1964
1965
0
    linkname_pos = mz_stream_tell(zip->file_info_stream);
1966
0
    if (file_info->linkname)
1967
0
        mz_stream_write(zip->file_info_stream, file_info->linkname, (int32_t)strlen(file_info->linkname));
1968
0
    mz_stream_write_uint8(zip->file_info_stream, 0);
1969
1970
0
    mz_stream_mem_get_buffer_at(zip->file_info_stream, filename_pos, (const void **)&zip->file_info.filename);
1971
0
    mz_stream_mem_get_buffer_at(zip->file_info_stream, extrafield_pos, (const void **)&zip->file_info.extrafield);
1972
0
    mz_stream_mem_get_buffer_at(zip->file_info_stream, comment_pos, (const void **)&zip->file_info.comment);
1973
0
    mz_stream_mem_get_buffer_at(zip->file_info_stream, linkname_pos, (const void **)&zip->file_info.linkname);
1974
1975
0
    if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE) {
1976
0
        if ((compress_level == 8) || (compress_level == 9))
1977
0
            zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
1978
0
        if (compress_level == 2)
1979
0
            zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
1980
0
        if (compress_level == 1)
1981
0
            zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1982
0
    }
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
0
    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
0
    if (!raw && !is_dir) {
1993
0
        if (zip->data_descriptor)
1994
0
            zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
1995
0
        if (password)
1996
0
            zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
1997
0
    }
1998
1999
0
    mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
2000
0
    zip->file_info.disk_number = (uint32_t)disk_number;
2001
0
    zip->file_info.disk_offset = mz_stream_tell(zip->stream);
2002
2003
0
    if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) {
2004
0
#ifdef HAVE_PKCRYPT
2005
        /* Pre-calculated CRC value is required for PKWARE traditional encryption */
2006
0
        uint32_t dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
2007
0
        zip->file_info.pk_verify = mz_zip_get_pk_verify(dos_date, zip->file_info.crc, zip->file_info.flag);
2008
0
#endif
2009
0
#ifdef HAVE_WZAES
2010
0
        if (zip->file_info.aes_version && zip->file_info.aes_strength == 0)
2011
0
            zip->file_info.aes_strength = MZ_AES_STRENGTH_256;
2012
0
#endif
2013
0
    }
2014
2015
0
    zip->file_info.crc = 0;
2016
0
    zip->file_info.compressed_size = 0;
2017
2018
0
    if ((compress_level == 0) || (is_dir))
2019
0
        zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
2020
2021
0
#ifdef MZ_ZIP_NO_COMPRESSION
2022
0
    if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
2023
0
        err = MZ_SUPPORT_ERROR;
2024
0
#endif
2025
0
    if (err == MZ_OK)
2026
0
        err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
2027
0
    if (err == MZ_OK)
2028
0
        err = mz_zip_entry_open_int(zip, raw, compress_level, password);
2029
2030
0
    return err;
2031
0
}
2032
2033
26.1k
int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len) {
2034
26.1k
    mz_zip *zip = (mz_zip *)handle;
2035
26.1k
    int32_t read = 0;
2036
2037
26.1k
    if (!zip || mz_zip_entry_is_open(zip) != MZ_OK)
2038
0
        return MZ_PARAM_ERROR;
2039
26.1k
    if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) /* zlib limitation */
2040
0
        return MZ_PARAM_ERROR;
2041
26.1k
    if (len == 0)
2042
0
        return MZ_PARAM_ERROR;
2043
2044
26.1k
    if (zip->file_info.compressed_size == 0)
2045
1.58k
        return 0;
2046
2047
    /* Read entire entry even if uncompressed_size = 0, otherwise */
2048
    /* aes encryption validation will fail if compressed_size > 0 */
2049
24.5k
    read = mz_stream_read(zip->compress_stream, buf, len);
2050
24.5k
    if (read > 0)
2051
17.8k
        zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read);
2052
2053
24.5k
    mz_zip_print("Zip - Entry - Read - %" PRId32 " (max %" PRId32 ")\n", read, len);
2054
2055
24.5k
    return read;
2056
26.1k
}
2057
2058
0
int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len) {
2059
0
    mz_zip *zip = (mz_zip *)handle;
2060
0
    int32_t written = 0;
2061
2062
0
    if (!zip || mz_zip_entry_is_open(zip) != MZ_OK)
2063
0
        return MZ_PARAM_ERROR;
2064
0
    written = mz_stream_write(zip->compress_stream, buf, len);
2065
0
    if (written > 0)
2066
0
        zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
2067
2068
0
    mz_zip_print("Zip - Entry - Write - %" PRId32 " (max %" PRId32 ")\n", written, len);
2069
0
    return written;
2070
0
}
2071
2072
26.1k
int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size) {
2073
26.1k
    mz_zip *zip = (mz_zip *)handle;
2074
26.1k
    int64_t total_in = 0;
2075
26.1k
    int32_t err = MZ_OK;
2076
26.1k
    uint8_t zip64 = 0;
2077
2078
26.1k
    if (!zip || mz_zip_entry_is_open(zip) != MZ_OK)
2079
0
        return MZ_PARAM_ERROR;
2080
2081
26.1k
    mz_stream_close(zip->compress_stream);
2082
2083
26.1k
    mz_zip_print("Zip - Entry - Read Close\n");
2084
2085
26.1k
    if (crc32)
2086
0
        *crc32 = zip->file_info.crc;
2087
26.1k
    if (compressed_size)
2088
0
        *compressed_size = zip->file_info.compressed_size;
2089
26.1k
    if (uncompressed_size)
2090
0
        *uncompressed_size = zip->file_info.uncompressed_size;
2091
2092
26.1k
    mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
2093
2094
26.1k
    if ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) &&
2095
26.1k
        ((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
26.1k
    if ((err == MZ_OK) && (total_in == zip->file_info.compressed_size) && (!zip->entry_raw)) {
2119
7.74k
#ifdef HAVE_WZAES
2120
        /* AES zip version AE-1 will expect a valid crc as well */
2121
7.74k
        if (zip->file_info.aes_version <= 0x0001)
2122
7.45k
#endif
2123
7.45k
        {
2124
7.45k
            if (zip->entry_crc32 != zip->file_info.crc) {
2125
162
                mz_zip_print("Zip - Entry - Crc failed (actual 0x%08" PRIx32 " expected 0x%08" PRIx32 ")\n",
2126
162
                             zip->entry_crc32, zip->file_info.crc);
2127
2128
162
                err = MZ_CRC_ERROR;
2129
162
            }
2130
7.45k
        }
2131
7.74k
    }
2132
2133
26.1k
    mz_zip_entry_close_int(zip);
2134
2135
26.1k
    return err;
2136
26.1k
}
2137
2138
0
int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size) {
2139
0
    mz_zip *zip = (mz_zip *)handle;
2140
0
    int64_t end_disk_number = 0;
2141
0
    int32_t err = MZ_OK;
2142
0
    uint8_t zip64 = 0;
2143
2144
0
    if (!zip || mz_zip_entry_is_open(zip) != MZ_OK)
2145
0
        return MZ_PARAM_ERROR;
2146
2147
0
    mz_stream_close(zip->compress_stream);
2148
2149
0
    if (!zip->entry_raw)
2150
0
        crc32 = zip->entry_crc32;
2151
2152
0
    mz_zip_print("Zip - Entry - Write Close (crc 0x%08" PRIx32 " cs %" PRId64 " ucs %" PRId64 ")\n", crc32,
2153
0
                 compressed_size, uncompressed_size);
2154
2155
    /* If sizes are not set, then read them from the compression stream */
2156
0
    if (compressed_size < 0)
2157
0
        mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2158
0
    if (uncompressed_size < 0)
2159
0
        mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size);
2160
2161
0
    if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) {
2162
0
        mz_stream_set_base(zip->crypt_stream, zip->stream);
2163
0
        err = mz_stream_close(zip->crypt_stream);
2164
2165
0
        mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2166
0
    }
2167
2168
0
    mz_zip_entry_needs_zip64(&zip->file_info, 1, &zip64);
2169
2170
0
    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
0
        if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
2175
0
            err = mz_zip_entry_write_descriptor(zip->stream, zip64, 0, compressed_size, 0);
2176
0
        else
2177
0
            err = mz_zip_entry_write_descriptor(zip->stream, zip64, crc32, compressed_size, uncompressed_size);
2178
0
    }
2179
2180
    /* Write file info to central directory */
2181
2182
0
    mz_zip_print("Zip - Entry - Write cd (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n", uncompressed_size,
2183
0
                 compressed_size, crc32);
2184
2185
0
    zip->file_info.crc = crc32;
2186
0
    zip->file_info.compressed_size = compressed_size;
2187
0
    zip->file_info.uncompressed_size = uncompressed_size;
2188
2189
0
    if (err == MZ_OK)
2190
0
        err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
2191
2192
    /* Update local header with crc32 and sizes */
2193
0
    if ((err == MZ_OK) && ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) == 0) &&
2194
0
        ((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
0
    zip->number_entry += 1;
2231
2232
0
    mz_zip_entry_close_int(zip);
2233
2234
0
    return err;
2235
0
}
2236
2237
27.3k
int32_t mz_zip_entry_seek_local_header(void *handle) {
2238
27.3k
    mz_zip *zip = (mz_zip *)handle;
2239
27.3k
    int64_t disk_size = 0;
2240
27.3k
    uint32_t disk_number = 0;
2241
2242
27.3k
    if (!zip)
2243
0
        return MZ_PARAM_ERROR;
2244
27.3k
    disk_number = zip->file_info.disk_number;
2245
27.3k
    if (disk_number == zip->disk_number_with_cd) {
2246
7.20k
        mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size);
2247
7.20k
        if ((disk_size == 0) || ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0))
2248
7.20k
            disk_number = (uint32_t)-1;
2249
7.20k
    }
2250
2251
27.3k
    mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, disk_number);
2252
2253
27.3k
    mz_zip_print("Zip - Entry - Seek local (disk %" PRId32 " offset %" PRId64 ")\n", disk_number,
2254
27.3k
                 zip->file_info.disk_offset);
2255
2256
    /* Guard against seek overflows */
2257
27.3k
    if ((zip->disk_offset_shift > 0) && (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
2258
24
        return MZ_FORMAT_ERROR;
2259
2260
27.3k
    return mz_stream_seek(zip->stream, zip->file_info.disk_offset + zip->disk_offset_shift, MZ_SEEK_SET);
2261
27.3k
}
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
28.8k
int32_t mz_zip_entry_close(void *handle) {
2274
28.8k
    return mz_zip_entry_close_raw(handle, UINT64_MAX, 0);
2275
28.8k
}
2276
2277
28.8k
int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32) {
2278
28.8k
    mz_zip *zip = (mz_zip *)handle;
2279
28.8k
    int32_t err = MZ_OK;
2280
2281
28.8k
    if (!zip || mz_zip_entry_is_open(zip) != MZ_OK)
2282
2.74k
        return MZ_PARAM_ERROR;
2283
2284
26.1k
    if (zip->open_mode & MZ_OPEN_MODE_WRITE)
2285
0
        err = mz_zip_entry_write_close(zip, crc32, UINT64_MAX, uncompressed_size);
2286
26.1k
    else
2287
26.1k
        err = mz_zip_entry_read_close(zip, NULL, NULL, NULL);
2288
2289
26.1k
    return err;
2290
28.8k
}
2291
2292
26.1k
int32_t mz_zip_entry_is_dir(void *handle) {
2293
26.1k
    mz_zip *zip = (mz_zip *)handle;
2294
26.1k
    int32_t filename_length = 0;
2295
2296
26.1k
    if (!zip || !zip->entry_scanned)
2297
0
        return MZ_PARAM_ERROR;
2298
26.1k
    if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2299
623
        return MZ_OK;
2300
2301
25.4k
    filename_length = (int32_t)strlen(zip->file_info.filename);
2302
25.4k
    if (filename_length > 0) {
2303
7.34k
        if (mz_os_is_dir_separator(zip->file_info.filename[filename_length - 1]))
2304
538
            return MZ_OK;
2305
7.34k
    }
2306
24.9k
    return MZ_EXIST_ERROR;
2307
25.4k
}
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
27.3k
int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info) {
2321
27.3k
    mz_zip *zip = (mz_zip *)handle;
2322
2323
27.3k
    if (!zip)
2324
0
        return MZ_PARAM_ERROR;
2325
2326
27.3k
    if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0) {
2327
27.3k
        if (!zip->entry_scanned)
2328
0
            return MZ_PARAM_ERROR;
2329
27.3k
    }
2330
2331
27.3k
    *file_info = &zip->file_info;
2332
27.3k
    return MZ_OK;
2333
27.3k
}
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
583k
static int32_t mz_zip_goto_next_entry_int(void *handle) {
2355
583k
    mz_zip *zip = (mz_zip *)handle;
2356
583k
    int32_t err = MZ_OK;
2357
2358
583k
    if (!zip)
2359
0
        return MZ_PARAM_ERROR;
2360
2361
583k
    zip->entry_scanned = 0;
2362
2363
583k
    mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
2364
2365
583k
    err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
2366
583k
    if (err == MZ_OK)
2367
583k
        err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
2368
583k
    if (err == MZ_OK)
2369
571k
        zip->entry_scanned = 1;
2370
583k
    return err;
2371
583k
}
2372
2373
26.1k
int64_t mz_zip_get_entry(void *handle) {
2374
26.1k
    mz_zip *zip = (mz_zip *)handle;
2375
2376
26.1k
    if (!zip)
2377
0
        return MZ_PARAM_ERROR;
2378
2379
26.1k
    return zip->cd_current_pos;
2380
26.1k
}
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
13.5k
int32_t mz_zip_goto_first_entry(void *handle) {
2397
13.5k
    mz_zip *zip = (mz_zip *)handle;
2398
2399
13.5k
    if (!zip)
2400
0
        return MZ_PARAM_ERROR;
2401
2402
13.5k
    zip->cd_current_pos = zip->cd_start_pos;
2403
2404
13.5k
    return mz_zip_goto_next_entry_int(zip);
2405
13.5k
}
2406
2407
569k
int32_t mz_zip_goto_next_entry(void *handle) {
2408
569k
    mz_zip *zip = (mz_zip *)handle;
2409
2410
569k
    if (!zip)
2411
0
        return MZ_PARAM_ERROR;
2412
2413
569k
    zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
2414
569k
        zip->file_info.extrafield_size + zip->file_info.comment_size;
2415
2416
569k
    return mz_zip_goto_next_entry_int(zip);
2417
569k
}
2418
2419
10.9k
int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case) {
2420
10.9k
    mz_zip *zip = (mz_zip *)handle;
2421
10.9k
    int32_t err = MZ_OK;
2422
10.9k
    int32_t result = 0;
2423
2424
10.9k
    if (!zip || !filename)
2425
0
        return MZ_PARAM_ERROR;
2426
2427
    /* If we are already on the current entry, no need to search */
2428
10.9k
    if (zip->entry_scanned && zip->file_info.filename) {
2429
1.63k
        result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2430
1.63k
        if (result == 0)
2431
180
            return MZ_OK;
2432
1.63k
    }
2433
2434
    /* Search all entries starting at the first */
2435
10.7k
    err = mz_zip_goto_first_entry(zip);
2436
554k
    while (err == MZ_OK) {
2437
543k
        result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2438
543k
        if (result == 0)
2439
143
            return MZ_OK;
2440
2441
543k
        err = mz_zip_goto_next_entry(zip);
2442
543k
    }
2443
2444
10.6k
    return err;
2445
10.7k
}
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
217k
int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby) {
2485
217k
    uint32_t posix_attrib = 0;
2486
217k
    uint8_t system = MZ_HOST_SYSTEM(version_madeby);
2487
217k
    int32_t err = MZ_OK;
2488
2489
217k
    err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2490
217k
    if (err == MZ_OK) {
2491
208k
        if ((posix_attrib & 0170000) == 0040000) /* S_ISDIR */
2492
623
            return MZ_OK;
2493
208k
    }
2494
2495
217k
    return MZ_EXIST_ERROR;
2496
217k
}
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
217k
int32_t mz_zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys, uint32_t *target_attrib) {
2513
217k
    if (!target_attrib)
2514
0
        return MZ_PARAM_ERROR;
2515
2516
217k
    *target_attrib = 0;
2517
2518
217k
    if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS)) {
2519
202k
        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
202k
        if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) ||
2524
202k
            (target_sys == MZ_HOST_SYSTEM_RISCOS))
2525
202k
            return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
2526
202k
    } else if ((src_sys == MZ_HOST_SYSTEM_UNIX) || (src_sys == MZ_HOST_SYSTEM_OSX_DARWIN) ||
2527
15.7k
               (src_sys == MZ_HOST_SYSTEM_RISCOS)) {
2528
        /* If high bytes are set, it contains unix specific attributes */
2529
6.51k
        if ((src_attrib >> 16) != 0)
2530
6.05k
            src_attrib >>= 16;
2531
2532
6.51k
        if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) ||
2533
6.51k
            (target_sys == MZ_HOST_SYSTEM_RISCOS)) {
2534
6.51k
            *target_attrib = src_attrib;
2535
6.51k
            return MZ_OK;
2536
6.51k
        }
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
9.19k
    return MZ_SUPPORT_ERROR;
2542
217k
}
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
202k
int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib) {
2567
202k
    if (!posix_attrib)
2568
0
        return MZ_PARAM_ERROR;
2569
2570
202k
    *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
2571
    /* FILE_ATTRIBUTE_READONLY */
2572
202k
    if ((win32_attrib & 0x01) == 0)
2573
201k
        *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
2574
    /* FILE_ATTRIBUTE_REPARSE_POINT */
2575
202k
    if ((win32_attrib & 0x400) == 0x400)
2576
875
        *posix_attrib |= 0120000; /* S_IFLNK */
2577
    /* FILE_ATTRIBUTE_DIRECTORY */
2578
201k
    else if ((win32_attrib & 0x10) == 0x10)
2579
331
        *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
2580
200k
    else
2581
200k
        *posix_attrib |= 0100000; /* S_IFREG */
2582
2583
202k
    return MZ_OK;
2584
202k
}
2585
2586
/***************************************************************************/
2587
2588
9.16k
int32_t mz_zip_extrafield_find(void *stream, uint16_t type, int32_t max_seek, uint16_t *length) {
2589
9.16k
    int32_t err = MZ_OK;
2590
9.16k
    uint16_t field_type = 0;
2591
9.16k
    uint16_t field_length = 0;
2592
2593
9.16k
    if (max_seek < 4)
2594
828
        return MZ_EXIST_ERROR;
2595
2596
15.9k
    do {
2597
15.9k
        err = mz_stream_read_uint16(stream, &field_type);
2598
15.9k
        if (err == MZ_OK)
2599
13.9k
            err = mz_stream_read_uint16(stream, &field_length);
2600
15.9k
        if (err != MZ_OK)
2601
3.80k
            break;
2602
2603
12.1k
        if (type == field_type) {
2604
1.63k
            if (length)
2605
0
                *length = field_length;
2606
1.63k
            return MZ_OK;
2607
1.63k
        }
2608
2609
10.4k
        max_seek -= field_length - 4;
2610
10.4k
        if (max_seek < 0)
2611
2.30k
            return MZ_EXIST_ERROR;
2612
2613
8.15k
        err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR);
2614
8.15k
    } while (err == MZ_OK);
2615
2616
4.39k
    return MZ_EXIST_ERROR;
2617
8.34k
}
2618
2619
int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size, uint16_t type,
2620
12.8k
                                   uint16_t *length) {
2621
12.8k
    void *file_extra_stream = NULL;
2622
12.8k
    int32_t err = MZ_OK;
2623
2624
12.8k
    if (!extrafield || !extrafield_size)
2625
3.70k
        return MZ_PARAM_ERROR;
2626
2627
9.16k
    file_extra_stream = mz_stream_mem_create();
2628
9.16k
    if (!file_extra_stream)
2629
0
        return MZ_MEM_ERROR;
2630
2631
9.16k
    mz_stream_mem_set_buffer(file_extra_stream, (void *)extrafield, extrafield_size);
2632
9.16k
    err = mz_zip_extrafield_find(file_extra_stream, type, extrafield_size, length);
2633
9.16k
    mz_stream_mem_delete(&file_extra_stream);
2634
9.16k
    return err;
2635
9.16k
}
2636
2637
276k
int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length) {
2638
276k
    int32_t err = MZ_OK;
2639
276k
    if (!type || !length)
2640
0
        return MZ_PARAM_ERROR;
2641
276k
    err = mz_stream_read_uint16(stream, type);
2642
276k
    if (err == MZ_OK)
2643
276k
        err = mz_stream_read_uint16(stream, length);
2644
276k
    return err;
2645
276k
}
2646
2647
17.1k
int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length) {
2648
17.1k
    int32_t err = MZ_OK;
2649
17.1k
    err = mz_stream_write_uint16(stream, type);
2650
17.1k
    if (err == MZ_OK)
2651
17.1k
        err = mz_stream_write_uint16(stream, length);
2652
17.1k
    return err;
2653
17.1k
}
2654
2655
/***************************************************************************/
2656
2657
191k
static int32_t mz_zip_invalid_date(const struct tm *ptm) {
2658
2.10M
#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
2659
191k
    return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */
2660
191k
            !datevalue_in_range(0, 11, ptm->tm_mon) || !datevalue_in_range(1, 31, ptm->tm_mday) ||
2661
191k
            !datevalue_in_range(0, 23, ptm->tm_hour) || !datevalue_in_range(0, 59, ptm->tm_min) ||
2662
191k
            !datevalue_in_range(0, 59, ptm->tm_sec));
2663
191k
#undef datevalue_in_range
2664
191k
}
2665
2666
792k
static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm) {
2667
792k
    uint64_t date = (uint64_t)(dos_date >> 16);
2668
2669
792k
    ptm->tm_mday = (int16_t)(date & 0x1f);
2670
792k
    ptm->tm_mon = (int16_t)(((date & 0x1E0) / 0x20) - 1);
2671
792k
    ptm->tm_year = (int16_t)(((date & 0x0FE00) / 0x0200) + 80);
2672
792k
    ptm->tm_hour = (int16_t)((dos_date & 0xF800) / 0x800);
2673
792k
    ptm->tm_min = (int16_t)((dos_date & 0x7E0) / 0x20);
2674
792k
    ptm->tm_sec = (int16_t)(2 * (dos_date & 0x1f));
2675
792k
    ptm->tm_isdst = -1;
2676
792k
}
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
792k
time_t mz_zip_dosdate_to_time_t(uint64_t dos_date) {
2693
792k
    struct tm ptm;
2694
792k
    mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
2695
792k
    return mz_zip_tm_to_time_t(&ptm);
2696
792k
}
2697
2698
792k
time_t mz_zip_tm_to_time_t(struct tm *ptm) {
2699
792k
    return mktime(ptm);
2700
792k
}
2701
2702
191k
int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm) {
2703
191k
    struct tm ltm;
2704
191k
    if (!ptm)
2705
0
        return MZ_PARAM_ERROR;
2706
191k
    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
191k
    memcpy(ptm, &ltm, sizeof(struct tm));
2712
191k
    return MZ_OK;
2713
191k
}
2714
2715
191k
uint32_t mz_zip_time_t_to_dos_date(time_t unix_time) {
2716
191k
    struct tm ptm;
2717
191k
    mz_zip_time_t_to_tm(unix_time, &ptm);
2718
191k
    return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
2719
191k
}
2720
2721
191k
uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm) {
2722
191k
    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
191k
    memcpy(&fixed_tm, ptm, sizeof(struct tm));
2732
191k
    if (fixed_tm.tm_year >= 1980) /* range [1980, 2107] */
2733
1.53k
        fixed_tm.tm_year -= 1980;
2734
190k
    else if (fixed_tm.tm_year >= 80) /* range [80, 207] */
2735
182k
        fixed_tm.tm_year -= 80;
2736
7.54k
    else /* range [00, 79] */
2737
7.54k
        fixed_tm.tm_year += 20;
2738
2739
191k
    if (mz_zip_invalid_date(&fixed_tm))
2740
1.65k
        return 0;
2741
2742
190k
    return (((uint32_t)fixed_tm.tm_mday + (32 * ((uint32_t)fixed_tm.tm_mon + 1)) + (512 * (uint32_t)fixed_tm.tm_year))
2743
190k
            << 16) |
2744
190k
        (((uint32_t)fixed_tm.tm_sec / 2) + (32 * (uint32_t)fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
2745
191k
}
2746
2747
83.3k
int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time) {
2748
83.3k
    *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
2749
83.3k
    return MZ_OK;
2750
83.3k
}
2751
2752
5.09k
int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time) {
2753
5.09k
    *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
2754
5.09k
    return MZ_OK;
2755
5.09k
}
2756
2757
/***************************************************************************/
2758
2759
545k
int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case) {
2760
549k
    do {
2761
549k
        if ((*path1 == '\\' && *path2 == '/') || (*path2 == '\\' && *path1 == '/')) {
2762
            /* Ignore comparison of path slashes */
2763
549k
        } else if (ignore_case) {
2764
273k
            if (tolower(*path1) != tolower(*path2))
2765
269k
                break;
2766
275k
        } else if (*path1 != *path2) {
2767
273k
            break;
2768
273k
        }
2769
2770
5.29k
        path1 += 1;
2771
5.29k
        path2 += 1;
2772
5.29k
    } while (*path1 != 0 && *path2 != 0);
2773
2774
545k
    if (ignore_case)
2775
270k
        return (int32_t)(tolower(*path1) - tolower(*path2));
2776
2777
274k
    return (int32_t)(*path1 - *path2);
2778
545k
}
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
/***************************************************************************/