blob: 0aa48f52600f797ad30957c96230039ae3601c8c [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvaziri0f8d9112018-08-05 10:33:42 -07002 Version 2.4.0, August 5, 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 Moinvazirifa146ad2018-08-07 23:50:31 -07007 Copyright (C) 2009-2010 Mathias Svensson & Even Rouault
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08008 Modifications for Zip64 support
9 http://result42.com
10 Copyright (C) 1998-2010 Gilles Vollant
11 http://www.winimage.com/zLibDll/minizip.html
12
13 This program is distributed under the terms of the same license as zlib.
14 See the accompanying LICENSE file for the full text of the license.
15*/
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <stdint.h>
20#include <string.h>
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -070021#include <ctype.h>
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -070022#include <time.h>
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080023#include <errno.h>
Nathan Moinvaziri34eff622018-01-22 09:25:15 -080024#include <limits.h>
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080025
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070026#include "mz.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080027#include "mz_strm.h"
28#ifdef HAVE_AES
29# include "mz_strm_aes.h"
30#endif
31#ifdef HAVE_BZIP2
32# include "mz_strm_bzip.h"
33#endif
Nathan Moinvazirib47b41b2018-07-11 09:44:29 -070034#include "mz_strm_crc32.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080035#ifdef HAVE_LZMA
36# include "mz_strm_lzma.h"
37#endif
Nathan Moinvazirib47b41b2018-07-11 09:44:29 -070038#include "mz_strm_mem.h"
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -070039#ifdef HAVE_PKCRYPT
40# include "mz_strm_pkcrypt.h"
41#endif
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -080042#ifdef HAVE_ZLIB
43# include "mz_strm_zlib.h"
44#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080045
46#include "mz_zip.h"
47
48/***************************************************************************/
49
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070050#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
51#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
52#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
53#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
54#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
55#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
56
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070057#define MZ_ZIP_SIZE_CD_ITEM (0x2e)
58#define MZ_ZIP_SIZE_CD_LOCATOR64 (0x14)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080059
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070060#define MZ_ZIP_EXTENSION_ZIP64 (0x0001)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -070061#define MZ_ZIP_EXTENSION_NTFS (0x000a)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070062#define MZ_ZIP_EXTENSION_AES (0x9901)
63
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080064/***************************************************************************/
65
66typedef struct mz_zip_s
67{
68 mz_zip_file file_info;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070069 mz_zip_file local_file_info;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080070
71 void *stream; // main stream
Nathan Moinvaziricda36002017-10-21 09:37:18 -070072 void *cd_stream; // pointer to the stream with the cd
73 void *cd_mem_stream; // memory stream for central directory
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080074 void *compress_stream; // compression stream
75 void *crc32_stream; // crc32 stream
76 void *crypt_stream; // encryption stream
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070077 void *file_info_stream; // memory stream for storing file info
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -070078 void *local_file_info_stream; // memory stream for storing local file info
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080079
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070080 int32_t open_mode;
81
Nathan Moinvazirifd039e32017-10-22 14:40:39 -070082 uint32_t disk_number_with_cd; // number of the disk with the central dir
Nathan Moinvaziriee614ff2018-07-12 12:36:33 -070083 uint64_t disk_offset_shift; // correction for zips that have wrong offset start of cd
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070084
Nathan Moinvaziricda36002017-10-21 09:37:18 -070085 uint64_t cd_start_pos; // pos of the first file in the central dir stream
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070086 uint64_t cd_current_pos; // pos of the current file in the central dir
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070087 uint64_t cd_offset; // offset of start of central directory
88 uint64_t cd_size; // size of the central directory
89
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -070090 uint8_t entry_scanned; // entry header information read ok
91 uint8_t entry_opened; // entry is open for read/write
92 uint8_t entry_raw; // entry opened with raw mode
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070093
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070094 int64_t number_entry;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070095
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -070096 uint16_t version_madeby;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070097 char *comment;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080098} mz_zip;
99
100/***************************************************************************/
101
Nathan Moinvazirie824da82018-07-26 17:53:15 -0700102// Locate the end of central directory of a zip file (at the end, just before the global comment)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700103static int32_t mz_zip_search_eocd(void *stream, uint64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800104{
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700105 uint8_t buf[1024 + 4];
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700106 int64_t file_size = 0;
107 int64_t back_read = 0;
108 int64_t max_back = UINT16_MAX; // maximum size of global comment
109 int32_t read_size = sizeof(buf);
110 int64_t read_pos = 0;
111 int32_t i = 0;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700112
Nathan Moinvazirif6e81cd2017-10-10 18:24:03 -0700113 *central_pos = 0;
114
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700115 if (mz_stream_seek(stream, 0, MZ_SEEK_END) != MZ_OK)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700116 return MZ_STREAM_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800117
118 file_size = mz_stream_tell(stream);
119
120 if (max_back > file_size)
121 max_back = file_size;
122
123 while (back_read < max_back)
124 {
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700125 back_read += (sizeof(buf) - 4);
126 if (back_read > max_back)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800127 back_read = max_back;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800128
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700129 read_pos = file_size - back_read;
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700130 if (read_size > (file_size - read_pos))
131 read_size = (uint32_t)(file_size - read_pos);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800132
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700133 if (mz_stream_seek(stream, read_pos, MZ_SEEK_SET) != MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800134 break;
135 if (mz_stream_read(stream, buf, read_size) != read_size)
136 break;
137
138 for (i = read_size - 3; (i--) > 0;)
139 {
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700140 if (((*(buf + i)) == (MZ_ZIP_MAGIC_ENDHEADER & 0xff)) &&
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700141 ((*(buf + i + 1)) == (MZ_ZIP_MAGIC_ENDHEADER >> 8 & 0xff)) &&
142 ((*(buf + i + 2)) == (MZ_ZIP_MAGIC_ENDHEADER >> 16 & 0xff)) &&
143 ((*(buf + i + 3)) == (MZ_ZIP_MAGIC_ENDHEADER >> 24 & 0xff)))
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800144 {
Nathan Moinvazirif6e81cd2017-10-10 18:24:03 -0700145 *central_pos = read_pos + i;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700146 return MZ_OK;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800147 }
148 }
149
Nathan Moinvazirif6e81cd2017-10-10 18:24:03 -0700150 if (*central_pos != 0)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800151 break;
152 }
153
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700154 return MZ_EXIST_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800155}
156
Nathan Moinvazirie824da82018-07-26 17:53:15 -0700157// Locate the end of central directory 64 of a zip file
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700158static int32_t mz_zip_search_zip64_eocd(void *stream, const uint64_t end_central_offset, uint64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800159{
160 uint64_t offset = 0;
161 uint32_t value32 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700162 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700163
164
165 *central_pos = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800166
167 // Zip64 end of central directory locator
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700168 err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800169 // Read locator signature
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700170 if (err == MZ_OK)
171 {
172 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700173 if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700174 err = MZ_FORMAT_ERROR;
175 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800176 // Number of the disk with the start of the zip64 end of central directory
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700177 if (err == MZ_OK)
178 err = mz_stream_read_uint32(stream, &value32);
179 // Relative offset of the zip64 end of central directory record8
180 if (err == MZ_OK)
181 err = mz_stream_read_uint64(stream, &offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800182 // Total number of disks
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700183 if (err == MZ_OK)
184 err = mz_stream_read_uint32(stream, &value32);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800185 // Goto end of central directory record
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700186 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700187 err = mz_stream_seek(stream, offset, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800188 // The signature
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700189 if (err == MZ_OK)
190 {
191 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700192 if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700193 err = MZ_FORMAT_ERROR;
194 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800195
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700196 if (err == MZ_OK)
197 *central_pos = offset;
198
199 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800200}
201
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700202static int32_t mz_zip_read_cd(void *handle)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800203{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700204 mz_zip *zip = (mz_zip *)handle;
205 int64_t number_entry_cd = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700206 uint64_t number_entry_cd64 = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800207 uint64_t number_entry = 0;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700208 uint64_t eocd_pos = 0;
209 uint64_t eocd_pos64 = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800210 uint16_t value16 = 0;
211 uint32_t value32 = 0;
212 uint64_t value64 = 0;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700213 uint16_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700214 int32_t err = MZ_OK;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800215
216
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800217 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700218 return MZ_PARAM_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800219
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700220 // Read and cache central directory records
Nathan Moinvaziri77687f32018-08-09 16:29:26 -0700221 err = mz_zip_search_eocd(zip->stream, &eocd_pos);
222 if (err == MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800223 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700224 // Read end of central directory info
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700225 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700226 // The signature, already checked
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800227 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700228 err = mz_stream_read_uint32(zip->stream, &value32);
229 // Number of this disk
230 if (err == MZ_OK)
231 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700232 // Number of the disk with the start of the central directory
233 if (err == MZ_OK)
234 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700235 zip->disk_number_with_cd = value16;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700236 // Total number of entries in the central dir on this disk
237 if (err == MZ_OK)
238 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700239 zip->number_entry = value16;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700240 // Total number of entries in the central dir
241 if (err == MZ_OK)
242 err = mz_stream_read_uint16(zip->stream, &value16);
243 number_entry_cd = value16;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700244 if (number_entry_cd != zip->number_entry)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700245 err = MZ_FORMAT_ERROR;
246 // Size of the central directory
247 if (err == MZ_OK)
248 err = mz_stream_read_uint32(zip->stream, &value32);
249 if (err == MZ_OK)
250 zip->cd_size = value32;
251 // Offset of start of central directory with respect to the starting disk number
252 if (err == MZ_OK)
253 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvaziriee614ff2018-07-12 12:36:33 -0700254 if (err == MZ_OK)
255 zip->cd_offset = value32;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700256 // Zip file global comment length
257 if (err == MZ_OK)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700258 err = mz_stream_read_uint16(zip->stream, &comment_size);
Nathan Moinvaziri51bf61d2018-07-26 16:41:54 -0700259 if ((err == MZ_OK) && (comment_size > 0))
260 {
261 zip->comment = (char *)MZ_ALLOC(comment_size + 1);
262 if (zip->comment)
263 {
264 if (mz_stream_read(zip->stream, zip->comment, comment_size) != comment_size)
265 err = MZ_STREAM_ERROR;
266 zip->comment[comment_size] = 0;
267 }
268 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800269
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700270 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800271 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700272 // Format should be Zip64, as the central directory or file size is too large
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700273 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800274 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700275 eocd_pos = eocd_pos64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700276
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700277 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700278 // The signature, already checked
279 if (err == MZ_OK)
280 err = mz_stream_read_uint32(zip->stream, &value32);
281 // Size of zip64 end of central directory record
282 if (err == MZ_OK)
283 err = mz_stream_read_uint64(zip->stream, &value64);
284 // Version made by
285 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700286 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700287 // Version needed to extract
288 if (err == MZ_OK)
289 err = mz_stream_read_uint16(zip->stream, &value16);
290 // Number of this disk
291 if (err == MZ_OK)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -0700292 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700293 // Number of the disk with the start of the central directory
294 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700295 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700296 // Total number of entries in the central directory on this disk
297 if (err == MZ_OK)
298 err = mz_stream_read_uint64(zip->stream, &number_entry);
299 // Total number of entries in the central directory
300 if (err == MZ_OK)
301 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
302 if (number_entry == UINT32_MAX)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700303 zip->number_entry = number_entry_cd64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700304 // Size of the central directory
305 if (err == MZ_OK)
306 err = mz_stream_read_uint64(zip->stream, &zip->cd_size);
307 // Offset of start of central directory with respect to the starting disk number
308 if (err == MZ_OK)
309 err = mz_stream_read_uint64(zip->stream, &zip->cd_offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800310 }
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700311 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700312 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700313 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700314 err = MZ_FORMAT_ERROR;
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700315 }
316 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800317 }
318
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700319 if (err == MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800320 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700321 if (eocd_pos < zip->cd_offset + zip->cd_size)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700322 err = MZ_FORMAT_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800323 }
324
Nathan Moinvaziriee614ff2018-07-12 12:36:33 -0700325 if (err == MZ_OK)
326 {
327 // Verify central directory signature exists at offset
328 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
329 if (err == MZ_OK)
330 err = mz_stream_read_uint32(zip->stream, &value32);
331 if (value32 != MZ_ZIP_MAGIC_CENTRALHEADER)
332 {
333 // If not found attempt to seek backward to find it
334 err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
335 if (err == MZ_OK)
336 err = mz_stream_read_uint32(zip->stream, &value32);
337 if (value32 == MZ_ZIP_MAGIC_CENTRALHEADER)
338 {
339 // If found compensate for incorrect locations
340 value64 = zip->cd_offset;
341 zip->cd_offset = eocd_pos - zip->cd_size;
342 zip->disk_offset_shift = zip->cd_offset - value64;
343 }
344 }
345 }
346
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800347 return err;
348}
349
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700350static int32_t mz_zip_write_cd(void *handle)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800351{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700352 mz_zip *zip = (mz_zip *)handle;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800353 uint16_t comment_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700354 uint64_t zip64_eocd_pos_inzip = 0;
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700355 int64_t disk_number = 0;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -0700356 int64_t disk_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700357 int32_t err = MZ_OK;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800358
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700359
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700360 if (zip == NULL)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800361 return MZ_PARAM_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000362
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700363 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700364 zip->disk_number_with_cd = (uint32_t)disk_number;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -0700365 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700366 zip->disk_number_with_cd += 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700367 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800368
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700369 zip->cd_offset = mz_stream_tell(zip->stream);
370 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700371 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700372 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
Viktor Szakats915b82e2018-04-24 10:02:39 +0000373
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700374 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800375
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800376 // Write the ZIP64 central directory header
juanii3f59ffc2018-02-08 12:44:17 -0300377 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800378 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700379 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800380
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700381 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800382
383 // Size of this 'zip64 end of central directory'
384 if (err == MZ_OK)
385 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
386 // Version made by
387 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700388 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800389 // Version needed
390 if (err == MZ_OK)
391 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
392 // Number of this disk
393 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700394 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800395 // Number of the disk with the start of the central directory
396 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700397 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800398 // Total number of entries in the central dir on this disk
399 if (err == MZ_OK)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700400 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800401 // Total number of entries in the central dir
402 if (err == MZ_OK)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700403 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800404 // Size of the central directory
405 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700406 err = mz_stream_write_uint64(zip->stream, (uint64_t)zip->cd_size);
407 // Offset of start of central directory with respect to the starting disk number
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800408 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700409 err = mz_stream_write_uint64(zip->stream, zip->cd_offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800410 if (err == MZ_OK)
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700411 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700412
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800413 // Number of the disk with the start of the central directory
414 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700415 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800416 // Relative offset to the end of zip64 central directory
417 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700418 err = mz_stream_write_uint64(zip->stream, zip64_eocd_pos_inzip);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800419 // Number of the disk with the start of the central directory
420 if (err == MZ_OK)
juaniic65486d2018-02-08 17:07:07 -0300421 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800422 }
423
424 // Write the central directory header
425
Viktor Szakats915b82e2018-04-24 10:02:39 +0000426 // Signature
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800427 if (err == MZ_OK)
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700428 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800429 // Number of this disk
430 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700431 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800432 // Number of the disk with the start of the central directory
433 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700434 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800435 // Total number of entries in the central dir on this disk
436 if (err == MZ_OK)
437 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700438 if (zip->number_entry >= UINT16_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800439 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
440 else
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700441 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800442 }
443 // Total number of entries in the central dir
444 if (err == MZ_OK)
445 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700446 if (zip->number_entry >= UINT16_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800447 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
448 else
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700449 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800450 }
451 // Size of the central directory
452 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700453 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800454 // Offset of start of central directory with respect to the starting disk number
455 if (err == MZ_OK)
456 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700457 if (zip->cd_offset >= UINT32_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800458 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
459 else
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700460 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800461 }
462
463 // Write global comment
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700464 if (zip->comment != NULL)
465 comment_size = (uint16_t)strlen(zip->comment);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800466 if (err == MZ_OK)
467 err = mz_stream_write_uint16(zip->stream, comment_size);
468 if (err == MZ_OK)
469 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700470 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800471 err = MZ_STREAM_ERROR;
472 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700473 return err;
474}
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800475
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -0700476extern void *mz_zip_create(void **handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700477{
478 mz_zip *zip = NULL;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -0700479
480 zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
481 if (zip != NULL)
482 {
483 memset(zip, 0, sizeof(mz_zip));
484 }
485 if (handle != NULL)
486 *handle = zip;
487
488 return zip;
489}
490
491extern void mz_zip_delete(void **handle)
492{
493 mz_zip *zip = NULL;
494 if (handle == NULL)
495 return;
496 zip = (mz_zip *)*handle;
497 if (zip != NULL)
498 {
499 MZ_FREE(zip);
500 }
501 *handle = NULL;
502}
503
504extern int32_t mz_zip_open(void *handle, void *stream, int32_t mode)
505{
506 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700507 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700508
509
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700510 if (zip == NULL)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -0700511 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700512
513 zip->stream = stream;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700514
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800515 if (mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700516 {
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700517 mz_stream_mem_create(&zip->cd_mem_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700518 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700519
520 zip->cd_stream = zip->cd_mem_stream;
521 }
522 else
523 {
524 zip->cd_stream = stream;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700525 }
526
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800527 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700528 {
Nathan Moinvaziri446d0622018-07-16 19:17:46 -0700529 if ((mode & MZ_OPEN_MODE_CREATE) == 0)
530 err = mz_zip_read_cd(zip);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700531
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700532 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700533 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700534 if (zip->cd_size > 0)
535 {
536 // Store central directory in memory
537 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
538 if (err == MZ_OK)
539 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (uint32_t)zip->cd_size);
540 if (err == MZ_OK)
541 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
542 }
543 else
544 {
545 // If no central directory, append new zip to end of file
546 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
547 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700548 }
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700549 else
550 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700551 zip->cd_start_pos = zip->cd_offset;
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700552 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700553 }
554
555 if (err == MZ_OK)
556 {
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700557 // Memory streams used to store variable length file info data
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700558 mz_stream_mem_create(&zip->file_info_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700559 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700560
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700561 mz_stream_mem_create(&zip->local_file_info_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700562 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700563 }
564
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700565 if (err != MZ_OK)
566 {
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800567 mz_zip_close(zip);
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -0700568 return err;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700569 }
570
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800571 zip->open_mode = mode;
572
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -0700573 return err;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700574}
575
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800576extern int32_t mz_zip_close(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700577{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700578 mz_zip *zip = (mz_zip *)handle;
579 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700580
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700581 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700582 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700583
Nathan Moinvaziri77687f32018-08-09 16:29:26 -0700584 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700585 {
586 err = mz_zip_entry_close(handle);
587 if (err != MZ_OK)
588 return err;
589 }
590
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700591 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700592 err = mz_zip_write_cd(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700593
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700594 if (zip->cd_mem_stream != NULL)
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700595 {
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700596 mz_stream_close(zip->cd_mem_stream);
597 mz_stream_delete(&zip->cd_mem_stream);
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700598 }
599
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800600 if (zip->file_info_stream != NULL)
601 {
602 mz_stream_mem_close(zip->file_info_stream);
603 mz_stream_mem_delete(&zip->file_info_stream);
604 }
605 if (zip->local_file_info_stream != NULL)
606 {
607 mz_stream_mem_close(zip->local_file_info_stream);
608 mz_stream_mem_delete(&zip->local_file_info_stream);
609 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700610
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700611 if (zip->comment)
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700612 MZ_FREE(zip->comment);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700613
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800614 return err;
615}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700616
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800617extern int32_t mz_zip_get_comment(void *handle, const char **comment)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700618{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700619 mz_zip *zip = (mz_zip *)handle;
620 if (zip == NULL || comment == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700621 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700622 if (zip->comment == NULL)
623 return MZ_EXIST_ERROR;
624 *comment = zip->comment;
625 return MZ_OK;
626}
627
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800628extern int32_t mz_zip_set_comment(void *handle, const char *comment)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700629{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700630 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700631 uint16_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700632 if (zip == NULL || comment == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700633 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700634 if (zip->comment != NULL)
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700635 MZ_FREE(zip->comment);
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700636 comment_size = (uint16_t)(strlen(comment) + 1);
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700637 zip->comment = (char *)MZ_ALLOC(comment_size);
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700638 strncpy(zip->comment, comment, comment_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700639 return MZ_OK;
640}
641
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800642extern int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700643{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700644 mz_zip *zip = (mz_zip *)handle;
645 if (zip == NULL || version_madeby == NULL)
646 return MZ_PARAM_ERROR;
647 *version_madeby = zip->version_madeby;
648 return MZ_OK;
649}
650
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800651extern int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700652{
653 mz_zip *zip = (mz_zip *)handle;
654 if (zip == NULL)
655 return MZ_PARAM_ERROR;
656 zip->version_madeby = version_madeby;
657 return MZ_OK;
658}
659
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700660// Get info about the current file in the zip file
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700661static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file *file_info, void *file_info_stream)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700662{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700663 uint64_t ntfs_time = 0;
664 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700665 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700666 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700667 uint32_t extra_pos = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700668 uint32_t extra_data_size_read = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700669 uint16_t extra_header_id = 0;
670 uint16_t extra_data_size = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700671 uint16_t ntfs_attrib_id = 0;
672 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700673 uint16_t value16 = 0;
674 uint32_t value32 = 0;
675 uint64_t value64 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700676 int64_t max_seek = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700677 int64_t seek = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700678 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700679
680
681 memset(file_info, 0, sizeof(mz_zip_file));
682
683 // Check the magic
684 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700685 if (err == MZ_END_OF_STREAM)
686 err = MZ_END_OF_LIST;
687 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700688 err = MZ_END_OF_LIST;
689 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
690 err = MZ_FORMAT_ERROR;
691 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
692 err = MZ_FORMAT_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000693
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700694 // Read header fields
695 if (err == MZ_OK)
696 {
697 if (!local)
698 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
699 if (err == MZ_OK)
700 err = mz_stream_read_uint16(stream, &file_info->version_needed);
701 if (err == MZ_OK)
702 err = mz_stream_read_uint16(stream, &file_info->flag);
703 if (err == MZ_OK)
704 err = mz_stream_read_uint16(stream, &file_info->compression_method);
705 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700706 {
707 err = mz_stream_read_uint32(stream, &dos_date);
708 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
709 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700710 if (err == MZ_OK)
711 err = mz_stream_read_uint32(stream, &file_info->crc);
712 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700713 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700714 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700715 file_info->compressed_size = value32;
716 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700717 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700718 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700719 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700720 file_info->uncompressed_size = value32;
721 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700722 if (err == MZ_OK)
723 err = mz_stream_read_uint16(stream, &file_info->filename_size);
724 if (err == MZ_OK)
725 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
726 if (!local)
727 {
728 if (err == MZ_OK)
729 err = mz_stream_read_uint16(stream, &file_info->comment_size);
730 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700731 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700732 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700733 file_info->disk_number = value16;
734 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700735 if (err == MZ_OK)
736 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
737 if (err == MZ_OK)
738 err = mz_stream_read_uint32(stream, &file_info->external_fa);
739 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700740 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700741 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700742 file_info->disk_offset = value32;
743 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700744 }
745 }
746
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700747 max_seek = file_info->filename_size + file_info->extrafield_size + file_info->comment_size + 3;
748 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700749 err = mz_stream_seek(file_info_stream, max_seek, MZ_SEEK_SET);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700750 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700751 err = mz_stream_seek(file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700752
753 if ((err == MZ_OK) && (file_info->filename_size > 0))
754 {
Nathan Moinvaziria710bd72018-05-09 00:46:47 -0700755 mz_stream_mem_get_buffer(file_info_stream, (const void **)&file_info->filename);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700756
757 err = mz_stream_copy(file_info_stream, stream, file_info->filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700758 if (err == MZ_OK)
759 err = mz_stream_write_uint8(file_info_stream, 0);
760
761 seek += file_info->filename_size + 1;
762 }
763
764 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
765 {
Nathan Moinvaziria710bd72018-05-09 00:46:47 -0700766 mz_stream_mem_get_buffer_at(file_info_stream, seek, (const void **)&file_info->extrafield);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700767
768 err = mz_stream_copy(file_info_stream, stream, file_info->extrafield_size);
769 if (err == MZ_OK)
770 err = mz_stream_write_uint8(file_info_stream, 0);
771
772 // Seek back and parse the extra field
773 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700774 err = mz_stream_seek(file_info_stream, seek, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700775
776 seek += file_info->extrafield_size + 1;
777
778 while ((err == MZ_OK) && (extra_pos < file_info->extrafield_size))
779 {
780 err = mz_stream_read_uint16(file_info_stream, &extra_header_id);
781 if (err == MZ_OK)
782 err = mz_stream_read_uint16(file_info_stream, &extra_data_size);
783
784 // ZIP64 extra field
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700785 if (extra_header_id == MZ_ZIP_EXTENSION_ZIP64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700786 {
787 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
788 err = mz_stream_read_uint64(file_info_stream, &file_info->uncompressed_size);
789 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
790 err = mz_stream_read_uint64(file_info_stream, &file_info->compressed_size);
791 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700792 err = mz_stream_read_uint64(file_info_stream, &file_info->disk_offset);
juanii4ae79922018-02-11 14:29:36 -0300793 if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700794 err = mz_stream_read_uint32(file_info_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700795 }
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700796 // NTFS extra field
797 else if (extra_header_id == MZ_ZIP_EXTENSION_NTFS)
798 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700799 if (err == MZ_OK)
800 err = mz_stream_read_uint32(file_info_stream, &reserved);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700801 extra_data_size_read = 4;
802
803 while ((err == MZ_OK) && (extra_data_size_read < extra_data_size))
804 {
805 err = mz_stream_read_uint16(file_info_stream, &ntfs_attrib_id);
806 if (err == MZ_OK)
807 err = mz_stream_read_uint16(file_info_stream, &ntfs_attrib_size);
808
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700809 if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700810 {
811 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
812 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
813
juanii7063b0e2018-02-11 13:56:21 -0300814 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700815 {
816 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
817 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
818 }
juanii7063b0e2018-02-11 13:56:21 -0300819 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700820 {
821 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
822 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
823 }
824 }
825 else
826 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700827 if (err == MZ_OK)
828 err = mz_stream_seek(file_info_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700829 }
830
831 extra_data_size_read += ntfs_attrib_size + 4;
832 }
833 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700834#ifdef HAVE_AES
835 // AES extra field
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700836 else if (extra_header_id == MZ_ZIP_EXTENSION_AES)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700837 {
838 uint8_t value8 = 0;
839 // Verify version info
840 err = mz_stream_read_uint16(file_info_stream, &value16);
841 // Support AE-1 and AE-2
842 if (value16 != 1 && value16 != 2)
843 err = MZ_FORMAT_ERROR;
844 file_info->aes_version = value16;
845 if (err == MZ_OK)
846 err = mz_stream_read_uint8(file_info_stream, &value8);
847 if ((char)value8 != 'A')
848 err = MZ_FORMAT_ERROR;
849 if (err == MZ_OK)
850 err = mz_stream_read_uint8(file_info_stream, &value8);
851 if ((char)value8 != 'E')
852 err = MZ_FORMAT_ERROR;
853 // Get AES encryption strength and actual compression method
854 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700855 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700856 err = mz_stream_read_uint8(file_info_stream, &value8);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700857 file_info->aes_encryption_mode = value8;
858 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700859 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700860 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700861 err = mz_stream_read_uint16(file_info_stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700862 file_info->compression_method = value16;
863 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700864 }
865#endif
866 else
867 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700868 err = mz_stream_seek(file_info_stream, extra_data_size, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700869 }
870
871 extra_pos += 4 + extra_data_size;
872 }
873 }
874
875 if ((err == MZ_OK) && (file_info->comment_size > 0))
876 {
Nathan Moinvaziria710bd72018-05-09 00:46:47 -0700877 mz_stream_mem_get_buffer_at(file_info_stream, seek, (const void **)&file_info->comment);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700878
879 err = mz_stream_copy(file_info_stream, stream, file_info->comment_size);
880 if (err == MZ_OK)
881 err = mz_stream_write_uint8(file_info_stream, 0);
882 }
883
884 return err;
885}
886
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700887static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700888{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700889 uint64_t ntfs_time = 0;
890 uint32_t reserved = 0;
891 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700892 uint16_t extrafield_size = 0;
893 uint16_t extrafield_zip64_size = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700894 uint16_t extrafield_ntfs_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700895 uint16_t filename_size = 0;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700896 uint16_t filename_length = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700897 uint16_t comment_size = 0;
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700898 uint16_t version_needed = 0;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700899 uint16_t field_type = 0;
900 uint16_t field_length = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700901 int32_t err = MZ_OK;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700902 int32_t err_mem = MZ_OK;
903 uint8_t zip64 = 0;
904 uint8_t skip_aes = 0;
905 void *extrafield_ms = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700906
907 if (file_info == NULL)
908 return MZ_PARAM_ERROR;
909
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700910 // Calculate extra field sizes
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700911 if (file_info->uncompressed_size >= UINT32_MAX)
912 extrafield_zip64_size += 8;
913 if (file_info->compressed_size >= UINT32_MAX)
914 extrafield_zip64_size += 8;
915 if (file_info->disk_offset >= UINT32_MAX)
916 extrafield_zip64_size += 8;
917
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700918 if (file_info->zip64 == MZ_ZIP64_AUTO)
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700919 {
920 // If uncompressed size is unknown, assume zip64 for 64-bit data descriptors
921 zip64 = (local && file_info->uncompressed_size == 0) || (extrafield_zip64_size > 0);
922 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700923 else if (file_info->zip64 == MZ_ZIP64_FORCE)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700924 {
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700925 zip64 = 1;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700926 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700927 else if (file_info->zip64 == MZ_ZIP64_DISABLE)
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700928 {
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700929 // Zip64 extension is required to zip file
930 if (extrafield_zip64_size > 0)
931 return MZ_PARAM_ERROR;
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700932 }
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700933
934 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700935 {
936 extrafield_size += 4;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700937 extrafield_size += extrafield_zip64_size;
938 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700939
940 // Calculate extra field size and check for duplicates
941 if (file_info->extrafield_size > 0)
942 {
943 mz_stream_mem_create(&extrafield_ms);
944 mz_stream_mem_set_buffer(extrafield_ms, (void *)file_info->extrafield, file_info->extrafield_size);
945
946 do
947 {
948 err_mem = mz_stream_read_uint16(extrafield_ms, &field_type);
949 if (err_mem == MZ_OK)
950 err_mem = mz_stream_read_uint16(extrafield_ms, &field_length);
951 if (err_mem != MZ_OK)
952 break;
953
954 // Prefer incoming ntfs, aes extensions over ours
955 if (field_type == MZ_ZIP_EXTENSION_AES)
956 skip_aes = 1;
957
958 // Prefer our zip64, ntfs extension over incoming
959 if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS)
960 extrafield_size += 4 + field_length;
961
962 if (err_mem == MZ_OK)
963 err_mem = mz_stream_seek(extrafield_ms, field_length, SEEK_CUR);
964 }
965 while (err_mem == MZ_OK);
966 }
967
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700968#ifdef HAVE_AES
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700969 if (!skip_aes)
970 {
971 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
972 extrafield_size += 4 + 7;
973 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700974#endif
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700975 // NTFS timestamps
Nathan Moinvazirid9e159a2018-02-13 15:30:30 -0800976 if ((file_info->modified_date != 0) &&
977 (file_info->accessed_date != 0) &&
978 (file_info->creation_date != 0))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700979 {
juanii3679a3d2018-02-11 13:55:38 -0300980 extrafield_ntfs_size += 8 + 8 + 8 + 4 + 2 + 2;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700981 extrafield_size += 4;
982 extrafield_size += extrafield_ntfs_size;
983 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700984
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700985 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700986 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700987 else
988 {
989 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
990 if (err == MZ_OK)
991 err = mz_stream_write_uint16(stream, file_info->version_madeby);
992 }
993
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700994 // Calculate version needed to extract
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700995 if (err == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700996 {
997 version_needed = file_info->version_needed;
998 if (version_needed == 0)
999 {
1000 version_needed = 20;
1001 if (zip64)
1002 version_needed = 45;
1003#ifdef HAVE_AES
1004 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
1005 version_needed = 51;
1006#endif
1007#ifdef HAVE_LZMA
1008 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
1009 version_needed = 63;
1010#endif
1011 }
1012 err = mz_stream_write_uint16(stream, version_needed);
1013 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001014 if (err == MZ_OK)
1015 err = mz_stream_write_uint16(stream, file_info->flag);
1016 if (err == MZ_OK)
1017 {
1018#ifdef HAVE_AES
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001019 if (file_info->aes_version)
1020 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001021 else
1022#endif
1023 err = mz_stream_write_uint16(stream, file_info->compression_method);
1024 }
1025 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001026 {
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -07001027 if (file_info->modified_date != 0)
1028 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001029 err = mz_stream_write_uint32(stream, dos_date);
1030 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001031
1032 if (err == MZ_OK)
1033 err = mz_stream_write_uint32(stream, file_info->crc); // crc
1034 if (err == MZ_OK)
1035 {
1036 if (file_info->compressed_size >= UINT32_MAX) // compr size
1037 err = mz_stream_write_uint32(stream, UINT32_MAX);
1038 else
1039 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
1040 }
1041 if (err == MZ_OK)
1042 {
1043 if (file_info->uncompressed_size >= UINT32_MAX) // uncompr size
1044 err = mz_stream_write_uint32(stream, UINT32_MAX);
1045 else
1046 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
1047 }
1048
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001049 filename_length = (uint16_t)strlen(file_info->filename);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001050 if (err == MZ_OK)
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001051 {
1052 filename_size = filename_length;
1053 if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -07001054 {
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001055 if ((file_info->filename[filename_length - 1] == '/') ||
Nathan Moinvaziri46f71432018-05-03 17:24:32 -07001056 (file_info->filename[filename_length - 1] == '\\'))
1057 filename_length -= 1;
1058 else
1059 filename_size += 1;
1060 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001061 err = mz_stream_write_uint16(stream, filename_size);
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001062 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001063 if (err == MZ_OK)
1064 err = mz_stream_write_uint16(stream, extrafield_size);
1065
1066 if (!local)
1067 {
1068 if (file_info->comment != NULL)
1069 comment_size = (uint16_t)strlen(file_info->comment);
1070 if (err == MZ_OK)
1071 err = mz_stream_write_uint16(stream, comment_size);
1072 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001073 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001074 if (err == MZ_OK)
1075 err = mz_stream_write_uint16(stream, file_info->internal_fa);
1076 if (err == MZ_OK)
1077 err = mz_stream_write_uint32(stream, file_info->external_fa);
1078 if (err == MZ_OK)
1079 {
1080 if (file_info->disk_offset >= UINT32_MAX)
1081 err = mz_stream_write_uint32(stream, UINT32_MAX);
1082 else
1083 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
1084 }
1085 }
1086
1087 if (err == MZ_OK)
1088 {
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001089 if (mz_stream_write(stream, file_info->filename, filename_length) != filename_length)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001090 err = MZ_STREAM_ERROR;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001091 if (err == MZ_OK)
1092 {
Nathan Moinvaziri240b3b62018-05-02 13:38:14 -07001093 // Ensure that directories have a slash appended to them for compatibility
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001094 if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK)
1095 err = mz_stream_write_uint8(stream, '/');
1096 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001097 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001098
1099 if (file_info->extrafield_size > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001100 {
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001101 err_mem = mz_stream_mem_seek(extrafield_ms, 0, SEEK_SET);
1102 while (err == MZ_OK && err_mem == MZ_OK)
1103 {
1104 err_mem = mz_stream_read_uint16(extrafield_ms, &field_type);
1105 if (err_mem == MZ_OK)
1106 err_mem = mz_stream_read_uint16(extrafield_ms, &field_length);
1107 if (err_mem != MZ_OK)
1108 break;
1109
1110 // Prefer our zip 64, ntfs extensions over incoming
1111 if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS)
1112 {
1113 err_mem = mz_stream_seek(extrafield_ms, field_length, SEEK_CUR);
1114 continue;
1115 }
1116
1117 err = mz_stream_write_uint16(stream, field_type);
1118 if (err == MZ_OK)
1119 err = mz_stream_write_uint16(stream, field_length);
1120 if (err == MZ_OK)
1121 err = mz_stream_copy(stream, extrafield_ms, field_length);
1122 }
1123
1124 mz_stream_mem_delete(&extrafield_ms);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001125 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001126
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001127 // Add ZIP64 extra info header to central directory
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001128 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001129 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001130 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_ZIP64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001131 if (err == MZ_OK)
1132 err = mz_stream_write_uint16(stream, extrafield_zip64_size);
1133 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
1134 err = mz_stream_write_uint64(stream, file_info->uncompressed_size);
1135 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
1136 err = mz_stream_write_uint64(stream, file_info->compressed_size);
1137 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
1138 err = mz_stream_write_uint64(stream, file_info->disk_offset);
1139 }
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001140 // Write NTFS timestamps
1141 if ((err == MZ_OK) && (extrafield_ntfs_size > 0))
1142 {
1143 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_NTFS);
1144 if (err == MZ_OK)
1145 err = mz_stream_write_uint16(stream, extrafield_ntfs_size);
1146 if (err == MZ_OK)
1147 err = mz_stream_write_uint32(stream, reserved);
1148 if (err == MZ_OK)
1149 err = mz_stream_write_uint16(stream, 0x01);
1150 if (err == MZ_OK)
1151 err = mz_stream_write_uint16(stream, extrafield_ntfs_size - 8);
juanii3679a3d2018-02-11 13:55:38 -03001152 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001153 {
1154 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
1155 err = mz_stream_write_uint64(stream, ntfs_time);
1156 }
juanii3679a3d2018-02-11 13:55:38 -03001157 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001158 {
1159 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
1160 err = mz_stream_write_uint64(stream, ntfs_time);
1161 }
juanii3679a3d2018-02-11 13:55:38 -03001162 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001163 {
1164 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
1165 err = mz_stream_write_uint64(stream, ntfs_time);
1166 }
1167 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001168#ifdef HAVE_AES
1169 // Write AES extra info header to central directory
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001170 if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001171 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001172 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001173 if (err == MZ_OK)
1174 err = mz_stream_write_uint16(stream, 7);
1175 if (err == MZ_OK)
1176 err = mz_stream_write_uint16(stream, file_info->aes_version);
1177 if (err == MZ_OK)
1178 err = mz_stream_write_uint8(stream, 'A');
1179 if (err == MZ_OK)
1180 err = mz_stream_write_uint8(stream, 'E');
1181 if (err == MZ_OK)
1182 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
1183 if (err == MZ_OK)
1184 err = mz_stream_write_uint16(stream, file_info->compression_method);
1185 }
1186#endif
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001187 if ((err == MZ_OK) && (!local) && (file_info->comment != NULL))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001188 {
1189 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != MZ_OK)
1190 err = MZ_STREAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001191 }
1192
1193 return err;
1194}
1195
Nathan Moinvaziri26308b22018-08-08 09:40:15 -07001196static 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 -07001197{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001198 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001199 int64_t max_total_in = 0;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001200 int64_t header_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001201 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001202 int32_t err = MZ_OK;
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001203 uint8_t use_crypt = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001204
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001205 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001206 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001207
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001208 switch (zip->file_info.compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001209 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001210 case MZ_COMPRESS_METHOD_STORE:
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001211 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001212#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001213 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001214#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001215#if HAVE_LZMA
1216 case MZ_COMPRESS_METHOD_LZMA:
1217#endif
1218 err = MZ_OK;
1219 break;
1220 default:
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001221 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001222 }
1223
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001224 zip->entry_raw = raw;
1225
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001226 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password != NULL))
1227 {
1228 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
1229 {
1230 // Encrypt only when we are not trying to write raw and password is supplied.
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001231 if (!zip->entry_raw)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001232 use_crypt = 1;
1233 }
1234 else if (zip->open_mode & MZ_OPEN_MODE_READ)
1235 {
1236 // Decrypt only when password is supplied. Don't error when password
1237 // is not supplied as we may want to read the raw encrypted data.
1238 use_crypt = 1;
1239 }
1240 }
1241
1242 if ((err == MZ_OK) && (use_crypt))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001243 {
1244#ifdef HAVE_AES
1245 if (zip->file_info.aes_version)
1246 {
1247 mz_stream_aes_create(&zip->crypt_stream);
1248 mz_stream_aes_set_password(zip->crypt_stream, password);
1249 mz_stream_aes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001250 }
1251 else
1252#endif
1253 {
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001254#ifdef HAVE_PKCRYPT
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001255 uint8_t verify1 = 0;
1256 uint8_t verify2 = 0;
1257
1258 // Info-ZIP modification to ZipCrypto format:
1259 // If bit 3 of the general purpose bit flag is set, it uses high byte of 16-bit File Time.
1260
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001261 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1262 {
1263 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001264
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001265 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1266
1267 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1268 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1269 }
1270 else
1271 {
1272 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1273 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1274 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001275
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001276 mz_stream_pkcrypt_create(&zip->crypt_stream);
1277 mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1278 mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001279#endif
1280 }
1281 }
1282
1283 if (err == MZ_OK)
1284 {
1285 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001286 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001287
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001288 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001289
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001290 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001291 }
1292
1293 if (err == MZ_OK)
1294 {
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001295 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001296 mz_stream_raw_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001297#ifdef HAVE_ZLIB
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001298 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001299 mz_stream_zlib_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001300#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001301#ifdef HAVE_BZIP2
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001302 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001303 mz_stream_bzip_create(&zip->compress_stream);
1304#endif
1305#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001306 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001307 mz_stream_lzma_create(&zip->compress_stream);
1308#endif
1309 else
1310 err = MZ_PARAM_ERROR;
1311 }
1312
1313 if (err == MZ_OK)
1314 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001315 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001316 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001317 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001318 }
1319 else
1320 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001321 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 -07001322 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001323 max_total_in = zip->file_info.compressed_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001324 mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1325
1326 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1327 max_total_in -= header_size;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001328 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1329 max_total_in -= footer_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001330
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001331 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001332 }
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001333 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 -03001334 {
1335 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1336 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1337 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001338 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001339
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001340 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1341
1342 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1343 }
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001344
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001345 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001346 {
1347 mz_stream_crc32_create(&zip->crc32_stream);
1348 mz_stream_set_base(zip->crc32_stream, zip->compress_stream);
1349
1350 err = mz_stream_open(zip->crc32_stream, NULL, zip->open_mode);
1351 }
1352
1353 if (err == MZ_OK)
1354 {
1355 zip->entry_opened = 1;
1356 }
1357
1358 return err;
1359}
1360
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001361extern int32_t mz_zip_entry_is_open(void *handle)
1362{
1363 mz_zip *zip = (mz_zip *)handle;
1364 if (zip == NULL)
1365 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001366 if (zip->entry_opened == 0)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001367 return MZ_EXIST_ERROR;
1368 return MZ_OK;
1369}
1370
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001371extern int32_t mz_zip_entry_is_dir(void *handle)
1372{
1373 mz_zip *zip = (mz_zip *)handle;
1374 int32_t filename_length = 0;
1375
1376 if (zip == NULL)
1377 return MZ_PARAM_ERROR;
1378 if (zip->entry_scanned == 0)
1379 return MZ_PARAM_ERROR;
1380 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
1381 return MZ_OK;
1382
1383 filename_length = (int32_t )strlen(zip->file_info.filename);
1384 if (filename_length > 0)
1385 {
1386 if ((zip->file_info.filename[filename_length - 1] == '/') ||
1387 (zip->file_info.filename[filename_length - 1] == '\\'))
1388 return MZ_OK;
1389 }
1390 return MZ_EXIST_ERROR;
1391}
1392
Nathan Moinvaziri26308b22018-08-08 09:40:15 -07001393extern int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001394{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001395 mz_zip *zip = (mz_zip *)handle;
1396 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001397
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001398#if defined(MZ_ZIP_NO_ENCRYPTION)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001399 if (password != NULL)
1400 return MZ_PARAM_ERROR;
1401#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001402 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001403 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001404 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001405 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001406 if (zip->entry_scanned == 0)
1407 return MZ_PARAM_ERROR;
Nathan Moinvaziri0f09a002018-04-23 18:48:30 -07001408 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) && (!raw))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001409 return MZ_PARAM_ERROR;
1410
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001411 if (zip->file_info.disk_number == zip->disk_number_with_cd)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001412 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1413 else
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001414 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001415
Nathan Moinvaziriee614ff2018-07-12 12:36:33 -07001416 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 -07001417 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001418 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 -07001419
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001420#ifdef MZ_ZIP_NO_DECOMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001421 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001422 err = MZ_SUPPORT_ERROR;
1423#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001424 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001425 err = mz_zip_entry_open_int(handle, raw, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001426
1427 return err;
1428}
1429
Nathan Moinvazirid08e1562018-08-08 09:43:14 -07001430extern int32_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 -07001431{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001432 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001433 int64_t disk_number = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001434 int32_t err = MZ_OK;
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001435 int32_t len = 0;
1436 const char *buf_ptr = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001437
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001438#if defined(MZ_ZIP_NO_ENCRYPTION)
tz-lomb1b25802017-11-10 15:03:02 +03001439 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001440 return MZ_PARAM_ERROR;
1441#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001442 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001443 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001444
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001445 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001446 {
1447 err = mz_zip_entry_close(handle);
1448 if (err != MZ_OK)
1449 return err;
1450 }
1451
1452 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001453
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001454 mz_stream_seek(zip->file_info_stream, 0, SEEK_SET);
1455 mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
1456
1457 // Copy filename, extrafield, and comment internally
1458 if (file_info->filename != NULL)
1459 {
1460 err = mz_stream_mem_get_buffer_at_current(zip->file_info_stream, &buf_ptr);
1461 if (err == MZ_OK)
1462 err = mz_stream_write_chars(zip->file_info_stream, file_info->filename, 1);
1463 if (err == MZ_OK)
1464 zip->file_info.filename = buf_ptr;
1465 }
1466 if (file_info->extrafield != NULL)
1467 {
1468 err = mz_stream_mem_get_buffer_at_current(zip->file_info_stream, &buf_ptr);
1469 if (err == MZ_OK)
1470 err = mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
1471 if (err == MZ_OK)
1472 zip->file_info.extrafield = buf_ptr;
1473 }
1474 if (file_info->comment != NULL)
1475 {
1476 err = mz_stream_mem_get_buffer_at_current(zip->file_info_stream, &buf_ptr);
1477 if (err == MZ_OK)
1478 err = mz_stream_write_chars(zip->file_info_stream, file_info->comment, 1);
1479 if (err == MZ_OK)
1480 zip->file_info.comment = buf_ptr;
1481 }
1482
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001483 if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001484 {
1485 if ((compress_level == 8) || (compress_level == 9))
1486 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
1487 if (compress_level == 2)
1488 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
1489 if (compress_level == 1)
1490 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1491 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001492#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001493 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001494 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001495#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001496
1497 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001498
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001499 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001500 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001501
juaniib8887e92018-02-14 00:51:05 -03001502 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1503 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001504
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001505 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001506 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001507 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001508
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001509#ifdef HAVE_AES
1510 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
1511 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
1512#endif
1513
Nathan Moinvaziri4827f712018-05-02 10:50:47 -07001514 if ((compress_level == 0) || (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK))
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001515 zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
Viktor Szakats2884e672018-04-30 08:12:13 +00001516
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001517#ifdef MZ_ZIP_NO_COMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001518 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001519 err = MZ_SUPPORT_ERROR;
1520#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001521 if (err == MZ_OK)
1522 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
1523 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001524 err = mz_zip_entry_open_int(handle, raw, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001525
1526 return err;
1527}
1528
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001529extern int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001530{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001531 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001532 int32_t read = 0;
1533
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001534 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001535 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001536 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) // zlib limitation
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001537 return MZ_PARAM_ERROR;
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07001538 if (len == 0)
1539 return MZ_PARAM_ERROR;
1540
Nathan Moinvazirieb80cd92018-07-11 16:45:09 -07001541 if (zip->file_info.compressed_size == 0)
1542 return 0;
1543
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07001544 // Read entire entry even if uncompressed_size = 0, otherwise
1545 // aes encryption validation will fail if compressed_size > 0
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001546 read = mz_stream_read(zip->crc32_stream, buf, len);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001547 return read;
1548}
1549
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001550extern int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001551{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001552 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001553 int32_t written = 0;
1554
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001555 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001556 return MZ_PARAM_ERROR;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001557 written = mz_stream_write(zip->crc32_stream, buf, len);
1558 return written;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001559}
1560
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001561extern int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001562{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001563 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001564 if (zip == NULL || !zip->entry_scanned)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001565 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001566 *file_info = &zip->file_info;
1567 return MZ_OK;
1568}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001569
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001570extern int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001571{
1572 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001573 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001574 return MZ_PARAM_ERROR;
1575 *local_file_info = &zip->local_file_info;
1576 return MZ_OK;
1577}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001578
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07001579extern int32_t mz_zip_entry_close(void *handle)
1580{
1581 return mz_zip_entry_close_raw(handle, 0, 0);
1582}
1583
1584extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001585{
1586 mz_zip *zip = (mz_zip *)handle;
1587 uint64_t compressed_size = 0;
Nathan Moinvaziri8871f6c2018-07-08 19:23:56 -07001588 int64_t total_in = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001589 int32_t err = MZ_OK;
1590
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001591 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001592 return MZ_PARAM_ERROR;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001593
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001594 mz_stream_close(zip->compress_stream);
Nathan Moinvaziri6ac2ff42018-07-21 14:29:29 -07001595
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07001596 if (!zip->entry_raw)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001597 crc32 = mz_stream_crc32_get_value(zip->crc32_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001598
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001599 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001600 {
1601#ifdef HAVE_AES
1602 // AES zip version AE-1 will expect a valid crc as well
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001603 if (zip->file_info.aes_version <= 0x0001)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001604#endif
1605 {
Nathan Moinvaziri6ac2ff42018-07-21 14:29:29 -07001606 mz_stream_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
Nathan Moinvaziri184f4cb2018-07-09 07:53:17 -07001607 // If entire entry was not read this will fail
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07001608 if ((total_in > 0) && (!zip->entry_raw))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001609 {
1610 if (crc32 != zip->file_info.crc)
1611 err = MZ_CRC_ERROR;
1612 }
1613 }
1614 }
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001615
1616 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07001617 if (!zip->entry_raw)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001618 mz_stream_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001619
1620 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
1621 {
1622 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001623 err = mz_stream_close(zip->crypt_stream);
1624
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001625 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001626 }
1627
1628 mz_stream_delete(&zip->crypt_stream);
1629
1630 mz_stream_delete(&zip->compress_stream);
1631 mz_stream_crc32_delete(&zip->crc32_stream);
1632
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001633 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001634 {
1635 if (err == MZ_OK)
1636 {
1637 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
1638 if (err == MZ_OK)
1639 err = mz_stream_write_uint32(zip->stream, crc32);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001640 // Store data descriptor as 8 bytes if zip 64 extension enabled
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001641 if (err == MZ_OK)
1642 {
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001643 // Zip 64 extension is enabled when uncompressed size is > UINT32_mAX
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001644 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001645 err = mz_stream_write_uint32(zip->stream, (uint32_t)compressed_size);
Andrew Gunnerson3c52d212018-01-05 22:28:12 -05001646 else
1647 err = mz_stream_write_uint64(zip->stream, compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001648 }
1649 if (err == MZ_OK)
1650 {
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001651 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001652 err = mz_stream_write_uint32(zip->stream, (uint32_t)uncompressed_size);
Andrew Gunnerson3c52d212018-01-05 22:28:12 -05001653 else
1654 err = mz_stream_write_uint64(zip->stream, uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001655 }
1656 }
1657
1658 zip->file_info.crc = crc32;
1659 zip->file_info.compressed_size = compressed_size;
1660 zip->file_info.uncompressed_size = uncompressed_size;
1661
1662 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001663 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001664
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001665 zip->number_entry += 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001666 }
1667
1668 zip->entry_opened = 0;
1669
1670 return err;
1671}
1672
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001673static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001674{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001675 mz_zip *zip = (mz_zip *)handle;
1676 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001677
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001678 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001679 return MZ_PARAM_ERROR;
1680
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001681 zip->entry_scanned = 0;
1682
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001683 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00001684
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001685 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001686 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001687 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001688 if (err == MZ_OK)
1689 zip->entry_scanned = 1;
1690 return err;
1691}
1692
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001693extern int32_t mz_zip_get_number_entry(void *handle, int64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001694{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001695 mz_zip *zip = (mz_zip *)handle;
1696 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001697 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001698 *number_entry = zip->number_entry;
1699 return MZ_OK;
1700}
1701
Viktor Szakats0f0535f2018-05-02 13:15:58 +00001702extern int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03001703{
1704 mz_zip *zip = (mz_zip *)handle;
1705 if (zip == NULL || disk_number_with_cd == NULL)
1706 return MZ_PARAM_ERROR;
1707 *disk_number_with_cd = zip->disk_number_with_cd;
1708 return MZ_OK;
1709}
1710
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001711extern int64_t mz_zip_get_entry(void *handle)
1712{
1713 mz_zip *zip = (mz_zip *)handle;
1714
1715 if (zip == NULL)
1716 return MZ_PARAM_ERROR;
1717
1718 return zip->cd_current_pos;
1719}
1720
1721extern int32_t mz_zip_goto_entry(void *handle, uint64_t cd_pos)
1722{
1723 mz_zip *zip = (mz_zip *)handle;
1724
1725 if (zip == NULL)
1726 return MZ_PARAM_ERROR;
1727
1728 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
1729 return MZ_PARAM_ERROR;
1730
1731 zip->cd_current_pos = cd_pos;
1732
1733 return mz_zip_goto_next_entry_int(handle);
1734}
1735
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001736extern int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001737{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001738 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001739
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001740 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001741 return MZ_PARAM_ERROR;
1742
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001743 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001744
1745 return mz_zip_goto_next_entry_int(handle);
1746}
1747
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001748extern int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001749{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001750 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001751
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001752 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001753 return MZ_PARAM_ERROR;
1754
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001755 zip->cd_current_pos += MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
1756 zip->file_info.extrafield_size + zip->file_info.comment_size;
1757
1758 return mz_zip_goto_next_entry_int(handle);
1759}
1760
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001761extern int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001762{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001763 mz_zip *zip = (mz_zip *)handle;
1764 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001765 int32_t result = 0;
1766
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001767 if (zip == NULL || filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001768 return MZ_PARAM_ERROR;
1769
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001770 // If we are already on the current entry, no need to search
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001771 if ((zip->entry_scanned) && (zip->file_info.filename != NULL))
1772 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001773 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001774 if (result == 0)
1775 return MZ_OK;
1776 }
1777
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001778 // Search all entries starting at the first
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001779 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001780 while (err == MZ_OK)
1781 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001782 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
1783 if (result == 0)
1784 return MZ_OK;
1785
1786 err = mz_zip_goto_next_entry(handle);
1787 }
1788
1789 return err;
1790}
1791
1792extern int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
1793{
1794 mz_zip *zip = (mz_zip *)handle;
1795 int32_t err = MZ_OK;
1796 int32_t result = 0;
1797
1798 // Search first entry looking for match
1799 err = mz_zip_goto_first_entry(handle);
1800 if (err != MZ_OK)
1801 return err;
1802
1803 result = cb(handle, userdata, &zip->file_info);
1804 if (result == 0)
1805 return MZ_OK;
1806
1807 return mz_zip_locate_next_entry(handle, userdata, cb);
1808}
1809
1810extern int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
1811{
1812 mz_zip *zip = (mz_zip *)handle;
1813 int32_t err = MZ_OK;
1814 int32_t result = 0;
1815
1816 // Search next entries looking for match
1817 err = mz_zip_goto_next_entry(handle);
1818 while (err == MZ_OK)
1819 {
1820 result = cb(handle, userdata, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001821 if (result == 0)
1822 return MZ_OK;
1823
1824 err = mz_zip_goto_next_entry(handle);
1825 }
1826
1827 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001828}
1829
1830/***************************************************************************/
1831
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07001832int32_t mz_zip_attrib_is_dir(int32_t attributes, int32_t version_madeby)
1833{
1834 int32_t host_system = (uint8_t)(version_madeby >> 8);
1835
1836 if (host_system == MZ_HOST_SYSTEM_MSDOS || host_system == MZ_HOST_SYSTEM_WINDOWS_NTFS)
1837 {
1838 if ((attributes & 0x10) == 0x10) // FILE_ATTRIBUTE_DIRECTORY
1839 return MZ_OK;
1840 }
1841 else if (host_system == MZ_HOST_SYSTEM_UNIX || host_system == MZ_HOST_SYSTEM_OSX_DARWIN)
1842 {
1843 if ((attributes & 00170000) == 0040000) // S_ISDIR
1844 return MZ_OK;
1845 }
1846
1847 return MZ_EXIST_ERROR;
1848}
1849
1850/***************************************************************************/
1851
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001852static int32_t mz_zip_invalid_date(const struct tm *ptm)
1853{
1854#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001855 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || // 1980-based year, allow 80 extra
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001856 !datevalue_in_range(0, 11, ptm->tm_mon) ||
1857 !datevalue_in_range(1, 31, ptm->tm_mday) ||
1858 !datevalue_in_range(0, 23, ptm->tm_hour) ||
1859 !datevalue_in_range(0, 59, ptm->tm_min) ||
1860 !datevalue_in_range(0, 59, ptm->tm_sec));
1861#undef datevalue_in_range
1862}
1863
1864static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
1865{
1866 uint64_t date = (uint64_t)(dos_date >> 16);
1867
1868 ptm->tm_mday = (uint16_t)(date & 0x1f);
1869 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
1870 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
1871 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
1872 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
1873 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
1874 ptm->tm_isdst = -1;
1875}
1876
1877int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
1878{
1879 if (ptm == NULL)
1880 return MZ_PARAM_ERROR;
1881
1882 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
1883
1884 if (mz_zip_invalid_date(ptm))
1885 {
1886 // Invalid date stored, so don't return it
1887 memset(ptm, 0, sizeof(struct tm));
1888 return MZ_FORMAT_ERROR;
1889 }
1890 return MZ_OK;
1891}
1892
1893time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
1894{
1895 struct tm ptm;
1896 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
1897 return mktime(&ptm);
1898}
1899
1900int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
1901{
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001902 struct tm *ltm = NULL;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001903 if (ptm == NULL)
1904 return MZ_PARAM_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001905 ltm = localtime(&unix_time); // Returns a 1900-based year
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001906 if (ltm == NULL)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001907 {
1908 // Invalid date stored, so don't return it
1909 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001910 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001911 }
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001912 memcpy(ptm, ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001913 return MZ_OK;
1914}
1915
1916uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
1917{
1918 struct tm ptm;
1919 mz_zip_time_t_to_tm(unix_time, &ptm);
1920 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
1921}
1922
1923uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
1924{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07001925 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001926
1927 // Years supported:
1928
1929 // [00, 79] (assumed to be between 2000 and 2079)
1930 // [80, 207] (assumed to be between 1980 and 2107, typical output of old
1931 // software that does 'year-1900' to get a double digit year)
1932 // [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.)
1933
1934 memcpy(&fixed_tm, ptm, sizeof(struct tm));
1935 if (fixed_tm.tm_year >= 1980) // range [1980, 2107]
1936 fixed_tm.tm_year -= 1980;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001937 else if (fixed_tm.tm_year >= 80) // range [80, 207]
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001938 fixed_tm.tm_year -= 80;
1939 else // range [00, 79]
1940 fixed_tm.tm_year += 20;
1941
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001942 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001943 return 0;
1944
1945 return (uint32_t)(((fixed_tm.tm_mday) + (32 * (fixed_tm.tm_mon + 1)) + (512 * fixed_tm.tm_year)) << 16) |
1946 ((fixed_tm.tm_sec / 2) + (32 * fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
1947}
1948
1949int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
1950{
1951 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
1952 return MZ_OK;
1953}
1954
1955int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
1956{
1957 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
1958 return MZ_OK;
1959}
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001960
1961int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case)
1962{
1963 do
1964 {
1965 if ((*path1 == '\\' && *path2 == '/') ||
1966 (*path2 == '\\' && *path1 == '/'))
1967 {
1968 // Ignore comparison of path slashes
1969 }
1970 else if (ignore_case)
1971 {
1972 if (tolower(*path1) != tolower(*path2))
1973 break;
1974 }
1975 else if (*path1 != *path2)
1976 {
1977 break;
1978 }
1979
1980 path1 += 1;
1981 path2 += 1;
1982 }
1983 while (*path1 != 0 && *path2 != 0);
1984
1985 if (ignore_case)
1986 return (int32_t)(tolower(*path1) - tolower(*path2));
1987
1988 return (int32_t)(*path1 - *path2);
1989}
1990
1991/***************************************************************************/