Coverage Report

Created: 2026-02-14 07:09

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