blob: 7170857b308f180d5d7c59d7c27b574e6831db49 [file] [log] [blame]
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08001/* zip.c -- Zip manipulation
Nathan Moinvaziric4709062018-11-06 21:12:16 -08002 Version 2.7.4, November 6, 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
Nathan Moinvaziri79cfab02018-08-17 12:21:06 -07007 Copyright (C) 2009-2010 Mathias Svensson
Antoine Cœur0c2fceb2017-10-05 15:30:37 +08008 Modifications for Zip64 support
9 http://result42.com
Nathan Moinvaziri79cfab02018-08-17 12:21:06 -070010 Copyright (C) 2007-2008 Even Rouault
11 Modifications of Unzip for Zip64
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080012 Copyright (C) 1998-2010 Gilles Vollant
Viktor Szakats9dea6f22018-10-17 22:39:01 +000013 https://www.winimage.com/zLibDll/minizip.html
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080014
15 This program is distributed under the terms of the same license as zlib.
16 See the accompanying LICENSE file for the full text of the license.
17*/
18
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080019#include <stdlib.h>
20#include <stdint.h>
Nathan Moinvaziri85d36c52018-08-31 16:43:41 -070021#include <stdio.h>
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080022#include <string.h>
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -070023#include <ctype.h>
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -070024#include <time.h>
Nathan Moinvaziri34eff622018-01-22 09:25:15 -080025#include <limits.h>
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080026
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -070027#include "mz.h"
Nathan Moinvaziri5f091882018-10-24 18:06:08 -070028#include "mz_crypt.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080029#include "mz_strm.h"
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080030#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 Moinvazirib47b41b2018-07-11 09:44:29 -070036#include "mz_strm_mem.h"
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -070037#ifdef HAVE_PKCRYPT
38# include "mz_strm_pkcrypt.h"
39#endif
Nathan Moinvaziri21a31022018-10-24 09:50:16 -070040#ifdef HAVE_AES
41# include "mz_strm_wzaes.h"
42#endif
Nathan Moinvaziri4f6a0e32017-11-10 08:45:14 -080043#ifdef HAVE_ZLIB
44# include "mz_strm_zlib.h"
45#endif
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080046
47#include "mz_zip.h"
48
Nathan Moinvaziric565fa82018-10-19 08:48:33 -070049#if defined(_MSC_VER) && _MSC_VER < 1900
50# define snprintf _snprintf
51#endif
52
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080053/***************************************************************************/
54
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070055#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
56#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
57#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080058#define MZ_ZIP_MAGIC_ENDHEADERU8 { 0x50, 0x4b, 0x05, 0x06 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070059#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
60#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
61#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -080062#define MZ_ZIP_MAGIC_DATADESCRIPTORU8 { 0x50, 0x4b, 0x07, 0x08 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070063
Nathan Moinvaziri638f31f2018-08-27 08:17:16 -070064#define MZ_ZIP_SIZE_LD_ITEM (32)
65#define MZ_ZIP_SIZE_CD_ITEM (46)
66#define MZ_ZIP_SIZE_CD_LOCATOR64 (20)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080067
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080068/***************************************************************************/
69
70typedef struct mz_zip_s
71{
72 mz_zip_file file_info;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070073 mz_zip_file local_file_info;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080074
75 void *stream; // main stream
Nathan Moinvaziricda36002017-10-21 09:37:18 -070076 void *cd_stream; // pointer to the stream with the cd
77 void *cd_mem_stream; // memory stream for central directory
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080078 void *compress_stream; // compression stream
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080079 void *crypt_stream; // encryption stream
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070080 void *file_info_stream; // memory stream for storing file info
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -070081 void *local_file_info_stream; // memory stream for storing local file info
Antoine Cœur0c2fceb2017-10-05 15:30:37 +080082
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070083 int32_t open_mode;
84
Nathan Moinvazirifd039e32017-10-22 14:40:39 -070085 uint32_t disk_number_with_cd; // number of the disk with the central dir
Nathan Moinvaziri413822a2018-10-20 09:45:07 -070086 int64_t disk_offset_shift; // correction for zips that have wrong offset start of cd
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070087
Nathan Moinvaziri413822a2018-10-20 09:45:07 -070088 int64_t cd_start_pos; // pos of the first file in the central dir stream
89 int64_t cd_current_pos; // pos of the current file in the central dir
90 int64_t cd_offset; // offset of start of central directory
91 int64_t cd_size; // size of the central directory
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070092
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -070093 uint8_t entry_scanned; // entry header information read ok
94 uint8_t entry_opened; // entry is open for read/write
95 uint8_t entry_raw; // entry opened with raw mode
Nathan Moinvazirie63d2312018-11-03 19:45:41 -070096 uint32_t entry_crc32; // entry crc32
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -070097
Nathan Moinvaziri904c4082018-10-08 23:31:21 -070098 uint64_t number_entry;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -070099
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700100 uint16_t version_madeby;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700101 char *comment;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800102} mz_zip;
103
104/***************************************************************************/
105
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800106#if 0
107# define mz_zip_print printf
108#else
109# define mz_zip_print(fmt,...)
110#endif
111
112/***************************************************************************/
113
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800114// Locate the end of central directory
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700115static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800116{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700117 int64_t file_size = 0;
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800118 int64_t max_back = 0;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800119 uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8;
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700120 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700121
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700122 err = mz_stream_seek(stream, 0, MZ_SEEK_END);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800123
124 file_size = mz_stream_tell(stream);
Nathan Moinvaziri286a8172018-11-08 14:56:14 -0800125
126 // maximum seek is size of global comment + extra
127 max_back = UINT16_MAX + 128;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800128 if (max_back > file_size)
129 max_back = file_size;
130
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800131 return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800132}
133
Nathan Moinvazirie824da82018-07-26 17:53:15 -0700134// Locate the end of central directory 64 of a zip file
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700135static int32_t mz_zip_search_zip64_eocd(void *stream, const int64_t end_central_offset, int64_t *central_pos)
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800136{
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700137 int64_t offset = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800138 uint32_t value32 = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700139 int32_t err = MZ_OK;
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700140
141
142 *central_pos = 0;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800143
144 // Zip64 end of central directory locator
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -0700145 err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800146 // Read locator signature
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700147 if (err == MZ_OK)
148 {
149 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700150 if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700151 err = MZ_FORMAT_ERROR;
152 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800153 // Number of the disk with the start of the zip64 end of central directory
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700154 if (err == MZ_OK)
155 err = mz_stream_read_uint32(stream, &value32);
156 // Relative offset of the zip64 end of central directory record8
157 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700158 err = mz_stream_read_uint64(stream, (uint64_t *)&offset);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800159 // Total number of disks
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700160 if (err == MZ_OK)
161 err = mz_stream_read_uint32(stream, &value32);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800162 // Goto end of central directory record
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700163 if (err == MZ_OK)
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700164 err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET);
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800165 // The signature
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700166 if (err == MZ_OK)
167 {
168 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri00cca9f2017-10-16 07:37:11 -0700169 if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700170 err = MZ_FORMAT_ERROR;
171 }
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800172
Nathan Moinvaziri3a0fb552017-10-10 12:47:48 -0700173 if (err == MZ_OK)
174 *central_pos = offset;
175
176 return err;
Antoine Cœur0c2fceb2017-10-05 15:30:37 +0800177}
178
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700179// Get info about the current file in the zip file
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700180static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file *file_info, void *file_extra_stream)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700181{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700182 uint64_t ntfs_time = 0;
183 uint32_t reserved = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700184 uint32_t magic = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700185 uint32_t dos_date = 0;
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800186 uint32_t field_pos = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700187 uint16_t field_type = 0;
188 uint16_t field_length = 0;
189 uint32_t field_length_read = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700190 uint16_t ntfs_attrib_id = 0;
191 uint16_t ntfs_attrib_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700192 uint16_t value16 = 0;
193 uint32_t value32 = 0;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800194 int64_t extrafield_pos = -1;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800195 int64_t comment_pos = -1;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700196 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700197
198
199 memset(file_info, 0, sizeof(mz_zip_file));
200
201 // Check the magic
202 err = mz_stream_read_uint32(stream, &magic);
Nathan Moinvaziricda36002017-10-21 09:37:18 -0700203 if (err == MZ_END_OF_STREAM)
204 err = MZ_END_OF_LIST;
205 else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700206 err = MZ_END_OF_LIST;
207 else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
208 err = MZ_FORMAT_ERROR;
209 else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
210 err = MZ_FORMAT_ERROR;
Viktor Szakats915b82e2018-04-24 10:02:39 +0000211
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700212 // Read header fields
213 if (err == MZ_OK)
214 {
215 if (!local)
216 err = mz_stream_read_uint16(stream, &file_info->version_madeby);
217 if (err == MZ_OK)
218 err = mz_stream_read_uint16(stream, &file_info->version_needed);
219 if (err == MZ_OK)
220 err = mz_stream_read_uint16(stream, &file_info->flag);
221 if (err == MZ_OK)
222 err = mz_stream_read_uint16(stream, &file_info->compression_method);
223 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700224 {
225 err = mz_stream_read_uint32(stream, &dos_date);
226 file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
227 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700228 if (err == MZ_OK)
229 err = mz_stream_read_uint32(stream, &file_info->crc);
230 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700231 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700232 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700233 file_info->compressed_size = value32;
234 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700235 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700236 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700237 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700238 file_info->uncompressed_size = value32;
239 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700240 if (err == MZ_OK)
241 err = mz_stream_read_uint16(stream, &file_info->filename_size);
242 if (err == MZ_OK)
243 err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
244 if (!local)
245 {
246 if (err == MZ_OK)
247 err = mz_stream_read_uint16(stream, &file_info->comment_size);
248 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700249 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700250 err = mz_stream_read_uint16(stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700251 file_info->disk_number = value16;
252 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700253 if (err == MZ_OK)
254 err = mz_stream_read_uint16(stream, &file_info->internal_fa);
255 if (err == MZ_OK)
256 err = mz_stream_read_uint32(stream, &file_info->external_fa);
257 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700258 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700259 err = mz_stream_read_uint32(stream, &value32);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700260 file_info->disk_offset = value32;
261 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700262 }
263 }
264
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700265 if (err == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800266 err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700267
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800268 // Copy variable length data to memory stream for later retrieval
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700269 if ((err == MZ_OK) && (file_info->filename_size > 0))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700270 err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800271 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800272 extrafield_pos = (int64_t)file_info->filename_size + 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700273
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800274 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
275 err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800276 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800277
278 comment_pos = extrafield_pos + (int64_t)file_info->extrafield_size + 1;
279 if ((err == MZ_OK) && (file_info->comment_size > 0))
280 err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800281 mz_stream_write_uint8(file_extra_stream, 0);
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800282
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700283 if ((err == MZ_OK) && (file_info->extrafield_size > 0))
284 {
Nathan Moinvaziri69e35492018-11-05 15:48:41 -0800285 // Seek to and parse the extra field
286 err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700287
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800288 while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700289 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700290 err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800291 field_pos += 4;
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800292
293 // Don't allow field length to exceed size of remaining extrafield
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800294 if (field_length > (file_info->extrafield_size - field_pos))
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -0800295 field_length = (uint16_t)(file_info->extrafield_size - field_pos);
Nathan Moinvazirifeb86c52018-11-05 11:40:21 -0800296
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700297 // Read ZIP64 extra field
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800298 if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700299 {
300 if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700301 err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700302 if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700303 err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700304 if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700305 err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset);
juanii4ae79922018-02-11 14:29:36 -0300306 if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700307 err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700308 }
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700309 // Read NTFS extra field
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800310 else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700311 {
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700312 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700313 err = mz_stream_read_uint32(file_extra_stream, &reserved);
314 field_length_read = 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700315
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800316 while ((err == MZ_OK) && (field_length_read + 4 <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700317 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700318 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700319 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700320 err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800321 field_length_read += 4;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700322
Nathan Moinvaziri7a3b6982018-05-09 00:45:02 -0700323 if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700324 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700325 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700326 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
327
juanii7063b0e2018-02-11 13:56:21 -0300328 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700329 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700330 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700331 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
332 }
juanii7063b0e2018-02-11 13:56:21 -0300333 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700334 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700335 err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700336 mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
337 }
338 }
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800339 else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700340 {
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800341 err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700342 }
343
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800344 field_length_read += ntfs_attrib_size;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700345 }
346 }
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700347 // Read UNIX1 extra field
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800348 else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700349 {
350 if (err == MZ_OK && file_info->accessed_date == 0)
351 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700352 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700353 if (err == MZ_OK)
354 file_info->accessed_date = value32;
355 }
356 if (err == MZ_OK && file_info->modified_date == 0)
357 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700358 err = mz_stream_read_uint32(file_extra_stream, &value32);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700359 if (err == MZ_OK)
360 file_info->modified_date = value32;
361 }
362 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700363 err = mz_stream_read_uint16(file_extra_stream, &value16); // User id
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700364 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700365 err = mz_stream_read_uint16(file_extra_stream, &value16); // Group id
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700366
367 // Skip variable data
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800368 mz_stream_seek(file_extra_stream, field_length - 12, MZ_SEEK_CUR);
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700369 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700370#ifdef HAVE_AES
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700371 // Read AES extra field
Nathan Moinvaziri761d28e2018-11-07 06:47:15 -0800372 else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700373 {
374 uint8_t value8 = 0;
375 // Verify version info
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700376 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700377 // Support AE-1 and AE-2
378 if (value16 != 1 && value16 != 2)
379 err = MZ_FORMAT_ERROR;
380 file_info->aes_version = value16;
381 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700382 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700383 if ((char)value8 != 'A')
384 err = MZ_FORMAT_ERROR;
385 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700386 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700387 if ((char)value8 != 'E')
388 err = MZ_FORMAT_ERROR;
389 // Get AES encryption strength and actual compression method
390 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700391 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700392 err = mz_stream_read_uint8(file_extra_stream, &value8);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700393 file_info->aes_encryption_mode = value8;
394 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700395 if (err == MZ_OK)
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700396 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700397 err = mz_stream_read_uint16(file_extra_stream, &value16);
Nathan Moinvaziri2ecd8a02018-07-13 12:31:39 -0700398 file_info->compression_method = value16;
399 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700400 }
401#endif
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800402 else if (field_length > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700403 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700404 err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700405 }
406
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800407 field_pos += field_length;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700408 }
409 }
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800410
411 // Get pointers to variable length data
412 mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename);
413 if (extrafield_pos >= 0)
414 mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield);
415 if (comment_pos >= 0)
416 mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment);
417
418 // Set to empty string just in-case
419 if (file_info->filename == NULL)
420 file_info->filename = "";
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800421 if (file_info->extrafield == NULL)
422 file_info->extrafield_size = 0;
Nathan Moinvaziri60008442018-11-07 06:29:01 -0800423 if (file_info->comment == NULL)
424 file_info->comment = "";
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700425
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800426 if (err == MZ_OK)
427 {
428 mz_zip_print("Zip - Entry - Read header - %s (local %"PRId8")\n",
429 file_info->filename, local);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -0800430 mz_zip_print("Zip - Entry - Read header compress (ucs %lld cs %lld crc 0x%08x)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800431 file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
432 if (!local)
Nathan Moinvazirieffc1422018-11-08 15:24:17 -0800433 mz_zip_print("Zip - Entry - Read header disk (disk %d offset %lld)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800434 file_info->disk_number, file_info->disk_offset);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -0800435 mz_zip_print("Zip - Entry - Read header variable (fnl %d efs %d cms %d)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800436 file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
437 }
438
439 return err;
440}
441
442static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size)
443{
444 uint32_t value32 = 0;
445 int64_t value64 = 0;
446 int32_t err = MZ_OK;
447
448
449 err = mz_stream_read_uint32(stream, &value32);
450 if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR)
451 err = MZ_FORMAT_ERROR;
452 if (err == MZ_OK)
453 err = mz_stream_read_uint32(stream, &value32);
454 if ((err == MZ_OK) && (crc32 != NULL))
455 *crc32 = value32;
456 if (err == MZ_OK)
457 {
458 // If zip 64 extension is enabled then read as 8 byte
459 if (!zip64)
460 {
461 err = mz_stream_read_uint32(stream, &value32);
462 value64 = value32;
463 }
464 else
465 err = mz_stream_read_int64(stream, &value64);
466 if ((err == MZ_OK) && (compressed_size != NULL))
467 *compressed_size = value64;
468 }
469 if (err == MZ_OK)
470 {
471 if (!zip64)
472 {
473 err = mz_stream_read_uint32(stream, &value32);
474 value64 = value32;
475 }
476 else
477 err = mz_stream_read_int64(stream, &value64);
478 if ((err == MZ_OK) && (uncompressed_size != NULL))
479 *uncompressed_size = value64;
480 }
481
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700482 return err;
483}
484
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700485static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700486{
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700487 uint64_t ntfs_time = 0;
488 uint32_t reserved = 0;
489 uint32_t dos_date = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700490 uint16_t extrafield_size = 0;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700491 uint16_t field_type = 0;
492 uint16_t field_length = 0;
493 uint16_t field_length_zip64 = 0;
494 uint16_t field_length_ntfs = 0;
495 uint16_t field_length_aes = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700496 uint16_t filename_size = 0;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700497 uint16_t filename_length = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700498 uint16_t comment_size = 0;
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700499 uint16_t version_needed = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700500 int32_t err = MZ_OK;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700501 int32_t err_mem = MZ_OK;
502 uint8_t zip64 = 0;
503 uint8_t skip_aes = 0;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700504 uint8_t mask = 0;
505 uint8_t write_end_slash = 0;
506 const char *filename = NULL;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700507 char masked_name[64];
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700508 void *file_extra_stream = NULL;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700509
510 if (file_info == NULL)
511 return MZ_PARAM_ERROR;
512
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700513 if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO))
514 mask = 1;
515
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700516 // Calculate extra field sizes
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700517 if (file_info->uncompressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700518 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700519 if (file_info->compressed_size >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700520 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700521 if (file_info->disk_offset >= UINT32_MAX)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700522 field_length_zip64 += 8;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700523
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700524 if (file_info->zip64 == MZ_ZIP64_AUTO)
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700525 {
526 // If uncompressed size is unknown, assume zip64 for 64-bit data descriptors
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700527 zip64 = (local && file_info->uncompressed_size == 0) || (field_length_zip64 > 0);
Nathan Moinvaziri121e0882018-05-03 17:57:20 -0700528 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700529 else if (file_info->zip64 == MZ_ZIP64_FORCE)
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700530 {
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700531 zip64 = 1;
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700532 }
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700533 else if (file_info->zip64 == MZ_ZIP64_DISABLE)
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700534 {
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700535 // Zip64 extension is required to zip file
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700536 if (field_length_zip64 > 0)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700537 return MZ_PARAM_ERROR;
Nathan Moinvaziria56a08c2018-05-03 09:35:37 -0700538 }
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700539
540 if (zip64)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700541 {
542 extrafield_size += 4;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700543 extrafield_size += field_length_zip64;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700544 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700545
546 // Calculate extra field size and check for duplicates
547 if (file_info->extrafield_size > 0)
548 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700549 mz_stream_mem_create(&file_extra_stream);
Nathan Moinvaziri915c5132018-10-26 20:00:52 -0700550 mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
551 file_info->extrafield_size);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700552
553 do
554 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700555 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700556 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700557 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700558 if (err_mem != MZ_OK)
559 break;
560
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700561 // Prefer incoming aes extensions over ours
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700562 if (field_type == MZ_ZIP_EXTENSION_AES)
563 skip_aes = 1;
564
565 // Prefer our zip64, ntfs extension over incoming
566 if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS)
567 extrafield_size += 4 + field_length;
568
569 if (err_mem == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800570 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700571 }
572 while (err_mem == MZ_OK);
573 }
574
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700575#ifdef HAVE_AES
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700576 if (!skip_aes)
577 {
578 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700579 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700580 field_length_aes = 1 + 1 + 1 + 2 + 2;
581 extrafield_size += 4 + field_length_aes;
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700582 }
583 }
Nathan Moinvaziria2bb08c2018-10-28 13:45:30 -0700584#else
Nathan Moinvaziri4ae469a2018-10-28 15:32:04 -0700585 MZ_UNUSED(skip_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700586#endif
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700587 // NTFS timestamps
Nathan Moinvazirid9e159a2018-02-13 15:30:30 -0800588 if ((file_info->modified_date != 0) &&
589 (file_info->accessed_date != 0) &&
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700590 (file_info->creation_date != 0) && (!mask))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700591 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700592 field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
593 extrafield_size += 4 + field_length_ntfs;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700594 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700595
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700596 if (local)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700597 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700598 else
599 {
600 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
601 if (err == MZ_OK)
602 err = mz_stream_write_uint16(stream, file_info->version_madeby);
603 }
604
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700605 // Calculate version needed to extract
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700606 if (err == MZ_OK)
Nathan Moinvaziri46f71432018-05-03 17:24:32 -0700607 {
608 version_needed = file_info->version_needed;
609 if (version_needed == 0)
610 {
611 version_needed = 20;
612 if (zip64)
613 version_needed = 45;
614#ifdef HAVE_AES
615 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
616 version_needed = 51;
617#endif
618#ifdef HAVE_LZMA
619 if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
620 version_needed = 63;
621#endif
622 }
623 err = mz_stream_write_uint16(stream, version_needed);
624 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700625 if (err == MZ_OK)
626 err = mz_stream_write_uint16(stream, file_info->flag);
627 if (err == MZ_OK)
628 {
629#ifdef HAVE_AES
Nathan Moinvaziri413822a2018-10-20 09:45:07 -0700630 if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -0700631 err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700632 else
633#endif
634 err = mz_stream_write_uint16(stream, file_info->compression_method);
635 }
636 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700637 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700638 if (file_info->modified_date != 0 && !mask)
Nathan Moinvaziri17cdaec2017-10-26 10:18:41 -0700639 dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700640 err = mz_stream_write_uint32(stream, dos_date);
641 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700642
643 if (err == MZ_OK)
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700644 {
645 if (mask)
646 err = mz_stream_write_uint32(stream, 0);
647 else
648 err = mz_stream_write_uint32(stream, file_info->crc); // crc
649 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700650 if (err == MZ_OK)
651 {
652 if (file_info->compressed_size >= UINT32_MAX) // compr size
653 err = mz_stream_write_uint32(stream, UINT32_MAX);
654 else
655 err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
656 }
657 if (err == MZ_OK)
658 {
659 if (file_info->uncompressed_size >= UINT32_MAX) // uncompr size
660 err = mz_stream_write_uint32(stream, UINT32_MAX);
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700661 else if (mask)
662 err = mz_stream_write_uint32(stream, 0);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700663 else
664 err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
665 }
666
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700667 if (mask)
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700668 {
Nathan Moinvazirieffc1422018-11-08 15:24:17 -0800669 snprintf(masked_name, sizeof(masked_name), "%x_%llx", file_info->disk_number,
670 file_info->disk_offset);
Nathan Moinvazirifa620e42018-10-20 10:37:10 -0700671 filename = masked_name;
Nathan Moinvaziri3ad1a922018-05-02 13:04:41 -0700672 }
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700673 else
674 {
675 filename = file_info->filename;
676
677 if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) &&
678 ((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\')))
679 {
680 filename_size += 1;
681 write_end_slash = 1;
682 }
683 }
684
685 filename_length = (uint16_t)strlen(filename);
686 filename_size += filename_length;
687
688 if (err == MZ_OK)
689 err = mz_stream_write_uint16(stream, filename_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700690 if (err == MZ_OK)
691 err = mz_stream_write_uint16(stream, extrafield_size);
692
693 if (!local)
694 {
695 if (file_info->comment != NULL)
696 comment_size = (uint16_t)strlen(file_info->comment);
697 if (err == MZ_OK)
698 err = mz_stream_write_uint16(stream, comment_size);
699 if (err == MZ_OK)
Nathan Moinvazirifd039e32017-10-22 14:40:39 -0700700 err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700701 if (err == MZ_OK)
702 err = mz_stream_write_uint16(stream, file_info->internal_fa);
703 if (err == MZ_OK)
704 err = mz_stream_write_uint32(stream, file_info->external_fa);
705 if (err == MZ_OK)
706 {
707 if (file_info->disk_offset >= UINT32_MAX)
708 err = mz_stream_write_uint32(stream, UINT32_MAX);
709 else
710 err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
711 }
712 }
713
714 if (err == MZ_OK)
715 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700716 if (mz_stream_write(stream, filename, filename_length) != filename_length)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700717 err = MZ_WRITE_ERROR;
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700718
719 // Ensure that directories have a slash appended to them for compatibility
720 if (err == MZ_OK && write_end_slash)
721 err = mz_stream_write_uint8(stream, '/');
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700722 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700723
724 if (file_info->extrafield_size > 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700725 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800726 err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700727 while (err == MZ_OK && err_mem == MZ_OK)
728 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700729 err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700730 if (err_mem == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700731 err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700732 if (err_mem != MZ_OK)
733 break;
734
735 // Prefer our zip 64, ntfs extensions over incoming
736 if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS)
737 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800738 err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700739 continue;
740 }
741
742 err = mz_stream_write_uint16(stream, field_type);
743 if (err == MZ_OK)
744 err = mz_stream_write_uint16(stream, field_length);
745 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700746 err = mz_stream_copy(stream, file_extra_stream, field_length);
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700747 }
748
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700749 mz_stream_mem_delete(&file_extra_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700750 }
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700751
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700752 // Write ZIP64 extra field
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -0700753 if ((err == MZ_OK) && (zip64))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700754 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700755 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700756 if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
Nathan Moinvaziric565fa82018-10-19 08:48:33 -0700757 {
758 if (mask)
759 err = mz_stream_write_int64(stream, 0);
760 else
761 err = mz_stream_write_int64(stream, file_info->uncompressed_size);
762 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700763 if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700764 err = mz_stream_write_int64(stream, file_info->compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700765 if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
Nathan Moinvaziri904c4082018-10-08 23:31:21 -0700766 err = mz_stream_write_int64(stream, file_info->disk_offset);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700767 }
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700768 // Write NTFS extra field
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700769 if ((err == MZ_OK) && (field_length_ntfs > 0))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700770 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700771 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs);
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700772 if (err == MZ_OK)
773 err = mz_stream_write_uint32(stream, reserved);
774 if (err == MZ_OK)
775 err = mz_stream_write_uint16(stream, 0x01);
776 if (err == MZ_OK)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700777 err = mz_stream_write_uint16(stream, field_length_ntfs - 8);
juanii3679a3d2018-02-11 13:55:38 -0300778 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700779 {
780 mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
781 err = mz_stream_write_uint64(stream, ntfs_time);
782 }
juanii3679a3d2018-02-11 13:55:38 -0300783 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700784 {
785 mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
786 err = mz_stream_write_uint64(stream, ntfs_time);
787 }
juanii3679a3d2018-02-11 13:55:38 -0300788 if (err == MZ_OK)
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -0700789 {
790 mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
791 err = mz_stream_write_uint64(stream, ntfs_time);
792 }
793 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700794#ifdef HAVE_AES
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -0700795 // Write AES extra field
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700796 if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700797 {
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -0700798 err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700799 if (err == MZ_OK)
800 err = mz_stream_write_uint16(stream, file_info->aes_version);
801 if (err == MZ_OK)
802 err = mz_stream_write_uint8(stream, 'A');
803 if (err == MZ_OK)
804 err = mz_stream_write_uint8(stream, 'E');
805 if (err == MZ_OK)
806 err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
807 if (err == MZ_OK)
808 err = mz_stream_write_uint16(stream, file_info->compression_method);
809 }
810#endif
Nathan Moinvaziri298126a2018-07-31 10:19:48 -0700811 if ((err == MZ_OK) && (!local) && (file_info->comment != NULL))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700812 {
Nathan Moinvazirica014e62018-08-15 07:31:12 -0700813 if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size)
Nathan Moinvaziri05e03ca2018-10-28 16:15:13 -0700814 err = MZ_WRITE_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -0700815 }
816
817 return err;
818}
819
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800820static int32_t mz_zip_entry_write_descriptor(void *stream, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size)
821{
822 int32_t err = MZ_OK;
823
824 err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
825 if (err == MZ_OK)
826 err = mz_stream_write_uint32(stream, crc32);
827
828 // Store data descriptor as 8 bytes if zip 64 extension enabled
829 if (err == MZ_OK)
830 {
831 // Zip 64 extension is enabled when uncompressed size is > UINT32_MAX
832 if (uncompressed_size <= UINT32_MAX)
833 err = mz_stream_write_uint32(stream, (uint32_t)compressed_size);
834 else
835 err = mz_stream_write_int64(stream, compressed_size);
836 }
837 if (err == MZ_OK)
838 {
839 if (uncompressed_size <= UINT32_MAX)
840 err = mz_stream_write_uint32(stream, (uint32_t)uncompressed_size);
841 else
842 err = mz_stream_write_int64(stream, uncompressed_size);
843 }
844
845 return err;
846}
847
848static int32_t mz_zip_read_cd(void *handle)
849{
850 mz_zip *zip = (mz_zip *)handle;
851 uint64_t number_entry_cd64 = 0;
852 uint64_t number_entry = 0;
853 uint64_t number_entry_cd = 0;
854 int64_t eocd_pos = 0;
855 int64_t eocd_pos64 = 0;
856 int64_t value64i = 0;
857 uint16_t value16 = 0;
858 uint32_t value32 = 0;
859 uint64_t value64 = 0;
860 uint16_t comment_size = 0;
861 int32_t comment_read = 0;
862 int32_t err = MZ_OK;
863
864
865 if (zip == NULL)
866 return MZ_PARAM_ERROR;
867
868 // Read and cache central directory records
869 err = mz_zip_search_eocd(zip->stream, &eocd_pos);
870 if (err == MZ_OK)
871 {
872 // The signature, already checked
873 err = mz_stream_read_uint32(zip->stream, &value32);
874 // Number of this disk
875 if (err == MZ_OK)
876 err = mz_stream_read_uint16(zip->stream, &value16);
877 // Number of the disk with the start of the central directory
878 if (err == MZ_OK)
879 err = mz_stream_read_uint16(zip->stream, &value16);
880 zip->disk_number_with_cd = value16;
881 // Total number of entries in the central dir on this disk
882 if (err == MZ_OK)
883 err = mz_stream_read_uint16(zip->stream, &value16);
884 zip->number_entry = value16;
885 // Total number of entries in the central dir
886 if (err == MZ_OK)
887 err = mz_stream_read_uint16(zip->stream, &value16);
888 number_entry_cd = value16;
889 if (number_entry_cd != zip->number_entry)
890 err = MZ_FORMAT_ERROR;
891 // Size of the central directory
892 if (err == MZ_OK)
893 err = mz_stream_read_uint32(zip->stream, &value32);
894 if (err == MZ_OK)
895 zip->cd_size = value32;
896 // Offset of start of central directory with respect to the starting disk number
897 if (err == MZ_OK)
898 err = mz_stream_read_uint32(zip->stream, &value32);
899 if (err == MZ_OK)
900 zip->cd_offset = value32;
901 // Zip file global comment length
902 if (err == MZ_OK)
903 err = mz_stream_read_uint16(zip->stream, &comment_size);
904 if ((err == MZ_OK) && (comment_size > 0))
905 {
906 zip->comment = (char *)MZ_ALLOC(comment_size + 1);
907 if (zip->comment != NULL)
908 {
909 comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
910 // Don't fail if incorrect comment length read, not critical
911 if (comment_read < 0)
912 comment_read = 0;
913 zip->comment[comment_read] = 0;
914 }
915 }
916
917 if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
918 {
919 // Format should be Zip64, as the central directory or file size is too large
920 if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
921 {
922 eocd_pos = eocd_pos64;
923
924 err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
925 // The signature, already checked
926 if (err == MZ_OK)
927 err = mz_stream_read_uint32(zip->stream, &value32);
928 // Size of zip64 end of central directory record
929 if (err == MZ_OK)
930 err = mz_stream_read_uint64(zip->stream, &value64);
931 // Version made by
932 if (err == MZ_OK)
933 err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
934 // Version needed to extract
935 if (err == MZ_OK)
936 err = mz_stream_read_uint16(zip->stream, &value16);
937 // Number of this disk
938 if (err == MZ_OK)
939 err = mz_stream_read_uint32(zip->stream, &value32);
940 // Number of the disk with the start of the central directory
941 if (err == MZ_OK)
942 err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
943 // Total number of entries in the central directory on this disk
944 if (err == MZ_OK)
945 err = mz_stream_read_uint64(zip->stream, &number_entry);
946 // Total number of entries in the central directory
947 if (err == MZ_OK)
948 err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
949 if (number_entry == UINT32_MAX)
950 zip->number_entry = number_entry_cd64;
951 // Size of the central directory
952 if (err == MZ_OK)
953 err = mz_stream_read_int64(zip->stream, &zip->cd_size);
954 // Offset of start of central directory with respect to the starting disk number
955 if (err == MZ_OK)
956 err = mz_stream_read_int64(zip->stream, &zip->cd_offset);
957 }
958 else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
959 (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
960 {
961 err = MZ_FORMAT_ERROR;
962 }
963 }
964 }
965
966 if (err == MZ_OK)
967 {
Nathan Moinvazirieffc1422018-11-08 15:24:17 -0800968 mz_zip_print("Zip - Read cd (disk %d entries %lld offset %lld size %lld)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -0800969 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
970
971 // Verify central directory signature exists at offset
972 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
973 if (err == MZ_OK)
974 err = mz_stream_read_uint32(zip->stream, &value32);
975 if (value32 != MZ_ZIP_MAGIC_CENTRALHEADER)
976 {
977 // If not found attempt to seek backward to find it
978 err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
979 if (err == MZ_OK)
980 err = mz_stream_read_uint32(zip->stream, &value32);
981 if (value32 == MZ_ZIP_MAGIC_CENTRALHEADER)
982 {
983 // If found compensate for incorrect locations
984 value64i = zip->cd_offset;
985 zip->cd_offset = eocd_pos - zip->cd_size;
986 // Assume disk has prepended data
987 zip->disk_offset_shift = zip->cd_offset - value64i;
988 }
989 }
990 }
991
992 if (err == MZ_OK)
993 {
994 if (eocd_pos < zip->cd_offset)
995 {
996 // End of central dir should always come after central dir
997 err = MZ_FORMAT_ERROR;
998 }
999 else if (eocd_pos < zip->cd_offset + zip->cd_size)
1000 {
1001 // Truncate size of cd if incorrect size or offset provided
1002 zip->cd_size = eocd_pos - zip->cd_offset;
1003 }
1004 }
1005
1006 return err;
1007}
1008
1009static int32_t mz_zip_write_cd(void *handle)
1010{
1011 mz_zip *zip = (mz_zip *)handle;
1012 int64_t zip64_eocd_pos_inzip = 0;
1013 uint16_t comment_size = 0;
1014 int64_t disk_number = 0;
1015 int64_t disk_size = 0;
1016 int32_t err = MZ_OK;
1017
1018
1019 if (zip == NULL)
1020 return MZ_PARAM_ERROR;
1021
1022 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
1023 zip->disk_number_with_cd = (uint32_t)disk_number;
1024 if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
1025 zip->disk_number_with_cd += 1;
1026 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1027
1028 zip->cd_offset = mz_stream_tell(zip->stream);
1029 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
1030 zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
1031 mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
1032
1033 err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
1034
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001035 mz_zip_print("Zip - Write cd (disk %d entries %lld offset %lld size %lld)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001036 zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
1037
1038 // Write the ZIP64 central directory header
1039 if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
1040 {
1041 zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
1042
1043 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
1044
1045 // Size of this 'zip64 end of central directory'
1046 if (err == MZ_OK)
1047 err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
1048 // Version made by
1049 if (err == MZ_OK)
1050 err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
1051 // Version needed
1052 if (err == MZ_OK)
1053 err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
1054 // Number of this disk
1055 if (err == MZ_OK)
1056 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
1057 // Number of the disk with the start of the central directory
1058 if (err == MZ_OK)
1059 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
1060 // Total number of entries in the central dir on this disk
1061 if (err == MZ_OK)
1062 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
1063 // Total number of entries in the central dir
1064 if (err == MZ_OK)
1065 err = mz_stream_write_uint64(zip->stream, zip->number_entry);
1066 // Size of the central directory
1067 if (err == MZ_OK)
1068 err = mz_stream_write_int64(zip->stream, zip->cd_size);
1069 // Offset of start of central directory with respect to the starting disk number
1070 if (err == MZ_OK)
1071 err = mz_stream_write_int64(zip->stream, zip->cd_offset);
1072 if (err == MZ_OK)
1073 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
1074
1075 // Number of the disk with the start of the central directory
1076 if (err == MZ_OK)
1077 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
1078 // Relative offset to the end of zip64 central directory
1079 if (err == MZ_OK)
1080 err = mz_stream_write_int64(zip->stream, zip64_eocd_pos_inzip);
1081 // Number of the disk with the start of the central directory
1082 if (err == MZ_OK)
1083 err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
1084 }
1085
1086 // Write the central directory header
1087
1088 // Signature
1089 if (err == MZ_OK)
1090 err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
1091 // Number of this disk
1092 if (err == MZ_OK)
1093 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
1094 // Number of the disk with the start of the central directory
1095 if (err == MZ_OK)
1096 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
1097 // Total number of entries in the central dir on this disk
1098 if (err == MZ_OK)
1099 {
1100 if (zip->number_entry >= UINT16_MAX)
1101 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1102 else
1103 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1104 }
1105 // Total number of entries in the central dir
1106 if (err == MZ_OK)
1107 {
1108 if (zip->number_entry >= UINT16_MAX)
1109 err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
1110 else
1111 err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
1112 }
1113 // Size of the central directory
1114 if (err == MZ_OK)
1115 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
1116 // Offset of start of central directory with respect to the starting disk number
1117 if (err == MZ_OK)
1118 {
1119 if (zip->cd_offset >= UINT32_MAX)
1120 err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
1121 else
1122 err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
1123 }
1124
1125 // Write global comment
1126 if (zip->comment != NULL)
1127 comment_size = (uint16_t)strlen(zip->comment);
1128 if (err == MZ_OK)
1129 err = mz_stream_write_uint16(zip->stream, comment_size);
1130 if (err == MZ_OK)
1131 {
1132 if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
1133 err = MZ_READ_ERROR;
1134 }
1135 return err;
1136}
1137
1138static int32_t mz_zip_rebuild_cd(void *handle)
1139{
1140 mz_zip *zip = (mz_zip *)handle;
1141 mz_zip_file local_file_info;
1142 void *local_file_info_stream = NULL;
1143 void *file_extra_stream = NULL;
1144 void *cd_mem_stream = NULL;
1145 uint64_t number_entry = 0;
1146 int64_t descriptor_pos = 0;
1147 int64_t disk_offset = 0;
1148 int64_t disk_number = 0;
1149 int64_t compressed_size = 0;
1150 int64_t uncompressed_size = 0;
1151 uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8;
1152 uint32_t crc32 = 0;
1153 int32_t disk_number_with_cd = 0;
1154 int32_t err = MZ_OK;
1155 uint8_t zip64 = 0;
1156
1157
1158 mz_zip_print("Zip - Rebuild cd\n");
1159
1160 mz_zip_get_cd_mem_stream(handle, &cd_mem_stream);
1161
1162 // Determine if we are on a split disk or not
1163 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0);
1164 if (mz_stream_tell(zip->stream) < 0)
1165 {
1166 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1167 mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
1168 }
1169 else
1170 disk_number_with_cd = 1;
1171
1172 if (mz_stream_is_open(cd_mem_stream) != MZ_OK)
1173 err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1174
1175 mz_stream_mem_create(&local_file_info_stream);
1176 mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1177
1178 while (err == MZ_OK)
1179 {
1180 memset(&local_file_info, 0, sizeof(local_file_info));
1181
1182 // Get current offset and disk number for central dir record
1183 disk_offset = mz_stream_tell(zip->stream);
1184 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1185
1186 // Read local headers
1187 err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream);
1188
1189 local_file_info.disk_offset = disk_offset;
1190 if (disk_number < 0)
1191 disk_number = 0;
1192 local_file_info.disk_number = (uint32_t)disk_number;
1193
1194 if (err == MZ_OK)
1195 {
1196 if (local_file_info.compressed_size > 0)
1197 {
1198 err = mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR);
1199 }
1200 else if (local_file_info.uncompressed_size > 0)
1201 {
1202 err = mz_stream_find(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
1203 INT64_MAX, &descriptor_pos);
1204 }
1205 }
1206
1207 // Read descriptor if it exists so we can get to the next local header
1208 if ((err == MZ_OK) && (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
1209 {
1210 mz_stream_mem_create(&file_extra_stream);
1211 mz_stream_mem_set_buffer(file_extra_stream, (void *)local_file_info.extrafield,
1212 local_file_info.extrafield_size);
1213
1214 zip64 = 0;
1215 if (mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
1216 zip64 = 1;
1217
1218 mz_stream_mem_delete(&file_extra_stream);
1219
1220 err = mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32,
1221 &compressed_size, &uncompressed_size);
1222
1223 if (local_file_info.crc == 0)
1224 local_file_info.crc = crc32;
1225 if (local_file_info.compressed_size == 0)
1226 local_file_info.compressed_size = compressed_size;
1227 if (local_file_info.uncompressed_size == 0)
1228 local_file_info.uncompressed_size = uncompressed_size;
1229 }
1230
1231 // Rewrite central dir with local headers and offsets
1232 if (err == MZ_OK)
1233 err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
1234
1235 if (err == MZ_OK)
1236 number_entry += 1;
1237 }
1238
1239 mz_stream_mem_delete(&local_file_info_stream);
1240
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001241 mz_zip_print("Zip - Rebuild cd complete (cddisk %d entries %lld)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001242 disk_number_with_cd, number_entry);
1243
1244 if (number_entry == 0)
1245 return err;
1246
1247 // Set new upper seek boundary for central dir mem stream
1248 disk_offset = mz_stream_tell(cd_mem_stream);
1249 mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset);
1250
1251 // Set new central directory info
1252 mz_zip_set_cd_stream(handle, 0, cd_mem_stream);
1253 mz_zip_set_number_entry(handle, number_entry);
1254 mz_zip_set_disk_number_with_cd(handle, disk_number_with_cd);
1255
1256 return MZ_OK;
1257}
1258
1259void *mz_zip_create(void **handle)
1260{
1261 mz_zip *zip = NULL;
1262
1263 zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
1264 if (zip != NULL)
1265 memset(zip, 0, sizeof(mz_zip));
1266 if (handle != NULL)
1267 *handle = zip;
1268
1269 return zip;
1270}
1271
1272void mz_zip_delete(void **handle)
1273{
1274 mz_zip *zip = NULL;
1275 if (handle == NULL)
1276 return;
1277 zip = (mz_zip *)*handle;
1278 if (zip != NULL)
1279 {
1280 MZ_FREE(zip);
1281 }
1282 *handle = NULL;
1283}
1284
1285int32_t mz_zip_open(void *handle, void *stream, int32_t mode)
1286{
1287 mz_zip *zip = (mz_zip *)handle;
1288 int32_t err = MZ_OK;
1289
1290
1291 if (zip == NULL)
1292 return MZ_PARAM_ERROR;
1293
1294 mz_zip_print("Zip - Open\n");
1295
1296 zip->stream = stream;
1297
1298 mz_stream_mem_create(&zip->cd_mem_stream);
1299
1300 if (mode & MZ_OPEN_MODE_WRITE)
1301 {
1302 mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
1303 zip->cd_stream = zip->cd_mem_stream;
1304 }
1305 else
1306 {
1307 zip->cd_stream = stream;
1308 }
1309
1310 if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
1311 {
1312 if ((mode & MZ_OPEN_MODE_CREATE) == 0)
1313 {
1314 err = mz_zip_read_cd(zip);
1315 if (err != MZ_OK)
1316 {
1317#if 0
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001318 mz_zip_print("Zip - Error detected reading cd (%d)", err);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001319 if (mz_zip_rebuild_cd(zip) == MZ_OK)
1320 err = MZ_OK;
1321#endif
1322 }
1323 }
1324
1325 if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
1326 {
1327 if (zip->cd_size > 0)
1328 {
1329 // Store central directory in memory
1330 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1331 if (err == MZ_OK)
1332 err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size);
1333 if (err == MZ_OK)
1334 err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
1335 }
1336 else
1337 {
1338 // If no central directory, append new zip to end of file
1339 err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
1340 }
1341 }
1342 else
1343 {
1344 zip->cd_start_pos = zip->cd_offset;
1345 }
1346 }
1347
1348 if (err != MZ_OK)
1349 {
1350 mz_zip_close(zip);
1351 return err;
1352 }
1353
1354 // Memory streams used to store variable length file info data
1355 mz_stream_mem_create(&zip->file_info_stream);
1356 mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1357
1358 mz_stream_mem_create(&zip->local_file_info_stream);
1359 mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
1360
1361 zip->open_mode = mode;
1362
1363 return err;
1364}
1365
1366int32_t mz_zip_close(void *handle)
1367{
1368 mz_zip *zip = (mz_zip *)handle;
1369 int32_t err = MZ_OK;
1370
1371 if (zip == NULL)
1372 return MZ_PARAM_ERROR;
1373
1374 mz_zip_print("Zip - Close\n");
1375
1376 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001377 err = mz_zip_entry_close(handle);
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001378
1379 if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE))
1380 err = mz_zip_write_cd(handle);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001381
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001382 if (zip->cd_mem_stream != NULL)
1383 {
1384 mz_stream_close(zip->cd_mem_stream);
1385 mz_stream_delete(&zip->cd_mem_stream);
1386 }
1387
1388 if (zip->file_info_stream != NULL)
1389 {
1390 mz_stream_mem_close(zip->file_info_stream);
1391 mz_stream_mem_delete(&zip->file_info_stream);
1392 }
1393 if (zip->local_file_info_stream != NULL)
1394 {
1395 mz_stream_mem_close(zip->local_file_info_stream);
1396 mz_stream_mem_delete(&zip->local_file_info_stream);
1397 }
1398
1399 if (zip->comment)
1400 {
1401 MZ_FREE(zip->comment);
1402 zip->comment = NULL;
1403 }
1404
1405 zip->stream = NULL;
1406 zip->cd_stream = NULL;
1407
1408 return err;
1409}
1410
1411int32_t mz_zip_get_comment(void *handle, const char **comment)
1412{
1413 mz_zip *zip = (mz_zip *)handle;
1414 if (zip == NULL || comment == NULL)
1415 return MZ_PARAM_ERROR;
1416 if (zip->comment == NULL)
1417 return MZ_EXIST_ERROR;
1418 *comment = zip->comment;
1419 return MZ_OK;
1420}
1421
1422int32_t mz_zip_set_comment(void *handle, const char *comment)
1423{
1424 mz_zip *zip = (mz_zip *)handle;
1425 uint16_t comment_size = 0;
1426 if (zip == NULL || comment == NULL)
1427 return MZ_PARAM_ERROR;
1428 if (zip->comment != NULL)
1429 MZ_FREE(zip->comment);
1430 comment_size = (uint16_t)(strlen(comment) + 1);
1431 zip->comment = (char *)MZ_ALLOC(comment_size);
1432 if (zip->comment == NULL)
1433 return MZ_MEM_ERROR;
1434 strncpy(zip->comment, comment, comment_size - 1);
1435 zip->comment[comment_size - 1] = 0;
1436 return MZ_OK;
1437}
1438
1439int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
1440{
1441 mz_zip *zip = (mz_zip *)handle;
1442 if (zip == NULL || version_madeby == NULL)
1443 return MZ_PARAM_ERROR;
1444 *version_madeby = zip->version_madeby;
1445 return MZ_OK;
1446}
1447
1448int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
1449{
1450 mz_zip *zip = (mz_zip *)handle;
1451 if (zip == NULL)
1452 return MZ_PARAM_ERROR;
1453 zip->version_madeby = version_madeby;
1454 return MZ_OK;
1455}
1456
1457int32_t mz_zip_get_stream(void *handle, void **stream)
1458{
1459 mz_zip *zip = (mz_zip *)handle;
1460 if (zip == NULL || stream == NULL)
1461 return MZ_PARAM_ERROR;
1462 *stream = zip->stream;
1463 if (*stream == NULL)
1464 return MZ_EXIST_ERROR;
1465 return MZ_OK;
1466}
1467
1468int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream)
1469{
1470 mz_zip *zip = (mz_zip *)handle;
1471 if (zip == NULL || cd_stream == NULL)
1472 return MZ_PARAM_ERROR;
1473 zip->cd_stream = cd_stream;
1474 zip->cd_start_pos = cd_start_pos;
1475 return MZ_OK;
1476}
1477
1478int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream)
1479{
1480 mz_zip *zip = (mz_zip *)handle;
1481 if (zip == NULL || cd_mem_stream == NULL)
1482 return MZ_PARAM_ERROR;
1483 *cd_mem_stream = zip->cd_mem_stream;
1484 if (*cd_mem_stream == NULL)
1485 return MZ_EXIST_ERROR;
1486 return MZ_OK;
1487}
1488
1489static int32_t mz_zip_entry_close_int(void *handle)
1490{
1491 mz_zip *zip = (mz_zip *)handle;
1492
1493 if (zip->crypt_stream != NULL)
1494 mz_stream_delete(&zip->crypt_stream);
1495 zip->crypt_stream = NULL;
1496 if (zip->compress_stream != NULL)
1497 mz_stream_delete(&zip->compress_stream);
1498 zip->compress_stream = NULL;
1499
1500 zip->entry_opened = 0;
1501
1502 return MZ_OK;
1503}
1504
Nathan Moinvaziri26308b22018-08-08 09:40:15 -07001505static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress_level, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001506{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001507 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001508 int64_t max_total_in = 0;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001509 int64_t header_size = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001510 int64_t footer_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001511 int32_t err = MZ_OK;
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001512 uint8_t use_crypt = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001513
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001514 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001515 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001516
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001517 switch (zip->file_info.compression_method)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001518 {
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001519 case MZ_COMPRESS_METHOD_STORE:
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001520 case MZ_COMPRESS_METHOD_DEFLATE:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001521#ifdef HAVE_BZIP2
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001522 case MZ_COMPRESS_METHOD_BZIP2:
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001523#endif
Nathan Moinvaziri566dfe02018-10-26 00:36:30 -07001524#ifdef HAVE_LZMA
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001525 case MZ_COMPRESS_METHOD_LZMA:
1526#endif
1527 err = MZ_OK;
1528 break;
1529 default:
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001530 return MZ_SUPPORT_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001531 }
1532
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001533 zip->entry_raw = raw;
1534
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001535 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password != NULL))
1536 {
1537 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
1538 {
1539 // Encrypt only when we are not trying to write raw and password is supplied.
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001540 if (!zip->entry_raw)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001541 use_crypt = 1;
1542 }
1543 else if (zip->open_mode & MZ_OPEN_MODE_READ)
1544 {
1545 // Decrypt only when password is supplied. Don't error when password
1546 // is not supplied as we may want to read the raw encrypted data.
1547 use_crypt = 1;
1548 }
1549 }
1550
1551 if ((err == MZ_OK) && (use_crypt))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001552 {
1553#ifdef HAVE_AES
1554 if (zip->file_info.aes_version)
1555 {
Nathan Moinvaziri21a31022018-10-24 09:50:16 -07001556 mz_stream_wzaes_create(&zip->crypt_stream);
1557 mz_stream_wzaes_set_password(zip->crypt_stream, password);
1558 mz_stream_wzaes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001559 }
1560 else
1561#endif
1562 {
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001563#ifdef HAVE_PKCRYPT
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001564 uint8_t verify1 = 0;
1565 uint8_t verify2 = 0;
1566
1567 // Info-ZIP modification to ZipCrypto format:
1568 // If bit 3 of the general purpose bit flag is set, it uses high byte of 16-bit File Time.
1569
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001570 if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
1571 {
1572 uint32_t dos_date = 0;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07001573
Nathan Moinvaziri18a30652017-12-07 06:59:53 -08001574 dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
1575
1576 verify1 = (uint8_t)((dos_date >> 16) & 0xff);
1577 verify2 = (uint8_t)((dos_date >> 8) & 0xff);
1578 }
1579 else
1580 {
1581 verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
1582 verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
1583 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001584
Nathan Moinvaziri0e5c9dc2018-05-23 20:21:48 -07001585 mz_stream_pkcrypt_create(&zip->crypt_stream);
1586 mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
1587 mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001588#endif
1589 }
1590 }
1591
1592 if (err == MZ_OK)
1593 {
1594 if (zip->crypt_stream == NULL)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001595 mz_stream_raw_create(&zip->crypt_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001596
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001597 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001598
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001599 err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001600 }
1601
1602 if (err == MZ_OK)
1603 {
Nathan Moinvaziria64c9192018-08-08 17:47:12 -07001604 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001605 mz_stream_raw_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001606#ifdef HAVE_ZLIB
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001607 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001608 mz_stream_zlib_create(&zip->compress_stream);
Andy Maloney3909c232018-05-22 09:02:09 -04001609#endif
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001610#ifdef HAVE_BZIP2
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001611 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001612 mz_stream_bzip_create(&zip->compress_stream);
1613#endif
1614#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001615 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001616 mz_stream_lzma_create(&zip->compress_stream);
1617#endif
1618 else
1619 err = MZ_PARAM_ERROR;
1620 }
1621
1622 if (err == MZ_OK)
1623 {
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001624 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001625 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001626 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001627 }
1628 else
1629 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001630 if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE || zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001631 {
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001632 max_total_in = zip->file_info.compressed_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001633 mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
1634
1635 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
1636 max_total_in -= header_size;
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001637 if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
1638 max_total_in -= footer_size;
Nathan Moinvazirifa8105b2018-07-08 19:05:00 -07001639
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001640 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001641 }
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001642 if ((zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA) && (zip->file_info.flag & MZ_ZIP_FLAG_LZMA_EOS_MARKER) == 0)
juanii55bcdaf2018-02-11 20:55:57 -03001643 {
1644 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
1645 mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
1646 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001647 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001648
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001649 mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
1650
1651 err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
1652 }
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001653
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001654 if (err == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001655 {
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001656 zip->entry_opened = 1;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001657 zip->entry_crc32 = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001658 }
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001659 else
1660 {
1661 mz_zip_entry_close_int(handle);
1662 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001663
1664 return err;
1665}
1666
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001667int32_t mz_zip_entry_is_open(void *handle)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001668{
1669 mz_zip *zip = (mz_zip *)handle;
1670 if (zip == NULL)
1671 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001672 if (zip->entry_opened == 0)
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001673 return MZ_EXIST_ERROR;
1674 return MZ_OK;
1675}
1676
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001677int32_t mz_zip_entry_is_dir(void *handle)
Nathan Moinvaziri829ffb52018-08-14 14:00:16 -07001678{
1679 mz_zip *zip = (mz_zip *)handle;
1680 int32_t filename_length = 0;
1681
1682 if (zip == NULL)
1683 return MZ_PARAM_ERROR;
1684 if (zip->entry_scanned == 0)
1685 return MZ_PARAM_ERROR;
1686 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
1687 return MZ_OK;
1688
1689 filename_length = (int32_t )strlen(zip->file_info.filename);
1690 if (filename_length > 0)
1691 {
1692 if ((zip->file_info.filename[filename_length - 1] == '/') ||
1693 (zip->file_info.filename[filename_length - 1] == '\\'))
1694 return MZ_OK;
1695 }
1696 return MZ_EXIST_ERROR;
1697}
1698
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001699int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001700{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001701 mz_zip *zip = (mz_zip *)handle;
1702 int32_t err = MZ_OK;
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001703 int32_t err_shift = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001704
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001705#if defined(MZ_ZIP_NO_ENCRYPTION)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001706 if (password != NULL)
1707 return MZ_PARAM_ERROR;
1708#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001709 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001710 return MZ_PARAM_ERROR;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001711 if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001712 return MZ_PARAM_ERROR;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001713 if (zip->entry_scanned == 0)
1714 return MZ_PARAM_ERROR;
Nathan Moinvaziri0f09a002018-04-23 18:48:30 -07001715 if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) && (!raw))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001716 return MZ_PARAM_ERROR;
1717
Anand K Mistrycf622bb2018-11-09 11:32:42 +11001718 // Guard against seek overflows.
1719 if ((zip->disk_offset_shift > 0) &&
1720 (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
1721 return MZ_FORMAT_ERROR;
1722
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001723 if (zip->file_info.disk_number == zip->disk_number_with_cd)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001724 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
1725 else
Nathan Moinvazirifd039e32017-10-22 14:40:39 -07001726 mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001727
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001728 mz_zip_print("Zip - Entry - Read open (disk %d "PRId16" offset %lld)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001729 zip->file_info.disk_number, zip->file_info.disk_offset);
1730
Nathan Moinvaziriee614ff2018-07-12 12:36:33 -07001731 err = mz_stream_seek(zip->stream, zip->file_info.disk_offset + zip->disk_offset_shift, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001732 if (err == MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001733 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 -07001734
Nathan Moinvaziriaacf8732018-11-06 09:38:46 -08001735 if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0)
1736 {
1737 // Perhaps we didn't compensated correctly for incorrect cd offset
1738 err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
1739 if (err_shift == MZ_OK)
1740 err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
1741 if (err_shift == MZ_OK)
1742 {
1743 zip->disk_offset_shift = 0;
1744 err = err_shift;
1745 }
1746 }
1747
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001748#ifdef MZ_ZIP_NO_DECOMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001749 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001750 err = MZ_SUPPORT_ERROR;
Viktor Szakatse7215072018-09-04 15:06:53 +00001751#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001752 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001753 err = mz_zip_entry_open_int(handle, raw, 0, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001754
1755 return err;
1756}
1757
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001758int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int16_t compress_level, uint8_t raw, const char *password)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001759{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001760 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001761 int64_t disk_number = 0;
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001762 uint8_t is_dir = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001763 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001764
Nathan Moinvaziri40427632018-07-22 11:12:40 -07001765#if defined(MZ_ZIP_NO_ENCRYPTION)
tz-lomb1b25802017-11-10 15:03:02 +03001766 if (password != NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001767 return MZ_PARAM_ERROR;
1768#endif
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001769 if (zip == NULL || file_info == NULL || file_info->filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001770 return MZ_PARAM_ERROR;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001771
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001772 if (mz_zip_entry_is_open(handle) == MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001773 {
1774 err = mz_zip_entry_close(handle);
1775 if (err != MZ_OK)
1776 return err;
1777 }
1778
1779 memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001780
1781 mz_zip_print("Zip - Entry - Write open - %s (level %"PRId16" raw %"PRId8")\n",
1782 zip->file_info.filename, compress_level, raw);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001783
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001784 mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001785 mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
1786
1787 // Copy filename, extrafield, and comment internally
1788 if (file_info->filename != NULL)
1789 {
Nathan Moinvaziric714ef62018-10-25 22:01:02 -07001790 mz_stream_mem_get_buffer_at_current(zip->file_info_stream, (const void **)&zip->file_info.filename);
Nathan Moinvazirica014e62018-08-15 07:31:12 -07001791 mz_stream_write_chars(zip->file_info_stream, file_info->filename, 1);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001792 }
1793 if (file_info->extrafield != NULL)
1794 {
Nathan Moinvaziri6bde0342018-10-25 22:05:13 -07001795 mz_stream_mem_get_buffer_at_current(zip->file_info_stream, (const void **)&zip->file_info.extrafield);
Nathan Moinvazirica014e62018-08-15 07:31:12 -07001796 mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001797 }
1798 if (file_info->comment != NULL)
1799 {
Nathan Moinvaziric714ef62018-10-25 22:01:02 -07001800 mz_stream_mem_get_buffer_at_current(zip->file_info_stream, (const void **)&zip->file_info.comment);
Nathan Moinvazirica014e62018-08-15 07:31:12 -07001801 mz_stream_write_chars(zip->file_info_stream, file_info->comment, 1);
Nathan Moinvazirif5cc4c02018-08-14 17:46:36 -07001802 }
1803
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001804 if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001805 {
1806 if ((compress_level == 8) || (compress_level == 9))
1807 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
1808 if (compress_level == 2)
1809 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
1810 if (compress_level == 1)
1811 zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
1812 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001813#ifdef HAVE_LZMA
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001814 else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001815 zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001816#endif
Nathan Moinvaziri91a76f62017-11-10 07:39:06 -08001817
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001818 if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
1819 is_dir = 1;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001820
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001821 if (!is_dir)
1822 {
1823 zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
1824 if (password != NULL)
1825 zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
1826 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001827
juaniib8887e92018-02-14 00:51:05 -03001828 mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
1829 zip->file_info.disk_number = (uint32_t)disk_number;
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001830
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001831 zip->file_info.disk_offset = mz_stream_tell(zip->stream);
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001832 zip->file_info.crc = 0;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001833 zip->file_info.compressed_size = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001834
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001835#ifdef HAVE_AES
1836 if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
1837 zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
1838#endif
1839
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001840 if ((compress_level == 0) || (is_dir))
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001841 zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
Viktor Szakats2884e672018-04-30 08:12:13 +00001842
Nathan Moinvaziria6d1f662018-07-22 10:35:49 -07001843#ifdef MZ_ZIP_NO_COMPRESSION
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001844 if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001845 err = MZ_SUPPORT_ERROR;
1846#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001847 if (err == MZ_OK)
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07001848 err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001849 if (err == MZ_OK)
Nathan Moinvazirifa146ad2018-08-07 23:50:31 -07001850 err = mz_zip_entry_open_int(handle, raw, compress_level, password);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001851
1852 return err;
1853}
1854
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001855int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001856{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001857 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001858 int32_t read = 0;
1859
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001860 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001861 return MZ_PARAM_ERROR;
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07001862 if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) // zlib limitation
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001863 return MZ_PARAM_ERROR;
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07001864 if (len == 0)
1865 return MZ_PARAM_ERROR;
1866
Nathan Moinvazirieb80cd92018-07-11 16:45:09 -07001867 if (zip->file_info.compressed_size == 0)
1868 return 0;
1869
Nathan Moinvazirid7814e92018-07-11 14:54:14 -07001870 // Read entire entry even if uncompressed_size = 0, otherwise
1871 // aes encryption validation will fail if compressed_size > 0
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001872 read = mz_stream_read(zip->compress_stream, buf, len);
1873 if (read > 0)
1874 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001875
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001876 mz_zip_print("Zip - Entry - Read - %d (max %d)\n", read, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001877
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001878 return read;
1879}
1880
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001881int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001882{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001883 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001884 int32_t written = 0;
1885
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001886 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001887 return MZ_PARAM_ERROR;
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001888 written = mz_stream_write(zip->compress_stream, buf, len);
1889 if (written > 0)
1890 zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001891
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001892 mz_zip_print("Zip - Entry - Write - %d (max %d)\n", written, len);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001893
Nathan Moinvaziri0a9282d2018-06-19 11:59:07 -07001894 return written;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001895}
1896
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001897int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001898{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001899 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07001900
1901 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001902 return MZ_PARAM_ERROR;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07001903
1904 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
1905 {
1906 if (!zip->entry_scanned)
1907 return MZ_PARAM_ERROR;
1908 }
1909
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001910 *file_info = &zip->file_info;
1911 return MZ_OK;
1912}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001913
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001914int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001915{
1916 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001917 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001918 return MZ_PARAM_ERROR;
1919 *local_file_info = &zip->local_file_info;
1920 return MZ_OK;
1921}
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001922
Nathan Moinvaziri915c5132018-10-26 20:00:52 -07001923int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size)
1924{
1925 mz_zip *zip = (mz_zip *)handle;
1926
1927 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
1928 return MZ_PARAM_ERROR;
1929
1930 zip->file_info.extrafield = extrafield;
1931 zip->file_info.extrafield_size = extrafield_size;
1932 return MZ_OK;
1933}
1934
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07001935int32_t mz_zip_entry_close(void *handle)
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07001936{
1937 return mz_zip_entry_close_raw(handle, 0, 0);
1938}
1939
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07001940int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32)
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001941{
1942 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07001943 int64_t compressed_size = 0;
Nathan Moinvaziri8871f6c2018-07-08 19:23:56 -07001944 int64_t total_in = 0;
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07001945 int32_t err = MZ_OK;
1946
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07001947 if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001948 return MZ_PARAM_ERROR;
Nathan Moinvaziri915c5132018-10-26 20:00:52 -07001949
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001950 mz_stream_close(zip->compress_stream);
Nathan Moinvaziri6ac2ff42018-07-21 14:29:29 -07001951
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07001952 if (!zip->entry_raw)
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001953 crc32 = zip->entry_crc32;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001954
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001955 mz_zip_print("Zip - Entry - Close (ucs %lld crc 0x%08x)\n", compressed_size, crc32);
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001956
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001957 if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001958 {
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001959 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
Nathan Moinvaziri413822a2018-10-20 09:45:07 -07001960
1961 // If entire entry was not read verification will fail
1962 if ((total_in > 0) && (!zip->entry_raw))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001963 {
Nathan Moinvaziri413822a2018-10-20 09:45:07 -07001964#ifdef HAVE_AES
Nathan Moinvaziri413822a2018-10-20 09:45:07 -07001965 // AES zip version AE-1 will expect a valid crc as well
1966 if (zip->file_info.aes_version <= 0x0001)
1967#endif
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001968 {
1969 if (crc32 != zip->file_info.crc)
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001970 {
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08001971 mz_zip_print("Zip - Entry - Crc failed (actual 0x%08x expected 0x%08x)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001972 crc32, zip->file_info.crc);
1973
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001974 err = MZ_CRC_ERROR;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001975 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001976 }
1977 }
1978 }
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001979
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07001980 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
Nathan Moinvaziri9eb113d2018-08-08 17:38:38 -07001981 if (!zip->entry_raw)
Nathan Moinvazirie63d2312018-11-03 19:45:41 -07001982 mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001983
1984 if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
1985 {
1986 mz_stream_set_base(zip->crypt_stream, zip->stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001987 err = mz_stream_close(zip->crypt_stream);
1988
Nathan Moinvaziria66cc312017-10-18 16:51:10 -07001989 mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001990 }
1991
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07001992 if (zip->open_mode & MZ_OPEN_MODE_WRITE)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001993 {
Nathan Moinvaziric565fa82018-10-19 08:48:33 -07001994 if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07001995 {
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08001996 if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
1997 err = mz_zip_entry_write_descriptor(zip->stream, 0, compressed_size, 0);
1998 else
1999 err = mz_zip_entry_write_descriptor(zip->stream, crc32, compressed_size, uncompressed_size);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002000 }
2001
2002 zip->file_info.crc = crc32;
2003 zip->file_info.compressed_size = compressed_size;
2004 zip->file_info.uncompressed_size = uncompressed_size;
2005
Nathan Moinvazirieffc1422018-11-08 15:24:17 -08002006 mz_zip_print("Zip - Entry - Write cd (ucs %lld cs %lld crc 0x%08x)\n",
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002007 uncompressed_size, compressed_size, crc32);
2008
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002009 if (err == MZ_OK)
Nathan Moinvazirifa620e42018-10-20 10:37:10 -07002010 err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002011
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002012 zip->number_entry += 1;
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002013 }
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002014
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002015 mz_zip_entry_close_int(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002016
2017 return err;
2018}
2019
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002020static int32_t mz_zip_goto_next_entry_int(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002021{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002022 mz_zip *zip = (mz_zip *)handle;
2023 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002024
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002025 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002026 return MZ_PARAM_ERROR;
2027
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002028 zip->entry_scanned = 0;
2029
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002030 mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
Viktor Szakats915b82e2018-04-24 10:02:39 +00002031
Nathan Moinvaziri79ac3382017-10-23 17:22:36 -07002032 err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002033 if (err == MZ_OK)
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002034 err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002035 if (err == MZ_OK)
2036 zip->entry_scanned = 1;
2037 return err;
2038}
2039
Nathan Moinvaziric565fa82018-10-19 08:48:33 -07002040int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry)
2041{
2042 mz_zip *zip = (mz_zip *)handle;
2043 if (zip == NULL)
2044 return MZ_PARAM_ERROR;
2045 zip->number_entry = number_entry;
2046 return MZ_OK;
2047}
2048
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002049int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002050{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002051 mz_zip *zip = (mz_zip *)handle;
2052 if (zip == NULL || number_entry == NULL)
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002053 return MZ_PARAM_ERROR;
Nathan Moinvaziri6e21e862017-10-19 09:57:54 -07002054 *number_entry = zip->number_entry;
2055 return MZ_OK;
2056}
2057
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002058int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd)
2059{
2060 mz_zip *zip = (mz_zip *)handle;
2061 if (zip == NULL)
2062 return MZ_PARAM_ERROR;
2063 zip->disk_number_with_cd = disk_number_with_cd;
2064 return MZ_OK;
2065}
2066
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002067int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
juanii566b9782018-02-10 15:07:54 -03002068{
2069 mz_zip *zip = (mz_zip *)handle;
2070 if (zip == NULL || disk_number_with_cd == NULL)
2071 return MZ_PARAM_ERROR;
2072 *disk_number_with_cd = zip->disk_number_with_cd;
2073 return MZ_OK;
2074}
2075
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002076int64_t mz_zip_get_entry(void *handle)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002077{
2078 mz_zip *zip = (mz_zip *)handle;
2079
2080 if (zip == NULL)
2081 return MZ_PARAM_ERROR;
2082
2083 return zip->cd_current_pos;
2084}
2085
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002086int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002087{
2088 mz_zip *zip = (mz_zip *)handle;
2089
2090 if (zip == NULL)
2091 return MZ_PARAM_ERROR;
2092
2093 if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
2094 return MZ_PARAM_ERROR;
2095
2096 zip->cd_current_pos = cd_pos;
2097
2098 return mz_zip_goto_next_entry_int(handle);
2099}
2100
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002101int32_t mz_zip_goto_first_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002102{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002103 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002104
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002105 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002106 return MZ_PARAM_ERROR;
2107
Nathan Moinvaziricda36002017-10-21 09:37:18 -07002108 zip->cd_current_pos = zip->cd_start_pos;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002109
2110 return mz_zip_goto_next_entry_int(handle);
2111}
2112
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002113int32_t mz_zip_goto_next_entry(void *handle)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002114{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002115 mz_zip *zip = (mz_zip *)handle;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002116
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002117 if (zip == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002118 return MZ_PARAM_ERROR;
2119
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002120 zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002121 zip->file_info.extrafield_size + zip->file_info.comment_size;
2122
2123 return mz_zip_goto_next_entry_int(handle);
2124}
2125
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002126int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002127{
Nathan Moinvaziri5f9e5d32017-10-20 07:59:39 -07002128 mz_zip *zip = (mz_zip *)handle;
2129 int32_t err = MZ_OK;
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002130 int32_t result = 0;
2131
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002132 if (zip == NULL || filename == NULL)
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002133 return MZ_PARAM_ERROR;
2134
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002135 // If we are already on the current entry, no need to search
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002136 if ((zip->entry_scanned) && (zip->file_info.filename != NULL))
2137 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002138 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
Nathan Moinvaziri77687f32018-08-09 16:29:26 -07002139 if (result == 0)
2140 return MZ_OK;
2141 }
2142
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002143 // Search all entries starting at the first
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002144 err = mz_zip_goto_first_entry(handle);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002145 while (err == MZ_OK)
2146 {
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002147 result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
2148 if (result == 0)
2149 return MZ_OK;
2150
2151 err = mz_zip_goto_next_entry(handle);
2152 }
2153
2154 return err;
2155}
2156
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002157int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002158{
2159 mz_zip *zip = (mz_zip *)handle;
2160 int32_t err = MZ_OK;
2161 int32_t result = 0;
2162
2163 // Search first entry looking for match
2164 err = mz_zip_goto_first_entry(handle);
2165 if (err != MZ_OK)
2166 return err;
2167
2168 result = cb(handle, userdata, &zip->file_info);
2169 if (result == 0)
2170 return MZ_OK;
2171
2172 return mz_zip_locate_next_entry(handle, userdata, cb);
2173}
2174
Nathan Moinvaziri590e5902018-08-17 11:10:35 -07002175int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002176{
2177 mz_zip *zip = (mz_zip *)handle;
2178 int32_t err = MZ_OK;
2179 int32_t result = 0;
2180
2181 // Search next entries looking for match
2182 err = mz_zip_goto_next_entry(handle);
2183 while (err == MZ_OK)
2184 {
2185 result = cb(handle, userdata, &zip->file_info);
Nathan Moinvaziri8f67ff92017-10-17 23:22:29 -07002186 if (result == 0)
2187 return MZ_OK;
2188
2189 err = mz_zip_goto_next_entry(handle);
2190 }
2191
2192 return err;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002193}
2194
2195/***************************************************************************/
2196
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002197int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002198{
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002199 uint32_t posix_attrib = 0;
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002200 uint8_t system = MZ_HOST_SYSTEM(version_madeby);
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002201 int32_t err = MZ_OK;
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002202
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002203 err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
2204 if (err == MZ_OK)
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002205 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002206 if ((posix_attrib & 0170000) == 0040000) // S_ISDIR
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002207 return MZ_OK;
2208 }
2209
2210 return MZ_EXIST_ERROR;
2211}
2212
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002213int32_t mz_zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys, uint32_t *target_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002214{
2215 if (target_attrib == NULL)
2216 return MZ_PARAM_ERROR;
2217
2218 *target_attrib = 0;
2219
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002220 if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002221 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002222 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002223 {
2224 *target_attrib = src_attrib;
2225 return MZ_OK;
2226 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002227 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002228 return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
2229 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002230 else if ((src_sys == MZ_HOST_SYSTEM_UNIX) || (src_sys == MZ_HOST_SYSTEM_OSX_DARWIN))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002231 {
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002232 if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002233 {
Nathan Moinvaziri5e6bac52018-10-08 23:47:02 -07002234 // If high bytes are set, it contains unix specific attributes
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002235 if ((src_attrib >> 16) != 0)
2236 src_attrib >>= 16;
2237
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002238 *target_attrib = src_attrib;
2239 return MZ_OK;
2240 }
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002241 if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002242 return mz_zip_attrib_posix_to_win32(src_attrib, target_attrib);
2243 }
2244
2245 return MZ_SUPPORT_ERROR;
2246}
2247
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002248int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002249{
2250 if (win32_attrib == NULL)
2251 return MZ_PARAM_ERROR;
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002252
2253 *win32_attrib = 0;
2254
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002255 // S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH
2256 if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002257 *win32_attrib |= 0x01; // FILE_ATTRIBUTE_READONLY
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002258 // S_IFDIR
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002259 if ((posix_attrib & 0170000) == 0040000)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002260 *win32_attrib |= 0x10; // FILE_ATTRIBUTE_DIRECTORY
Nathan Moinvazirida4b58f2018-08-20 18:21:26 -07002261 // S_IFREG
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002262 else
2263 *win32_attrib |= 0x80; // FILE_ATTRIBUTE_NORMAL
2264
2265 return MZ_OK;
2266}
2267
Nathan Moinvaziri2bb21d72018-10-08 21:47:15 -07002268int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib)
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002269{
2270 if (posix_attrib == NULL)
2271 return MZ_PARAM_ERROR;
2272
2273 *posix_attrib = 0000444; // S_IRUSR | S_IRGRP | S_IROTH
2274 // FILE_ATTRIBUTE_READONLY
2275 if ((win32_attrib & 0x01) == 0)
2276 *posix_attrib |= 0000222; // S_IWUSR | S_IWGRP | S_IWOTH
2277 // FILE_ATTRIBUTE_DIRECTORY
2278 if ((win32_attrib & 0x10) == 0x10)
2279 *posix_attrib |= 0040111; // S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002280 else
2281 *posix_attrib |= 0100000; // S_IFREG
2282
2283 return MZ_OK;
2284}
2285
Nathan Moinvaziribb3b75b2018-05-02 10:48:51 -07002286/***************************************************************************/
2287
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002288int32_t mz_zip_extrafield_find(void *stream, uint16_t type, uint16_t *length)
2289{
2290 int32_t err = MZ_OK;
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002291 uint16_t field_type = 0;
2292 uint16_t field_length = 0;
2293
2294 do
2295 {
2296 err = mz_stream_read_uint16(stream, &field_type);
2297 if (err == MZ_OK)
2298 err = mz_stream_read_uint16(stream, &field_length);
2299 if (err != MZ_OK)
2300 break;
2301
2302 if (type == field_type)
2303 {
2304 if (length != NULL)
2305 *length = field_length;
2306 return MZ_OK;
2307 }
2308
Nathan Moinvaziri0dfcdf92018-11-07 20:33:47 -08002309 err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR);
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002310 }
2311 while (err == MZ_OK);
2312
2313 return MZ_EXIST_ERROR;
2314}
2315
2316int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length)
2317{
2318 int32_t err = MZ_OK;
2319 if (type == NULL || length == NULL)
2320 return MZ_PARAM_ERROR;
2321 err = mz_stream_read_uint16(stream, type);
2322 if (err == MZ_OK)
2323 err = mz_stream_read_uint16(stream, length);
2324 return err;
2325}
2326
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002327int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length)
Nathan Moinvazirie1f68fc2018-10-23 09:04:04 -07002328{
2329 int32_t err = MZ_OK;
2330 err = mz_stream_write_uint16(stream, type);
2331 if (err == MZ_OK)
2332 err = mz_stream_write_uint16(stream, length);
2333 return err;
2334}
2335
2336/***************************************************************************/
2337
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002338static int32_t mz_zip_invalid_date(const struct tm *ptm)
2339{
2340#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002341 return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || // 1980-based year, allow 80 extra
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002342 !datevalue_in_range(0, 11, ptm->tm_mon) ||
2343 !datevalue_in_range(1, 31, ptm->tm_mday) ||
2344 !datevalue_in_range(0, 23, ptm->tm_hour) ||
2345 !datevalue_in_range(0, 59, ptm->tm_min) ||
2346 !datevalue_in_range(0, 59, ptm->tm_sec));
2347#undef datevalue_in_range
2348}
2349
2350static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
2351{
2352 uint64_t date = (uint64_t)(dos_date >> 16);
2353
Nathan Moinvaziri1d6d1832018-11-10 09:03:55 -08002354 ptm->tm_mday = (uint16_t)(date & 0x1f);
2355 ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
2356 ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
2357 ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
2358 ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
2359 ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002360 ptm->tm_isdst = -1;
2361}
2362
2363int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
2364{
2365 if (ptm == NULL)
2366 return MZ_PARAM_ERROR;
2367
2368 mz_zip_dosdate_to_raw_tm(dos_date, ptm);
2369
2370 if (mz_zip_invalid_date(ptm))
2371 {
2372 // Invalid date stored, so don't return it
2373 memset(ptm, 0, sizeof(struct tm));
2374 return MZ_FORMAT_ERROR;
2375 }
2376 return MZ_OK;
2377}
2378
2379time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
2380{
2381 struct tm ptm;
2382 mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
2383 return mktime(&ptm);
2384}
2385
2386int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
2387{
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002388 struct tm *ltm = NULL;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002389 if (ptm == NULL)
2390 return MZ_PARAM_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002391 ltm = localtime(&unix_time); // Returns a 1900-based year
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002392 if (ltm == NULL)
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002393 {
2394 // Invalid date stored, so don't return it
2395 memset(ptm, 0, sizeof(struct tm));
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002396 return MZ_INTERNAL_ERROR;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002397 }
Nathan Moinvaziri17fcdcd2017-10-26 08:13:13 -07002398 memcpy(ptm, ltm, sizeof(struct tm));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002399 return MZ_OK;
2400}
2401
2402uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
2403{
2404 struct tm ptm;
2405 mz_zip_time_t_to_tm(unix_time, &ptm);
2406 return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
2407}
2408
2409uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
2410{
Nathan Moinvazirie33916b2018-05-01 13:45:08 -07002411 struct tm fixed_tm;
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002412
2413 // Years supported:
2414
2415 // [00, 79] (assumed to be between 2000 and 2079)
2416 // [80, 207] (assumed to be between 1980 and 2107, typical output of old
2417 // software that does 'year-1900' to get a double digit year)
2418 // [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.)
2419
2420 memcpy(&fixed_tm, ptm, sizeof(struct tm));
2421 if (fixed_tm.tm_year >= 1980) // range [1980, 2107]
2422 fixed_tm.tm_year -= 1980;
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002423 else if (fixed_tm.tm_year >= 80) // range [80, 207]
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002424 fixed_tm.tm_year -= 80;
2425 else // range [00, 79]
2426 fixed_tm.tm_year += 20;
2427
Viktor Szakatsce26dba2018-04-22 19:30:34 +00002428 if (mz_zip_invalid_date(&fixed_tm))
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002429 return 0;
2430
Anand K Mistry57b65f82018-11-09 10:52:19 +11002431 return (((uint32_t)fixed_tm.tm_mday + (32 * ((uint32_t)fixed_tm.tm_mon + 1)) + (512 * (uint32_t)fixed_tm.tm_year)) << 16) |
Nathan Moinvaziri904c4082018-10-08 23:31:21 -07002432 (((uint32_t)fixed_tm.tm_sec / 2) + (32 * (uint32_t)fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
Nathan Moinvaziri9eab88e2017-10-22 14:32:37 -07002433}
2434
2435int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
2436{
2437 *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
2438 return MZ_OK;
2439}
2440
2441int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
2442{
2443 *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
2444 return MZ_OK;
2445}
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002446
Nathan Moinvaziri4b8e4b32018-08-20 09:06:23 -07002447/***************************************************************************/
2448
Nathan Moinvaziri9a170d42018-08-13 23:07:42 -07002449int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case)
2450{
2451 do
2452 {
2453 if ((*path1 == '\\' && *path2 == '/') ||
2454 (*path2 == '\\' && *path1 == '/'))
2455 {
2456 // Ignore comparison of path slashes
2457 }
2458 else if (ignore_case)
2459 {
2460 if (tolower(*path1) != tolower(*path2))
2461 break;
2462 }
2463 else if (*path1 != *path2)
2464 {
2465 break;
2466 }
2467
2468 path1 += 1;
2469 path2 += 1;
2470 }
2471 while (*path1 != 0 && *path2 != 0);
2472
2473 if (ignore_case)
2474 return (int32_t)(tolower(*path1) - tolower(*path2));
2475
2476 return (int32_t)(*path1 - *path2);
2477}
2478
2479/***************************************************************************/