blob: cdf573356aa2cf419c00319485f5cbc51e7ce59e [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvaziri10ac2912019-09-18 16:55:43 -07002 Version 2.9.0, September 18, 2019
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08003 part of the MiniZip project
4
Nathan Moinvaziri2ca7f392019-01-08 16:07:10 -08005 Copyright (C) 2010-2019 Nathan Moinvaziri
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08006 https://github.com/nmoinvaz/minizip
Nathan Moinvaziri79cfab02018-08-17 12:21:06 -07007 Copyright (C) 2009-2010 Mathias Svensson
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08008 Modifications for Zip64 support
9 http://result42.com
Nathan Moinvaziri79cfab02018-08-17 12:21:06 -070010 Copyright (C) 2007-2008 Even Rouault
11 Modifications of Unzip for Zip64
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080012 Copyright (C) 1998-2010 Gilles Vollant
Viktor Szakats9dea6f22018-10-17 22:39:01 +000013 https://www.winimage.com/zLibDll/minizip.html
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080014
15 This program is distributed under the terms of the same license as zlib.
16 See the accompanying LICENSE file for the full text of the license.
17*/
18
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080019
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070020#include "mz.h"
Nathan Moinvaziri5f091882018-10-24 18:06:08 -070021#include "mz_crypt.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080022#include "mz_strm.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080023#ifdef HAVE_BZIP2
24# include "mz_strm_bzip.h"
25#endif
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -080026#ifdef HAVE_LIBCOMP
Nathan Moinvaziri90d31c72018-11-20 03:17:20 -080027# include "mz_strm_libcomp.h"
28#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080029#ifdef HAVE_LZMA
30# include "mz_strm_lzma.h"
31#endif
Nathan Moinvazirib47b41b2018-07-11 09:44:29 -070032#include "mz_strm_mem.h"
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -070033#ifdef HAVE_PKCRYPT
34# include "mz_strm_pkcrypt.h"
35#endif
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -080036#ifdef HAVE_WZAES
Nathan Moinvaziri21a31022018-10-24 09:50:16 -070037# include "mz_strm_wzaes.h"
38#endif
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -080039#ifdef HAVE_ZLIB
40# include "mz_strm_zlib.h"
41#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080042
43#include "mz_zip.h"
44
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080045#include <ctype.h> /* tolower */
Nathan Moinvazirie8496272018-11-21 15:05:58 -080046#include <stdio.h> /* snprintf */
Nathan Moinvaziri1ee609b2018-11-19 21:34:35 -080047
zedcf076662019-05-15 21:43:56 +030048#if defined(_MSC_VER) || defined(__MINGW32__)
Nathan Moinvaziri2e0a20a2019-04-28 13:03:11 -070049# define localtime_r(t1,t2) (localtime_s(t2,t1) == 0 ? t1 : NULL)
zed65447dd2019-05-16 22:19:08 +030050#endif
51#if defined(_MSC_VER) && (_MSC_VER < 1900)
52# define snprintf _snprintf
Nathan Moinvaziric565fa82018-10-19 08:48:33 -070053#endif
54
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080055/***************************************************************************/
56
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070057#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
Nathan Moinvaziribe483d82019-10-19 17:30:23 -070058#define MZ_ZIP_MAGIC_LOCALHEADERU8 { 0x50, 0x4b, 0x03, 0x04 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070059#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
Nathan Moinvaziri545d7292019-10-21 15:32:18 -070060#define MZ_ZIP_MAGIC_CENTRALHEADERU8 { 0x50, 0x4b, 0x01, 0x02 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070061#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080062#define MZ_ZIP_MAGIC_ENDHEADERU8 { 0x50, 0x4b, 0x05, 0x06 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070063#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
64#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
65#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080066#define MZ_ZIP_MAGIC_DATADESCRIPTORU8 { 0x50, 0x4b, 0x07, 0x08 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070067
Nathan Moinvaziriba1db162018-11-23 10:07:35 -080068#define MZ_ZIP_SIZE_LD_ITEM (30)
Nathan Moinvaziri638f31f2018-08-27 08:17:16 -070069#define MZ_ZIP_SIZE_CD_ITEM (46)
70#define MZ_ZIP_SIZE_CD_LOCATOR64 (20)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080071
Nathan Moinvaziric78c7822018-12-01 20:42:36 -080072#ifndef MZ_ZIP_EOCD_MAX_BACK
73#define MZ_ZIP_EOCD_MAX_BACK (1 << 20)
74#endif
75
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080076/***************************************************************************/
77
78typedef struct mz_zip_s
79{
80 mz_zip_file file_info;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070081 mz_zip_file local_file_info;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080082
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080083 void *stream; /* main stream */
84 void *cd_stream; /* pointer to the stream with the cd */
85 void *cd_mem_stream; /* memory stream for central directory */
86 void *compress_stream; /* compression stream */
87 void *crypt_stream; /* encryption stream */
88 void *file_info_stream; /* memory stream for storing file info */
89 void *local_file_info_stream; /* memory stream for storing local file info */
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080090
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070091 int32_t open_mode;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -080092 uint8_t recover;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070093
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080094 uint32_t disk_number_with_cd; /* number of the disk with the central dir */
95 int64_t disk_offset_shift; /* correction for zips that have wrong offset start of cd */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070096
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080097 int64_t cd_start_pos; /* pos of the first file in the central dir stream */
98 int64_t cd_current_pos; /* pos of the current file in the central dir */
99 int64_t cd_offset; /* offset of start of central directory */
100 int64_t cd_size; /* size of the central directory */
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -0700101 uint32_t cd_signature; /* signature of central directory */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700102
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800103 uint8_t entry_scanned; /* entry header information read ok */
104 uint8_t entry_opened; /* entry is open for read/write */
105 uint8_t entry_raw; /* entry opened with raw mode */
106 uint32_t entry_crc32; /* entry crc32 */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700107
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700108 uint64_t number_entry;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700109
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700110 uint16_t version_madeby;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700111 char *comment;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800112} mz_zip;
113
114/***************************************************************************/
115
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800116#if 0
117# define mz_zip_print printf
118#else
119# define mz_zip_print(fmt,...)
120#endif
121
122/***************************************************************************/
123
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800124/* Locate the end of central directory */
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700125static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800126{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700127 int64_t file_size = 0;
Nathan Moinvaziric78c7822018-12-01 20:42:36 -0800128 int64_t max_back = MZ_ZIP_EOCD_MAX_BACK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800129 uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8;
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700130 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700131
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700132 err = mz_stream_seek(stream, 0, MZ_SEEK_END);
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800133 if (err != MZ_OK)
134 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800135
136 file_size = mz_stream_tell(stream);
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800137
Nathan Moinvaziric78c7822018-12-01 20:42:36 -0800138 if (max_back <= 0 || max_back > file_size)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800139 max_back = file_size;
140
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800141 return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800142}
143
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800144/* Locate the end of central directory 64 of a zip file */
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700145static int32_t mz_zip_search_zip64_eocd(void *stream, const int64_t end_central_offset, int64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800146{
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700147 int64_t offset = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800148 uint32_t value32 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700149 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700150
151
152 *central_pos = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800153
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800154 /* Zip64 end of central directory locator */
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700155 err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800156 /* Read locator signature */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700157 if (err == MZ_OK)
158 {
159 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700160 if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700161 err = MZ_FORMAT_ERROR;
162 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800163 /* Number of the disk with the start of the zip64 end of central directory */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700164 if (err == MZ_OK)
165 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800166 /* Relative offset of the zip64 end of central directory record8 */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700167 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700168 err = mz_stream_read_uint64(stream, (uint64_t *)&offset);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800169 /* Total number of disks */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700170 if (err == MZ_OK)
171 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800172 /* Goto end of central directory record */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700173 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700174 err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800175 /* The signature */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700176 if (err == MZ_OK)
177 {
178 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700179 if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700180 err = MZ_FORMAT_ERROR;
181 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800182
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700183 if (err == MZ_OK)
184 *central_pos = offset;
185
186 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800187}
188
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800189/* Get info about the current file in the zip file */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700190static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file *file_info, void *file_extra_stream)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700191{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700192 uint64_t ntfs_time = 0;
193 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700194 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700195 uint32_t dos_date = 0;
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800196 uint32_t field_pos = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700197 uint16_t field_type = 0;
198 uint16_t field_length = 0;
199 uint32_t field_length_read = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700200 uint16_t ntfs_attrib_id = 0;
201 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700202 uint16_t linkname_size;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700203 uint16_t value16 = 0;
204 uint32_t value32 = 0;
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800205 int64_t extrafield_pos = 0;
206 int64_t comment_pos = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700207 int64_t linkname_pos = 0;
208 int64_t saved_pos = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700209 int32_t err = MZ_OK;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700210 char *linkname = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700211
212
213 memset(file_info, 0, sizeof(mz_zip_file));
214
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800215 /* Check the magic */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700216 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700217 if (err == MZ_END_OF_STREAM)
218 err = MZ_END_OF_LIST;
219 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700220 err = MZ_END_OF_LIST;
221 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
222 err = MZ_FORMAT_ERROR;
223 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
224 err = MZ_FORMAT_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000225
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800226 /* Read header fields */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700227 if (err == MZ_OK)
228 {
229 if (!local)
230 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
231 if (err == MZ_OK)
232 err = mz_stream_read_uint16(stream, &file_info->version_needed);
233 if (err == MZ_OK)
234 err = mz_stream_read_uint16(stream, &file_info->flag);
235 if (err == MZ_OK)
236 err = mz_stream_read_uint16(stream, &file_info->compression_method);
237 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700238 {
239 err = mz_stream_read_uint32(stream, &dos_date);
240 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
241 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700242 if (err == MZ_OK)
243 err = mz_stream_read_uint32(stream, &file_info->crc);
244 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700245 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700246 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700247 file_info->compressed_size = value32;
248 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700249 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700250 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700251 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700252 file_info->uncompressed_size = value32;
253 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700254 if (err == MZ_OK)
255 err = mz_stream_read_uint16(stream, &file_info->filename_size);
256 if (err == MZ_OK)
257 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
258 if (!local)
259 {
260 if (err == MZ_OK)
261 err = mz_stream_read_uint16(stream, &file_info->comment_size);
262 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700263 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700264 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700265 file_info->disk_number = value16;
266 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700267 if (err == MZ_OK)
268 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
269 if (err == MZ_OK)
270 err = mz_stream_read_uint32(stream, &file_info->external_fa);
271 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700272 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700273 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700274 file_info->disk_offset = value32;
275 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700276 }
277 }
278
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700279 if (err == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800280 err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700281
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800282 /* Copy variable length data to memory stream for later retrieval */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700283 if ((err == MZ_OK) && (file_info->filename_size > 0))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700284 err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800285 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800286 extrafield_pos = mz_stream_tell(file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700287
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800288 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
289 err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800290 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800291
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800292 comment_pos = mz_stream_tell(file_extra_stream);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800293 if ((err == MZ_OK) && (file_info->comment_size > 0))
294 err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800295 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700296
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700297 linkname_pos = mz_stream_tell(file_extra_stream);
298 /* Overwrite if we encounter UNIX1 extra block */
299 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700300
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700301 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
302 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800303 /* Seek to and parse the extra field */
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800304 err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700305
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800306 while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700307 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700308 err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length);
Nathan Moinvaziriea2bd792018-11-19 17:44:44 -0800309 if (err != MZ_OK)
310 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800311 field_pos += 4;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800312
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800313 /* Don't allow field length to exceed size of remaining extrafield */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800314 if (field_length > (file_info->extrafield_size - field_pos))
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -0800315 field_length = (uint16_t)(file_info->extrafield_size - field_pos);
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800316
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800317 /* Read ZIP64 extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800318 if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700319 {
320 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800321 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700322 err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800323 if (file_info->uncompressed_size < 0)
324 err = MZ_FORMAT_ERROR;
325 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700326 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800327 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700328 err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800329 if (file_info->compressed_size < 0)
330 err = MZ_FORMAT_ERROR;
331 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700332 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800333 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700334 err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800335 if (file_info->disk_offset < 0)
336 err = MZ_FORMAT_ERROR;
337 }
juanii4ae79922018-02-11 14:29:36 -0300338 if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700339 err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700340 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800341 /* Read NTFS extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800342 else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700343 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700344 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700345 err = mz_stream_read_uint32(file_extra_stream, &reserved);
346 field_length_read = 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700347
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800348 while ((err == MZ_OK) && (field_length_read + 4 <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700349 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700350 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700351 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700352 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800353 field_length_read += 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700354
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700355 if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700356 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700357 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700358 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
359
juanii7063b0e2018-02-11 13:56:21 -0300360 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700361 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700362 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700363 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
364 }
juanii7063b0e2018-02-11 13:56:21 -0300365 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700366 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700367 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700368 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
369 }
370 }
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800371 else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700372 {
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800373 err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700374 }
375
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800376 field_length_read += ntfs_attrib_size;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700377 }
378 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800379 /* Read UNIX1 extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800380 else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700381 {
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700382 if (err == MZ_OK)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700383 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700384 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700385 if (err == MZ_OK && file_info->accessed_date == 0)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700386 file_info->accessed_date = value32;
387 }
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700388 if (err == MZ_OK)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700389 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700390 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700391 if (err == MZ_OK && file_info->modified_date == 0)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700392 file_info->modified_date = value32;
393 }
394 if (err == MZ_OK)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800395 err = mz_stream_read_uint16(file_extra_stream, &value16); /* User id */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700396 if (err == MZ_OK)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800397 err = mz_stream_read_uint16(file_extra_stream, &value16); /* Group id */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700398
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700399 /* Copy linkname to end of file extra stream so we can return null
400 terminated string */
401 linkname_size = field_length - 12;
402 if ((err == MZ_OK) && (linkname_size > 0))
403 {
404 linkname = (char *)MZ_ALLOC(linkname_size);
405 if (linkname != NULL)
406 {
407 if (mz_stream_read(file_extra_stream, linkname, linkname_size) != linkname_size)
408 err = MZ_READ_ERROR;
409 if (err == MZ_OK)
410 {
411 saved_pos = mz_stream_tell(file_extra_stream);
412
413 mz_stream_seek(file_extra_stream, linkname_pos, MZ_SEEK_SET);
414 mz_stream_write(file_extra_stream, linkname, linkname_size);
415 mz_stream_write_uint8(file_extra_stream, 0);
416
417 mz_stream_seek(file_extra_stream, saved_pos, MZ_SEEK_SET);
418 }
419 MZ_FREE(linkname);
420 }
421 }
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700422 }
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800423#ifdef HAVE_WZAES
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800424 /* Read AES extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800425 else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700426 {
427 uint8_t value8 = 0;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800428 /* Verify version info */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700429 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800430 /* Support AE-1 and AE-2 */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700431 if (value16 != 1 && value16 != 2)
432 err = MZ_FORMAT_ERROR;
433 file_info->aes_version = value16;
434 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700435 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700436 if ((char)value8 != 'A')
437 err = MZ_FORMAT_ERROR;
438 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700439 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700440 if ((char)value8 != 'E')
441 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800442 /* Get AES encryption strength and actual compression method */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700443 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700444 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700445 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700446 file_info->aes_encryption_mode = value8;
447 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700448 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700449 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700450 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700451 file_info->compression_method = value16;
452 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700453 }
454#endif
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800455 else if (field_length > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700456 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700457 err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700458 }
459
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800460 field_pos += field_length;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700461 }
462 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700463
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800464 /* Get pointers to variable length data */
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800465 mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename);
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800466 mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield);
467 mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700468 mz_stream_mem_get_buffer_at(file_extra_stream, linkname_pos, (const void **)&file_info->linkname);
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800469
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800470 /* Set to empty string just in-case */
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800471 if (file_info->filename == NULL)
472 file_info->filename = "";
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800473 if (file_info->extrafield == NULL)
474 file_info->extrafield_size = 0;
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800475 if (file_info->comment == NULL)
476 file_info->comment = "";
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700477 if (file_info->linkname == NULL)
478 file_info->linkname = "";
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700479
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800480 if (err == MZ_OK)
481 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700482 mz_zip_print("Zip - Entry - Read header - %s (local %" PRId8 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800483 file_info->filename, local);
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700484 mz_zip_print("Zip - Entry - Read header compress (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800485 file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
486 if (!local)
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800487 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700488 mz_zip_print("Zip - Entry - Read header disk (disk %" PRIu32 " offset %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800489 file_info->disk_number, file_info->disk_offset);
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800490 }
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700491 mz_zip_print("Zip - Entry - Read header variable (fnl %" PRId32 " efs %" PRId32 " cms %" PRId32 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800492 file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
493 }
494
495 return err;
496}
497
498static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size)
499{
500 uint32_t value32 = 0;
501 int64_t value64 = 0;
502 int32_t err = MZ_OK;
503
504
505 err = mz_stream_read_uint32(stream, &value32);
506 if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR)
507 err = MZ_FORMAT_ERROR;
508 if (err == MZ_OK)
509 err = mz_stream_read_uint32(stream, &value32);
510 if ((err == MZ_OK) && (crc32 != NULL))
511 *crc32 = value32;
512 if (err == MZ_OK)
513 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800514 /* If zip 64 extension is enabled then read as 8 byte */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800515 if (!zip64)
516 {
517 err = mz_stream_read_uint32(stream, &value32);
518 value64 = value32;
519 }
520 else
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800521 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800522 err = mz_stream_read_int64(stream, &value64);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800523 if (value64 < 0)
524 err = MZ_FORMAT_ERROR;
525 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800526 if ((err == MZ_OK) && (compressed_size != NULL))
527 *compressed_size = value64;
528 }
529 if (err == MZ_OK)
530 {
531 if (!zip64)
532 {
533 err = mz_stream_read_uint32(stream, &value32);
534 value64 = value32;
535 }
536 else
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800537 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800538 err = mz_stream_read_int64(stream, &value64);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800539 if (value64 < 0)
540 err = MZ_FORMAT_ERROR;
541 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800542 if ((err == MZ_OK) && (uncompressed_size != NULL))
543 *uncompressed_size = value64;
544 }
545
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700546 return err;
547}
548
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700549static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700550{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700551 uint64_t ntfs_time = 0;
552 uint32_t reserved = 0;
553 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700554 uint16_t extrafield_size = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700555 uint16_t field_type = 0;
556 uint16_t field_length = 0;
557 uint16_t field_length_zip64 = 0;
558 uint16_t field_length_ntfs = 0;
559 uint16_t field_length_aes = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700560 uint16_t field_length_unix1 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700561 uint16_t filename_size = 0;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700562 uint16_t filename_length = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700563 uint16_t linkname_size = 0;
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700564 uint16_t version_needed = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800565 int32_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700566 int32_t err = MZ_OK;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700567 int32_t err_mem = MZ_OK;
568 uint8_t zip64 = 0;
569 uint8_t skip_aes = 0;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700570 uint8_t mask = 0;
571 uint8_t write_end_slash = 0;
572 const char *filename = NULL;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700573 char masked_name[64];
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700574 void *file_extra_stream = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700575
576 if (file_info == NULL)
577 return MZ_PARAM_ERROR;
578
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700579 if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO))
580 mask = 1;
581
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800582 /* Calculate extra field sizes */
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700583 if (file_info->uncompressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700584 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700585 if (file_info->compressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700586 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700587 if (file_info->disk_offset >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700588 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700589
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700590 if (file_info->zip64 == MZ_ZIP64_AUTO)
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700591 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800592 /* If uncompressed size is unknown, assume zip64 for 64-bit data descriptors */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700593 zip64 = (local && file_info->uncompressed_size == 0) || (field_length_zip64 > 0);
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700594 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700595 else if (file_info->zip64 == MZ_ZIP64_FORCE)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700596 {
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700597 zip64 = 1;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700598 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700599 else if (file_info->zip64 == MZ_ZIP64_DISABLE)
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700600 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800601 /* Zip64 extension is required to zip file */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700602 if (field_length_zip64 > 0)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700603 return MZ_PARAM_ERROR;
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700604 }
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700605
606 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700607 {
608 extrafield_size += 4;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700609 extrafield_size += field_length_zip64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700610 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700611
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800612 /* Calculate extra field size and check for duplicates */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700613 if (file_info->extrafield_size > 0)
614 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700615 mz_stream_mem_create(&file_extra_stream);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700616 mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
Nathan Moinvaziri915c5132018-10-26 20:00:52 -0700617 file_info->extrafield_size);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700618
619 do
620 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700621 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700622 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700623 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700624 if (err_mem != MZ_OK)
625 break;
626
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800627 /* Prefer incoming aes extensions over ours */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700628 if (field_type == MZ_ZIP_EXTENSION_AES)
629 skip_aes = 1;
630
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700631 /* Prefer our zip64, ntfs, unix1 extension over incoming */
632 if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS &&
633 field_type != MZ_ZIP_EXTENSION_UNIX1)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700634 extrafield_size += 4 + field_length;
635
636 if (err_mem == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800637 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700638 }
639 while (err_mem == MZ_OK);
640 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700641
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800642#ifdef HAVE_WZAES
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700643 if (!skip_aes)
644 {
645 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700646 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700647 field_length_aes = 1 + 1 + 1 + 2 + 2;
648 extrafield_size += 4 + field_length_aes;
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700649 }
650 }
Nathan Moinvaziria2bb08c2018-10-28 13:45:30 -0700651#else
Nathan Moinvaziri7de94cf2018-11-19 20:51:06 -0800652 MZ_UNUSED(field_length_aes);
Nathan Moinvaziri4ae469a2018-10-28 15:32:04 -0700653 MZ_UNUSED(skip_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700654#endif
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800655 /* NTFS timestamps */
Nathan Moinvazirid9e159a2018-02-13 15:30:30 -0800656 if ((file_info->modified_date != 0) &&
657 (file_info->accessed_date != 0) &&
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700658 (file_info->creation_date != 0) && (!mask))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700659 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700660 field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
661 extrafield_size += 4 + field_length_ntfs;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700662 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700663
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700664 /* Unix1 symbolic links */
665 if (file_info->linkname != NULL && *file_info->linkname != 0)
666 {
667 linkname_size = (uint16_t)strlen(file_info->linkname);
668 field_length_unix1 = 12 + linkname_size;
669 extrafield_size += 4 + field_length_unix1;
670 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700671
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700672 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700673 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700674 else
675 {
676 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
677 if (err == MZ_OK)
678 err = mz_stream_write_uint16(stream, file_info->version_madeby);
679 }
680
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800681 /* Calculate version needed to extract */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700682 if (err == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700683 {
684 version_needed = file_info->version_needed;
685 if (version_needed == 0)
686 {
687 version_needed = 20;
688 if (zip64)
689 version_needed = 45;
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800690#ifdef HAVE_WZAES
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700691 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
692 version_needed = 51;
693#endif
694#ifdef HAVE_LZMA
695 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
696 version_needed = 63;
697#endif
698 }
699 err = mz_stream_write_uint16(stream, version_needed);
700 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700701 if (err == MZ_OK)
702 err = mz_stream_write_uint16(stream, file_info->flag);
703 if (err == MZ_OK)
704 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800705#ifdef HAVE_WZAES
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700706 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700707 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700708 else
709#endif
710 err = mz_stream_write_uint16(stream, file_info->compression_method);
711 }
712 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700713 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700714 if (file_info->modified_date != 0 && !mask)
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -0700715 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700716 err = mz_stream_write_uint32(stream, dos_date);
717 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700718
719 if (err == MZ_OK)
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700720 {
721 if (mask)
722 err = mz_stream_write_uint32(stream, 0);
723 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800724 err = mz_stream_write_uint32(stream, file_info->crc); /* crc */
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700725 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700726 if (err == MZ_OK)
727 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800728 if (file_info->compressed_size >= UINT32_MAX) /* compr size */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700729 err = mz_stream_write_uint32(stream, UINT32_MAX);
730 else
731 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
732 }
733 if (err == MZ_OK)
734 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800735 if (file_info->uncompressed_size >= UINT32_MAX) /* uncompr size */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700736 err = mz_stream_write_uint32(stream, UINT32_MAX);
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700737 else if (mask)
738 err = mz_stream_write_uint32(stream, 0);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700739 else
740 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
741 }
742
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700743 if (mask)
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700744 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700745 snprintf(masked_name, sizeof(masked_name), "%" PRIx32 "_%" PRIx64,
Nathan Moinvaziri264dc182018-11-13 17:42:13 -0800746 file_info->disk_number, file_info->disk_offset);
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700747 filename = masked_name;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700748 }
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700749 else
750 {
751 filename = file_info->filename;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700752 }
753
754 filename_length = (uint16_t)strlen(filename);
755 filename_size += filename_length;
756
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800757 if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) &&
758 ((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\')))
759 {
760 filename_size += 1;
761 write_end_slash = 1;
762 }
763
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700764 if (err == MZ_OK)
765 err = mz_stream_write_uint16(stream, filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700766 if (err == MZ_OK)
767 err = mz_stream_write_uint16(stream, extrafield_size);
768
769 if (!local)
770 {
771 if (file_info->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800772 {
773 comment_size = (int32_t)strlen(file_info->comment);
774 if (comment_size > UINT16_MAX)
775 comment_size = UINT16_MAX;
776 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700777 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800778 err = mz_stream_write_uint16(stream, (uint16_t)comment_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700779 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700780 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700781 if (err == MZ_OK)
782 err = mz_stream_write_uint16(stream, file_info->internal_fa);
783 if (err == MZ_OK)
784 err = mz_stream_write_uint32(stream, file_info->external_fa);
785 if (err == MZ_OK)
786 {
787 if (file_info->disk_offset >= UINT32_MAX)
788 err = mz_stream_write_uint32(stream, UINT32_MAX);
789 else
790 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
791 }
792 }
793
794 if (err == MZ_OK)
795 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700796 if (mz_stream_write(stream, filename, filename_length) != filename_length)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700797 err = MZ_WRITE_ERROR;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700798
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800799 /* Ensure that directories have a slash appended to them for compatibility */
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700800 if (err == MZ_OK && write_end_slash)
801 err = mz_stream_write_uint8(stream, '/');
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700802 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700803
804 if (file_info->extrafield_size > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700805 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800806 err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700807 while (err == MZ_OK && err_mem == MZ_OK)
808 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700809 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700810 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700811 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700812 if (err_mem != MZ_OK)
813 break;
814
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700815 /* Prefer our zip 64, ntfs, unix1 extensions over incoming */
816 if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS ||
817 field_type == MZ_ZIP_EXTENSION_UNIX1)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700818 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800819 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700820 continue;
821 }
822
823 err = mz_stream_write_uint16(stream, field_type);
824 if (err == MZ_OK)
825 err = mz_stream_write_uint16(stream, field_length);
826 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700827 err = mz_stream_copy(stream, file_extra_stream, field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700828 }
829
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700830 mz_stream_mem_delete(&file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700831 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700832
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800833 /* Write ZIP64 extra field */
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700834 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700835 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700836 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700837 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700838 {
839 if (mask)
840 err = mz_stream_write_int64(stream, 0);
841 else
842 err = mz_stream_write_int64(stream, file_info->uncompressed_size);
843 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700844 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700845 err = mz_stream_write_int64(stream, file_info->compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700846 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700847 err = mz_stream_write_int64(stream, file_info->disk_offset);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700848 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800849 /* Write NTFS extra field */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700850 if ((err == MZ_OK) && (field_length_ntfs > 0))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700851 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700852 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700853 if (err == MZ_OK)
854 err = mz_stream_write_uint32(stream, reserved);
855 if (err == MZ_OK)
856 err = mz_stream_write_uint16(stream, 0x01);
857 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700858 err = mz_stream_write_uint16(stream, field_length_ntfs - 8);
juanii3679a3d2018-02-11 13:55:38 -0300859 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700860 {
861 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
862 err = mz_stream_write_uint64(stream, ntfs_time);
863 }
juanii3679a3d2018-02-11 13:55:38 -0300864 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700865 {
866 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
867 err = mz_stream_write_uint64(stream, ntfs_time);
868 }
juanii3679a3d2018-02-11 13:55:38 -0300869 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700870 {
871 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
872 err = mz_stream_write_uint64(stream, ntfs_time);
873 }
874 }
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700875 /* Write UNIX extra block extra field */
876 if ((err == MZ_OK) && (field_length_unix1 > 0))
877 {
878 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_UNIX1, field_length_unix1);
879 if (err == MZ_OK)
Nathan Moinvaziriaeb6cdd2019-05-06 13:40:32 -0700880 err = mz_stream_write_uint32(stream, (uint32_t)file_info->accessed_date);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700881 if (err == MZ_OK)
Nathan Moinvaziriaeb6cdd2019-05-06 13:40:32 -0700882 err = mz_stream_write_uint32(stream, (uint32_t)file_info->modified_date);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700883 if (err == MZ_OK) /* User id */
884 err = mz_stream_write_uint16(stream, 0);
885 if (err == MZ_OK) /* Group id */
886 err = mz_stream_write_uint16(stream, 0);
887 if (err == MZ_OK && linkname_size > 0)
888 {
889 if (mz_stream_write(stream, file_info->linkname, linkname_size) != linkname_size)
890 err = MZ_WRITE_ERROR;
891 }
892 }
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800893#ifdef HAVE_WZAES
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800894 /* Write AES extra field */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700895 if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700896 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700897 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700898 if (err == MZ_OK)
899 err = mz_stream_write_uint16(stream, file_info->aes_version);
900 if (err == MZ_OK)
901 err = mz_stream_write_uint8(stream, 'A');
902 if (err == MZ_OK)
903 err = mz_stream_write_uint8(stream, 'E');
904 if (err == MZ_OK)
905 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
906 if (err == MZ_OK)
907 err = mz_stream_write_uint16(stream, file_info->compression_method);
908 }
909#endif
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700910 if ((err == MZ_OK) && (!local) && (file_info->comment != NULL))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700911 {
Nathan Moinvazirica014e62018-08-15 07:31:12 -0700912 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700913 err = MZ_WRITE_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700914 }
915
916 return err;
917}
918
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800919static int32_t mz_zip_entry_write_descriptor(void *stream, uint8_t zip64, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800920{
921 int32_t err = MZ_OK;
922
923 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
924 if (err == MZ_OK)
925 err = mz_stream_write_uint32(stream, crc32);
926
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800927 /* Store data descriptor as 8 bytes if zip 64 extension enabled */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800928 if (err == MZ_OK)
929 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800930 /* Zip 64 extension is enabled when uncompressed size is > UINT32_MAX */
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800931 if (!zip64)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800932 err = mz_stream_write_uint32(stream, (uint32_t)compressed_size);
933 else
934 err = mz_stream_write_int64(stream, compressed_size);
935 }
936 if (err == MZ_OK)
937 {
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800938 if (!zip64)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800939 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
947static int32_t mz_zip_read_cd(void *handle)
948{
949 mz_zip *zip = (mz_zip *)handle;
950 uint64_t number_entry_cd64 = 0;
951 uint64_t number_entry = 0;
952 uint64_t number_entry_cd = 0;
953 int64_t eocd_pos = 0;
954 int64_t eocd_pos64 = 0;
955 int64_t value64i = 0;
956 uint16_t value16 = 0;
957 uint32_t value32 = 0;
958 uint64_t value64 = 0;
959 uint16_t comment_size = 0;
960 int32_t comment_read = 0;
961 int32_t err = MZ_OK;
962
963
964 if (zip == NULL)
965 return MZ_PARAM_ERROR;
966
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800967 /* Read and cache central directory records */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800968 err = mz_zip_search_eocd(zip->stream, &eocd_pos);
969 if (err == MZ_OK)
970 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800971 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800972 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800973 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800974 if (err == MZ_OK)
975 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800976 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800977 if (err == MZ_OK)
978 err = mz_stream_read_uint16(zip->stream, &value16);
979 zip->disk_number_with_cd = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800980 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800981 if (err == MZ_OK)
982 err = mz_stream_read_uint16(zip->stream, &value16);
983 zip->number_entry = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800984 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800985 if (err == MZ_OK)
986 err = mz_stream_read_uint16(zip->stream, &value16);
987 number_entry_cd = value16;
988 if (number_entry_cd != zip->number_entry)
989 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800990 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800991 if (err == MZ_OK)
992 err = mz_stream_read_uint32(zip->stream, &value32);
993 if (err == MZ_OK)
994 zip->cd_size = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800995 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800996 if (err == MZ_OK)
997 err = mz_stream_read_uint32(zip->stream, &value32);
998 if (err == MZ_OK)
999 zip->cd_offset = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001000 /* Zip file global comment length */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001001 if (err == MZ_OK)
1002 err = mz_stream_read_uint16(zip->stream, &comment_size);
1003 if ((err == MZ_OK) && (comment_size > 0))
1004 {
1005 zip->comment = (char *)MZ_ALLOC(comment_size + 1);
1006 if (zip->comment != NULL)
1007 {
1008 comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001009 /* Don't fail if incorrect comment length read, not critical */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001010 if (comment_read < 0)
1011 comment_read = 0;
1012 zip->comment[comment_read] = 0;
1013 }
1014 }
1015
1016 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
1017 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001018 /* Format should be Zip64, as the central directory or file size is too large */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001019 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
1020 {
1021 eocd_pos = eocd_pos64;
1022
1023 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001024 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001025 if (err == MZ_OK)
1026 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001027 /* Size of zip64 end of central directory record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001028 if (err == MZ_OK)
1029 err = mz_stream_read_uint64(zip->stream, &value64);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001030 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001031 if (err == MZ_OK)
1032 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001033 /* Version needed to extract */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001034 if (err == MZ_OK)
1035 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001036 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001037 if (err == MZ_OK)
1038 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001039 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001040 if (err == MZ_OK)
1041 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001042 /* Total number of entries in the central directory on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001043 if (err == MZ_OK)
1044 err = mz_stream_read_uint64(zip->stream, &number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001045 /* Total number of entries in the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001046 if (err == MZ_OK)
1047 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
1048 if (number_entry == UINT32_MAX)
1049 zip->number_entry = number_entry_cd64;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001050 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001051 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001052 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001053 err = mz_stream_read_int64(zip->stream, &zip->cd_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001054 if (zip->cd_size < 0)
1055 err = MZ_FORMAT_ERROR;
1056 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001057 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001058 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001059 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001060 err = mz_stream_read_int64(zip->stream, &zip->cd_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001061 if (zip->cd_offset < 0)
1062 err = MZ_FORMAT_ERROR;
1063 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001064 }
1065 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
1066 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
1067 {
1068 err = MZ_FORMAT_ERROR;
1069 }
1070 }
1071 }
1072
1073 if (err == MZ_OK)
1074 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001075 mz_zip_print("Zip - Read cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001076 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1077
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001078 /* Verify central directory signature exists at offset */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001079 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1080 if (err == MZ_OK)
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001081 err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001082 if ((err == MZ_OK) && (zip->cd_signature != MZ_ZIP_MAGIC_CENTRALHEADER))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001083 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001084 /* If cd exists in large file and no zip-64 support, error for recover */
1085 if (eocd_pos > UINT32_MAX && eocd_pos64 == 0)
1086 err = MZ_FORMAT_ERROR;
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001087 /* If cd not found attempt to seek backward to find it */
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001088 if (err == MZ_OK)
1089 err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001090 if (err == MZ_OK)
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001091 err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001092 if ((err == MZ_OK) && (zip->cd_signature == MZ_ZIP_MAGIC_CENTRALHEADER))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001093 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001094
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001095 /* If found compensate for incorrect locations */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001096 value64i = zip->cd_offset;
1097 zip->cd_offset = eocd_pos - zip->cd_size;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001098 /* Assume disk has prepended data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001099 zip->disk_offset_shift = zip->cd_offset - value64i;
1100 }
1101 }
1102 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001103
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001104 if (err == MZ_OK)
1105 {
1106 if (eocd_pos < zip->cd_offset)
1107 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001108 /* End of central dir should always come after central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001109 err = MZ_FORMAT_ERROR;
1110 }
1111 else if (eocd_pos < zip->cd_offset + zip->cd_size)
1112 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001113 /* Truncate size of cd if incorrect size or offset provided */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001114 zip->cd_size = eocd_pos - zip->cd_offset;
1115 }
1116 }
1117
1118 return err;
1119}
1120
1121static int32_t mz_zip_write_cd(void *handle)
1122{
1123 mz_zip *zip = (mz_zip *)handle;
1124 int64_t zip64_eocd_pos_inzip = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001125 int64_t disk_number = 0;
1126 int64_t disk_size = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001127 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001128 int32_t err = MZ_OK;
1129
1130
1131 if (zip == NULL)
1132 return MZ_PARAM_ERROR;
1133
1134 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
1135 zip->disk_number_with_cd = (uint32_t)disk_number;
1136 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
1137 zip->disk_number_with_cd += 1;
1138 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1139
1140 zip->cd_offset = mz_stream_tell(zip->stream);
1141 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
1142 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
1143 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
1144
1145 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
1146
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001147 mz_zip_print("Zip - Write cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001148 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1149
Antoine Cœur61f224a2019-05-02 01:42:09 +08001150 if (zip->cd_size == 0 && zip->number_entry > 0)
Nathan Moinvaziri64faa252019-04-21 21:38:48 -07001151 {
1152 // Zip does not contain central directory, open with recovery option
1153 return MZ_FORMAT_ERROR;
1154 }
1155
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001156 /* Write the ZIP64 central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001157 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
1158 {
1159 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
1160
1161 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
1162
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001163 /* Size of this 'zip64 end of central directory' */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001164 if (err == MZ_OK)
1165 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001166 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001167 if (err == MZ_OK)
1168 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001169 /* Version needed */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001170 if (err == MZ_OK)
1171 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001172 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001173 if (err == MZ_OK)
1174 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001175 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001176 if (err == MZ_OK)
1177 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001178 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001179 if (err == MZ_OK)
1180 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001181 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001182 if (err == MZ_OK)
1183 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001184 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001185 if (err == MZ_OK)
1186 err = mz_stream_write_int64(zip->stream, zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001187 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001188 if (err == MZ_OK)
1189 err = mz_stream_write_int64(zip->stream, zip->cd_offset);
1190 if (err == MZ_OK)
1191 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
1192
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001193 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001194 if (err == MZ_OK)
1195 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001196 /* Relative offset to the end of zip64 central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001197 if (err == MZ_OK)
1198 err = mz_stream_write_int64(zip->stream, zip64_eocd_pos_inzip);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001199 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001200 if (err == MZ_OK)
1201 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
1202 }
1203
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001204 /* Write the central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001205
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001206 /* Signature */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001207 if (err == MZ_OK)
1208 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001209 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001210 if (err == MZ_OK)
1211 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001212 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001213 if (err == MZ_OK)
1214 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001215 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001216 if (err == MZ_OK)
1217 {
1218 if (zip->number_entry >= UINT16_MAX)
1219 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1220 else
1221 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1222 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001223 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001224 if (err == MZ_OK)
1225 {
1226 if (zip->number_entry >= UINT16_MAX)
1227 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1228 else
1229 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1230 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001231 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001232 if (err == MZ_OK)
1233 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001234 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001235 if (err == MZ_OK)
1236 {
1237 if (zip->cd_offset >= UINT32_MAX)
1238 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
1239 else
1240 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
1241 }
1242
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001243 /* Write global comment */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001244 if (zip->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001245 {
1246 comment_size = (int32_t)strlen(zip->comment);
1247 if (comment_size > UINT16_MAX)
1248 comment_size = UINT16_MAX;
1249 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001250 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001251 err = mz_stream_write_uint16(zip->stream, (uint16_t)comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001252 if (err == MZ_OK)
1253 {
1254 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
1255 err = MZ_READ_ERROR;
1256 }
1257 return err;
1258}
1259
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001260static int32_t mz_zip_recover_cd(void *handle)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001261{
1262 mz_zip *zip = (mz_zip *)handle;
1263 mz_zip_file local_file_info;
1264 void *local_file_info_stream = NULL;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001265 void *cd_mem_stream = NULL;
1266 uint64_t number_entry = 0;
1267 int64_t descriptor_pos = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001268 int64_t next_header_pos = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001269 int64_t disk_offset = 0;
1270 int64_t disk_number = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001271 int64_t compressed_pos = 0;
1272 int64_t compressed_end_pos = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001273 int64_t compressed_size = 0;
1274 int64_t uncompressed_size = 0;
1275 uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001276 uint8_t local_header_magic[4] = MZ_ZIP_MAGIC_LOCALHEADERU8;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001277 uint8_t central_header_magic[4] = MZ_ZIP_MAGIC_CENTRALHEADERU8;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001278 uint32_t crc32 = 0;
1279 int32_t disk_number_with_cd = 0;
1280 int32_t err = MZ_OK;
1281 uint8_t zip64 = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001282 uint8_t eof = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001283
1284
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001285 mz_zip_print("Zip - Recover - Start\n");
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001286
1287 mz_zip_get_cd_mem_stream(handle, &cd_mem_stream);
1288
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001289 /* Determine if we are on a split disk or not */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001290 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0);
1291 if (mz_stream_tell(zip->stream) < 0)
1292 {
1293 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1294 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1295 }
1296 else
1297 disk_number_with_cd = 1;
1298
1299 if (mz_stream_is_open(cd_mem_stream) != MZ_OK)
1300 err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001301
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001302 mz_stream_mem_create(&local_file_info_stream);
1303 mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri57cbcd22019-10-21 15:37:58 -07001304
1305 if (err == MZ_OK)
1306 {
1307 err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001308 INT64_MAX, &next_header_pos);
Nathan Moinvaziri57cbcd22019-10-21 15:37:58 -07001309 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001310
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001311 while (err == MZ_OK && !eof)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001312 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001313 /* Get current offset and disk number for central dir record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001314 disk_offset = mz_stream_tell(zip->stream);
1315 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1316
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001317 /* Read local headers */
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001318 memset(&local_file_info, 0, sizeof(local_file_info));
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001319 err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream);
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001320 if (err != MZ_OK)
1321 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001322
1323 local_file_info.disk_offset = disk_offset;
1324 if (disk_number < 0)
1325 disk_number = 0;
1326 local_file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001327
1328 compressed_pos = mz_stream_tell(zip->stream);
1329
1330 if ((err == MZ_OK) && (local_file_info.compressed_size > 0))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001331 {
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001332 mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001333 }
1334
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001335 /* Search for the next local header */
1336 err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
1337 INT64_MAX, &next_header_pos);
1338
1339 if (err == MZ_EXIST_ERROR)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001340 {
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001341 mz_stream_seek(zip->stream, compressed_pos, MZ_SEEK_SET);
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001342
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001343 /* Search for central dir if no local header found */
1344 err = mz_stream_find(zip->stream, (const void *)central_header_magic, sizeof(central_header_magic),
1345 INT64_MAX, &next_header_pos);
1346
1347 if (err == MZ_EXIST_ERROR)
1348 {
1349 /* Get end of stream if no central header found */
1350 mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1351 next_header_pos = mz_stream_tell(zip->stream);
1352 }
1353
1354 eof = 1;
1355 }
1356
1357 /* Search backwards for the descriptor */
1358 err = mz_stream_find_reverse(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
1359 INT8_MAX, &descriptor_pos);
1360 if (err == MZ_OK)
1361 {
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001362 if (mz_zip_extrafield_contains(local_file_info.extrafield,
1363 local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001364 zip64 = 1;
1365
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001366 err = mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32,
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001367 &compressed_size, &uncompressed_size);
1368
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001369 if (err == MZ_OK)
1370 {
1371 if (local_file_info.crc == 0)
1372 local_file_info.crc = crc32;
1373 if (local_file_info.compressed_size == 0)
1374 local_file_info.compressed_size = compressed_size;
1375 if (local_file_info.uncompressed_size == 0)
1376 local_file_info.uncompressed_size = uncompressed_size;
1377 }
1378
1379 compressed_end_pos = descriptor_pos;
1380 }
1381 else
1382 {
1383 compressed_end_pos = next_header_pos;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001384 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001385
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001386 compressed_size = compressed_end_pos - compressed_pos;
1387
1388 if (compressed_size > UINT32_MAX)
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001389 {
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001390 /* Update sizes if 4GB file is written with no ZIP64 support */
1391 if (local_file_info.uncompressed_size < UINT32_MAX)
1392 {
1393 local_file_info.compressed_size = compressed_size;
1394 local_file_info.uncompressed_size = 0;
1395 }
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001396 }
1397
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001398 mz_zip_print("Zip - Recover - Entry %s (csize %" PRId64 " usize %" PRId64 " flags 0x%" PRIx16 ")\n",
1399 local_file_info.filename, local_file_info.compressed_size, local_file_info.uncompressed_size,
1400 local_file_info.flag);
1401
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001402 /* Rewrite central dir with local headers and offsets */
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001403 err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001404 if (err == MZ_OK)
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001405 number_entry += 1;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001406
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001407 err = mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001408 }
1409
1410 mz_stream_mem_delete(&local_file_info_stream);
1411
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001412 mz_zip_print("Zip - Recover - Complete (cddisk %" PRId32 " entries %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001413 disk_number_with_cd, number_entry);
1414
1415 if (number_entry == 0)
1416 return err;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001417
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001418 /* Set new upper seek boundary for central dir mem stream */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001419 disk_offset = mz_stream_tell(cd_mem_stream);
1420 mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset);
1421
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001422 /* Set new central directory info */
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001423 mz_zip_set_cd_stream(handle, 0, cd_mem_stream);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001424 mz_zip_set_number_entry(handle, number_entry);
1425 mz_zip_set_disk_number_with_cd(handle, disk_number_with_cd);
1426
1427 return MZ_OK;
1428}
1429
1430void *mz_zip_create(void **handle)
1431{
1432 mz_zip *zip = NULL;
1433
1434 zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
1435 if (zip != NULL)
1436 memset(zip, 0, sizeof(mz_zip));
1437 if (handle != NULL)
1438 *handle = zip;
1439
1440 return zip;
1441}
1442
1443void mz_zip_delete(void **handle)
1444{
1445 mz_zip *zip = NULL;
1446 if (handle == NULL)
1447 return;
1448 zip = (mz_zip *)*handle;
1449 if (zip != NULL)
1450 {
1451 MZ_FREE(zip);
1452 }
1453 *handle = NULL;
1454}
1455
1456int32_t mz_zip_open(void *handle, void *stream, int32_t mode)
1457{
1458 mz_zip *zip = (mz_zip *)handle;
1459 int32_t err = MZ_OK;
1460
1461
1462 if (zip == NULL)
1463 return MZ_PARAM_ERROR;
1464
1465 mz_zip_print("Zip - Open\n");
1466
1467 zip->stream = stream;
1468
1469 mz_stream_mem_create(&zip->cd_mem_stream);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001470
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001471 if (mode & MZ_OPEN_MODE_WRITE)
1472 {
1473 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1474 zip->cd_stream = zip->cd_mem_stream;
1475 }
1476 else
1477 {
1478 zip->cd_stream = stream;
1479 }
1480
1481 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
1482 {
1483 if ((mode & MZ_OPEN_MODE_CREATE) == 0)
1484 {
1485 err = mz_zip_read_cd(zip);
1486 if (err != MZ_OK)
1487 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001488 mz_zip_print("Zip - Error detected reading cd (%" PRId32 ")\n", err);
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001489 if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001490 err = MZ_OK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001491 }
1492 }
1493
1494 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
1495 {
1496 if (zip->cd_size > 0)
1497 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001498 /* Store central directory in memory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001499 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1500 if (err == MZ_OK)
1501 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size);
1502 if (err == MZ_OK)
1503 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1504 }
1505 else
1506 {
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001507 if (zip->cd_signature == MZ_ZIP_MAGIC_ENDHEADER)
1508 {
1509 /* If tiny zip then overwrite end header */
1510 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1511 }
1512 else
1513 {
1514 /* If no central directory, append new zip to end of file */
1515 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1516 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001517 }
Nathan Moinvaziri2ce1fe22018-11-23 11:53:11 -08001518
1519 if (zip->disk_number_with_cd > 0)
1520 {
1521 /* Move to last disk to begin appending */
1522 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->disk_number_with_cd - 1);
1523 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001524 }
1525 else
1526 {
1527 zip->cd_start_pos = zip->cd_offset;
1528 }
1529 }
1530
1531 if (err != MZ_OK)
1532 {
1533 mz_zip_close(zip);
1534 return err;
1535 }
1536
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001537 /* Memory streams used to store variable length file info data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001538 mz_stream_mem_create(&zip->file_info_stream);
1539 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1540
1541 mz_stream_mem_create(&zip->local_file_info_stream);
1542 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1543
1544 zip->open_mode = mode;
1545
1546 return err;
1547}
1548
1549int32_t mz_zip_close(void *handle)
1550{
1551 mz_zip *zip = (mz_zip *)handle;
1552 int32_t err = MZ_OK;
1553
1554 if (zip == NULL)
1555 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001556
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001557 mz_zip_print("Zip - Close\n");
1558
1559 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001560 err = mz_zip_entry_close(handle);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001561
1562 if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE))
1563 err = mz_zip_write_cd(handle);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001564
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001565 if (zip->cd_mem_stream != NULL)
1566 {
1567 mz_stream_close(zip->cd_mem_stream);
1568 mz_stream_delete(&zip->cd_mem_stream);
1569 }
1570
1571 if (zip->file_info_stream != NULL)
1572 {
1573 mz_stream_mem_close(zip->file_info_stream);
1574 mz_stream_mem_delete(&zip->file_info_stream);
1575 }
1576 if (zip->local_file_info_stream != NULL)
1577 {
1578 mz_stream_mem_close(zip->local_file_info_stream);
1579 mz_stream_mem_delete(&zip->local_file_info_stream);
1580 }
1581
1582 if (zip->comment)
1583 {
1584 MZ_FREE(zip->comment);
1585 zip->comment = NULL;
1586 }
1587
1588 zip->stream = NULL;
1589 zip->cd_stream = NULL;
1590
1591 return err;
1592}
1593
1594int32_t mz_zip_get_comment(void *handle, const char **comment)
1595{
1596 mz_zip *zip = (mz_zip *)handle;
1597 if (zip == NULL || comment == NULL)
1598 return MZ_PARAM_ERROR;
1599 if (zip->comment == NULL)
1600 return MZ_EXIST_ERROR;
1601 *comment = zip->comment;
1602 return MZ_OK;
1603}
1604
1605int32_t mz_zip_set_comment(void *handle, const char *comment)
1606{
1607 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001608 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001609 if (zip == NULL || comment == NULL)
1610 return MZ_PARAM_ERROR;
1611 if (zip->comment != NULL)
1612 MZ_FREE(zip->comment);
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001613 comment_size = (int32_t)strlen(comment);
1614 if (comment_size > UINT16_MAX)
1615 return MZ_PARAM_ERROR;
Nathan Moinvaziria6ecab52019-01-22 13:14:01 -08001616 zip->comment = (char *)MZ_ALLOC(comment_size+1);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001617 if (zip->comment == NULL)
1618 return MZ_MEM_ERROR;
Nathan Moinvaziria6ecab52019-01-22 13:14:01 -08001619 memset(zip->comment, 0, comment_size+1);
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001620 strncpy(zip->comment, comment, comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001621 return MZ_OK;
1622}
1623
1624int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
1625{
1626 mz_zip *zip = (mz_zip *)handle;
1627 if (zip == NULL || version_madeby == NULL)
1628 return MZ_PARAM_ERROR;
1629 *version_madeby = zip->version_madeby;
1630 return MZ_OK;
1631}
1632
1633int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
1634{
1635 mz_zip *zip = (mz_zip *)handle;
1636 if (zip == NULL)
1637 return MZ_PARAM_ERROR;
1638 zip->version_madeby = version_madeby;
1639 return MZ_OK;
1640}
1641
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001642int32_t mz_zip_set_recover(void *handle, uint8_t recover)
1643{
1644 mz_zip *zip = (mz_zip *)handle;
1645 if (zip == NULL)
1646 return MZ_PARAM_ERROR;
1647 zip->recover = recover;
1648 return MZ_OK;
1649}
1650
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001651int32_t mz_zip_get_stream(void *handle, void **stream)
1652{
1653 mz_zip *zip = (mz_zip *)handle;
1654 if (zip == NULL || stream == NULL)
1655 return MZ_PARAM_ERROR;
1656 *stream = zip->stream;
1657 if (*stream == NULL)
1658 return MZ_EXIST_ERROR;
1659 return MZ_OK;
1660}
1661
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001662int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001663{
1664 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001665 if (zip == NULL || cd_stream == NULL)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001666 return MZ_PARAM_ERROR;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001667 zip->cd_offset = 0;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001668 zip->cd_stream = cd_stream;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001669 zip->cd_start_pos = cd_start_pos;
1670 return MZ_OK;
1671}
1672
1673int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream)
1674{
1675 mz_zip *zip = (mz_zip *)handle;
1676 if (zip == NULL || cd_mem_stream == NULL)
1677 return MZ_PARAM_ERROR;
1678 *cd_mem_stream = zip->cd_mem_stream;
1679 if (*cd_mem_stream == NULL)
1680 return MZ_EXIST_ERROR;
1681 return MZ_OK;
1682}
1683
1684static int32_t mz_zip_entry_close_int(void *handle)
1685{
1686 mz_zip *zip = (mz_zip *)handle;
1687
1688 if (zip->crypt_stream != NULL)
1689 mz_stream_delete(&zip->crypt_stream);
1690 zip->crypt_stream = NULL;
1691 if (zip->compress_stream != NULL)
1692 mz_stream_delete(&zip->compress_stream);
1693 zip->compress_stream = NULL;
1694
1695 zip->entry_opened = 0;
1696
1697 return MZ_OK;
1698}
1699
Nathan Moinvaziri26308b22018-08-08 09:40:15 -07001700static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress_level, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001701{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001702 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001703 int64_t max_total_in = 0;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001704 int64_t header_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001705 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001706 int32_t err = MZ_OK;
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001707 uint8_t use_crypt = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001708
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001709 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001710 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001711
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001712 switch (zip->file_info.compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001713 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001714 case MZ_COMPRESS_METHOD_STORE:
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001715 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001716#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001717 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001718#endif
Nathan Moinvaziri566dfe02018-10-26 00:36:30 -07001719#ifdef HAVE_LZMA
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001720 case MZ_COMPRESS_METHOD_LZMA:
1721#endif
1722 err = MZ_OK;
1723 break;
1724 default:
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001725 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001726 }
1727
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001728#ifndef HAVE_WZAES
1729 if (zip->file_info.aes_version)
1730 return MZ_SUPPORT_ERROR;
1731#endif
1732
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001733 zip->entry_raw = raw;
1734
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001735 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password != NULL))
1736 {
1737 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
1738 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001739 /* Encrypt only when we are not trying to write raw and password is supplied. */
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001740 if (!zip->entry_raw)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001741 use_crypt = 1;
1742 }
1743 else if (zip->open_mode & MZ_OPEN_MODE_READ)
1744 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001745 /* Decrypt only when password is supplied. Don't error when password */
1746 /* is not supplied as we may want to read the raw encrypted data. */
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001747 use_crypt = 1;
1748 }
1749 }
1750
1751 if ((err == MZ_OK) && (use_crypt))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001752 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001753#ifdef HAVE_WZAES
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001754 if (zip->file_info.aes_version)
1755 {
Nathan Moinvaziri21a31022018-10-24 09:50:16 -07001756 mz_stream_wzaes_create(&zip->crypt_stream);
1757 mz_stream_wzaes_set_password(zip->crypt_stream, password);
1758 mz_stream_wzaes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001759 }
1760 else
1761#endif
1762 {
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001763#ifdef HAVE_PKCRYPT
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001764 uint8_t verify1 = 0;
1765 uint8_t verify2 = 0;
1766
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001767 /* Info-ZIP modification to ZipCrypto format: */
1768 /* If bit 3 of the general purpose bit flag is set, it uses high byte of 16-bit File Time. */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001769
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001770 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1771 {
1772 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001773
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001774 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1775
1776 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1777 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1778 }
1779 else
1780 {
1781 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1782 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1783 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001784
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001785 mz_stream_pkcrypt_create(&zip->crypt_stream);
1786 mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1787 mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001788#endif
1789 }
1790 }
1791
1792 if (err == MZ_OK)
1793 {
1794 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001795 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001796
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001797 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001798
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001799 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001800 }
1801
1802 if (err == MZ_OK)
1803 {
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001804 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001805 mz_stream_raw_create(&zip->compress_stream);
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001806#if defined(HAVE_ZLIB) || defined(HAVE_LIBCOMP)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001807 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001808 mz_stream_zlib_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001809#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001810#ifdef HAVE_BZIP2
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001811 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001812 mz_stream_bzip_create(&zip->compress_stream);
1813#endif
1814#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001815 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001816 mz_stream_lzma_create(&zip->compress_stream);
1817#endif
1818 else
1819 err = MZ_PARAM_ERROR;
1820 }
1821
1822 if (err == MZ_OK)
1823 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001824 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001825 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001826 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001827 }
1828 else
1829 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001830#ifndef HAVE_LIBCOMP
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001831 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE || zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
Nathan Moinvaziri90d31c72018-11-20 03:17:20 -08001832#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001833 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001834 max_total_in = zip->file_info.compressed_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001835 mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1836
1837 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1838 max_total_in -= header_size;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001839 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1840 max_total_in -= footer_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001841
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001842 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001843 }
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001844 if ((zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA) && (zip->file_info.flag & MZ_ZIP_FLAG_LZMA_EOS_MARKER) == 0)
juanii55bcdaf2018-02-11 20:55:57 -03001845 {
1846 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1847 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1848 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001849 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001850
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001851 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1852
1853 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1854 }
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001855
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001856 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001857 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001858 zip->entry_opened = 1;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001859 zip->entry_crc32 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001860 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001861 else
1862 {
1863 mz_zip_entry_close_int(handle);
1864 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001865
1866 return err;
1867}
1868
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001869int32_t mz_zip_entry_is_open(void *handle)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001870{
1871 mz_zip *zip = (mz_zip *)handle;
1872 if (zip == NULL)
1873 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001874 if (zip->entry_opened == 0)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001875 return MZ_EXIST_ERROR;
1876 return MZ_OK;
1877}
1878
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001879static int32_t mz_zip_seek_to_local_header(void *handle)
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001880{
1881 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001882
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001883
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001884 if (zip->file_info.disk_number == zip->disk_number_with_cd)
1885 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1886 else
1887 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001888
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001889 mz_zip_print("Zip - Entry - Seek local (disk %" PRId32 " offset %" PRId64 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001890 zip->file_info.disk_number, zip->file_info.disk_offset);
Nathan Moinvaziri09725902019-02-20 09:15:48 -08001891
1892 /* Guard against seek overflows */
1893 if ((zip->disk_offset_shift > 0) &&
1894 (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
1895 return MZ_FORMAT_ERROR;
1896
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001897 return mz_stream_seek(zip->stream, zip->file_info.disk_offset + zip->disk_offset_shift, MZ_SEEK_SET);
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001898}
1899
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001900int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001901{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001902 mz_zip *zip = (mz_zip *)handle;
1903 int32_t err = MZ_OK;
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001904 int32_t err_shift = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001905
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001906#if defined(MZ_ZIP_NO_ENCRYPTION)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001907 if (password != NULL)
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001908 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001909#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001910 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001911 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001912 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001913 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001914 if (zip->entry_scanned == 0)
1915 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001916
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001917 mz_zip_print("Zip - Entry - Read open (raw %" PRId32 ")\n", raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001918
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001919 err = mz_zip_seek_to_local_header(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001920 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001921 err = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001922
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001923 if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0)
1924 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001925 /* Perhaps we didn't compensated correctly for incorrect cd offset */
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001926 err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
1927 if (err_shift == MZ_OK)
1928 err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
1929 if (err_shift == MZ_OK)
1930 {
1931 zip->disk_offset_shift = 0;
1932 err = err_shift;
1933 }
1934 }
1935
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001936#ifdef MZ_ZIP_NO_DECOMPRESSION
Nathan Moinvaziri2eb29072018-11-23 10:32:21 -08001937 if (!raw && zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001938 err = MZ_SUPPORT_ERROR;
Viktor Szakatse7215072018-09-04 15:06:53 +00001939#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001940 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001941 err = mz_zip_entry_open_int(handle, raw, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001942
1943 return err;
1944}
1945
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001946int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int16_t compress_level, uint8_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001947{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001948 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001949 int64_t filename_pos = -1;
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001950 int64_t extrafield_pos = 0;
1951 int64_t comment_pos = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07001952 int64_t linkname_pos = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001953 int64_t disk_number = 0;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001954 uint8_t is_dir = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001955 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001956
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001957#if defined(MZ_ZIP_NO_ENCRYPTION)
tz-lomb1b25802017-11-10 15:03:02 +03001958 if (password != NULL)
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001959 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001960#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001961 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001962 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001963
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001964 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001965 {
1966 err = mz_zip_entry_close(handle);
1967 if (err != MZ_OK)
1968 return err;
1969 }
1970
1971 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001972
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001973 mz_zip_print("Zip - Entry - Write open - %s (level %" PRId16 " raw %" PRId8 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001974 zip->file_info.filename, compress_level, raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001975
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001976 mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001977 mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
1978
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001979 /* Copy filename, extrafield, and comment internally */
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001980 filename_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001981 if (file_info->filename != NULL)
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001982 mz_stream_write(zip->file_info_stream, file_info->filename, (int32_t)strlen(file_info->filename));
1983 mz_stream_write_uint8(zip->file_info_stream, 0);
1984
1985 extrafield_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001986 if (file_info->extrafield != NULL)
Nathan Moinvazirica014e62018-08-15 07:31:12 -07001987 mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001988 mz_stream_write_uint8(zip->file_info_stream, 0);
1989
1990 comment_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001991 if (file_info->comment != NULL)
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001992 mz_stream_write(zip->file_info_stream, file_info->comment, file_info->comment_size);
1993 mz_stream_write_uint8(zip->file_info_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001994
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07001995 linkname_pos = mz_stream_tell(zip->file_info_stream);
1996 if (file_info->linkname != NULL)
1997 mz_stream_write(zip->file_info_stream, file_info->linkname, (int32_t)strlen(file_info->linkname));
1998 mz_stream_write_uint8(zip->file_info_stream, 0);
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001999
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08002000 mz_stream_mem_get_buffer_at(zip->file_info_stream, filename_pos, (const void **)&zip->file_info.filename);
2001 mz_stream_mem_get_buffer_at(zip->file_info_stream, extrafield_pos, (const void **)&zip->file_info.extrafield);
2002 mz_stream_mem_get_buffer_at(zip->file_info_stream, comment_pos, (const void **)&zip->file_info.comment);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002003 mz_stream_mem_get_buffer_at(zip->file_info_stream, linkname_pos, (const void **)&zip->file_info.linkname);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002004
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002005 if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002006 {
2007 if ((compress_level == 8) || (compress_level == 9))
2008 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
2009 if (compress_level == 2)
2010 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
2011 if (compress_level == 1)
2012 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
2013 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002014#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002015 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002016 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002017#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002018
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002019 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2020 is_dir = 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002021
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002022 if (!is_dir)
2023 {
2024 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
2025 if (password != NULL)
2026 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
2027 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002028
juaniib8887e92018-02-14 00:51:05 -03002029 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
2030 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002031
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002032 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07002033 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002034 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002035
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08002036#ifdef HAVE_WZAES
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002037 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
2038 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
2039#endif
2040
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002041 if ((compress_level == 0) || (is_dir))
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002042 zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
Viktor Szakats2884e672018-04-30 08:12:13 +00002043
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07002044#ifdef MZ_ZIP_NO_COMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002045 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002046 err = MZ_SUPPORT_ERROR;
2047#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002048 if (err == MZ_OK)
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002049 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002050 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002051 err = mz_zip_entry_open_int(handle, raw, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002052
2053 return err;
2054}
2055
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002056int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002057{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002058 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002059 int32_t read = 0;
2060
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002061 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002062 return MZ_PARAM_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002063 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) /* zlib limitation */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002064 return MZ_PARAM_ERROR;
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07002065 if (len == 0)
2066 return MZ_PARAM_ERROR;
2067
Nathan Moinvazirieb80cd92018-07-11 16:45:09 -07002068 if (zip->file_info.compressed_size == 0)
2069 return 0;
2070
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002071 /* Read entire entry even if uncompressed_size = 0, otherwise */
2072 /* aes encryption validation will fail if compressed_size > 0 */
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07002073 read = mz_stream_read(zip->compress_stream, buf, len);
2074 if (read > 0)
2075 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002076
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002077 mz_zip_print("Zip - Entry - Read - %" PRId32 " (max %" PRId32 ")\n", read, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002078
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002079 return read;
2080}
2081
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002082int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002083{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002084 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002085 int32_t written = 0;
2086
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002087 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002088 return MZ_PARAM_ERROR;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07002089 written = mz_stream_write(zip->compress_stream, buf, len);
2090 if (written > 0)
2091 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002092
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002093 mz_zip_print("Zip - Entry - Write - %" PRId32 " (max %" PRId32 ")\n", written, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002094
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002095 return written;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002096}
2097
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002098int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002099 int64_t *uncompressed_size)
2100{
2101 mz_zip *zip = (mz_zip *)handle;
2102 int64_t total_in = 0;
2103 int32_t err = MZ_OK;
2104 uint8_t zip64 = 0;
2105
2106 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2107 return MZ_PARAM_ERROR;
2108
2109 mz_stream_close(zip->compress_stream);
2110
2111 mz_zip_print("Zip - Entry - Read Close\n");
2112
2113 if (crc32 != NULL)
2114 *crc32 = zip->file_info.crc;
2115 if (compressed_size != NULL)
2116 *compressed_size = zip->file_info.compressed_size;
2117 if (uncompressed_size != NULL)
2118 *uncompressed_size = zip->file_info.uncompressed_size;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002119
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002120 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
2121
2122 if ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) &&
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002123 ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0) &&
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002124 (crc32 != NULL || compressed_size != NULL || uncompressed_size != NULL))
2125 {
2126 /* Check to see if data descriptor is zip64 bit format or not */
2127 if (mz_zip_extrafield_contains(zip->local_file_info.extrafield,
2128 zip->local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
2129 zip64 = 1;
2130
2131 err = mz_zip_seek_to_local_header(handle);
2132
2133 /* Seek to end of compressed stream since we might have over-read during compression */
2134 if (err == MZ_OK)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002135 err = mz_stream_seek(zip->stream, MZ_ZIP_SIZE_LD_ITEM +
2136 (int64_t)zip->local_file_info.filename_size +
2137 (int64_t)zip->local_file_info.extrafield_size +
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002138 total_in, MZ_SEEK_CUR);
2139
2140 /* Read data descriptor */
2141 if (err == MZ_OK)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002142 err = mz_zip_entry_read_descriptor(zip->stream, zip64,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002143 crc32, compressed_size, uncompressed_size);
2144 }
2145
2146 /* If entire entry was not read verification will fail */
2147 if ((err == MZ_OK) && (total_in > 0) && (!zip->entry_raw))
2148 {
2149#ifdef HAVE_WZAES
2150 /* AES zip version AE-1 will expect a valid crc as well */
2151 if (zip->file_info.aes_version <= 0x0001)
2152#endif
2153 {
2154 if (zip->entry_crc32 != zip->file_info.crc)
2155 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002156 mz_zip_print("Zip - Entry - Crc failed (actual 0x%08" PRIx32 " expected 0x%08" PRIx32 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002157 zip->entry_crc32, zip->file_info.crc);
2158
2159 err = MZ_CRC_ERROR;
2160 }
2161 }
2162 }
2163
2164 mz_zip_entry_close_int(handle);
2165
2166 return err;
2167}
2168
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002169int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002170 int64_t uncompressed_size)
2171{
2172 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002173 int32_t err = MZ_OK;
2174 uint8_t zip64 = 0;
2175
2176 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2177 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002178
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002179 mz_stream_close(zip->compress_stream);
2180
2181 if (!zip->entry_raw)
2182 crc32 = zip->entry_crc32;
2183
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002184 mz_zip_print("Zip - Entry - Write Close (crc 0x%08" PRIx32 " cs %" PRId64 " ucs %" PRId64 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002185 crc32, compressed_size, uncompressed_size);
2186
2187 /* If sizes are not set, then read them from the compression stream */
2188 if (compressed_size < 0)
2189 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2190 if (uncompressed_size < 0)
Nathan Moinvazirid9318432018-12-01 07:56:28 -08002191 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002192
2193 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
2194 {
2195 mz_stream_set_base(zip->crypt_stream, zip->stream);
2196 err = mz_stream_close(zip->crypt_stream);
2197
2198 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2199 }
2200
2201 if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
2202 {
2203 /* Determine if we need to write data descriptor in zip64 format,
2204 if local extrafield was saved with zip64 extrafield */
2205 if (zip->file_info.zip64 == MZ_ZIP64_AUTO)
2206 {
2207 if (zip->file_info.uncompressed_size >= UINT32_MAX)
2208 zip64 = 1;
2209 if (zip->file_info.compressed_size >= UINT32_MAX)
2210 zip64 = 1;
2211 if (zip->file_info.disk_offset >= UINT32_MAX)
2212 zip64 = 1;
2213 else if (zip->file_info.uncompressed_size == 0)
2214 zip64 = 1;
2215 }
2216 else if (zip->file_info.zip64 == MZ_ZIP64_FORCE)
2217 {
2218 zip64 = 1;
2219 }
2220
2221 if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002222 err = mz_zip_entry_write_descriptor(zip->stream,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002223 zip64, 0, compressed_size, 0);
2224 else
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002225 err = mz_zip_entry_write_descriptor(zip->stream,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002226 zip64, crc32, compressed_size, uncompressed_size);
2227 }
2228
2229 /* Write file info to central directory */
2230
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002231 mz_zip_print("Zip - Entry - Write cd (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002232 uncompressed_size, compressed_size, crc32);
2233
2234 zip->file_info.crc = crc32;
2235 zip->file_info.compressed_size = compressed_size;
2236 zip->file_info.uncompressed_size = uncompressed_size;
2237
2238 if (err == MZ_OK)
2239 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
2240
2241 zip->number_entry += 1;
2242
2243 mz_zip_entry_close_int(handle);
2244
2245 return err;
2246}
2247
2248int32_t mz_zip_entry_is_dir(void *handle)
2249{
2250 mz_zip *zip = (mz_zip *)handle;
2251 int32_t filename_length = 0;
2252
2253 if (zip == NULL)
2254 return MZ_PARAM_ERROR;
2255 if (zip->entry_scanned == 0)
2256 return MZ_PARAM_ERROR;
2257 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2258 return MZ_OK;
2259
2260 filename_length = (int32_t)strlen(zip->file_info.filename);
2261 if (filename_length > 0)
2262 {
2263 if ((zip->file_info.filename[filename_length - 1] == '/') ||
2264 (zip->file_info.filename[filename_length - 1] == '\\'))
2265 return MZ_OK;
2266 }
2267 return MZ_EXIST_ERROR;
2268}
2269
Nathan Moinvaziri3249eac2019-05-02 21:07:39 -07002270int32_t mz_zip_entry_is_symlink(void *handle)
2271{
2272 mz_zip *zip = (mz_zip *)handle;
2273
2274 if (zip == NULL)
2275 return MZ_PARAM_ERROR;
2276 if (zip->entry_scanned == 0)
2277 return MZ_PARAM_ERROR;
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002278 if (mz_zip_attrib_is_symlink(zip->file_info.external_fa, zip->file_info.version_madeby) != MZ_OK)
2279 return MZ_EXIST_ERROR;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002280 if (zip->file_info.linkname == NULL || *zip->file_info.linkname == 0)
2281 return MZ_EXIST_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002282
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002283 return MZ_OK;
Nathan Moinvaziri3249eac2019-05-02 21:07:39 -07002284}
2285
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002286int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002287{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002288 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002289
2290 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002291 return MZ_PARAM_ERROR;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002292
2293 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
2294 {
2295 if (!zip->entry_scanned)
2296 return MZ_PARAM_ERROR;
2297 }
2298
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002299 *file_info = &zip->file_info;
2300 return MZ_OK;
2301}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002302
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002303int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002304{
2305 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002306 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002307 return MZ_PARAM_ERROR;
2308 *local_file_info = &zip->local_file_info;
2309 return MZ_OK;
2310}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002311
Nathan Moinvaziri915c5132018-10-26 20:00:52 -07002312int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size)
2313{
2314 mz_zip *zip = (mz_zip *)handle;
2315
2316 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2317 return MZ_PARAM_ERROR;
2318
2319 zip->file_info.extrafield = extrafield;
2320 zip->file_info.extrafield_size = extrafield_size;
2321 return MZ_OK;
2322}
2323
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002324int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07002325{
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002326 return mz_zip_entry_close_raw(handle, UINT64_MAX, 0);
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07002327}
2328
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002329int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002330{
2331 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002332 int32_t err = MZ_OK;
2333
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002334 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002335 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002336
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002337 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002338 err = mz_zip_entry_write_close(handle, crc32, UINT64_MAX, uncompressed_size);
2339 else
2340 err = mz_zip_entry_read_close(handle, NULL, NULL, NULL);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002341
2342 return err;
2343}
2344
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002345static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002346{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002347 mz_zip *zip = (mz_zip *)handle;
2348 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002349
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002350 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002351 return MZ_PARAM_ERROR;
2352
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002353 zip->entry_scanned = 0;
2354
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002355 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00002356
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002357 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002358 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002359 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002360 if (err == MZ_OK)
2361 zip->entry_scanned = 1;
2362 return err;
2363}
2364
Nathan Moinvaziric565fa82018-10-19 08:48:33 -07002365int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry)
2366{
2367 mz_zip *zip = (mz_zip *)handle;
2368 if (zip == NULL)
2369 return MZ_PARAM_ERROR;
2370 zip->number_entry = number_entry;
2371 return MZ_OK;
2372}
2373
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002374int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002375{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002376 mz_zip *zip = (mz_zip *)handle;
2377 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002378 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002379 *number_entry = zip->number_entry;
2380 return MZ_OK;
2381}
2382
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002383int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd)
2384{
2385 mz_zip *zip = (mz_zip *)handle;
2386 if (zip == NULL)
2387 return MZ_PARAM_ERROR;
2388 zip->disk_number_with_cd = disk_number_with_cd;
2389 return MZ_OK;
2390}
2391
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002392int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03002393{
2394 mz_zip *zip = (mz_zip *)handle;
2395 if (zip == NULL || disk_number_with_cd == NULL)
2396 return MZ_PARAM_ERROR;
2397 *disk_number_with_cd = zip->disk_number_with_cd;
2398 return MZ_OK;
2399}
2400
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002401int64_t mz_zip_get_entry(void *handle)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002402{
2403 mz_zip *zip = (mz_zip *)handle;
2404
2405 if (zip == NULL)
2406 return MZ_PARAM_ERROR;
2407
2408 return zip->cd_current_pos;
2409}
2410
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002411int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002412{
2413 mz_zip *zip = (mz_zip *)handle;
2414
2415 if (zip == NULL)
2416 return MZ_PARAM_ERROR;
2417
2418 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
2419 return MZ_PARAM_ERROR;
2420
2421 zip->cd_current_pos = cd_pos;
2422
2423 return mz_zip_goto_next_entry_int(handle);
2424}
2425
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002426int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002427{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002428 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002429
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002430 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002431 return MZ_PARAM_ERROR;
2432
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002433 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002434
2435 return mz_zip_goto_next_entry_int(handle);
2436}
2437
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002438int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002439{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002440 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002441
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002442 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002443 return MZ_PARAM_ERROR;
2444
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002445 zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002446 zip->file_info.extrafield_size + zip->file_info.comment_size;
2447
2448 return mz_zip_goto_next_entry_int(handle);
2449}
2450
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002451int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002452{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002453 mz_zip *zip = (mz_zip *)handle;
2454 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002455 int32_t result = 0;
2456
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002457 if (zip == NULL || filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002458 return MZ_PARAM_ERROR;
2459
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002460 /* If we are already on the current entry, no need to search */
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002461 if ((zip->entry_scanned) && (zip->file_info.filename != NULL))
2462 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002463 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002464 if (result == 0)
2465 return MZ_OK;
2466 }
2467
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002468 /* Search all entries starting at the first */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002469 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002470 while (err == MZ_OK)
2471 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002472 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2473 if (result == 0)
2474 return MZ_OK;
2475
2476 err = mz_zip_goto_next_entry(handle);
2477 }
2478
2479 return err;
2480}
2481
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002482int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002483{
2484 mz_zip *zip = (mz_zip *)handle;
2485 int32_t err = MZ_OK;
2486 int32_t result = 0;
2487
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002488 /* Search first entry looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002489 err = mz_zip_goto_first_entry(handle);
2490 if (err != MZ_OK)
2491 return err;
2492
2493 result = cb(handle, userdata, &zip->file_info);
2494 if (result == 0)
2495 return MZ_OK;
2496
2497 return mz_zip_locate_next_entry(handle, userdata, cb);
2498}
2499
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002500int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002501{
2502 mz_zip *zip = (mz_zip *)handle;
2503 int32_t err = MZ_OK;
2504 int32_t result = 0;
2505
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002506 /* Search next entries looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002507 err = mz_zip_goto_next_entry(handle);
2508 while (err == MZ_OK)
2509 {
2510 result = cb(handle, userdata, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002511 if (result == 0)
2512 return MZ_OK;
2513
2514 err = mz_zip_goto_next_entry(handle);
2515 }
2516
2517 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002518}
2519
2520/***************************************************************************/
2521
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002522int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002523{
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002524 uint32_t posix_attrib = 0;
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002525 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002526 int32_t err = MZ_OK;
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002527
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002528 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2529 if (err == MZ_OK)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002530 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002531 if ((posix_attrib & 0170000) == 0040000) /* S_ISDIR */
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002532 return MZ_OK;
2533 }
2534
2535 return MZ_EXIST_ERROR;
2536}
2537
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002538int32_t mz_zip_attrib_is_symlink(uint32_t attrib, int32_t version_madeby)
2539{
2540 uint32_t posix_attrib = 0;
2541 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
2542 int32_t err = MZ_OK;
2543
2544 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2545 if (err == MZ_OK)
2546 {
2547 if ((posix_attrib & 0170000) == 0120000) /* S_ISLNK */
2548 return MZ_OK;
2549 }
2550
2551 return MZ_EXIST_ERROR;
2552}
2553
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002554int32_t mz_zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys, uint32_t *target_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002555{
2556 if (target_attrib == NULL)
2557 return MZ_PARAM_ERROR;
2558
2559 *target_attrib = 0;
2560
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002561 if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002562 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002563 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002564 {
2565 *target_attrib = src_attrib;
2566 return MZ_OK;
2567 }
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002568 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (target_sys == MZ_HOST_SYSTEM_RISCOS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002569 return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
2570 }
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002571 else if ((src_sys == MZ_HOST_SYSTEM_UNIX) || (src_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (src_sys == MZ_HOST_SYSTEM_RISCOS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002572 {
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002573 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (target_sys == MZ_HOST_SYSTEM_RISCOS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002574 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002575 /* If high bytes are set, it contains unix specific attributes */
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002576 if ((src_attrib >> 16) != 0)
2577 src_attrib >>= 16;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002578
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002579 *target_attrib = src_attrib;
2580 return MZ_OK;
2581 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002582 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002583 return mz_zip_attrib_posix_to_win32(src_attrib, target_attrib);
2584 }
2585
2586 return MZ_SUPPORT_ERROR;
2587}
2588
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002589int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002590{
2591 if (win32_attrib == NULL)
2592 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002593
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002594 *win32_attrib = 0;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002595
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002596 /* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002597 if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002598 *win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002599 /* S_IFLNK */
2600 if ((posix_attrib & 0170000) == 0120000)
2601 *win32_attrib |= 0x400; /* FILE_ATTRIBUTE_REPARSE_POINT */
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002602 /* S_IFDIR */
Nathan Moinvaziri7d94b1f2019-05-08 23:41:47 -07002603 else if ((posix_attrib & 0170000) == 0040000)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002604 *win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
2605 /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002606 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002607 *win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002608
2609 return MZ_OK;
2610}
2611
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002612int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002613{
2614 if (posix_attrib == NULL)
2615 return MZ_PARAM_ERROR;
2616
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002617 *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
2618 /* FILE_ATTRIBUTE_READONLY */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002619 if ((win32_attrib & 0x01) == 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002620 *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002621 /* FILE_ATTRIBUTE_REPARSE_POINT */
2622 if ((win32_attrib & 0x400) == 0x400)
2623 *posix_attrib |= 0120000; /* S_IFLNK */
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002624 /* FILE_ATTRIBUTE_DIRECTORY */
Nathan Moinvaziri7d94b1f2019-05-08 23:41:47 -07002625 else if ((win32_attrib & 0x10) == 0x10)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002626 *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002627 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002628 *posix_attrib |= 0100000; /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002629
2630 return MZ_OK;
2631}
2632
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002633/***************************************************************************/
2634
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002635int32_t mz_zip_extrafield_find(void *stream, uint16_t type, uint16_t *length)
2636{
2637 int32_t err = MZ_OK;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002638 uint16_t field_type = 0;
2639 uint16_t field_length = 0;
2640
2641 do
2642 {
2643 err = mz_stream_read_uint16(stream, &field_type);
2644 if (err == MZ_OK)
2645 err = mz_stream_read_uint16(stream, &field_length);
2646 if (err != MZ_OK)
2647 break;
2648
2649 if (type == field_type)
2650 {
2651 if (length != NULL)
2652 *length = field_length;
2653 return MZ_OK;
2654 }
2655
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002656 err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR);
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002657 }
2658 while (err == MZ_OK);
2659
2660 return MZ_EXIST_ERROR;
2661}
2662
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002663int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size,
2664 uint16_t type, uint16_t *length)
2665{
2666 void *file_extra_stream = NULL;
2667 int32_t err = MZ_OK;
2668
2669 if (extrafield == NULL || extrafield_size == 0)
2670 return MZ_PARAM_ERROR;
2671
2672 mz_stream_mem_create(&file_extra_stream);
2673 mz_stream_mem_set_buffer(file_extra_stream, (void *)extrafield, extrafield_size);
2674
2675 err = mz_zip_extrafield_find(file_extra_stream, type, length);
2676
2677 mz_stream_mem_delete(&file_extra_stream);
2678
2679 return err;
2680}
2681
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002682int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length)
2683{
2684 int32_t err = MZ_OK;
2685 if (type == NULL || length == NULL)
2686 return MZ_PARAM_ERROR;
2687 err = mz_stream_read_uint16(stream, type);
2688 if (err == MZ_OK)
2689 err = mz_stream_read_uint16(stream, length);
2690 return err;
2691}
2692
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002693int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002694{
2695 int32_t err = MZ_OK;
2696 err = mz_stream_write_uint16(stream, type);
2697 if (err == MZ_OK)
2698 err = mz_stream_write_uint16(stream, length);
2699 return err;
2700}
2701
2702/***************************************************************************/
2703
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002704static int32_t mz_zip_invalid_date(const struct tm *ptm)
2705{
2706#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002707 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002708 !datevalue_in_range(0, 11, ptm->tm_mon) ||
2709 !datevalue_in_range(1, 31, ptm->tm_mday) ||
2710 !datevalue_in_range(0, 23, ptm->tm_hour) ||
2711 !datevalue_in_range(0, 59, ptm->tm_min) ||
2712 !datevalue_in_range(0, 59, ptm->tm_sec));
2713#undef datevalue_in_range
2714}
2715
2716static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
2717{
2718 uint64_t date = (uint64_t)(dos_date >> 16);
2719
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002720 ptm->tm_mday = (uint16_t)(date & 0x1f);
2721 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
2722 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
2723 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
2724 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
2725 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002726 ptm->tm_isdst = -1;
2727}
2728
2729int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
2730{
2731 if (ptm == NULL)
2732 return MZ_PARAM_ERROR;
2733
2734 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
2735
2736 if (mz_zip_invalid_date(ptm))
2737 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002738 /* Invalid date stored, so don't return it */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002739 memset(ptm, 0, sizeof(struct tm));
2740 return MZ_FORMAT_ERROR;
2741 }
2742 return MZ_OK;
2743}
2744
2745time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
2746{
2747 struct tm ptm;
2748 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
2749 return mktime(&ptm);
2750}
2751
2752int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
2753{
Nathan Moinvaziricd3e9e12019-04-28 10:22:49 -07002754 struct tm ltm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002755 if (ptm == NULL)
2756 return MZ_PARAM_ERROR;
Nathan Moinvaziri2e0a20a2019-04-28 13:03:11 -07002757 if (localtime_r(&unix_time, &ltm) == NULL) /* Returns a 1900-based year */
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002758 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002759 /* Invalid date stored, so don't return it */
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002760 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002761 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002762 }
Nathan Moinvaziricd3e9e12019-04-28 10:22:49 -07002763 memcpy(ptm, &ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002764 return MZ_OK;
2765}
2766
2767uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
2768{
2769 struct tm ptm;
2770 mz_zip_time_t_to_tm(unix_time, &ptm);
2771 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
2772}
2773
2774uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
2775{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07002776 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002777
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002778 /* Years supported: */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002779
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002780 /* [00, 79] (assumed to be between 2000 and 2079) */
2781 /* [80, 207] (assumed to be between 1980 and 2107, typical output of old */
2782 /* software that does 'year-1900' to get a double digit year) */
2783 /* [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.) */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002784
2785 memcpy(&fixed_tm, ptm, sizeof(struct tm));
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002786 if (fixed_tm.tm_year >= 1980) /* range [1980, 2107] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002787 fixed_tm.tm_year -= 1980;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002788 else if (fixed_tm.tm_year >= 80) /* range [80, 207] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002789 fixed_tm.tm_year -= 80;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002790 else /* range [00, 79] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002791 fixed_tm.tm_year += 20;
2792
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002793 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002794 return 0;
2795
Anand K Mistry57b65f82018-11-09 10:52:19 +11002796 return (((uint32_t)fixed_tm.tm_mday + (32 * ((uint32_t)fixed_tm.tm_mon + 1)) + (512 * (uint32_t)fixed_tm.tm_year)) << 16) |
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002797 (((uint32_t)fixed_tm.tm_sec / 2) + (32 * (uint32_t)fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002798}
2799
2800int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
2801{
2802 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
2803 return MZ_OK;
2804}
2805
2806int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
2807{
2808 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
2809 return MZ_OK;
2810}
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002811
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002812/***************************************************************************/
2813
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002814int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case)
2815{
2816 do
2817 {
2818 if ((*path1 == '\\' && *path2 == '/') ||
2819 (*path2 == '\\' && *path1 == '/'))
2820 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002821 /* Ignore comparison of path slashes */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002822 }
2823 else if (ignore_case)
2824 {
2825 if (tolower(*path1) != tolower(*path2))
2826 break;
2827 }
2828 else if (*path1 != *path2)
2829 {
2830 break;
2831 }
2832
2833 path1 += 1;
2834 path2 += 1;
2835 }
2836 while (*path1 != 0 && *path2 != 0);
2837
2838 if (ignore_case)
2839 return (int32_t)(tolower(*path1) - tolower(*path2));
2840
2841 return (int32_t)(*path1 - *path2);
2842}
2843
2844/***************************************************************************/