Coverage Report

Created: 2025-04-11 06:10

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