blob: 4961f6669dcf86c81603adfc9a87276403dfcfbb [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#include <stdlib.h>
20#include <stdint.h>
Nathan Moinvaziri85d36c52018-08-31 16:43:41 -070021#include <stdio.h>
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080022#include <string.h>
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -070023#include <ctype.h>
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -070024#include <time.h>
Nathan Moinvaziri34eff622018-01-22 09:25:15 -080025#include <limits.h>
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080026
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070027#include "mz.h"
Nathan Moinvaziri5f091882018-10-24 18:06:08 -070028#include "mz_crypt.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080029#include "mz_strm.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080030#ifdef HAVE_BZIP2
31# include "mz_strm_bzip.h"
32#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080033#ifdef HAVE_LZMA
34# include "mz_strm_lzma.h"
35#endif
Nathan Moinvazirib47b41b2018-07-11 09:44:29 -070036#include "mz_strm_mem.h"
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -070037#ifdef HAVE_PKCRYPT
38# include "mz_strm_pkcrypt.h"
39#endif
Nathan Moinvaziri21a31022018-10-24 09:50:16 -070040#ifdef HAVE_AES
41# include "mz_strm_wzaes.h"
42#endif
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -080043#ifdef HAVE_ZLIB
44# include "mz_strm_zlib.h"
45#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080046
47#include "mz_zip.h"
48
Nathan Moinvaziric565fa82018-10-19 08:48:33 -070049#if defined(_MSC_VER) && _MSC_VER < 1900
50# define snprintf _snprintf
51#endif
52
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080053/***************************************************************************/
54
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070055#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
56#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
57#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080058#define MZ_ZIP_MAGIC_ENDHEADERU8 { 0x50, 0x4b, 0x05, 0x06 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070059#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
60#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
61#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080062#define MZ_ZIP_MAGIC_DATADESCRIPTORU8 { 0x50, 0x4b, 0x07, 0x08 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070063
Nathan Moinvaziri638f31f2018-08-27 08:17:16 -070064#define MZ_ZIP_SIZE_LD_ITEM (32)
65#define MZ_ZIP_SIZE_CD_ITEM (46)
66#define MZ_ZIP_SIZE_CD_LOCATOR64 (20)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080067
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080068/***************************************************************************/
69
70typedef struct mz_zip_s
71{
72 mz_zip_file file_info;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070073 mz_zip_file local_file_info;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080074
75 void *stream; // main stream
Nathan Moinvaziricda36002017-10-21 09:37:18 -070076 void *cd_stream; // pointer to the stream with the cd
77 void *cd_mem_stream; // memory stream for central directory
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080078 void *compress_stream; // compression stream
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080079 void *crypt_stream; // encryption stream
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070080 void *file_info_stream; // memory stream for storing file info
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -070081 void *local_file_info_stream; // memory stream for storing local file info
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080082
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070083 int32_t open_mode;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -080084 uint8_t recover;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070085
Nathan Moinvazirifd039e32017-10-22 14:40:39 -070086 uint32_t disk_number_with_cd; // number of the disk with the central dir
Nathan Moinvaziri413822a2018-10-20 09:45:07 -070087 int64_t disk_offset_shift; // correction for zips that have wrong offset start of cd
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070088
Nathan Moinvaziri413822a2018-10-20 09:45:07 -070089 int64_t cd_start_pos; // pos of the first file in the central dir stream
90 int64_t cd_current_pos; // pos of the current file in the central dir
91 int64_t cd_offset; // offset of start of central directory
92 int64_t cd_size; // size of the central directory
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070093
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -070094 uint8_t entry_scanned; // entry header information read ok
95 uint8_t entry_opened; // entry is open for read/write
96 uint8_t entry_raw; // entry opened with raw mode
Nathan Moinvazirie63d2312018-11-03 19:45:41 -070097 uint32_t entry_crc32; // entry crc32
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070098
Nathan Moinvaziri904c4082018-10-08 23:31:21 -070099 uint64_t number_entry;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700100
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700101 uint16_t version_madeby;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700102 char *comment;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800103} mz_zip;
104
105/***************************************************************************/
106
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800107#if 0
108# define mz_zip_print printf
109#else
110# define mz_zip_print(fmt,...)
111#endif
112
113/***************************************************************************/
114
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800115// Locate the end of central directory
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700116static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800117{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700118 int64_t file_size = 0;
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800119 int64_t max_back = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800120 uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8;
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700121 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700122
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700123 err = mz_stream_seek(stream, 0, MZ_SEEK_END);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800124
125 file_size = mz_stream_tell(stream);
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800126
127 // maximum seek is size of global comment + extra
128 max_back = UINT16_MAX + 128;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800129 if (max_back > file_size)
130 max_back = file_size;
131
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800132 return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800133}
134
Nathan Moinvazirie824da82018-07-26 17:53:15 -0700135// Locate the end of central directory 64 of a zip file
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700136static 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 +0800137{
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700138 int64_t offset = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800139 uint32_t value32 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700140 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700141
142
143 *central_pos = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800144
145 // Zip64 end of central directory locator
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700146 err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800147 // Read locator signature
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700148 if (err == MZ_OK)
149 {
150 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700151 if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700152 err = MZ_FORMAT_ERROR;
153 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800154 // Number of the disk with the start of the zip64 end of central directory
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700155 if (err == MZ_OK)
156 err = mz_stream_read_uint32(stream, &value32);
157 // Relative offset of the zip64 end of central directory record8
158 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700159 err = mz_stream_read_uint64(stream, (uint64_t *)&offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800160 // Total number of disks
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700161 if (err == MZ_OK)
162 err = mz_stream_read_uint32(stream, &value32);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800163 // Goto end of central directory record
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700164 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700165 err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800166 // The signature
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700167 if (err == MZ_OK)
168 {
169 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700170 if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700171 err = MZ_FORMAT_ERROR;
172 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800173
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700174 if (err == MZ_OK)
175 *central_pos = offset;
176
177 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800178}
179
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700180// Get info about the current file in the zip file
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700181static 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 -0700182{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700183 uint64_t ntfs_time = 0;
184 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700185 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700186 uint32_t dos_date = 0;
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800187 uint32_t field_pos = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700188 uint16_t field_type = 0;
189 uint16_t field_length = 0;
190 uint32_t field_length_read = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700191 uint16_t ntfs_attrib_id = 0;
192 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700193 uint16_t value16 = 0;
194 uint32_t value32 = 0;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800195 int64_t extrafield_pos = -1;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800196 int64_t comment_pos = -1;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700197 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700198
199
200 memset(file_info, 0, sizeof(mz_zip_file));
201
202 // Check the magic
203 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700204 if (err == MZ_END_OF_STREAM)
205 err = MZ_END_OF_LIST;
206 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700207 err = MZ_END_OF_LIST;
208 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
209 err = MZ_FORMAT_ERROR;
210 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
211 err = MZ_FORMAT_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000212
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700213 // Read header fields
214 if (err == MZ_OK)
215 {
216 if (!local)
217 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
218 if (err == MZ_OK)
219 err = mz_stream_read_uint16(stream, &file_info->version_needed);
220 if (err == MZ_OK)
221 err = mz_stream_read_uint16(stream, &file_info->flag);
222 if (err == MZ_OK)
223 err = mz_stream_read_uint16(stream, &file_info->compression_method);
224 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700225 {
226 err = mz_stream_read_uint32(stream, &dos_date);
227 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
228 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700229 if (err == MZ_OK)
230 err = mz_stream_read_uint32(stream, &file_info->crc);
231 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700232 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700233 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700234 file_info->compressed_size = value32;
235 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700236 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700237 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700238 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700239 file_info->uncompressed_size = value32;
240 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700241 if (err == MZ_OK)
242 err = mz_stream_read_uint16(stream, &file_info->filename_size);
243 if (err == MZ_OK)
244 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
245 if (!local)
246 {
247 if (err == MZ_OK)
248 err = mz_stream_read_uint16(stream, &file_info->comment_size);
249 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700250 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700251 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700252 file_info->disk_number = value16;
253 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700254 if (err == MZ_OK)
255 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
256 if (err == MZ_OK)
257 err = mz_stream_read_uint32(stream, &file_info->external_fa);
258 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700259 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700260 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700261 file_info->disk_offset = value32;
262 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700263 }
264 }
265
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700266 if (err == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800267 err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700268
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800269 // Copy variable length data to memory stream for later retrieval
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700270 if ((err == MZ_OK) && (file_info->filename_size > 0))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700271 err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800272 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800273 extrafield_pos = (int64_t)file_info->filename_size + 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700274
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800275 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
276 err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800277 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800278
279 comment_pos = extrafield_pos + (int64_t)file_info->extrafield_size + 1;
280 if ((err == MZ_OK) && (file_info->comment_size > 0))
281 err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800282 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800283
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700284 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
285 {
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800286 // Seek to and parse the extra field
287 err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700288
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800289 while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700290 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700291 err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800292 field_pos += 4;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800293
294 // Don't allow field length to exceed size of remaining extrafield
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800295 if (field_length > (file_info->extrafield_size - field_pos))
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -0800296 field_length = (uint16_t)(file_info->extrafield_size - field_pos);
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800297
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700298 // Read ZIP64 extra field
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800299 if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700300 {
301 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700302 err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700303 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700304 err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700305 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700306 err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset);
juanii4ae79922018-02-11 14:29:36 -0300307 if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700308 err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700309 }
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700310 // Read NTFS extra field
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800311 else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700312 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700313 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700314 err = mz_stream_read_uint32(file_extra_stream, &reserved);
315 field_length_read = 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700316
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800317 while ((err == MZ_OK) && (field_length_read + 4 <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700318 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700319 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700320 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700321 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800322 field_length_read += 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700323
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700324 if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700325 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700326 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700327 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
328
juanii7063b0e2018-02-11 13:56:21 -0300329 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700330 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700331 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700332 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
333 }
juanii7063b0e2018-02-11 13:56:21 -0300334 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700335 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700336 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700337 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
338 }
339 }
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800340 else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700341 {
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800342 err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700343 }
344
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800345 field_length_read += ntfs_attrib_size;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700346 }
347 }
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700348 // Read UNIX1 extra field
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800349 else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700350 {
351 if (err == MZ_OK && file_info->accessed_date == 0)
352 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700353 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700354 if (err == MZ_OK)
355 file_info->accessed_date = value32;
356 }
357 if (err == MZ_OK && file_info->modified_date == 0)
358 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700359 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700360 if (err == MZ_OK)
361 file_info->modified_date = value32;
362 }
363 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700364 err = mz_stream_read_uint16(file_extra_stream, &value16); // User id
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700365 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700366 err = mz_stream_read_uint16(file_extra_stream, &value16); // Group id
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700367
368 // Skip variable data
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800369 mz_stream_seek(file_extra_stream, field_length - 12, MZ_SEEK_CUR);
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700370 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700371#ifdef HAVE_AES
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700372 // Read AES extra field
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800373 else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700374 {
375 uint8_t value8 = 0;
376 // Verify version info
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700377 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700378 // Support AE-1 and AE-2
379 if (value16 != 1 && value16 != 2)
380 err = MZ_FORMAT_ERROR;
381 file_info->aes_version = value16;
382 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700383 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700384 if ((char)value8 != 'A')
385 err = MZ_FORMAT_ERROR;
386 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700387 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700388 if ((char)value8 != 'E')
389 err = MZ_FORMAT_ERROR;
390 // Get AES encryption strength and actual compression method
391 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700392 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700393 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700394 file_info->aes_encryption_mode = value8;
395 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700396 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700397 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700398 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700399 file_info->compression_method = value16;
400 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700401 }
402#endif
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800403 else if (field_length > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700404 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700405 err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700406 }
407
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800408 field_pos += field_length;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700409 }
410 }
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800411
412 // Get pointers to variable length data
413 mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename);
414 if (extrafield_pos >= 0)
415 mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield);
416 if (comment_pos >= 0)
417 mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment);
418
419 // Set to empty string just in-case
420 if (file_info->filename == NULL)
421 file_info->filename = "";
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800422 if (file_info->extrafield == NULL)
423 file_info->extrafield_size = 0;
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800424 if (file_info->comment == NULL)
425 file_info->comment = "";
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700426
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800427 if (err == MZ_OK)
428 {
429 mz_zip_print("Zip - Entry - Read header - %s (local %"PRId8")\n",
430 file_info->filename, local);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -0800431 mz_zip_print("Zip - Entry - Read header compress (ucs %lld cs %lld crc 0x%08x)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800432 file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
433 if (!local)
Nathan Moinvazirieffc1422018-11-08 15:24:17 -0800434 mz_zip_print("Zip - Entry - Read header disk (disk %d offset %lld)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800435 file_info->disk_number, file_info->disk_offset);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -0800436 mz_zip_print("Zip - Entry - Read header variable (fnl %d efs %d cms %d)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800437 file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
438 }
439
440 return err;
441}
442
443static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size)
444{
445 uint32_t value32 = 0;
446 int64_t value64 = 0;
447 int32_t err = MZ_OK;
448
449
450 err = mz_stream_read_uint32(stream, &value32);
451 if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR)
452 err = MZ_FORMAT_ERROR;
453 if (err == MZ_OK)
454 err = mz_stream_read_uint32(stream, &value32);
455 if ((err == MZ_OK) && (crc32 != NULL))
456 *crc32 = value32;
457 if (err == MZ_OK)
458 {
459 // If zip 64 extension is enabled then read as 8 byte
460 if (!zip64)
461 {
462 err = mz_stream_read_uint32(stream, &value32);
463 value64 = value32;
464 }
465 else
466 err = mz_stream_read_int64(stream, &value64);
467 if ((err == MZ_OK) && (compressed_size != NULL))
468 *compressed_size = value64;
469 }
470 if (err == MZ_OK)
471 {
472 if (!zip64)
473 {
474 err = mz_stream_read_uint32(stream, &value32);
475 value64 = value32;
476 }
477 else
478 err = mz_stream_read_int64(stream, &value64);
479 if ((err == MZ_OK) && (uncompressed_size != NULL))
480 *uncompressed_size = value64;
481 }
482
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700483 return err;
484}
485
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700486static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700487{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700488 uint64_t ntfs_time = 0;
489 uint32_t reserved = 0;
490 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700491 uint16_t extrafield_size = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700492 uint16_t field_type = 0;
493 uint16_t field_length = 0;
494 uint16_t field_length_zip64 = 0;
495 uint16_t field_length_ntfs = 0;
496 uint16_t field_length_aes = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700497 uint16_t filename_size = 0;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700498 uint16_t filename_length = 0;
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700499 uint16_t version_needed = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800500 int32_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700501 int32_t err = MZ_OK;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700502 int32_t err_mem = MZ_OK;
503 uint8_t zip64 = 0;
504 uint8_t skip_aes = 0;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700505 uint8_t mask = 0;
506 uint8_t write_end_slash = 0;
507 const char *filename = NULL;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700508 char masked_name[64];
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700509 void *file_extra_stream = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700510
511 if (file_info == NULL)
512 return MZ_PARAM_ERROR;
513
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700514 if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO))
515 mask = 1;
516
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700517 // Calculate extra field sizes
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700518 if (file_info->uncompressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700519 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700520 if (file_info->compressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700521 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700522 if (file_info->disk_offset >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700523 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700524
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700525 if (file_info->zip64 == MZ_ZIP64_AUTO)
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700526 {
527 // If uncompressed size is unknown, assume zip64 for 64-bit data descriptors
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700528 zip64 = (local && file_info->uncompressed_size == 0) || (field_length_zip64 > 0);
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700529 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700530 else if (file_info->zip64 == MZ_ZIP64_FORCE)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700531 {
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700532 zip64 = 1;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700533 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700534 else if (file_info->zip64 == MZ_ZIP64_DISABLE)
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700535 {
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700536 // Zip64 extension is required to zip file
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700537 if (field_length_zip64 > 0)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700538 return MZ_PARAM_ERROR;
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700539 }
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700540
541 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700542 {
543 extrafield_size += 4;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700544 extrafield_size += field_length_zip64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700545 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700546
547 // Calculate extra field size and check for duplicates
548 if (file_info->extrafield_size > 0)
549 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700550 mz_stream_mem_create(&file_extra_stream);
Nathan Moinvaziri915c5132018-10-26 20:00:52 -0700551 mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
552 file_info->extrafield_size);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700553
554 do
555 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700556 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700557 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700558 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700559 if (err_mem != MZ_OK)
560 break;
561
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700562 // Prefer incoming aes extensions over ours
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700563 if (field_type == MZ_ZIP_EXTENSION_AES)
564 skip_aes = 1;
565
566 // Prefer our zip64, ntfs extension over incoming
567 if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS)
568 extrafield_size += 4 + field_length;
569
570 if (err_mem == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800571 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700572 }
573 while (err_mem == MZ_OK);
574 }
575
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700576#ifdef HAVE_AES
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700577 if (!skip_aes)
578 {
579 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700580 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700581 field_length_aes = 1 + 1 + 1 + 2 + 2;
582 extrafield_size += 4 + field_length_aes;
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700583 }
584 }
Nathan Moinvaziria2bb08c2018-10-28 13:45:30 -0700585#else
Nathan Moinvaziri4ae469a2018-10-28 15:32:04 -0700586 MZ_UNUSED(skip_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700587#endif
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700588 // NTFS timestamps
Nathan Moinvazirid9e159a2018-02-13 15:30:30 -0800589 if ((file_info->modified_date != 0) &&
590 (file_info->accessed_date != 0) &&
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700591 (file_info->creation_date != 0) && (!mask))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700592 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700593 field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
594 extrafield_size += 4 + field_length_ntfs;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700595 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700596
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700597 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700598 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700599 else
600 {
601 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
602 if (err == MZ_OK)
603 err = mz_stream_write_uint16(stream, file_info->version_madeby);
604 }
605
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700606 // Calculate version needed to extract
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700607 if (err == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700608 {
609 version_needed = file_info->version_needed;
610 if (version_needed == 0)
611 {
612 version_needed = 20;
613 if (zip64)
614 version_needed = 45;
615#ifdef HAVE_AES
616 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
617 version_needed = 51;
618#endif
619#ifdef HAVE_LZMA
620 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
621 version_needed = 63;
622#endif
623 }
624 err = mz_stream_write_uint16(stream, version_needed);
625 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700626 if (err == MZ_OK)
627 err = mz_stream_write_uint16(stream, file_info->flag);
628 if (err == MZ_OK)
629 {
630#ifdef HAVE_AES
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700631 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700632 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700633 else
634#endif
635 err = mz_stream_write_uint16(stream, file_info->compression_method);
636 }
637 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700638 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700639 if (file_info->modified_date != 0 && !mask)
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -0700640 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700641 err = mz_stream_write_uint32(stream, dos_date);
642 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700643
644 if (err == MZ_OK)
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700645 {
646 if (mask)
647 err = mz_stream_write_uint32(stream, 0);
648 else
649 err = mz_stream_write_uint32(stream, file_info->crc); // crc
650 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700651 if (err == MZ_OK)
652 {
653 if (file_info->compressed_size >= UINT32_MAX) // compr size
654 err = mz_stream_write_uint32(stream, UINT32_MAX);
655 else
656 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
657 }
658 if (err == MZ_OK)
659 {
660 if (file_info->uncompressed_size >= UINT32_MAX) // uncompr size
661 err = mz_stream_write_uint32(stream, UINT32_MAX);
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700662 else if (mask)
663 err = mz_stream_write_uint32(stream, 0);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700664 else
665 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
666 }
667
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700668 if (mask)
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700669 {
Nathan Moinvazirieffc1422018-11-08 15:24:17 -0800670 snprintf(masked_name, sizeof(masked_name), "%x_%llx", file_info->disk_number,
671 file_info->disk_offset);
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700672 filename = masked_name;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700673 }
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700674 else
675 {
676 filename = file_info->filename;
677
678 if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) &&
679 ((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\')))
680 {
681 filename_size += 1;
682 write_end_slash = 1;
683 }
684 }
685
686 filename_length = (uint16_t)strlen(filename);
687 filename_size += filename_length;
688
689 if (err == MZ_OK)
690 err = mz_stream_write_uint16(stream, filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700691 if (err == MZ_OK)
692 err = mz_stream_write_uint16(stream, extrafield_size);
693
694 if (!local)
695 {
696 if (file_info->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800697 {
698 comment_size = (int32_t)strlen(file_info->comment);
699 if (comment_size > UINT16_MAX)
700 comment_size = UINT16_MAX;
701 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700702 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -0800703 err = mz_stream_write_uint16(stream, (uint16_t)comment_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700704 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700705 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700706 if (err == MZ_OK)
707 err = mz_stream_write_uint16(stream, file_info->internal_fa);
708 if (err == MZ_OK)
709 err = mz_stream_write_uint32(stream, file_info->external_fa);
710 if (err == MZ_OK)
711 {
712 if (file_info->disk_offset >= UINT32_MAX)
713 err = mz_stream_write_uint32(stream, UINT32_MAX);
714 else
715 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
716 }
717 }
718
719 if (err == MZ_OK)
720 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700721 if (mz_stream_write(stream, filename, filename_length) != filename_length)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700722 err = MZ_WRITE_ERROR;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700723
724 // Ensure that directories have a slash appended to them for compatibility
725 if (err == MZ_OK && write_end_slash)
726 err = mz_stream_write_uint8(stream, '/');
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700727 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700728
729 if (file_info->extrafield_size > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700730 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800731 err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700732 while (err == MZ_OK && err_mem == MZ_OK)
733 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700734 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700735 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700736 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700737 if (err_mem != MZ_OK)
738 break;
739
740 // Prefer our zip 64, ntfs extensions over incoming
741 if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS)
742 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800743 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700744 continue;
745 }
746
747 err = mz_stream_write_uint16(stream, field_type);
748 if (err == MZ_OK)
749 err = mz_stream_write_uint16(stream, field_length);
750 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700751 err = mz_stream_copy(stream, file_extra_stream, field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700752 }
753
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700754 mz_stream_mem_delete(&file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700755 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700756
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700757 // Write ZIP64 extra field
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700758 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700759 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700760 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700761 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700762 {
763 if (mask)
764 err = mz_stream_write_int64(stream, 0);
765 else
766 err = mz_stream_write_int64(stream, file_info->uncompressed_size);
767 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700768 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700769 err = mz_stream_write_int64(stream, file_info->compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700770 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700771 err = mz_stream_write_int64(stream, file_info->disk_offset);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700772 }
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700773 // Write NTFS extra field
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700774 if ((err == MZ_OK) && (field_length_ntfs > 0))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700775 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700776 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700777 if (err == MZ_OK)
778 err = mz_stream_write_uint32(stream, reserved);
779 if (err == MZ_OK)
780 err = mz_stream_write_uint16(stream, 0x01);
781 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700782 err = mz_stream_write_uint16(stream, field_length_ntfs - 8);
juanii3679a3d2018-02-11 13:55:38 -0300783 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700784 {
785 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
786 err = mz_stream_write_uint64(stream, ntfs_time);
787 }
juanii3679a3d2018-02-11 13:55:38 -0300788 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700789 {
790 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
791 err = mz_stream_write_uint64(stream, ntfs_time);
792 }
juanii3679a3d2018-02-11 13:55:38 -0300793 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700794 {
795 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
796 err = mz_stream_write_uint64(stream, ntfs_time);
797 }
798 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700799#ifdef HAVE_AES
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700800 // Write AES extra field
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700801 if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700802 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700803 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700804 if (err == MZ_OK)
805 err = mz_stream_write_uint16(stream, file_info->aes_version);
806 if (err == MZ_OK)
807 err = mz_stream_write_uint8(stream, 'A');
808 if (err == MZ_OK)
809 err = mz_stream_write_uint8(stream, 'E');
810 if (err == MZ_OK)
811 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
812 if (err == MZ_OK)
813 err = mz_stream_write_uint16(stream, file_info->compression_method);
814 }
815#endif
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700816 if ((err == MZ_OK) && (!local) && (file_info->comment != NULL))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700817 {
Nathan Moinvazirica014e62018-08-15 07:31:12 -0700818 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700819 err = MZ_WRITE_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700820 }
821
822 return err;
823}
824
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800825static int32_t mz_zip_entry_write_descriptor(void *stream, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size)
826{
827 int32_t err = MZ_OK;
828
829 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
830 if (err == MZ_OK)
831 err = mz_stream_write_uint32(stream, crc32);
832
833 // Store data descriptor as 8 bytes if zip 64 extension enabled
834 if (err == MZ_OK)
835 {
836 // Zip 64 extension is enabled when uncompressed size is > UINT32_MAX
837 if (uncompressed_size <= UINT32_MAX)
838 err = mz_stream_write_uint32(stream, (uint32_t)compressed_size);
839 else
840 err = mz_stream_write_int64(stream, compressed_size);
841 }
842 if (err == MZ_OK)
843 {
844 if (uncompressed_size <= UINT32_MAX)
845 err = mz_stream_write_uint32(stream, (uint32_t)uncompressed_size);
846 else
847 err = mz_stream_write_int64(stream, uncompressed_size);
848 }
849
850 return err;
851}
852
853static int32_t mz_zip_read_cd(void *handle)
854{
855 mz_zip *zip = (mz_zip *)handle;
856 uint64_t number_entry_cd64 = 0;
857 uint64_t number_entry = 0;
858 uint64_t number_entry_cd = 0;
859 int64_t eocd_pos = 0;
860 int64_t eocd_pos64 = 0;
861 int64_t value64i = 0;
862 uint16_t value16 = 0;
863 uint32_t value32 = 0;
864 uint64_t value64 = 0;
865 uint16_t comment_size = 0;
866 int32_t comment_read = 0;
867 int32_t err = MZ_OK;
868
869
870 if (zip == NULL)
871 return MZ_PARAM_ERROR;
872
873 // Read and cache central directory records
874 err = mz_zip_search_eocd(zip->stream, &eocd_pos);
875 if (err == MZ_OK)
876 {
877 // The signature, already checked
878 err = mz_stream_read_uint32(zip->stream, &value32);
879 // Number of this disk
880 if (err == MZ_OK)
881 err = mz_stream_read_uint16(zip->stream, &value16);
882 // Number of the disk with the start of the central directory
883 if (err == MZ_OK)
884 err = mz_stream_read_uint16(zip->stream, &value16);
885 zip->disk_number_with_cd = value16;
886 // Total number of entries in the central dir on this disk
887 if (err == MZ_OK)
888 err = mz_stream_read_uint16(zip->stream, &value16);
889 zip->number_entry = value16;
890 // Total number of entries in the central dir
891 if (err == MZ_OK)
892 err = mz_stream_read_uint16(zip->stream, &value16);
893 number_entry_cd = value16;
894 if (number_entry_cd != zip->number_entry)
895 err = MZ_FORMAT_ERROR;
896 // Size of the central directory
897 if (err == MZ_OK)
898 err = mz_stream_read_uint32(zip->stream, &value32);
899 if (err == MZ_OK)
900 zip->cd_size = value32;
901 // Offset of start of central directory with respect to the starting disk number
902 if (err == MZ_OK)
903 err = mz_stream_read_uint32(zip->stream, &value32);
904 if (err == MZ_OK)
905 zip->cd_offset = value32;
906 // Zip file global comment length
907 if (err == MZ_OK)
908 err = mz_stream_read_uint16(zip->stream, &comment_size);
909 if ((err == MZ_OK) && (comment_size > 0))
910 {
911 zip->comment = (char *)MZ_ALLOC(comment_size + 1);
912 if (zip->comment != NULL)
913 {
914 comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
915 // Don't fail if incorrect comment length read, not critical
916 if (comment_read < 0)
917 comment_read = 0;
918 zip->comment[comment_read] = 0;
919 }
920 }
921
922 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
923 {
924 // Format should be Zip64, as the central directory or file size is too large
925 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
926 {
927 eocd_pos = eocd_pos64;
928
929 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
930 // The signature, already checked
931 if (err == MZ_OK)
932 err = mz_stream_read_uint32(zip->stream, &value32);
933 // Size of zip64 end of central directory record
934 if (err == MZ_OK)
935 err = mz_stream_read_uint64(zip->stream, &value64);
936 // Version made by
937 if (err == MZ_OK)
938 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
939 // Version needed to extract
940 if (err == MZ_OK)
941 err = mz_stream_read_uint16(zip->stream, &value16);
942 // Number of this disk
943 if (err == MZ_OK)
944 err = mz_stream_read_uint32(zip->stream, &value32);
945 // Number of the disk with the start of the central directory
946 if (err == MZ_OK)
947 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
948 // Total number of entries in the central directory on this disk
949 if (err == MZ_OK)
950 err = mz_stream_read_uint64(zip->stream, &number_entry);
951 // Total number of entries in the central directory
952 if (err == MZ_OK)
953 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
954 if (number_entry == UINT32_MAX)
955 zip->number_entry = number_entry_cd64;
956 // Size of the central directory
957 if (err == MZ_OK)
958 err = mz_stream_read_int64(zip->stream, &zip->cd_size);
959 // Offset of start of central directory with respect to the starting disk number
960 if (err == MZ_OK)
961 err = mz_stream_read_int64(zip->stream, &zip->cd_offset);
962 }
963 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
964 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
965 {
966 err = MZ_FORMAT_ERROR;
967 }
968 }
969 }
970
971 if (err == MZ_OK)
972 {
Nathan Moinvazirieffc1422018-11-08 15:24:17 -0800973 mz_zip_print("Zip - Read cd (disk %d entries %lld offset %lld size %lld)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800974 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
975
976 // Verify central directory signature exists at offset
977 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
978 if (err == MZ_OK)
979 err = mz_stream_read_uint32(zip->stream, &value32);
980 if (value32 != MZ_ZIP_MAGIC_CENTRALHEADER)
981 {
982 // If not found attempt to seek backward to find it
983 err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
984 if (err == MZ_OK)
985 err = mz_stream_read_uint32(zip->stream, &value32);
986 if (value32 == MZ_ZIP_MAGIC_CENTRALHEADER)
987 {
988 // If found compensate for incorrect locations
989 value64i = zip->cd_offset;
990 zip->cd_offset = eocd_pos - zip->cd_size;
991 // Assume disk has prepended data
992 zip->disk_offset_shift = zip->cd_offset - value64i;
993 }
994 }
995 }
996
997 if (err == MZ_OK)
998 {
999 if (eocd_pos < zip->cd_offset)
1000 {
1001 // End of central dir should always come after central dir
1002 err = MZ_FORMAT_ERROR;
1003 }
1004 else if (eocd_pos < zip->cd_offset + zip->cd_size)
1005 {
1006 // Truncate size of cd if incorrect size or offset provided
1007 zip->cd_size = eocd_pos - zip->cd_offset;
1008 }
1009 }
1010
1011 return err;
1012}
1013
1014static int32_t mz_zip_write_cd(void *handle)
1015{
1016 mz_zip *zip = (mz_zip *)handle;
1017 int64_t zip64_eocd_pos_inzip = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001018 int64_t disk_number = 0;
1019 int64_t disk_size = 0;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001020 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001021 int32_t err = MZ_OK;
1022
1023
1024 if (zip == NULL)
1025 return MZ_PARAM_ERROR;
1026
1027 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
1028 zip->disk_number_with_cd = (uint32_t)disk_number;
1029 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
1030 zip->disk_number_with_cd += 1;
1031 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1032
1033 zip->cd_offset = mz_stream_tell(zip->stream);
1034 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
1035 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
1036 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
1037
1038 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
1039
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001040 mz_zip_print("Zip - Write cd (disk %d entries %lld offset %lld size %lld)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001041 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1042
1043 // Write the ZIP64 central directory header
1044 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
1045 {
1046 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
1047
1048 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
1049
1050 // Size of this 'zip64 end of central directory'
1051 if (err == MZ_OK)
1052 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
1053 // Version made by
1054 if (err == MZ_OK)
1055 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
1056 // Version needed
1057 if (err == MZ_OK)
1058 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
1059 // Number of this disk
1060 if (err == MZ_OK)
1061 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
1062 // Number of the disk with the start of the central directory
1063 if (err == MZ_OK)
1064 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
1065 // Total number of entries in the central dir on this disk
1066 if (err == MZ_OK)
1067 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
1068 // Total number of entries in the central dir
1069 if (err == MZ_OK)
1070 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
1071 // Size of the central directory
1072 if (err == MZ_OK)
1073 err = mz_stream_write_int64(zip->stream, zip->cd_size);
1074 // Offset of start of central directory with respect to the starting disk number
1075 if (err == MZ_OK)
1076 err = mz_stream_write_int64(zip->stream, zip->cd_offset);
1077 if (err == MZ_OK)
1078 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
1079
1080 // Number of the disk with the start of the central directory
1081 if (err == MZ_OK)
1082 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
1083 // Relative offset to the end of zip64 central directory
1084 if (err == MZ_OK)
1085 err = mz_stream_write_int64(zip->stream, zip64_eocd_pos_inzip);
1086 // Number of the disk with the start of the central directory
1087 if (err == MZ_OK)
1088 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
1089 }
1090
1091 // Write the central directory header
1092
1093 // Signature
1094 if (err == MZ_OK)
1095 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
1096 // Number of this disk
1097 if (err == MZ_OK)
1098 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
1099 // Number of the disk with the start of the central directory
1100 if (err == MZ_OK)
1101 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
1102 // Total number of entries in the central dir on this disk
1103 if (err == MZ_OK)
1104 {
1105 if (zip->number_entry >= UINT16_MAX)
1106 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1107 else
1108 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1109 }
1110 // Total number of entries in the central dir
1111 if (err == MZ_OK)
1112 {
1113 if (zip->number_entry >= UINT16_MAX)
1114 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1115 else
1116 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1117 }
1118 // Size of the central directory
1119 if (err == MZ_OK)
1120 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
1121 // Offset of start of central directory with respect to the starting disk number
1122 if (err == MZ_OK)
1123 {
1124 if (zip->cd_offset >= UINT32_MAX)
1125 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
1126 else
1127 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
1128 }
1129
1130 // Write global comment
1131 if (zip->comment != NULL)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001132 {
1133 comment_size = (int32_t)strlen(zip->comment);
1134 if (comment_size > UINT16_MAX)
1135 comment_size = UINT16_MAX;
1136 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001137 if (err == MZ_OK)
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001138 err = mz_stream_write_uint16(zip->stream, (uint16_t)comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001139 if (err == MZ_OK)
1140 {
1141 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
1142 err = MZ_READ_ERROR;
1143 }
1144 return err;
1145}
1146
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001147static int32_t mz_zip_recover_cd(void *handle)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001148{
1149 mz_zip *zip = (mz_zip *)handle;
1150 mz_zip_file local_file_info;
1151 void *local_file_info_stream = NULL;
1152 void *file_extra_stream = NULL;
1153 void *cd_mem_stream = NULL;
1154 uint64_t number_entry = 0;
1155 int64_t descriptor_pos = 0;
1156 int64_t disk_offset = 0;
1157 int64_t disk_number = 0;
1158 int64_t compressed_size = 0;
1159 int64_t uncompressed_size = 0;
1160 uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8;
1161 uint32_t crc32 = 0;
1162 int32_t disk_number_with_cd = 0;
1163 int32_t err = MZ_OK;
1164 uint8_t zip64 = 0;
1165
1166
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001167 mz_zip_print("Zip - Recover cd\n");
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001168
1169 mz_zip_get_cd_mem_stream(handle, &cd_mem_stream);
1170
1171 // Determine if we are on a split disk or not
1172 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0);
1173 if (mz_stream_tell(zip->stream) < 0)
1174 {
1175 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1176 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1177 }
1178 else
1179 disk_number_with_cd = 1;
1180
1181 if (mz_stream_is_open(cd_mem_stream) != MZ_OK)
1182 err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1183
1184 mz_stream_mem_create(&local_file_info_stream);
1185 mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1186
1187 while (err == MZ_OK)
1188 {
1189 memset(&local_file_info, 0, sizeof(local_file_info));
1190
1191 // Get current offset and disk number for central dir record
1192 disk_offset = mz_stream_tell(zip->stream);
1193 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1194
1195 // Read local headers
1196 err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream);
1197
1198 local_file_info.disk_offset = disk_offset;
1199 if (disk_number < 0)
1200 disk_number = 0;
1201 local_file_info.disk_number = (uint32_t)disk_number;
1202
1203 if (err == MZ_OK)
1204 {
1205 if (local_file_info.compressed_size > 0)
1206 {
1207 err = mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR);
1208 }
1209 else if (local_file_info.uncompressed_size > 0)
1210 {
1211 err = mz_stream_find(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
1212 INT64_MAX, &descriptor_pos);
1213 }
1214 }
1215
1216 // Read descriptor if it exists so we can get to the next local header
1217 if ((err == MZ_OK) && (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
1218 {
1219 mz_stream_mem_create(&file_extra_stream);
1220 mz_stream_mem_set_buffer(file_extra_stream, (void *)local_file_info.extrafield,
1221 local_file_info.extrafield_size);
1222
1223 zip64 = 0;
1224 if (mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
1225 zip64 = 1;
1226
1227 mz_stream_mem_delete(&file_extra_stream);
1228
1229 err = mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32,
1230 &compressed_size, &uncompressed_size);
1231
1232 if (local_file_info.crc == 0)
1233 local_file_info.crc = crc32;
1234 if (local_file_info.compressed_size == 0)
1235 local_file_info.compressed_size = compressed_size;
1236 if (local_file_info.uncompressed_size == 0)
1237 local_file_info.uncompressed_size = uncompressed_size;
1238 }
1239
1240 // Rewrite central dir with local headers and offsets
1241 if (err == MZ_OK)
1242 err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
1243
1244 if (err == MZ_OK)
1245 number_entry += 1;
1246 }
1247
1248 mz_stream_mem_delete(&local_file_info_stream);
1249
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001250 mz_zip_print("Zip - Recover cd complete (cddisk %d entries %lld)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001251 disk_number_with_cd, number_entry);
1252
1253 if (number_entry == 0)
1254 return err;
1255
1256 // Set new upper seek boundary for central dir mem stream
1257 disk_offset = mz_stream_tell(cd_mem_stream);
1258 mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset);
1259
1260 // Set new central directory info
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001261 mz_zip_set_cd_start_pos(handle, 0);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001262 mz_zip_set_number_entry(handle, number_entry);
1263 mz_zip_set_disk_number_with_cd(handle, disk_number_with_cd);
1264
1265 return MZ_OK;
1266}
1267
1268void *mz_zip_create(void **handle)
1269{
1270 mz_zip *zip = NULL;
1271
1272 zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
1273 if (zip != NULL)
1274 memset(zip, 0, sizeof(mz_zip));
1275 if (handle != NULL)
1276 *handle = zip;
1277
1278 return zip;
1279}
1280
1281void mz_zip_delete(void **handle)
1282{
1283 mz_zip *zip = NULL;
1284 if (handle == NULL)
1285 return;
1286 zip = (mz_zip *)*handle;
1287 if (zip != NULL)
1288 {
1289 MZ_FREE(zip);
1290 }
1291 *handle = NULL;
1292}
1293
1294int32_t mz_zip_open(void *handle, void *stream, int32_t mode)
1295{
1296 mz_zip *zip = (mz_zip *)handle;
1297 int32_t err = MZ_OK;
1298
1299
1300 if (zip == NULL)
1301 return MZ_PARAM_ERROR;
1302
1303 mz_zip_print("Zip - Open\n");
1304
1305 zip->stream = stream;
1306
1307 mz_stream_mem_create(&zip->cd_mem_stream);
1308
1309 if (mode & MZ_OPEN_MODE_WRITE)
1310 {
1311 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1312 zip->cd_stream = zip->cd_mem_stream;
1313 }
1314 else
1315 {
1316 zip->cd_stream = stream;
1317 }
1318
1319 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
1320 {
1321 if ((mode & MZ_OPEN_MODE_CREATE) == 0)
1322 {
1323 err = mz_zip_read_cd(zip);
1324 if (err != MZ_OK)
1325 {
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001326 mz_zip_print("Zip - Error detected reading cd (%d)", err);
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001327 if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001328 err = MZ_OK;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001329 }
1330 }
1331
1332 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
1333 {
1334 if (zip->cd_size > 0)
1335 {
1336 // Store central directory in memory
1337 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1338 if (err == MZ_OK)
1339 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size);
1340 if (err == MZ_OK)
1341 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1342 }
1343 else
1344 {
1345 // If no central directory, append new zip to end of file
1346 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1347 }
1348 }
1349 else
1350 {
1351 zip->cd_start_pos = zip->cd_offset;
1352 }
1353 }
1354
1355 if (err != MZ_OK)
1356 {
1357 mz_zip_close(zip);
1358 return err;
1359 }
1360
1361 // Memory streams used to store variable length file info data
1362 mz_stream_mem_create(&zip->file_info_stream);
1363 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1364
1365 mz_stream_mem_create(&zip->local_file_info_stream);
1366 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1367
1368 zip->open_mode = mode;
1369
1370 return err;
1371}
1372
1373int32_t mz_zip_close(void *handle)
1374{
1375 mz_zip *zip = (mz_zip *)handle;
1376 int32_t err = MZ_OK;
1377
1378 if (zip == NULL)
1379 return MZ_PARAM_ERROR;
1380
1381 mz_zip_print("Zip - Close\n");
1382
1383 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001384 err = mz_zip_entry_close(handle);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001385
1386 if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE))
1387 err = mz_zip_write_cd(handle);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001388
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001389 if (zip->cd_mem_stream != NULL)
1390 {
1391 mz_stream_close(zip->cd_mem_stream);
1392 mz_stream_delete(&zip->cd_mem_stream);
1393 }
1394
1395 if (zip->file_info_stream != NULL)
1396 {
1397 mz_stream_mem_close(zip->file_info_stream);
1398 mz_stream_mem_delete(&zip->file_info_stream);
1399 }
1400 if (zip->local_file_info_stream != NULL)
1401 {
1402 mz_stream_mem_close(zip->local_file_info_stream);
1403 mz_stream_mem_delete(&zip->local_file_info_stream);
1404 }
1405
1406 if (zip->comment)
1407 {
1408 MZ_FREE(zip->comment);
1409 zip->comment = NULL;
1410 }
1411
1412 zip->stream = NULL;
1413 zip->cd_stream = NULL;
1414
1415 return err;
1416}
1417
1418int32_t mz_zip_get_comment(void *handle, const char **comment)
1419{
1420 mz_zip *zip = (mz_zip *)handle;
1421 if (zip == NULL || comment == NULL)
1422 return MZ_PARAM_ERROR;
1423 if (zip->comment == NULL)
1424 return MZ_EXIST_ERROR;
1425 *comment = zip->comment;
1426 return MZ_OK;
1427}
1428
1429int32_t mz_zip_set_comment(void *handle, const char *comment)
1430{
1431 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001432 int32_t comment_size = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001433 if (zip == NULL || comment == NULL)
1434 return MZ_PARAM_ERROR;
1435 if (zip->comment != NULL)
1436 MZ_FREE(zip->comment);
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001437 comment_size = (int32_t)(strlen(comment) + 1);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001438 zip->comment = (char *)MZ_ALLOC(comment_size);
1439 if (zip->comment == NULL)
1440 return MZ_MEM_ERROR;
1441 strncpy(zip->comment, comment, comment_size - 1);
1442 zip->comment[comment_size - 1] = 0;
1443 return MZ_OK;
1444}
1445
1446int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
1447{
1448 mz_zip *zip = (mz_zip *)handle;
1449 if (zip == NULL || version_madeby == NULL)
1450 return MZ_PARAM_ERROR;
1451 *version_madeby = zip->version_madeby;
1452 return MZ_OK;
1453}
1454
1455int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
1456{
1457 mz_zip *zip = (mz_zip *)handle;
1458 if (zip == NULL)
1459 return MZ_PARAM_ERROR;
1460 zip->version_madeby = version_madeby;
1461 return MZ_OK;
1462}
1463
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001464int32_t mz_zip_set_recover(void *handle, uint8_t recover)
1465{
1466 mz_zip *zip = (mz_zip *)handle;
1467 if (zip == NULL)
1468 return MZ_PARAM_ERROR;
1469 zip->recover = recover;
1470 return MZ_OK;
1471}
1472
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001473int32_t mz_zip_get_stream(void *handle, void **stream)
1474{
1475 mz_zip *zip = (mz_zip *)handle;
1476 if (zip == NULL || stream == NULL)
1477 return MZ_PARAM_ERROR;
1478 *stream = zip->stream;
1479 if (*stream == NULL)
1480 return MZ_EXIST_ERROR;
1481 return MZ_OK;
1482}
1483
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001484int32_t mz_zip_set_cd_start_pos(void *handle, int64_t cd_start_pos)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001485{
1486 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziridd103f42018-11-13 14:50:32 -08001487 if (zip == NULL)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001488 return MZ_PARAM_ERROR;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001489 zip->cd_start_pos = cd_start_pos;
1490 return MZ_OK;
1491}
1492
1493int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream)
1494{
1495 mz_zip *zip = (mz_zip *)handle;
1496 if (zip == NULL || cd_mem_stream == NULL)
1497 return MZ_PARAM_ERROR;
1498 *cd_mem_stream = zip->cd_mem_stream;
1499 if (*cd_mem_stream == NULL)
1500 return MZ_EXIST_ERROR;
1501 return MZ_OK;
1502}
1503
1504static int32_t mz_zip_entry_close_int(void *handle)
1505{
1506 mz_zip *zip = (mz_zip *)handle;
1507
1508 if (zip->crypt_stream != NULL)
1509 mz_stream_delete(&zip->crypt_stream);
1510 zip->crypt_stream = NULL;
1511 if (zip->compress_stream != NULL)
1512 mz_stream_delete(&zip->compress_stream);
1513 zip->compress_stream = NULL;
1514
1515 zip->entry_opened = 0;
1516
1517 return MZ_OK;
1518}
1519
Nathan Moinvaziri26308b22018-08-08 09:40:15 -07001520static 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 -07001521{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001522 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001523 int64_t max_total_in = 0;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001524 int64_t header_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001525 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001526 int32_t err = MZ_OK;
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001527 uint8_t use_crypt = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001528
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001529 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001530 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001531
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001532 switch (zip->file_info.compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001533 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001534 case MZ_COMPRESS_METHOD_STORE:
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001535 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001536#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001537 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001538#endif
Nathan Moinvaziri566dfe02018-10-26 00:36:30 -07001539#ifdef HAVE_LZMA
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001540 case MZ_COMPRESS_METHOD_LZMA:
1541#endif
1542 err = MZ_OK;
1543 break;
1544 default:
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001545 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001546 }
1547
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001548 zip->entry_raw = raw;
1549
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001550 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password != NULL))
1551 {
1552 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
1553 {
1554 // Encrypt only when we are not trying to write raw and password is supplied.
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001555 if (!zip->entry_raw)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001556 use_crypt = 1;
1557 }
1558 else if (zip->open_mode & MZ_OPEN_MODE_READ)
1559 {
1560 // Decrypt only when password is supplied. Don't error when password
1561 // is not supplied as we may want to read the raw encrypted data.
1562 use_crypt = 1;
1563 }
1564 }
1565
1566 if ((err == MZ_OK) && (use_crypt))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001567 {
1568#ifdef HAVE_AES
1569 if (zip->file_info.aes_version)
1570 {
Nathan Moinvaziri21a31022018-10-24 09:50:16 -07001571 mz_stream_wzaes_create(&zip->crypt_stream);
1572 mz_stream_wzaes_set_password(zip->crypt_stream, password);
1573 mz_stream_wzaes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001574 }
1575 else
1576#endif
1577 {
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001578#ifdef HAVE_PKCRYPT
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001579 uint8_t verify1 = 0;
1580 uint8_t verify2 = 0;
1581
1582 // Info-ZIP modification to ZipCrypto format:
1583 // If bit 3 of the general purpose bit flag is set, it uses high byte of 16-bit File Time.
1584
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001585 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1586 {
1587 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001588
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001589 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1590
1591 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1592 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1593 }
1594 else
1595 {
1596 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1597 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1598 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001599
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001600 mz_stream_pkcrypt_create(&zip->crypt_stream);
1601 mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1602 mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001603#endif
1604 }
1605 }
1606
1607 if (err == MZ_OK)
1608 {
1609 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001610 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001611
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001612 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001613
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001614 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001615 }
1616
1617 if (err == MZ_OK)
1618 {
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001619 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001620 mz_stream_raw_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001621#ifdef HAVE_ZLIB
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001622 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001623 mz_stream_zlib_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001624#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001625#ifdef HAVE_BZIP2
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001626 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001627 mz_stream_bzip_create(&zip->compress_stream);
1628#endif
1629#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001630 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001631 mz_stream_lzma_create(&zip->compress_stream);
1632#endif
1633 else
1634 err = MZ_PARAM_ERROR;
1635 }
1636
1637 if (err == MZ_OK)
1638 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001639 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001640 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001641 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001642 }
1643 else
1644 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001645 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE || zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001646 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001647 max_total_in = zip->file_info.compressed_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001648 mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1649
1650 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1651 max_total_in -= header_size;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001652 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1653 max_total_in -= footer_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001654
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001655 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001656 }
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001657 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 -03001658 {
1659 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1660 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1661 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001662 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001663
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001664 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1665
1666 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1667 }
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001668
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001669 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001670 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001671 zip->entry_opened = 1;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001672 zip->entry_crc32 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001673 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001674 else
1675 {
1676 mz_zip_entry_close_int(handle);
1677 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001678
1679 return err;
1680}
1681
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001682int32_t mz_zip_entry_is_open(void *handle)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001683{
1684 mz_zip *zip = (mz_zip *)handle;
1685 if (zip == NULL)
1686 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001687 if (zip->entry_opened == 0)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001688 return MZ_EXIST_ERROR;
1689 return MZ_OK;
1690}
1691
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001692int32_t mz_zip_entry_is_dir(void *handle)
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001693{
1694 mz_zip *zip = (mz_zip *)handle;
1695 int32_t filename_length = 0;
1696
1697 if (zip == NULL)
1698 return MZ_PARAM_ERROR;
1699 if (zip->entry_scanned == 0)
1700 return MZ_PARAM_ERROR;
1701 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
1702 return MZ_OK;
1703
1704 filename_length = (int32_t )strlen(zip->file_info.filename);
1705 if (filename_length > 0)
1706 {
1707 if ((zip->file_info.filename[filename_length - 1] == '/') ||
1708 (zip->file_info.filename[filename_length - 1] == '\\'))
1709 return MZ_OK;
1710 }
1711 return MZ_EXIST_ERROR;
1712}
1713
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001714int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001715{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001716 mz_zip *zip = (mz_zip *)handle;
1717 int32_t err = MZ_OK;
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001718 int32_t err_shift = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001719
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001720#if defined(MZ_ZIP_NO_ENCRYPTION)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001721 if (password != NULL)
1722 return MZ_PARAM_ERROR;
1723#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001724 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001725 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001726 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001727 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001728 if (zip->entry_scanned == 0)
1729 return MZ_PARAM_ERROR;
Nathan Moinvaziri0f09a002018-04-23 18:48:30 -07001730 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) && (!raw))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001731 return MZ_PARAM_ERROR;
1732
Anand K Mistrycf622bb2018-11-09 11:32:42 +11001733 // Guard against seek overflows.
1734 if ((zip->disk_offset_shift > 0) &&
1735 (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
1736 return MZ_FORMAT_ERROR;
1737
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001738 if (zip->file_info.disk_number == zip->disk_number_with_cd)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001739 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1740 else
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001741 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001742
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001743 mz_zip_print("Zip - Entry - Read open (disk %d "PRId16" offset %lld)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001744 zip->file_info.disk_number, zip->file_info.disk_offset);
1745
Nathan Moinvaziriee614ff2018-07-12 12:36:33 -07001746 err = mz_stream_seek(zip->stream, zip->file_info.disk_offset + zip->disk_offset_shift, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001747 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001748 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 -07001749
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001750 if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0)
1751 {
1752 // Perhaps we didn't compensated correctly for incorrect cd offset
1753 err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
1754 if (err_shift == MZ_OK)
1755 err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
1756 if (err_shift == MZ_OK)
1757 {
1758 zip->disk_offset_shift = 0;
1759 err = err_shift;
1760 }
1761 }
1762
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001763#ifdef MZ_ZIP_NO_DECOMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001764 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001765 err = MZ_SUPPORT_ERROR;
Viktor Szakatse7215072018-09-04 15:06:53 +00001766#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001767 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001768 err = mz_zip_entry_open_int(handle, raw, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001769
1770 return err;
1771}
1772
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001773int32_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 -07001774{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001775 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001776 int64_t disk_number = 0;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001777 uint8_t is_dir = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001778 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001779
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001780#if defined(MZ_ZIP_NO_ENCRYPTION)
tz-lomb1b25802017-11-10 15:03:02 +03001781 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001782 return MZ_PARAM_ERROR;
1783#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001784 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001785 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001786
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001787 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001788 {
1789 err = mz_zip_entry_close(handle);
1790 if (err != MZ_OK)
1791 return err;
1792 }
1793
1794 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001795
1796 mz_zip_print("Zip - Entry - Write open - %s (level %"PRId16" raw %"PRId8")\n",
1797 zip->file_info.filename, compress_level, raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001798
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001799 mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001800 mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
1801
1802 // Copy filename, extrafield, and comment internally
1803 if (file_info->filename != NULL)
1804 {
Nathan Moinvaziric714ef62018-10-25 22:01:02 -07001805 mz_stream_mem_get_buffer_at_current(zip->file_info_stream, (const void **)&zip->file_info.filename);
Nathan Moinvazirica014e62018-08-15 07:31:12 -07001806 mz_stream_write_chars(zip->file_info_stream, file_info->filename, 1);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001807 }
1808 if (file_info->extrafield != NULL)
1809 {
Nathan Moinvaziri6bde0342018-10-25 22:05:13 -07001810 mz_stream_mem_get_buffer_at_current(zip->file_info_stream, (const void **)&zip->file_info.extrafield);
Nathan Moinvazirica014e62018-08-15 07:31:12 -07001811 mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001812 }
1813 if (file_info->comment != NULL)
1814 {
Nathan Moinvaziric714ef62018-10-25 22:01:02 -07001815 mz_stream_mem_get_buffer_at_current(zip->file_info_stream, (const void **)&zip->file_info.comment);
Nathan Moinvazirica014e62018-08-15 07:31:12 -07001816 mz_stream_write_chars(zip->file_info_stream, file_info->comment, 1);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001817 }
1818
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001819 if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001820 {
1821 if ((compress_level == 8) || (compress_level == 9))
1822 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
1823 if (compress_level == 2)
1824 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
1825 if (compress_level == 1)
1826 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1827 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001828#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001829 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001830 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001831#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001832
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001833 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
1834 is_dir = 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001835
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001836 if (!is_dir)
1837 {
1838 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
1839 if (password != NULL)
1840 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
1841 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001842
juaniib8887e92018-02-14 00:51:05 -03001843 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1844 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001845
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001846 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001847 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001848 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001849
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001850#ifdef HAVE_AES
1851 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
1852 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
1853#endif
1854
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001855 if ((compress_level == 0) || (is_dir))
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001856 zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
Viktor Szakats2884e672018-04-30 08:12:13 +00001857
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001858#ifdef MZ_ZIP_NO_COMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001859 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001860 err = MZ_SUPPORT_ERROR;
1861#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001862 if (err == MZ_OK)
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001863 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001864 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001865 err = mz_zip_entry_open_int(handle, raw, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001866
1867 return err;
1868}
1869
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001870int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001871{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001872 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001873 int32_t read = 0;
1874
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001875 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001876 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001877 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) // zlib limitation
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001878 return MZ_PARAM_ERROR;
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07001879 if (len == 0)
1880 return MZ_PARAM_ERROR;
1881
Nathan Moinvazirieb80cd92018-07-11 16:45:09 -07001882 if (zip->file_info.compressed_size == 0)
1883 return 0;
1884
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07001885 // Read entire entry even if uncompressed_size = 0, otherwise
1886 // aes encryption validation will fail if compressed_size > 0
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001887 read = mz_stream_read(zip->compress_stream, buf, len);
1888 if (read > 0)
1889 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001890
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001891 mz_zip_print("Zip - Entry - Read - %d (max %d)\n", read, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001892
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001893 return read;
1894}
1895
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001896int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001897{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001898 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001899 int32_t written = 0;
1900
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001901 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001902 return MZ_PARAM_ERROR;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001903 written = mz_stream_write(zip->compress_stream, buf, len);
1904 if (written > 0)
1905 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001906
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001907 mz_zip_print("Zip - Entry - Write - %d (max %d)\n", written, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001908
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001909 return written;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001910}
1911
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001912int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001913{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001914 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07001915
1916 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001917 return MZ_PARAM_ERROR;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07001918
1919 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
1920 {
1921 if (!zip->entry_scanned)
1922 return MZ_PARAM_ERROR;
1923 }
1924
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001925 *file_info = &zip->file_info;
1926 return MZ_OK;
1927}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001928
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001929int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001930{
1931 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001932 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001933 return MZ_PARAM_ERROR;
1934 *local_file_info = &zip->local_file_info;
1935 return MZ_OK;
1936}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001937
Nathan Moinvaziri915c5132018-10-26 20:00:52 -07001938int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size)
1939{
1940 mz_zip *zip = (mz_zip *)handle;
1941
1942 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
1943 return MZ_PARAM_ERROR;
1944
1945 zip->file_info.extrafield = extrafield;
1946 zip->file_info.extrafield_size = extrafield_size;
1947 return MZ_OK;
1948}
1949
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001950int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07001951{
1952 return mz_zip_entry_close_raw(handle, 0, 0);
1953}
1954
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07001955int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001956{
1957 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07001958 int64_t compressed_size = 0;
Nathan Moinvaziri8871f6c2018-07-08 19:23:56 -07001959 int64_t total_in = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001960 int32_t err = MZ_OK;
1961
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001962 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001963 return MZ_PARAM_ERROR;
Nathan Moinvaziri915c5132018-10-26 20:00:52 -07001964
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001965 mz_stream_close(zip->compress_stream);
Nathan Moinvaziri6ac2ff42018-07-21 14:29:29 -07001966
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07001967 if (!zip->entry_raw)
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001968 crc32 = zip->entry_crc32;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001969
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001970 mz_zip_print("Zip - Entry - Close (ucs %lld crc 0x%08x)\n", compressed_size, crc32);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001971
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001972 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001973 {
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001974 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
Nathan Moinvaziri413822a2018-10-20 09:45:07 -07001975
1976 // If entire entry was not read verification will fail
1977 if ((total_in > 0) && (!zip->entry_raw))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001978 {
Nathan Moinvaziri413822a2018-10-20 09:45:07 -07001979#ifdef HAVE_AES
Nathan Moinvaziri413822a2018-10-20 09:45:07 -07001980 // AES zip version AE-1 will expect a valid crc as well
1981 if (zip->file_info.aes_version <= 0x0001)
1982#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001983 {
1984 if (crc32 != zip->file_info.crc)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001985 {
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001986 mz_zip_print("Zip - Entry - Crc failed (actual 0x%08x expected 0x%08x)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001987 crc32, zip->file_info.crc);
1988
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001989 err = MZ_CRC_ERROR;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001990 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001991 }
1992 }
1993 }
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001994
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07001995 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07001996 if (!zip->entry_raw)
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001997 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001998
1999 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
2000 {
2001 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002002 err = mz_stream_close(zip->crypt_stream);
2003
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07002004 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002005 }
2006
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002007 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002008 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -07002009 if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002010 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002011 if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
2012 err = mz_zip_entry_write_descriptor(zip->stream, 0, compressed_size, 0);
2013 else
2014 err = mz_zip_entry_write_descriptor(zip->stream, crc32, compressed_size, uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002015 }
2016
2017 zip->file_info.crc = crc32;
2018 zip->file_info.compressed_size = compressed_size;
2019 zip->file_info.uncompressed_size = uncompressed_size;
2020
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08002021 mz_zip_print("Zip - Entry - Write cd (ucs %lld cs %lld crc 0x%08x)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002022 uncompressed_size, compressed_size, crc32);
2023
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002024 if (err == MZ_OK)
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002025 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002026
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002027 zip->number_entry += 1;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002028 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002029
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002030 mz_zip_entry_close_int(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002031
2032 return err;
2033}
2034
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002035static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002036{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002037 mz_zip *zip = (mz_zip *)handle;
2038 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002039
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002040 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002041 return MZ_PARAM_ERROR;
2042
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002043 zip->entry_scanned = 0;
2044
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002045 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00002046
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002047 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002048 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002049 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002050 if (err == MZ_OK)
2051 zip->entry_scanned = 1;
2052 return err;
2053}
2054
Nathan Moinvaziric565fa82018-10-19 08:48:33 -07002055int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry)
2056{
2057 mz_zip *zip = (mz_zip *)handle;
2058 if (zip == NULL)
2059 return MZ_PARAM_ERROR;
2060 zip->number_entry = number_entry;
2061 return MZ_OK;
2062}
2063
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002064int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002065{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002066 mz_zip *zip = (mz_zip *)handle;
2067 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002068 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002069 *number_entry = zip->number_entry;
2070 return MZ_OK;
2071}
2072
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002073int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd)
2074{
2075 mz_zip *zip = (mz_zip *)handle;
2076 if (zip == NULL)
2077 return MZ_PARAM_ERROR;
2078 zip->disk_number_with_cd = disk_number_with_cd;
2079 return MZ_OK;
2080}
2081
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002082int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03002083{
2084 mz_zip *zip = (mz_zip *)handle;
2085 if (zip == NULL || disk_number_with_cd == NULL)
2086 return MZ_PARAM_ERROR;
2087 *disk_number_with_cd = zip->disk_number_with_cd;
2088 return MZ_OK;
2089}
2090
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002091int64_t mz_zip_get_entry(void *handle)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002092{
2093 mz_zip *zip = (mz_zip *)handle;
2094
2095 if (zip == NULL)
2096 return MZ_PARAM_ERROR;
2097
2098 return zip->cd_current_pos;
2099}
2100
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002101int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002102{
2103 mz_zip *zip = (mz_zip *)handle;
2104
2105 if (zip == NULL)
2106 return MZ_PARAM_ERROR;
2107
2108 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
2109 return MZ_PARAM_ERROR;
2110
2111 zip->cd_current_pos = cd_pos;
2112
2113 return mz_zip_goto_next_entry_int(handle);
2114}
2115
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002116int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002117{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002118 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002119
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002120 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002121 return MZ_PARAM_ERROR;
2122
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002123 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002124
2125 return mz_zip_goto_next_entry_int(handle);
2126}
2127
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002128int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002129{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002130 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002131
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002132 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002133 return MZ_PARAM_ERROR;
2134
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002135 zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002136 zip->file_info.extrafield_size + zip->file_info.comment_size;
2137
2138 return mz_zip_goto_next_entry_int(handle);
2139}
2140
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002141int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002142{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002143 mz_zip *zip = (mz_zip *)handle;
2144 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002145 int32_t result = 0;
2146
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002147 if (zip == NULL || filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002148 return MZ_PARAM_ERROR;
2149
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002150 // If we are already on the current entry, no need to search
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002151 if ((zip->entry_scanned) && (zip->file_info.filename != NULL))
2152 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002153 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002154 if (result == 0)
2155 return MZ_OK;
2156 }
2157
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002158 // Search all entries starting at the first
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002159 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002160 while (err == MZ_OK)
2161 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002162 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2163 if (result == 0)
2164 return MZ_OK;
2165
2166 err = mz_zip_goto_next_entry(handle);
2167 }
2168
2169 return err;
2170}
2171
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002172int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002173{
2174 mz_zip *zip = (mz_zip *)handle;
2175 int32_t err = MZ_OK;
2176 int32_t result = 0;
2177
2178 // Search first entry looking for match
2179 err = mz_zip_goto_first_entry(handle);
2180 if (err != MZ_OK)
2181 return err;
2182
2183 result = cb(handle, userdata, &zip->file_info);
2184 if (result == 0)
2185 return MZ_OK;
2186
2187 return mz_zip_locate_next_entry(handle, userdata, cb);
2188}
2189
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002190int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002191{
2192 mz_zip *zip = (mz_zip *)handle;
2193 int32_t err = MZ_OK;
2194 int32_t result = 0;
2195
2196 // Search next entries looking for match
2197 err = mz_zip_goto_next_entry(handle);
2198 while (err == MZ_OK)
2199 {
2200 result = cb(handle, userdata, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002201 if (result == 0)
2202 return MZ_OK;
2203
2204 err = mz_zip_goto_next_entry(handle);
2205 }
2206
2207 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002208}
2209
2210/***************************************************************************/
2211
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002212int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002213{
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002214 uint32_t posix_attrib = 0;
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002215 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002216 int32_t err = MZ_OK;
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002217
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002218 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2219 if (err == MZ_OK)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002220 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002221 if ((posix_attrib & 0170000) == 0040000) // S_ISDIR
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002222 return MZ_OK;
2223 }
2224
2225 return MZ_EXIST_ERROR;
2226}
2227
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002228int32_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 -07002229{
2230 if (target_attrib == NULL)
2231 return MZ_PARAM_ERROR;
2232
2233 *target_attrib = 0;
2234
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002235 if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002236 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002237 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002238 {
2239 *target_attrib = src_attrib;
2240 return MZ_OK;
2241 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002242 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002243 return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
2244 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002245 else if ((src_sys == MZ_HOST_SYSTEM_UNIX) || (src_sys == MZ_HOST_SYSTEM_OSX_DARWIN))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002246 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002247 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002248 {
Nathan Moinvaziri5e6bac52018-10-08 23:47:02 -07002249 // If high bytes are set, it contains unix specific attributes
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002250 if ((src_attrib >> 16) != 0)
2251 src_attrib >>= 16;
2252
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002253 *target_attrib = src_attrib;
2254 return MZ_OK;
2255 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002256 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002257 return mz_zip_attrib_posix_to_win32(src_attrib, target_attrib);
2258 }
2259
2260 return MZ_SUPPORT_ERROR;
2261}
2262
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002263int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002264{
2265 if (win32_attrib == NULL)
2266 return MZ_PARAM_ERROR;
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002267
2268 *win32_attrib = 0;
2269
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002270 // S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH
2271 if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002272 *win32_attrib |= 0x01; // FILE_ATTRIBUTE_READONLY
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002273 // S_IFDIR
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002274 if ((posix_attrib & 0170000) == 0040000)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002275 *win32_attrib |= 0x10; // FILE_ATTRIBUTE_DIRECTORY
Nathan Moinvazirida4b58f2018-08-20 18:21:26 -07002276 // S_IFREG
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002277 else
2278 *win32_attrib |= 0x80; // FILE_ATTRIBUTE_NORMAL
2279
2280 return MZ_OK;
2281}
2282
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002283int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002284{
2285 if (posix_attrib == NULL)
2286 return MZ_PARAM_ERROR;
2287
2288 *posix_attrib = 0000444; // S_IRUSR | S_IRGRP | S_IROTH
2289 // FILE_ATTRIBUTE_READONLY
2290 if ((win32_attrib & 0x01) == 0)
2291 *posix_attrib |= 0000222; // S_IWUSR | S_IWGRP | S_IWOTH
2292 // FILE_ATTRIBUTE_DIRECTORY
2293 if ((win32_attrib & 0x10) == 0x10)
2294 *posix_attrib |= 0040111; // S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002295 else
2296 *posix_attrib |= 0100000; // S_IFREG
2297
2298 return MZ_OK;
2299}
2300
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002301/***************************************************************************/
2302
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002303int32_t mz_zip_extrafield_find(void *stream, uint16_t type, uint16_t *length)
2304{
2305 int32_t err = MZ_OK;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002306 uint16_t field_type = 0;
2307 uint16_t field_length = 0;
2308
2309 do
2310 {
2311 err = mz_stream_read_uint16(stream, &field_type);
2312 if (err == MZ_OK)
2313 err = mz_stream_read_uint16(stream, &field_length);
2314 if (err != MZ_OK)
2315 break;
2316
2317 if (type == field_type)
2318 {
2319 if (length != NULL)
2320 *length = field_length;
2321 return MZ_OK;
2322 }
2323
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002324 err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR);
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002325 }
2326 while (err == MZ_OK);
2327
2328 return MZ_EXIST_ERROR;
2329}
2330
2331int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length)
2332{
2333 int32_t err = MZ_OK;
2334 if (type == NULL || length == NULL)
2335 return MZ_PARAM_ERROR;
2336 err = mz_stream_read_uint16(stream, type);
2337 if (err == MZ_OK)
2338 err = mz_stream_read_uint16(stream, length);
2339 return err;
2340}
2341
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002342int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002343{
2344 int32_t err = MZ_OK;
2345 err = mz_stream_write_uint16(stream, type);
2346 if (err == MZ_OK)
2347 err = mz_stream_write_uint16(stream, length);
2348 return err;
2349}
2350
2351/***************************************************************************/
2352
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002353static int32_t mz_zip_invalid_date(const struct tm *ptm)
2354{
2355#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002356 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || // 1980-based year, allow 80 extra
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002357 !datevalue_in_range(0, 11, ptm->tm_mon) ||
2358 !datevalue_in_range(1, 31, ptm->tm_mday) ||
2359 !datevalue_in_range(0, 23, ptm->tm_hour) ||
2360 !datevalue_in_range(0, 59, ptm->tm_min) ||
2361 !datevalue_in_range(0, 59, ptm->tm_sec));
2362#undef datevalue_in_range
2363}
2364
2365static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
2366{
2367 uint64_t date = (uint64_t)(dos_date >> 16);
2368
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002369 ptm->tm_mday = (uint16_t)(date & 0x1f);
2370 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
2371 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
2372 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
2373 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
2374 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002375 ptm->tm_isdst = -1;
2376}
2377
2378int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
2379{
2380 if (ptm == NULL)
2381 return MZ_PARAM_ERROR;
2382
2383 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
2384
2385 if (mz_zip_invalid_date(ptm))
2386 {
2387 // Invalid date stored, so don't return it
2388 memset(ptm, 0, sizeof(struct tm));
2389 return MZ_FORMAT_ERROR;
2390 }
2391 return MZ_OK;
2392}
2393
2394time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
2395{
2396 struct tm ptm;
2397 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
2398 return mktime(&ptm);
2399}
2400
2401int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
2402{
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002403 struct tm *ltm = NULL;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002404 if (ptm == NULL)
2405 return MZ_PARAM_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002406 ltm = localtime(&unix_time); // Returns a 1900-based year
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002407 if (ltm == NULL)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002408 {
2409 // Invalid date stored, so don't return it
2410 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002411 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002412 }
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002413 memcpy(ptm, ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002414 return MZ_OK;
2415}
2416
2417uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
2418{
2419 struct tm ptm;
2420 mz_zip_time_t_to_tm(unix_time, &ptm);
2421 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
2422}
2423
2424uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
2425{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07002426 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002427
2428 // Years supported:
2429
2430 // [00, 79] (assumed to be between 2000 and 2079)
2431 // [80, 207] (assumed to be between 1980 and 2107, typical output of old
2432 // software that does 'year-1900' to get a double digit year)
2433 // [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.)
2434
2435 memcpy(&fixed_tm, ptm, sizeof(struct tm));
2436 if (fixed_tm.tm_year >= 1980) // range [1980, 2107]
2437 fixed_tm.tm_year -= 1980;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002438 else if (fixed_tm.tm_year >= 80) // range [80, 207]
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002439 fixed_tm.tm_year -= 80;
2440 else // range [00, 79]
2441 fixed_tm.tm_year += 20;
2442
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002443 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002444 return 0;
2445
Anand K Mistry57b65f82018-11-09 10:52:19 +11002446 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 -07002447 (((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 -07002448}
2449
2450int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
2451{
2452 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
2453 return MZ_OK;
2454}
2455
2456int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
2457{
2458 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
2459 return MZ_OK;
2460}
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002461
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002462/***************************************************************************/
2463
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002464int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case)
2465{
2466 do
2467 {
2468 if ((*path1 == '\\' && *path2 == '/') ||
2469 (*path2 == '\\' && *path1 == '/'))
2470 {
2471 // Ignore comparison of path slashes
2472 }
2473 else if (ignore_case)
2474 {
2475 if (tolower(*path1) != tolower(*path2))
2476 break;
2477 }
2478 else if (*path1 != *path2)
2479 {
2480 break;
2481 }
2482
2483 path1 += 1;
2484 path2 += 1;
2485 }
2486 while (*path1 != 0 && *path2 != 0);
2487
2488 if (ignore_case)
2489 return (int32_t)(tolower(*path1) - tolower(*path2));
2490
2491 return (int32_t)(*path1 - *path2);
2492}
2493
2494/***************************************************************************/