Coverage Report

Created: 2026-02-14 07:09

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