blob: 5dc22e7467185acb329ad76c827f13df59d1833f [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvaziri2aa369c2020-05-21 07:31:09 -07002 Version 2.9.3, May 21, 2020
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08003 part of the MiniZip project
4
Nathan Moinvazirib39f7a02020-02-12 08:25:33 -08005 Copyright (C) 2010-2020 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 Moinvazirid7e6aa02020-02-12 08:31:21 -0800553static int32_t mz_zip_entry_write_crc_sizes(void *stream, uint8_t mask, mz_zip_file *file_info)
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -0800554{
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 Moinvazirid7e6aa02020-02-12 08:31:21 -0800752 err = mz_zip_entry_write_crc_sizes(stream, 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;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800962 uint64_t number_entry_cd = 0;
963 int64_t eocd_pos = 0;
964 int64_t eocd_pos64 = 0;
965 int64_t value64i = 0;
966 uint16_t value16 = 0;
967 uint32_t value32 = 0;
968 uint64_t value64 = 0;
969 uint16_t comment_size = 0;
970 int32_t comment_read = 0;
971 int32_t err = MZ_OK;
972
973
974 if (zip == NULL)
975 return MZ_PARAM_ERROR;
976
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800977 /* Read and cache central directory records */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800978 err = mz_zip_search_eocd(zip->stream, &eocd_pos);
979 if (err == MZ_OK)
980 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800981 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800982 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800983 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800984 if (err == MZ_OK)
985 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800986 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800987 if (err == MZ_OK)
988 err = mz_stream_read_uint16(zip->stream, &value16);
989 zip->disk_number_with_cd = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800990 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800991 if (err == MZ_OK)
992 err = mz_stream_read_uint16(zip->stream, &value16);
993 zip->number_entry = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800994 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800995 if (err == MZ_OK)
996 err = mz_stream_read_uint16(zip->stream, &value16);
997 number_entry_cd = value16;
998 if (number_entry_cd != zip->number_entry)
999 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001000 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001001 if (err == MZ_OK)
1002 err = mz_stream_read_uint32(zip->stream, &value32);
1003 if (err == MZ_OK)
1004 zip->cd_size = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001005 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001006 if (err == MZ_OK)
1007 err = mz_stream_read_uint32(zip->stream, &value32);
1008 if (err == MZ_OK)
1009 zip->cd_offset = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001010 /* Zip file global comment length */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001011 if (err == MZ_OK)
1012 err = mz_stream_read_uint16(zip->stream, &comment_size);
1013 if ((err == MZ_OK) && (comment_size > 0))
1014 {
1015 zip->comment = (char *)MZ_ALLOC(comment_size + 1);
1016 if (zip->comment != NULL)
1017 {
1018 comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001019 /* Don't fail if incorrect comment length read, not critical */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001020 if (comment_read < 0)
1021 comment_read = 0;
1022 zip->comment[comment_read] = 0;
1023 }
1024 }
1025
1026 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
1027 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001028 /* Format should be Zip64, as the central directory or file size is too large */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001029 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
1030 {
1031 eocd_pos = eocd_pos64;
1032
1033 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001034 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001035 if (err == MZ_OK)
1036 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001037 /* Size of zip64 end of central directory record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001038 if (err == MZ_OK)
1039 err = mz_stream_read_uint64(zip->stream, &value64);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001040 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001041 if (err == MZ_OK)
1042 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001043 /* Version needed to extract */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001044 if (err == MZ_OK)
1045 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001046 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001047 if (err == MZ_OK)
1048 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001049 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001050 if (err == MZ_OK)
1051 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001052 /* Total number of entries in the central directory on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001053 if (err == MZ_OK)
Gabriel Dubois3b3961e2020-03-20 15:48:40 +00001054 err = mz_stream_read_uint64(zip->stream, &zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001055 /* Total number of entries in the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001056 if (err == MZ_OK)
1057 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
Gabriel Dubois3b3961e2020-03-20 15:48:40 +00001058 if (zip->number_entry != number_entry_cd64)
1059 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001060 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001061 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001062 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001063 err = mz_stream_read_int64(zip->stream, &zip->cd_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001064 if (zip->cd_size < 0)
1065 err = MZ_FORMAT_ERROR;
1066 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001067 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001068 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001069 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001070 err = mz_stream_read_int64(zip->stream, &zip->cd_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001071 if (zip->cd_offset < 0)
1072 err = MZ_FORMAT_ERROR;
1073 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001074 }
1075 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
1076 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
1077 {
1078 err = MZ_FORMAT_ERROR;
1079 }
1080 }
1081 }
1082
1083 if (err == MZ_OK)
1084 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001085 mz_zip_print("Zip - Read cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001086 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1087
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001088 /* Verify central directory signature exists at offset */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001089 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1090 if (err == MZ_OK)
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001091 err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001092 if ((err == MZ_OK) && (zip->cd_signature != MZ_ZIP_MAGIC_CENTRALHEADER))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001093 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001094 /* If cd exists in large file and no zip-64 support, error for recover */
1095 if (eocd_pos > UINT32_MAX && eocd_pos64 == 0)
1096 err = MZ_FORMAT_ERROR;
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001097 /* If cd not found attempt to seek backward to find it */
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001098 if (err == MZ_OK)
1099 err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001100 if (err == MZ_OK)
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001101 err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001102 if ((err == MZ_OK) && (zip->cd_signature == MZ_ZIP_MAGIC_CENTRALHEADER))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001103 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001104
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001105 /* If found compensate for incorrect locations */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001106 value64i = zip->cd_offset;
1107 zip->cd_offset = eocd_pos - zip->cd_size;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001108 /* Assume disk has prepended data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001109 zip->disk_offset_shift = zip->cd_offset - value64i;
1110 }
1111 }
1112 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001113
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001114 if (err == MZ_OK)
1115 {
1116 if (eocd_pos < zip->cd_offset)
1117 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001118 /* End of central dir should always come after central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001119 err = MZ_FORMAT_ERROR;
1120 }
1121 else if (eocd_pos < zip->cd_offset + zip->cd_size)
1122 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001123 /* Truncate size of cd if incorrect size or offset provided */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001124 zip->cd_size = eocd_pos - zip->cd_offset;
1125 }
1126 }
1127
1128 return err;
1129}
1130
1131static int32_t mz_zip_write_cd(void *handle)
1132{
1133 mz_zip *zip = (mz_zip *)handle;
1134 int64_t zip64_eocd_pos_inzip = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001135 int64_t disk_number = 0;
1136 int64_t disk_size = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001137 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001138 int32_t err = MZ_OK;
1139
1140
1141 if (zip == NULL)
1142 return MZ_PARAM_ERROR;
1143
1144 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
1145 zip->disk_number_with_cd = (uint32_t)disk_number;
1146 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
1147 zip->disk_number_with_cd += 1;
1148 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Nathan Moinvazirif286b832019-11-10 17:09:48 -08001149 if ((zip->disk_number_with_cd > 0) && (zip->open_mode & MZ_OPEN_MODE_APPEND))
1150 {
1151 // Overwrite existing central directory if using split disks
1152 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1153 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001154
1155 zip->cd_offset = mz_stream_tell(zip->stream);
1156 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
1157 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
1158 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
1159
1160 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
1161
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001162 mz_zip_print("Zip - Write cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001163 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1164
Antoine Cœur61f224a2019-05-02 01:42:09 +08001165 if (zip->cd_size == 0 && zip->number_entry > 0)
Nathan Moinvaziri64faa252019-04-21 21:38:48 -07001166 {
1167 // Zip does not contain central directory, open with recovery option
1168 return MZ_FORMAT_ERROR;
1169 }
1170
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001171 /* Write the ZIP64 central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001172 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
1173 {
1174 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
1175
1176 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
1177
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001178 /* Size of this 'zip64 end of central directory' */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001179 if (err == MZ_OK)
1180 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001181 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001182 if (err == MZ_OK)
1183 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001184 /* Version needed */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001185 if (err == MZ_OK)
1186 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001187 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001188 if (err == MZ_OK)
1189 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001190 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001191 if (err == MZ_OK)
1192 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001193 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001194 if (err == MZ_OK)
1195 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001196 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001197 if (err == MZ_OK)
1198 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001199 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001200 if (err == MZ_OK)
1201 err = mz_stream_write_int64(zip->stream, zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001202 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001203 if (err == MZ_OK)
1204 err = mz_stream_write_int64(zip->stream, zip->cd_offset);
1205 if (err == MZ_OK)
1206 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
1207
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001208 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001209 if (err == MZ_OK)
1210 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001211 /* Relative offset to the end of zip64 central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001212 if (err == MZ_OK)
1213 err = mz_stream_write_int64(zip->stream, zip64_eocd_pos_inzip);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001214 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001215 if (err == MZ_OK)
1216 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
1217 }
1218
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001219 /* Write the central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001220
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001221 /* Signature */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001222 if (err == MZ_OK)
1223 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001224 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001225 if (err == MZ_OK)
1226 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001227 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001228 if (err == MZ_OK)
1229 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001230 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001231 if (err == MZ_OK)
1232 {
1233 if (zip->number_entry >= UINT16_MAX)
1234 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1235 else
1236 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1237 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001238 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001239 if (err == MZ_OK)
1240 {
1241 if (zip->number_entry >= UINT16_MAX)
1242 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1243 else
1244 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1245 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001246 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001247 if (err == MZ_OK)
1248 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001249 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001250 if (err == MZ_OK)
1251 {
1252 if (zip->cd_offset >= UINT32_MAX)
1253 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
1254 else
1255 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
1256 }
1257
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001258 /* Write global comment */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001259 if (zip->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001260 {
1261 comment_size = (int32_t)strlen(zip->comment);
1262 if (comment_size > UINT16_MAX)
1263 comment_size = UINT16_MAX;
1264 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001265 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001266 err = mz_stream_write_uint16(zip->stream, (uint16_t)comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001267 if (err == MZ_OK)
1268 {
1269 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
1270 err = MZ_READ_ERROR;
1271 }
1272 return err;
1273}
1274
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001275static int32_t mz_zip_recover_cd(void *handle)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001276{
1277 mz_zip *zip = (mz_zip *)handle;
1278 mz_zip_file local_file_info;
1279 void *local_file_info_stream = NULL;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001280 void *cd_mem_stream = NULL;
1281 uint64_t number_entry = 0;
1282 int64_t descriptor_pos = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001283 int64_t next_header_pos = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001284 int64_t disk_offset = 0;
1285 int64_t disk_number = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001286 int64_t compressed_pos = 0;
1287 int64_t compressed_end_pos = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001288 int64_t compressed_size = 0;
1289 int64_t uncompressed_size = 0;
1290 uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001291 uint8_t local_header_magic[4] = MZ_ZIP_MAGIC_LOCALHEADERU8;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001292 uint8_t central_header_magic[4] = MZ_ZIP_MAGIC_CENTRALHEADERU8;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001293 uint32_t crc32 = 0;
1294 int32_t disk_number_with_cd = 0;
1295 int32_t err = MZ_OK;
1296 uint8_t zip64 = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001297 uint8_t eof = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001298
1299
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001300 mz_zip_print("Zip - Recover - Start\n");
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001301
1302 mz_zip_get_cd_mem_stream(handle, &cd_mem_stream);
1303
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001304 /* Determine if we are on a split disk or not */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001305 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0);
1306 if (mz_stream_tell(zip->stream) < 0)
1307 {
1308 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1309 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1310 }
1311 else
1312 disk_number_with_cd = 1;
1313
1314 if (mz_stream_is_open(cd_mem_stream) != MZ_OK)
1315 err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001316
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001317 mz_stream_mem_create(&local_file_info_stream);
1318 mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri57cbcd22019-10-21 15:37:58 -07001319
1320 if (err == MZ_OK)
1321 {
1322 err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001323 INT64_MAX, &next_header_pos);
Nathan Moinvaziri57cbcd22019-10-21 15:37:58 -07001324 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001325
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001326 while (err == MZ_OK && !eof)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001327 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001328 /* Get current offset and disk number for central dir record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001329 disk_offset = mz_stream_tell(zip->stream);
1330 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1331
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001332 /* Read local headers */
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001333 memset(&local_file_info, 0, sizeof(local_file_info));
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001334 err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream);
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001335 if (err != MZ_OK)
1336 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001337
1338 local_file_info.disk_offset = disk_offset;
1339 if (disk_number < 0)
1340 disk_number = 0;
1341 local_file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri370ab032019-11-15 13:24:17 -08001342
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001343 compressed_pos = mz_stream_tell(zip->stream);
Nathan Moinvaziri370ab032019-11-15 13:24:17 -08001344
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001345 if ((err == MZ_OK) && (local_file_info.compressed_size > 0))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001346 {
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001347 mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001348 }
1349
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001350 while (1)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001351 {
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001352 /* Search for the next local header */
1353 err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
1354 INT64_MAX, &next_header_pos);
Nathan Moinvaziri370ab032019-11-15 13:24:17 -08001355
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001356 if (err == MZ_EXIST_ERROR)
1357 {
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001358 mz_stream_seek(zip->stream, compressed_pos, MZ_SEEK_SET);
1359
1360 /* Search for central dir if no local header found */
1361 err = mz_stream_find(zip->stream, (const void *)central_header_magic, sizeof(central_header_magic),
1362 INT64_MAX, &next_header_pos);
1363
1364 if (err == MZ_EXIST_ERROR)
1365 {
1366 /* Get end of stream if no central header found */
1367 mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1368 next_header_pos = mz_stream_tell(zip->stream);
1369 }
1370
1371 eof = 1;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001372 }
1373
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001374 if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR || local_file_info.compressed_size == 0)
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001375 {
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001376 /* Search backwards for the descriptor, seeking too far back will be incorrect if compressed size is small */
1377 err = mz_stream_find_reverse(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
1378 MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR, &descriptor_pos);
1379 if (err == MZ_OK)
1380 {
1381 if (mz_zip_extrafield_contains(local_file_info.extrafield,
1382 local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
1383 zip64 = 1;
1384
1385 err = mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32,
1386 &compressed_size, &uncompressed_size);
1387
1388 if (err == MZ_OK)
1389 {
1390 if (local_file_info.crc == 0)
1391 local_file_info.crc = crc32;
1392 if (local_file_info.compressed_size == 0)
1393 local_file_info.compressed_size = compressed_size;
1394 if (local_file_info.uncompressed_size == 0)
1395 local_file_info.uncompressed_size = uncompressed_size;
1396 }
1397
1398 compressed_end_pos = descriptor_pos;
1399 }
1400 else if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1401 {
1402 /* Wrong local file entry found, keep searching */
1403 next_header_pos += 1;
1404 mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
1405 continue;
1406 }
1407 }
1408 else
1409 {
1410 compressed_end_pos = next_header_pos;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001411 }
1412
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001413 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001414 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001415
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001416 compressed_size = compressed_end_pos - compressed_pos;
1417
1418 if (compressed_size > UINT32_MAX)
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001419 {
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001420 /* Update sizes if 4GB file is written with no ZIP64 support */
1421 if (local_file_info.uncompressed_size < UINT32_MAX)
1422 {
1423 local_file_info.compressed_size = compressed_size;
1424 local_file_info.uncompressed_size = 0;
1425 }
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001426 }
1427
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001428 mz_zip_print("Zip - Recover - Entry %s (csize %" PRId64 " usize %" PRId64 " flags 0x%" PRIx16 ")\n",
1429 local_file_info.filename, local_file_info.compressed_size, local_file_info.uncompressed_size,
1430 local_file_info.flag);
1431
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001432 /* Rewrite central dir with local headers and offsets */
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001433 err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001434 if (err == MZ_OK)
Nathan Moinvaziri370ab032019-11-15 13:24:17 -08001435 number_entry += 1;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001436
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001437 err = mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001438 }
1439
1440 mz_stream_mem_delete(&local_file_info_stream);
1441
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001442 mz_zip_print("Zip - Recover - Complete (cddisk %" PRId32 " entries %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001443 disk_number_with_cd, number_entry);
1444
1445 if (number_entry == 0)
1446 return err;
Nathan Moinvaziri370ab032019-11-15 13:24:17 -08001447
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001448 /* Set new upper seek boundary for central dir mem stream */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001449 disk_offset = mz_stream_tell(cd_mem_stream);
1450 mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset);
1451
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001452 /* Set new central directory info */
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001453 mz_zip_set_cd_stream(handle, 0, cd_mem_stream);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001454 mz_zip_set_number_entry(handle, number_entry);
1455 mz_zip_set_disk_number_with_cd(handle, disk_number_with_cd);
1456
1457 return MZ_OK;
1458}
1459
1460void *mz_zip_create(void **handle)
1461{
1462 mz_zip *zip = NULL;
1463
1464 zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
1465 if (zip != NULL)
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08001466 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001467 memset(zip, 0, sizeof(mz_zip));
Nathan Moinvaziri41fcda92020-01-04 16:55:21 -08001468 zip->data_descriptor = 1;
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08001469 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001470 if (handle != NULL)
1471 *handle = zip;
1472
1473 return zip;
1474}
1475
1476void mz_zip_delete(void **handle)
1477{
1478 mz_zip *zip = NULL;
1479 if (handle == NULL)
1480 return;
1481 zip = (mz_zip *)*handle;
1482 if (zip != NULL)
1483 {
1484 MZ_FREE(zip);
1485 }
1486 *handle = NULL;
1487}
1488
1489int32_t mz_zip_open(void *handle, void *stream, int32_t mode)
1490{
1491 mz_zip *zip = (mz_zip *)handle;
1492 int32_t err = MZ_OK;
1493
1494
1495 if (zip == NULL)
1496 return MZ_PARAM_ERROR;
1497
1498 mz_zip_print("Zip - Open\n");
1499
1500 zip->stream = stream;
1501
1502 mz_stream_mem_create(&zip->cd_mem_stream);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001503
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001504 if (mode & MZ_OPEN_MODE_WRITE)
1505 {
1506 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1507 zip->cd_stream = zip->cd_mem_stream;
1508 }
1509 else
1510 {
1511 zip->cd_stream = stream;
1512 }
1513
1514 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
1515 {
1516 if ((mode & MZ_OPEN_MODE_CREATE) == 0)
1517 {
1518 err = mz_zip_read_cd(zip);
1519 if (err != MZ_OK)
1520 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001521 mz_zip_print("Zip - Error detected reading cd (%" PRId32 ")\n", err);
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001522 if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001523 err = MZ_OK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001524 }
1525 }
1526
1527 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
1528 {
1529 if (zip->cd_size > 0)
1530 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001531 /* Store central directory in memory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001532 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1533 if (err == MZ_OK)
1534 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size);
1535 if (err == MZ_OK)
1536 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1537 }
1538 else
1539 {
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001540 if (zip->cd_signature == MZ_ZIP_MAGIC_ENDHEADER)
1541 {
1542 /* If tiny zip then overwrite end header */
1543 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1544 }
1545 else
1546 {
1547 /* If no central directory, append new zip to end of file */
1548 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1549 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001550 }
Nathan Moinvaziri2ce1fe22018-11-23 11:53:11 -08001551
1552 if (zip->disk_number_with_cd > 0)
1553 {
1554 /* Move to last disk to begin appending */
1555 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->disk_number_with_cd - 1);
1556 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001557 }
1558 else
1559 {
1560 zip->cd_start_pos = zip->cd_offset;
1561 }
1562 }
1563
1564 if (err != MZ_OK)
1565 {
1566 mz_zip_close(zip);
1567 return err;
1568 }
1569
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001570 /* Memory streams used to store variable length file info data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001571 mz_stream_mem_create(&zip->file_info_stream);
1572 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1573
1574 mz_stream_mem_create(&zip->local_file_info_stream);
1575 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1576
1577 zip->open_mode = mode;
1578
1579 return err;
1580}
1581
1582int32_t mz_zip_close(void *handle)
1583{
1584 mz_zip *zip = (mz_zip *)handle;
1585 int32_t err = MZ_OK;
1586
1587 if (zip == NULL)
1588 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001589
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001590 mz_zip_print("Zip - Close\n");
1591
1592 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001593 err = mz_zip_entry_close(handle);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001594
1595 if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE))
1596 err = mz_zip_write_cd(handle);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001597
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001598 if (zip->cd_mem_stream != NULL)
1599 {
1600 mz_stream_close(zip->cd_mem_stream);
1601 mz_stream_delete(&zip->cd_mem_stream);
1602 }
1603
1604 if (zip->file_info_stream != NULL)
1605 {
1606 mz_stream_mem_close(zip->file_info_stream);
1607 mz_stream_mem_delete(&zip->file_info_stream);
1608 }
1609 if (zip->local_file_info_stream != NULL)
1610 {
1611 mz_stream_mem_close(zip->local_file_info_stream);
1612 mz_stream_mem_delete(&zip->local_file_info_stream);
1613 }
1614
1615 if (zip->comment)
1616 {
1617 MZ_FREE(zip->comment);
1618 zip->comment = NULL;
1619 }
1620
1621 zip->stream = NULL;
1622 zip->cd_stream = NULL;
1623
1624 return err;
1625}
1626
1627int32_t mz_zip_get_comment(void *handle, const char **comment)
1628{
1629 mz_zip *zip = (mz_zip *)handle;
1630 if (zip == NULL || comment == NULL)
1631 return MZ_PARAM_ERROR;
1632 if (zip->comment == NULL)
1633 return MZ_EXIST_ERROR;
1634 *comment = zip->comment;
1635 return MZ_OK;
1636}
1637
1638int32_t mz_zip_set_comment(void *handle, const char *comment)
1639{
1640 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001641 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001642 if (zip == NULL || comment == NULL)
1643 return MZ_PARAM_ERROR;
1644 if (zip->comment != NULL)
1645 MZ_FREE(zip->comment);
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001646 comment_size = (int32_t)strlen(comment);
1647 if (comment_size > UINT16_MAX)
1648 return MZ_PARAM_ERROR;
Nathan Moinvaziria6ecab52019-01-22 13:14:01 -08001649 zip->comment = (char *)MZ_ALLOC(comment_size+1);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001650 if (zip->comment == NULL)
1651 return MZ_MEM_ERROR;
Nathan Moinvaziria6ecab52019-01-22 13:14:01 -08001652 memset(zip->comment, 0, comment_size+1);
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001653 strncpy(zip->comment, comment, comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001654 return MZ_OK;
1655}
1656
1657int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
1658{
1659 mz_zip *zip = (mz_zip *)handle;
1660 if (zip == NULL || version_madeby == NULL)
1661 return MZ_PARAM_ERROR;
1662 *version_madeby = zip->version_madeby;
1663 return MZ_OK;
1664}
1665
1666int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
1667{
1668 mz_zip *zip = (mz_zip *)handle;
1669 if (zip == NULL)
1670 return MZ_PARAM_ERROR;
1671 zip->version_madeby = version_madeby;
1672 return MZ_OK;
1673}
1674
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001675int32_t mz_zip_set_recover(void *handle, uint8_t recover)
1676{
1677 mz_zip *zip = (mz_zip *)handle;
1678 if (zip == NULL)
1679 return MZ_PARAM_ERROR;
1680 zip->recover = recover;
1681 return MZ_OK;
1682}
1683
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08001684int32_t mz_zip_set_data_descriptor(void *handle, uint8_t data_descriptor)
1685{
1686 mz_zip *zip = (mz_zip *)handle;
1687 if (zip == NULL)
1688 return MZ_PARAM_ERROR;
1689 zip->data_descriptor = data_descriptor;
1690 return MZ_OK;
1691}
1692
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001693int32_t mz_zip_get_stream(void *handle, void **stream)
1694{
1695 mz_zip *zip = (mz_zip *)handle;
1696 if (zip == NULL || stream == NULL)
1697 return MZ_PARAM_ERROR;
1698 *stream = zip->stream;
1699 if (*stream == NULL)
1700 return MZ_EXIST_ERROR;
1701 return MZ_OK;
1702}
1703
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001704int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001705{
1706 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001707 if (zip == NULL || cd_stream == NULL)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001708 return MZ_PARAM_ERROR;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001709 zip->cd_offset = 0;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001710 zip->cd_stream = cd_stream;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001711 zip->cd_start_pos = cd_start_pos;
1712 return MZ_OK;
1713}
1714
1715int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream)
1716{
1717 mz_zip *zip = (mz_zip *)handle;
1718 if (zip == NULL || cd_mem_stream == NULL)
1719 return MZ_PARAM_ERROR;
1720 *cd_mem_stream = zip->cd_mem_stream;
1721 if (*cd_mem_stream == NULL)
1722 return MZ_EXIST_ERROR;
1723 return MZ_OK;
1724}
1725
Nathan Moinvaziri163352f2020-02-08 21:28:23 -08001726int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry)
1727{
1728 mz_zip *zip = (mz_zip *)handle;
1729 if (zip == NULL)
1730 return MZ_PARAM_ERROR;
1731 zip->number_entry = number_entry;
1732 return MZ_OK;
1733}
1734
1735int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry)
1736{
1737 mz_zip *zip = (mz_zip *)handle;
1738 if (zip == NULL || number_entry == NULL)
1739 return MZ_PARAM_ERROR;
1740 *number_entry = zip->number_entry;
1741 return MZ_OK;
1742}
1743
1744int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd)
1745{
1746 mz_zip *zip = (mz_zip *)handle;
1747 if (zip == NULL)
1748 return MZ_PARAM_ERROR;
1749 zip->disk_number_with_cd = disk_number_with_cd;
1750 return MZ_OK;
1751}
1752
1753int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
1754{
1755 mz_zip *zip = (mz_zip *)handle;
1756 if (zip == NULL || disk_number_with_cd == NULL)
1757 return MZ_PARAM_ERROR;
1758 *disk_number_with_cd = zip->disk_number_with_cd;
1759 return MZ_OK;
1760}
1761
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001762static int32_t mz_zip_entry_close_int(void *handle)
1763{
1764 mz_zip *zip = (mz_zip *)handle;
1765
1766 if (zip->crypt_stream != NULL)
1767 mz_stream_delete(&zip->crypt_stream);
1768 zip->crypt_stream = NULL;
1769 if (zip->compress_stream != NULL)
1770 mz_stream_delete(&zip->compress_stream);
1771 zip->compress_stream = NULL;
1772
1773 zip->entry_opened = 0;
1774
1775 return MZ_OK;
1776}
1777
Nathan Moinvaziri26308b22018-08-08 09:40:15 -07001778static 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 -07001779{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001780 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001781 int64_t max_total_in = 0;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001782 int64_t header_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001783 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001784 int32_t err = MZ_OK;
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001785 uint8_t use_crypt = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001786
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001787 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001788 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001789
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001790 switch (zip->file_info.compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001791 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001792 case MZ_COMPRESS_METHOD_STORE:
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001793 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001794#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001795 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001796#endif
Nathan Moinvaziri566dfe02018-10-26 00:36:30 -07001797#ifdef HAVE_LZMA
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001798 case MZ_COMPRESS_METHOD_LZMA:
1799#endif
1800 err = MZ_OK;
1801 break;
1802 default:
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001803 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001804 }
1805
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001806#ifndef HAVE_WZAES
1807 if (zip->file_info.aes_version)
1808 return MZ_SUPPORT_ERROR;
1809#endif
1810
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001811 zip->entry_raw = raw;
1812
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001813 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password != NULL))
1814 {
1815 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
1816 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001817 /* Encrypt only when we are not trying to write raw and password is supplied. */
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001818 if (!zip->entry_raw)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001819 use_crypt = 1;
1820 }
1821 else if (zip->open_mode & MZ_OPEN_MODE_READ)
1822 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001823 /* Decrypt only when password is supplied. Don't error when password */
1824 /* is not supplied as we may want to read the raw encrypted data. */
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001825 use_crypt = 1;
1826 }
1827 }
1828
1829 if ((err == MZ_OK) && (use_crypt))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001830 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001831#ifdef HAVE_WZAES
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001832 if (zip->file_info.aes_version)
1833 {
Nathan Moinvaziri21a31022018-10-24 09:50:16 -07001834 mz_stream_wzaes_create(&zip->crypt_stream);
1835 mz_stream_wzaes_set_password(zip->crypt_stream, password);
1836 mz_stream_wzaes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001837 }
1838 else
1839#endif
1840 {
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001841#ifdef HAVE_PKCRYPT
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001842 uint8_t verify1 = 0;
1843 uint8_t verify2 = 0;
1844
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001845 /* Info-ZIP modification to ZipCrypto format: */
1846 /* 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 -07001847
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001848 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1849 {
1850 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001851
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001852 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1853
1854 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1855 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1856 }
1857 else
1858 {
1859 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1860 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1861 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001862
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001863 mz_stream_pkcrypt_create(&zip->crypt_stream);
1864 mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1865 mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001866#endif
1867 }
1868 }
1869
1870 if (err == MZ_OK)
1871 {
1872 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001873 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001874
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001875 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001876
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001877 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001878 }
1879
1880 if (err == MZ_OK)
1881 {
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001882 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001883 mz_stream_raw_create(&zip->compress_stream);
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001884#if defined(HAVE_ZLIB) || defined(HAVE_LIBCOMP)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001885 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001886 mz_stream_zlib_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001887#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001888#ifdef HAVE_BZIP2
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001889 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001890 mz_stream_bzip_create(&zip->compress_stream);
1891#endif
1892#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001893 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001894 mz_stream_lzma_create(&zip->compress_stream);
1895#endif
1896 else
1897 err = MZ_PARAM_ERROR;
1898 }
1899
1900 if (err == MZ_OK)
1901 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001902 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001903 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001904 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001905 }
1906 else
1907 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001908#ifndef HAVE_LIBCOMP
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001909 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 -08001910#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001911 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001912 max_total_in = zip->file_info.compressed_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001913 mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1914
1915 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1916 max_total_in -= header_size;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001917 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1918 max_total_in -= footer_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001919
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001920 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001921 }
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001922 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 -03001923 {
1924 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1925 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1926 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001927 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001928
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001929 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1930
1931 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1932 }
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001933
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001934 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001935 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001936 zip->entry_opened = 1;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001937 zip->entry_crc32 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001938 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001939 else
1940 {
1941 mz_zip_entry_close_int(handle);
1942 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001943
1944 return err;
1945}
1946
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001947int32_t mz_zip_entry_is_open(void *handle)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001948{
1949 mz_zip *zip = (mz_zip *)handle;
1950 if (zip == NULL)
1951 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001952 if (zip->entry_opened == 0)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001953 return MZ_EXIST_ERROR;
1954 return MZ_OK;
1955}
1956
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001957static int32_t mz_zip_seek_to_local_header(void *handle)
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001958{
1959 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri41fcda92020-01-04 16:55:21 -08001960 int64_t disk_size = 0;
Nathan Moinvazirid7e6aa02020-02-12 08:31:21 -08001961 uint32_t disk_number = zip->file_info.disk_number;
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001962
Nathan Moinvaziri41fcda92020-01-04 16:55:21 -08001963 if (disk_number == zip->disk_number_with_cd)
1964 {
1965 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size);
1966 if ((disk_size == 0) || ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0))
Nathan Moinvaziri16704822020-06-07 18:40:32 -07001967 disk_number = (uint32_t)-1;
Nathan Moinvaziri41fcda92020-01-04 16:55:21 -08001968 }
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001969
Nathan Moinvaziri41fcda92020-01-04 16:55:21 -08001970 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, disk_number);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001971
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001972 mz_zip_print("Zip - Entry - Seek local (disk %" PRId32 " offset %" PRId64 ")\n",
Nathan Moinvaziri41fcda92020-01-04 16:55:21 -08001973 disk_number, zip->file_info.disk_offset);
Nathan Moinvaziri09725902019-02-20 09:15:48 -08001974
1975 /* Guard against seek overflows */
1976 if ((zip->disk_offset_shift > 0) &&
1977 (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
1978 return MZ_FORMAT_ERROR;
1979
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001980 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 -07001981}
1982
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001983int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001984{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001985 mz_zip *zip = (mz_zip *)handle;
1986 int32_t err = MZ_OK;
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001987 int32_t err_shift = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001988
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001989#if defined(MZ_ZIP_NO_ENCRYPTION)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001990 if (password != NULL)
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001991 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001992#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001993 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001994 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001995 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001996 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001997 if (zip->entry_scanned == 0)
1998 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001999
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002000 mz_zip_print("Zip - Entry - Read open (raw %" PRId32 ")\n", raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002001
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002002 err = mz_zip_seek_to_local_header(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002003 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002004 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 -07002005
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08002006 if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0)
2007 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002008 /* Perhaps we didn't compensated correctly for incorrect cd offset */
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08002009 err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
2010 if (err_shift == MZ_OK)
2011 err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
2012 if (err_shift == MZ_OK)
2013 {
2014 zip->disk_offset_shift = 0;
2015 err = err_shift;
2016 }
2017 }
2018
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07002019#ifdef MZ_ZIP_NO_DECOMPRESSION
Nathan Moinvaziri2eb29072018-11-23 10:32:21 -08002020 if (!raw && zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002021 err = MZ_SUPPORT_ERROR;
Viktor Szakatse7215072018-09-04 15:06:53 +00002022#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002023 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002024 err = mz_zip_entry_open_int(handle, raw, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002025
2026 return err;
2027}
2028
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002029int32_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 -07002030{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002031 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002032 int64_t filename_pos = -1;
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08002033 int64_t extrafield_pos = 0;
2034 int64_t comment_pos = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002035 int64_t linkname_pos = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002036 int64_t disk_number = 0;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002037 uint8_t is_dir = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002038 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002039
Nathan Moinvaziri40427632018-07-22 11:12:40 -07002040#if defined(MZ_ZIP_NO_ENCRYPTION)
tz-lomb1b25802017-11-10 15:03:02 +03002041 if (password != NULL)
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08002042 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002043#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002044 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002045 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002046
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002047 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002048 {
2049 err = mz_zip_entry_close(handle);
2050 if (err != MZ_OK)
2051 return err;
2052 }
2053
2054 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002055
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002056 mz_zip_print("Zip - Entry - Write open - %s (level %" PRId16 " raw %" PRId8 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002057 zip->file_info.filename, compress_level, raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002058
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002059 mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07002060 mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
2061
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002062 /* Copy filename, extrafield, and comment internally */
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002063 filename_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07002064 if (file_info->filename != NULL)
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002065 mz_stream_write(zip->file_info_stream, file_info->filename, (int32_t)strlen(file_info->filename));
2066 mz_stream_write_uint8(zip->file_info_stream, 0);
2067
2068 extrafield_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07002069 if (file_info->extrafield != NULL)
Nathan Moinvazirica014e62018-08-15 07:31:12 -07002070 mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002071 mz_stream_write_uint8(zip->file_info_stream, 0);
2072
2073 comment_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07002074 if (file_info->comment != NULL)
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002075 mz_stream_write(zip->file_info_stream, file_info->comment, file_info->comment_size);
2076 mz_stream_write_uint8(zip->file_info_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002077
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002078 linkname_pos = mz_stream_tell(zip->file_info_stream);
2079 if (file_info->linkname != NULL)
2080 mz_stream_write(zip->file_info_stream, file_info->linkname, (int32_t)strlen(file_info->linkname));
2081 mz_stream_write_uint8(zip->file_info_stream, 0);
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002082
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08002083 mz_stream_mem_get_buffer_at(zip->file_info_stream, filename_pos, (const void **)&zip->file_info.filename);
2084 mz_stream_mem_get_buffer_at(zip->file_info_stream, extrafield_pos, (const void **)&zip->file_info.extrafield);
2085 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 -07002086 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 -07002087
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002088 if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002089 {
2090 if ((compress_level == 8) || (compress_level == 9))
2091 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
2092 if (compress_level == 2)
2093 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
2094 if (compress_level == 1)
2095 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
2096 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002097#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002098 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002099 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002100#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002101
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002102 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2103 is_dir = 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002104
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002105 if (!is_dir)
2106 {
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08002107 if (zip->data_descriptor)
2108 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002109 if (password != NULL)
2110 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
2111 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002112
juaniib8887e92018-02-14 00:51:05 -03002113 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
2114 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002115
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002116 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07002117 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002118 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002119
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08002120#ifdef HAVE_WZAES
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002121 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
2122 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
2123#endif
2124
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002125 if ((compress_level == 0) || (is_dir))
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002126 zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
Viktor Szakats2884e672018-04-30 08:12:13 +00002127
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07002128#ifdef MZ_ZIP_NO_COMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002129 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002130 err = MZ_SUPPORT_ERROR;
2131#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002132 if (err == MZ_OK)
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002133 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002134 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002135 err = mz_zip_entry_open_int(handle, raw, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002136
2137 return err;
2138}
2139
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002140int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002141{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002142 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002143 int32_t read = 0;
2144
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002145 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002146 return MZ_PARAM_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002147 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) /* zlib limitation */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002148 return MZ_PARAM_ERROR;
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07002149 if (len == 0)
2150 return MZ_PARAM_ERROR;
2151
Nathan Moinvazirieb80cd92018-07-11 16:45:09 -07002152 if (zip->file_info.compressed_size == 0)
2153 return 0;
2154
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002155 /* Read entire entry even if uncompressed_size = 0, otherwise */
2156 /* aes encryption validation will fail if compressed_size > 0 */
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07002157 read = mz_stream_read(zip->compress_stream, buf, len);
2158 if (read > 0)
2159 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002160
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002161 mz_zip_print("Zip - Entry - Read - %" PRId32 " (max %" PRId32 ")\n", read, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002162
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002163 return read;
2164}
2165
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002166int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002167{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002168 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002169 int32_t written = 0;
2170
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002171 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002172 return MZ_PARAM_ERROR;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07002173 written = mz_stream_write(zip->compress_stream, buf, len);
2174 if (written > 0)
2175 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002176
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002177 mz_zip_print("Zip - Entry - Write - %" PRId32 " (max %" PRId32 ")\n", written, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002178
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002179 return written;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002180}
2181
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002182int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002183 int64_t *uncompressed_size)
2184{
2185 mz_zip *zip = (mz_zip *)handle;
2186 int64_t total_in = 0;
2187 int32_t err = MZ_OK;
2188 uint8_t zip64 = 0;
2189
2190 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2191 return MZ_PARAM_ERROR;
2192
2193 mz_stream_close(zip->compress_stream);
2194
2195 mz_zip_print("Zip - Entry - Read Close\n");
2196
2197 if (crc32 != NULL)
2198 *crc32 = zip->file_info.crc;
2199 if (compressed_size != NULL)
2200 *compressed_size = zip->file_info.compressed_size;
2201 if (uncompressed_size != NULL)
2202 *uncompressed_size = zip->file_info.uncompressed_size;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002203
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002204 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
2205
2206 if ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) &&
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002207 ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0) &&
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002208 (crc32 != NULL || compressed_size != NULL || uncompressed_size != NULL))
2209 {
2210 /* Check to see if data descriptor is zip64 bit format or not */
2211 if (mz_zip_extrafield_contains(zip->local_file_info.extrafield,
2212 zip->local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
2213 zip64 = 1;
2214
2215 err = mz_zip_seek_to_local_header(handle);
2216
2217 /* Seek to end of compressed stream since we might have over-read during compression */
2218 if (err == MZ_OK)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002219 err = mz_stream_seek(zip->stream, MZ_ZIP_SIZE_LD_ITEM +
2220 (int64_t)zip->local_file_info.filename_size +
2221 (int64_t)zip->local_file_info.extrafield_size +
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002222 total_in, MZ_SEEK_CUR);
2223
2224 /* Read data descriptor */
2225 if (err == MZ_OK)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002226 err = mz_zip_entry_read_descriptor(zip->stream, zip64,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002227 crc32, compressed_size, uncompressed_size);
2228 }
2229
2230 /* If entire entry was not read verification will fail */
2231 if ((err == MZ_OK) && (total_in > 0) && (!zip->entry_raw))
2232 {
2233#ifdef HAVE_WZAES
2234 /* AES zip version AE-1 will expect a valid crc as well */
2235 if (zip->file_info.aes_version <= 0x0001)
2236#endif
2237 {
2238 if (zip->entry_crc32 != zip->file_info.crc)
2239 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002240 mz_zip_print("Zip - Entry - Crc failed (actual 0x%08" PRIx32 " expected 0x%08" PRIx32 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002241 zip->entry_crc32, zip->file_info.crc);
2242
2243 err = MZ_CRC_ERROR;
2244 }
2245 }
2246 }
2247
2248 mz_zip_entry_close_int(handle);
2249
2250 return err;
2251}
2252
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002253int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002254 int64_t uncompressed_size)
2255{
2256 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08002257 int64_t end_disk_number = 0;
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002258 int32_t err = MZ_OK;
2259 uint8_t zip64 = 0;
2260
2261 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2262 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002263
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002264 mz_stream_close(zip->compress_stream);
2265
2266 if (!zip->entry_raw)
2267 crc32 = zip->entry_crc32;
2268
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002269 mz_zip_print("Zip - Entry - Write Close (crc 0x%08" PRIx32 " cs %" PRId64 " ucs %" PRId64 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002270 crc32, compressed_size, uncompressed_size);
2271
2272 /* If sizes are not set, then read them from the compression stream */
2273 if (compressed_size < 0)
2274 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2275 if (uncompressed_size < 0)
Nathan Moinvazirid9318432018-12-01 07:56:28 -08002276 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002277
2278 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
2279 {
2280 mz_stream_set_base(zip->crypt_stream, zip->stream);
2281 err = mz_stream_close(zip->crypt_stream);
2282
2283 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2284 }
2285
2286 if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
2287 {
2288 /* Determine if we need to write data descriptor in zip64 format,
2289 if local extrafield was saved with zip64 extrafield */
2290 if (zip->file_info.zip64 == MZ_ZIP64_AUTO)
2291 {
2292 if (zip->file_info.uncompressed_size >= UINT32_MAX)
2293 zip64 = 1;
2294 if (zip->file_info.compressed_size >= UINT32_MAX)
2295 zip64 = 1;
2296 if (zip->file_info.disk_offset >= UINT32_MAX)
2297 zip64 = 1;
2298 else if (zip->file_info.uncompressed_size == 0)
2299 zip64 = 1;
2300 }
2301 else if (zip->file_info.zip64 == MZ_ZIP64_FORCE)
2302 {
2303 zip64 = 1;
2304 }
2305
2306 if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002307 err = mz_zip_entry_write_descriptor(zip->stream,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002308 zip64, 0, compressed_size, 0);
2309 else
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002310 err = mz_zip_entry_write_descriptor(zip->stream,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002311 zip64, crc32, compressed_size, uncompressed_size);
2312 }
2313
2314 /* Write file info to central directory */
2315
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002316 mz_zip_print("Zip - Entry - Write cd (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002317 uncompressed_size, compressed_size, crc32);
2318
2319 zip->file_info.crc = crc32;
2320 zip->file_info.compressed_size = compressed_size;
2321 zip->file_info.uncompressed_size = uncompressed_size;
2322
2323 if (err == MZ_OK)
2324 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
2325
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08002326 /* Update local header with crc32 and sizes */
2327 if ((err == MZ_OK) && ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) == 0) &&
2328 ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0))
2329 {
2330 /* Save the disk number and position we are to seek back after updating local header */
Nathan Moinvaziri58b37d32020-02-08 20:32:58 -08002331 int64_t end_pos = mz_stream_tell(zip->stream);
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08002332 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &end_disk_number);
2333
2334 err = mz_zip_seek_to_local_header(handle);
2335
2336 if (err == MZ_OK)
2337 {
2338 /* Seek to crc32 and sizes offset in local header */
2339 err = mz_stream_seek(zip->stream, MZ_ZIP_OFFSET_CRC_SIZES, MZ_SEEK_CUR);
2340 }
2341
2342 if (err == MZ_OK)
Nathan Moinvazirid7e6aa02020-02-12 08:31:21 -08002343 err = mz_zip_entry_write_crc_sizes(zip->stream, 0, &zip->file_info);
Nathan Moinvaziri6c87ff42020-01-03 16:58:16 -08002344
2345 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, end_disk_number);
2346 mz_stream_seek(zip->stream, end_pos, MZ_SEEK_SET);
2347 }
2348
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002349 zip->number_entry += 1;
2350
2351 mz_zip_entry_close_int(handle);
2352
2353 return err;
2354}
2355
Nathan Moinvaziri163352f2020-02-08 21:28:23 -08002356int32_t mz_zip_entry_close(void *handle)
2357{
2358 return mz_zip_entry_close_raw(handle, UINT64_MAX, 0);
2359}
2360
2361int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32)
2362{
2363 mz_zip *zip = (mz_zip *)handle;
2364 int32_t err = MZ_OK;
2365
2366 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2367 return MZ_PARAM_ERROR;
2368
2369 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
2370 err = mz_zip_entry_write_close(handle, crc32, UINT64_MAX, uncompressed_size);
2371 else
2372 err = mz_zip_entry_read_close(handle, NULL, NULL, NULL);
2373
2374 return err;
2375}
2376
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002377int32_t mz_zip_entry_is_dir(void *handle)
2378{
2379 mz_zip *zip = (mz_zip *)handle;
2380 int32_t filename_length = 0;
2381
2382 if (zip == NULL)
2383 return MZ_PARAM_ERROR;
2384 if (zip->entry_scanned == 0)
2385 return MZ_PARAM_ERROR;
2386 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2387 return MZ_OK;
2388
2389 filename_length = (int32_t)strlen(zip->file_info.filename);
2390 if (filename_length > 0)
2391 {
2392 if ((zip->file_info.filename[filename_length - 1] == '/') ||
2393 (zip->file_info.filename[filename_length - 1] == '\\'))
2394 return MZ_OK;
2395 }
2396 return MZ_EXIST_ERROR;
2397}
2398
Nathan Moinvaziri3249eac2019-05-02 21:07:39 -07002399int32_t mz_zip_entry_is_symlink(void *handle)
2400{
2401 mz_zip *zip = (mz_zip *)handle;
2402
2403 if (zip == NULL)
2404 return MZ_PARAM_ERROR;
2405 if (zip->entry_scanned == 0)
2406 return MZ_PARAM_ERROR;
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002407 if (mz_zip_attrib_is_symlink(zip->file_info.external_fa, zip->file_info.version_madeby) != MZ_OK)
2408 return MZ_EXIST_ERROR;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002409 if (zip->file_info.linkname == NULL || *zip->file_info.linkname == 0)
2410 return MZ_EXIST_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002411
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002412 return MZ_OK;
Nathan Moinvaziri3249eac2019-05-02 21:07:39 -07002413}
2414
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002415int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002416{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002417 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002418
2419 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002420 return MZ_PARAM_ERROR;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002421
2422 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
2423 {
2424 if (!zip->entry_scanned)
2425 return MZ_PARAM_ERROR;
2426 }
2427
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002428 *file_info = &zip->file_info;
2429 return MZ_OK;
2430}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002431
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002432int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002433{
2434 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002435 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002436 return MZ_PARAM_ERROR;
2437 *local_file_info = &zip->local_file_info;
2438 return MZ_OK;
2439}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002440
Nathan Moinvaziri915c5132018-10-26 20:00:52 -07002441int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size)
2442{
2443 mz_zip *zip = (mz_zip *)handle;
2444
2445 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2446 return MZ_PARAM_ERROR;
2447
2448 zip->file_info.extrafield = extrafield;
2449 zip->file_info.extrafield_size = extrafield_size;
2450 return MZ_OK;
2451}
2452
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002453static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002454{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002455 mz_zip *zip = (mz_zip *)handle;
2456 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002457
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002458 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002459 return MZ_PARAM_ERROR;
2460
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002461 zip->entry_scanned = 0;
2462
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002463 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00002464
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002465 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002466 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002467 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002468 if (err == MZ_OK)
2469 zip->entry_scanned = 1;
2470 return err;
2471}
2472
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002473int64_t mz_zip_get_entry(void *handle)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002474{
2475 mz_zip *zip = (mz_zip *)handle;
2476
2477 if (zip == NULL)
2478 return MZ_PARAM_ERROR;
2479
2480 return zip->cd_current_pos;
2481}
2482
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002483int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002484{
2485 mz_zip *zip = (mz_zip *)handle;
2486
2487 if (zip == NULL)
2488 return MZ_PARAM_ERROR;
2489
2490 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
2491 return MZ_PARAM_ERROR;
2492
2493 zip->cd_current_pos = cd_pos;
2494
2495 return mz_zip_goto_next_entry_int(handle);
2496}
2497
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002498int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002499{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002500 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002501
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002502 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002503 return MZ_PARAM_ERROR;
2504
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002505 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002506
2507 return mz_zip_goto_next_entry_int(handle);
2508}
2509
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002510int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002511{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002512 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002513
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002514 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002515 return MZ_PARAM_ERROR;
2516
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002517 zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002518 zip->file_info.extrafield_size + zip->file_info.comment_size;
2519
2520 return mz_zip_goto_next_entry_int(handle);
2521}
2522
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002523int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002524{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002525 mz_zip *zip = (mz_zip *)handle;
2526 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002527 int32_t result = 0;
2528
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002529 if (zip == NULL || filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002530 return MZ_PARAM_ERROR;
2531
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002532 /* If we are already on the current entry, no need to search */
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002533 if ((zip->entry_scanned) && (zip->file_info.filename != NULL))
2534 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002535 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002536 if (result == 0)
2537 return MZ_OK;
2538 }
2539
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002540 /* Search all entries starting at the first */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002541 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002542 while (err == MZ_OK)
2543 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002544 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2545 if (result == 0)
2546 return MZ_OK;
2547
2548 err = mz_zip_goto_next_entry(handle);
2549 }
2550
2551 return err;
2552}
2553
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002554int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002555{
2556 mz_zip *zip = (mz_zip *)handle;
2557 int32_t err = MZ_OK;
2558 int32_t result = 0;
2559
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002560 /* Search first entry looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002561 err = mz_zip_goto_first_entry(handle);
2562 if (err != MZ_OK)
2563 return err;
2564
2565 result = cb(handle, userdata, &zip->file_info);
2566 if (result == 0)
2567 return MZ_OK;
2568
2569 return mz_zip_locate_next_entry(handle, userdata, cb);
2570}
2571
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002572int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002573{
2574 mz_zip *zip = (mz_zip *)handle;
2575 int32_t err = MZ_OK;
2576 int32_t result = 0;
2577
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002578 /* Search next entries looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002579 err = mz_zip_goto_next_entry(handle);
2580 while (err == MZ_OK)
2581 {
2582 result = cb(handle, userdata, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002583 if (result == 0)
2584 return MZ_OK;
2585
2586 err = mz_zip_goto_next_entry(handle);
2587 }
2588
2589 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002590}
2591
2592/***************************************************************************/
2593
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002594int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002595{
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002596 uint32_t posix_attrib = 0;
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002597 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002598 int32_t err = MZ_OK;
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002599
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002600 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2601 if (err == MZ_OK)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002602 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002603 if ((posix_attrib & 0170000) == 0040000) /* S_ISDIR */
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002604 return MZ_OK;
2605 }
2606
2607 return MZ_EXIST_ERROR;
2608}
2609
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002610int32_t mz_zip_attrib_is_symlink(uint32_t attrib, int32_t version_madeby)
2611{
2612 uint32_t posix_attrib = 0;
2613 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
2614 int32_t err = MZ_OK;
2615
2616 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2617 if (err == MZ_OK)
2618 {
2619 if ((posix_attrib & 0170000) == 0120000) /* S_ISLNK */
2620 return MZ_OK;
2621 }
2622
2623 return MZ_EXIST_ERROR;
2624}
2625
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002626int32_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 -07002627{
2628 if (target_attrib == NULL)
2629 return MZ_PARAM_ERROR;
2630
2631 *target_attrib = 0;
2632
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002633 if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002634 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002635 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002636 {
2637 *target_attrib = src_attrib;
2638 return MZ_OK;
2639 }
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002640 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 -07002641 return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
2642 }
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002643 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 -07002644 {
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002645 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 -07002646 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002647 /* If high bytes are set, it contains unix specific attributes */
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002648 if ((src_attrib >> 16) != 0)
2649 src_attrib >>= 16;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002650
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002651 *target_attrib = src_attrib;
2652 return MZ_OK;
2653 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002654 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002655 return mz_zip_attrib_posix_to_win32(src_attrib, target_attrib);
2656 }
2657
2658 return MZ_SUPPORT_ERROR;
2659}
2660
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002661int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002662{
2663 if (win32_attrib == NULL)
2664 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002665
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002666 *win32_attrib = 0;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002667
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002668 /* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002669 if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002670 *win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002671 /* S_IFLNK */
2672 if ((posix_attrib & 0170000) == 0120000)
2673 *win32_attrib |= 0x400; /* FILE_ATTRIBUTE_REPARSE_POINT */
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002674 /* S_IFDIR */
Nathan Moinvaziri7d94b1f2019-05-08 23:41:47 -07002675 else if ((posix_attrib & 0170000) == 0040000)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002676 *win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
2677 /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002678 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002679 *win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002680
2681 return MZ_OK;
2682}
2683
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002684int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002685{
2686 if (posix_attrib == NULL)
2687 return MZ_PARAM_ERROR;
2688
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002689 *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
2690 /* FILE_ATTRIBUTE_READONLY */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002691 if ((win32_attrib & 0x01) == 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002692 *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002693 /* FILE_ATTRIBUTE_REPARSE_POINT */
2694 if ((win32_attrib & 0x400) == 0x400)
2695 *posix_attrib |= 0120000; /* S_IFLNK */
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002696 /* FILE_ATTRIBUTE_DIRECTORY */
Nathan Moinvaziri7d94b1f2019-05-08 23:41:47 -07002697 else if ((win32_attrib & 0x10) == 0x10)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002698 *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002699 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002700 *posix_attrib |= 0100000; /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002701
2702 return MZ_OK;
2703}
2704
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002705/***************************************************************************/
2706
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002707int32_t mz_zip_extrafield_find(void *stream, uint16_t type, uint16_t *length)
2708{
2709 int32_t err = MZ_OK;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002710 uint16_t field_type = 0;
2711 uint16_t field_length = 0;
2712
2713 do
2714 {
2715 err = mz_stream_read_uint16(stream, &field_type);
2716 if (err == MZ_OK)
2717 err = mz_stream_read_uint16(stream, &field_length);
2718 if (err != MZ_OK)
2719 break;
2720
2721 if (type == field_type)
2722 {
2723 if (length != NULL)
2724 *length = field_length;
2725 return MZ_OK;
2726 }
2727
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002728 err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR);
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002729 }
2730 while (err == MZ_OK);
2731
2732 return MZ_EXIST_ERROR;
2733}
2734
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002735int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size,
2736 uint16_t type, uint16_t *length)
2737{
2738 void *file_extra_stream = NULL;
2739 int32_t err = MZ_OK;
2740
2741 if (extrafield == NULL || extrafield_size == 0)
2742 return MZ_PARAM_ERROR;
2743
2744 mz_stream_mem_create(&file_extra_stream);
2745 mz_stream_mem_set_buffer(file_extra_stream, (void *)extrafield, extrafield_size);
2746
2747 err = mz_zip_extrafield_find(file_extra_stream, type, length);
2748
2749 mz_stream_mem_delete(&file_extra_stream);
2750
2751 return err;
2752}
2753
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002754int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length)
2755{
2756 int32_t err = MZ_OK;
2757 if (type == NULL || length == NULL)
2758 return MZ_PARAM_ERROR;
2759 err = mz_stream_read_uint16(stream, type);
2760 if (err == MZ_OK)
2761 err = mz_stream_read_uint16(stream, length);
2762 return err;
2763}
2764
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002765int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002766{
2767 int32_t err = MZ_OK;
2768 err = mz_stream_write_uint16(stream, type);
2769 if (err == MZ_OK)
2770 err = mz_stream_write_uint16(stream, length);
2771 return err;
2772}
2773
2774/***************************************************************************/
2775
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002776static int32_t mz_zip_invalid_date(const struct tm *ptm)
2777{
2778#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002779 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002780 !datevalue_in_range(0, 11, ptm->tm_mon) ||
2781 !datevalue_in_range(1, 31, ptm->tm_mday) ||
2782 !datevalue_in_range(0, 23, ptm->tm_hour) ||
2783 !datevalue_in_range(0, 59, ptm->tm_min) ||
2784 !datevalue_in_range(0, 59, ptm->tm_sec));
2785#undef datevalue_in_range
2786}
2787
2788static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
2789{
2790 uint64_t date = (uint64_t)(dos_date >> 16);
2791
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002792 ptm->tm_mday = (uint16_t)(date & 0x1f);
2793 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
2794 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
2795 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
2796 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
2797 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002798 ptm->tm_isdst = -1;
2799}
2800
2801int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
2802{
2803 if (ptm == NULL)
2804 return MZ_PARAM_ERROR;
2805
2806 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
2807
2808 if (mz_zip_invalid_date(ptm))
2809 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002810 /* Invalid date stored, so don't return it */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002811 memset(ptm, 0, sizeof(struct tm));
2812 return MZ_FORMAT_ERROR;
2813 }
2814 return MZ_OK;
2815}
2816
2817time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
2818{
2819 struct tm ptm;
2820 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
2821 return mktime(&ptm);
2822}
2823
2824int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
2825{
Nathan Moinvaziricd3e9e12019-04-28 10:22:49 -07002826 struct tm ltm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002827 if (ptm == NULL)
2828 return MZ_PARAM_ERROR;
Nathan Moinvaziri2e0a20a2019-04-28 13:03:11 -07002829 if (localtime_r(&unix_time, &ltm) == NULL) /* Returns a 1900-based year */
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002830 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002831 /* Invalid date stored, so don't return it */
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002832 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002833 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002834 }
Nathan Moinvaziricd3e9e12019-04-28 10:22:49 -07002835 memcpy(ptm, &ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002836 return MZ_OK;
2837}
2838
2839uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
2840{
2841 struct tm ptm;
2842 mz_zip_time_t_to_tm(unix_time, &ptm);
2843 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
2844}
2845
2846uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
2847{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07002848 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002849
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002850 /* Years supported: */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002851
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002852 /* [00, 79] (assumed to be between 2000 and 2079) */
2853 /* [80, 207] (assumed to be between 1980 and 2107, typical output of old */
2854 /* software that does 'year-1900' to get a double digit year) */
2855 /* [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.) */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002856
2857 memcpy(&fixed_tm, ptm, sizeof(struct tm));
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002858 if (fixed_tm.tm_year >= 1980) /* range [1980, 2107] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002859 fixed_tm.tm_year -= 1980;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002860 else if (fixed_tm.tm_year >= 80) /* range [80, 207] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002861 fixed_tm.tm_year -= 80;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002862 else /* range [00, 79] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002863 fixed_tm.tm_year += 20;
2864
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002865 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002866 return 0;
2867
Anand K Mistry57b65f82018-11-09 10:52:19 +11002868 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 -07002869 (((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 -07002870}
2871
2872int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
2873{
2874 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
2875 return MZ_OK;
2876}
2877
2878int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
2879{
2880 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
2881 return MZ_OK;
2882}
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002883
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002884/***************************************************************************/
2885
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002886int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case)
2887{
2888 do
2889 {
2890 if ((*path1 == '\\' && *path2 == '/') ||
2891 (*path2 == '\\' && *path1 == '/'))
2892 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002893 /* Ignore comparison of path slashes */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002894 }
2895 else if (ignore_case)
2896 {
2897 if (tolower(*path1) != tolower(*path2))
2898 break;
2899 }
2900 else if (*path1 != *path2)
2901 {
2902 break;
2903 }
2904
2905 path1 += 1;
2906 path2 += 1;
2907 }
2908 while (*path1 != 0 && *path2 != 0);
2909
2910 if (ignore_case)
2911 return (int32_t)(tolower(*path1) - tolower(*path2));
2912
2913 return (int32_t)(*path1 - *path2);
2914}
2915
2916/***************************************************************************/