blob: 3aac1dc61ce2627c82e298f45aefddc0b875e6fb [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 Moinvaziri9eab88e2017-10-22 14:32:37 -070021#include <time.h>
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080022#include <errno.h>
Nathan Moinvaziri34eff622018-01-22 09:25:15 -080023#include <limits.h>
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080024
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070025#include "mz.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080026#include "mz_strm.h"
27#ifdef HAVE_AES
28# include "mz_strm_aes.h"
29#endif
30#ifdef HAVE_BZIP2
31# include "mz_strm_bzip.h"
32#endif
Nathan Moinvazirib47b41b2018-07-11 09:44:29 -070033#include "mz_strm_crc32.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080034#ifdef HAVE_LZMA
35# include "mz_strm_lzma.h"
36#endif
Nathan Moinvazirib47b41b2018-07-11 09:44:29 -070037#include "mz_strm_mem.h"
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -070038#ifdef HAVE_PKCRYPT
39# include "mz_strm_pkcrypt.h"
40#endif
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -080041#ifdef HAVE_ZLIB
42# include "mz_strm_zlib.h"
43#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080044
45#include "mz_zip.h"
46
47/***************************************************************************/
48
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070049#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
50#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
51#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
52#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
53#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
54#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
55
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070056#define MZ_ZIP_SIZE_CD_ITEM (0x2e)
57#define MZ_ZIP_SIZE_CD_LOCATOR64 (0x14)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080058
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070059#define MZ_ZIP_EXTENSION_ZIP64 (0x0001)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -070060#define MZ_ZIP_EXTENSION_NTFS (0x000a)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070061#define MZ_ZIP_EXTENSION_AES (0x9901)
62
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080063/***************************************************************************/
64
65typedef struct mz_zip_s
66{
67 mz_zip_file file_info;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070068 mz_zip_file local_file_info;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080069
70 void *stream; // main stream
Nathan Moinvaziricda36002017-10-21 09:37:18 -070071 void *cd_stream; // pointer to the stream with the cd
72 void *cd_mem_stream; // memory stream for central directory
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080073 void *compress_stream; // compression stream
74 void *crc32_stream; // crc32 stream
75 void *crypt_stream; // encryption stream
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070076 void *file_info_stream; // memory stream for storing file info
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -070077 void *local_file_info_stream; // memory stream for storing local file info
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080078
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070079 int32_t open_mode;
80
Nathan Moinvazirifd039e32017-10-22 14:40:39 -070081 uint32_t disk_number_with_cd; // number of the disk with the central dir
Nathan Moinvaziriee614ff2018-07-12 12:36:33 -070082 uint64_t disk_offset_shift; // correction for zips that have wrong offset start of cd
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070083
Nathan Moinvaziricda36002017-10-21 09:37:18 -070084 uint64_t cd_start_pos; // pos of the first file in the central dir stream
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070085 uint64_t cd_current_pos; // pos of the current file in the central dir
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070086 uint64_t cd_offset; // offset of start of central directory
87 uint64_t cd_size; // size of the central directory
88
Viktor Szakats915b82e2018-04-24 10:02:39 +000089 uint16_t entry_scanned;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070090 uint16_t entry_opened; // 1 if a file in the zip is currently writ.
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070091
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070092 int64_t number_entry;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070093
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -070094 uint16_t version_madeby;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070095 char *comment;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080096} mz_zip;
97
98/***************************************************************************/
99
Nathan Moinvazirie824da82018-07-26 17:53:15 -0700100// 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 -0700101static int32_t mz_zip_search_eocd(void *stream, uint64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800102{
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700103 uint8_t buf[1024 + 4];
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700104 int64_t file_size = 0;
105 int64_t back_read = 0;
106 int64_t max_back = UINT16_MAX; // maximum size of global comment
107 int32_t read_size = sizeof(buf);
108 int64_t read_pos = 0;
109 int32_t i = 0;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700110
Nathan Moinvazirif6e81cd2017-10-10 18:24:03 -0700111 *central_pos = 0;
112
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700113 if (mz_stream_seek(stream, 0, MZ_SEEK_END) != MZ_OK)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700114 return MZ_STREAM_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800115
116 file_size = mz_stream_tell(stream);
117
118 if (max_back > file_size)
119 max_back = file_size;
120
121 while (back_read < max_back)
122 {
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700123 back_read += (sizeof(buf) - 4);
124 if (back_read > max_back)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800125 back_read = max_back;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800126
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700127 read_pos = file_size - back_read;
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700128 if (read_size > (file_size - read_pos))
129 read_size = (uint32_t)(file_size - read_pos);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800130
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700131 if (mz_stream_seek(stream, read_pos, MZ_SEEK_SET) != MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800132 break;
133 if (mz_stream_read(stream, buf, read_size) != read_size)
134 break;
135
136 for (i = read_size - 3; (i--) > 0;)
137 {
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700138 if (((*(buf + i)) == (MZ_ZIP_MAGIC_ENDHEADER & 0xff)) &&
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700139 ((*(buf + i + 1)) == (MZ_ZIP_MAGIC_ENDHEADER >> 8 & 0xff)) &&
140 ((*(buf + i + 2)) == (MZ_ZIP_MAGIC_ENDHEADER >> 16 & 0xff)) &&
141 ((*(buf + i + 3)) == (MZ_ZIP_MAGIC_ENDHEADER >> 24 & 0xff)))
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800142 {
Nathan Moinvazirif6e81cd2017-10-10 18:24:03 -0700143 *central_pos = read_pos + i;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700144 return MZ_OK;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800145 }
146 }
147
Nathan Moinvazirif6e81cd2017-10-10 18:24:03 -0700148 if (*central_pos != 0)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800149 break;
150 }
151
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700152 return MZ_EXIST_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800153}
154
Nathan Moinvazirie824da82018-07-26 17:53:15 -0700155// Locate the end of central directory 64 of a zip file
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700156static 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 +0800157{
158 uint64_t offset = 0;
159 uint32_t value32 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700160 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700161
162
163 *central_pos = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800164
165 // Zip64 end of central directory locator
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700166 err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800167 // Read locator signature
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700168 if (err == MZ_OK)
169 {
170 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700171 if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700172 err = MZ_FORMAT_ERROR;
173 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800174 // Number of the disk with the start of the zip64 end of central directory
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700175 if (err == MZ_OK)
176 err = mz_stream_read_uint32(stream, &value32);
177 // Relative offset of the zip64 end of central directory record8
178 if (err == MZ_OK)
179 err = mz_stream_read_uint64(stream, &offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800180 // Total number of disks
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700181 if (err == MZ_OK)
182 err = mz_stream_read_uint32(stream, &value32);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800183 // Goto end of central directory record
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700184 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700185 err = mz_stream_seek(stream, offset, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800186 // The signature
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700187 if (err == MZ_OK)
188 {
189 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700190 if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700191 err = MZ_FORMAT_ERROR;
192 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800193
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700194 if (err == MZ_OK)
195 *central_pos = offset;
196
197 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800198}
199
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700200static int32_t mz_zip_read_cd(void *handle)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800201{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700202 mz_zip *zip = (mz_zip *)handle;
203 int64_t number_entry_cd = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700204 uint64_t number_entry_cd64 = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800205 uint64_t number_entry = 0;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700206 uint64_t eocd_pos = 0;
207 uint64_t eocd_pos64 = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800208 uint16_t value16 = 0;
209 uint32_t value32 = 0;
210 uint64_t value64 = 0;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700211 uint16_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700212 int32_t err = MZ_OK;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800213
214
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800215 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700216 return MZ_PARAM_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800217
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700218 // Read and cache central directory records
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700219 if (mz_zip_search_eocd(zip->stream, &eocd_pos) == MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800220 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700221 // Read end of central directory info
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700222 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700223 // The signature, already checked
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800224 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700225 err = mz_stream_read_uint32(zip->stream, &value32);
226 // Number of this disk
227 if (err == MZ_OK)
228 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700229 // Number of the disk with the start of the central directory
230 if (err == MZ_OK)
231 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700232 zip->disk_number_with_cd = value16;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700233 // Total number of entries in the central dir on this disk
234 if (err == MZ_OK)
235 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700236 zip->number_entry = value16;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700237 // Total number of entries in the central dir
238 if (err == MZ_OK)
239 err = mz_stream_read_uint16(zip->stream, &value16);
240 number_entry_cd = value16;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700241 if (number_entry_cd != zip->number_entry)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700242 err = MZ_FORMAT_ERROR;
243 // Size of the central directory
244 if (err == MZ_OK)
245 err = mz_stream_read_uint32(zip->stream, &value32);
246 if (err == MZ_OK)
247 zip->cd_size = value32;
248 // Offset of start of central directory with respect to the starting disk number
249 if (err == MZ_OK)
250 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvaziriee614ff2018-07-12 12:36:33 -0700251 if (err == MZ_OK)
252 zip->cd_offset = value32;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700253 // Zip file global comment length
254 if (err == MZ_OK)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700255 err = mz_stream_read_uint16(zip->stream, &comment_size);
Nathan Moinvaziri51bf61d2018-07-26 16:41:54 -0700256 if ((err == MZ_OK) && (comment_size > 0))
257 {
258 zip->comment = (char *)MZ_ALLOC(comment_size + 1);
259 if (zip->comment)
260 {
261 if (mz_stream_read(zip->stream, zip->comment, comment_size) != comment_size)
262 err = MZ_STREAM_ERROR;
263 zip->comment[comment_size] = 0;
264 }
265 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800266
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700267 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800268 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700269 // Format should be Zip64, as the central directory or file size is too large
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700270 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800271 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700272 eocd_pos = eocd_pos64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700273
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700274 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700275 // The signature, already checked
276 if (err == MZ_OK)
277 err = mz_stream_read_uint32(zip->stream, &value32);
278 // Size of zip64 end of central directory record
279 if (err == MZ_OK)
280 err = mz_stream_read_uint64(zip->stream, &value64);
281 // Version made by
282 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700283 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700284 // Version needed to extract
285 if (err == MZ_OK)
286 err = mz_stream_read_uint16(zip->stream, &value16);
287 // Number of this disk
288 if (err == MZ_OK)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -0700289 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700290 // Number of the disk with the start of the central directory
291 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700292 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700293 // Total number of entries in the central directory on this disk
294 if (err == MZ_OK)
295 err = mz_stream_read_uint64(zip->stream, &number_entry);
296 // Total number of entries in the central directory
297 if (err == MZ_OK)
298 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
299 if (number_entry == UINT32_MAX)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700300 zip->number_entry = number_entry_cd64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700301 // Size of the central directory
302 if (err == MZ_OK)
303 err = mz_stream_read_uint64(zip->stream, &zip->cd_size);
304 // Offset of start of central directory with respect to the starting disk number
305 if (err == MZ_OK)
306 err = mz_stream_read_uint64(zip->stream, &zip->cd_offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800307 }
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700308 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700309 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700310 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700311 err = MZ_FORMAT_ERROR;
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700312 }
313 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800314 }
315
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700316 if (err == MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800317 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700318 if (eocd_pos < zip->cd_offset + zip->cd_size)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700319 err = MZ_FORMAT_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800320 }
321
Nathan Moinvaziriee614ff2018-07-12 12:36:33 -0700322 if (err == MZ_OK)
323 {
324 // Verify central directory signature exists at offset
325 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
326 if (err == MZ_OK)
327 err = mz_stream_read_uint32(zip->stream, &value32);
328 if (value32 != MZ_ZIP_MAGIC_CENTRALHEADER)
329 {
330 // If not found attempt to seek backward to find it
331 err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
332 if (err == MZ_OK)
333 err = mz_stream_read_uint32(zip->stream, &value32);
334 if (value32 == MZ_ZIP_MAGIC_CENTRALHEADER)
335 {
336 // If found compensate for incorrect locations
337 value64 = zip->cd_offset;
338 zip->cd_offset = eocd_pos - zip->cd_size;
339 zip->disk_offset_shift = zip->cd_offset - value64;
340 }
341 }
342 }
343
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800344 return err;
345}
346
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700347static int32_t mz_zip_write_cd(void *handle)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800348{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700349 mz_zip *zip = (mz_zip *)handle;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800350 uint16_t comment_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700351 uint64_t zip64_eocd_pos_inzip = 0;
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700352 int64_t disk_number = 0;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -0700353 int64_t disk_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700354 int32_t err = MZ_OK;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800355
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700356
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700357 if (zip == NULL)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800358 return MZ_PARAM_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000359
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700360 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700361 zip->disk_number_with_cd = (uint32_t)disk_number;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -0700362 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 -0700363 zip->disk_number_with_cd += 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700364 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800365
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700366 zip->cd_offset = mz_stream_tell(zip->stream);
367 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700368 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700369 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
Viktor Szakats915b82e2018-04-24 10:02:39 +0000370
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700371 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800372
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800373 // Write the ZIP64 central directory header
juanii3f59ffc2018-02-08 12:44:17 -0300374 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800375 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700376 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800377
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700378 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800379
380 // Size of this 'zip64 end of central directory'
381 if (err == MZ_OK)
382 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
383 // Version made by
384 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700385 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800386 // Version needed
387 if (err == MZ_OK)
388 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
389 // Number of this disk
390 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700391 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800392 // Number of the disk with the start of the central directory
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 // Total number of entries in the central dir on this disk
396 if (err == MZ_OK)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700397 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800398 // Total number of entries in the central dir
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 // Size of the central directory
402 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700403 err = mz_stream_write_uint64(zip->stream, (uint64_t)zip->cd_size);
404 // Offset of start of central directory with respect to the starting disk number
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800405 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700406 err = mz_stream_write_uint64(zip->stream, zip->cd_offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800407 if (err == MZ_OK)
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700408 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700409
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800410 // Number of the disk with the start of the central directory
411 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700412 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800413 // Relative offset to the end of zip64 central directory
414 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700415 err = mz_stream_write_uint64(zip->stream, zip64_eocd_pos_inzip);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800416 // Number of the disk with the start of the central directory
417 if (err == MZ_OK)
juaniic65486d2018-02-08 17:07:07 -0300418 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800419 }
420
421 // Write the central directory header
422
Viktor Szakats915b82e2018-04-24 10:02:39 +0000423 // Signature
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800424 if (err == MZ_OK)
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700425 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800426 // Number of this disk
427 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700428 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800429 // Number of the disk with the start of the central directory
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 // Total number of entries in the central dir on this disk
433 if (err == MZ_OK)
434 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700435 if (zip->number_entry >= UINT16_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800436 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
437 else
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700438 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800439 }
440 // Total number of entries in the central dir
441 if (err == MZ_OK)
442 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700443 if (zip->number_entry >= UINT16_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800444 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
445 else
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700446 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800447 }
448 // Size of the central directory
449 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700450 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800451 // Offset of start of central directory with respect to the starting disk number
452 if (err == MZ_OK)
453 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700454 if (zip->cd_offset >= UINT32_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800455 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
456 else
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700457 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800458 }
459
460 // Write global comment
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700461 if (zip->comment != NULL)
462 comment_size = (uint16_t)strlen(zip->comment);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800463 if (err == MZ_OK)
464 err = mz_stream_write_uint16(zip->stream, comment_size);
465 if (err == MZ_OK)
466 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700467 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800468 err = MZ_STREAM_ERROR;
469 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700470 return err;
471}
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800472
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800473extern void* mz_zip_open(void *stream, int32_t mode)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700474{
475 mz_zip *zip = NULL;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700476 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700477
478
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700479 zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700480 if (zip == NULL)
481 return NULL;
482
483 memset(zip, 0, sizeof(mz_zip));
484
485 zip->stream = stream;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700486
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800487 if (mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700488 {
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700489 mz_stream_mem_create(&zip->cd_mem_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700490 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700491
492 zip->cd_stream = zip->cd_mem_stream;
493 }
494 else
495 {
496 zip->cd_stream = stream;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700497 }
498
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800499 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700500 {
Nathan Moinvaziri446d0622018-07-16 19:17:46 -0700501 if ((mode & MZ_OPEN_MODE_CREATE) == 0)
502 err = mz_zip_read_cd(zip);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700503
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700504 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700505 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700506 if (zip->cd_size > 0)
507 {
508 // Store central directory in memory
509 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
510 if (err == MZ_OK)
511 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (uint32_t)zip->cd_size);
512 if (err == MZ_OK)
513 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
514 }
515 else
516 {
517 // If no central directory, append new zip to end of file
518 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
519 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700520 }
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700521 else
522 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700523 zip->cd_start_pos = zip->cd_offset;
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700524 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700525 }
526
527 if (err == MZ_OK)
528 {
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700529 // Memory streams used to store variable length file info data
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700530 mz_stream_mem_create(&zip->file_info_stream);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700531 mz_stream_mem_set_grow_size(zip->file_info_stream, 4096);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700532 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700533
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700534 mz_stream_mem_create(&zip->local_file_info_stream);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700535 mz_stream_mem_set_grow_size(zip->local_file_info_stream, 4096);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700536 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700537 }
538
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700539 if (err != MZ_OK)
540 {
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800541 mz_zip_close(zip);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700542 return NULL;
543 }
544
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800545 zip->open_mode = mode;
546
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700547 return zip;
548}
549
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800550extern int32_t mz_zip_close(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700551{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700552 mz_zip *zip = (mz_zip *)handle;
553 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700554
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700555 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700556 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700557
558 if (zip->entry_opened == 1)
559 {
560 err = mz_zip_entry_close(handle);
561 if (err != MZ_OK)
562 return err;
563 }
564
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700565 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700566 err = mz_zip_write_cd(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700567
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700568 if (zip->cd_mem_stream != NULL)
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700569 {
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700570 mz_stream_close(zip->cd_mem_stream);
571 mz_stream_delete(&zip->cd_mem_stream);
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700572 }
573
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800574 if (zip->file_info_stream != NULL)
575 {
576 mz_stream_mem_close(zip->file_info_stream);
577 mz_stream_mem_delete(&zip->file_info_stream);
578 }
579 if (zip->local_file_info_stream != NULL)
580 {
581 mz_stream_mem_close(zip->local_file_info_stream);
582 mz_stream_mem_delete(&zip->local_file_info_stream);
583 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700584
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700585 if (zip->comment)
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700586 MZ_FREE(zip->comment);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700587
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700588 MZ_FREE(zip);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800589
590 return err;
591}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700592
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800593extern int32_t mz_zip_get_comment(void *handle, const char **comment)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700594{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700595 mz_zip *zip = (mz_zip *)handle;
596 if (zip == NULL || comment == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700597 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700598 if (zip->comment == NULL)
599 return MZ_EXIST_ERROR;
600 *comment = zip->comment;
601 return MZ_OK;
602}
603
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800604extern int32_t mz_zip_set_comment(void *handle, const char *comment)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700605{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700606 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700607 uint16_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700608 if (zip == NULL || comment == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700609 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700610 if (zip->comment != NULL)
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700611 MZ_FREE(zip->comment);
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700612 comment_size = (uint16_t)(strlen(comment) + 1);
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700613 zip->comment = (char *)MZ_ALLOC(comment_size);
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700614 strncpy(zip->comment, comment, comment_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700615 return MZ_OK;
616}
617
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800618extern int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700619{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700620 mz_zip *zip = (mz_zip *)handle;
621 if (zip == NULL || version_madeby == NULL)
622 return MZ_PARAM_ERROR;
623 *version_madeby = zip->version_madeby;
624 return MZ_OK;
625}
626
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800627extern int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700628{
629 mz_zip *zip = (mz_zip *)handle;
630 if (zip == NULL)
631 return MZ_PARAM_ERROR;
632 zip->version_madeby = version_madeby;
633 return MZ_OK;
634}
635
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700636// Get info about the current file in the zip file
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700637static 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 -0700638{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700639 uint64_t ntfs_time = 0;
640 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700641 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700642 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700643 uint32_t extra_pos = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700644 uint32_t extra_data_size_read = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700645 uint16_t extra_header_id = 0;
646 uint16_t extra_data_size = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700647 uint16_t ntfs_attrib_id = 0;
648 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700649 uint16_t value16 = 0;
650 uint32_t value32 = 0;
651 uint64_t value64 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700652 int64_t max_seek = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700653 int64_t seek = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700654 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700655
656
657 memset(file_info, 0, sizeof(mz_zip_file));
658
659 // Check the magic
660 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700661 if (err == MZ_END_OF_STREAM)
662 err = MZ_END_OF_LIST;
663 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700664 err = MZ_END_OF_LIST;
665 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
666 err = MZ_FORMAT_ERROR;
667 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
668 err = MZ_FORMAT_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000669
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700670 // Read header fields
671 if (err == MZ_OK)
672 {
673 if (!local)
674 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
675 if (err == MZ_OK)
676 err = mz_stream_read_uint16(stream, &file_info->version_needed);
677 if (err == MZ_OK)
678 err = mz_stream_read_uint16(stream, &file_info->flag);
679 if (err == MZ_OK)
680 err = mz_stream_read_uint16(stream, &file_info->compression_method);
681 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700682 {
683 err = mz_stream_read_uint32(stream, &dos_date);
684 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
685 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700686 if (err == MZ_OK)
687 err = mz_stream_read_uint32(stream, &file_info->crc);
688 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700689 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700690 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700691 file_info->compressed_size = value32;
692 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700693 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700694 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700695 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700696 file_info->uncompressed_size = value32;
697 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700698 if (err == MZ_OK)
699 err = mz_stream_read_uint16(stream, &file_info->filename_size);
700 if (err == MZ_OK)
701 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
702 if (!local)
703 {
704 if (err == MZ_OK)
705 err = mz_stream_read_uint16(stream, &file_info->comment_size);
706 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700707 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700708 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700709 file_info->disk_number = value16;
710 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700711 if (err == MZ_OK)
712 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
713 if (err == MZ_OK)
714 err = mz_stream_read_uint32(stream, &file_info->external_fa);
715 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700716 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700717 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700718 file_info->disk_offset = value32;
719 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700720 }
721 }
722
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700723 max_seek = file_info->filename_size + file_info->extrafield_size + file_info->comment_size + 3;
724 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700725 err = mz_stream_seek(file_info_stream, max_seek, MZ_SEEK_SET);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700726 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700727 err = mz_stream_seek(file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700728
729 if ((err == MZ_OK) && (file_info->filename_size > 0))
730 {
Nathan Moinvaziria710bd72018-05-09 00:46:47 -0700731 mz_stream_mem_get_buffer(file_info_stream, (const void **)&file_info->filename);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700732
733 err = mz_stream_copy(file_info_stream, stream, file_info->filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700734 if (err == MZ_OK)
735 err = mz_stream_write_uint8(file_info_stream, 0);
736
737 seek += file_info->filename_size + 1;
738 }
739
740 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
741 {
Nathan Moinvaziria710bd72018-05-09 00:46:47 -0700742 mz_stream_mem_get_buffer_at(file_info_stream, seek, (const void **)&file_info->extrafield);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700743
744 err = mz_stream_copy(file_info_stream, stream, file_info->extrafield_size);
745 if (err == MZ_OK)
746 err = mz_stream_write_uint8(file_info_stream, 0);
747
748 // Seek back and parse the extra field
749 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700750 err = mz_stream_seek(file_info_stream, seek, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700751
752 seek += file_info->extrafield_size + 1;
753
754 while ((err == MZ_OK) && (extra_pos < file_info->extrafield_size))
755 {
756 err = mz_stream_read_uint16(file_info_stream, &extra_header_id);
757 if (err == MZ_OK)
758 err = mz_stream_read_uint16(file_info_stream, &extra_data_size);
759
760 // ZIP64 extra field
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700761 if (extra_header_id == MZ_ZIP_EXTENSION_ZIP64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700762 {
763 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
764 err = mz_stream_read_uint64(file_info_stream, &file_info->uncompressed_size);
765 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
766 err = mz_stream_read_uint64(file_info_stream, &file_info->compressed_size);
767 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700768 err = mz_stream_read_uint64(file_info_stream, &file_info->disk_offset);
juanii4ae79922018-02-11 14:29:36 -0300769 if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700770 err = mz_stream_read_uint32(file_info_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700771 }
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700772 // NTFS extra field
773 else if (extra_header_id == MZ_ZIP_EXTENSION_NTFS)
774 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700775 if (err == MZ_OK)
776 err = mz_stream_read_uint32(file_info_stream, &reserved);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700777 extra_data_size_read = 4;
778
779 while ((err == MZ_OK) && (extra_data_size_read < extra_data_size))
780 {
781 err = mz_stream_read_uint16(file_info_stream, &ntfs_attrib_id);
782 if (err == MZ_OK)
783 err = mz_stream_read_uint16(file_info_stream, &ntfs_attrib_size);
784
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700785 if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700786 {
787 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
788 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
789
juanii7063b0e2018-02-11 13:56:21 -0300790 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700791 {
792 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
793 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
794 }
juanii7063b0e2018-02-11 13:56:21 -0300795 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700796 {
797 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
798 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
799 }
800 }
801 else
802 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700803 if (err == MZ_OK)
804 err = mz_stream_seek(file_info_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700805 }
806
807 extra_data_size_read += ntfs_attrib_size + 4;
808 }
809 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700810#ifdef HAVE_AES
811 // AES extra field
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700812 else if (extra_header_id == MZ_ZIP_EXTENSION_AES)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700813 {
814 uint8_t value8 = 0;
815 // Verify version info
816 err = mz_stream_read_uint16(file_info_stream, &value16);
817 // Support AE-1 and AE-2
818 if (value16 != 1 && value16 != 2)
819 err = MZ_FORMAT_ERROR;
820 file_info->aes_version = value16;
821 if (err == MZ_OK)
822 err = mz_stream_read_uint8(file_info_stream, &value8);
823 if ((char)value8 != 'A')
824 err = MZ_FORMAT_ERROR;
825 if (err == MZ_OK)
826 err = mz_stream_read_uint8(file_info_stream, &value8);
827 if ((char)value8 != 'E')
828 err = MZ_FORMAT_ERROR;
829 // Get AES encryption strength and actual compression method
830 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700831 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700832 err = mz_stream_read_uint8(file_info_stream, &value8);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700833 file_info->aes_encryption_mode = value8;
834 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700835 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700836 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700837 err = mz_stream_read_uint16(file_info_stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700838 file_info->compression_method = value16;
839 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700840 }
841#endif
842 else
843 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700844 err = mz_stream_seek(file_info_stream, extra_data_size, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700845 }
846
847 extra_pos += 4 + extra_data_size;
848 }
849 }
850
851 if ((err == MZ_OK) && (file_info->comment_size > 0))
852 {
Nathan Moinvaziria710bd72018-05-09 00:46:47 -0700853 mz_stream_mem_get_buffer_at(file_info_stream, seek, (const void **)&file_info->comment);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700854
855 err = mz_stream_copy(file_info_stream, stream, file_info->comment_size);
856 if (err == MZ_OK)
857 err = mz_stream_write_uint8(file_info_stream, 0);
858 }
859
860 return err;
861}
862
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700863static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700864{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700865 uint64_t ntfs_time = 0;
866 uint32_t reserved = 0;
867 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700868 uint16_t extrafield_size = 0;
869 uint16_t extrafield_zip64_size = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700870 uint16_t extrafield_ntfs_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700871 uint16_t filename_size = 0;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700872 uint16_t filename_length = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700873 uint16_t comment_size = 0;
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700874 uint16_t version_needed = 0;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700875 uint16_t field_type = 0;
876 uint16_t field_length = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700877 int32_t err = MZ_OK;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700878 int32_t err_mem = MZ_OK;
879 uint8_t zip64 = 0;
880 uint8_t skip_aes = 0;
881 void *extrafield_ms = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700882
883 if (file_info == NULL)
884 return MZ_PARAM_ERROR;
885
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700886 // Calculate extra field sizes
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700887 if (file_info->uncompressed_size >= UINT32_MAX)
888 extrafield_zip64_size += 8;
889 if (file_info->compressed_size >= UINT32_MAX)
890 extrafield_zip64_size += 8;
891 if (file_info->disk_offset >= UINT32_MAX)
892 extrafield_zip64_size += 8;
893
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700894 if (file_info->zip64 == MZ_ZIP64_AUTO)
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700895 {
896 // If uncompressed size is unknown, assume zip64 for 64-bit data descriptors
897 zip64 = (local && file_info->uncompressed_size == 0) || (extrafield_zip64_size > 0);
898 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700899 else if (file_info->zip64 == MZ_ZIP64_FORCE)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700900 {
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700901 zip64 = 1;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700902 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700903 else if (file_info->zip64 == MZ_ZIP64_DISABLE)
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700904 {
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700905 // Zip64 extension is required to zip file
906 if (extrafield_zip64_size > 0)
907 return MZ_PARAM_ERROR;
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700908 }
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700909
910 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700911 {
912 extrafield_size += 4;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700913 extrafield_size += extrafield_zip64_size;
914 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700915
916 // Calculate extra field size and check for duplicates
917 if (file_info->extrafield_size > 0)
918 {
919 mz_stream_mem_create(&extrafield_ms);
920 mz_stream_mem_set_buffer(extrafield_ms, (void *)file_info->extrafield, file_info->extrafield_size);
921
922 do
923 {
924 err_mem = mz_stream_read_uint16(extrafield_ms, &field_type);
925 if (err_mem == MZ_OK)
926 err_mem = mz_stream_read_uint16(extrafield_ms, &field_length);
927 if (err_mem != MZ_OK)
928 break;
929
930 // Prefer incoming ntfs, aes extensions over ours
931 if (field_type == MZ_ZIP_EXTENSION_AES)
932 skip_aes = 1;
933
934 // Prefer our zip64, ntfs extension over incoming
935 if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS)
936 extrafield_size += 4 + field_length;
937
938 if (err_mem == MZ_OK)
939 err_mem = mz_stream_seek(extrafield_ms, field_length, SEEK_CUR);
940 }
941 while (err_mem == MZ_OK);
942 }
943
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700944#ifdef HAVE_AES
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700945 if (!skip_aes)
946 {
947 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
948 extrafield_size += 4 + 7;
949 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700950#endif
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700951 // NTFS timestamps
Nathan Moinvazirid9e159a2018-02-13 15:30:30 -0800952 if ((file_info->modified_date != 0) &&
953 (file_info->accessed_date != 0) &&
954 (file_info->creation_date != 0))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700955 {
juanii3679a3d2018-02-11 13:55:38 -0300956 extrafield_ntfs_size += 8 + 8 + 8 + 4 + 2 + 2;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700957 extrafield_size += 4;
958 extrafield_size += extrafield_ntfs_size;
959 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700960
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700961 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700962 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700963 else
964 {
965 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
966 if (err == MZ_OK)
967 err = mz_stream_write_uint16(stream, file_info->version_madeby);
968 }
969
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700970 // Calculate version needed to extract
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700971 if (err == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700972 {
973 version_needed = file_info->version_needed;
974 if (version_needed == 0)
975 {
976 version_needed = 20;
977 if (zip64)
978 version_needed = 45;
979#ifdef HAVE_AES
980 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
981 version_needed = 51;
982#endif
983#ifdef HAVE_LZMA
984 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
985 version_needed = 63;
986#endif
987 }
988 err = mz_stream_write_uint16(stream, version_needed);
989 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700990 if (err == MZ_OK)
991 err = mz_stream_write_uint16(stream, file_info->flag);
992 if (err == MZ_OK)
993 {
994#ifdef HAVE_AES
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700995 if (file_info->aes_version)
996 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700997 else
998#endif
999 err = mz_stream_write_uint16(stream, file_info->compression_method);
1000 }
1001 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001002 {
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -07001003 if (file_info->modified_date != 0)
1004 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001005 err = mz_stream_write_uint32(stream, dos_date);
1006 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001007
1008 if (err == MZ_OK)
1009 err = mz_stream_write_uint32(stream, file_info->crc); // crc
1010 if (err == MZ_OK)
1011 {
1012 if (file_info->compressed_size >= UINT32_MAX) // compr size
1013 err = mz_stream_write_uint32(stream, UINT32_MAX);
1014 else
1015 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
1016 }
1017 if (err == MZ_OK)
1018 {
1019 if (file_info->uncompressed_size >= UINT32_MAX) // uncompr size
1020 err = mz_stream_write_uint32(stream, UINT32_MAX);
1021 else
1022 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
1023 }
1024
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001025 filename_length = (uint16_t)strlen(file_info->filename);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001026 if (err == MZ_OK)
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001027 {
1028 filename_size = filename_length;
1029 if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -07001030 {
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001031 if ((file_info->filename[filename_length - 1] == '/') ||
Nathan Moinvaziri46f71432018-05-03 17:24:32 -07001032 (file_info->filename[filename_length - 1] == '\\'))
1033 filename_length -= 1;
1034 else
1035 filename_size += 1;
1036 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001037 err = mz_stream_write_uint16(stream, filename_size);
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001038 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001039 if (err == MZ_OK)
1040 err = mz_stream_write_uint16(stream, extrafield_size);
1041
1042 if (!local)
1043 {
1044 if (file_info->comment != NULL)
1045 comment_size = (uint16_t)strlen(file_info->comment);
1046 if (err == MZ_OK)
1047 err = mz_stream_write_uint16(stream, comment_size);
1048 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001049 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001050 if (err == MZ_OK)
1051 err = mz_stream_write_uint16(stream, file_info->internal_fa);
1052 if (err == MZ_OK)
1053 err = mz_stream_write_uint32(stream, file_info->external_fa);
1054 if (err == MZ_OK)
1055 {
1056 if (file_info->disk_offset >= UINT32_MAX)
1057 err = mz_stream_write_uint32(stream, UINT32_MAX);
1058 else
1059 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
1060 }
1061 }
1062
1063 if (err == MZ_OK)
1064 {
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001065 if (mz_stream_write(stream, file_info->filename, filename_length) != filename_length)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001066 err = MZ_STREAM_ERROR;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001067 if (err == MZ_OK)
1068 {
Nathan Moinvaziri240b3b62018-05-02 13:38:14 -07001069 // Ensure that directories have a slash appended to them for compatibility
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001070 if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK)
1071 err = mz_stream_write_uint8(stream, '/');
1072 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001073 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001074
1075 if (file_info->extrafield_size > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001076 {
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001077 err_mem = mz_stream_mem_seek(extrafield_ms, 0, SEEK_SET);
1078 while (err == MZ_OK && err_mem == MZ_OK)
1079 {
1080 err_mem = mz_stream_read_uint16(extrafield_ms, &field_type);
1081 if (err_mem == MZ_OK)
1082 err_mem = mz_stream_read_uint16(extrafield_ms, &field_length);
1083 if (err_mem != MZ_OK)
1084 break;
1085
1086 // Prefer our zip 64, ntfs extensions over incoming
1087 if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS)
1088 {
1089 err_mem = mz_stream_seek(extrafield_ms, field_length, SEEK_CUR);
1090 continue;
1091 }
1092
1093 err = mz_stream_write_uint16(stream, field_type);
1094 if (err == MZ_OK)
1095 err = mz_stream_write_uint16(stream, field_length);
1096 if (err == MZ_OK)
1097 err = mz_stream_copy(stream, extrafield_ms, field_length);
1098 }
1099
1100 mz_stream_mem_delete(&extrafield_ms);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001101 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001102
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001103 // Add ZIP64 extra info header to central directory
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001104 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001105 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001106 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_ZIP64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001107 if (err == MZ_OK)
1108 err = mz_stream_write_uint16(stream, extrafield_zip64_size);
1109 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
1110 err = mz_stream_write_uint64(stream, file_info->uncompressed_size);
1111 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
1112 err = mz_stream_write_uint64(stream, file_info->compressed_size);
1113 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
1114 err = mz_stream_write_uint64(stream, file_info->disk_offset);
1115 }
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001116 // Write NTFS timestamps
1117 if ((err == MZ_OK) && (extrafield_ntfs_size > 0))
1118 {
1119 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_NTFS);
1120 if (err == MZ_OK)
1121 err = mz_stream_write_uint16(stream, extrafield_ntfs_size);
1122 if (err == MZ_OK)
1123 err = mz_stream_write_uint32(stream, reserved);
1124 if (err == MZ_OK)
1125 err = mz_stream_write_uint16(stream, 0x01);
1126 if (err == MZ_OK)
1127 err = mz_stream_write_uint16(stream, extrafield_ntfs_size - 8);
juanii3679a3d2018-02-11 13:55:38 -03001128 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001129 {
1130 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
1131 err = mz_stream_write_uint64(stream, ntfs_time);
1132 }
juanii3679a3d2018-02-11 13:55:38 -03001133 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001134 {
1135 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
1136 err = mz_stream_write_uint64(stream, ntfs_time);
1137 }
juanii3679a3d2018-02-11 13:55:38 -03001138 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001139 {
1140 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
1141 err = mz_stream_write_uint64(stream, ntfs_time);
1142 }
1143 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001144#ifdef HAVE_AES
1145 // Write AES extra info header to central directory
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001146 if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001147 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001148 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001149 if (err == MZ_OK)
1150 err = mz_stream_write_uint16(stream, 7);
1151 if (err == MZ_OK)
1152 err = mz_stream_write_uint16(stream, file_info->aes_version);
1153 if (err == MZ_OK)
1154 err = mz_stream_write_uint8(stream, 'A');
1155 if (err == MZ_OK)
1156 err = mz_stream_write_uint8(stream, 'E');
1157 if (err == MZ_OK)
1158 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
1159 if (err == MZ_OK)
1160 err = mz_stream_write_uint16(stream, file_info->compression_method);
1161 }
1162#endif
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001163 if ((err == MZ_OK) && (!local) && (file_info->comment != NULL))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001164 {
1165 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != MZ_OK)
1166 err = MZ_STREAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001167 }
1168
1169 return err;
1170}
1171
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001172static int32_t mz_zip_entry_open_int(void *handle, int16_t raw, int16_t compress_level, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001173{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001174 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001175 int64_t max_total_in = 0;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001176 int64_t header_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001177 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001178 int32_t err = MZ_OK;
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001179 uint8_t use_crypt = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001180
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001181 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001182 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001183
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001184 switch (zip->file_info.compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001185 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001186 case MZ_COMPRESS_METHOD_STORE:
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001187 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001188#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001189 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001190#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001191#if HAVE_LZMA
1192 case MZ_COMPRESS_METHOD_LZMA:
1193#endif
1194 err = MZ_OK;
1195 break;
1196 default:
1197 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001198 }
1199
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001200 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password != NULL))
1201 {
1202 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
1203 {
1204 // Encrypt only when we are not trying to write raw and password is supplied.
1205 if (!raw)
1206 use_crypt = 1;
1207 }
1208 else if (zip->open_mode & MZ_OPEN_MODE_READ)
1209 {
1210 // Decrypt only when password is supplied. Don't error when password
1211 // is not supplied as we may want to read the raw encrypted data.
1212 use_crypt = 1;
1213 }
1214 }
1215
1216 if ((err == MZ_OK) && (use_crypt))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001217 {
1218#ifdef HAVE_AES
1219 if (zip->file_info.aes_version)
1220 {
1221 mz_stream_aes_create(&zip->crypt_stream);
1222 mz_stream_aes_set_password(zip->crypt_stream, password);
1223 mz_stream_aes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001224 }
1225 else
1226#endif
1227 {
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001228#ifdef HAVE_PKCRYPT
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001229 uint8_t verify1 = 0;
1230 uint8_t verify2 = 0;
1231
1232 // Info-ZIP modification to ZipCrypto format:
1233 // If bit 3 of the general purpose bit flag is set, it uses high byte of 16-bit File Time.
1234
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001235 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1236 {
1237 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001238
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001239 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1240
1241 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1242 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1243 }
1244 else
1245 {
1246 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1247 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1248 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001249
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001250 mz_stream_pkcrypt_create(&zip->crypt_stream);
1251 mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1252 mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001253#endif
1254 }
1255 }
1256
1257 if (err == MZ_OK)
1258 {
1259 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001260 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001261
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001262 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001263
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001264 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001265 }
1266
1267 if (err == MZ_OK)
1268 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001269 if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001270 mz_stream_raw_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001271#ifdef HAVE_ZLIB
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001272 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001273 mz_stream_zlib_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001274#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001275#ifdef HAVE_BZIP2
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001276 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001277 mz_stream_bzip_create(&zip->compress_stream);
1278#endif
1279#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001280 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001281 mz_stream_lzma_create(&zip->compress_stream);
1282#endif
1283 else
1284 err = MZ_PARAM_ERROR;
1285 }
1286
1287 if (err == MZ_OK)
1288 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001289 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001290 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001291 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001292 }
1293 else
1294 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001295 if (raw || zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001296 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001297 max_total_in = zip->file_info.compressed_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001298 mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1299
1300 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1301 max_total_in -= header_size;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001302 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1303 max_total_in -= footer_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001304
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001305 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001306 }
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001307 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 -03001308 {
1309 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1310 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1311 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001312 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001313
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001314 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1315
1316 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1317 }
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001318 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001319 {
1320 mz_stream_crc32_create(&zip->crc32_stream);
1321 mz_stream_set_base(zip->crc32_stream, zip->compress_stream);
1322
1323 err = mz_stream_open(zip->crc32_stream, NULL, zip->open_mode);
1324 }
1325
1326 if (err == MZ_OK)
1327 {
1328 zip->entry_opened = 1;
1329 }
1330
1331 return err;
1332}
1333
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001334extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001335{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001336 mz_zip *zip = (mz_zip *)handle;
1337 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001338
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001339#if defined(MZ_ZIP_NO_ENCRYPTION)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001340 if (password != NULL)
1341 return MZ_PARAM_ERROR;
1342#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001343 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001344 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001345 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001346 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001347 if (zip->entry_scanned == 0)
1348 return MZ_PARAM_ERROR;
Nathan Moinvaziri0f09a002018-04-23 18:48:30 -07001349 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) && (!raw))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001350 return MZ_PARAM_ERROR;
1351
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001352 if (zip->file_info.disk_number == zip->disk_number_with_cd)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001353 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1354 else
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001355 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001356
Nathan Moinvaziriee614ff2018-07-12 12:36:33 -07001357 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 -07001358 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001359 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 -07001360
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001361#ifdef MZ_ZIP_NO_DECOMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001362 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001363 err = MZ_SUPPORT_ERROR;
1364#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001365 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001366 err = mz_zip_entry_open_int(handle, raw, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001367
1368 return err;
1369}
1370
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001371extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int16_t raw, int16_t compress_level, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001372{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001373 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001374 int64_t disk_number = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001375 int32_t err = MZ_OK;
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001376
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001377
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001378#if defined(MZ_ZIP_NO_ENCRYPTION)
tz-lomb1b25802017-11-10 15:03:02 +03001379 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001380 return MZ_PARAM_ERROR;
1381#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001382 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001383 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001384
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001385 if (zip->entry_opened == 1)
1386 {
1387 err = mz_zip_entry_close(handle);
1388 if (err != MZ_OK)
1389 return err;
1390 }
1391
1392 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001393
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001394 if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001395 {
1396 if ((compress_level == 8) || (compress_level == 9))
1397 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
1398 if (compress_level == 2)
1399 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
1400 if (compress_level == 1)
1401 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1402 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001403#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001404 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001405 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001406#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001407
1408 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001409
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001410 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001411 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001412
juaniib8887e92018-02-14 00:51:05 -03001413 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1414 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001415
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001416 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001417 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001418 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001419
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001420#ifdef HAVE_AES
1421 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
1422 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
1423#endif
1424
Nathan Moinvaziri4827f712018-05-02 10:50:47 -07001425 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 -07001426 zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
Viktor Szakats2884e672018-04-30 08:12:13 +00001427
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001428#ifdef MZ_ZIP_NO_COMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001429 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001430 err = MZ_SUPPORT_ERROR;
1431#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001432 if (err == MZ_OK)
1433 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
1434 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001435 err = mz_zip_entry_open_int(handle, raw, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001436
1437 return err;
1438}
1439
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001440extern int32_t mz_zip_entry_read(void *handle, void *buf, uint32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001441{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001442 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001443 int32_t read = 0;
1444
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001445 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001446 return MZ_PARAM_ERROR;
Nathan Moinvaziri930f9182018-01-22 08:51:07 -08001447 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) // Zlib limitation
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001448 return MZ_PARAM_ERROR;
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07001449 if (len == 0)
1450 return MZ_PARAM_ERROR;
1451
Nathan Moinvazirieb80cd92018-07-11 16:45:09 -07001452 if (zip->file_info.compressed_size == 0)
1453 return 0;
1454
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07001455 // Read entire entry even if uncompressed_size = 0, otherwise
1456 // aes encryption validation will fail if compressed_size > 0
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001457 read = mz_stream_read(zip->crc32_stream, buf, len);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001458 return read;
1459}
1460
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001461extern int32_t mz_zip_entry_write(void *handle, const void *buf, uint32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001462{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001463 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001464 int32_t written = 0;
1465
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001466 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001467 return MZ_PARAM_ERROR;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001468 written = mz_stream_write(zip->crc32_stream, buf, len);
1469 return written;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001470}
1471
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001472extern int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001473{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001474 mz_zip *zip = (mz_zip *)handle;
1475 if (zip == NULL || zip->entry_scanned == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001476 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001477 *file_info = &zip->file_info;
1478 return MZ_OK;
1479}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001480
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001481extern int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001482{
1483 mz_zip *zip = (mz_zip *)handle;
1484 if (zip == NULL || zip->entry_scanned == 0 || zip->entry_opened == 0)
1485 return MZ_PARAM_ERROR;
1486 *local_file_info = &zip->local_file_info;
1487 return MZ_OK;
1488}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001489
Nathan Moinvaziri6ac2ff42018-07-21 14:29:29 -07001490static int32_t mz_zip_entry_close_int(void *handle, int16_t raw, uint64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001491{
1492 mz_zip *zip = (mz_zip *)handle;
1493 uint64_t compressed_size = 0;
Nathan Moinvaziri8871f6c2018-07-08 19:23:56 -07001494 int64_t total_in = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001495 int32_t err = MZ_OK;
1496
1497 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001498 return MZ_PARAM_ERROR;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001499
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001500 mz_stream_close(zip->compress_stream);
Nathan Moinvaziri6ac2ff42018-07-21 14:29:29 -07001501
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001502 if (!raw)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001503 crc32 = mz_stream_crc32_get_value(zip->crc32_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001504
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001505 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001506 {
1507#ifdef HAVE_AES
1508 // AES zip version AE-1 will expect a valid crc as well
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001509 if (zip->file_info.aes_version <= 0x0001)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001510#endif
1511 {
Nathan Moinvaziri6ac2ff42018-07-21 14:29:29 -07001512 mz_stream_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
Nathan Moinvaziri184f4cb2018-07-09 07:53:17 -07001513 // If entire entry was not read this will fail
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001514 if ((total_in > 0) && (!raw))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001515 {
1516 if (crc32 != zip->file_info.crc)
1517 err = MZ_CRC_ERROR;
1518 }
1519 }
1520 }
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001521
1522 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001523 if (!raw)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001524 mz_stream_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001525
1526 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
1527 {
1528 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001529 err = mz_stream_close(zip->crypt_stream);
1530
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001531 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001532 }
1533
1534 mz_stream_delete(&zip->crypt_stream);
1535
1536 mz_stream_delete(&zip->compress_stream);
1537 mz_stream_crc32_delete(&zip->crc32_stream);
1538
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001539 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001540 {
1541 if (err == MZ_OK)
1542 {
1543 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
1544 if (err == MZ_OK)
1545 err = mz_stream_write_uint32(zip->stream, crc32);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001546 // Store data descriptor as 8 bytes if zip 64 extension enabled
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001547 if (err == MZ_OK)
1548 {
Nathan Moinvaziri298126a2018-07-31 10:19:48 -07001549 // Zip 64 extension is enabled when uncompressed size is > UINT32_mAX
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001550 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001551 err = mz_stream_write_uint32(zip->stream, (uint32_t)compressed_size);
Andrew Gunnerson3c52d212018-01-05 22:28:12 -05001552 else
1553 err = mz_stream_write_uint64(zip->stream, compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001554 }
1555 if (err == MZ_OK)
1556 {
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001557 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001558 err = mz_stream_write_uint32(zip->stream, (uint32_t)uncompressed_size);
Andrew Gunnerson3c52d212018-01-05 22:28:12 -05001559 else
1560 err = mz_stream_write_uint64(zip->stream, uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001561 }
1562 }
1563
1564 zip->file_info.crc = crc32;
1565 zip->file_info.compressed_size = compressed_size;
1566 zip->file_info.uncompressed_size = uncompressed_size;
1567
1568 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001569 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001570
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001571 zip->number_entry += 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001572 }
1573
1574 zip->entry_opened = 0;
1575
1576 return err;
1577}
1578
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001579extern int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001580{
Nathan Moinvaziri6ac2ff42018-07-21 14:29:29 -07001581 return mz_zip_entry_close_int(handle, 0, 0, 0);
1582}
1583
1584extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32)
1585{
1586 return mz_zip_entry_close_int(handle, 1, uncompressed_size, crc32);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001587}
1588
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001589static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001590{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001591 mz_zip *zip = (mz_zip *)handle;
1592 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001593
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001594 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001595 return MZ_PARAM_ERROR;
1596
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001597 zip->entry_scanned = 0;
1598
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001599 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00001600
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001601 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001602 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001603 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001604 if (err == MZ_OK)
1605 zip->entry_scanned = 1;
1606 return err;
1607}
1608
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001609extern int32_t mz_zip_get_number_entry(void *handle, int64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001610{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001611 mz_zip *zip = (mz_zip *)handle;
1612 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001613 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001614 *number_entry = zip->number_entry;
1615 return MZ_OK;
1616}
1617
Viktor Szakats0f0535f2018-05-02 13:15:58 +00001618extern int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03001619{
1620 mz_zip *zip = (mz_zip *)handle;
1621 if (zip == NULL || disk_number_with_cd == NULL)
1622 return MZ_PARAM_ERROR;
1623 *disk_number_with_cd = zip->disk_number_with_cd;
1624 return MZ_OK;
1625}
1626
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001627extern int64_t mz_zip_get_entry(void *handle)
1628{
1629 mz_zip *zip = (mz_zip *)handle;
1630
1631 if (zip == NULL)
1632 return MZ_PARAM_ERROR;
1633
1634 return zip->cd_current_pos;
1635}
1636
1637extern int32_t mz_zip_goto_entry(void *handle, uint64_t cd_pos)
1638{
1639 mz_zip *zip = (mz_zip *)handle;
1640
1641 if (zip == NULL)
1642 return MZ_PARAM_ERROR;
1643
1644 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
1645 return MZ_PARAM_ERROR;
1646
1647 zip->cd_current_pos = cd_pos;
1648
1649 return mz_zip_goto_next_entry_int(handle);
1650}
1651
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001652extern int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001653{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001654 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001655
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001656 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001657 return MZ_PARAM_ERROR;
1658
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001659 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001660
1661 return mz_zip_goto_next_entry_int(handle);
1662}
1663
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001664extern int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001665{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001666 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001667
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001668 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001669 return MZ_PARAM_ERROR;
1670
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001671 zip->cd_current_pos += MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
1672 zip->file_info.extrafield_size + zip->file_info.comment_size;
1673
1674 return mz_zip_goto_next_entry_int(handle);
1675}
1676
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001677extern int32_t mz_zip_locate_entry(void *handle, const char *filename, mz_filename_compare_cb filename_compare_cb)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001678{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001679 mz_zip *zip = (mz_zip *)handle;
1680 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001681 int32_t result = 0;
1682
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001683 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001684 return MZ_PARAM_ERROR;
1685
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001686 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001687 while (err == MZ_OK)
1688 {
1689 if (filename_compare_cb != NULL)
1690 result = filename_compare_cb(handle, zip->file_info.filename, filename);
1691 else
1692 result = strcmp(zip->file_info.filename, filename);
1693
1694 if (result == 0)
1695 return MZ_OK;
1696
1697 err = mz_zip_goto_next_entry(handle);
1698 }
1699
1700 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001701}
1702
1703/***************************************************************************/
1704
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07001705int32_t mz_zip_attrib_is_dir(int32_t attributes, int32_t version_madeby)
1706{
1707 int32_t host_system = (uint8_t)(version_madeby >> 8);
1708
1709 if (host_system == MZ_HOST_SYSTEM_MSDOS || host_system == MZ_HOST_SYSTEM_WINDOWS_NTFS)
1710 {
1711 if ((attributes & 0x10) == 0x10) // FILE_ATTRIBUTE_DIRECTORY
1712 return MZ_OK;
1713 }
1714 else if (host_system == MZ_HOST_SYSTEM_UNIX || host_system == MZ_HOST_SYSTEM_OSX_DARWIN)
1715 {
1716 if ((attributes & 00170000) == 0040000) // S_ISDIR
1717 return MZ_OK;
1718 }
1719
1720 return MZ_EXIST_ERROR;
1721}
1722
1723/***************************************************************************/
1724
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001725static int32_t mz_zip_invalid_date(const struct tm *ptm)
1726{
1727#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001728 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || // 1980-based year, allow 80 extra
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001729 !datevalue_in_range(0, 11, ptm->tm_mon) ||
1730 !datevalue_in_range(1, 31, ptm->tm_mday) ||
1731 !datevalue_in_range(0, 23, ptm->tm_hour) ||
1732 !datevalue_in_range(0, 59, ptm->tm_min) ||
1733 !datevalue_in_range(0, 59, ptm->tm_sec));
1734#undef datevalue_in_range
1735}
1736
1737static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
1738{
1739 uint64_t date = (uint64_t)(dos_date >> 16);
1740
1741 ptm->tm_mday = (uint16_t)(date & 0x1f);
1742 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
1743 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
1744 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
1745 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
1746 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
1747 ptm->tm_isdst = -1;
1748}
1749
1750int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
1751{
1752 if (ptm == NULL)
1753 return MZ_PARAM_ERROR;
1754
1755 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
1756
1757 if (mz_zip_invalid_date(ptm))
1758 {
1759 // Invalid date stored, so don't return it
1760 memset(ptm, 0, sizeof(struct tm));
1761 return MZ_FORMAT_ERROR;
1762 }
1763 return MZ_OK;
1764}
1765
1766time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
1767{
1768 struct tm ptm;
1769 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
1770 return mktime(&ptm);
1771}
1772
1773int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
1774{
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001775 struct tm *ltm = NULL;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001776 if (ptm == NULL)
1777 return MZ_PARAM_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001778 ltm = localtime(&unix_time); // Returns a 1900-based year
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001779 if (ltm == NULL)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001780 {
1781 // Invalid date stored, so don't return it
1782 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001783 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001784 }
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001785 memcpy(ptm, ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001786 return MZ_OK;
1787}
1788
1789uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
1790{
1791 struct tm ptm;
1792 mz_zip_time_t_to_tm(unix_time, &ptm);
1793 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
1794}
1795
1796uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
1797{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07001798 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001799
1800 // Years supported:
1801
1802 // [00, 79] (assumed to be between 2000 and 2079)
1803 // [80, 207] (assumed to be between 1980 and 2107, typical output of old
1804 // software that does 'year-1900' to get a double digit year)
1805 // [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.)
1806
1807 memcpy(&fixed_tm, ptm, sizeof(struct tm));
1808 if (fixed_tm.tm_year >= 1980) // range [1980, 2107]
1809 fixed_tm.tm_year -= 1980;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001810 else if (fixed_tm.tm_year >= 80) // range [80, 207]
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001811 fixed_tm.tm_year -= 80;
1812 else // range [00, 79]
1813 fixed_tm.tm_year += 20;
1814
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001815 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001816 return 0;
1817
1818 return (uint32_t)(((fixed_tm.tm_mday) + (32 * (fixed_tm.tm_mon + 1)) + (512 * fixed_tm.tm_year)) << 16) |
1819 ((fixed_tm.tm_sec / 2) + (32 * fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
1820}
1821
1822int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
1823{
1824 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
1825 return MZ_OK;
1826}
1827
1828int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
1829{
1830 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
1831 return MZ_OK;
1832}