blob: b39d995ef6d10c352853eb55c57fa0d99342cd36 [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvaziri9805ab42019-02-14 17:16:01 -08002 Version 2.8.4, February 14, 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
Nathan Moinvazirie8496272018-11-21 15:05:58 -080048#if defined(_MSC_VER) && (_MSC_VER < 1900)
Nathan Moinvaziric565fa82018-10-19 08:48:33 -070049# define snprintf _snprintf
50#endif
51
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080052/***************************************************************************/
53
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070054#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
55#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
56#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080057#define MZ_ZIP_MAGIC_ENDHEADERU8 { 0x50, 0x4b, 0x05, 0x06 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070058#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
59#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
60#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080061#define MZ_ZIP_MAGIC_DATADESCRIPTORU8 { 0x50, 0x4b, 0x07, 0x08 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070062
Nathan Moinvaziriba1db162018-11-23 10:07:35 -080063#define MZ_ZIP_SIZE_LD_ITEM (30)
Nathan Moinvaziri638f31f2018-08-27 08:17:16 -070064#define MZ_ZIP_SIZE_CD_ITEM (46)
65#define MZ_ZIP_SIZE_CD_LOCATOR64 (20)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080066
Nathan Moinvaziric78c7822018-12-01 20:42:36 -080067#ifndef MZ_ZIP_EOCD_MAX_BACK
68#define MZ_ZIP_EOCD_MAX_BACK (1 << 20)
69#endif
70
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080071/***************************************************************************/
72
73typedef struct mz_zip_s
74{
75 mz_zip_file file_info;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070076 mz_zip_file local_file_info;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080077
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080078 void *stream; /* main stream */
79 void *cd_stream; /* pointer to the stream with the cd */
80 void *cd_mem_stream; /* memory stream for central directory */
81 void *compress_stream; /* compression stream */
82 void *crypt_stream; /* encryption stream */
83 void *file_info_stream; /* memory stream for storing file info */
84 void *local_file_info_stream; /* memory stream for storing local file info */
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080085
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070086 int32_t open_mode;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -080087 uint8_t recover;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070088
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080089 uint32_t disk_number_with_cd; /* number of the disk with the central dir */
90 int64_t disk_offset_shift; /* correction for zips that have wrong offset start of cd */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070091
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080092 int64_t cd_start_pos; /* pos of the first file in the central dir stream */
93 int64_t cd_current_pos; /* pos of the current file in the central dir */
94 int64_t cd_offset; /* offset of start of central directory */
95 int64_t cd_size; /* size of the central directory */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070096
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080097 uint8_t entry_scanned; /* entry header information read ok */
98 uint8_t entry_opened; /* entry is open for read/write */
99 uint8_t entry_raw; /* entry opened with raw mode */
100 uint32_t entry_crc32; /* entry crc32 */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700101
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700102 uint64_t number_entry;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700103
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700104 uint16_t version_madeby;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700105 char *comment;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800106} mz_zip;
107
108/***************************************************************************/
109
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800110#if 0
111# define mz_zip_print printf
112#else
113# define mz_zip_print(fmt,...)
114#endif
115
116/***************************************************************************/
117
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800118/* Locate the end of central directory */
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700119static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800120{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700121 int64_t file_size = 0;
Nathan Moinvaziric78c7822018-12-01 20:42:36 -0800122 int64_t max_back = MZ_ZIP_EOCD_MAX_BACK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800123 uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8;
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700124 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700125
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700126 err = mz_stream_seek(stream, 0, MZ_SEEK_END);
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800127 if (err != MZ_OK)
128 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800129
130 file_size = mz_stream_tell(stream);
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800131
Nathan Moinvaziric78c7822018-12-01 20:42:36 -0800132 if (max_back <= 0 || max_back > file_size)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800133 max_back = file_size;
134
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800135 return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800136}
137
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800138/* Locate the end of central directory 64 of a zip file */
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700139static 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 +0800140{
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700141 int64_t offset = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800142 uint32_t value32 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700143 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700144
145
146 *central_pos = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800147
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800148 /* Zip64 end of central directory locator */
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700149 err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800150 /* Read locator signature */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700151 if (err == MZ_OK)
152 {
153 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700154 if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700155 err = MZ_FORMAT_ERROR;
156 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800157 /* Number of the disk with the start of the zip64 end of central directory */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700158 if (err == MZ_OK)
159 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800160 /* Relative offset of the zip64 end of central directory record8 */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700161 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700162 err = mz_stream_read_uint64(stream, (uint64_t *)&offset);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800163 /* Total number of disks */
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 /* Goto end of central directory record */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700167 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700168 err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800169 /* The signature */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700170 if (err == MZ_OK)
171 {
172 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700173 if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700174 err = MZ_FORMAT_ERROR;
175 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800176
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700177 if (err == MZ_OK)
178 *central_pos = offset;
179
180 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800181}
182
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800183/* Get info about the current file in the zip file */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700184static 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 -0700185{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700186 uint64_t ntfs_time = 0;
187 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700188 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700189 uint32_t dos_date = 0;
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800190 uint32_t field_pos = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700191 uint16_t field_type = 0;
192 uint16_t field_length = 0;
193 uint32_t field_length_read = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700194 uint16_t ntfs_attrib_id = 0;
195 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700196 uint16_t value16 = 0;
197 uint32_t value32 = 0;
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800198 int64_t extrafield_pos = 0;
199 int64_t comment_pos = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700200 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700201
202
203 memset(file_info, 0, sizeof(mz_zip_file));
204
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800205 /* Check the magic */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700206 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700207 if (err == MZ_END_OF_STREAM)
208 err = MZ_END_OF_LIST;
209 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700210 err = MZ_END_OF_LIST;
211 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
212 err = MZ_FORMAT_ERROR;
213 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
214 err = MZ_FORMAT_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000215
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800216 /* Read header fields */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700217 if (err == MZ_OK)
218 {
219 if (!local)
220 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
221 if (err == MZ_OK)
222 err = mz_stream_read_uint16(stream, &file_info->version_needed);
223 if (err == MZ_OK)
224 err = mz_stream_read_uint16(stream, &file_info->flag);
225 if (err == MZ_OK)
226 err = mz_stream_read_uint16(stream, &file_info->compression_method);
227 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700228 {
229 err = mz_stream_read_uint32(stream, &dos_date);
230 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
231 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700232 if (err == MZ_OK)
233 err = mz_stream_read_uint32(stream, &file_info->crc);
234 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700235 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700236 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700237 file_info->compressed_size = value32;
238 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700239 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700240 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700241 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700242 file_info->uncompressed_size = value32;
243 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700244 if (err == MZ_OK)
245 err = mz_stream_read_uint16(stream, &file_info->filename_size);
246 if (err == MZ_OK)
247 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
248 if (!local)
249 {
250 if (err == MZ_OK)
251 err = mz_stream_read_uint16(stream, &file_info->comment_size);
252 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700253 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700254 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700255 file_info->disk_number = value16;
256 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700257 if (err == MZ_OK)
258 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
259 if (err == MZ_OK)
260 err = mz_stream_read_uint32(stream, &file_info->external_fa);
261 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700262 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700263 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700264 file_info->disk_offset = value32;
265 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700266 }
267 }
268
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700269 if (err == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800270 err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700271
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800272 /* Copy variable length data to memory stream for later retrieval */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700273 if ((err == MZ_OK) && (file_info->filename_size > 0))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700274 err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800275 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800276 extrafield_pos = mz_stream_tell(file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700277
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800278 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
279 err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800280 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800281
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800282 comment_pos = mz_stream_tell(file_extra_stream);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800283 if ((err == MZ_OK) && (file_info->comment_size > 0))
284 err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800285 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800286
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700287 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
288 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800289 /* Seek to and parse the extra field */
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800290 err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700291
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800292 while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700293 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700294 err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length);
Nathan Moinvaziriea2bd792018-11-19 17:44:44 -0800295 if (err != MZ_OK)
296 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800297 field_pos += 4;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800298
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800299 /* Don't allow field length to exceed size of remaining extrafield */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800300 if (field_length > (file_info->extrafield_size - field_pos))
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -0800301 field_length = (uint16_t)(file_info->extrafield_size - field_pos);
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800302
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800303 /* Read ZIP64 extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800304 if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700305 {
306 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800307 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700308 err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800309 if (file_info->uncompressed_size < 0)
310 err = MZ_FORMAT_ERROR;
311 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700312 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800313 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700314 err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800315 if (file_info->compressed_size < 0)
316 err = MZ_FORMAT_ERROR;
317 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700318 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800319 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700320 err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800321 if (file_info->disk_offset < 0)
322 err = MZ_FORMAT_ERROR;
323 }
juanii4ae79922018-02-11 14:29:36 -0300324 if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700325 err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700326 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800327 /* Read NTFS extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800328 else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700329 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700330 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700331 err = mz_stream_read_uint32(file_extra_stream, &reserved);
332 field_length_read = 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700333
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800334 while ((err == MZ_OK) && (field_length_read + 4 <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700335 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700336 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700337 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700338 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800339 field_length_read += 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700340
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700341 if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700342 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700343 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700344 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
345
juanii7063b0e2018-02-11 13:56:21 -0300346 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700347 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700348 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700349 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
350 }
juanii7063b0e2018-02-11 13:56:21 -0300351 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700352 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700353 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700354 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
355 }
356 }
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800357 else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700358 {
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800359 err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700360 }
361
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800362 field_length_read += ntfs_attrib_size;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700363 }
364 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800365 /* Read UNIX1 extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800366 else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700367 {
368 if (err == MZ_OK && file_info->accessed_date == 0)
369 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700370 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700371 if (err == MZ_OK)
372 file_info->accessed_date = value32;
373 }
374 if (err == MZ_OK && file_info->modified_date == 0)
375 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700376 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700377 if (err == MZ_OK)
378 file_info->modified_date = value32;
379 }
380 if (err == MZ_OK)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800381 err = mz_stream_read_uint16(file_extra_stream, &value16); /* User id */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700382 if (err == MZ_OK)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800383 err = mz_stream_read_uint16(file_extra_stream, &value16); /* Group id */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700384
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800385 /* Skip variable data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800386 mz_stream_seek(file_extra_stream, field_length - 12, MZ_SEEK_CUR);
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700387 }
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800388#ifdef HAVE_WZAES
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800389 /* Read AES extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800390 else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700391 {
392 uint8_t value8 = 0;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800393 /* Verify version info */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700394 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800395 /* Support AE-1 and AE-2 */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700396 if (value16 != 1 && value16 != 2)
397 err = MZ_FORMAT_ERROR;
398 file_info->aes_version = value16;
399 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700400 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700401 if ((char)value8 != 'A')
402 err = MZ_FORMAT_ERROR;
403 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700404 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700405 if ((char)value8 != 'E')
406 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800407 /* Get AES encryption strength and actual compression method */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700408 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700409 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700410 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700411 file_info->aes_encryption_mode = value8;
412 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700413 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700414 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700415 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700416 file_info->compression_method = value16;
417 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700418 }
419#endif
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800420 else if (field_length > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700421 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700422 err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700423 }
424
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800425 field_pos += field_length;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700426 }
427 }
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800428
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800429 /* Get pointers to variable length data */
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800430 mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename);
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800431 mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield);
432 mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment);
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800433
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800434 /* Set to empty string just in-case */
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800435 if (file_info->filename == NULL)
436 file_info->filename = "";
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800437 if (file_info->extrafield == NULL)
438 file_info->extrafield_size = 0;
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800439 if (file_info->comment == NULL)
440 file_info->comment = "";
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700441
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800442 if (err == MZ_OK)
443 {
444 mz_zip_print("Zip - Entry - Read header - %s (local %"PRId8")\n",
445 file_info->filename, local);
Nathan Moinvaziri264dc182018-11-13 17:42:13 -0800446 mz_zip_print("Zip - Entry - Read header compress (ucs %"PRId64" cs %"PRId64" crc 0x%08"PRIx32")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800447 file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
448 if (!local)
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800449 {
450 mz_zip_print("Zip - Entry - Read header disk (disk %"PRIu32" offset %"PRId64")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800451 file_info->disk_number, file_info->disk_offset);
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800452 }
Nathan Moinvaziri264dc182018-11-13 17:42:13 -0800453 mz_zip_print("Zip - Entry - Read header variable (fnl %"PRId32" efs %"PRId32" cms %"PRId32")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800454 file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
455 }
456
457 return err;
458}
459
460static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size)
461{
462 uint32_t value32 = 0;
463 int64_t value64 = 0;
464 int32_t err = MZ_OK;
465
466
467 err = mz_stream_read_uint32(stream, &value32);
468 if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR)
469 err = MZ_FORMAT_ERROR;
470 if (err == MZ_OK)
471 err = mz_stream_read_uint32(stream, &value32);
472 if ((err == MZ_OK) && (crc32 != NULL))
473 *crc32 = value32;
474 if (err == MZ_OK)
475 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800476 /* If zip 64 extension is enabled then read as 8 byte */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800477 if (!zip64)
478 {
479 err = mz_stream_read_uint32(stream, &value32);
480 value64 = value32;
481 }
482 else
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800483 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800484 err = mz_stream_read_int64(stream, &value64);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800485 if (value64 < 0)
486 err = MZ_FORMAT_ERROR;
487 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800488 if ((err == MZ_OK) && (compressed_size != NULL))
489 *compressed_size = value64;
490 }
491 if (err == MZ_OK)
492 {
493 if (!zip64)
494 {
495 err = mz_stream_read_uint32(stream, &value32);
496 value64 = value32;
497 }
498 else
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800499 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800500 err = mz_stream_read_int64(stream, &value64);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800501 if (value64 < 0)
502 err = MZ_FORMAT_ERROR;
503 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800504 if ((err == MZ_OK) && (uncompressed_size != NULL))
505 *uncompressed_size = value64;
506 }
507
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700508 return err;
509}
510
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700511static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700512{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700513 uint64_t ntfs_time = 0;
514 uint32_t reserved = 0;
515 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700516 uint16_t extrafield_size = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700517 uint16_t field_type = 0;
518 uint16_t field_length = 0;
519 uint16_t field_length_zip64 = 0;
520 uint16_t field_length_ntfs = 0;
521 uint16_t field_length_aes = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700522 uint16_t filename_size = 0;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700523 uint16_t filename_length = 0;
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700524 uint16_t version_needed = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800525 int32_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700526 int32_t err = MZ_OK;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700527 int32_t err_mem = MZ_OK;
528 uint8_t zip64 = 0;
529 uint8_t skip_aes = 0;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700530 uint8_t mask = 0;
531 uint8_t write_end_slash = 0;
532 const char *filename = NULL;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700533 char masked_name[64];
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700534 void *file_extra_stream = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700535
536 if (file_info == NULL)
537 return MZ_PARAM_ERROR;
538
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700539 if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO))
540 mask = 1;
541
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800542 /* Calculate extra field sizes */
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700543 if (file_info->uncompressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700544 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700545 if (file_info->compressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700546 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700547 if (file_info->disk_offset >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700548 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700549
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700550 if (file_info->zip64 == MZ_ZIP64_AUTO)
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700551 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800552 /* If uncompressed size is unknown, assume zip64 for 64-bit data descriptors */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700553 zip64 = (local && file_info->uncompressed_size == 0) || (field_length_zip64 > 0);
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700554 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700555 else if (file_info->zip64 == MZ_ZIP64_FORCE)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700556 {
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700557 zip64 = 1;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700558 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700559 else if (file_info->zip64 == MZ_ZIP64_DISABLE)
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700560 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800561 /* Zip64 extension is required to zip file */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700562 if (field_length_zip64 > 0)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700563 return MZ_PARAM_ERROR;
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700564 }
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700565
566 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700567 {
568 extrafield_size += 4;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700569 extrafield_size += field_length_zip64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700570 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700571
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800572 /* Calculate extra field size and check for duplicates */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700573 if (file_info->extrafield_size > 0)
574 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700575 mz_stream_mem_create(&file_extra_stream);
Nathan Moinvaziri915c5132018-10-26 20:00:52 -0700576 mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
577 file_info->extrafield_size);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700578
579 do
580 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700581 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700582 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700583 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700584 if (err_mem != MZ_OK)
585 break;
586
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800587 /* Prefer incoming aes extensions over ours */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700588 if (field_type == MZ_ZIP_EXTENSION_AES)
589 skip_aes = 1;
590
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800591 /* Prefer our zip64, ntfs extension over incoming */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700592 if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS)
593 extrafield_size += 4 + field_length;
594
595 if (err_mem == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800596 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700597 }
598 while (err_mem == MZ_OK);
599 }
600
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800601#ifdef HAVE_WZAES
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700602 if (!skip_aes)
603 {
604 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700605 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700606 field_length_aes = 1 + 1 + 1 + 2 + 2;
607 extrafield_size += 4 + field_length_aes;
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700608 }
609 }
Nathan Moinvaziria2bb08c2018-10-28 13:45:30 -0700610#else
Nathan Moinvaziri7de94cf2018-11-19 20:51:06 -0800611 MZ_UNUSED(field_length_aes);
Nathan Moinvaziri4ae469a2018-10-28 15:32:04 -0700612 MZ_UNUSED(skip_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700613#endif
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800614 /* NTFS timestamps */
Nathan Moinvazirid9e159a2018-02-13 15:30:30 -0800615 if ((file_info->modified_date != 0) &&
616 (file_info->accessed_date != 0) &&
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700617 (file_info->creation_date != 0) && (!mask))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700618 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700619 field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
620 extrafield_size += 4 + field_length_ntfs;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700621 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700622
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700623 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700624 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700625 else
626 {
627 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
628 if (err == MZ_OK)
629 err = mz_stream_write_uint16(stream, file_info->version_madeby);
630 }
631
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800632 /* Calculate version needed to extract */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700633 if (err == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700634 {
635 version_needed = file_info->version_needed;
636 if (version_needed == 0)
637 {
638 version_needed = 20;
639 if (zip64)
640 version_needed = 45;
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800641#ifdef HAVE_WZAES
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700642 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
643 version_needed = 51;
644#endif
645#ifdef HAVE_LZMA
646 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
647 version_needed = 63;
648#endif
649 }
650 err = mz_stream_write_uint16(stream, version_needed);
651 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700652 if (err == MZ_OK)
653 err = mz_stream_write_uint16(stream, file_info->flag);
654 if (err == MZ_OK)
655 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800656#ifdef HAVE_WZAES
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700657 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700658 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700659 else
660#endif
661 err = mz_stream_write_uint16(stream, file_info->compression_method);
662 }
663 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700664 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700665 if (file_info->modified_date != 0 && !mask)
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -0700666 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700667 err = mz_stream_write_uint32(stream, dos_date);
668 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700669
670 if (err == MZ_OK)
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700671 {
672 if (mask)
673 err = mz_stream_write_uint32(stream, 0);
674 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800675 err = mz_stream_write_uint32(stream, file_info->crc); /* crc */
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700676 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700677 if (err == MZ_OK)
678 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800679 if (file_info->compressed_size >= UINT32_MAX) /* compr size */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700680 err = mz_stream_write_uint32(stream, UINT32_MAX);
681 else
682 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
683 }
684 if (err == MZ_OK)
685 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800686 if (file_info->uncompressed_size >= UINT32_MAX) /* uncompr size */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700687 err = mz_stream_write_uint32(stream, UINT32_MAX);
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700688 else if (mask)
689 err = mz_stream_write_uint32(stream, 0);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700690 else
691 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
692 }
693
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700694 if (mask)
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700695 {
Nathan Moinvaziri264dc182018-11-13 17:42:13 -0800696 snprintf(masked_name, sizeof(masked_name), "%"PRIx32"_%"PRIx64,
697 file_info->disk_number, file_info->disk_offset);
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700698 filename = masked_name;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700699 }
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700700 else
701 {
702 filename = file_info->filename;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700703 }
704
705 filename_length = (uint16_t)strlen(filename);
706 filename_size += filename_length;
707
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800708 if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) &&
709 ((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\')))
710 {
711 filename_size += 1;
712 write_end_slash = 1;
713 }
714
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700715 if (err == MZ_OK)
716 err = mz_stream_write_uint16(stream, filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700717 if (err == MZ_OK)
718 err = mz_stream_write_uint16(stream, extrafield_size);
719
720 if (!local)
721 {
722 if (file_info->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800723 {
724 comment_size = (int32_t)strlen(file_info->comment);
725 if (comment_size > UINT16_MAX)
726 comment_size = UINT16_MAX;
727 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700728 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800729 err = mz_stream_write_uint16(stream, (uint16_t)comment_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700730 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700731 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700732 if (err == MZ_OK)
733 err = mz_stream_write_uint16(stream, file_info->internal_fa);
734 if (err == MZ_OK)
735 err = mz_stream_write_uint32(stream, file_info->external_fa);
736 if (err == MZ_OK)
737 {
738 if (file_info->disk_offset >= UINT32_MAX)
739 err = mz_stream_write_uint32(stream, UINT32_MAX);
740 else
741 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
742 }
743 }
744
745 if (err == MZ_OK)
746 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700747 if (mz_stream_write(stream, filename, filename_length) != filename_length)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700748 err = MZ_WRITE_ERROR;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700749
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800750 /* Ensure that directories have a slash appended to them for compatibility */
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700751 if (err == MZ_OK && write_end_slash)
752 err = mz_stream_write_uint8(stream, '/');
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700753 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700754
755 if (file_info->extrafield_size > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700756 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800757 err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700758 while (err == MZ_OK && err_mem == MZ_OK)
759 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700760 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700761 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700762 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700763 if (err_mem != MZ_OK)
764 break;
765
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800766 /* Prefer our zip 64, ntfs extensions over incoming */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700767 if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS)
768 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800769 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700770 continue;
771 }
772
773 err = mz_stream_write_uint16(stream, field_type);
774 if (err == MZ_OK)
775 err = mz_stream_write_uint16(stream, field_length);
776 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700777 err = mz_stream_copy(stream, file_extra_stream, field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700778 }
779
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700780 mz_stream_mem_delete(&file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700781 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700782
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800783 /* Write ZIP64 extra field */
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700784 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700785 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700786 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700787 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700788 {
789 if (mask)
790 err = mz_stream_write_int64(stream, 0);
791 else
792 err = mz_stream_write_int64(stream, file_info->uncompressed_size);
793 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700794 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700795 err = mz_stream_write_int64(stream, file_info->compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700796 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700797 err = mz_stream_write_int64(stream, file_info->disk_offset);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700798 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800799 /* Write NTFS extra field */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700800 if ((err == MZ_OK) && (field_length_ntfs > 0))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700801 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700802 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700803 if (err == MZ_OK)
804 err = mz_stream_write_uint32(stream, reserved);
805 if (err == MZ_OK)
806 err = mz_stream_write_uint16(stream, 0x01);
807 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700808 err = mz_stream_write_uint16(stream, field_length_ntfs - 8);
juanii3679a3d2018-02-11 13:55:38 -0300809 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700810 {
811 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
812 err = mz_stream_write_uint64(stream, ntfs_time);
813 }
juanii3679a3d2018-02-11 13:55:38 -0300814 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700815 {
816 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
817 err = mz_stream_write_uint64(stream, ntfs_time);
818 }
juanii3679a3d2018-02-11 13:55:38 -0300819 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700820 {
821 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
822 err = mz_stream_write_uint64(stream, ntfs_time);
823 }
824 }
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800825#ifdef HAVE_WZAES
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800826 /* Write AES extra field */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700827 if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700828 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700829 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700830 if (err == MZ_OK)
831 err = mz_stream_write_uint16(stream, file_info->aes_version);
832 if (err == MZ_OK)
833 err = mz_stream_write_uint8(stream, 'A');
834 if (err == MZ_OK)
835 err = mz_stream_write_uint8(stream, 'E');
836 if (err == MZ_OK)
837 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
838 if (err == MZ_OK)
839 err = mz_stream_write_uint16(stream, file_info->compression_method);
840 }
841#endif
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700842 if ((err == MZ_OK) && (!local) && (file_info->comment != NULL))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700843 {
Nathan Moinvazirica014e62018-08-15 07:31:12 -0700844 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700845 err = MZ_WRITE_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700846 }
847
848 return err;
849}
850
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800851static 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 -0800852{
853 int32_t err = MZ_OK;
854
855 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
856 if (err == MZ_OK)
857 err = mz_stream_write_uint32(stream, crc32);
858
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800859 /* Store data descriptor as 8 bytes if zip 64 extension enabled */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800860 if (err == MZ_OK)
861 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800862 /* Zip 64 extension is enabled when uncompressed size is > UINT32_MAX */
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800863 if (!zip64)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800864 err = mz_stream_write_uint32(stream, (uint32_t)compressed_size);
865 else
866 err = mz_stream_write_int64(stream, compressed_size);
867 }
868 if (err == MZ_OK)
869 {
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800870 if (!zip64)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800871 err = mz_stream_write_uint32(stream, (uint32_t)uncompressed_size);
872 else
873 err = mz_stream_write_int64(stream, uncompressed_size);
874 }
875
876 return err;
877}
878
879static int32_t mz_zip_read_cd(void *handle)
880{
881 mz_zip *zip = (mz_zip *)handle;
882 uint64_t number_entry_cd64 = 0;
883 uint64_t number_entry = 0;
884 uint64_t number_entry_cd = 0;
885 int64_t eocd_pos = 0;
886 int64_t eocd_pos64 = 0;
887 int64_t value64i = 0;
888 uint16_t value16 = 0;
889 uint32_t value32 = 0;
890 uint64_t value64 = 0;
891 uint16_t comment_size = 0;
892 int32_t comment_read = 0;
893 int32_t err = MZ_OK;
894
895
896 if (zip == NULL)
897 return MZ_PARAM_ERROR;
898
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800899 /* Read and cache central directory records */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800900 err = mz_zip_search_eocd(zip->stream, &eocd_pos);
901 if (err == MZ_OK)
902 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800903 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800904 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800905 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800906 if (err == MZ_OK)
907 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800908 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800909 if (err == MZ_OK)
910 err = mz_stream_read_uint16(zip->stream, &value16);
911 zip->disk_number_with_cd = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800912 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800913 if (err == MZ_OK)
914 err = mz_stream_read_uint16(zip->stream, &value16);
915 zip->number_entry = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800916 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800917 if (err == MZ_OK)
918 err = mz_stream_read_uint16(zip->stream, &value16);
919 number_entry_cd = value16;
920 if (number_entry_cd != zip->number_entry)
921 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800922 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800923 if (err == MZ_OK)
924 err = mz_stream_read_uint32(zip->stream, &value32);
925 if (err == MZ_OK)
926 zip->cd_size = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800927 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800928 if (err == MZ_OK)
929 err = mz_stream_read_uint32(zip->stream, &value32);
930 if (err == MZ_OK)
931 zip->cd_offset = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800932 /* Zip file global comment length */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800933 if (err == MZ_OK)
934 err = mz_stream_read_uint16(zip->stream, &comment_size);
935 if ((err == MZ_OK) && (comment_size > 0))
936 {
937 zip->comment = (char *)MZ_ALLOC(comment_size + 1);
938 if (zip->comment != NULL)
939 {
940 comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800941 /* Don't fail if incorrect comment length read, not critical */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800942 if (comment_read < 0)
943 comment_read = 0;
944 zip->comment[comment_read] = 0;
945 }
946 }
947
948 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
949 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800950 /* Format should be Zip64, as the central directory or file size is too large */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800951 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
952 {
953 eocd_pos = eocd_pos64;
954
955 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800956 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800957 if (err == MZ_OK)
958 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800959 /* Size of zip64 end of central directory record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800960 if (err == MZ_OK)
961 err = mz_stream_read_uint64(zip->stream, &value64);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800962 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800963 if (err == MZ_OK)
964 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800965 /* Version needed to extract */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800966 if (err == MZ_OK)
967 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800968 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800969 if (err == MZ_OK)
970 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800971 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800972 if (err == MZ_OK)
973 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800974 /* Total number of entries in the central directory on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800975 if (err == MZ_OK)
976 err = mz_stream_read_uint64(zip->stream, &number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800977 /* Total number of entries in the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800978 if (err == MZ_OK)
979 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
980 if (number_entry == UINT32_MAX)
981 zip->number_entry = number_entry_cd64;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800982 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800983 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800984 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800985 err = mz_stream_read_int64(zip->stream, &zip->cd_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800986 if (zip->cd_size < 0)
987 err = MZ_FORMAT_ERROR;
988 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800989 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800990 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800991 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800992 err = mz_stream_read_int64(zip->stream, &zip->cd_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800993 if (zip->cd_offset < 0)
994 err = MZ_FORMAT_ERROR;
995 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800996 }
997 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
998 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
999 {
1000 err = MZ_FORMAT_ERROR;
1001 }
1002 }
1003 }
1004
1005 if (err == MZ_OK)
1006 {
Nathan Moinvaziri264dc182018-11-13 17:42:13 -08001007 mz_zip_print("Zip - Read cd (disk %"PRId32" entries %"PRId64" offset %"PRId64" size %"PRId64")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001008 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1009
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001010 /* Verify central directory signature exists at offset */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001011 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1012 if (err == MZ_OK)
1013 err = mz_stream_read_uint32(zip->stream, &value32);
1014 if (value32 != MZ_ZIP_MAGIC_CENTRALHEADER)
1015 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001016 /* If not found attempt to seek backward to find it */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001017 err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
1018 if (err == MZ_OK)
1019 err = mz_stream_read_uint32(zip->stream, &value32);
1020 if (value32 == MZ_ZIP_MAGIC_CENTRALHEADER)
1021 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001022 /* If found compensate for incorrect locations */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001023 value64i = zip->cd_offset;
1024 zip->cd_offset = eocd_pos - zip->cd_size;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001025 /* Assume disk has prepended data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001026 zip->disk_offset_shift = zip->cd_offset - value64i;
1027 }
1028 }
1029 }
1030
1031 if (err == MZ_OK)
1032 {
1033 if (eocd_pos < zip->cd_offset)
1034 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001035 /* End of central dir should always come after central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001036 err = MZ_FORMAT_ERROR;
1037 }
1038 else if (eocd_pos < zip->cd_offset + zip->cd_size)
1039 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001040 /* Truncate size of cd if incorrect size or offset provided */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001041 zip->cd_size = eocd_pos - zip->cd_offset;
1042 }
1043 }
1044
1045 return err;
1046}
1047
1048static int32_t mz_zip_write_cd(void *handle)
1049{
1050 mz_zip *zip = (mz_zip *)handle;
1051 int64_t zip64_eocd_pos_inzip = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001052 int64_t disk_number = 0;
1053 int64_t disk_size = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001054 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001055 int32_t err = MZ_OK;
1056
1057
1058 if (zip == NULL)
1059 return MZ_PARAM_ERROR;
1060
1061 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
1062 zip->disk_number_with_cd = (uint32_t)disk_number;
1063 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
1064 zip->disk_number_with_cd += 1;
1065 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1066
1067 zip->cd_offset = mz_stream_tell(zip->stream);
1068 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
1069 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
1070 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
1071
1072 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
1073
Nathan Moinvaziri264dc182018-11-13 17:42:13 -08001074 mz_zip_print("Zip - Write cd (disk %"PRId32" entries %"PRId64" offset %"PRId64" size %"PRId64")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001075 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1076
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001077 /* Write the ZIP64 central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001078 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
1079 {
1080 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
1081
1082 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
1083
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001084 /* Size of this 'zip64 end of central directory' */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001085 if (err == MZ_OK)
1086 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001087 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001088 if (err == MZ_OK)
1089 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001090 /* Version needed */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001091 if (err == MZ_OK)
1092 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001093 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001094 if (err == MZ_OK)
1095 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001096 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001097 if (err == MZ_OK)
1098 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001099 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001100 if (err == MZ_OK)
1101 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001102 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001103 if (err == MZ_OK)
1104 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001105 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001106 if (err == MZ_OK)
1107 err = mz_stream_write_int64(zip->stream, zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001108 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001109 if (err == MZ_OK)
1110 err = mz_stream_write_int64(zip->stream, zip->cd_offset);
1111 if (err == MZ_OK)
1112 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
1113
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001114 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001115 if (err == MZ_OK)
1116 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001117 /* Relative offset to the end of zip64 central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001118 if (err == MZ_OK)
1119 err = mz_stream_write_int64(zip->stream, zip64_eocd_pos_inzip);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001120 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001121 if (err == MZ_OK)
1122 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
1123 }
1124
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001125 /* Write the central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001126
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001127 /* Signature */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001128 if (err == MZ_OK)
1129 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001130 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001131 if (err == MZ_OK)
1132 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001133 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001134 if (err == MZ_OK)
1135 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001136 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001137 if (err == MZ_OK)
1138 {
1139 if (zip->number_entry >= UINT16_MAX)
1140 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1141 else
1142 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1143 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001144 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001145 if (err == MZ_OK)
1146 {
1147 if (zip->number_entry >= UINT16_MAX)
1148 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1149 else
1150 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1151 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001152 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001153 if (err == MZ_OK)
1154 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001155 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001156 if (err == MZ_OK)
1157 {
1158 if (zip->cd_offset >= UINT32_MAX)
1159 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
1160 else
1161 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
1162 }
1163
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001164 /* Write global comment */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001165 if (zip->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001166 {
1167 comment_size = (int32_t)strlen(zip->comment);
1168 if (comment_size > UINT16_MAX)
1169 comment_size = UINT16_MAX;
1170 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001171 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001172 err = mz_stream_write_uint16(zip->stream, (uint16_t)comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001173 if (err == MZ_OK)
1174 {
1175 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
1176 err = MZ_READ_ERROR;
1177 }
1178 return err;
1179}
1180
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001181static int32_t mz_zip_recover_cd(void *handle)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001182{
1183 mz_zip *zip = (mz_zip *)handle;
1184 mz_zip_file local_file_info;
1185 void *local_file_info_stream = NULL;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001186 void *cd_mem_stream = NULL;
1187 uint64_t number_entry = 0;
1188 int64_t descriptor_pos = 0;
1189 int64_t disk_offset = 0;
1190 int64_t disk_number = 0;
1191 int64_t compressed_size = 0;
1192 int64_t uncompressed_size = 0;
1193 uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8;
1194 uint32_t crc32 = 0;
1195 int32_t disk_number_with_cd = 0;
1196 int32_t err = MZ_OK;
1197 uint8_t zip64 = 0;
1198
1199
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001200 mz_zip_print("Zip - Recover cd\n");
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001201
1202 mz_zip_get_cd_mem_stream(handle, &cd_mem_stream);
1203
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001204 /* Determine if we are on a split disk or not */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001205 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0);
1206 if (mz_stream_tell(zip->stream) < 0)
1207 {
1208 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1209 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1210 }
1211 else
1212 disk_number_with_cd = 1;
1213
1214 if (mz_stream_is_open(cd_mem_stream) != MZ_OK)
1215 err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1216
1217 mz_stream_mem_create(&local_file_info_stream);
1218 mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1219
1220 while (err == MZ_OK)
1221 {
1222 memset(&local_file_info, 0, sizeof(local_file_info));
1223
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001224 /* Get current offset and disk number for central dir record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001225 disk_offset = mz_stream_tell(zip->stream);
1226 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1227
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001228 /* Read local headers */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001229 err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream);
1230
1231 local_file_info.disk_offset = disk_offset;
1232 if (disk_number < 0)
1233 disk_number = 0;
1234 local_file_info.disk_number = (uint32_t)disk_number;
1235
1236 if (err == MZ_OK)
1237 {
1238 if (local_file_info.compressed_size > 0)
1239 {
1240 err = mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR);
1241 }
1242 else if (local_file_info.uncompressed_size > 0)
1243 {
1244 err = mz_stream_find(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
1245 INT64_MAX, &descriptor_pos);
1246 }
1247 }
1248
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001249 /* Read descriptor if it exists so we can get to the next local header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001250 if ((err == MZ_OK) && (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
1251 {
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001252 if (mz_zip_extrafield_contains(local_file_info.extrafield,
1253 local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001254 zip64 = 1;
1255
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001256 err = mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32,
1257 &compressed_size, &uncompressed_size);
1258
1259 if (local_file_info.crc == 0)
1260 local_file_info.crc = crc32;
1261 if (local_file_info.compressed_size == 0)
1262 local_file_info.compressed_size = compressed_size;
1263 if (local_file_info.uncompressed_size == 0)
1264 local_file_info.uncompressed_size = uncompressed_size;
1265 }
1266
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001267 /* Rewrite central dir with local headers and offsets */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001268 if (err == MZ_OK)
1269 err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
1270
1271 if (err == MZ_OK)
1272 number_entry += 1;
1273 }
1274
1275 mz_stream_mem_delete(&local_file_info_stream);
1276
Nathan Moinvaziri264dc182018-11-13 17:42:13 -08001277 mz_zip_print("Zip - Recover cd complete (cddisk %"PRId32" entries %"PRId64")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001278 disk_number_with_cd, number_entry);
1279
1280 if (number_entry == 0)
1281 return err;
1282
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001283 /* Set new upper seek boundary for central dir mem stream */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001284 disk_offset = mz_stream_tell(cd_mem_stream);
1285 mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset);
1286
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001287 /* Set new central directory info */
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001288 mz_zip_set_cd_stream(handle, 0, cd_mem_stream);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001289 mz_zip_set_number_entry(handle, number_entry);
1290 mz_zip_set_disk_number_with_cd(handle, disk_number_with_cd);
1291
1292 return MZ_OK;
1293}
1294
1295void *mz_zip_create(void **handle)
1296{
1297 mz_zip *zip = NULL;
1298
1299 zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
1300 if (zip != NULL)
1301 memset(zip, 0, sizeof(mz_zip));
1302 if (handle != NULL)
1303 *handle = zip;
1304
1305 return zip;
1306}
1307
1308void mz_zip_delete(void **handle)
1309{
1310 mz_zip *zip = NULL;
1311 if (handle == NULL)
1312 return;
1313 zip = (mz_zip *)*handle;
1314 if (zip != NULL)
1315 {
1316 MZ_FREE(zip);
1317 }
1318 *handle = NULL;
1319}
1320
1321int32_t mz_zip_open(void *handle, void *stream, int32_t mode)
1322{
1323 mz_zip *zip = (mz_zip *)handle;
1324 int32_t err = MZ_OK;
1325
1326
1327 if (zip == NULL)
1328 return MZ_PARAM_ERROR;
1329
1330 mz_zip_print("Zip - Open\n");
1331
1332 zip->stream = stream;
1333
1334 mz_stream_mem_create(&zip->cd_mem_stream);
1335
1336 if (mode & MZ_OPEN_MODE_WRITE)
1337 {
1338 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1339 zip->cd_stream = zip->cd_mem_stream;
1340 }
1341 else
1342 {
1343 zip->cd_stream = stream;
1344 }
1345
1346 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
1347 {
1348 if ((mode & MZ_OPEN_MODE_CREATE) == 0)
1349 {
1350 err = mz_zip_read_cd(zip);
1351 if (err != MZ_OK)
1352 {
Nathan Moinvaziri264dc182018-11-13 17:42:13 -08001353 mz_zip_print("Zip - Error detected reading cd (%"PRId32")", err);
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001354 if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001355 err = MZ_OK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001356 }
1357 }
1358
1359 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
1360 {
1361 if (zip->cd_size > 0)
1362 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001363 /* Store central directory in memory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001364 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1365 if (err == MZ_OK)
1366 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size);
1367 if (err == MZ_OK)
1368 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1369 }
1370 else
1371 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001372 /* If no central directory, append new zip to end of file */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001373 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1374 }
Nathan Moinvaziri2ce1fe22018-11-23 11:53:11 -08001375
1376 if (zip->disk_number_with_cd > 0)
1377 {
1378 /* Move to last disk to begin appending */
1379 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->disk_number_with_cd - 1);
1380 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001381 }
1382 else
1383 {
1384 zip->cd_start_pos = zip->cd_offset;
1385 }
1386 }
1387
1388 if (err != MZ_OK)
1389 {
1390 mz_zip_close(zip);
1391 return err;
1392 }
1393
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001394 /* Memory streams used to store variable length file info data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001395 mz_stream_mem_create(&zip->file_info_stream);
1396 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1397
1398 mz_stream_mem_create(&zip->local_file_info_stream);
1399 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1400
1401 zip->open_mode = mode;
1402
1403 return err;
1404}
1405
1406int32_t mz_zip_close(void *handle)
1407{
1408 mz_zip *zip = (mz_zip *)handle;
1409 int32_t err = MZ_OK;
1410
1411 if (zip == NULL)
1412 return MZ_PARAM_ERROR;
1413
1414 mz_zip_print("Zip - Close\n");
1415
1416 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001417 err = mz_zip_entry_close(handle);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001418
1419 if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE))
1420 err = mz_zip_write_cd(handle);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001421
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001422 if (zip->cd_mem_stream != NULL)
1423 {
1424 mz_stream_close(zip->cd_mem_stream);
1425 mz_stream_delete(&zip->cd_mem_stream);
1426 }
1427
1428 if (zip->file_info_stream != NULL)
1429 {
1430 mz_stream_mem_close(zip->file_info_stream);
1431 mz_stream_mem_delete(&zip->file_info_stream);
1432 }
1433 if (zip->local_file_info_stream != NULL)
1434 {
1435 mz_stream_mem_close(zip->local_file_info_stream);
1436 mz_stream_mem_delete(&zip->local_file_info_stream);
1437 }
1438
1439 if (zip->comment)
1440 {
1441 MZ_FREE(zip->comment);
1442 zip->comment = NULL;
1443 }
1444
1445 zip->stream = NULL;
1446 zip->cd_stream = NULL;
1447
1448 return err;
1449}
1450
1451int32_t mz_zip_get_comment(void *handle, const char **comment)
1452{
1453 mz_zip *zip = (mz_zip *)handle;
1454 if (zip == NULL || comment == NULL)
1455 return MZ_PARAM_ERROR;
1456 if (zip->comment == NULL)
1457 return MZ_EXIST_ERROR;
1458 *comment = zip->comment;
1459 return MZ_OK;
1460}
1461
1462int32_t mz_zip_set_comment(void *handle, const char *comment)
1463{
1464 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001465 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001466 if (zip == NULL || comment == NULL)
1467 return MZ_PARAM_ERROR;
1468 if (zip->comment != NULL)
1469 MZ_FREE(zip->comment);
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001470 comment_size = (int32_t)strlen(comment);
1471 if (comment_size > UINT16_MAX)
1472 return MZ_PARAM_ERROR;
Nathan Moinvaziria6ecab52019-01-22 13:14:01 -08001473 zip->comment = (char *)MZ_ALLOC(comment_size+1);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001474 if (zip->comment == NULL)
1475 return MZ_MEM_ERROR;
Nathan Moinvaziria6ecab52019-01-22 13:14:01 -08001476 memset(zip->comment, 0, comment_size+1);
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001477 strncpy(zip->comment, comment, comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001478 return MZ_OK;
1479}
1480
1481int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
1482{
1483 mz_zip *zip = (mz_zip *)handle;
1484 if (zip == NULL || version_madeby == NULL)
1485 return MZ_PARAM_ERROR;
1486 *version_madeby = zip->version_madeby;
1487 return MZ_OK;
1488}
1489
1490int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
1491{
1492 mz_zip *zip = (mz_zip *)handle;
1493 if (zip == NULL)
1494 return MZ_PARAM_ERROR;
1495 zip->version_madeby = version_madeby;
1496 return MZ_OK;
1497}
1498
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001499int32_t mz_zip_set_recover(void *handle, uint8_t recover)
1500{
1501 mz_zip *zip = (mz_zip *)handle;
1502 if (zip == NULL)
1503 return MZ_PARAM_ERROR;
1504 zip->recover = recover;
1505 return MZ_OK;
1506}
1507
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001508int32_t mz_zip_get_stream(void *handle, void **stream)
1509{
1510 mz_zip *zip = (mz_zip *)handle;
1511 if (zip == NULL || stream == NULL)
1512 return MZ_PARAM_ERROR;
1513 *stream = zip->stream;
1514 if (*stream == NULL)
1515 return MZ_EXIST_ERROR;
1516 return MZ_OK;
1517}
1518
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001519int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001520{
1521 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001522 if (zip == NULL || cd_stream == NULL)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001523 return MZ_PARAM_ERROR;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001524 zip->cd_stream = cd_stream;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001525 zip->cd_start_pos = cd_start_pos;
1526 return MZ_OK;
1527}
1528
1529int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream)
1530{
1531 mz_zip *zip = (mz_zip *)handle;
1532 if (zip == NULL || cd_mem_stream == NULL)
1533 return MZ_PARAM_ERROR;
1534 *cd_mem_stream = zip->cd_mem_stream;
1535 if (*cd_mem_stream == NULL)
1536 return MZ_EXIST_ERROR;
1537 return MZ_OK;
1538}
1539
1540static int32_t mz_zip_entry_close_int(void *handle)
1541{
1542 mz_zip *zip = (mz_zip *)handle;
1543
1544 if (zip->crypt_stream != NULL)
1545 mz_stream_delete(&zip->crypt_stream);
1546 zip->crypt_stream = NULL;
1547 if (zip->compress_stream != NULL)
1548 mz_stream_delete(&zip->compress_stream);
1549 zip->compress_stream = NULL;
1550
1551 zip->entry_opened = 0;
1552
1553 return MZ_OK;
1554}
1555
Nathan Moinvaziri26308b22018-08-08 09:40:15 -07001556static 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 -07001557{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001558 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001559 int64_t max_total_in = 0;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001560 int64_t header_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001561 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001562 int32_t err = MZ_OK;
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001563 uint8_t use_crypt = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001564
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001565 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001566 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001567
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001568 switch (zip->file_info.compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001569 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001570 case MZ_COMPRESS_METHOD_STORE:
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001571 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001572#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001573 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001574#endif
Nathan Moinvaziri566dfe02018-10-26 00:36:30 -07001575#ifdef HAVE_LZMA
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001576 case MZ_COMPRESS_METHOD_LZMA:
1577#endif
1578 err = MZ_OK;
1579 break;
1580 default:
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001581 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001582 }
1583
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001584#ifndef HAVE_WZAES
1585 if (zip->file_info.aes_version)
1586 return MZ_SUPPORT_ERROR;
1587#endif
1588
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001589 zip->entry_raw = raw;
1590
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001591 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password != NULL))
1592 {
1593 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
1594 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001595 /* Encrypt only when we are not trying to write raw and password is supplied. */
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001596 if (!zip->entry_raw)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001597 use_crypt = 1;
1598 }
1599 else if (zip->open_mode & MZ_OPEN_MODE_READ)
1600 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001601 /* Decrypt only when password is supplied. Don't error when password */
1602 /* is not supplied as we may want to read the raw encrypted data. */
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001603 use_crypt = 1;
1604 }
1605 }
1606
1607 if ((err == MZ_OK) && (use_crypt))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001608 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001609#ifdef HAVE_WZAES
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001610 if (zip->file_info.aes_version)
1611 {
Nathan Moinvaziri21a31022018-10-24 09:50:16 -07001612 mz_stream_wzaes_create(&zip->crypt_stream);
1613 mz_stream_wzaes_set_password(zip->crypt_stream, password);
1614 mz_stream_wzaes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001615 }
1616 else
1617#endif
1618 {
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001619#ifdef HAVE_PKCRYPT
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001620 uint8_t verify1 = 0;
1621 uint8_t verify2 = 0;
1622
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001623 /* Info-ZIP modification to ZipCrypto format: */
1624 /* 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 -07001625
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001626 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1627 {
1628 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001629
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001630 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1631
1632 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1633 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1634 }
1635 else
1636 {
1637 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1638 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1639 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001640
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001641 mz_stream_pkcrypt_create(&zip->crypt_stream);
1642 mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1643 mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001644#endif
1645 }
1646 }
1647
1648 if (err == MZ_OK)
1649 {
1650 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001651 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001652
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001653 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001654
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001655 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001656 }
1657
1658 if (err == MZ_OK)
1659 {
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001660 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001661 mz_stream_raw_create(&zip->compress_stream);
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001662#if defined(HAVE_ZLIB) || defined(HAVE_LIBCOMP)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001663 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001664 mz_stream_zlib_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001665#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001666#ifdef HAVE_BZIP2
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001667 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001668 mz_stream_bzip_create(&zip->compress_stream);
1669#endif
1670#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001671 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001672 mz_stream_lzma_create(&zip->compress_stream);
1673#endif
1674 else
1675 err = MZ_PARAM_ERROR;
1676 }
1677
1678 if (err == MZ_OK)
1679 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001680 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001681 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001682 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001683 }
1684 else
1685 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001686#ifndef HAVE_LIBCOMP
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001687 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 -08001688#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001689 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001690 max_total_in = zip->file_info.compressed_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001691 mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1692
1693 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1694 max_total_in -= header_size;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001695 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1696 max_total_in -= footer_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001697
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001698 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001699 }
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001700 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 -03001701 {
1702 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1703 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1704 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001705 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001706
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001707 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1708
1709 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1710 }
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001711
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001712 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001713 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001714 zip->entry_opened = 1;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001715 zip->entry_crc32 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001716 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001717 else
1718 {
1719 mz_zip_entry_close_int(handle);
1720 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001721
1722 return err;
1723}
1724
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001725int32_t mz_zip_entry_is_open(void *handle)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001726{
1727 mz_zip *zip = (mz_zip *)handle;
1728 if (zip == NULL)
1729 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001730 if (zip->entry_opened == 0)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001731 return MZ_EXIST_ERROR;
1732 return MZ_OK;
1733}
1734
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001735static int32_t mz_zip_seek_to_local_header(void *handle)
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001736{
1737 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001738
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001739
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001740 if (zip->file_info.disk_number == zip->disk_number_with_cd)
1741 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1742 else
1743 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
1744
1745 mz_zip_print("Zip - Entry - Seek local (disk %"PRId32" offset %"PRId64")\n",
1746 zip->file_info.disk_number, zip->file_info.disk_offset);
Nathan Moinvaziri09725902019-02-20 09:15:48 -08001747
1748 /* Guard against seek overflows */
1749 if ((zip->disk_offset_shift > 0) &&
1750 (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
1751 return MZ_FORMAT_ERROR;
1752
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001753 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 -07001754}
1755
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001756int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001757{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001758 mz_zip *zip = (mz_zip *)handle;
1759 int32_t err = MZ_OK;
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001760 int32_t err_shift = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001761
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001762#if defined(MZ_ZIP_NO_ENCRYPTION)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001763 if (password != NULL)
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001764 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001765#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001766 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001767 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001768 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001769 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001770 if (zip->entry_scanned == 0)
1771 return MZ_PARAM_ERROR;
Nathan Moinvaziri0f09a002018-04-23 18:48:30 -07001772 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) && (!raw))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001773 return MZ_PARAM_ERROR;
1774
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001775 mz_zip_print("Zip - Entry - Read open (raw %"PRId32")\n", raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001776
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001777 err = mz_zip_seek_to_local_header(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001778 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001779 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 -07001780
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001781 if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0)
1782 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001783 /* Perhaps we didn't compensated correctly for incorrect cd offset */
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001784 err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
1785 if (err_shift == MZ_OK)
1786 err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
1787 if (err_shift == MZ_OK)
1788 {
1789 zip->disk_offset_shift = 0;
1790 err = err_shift;
1791 }
1792 }
1793
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001794#ifdef MZ_ZIP_NO_DECOMPRESSION
Nathan Moinvaziri2eb29072018-11-23 10:32:21 -08001795 if (!raw && zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001796 err = MZ_SUPPORT_ERROR;
Viktor Szakatse7215072018-09-04 15:06:53 +00001797#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001798 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001799 err = mz_zip_entry_open_int(handle, raw, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001800
1801 return err;
1802}
1803
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001804int32_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 -07001805{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001806 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001807 int64_t filename_pos = -1;
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001808 int64_t extrafield_pos = 0;
1809 int64_t comment_pos = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001810 int64_t disk_number = 0;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001811 uint8_t is_dir = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001812 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001813
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001814#if defined(MZ_ZIP_NO_ENCRYPTION)
tz-lomb1b25802017-11-10 15:03:02 +03001815 if (password != NULL)
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001816 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001817#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001818 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001819 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001820
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001821 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001822 {
1823 err = mz_zip_entry_close(handle);
1824 if (err != MZ_OK)
1825 return err;
1826 }
1827
1828 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001829
1830 mz_zip_print("Zip - Entry - Write open - %s (level %"PRId16" raw %"PRId8")\n",
1831 zip->file_info.filename, compress_level, raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001832
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001833 mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001834 mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
1835
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001836 /* Copy filename, extrafield, and comment internally */
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001837 filename_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001838 if (file_info->filename != NULL)
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001839 mz_stream_write(zip->file_info_stream, file_info->filename, (int32_t)strlen(file_info->filename));
1840 mz_stream_write_uint8(zip->file_info_stream, 0);
1841
1842 extrafield_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001843 if (file_info->extrafield != NULL)
Nathan Moinvazirica014e62018-08-15 07:31:12 -07001844 mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001845 mz_stream_write_uint8(zip->file_info_stream, 0);
1846
1847 comment_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001848 if (file_info->comment != NULL)
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001849 mz_stream_write(zip->file_info_stream, file_info->comment, file_info->comment_size);
1850 mz_stream_write_uint8(zip->file_info_stream, 0);
1851
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001852 mz_stream_mem_get_buffer_at(zip->file_info_stream, filename_pos, (const void **)&zip->file_info.filename);
1853 mz_stream_mem_get_buffer_at(zip->file_info_stream, extrafield_pos, (const void **)&zip->file_info.extrafield);
1854 mz_stream_mem_get_buffer_at(zip->file_info_stream, comment_pos, (const void **)&zip->file_info.comment);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001855
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001856 if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001857 {
1858 if ((compress_level == 8) || (compress_level == 9))
1859 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
1860 if (compress_level == 2)
1861 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
1862 if (compress_level == 1)
1863 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1864 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001865#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001866 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001867 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001868#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001869
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001870 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
1871 is_dir = 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001872
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001873 if (!is_dir)
1874 {
1875 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
1876 if (password != NULL)
1877 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
1878 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001879
juaniib8887e92018-02-14 00:51:05 -03001880 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1881 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001882
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001883 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001884 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001885 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001886
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001887#ifdef HAVE_WZAES
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001888 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
1889 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
1890#endif
1891
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001892 if ((compress_level == 0) || (is_dir))
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001893 zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
Viktor Szakats2884e672018-04-30 08:12:13 +00001894
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001895#ifdef MZ_ZIP_NO_COMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001896 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001897 err = MZ_SUPPORT_ERROR;
1898#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001899 if (err == MZ_OK)
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001900 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001901 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001902 err = mz_zip_entry_open_int(handle, raw, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001903
1904 return err;
1905}
1906
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001907int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001908{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001909 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001910 int32_t read = 0;
1911
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001912 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001913 return MZ_PARAM_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001914 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) /* zlib limitation */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001915 return MZ_PARAM_ERROR;
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07001916 if (len == 0)
1917 return MZ_PARAM_ERROR;
1918
Nathan Moinvazirieb80cd92018-07-11 16:45:09 -07001919 if (zip->file_info.compressed_size == 0)
1920 return 0;
1921
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001922 /* Read entire entry even if uncompressed_size = 0, otherwise */
1923 /* aes encryption validation will fail if compressed_size > 0 */
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001924 read = mz_stream_read(zip->compress_stream, buf, len);
1925 if (read > 0)
1926 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001927
Nathan Moinvaziri264dc182018-11-13 17:42:13 -08001928 mz_zip_print("Zip - Entry - Read - %"PRId32" (max %"PRId32")\n", read, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001929
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001930 return read;
1931}
1932
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001933int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001934{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001935 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001936 int32_t written = 0;
1937
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001938 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001939 return MZ_PARAM_ERROR;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001940 written = mz_stream_write(zip->compress_stream, buf, len);
1941 if (written > 0)
1942 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001943
Nathan Moinvaziri264dc182018-11-13 17:42:13 -08001944 mz_zip_print("Zip - Entry - Write - %"PRId32" (max %"PRId32")\n", written, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001945
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001946 return written;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001947}
1948
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001949int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size,
1950 int64_t *uncompressed_size)
1951{
1952 mz_zip *zip = (mz_zip *)handle;
1953 int64_t total_in = 0;
1954 int32_t err = MZ_OK;
1955 uint8_t zip64 = 0;
1956
1957 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
1958 return MZ_PARAM_ERROR;
1959
1960 mz_stream_close(zip->compress_stream);
1961
1962 mz_zip_print("Zip - Entry - Read Close\n");
1963
1964 if (crc32 != NULL)
1965 *crc32 = zip->file_info.crc;
1966 if (compressed_size != NULL)
1967 *compressed_size = zip->file_info.compressed_size;
1968 if (uncompressed_size != NULL)
1969 *uncompressed_size = zip->file_info.uncompressed_size;
1970
1971 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
1972
1973 if ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) &&
1974 ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0) &&
1975 (crc32 != NULL || compressed_size != NULL || uncompressed_size != NULL))
1976 {
1977 /* Check to see if data descriptor is zip64 bit format or not */
1978 if (mz_zip_extrafield_contains(zip->local_file_info.extrafield,
1979 zip->local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
1980 zip64 = 1;
1981
1982 err = mz_zip_seek_to_local_header(handle);
1983
1984 /* Seek to end of compressed stream since we might have over-read during compression */
1985 if (err == MZ_OK)
1986 err = mz_stream_seek(zip->stream, MZ_ZIP_SIZE_LD_ITEM +
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001987 (int64_t)zip->local_file_info.filename_size +
1988 (int64_t)zip->local_file_info.extrafield_size +
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001989 total_in, MZ_SEEK_CUR);
1990
1991 /* Read data descriptor */
1992 if (err == MZ_OK)
1993 err = mz_zip_entry_read_descriptor(zip->stream, zip64,
1994 crc32, compressed_size, uncompressed_size);
1995 }
1996
1997 /* If entire entry was not read verification will fail */
1998 if ((err == MZ_OK) && (total_in > 0) && (!zip->entry_raw))
1999 {
2000#ifdef HAVE_WZAES
2001 /* AES zip version AE-1 will expect a valid crc as well */
2002 if (zip->file_info.aes_version <= 0x0001)
2003#endif
2004 {
2005 if (zip->entry_crc32 != zip->file_info.crc)
2006 {
2007 mz_zip_print("Zip - Entry - Crc failed (actual 0x%08"PRIx32" expected 0x%08"PRIx32")\n",
2008 zip->entry_crc32, zip->file_info.crc);
2009
2010 err = MZ_CRC_ERROR;
2011 }
2012 }
2013 }
2014
2015 mz_zip_entry_close_int(handle);
2016
2017 return err;
2018}
2019
2020int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size,
2021 int64_t uncompressed_size)
2022{
2023 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002024 int32_t err = MZ_OK;
2025 uint8_t zip64 = 0;
2026
2027 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2028 return MZ_PARAM_ERROR;
2029
2030 mz_stream_close(zip->compress_stream);
2031
2032 if (!zip->entry_raw)
2033 crc32 = zip->entry_crc32;
2034
2035 mz_zip_print("Zip - Entry - Write Close (crc 0x%08"PRIx32" cs %"PRId64" ucs %"PRId64" )\n",
2036 crc32, compressed_size, uncompressed_size);
2037
2038 /* If sizes are not set, then read them from the compression stream */
2039 if (compressed_size < 0)
2040 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2041 if (uncompressed_size < 0)
Nathan Moinvazirid9318432018-12-01 07:56:28 -08002042 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002043
2044 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
2045 {
2046 mz_stream_set_base(zip->crypt_stream, zip->stream);
2047 err = mz_stream_close(zip->crypt_stream);
2048
2049 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2050 }
2051
2052 if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
2053 {
2054 /* Determine if we need to write data descriptor in zip64 format,
2055 if local extrafield was saved with zip64 extrafield */
2056 if (zip->file_info.zip64 == MZ_ZIP64_AUTO)
2057 {
2058 if (zip->file_info.uncompressed_size >= UINT32_MAX)
2059 zip64 = 1;
2060 if (zip->file_info.compressed_size >= UINT32_MAX)
2061 zip64 = 1;
2062 if (zip->file_info.disk_offset >= UINT32_MAX)
2063 zip64 = 1;
2064 else if (zip->file_info.uncompressed_size == 0)
2065 zip64 = 1;
2066 }
2067 else if (zip->file_info.zip64 == MZ_ZIP64_FORCE)
2068 {
2069 zip64 = 1;
2070 }
2071
2072 if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
2073 err = mz_zip_entry_write_descriptor(zip->stream,
2074 zip64, 0, compressed_size, 0);
2075 else
2076 err = mz_zip_entry_write_descriptor(zip->stream,
2077 zip64, crc32, compressed_size, uncompressed_size);
2078 }
2079
2080 /* Write file info to central directory */
2081
2082 mz_zip_print("Zip - Entry - Write cd (ucs %"PRId64" cs %"PRId64" crc 0x%08"PRIx32")\n",
2083 uncompressed_size, compressed_size, crc32);
2084
2085 zip->file_info.crc = crc32;
2086 zip->file_info.compressed_size = compressed_size;
2087 zip->file_info.uncompressed_size = uncompressed_size;
2088
2089 if (err == MZ_OK)
2090 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
2091
2092 zip->number_entry += 1;
2093
2094 mz_zip_entry_close_int(handle);
2095
2096 return err;
2097}
2098
2099int32_t mz_zip_entry_is_dir(void *handle)
2100{
2101 mz_zip *zip = (mz_zip *)handle;
2102 int32_t filename_length = 0;
2103
2104 if (zip == NULL)
2105 return MZ_PARAM_ERROR;
2106 if (zip->entry_scanned == 0)
2107 return MZ_PARAM_ERROR;
2108 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2109 return MZ_OK;
2110
2111 filename_length = (int32_t)strlen(zip->file_info.filename);
2112 if (filename_length > 0)
2113 {
2114 if ((zip->file_info.filename[filename_length - 1] == '/') ||
2115 (zip->file_info.filename[filename_length - 1] == '\\'))
2116 return MZ_OK;
2117 }
2118 return MZ_EXIST_ERROR;
2119}
2120
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002121int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002122{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002123 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002124
2125 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002126 return MZ_PARAM_ERROR;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002127
2128 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
2129 {
2130 if (!zip->entry_scanned)
2131 return MZ_PARAM_ERROR;
2132 }
2133
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002134 *file_info = &zip->file_info;
2135 return MZ_OK;
2136}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002137
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002138int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002139{
2140 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002141 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002142 return MZ_PARAM_ERROR;
2143 *local_file_info = &zip->local_file_info;
2144 return MZ_OK;
2145}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002146
Nathan Moinvaziri915c5132018-10-26 20:00:52 -07002147int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size)
2148{
2149 mz_zip *zip = (mz_zip *)handle;
2150
2151 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2152 return MZ_PARAM_ERROR;
2153
2154 zip->file_info.extrafield = extrafield;
2155 zip->file_info.extrafield_size = extrafield_size;
2156 return MZ_OK;
2157}
2158
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002159int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07002160{
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002161 return mz_zip_entry_close_raw(handle, UINT64_MAX, 0);
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07002162}
2163
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002164int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002165{
2166 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002167 int32_t err = MZ_OK;
2168
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002169 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002170 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002171
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002172 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002173 err = mz_zip_entry_write_close(handle, crc32, UINT64_MAX, uncompressed_size);
2174 else
2175 err = mz_zip_entry_read_close(handle, NULL, NULL, NULL);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002176
2177 return err;
2178}
2179
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002180static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002181{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002182 mz_zip *zip = (mz_zip *)handle;
2183 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002184
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002185 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002186 return MZ_PARAM_ERROR;
2187
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002188 zip->entry_scanned = 0;
2189
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002190 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00002191
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002192 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002193 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002194 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002195 if (err == MZ_OK)
2196 zip->entry_scanned = 1;
2197 return err;
2198}
2199
Nathan Moinvaziric565fa82018-10-19 08:48:33 -07002200int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry)
2201{
2202 mz_zip *zip = (mz_zip *)handle;
2203 if (zip == NULL)
2204 return MZ_PARAM_ERROR;
2205 zip->number_entry = number_entry;
2206 return MZ_OK;
2207}
2208
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002209int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002210{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002211 mz_zip *zip = (mz_zip *)handle;
2212 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002213 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002214 *number_entry = zip->number_entry;
2215 return MZ_OK;
2216}
2217
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002218int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd)
2219{
2220 mz_zip *zip = (mz_zip *)handle;
2221 if (zip == NULL)
2222 return MZ_PARAM_ERROR;
2223 zip->disk_number_with_cd = disk_number_with_cd;
2224 return MZ_OK;
2225}
2226
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002227int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03002228{
2229 mz_zip *zip = (mz_zip *)handle;
2230 if (zip == NULL || disk_number_with_cd == NULL)
2231 return MZ_PARAM_ERROR;
2232 *disk_number_with_cd = zip->disk_number_with_cd;
2233 return MZ_OK;
2234}
2235
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002236int64_t mz_zip_get_entry(void *handle)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002237{
2238 mz_zip *zip = (mz_zip *)handle;
2239
2240 if (zip == NULL)
2241 return MZ_PARAM_ERROR;
2242
2243 return zip->cd_current_pos;
2244}
2245
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002246int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002247{
2248 mz_zip *zip = (mz_zip *)handle;
2249
2250 if (zip == NULL)
2251 return MZ_PARAM_ERROR;
2252
2253 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
2254 return MZ_PARAM_ERROR;
2255
2256 zip->cd_current_pos = cd_pos;
2257
2258 return mz_zip_goto_next_entry_int(handle);
2259}
2260
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002261int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002262{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002263 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002264
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002265 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002266 return MZ_PARAM_ERROR;
2267
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002268 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002269
2270 return mz_zip_goto_next_entry_int(handle);
2271}
2272
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002273int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002274{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002275 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002276
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002277 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002278 return MZ_PARAM_ERROR;
2279
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002280 zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002281 zip->file_info.extrafield_size + zip->file_info.comment_size;
2282
2283 return mz_zip_goto_next_entry_int(handle);
2284}
2285
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002286int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002287{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002288 mz_zip *zip = (mz_zip *)handle;
2289 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002290 int32_t result = 0;
2291
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002292 if (zip == NULL || filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002293 return MZ_PARAM_ERROR;
2294
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002295 /* If we are already on the current entry, no need to search */
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002296 if ((zip->entry_scanned) && (zip->file_info.filename != NULL))
2297 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002298 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002299 if (result == 0)
2300 return MZ_OK;
2301 }
2302
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002303 /* Search all entries starting at the first */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002304 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002305 while (err == MZ_OK)
2306 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002307 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2308 if (result == 0)
2309 return MZ_OK;
2310
2311 err = mz_zip_goto_next_entry(handle);
2312 }
2313
2314 return err;
2315}
2316
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002317int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002318{
2319 mz_zip *zip = (mz_zip *)handle;
2320 int32_t err = MZ_OK;
2321 int32_t result = 0;
2322
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002323 /* Search first entry looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002324 err = mz_zip_goto_first_entry(handle);
2325 if (err != MZ_OK)
2326 return err;
2327
2328 result = cb(handle, userdata, &zip->file_info);
2329 if (result == 0)
2330 return MZ_OK;
2331
2332 return mz_zip_locate_next_entry(handle, userdata, cb);
2333}
2334
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002335int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002336{
2337 mz_zip *zip = (mz_zip *)handle;
2338 int32_t err = MZ_OK;
2339 int32_t result = 0;
2340
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002341 /* Search next entries looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002342 err = mz_zip_goto_next_entry(handle);
2343 while (err == MZ_OK)
2344 {
2345 result = cb(handle, userdata, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002346 if (result == 0)
2347 return MZ_OK;
2348
2349 err = mz_zip_goto_next_entry(handle);
2350 }
2351
2352 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002353}
2354
2355/***************************************************************************/
2356
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002357int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002358{
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002359 uint32_t posix_attrib = 0;
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002360 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002361 int32_t err = MZ_OK;
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002362
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002363 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2364 if (err == MZ_OK)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002365 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002366 if ((posix_attrib & 0170000) == 0040000) /* S_ISDIR */
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002367 return MZ_OK;
2368 }
2369
2370 return MZ_EXIST_ERROR;
2371}
2372
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002373int32_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 -07002374{
2375 if (target_attrib == NULL)
2376 return MZ_PARAM_ERROR;
2377
2378 *target_attrib = 0;
2379
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002380 if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002381 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002382 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002383 {
2384 *target_attrib = src_attrib;
2385 return MZ_OK;
2386 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002387 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002388 return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
2389 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002390 else if ((src_sys == MZ_HOST_SYSTEM_UNIX) || (src_sys == MZ_HOST_SYSTEM_OSX_DARWIN))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002391 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002392 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002393 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002394 /* If high bytes are set, it contains unix specific attributes */
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002395 if ((src_attrib >> 16) != 0)
2396 src_attrib >>= 16;
2397
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002398 *target_attrib = src_attrib;
2399 return MZ_OK;
2400 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002401 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002402 return mz_zip_attrib_posix_to_win32(src_attrib, target_attrib);
2403 }
2404
2405 return MZ_SUPPORT_ERROR;
2406}
2407
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002408int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002409{
2410 if (win32_attrib == NULL)
2411 return MZ_PARAM_ERROR;
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002412
2413 *win32_attrib = 0;
2414
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002415 /* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002416 if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002417 *win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
2418 /* S_IFDIR */
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002419 if ((posix_attrib & 0170000) == 0040000)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002420 *win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
2421 /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002422 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002423 *win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002424
2425 return MZ_OK;
2426}
2427
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002428int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002429{
2430 if (posix_attrib == NULL)
2431 return MZ_PARAM_ERROR;
2432
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002433 *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
2434 /* FILE_ATTRIBUTE_READONLY */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002435 if ((win32_attrib & 0x01) == 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002436 *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
2437 /* FILE_ATTRIBUTE_DIRECTORY */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002438 if ((win32_attrib & 0x10) == 0x10)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002439 *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002440 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002441 *posix_attrib |= 0100000; /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002442
2443 return MZ_OK;
2444}
2445
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002446/***************************************************************************/
2447
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002448int32_t mz_zip_extrafield_find(void *stream, uint16_t type, uint16_t *length)
2449{
2450 int32_t err = MZ_OK;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002451 uint16_t field_type = 0;
2452 uint16_t field_length = 0;
2453
2454 do
2455 {
2456 err = mz_stream_read_uint16(stream, &field_type);
2457 if (err == MZ_OK)
2458 err = mz_stream_read_uint16(stream, &field_length);
2459 if (err != MZ_OK)
2460 break;
2461
2462 if (type == field_type)
2463 {
2464 if (length != NULL)
2465 *length = field_length;
2466 return MZ_OK;
2467 }
2468
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002469 err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR);
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002470 }
2471 while (err == MZ_OK);
2472
2473 return MZ_EXIST_ERROR;
2474}
2475
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002476int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size,
2477 uint16_t type, uint16_t *length)
2478{
2479 void *file_extra_stream = NULL;
2480 int32_t err = MZ_OK;
2481
2482 if (extrafield == NULL || extrafield_size == 0)
2483 return MZ_PARAM_ERROR;
2484
2485 mz_stream_mem_create(&file_extra_stream);
2486 mz_stream_mem_set_buffer(file_extra_stream, (void *)extrafield, extrafield_size);
2487
2488 err = mz_zip_extrafield_find(file_extra_stream, type, length);
2489
2490 mz_stream_mem_delete(&file_extra_stream);
2491
2492 return err;
2493}
2494
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002495int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length)
2496{
2497 int32_t err = MZ_OK;
2498 if (type == NULL || length == NULL)
2499 return MZ_PARAM_ERROR;
2500 err = mz_stream_read_uint16(stream, type);
2501 if (err == MZ_OK)
2502 err = mz_stream_read_uint16(stream, length);
2503 return err;
2504}
2505
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002506int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002507{
2508 int32_t err = MZ_OK;
2509 err = mz_stream_write_uint16(stream, type);
2510 if (err == MZ_OK)
2511 err = mz_stream_write_uint16(stream, length);
2512 return err;
2513}
2514
2515/***************************************************************************/
2516
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002517static int32_t mz_zip_invalid_date(const struct tm *ptm)
2518{
2519#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002520 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002521 !datevalue_in_range(0, 11, ptm->tm_mon) ||
2522 !datevalue_in_range(1, 31, ptm->tm_mday) ||
2523 !datevalue_in_range(0, 23, ptm->tm_hour) ||
2524 !datevalue_in_range(0, 59, ptm->tm_min) ||
2525 !datevalue_in_range(0, 59, ptm->tm_sec));
2526#undef datevalue_in_range
2527}
2528
2529static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
2530{
2531 uint64_t date = (uint64_t)(dos_date >> 16);
2532
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002533 ptm->tm_mday = (uint16_t)(date & 0x1f);
2534 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
2535 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
2536 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
2537 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
2538 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002539 ptm->tm_isdst = -1;
2540}
2541
2542int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
2543{
2544 if (ptm == NULL)
2545 return MZ_PARAM_ERROR;
2546
2547 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
2548
2549 if (mz_zip_invalid_date(ptm))
2550 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002551 /* Invalid date stored, so don't return it */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002552 memset(ptm, 0, sizeof(struct tm));
2553 return MZ_FORMAT_ERROR;
2554 }
2555 return MZ_OK;
2556}
2557
2558time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
2559{
2560 struct tm ptm;
2561 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
2562 return mktime(&ptm);
2563}
2564
2565int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
2566{
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002567 struct tm *ltm = NULL;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002568 if (ptm == NULL)
2569 return MZ_PARAM_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002570 ltm = localtime(&unix_time); /* Returns a 1900-based year */
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002571 if (ltm == NULL)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002572 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002573 /* Invalid date stored, so don't return it */
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002574 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002575 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002576 }
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002577 memcpy(ptm, ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002578 return MZ_OK;
2579}
2580
2581uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
2582{
2583 struct tm ptm;
2584 mz_zip_time_t_to_tm(unix_time, &ptm);
2585 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
2586}
2587
2588uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
2589{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07002590 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002591
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002592 /* Years supported: */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002593
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002594 /* [00, 79] (assumed to be between 2000 and 2079) */
2595 /* [80, 207] (assumed to be between 1980 and 2107, typical output of old */
2596 /* software that does 'year-1900' to get a double digit year) */
2597 /* [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.) */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002598
2599 memcpy(&fixed_tm, ptm, sizeof(struct tm));
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002600 if (fixed_tm.tm_year >= 1980) /* range [1980, 2107] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002601 fixed_tm.tm_year -= 1980;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002602 else if (fixed_tm.tm_year >= 80) /* range [80, 207] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002603 fixed_tm.tm_year -= 80;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002604 else /* range [00, 79] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002605 fixed_tm.tm_year += 20;
2606
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002607 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002608 return 0;
2609
Anand K Mistry57b65f82018-11-09 10:52:19 +11002610 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 -07002611 (((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 -07002612}
2613
2614int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
2615{
2616 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
2617 return MZ_OK;
2618}
2619
2620int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
2621{
2622 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
2623 return MZ_OK;
2624}
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002625
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002626/***************************************************************************/
2627
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002628int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case)
2629{
2630 do
2631 {
2632 if ((*path1 == '\\' && *path2 == '/') ||
2633 (*path2 == '\\' && *path1 == '/'))
2634 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002635 /* Ignore comparison of path slashes */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002636 }
2637 else if (ignore_case)
2638 {
2639 if (tolower(*path1) != tolower(*path2))
2640 break;
2641 }
2642 else if (*path1 != *path2)
2643 {
2644 break;
2645 }
2646
2647 path1 += 1;
2648 path2 += 1;
2649 }
2650 while (*path1 != 0 && *path2 != 0);
2651
2652 if (ignore_case)
2653 return (int32_t)(tolower(*path1) - tolower(*path2));
2654
2655 return (int32_t)(*path1 - *path2);
2656}
2657
2658/***************************************************************************/