blob: c29f867996b5f8f045521ad870c861b4c547561f [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvaziri10ac2912019-09-18 16:55:43 -07002 Version 2.9.0, September 18, 2019
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08003 part of the MiniZip project
4
Nathan Moinvaziri2ca7f392019-01-08 16:07:10 -08005 Copyright (C) 2010-2019 Nathan Moinvaziri
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08006 https://github.com/nmoinvaz/minizip
Nathan Moinvaziri79cfab02018-08-17 12:21:06 -07007 Copyright (C) 2009-2010 Mathias Svensson
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08008 Modifications for Zip64 support
9 http://result42.com
Nathan Moinvaziri79cfab02018-08-17 12:21:06 -070010 Copyright (C) 2007-2008 Even Rouault
11 Modifications of Unzip for Zip64
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080012 Copyright (C) 1998-2010 Gilles Vollant
Viktor Szakats9dea6f22018-10-17 22:39:01 +000013 https://www.winimage.com/zLibDll/minizip.html
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080014
15 This program is distributed under the terms of the same license as zlib.
16 See the accompanying LICENSE file for the full text of the license.
17*/
18
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080019
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070020#include "mz.h"
Nathan Moinvaziri5f091882018-10-24 18:06:08 -070021#include "mz_crypt.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080022#include "mz_strm.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080023#ifdef HAVE_BZIP2
24# include "mz_strm_bzip.h"
25#endif
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -080026#ifdef HAVE_LIBCOMP
Nathan Moinvaziri90d31c72018-11-20 03:17:20 -080027# include "mz_strm_libcomp.h"
28#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080029#ifdef HAVE_LZMA
30# include "mz_strm_lzma.h"
31#endif
Nathan Moinvazirib47b41b2018-07-11 09:44:29 -070032#include "mz_strm_mem.h"
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -070033#ifdef HAVE_PKCRYPT
34# include "mz_strm_pkcrypt.h"
35#endif
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -080036#ifdef HAVE_WZAES
Nathan Moinvaziri21a31022018-10-24 09:50:16 -070037# include "mz_strm_wzaes.h"
38#endif
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -080039#ifdef HAVE_ZLIB
40# include "mz_strm_zlib.h"
41#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080042
43#include "mz_zip.h"
44
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080045#include <ctype.h> /* tolower */
Nathan Moinvazirie8496272018-11-21 15:05:58 -080046#include <stdio.h> /* snprintf */
Nathan Moinvaziri1ee609b2018-11-19 21:34:35 -080047
zedcf076662019-05-15 21:43:56 +030048#if defined(_MSC_VER) || defined(__MINGW32__)
Nathan Moinvaziri2e0a20a2019-04-28 13:03:11 -070049# define localtime_r(t1,t2) (localtime_s(t2,t1) == 0 ? t1 : NULL)
zed65447dd2019-05-16 22:19:08 +030050#endif
51#if defined(_MSC_VER) && (_MSC_VER < 1900)
52# define snprintf _snprintf
Nathan Moinvaziric565fa82018-10-19 08:48:33 -070053#endif
54
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080055/***************************************************************************/
56
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070057#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
Nathan Moinvaziribe483d82019-10-19 17:30:23 -070058#define MZ_ZIP_MAGIC_LOCALHEADERU8 { 0x50, 0x4b, 0x03, 0x04 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070059#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
Nathan Moinvaziri545d7292019-10-21 15:32:18 -070060#define MZ_ZIP_MAGIC_CENTRALHEADERU8 { 0x50, 0x4b, 0x01, 0x02 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070061#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080062#define MZ_ZIP_MAGIC_ENDHEADERU8 { 0x50, 0x4b, 0x05, 0x06 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070063#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
64#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
65#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080066#define MZ_ZIP_MAGIC_DATADESCRIPTORU8 { 0x50, 0x4b, 0x07, 0x08 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070067
Nathan Moinvaziriba1db162018-11-23 10:07:35 -080068#define MZ_ZIP_SIZE_LD_ITEM (30)
Nathan Moinvaziri638f31f2018-08-27 08:17:16 -070069#define MZ_ZIP_SIZE_CD_ITEM (46)
70#define MZ_ZIP_SIZE_CD_LOCATOR64 (20)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080071
Nathan Moinvaziric78c7822018-12-01 20:42:36 -080072#ifndef MZ_ZIP_EOCD_MAX_BACK
73#define MZ_ZIP_EOCD_MAX_BACK (1 << 20)
74#endif
75
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080076/***************************************************************************/
77
78typedef struct mz_zip_s
79{
80 mz_zip_file file_info;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070081 mz_zip_file local_file_info;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080082
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080083 void *stream; /* main stream */
84 void *cd_stream; /* pointer to the stream with the cd */
85 void *cd_mem_stream; /* memory stream for central directory */
86 void *compress_stream; /* compression stream */
87 void *crypt_stream; /* encryption stream */
88 void *file_info_stream; /* memory stream for storing file info */
89 void *local_file_info_stream; /* memory stream for storing local file info */
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080090
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070091 int32_t open_mode;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -080092 uint8_t recover;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070093
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080094 uint32_t disk_number_with_cd; /* number of the disk with the central dir */
95 int64_t disk_offset_shift; /* correction for zips that have wrong offset start of cd */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070096
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080097 int64_t cd_start_pos; /* pos of the first file in the central dir stream */
98 int64_t cd_current_pos; /* pos of the current file in the central dir */
99 int64_t cd_offset; /* offset of start of central directory */
100 int64_t cd_size; /* size of the central directory */
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -0700101 uint32_t cd_signature; /* signature of central directory */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700102
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800103 uint8_t entry_scanned; /* entry header information read ok */
104 uint8_t entry_opened; /* entry is open for read/write */
105 uint8_t entry_raw; /* entry opened with raw mode */
106 uint32_t entry_crc32; /* entry crc32 */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700107
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700108 uint64_t number_entry;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700109
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700110 uint16_t version_madeby;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700111 char *comment;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800112} mz_zip;
113
114/***************************************************************************/
115
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800116#if 0
117# define mz_zip_print printf
118#else
119# define mz_zip_print(fmt,...)
120#endif
121
122/***************************************************************************/
123
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800124/* Locate the end of central directory */
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700125static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800126{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700127 int64_t file_size = 0;
Nathan Moinvaziric78c7822018-12-01 20:42:36 -0800128 int64_t max_back = MZ_ZIP_EOCD_MAX_BACK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800129 uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8;
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700130 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700131
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700132 err = mz_stream_seek(stream, 0, MZ_SEEK_END);
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800133 if (err != MZ_OK)
134 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800135
136 file_size = mz_stream_tell(stream);
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800137
Nathan Moinvaziric78c7822018-12-01 20:42:36 -0800138 if (max_back <= 0 || max_back > file_size)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800139 max_back = file_size;
140
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800141 return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800142}
143
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800144/* Locate the end of central directory 64 of a zip file */
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700145static int32_t mz_zip_search_zip64_eocd(void *stream, const int64_t end_central_offset, int64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800146{
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700147 int64_t offset = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800148 uint32_t value32 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700149 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700150
151
152 *central_pos = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800153
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800154 /* Zip64 end of central directory locator */
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700155 err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800156 /* Read locator signature */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700157 if (err == MZ_OK)
158 {
159 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700160 if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700161 err = MZ_FORMAT_ERROR;
162 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800163 /* Number of the disk with the start of the zip64 end of central directory */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700164 if (err == MZ_OK)
165 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800166 /* Relative offset of the zip64 end of central directory record8 */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700167 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700168 err = mz_stream_read_uint64(stream, (uint64_t *)&offset);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800169 /* Total number of disks */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700170 if (err == MZ_OK)
171 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800172 /* Goto end of central directory record */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700173 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700174 err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800175 /* The signature */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700176 if (err == MZ_OK)
177 {
178 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700179 if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700180 err = MZ_FORMAT_ERROR;
181 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800182
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700183 if (err == MZ_OK)
184 *central_pos = offset;
185
186 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800187}
188
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800189/* Get info about the current file in the zip file */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700190static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file *file_info, void *file_extra_stream)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700191{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700192 uint64_t ntfs_time = 0;
193 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700194 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700195 uint32_t dos_date = 0;
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800196 uint32_t field_pos = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700197 uint16_t field_type = 0;
198 uint16_t field_length = 0;
199 uint32_t field_length_read = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700200 uint16_t ntfs_attrib_id = 0;
201 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700202 uint16_t linkname_size;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700203 uint16_t value16 = 0;
204 uint32_t value32 = 0;
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800205 int64_t extrafield_pos = 0;
206 int64_t comment_pos = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700207 int64_t linkname_pos = 0;
208 int64_t saved_pos = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700209 int32_t err = MZ_OK;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700210 char *linkname = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700211
212
213 memset(file_info, 0, sizeof(mz_zip_file));
214
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800215 /* Check the magic */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700216 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700217 if (err == MZ_END_OF_STREAM)
218 err = MZ_END_OF_LIST;
219 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700220 err = MZ_END_OF_LIST;
221 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
222 err = MZ_FORMAT_ERROR;
223 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
224 err = MZ_FORMAT_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000225
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800226 /* Read header fields */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700227 if (err == MZ_OK)
228 {
229 if (!local)
230 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
231 if (err == MZ_OK)
232 err = mz_stream_read_uint16(stream, &file_info->version_needed);
233 if (err == MZ_OK)
234 err = mz_stream_read_uint16(stream, &file_info->flag);
235 if (err == MZ_OK)
236 err = mz_stream_read_uint16(stream, &file_info->compression_method);
237 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700238 {
239 err = mz_stream_read_uint32(stream, &dos_date);
240 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
241 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700242 if (err == MZ_OK)
243 err = mz_stream_read_uint32(stream, &file_info->crc);
244 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700245 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700246 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700247 file_info->compressed_size = value32;
248 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700249 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700250 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700251 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700252 file_info->uncompressed_size = value32;
253 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700254 if (err == MZ_OK)
255 err = mz_stream_read_uint16(stream, &file_info->filename_size);
256 if (err == MZ_OK)
257 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
258 if (!local)
259 {
260 if (err == MZ_OK)
261 err = mz_stream_read_uint16(stream, &file_info->comment_size);
262 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700263 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700264 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700265 file_info->disk_number = value16;
266 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700267 if (err == MZ_OK)
268 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
269 if (err == MZ_OK)
270 err = mz_stream_read_uint32(stream, &file_info->external_fa);
271 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700272 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700273 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700274 file_info->disk_offset = value32;
275 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700276 }
277 }
278
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700279 if (err == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800280 err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700281
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800282 /* Copy variable length data to memory stream for later retrieval */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700283 if ((err == MZ_OK) && (file_info->filename_size > 0))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700284 err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800285 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800286 extrafield_pos = mz_stream_tell(file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700287
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800288 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
289 err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800290 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800291
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800292 comment_pos = mz_stream_tell(file_extra_stream);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800293 if ((err == MZ_OK) && (file_info->comment_size > 0))
294 err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800295 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700296
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700297 linkname_pos = mz_stream_tell(file_extra_stream);
298 /* Overwrite if we encounter UNIX1 extra block */
299 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700300
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700301 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
302 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800303 /* Seek to and parse the extra field */
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800304 err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700305
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800306 while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700307 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700308 err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length);
Nathan Moinvaziriea2bd792018-11-19 17:44:44 -0800309 if (err != MZ_OK)
310 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800311 field_pos += 4;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800312
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800313 /* Don't allow field length to exceed size of remaining extrafield */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800314 if (field_length > (file_info->extrafield_size - field_pos))
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -0800315 field_length = (uint16_t)(file_info->extrafield_size - field_pos);
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800316
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800317 /* Read ZIP64 extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800318 if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700319 {
320 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800321 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700322 err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800323 if (file_info->uncompressed_size < 0)
324 err = MZ_FORMAT_ERROR;
325 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700326 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800327 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700328 err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800329 if (file_info->compressed_size < 0)
330 err = MZ_FORMAT_ERROR;
331 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700332 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800333 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700334 err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800335 if (file_info->disk_offset < 0)
336 err = MZ_FORMAT_ERROR;
337 }
juanii4ae79922018-02-11 14:29:36 -0300338 if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700339 err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700340 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800341 /* Read NTFS extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800342 else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700343 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700344 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700345 err = mz_stream_read_uint32(file_extra_stream, &reserved);
346 field_length_read = 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700347
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800348 while ((err == MZ_OK) && (field_length_read + 4 <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700349 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700350 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700351 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700352 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800353 field_length_read += 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700354
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700355 if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700356 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700357 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700358 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
359
juanii7063b0e2018-02-11 13:56:21 -0300360 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700361 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700362 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700363 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
364 }
juanii7063b0e2018-02-11 13:56:21 -0300365 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700366 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700367 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700368 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
369 }
370 }
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800371 else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700372 {
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800373 err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700374 }
375
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800376 field_length_read += ntfs_attrib_size;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700377 }
378 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800379 /* Read UNIX1 extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800380 else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700381 {
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700382 if (err == MZ_OK)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700383 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700384 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700385 if (err == MZ_OK && file_info->accessed_date == 0)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700386 file_info->accessed_date = value32;
387 }
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700388 if (err == MZ_OK)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700389 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700390 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700391 if (err == MZ_OK && file_info->modified_date == 0)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700392 file_info->modified_date = value32;
393 }
394 if (err == MZ_OK)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800395 err = mz_stream_read_uint16(file_extra_stream, &value16); /* User id */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700396 if (err == MZ_OK)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800397 err = mz_stream_read_uint16(file_extra_stream, &value16); /* Group id */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700398
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700399 /* Copy linkname to end of file extra stream so we can return null
400 terminated string */
401 linkname_size = field_length - 12;
402 if ((err == MZ_OK) && (linkname_size > 0))
403 {
404 linkname = (char *)MZ_ALLOC(linkname_size);
405 if (linkname != NULL)
406 {
407 if (mz_stream_read(file_extra_stream, linkname, linkname_size) != linkname_size)
408 err = MZ_READ_ERROR;
409 if (err == MZ_OK)
410 {
411 saved_pos = mz_stream_tell(file_extra_stream);
412
413 mz_stream_seek(file_extra_stream, linkname_pos, MZ_SEEK_SET);
414 mz_stream_write(file_extra_stream, linkname, linkname_size);
415 mz_stream_write_uint8(file_extra_stream, 0);
416
417 mz_stream_seek(file_extra_stream, saved_pos, MZ_SEEK_SET);
418 }
419 MZ_FREE(linkname);
420 }
421 }
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700422 }
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800423#ifdef HAVE_WZAES
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800424 /* Read AES extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800425 else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700426 {
427 uint8_t value8 = 0;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800428 /* Verify version info */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700429 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800430 /* Support AE-1 and AE-2 */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700431 if (value16 != 1 && value16 != 2)
432 err = MZ_FORMAT_ERROR;
433 file_info->aes_version = value16;
434 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700435 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700436 if ((char)value8 != 'A')
437 err = MZ_FORMAT_ERROR;
438 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700439 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700440 if ((char)value8 != 'E')
441 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800442 /* Get AES encryption strength and actual compression method */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700443 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700444 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700445 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700446 file_info->aes_encryption_mode = value8;
447 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700448 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700449 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700450 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700451 file_info->compression_method = value16;
452 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700453 }
454#endif
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800455 else if (field_length > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700456 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700457 err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700458 }
459
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800460 field_pos += field_length;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700461 }
462 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700463
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800464 /* Get pointers to variable length data */
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800465 mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename);
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800466 mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield);
467 mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700468 mz_stream_mem_get_buffer_at(file_extra_stream, linkname_pos, (const void **)&file_info->linkname);
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800469
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800470 /* Set to empty string just in-case */
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800471 if (file_info->filename == NULL)
472 file_info->filename = "";
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800473 if (file_info->extrafield == NULL)
474 file_info->extrafield_size = 0;
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800475 if (file_info->comment == NULL)
476 file_info->comment = "";
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700477 if (file_info->linkname == NULL)
478 file_info->linkname = "";
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700479
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800480 if (err == MZ_OK)
481 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700482 mz_zip_print("Zip - Entry - Read header - %s (local %" PRId8 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800483 file_info->filename, local);
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700484 mz_zip_print("Zip - Entry - Read header compress (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800485 file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
486 if (!local)
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800487 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700488 mz_zip_print("Zip - Entry - Read header disk (disk %" PRIu32 " offset %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800489 file_info->disk_number, file_info->disk_offset);
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800490 }
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700491 mz_zip_print("Zip - Entry - Read header variable (fnl %" PRId32 " efs %" PRId32 " cms %" PRId32 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800492 file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
493 }
494
495 return err;
496}
497
498static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size)
499{
500 uint32_t value32 = 0;
501 int64_t value64 = 0;
502 int32_t err = MZ_OK;
503
504
505 err = mz_stream_read_uint32(stream, &value32);
506 if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR)
507 err = MZ_FORMAT_ERROR;
508 if (err == MZ_OK)
509 err = mz_stream_read_uint32(stream, &value32);
510 if ((err == MZ_OK) && (crc32 != NULL))
511 *crc32 = value32;
512 if (err == MZ_OK)
513 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800514 /* If zip 64 extension is enabled then read as 8 byte */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800515 if (!zip64)
516 {
517 err = mz_stream_read_uint32(stream, &value32);
518 value64 = value32;
519 }
520 else
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800521 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800522 err = mz_stream_read_int64(stream, &value64);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800523 if (value64 < 0)
524 err = MZ_FORMAT_ERROR;
525 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800526 if ((err == MZ_OK) && (compressed_size != NULL))
527 *compressed_size = value64;
528 }
529 if (err == MZ_OK)
530 {
531 if (!zip64)
532 {
533 err = mz_stream_read_uint32(stream, &value32);
534 value64 = value32;
535 }
536 else
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800537 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800538 err = mz_stream_read_int64(stream, &value64);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800539 if (value64 < 0)
540 err = MZ_FORMAT_ERROR;
541 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800542 if ((err == MZ_OK) && (uncompressed_size != NULL))
543 *uncompressed_size = value64;
544 }
545
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700546 return err;
547}
548
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700549static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700550{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700551 uint64_t ntfs_time = 0;
552 uint32_t reserved = 0;
553 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700554 uint16_t extrafield_size = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700555 uint16_t field_type = 0;
556 uint16_t field_length = 0;
557 uint16_t field_length_zip64 = 0;
558 uint16_t field_length_ntfs = 0;
559 uint16_t field_length_aes = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700560 uint16_t field_length_unix1 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700561 uint16_t filename_size = 0;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700562 uint16_t filename_length = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700563 uint16_t linkname_size = 0;
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700564 uint16_t version_needed = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800565 int32_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700566 int32_t err = MZ_OK;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700567 int32_t err_mem = MZ_OK;
568 uint8_t zip64 = 0;
569 uint8_t skip_aes = 0;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700570 uint8_t mask = 0;
571 uint8_t write_end_slash = 0;
572 const char *filename = NULL;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700573 char masked_name[64];
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700574 void *file_extra_stream = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700575
576 if (file_info == NULL)
577 return MZ_PARAM_ERROR;
578
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700579 if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO))
580 mask = 1;
581
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800582 /* Calculate extra field sizes */
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700583 if (file_info->uncompressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700584 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700585 if (file_info->compressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700586 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700587 if (file_info->disk_offset >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700588 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700589
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700590 if (file_info->zip64 == MZ_ZIP64_AUTO)
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700591 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800592 /* If uncompressed size is unknown, assume zip64 for 64-bit data descriptors */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700593 zip64 = (local && file_info->uncompressed_size == 0) || (field_length_zip64 > 0);
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700594 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700595 else if (file_info->zip64 == MZ_ZIP64_FORCE)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700596 {
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700597 zip64 = 1;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700598 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700599 else if (file_info->zip64 == MZ_ZIP64_DISABLE)
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700600 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800601 /* Zip64 extension is required to zip file */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700602 if (field_length_zip64 > 0)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700603 return MZ_PARAM_ERROR;
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700604 }
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700605
606 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700607 {
608 extrafield_size += 4;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700609 extrafield_size += field_length_zip64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700610 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700611
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800612 /* Calculate extra field size and check for duplicates */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700613 if (file_info->extrafield_size > 0)
614 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700615 mz_stream_mem_create(&file_extra_stream);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700616 mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
Nathan Moinvaziri915c5132018-10-26 20:00:52 -0700617 file_info->extrafield_size);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700618
619 do
620 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700621 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700622 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700623 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700624 if (err_mem != MZ_OK)
625 break;
626
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800627 /* Prefer incoming aes extensions over ours */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700628 if (field_type == MZ_ZIP_EXTENSION_AES)
629 skip_aes = 1;
630
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700631 /* Prefer our zip64, ntfs, unix1 extension over incoming */
632 if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS &&
633 field_type != MZ_ZIP_EXTENSION_UNIX1)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700634 extrafield_size += 4 + field_length;
635
636 if (err_mem == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800637 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700638 }
639 while (err_mem == MZ_OK);
640 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700641
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800642#ifdef HAVE_WZAES
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700643 if (!skip_aes)
644 {
645 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700646 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700647 field_length_aes = 1 + 1 + 1 + 2 + 2;
648 extrafield_size += 4 + field_length_aes;
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700649 }
650 }
Nathan Moinvaziria2bb08c2018-10-28 13:45:30 -0700651#else
Nathan Moinvaziri7de94cf2018-11-19 20:51:06 -0800652 MZ_UNUSED(field_length_aes);
Nathan Moinvaziri4ae469a2018-10-28 15:32:04 -0700653 MZ_UNUSED(skip_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700654#endif
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800655 /* NTFS timestamps */
Nathan Moinvazirid9e159a2018-02-13 15:30:30 -0800656 if ((file_info->modified_date != 0) &&
657 (file_info->accessed_date != 0) &&
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700658 (file_info->creation_date != 0) && (!mask))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700659 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700660 field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
661 extrafield_size += 4 + field_length_ntfs;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700662 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700663
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700664 /* Unix1 symbolic links */
665 if (file_info->linkname != NULL && *file_info->linkname != 0)
666 {
667 linkname_size = (uint16_t)strlen(file_info->linkname);
668 field_length_unix1 = 12 + linkname_size;
669 extrafield_size += 4 + field_length_unix1;
670 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700671
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700672 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700673 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700674 else
675 {
676 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
677 if (err == MZ_OK)
678 err = mz_stream_write_uint16(stream, file_info->version_madeby);
679 }
680
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800681 /* Calculate version needed to extract */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700682 if (err == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700683 {
684 version_needed = file_info->version_needed;
685 if (version_needed == 0)
686 {
687 version_needed = 20;
688 if (zip64)
689 version_needed = 45;
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800690#ifdef HAVE_WZAES
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700691 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
692 version_needed = 51;
693#endif
694#ifdef HAVE_LZMA
695 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
696 version_needed = 63;
697#endif
698 }
699 err = mz_stream_write_uint16(stream, version_needed);
700 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700701 if (err == MZ_OK)
702 err = mz_stream_write_uint16(stream, file_info->flag);
703 if (err == MZ_OK)
704 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800705#ifdef HAVE_WZAES
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700706 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700707 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700708 else
709#endif
710 err = mz_stream_write_uint16(stream, file_info->compression_method);
711 }
712 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700713 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700714 if (file_info->modified_date != 0 && !mask)
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -0700715 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700716 err = mz_stream_write_uint32(stream, dos_date);
717 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700718
719 if (err == MZ_OK)
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700720 {
721 if (mask)
722 err = mz_stream_write_uint32(stream, 0);
723 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800724 err = mz_stream_write_uint32(stream, file_info->crc); /* crc */
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700725 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700726 if (err == MZ_OK)
727 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800728 if (file_info->compressed_size >= UINT32_MAX) /* compr size */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700729 err = mz_stream_write_uint32(stream, UINT32_MAX);
730 else
731 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
732 }
733 if (err == MZ_OK)
734 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800735 if (file_info->uncompressed_size >= UINT32_MAX) /* uncompr size */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700736 err = mz_stream_write_uint32(stream, UINT32_MAX);
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700737 else if (mask)
738 err = mz_stream_write_uint32(stream, 0);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700739 else
740 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
741 }
742
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700743 if (mask)
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700744 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700745 snprintf(masked_name, sizeof(masked_name), "%" PRIx32 "_%" PRIx64,
Nathan Moinvaziri264dc182018-11-13 17:42:13 -0800746 file_info->disk_number, file_info->disk_offset);
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700747 filename = masked_name;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700748 }
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700749 else
750 {
751 filename = file_info->filename;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700752 }
753
754 filename_length = (uint16_t)strlen(filename);
755 filename_size += filename_length;
756
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800757 if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) &&
758 ((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\')))
759 {
760 filename_size += 1;
761 write_end_slash = 1;
762 }
763
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700764 if (err == MZ_OK)
765 err = mz_stream_write_uint16(stream, filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700766 if (err == MZ_OK)
767 err = mz_stream_write_uint16(stream, extrafield_size);
768
769 if (!local)
770 {
771 if (file_info->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800772 {
773 comment_size = (int32_t)strlen(file_info->comment);
774 if (comment_size > UINT16_MAX)
775 comment_size = UINT16_MAX;
776 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700777 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800778 err = mz_stream_write_uint16(stream, (uint16_t)comment_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700779 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700780 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700781 if (err == MZ_OK)
782 err = mz_stream_write_uint16(stream, file_info->internal_fa);
783 if (err == MZ_OK)
784 err = mz_stream_write_uint32(stream, file_info->external_fa);
785 if (err == MZ_OK)
786 {
787 if (file_info->disk_offset >= UINT32_MAX)
788 err = mz_stream_write_uint32(stream, UINT32_MAX);
789 else
790 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
791 }
792 }
793
794 if (err == MZ_OK)
795 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700796 if (mz_stream_write(stream, filename, filename_length) != filename_length)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700797 err = MZ_WRITE_ERROR;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700798
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800799 /* Ensure that directories have a slash appended to them for compatibility */
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700800 if (err == MZ_OK && write_end_slash)
801 err = mz_stream_write_uint8(stream, '/');
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700802 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700803
804 if (file_info->extrafield_size > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700805 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800806 err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700807 while (err == MZ_OK && err_mem == MZ_OK)
808 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700809 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700810 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700811 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700812 if (err_mem != MZ_OK)
813 break;
814
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700815 /* Prefer our zip 64, ntfs, unix1 extensions over incoming */
816 if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS ||
817 field_type == MZ_ZIP_EXTENSION_UNIX1)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700818 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800819 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700820 continue;
821 }
822
823 err = mz_stream_write_uint16(stream, field_type);
824 if (err == MZ_OK)
825 err = mz_stream_write_uint16(stream, field_length);
826 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700827 err = mz_stream_copy(stream, file_extra_stream, field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700828 }
829
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700830 mz_stream_mem_delete(&file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700831 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700832
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800833 /* Write ZIP64 extra field */
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700834 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700835 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700836 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700837 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700838 {
839 if (mask)
840 err = mz_stream_write_int64(stream, 0);
841 else
842 err = mz_stream_write_int64(stream, file_info->uncompressed_size);
843 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700844 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700845 err = mz_stream_write_int64(stream, file_info->compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700846 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700847 err = mz_stream_write_int64(stream, file_info->disk_offset);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700848 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800849 /* Write NTFS extra field */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700850 if ((err == MZ_OK) && (field_length_ntfs > 0))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700851 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700852 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700853 if (err == MZ_OK)
854 err = mz_stream_write_uint32(stream, reserved);
855 if (err == MZ_OK)
856 err = mz_stream_write_uint16(stream, 0x01);
857 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700858 err = mz_stream_write_uint16(stream, field_length_ntfs - 8);
juanii3679a3d2018-02-11 13:55:38 -0300859 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700860 {
861 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
862 err = mz_stream_write_uint64(stream, ntfs_time);
863 }
juanii3679a3d2018-02-11 13:55:38 -0300864 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700865 {
866 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
867 err = mz_stream_write_uint64(stream, ntfs_time);
868 }
juanii3679a3d2018-02-11 13:55:38 -0300869 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700870 {
871 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
872 err = mz_stream_write_uint64(stream, ntfs_time);
873 }
874 }
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700875 /* Write UNIX extra block extra field */
876 if ((err == MZ_OK) && (field_length_unix1 > 0))
877 {
878 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_UNIX1, field_length_unix1);
879 if (err == MZ_OK)
Nathan Moinvaziriaeb6cdd2019-05-06 13:40:32 -0700880 err = mz_stream_write_uint32(stream, (uint32_t)file_info->accessed_date);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700881 if (err == MZ_OK)
Nathan Moinvaziriaeb6cdd2019-05-06 13:40:32 -0700882 err = mz_stream_write_uint32(stream, (uint32_t)file_info->modified_date);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700883 if (err == MZ_OK) /* User id */
884 err = mz_stream_write_uint16(stream, 0);
885 if (err == MZ_OK) /* Group id */
886 err = mz_stream_write_uint16(stream, 0);
887 if (err == MZ_OK && linkname_size > 0)
888 {
889 if (mz_stream_write(stream, file_info->linkname, linkname_size) != linkname_size)
890 err = MZ_WRITE_ERROR;
891 }
892 }
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800893#ifdef HAVE_WZAES
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800894 /* Write AES extra field */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700895 if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700896 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700897 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700898 if (err == MZ_OK)
899 err = mz_stream_write_uint16(stream, file_info->aes_version);
900 if (err == MZ_OK)
901 err = mz_stream_write_uint8(stream, 'A');
902 if (err == MZ_OK)
903 err = mz_stream_write_uint8(stream, 'E');
904 if (err == MZ_OK)
905 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
906 if (err == MZ_OK)
907 err = mz_stream_write_uint16(stream, file_info->compression_method);
908 }
909#endif
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700910 if ((err == MZ_OK) && (!local) && (file_info->comment != NULL))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700911 {
Nathan Moinvazirica014e62018-08-15 07:31:12 -0700912 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700913 err = MZ_WRITE_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700914 }
915
916 return err;
917}
918
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800919static int32_t mz_zip_entry_write_descriptor(void *stream, uint8_t zip64, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800920{
921 int32_t err = MZ_OK;
922
923 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
924 if (err == MZ_OK)
925 err = mz_stream_write_uint32(stream, crc32);
926
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800927 /* Store data descriptor as 8 bytes if zip 64 extension enabled */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800928 if (err == MZ_OK)
929 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800930 /* Zip 64 extension is enabled when uncompressed size is > UINT32_MAX */
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800931 if (!zip64)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800932 err = mz_stream_write_uint32(stream, (uint32_t)compressed_size);
933 else
934 err = mz_stream_write_int64(stream, compressed_size);
935 }
936 if (err == MZ_OK)
937 {
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800938 if (!zip64)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800939 err = mz_stream_write_uint32(stream, (uint32_t)uncompressed_size);
940 else
941 err = mz_stream_write_int64(stream, uncompressed_size);
942 }
943
944 return err;
945}
946
947static int32_t mz_zip_read_cd(void *handle)
948{
949 mz_zip *zip = (mz_zip *)handle;
950 uint64_t number_entry_cd64 = 0;
951 uint64_t number_entry = 0;
952 uint64_t number_entry_cd = 0;
953 int64_t eocd_pos = 0;
954 int64_t eocd_pos64 = 0;
955 int64_t value64i = 0;
956 uint16_t value16 = 0;
957 uint32_t value32 = 0;
958 uint64_t value64 = 0;
959 uint16_t comment_size = 0;
960 int32_t comment_read = 0;
961 int32_t err = MZ_OK;
962
963
964 if (zip == NULL)
965 return MZ_PARAM_ERROR;
966
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800967 /* Read and cache central directory records */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800968 err = mz_zip_search_eocd(zip->stream, &eocd_pos);
969 if (err == MZ_OK)
970 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800971 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800972 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800973 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800974 if (err == MZ_OK)
975 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800976 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800977 if (err == MZ_OK)
978 err = mz_stream_read_uint16(zip->stream, &value16);
979 zip->disk_number_with_cd = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800980 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800981 if (err == MZ_OK)
982 err = mz_stream_read_uint16(zip->stream, &value16);
983 zip->number_entry = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800984 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800985 if (err == MZ_OK)
986 err = mz_stream_read_uint16(zip->stream, &value16);
987 number_entry_cd = value16;
988 if (number_entry_cd != zip->number_entry)
989 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800990 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800991 if (err == MZ_OK)
992 err = mz_stream_read_uint32(zip->stream, &value32);
993 if (err == MZ_OK)
994 zip->cd_size = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800995 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800996 if (err == MZ_OK)
997 err = mz_stream_read_uint32(zip->stream, &value32);
998 if (err == MZ_OK)
999 zip->cd_offset = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001000 /* Zip file global comment length */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001001 if (err == MZ_OK)
1002 err = mz_stream_read_uint16(zip->stream, &comment_size);
1003 if ((err == MZ_OK) && (comment_size > 0))
1004 {
1005 zip->comment = (char *)MZ_ALLOC(comment_size + 1);
1006 if (zip->comment != NULL)
1007 {
1008 comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001009 /* Don't fail if incorrect comment length read, not critical */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001010 if (comment_read < 0)
1011 comment_read = 0;
1012 zip->comment[comment_read] = 0;
1013 }
1014 }
1015
1016 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
1017 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001018 /* Format should be Zip64, as the central directory or file size is too large */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001019 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
1020 {
1021 eocd_pos = eocd_pos64;
1022
1023 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001024 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001025 if (err == MZ_OK)
1026 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001027 /* Size of zip64 end of central directory record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001028 if (err == MZ_OK)
1029 err = mz_stream_read_uint64(zip->stream, &value64);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001030 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001031 if (err == MZ_OK)
1032 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001033 /* Version needed to extract */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001034 if (err == MZ_OK)
1035 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001036 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001037 if (err == MZ_OK)
1038 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001039 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001040 if (err == MZ_OK)
1041 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001042 /* Total number of entries in the central directory on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001043 if (err == MZ_OK)
1044 err = mz_stream_read_uint64(zip->stream, &number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001045 /* Total number of entries in the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001046 if (err == MZ_OK)
1047 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
1048 if (number_entry == UINT32_MAX)
1049 zip->number_entry = number_entry_cd64;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001050 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001051 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001052 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001053 err = mz_stream_read_int64(zip->stream, &zip->cd_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001054 if (zip->cd_size < 0)
1055 err = MZ_FORMAT_ERROR;
1056 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001057 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001058 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001059 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001060 err = mz_stream_read_int64(zip->stream, &zip->cd_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001061 if (zip->cd_offset < 0)
1062 err = MZ_FORMAT_ERROR;
1063 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001064 }
1065 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
1066 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
1067 {
1068 err = MZ_FORMAT_ERROR;
1069 }
1070 }
1071 }
1072
1073 if (err == MZ_OK)
1074 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001075 mz_zip_print("Zip - Read cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001076 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1077
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001078 /* Verify central directory signature exists at offset */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001079 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1080 if (err == MZ_OK)
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001081 err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001082 if ((err == MZ_OK) && (zip->cd_signature != MZ_ZIP_MAGIC_CENTRALHEADER))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001083 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001084 /* If cd exists in large file and no zip-64 support, error for recover */
1085 if (eocd_pos > UINT32_MAX && eocd_pos64 == 0)
1086 err = MZ_FORMAT_ERROR;
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001087 /* If cd not found attempt to seek backward to find it */
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001088 if (err == MZ_OK)
1089 err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001090 if (err == MZ_OK)
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001091 err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001092 if ((err == MZ_OK) && (zip->cd_signature == MZ_ZIP_MAGIC_CENTRALHEADER))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001093 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001094
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001095 /* If found compensate for incorrect locations */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001096 value64i = zip->cd_offset;
1097 zip->cd_offset = eocd_pos - zip->cd_size;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001098 /* Assume disk has prepended data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001099 zip->disk_offset_shift = zip->cd_offset - value64i;
1100 }
1101 }
1102 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001103
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001104 if (err == MZ_OK)
1105 {
1106 if (eocd_pos < zip->cd_offset)
1107 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001108 /* End of central dir should always come after central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001109 err = MZ_FORMAT_ERROR;
1110 }
1111 else if (eocd_pos < zip->cd_offset + zip->cd_size)
1112 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001113 /* Truncate size of cd if incorrect size or offset provided */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001114 zip->cd_size = eocd_pos - zip->cd_offset;
1115 }
1116 }
1117
1118 return err;
1119}
1120
1121static int32_t mz_zip_write_cd(void *handle)
1122{
1123 mz_zip *zip = (mz_zip *)handle;
1124 int64_t zip64_eocd_pos_inzip = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001125 int64_t disk_number = 0;
1126 int64_t disk_size = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001127 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001128 int32_t err = MZ_OK;
1129
1130
1131 if (zip == NULL)
1132 return MZ_PARAM_ERROR;
1133
1134 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
1135 zip->disk_number_with_cd = (uint32_t)disk_number;
1136 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
1137 zip->disk_number_with_cd += 1;
1138 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Nathan Moinvazirif286b832019-11-10 17:09:48 -08001139 if ((zip->disk_number_with_cd > 0) && (zip->open_mode & MZ_OPEN_MODE_APPEND))
1140 {
1141 // Overwrite existing central directory if using split disks
1142 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1143 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001144
1145 zip->cd_offset = mz_stream_tell(zip->stream);
1146 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
1147 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
1148 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
1149
1150 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
1151
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001152 mz_zip_print("Zip - Write cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001153 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1154
Antoine Cœur61f224a2019-05-02 01:42:09 +08001155 if (zip->cd_size == 0 && zip->number_entry > 0)
Nathan Moinvaziri64faa252019-04-21 21:38:48 -07001156 {
1157 // Zip does not contain central directory, open with recovery option
1158 return MZ_FORMAT_ERROR;
1159 }
1160
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001161 /* Write the ZIP64 central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001162 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
1163 {
1164 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
1165
1166 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
1167
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001168 /* Size of this 'zip64 end of central directory' */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001169 if (err == MZ_OK)
1170 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001171 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001172 if (err == MZ_OK)
1173 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001174 /* Version needed */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001175 if (err == MZ_OK)
1176 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001177 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001178 if (err == MZ_OK)
1179 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001180 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001181 if (err == MZ_OK)
1182 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001183 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001184 if (err == MZ_OK)
1185 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001186 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001187 if (err == MZ_OK)
1188 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001189 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001190 if (err == MZ_OK)
1191 err = mz_stream_write_int64(zip->stream, zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001192 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001193 if (err == MZ_OK)
1194 err = mz_stream_write_int64(zip->stream, zip->cd_offset);
1195 if (err == MZ_OK)
1196 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
1197
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001198 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001199 if (err == MZ_OK)
1200 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001201 /* Relative offset to the end of zip64 central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001202 if (err == MZ_OK)
1203 err = mz_stream_write_int64(zip->stream, zip64_eocd_pos_inzip);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001204 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001205 if (err == MZ_OK)
1206 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
1207 }
1208
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001209 /* Write the central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001210
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001211 /* Signature */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001212 if (err == MZ_OK)
1213 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001214 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001215 if (err == MZ_OK)
1216 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001217 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001218 if (err == MZ_OK)
1219 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001220 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001221 if (err == MZ_OK)
1222 {
1223 if (zip->number_entry >= UINT16_MAX)
1224 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1225 else
1226 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1227 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001228 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001229 if (err == MZ_OK)
1230 {
1231 if (zip->number_entry >= UINT16_MAX)
1232 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1233 else
1234 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1235 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001236 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001237 if (err == MZ_OK)
1238 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001239 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001240 if (err == MZ_OK)
1241 {
1242 if (zip->cd_offset >= UINT32_MAX)
1243 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
1244 else
1245 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
1246 }
1247
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001248 /* Write global comment */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001249 if (zip->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001250 {
1251 comment_size = (int32_t)strlen(zip->comment);
1252 if (comment_size > UINT16_MAX)
1253 comment_size = UINT16_MAX;
1254 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001255 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001256 err = mz_stream_write_uint16(zip->stream, (uint16_t)comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001257 if (err == MZ_OK)
1258 {
1259 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
1260 err = MZ_READ_ERROR;
1261 }
1262 return err;
1263}
1264
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001265static int32_t mz_zip_recover_cd(void *handle)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001266{
1267 mz_zip *zip = (mz_zip *)handle;
1268 mz_zip_file local_file_info;
1269 void *local_file_info_stream = NULL;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001270 void *cd_mem_stream = NULL;
1271 uint64_t number_entry = 0;
1272 int64_t descriptor_pos = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001273 int64_t next_header_pos = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001274 int64_t disk_offset = 0;
1275 int64_t disk_number = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001276 int64_t compressed_pos = 0;
1277 int64_t compressed_end_pos = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001278 int64_t compressed_size = 0;
1279 int64_t uncompressed_size = 0;
1280 uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001281 uint8_t local_header_magic[4] = MZ_ZIP_MAGIC_LOCALHEADERU8;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001282 uint8_t central_header_magic[4] = MZ_ZIP_MAGIC_CENTRALHEADERU8;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001283 uint32_t crc32 = 0;
1284 int32_t disk_number_with_cd = 0;
1285 int32_t err = MZ_OK;
1286 uint8_t zip64 = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001287 uint8_t eof = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001288
1289
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001290 mz_zip_print("Zip - Recover - Start\n");
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001291
1292 mz_zip_get_cd_mem_stream(handle, &cd_mem_stream);
1293
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001294 /* Determine if we are on a split disk or not */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001295 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0);
1296 if (mz_stream_tell(zip->stream) < 0)
1297 {
1298 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1299 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1300 }
1301 else
1302 disk_number_with_cd = 1;
1303
1304 if (mz_stream_is_open(cd_mem_stream) != MZ_OK)
1305 err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001306
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001307 mz_stream_mem_create(&local_file_info_stream);
1308 mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri57cbcd22019-10-21 15:37:58 -07001309
1310 if (err == MZ_OK)
1311 {
1312 err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001313 INT64_MAX, &next_header_pos);
Nathan Moinvaziri57cbcd22019-10-21 15:37:58 -07001314 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001315
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001316 while (err == MZ_OK && !eof)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001317 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001318 /* Get current offset and disk number for central dir record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001319 disk_offset = mz_stream_tell(zip->stream);
1320 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1321
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001322 /* Read local headers */
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001323 memset(&local_file_info, 0, sizeof(local_file_info));
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001324 err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream);
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001325 if (err != MZ_OK)
1326 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001327
1328 local_file_info.disk_offset = disk_offset;
1329 if (disk_number < 0)
1330 disk_number = 0;
1331 local_file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001332
1333 compressed_pos = mz_stream_tell(zip->stream);
1334
1335 if ((err == MZ_OK) && (local_file_info.compressed_size > 0))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001336 {
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001337 mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001338 }
1339
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001340 /* Search for the next local header */
1341 err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
1342 INT64_MAX, &next_header_pos);
1343
1344 if (err == MZ_EXIST_ERROR)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001345 {
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001346 mz_stream_seek(zip->stream, compressed_pos, MZ_SEEK_SET);
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001347
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001348 /* Search for central dir if no local header found */
1349 err = mz_stream_find(zip->stream, (const void *)central_header_magic, sizeof(central_header_magic),
1350 INT64_MAX, &next_header_pos);
1351
1352 if (err == MZ_EXIST_ERROR)
1353 {
1354 /* Get end of stream if no central header found */
1355 mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1356 next_header_pos = mz_stream_tell(zip->stream);
1357 }
1358
1359 eof = 1;
1360 }
1361
1362 /* Search backwards for the descriptor */
1363 err = mz_stream_find_reverse(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
1364 INT8_MAX, &descriptor_pos);
1365 if (err == MZ_OK)
1366 {
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001367 if (mz_zip_extrafield_contains(local_file_info.extrafield,
1368 local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001369 zip64 = 1;
1370
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001371 err = mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32,
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001372 &compressed_size, &uncompressed_size);
1373
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001374 if (err == MZ_OK)
1375 {
1376 if (local_file_info.crc == 0)
1377 local_file_info.crc = crc32;
1378 if (local_file_info.compressed_size == 0)
1379 local_file_info.compressed_size = compressed_size;
1380 if (local_file_info.uncompressed_size == 0)
1381 local_file_info.uncompressed_size = uncompressed_size;
1382 }
1383
1384 compressed_end_pos = descriptor_pos;
1385 }
1386 else
1387 {
1388 compressed_end_pos = next_header_pos;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001389 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001390
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001391 compressed_size = compressed_end_pos - compressed_pos;
1392
1393 if (compressed_size > UINT32_MAX)
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001394 {
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001395 /* Update sizes if 4GB file is written with no ZIP64 support */
1396 if (local_file_info.uncompressed_size < UINT32_MAX)
1397 {
1398 local_file_info.compressed_size = compressed_size;
1399 local_file_info.uncompressed_size = 0;
1400 }
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001401 }
1402
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001403 mz_zip_print("Zip - Recover - Entry %s (csize %" PRId64 " usize %" PRId64 " flags 0x%" PRIx16 ")\n",
1404 local_file_info.filename, local_file_info.compressed_size, local_file_info.uncompressed_size,
1405 local_file_info.flag);
1406
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001407 /* Rewrite central dir with local headers and offsets */
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001408 err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001409 if (err == MZ_OK)
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001410 number_entry += 1;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001411
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001412 err = mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001413 }
1414
1415 mz_stream_mem_delete(&local_file_info_stream);
1416
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001417 mz_zip_print("Zip - Recover - Complete (cddisk %" PRId32 " entries %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001418 disk_number_with_cd, number_entry);
1419
1420 if (number_entry == 0)
1421 return err;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001422
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001423 /* Set new upper seek boundary for central dir mem stream */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001424 disk_offset = mz_stream_tell(cd_mem_stream);
1425 mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset);
1426
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001427 /* Set new central directory info */
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001428 mz_zip_set_cd_stream(handle, 0, cd_mem_stream);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001429 mz_zip_set_number_entry(handle, number_entry);
1430 mz_zip_set_disk_number_with_cd(handle, disk_number_with_cd);
1431
1432 return MZ_OK;
1433}
1434
1435void *mz_zip_create(void **handle)
1436{
1437 mz_zip *zip = NULL;
1438
1439 zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
1440 if (zip != NULL)
1441 memset(zip, 0, sizeof(mz_zip));
1442 if (handle != NULL)
1443 *handle = zip;
1444
1445 return zip;
1446}
1447
1448void mz_zip_delete(void **handle)
1449{
1450 mz_zip *zip = NULL;
1451 if (handle == NULL)
1452 return;
1453 zip = (mz_zip *)*handle;
1454 if (zip != NULL)
1455 {
1456 MZ_FREE(zip);
1457 }
1458 *handle = NULL;
1459}
1460
1461int32_t mz_zip_open(void *handle, void *stream, int32_t mode)
1462{
1463 mz_zip *zip = (mz_zip *)handle;
1464 int32_t err = MZ_OK;
1465
1466
1467 if (zip == NULL)
1468 return MZ_PARAM_ERROR;
1469
1470 mz_zip_print("Zip - Open\n");
1471
1472 zip->stream = stream;
1473
1474 mz_stream_mem_create(&zip->cd_mem_stream);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001475
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001476 if (mode & MZ_OPEN_MODE_WRITE)
1477 {
1478 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1479 zip->cd_stream = zip->cd_mem_stream;
1480 }
1481 else
1482 {
1483 zip->cd_stream = stream;
1484 }
1485
1486 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
1487 {
1488 if ((mode & MZ_OPEN_MODE_CREATE) == 0)
1489 {
1490 err = mz_zip_read_cd(zip);
1491 if (err != MZ_OK)
1492 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001493 mz_zip_print("Zip - Error detected reading cd (%" PRId32 ")\n", err);
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001494 if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001495 err = MZ_OK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001496 }
1497 }
1498
1499 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
1500 {
1501 if (zip->cd_size > 0)
1502 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001503 /* Store central directory in memory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001504 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1505 if (err == MZ_OK)
1506 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size);
1507 if (err == MZ_OK)
1508 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1509 }
1510 else
1511 {
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001512 if (zip->cd_signature == MZ_ZIP_MAGIC_ENDHEADER)
1513 {
1514 /* If tiny zip then overwrite end header */
1515 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1516 }
1517 else
1518 {
1519 /* If no central directory, append new zip to end of file */
1520 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1521 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001522 }
Nathan Moinvaziri2ce1fe22018-11-23 11:53:11 -08001523
1524 if (zip->disk_number_with_cd > 0)
1525 {
1526 /* Move to last disk to begin appending */
1527 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->disk_number_with_cd - 1);
1528 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001529 }
1530 else
1531 {
1532 zip->cd_start_pos = zip->cd_offset;
1533 }
1534 }
1535
1536 if (err != MZ_OK)
1537 {
1538 mz_zip_close(zip);
1539 return err;
1540 }
1541
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001542 /* Memory streams used to store variable length file info data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001543 mz_stream_mem_create(&zip->file_info_stream);
1544 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1545
1546 mz_stream_mem_create(&zip->local_file_info_stream);
1547 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1548
1549 zip->open_mode = mode;
1550
1551 return err;
1552}
1553
1554int32_t mz_zip_close(void *handle)
1555{
1556 mz_zip *zip = (mz_zip *)handle;
1557 int32_t err = MZ_OK;
1558
1559 if (zip == NULL)
1560 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001561
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001562 mz_zip_print("Zip - Close\n");
1563
1564 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001565 err = mz_zip_entry_close(handle);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001566
1567 if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE))
1568 err = mz_zip_write_cd(handle);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001569
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001570 if (zip->cd_mem_stream != NULL)
1571 {
1572 mz_stream_close(zip->cd_mem_stream);
1573 mz_stream_delete(&zip->cd_mem_stream);
1574 }
1575
1576 if (zip->file_info_stream != NULL)
1577 {
1578 mz_stream_mem_close(zip->file_info_stream);
1579 mz_stream_mem_delete(&zip->file_info_stream);
1580 }
1581 if (zip->local_file_info_stream != NULL)
1582 {
1583 mz_stream_mem_close(zip->local_file_info_stream);
1584 mz_stream_mem_delete(&zip->local_file_info_stream);
1585 }
1586
1587 if (zip->comment)
1588 {
1589 MZ_FREE(zip->comment);
1590 zip->comment = NULL;
1591 }
1592
1593 zip->stream = NULL;
1594 zip->cd_stream = NULL;
1595
1596 return err;
1597}
1598
1599int32_t mz_zip_get_comment(void *handle, const char **comment)
1600{
1601 mz_zip *zip = (mz_zip *)handle;
1602 if (zip == NULL || comment == NULL)
1603 return MZ_PARAM_ERROR;
1604 if (zip->comment == NULL)
1605 return MZ_EXIST_ERROR;
1606 *comment = zip->comment;
1607 return MZ_OK;
1608}
1609
1610int32_t mz_zip_set_comment(void *handle, const char *comment)
1611{
1612 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001613 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001614 if (zip == NULL || comment == NULL)
1615 return MZ_PARAM_ERROR;
1616 if (zip->comment != NULL)
1617 MZ_FREE(zip->comment);
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001618 comment_size = (int32_t)strlen(comment);
1619 if (comment_size > UINT16_MAX)
1620 return MZ_PARAM_ERROR;
Nathan Moinvaziria6ecab52019-01-22 13:14:01 -08001621 zip->comment = (char *)MZ_ALLOC(comment_size+1);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001622 if (zip->comment == NULL)
1623 return MZ_MEM_ERROR;
Nathan Moinvaziria6ecab52019-01-22 13:14:01 -08001624 memset(zip->comment, 0, comment_size+1);
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001625 strncpy(zip->comment, comment, comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001626 return MZ_OK;
1627}
1628
1629int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
1630{
1631 mz_zip *zip = (mz_zip *)handle;
1632 if (zip == NULL || version_madeby == NULL)
1633 return MZ_PARAM_ERROR;
1634 *version_madeby = zip->version_madeby;
1635 return MZ_OK;
1636}
1637
1638int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
1639{
1640 mz_zip *zip = (mz_zip *)handle;
1641 if (zip == NULL)
1642 return MZ_PARAM_ERROR;
1643 zip->version_madeby = version_madeby;
1644 return MZ_OK;
1645}
1646
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001647int32_t mz_zip_set_recover(void *handle, uint8_t recover)
1648{
1649 mz_zip *zip = (mz_zip *)handle;
1650 if (zip == NULL)
1651 return MZ_PARAM_ERROR;
1652 zip->recover = recover;
1653 return MZ_OK;
1654}
1655
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001656int32_t mz_zip_get_stream(void *handle, void **stream)
1657{
1658 mz_zip *zip = (mz_zip *)handle;
1659 if (zip == NULL || stream == NULL)
1660 return MZ_PARAM_ERROR;
1661 *stream = zip->stream;
1662 if (*stream == NULL)
1663 return MZ_EXIST_ERROR;
1664 return MZ_OK;
1665}
1666
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001667int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001668{
1669 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001670 if (zip == NULL || cd_stream == NULL)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001671 return MZ_PARAM_ERROR;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001672 zip->cd_offset = 0;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001673 zip->cd_stream = cd_stream;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001674 zip->cd_start_pos = cd_start_pos;
1675 return MZ_OK;
1676}
1677
1678int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream)
1679{
1680 mz_zip *zip = (mz_zip *)handle;
1681 if (zip == NULL || cd_mem_stream == NULL)
1682 return MZ_PARAM_ERROR;
1683 *cd_mem_stream = zip->cd_mem_stream;
1684 if (*cd_mem_stream == NULL)
1685 return MZ_EXIST_ERROR;
1686 return MZ_OK;
1687}
1688
1689static int32_t mz_zip_entry_close_int(void *handle)
1690{
1691 mz_zip *zip = (mz_zip *)handle;
1692
1693 if (zip->crypt_stream != NULL)
1694 mz_stream_delete(&zip->crypt_stream);
1695 zip->crypt_stream = NULL;
1696 if (zip->compress_stream != NULL)
1697 mz_stream_delete(&zip->compress_stream);
1698 zip->compress_stream = NULL;
1699
1700 zip->entry_opened = 0;
1701
1702 return MZ_OK;
1703}
1704
Nathan Moinvaziri26308b22018-08-08 09:40:15 -07001705static 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 -07001706{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001707 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001708 int64_t max_total_in = 0;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001709 int64_t header_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001710 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001711 int32_t err = MZ_OK;
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001712 uint8_t use_crypt = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001713
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001714 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001715 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001716
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001717 switch (zip->file_info.compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001718 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001719 case MZ_COMPRESS_METHOD_STORE:
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001720 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001721#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001722 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001723#endif
Nathan Moinvaziri566dfe02018-10-26 00:36:30 -07001724#ifdef HAVE_LZMA
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001725 case MZ_COMPRESS_METHOD_LZMA:
1726#endif
1727 err = MZ_OK;
1728 break;
1729 default:
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001730 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001731 }
1732
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001733#ifndef HAVE_WZAES
1734 if (zip->file_info.aes_version)
1735 return MZ_SUPPORT_ERROR;
1736#endif
1737
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001738 zip->entry_raw = raw;
1739
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001740 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password != NULL))
1741 {
1742 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
1743 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001744 /* Encrypt only when we are not trying to write raw and password is supplied. */
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001745 if (!zip->entry_raw)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001746 use_crypt = 1;
1747 }
1748 else if (zip->open_mode & MZ_OPEN_MODE_READ)
1749 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001750 /* Decrypt only when password is supplied. Don't error when password */
1751 /* is not supplied as we may want to read the raw encrypted data. */
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001752 use_crypt = 1;
1753 }
1754 }
1755
1756 if ((err == MZ_OK) && (use_crypt))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001757 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001758#ifdef HAVE_WZAES
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001759 if (zip->file_info.aes_version)
1760 {
Nathan Moinvaziri21a31022018-10-24 09:50:16 -07001761 mz_stream_wzaes_create(&zip->crypt_stream);
1762 mz_stream_wzaes_set_password(zip->crypt_stream, password);
1763 mz_stream_wzaes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001764 }
1765 else
1766#endif
1767 {
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001768#ifdef HAVE_PKCRYPT
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001769 uint8_t verify1 = 0;
1770 uint8_t verify2 = 0;
1771
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001772 /* Info-ZIP modification to ZipCrypto format: */
1773 /* 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 -07001774
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001775 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1776 {
1777 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001778
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001779 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1780
1781 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1782 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1783 }
1784 else
1785 {
1786 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1787 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1788 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001789
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001790 mz_stream_pkcrypt_create(&zip->crypt_stream);
1791 mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1792 mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001793#endif
1794 }
1795 }
1796
1797 if (err == MZ_OK)
1798 {
1799 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001800 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001801
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001802 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001803
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001804 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001805 }
1806
1807 if (err == MZ_OK)
1808 {
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001809 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001810 mz_stream_raw_create(&zip->compress_stream);
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001811#if defined(HAVE_ZLIB) || defined(HAVE_LIBCOMP)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001812 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001813 mz_stream_zlib_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001814#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001815#ifdef HAVE_BZIP2
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001816 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001817 mz_stream_bzip_create(&zip->compress_stream);
1818#endif
1819#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001820 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001821 mz_stream_lzma_create(&zip->compress_stream);
1822#endif
1823 else
1824 err = MZ_PARAM_ERROR;
1825 }
1826
1827 if (err == MZ_OK)
1828 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001829 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001830 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001831 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001832 }
1833 else
1834 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001835#ifndef HAVE_LIBCOMP
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001836 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 -08001837#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001838 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001839 max_total_in = zip->file_info.compressed_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001840 mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1841
1842 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1843 max_total_in -= header_size;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001844 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1845 max_total_in -= footer_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001846
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001847 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001848 }
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001849 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 -03001850 {
1851 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1852 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1853 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001854 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001855
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001856 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1857
1858 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1859 }
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001860
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001861 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001862 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001863 zip->entry_opened = 1;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001864 zip->entry_crc32 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001865 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001866 else
1867 {
1868 mz_zip_entry_close_int(handle);
1869 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001870
1871 return err;
1872}
1873
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001874int32_t mz_zip_entry_is_open(void *handle)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001875{
1876 mz_zip *zip = (mz_zip *)handle;
1877 if (zip == NULL)
1878 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001879 if (zip->entry_opened == 0)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001880 return MZ_EXIST_ERROR;
1881 return MZ_OK;
1882}
1883
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001884static int32_t mz_zip_seek_to_local_header(void *handle)
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001885{
1886 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001887
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001888
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001889 if (zip->file_info.disk_number == zip->disk_number_with_cd)
1890 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1891 else
1892 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001893
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001894 mz_zip_print("Zip - Entry - Seek local (disk %" PRId32 " offset %" PRId64 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001895 zip->file_info.disk_number, zip->file_info.disk_offset);
Nathan Moinvaziri09725902019-02-20 09:15:48 -08001896
1897 /* Guard against seek overflows */
1898 if ((zip->disk_offset_shift > 0) &&
1899 (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
1900 return MZ_FORMAT_ERROR;
1901
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001902 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 -07001903}
1904
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001905int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001906{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001907 mz_zip *zip = (mz_zip *)handle;
1908 int32_t err = MZ_OK;
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001909 int32_t err_shift = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001910
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001911#if defined(MZ_ZIP_NO_ENCRYPTION)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001912 if (password != NULL)
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001913 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001914#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001915 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001916 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001917 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001918 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001919 if (zip->entry_scanned == 0)
1920 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001921
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001922 mz_zip_print("Zip - Entry - Read open (raw %" PRId32 ")\n", raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001923
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001924 err = mz_zip_seek_to_local_header(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001925 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001926 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 -07001927
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001928 if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0)
1929 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001930 /* Perhaps we didn't compensated correctly for incorrect cd offset */
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001931 err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
1932 if (err_shift == MZ_OK)
1933 err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
1934 if (err_shift == MZ_OK)
1935 {
1936 zip->disk_offset_shift = 0;
1937 err = err_shift;
1938 }
1939 }
1940
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001941#ifdef MZ_ZIP_NO_DECOMPRESSION
Nathan Moinvaziri2eb29072018-11-23 10:32:21 -08001942 if (!raw && zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001943 err = MZ_SUPPORT_ERROR;
Viktor Szakatse7215072018-09-04 15:06:53 +00001944#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001945 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001946 err = mz_zip_entry_open_int(handle, raw, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001947
1948 return err;
1949}
1950
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001951int32_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 -07001952{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001953 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001954 int64_t filename_pos = -1;
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001955 int64_t extrafield_pos = 0;
1956 int64_t comment_pos = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07001957 int64_t linkname_pos = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001958 int64_t disk_number = 0;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001959 uint8_t is_dir = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001960 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001961
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001962#if defined(MZ_ZIP_NO_ENCRYPTION)
tz-lomb1b25802017-11-10 15:03:02 +03001963 if (password != NULL)
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001964 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001965#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001966 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001967 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001968
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001969 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001970 {
1971 err = mz_zip_entry_close(handle);
1972 if (err != MZ_OK)
1973 return err;
1974 }
1975
1976 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001977
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001978 mz_zip_print("Zip - Entry - Write open - %s (level %" PRId16 " raw %" PRId8 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001979 zip->file_info.filename, compress_level, raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001980
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001981 mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001982 mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
1983
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001984 /* Copy filename, extrafield, and comment internally */
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001985 filename_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001986 if (file_info->filename != NULL)
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001987 mz_stream_write(zip->file_info_stream, file_info->filename, (int32_t)strlen(file_info->filename));
1988 mz_stream_write_uint8(zip->file_info_stream, 0);
1989
1990 extrafield_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001991 if (file_info->extrafield != NULL)
Nathan Moinvazirica014e62018-08-15 07:31:12 -07001992 mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001993 mz_stream_write_uint8(zip->file_info_stream, 0);
1994
1995 comment_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001996 if (file_info->comment != NULL)
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001997 mz_stream_write(zip->file_info_stream, file_info->comment, file_info->comment_size);
1998 mz_stream_write_uint8(zip->file_info_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001999
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002000 linkname_pos = mz_stream_tell(zip->file_info_stream);
2001 if (file_info->linkname != NULL)
2002 mz_stream_write(zip->file_info_stream, file_info->linkname, (int32_t)strlen(file_info->linkname));
2003 mz_stream_write_uint8(zip->file_info_stream, 0);
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002004
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08002005 mz_stream_mem_get_buffer_at(zip->file_info_stream, filename_pos, (const void **)&zip->file_info.filename);
2006 mz_stream_mem_get_buffer_at(zip->file_info_stream, extrafield_pos, (const void **)&zip->file_info.extrafield);
2007 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 -07002008 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 -07002009
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002010 if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002011 {
2012 if ((compress_level == 8) || (compress_level == 9))
2013 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
2014 if (compress_level == 2)
2015 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
2016 if (compress_level == 1)
2017 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
2018 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002019#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002020 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002021 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002022#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002023
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002024 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2025 is_dir = 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002026
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002027 if (!is_dir)
2028 {
2029 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
2030 if (password != NULL)
2031 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
2032 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002033
juaniib8887e92018-02-14 00:51:05 -03002034 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
2035 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002036
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002037 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07002038 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002039 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002040
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08002041#ifdef HAVE_WZAES
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002042 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
2043 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
2044#endif
2045
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002046 if ((compress_level == 0) || (is_dir))
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002047 zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
Viktor Szakats2884e672018-04-30 08:12:13 +00002048
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07002049#ifdef MZ_ZIP_NO_COMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002050 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002051 err = MZ_SUPPORT_ERROR;
2052#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002053 if (err == MZ_OK)
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002054 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002055 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002056 err = mz_zip_entry_open_int(handle, raw, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002057
2058 return err;
2059}
2060
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002061int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002062{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002063 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002064 int32_t read = 0;
2065
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002066 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002067 return MZ_PARAM_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002068 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) /* zlib limitation */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002069 return MZ_PARAM_ERROR;
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07002070 if (len == 0)
2071 return MZ_PARAM_ERROR;
2072
Nathan Moinvazirieb80cd92018-07-11 16:45:09 -07002073 if (zip->file_info.compressed_size == 0)
2074 return 0;
2075
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002076 /* Read entire entry even if uncompressed_size = 0, otherwise */
2077 /* aes encryption validation will fail if compressed_size > 0 */
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07002078 read = mz_stream_read(zip->compress_stream, buf, len);
2079 if (read > 0)
2080 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002081
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002082 mz_zip_print("Zip - Entry - Read - %" PRId32 " (max %" PRId32 ")\n", read, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002083
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002084 return read;
2085}
2086
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002087int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002088{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002089 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002090 int32_t written = 0;
2091
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002092 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002093 return MZ_PARAM_ERROR;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07002094 written = mz_stream_write(zip->compress_stream, buf, len);
2095 if (written > 0)
2096 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002097
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002098 mz_zip_print("Zip - Entry - Write - %" PRId32 " (max %" PRId32 ")\n", written, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002099
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002100 return written;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002101}
2102
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002103int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002104 int64_t *uncompressed_size)
2105{
2106 mz_zip *zip = (mz_zip *)handle;
2107 int64_t total_in = 0;
2108 int32_t err = MZ_OK;
2109 uint8_t zip64 = 0;
2110
2111 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2112 return MZ_PARAM_ERROR;
2113
2114 mz_stream_close(zip->compress_stream);
2115
2116 mz_zip_print("Zip - Entry - Read Close\n");
2117
2118 if (crc32 != NULL)
2119 *crc32 = zip->file_info.crc;
2120 if (compressed_size != NULL)
2121 *compressed_size = zip->file_info.compressed_size;
2122 if (uncompressed_size != NULL)
2123 *uncompressed_size = zip->file_info.uncompressed_size;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002124
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002125 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
2126
2127 if ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) &&
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002128 ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0) &&
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002129 (crc32 != NULL || compressed_size != NULL || uncompressed_size != NULL))
2130 {
2131 /* Check to see if data descriptor is zip64 bit format or not */
2132 if (mz_zip_extrafield_contains(zip->local_file_info.extrafield,
2133 zip->local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
2134 zip64 = 1;
2135
2136 err = mz_zip_seek_to_local_header(handle);
2137
2138 /* Seek to end of compressed stream since we might have over-read during compression */
2139 if (err == MZ_OK)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002140 err = mz_stream_seek(zip->stream, MZ_ZIP_SIZE_LD_ITEM +
2141 (int64_t)zip->local_file_info.filename_size +
2142 (int64_t)zip->local_file_info.extrafield_size +
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002143 total_in, MZ_SEEK_CUR);
2144
2145 /* Read data descriptor */
2146 if (err == MZ_OK)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002147 err = mz_zip_entry_read_descriptor(zip->stream, zip64,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002148 crc32, compressed_size, uncompressed_size);
2149 }
2150
2151 /* If entire entry was not read verification will fail */
2152 if ((err == MZ_OK) && (total_in > 0) && (!zip->entry_raw))
2153 {
2154#ifdef HAVE_WZAES
2155 /* AES zip version AE-1 will expect a valid crc as well */
2156 if (zip->file_info.aes_version <= 0x0001)
2157#endif
2158 {
2159 if (zip->entry_crc32 != zip->file_info.crc)
2160 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002161 mz_zip_print("Zip - Entry - Crc failed (actual 0x%08" PRIx32 " expected 0x%08" PRIx32 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002162 zip->entry_crc32, zip->file_info.crc);
2163
2164 err = MZ_CRC_ERROR;
2165 }
2166 }
2167 }
2168
2169 mz_zip_entry_close_int(handle);
2170
2171 return err;
2172}
2173
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002174int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002175 int64_t uncompressed_size)
2176{
2177 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002178 int32_t err = MZ_OK;
2179 uint8_t zip64 = 0;
2180
2181 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2182 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002183
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002184 mz_stream_close(zip->compress_stream);
2185
2186 if (!zip->entry_raw)
2187 crc32 = zip->entry_crc32;
2188
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002189 mz_zip_print("Zip - Entry - Write Close (crc 0x%08" PRIx32 " cs %" PRId64 " ucs %" PRId64 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002190 crc32, compressed_size, uncompressed_size);
2191
2192 /* If sizes are not set, then read them from the compression stream */
2193 if (compressed_size < 0)
2194 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2195 if (uncompressed_size < 0)
Nathan Moinvazirid9318432018-12-01 07:56:28 -08002196 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002197
2198 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
2199 {
2200 mz_stream_set_base(zip->crypt_stream, zip->stream);
2201 err = mz_stream_close(zip->crypt_stream);
2202
2203 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2204 }
2205
2206 if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
2207 {
2208 /* Determine if we need to write data descriptor in zip64 format,
2209 if local extrafield was saved with zip64 extrafield */
2210 if (zip->file_info.zip64 == MZ_ZIP64_AUTO)
2211 {
2212 if (zip->file_info.uncompressed_size >= UINT32_MAX)
2213 zip64 = 1;
2214 if (zip->file_info.compressed_size >= UINT32_MAX)
2215 zip64 = 1;
2216 if (zip->file_info.disk_offset >= UINT32_MAX)
2217 zip64 = 1;
2218 else if (zip->file_info.uncompressed_size == 0)
2219 zip64 = 1;
2220 }
2221 else if (zip->file_info.zip64 == MZ_ZIP64_FORCE)
2222 {
2223 zip64 = 1;
2224 }
2225
2226 if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002227 err = mz_zip_entry_write_descriptor(zip->stream,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002228 zip64, 0, compressed_size, 0);
2229 else
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002230 err = mz_zip_entry_write_descriptor(zip->stream,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002231 zip64, crc32, compressed_size, uncompressed_size);
2232 }
2233
2234 /* Write file info to central directory */
2235
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002236 mz_zip_print("Zip - Entry - Write cd (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002237 uncompressed_size, compressed_size, crc32);
2238
2239 zip->file_info.crc = crc32;
2240 zip->file_info.compressed_size = compressed_size;
2241 zip->file_info.uncompressed_size = uncompressed_size;
2242
2243 if (err == MZ_OK)
2244 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
2245
2246 zip->number_entry += 1;
2247
2248 mz_zip_entry_close_int(handle);
2249
2250 return err;
2251}
2252
2253int32_t mz_zip_entry_is_dir(void *handle)
2254{
2255 mz_zip *zip = (mz_zip *)handle;
2256 int32_t filename_length = 0;
2257
2258 if (zip == NULL)
2259 return MZ_PARAM_ERROR;
2260 if (zip->entry_scanned == 0)
2261 return MZ_PARAM_ERROR;
2262 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2263 return MZ_OK;
2264
2265 filename_length = (int32_t)strlen(zip->file_info.filename);
2266 if (filename_length > 0)
2267 {
2268 if ((zip->file_info.filename[filename_length - 1] == '/') ||
2269 (zip->file_info.filename[filename_length - 1] == '\\'))
2270 return MZ_OK;
2271 }
2272 return MZ_EXIST_ERROR;
2273}
2274
Nathan Moinvaziri3249eac2019-05-02 21:07:39 -07002275int32_t mz_zip_entry_is_symlink(void *handle)
2276{
2277 mz_zip *zip = (mz_zip *)handle;
2278
2279 if (zip == NULL)
2280 return MZ_PARAM_ERROR;
2281 if (zip->entry_scanned == 0)
2282 return MZ_PARAM_ERROR;
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002283 if (mz_zip_attrib_is_symlink(zip->file_info.external_fa, zip->file_info.version_madeby) != MZ_OK)
2284 return MZ_EXIST_ERROR;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002285 if (zip->file_info.linkname == NULL || *zip->file_info.linkname == 0)
2286 return MZ_EXIST_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002287
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002288 return MZ_OK;
Nathan Moinvaziri3249eac2019-05-02 21:07:39 -07002289}
2290
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002291int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002292{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002293 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002294
2295 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002296 return MZ_PARAM_ERROR;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002297
2298 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
2299 {
2300 if (!zip->entry_scanned)
2301 return MZ_PARAM_ERROR;
2302 }
2303
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002304 *file_info = &zip->file_info;
2305 return MZ_OK;
2306}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002307
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002308int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002309{
2310 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002311 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002312 return MZ_PARAM_ERROR;
2313 *local_file_info = &zip->local_file_info;
2314 return MZ_OK;
2315}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002316
Nathan Moinvaziri915c5132018-10-26 20:00:52 -07002317int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size)
2318{
2319 mz_zip *zip = (mz_zip *)handle;
2320
2321 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2322 return MZ_PARAM_ERROR;
2323
2324 zip->file_info.extrafield = extrafield;
2325 zip->file_info.extrafield_size = extrafield_size;
2326 return MZ_OK;
2327}
2328
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002329int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07002330{
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002331 return mz_zip_entry_close_raw(handle, UINT64_MAX, 0);
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07002332}
2333
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002334int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002335{
2336 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002337 int32_t err = MZ_OK;
2338
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002339 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002340 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002341
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002342 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002343 err = mz_zip_entry_write_close(handle, crc32, UINT64_MAX, uncompressed_size);
2344 else
2345 err = mz_zip_entry_read_close(handle, NULL, NULL, NULL);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002346
2347 return err;
2348}
2349
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002350static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002351{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002352 mz_zip *zip = (mz_zip *)handle;
2353 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002354
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002355 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002356 return MZ_PARAM_ERROR;
2357
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002358 zip->entry_scanned = 0;
2359
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002360 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00002361
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002362 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002363 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002364 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002365 if (err == MZ_OK)
2366 zip->entry_scanned = 1;
2367 return err;
2368}
2369
Nathan Moinvaziric565fa82018-10-19 08:48:33 -07002370int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry)
2371{
2372 mz_zip *zip = (mz_zip *)handle;
2373 if (zip == NULL)
2374 return MZ_PARAM_ERROR;
2375 zip->number_entry = number_entry;
2376 return MZ_OK;
2377}
2378
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002379int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002380{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002381 mz_zip *zip = (mz_zip *)handle;
2382 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002383 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002384 *number_entry = zip->number_entry;
2385 return MZ_OK;
2386}
2387
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002388int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd)
2389{
2390 mz_zip *zip = (mz_zip *)handle;
2391 if (zip == NULL)
2392 return MZ_PARAM_ERROR;
2393 zip->disk_number_with_cd = disk_number_with_cd;
2394 return MZ_OK;
2395}
2396
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002397int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03002398{
2399 mz_zip *zip = (mz_zip *)handle;
2400 if (zip == NULL || disk_number_with_cd == NULL)
2401 return MZ_PARAM_ERROR;
2402 *disk_number_with_cd = zip->disk_number_with_cd;
2403 return MZ_OK;
2404}
2405
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002406int64_t mz_zip_get_entry(void *handle)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002407{
2408 mz_zip *zip = (mz_zip *)handle;
2409
2410 if (zip == NULL)
2411 return MZ_PARAM_ERROR;
2412
2413 return zip->cd_current_pos;
2414}
2415
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002416int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002417{
2418 mz_zip *zip = (mz_zip *)handle;
2419
2420 if (zip == NULL)
2421 return MZ_PARAM_ERROR;
2422
2423 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
2424 return MZ_PARAM_ERROR;
2425
2426 zip->cd_current_pos = cd_pos;
2427
2428 return mz_zip_goto_next_entry_int(handle);
2429}
2430
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002431int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002432{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002433 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002434
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002435 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002436 return MZ_PARAM_ERROR;
2437
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002438 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002439
2440 return mz_zip_goto_next_entry_int(handle);
2441}
2442
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002443int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002444{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002445 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002446
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002447 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002448 return MZ_PARAM_ERROR;
2449
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002450 zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002451 zip->file_info.extrafield_size + zip->file_info.comment_size;
2452
2453 return mz_zip_goto_next_entry_int(handle);
2454}
2455
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002456int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002457{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002458 mz_zip *zip = (mz_zip *)handle;
2459 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002460 int32_t result = 0;
2461
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002462 if (zip == NULL || filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002463 return MZ_PARAM_ERROR;
2464
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002465 /* If we are already on the current entry, no need to search */
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002466 if ((zip->entry_scanned) && (zip->file_info.filename != NULL))
2467 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002468 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002469 if (result == 0)
2470 return MZ_OK;
2471 }
2472
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002473 /* Search all entries starting at the first */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002474 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002475 while (err == MZ_OK)
2476 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002477 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2478 if (result == 0)
2479 return MZ_OK;
2480
2481 err = mz_zip_goto_next_entry(handle);
2482 }
2483
2484 return err;
2485}
2486
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002487int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002488{
2489 mz_zip *zip = (mz_zip *)handle;
2490 int32_t err = MZ_OK;
2491 int32_t result = 0;
2492
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002493 /* Search first entry looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002494 err = mz_zip_goto_first_entry(handle);
2495 if (err != MZ_OK)
2496 return err;
2497
2498 result = cb(handle, userdata, &zip->file_info);
2499 if (result == 0)
2500 return MZ_OK;
2501
2502 return mz_zip_locate_next_entry(handle, userdata, cb);
2503}
2504
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002505int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002506{
2507 mz_zip *zip = (mz_zip *)handle;
2508 int32_t err = MZ_OK;
2509 int32_t result = 0;
2510
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002511 /* Search next entries looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002512 err = mz_zip_goto_next_entry(handle);
2513 while (err == MZ_OK)
2514 {
2515 result = cb(handle, userdata, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002516 if (result == 0)
2517 return MZ_OK;
2518
2519 err = mz_zip_goto_next_entry(handle);
2520 }
2521
2522 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002523}
2524
2525/***************************************************************************/
2526
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002527int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002528{
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002529 uint32_t posix_attrib = 0;
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002530 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002531 int32_t err = MZ_OK;
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002532
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002533 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2534 if (err == MZ_OK)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002535 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002536 if ((posix_attrib & 0170000) == 0040000) /* S_ISDIR */
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002537 return MZ_OK;
2538 }
2539
2540 return MZ_EXIST_ERROR;
2541}
2542
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002543int32_t mz_zip_attrib_is_symlink(uint32_t attrib, int32_t version_madeby)
2544{
2545 uint32_t posix_attrib = 0;
2546 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
2547 int32_t err = MZ_OK;
2548
2549 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2550 if (err == MZ_OK)
2551 {
2552 if ((posix_attrib & 0170000) == 0120000) /* S_ISLNK */
2553 return MZ_OK;
2554 }
2555
2556 return MZ_EXIST_ERROR;
2557}
2558
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002559int32_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 -07002560{
2561 if (target_attrib == NULL)
2562 return MZ_PARAM_ERROR;
2563
2564 *target_attrib = 0;
2565
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002566 if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002567 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002568 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002569 {
2570 *target_attrib = src_attrib;
2571 return MZ_OK;
2572 }
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002573 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (target_sys == MZ_HOST_SYSTEM_RISCOS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002574 return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
2575 }
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002576 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 -07002577 {
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002578 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 -07002579 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002580 /* If high bytes are set, it contains unix specific attributes */
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002581 if ((src_attrib >> 16) != 0)
2582 src_attrib >>= 16;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002583
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002584 *target_attrib = src_attrib;
2585 return MZ_OK;
2586 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002587 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002588 return mz_zip_attrib_posix_to_win32(src_attrib, target_attrib);
2589 }
2590
2591 return MZ_SUPPORT_ERROR;
2592}
2593
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002594int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002595{
2596 if (win32_attrib == NULL)
2597 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002598
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002599 *win32_attrib = 0;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002600
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002601 /* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002602 if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002603 *win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002604 /* S_IFLNK */
2605 if ((posix_attrib & 0170000) == 0120000)
2606 *win32_attrib |= 0x400; /* FILE_ATTRIBUTE_REPARSE_POINT */
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002607 /* S_IFDIR */
Nathan Moinvaziri7d94b1f2019-05-08 23:41:47 -07002608 else if ((posix_attrib & 0170000) == 0040000)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002609 *win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
2610 /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002611 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002612 *win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002613
2614 return MZ_OK;
2615}
2616
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002617int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002618{
2619 if (posix_attrib == NULL)
2620 return MZ_PARAM_ERROR;
2621
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002622 *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
2623 /* FILE_ATTRIBUTE_READONLY */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002624 if ((win32_attrib & 0x01) == 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002625 *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002626 /* FILE_ATTRIBUTE_REPARSE_POINT */
2627 if ((win32_attrib & 0x400) == 0x400)
2628 *posix_attrib |= 0120000; /* S_IFLNK */
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002629 /* FILE_ATTRIBUTE_DIRECTORY */
Nathan Moinvaziri7d94b1f2019-05-08 23:41:47 -07002630 else if ((win32_attrib & 0x10) == 0x10)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002631 *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002632 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002633 *posix_attrib |= 0100000; /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002634
2635 return MZ_OK;
2636}
2637
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002638/***************************************************************************/
2639
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002640int32_t mz_zip_extrafield_find(void *stream, uint16_t type, uint16_t *length)
2641{
2642 int32_t err = MZ_OK;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002643 uint16_t field_type = 0;
2644 uint16_t field_length = 0;
2645
2646 do
2647 {
2648 err = mz_stream_read_uint16(stream, &field_type);
2649 if (err == MZ_OK)
2650 err = mz_stream_read_uint16(stream, &field_length);
2651 if (err != MZ_OK)
2652 break;
2653
2654 if (type == field_type)
2655 {
2656 if (length != NULL)
2657 *length = field_length;
2658 return MZ_OK;
2659 }
2660
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002661 err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR);
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002662 }
2663 while (err == MZ_OK);
2664
2665 return MZ_EXIST_ERROR;
2666}
2667
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002668int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size,
2669 uint16_t type, uint16_t *length)
2670{
2671 void *file_extra_stream = NULL;
2672 int32_t err = MZ_OK;
2673
2674 if (extrafield == NULL || extrafield_size == 0)
2675 return MZ_PARAM_ERROR;
2676
2677 mz_stream_mem_create(&file_extra_stream);
2678 mz_stream_mem_set_buffer(file_extra_stream, (void *)extrafield, extrafield_size);
2679
2680 err = mz_zip_extrafield_find(file_extra_stream, type, length);
2681
2682 mz_stream_mem_delete(&file_extra_stream);
2683
2684 return err;
2685}
2686
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002687int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length)
2688{
2689 int32_t err = MZ_OK;
2690 if (type == NULL || length == NULL)
2691 return MZ_PARAM_ERROR;
2692 err = mz_stream_read_uint16(stream, type);
2693 if (err == MZ_OK)
2694 err = mz_stream_read_uint16(stream, length);
2695 return err;
2696}
2697
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002698int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002699{
2700 int32_t err = MZ_OK;
2701 err = mz_stream_write_uint16(stream, type);
2702 if (err == MZ_OK)
2703 err = mz_stream_write_uint16(stream, length);
2704 return err;
2705}
2706
2707/***************************************************************************/
2708
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002709static int32_t mz_zip_invalid_date(const struct tm *ptm)
2710{
2711#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002712 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002713 !datevalue_in_range(0, 11, ptm->tm_mon) ||
2714 !datevalue_in_range(1, 31, ptm->tm_mday) ||
2715 !datevalue_in_range(0, 23, ptm->tm_hour) ||
2716 !datevalue_in_range(0, 59, ptm->tm_min) ||
2717 !datevalue_in_range(0, 59, ptm->tm_sec));
2718#undef datevalue_in_range
2719}
2720
2721static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
2722{
2723 uint64_t date = (uint64_t)(dos_date >> 16);
2724
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002725 ptm->tm_mday = (uint16_t)(date & 0x1f);
2726 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
2727 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
2728 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
2729 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
2730 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002731 ptm->tm_isdst = -1;
2732}
2733
2734int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
2735{
2736 if (ptm == NULL)
2737 return MZ_PARAM_ERROR;
2738
2739 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
2740
2741 if (mz_zip_invalid_date(ptm))
2742 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002743 /* Invalid date stored, so don't return it */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002744 memset(ptm, 0, sizeof(struct tm));
2745 return MZ_FORMAT_ERROR;
2746 }
2747 return MZ_OK;
2748}
2749
2750time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
2751{
2752 struct tm ptm;
2753 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
2754 return mktime(&ptm);
2755}
2756
2757int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
2758{
Nathan Moinvaziricd3e9e12019-04-28 10:22:49 -07002759 struct tm ltm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002760 if (ptm == NULL)
2761 return MZ_PARAM_ERROR;
Nathan Moinvaziri2e0a20a2019-04-28 13:03:11 -07002762 if (localtime_r(&unix_time, &ltm) == NULL) /* Returns a 1900-based year */
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002763 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002764 /* Invalid date stored, so don't return it */
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002765 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002766 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002767 }
Nathan Moinvaziricd3e9e12019-04-28 10:22:49 -07002768 memcpy(ptm, &ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002769 return MZ_OK;
2770}
2771
2772uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
2773{
2774 struct tm ptm;
2775 mz_zip_time_t_to_tm(unix_time, &ptm);
2776 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
2777}
2778
2779uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
2780{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07002781 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002782
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002783 /* Years supported: */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002784
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002785 /* [00, 79] (assumed to be between 2000 and 2079) */
2786 /* [80, 207] (assumed to be between 1980 and 2107, typical output of old */
2787 /* software that does 'year-1900' to get a double digit year) */
2788 /* [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.) */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002789
2790 memcpy(&fixed_tm, ptm, sizeof(struct tm));
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002791 if (fixed_tm.tm_year >= 1980) /* range [1980, 2107] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002792 fixed_tm.tm_year -= 1980;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002793 else if (fixed_tm.tm_year >= 80) /* range [80, 207] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002794 fixed_tm.tm_year -= 80;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002795 else /* range [00, 79] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002796 fixed_tm.tm_year += 20;
2797
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002798 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002799 return 0;
2800
Anand K Mistry57b65f82018-11-09 10:52:19 +11002801 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 -07002802 (((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 -07002803}
2804
2805int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
2806{
2807 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
2808 return MZ_OK;
2809}
2810
2811int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
2812{
2813 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
2814 return MZ_OK;
2815}
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002816
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002817/***************************************************************************/
2818
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002819int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case)
2820{
2821 do
2822 {
2823 if ((*path1 == '\\' && *path2 == '/') ||
2824 (*path2 == '\\' && *path1 == '/'))
2825 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002826 /* Ignore comparison of path slashes */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002827 }
2828 else if (ignore_case)
2829 {
2830 if (tolower(*path1) != tolower(*path2))
2831 break;
2832 }
2833 else if (*path1 != *path2)
2834 {
2835 break;
2836 }
2837
2838 path1 += 1;
2839 path2 += 1;
2840 }
2841 while (*path1 != 0 && *path2 != 0);
2842
2843 if (ignore_case)
2844 return (int32_t)(tolower(*path1) - tolower(*path2));
2845
2846 return (int32_t)(*path1 - *path2);
2847}
2848
2849/***************************************************************************/