Coverage Report

Created: 2025-06-16 06:10

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