blob: 337e969fb16108103337315865611dc84099dcb2 [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvazirie1aad8c2018-05-09 19:48:55 -07002 Version 2.3.1, May 9th, 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
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080033#ifdef HAVE_LZMA
34# include "mz_strm_lzma.h"
35#endif
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -070036#ifdef HAVE_PKCRYPT
37# include "mz_strm_pkcrypt.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)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080057
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070058#define MZ_ZIP_EXTENSION_ZIP64 (0x0001)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -070059#define MZ_ZIP_EXTENSION_NTFS (0x000a)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070060#define MZ_ZIP_EXTENSION_AES (0x9901)
61
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080062/***************************************************************************/
63
64typedef struct mz_zip_s
65{
66 mz_zip_file file_info;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070067 mz_zip_file local_file_info;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080068
69 void *stream; // main stream
Nathan Moinvaziricda36002017-10-21 09:37:18 -070070 void *cd_stream; // pointer to the stream with the cd
71 void *cd_mem_stream; // memory stream for central directory
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080072 void *compress_stream; // compression stream
73 void *crc32_stream; // crc32 stream
74 void *crypt_stream; // encryption stream
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070075 void *file_info_stream; // memory stream for storing file info
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -070076 void *local_file_info_stream; // memory stream for storing local file info
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080077
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070078 int32_t open_mode;
79
Nathan Moinvazirifd039e32017-10-22 14:40:39 -070080 uint32_t disk_number_with_cd; // number of the disk with the central dir
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070081
Nathan Moinvaziricda36002017-10-21 09:37:18 -070082 uint64_t cd_start_pos; // pos of the first file in the central dir stream
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070083 uint64_t cd_current_pos; // pos of the current file in the central dir
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070084 uint64_t cd_offset; // offset of start of central directory
85 uint64_t cd_size; // size of the central directory
86
Viktor Szakats915b82e2018-04-24 10:02:39 +000087 uint16_t entry_scanned;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070088 uint16_t entry_opened; // 1 if a file in the zip is currently writ.
89 uint64_t entry_read;
90
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070091 int64_t number_entry;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070092
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -070093 int16_t compression_method;
94
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -070095 uint16_t version_madeby;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070096 char *comment;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080097} mz_zip;
98
99/***************************************************************************/
100
101// Locate the central directory of a zip file (at the end, just before the global comment)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700102static int32_t mz_zip_search_eocd(void *stream, uint64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800103{
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700104 uint8_t buf[1024 + 4];
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700105 int64_t file_size = 0;
106 int64_t back_read = 0;
107 int64_t max_back = UINT16_MAX; // maximum size of global comment
108 int32_t read_size = sizeof(buf);
109 int64_t read_pos = 0;
110 int32_t i = 0;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700111
Nathan Moinvazirif6e81cd2017-10-10 18:24:03 -0700112 *central_pos = 0;
113
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700114 if (mz_stream_seek(stream, 0, MZ_SEEK_END) != MZ_OK)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700115 return MZ_STREAM_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800116
117 file_size = mz_stream_tell(stream);
118
119 if (max_back > file_size)
120 max_back = file_size;
121
122 while (back_read < max_back)
123 {
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700124 back_read += (sizeof(buf) - 4);
125 if (back_read > max_back)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800126 back_read = max_back;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800127
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700128 read_pos = file_size - back_read;
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700129 if (read_size > (file_size - read_pos))
130 read_size = (uint32_t)(file_size - read_pos);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800131
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700132 if (mz_stream_seek(stream, read_pos, MZ_SEEK_SET) != MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800133 break;
134 if (mz_stream_read(stream, buf, read_size) != read_size)
135 break;
136
137 for (i = read_size - 3; (i--) > 0;)
138 {
Nathan Moinvaziri9ab31ba2017-10-16 07:51:09 -0700139 if (((*(buf + i)) == (MZ_ZIP_MAGIC_ENDHEADER & 0xff)) &&
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700140 ((*(buf + i + 1)) == (MZ_ZIP_MAGIC_ENDHEADER >> 8 & 0xff)) &&
141 ((*(buf + i + 2)) == (MZ_ZIP_MAGIC_ENDHEADER >> 16 & 0xff)) &&
142 ((*(buf + i + 3)) == (MZ_ZIP_MAGIC_ENDHEADER >> 24 & 0xff)))
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800143 {
Nathan Moinvazirif6e81cd2017-10-10 18:24:03 -0700144 *central_pos = read_pos + i;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700145 return MZ_OK;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800146 }
147 }
148
Nathan Moinvazirif6e81cd2017-10-10 18:24:03 -0700149 if (*central_pos != 0)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800150 break;
151 }
152
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700153 return MZ_EXIST_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800154}
155
156// Locate the central directory 64 of a zip file (at the end, just before the global comment)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700157static 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 +0800158{
159 uint64_t offset = 0;
160 uint32_t value32 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700161 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700162
163
164 *central_pos = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800165
166 // Zip64 end of central directory locator
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700167 err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800168 // Read locator signature
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700169 if (err == MZ_OK)
170 {
171 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700172 if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700173 err = MZ_FORMAT_ERROR;
174 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800175 // Number of the disk with the start of the zip64 end of central directory
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700176 if (err == MZ_OK)
177 err = mz_stream_read_uint32(stream, &value32);
178 // Relative offset of the zip64 end of central directory record8
179 if (err == MZ_OK)
180 err = mz_stream_read_uint64(stream, &offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800181 // Total number of disks
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700182 if (err == MZ_OK)
183 err = mz_stream_read_uint32(stream, &value32);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800184 // Goto end of central directory record
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700185 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700186 err = mz_stream_seek(stream, offset, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800187 // The signature
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700188 if (err == MZ_OK)
189 {
190 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700191 if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700192 err = MZ_FORMAT_ERROR;
193 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800194
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700195 if (err == MZ_OK)
196 *central_pos = offset;
197
198 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800199}
200
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700201static int32_t mz_zip_read_cd(void *handle)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800202{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700203 mz_zip *zip = (mz_zip *)handle;
204 int64_t number_entry_cd = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700205 uint64_t number_entry_cd64 = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800206 uint64_t number_entry = 0;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700207 uint64_t eocd_pos = 0;
208 uint64_t eocd_pos64 = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800209 uint16_t value16 = 0;
210 uint32_t value32 = 0;
211 uint64_t value64 = 0;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700212 uint16_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700213 int32_t err = MZ_OK;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800214
215
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800216 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700217 return MZ_PARAM_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800218
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700219 // Read and cache central directory records
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700220 if (mz_zip_search_eocd(zip->stream, &eocd_pos) == MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800221 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700222 // Read end of central directory info
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700223 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700224 // The signature, already checked
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800225 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700226 err = mz_stream_read_uint32(zip->stream, &value32);
227 // Number of this disk
228 if (err == MZ_OK)
229 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700230 // Number of the disk with the start of the central directory
231 if (err == MZ_OK)
232 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700233 zip->disk_number_with_cd = value16;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700234 // Total number of entries in the central dir on this disk
235 if (err == MZ_OK)
236 err = mz_stream_read_uint16(zip->stream, &value16);
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700237 zip->number_entry = value16;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700238 // Total number of entries in the central dir
239 if (err == MZ_OK)
240 err = mz_stream_read_uint16(zip->stream, &value16);
241 number_entry_cd = value16;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700242 if (number_entry_cd != zip->number_entry)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700243 err = MZ_FORMAT_ERROR;
244 // Size of the central directory
245 if (err == MZ_OK)
246 err = mz_stream_read_uint32(zip->stream, &value32);
247 if (err == MZ_OK)
248 zip->cd_size = value32;
249 // Offset of start of central directory with respect to the starting disk number
250 if (err == MZ_OK)
251 err = mz_stream_read_uint32(zip->stream, &value32);
252 zip->cd_offset = value32;
253 // Zip file global comment length
254 if (err == MZ_OK)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700255 err = mz_stream_read_uint16(zip->stream, &comment_size);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800256
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700257 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800258 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700259 // Format should be Zip64, as the central directory or file size is too large
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700260 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800261 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700262 eocd_pos = eocd_pos64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700263
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700264 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700265 // The signature, already checked
266 if (err == MZ_OK)
267 err = mz_stream_read_uint32(zip->stream, &value32);
268 // Size of zip64 end of central directory record
269 if (err == MZ_OK)
270 err = mz_stream_read_uint64(zip->stream, &value64);
271 // Version made by
272 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700273 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700274 // Version needed to extract
275 if (err == MZ_OK)
276 err = mz_stream_read_uint16(zip->stream, &value16);
277 // Number of this disk
278 if (err == MZ_OK)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -0700279 err = mz_stream_read_uint32(zip->stream, &value32);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700280 // Number of the disk with the start of the central directory
281 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700282 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700283 // Total number of entries in the central directory on this disk
284 if (err == MZ_OK)
285 err = mz_stream_read_uint64(zip->stream, &number_entry);
286 // Total number of entries in the central directory
287 if (err == MZ_OK)
288 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
289 if (number_entry == UINT32_MAX)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700290 zip->number_entry = number_entry_cd64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700291 // Size of the central directory
292 if (err == MZ_OK)
293 err = mz_stream_read_uint64(zip->stream, &zip->cd_size);
294 // Offset of start of central directory with respect to the starting disk number
295 if (err == MZ_OK)
296 err = mz_stream_read_uint64(zip->stream, &zip->cd_offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800297 }
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700298 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700299 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700300 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700301 err = MZ_FORMAT_ERROR;
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700302 }
303 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800304 }
305
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700306 if (err == MZ_OK)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800307 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700308 if (eocd_pos < zip->cd_offset + zip->cd_size)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700309 err = MZ_FORMAT_ERROR;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800310 }
311
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700312 if ((err == MZ_OK) && (comment_size > 0))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700313 {
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700314 zip->comment = (char *)MZ_ALLOC(comment_size + 1);
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700315 if (zip->comment)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700316 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700317 if (mz_stream_read(zip->stream, zip->comment, comment_size) != comment_size)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700318 err = MZ_STREAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700319 zip->comment[comment_size] = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700320 }
321 }
322
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800323 return err;
324}
325
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700326static int32_t mz_zip_write_cd(void *handle)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800327{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700328 mz_zip *zip = (mz_zip *)handle;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800329 uint16_t comment_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700330 uint64_t zip64_eocd_pos_inzip = 0;
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700331 int64_t disk_number = 0;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -0700332 int64_t disk_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700333 int32_t err = MZ_OK;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800334
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700335
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700336 if (zip == NULL)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800337 return MZ_PARAM_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000338
Nathan Moinvaziri016ad472017-10-09 23:36:30 -0700339 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700340 zip->disk_number_with_cd = (uint32_t)disk_number;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -0700341 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 -0700342 zip->disk_number_with_cd += 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700343 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800344
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700345 zip->cd_offset = mz_stream_tell(zip->stream);
346 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700347 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700348 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
Viktor Szakats915b82e2018-04-24 10:02:39 +0000349
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700350 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800351
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800352 // Write the ZIP64 central directory header
juanii3f59ffc2018-02-08 12:44:17 -0300353 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800354 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700355 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800356
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700357 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800358
359 // Size of this 'zip64 end of central directory'
360 if (err == MZ_OK)
361 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
362 // Version made by
363 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700364 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800365 // Version needed
366 if (err == MZ_OK)
367 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
368 // Number of this disk
369 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700370 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800371 // Number of the disk with the start of the central directory
372 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700373 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800374 // Total number of entries in the central dir on this disk
375 if (err == MZ_OK)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700376 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800377 // Total number of entries in the central dir
378 if (err == MZ_OK)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700379 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800380 // Size of the central directory
381 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700382 err = mz_stream_write_uint64(zip->stream, (uint64_t)zip->cd_size);
383 // Offset of start of central directory with respect to the starting disk number
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800384 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700385 err = mz_stream_write_uint64(zip->stream, zip->cd_offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800386 if (err == MZ_OK)
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700387 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700388
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800389 // Number of the disk with the start of the central directory
390 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700391 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800392 // Relative offset to the end of zip64 central directory
393 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700394 err = mz_stream_write_uint64(zip->stream, zip64_eocd_pos_inzip);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800395 // Number of the disk with the start of the central directory
396 if (err == MZ_OK)
juaniic65486d2018-02-08 17:07:07 -0300397 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800398 }
399
400 // Write the central directory header
401
Viktor Szakats915b82e2018-04-24 10:02:39 +0000402 // Signature
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800403 if (err == MZ_OK)
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700404 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800405 // Number of this disk
406 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700407 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800408 // Number of the disk with the start of the central directory
409 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700410 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800411 // Total number of entries in the central dir on this disk
412 if (err == MZ_OK)
413 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700414 if (zip->number_entry >= UINT16_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800415 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
416 else
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700417 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800418 }
419 // Total number of entries in the central dir
420 if (err == MZ_OK)
421 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700422 if (zip->number_entry >= UINT16_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800423 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
424 else
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700425 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800426 }
427 // Size of the central directory
428 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700429 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800430 // Offset of start of central directory with respect to the starting disk number
431 if (err == MZ_OK)
432 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700433 if (zip->cd_offset >= UINT32_MAX)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800434 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
435 else
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700436 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800437 }
438
439 // Write global comment
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700440 if (zip->comment != NULL)
441 comment_size = (uint16_t)strlen(zip->comment);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800442 if (err == MZ_OK)
443 err = mz_stream_write_uint16(zip->stream, comment_size);
444 if (err == MZ_OK)
445 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700446 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800447 err = MZ_STREAM_ERROR;
448 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700449 return err;
450}
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800451
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800452extern void* mz_zip_open(void *stream, int32_t mode)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700453{
454 mz_zip *zip = NULL;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700455 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700456
457
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700458 zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700459 if (zip == NULL)
460 return NULL;
461
462 memset(zip, 0, sizeof(mz_zip));
463
464 zip->stream = stream;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700465
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800466 if (mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700467 {
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700468 mz_stream_mem_create(&zip->cd_mem_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700469 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700470
471 zip->cd_stream = zip->cd_mem_stream;
472 }
473 else
474 {
475 zip->cd_stream = stream;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700476 }
477
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800478 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700479 {
480 err = mz_zip_read_cd(zip);
481
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700482 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700483 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700484 if (zip->cd_size > 0)
485 {
486 // Store central directory in memory
487 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
488 if (err == MZ_OK)
489 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (uint32_t)zip->cd_size);
490 if (err == MZ_OK)
491 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
492 }
493 else
494 {
495 // If no central directory, append new zip to end of file
496 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
497 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700498 }
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700499 else
500 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700501 zip->cd_start_pos = zip->cd_offset;
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700502 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700503 }
504
505 if (err == MZ_OK)
506 {
507 mz_stream_mem_create(&zip->file_info_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700508 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700509 mz_stream_mem_create(&zip->local_file_info_stream);
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700510 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700511 }
512
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700513 if (err != MZ_OK)
514 {
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800515 mz_zip_close(zip);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700516 return NULL;
517 }
518
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800519 zip->open_mode = mode;
520
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700521 return zip;
522}
523
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800524extern int32_t mz_zip_close(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700525{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700526 mz_zip *zip = (mz_zip *)handle;
527 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700528
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700529 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700530 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700531
532 if (zip->entry_opened == 1)
533 {
534 err = mz_zip_entry_close(handle);
535 if (err != MZ_OK)
536 return err;
537 }
538
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700539 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700540 err = mz_zip_write_cd(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700541
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700542 if (zip->cd_mem_stream != NULL)
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700543 {
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700544 mz_stream_close(zip->cd_mem_stream);
545 mz_stream_delete(&zip->cd_mem_stream);
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -0700546 }
547
Nathan Moinvaziri3f7de8e2017-12-11 21:09:52 -0800548 if (zip->file_info_stream != NULL)
549 {
550 mz_stream_mem_close(zip->file_info_stream);
551 mz_stream_mem_delete(&zip->file_info_stream);
552 }
553 if (zip->local_file_info_stream != NULL)
554 {
555 mz_stream_mem_close(zip->local_file_info_stream);
556 mz_stream_mem_delete(&zip->local_file_info_stream);
557 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700558
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700559 if (zip->comment)
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700560 MZ_FREE(zip->comment);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700561
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700562 MZ_FREE(zip);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800563
564 return err;
565}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700566
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800567extern int32_t mz_zip_get_comment(void *handle, const char **comment)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700568{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700569 mz_zip *zip = (mz_zip *)handle;
570 if (zip == NULL || comment == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700571 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700572 if (zip->comment == NULL)
573 return MZ_EXIST_ERROR;
574 *comment = zip->comment;
575 return MZ_OK;
576}
577
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800578extern int32_t mz_zip_set_comment(void *handle, const char *comment)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700579{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700580 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700581 uint16_t comment_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700582 if (zip == NULL || comment == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700583 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700584 if (zip->comment != NULL)
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700585 MZ_FREE(zip->comment);
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700586 comment_size = (uint16_t)(strlen(comment) + 1);
Nathan Moinvaziri5244fc02018-05-02 20:01:35 -0700587 zip->comment = (char *)MZ_ALLOC(comment_size);
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700588 strncpy(zip->comment, comment, comment_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700589 return MZ_OK;
590}
591
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800592extern int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700593{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700594 mz_zip *zip = (mz_zip *)handle;
595 if (zip == NULL || version_madeby == NULL)
596 return MZ_PARAM_ERROR;
597 *version_madeby = zip->version_madeby;
598 return MZ_OK;
599}
600
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -0800601extern int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700602{
603 mz_zip *zip = (mz_zip *)handle;
604 if (zip == NULL)
605 return MZ_PARAM_ERROR;
606 zip->version_madeby = version_madeby;
607 return MZ_OK;
608}
609
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700610// Get info about the current file in the zip file
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700611static 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 -0700612{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700613 uint64_t ntfs_time = 0;
614 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700615 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700616 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700617 uint32_t extra_pos = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700618 uint32_t extra_data_size_read = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700619 uint16_t extra_header_id = 0;
620 uint16_t extra_data_size = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700621 uint16_t ntfs_attrib_id = 0;
622 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700623 uint16_t value16 = 0;
624 uint32_t value32 = 0;
625 uint64_t value64 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700626 int64_t max_seek = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700627 int64_t seek = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700628 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700629
630
631 memset(file_info, 0, sizeof(mz_zip_file));
632
633 // Check the magic
634 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700635 if (err == MZ_END_OF_STREAM)
636 err = MZ_END_OF_LIST;
637 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700638 err = MZ_END_OF_LIST;
639 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
640 err = MZ_FORMAT_ERROR;
641 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
642 err = MZ_FORMAT_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000643
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700644 // Read header fields
645 if (err == MZ_OK)
646 {
647 if (!local)
648 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
649 if (err == MZ_OK)
650 err = mz_stream_read_uint16(stream, &file_info->version_needed);
651 if (err == MZ_OK)
652 err = mz_stream_read_uint16(stream, &file_info->flag);
653 if (err == MZ_OK)
654 err = mz_stream_read_uint16(stream, &file_info->compression_method);
655 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700656 {
657 err = mz_stream_read_uint32(stream, &dos_date);
658 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
659 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700660 if (err == MZ_OK)
661 err = mz_stream_read_uint32(stream, &file_info->crc);
662 if (err == MZ_OK)
663 err = mz_stream_read_uint32(stream, &value32);
664 file_info->compressed_size = value32;
665 if (err == MZ_OK)
666 err = mz_stream_read_uint32(stream, &value32);
667 file_info->uncompressed_size = value32;
668 if (err == MZ_OK)
669 err = mz_stream_read_uint16(stream, &file_info->filename_size);
670 if (err == MZ_OK)
671 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
672 if (!local)
673 {
674 if (err == MZ_OK)
675 err = mz_stream_read_uint16(stream, &file_info->comment_size);
676 if (err == MZ_OK)
677 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700678 file_info->disk_number = value16;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700679 if (err == MZ_OK)
680 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
681 if (err == MZ_OK)
682 err = mz_stream_read_uint32(stream, &file_info->external_fa);
683 if (err == MZ_OK)
684 err = mz_stream_read_uint32(stream, &value32);
685 file_info->disk_offset = value32;
686 }
687 }
688
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700689 max_seek = file_info->filename_size + file_info->extrafield_size + file_info->comment_size + 3;
690 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700691 err = mz_stream_seek(file_info_stream, max_seek, MZ_SEEK_SET);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700692 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700693 err = mz_stream_seek(file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700694
695 if ((err == MZ_OK) && (file_info->filename_size > 0))
696 {
Nathan Moinvaziria710bd72018-05-09 00:46:47 -0700697 mz_stream_mem_get_buffer(file_info_stream, (const void **)&file_info->filename);
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700698
699 err = mz_stream_copy(file_info_stream, stream, file_info->filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700700 if (err == MZ_OK)
701 err = mz_stream_write_uint8(file_info_stream, 0);
702
703 seek += file_info->filename_size + 1;
704 }
705
706 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
707 {
Nathan Moinvaziria710bd72018-05-09 00:46:47 -0700708 mz_stream_mem_get_buffer_at(file_info_stream, seek, (const void **)&file_info->extrafield);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700709
710 err = mz_stream_copy(file_info_stream, stream, file_info->extrafield_size);
711 if (err == MZ_OK)
712 err = mz_stream_write_uint8(file_info_stream, 0);
713
714 // Seek back and parse the extra field
715 if (err == MZ_OK)
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700716 err = mz_stream_seek(file_info_stream, seek, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700717
718 seek += file_info->extrafield_size + 1;
719
720 while ((err == MZ_OK) && (extra_pos < file_info->extrafield_size))
721 {
722 err = mz_stream_read_uint16(file_info_stream, &extra_header_id);
723 if (err == MZ_OK)
724 err = mz_stream_read_uint16(file_info_stream, &extra_data_size);
725
726 // ZIP64 extra field
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700727 if (extra_header_id == MZ_ZIP_EXTENSION_ZIP64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700728 {
729 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
730 err = mz_stream_read_uint64(file_info_stream, &file_info->uncompressed_size);
731 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
732 err = mz_stream_read_uint64(file_info_stream, &file_info->compressed_size);
733 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
734 err = mz_stream_read_uint64(file_info_stream, &value64);
735 file_info->disk_offset = value64;
juanii4ae79922018-02-11 14:29:36 -0300736 if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700737 err = mz_stream_read_uint32(file_info_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700738 }
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700739 // NTFS extra field
740 else if (extra_header_id == MZ_ZIP_EXTENSION_NTFS)
741 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700742 if (err == MZ_OK)
743 err = mz_stream_read_uint32(file_info_stream, &reserved);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700744 extra_data_size_read = 4;
745
746 while ((err == MZ_OK) && (extra_data_size_read < extra_data_size))
747 {
748 err = mz_stream_read_uint16(file_info_stream, &ntfs_attrib_id);
749 if (err == MZ_OK)
750 err = mz_stream_read_uint16(file_info_stream, &ntfs_attrib_size);
751
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700752 if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700753 {
754 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
755 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
756
juanii7063b0e2018-02-11 13:56:21 -0300757 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700758 {
759 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
760 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
761 }
juanii7063b0e2018-02-11 13:56:21 -0300762 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700763 {
764 err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
765 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
766 }
767 }
768 else
769 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700770 if (err == MZ_OK)
771 err = mz_stream_seek(file_info_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700772 }
773
774 extra_data_size_read += ntfs_attrib_size + 4;
775 }
776 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700777#ifdef HAVE_AES
778 // AES extra field
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700779 else if (extra_header_id == MZ_ZIP_EXTENSION_AES)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700780 {
781 uint8_t value8 = 0;
782 // Verify version info
783 err = mz_stream_read_uint16(file_info_stream, &value16);
784 // Support AE-1 and AE-2
785 if (value16 != 1 && value16 != 2)
786 err = MZ_FORMAT_ERROR;
787 file_info->aes_version = value16;
788 if (err == MZ_OK)
789 err = mz_stream_read_uint8(file_info_stream, &value8);
790 if ((char)value8 != 'A')
791 err = MZ_FORMAT_ERROR;
792 if (err == MZ_OK)
793 err = mz_stream_read_uint8(file_info_stream, &value8);
794 if ((char)value8 != 'E')
795 err = MZ_FORMAT_ERROR;
796 // Get AES encryption strength and actual compression method
797 if (err == MZ_OK)
798 err = mz_stream_read_uint8(file_info_stream, &value8);
799 file_info->aes_encryption_mode = value8;
800 if (err == MZ_OK)
801 err = mz_stream_read_uint16(file_info_stream, &value16);
802 file_info->compression_method = value16;
803 }
804#endif
805 else
806 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700807 err = mz_stream_seek(file_info_stream, extra_data_size, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700808 }
809
810 extra_pos += 4 + extra_data_size;
811 }
812 }
813
814 if ((err == MZ_OK) && (file_info->comment_size > 0))
815 {
Nathan Moinvaziria710bd72018-05-09 00:46:47 -0700816 mz_stream_mem_get_buffer_at(file_info_stream, seek, (const void **)&file_info->comment);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700817
818 err = mz_stream_copy(file_info_stream, stream, file_info->comment_size);
819 if (err == MZ_OK)
820 err = mz_stream_write_uint8(file_info_stream, 0);
821 }
822
823 return err;
824}
825
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700826static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700827{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700828 uint64_t ntfs_time = 0;
829 uint32_t reserved = 0;
830 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700831 uint16_t extrafield_size = 0;
832 uint16_t extrafield_zip64_size = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700833 uint16_t extrafield_ntfs_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700834 uint16_t filename_size = 0;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700835 uint16_t filename_length = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700836 uint16_t comment_size = 0;
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700837 uint16_t version_needed = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700838 uint8_t zip64 = 0;
839 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700840
841 if (file_info == NULL)
842 return MZ_PARAM_ERROR;
843
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700844 // Calculate extra field sizes
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700845 extrafield_size = file_info->extrafield_size;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700846
847 if (file_info->uncompressed_size >= UINT32_MAX)
848 extrafield_zip64_size += 8;
849 if (file_info->compressed_size >= UINT32_MAX)
850 extrafield_zip64_size += 8;
851 if (file_info->disk_offset >= UINT32_MAX)
852 extrafield_zip64_size += 8;
853
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700854 if (file_info->zip64 == MZ_ZIP64_AUTO)
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700855 {
856 // If uncompressed size is unknown, assume zip64 for 64-bit data descriptors
857 zip64 = (local && file_info->uncompressed_size == 0) || (extrafield_zip64_size > 0);
858 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700859 else if (file_info->zip64 == MZ_ZIP64_FORCE)
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700860 zip64 = 1;
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700861 else if (file_info->zip64 == MZ_ZIP64_DISABLE)
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700862 {
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700863 // Zip64 extension is required to zip file
864 if (extrafield_zip64_size > 0)
865 return MZ_PARAM_ERROR;
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700866 }
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700867
868 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700869 {
870 extrafield_size += 4;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700871 extrafield_size += extrafield_zip64_size;
872 }
873#ifdef HAVE_AES
874 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
875 extrafield_size += 4 + 7;
876#endif
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700877 // NTFS timestamps
Nathan Moinvazirid9e159a2018-02-13 15:30:30 -0800878 if ((file_info->modified_date != 0) &&
879 (file_info->accessed_date != 0) &&
880 (file_info->creation_date != 0))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700881 {
juanii3679a3d2018-02-11 13:55:38 -0300882 extrafield_ntfs_size += 8 + 8 + 8 + 4 + 2 + 2;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700883 extrafield_size += 4;
884 extrafield_size += extrafield_ntfs_size;
885 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700886
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700887 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700888 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700889 else
890 {
891 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
892 if (err == MZ_OK)
893 err = mz_stream_write_uint16(stream, file_info->version_madeby);
894 }
895
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700896 // Calculate version needed to extract
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700897 if (err == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700898 {
899 version_needed = file_info->version_needed;
900 if (version_needed == 0)
901 {
902 version_needed = 20;
903 if (zip64)
904 version_needed = 45;
905#ifdef HAVE_AES
906 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
907 version_needed = 51;
908#endif
909#ifdef HAVE_LZMA
910 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
911 version_needed = 63;
912#endif
913 }
914 err = mz_stream_write_uint16(stream, version_needed);
915 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700916 if (err == MZ_OK)
917 err = mz_stream_write_uint16(stream, file_info->flag);
918 if (err == MZ_OK)
919 {
920#ifdef HAVE_AES
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700921 if (file_info->aes_version)
922 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700923 else
924#endif
925 err = mz_stream_write_uint16(stream, file_info->compression_method);
926 }
927 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700928 {
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -0700929 if (file_info->modified_date != 0)
930 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700931 err = mz_stream_write_uint32(stream, dos_date);
932 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700933
934 if (err == MZ_OK)
935 err = mz_stream_write_uint32(stream, file_info->crc); // crc
936 if (err == MZ_OK)
937 {
938 if (file_info->compressed_size >= UINT32_MAX) // compr size
939 err = mz_stream_write_uint32(stream, UINT32_MAX);
940 else
941 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
942 }
943 if (err == MZ_OK)
944 {
945 if (file_info->uncompressed_size >= UINT32_MAX) // uncompr size
946 err = mz_stream_write_uint32(stream, UINT32_MAX);
947 else
948 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
949 }
950
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700951 filename_length = (uint16_t)strlen(file_info->filename);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700952 if (err == MZ_OK)
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700953 {
954 filename_size = filename_length;
955 if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700956 {
957 if ((file_info->filename[filename_length - 1] == '/') ||
958 (file_info->filename[filename_length - 1] == '\\'))
959 filename_length -= 1;
960 else
961 filename_size += 1;
962 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700963 err = mz_stream_write_uint16(stream, filename_size);
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700964 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700965 if (err == MZ_OK)
966 err = mz_stream_write_uint16(stream, extrafield_size);
967
968 if (!local)
969 {
970 if (file_info->comment != NULL)
971 comment_size = (uint16_t)strlen(file_info->comment);
972 if (err == MZ_OK)
973 err = mz_stream_write_uint16(stream, comment_size);
974 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700975 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700976 if (err == MZ_OK)
977 err = mz_stream_write_uint16(stream, file_info->internal_fa);
978 if (err == MZ_OK)
979 err = mz_stream_write_uint32(stream, file_info->external_fa);
980 if (err == MZ_OK)
981 {
982 if (file_info->disk_offset >= UINT32_MAX)
983 err = mz_stream_write_uint32(stream, UINT32_MAX);
984 else
985 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
986 }
987 }
988
989 if (err == MZ_OK)
990 {
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700991 if (mz_stream_write(stream, file_info->filename, filename_length) != filename_length)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700992 err = MZ_STREAM_ERROR;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700993 if (err == MZ_OK)
994 {
Nathan Moinvaziri240b3b62018-05-02 13:38:14 -0700995 // Ensure that directories have a slash appended to them for compatibility
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700996 if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK)
997 err = mz_stream_write_uint8(stream, '/');
998 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700999 }
1000 if (err == MZ_OK)
1001 {
1002 if (mz_stream_write(stream, file_info->extrafield, file_info->extrafield_size) != file_info->extrafield_size)
1003 err = MZ_STREAM_ERROR;
1004 }
1005 // Add ZIP64 extra info header to central directory
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001006 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001007 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001008 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_ZIP64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001009 if (err == MZ_OK)
1010 err = mz_stream_write_uint16(stream, extrafield_zip64_size);
1011 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
1012 err = mz_stream_write_uint64(stream, file_info->uncompressed_size);
1013 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
1014 err = mz_stream_write_uint64(stream, file_info->compressed_size);
1015 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
1016 err = mz_stream_write_uint64(stream, file_info->disk_offset);
1017 }
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001018 // Write NTFS timestamps
1019 if ((err == MZ_OK) && (extrafield_ntfs_size > 0))
1020 {
1021 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_NTFS);
1022 if (err == MZ_OK)
1023 err = mz_stream_write_uint16(stream, extrafield_ntfs_size);
1024 if (err == MZ_OK)
1025 err = mz_stream_write_uint32(stream, reserved);
1026 if (err == MZ_OK)
1027 err = mz_stream_write_uint16(stream, 0x01);
1028 if (err == MZ_OK)
1029 err = mz_stream_write_uint16(stream, extrafield_ntfs_size - 8);
juanii3679a3d2018-02-11 13:55:38 -03001030 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001031 {
1032 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
1033 err = mz_stream_write_uint64(stream, ntfs_time);
1034 }
juanii3679a3d2018-02-11 13:55:38 -03001035 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001036 {
1037 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
1038 err = mz_stream_write_uint64(stream, ntfs_time);
1039 }
juanii3679a3d2018-02-11 13:55:38 -03001040 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001041 {
1042 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
1043 err = mz_stream_write_uint64(stream, ntfs_time);
1044 }
1045 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001046#ifdef HAVE_AES
1047 // Write AES extra info header to central directory
1048 if ((err == MZ_OK) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
1049 {
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001050 err = mz_stream_write_uint16(stream, MZ_ZIP_EXTENSION_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001051 if (err == MZ_OK)
1052 err = mz_stream_write_uint16(stream, 7);
1053 if (err == MZ_OK)
1054 err = mz_stream_write_uint16(stream, file_info->aes_version);
1055 if (err == MZ_OK)
1056 err = mz_stream_write_uint8(stream, 'A');
1057 if (err == MZ_OK)
1058 err = mz_stream_write_uint8(stream, 'E');
1059 if (err == MZ_OK)
1060 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
1061 if (err == MZ_OK)
1062 err = mz_stream_write_uint16(stream, file_info->compression_method);
1063 }
1064#endif
1065 if ((err == MZ_OK) && (file_info->comment != NULL))
1066 {
1067 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != MZ_OK)
1068 err = MZ_STREAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001069 }
1070
1071 return err;
1072}
1073
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001074static 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 -07001075{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001076 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001077 int64_t max_total_in = 0;
1078 int64_t total_in = 0;
1079 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001080 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001081
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001082 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001083 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001084
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001085 zip->compression_method = compression_method;
1086
1087 switch (zip->compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001088 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001089 case MZ_COMPRESS_METHOD_RAW:
1090 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001091#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001092 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001093#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001094#if HAVE_LZMA
1095 case MZ_COMPRESS_METHOD_LZMA:
1096#endif
1097 err = MZ_OK;
1098 break;
1099 default:
1100 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001101 }
1102
Nathan Moinvaziri0f09a002018-04-23 18:48:30 -07001103 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) && (zip->compression_method != MZ_COMPRESS_METHOD_RAW))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001104 return MZ_PARAM_ERROR;
1105
Nathan Moinvaziri0f09a002018-04-23 18:48:30 -07001106 if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (zip->compression_method != MZ_COMPRESS_METHOD_RAW))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001107 {
1108#ifdef HAVE_AES
1109 if (zip->file_info.aes_version)
1110 {
1111 mz_stream_aes_create(&zip->crypt_stream);
1112 mz_stream_aes_set_password(zip->crypt_stream, password);
1113 mz_stream_aes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001114 }
1115 else
1116#endif
1117 {
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001118#ifdef HAVE_PKCRYPT
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001119 uint8_t verify1 = 0;
1120 uint8_t verify2 = 0;
1121
1122 // Info-ZIP modification to ZipCrypto format:
1123 // If bit 3 of the general purpose bit flag is set, it uses high byte of 16-bit File Time.
1124
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001125 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1126 {
1127 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001128
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001129 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1130
1131 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1132 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1133 }
1134 else
1135 {
1136 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1137 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1138 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001139
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001140 mz_stream_pkcrypt_create(&zip->crypt_stream);
1141 mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1142 mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001143#endif
1144 }
1145 }
1146
1147 if (err == MZ_OK)
1148 {
1149 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001150 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001151
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001152 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001153
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001154 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001155 }
1156
1157 if (err == MZ_OK)
1158 {
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001159 if (zip->compression_method == MZ_COMPRESS_METHOD_RAW)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001160 mz_stream_raw_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001161#ifdef HAVE_ZLIB
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001162 else if (zip->compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001163 mz_stream_zlib_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001164#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001165#ifdef HAVE_BZIP2
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001166 else if (zip->compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001167 mz_stream_bzip_create(&zip->compress_stream);
1168#endif
1169#ifdef HAVE_LZMA
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001170 else if (zip->compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001171 mz_stream_lzma_create(&zip->compress_stream);
1172#endif
1173 else
1174 err = MZ_PARAM_ERROR;
1175 }
1176
1177 if (err == MZ_OK)
1178 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001179 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001180 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001181 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001182 }
1183 else
1184 {
Nathan Moinvaziri5e8f4d72018-03-15 10:43:18 -07001185 if (zip->compression_method == MZ_COMPRESS_METHOD_RAW || zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001186 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001187 max_total_in = zip->file_info.compressed_size;
1188 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1189 max_total_in -= footer_size;
1190 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in) == MZ_OK)
1191 max_total_in -= total_in;
1192 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001193 }
Nathan Moinvaziri5e8f4d72018-03-15 10:43:18 -07001194 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 -03001195 {
1196 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1197 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1198 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001199 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001200
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001201 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1202
1203 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1204 }
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001205 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001206 {
1207 mz_stream_crc32_create(&zip->crc32_stream);
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -08001208#ifdef HAVE_ZLIB
1209 mz_stream_crc32_set_update_func(zip->crc32_stream,
1210 (mz_stream_crc32_update)mz_stream_zlib_get_crc32_update());
1211#elif defined(HAVE_LZMA)
1212 mz_stream_crc32_set_update_func(zip->crc32_stream,
1213 (mz_stream_crc32_update)mz_stream_lzma_get_crc32_update());
1214#else
1215 #error ZLIB or LZMA required for CRC32
1216#endif
1217
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001218 mz_stream_set_base(zip->crc32_stream, zip->compress_stream);
1219
1220 err = mz_stream_open(zip->crc32_stream, NULL, zip->open_mode);
1221 }
1222
1223 if (err == MZ_OK)
1224 {
1225 zip->entry_opened = 1;
1226 }
1227
1228 return err;
1229}
1230
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001231extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001232{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001233 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001234 int16_t compression_method = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001235 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001236
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001237#if !defined(HAVE_PKCRYPT) && !defined(HAVE_AES)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001238 if (password != NULL)
1239 return MZ_PARAM_ERROR;
1240#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001241 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001242 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001243 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001244 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001245 if (zip->entry_scanned == 0)
1246 return MZ_PARAM_ERROR;
Nathan Moinvaziri0f09a002018-04-23 18:48:30 -07001247 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) && (!raw))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001248 return MZ_PARAM_ERROR;
1249
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001250 if (zip->file_info.disk_number == zip->disk_number_with_cd)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001251 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1252 else
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001253 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001254
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001255 err = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001256 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001257 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 -07001258
Nathan Moinvaziri51f72502017-10-26 10:15:21 -07001259 compression_method = zip->file_info.compression_method;
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001260 if (raw)
1261 compression_method = MZ_COMPRESS_METHOD_RAW;
1262
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001263 if (err == MZ_OK)
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001264 err = mz_zip_entry_open_int(handle, compression_method, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001265
1266 return err;
1267}
1268
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001269extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int16_t compress_level, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001270{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001271 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001272 int64_t disk_number = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001273 int32_t err = MZ_OK;
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001274 int16_t compression_method = 0;
1275
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001276
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001277#if !defined(HAVE_PKCRYPT) && !defined(HAVE_AES)
tz-lomb1b25802017-11-10 15:03:02 +03001278 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001279 return MZ_PARAM_ERROR;
1280#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001281 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001282 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001283
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001284 if (zip->entry_opened == 1)
1285 {
1286 err = mz_zip_entry_close(handle);
1287 if (err != MZ_OK)
1288 return err;
1289 }
1290
1291 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001292
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001293 compression_method = zip->file_info.compression_method;
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001294
1295 if (compression_method == MZ_COMPRESS_METHOD_DEFLATE)
1296 {
1297 if ((compress_level == 8) || (compress_level == 9))
1298 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
1299 if (compress_level == 2)
1300 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
1301 if (compress_level == 1)
1302 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1303 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001304#ifdef HAVE_LZMA
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001305 else if (compression_method == MZ_COMPRESS_METHOD_LZMA)
1306 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001307#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001308
1309 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001310
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001311 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001312 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
1313 else
1314 zip->file_info.flag &= ~MZ_ZIP_FLAG_ENCRYPTED;
1315
juaniib8887e92018-02-14 00:51:05 -03001316 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1317 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001318
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001319 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001320 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001321 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001322
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001323#ifdef HAVE_AES
1324 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
1325 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
1326#endif
1327
Nathan Moinvaziri4827f712018-05-02 10:50:47 -07001328 if ((compress_level == 0) || (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK))
Viktor Szakats2884e672018-04-30 08:12:13 +00001329 compression_method = MZ_COMPRESS_METHOD_RAW;
1330
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001331 if (err == MZ_OK)
1332 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
1333 if (err == MZ_OK)
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001334 err = mz_zip_entry_open_int(handle, compression_method, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001335
1336 return err;
1337}
1338
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001339extern int32_t mz_zip_entry_read(void *handle, void *buf, uint32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001340{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001341 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001342 int32_t read = 0;
1343
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001344 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001345 return MZ_PARAM_ERROR;
Nathan Moinvaziri930f9182018-01-22 08:51:07 -08001346 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) // Zlib limitation
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001347 return MZ_PARAM_ERROR;
Nathan Moinvaziria86a4352018-01-02 13:12:07 -08001348 if (len == 0 || zip->file_info.uncompressed_size == 0)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001349 return 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001350 read = mz_stream_read(zip->crc32_stream, buf, len);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001351 if (read > 0)
1352 zip->entry_read += read;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001353 return read;
1354}
1355
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001356extern int32_t mz_zip_entry_write(void *handle, const void *buf, uint32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001357{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001358 mz_zip *zip = (mz_zip *)handle;
1359 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001360 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001361 return mz_stream_write(zip->crc32_stream, buf, len);
1362}
1363
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001364extern int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001365{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001366 mz_zip *zip = (mz_zip *)handle;
1367 if (zip == NULL || zip->entry_scanned == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001368 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001369 *file_info = &zip->file_info;
1370 return MZ_OK;
1371}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001372
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001373extern int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001374{
1375 mz_zip *zip = (mz_zip *)handle;
1376 if (zip == NULL || zip->entry_scanned == 0 || zip->entry_opened == 0)
1377 return MZ_PARAM_ERROR;
1378 *local_file_info = &zip->local_file_info;
1379 return MZ_OK;
1380}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001381
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001382extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001383{
1384 mz_zip *zip = (mz_zip *)handle;
1385 uint64_t compressed_size = 0;
1386 int32_t err = MZ_OK;
1387
1388 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001389 return MZ_PARAM_ERROR;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001390
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001391 mz_stream_close(zip->compress_stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001392 if (crc32 == 0)
1393 crc32 = mz_stream_crc32_get_value(zip->crc32_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001394
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001395 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001396 {
1397#ifdef HAVE_AES
1398 // AES zip version AE-1 will expect a valid crc as well
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001399 if (zip->file_info.aes_version <= 0x0001)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001400#endif
1401 {
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001402 if ((zip->entry_read > 0) && (zip->compression_method != MZ_COMPRESS_METHOD_RAW))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001403 {
1404 if (crc32 != zip->file_info.crc)
1405 err = MZ_CRC_ERROR;
1406 }
1407 }
1408 }
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001409
1410 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001411 if ((zip->compression_method != MZ_COMPRESS_METHOD_RAW) || (uncompressed_size == 0))
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001412 mz_stream_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001413
1414 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
1415 {
1416 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001417 err = mz_stream_close(zip->crypt_stream);
1418
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001419 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001420 }
1421
1422 mz_stream_delete(&zip->crypt_stream);
1423
1424 mz_stream_delete(&zip->compress_stream);
1425 mz_stream_crc32_delete(&zip->crc32_stream);
1426
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001427 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001428 {
1429 if (err == MZ_OK)
1430 {
1431 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
1432 if (err == MZ_OK)
1433 err = mz_stream_write_uint32(zip->stream, crc32);
1434 if (err == MZ_OK)
1435 {
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001436 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001437 err = mz_stream_write_uint32(zip->stream, (uint32_t)compressed_size);
Andrew Gunnerson3c52d212018-01-05 22:28:12 -05001438 else
1439 err = mz_stream_write_uint64(zip->stream, compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001440 }
1441 if (err == MZ_OK)
1442 {
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001443 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001444 err = mz_stream_write_uint32(zip->stream, (uint32_t)uncompressed_size);
Andrew Gunnerson3c52d212018-01-05 22:28:12 -05001445 else
1446 err = mz_stream_write_uint64(zip->stream, uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001447 }
1448 }
1449
1450 zip->file_info.crc = crc32;
1451 zip->file_info.compressed_size = compressed_size;
1452 zip->file_info.uncompressed_size = uncompressed_size;
1453
1454 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001455 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001456
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001457 zip->number_entry += 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001458 }
1459
1460 zip->entry_opened = 0;
1461
1462 return err;
1463}
1464
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001465extern int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001466{
1467 return mz_zip_entry_close_raw(handle, 0, 0);
1468}
1469
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001470static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001471{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001472 mz_zip *zip = (mz_zip *)handle;
1473 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001474
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001475 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001476 return MZ_PARAM_ERROR;
1477
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001478 zip->entry_scanned = 0;
1479
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001480 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00001481
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001482 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001483 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001484 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001485 if (err == MZ_OK)
1486 zip->entry_scanned = 1;
1487 return err;
1488}
1489
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001490extern int32_t mz_zip_get_number_entry(void *handle, int64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001491{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001492 mz_zip *zip = (mz_zip *)handle;
1493 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001494 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001495 *number_entry = zip->number_entry;
1496 return MZ_OK;
1497}
1498
Viktor Szakats0f0535f2018-05-02 13:15:58 +00001499extern int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03001500{
1501 mz_zip *zip = (mz_zip *)handle;
1502 if (zip == NULL || disk_number_with_cd == NULL)
1503 return MZ_PARAM_ERROR;
1504 *disk_number_with_cd = zip->disk_number_with_cd;
1505 return MZ_OK;
1506}
1507
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001508extern int64_t mz_zip_get_entry(void *handle)
1509{
1510 mz_zip *zip = (mz_zip *)handle;
1511
1512 if (zip == NULL)
1513 return MZ_PARAM_ERROR;
1514
1515 return zip->cd_current_pos;
1516}
1517
1518extern int32_t mz_zip_goto_entry(void *handle, uint64_t cd_pos)
1519{
1520 mz_zip *zip = (mz_zip *)handle;
1521
1522 if (zip == NULL)
1523 return MZ_PARAM_ERROR;
1524
1525 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
1526 return MZ_PARAM_ERROR;
1527
1528 zip->cd_current_pos = cd_pos;
1529
1530 return mz_zip_goto_next_entry_int(handle);
1531}
1532
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001533extern int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001534{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001535 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001536
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001537 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001538 return MZ_PARAM_ERROR;
1539
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001540 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001541
1542 return mz_zip_goto_next_entry_int(handle);
1543}
1544
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001545extern int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001546{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001547 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001548
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001549 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001550 return MZ_PARAM_ERROR;
1551
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001552 zip->cd_current_pos += MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
1553 zip->file_info.extrafield_size + zip->file_info.comment_size;
1554
1555 return mz_zip_goto_next_entry_int(handle);
1556}
1557
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001558extern 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 -07001559{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001560 mz_zip *zip = (mz_zip *)handle;
1561 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001562 int32_t result = 0;
1563
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001564 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001565 return MZ_PARAM_ERROR;
1566
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001567 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001568 while (err == MZ_OK)
1569 {
1570 if (filename_compare_cb != NULL)
1571 result = filename_compare_cb(handle, zip->file_info.filename, filename);
1572 else
1573 result = strcmp(zip->file_info.filename, filename);
1574
1575 if (result == 0)
1576 return MZ_OK;
1577
1578 err = mz_zip_goto_next_entry(handle);
1579 }
1580
1581 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001582}
1583
1584/***************************************************************************/
1585
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07001586int32_t mz_zip_attrib_is_dir(int32_t attributes, int32_t version_madeby)
1587{
1588 int32_t host_system = (uint8_t)(version_madeby >> 8);
1589
1590 if (host_system == MZ_HOST_SYSTEM_MSDOS || host_system == MZ_HOST_SYSTEM_WINDOWS_NTFS)
1591 {
1592 if ((attributes & 0x10) == 0x10) // FILE_ATTRIBUTE_DIRECTORY
1593 return MZ_OK;
1594 }
1595 else if (host_system == MZ_HOST_SYSTEM_UNIX || host_system == MZ_HOST_SYSTEM_OSX_DARWIN)
1596 {
1597 if ((attributes & 00170000) == 0040000) // S_ISDIR
1598 return MZ_OK;
1599 }
1600
1601 return MZ_EXIST_ERROR;
1602}
1603
1604/***************************************************************************/
1605
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001606static int32_t mz_zip_invalid_date(const struct tm *ptm)
1607{
1608#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001609 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || // 1980-based year, allow 80 extra
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001610 !datevalue_in_range(0, 11, ptm->tm_mon) ||
1611 !datevalue_in_range(1, 31, ptm->tm_mday) ||
1612 !datevalue_in_range(0, 23, ptm->tm_hour) ||
1613 !datevalue_in_range(0, 59, ptm->tm_min) ||
1614 !datevalue_in_range(0, 59, ptm->tm_sec));
1615#undef datevalue_in_range
1616}
1617
1618static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
1619{
1620 uint64_t date = (uint64_t)(dos_date >> 16);
1621
1622 ptm->tm_mday = (uint16_t)(date & 0x1f);
1623 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
1624 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
1625 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
1626 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
1627 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
1628 ptm->tm_isdst = -1;
1629}
1630
1631int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
1632{
1633 if (ptm == NULL)
1634 return MZ_PARAM_ERROR;
1635
1636 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
1637
1638 if (mz_zip_invalid_date(ptm))
1639 {
1640 // Invalid date stored, so don't return it
1641 memset(ptm, 0, sizeof(struct tm));
1642 return MZ_FORMAT_ERROR;
1643 }
1644 return MZ_OK;
1645}
1646
1647time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
1648{
1649 struct tm ptm;
1650 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
1651 return mktime(&ptm);
1652}
1653
1654int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
1655{
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001656 struct tm *ltm = NULL;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001657 if (ptm == NULL)
1658 return MZ_PARAM_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001659 ltm = localtime(&unix_time); // Returns a 1900-based year
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001660 if (ltm == NULL)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001661 {
1662 // Invalid date stored, so don't return it
1663 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001664 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001665 }
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001666 memcpy(ptm, ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001667 return MZ_OK;
1668}
1669
1670uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
1671{
1672 struct tm ptm;
1673 mz_zip_time_t_to_tm(unix_time, &ptm);
1674 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
1675}
1676
1677uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
1678{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07001679 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001680
1681 // Years supported:
1682
1683 // [00, 79] (assumed to be between 2000 and 2079)
1684 // [80, 207] (assumed to be between 1980 and 2107, typical output of old
1685 // software that does 'year-1900' to get a double digit year)
1686 // [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.)
1687
1688 memcpy(&fixed_tm, ptm, sizeof(struct tm));
1689 if (fixed_tm.tm_year >= 1980) // range [1980, 2107]
1690 fixed_tm.tm_year -= 1980;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001691 else if (fixed_tm.tm_year >= 80) // range [80, 207]
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001692 fixed_tm.tm_year -= 80;
1693 else // range [00, 79]
1694 fixed_tm.tm_year += 20;
1695
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001696 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001697 return 0;
1698
1699 return (uint32_t)(((fixed_tm.tm_mday) + (32 * (fixed_tm.tm_mon + 1)) + (512 * fixed_tm.tm_year)) << 16) |
1700 ((fixed_tm.tm_sec / 2) + (32 * fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
1701}
1702
1703int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
1704{
1705 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
1706 return MZ_OK;
1707}
1708
1709int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
1710{
1711 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
1712 return MZ_OK;
1713}