Coverage Report

Created: 2026-01-09 06:30

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