Coverage Report

Created: 2025-08-28 06:22

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