1 /*-
2  * Copyright (c) 2004 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_zip.c,v 1.24 2008/06/15 05:15:53 kientzle Exp $");
28 
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #include <stdio.h>
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #include <time.h>
37 #ifdef HAVE_ZLIB_H
38 #include <zlib.h>
39 #endif
40 
41 #include "archive.h"
42 #include "archive_entry.h"
43 #include "archive_private.h"
44 #include "archive_read_private.h"
45 #include "archive_endian.h"
46 
47 struct zip {
48 	/* entry_bytes_remaining is the number of bytes we expect. */
49 	int64_t			entry_bytes_remaining;
50 	int64_t			entry_offset;
51 
52 	/* These count the number of bytes actually read for the entry. */
53 	int64_t			entry_compressed_bytes_read;
54 	int64_t			entry_uncompressed_bytes_read;
55 
56 	unsigned		version;
57 	unsigned		system;
58 	unsigned		flags;
59 	unsigned		compression;
60 	const char *		compression_name;
61 	time_t			mtime;
62 	time_t			ctime;
63 	time_t			atime;
64 	mode_t			mode;
65 	uid_t			uid;
66 	gid_t			gid;
67 
68 	/* Flags to mark progress of decompression. */
69 	char			decompress_init;
70 	char			end_of_entry;
71 	char			end_of_entry_cleanup;
72 
73 	long			crc32;
74 	ssize_t			filename_length;
75 	ssize_t			extra_length;
76 	int64_t			uncompressed_size;
77 	int64_t			compressed_size;
78 
79 	unsigned char 		*uncompressed_buffer;
80 	size_t 			uncompressed_buffer_size;
81 #ifdef HAVE_ZLIB_H
82 	z_stream		stream;
83 	char			stream_valid;
84 #endif
85 
86 	struct archive_string	pathname;
87 	struct archive_string	extra;
88 	char	format_name[64];
89 };
90 
91 #define ZIP_LENGTH_AT_END	8
92 
93 struct zip_file_header {
94 	char	signature[4];
95 	char	version[2];
96 	char	flags[2];
97 	char	compression[2];
98 	char	timedate[4];
99 	char	crc32[4];
100 	char	compressed_size[4];
101 	char	uncompressed_size[4];
102 	char	filename_length[2];
103 	char	extra_length[2];
104 };
105 
106 static const char *compression_names[] = {
107 	"uncompressed",
108 	"shrinking",
109 	"reduced-1",
110 	"reduced-2",
111 	"reduced-3",
112 	"reduced-4",
113 	"imploded",
114 	"reserved",
115 	"deflation"
116 };
117 
118 static int	archive_read_format_zip_bid(struct archive_read *);
119 static int	archive_read_format_zip_cleanup(struct archive_read *);
120 static int	archive_read_format_zip_read_data(struct archive_read *,
121 		    const void **, size_t *, off_t *);
122 static int	archive_read_format_zip_read_data_skip(struct archive_read *a);
123 static int	archive_read_format_zip_read_header(struct archive_read *,
124 		    struct archive_entry *);
125 static int	zip_read_data_deflate(struct archive_read *a, const void **buff,
126 		    size_t *size, off_t *offset);
127 static int	zip_read_data_none(struct archive_read *a, const void **buff,
128 		    size_t *size, off_t *offset);
129 static int	zip_read_file_header(struct archive_read *a,
130 		    struct archive_entry *entry, struct zip *zip);
131 static time_t	zip_time(const char *);
132 static void process_extra(const void* extra, struct zip* zip);
133 
134 int
135 archive_read_support_format_zip(struct archive *_a)
136 {
137 	struct archive_read *a = (struct archive_read *)_a;
138 	struct zip *zip;
139 	int r;
140 
141 	zip = (struct zip *)malloc(sizeof(*zip));
142 	if (zip == NULL) {
143 		archive_set_error(&a->archive, ENOMEM, "Can't allocate zip data");
144 		return (ARCHIVE_FATAL);
145 	}
146 	memset(zip, 0, sizeof(*zip));
147 
148 	r = __archive_read_register_format(a,
149 	    zip,
150 	    archive_read_format_zip_bid,
151 	    archive_read_format_zip_read_header,
152 	    archive_read_format_zip_read_data,
153 	    archive_read_format_zip_read_data_skip,
154 	    archive_read_format_zip_cleanup);
155 
156 	if (r != ARCHIVE_OK)
157 		free(zip);
158 	return (ARCHIVE_OK);
159 }
160 
161 
162 static int
163 archive_read_format_zip_bid(struct archive_read *a)
164 {
165 	const char *p;
166 	const void *buff;
167 	size_t bytes_avail;
168 
169 	if ((p = __archive_read_ahead(a, 4)) == NULL)
170 		return (-1);
171 
172 	/*
173 	 * Bid of 30 here is: 16 bits for "PK",
174 	 * next 16-bit field has four options (-2 bits).
175 	 * 16 + 16-2 = 30.
176 	 */
177 	if (p[0] == 'P' && p[1] == 'K') {
178 		if ((p[2] == '\001' && p[3] == '\002')
179 		    || (p[2] == '\003' && p[3] == '\004')
180 		    || (p[2] == '\005' && p[3] == '\006')
181 		    || (p[2] == '\007' && p[3] == '\010')
182 		    || (p[2] == '0' && p[3] == '0'))
183 			return (30);
184 	}
185 
186 	/*
187 	 * Attempt to handle self-extracting archives
188 	 * by noting a PE header and searching forward
189 	 * up to 64k for a 'PK\003\004' marker.
190 	 */
191 	if (p[0] == 'M' && p[1] == 'Z') {
192 		/*
193 		 * TODO: Additional checks that this really is a PE
194 		 * file before we invoke the 128k lookahead below.
195 		 * No point in allocating a bigger lookahead buffer
196 		 * if we don't need to.
197 		 */
198 		/*
199 		 * TODO: Of course, the compression layer lookahead
200 		 * buffers aren't dynamically sized yet; they should be.
201 		 */
202 		bytes_avail = (a->decompressor->read_ahead)(a, &buff, 128*1024);
203 		p = (const char *)buff;
204 
205 		/*
206 		 * TODO: Optimize by jumping forward based on values
207 		 * in the PE header.  Note that we don't need to be
208 		 * exact, but we mustn't skip too far.  The search
209 		 * below will compensate if we undershoot.  Skipping
210 		 * will also reduce the chance of false positives
211 		 * (which is not really all that high to begin with,
212 		 * so maybe skipping isn't really necessary).
213 		 */
214 
215 		while (p < bytes_avail + (const char *)buff) {
216 			if (p[0] == 'P' && p[1] == 'K' /* "PK" signature */
217 			    && p[2] == 3 && p[3] == 4 /* File entry */
218 			    && p[8] == 8 /* compression == deflate */
219 			    && p[9] == 0 /* High byte of compression */
220 				)
221 			{
222 				return (30);
223 			}
224 			++p;
225 		}
226 	}
227 
228 	return (0);
229 }
230 
231 /*
232  * Search forward for a "PK\003\004" file header.  This handles the
233  * case of self-extracting archives, where there is an executable
234  * prepended to the ZIP archive.
235  */
236 static int
237 skip_sfx(struct archive_read *a)
238 {
239 	const void *h;
240 	const char *p, *q;
241 	size_t skip, bytes;
242 
243 	/*
244 	 * TODO: We should be able to skip forward by a bunch
245 	 * by lifting some values from the PE header.  We don't
246 	 * need to be exact (we're still going to search forward
247 	 * to find the header), but it will speed things up and
248 	 * reduce the chance of a false positive.
249 	 */
250 	for (;;) {
251 		bytes = (a->decompressor->read_ahead)(a, &h, 4096);
252 		if (bytes < 4)
253 			return (ARCHIVE_FATAL);
254 		p = h;
255 		q = p + bytes;
256 
257 		/*
258 		 * Scan ahead until we find something that looks
259 		 * like the zip header.
260 		 */
261 		while (p + 4 < q) {
262 			switch (p[3]) {
263 			case '\004':
264 				/* TODO: Additional verification here. */
265 				if (memcmp("PK\003\004", p, 4) == 0) {
266 					skip = p - (const char *)h;
267 					(a->decompressor->consume)(a, skip);
268 					return (ARCHIVE_OK);
269 				}
270 				p += 4;
271 				break;
272 			case '\003': p += 1; break;
273 			case 'K': p += 2; break;
274 			case 'P': p += 3; break;
275 			default: p += 4; break;
276 			}
277 		}
278 		skip = p - (const char *)h;
279 		(a->decompressor->consume)(a, skip);
280 	}
281 }
282 
283 static int
284 archive_read_format_zip_read_header(struct archive_read *a,
285     struct archive_entry *entry)
286 {
287 	const void *h;
288 	const char *signature;
289 	struct zip *zip;
290 	int r = ARCHIVE_OK, r1;
291 
292 	a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
293 	if (a->archive.archive_format_name == NULL)
294 		a->archive.archive_format_name = "ZIP";
295 
296 	zip = (struct zip *)(a->format->data);
297 	zip->decompress_init = 0;
298 	zip->end_of_entry = 0;
299 	zip->end_of_entry_cleanup = 0;
300 	zip->entry_uncompressed_bytes_read = 0;
301 	zip->entry_compressed_bytes_read = 0;
302 	if ((h = __archive_read_ahead(a, 4)) == NULL)
303 		return (ARCHIVE_FATAL);
304 
305 	signature = (const char *)h;
306 	if (signature[0] == 'M' && signature[1] == 'Z') {
307 		/* This is an executable?  Must be self-extracting... */
308 		r = skip_sfx(a);
309 		if (r < ARCHIVE_WARN)
310 			return (r);
311 		if ((h = __archive_read_ahead(a, 4)) == NULL)
312 			return (ARCHIVE_FATAL);
313 		signature = (const char *)h;
314 	}
315 
316 	if (signature[0] != 'P' || signature[1] != 'K') {
317 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
318 		    "Bad ZIP file");
319 		return (ARCHIVE_FATAL);
320 	}
321 
322 	/*
323 	 * "PK00" signature is used for "split" archives that
324 	 * only have a single segment.  This means we can just
325 	 * skip the PK00; the first real file header should follow.
326 	 */
327 	if (signature[2] == '0' && signature[3] == '0') {
328 		(a->decompressor->consume)(a, 4);
329 		if ((h = __archive_read_ahead(a, 4)) == NULL)
330 			return (ARCHIVE_FATAL);
331 		signature = (const char *)h;
332 		if (signature[0] != 'P' || signature[1] != 'K') {
333 			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
334 			    "Bad ZIP file");
335 			return (ARCHIVE_FATAL);
336 		}
337 	}
338 
339 	if (signature[2] == '\001' && signature[3] == '\002') {
340 		/* Beginning of central directory. */
341 		return (ARCHIVE_EOF);
342 	}
343 
344 	if (signature[2] == '\003' && signature[3] == '\004') {
345 		/* Regular file entry. */
346 		r1 = zip_read_file_header(a, entry, zip);
347 		if (r1 != ARCHIVE_OK)
348 			return (r1);
349 		return (r);
350 	}
351 
352 	if (signature[2] == '\005' && signature[3] == '\006') {
353 		/* End-of-archive record. */
354 		return (ARCHIVE_EOF);
355 	}
356 
357 	if (signature[2] == '\007' && signature[3] == '\010') {
358 		/*
359 		 * We should never encounter this record here;
360 		 * see ZIP_LENGTH_AT_END handling below for details.
361 		 */
362 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
363 		    "Bad ZIP file: Unexpected end-of-entry record");
364 		return (ARCHIVE_FATAL);
365 	}
366 
367 	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
368 	    "Damaged ZIP file or unsupported format variant (%d,%d)",
369 	    signature[2], signature[3]);
370 	return (ARCHIVE_FATAL);
371 }
372 
373 int
374 zip_read_file_header(struct archive_read *a, struct archive_entry *entry,
375     struct zip *zip)
376 {
377 	const struct zip_file_header *p;
378 	const void *h;
379 
380 	if ((p = __archive_read_ahead(a, sizeof *p)) == NULL) {
381 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
382 		    "Truncated ZIP file header");
383 		return (ARCHIVE_FATAL);
384 	}
385 
386 	zip->version = p->version[0];
387 	zip->system = p->version[1];
388 	zip->flags = archive_le16dec(p->flags);
389 	zip->compression = archive_le16dec(p->compression);
390 	if (zip->compression <
391 	    sizeof(compression_names)/sizeof(compression_names[0]))
392 		zip->compression_name = compression_names[zip->compression];
393 	else
394 		zip->compression_name = "??";
395 	zip->mtime = zip_time(p->timedate);
396 	zip->ctime = 0;
397 	zip->atime = 0;
398 	zip->mode = 0;
399 	zip->uid = 0;
400 	zip->gid = 0;
401 	zip->crc32 = archive_le32dec(p->crc32);
402 	zip->filename_length = archive_le16dec(p->filename_length);
403 	zip->extra_length = archive_le16dec(p->extra_length);
404 	zip->uncompressed_size = archive_le32dec(p->uncompressed_size);
405 	zip->compressed_size = archive_le32dec(p->compressed_size);
406 
407 	(a->decompressor->consume)(a, sizeof(struct zip_file_header));
408 
409 
410 	/* Read the filename. */
411 	if ((h = __archive_read_ahead(a, zip->filename_length)) == NULL) {
412 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
413 		    "Truncated ZIP file header");
414 		return (ARCHIVE_FATAL);
415 	}
416 	if (archive_string_ensure(&zip->pathname, zip->filename_length) == NULL)
417 		__archive_errx(1, "Out of memory");
418 	archive_strncpy(&zip->pathname, h, zip->filename_length);
419 	(a->decompressor->consume)(a, zip->filename_length);
420 	archive_entry_set_pathname(entry, zip->pathname.s);
421 
422 	if (zip->pathname.s[archive_strlen(&zip->pathname) - 1] == '/')
423 		zip->mode = AE_IFDIR | 0777;
424 	else
425 		zip->mode = AE_IFREG | 0777;
426 
427 	/* Read the extra data. */
428 	if ((h = __archive_read_ahead(a, zip->extra_length)) == NULL) {
429 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
430 		    "Truncated ZIP file header");
431 		return (ARCHIVE_FATAL);
432 	}
433 	process_extra(h, zip);
434 	(a->decompressor->consume)(a, zip->extra_length);
435 
436 	/* Populate some additional entry fields: */
437 	archive_entry_set_mode(entry, zip->mode);
438 	archive_entry_set_uid(entry, zip->uid);
439 	archive_entry_set_gid(entry, zip->gid);
440 	archive_entry_set_mtime(entry, zip->mtime, 0);
441 	archive_entry_set_ctime(entry, zip->ctime, 0);
442 	archive_entry_set_atime(entry, zip->atime, 0);
443 	archive_entry_set_size(entry, zip->uncompressed_size);
444 
445 	zip->entry_bytes_remaining = zip->compressed_size;
446 	zip->entry_offset = 0;
447 
448 	/* If there's no body, force read_data() to return EOF immediately. */
449 	if (0 == (zip->flags & ZIP_LENGTH_AT_END)
450 	    && zip->entry_bytes_remaining < 1)
451 		zip->end_of_entry = 1;
452 
453 	/* Set up a more descriptive format name. */
454 	sprintf(zip->format_name, "ZIP %d.%d (%s)",
455 	    zip->version / 10, zip->version % 10,
456 	    zip->compression_name);
457 	a->archive.archive_format_name = zip->format_name;
458 
459 	return (ARCHIVE_OK);
460 }
461 
462 /* Convert an MSDOS-style date/time into Unix-style time. */
463 static time_t
464 zip_time(const char *p)
465 {
466 	int msTime, msDate;
467 	struct tm ts;
468 
469 	msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
470 	msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
471 
472 	memset(&ts, 0, sizeof(ts));
473 	ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
474 	ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
475 	ts.tm_mday = msDate & 0x1f; /* Day of month. */
476 	ts.tm_hour = (msTime >> 11) & 0x1f;
477 	ts.tm_min = (msTime >> 5) & 0x3f;
478 	ts.tm_sec = (msTime << 1) & 0x3e;
479 	ts.tm_isdst = -1;
480 	return mktime(&ts);
481 }
482 
483 static int
484 archive_read_format_zip_read_data(struct archive_read *a,
485     const void **buff, size_t *size, off_t *offset)
486 {
487 	int r;
488 	struct zip *zip;
489 
490 	zip = (struct zip *)(a->format->data);
491 
492 	/*
493 	 * If we hit end-of-entry last time, clean up and return
494 	 * ARCHIVE_EOF this time.
495 	 */
496 	if (zip->end_of_entry) {
497 		if (!zip->end_of_entry_cleanup) {
498 			if (zip->flags & ZIP_LENGTH_AT_END) {
499 				const char *p;
500 
501 				if ((p = __archive_read_ahead(a, 16)) == NULL) {
502 					archive_set_error(&a->archive,
503 					    ARCHIVE_ERRNO_FILE_FORMAT,
504 					    "Truncated ZIP end-of-file record");
505 					return (ARCHIVE_FATAL);
506 				}
507 				zip->crc32 = archive_le32dec(p + 4);
508 				zip->compressed_size = archive_le32dec(p + 8);
509 				zip->uncompressed_size = archive_le32dec(p + 12);
510 				(a->decompressor->consume)(a, 16);
511 			}
512 
513 			/* Check file size, CRC against these values. */
514 			if (zip->compressed_size != zip->entry_compressed_bytes_read) {
515 				archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
516 				    "ZIP compressed data is wrong size");
517 				return (ARCHIVE_WARN);
518 			}
519 			/* Size field only stores the lower 32 bits of the actual size. */
520 			if ((zip->uncompressed_size & UINT32_MAX)
521 			    != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
522 				archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
523 				    "ZIP uncompressed data is wrong size");
524 				return (ARCHIVE_WARN);
525 			}
526 /* TODO: Compute CRC. */
527 /*
528 			if (zip->crc32 != zip->entry_crc32_calculated) {
529 				archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
530 				    "ZIP data CRC error");
531 				return (ARCHIVE_WARN);
532 			}
533 */
534 			/* End-of-entry cleanup done. */
535 			zip->end_of_entry_cleanup = 1;
536 		}
537 		*offset = zip->entry_uncompressed_bytes_read;
538 		*size = 0;
539 		*buff = NULL;
540 		return (ARCHIVE_EOF);
541 	}
542 
543 	switch(zip->compression) {
544 	case 0:  /* No compression. */
545 		r =  zip_read_data_none(a, buff, size, offset);
546 		break;
547 	case 8: /* Deflate compression. */
548 		r =  zip_read_data_deflate(a, buff, size, offset);
549 		break;
550 	default: /* Unsupported compression. */
551 		*buff = NULL;
552 		*size = 0;
553 		*offset = 0;
554 		/* Return a warning. */
555 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
556 		    "Unsupported ZIP compression method (%s)",
557 		    zip->compression_name);
558 		if (zip->flags & ZIP_LENGTH_AT_END) {
559 			/*
560 			 * ZIP_LENGTH_AT_END requires us to
561 			 * decompress the entry in order to
562 			 * skip it, but we don't know this
563 			 * compression method, so we give up.
564 			 */
565 			r = ARCHIVE_FATAL;
566 		} else {
567 			/* We can't decompress this entry, but we will
568 			 * be able to skip() it and try the next entry. */
569 			r = ARCHIVE_WARN;
570 		}
571 		break;
572 	}
573 	return (r);
574 }
575 
576 /*
577  * Read "uncompressed" data.  According to the current specification,
578  * if ZIP_LENGTH_AT_END is specified, then the size fields in the
579  * initial file header are supposed to be set to zero.  This would, of
580  * course, make it impossible for us to read the archive, since we
581  * couldn't determine the end of the file data.  Info-ZIP seems to
582  * include the real size fields both before and after the data in this
583  * case (the CRC only appears afterwards), so this works as you would
584  * expect.
585  *
586  * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
587  * zip->end_of_entry if it consumes all of the data.
588  */
589 static int
590 zip_read_data_none(struct archive_read *a, const void **buff,
591     size_t *size, off_t *offset)
592 {
593 	struct zip *zip;
594 	ssize_t bytes_avail;
595 
596 	zip = (struct zip *)(a->format->data);
597 
598 	if (zip->entry_bytes_remaining == 0) {
599 		*buff = NULL;
600 		*size = 0;
601 		*offset = zip->entry_offset;
602 		zip->end_of_entry = 1;
603 		return (ARCHIVE_OK);
604 	}
605 	/*
606 	 * Note: '1' here is a performance optimization.
607 	 * Recall that the decompression layer returns a count of
608 	 * available bytes; asking for more than that forces the
609 	 * decompressor to combine reads by copying data.
610 	 */
611 	bytes_avail = (a->decompressor->read_ahead)(a, buff, 1);
612 	if (bytes_avail <= 0) {
613 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
614 		    "Truncated ZIP file data");
615 		return (ARCHIVE_FATAL);
616 	}
617 	if (bytes_avail > zip->entry_bytes_remaining)
618 		bytes_avail = zip->entry_bytes_remaining;
619 	(a->decompressor->consume)(a, bytes_avail);
620 	*size = bytes_avail;
621 	*offset = zip->entry_offset;
622 	zip->entry_offset += *size;
623 	zip->entry_bytes_remaining -= *size;
624 	zip->entry_uncompressed_bytes_read += *size;
625 	zip->entry_compressed_bytes_read += *size;
626 	return (ARCHIVE_OK);
627 }
628 
629 #ifdef HAVE_ZLIB_H
630 static int
631 zip_read_data_deflate(struct archive_read *a, const void **buff,
632     size_t *size, off_t *offset)
633 {
634 	struct zip *zip;
635 	ssize_t bytes_avail;
636 	const void *compressed_buff;
637 	int r;
638 
639 	zip = (struct zip *)(a->format->data);
640 
641 	/* If the buffer hasn't been allocated, allocate it now. */
642 	if (zip->uncompressed_buffer == NULL) {
643 		zip->uncompressed_buffer_size = 32 * 1024;
644 		zip->uncompressed_buffer
645 		    = (unsigned char *)malloc(zip->uncompressed_buffer_size);
646 		if (zip->uncompressed_buffer == NULL) {
647 			archive_set_error(&a->archive, ENOMEM,
648 			    "No memory for ZIP decompression");
649 			return (ARCHIVE_FATAL);
650 		}
651 	}
652 
653 	/* If we haven't yet read any data, initialize the decompressor. */
654 	if (!zip->decompress_init) {
655 		if (zip->stream_valid)
656 			r = inflateReset(&zip->stream);
657 		else
658 			r = inflateInit2(&zip->stream,
659 			    -15 /* Don't check for zlib header */);
660 		if (r != Z_OK) {
661 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
662 			    "Can't initialize ZIP decompression.");
663 			return (ARCHIVE_FATAL);
664 		}
665 		/* Stream structure has been set up. */
666 		zip->stream_valid = 1;
667 		/* We've initialized decompression for this stream. */
668 		zip->decompress_init = 1;
669 	}
670 
671 	/*
672 	 * Note: '1' here is a performance optimization.
673 	 * Recall that the decompression layer returns a count of
674 	 * available bytes; asking for more than that forces the
675 	 * decompressor to combine reads by copying data.
676 	 */
677 	bytes_avail = (a->decompressor->read_ahead)(a, &compressed_buff, 1);
678 	if (bytes_avail <= 0) {
679 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
680 		    "Truncated ZIP file body");
681 		return (ARCHIVE_FATAL);
682 	}
683 
684 	/*
685 	 * A bug in zlib.h: stream.next_in should be marked 'const'
686 	 * but isn't (the library never alters data through the
687 	 * next_in pointer, only reads it).  The result: this ugly
688 	 * cast to remove 'const'.
689 	 */
690 	zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
691 	zip->stream.avail_in = bytes_avail;
692 	zip->stream.total_in = 0;
693 	zip->stream.next_out = zip->uncompressed_buffer;
694 	zip->stream.avail_out = zip->uncompressed_buffer_size;
695 	zip->stream.total_out = 0;
696 
697 	r = inflate(&zip->stream, 0);
698 	switch (r) {
699 	case Z_OK:
700 		break;
701 	case Z_STREAM_END:
702 		zip->end_of_entry = 1;
703 		break;
704 	case Z_MEM_ERROR:
705 		archive_set_error(&a->archive, ENOMEM,
706 		    "Out of memory for ZIP decompression");
707 		return (ARCHIVE_FATAL);
708 	default:
709 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
710 		    "ZIP decompression failed (%d)", r);
711 		return (ARCHIVE_FATAL);
712 	}
713 
714 	/* Consume as much as the compressor actually used. */
715 	bytes_avail = zip->stream.total_in;
716 	(a->decompressor->consume)(a, bytes_avail);
717 	zip->entry_bytes_remaining -= bytes_avail;
718 	zip->entry_compressed_bytes_read += bytes_avail;
719 
720 	*offset = zip->entry_offset;
721 	*size = zip->stream.total_out;
722 	zip->entry_uncompressed_bytes_read += *size;
723 	*buff = zip->uncompressed_buffer;
724 	zip->entry_offset += *size;
725 	return (ARCHIVE_OK);
726 }
727 #else
728 static int
729 zip_read_data_deflate(struct archive_read *a, const void **buff,
730     size_t *size, off_t *offset)
731 {
732 	*buff = NULL;
733 	*size = 0;
734 	*offset = 0;
735 	archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
736 	    "libarchive compiled without deflate support (no libz)");
737 	return (ARCHIVE_FATAL);
738 }
739 #endif
740 
741 static int
742 archive_read_format_zip_read_data_skip(struct archive_read *a)
743 {
744 	struct zip *zip;
745 	const void *buff = NULL;
746 	off_t bytes_skipped;
747 
748 	zip = (struct zip *)(a->format->data);
749 
750 	/* If we've already read to end of data, we're done. */
751 	if (zip->end_of_entry_cleanup)
752 		return (ARCHIVE_OK);
753 
754 	/*
755 	 * If the length is at the end, we have no choice but
756 	 * to decompress all the data to find the end marker.
757 	 */
758 	if (zip->flags & ZIP_LENGTH_AT_END) {
759 		size_t size;
760 		off_t offset;
761 		int r;
762 		do {
763 			r = archive_read_format_zip_read_data(a, &buff,
764 			    &size, &offset);
765 		} while (r == ARCHIVE_OK);
766 		return (r);
767 	}
768 
769 	/*
770 	 * If the length is at the beginning, we can skip the
771 	 * compressed data much more quickly.
772 	 */
773 	bytes_skipped = (a->decompressor->skip)(a, zip->entry_bytes_remaining);
774 	if (bytes_skipped < 0)
775 		return (ARCHIVE_FATAL);
776 
777 	/* This entry is finished and done. */
778 	zip->end_of_entry_cleanup = zip->end_of_entry = 1;
779 	return (ARCHIVE_OK);
780 }
781 
782 static int
783 archive_read_format_zip_cleanup(struct archive_read *a)
784 {
785 	struct zip *zip;
786 
787 	zip = (struct zip *)(a->format->data);
788 #ifdef HAVE_ZLIB_H
789 	if (zip->stream_valid)
790 		inflateEnd(&zip->stream);
791 #endif
792 	free(zip->uncompressed_buffer);
793 	archive_string_free(&(zip->pathname));
794 	archive_string_free(&(zip->extra));
795 	free(zip);
796 	(a->format->data) = NULL;
797 	return (ARCHIVE_OK);
798 }
799 
800 /*
801  * The extra data is stored as a list of
802  *	id1+size1+data1 + id2+size2+data2 ...
803  *  triplets.  id and size are 2 bytes each.
804  */
805 static void
806 process_extra(const void* extra, struct zip* zip)
807 {
808 	int offset = 0;
809 	const char *p = (const char *)extra;
810 	while (offset < zip->extra_length - 4)
811 	{
812 		unsigned short headerid = archive_le16dec(p + offset);
813 		unsigned short datasize = archive_le16dec(p + offset + 2);
814 		offset += 4;
815 		if (offset + datasize > zip->extra_length)
816 			break;
817 #ifdef DEBUG
818 		fprintf(stderr, "Header id 0x%04x, length %d\n",
819 		    headerid, datasize);
820 #endif
821 		switch (headerid) {
822 		case 0x0001:
823 			/* Zip64 extended information extra field. */
824 			if (datasize >= 8)
825 				zip->uncompressed_size = archive_le64dec(p + offset);
826 			if (datasize >= 16)
827 				zip->compressed_size = archive_le64dec(p + offset + 8);
828 			break;
829 		case 0x5455:
830 		{
831 			/* Extended time field "UT". */
832 			int flags = p[offset];
833 			offset++;
834 			datasize--;
835 			/* Flag bits indicate which dates are present. */
836 			if (flags & 0x01)
837 			{
838 #ifdef DEBUG
839 				fprintf(stderr, "mtime: %lld -> %d\n",
840 				    (long long)zip->mtime,
841 				    archive_le32dec(p + offset));
842 #endif
843 				if (datasize < 4)
844 					break;
845 				zip->mtime = archive_le32dec(p + offset);
846 				offset += 4;
847 				datasize -= 4;
848 			}
849 			if (flags & 0x02)
850 			{
851 				if (datasize < 4)
852 					break;
853 				zip->atime = archive_le32dec(p + offset);
854 				offset += 4;
855 				datasize -= 4;
856 			}
857 			if (flags & 0x04)
858 			{
859 				if (datasize < 4)
860 					break;
861 				zip->ctime = archive_le32dec(p + offset);
862 				offset += 4;
863 				datasize -= 4;
864 			}
865 			break;
866 		}
867 		case 0x7855:
868 			/* Info-ZIP Unix Extra Field (type 2) "Ux". */
869 #ifdef DEBUG
870 			fprintf(stderr, "uid %d gid %d\n",
871 			    archive_le16dec(p + offset),
872 			    archive_le16dec(p + offset + 2));
873 #endif
874 			if (datasize >= 2)
875 				zip->uid = archive_le16dec(p + offset);
876 			if (datasize >= 4)
877 				zip->gid = archive_le16dec(p + offset + 2);
878 			break;
879 		default:
880 			break;
881 		}
882 		offset += datasize;
883 	}
884 #ifdef DEBUG
885 	if (offset != zip->extra_length)
886 	{
887 		fprintf(stderr,
888 		    "Extra data field contents do not match reported size!");
889 	}
890 #endif
891 }
892