blob: a7d7ad5f8828110dd139a7a3d5fd2ebb48b7704f [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvaziri10ac2912019-09-18 16:55:43 -07002 Version 2.9.0, September 18, 2019
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08003 part of the MiniZip project
4
Nathan Moinvaziri2ca7f392019-01-08 16:07:10 -08005 Copyright (C) 2010-2019 Nathan Moinvaziri
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08006 https://github.com/nmoinvaz/minizip
Nathan Moinvaziri79cfab02018-08-17 12:21:06 -07007 Copyright (C) 2009-2010 Mathias Svensson
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08008 Modifications for Zip64 support
9 http://result42.com
Nathan Moinvaziri79cfab02018-08-17 12:21:06 -070010 Copyright (C) 2007-2008 Even Rouault
11 Modifications of Unzip for Zip64
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080012 Copyright (C) 1998-2010 Gilles Vollant
Viktor Szakats9dea6f22018-10-17 22:39:01 +000013 https://www.winimage.com/zLibDll/minizip.html
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080014
15 This program is distributed under the terms of the same license as zlib.
16 See the accompanying LICENSE file for the full text of the license.
17*/
18
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080019
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070020#include "mz.h"
Nathan Moinvaziri5f091882018-10-24 18:06:08 -070021#include "mz_crypt.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080022#include "mz_strm.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080023#ifdef HAVE_BZIP2
24# include "mz_strm_bzip.h"
25#endif
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -080026#ifdef HAVE_LIBCOMP
Nathan Moinvaziri90d31c72018-11-20 03:17:20 -080027# include "mz_strm_libcomp.h"
28#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080029#ifdef HAVE_LZMA
30# include "mz_strm_lzma.h"
31#endif
Nathan Moinvazirib47b41b2018-07-11 09:44:29 -070032#include "mz_strm_mem.h"
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -070033#ifdef HAVE_PKCRYPT
34# include "mz_strm_pkcrypt.h"
35#endif
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -080036#ifdef HAVE_WZAES
Nathan Moinvaziri21a31022018-10-24 09:50:16 -070037# include "mz_strm_wzaes.h"
38#endif
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -080039#ifdef HAVE_ZLIB
40# include "mz_strm_zlib.h"
41#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080042
43#include "mz_zip.h"
44
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080045#include <ctype.h> /* tolower */
Nathan Moinvazirie8496272018-11-21 15:05:58 -080046#include <stdio.h> /* snprintf */
Nathan Moinvaziri1ee609b2018-11-19 21:34:35 -080047
zedcf076662019-05-15 21:43:56 +030048#if defined(_MSC_VER) || defined(__MINGW32__)
Nathan Moinvaziri2e0a20a2019-04-28 13:03:11 -070049# define localtime_r(t1,t2) (localtime_s(t2,t1) == 0 ? t1 : NULL)
zed65447dd2019-05-16 22:19:08 +030050#endif
51#if defined(_MSC_VER) && (_MSC_VER < 1900)
52# define snprintf _snprintf
Nathan Moinvaziric565fa82018-10-19 08:48:33 -070053#endif
54
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080055/***************************************************************************/
56
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070057#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
Nathan Moinvaziribe483d82019-10-19 17:30:23 -070058#define MZ_ZIP_MAGIC_LOCALHEADERU8 { 0x50, 0x4b, 0x03, 0x04 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070059#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
Nathan Moinvaziri545d7292019-10-21 15:32:18 -070060#define MZ_ZIP_MAGIC_CENTRALHEADERU8 { 0x50, 0x4b, 0x01, 0x02 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070061#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080062#define MZ_ZIP_MAGIC_ENDHEADERU8 { 0x50, 0x4b, 0x05, 0x06 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070063#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
64#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
65#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080066#define MZ_ZIP_MAGIC_DATADESCRIPTORU8 { 0x50, 0x4b, 0x07, 0x08 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070067
Nathan Moinvaziriba1db162018-11-23 10:07:35 -080068#define MZ_ZIP_SIZE_LD_ITEM (30)
Nathan Moinvaziri638f31f2018-08-27 08:17:16 -070069#define MZ_ZIP_SIZE_CD_ITEM (46)
70#define MZ_ZIP_SIZE_CD_LOCATOR64 (20)
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -080071#define MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR (24)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080072
Nathan Moinvaziric78c7822018-12-01 20:42:36 -080073#ifndef MZ_ZIP_EOCD_MAX_BACK
74#define MZ_ZIP_EOCD_MAX_BACK (1 << 20)
75#endif
76
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080077/***************************************************************************/
78
79typedef struct mz_zip_s
80{
81 mz_zip_file file_info;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070082 mz_zip_file local_file_info;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080083
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080084 void *stream; /* main stream */
85 void *cd_stream; /* pointer to the stream with the cd */
86 void *cd_mem_stream; /* memory stream for central directory */
87 void *compress_stream; /* compression stream */
88 void *crypt_stream; /* encryption stream */
89 void *file_info_stream; /* memory stream for storing file info */
90 void *local_file_info_stream; /* memory stream for storing local file info */
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080091
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070092 int32_t open_mode;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -080093 uint8_t recover;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070094
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080095 uint32_t disk_number_with_cd; /* number of the disk with the central dir */
96 int64_t disk_offset_shift; /* correction for zips that have wrong offset start of cd */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070097
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080098 int64_t cd_start_pos; /* pos of the first file in the central dir stream */
99 int64_t cd_current_pos; /* pos of the current file in the central dir */
100 int64_t cd_offset; /* offset of start of central directory */
101 int64_t cd_size; /* size of the central directory */
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -0700102 uint32_t cd_signature; /* signature of central directory */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700103
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800104 uint8_t entry_scanned; /* entry header information read ok */
105 uint8_t entry_opened; /* entry is open for read/write */
106 uint8_t entry_raw; /* entry opened with raw mode */
107 uint32_t entry_crc32; /* entry crc32 */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700108
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700109 uint64_t number_entry;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700110
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700111 uint16_t version_madeby;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700112 char *comment;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800113} mz_zip;
114
115/***************************************************************************/
116
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800117#if 0
118# define mz_zip_print printf
119#else
120# define mz_zip_print(fmt,...)
121#endif
122
123/***************************************************************************/
124
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800125/* Locate the end of central directory */
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700126static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800127{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700128 int64_t file_size = 0;
Nathan Moinvaziric78c7822018-12-01 20:42:36 -0800129 int64_t max_back = MZ_ZIP_EOCD_MAX_BACK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800130 uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8;
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700131 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700132
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700133 err = mz_stream_seek(stream, 0, MZ_SEEK_END);
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800134 if (err != MZ_OK)
135 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800136
137 file_size = mz_stream_tell(stream);
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800138
Nathan Moinvaziric78c7822018-12-01 20:42:36 -0800139 if (max_back <= 0 || max_back > file_size)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800140 max_back = file_size;
141
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800142 return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800143}
144
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800145/* Locate the end of central directory 64 of a zip file */
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700146static 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 +0800147{
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700148 int64_t offset = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800149 uint32_t value32 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700150 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700151
152
153 *central_pos = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800154
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800155 /* Zip64 end of central directory locator */
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700156 err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800157 /* Read locator signature */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700158 if (err == MZ_OK)
159 {
160 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700161 if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700162 err = MZ_FORMAT_ERROR;
163 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800164 /* Number of the disk with the start of the zip64 end of central directory */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700165 if (err == MZ_OK)
166 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800167 /* Relative offset of the zip64 end of central directory record8 */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700168 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700169 err = mz_stream_read_uint64(stream, (uint64_t *)&offset);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800170 /* Total number of disks */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700171 if (err == MZ_OK)
172 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800173 /* Goto end of central directory record */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700174 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700175 err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800176 /* The signature */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700177 if (err == MZ_OK)
178 {
179 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700180 if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700181 err = MZ_FORMAT_ERROR;
182 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800183
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700184 if (err == MZ_OK)
185 *central_pos = offset;
186
187 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800188}
189
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800190/* Get info about the current file in the zip file */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700191static 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 -0700192{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700193 uint64_t ntfs_time = 0;
194 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700195 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700196 uint32_t dos_date = 0;
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800197 uint32_t field_pos = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700198 uint16_t field_type = 0;
199 uint16_t field_length = 0;
200 uint32_t field_length_read = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700201 uint16_t ntfs_attrib_id = 0;
202 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700203 uint16_t linkname_size;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700204 uint16_t value16 = 0;
205 uint32_t value32 = 0;
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800206 int64_t extrafield_pos = 0;
207 int64_t comment_pos = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700208 int64_t linkname_pos = 0;
209 int64_t saved_pos = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700210 int32_t err = MZ_OK;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700211 char *linkname = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700212
213
214 memset(file_info, 0, sizeof(mz_zip_file));
215
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800216 /* Check the magic */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700217 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700218 if (err == MZ_END_OF_STREAM)
219 err = MZ_END_OF_LIST;
220 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700221 err = MZ_END_OF_LIST;
222 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
223 err = MZ_FORMAT_ERROR;
224 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
225 err = MZ_FORMAT_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000226
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800227 /* Read header fields */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700228 if (err == MZ_OK)
229 {
230 if (!local)
231 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
232 if (err == MZ_OK)
233 err = mz_stream_read_uint16(stream, &file_info->version_needed);
234 if (err == MZ_OK)
235 err = mz_stream_read_uint16(stream, &file_info->flag);
236 if (err == MZ_OK)
237 err = mz_stream_read_uint16(stream, &file_info->compression_method);
238 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700239 {
240 err = mz_stream_read_uint32(stream, &dos_date);
241 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
242 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700243 if (err == MZ_OK)
244 err = mz_stream_read_uint32(stream, &file_info->crc);
245 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700246 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700247 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700248 file_info->compressed_size = value32;
249 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700250 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700251 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700252 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700253 file_info->uncompressed_size = value32;
254 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700255 if (err == MZ_OK)
256 err = mz_stream_read_uint16(stream, &file_info->filename_size);
257 if (err == MZ_OK)
258 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
259 if (!local)
260 {
261 if (err == MZ_OK)
262 err = mz_stream_read_uint16(stream, &file_info->comment_size);
263 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700264 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700265 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700266 file_info->disk_number = value16;
267 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700268 if (err == MZ_OK)
269 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
270 if (err == MZ_OK)
271 err = mz_stream_read_uint32(stream, &file_info->external_fa);
272 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700273 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700274 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700275 file_info->disk_offset = value32;
276 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700277 }
278 }
279
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700280 if (err == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800281 err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700282
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800283 /* Copy variable length data to memory stream for later retrieval */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700284 if ((err == MZ_OK) && (file_info->filename_size > 0))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700285 err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800286 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800287 extrafield_pos = mz_stream_tell(file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700288
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800289 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
290 err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800291 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800292
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800293 comment_pos = mz_stream_tell(file_extra_stream);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800294 if ((err == MZ_OK) && (file_info->comment_size > 0))
295 err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800296 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700297
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700298 linkname_pos = mz_stream_tell(file_extra_stream);
299 /* Overwrite if we encounter UNIX1 extra block */
300 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700301
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700302 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
303 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800304 /* Seek to and parse the extra field */
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800305 err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700306
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800307 while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700308 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700309 err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length);
Nathan Moinvaziriea2bd792018-11-19 17:44:44 -0800310 if (err != MZ_OK)
311 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800312 field_pos += 4;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800313
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800314 /* Don't allow field length to exceed size of remaining extrafield */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800315 if (field_length > (file_info->extrafield_size - field_pos))
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -0800316 field_length = (uint16_t)(file_info->extrafield_size - field_pos);
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800317
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800318 /* Read ZIP64 extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800319 if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700320 {
321 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800322 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700323 err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800324 if (file_info->uncompressed_size < 0)
325 err = MZ_FORMAT_ERROR;
326 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700327 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800328 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700329 err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800330 if (file_info->compressed_size < 0)
331 err = MZ_FORMAT_ERROR;
332 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700333 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800334 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700335 err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800336 if (file_info->disk_offset < 0)
337 err = MZ_FORMAT_ERROR;
338 }
juanii4ae79922018-02-11 14:29:36 -0300339 if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700340 err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700341 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800342 /* Read NTFS extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800343 else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700344 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700345 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700346 err = mz_stream_read_uint32(file_extra_stream, &reserved);
347 field_length_read = 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700348
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800349 while ((err == MZ_OK) && (field_length_read + 4 <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700350 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700351 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700352 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700353 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800354 field_length_read += 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700355
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700356 if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700357 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700358 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700359 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
360
juanii7063b0e2018-02-11 13:56:21 -0300361 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700362 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700363 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700364 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
365 }
juanii7063b0e2018-02-11 13:56:21 -0300366 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700367 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700368 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700369 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
370 }
371 }
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800372 else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700373 {
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800374 err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700375 }
376
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800377 field_length_read += ntfs_attrib_size;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700378 }
379 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800380 /* Read UNIX1 extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800381 else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700382 {
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700383 if (err == MZ_OK)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700384 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700385 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700386 if (err == MZ_OK && file_info->accessed_date == 0)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700387 file_info->accessed_date = value32;
388 }
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700389 if (err == MZ_OK)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700390 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700391 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700392 if (err == MZ_OK && file_info->modified_date == 0)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700393 file_info->modified_date = value32;
394 }
395 if (err == MZ_OK)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800396 err = mz_stream_read_uint16(file_extra_stream, &value16); /* User id */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700397 if (err == MZ_OK)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800398 err = mz_stream_read_uint16(file_extra_stream, &value16); /* Group id */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700399
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700400 /* Copy linkname to end of file extra stream so we can return null
401 terminated string */
402 linkname_size = field_length - 12;
403 if ((err == MZ_OK) && (linkname_size > 0))
404 {
405 linkname = (char *)MZ_ALLOC(linkname_size);
406 if (linkname != NULL)
407 {
408 if (mz_stream_read(file_extra_stream, linkname, linkname_size) != linkname_size)
409 err = MZ_READ_ERROR;
410 if (err == MZ_OK)
411 {
412 saved_pos = mz_stream_tell(file_extra_stream);
413
414 mz_stream_seek(file_extra_stream, linkname_pos, MZ_SEEK_SET);
415 mz_stream_write(file_extra_stream, linkname, linkname_size);
416 mz_stream_write_uint8(file_extra_stream, 0);
417
418 mz_stream_seek(file_extra_stream, saved_pos, MZ_SEEK_SET);
419 }
420 MZ_FREE(linkname);
421 }
422 }
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700423 }
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800424#ifdef HAVE_WZAES
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800425 /* Read AES extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800426 else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700427 {
428 uint8_t value8 = 0;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800429 /* Verify version info */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700430 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800431 /* Support AE-1 and AE-2 */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700432 if (value16 != 1 && value16 != 2)
433 err = MZ_FORMAT_ERROR;
434 file_info->aes_version = value16;
435 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700436 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700437 if ((char)value8 != 'A')
438 err = MZ_FORMAT_ERROR;
439 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700440 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700441 if ((char)value8 != 'E')
442 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800443 /* Get AES encryption strength and actual compression method */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700444 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700445 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700446 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700447 file_info->aes_encryption_mode = value8;
448 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700449 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700450 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700451 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700452 file_info->compression_method = value16;
453 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700454 }
455#endif
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800456 else if (field_length > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700457 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700458 err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700459 }
460
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800461 field_pos += field_length;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700462 }
463 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700464
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800465 /* Get pointers to variable length data */
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800466 mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename);
Nathan Moinvazirif9e34482018-11-27 09:45:09 -0800467 mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield);
468 mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700469 mz_stream_mem_get_buffer_at(file_extra_stream, linkname_pos, (const void **)&file_info->linkname);
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800470
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800471 /* Set to empty string just in-case */
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800472 if (file_info->filename == NULL)
473 file_info->filename = "";
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800474 if (file_info->extrafield == NULL)
475 file_info->extrafield_size = 0;
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800476 if (file_info->comment == NULL)
477 file_info->comment = "";
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700478 if (file_info->linkname == NULL)
479 file_info->linkname = "";
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700480
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800481 if (err == MZ_OK)
482 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700483 mz_zip_print("Zip - Entry - Read header - %s (local %" PRId8 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800484 file_info->filename, local);
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700485 mz_zip_print("Zip - Entry - Read header compress (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800486 file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
487 if (!local)
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800488 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700489 mz_zip_print("Zip - Entry - Read header disk (disk %" PRIu32 " offset %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800490 file_info->disk_number, file_info->disk_offset);
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800491 }
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700492 mz_zip_print("Zip - Entry - Read header variable (fnl %" PRId32 " efs %" PRId32 " cms %" PRId32 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800493 file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
494 }
495
496 return err;
497}
498
499static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size)
500{
501 uint32_t value32 = 0;
502 int64_t value64 = 0;
503 int32_t err = MZ_OK;
504
505
506 err = mz_stream_read_uint32(stream, &value32);
507 if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR)
508 err = MZ_FORMAT_ERROR;
509 if (err == MZ_OK)
510 err = mz_stream_read_uint32(stream, &value32);
511 if ((err == MZ_OK) && (crc32 != NULL))
512 *crc32 = value32;
513 if (err == MZ_OK)
514 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800515 /* If zip 64 extension is enabled then read as 8 byte */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800516 if (!zip64)
517 {
518 err = mz_stream_read_uint32(stream, &value32);
519 value64 = value32;
520 }
521 else
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800522 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800523 err = mz_stream_read_int64(stream, &value64);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800524 if (value64 < 0)
525 err = MZ_FORMAT_ERROR;
526 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800527 if ((err == MZ_OK) && (compressed_size != NULL))
528 *compressed_size = value64;
529 }
530 if (err == MZ_OK)
531 {
532 if (!zip64)
533 {
534 err = mz_stream_read_uint32(stream, &value32);
535 value64 = value32;
536 }
537 else
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800538 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800539 err = mz_stream_read_int64(stream, &value64);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800540 if (value64 < 0)
541 err = MZ_FORMAT_ERROR;
542 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800543 if ((err == MZ_OK) && (uncompressed_size != NULL))
544 *uncompressed_size = value64;
545 }
546
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700547 return err;
548}
549
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700550static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700551{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700552 uint64_t ntfs_time = 0;
553 uint32_t reserved = 0;
554 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700555 uint16_t extrafield_size = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700556 uint16_t field_type = 0;
557 uint16_t field_length = 0;
558 uint16_t field_length_zip64 = 0;
559 uint16_t field_length_ntfs = 0;
560 uint16_t field_length_aes = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700561 uint16_t field_length_unix1 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700562 uint16_t filename_size = 0;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700563 uint16_t filename_length = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700564 uint16_t linkname_size = 0;
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700565 uint16_t version_needed = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800566 int32_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700567 int32_t err = MZ_OK;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700568 int32_t err_mem = MZ_OK;
569 uint8_t zip64 = 0;
570 uint8_t skip_aes = 0;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700571 uint8_t mask = 0;
572 uint8_t write_end_slash = 0;
573 const char *filename = NULL;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700574 char masked_name[64];
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700575 void *file_extra_stream = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700576
577 if (file_info == NULL)
578 return MZ_PARAM_ERROR;
579
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700580 if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO))
581 mask = 1;
582
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800583 /* Calculate extra field sizes */
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700584 if (file_info->uncompressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700585 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700586 if (file_info->compressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700587 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700588 if (file_info->disk_offset >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700589 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700590
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700591 if (file_info->zip64 == MZ_ZIP64_AUTO)
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700592 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800593 /* If uncompressed size is unknown, assume zip64 for 64-bit data descriptors */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700594 zip64 = (local && file_info->uncompressed_size == 0) || (field_length_zip64 > 0);
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700595 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700596 else if (file_info->zip64 == MZ_ZIP64_FORCE)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700597 {
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700598 zip64 = 1;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700599 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700600 else if (file_info->zip64 == MZ_ZIP64_DISABLE)
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700601 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800602 /* Zip64 extension is required to zip file */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700603 if (field_length_zip64 > 0)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700604 return MZ_PARAM_ERROR;
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700605 }
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700606
607 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700608 {
609 extrafield_size += 4;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700610 extrafield_size += field_length_zip64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700611 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700612
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800613 /* Calculate extra field size and check for duplicates */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700614 if (file_info->extrafield_size > 0)
615 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700616 mz_stream_mem_create(&file_extra_stream);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700617 mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
Nathan Moinvaziri915c5132018-10-26 20:00:52 -0700618 file_info->extrafield_size);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700619
620 do
621 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700622 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700623 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700624 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700625 if (err_mem != MZ_OK)
626 break;
627
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800628 /* Prefer incoming aes extensions over ours */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700629 if (field_type == MZ_ZIP_EXTENSION_AES)
630 skip_aes = 1;
631
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700632 /* Prefer our zip64, ntfs, unix1 extension over incoming */
633 if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS &&
634 field_type != MZ_ZIP_EXTENSION_UNIX1)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700635 extrafield_size += 4 + field_length;
636
637 if (err_mem == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800638 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700639 }
640 while (err_mem == MZ_OK);
641 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700642
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800643#ifdef HAVE_WZAES
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700644 if (!skip_aes)
645 {
646 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700647 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700648 field_length_aes = 1 + 1 + 1 + 2 + 2;
649 extrafield_size += 4 + field_length_aes;
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700650 }
651 }
Nathan Moinvaziria2bb08c2018-10-28 13:45:30 -0700652#else
Nathan Moinvaziri7de94cf2018-11-19 20:51:06 -0800653 MZ_UNUSED(field_length_aes);
Nathan Moinvaziri4ae469a2018-10-28 15:32:04 -0700654 MZ_UNUSED(skip_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700655#endif
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800656 /* NTFS timestamps */
Nathan Moinvazirid9e159a2018-02-13 15:30:30 -0800657 if ((file_info->modified_date != 0) &&
658 (file_info->accessed_date != 0) &&
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700659 (file_info->creation_date != 0) && (!mask))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700660 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700661 field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
662 extrafield_size += 4 + field_length_ntfs;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700663 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700664
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700665 /* Unix1 symbolic links */
666 if (file_info->linkname != NULL && *file_info->linkname != 0)
667 {
668 linkname_size = (uint16_t)strlen(file_info->linkname);
669 field_length_unix1 = 12 + linkname_size;
670 extrafield_size += 4 + field_length_unix1;
671 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -0700672
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700673 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700674 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700675 else
676 {
677 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
678 if (err == MZ_OK)
679 err = mz_stream_write_uint16(stream, file_info->version_madeby);
680 }
681
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800682 /* Calculate version needed to extract */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700683 if (err == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700684 {
685 version_needed = file_info->version_needed;
686 if (version_needed == 0)
687 {
688 version_needed = 20;
689 if (zip64)
690 version_needed = 45;
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800691#ifdef HAVE_WZAES
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700692 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
693 version_needed = 51;
694#endif
695#ifdef HAVE_LZMA
696 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
697 version_needed = 63;
698#endif
699 }
700 err = mz_stream_write_uint16(stream, version_needed);
701 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700702 if (err == MZ_OK)
703 err = mz_stream_write_uint16(stream, file_info->flag);
704 if (err == MZ_OK)
705 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800706#ifdef HAVE_WZAES
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700707 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700708 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700709 else
710#endif
711 err = mz_stream_write_uint16(stream, file_info->compression_method);
712 }
713 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700714 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700715 if (file_info->modified_date != 0 && !mask)
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -0700716 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700717 err = mz_stream_write_uint32(stream, dos_date);
718 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700719
720 if (err == MZ_OK)
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700721 {
722 if (mask)
723 err = mz_stream_write_uint32(stream, 0);
724 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800725 err = mz_stream_write_uint32(stream, file_info->crc); /* crc */
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700726 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700727 if (err == MZ_OK)
728 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800729 if (file_info->compressed_size >= UINT32_MAX) /* compr size */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700730 err = mz_stream_write_uint32(stream, UINT32_MAX);
731 else
732 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
733 }
734 if (err == MZ_OK)
735 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800736 if (file_info->uncompressed_size >= UINT32_MAX) /* uncompr size */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700737 err = mz_stream_write_uint32(stream, UINT32_MAX);
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700738 else if (mask)
739 err = mz_stream_write_uint32(stream, 0);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700740 else
741 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
742 }
743
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700744 if (mask)
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700745 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -0700746 snprintf(masked_name, sizeof(masked_name), "%" PRIx32 "_%" PRIx64,
Nathan Moinvaziri264dc182018-11-13 17:42:13 -0800747 file_info->disk_number, file_info->disk_offset);
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700748 filename = masked_name;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700749 }
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700750 else
751 {
752 filename = file_info->filename;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700753 }
754
755 filename_length = (uint16_t)strlen(filename);
756 filename_size += filename_length;
757
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800758 if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) &&
759 ((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\')))
760 {
761 filename_size += 1;
762 write_end_slash = 1;
763 }
764
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700765 if (err == MZ_OK)
766 err = mz_stream_write_uint16(stream, filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700767 if (err == MZ_OK)
768 err = mz_stream_write_uint16(stream, extrafield_size);
769
770 if (!local)
771 {
772 if (file_info->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800773 {
774 comment_size = (int32_t)strlen(file_info->comment);
775 if (comment_size > UINT16_MAX)
776 comment_size = UINT16_MAX;
777 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700778 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800779 err = mz_stream_write_uint16(stream, (uint16_t)comment_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700780 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700781 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700782 if (err == MZ_OK)
783 err = mz_stream_write_uint16(stream, file_info->internal_fa);
784 if (err == MZ_OK)
785 err = mz_stream_write_uint32(stream, file_info->external_fa);
786 if (err == MZ_OK)
787 {
788 if (file_info->disk_offset >= UINT32_MAX)
789 err = mz_stream_write_uint32(stream, UINT32_MAX);
790 else
791 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
792 }
793 }
794
795 if (err == MZ_OK)
796 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700797 if (mz_stream_write(stream, filename, filename_length) != filename_length)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700798 err = MZ_WRITE_ERROR;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700799
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800800 /* Ensure that directories have a slash appended to them for compatibility */
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700801 if (err == MZ_OK && write_end_slash)
802 err = mz_stream_write_uint8(stream, '/');
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700803 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700804
805 if (file_info->extrafield_size > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700806 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800807 err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700808 while (err == MZ_OK && err_mem == MZ_OK)
809 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700810 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700811 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700812 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700813 if (err_mem != MZ_OK)
814 break;
815
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700816 /* Prefer our zip 64, ntfs, unix1 extensions over incoming */
817 if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS ||
818 field_type == MZ_ZIP_EXTENSION_UNIX1)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700819 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800820 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700821 continue;
822 }
823
824 err = mz_stream_write_uint16(stream, field_type);
825 if (err == MZ_OK)
826 err = mz_stream_write_uint16(stream, field_length);
827 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700828 err = mz_stream_copy(stream, file_extra_stream, field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700829 }
830
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700831 mz_stream_mem_delete(&file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700832 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700833
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800834 /* Write ZIP64 extra field */
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700835 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700836 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700837 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700838 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700839 {
840 if (mask)
841 err = mz_stream_write_int64(stream, 0);
842 else
843 err = mz_stream_write_int64(stream, file_info->uncompressed_size);
844 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700845 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700846 err = mz_stream_write_int64(stream, file_info->compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700847 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700848 err = mz_stream_write_int64(stream, file_info->disk_offset);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700849 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800850 /* Write NTFS extra field */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700851 if ((err == MZ_OK) && (field_length_ntfs > 0))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700852 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700853 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700854 if (err == MZ_OK)
855 err = mz_stream_write_uint32(stream, reserved);
856 if (err == MZ_OK)
857 err = mz_stream_write_uint16(stream, 0x01);
858 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700859 err = mz_stream_write_uint16(stream, field_length_ntfs - 8);
juanii3679a3d2018-02-11 13:55:38 -0300860 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700861 {
862 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
863 err = mz_stream_write_uint64(stream, ntfs_time);
864 }
juanii3679a3d2018-02-11 13:55:38 -0300865 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700866 {
867 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
868 err = mz_stream_write_uint64(stream, ntfs_time);
869 }
juanii3679a3d2018-02-11 13:55:38 -0300870 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700871 {
872 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
873 err = mz_stream_write_uint64(stream, ntfs_time);
874 }
875 }
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700876 /* Write UNIX extra block extra field */
877 if ((err == MZ_OK) && (field_length_unix1 > 0))
878 {
879 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_UNIX1, field_length_unix1);
880 if (err == MZ_OK)
Nathan Moinvaziriaeb6cdd2019-05-06 13:40:32 -0700881 err = mz_stream_write_uint32(stream, (uint32_t)file_info->accessed_date);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700882 if (err == MZ_OK)
Nathan Moinvaziriaeb6cdd2019-05-06 13:40:32 -0700883 err = mz_stream_write_uint32(stream, (uint32_t)file_info->modified_date);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -0700884 if (err == MZ_OK) /* User id */
885 err = mz_stream_write_uint16(stream, 0);
886 if (err == MZ_OK) /* Group id */
887 err = mz_stream_write_uint16(stream, 0);
888 if (err == MZ_OK && linkname_size > 0)
889 {
890 if (mz_stream_write(stream, file_info->linkname, linkname_size) != linkname_size)
891 err = MZ_WRITE_ERROR;
892 }
893 }
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800894#ifdef HAVE_WZAES
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800895 /* Write AES extra field */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700896 if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700897 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700898 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700899 if (err == MZ_OK)
900 err = mz_stream_write_uint16(stream, file_info->aes_version);
901 if (err == MZ_OK)
902 err = mz_stream_write_uint8(stream, 'A');
903 if (err == MZ_OK)
904 err = mz_stream_write_uint8(stream, 'E');
905 if (err == MZ_OK)
906 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
907 if (err == MZ_OK)
908 err = mz_stream_write_uint16(stream, file_info->compression_method);
909 }
910#endif
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700911 if ((err == MZ_OK) && (!local) && (file_info->comment != NULL))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700912 {
Nathan Moinvazirica014e62018-08-15 07:31:12 -0700913 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700914 err = MZ_WRITE_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700915 }
916
917 return err;
918}
919
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800920static 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 -0800921{
922 int32_t err = MZ_OK;
923
924 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
925 if (err == MZ_OK)
926 err = mz_stream_write_uint32(stream, crc32);
927
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800928 /* Store data descriptor as 8 bytes if zip 64 extension enabled */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800929 if (err == MZ_OK)
930 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800931 /* Zip 64 extension is enabled when uncompressed size is > UINT32_MAX */
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800932 if (!zip64)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800933 err = mz_stream_write_uint32(stream, (uint32_t)compressed_size);
934 else
935 err = mz_stream_write_int64(stream, compressed_size);
936 }
937 if (err == MZ_OK)
938 {
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800939 if (!zip64)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800940 err = mz_stream_write_uint32(stream, (uint32_t)uncompressed_size);
941 else
942 err = mz_stream_write_int64(stream, uncompressed_size);
943 }
944
945 return err;
946}
947
948static int32_t mz_zip_read_cd(void *handle)
949{
950 mz_zip *zip = (mz_zip *)handle;
951 uint64_t number_entry_cd64 = 0;
952 uint64_t number_entry = 0;
953 uint64_t number_entry_cd = 0;
954 int64_t eocd_pos = 0;
955 int64_t eocd_pos64 = 0;
956 int64_t value64i = 0;
957 uint16_t value16 = 0;
958 uint32_t value32 = 0;
959 uint64_t value64 = 0;
960 uint16_t comment_size = 0;
961 int32_t comment_read = 0;
962 int32_t err = MZ_OK;
963
964
965 if (zip == NULL)
966 return MZ_PARAM_ERROR;
967
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800968 /* Read and cache central directory records */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800969 err = mz_zip_search_eocd(zip->stream, &eocd_pos);
970 if (err == MZ_OK)
971 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800972 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800973 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800974 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800975 if (err == MZ_OK)
976 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800977 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800978 if (err == MZ_OK)
979 err = mz_stream_read_uint16(zip->stream, &value16);
980 zip->disk_number_with_cd = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800981 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800982 if (err == MZ_OK)
983 err = mz_stream_read_uint16(zip->stream, &value16);
984 zip->number_entry = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800985 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800986 if (err == MZ_OK)
987 err = mz_stream_read_uint16(zip->stream, &value16);
988 number_entry_cd = value16;
989 if (number_entry_cd != zip->number_entry)
990 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800991 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800992 if (err == MZ_OK)
993 err = mz_stream_read_uint32(zip->stream, &value32);
994 if (err == MZ_OK)
995 zip->cd_size = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800996 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800997 if (err == MZ_OK)
998 err = mz_stream_read_uint32(zip->stream, &value32);
999 if (err == MZ_OK)
1000 zip->cd_offset = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001001 /* Zip file global comment length */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001002 if (err == MZ_OK)
1003 err = mz_stream_read_uint16(zip->stream, &comment_size);
1004 if ((err == MZ_OK) && (comment_size > 0))
1005 {
1006 zip->comment = (char *)MZ_ALLOC(comment_size + 1);
1007 if (zip->comment != NULL)
1008 {
1009 comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001010 /* Don't fail if incorrect comment length read, not critical */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001011 if (comment_read < 0)
1012 comment_read = 0;
1013 zip->comment[comment_read] = 0;
1014 }
1015 }
1016
1017 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
1018 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001019 /* Format should be Zip64, as the central directory or file size is too large */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001020 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
1021 {
1022 eocd_pos = eocd_pos64;
1023
1024 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001025 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001026 if (err == MZ_OK)
1027 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001028 /* Size of zip64 end of central directory record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001029 if (err == MZ_OK)
1030 err = mz_stream_read_uint64(zip->stream, &value64);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001031 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001032 if (err == MZ_OK)
1033 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001034 /* Version needed to extract */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001035 if (err == MZ_OK)
1036 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001037 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001038 if (err == MZ_OK)
1039 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001040 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001041 if (err == MZ_OK)
1042 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001043 /* Total number of entries in the central directory on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001044 if (err == MZ_OK)
1045 err = mz_stream_read_uint64(zip->stream, &number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001046 /* Total number of entries in the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001047 if (err == MZ_OK)
1048 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
1049 if (number_entry == UINT32_MAX)
1050 zip->number_entry = number_entry_cd64;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001051 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001052 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001053 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001054 err = mz_stream_read_int64(zip->stream, &zip->cd_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001055 if (zip->cd_size < 0)
1056 err = MZ_FORMAT_ERROR;
1057 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001058 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001059 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001060 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001061 err = mz_stream_read_int64(zip->stream, &zip->cd_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001062 if (zip->cd_offset < 0)
1063 err = MZ_FORMAT_ERROR;
1064 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001065 }
1066 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
1067 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
1068 {
1069 err = MZ_FORMAT_ERROR;
1070 }
1071 }
1072 }
1073
1074 if (err == MZ_OK)
1075 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001076 mz_zip_print("Zip - Read cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001077 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1078
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001079 /* Verify central directory signature exists at offset */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001080 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1081 if (err == MZ_OK)
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001082 err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001083 if ((err == MZ_OK) && (zip->cd_signature != MZ_ZIP_MAGIC_CENTRALHEADER))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001084 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001085 /* If cd exists in large file and no zip-64 support, error for recover */
1086 if (eocd_pos > UINT32_MAX && eocd_pos64 == 0)
1087 err = MZ_FORMAT_ERROR;
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001088 /* If cd not found attempt to seek backward to find it */
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001089 if (err == MZ_OK)
1090 err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001091 if (err == MZ_OK)
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001092 err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001093 if ((err == MZ_OK) && (zip->cd_signature == MZ_ZIP_MAGIC_CENTRALHEADER))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001094 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001095
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001096 /* If found compensate for incorrect locations */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001097 value64i = zip->cd_offset;
1098 zip->cd_offset = eocd_pos - zip->cd_size;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001099 /* Assume disk has prepended data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001100 zip->disk_offset_shift = zip->cd_offset - value64i;
1101 }
1102 }
1103 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001104
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001105 if (err == MZ_OK)
1106 {
1107 if (eocd_pos < zip->cd_offset)
1108 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001109 /* End of central dir should always come after central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001110 err = MZ_FORMAT_ERROR;
1111 }
1112 else if (eocd_pos < zip->cd_offset + zip->cd_size)
1113 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001114 /* Truncate size of cd if incorrect size or offset provided */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001115 zip->cd_size = eocd_pos - zip->cd_offset;
1116 }
1117 }
1118
1119 return err;
1120}
1121
1122static int32_t mz_zip_write_cd(void *handle)
1123{
1124 mz_zip *zip = (mz_zip *)handle;
1125 int64_t zip64_eocd_pos_inzip = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001126 int64_t disk_number = 0;
1127 int64_t disk_size = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001128 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001129 int32_t err = MZ_OK;
1130
1131
1132 if (zip == NULL)
1133 return MZ_PARAM_ERROR;
1134
1135 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
1136 zip->disk_number_with_cd = (uint32_t)disk_number;
1137 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
1138 zip->disk_number_with_cd += 1;
1139 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Nathan Moinvazirif286b832019-11-10 17:09:48 -08001140 if ((zip->disk_number_with_cd > 0) && (zip->open_mode & MZ_OPEN_MODE_APPEND))
1141 {
1142 // Overwrite existing central directory if using split disks
1143 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1144 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001145
1146 zip->cd_offset = mz_stream_tell(zip->stream);
1147 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
1148 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
1149 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
1150
1151 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
1152
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001153 mz_zip_print("Zip - Write cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001154 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1155
Antoine Cœur61f224a2019-05-02 01:42:09 +08001156 if (zip->cd_size == 0 && zip->number_entry > 0)
Nathan Moinvaziri64faa252019-04-21 21:38:48 -07001157 {
1158 // Zip does not contain central directory, open with recovery option
1159 return MZ_FORMAT_ERROR;
1160 }
1161
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001162 /* Write the ZIP64 central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001163 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
1164 {
1165 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
1166
1167 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
1168
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001169 /* Size of this 'zip64 end of central directory' */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001170 if (err == MZ_OK)
1171 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001172 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001173 if (err == MZ_OK)
1174 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001175 /* Version needed */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001176 if (err == MZ_OK)
1177 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001178 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001179 if (err == MZ_OK)
1180 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001181 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001182 if (err == MZ_OK)
1183 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001184 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001185 if (err == MZ_OK)
1186 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001187 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001188 if (err == MZ_OK)
1189 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001190 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001191 if (err == MZ_OK)
1192 err = mz_stream_write_int64(zip->stream, zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001193 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001194 if (err == MZ_OK)
1195 err = mz_stream_write_int64(zip->stream, zip->cd_offset);
1196 if (err == MZ_OK)
1197 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
1198
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001199 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001200 if (err == MZ_OK)
1201 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001202 /* Relative offset to the end of zip64 central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001203 if (err == MZ_OK)
1204 err = mz_stream_write_int64(zip->stream, zip64_eocd_pos_inzip);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001205 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001206 if (err == MZ_OK)
1207 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
1208 }
1209
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001210 /* Write the central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001211
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001212 /* Signature */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001213 if (err == MZ_OK)
1214 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001215 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001216 if (err == MZ_OK)
1217 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001218 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001219 if (err == MZ_OK)
1220 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001221 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001222 if (err == MZ_OK)
1223 {
1224 if (zip->number_entry >= UINT16_MAX)
1225 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1226 else
1227 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1228 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001229 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001230 if (err == MZ_OK)
1231 {
1232 if (zip->number_entry >= UINT16_MAX)
1233 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1234 else
1235 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1236 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001237 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001238 if (err == MZ_OK)
1239 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001240 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001241 if (err == MZ_OK)
1242 {
1243 if (zip->cd_offset >= UINT32_MAX)
1244 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
1245 else
1246 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
1247 }
1248
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001249 /* Write global comment */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001250 if (zip->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001251 {
1252 comment_size = (int32_t)strlen(zip->comment);
1253 if (comment_size > UINT16_MAX)
1254 comment_size = UINT16_MAX;
1255 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001256 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001257 err = mz_stream_write_uint16(zip->stream, (uint16_t)comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001258 if (err == MZ_OK)
1259 {
1260 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
1261 err = MZ_READ_ERROR;
1262 }
1263 return err;
1264}
1265
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001266static int32_t mz_zip_recover_cd(void *handle)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001267{
1268 mz_zip *zip = (mz_zip *)handle;
1269 mz_zip_file local_file_info;
1270 void *local_file_info_stream = NULL;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001271 void *cd_mem_stream = NULL;
1272 uint64_t number_entry = 0;
1273 int64_t descriptor_pos = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001274 int64_t next_header_pos = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001275 int64_t disk_offset = 0;
1276 int64_t disk_number = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001277 int64_t compressed_pos = 0;
1278 int64_t compressed_end_pos = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001279 int64_t compressed_size = 0;
1280 int64_t uncompressed_size = 0;
1281 uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001282 uint8_t local_header_magic[4] = MZ_ZIP_MAGIC_LOCALHEADERU8;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001283 uint8_t central_header_magic[4] = MZ_ZIP_MAGIC_CENTRALHEADERU8;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001284 uint32_t crc32 = 0;
1285 int32_t disk_number_with_cd = 0;
1286 int32_t err = MZ_OK;
1287 uint8_t zip64 = 0;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001288 uint8_t eof = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001289
1290
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001291 mz_zip_print("Zip - Recover - Start\n");
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001292
1293 mz_zip_get_cd_mem_stream(handle, &cd_mem_stream);
1294
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001295 /* Determine if we are on a split disk or not */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001296 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0);
1297 if (mz_stream_tell(zip->stream) < 0)
1298 {
1299 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1300 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1301 }
1302 else
1303 disk_number_with_cd = 1;
1304
1305 if (mz_stream_is_open(cd_mem_stream) != MZ_OK)
1306 err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001307
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001308 mz_stream_mem_create(&local_file_info_stream);
1309 mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri57cbcd22019-10-21 15:37:58 -07001310
1311 if (err == MZ_OK)
1312 {
1313 err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001314 INT64_MAX, &next_header_pos);
Nathan Moinvaziri57cbcd22019-10-21 15:37:58 -07001315 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001316
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001317 while (err == MZ_OK && !eof)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001318 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001319 /* Get current offset and disk number for central dir record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001320 disk_offset = mz_stream_tell(zip->stream);
1321 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1322
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001323 /* Read local headers */
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001324 memset(&local_file_info, 0, sizeof(local_file_info));
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001325 err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream);
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001326 if (err != MZ_OK)
1327 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001328
1329 local_file_info.disk_offset = disk_offset;
1330 if (disk_number < 0)
1331 disk_number = 0;
1332 local_file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001333
1334 compressed_pos = mz_stream_tell(zip->stream);
1335
1336 if ((err == MZ_OK) && (local_file_info.compressed_size > 0))
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001337 {
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001338 mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001339 }
1340
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001341 while (1)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001342 {
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001343 /* Search for the next local header */
1344 err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
1345 INT64_MAX, &next_header_pos);
1346
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001347 if (err == MZ_EXIST_ERROR)
1348 {
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001349 mz_stream_seek(zip->stream, compressed_pos, MZ_SEEK_SET);
1350
1351 /* Search for central dir if no local header found */
1352 err = mz_stream_find(zip->stream, (const void *)central_header_magic, sizeof(central_header_magic),
1353 INT64_MAX, &next_header_pos);
1354
1355 if (err == MZ_EXIST_ERROR)
1356 {
1357 /* Get end of stream if no central header found */
1358 mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1359 next_header_pos = mz_stream_tell(zip->stream);
1360 }
1361
1362 eof = 1;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001363 }
1364
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001365 if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR || local_file_info.compressed_size == 0)
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001366 {
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001367 /* Search backwards for the descriptor, seeking too far back will be incorrect if compressed size is small */
1368 err = mz_stream_find_reverse(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
1369 MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR, &descriptor_pos);
1370 if (err == MZ_OK)
1371 {
1372 if (mz_zip_extrafield_contains(local_file_info.extrafield,
1373 local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
1374 zip64 = 1;
1375
1376 err = mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32,
1377 &compressed_size, &uncompressed_size);
1378
1379 if (err == MZ_OK)
1380 {
1381 if (local_file_info.crc == 0)
1382 local_file_info.crc = crc32;
1383 if (local_file_info.compressed_size == 0)
1384 local_file_info.compressed_size = compressed_size;
1385 if (local_file_info.uncompressed_size == 0)
1386 local_file_info.uncompressed_size = uncompressed_size;
1387 }
1388
1389 compressed_end_pos = descriptor_pos;
1390 }
1391 else if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1392 {
1393 /* Wrong local file entry found, keep searching */
1394 next_header_pos += 1;
1395 mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
1396 continue;
1397 }
1398 }
1399 else
1400 {
1401 compressed_end_pos = next_header_pos;
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001402 }
1403
Nathan Moinvaziri4b5bd872019-11-11 17:04:05 -08001404 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001405 }
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001406
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001407 compressed_size = compressed_end_pos - compressed_pos;
1408
1409 if (compressed_size > UINT32_MAX)
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001410 {
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001411 /* Update sizes if 4GB file is written with no ZIP64 support */
1412 if (local_file_info.uncompressed_size < UINT32_MAX)
1413 {
1414 local_file_info.compressed_size = compressed_size;
1415 local_file_info.uncompressed_size = 0;
1416 }
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001417 }
1418
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001419 mz_zip_print("Zip - Recover - Entry %s (csize %" PRId64 " usize %" PRId64 " flags 0x%" PRIx16 ")\n",
1420 local_file_info.filename, local_file_info.compressed_size, local_file_info.uncompressed_size,
1421 local_file_info.flag);
1422
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001423 /* Rewrite central dir with local headers and offsets */
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001424 err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001425 if (err == MZ_OK)
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001426 number_entry += 1;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001427
Nathan Moinvaziri545d7292019-10-21 15:32:18 -07001428 err = mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001429 }
1430
1431 mz_stream_mem_delete(&local_file_info_stream);
1432
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001433 mz_zip_print("Zip - Recover - Complete (cddisk %" PRId32 " entries %" PRId64 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001434 disk_number_with_cd, number_entry);
1435
1436 if (number_entry == 0)
1437 return err;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001438
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001439 /* Set new upper seek boundary for central dir mem stream */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001440 disk_offset = mz_stream_tell(cd_mem_stream);
1441 mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset);
1442
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001443 /* Set new central directory info */
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001444 mz_zip_set_cd_stream(handle, 0, cd_mem_stream);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001445 mz_zip_set_number_entry(handle, number_entry);
1446 mz_zip_set_disk_number_with_cd(handle, disk_number_with_cd);
1447
1448 return MZ_OK;
1449}
1450
1451void *mz_zip_create(void **handle)
1452{
1453 mz_zip *zip = NULL;
1454
1455 zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
1456 if (zip != NULL)
1457 memset(zip, 0, sizeof(mz_zip));
1458 if (handle != NULL)
1459 *handle = zip;
1460
1461 return zip;
1462}
1463
1464void mz_zip_delete(void **handle)
1465{
1466 mz_zip *zip = NULL;
1467 if (handle == NULL)
1468 return;
1469 zip = (mz_zip *)*handle;
1470 if (zip != NULL)
1471 {
1472 MZ_FREE(zip);
1473 }
1474 *handle = NULL;
1475}
1476
1477int32_t mz_zip_open(void *handle, void *stream, int32_t mode)
1478{
1479 mz_zip *zip = (mz_zip *)handle;
1480 int32_t err = MZ_OK;
1481
1482
1483 if (zip == NULL)
1484 return MZ_PARAM_ERROR;
1485
1486 mz_zip_print("Zip - Open\n");
1487
1488 zip->stream = stream;
1489
1490 mz_stream_mem_create(&zip->cd_mem_stream);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001491
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001492 if (mode & MZ_OPEN_MODE_WRITE)
1493 {
1494 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1495 zip->cd_stream = zip->cd_mem_stream;
1496 }
1497 else
1498 {
1499 zip->cd_stream = stream;
1500 }
1501
1502 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
1503 {
1504 if ((mode & MZ_OPEN_MODE_CREATE) == 0)
1505 {
1506 err = mz_zip_read_cd(zip);
1507 if (err != MZ_OK)
1508 {
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001509 mz_zip_print("Zip - Error detected reading cd (%" PRId32 ")\n", err);
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001510 if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001511 err = MZ_OK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001512 }
1513 }
1514
1515 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
1516 {
1517 if (zip->cd_size > 0)
1518 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001519 /* Store central directory in memory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001520 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1521 if (err == MZ_OK)
1522 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size);
1523 if (err == MZ_OK)
1524 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1525 }
1526 else
1527 {
Nathan Moinvaziri0e1fd892019-09-28 20:55:37 -07001528 if (zip->cd_signature == MZ_ZIP_MAGIC_ENDHEADER)
1529 {
1530 /* If tiny zip then overwrite end header */
1531 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1532 }
1533 else
1534 {
1535 /* If no central directory, append new zip to end of file */
1536 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1537 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001538 }
Nathan Moinvaziri2ce1fe22018-11-23 11:53:11 -08001539
1540 if (zip->disk_number_with_cd > 0)
1541 {
1542 /* Move to last disk to begin appending */
1543 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->disk_number_with_cd - 1);
1544 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001545 }
1546 else
1547 {
1548 zip->cd_start_pos = zip->cd_offset;
1549 }
1550 }
1551
1552 if (err != MZ_OK)
1553 {
1554 mz_zip_close(zip);
1555 return err;
1556 }
1557
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001558 /* Memory streams used to store variable length file info data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001559 mz_stream_mem_create(&zip->file_info_stream);
1560 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1561
1562 mz_stream_mem_create(&zip->local_file_info_stream);
1563 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1564
1565 zip->open_mode = mode;
1566
1567 return err;
1568}
1569
1570int32_t mz_zip_close(void *handle)
1571{
1572 mz_zip *zip = (mz_zip *)handle;
1573 int32_t err = MZ_OK;
1574
1575 if (zip == NULL)
1576 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001577
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001578 mz_zip_print("Zip - Close\n");
1579
1580 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001581 err = mz_zip_entry_close(handle);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001582
1583 if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE))
1584 err = mz_zip_write_cd(handle);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001585
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001586 if (zip->cd_mem_stream != NULL)
1587 {
1588 mz_stream_close(zip->cd_mem_stream);
1589 mz_stream_delete(&zip->cd_mem_stream);
1590 }
1591
1592 if (zip->file_info_stream != NULL)
1593 {
1594 mz_stream_mem_close(zip->file_info_stream);
1595 mz_stream_mem_delete(&zip->file_info_stream);
1596 }
1597 if (zip->local_file_info_stream != NULL)
1598 {
1599 mz_stream_mem_close(zip->local_file_info_stream);
1600 mz_stream_mem_delete(&zip->local_file_info_stream);
1601 }
1602
1603 if (zip->comment)
1604 {
1605 MZ_FREE(zip->comment);
1606 zip->comment = NULL;
1607 }
1608
1609 zip->stream = NULL;
1610 zip->cd_stream = NULL;
1611
1612 return err;
1613}
1614
1615int32_t mz_zip_get_comment(void *handle, const char **comment)
1616{
1617 mz_zip *zip = (mz_zip *)handle;
1618 if (zip == NULL || comment == NULL)
1619 return MZ_PARAM_ERROR;
1620 if (zip->comment == NULL)
1621 return MZ_EXIST_ERROR;
1622 *comment = zip->comment;
1623 return MZ_OK;
1624}
1625
1626int32_t mz_zip_set_comment(void *handle, const char *comment)
1627{
1628 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001629 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001630 if (zip == NULL || comment == NULL)
1631 return MZ_PARAM_ERROR;
1632 if (zip->comment != NULL)
1633 MZ_FREE(zip->comment);
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001634 comment_size = (int32_t)strlen(comment);
1635 if (comment_size > UINT16_MAX)
1636 return MZ_PARAM_ERROR;
Nathan Moinvaziria6ecab52019-01-22 13:14:01 -08001637 zip->comment = (char *)MZ_ALLOC(comment_size+1);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001638 if (zip->comment == NULL)
1639 return MZ_MEM_ERROR;
Nathan Moinvaziria6ecab52019-01-22 13:14:01 -08001640 memset(zip->comment, 0, comment_size+1);
Nathan Moinvaziri2df1ecb2018-12-01 08:59:21 -08001641 strncpy(zip->comment, comment, comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001642 return MZ_OK;
1643}
1644
1645int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
1646{
1647 mz_zip *zip = (mz_zip *)handle;
1648 if (zip == NULL || version_madeby == NULL)
1649 return MZ_PARAM_ERROR;
1650 *version_madeby = zip->version_madeby;
1651 return MZ_OK;
1652}
1653
1654int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
1655{
1656 mz_zip *zip = (mz_zip *)handle;
1657 if (zip == NULL)
1658 return MZ_PARAM_ERROR;
1659 zip->version_madeby = version_madeby;
1660 return MZ_OK;
1661}
1662
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001663int32_t mz_zip_set_recover(void *handle, uint8_t recover)
1664{
1665 mz_zip *zip = (mz_zip *)handle;
1666 if (zip == NULL)
1667 return MZ_PARAM_ERROR;
1668 zip->recover = recover;
1669 return MZ_OK;
1670}
1671
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001672int32_t mz_zip_get_stream(void *handle, void **stream)
1673{
1674 mz_zip *zip = (mz_zip *)handle;
1675 if (zip == NULL || stream == NULL)
1676 return MZ_PARAM_ERROR;
1677 *stream = zip->stream;
1678 if (*stream == NULL)
1679 return MZ_EXIST_ERROR;
1680 return MZ_OK;
1681}
1682
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001683int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001684{
1685 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001686 if (zip == NULL || cd_stream == NULL)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001687 return MZ_PARAM_ERROR;
Nathan Moinvaziribe483d82019-10-19 17:30:23 -07001688 zip->cd_offset = 0;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001689 zip->cd_stream = cd_stream;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001690 zip->cd_start_pos = cd_start_pos;
1691 return MZ_OK;
1692}
1693
1694int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream)
1695{
1696 mz_zip *zip = (mz_zip *)handle;
1697 if (zip == NULL || cd_mem_stream == NULL)
1698 return MZ_PARAM_ERROR;
1699 *cd_mem_stream = zip->cd_mem_stream;
1700 if (*cd_mem_stream == NULL)
1701 return MZ_EXIST_ERROR;
1702 return MZ_OK;
1703}
1704
1705static int32_t mz_zip_entry_close_int(void *handle)
1706{
1707 mz_zip *zip = (mz_zip *)handle;
1708
1709 if (zip->crypt_stream != NULL)
1710 mz_stream_delete(&zip->crypt_stream);
1711 zip->crypt_stream = NULL;
1712 if (zip->compress_stream != NULL)
1713 mz_stream_delete(&zip->compress_stream);
1714 zip->compress_stream = NULL;
1715
1716 zip->entry_opened = 0;
1717
1718 return MZ_OK;
1719}
1720
Nathan Moinvaziri26308b22018-08-08 09:40:15 -07001721static 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 -07001722{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001723 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001724 int64_t max_total_in = 0;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001725 int64_t header_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001726 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001727 int32_t err = MZ_OK;
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001728 uint8_t use_crypt = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001729
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001730 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001731 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001732
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001733 switch (zip->file_info.compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001734 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001735 case MZ_COMPRESS_METHOD_STORE:
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001736 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001737#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001738 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001739#endif
Nathan Moinvaziri566dfe02018-10-26 00:36:30 -07001740#ifdef HAVE_LZMA
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001741 case MZ_COMPRESS_METHOD_LZMA:
1742#endif
1743 err = MZ_OK;
1744 break;
1745 default:
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001746 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001747 }
1748
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001749#ifndef HAVE_WZAES
1750 if (zip->file_info.aes_version)
1751 return MZ_SUPPORT_ERROR;
1752#endif
1753
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001754 zip->entry_raw = raw;
1755
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001756 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password != NULL))
1757 {
1758 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
1759 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001760 /* Encrypt only when we are not trying to write raw and password is supplied. */
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001761 if (!zip->entry_raw)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001762 use_crypt = 1;
1763 }
1764 else if (zip->open_mode & MZ_OPEN_MODE_READ)
1765 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001766 /* Decrypt only when password is supplied. Don't error when password */
1767 /* is not supplied as we may want to read the raw encrypted data. */
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001768 use_crypt = 1;
1769 }
1770 }
1771
1772 if ((err == MZ_OK) && (use_crypt))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001773 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001774#ifdef HAVE_WZAES
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001775 if (zip->file_info.aes_version)
1776 {
Nathan Moinvaziri21a31022018-10-24 09:50:16 -07001777 mz_stream_wzaes_create(&zip->crypt_stream);
1778 mz_stream_wzaes_set_password(zip->crypt_stream, password);
1779 mz_stream_wzaes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001780 }
1781 else
1782#endif
1783 {
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001784#ifdef HAVE_PKCRYPT
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001785 uint8_t verify1 = 0;
1786 uint8_t verify2 = 0;
1787
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001788 /* Info-ZIP modification to ZipCrypto format: */
1789 /* 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 -07001790
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001791 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1792 {
1793 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001794
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001795 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1796
1797 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1798 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1799 }
1800 else
1801 {
1802 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1803 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1804 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001805
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001806 mz_stream_pkcrypt_create(&zip->crypt_stream);
1807 mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1808 mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001809#endif
1810 }
1811 }
1812
1813 if (err == MZ_OK)
1814 {
1815 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001816 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001817
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001818 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001819
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001820 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001821 }
1822
1823 if (err == MZ_OK)
1824 {
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001825 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001826 mz_stream_raw_create(&zip->compress_stream);
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001827#if defined(HAVE_ZLIB) || defined(HAVE_LIBCOMP)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001828 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001829 mz_stream_zlib_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001830#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001831#ifdef HAVE_BZIP2
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001832 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001833 mz_stream_bzip_create(&zip->compress_stream);
1834#endif
1835#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001836 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001837 mz_stream_lzma_create(&zip->compress_stream);
1838#endif
1839 else
1840 err = MZ_PARAM_ERROR;
1841 }
1842
1843 if (err == MZ_OK)
1844 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001845 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001846 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001847 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001848 }
1849 else
1850 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001851#ifndef HAVE_LIBCOMP
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001852 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 -08001853#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001854 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001855 max_total_in = zip->file_info.compressed_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001856 mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1857
1858 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1859 max_total_in -= header_size;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001860 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1861 max_total_in -= footer_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001862
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001863 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001864 }
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001865 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 -03001866 {
1867 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1868 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1869 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001870 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001871
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001872 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1873
1874 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1875 }
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001876
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001877 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001878 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001879 zip->entry_opened = 1;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001880 zip->entry_crc32 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001881 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001882 else
1883 {
1884 mz_zip_entry_close_int(handle);
1885 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001886
1887 return err;
1888}
1889
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001890int32_t mz_zip_entry_is_open(void *handle)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001891{
1892 mz_zip *zip = (mz_zip *)handle;
1893 if (zip == NULL)
1894 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001895 if (zip->entry_opened == 0)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001896 return MZ_EXIST_ERROR;
1897 return MZ_OK;
1898}
1899
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001900static int32_t mz_zip_seek_to_local_header(void *handle)
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001901{
1902 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001903
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001904
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001905 if (zip->file_info.disk_number == zip->disk_number_with_cd)
1906 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1907 else
1908 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001909
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001910 mz_zip_print("Zip - Entry - Seek local (disk %" PRId32 " offset %" PRId64 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001911 zip->file_info.disk_number, zip->file_info.disk_offset);
Nathan Moinvaziri09725902019-02-20 09:15:48 -08001912
1913 /* Guard against seek overflows */
1914 if ((zip->disk_offset_shift > 0) &&
1915 (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
1916 return MZ_FORMAT_ERROR;
1917
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001918 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 -07001919}
1920
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001921int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001922{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001923 mz_zip *zip = (mz_zip *)handle;
1924 int32_t err = MZ_OK;
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001925 int32_t err_shift = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001926
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001927#if defined(MZ_ZIP_NO_ENCRYPTION)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001928 if (password != NULL)
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001929 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001930#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001931 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001932 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001933 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001934 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001935 if (zip->entry_scanned == 0)
1936 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001937
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001938 mz_zip_print("Zip - Entry - Read open (raw %" PRId32 ")\n", raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001939
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001940 err = mz_zip_seek_to_local_header(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001941 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001942 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 -07001943
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001944 if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0)
1945 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001946 /* Perhaps we didn't compensated correctly for incorrect cd offset */
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001947 err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
1948 if (err_shift == MZ_OK)
1949 err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
1950 if (err_shift == MZ_OK)
1951 {
1952 zip->disk_offset_shift = 0;
1953 err = err_shift;
1954 }
1955 }
1956
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001957#ifdef MZ_ZIP_NO_DECOMPRESSION
Nathan Moinvaziri2eb29072018-11-23 10:32:21 -08001958 if (!raw && zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001959 err = MZ_SUPPORT_ERROR;
Viktor Szakatse7215072018-09-04 15:06:53 +00001960#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001961 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001962 err = mz_zip_entry_open_int(handle, raw, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001963
1964 return err;
1965}
1966
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001967int32_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 -07001968{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001969 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08001970 int64_t filename_pos = -1;
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001971 int64_t extrafield_pos = 0;
1972 int64_t comment_pos = 0;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07001973 int64_t linkname_pos = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001974 int64_t disk_number = 0;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001975 uint8_t is_dir = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001976 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001977
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001978#if defined(MZ_ZIP_NO_ENCRYPTION)
tz-lomb1b25802017-11-10 15:03:02 +03001979 if (password != NULL)
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08001980 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001981#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001982 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001983 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001984
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001985 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001986 {
1987 err = mz_zip_entry_close(handle);
1988 if (err != MZ_OK)
1989 return err;
1990 }
1991
1992 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07001993
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07001994 mz_zip_print("Zip - Entry - Write open - %s (level %" PRId16 " raw %" PRId8 ")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001995 zip->file_info.filename, compress_level, raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001996
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001997 mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001998 mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
1999
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002000 /* Copy filename, extrafield, and comment internally */
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002001 filename_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07002002 if (file_info->filename != NULL)
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002003 mz_stream_write(zip->file_info_stream, file_info->filename, (int32_t)strlen(file_info->filename));
2004 mz_stream_write_uint8(zip->file_info_stream, 0);
2005
2006 extrafield_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07002007 if (file_info->extrafield != NULL)
Nathan Moinvazirica014e62018-08-15 07:31:12 -07002008 mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002009 mz_stream_write_uint8(zip->file_info_stream, 0);
2010
2011 comment_pos = mz_stream_tell(zip->file_info_stream);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07002012 if (file_info->comment != NULL)
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002013 mz_stream_write(zip->file_info_stream, file_info->comment, file_info->comment_size);
2014 mz_stream_write_uint8(zip->file_info_stream, 0);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002015
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002016 linkname_pos = mz_stream_tell(zip->file_info_stream);
2017 if (file_info->linkname != NULL)
2018 mz_stream_write(zip->file_info_stream, file_info->linkname, (int32_t)strlen(file_info->linkname));
2019 mz_stream_write_uint8(zip->file_info_stream, 0);
Nathan Moinvaziri6b64b922018-11-23 11:41:35 -08002020
Nathan Moinvazirif9e34482018-11-27 09:45:09 -08002021 mz_stream_mem_get_buffer_at(zip->file_info_stream, filename_pos, (const void **)&zip->file_info.filename);
2022 mz_stream_mem_get_buffer_at(zip->file_info_stream, extrafield_pos, (const void **)&zip->file_info.extrafield);
2023 mz_stream_mem_get_buffer_at(zip->file_info_stream, comment_pos, (const void **)&zip->file_info.comment);
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002024 mz_stream_mem_get_buffer_at(zip->file_info_stream, linkname_pos, (const void **)&zip->file_info.linkname);
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002025
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002026 if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002027 {
2028 if ((compress_level == 8) || (compress_level == 9))
2029 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
2030 if (compress_level == 2)
2031 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
2032 if (compress_level == 1)
2033 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
2034 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002035#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002036 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002037 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002038#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08002039
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002040 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2041 is_dir = 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002042
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002043 if (!is_dir)
2044 {
2045 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
2046 if (password != NULL)
2047 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
2048 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002049
juaniib8887e92018-02-14 00:51:05 -03002050 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
2051 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002052
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002053 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07002054 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002055 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002056
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08002057#ifdef HAVE_WZAES
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002058 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
2059 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
2060#endif
2061
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002062 if ((compress_level == 0) || (is_dir))
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002063 zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
Viktor Szakats2884e672018-04-30 08:12:13 +00002064
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07002065#ifdef MZ_ZIP_NO_COMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002066 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002067 err = MZ_SUPPORT_ERROR;
2068#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002069 if (err == MZ_OK)
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002070 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002071 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07002072 err = mz_zip_entry_open_int(handle, raw, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002073
2074 return err;
2075}
2076
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002077int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002078{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002079 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002080 int32_t read = 0;
2081
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002082 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002083 return MZ_PARAM_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002084 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) /* zlib limitation */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002085 return MZ_PARAM_ERROR;
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07002086 if (len == 0)
2087 return MZ_PARAM_ERROR;
2088
Nathan Moinvazirieb80cd92018-07-11 16:45:09 -07002089 if (zip->file_info.compressed_size == 0)
2090 return 0;
2091
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002092 /* Read entire entry even if uncompressed_size = 0, otherwise */
2093 /* aes encryption validation will fail if compressed_size > 0 */
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07002094 read = mz_stream_read(zip->compress_stream, buf, len);
2095 if (read > 0)
2096 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002097
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002098 mz_zip_print("Zip - Entry - Read - %" PRId32 " (max %" PRId32 ")\n", read, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002099
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002100 return read;
2101}
2102
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002103int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002104{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002105 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002106 int32_t written = 0;
2107
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002108 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002109 return MZ_PARAM_ERROR;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07002110 written = mz_stream_write(zip->compress_stream, buf, len);
2111 if (written > 0)
2112 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002113
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002114 mz_zip_print("Zip - Entry - Write - %" PRId32 " (max %" PRId32 ")\n", written, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002115
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07002116 return written;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002117}
2118
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002119int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002120 int64_t *uncompressed_size)
2121{
2122 mz_zip *zip = (mz_zip *)handle;
2123 int64_t total_in = 0;
2124 int32_t err = MZ_OK;
2125 uint8_t zip64 = 0;
2126
2127 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2128 return MZ_PARAM_ERROR;
2129
2130 mz_stream_close(zip->compress_stream);
2131
2132 mz_zip_print("Zip - Entry - Read Close\n");
2133
2134 if (crc32 != NULL)
2135 *crc32 = zip->file_info.crc;
2136 if (compressed_size != NULL)
2137 *compressed_size = zip->file_info.compressed_size;
2138 if (uncompressed_size != NULL)
2139 *uncompressed_size = zip->file_info.uncompressed_size;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002140
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002141 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
2142
2143 if ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) &&
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002144 ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0) &&
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002145 (crc32 != NULL || compressed_size != NULL || uncompressed_size != NULL))
2146 {
2147 /* Check to see if data descriptor is zip64 bit format or not */
2148 if (mz_zip_extrafield_contains(zip->local_file_info.extrafield,
2149 zip->local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
2150 zip64 = 1;
2151
2152 err = mz_zip_seek_to_local_header(handle);
2153
2154 /* Seek to end of compressed stream since we might have over-read during compression */
2155 if (err == MZ_OK)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002156 err = mz_stream_seek(zip->stream, MZ_ZIP_SIZE_LD_ITEM +
2157 (int64_t)zip->local_file_info.filename_size +
2158 (int64_t)zip->local_file_info.extrafield_size +
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002159 total_in, MZ_SEEK_CUR);
2160
2161 /* Read data descriptor */
2162 if (err == MZ_OK)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002163 err = mz_zip_entry_read_descriptor(zip->stream, zip64,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002164 crc32, compressed_size, uncompressed_size);
2165 }
2166
2167 /* If entire entry was not read verification will fail */
2168 if ((err == MZ_OK) && (total_in > 0) && (!zip->entry_raw))
2169 {
2170#ifdef HAVE_WZAES
2171 /* AES zip version AE-1 will expect a valid crc as well */
2172 if (zip->file_info.aes_version <= 0x0001)
2173#endif
2174 {
2175 if (zip->entry_crc32 != zip->file_info.crc)
2176 {
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002177 mz_zip_print("Zip - Entry - Crc failed (actual 0x%08" PRIx32 " expected 0x%08" PRIx32 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002178 zip->entry_crc32, zip->file_info.crc);
2179
2180 err = MZ_CRC_ERROR;
2181 }
2182 }
2183 }
2184
2185 mz_zip_entry_close_int(handle);
2186
2187 return err;
2188}
2189
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002190int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002191 int64_t uncompressed_size)
2192{
2193 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002194 int32_t err = MZ_OK;
2195 uint8_t zip64 = 0;
2196
2197 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2198 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002199
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002200 mz_stream_close(zip->compress_stream);
2201
2202 if (!zip->entry_raw)
2203 crc32 = zip->entry_crc32;
2204
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002205 mz_zip_print("Zip - Entry - Write Close (crc 0x%08" PRIx32 " cs %" PRId64 " ucs %" PRId64 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002206 crc32, compressed_size, uncompressed_size);
2207
2208 /* If sizes are not set, then read them from the compression stream */
2209 if (compressed_size < 0)
2210 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2211 if (uncompressed_size < 0)
Nathan Moinvazirid9318432018-12-01 07:56:28 -08002212 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002213
2214 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
2215 {
2216 mz_stream_set_base(zip->crypt_stream, zip->stream);
2217 err = mz_stream_close(zip->crypt_stream);
2218
2219 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2220 }
2221
2222 if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
2223 {
2224 /* Determine if we need to write data descriptor in zip64 format,
2225 if local extrafield was saved with zip64 extrafield */
2226 if (zip->file_info.zip64 == MZ_ZIP64_AUTO)
2227 {
2228 if (zip->file_info.uncompressed_size >= UINT32_MAX)
2229 zip64 = 1;
2230 if (zip->file_info.compressed_size >= UINT32_MAX)
2231 zip64 = 1;
2232 if (zip->file_info.disk_offset >= UINT32_MAX)
2233 zip64 = 1;
2234 else if (zip->file_info.uncompressed_size == 0)
2235 zip64 = 1;
2236 }
2237 else if (zip->file_info.zip64 == MZ_ZIP64_FORCE)
2238 {
2239 zip64 = 1;
2240 }
2241
2242 if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002243 err = mz_zip_entry_write_descriptor(zip->stream,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002244 zip64, 0, compressed_size, 0);
2245 else
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002246 err = mz_zip_entry_write_descriptor(zip->stream,
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002247 zip64, crc32, compressed_size, uncompressed_size);
2248 }
2249
2250 /* Write file info to central directory */
2251
Nathan Moinvazirife33c3d2019-09-16 11:07:43 -07002252 mz_zip_print("Zip - Entry - Write cd (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002253 uncompressed_size, compressed_size, crc32);
2254
2255 zip->file_info.crc = crc32;
2256 zip->file_info.compressed_size = compressed_size;
2257 zip->file_info.uncompressed_size = uncompressed_size;
2258
2259 if (err == MZ_OK)
2260 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
2261
2262 zip->number_entry += 1;
2263
2264 mz_zip_entry_close_int(handle);
2265
2266 return err;
2267}
2268
2269int32_t mz_zip_entry_is_dir(void *handle)
2270{
2271 mz_zip *zip = (mz_zip *)handle;
2272 int32_t filename_length = 0;
2273
2274 if (zip == NULL)
2275 return MZ_PARAM_ERROR;
2276 if (zip->entry_scanned == 0)
2277 return MZ_PARAM_ERROR;
2278 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2279 return MZ_OK;
2280
2281 filename_length = (int32_t)strlen(zip->file_info.filename);
2282 if (filename_length > 0)
2283 {
2284 if ((zip->file_info.filename[filename_length - 1] == '/') ||
2285 (zip->file_info.filename[filename_length - 1] == '\\'))
2286 return MZ_OK;
2287 }
2288 return MZ_EXIST_ERROR;
2289}
2290
Nathan Moinvaziri3249eac2019-05-02 21:07:39 -07002291int32_t mz_zip_entry_is_symlink(void *handle)
2292{
2293 mz_zip *zip = (mz_zip *)handle;
2294
2295 if (zip == NULL)
2296 return MZ_PARAM_ERROR;
2297 if (zip->entry_scanned == 0)
2298 return MZ_PARAM_ERROR;
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002299 if (mz_zip_attrib_is_symlink(zip->file_info.external_fa, zip->file_info.version_madeby) != MZ_OK)
2300 return MZ_EXIST_ERROR;
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002301 if (zip->file_info.linkname == NULL || *zip->file_info.linkname == 0)
2302 return MZ_EXIST_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002303
Nathan Moinvaziria5a1d5d2019-05-05 20:13:58 -07002304 return MZ_OK;
Nathan Moinvaziri3249eac2019-05-02 21:07:39 -07002305}
2306
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002307int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002308{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002309 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002310
2311 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002312 return MZ_PARAM_ERROR;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002313
2314 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
2315 {
2316 if (!zip->entry_scanned)
2317 return MZ_PARAM_ERROR;
2318 }
2319
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002320 *file_info = &zip->file_info;
2321 return MZ_OK;
2322}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002323
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002324int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002325{
2326 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002327 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002328 return MZ_PARAM_ERROR;
2329 *local_file_info = &zip->local_file_info;
2330 return MZ_OK;
2331}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002332
Nathan Moinvaziri915c5132018-10-26 20:00:52 -07002333int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size)
2334{
2335 mz_zip *zip = (mz_zip *)handle;
2336
2337 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2338 return MZ_PARAM_ERROR;
2339
2340 zip->file_info.extrafield = extrafield;
2341 zip->file_info.extrafield_size = extrafield_size;
2342 return MZ_OK;
2343}
2344
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002345int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07002346{
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002347 return mz_zip_entry_close_raw(handle, UINT64_MAX, 0);
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07002348}
2349
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002350int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002351{
2352 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002353 int32_t err = MZ_OK;
2354
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002355 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002356 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002357
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002358 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002359 err = mz_zip_entry_write_close(handle, crc32, UINT64_MAX, uncompressed_size);
2360 else
2361 err = mz_zip_entry_read_close(handle, NULL, NULL, NULL);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002362
2363 return err;
2364}
2365
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002366static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002367{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002368 mz_zip *zip = (mz_zip *)handle;
2369 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002370
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002371 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002372 return MZ_PARAM_ERROR;
2373
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002374 zip->entry_scanned = 0;
2375
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002376 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00002377
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002378 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002379 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002380 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002381 if (err == MZ_OK)
2382 zip->entry_scanned = 1;
2383 return err;
2384}
2385
Nathan Moinvaziric565fa82018-10-19 08:48:33 -07002386int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry)
2387{
2388 mz_zip *zip = (mz_zip *)handle;
2389 if (zip == NULL)
2390 return MZ_PARAM_ERROR;
2391 zip->number_entry = number_entry;
2392 return MZ_OK;
2393}
2394
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002395int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002396{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002397 mz_zip *zip = (mz_zip *)handle;
2398 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002399 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002400 *number_entry = zip->number_entry;
2401 return MZ_OK;
2402}
2403
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002404int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd)
2405{
2406 mz_zip *zip = (mz_zip *)handle;
2407 if (zip == NULL)
2408 return MZ_PARAM_ERROR;
2409 zip->disk_number_with_cd = disk_number_with_cd;
2410 return MZ_OK;
2411}
2412
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002413int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03002414{
2415 mz_zip *zip = (mz_zip *)handle;
2416 if (zip == NULL || disk_number_with_cd == NULL)
2417 return MZ_PARAM_ERROR;
2418 *disk_number_with_cd = zip->disk_number_with_cd;
2419 return MZ_OK;
2420}
2421
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002422int64_t mz_zip_get_entry(void *handle)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002423{
2424 mz_zip *zip = (mz_zip *)handle;
2425
2426 if (zip == NULL)
2427 return MZ_PARAM_ERROR;
2428
2429 return zip->cd_current_pos;
2430}
2431
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002432int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002433{
2434 mz_zip *zip = (mz_zip *)handle;
2435
2436 if (zip == NULL)
2437 return MZ_PARAM_ERROR;
2438
2439 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
2440 return MZ_PARAM_ERROR;
2441
2442 zip->cd_current_pos = cd_pos;
2443
2444 return mz_zip_goto_next_entry_int(handle);
2445}
2446
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002447int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002448{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002449 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002450
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002451 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002452 return MZ_PARAM_ERROR;
2453
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002454 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002455
2456 return mz_zip_goto_next_entry_int(handle);
2457}
2458
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002459int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002460{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002461 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002462
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002463 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002464 return MZ_PARAM_ERROR;
2465
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002466 zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002467 zip->file_info.extrafield_size + zip->file_info.comment_size;
2468
2469 return mz_zip_goto_next_entry_int(handle);
2470}
2471
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002472int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002473{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002474 mz_zip *zip = (mz_zip *)handle;
2475 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002476 int32_t result = 0;
2477
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002478 if (zip == NULL || filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002479 return MZ_PARAM_ERROR;
2480
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002481 /* If we are already on the current entry, no need to search */
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002482 if ((zip->entry_scanned) && (zip->file_info.filename != NULL))
2483 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002484 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002485 if (result == 0)
2486 return MZ_OK;
2487 }
2488
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002489 /* Search all entries starting at the first */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002490 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002491 while (err == MZ_OK)
2492 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002493 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2494 if (result == 0)
2495 return MZ_OK;
2496
2497 err = mz_zip_goto_next_entry(handle);
2498 }
2499
2500 return err;
2501}
2502
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002503int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002504{
2505 mz_zip *zip = (mz_zip *)handle;
2506 int32_t err = MZ_OK;
2507 int32_t result = 0;
2508
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002509 /* Search first entry looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002510 err = mz_zip_goto_first_entry(handle);
2511 if (err != MZ_OK)
2512 return err;
2513
2514 result = cb(handle, userdata, &zip->file_info);
2515 if (result == 0)
2516 return MZ_OK;
2517
2518 return mz_zip_locate_next_entry(handle, userdata, cb);
2519}
2520
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002521int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002522{
2523 mz_zip *zip = (mz_zip *)handle;
2524 int32_t err = MZ_OK;
2525 int32_t result = 0;
2526
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002527 /* Search next entries looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002528 err = mz_zip_goto_next_entry(handle);
2529 while (err == MZ_OK)
2530 {
2531 result = cb(handle, userdata, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002532 if (result == 0)
2533 return MZ_OK;
2534
2535 err = mz_zip_goto_next_entry(handle);
2536 }
2537
2538 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002539}
2540
2541/***************************************************************************/
2542
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002543int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002544{
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002545 uint32_t posix_attrib = 0;
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002546 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002547 int32_t err = MZ_OK;
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002548
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002549 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2550 if (err == MZ_OK)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002551 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002552 if ((posix_attrib & 0170000) == 0040000) /* S_ISDIR */
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002553 return MZ_OK;
2554 }
2555
2556 return MZ_EXIST_ERROR;
2557}
2558
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002559int32_t mz_zip_attrib_is_symlink(uint32_t attrib, int32_t version_madeby)
2560{
2561 uint32_t posix_attrib = 0;
2562 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
2563 int32_t err = MZ_OK;
2564
2565 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2566 if (err == MZ_OK)
2567 {
2568 if ((posix_attrib & 0170000) == 0120000) /* S_ISLNK */
2569 return MZ_OK;
2570 }
2571
2572 return MZ_EXIST_ERROR;
2573}
2574
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002575int32_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 -07002576{
2577 if (target_attrib == NULL)
2578 return MZ_PARAM_ERROR;
2579
2580 *target_attrib = 0;
2581
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002582 if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002583 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002584 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002585 {
2586 *target_attrib = src_attrib;
2587 return MZ_OK;
2588 }
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002589 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (target_sys == MZ_HOST_SYSTEM_RISCOS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002590 return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
2591 }
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002592 else if ((src_sys == MZ_HOST_SYSTEM_UNIX) || (src_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (src_sys == MZ_HOST_SYSTEM_RISCOS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002593 {
Cameron Cawley28f78ff2019-08-06 14:14:14 +01002594 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (target_sys == MZ_HOST_SYSTEM_RISCOS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002595 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002596 /* If high bytes are set, it contains unix specific attributes */
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002597 if ((src_attrib >> 16) != 0)
2598 src_attrib >>= 16;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002599
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002600 *target_attrib = src_attrib;
2601 return MZ_OK;
2602 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002603 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002604 return mz_zip_attrib_posix_to_win32(src_attrib, target_attrib);
2605 }
2606
2607 return MZ_SUPPORT_ERROR;
2608}
2609
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002610int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002611{
2612 if (win32_attrib == NULL)
2613 return MZ_PARAM_ERROR;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002614
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002615 *win32_attrib = 0;
Nathan Moinvaziri32fd8052019-07-04 10:32:02 -07002616
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002617 /* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002618 if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002619 *win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002620 /* S_IFLNK */
2621 if ((posix_attrib & 0170000) == 0120000)
2622 *win32_attrib |= 0x400; /* FILE_ATTRIBUTE_REPARSE_POINT */
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002623 /* S_IFDIR */
Nathan Moinvaziri7d94b1f2019-05-08 23:41:47 -07002624 else if ((posix_attrib & 0170000) == 0040000)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002625 *win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
2626 /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002627 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002628 *win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002629
2630 return MZ_OK;
2631}
2632
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002633int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002634{
2635 if (posix_attrib == NULL)
2636 return MZ_PARAM_ERROR;
2637
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002638 *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
2639 /* FILE_ATTRIBUTE_READONLY */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002640 if ((win32_attrib & 0x01) == 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002641 *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
Nathan Moinvaziri426543d2019-05-07 09:46:25 -07002642 /* FILE_ATTRIBUTE_REPARSE_POINT */
2643 if ((win32_attrib & 0x400) == 0x400)
2644 *posix_attrib |= 0120000; /* S_IFLNK */
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002645 /* FILE_ATTRIBUTE_DIRECTORY */
Nathan Moinvaziri7d94b1f2019-05-08 23:41:47 -07002646 else if ((win32_attrib & 0x10) == 0x10)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002647 *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002648 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002649 *posix_attrib |= 0100000; /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002650
2651 return MZ_OK;
2652}
2653
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002654/***************************************************************************/
2655
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002656int32_t mz_zip_extrafield_find(void *stream, uint16_t type, uint16_t *length)
2657{
2658 int32_t err = MZ_OK;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002659 uint16_t field_type = 0;
2660 uint16_t field_length = 0;
2661
2662 do
2663 {
2664 err = mz_stream_read_uint16(stream, &field_type);
2665 if (err == MZ_OK)
2666 err = mz_stream_read_uint16(stream, &field_length);
2667 if (err != MZ_OK)
2668 break;
2669
2670 if (type == field_type)
2671 {
2672 if (length != NULL)
2673 *length = field_length;
2674 return MZ_OK;
2675 }
2676
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002677 err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR);
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002678 }
2679 while (err == MZ_OK);
2680
2681 return MZ_EXIST_ERROR;
2682}
2683
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002684int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size,
2685 uint16_t type, uint16_t *length)
2686{
2687 void *file_extra_stream = NULL;
2688 int32_t err = MZ_OK;
2689
2690 if (extrafield == NULL || extrafield_size == 0)
2691 return MZ_PARAM_ERROR;
2692
2693 mz_stream_mem_create(&file_extra_stream);
2694 mz_stream_mem_set_buffer(file_extra_stream, (void *)extrafield, extrafield_size);
2695
2696 err = mz_zip_extrafield_find(file_extra_stream, type, length);
2697
2698 mz_stream_mem_delete(&file_extra_stream);
2699
2700 return err;
2701}
2702
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002703int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length)
2704{
2705 int32_t err = MZ_OK;
2706 if (type == NULL || length == NULL)
2707 return MZ_PARAM_ERROR;
2708 err = mz_stream_read_uint16(stream, type);
2709 if (err == MZ_OK)
2710 err = mz_stream_read_uint16(stream, length);
2711 return err;
2712}
2713
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002714int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002715{
2716 int32_t err = MZ_OK;
2717 err = mz_stream_write_uint16(stream, type);
2718 if (err == MZ_OK)
2719 err = mz_stream_write_uint16(stream, length);
2720 return err;
2721}
2722
2723/***************************************************************************/
2724
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002725static int32_t mz_zip_invalid_date(const struct tm *ptm)
2726{
2727#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002728 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002729 !datevalue_in_range(0, 11, ptm->tm_mon) ||
2730 !datevalue_in_range(1, 31, ptm->tm_mday) ||
2731 !datevalue_in_range(0, 23, ptm->tm_hour) ||
2732 !datevalue_in_range(0, 59, ptm->tm_min) ||
2733 !datevalue_in_range(0, 59, ptm->tm_sec));
2734#undef datevalue_in_range
2735}
2736
2737static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
2738{
2739 uint64_t date = (uint64_t)(dos_date >> 16);
2740
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002741 ptm->tm_mday = (uint16_t)(date & 0x1f);
2742 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
2743 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
2744 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
2745 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
2746 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002747 ptm->tm_isdst = -1;
2748}
2749
2750int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
2751{
2752 if (ptm == NULL)
2753 return MZ_PARAM_ERROR;
2754
2755 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
2756
2757 if (mz_zip_invalid_date(ptm))
2758 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002759 /* Invalid date stored, so don't return it */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002760 memset(ptm, 0, sizeof(struct tm));
2761 return MZ_FORMAT_ERROR;
2762 }
2763 return MZ_OK;
2764}
2765
2766time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
2767{
2768 struct tm ptm;
2769 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
2770 return mktime(&ptm);
2771}
2772
2773int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
2774{
Nathan Moinvaziricd3e9e12019-04-28 10:22:49 -07002775 struct tm ltm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002776 if (ptm == NULL)
2777 return MZ_PARAM_ERROR;
Nathan Moinvaziri2e0a20a2019-04-28 13:03:11 -07002778 if (localtime_r(&unix_time, &ltm) == NULL) /* Returns a 1900-based year */
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002779 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002780 /* Invalid date stored, so don't return it */
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002781 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002782 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002783 }
Nathan Moinvaziricd3e9e12019-04-28 10:22:49 -07002784 memcpy(ptm, &ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002785 return MZ_OK;
2786}
2787
2788uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
2789{
2790 struct tm ptm;
2791 mz_zip_time_t_to_tm(unix_time, &ptm);
2792 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
2793}
2794
2795uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
2796{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07002797 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002798
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002799 /* Years supported: */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002800
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002801 /* [00, 79] (assumed to be between 2000 and 2079) */
2802 /* [80, 207] (assumed to be between 1980 and 2107, typical output of old */
2803 /* software that does 'year-1900' to get a double digit year) */
2804 /* [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.) */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002805
2806 memcpy(&fixed_tm, ptm, sizeof(struct tm));
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002807 if (fixed_tm.tm_year >= 1980) /* range [1980, 2107] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002808 fixed_tm.tm_year -= 1980;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002809 else if (fixed_tm.tm_year >= 80) /* range [80, 207] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002810 fixed_tm.tm_year -= 80;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002811 else /* range [00, 79] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002812 fixed_tm.tm_year += 20;
2813
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002814 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002815 return 0;
2816
Anand K Mistry57b65f82018-11-09 10:52:19 +11002817 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 -07002818 (((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 -07002819}
2820
2821int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
2822{
2823 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
2824 return MZ_OK;
2825}
2826
2827int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
2828{
2829 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
2830 return MZ_OK;
2831}
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002832
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002833/***************************************************************************/
2834
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002835int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case)
2836{
2837 do
2838 {
2839 if ((*path1 == '\\' && *path2 == '/') ||
2840 (*path2 == '\\' && *path1 == '/'))
2841 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002842 /* Ignore comparison of path slashes */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002843 }
2844 else if (ignore_case)
2845 {
2846 if (tolower(*path1) != tolower(*path2))
2847 break;
2848 }
2849 else if (*path1 != *path2)
2850 {
2851 break;
2852 }
2853
2854 path1 += 1;
2855 path2 += 1;
2856 }
2857 while (*path1 != 0 && *path2 != 0);
2858
2859 if (ignore_case)
2860 return (int32_t)(tolower(*path1) - tolower(*path2));
2861
2862 return (int32_t)(*path1 - *path2);
2863}
2864
2865/***************************************************************************/