/src/libzip/lib/zip_open.c
Line | Count | Source |
1 | | /* |
2 | | zip_open.c -- open zip archive by name |
3 | | Copyright (C) 1999-2024 Dieter Baron and Thomas Klausner |
4 | | |
5 | | This file is part of libzip, a library to manipulate ZIP archives. |
6 | | The authors can be contacted at <info@libzip.org> |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | 1. Redistributions of source code must retain the above copyright |
12 | | notice, this list of conditions and the following disclaimer. |
13 | | 2. Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in |
15 | | the documentation and/or other materials provided with the |
16 | | distribution. |
17 | | 3. The names of the authors may not be used to endorse or promote |
18 | | products derived from this software without specific prior |
19 | | written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
22 | | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
25 | | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
27 | | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
29 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
30 | | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
31 | | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | #include <limits.h> |
35 | | #include <stdio.h> |
36 | | #include <stdlib.h> |
37 | | #include <string.h> |
38 | | |
39 | | #include "zipint.h" |
40 | | |
41 | | typedef enum { |
42 | | EXISTS_ERROR = -1, |
43 | | EXISTS_NOT = 0, |
44 | | EXISTS_OK |
45 | | } exists_t; |
46 | | |
47 | | typedef enum { |
48 | | CDIR_OK, |
49 | | CDIR_INVALID, |
50 | | CDIR_NOT_FOUND |
51 | | } cdir_status_t; |
52 | | |
53 | | static bool check_eocd(zip_cdir_t *cd, unsigned int flags, zip_error_t *error); |
54 | | static bool check_magic(zip_uint64_t offset, zip_buffer_t *buffer, zip_uint64_t buffer_offset, zip_source_t *src, const char *magic); |
55 | | static zip_t *_zip_allocate_new(zip_source_t *src, unsigned int flags, zip_error_t *error); |
56 | | static zip_int64_t _zip_checkcons(zip_t *za, zip_cdir_t *cdir, zip_error_t *error); |
57 | | static void zip_check_torrentzip(zip_t *za, const zip_cdir_t *cdir); |
58 | | static zip_cdir_t *_zip_find_central_dir(zip_t *za, zip_uint64_t len); |
59 | | static exists_t _zip_file_exists(zip_source_t *src, zip_error_t *error); |
60 | | static int _zip_headercomp(const zip_dirent_t *, const zip_dirent_t *); |
61 | | static bool _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_cdir_t **cdirp, zip_error_t *error); |
62 | | static zip_cdir_t *_zip_read_eocd(zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_error_t *error); |
63 | | static cdir_status_t _zip_read_eocd64(zip_cdir_t *cdir, zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags, zip_error_t *error); |
64 | | static const unsigned char *find_eocd(zip_buffer_t *buffer, const unsigned char *last); |
65 | | |
66 | | |
67 | 0 | ZIP_EXTERN zip_t *zip_open(const char *fn, int _flags, int *zep) { |
68 | 0 | zip_t *za; |
69 | 0 | zip_source_t *src; |
70 | 0 | struct zip_error error; |
71 | |
|
72 | 0 | zip_error_init(&error); |
73 | 0 | if ((src = zip_source_file_create(fn, 0, -1, &error)) == NULL) { |
74 | 0 | _zip_set_open_error(zep, &error, 0); |
75 | 0 | zip_error_fini(&error); |
76 | 0 | return NULL; |
77 | 0 | } |
78 | | |
79 | 0 | if ((za = zip_open_from_source(src, _flags, &error)) == NULL) { |
80 | 0 | zip_source_free(src); |
81 | 0 | _zip_set_open_error(zep, &error, 0); |
82 | 0 | zip_error_fini(&error); |
83 | 0 | return NULL; |
84 | 0 | } |
85 | | |
86 | 0 | zip_error_fini(&error); |
87 | 0 | return za; |
88 | 0 | } |
89 | | |
90 | | |
91 | 5.00k | ZIP_EXTERN zip_t *zip_open_from_source(zip_source_t *src, int _flags, zip_error_t *error) { |
92 | 5.00k | unsigned int flags; |
93 | 5.00k | zip_int64_t supported; |
94 | 5.00k | exists_t exists; |
95 | | |
96 | 5.00k | if (_flags < 0 || src == NULL) { |
97 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
98 | 0 | return NULL; |
99 | 0 | } |
100 | 5.00k | flags = (unsigned int)_flags; |
101 | | |
102 | 5.00k | supported = zip_source_supports(src); |
103 | 5.00k | if ((supported & ZIP_SOURCE_SUPPORTS_SEEKABLE) != ZIP_SOURCE_SUPPORTS_SEEKABLE) { |
104 | 0 | zip_error_set(error, ZIP_ER_OPNOTSUPP, 0); |
105 | 0 | return NULL; |
106 | 0 | } |
107 | 5.00k | if ((supported & ZIP_SOURCE_SUPPORTS_WRITABLE) != ZIP_SOURCE_SUPPORTS_WRITABLE) { |
108 | 0 | flags |= ZIP_RDONLY; |
109 | 0 | } |
110 | | |
111 | 5.00k | if ((flags & (ZIP_RDONLY | ZIP_TRUNCATE)) == (ZIP_RDONLY | ZIP_TRUNCATE)) { |
112 | 0 | zip_error_set(error, ZIP_ER_RDONLY, 0); |
113 | 0 | return NULL; |
114 | 0 | } |
115 | | |
116 | 5.00k | exists = _zip_file_exists(src, error); |
117 | 5.00k | switch (exists) { |
118 | 0 | case EXISTS_ERROR: |
119 | 0 | return NULL; |
120 | | |
121 | 0 | case EXISTS_NOT: |
122 | 0 | if ((flags & ZIP_CREATE) == 0) { |
123 | 0 | zip_error_set(error, ZIP_ER_NOENT, 0); |
124 | 0 | return NULL; |
125 | 0 | } |
126 | 0 | return _zip_allocate_new(src, flags, error); |
127 | | |
128 | 5.00k | default: { |
129 | 5.00k | zip_t *za; |
130 | 5.00k | if (flags & ZIP_EXCL) { |
131 | 0 | zip_error_set(error, ZIP_ER_EXISTS, 0); |
132 | 0 | return NULL; |
133 | 0 | } |
134 | 5.00k | if (zip_source_open(src) < 0) { |
135 | 0 | zip_error_set_from_source(error, src); |
136 | 0 | return NULL; |
137 | 0 | } |
138 | | |
139 | 5.00k | if (flags & ZIP_TRUNCATE) { |
140 | 0 | za = _zip_allocate_new(src, flags, error); |
141 | 0 | } |
142 | 5.00k | else { |
143 | | /* ZIP_CREATE gets ignored if file exists and not ZIP_EXCL, just like open() */ |
144 | 5.00k | za = _zip_open(src, flags, error); |
145 | 5.00k | } |
146 | | |
147 | 5.00k | if (za == NULL) { |
148 | 2.72k | zip_source_close(src); |
149 | 2.72k | return NULL; |
150 | 2.72k | } |
151 | 2.28k | return za; |
152 | 5.00k | } |
153 | 5.00k | } |
154 | 5.00k | } |
155 | | |
156 | | |
157 | 634 | static bool _is_truncated_zip(zip_source_t *src) { |
158 | 634 | unsigned char data[4]; |
159 | | /* check if the source is a truncated zip archive: true if yes, no |
160 | | if not or can't be determined */ |
161 | 634 | if (zip_source_seek(src, 0, SEEK_SET) < 0) { |
162 | 0 | return false; |
163 | 0 | } |
164 | | |
165 | 634 | if (zip_source_read(src, data, 4) != 4) { |
166 | 3 | return false; |
167 | 3 | } |
168 | | |
169 | 631 | if (memcmp(data, LOCAL_MAGIC, 4) == 0) { |
170 | | /* file starts with a ZIP local header signature */ |
171 | 28 | return true; |
172 | 28 | } |
173 | 603 | return false; |
174 | 631 | } |
175 | | |
176 | | |
177 | 5.00k | zip_t *_zip_open(zip_source_t *src, unsigned int flags, zip_error_t *error) { |
178 | 5.00k | zip_t *za; |
179 | 5.00k | zip_cdir_t *cdir; |
180 | 5.00k | struct zip_stat st; |
181 | 5.00k | zip_uint64_t len, idx; |
182 | | |
183 | 5.00k | zip_stat_init(&st); |
184 | 5.00k | if (zip_source_stat(src, &st) < 0) { |
185 | 0 | zip_error_set_from_source(error, src); |
186 | 0 | return NULL; |
187 | 0 | } |
188 | 5.00k | if ((st.valid & ZIP_STAT_SIZE) == 0) { |
189 | 0 | zip_error_set(error, ZIP_ER_SEEK, EOPNOTSUPP); |
190 | 0 | return NULL; |
191 | 0 | } |
192 | 5.00k | len = st.size; |
193 | | |
194 | | |
195 | 5.00k | if ((za = _zip_allocate_new(src, flags, error)) == NULL) { |
196 | 0 | return NULL; |
197 | 0 | } |
198 | | |
199 | | /* treat empty files as empty archives */ |
200 | 5.00k | if (len == 0 && zip_source_accept_empty(src)) { |
201 | 0 | return za; |
202 | 0 | } |
203 | | |
204 | 5.00k | if ((cdir = _zip_find_central_dir(za, len)) == NULL) { |
205 | 2.72k | _zip_error_copy(error, &za->error); |
206 | 2.72k | if (zip_error_code_zip(&za->error) == ZIP_ER_NOZIP) { |
207 | | /* not a zip - find out if it's truncated */ |
208 | 634 | if (_is_truncated_zip(src)) { |
209 | 28 | zip_error_set(error, ZIP_ER_TRUNCATED_ZIP, 0); |
210 | 28 | } |
211 | 634 | } |
212 | | /* keep src so discard does not get rid of it */ |
213 | 2.72k | zip_source_keep(src); |
214 | 2.72k | zip_discard(za); |
215 | 2.72k | return NULL; |
216 | 2.72k | } |
217 | | |
218 | 2.28k | za->entry = cdir->entry; |
219 | 2.28k | za->nentry = cdir->nentry; |
220 | 2.28k | za->nentry_alloc = cdir->nentry_alloc; |
221 | | |
222 | 2.28k | zip_check_torrentzip(za, cdir); |
223 | | |
224 | 2.28k | if (ZIP_IS_TORRENTZIP(za)) { |
225 | | /* Torrentzip uses the archive comment to detect changes by tools that are not torrentzip aware. */ |
226 | 2 | _zip_string_free(cdir->comment); |
227 | 2 | } |
228 | 2.27k | else { |
229 | 2.27k | za->comment_orig = cdir->comment; |
230 | 2.27k | } |
231 | | |
232 | 2.28k | free(cdir); |
233 | | |
234 | 2.28k | _zip_hash_reserve_capacity(za->names, za->nentry, &za->error); |
235 | | |
236 | 36.0k | for (idx = 0; idx < za->nentry; idx++) { |
237 | 33.7k | const zip_uint8_t *name = _zip_string_get(za->entry[idx].orig->filename, NULL, 0, error); |
238 | 33.7k | if (name == NULL) { |
239 | | /* keep src so discard does not get rid of it */ |
240 | 0 | zip_source_keep(src); |
241 | 0 | zip_discard(za); |
242 | 0 | return NULL; |
243 | 0 | } |
244 | | |
245 | 33.7k | if (_zip_hash_add(za->names, name, idx, ZIP_FL_UNCHANGED, &za->error) == false) { |
246 | 23.7k | if (za->error.zip_err != ZIP_ER_EXISTS || (flags & ZIP_CHECKCONS)) { |
247 | 0 | _zip_error_copy(error, &za->error); |
248 | | /* keep src so discard does not get rid of it */ |
249 | 0 | zip_source_keep(src); |
250 | 0 | zip_discard(za); |
251 | 0 | return NULL; |
252 | 0 | } |
253 | 23.7k | } |
254 | 33.7k | } |
255 | | |
256 | 2.28k | za->ch_flags = za->flags; |
257 | | |
258 | 2.28k | return za; |
259 | 2.28k | } |
260 | | |
261 | | |
262 | 0 | void _zip_set_open_error(int *zep, const zip_error_t *err, int ze) { |
263 | 0 | if (err) { |
264 | 0 | ze = zip_error_code_zip(err); |
265 | 0 | switch (zip_error_system_type(err)) { |
266 | 0 | case ZIP_ET_SYS: |
267 | 0 | case ZIP_ET_LIBZIP: |
268 | 0 | errno = zip_error_code_system(err); |
269 | 0 | break; |
270 | | |
271 | 0 | default: |
272 | 0 | break; |
273 | 0 | } |
274 | 0 | } |
275 | | |
276 | 0 | if (zep) { |
277 | 0 | *zep = ze; |
278 | 0 | } |
279 | 0 | } |
280 | | |
281 | | |
282 | | /* _zip_readcdir: |
283 | | tries to find a valid end-of-central-directory at the beginning of |
284 | | buf, and then the corresponding central directory entries. |
285 | | Returns a struct zip_cdir which contains the central directory |
286 | | entries, or NULL if unsuccessful. */ |
287 | | |
288 | 67.4k | static bool _zip_read_cdir(zip_t *za, zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_cdir_t **cdirp, zip_error_t *error) { |
289 | 67.4k | zip_cdir_t *cd; |
290 | 67.4k | zip_uint16_t comment_len; |
291 | 67.4k | zip_uint64_t i, left; |
292 | 67.4k | zip_uint64_t eocd_offset = _zip_buffer_offset(buffer); |
293 | 67.4k | zip_buffer_t *cd_buffer; |
294 | 67.4k | bool eocd64_found = false; |
295 | | |
296 | 67.4k | *cdirp = NULL; |
297 | | |
298 | 67.4k | if ((cd = _zip_read_eocd(buffer, buf_offset, error)) == NULL) { |
299 | 315 | return false; |
300 | 315 | } |
301 | | |
302 | 67.1k | if (eocd_offset >= EOCD64LOCLEN && memcmp(_zip_buffer_data(buffer) + eocd_offset - EOCD64LOCLEN, EOCD64LOC_MAGIC, 4) == 0) { |
303 | 9.83k | eocd64_found = true; |
304 | 9.83k | _zip_buffer_set_offset(buffer, eocd_offset - EOCD64LOCLEN); |
305 | 9.83k | switch (_zip_read_eocd64(cd, za->src, buffer, buf_offset, za->open_flags, error)) { |
306 | 188 | case CDIR_OK: |
307 | 188 | break; |
308 | | |
309 | 773 | case CDIR_INVALID: |
310 | 773 | _zip_cdir_free(cd); |
311 | 773 | return true; |
312 | | |
313 | 8.87k | case CDIR_NOT_FOUND: |
314 | 8.87k | _zip_cdir_free(cd); |
315 | 8.87k | return false; |
316 | 9.83k | } |
317 | 9.83k | } |
318 | | |
319 | 57.4k | if ((cd->eocd_disk != 0 || cd->this_disk != 0) && !eocd64_found && cd->eocd_disk != cd->this_disk) { |
320 | | /* If the central directory doesn't start on this disk, we can't check that offset is valid. Check as much as we can instead. */ |
321 | 45.1k | if (cd->this_disk < cd->eocd_disk) { |
322 | | /* Disks before the start of the central directory don't contain an EOCD. */ |
323 | 3.68k | _zip_cdir_free(cd); |
324 | 3.68k | return false; |
325 | 3.68k | } |
326 | 41.4k | if (cd->size <= cd->eocd_offset) { |
327 | | /* The complete central directory would fit on this disk. */ |
328 | 3.02k | _zip_cdir_free(cd); |
329 | 3.02k | return false; |
330 | 3.02k | } |
331 | 41.4k | } |
332 | | |
333 | 50.7k | if (!eocd64_found) { |
334 | 50.5k | if (cd->this_disk == 0 && cd->eocd_disk == 0 && cd->eocd_offset == 0 && cd->offset == 0 && cd->num_entries == 0) { |
335 | | /* An empty archive doesn't contain central directory entries. */ |
336 | 359 | } |
337 | 50.1k | else if (!check_magic(cd->offset, buffer, buf_offset, za->src, CENTRAL_MAGIC)) { |
338 | 47.0k | _zip_cdir_free(cd); |
339 | 47.0k | return false; |
340 | 47.0k | } |
341 | 50.5k | } |
342 | | |
343 | | /* We accept this EOCD as valid and won't search for an earlier one if it is unusable. */ |
344 | | |
345 | 3.67k | if (!check_eocd(cd, za->open_flags, error)) { |
346 | 119 | _zip_cdir_free(cd); |
347 | 119 | return true; |
348 | 119 | } |
349 | | |
350 | 3.55k | _zip_buffer_set_offset(buffer, eocd_offset + 20); |
351 | 3.55k | comment_len = _zip_buffer_get_16(buffer); |
352 | | |
353 | 3.55k | if (cd->offset + cd->size > buf_offset + eocd_offset) { |
354 | | /* cdir spans past EOCD record */ |
355 | 133 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_CDIR_OVERLAPS_EOCD); |
356 | 133 | _zip_cdir_free(cd); |
357 | 133 | return true; |
358 | 133 | } |
359 | | |
360 | 3.41k | if (comment_len || (za->open_flags & ZIP_CHECKCONS)) { |
361 | 2.86k | zip_uint64_t tail_len; |
362 | | |
363 | 2.86k | _zip_buffer_set_offset(buffer, eocd_offset + EOCDLEN); |
364 | 2.86k | tail_len = _zip_buffer_left(buffer); |
365 | | |
366 | 2.86k | if (tail_len != comment_len) { |
367 | 2.71k | if (za->open_flags & ZIP_CHECKCONS) { |
368 | 0 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_COMMENT_LENGTH_INVALID); |
369 | 0 | _zip_cdir_free(cd); |
370 | 0 | return true; |
371 | 0 | } |
372 | 2.71k | if (tail_len < comment_len) { |
373 | 2.62k | comment_len = (zip_uint16_t)tail_len; |
374 | 2.62k | } |
375 | 2.71k | } |
376 | | |
377 | 2.86k | if (comment_len) { |
378 | 678 | if ((cd->comment = _zip_string_new(_zip_buffer_get(buffer, comment_len), comment_len, ZIP_FL_ENC_GUESS, error)) == NULL) { |
379 | 0 | _zip_cdir_free(cd); |
380 | 0 | return true; |
381 | 0 | } |
382 | 678 | } |
383 | 2.86k | } |
384 | | |
385 | 3.41k | if (cd->offset >= buf_offset) { |
386 | 3.27k | zip_uint8_t *data; |
387 | | /* if buffer already read in, use it */ |
388 | 3.27k | _zip_buffer_set_offset(buffer, cd->offset - buf_offset); |
389 | | |
390 | 3.27k | if ((data = _zip_buffer_get(buffer, cd->size)) == NULL) { |
391 | 0 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_CDIR_LENGTH_INVALID); |
392 | 0 | _zip_cdir_free(cd); |
393 | 0 | return true; |
394 | 0 | } |
395 | 3.27k | if ((cd_buffer = _zip_buffer_new(data, cd->size)) == NULL) { |
396 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
397 | 0 | _zip_cdir_free(cd); |
398 | 0 | return true; |
399 | 0 | } |
400 | 3.27k | } |
401 | 144 | else { |
402 | 144 | cd_buffer = NULL; |
403 | | |
404 | 144 | if (zip_source_seek(za->src, (zip_int64_t)cd->offset, SEEK_SET) < 0) { |
405 | 0 | zip_error_set_from_source(error, za->src); |
406 | 0 | _zip_cdir_free(cd); |
407 | 0 | return true; |
408 | 0 | } |
409 | | |
410 | | /* possible consistency check: cd->offset = len-(cd->size+cd->comment_len+EOCDLEN) ? */ |
411 | 144 | if (zip_source_tell(za->src) != (zip_int64_t)cd->offset) { |
412 | 0 | zip_error_set(error, ZIP_ER_NOZIP, 0); |
413 | 0 | _zip_cdir_free(cd); |
414 | 0 | return true; |
415 | 0 | } |
416 | 144 | } |
417 | | |
418 | 3.41k | if (!_zip_cdir_grow(cd, cd->num_entries, error)) { |
419 | 0 | _zip_cdir_free(cd); |
420 | 0 | _zip_buffer_free(cd_buffer); |
421 | 0 | return true; |
422 | 0 | } |
423 | 3.41k | left = (zip_uint64_t)cd->size; |
424 | 3.41k | i = 0; |
425 | 48.6k | while (left > 0) { |
426 | 46.2k | bool grown = false; |
427 | 46.2k | zip_int64_t entry_size; |
428 | | |
429 | 46.2k | if (i == cd->nentry) { |
430 | | /* InfoZIP has a hack to avoid using Zip64: it stores nentries % 0x10000 */ |
431 | | /* This hack isn't applicable if we're using Zip64, or if there is no central directory entry following. */ |
432 | | |
433 | 196 | if (cd->is_zip64 || left < CDENTRYSIZE) { |
434 | 44 | break; |
435 | 44 | } |
436 | | |
437 | 152 | if (!_zip_cdir_grow(cd, 0x10000, error)) { |
438 | 0 | _zip_cdir_free(cd); |
439 | 0 | _zip_buffer_free(cd_buffer); |
440 | 0 | return true; |
441 | 0 | } |
442 | 152 | grown = true; |
443 | 152 | } |
444 | | |
445 | 46.2k | if (left < CDENTRYSIZE) { |
446 | 109 | if (grown) { |
447 | 0 | zip_error_set(error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_CDIR_ENTRY_INVALID, i)); |
448 | 0 | } |
449 | 109 | else { |
450 | 109 | zip_error_set(error, ZIP_ER_NOZIP, 0); |
451 | 109 | } |
452 | 109 | _zip_cdir_free(cd); |
453 | 109 | _zip_buffer_free(cd_buffer); |
454 | 109 | return true; |
455 | 109 | } |
456 | | |
457 | 46.1k | if ((cd->entry[i].orig = _zip_dirent_new()) == NULL || (entry_size = _zip_dirent_read(cd->entry[i].orig, za->src, cd_buffer, false, cd->is_zip64, 0, za->open_flags & ZIP_CHECKCONS, error)) < 0) { |
458 | 843 | if (zip_error_code_zip(error) == ZIP_ER_INCONS) { |
459 | 642 | zip_error_set(error, ZIP_ER_INCONS, ADD_INDEX_TO_DETAIL(zip_error_code_system(error), i)); |
460 | 642 | } |
461 | 201 | else if (grown && zip_error_code_zip(error) == ZIP_ER_NOZIP) { |
462 | 25 | zip_error_set(error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_CDIR_ENTRY_INVALID, i)); |
463 | 25 | } |
464 | 843 | _zip_cdir_free(cd); |
465 | 843 | _zip_buffer_free(cd_buffer); |
466 | 843 | return true; |
467 | 843 | } |
468 | 45.2k | i++; |
469 | 45.2k | if (!ASSIGN_UNSIGNED_SUBTRACT_FITS(left, left, (zip_uint64_t)entry_size)) { |
470 | 27 | zip_error_set(error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_VARIABLE_SIZE_OVERFLOW, i)); |
471 | 27 | _zip_cdir_free(cd); |
472 | 27 | _zip_buffer_free(cd_buffer); |
473 | 27 | return true; |
474 | 27 | } |
475 | 45.2k | } |
476 | | |
477 | | /* If we didn't fill all we grew, cd->num_entries was wrong. */ |
478 | 2.44k | if (i != cd->nentry || left > 0) { |
479 | 160 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_CDIR_WRONG_ENTRIES_COUNT); |
480 | 160 | _zip_buffer_free(cd_buffer); |
481 | 160 | _zip_cdir_free(cd); |
482 | 160 | return true; |
483 | 160 | } |
484 | | |
485 | 2.28k | if (za->open_flags & ZIP_CHECKCONS) { |
486 | 0 | bool ok; |
487 | |
|
488 | 0 | if (cd_buffer) { |
489 | 0 | ok = _zip_buffer_eof(cd_buffer); |
490 | 0 | } |
491 | 0 | else { |
492 | 0 | zip_int64_t offset = zip_source_tell(za->src); |
493 | |
|
494 | 0 | if (offset < 0) { |
495 | 0 | zip_error_set_from_source(error, za->src); |
496 | 0 | _zip_cdir_free(cd); |
497 | 0 | return true; |
498 | 0 | } |
499 | 0 | ok = ((zip_uint64_t)offset == cd->offset + cd->size); |
500 | 0 | } |
501 | | |
502 | 0 | if (!ok) { |
503 | 0 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_CDIR_LENGTH_INVALID); |
504 | 0 | _zip_buffer_free(cd_buffer); |
505 | 0 | _zip_cdir_free(cd); |
506 | 0 | return true; |
507 | 0 | } |
508 | 0 | } |
509 | | |
510 | 2.28k | _zip_buffer_free(cd_buffer); |
511 | 2.28k | *cdirp = cd; |
512 | 2.28k | return true; |
513 | 2.28k | } |
514 | | |
515 | | |
516 | 60.0k | static bool check_magic(zip_uint64_t offset, zip_buffer_t *buffer, zip_uint64_t buffer_offset, zip_source_t *src, const char *magic) { |
517 | 60.0k | if (buffer_offset <= offset) { |
518 | 57.8k | zip_uint8_t *data; |
519 | 57.8k | if (_zip_buffer_set_offset(buffer, offset - buffer_offset) < 0 || (data = _zip_buffer_get(buffer, MAGIC_LEN)) == NULL) { |
520 | 47.6k | return false; |
521 | 47.6k | } |
522 | 10.1k | return memcmp(data, magic, MAGIC_LEN) == 0; |
523 | 57.8k | } |
524 | 2.19k | else { |
525 | 2.19k | zip_uint8_t data[MAGIC_LEN]; |
526 | | |
527 | 2.19k | if (zip_source_seek(src, offset, SEEK_SET) < 0 || zip_source_read(src, data, MAGIC_LEN) != MAGIC_LEN) { |
528 | 0 | return false; |
529 | 0 | } |
530 | 2.19k | return memcmp(data, magic, MAGIC_LEN) == 0; |
531 | 2.19k | } |
532 | 60.0k | } |
533 | | |
534 | | |
535 | | typedef struct zip_entry_span { |
536 | | zip_uint64_t start; /* offset of the local header */ |
537 | | zip_uint64_t end; /* offset just past the end of the entry's data, including its data descriptor if it uses one */ |
538 | | zip_uint64_t index; /* index of the entry in the central directory */ |
539 | | } zip_entry_span_t; |
540 | | |
541 | | |
542 | 0 | static int _zip_entry_span_compare(const void *a, const void *b) { |
543 | 0 | const zip_entry_span_t *span_a = (const zip_entry_span_t *)a; |
544 | 0 | const zip_entry_span_t *span_b = (const zip_entry_span_t *)b; |
545 | |
|
546 | 0 | if (span_a->start != span_b->start) { |
547 | 0 | return span_a->start < span_b->start ? -1 : 1; |
548 | 0 | } |
549 | 0 | if (span_a->end != span_b->end) { |
550 | 0 | return span_a->end > span_b->end ? -1 : 1; |
551 | 0 | } |
552 | 0 | return 0; |
553 | 0 | } |
554 | | |
555 | | |
556 | | /* _zip_checkcons: |
557 | | Checks the consistency of the central directory by comparing central |
558 | | directory entries with local headers, checking for plausible |
559 | | file and header offsets, and checking that the local headers and |
560 | | data of the entries cover the archive from the first local header |
561 | | up to the central directory without overlapping or leaving gaps. |
562 | | Returns -1 if not plausible, else the difference between the lowest |
563 | | and the highest fileposition reached */ |
564 | | |
565 | 0 | static zip_int64_t _zip_checkcons(zip_t *za, zip_cdir_t *cd, zip_error_t *error) { |
566 | 0 | zip_uint64_t i; |
567 | 0 | zip_uint64_t min, max, j; |
568 | 0 | zip_entry_span_t *spans = NULL; |
569 | 0 | zip_int64_t local_metadata_size; |
570 | 0 | struct zip_dirent temp; |
571 | 0 | int detail; |
572 | |
|
573 | 0 | _zip_dirent_init(&temp); |
574 | 0 | if (cd->nentry) { |
575 | 0 | max = cd->entry[0].orig->offset; |
576 | 0 | min = cd->entry[0].orig->offset; |
577 | |
|
578 | 0 | if ((spans = (zip_entry_span_t *)malloc(cd->nentry * sizeof(*spans))) == NULL) { |
579 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
580 | 0 | return -1; |
581 | 0 | } |
582 | 0 | } |
583 | 0 | else { |
584 | 0 | min = max = 0; |
585 | 0 | } |
586 | | |
587 | | /* |
588 | | Currently we use the presence of a Zip64 End of Central Directory do decide |
589 | | if the archive is Zip64 and use that to decide the size of data descriptors |
590 | | in local headers. This is not guaranteed to be correct. We could also assume |
591 | | Zip64 format if any of the central directory entries has a Zip64 extra field. |
592 | | |
593 | | Since we only read local headers when checking consistency, this is not a |
594 | | problem for normal use, but only when checking consistency for archives using |
595 | | data descriptors. |
596 | | */ |
597 | | |
598 | 0 | for (i = 0; i < cd->nentry; i++) { |
599 | 0 | if (cd->entry[i].orig->offset < min) { |
600 | 0 | min = cd->entry[i].orig->offset; |
601 | 0 | } |
602 | 0 | if (min > (zip_uint64_t)cd->offset) { |
603 | 0 | zip_error_set(error, ZIP_ER_NOZIP, 0); |
604 | 0 | free(spans); |
605 | 0 | return -1; |
606 | 0 | } |
607 | | |
608 | 0 | if (zip_source_seek(za->src, (zip_int64_t)cd->entry[i].orig->offset, SEEK_SET) < 0) { |
609 | 0 | zip_error_set_from_source(error, za->src); |
610 | 0 | free(spans); |
611 | 0 | return -1; |
612 | 0 | } |
613 | | |
614 | 0 | if ((local_metadata_size = _zip_dirent_read(&temp, za->src, NULL, true, cd->is_zip64, cd->entry[i].orig->comp_size, true, error)) == -1) { |
615 | 0 | if (zip_error_code_zip(error) == ZIP_ER_INCONS) { |
616 | 0 | zip_error_set(error, ZIP_ER_INCONS, ADD_INDEX_TO_DETAIL(zip_error_code_system(error), i)); |
617 | 0 | } |
618 | 0 | _zip_dirent_finalize(&temp); |
619 | 0 | free(spans); |
620 | 0 | return -1; |
621 | 0 | } |
622 | | |
623 | 0 | if (_zip_headercomp(cd->entry[i].orig, &temp) != 0) { |
624 | 0 | zip_error_set(error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_ENTRY_HEADER_MISMATCH, i)); |
625 | 0 | _zip_dirent_finalize(&temp); |
626 | 0 | free(spans); |
627 | 0 | return -1; |
628 | 0 | } |
629 | | |
630 | 0 | if (!ASSIGN_UNSIGNED_ADD_THREE_FITS(j, cd->entry[i].orig->comp_size, local_metadata_size, cd->entry[i].orig->offset)) { |
631 | 0 | zip_error_set(error, ZIP_ER_NOZIP, 0); |
632 | 0 | _zip_dirent_finalize(&temp); |
633 | 0 | free(spans); |
634 | 0 | return -1; |
635 | 0 | } |
636 | | |
637 | 0 | if (j > max) { |
638 | 0 | max = j; |
639 | 0 | } |
640 | |
|
641 | 0 | spans[i].start = cd->entry[i].orig->offset; |
642 | 0 | spans[i].end = j; |
643 | 0 | spans[i].index = i; |
644 | |
|
645 | 0 | cd->entry[i].orig->extra_fields.local = temp.extra_fields.local; |
646 | 0 | cd->entry[i].orig->local_extra_fields_read = 1; |
647 | 0 | temp.extra_fields.local = NULL; |
648 | |
|
649 | 0 | _zip_dirent_finalize(&temp); |
650 | |
|
651 | 0 | if ((detail = zip_dirent_check_consistency(cd->entry[i].orig)) != 0) { |
652 | 0 | zip_error_set(error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(detail, i)); |
653 | 0 | free(spans); |
654 | 0 | return -1; |
655 | 0 | } |
656 | 0 | } |
657 | | |
658 | 0 | if (cd->nentry > 0) { |
659 | 0 | zip_uint64_t k; |
660 | |
|
661 | 0 | qsort(spans, (size_t)cd->nentry, sizeof(*spans), _zip_entry_span_compare); |
662 | |
|
663 | 0 | for (k = 1; k < cd->nentry; k++) { |
664 | 0 | if (spans[k].start < spans[k - 1].end) { |
665 | 0 | zip_error_set(error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_ENTRY_OVERLAPS_ENTRY, spans[k].index)); |
666 | 0 | free(spans); |
667 | 0 | return -1; |
668 | 0 | } |
669 | 0 | if (spans[k].start > spans[k - 1].end) { |
670 | 0 | zip_error_set(error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_ENTRY_GAP, spans[k].index)); |
671 | 0 | free(spans); |
672 | 0 | return -1; |
673 | 0 | } |
674 | 0 | } |
675 | | |
676 | | /* We allow data before the first entry, which is used for self extracting archives. */ |
677 | | |
678 | 0 | if (spans[cd->nentry - 1].end < (zip_uint64_t)cd->offset) { |
679 | 0 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_CDIR_GAP); |
680 | 0 | free(spans); |
681 | 0 | return -1; |
682 | 0 | } |
683 | 0 | if (spans[cd->nentry - 1].end > (zip_uint64_t)cd->offset) { |
684 | 0 | zip_error_set(error, ZIP_ER_INCONS, MAKE_DETAIL_WITH_INDEX(ZIP_ER_DETAIL_ENTRY_OVERLAPS_CDIR, spans[cd->nentry - 1].index)); |
685 | 0 | free(spans); |
686 | 0 | return -1; |
687 | 0 | } |
688 | | |
689 | 0 | free(spans); |
690 | 0 | } |
691 | | |
692 | 0 | return (max - min) < ZIP_INT64_MAX ? (zip_int64_t)(max - min) : ZIP_INT64_MAX; |
693 | 0 | } |
694 | | |
695 | | |
696 | | /* _zip_headercomp: |
697 | | compares a central directory entry and a local file header |
698 | | Return 0 if they are consistent, -1 if not. */ |
699 | | |
700 | 0 | static int _zip_headercomp(const zip_dirent_t *central, const zip_dirent_t *local) { |
701 | 0 | if ((central->version_needed < local->version_needed) |
702 | | #if 0 |
703 | | /* some zip-files have different values in local |
704 | | and global headers for the bitflags */ |
705 | | || (central->bitflags != local->bitflags) |
706 | | #endif |
707 | 0 | || (central->comp_method != local->comp_method) || (central->last_mod.time != local->last_mod.time) || (central->last_mod.date != local->last_mod.date) || !_zip_string_equal(central->filename, local->filename)) |
708 | 0 | return -1; |
709 | | |
710 | 0 | if ((central->crc != local->crc) || (central->comp_size != local->comp_size) || (central->uncomp_size != local->uncomp_size)) { |
711 | | /* InfoZip stores valid values in local header even when data descriptor is used. |
712 | | This is in violation of the appnote. |
713 | | macOS Archive sets the compressed size even when data descriptor is used ( but not the others), |
714 | | also in violation of the appnote. |
715 | | */ |
716 | | /* if data descriptor is not used, the values must match */ |
717 | 0 | if ((local->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0) { |
718 | 0 | return -1; |
719 | 0 | } |
720 | | /* when using a data descriptor, the local header value must be zero or match */ |
721 | 0 | if ((local->crc != 0 && central->crc != local->crc) || (local->comp_size != 0 && central->comp_size != local->comp_size) || (local->uncomp_size != 0 && central->uncomp_size != local->uncomp_size)) { |
722 | 0 | return -1; |
723 | 0 | } |
724 | 0 | } |
725 | | |
726 | 0 | return 0; |
727 | 0 | } |
728 | | |
729 | | |
730 | 5.00k | static zip_t *_zip_allocate_new(zip_source_t *src, unsigned int flags, zip_error_t *error) { |
731 | 5.00k | zip_t *za; |
732 | | |
733 | 5.00k | if ((za = _zip_new(error)) == NULL) { |
734 | 0 | return NULL; |
735 | 0 | } |
736 | | |
737 | 5.00k | za->src = src; |
738 | 5.00k | za->open_flags = flags; |
739 | 5.00k | za->flags = 0; |
740 | 5.00k | za->ch_flags = 0; |
741 | 5.00k | za->write_crc = NULL; |
742 | | |
743 | 5.00k | if (flags & ZIP_RDONLY) { |
744 | 0 | za->flags |= ZIP_AFL_RDONLY; |
745 | 0 | za->ch_flags |= ZIP_AFL_RDONLY; |
746 | 0 | } |
747 | | |
748 | 5.00k | return za; |
749 | 5.00k | } |
750 | | |
751 | | |
752 | | /* |
753 | | * tests for file existence |
754 | | */ |
755 | 5.00k | static exists_t _zip_file_exists(zip_source_t *src, zip_error_t *error) { |
756 | 5.00k | struct zip_stat st; |
757 | | |
758 | 5.00k | zip_stat_init(&st); |
759 | 5.00k | if (zip_source_stat(src, &st) != 0) { |
760 | 0 | zip_error_t *src_error = zip_source_error(src); |
761 | 0 | if (zip_error_code_zip(src_error) == ZIP_ER_READ && zip_error_code_system(src_error) == ENOENT) { |
762 | 0 | return EXISTS_NOT; |
763 | 0 | } |
764 | 0 | _zip_error_copy(error, src_error); |
765 | 0 | return EXISTS_ERROR; |
766 | 0 | } |
767 | | |
768 | 5.00k | return EXISTS_OK; |
769 | 5.00k | } |
770 | | |
771 | | |
772 | 5.00k | static zip_cdir_t *_zip_find_central_dir(zip_t *za, zip_uint64_t len) { |
773 | 5.00k | zip_cdir_t *cdir; |
774 | 5.00k | const zip_uint8_t *match; |
775 | 5.00k | zip_int64_t buf_offset; |
776 | 5.00k | zip_uint64_t buflen; |
777 | 5.00k | zip_error_t error; |
778 | 5.00k | zip_buffer_t *buffer; |
779 | | |
780 | 5.00k | if (len < EOCDLEN) { |
781 | 53 | zip_error_set(&za->error, ZIP_ER_NOZIP, 0); |
782 | 53 | return NULL; |
783 | 53 | } |
784 | | |
785 | 4.95k | buflen = (len < CDBUFSIZE ? len : CDBUFSIZE); |
786 | 4.95k | if (zip_source_seek(za->src, -(zip_int64_t)buflen, SEEK_END) < 0) { |
787 | 0 | zip_error_t *src_error = zip_source_error(za->src); |
788 | 0 | if (zip_error_code_zip(src_error) != ZIP_ER_SEEK || zip_error_code_system(src_error) != EFBIG) { |
789 | | /* seek before start of file on my machine */ |
790 | 0 | _zip_error_copy(&za->error, src_error); |
791 | 0 | return NULL; |
792 | 0 | } |
793 | 0 | } |
794 | 4.95k | if ((buf_offset = zip_source_tell(za->src)) < 0) { |
795 | 0 | zip_error_set_from_source(&za->error, za->src); |
796 | 0 | return NULL; |
797 | 0 | } |
798 | | |
799 | 4.95k | if ((buffer = _zip_buffer_new_from_source(za->src, buflen, NULL, &za->error)) == NULL) { |
800 | 0 | return NULL; |
801 | 0 | } |
802 | | |
803 | 4.95k | cdir = NULL; |
804 | 4.95k | if (buflen >= CDBUFSIZE) { |
805 | | /* EOCD64 locator is before EOCD, so leave place for it */ |
806 | 198 | _zip_buffer_set_offset(buffer, EOCD64LOCLEN); |
807 | 198 | } |
808 | 4.95k | zip_error_set(&error, ZIP_ER_NOZIP, 0); |
809 | | |
810 | 4.95k | match = NULL; |
811 | 67.9k | while ((match = find_eocd(buffer, match)) != NULL) { |
812 | 67.4k | _zip_buffer_set_offset(buffer, (zip_uint64_t)(match - _zip_buffer_data(buffer))); |
813 | 67.4k | if (_zip_read_cdir(za, buffer, (zip_uint64_t)buf_offset, &cdir, &error)) { |
814 | 4.44k | if (cdir != NULL && (za->open_flags & ZIP_CHECKCONS) && _zip_checkcons(za, cdir, &error) < 0) { |
815 | 0 | _zip_cdir_free(cdir); |
816 | 0 | cdir = NULL; |
817 | 0 | } |
818 | 4.44k | break; |
819 | 4.44k | } |
820 | 67.4k | } |
821 | | |
822 | 4.95k | _zip_buffer_free(buffer); |
823 | | |
824 | 4.95k | if (cdir == NULL) { |
825 | 2.67k | _zip_error_copy(&za->error, &error); |
826 | 2.67k | } |
827 | 4.95k | return cdir; |
828 | 4.95k | } |
829 | | |
830 | | |
831 | 67.9k | static const unsigned char *find_eocd(zip_buffer_t *buffer, const unsigned char *last) { |
832 | 67.9k | const unsigned char *data = _zip_buffer_data(buffer); |
833 | 67.9k | const unsigned char *p; |
834 | | |
835 | 67.9k | if (last == NULL) { |
836 | 4.95k | if (_zip_buffer_size(buffer) < MAGIC_LEN) { |
837 | 0 | return NULL; |
838 | 0 | } |
839 | 4.95k | last = data + _zip_buffer_size(buffer) - MAGIC_LEN; |
840 | 4.95k | } |
841 | 62.9k | else { |
842 | 62.9k | if (last == _zip_buffer_data(buffer)) { |
843 | 244 | return NULL; |
844 | 244 | } |
845 | 62.7k | last -= 1; |
846 | 62.7k | } |
847 | | |
848 | 5.66M | for (p = last; p >= data; p -= 1) { |
849 | 5.66M | if (*p == EOCD_MAGIC[0]) { |
850 | 222k | if (memcmp(p, EOCD_MAGIC, MAGIC_LEN) == 0) { |
851 | 67.4k | return p; |
852 | 67.4k | } |
853 | 222k | } |
854 | 5.59M | if (p == data) { |
855 | | /* Avoid undefined behavior by creating pointer outside buffer */ |
856 | 263 | break; |
857 | 263 | } |
858 | 5.59M | } |
859 | | |
860 | 263 | return NULL; |
861 | 67.6k | } |
862 | | |
863 | | |
864 | 67.4k | static zip_cdir_t *_zip_read_eocd(zip_buffer_t *buffer, zip_uint64_t buf_offset, zip_error_t *error) { |
865 | 67.4k | zip_cdir_t *cd; |
866 | | |
867 | 67.4k | if (_zip_buffer_left(buffer) < EOCDLEN) { |
868 | 315 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_EOCD_LENGTH_INVALID); |
869 | 315 | return NULL; |
870 | 315 | } |
871 | | |
872 | 67.1k | if ((cd = _zip_cdir_new(error)) == NULL) { |
873 | 0 | return NULL; |
874 | 0 | } |
875 | | |
876 | 67.1k | cd->eocd_offset = buf_offset + _zip_buffer_offset(buffer); |
877 | | /* This function is only called where EOCD magic was found, so no need to check that here. */ |
878 | 67.1k | _zip_buffer_skip(buffer, MAGIC_LEN); |
879 | 67.1k | cd->is_zip64 = false; |
880 | 67.1k | cd->this_disk = _zip_buffer_get_16(buffer); |
881 | 67.1k | cd->eocd_disk = _zip_buffer_get_16(buffer); |
882 | | |
883 | | /* number of cdir-entries on this disk */ |
884 | 67.1k | cd->disk_entries = _zip_buffer_get_16(buffer); |
885 | | /* number of cdir-entries */ |
886 | 67.1k | cd->num_entries = _zip_buffer_get_16(buffer); |
887 | 67.1k | cd->size = _zip_buffer_get_32(buffer); |
888 | 67.1k | cd->offset = _zip_buffer_get_32(buffer); |
889 | | |
890 | 67.1k | return cd; |
891 | 67.1k | } |
892 | | |
893 | 3.67k | static bool check_eocd(zip_cdir_t *cd, unsigned int flags, zip_error_t *error) { |
894 | 3.67k | if (cd->disk_entries != cd->num_entries || cd->this_disk != 0 || cd->eocd_disk != 0) { |
895 | 119 | zip_error_set(error, ZIP_ER_MULTIDISK, 0); |
896 | 119 | return false; |
897 | 119 | } |
898 | | |
899 | 3.55k | if (!UNSIGNED_ADD_FITS(cd->offset, cd->size)) { |
900 | 0 | zip_error_set(error, ZIP_ER_SEEK, EFBIG); |
901 | 0 | return false; |
902 | 0 | } |
903 | | |
904 | 3.55k | if ((flags & ZIP_CHECKCONS) && cd->offset + cd->size != cd->eocd_offset) { |
905 | 0 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_CDIR_OVERLAPS_EOCD); |
906 | 0 | return false; |
907 | 0 | } |
908 | | |
909 | 3.55k | return true; |
910 | 3.55k | } |
911 | | |
912 | | |
913 | 9.83k | cdir_status_t _zip_read_eocd64(zip_cdir_t *cdir, zip_source_t *src, zip_buffer_t *buffer, zip_uint64_t buf_offset, unsigned int flags, zip_error_t *error) { |
914 | 9.83k | zip_uint64_t offset; |
915 | 9.83k | zip_uint8_t eocd[EOCD64LEN]; |
916 | 9.83k | zip_uint64_t eocd_offset; |
917 | 9.83k | zip_uint64_t size, nentry, i, eocdloc_offset; |
918 | 9.83k | bool free_buffer; |
919 | 9.83k | zip_uint32_t num_disks, eocd_disk, this_disk; |
920 | | |
921 | | /* The offset of the end of the buffer is less than ZIP_UINT64_MAX, so this can't overflow. */ |
922 | 9.83k | eocdloc_offset = buf_offset + _zip_buffer_offset(buffer); |
923 | | |
924 | 9.83k | _zip_buffer_get(buffer, 4); /* magic already verified */ |
925 | | |
926 | 9.83k | eocd_disk = _zip_buffer_get_32(buffer); |
927 | 9.83k | eocd_offset = _zip_buffer_get_64(buffer); |
928 | 9.83k | num_disks = _zip_buffer_get_32(buffer); |
929 | | |
930 | 9.83k | if (!check_magic(eocd_offset, buffer, buf_offset, src, EOCD64_MAGIC)) { |
931 | 8.87k | return CDIR_NOT_FOUND; |
932 | 8.87k | } |
933 | | |
934 | 961 | if (num_disks != 1) { |
935 | 46 | zip_error_set(error, ZIP_ER_MULTIDISK, 0); |
936 | 46 | return CDIR_INVALID; |
937 | 46 | } |
938 | | |
939 | | /* valid seek value for start of EOCD */ |
940 | 915 | if (eocd_offset > ZIP_INT64_MAX) { |
941 | 0 | zip_error_set(error, ZIP_ER_SEEK, EFBIG); |
942 | 0 | return CDIR_INVALID; |
943 | 0 | } |
944 | | |
945 | | /* does EOCD fit before EOCD locator? */ |
946 | 915 | if (eocd_offset + EOCD64LEN > eocdloc_offset) { |
947 | 15 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_EOCD64_OVERLAPS_EOCD); |
948 | 15 | return CDIR_INVALID; |
949 | 15 | } |
950 | | |
951 | | /* make sure current position of buffer is beginning of EOCD */ |
952 | 900 | if (eocd_offset >= buf_offset && eocd_offset + EOCD64LEN <= buf_offset + _zip_buffer_size(buffer)) { |
953 | 887 | _zip_buffer_set_offset(buffer, eocd_offset - buf_offset); |
954 | 887 | free_buffer = false; |
955 | 887 | } |
956 | 13 | else { |
957 | 13 | if (zip_source_seek(src, (zip_int64_t)eocd_offset, SEEK_SET) < 0) { |
958 | 0 | zip_error_set_from_source(error, src); |
959 | 0 | return CDIR_INVALID; |
960 | 0 | } |
961 | 13 | if ((buffer = _zip_buffer_new_from_source(src, EOCD64LEN, eocd, error)) == NULL) { |
962 | 0 | return CDIR_INVALID; |
963 | 0 | } |
964 | 13 | free_buffer = true; |
965 | 13 | } |
966 | | |
967 | 900 | if (memcmp(_zip_buffer_get(buffer, 4), EOCD64_MAGIC, 4) != 0) { |
968 | 0 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_EOCD64_WRONG_MAGIC); |
969 | 0 | if (free_buffer) { |
970 | 0 | _zip_buffer_free(buffer); |
971 | 0 | } |
972 | 0 | return CDIR_INVALID; |
973 | 0 | } |
974 | | |
975 | | /* size of EOCD */ |
976 | 900 | size = _zip_buffer_get_64(buffer); |
977 | | |
978 | | /* Is there a hole between EOCD and EOCD locator, or do they overlap? Also check for overflow. */ |
979 | 900 | if ((flags & ZIP_CHECKCONS) && (!UNSIGNED_ADD_THREE_FITS(size, eocd_offset, 12) || size + eocd_offset + 12 != eocdloc_offset)) { |
980 | 0 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_EOCD64_OVERLAPS_EOCD); |
981 | 0 | if (free_buffer) { |
982 | 0 | _zip_buffer_free(buffer); |
983 | 0 | } |
984 | 0 | return CDIR_INVALID; |
985 | 0 | } |
986 | | |
987 | 900 | _zip_buffer_get(buffer, 4); /* skip version made by/needed */ |
988 | | |
989 | 900 | this_disk = _zip_buffer_get_32(buffer); |
990 | 900 | if (_zip_buffer_get_32(buffer) != eocd_disk) { |
991 | 65 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_EOCD64_LOCATOR_MISMATCH); |
992 | 65 | if (free_buffer) { |
993 | 3 | _zip_buffer_free(buffer); |
994 | 3 | } |
995 | 65 | return CDIR_INVALID; |
996 | 65 | } |
997 | | |
998 | 835 | i = _zip_buffer_get_64(buffer); |
999 | 835 | nentry = _zip_buffer_get_64(buffer); |
1000 | | |
1001 | 835 | if (nentry != i) { |
1002 | 110 | zip_error_set(error, ZIP_ER_MULTIDISK, 0); |
1003 | 110 | if (free_buffer) { |
1004 | 2 | _zip_buffer_free(buffer); |
1005 | 2 | } |
1006 | 110 | return CDIR_INVALID; |
1007 | 110 | } |
1008 | | |
1009 | 725 | size = _zip_buffer_get_64(buffer); |
1010 | 725 | offset = _zip_buffer_get_64(buffer); |
1011 | | |
1012 | | /* did we read past the end of the buffer? */ |
1013 | 725 | if (!_zip_buffer_ok(buffer)) { |
1014 | 0 | zip_error_set(error, ZIP_ER_INTERNAL, 0); |
1015 | 0 | if (free_buffer) { |
1016 | 0 | _zip_buffer_free(buffer); |
1017 | 0 | } |
1018 | 0 | return CDIR_INVALID; |
1019 | 0 | } |
1020 | | |
1021 | 725 | if (free_buffer) { |
1022 | 8 | _zip_buffer_free(buffer); |
1023 | 8 | } |
1024 | | |
1025 | 725 | if (offset > ZIP_INT64_MAX || !UNSIGNED_ADD_FITS(offset, size)) { |
1026 | 68 | zip_error_set(error, ZIP_ER_SEEK, EFBIG); |
1027 | 68 | return CDIR_INVALID; |
1028 | 68 | } |
1029 | | |
1030 | 657 | if (!UNSIGNED_MULTIPLY_FITS_CAPPED(nentry, CDENTRYSIZE, size)) { |
1031 | 19 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_CDIR_INVALID); |
1032 | 19 | return CDIR_INVALID; |
1033 | 19 | } |
1034 | | |
1035 | 638 | if ((cdir->size != 0xffffffff && cdir->size != size) || (cdir->offset != 0xffffffff && cdir->offset != offset) || (cdir->num_entries != 0xffff && cdir->num_entries != nentry) || (cdir->disk_entries != 0xffff && cdir->disk_entries != i) || (cdir->this_disk != 0xffff && cdir->this_disk != this_disk) || (cdir->eocd_disk != 0xffff && cdir->eocd_disk != eocd_disk)) { |
1036 | 450 | zip_error_set(error, ZIP_ER_INCONS, ZIP_ER_DETAIL_EOCD64_MISMATCH); |
1037 | 450 | return CDIR_INVALID; |
1038 | 450 | } |
1039 | | |
1040 | 188 | cdir->is_zip64 = true; |
1041 | 188 | cdir->size = size; |
1042 | 188 | cdir->offset = offset; |
1043 | 188 | cdir->disk_entries = i; |
1044 | 188 | cdir->num_entries = nentry; |
1045 | 188 | cdir->this_disk = this_disk; |
1046 | 188 | cdir->eocd_disk = eocd_disk; |
1047 | 188 | cdir->eocd_offset = eocd_offset; |
1048 | | |
1049 | 188 | return CDIR_OK; |
1050 | 638 | } |
1051 | | |
1052 | | |
1053 | 744 | static int decode_hex(char c) { |
1054 | 744 | if (c >= '0' && c <= '9') { |
1055 | 438 | return c - '0'; |
1056 | 438 | } |
1057 | 306 | else if (c >= 'A' && c <= 'F') { |
1058 | 244 | return c - 'A' + 10; |
1059 | 244 | } |
1060 | 62 | else { |
1061 | 62 | return -1; |
1062 | 62 | } |
1063 | 744 | } |
1064 | | |
1065 | | /* _zip_check_torrentzip: |
1066 | | check whether ZA has a valid TORRENTZIP comment, i.e. is torrentzipped */ |
1067 | | |
1068 | 2.28k | static void zip_check_torrentzip(zip_t *za, const zip_cdir_t *cdir) { |
1069 | 2.28k | zip_uint32_t crc_should; |
1070 | 2.28k | char buf[8 + 1]; |
1071 | 2.28k | size_t i; |
1072 | | |
1073 | 2.28k | if (cdir == NULL) { |
1074 | 0 | return; |
1075 | 0 | } |
1076 | | |
1077 | 2.28k | if (_zip_string_length(cdir->comment) != TORRENTZIP_SIGNATURE_LENGTH + TORRENTZIP_CRC_LENGTH || strncmp((const char *)cdir->comment->raw, TORRENTZIP_SIGNATURE, TORRENTZIP_SIGNATURE_LENGTH) != 0) { |
1078 | 2.16k | return; |
1079 | 2.16k | } |
1080 | | |
1081 | 112 | memcpy(buf, cdir->comment->raw + TORRENTZIP_SIGNATURE_LENGTH, TORRENTZIP_CRC_LENGTH); |
1082 | 112 | buf[TORRENTZIP_CRC_LENGTH] = '\0'; |
1083 | 112 | crc_should = 0; |
1084 | 443 | for (i = 0; i < TORRENTZIP_CRC_LENGTH; i += 2) { |
1085 | 372 | int low, high; |
1086 | 372 | high = decode_hex((buf[i])); |
1087 | 372 | low = decode_hex(buf[i + 1]); |
1088 | 372 | if (high < 0 || low < 0) { |
1089 | 41 | return; |
1090 | 41 | } |
1091 | 331 | crc_should = (crc_should << 8) + (high << 4) + low; |
1092 | 331 | } |
1093 | | |
1094 | 71 | { |
1095 | 71 | zip_stat_t st; |
1096 | 71 | zip_source_t *src_window; |
1097 | 71 | zip_source_t *src_crc; |
1098 | 71 | zip_uint8_t buffer[512]; |
1099 | 71 | zip_int64_t ret; |
1100 | | |
1101 | 71 | zip_stat_init(&st); |
1102 | 71 | st.valid |= ZIP_STAT_SIZE | ZIP_STAT_CRC; |
1103 | 71 | st.size = cdir->size; |
1104 | 71 | st.crc = crc_should; |
1105 | 71 | if ((src_window = _zip_source_window_new(za->src, cdir->offset, cdir->size, &st, 0, NULL, NULL, NULL, 0, false, NULL)) == NULL) { |
1106 | 0 | return; |
1107 | 0 | } |
1108 | 71 | if ((src_crc = zip_source_crc_create(src_window, 1, NULL)) == NULL) { |
1109 | 0 | zip_source_free(src_window); |
1110 | 0 | return; |
1111 | 0 | } |
1112 | 71 | if (zip_source_open(src_crc) != 0) { |
1113 | 0 | zip_source_free(src_crc); |
1114 | 0 | return; |
1115 | 0 | } |
1116 | 181 | while ((ret = zip_source_read(src_crc, buffer, sizeof(buffer))) > 0) { |
1117 | 110 | } |
1118 | 71 | zip_source_free(src_crc); |
1119 | 71 | if (ret < 0) { |
1120 | 69 | return; |
1121 | 69 | } |
1122 | 71 | } |
1123 | | |
1124 | | /* TODO: if check consistency, check cdir entries for valid values */ |
1125 | 2 | za->flags |= ZIP_AFL_IS_TORRENTZIP; |
1126 | 2 | } |