Coverage Report

Created: 2025-08-26 06:44

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