blob: c8a93cc9eec11d946bb73d0e84b895dadd70afd6 [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvaziri44d20792017-10-23 17:35:22 -07002 Version 2.2.1, October 23rd, 2017
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08003 part of the MiniZip project
4
5 Copyright (C) 2010-2017 Nathan Moinvaziri
6 Modifications for AES, PKWARE disk spanning
7 https://github.com/nmoinvaz/minizip
8 Copyright (C) 2009-2010 Mathias Svensson
9 Modifications for Zip64 support
10 http://result42.com
11 Copyright (C) 1998-2010 Gilles Vollant
12 http://www.winimage.com/zLibDll/minizip.html
13
14 This program is distributed under the terms of the same license as zlib.
15 See the accompanying LICENSE file for the full text of the license.
16*/
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <stdint.h>
21#include <string.h>
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -070022#include <time.h>
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080023#include <errno.h>
24
25#include "zlib.h"
26
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070027#include "mz.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080028#include "mz_strm.h"
29#ifdef HAVE_AES
30# include "mz_strm_aes.h"
31#endif
32#ifdef HAVE_BZIP2
33# include "mz_strm_bzip.h"
34#endif
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070035#ifdef HAVE_CRYPT
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080036# include "mz_strm_crypt.h"
37#endif
38#ifdef HAVE_LZMA
39# include "mz_strm_lzma.h"
40#endif
41#include "mz_strm_mem.h"
42#include "mz_strm_zlib.h"
43
44#include "mz_zip.h"
45
46/***************************************************************************/
47
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070048#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
49#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
50#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
51#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
52#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
53#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
54
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070055#define MZ_ZIP_SIZE_CD_ITEM (0x2e)
56#define MZ_ZIP_SIZE_CD_LOCATOR64 (0x14)
57#define MZ_ZIP_SIZE_LOCALHEADER (0x1e)
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 Moinvaziri8f67ff92017-10-17 23:22:29 -070082
Nathan Moinvaziricda36002017-10-21 09:37:18 -070083 uint64_t cd_start_pos; // pos of the first file in the central dir stream
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070084 uint64_t cd_current_pos; // pos of the current file in the central dir
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070085 uint64_t cd_offset; // offset of start of central directory
86 uint64_t cd_size; // size of the central directory
87
88 uint16_t entry_scanned;
89 uint16_t entry_opened; // 1 if a file in the zip is currently writ.
90 uint64_t entry_read;
91
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070092 int64_t number_entry;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070093
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -070094 int16_t compression_method;
95
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -070096 uint16_t version_madeby;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070097 char *comment;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080098} mz_zip;
99
100/***************************************************************************/
101
102// Locate the central directory of a zip file (at the end, just before the global comment)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700103static int32_t mz_zip_search_eocd(void *stream, uint64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800104{
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700105 uint8_t buf[1024 + 4];
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700106 int64_t file_size = 0;
107 int64_t back_read = 0;
108 int64_t max_back = UINT16_MAX; // maximum size of global comment
109 int32_t read_size = sizeof(buf);
110 int64_t read_pos = 0;
111 int32_t i = 0;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700112
Nathan Moinvazirif6e81cd2017-10-10 18:24:03 -0700113 *central_pos = 0;
114
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700115 if (mz_stream_seek(stream, 0, MZ_SEEK_END) != MZ_OK)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700116 return MZ_STREAM_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800117
118 file_size = mz_stream_tell(stream);
119
120 if (max_back > file_size)
121 max_back = file_size;
122
123 while (back_read < max_back)
124 {
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700125 back_read += (sizeof(buf) - 4);
126 if (back_read > max_back)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800127 back_read = max_back;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800128
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700129 read_pos = file_size - back_read;
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700130 if (read_size > (file_size - read_pos))
131 read_size = (uint32_t)(file_size - read_pos);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800132
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700133 if (mz_stream_seek(stream, read_pos, MZ_SEEK_SET) != MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800134 break;
135 if (mz_stream_read(stream, buf, read_size) != read_size)
136 break;
137
138 for (i = read_size - 3; (i--) > 0;)
139 {
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700140 if (((*(buf + i)) == (MZ_ZIP_MAGIC_ENDHEADER & 0xff)) &&
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700141 ((*(buf + i + 1)) == (MZ_ZIP_MAGIC_ENDHEADER >> 8 & 0xff)) &&
142 ((*(buf + i + 2)) == (MZ_ZIP_MAGIC_ENDHEADER >> 16 & 0xff)) &&
143 ((*(buf + i + 3)) == (MZ_ZIP_MAGIC_ENDHEADER >> 24 & 0xff)))
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800144 {
Nathan Moinvazirif6e81cd2017-10-10 18:24:03 -0700145 *central_pos = read_pos + i;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700146 return MZ_OK;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800147 }
148 }
149
Nathan Moinvazirif6e81cd2017-10-10 18:24:03 -0700150 if (*central_pos != 0)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800151 break;
152 }
153
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700154 return MZ_EXIST_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800155}
156
157// Locate the central directory 64 of a zip file (at the end, just before the global comment)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700158static int32_t mz_zip_search_zip64_eocd(void *stream, const uint64_t end_central_offset, uint64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800159{
160 uint64_t offset = 0;
161 uint32_t value32 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700162 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700163
164
165 *central_pos = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800166
167 // Zip64 end of central directory locator
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700168 err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800169 // Read locator signature
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700170 if (err == MZ_OK)
171 {
172 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700173 if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700174 err = MZ_FORMAT_ERROR;
175 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800176 // Number of the disk with the start of the zip64 end of central directory
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700177 if (err == MZ_OK)
178 err = mz_stream_read_uint32(stream, &value32);
179 // Relative offset of the zip64 end of central directory record8
180 if (err == MZ_OK)
181 err = mz_stream_read_uint64(stream, &offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800182 // Total number of disks
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700183 if (err == MZ_OK)
184 err = mz_stream_read_uint32(stream, &value32);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800185 // Goto end of central directory record
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700186 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700187 err = mz_stream_seek(stream, offset, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800188 // The signature
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700189 if (err == MZ_OK)
190 {
191 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700192 if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700193 err = MZ_FORMAT_ERROR;
194 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800195
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700196 if (err == MZ_OK)
197 *central_pos = offset;
198
199 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800200}
201
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700202static int32_t mz_zip_read_cd(void *handle)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800203{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700204 mz_zip *zip = (mz_zip *)handle;
205 int64_t number_entry_cd = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700206 uint64_t number_entry_cd64 = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800207 uint64_t number_entry = 0;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700208 uint64_t eocd_pos = 0;
209 uint64_t eocd_pos64 = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800210 uint16_t value16 = 0;
211 uint32_t value32 = 0;
212 uint64_t value64 = 0;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700213 uint16_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700214 int32_t err = MZ_OK;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800215
216
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800217 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700218 return MZ_PARAM_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800219
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700220 // Read and cache central directory records
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700221 if (mz_zip_search_eocd(zip->stream, &eocd_pos) == MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800222 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700223 // Read end of central directory info
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700224 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700225 // The signature, already checked
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800226 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700227 err = mz_stream_read_uint32(zip->stream, &value32);
228 // Number of this disk
229 if (err == MZ_OK)
230 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700231 // Number of the disk with the start of the central directory
232 if (err == MZ_OK)
233 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700234 zip->disk_number_with_cd = value16;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700235 // Total number of entries in the central dir on this disk
236 if (err == MZ_OK)
237 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700238 zip->number_entry = value16;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700239 // Total number of entries in the central dir
240 if (err == MZ_OK)
241 err = mz_stream_read_uint16(zip->stream, &value16);
242 number_entry_cd = value16;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700243 if (number_entry_cd != zip->number_entry)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700244 err = MZ_FORMAT_ERROR;
245 // Size of the central directory
246 if (err == MZ_OK)
247 err = mz_stream_read_uint32(zip->stream, &value32);
248 if (err == MZ_OK)
249 zip->cd_size = value32;
250 // Offset of start of central directory with respect to the starting disk number
251 if (err == MZ_OK)
252 err = mz_stream_read_uint32(zip->stream, &value32);
253 zip->cd_offset = value32;
254 // Zip file global comment length
255 if (err == MZ_OK)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700256 err = mz_stream_read_uint16(zip->stream, &comment_size);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800257
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700258 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800259 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700260 // Format should be Zip64, as the central directory or file size is too large
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700261 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800262 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700263 eocd_pos = eocd_pos64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700264
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700265 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700266 // The signature, already checked
267 if (err == MZ_OK)
268 err = mz_stream_read_uint32(zip->stream, &value32);
269 // Size of zip64 end of central directory record
270 if (err == MZ_OK)
271 err = mz_stream_read_uint64(zip->stream, &value64);
272 // Version made by
273 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700274 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700275 // Version needed to extract
276 if (err == MZ_OK)
277 err = mz_stream_read_uint16(zip->stream, &value16);
278 // Number of this disk
279 if (err == MZ_OK)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -0700280 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700281 // Number of the disk with the start of the central directory
282 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700283 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700284 // Total number of entries in the central directory on this disk
285 if (err == MZ_OK)
286 err = mz_stream_read_uint64(zip->stream, &number_entry);
287 // Total number of entries in the central directory
288 if (err == MZ_OK)
289 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
290 if (number_entry == UINT32_MAX)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700291 zip->number_entry = number_entry_cd64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700292 // Size of the central directory
293 if (err == MZ_OK)
294 err = mz_stream_read_uint64(zip->stream, &zip->cd_size);
295 // Offset of start of central directory with respect to the starting disk number
296 if (err == MZ_OK)
297 err = mz_stream_read_uint64(zip->stream, &zip->cd_offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800298 }
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700299 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700300 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700301 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700302 err = MZ_FORMAT_ERROR;
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700303 }
304 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800305 }
306
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700307 if (err == MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800308 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700309 if (eocd_pos < zip->cd_offset + zip->cd_size)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700310 err = MZ_FORMAT_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800311 }
312
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700313 if ((err == MZ_OK) && (comment_size > 0))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700314 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700315 zip->comment = (char *)malloc(comment_size + 1);
316 if (zip->comment)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700317 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700318 if (mz_stream_read(zip->stream, zip->comment, comment_size) != comment_size)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700319 err = MZ_STREAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700320 zip->comment[comment_size] = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700321 }
322 }
323
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800324 return err;
325}
326
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700327static int32_t mz_zip_write_cd(void *handle)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800328{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700329 mz_zip *zip = (mz_zip *)handle;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800330 uint16_t comment_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700331 uint64_t zip64_eocd_pos_inzip = 0;
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700332 int64_t disk_number = 0;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -0700333 int64_t disk_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700334 int32_t err = MZ_OK;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800335
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700336
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700337 if (zip == NULL)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800338 return MZ_PARAM_ERROR;
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700339
340 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700341 zip->disk_number_with_cd = (uint32_t)disk_number;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -0700342 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 -0700343 zip->disk_number_with_cd += 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700344 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800345
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700346 zip->cd_offset = mz_stream_tell(zip->stream);
347 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700348 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700349 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800350
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700351 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800352
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800353 // Write the ZIP64 central directory header
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700354 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT32_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800355 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700356 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800357
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700358 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800359
360 // Size of this 'zip64 end of central directory'
361 if (err == MZ_OK)
362 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
363 // Version made by
364 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700365 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800366 // Version needed
367 if (err == MZ_OK)
368 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
369 // Number of this disk
370 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700371 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800372 // Number of the disk with the start of the central directory
373 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700374 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800375 // Total number of entries in the central dir on this disk
376 if (err == MZ_OK)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700377 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800378 // Total number of entries in the central dir
379 if (err == MZ_OK)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700380 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800381 // Size of the central directory
382 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700383 err = mz_stream_write_uint64(zip->stream, (uint64_t)zip->cd_size);
384 // Offset of start of central directory with respect to the starting disk number
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800385 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700386 err = mz_stream_write_uint64(zip->stream, zip->cd_offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800387 if (err == MZ_OK)
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700388 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700389
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800390 // Number of the disk with the start of the central directory
391 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700392 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800393 // Relative offset to the end of zip64 central directory
394 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700395 err = mz_stream_write_uint64(zip->stream, zip64_eocd_pos_inzip);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800396 // Number of the disk with the start of the central directory
397 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700398 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800399 }
400
401 // Write the central directory header
402
403 // Signature
404 if (err == MZ_OK)
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700405 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800406 // Number of this disk
407 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700408 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800409 // Number of the disk with the start of the central directory
410 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700411 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800412 // Total number of entries in the central dir on this disk
413 if (err == MZ_OK)
414 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700415 if (zip->number_entry >= UINT16_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800416 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
417 else
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700418 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800419 }
420 // Total number of entries in the central dir
421 if (err == MZ_OK)
422 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700423 if (zip->number_entry >= UINT16_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800424 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
425 else
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700426 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800427 }
428 // Size of the central directory
429 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700430 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800431 // Offset of start of central directory with respect to the starting disk number
432 if (err == MZ_OK)
433 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700434 if (zip->cd_offset >= UINT32_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800435 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
436 else
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700437 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800438 }
439
440 // Write global comment
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700441 if (zip->comment != NULL)
442 comment_size = (uint16_t)strlen(zip->comment);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800443 if (err == MZ_OK)
444 err = mz_stream_write_uint16(zip->stream, comment_size);
445 if (err == MZ_OK)
446 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700447 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800448 err = MZ_STREAM_ERROR;
449 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700450 return err;
451}
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800452
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700453extern void* ZEXPORT mz_zip_open(void *stream, int32_t mode)
454{
455 mz_zip *zip = NULL;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700456 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700457
458
459 zip = (mz_zip *)malloc(sizeof(mz_zip));
460 if (zip == NULL)
461 return NULL;
462
463 memset(zip, 0, sizeof(mz_zip));
464
465 zip->stream = stream;
466 zip->open_mode = mode;
467
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700468 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700469 {
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700470 mz_stream_mem_create(&zip->cd_mem_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700471 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700472
473 zip->cd_stream = zip->cd_mem_stream;
474 }
475 else
476 {
477 zip->cd_stream = stream;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700478 }
479
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700480 if ((zip->open_mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700481 {
482 err = mz_zip_read_cd(zip);
483
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700484 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700485 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700486 if (zip->cd_size > 0)
487 {
488 // Store central directory in memory
489 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
490 if (err == MZ_OK)
491 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (uint32_t)zip->cd_size);
492 if (err == MZ_OK)
493 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
494 }
495 else
496 {
497 // If no central directory, append new zip to end of file
498 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
499 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700500 }
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700501 else
502 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700503 zip->cd_start_pos = zip->cd_offset;
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700504 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700505 }
506
507 if (err == MZ_OK)
508 {
509 mz_stream_mem_create(&zip->file_info_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700510 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700511 mz_stream_mem_create(&zip->local_file_info_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700512 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700513 }
514
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700515 if (err != MZ_OK)
516 {
517 if (zip->file_info_stream != NULL)
518 mz_stream_mem_delete(&zip->file_info_stream);
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700519 if (zip->local_file_info_stream != NULL)
520 mz_stream_mem_delete(&zip->local_file_info_stream);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700521 if (zip->cd_mem_stream != NULL)
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700522 {
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700523 mz_stream_close(zip->cd_mem_stream);
524 mz_stream_delete(&zip->cd_mem_stream);
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700525 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700526
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700527 if (zip->comment)
528 free(zip->comment);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700529
530 free(zip);
531 return NULL;
532 }
533
534 return zip;
535}
536
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700537extern int32_t ZEXPORT mz_zip_close(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700538{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700539 mz_zip *zip = (mz_zip *)handle;
540 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700541
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700542 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700543 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700544
545 if (zip->entry_opened == 1)
546 {
547 err = mz_zip_entry_close(handle);
548 if (err != MZ_OK)
549 return err;
550 }
551
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700552 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700553 err = mz_zip_write_cd(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700554
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700555 if (zip->cd_mem_stream != NULL)
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700556 {
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700557 mz_stream_close(zip->cd_mem_stream);
558 mz_stream_delete(&zip->cd_mem_stream);
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700559 }
560
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700561 mz_stream_mem_close(zip->file_info_stream);
562 mz_stream_mem_delete(&zip->file_info_stream);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700563 mz_stream_mem_close(zip->local_file_info_stream);
564 mz_stream_mem_delete(&zip->local_file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700565
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700566 if (zip->comment)
567 free(zip->comment);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700568
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800569 free(zip);
570
571 return err;
572}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700573
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700574extern int32_t ZEXPORT mz_zip_get_comment(void *handle, const char **comment)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700575{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700576 mz_zip *zip = (mz_zip *)handle;
577 if (zip == NULL || comment == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700578 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700579 if (zip->comment == NULL)
580 return MZ_EXIST_ERROR;
581 *comment = zip->comment;
582 return MZ_OK;
583}
584
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700585extern int32_t ZEXPORT mz_zip_set_comment(void *handle, const char *comment)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700586{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700587 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700588 uint16_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700589 if (zip == NULL || comment == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700590 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700591 if (zip->comment != NULL)
592 free(zip->comment);
593 comment_size = (uint16_t)(strlen(comment) + 1);
594 zip->comment = (char *)malloc(comment_size);
595 strncpy(zip->comment, comment, comment_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700596 return MZ_OK;
597}
598
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700599extern int32_t ZEXPORT mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700600{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700601 mz_zip *zip = (mz_zip *)handle;
602 if (zip == NULL || version_madeby == NULL)
603 return MZ_PARAM_ERROR;
604 *version_madeby = zip->version_madeby;
605 return MZ_OK;
606}
607
608extern int32_t ZEXPORT mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
609{
610 mz_zip *zip = (mz_zip *)handle;
611 if (zip == NULL)
612 return MZ_PARAM_ERROR;
613 zip->version_madeby = version_madeby;
614 return MZ_OK;
615}
616
617static int16_t mz_zip_entry_get_version_needed(int16_t zip64, mz_zip_file *file_info)
618{
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700619 int16_t version_needed = 20;
620
621 if (file_info == NULL)
622 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700623 if (file_info->version_needed > 0)
624 version_needed = file_info->version_needed;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700625 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700626 version_needed = 45;
627#ifdef HAVE_AES
628 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
629 version_needed = 51;
630#endif
631#ifdef HAVE_LZMA
632 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
633 version_needed = 63;
634#endif
635 return version_needed;
636}
637
638// Get info about the current file in the zip file
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700639static 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 -0700640{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700641 uint64_t ntfs_time = 0;
642 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700643 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700644 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700645 uint32_t extra_pos = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700646 uint32_t extra_data_size_read = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700647 uint16_t extra_header_id = 0;
648 uint16_t extra_data_size = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700649 uint16_t ntfs_attrib_id = 0;
650 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700651 uint16_t value16 = 0;
652 uint32_t value32 = 0;
653 uint64_t value64 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700654 int64_t max_seek = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700655 int64_t seek = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700656 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700657
658
659 memset(file_info, 0, sizeof(mz_zip_file));
660
661 // Check the magic
662 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700663 if (err == MZ_END_OF_STREAM)
664 err = MZ_END_OF_LIST;
665 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700666 err = MZ_END_OF_LIST;
667 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
668 err = MZ_FORMAT_ERROR;
669 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
670 err = MZ_FORMAT_ERROR;
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700671
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700672 // Read header fields
673 if (err == MZ_OK)
674 {
675 if (!local)
676 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
677 if (err == MZ_OK)
678 err = mz_stream_read_uint16(stream, &file_info->version_needed);
679 if (err == MZ_OK)
680 err = mz_stream_read_uint16(stream, &file_info->flag);
681 if (err == MZ_OK)
682 err = mz_stream_read_uint16(stream, &file_info->compression_method);
683 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700684 {
685 err = mz_stream_read_uint32(stream, &dos_date);
686 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
687 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700688 if (err == MZ_OK)
689 err = mz_stream_read_uint32(stream, &file_info->crc);
690 if (err == MZ_OK)
691 err = mz_stream_read_uint32(stream, &value32);
692 file_info->compressed_size = value32;
693 if (err == MZ_OK)
694 err = mz_stream_read_uint32(stream, &value32);
695 file_info->uncompressed_size = value32;
696 if (err == MZ_OK)
697 err = mz_stream_read_uint16(stream, &file_info->filename_size);
698 if (err == MZ_OK)
699 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
700 if (!local)
701 {
702 if (err == MZ_OK)
703 err = mz_stream_read_uint16(stream, &file_info->comment_size);
704 if (err == MZ_OK)
705 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700706 file_info->disk_number = value16;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700707 if (err == MZ_OK)
708 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
709 if (err == MZ_OK)
710 err = mz_stream_read_uint32(stream, &file_info->external_fa);
711 if (err == MZ_OK)
712 err = mz_stream_read_uint32(stream, &value32);
713 file_info->disk_offset = value32;
714 }
715 }
716
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700717 max_seek = file_info->filename_size + file_info->extrafield_size + file_info->comment_size + 3;
718 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700719 err = mz_stream_seek(file_info_stream, max_seek, MZ_SEEK_SET);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700720 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700721 err = mz_stream_seek(file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700722
723 if ((err == MZ_OK) && (file_info->filename_size > 0))
724 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700725 mz_stream_mem_get_buffer(file_info_stream, (void **)&file_info->filename);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700726
727 err = mz_stream_copy(file_info_stream, stream, file_info->filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700728 if (err == MZ_OK)
729 err = mz_stream_write_uint8(file_info_stream, 0);
730
731 seek += file_info->filename_size + 1;
732 }
733
734 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
735 {
736 mz_stream_mem_get_buffer_at(file_info_stream, seek, (void **)&file_info->extrafield);
737
738 err = mz_stream_copy(file_info_stream, stream, file_info->extrafield_size);
739 if (err == MZ_OK)
740 err = mz_stream_write_uint8(file_info_stream, 0);
741
742 // Seek back and parse the extra field
743 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700744 err = mz_stream_seek(file_info_stream, seek, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700745
746 seek += file_info->extrafield_size + 1;
747
748 while ((err == MZ_OK) && (extra_pos < file_info->extrafield_size))
749 {
750 err = mz_stream_read_uint16(file_info_stream, &extra_header_id);
751 if (err == MZ_OK)
752 err = mz_stream_read_uint16(file_info_stream, &extra_data_size);
753
754 // ZIP64 extra field
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700755 if (extra_header_id == MZ_ZIP_EXTENSION_ZIP64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700756 {
757 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
758 err = mz_stream_read_uint64(file_info_stream, &file_info->uncompressed_size);
759 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
760 err = mz_stream_read_uint64(file_info_stream, &file_info->compressed_size);
761 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
762 err = mz_stream_read_uint64(file_info_stream, &value64);
763 file_info->disk_offset = value64;
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700764 if ((err == MZ_OK) && (file_info->disk_number == UINT32_MAX))
765 err = mz_stream_read_uint32(file_info_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700766 }
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700767 // NTFS extra field
768 else if (extra_header_id == MZ_ZIP_EXTENSION_NTFS)
769 {
770 err = mz_stream_read_uint32(file_info_stream, &reserved);
771 extra_data_size_read = 4;
772
773 while ((err == MZ_OK) && (extra_data_size_read < extra_data_size))
774 {
775 err = mz_stream_read_uint16(file_info_stream, &ntfs_attrib_id);
776 if (err == MZ_OK)
777 err = mz_stream_read_uint16(file_info_stream, &ntfs_attrib_size);
778
779 if (ntfs_attrib_id == 0x01 && ntfs_attrib_size >= 8)
780 {
781 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
782 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
783
784 if ((err == MZ_OK) && (ntfs_attrib_size >= 16))
785 {
786 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
787 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
788 }
789 if ((err == MZ_OK) && (ntfs_attrib_size >= 24))
790 {
791 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
792 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
793 }
794 }
795 else
796 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700797 err = mz_stream_seek(file_info_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700798 }
799
800 extra_data_size_read += ntfs_attrib_size + 4;
801 }
802 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700803#ifdef HAVE_AES
804 // AES extra field
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700805 else if (extra_header_id == MZ_ZIP_EXTENSION_AES)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700806 {
807 uint8_t value8 = 0;
808 // Verify version info
809 err = mz_stream_read_uint16(file_info_stream, &value16);
810 // Support AE-1 and AE-2
811 if (value16 != 1 && value16 != 2)
812 err = MZ_FORMAT_ERROR;
813 file_info->aes_version = value16;
814 if (err == MZ_OK)
815 err = mz_stream_read_uint8(file_info_stream, &value8);
816 if ((char)value8 != 'A')
817 err = MZ_FORMAT_ERROR;
818 if (err == MZ_OK)
819 err = mz_stream_read_uint8(file_info_stream, &value8);
820 if ((char)value8 != 'E')
821 err = MZ_FORMAT_ERROR;
822 // Get AES encryption strength and actual compression method
823 if (err == MZ_OK)
824 err = mz_stream_read_uint8(file_info_stream, &value8);
825 file_info->aes_encryption_mode = value8;
826 if (err == MZ_OK)
827 err = mz_stream_read_uint16(file_info_stream, &value16);
828 file_info->compression_method = value16;
829 }
830#endif
831 else
832 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700833 err = mz_stream_seek(file_info_stream, extra_data_size, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700834 }
835
836 extra_pos += 4 + extra_data_size;
837 }
838 }
839
840 if ((err == MZ_OK) && (file_info->comment_size > 0))
841 {
842 mz_stream_mem_get_buffer_at(file_info_stream, seek, (void **)&file_info->comment);
843
844 err = mz_stream_copy(file_info_stream, stream, file_info->comment_size);
845 if (err == MZ_OK)
846 err = mz_stream_write_uint8(file_info_stream, 0);
847 }
848
849 return err;
850}
851
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700852static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700853{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700854 struct tm *local_time = NULL;
855 uint64_t ntfs_time = 0;
856 uint32_t reserved = 0;
857 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700858 uint16_t extrafield_size = 0;
859 uint16_t extrafield_zip64_size = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700860 uint16_t extrafield_ntfs_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700861 uint16_t filename_size = 0;
862 uint16_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700863 uint8_t zip64 = 0;
864 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700865
866 if (file_info == NULL)
867 return MZ_PARAM_ERROR;
868
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700869 extrafield_size = file_info->extrafield_size;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700870
871 if (file_info->uncompressed_size >= UINT32_MAX)
872 extrafield_zip64_size += 8;
873 if (file_info->compressed_size >= UINT32_MAX)
874 extrafield_zip64_size += 8;
875 if (file_info->disk_offset >= UINT32_MAX)
876 extrafield_zip64_size += 8;
877
878 // Use 64-bit data descriptor if we don't know uncompressed size ahead of time
879 zip64 = (local && file_info->uncompressed_size == 0) || (extrafield_zip64_size > 0);
880
881 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700882 {
883 extrafield_size += 4;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700884 extrafield_size += extrafield_zip64_size;
885 }
886#ifdef HAVE_AES
887 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
888 extrafield_size += 4 + 7;
889#endif
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700890 // NTFS timestamps
891 if (file_info->modified_date != 0)
892 extrafield_ntfs_size += 8;
893 if (file_info->accessed_date != 0)
894 extrafield_ntfs_size += 8;
895 if (file_info->creation_date != 0)
896 extrafield_ntfs_size += 8;
897
898 if (extrafield_ntfs_size > 4)
899 {
900 extrafield_ntfs_size += 4 + 4;
901
902 extrafield_size += 4;
903 extrafield_size += extrafield_ntfs_size;
904 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700905
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700906 file_info->version_needed = mz_zip_entry_get_version_needed(zip64, file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700907
908 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700909 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700910 else
911 {
912 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
913 if (err == MZ_OK)
914 err = mz_stream_write_uint16(stream, file_info->version_madeby);
915 }
916
917 if (err == MZ_OK)
918 err = mz_stream_write_uint16(stream, file_info->version_needed);
919 if (err == MZ_OK)
920 err = mz_stream_write_uint16(stream, file_info->flag);
921 if (err == MZ_OK)
922 {
923#ifdef HAVE_AES
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700924 if (file_info->aes_version)
925 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700926 else
927#endif
928 err = mz_stream_write_uint16(stream, file_info->compression_method);
929 }
930 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700931 {
932 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
933 err = mz_stream_write_uint32(stream, dos_date);
934 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700935
936 if (err == MZ_OK)
937 err = mz_stream_write_uint32(stream, file_info->crc); // crc
938 if (err == MZ_OK)
939 {
940 if (file_info->compressed_size >= UINT32_MAX) // compr size
941 err = mz_stream_write_uint32(stream, UINT32_MAX);
942 else
943 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
944 }
945 if (err == MZ_OK)
946 {
947 if (file_info->uncompressed_size >= UINT32_MAX) // uncompr size
948 err = mz_stream_write_uint32(stream, UINT32_MAX);
949 else
950 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
951 }
952
953 filename_size = (uint16_t)strlen(file_info->filename);
954 if (err == MZ_OK)
955 err = mz_stream_write_uint16(stream, filename_size);
956 if (err == MZ_OK)
957 err = mz_stream_write_uint16(stream, extrafield_size);
958
959 if (!local)
960 {
961 if (file_info->comment != NULL)
962 comment_size = (uint16_t)strlen(file_info->comment);
963 if (err == MZ_OK)
964 err = mz_stream_write_uint16(stream, comment_size);
965 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700966 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700967 if (err == MZ_OK)
968 err = mz_stream_write_uint16(stream, file_info->internal_fa);
969 if (err == MZ_OK)
970 err = mz_stream_write_uint32(stream, file_info->external_fa);
971 if (err == MZ_OK)
972 {
973 if (file_info->disk_offset >= UINT32_MAX)
974 err = mz_stream_write_uint32(stream, UINT32_MAX);
975 else
976 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
977 }
978 }
979
980 if (err == MZ_OK)
981 {
982 if (mz_stream_write(stream, file_info->filename, filename_size) != filename_size)
983 err = MZ_STREAM_ERROR;
984 }
985 if (err == MZ_OK)
986 {
987 if (mz_stream_write(stream, file_info->extrafield, file_info->extrafield_size) != file_info->extrafield_size)
988 err = MZ_STREAM_ERROR;
989 }
990 // Add ZIP64 extra info header to central directory
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700991 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700992 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700993 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_ZIP64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700994 if (err == MZ_OK)
995 err = mz_stream_write_uint16(stream, extrafield_zip64_size);
996 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
997 err = mz_stream_write_uint64(stream, file_info->uncompressed_size);
998 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
999 err = mz_stream_write_uint64(stream, file_info->compressed_size);
1000 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
1001 err = mz_stream_write_uint64(stream, file_info->disk_offset);
1002 }
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001003 // Write NTFS timestamps
1004 if ((err == MZ_OK) && (extrafield_ntfs_size > 0))
1005 {
1006 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_NTFS);
1007 if (err == MZ_OK)
1008 err = mz_stream_write_uint16(stream, extrafield_ntfs_size);
1009 if (err == MZ_OK)
1010 err = mz_stream_write_uint32(stream, reserved);
1011 if (err == MZ_OK)
1012 err = mz_stream_write_uint16(stream, 0x01);
1013 if (err == MZ_OK)
1014 err = mz_stream_write_uint16(stream, extrafield_ntfs_size - 8);
1015 if ((err == MZ_OK) && (file_info->modified_date != 0))
1016 {
1017 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
1018 err = mz_stream_write_uint64(stream, ntfs_time);
1019 }
1020 if ((err == MZ_OK) && (file_info->accessed_date != 0))
1021 {
1022 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
1023 err = mz_stream_write_uint64(stream, ntfs_time);
1024 }
1025 if ((err == MZ_OK) && (file_info->creation_date != 0))
1026 {
1027 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
1028 err = mz_stream_write_uint64(stream, ntfs_time);
1029 }
1030 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001031#ifdef HAVE_AES
1032 // Write AES extra info header to central directory
1033 if ((err == MZ_OK) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
1034 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001035 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001036 if (err == MZ_OK)
1037 err = mz_stream_write_uint16(stream, 7);
1038 if (err == MZ_OK)
1039 err = mz_stream_write_uint16(stream, file_info->aes_version);
1040 if (err == MZ_OK)
1041 err = mz_stream_write_uint8(stream, 'A');
1042 if (err == MZ_OK)
1043 err = mz_stream_write_uint8(stream, 'E');
1044 if (err == MZ_OK)
1045 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
1046 if (err == MZ_OK)
1047 err = mz_stream_write_uint16(stream, file_info->compression_method);
1048 }
1049#endif
1050 if ((err == MZ_OK) && (file_info->comment != NULL))
1051 {
1052 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != MZ_OK)
1053 err = MZ_STREAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001054 }
1055
1056 return err;
1057}
1058
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001059static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, int16_t compress_level, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001060{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001061 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001062 int64_t max_total_in = 0;
1063 int64_t total_in = 0;
1064 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001065 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001066
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001067 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001068 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001069
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001070 zip->compression_method = compression_method;
1071
1072 switch (zip->compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001073 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001074 case MZ_COMPRESS_METHOD_RAW:
1075 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001076#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001077 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001078#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001079#if HAVE_LZMA
1080 case MZ_COMPRESS_METHOD_LZMA:
1081#endif
1082 err = MZ_OK;
1083 break;
1084 default:
1085 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001086 }
1087
1088 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL))
1089 return MZ_PARAM_ERROR;
1090
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001091 if ((err == Z_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED))
1092 {
1093#ifdef HAVE_AES
1094 if (zip->file_info.aes_version)
1095 {
1096 mz_stream_aes_create(&zip->crypt_stream);
1097 mz_stream_aes_set_password(zip->crypt_stream, password);
1098 mz_stream_aes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001099 }
1100 else
1101#endif
1102 {
1103#ifdef HAVE_CRYPT
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001104 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001105 uint8_t verify1 = 0;
1106 uint8_t verify2 = 0;
1107
1108 // Info-ZIP modification to ZipCrypto format:
1109 // If bit 3 of the general purpose bit flag is set, it uses high byte of 16-bit File Time.
1110
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001111 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1112
1113 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1114 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001115
1116 mz_stream_crypt_create(&zip->crypt_stream);
1117 mz_stream_crypt_set_password(zip->crypt_stream, password);
1118 mz_stream_crypt_set_verify(zip->crypt_stream, verify1, verify2);
1119#endif
1120 }
1121 }
1122
1123 if (err == MZ_OK)
1124 {
1125 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001126 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001127
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001128 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001129
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001130 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001131 }
1132
1133 if (err == MZ_OK)
1134 {
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001135 if (zip->compression_method == MZ_COMPRESS_METHOD_RAW)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001136 mz_stream_raw_create(&zip->compress_stream);
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001137 else if (zip->compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001138 mz_stream_zlib_create(&zip->compress_stream);
1139#ifdef HAVE_BZIP2
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001140 else if (zip->compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001141 mz_stream_bzip_create(&zip->compress_stream);
1142#endif
1143#ifdef HAVE_LZMA
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001144 else if (zip->compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001145 mz_stream_lzma_create(&zip->compress_stream);
1146#endif
1147 else
1148 err = MZ_PARAM_ERROR;
1149 }
1150
1151 if (err == MZ_OK)
1152 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001153 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001154 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001155 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001156 }
1157 else
1158 {
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001159 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED || zip->compression_method == MZ_COMPRESS_METHOD_RAW)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001160 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001161 max_total_in = zip->file_info.compressed_size;
1162 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1163 max_total_in -= footer_size;
1164 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in) == MZ_OK)
1165 max_total_in -= total_in;
1166 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001167 }
1168 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001169
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001170 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1171
1172 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1173 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001174 if (err == Z_OK)
1175 {
1176 mz_stream_crc32_create(&zip->crc32_stream);
1177 mz_stream_set_base(zip->crc32_stream, zip->compress_stream);
1178
1179 err = mz_stream_open(zip->crc32_stream, NULL, zip->open_mode);
1180 }
1181
1182 if (err == MZ_OK)
1183 {
1184 zip->entry_opened = 1;
1185 }
1186
1187 return err;
1188}
1189
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001190extern int32_t ZEXPORT mz_zip_entry_read_open(void *handle, int16_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001191{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001192 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001193 int16_t compression_method = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001194 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001195
1196#if !defined(HAVE_CRYPT) && !defined(HAVE_AES)
1197 if (password != NULL)
1198 return MZ_PARAM_ERROR;
1199#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001200 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001201 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001202 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001203 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001204 if (zip->entry_scanned == 0)
1205 return MZ_PARAM_ERROR;
1206 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL))
1207 return MZ_PARAM_ERROR;
1208
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001209 if (zip->file_info.disk_number == zip->disk_number_with_cd)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001210 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1211 else
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001212 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001213
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001214 err = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001215 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001216 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 -07001217
1218 compression_method = zip->compression_method;
1219 if (raw)
1220 compression_method = MZ_COMPRESS_METHOD_RAW;
1221
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001222 if (err == MZ_OK)
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001223 err = mz_zip_entry_open_int(handle, compression_method, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001224
1225 return err;
1226}
1227
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001228extern int32_t ZEXPORT mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info,
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001229 int16_t compress_level, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001230{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001231 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001232 int64_t disk_number = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001233 int32_t err = MZ_OK;
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001234 int16_t compression_method = 0;
1235
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001236
1237#if !defined(HAVE_CRYPT) && !defined(HAVE_AES)
1238 if (crypt_info != NULL)
1239 return MZ_PARAM_ERROR;
1240#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001241 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001242 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001243
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001244 if (zip->entry_opened == 1)
1245 {
1246 err = mz_zip_entry_close(handle);
1247 if (err != MZ_OK)
1248 return err;
1249 }
1250
1251 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001252
1253 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
1254#ifdef HAVE_LZMA
1255 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
1256#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001257 if ((compress_level == 8) || (compress_level == 9))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001258 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001259 if (compress_level == 2)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001260 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001261 if (compress_level == 1)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001262 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1263
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001264 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001265 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
1266 else
1267 zip->file_info.flag &= ~MZ_ZIP_FLAG_ENCRYPTED;
1268
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001269 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
1270 zip->file_info.disk_number = (uint32_t)disk_number;
1271
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001272 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001273 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001274 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001275
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001276#ifdef HAVE_AES
1277 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
1278 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
1279#endif
1280
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001281 compression_method = zip->file_info.compression_method;
1282 if (compress_level == 0)
1283 compression_method = MZ_COMPRESS_METHOD_RAW;
1284
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001285 if (err == MZ_OK)
1286 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
1287 if (err == MZ_OK)
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001288 err = mz_zip_entry_open_int(handle, compression_method, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001289
1290 return err;
1291}
1292
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001293extern int32_t ZEXPORT mz_zip_entry_read(void *handle, void *buf, uint32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001294{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001295 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001296 int32_t read = 0;
1297
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001298 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001299 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001300 if (len > UINT16_MAX) // Zlib limitation
1301 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001302 if (len == 0)
1303 return 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001304 read = mz_stream_read(zip->crc32_stream, buf, len);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001305 if (read > 0)
1306 zip->entry_read += read;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001307 return read;
1308}
1309
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001310extern int32_t ZEXPORT mz_zip_entry_write(void *handle, const void *buf, uint32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001311{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001312 mz_zip *zip = (mz_zip *)handle;
1313 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001314 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001315 return mz_stream_write(zip->crc32_stream, buf, len);
1316}
1317
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001318extern int32_t ZEXPORT mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001319{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001320 mz_zip *zip = (mz_zip *)handle;
1321 if (zip == NULL || zip->entry_scanned == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001322 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001323 *file_info = &zip->file_info;
1324 return MZ_OK;
1325}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001326
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001327extern int32_t ZEXPORT mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
1328{
1329 mz_zip *zip = (mz_zip *)handle;
1330 if (zip == NULL || zip->entry_scanned == 0 || zip->entry_opened == 0)
1331 return MZ_PARAM_ERROR;
1332 *local_file_info = &zip->local_file_info;
1333 return MZ_OK;
1334}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001335
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001336extern int32_t ZEXPORT mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32)
1337{
1338 mz_zip *zip = (mz_zip *)handle;
1339 uint64_t compressed_size = 0;
1340 int32_t err = MZ_OK;
1341
1342 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001343 return MZ_PARAM_ERROR;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001344
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001345 mz_stream_close(zip->compress_stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001346 if (crc32 == 0)
1347 crc32 = mz_stream_crc32_get_value(zip->crc32_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001348
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001349 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001350 {
1351#ifdef HAVE_AES
1352 // AES zip version AE-1 will expect a valid crc as well
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001353 if (zip->file_info.aes_version <= 0x0001)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001354#endif
1355 {
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001356 if ((zip->entry_read > 0) && (zip->compression_method != MZ_COMPRESS_METHOD_RAW))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001357 {
1358 if (crc32 != zip->file_info.crc)
1359 err = MZ_CRC_ERROR;
1360 }
1361 }
1362 }
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001363
1364 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001365 if ((zip->compression_method != MZ_COMPRESS_METHOD_RAW) || (uncompressed_size == 0))
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001366 mz_stream_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001367
1368 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
1369 {
1370 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001371 err = mz_stream_close(zip->crypt_stream);
1372
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001373 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001374 }
1375
1376 mz_stream_delete(&zip->crypt_stream);
1377
1378 mz_stream_delete(&zip->compress_stream);
1379 mz_stream_crc32_delete(&zip->crc32_stream);
1380
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001381 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001382 {
1383 if (err == MZ_OK)
1384 {
1385 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
1386 if (err == MZ_OK)
1387 err = mz_stream_write_uint32(zip->stream, crc32);
1388 if (err == MZ_OK)
1389 {
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001390 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001391 err = mz_stream_write_uint64(zip->stream, compressed_size);
1392 else
1393 err = mz_stream_write_uint32(zip->stream, (uint32_t)compressed_size);
1394 }
1395 if (err == MZ_OK)
1396 {
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001397 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001398 err = mz_stream_write_uint64(zip->stream, uncompressed_size);
1399 else
1400 err = mz_stream_write_uint32(zip->stream, (uint32_t)uncompressed_size);
1401 }
1402 }
1403
1404 zip->file_info.crc = crc32;
1405 zip->file_info.compressed_size = compressed_size;
1406 zip->file_info.uncompressed_size = uncompressed_size;
1407
1408 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001409 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001410
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001411 zip->number_entry += 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001412 }
1413
1414 zip->entry_opened = 0;
1415
1416 return err;
1417}
1418
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001419extern int32_t ZEXPORT mz_zip_entry_close(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001420{
1421 return mz_zip_entry_close_raw(handle, 0, 0);
1422}
1423
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001424static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001425{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001426 mz_zip *zip = (mz_zip *)handle;
1427 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001428
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001429 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001430 return MZ_PARAM_ERROR;
1431
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001432 zip->entry_scanned = 0;
1433
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001434 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001435
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001436 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001437 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001438 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001439 if (err == MZ_OK)
1440 zip->entry_scanned = 1;
1441 return err;
1442}
1443
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001444extern int32_t ZEXPORT mz_zip_get_number_entry(void *handle, int64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001445{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001446 mz_zip *zip = (mz_zip *)handle;
1447 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001448 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001449 *number_entry = zip->number_entry;
1450 return MZ_OK;
1451}
1452
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001453extern int32_t ZEXPORT mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001454{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001455 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001456
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001457 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001458 return MZ_PARAM_ERROR;
1459
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001460 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001461
1462 return mz_zip_goto_next_entry_int(handle);
1463}
1464
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001465extern int32_t ZEXPORT mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001466{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001467 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001468
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001469 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001470 return MZ_PARAM_ERROR;
1471
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001472 zip->cd_current_pos += MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
1473 zip->file_info.extrafield_size + zip->file_info.comment_size;
1474
1475 return mz_zip_goto_next_entry_int(handle);
1476}
1477
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001478extern int32_t ZEXPORT mz_zip_locate_entry(void *handle, const char *filename, mz_filename_compare_cb filename_compare_cb)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001479{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001480 mz_zip *zip = (mz_zip *)handle;
1481 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001482 int32_t result = 0;
1483
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001484 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001485 return MZ_PARAM_ERROR;
1486
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001487 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001488 while (err == MZ_OK)
1489 {
1490 if (filename_compare_cb != NULL)
1491 result = filename_compare_cb(handle, zip->file_info.filename, filename);
1492 else
1493 result = strcmp(zip->file_info.filename, filename);
1494
1495 if (result == 0)
1496 return MZ_OK;
1497
1498 err = mz_zip_goto_next_entry(handle);
1499 }
1500
1501 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001502}
1503
1504/***************************************************************************/
1505
1506static int32_t mz_zip_invalid_date(const struct tm *ptm)
1507{
1508#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
1509 return (!datevalue_in_range(0, 207, ptm->tm_year) ||
1510 !datevalue_in_range(0, 11, ptm->tm_mon) ||
1511 !datevalue_in_range(1, 31, ptm->tm_mday) ||
1512 !datevalue_in_range(0, 23, ptm->tm_hour) ||
1513 !datevalue_in_range(0, 59, ptm->tm_min) ||
1514 !datevalue_in_range(0, 59, ptm->tm_sec));
1515#undef datevalue_in_range
1516}
1517
1518static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
1519{
1520 uint64_t date = (uint64_t)(dos_date >> 16);
1521
1522 ptm->tm_mday = (uint16_t)(date & 0x1f);
1523 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
1524 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
1525 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
1526 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
1527 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
1528 ptm->tm_isdst = -1;
1529}
1530
1531int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
1532{
1533 if (ptm == NULL)
1534 return MZ_PARAM_ERROR;
1535
1536 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
1537
1538 if (mz_zip_invalid_date(ptm))
1539 {
1540 // Invalid date stored, so don't return it
1541 memset(ptm, 0, sizeof(struct tm));
1542 return MZ_FORMAT_ERROR;
1543 }
1544 return MZ_OK;
1545}
1546
1547time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
1548{
1549 struct tm ptm;
1550 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
1551 return mktime(&ptm);
1552}
1553
1554int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
1555{
1556 if (ptm == NULL)
1557 return MZ_PARAM_ERROR;
1558
1559 memcpy(ptm, localtime(&unix_time), sizeof(struct tm));
1560 return MZ_OK;
1561}
1562
1563uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
1564{
1565 struct tm ptm;
1566 mz_zip_time_t_to_tm(unix_time, &ptm);
1567 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
1568}
1569
1570uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
1571{
1572 struct tm fixed_tm = { 0 };
1573 uint32_t dos_date = 0;
1574
1575 // Years supported:
1576
1577 // [00, 79] (assumed to be between 2000 and 2079)
1578 // [80, 207] (assumed to be between 1980 and 2107, typical output of old
1579 // software that does 'year-1900' to get a double digit year)
1580 // [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.)
1581
1582 memcpy(&fixed_tm, ptm, sizeof(struct tm));
1583 if (fixed_tm.tm_year >= 1980) // range [1980, 2107]
1584 fixed_tm.tm_year -= 1980;
1585 else if (fixed_tm.tm_year >= 80) // range [80, 99]
1586 fixed_tm.tm_year -= 80;
1587 else // range [00, 79]
1588 fixed_tm.tm_year += 20;
1589
1590 if (mz_zip_invalid_date(ptm))
1591 return 0;
1592
1593 return (uint32_t)(((fixed_tm.tm_mday) + (32 * (fixed_tm.tm_mon + 1)) + (512 * fixed_tm.tm_year)) << 16) |
1594 ((fixed_tm.tm_sec / 2) + (32 * fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
1595}
1596
1597int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
1598{
1599 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
1600 return MZ_OK;
1601}
1602
1603int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
1604{
1605 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
1606 return MZ_OK;
1607}