blob: 6aa163a261f600278301370d6a14ebf60878ed21 [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvaziriffa830f2018-07-11 16:34:01 -07002 Version 2.3.6, July 11, 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 Moinvazirib47b41b2018-07-11 09:44:29 -070033#include "mz_strm_crc32.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080034#ifdef HAVE_LZMA
35# include "mz_strm_lzma.h"
36#endif
Nathan Moinvazirib47b41b2018-07-11 09:44:29 -070037#include "mz_strm_mem.h"
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -070038#ifdef HAVE_PKCRYPT
39# include "mz_strm_pkcrypt.h"
40#endif
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -080041#ifdef HAVE_ZLIB
42# include "mz_strm_zlib.h"
43#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080044
45#include "mz_zip.h"
46
47/***************************************************************************/
48
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070049#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
50#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
51#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
52#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
53#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
54#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
55
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070056#define MZ_ZIP_SIZE_CD_ITEM (0x2e)
57#define MZ_ZIP_SIZE_CD_LOCATOR64 (0x14)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080058
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070059#define MZ_ZIP_EXTENSION_ZIP64 (0x0001)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -070060#define MZ_ZIP_EXTENSION_NTFS (0x000a)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070061#define MZ_ZIP_EXTENSION_AES (0x9901)
62
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080063/***************************************************************************/
64
65typedef struct mz_zip_s
66{
67 mz_zip_file file_info;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070068 mz_zip_file local_file_info;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080069
70 void *stream; // main stream
Nathan Moinvaziricda36002017-10-21 09:37:18 -070071 void *cd_stream; // pointer to the stream with the cd
72 void *cd_mem_stream; // memory stream for central directory
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080073 void *compress_stream; // compression stream
74 void *crc32_stream; // crc32 stream
75 void *crypt_stream; // encryption stream
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070076 void *file_info_stream; // memory stream for storing file info
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -070077 void *local_file_info_stream; // memory stream for storing local file info
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080078
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070079 int32_t open_mode;
80
Nathan Moinvazirifd039e32017-10-22 14:40:39 -070081 uint32_t disk_number_with_cd; // number of the disk with the central dir
Nathan 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
Viktor Szakats915b82e2018-04-24 10:02:39 +000088 uint16_t entry_scanned;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070089 uint16_t entry_opened; // 1 if a file in the zip is currently writ.
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070090
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;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001078 int64_t header_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001079 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;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001188 mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1189
1190 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1191 max_total_in -= header_size;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001192 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1193 max_total_in -= footer_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001194
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001195 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001196 }
Nathan Moinvaziri5e8f4d72018-03-15 10:43:18 -07001197 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 -03001198 {
1199 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1200 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1201 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001202 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001203
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001204 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1205
1206 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1207 }
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001208 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001209 {
1210 mz_stream_crc32_create(&zip->crc32_stream);
1211 mz_stream_set_base(zip->crc32_stream, zip->compress_stream);
1212
1213 err = mz_stream_open(zip->crc32_stream, NULL, zip->open_mode);
1214 }
1215
1216 if (err == MZ_OK)
1217 {
1218 zip->entry_opened = 1;
1219 }
1220
1221 return err;
1222}
1223
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001224extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001225{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001226 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001227 int16_t compression_method = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001228 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001229
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001230#if !defined(HAVE_PKCRYPT) && !defined(HAVE_AES)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001231 if (password != NULL)
1232 return MZ_PARAM_ERROR;
1233#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001234 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001235 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001236 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001237 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001238 if (zip->entry_scanned == 0)
1239 return MZ_PARAM_ERROR;
Nathan Moinvaziri0f09a002018-04-23 18:48:30 -07001240 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) && (!raw))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001241 return MZ_PARAM_ERROR;
1242
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001243 if (zip->file_info.disk_number == zip->disk_number_with_cd)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001244 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1245 else
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001246 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001247
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001248 err = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001249 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001250 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 -07001251
Nathan Moinvaziri51f72502017-10-26 10:15:21 -07001252 compression_method = zip->file_info.compression_method;
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001253 if (raw)
1254 compression_method = MZ_COMPRESS_METHOD_RAW;
1255
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001256#ifdef MZ_ZIP_COMPRESS_ONLY
1257 if (compression_method != MZ_COMPRESS_METHOD_RAW)
1258 err = MZ_SUPPORT_ERROR;
1259#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001260 if (err == MZ_OK)
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001261 err = mz_zip_entry_open_int(handle, compression_method, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001262
1263 return err;
1264}
1265
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -07001266extern 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 -07001267{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001268 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001269 int64_t disk_number = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001270 int32_t err = MZ_OK;
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001271 int16_t compression_method = 0;
1272
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001273
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001274#if !defined(HAVE_PKCRYPT) && !defined(HAVE_AES)
tz-lomb1b25802017-11-10 15:03:02 +03001275 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001276 return MZ_PARAM_ERROR;
1277#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001278 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001279 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001280
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001281 if (zip->entry_opened == 1)
1282 {
1283 err = mz_zip_entry_close(handle);
1284 if (err != MZ_OK)
1285 return err;
1286 }
1287
1288 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001289
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001290 compression_method = zip->file_info.compression_method;
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001291
1292 if (compression_method == MZ_COMPRESS_METHOD_DEFLATE)
1293 {
1294 if ((compress_level == 8) || (compress_level == 9))
1295 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
1296 if (compress_level == 2)
1297 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
1298 if (compress_level == 1)
1299 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1300 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001301#ifdef HAVE_LZMA
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001302 else if (compression_method == MZ_COMPRESS_METHOD_LZMA)
1303 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001304#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001305
1306 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001307
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001308 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001309 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
1310 else
1311 zip->file_info.flag &= ~MZ_ZIP_FLAG_ENCRYPTED;
1312
juaniib8887e92018-02-14 00:51:05 -03001313 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1314 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001315
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001316 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001317 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001318 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001319
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001320#ifdef HAVE_AES
1321 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
1322 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
1323#endif
1324
Nathan Moinvaziri4827f712018-05-02 10:50:47 -07001325 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 +00001326 compression_method = MZ_COMPRESS_METHOD_RAW;
1327
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001328#ifdef MZ_ZIP_DECOMPRESS_ONLY
1329 if (compression_method != MZ_COMPRESS_METHOD_RAW)
1330 err = MZ_SUPPORT_ERROR;
1331#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001332 if (err == MZ_OK)
1333 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
1334 if (err == MZ_OK)
Nathan Moinvaziri51bf64a2017-10-20 14:23:37 -07001335 err = mz_zip_entry_open_int(handle, compression_method, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001336
1337 return err;
1338}
1339
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001340extern int32_t mz_zip_entry_read(void *handle, void *buf, uint32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001341{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001342 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001343 int32_t read = 0;
1344
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001345 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001346 return MZ_PARAM_ERROR;
Nathan Moinvaziri930f9182018-01-22 08:51:07 -08001347 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) // Zlib limitation
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001348 return MZ_PARAM_ERROR;
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07001349 if (len == 0)
1350 return MZ_PARAM_ERROR;
1351
Nathan Moinvazirieb80cd92018-07-11 16:45:09 -07001352 if (zip->file_info.compressed_size == 0)
1353 return 0;
1354
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07001355 // Read entire entry even if uncompressed_size = 0, otherwise
1356 // aes encryption validation will fail if compressed_size > 0
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001357 read = mz_stream_read(zip->crc32_stream, buf, len);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001358 return read;
1359}
1360
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001361extern int32_t mz_zip_entry_write(void *handle, const void *buf, uint32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001362{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001363 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001364 int32_t written = 0;
1365
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001366 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001367 return MZ_PARAM_ERROR;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001368 written = mz_stream_write(zip->crc32_stream, buf, len);
1369 return written;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001370}
1371
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001372extern int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001373{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001374 mz_zip *zip = (mz_zip *)handle;
1375 if (zip == NULL || zip->entry_scanned == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001376 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001377 *file_info = &zip->file_info;
1378 return MZ_OK;
1379}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001380
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001381extern int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001382{
1383 mz_zip *zip = (mz_zip *)handle;
1384 if (zip == NULL || zip->entry_scanned == 0 || zip->entry_opened == 0)
1385 return MZ_PARAM_ERROR;
1386 *local_file_info = &zip->local_file_info;
1387 return MZ_OK;
1388}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001389
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001390extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001391{
1392 mz_zip *zip = (mz_zip *)handle;
1393 uint64_t compressed_size = 0;
Nathan Moinvaziri8871f6c2018-07-08 19:23:56 -07001394 int64_t total_in = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001395 int32_t err = MZ_OK;
1396
1397 if (zip == NULL || zip->entry_opened == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001398 return MZ_PARAM_ERROR;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001399
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001400 mz_stream_close(zip->compress_stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001401 if (crc32 == 0)
1402 crc32 = mz_stream_crc32_get_value(zip->crc32_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001403
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001404 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001405 {
1406#ifdef HAVE_AES
1407 // AES zip version AE-1 will expect a valid crc as well
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001408 if (zip->file_info.aes_version <= 0x0001)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001409#endif
1410 {
Nathan Moinvaziri8871f6c2018-07-08 19:23:56 -07001411 mz_stream_crc32_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
Nathan Moinvaziri184f4cb2018-07-09 07:53:17 -07001412 // If entire entry was not read this will fail
Nathan Moinvaziri8871f6c2018-07-08 19:23:56 -07001413 if ((total_in > 0) && (zip->compression_method != MZ_COMPRESS_METHOD_RAW))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001414 {
1415 if (crc32 != zip->file_info.crc)
1416 err = MZ_CRC_ERROR;
1417 }
1418 }
1419 }
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001420
1421 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri903b73d2017-10-20 08:47:19 -07001422 if ((zip->compression_method != MZ_COMPRESS_METHOD_RAW) || (uncompressed_size == 0))
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001423 mz_stream_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001424
1425 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
1426 {
1427 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001428 err = mz_stream_close(zip->crypt_stream);
1429
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001430 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001431 }
1432
1433 mz_stream_delete(&zip->crypt_stream);
1434
1435 mz_stream_delete(&zip->compress_stream);
1436 mz_stream_crc32_delete(&zip->crc32_stream);
1437
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001438 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001439 {
1440 if (err == MZ_OK)
1441 {
1442 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
1443 if (err == MZ_OK)
1444 err = mz_stream_write_uint32(zip->stream, crc32);
1445 if (err == MZ_OK)
1446 {
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001447 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001448 err = mz_stream_write_uint32(zip->stream, (uint32_t)compressed_size);
Andrew Gunnerson3c52d212018-01-05 22:28:12 -05001449 else
1450 err = mz_stream_write_uint64(zip->stream, compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001451 }
1452 if (err == MZ_OK)
1453 {
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001454 if (zip->file_info.uncompressed_size <= UINT32_MAX)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001455 err = mz_stream_write_uint32(zip->stream, (uint32_t)uncompressed_size);
Andrew Gunnerson3c52d212018-01-05 22:28:12 -05001456 else
1457 err = mz_stream_write_uint64(zip->stream, uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001458 }
1459 }
1460
1461 zip->file_info.crc = crc32;
1462 zip->file_info.compressed_size = compressed_size;
1463 zip->file_info.uncompressed_size = uncompressed_size;
1464
1465 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001466 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001467
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001468 zip->number_entry += 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001469 }
1470
1471 zip->entry_opened = 0;
1472
1473 return err;
1474}
1475
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001476extern int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001477{
1478 return mz_zip_entry_close_raw(handle, 0, 0);
1479}
1480
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001481static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001482{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001483 mz_zip *zip = (mz_zip *)handle;
1484 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001485
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001486 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001487 return MZ_PARAM_ERROR;
1488
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001489 zip->entry_scanned = 0;
1490
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001491 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00001492
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001493 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001494 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001495 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001496 if (err == MZ_OK)
1497 zip->entry_scanned = 1;
1498 return err;
1499}
1500
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001501extern int32_t mz_zip_get_number_entry(void *handle, int64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001502{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001503 mz_zip *zip = (mz_zip *)handle;
1504 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001505 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07001506 *number_entry = zip->number_entry;
1507 return MZ_OK;
1508}
1509
Viktor Szakats0f0535f2018-05-02 13:15:58 +00001510extern int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03001511{
1512 mz_zip *zip = (mz_zip *)handle;
1513 if (zip == NULL || disk_number_with_cd == NULL)
1514 return MZ_PARAM_ERROR;
1515 *disk_number_with_cd = zip->disk_number_with_cd;
1516 return MZ_OK;
1517}
1518
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001519extern int64_t mz_zip_get_entry(void *handle)
1520{
1521 mz_zip *zip = (mz_zip *)handle;
1522
1523 if (zip == NULL)
1524 return MZ_PARAM_ERROR;
1525
1526 return zip->cd_current_pos;
1527}
1528
1529extern int32_t mz_zip_goto_entry(void *handle, uint64_t cd_pos)
1530{
1531 mz_zip *zip = (mz_zip *)handle;
1532
1533 if (zip == NULL)
1534 return MZ_PARAM_ERROR;
1535
1536 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
1537 return MZ_PARAM_ERROR;
1538
1539 zip->cd_current_pos = cd_pos;
1540
1541 return mz_zip_goto_next_entry_int(handle);
1542}
1543
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001544extern int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001545{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001546 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001547
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001548 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001549 return MZ_PARAM_ERROR;
1550
Nathan Moinvaziricda36002017-10-21 09:37:18 -07001551 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001552
1553 return mz_zip_goto_next_entry_int(handle);
1554}
1555
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001556extern int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001557{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001558 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001559
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001560 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001561 return MZ_PARAM_ERROR;
1562
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001563 zip->cd_current_pos += MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
1564 zip->file_info.extrafield_size + zip->file_info.comment_size;
1565
1566 return mz_zip_goto_next_entry_int(handle);
1567}
1568
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001569extern 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 -07001570{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001571 mz_zip *zip = (mz_zip *)handle;
1572 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001573 int32_t result = 0;
1574
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001575 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001576 return MZ_PARAM_ERROR;
1577
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001578 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001579 while (err == MZ_OK)
1580 {
1581 if (filename_compare_cb != NULL)
1582 result = filename_compare_cb(handle, zip->file_info.filename, filename);
1583 else
1584 result = strcmp(zip->file_info.filename, filename);
1585
1586 if (result == 0)
1587 return MZ_OK;
1588
1589 err = mz_zip_goto_next_entry(handle);
1590 }
1591
1592 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001593}
1594
1595/***************************************************************************/
1596
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07001597int32_t mz_zip_attrib_is_dir(int32_t attributes, int32_t version_madeby)
1598{
1599 int32_t host_system = (uint8_t)(version_madeby >> 8);
1600
1601 if (host_system == MZ_HOST_SYSTEM_MSDOS || host_system == MZ_HOST_SYSTEM_WINDOWS_NTFS)
1602 {
1603 if ((attributes & 0x10) == 0x10) // FILE_ATTRIBUTE_DIRECTORY
1604 return MZ_OK;
1605 }
1606 else if (host_system == MZ_HOST_SYSTEM_UNIX || host_system == MZ_HOST_SYSTEM_OSX_DARWIN)
1607 {
1608 if ((attributes & 00170000) == 0040000) // S_ISDIR
1609 return MZ_OK;
1610 }
1611
1612 return MZ_EXIST_ERROR;
1613}
1614
1615/***************************************************************************/
1616
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001617static int32_t mz_zip_invalid_date(const struct tm *ptm)
1618{
1619#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001620 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || // 1980-based year, allow 80 extra
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001621 !datevalue_in_range(0, 11, ptm->tm_mon) ||
1622 !datevalue_in_range(1, 31, ptm->tm_mday) ||
1623 !datevalue_in_range(0, 23, ptm->tm_hour) ||
1624 !datevalue_in_range(0, 59, ptm->tm_min) ||
1625 !datevalue_in_range(0, 59, ptm->tm_sec));
1626#undef datevalue_in_range
1627}
1628
1629static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
1630{
1631 uint64_t date = (uint64_t)(dos_date >> 16);
1632
1633 ptm->tm_mday = (uint16_t)(date & 0x1f);
1634 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
1635 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
1636 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
1637 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
1638 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
1639 ptm->tm_isdst = -1;
1640}
1641
1642int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
1643{
1644 if (ptm == NULL)
1645 return MZ_PARAM_ERROR;
1646
1647 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
1648
1649 if (mz_zip_invalid_date(ptm))
1650 {
1651 // Invalid date stored, so don't return it
1652 memset(ptm, 0, sizeof(struct tm));
1653 return MZ_FORMAT_ERROR;
1654 }
1655 return MZ_OK;
1656}
1657
1658time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
1659{
1660 struct tm ptm;
1661 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
1662 return mktime(&ptm);
1663}
1664
1665int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
1666{
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001667 struct tm *ltm = NULL;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001668 if (ptm == NULL)
1669 return MZ_PARAM_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001670 ltm = localtime(&unix_time); // Returns a 1900-based year
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001671 if (ltm == NULL)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001672 {
1673 // Invalid date stored, so don't return it
1674 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001675 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001676 }
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07001677 memcpy(ptm, ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001678 return MZ_OK;
1679}
1680
1681uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
1682{
1683 struct tm ptm;
1684 mz_zip_time_t_to_tm(unix_time, &ptm);
1685 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
1686}
1687
1688uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
1689{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07001690 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001691
1692 // Years supported:
1693
1694 // [00, 79] (assumed to be between 2000 and 2079)
1695 // [80, 207] (assumed to be between 1980 and 2107, typical output of old
1696 // software that does 'year-1900' to get a double digit year)
1697 // [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.)
1698
1699 memcpy(&fixed_tm, ptm, sizeof(struct tm));
1700 if (fixed_tm.tm_year >= 1980) // range [1980, 2107]
1701 fixed_tm.tm_year -= 1980;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001702 else if (fixed_tm.tm_year >= 80) // range [80, 207]
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001703 fixed_tm.tm_year -= 80;
1704 else // range [00, 79]
1705 fixed_tm.tm_year += 20;
1706
Viktor Szakatsce26dba2018-04-22 19:30:34 +00001707 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001708 return 0;
1709
1710 return (uint32_t)(((fixed_tm.tm_mday) + (32 * (fixed_tm.tm_mon + 1)) + (512 * fixed_tm.tm_year)) << 16) |
1711 ((fixed_tm.tm_sec / 2) + (32 * fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
1712}
1713
1714int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
1715{
1716 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
1717 return MZ_OK;
1718}
1719
1720int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
1721{
1722 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
1723 return MZ_OK;
1724}