archive_read_support_format_zip.c revision 302001
1/*-
2 * Copyright (c) 2004-2013 Tim Kientzle
3 * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
4 * Copyright (c) 2013 Konrad Kleine
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "archive_platform.h"
29__FBSDID("$FreeBSD: stable/10/contrib/libarchive/libarchive/archive_read_support_format_zip.c 302001 2016-06-17 22:40:10Z mm $");
30
31/*
32 * The definitive documentation of the Zip file format is:
33 *   http://www.pkware.com/documents/casestudies/APPNOTE.TXT
34 *
35 * The Info-Zip project has pioneered various extensions to better
36 * support Zip on Unix, including the 0x5455 "UT", 0x5855 "UX", 0x7855
37 * "Ux", and 0x7875 "ux" extensions for time and ownership
38 * information.
39 *
40 * History of this code: The streaming Zip reader was first added to
41 * libarchive in January 2005.  Support for seekable input sources was
42 * added in Nov 2011.  Zip64 support (including a significant code
43 * refactoring) was added in 2014.
44 */
45
46#ifdef HAVE_ERRNO_H
47#include <errno.h>
48#endif
49#ifdef HAVE_STDLIB_H
50#include <stdlib.h>
51#endif
52#ifdef HAVE_ZLIB_H
53#include <zlib.h>
54#endif
55
56#include "archive.h"
57#include "archive_digest_private.h"
58#include "archive_cryptor_private.h"
59#include "archive_endian.h"
60#include "archive_entry.h"
61#include "archive_entry_locale.h"
62#include "archive_hmac_private.h"
63#include "archive_private.h"
64#include "archive_rb.h"
65#include "archive_read_private.h"
66
67#ifndef HAVE_ZLIB_H
68#include "archive_crc32.h"
69#endif
70
71struct zip_entry {
72	struct archive_rb_node	node;
73	struct zip_entry	*next;
74	int64_t			local_header_offset;
75	int64_t			compressed_size;
76	int64_t			uncompressed_size;
77	int64_t			gid;
78	int64_t			uid;
79	struct archive_string	rsrcname;
80	time_t			mtime;
81	time_t			atime;
82	time_t			ctime;
83	uint32_t		crc32;
84	uint16_t		mode;
85	uint16_t		zip_flags; /* From GP Flags Field */
86	unsigned char		compression;
87	unsigned char		system; /* From "version written by" */
88	unsigned char		flags; /* Our extra markers. */
89	unsigned char		decdat;/* Used for Decryption check */
90
91	/* WinZip AES encryption extra field should be available
92	 * when compression is 99. */
93	struct {
94		/* Vendor version: AE-1 - 0x0001, AE-2 - 0x0002 */
95		unsigned	vendor;
96#define AES_VENDOR_AE_1	0x0001
97#define AES_VENDOR_AE_2	0x0002
98		/* AES encryption strength:
99		 * 1 - 128 bits, 2 - 192 bits, 2 - 256 bits. */
100		unsigned	strength;
101		/* Actual compression method. */
102		unsigned char	compression;
103	}			aes_extra;
104};
105
106struct trad_enc_ctx {
107	uint32_t	keys[3];
108};
109
110/* Bits used in zip_flags. */
111#define ZIP_ENCRYPTED	(1 << 0)
112#define ZIP_LENGTH_AT_END	(1 << 3)
113#define ZIP_STRONG_ENCRYPTED	(1 << 6)
114#define ZIP_UTF8_NAME	(1 << 11)
115/* See "7.2 Single Password Symmetric Encryption Method"
116   in http://www.pkware.com/documents/casestudies/APPNOTE.TXT */
117#define ZIP_CENTRAL_DIRECTORY_ENCRYPTED	(1 << 13)
118
119/* Bits used in flags. */
120#define LA_USED_ZIP64	(1 << 0)
121#define LA_FROM_CENTRAL_DIRECTORY (1 << 1)
122
123/*
124 * See "WinZip - AES Encryption Information"
125 *     http://www.winzip.com/aes_info.htm
126 */
127/* Value used in compression method. */
128#define WINZIP_AES_ENCRYPTION	99
129/* Authentication code size. */
130#define AUTH_CODE_SIZE	10
131/**/
132#define MAX_DERIVED_KEY_BUF_SIZE	(AES_MAX_KEY_SIZE * 2 + 2)
133
134struct zip {
135	/* Structural information about the archive. */
136	struct archive_string	format_name;
137	int64_t			central_directory_offset;
138	size_t			central_directory_entries_total;
139	size_t			central_directory_entries_on_this_disk;
140	int			has_encrypted_entries;
141
142	/* List of entries (seekable Zip only) */
143	struct zip_entry	*zip_entries;
144	struct archive_rb_tree	tree;
145	struct archive_rb_tree	tree_rsrc;
146
147	/* Bytes read but not yet consumed via __archive_read_consume() */
148	size_t			unconsumed;
149
150	/* Information about entry we're currently reading. */
151	struct zip_entry	*entry;
152	int64_t			entry_bytes_remaining;
153
154	/* These count the number of bytes actually read for the entry. */
155	int64_t			entry_compressed_bytes_read;
156	int64_t			entry_uncompressed_bytes_read;
157
158	/* Running CRC32 of the decompressed data */
159	unsigned long		entry_crc32;
160	unsigned long		(*crc32func)(unsigned long, const void *,
161				    size_t);
162	char			ignore_crc32;
163
164	/* Flags to mark progress of decompression. */
165	char			decompress_init;
166	char			end_of_entry;
167
168#ifdef HAVE_ZLIB_H
169	unsigned char 		*uncompressed_buffer;
170	size_t 			uncompressed_buffer_size;
171	z_stream		stream;
172	char			stream_valid;
173#endif
174
175	struct archive_string_conv *sconv;
176	struct archive_string_conv *sconv_default;
177	struct archive_string_conv *sconv_utf8;
178	int			init_default_conversion;
179	int			process_mac_extensions;
180
181	char			init_decryption;
182
183	/* Decryption buffer. */
184	unsigned char 		*decrypted_buffer;
185	unsigned char 		*decrypted_ptr;
186	size_t 			decrypted_buffer_size;
187	size_t 			decrypted_bytes_remaining;
188	size_t 			decrypted_unconsumed_bytes;
189
190	/* Traditional PKWARE decryption. */
191	struct trad_enc_ctx	tctx;
192	char			tctx_valid;
193
194	/* WinZip AES decyption. */
195	/* Contexts used for AES decryption. */
196	archive_crypto_ctx	cctx;
197	char			cctx_valid;
198	archive_hmac_sha1_ctx	hctx;
199	char			hctx_valid;
200
201	/* Strong encryption's decryption header information. */
202	unsigned		iv_size;
203	unsigned		alg_id;
204	unsigned		bit_len;
205	unsigned		flags;
206	unsigned		erd_size;
207	unsigned		v_size;
208	unsigned		v_crc32;
209	uint8_t			*iv;
210	uint8_t			*erd;
211	uint8_t			*v_data;
212};
213
214/* Many systems define min or MIN, but not all. */
215#define	zipmin(a,b) ((a) < (b) ? (a) : (b))
216
217/* ------------------------------------------------------------------------ */
218
219/*
220  Traditional PKWARE Decryption functions.
221 */
222
223static void
224trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
225{
226	uint8_t t;
227#define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
228
229	ctx->keys[0] = CRC32(ctx->keys[0], c);
230	ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
231	t = (ctx->keys[1] >> 24) & 0xff;
232	ctx->keys[2] = CRC32(ctx->keys[2], t);
233#undef CRC32
234}
235
236static uint8_t
237trad_enc_decypt_byte(struct trad_enc_ctx *ctx)
238{
239	unsigned temp = ctx->keys[2] | 2;
240	return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
241}
242
243static void
244trad_enc_decrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
245    size_t in_len, uint8_t *out, size_t out_len)
246{
247	unsigned i, max;
248
249	max = (unsigned)((in_len < out_len)? in_len: out_len);
250
251	for (i = 0; i < max; i++) {
252		uint8_t t = in[i] ^ trad_enc_decypt_byte(ctx);
253		out[i] = t;
254		trad_enc_update_keys(ctx, t);
255	}
256}
257
258static int
259trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len,
260    const uint8_t *key, size_t key_len, uint8_t *crcchk)
261{
262	uint8_t header[12];
263
264	if (key_len < 12) {
265		*crcchk = 0xff;
266		return -1;
267	}
268
269	ctx->keys[0] = 305419896L;
270	ctx->keys[1] = 591751049L;
271	ctx->keys[2] = 878082192L;
272
273	for (;pw_len; --pw_len)
274		trad_enc_update_keys(ctx, *pw++);
275
276	trad_enc_decrypt_update(ctx, key, 12, header, 12);
277	/* Return the last byte for CRC check. */
278	*crcchk = header[11];
279	return 0;
280}
281
282#if 0
283static void
284crypt_derive_key_sha1(const void *p, int size, unsigned char *key,
285    int key_size)
286{
287#define MD_SIZE 20
288	archive_sha1_ctx ctx;
289	unsigned char md1[MD_SIZE];
290	unsigned char md2[MD_SIZE * 2];
291	unsigned char mkb[64];
292	int i;
293
294	archive_sha1_init(&ctx);
295	archive_sha1_update(&ctx, p, size);
296	archive_sha1_final(&ctx, md1);
297
298	memset(mkb, 0x36, sizeof(mkb));
299	for (i = 0; i < MD_SIZE; i++)
300		mkb[i] ^= md1[i];
301	archive_sha1_init(&ctx);
302	archive_sha1_update(&ctx, mkb, sizeof(mkb));
303	archive_sha1_final(&ctx, md2);
304
305	memset(mkb, 0x5C, sizeof(mkb));
306	for (i = 0; i < MD_SIZE; i++)
307		mkb[i] ^= md1[i];
308	archive_sha1_init(&ctx);
309	archive_sha1_update(&ctx, mkb, sizeof(mkb));
310	archive_sha1_final(&ctx, md2 + MD_SIZE);
311
312	if (key_size > 32)
313		key_size = 32;
314	memcpy(key, md2, key_size);
315#undef MD_SIZE
316}
317#endif
318
319/*
320 * Common code for streaming or seeking modes.
321 *
322 * Includes code to read local file headers, decompress data
323 * from entry bodies, and common API.
324 */
325
326static unsigned long
327real_crc32(unsigned long crc, const void *buff, size_t len)
328{
329	return crc32(crc, buff, (unsigned int)len);
330}
331
332/* Used by "ignorecrc32" option to speed up tests. */
333static unsigned long
334fake_crc32(unsigned long crc, const void *buff, size_t len)
335{
336	(void)crc; /* UNUSED */
337	(void)buff; /* UNUSED */
338	(void)len; /* UNUSED */
339	return 0;
340}
341
342static struct {
343	int id;
344	const char * name;
345} compression_methods[] = {
346	{0, "uncompressed"}, /* The file is stored (no compression) */
347	{1, "shrinking"}, /* The file is Shrunk */
348	{2, "reduced-1"}, /* The file is Reduced with compression factor 1 */
349	{3, "reduced-2"}, /* The file is Reduced with compression factor 2 */
350	{4, "reduced-3"}, /* The file is Reduced with compression factor 3 */
351	{5, "reduced-4"}, /* The file is Reduced with compression factor 4 */
352	{6, "imploded"},  /* The file is Imploded */
353	{7, "reserved"},  /* Reserved for Tokenizing compression algorithm */
354	{8, "deflation"}, /* The file is Deflated */
355	{9, "deflation-64-bit"}, /* Enhanced Deflating using Deflate64(tm) */
356	{10, "ibm-terse"},/* PKWARE Data Compression Library Imploding
357			   * (old IBM TERSE) */
358	{11, "reserved"}, /* Reserved by PKWARE */
359	{12, "bzip"},     /* File is compressed using BZIP2 algorithm */
360	{13, "reserved"}, /* Reserved by PKWARE */
361	{14, "lzma"},     /* LZMA (EFS) */
362	{15, "reserved"}, /* Reserved by PKWARE */
363	{16, "reserved"}, /* Reserved by PKWARE */
364	{17, "reserved"}, /* Reserved by PKWARE */
365	{18, "ibm-terse-new"}, /* File is compressed using IBM TERSE (new) */
366	{19, "ibm-lz777"},/* IBM LZ77 z Architecture (PFS) */
367	{97, "wav-pack"}, /* WavPack compressed data */
368	{98, "ppmd-1"},   /* PPMd version I, Rev 1 */
369	{99, "aes"}       /* WinZip AES encryption  */
370};
371
372static const char *
373compression_name(const int compression)
374{
375	static const int num_compression_methods =
376		sizeof(compression_methods)/sizeof(compression_methods[0]);
377	int i=0;
378
379	while(compression >= 0 && i < num_compression_methods) {
380		if (compression_methods[i].id == compression)
381			return compression_methods[i].name;
382		i++;
383	}
384	return "??";
385}
386
387/* Convert an MSDOS-style date/time into Unix-style time. */
388static time_t
389zip_time(const char *p)
390{
391	int msTime, msDate;
392	struct tm ts;
393
394	msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
395	msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
396
397	memset(&ts, 0, sizeof(ts));
398	ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
399	ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
400	ts.tm_mday = msDate & 0x1f; /* Day of month. */
401	ts.tm_hour = (msTime >> 11) & 0x1f;
402	ts.tm_min = (msTime >> 5) & 0x3f;
403	ts.tm_sec = (msTime << 1) & 0x3e;
404	ts.tm_isdst = -1;
405	return mktime(&ts);
406}
407
408/*
409 * The extra data is stored as a list of
410 *	id1+size1+data1 + id2+size2+data2 ...
411 *  triplets.  id and size are 2 bytes each.
412 */
413static void
414process_extra(const char *p, size_t extra_length, struct zip_entry* zip_entry)
415{
416	unsigned offset = 0;
417
418	while (offset < extra_length - 4) {
419		unsigned short headerid = archive_le16dec(p + offset);
420		unsigned short datasize = archive_le16dec(p + offset + 2);
421
422		offset += 4;
423		if (offset + datasize > extra_length) {
424			break;
425		}
426#ifdef DEBUG
427		fprintf(stderr, "Header id 0x%04x, length %d\n",
428		    headerid, datasize);
429#endif
430		switch (headerid) {
431		case 0x0001:
432			/* Zip64 extended information extra field. */
433			zip_entry->flags |= LA_USED_ZIP64;
434			if (zip_entry->uncompressed_size == 0xffffffff) {
435				if (datasize < 8)
436					break;
437				zip_entry->uncompressed_size =
438				    archive_le64dec(p + offset);
439				offset += 8;
440				datasize -= 8;
441			}
442			if (zip_entry->compressed_size == 0xffffffff) {
443				if (datasize < 8)
444					break;
445				zip_entry->compressed_size =
446				    archive_le64dec(p + offset);
447				offset += 8;
448				datasize -= 8;
449			}
450			if (zip_entry->local_header_offset == 0xffffffff) {
451				if (datasize < 8)
452					break;
453				zip_entry->local_header_offset =
454				    archive_le64dec(p + offset);
455				offset += 8;
456				datasize -= 8;
457			}
458			/* archive_le32dec(p + offset) gives disk
459			 * on which file starts, but we don't handle
460			 * multi-volume Zip files. */
461			break;
462#ifdef DEBUG
463		case 0x0017:
464		{
465			/* Strong encryption field. */
466			if (archive_le16dec(p + offset) == 2) {
467				unsigned algId =
468					archive_le16dec(p + offset + 2);
469				unsigned bitLen =
470					archive_le16dec(p + offset + 4);
471				int	 flags =
472					archive_le16dec(p + offset + 6);
473				fprintf(stderr, "algId=0x%04x, bitLen=%u, "
474				    "flgas=%d\n", algId, bitLen,flags);
475			}
476			break;
477		}
478#endif
479		case 0x5455:
480		{
481			/* Extended time field "UT". */
482			int flags = p[offset];
483			offset++;
484			datasize--;
485			/* Flag bits indicate which dates are present. */
486			if (flags & 0x01)
487			{
488#ifdef DEBUG
489				fprintf(stderr, "mtime: %lld -> %d\n",
490				    (long long)zip_entry->mtime,
491				    archive_le32dec(p + offset));
492#endif
493				if (datasize < 4)
494					break;
495				zip_entry->mtime = archive_le32dec(p + offset);
496				offset += 4;
497				datasize -= 4;
498			}
499			if (flags & 0x02)
500			{
501				if (datasize < 4)
502					break;
503				zip_entry->atime = archive_le32dec(p + offset);
504				offset += 4;
505				datasize -= 4;
506			}
507			if (flags & 0x04)
508			{
509				if (datasize < 4)
510					break;
511				zip_entry->ctime = archive_le32dec(p + offset);
512				offset += 4;
513				datasize -= 4;
514			}
515			break;
516		}
517		case 0x5855:
518		{
519			/* Info-ZIP Unix Extra Field (old version) "UX". */
520			if (datasize >= 8) {
521				zip_entry->atime = archive_le32dec(p + offset);
522				zip_entry->mtime =
523				    archive_le32dec(p + offset + 4);
524			}
525			if (datasize >= 12) {
526				zip_entry->uid =
527				    archive_le16dec(p + offset + 8);
528				zip_entry->gid =
529				    archive_le16dec(p + offset + 10);
530			}
531			break;
532		}
533		case 0x6c78:
534		{
535			/* Experimental 'xl' field */
536			/*
537			 * Introduced Dec 2013 to provide a way to
538			 * include external file attributes (and other
539			 * fields that ordinarily appear only in
540			 * central directory) in local file header.
541			 * This provides file type and permission
542			 * information necessary to support full
543			 * streaming extraction.  Currently being
544			 * discussed with other Zip developers
545			 * ... subject to change.
546			 *
547			 * Format:
548			 *  The field starts with a bitmap that specifies
549			 *  which additional fields are included.  The
550			 *  bitmap is variable length and can be extended in
551			 *  the future.
552			 *
553			 *  n bytes - feature bitmap: first byte has low-order
554			 *    7 bits.  If high-order bit is set, a subsequent
555			 *    byte holds the next 7 bits, etc.
556			 *
557			 *  if bitmap & 1, 2 byte "version made by"
558			 *  if bitmap & 2, 2 byte "internal file attributes"
559			 *  if bitmap & 4, 4 byte "external file attributes"
560			 *  if bitmap & 8, 2 byte comment length + n byte comment
561			 */
562			int bitmap, bitmap_last;
563
564			if (datasize < 1)
565				break;
566			bitmap_last = bitmap = 0xff & p[offset];
567			offset += 1;
568			datasize -= 1;
569
570			/* We only support first 7 bits of bitmap; skip rest. */
571			while ((bitmap_last & 0x80) != 0
572			    && datasize >= 1) {
573				bitmap_last = p[offset];
574				offset += 1;
575				datasize -= 1;
576			}
577
578			if (bitmap & 1) {
579				/* 2 byte "version made by" */
580				if (datasize < 2)
581					break;
582				zip_entry->system
583				    = archive_le16dec(p + offset) >> 8;
584				offset += 2;
585				datasize -= 2;
586			}
587			if (bitmap & 2) {
588				/* 2 byte "internal file attributes" */
589				uint32_t internal_attributes;
590				if (datasize < 2)
591					break;
592				internal_attributes
593				    = archive_le16dec(p + offset);
594				/* Not used by libarchive at present. */
595				(void)internal_attributes; /* UNUSED */
596				offset += 2;
597				datasize -= 2;
598			}
599			if (bitmap & 4) {
600				/* 4 byte "external file attributes" */
601				uint32_t external_attributes;
602				if (datasize < 4)
603					break;
604				external_attributes
605				    = archive_le32dec(p + offset);
606				if (zip_entry->system == 3) {
607					zip_entry->mode
608					    = external_attributes >> 16;
609				} else if (zip_entry->system == 0) {
610					// Interpret MSDOS directory bit
611					if (0x10 == (external_attributes & 0x10)) {
612						zip_entry->mode = AE_IFDIR | 0775;
613					} else {
614						zip_entry->mode = AE_IFREG | 0664;
615					}
616					if (0x01 == (external_attributes & 0x01)) {
617						// Read-only bit; strip write permissions
618						zip_entry->mode &= 0555;
619					}
620				} else {
621					zip_entry->mode = 0;
622				}
623				offset += 4;
624				datasize -= 4;
625			}
626			if (bitmap & 8) {
627				/* 2 byte comment length + comment */
628				uint32_t comment_length;
629				if (datasize < 2)
630					break;
631				comment_length
632				    = archive_le16dec(p + offset);
633				offset += 2;
634				datasize -= 2;
635
636				if (datasize < comment_length)
637					break;
638				/* Comment is not supported by libarchive */
639				offset += comment_length;
640				datasize -= comment_length;
641			}
642			break;
643		}
644		case 0x7855:
645			/* Info-ZIP Unix Extra Field (type 2) "Ux". */
646#ifdef DEBUG
647			fprintf(stderr, "uid %d gid %d\n",
648			    archive_le16dec(p + offset),
649			    archive_le16dec(p + offset + 2));
650#endif
651			if (datasize >= 2)
652				zip_entry->uid = archive_le16dec(p + offset);
653			if (datasize >= 4)
654				zip_entry->gid =
655				    archive_le16dec(p + offset + 2);
656			break;
657		case 0x7875:
658		{
659			/* Info-Zip Unix Extra Field (type 3) "ux". */
660			int uidsize = 0, gidsize = 0;
661
662			/* TODO: support arbitrary uidsize/gidsize. */
663			if (datasize >= 1 && p[offset] == 1) {/* version=1 */
664				if (datasize >= 4) {
665					/* get a uid size. */
666					uidsize = 0xff & (int)p[offset+1];
667					if (uidsize == 2)
668						zip_entry->uid =
669						    archive_le16dec(
670						        p + offset + 2);
671					else if (uidsize == 4 && datasize >= 6)
672						zip_entry->uid =
673						    archive_le32dec(
674						        p + offset + 2);
675				}
676				if (datasize >= (2 + uidsize + 3)) {
677					/* get a gid size. */
678					gidsize = 0xff & (int)p[offset+2+uidsize];
679					if (gidsize == 2)
680						zip_entry->gid =
681						    archive_le16dec(
682						        p+offset+2+uidsize+1);
683					else if (gidsize == 4 &&
684					    datasize >= (2 + uidsize + 5))
685						zip_entry->gid =
686						    archive_le32dec(
687						        p+offset+2+uidsize+1);
688				}
689			}
690			break;
691		}
692		case 0x9901:
693			/* WinZIp AES extra data field. */
694			if (p[offset + 2] == 'A' && p[offset + 3] == 'E') {
695				/* Vendor version. */
696				zip_entry->aes_extra.vendor =
697				    archive_le16dec(p + offset);
698				/* AES encryption strength. */
699				zip_entry->aes_extra.strength = p[offset + 4];
700				/* Actual compression method. */
701				zip_entry->aes_extra.compression =
702				    p[offset + 5];
703			}
704			break;
705		default:
706			break;
707		}
708		offset += datasize;
709	}
710#ifdef DEBUG
711	if (offset != extra_length)
712	{
713		fprintf(stderr,
714		    "Extra data field contents do not match reported size!\n");
715	}
716#endif
717}
718
719/*
720 * Assumes file pointer is at beginning of local file header.
721 */
722static int
723zip_read_local_file_header(struct archive_read *a, struct archive_entry *entry,
724    struct zip *zip)
725{
726	const char *p;
727	const void *h;
728	const wchar_t *wp;
729	const char *cp;
730	size_t len, filename_length, extra_length;
731	struct archive_string_conv *sconv;
732	struct zip_entry *zip_entry = zip->entry;
733	struct zip_entry zip_entry_central_dir;
734	int ret = ARCHIVE_OK;
735	char version;
736
737	/* Save a copy of the original for consistency checks. */
738	zip_entry_central_dir = *zip_entry;
739
740	zip->decompress_init = 0;
741	zip->end_of_entry = 0;
742	zip->entry_uncompressed_bytes_read = 0;
743	zip->entry_compressed_bytes_read = 0;
744	zip->entry_crc32 = zip->crc32func(0, NULL, 0);
745
746	/* Setup default conversion. */
747	if (zip->sconv == NULL && !zip->init_default_conversion) {
748		zip->sconv_default =
749		    archive_string_default_conversion_for_read(&(a->archive));
750		zip->init_default_conversion = 1;
751	}
752
753	if ((p = __archive_read_ahead(a, 30, NULL)) == NULL) {
754		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
755		    "Truncated ZIP file header");
756		return (ARCHIVE_FATAL);
757	}
758
759	if (memcmp(p, "PK\003\004", 4) != 0) {
760		archive_set_error(&a->archive, -1, "Damaged Zip archive");
761		return ARCHIVE_FATAL;
762	}
763	version = p[4];
764	zip_entry->system = p[5];
765	zip_entry->zip_flags = archive_le16dec(p + 6);
766	if (zip_entry->zip_flags & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)) {
767		zip->has_encrypted_entries = 1;
768		archive_entry_set_is_data_encrypted(entry, 1);
769		if (zip_entry->zip_flags & ZIP_CENTRAL_DIRECTORY_ENCRYPTED &&
770			zip_entry->zip_flags & ZIP_ENCRYPTED &&
771			zip_entry->zip_flags & ZIP_STRONG_ENCRYPTED) {
772			archive_entry_set_is_metadata_encrypted(entry, 1);
773			return ARCHIVE_FATAL;
774		}
775	}
776	zip->init_decryption = (zip_entry->zip_flags & ZIP_ENCRYPTED);
777	zip_entry->compression = (char)archive_le16dec(p + 8);
778	zip_entry->mtime = zip_time(p + 10);
779	zip_entry->crc32 = archive_le32dec(p + 14);
780	if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
781		zip_entry->decdat = p[11];
782	else
783		zip_entry->decdat = p[17];
784	zip_entry->compressed_size = archive_le32dec(p + 18);
785	zip_entry->uncompressed_size = archive_le32dec(p + 22);
786	filename_length = archive_le16dec(p + 26);
787	extra_length = archive_le16dec(p + 28);
788
789	__archive_read_consume(a, 30);
790
791	/* Read the filename. */
792	if ((h = __archive_read_ahead(a, filename_length, NULL)) == NULL) {
793		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
794		    "Truncated ZIP file header");
795		return (ARCHIVE_FATAL);
796	}
797	if (zip_entry->zip_flags & ZIP_UTF8_NAME) {
798		/* The filename is stored to be UTF-8. */
799		if (zip->sconv_utf8 == NULL) {
800			zip->sconv_utf8 =
801			    archive_string_conversion_from_charset(
802				&a->archive, "UTF-8", 1);
803			if (zip->sconv_utf8 == NULL)
804				return (ARCHIVE_FATAL);
805		}
806		sconv = zip->sconv_utf8;
807	} else if (zip->sconv != NULL)
808		sconv = zip->sconv;
809	else
810		sconv = zip->sconv_default;
811
812	if (archive_entry_copy_pathname_l(entry,
813	    h, filename_length, sconv) != 0) {
814		if (errno == ENOMEM) {
815			archive_set_error(&a->archive, ENOMEM,
816			    "Can't allocate memory for Pathname");
817			return (ARCHIVE_FATAL);
818		}
819		archive_set_error(&a->archive,
820		    ARCHIVE_ERRNO_FILE_FORMAT,
821		    "Pathname cannot be converted "
822		    "from %s to current locale.",
823		    archive_string_conversion_charset_name(sconv));
824		ret = ARCHIVE_WARN;
825	}
826	__archive_read_consume(a, filename_length);
827
828	/* Read the extra data. */
829	if ((h = __archive_read_ahead(a, extra_length, NULL)) == NULL) {
830		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
831		    "Truncated ZIP file header");
832		return (ARCHIVE_FATAL);
833	}
834
835	process_extra(h, extra_length, zip_entry);
836	__archive_read_consume(a, extra_length);
837
838	/* Work around a bug in Info-Zip: When reading from a pipe, it
839	 * stats the pipe instead of synthesizing a file entry. */
840	if ((zip_entry->mode & AE_IFMT) == AE_IFIFO) {
841		zip_entry->mode &= ~ AE_IFMT;
842		zip_entry->mode |= AE_IFREG;
843	}
844
845	if ((zip_entry->mode & AE_IFMT) == 0) {
846		/* Especially in streaming mode, we can end up
847		   here without having seen proper mode information.
848		   Guess from the filename. */
849		wp = archive_entry_pathname_w(entry);
850		if (wp != NULL) {
851			len = wcslen(wp);
852			if (len > 0 && wp[len - 1] == L'/')
853				zip_entry->mode |= AE_IFDIR;
854			else
855				zip_entry->mode |= AE_IFREG;
856		} else {
857			cp = archive_entry_pathname(entry);
858			len = (cp != NULL)?strlen(cp):0;
859			if (len > 0 && cp[len - 1] == '/')
860				zip_entry->mode |= AE_IFDIR;
861			else
862				zip_entry->mode |= AE_IFREG;
863		}
864		if (zip_entry->mode == AE_IFDIR) {
865			zip_entry->mode |= 0775;
866		} else if (zip_entry->mode == AE_IFREG) {
867			zip_entry->mode |= 0664;
868		}
869	}
870
871	/* Make sure directories end in '/' */
872	if ((zip_entry->mode & AE_IFMT) == AE_IFDIR) {
873		wp = archive_entry_pathname_w(entry);
874		if (wp != NULL) {
875			len = wcslen(wp);
876			if (len > 0 && wp[len - 1] != L'/') {
877				struct archive_wstring s;
878				archive_string_init(&s);
879				archive_wstrcat(&s, wp);
880				archive_wstrappend_wchar(&s, L'/');
881				archive_entry_copy_pathname_w(entry, s.s);
882			}
883		} else {
884			cp = archive_entry_pathname(entry);
885			len = (cp != NULL)?strlen(cp):0;
886			if (len > 0 && cp[len - 1] != '/') {
887				struct archive_string s;
888				archive_string_init(&s);
889				archive_strcat(&s, cp);
890				archive_strappend_char(&s, '/');
891				archive_entry_set_pathname(entry, s.s);
892			}
893		}
894	}
895
896	if (zip_entry->flags & LA_FROM_CENTRAL_DIRECTORY) {
897		/* If this came from the central dir, it's size info
898		 * is definitive, so ignore the length-at-end flag. */
899		zip_entry->zip_flags &= ~ZIP_LENGTH_AT_END;
900		/* If local header is missing a value, use the one from
901		   the central directory.  If both have it, warn about
902		   mismatches. */
903		if (zip_entry->crc32 == 0) {
904			zip_entry->crc32 = zip_entry_central_dir.crc32;
905		} else if (!zip->ignore_crc32
906		    && zip_entry->crc32 != zip_entry_central_dir.crc32) {
907			archive_set_error(&a->archive,
908			    ARCHIVE_ERRNO_FILE_FORMAT,
909			    "Inconsistent CRC32 values");
910			ret = ARCHIVE_WARN;
911		}
912		if (zip_entry->compressed_size == 0) {
913			zip_entry->compressed_size
914			    = zip_entry_central_dir.compressed_size;
915		} else if (zip_entry->compressed_size
916		    != zip_entry_central_dir.compressed_size) {
917			archive_set_error(&a->archive,
918			    ARCHIVE_ERRNO_FILE_FORMAT,
919			    "Inconsistent compressed size: "
920			    "%jd in central directory, %jd in local header",
921			    (intmax_t)zip_entry_central_dir.compressed_size,
922			    (intmax_t)zip_entry->compressed_size);
923			ret = ARCHIVE_WARN;
924		}
925		if (zip_entry->uncompressed_size == 0) {
926			zip_entry->uncompressed_size
927			    = zip_entry_central_dir.uncompressed_size;
928		} else if (zip_entry->uncompressed_size
929		    != zip_entry_central_dir.uncompressed_size) {
930			archive_set_error(&a->archive,
931			    ARCHIVE_ERRNO_FILE_FORMAT,
932			    "Inconsistent uncompressed size: "
933			    "%jd in central directory, %jd in local header",
934			    (intmax_t)zip_entry_central_dir.uncompressed_size,
935			    (intmax_t)zip_entry->uncompressed_size);
936			ret = ARCHIVE_WARN;
937		}
938	}
939
940	/* Populate some additional entry fields: */
941	archive_entry_set_mode(entry, zip_entry->mode);
942	archive_entry_set_uid(entry, zip_entry->uid);
943	archive_entry_set_gid(entry, zip_entry->gid);
944	archive_entry_set_mtime(entry, zip_entry->mtime, 0);
945	archive_entry_set_ctime(entry, zip_entry->ctime, 0);
946	archive_entry_set_atime(entry, zip_entry->atime, 0);
947
948	if ((zip->entry->mode & AE_IFMT) == AE_IFLNK) {
949		size_t linkname_length;
950
951		if (zip_entry->compressed_size > 64 * 1024) {
952			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
953			    "Zip file with oversized link entry");
954			return ARCHIVE_FATAL;
955		}
956
957		linkname_length = (size_t)zip_entry->compressed_size;
958
959		archive_entry_set_size(entry, 0);
960		p = __archive_read_ahead(a, linkname_length, NULL);
961		if (p == NULL) {
962			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
963			    "Truncated Zip file");
964			return ARCHIVE_FATAL;
965		}
966
967		sconv = zip->sconv;
968		if (sconv == NULL && (zip->entry->zip_flags & ZIP_UTF8_NAME))
969			sconv = zip->sconv_utf8;
970		if (sconv == NULL)
971			sconv = zip->sconv_default;
972		if (archive_entry_copy_symlink_l(entry, p, linkname_length,
973		    sconv) != 0) {
974			if (errno != ENOMEM && sconv == zip->sconv_utf8 &&
975			    (zip->entry->zip_flags & ZIP_UTF8_NAME))
976			    archive_entry_copy_symlink_l(entry, p,
977				linkname_length, NULL);
978			if (errno == ENOMEM) {
979				archive_set_error(&a->archive, ENOMEM,
980				    "Can't allocate memory for Symlink");
981				return (ARCHIVE_FATAL);
982			}
983			/*
984			 * Since there is no character-set regulation for
985			 * symlink name, do not report the conversion error
986			 * in an automatic conversion.
987			 */
988			if (sconv != zip->sconv_utf8 ||
989			    (zip->entry->zip_flags & ZIP_UTF8_NAME) == 0) {
990				archive_set_error(&a->archive,
991				    ARCHIVE_ERRNO_FILE_FORMAT,
992				    "Symlink cannot be converted "
993				    "from %s to current locale.",
994				    archive_string_conversion_charset_name(
995					sconv));
996				ret = ARCHIVE_WARN;
997			}
998		}
999		zip_entry->uncompressed_size = zip_entry->compressed_size = 0;
1000
1001		if (__archive_read_consume(a, linkname_length) < 0) {
1002			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1003			    "Read error skipping symlink target name");
1004			return ARCHIVE_FATAL;
1005		}
1006	} else if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
1007	    || zip_entry->uncompressed_size > 0) {
1008		/* Set the size only if it's meaningful. */
1009		archive_entry_set_size(entry, zip_entry->uncompressed_size);
1010	}
1011	zip->entry_bytes_remaining = zip_entry->compressed_size;
1012
1013	/* If there's no body, force read_data() to return EOF immediately. */
1014	if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
1015	    && zip->entry_bytes_remaining < 1)
1016		zip->end_of_entry = 1;
1017
1018	/* Set up a more descriptive format name. */
1019	archive_string_sprintf(&zip->format_name, "ZIP %d.%d (%s)",
1020	    version / 10, version % 10,
1021	    compression_name(zip->entry->compression));
1022	a->archive.archive_format_name = zip->format_name.s;
1023
1024	return (ret);
1025}
1026
1027static int
1028check_authentication_code(struct archive_read *a, const void *_p)
1029{
1030	struct zip *zip = (struct zip *)(a->format->data);
1031
1032	/* Check authentication code. */
1033	if (zip->hctx_valid) {
1034		const void *p;
1035		uint8_t hmac[20];
1036		size_t hmac_len = 20;
1037		int cmp;
1038
1039		archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
1040		if (_p == NULL) {
1041			/* Read authentication code. */
1042			p = __archive_read_ahead(a, AUTH_CODE_SIZE, NULL);
1043			if (p == NULL) {
1044				archive_set_error(&a->archive,
1045				    ARCHIVE_ERRNO_FILE_FORMAT,
1046				    "Truncated ZIP file data");
1047				return (ARCHIVE_FATAL);
1048			}
1049		} else {
1050			p = _p;
1051		}
1052		cmp = memcmp(hmac, p, AUTH_CODE_SIZE);
1053		__archive_read_consume(a, AUTH_CODE_SIZE);
1054		if (cmp != 0) {
1055			archive_set_error(&a->archive,
1056			    ARCHIVE_ERRNO_MISC,
1057			    "ZIP bad Authentication code");
1058			return (ARCHIVE_WARN);
1059		}
1060	}
1061	return (ARCHIVE_OK);
1062}
1063
1064/*
1065 * Read "uncompressed" data.  There are three cases:
1066 *  1) We know the size of the data.  This is always true for the
1067 * seeking reader (we've examined the Central Directory already).
1068 *  2) ZIP_LENGTH_AT_END was set, but only the CRC was deferred.
1069 * Info-ZIP seems to do this; we know the size but have to grab
1070 * the CRC from the data descriptor afterwards.
1071 *  3) We're streaming and ZIP_LENGTH_AT_END was specified and
1072 * we have no size information.  In this case, we can do pretty
1073 * well by watching for the data descriptor record.  The data
1074 * descriptor is 16 bytes and includes a computed CRC that should
1075 * provide a strong check.
1076 *
1077 * TODO: Technically, the PK\007\010 signature is optional.
1078 * In the original spec, the data descriptor contained CRC
1079 * and size fields but had no leading signature.  In practice,
1080 * newer writers seem to provide the signature pretty consistently.
1081 *
1082 * For uncompressed data, the PK\007\010 marker seems essential
1083 * to be sure we've actually seen the end of the entry.
1084 *
1085 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
1086 * zip->end_of_entry if it consumes all of the data.
1087 */
1088static int
1089zip_read_data_none(struct archive_read *a, const void **_buff,
1090    size_t *size, int64_t *offset)
1091{
1092	struct zip *zip;
1093	const char *buff;
1094	ssize_t bytes_avail;
1095	int r;
1096
1097	(void)offset; /* UNUSED */
1098
1099	zip = (struct zip *)(a->format->data);
1100
1101	if (zip->entry->zip_flags & ZIP_LENGTH_AT_END) {
1102		const char *p;
1103		ssize_t grabbing_bytes = 24;
1104
1105		if (zip->hctx_valid)
1106			grabbing_bytes += AUTH_CODE_SIZE;
1107		/* Grab at least 24 bytes. */
1108		buff = __archive_read_ahead(a, grabbing_bytes, &bytes_avail);
1109		if (bytes_avail < grabbing_bytes) {
1110			/* Zip archives have end-of-archive markers
1111			   that are longer than this, so a failure to get at
1112			   least 24 bytes really does indicate a truncated
1113			   file. */
1114			archive_set_error(&a->archive,
1115			    ARCHIVE_ERRNO_FILE_FORMAT,
1116			    "Truncated ZIP file data");
1117			return (ARCHIVE_FATAL);
1118		}
1119		/* Check for a complete PK\007\010 signature, followed
1120		 * by the correct 4-byte CRC. */
1121		p = buff;
1122		if (zip->hctx_valid)
1123			p += AUTH_CODE_SIZE;
1124		if (p[0] == 'P' && p[1] == 'K'
1125		    && p[2] == '\007' && p[3] == '\010'
1126		    && (archive_le32dec(p + 4) == zip->entry_crc32
1127			|| zip->ignore_crc32
1128			|| (zip->hctx_valid
1129			 && zip->entry->aes_extra.vendor == AES_VENDOR_AE_2))) {
1130			if (zip->entry->flags & LA_USED_ZIP64) {
1131				zip->entry->crc32 = archive_le32dec(p + 4);
1132				zip->entry->compressed_size =
1133					archive_le64dec(p + 8);
1134				zip->entry->uncompressed_size =
1135					archive_le64dec(p + 16);
1136				zip->unconsumed = 24;
1137			} else {
1138				zip->entry->crc32 = archive_le32dec(p + 4);
1139				zip->entry->compressed_size =
1140					archive_le32dec(p + 8);
1141				zip->entry->uncompressed_size =
1142					archive_le32dec(p + 12);
1143				zip->unconsumed = 16;
1144			}
1145			if (zip->hctx_valid) {
1146				r = check_authentication_code(a, buff);
1147				if (r != ARCHIVE_OK)
1148					return (r);
1149			}
1150			zip->end_of_entry = 1;
1151			return (ARCHIVE_OK);
1152		}
1153		/* If not at EOF, ensure we consume at least one byte. */
1154		++p;
1155
1156		/* Scan forward until we see where a PK\007\010 signature
1157		 * might be. */
1158		/* Return bytes up until that point.  On the next call,
1159		 * the code above will verify the data descriptor. */
1160		while (p < buff + bytes_avail - 4) {
1161			if (p[3] == 'P') { p += 3; }
1162			else if (p[3] == 'K') { p += 2; }
1163			else if (p[3] == '\007') { p += 1; }
1164			else if (p[3] == '\010' && p[2] == '\007'
1165			    && p[1] == 'K' && p[0] == 'P') {
1166				if (zip->hctx_valid)
1167					p -= AUTH_CODE_SIZE;
1168				break;
1169			} else { p += 4; }
1170		}
1171		bytes_avail = p - buff;
1172	} else {
1173		if (zip->entry_bytes_remaining == 0) {
1174			zip->end_of_entry = 1;
1175			if (zip->hctx_valid) {
1176				r = check_authentication_code(a, NULL);
1177				if (r != ARCHIVE_OK)
1178					return (r);
1179			}
1180			return (ARCHIVE_OK);
1181		}
1182		/* Grab a bunch of bytes. */
1183		buff = __archive_read_ahead(a, 1, &bytes_avail);
1184		if (bytes_avail <= 0) {
1185			archive_set_error(&a->archive,
1186			    ARCHIVE_ERRNO_FILE_FORMAT,
1187			    "Truncated ZIP file data");
1188			return (ARCHIVE_FATAL);
1189		}
1190		if (bytes_avail > zip->entry_bytes_remaining)
1191			bytes_avail = (ssize_t)zip->entry_bytes_remaining;
1192	}
1193	if (zip->tctx_valid || zip->cctx_valid) {
1194		size_t dec_size = bytes_avail;
1195
1196		if (dec_size > zip->decrypted_buffer_size)
1197			dec_size = zip->decrypted_buffer_size;
1198		if (zip->tctx_valid) {
1199			trad_enc_decrypt_update(&zip->tctx,
1200			    (const uint8_t *)buff, dec_size,
1201			    zip->decrypted_buffer, dec_size);
1202		} else {
1203			size_t dsize = dec_size;
1204			archive_hmac_sha1_update(&zip->hctx,
1205			    (const uint8_t *)buff, dec_size);
1206			archive_decrypto_aes_ctr_update(&zip->cctx,
1207			    (const uint8_t *)buff, dec_size,
1208			    zip->decrypted_buffer, &dsize);
1209		}
1210		bytes_avail = dec_size;
1211		buff = (const char *)zip->decrypted_buffer;
1212	}
1213	*size = bytes_avail;
1214	zip->entry_bytes_remaining -= bytes_avail;
1215	zip->entry_uncompressed_bytes_read += bytes_avail;
1216	zip->entry_compressed_bytes_read += bytes_avail;
1217	zip->unconsumed += bytes_avail;
1218	*_buff = buff;
1219	return (ARCHIVE_OK);
1220}
1221
1222#ifdef HAVE_ZLIB_H
1223static int
1224zip_deflate_init(struct archive_read *a, struct zip *zip)
1225{
1226	int r;
1227
1228	/* If we haven't yet read any data, initialize the decompressor. */
1229	if (!zip->decompress_init) {
1230		if (zip->stream_valid)
1231			r = inflateReset(&zip->stream);
1232		else
1233			r = inflateInit2(&zip->stream,
1234			    -15 /* Don't check for zlib header */);
1235		if (r != Z_OK) {
1236			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1237			    "Can't initialize ZIP decompression.");
1238			return (ARCHIVE_FATAL);
1239		}
1240		/* Stream structure has been set up. */
1241		zip->stream_valid = 1;
1242		/* We've initialized decompression for this stream. */
1243		zip->decompress_init = 1;
1244	}
1245	return (ARCHIVE_OK);
1246}
1247
1248static int
1249zip_read_data_deflate(struct archive_read *a, const void **buff,
1250    size_t *size, int64_t *offset)
1251{
1252	struct zip *zip;
1253	ssize_t bytes_avail;
1254	const void *compressed_buff, *sp;
1255	int r;
1256
1257	(void)offset; /* UNUSED */
1258
1259	zip = (struct zip *)(a->format->data);
1260
1261	/* If the buffer hasn't been allocated, allocate it now. */
1262	if (zip->uncompressed_buffer == NULL) {
1263		zip->uncompressed_buffer_size = 256 * 1024;
1264		zip->uncompressed_buffer
1265		    = (unsigned char *)malloc(zip->uncompressed_buffer_size);
1266		if (zip->uncompressed_buffer == NULL) {
1267			archive_set_error(&a->archive, ENOMEM,
1268			    "No memory for ZIP decompression");
1269			return (ARCHIVE_FATAL);
1270		}
1271	}
1272
1273	r = zip_deflate_init(a, zip);
1274	if (r != ARCHIVE_OK)
1275		return (r);
1276
1277	/*
1278	 * Note: '1' here is a performance optimization.
1279	 * Recall that the decompression layer returns a count of
1280	 * available bytes; asking for more than that forces the
1281	 * decompressor to combine reads by copying data.
1282	 */
1283	compressed_buff = sp = __archive_read_ahead(a, 1, &bytes_avail);
1284	if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
1285	    && bytes_avail > zip->entry_bytes_remaining) {
1286		bytes_avail = (ssize_t)zip->entry_bytes_remaining;
1287	}
1288	if (bytes_avail <= 0) {
1289		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1290		    "Truncated ZIP file body");
1291		return (ARCHIVE_FATAL);
1292	}
1293
1294	if (zip->tctx_valid || zip->cctx_valid) {
1295		if (zip->decrypted_bytes_remaining < (size_t)bytes_avail) {
1296			size_t buff_remaining = zip->decrypted_buffer_size
1297			    - (zip->decrypted_ptr - zip->decrypted_buffer);
1298
1299			if (buff_remaining > (size_t)bytes_avail)
1300				buff_remaining = (size_t)bytes_avail;
1301
1302			if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) &&
1303			      zip->entry_bytes_remaining > 0) {
1304				if ((int64_t)(zip->decrypted_bytes_remaining
1305				    + buff_remaining)
1306				      > zip->entry_bytes_remaining) {
1307					if (zip->entry_bytes_remaining <
1308					      (int64_t)zip->decrypted_bytes_remaining)
1309						buff_remaining = 0;
1310					else
1311						buff_remaining =
1312						    (size_t)zip->entry_bytes_remaining
1313						      - zip->decrypted_bytes_remaining;
1314				}
1315			}
1316			if (buff_remaining > 0) {
1317				if (zip->tctx_valid) {
1318					trad_enc_decrypt_update(&zip->tctx,
1319					    compressed_buff, buff_remaining,
1320					    zip->decrypted_ptr
1321					      + zip->decrypted_bytes_remaining,
1322					    buff_remaining);
1323				} else {
1324					size_t dsize = buff_remaining;
1325					archive_decrypto_aes_ctr_update(
1326					    &zip->cctx,
1327					    compressed_buff, buff_remaining,
1328					    zip->decrypted_ptr
1329					      + zip->decrypted_bytes_remaining,
1330					    &dsize);
1331				}
1332				zip->decrypted_bytes_remaining += buff_remaining;
1333			}
1334		}
1335		bytes_avail = zip->decrypted_bytes_remaining;
1336		compressed_buff = (const char *)zip->decrypted_ptr;
1337	}
1338
1339	/*
1340	 * A bug in zlib.h: stream.next_in should be marked 'const'
1341	 * but isn't (the library never alters data through the
1342	 * next_in pointer, only reads it).  The result: this ugly
1343	 * cast to remove 'const'.
1344	 */
1345	zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
1346	zip->stream.avail_in = (uInt)bytes_avail;
1347	zip->stream.total_in = 0;
1348	zip->stream.next_out = zip->uncompressed_buffer;
1349	zip->stream.avail_out = (uInt)zip->uncompressed_buffer_size;
1350	zip->stream.total_out = 0;
1351
1352	r = inflate(&zip->stream, 0);
1353	switch (r) {
1354	case Z_OK:
1355		break;
1356	case Z_STREAM_END:
1357		zip->end_of_entry = 1;
1358		break;
1359	case Z_MEM_ERROR:
1360		archive_set_error(&a->archive, ENOMEM,
1361		    "Out of memory for ZIP decompression");
1362		return (ARCHIVE_FATAL);
1363	default:
1364		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1365		    "ZIP decompression failed (%d)", r);
1366		return (ARCHIVE_FATAL);
1367	}
1368
1369	/* Consume as much as the compressor actually used. */
1370	bytes_avail = zip->stream.total_in;
1371	if (zip->tctx_valid || zip->cctx_valid) {
1372		zip->decrypted_bytes_remaining -= bytes_avail;
1373		if (zip->decrypted_bytes_remaining == 0)
1374			zip->decrypted_ptr = zip->decrypted_buffer;
1375		else
1376			zip->decrypted_ptr += bytes_avail;
1377	}
1378	/* Calculate compressed data as much as we used.*/
1379	if (zip->hctx_valid)
1380		archive_hmac_sha1_update(&zip->hctx, sp, bytes_avail);
1381	__archive_read_consume(a, bytes_avail);
1382	zip->entry_bytes_remaining -= bytes_avail;
1383	zip->entry_compressed_bytes_read += bytes_avail;
1384
1385	*size = zip->stream.total_out;
1386	zip->entry_uncompressed_bytes_read += zip->stream.total_out;
1387	*buff = zip->uncompressed_buffer;
1388
1389	if (zip->end_of_entry && zip->hctx_valid) {
1390		r = check_authentication_code(a, NULL);
1391		if (r != ARCHIVE_OK)
1392			return (r);
1393	}
1394
1395	if (zip->end_of_entry && (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) {
1396		const char *p;
1397
1398		if (NULL == (p = __archive_read_ahead(a, 24, NULL))) {
1399			archive_set_error(&a->archive,
1400			    ARCHIVE_ERRNO_FILE_FORMAT,
1401			    "Truncated ZIP end-of-file record");
1402			return (ARCHIVE_FATAL);
1403		}
1404		/* Consume the optional PK\007\010 marker. */
1405		if (p[0] == 'P' && p[1] == 'K' &&
1406		    p[2] == '\007' && p[3] == '\010') {
1407			p += 4;
1408			zip->unconsumed = 4;
1409		}
1410		if (zip->entry->flags & LA_USED_ZIP64) {
1411			zip->entry->crc32 = archive_le32dec(p);
1412			zip->entry->compressed_size = archive_le64dec(p + 4);
1413			zip->entry->uncompressed_size = archive_le64dec(p + 12);
1414			zip->unconsumed += 20;
1415		} else {
1416			zip->entry->crc32 = archive_le32dec(p);
1417			zip->entry->compressed_size = archive_le32dec(p + 4);
1418			zip->entry->uncompressed_size = archive_le32dec(p + 8);
1419			zip->unconsumed += 12;
1420		}
1421	}
1422
1423	return (ARCHIVE_OK);
1424}
1425#endif
1426
1427static int
1428read_decryption_header(struct archive_read *a)
1429{
1430	struct zip *zip = (struct zip *)(a->format->data);
1431	const char *p;
1432	unsigned int remaining_size;
1433	unsigned int ts;
1434
1435	/*
1436	 * Read an initialization vector data field.
1437	 */
1438	p = __archive_read_ahead(a, 2, NULL);
1439	if (p == NULL)
1440		goto truncated;
1441	ts = zip->iv_size;
1442	zip->iv_size = archive_le16dec(p);
1443	__archive_read_consume(a, 2);
1444	if (ts < zip->iv_size) {
1445		free(zip->iv);
1446		zip->iv = NULL;
1447	}
1448	p = __archive_read_ahead(a, zip->iv_size, NULL);
1449	if (p == NULL)
1450		goto truncated;
1451	if (zip->iv == NULL) {
1452		zip->iv = malloc(zip->iv_size);
1453		if (zip->iv == NULL)
1454			goto nomem;
1455	}
1456	memcpy(zip->iv, p, zip->iv_size);
1457	__archive_read_consume(a, zip->iv_size);
1458
1459	/*
1460	 * Read a size of remaining decryption header field.
1461	 */
1462	p = __archive_read_ahead(a, 14, NULL);
1463	if (p == NULL)
1464		goto truncated;
1465	remaining_size = archive_le32dec(p);
1466	if (remaining_size < 16 || remaining_size > (1 << 18))
1467		goto corrupted;
1468
1469	/* Check if format version is supported. */
1470	if (archive_le16dec(p+4) != 3) {
1471		archive_set_error(&a->archive,
1472		    ARCHIVE_ERRNO_FILE_FORMAT,
1473		    "Unsupported encryption format version: %u",
1474		    archive_le16dec(p+4));
1475		return (ARCHIVE_FAILED);
1476	}
1477
1478	/*
1479	 * Read an encryption algorithm field.
1480	 */
1481	zip->alg_id = archive_le16dec(p+6);
1482	switch (zip->alg_id) {
1483	case 0x6601:/* DES */
1484	case 0x6602:/* RC2 */
1485	case 0x6603:/* 3DES 168 */
1486	case 0x6609:/* 3DES 112 */
1487	case 0x660E:/* AES 128 */
1488	case 0x660F:/* AES 192 */
1489	case 0x6610:/* AES 256 */
1490	case 0x6702:/* RC2 (version >= 5.2) */
1491	case 0x6720:/* Blowfish */
1492	case 0x6721:/* Twofish */
1493	case 0x6801:/* RC4 */
1494		/* Suuported encryption algorithm. */
1495		break;
1496	default:
1497		archive_set_error(&a->archive,
1498		    ARCHIVE_ERRNO_FILE_FORMAT,
1499		    "Unknown encryption algorithm: %u", zip->alg_id);
1500		return (ARCHIVE_FAILED);
1501	}
1502
1503	/*
1504	 * Read a bit length field.
1505	 */
1506	zip->bit_len = archive_le16dec(p+8);
1507
1508	/*
1509	 * Read a flags field.
1510	 */
1511	zip->flags = archive_le16dec(p+10);
1512	switch (zip->flags & 0xf000) {
1513	case 0x0001: /* Password is required to decrypt. */
1514	case 0x0002: /* Certificates only. */
1515	case 0x0003: /* Password or certificate required to decrypt. */
1516		break;
1517	default:
1518		archive_set_error(&a->archive,
1519		    ARCHIVE_ERRNO_FILE_FORMAT,
1520		    "Unknown encryption flag: %u", zip->flags);
1521		return (ARCHIVE_FAILED);
1522	}
1523	if ((zip->flags & 0xf000) == 0 ||
1524	    (zip->flags & 0xf000) == 0x4000) {
1525		archive_set_error(&a->archive,
1526		    ARCHIVE_ERRNO_FILE_FORMAT,
1527		    "Unknown encryption flag: %u", zip->flags);
1528		return (ARCHIVE_FAILED);
1529	}
1530
1531	/*
1532	 * Read an encrypted random data field.
1533	 */
1534	ts = zip->erd_size;
1535	zip->erd_size = archive_le16dec(p+12);
1536	__archive_read_consume(a, 14);
1537	if ((zip->erd_size & 0xf) != 0 ||
1538	    (zip->erd_size + 16) > remaining_size ||
1539	    (zip->erd_size + 16) < zip->erd_size)
1540		goto corrupted;
1541
1542	if (ts < zip->erd_size) {
1543		free(zip->erd);
1544		zip->erd = NULL;
1545	}
1546	p = __archive_read_ahead(a, zip->erd_size, NULL);
1547	if (p == NULL)
1548		goto truncated;
1549	if (zip->erd == NULL) {
1550		zip->erd = malloc(zip->erd_size);
1551		if (zip->erd == NULL)
1552			goto nomem;
1553	}
1554	memcpy(zip->erd, p, zip->erd_size);
1555	__archive_read_consume(a, zip->erd_size);
1556
1557	/*
1558	 * Read a reserved data field.
1559	 */
1560	p = __archive_read_ahead(a, 4, NULL);
1561	if (p == NULL)
1562		goto truncated;
1563	/* Reserved data size should be zero. */
1564	if (archive_le32dec(p) != 0)
1565		goto corrupted;
1566	__archive_read_consume(a, 4);
1567
1568	/*
1569	 * Read a password validation data field.
1570	 */
1571	p = __archive_read_ahead(a, 2, NULL);
1572	if (p == NULL)
1573		goto truncated;
1574	ts = zip->v_size;
1575	zip->v_size = archive_le16dec(p);
1576	__archive_read_consume(a, 2);
1577	if ((zip->v_size & 0x0f) != 0 ||
1578	    (zip->erd_size + zip->v_size + 16) > remaining_size ||
1579	    (zip->erd_size + zip->v_size + 16) < (zip->erd_size + zip->v_size))
1580		goto corrupted;
1581	if (ts < zip->v_size) {
1582		free(zip->v_data);
1583		zip->v_data = NULL;
1584	}
1585	p = __archive_read_ahead(a, zip->v_size, NULL);
1586	if (p == NULL)
1587		goto truncated;
1588	if (zip->v_data == NULL) {
1589		zip->v_data = malloc(zip->v_size);
1590		if (zip->v_data == NULL)
1591			goto nomem;
1592	}
1593	memcpy(zip->v_data, p, zip->v_size);
1594	__archive_read_consume(a, zip->v_size);
1595
1596	p = __archive_read_ahead(a, 4, NULL);
1597	if (p == NULL)
1598		goto truncated;
1599	zip->v_crc32 = archive_le32dec(p);
1600	__archive_read_consume(a, 4);
1601
1602	/*return (ARCHIVE_OK);
1603	 * This is not fully implemnted yet.*/
1604	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1605	    "Encrypted file is unsupported");
1606	return (ARCHIVE_FAILED);
1607truncated:
1608	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1609	    "Truncated ZIP file data");
1610	return (ARCHIVE_FATAL);
1611corrupted:
1612	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1613	    "Corrupted ZIP file data");
1614	return (ARCHIVE_FATAL);
1615nomem:
1616	archive_set_error(&a->archive, ENOMEM,
1617	    "No memory for ZIP decryption");
1618	return (ARCHIVE_FATAL);
1619}
1620
1621static int
1622zip_alloc_decryption_buffer(struct archive_read *a)
1623{
1624	struct zip *zip = (struct zip *)(a->format->data);
1625	size_t bs = 256 * 1024;
1626
1627	if (zip->decrypted_buffer == NULL) {
1628		zip->decrypted_buffer_size = bs;
1629		zip->decrypted_buffer = malloc(bs);
1630		if (zip->decrypted_buffer == NULL) {
1631			archive_set_error(&a->archive, ENOMEM,
1632			    "No memory for ZIP decryption");
1633			return (ARCHIVE_FATAL);
1634		}
1635	}
1636	zip->decrypted_ptr = zip->decrypted_buffer;
1637	return (ARCHIVE_OK);
1638}
1639
1640static int
1641init_traditional_PKWARE_decryption(struct archive_read *a)
1642{
1643	struct zip *zip = (struct zip *)(a->format->data);
1644	const void *p;
1645	int retry;
1646	int r;
1647
1648	if (zip->tctx_valid)
1649		return (ARCHIVE_OK);
1650
1651	/*
1652	   Read the 12 bytes encryption header stored at
1653	   the start of the data area.
1654	 */
1655#define ENC_HEADER_SIZE	12
1656	if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
1657	    && zip->entry_bytes_remaining < ENC_HEADER_SIZE) {
1658		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1659		    "Truncated Zip encrypted body: only %jd bytes available",
1660		    (intmax_t)zip->entry_bytes_remaining);
1661		return (ARCHIVE_FATAL);
1662	}
1663
1664	p = __archive_read_ahead(a, ENC_HEADER_SIZE, NULL);
1665	if (p == NULL) {
1666		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1667		    "Truncated ZIP file data");
1668		return (ARCHIVE_FATAL);
1669	}
1670
1671	for (retry = 0;; retry++) {
1672		const char *passphrase;
1673		uint8_t crcchk;
1674
1675		passphrase = __archive_read_next_passphrase(a);
1676		if (passphrase == NULL) {
1677			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1678			    (retry > 0)?
1679				"Incorrect passphrase":
1680				"Passphrase required for this entry");
1681			return (ARCHIVE_FAILED);
1682		}
1683
1684		/*
1685		 * Initialize ctx for Traditional PKWARE Decyption.
1686		 */
1687		r = trad_enc_init(&zip->tctx, passphrase, strlen(passphrase),
1688			p, ENC_HEADER_SIZE, &crcchk);
1689		if (r == 0 && crcchk == zip->entry->decdat)
1690			break;/* The passphrase is OK. */
1691		if (retry > 10000) {
1692			/* Avoid infinity loop. */
1693			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1694			    "Too many incorrect passphrases");
1695			return (ARCHIVE_FAILED);
1696		}
1697	}
1698
1699	__archive_read_consume(a, ENC_HEADER_SIZE);
1700	zip->tctx_valid = 1;
1701	if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) {
1702	    zip->entry_bytes_remaining -= ENC_HEADER_SIZE;
1703	}
1704	/*zip->entry_uncompressed_bytes_read += ENC_HEADER_SIZE;*/
1705	zip->entry_compressed_bytes_read += ENC_HEADER_SIZE;
1706	zip->decrypted_bytes_remaining = 0;
1707
1708	return (zip_alloc_decryption_buffer(a));
1709#undef ENC_HEADER_SIZE
1710}
1711
1712static int
1713init_WinZip_AES_decryption(struct archive_read *a)
1714{
1715	struct zip *zip = (struct zip *)(a->format->data);
1716	const void *p;
1717	const uint8_t *pv;
1718	size_t key_len, salt_len;
1719	uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
1720	int retry;
1721	int r;
1722
1723	if (zip->cctx_valid || zip->hctx_valid)
1724		return (ARCHIVE_OK);
1725
1726	switch (zip->entry->aes_extra.strength) {
1727	case 1: salt_len = 8;  key_len = 16; break;
1728	case 2: salt_len = 12; key_len = 24; break;
1729	case 3: salt_len = 16; key_len = 32; break;
1730	default: goto corrupted;
1731	}
1732	p = __archive_read_ahead(a, salt_len + 2, NULL);
1733	if (p == NULL)
1734		goto truncated;
1735
1736	for (retry = 0;; retry++) {
1737		const char *passphrase;
1738
1739		passphrase = __archive_read_next_passphrase(a);
1740		if (passphrase == NULL) {
1741			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1742			    (retry > 0)?
1743				"Incorrect passphrase":
1744				"Passphrase required for this entry");
1745			return (ARCHIVE_FAILED);
1746		}
1747		memset(derived_key, 0, sizeof(derived_key));
1748		r = archive_pbkdf2_sha1(passphrase, strlen(passphrase),
1749		    p, salt_len, 1000, derived_key, key_len * 2 + 2);
1750		if (r != 0) {
1751			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1752			    "Decryption is unsupported due to lack of "
1753			    "crypto library");
1754			return (ARCHIVE_FAILED);
1755		}
1756
1757		/* Check password verification value. */
1758		pv = ((const uint8_t *)p) + salt_len;
1759		if (derived_key[key_len * 2] == pv[0] &&
1760		    derived_key[key_len * 2 + 1] == pv[1])
1761			break;/* The passphrase is OK. */
1762		if (retry > 10000) {
1763			/* Avoid infinity loop. */
1764			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1765			    "Too many incorrect passphrases");
1766			return (ARCHIVE_FAILED);
1767		}
1768	}
1769
1770	r = archive_decrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
1771	if (r != 0) {
1772		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1773		    "Decryption is unsupported due to lack of crypto library");
1774		return (ARCHIVE_FAILED);
1775	}
1776	r = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len, key_len);
1777	if (r != 0) {
1778		archive_decrypto_aes_ctr_release(&zip->cctx);
1779		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1780		    "Failed to initialize HMAC-SHA1");
1781		return (ARCHIVE_FAILED);
1782	}
1783	zip->cctx_valid = zip->hctx_valid = 1;
1784	__archive_read_consume(a, salt_len + 2);
1785	zip->entry_bytes_remaining -= salt_len + 2 + AUTH_CODE_SIZE;
1786	if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
1787	    && zip->entry_bytes_remaining < 0)
1788		goto corrupted;
1789	zip->entry_compressed_bytes_read += salt_len + 2 + AUTH_CODE_SIZE;
1790	zip->decrypted_bytes_remaining = 0;
1791
1792	zip->entry->compression = zip->entry->aes_extra.compression;
1793	return (zip_alloc_decryption_buffer(a));
1794
1795truncated:
1796	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1797	    "Truncated ZIP file data");
1798	return (ARCHIVE_FATAL);
1799corrupted:
1800	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1801	    "Corrupted ZIP file data");
1802	return (ARCHIVE_FATAL);
1803}
1804
1805static int
1806archive_read_format_zip_read_data(struct archive_read *a,
1807    const void **buff, size_t *size, int64_t *offset)
1808{
1809	int r;
1810	struct zip *zip = (struct zip *)(a->format->data);
1811
1812	if (zip->has_encrypted_entries ==
1813			ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
1814		zip->has_encrypted_entries = 0;
1815	}
1816
1817	*offset = zip->entry_uncompressed_bytes_read;
1818	*size = 0;
1819	*buff = NULL;
1820
1821	/* If we hit end-of-entry last time, return ARCHIVE_EOF. */
1822	if (zip->end_of_entry)
1823		return (ARCHIVE_EOF);
1824
1825	/* Return EOF immediately if this is a non-regular file. */
1826	if (AE_IFREG != (zip->entry->mode & AE_IFMT))
1827		return (ARCHIVE_EOF);
1828
1829	__archive_read_consume(a, zip->unconsumed);
1830	zip->unconsumed = 0;
1831
1832	if (zip->init_decryption) {
1833		zip->has_encrypted_entries = 1;
1834		if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED)
1835			r = read_decryption_header(a);
1836		else if (zip->entry->compression == WINZIP_AES_ENCRYPTION)
1837			r = init_WinZip_AES_decryption(a);
1838		else
1839			r = init_traditional_PKWARE_decryption(a);
1840		if (r != ARCHIVE_OK)
1841			return (r);
1842		zip->init_decryption = 0;
1843	}
1844
1845	switch(zip->entry->compression) {
1846	case 0:  /* No compression. */
1847		r =  zip_read_data_none(a, buff, size, offset);
1848		break;
1849#ifdef HAVE_ZLIB_H
1850	case 8: /* Deflate compression. */
1851		r =  zip_read_data_deflate(a, buff, size, offset);
1852		break;
1853#endif
1854	default: /* Unsupported compression. */
1855		/* Return a warning. */
1856		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1857		    "Unsupported ZIP compression method (%s)",
1858		    compression_name(zip->entry->compression));
1859		/* We can't decompress this entry, but we will
1860		 * be able to skip() it and try the next entry. */
1861		return (ARCHIVE_FAILED);
1862		break;
1863	}
1864	if (r != ARCHIVE_OK)
1865		return (r);
1866	/* Update checksum */
1867	if (*size)
1868		zip->entry_crc32 = zip->crc32func(zip->entry_crc32, *buff,
1869		    (unsigned)*size);
1870	/* If we hit the end, swallow any end-of-data marker. */
1871	if (zip->end_of_entry) {
1872		/* Check file size, CRC against these values. */
1873		if (zip->entry->compressed_size !=
1874		    zip->entry_compressed_bytes_read) {
1875			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1876			    "ZIP compressed data is wrong size "
1877			    "(read %jd, expected %jd)",
1878			    (intmax_t)zip->entry_compressed_bytes_read,
1879			    (intmax_t)zip->entry->compressed_size);
1880			return (ARCHIVE_WARN);
1881		}
1882		/* Size field only stores the lower 32 bits of the actual
1883		 * size. */
1884		if ((zip->entry->uncompressed_size & UINT32_MAX)
1885		    != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
1886			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1887			    "ZIP uncompressed data is wrong size "
1888			    "(read %jd, expected %jd)\n",
1889			    (intmax_t)zip->entry_uncompressed_bytes_read,
1890			    (intmax_t)zip->entry->uncompressed_size);
1891			return (ARCHIVE_WARN);
1892		}
1893		/* Check computed CRC against header */
1894		if ((!zip->hctx_valid ||
1895		      zip->entry->aes_extra.vendor != AES_VENDOR_AE_2) &&
1896		   zip->entry->crc32 != zip->entry_crc32
1897		    && !zip->ignore_crc32) {
1898			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1899			    "ZIP bad CRC: 0x%lx should be 0x%lx",
1900			    (unsigned long)zip->entry_crc32,
1901			    (unsigned long)zip->entry->crc32);
1902			return (ARCHIVE_WARN);
1903		}
1904	}
1905
1906	return (ARCHIVE_OK);
1907}
1908
1909static int
1910archive_read_format_zip_cleanup(struct archive_read *a)
1911{
1912	struct zip *zip;
1913	struct zip_entry *zip_entry, *next_zip_entry;
1914
1915	zip = (struct zip *)(a->format->data);
1916#ifdef HAVE_ZLIB_H
1917	if (zip->stream_valid)
1918		inflateEnd(&zip->stream);
1919	free(zip->uncompressed_buffer);
1920#endif
1921	if (zip->zip_entries) {
1922		zip_entry = zip->zip_entries;
1923		while (zip_entry != NULL) {
1924			next_zip_entry = zip_entry->next;
1925			archive_string_free(&zip_entry->rsrcname);
1926			free(zip_entry);
1927			zip_entry = next_zip_entry;
1928		}
1929	}
1930	free(zip->decrypted_buffer);
1931	if (zip->cctx_valid)
1932		archive_decrypto_aes_ctr_release(&zip->cctx);
1933	if (zip->hctx_valid)
1934		archive_hmac_sha1_cleanup(&zip->hctx);
1935	free(zip->iv);
1936	free(zip->erd);
1937	free(zip->v_data);
1938	archive_string_free(&zip->format_name);
1939	free(zip);
1940	(a->format->data) = NULL;
1941	return (ARCHIVE_OK);
1942}
1943
1944static int
1945archive_read_format_zip_has_encrypted_entries(struct archive_read *_a)
1946{
1947	if (_a && _a->format) {
1948		struct zip * zip = (struct zip *)_a->format->data;
1949		if (zip) {
1950			return zip->has_encrypted_entries;
1951		}
1952	}
1953	return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
1954}
1955
1956static int
1957archive_read_format_zip_options(struct archive_read *a,
1958    const char *key, const char *val)
1959{
1960	struct zip *zip;
1961	int ret = ARCHIVE_FAILED;
1962
1963	zip = (struct zip *)(a->format->data);
1964	if (strcmp(key, "compat-2x")  == 0) {
1965		/* Handle filenames as libarchive 2.x */
1966		zip->init_default_conversion = (val != NULL) ? 1 : 0;
1967		return (ARCHIVE_OK);
1968	} else if (strcmp(key, "hdrcharset")  == 0) {
1969		if (val == NULL || val[0] == 0)
1970			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1971			    "zip: hdrcharset option needs a character-set name"
1972			);
1973		else {
1974			zip->sconv = archive_string_conversion_from_charset(
1975			    &a->archive, val, 0);
1976			if (zip->sconv != NULL) {
1977				if (strcmp(val, "UTF-8") == 0)
1978					zip->sconv_utf8 = zip->sconv;
1979				ret = ARCHIVE_OK;
1980			} else
1981				ret = ARCHIVE_FATAL;
1982		}
1983		return (ret);
1984	} else if (strcmp(key, "ignorecrc32") == 0) {
1985		/* Mostly useful for testing. */
1986		if (val == NULL || val[0] == 0) {
1987			zip->crc32func = real_crc32;
1988			zip->ignore_crc32 = 0;
1989		} else {
1990			zip->crc32func = fake_crc32;
1991			zip->ignore_crc32 = 1;
1992		}
1993		return (ARCHIVE_OK);
1994	} else if (strcmp(key, "mac-ext") == 0) {
1995		zip->process_mac_extensions = (val != NULL && val[0] != 0);
1996		return (ARCHIVE_OK);
1997	}
1998
1999	/* Note: The "warn" return is just to inform the options
2000	 * supervisor that we didn't handle it.  It will generate
2001	 * a suitable error if no one used this option. */
2002	return (ARCHIVE_WARN);
2003}
2004
2005int
2006archive_read_support_format_zip(struct archive *a)
2007{
2008	int r;
2009	r = archive_read_support_format_zip_streamable(a);
2010	if (r != ARCHIVE_OK)
2011		return r;
2012	return (archive_read_support_format_zip_seekable(a));
2013}
2014
2015/* ------------------------------------------------------------------------ */
2016
2017/*
2018 * Streaming-mode support
2019 */
2020
2021
2022static int
2023archive_read_support_format_zip_capabilities_streamable(struct archive_read * a)
2024{
2025	(void)a; /* UNUSED */
2026	return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
2027		ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
2028}
2029
2030static int
2031archive_read_format_zip_streamable_bid(struct archive_read *a, int best_bid)
2032{
2033	const char *p;
2034
2035	(void)best_bid; /* UNUSED */
2036
2037	if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
2038		return (-1);
2039
2040	/*
2041	 * Bid of 29 here comes from:
2042	 *  + 16 bits for "PK",
2043	 *  + next 16-bit field has 6 options so contributes
2044	 *    about 16 - log_2(6) ~= 16 - 2.6 ~= 13 bits
2045	 *
2046	 * So we've effectively verified ~29 total bits of check data.
2047	 */
2048	if (p[0] == 'P' && p[1] == 'K') {
2049		if ((p[2] == '\001' && p[3] == '\002')
2050		    || (p[2] == '\003' && p[3] == '\004')
2051		    || (p[2] == '\005' && p[3] == '\006')
2052		    || (p[2] == '\006' && p[3] == '\006')
2053		    || (p[2] == '\007' && p[3] == '\010')
2054		    || (p[2] == '0' && p[3] == '0'))
2055			return (29);
2056	}
2057
2058	/* TODO: It's worth looking ahead a little bit for a valid
2059	 * PK signature.  In particular, that would make it possible
2060	 * to read some UUEncoded SFX files or SFX files coming from
2061	 * a network socket. */
2062
2063	return (0);
2064}
2065
2066static int
2067archive_read_format_zip_streamable_read_header(struct archive_read *a,
2068    struct archive_entry *entry)
2069{
2070	struct zip *zip;
2071
2072	a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
2073	if (a->archive.archive_format_name == NULL)
2074		a->archive.archive_format_name = "ZIP";
2075
2076	zip = (struct zip *)(a->format->data);
2077
2078	/*
2079	 * It should be sufficient to call archive_read_next_header() for
2080	 * a reader to determine if an entry is encrypted or not. If the
2081	 * encryption of an entry is only detectable when calling
2082	 * archive_read_data(), so be it. We'll do the same check there
2083	 * as well.
2084	 */
2085	if (zip->has_encrypted_entries ==
2086			ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
2087		zip->has_encrypted_entries = 0;
2088
2089	/* Make sure we have a zip_entry structure to use. */
2090	if (zip->zip_entries == NULL) {
2091		zip->zip_entries = malloc(sizeof(struct zip_entry));
2092		if (zip->zip_entries == NULL) {
2093			archive_set_error(&a->archive, ENOMEM,
2094			    "Out  of memory");
2095			return ARCHIVE_FATAL;
2096		}
2097	}
2098	zip->entry = zip->zip_entries;
2099	memset(zip->entry, 0, sizeof(struct zip_entry));
2100
2101	if (zip->cctx_valid)
2102		archive_decrypto_aes_ctr_release(&zip->cctx);
2103	if (zip->hctx_valid)
2104		archive_hmac_sha1_cleanup(&zip->hctx);
2105	zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
2106	__archive_read_reset_passphrase(a);
2107
2108	/* Search ahead for the next local file header. */
2109	__archive_read_consume(a, zip->unconsumed);
2110	zip->unconsumed = 0;
2111	for (;;) {
2112		int64_t skipped = 0;
2113		const char *p, *end;
2114		ssize_t bytes;
2115
2116		p = __archive_read_ahead(a, 4, &bytes);
2117		if (p == NULL)
2118			return (ARCHIVE_FATAL);
2119		end = p + bytes;
2120
2121		while (p + 4 <= end) {
2122			if (p[0] == 'P' && p[1] == 'K') {
2123				if (p[2] == '\003' && p[3] == '\004') {
2124					/* Regular file entry. */
2125					__archive_read_consume(a, skipped);
2126					return zip_read_local_file_header(a,
2127					    entry, zip);
2128				}
2129
2130                              /*
2131                               * TODO: We cannot restore permissions
2132                               * based only on the local file headers.
2133                               * Consider scanning the central
2134                               * directory and returning additional
2135                               * entries for at least directories.
2136                               * This would allow us to properly set
2137                               * directory permissions.
2138			       *
2139			       * This won't help us fix symlinks
2140			       * and may not help with regular file
2141			       * permissions, either.  <sigh>
2142                               */
2143                              if (p[2] == '\001' && p[3] == '\002') {
2144                                      return (ARCHIVE_EOF);
2145                              }
2146
2147                              /* End of central directory?  Must be an
2148                               * empty archive. */
2149                              if ((p[2] == '\005' && p[3] == '\006')
2150                                  || (p[2] == '\006' && p[3] == '\006'))
2151                                      return (ARCHIVE_EOF);
2152			}
2153			++p;
2154			++skipped;
2155		}
2156		__archive_read_consume(a, skipped);
2157	}
2158}
2159
2160static int
2161archive_read_format_zip_read_data_skip_streamable(struct archive_read *a)
2162{
2163	struct zip *zip;
2164	int64_t bytes_skipped;
2165
2166	zip = (struct zip *)(a->format->data);
2167	bytes_skipped = __archive_read_consume(a, zip->unconsumed);
2168	zip->unconsumed = 0;
2169	if (bytes_skipped < 0)
2170		return (ARCHIVE_FATAL);
2171
2172	/* If we've already read to end of data, we're done. */
2173	if (zip->end_of_entry)
2174		return (ARCHIVE_OK);
2175
2176	/* So we know we're streaming... */
2177	if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
2178	    || zip->entry->compressed_size > 0) {
2179		/* We know the compressed length, so we can just skip. */
2180		bytes_skipped = __archive_read_consume(a,
2181					zip->entry_bytes_remaining);
2182		if (bytes_skipped < 0)
2183			return (ARCHIVE_FATAL);
2184		return (ARCHIVE_OK);
2185	}
2186
2187	if (zip->init_decryption) {
2188		int r;
2189
2190		zip->has_encrypted_entries = 1;
2191		if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED)
2192			r = read_decryption_header(a);
2193		else if (zip->entry->compression == WINZIP_AES_ENCRYPTION)
2194			r = init_WinZip_AES_decryption(a);
2195		else
2196			r = init_traditional_PKWARE_decryption(a);
2197		if (r != ARCHIVE_OK)
2198			return (r);
2199		zip->init_decryption = 0;
2200	}
2201
2202	/* We're streaming and we don't know the length. */
2203	/* If the body is compressed and we know the format, we can
2204	 * find an exact end-of-entry by decompressing it. */
2205	switch (zip->entry->compression) {
2206#ifdef HAVE_ZLIB_H
2207	case 8: /* Deflate compression. */
2208		while (!zip->end_of_entry) {
2209			int64_t offset = 0;
2210			const void *buff = NULL;
2211			size_t size = 0;
2212			int r;
2213			r =  zip_read_data_deflate(a, &buff, &size, &offset);
2214			if (r != ARCHIVE_OK)
2215				return (r);
2216		}
2217		return ARCHIVE_OK;
2218#endif
2219	default: /* Uncompressed or unknown. */
2220		/* Scan for a PK\007\010 signature. */
2221		for (;;) {
2222			const char *p, *buff;
2223			ssize_t bytes_avail;
2224			buff = __archive_read_ahead(a, 16, &bytes_avail);
2225			if (bytes_avail < 16) {
2226				archive_set_error(&a->archive,
2227				    ARCHIVE_ERRNO_FILE_FORMAT,
2228				    "Truncated ZIP file data");
2229				return (ARCHIVE_FATAL);
2230			}
2231			p = buff;
2232			while (p <= buff + bytes_avail - 16) {
2233				if (p[3] == 'P') { p += 3; }
2234				else if (p[3] == 'K') { p += 2; }
2235				else if (p[3] == '\007') { p += 1; }
2236				else if (p[3] == '\010' && p[2] == '\007'
2237				    && p[1] == 'K' && p[0] == 'P') {
2238					if (zip->entry->flags & LA_USED_ZIP64)
2239						__archive_read_consume(a,
2240						    p - buff + 24);
2241					else
2242						__archive_read_consume(a,
2243						    p - buff + 16);
2244					return ARCHIVE_OK;
2245				} else { p += 4; }
2246			}
2247			__archive_read_consume(a, p - buff);
2248		}
2249	}
2250}
2251
2252int
2253archive_read_support_format_zip_streamable(struct archive *_a)
2254{
2255	struct archive_read *a = (struct archive_read *)_a;
2256	struct zip *zip;
2257	int r;
2258
2259	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
2260	    ARCHIVE_STATE_NEW, "archive_read_support_format_zip");
2261
2262	zip = (struct zip *)calloc(1, sizeof(*zip));
2263	if (zip == NULL) {
2264		archive_set_error(&a->archive, ENOMEM,
2265		    "Can't allocate zip data");
2266		return (ARCHIVE_FATAL);
2267	}
2268
2269	/* Streamable reader doesn't support mac extensions. */
2270	zip->process_mac_extensions = 0;
2271
2272	/*
2273	 * Until enough data has been read, we cannot tell about
2274	 * any encrypted entries yet.
2275	 */
2276	zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
2277	zip->crc32func = real_crc32;
2278
2279	r = __archive_read_register_format(a,
2280	    zip,
2281	    "zip",
2282	    archive_read_format_zip_streamable_bid,
2283	    archive_read_format_zip_options,
2284	    archive_read_format_zip_streamable_read_header,
2285	    archive_read_format_zip_read_data,
2286	    archive_read_format_zip_read_data_skip_streamable,
2287	    NULL,
2288	    archive_read_format_zip_cleanup,
2289	    archive_read_support_format_zip_capabilities_streamable,
2290	    archive_read_format_zip_has_encrypted_entries);
2291
2292	if (r != ARCHIVE_OK)
2293		free(zip);
2294	return (ARCHIVE_OK);
2295}
2296
2297/* ------------------------------------------------------------------------ */
2298
2299/*
2300 * Seeking-mode support
2301 */
2302
2303static int
2304archive_read_support_format_zip_capabilities_seekable(struct archive_read * a)
2305{
2306	(void)a; /* UNUSED */
2307	return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
2308		ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
2309}
2310
2311/*
2312 * TODO: This is a performance sink because it forces the read core to
2313 * drop buffered data from the start of file, which will then have to
2314 * be re-read again if this bidder loses.
2315 *
2316 * We workaround this a little by passing in the best bid so far so
2317 * that later bidders can do nothing if they know they'll never
2318 * outbid.  But we can certainly do better...
2319 */
2320static int
2321read_eocd(struct zip *zip, const char *p, int64_t current_offset)
2322{
2323	/* Sanity-check the EOCD we've found. */
2324
2325	/* This must be the first volume. */
2326	if (archive_le16dec(p + 4) != 0)
2327		return 0;
2328	/* Central directory must be on this volume. */
2329	if (archive_le16dec(p + 4) != archive_le16dec(p + 6))
2330		return 0;
2331	/* All central directory entries must be on this volume. */
2332	if (archive_le16dec(p + 10) != archive_le16dec(p + 8))
2333		return 0;
2334	/* Central directory can't extend beyond start of EOCD record. */
2335	if (archive_le32dec(p + 16) + archive_le32dec(p + 12)
2336	    > current_offset)
2337		return 0;
2338
2339	/* Save the central directory location for later use. */
2340	zip->central_directory_offset = archive_le32dec(p + 16);
2341
2342	/* This is just a tiny bit higher than the maximum
2343	   returned by the streaming Zip bidder.  This ensures
2344	   that the more accurate seeking Zip parser wins
2345	   whenever seek is available. */
2346	return 32;
2347}
2348
2349/*
2350 * Examine Zip64 EOCD locator:  If it's valid, store the information
2351 * from it.
2352 */
2353static void
2354read_zip64_eocd(struct archive_read *a, struct zip *zip, const char *p)
2355{
2356	int64_t eocd64_offset;
2357	int64_t eocd64_size;
2358
2359	/* Sanity-check the locator record. */
2360
2361	/* Central dir must be on first volume. */
2362	if (archive_le32dec(p + 4) != 0)
2363		return;
2364	/* Must be only a single volume. */
2365	if (archive_le32dec(p + 16) != 1)
2366		return;
2367
2368	/* Find the Zip64 EOCD record. */
2369	eocd64_offset = archive_le64dec(p + 8);
2370	if (__archive_read_seek(a, eocd64_offset, SEEK_SET) < 0)
2371		return;
2372	if ((p = __archive_read_ahead(a, 56, NULL)) == NULL)
2373		return;
2374	/* Make sure we can read all of it. */
2375	eocd64_size = archive_le64dec(p + 4) + 12;
2376	if (eocd64_size < 56 || eocd64_size > 16384)
2377		return;
2378	if ((p = __archive_read_ahead(a, (size_t)eocd64_size, NULL)) == NULL)
2379		return;
2380
2381	/* Sanity-check the EOCD64 */
2382	if (archive_le32dec(p + 16) != 0) /* Must be disk #0 */
2383		return;
2384	if (archive_le32dec(p + 20) != 0) /* CD must be on disk #0 */
2385		return;
2386	/* CD can't be split. */
2387	if (archive_le64dec(p + 24) != archive_le64dec(p + 32))
2388		return;
2389
2390	/* Save the central directory offset for later use. */
2391	zip->central_directory_offset = archive_le64dec(p + 48);
2392}
2393
2394static int
2395archive_read_format_zip_seekable_bid(struct archive_read *a, int best_bid)
2396{
2397	struct zip *zip = (struct zip *)a->format->data;
2398	int64_t file_size, current_offset;
2399	const char *p;
2400	int i, tail;
2401
2402	/* If someone has already bid more than 32, then avoid
2403	   trashing the look-ahead buffers with a seek. */
2404	if (best_bid > 32)
2405		return (-1);
2406
2407	file_size = __archive_read_seek(a, 0, SEEK_END);
2408	if (file_size <= 0)
2409		return 0;
2410
2411	/* Search last 16k of file for end-of-central-directory
2412	 * record (which starts with PK\005\006) */
2413	tail = (int)zipmin(1024 * 16, file_size);
2414	current_offset = __archive_read_seek(a, -tail, SEEK_END);
2415	if (current_offset < 0)
2416		return 0;
2417	if ((p = __archive_read_ahead(a, (size_t)tail, NULL)) == NULL)
2418		return 0;
2419	/* Boyer-Moore search backwards from the end, since we want
2420	 * to match the last EOCD in the file (there can be more than
2421	 * one if there is an uncompressed Zip archive as a member
2422	 * within this Zip archive). */
2423	for (i = tail - 22; i > 0;) {
2424		switch (p[i]) {
2425		case 'P':
2426			if (memcmp(p + i, "PK\005\006", 4) == 0) {
2427				int ret = read_eocd(zip, p + i,
2428				    current_offset + i);
2429				if (ret > 0) {
2430					/* Zip64 EOCD locator precedes
2431					 * regular EOCD if present. */
2432					if (i >= 20
2433					    && memcmp(p + i - 20, "PK\006\007", 4) == 0) {
2434						read_zip64_eocd(a, zip, p + i - 20);
2435					}
2436					return (ret);
2437				}
2438			}
2439			i -= 4;
2440			break;
2441		case 'K': i -= 1; break;
2442		case 005: i -= 2; break;
2443		case 006: i -= 3; break;
2444		default: i -= 4; break;
2445		}
2446	}
2447	return 0;
2448}
2449
2450/* The red-black trees are only used in seeking mode to manage
2451 * the in-memory copy of the central directory. */
2452
2453static int
2454cmp_node(const struct archive_rb_node *n1, const struct archive_rb_node *n2)
2455{
2456	const struct zip_entry *e1 = (const struct zip_entry *)n1;
2457	const struct zip_entry *e2 = (const struct zip_entry *)n2;
2458
2459	if (e1->local_header_offset > e2->local_header_offset)
2460		return -1;
2461	if (e1->local_header_offset < e2->local_header_offset)
2462		return 1;
2463	return 0;
2464}
2465
2466static int
2467cmp_key(const struct archive_rb_node *n, const void *key)
2468{
2469	/* This function won't be called */
2470	(void)n; /* UNUSED */
2471	(void)key; /* UNUSED */
2472	return 1;
2473}
2474
2475static const struct archive_rb_tree_ops rb_ops = {
2476	&cmp_node, &cmp_key
2477};
2478
2479static int
2480rsrc_cmp_node(const struct archive_rb_node *n1,
2481    const struct archive_rb_node *n2)
2482{
2483	const struct zip_entry *e1 = (const struct zip_entry *)n1;
2484	const struct zip_entry *e2 = (const struct zip_entry *)n2;
2485
2486	return (strcmp(e2->rsrcname.s, e1->rsrcname.s));
2487}
2488
2489static int
2490rsrc_cmp_key(const struct archive_rb_node *n, const void *key)
2491{
2492	const struct zip_entry *e = (const struct zip_entry *)n;
2493	return (strcmp((const char *)key, e->rsrcname.s));
2494}
2495
2496static const struct archive_rb_tree_ops rb_rsrc_ops = {
2497	&rsrc_cmp_node, &rsrc_cmp_key
2498};
2499
2500static const char *
2501rsrc_basename(const char *name, size_t name_length)
2502{
2503	const char *s, *r;
2504
2505	r = s = name;
2506	for (;;) {
2507		s = memchr(s, '/', name_length - (s - name));
2508		if (s == NULL)
2509			break;
2510		r = ++s;
2511	}
2512	return (r);
2513}
2514
2515static void
2516expose_parent_dirs(struct zip *zip, const char *name, size_t name_length)
2517{
2518	struct archive_string str;
2519	struct zip_entry *dir;
2520	char *s;
2521
2522	archive_string_init(&str);
2523	archive_strncpy(&str, name, name_length);
2524	for (;;) {
2525		s = strrchr(str.s, '/');
2526		if (s == NULL)
2527			break;
2528		*s = '\0';
2529		/* Transfer the parent directory from zip->tree_rsrc RB
2530		 * tree to zip->tree RB tree to expose. */
2531		dir = (struct zip_entry *)
2532		    __archive_rb_tree_find_node(&zip->tree_rsrc, str.s);
2533		if (dir == NULL)
2534			break;
2535		__archive_rb_tree_remove_node(&zip->tree_rsrc, &dir->node);
2536		archive_string_free(&dir->rsrcname);
2537		__archive_rb_tree_insert_node(&zip->tree, &dir->node);
2538	}
2539	archive_string_free(&str);
2540}
2541
2542static int
2543slurp_central_directory(struct archive_read *a, struct zip *zip)
2544{
2545	ssize_t i;
2546	unsigned found;
2547	int64_t correction;
2548	ssize_t bytes_avail;
2549	const char *p;
2550
2551	/*
2552	 * Find the start of the central directory.  The end-of-CD
2553	 * record has our starting point, but there are lots of
2554	 * Zip archives which have had other data prepended to the
2555	 * file, which makes the recorded offsets all too small.
2556	 * So we search forward from the specified offset until we
2557	 * find the real start of the central directory.  Then we
2558	 * know the correction we need to apply to account for leading
2559	 * padding.
2560	 */
2561	if (__archive_read_seek(a, zip->central_directory_offset, SEEK_SET) < 0)
2562		return ARCHIVE_FATAL;
2563
2564	found = 0;
2565	while (!found) {
2566		if ((p = __archive_read_ahead(a, 20, &bytes_avail)) == NULL)
2567			return ARCHIVE_FATAL;
2568		for (found = 0, i = 0; !found && i < bytes_avail - 4;) {
2569			switch (p[i + 3]) {
2570			case 'P': i += 3; break;
2571			case 'K': i += 2; break;
2572			case 001: i += 1; break;
2573			case 002:
2574				if (memcmp(p + i, "PK\001\002", 4) == 0) {
2575					p += i;
2576					found = 1;
2577				} else
2578					i += 4;
2579				break;
2580			case 005: i += 1; break;
2581			case 006:
2582				if (memcmp(p + i, "PK\005\006", 4) == 0) {
2583					p += i;
2584					found = 1;
2585				} else if (memcmp(p + i, "PK\006\006", 4) == 0) {
2586					p += i;
2587					found = 1;
2588				} else
2589					i += 1;
2590				break;
2591			default: i += 4; break;
2592			}
2593		}
2594		__archive_read_consume(a, i);
2595	}
2596	correction = archive_filter_bytes(&a->archive, 0)
2597			- zip->central_directory_offset;
2598
2599	__archive_rb_tree_init(&zip->tree, &rb_ops);
2600	__archive_rb_tree_init(&zip->tree_rsrc, &rb_rsrc_ops);
2601
2602	zip->central_directory_entries_total = 0;
2603	while (1) {
2604		struct zip_entry *zip_entry;
2605		size_t filename_length, extra_length, comment_length;
2606		uint32_t external_attributes;
2607		const char *name, *r;
2608
2609		if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
2610			return ARCHIVE_FATAL;
2611		if (memcmp(p, "PK\006\006", 4) == 0
2612		    || memcmp(p, "PK\005\006", 4) == 0) {
2613			break;
2614		} else if (memcmp(p, "PK\001\002", 4) != 0) {
2615			archive_set_error(&a->archive,
2616			    -1, "Invalid central directory signature");
2617			return ARCHIVE_FATAL;
2618		}
2619		if ((p = __archive_read_ahead(a, 46, NULL)) == NULL)
2620			return ARCHIVE_FATAL;
2621
2622		zip_entry = calloc(1, sizeof(struct zip_entry));
2623		zip_entry->next = zip->zip_entries;
2624		zip_entry->flags |= LA_FROM_CENTRAL_DIRECTORY;
2625		zip->zip_entries = zip_entry;
2626		zip->central_directory_entries_total++;
2627
2628		/* version = p[4]; */
2629		zip_entry->system = p[5];
2630		/* version_required = archive_le16dec(p + 6); */
2631		zip_entry->zip_flags = archive_le16dec(p + 8);
2632		if (zip_entry->zip_flags
2633		      & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)){
2634			zip->has_encrypted_entries = 1;
2635		}
2636		zip_entry->compression = (char)archive_le16dec(p + 10);
2637		zip_entry->mtime = zip_time(p + 12);
2638		zip_entry->crc32 = archive_le32dec(p + 16);
2639		if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
2640			zip_entry->decdat = p[13];
2641		else
2642			zip_entry->decdat = p[19];
2643		zip_entry->compressed_size = archive_le32dec(p + 20);
2644		zip_entry->uncompressed_size = archive_le32dec(p + 24);
2645		filename_length = archive_le16dec(p + 28);
2646		extra_length = archive_le16dec(p + 30);
2647		comment_length = archive_le16dec(p + 32);
2648		/* disk_start = archive_le16dec(p + 34); */ /* Better be zero. */
2649		/* internal_attributes = archive_le16dec(p + 36); */ /* text bit */
2650		external_attributes = archive_le32dec(p + 38);
2651		zip_entry->local_header_offset =
2652		    archive_le32dec(p + 42) + correction;
2653
2654		/* If we can't guess the mode, leave it zero here;
2655		   when we read the local file header we might get
2656		   more information. */
2657		if (zip_entry->system == 3) {
2658			zip_entry->mode = external_attributes >> 16;
2659		} else if (zip_entry->system == 0) {
2660			// Interpret MSDOS directory bit
2661			if (0x10 == (external_attributes & 0x10)) {
2662				zip_entry->mode = AE_IFDIR | 0775;
2663			} else {
2664				zip_entry->mode = AE_IFREG | 0664;
2665			}
2666			if (0x01 == (external_attributes & 0x01)) {
2667				// Read-only bit; strip write permissions
2668				zip_entry->mode &= 0555;
2669			}
2670		} else {
2671			zip_entry->mode = 0;
2672		}
2673
2674		/* We're done with the regular data; get the filename and
2675		 * extra data. */
2676		__archive_read_consume(a, 46);
2677		p = __archive_read_ahead(a, filename_length + extra_length,
2678			NULL);
2679		if (p == NULL) {
2680			archive_set_error(&a->archive,
2681			    ARCHIVE_ERRNO_FILE_FORMAT,
2682			    "Truncated ZIP file header");
2683			return ARCHIVE_FATAL;
2684		}
2685		process_extra(p + filename_length, extra_length, zip_entry);
2686
2687		/*
2688		 * Mac resource fork files are stored under the
2689		 * "__MACOSX/" directory, so we should check if
2690		 * it is.
2691		 */
2692		if (!zip->process_mac_extensions) {
2693			/* Treat every entry as a regular entry. */
2694			__archive_rb_tree_insert_node(&zip->tree,
2695			    &zip_entry->node);
2696		} else {
2697			name = p;
2698			r = rsrc_basename(name, filename_length);
2699			if (filename_length >= 9 &&
2700			    strncmp("__MACOSX/", name, 9) == 0) {
2701				/* If this file is not a resource fork nor
2702				 * a directory. We should treat it as a non
2703				 * resource fork file to expose it. */
2704				if (name[filename_length-1] != '/' &&
2705				    (r - name < 3 || r[0] != '.' || r[1] != '_')) {
2706					__archive_rb_tree_insert_node(
2707					    &zip->tree, &zip_entry->node);
2708					/* Expose its parent directories. */
2709					expose_parent_dirs(zip, name,
2710					    filename_length);
2711				} else {
2712					/* This file is a resource fork file or
2713					 * a directory. */
2714					archive_strncpy(&(zip_entry->rsrcname),
2715					     name, filename_length);
2716					__archive_rb_tree_insert_node(
2717					    &zip->tree_rsrc, &zip_entry->node);
2718				}
2719			} else {
2720				/* Generate resource fork name to find its
2721				 * resource file at zip->tree_rsrc. */
2722				archive_strcpy(&(zip_entry->rsrcname),
2723				    "__MACOSX/");
2724				archive_strncat(&(zip_entry->rsrcname),
2725				    name, r - name);
2726				archive_strcat(&(zip_entry->rsrcname), "._");
2727				archive_strncat(&(zip_entry->rsrcname),
2728				    name + (r - name),
2729				    filename_length - (r - name));
2730				/* Register an entry to RB tree to sort it by
2731				 * file offset. */
2732				__archive_rb_tree_insert_node(&zip->tree,
2733				    &zip_entry->node);
2734			}
2735		}
2736
2737		/* Skip the comment too ... */
2738		__archive_read_consume(a,
2739		    filename_length + extra_length + comment_length);
2740	}
2741
2742	return ARCHIVE_OK;
2743}
2744
2745static ssize_t
2746zip_get_local_file_header_size(struct archive_read *a, size_t extra)
2747{
2748	const char *p;
2749	ssize_t filename_length, extra_length;
2750
2751	if ((p = __archive_read_ahead(a, extra + 30, NULL)) == NULL) {
2752		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2753		    "Truncated ZIP file header");
2754		return (ARCHIVE_WARN);
2755	}
2756	p += extra;
2757
2758	if (memcmp(p, "PK\003\004", 4) != 0) {
2759		archive_set_error(&a->archive, -1, "Damaged Zip archive");
2760		return ARCHIVE_WARN;
2761	}
2762	filename_length = archive_le16dec(p + 26);
2763	extra_length = archive_le16dec(p + 28);
2764
2765	return (30 + filename_length + extra_length);
2766}
2767
2768static int
2769zip_read_mac_metadata(struct archive_read *a, struct archive_entry *entry,
2770    struct zip_entry *rsrc)
2771{
2772	struct zip *zip = (struct zip *)a->format->data;
2773	unsigned char *metadata, *mp;
2774	int64_t offset = archive_filter_bytes(&a->archive, 0);
2775	size_t remaining_bytes, metadata_bytes;
2776	ssize_t hsize;
2777	int ret = ARCHIVE_OK, eof;
2778
2779	switch(rsrc->compression) {
2780	case 0:  /* No compression. */
2781		if (rsrc->uncompressed_size != rsrc->compressed_size) {
2782			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2783			    "Malformed OS X metadata entry: inconsistent size");
2784			return (ARCHIVE_FATAL);
2785		}
2786#ifdef HAVE_ZLIB_H
2787	case 8: /* Deflate compression. */
2788#endif
2789		break;
2790	default: /* Unsupported compression. */
2791		/* Return a warning. */
2792		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2793		    "Unsupported ZIP compression method (%s)",
2794		    compression_name(rsrc->compression));
2795		/* We can't decompress this entry, but we will
2796		 * be able to skip() it and try the next entry. */
2797		return (ARCHIVE_WARN);
2798	}
2799
2800	if (rsrc->uncompressed_size > (4 * 1024 * 1024)) {
2801		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2802		    "Mac metadata is too large: %jd > 4M bytes",
2803		    (intmax_t)rsrc->uncompressed_size);
2804		return (ARCHIVE_WARN);
2805	}
2806	if (rsrc->compressed_size > (4 * 1024 * 1024)) {
2807		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2808		    "Mac metadata is too large: %jd > 4M bytes",
2809		    (intmax_t)rsrc->compressed_size);
2810		return (ARCHIVE_WARN);
2811	}
2812
2813	metadata = malloc((size_t)rsrc->uncompressed_size);
2814	if (metadata == NULL) {
2815		archive_set_error(&a->archive, ENOMEM,
2816		    "Can't allocate memory for Mac metadata");
2817		return (ARCHIVE_FATAL);
2818	}
2819
2820	if (offset < rsrc->local_header_offset)
2821		__archive_read_consume(a, rsrc->local_header_offset - offset);
2822	else if (offset != rsrc->local_header_offset) {
2823		__archive_read_seek(a, rsrc->local_header_offset, SEEK_SET);
2824	}
2825
2826	hsize = zip_get_local_file_header_size(a, 0);
2827	__archive_read_consume(a, hsize);
2828
2829	remaining_bytes = (size_t)rsrc->compressed_size;
2830	metadata_bytes = (size_t)rsrc->uncompressed_size;
2831	mp = metadata;
2832	eof = 0;
2833	while (!eof && remaining_bytes) {
2834		const unsigned char *p;
2835		ssize_t bytes_avail;
2836		size_t bytes_used;
2837
2838		p = __archive_read_ahead(a, 1, &bytes_avail);
2839		if (p == NULL) {
2840			archive_set_error(&a->archive,
2841			    ARCHIVE_ERRNO_FILE_FORMAT,
2842			    "Truncated ZIP file header");
2843			ret = ARCHIVE_WARN;
2844			goto exit_mac_metadata;
2845		}
2846		if ((size_t)bytes_avail > remaining_bytes)
2847			bytes_avail = remaining_bytes;
2848		switch(rsrc->compression) {
2849		case 0:  /* No compression. */
2850			if ((size_t)bytes_avail > metadata_bytes)
2851				bytes_avail = metadata_bytes;
2852			memcpy(mp, p, bytes_avail);
2853			bytes_used = (size_t)bytes_avail;
2854			metadata_bytes -= bytes_used;
2855			mp += bytes_used;
2856			if (metadata_bytes == 0)
2857				eof = 1;
2858			break;
2859#ifdef HAVE_ZLIB_H
2860		case 8: /* Deflate compression. */
2861		{
2862			int r;
2863
2864			ret = zip_deflate_init(a, zip);
2865			if (ret != ARCHIVE_OK)
2866				goto exit_mac_metadata;
2867			zip->stream.next_in =
2868			    (Bytef *)(uintptr_t)(const void *)p;
2869			zip->stream.avail_in = (uInt)bytes_avail;
2870			zip->stream.total_in = 0;
2871			zip->stream.next_out = mp;
2872			zip->stream.avail_out = (uInt)metadata_bytes;
2873			zip->stream.total_out = 0;
2874
2875			r = inflate(&zip->stream, 0);
2876			switch (r) {
2877			case Z_OK:
2878				break;
2879			case Z_STREAM_END:
2880				eof = 1;
2881				break;
2882			case Z_MEM_ERROR:
2883				archive_set_error(&a->archive, ENOMEM,
2884				    "Out of memory for ZIP decompression");
2885				ret = ARCHIVE_FATAL;
2886				goto exit_mac_metadata;
2887			default:
2888				archive_set_error(&a->archive,
2889				    ARCHIVE_ERRNO_MISC,
2890				    "ZIP decompression failed (%d)", r);
2891				ret = ARCHIVE_FATAL;
2892				goto exit_mac_metadata;
2893			}
2894			bytes_used = zip->stream.total_in;
2895			metadata_bytes -= zip->stream.total_out;
2896			mp += zip->stream.total_out;
2897			break;
2898		}
2899#endif
2900		default:
2901			bytes_used = 0;
2902			break;
2903		}
2904		__archive_read_consume(a, bytes_used);
2905		remaining_bytes -= bytes_used;
2906	}
2907	archive_entry_copy_mac_metadata(entry, metadata,
2908	    (size_t)rsrc->uncompressed_size - metadata_bytes);
2909
2910exit_mac_metadata:
2911	__archive_read_seek(a, offset, SEEK_SET);
2912	zip->decompress_init = 0;
2913	free(metadata);
2914	return (ret);
2915}
2916
2917static int
2918archive_read_format_zip_seekable_read_header(struct archive_read *a,
2919	struct archive_entry *entry)
2920{
2921	struct zip *zip = (struct zip *)a->format->data;
2922	struct zip_entry *rsrc;
2923	int64_t offset;
2924	int r, ret = ARCHIVE_OK;
2925
2926	/*
2927	 * It should be sufficient to call archive_read_next_header() for
2928	 * a reader to determine if an entry is encrypted or not. If the
2929	 * encryption of an entry is only detectable when calling
2930	 * archive_read_data(), so be it. We'll do the same check there
2931	 * as well.
2932	 */
2933	if (zip->has_encrypted_entries ==
2934			ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
2935		zip->has_encrypted_entries = 0;
2936
2937	a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
2938	if (a->archive.archive_format_name == NULL)
2939		a->archive.archive_format_name = "ZIP";
2940
2941	if (zip->zip_entries == NULL) {
2942		r = slurp_central_directory(a, zip);
2943		if (r != ARCHIVE_OK)
2944			return r;
2945		/* Get first entry whose local header offset is lower than
2946		 * other entries in the archive file. */
2947		zip->entry =
2948		    (struct zip_entry *)ARCHIVE_RB_TREE_MIN(&zip->tree);
2949	} else if (zip->entry != NULL) {
2950		/* Get next entry in local header offset order. */
2951		zip->entry = (struct zip_entry *)__archive_rb_tree_iterate(
2952		    &zip->tree, &zip->entry->node, ARCHIVE_RB_DIR_RIGHT);
2953	}
2954
2955	if (zip->entry == NULL)
2956		return ARCHIVE_EOF;
2957
2958	if (zip->entry->rsrcname.s)
2959		rsrc = (struct zip_entry *)__archive_rb_tree_find_node(
2960		    &zip->tree_rsrc, zip->entry->rsrcname.s);
2961	else
2962		rsrc = NULL;
2963
2964	if (zip->cctx_valid)
2965		archive_decrypto_aes_ctr_release(&zip->cctx);
2966	if (zip->hctx_valid)
2967		archive_hmac_sha1_cleanup(&zip->hctx);
2968	zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
2969	__archive_read_reset_passphrase(a);
2970
2971	/* File entries are sorted by the header offset, we should mostly
2972	 * use __archive_read_consume to advance a read point to avoid redundant
2973	 * data reading.  */
2974	offset = archive_filter_bytes(&a->archive, 0);
2975	if (offset < zip->entry->local_header_offset)
2976		__archive_read_consume(a,
2977		    zip->entry->local_header_offset - offset);
2978	else if (offset != zip->entry->local_header_offset) {
2979		__archive_read_seek(a, zip->entry->local_header_offset,
2980		    SEEK_SET);
2981	}
2982	zip->unconsumed = 0;
2983	r = zip_read_local_file_header(a, entry, zip);
2984	if (r != ARCHIVE_OK)
2985		return r;
2986	if (rsrc) {
2987		int ret2 = zip_read_mac_metadata(a, entry, rsrc);
2988		if (ret2 < ret)
2989			ret = ret2;
2990	}
2991	return (ret);
2992}
2993
2994/*
2995 * We're going to seek for the next header anyway, so we don't
2996 * need to bother doing anything here.
2997 */
2998static int
2999archive_read_format_zip_read_data_skip_seekable(struct archive_read *a)
3000{
3001	struct zip *zip;
3002	zip = (struct zip *)(a->format->data);
3003
3004	zip->unconsumed = 0;
3005	return (ARCHIVE_OK);
3006}
3007
3008int
3009archive_read_support_format_zip_seekable(struct archive *_a)
3010{
3011	struct archive_read *a = (struct archive_read *)_a;
3012	struct zip *zip;
3013	int r;
3014
3015	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
3016	    ARCHIVE_STATE_NEW, "archive_read_support_format_zip_seekable");
3017
3018	zip = (struct zip *)calloc(1, sizeof(*zip));
3019	if (zip == NULL) {
3020		archive_set_error(&a->archive, ENOMEM,
3021		    "Can't allocate zip data");
3022		return (ARCHIVE_FATAL);
3023	}
3024
3025#ifdef HAVE_COPYFILE_H
3026	/* Set this by default on Mac OS. */
3027	zip->process_mac_extensions = 1;
3028#endif
3029
3030	/*
3031	 * Until enough data has been read, we cannot tell about
3032	 * any encrypted entries yet.
3033	 */
3034	zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
3035	zip->crc32func = real_crc32;
3036
3037	r = __archive_read_register_format(a,
3038	    zip,
3039	    "zip",
3040	    archive_read_format_zip_seekable_bid,
3041	    archive_read_format_zip_options,
3042	    archive_read_format_zip_seekable_read_header,
3043	    archive_read_format_zip_read_data,
3044	    archive_read_format_zip_read_data_skip_seekable,
3045	    NULL,
3046	    archive_read_format_zip_cleanup,
3047	    archive_read_support_format_zip_capabilities_seekable,
3048	    archive_read_format_zip_has_encrypted_entries);
3049
3050	if (r != ARCHIVE_OK)
3051		free(zip);
3052	return (ARCHIVE_OK);
3053}
3054