blob: 0fb93533b6af94e7ee79ac1ae6ac1c63027e1d7e [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvaziri370ab032019-11-15 13:24:17 -08002 Version 2.9.1, November 15, 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)
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -080071#define MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR (24)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080072
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -080073#define MZ_ZIP_OFFSET_CRC_SIZES (14)
74
Nathan Moinvaziric78c7822018-12-01 20:42:36 -080075#ifndef MZ_ZIP_EOCD_MAX_BACK
76#define MZ_ZIP_EOCD_MAX_BACK (1 << 20)
77#endif
78
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080079/***************************************************************************/
80
81typedef struct mz_zip_s
82{
83 mz_zip_file file_info;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070084 mz_zip_file local_file_info;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080085
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080086 void *stream; /* main stream */
87 void *cd_stream; /* pointer to the stream with the cd */
88 void *cd_mem_stream; /* memory stream for central directory */
89 void *compress_stream; /* compression stream */
90 void *crypt_stream; /* encryption stream */
91 void *file_info_stream; /* memory stream for storing file info */
92 void *local_file_info_stream; /* memory stream for storing local file info */
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080093
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070094 int32_t open_mode;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -080095 uint8_t recover;
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -080096 uint8_t data_descriptor;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070097
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080098 uint32_t disk_number_with_cd; /* number of the disk with the central dir */
99 int64_t disk_offset_shift; /* correction for zips that have wrong offset start of cd */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700100
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800101 int64_t cd_start_pos; /* pos of the first file in the central dir stream */
102 int64_t cd_current_pos; /* pos of the current file in the central dir */
103 int64_t cd_offset; /* offset of start of central directory */
104 int64_t cd_size; /* size of the central directory */
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -0700105 uint32_t cd_signature; /* signature of central directory */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700106
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800107 uint8_t entry_scanned; /* entry header information read ok */
108 uint8_t entry_opened; /* entry is open for read/write */
109 uint8_t entry_raw; /* entry opened with raw mode */
110 uint32_t entry_crc32; /* entry crc32 */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700111
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700112 uint64_t number_entry;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700113
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700114 uint16_t version_madeby;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700115 char *comment;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800116} mz_zip;
117
118/***************************************************************************/
119
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800120#if 0
121# define mz_zip_print printf
122#else
123# define mz_zip_print(fmt,...)
124#endif
125
126/***************************************************************************/
127
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800128/* Locate the end of central directory */
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700129static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800130{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700131 int64_t file_size = 0;
Nathan Moinvaziric78c7822018-12-01 20:42:36 -0800132 int64_t max_back = MZ_ZIP_EOCD_MAX_BACK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800133 uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8;
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700134 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700135
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700136 err = mz_stream_seek(stream, 0, MZ_SEEK_END);
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800137 if (err != MZ_OK)
138 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800139
140 file_size = mz_stream_tell(stream);
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800141
Nathan Moinvaziric78c7822018-12-01 20:42:36 -0800142 if (max_back <= 0 || max_back > file_size)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800143 max_back = file_size;
144
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800145 return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800146}
147
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800148/* Locate the end of central directory 64 of a zip file */
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700149static 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 +0800150{
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700151 int64_t offset = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800152 uint32_t value32 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700153 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700154
155
156 *central_pos = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800157
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800158 /* Zip64 end of central directory locator */
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700159 err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800160 /* Read locator signature */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700161 if (err == MZ_OK)
162 {
163 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700164 if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700165 err = MZ_FORMAT_ERROR;
166 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800167 /* Number of the disk with the start of the zip64 end of central directory */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700168 if (err == MZ_OK)
169 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800170 /* Relative offset of the zip64 end of central directory record8 */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700171 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700172 err = mz_stream_read_uint64(stream, (uint64_t *)&offset);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800173 /* Total number of disks */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700174 if (err == MZ_OK)
175 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800176 /* Goto end of central directory record */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700177 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700178 err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800179 /* The signature */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700180 if (err == MZ_OK)
181 {
182 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700183 if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700184 err = MZ_FORMAT_ERROR;
185 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800186
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700187 if (err == MZ_OK)
188 *central_pos = offset;
189
190 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800191}
192
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800193/* Get info about the current file in the zip file */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700194static 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 -0700195{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700196 uint64_t ntfs_time = 0;
197 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700198 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700199 uint32_t dos_date = 0;
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800200 uint32_t field_pos = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700201 uint16_t field_type = 0;
202 uint16_t field_length = 0;
203 uint32_t field_length_read = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700204 uint16_t ntfs_attrib_id = 0;
205 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700206 uint16_t linkname_size;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700207 uint16_t value16 = 0;
208 uint32_t value32 = 0;
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800209 int64_t extrafield_pos = 0;
210 int64_t comment_pos = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700211 int64_t linkname_pos = 0;
212 int64_t saved_pos = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700213 int32_t err = MZ_OK;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700214 char *linkname = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700215
216
217 memset(file_info, 0, sizeof(mz_zip_file));
218
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800219 /* Check the magic */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700220 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700221 if (err == MZ_END_OF_STREAM)
222 err = MZ_END_OF_LIST;
223 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700224 err = MZ_END_OF_LIST;
225 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
226 err = MZ_FORMAT_ERROR;
227 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
228 err = MZ_FORMAT_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000229
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800230 /* Read header fields */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700231 if (err == MZ_OK)
232 {
233 if (!local)
234 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
235 if (err == MZ_OK)
236 err = mz_stream_read_uint16(stream, &file_info->version_needed);
237 if (err == MZ_OK)
238 err = mz_stream_read_uint16(stream, &file_info->flag);
239 if (err == MZ_OK)
240 err = mz_stream_read_uint16(stream, &file_info->compression_method);
241 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700242 {
243 err = mz_stream_read_uint32(stream, &dos_date);
244 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
245 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700246 if (err == MZ_OK)
247 err = mz_stream_read_uint32(stream, &file_info->crc);
248 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700249 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700250 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700251 file_info->compressed_size = value32;
252 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700253 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700254 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700255 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700256 file_info->uncompressed_size = value32;
257 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700258 if (err == MZ_OK)
259 err = mz_stream_read_uint16(stream, &file_info->filename_size);
260 if (err == MZ_OK)
261 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
262 if (!local)
263 {
264 if (err == MZ_OK)
265 err = mz_stream_read_uint16(stream, &file_info->comment_size);
266 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700267 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700268 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700269 file_info->disk_number = value16;
270 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700271 if (err == MZ_OK)
272 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
273 if (err == MZ_OK)
274 err = mz_stream_read_uint32(stream, &file_info->external_fa);
275 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700276 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700277 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700278 file_info->disk_offset = value32;
279 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700280 }
281 }
282
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700283 if (err == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800284 err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700285
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800286 /* Copy variable length data to memory stream for later retrieval */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700287 if ((err == MZ_OK) && (file_info->filename_size > 0))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700288 err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800289 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800290 extrafield_pos = mz_stream_tell(file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700291
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800292 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
293 err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800294 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800295
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800296 comment_pos = mz_stream_tell(file_extra_stream);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800297 if ((err == MZ_OK) && (file_info->comment_size > 0))
298 err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800299 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700300
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700301 linkname_pos = mz_stream_tell(file_extra_stream);
302 /* Overwrite if we encounter UNIX1 extra block */
303 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700304
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700305 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
306 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800307 /* Seek to and parse the extra field */
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800308 err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700309
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800310 while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700311 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700312 err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length);
Nathan Moinvaziriea2bd792018-11-19 17:44:44 -0800313 if (err != MZ_OK)
314 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800315 field_pos += 4;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800316
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800317 /* Don't allow field length to exceed size of remaining extrafield */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800318 if (field_length > (file_info->extrafield_size - field_pos))
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -0800319 field_length = (uint16_t)(file_info->extrafield_size - field_pos);
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800320
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800321 /* Read ZIP64 extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800322 if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700323 {
324 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800325 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700326 err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800327 if (file_info->uncompressed_size < 0)
328 err = MZ_FORMAT_ERROR;
329 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700330 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800331 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700332 err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800333 if (file_info->compressed_size < 0)
334 err = MZ_FORMAT_ERROR;
335 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700336 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800337 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700338 err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800339 if (file_info->disk_offset < 0)
340 err = MZ_FORMAT_ERROR;
341 }
juanii4ae79922018-02-11 14:29:36 -0300342 if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700343 err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700344 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800345 /* Read NTFS extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800346 else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700347 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700348 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700349 err = mz_stream_read_uint32(file_extra_stream, &reserved);
350 field_length_read = 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700351
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800352 while ((err == MZ_OK) && (field_length_read + 4 <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700353 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700354 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700355 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700356 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800357 field_length_read += 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700358
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700359 if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700360 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700361 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700362 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
363
juanii7063b0e2018-02-11 13:56:21 -0300364 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700365 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700366 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700367 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
368 }
juanii7063b0e2018-02-11 13:56:21 -0300369 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700370 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700371 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700372 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
373 }
374 }
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800375 else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700376 {
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800377 err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700378 }
379
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800380 field_length_read += ntfs_attrib_size;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700381 }
382 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800383 /* Read UNIX1 extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800384 else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700385 {
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700386 if (err == MZ_OK)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700387 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700388 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700389 if (err == MZ_OK && file_info->accessed_date == 0)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700390 file_info->accessed_date = value32;
391 }
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700392 if (err == MZ_OK)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700393 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700394 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700395 if (err == MZ_OK && file_info->modified_date == 0)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700396 file_info->modified_date = value32;
397 }
398 if (err == MZ_OK)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800399 err = mz_stream_read_uint16(file_extra_stream, &value16); /* User id */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700400 if (err == MZ_OK)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800401 err = mz_stream_read_uint16(file_extra_stream, &value16); /* Group id */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700402
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700403 /* Copy linkname to end of file extra stream so we can return null
404 terminated string */
405 linkname_size = field_length - 12;
406 if ((err == MZ_OK) && (linkname_size > 0))
407 {
408 linkname = (char *)MZ_ALLOC(linkname_size);
409 if (linkname != NULL)
410 {
411 if (mz_stream_read(file_extra_stream, linkname, linkname_size) != linkname_size)
412 err = MZ_READ_ERROR;
413 if (err == MZ_OK)
414 {
415 saved_pos = mz_stream_tell(file_extra_stream);
416
417 mz_stream_seek(file_extra_stream, linkname_pos, MZ_SEEK_SET);
418 mz_stream_write(file_extra_stream, linkname, linkname_size);
419 mz_stream_write_uint8(file_extra_stream, 0);
420
421 mz_stream_seek(file_extra_stream, saved_pos, MZ_SEEK_SET);
422 }
423 MZ_FREE(linkname);
424 }
425 }
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700426 }
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800427#ifdef HAVE_WZAES
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800428 /* Read AES extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800429 else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700430 {
431 uint8_t value8 = 0;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800432 /* Verify version info */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700433 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800434 /* Support AE-1 and AE-2 */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700435 if (value16 != 1 && value16 != 2)
436 err = MZ_FORMAT_ERROR;
437 file_info->aes_version = value16;
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 != 'A')
441 err = MZ_FORMAT_ERROR;
442 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700443 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700444 if ((char)value8 != 'E')
445 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800446 /* Get AES encryption strength and actual compression method */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700447 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700448 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700449 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700450 file_info->aes_encryption_mode = value8;
451 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700452 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700453 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700454 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700455 file_info->compression_method = value16;
456 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700457 }
458#endif
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800459 else if (field_length > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700460 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700461 err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700462 }
463
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800464 field_pos += field_length;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700465 }
466 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700467
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800468 /* Get pointers to variable length data */
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800469 mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename);
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800470 mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield);
471 mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700472 mz_stream_mem_get_buffer_at(file_extra_stream, linkname_pos, (const void **)&file_info->linkname);
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800473
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800474 /* Set to empty string just in-case */
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800475 if (file_info->filename == NULL)
476 file_info->filename = "";
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800477 if (file_info->extrafield == NULL)
478 file_info->extrafield_size = 0;
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800479 if (file_info->comment == NULL)
480 file_info->comment = "";
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700481 if (file_info->linkname == NULL)
482 file_info->linkname = "";
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700483
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800484 if (err == MZ_OK)
485 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700486 mz_zip_print("Zip - Entry - Read header - %s (local %" PRId8 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800487 file_info->filename, local);
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700488 mz_zip_print("Zip - Entry - Read header compress (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800489 file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
490 if (!local)
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800491 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700492 mz_zip_print("Zip - Entry - Read header disk (disk %" PRIu32 " offset %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800493 file_info->disk_number, file_info->disk_offset);
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800494 }
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700495 mz_zip_print("Zip - Entry - Read header variable (fnl %" PRId32 " efs %" PRId32 " cms %" PRId32 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800496 file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
497 }
498
499 return err;
500}
501
502static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size)
503{
504 uint32_t value32 = 0;
505 int64_t value64 = 0;
506 int32_t err = MZ_OK;
507
508
509 err = mz_stream_read_uint32(stream, &value32);
510 if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR)
511 err = MZ_FORMAT_ERROR;
512 if (err == MZ_OK)
513 err = mz_stream_read_uint32(stream, &value32);
514 if ((err == MZ_OK) && (crc32 != NULL))
515 *crc32 = value32;
516 if (err == MZ_OK)
517 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800518 /* If zip 64 extension is enabled then read as 8 byte */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800519 if (!zip64)
520 {
521 err = mz_stream_read_uint32(stream, &value32);
522 value64 = value32;
523 }
524 else
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800525 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800526 err = mz_stream_read_int64(stream, &value64);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800527 if (value64 < 0)
528 err = MZ_FORMAT_ERROR;
529 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800530 if ((err == MZ_OK) && (compressed_size != NULL))
531 *compressed_size = value64;
532 }
533 if (err == MZ_OK)
534 {
535 if (!zip64)
536 {
537 err = mz_stream_read_uint32(stream, &value32);
538 value64 = value32;
539 }
540 else
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800541 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800542 err = mz_stream_read_int64(stream, &value64);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800543 if (value64 < 0)
544 err = MZ_FORMAT_ERROR;
545 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800546 if ((err == MZ_OK) && (uncompressed_size != NULL))
547 *uncompressed_size = value64;
548 }
549
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700550 return err;
551}
552
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -0800553static int32_t mz_zip_entry_write_crc_sizes(void *stream, uint8_t local, uint8_t mask, mz_zip_file *file_info)
554{
555 int32_t err = MZ_OK;
556
557 if (mask)
558 err = mz_stream_write_uint32(stream, 0);
559 else
560 err = mz_stream_write_uint32(stream, file_info->crc); /* crc */
561
562 if (err == MZ_OK)
563 {
564 if (file_info->compressed_size >= UINT32_MAX) /* compr size */
565 err = mz_stream_write_uint32(stream, UINT32_MAX);
566 else
567 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
568 }
569 if (err == MZ_OK)
570 {
571 if (file_info->uncompressed_size >= UINT32_MAX) /* uncompr size */
572 err = mz_stream_write_uint32(stream, UINT32_MAX);
573 else if (mask)
574 err = mz_stream_write_uint32(stream, 0);
575 else
576 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
577 }
578 return err;
579}
580
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700581static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700582{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700583 uint64_t ntfs_time = 0;
584 uint32_t reserved = 0;
585 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700586 uint16_t extrafield_size = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700587 uint16_t field_type = 0;
588 uint16_t field_length = 0;
589 uint16_t field_length_zip64 = 0;
590 uint16_t field_length_ntfs = 0;
591 uint16_t field_length_aes = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700592 uint16_t field_length_unix1 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700593 uint16_t filename_size = 0;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700594 uint16_t filename_length = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700595 uint16_t linkname_size = 0;
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700596 uint16_t version_needed = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800597 int32_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700598 int32_t err = MZ_OK;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700599 int32_t err_mem = MZ_OK;
600 uint8_t zip64 = 0;
601 uint8_t skip_aes = 0;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700602 uint8_t mask = 0;
603 uint8_t write_end_slash = 0;
604 const char *filename = NULL;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700605 char masked_name[64];
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700606 void *file_extra_stream = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700607
608 if (file_info == NULL)
609 return MZ_PARAM_ERROR;
610
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700611 if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO))
612 mask = 1;
613
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800614 /* Calculate extra field sizes */
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700615 if (file_info->uncompressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700616 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700617 if (file_info->compressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700618 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700619 if (file_info->disk_offset >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700620 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700621
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700622 if (file_info->zip64 == MZ_ZIP64_AUTO)
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700623 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800624 /* If uncompressed size is unknown, assume zip64 for 64-bit data descriptors */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700625 zip64 = (local && file_info->uncompressed_size == 0) || (field_length_zip64 > 0);
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700626 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700627 else if (file_info->zip64 == MZ_ZIP64_FORCE)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700628 {
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700629 zip64 = 1;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700630 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700631 else if (file_info->zip64 == MZ_ZIP64_DISABLE)
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700632 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800633 /* Zip64 extension is required to zip file */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700634 if (field_length_zip64 > 0)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700635 return MZ_PARAM_ERROR;
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700636 }
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700637
638 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700639 {
640 extrafield_size += 4;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700641 extrafield_size += field_length_zip64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700642 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700643
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800644 /* Calculate extra field size and check for duplicates */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700645 if (file_info->extrafield_size > 0)
646 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700647 mz_stream_mem_create(&file_extra_stream);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700648 mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
Nathan Moinvaziri915c5132018-10-26 20:00:52 -0700649 file_info->extrafield_size);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700650
651 do
652 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700653 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700654 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700655 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700656 if (err_mem != MZ_OK)
657 break;
658
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800659 /* Prefer incoming aes extensions over ours */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700660 if (field_type == MZ_ZIP_EXTENSION_AES)
661 skip_aes = 1;
662
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700663 /* Prefer our zip64, ntfs, unix1 extension over incoming */
664 if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS &&
665 field_type != MZ_ZIP_EXTENSION_UNIX1)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700666 extrafield_size += 4 + field_length;
667
668 if (err_mem == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800669 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700670 }
671 while (err_mem == MZ_OK);
672 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700673
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800674#ifdef HAVE_WZAES
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700675 if (!skip_aes)
676 {
677 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700678 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700679 field_length_aes = 1 + 1 + 1 + 2 + 2;
680 extrafield_size += 4 + field_length_aes;
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700681 }
682 }
Nathan Moinvaziria2bb08c2018-10-28 13:45:30 -0700683#else
Nathan Moinvaziri7de94cf2018-11-19 20:51:06 -0800684 MZ_UNUSED(field_length_aes);
Nathan Moinvaziri4ae469a2018-10-28 15:32:04 -0700685 MZ_UNUSED(skip_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700686#endif
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800687 /* NTFS timestamps */
Nathan Moinvazirid9e159a2018-02-13 15:30:30 -0800688 if ((file_info->modified_date != 0) &&
689 (file_info->accessed_date != 0) &&
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700690 (file_info->creation_date != 0) && (!mask))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700691 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700692 field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
693 extrafield_size += 4 + field_length_ntfs;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700694 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700695
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700696 /* Unix1 symbolic links */
697 if (file_info->linkname != NULL && *file_info->linkname != 0)
698 {
699 linkname_size = (uint16_t)strlen(file_info->linkname);
700 field_length_unix1 = 12 + linkname_size;
701 extrafield_size += 4 + field_length_unix1;
702 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700703
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700704 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700705 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700706 else
707 {
708 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
709 if (err == MZ_OK)
710 err = mz_stream_write_uint16(stream, file_info->version_madeby);
711 }
712
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800713 /* Calculate version needed to extract */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700714 if (err == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700715 {
716 version_needed = file_info->version_needed;
717 if (version_needed == 0)
718 {
719 version_needed = 20;
720 if (zip64)
721 version_needed = 45;
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800722#ifdef HAVE_WZAES
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700723 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
724 version_needed = 51;
725#endif
726#ifdef HAVE_LZMA
727 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
728 version_needed = 63;
729#endif
730 }
731 err = mz_stream_write_uint16(stream, version_needed);
732 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700733 if (err == MZ_OK)
734 err = mz_stream_write_uint16(stream, file_info->flag);
735 if (err == MZ_OK)
736 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800737#ifdef HAVE_WZAES
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700738 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700739 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700740 else
741#endif
742 err = mz_stream_write_uint16(stream, file_info->compression_method);
743 }
744 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700745 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700746 if (file_info->modified_date != 0 && !mask)
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -0700747 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700748 err = mz_stream_write_uint32(stream, dos_date);
749 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700750
751 if (err == MZ_OK)
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -0800752 err = mz_zip_entry_write_crc_sizes(stream, local, mask, file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700753
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700754 if (mask)
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700755 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700756 snprintf(masked_name, sizeof(masked_name), "%" PRIx32 "_%" PRIx64,
Nathan Moinvaziri264dc182018-11-13 17:42:13 -0800757 file_info->disk_number, file_info->disk_offset);
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700758 filename = masked_name;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700759 }
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700760 else
761 {
762 filename = file_info->filename;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700763 }
764
765 filename_length = (uint16_t)strlen(filename);
766 filename_size += filename_length;
767
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800768 if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) &&
769 ((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\')))
770 {
771 filename_size += 1;
772 write_end_slash = 1;
773 }
774
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700775 if (err == MZ_OK)
776 err = mz_stream_write_uint16(stream, filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700777 if (err == MZ_OK)
778 err = mz_stream_write_uint16(stream, extrafield_size);
779
780 if (!local)
781 {
782 if (file_info->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800783 {
784 comment_size = (int32_t)strlen(file_info->comment);
785 if (comment_size > UINT16_MAX)
786 comment_size = UINT16_MAX;
787 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700788 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800789 err = mz_stream_write_uint16(stream, (uint16_t)comment_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700790 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700791 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700792 if (err == MZ_OK)
793 err = mz_stream_write_uint16(stream, file_info->internal_fa);
794 if (err == MZ_OK)
795 err = mz_stream_write_uint32(stream, file_info->external_fa);
796 if (err == MZ_OK)
797 {
798 if (file_info->disk_offset >= UINT32_MAX)
799 err = mz_stream_write_uint32(stream, UINT32_MAX);
800 else
801 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
802 }
803 }
804
805 if (err == MZ_OK)
806 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700807 if (mz_stream_write(stream, filename, filename_length) != filename_length)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700808 err = MZ_WRITE_ERROR;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700809
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800810 /* Ensure that directories have a slash appended to them for compatibility */
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700811 if (err == MZ_OK && write_end_slash)
812 err = mz_stream_write_uint8(stream, '/');
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700813 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700814
815 if (file_info->extrafield_size > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700816 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800817 err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700818 while (err == MZ_OK && err_mem == MZ_OK)
819 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700820 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700821 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700822 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700823 if (err_mem != MZ_OK)
824 break;
825
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700826 /* Prefer our zip 64, ntfs, unix1 extensions over incoming */
827 if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS ||
828 field_type == MZ_ZIP_EXTENSION_UNIX1)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700829 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800830 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700831 continue;
832 }
833
834 err = mz_stream_write_uint16(stream, field_type);
835 if (err == MZ_OK)
836 err = mz_stream_write_uint16(stream, field_length);
837 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700838 err = mz_stream_copy(stream, file_extra_stream, field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700839 }
840
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700841 mz_stream_mem_delete(&file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700842 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700843
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800844 /* Write ZIP64 extra field */
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700845 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700846 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700847 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700848 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700849 {
850 if (mask)
851 err = mz_stream_write_int64(stream, 0);
852 else
853 err = mz_stream_write_int64(stream, file_info->uncompressed_size);
854 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700855 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700856 err = mz_stream_write_int64(stream, file_info->compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700857 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700858 err = mz_stream_write_int64(stream, file_info->disk_offset);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700859 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800860 /* Write NTFS extra field */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700861 if ((err == MZ_OK) && (field_length_ntfs > 0))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700862 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700863 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700864 if (err == MZ_OK)
865 err = mz_stream_write_uint32(stream, reserved);
866 if (err == MZ_OK)
867 err = mz_stream_write_uint16(stream, 0x01);
868 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700869 err = mz_stream_write_uint16(stream, field_length_ntfs - 8);
juanii3679a3d2018-02-11 13:55:38 -0300870 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700871 {
872 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
873 err = mz_stream_write_uint64(stream, ntfs_time);
874 }
juanii3679a3d2018-02-11 13:55:38 -0300875 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700876 {
877 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
878 err = mz_stream_write_uint64(stream, ntfs_time);
879 }
juanii3679a3d2018-02-11 13:55:38 -0300880 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700881 {
882 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
883 err = mz_stream_write_uint64(stream, ntfs_time);
884 }
885 }
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700886 /* Write UNIX extra block extra field */
887 if ((err == MZ_OK) && (field_length_unix1 > 0))
888 {
889 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_UNIX1, field_length_unix1);
890 if (err == MZ_OK)
Nathan Moinvaziriaeb6cdd2019-05-06 13:40:32 -0700891 err = mz_stream_write_uint32(stream, (uint32_t)file_info->accessed_date);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700892 if (err == MZ_OK)
Nathan Moinvaziriaeb6cdd2019-05-06 13:40:32 -0700893 err = mz_stream_write_uint32(stream, (uint32_t)file_info->modified_date);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700894 if (err == MZ_OK) /* User id */
895 err = mz_stream_write_uint16(stream, 0);
896 if (err == MZ_OK) /* Group id */
897 err = mz_stream_write_uint16(stream, 0);
898 if (err == MZ_OK && linkname_size > 0)
899 {
900 if (mz_stream_write(stream, file_info->linkname, linkname_size) != linkname_size)
901 err = MZ_WRITE_ERROR;
902 }
903 }
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800904#ifdef HAVE_WZAES
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800905 /* Write AES extra field */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700906 if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700907 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700908 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700909 if (err == MZ_OK)
910 err = mz_stream_write_uint16(stream, file_info->aes_version);
911 if (err == MZ_OK)
912 err = mz_stream_write_uint8(stream, 'A');
913 if (err == MZ_OK)
914 err = mz_stream_write_uint8(stream, 'E');
915 if (err == MZ_OK)
916 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
917 if (err == MZ_OK)
918 err = mz_stream_write_uint16(stream, file_info->compression_method);
919 }
920#endif
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700921 if ((err == MZ_OK) && (!local) && (file_info->comment != NULL))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700922 {
Nathan Moinvazirica014e62018-08-15 07:31:12 -0700923 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700924 err = MZ_WRITE_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700925 }
926
927 return err;
928}
929
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800930static 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 -0800931{
932 int32_t err = MZ_OK;
933
934 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
935 if (err == MZ_OK)
936 err = mz_stream_write_uint32(stream, crc32);
937
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800938 /* Store data descriptor as 8 bytes if zip 64 extension enabled */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800939 if (err == MZ_OK)
940 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800941 /* Zip 64 extension is enabled when uncompressed size is > UINT32_MAX */
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800942 if (!zip64)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800943 err = mz_stream_write_uint32(stream, (uint32_t)compressed_size);
944 else
945 err = mz_stream_write_int64(stream, compressed_size);
946 }
947 if (err == MZ_OK)
948 {
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800949 if (!zip64)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800950 err = mz_stream_write_uint32(stream, (uint32_t)uncompressed_size);
951 else
952 err = mz_stream_write_int64(stream, uncompressed_size);
953 }
954
955 return err;
956}
957
958static int32_t mz_zip_read_cd(void *handle)
959{
960 mz_zip *zip = (mz_zip *)handle;
961 uint64_t number_entry_cd64 = 0;
962 uint64_t number_entry = 0;
963 uint64_t number_entry_cd = 0;
964 int64_t eocd_pos = 0;
965 int64_t eocd_pos64 = 0;
966 int64_t value64i = 0;
967 uint16_t value16 = 0;
968 uint32_t value32 = 0;
969 uint64_t value64 = 0;
970 uint16_t comment_size = 0;
971 int32_t comment_read = 0;
972 int32_t err = MZ_OK;
973
974
975 if (zip == NULL)
976 return MZ_PARAM_ERROR;
977
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800978 /* Read and cache central directory records */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800979 err = mz_zip_search_eocd(zip->stream, &eocd_pos);
980 if (err == MZ_OK)
981 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800982 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800983 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800984 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800985 if (err == MZ_OK)
986 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800987 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800988 if (err == MZ_OK)
989 err = mz_stream_read_uint16(zip->stream, &value16);
990 zip->disk_number_with_cd = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800991 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800992 if (err == MZ_OK)
993 err = mz_stream_read_uint16(zip->stream, &value16);
994 zip->number_entry = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800995 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800996 if (err == MZ_OK)
997 err = mz_stream_read_uint16(zip->stream, &value16);
998 number_entry_cd = value16;
999 if (number_entry_cd != zip->number_entry)
1000 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001001 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001002 if (err == MZ_OK)
1003 err = mz_stream_read_uint32(zip->stream, &value32);
1004 if (err == MZ_OK)
1005 zip->cd_size = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001006 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001007 if (err == MZ_OK)
1008 err = mz_stream_read_uint32(zip->stream, &value32);
1009 if (err == MZ_OK)
1010 zip->cd_offset = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001011 /* Zip file global comment length */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001012 if (err == MZ_OK)
1013 err = mz_stream_read_uint16(zip->stream, &comment_size);
1014 if ((err == MZ_OK) && (comment_size > 0))
1015 {
1016 zip->comment = (char *)MZ_ALLOC(comment_size + 1);
1017 if (zip->comment != NULL)
1018 {
1019 comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001020 /* Don't fail if incorrect comment length read, not critical */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001021 if (comment_read < 0)
1022 comment_read = 0;
1023 zip->comment[comment_read] = 0;
1024 }
1025 }
1026
1027 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
1028 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001029 /* Format should be Zip64, as the central directory or file size is too large */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001030 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
1031 {
1032 eocd_pos = eocd_pos64;
1033
1034 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001035 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001036 if (err == MZ_OK)
1037 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001038 /* Size of zip64 end of central directory record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001039 if (err == MZ_OK)
1040 err = mz_stream_read_uint64(zip->stream, &value64);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001041 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001042 if (err == MZ_OK)
1043 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001044 /* Version needed to extract */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001045 if (err == MZ_OK)
1046 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001047 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001048 if (err == MZ_OK)
1049 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001050 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001051 if (err == MZ_OK)
1052 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001053 /* Total number of entries in the central directory on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001054 if (err == MZ_OK)
1055 err = mz_stream_read_uint64(zip->stream, &number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001056 /* Total number of entries in the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001057 if (err == MZ_OK)
1058 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
1059 if (number_entry == UINT32_MAX)
1060 zip->number_entry = number_entry_cd64;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001061 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001062 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001063 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001064 err = mz_stream_read_int64(zip->stream, &zip->cd_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001065 if (zip->cd_size < 0)
1066 err = MZ_FORMAT_ERROR;
1067 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001068 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001069 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001070 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001071 err = mz_stream_read_int64(zip->stream, &zip->cd_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001072 if (zip->cd_offset < 0)
1073 err = MZ_FORMAT_ERROR;
1074 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001075 }
1076 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
1077 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
1078 {
1079 err = MZ_FORMAT_ERROR;
1080 }
1081 }
1082 }
1083
1084 if (err == MZ_OK)
1085 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001086 mz_zip_print("Zip - Read cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001087 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1088
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001089 /* Verify central directory signature exists at offset */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001090 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1091 if (err == MZ_OK)
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001092 err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001093 if ((err == MZ_OK) && (zip->cd_signature != MZ_ZIP_MAGIC_CENTRALHEADER))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001094 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001095 /* If cd exists in large file and no zip-64 support, error for recover */
1096 if (eocd_pos > UINT32_MAX && eocd_pos64 == 0)
1097 err = MZ_FORMAT_ERROR;
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001098 /* If cd not found attempt to seek backward to find it */
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001099 if (err == MZ_OK)
1100 err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001101 if (err == MZ_OK)
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001102 err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001103 if ((err == MZ_OK) && (zip->cd_signature == MZ_ZIP_MAGIC_CENTRALHEADER))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001104 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001105
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001106 /* If found compensate for incorrect locations */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001107 value64i = zip->cd_offset;
1108 zip->cd_offset = eocd_pos - zip->cd_size;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001109 /* Assume disk has prepended data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001110 zip->disk_offset_shift = zip->cd_offset - value64i;
1111 }
1112 }
1113 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001114
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001115 if (err == MZ_OK)
1116 {
1117 if (eocd_pos < zip->cd_offset)
1118 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001119 /* End of central dir should always come after central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001120 err = MZ_FORMAT_ERROR;
1121 }
1122 else if (eocd_pos < zip->cd_offset + zip->cd_size)
1123 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001124 /* Truncate size of cd if incorrect size or offset provided */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001125 zip->cd_size = eocd_pos - zip->cd_offset;
1126 }
1127 }
1128
1129 return err;
1130}
1131
1132static int32_t mz_zip_write_cd(void *handle)
1133{
1134 mz_zip *zip = (mz_zip *)handle;
1135 int64_t zip64_eocd_pos_inzip = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001136 int64_t disk_number = 0;
1137 int64_t disk_size = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001138 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001139 int32_t err = MZ_OK;
1140
1141
1142 if (zip == NULL)
1143 return MZ_PARAM_ERROR;
1144
1145 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
1146 zip->disk_number_with_cd = (uint32_t)disk_number;
1147 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
1148 zip->disk_number_with_cd += 1;
1149 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Nathan Moinvazirif286b832019-11-10 17:09:48 -08001150 if ((zip->disk_number_with_cd > 0) && (zip->open_mode & MZ_OPEN_MODE_APPEND))
1151 {
1152 // Overwrite existing central directory if using split disks
1153 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1154 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001155
1156 zip->cd_offset = mz_stream_tell(zip->stream);
1157 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
1158 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
1159 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
1160
1161 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
1162
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001163 mz_zip_print("Zip - Write cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001164 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1165
Antoine Cœur61f224a2019-05-02 01:42:09 +08001166 if (zip->cd_size == 0 && zip->number_entry > 0)
Nathan Moinvaziri64faa252019-04-21 21:38:48 -07001167 {
1168 // Zip does not contain central directory, open with recovery option
1169 return MZ_FORMAT_ERROR;
1170 }
1171
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001172 /* Write the ZIP64 central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001173 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
1174 {
1175 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
1176
1177 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
1178
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001179 /* Size of this 'zip64 end of central directory' */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001180 if (err == MZ_OK)
1181 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001182 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001183 if (err == MZ_OK)
1184 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001185 /* Version needed */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001186 if (err == MZ_OK)
1187 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001188 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001189 if (err == MZ_OK)
1190 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001191 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001192 if (err == MZ_OK)
1193 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001194 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001195 if (err == MZ_OK)
1196 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001197 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001198 if (err == MZ_OK)
1199 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001200 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001201 if (err == MZ_OK)
1202 err = mz_stream_write_int64(zip->stream, zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001203 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001204 if (err == MZ_OK)
1205 err = mz_stream_write_int64(zip->stream, zip->cd_offset);
1206 if (err == MZ_OK)
1207 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
1208
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001209 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001210 if (err == MZ_OK)
1211 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001212 /* Relative offset to the end of zip64 central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001213 if (err == MZ_OK)
1214 err = mz_stream_write_int64(zip->stream, zip64_eocd_pos_inzip);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001215 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001216 if (err == MZ_OK)
1217 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
1218 }
1219
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001220 /* Write the central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001221
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001222 /* Signature */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001223 if (err == MZ_OK)
1224 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001225 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001226 if (err == MZ_OK)
1227 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001228 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001229 if (err == MZ_OK)
1230 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001231 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001232 if (err == MZ_OK)
1233 {
1234 if (zip->number_entry >= UINT16_MAX)
1235 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1236 else
1237 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1238 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001239 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001240 if (err == MZ_OK)
1241 {
1242 if (zip->number_entry >= UINT16_MAX)
1243 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1244 else
1245 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1246 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001247 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001248 if (err == MZ_OK)
1249 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001250 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001251 if (err == MZ_OK)
1252 {
1253 if (zip->cd_offset >= UINT32_MAX)
1254 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
1255 else
1256 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
1257 }
1258
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001259 /* Write global comment */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001260 if (zip->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001261 {
1262 comment_size = (int32_t)strlen(zip->comment);
1263 if (comment_size > UINT16_MAX)
1264 comment_size = UINT16_MAX;
1265 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001266 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001267 err = mz_stream_write_uint16(zip->stream, (uint16_t)comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001268 if (err == MZ_OK)
1269 {
1270 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
1271 err = MZ_READ_ERROR;
1272 }
1273 return err;
1274}
1275
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001276static int32_t mz_zip_recover_cd(void *handle)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001277{
1278 mz_zip *zip = (mz_zip *)handle;
1279 mz_zip_file local_file_info;
1280 void *local_file_info_stream = NULL;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001281 void *cd_mem_stream = NULL;
1282 uint64_t number_entry = 0;
1283 int64_t descriptor_pos = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001284 int64_t next_header_pos = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001285 int64_t disk_offset = 0;
1286 int64_t disk_number = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001287 int64_t compressed_pos = 0;
1288 int64_t compressed_end_pos = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001289 int64_t compressed_size = 0;
1290 int64_t uncompressed_size = 0;
1291 uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001292 uint8_t local_header_magic[4] = MZ_ZIP_MAGIC_LOCALHEADERU8;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001293 uint8_t central_header_magic[4] = MZ_ZIP_MAGIC_CENTRALHEADERU8;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001294 uint32_t crc32 = 0;
1295 int32_t disk_number_with_cd = 0;
1296 int32_t err = MZ_OK;
1297 uint8_t zip64 = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001298 uint8_t eof = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001299
1300
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001301 mz_zip_print("Zip - Recover - Start\n");
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001302
1303 mz_zip_get_cd_mem_stream(handle, &cd_mem_stream);
1304
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001305 /* Determine if we are on a split disk or not */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001306 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0);
1307 if (mz_stream_tell(zip->stream) < 0)
1308 {
1309 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1310 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1311 }
1312 else
1313 disk_number_with_cd = 1;
1314
1315 if (mz_stream_is_open(cd_mem_stream) != MZ_OK)
1316 err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001317
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001318 mz_stream_mem_create(&local_file_info_stream);
1319 mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri57cbcd22019-10-21 15:37:58 -07001320
1321 if (err == MZ_OK)
1322 {
1323 err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001324 INT64_MAX, &next_header_pos);
Nathan Moinvaziri57cbcd22019-10-21 15:37:58 -07001325 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001326
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001327 while (err == MZ_OK && !eof)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001328 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001329 /* Get current offset and disk number for central dir record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001330 disk_offset = mz_stream_tell(zip->stream);
1331 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1332
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001333 /* Read local headers */
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001334 memset(&local_file_info, 0, sizeof(local_file_info));
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001335 err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream);
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001336 if (err != MZ_OK)
1337 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001338
1339 local_file_info.disk_offset = disk_offset;
1340 if (disk_number < 0)
1341 disk_number = 0;
1342 local_file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri370ab032019-11-15 13:24:17 -08001343
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001344 compressed_pos = mz_stream_tell(zip->stream);
Nathan Moinvaziri370ab032019-11-15 13:24:17 -08001345
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001346 if ((err == MZ_OK) && (local_file_info.compressed_size > 0))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001347 {
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001348 mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001349 }
1350
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001351 while (1)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001352 {
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001353 /* Search for the next local header */
1354 err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
1355 INT64_MAX, &next_header_pos);
Nathan Moinvaziri370ab032019-11-15 13:24:17 -08001356
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001357 if (err == MZ_EXIST_ERROR)
1358 {
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001359 mz_stream_seek(zip->stream, compressed_pos, MZ_SEEK_SET);
1360
1361 /* Search for central dir if no local header found */
1362 err = mz_stream_find(zip->stream, (const void *)central_header_magic, sizeof(central_header_magic),
1363 INT64_MAX, &next_header_pos);
1364
1365 if (err == MZ_EXIST_ERROR)
1366 {
1367 /* Get end of stream if no central header found */
1368 mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1369 next_header_pos = mz_stream_tell(zip->stream);
1370 }
1371
1372 eof = 1;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001373 }
1374
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001375 if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR || local_file_info.compressed_size == 0)
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001376 {
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001377 /* Search backwards for the descriptor, seeking too far back will be incorrect if compressed size is small */
1378 err = mz_stream_find_reverse(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
1379 MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR, &descriptor_pos);
1380 if (err == MZ_OK)
1381 {
1382 if (mz_zip_extrafield_contains(local_file_info.extrafield,
1383 local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
1384 zip64 = 1;
1385
1386 err = mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32,
1387 &compressed_size, &uncompressed_size);
1388
1389 if (err == MZ_OK)
1390 {
1391 if (local_file_info.crc == 0)
1392 local_file_info.crc = crc32;
1393 if (local_file_info.compressed_size == 0)
1394 local_file_info.compressed_size = compressed_size;
1395 if (local_file_info.uncompressed_size == 0)
1396 local_file_info.uncompressed_size = uncompressed_size;
1397 }
1398
1399 compressed_end_pos = descriptor_pos;
1400 }
1401 else if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1402 {
1403 /* Wrong local file entry found, keep searching */
1404 next_header_pos += 1;
1405 mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
1406 continue;
1407 }
1408 }
1409 else
1410 {
1411 compressed_end_pos = next_header_pos;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001412 }
1413
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001414 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001415 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001416
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001417 compressed_size = compressed_end_pos - compressed_pos;
1418
1419 if (compressed_size > UINT32_MAX)
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001420 {
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001421 /* Update sizes if 4GB file is written with no ZIP64 support */
1422 if (local_file_info.uncompressed_size < UINT32_MAX)
1423 {
1424 local_file_info.compressed_size = compressed_size;
1425 local_file_info.uncompressed_size = 0;
1426 }
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001427 }
1428
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001429 mz_zip_print("Zip - Recover - Entry %s (csize %" PRId64 " usize %" PRId64 " flags 0x%" PRIx16 ")\n",
1430 local_file_info.filename, local_file_info.compressed_size, local_file_info.uncompressed_size,
1431 local_file_info.flag);
1432
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001433 /* Rewrite central dir with local headers and offsets */
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001434 err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001435 if (err == MZ_OK)
Nathan Moinvaziri370ab032019-11-15 13:24:17 -08001436 number_entry += 1;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001437
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001438 err = mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001439 }
1440
1441 mz_stream_mem_delete(&local_file_info_stream);
1442
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001443 mz_zip_print("Zip - Recover - Complete (cddisk %" PRId32 " entries %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001444 disk_number_with_cd, number_entry);
1445
1446 if (number_entry == 0)
1447 return err;
Nathan Moinvaziri370ab032019-11-15 13:24:17 -08001448
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001449 /* Set new upper seek boundary for central dir mem stream */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001450 disk_offset = mz_stream_tell(cd_mem_stream);
1451 mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset);
1452
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001453 /* Set new central directory info */
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001454 mz_zip_set_cd_stream(handle, 0, cd_mem_stream);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001455 mz_zip_set_number_entry(handle, number_entry);
1456 mz_zip_set_disk_number_with_cd(handle, disk_number_with_cd);
1457
1458 return MZ_OK;
1459}
1460
1461void *mz_zip_create(void **handle)
1462{
1463 mz_zip *zip = NULL;
1464
1465 zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
1466 if (zip != NULL)
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08001467 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001468 memset(zip, 0, sizeof(mz_zip));
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08001469 zip->data_descriptor = 0;
1470 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001471 if (handle != NULL)
1472 *handle = zip;
1473
1474 return zip;
1475}
1476
1477void mz_zip_delete(void **handle)
1478{
1479 mz_zip *zip = NULL;
1480 if (handle == NULL)
1481 return;
1482 zip = (mz_zip *)*handle;
1483 if (zip != NULL)
1484 {
1485 MZ_FREE(zip);
1486 }
1487 *handle = NULL;
1488}
1489
1490int32_t mz_zip_open(void *handle, void *stream, int32_t mode)
1491{
1492 mz_zip *zip = (mz_zip *)handle;
1493 int32_t err = MZ_OK;
1494
1495
1496 if (zip == NULL)
1497 return MZ_PARAM_ERROR;
1498
1499 mz_zip_print("Zip - Open\n");
1500
1501 zip->stream = stream;
1502
1503 mz_stream_mem_create(&zip->cd_mem_stream);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001504
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001505 if (mode & MZ_OPEN_MODE_WRITE)
1506 {
1507 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1508 zip->cd_stream = zip->cd_mem_stream;
1509 }
1510 else
1511 {
1512 zip->cd_stream = stream;
1513 }
1514
1515 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
1516 {
1517 if ((mode & MZ_OPEN_MODE_CREATE) == 0)
1518 {
1519 err = mz_zip_read_cd(zip);
1520 if (err != MZ_OK)
1521 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001522 mz_zip_print("Zip - Error detected reading cd (%" PRId32 ")\n", err);
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001523 if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001524 err = MZ_OK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001525 }
1526 }
1527
1528 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
1529 {
1530 if (zip->cd_size > 0)
1531 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001532 /* Store central directory in memory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001533 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1534 if (err == MZ_OK)
1535 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size);
1536 if (err == MZ_OK)
1537 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1538 }
1539 else
1540 {
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001541 if (zip->cd_signature == MZ_ZIP_MAGIC_ENDHEADER)
1542 {
1543 /* If tiny zip then overwrite end header */
1544 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1545 }
1546 else
1547 {
1548 /* If no central directory, append new zip to end of file */
1549 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1550 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001551 }
Nathan Moinvaziri2ce1fe22018-11-23 11:53:11 -08001552
1553 if (zip->disk_number_with_cd > 0)
1554 {
1555 /* Move to last disk to begin appending */
1556 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->disk_number_with_cd - 1);
1557 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001558 }
1559 else
1560 {
1561 zip->cd_start_pos = zip->cd_offset;
1562 }
1563 }
1564
1565 if (err != MZ_OK)
1566 {
1567 mz_zip_close(zip);
1568 return err;
1569 }
1570
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001571 /* Memory streams used to store variable length file info data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001572 mz_stream_mem_create(&zip->file_info_stream);
1573 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1574
1575 mz_stream_mem_create(&zip->local_file_info_stream);
1576 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1577
1578 zip->open_mode = mode;
1579
1580 return err;
1581}
1582
1583int32_t mz_zip_close(void *handle)
1584{
1585 mz_zip *zip = (mz_zip *)handle;
1586 int32_t err = MZ_OK;
1587
1588 if (zip == NULL)
1589 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001590
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001591 mz_zip_print("Zip - Close\n");
1592
1593 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001594 err = mz_zip_entry_close(handle);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001595
1596 if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE))
1597 err = mz_zip_write_cd(handle);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001598
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001599 if (zip->cd_mem_stream != NULL)
1600 {
1601 mz_stream_close(zip->cd_mem_stream);
1602 mz_stream_delete(&zip->cd_mem_stream);
1603 }
1604
1605 if (zip->file_info_stream != NULL)
1606 {
1607 mz_stream_mem_close(zip->file_info_stream);
1608 mz_stream_mem_delete(&zip->file_info_stream);
1609 }
1610 if (zip->local_file_info_stream != NULL)
1611 {
1612 mz_stream_mem_close(zip->local_file_info_stream);
1613 mz_stream_mem_delete(&zip->local_file_info_stream);
1614 }
1615
1616 if (zip->comment)
1617 {
1618 MZ_FREE(zip->comment);
1619 zip->comment = NULL;
1620 }
1621
1622 zip->stream = NULL;
1623 zip->cd_stream = NULL;
1624
1625 return err;
1626}
1627
1628int32_t mz_zip_get_comment(void *handle, const char **comment)
1629{
1630 mz_zip *zip = (mz_zip *)handle;
1631 if (zip == NULL || comment == NULL)
1632 return MZ_PARAM_ERROR;
1633 if (zip->comment == NULL)
1634 return MZ_EXIST_ERROR;
1635 *comment = zip->comment;
1636 return MZ_OK;
1637}
1638
1639int32_t mz_zip_set_comment(void *handle, const char *comment)
1640{
1641 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001642 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001643 if (zip == NULL || comment == NULL)
1644 return MZ_PARAM_ERROR;
1645 if (zip->comment != NULL)
1646 MZ_FREE(zip->comment);
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001647 comment_size = (int32_t)strlen(comment);
1648 if (comment_size > UINT16_MAX)
1649 return MZ_PARAM_ERROR;
Nathan Moinvaziria6ecab52019-01-22 13:14:01 -08001650 zip->comment = (char *)MZ_ALLOC(comment_size+1);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001651 if (zip->comment == NULL)
1652 return MZ_MEM_ERROR;
Nathan Moinvaziria6ecab52019-01-22 13:14:01 -08001653 memset(zip->comment, 0, comment_size+1);
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001654 strncpy(zip->comment, comment, comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001655 return MZ_OK;
1656}
1657
1658int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
1659{
1660 mz_zip *zip = (mz_zip *)handle;
1661 if (zip == NULL || version_madeby == NULL)
1662 return MZ_PARAM_ERROR;
1663 *version_madeby = zip->version_madeby;
1664 return MZ_OK;
1665}
1666
1667int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
1668{
1669 mz_zip *zip = (mz_zip *)handle;
1670 if (zip == NULL)
1671 return MZ_PARAM_ERROR;
1672 zip->version_madeby = version_madeby;
1673 return MZ_OK;
1674}
1675
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001676int32_t mz_zip_set_recover(void *handle, uint8_t recover)
1677{
1678 mz_zip *zip = (mz_zip *)handle;
1679 if (zip == NULL)
1680 return MZ_PARAM_ERROR;
1681 zip->recover = recover;
1682 return MZ_OK;
1683}
1684
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08001685int32_t mz_zip_set_data_descriptor(void *handle, uint8_t data_descriptor)
1686{
1687 mz_zip *zip = (mz_zip *)handle;
1688 if (zip == NULL)
1689 return MZ_PARAM_ERROR;
1690 zip->data_descriptor = data_descriptor;
1691 return MZ_OK;
1692}
1693
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001694int32_t mz_zip_get_stream(void *handle, void **stream)
1695{
1696 mz_zip *zip = (mz_zip *)handle;
1697 if (zip == NULL || stream == NULL)
1698 return MZ_PARAM_ERROR;
1699 *stream = zip->stream;
1700 if (*stream == NULL)
1701 return MZ_EXIST_ERROR;
1702 return MZ_OK;
1703}
1704
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001705int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001706{
1707 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001708 if (zip == NULL || cd_stream == NULL)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001709 return MZ_PARAM_ERROR;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001710 zip->cd_offset = 0;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001711 zip->cd_stream = cd_stream;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001712 zip->cd_start_pos = cd_start_pos;
1713 return MZ_OK;
1714}
1715
1716int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream)
1717{
1718 mz_zip *zip = (mz_zip *)handle;
1719 if (zip == NULL || cd_mem_stream == NULL)
1720 return MZ_PARAM_ERROR;
1721 *cd_mem_stream = zip->cd_mem_stream;
1722 if (*cd_mem_stream == NULL)
1723 return MZ_EXIST_ERROR;
1724 return MZ_OK;
1725}
1726
1727static int32_t mz_zip_entry_close_int(void *handle)
1728{
1729 mz_zip *zip = (mz_zip *)handle;
1730
1731 if (zip->crypt_stream != NULL)
1732 mz_stream_delete(&zip->crypt_stream);
1733 zip->crypt_stream = NULL;
1734 if (zip->compress_stream != NULL)
1735 mz_stream_delete(&zip->compress_stream);
1736 zip->compress_stream = NULL;
1737
1738 zip->entry_opened = 0;
1739
1740 return MZ_OK;
1741}
1742
Nathan Moinvaziri26308b22018-08-08 09:40:15 -07001743static 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 -07001744{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001745 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001746 int64_t max_total_in = 0;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001747 int64_t header_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001748 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001749 int32_t err = MZ_OK;
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001750 uint8_t use_crypt = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001751
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001752 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001753 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001754
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001755 switch (zip->file_info.compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001756 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001757 case MZ_COMPRESS_METHOD_STORE:
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001758 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001759#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001760 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001761#endif
Nathan Moinvaziri566dfe02018-10-26 00:36:30 -07001762#ifdef HAVE_LZMA
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001763 case MZ_COMPRESS_METHOD_LZMA:
1764#endif
1765 err = MZ_OK;
1766 break;
1767 default:
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001768 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001769 }
1770
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001771#ifndef HAVE_WZAES
1772 if (zip->file_info.aes_version)
1773 return MZ_SUPPORT_ERROR;
1774#endif
1775
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001776 zip->entry_raw = raw;
1777
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001778 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password != NULL))
1779 {
1780 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
1781 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001782 /* Encrypt only when we are not trying to write raw and password is supplied. */
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001783 if (!zip->entry_raw)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001784 use_crypt = 1;
1785 }
1786 else if (zip->open_mode & MZ_OPEN_MODE_READ)
1787 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001788 /* Decrypt only when password is supplied. Don't error when password */
1789 /* is not supplied as we may want to read the raw encrypted data. */
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001790 use_crypt = 1;
1791 }
1792 }
1793
1794 if ((err == MZ_OK) && (use_crypt))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001795 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001796#ifdef HAVE_WZAES
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001797 if (zip->file_info.aes_version)
1798 {
Nathan Moinvaziri21a31022018-10-24 09:50:16 -07001799 mz_stream_wzaes_create(&zip->crypt_stream);
1800 mz_stream_wzaes_set_password(zip->crypt_stream, password);
1801 mz_stream_wzaes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001802 }
1803 else
1804#endif
1805 {
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001806#ifdef HAVE_PKCRYPT
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001807 uint8_t verify1 = 0;
1808 uint8_t verify2 = 0;
1809
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001810 /* Info-ZIP modification to ZipCrypto format: */
1811 /* 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 -07001812
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001813 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1814 {
1815 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001816
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001817 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1818
1819 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1820 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1821 }
1822 else
1823 {
1824 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1825 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1826 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001827
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001828 mz_stream_pkcrypt_create(&zip->crypt_stream);
1829 mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1830 mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001831#endif
1832 }
1833 }
1834
1835 if (err == MZ_OK)
1836 {
1837 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001838 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001839
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001840 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001841
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001842 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001843 }
1844
1845 if (err == MZ_OK)
1846 {
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001847 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001848 mz_stream_raw_create(&zip->compress_stream);
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001849#if defined(HAVE_ZLIB) || defined(HAVE_LIBCOMP)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001850 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001851 mz_stream_zlib_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001852#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001853#ifdef HAVE_BZIP2
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001854 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001855 mz_stream_bzip_create(&zip->compress_stream);
1856#endif
1857#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001858 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001859 mz_stream_lzma_create(&zip->compress_stream);
1860#endif
1861 else
1862 err = MZ_PARAM_ERROR;
1863 }
1864
1865 if (err == MZ_OK)
1866 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001867 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001868 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001869 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001870 }
1871 else
1872 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001873#ifndef HAVE_LIBCOMP
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001874 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 -08001875#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001876 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001877 max_total_in = zip->file_info.compressed_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001878 mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1879
1880 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1881 max_total_in -= header_size;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001882 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1883 max_total_in -= footer_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001884
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001885 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001886 }
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001887 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 -03001888 {
1889 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1890 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1891 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001892 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001893
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001894 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1895
1896 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1897 }
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001898
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001899 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001900 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001901 zip->entry_opened = 1;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001902 zip->entry_crc32 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001903 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001904 else
1905 {
1906 mz_zip_entry_close_int(handle);
1907 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001908
1909 return err;
1910}
1911
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001912int32_t mz_zip_entry_is_open(void *handle)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001913{
1914 mz_zip *zip = (mz_zip *)handle;
1915 if (zip == NULL)
1916 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001917 if (zip->entry_opened == 0)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001918 return MZ_EXIST_ERROR;
1919 return MZ_OK;
1920}
1921
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001922static int32_t mz_zip_seek_to_local_header(void *handle)
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001923{
1924 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001925
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001926
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08001927 if (((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0) && (zip->file_info.disk_number == zip->disk_number_with_cd))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001928 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1929 else
1930 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001931
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001932 mz_zip_print("Zip - Entry - Seek local (disk %" PRId32 " offset %" PRId64 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001933 zip->file_info.disk_number, zip->file_info.disk_offset);
Nathan Moinvaziri09725902019-02-20 09:15:48 -08001934
1935 /* Guard against seek overflows */
1936 if ((zip->disk_offset_shift > 0) &&
1937 (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
1938 return MZ_FORMAT_ERROR;
1939
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001940 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 -07001941}
1942
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001943int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001944{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001945 mz_zip *zip = (mz_zip *)handle;
1946 int32_t err = MZ_OK;
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001947 int32_t err_shift = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001948
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001949#if defined(MZ_ZIP_NO_ENCRYPTION)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001950 if (password != NULL)
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001951 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001952#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001953 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001954 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001955 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001956 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001957 if (zip->entry_scanned == 0)
1958 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001959
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001960 mz_zip_print("Zip - Entry - Read open (raw %" PRId32 ")\n", raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001961
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001962 err = mz_zip_seek_to_local_header(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001963 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001964 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 -07001965
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001966 if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0)
1967 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001968 /* Perhaps we didn't compensated correctly for incorrect cd offset */
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001969 err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
1970 if (err_shift == MZ_OK)
1971 err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
1972 if (err_shift == MZ_OK)
1973 {
1974 zip->disk_offset_shift = 0;
1975 err = err_shift;
1976 }
1977 }
1978
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001979#ifdef MZ_ZIP_NO_DECOMPRESSION
Nathan Moinvaziri2eb29072018-11-23 10:32:21 -08001980 if (!raw && zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001981 err = MZ_SUPPORT_ERROR;
Viktor Szakatse7215072018-09-04 15:06:53 +00001982#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001983 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001984 err = mz_zip_entry_open_int(handle, raw, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001985
1986 return err;
1987}
1988
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001989int32_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 -07001990{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001991 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001992 int64_t filename_pos = -1;
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001993 int64_t extrafield_pos = 0;
1994 int64_t comment_pos = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07001995 int64_t linkname_pos = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001996 int64_t disk_number = 0;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001997 uint8_t is_dir = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001998 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001999
Nathan Moinvaziri40427632018-07-22 11:12:40 -07002000#if defined(MZ_ZIP_NO_ENCRYPTION)
tz-lomb1b25802017-11-10 15:03:02 +03002001 if (password != NULL)
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08002002 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002003#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002004 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002005 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002006
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002007 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002008 {
2009 err = mz_zip_entry_close(handle);
2010 if (err != MZ_OK)
2011 return err;
2012 }
2013
2014 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002015
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002016 mz_zip_print("Zip - Entry - Write open - %s (level %" PRId16 " raw %" PRId8 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002017 zip->file_info.filename, compress_level, raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002018
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002019 mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07002020 mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
2021
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002022 /* Copy filename, extrafield, and comment internally */
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002023 filename_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07002024 if (file_info->filename != NULL)
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002025 mz_stream_write(zip->file_info_stream, file_info->filename, (int32_t)strlen(file_info->filename));
2026 mz_stream_write_uint8(zip->file_info_stream, 0);
2027
2028 extrafield_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07002029 if (file_info->extrafield != NULL)
Nathan Moinvazirica014e62018-08-15 07:31:12 -07002030 mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002031 mz_stream_write_uint8(zip->file_info_stream, 0);
2032
2033 comment_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07002034 if (file_info->comment != NULL)
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002035 mz_stream_write(zip->file_info_stream, file_info->comment, file_info->comment_size);
2036 mz_stream_write_uint8(zip->file_info_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002037
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002038 linkname_pos = mz_stream_tell(zip->file_info_stream);
2039 if (file_info->linkname != NULL)
2040 mz_stream_write(zip->file_info_stream, file_info->linkname, (int32_t)strlen(file_info->linkname));
2041 mz_stream_write_uint8(zip->file_info_stream, 0);
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002042
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08002043 mz_stream_mem_get_buffer_at(zip->file_info_stream, filename_pos, (const void **)&zip->file_info.filename);
2044 mz_stream_mem_get_buffer_at(zip->file_info_stream, extrafield_pos, (const void **)&zip->file_info.extrafield);
2045 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 -07002046 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 -07002047
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002048 if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002049 {
2050 if ((compress_level == 8) || (compress_level == 9))
2051 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
2052 if (compress_level == 2)
2053 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
2054 if (compress_level == 1)
2055 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
2056 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002057#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002058 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002059 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002060#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002061
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002062 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2063 is_dir = 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002064
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002065 if (!is_dir)
2066 {
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08002067 if (zip->data_descriptor)
2068 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002069 if (password != NULL)
2070 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
2071 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002072
juaniib8887e92018-02-14 00:51:05 -03002073 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
2074 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002075
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002076 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07002077 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002078 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002079
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08002080#ifdef HAVE_WZAES
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002081 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
2082 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
2083#endif
2084
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002085 if ((compress_level == 0) || (is_dir))
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002086 zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
Viktor Szakats2884e672018-04-30 08:12:13 +00002087
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07002088#ifdef MZ_ZIP_NO_COMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002089 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002090 err = MZ_SUPPORT_ERROR;
2091#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002092 if (err == MZ_OK)
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002093 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002094 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002095 err = mz_zip_entry_open_int(handle, raw, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002096
2097 return err;
2098}
2099
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002100int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002101{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002102 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002103 int32_t read = 0;
2104
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002105 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002106 return MZ_PARAM_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002107 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) /* zlib limitation */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002108 return MZ_PARAM_ERROR;
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07002109 if (len == 0)
2110 return MZ_PARAM_ERROR;
2111
Nathan Moinvazirieb80cd92018-07-11 16:45:09 -07002112 if (zip->file_info.compressed_size == 0)
2113 return 0;
2114
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002115 /* Read entire entry even if uncompressed_size = 0, otherwise */
2116 /* aes encryption validation will fail if compressed_size > 0 */
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07002117 read = mz_stream_read(zip->compress_stream, buf, len);
2118 if (read > 0)
2119 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002120
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002121 mz_zip_print("Zip - Entry - Read - %" PRId32 " (max %" PRId32 ")\n", read, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002122
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002123 return read;
2124}
2125
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002126int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002127{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002128 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002129 int32_t written = 0;
2130
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002131 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002132 return MZ_PARAM_ERROR;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07002133 written = mz_stream_write(zip->compress_stream, buf, len);
2134 if (written > 0)
2135 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002136
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002137 mz_zip_print("Zip - Entry - Write - %" PRId32 " (max %" PRId32 ")\n", written, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002138
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002139 return written;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002140}
2141
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002142int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002143 int64_t *uncompressed_size)
2144{
2145 mz_zip *zip = (mz_zip *)handle;
2146 int64_t total_in = 0;
2147 int32_t err = MZ_OK;
2148 uint8_t zip64 = 0;
2149
2150 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2151 return MZ_PARAM_ERROR;
2152
2153 mz_stream_close(zip->compress_stream);
2154
2155 mz_zip_print("Zip - Entry - Read Close\n");
2156
2157 if (crc32 != NULL)
2158 *crc32 = zip->file_info.crc;
2159 if (compressed_size != NULL)
2160 *compressed_size = zip->file_info.compressed_size;
2161 if (uncompressed_size != NULL)
2162 *uncompressed_size = zip->file_info.uncompressed_size;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002163
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002164 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
2165
2166 if ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) &&
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002167 ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0) &&
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002168 (crc32 != NULL || compressed_size != NULL || uncompressed_size != NULL))
2169 {
2170 /* Check to see if data descriptor is zip64 bit format or not */
2171 if (mz_zip_extrafield_contains(zip->local_file_info.extrafield,
2172 zip->local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
2173 zip64 = 1;
2174
2175 err = mz_zip_seek_to_local_header(handle);
2176
2177 /* Seek to end of compressed stream since we might have over-read during compression */
2178 if (err == MZ_OK)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002179 err = mz_stream_seek(zip->stream, MZ_ZIP_SIZE_LD_ITEM +
2180 (int64_t)zip->local_file_info.filename_size +
2181 (int64_t)zip->local_file_info.extrafield_size +
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002182 total_in, MZ_SEEK_CUR);
2183
2184 /* Read data descriptor */
2185 if (err == MZ_OK)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002186 err = mz_zip_entry_read_descriptor(zip->stream, zip64,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002187 crc32, compressed_size, uncompressed_size);
2188 }
2189
2190 /* If entire entry was not read verification will fail */
2191 if ((err == MZ_OK) && (total_in > 0) && (!zip->entry_raw))
2192 {
2193#ifdef HAVE_WZAES
2194 /* AES zip version AE-1 will expect a valid crc as well */
2195 if (zip->file_info.aes_version <= 0x0001)
2196#endif
2197 {
2198 if (zip->entry_crc32 != zip->file_info.crc)
2199 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002200 mz_zip_print("Zip - Entry - Crc failed (actual 0x%08" PRIx32 " expected 0x%08" PRIx32 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002201 zip->entry_crc32, zip->file_info.crc);
2202
2203 err = MZ_CRC_ERROR;
2204 }
2205 }
2206 }
2207
2208 mz_zip_entry_close_int(handle);
2209
2210 return err;
2211}
2212
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002213int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002214 int64_t uncompressed_size)
2215{
2216 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08002217 int64_t end_pos = 0;
2218 int64_t end_disk_number = 0;
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002219 int32_t err = MZ_OK;
2220 uint8_t zip64 = 0;
2221
2222 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2223 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002224
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002225 mz_stream_close(zip->compress_stream);
2226
2227 if (!zip->entry_raw)
2228 crc32 = zip->entry_crc32;
2229
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002230 mz_zip_print("Zip - Entry - Write Close (crc 0x%08" PRIx32 " cs %" PRId64 " ucs %" PRId64 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002231 crc32, compressed_size, uncompressed_size);
2232
2233 /* If sizes are not set, then read them from the compression stream */
2234 if (compressed_size < 0)
2235 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2236 if (uncompressed_size < 0)
Nathan Moinvazirid9318432018-12-01 07:56:28 -08002237 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002238
2239 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
2240 {
2241 mz_stream_set_base(zip->crypt_stream, zip->stream);
2242 err = mz_stream_close(zip->crypt_stream);
2243
2244 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2245 }
2246
2247 if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
2248 {
2249 /* Determine if we need to write data descriptor in zip64 format,
2250 if local extrafield was saved with zip64 extrafield */
2251 if (zip->file_info.zip64 == MZ_ZIP64_AUTO)
2252 {
2253 if (zip->file_info.uncompressed_size >= UINT32_MAX)
2254 zip64 = 1;
2255 if (zip->file_info.compressed_size >= UINT32_MAX)
2256 zip64 = 1;
2257 if (zip->file_info.disk_offset >= UINT32_MAX)
2258 zip64 = 1;
2259 else if (zip->file_info.uncompressed_size == 0)
2260 zip64 = 1;
2261 }
2262 else if (zip->file_info.zip64 == MZ_ZIP64_FORCE)
2263 {
2264 zip64 = 1;
2265 }
2266
2267 if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002268 err = mz_zip_entry_write_descriptor(zip->stream,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002269 zip64, 0, compressed_size, 0);
2270 else
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002271 err = mz_zip_entry_write_descriptor(zip->stream,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002272 zip64, crc32, compressed_size, uncompressed_size);
2273 }
2274
2275 /* Write file info to central directory */
2276
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002277 mz_zip_print("Zip - Entry - Write cd (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002278 uncompressed_size, compressed_size, crc32);
2279
2280 zip->file_info.crc = crc32;
2281 zip->file_info.compressed_size = compressed_size;
2282 zip->file_info.uncompressed_size = uncompressed_size;
2283
2284 if (err == MZ_OK)
2285 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
2286
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08002287 /* Update local header with crc32 and sizes */
2288 if ((err == MZ_OK) && ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) == 0) &&
2289 ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0))
2290 {
2291 /* Save the disk number and position we are to seek back after updating local header */
2292 end_pos = mz_stream_tell(zip->stream);
2293 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &end_disk_number);
2294
2295 err = mz_zip_seek_to_local_header(handle);
2296
2297 if (err == MZ_OK)
2298 {
2299 /* Seek to crc32 and sizes offset in local header */
2300 err = mz_stream_seek(zip->stream, MZ_ZIP_OFFSET_CRC_SIZES, MZ_SEEK_CUR);
2301 }
2302
2303 if (err == MZ_OK)
2304 err = mz_zip_entry_write_crc_sizes(zip->stream, 1, 0, &zip->file_info);
2305
2306 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, end_disk_number);
2307 mz_stream_seek(zip->stream, end_pos, MZ_SEEK_SET);
2308 }
2309
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002310 zip->number_entry += 1;
2311
2312 mz_zip_entry_close_int(handle);
2313
2314 return err;
2315}
2316
2317int32_t mz_zip_entry_is_dir(void *handle)
2318{
2319 mz_zip *zip = (mz_zip *)handle;
2320 int32_t filename_length = 0;
2321
2322 if (zip == NULL)
2323 return MZ_PARAM_ERROR;
2324 if (zip->entry_scanned == 0)
2325 return MZ_PARAM_ERROR;
2326 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2327 return MZ_OK;
2328
2329 filename_length = (int32_t)strlen(zip->file_info.filename);
2330 if (filename_length > 0)
2331 {
2332 if ((zip->file_info.filename[filename_length - 1] == '/') ||
2333 (zip->file_info.filename[filename_length - 1] == '\\'))
2334 return MZ_OK;
2335 }
2336 return MZ_EXIST_ERROR;
2337}
2338
Nathan Moinvaziri3249eac2019-05-02 21:07:39 -07002339int32_t mz_zip_entry_is_symlink(void *handle)
2340{
2341 mz_zip *zip = (mz_zip *)handle;
2342
2343 if (zip == NULL)
2344 return MZ_PARAM_ERROR;
2345 if (zip->entry_scanned == 0)
2346 return MZ_PARAM_ERROR;
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002347 if (mz_zip_attrib_is_symlink(zip->file_info.external_fa, zip->file_info.version_madeby) != MZ_OK)
2348 return MZ_EXIST_ERROR;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002349 if (zip->file_info.linkname == NULL || *zip->file_info.linkname == 0)
2350 return MZ_EXIST_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002351
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002352 return MZ_OK;
Nathan Moinvaziri3249eac2019-05-02 21:07:39 -07002353}
2354
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002355int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002356{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002357 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002358
2359 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002360 return MZ_PARAM_ERROR;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002361
2362 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
2363 {
2364 if (!zip->entry_scanned)
2365 return MZ_PARAM_ERROR;
2366 }
2367
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002368 *file_info = &zip->file_info;
2369 return MZ_OK;
2370}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002371
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002372int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002373{
2374 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002375 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002376 return MZ_PARAM_ERROR;
2377 *local_file_info = &zip->local_file_info;
2378 return MZ_OK;
2379}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002380
Nathan Moinvaziri915c5132018-10-26 20:00:52 -07002381int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size)
2382{
2383 mz_zip *zip = (mz_zip *)handle;
2384
2385 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2386 return MZ_PARAM_ERROR;
2387
2388 zip->file_info.extrafield = extrafield;
2389 zip->file_info.extrafield_size = extrafield_size;
2390 return MZ_OK;
2391}
2392
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002393int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07002394{
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002395 return mz_zip_entry_close_raw(handle, UINT64_MAX, 0);
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07002396}
2397
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002398int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002399{
2400 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002401 int32_t err = MZ_OK;
2402
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002403 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002404 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002405
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002406 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002407 err = mz_zip_entry_write_close(handle, crc32, UINT64_MAX, uncompressed_size);
2408 else
2409 err = mz_zip_entry_read_close(handle, NULL, NULL, NULL);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002410
2411 return err;
2412}
2413
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002414static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002415{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002416 mz_zip *zip = (mz_zip *)handle;
2417 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002418
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002419 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002420 return MZ_PARAM_ERROR;
2421
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002422 zip->entry_scanned = 0;
2423
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002424 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00002425
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002426 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002427 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002428 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002429 if (err == MZ_OK)
2430 zip->entry_scanned = 1;
2431 return err;
2432}
2433
Nathan Moinvaziric565fa82018-10-19 08:48:33 -07002434int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry)
2435{
2436 mz_zip *zip = (mz_zip *)handle;
2437 if (zip == NULL)
2438 return MZ_PARAM_ERROR;
2439 zip->number_entry = number_entry;
2440 return MZ_OK;
2441}
2442
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002443int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002444{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002445 mz_zip *zip = (mz_zip *)handle;
2446 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002447 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002448 *number_entry = zip->number_entry;
2449 return MZ_OK;
2450}
2451
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002452int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd)
2453{
2454 mz_zip *zip = (mz_zip *)handle;
2455 if (zip == NULL)
2456 return MZ_PARAM_ERROR;
2457 zip->disk_number_with_cd = disk_number_with_cd;
2458 return MZ_OK;
2459}
2460
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002461int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03002462{
2463 mz_zip *zip = (mz_zip *)handle;
2464 if (zip == NULL || disk_number_with_cd == NULL)
2465 return MZ_PARAM_ERROR;
2466 *disk_number_with_cd = zip->disk_number_with_cd;
2467 return MZ_OK;
2468}
2469
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002470int64_t mz_zip_get_entry(void *handle)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002471{
2472 mz_zip *zip = (mz_zip *)handle;
2473
2474 if (zip == NULL)
2475 return MZ_PARAM_ERROR;
2476
2477 return zip->cd_current_pos;
2478}
2479
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002480int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002481{
2482 mz_zip *zip = (mz_zip *)handle;
2483
2484 if (zip == NULL)
2485 return MZ_PARAM_ERROR;
2486
2487 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
2488 return MZ_PARAM_ERROR;
2489
2490 zip->cd_current_pos = cd_pos;
2491
2492 return mz_zip_goto_next_entry_int(handle);
2493}
2494
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002495int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002496{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002497 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002498
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002499 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002500 return MZ_PARAM_ERROR;
2501
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002502 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002503
2504 return mz_zip_goto_next_entry_int(handle);
2505}
2506
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002507int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002508{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002509 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002510
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002511 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002512 return MZ_PARAM_ERROR;
2513
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002514 zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002515 zip->file_info.extrafield_size + zip->file_info.comment_size;
2516
2517 return mz_zip_goto_next_entry_int(handle);
2518}
2519
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002520int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002521{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002522 mz_zip *zip = (mz_zip *)handle;
2523 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002524 int32_t result = 0;
2525
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002526 if (zip == NULL || filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002527 return MZ_PARAM_ERROR;
2528
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002529 /* If we are already on the current entry, no need to search */
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002530 if ((zip->entry_scanned) && (zip->file_info.filename != NULL))
2531 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002532 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002533 if (result == 0)
2534 return MZ_OK;
2535 }
2536
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002537 /* Search all entries starting at the first */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002538 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002539 while (err == MZ_OK)
2540 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002541 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2542 if (result == 0)
2543 return MZ_OK;
2544
2545 err = mz_zip_goto_next_entry(handle);
2546 }
2547
2548 return err;
2549}
2550
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002551int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002552{
2553 mz_zip *zip = (mz_zip *)handle;
2554 int32_t err = MZ_OK;
2555 int32_t result = 0;
2556
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002557 /* Search first entry looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002558 err = mz_zip_goto_first_entry(handle);
2559 if (err != MZ_OK)
2560 return err;
2561
2562 result = cb(handle, userdata, &zip->file_info);
2563 if (result == 0)
2564 return MZ_OK;
2565
2566 return mz_zip_locate_next_entry(handle, userdata, cb);
2567}
2568
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002569int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002570{
2571 mz_zip *zip = (mz_zip *)handle;
2572 int32_t err = MZ_OK;
2573 int32_t result = 0;
2574
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002575 /* Search next entries looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002576 err = mz_zip_goto_next_entry(handle);
2577 while (err == MZ_OK)
2578 {
2579 result = cb(handle, userdata, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002580 if (result == 0)
2581 return MZ_OK;
2582
2583 err = mz_zip_goto_next_entry(handle);
2584 }
2585
2586 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002587}
2588
2589/***************************************************************************/
2590
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002591int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002592{
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002593 uint32_t posix_attrib = 0;
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002594 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002595 int32_t err = MZ_OK;
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002596
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002597 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2598 if (err == MZ_OK)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002599 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002600 if ((posix_attrib & 0170000) == 0040000) /* S_ISDIR */
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002601 return MZ_OK;
2602 }
2603
2604 return MZ_EXIST_ERROR;
2605}
2606
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002607int32_t mz_zip_attrib_is_symlink(uint32_t attrib, int32_t version_madeby)
2608{
2609 uint32_t posix_attrib = 0;
2610 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
2611 int32_t err = MZ_OK;
2612
2613 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2614 if (err == MZ_OK)
2615 {
2616 if ((posix_attrib & 0170000) == 0120000) /* S_ISLNK */
2617 return MZ_OK;
2618 }
2619
2620 return MZ_EXIST_ERROR;
2621}
2622
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002623int32_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 -07002624{
2625 if (target_attrib == NULL)
2626 return MZ_PARAM_ERROR;
2627
2628 *target_attrib = 0;
2629
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002630 if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002631 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002632 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002633 {
2634 *target_attrib = src_attrib;
2635 return MZ_OK;
2636 }
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002637 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 -07002638 return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
2639 }
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002640 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 -07002641 {
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002642 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 -07002643 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002644 /* If high bytes are set, it contains unix specific attributes */
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002645 if ((src_attrib >> 16) != 0)
2646 src_attrib >>= 16;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002647
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002648 *target_attrib = src_attrib;
2649 return MZ_OK;
2650 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002651 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002652 return mz_zip_attrib_posix_to_win32(src_attrib, target_attrib);
2653 }
2654
2655 return MZ_SUPPORT_ERROR;
2656}
2657
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002658int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002659{
2660 if (win32_attrib == NULL)
2661 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002662
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002663 *win32_attrib = 0;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002664
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002665 /* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002666 if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002667 *win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002668 /* S_IFLNK */
2669 if ((posix_attrib & 0170000) == 0120000)
2670 *win32_attrib |= 0x400; /* FILE_ATTRIBUTE_REPARSE_POINT */
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002671 /* S_IFDIR */
Nathan Moinvaziri7d94b1f2019-05-08 23:41:47 -07002672 else if ((posix_attrib & 0170000) == 0040000)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002673 *win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
2674 /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002675 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002676 *win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002677
2678 return MZ_OK;
2679}
2680
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002681int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002682{
2683 if (posix_attrib == NULL)
2684 return MZ_PARAM_ERROR;
2685
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002686 *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
2687 /* FILE_ATTRIBUTE_READONLY */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002688 if ((win32_attrib & 0x01) == 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002689 *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002690 /* FILE_ATTRIBUTE_REPARSE_POINT */
2691 if ((win32_attrib & 0x400) == 0x400)
2692 *posix_attrib |= 0120000; /* S_IFLNK */
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002693 /* FILE_ATTRIBUTE_DIRECTORY */
Nathan Moinvaziri7d94b1f2019-05-08 23:41:47 -07002694 else if ((win32_attrib & 0x10) == 0x10)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002695 *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002696 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002697 *posix_attrib |= 0100000; /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002698
2699 return MZ_OK;
2700}
2701
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002702/***************************************************************************/
2703
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002704int32_t mz_zip_extrafield_find(void *stream, uint16_t type, uint16_t *length)
2705{
2706 int32_t err = MZ_OK;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002707 uint16_t field_type = 0;
2708 uint16_t field_length = 0;
2709
2710 do
2711 {
2712 err = mz_stream_read_uint16(stream, &field_type);
2713 if (err == MZ_OK)
2714 err = mz_stream_read_uint16(stream, &field_length);
2715 if (err != MZ_OK)
2716 break;
2717
2718 if (type == field_type)
2719 {
2720 if (length != NULL)
2721 *length = field_length;
2722 return MZ_OK;
2723 }
2724
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002725 err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR);
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002726 }
2727 while (err == MZ_OK);
2728
2729 return MZ_EXIST_ERROR;
2730}
2731
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002732int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size,
2733 uint16_t type, uint16_t *length)
2734{
2735 void *file_extra_stream = NULL;
2736 int32_t err = MZ_OK;
2737
2738 if (extrafield == NULL || extrafield_size == 0)
2739 return MZ_PARAM_ERROR;
2740
2741 mz_stream_mem_create(&file_extra_stream);
2742 mz_stream_mem_set_buffer(file_extra_stream, (void *)extrafield, extrafield_size);
2743
2744 err = mz_zip_extrafield_find(file_extra_stream, type, length);
2745
2746 mz_stream_mem_delete(&file_extra_stream);
2747
2748 return err;
2749}
2750
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002751int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length)
2752{
2753 int32_t err = MZ_OK;
2754 if (type == NULL || length == NULL)
2755 return MZ_PARAM_ERROR;
2756 err = mz_stream_read_uint16(stream, type);
2757 if (err == MZ_OK)
2758 err = mz_stream_read_uint16(stream, length);
2759 return err;
2760}
2761
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002762int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002763{
2764 int32_t err = MZ_OK;
2765 err = mz_stream_write_uint16(stream, type);
2766 if (err == MZ_OK)
2767 err = mz_stream_write_uint16(stream, length);
2768 return err;
2769}
2770
2771/***************************************************************************/
2772
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002773static int32_t mz_zip_invalid_date(const struct tm *ptm)
2774{
2775#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002776 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002777 !datevalue_in_range(0, 11, ptm->tm_mon) ||
2778 !datevalue_in_range(1, 31, ptm->tm_mday) ||
2779 !datevalue_in_range(0, 23, ptm->tm_hour) ||
2780 !datevalue_in_range(0, 59, ptm->tm_min) ||
2781 !datevalue_in_range(0, 59, ptm->tm_sec));
2782#undef datevalue_in_range
2783}
2784
2785static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
2786{
2787 uint64_t date = (uint64_t)(dos_date >> 16);
2788
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002789 ptm->tm_mday = (uint16_t)(date & 0x1f);
2790 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
2791 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
2792 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
2793 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
2794 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002795 ptm->tm_isdst = -1;
2796}
2797
2798int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
2799{
2800 if (ptm == NULL)
2801 return MZ_PARAM_ERROR;
2802
2803 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
2804
2805 if (mz_zip_invalid_date(ptm))
2806 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002807 /* Invalid date stored, so don't return it */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002808 memset(ptm, 0, sizeof(struct tm));
2809 return MZ_FORMAT_ERROR;
2810 }
2811 return MZ_OK;
2812}
2813
2814time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
2815{
2816 struct tm ptm;
2817 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
2818 return mktime(&ptm);
2819}
2820
2821int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
2822{
Nathan Moinvaziricd3e9e12019-04-28 10:22:49 -07002823 struct tm ltm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002824 if (ptm == NULL)
2825 return MZ_PARAM_ERROR;
Nathan Moinvaziri2e0a20a2019-04-28 13:03:11 -07002826 if (localtime_r(&unix_time, &ltm) == NULL) /* Returns a 1900-based year */
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002827 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002828 /* Invalid date stored, so don't return it */
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002829 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002830 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002831 }
Nathan Moinvaziricd3e9e12019-04-28 10:22:49 -07002832 memcpy(ptm, &ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002833 return MZ_OK;
2834}
2835
2836uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
2837{
2838 struct tm ptm;
2839 mz_zip_time_t_to_tm(unix_time, &ptm);
2840 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
2841}
2842
2843uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
2844{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07002845 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002846
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002847 /* Years supported: */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002848
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002849 /* [00, 79] (assumed to be between 2000 and 2079) */
2850 /* [80, 207] (assumed to be between 1980 and 2107, typical output of old */
2851 /* software that does 'year-1900' to get a double digit year) */
2852 /* [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.) */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002853
2854 memcpy(&fixed_tm, ptm, sizeof(struct tm));
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002855 if (fixed_tm.tm_year >= 1980) /* range [1980, 2107] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002856 fixed_tm.tm_year -= 1980;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002857 else if (fixed_tm.tm_year >= 80) /* range [80, 207] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002858 fixed_tm.tm_year -= 80;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002859 else /* range [00, 79] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002860 fixed_tm.tm_year += 20;
2861
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002862 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002863 return 0;
2864
Anand K Mistry57b65f82018-11-09 10:52:19 +11002865 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 -07002866 (((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 -07002867}
2868
2869int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
2870{
2871 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
2872 return MZ_OK;
2873}
2874
2875int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
2876{
2877 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
2878 return MZ_OK;
2879}
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002880
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002881/***************************************************************************/
2882
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002883int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case)
2884{
2885 do
2886 {
2887 if ((*path1 == '\\' && *path2 == '/') ||
2888 (*path2 == '\\' && *path1 == '/'))
2889 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002890 /* Ignore comparison of path slashes */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002891 }
2892 else if (ignore_case)
2893 {
2894 if (tolower(*path1) != tolower(*path2))
2895 break;
2896 }
2897 else if (*path1 != *path2)
2898 {
2899 break;
2900 }
2901
2902 path1 += 1;
2903 path2 += 1;
2904 }
2905 while (*path1 != 0 && *path2 != 0);
2906
2907 if (ignore_case)
2908 return (int32_t)(tolower(*path1) - tolower(*path2));
2909
2910 return (int32_t)(*path1 - *path2);
2911}
2912
2913/***************************************************************************/