blob: 9e37dd79d731ef9314eb1038d01b16b05c230c3b [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvaziriad8e3dd2018-04-18 20:54:52 -07002 Version 2.2.9, April 18th, 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;
juanii4ae79922018-02-11 14:29:36 -0300758 if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700759 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
Nathan Moinvazirie56962c2018-02-13 15:31:15 -0800773 if ((ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700774 {
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
juanii7063b0e2018-02-11 13:56:21 -0300778 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700779 {
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 }
juanii7063b0e2018-02-11 13:56:21 -0300783 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700784 {
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
Nathan Moinvazirid9e159a2018-02-13 15:30:30 -0800884 if ((file_info->modified_date != 0) &&
885 (file_info->accessed_date != 0) &&
886 (file_info->creation_date != 0))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700887 {
juanii3679a3d2018-02-11 13:55:38 -0300888 extrafield_ntfs_size += 8 + 8 + 8 + 4 + 2 + 2;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700889 extrafield_size += 4;
890 extrafield_size += extrafield_ntfs_size;
891 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700892
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700893 file_info->version_needed = mz_zip_entry_get_version_needed(zip64, file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700894
895 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700896 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700897 else
898 {
899 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
900 if (err == MZ_OK)
901 err = mz_stream_write_uint16(stream, file_info->version_madeby);
902 }
903
904 if (err == MZ_OK)
905 err = mz_stream_write_uint16(stream, file_info->version_needed);
906 if (err == MZ_OK)
907 err = mz_stream_write_uint16(stream, file_info->flag);
908 if (err == MZ_OK)
909 {
910#ifdef HAVE_AES
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700911 if (file_info->aes_version)
912 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700913 else
914#endif
915 err = mz_stream_write_uint16(stream, file_info->compression_method);
916 }
917 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700918 {
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -0700919 if (file_info->modified_date != 0)
920 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700921 err = mz_stream_write_uint32(stream, dos_date);
922 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700923
924 if (err == MZ_OK)
925 err = mz_stream_write_uint32(stream, file_info->crc); // crc
926 if (err == MZ_OK)
927 {
928 if (file_info->compressed_size >= UINT32_MAX) // compr size
929 err = mz_stream_write_uint32(stream, UINT32_MAX);
930 else
931 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
932 }
933 if (err == MZ_OK)
934 {
935 if (file_info->uncompressed_size >= UINT32_MAX) // uncompr size
936 err = mz_stream_write_uint32(stream, UINT32_MAX);
937 else
938 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
939 }
940
941 filename_size = (uint16_t)strlen(file_info->filename);
942 if (err == MZ_OK)
943 err = mz_stream_write_uint16(stream, filename_size);
944 if (err == MZ_OK)
945 err = mz_stream_write_uint16(stream, extrafield_size);
946
947 if (!local)
948 {
949 if (file_info->comment != NULL)
950 comment_size = (uint16_t)strlen(file_info->comment);
951 if (err == MZ_OK)
952 err = mz_stream_write_uint16(stream, comment_size);
953 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700954 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700955 if (err == MZ_OK)
956 err = mz_stream_write_uint16(stream, file_info->internal_fa);
957 if (err == MZ_OK)
958 err = mz_stream_write_uint32(stream, file_info->external_fa);
959 if (err == MZ_OK)
960 {
961 if (file_info->disk_offset >= UINT32_MAX)
962 err = mz_stream_write_uint32(stream, UINT32_MAX);
963 else
964 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
965 }
966 }
967
968 if (err == MZ_OK)
969 {
970 if (mz_stream_write(stream, file_info->filename, filename_size) != filename_size)
971 err = MZ_STREAM_ERROR;
972 }
973 if (err == MZ_OK)
974 {
975 if (mz_stream_write(stream, file_info->extrafield, file_info->extrafield_size) != file_info->extrafield_size)
976 err = MZ_STREAM_ERROR;
977 }
978 // Add ZIP64 extra info header to central directory
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700979 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700980 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700981 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_ZIP64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700982 if (err == MZ_OK)
983 err = mz_stream_write_uint16(stream, extrafield_zip64_size);
984 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
985 err = mz_stream_write_uint64(stream, file_info->uncompressed_size);
986 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
987 err = mz_stream_write_uint64(stream, file_info->compressed_size);
988 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
989 err = mz_stream_write_uint64(stream, file_info->disk_offset);
990 }
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700991 // Write NTFS timestamps
992 if ((err == MZ_OK) && (extrafield_ntfs_size > 0))
993 {
994 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_NTFS);
995 if (err == MZ_OK)
996 err = mz_stream_write_uint16(stream, extrafield_ntfs_size);
997 if (err == MZ_OK)
998 err = mz_stream_write_uint32(stream, reserved);
999 if (err == MZ_OK)
1000 err = mz_stream_write_uint16(stream, 0x01);
1001 if (err == MZ_OK)
1002 err = mz_stream_write_uint16(stream, extrafield_ntfs_size - 8);
juanii3679a3d2018-02-11 13:55:38 -03001003 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001004 {
1005 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
1006 err = mz_stream_write_uint64(stream, ntfs_time);
1007 }
juanii3679a3d2018-02-11 13:55:38 -03001008 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001009 {
1010 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
1011 err = mz_stream_write_uint64(stream, ntfs_time);
1012 }
juanii3679a3d2018-02-11 13:55:38 -03001013 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001014 {
1015 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
1016 err = mz_stream_write_uint64(stream, ntfs_time);
1017 }
1018 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001019#ifdef HAVE_AES
1020 // Write AES extra info header to central directory
1021 if ((err == MZ_OK) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
1022 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001023 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001024 if (err == MZ_OK)
1025 err = mz_stream_write_uint16(stream, 7);
1026 if (err == MZ_OK)
1027 err = mz_stream_write_uint16(stream, file_info->aes_version);
1028 if (err == MZ_OK)
1029 err = mz_stream_write_uint8(stream, 'A');
1030 if (err == MZ_OK)
1031 err = mz_stream_write_uint8(stream, 'E');
1032 if (err == MZ_OK)
1033 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
1034 if (err == MZ_OK)
1035 err = mz_stream_write_uint16(stream, file_info->compression_method);
1036 }
1037#endif
1038 if ((err == MZ_OK) && (file_info->comment != NULL))
1039 {
1040 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != MZ_OK)
1041 err = MZ_STREAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001042 }
1043
1044 return err;
1045}
1046
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001047static 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 -07001048{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001049 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001050 int64_t max_total_in = 0;
1051 int64_t total_in = 0;
1052 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001053 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001054
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001055 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001056 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001057
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001058 zip->compression_method = compression_method;
1059
1060 switch (zip->compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001061 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001062 case MZ_COMPRESS_METHOD_RAW:
1063 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001064#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001065 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001066#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001067#if HAVE_LZMA
1068 case MZ_COMPRESS_METHOD_LZMA:
1069#endif
1070 err = MZ_OK;
1071 break;
1072 default:
1073 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001074 }
1075
1076 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL))
1077 return MZ_PARAM_ERROR;
1078
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001079 if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001080 {
1081#ifdef HAVE_AES
1082 if (zip->file_info.aes_version)
1083 {
1084 mz_stream_aes_create(&zip->crypt_stream);
1085 mz_stream_aes_set_password(zip->crypt_stream, password);
1086 mz_stream_aes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001087 }
1088 else
1089#endif
1090 {
1091#ifdef HAVE_CRYPT
1092 uint8_t verify1 = 0;
1093 uint8_t verify2 = 0;
1094
1095 // Info-ZIP modification to ZipCrypto format:
1096 // If bit 3 of the general purpose bit flag is set, it uses high byte of 16-bit File Time.
1097
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001098 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1099 {
1100 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001101
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001102 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1103
1104 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1105 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1106 }
1107 else
1108 {
1109 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1110 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1111 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001112
1113 mz_stream_crypt_create(&zip->crypt_stream);
1114 mz_stream_crypt_set_password(zip->crypt_stream, password);
1115 mz_stream_crypt_set_verify(zip->crypt_stream, verify1, verify2);
1116#endif
1117 }
1118 }
1119
1120 if (err == MZ_OK)
1121 {
1122 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001123 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001124
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001125 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001126
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001127 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001128 }
1129
1130 if (err == MZ_OK)
1131 {
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001132 if (zip->compression_method == MZ_COMPRESS_METHOD_RAW)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001133 mz_stream_raw_create(&zip->compress_stream);
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001134 else if (zip->compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001135 mz_stream_zlib_create(&zip->compress_stream);
1136#ifdef HAVE_BZIP2
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001137 else if (zip->compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001138 mz_stream_bzip_create(&zip->compress_stream);
1139#endif
1140#ifdef HAVE_LZMA
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001141 else if (zip->compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001142 mz_stream_lzma_create(&zip->compress_stream);
1143#endif
1144 else
1145 err = MZ_PARAM_ERROR;
1146 }
1147
1148 if (err == MZ_OK)
1149 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001150 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001151 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001152 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001153 }
1154 else
1155 {
Nathan Moinvaziri5e8f4d72018-03-15 10:43:18 -07001156 if (zip->compression_method == MZ_COMPRESS_METHOD_RAW || zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001157 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001158 max_total_in = zip->file_info.compressed_size;
1159 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1160 max_total_in -= footer_size;
1161 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in) == MZ_OK)
1162 max_total_in -= total_in;
1163 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001164 }
Nathan Moinvaziri5e8f4d72018-03-15 10:43:18 -07001165 if (zip->compression_method == MZ_COMPRESS_METHOD_LZMA && (zip->file_info.flag & MZ_ZIP_FLAG_LZMA_EOS_MARKER) == 0)
juanii55bcdaf2018-02-11 20:55:57 -03001166 {
1167 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1168 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1169 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001170 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001171
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001172 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1173
1174 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1175 }
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001176 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001177 {
1178 mz_stream_crc32_create(&zip->crc32_stream);
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -08001179#ifdef HAVE_ZLIB
1180 mz_stream_crc32_set_update_func(zip->crc32_stream,
1181 (mz_stream_crc32_update)mz_stream_zlib_get_crc32_update());
1182#elif defined(HAVE_LZMA)
1183 mz_stream_crc32_set_update_func(zip->crc32_stream,
1184 (mz_stream_crc32_update)mz_stream_lzma_get_crc32_update());
1185#else
1186 #error ZLIB or LZMA required for CRC32
1187#endif
1188
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001189 mz_stream_set_base(zip->crc32_stream, zip->compress_stream);
1190
1191 err = mz_stream_open(zip->crc32_stream, NULL, zip->open_mode);
1192 }
1193
1194 if (err == MZ_OK)
1195 {
1196 zip->entry_opened = 1;
1197 }
1198
1199 return err;
1200}
1201
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001202extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001203{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001204 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001205 int16_t compression_method = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001206 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001207
1208#if !defined(HAVE_CRYPT) && !defined(HAVE_AES)
1209 if (password != NULL)
1210 return MZ_PARAM_ERROR;
1211#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001212 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001213 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001214 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001215 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001216 if (zip->entry_scanned == 0)
1217 return MZ_PARAM_ERROR;
1218 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL))
1219 return MZ_PARAM_ERROR;
1220
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001221 if (zip->file_info.disk_number == zip->disk_number_with_cd)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001222 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1223 else
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001224 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001225
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001226 err = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001227 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001228 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 -07001229
Nathan Moinvaziri51f72502017-10-26 10:15:21 -07001230 compression_method = zip->file_info.compression_method;
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001231 if (raw)
1232 compression_method = MZ_COMPRESS_METHOD_RAW;
1233
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001234 if (err == MZ_OK)
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001235 err = mz_zip_entry_open_int(handle, compression_method, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001236
1237 return err;
1238}
1239
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001240extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info,
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001241 int16_t compress_level, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001242{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001243 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001244 int64_t disk_number = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001245 int32_t err = MZ_OK;
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001246 int16_t compression_method = 0;
1247
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001248
1249#if !defined(HAVE_CRYPT) && !defined(HAVE_AES)
tz-lomb1b25802017-11-10 15:03:02 +03001250 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001251 return MZ_PARAM_ERROR;
1252#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001253 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001254 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001255
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001256 if (zip->entry_opened == 1)
1257 {
1258 err = mz_zip_entry_close(handle);
1259 if (err != MZ_OK)
1260 return err;
1261 }
1262
1263 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001264
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001265 compression_method = zip->file_info.compression_method;
1266 if (compress_level == 0)
1267 compression_method = MZ_COMPRESS_METHOD_RAW;
1268
1269 if (compression_method == MZ_COMPRESS_METHOD_DEFLATE)
1270 {
1271 if ((compress_level == 8) || (compress_level == 9))
1272 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
1273 if (compress_level == 2)
1274 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
1275 if (compress_level == 1)
1276 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1277 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001278#ifdef HAVE_LZMA
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001279 else if (compression_method == MZ_COMPRESS_METHOD_LZMA)
1280 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001281#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001282
1283 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001284
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001285 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001286 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
1287 else
1288 zip->file_info.flag &= ~MZ_ZIP_FLAG_ENCRYPTED;
1289
juaniib8887e92018-02-14 00:51:05 -03001290 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1291 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001292
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001293 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001294 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001295 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001296
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001297#ifdef HAVE_AES
1298 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
1299 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
1300#endif
1301
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001302 if (err == MZ_OK)
1303 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
1304 if (err == MZ_OK)
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001305 err = mz_zip_entry_open_int(handle, compression_method, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001306
1307 return err;
1308}
1309
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001310extern int32_t mz_zip_entry_read(void *handle, 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;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001313 int32_t read = 0;
1314
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001315 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001316 return MZ_PARAM_ERROR;
Nathan Moinvaziri930f9182018-01-22 08:51:07 -08001317 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) // Zlib limitation
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001318 return MZ_PARAM_ERROR;
Nathan Moinvaziria86a4352018-01-02 13:12:07 -08001319 if (len == 0 || zip->file_info.uncompressed_size == 0)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001320 return 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001321 read = mz_stream_read(zip->crc32_stream, buf, len);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001322 if (read > 0)
1323 zip->entry_read += read;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001324 return read;
1325}
1326
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001327extern int32_t mz_zip_entry_write(void *handle, const void *buf, uint32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001328{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001329 mz_zip *zip = (mz_zip *)handle;
1330 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001331 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001332 return mz_stream_write(zip->crc32_stream, buf, len);
1333}
1334
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001335extern int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001336{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001337 mz_zip *zip = (mz_zip *)handle;
1338 if (zip == NULL || zip->entry_scanned == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001339 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001340 *file_info = &zip->file_info;
1341 return MZ_OK;
1342}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001343
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001344extern int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001345{
1346 mz_zip *zip = (mz_zip *)handle;
1347 if (zip == NULL || zip->entry_scanned == 0 || zip->entry_opened == 0)
1348 return MZ_PARAM_ERROR;
1349 *local_file_info = &zip->local_file_info;
1350 return MZ_OK;
1351}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001352
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001353extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001354{
1355 mz_zip *zip = (mz_zip *)handle;
1356 uint64_t compressed_size = 0;
1357 int32_t err = MZ_OK;
1358
1359 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001360 return MZ_PARAM_ERROR;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001361
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001362 mz_stream_close(zip->compress_stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001363 if (crc32 == 0)
1364 crc32 = mz_stream_crc32_get_value(zip->crc32_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001365
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001366 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001367 {
1368#ifdef HAVE_AES
1369 // AES zip version AE-1 will expect a valid crc as well
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001370 if (zip->file_info.aes_version <= 0x0001)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001371#endif
1372 {
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001373 if ((zip->entry_read > 0) && (zip->compression_method != MZ_COMPRESS_METHOD_RAW))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001374 {
1375 if (crc32 != zip->file_info.crc)
1376 err = MZ_CRC_ERROR;
1377 }
1378 }
1379 }
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001380
1381 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001382 if ((zip->compression_method != MZ_COMPRESS_METHOD_RAW) || (uncompressed_size == 0))
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001383 mz_stream_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001384
1385 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
1386 {
1387 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001388 err = mz_stream_close(zip->crypt_stream);
1389
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001390 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001391 }
1392
1393 mz_stream_delete(&zip->crypt_stream);
1394
1395 mz_stream_delete(&zip->compress_stream);
1396 mz_stream_crc32_delete(&zip->crc32_stream);
1397
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001398 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001399 {
1400 if (err == MZ_OK)
1401 {
1402 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
1403 if (err == MZ_OK)
1404 err = mz_stream_write_uint32(zip->stream, crc32);
1405 if (err == MZ_OK)
1406 {
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001407 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001408 err = mz_stream_write_uint32(zip->stream, (uint32_t)compressed_size);
Andrew Gunnerson3c52d212018-01-05 22:28:12 -05001409 else
1410 err = mz_stream_write_uint64(zip->stream, compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001411 }
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)uncompressed_size);
Andrew Gunnerson3c52d212018-01-05 22:28:12 -05001416 else
1417 err = mz_stream_write_uint64(zip->stream, uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001418 }
1419 }
1420
1421 zip->file_info.crc = crc32;
1422 zip->file_info.compressed_size = compressed_size;
1423 zip->file_info.uncompressed_size = uncompressed_size;
1424
1425 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001426 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001427
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001428 zip->number_entry += 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001429 }
1430
1431 zip->entry_opened = 0;
1432
1433 return err;
1434}
1435
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001436extern int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001437{
1438 return mz_zip_entry_close_raw(handle, 0, 0);
1439}
1440
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001441static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001442{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001443 mz_zip *zip = (mz_zip *)handle;
1444 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001445
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001446 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001447 return MZ_PARAM_ERROR;
1448
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001449 zip->entry_scanned = 0;
1450
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001451 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001452
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001453 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001454 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001455 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001456 if (err == MZ_OK)
1457 zip->entry_scanned = 1;
1458 return err;
1459}
1460
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001461extern int32_t mz_zip_get_number_entry(void *handle, int64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001462{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001463 mz_zip *zip = (mz_zip *)handle;
1464 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001465 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001466 *number_entry = zip->number_entry;
1467 return MZ_OK;
1468}
1469
Nathan Moinvaziri8d8a9872018-03-19 18:23:13 -07001470extern int32_t mz_zip_get_disk_number_with_cd(void *handle, int32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03001471{
1472 mz_zip *zip = (mz_zip *)handle;
1473 if (zip == NULL || disk_number_with_cd == NULL)
1474 return MZ_PARAM_ERROR;
1475 *disk_number_with_cd = zip->disk_number_with_cd;
1476 return MZ_OK;
1477}
1478
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001479extern int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001480{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001481 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001482
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001483 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001484 return MZ_PARAM_ERROR;
1485
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001486 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001487
1488 return mz_zip_goto_next_entry_int(handle);
1489}
1490
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001491extern int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001492{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001493 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001494
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001495 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001496 return MZ_PARAM_ERROR;
1497
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001498 zip->cd_current_pos += MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
1499 zip->file_info.extrafield_size + zip->file_info.comment_size;
1500
1501 return mz_zip_goto_next_entry_int(handle);
1502}
1503
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001504extern 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 -07001505{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001506 mz_zip *zip = (mz_zip *)handle;
1507 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001508 int32_t result = 0;
1509
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001510 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001511 return MZ_PARAM_ERROR;
1512
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001513 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001514 while (err == MZ_OK)
1515 {
1516 if (filename_compare_cb != NULL)
1517 result = filename_compare_cb(handle, zip->file_info.filename, filename);
1518 else
1519 result = strcmp(zip->file_info.filename, filename);
1520
1521 if (result == 0)
1522 return MZ_OK;
1523
1524 err = mz_zip_goto_next_entry(handle);
1525 }
1526
1527 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001528}
1529
1530/***************************************************************************/
1531
1532static int32_t mz_zip_invalid_date(const struct tm *ptm)
1533{
1534#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
1535 return (!datevalue_in_range(0, 207, ptm->tm_year) ||
1536 !datevalue_in_range(0, 11, ptm->tm_mon) ||
1537 !datevalue_in_range(1, 31, ptm->tm_mday) ||
1538 !datevalue_in_range(0, 23, ptm->tm_hour) ||
1539 !datevalue_in_range(0, 59, ptm->tm_min) ||
1540 !datevalue_in_range(0, 59, ptm->tm_sec));
1541#undef datevalue_in_range
1542}
1543
1544static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
1545{
1546 uint64_t date = (uint64_t)(dos_date >> 16);
1547
1548 ptm->tm_mday = (uint16_t)(date & 0x1f);
1549 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
1550 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
1551 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
1552 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
1553 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
1554 ptm->tm_isdst = -1;
1555}
1556
1557int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
1558{
1559 if (ptm == NULL)
1560 return MZ_PARAM_ERROR;
1561
1562 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
1563
1564 if (mz_zip_invalid_date(ptm))
1565 {
1566 // Invalid date stored, so don't return it
1567 memset(ptm, 0, sizeof(struct tm));
1568 return MZ_FORMAT_ERROR;
1569 }
1570 return MZ_OK;
1571}
1572
1573time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
1574{
1575 struct tm ptm;
1576 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
1577 return mktime(&ptm);
1578}
1579
1580int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
1581{
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001582 struct tm *ltm = NULL;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001583 if (ptm == NULL)
1584 return MZ_PARAM_ERROR;
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001585 ltm = localtime(&unix_time);
1586 if (ltm == NULL)
1587 return MZ_INTERNAL_ERROR;
1588 memcpy(ptm, ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001589 return MZ_OK;
1590}
1591
1592uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
1593{
1594 struct tm ptm;
1595 mz_zip_time_t_to_tm(unix_time, &ptm);
1596 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
1597}
1598
1599uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
1600{
1601 struct tm fixed_tm = { 0 };
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001602
1603 // Years supported:
1604
1605 // [00, 79] (assumed to be between 2000 and 2079)
1606 // [80, 207] (assumed to be between 1980 and 2107, typical output of old
1607 // software that does 'year-1900' to get a double digit year)
1608 // [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.)
1609
1610 memcpy(&fixed_tm, ptm, sizeof(struct tm));
1611 if (fixed_tm.tm_year >= 1980) // range [1980, 2107]
1612 fixed_tm.tm_year -= 1980;
1613 else if (fixed_tm.tm_year >= 80) // range [80, 99]
1614 fixed_tm.tm_year -= 80;
1615 else // range [00, 79]
1616 fixed_tm.tm_year += 20;
1617
1618 if (mz_zip_invalid_date(ptm))
1619 return 0;
1620
1621 return (uint32_t)(((fixed_tm.tm_mday) + (32 * (fixed_tm.tm_mon + 1)) + (512 * fixed_tm.tm_year)) << 16) |
1622 ((fixed_tm.tm_sec / 2) + (32 * fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
1623}
1624
1625int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
1626{
1627 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
1628 return MZ_OK;
1629}
1630
1631int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
1632{
1633 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
1634 return MZ_OK;
1635}