Coverage Report

Created: 2023-03-26 06:14

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