blob: a8aba71b4d122d9e9b14c7dea296184821b8a9df [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvaziri5c3dd5c2018-01-30 12:27:22 -08002 Version 2.2.7, January 30th, 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
7 Copyright (C) 2009-2010 Mathias Svensson
8 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 Moinvaziri00cca9f2017-10-16 07:37:11 -070033#ifdef HAVE_CRYPT
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080034# include "mz_strm_crypt.h"
35#endif
36#ifdef HAVE_LZMA
37# include "mz_strm_lzma.h"
38#endif
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -080039#ifdef HAVE_ZLIB
40# include "mz_strm_zlib.h"
41#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080042#include "mz_strm_mem.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080043
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
juanii3f59ffc2018-02-08 12:44:17 -0300354 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_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)
juaniic65486d2018-02-08 17:07:07 -0300398 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
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 Moinvaziri91a76f62017-11-10 07:39:06 -0800453extern void* mz_zip_open(void *stream, int32_t mode)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700454{
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;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700466
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800467 if (mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700468 {
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700469 mz_stream_mem_create(&zip->cd_mem_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700470 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700471
472 zip->cd_stream = zip->cd_mem_stream;
473 }
474 else
475 {
476 zip->cd_stream = stream;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700477 }
478
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800479 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700480 {
481 err = mz_zip_read_cd(zip);
482
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700483 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700484 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700485 if (zip->cd_size > 0)
486 {
487 // Store central directory in memory
488 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
489 if (err == MZ_OK)
490 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (uint32_t)zip->cd_size);
491 if (err == MZ_OK)
492 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
493 }
494 else
495 {
496 // If no central directory, append new zip to end of file
497 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
498 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700499 }
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700500 else
501 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700502 zip->cd_start_pos = zip->cd_offset;
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700503 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700504 }
505
506 if (err == MZ_OK)
507 {
508 mz_stream_mem_create(&zip->file_info_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700509 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700510 mz_stream_mem_create(&zip->local_file_info_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700511 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700512 }
513
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700514 if (err != MZ_OK)
515 {
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800516 mz_zip_close(zip);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700517 return NULL;
518 }
519
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800520 zip->open_mode = mode;
521
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700522 return zip;
523}
524
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800525extern int32_t mz_zip_close(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700526{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700527 mz_zip *zip = (mz_zip *)handle;
528 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700529
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700530 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700531 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700532
533 if (zip->entry_opened == 1)
534 {
535 err = mz_zip_entry_close(handle);
536 if (err != MZ_OK)
537 return err;
538 }
539
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700540 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700541 err = mz_zip_write_cd(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700542
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700543 if (zip->cd_mem_stream != NULL)
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700544 {
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700545 mz_stream_close(zip->cd_mem_stream);
546 mz_stream_delete(&zip->cd_mem_stream);
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700547 }
548
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800549 if (zip->file_info_stream != NULL)
550 {
551 mz_stream_mem_close(zip->file_info_stream);
552 mz_stream_mem_delete(&zip->file_info_stream);
553 }
554 if (zip->local_file_info_stream != NULL)
555 {
556 mz_stream_mem_close(zip->local_file_info_stream);
557 mz_stream_mem_delete(&zip->local_file_info_stream);
558 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700559
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700560 if (zip->comment)
561 free(zip->comment);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700562
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800563 free(zip);
564
565 return err;
566}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700567
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800568extern int32_t mz_zip_get_comment(void *handle, const char **comment)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700569{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700570 mz_zip *zip = (mz_zip *)handle;
571 if (zip == NULL || comment == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700572 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700573 if (zip->comment == NULL)
574 return MZ_EXIST_ERROR;
575 *comment = zip->comment;
576 return MZ_OK;
577}
578
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800579extern int32_t mz_zip_set_comment(void *handle, const char *comment)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700580{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700581 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700582 uint16_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700583 if (zip == NULL || comment == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700584 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700585 if (zip->comment != NULL)
586 free(zip->comment);
587 comment_size = (uint16_t)(strlen(comment) + 1);
588 zip->comment = (char *)malloc(comment_size);
589 strncpy(zip->comment, comment, comment_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700590 return MZ_OK;
591}
592
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800593extern int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
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 || version_madeby == NULL)
597 return MZ_PARAM_ERROR;
598 *version_madeby = zip->version_madeby;
599 return MZ_OK;
600}
601
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800602extern int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700603{
604 mz_zip *zip = (mz_zip *)handle;
605 if (zip == NULL)
606 return MZ_PARAM_ERROR;
607 zip->version_madeby = version_madeby;
608 return MZ_OK;
609}
610
611static int16_t mz_zip_entry_get_version_needed(int16_t zip64, mz_zip_file *file_info)
612{
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700613 int16_t version_needed = 20;
614
615 if (file_info == NULL)
616 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700617 if (file_info->version_needed > 0)
618 version_needed = file_info->version_needed;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700619 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700620 version_needed = 45;
621#ifdef HAVE_AES
622 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
623 version_needed = 51;
624#endif
625#ifdef HAVE_LZMA
626 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
627 version_needed = 63;
628#endif
629 return version_needed;
630}
631
632// Get info about the current file in the zip file
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700633static 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 -0700634{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700635 uint64_t ntfs_time = 0;
636 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700637 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700638 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700639 uint32_t extra_pos = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700640 uint32_t extra_data_size_read = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700641 uint16_t extra_header_id = 0;
642 uint16_t extra_data_size = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700643 uint16_t ntfs_attrib_id = 0;
644 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700645 uint16_t value16 = 0;
646 uint32_t value32 = 0;
647 uint64_t value64 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700648 int64_t max_seek = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700649 int64_t seek = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700650 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700651
652
653 memset(file_info, 0, sizeof(mz_zip_file));
654
655 // Check the magic
656 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700657 if (err == MZ_END_OF_STREAM)
658 err = MZ_END_OF_LIST;
659 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700660 err = MZ_END_OF_LIST;
661 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
662 err = MZ_FORMAT_ERROR;
663 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
664 err = MZ_FORMAT_ERROR;
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700665
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700666 // Read header fields
667 if (err == MZ_OK)
668 {
669 if (!local)
670 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
671 if (err == MZ_OK)
672 err = mz_stream_read_uint16(stream, &file_info->version_needed);
673 if (err == MZ_OK)
674 err = mz_stream_read_uint16(stream, &file_info->flag);
675 if (err == MZ_OK)
676 err = mz_stream_read_uint16(stream, &file_info->compression_method);
677 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700678 {
679 err = mz_stream_read_uint32(stream, &dos_date);
680 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
681 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700682 if (err == MZ_OK)
683 err = mz_stream_read_uint32(stream, &file_info->crc);
684 if (err == MZ_OK)
685 err = mz_stream_read_uint32(stream, &value32);
686 file_info->compressed_size = value32;
687 if (err == MZ_OK)
688 err = mz_stream_read_uint32(stream, &value32);
689 file_info->uncompressed_size = value32;
690 if (err == MZ_OK)
691 err = mz_stream_read_uint16(stream, &file_info->filename_size);
692 if (err == MZ_OK)
693 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
694 if (!local)
695 {
696 if (err == MZ_OK)
697 err = mz_stream_read_uint16(stream, &file_info->comment_size);
698 if (err == MZ_OK)
699 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700700 file_info->disk_number = value16;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700701 if (err == MZ_OK)
702 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
703 if (err == MZ_OK)
704 err = mz_stream_read_uint32(stream, &file_info->external_fa);
705 if (err == MZ_OK)
706 err = mz_stream_read_uint32(stream, &value32);
707 file_info->disk_offset = value32;
708 }
709 }
710
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700711 max_seek = file_info->filename_size + file_info->extrafield_size + file_info->comment_size + 3;
712 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700713 err = mz_stream_seek(file_info_stream, max_seek, MZ_SEEK_SET);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700714 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700715 err = mz_stream_seek(file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700716
717 if ((err == MZ_OK) && (file_info->filename_size > 0))
718 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700719 mz_stream_mem_get_buffer(file_info_stream, (void **)&file_info->filename);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700720
721 err = mz_stream_copy(file_info_stream, stream, file_info->filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700722 if (err == MZ_OK)
723 err = mz_stream_write_uint8(file_info_stream, 0);
724
725 seek += file_info->filename_size + 1;
726 }
727
728 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
729 {
730 mz_stream_mem_get_buffer_at(file_info_stream, seek, (void **)&file_info->extrafield);
731
732 err = mz_stream_copy(file_info_stream, stream, file_info->extrafield_size);
733 if (err == MZ_OK)
734 err = mz_stream_write_uint8(file_info_stream, 0);
735
736 // Seek back and parse the extra field
737 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700738 err = mz_stream_seek(file_info_stream, seek, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700739
740 seek += file_info->extrafield_size + 1;
741
742 while ((err == MZ_OK) && (extra_pos < file_info->extrafield_size))
743 {
744 err = mz_stream_read_uint16(file_info_stream, &extra_header_id);
745 if (err == MZ_OK)
746 err = mz_stream_read_uint16(file_info_stream, &extra_data_size);
747
748 // ZIP64 extra field
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700749 if (extra_header_id == MZ_ZIP_EXTENSION_ZIP64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700750 {
751 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
752 err = mz_stream_read_uint64(file_info_stream, &file_info->uncompressed_size);
753 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
754 err = mz_stream_read_uint64(file_info_stream, &file_info->compressed_size);
755 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
756 err = mz_stream_read_uint64(file_info_stream, &value64);
757 file_info->disk_offset = value64;
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700758 if ((err == MZ_OK) && (file_info->disk_number == UINT32_MAX))
759 err = mz_stream_read_uint32(file_info_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700760 }
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700761 // NTFS extra field
762 else if (extra_header_id == MZ_ZIP_EXTENSION_NTFS)
763 {
764 err = mz_stream_read_uint32(file_info_stream, &reserved);
765 extra_data_size_read = 4;
766
767 while ((err == MZ_OK) && (extra_data_size_read < extra_data_size))
768 {
769 err = mz_stream_read_uint16(file_info_stream, &ntfs_attrib_id);
770 if (err == MZ_OK)
771 err = mz_stream_read_uint16(file_info_stream, &ntfs_attrib_size);
772
773 if (ntfs_attrib_id == 0x01 && ntfs_attrib_size >= 8)
774 {
775 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
776 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
777
778 if ((err == MZ_OK) && (ntfs_attrib_size >= 16))
779 {
780 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
781 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
782 }
783 if ((err == MZ_OK) && (ntfs_attrib_size >= 24))
784 {
785 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
786 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
787 }
788 }
789 else
790 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700791 err = mz_stream_seek(file_info_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700792 }
793
794 extra_data_size_read += ntfs_attrib_size + 4;
795 }
796 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700797#ifdef HAVE_AES
798 // AES extra field
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700799 else if (extra_header_id == MZ_ZIP_EXTENSION_AES)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700800 {
801 uint8_t value8 = 0;
802 // Verify version info
803 err = mz_stream_read_uint16(file_info_stream, &value16);
804 // Support AE-1 and AE-2
805 if (value16 != 1 && value16 != 2)
806 err = MZ_FORMAT_ERROR;
807 file_info->aes_version = value16;
808 if (err == MZ_OK)
809 err = mz_stream_read_uint8(file_info_stream, &value8);
810 if ((char)value8 != 'A')
811 err = MZ_FORMAT_ERROR;
812 if (err == MZ_OK)
813 err = mz_stream_read_uint8(file_info_stream, &value8);
814 if ((char)value8 != 'E')
815 err = MZ_FORMAT_ERROR;
816 // Get AES encryption strength and actual compression method
817 if (err == MZ_OK)
818 err = mz_stream_read_uint8(file_info_stream, &value8);
819 file_info->aes_encryption_mode = value8;
820 if (err == MZ_OK)
821 err = mz_stream_read_uint16(file_info_stream, &value16);
822 file_info->compression_method = value16;
823 }
824#endif
825 else
826 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700827 err = mz_stream_seek(file_info_stream, extra_data_size, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700828 }
829
830 extra_pos += 4 + extra_data_size;
831 }
832 }
833
834 if ((err == MZ_OK) && (file_info->comment_size > 0))
835 {
836 mz_stream_mem_get_buffer_at(file_info_stream, seek, (void **)&file_info->comment);
837
838 err = mz_stream_copy(file_info_stream, stream, file_info->comment_size);
839 if (err == MZ_OK)
840 err = mz_stream_write_uint8(file_info_stream, 0);
841 }
842
843 return err;
844}
845
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700846static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700847{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700848 uint64_t ntfs_time = 0;
849 uint32_t reserved = 0;
850 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700851 uint16_t extrafield_size = 0;
852 uint16_t extrafield_zip64_size = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700853 uint16_t extrafield_ntfs_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700854 uint16_t filename_size = 0;
855 uint16_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700856 uint8_t zip64 = 0;
857 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700858
859 if (file_info == NULL)
860 return MZ_PARAM_ERROR;
861
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700862 extrafield_size = file_info->extrafield_size;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700863
864 if (file_info->uncompressed_size >= UINT32_MAX)
865 extrafield_zip64_size += 8;
866 if (file_info->compressed_size >= UINT32_MAX)
867 extrafield_zip64_size += 8;
868 if (file_info->disk_offset >= UINT32_MAX)
869 extrafield_zip64_size += 8;
870
871 // Use 64-bit data descriptor if we don't know uncompressed size ahead of time
872 zip64 = (local && file_info->uncompressed_size == 0) || (extrafield_zip64_size > 0);
873
874 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700875 {
876 extrafield_size += 4;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700877 extrafield_size += extrafield_zip64_size;
878 }
879#ifdef HAVE_AES
880 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
881 extrafield_size += 4 + 7;
882#endif
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700883 // NTFS timestamps
884 if (file_info->modified_date != 0)
885 extrafield_ntfs_size += 8;
886 if (file_info->accessed_date != 0)
887 extrafield_ntfs_size += 8;
888 if (file_info->creation_date != 0)
889 extrafield_ntfs_size += 8;
890
891 if (extrafield_ntfs_size > 4)
892 {
893 extrafield_ntfs_size += 4 + 4;
894
895 extrafield_size += 4;
896 extrafield_size += extrafield_ntfs_size;
897 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700898
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700899 file_info->version_needed = mz_zip_entry_get_version_needed(zip64, file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700900
901 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700902 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700903 else
904 {
905 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
906 if (err == MZ_OK)
907 err = mz_stream_write_uint16(stream, file_info->version_madeby);
908 }
909
910 if (err == MZ_OK)
911 err = mz_stream_write_uint16(stream, file_info->version_needed);
912 if (err == MZ_OK)
913 err = mz_stream_write_uint16(stream, file_info->flag);
914 if (err == MZ_OK)
915 {
916#ifdef HAVE_AES
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700917 if (file_info->aes_version)
918 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700919 else
920#endif
921 err = mz_stream_write_uint16(stream, file_info->compression_method);
922 }
923 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700924 {
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -0700925 if (file_info->modified_date != 0)
926 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700927 err = mz_stream_write_uint32(stream, dos_date);
928 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700929
930 if (err == MZ_OK)
931 err = mz_stream_write_uint32(stream, file_info->crc); // crc
932 if (err == MZ_OK)
933 {
934 if (file_info->compressed_size >= UINT32_MAX) // compr size
935 err = mz_stream_write_uint32(stream, UINT32_MAX);
936 else
937 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
938 }
939 if (err == MZ_OK)
940 {
941 if (file_info->uncompressed_size >= UINT32_MAX) // uncompr size
942 err = mz_stream_write_uint32(stream, UINT32_MAX);
943 else
944 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
945 }
946
947 filename_size = (uint16_t)strlen(file_info->filename);
948 if (err == MZ_OK)
949 err = mz_stream_write_uint16(stream, filename_size);
950 if (err == MZ_OK)
951 err = mz_stream_write_uint16(stream, extrafield_size);
952
953 if (!local)
954 {
955 if (file_info->comment != NULL)
956 comment_size = (uint16_t)strlen(file_info->comment);
957 if (err == MZ_OK)
958 err = mz_stream_write_uint16(stream, comment_size);
959 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700960 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700961 if (err == MZ_OK)
962 err = mz_stream_write_uint16(stream, file_info->internal_fa);
963 if (err == MZ_OK)
964 err = mz_stream_write_uint32(stream, file_info->external_fa);
965 if (err == MZ_OK)
966 {
967 if (file_info->disk_offset >= UINT32_MAX)
968 err = mz_stream_write_uint32(stream, UINT32_MAX);
969 else
970 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
971 }
972 }
973
974 if (err == MZ_OK)
975 {
976 if (mz_stream_write(stream, file_info->filename, filename_size) != filename_size)
977 err = MZ_STREAM_ERROR;
978 }
979 if (err == MZ_OK)
980 {
981 if (mz_stream_write(stream, file_info->extrafield, file_info->extrafield_size) != file_info->extrafield_size)
982 err = MZ_STREAM_ERROR;
983 }
984 // Add ZIP64 extra info header to central directory
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700985 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700986 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700987 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_ZIP64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700988 if (err == MZ_OK)
989 err = mz_stream_write_uint16(stream, extrafield_zip64_size);
990 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
991 err = mz_stream_write_uint64(stream, file_info->uncompressed_size);
992 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
993 err = mz_stream_write_uint64(stream, file_info->compressed_size);
994 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
995 err = mz_stream_write_uint64(stream, file_info->disk_offset);
996 }
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700997 // Write NTFS timestamps
998 if ((err == MZ_OK) && (extrafield_ntfs_size > 0))
999 {
1000 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_NTFS);
1001 if (err == MZ_OK)
1002 err = mz_stream_write_uint16(stream, extrafield_ntfs_size);
1003 if (err == MZ_OK)
1004 err = mz_stream_write_uint32(stream, reserved);
1005 if (err == MZ_OK)
1006 err = mz_stream_write_uint16(stream, 0x01);
1007 if (err == MZ_OK)
1008 err = mz_stream_write_uint16(stream, extrafield_ntfs_size - 8);
1009 if ((err == MZ_OK) && (file_info->modified_date != 0))
1010 {
1011 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
1012 err = mz_stream_write_uint64(stream, ntfs_time);
1013 }
1014 if ((err == MZ_OK) && (file_info->accessed_date != 0))
1015 {
1016 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
1017 err = mz_stream_write_uint64(stream, ntfs_time);
1018 }
1019 if ((err == MZ_OK) && (file_info->creation_date != 0))
1020 {
1021 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
1022 err = mz_stream_write_uint64(stream, ntfs_time);
1023 }
1024 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001025#ifdef HAVE_AES
1026 // Write AES extra info header to central directory
1027 if ((err == MZ_OK) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
1028 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001029 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001030 if (err == MZ_OK)
1031 err = mz_stream_write_uint16(stream, 7);
1032 if (err == MZ_OK)
1033 err = mz_stream_write_uint16(stream, file_info->aes_version);
1034 if (err == MZ_OK)
1035 err = mz_stream_write_uint8(stream, 'A');
1036 if (err == MZ_OK)
1037 err = mz_stream_write_uint8(stream, 'E');
1038 if (err == MZ_OK)
1039 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
1040 if (err == MZ_OK)
1041 err = mz_stream_write_uint16(stream, file_info->compression_method);
1042 }
1043#endif
1044 if ((err == MZ_OK) && (file_info->comment != NULL))
1045 {
1046 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != MZ_OK)
1047 err = MZ_STREAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001048 }
1049
1050 return err;
1051}
1052
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001053static 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 -07001054{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001055 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001056 int64_t max_total_in = 0;
1057 int64_t total_in = 0;
1058 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001059 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001060
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001061 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001062 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001063
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001064 zip->compression_method = compression_method;
1065
1066 switch (zip->compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001067 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001068 case MZ_COMPRESS_METHOD_RAW:
1069 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001070#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001071 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001072#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001073#if HAVE_LZMA
1074 case MZ_COMPRESS_METHOD_LZMA:
1075#endif
1076 err = MZ_OK;
1077 break;
1078 default:
1079 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001080 }
1081
1082 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL))
1083 return MZ_PARAM_ERROR;
1084
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001085 if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001086 {
1087#ifdef HAVE_AES
1088 if (zip->file_info.aes_version)
1089 {
1090 mz_stream_aes_create(&zip->crypt_stream);
1091 mz_stream_aes_set_password(zip->crypt_stream, password);
1092 mz_stream_aes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001093 }
1094 else
1095#endif
1096 {
1097#ifdef HAVE_CRYPT
1098 uint8_t verify1 = 0;
1099 uint8_t verify2 = 0;
1100
1101 // Info-ZIP modification to ZipCrypto format:
1102 // If bit 3 of the general purpose bit flag is set, it uses high byte of 16-bit File Time.
1103
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001104 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1105 {
1106 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001107
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001108 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1109
1110 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1111 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1112 }
1113 else
1114 {
1115 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1116 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1117 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001118
1119 mz_stream_crypt_create(&zip->crypt_stream);
1120 mz_stream_crypt_set_password(zip->crypt_stream, password);
1121 mz_stream_crypt_set_verify(zip->crypt_stream, verify1, verify2);
1122#endif
1123 }
1124 }
1125
1126 if (err == MZ_OK)
1127 {
1128 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001129 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001130
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001131 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001132
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001133 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001134 }
1135
1136 if (err == MZ_OK)
1137 {
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001138 if (zip->compression_method == MZ_COMPRESS_METHOD_RAW)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001139 mz_stream_raw_create(&zip->compress_stream);
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001140 else if (zip->compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001141 mz_stream_zlib_create(&zip->compress_stream);
1142#ifdef HAVE_BZIP2
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001143 else if (zip->compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001144 mz_stream_bzip_create(&zip->compress_stream);
1145#endif
1146#ifdef HAVE_LZMA
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001147 else if (zip->compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001148 mz_stream_lzma_create(&zip->compress_stream);
1149#endif
1150 else
1151 err = MZ_PARAM_ERROR;
1152 }
1153
1154 if (err == MZ_OK)
1155 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001156 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001157 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001158 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001159 }
1160 else
1161 {
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001162 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED || zip->compression_method == MZ_COMPRESS_METHOD_RAW)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001163 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001164 max_total_in = zip->file_info.compressed_size;
1165 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1166 max_total_in -= footer_size;
1167 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in) == MZ_OK)
1168 max_total_in -= total_in;
1169 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001170 }
juanii55bcdaf2018-02-11 20:55:57 -03001171 if (zip->compression_method == MZ_COMPRESS_METHOD_LZMA &&
1172 (zip->file_info.flag & MZ_ZIP_FLAG_LZMA_EOS_MARKER) == 0)
1173 {
1174 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1175 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1176 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001177 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001178
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001179 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1180
1181 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1182 }
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001183 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001184 {
1185 mz_stream_crc32_create(&zip->crc32_stream);
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -08001186#ifdef HAVE_ZLIB
1187 mz_stream_crc32_set_update_func(zip->crc32_stream,
1188 (mz_stream_crc32_update)mz_stream_zlib_get_crc32_update());
1189#elif defined(HAVE_LZMA)
1190 mz_stream_crc32_set_update_func(zip->crc32_stream,
1191 (mz_stream_crc32_update)mz_stream_lzma_get_crc32_update());
1192#else
1193 #error ZLIB or LZMA required for CRC32
1194#endif
1195
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001196 mz_stream_set_base(zip->crc32_stream, zip->compress_stream);
1197
1198 err = mz_stream_open(zip->crc32_stream, NULL, zip->open_mode);
1199 }
1200
1201 if (err == MZ_OK)
1202 {
1203 zip->entry_opened = 1;
1204 }
1205
1206 return err;
1207}
1208
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001209extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001210{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001211 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001212 int16_t compression_method = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001213 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001214
1215#if !defined(HAVE_CRYPT) && !defined(HAVE_AES)
1216 if (password != NULL)
1217 return MZ_PARAM_ERROR;
1218#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001219 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001220 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001221 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001222 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001223 if (zip->entry_scanned == 0)
1224 return MZ_PARAM_ERROR;
1225 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL))
1226 return MZ_PARAM_ERROR;
1227
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001228 if (zip->file_info.disk_number == zip->disk_number_with_cd)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001229 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1230 else
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001231 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001232
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001233 err = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001234 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001235 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 -07001236
Nathan Moinvaziri51f72502017-10-26 10:15:21 -07001237 compression_method = zip->file_info.compression_method;
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001238 if (raw)
1239 compression_method = MZ_COMPRESS_METHOD_RAW;
1240
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001241 if (err == MZ_OK)
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001242 err = mz_zip_entry_open_int(handle, compression_method, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001243
1244 return err;
1245}
1246
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001247extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info,
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001248 int16_t compress_level, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001249{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001250 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001251 int64_t disk_number = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001252 int32_t err = MZ_OK;
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001253 int16_t compression_method = 0;
1254
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001255
1256#if !defined(HAVE_CRYPT) && !defined(HAVE_AES)
tz-lomb1b25802017-11-10 15:03:02 +03001257 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001258 return MZ_PARAM_ERROR;
1259#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001260 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001261 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001262
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001263 if (zip->entry_opened == 1)
1264 {
1265 err = mz_zip_entry_close(handle);
1266 if (err != MZ_OK)
1267 return err;
1268 }
1269
1270 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001271
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001272 compression_method = zip->file_info.compression_method;
1273 if (compress_level == 0)
1274 compression_method = MZ_COMPRESS_METHOD_RAW;
1275
1276 if (compression_method == MZ_COMPRESS_METHOD_DEFLATE)
1277 {
1278 if ((compress_level == 8) || (compress_level == 9))
1279 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
1280 if (compress_level == 2)
1281 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
1282 if (compress_level == 1)
1283 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1284 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001285#ifdef HAVE_LZMA
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001286 else if (compression_method == MZ_COMPRESS_METHOD_LZMA)
1287 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001288#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001289
1290 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001291
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001292 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001293 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
1294 else
1295 zip->file_info.flag &= ~MZ_ZIP_FLAG_ENCRYPTED;
1296
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001297 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
1298 zip->file_info.disk_number = (uint32_t)disk_number;
1299
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001300 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001301 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001302 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001303
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001304#ifdef HAVE_AES
1305 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
1306 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
1307#endif
1308
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001309 if (err == MZ_OK)
1310 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
1311 if (err == MZ_OK)
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001312 err = mz_zip_entry_open_int(handle, compression_method, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001313
1314 return err;
1315}
1316
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001317extern int32_t mz_zip_entry_read(void *handle, void *buf, uint32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001318{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001319 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001320 int32_t read = 0;
1321
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001322 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001323 return MZ_PARAM_ERROR;
Nathan Moinvaziri930f9182018-01-22 08:51:07 -08001324 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) // Zlib limitation
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001325 return MZ_PARAM_ERROR;
Nathan Moinvaziria86a4352018-01-02 13:12:07 -08001326 if (len == 0 || zip->file_info.uncompressed_size == 0)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001327 return 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001328 read = mz_stream_read(zip->crc32_stream, buf, len);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001329 if (read > 0)
1330 zip->entry_read += read;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001331 return read;
1332}
1333
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001334extern int32_t mz_zip_entry_write(void *handle, const void *buf, uint32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001335{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001336 mz_zip *zip = (mz_zip *)handle;
1337 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001338 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001339 return mz_stream_write(zip->crc32_stream, buf, len);
1340}
1341
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001342extern int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001343{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001344 mz_zip *zip = (mz_zip *)handle;
1345 if (zip == NULL || zip->entry_scanned == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001346 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001347 *file_info = &zip->file_info;
1348 return MZ_OK;
1349}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001350
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001351extern int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001352{
1353 mz_zip *zip = (mz_zip *)handle;
1354 if (zip == NULL || zip->entry_scanned == 0 || zip->entry_opened == 0)
1355 return MZ_PARAM_ERROR;
1356 *local_file_info = &zip->local_file_info;
1357 return MZ_OK;
1358}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001359
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001360extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001361{
1362 mz_zip *zip = (mz_zip *)handle;
1363 uint64_t compressed_size = 0;
1364 int32_t err = MZ_OK;
1365
1366 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001367 return MZ_PARAM_ERROR;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001368
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001369 mz_stream_close(zip->compress_stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001370 if (crc32 == 0)
1371 crc32 = mz_stream_crc32_get_value(zip->crc32_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001372
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001373 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001374 {
1375#ifdef HAVE_AES
1376 // AES zip version AE-1 will expect a valid crc as well
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001377 if (zip->file_info.aes_version <= 0x0001)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001378#endif
1379 {
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001380 if ((zip->entry_read > 0) && (zip->compression_method != MZ_COMPRESS_METHOD_RAW))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001381 {
1382 if (crc32 != zip->file_info.crc)
1383 err = MZ_CRC_ERROR;
1384 }
1385 }
1386 }
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001387
1388 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001389 if ((zip->compression_method != MZ_COMPRESS_METHOD_RAW) || (uncompressed_size == 0))
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001390 mz_stream_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001391
1392 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
1393 {
1394 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001395 err = mz_stream_close(zip->crypt_stream);
1396
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001397 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001398 }
1399
1400 mz_stream_delete(&zip->crypt_stream);
1401
1402 mz_stream_delete(&zip->compress_stream);
1403 mz_stream_crc32_delete(&zip->crc32_stream);
1404
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001405 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001406 {
1407 if (err == MZ_OK)
1408 {
1409 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
1410 if (err == MZ_OK)
1411 err = mz_stream_write_uint32(zip->stream, crc32);
1412 if (err == MZ_OK)
1413 {
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001414 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001415 err = mz_stream_write_uint32(zip->stream, (uint32_t)compressed_size);
Andrew Gunnerson3c52d212018-01-05 22:28:12 -05001416 else
1417 err = mz_stream_write_uint64(zip->stream, compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001418 }
1419 if (err == MZ_OK)
1420 {
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001421 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001422 err = mz_stream_write_uint32(zip->stream, (uint32_t)uncompressed_size);
Andrew Gunnerson3c52d212018-01-05 22:28:12 -05001423 else
1424 err = mz_stream_write_uint64(zip->stream, uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001425 }
1426 }
1427
1428 zip->file_info.crc = crc32;
1429 zip->file_info.compressed_size = compressed_size;
1430 zip->file_info.uncompressed_size = uncompressed_size;
1431
1432 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001433 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001434
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001435 zip->number_entry += 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001436 }
1437
1438 zip->entry_opened = 0;
1439
1440 return err;
1441}
1442
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001443extern int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001444{
1445 return mz_zip_entry_close_raw(handle, 0, 0);
1446}
1447
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001448static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001449{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001450 mz_zip *zip = (mz_zip *)handle;
1451 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001452
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001453 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001454 return MZ_PARAM_ERROR;
1455
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001456 zip->entry_scanned = 0;
1457
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001458 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001459
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001460 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001461 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001462 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001463 if (err == MZ_OK)
1464 zip->entry_scanned = 1;
1465 return err;
1466}
1467
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001468extern int32_t mz_zip_get_number_entry(void *handle, int64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001469{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001470 mz_zip *zip = (mz_zip *)handle;
1471 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001472 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001473 *number_entry = zip->number_entry;
1474 return MZ_OK;
1475}
1476
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001477extern int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001478{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001479 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001480
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001481 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001482 return MZ_PARAM_ERROR;
1483
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001484 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001485
1486 return mz_zip_goto_next_entry_int(handle);
1487}
1488
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001489extern int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001490{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001491 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001492
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001493 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001494 return MZ_PARAM_ERROR;
1495
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001496 zip->cd_current_pos += MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
1497 zip->file_info.extrafield_size + zip->file_info.comment_size;
1498
1499 return mz_zip_goto_next_entry_int(handle);
1500}
1501
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001502extern 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 -07001503{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001504 mz_zip *zip = (mz_zip *)handle;
1505 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001506 int32_t result = 0;
1507
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001508 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001509 return MZ_PARAM_ERROR;
1510
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001511 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001512 while (err == MZ_OK)
1513 {
1514 if (filename_compare_cb != NULL)
1515 result = filename_compare_cb(handle, zip->file_info.filename, filename);
1516 else
1517 result = strcmp(zip->file_info.filename, filename);
1518
1519 if (result == 0)
1520 return MZ_OK;
1521
1522 err = mz_zip_goto_next_entry(handle);
1523 }
1524
1525 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001526}
1527
1528/***************************************************************************/
1529
1530static int32_t mz_zip_invalid_date(const struct tm *ptm)
1531{
1532#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
1533 return (!datevalue_in_range(0, 207, ptm->tm_year) ||
1534 !datevalue_in_range(0, 11, ptm->tm_mon) ||
1535 !datevalue_in_range(1, 31, ptm->tm_mday) ||
1536 !datevalue_in_range(0, 23, ptm->tm_hour) ||
1537 !datevalue_in_range(0, 59, ptm->tm_min) ||
1538 !datevalue_in_range(0, 59, ptm->tm_sec));
1539#undef datevalue_in_range
1540}
1541
1542static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
1543{
1544 uint64_t date = (uint64_t)(dos_date >> 16);
1545
1546 ptm->tm_mday = (uint16_t)(date & 0x1f);
1547 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
1548 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
1549 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
1550 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
1551 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
1552 ptm->tm_isdst = -1;
1553}
1554
1555int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
1556{
1557 if (ptm == NULL)
1558 return MZ_PARAM_ERROR;
1559
1560 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
1561
1562 if (mz_zip_invalid_date(ptm))
1563 {
1564 // Invalid date stored, so don't return it
1565 memset(ptm, 0, sizeof(struct tm));
1566 return MZ_FORMAT_ERROR;
1567 }
1568 return MZ_OK;
1569}
1570
1571time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
1572{
1573 struct tm ptm;
1574 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
1575 return mktime(&ptm);
1576}
1577
1578int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
1579{
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001580 struct tm *ltm = NULL;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001581 if (ptm == NULL)
1582 return MZ_PARAM_ERROR;
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001583 ltm = localtime(&unix_time);
1584 if (ltm == NULL)
1585 return MZ_INTERNAL_ERROR;
1586 memcpy(ptm, ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001587 return MZ_OK;
1588}
1589
1590uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
1591{
1592 struct tm ptm;
1593 mz_zip_time_t_to_tm(unix_time, &ptm);
1594 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
1595}
1596
1597uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
1598{
1599 struct tm fixed_tm = { 0 };
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001600
1601 // Years supported:
1602
1603 // [00, 79] (assumed to be between 2000 and 2079)
1604 // [80, 207] (assumed to be between 1980 and 2107, typical output of old
1605 // software that does 'year-1900' to get a double digit year)
1606 // [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.)
1607
1608 memcpy(&fixed_tm, ptm, sizeof(struct tm));
1609 if (fixed_tm.tm_year >= 1980) // range [1980, 2107]
1610 fixed_tm.tm_year -= 1980;
1611 else if (fixed_tm.tm_year >= 80) // range [80, 99]
1612 fixed_tm.tm_year -= 80;
1613 else // range [00, 79]
1614 fixed_tm.tm_year += 20;
1615
1616 if (mz_zip_invalid_date(ptm))
1617 return 0;
1618
1619 return (uint32_t)(((fixed_tm.tm_mday) + (32 * (fixed_tm.tm_mon + 1)) + (512 * fixed_tm.tm_year)) << 16) |
1620 ((fixed_tm.tm_sec / 2) + (32 * fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
1621}
1622
1623int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
1624{
1625 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
1626 return MZ_OK;
1627}
1628
1629int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
1630{
1631 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
1632 return MZ_OK;
1633}