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