Line | Count | Source (jump to first uncovered line) |
1 | | /* zip.c -- Zip manipulation |
2 | | part of the minizip-ng project |
3 | | |
4 | | Copyright (C) Nathan Moinvaziri |
5 | | https://github.com/zlib-ng/minizip-ng |
6 | | Copyright (C) 2009-2010 Mathias Svensson |
7 | | Modifications for Zip64 support |
8 | | http://result42.com |
9 | | Copyright (C) 2007-2008 Even Rouault |
10 | | Modifications of Unzip for Zip64 |
11 | | Copyright (C) 1998-2010 Gilles Vollant |
12 | | https://www.winimage.com/zLibDll/minizip.html |
13 | | |
14 | | This program is distributed under the terms of the same license as zlib. |
15 | | See the accompanying LICENSE file for the full text of the license. |
16 | | */ |
17 | | |
18 | | #include "mz.h" |
19 | | #include "mz_crypt.h" |
20 | | #include "mz_strm.h" |
21 | | #ifdef HAVE_BZIP2 |
22 | | # include "mz_strm_bzip.h" |
23 | | #endif |
24 | | #ifdef HAVE_LIBCOMP |
25 | | # include "mz_strm_libcomp.h" |
26 | | #endif |
27 | | #ifdef HAVE_LZMA |
28 | | # include "mz_strm_lzma.h" |
29 | | #endif |
30 | | #include "mz_strm_mem.h" |
31 | | #ifdef HAVE_PKCRYPT |
32 | | # include "mz_strm_pkcrypt.h" |
33 | | #endif |
34 | | #ifdef HAVE_WZAES |
35 | | # include "mz_strm_wzaes.h" |
36 | | #endif |
37 | | #ifdef HAVE_ZLIB |
38 | | # include "mz_strm_zlib.h" |
39 | | #endif |
40 | | #ifdef HAVE_ZSTD |
41 | | # include "mz_strm_zstd.h" |
42 | | #endif |
43 | | |
44 | | #include "mz_zip.h" |
45 | | |
46 | | #include <ctype.h> /* tolower */ |
47 | | #include <stdio.h> /* snprintf */ |
48 | | |
49 | | #if defined(_MSC_VER) || defined(__MINGW32__) |
50 | | # define localtime_r(t1, t2) (localtime_s(t2, t1) == 0 ? t1 : NULL) |
51 | | #endif |
52 | | #if defined(_MSC_VER) && (_MSC_VER < 1900) |
53 | | # define snprintf _snprintf |
54 | | #endif |
55 | | |
56 | | /***************************************************************************/ |
57 | | |
58 | 10.7k | #define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50) |
59 | 0 | #define MZ_ZIP_MAGIC_LOCALHEADERU8 { 0x50, 0x4b, 0x03, 0x04 } |
60 | 67.0k | #define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50) |
61 | 0 | #define MZ_ZIP_MAGIC_CENTRALHEADERU8 { 0x50, 0x4b, 0x01, 0x02 } |
62 | 148k | #define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50) |
63 | 3.82k | #define MZ_ZIP_MAGIC_ENDHEADERU8 { 0x50, 0x4b, 0x05, 0x06 } |
64 | 74.7k | #define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50) |
65 | 534 | #define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50) |
66 | 0 | #define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50) |
67 | 0 | #define MZ_ZIP_MAGIC_DATADESCRIPTORU8 { 0x50, 0x4b, 0x07, 0x08 } |
68 | | |
69 | 0 | #define MZ_ZIP_SIZE_LD_ITEM (30) |
70 | 56.8k | #define MZ_ZIP_SIZE_CD_ITEM (46) |
71 | 587 | #define MZ_ZIP_SIZE_CD_LOCATOR64 (20) |
72 | 0 | #define MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR (24) |
73 | | |
74 | 0 | #define MZ_ZIP_OFFSET_CRC_SIZES (14) |
75 | 0 | #define MZ_ZIP_UNCOMPR_SIZE64_CUSHION (2 * 1024 * 1024) |
76 | | |
77 | | #ifndef MZ_ZIP_EOCD_MAX_BACK |
78 | 3.82k | #define MZ_ZIP_EOCD_MAX_BACK (1 << 20) |
79 | | #endif |
80 | | |
81 | | /***************************************************************************/ |
82 | | |
83 | | typedef struct mz_zip_s { |
84 | | mz_zip_file file_info; |
85 | | mz_zip_file local_file_info; |
86 | | |
87 | | void *stream; /* main stream */ |
88 | | void *cd_stream; /* pointer to the stream with the cd */ |
89 | | void *cd_mem_stream; /* memory stream for central directory */ |
90 | | void *compress_stream; /* compression stream */ |
91 | | void *crypt_stream; /* encryption stream */ |
92 | | void *file_info_stream; /* memory stream for storing file info */ |
93 | | void *local_file_info_stream; /* memory stream for storing local file info */ |
94 | | |
95 | | int32_t open_mode; |
96 | | uint8_t recover; |
97 | | uint8_t data_descriptor; |
98 | | |
99 | | uint32_t disk_number_with_cd; /* number of the disk with the central dir */ |
100 | | int64_t disk_offset_shift; /* correction for zips that have wrong offset start of cd */ |
101 | | |
102 | | int64_t cd_start_pos; /* pos of the first file in the central dir stream */ |
103 | | int64_t cd_current_pos; /* pos of the current file in the central dir */ |
104 | | int64_t cd_offset; /* offset of start of central directory */ |
105 | | int64_t cd_size; /* size of the central directory */ |
106 | | uint32_t cd_signature; /* signature of central directory */ |
107 | | |
108 | | uint8_t entry_scanned; /* entry header information read ok */ |
109 | | uint8_t entry_opened; /* entry is open for read/write */ |
110 | | uint8_t entry_raw; /* entry opened with raw mode */ |
111 | | uint32_t entry_crc32; /* entry crc32 */ |
112 | | |
113 | | uint64_t number_entry; |
114 | | |
115 | | uint16_t version_madeby; |
116 | | char *comment; |
117 | | } mz_zip; |
118 | | |
119 | | /***************************************************************************/ |
120 | | |
121 | | #if 0 |
122 | | # define mz_zip_print printf |
123 | | #else |
124 | | # define mz_zip_print(fmt, ...) |
125 | | #endif |
126 | | |
127 | | /***************************************************************************/ |
128 | | |
129 | | /* Locate the end of central directory */ |
130 | 3.82k | static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos) { |
131 | 3.82k | int64_t file_size = 0; |
132 | 3.82k | int64_t max_back = MZ_ZIP_EOCD_MAX_BACK; |
133 | 3.82k | uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8; |
134 | 3.82k | int32_t err = MZ_OK; |
135 | | |
136 | 3.82k | err = mz_stream_seek(stream, 0, MZ_SEEK_END); |
137 | 3.82k | if (err != MZ_OK) |
138 | 0 | return err; |
139 | | |
140 | 3.82k | file_size = mz_stream_tell(stream); |
141 | | |
142 | 3.82k | if (max_back <= 0 || max_back > file_size) |
143 | 3.81k | max_back = file_size; |
144 | | |
145 | 3.82k | return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos); |
146 | 3.82k | } |
147 | | |
148 | | /* Locate the end of central directory 64 of a zip file */ |
149 | 587 | static int32_t mz_zip_search_zip64_eocd(void *stream, const int64_t end_central_offset, int64_t *central_pos) { |
150 | 587 | int64_t offset = 0; |
151 | 587 | uint32_t value32 = 0; |
152 | 587 | int32_t err = MZ_OK; |
153 | | |
154 | 587 | *central_pos = 0; |
155 | | |
156 | | /* Zip64 end of central directory locator */ |
157 | 587 | err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET); |
158 | | /* Read locator signature */ |
159 | 587 | if (err == MZ_OK) { |
160 | 534 | err = mz_stream_read_uint32(stream, &value32); |
161 | 534 | if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64) |
162 | 51 | err = MZ_FORMAT_ERROR; |
163 | 534 | } |
164 | | /* Number of the disk with the start of the zip64 end of central directory */ |
165 | 587 | if (err == MZ_OK) |
166 | 483 | err = mz_stream_read_uint32(stream, &value32); |
167 | | /* Relative offset of the zip64 end of central directory record8 */ |
168 | 587 | if (err == MZ_OK) |
169 | 483 | err = mz_stream_read_uint64(stream, (uint64_t *)&offset); |
170 | | /* Total number of disks */ |
171 | 587 | if (err == MZ_OK) |
172 | 483 | err = mz_stream_read_uint32(stream, &value32); |
173 | | /* Goto end of central directory record */ |
174 | 587 | if (err == MZ_OK) |
175 | 483 | err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET); |
176 | | /* The signature */ |
177 | 587 | if (err == MZ_OK) { |
178 | 395 | err = mz_stream_read_uint32(stream, &value32); |
179 | 395 | if (value32 != MZ_ZIP_MAGIC_ENDHEADER64) |
180 | 48 | err = MZ_FORMAT_ERROR; |
181 | 395 | } |
182 | | |
183 | 587 | if (err == MZ_OK) |
184 | 347 | *central_pos = offset; |
185 | | |
186 | 587 | return err; |
187 | 587 | } |
188 | | |
189 | | #ifdef HAVE_PKCRYPT |
190 | | /* Get PKWARE traditional encryption verifier */ |
191 | | static uint16_t mz_zip_get_pk_verify(uint32_t dos_date, uint64_t crc, uint16_t flag) |
192 | 45.0k | { |
193 | | /* Info-ZIP modification to ZipCrypto format: if bit 3 of the general |
194 | | * purpose bit flag is set, it uses high byte of 16-bit File Time. */ |
195 | 45.0k | if (flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) |
196 | 25.8k | return ((dos_date >> 16) & 0xff) << 8 | ((dos_date >> 8) & 0xff); |
197 | 19.1k | return ((crc >> 16) & 0xff) << 8 | ((crc >> 24) & 0xff); |
198 | 45.0k | } |
199 | | #endif |
200 | | |
201 | | /* Get info about the current file in the zip file */ |
202 | 82.0k | static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file *file_info, void *file_extra_stream) { |
203 | 82.0k | uint64_t ntfs_time = 0; |
204 | 82.0k | uint32_t reserved = 0; |
205 | 82.0k | uint32_t magic = 0; |
206 | 82.0k | uint32_t dos_date = 0; |
207 | 82.0k | uint32_t field_pos = 0; |
208 | 82.0k | uint16_t field_type = 0; |
209 | 82.0k | uint16_t field_length = 0; |
210 | 82.0k | uint32_t field_length_read = 0; |
211 | 82.0k | uint16_t ntfs_attrib_id = 0; |
212 | 82.0k | uint16_t ntfs_attrib_size = 0; |
213 | 82.0k | uint16_t linkname_size; |
214 | 82.0k | uint16_t value16 = 0; |
215 | 82.0k | uint32_t value32 = 0; |
216 | 82.0k | int64_t extrafield_pos = 0; |
217 | 82.0k | int64_t comment_pos = 0; |
218 | 82.0k | int64_t linkname_pos = 0; |
219 | 82.0k | int64_t saved_pos = 0; |
220 | 82.0k | int32_t err = MZ_OK; |
221 | 82.0k | char *linkname = NULL; |
222 | | |
223 | 82.0k | memset(file_info, 0, sizeof(mz_zip_file)); |
224 | | |
225 | | /* Check the magic */ |
226 | 82.0k | err = mz_stream_read_uint32(stream, &magic); |
227 | 82.0k | if (err == MZ_END_OF_STREAM) |
228 | 7.60k | err = MZ_END_OF_LIST; |
229 | 74.4k | else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64) |
230 | 65 | err = MZ_END_OF_LIST; |
231 | 74.3k | else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER)) |
232 | 352 | err = MZ_FORMAT_ERROR; |
233 | 73.9k | else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER)) |
234 | 2.21k | err = MZ_FORMAT_ERROR; |
235 | | |
236 | | /* Read header fields */ |
237 | 82.0k | if (err == MZ_OK) { |
238 | 71.7k | if (!local) |
239 | 61.3k | err = mz_stream_read_uint16(stream, &file_info->version_madeby); |
240 | 71.7k | if (err == MZ_OK) |
241 | 71.7k | err = mz_stream_read_uint16(stream, &file_info->version_needed); |
242 | 71.7k | if (err == MZ_OK) |
243 | 71.6k | err = mz_stream_read_uint16(stream, &file_info->flag); |
244 | 71.7k | if (err == MZ_OK) |
245 | 71.6k | err = mz_stream_read_uint16(stream, &file_info->compression_method); |
246 | 71.7k | if (err == MZ_OK) { |
247 | 71.6k | err = mz_stream_read_uint32(stream, &dos_date); |
248 | 71.6k | file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date); |
249 | 71.6k | } |
250 | 71.7k | if (err == MZ_OK) |
251 | 71.5k | err = mz_stream_read_uint32(stream, &file_info->crc); |
252 | 71.7k | #ifdef HAVE_PKCRYPT |
253 | 71.7k | if (err == MZ_OK && file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) { |
254 | | /* Use dos_date from header instead of derived from time in zip extensions */ |
255 | 45.0k | file_info->pk_verify = mz_zip_get_pk_verify(dos_date, file_info->crc, file_info->flag); |
256 | 45.0k | } |
257 | 71.7k | #endif |
258 | 71.7k | if (err == MZ_OK) { |
259 | 71.5k | err = mz_stream_read_uint32(stream, &value32); |
260 | 71.5k | file_info->compressed_size = value32; |
261 | 71.5k | } |
262 | 71.7k | if (err == MZ_OK) { |
263 | 71.4k | err = mz_stream_read_uint32(stream, &value32); |
264 | 71.4k | file_info->uncompressed_size = value32; |
265 | 71.4k | } |
266 | 71.7k | if (err == MZ_OK) |
267 | 71.3k | err = mz_stream_read_uint16(stream, &file_info->filename_size); |
268 | 71.7k | if (err == MZ_OK) |
269 | 71.2k | err = mz_stream_read_uint16(stream, &file_info->extrafield_size); |
270 | 71.7k | if (!local) { |
271 | 61.3k | if (err == MZ_OK) |
272 | 60.8k | err = mz_stream_read_uint16(stream, &file_info->comment_size); |
273 | 61.3k | if (err == MZ_OK) { |
274 | 60.7k | err = mz_stream_read_uint16(stream, &value16); |
275 | 60.7k | file_info->disk_number = value16; |
276 | 60.7k | } |
277 | 61.3k | if (err == MZ_OK) |
278 | 60.7k | err = mz_stream_read_uint16(stream, &file_info->internal_fa); |
279 | 61.3k | if (err == MZ_OK) |
280 | 60.7k | err = mz_stream_read_uint32(stream, &file_info->external_fa); |
281 | 61.3k | if (err == MZ_OK) { |
282 | 60.6k | err = mz_stream_read_uint32(stream, &value32); |
283 | 60.6k | file_info->disk_offset = value32; |
284 | 60.6k | } |
285 | 61.3k | } |
286 | 71.7k | } |
287 | | |
288 | 82.0k | if (err == MZ_OK) |
289 | 71.0k | err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET); |
290 | | |
291 | | /* Copy variable length data to memory stream for later retrieval */ |
292 | 82.0k | if ((err == MZ_OK) && (file_info->filename_size > 0)) |
293 | 8.97k | err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size); |
294 | 82.0k | mz_stream_write_uint8(file_extra_stream, 0); |
295 | 82.0k | extrafield_pos = mz_stream_tell(file_extra_stream); |
296 | | |
297 | 82.0k | if ((err == MZ_OK) && (file_info->extrafield_size > 0)) |
298 | 18.8k | err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size); |
299 | 82.0k | mz_stream_write_uint8(file_extra_stream, 0); |
300 | | |
301 | 82.0k | comment_pos = mz_stream_tell(file_extra_stream); |
302 | 82.0k | if ((err == MZ_OK) && (file_info->comment_size > 0)) |
303 | 1.67k | err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size); |
304 | 82.0k | mz_stream_write_uint8(file_extra_stream, 0); |
305 | | |
306 | 82.0k | linkname_pos = mz_stream_tell(file_extra_stream); |
307 | | /* Overwrite if we encounter UNIX1 extra block */ |
308 | 82.0k | mz_stream_write_uint8(file_extra_stream, 0); |
309 | | |
310 | 82.0k | if ((err == MZ_OK) && (file_info->extrafield_size > 0)) { |
311 | | /* Seek to and parse the extra field */ |
312 | 18.6k | err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET); |
313 | | |
314 | 89.2k | while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size)) { |
315 | 70.7k | err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length); |
316 | 70.7k | if (err != MZ_OK) |
317 | 63 | break; |
318 | 70.6k | field_pos += 4; |
319 | | |
320 | | /* Don't allow field length to exceed size of remaining extrafield */ |
321 | 70.6k | if (field_length > (file_info->extrafield_size - field_pos)) |
322 | 16.5k | field_length = (uint16_t)(file_info->extrafield_size - field_pos); |
323 | | |
324 | | /* Read ZIP64 extra field */ |
325 | 70.6k | if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8)) { |
326 | 5.91k | if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX)) { |
327 | 1.47k | err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size); |
328 | 1.47k | if (file_info->uncompressed_size < 0) |
329 | 393 | err = MZ_FORMAT_ERROR; |
330 | 1.47k | } |
331 | 5.91k | if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX)) { |
332 | 1.67k | err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size); |
333 | 1.67k | if (file_info->compressed_size < 0) |
334 | 276 | err = MZ_FORMAT_ERROR; |
335 | 1.67k | } |
336 | 5.91k | if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX)) { |
337 | 1.61k | err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset); |
338 | 1.61k | if (file_info->disk_offset < 0) |
339 | 407 | err = MZ_FORMAT_ERROR; |
340 | 1.61k | } |
341 | 5.91k | if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX)) |
342 | 558 | err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number); |
343 | 5.91k | } |
344 | | /* Read NTFS extra field */ |
345 | 64.7k | else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4)) { |
346 | 24.2k | if (err == MZ_OK) |
347 | 24.2k | err = mz_stream_read_uint32(file_extra_stream, &reserved); |
348 | 24.2k | field_length_read = 4; |
349 | | |
350 | 65.0k | while ((err == MZ_OK) && (field_length_read + 4 <= field_length)) { |
351 | 40.7k | err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id); |
352 | 40.7k | if (err == MZ_OK) |
353 | 40.7k | err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size); |
354 | 40.7k | field_length_read += 4; |
355 | | |
356 | 40.7k | if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24)) { |
357 | 22.6k | err = mz_stream_read_uint64(file_extra_stream, &ntfs_time); |
358 | 22.6k | mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date); |
359 | | |
360 | 22.6k | if (err == MZ_OK) { |
361 | 22.6k | err = mz_stream_read_uint64(file_extra_stream, &ntfs_time); |
362 | 22.6k | mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date); |
363 | 22.6k | } |
364 | 22.6k | if (err == MZ_OK) { |
365 | 22.6k | err = mz_stream_read_uint64(file_extra_stream, &ntfs_time); |
366 | 22.6k | mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date); |
367 | 22.6k | } |
368 | 22.6k | } else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length)) { |
369 | 16.4k | err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR); |
370 | 16.4k | } |
371 | | |
372 | 40.7k | field_length_read += ntfs_attrib_size; |
373 | 40.7k | } |
374 | 24.2k | } |
375 | | /* Read UNIX1 extra field */ |
376 | 40.4k | else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12)) { |
377 | 2.02k | if (err == MZ_OK) { |
378 | 2.02k | err = mz_stream_read_uint32(file_extra_stream, &value32); |
379 | 2.02k | if (err == MZ_OK && file_info->accessed_date == 0) |
380 | 1.08k | file_info->accessed_date = value32; |
381 | 2.02k | } |
382 | 2.02k | if (err == MZ_OK) { |
383 | 2.01k | err = mz_stream_read_uint32(file_extra_stream, &value32); |
384 | 2.01k | if (err == MZ_OK && file_info->modified_date == 0) |
385 | 0 | file_info->modified_date = value32; |
386 | 2.01k | } |
387 | 2.02k | if (err == MZ_OK) |
388 | 1.99k | err = mz_stream_read_uint16(file_extra_stream, &value16); /* User id */ |
389 | 2.02k | if (err == MZ_OK) |
390 | 1.98k | err = mz_stream_read_uint16(file_extra_stream, &value16); /* Group id */ |
391 | | |
392 | | /* Copy linkname to end of file extra stream so we can return null |
393 | | terminated string */ |
394 | 2.02k | linkname_size = field_length - 12; |
395 | 2.02k | if ((err == MZ_OK) && (linkname_size > 0)) { |
396 | 1.35k | linkname = (char *)malloc(linkname_size); |
397 | 1.35k | if (linkname) { |
398 | 1.35k | if (mz_stream_read(file_extra_stream, linkname, linkname_size) != linkname_size) |
399 | 64 | err = MZ_READ_ERROR; |
400 | 1.35k | if (err == MZ_OK) { |
401 | 1.28k | saved_pos = mz_stream_tell(file_extra_stream); |
402 | | |
403 | 1.28k | mz_stream_seek(file_extra_stream, linkname_pos, MZ_SEEK_SET); |
404 | 1.28k | mz_stream_write(file_extra_stream, linkname, linkname_size); |
405 | 1.28k | mz_stream_write_uint8(file_extra_stream, 0); |
406 | | |
407 | 1.28k | mz_stream_seek(file_extra_stream, saved_pos, MZ_SEEK_SET); |
408 | 1.28k | } |
409 | 1.35k | free(linkname); |
410 | 1.35k | } |
411 | 1.35k | } |
412 | 2.02k | } |
413 | 38.4k | #ifdef HAVE_WZAES |
414 | | /* Read AES extra field */ |
415 | 38.4k | else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7)) { |
416 | 9.76k | uint8_t value8 = 0; |
417 | | /* Verify version info */ |
418 | 9.76k | err = mz_stream_read_uint16(file_extra_stream, &value16); |
419 | | /* Support AE-1 and AE-2 */ |
420 | 9.76k | if (value16 != 1 && value16 != 2) |
421 | 35 | err = MZ_FORMAT_ERROR; |
422 | 9.76k | file_info->aes_version = value16; |
423 | 9.76k | if (err == MZ_OK) |
424 | 9.73k | err = mz_stream_read_uint8(file_extra_stream, &value8); |
425 | 9.76k | if ((char)value8 != 'A') |
426 | 61 | err = MZ_FORMAT_ERROR; |
427 | 9.76k | if (err == MZ_OK) |
428 | 9.70k | err = mz_stream_read_uint8(file_extra_stream, &value8); |
429 | 9.76k | if ((char)value8 != 'E') |
430 | 67 | err = MZ_FORMAT_ERROR; |
431 | | /* Get AES encryption strength and actual compression method */ |
432 | 9.76k | if (err == MZ_OK) { |
433 | 9.69k | err = mz_stream_read_uint8(file_extra_stream, &value8); |
434 | 9.69k | file_info->aes_encryption_mode = value8; |
435 | 9.69k | } |
436 | 9.76k | if (err == MZ_OK) { |
437 | 9.69k | err = mz_stream_read_uint16(file_extra_stream, &value16); |
438 | 9.69k | file_info->compression_method = value16; |
439 | 9.69k | } |
440 | 9.76k | } |
441 | 28.6k | #endif |
442 | 28.6k | else if (field_length > 0) { |
443 | 14.3k | err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR); |
444 | 14.3k | } |
445 | | |
446 | 70.6k | field_pos += field_length; |
447 | 70.6k | } |
448 | 18.6k | } |
449 | | |
450 | | /* Get pointers to variable length data */ |
451 | 82.0k | mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename); |
452 | 82.0k | mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield); |
453 | 82.0k | mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment); |
454 | 82.0k | mz_stream_mem_get_buffer_at(file_extra_stream, linkname_pos, (const void **)&file_info->linkname); |
455 | | |
456 | | /* Set to empty string just in-case */ |
457 | 82.0k | if (!file_info->filename) |
458 | 0 | file_info->filename = ""; |
459 | 82.0k | if (!file_info->extrafield) |
460 | 0 | file_info->extrafield_size = 0; |
461 | 82.0k | if (!file_info->comment) |
462 | 0 | file_info->comment = ""; |
463 | 82.0k | if (!file_info->linkname) |
464 | 0 | file_info->linkname = ""; |
465 | | |
466 | 82.0k | if (err == MZ_OK) { |
467 | 68.6k | mz_zip_print("Zip - Entry - Read header - %s (local %" PRId8 ")\n", |
468 | 68.6k | file_info->filename, local); |
469 | 68.6k | mz_zip_print("Zip - Entry - Read header compress (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n", |
470 | 68.6k | file_info->uncompressed_size, file_info->compressed_size, file_info->crc); |
471 | 68.6k | if (!local) { |
472 | 58.4k | mz_zip_print("Zip - Entry - Read header disk (disk %" PRIu32 " offset %" PRId64 ")\n", |
473 | 58.4k | file_info->disk_number, file_info->disk_offset); |
474 | 58.4k | } |
475 | 68.6k | mz_zip_print("Zip - Entry - Read header variable (fnl %" PRId32 " efs %" PRId32 " cms %" PRId32 ")\n", |
476 | 68.6k | file_info->filename_size, file_info->extrafield_size, file_info->comment_size); |
477 | 68.6k | } |
478 | | |
479 | 82.0k | return err; |
480 | 82.0k | } |
481 | | |
482 | 0 | static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size) { |
483 | 0 | uint32_t value32 = 0; |
484 | 0 | int64_t value64 = 0; |
485 | 0 | int32_t err = MZ_OK; |
486 | |
|
487 | 0 | err = mz_stream_read_uint32(stream, &value32); |
488 | 0 | if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR) |
489 | 0 | err = MZ_FORMAT_ERROR; |
490 | 0 | if (err == MZ_OK) |
491 | 0 | err = mz_stream_read_uint32(stream, &value32); |
492 | 0 | if (err == MZ_OK && crc32) |
493 | 0 | *crc32 = value32; |
494 | 0 | if (err == MZ_OK) { |
495 | | /* If zip 64 extension is enabled then read as 8 byte */ |
496 | 0 | if (!zip64) { |
497 | 0 | err = mz_stream_read_uint32(stream, &value32); |
498 | 0 | value64 = value32; |
499 | 0 | } else { |
500 | 0 | err = mz_stream_read_int64(stream, &value64); |
501 | 0 | if (value64 < 0) |
502 | 0 | err = MZ_FORMAT_ERROR; |
503 | 0 | } |
504 | 0 | if (err == MZ_OK && compressed_size) |
505 | 0 | *compressed_size = value64; |
506 | 0 | } |
507 | 0 | if (err == MZ_OK) { |
508 | 0 | if (!zip64) { |
509 | 0 | err = mz_stream_read_uint32(stream, &value32); |
510 | 0 | value64 = value32; |
511 | 0 | } else { |
512 | 0 | err = mz_stream_read_int64(stream, &value64); |
513 | 0 | if (value64 < 0) |
514 | 0 | err = MZ_FORMAT_ERROR; |
515 | 0 | } |
516 | 0 | if (err == MZ_OK && uncompressed_size) |
517 | 0 | *uncompressed_size = value64; |
518 | 0 | } |
519 | |
|
520 | 0 | return err; |
521 | 0 | } |
522 | | |
523 | 0 | static int32_t mz_zip_entry_write_crc_sizes(void *stream, uint8_t zip64, uint8_t mask, mz_zip_file *file_info) { |
524 | 0 | int32_t err = MZ_OK; |
525 | |
|
526 | 0 | if (mask) |
527 | 0 | err = mz_stream_write_uint32(stream, 0); |
528 | 0 | else |
529 | 0 | err = mz_stream_write_uint32(stream, file_info->crc); /* crc */ |
530 | | |
531 | | /* For backwards-compatibility with older zip applications we set all sizes to UINT32_MAX |
532 | | * when zip64 is needed, instead of only setting sizes larger than UINT32_MAX. */ |
533 | |
|
534 | 0 | if (err == MZ_OK) { |
535 | 0 | if (zip64) /* compr size */ |
536 | 0 | err = mz_stream_write_uint32(stream, UINT32_MAX); |
537 | 0 | else |
538 | 0 | err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size); |
539 | 0 | } |
540 | 0 | if (err == MZ_OK) { |
541 | 0 | if (mask) /* uncompr size */ |
542 | 0 | err = mz_stream_write_uint32(stream, 0); |
543 | 0 | else if (zip64) |
544 | 0 | err = mz_stream_write_uint32(stream, UINT32_MAX); |
545 | 0 | else |
546 | 0 | err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size); |
547 | 0 | } |
548 | 0 | return err; |
549 | 0 | } |
550 | | |
551 | 0 | static int32_t mz_zip_entry_needs_zip64(mz_zip_file *file_info, uint8_t local, uint8_t *zip64) { |
552 | 0 | uint32_t max_uncompressed_size = UINT32_MAX; |
553 | 0 | uint8_t needs_zip64 = 0; |
554 | |
|
555 | 0 | if (!zip64) |
556 | 0 | return MZ_PARAM_ERROR; |
557 | | |
558 | 0 | *zip64 = 0; |
559 | |
|
560 | 0 | if (local) { |
561 | | /* At local header we might not know yet whether compressed size will overflow unsigned |
562 | | 32-bit integer which might happen for high entropy data so we give it some cushion */ |
563 | |
|
564 | 0 | max_uncompressed_size -= MZ_ZIP_UNCOMPR_SIZE64_CUSHION; |
565 | 0 | } |
566 | |
|
567 | 0 | needs_zip64 = (file_info->uncompressed_size >= max_uncompressed_size) || |
568 | 0 | (file_info->compressed_size >= UINT32_MAX); |
569 | |
|
570 | 0 | if (!local) { |
571 | | /* Disk offset and number only used in central directory header */ |
572 | 0 | needs_zip64 |= (file_info->disk_offset >= UINT32_MAX) || |
573 | 0 | (file_info->disk_number >= UINT16_MAX); |
574 | 0 | } |
575 | |
|
576 | 0 | if (file_info->zip64 == MZ_ZIP64_AUTO) { |
577 | | /* If uncompressed size is unknown, assume zip64 for 64-bit data descriptors */ |
578 | 0 | if (local && file_info->uncompressed_size == 0) { |
579 | | /* Don't use zip64 for local header directory entries */ |
580 | 0 | if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) != MZ_OK) { |
581 | 0 | *zip64 = 1; |
582 | 0 | } |
583 | 0 | } |
584 | 0 | *zip64 |= needs_zip64; |
585 | 0 | } else if (file_info->zip64 == MZ_ZIP64_FORCE) { |
586 | 0 | *zip64 = 1; |
587 | 0 | } else if (file_info->zip64 == MZ_ZIP64_DISABLE) { |
588 | | /* Zip64 extension is required to zip file */ |
589 | 0 | if (needs_zip64) |
590 | 0 | return MZ_PARAM_ERROR; |
591 | 0 | } |
592 | | |
593 | 0 | return MZ_OK; |
594 | 0 | } |
595 | | |
596 | 0 | static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info) { |
597 | 0 | uint64_t ntfs_time = 0; |
598 | 0 | uint32_t reserved = 0; |
599 | 0 | uint32_t dos_date = 0; |
600 | 0 | uint16_t extrafield_size = 0; |
601 | 0 | uint16_t field_type = 0; |
602 | 0 | uint16_t field_length = 0; |
603 | 0 | uint16_t field_length_zip64 = 0; |
604 | 0 | uint16_t field_length_ntfs = 0; |
605 | 0 | uint16_t field_length_aes = 0; |
606 | 0 | uint16_t field_length_unix1 = 0; |
607 | 0 | uint16_t filename_size = 0; |
608 | 0 | uint16_t filename_length = 0; |
609 | 0 | uint16_t linkname_size = 0; |
610 | 0 | uint16_t version_needed = 0; |
611 | 0 | int32_t comment_size = 0; |
612 | 0 | int32_t err = MZ_OK; |
613 | 0 | int32_t err_mem = MZ_OK; |
614 | 0 | uint8_t zip64 = 0; |
615 | 0 | uint8_t skip_aes = 0; |
616 | 0 | uint8_t mask = 0; |
617 | 0 | uint8_t write_end_slash = 0; |
618 | 0 | const char *filename = NULL; |
619 | 0 | char masked_name[64]; |
620 | 0 | void *file_extra_stream = NULL; |
621 | |
|
622 | 0 | if (!file_info) |
623 | 0 | return MZ_PARAM_ERROR; |
624 | | |
625 | 0 | if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)) |
626 | 0 | mask = 1; |
627 | | |
628 | | /* Determine if zip64 extra field is necessary */ |
629 | 0 | err = mz_zip_entry_needs_zip64(file_info, local, &zip64); |
630 | 0 | if (err != MZ_OK) |
631 | 0 | return err; |
632 | | |
633 | | /* Start calculating extra field sizes */ |
634 | 0 | if (zip64) { |
635 | | /* Both compressed and uncompressed sizes must be included (at least in local header) */ |
636 | 0 | field_length_zip64 = 8 + 8; |
637 | 0 | if ((!local) && (file_info->disk_offset >= UINT32_MAX)) |
638 | 0 | field_length_zip64 += 8; |
639 | |
|
640 | 0 | extrafield_size += 4; |
641 | 0 | extrafield_size += field_length_zip64; |
642 | 0 | } |
643 | | |
644 | | /* Calculate extra field size and check for duplicates */ |
645 | 0 | if (file_info->extrafield_size > 0) { |
646 | 0 | mz_stream_mem_create(&file_extra_stream); |
647 | 0 | mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield, |
648 | 0 | file_info->extrafield_size); |
649 | |
|
650 | 0 | do { |
651 | 0 | err_mem = mz_stream_read_uint16(file_extra_stream, &field_type); |
652 | 0 | if (err_mem == MZ_OK) |
653 | 0 | err_mem = mz_stream_read_uint16(file_extra_stream, &field_length); |
654 | 0 | if (err_mem != MZ_OK) |
655 | 0 | break; |
656 | | |
657 | | /* Prefer incoming aes extensions over ours */ |
658 | 0 | if (field_type == MZ_ZIP_EXTENSION_AES) |
659 | 0 | skip_aes = 1; |
660 | | |
661 | | /* Prefer our zip64, ntfs, unix1 extension over incoming */ |
662 | 0 | if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS && |
663 | 0 | field_type != MZ_ZIP_EXTENSION_UNIX1) |
664 | 0 | extrafield_size += 4 + field_length; |
665 | |
|
666 | 0 | if (err_mem == MZ_OK) |
667 | 0 | err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR); |
668 | 0 | } while (err_mem == MZ_OK); |
669 | 0 | } |
670 | | |
671 | 0 | #ifdef HAVE_WZAES |
672 | 0 | if (!skip_aes) { |
673 | 0 | if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version)) { |
674 | 0 | field_length_aes = 1 + 1 + 1 + 2 + 2; |
675 | 0 | extrafield_size += 4 + field_length_aes; |
676 | 0 | } |
677 | 0 | } |
678 | | #else |
679 | | MZ_UNUSED(field_length_aes); |
680 | | MZ_UNUSED(skip_aes); |
681 | | #endif |
682 | | /* NTFS timestamps */ |
683 | 0 | if ((file_info->modified_date != 0) && |
684 | 0 | (file_info->accessed_date != 0) && |
685 | 0 | (file_info->creation_date != 0) && (!mask)) { |
686 | 0 | field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2; |
687 | 0 | extrafield_size += 4 + field_length_ntfs; |
688 | 0 | } |
689 | | |
690 | | /* Unix1 symbolic links */ |
691 | 0 | if (file_info->linkname && *file_info->linkname != 0) { |
692 | 0 | linkname_size = (uint16_t)strlen(file_info->linkname); |
693 | 0 | field_length_unix1 = 12 + linkname_size; |
694 | 0 | extrafield_size += 4 + field_length_unix1; |
695 | 0 | } |
696 | |
|
697 | 0 | if (local) |
698 | 0 | err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER); |
699 | 0 | else { |
700 | 0 | err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER); |
701 | 0 | if (err == MZ_OK) |
702 | 0 | err = mz_stream_write_uint16(stream, file_info->version_madeby); |
703 | 0 | } |
704 | | |
705 | | /* Calculate version needed to extract */ |
706 | 0 | if (err == MZ_OK) { |
707 | 0 | version_needed = file_info->version_needed; |
708 | 0 | if (version_needed == 0) { |
709 | 0 | version_needed = 20; |
710 | 0 | if (zip64) |
711 | 0 | version_needed = 45; |
712 | 0 | #ifdef HAVE_WZAES |
713 | 0 | if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version)) |
714 | 0 | version_needed = 51; |
715 | 0 | #endif |
716 | | #if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP) |
717 | | if ((file_info->compression_method == MZ_COMPRESS_METHOD_LZMA) || |
718 | | (file_info->compression_method == MZ_COMPRESS_METHOD_XZ)) |
719 | | version_needed = 63; |
720 | | #endif |
721 | 0 | } |
722 | 0 | err = mz_stream_write_uint16(stream, version_needed); |
723 | 0 | } |
724 | 0 | if (err == MZ_OK) |
725 | 0 | err = mz_stream_write_uint16(stream, file_info->flag); |
726 | 0 | if (err == MZ_OK) { |
727 | 0 | #ifdef HAVE_WZAES |
728 | 0 | if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version)) |
729 | 0 | err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES); |
730 | 0 | else |
731 | 0 | #endif |
732 | 0 | err = mz_stream_write_uint16(stream, file_info->compression_method); |
733 | 0 | } |
734 | 0 | if (err == MZ_OK) { |
735 | 0 | if (file_info->modified_date != 0 && !mask) |
736 | 0 | dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date); |
737 | 0 | err = mz_stream_write_uint32(stream, dos_date); |
738 | 0 | } |
739 | |
|
740 | 0 | if (err == MZ_OK) |
741 | 0 | err = mz_zip_entry_write_crc_sizes(stream, zip64, mask, file_info); |
742 | |
|
743 | 0 | if (mask) { |
744 | 0 | snprintf(masked_name, sizeof(masked_name), "%" PRIx32 "_%" PRIx64, |
745 | 0 | file_info->disk_number, file_info->disk_offset); |
746 | 0 | filename = masked_name; |
747 | 0 | } else { |
748 | 0 | filename = file_info->filename; |
749 | 0 | } |
750 | |
|
751 | 0 | filename_length = (uint16_t)strlen(filename); |
752 | 0 | filename_size += filename_length; |
753 | |
|
754 | 0 | if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) && |
755 | 0 | ((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\'))) { |
756 | 0 | filename_size += 1; |
757 | 0 | write_end_slash = 1; |
758 | 0 | } |
759 | |
|
760 | 0 | if (err == MZ_OK) |
761 | 0 | err = mz_stream_write_uint16(stream, filename_size); |
762 | 0 | if (err == MZ_OK) |
763 | 0 | err = mz_stream_write_uint16(stream, extrafield_size); |
764 | |
|
765 | 0 | if (!local) { |
766 | 0 | if (file_info->comment) { |
767 | 0 | comment_size = (int32_t)strlen(file_info->comment); |
768 | 0 | if (comment_size > UINT16_MAX) |
769 | 0 | comment_size = UINT16_MAX; |
770 | 0 | } |
771 | 0 | if (err == MZ_OK) |
772 | 0 | err = mz_stream_write_uint16(stream, (uint16_t)comment_size); |
773 | 0 | if (err == MZ_OK) |
774 | 0 | err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number); |
775 | 0 | if (err == MZ_OK) |
776 | 0 | err = mz_stream_write_uint16(stream, file_info->internal_fa); |
777 | 0 | if (err == MZ_OK) |
778 | 0 | err = mz_stream_write_uint32(stream, file_info->external_fa); |
779 | 0 | if (err == MZ_OK) { |
780 | 0 | if (file_info->disk_offset >= UINT32_MAX) |
781 | 0 | err = mz_stream_write_uint32(stream, UINT32_MAX); |
782 | 0 | else |
783 | 0 | err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset); |
784 | 0 | } |
785 | 0 | } |
786 | |
|
787 | 0 | if (err == MZ_OK) { |
788 | 0 | const char *backslash = NULL; |
789 | 0 | const char *next = filename; |
790 | 0 | int32_t left = filename_length; |
791 | | |
792 | | /* Ensure all slashes are written as forward slashes according to 4.4.17.1 */ |
793 | 0 | while ((err == MZ_OK) && (backslash = strrchr(next, '\\'))) { |
794 | 0 | int32_t part_length = (int32_t)(backslash - next); |
795 | |
|
796 | 0 | if (mz_stream_write(stream, next, part_length) != part_length || |
797 | 0 | mz_stream_write(stream, "/", 1) != 1) |
798 | 0 | err = MZ_WRITE_ERROR; |
799 | |
|
800 | 0 | left -= part_length + 1; |
801 | 0 | next = backslash + 1; |
802 | 0 | } |
803 | |
|
804 | 0 | if (err == MZ_OK && left > 0) { |
805 | 0 | if (mz_stream_write(stream, next, left) != left) |
806 | 0 | err = MZ_WRITE_ERROR; |
807 | 0 | } |
808 | | |
809 | | /* Ensure that directories have a slash appended to them for compatibility */ |
810 | 0 | if (err == MZ_OK && write_end_slash) |
811 | 0 | err = mz_stream_write_uint8(stream, '/'); |
812 | 0 | } |
813 | | |
814 | | /* Write ZIP64 extra field first so we can update sizes later if data descriptor not used */ |
815 | 0 | if ((err == MZ_OK) && (zip64)) { |
816 | 0 | err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64); |
817 | 0 | if (err == MZ_OK) { |
818 | 0 | if (mask) |
819 | 0 | err = mz_stream_write_int64(stream, 0); |
820 | 0 | else |
821 | 0 | err = mz_stream_write_int64(stream, file_info->uncompressed_size); |
822 | 0 | } |
823 | 0 | if (err == MZ_OK) |
824 | 0 | err = mz_stream_write_int64(stream, file_info->compressed_size); |
825 | 0 | if ((err == MZ_OK) && (!local) && (file_info->disk_offset >= UINT32_MAX)) |
826 | 0 | err = mz_stream_write_int64(stream, file_info->disk_offset); |
827 | 0 | if ((err == MZ_OK) && (!local) && (file_info->disk_number >= UINT16_MAX)) |
828 | 0 | err = mz_stream_write_uint32(stream, file_info->disk_number); |
829 | 0 | } |
830 | | /* Write NTFS extra field */ |
831 | 0 | if ((err == MZ_OK) && (field_length_ntfs > 0)) { |
832 | 0 | err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs); |
833 | 0 | if (err == MZ_OK) |
834 | 0 | err = mz_stream_write_uint32(stream, reserved); |
835 | 0 | if (err == MZ_OK) |
836 | 0 | err = mz_stream_write_uint16(stream, 0x01); |
837 | 0 | if (err == MZ_OK) |
838 | 0 | err = mz_stream_write_uint16(stream, field_length_ntfs - 8); |
839 | 0 | if (err == MZ_OK) { |
840 | 0 | mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time); |
841 | 0 | err = mz_stream_write_uint64(stream, ntfs_time); |
842 | 0 | } |
843 | 0 | if (err == MZ_OK) { |
844 | 0 | mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time); |
845 | 0 | err = mz_stream_write_uint64(stream, ntfs_time); |
846 | 0 | } |
847 | 0 | if (err == MZ_OK) { |
848 | 0 | mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time); |
849 | 0 | err = mz_stream_write_uint64(stream, ntfs_time); |
850 | 0 | } |
851 | 0 | } |
852 | | /* Write UNIX extra block extra field */ |
853 | 0 | if ((err == MZ_OK) && (field_length_unix1 > 0)) { |
854 | 0 | err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_UNIX1, field_length_unix1); |
855 | 0 | if (err == MZ_OK) |
856 | 0 | err = mz_stream_write_uint32(stream, (uint32_t)file_info->accessed_date); |
857 | 0 | if (err == MZ_OK) |
858 | 0 | err = mz_stream_write_uint32(stream, (uint32_t)file_info->modified_date); |
859 | 0 | if (err == MZ_OK) /* User id */ |
860 | 0 | err = mz_stream_write_uint16(stream, 0); |
861 | 0 | if (err == MZ_OK) /* Group id */ |
862 | 0 | err = mz_stream_write_uint16(stream, 0); |
863 | 0 | if (err == MZ_OK && linkname_size > 0) { |
864 | 0 | if (mz_stream_write(stream, file_info->linkname, linkname_size) != linkname_size) |
865 | 0 | err = MZ_WRITE_ERROR; |
866 | 0 | } |
867 | 0 | } |
868 | 0 | #ifdef HAVE_WZAES |
869 | | /* Write AES extra field */ |
870 | 0 | if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version)) { |
871 | 0 | err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes); |
872 | 0 | if (err == MZ_OK) |
873 | 0 | err = mz_stream_write_uint16(stream, file_info->aes_version); |
874 | 0 | if (err == MZ_OK) |
875 | 0 | err = mz_stream_write_uint8(stream, 'A'); |
876 | 0 | if (err == MZ_OK) |
877 | 0 | err = mz_stream_write_uint8(stream, 'E'); |
878 | 0 | if (err == MZ_OK) |
879 | 0 | err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode); |
880 | 0 | if (err == MZ_OK) |
881 | 0 | err = mz_stream_write_uint16(stream, file_info->compression_method); |
882 | 0 | } |
883 | 0 | #endif |
884 | |
|
885 | 0 | if (file_info->extrafield_size > 0) { |
886 | 0 | err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET); |
887 | 0 | while (err == MZ_OK && err_mem == MZ_OK) { |
888 | 0 | err_mem = mz_stream_read_uint16(file_extra_stream, &field_type); |
889 | 0 | if (err_mem == MZ_OK) |
890 | 0 | err_mem = mz_stream_read_uint16(file_extra_stream, &field_length); |
891 | 0 | if (err_mem != MZ_OK) |
892 | 0 | break; |
893 | | |
894 | | /* Prefer our zip 64, ntfs, unix1 extensions over incoming */ |
895 | 0 | if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS || |
896 | 0 | field_type == MZ_ZIP_EXTENSION_UNIX1) { |
897 | 0 | err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR); |
898 | 0 | continue; |
899 | 0 | } |
900 | | |
901 | 0 | err = mz_stream_write_uint16(stream, field_type); |
902 | 0 | if (err == MZ_OK) |
903 | 0 | err = mz_stream_write_uint16(stream, field_length); |
904 | 0 | if (err == MZ_OK) |
905 | 0 | err = mz_stream_copy(stream, file_extra_stream, field_length); |
906 | 0 | } |
907 | |
|
908 | 0 | mz_stream_mem_delete(&file_extra_stream); |
909 | 0 | } |
910 | |
|
911 | 0 | if (err == MZ_OK && !local && file_info->comment) { |
912 | 0 | if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size) |
913 | 0 | err = MZ_WRITE_ERROR; |
914 | 0 | } |
915 | |
|
916 | 0 | return err; |
917 | 0 | } |
918 | | |
919 | 0 | static int32_t mz_zip_entry_write_descriptor(void *stream, uint8_t zip64, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size) { |
920 | 0 | int32_t err = MZ_OK; |
921 | |
|
922 | 0 | err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR); |
923 | 0 | if (err == MZ_OK) |
924 | 0 | err = mz_stream_write_uint32(stream, crc32); |
925 | | |
926 | | /* Store data descriptor as 8 bytes if zip 64 extension enabled */ |
927 | 0 | if (err == MZ_OK) { |
928 | | /* Zip 64 extension is enabled when uncompressed size is > UINT32_MAX */ |
929 | 0 | if (!zip64) |
930 | 0 | err = mz_stream_write_uint32(stream, (uint32_t)compressed_size); |
931 | 0 | else |
932 | 0 | err = mz_stream_write_int64(stream, compressed_size); |
933 | 0 | } |
934 | 0 | if (err == MZ_OK) { |
935 | 0 | if (!zip64) |
936 | 0 | err = mz_stream_write_uint32(stream, (uint32_t)uncompressed_size); |
937 | 0 | else |
938 | 0 | err = mz_stream_write_int64(stream, uncompressed_size); |
939 | 0 | } |
940 | |
|
941 | 0 | return err; |
942 | 0 | } |
943 | | |
944 | 3.82k | static int32_t mz_zip_read_cd(void *handle) { |
945 | 3.82k | mz_zip *zip = (mz_zip *)handle; |
946 | 3.82k | uint64_t number_entry_cd64 = 0; |
947 | 3.82k | uint64_t number_entry_cd = 0; |
948 | 3.82k | int64_t eocd_pos = 0; |
949 | 3.82k | int64_t eocd_pos64 = 0; |
950 | 3.82k | int64_t value64i = 0; |
951 | 3.82k | uint16_t value16 = 0; |
952 | 3.82k | uint32_t value32 = 0; |
953 | 3.82k | uint64_t value64 = 0; |
954 | 3.82k | uint16_t comment_size = 0; |
955 | 3.82k | int32_t comment_read = 0; |
956 | 3.82k | int32_t err = MZ_OK; |
957 | | |
958 | 3.82k | if (!zip) |
959 | 0 | return MZ_PARAM_ERROR; |
960 | | |
961 | | /* Read and cache central directory records */ |
962 | 3.82k | err = mz_zip_search_eocd(zip->stream, &eocd_pos); |
963 | 3.82k | if (err == MZ_OK) { |
964 | | /* The signature, already checked */ |
965 | 3.74k | err = mz_stream_read_uint32(zip->stream, &value32); |
966 | | /* Number of this disk */ |
967 | 3.74k | if (err == MZ_OK) |
968 | 3.74k | err = mz_stream_read_uint16(zip->stream, &value16); |
969 | | /* Number of the disk with the start of the central directory */ |
970 | 3.74k | if (err == MZ_OK) |
971 | 3.72k | err = mz_stream_read_uint16(zip->stream, &value16); |
972 | 3.74k | zip->disk_number_with_cd = value16; |
973 | | /* Total number of entries in the central dir on this disk */ |
974 | 3.74k | if (err == MZ_OK) |
975 | 3.72k | err = mz_stream_read_uint16(zip->stream, &value16); |
976 | 3.74k | zip->number_entry = value16; |
977 | | /* Total number of entries in the central dir */ |
978 | 3.74k | if (err == MZ_OK) |
979 | 3.72k | err = mz_stream_read_uint16(zip->stream, &value16); |
980 | 3.74k | number_entry_cd = value16; |
981 | 3.74k | if (number_entry_cd != zip->number_entry) |
982 | 32 | err = MZ_FORMAT_ERROR; |
983 | | /* Size of the central directory */ |
984 | 3.74k | if (err == MZ_OK) |
985 | 3.68k | err = mz_stream_read_uint32(zip->stream, &value32); |
986 | 3.74k | if (err == MZ_OK) |
987 | 3.68k | zip->cd_size = value32; |
988 | | /* Offset of start of central directory with respect to the starting disk number */ |
989 | 3.74k | if (err == MZ_OK) |
990 | 3.68k | err = mz_stream_read_uint32(zip->stream, &value32); |
991 | 3.74k | if (err == MZ_OK) |
992 | 3.67k | zip->cd_offset = value32; |
993 | | /* Zip file global comment length */ |
994 | 3.74k | if (err == MZ_OK) |
995 | 3.67k | err = mz_stream_read_uint16(zip->stream, &comment_size); |
996 | 3.74k | if ((err == MZ_OK) && (comment_size > 0)) { |
997 | 2.91k | zip->comment = (char *)malloc(comment_size + 1); |
998 | 2.91k | if (zip->comment) { |
999 | 2.91k | comment_read = mz_stream_read(zip->stream, zip->comment, comment_size); |
1000 | | /* Don't fail if incorrect comment length read, not critical */ |
1001 | 2.91k | if (comment_read < 0) |
1002 | 0 | comment_read = 0; |
1003 | 2.91k | zip->comment[comment_read] = 0; |
1004 | 2.91k | } |
1005 | 2.91k | } |
1006 | | |
1007 | 3.74k | if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))) { |
1008 | | /* Format should be Zip64, as the central directory or file size is too large */ |
1009 | 587 | if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK) { |
1010 | 347 | eocd_pos = eocd_pos64; |
1011 | | |
1012 | 347 | err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET); |
1013 | | /* The signature, already checked */ |
1014 | 347 | if (err == MZ_OK) |
1015 | 347 | err = mz_stream_read_uint32(zip->stream, &value32); |
1016 | | /* Size of zip64 end of central directory record */ |
1017 | 347 | if (err == MZ_OK) |
1018 | 347 | err = mz_stream_read_uint64(zip->stream, &value64); |
1019 | | /* Version made by */ |
1020 | 347 | if (err == MZ_OK) |
1021 | 345 | err = mz_stream_read_uint16(zip->stream, &zip->version_madeby); |
1022 | | /* Version needed to extract */ |
1023 | 347 | if (err == MZ_OK) |
1024 | 345 | err = mz_stream_read_uint16(zip->stream, &value16); |
1025 | | /* Number of this disk */ |
1026 | 347 | if (err == MZ_OK) |
1027 | 345 | err = mz_stream_read_uint32(zip->stream, &value32); |
1028 | | /* Number of the disk with the start of the central directory */ |
1029 | 347 | if (err == MZ_OK) |
1030 | 345 | err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd); |
1031 | | /* Total number of entries in the central directory on this disk */ |
1032 | 347 | if (err == MZ_OK) |
1033 | 345 | err = mz_stream_read_uint64(zip->stream, &zip->number_entry); |
1034 | | /* Total number of entries in the central directory */ |
1035 | 347 | if (err == MZ_OK) |
1036 | 344 | err = mz_stream_read_uint64(zip->stream, &number_entry_cd64); |
1037 | 347 | if (zip->number_entry != number_entry_cd64) |
1038 | 108 | err = MZ_FORMAT_ERROR; |
1039 | | /* Size of the central directory */ |
1040 | 347 | if (err == MZ_OK) { |
1041 | 238 | err = mz_stream_read_int64(zip->stream, &zip->cd_size); |
1042 | 238 | if (zip->cd_size < 0) |
1043 | 77 | err = MZ_FORMAT_ERROR; |
1044 | 238 | } |
1045 | | /* Offset of start of central directory with respect to the starting disk number */ |
1046 | 347 | if (err == MZ_OK) { |
1047 | 156 | err = mz_stream_read_int64(zip->stream, &zip->cd_offset); |
1048 | 156 | if (zip->cd_offset < 0) |
1049 | 79 | err = MZ_FORMAT_ERROR; |
1050 | 156 | } |
1051 | 347 | } else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) || |
1052 | 240 | (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)) { |
1053 | 240 | err = MZ_FORMAT_ERROR; |
1054 | 240 | } |
1055 | 587 | } |
1056 | 3.74k | } |
1057 | | |
1058 | 3.82k | if (err == MZ_OK) { |
1059 | 3.14k | mz_zip_print("Zip - Read cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n", |
1060 | 3.14k | zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size); |
1061 | | |
1062 | | /* Verify central directory signature exists at offset */ |
1063 | 3.14k | err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET); |
1064 | 3.14k | if (err == MZ_OK) |
1065 | 3.02k | err = mz_stream_read_uint32(zip->stream, &zip->cd_signature); |
1066 | 3.14k | if ((err == MZ_OK) && (zip->cd_signature != MZ_ZIP_MAGIC_CENTRALHEADER)) { |
1067 | | /* If cd exists in large file and no zip-64 support, error for recover */ |
1068 | 583 | if (eocd_pos > UINT32_MAX && eocd_pos64 == 0) |
1069 | 0 | err = MZ_FORMAT_ERROR; |
1070 | | /* If cd not found attempt to seek backward to find it */ |
1071 | 583 | if (err == MZ_OK) |
1072 | 583 | err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET); |
1073 | 583 | if (err == MZ_OK) |
1074 | 485 | err = mz_stream_read_uint32(zip->stream, &zip->cd_signature); |
1075 | 583 | if ((err == MZ_OK) && (zip->cd_signature == MZ_ZIP_MAGIC_CENTRALHEADER)) { |
1076 | | /* If found compensate for incorrect locations */ |
1077 | 307 | value64i = zip->cd_offset; |
1078 | 307 | zip->cd_offset = eocd_pos - zip->cd_size; |
1079 | | /* Assume disk has prepended data */ |
1080 | 307 | zip->disk_offset_shift = zip->cd_offset - value64i; |
1081 | 307 | } |
1082 | 583 | } |
1083 | 3.14k | } |
1084 | | |
1085 | 3.82k | if (err == MZ_OK) { |
1086 | 2.92k | if (eocd_pos < zip->cd_offset) { |
1087 | | /* End of central dir should always come after central dir */ |
1088 | 18 | err = MZ_FORMAT_ERROR; |
1089 | 2.91k | } else if ((uint64_t)eocd_pos < (uint64_t)zip->cd_offset + zip->cd_size) { |
1090 | | /* Truncate size of cd if incorrect size or offset provided */ |
1091 | 2.01k | zip->cd_size = eocd_pos - zip->cd_offset; |
1092 | 2.01k | } |
1093 | 2.92k | } |
1094 | | |
1095 | 3.82k | return err; |
1096 | 3.82k | } |
1097 | | |
1098 | 0 | static int32_t mz_zip_write_cd(void *handle) { |
1099 | 0 | mz_zip *zip = (mz_zip *)handle; |
1100 | 0 | int64_t zip64_eocd_pos_inzip = 0; |
1101 | 0 | int64_t disk_number = 0; |
1102 | 0 | int64_t disk_size = 0; |
1103 | 0 | int32_t comment_size = 0; |
1104 | 0 | int32_t err = MZ_OK; |
1105 | |
|
1106 | 0 | if (!zip) |
1107 | 0 | return MZ_PARAM_ERROR; |
1108 | | |
1109 | 0 | if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK) |
1110 | 0 | zip->disk_number_with_cd = (uint32_t)disk_number; |
1111 | 0 | if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0) |
1112 | 0 | zip->disk_number_with_cd += 1; |
1113 | 0 | mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1); |
1114 | 0 | if ((zip->disk_number_with_cd > 0) && (zip->open_mode & MZ_OPEN_MODE_APPEND)) { |
1115 | | // Overwrite existing central directory if using split disks |
1116 | 0 | mz_stream_seek(zip->stream, 0, MZ_SEEK_SET); |
1117 | 0 | } |
1118 | |
|
1119 | 0 | zip->cd_offset = mz_stream_tell(zip->stream); |
1120 | 0 | mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END); |
1121 | 0 | zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream); |
1122 | 0 | mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET); |
1123 | |
|
1124 | 0 | err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size); |
1125 | |
|
1126 | 0 | mz_zip_print("Zip - Write cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n", |
1127 | 0 | zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size); |
1128 | |
|
1129 | 0 | if (zip->cd_size == 0 && zip->number_entry > 0) { |
1130 | | // Zip does not contain central directory, open with recovery option |
1131 | 0 | return MZ_FORMAT_ERROR; |
1132 | 0 | } |
1133 | | |
1134 | | /* Write the ZIP64 central directory header */ |
1135 | 0 | if (zip->cd_offset >= UINT32_MAX || zip->number_entry >= UINT16_MAX) { |
1136 | 0 | zip64_eocd_pos_inzip = mz_stream_tell(zip->stream); |
1137 | |
|
1138 | 0 | err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64); |
1139 | | |
1140 | | /* Size of this 'zip64 end of central directory' */ |
1141 | 0 | if (err == MZ_OK) |
1142 | 0 | err = mz_stream_write_uint64(zip->stream, (uint64_t)44); |
1143 | | /* Version made by */ |
1144 | 0 | if (err == MZ_OK) |
1145 | 0 | err = mz_stream_write_uint16(zip->stream, zip->version_madeby); |
1146 | | /* Version needed */ |
1147 | 0 | if (err == MZ_OK) |
1148 | 0 | err = mz_stream_write_uint16(zip->stream, (uint16_t)45); |
1149 | | /* Number of this disk */ |
1150 | 0 | if (err == MZ_OK) |
1151 | 0 | err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd); |
1152 | | /* Number of the disk with the start of the central directory */ |
1153 | 0 | if (err == MZ_OK) |
1154 | 0 | err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd); |
1155 | | /* Total number of entries in the central dir on this disk */ |
1156 | 0 | if (err == MZ_OK) |
1157 | 0 | err = mz_stream_write_uint64(zip->stream, zip->number_entry); |
1158 | | /* Total number of entries in the central dir */ |
1159 | 0 | if (err == MZ_OK) |
1160 | 0 | err = mz_stream_write_uint64(zip->stream, zip->number_entry); |
1161 | | /* Size of the central directory */ |
1162 | 0 | if (err == MZ_OK) |
1163 | 0 | err = mz_stream_write_int64(zip->stream, zip->cd_size); |
1164 | | /* Offset of start of central directory with respect to the starting disk number */ |
1165 | 0 | if (err == MZ_OK) |
1166 | 0 | err = mz_stream_write_int64(zip->stream, zip->cd_offset); |
1167 | 0 | if (err == MZ_OK) |
1168 | 0 | err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64); |
1169 | | |
1170 | | /* Number of the disk with the start of the central directory */ |
1171 | 0 | if (err == MZ_OK) |
1172 | 0 | err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd); |
1173 | | /* Relative offset to the end of zip64 central directory */ |
1174 | 0 | if (err == MZ_OK) |
1175 | 0 | err = mz_stream_write_int64(zip->stream, zip64_eocd_pos_inzip); |
1176 | | /* Number of the disk with the start of the central directory */ |
1177 | 0 | if (err == MZ_OK) |
1178 | 0 | err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1); |
1179 | 0 | } |
1180 | | |
1181 | | /* Write the central directory header */ |
1182 | | |
1183 | | /* Signature */ |
1184 | 0 | if (err == MZ_OK) |
1185 | 0 | err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER); |
1186 | | /* Number of this disk */ |
1187 | 0 | if (err == MZ_OK) |
1188 | 0 | err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd); |
1189 | | /* Number of the disk with the start of the central directory */ |
1190 | 0 | if (err == MZ_OK) |
1191 | 0 | err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd); |
1192 | | /* Total number of entries in the central dir on this disk */ |
1193 | 0 | if (err == MZ_OK) { |
1194 | 0 | if (zip->number_entry >= UINT16_MAX) |
1195 | 0 | err = mz_stream_write_uint16(zip->stream, UINT16_MAX); |
1196 | 0 | else |
1197 | 0 | err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry); |
1198 | 0 | } |
1199 | | /* Total number of entries in the central dir */ |
1200 | 0 | if (err == MZ_OK) { |
1201 | 0 | if (zip->number_entry >= UINT16_MAX) |
1202 | 0 | err = mz_stream_write_uint16(zip->stream, UINT16_MAX); |
1203 | 0 | else |
1204 | 0 | err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry); |
1205 | 0 | } |
1206 | | /* Size of the central directory */ |
1207 | 0 | if (err == MZ_OK) |
1208 | 0 | err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size); |
1209 | | /* Offset of start of central directory with respect to the starting disk number */ |
1210 | 0 | if (err == MZ_OK) { |
1211 | 0 | if (zip->cd_offset >= UINT32_MAX) |
1212 | 0 | err = mz_stream_write_uint32(zip->stream, UINT32_MAX); |
1213 | 0 | else |
1214 | 0 | err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset); |
1215 | 0 | } |
1216 | | |
1217 | | /* Write global comment */ |
1218 | 0 | if (zip->comment) { |
1219 | 0 | comment_size = (int32_t)strlen(zip->comment); |
1220 | 0 | if (comment_size > UINT16_MAX) |
1221 | 0 | comment_size = UINT16_MAX; |
1222 | 0 | } |
1223 | 0 | if (err == MZ_OK) |
1224 | 0 | err = mz_stream_write_uint16(zip->stream, (uint16_t)comment_size); |
1225 | 0 | if (err == MZ_OK) { |
1226 | 0 | if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size) |
1227 | 0 | err = MZ_READ_ERROR; |
1228 | 0 | } |
1229 | 0 | return err; |
1230 | 0 | } |
1231 | | |
1232 | 0 | static int32_t mz_zip_recover_cd(void *handle) { |
1233 | 0 | mz_zip *zip = (mz_zip *)handle; |
1234 | 0 | mz_zip_file local_file_info; |
1235 | 0 | void *local_file_info_stream = NULL; |
1236 | 0 | void *cd_mem_stream = NULL; |
1237 | 0 | uint64_t number_entry = 0; |
1238 | 0 | int64_t descriptor_pos = 0; |
1239 | 0 | int64_t next_header_pos = 0; |
1240 | 0 | int64_t disk_offset = 0; |
1241 | 0 | int64_t disk_number = 0; |
1242 | 0 | int64_t compressed_pos = 0; |
1243 | 0 | int64_t compressed_end_pos = 0; |
1244 | 0 | int64_t compressed_size = 0; |
1245 | 0 | int64_t uncompressed_size = 0; |
1246 | 0 | uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8; |
1247 | 0 | uint8_t local_header_magic[4] = MZ_ZIP_MAGIC_LOCALHEADERU8; |
1248 | 0 | uint8_t central_header_magic[4] = MZ_ZIP_MAGIC_CENTRALHEADERU8; |
1249 | 0 | uint32_t crc32 = 0; |
1250 | 0 | int32_t disk_number_with_cd = 0; |
1251 | 0 | int32_t err = MZ_OK; |
1252 | 0 | uint8_t zip64 = 0; |
1253 | 0 | uint8_t eof = 0; |
1254 | |
|
1255 | 0 | mz_zip_print("Zip - Recover - Start\n"); |
1256 | |
|
1257 | 0 | mz_zip_get_cd_mem_stream(handle, &cd_mem_stream); |
1258 | | |
1259 | | /* Determine if we are on a split disk or not */ |
1260 | 0 | mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0); |
1261 | 0 | if (mz_stream_tell(zip->stream) < 0) { |
1262 | 0 | mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1); |
1263 | 0 | mz_stream_seek(zip->stream, 0, MZ_SEEK_SET); |
1264 | 0 | } else |
1265 | 0 | disk_number_with_cd = 1; |
1266 | |
|
1267 | 0 | if (mz_stream_is_open(cd_mem_stream) != MZ_OK) |
1268 | 0 | err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE); |
1269 | |
|
1270 | 0 | mz_stream_mem_create(&local_file_info_stream); |
1271 | 0 | mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE); |
1272 | |
|
1273 | 0 | if (err == MZ_OK) { |
1274 | 0 | err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic), |
1275 | 0 | INT64_MAX, &next_header_pos); |
1276 | 0 | } |
1277 | |
|
1278 | 0 | while (err == MZ_OK && !eof) { |
1279 | | /* Get current offset and disk number for central dir record */ |
1280 | 0 | disk_offset = mz_stream_tell(zip->stream); |
1281 | 0 | mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number); |
1282 | | |
1283 | | /* Read local headers */ |
1284 | 0 | memset(&local_file_info, 0, sizeof(local_file_info)); |
1285 | 0 | err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream); |
1286 | 0 | if (err != MZ_OK) |
1287 | 0 | break; |
1288 | | |
1289 | 0 | local_file_info.disk_offset = disk_offset; |
1290 | 0 | if (disk_number < 0) |
1291 | 0 | disk_number = 0; |
1292 | 0 | local_file_info.disk_number = (uint32_t)disk_number; |
1293 | |
|
1294 | 0 | compressed_pos = mz_stream_tell(zip->stream); |
1295 | |
|
1296 | 0 | if ((err == MZ_OK) && (local_file_info.compressed_size > 0)) { |
1297 | 0 | mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR); |
1298 | 0 | } |
1299 | |
|
1300 | 0 | for (;;) { |
1301 | | /* Search for the next local header */ |
1302 | 0 | err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic), |
1303 | 0 | INT64_MAX, &next_header_pos); |
1304 | |
|
1305 | 0 | if (err == MZ_EXIST_ERROR) { |
1306 | 0 | mz_stream_seek(zip->stream, compressed_pos, MZ_SEEK_SET); |
1307 | | |
1308 | | /* Search for central dir if no local header found */ |
1309 | 0 | err = mz_stream_find(zip->stream, (const void *)central_header_magic, sizeof(central_header_magic), |
1310 | 0 | INT64_MAX, &next_header_pos); |
1311 | |
|
1312 | 0 | if (err == MZ_EXIST_ERROR) { |
1313 | | /* Get end of stream if no central header found */ |
1314 | 0 | mz_stream_seek(zip->stream, 0, MZ_SEEK_END); |
1315 | 0 | next_header_pos = mz_stream_tell(zip->stream); |
1316 | 0 | } |
1317 | |
|
1318 | 0 | eof = 1; |
1319 | 0 | } |
1320 | |
|
1321 | 0 | if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR || local_file_info.compressed_size == 0) { |
1322 | | /* Search backwards for the descriptor, seeking too far back will be incorrect if compressed size is small */ |
1323 | 0 | err = mz_stream_find_reverse(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic), |
1324 | 0 | MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR, &descriptor_pos); |
1325 | 0 | if (err == MZ_OK) { |
1326 | 0 | if (mz_zip_extrafield_contains(local_file_info.extrafield, |
1327 | 0 | local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK) |
1328 | 0 | zip64 = 1; |
1329 | |
|
1330 | 0 | err = mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32, |
1331 | 0 | &compressed_size, &uncompressed_size); |
1332 | |
|
1333 | 0 | if (err == MZ_OK) { |
1334 | 0 | if (local_file_info.crc == 0) |
1335 | 0 | local_file_info.crc = crc32; |
1336 | 0 | if (local_file_info.compressed_size == 0) |
1337 | 0 | local_file_info.compressed_size = compressed_size; |
1338 | 0 | if (local_file_info.uncompressed_size == 0) |
1339 | 0 | local_file_info.uncompressed_size = uncompressed_size; |
1340 | 0 | } |
1341 | |
|
1342 | 0 | compressed_end_pos = descriptor_pos; |
1343 | 0 | } else if (eof) { |
1344 | 0 | compressed_end_pos = next_header_pos; |
1345 | 0 | } else if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) { |
1346 | | /* Wrong local file entry found, keep searching */ |
1347 | 0 | next_header_pos += 1; |
1348 | 0 | mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET); |
1349 | 0 | continue; |
1350 | 0 | } |
1351 | 0 | } else { |
1352 | 0 | compressed_end_pos = next_header_pos; |
1353 | 0 | } |
1354 | | |
1355 | 0 | break; |
1356 | 0 | } |
1357 | |
|
1358 | 0 | compressed_size = compressed_end_pos - compressed_pos; |
1359 | |
|
1360 | 0 | if (compressed_size > UINT32_MAX) { |
1361 | | /* Update sizes if 4GB file is written with no ZIP64 support */ |
1362 | 0 | if (local_file_info.uncompressed_size < UINT32_MAX) { |
1363 | 0 | local_file_info.compressed_size = compressed_size; |
1364 | 0 | local_file_info.uncompressed_size = 0; |
1365 | 0 | } |
1366 | 0 | } |
1367 | |
|
1368 | 0 | mz_zip_print("Zip - Recover - Entry %s (csize %" PRId64 " usize %" PRId64 " flags 0x%" PRIx16 ")\n", |
1369 | 0 | local_file_info.filename, local_file_info.compressed_size, local_file_info.uncompressed_size, |
1370 | 0 | local_file_info.flag); |
1371 | | |
1372 | | /* Rewrite central dir with local headers and offsets */ |
1373 | 0 | err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info); |
1374 | 0 | if (err == MZ_OK) |
1375 | 0 | number_entry += 1; |
1376 | |
|
1377 | 0 | err = mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET); |
1378 | 0 | } |
1379 | |
|
1380 | 0 | mz_stream_mem_delete(&local_file_info_stream); |
1381 | |
|
1382 | 0 | mz_zip_print("Zip - Recover - Complete (cddisk %" PRId32 " entries %" PRId64 ")\n", |
1383 | 0 | disk_number_with_cd, number_entry); |
1384 | |
|
1385 | 0 | if (number_entry == 0) |
1386 | 0 | return err; |
1387 | | |
1388 | | /* Set new upper seek boundary for central dir mem stream */ |
1389 | 0 | disk_offset = mz_stream_tell(cd_mem_stream); |
1390 | 0 | mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset); |
1391 | | |
1392 | | /* Set new central directory info */ |
1393 | 0 | mz_zip_set_cd_stream(handle, 0, cd_mem_stream); |
1394 | 0 | mz_zip_set_number_entry(handle, number_entry); |
1395 | 0 | mz_zip_set_disk_number_with_cd(handle, disk_number_with_cd); |
1396 | |
|
1397 | 0 | return MZ_OK; |
1398 | 0 | } |
1399 | | |
1400 | 3.82k | void *mz_zip_create(void **handle) { |
1401 | 3.82k | mz_zip *zip = NULL; |
1402 | | |
1403 | 3.82k | zip = (mz_zip *)calloc(1, sizeof(mz_zip)); |
1404 | 3.82k | if (zip) |
1405 | 3.82k | zip->data_descriptor = 1; |
1406 | 3.82k | if (handle) |
1407 | 3.82k | *handle = zip; |
1408 | | |
1409 | 3.82k | return zip; |
1410 | 3.82k | } |
1411 | | |
1412 | 3.82k | void mz_zip_delete(void **handle) { |
1413 | 3.82k | mz_zip *zip = NULL; |
1414 | 3.82k | if (!handle) |
1415 | 0 | return; |
1416 | 3.82k | zip = (mz_zip *)*handle; |
1417 | 3.82k | if (zip) { |
1418 | 3.82k | free(zip); |
1419 | 3.82k | } |
1420 | 3.82k | *handle = NULL; |
1421 | 3.82k | } |
1422 | | |
1423 | 3.82k | int32_t mz_zip_open(void *handle, void *stream, int32_t mode) { |
1424 | 3.82k | mz_zip *zip = (mz_zip *)handle; |
1425 | 3.82k | int32_t err = MZ_OK; |
1426 | | |
1427 | 3.82k | if (!zip) |
1428 | 0 | return MZ_PARAM_ERROR; |
1429 | | |
1430 | 3.82k | mz_zip_print("Zip - Open\n"); |
1431 | | |
1432 | 3.82k | zip->stream = stream; |
1433 | | |
1434 | 3.82k | mz_stream_mem_create(&zip->cd_mem_stream); |
1435 | | |
1436 | 3.82k | if (mode & MZ_OPEN_MODE_WRITE) { |
1437 | 0 | mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE); |
1438 | 0 | zip->cd_stream = zip->cd_mem_stream; |
1439 | 3.82k | } else { |
1440 | 3.82k | zip->cd_stream = stream; |
1441 | 3.82k | } |
1442 | | |
1443 | 3.82k | if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND)) { |
1444 | 3.82k | if ((mode & MZ_OPEN_MODE_CREATE) == 0) { |
1445 | 3.82k | err = mz_zip_read_cd(zip); |
1446 | 3.82k | if (err != MZ_OK) { |
1447 | 909 | mz_zip_print("Zip - Error detected reading cd (%" PRId32 ")\n", err); |
1448 | 909 | if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK) |
1449 | 0 | err = MZ_OK; |
1450 | 909 | } |
1451 | 3.82k | } |
1452 | | |
1453 | 3.82k | if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND)) { |
1454 | 0 | if (zip->cd_size > 0) { |
1455 | | /* Store central directory in memory */ |
1456 | 0 | err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET); |
1457 | 0 | if (err == MZ_OK) |
1458 | 0 | err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size); |
1459 | 0 | if (err == MZ_OK) |
1460 | 0 | err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET); |
1461 | 0 | } else { |
1462 | 0 | if (zip->cd_signature == MZ_ZIP_MAGIC_ENDHEADER) { |
1463 | | /* If tiny zip then overwrite end header */ |
1464 | 0 | err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET); |
1465 | 0 | } else { |
1466 | | /* If no central directory, append new zip to end of file */ |
1467 | 0 | err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END); |
1468 | 0 | } |
1469 | 0 | } |
1470 | |
|
1471 | 0 | if (zip->disk_number_with_cd > 0) { |
1472 | | /* Move to last disk to begin appending */ |
1473 | 0 | mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->disk_number_with_cd - 1); |
1474 | 0 | } |
1475 | 3.82k | } else { |
1476 | 3.82k | zip->cd_start_pos = zip->cd_offset; |
1477 | 3.82k | } |
1478 | 3.82k | } |
1479 | | |
1480 | 3.82k | if (err != MZ_OK) { |
1481 | 909 | mz_zip_close(zip); |
1482 | 909 | return err; |
1483 | 909 | } |
1484 | | |
1485 | | /* Memory streams used to store variable length file info data */ |
1486 | 2.91k | mz_stream_mem_create(&zip->file_info_stream); |
1487 | 2.91k | mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE); |
1488 | | |
1489 | 2.91k | mz_stream_mem_create(&zip->local_file_info_stream); |
1490 | 2.91k | mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE); |
1491 | | |
1492 | 2.91k | zip->open_mode = mode; |
1493 | | |
1494 | 2.91k | return err; |
1495 | 3.82k | } |
1496 | | |
1497 | 3.82k | int32_t mz_zip_close(void *handle) { |
1498 | 3.82k | mz_zip *zip = (mz_zip *)handle; |
1499 | 3.82k | int32_t err = MZ_OK; |
1500 | | |
1501 | 3.82k | if (!zip) |
1502 | 0 | return MZ_PARAM_ERROR; |
1503 | | |
1504 | 3.82k | mz_zip_print("Zip - Close\n"); |
1505 | | |
1506 | 3.82k | if (mz_zip_entry_is_open(handle) == MZ_OK) |
1507 | 0 | err = mz_zip_entry_close(handle); |
1508 | | |
1509 | 3.82k | if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE)) |
1510 | 0 | err = mz_zip_write_cd(handle); |
1511 | | |
1512 | 3.82k | if (zip->cd_mem_stream) { |
1513 | 3.82k | mz_stream_close(zip->cd_mem_stream); |
1514 | 3.82k | mz_stream_delete(&zip->cd_mem_stream); |
1515 | 3.82k | } |
1516 | | |
1517 | 3.82k | if (zip->file_info_stream) { |
1518 | 2.91k | mz_stream_mem_close(zip->file_info_stream); |
1519 | 2.91k | mz_stream_mem_delete(&zip->file_info_stream); |
1520 | 2.91k | } |
1521 | 3.82k | if (zip->local_file_info_stream) { |
1522 | 2.91k | mz_stream_mem_close(zip->local_file_info_stream); |
1523 | 2.91k | mz_stream_mem_delete(&zip->local_file_info_stream); |
1524 | 2.91k | } |
1525 | | |
1526 | 3.82k | if (zip->comment) { |
1527 | 2.91k | free(zip->comment); |
1528 | 2.91k | zip->comment = NULL; |
1529 | 2.91k | } |
1530 | | |
1531 | 3.82k | zip->stream = NULL; |
1532 | 3.82k | zip->cd_stream = NULL; |
1533 | | |
1534 | 3.82k | return err; |
1535 | 3.82k | } |
1536 | | |
1537 | 2.91k | int32_t mz_zip_get_comment(void *handle, const char **comment) { |
1538 | 2.91k | mz_zip *zip = (mz_zip *)handle; |
1539 | 2.91k | if (!zip || !comment) |
1540 | 0 | return MZ_PARAM_ERROR; |
1541 | 2.91k | if (!zip->comment) |
1542 | 691 | return MZ_EXIST_ERROR; |
1543 | 2.22k | *comment = zip->comment; |
1544 | 2.22k | return MZ_OK; |
1545 | 2.91k | } |
1546 | | |
1547 | 0 | int32_t mz_zip_set_comment(void *handle, const char *comment) { |
1548 | 0 | mz_zip *zip = (mz_zip *)handle; |
1549 | 0 | int32_t comment_size = 0; |
1550 | 0 | if (!zip || !comment) |
1551 | 0 | return MZ_PARAM_ERROR; |
1552 | 0 | if (zip->comment) |
1553 | 0 | free(zip->comment); |
1554 | 0 | comment_size = (int32_t)strlen(comment); |
1555 | 0 | if (comment_size > UINT16_MAX) |
1556 | 0 | return MZ_PARAM_ERROR; |
1557 | 0 | zip->comment = (char *)calloc(comment_size + 1, sizeof(char)); |
1558 | 0 | if (!zip->comment) |
1559 | 0 | return MZ_MEM_ERROR; |
1560 | 0 | strncpy(zip->comment, comment, comment_size); |
1561 | 0 | return MZ_OK; |
1562 | 0 | } |
1563 | | |
1564 | 2.91k | int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby) { |
1565 | 2.91k | mz_zip *zip = (mz_zip *)handle; |
1566 | 2.91k | if (!zip || !version_madeby) |
1567 | 0 | return MZ_PARAM_ERROR; |
1568 | 2.91k | *version_madeby = zip->version_madeby; |
1569 | 2.91k | return MZ_OK; |
1570 | 2.91k | } |
1571 | | |
1572 | 0 | int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby) { |
1573 | 0 | mz_zip *zip = (mz_zip *)handle; |
1574 | 0 | if (!zip) |
1575 | 0 | return MZ_PARAM_ERROR; |
1576 | 0 | zip->version_madeby = version_madeby; |
1577 | 0 | return MZ_OK; |
1578 | 0 | } |
1579 | | |
1580 | 0 | int32_t mz_zip_set_recover(void *handle, uint8_t recover) { |
1581 | 0 | mz_zip *zip = (mz_zip *)handle; |
1582 | 0 | if (!zip) |
1583 | 0 | return MZ_PARAM_ERROR; |
1584 | 0 | zip->recover = recover; |
1585 | 0 | return MZ_OK; |
1586 | 0 | } |
1587 | | |
1588 | 0 | int32_t mz_zip_set_data_descriptor(void *handle, uint8_t data_descriptor) { |
1589 | 0 | mz_zip *zip = (mz_zip *)handle; |
1590 | 0 | if (!zip) |
1591 | 0 | return MZ_PARAM_ERROR; |
1592 | 0 | zip->data_descriptor = data_descriptor; |
1593 | 0 | return MZ_OK; |
1594 | 0 | } |
1595 | | |
1596 | 0 | int32_t mz_zip_get_stream(void *handle, void **stream) { |
1597 | 0 | mz_zip *zip = (mz_zip *)handle; |
1598 | 0 | if (!zip || !stream) |
1599 | 0 | return MZ_PARAM_ERROR; |
1600 | 0 | *stream = zip->stream; |
1601 | 0 | if (!*stream) |
1602 | 0 | return MZ_EXIST_ERROR; |
1603 | 0 | return MZ_OK; |
1604 | 0 | } |
1605 | | |
1606 | 0 | int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream) { |
1607 | 0 | mz_zip *zip = (mz_zip *)handle; |
1608 | 0 | if (!zip || !cd_stream) |
1609 | 0 | return MZ_PARAM_ERROR; |
1610 | 0 | zip->cd_offset = 0; |
1611 | 0 | zip->cd_stream = cd_stream; |
1612 | 0 | zip->cd_start_pos = cd_start_pos; |
1613 | 0 | return MZ_OK; |
1614 | 0 | } |
1615 | | |
1616 | 0 | int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream) { |
1617 | 0 | mz_zip *zip = (mz_zip *)handle; |
1618 | 0 | if (!zip || !cd_mem_stream) |
1619 | 0 | return MZ_PARAM_ERROR; |
1620 | 0 | *cd_mem_stream = zip->cd_mem_stream; |
1621 | 0 | if (!*cd_mem_stream) |
1622 | 0 | return MZ_EXIST_ERROR; |
1623 | 0 | return MZ_OK; |
1624 | 0 | } |
1625 | | |
1626 | 0 | int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry) { |
1627 | 0 | mz_zip *zip = (mz_zip *)handle; |
1628 | 0 | if (!zip) |
1629 | 0 | return MZ_PARAM_ERROR; |
1630 | 0 | zip->number_entry = number_entry; |
1631 | 0 | return MZ_OK; |
1632 | 0 | } |
1633 | | |
1634 | 2.91k | int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry) { |
1635 | 2.91k | mz_zip *zip = (mz_zip *)handle; |
1636 | 2.91k | if (!zip || !number_entry) |
1637 | 0 | return MZ_PARAM_ERROR; |
1638 | 2.91k | *number_entry = zip->number_entry; |
1639 | 2.91k | return MZ_OK; |
1640 | 2.91k | } |
1641 | | |
1642 | 0 | int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd) { |
1643 | 0 | mz_zip *zip = (mz_zip *)handle; |
1644 | 0 | if (!zip) |
1645 | 0 | return MZ_PARAM_ERROR; |
1646 | 0 | zip->disk_number_with_cd = disk_number_with_cd; |
1647 | 0 | return MZ_OK; |
1648 | 0 | } |
1649 | | |
1650 | 0 | int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd) { |
1651 | 0 | mz_zip *zip = (mz_zip *)handle; |
1652 | 0 | if (!zip || !disk_number_with_cd) |
1653 | 0 | return MZ_PARAM_ERROR; |
1654 | 0 | *disk_number_with_cd = zip->disk_number_with_cd; |
1655 | 0 | return MZ_OK; |
1656 | 0 | } |
1657 | | |
1658 | 9.87k | static int32_t mz_zip_entry_close_int(void *handle) { |
1659 | 9.87k | mz_zip *zip = (mz_zip *)handle; |
1660 | | |
1661 | 9.87k | if (zip->crypt_stream) |
1662 | 9.87k | mz_stream_delete(&zip->crypt_stream); |
1663 | 9.87k | zip->crypt_stream = NULL; |
1664 | 9.87k | if (zip->compress_stream) |
1665 | 9.68k | mz_stream_delete(&zip->compress_stream); |
1666 | 9.87k | zip->compress_stream = NULL; |
1667 | | |
1668 | 9.87k | zip->entry_opened = 0; |
1669 | | |
1670 | 9.87k | return MZ_OK; |
1671 | 9.87k | } |
1672 | | |
1673 | 9.87k | static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress_level, const char *password) { |
1674 | 9.87k | mz_zip *zip = (mz_zip *)handle; |
1675 | 9.87k | int64_t max_total_in = 0; |
1676 | 9.87k | int64_t header_size = 0; |
1677 | 9.87k | int64_t footer_size = 0; |
1678 | 9.87k | int32_t err = MZ_OK; |
1679 | 9.87k | uint8_t use_crypt = 0; |
1680 | | |
1681 | 9.87k | if (!zip) |
1682 | 0 | return MZ_PARAM_ERROR; |
1683 | | |
1684 | 9.87k | switch (zip->file_info.compression_method) { |
1685 | 9.87k | case MZ_COMPRESS_METHOD_STORE: |
1686 | 9.87k | case MZ_COMPRESS_METHOD_DEFLATE: |
1687 | | #ifdef HAVE_BZIP2 |
1688 | | case MZ_COMPRESS_METHOD_BZIP2: |
1689 | | #endif |
1690 | | #ifdef HAVE_LZMA |
1691 | | case MZ_COMPRESS_METHOD_LZMA: |
1692 | | #endif |
1693 | | #if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP) |
1694 | | case MZ_COMPRESS_METHOD_XZ: |
1695 | | #endif |
1696 | | #ifdef HAVE_ZSTD |
1697 | | case MZ_COMPRESS_METHOD_ZSTD: |
1698 | | #endif |
1699 | 9.87k | err = MZ_OK; |
1700 | 9.87k | break; |
1701 | 0 | default: |
1702 | 0 | return MZ_SUPPORT_ERROR; |
1703 | 9.87k | } |
1704 | | |
1705 | | #ifndef HAVE_WZAES |
1706 | | if (zip->file_info.aes_version) |
1707 | | return MZ_SUPPORT_ERROR; |
1708 | | #endif |
1709 | | |
1710 | 9.87k | zip->entry_raw = raw; |
1711 | | |
1712 | 9.87k | if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password)) { |
1713 | 6.93k | if (zip->open_mode & MZ_OPEN_MODE_WRITE) { |
1714 | | /* Encrypt only when we are not trying to write raw and password is supplied. */ |
1715 | 0 | if (!zip->entry_raw) |
1716 | 0 | use_crypt = 1; |
1717 | 6.93k | } else if (zip->open_mode & MZ_OPEN_MODE_READ) { |
1718 | | /* Decrypt only when password is supplied. Don't error when password */ |
1719 | | /* is not supplied as we may want to read the raw encrypted data. */ |
1720 | 6.93k | use_crypt = 1; |
1721 | 6.93k | } |
1722 | 6.93k | } |
1723 | | |
1724 | 9.87k | if ((err == MZ_OK) && (use_crypt)) { |
1725 | 6.93k | #ifdef HAVE_WZAES |
1726 | 6.93k | if (zip->file_info.aes_version) { |
1727 | 1.72k | mz_stream_wzaes_create(&zip->crypt_stream); |
1728 | 1.72k | mz_stream_wzaes_set_password(zip->crypt_stream, password); |
1729 | 1.72k | mz_stream_wzaes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode); |
1730 | 1.72k | } else |
1731 | 5.20k | #endif |
1732 | 5.20k | { |
1733 | 5.20k | #ifdef HAVE_PKCRYPT |
1734 | 5.20k | uint8_t verify1 = (uint8_t)((zip->file_info.pk_verify >> 8) & 0xff); |
1735 | 5.20k | uint8_t verify2 = (uint8_t)((zip->file_info.pk_verify) & 0xff); |
1736 | | |
1737 | 5.20k | mz_stream_pkcrypt_create(&zip->crypt_stream); |
1738 | 5.20k | mz_stream_pkcrypt_set_password(zip->crypt_stream, password); |
1739 | 5.20k | mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2); |
1740 | 5.20k | #endif |
1741 | 5.20k | } |
1742 | 6.93k | } |
1743 | | |
1744 | 9.87k | if (err == MZ_OK) { |
1745 | 9.87k | if (!zip->crypt_stream) |
1746 | 2.94k | mz_stream_raw_create(&zip->crypt_stream); |
1747 | | |
1748 | 9.87k | mz_stream_set_base(zip->crypt_stream, zip->stream); |
1749 | | |
1750 | 9.87k | err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode); |
1751 | 9.87k | } |
1752 | | |
1753 | 9.87k | if (err == MZ_OK) { |
1754 | 9.68k | if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE) |
1755 | 9.68k | mz_stream_raw_create(&zip->compress_stream); |
1756 | | #ifdef HAVE_ZLIB |
1757 | | else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE) |
1758 | | mz_stream_zlib_create(&zip->compress_stream); |
1759 | | #endif |
1760 | | #ifdef HAVE_BZIP2 |
1761 | | else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2) |
1762 | | mz_stream_bzip_create(&zip->compress_stream); |
1763 | | #endif |
1764 | | #ifdef HAVE_LIBCOMP |
1765 | | else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE || |
1766 | | zip->file_info.compression_method == MZ_COMPRESS_METHOD_XZ) { |
1767 | | mz_stream_libcomp_create(&zip->compress_stream); |
1768 | | mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_METHOD, |
1769 | | zip->file_info.compression_method); |
1770 | | } |
1771 | | #endif |
1772 | | #ifdef HAVE_LZMA |
1773 | | else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA || |
1774 | | zip->file_info.compression_method == MZ_COMPRESS_METHOD_XZ) { |
1775 | | mz_stream_lzma_create(&zip->compress_stream); |
1776 | | mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_METHOD, |
1777 | | zip->file_info.compression_method); |
1778 | | } |
1779 | | #endif |
1780 | | #ifdef HAVE_ZSTD |
1781 | | else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_ZSTD) |
1782 | | mz_stream_zstd_create(&zip->compress_stream); |
1783 | | #endif |
1784 | 0 | else |
1785 | 0 | err = MZ_PARAM_ERROR; |
1786 | 9.68k | } |
1787 | | |
1788 | 9.87k | if (err == MZ_OK) { |
1789 | 9.68k | if (zip->open_mode & MZ_OPEN_MODE_WRITE) { |
1790 | 0 | mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level); |
1791 | 9.68k | } else { |
1792 | 9.68k | int32_t set_end_of_stream = 0; |
1793 | | |
1794 | 9.68k | #ifndef HAVE_LIBCOMP |
1795 | 9.68k | if (zip->entry_raw || |
1796 | 9.68k | zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE || |
1797 | 9.68k | zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) |
1798 | 9.68k | #endif |
1799 | 9.68k | { |
1800 | 9.68k | max_total_in = zip->file_info.compressed_size; |
1801 | 9.68k | mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in); |
1802 | | |
1803 | 9.68k | if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK) |
1804 | 6.73k | max_total_in -= header_size; |
1805 | 9.68k | if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK) |
1806 | 6.73k | max_total_in -= footer_size; |
1807 | | |
1808 | 9.68k | mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in); |
1809 | 9.68k | } |
1810 | | |
1811 | 9.68k | switch (zip->file_info.compression_method) { |
1812 | 0 | case MZ_COMPRESS_METHOD_LZMA: |
1813 | 0 | case MZ_COMPRESS_METHOD_XZ: |
1814 | 0 | set_end_of_stream = (zip->file_info.flag & MZ_ZIP_FLAG_LZMA_EOS_MARKER); |
1815 | 0 | break; |
1816 | 0 | case MZ_COMPRESS_METHOD_ZSTD: |
1817 | 0 | set_end_of_stream = 1; |
1818 | 0 | break; |
1819 | 9.68k | } |
1820 | | |
1821 | 9.68k | if (set_end_of_stream) { |
1822 | 0 | mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size); |
1823 | 0 | mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size); |
1824 | 0 | } |
1825 | 9.68k | } |
1826 | | |
1827 | 9.68k | mz_stream_set_base(zip->compress_stream, zip->crypt_stream); |
1828 | | |
1829 | 9.68k | err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode); |
1830 | 9.68k | } |
1831 | | |
1832 | 9.87k | if (err == MZ_OK) { |
1833 | 9.68k | zip->entry_opened = 1; |
1834 | 9.68k | zip->entry_crc32 = 0; |
1835 | 9.68k | } else { |
1836 | 193 | mz_zip_entry_close_int(handle); |
1837 | 193 | } |
1838 | | |
1839 | 9.87k | return err; |
1840 | 9.87k | } |
1841 | | |
1842 | 45.4k | int32_t mz_zip_entry_is_open(void *handle) { |
1843 | 45.4k | mz_zip *zip = (mz_zip *)handle; |
1844 | 45.4k | if (!zip) |
1845 | 0 | return MZ_PARAM_ERROR; |
1846 | 45.4k | if (zip->entry_opened == 0) |
1847 | 6.73k | return MZ_EXIST_ERROR; |
1848 | 38.7k | return MZ_OK; |
1849 | 45.4k | } |
1850 | | |
1851 | 11.0k | int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password) { |
1852 | 11.0k | mz_zip *zip = (mz_zip *)handle; |
1853 | 11.0k | int32_t err = MZ_OK; |
1854 | 11.0k | int32_t err_shift = MZ_OK; |
1855 | | |
1856 | | #if defined(MZ_ZIP_NO_ENCRYPTION) |
1857 | | if (password) |
1858 | | return MZ_SUPPORT_ERROR; |
1859 | | #endif |
1860 | 11.0k | if (!zip || !zip->entry_scanned) |
1861 | 0 | return MZ_PARAM_ERROR; |
1862 | 11.0k | if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0) |
1863 | 0 | return MZ_PARAM_ERROR; |
1864 | | |
1865 | 11.0k | mz_zip_print("Zip - Entry - Read open (raw %" PRId32 ")\n", raw); |
1866 | | |
1867 | 11.0k | err = mz_zip_entry_seek_local_header(handle); |
1868 | 11.0k | if (err == MZ_OK) |
1869 | 10.5k | err = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream); |
1870 | | |
1871 | 11.0k | if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0) { |
1872 | | /* Perhaps we didn't compensated correctly for incorrect cd offset */ |
1873 | 192 | err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET); |
1874 | 192 | if (err_shift == MZ_OK) |
1875 | 170 | err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream); |
1876 | 192 | if (err_shift == MZ_OK) { |
1877 | 135 | zip->disk_offset_shift = 0; |
1878 | 135 | err = err_shift; |
1879 | 135 | } |
1880 | 192 | } |
1881 | | |
1882 | 11.0k | #ifdef MZ_ZIP_NO_DECOMPRESSION |
1883 | 11.0k | if (!raw && zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE) |
1884 | 943 | err = MZ_SUPPORT_ERROR; |
1885 | 11.0k | #endif |
1886 | 11.0k | if (err == MZ_OK) |
1887 | 9.87k | err = mz_zip_entry_open_int(handle, raw, 0, password); |
1888 | | |
1889 | 11.0k | return err; |
1890 | 11.0k | } |
1891 | | |
1892 | 0 | int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int16_t compress_level, uint8_t raw, const char *password) { |
1893 | 0 | mz_zip *zip = (mz_zip *)handle; |
1894 | 0 | int64_t filename_pos = -1; |
1895 | 0 | int64_t extrafield_pos = 0; |
1896 | 0 | int64_t comment_pos = 0; |
1897 | 0 | int64_t linkname_pos = 0; |
1898 | 0 | int64_t disk_number = 0; |
1899 | 0 | uint8_t is_dir = 0; |
1900 | 0 | int32_t err = MZ_OK; |
1901 | |
|
1902 | | #if defined(MZ_ZIP_NO_ENCRYPTION) |
1903 | | if (password) |
1904 | | return MZ_SUPPORT_ERROR; |
1905 | | #endif |
1906 | 0 | if (!zip || !file_info || !file_info->filename) |
1907 | 0 | return MZ_PARAM_ERROR; |
1908 | | |
1909 | 0 | if (mz_zip_entry_is_open(handle) == MZ_OK) { |
1910 | 0 | err = mz_zip_entry_close(handle); |
1911 | 0 | if (err != MZ_OK) |
1912 | 0 | return err; |
1913 | 0 | } |
1914 | | |
1915 | 0 | memcpy(&zip->file_info, file_info, sizeof(mz_zip_file)); |
1916 | |
|
1917 | 0 | mz_zip_print("Zip - Entry - Write open - %s (level %" PRId16 " raw %" PRId8 ")\n", |
1918 | 0 | zip->file_info.filename, compress_level, raw); |
1919 | |
|
1920 | 0 | mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET); |
1921 | 0 | mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file)); |
1922 | | |
1923 | | /* Copy filename, extrafield, and comment internally */ |
1924 | 0 | filename_pos = mz_stream_tell(zip->file_info_stream); |
1925 | 0 | if (file_info->filename) |
1926 | 0 | mz_stream_write(zip->file_info_stream, file_info->filename, (int32_t)strlen(file_info->filename)); |
1927 | 0 | mz_stream_write_uint8(zip->file_info_stream, 0); |
1928 | |
|
1929 | 0 | extrafield_pos = mz_stream_tell(zip->file_info_stream); |
1930 | 0 | if (file_info->extrafield) |
1931 | 0 | mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size); |
1932 | 0 | mz_stream_write_uint8(zip->file_info_stream, 0); |
1933 | |
|
1934 | 0 | comment_pos = mz_stream_tell(zip->file_info_stream); |
1935 | 0 | if (file_info->comment) |
1936 | 0 | mz_stream_write(zip->file_info_stream, file_info->comment, file_info->comment_size); |
1937 | 0 | mz_stream_write_uint8(zip->file_info_stream, 0); |
1938 | |
|
1939 | 0 | linkname_pos = mz_stream_tell(zip->file_info_stream); |
1940 | 0 | if (file_info->linkname) |
1941 | 0 | mz_stream_write(zip->file_info_stream, file_info->linkname, (int32_t)strlen(file_info->linkname)); |
1942 | 0 | mz_stream_write_uint8(zip->file_info_stream, 0); |
1943 | |
|
1944 | 0 | mz_stream_mem_get_buffer_at(zip->file_info_stream, filename_pos, (const void **)&zip->file_info.filename); |
1945 | 0 | mz_stream_mem_get_buffer_at(zip->file_info_stream, extrafield_pos, (const void **)&zip->file_info.extrafield); |
1946 | 0 | mz_stream_mem_get_buffer_at(zip->file_info_stream, comment_pos, (const void **)&zip->file_info.comment); |
1947 | 0 | mz_stream_mem_get_buffer_at(zip->file_info_stream, linkname_pos, (const void **)&zip->file_info.linkname); |
1948 | |
|
1949 | 0 | if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE) { |
1950 | 0 | if ((compress_level == 8) || (compress_level == 9)) |
1951 | 0 | zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX; |
1952 | 0 | if (compress_level == 2) |
1953 | 0 | zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST; |
1954 | 0 | if (compress_level == 1) |
1955 | 0 | zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST; |
1956 | 0 | } |
1957 | | #if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP) |
1958 | | else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA || |
1959 | | zip->file_info.compression_method == MZ_COMPRESS_METHOD_XZ) |
1960 | | zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER; |
1961 | | #endif |
1962 | |
|
1963 | 0 | if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK) |
1964 | 0 | is_dir = 1; |
1965 | |
|
1966 | 0 | if (!is_dir) { |
1967 | 0 | if (zip->data_descriptor) |
1968 | 0 | zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR; |
1969 | 0 | if (password) |
1970 | 0 | zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED; |
1971 | 0 | } |
1972 | |
|
1973 | 0 | mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number); |
1974 | 0 | zip->file_info.disk_number = (uint32_t)disk_number; |
1975 | 0 | zip->file_info.disk_offset = mz_stream_tell(zip->stream); |
1976 | |
|
1977 | 0 | if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) { |
1978 | 0 | #ifdef HAVE_PKCRYPT |
1979 | | /* Pre-calculated CRC value is required for PKWARE traditional encryption */ |
1980 | 0 | uint32_t dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date); |
1981 | 0 | zip->file_info.pk_verify = mz_zip_get_pk_verify(dos_date, zip->file_info.crc, zip->file_info.flag); |
1982 | 0 | #endif |
1983 | 0 | #ifdef HAVE_WZAES |
1984 | 0 | if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0) |
1985 | 0 | zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256; |
1986 | 0 | #endif |
1987 | 0 | } |
1988 | |
|
1989 | 0 | zip->file_info.crc = 0; |
1990 | 0 | zip->file_info.compressed_size = 0; |
1991 | |
|
1992 | 0 | if ((compress_level == 0) || (is_dir)) |
1993 | 0 | zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE; |
1994 | |
|
1995 | 0 | #ifdef MZ_ZIP_NO_COMPRESSION |
1996 | 0 | if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE) |
1997 | 0 | err = MZ_SUPPORT_ERROR; |
1998 | 0 | #endif |
1999 | 0 | if (err == MZ_OK) |
2000 | 0 | err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info); |
2001 | 0 | if (err == MZ_OK) |
2002 | 0 | err = mz_zip_entry_open_int(handle, raw, compress_level, password); |
2003 | |
|
2004 | 0 | return err; |
2005 | 0 | } |
2006 | | |
2007 | 9.68k | int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len) { |
2008 | 9.68k | mz_zip *zip = (mz_zip *)handle; |
2009 | 9.68k | int32_t read = 0; |
2010 | | |
2011 | 9.68k | if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) |
2012 | 0 | return MZ_PARAM_ERROR; |
2013 | 9.68k | if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) /* zlib limitation */ |
2014 | 0 | return MZ_PARAM_ERROR; |
2015 | 9.68k | if (len == 0) |
2016 | 0 | return MZ_PARAM_ERROR; |
2017 | | |
2018 | 9.68k | if (zip->file_info.compressed_size == 0) |
2019 | 1.00k | return 0; |
2020 | | |
2021 | | /* Read entire entry even if uncompressed_size = 0, otherwise */ |
2022 | | /* aes encryption validation will fail if compressed_size > 0 */ |
2023 | 8.67k | read = mz_stream_read(zip->compress_stream, buf, len); |
2024 | 8.67k | if (read > 0) |
2025 | 5.84k | zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read); |
2026 | | |
2027 | 8.67k | mz_zip_print("Zip - Entry - Read - %" PRId32 " (max %" PRId32 ")\n", read, len); |
2028 | | |
2029 | 8.67k | return read; |
2030 | 9.68k | } |
2031 | | |
2032 | 0 | int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len) { |
2033 | 0 | mz_zip *zip = (mz_zip *)handle; |
2034 | 0 | int32_t written = 0; |
2035 | |
|
2036 | 0 | if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) |
2037 | 0 | return MZ_PARAM_ERROR; |
2038 | 0 | written = mz_stream_write(zip->compress_stream, buf, len); |
2039 | 0 | if (written > 0) |
2040 | 0 | zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written); |
2041 | |
|
2042 | 0 | mz_zip_print("Zip - Entry - Write - %" PRId32 " (max %" PRId32 ")\n", written, len); |
2043 | |
|
2044 | 0 | return written; |
2045 | 0 | } |
2046 | | |
2047 | | int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size, |
2048 | 9.68k | int64_t *uncompressed_size) { |
2049 | 9.68k | mz_zip *zip = (mz_zip *)handle; |
2050 | 9.68k | int64_t total_in = 0; |
2051 | 9.68k | int32_t err = MZ_OK; |
2052 | 9.68k | uint8_t zip64 = 0; |
2053 | | |
2054 | 9.68k | if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) |
2055 | 0 | return MZ_PARAM_ERROR; |
2056 | | |
2057 | 9.68k | mz_stream_close(zip->compress_stream); |
2058 | | |
2059 | 9.68k | mz_zip_print("Zip - Entry - Read Close\n"); |
2060 | | |
2061 | 9.68k | if (crc32) |
2062 | 0 | *crc32 = zip->file_info.crc; |
2063 | 9.68k | if (compressed_size) |
2064 | 0 | *compressed_size = zip->file_info.compressed_size; |
2065 | 9.68k | if (uncompressed_size) |
2066 | 0 | *uncompressed_size = zip->file_info.uncompressed_size; |
2067 | | |
2068 | 9.68k | mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in); |
2069 | | |
2070 | 9.68k | if ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) && |
2071 | 9.68k | ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0) && |
2072 | 9.68k | (crc32 || compressed_size || uncompressed_size)) { |
2073 | | /* Check to see if data descriptor is zip64 bit format or not */ |
2074 | 0 | if (mz_zip_extrafield_contains(zip->local_file_info.extrafield, |
2075 | 0 | zip->local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK) |
2076 | 0 | zip64 = 1; |
2077 | |
|
2078 | 0 | err = mz_zip_entry_seek_local_header(handle); |
2079 | | |
2080 | | /* Seek to end of compressed stream since we might have over-read during compression */ |
2081 | 0 | if (err == MZ_OK) |
2082 | 0 | err = mz_stream_seek(zip->stream, MZ_ZIP_SIZE_LD_ITEM + |
2083 | 0 | (int64_t)zip->local_file_info.filename_size + |
2084 | 0 | (int64_t)zip->local_file_info.extrafield_size + |
2085 | 0 | total_in, MZ_SEEK_CUR); |
2086 | | |
2087 | | /* Read data descriptor */ |
2088 | 0 | if (err == MZ_OK) |
2089 | 0 | err = mz_zip_entry_read_descriptor(zip->stream, zip64, |
2090 | 0 | crc32, compressed_size, uncompressed_size); |
2091 | 0 | } |
2092 | | |
2093 | | /* If entire entry was not read verification will fail */ |
2094 | 9.68k | if ((err == MZ_OK) && (total_in == zip->file_info.compressed_size) && (!zip->entry_raw)) { |
2095 | 1.19k | #ifdef HAVE_WZAES |
2096 | | /* AES zip version AE-1 will expect a valid crc as well */ |
2097 | 1.19k | if (zip->file_info.aes_version <= 0x0001) |
2098 | 960 | #endif |
2099 | 960 | { |
2100 | 960 | if (zip->entry_crc32 != zip->file_info.crc) { |
2101 | 111 | mz_zip_print("Zip - Entry - Crc failed (actual 0x%08" PRIx32 " expected 0x%08" PRIx32 ")\n", |
2102 | 111 | zip->entry_crc32, zip->file_info.crc); |
2103 | | |
2104 | 111 | err = MZ_CRC_ERROR; |
2105 | 111 | } |
2106 | 960 | } |
2107 | 1.19k | } |
2108 | | |
2109 | 9.68k | mz_zip_entry_close_int(handle); |
2110 | | |
2111 | 9.68k | return err; |
2112 | 9.68k | } |
2113 | | |
2114 | | int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size, |
2115 | 0 | int64_t uncompressed_size) { |
2116 | 0 | mz_zip *zip = (mz_zip *)handle; |
2117 | 0 | int64_t end_disk_number = 0; |
2118 | 0 | int32_t err = MZ_OK; |
2119 | 0 | uint8_t zip64 = 0; |
2120 | |
|
2121 | 0 | if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) |
2122 | 0 | return MZ_PARAM_ERROR; |
2123 | | |
2124 | 0 | mz_stream_close(zip->compress_stream); |
2125 | |
|
2126 | 0 | if (!zip->entry_raw) |
2127 | 0 | crc32 = zip->entry_crc32; |
2128 | |
|
2129 | 0 | mz_zip_print("Zip - Entry - Write Close (crc 0x%08" PRIx32 " cs %" PRId64 " ucs %" PRId64 ")\n", |
2130 | 0 | crc32, compressed_size, uncompressed_size); |
2131 | | |
2132 | | /* If sizes are not set, then read them from the compression stream */ |
2133 | 0 | if (compressed_size < 0) |
2134 | 0 | mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size); |
2135 | 0 | if (uncompressed_size < 0) |
2136 | 0 | mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size); |
2137 | |
|
2138 | 0 | if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) { |
2139 | 0 | mz_stream_set_base(zip->crypt_stream, zip->stream); |
2140 | 0 | err = mz_stream_close(zip->crypt_stream); |
2141 | |
|
2142 | 0 | mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size); |
2143 | 0 | } |
2144 | |
|
2145 | 0 | mz_zip_entry_needs_zip64(&zip->file_info, 1, &zip64); |
2146 | |
|
2147 | 0 | if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)) { |
2148 | | /* Determine if we need to write data descriptor in zip64 format, |
2149 | | if local extrafield was saved with zip64 extrafield */ |
2150 | |
|
2151 | 0 | if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) |
2152 | 0 | err = mz_zip_entry_write_descriptor(zip->stream, |
2153 | 0 | zip64, 0, compressed_size, 0); |
2154 | 0 | else |
2155 | 0 | err = mz_zip_entry_write_descriptor(zip->stream, |
2156 | 0 | zip64, crc32, compressed_size, uncompressed_size); |
2157 | 0 | } |
2158 | | |
2159 | | /* Write file info to central directory */ |
2160 | |
|
2161 | 0 | mz_zip_print("Zip - Entry - Write cd (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n", |
2162 | 0 | uncompressed_size, compressed_size, crc32); |
2163 | |
|
2164 | 0 | zip->file_info.crc = crc32; |
2165 | 0 | zip->file_info.compressed_size = compressed_size; |
2166 | 0 | zip->file_info.uncompressed_size = uncompressed_size; |
2167 | |
|
2168 | 0 | if (err == MZ_OK) |
2169 | 0 | err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info); |
2170 | | |
2171 | | /* Update local header with crc32 and sizes */ |
2172 | 0 | if ((err == MZ_OK) && ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) == 0) && |
2173 | 0 | ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0)) { |
2174 | | /* Save the disk number and position we are to seek back after updating local header */ |
2175 | 0 | int64_t end_pos = mz_stream_tell(zip->stream); |
2176 | 0 | mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &end_disk_number); |
2177 | |
|
2178 | 0 | err = mz_zip_entry_seek_local_header(handle); |
2179 | |
|
2180 | 0 | if (err == MZ_OK) { |
2181 | | /* Seek to crc32 and sizes offset in local header */ |
2182 | 0 | err = mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number); |
2183 | 0 | if (err == MZ_OK) |
2184 | 0 | err = mz_stream_seek(zip->stream, zip->file_info.disk_offset + MZ_ZIP_OFFSET_CRC_SIZES, MZ_SEEK_SET); |
2185 | 0 | } |
2186 | |
|
2187 | 0 | if (err == MZ_OK) |
2188 | 0 | err = mz_zip_entry_write_crc_sizes(zip->stream, zip64, 0, &zip->file_info); |
2189 | | |
2190 | | /* Seek to and update zip64 extension sizes */ |
2191 | 0 | if ((err == MZ_OK) && (zip64)) { |
2192 | 0 | int64_t filename_size = zip->file_info.filename_size; |
2193 | |
|
2194 | 0 | if (filename_size == 0) |
2195 | 0 | filename_size = strlen(zip->file_info.filename); |
2196 | | |
2197 | | /* Since we write zip64 extension first we know its offset */ |
2198 | 0 | err = mz_stream_seek(zip->stream, 2 + 2 + filename_size + 4, MZ_SEEK_CUR); |
2199 | |
|
2200 | 0 | if (err == MZ_OK) |
2201 | 0 | err = mz_stream_write_uint64(zip->stream, zip->file_info.uncompressed_size); |
2202 | 0 | if (err == MZ_OK) |
2203 | 0 | err = mz_stream_write_uint64(zip->stream, zip->file_info.compressed_size); |
2204 | 0 | } |
2205 | |
|
2206 | 0 | mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, end_disk_number); |
2207 | 0 | mz_stream_seek(zip->stream, end_pos, MZ_SEEK_SET); |
2208 | 0 | } |
2209 | |
|
2210 | 0 | zip->number_entry += 1; |
2211 | |
|
2212 | 0 | mz_zip_entry_close_int(handle); |
2213 | |
|
2214 | 0 | return err; |
2215 | 0 | } |
2216 | | |
2217 | 11.0k | int32_t mz_zip_entry_seek_local_header(void *handle) { |
2218 | 11.0k | mz_zip *zip = (mz_zip *)handle; |
2219 | 11.0k | int64_t disk_size = 0; |
2220 | 11.0k | uint32_t disk_number = zip->file_info.disk_number; |
2221 | | |
2222 | 11.0k | if (disk_number == zip->disk_number_with_cd) { |
2223 | 3.84k | mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size); |
2224 | 3.84k | if ((disk_size == 0) || ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)) |
2225 | 3.84k | disk_number = (uint32_t)-1; |
2226 | 3.84k | } |
2227 | | |
2228 | 11.0k | mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, disk_number); |
2229 | | |
2230 | 11.0k | mz_zip_print("Zip - Entry - Seek local (disk %" PRId32 " offset %" PRId64 ")\n", |
2231 | 11.0k | disk_number, zip->file_info.disk_offset); |
2232 | | |
2233 | | /* Guard against seek overflows */ |
2234 | 11.0k | if ((zip->disk_offset_shift > 0) && |
2235 | 11.0k | (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift))) |
2236 | 22 | return MZ_FORMAT_ERROR; |
2237 | | |
2238 | 11.0k | return mz_stream_seek(zip->stream, zip->file_info.disk_offset + zip->disk_offset_shift, MZ_SEEK_SET); |
2239 | 11.0k | } |
2240 | | |
2241 | 0 | int32_t mz_zip_entry_get_compress_stream(void *handle, void **compress_stream) { |
2242 | 0 | mz_zip *zip = (mz_zip *)handle; |
2243 | 0 | if (!zip || !compress_stream) |
2244 | 0 | return MZ_PARAM_ERROR; |
2245 | 0 | *compress_stream = zip->compress_stream; |
2246 | 0 | if (!*compress_stream) |
2247 | 0 | return MZ_EXIST_ERROR; |
2248 | 0 | return MZ_OK; |
2249 | 0 | } |
2250 | | |
2251 | 12.5k | int32_t mz_zip_entry_close(void *handle) { |
2252 | 12.5k | return mz_zip_entry_close_raw(handle, UINT64_MAX, 0); |
2253 | 12.5k | } |
2254 | | |
2255 | 12.5k | int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32) { |
2256 | 12.5k | mz_zip *zip = (mz_zip *)handle; |
2257 | 12.5k | int32_t err = MZ_OK; |
2258 | | |
2259 | 12.5k | if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) |
2260 | 2.91k | return MZ_PARAM_ERROR; |
2261 | | |
2262 | 9.68k | if (zip->open_mode & MZ_OPEN_MODE_WRITE) |
2263 | 0 | err = mz_zip_entry_write_close(handle, crc32, UINT64_MAX, uncompressed_size); |
2264 | 9.68k | else |
2265 | 9.68k | err = mz_zip_entry_read_close(handle, NULL, NULL, NULL); |
2266 | | |
2267 | 9.68k | return err; |
2268 | 12.5k | } |
2269 | | |
2270 | 9.68k | int32_t mz_zip_entry_is_dir(void *handle) { |
2271 | 9.68k | mz_zip *zip = (mz_zip *)handle; |
2272 | 9.68k | int32_t filename_length = 0; |
2273 | | |
2274 | 9.68k | if (!zip || !zip->entry_scanned) |
2275 | 0 | return MZ_PARAM_ERROR; |
2276 | 9.68k | if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK) |
2277 | 1.47k | return MZ_OK; |
2278 | | |
2279 | 8.21k | filename_length = (int32_t)strlen(zip->file_info.filename); |
2280 | 8.21k | if (filename_length > 0) { |
2281 | 753 | if ((zip->file_info.filename[filename_length - 1] == '/') || |
2282 | 753 | (zip->file_info.filename[filename_length - 1] == '\\')) |
2283 | 417 | return MZ_OK; |
2284 | 753 | } |
2285 | 7.79k | return MZ_EXIST_ERROR; |
2286 | 8.21k | } |
2287 | | |
2288 | 0 | int32_t mz_zip_entry_is_symlink(void *handle) { |
2289 | 0 | mz_zip *zip = (mz_zip *)handle; |
2290 | |
|
2291 | 0 | if (!zip || !zip->entry_scanned) |
2292 | 0 | return MZ_PARAM_ERROR; |
2293 | 0 | if (mz_zip_attrib_is_symlink(zip->file_info.external_fa, zip->file_info.version_madeby) != MZ_OK) |
2294 | 0 | return MZ_EXIST_ERROR; |
2295 | | |
2296 | 0 | return MZ_OK; |
2297 | 0 | } |
2298 | | |
2299 | 11.0k | int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info) { |
2300 | 11.0k | mz_zip *zip = (mz_zip *)handle; |
2301 | | |
2302 | 11.0k | if (!zip) |
2303 | 0 | return MZ_PARAM_ERROR; |
2304 | | |
2305 | 11.0k | if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0) { |
2306 | 11.0k | if (!zip->entry_scanned) |
2307 | 0 | return MZ_PARAM_ERROR; |
2308 | 11.0k | } |
2309 | | |
2310 | 11.0k | *file_info = &zip->file_info; |
2311 | 11.0k | return MZ_OK; |
2312 | 11.0k | } |
2313 | | |
2314 | 0 | int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info) { |
2315 | 0 | mz_zip *zip = (mz_zip *)handle; |
2316 | 0 | if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) |
2317 | 0 | return MZ_PARAM_ERROR; |
2318 | 0 | *local_file_info = &zip->local_file_info; |
2319 | 0 | return MZ_OK; |
2320 | 0 | } |
2321 | | |
2322 | 0 | int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size) { |
2323 | 0 | mz_zip *zip = (mz_zip *)handle; |
2324 | |
|
2325 | 0 | if (!zip || mz_zip_entry_is_open(handle) != MZ_OK) |
2326 | 0 | return MZ_PARAM_ERROR; |
2327 | | |
2328 | 0 | zip->file_info.extrafield = extrafield; |
2329 | 0 | zip->file_info.extrafield_size = extrafield_size; |
2330 | 0 | return MZ_OK; |
2331 | 0 | } |
2332 | | |
2333 | 71.2k | static int32_t mz_zip_goto_next_entry_int(void *handle) { |
2334 | 71.2k | mz_zip *zip = (mz_zip *)handle; |
2335 | 71.2k | int32_t err = MZ_OK; |
2336 | | |
2337 | 71.2k | if (!zip) |
2338 | 0 | return MZ_PARAM_ERROR; |
2339 | | |
2340 | 71.2k | zip->entry_scanned = 0; |
2341 | | |
2342 | 71.2k | mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1); |
2343 | | |
2344 | 71.2k | err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET); |
2345 | 71.2k | if (err == MZ_OK) |
2346 | 71.2k | err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream); |
2347 | 71.2k | if (err == MZ_OK) |
2348 | 58.4k | zip->entry_scanned = 1; |
2349 | 71.2k | return err; |
2350 | 71.2k | } |
2351 | | |
2352 | 9.68k | int64_t mz_zip_get_entry(void *handle) { |
2353 | 9.68k | mz_zip *zip = (mz_zip *)handle; |
2354 | | |
2355 | 9.68k | if (!zip) |
2356 | 0 | return MZ_PARAM_ERROR; |
2357 | | |
2358 | 9.68k | return zip->cd_current_pos; |
2359 | 9.68k | } |
2360 | | |
2361 | 0 | int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos) { |
2362 | 0 | mz_zip *zip = (mz_zip *)handle; |
2363 | |
|
2364 | 0 | if (!zip) |
2365 | 0 | return MZ_PARAM_ERROR; |
2366 | | |
2367 | 0 | if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size) |
2368 | 0 | return MZ_PARAM_ERROR; |
2369 | | |
2370 | 0 | zip->cd_current_pos = cd_pos; |
2371 | |
|
2372 | 0 | return mz_zip_goto_next_entry_int(handle); |
2373 | 0 | } |
2374 | | |
2375 | 14.4k | int32_t mz_zip_goto_first_entry(void *handle) { |
2376 | 14.4k | mz_zip *zip = (mz_zip *)handle; |
2377 | | |
2378 | 14.4k | if (!zip) |
2379 | 0 | return MZ_PARAM_ERROR; |
2380 | | |
2381 | 14.4k | zip->cd_current_pos = zip->cd_start_pos; |
2382 | | |
2383 | 14.4k | return mz_zip_goto_next_entry_int(handle); |
2384 | 14.4k | } |
2385 | | |
2386 | 56.8k | int32_t mz_zip_goto_next_entry(void *handle) { |
2387 | 56.8k | mz_zip *zip = (mz_zip *)handle; |
2388 | | |
2389 | 56.8k | if (!zip) |
2390 | 0 | return MZ_PARAM_ERROR; |
2391 | | |
2392 | 56.8k | zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size + |
2393 | 56.8k | zip->file_info.extrafield_size + zip->file_info.comment_size; |
2394 | | |
2395 | 56.8k | return mz_zip_goto_next_entry_int(handle); |
2396 | 56.8k | } |
2397 | | |
2398 | 11.6k | int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case) { |
2399 | 11.6k | mz_zip *zip = (mz_zip *)handle; |
2400 | 11.6k | int32_t err = MZ_OK; |
2401 | 11.6k | int32_t result = 0; |
2402 | | |
2403 | 11.6k | if (!zip || !filename) |
2404 | 0 | return MZ_PARAM_ERROR; |
2405 | | |
2406 | | /* If we are already on the current entry, no need to search */ |
2407 | 11.6k | if (zip->entry_scanned && zip->file_info.filename) { |
2408 | 1.66k | result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case); |
2409 | 1.66k | if (result == 0) |
2410 | 136 | return MZ_OK; |
2411 | 1.66k | } |
2412 | | |
2413 | | /* Search all entries starting at the first */ |
2414 | 11.5k | err = mz_zip_goto_first_entry(handle); |
2415 | 58.7k | while (err == MZ_OK) { |
2416 | 47.3k | result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case); |
2417 | 47.3k | if (result == 0) |
2418 | 99 | return MZ_OK; |
2419 | | |
2420 | 47.2k | err = mz_zip_goto_next_entry(handle); |
2421 | 47.2k | } |
2422 | | |
2423 | 11.4k | return err; |
2424 | 11.5k | } |
2425 | | |
2426 | 0 | int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb) { |
2427 | 0 | mz_zip *zip = (mz_zip *)handle; |
2428 | 0 | int32_t err = MZ_OK; |
2429 | 0 | int32_t result = 0; |
2430 | | |
2431 | | /* Search first entry looking for match */ |
2432 | 0 | err = mz_zip_goto_first_entry(handle); |
2433 | 0 | if (err != MZ_OK) |
2434 | 0 | return err; |
2435 | | |
2436 | 0 | result = cb(handle, userdata, &zip->file_info); |
2437 | 0 | if (result == 0) |
2438 | 0 | return MZ_OK; |
2439 | | |
2440 | 0 | return mz_zip_locate_next_entry(handle, userdata, cb); |
2441 | 0 | } |
2442 | | |
2443 | 0 | int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb) { |
2444 | 0 | mz_zip *zip = (mz_zip *)handle; |
2445 | 0 | int32_t err = MZ_OK; |
2446 | 0 | int32_t result = 0; |
2447 | | |
2448 | | /* Search next entries looking for match */ |
2449 | 0 | err = mz_zip_goto_next_entry(handle); |
2450 | 0 | while (err == MZ_OK) { |
2451 | 0 | result = cb(handle, userdata, &zip->file_info); |
2452 | 0 | if (result == 0) |
2453 | 0 | return MZ_OK; |
2454 | | |
2455 | 0 | err = mz_zip_goto_next_entry(handle); |
2456 | 0 | } |
2457 | | |
2458 | 0 | return err; |
2459 | 0 | } |
2460 | | |
2461 | | /***************************************************************************/ |
2462 | | |
2463 | 9.68k | int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby) { |
2464 | 9.68k | uint32_t posix_attrib = 0; |
2465 | 9.68k | uint8_t system = MZ_HOST_SYSTEM(version_madeby); |
2466 | 9.68k | int32_t err = MZ_OK; |
2467 | | |
2468 | 9.68k | err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib); |
2469 | 9.68k | if (err == MZ_OK) { |
2470 | 4.71k | if ((posix_attrib & 0170000) == 0040000) /* S_ISDIR */ |
2471 | 1.47k | return MZ_OK; |
2472 | 4.71k | } |
2473 | | |
2474 | 8.21k | return MZ_EXIST_ERROR; |
2475 | 9.68k | } |
2476 | | |
2477 | 0 | int32_t mz_zip_attrib_is_symlink(uint32_t attrib, int32_t version_madeby) { |
2478 | 0 | uint32_t posix_attrib = 0; |
2479 | 0 | uint8_t system = MZ_HOST_SYSTEM(version_madeby); |
2480 | 0 | int32_t err = MZ_OK; |
2481 | |
|
2482 | 0 | err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib); |
2483 | 0 | if (err == MZ_OK) { |
2484 | 0 | if ((posix_attrib & 0170000) == 0120000) /* S_ISLNK */ |
2485 | 0 | return MZ_OK; |
2486 | 0 | } |
2487 | | |
2488 | 0 | return MZ_EXIST_ERROR; |
2489 | 0 | } |
2490 | | |
2491 | 9.68k | int32_t mz_zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys, uint32_t *target_attrib) { |
2492 | 9.68k | if (!target_attrib) |
2493 | 0 | return MZ_PARAM_ERROR; |
2494 | | |
2495 | 9.68k | *target_attrib = 0; |
2496 | | |
2497 | 9.68k | if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS)) { |
2498 | 1.45k | if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS)) { |
2499 | 0 | *target_attrib = src_attrib; |
2500 | 0 | return MZ_OK; |
2501 | 0 | } |
2502 | 1.45k | if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (target_sys == MZ_HOST_SYSTEM_RISCOS)) |
2503 | 1.45k | return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib); |
2504 | 8.22k | } else if ((src_sys == MZ_HOST_SYSTEM_UNIX) || (src_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (src_sys == MZ_HOST_SYSTEM_RISCOS)) { |
2505 | | /* If high bytes are set, it contains unix specific attributes */ |
2506 | 3.25k | if ((src_attrib >> 16) != 0) |
2507 | 2.43k | src_attrib >>= 16; |
2508 | | |
2509 | 3.25k | if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (target_sys == MZ_HOST_SYSTEM_RISCOS)) { |
2510 | 3.25k | *target_attrib = src_attrib; |
2511 | 3.25k | return MZ_OK; |
2512 | 3.25k | } |
2513 | 0 | if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS)) |
2514 | 0 | return mz_zip_attrib_posix_to_win32(src_attrib, target_attrib); |
2515 | 0 | } |
2516 | | |
2517 | 4.96k | return MZ_SUPPORT_ERROR; |
2518 | 9.68k | } |
2519 | | |
2520 | 0 | int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib) { |
2521 | 0 | if (!win32_attrib) |
2522 | 0 | return MZ_PARAM_ERROR; |
2523 | | |
2524 | 0 | *win32_attrib = 0; |
2525 | | |
2526 | | /* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */ |
2527 | 0 | if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0) |
2528 | 0 | *win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */ |
2529 | | /* S_IFLNK */ |
2530 | 0 | if ((posix_attrib & 0170000) == 0120000) |
2531 | 0 | *win32_attrib |= 0x400; /* FILE_ATTRIBUTE_REPARSE_POINT */ |
2532 | | /* S_IFDIR */ |
2533 | 0 | else if ((posix_attrib & 0170000) == 0040000) |
2534 | 0 | *win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */ |
2535 | | /* S_IFREG */ |
2536 | 0 | else |
2537 | 0 | *win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */ |
2538 | |
|
2539 | 0 | return MZ_OK; |
2540 | 0 | } |
2541 | | |
2542 | 1.45k | int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib) { |
2543 | 1.45k | if (!posix_attrib) |
2544 | 0 | return MZ_PARAM_ERROR; |
2545 | | |
2546 | 1.45k | *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */ |
2547 | | /* FILE_ATTRIBUTE_READONLY */ |
2548 | 1.45k | if ((win32_attrib & 0x01) == 0) |
2549 | 720 | *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */ |
2550 | | /* FILE_ATTRIBUTE_REPARSE_POINT */ |
2551 | 1.45k | if ((win32_attrib & 0x400) == 0x400) |
2552 | 414 | *posix_attrib |= 0120000; /* S_IFLNK */ |
2553 | | /* FILE_ATTRIBUTE_DIRECTORY */ |
2554 | 1.04k | else if ((win32_attrib & 0x10) == 0x10) |
2555 | 436 | *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */ |
2556 | 607 | else |
2557 | 607 | *posix_attrib |= 0100000; /* S_IFREG */ |
2558 | | |
2559 | 1.45k | return MZ_OK; |
2560 | 1.45k | } |
2561 | | |
2562 | | /***************************************************************************/ |
2563 | | |
2564 | 0 | int32_t mz_zip_extrafield_find(void *stream, uint16_t type, int32_t max_seek, uint16_t *length) { |
2565 | 0 | int32_t err = MZ_OK; |
2566 | 0 | uint16_t field_type = 0; |
2567 | 0 | uint16_t field_length = 0; |
2568 | |
|
2569 | 0 | if (max_seek < 4) |
2570 | 0 | return MZ_EXIST_ERROR; |
2571 | | |
2572 | 0 | do { |
2573 | 0 | err = mz_stream_read_uint16(stream, &field_type); |
2574 | 0 | if (err == MZ_OK) |
2575 | 0 | err = mz_stream_read_uint16(stream, &field_length); |
2576 | 0 | if (err != MZ_OK) |
2577 | 0 | break; |
2578 | | |
2579 | 0 | if (type == field_type) { |
2580 | 0 | if (length) |
2581 | 0 | *length = field_length; |
2582 | 0 | return MZ_OK; |
2583 | 0 | } |
2584 | | |
2585 | 0 | max_seek -= field_length - 4; |
2586 | 0 | if (max_seek < 0) |
2587 | 0 | return MZ_EXIST_ERROR; |
2588 | | |
2589 | 0 | err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR); |
2590 | 0 | } while (err == MZ_OK); |
2591 | | |
2592 | 0 | return MZ_EXIST_ERROR; |
2593 | 0 | } |
2594 | | |
2595 | | int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size, |
2596 | 0 | uint16_t type, uint16_t *length) { |
2597 | 0 | void *file_extra_stream = NULL; |
2598 | 0 | int32_t err = MZ_OK; |
2599 | |
|
2600 | 0 | if (!extrafield || !extrafield_size) |
2601 | 0 | return MZ_PARAM_ERROR; |
2602 | | |
2603 | 0 | mz_stream_mem_create(&file_extra_stream); |
2604 | 0 | mz_stream_mem_set_buffer(file_extra_stream, (void *)extrafield, extrafield_size); |
2605 | |
|
2606 | 0 | err = mz_zip_extrafield_find(file_extra_stream, type, extrafield_size, length); |
2607 | |
|
2608 | 0 | mz_stream_mem_delete(&file_extra_stream); |
2609 | |
|
2610 | 0 | return err; |
2611 | 0 | } |
2612 | | |
2613 | 70.7k | int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length) { |
2614 | 70.7k | int32_t err = MZ_OK; |
2615 | 70.7k | if (!type || !length) |
2616 | 0 | return MZ_PARAM_ERROR; |
2617 | 70.7k | err = mz_stream_read_uint16(stream, type); |
2618 | 70.7k | if (err == MZ_OK) |
2619 | 70.6k | err = mz_stream_read_uint16(stream, length); |
2620 | 70.7k | return err; |
2621 | 70.7k | } |
2622 | | |
2623 | 0 | int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length) { |
2624 | 0 | int32_t err = MZ_OK; |
2625 | 0 | err = mz_stream_write_uint16(stream, type); |
2626 | 0 | if (err == MZ_OK) |
2627 | 0 | err = mz_stream_write_uint16(stream, length); |
2628 | 0 | return err; |
2629 | 0 | } |
2630 | | |
2631 | | /***************************************************************************/ |
2632 | | |
2633 | 0 | static int32_t mz_zip_invalid_date(const struct tm *ptm) { |
2634 | 0 | #define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max)) |
2635 | 0 | return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */ |
2636 | 0 | !datevalue_in_range(0, 11, ptm->tm_mon) || |
2637 | 0 | !datevalue_in_range(1, 31, ptm->tm_mday) || |
2638 | 0 | !datevalue_in_range(0, 23, ptm->tm_hour) || |
2639 | 0 | !datevalue_in_range(0, 59, ptm->tm_min) || |
2640 | 0 | !datevalue_in_range(0, 59, ptm->tm_sec)); |
2641 | 0 | #undef datevalue_in_range |
2642 | 0 | } |
2643 | | |
2644 | 71.6k | static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm) { |
2645 | 71.6k | uint64_t date = (uint64_t)(dos_date >> 16); |
2646 | | |
2647 | 71.6k | ptm->tm_mday = (uint16_t)(date & 0x1f); |
2648 | 71.6k | ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1); |
2649 | 71.6k | ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80); |
2650 | 71.6k | ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800); |
2651 | 71.6k | ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20); |
2652 | 71.6k | ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f)); |
2653 | 71.6k | ptm->tm_isdst = -1; |
2654 | 71.6k | } |
2655 | | |
2656 | 0 | int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm) { |
2657 | 0 | if (!ptm) |
2658 | 0 | return MZ_PARAM_ERROR; |
2659 | | |
2660 | 0 | mz_zip_dosdate_to_raw_tm(dos_date, ptm); |
2661 | |
|
2662 | 0 | if (mz_zip_invalid_date(ptm)) { |
2663 | | /* Invalid date stored, so don't return it */ |
2664 | 0 | memset(ptm, 0, sizeof(struct tm)); |
2665 | 0 | return MZ_FORMAT_ERROR; |
2666 | 0 | } |
2667 | 0 | return MZ_OK; |
2668 | 0 | } |
2669 | | |
2670 | 71.6k | time_t mz_zip_dosdate_to_time_t(uint64_t dos_date) { |
2671 | 71.6k | struct tm ptm; |
2672 | 71.6k | mz_zip_dosdate_to_raw_tm(dos_date, &ptm); |
2673 | 71.6k | return mktime(&ptm); |
2674 | 71.6k | } |
2675 | | |
2676 | 0 | int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm) { |
2677 | 0 | struct tm ltm; |
2678 | 0 | if (!ptm) |
2679 | 0 | return MZ_PARAM_ERROR; |
2680 | 0 | if (!localtime_r(&unix_time, <m)) { /* Returns a 1900-based year */ |
2681 | | /* Invalid date stored, so don't return it */ |
2682 | 0 | memset(ptm, 0, sizeof(struct tm)); |
2683 | 0 | return MZ_INTERNAL_ERROR; |
2684 | 0 | } |
2685 | 0 | memcpy(ptm, <m, sizeof(struct tm)); |
2686 | 0 | return MZ_OK; |
2687 | 0 | } |
2688 | | |
2689 | 0 | uint32_t mz_zip_time_t_to_dos_date(time_t unix_time) { |
2690 | 0 | struct tm ptm; |
2691 | 0 | mz_zip_time_t_to_tm(unix_time, &ptm); |
2692 | 0 | return mz_zip_tm_to_dosdate((const struct tm *)&ptm); |
2693 | 0 | } |
2694 | | |
2695 | 0 | uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm) { |
2696 | 0 | struct tm fixed_tm; |
2697 | | |
2698 | | /* Years supported: */ |
2699 | | |
2700 | | /* [00, 79] (assumed to be between 2000 and 2079) */ |
2701 | | /* [80, 207] (assumed to be between 1980 and 2107, typical output of old */ |
2702 | | /* software that does 'year-1900' to get a double digit year) */ |
2703 | | /* [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.) */ |
2704 | |
|
2705 | 0 | memcpy(&fixed_tm, ptm, sizeof(struct tm)); |
2706 | 0 | if (fixed_tm.tm_year >= 1980) /* range [1980, 2107] */ |
2707 | 0 | fixed_tm.tm_year -= 1980; |
2708 | 0 | else if (fixed_tm.tm_year >= 80) /* range [80, 207] */ |
2709 | 0 | fixed_tm.tm_year -= 80; |
2710 | 0 | else /* range [00, 79] */ |
2711 | 0 | fixed_tm.tm_year += 20; |
2712 | |
|
2713 | 0 | if (mz_zip_invalid_date(&fixed_tm)) |
2714 | 0 | return 0; |
2715 | | |
2716 | 0 | return (((uint32_t)fixed_tm.tm_mday + (32 * ((uint32_t)fixed_tm.tm_mon + 1)) + (512 * (uint32_t)fixed_tm.tm_year)) << 16) | |
2717 | 0 | (((uint32_t)fixed_tm.tm_sec / 2) + (32 * (uint32_t)fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour)); |
2718 | 0 | } |
2719 | | |
2720 | 67.9k | int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time) { |
2721 | 67.9k | *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000); |
2722 | 67.9k | return MZ_OK; |
2723 | 67.9k | } |
2724 | | |
2725 | 0 | int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time) { |
2726 | 0 | *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL; |
2727 | 0 | return MZ_OK; |
2728 | 0 | } |
2729 | | |
2730 | | /***************************************************************************/ |
2731 | | |
2732 | 49.0k | int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case) { |
2733 | 51.7k | do { |
2734 | 51.7k | if ((*path1 == '\\' && *path2 == '/') || |
2735 | 51.7k | (*path2 == '\\' && *path1 == '/')) { |
2736 | | /* Ignore comparison of path slashes */ |
2737 | 51.7k | } else if (ignore_case) { |
2738 | 25.3k | if (tolower(*path1) != tolower(*path2)) |
2739 | 22.9k | break; |
2740 | 26.3k | } else if (*path1 != *path2) { |
2741 | 25.0k | break; |
2742 | 25.0k | } |
2743 | | |
2744 | 3.76k | path1 += 1; |
2745 | 3.76k | path2 += 1; |
2746 | 3.76k | } while (*path1 != 0 && *path2 != 0); |
2747 | | |
2748 | 49.0k | if (ignore_case) |
2749 | 23.6k | return (int32_t)(tolower(*path1) - tolower(*path2)); |
2750 | | |
2751 | 25.3k | return (int32_t)(*path1 - *path2); |
2752 | 49.0k | } |
2753 | | |
2754 | | /***************************************************************************/ |
2755 | | |
2756 | | const char* mz_zip_get_compression_method_string(int32_t compression_method) |
2757 | 0 | { |
2758 | 0 | const char *method = "?"; |
2759 | 0 | switch (compression_method) { |
2760 | 0 | case MZ_COMPRESS_METHOD_STORE: |
2761 | 0 | method = "stored"; |
2762 | 0 | break; |
2763 | 0 | case MZ_COMPRESS_METHOD_DEFLATE: |
2764 | 0 | method = "deflate"; |
2765 | 0 | break; |
2766 | 0 | case MZ_COMPRESS_METHOD_BZIP2: |
2767 | 0 | method = "bzip2"; |
2768 | 0 | break; |
2769 | 0 | case MZ_COMPRESS_METHOD_LZMA: |
2770 | 0 | method = "lzma"; |
2771 | 0 | break; |
2772 | 0 | case MZ_COMPRESS_METHOD_XZ: |
2773 | 0 | method = "xz"; |
2774 | 0 | break; |
2775 | 0 | case MZ_COMPRESS_METHOD_ZSTD: |
2776 | 0 | method = "zstd"; |
2777 | 0 | break; |
2778 | 0 | } |
2779 | 0 | return method; |
2780 | 0 | } |
2781 | | |
2782 | | /***************************************************************************/ |