blob: 50c03a501b85b070de8d6a180bddf85e2eaef0fb [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvazirib2b082c2018-11-13 15:22:15 -08002 Version 2.7.5, November 13, 2018
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08003 part of the MiniZip project
4
Nathan Moinvazirifee885a2018-01-06 08:49:03 -08005 Copyright (C) 2010-2018 Nathan Moinvaziri
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08006 https://github.com/nmoinvaz/minizip
Nathan Moinvaziri79cfab02018-08-17 12:21:06 -07007 Copyright (C) 2009-2010 Mathias Svensson
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08008 Modifications for Zip64 support
9 http://result42.com
Nathan Moinvaziri79cfab02018-08-17 12:21:06 -070010 Copyright (C) 2007-2008 Even Rouault
11 Modifications of Unzip for Zip64
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080012 Copyright (C) 1998-2010 Gilles Vollant
Viktor Szakats9dea6f22018-10-17 22:39:01 +000013 https://www.winimage.com/zLibDll/minizip.html
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080014
15 This program is distributed under the terms of the same license as zlib.
16 See the accompanying LICENSE file for the full text of the license.
17*/
18
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080019
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070020#include "mz.h"
Nathan Moinvaziri5f091882018-10-24 18:06:08 -070021#include "mz_crypt.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080022#include "mz_strm.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080023#ifdef HAVE_BZIP2
24# include "mz_strm_bzip.h"
25#endif
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -080026#ifdef HAVE_LIBCOMP
Nathan Moinvaziri90d31c72018-11-20 03:17:20 -080027# include "mz_strm_libcomp.h"
28#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080029#ifdef HAVE_LZMA
30# include "mz_strm_lzma.h"
31#endif
Nathan Moinvazirib47b41b2018-07-11 09:44:29 -070032#include "mz_strm_mem.h"
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -070033#ifdef HAVE_PKCRYPT
34# include "mz_strm_pkcrypt.h"
35#endif
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -080036#ifdef HAVE_WZAES
Nathan Moinvaziri21a31022018-10-24 09:50:16 -070037# include "mz_strm_wzaes.h"
38#endif
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -080039#ifdef HAVE_ZLIB
40# include "mz_strm_zlib.h"
41#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080042
43#include "mz_zip.h"
44
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080045#include <ctype.h> /* tolower */
Nathan Moinvazirie8496272018-11-21 15:05:58 -080046#include <stdio.h> /* snprintf */
Nathan Moinvaziri1ee609b2018-11-19 21:34:35 -080047
Nathan Moinvazirie8496272018-11-21 15:05:58 -080048#if defined(_MSC_VER) && (_MSC_VER < 1900)
Nathan Moinvaziric565fa82018-10-19 08:48:33 -070049# define snprintf _snprintf
50#endif
51
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080052/***************************************************************************/
53
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070054#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
55#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
56#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080057#define MZ_ZIP_MAGIC_ENDHEADERU8 { 0x50, 0x4b, 0x05, 0x06 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070058#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
59#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
60#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080061#define MZ_ZIP_MAGIC_DATADESCRIPTORU8 { 0x50, 0x4b, 0x07, 0x08 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070062
Nathan Moinvaziriba1db162018-11-23 10:07:35 -080063#define MZ_ZIP_SIZE_LD_ITEM (30)
Nathan Moinvaziri638f31f2018-08-27 08:17:16 -070064#define MZ_ZIP_SIZE_CD_ITEM (46)
65#define MZ_ZIP_SIZE_CD_LOCATOR64 (20)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080066
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080067/***************************************************************************/
68
69typedef struct mz_zip_s
70{
71 mz_zip_file file_info;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070072 mz_zip_file local_file_info;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080073
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080074 void *stream; /* main stream */
75 void *cd_stream; /* pointer to the stream with the cd */
76 void *cd_mem_stream; /* memory stream for central directory */
77 void *compress_stream; /* compression stream */
78 void *crypt_stream; /* encryption stream */
79 void *file_info_stream; /* memory stream for storing file info */
80 void *local_file_info_stream; /* memory stream for storing local file info */
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080081
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070082 int32_t open_mode;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -080083 uint8_t recover;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070084
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080085 uint32_t disk_number_with_cd; /* number of the disk with the central dir */
86 int64_t disk_offset_shift; /* correction for zips that have wrong offset start of cd */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070087
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080088 int64_t cd_start_pos; /* pos of the first file in the central dir stream */
89 int64_t cd_current_pos; /* pos of the current file in the central dir */
90 int64_t cd_offset; /* offset of start of central directory */
91 int64_t cd_size; /* size of the central directory */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070092
Nathan Moinvazirib16ab562018-11-20 16:56:21 -080093 uint8_t entry_scanned; /* entry header information read ok */
94 uint8_t entry_opened; /* entry is open for read/write */
95 uint8_t entry_raw; /* entry opened with raw mode */
96 uint32_t entry_crc32; /* entry crc32 */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070097
Nathan Moinvaziri904c4082018-10-08 23:31:21 -070098 uint64_t number_entry;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070099
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700100 uint16_t version_madeby;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700101 char *comment;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800102} mz_zip;
103
104/***************************************************************************/
105
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800106#if 0
107# define mz_zip_print printf
108#else
109# define mz_zip_print(fmt,...)
110#endif
111
112/***************************************************************************/
113
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800114/* Locate the end of central directory */
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700115static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800116{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700117 int64_t file_size = 0;
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800118 int64_t max_back = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800119 uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8;
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700120 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700121
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700122 err = mz_stream_seek(stream, 0, MZ_SEEK_END);
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800123 if (err != MZ_OK)
124 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800125
126 file_size = mz_stream_tell(stream);
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800127
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800128 /* Maximum seek is size of global comment + extra */
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800129 max_back = UINT16_MAX + 128;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800130 if (max_back > file_size)
131 max_back = file_size;
132
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800133 return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800134}
135
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800136/* Locate the end of central directory 64 of a zip file */
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700137static 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 +0800138{
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700139 int64_t offset = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800140 uint32_t value32 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700141 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700142
143
144 *central_pos = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800145
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800146 /* Zip64 end of central directory locator */
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700147 err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800148 /* Read locator signature */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700149 if (err == MZ_OK)
150 {
151 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700152 if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700153 err = MZ_FORMAT_ERROR;
154 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800155 /* Number of the disk with the start of the zip64 end of central directory */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700156 if (err == MZ_OK)
157 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800158 /* Relative offset of the zip64 end of central directory record8 */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700159 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700160 err = mz_stream_read_uint64(stream, (uint64_t *)&offset);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800161 /* Total number of disks */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700162 if (err == MZ_OK)
163 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800164 /* Goto end of central directory record */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700165 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700166 err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800167 /* The signature */
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700168 if (err == MZ_OK)
169 {
170 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700171 if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700172 err = MZ_FORMAT_ERROR;
173 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800174
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700175 if (err == MZ_OK)
176 *central_pos = offset;
177
178 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800179}
180
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800181/* Get info about the current file in the zip file */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700182static 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 -0700183{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700184 uint64_t ntfs_time = 0;
185 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700186 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700187 uint32_t dos_date = 0;
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800188 uint32_t field_pos = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700189 uint16_t field_type = 0;
190 uint16_t field_length = 0;
191 uint32_t field_length_read = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700192 uint16_t ntfs_attrib_id = 0;
193 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700194 uint16_t value16 = 0;
195 uint32_t value32 = 0;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800196 int64_t extrafield_pos = -1;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800197 int64_t comment_pos = -1;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700198 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700199
200
201 memset(file_info, 0, sizeof(mz_zip_file));
202
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800203 /* Check the magic */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700204 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700205 if (err == MZ_END_OF_STREAM)
206 err = MZ_END_OF_LIST;
207 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700208 err = MZ_END_OF_LIST;
209 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
210 err = MZ_FORMAT_ERROR;
211 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
212 err = MZ_FORMAT_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000213
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800214 /* Read header fields */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700215 if (err == MZ_OK)
216 {
217 if (!local)
218 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
219 if (err == MZ_OK)
220 err = mz_stream_read_uint16(stream, &file_info->version_needed);
221 if (err == MZ_OK)
222 err = mz_stream_read_uint16(stream, &file_info->flag);
223 if (err == MZ_OK)
224 err = mz_stream_read_uint16(stream, &file_info->compression_method);
225 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700226 {
227 err = mz_stream_read_uint32(stream, &dos_date);
228 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
229 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700230 if (err == MZ_OK)
231 err = mz_stream_read_uint32(stream, &file_info->crc);
232 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700233 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700234 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700235 file_info->compressed_size = value32;
236 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700237 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700238 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700239 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700240 file_info->uncompressed_size = value32;
241 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700242 if (err == MZ_OK)
243 err = mz_stream_read_uint16(stream, &file_info->filename_size);
244 if (err == MZ_OK)
245 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
246 if (!local)
247 {
248 if (err == MZ_OK)
249 err = mz_stream_read_uint16(stream, &file_info->comment_size);
250 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700251 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700252 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700253 file_info->disk_number = value16;
254 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700255 if (err == MZ_OK)
256 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
257 if (err == MZ_OK)
258 err = mz_stream_read_uint32(stream, &file_info->external_fa);
259 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700260 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700261 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700262 file_info->disk_offset = value32;
263 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700264 }
265 }
266
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700267 if (err == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800268 err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700269
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800270 /* Copy variable length data to memory stream for later retrieval */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700271 if ((err == MZ_OK) && (file_info->filename_size > 0))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700272 err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800273 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800274 extrafield_pos = (int64_t)file_info->filename_size + 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700275
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800276 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
277 err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800278 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800279
280 comment_pos = extrafield_pos + (int64_t)file_info->extrafield_size + 1;
281 if ((err == MZ_OK) && (file_info->comment_size > 0))
282 err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800283 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800284
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700285 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
286 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800287 /* Seek to and parse the extra field */
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800288 err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700289
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800290 while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700291 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700292 err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length);
Nathan Moinvaziriea2bd792018-11-19 17:44:44 -0800293 if (err != MZ_OK)
294 break;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800295 field_pos += 4;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800296
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800297 /* Don't allow field length to exceed size of remaining extrafield */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800298 if (field_length > (file_info->extrafield_size - field_pos))
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -0800299 field_length = (uint16_t)(file_info->extrafield_size - field_pos);
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800300
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800301 /* Read ZIP64 extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800302 if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700303 {
304 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800305 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700306 err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800307 if (file_info->uncompressed_size < 0)
308 err = MZ_FORMAT_ERROR;
309 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700310 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800311 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700312 err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800313 if (file_info->compressed_size < 0)
314 err = MZ_FORMAT_ERROR;
315 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700316 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800317 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700318 err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800319 if (file_info->disk_offset < 0)
320 err = MZ_FORMAT_ERROR;
321 }
juanii4ae79922018-02-11 14:29:36 -0300322 if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700323 err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700324 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800325 /* Read NTFS extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800326 else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700327 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700328 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700329 err = mz_stream_read_uint32(file_extra_stream, &reserved);
330 field_length_read = 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700331
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800332 while ((err == MZ_OK) && (field_length_read + 4 <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700333 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700334 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700335 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700336 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800337 field_length_read += 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700338
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700339 if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700340 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700341 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700342 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
343
juanii7063b0e2018-02-11 13:56:21 -0300344 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700345 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700346 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700347 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
348 }
juanii7063b0e2018-02-11 13:56:21 -0300349 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700350 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700351 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700352 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
353 }
354 }
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800355 else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700356 {
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800357 err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700358 }
359
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800360 field_length_read += ntfs_attrib_size;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700361 }
362 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800363 /* Read UNIX1 extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800364 else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700365 {
366 if (err == MZ_OK && file_info->accessed_date == 0)
367 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700368 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700369 if (err == MZ_OK)
370 file_info->accessed_date = value32;
371 }
372 if (err == MZ_OK && file_info->modified_date == 0)
373 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700374 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700375 if (err == MZ_OK)
376 file_info->modified_date = value32;
377 }
378 if (err == MZ_OK)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800379 err = mz_stream_read_uint16(file_extra_stream, &value16); /* User id */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700380 if (err == MZ_OK)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800381 err = mz_stream_read_uint16(file_extra_stream, &value16); /* Group id */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700382
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800383 /* Skip variable data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800384 mz_stream_seek(file_extra_stream, field_length - 12, MZ_SEEK_CUR);
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700385 }
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800386#ifdef HAVE_WZAES
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800387 /* Read AES extra field */
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800388 else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700389 {
390 uint8_t value8 = 0;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800391 /* Verify version info */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700392 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800393 /* Support AE-1 and AE-2 */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700394 if (value16 != 1 && value16 != 2)
395 err = MZ_FORMAT_ERROR;
396 file_info->aes_version = value16;
397 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700398 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700399 if ((char)value8 != 'A')
400 err = MZ_FORMAT_ERROR;
401 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700402 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700403 if ((char)value8 != 'E')
404 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800405 /* Get AES encryption strength and actual compression method */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700406 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700407 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700408 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700409 file_info->aes_encryption_mode = value8;
410 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700411 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700412 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700413 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700414 file_info->compression_method = value16;
415 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700416 }
417#endif
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800418 else if (field_length > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700419 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700420 err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700421 }
422
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800423 field_pos += field_length;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700424 }
425 }
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800426
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800427 /* Get pointers to variable length data */
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800428 mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename);
429 if (extrafield_pos >= 0)
430 mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield);
431 if (comment_pos >= 0)
432 mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment);
433
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800434 /* Set to empty string just in-case */
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800435 if (file_info->filename == NULL)
436 file_info->filename = "";
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800437 if (file_info->extrafield == NULL)
438 file_info->extrafield_size = 0;
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800439 if (file_info->comment == NULL)
440 file_info->comment = "";
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700441
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800442 if (err == MZ_OK)
443 {
444 mz_zip_print("Zip - Entry - Read header - %s (local %"PRId8")\n",
445 file_info->filename, local);
Nathan Moinvaziri264dc182018-11-13 17:42:13 -0800446 mz_zip_print("Zip - Entry - Read header compress (ucs %"PRId64" cs %"PRId64" crc 0x%08"PRIx32")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800447 file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
448 if (!local)
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800449 {
450 mz_zip_print("Zip - Entry - Read header disk (disk %"PRIu32" offset %"PRId64")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800451 file_info->disk_number, file_info->disk_offset);
Nathan Moinvaziria93a2ad2018-11-13 17:51:42 -0800452 }
Nathan Moinvaziri264dc182018-11-13 17:42:13 -0800453 mz_zip_print("Zip - Entry - Read header variable (fnl %"PRId32" efs %"PRId32" cms %"PRId32")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800454 file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
455 }
456
457 return err;
458}
459
460static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size)
461{
462 uint32_t value32 = 0;
463 int64_t value64 = 0;
464 int32_t err = MZ_OK;
465
466
467 err = mz_stream_read_uint32(stream, &value32);
468 if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR)
469 err = MZ_FORMAT_ERROR;
470 if (err == MZ_OK)
471 err = mz_stream_read_uint32(stream, &value32);
472 if ((err == MZ_OK) && (crc32 != NULL))
473 *crc32 = value32;
474 if (err == MZ_OK)
475 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800476 /* If zip 64 extension is enabled then read as 8 byte */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800477 if (!zip64)
478 {
479 err = mz_stream_read_uint32(stream, &value32);
480 value64 = value32;
481 }
482 else
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800483 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800484 err = mz_stream_read_int64(stream, &value64);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800485 if (value64 < 0)
486 err = MZ_FORMAT_ERROR;
487 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800488 if ((err == MZ_OK) && (compressed_size != NULL))
489 *compressed_size = value64;
490 }
491 if (err == MZ_OK)
492 {
493 if (!zip64)
494 {
495 err = mz_stream_read_uint32(stream, &value32);
496 value64 = value32;
497 }
498 else
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800499 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800500 err = mz_stream_read_int64(stream, &value64);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800501 if (value64 < 0)
502 err = MZ_FORMAT_ERROR;
503 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800504 if ((err == MZ_OK) && (uncompressed_size != NULL))
505 *uncompressed_size = value64;
506 }
507
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700508 return err;
509}
510
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700511static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700512{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700513 uint64_t ntfs_time = 0;
514 uint32_t reserved = 0;
515 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700516 uint16_t extrafield_size = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700517 uint16_t field_type = 0;
518 uint16_t field_length = 0;
519 uint16_t field_length_zip64 = 0;
520 uint16_t field_length_ntfs = 0;
521 uint16_t field_length_aes = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700522 uint16_t filename_size = 0;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700523 uint16_t filename_length = 0;
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700524 uint16_t version_needed = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800525 int32_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700526 int32_t err = MZ_OK;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700527 int32_t err_mem = MZ_OK;
528 uint8_t zip64 = 0;
529 uint8_t skip_aes = 0;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700530 uint8_t mask = 0;
531 uint8_t write_end_slash = 0;
532 const char *filename = NULL;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700533 char masked_name[64];
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700534 void *file_extra_stream = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700535
536 if (file_info == NULL)
537 return MZ_PARAM_ERROR;
538
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700539 if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO))
540 mask = 1;
541
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800542 /* Calculate extra field sizes */
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700543 if (file_info->uncompressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700544 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700545 if (file_info->compressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700546 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700547 if (file_info->disk_offset >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700548 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700549
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700550 if (file_info->zip64 == MZ_ZIP64_AUTO)
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700551 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800552 /* If uncompressed size is unknown, assume zip64 for 64-bit data descriptors */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700553 zip64 = (local && file_info->uncompressed_size == 0) || (field_length_zip64 > 0);
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700554 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700555 else if (file_info->zip64 == MZ_ZIP64_FORCE)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700556 {
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700557 zip64 = 1;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700558 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700559 else if (file_info->zip64 == MZ_ZIP64_DISABLE)
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700560 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800561 /* Zip64 extension is required to zip file */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700562 if (field_length_zip64 > 0)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700563 return MZ_PARAM_ERROR;
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700564 }
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700565
566 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700567 {
568 extrafield_size += 4;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700569 extrafield_size += field_length_zip64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700570 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700571
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800572 /* Calculate extra field size and check for duplicates */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700573 if (file_info->extrafield_size > 0)
574 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700575 mz_stream_mem_create(&file_extra_stream);
Nathan Moinvaziri915c5132018-10-26 20:00:52 -0700576 mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
577 file_info->extrafield_size);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700578
579 do
580 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700581 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700582 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700583 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700584 if (err_mem != MZ_OK)
585 break;
586
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800587 /* Prefer incoming aes extensions over ours */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700588 if (field_type == MZ_ZIP_EXTENSION_AES)
589 skip_aes = 1;
590
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800591 /* Prefer our zip64, ntfs extension over incoming */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700592 if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS)
593 extrafield_size += 4 + field_length;
594
595 if (err_mem == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800596 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700597 }
598 while (err_mem == MZ_OK);
599 }
600
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800601#ifdef HAVE_WZAES
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700602 if (!skip_aes)
603 {
604 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700605 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700606 field_length_aes = 1 + 1 + 1 + 2 + 2;
607 extrafield_size += 4 + field_length_aes;
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700608 }
609 }
Nathan Moinvaziria2bb08c2018-10-28 13:45:30 -0700610#else
Nathan Moinvaziri7de94cf2018-11-19 20:51:06 -0800611 MZ_UNUSED(field_length_aes);
Nathan Moinvaziri4ae469a2018-10-28 15:32:04 -0700612 MZ_UNUSED(skip_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700613#endif
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800614 /* NTFS timestamps */
Nathan Moinvazirid9e159a2018-02-13 15:30:30 -0800615 if ((file_info->modified_date != 0) &&
616 (file_info->accessed_date != 0) &&
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700617 (file_info->creation_date != 0) && (!mask))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700618 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700619 field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
620 extrafield_size += 4 + field_length_ntfs;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700621 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700622
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700623 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700624 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700625 else
626 {
627 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
628 if (err == MZ_OK)
629 err = mz_stream_write_uint16(stream, file_info->version_madeby);
630 }
631
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800632 /* Calculate version needed to extract */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700633 if (err == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700634 {
635 version_needed = file_info->version_needed;
636 if (version_needed == 0)
637 {
638 version_needed = 20;
639 if (zip64)
640 version_needed = 45;
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800641#ifdef HAVE_WZAES
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700642 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
643 version_needed = 51;
644#endif
645#ifdef HAVE_LZMA
646 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
647 version_needed = 63;
648#endif
649 }
650 err = mz_stream_write_uint16(stream, version_needed);
651 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700652 if (err == MZ_OK)
653 err = mz_stream_write_uint16(stream, file_info->flag);
654 if (err == MZ_OK)
655 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800656#ifdef HAVE_WZAES
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700657 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700658 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700659 else
660#endif
661 err = mz_stream_write_uint16(stream, file_info->compression_method);
662 }
663 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700664 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700665 if (file_info->modified_date != 0 && !mask)
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -0700666 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700667 err = mz_stream_write_uint32(stream, dos_date);
668 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700669
670 if (err == MZ_OK)
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700671 {
672 if (mask)
673 err = mz_stream_write_uint32(stream, 0);
674 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800675 err = mz_stream_write_uint32(stream, file_info->crc); /* crc */
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700676 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700677 if (err == MZ_OK)
678 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800679 if (file_info->compressed_size >= UINT32_MAX) /* compr size */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700680 err = mz_stream_write_uint32(stream, UINT32_MAX);
681 else
682 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
683 }
684 if (err == MZ_OK)
685 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800686 if (file_info->uncompressed_size >= UINT32_MAX) /* uncompr size */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700687 err = mz_stream_write_uint32(stream, UINT32_MAX);
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700688 else if (mask)
689 err = mz_stream_write_uint32(stream, 0);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700690 else
691 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
692 }
693
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700694 if (mask)
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700695 {
Nathan Moinvaziri264dc182018-11-13 17:42:13 -0800696 snprintf(masked_name, sizeof(masked_name), "%"PRIx32"_%"PRIx64,
697 file_info->disk_number, file_info->disk_offset);
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700698 filename = masked_name;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700699 }
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700700 else
701 {
702 filename = file_info->filename;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700703 }
704
705 filename_length = (uint16_t)strlen(filename);
706 filename_size += filename_length;
707
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800708 if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) &&
709 ((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\')))
710 {
711 filename_size += 1;
712 write_end_slash = 1;
713 }
714
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700715 if (err == MZ_OK)
716 err = mz_stream_write_uint16(stream, filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700717 if (err == MZ_OK)
718 err = mz_stream_write_uint16(stream, extrafield_size);
719
720 if (!local)
721 {
722 if (file_info->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800723 {
724 comment_size = (int32_t)strlen(file_info->comment);
725 if (comment_size > UINT16_MAX)
726 comment_size = UINT16_MAX;
727 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700728 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800729 err = mz_stream_write_uint16(stream, (uint16_t)comment_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700730 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700731 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700732 if (err == MZ_OK)
733 err = mz_stream_write_uint16(stream, file_info->internal_fa);
734 if (err == MZ_OK)
735 err = mz_stream_write_uint32(stream, file_info->external_fa);
736 if (err == MZ_OK)
737 {
738 if (file_info->disk_offset >= UINT32_MAX)
739 err = mz_stream_write_uint32(stream, UINT32_MAX);
740 else
741 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
742 }
743 }
744
745 if (err == MZ_OK)
746 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700747 if (mz_stream_write(stream, filename, filename_length) != filename_length)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700748 err = MZ_WRITE_ERROR;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700749
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800750 /* Ensure that directories have a slash appended to them for compatibility */
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700751 if (err == MZ_OK && write_end_slash)
752 err = mz_stream_write_uint8(stream, '/');
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700753 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700754
755 if (file_info->extrafield_size > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700756 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800757 err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700758 while (err == MZ_OK && err_mem == MZ_OK)
759 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700760 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700761 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700762 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700763 if (err_mem != MZ_OK)
764 break;
765
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800766 /* Prefer our zip 64, ntfs extensions over incoming */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700767 if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS)
768 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800769 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700770 continue;
771 }
772
773 err = mz_stream_write_uint16(stream, field_type);
774 if (err == MZ_OK)
775 err = mz_stream_write_uint16(stream, field_length);
776 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700777 err = mz_stream_copy(stream, file_extra_stream, field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700778 }
779
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700780 mz_stream_mem_delete(&file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700781 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700782
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800783 /* Write ZIP64 extra field */
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700784 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700785 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700786 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700787 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700788 {
789 if (mask)
790 err = mz_stream_write_int64(stream, 0);
791 else
792 err = mz_stream_write_int64(stream, file_info->uncompressed_size);
793 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700794 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700795 err = mz_stream_write_int64(stream, file_info->compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700796 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700797 err = mz_stream_write_int64(stream, file_info->disk_offset);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700798 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800799 /* Write NTFS extra field */
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700800 if ((err == MZ_OK) && (field_length_ntfs > 0))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700801 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700802 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700803 if (err == MZ_OK)
804 err = mz_stream_write_uint32(stream, reserved);
805 if (err == MZ_OK)
806 err = mz_stream_write_uint16(stream, 0x01);
807 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700808 err = mz_stream_write_uint16(stream, field_length_ntfs - 8);
juanii3679a3d2018-02-11 13:55:38 -0300809 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700810 {
811 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
812 err = mz_stream_write_uint64(stream, ntfs_time);
813 }
juanii3679a3d2018-02-11 13:55:38 -0300814 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700815 {
816 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
817 err = mz_stream_write_uint64(stream, ntfs_time);
818 }
juanii3679a3d2018-02-11 13:55:38 -0300819 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700820 {
821 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
822 err = mz_stream_write_uint64(stream, ntfs_time);
823 }
824 }
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -0800825#ifdef HAVE_WZAES
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800826 /* Write AES extra field */
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700827 if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700828 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700829 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700830 if (err == MZ_OK)
831 err = mz_stream_write_uint16(stream, file_info->aes_version);
832 if (err == MZ_OK)
833 err = mz_stream_write_uint8(stream, 'A');
834 if (err == MZ_OK)
835 err = mz_stream_write_uint8(stream, 'E');
836 if (err == MZ_OK)
837 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
838 if (err == MZ_OK)
839 err = mz_stream_write_uint16(stream, file_info->compression_method);
840 }
841#endif
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700842 if ((err == MZ_OK) && (!local) && (file_info->comment != NULL))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700843 {
Nathan Moinvazirica014e62018-08-15 07:31:12 -0700844 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700845 err = MZ_WRITE_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700846 }
847
848 return err;
849}
850
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800851static int32_t mz_zip_entry_write_descriptor(void *stream, uint8_t zip64, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800852{
853 int32_t err = MZ_OK;
854
855 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
856 if (err == MZ_OK)
857 err = mz_stream_write_uint32(stream, crc32);
858
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800859 /* Store data descriptor as 8 bytes if zip 64 extension enabled */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800860 if (err == MZ_OK)
861 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800862 /* Zip 64 extension is enabled when uncompressed size is > UINT32_MAX */
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800863 if (!zip64)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800864 err = mz_stream_write_uint32(stream, (uint32_t)compressed_size);
865 else
866 err = mz_stream_write_int64(stream, compressed_size);
867 }
868 if (err == MZ_OK)
869 {
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800870 if (!zip64)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800871 err = mz_stream_write_uint32(stream, (uint32_t)uncompressed_size);
872 else
873 err = mz_stream_write_int64(stream, uncompressed_size);
874 }
875
876 return err;
877}
878
879static int32_t mz_zip_read_cd(void *handle)
880{
881 mz_zip *zip = (mz_zip *)handle;
882 uint64_t number_entry_cd64 = 0;
883 uint64_t number_entry = 0;
884 uint64_t number_entry_cd = 0;
885 int64_t eocd_pos = 0;
886 int64_t eocd_pos64 = 0;
887 int64_t value64i = 0;
888 uint16_t value16 = 0;
889 uint32_t value32 = 0;
890 uint64_t value64 = 0;
891 uint16_t comment_size = 0;
892 int32_t comment_read = 0;
893 int32_t err = MZ_OK;
894
895
896 if (zip == NULL)
897 return MZ_PARAM_ERROR;
898
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800899 /* Read and cache central directory records */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800900 err = mz_zip_search_eocd(zip->stream, &eocd_pos);
901 if (err == MZ_OK)
902 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800903 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800904 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800905 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800906 if (err == MZ_OK)
907 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800908 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800909 if (err == MZ_OK)
910 err = mz_stream_read_uint16(zip->stream, &value16);
911 zip->disk_number_with_cd = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800912 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800913 if (err == MZ_OK)
914 err = mz_stream_read_uint16(zip->stream, &value16);
915 zip->number_entry = value16;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800916 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800917 if (err == MZ_OK)
918 err = mz_stream_read_uint16(zip->stream, &value16);
919 number_entry_cd = value16;
920 if (number_entry_cd != zip->number_entry)
921 err = MZ_FORMAT_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800922 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800923 if (err == MZ_OK)
924 err = mz_stream_read_uint32(zip->stream, &value32);
925 if (err == MZ_OK)
926 zip->cd_size = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800927 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800928 if (err == MZ_OK)
929 err = mz_stream_read_uint32(zip->stream, &value32);
930 if (err == MZ_OK)
931 zip->cd_offset = value32;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800932 /* Zip file global comment length */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800933 if (err == MZ_OK)
934 err = mz_stream_read_uint16(zip->stream, &comment_size);
935 if ((err == MZ_OK) && (comment_size > 0))
936 {
937 zip->comment = (char *)MZ_ALLOC(comment_size + 1);
938 if (zip->comment != NULL)
939 {
940 comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800941 /* Don't fail if incorrect comment length read, not critical */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800942 if (comment_read < 0)
943 comment_read = 0;
944 zip->comment[comment_read] = 0;
945 }
946 }
947
948 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
949 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800950 /* Format should be Zip64, as the central directory or file size is too large */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800951 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
952 {
953 eocd_pos = eocd_pos64;
954
955 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800956 /* The signature, already checked */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800957 if (err == MZ_OK)
958 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800959 /* Size of zip64 end of central directory record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800960 if (err == MZ_OK)
961 err = mz_stream_read_uint64(zip->stream, &value64);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800962 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800963 if (err == MZ_OK)
964 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800965 /* Version needed to extract */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800966 if (err == MZ_OK)
967 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800968 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800969 if (err == MZ_OK)
970 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800971 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800972 if (err == MZ_OK)
973 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800974 /* Total number of entries in the central directory on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800975 if (err == MZ_OK)
976 err = mz_stream_read_uint64(zip->stream, &number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800977 /* Total number of entries in the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800978 if (err == MZ_OK)
979 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
980 if (number_entry == UINT32_MAX)
981 zip->number_entry = number_entry_cd64;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800982 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800983 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800984 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800985 err = mz_stream_read_int64(zip->stream, &zip->cd_size);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800986 if (zip->cd_size < 0)
987 err = MZ_FORMAT_ERROR;
988 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -0800989 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800990 if (err == MZ_OK)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800991 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800992 err = mz_stream_read_int64(zip->stream, &zip->cd_offset);
Nathan Moinvaziriba1db162018-11-23 10:07:35 -0800993 if (zip->cd_offset < 0)
994 err = MZ_FORMAT_ERROR;
995 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800996 }
997 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
998 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
999 {
1000 err = MZ_FORMAT_ERROR;
1001 }
1002 }
1003 }
1004
1005 if (err == MZ_OK)
1006 {
Nathan Moinvaziri264dc182018-11-13 17:42:13 -08001007 mz_zip_print("Zip - Read cd (disk %"PRId32" entries %"PRId64" offset %"PRId64" size %"PRId64")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001008 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1009
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001010 /* Verify central directory signature exists at offset */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001011 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1012 if (err == MZ_OK)
1013 err = mz_stream_read_uint32(zip->stream, &value32);
1014 if (value32 != MZ_ZIP_MAGIC_CENTRALHEADER)
1015 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001016 /* If not found attempt to seek backward to find it */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001017 err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
1018 if (err == MZ_OK)
1019 err = mz_stream_read_uint32(zip->stream, &value32);
1020 if (value32 == MZ_ZIP_MAGIC_CENTRALHEADER)
1021 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001022 /* If found compensate for incorrect locations */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001023 value64i = zip->cd_offset;
1024 zip->cd_offset = eocd_pos - zip->cd_size;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001025 /* Assume disk has prepended data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001026 zip->disk_offset_shift = zip->cd_offset - value64i;
1027 }
1028 }
1029 }
1030
1031 if (err == MZ_OK)
1032 {
1033 if (eocd_pos < zip->cd_offset)
1034 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001035 /* End of central dir should always come after central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001036 err = MZ_FORMAT_ERROR;
1037 }
1038 else if (eocd_pos < zip->cd_offset + zip->cd_size)
1039 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001040 /* Truncate size of cd if incorrect size or offset provided */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001041 zip->cd_size = eocd_pos - zip->cd_offset;
1042 }
1043 }
1044
1045 return err;
1046}
1047
1048static int32_t mz_zip_write_cd(void *handle)
1049{
1050 mz_zip *zip = (mz_zip *)handle;
1051 int64_t zip64_eocd_pos_inzip = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001052 int64_t disk_number = 0;
1053 int64_t disk_size = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001054 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001055 int32_t err = MZ_OK;
1056
1057
1058 if (zip == NULL)
1059 return MZ_PARAM_ERROR;
1060
1061 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
1062 zip->disk_number_with_cd = (uint32_t)disk_number;
1063 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
1064 zip->disk_number_with_cd += 1;
1065 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1066
1067 zip->cd_offset = mz_stream_tell(zip->stream);
1068 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
1069 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
1070 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
1071
1072 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
1073
Nathan Moinvaziri264dc182018-11-13 17:42:13 -08001074 mz_zip_print("Zip - Write cd (disk %"PRId32" entries %"PRId64" offset %"PRId64" size %"PRId64")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001075 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1076
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001077 /* Write the ZIP64 central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001078 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
1079 {
1080 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
1081
1082 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
1083
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001084 /* Size of this 'zip64 end of central directory' */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001085 if (err == MZ_OK)
1086 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001087 /* Version made by */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001088 if (err == MZ_OK)
1089 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001090 /* Version needed */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001091 if (err == MZ_OK)
1092 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001093 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001094 if (err == MZ_OK)
1095 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001096 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001097 if (err == MZ_OK)
1098 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001099 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001100 if (err == MZ_OK)
1101 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001102 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001103 if (err == MZ_OK)
1104 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001105 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001106 if (err == MZ_OK)
1107 err = mz_stream_write_int64(zip->stream, zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001108 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001109 if (err == MZ_OK)
1110 err = mz_stream_write_int64(zip->stream, zip->cd_offset);
1111 if (err == MZ_OK)
1112 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
1113
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001114 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001115 if (err == MZ_OK)
1116 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001117 /* Relative offset to the end of zip64 central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001118 if (err == MZ_OK)
1119 err = mz_stream_write_int64(zip->stream, zip64_eocd_pos_inzip);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001120 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001121 if (err == MZ_OK)
1122 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
1123 }
1124
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001125 /* Write the central directory header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001126
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001127 /* Signature */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001128 if (err == MZ_OK)
1129 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001130 /* Number of this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001131 if (err == MZ_OK)
1132 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001133 /* Number of the disk with the start of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001134 if (err == MZ_OK)
1135 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001136 /* Total number of entries in the central dir on this disk */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001137 if (err == MZ_OK)
1138 {
1139 if (zip->number_entry >= UINT16_MAX)
1140 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1141 else
1142 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1143 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001144 /* Total number of entries in the central dir */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001145 if (err == MZ_OK)
1146 {
1147 if (zip->number_entry >= UINT16_MAX)
1148 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1149 else
1150 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1151 }
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001152 /* Size of the central directory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001153 if (err == MZ_OK)
1154 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001155 /* Offset of start of central directory with respect to the starting disk number */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001156 if (err == MZ_OK)
1157 {
1158 if (zip->cd_offset >= UINT32_MAX)
1159 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
1160 else
1161 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
1162 }
1163
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001164 /* Write global comment */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001165 if (zip->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001166 {
1167 comment_size = (int32_t)strlen(zip->comment);
1168 if (comment_size > UINT16_MAX)
1169 comment_size = UINT16_MAX;
1170 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001171 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001172 err = mz_stream_write_uint16(zip->stream, (uint16_t)comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001173 if (err == MZ_OK)
1174 {
1175 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
1176 err = MZ_READ_ERROR;
1177 }
1178 return err;
1179}
1180
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001181static int32_t mz_zip_recover_cd(void *handle)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001182{
1183 mz_zip *zip = (mz_zip *)handle;
1184 mz_zip_file local_file_info;
1185 void *local_file_info_stream = NULL;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001186 void *cd_mem_stream = NULL;
1187 uint64_t number_entry = 0;
1188 int64_t descriptor_pos = 0;
1189 int64_t disk_offset = 0;
1190 int64_t disk_number = 0;
1191 int64_t compressed_size = 0;
1192 int64_t uncompressed_size = 0;
1193 uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8;
1194 uint32_t crc32 = 0;
1195 int32_t disk_number_with_cd = 0;
1196 int32_t err = MZ_OK;
1197 uint8_t zip64 = 0;
1198
1199
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001200 mz_zip_print("Zip - Recover cd\n");
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001201
1202 mz_zip_get_cd_mem_stream(handle, &cd_mem_stream);
1203
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001204 /* Determine if we are on a split disk or not */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001205 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0);
1206 if (mz_stream_tell(zip->stream) < 0)
1207 {
1208 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1209 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1210 }
1211 else
1212 disk_number_with_cd = 1;
1213
1214 if (mz_stream_is_open(cd_mem_stream) != MZ_OK)
1215 err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1216
1217 mz_stream_mem_create(&local_file_info_stream);
1218 mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1219
1220 while (err == MZ_OK)
1221 {
1222 memset(&local_file_info, 0, sizeof(local_file_info));
1223
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001224 /* Get current offset and disk number for central dir record */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001225 disk_offset = mz_stream_tell(zip->stream);
1226 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1227
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001228 /* Read local headers */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001229 err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream);
1230
1231 local_file_info.disk_offset = disk_offset;
1232 if (disk_number < 0)
1233 disk_number = 0;
1234 local_file_info.disk_number = (uint32_t)disk_number;
1235
1236 if (err == MZ_OK)
1237 {
1238 if (local_file_info.compressed_size > 0)
1239 {
1240 err = mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR);
1241 }
1242 else if (local_file_info.uncompressed_size > 0)
1243 {
1244 err = mz_stream_find(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
1245 INT64_MAX, &descriptor_pos);
1246 }
1247 }
1248
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001249 /* Read descriptor if it exists so we can get to the next local header */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001250 if ((err == MZ_OK) && (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
1251 {
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001252 if (mz_zip_extrafield_contains(local_file_info.extrafield,
1253 local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001254 zip64 = 1;
1255
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001256 err = mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32,
1257 &compressed_size, &uncompressed_size);
1258
1259 if (local_file_info.crc == 0)
1260 local_file_info.crc = crc32;
1261 if (local_file_info.compressed_size == 0)
1262 local_file_info.compressed_size = compressed_size;
1263 if (local_file_info.uncompressed_size == 0)
1264 local_file_info.uncompressed_size = uncompressed_size;
1265 }
1266
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001267 /* Rewrite central dir with local headers and offsets */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001268 if (err == MZ_OK)
1269 err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
1270
1271 if (err == MZ_OK)
1272 number_entry += 1;
1273 }
1274
1275 mz_stream_mem_delete(&local_file_info_stream);
1276
Nathan Moinvaziri264dc182018-11-13 17:42:13 -08001277 mz_zip_print("Zip - Recover cd complete (cddisk %"PRId32" entries %"PRId64")\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001278 disk_number_with_cd, number_entry);
1279
1280 if (number_entry == 0)
1281 return err;
1282
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001283 /* Set new upper seek boundary for central dir mem stream */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001284 disk_offset = mz_stream_tell(cd_mem_stream);
1285 mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset);
1286
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001287 /* Set new central directory info */
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001288 mz_zip_set_cd_stream(handle, 0, cd_mem_stream);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001289 mz_zip_set_number_entry(handle, number_entry);
1290 mz_zip_set_disk_number_with_cd(handle, disk_number_with_cd);
1291
1292 return MZ_OK;
1293}
1294
1295void *mz_zip_create(void **handle)
1296{
1297 mz_zip *zip = NULL;
1298
1299 zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
1300 if (zip != NULL)
1301 memset(zip, 0, sizeof(mz_zip));
1302 if (handle != NULL)
1303 *handle = zip;
1304
1305 return zip;
1306}
1307
1308void mz_zip_delete(void **handle)
1309{
1310 mz_zip *zip = NULL;
1311 if (handle == NULL)
1312 return;
1313 zip = (mz_zip *)*handle;
1314 if (zip != NULL)
1315 {
1316 MZ_FREE(zip);
1317 }
1318 *handle = NULL;
1319}
1320
1321int32_t mz_zip_open(void *handle, void *stream, int32_t mode)
1322{
1323 mz_zip *zip = (mz_zip *)handle;
1324 int32_t err = MZ_OK;
1325
1326
1327 if (zip == NULL)
1328 return MZ_PARAM_ERROR;
1329
1330 mz_zip_print("Zip - Open\n");
1331
1332 zip->stream = stream;
1333
1334 mz_stream_mem_create(&zip->cd_mem_stream);
1335
1336 if (mode & MZ_OPEN_MODE_WRITE)
1337 {
1338 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1339 zip->cd_stream = zip->cd_mem_stream;
1340 }
1341 else
1342 {
1343 zip->cd_stream = stream;
1344 }
1345
1346 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
1347 {
1348 if ((mode & MZ_OPEN_MODE_CREATE) == 0)
1349 {
1350 err = mz_zip_read_cd(zip);
1351 if (err != MZ_OK)
1352 {
Nathan Moinvaziri264dc182018-11-13 17:42:13 -08001353 mz_zip_print("Zip - Error detected reading cd (%"PRId32")", err);
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001354 if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001355 err = MZ_OK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001356 }
1357 }
1358
1359 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
1360 {
1361 if (zip->cd_size > 0)
1362 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001363 /* Store central directory in memory */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001364 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1365 if (err == MZ_OK)
1366 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size);
1367 if (err == MZ_OK)
1368 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1369 }
1370 else
1371 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001372 /* If no central directory, append new zip to end of file */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001373 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1374 }
1375 }
1376 else
1377 {
1378 zip->cd_start_pos = zip->cd_offset;
1379 }
1380 }
1381
1382 if (err != MZ_OK)
1383 {
1384 mz_zip_close(zip);
1385 return err;
1386 }
1387
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001388 /* Memory streams used to store variable length file info data */
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001389 mz_stream_mem_create(&zip->file_info_stream);
1390 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1391
1392 mz_stream_mem_create(&zip->local_file_info_stream);
1393 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1394
1395 zip->open_mode = mode;
1396
1397 return err;
1398}
1399
1400int32_t mz_zip_close(void *handle)
1401{
1402 mz_zip *zip = (mz_zip *)handle;
1403 int32_t err = MZ_OK;
1404
1405 if (zip == NULL)
1406 return MZ_PARAM_ERROR;
1407
1408 mz_zip_print("Zip - Close\n");
1409
1410 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001411 err = mz_zip_entry_close(handle);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001412
1413 if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE))
1414 err = mz_zip_write_cd(handle);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001415
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001416 if (zip->cd_mem_stream != NULL)
1417 {
1418 mz_stream_close(zip->cd_mem_stream);
1419 mz_stream_delete(&zip->cd_mem_stream);
1420 }
1421
1422 if (zip->file_info_stream != NULL)
1423 {
1424 mz_stream_mem_close(zip->file_info_stream);
1425 mz_stream_mem_delete(&zip->file_info_stream);
1426 }
1427 if (zip->local_file_info_stream != NULL)
1428 {
1429 mz_stream_mem_close(zip->local_file_info_stream);
1430 mz_stream_mem_delete(&zip->local_file_info_stream);
1431 }
1432
1433 if (zip->comment)
1434 {
1435 MZ_FREE(zip->comment);
1436 zip->comment = NULL;
1437 }
1438
1439 zip->stream = NULL;
1440 zip->cd_stream = NULL;
1441
1442 return err;
1443}
1444
1445int32_t mz_zip_get_comment(void *handle, const char **comment)
1446{
1447 mz_zip *zip = (mz_zip *)handle;
1448 if (zip == NULL || comment == NULL)
1449 return MZ_PARAM_ERROR;
1450 if (zip->comment == NULL)
1451 return MZ_EXIST_ERROR;
1452 *comment = zip->comment;
1453 return MZ_OK;
1454}
1455
1456int32_t mz_zip_set_comment(void *handle, const char *comment)
1457{
1458 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001459 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001460 if (zip == NULL || comment == NULL)
1461 return MZ_PARAM_ERROR;
1462 if (zip->comment != NULL)
1463 MZ_FREE(zip->comment);
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001464 comment_size = (int32_t)(strlen(comment) + 1);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001465 zip->comment = (char *)MZ_ALLOC(comment_size);
1466 if (zip->comment == NULL)
1467 return MZ_MEM_ERROR;
1468 strncpy(zip->comment, comment, comment_size - 1);
1469 zip->comment[comment_size - 1] = 0;
1470 return MZ_OK;
1471}
1472
1473int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
1474{
1475 mz_zip *zip = (mz_zip *)handle;
1476 if (zip == NULL || version_madeby == NULL)
1477 return MZ_PARAM_ERROR;
1478 *version_madeby = zip->version_madeby;
1479 return MZ_OK;
1480}
1481
1482int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
1483{
1484 mz_zip *zip = (mz_zip *)handle;
1485 if (zip == NULL)
1486 return MZ_PARAM_ERROR;
1487 zip->version_madeby = version_madeby;
1488 return MZ_OK;
1489}
1490
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001491int32_t mz_zip_set_recover(void *handle, uint8_t recover)
1492{
1493 mz_zip *zip = (mz_zip *)handle;
1494 if (zip == NULL)
1495 return MZ_PARAM_ERROR;
1496 zip->recover = recover;
1497 return MZ_OK;
1498}
1499
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001500int32_t mz_zip_get_stream(void *handle, void **stream)
1501{
1502 mz_zip *zip = (mz_zip *)handle;
1503 if (zip == NULL || stream == NULL)
1504 return MZ_PARAM_ERROR;
1505 *stream = zip->stream;
1506 if (*stream == NULL)
1507 return MZ_EXIST_ERROR;
1508 return MZ_OK;
1509}
1510
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001511int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001512{
1513 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001514 if (zip == NULL || cd_stream == NULL)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001515 return MZ_PARAM_ERROR;
Nathan Moinvaziri4d089732018-11-19 20:24:26 -08001516 zip->cd_stream = cd_stream;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001517 zip->cd_start_pos = cd_start_pos;
1518 return MZ_OK;
1519}
1520
1521int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream)
1522{
1523 mz_zip *zip = (mz_zip *)handle;
1524 if (zip == NULL || cd_mem_stream == NULL)
1525 return MZ_PARAM_ERROR;
1526 *cd_mem_stream = zip->cd_mem_stream;
1527 if (*cd_mem_stream == NULL)
1528 return MZ_EXIST_ERROR;
1529 return MZ_OK;
1530}
1531
1532static int32_t mz_zip_entry_close_int(void *handle)
1533{
1534 mz_zip *zip = (mz_zip *)handle;
1535
1536 if (zip->crypt_stream != NULL)
1537 mz_stream_delete(&zip->crypt_stream);
1538 zip->crypt_stream = NULL;
1539 if (zip->compress_stream != NULL)
1540 mz_stream_delete(&zip->compress_stream);
1541 zip->compress_stream = NULL;
1542
1543 zip->entry_opened = 0;
1544
1545 return MZ_OK;
1546}
1547
Nathan Moinvaziri26308b22018-08-08 09:40:15 -07001548static 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 -07001549{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001550 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001551 int64_t max_total_in = 0;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001552 int64_t header_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001553 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001554 int32_t err = MZ_OK;
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001555 uint8_t use_crypt = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001556
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001557 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001558 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001559
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001560 switch (zip->file_info.compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001561 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001562 case MZ_COMPRESS_METHOD_STORE:
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001563 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001564#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001565 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001566#endif
Nathan Moinvaziri566dfe02018-10-26 00:36:30 -07001567#ifdef HAVE_LZMA
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001568 case MZ_COMPRESS_METHOD_LZMA:
1569#endif
1570 err = MZ_OK;
1571 break;
1572 default:
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001573 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001574 }
1575
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001576 zip->entry_raw = raw;
1577
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001578 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password != NULL))
1579 {
1580 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
1581 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001582 /* Encrypt only when we are not trying to write raw and password is supplied. */
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001583 if (!zip->entry_raw)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001584 use_crypt = 1;
1585 }
1586 else if (zip->open_mode & MZ_OPEN_MODE_READ)
1587 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001588 /* Decrypt only when password is supplied. Don't error when password */
1589 /* is not supplied as we may want to read the raw encrypted data. */
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001590 use_crypt = 1;
1591 }
1592 }
1593
1594 if ((err == MZ_OK) && (use_crypt))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001595 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001596#ifdef HAVE_WZAES
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001597 if (zip->file_info.aes_version)
1598 {
Nathan Moinvaziri21a31022018-10-24 09:50:16 -07001599 mz_stream_wzaes_create(&zip->crypt_stream);
1600 mz_stream_wzaes_set_password(zip->crypt_stream, password);
1601 mz_stream_wzaes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001602 }
1603 else
1604#endif
1605 {
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001606#ifdef HAVE_PKCRYPT
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001607 uint8_t verify1 = 0;
1608 uint8_t verify2 = 0;
1609
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001610 /* Info-ZIP modification to ZipCrypto format: */
1611 /* 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 -07001612
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001613 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1614 {
1615 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001616
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001617 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1618
1619 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1620 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1621 }
1622 else
1623 {
1624 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1625 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1626 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001627
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001628 mz_stream_pkcrypt_create(&zip->crypt_stream);
1629 mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1630 mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001631#endif
1632 }
1633 }
1634
1635 if (err == MZ_OK)
1636 {
1637 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001638 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001639
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001640 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001641
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001642 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001643 }
1644
1645 if (err == MZ_OK)
1646 {
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001647 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001648 mz_stream_raw_create(&zip->compress_stream);
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001649#if defined(HAVE_ZLIB) || defined(HAVE_LIBCOMP)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001650 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001651 mz_stream_zlib_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001652#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001653#ifdef HAVE_BZIP2
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001654 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001655 mz_stream_bzip_create(&zip->compress_stream);
1656#endif
1657#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001658 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001659 mz_stream_lzma_create(&zip->compress_stream);
1660#endif
1661 else
1662 err = MZ_PARAM_ERROR;
1663 }
1664
1665 if (err == MZ_OK)
1666 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001667 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001668 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001669 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001670 }
1671 else
1672 {
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001673#ifndef HAVE_LIBCOMP
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001674 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 -08001675#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001676 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001677 max_total_in = zip->file_info.compressed_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001678 mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1679
1680 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1681 max_total_in -= header_size;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001682 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1683 max_total_in -= footer_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001684
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001685 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001686 }
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001687 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 -03001688 {
1689 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1690 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1691 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001692 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001693
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001694 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1695
1696 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1697 }
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001698
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001699 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001700 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001701 zip->entry_opened = 1;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001702 zip->entry_crc32 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001703 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001704 else
1705 {
1706 mz_zip_entry_close_int(handle);
1707 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001708
1709 return err;
1710}
1711
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001712int32_t mz_zip_entry_is_open(void *handle)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001713{
1714 mz_zip *zip = (mz_zip *)handle;
1715 if (zip == NULL)
1716 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001717 if (zip->entry_opened == 0)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001718 return MZ_EXIST_ERROR;
1719 return MZ_OK;
1720}
1721
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001722static int32_t mz_zip_seek_to_local_header(void *handle)
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001723{
1724 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001725
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001726
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001727 if (zip->file_info.disk_number == zip->disk_number_with_cd)
1728 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1729 else
1730 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
1731
1732 mz_zip_print("Zip - Entry - Seek local (disk %"PRId32" offset %"PRId64")\n",
1733 zip->file_info.disk_number, zip->file_info.disk_offset);
1734
1735 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 -07001736}
1737
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001738int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001739{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001740 mz_zip *zip = (mz_zip *)handle;
1741 int32_t err = MZ_OK;
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001742 int32_t err_shift = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001743
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001744#if defined(MZ_ZIP_NO_ENCRYPTION)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001745 if (password != NULL)
1746 return MZ_PARAM_ERROR;
1747#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001748 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001749 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001750 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001751 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001752 if (zip->entry_scanned == 0)
1753 return MZ_PARAM_ERROR;
Nathan Moinvaziri0f09a002018-04-23 18:48:30 -07001754 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) && (!raw))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001755 return MZ_PARAM_ERROR;
1756
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001757 mz_zip_print("Zip - Entry - Read open (raw %"PRId32")\n", raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001758
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001759 err = mz_zip_seek_to_local_header(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001760 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001761 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 -07001762
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001763 if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0)
1764 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001765 /* Perhaps we didn't compensated correctly for incorrect cd offset */
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001766 err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
1767 if (err_shift == MZ_OK)
1768 err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
1769 if (err_shift == MZ_OK)
1770 {
1771 zip->disk_offset_shift = 0;
1772 err = err_shift;
1773 }
1774 }
1775
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001776#ifdef MZ_ZIP_NO_DECOMPRESSION
Nathan Moinvaziri2eb29072018-11-23 10:32:21 -08001777 if (!raw && zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001778 err = MZ_SUPPORT_ERROR;
Viktor Szakatse7215072018-09-04 15:06:53 +00001779#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001780 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001781 err = mz_zip_entry_open_int(handle, raw, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001782
1783 return err;
1784}
1785
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001786int32_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 -07001787{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001788 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001789 int64_t disk_number = 0;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001790 uint8_t is_dir = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001791 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001792
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001793#if defined(MZ_ZIP_NO_ENCRYPTION)
tz-lomb1b25802017-11-10 15:03:02 +03001794 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001795 return MZ_PARAM_ERROR;
1796#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001797 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001798 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001799
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001800 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001801 {
1802 err = mz_zip_entry_close(handle);
1803 if (err != MZ_OK)
1804 return err;
1805 }
1806
1807 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001808
1809 mz_zip_print("Zip - Entry - Write open - %s (level %"PRId16" raw %"PRId8")\n",
1810 zip->file_info.filename, compress_level, raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001811
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001812 mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001813 mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
1814
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001815 /* Copy filename, extrafield, and comment internally */
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001816 if (file_info->filename != NULL)
1817 {
Nathan Moinvaziric714ef62018-10-25 22:01:02 -07001818 mz_stream_mem_get_buffer_at_current(zip->file_info_stream, (const void **)&zip->file_info.filename);
Nathan Moinvazirica014e62018-08-15 07:31:12 -07001819 mz_stream_write_chars(zip->file_info_stream, file_info->filename, 1);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001820 }
1821 if (file_info->extrafield != NULL)
1822 {
Nathan Moinvaziri6bde0342018-10-25 22:05:13 -07001823 mz_stream_mem_get_buffer_at_current(zip->file_info_stream, (const void **)&zip->file_info.extrafield);
Nathan Moinvazirica014e62018-08-15 07:31:12 -07001824 mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001825 }
1826 if (file_info->comment != NULL)
1827 {
Nathan Moinvaziric714ef62018-10-25 22:01:02 -07001828 mz_stream_mem_get_buffer_at_current(zip->file_info_stream, (const void **)&zip->file_info.comment);
Nathan Moinvazirica014e62018-08-15 07:31:12 -07001829 mz_stream_write_chars(zip->file_info_stream, file_info->comment, 1);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001830 }
1831
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001832 if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001833 {
1834 if ((compress_level == 8) || (compress_level == 9))
1835 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
1836 if (compress_level == 2)
1837 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
1838 if (compress_level == 1)
1839 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1840 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001841#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001842 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001843 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001844#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001845
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001846 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
1847 is_dir = 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001848
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001849 if (!is_dir)
1850 {
1851 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
1852 if (password != NULL)
1853 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
1854 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001855
juaniib8887e92018-02-14 00:51:05 -03001856 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1857 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001858
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001859 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001860 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001861 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001862
Nathan Moinvaziri76fb8902018-11-21 15:14:09 -08001863#ifdef HAVE_WZAES
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001864 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
1865 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
1866#endif
1867
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001868 if ((compress_level == 0) || (is_dir))
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001869 zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
Viktor Szakats2884e672018-04-30 08:12:13 +00001870
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001871#ifdef MZ_ZIP_NO_COMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001872 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001873 err = MZ_SUPPORT_ERROR;
1874#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001875 if (err == MZ_OK)
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001876 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001877 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001878 err = mz_zip_entry_open_int(handle, raw, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001879
1880 return err;
1881}
1882
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001883int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001884{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001885 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001886 int32_t read = 0;
1887
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001888 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001889 return MZ_PARAM_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001890 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) /* zlib limitation */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001891 return MZ_PARAM_ERROR;
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07001892 if (len == 0)
1893 return MZ_PARAM_ERROR;
1894
Nathan Moinvazirieb80cd92018-07-11 16:45:09 -07001895 if (zip->file_info.compressed_size == 0)
1896 return 0;
1897
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08001898 /* Read entire entry even if uncompressed_size = 0, otherwise */
1899 /* aes encryption validation will fail if compressed_size > 0 */
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001900 read = mz_stream_read(zip->compress_stream, buf, len);
1901 if (read > 0)
1902 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001903
Nathan Moinvaziri264dc182018-11-13 17:42:13 -08001904 mz_zip_print("Zip - Entry - Read - %"PRId32" (max %"PRId32")\n", read, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001905
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001906 return read;
1907}
1908
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001909int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001910{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001911 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001912 int32_t written = 0;
1913
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001914 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001915 return MZ_PARAM_ERROR;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001916 written = mz_stream_write(zip->compress_stream, buf, len);
1917 if (written > 0)
1918 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001919
Nathan Moinvaziri264dc182018-11-13 17:42:13 -08001920 mz_zip_print("Zip - Entry - Write - %"PRId32" (max %"PRId32")\n", written, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001921
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001922 return written;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001923}
1924
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001925int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size,
1926 int64_t *uncompressed_size)
1927{
1928 mz_zip *zip = (mz_zip *)handle;
1929 int64_t total_in = 0;
1930 int32_t err = MZ_OK;
1931 uint8_t zip64 = 0;
1932
1933 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
1934 return MZ_PARAM_ERROR;
1935
1936 mz_stream_close(zip->compress_stream);
1937
1938 mz_zip_print("Zip - Entry - Read Close\n");
1939
1940 if (crc32 != NULL)
1941 *crc32 = zip->file_info.crc;
1942 if (compressed_size != NULL)
1943 *compressed_size = zip->file_info.compressed_size;
1944 if (uncompressed_size != NULL)
1945 *uncompressed_size = zip->file_info.uncompressed_size;
1946
1947 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
1948
1949 if ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) &&
1950 ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0) &&
1951 (crc32 != NULL || compressed_size != NULL || uncompressed_size != NULL))
1952 {
1953 /* Check to see if data descriptor is zip64 bit format or not */
1954 if (mz_zip_extrafield_contains(zip->local_file_info.extrafield,
1955 zip->local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
1956 zip64 = 1;
1957
1958 err = mz_zip_seek_to_local_header(handle);
1959
1960 /* Seek to end of compressed stream since we might have over-read during compression */
1961 if (err == MZ_OK)
1962 err = mz_stream_seek(zip->stream, MZ_ZIP_SIZE_LD_ITEM +
1963 zip->local_file_info.filename_size + zip->local_file_info.extrafield_size +
1964 total_in, MZ_SEEK_CUR);
1965
1966 /* Read data descriptor */
1967 if (err == MZ_OK)
1968 err = mz_zip_entry_read_descriptor(zip->stream, zip64,
1969 crc32, compressed_size, uncompressed_size);
1970 }
1971
1972 /* If entire entry was not read verification will fail */
1973 if ((err == MZ_OK) && (total_in > 0) && (!zip->entry_raw))
1974 {
1975#ifdef HAVE_WZAES
1976 /* AES zip version AE-1 will expect a valid crc as well */
1977 if (zip->file_info.aes_version <= 0x0001)
1978#endif
1979 {
1980 if (zip->entry_crc32 != zip->file_info.crc)
1981 {
1982 mz_zip_print("Zip - Entry - Crc failed (actual 0x%08"PRIx32" expected 0x%08"PRIx32")\n",
1983 zip->entry_crc32, zip->file_info.crc);
1984
1985 err = MZ_CRC_ERROR;
1986 }
1987 }
1988 }
1989
1990 mz_zip_entry_close_int(handle);
1991
1992 return err;
1993}
1994
1995int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size,
1996 int64_t uncompressed_size)
1997{
1998 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08001999 int32_t err = MZ_OK;
2000 uint8_t zip64 = 0;
2001
2002 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2003 return MZ_PARAM_ERROR;
2004
2005 mz_stream_close(zip->compress_stream);
2006
2007 if (!zip->entry_raw)
2008 crc32 = zip->entry_crc32;
2009
2010 mz_zip_print("Zip - Entry - Write Close (crc 0x%08"PRIx32" cs %"PRId64" ucs %"PRId64" )\n",
2011 crc32, compressed_size, uncompressed_size);
2012
2013 /* If sizes are not set, then read them from the compression stream */
2014 if (compressed_size < 0)
2015 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2016 if (uncompressed_size < 0)
2017 {
2018 /* If using raw stream as compression stream in/out are equal */
2019 if (zip->entry_raw)
2020 uncompressed_size = compressed_size;
2021 else
2022 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size);
2023 }
2024
2025 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
2026 {
2027 mz_stream_set_base(zip->crypt_stream, zip->stream);
2028 err = mz_stream_close(zip->crypt_stream);
2029
2030 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
2031 }
2032
2033 if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
2034 {
2035 /* Determine if we need to write data descriptor in zip64 format,
2036 if local extrafield was saved with zip64 extrafield */
2037 if (zip->file_info.zip64 == MZ_ZIP64_AUTO)
2038 {
2039 if (zip->file_info.uncompressed_size >= UINT32_MAX)
2040 zip64 = 1;
2041 if (zip->file_info.compressed_size >= UINT32_MAX)
2042 zip64 = 1;
2043 if (zip->file_info.disk_offset >= UINT32_MAX)
2044 zip64 = 1;
2045 else if (zip->file_info.uncompressed_size == 0)
2046 zip64 = 1;
2047 }
2048 else if (zip->file_info.zip64 == MZ_ZIP64_FORCE)
2049 {
2050 zip64 = 1;
2051 }
2052
2053 if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
2054 err = mz_zip_entry_write_descriptor(zip->stream,
2055 zip64, 0, compressed_size, 0);
2056 else
2057 err = mz_zip_entry_write_descriptor(zip->stream,
2058 zip64, crc32, compressed_size, uncompressed_size);
2059 }
2060
2061 /* Write file info to central directory */
2062
2063 mz_zip_print("Zip - Entry - Write cd (ucs %"PRId64" cs %"PRId64" crc 0x%08"PRIx32")\n",
2064 uncompressed_size, compressed_size, crc32);
2065
2066 zip->file_info.crc = crc32;
2067 zip->file_info.compressed_size = compressed_size;
2068 zip->file_info.uncompressed_size = uncompressed_size;
2069
2070 if (err == MZ_OK)
2071 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
2072
2073 zip->number_entry += 1;
2074
2075 mz_zip_entry_close_int(handle);
2076
2077 return err;
2078}
2079
2080int32_t mz_zip_entry_is_dir(void *handle)
2081{
2082 mz_zip *zip = (mz_zip *)handle;
2083 int32_t filename_length = 0;
2084
2085 if (zip == NULL)
2086 return MZ_PARAM_ERROR;
2087 if (zip->entry_scanned == 0)
2088 return MZ_PARAM_ERROR;
2089 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
2090 return MZ_OK;
2091
2092 filename_length = (int32_t)strlen(zip->file_info.filename);
2093 if (filename_length > 0)
2094 {
2095 if ((zip->file_info.filename[filename_length - 1] == '/') ||
2096 (zip->file_info.filename[filename_length - 1] == '\\'))
2097 return MZ_OK;
2098 }
2099 return MZ_EXIST_ERROR;
2100}
2101
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002102int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002103{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002104 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002105
2106 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002107 return MZ_PARAM_ERROR;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002108
2109 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
2110 {
2111 if (!zip->entry_scanned)
2112 return MZ_PARAM_ERROR;
2113 }
2114
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002115 *file_info = &zip->file_info;
2116 return MZ_OK;
2117}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002118
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002119int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002120{
2121 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002122 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002123 return MZ_PARAM_ERROR;
2124 *local_file_info = &zip->local_file_info;
2125 return MZ_OK;
2126}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002127
Nathan Moinvaziri915c5132018-10-26 20:00:52 -07002128int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size)
2129{
2130 mz_zip *zip = (mz_zip *)handle;
2131
2132 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
2133 return MZ_PARAM_ERROR;
2134
2135 zip->file_info.extrafield = extrafield;
2136 zip->file_info.extrafield_size = extrafield_size;
2137 return MZ_OK;
2138}
2139
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002140int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07002141{
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002142 return mz_zip_entry_close_raw(handle, UINT64_MAX, 0);
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07002143}
2144
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002145int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002146{
2147 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002148 int32_t err = MZ_OK;
2149
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002150 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002151 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002152
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002153 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002154 err = mz_zip_entry_write_close(handle, crc32, UINT64_MAX, uncompressed_size);
2155 else
2156 err = mz_zip_entry_read_close(handle, NULL, NULL, NULL);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002157
2158 return err;
2159}
2160
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002161static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002162{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002163 mz_zip *zip = (mz_zip *)handle;
2164 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002165
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002166 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002167 return MZ_PARAM_ERROR;
2168
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002169 zip->entry_scanned = 0;
2170
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002171 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00002172
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002173 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002174 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002175 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002176 if (err == MZ_OK)
2177 zip->entry_scanned = 1;
2178 return err;
2179}
2180
Nathan Moinvaziric565fa82018-10-19 08:48:33 -07002181int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry)
2182{
2183 mz_zip *zip = (mz_zip *)handle;
2184 if (zip == NULL)
2185 return MZ_PARAM_ERROR;
2186 zip->number_entry = number_entry;
2187 return MZ_OK;
2188}
2189
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002190int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002191{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002192 mz_zip *zip = (mz_zip *)handle;
2193 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002194 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002195 *number_entry = zip->number_entry;
2196 return MZ_OK;
2197}
2198
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002199int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd)
2200{
2201 mz_zip *zip = (mz_zip *)handle;
2202 if (zip == NULL)
2203 return MZ_PARAM_ERROR;
2204 zip->disk_number_with_cd = disk_number_with_cd;
2205 return MZ_OK;
2206}
2207
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002208int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03002209{
2210 mz_zip *zip = (mz_zip *)handle;
2211 if (zip == NULL || disk_number_with_cd == NULL)
2212 return MZ_PARAM_ERROR;
2213 *disk_number_with_cd = zip->disk_number_with_cd;
2214 return MZ_OK;
2215}
2216
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002217int64_t mz_zip_get_entry(void *handle)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002218{
2219 mz_zip *zip = (mz_zip *)handle;
2220
2221 if (zip == NULL)
2222 return MZ_PARAM_ERROR;
2223
2224 return zip->cd_current_pos;
2225}
2226
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002227int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002228{
2229 mz_zip *zip = (mz_zip *)handle;
2230
2231 if (zip == NULL)
2232 return MZ_PARAM_ERROR;
2233
2234 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
2235 return MZ_PARAM_ERROR;
2236
2237 zip->cd_current_pos = cd_pos;
2238
2239 return mz_zip_goto_next_entry_int(handle);
2240}
2241
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002242int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002243{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002244 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002245
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002246 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002247 return MZ_PARAM_ERROR;
2248
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002249 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002250
2251 return mz_zip_goto_next_entry_int(handle);
2252}
2253
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002254int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002255{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002256 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002257
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002258 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002259 return MZ_PARAM_ERROR;
2260
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002261 zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002262 zip->file_info.extrafield_size + zip->file_info.comment_size;
2263
2264 return mz_zip_goto_next_entry_int(handle);
2265}
2266
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002267int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002268{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002269 mz_zip *zip = (mz_zip *)handle;
2270 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002271 int32_t result = 0;
2272
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002273 if (zip == NULL || filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002274 return MZ_PARAM_ERROR;
2275
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002276 /* If we are already on the current entry, no need to search */
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002277 if ((zip->entry_scanned) && (zip->file_info.filename != NULL))
2278 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002279 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002280 if (result == 0)
2281 return MZ_OK;
2282 }
2283
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002284 /* Search all entries starting at the first */
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002285 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002286 while (err == MZ_OK)
2287 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002288 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2289 if (result == 0)
2290 return MZ_OK;
2291
2292 err = mz_zip_goto_next_entry(handle);
2293 }
2294
2295 return err;
2296}
2297
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002298int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002299{
2300 mz_zip *zip = (mz_zip *)handle;
2301 int32_t err = MZ_OK;
2302 int32_t result = 0;
2303
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002304 /* Search first entry looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002305 err = mz_zip_goto_first_entry(handle);
2306 if (err != MZ_OK)
2307 return err;
2308
2309 result = cb(handle, userdata, &zip->file_info);
2310 if (result == 0)
2311 return MZ_OK;
2312
2313 return mz_zip_locate_next_entry(handle, userdata, cb);
2314}
2315
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002316int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002317{
2318 mz_zip *zip = (mz_zip *)handle;
2319 int32_t err = MZ_OK;
2320 int32_t result = 0;
2321
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002322 /* Search next entries looking for match */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002323 err = mz_zip_goto_next_entry(handle);
2324 while (err == MZ_OK)
2325 {
2326 result = cb(handle, userdata, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002327 if (result == 0)
2328 return MZ_OK;
2329
2330 err = mz_zip_goto_next_entry(handle);
2331 }
2332
2333 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002334}
2335
2336/***************************************************************************/
2337
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002338int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002339{
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002340 uint32_t posix_attrib = 0;
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002341 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002342 int32_t err = MZ_OK;
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002343
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002344 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2345 if (err == MZ_OK)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002346 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002347 if ((posix_attrib & 0170000) == 0040000) /* S_ISDIR */
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002348 return MZ_OK;
2349 }
2350
2351 return MZ_EXIST_ERROR;
2352}
2353
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002354int32_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 -07002355{
2356 if (target_attrib == NULL)
2357 return MZ_PARAM_ERROR;
2358
2359 *target_attrib = 0;
2360
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002361 if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002362 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002363 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002364 {
2365 *target_attrib = src_attrib;
2366 return MZ_OK;
2367 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002368 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002369 return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
2370 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002371 else if ((src_sys == MZ_HOST_SYSTEM_UNIX) || (src_sys == MZ_HOST_SYSTEM_OSX_DARWIN))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002372 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002373 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002374 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002375 /* If high bytes are set, it contains unix specific attributes */
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002376 if ((src_attrib >> 16) != 0)
2377 src_attrib >>= 16;
2378
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002379 *target_attrib = src_attrib;
2380 return MZ_OK;
2381 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002382 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002383 return mz_zip_attrib_posix_to_win32(src_attrib, target_attrib);
2384 }
2385
2386 return MZ_SUPPORT_ERROR;
2387}
2388
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002389int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002390{
2391 if (win32_attrib == NULL)
2392 return MZ_PARAM_ERROR;
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002393
2394 *win32_attrib = 0;
2395
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002396 /* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002397 if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002398 *win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
2399 /* S_IFDIR */
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002400 if ((posix_attrib & 0170000) == 0040000)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002401 *win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
2402 /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002403 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002404 *win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002405
2406 return MZ_OK;
2407}
2408
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002409int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002410{
2411 if (posix_attrib == NULL)
2412 return MZ_PARAM_ERROR;
2413
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002414 *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
2415 /* FILE_ATTRIBUTE_READONLY */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002416 if ((win32_attrib & 0x01) == 0)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002417 *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
2418 /* FILE_ATTRIBUTE_DIRECTORY */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002419 if ((win32_attrib & 0x10) == 0x10)
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002420 *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002421 else
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002422 *posix_attrib |= 0100000; /* S_IFREG */
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002423
2424 return MZ_OK;
2425}
2426
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002427/***************************************************************************/
2428
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002429int32_t mz_zip_extrafield_find(void *stream, uint16_t type, uint16_t *length)
2430{
2431 int32_t err = MZ_OK;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002432 uint16_t field_type = 0;
2433 uint16_t field_length = 0;
2434
2435 do
2436 {
2437 err = mz_stream_read_uint16(stream, &field_type);
2438 if (err == MZ_OK)
2439 err = mz_stream_read_uint16(stream, &field_length);
2440 if (err != MZ_OK)
2441 break;
2442
2443 if (type == field_type)
2444 {
2445 if (length != NULL)
2446 *length = field_length;
2447 return MZ_OK;
2448 }
2449
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002450 err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR);
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002451 }
2452 while (err == MZ_OK);
2453
2454 return MZ_EXIST_ERROR;
2455}
2456
Nathan Moinvaziriba1db162018-11-23 10:07:35 -08002457int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size,
2458 uint16_t type, uint16_t *length)
2459{
2460 void *file_extra_stream = NULL;
2461 int32_t err = MZ_OK;
2462
2463 if (extrafield == NULL || extrafield_size == 0)
2464 return MZ_PARAM_ERROR;
2465
2466 mz_stream_mem_create(&file_extra_stream);
2467 mz_stream_mem_set_buffer(file_extra_stream, (void *)extrafield, extrafield_size);
2468
2469 err = mz_zip_extrafield_find(file_extra_stream, type, length);
2470
2471 mz_stream_mem_delete(&file_extra_stream);
2472
2473 return err;
2474}
2475
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002476int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length)
2477{
2478 int32_t err = MZ_OK;
2479 if (type == NULL || length == NULL)
2480 return MZ_PARAM_ERROR;
2481 err = mz_stream_read_uint16(stream, type);
2482 if (err == MZ_OK)
2483 err = mz_stream_read_uint16(stream, length);
2484 return err;
2485}
2486
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002487int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002488{
2489 int32_t err = MZ_OK;
2490 err = mz_stream_write_uint16(stream, type);
2491 if (err == MZ_OK)
2492 err = mz_stream_write_uint16(stream, length);
2493 return err;
2494}
2495
2496/***************************************************************************/
2497
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002498static int32_t mz_zip_invalid_date(const struct tm *ptm)
2499{
2500#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002501 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002502 !datevalue_in_range(0, 11, ptm->tm_mon) ||
2503 !datevalue_in_range(1, 31, ptm->tm_mday) ||
2504 !datevalue_in_range(0, 23, ptm->tm_hour) ||
2505 !datevalue_in_range(0, 59, ptm->tm_min) ||
2506 !datevalue_in_range(0, 59, ptm->tm_sec));
2507#undef datevalue_in_range
2508}
2509
2510static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
2511{
2512 uint64_t date = (uint64_t)(dos_date >> 16);
2513
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002514 ptm->tm_mday = (uint16_t)(date & 0x1f);
2515 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
2516 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
2517 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
2518 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
2519 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002520 ptm->tm_isdst = -1;
2521}
2522
2523int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
2524{
2525 if (ptm == NULL)
2526 return MZ_PARAM_ERROR;
2527
2528 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
2529
2530 if (mz_zip_invalid_date(ptm))
2531 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002532 /* Invalid date stored, so don't return it */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002533 memset(ptm, 0, sizeof(struct tm));
2534 return MZ_FORMAT_ERROR;
2535 }
2536 return MZ_OK;
2537}
2538
2539time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
2540{
2541 struct tm ptm;
2542 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
2543 return mktime(&ptm);
2544}
2545
2546int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
2547{
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002548 struct tm *ltm = NULL;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002549 if (ptm == NULL)
2550 return MZ_PARAM_ERROR;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002551 ltm = localtime(&unix_time); /* Returns a 1900-based year */
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002552 if (ltm == NULL)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002553 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002554 /* Invalid date stored, so don't return it */
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002555 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002556 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002557 }
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002558 memcpy(ptm, ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002559 return MZ_OK;
2560}
2561
2562uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
2563{
2564 struct tm ptm;
2565 mz_zip_time_t_to_tm(unix_time, &ptm);
2566 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
2567}
2568
2569uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
2570{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07002571 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002572
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002573 /* Years supported: */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002574
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002575 /* [00, 79] (assumed to be between 2000 and 2079) */
2576 /* [80, 207] (assumed to be between 1980 and 2107, typical output of old */
2577 /* software that does 'year-1900' to get a double digit year) */
2578 /* [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.) */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002579
2580 memcpy(&fixed_tm, ptm, sizeof(struct tm));
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002581 if (fixed_tm.tm_year >= 1980) /* range [1980, 2107] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002582 fixed_tm.tm_year -= 1980;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002583 else if (fixed_tm.tm_year >= 80) /* range [80, 207] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002584 fixed_tm.tm_year -= 80;
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002585 else /* range [00, 79] */
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002586 fixed_tm.tm_year += 20;
2587
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002588 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002589 return 0;
2590
Anand K Mistry57b65f82018-11-09 10:52:19 +11002591 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 -07002592 (((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 -07002593}
2594
2595int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
2596{
2597 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
2598 return MZ_OK;
2599}
2600
2601int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
2602{
2603 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
2604 return MZ_OK;
2605}
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002606
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002607/***************************************************************************/
2608
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002609int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case)
2610{
2611 do
2612 {
2613 if ((*path1 == '\\' && *path2 == '/') ||
2614 (*path2 == '\\' && *path1 == '/'))
2615 {
Nathan Moinvazirib16ab562018-11-20 16:56:21 -08002616 /* Ignore comparison of path slashes */
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002617 }
2618 else if (ignore_case)
2619 {
2620 if (tolower(*path1) != tolower(*path2))
2621 break;
2622 }
2623 else if (*path1 != *path2)
2624 {
2625 break;
2626 }
2627
2628 path1 += 1;
2629 path2 += 1;
2630 }
2631 while (*path1 != 0 && *path2 != 0);
2632
2633 if (ignore_case)
2634 return (int32_t)(tolower(*path1) - tolower(*path2));
2635
2636 return (int32_t)(*path1 - *path2);
2637}
2638
2639/***************************************************************************/