Debian Codegraph @

mz_zip.c

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