archive_read_support_format_zip.c revision 353376
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/11/contrib/libarchive/libarchive/archive_read_support_format_zip.c 353376 2019-10-09 22:19:06Z 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#ifdef HAVE_BZLIB_H
56#include <bzlib.h>
57#endif
58#ifdef HAVE_LZMA_H
59#include <lzma.h>
60#endif
61
62#include "archive.h"
63#include "archive_digest_private.h"
64#include "archive_cryptor_private.h"
65#include "archive_endian.h"
66#include "archive_entry.h"
67#include "archive_entry_locale.h"
68#include "archive_hmac_private.h"
69#include "archive_private.h"
70#include "archive_rb.h"
71#include "archive_read_private.h"
72#include "archive_ppmd8_private.h"
73
74#ifndef HAVE_ZLIB_H
75#include "archive_crc32.h"
76#endif
77
78struct zip_entry {
79	struct archive_rb_node	node;
80	struct zip_entry	*next;
81	int64_t			local_header_offset;
82	int64_t			compressed_size;
83	int64_t			uncompressed_size;
84	int64_t			gid;
85	int64_t			uid;
86	struct archive_string	rsrcname;
87	time_t			mtime;
88	time_t			atime;
89	time_t			ctime;
90	uint32_t		crc32;
91	uint16_t		mode;
92	uint16_t		zip_flags; /* From GP Flags Field */
93	unsigned char		compression;
94	unsigned char		system; /* From "version written by" */
95	unsigned char		flags; /* Our extra markers. */
96	unsigned char		decdat;/* Used for Decryption check */
97
98	/* WinZip AES encryption extra field should be available
99	 * when compression is 99. */
100	struct {
101		/* Vendor version: AE-1 - 0x0001, AE-2 - 0x0002 */
102		unsigned	vendor;
103#define AES_VENDOR_AE_1	0x0001
104#define AES_VENDOR_AE_2	0x0002
105		/* AES encryption strength:
106		 * 1 - 128 bits, 2 - 192 bits, 2 - 256 bits. */
107		unsigned	strength;
108		/* Actual compression method. */
109		unsigned char	compression;
110	}			aes_extra;
111};
112
113struct trad_enc_ctx {
114	uint32_t	keys[3];
115};
116
117/* Bits used in zip_flags. */
118#define ZIP_ENCRYPTED	(1 << 0)
119#define ZIP_LENGTH_AT_END	(1 << 3)
120#define ZIP_STRONG_ENCRYPTED	(1 << 6)
121#define ZIP_UTF8_NAME	(1 << 11)
122/* See "7.2 Single Password Symmetric Encryption Method"
123   in http://www.pkware.com/documents/casestudies/APPNOTE.TXT */
124#define ZIP_CENTRAL_DIRECTORY_ENCRYPTED	(1 << 13)
125
126/* Bits used in flags. */
127#define LA_USED_ZIP64	(1 << 0)
128#define LA_FROM_CENTRAL_DIRECTORY (1 << 1)
129
130/*
131 * See "WinZip - AES Encryption Information"
132 *     http://www.winzip.com/aes_info.htm
133 */
134/* Value used in compression method. */
135#define WINZIP_AES_ENCRYPTION	99
136/* Authentication code size. */
137#define AUTH_CODE_SIZE	10
138/**/
139#define MAX_DERIVED_KEY_BUF_SIZE	(AES_MAX_KEY_SIZE * 2 + 2)
140
141struct zip {
142	/* Structural information about the archive. */
143	struct archive_string	format_name;
144	int64_t			central_directory_offset;
145	size_t			central_directory_entries_total;
146	size_t			central_directory_entries_on_this_disk;
147	int			has_encrypted_entries;
148
149	/* List of entries (seekable Zip only) */
150	struct zip_entry	*zip_entries;
151	struct archive_rb_tree	tree;
152	struct archive_rb_tree	tree_rsrc;
153
154	/* Bytes read but not yet consumed via __archive_read_consume() */
155	size_t			unconsumed;
156
157	/* Information about entry we're currently reading. */
158	struct zip_entry	*entry;
159	int64_t			entry_bytes_remaining;
160
161	/* These count the number of bytes actually read for the entry. */
162	int64_t			entry_compressed_bytes_read;
163	int64_t			entry_uncompressed_bytes_read;
164
165	/* Running CRC32 of the decompressed data */
166	unsigned long		entry_crc32;
167	unsigned long		(*crc32func)(unsigned long, const void *,
168				    size_t);
169	char			ignore_crc32;
170
171	/* Flags to mark progress of decompression. */
172	char			decompress_init;
173	char			end_of_entry;
174
175	unsigned char 		*uncompressed_buffer;
176	size_t 			uncompressed_buffer_size;
177
178#ifdef HAVE_ZLIB_H
179	z_stream		stream;
180	char			stream_valid;
181#endif
182
183#if HAVE_LZMA_H && HAVE_LIBLZMA
184	lzma_stream		zipx_lzma_stream;
185	char            zipx_lzma_valid;
186#endif
187
188#ifdef HAVE_BZLIB_H
189	bz_stream		bzstream;
190	char            bzstream_valid;
191#endif
192
193	IByteIn			zipx_ppmd_stream;
194	ssize_t			zipx_ppmd_read_compressed;
195	CPpmd8			ppmd8;
196	char			ppmd8_valid;
197	char			ppmd8_stream_failed;
198
199	struct archive_string_conv *sconv;
200	struct archive_string_conv *sconv_default;
201	struct archive_string_conv *sconv_utf8;
202	int			init_default_conversion;
203	int			process_mac_extensions;
204
205	char			init_decryption;
206
207	/* Decryption buffer. */
208	/*
209	 * The decrypted data starts at decrypted_ptr and
210	 * extends for decrypted_bytes_remaining.  Decryption
211	 * adds new data to the end of this block, data is returned
212	 * to clients from the beginning.  When the block hits the
213	 * end of decrypted_buffer, it has to be shuffled back to
214	 * the beginning of the buffer.
215	 */
216	unsigned char 		*decrypted_buffer;
217	unsigned char 		*decrypted_ptr;
218	size_t 			decrypted_buffer_size;
219	size_t 			decrypted_bytes_remaining;
220	size_t 			decrypted_unconsumed_bytes;
221
222	/* Traditional PKWARE decryption. */
223	struct trad_enc_ctx	tctx;
224	char			tctx_valid;
225
226	/* WinZip AES decryption. */
227	/* Contexts used for AES decryption. */
228	archive_crypto_ctx	cctx;
229	char			cctx_valid;
230	archive_hmac_sha1_ctx	hctx;
231	char			hctx_valid;
232
233	/* Strong encryption's decryption header information. */
234	unsigned		iv_size;
235	unsigned		alg_id;
236	unsigned		bit_len;
237	unsigned		flags;
238	unsigned		erd_size;
239	unsigned		v_size;
240	unsigned		v_crc32;
241	uint8_t			*iv;
242	uint8_t			*erd;
243	uint8_t			*v_data;
244};
245
246/* Many systems define min or MIN, but not all. */
247#define	zipmin(a,b) ((a) < (b) ? (a) : (b))
248
249/* This function is used by Ppmd8_DecodeSymbol during decompression of Ppmd8
250 * streams inside ZIP files. It has 2 purposes: one is to fetch the next
251 * compressed byte from the stream, second one is to increase the counter how
252 * many compressed bytes were read. */
253static Byte
254ppmd_read(void* p) {
255	/* Get the handle to current decompression context. */
256	struct archive_read *a = ((IByteIn*)p)->a;
257	struct zip *zip = (struct zip*) a->format->data;
258	ssize_t bytes_avail = 0;
259
260	/* Fetch next byte. */
261	const uint8_t* data = __archive_read_ahead(a, 1, &bytes_avail);
262	if(bytes_avail < 1) {
263		zip->ppmd8_stream_failed = 1;
264		return 0;
265	}
266
267	__archive_read_consume(a, 1);
268
269	/* Increment the counter. */
270	++zip->zipx_ppmd_read_compressed;
271
272	/* Return the next compressed byte. */
273	return data[0];
274}
275
276/* ------------------------------------------------------------------------ */
277
278/*
279  Traditional PKWARE Decryption functions.
280 */
281
282static void
283trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
284{
285	uint8_t t;
286#define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
287
288	ctx->keys[0] = CRC32(ctx->keys[0], c);
289	ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
290	t = (ctx->keys[1] >> 24) & 0xff;
291	ctx->keys[2] = CRC32(ctx->keys[2], t);
292#undef CRC32
293}
294
295static uint8_t
296trad_enc_decrypt_byte(struct trad_enc_ctx *ctx)
297{
298	unsigned temp = ctx->keys[2] | 2;
299	return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
300}
301
302static void
303trad_enc_decrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
304    size_t in_len, uint8_t *out, size_t out_len)
305{
306	unsigned i, max;
307
308	max = (unsigned)((in_len < out_len)? in_len: out_len);
309
310	for (i = 0; i < max; i++) {
311		uint8_t t = in[i] ^ trad_enc_decrypt_byte(ctx);
312		out[i] = t;
313		trad_enc_update_keys(ctx, t);
314	}
315}
316
317static int
318trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len,
319    const uint8_t *key, size_t key_len, uint8_t *crcchk)
320{
321	uint8_t header[12];
322
323	if (key_len < 12) {
324		*crcchk = 0xff;
325		return -1;
326	}
327
328	ctx->keys[0] = 305419896L;
329	ctx->keys[1] = 591751049L;
330	ctx->keys[2] = 878082192L;
331
332	for (;pw_len; --pw_len)
333		trad_enc_update_keys(ctx, *pw++);
334
335	trad_enc_decrypt_update(ctx, key, 12, header, 12);
336	/* Return the last byte for CRC check. */
337	*crcchk = header[11];
338	return 0;
339}
340
341#if 0
342static void
343crypt_derive_key_sha1(const void *p, int size, unsigned char *key,
344    int key_size)
345{
346#define MD_SIZE 20
347	archive_sha1_ctx ctx;
348	unsigned char md1[MD_SIZE];
349	unsigned char md2[MD_SIZE * 2];
350	unsigned char mkb[64];
351	int i;
352
353	archive_sha1_init(&ctx);
354	archive_sha1_update(&ctx, p, size);
355	archive_sha1_final(&ctx, md1);
356
357	memset(mkb, 0x36, sizeof(mkb));
358	for (i = 0; i < MD_SIZE; i++)
359		mkb[i] ^= md1[i];
360	archive_sha1_init(&ctx);
361	archive_sha1_update(&ctx, mkb, sizeof(mkb));
362	archive_sha1_final(&ctx, md2);
363
364	memset(mkb, 0x5C, sizeof(mkb));
365	for (i = 0; i < MD_SIZE; i++)
366		mkb[i] ^= md1[i];
367	archive_sha1_init(&ctx);
368	archive_sha1_update(&ctx, mkb, sizeof(mkb));
369	archive_sha1_final(&ctx, md2 + MD_SIZE);
370
371	if (key_size > 32)
372		key_size = 32;
373	memcpy(key, md2, key_size);
374#undef MD_SIZE
375}
376#endif
377
378/*
379 * Common code for streaming or seeking modes.
380 *
381 * Includes code to read local file headers, decompress data
382 * from entry bodies, and common API.
383 */
384
385static unsigned long
386real_crc32(unsigned long crc, const void *buff, size_t len)
387{
388	return crc32(crc, buff, (unsigned int)len);
389}
390
391/* Used by "ignorecrc32" option to speed up tests. */
392static unsigned long
393fake_crc32(unsigned long crc, const void *buff, size_t len)
394{
395	(void)crc; /* UNUSED */
396	(void)buff; /* UNUSED */
397	(void)len; /* UNUSED */
398	return 0;
399}
400
401static const struct {
402	int id;
403	const char * name;
404} compression_methods[] = {
405	{0, "uncompressed"}, /* The file is stored (no compression) */
406	{1, "shrinking"}, /* The file is Shrunk */
407	{2, "reduced-1"}, /* The file is Reduced with compression factor 1 */
408	{3, "reduced-2"}, /* The file is Reduced with compression factor 2 */
409	{4, "reduced-3"}, /* The file is Reduced with compression factor 3 */
410	{5, "reduced-4"}, /* The file is Reduced with compression factor 4 */
411	{6, "imploded"},  /* The file is Imploded */
412	{7, "reserved"},  /* Reserved for Tokenizing compression algorithm */
413	{8, "deflation"}, /* The file is Deflated */
414	{9, "deflation-64-bit"}, /* Enhanced Deflating using Deflate64(tm) */
415	{10, "ibm-terse"},/* PKWARE Data Compression Library Imploding
416			   * (old IBM TERSE) */
417	{11, "reserved"}, /* Reserved by PKWARE */
418	{12, "bzip"},     /* File is compressed using BZIP2 algorithm */
419	{13, "reserved"}, /* Reserved by PKWARE */
420	{14, "lzma"},     /* LZMA (EFS) */
421	{15, "reserved"}, /* Reserved by PKWARE */
422	{16, "reserved"}, /* Reserved by PKWARE */
423	{17, "reserved"}, /* Reserved by PKWARE */
424	{18, "ibm-terse-new"}, /* File is compressed using IBM TERSE (new) */
425	{19, "ibm-lz777"},/* IBM LZ77 z Architecture (PFS) */
426	{95, "xz"},       /* XZ compressed data */
427	{96, "jpeg"},     /* JPEG compressed data */
428	{97, "wav-pack"}, /* WavPack compressed data */
429	{98, "ppmd-1"},   /* PPMd version I, Rev 1 */
430	{99, "aes"}       /* WinZip AES encryption  */
431};
432
433static const char *
434compression_name(const int compression)
435{
436	static const int num_compression_methods =
437		sizeof(compression_methods)/sizeof(compression_methods[0]);
438	int i=0;
439
440	while(compression >= 0 && i < num_compression_methods) {
441		if (compression_methods[i].id == compression)
442			return compression_methods[i].name;
443		i++;
444	}
445	return "??";
446}
447
448/* Convert an MSDOS-style date/time into Unix-style time. */
449static time_t
450zip_time(const char *p)
451{
452	int msTime, msDate;
453	struct tm ts;
454
455	msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
456	msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
457
458	memset(&ts, 0, sizeof(ts));
459	ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
460	ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
461	ts.tm_mday = msDate & 0x1f; /* Day of month. */
462	ts.tm_hour = (msTime >> 11) & 0x1f;
463	ts.tm_min = (msTime >> 5) & 0x3f;
464	ts.tm_sec = (msTime << 1) & 0x3e;
465	ts.tm_isdst = -1;
466	return mktime(&ts);
467}
468
469/*
470 * The extra data is stored as a list of
471 *	id1+size1+data1 + id2+size2+data2 ...
472 *  triplets.  id and size are 2 bytes each.
473 */
474static int
475process_extra(struct archive_read *a, struct archive_entry *entry,
476     const char *p, size_t extra_length, struct zip_entry* zip_entry)
477{
478	unsigned offset = 0;
479	struct zip *zip = (struct zip *)(a->format->data);
480
481	if (extra_length == 0) {
482		return ARCHIVE_OK;
483	}
484
485	if (extra_length < 4) {
486		size_t i = 0;
487		/* Some ZIP files may have trailing 0 bytes. Let's check they
488		 * are all 0 and ignore them instead of returning an error.
489		 *
490		 * This is not technically correct, but some ZIP files look
491		 * like this and other tools support those files - so let's
492		 * also  support them.
493		 */
494		for (; i < extra_length; i++) {
495			if (p[i] != 0) {
496				archive_set_error(&a->archive,
497				    ARCHIVE_ERRNO_FILE_FORMAT,
498				    "Too-small extra data: "
499				    "Need at least 4 bytes, "
500				    "but only found %d bytes",
501				    (int)extra_length);
502				return ARCHIVE_FAILED;
503			}
504		}
505
506		return ARCHIVE_OK;
507	}
508
509	while (offset <= extra_length - 4) {
510		unsigned short headerid = archive_le16dec(p + offset);
511		unsigned short datasize = archive_le16dec(p + offset + 2);
512
513		offset += 4;
514		if (offset + datasize > extra_length) {
515			archive_set_error(&a->archive,
516			    ARCHIVE_ERRNO_FILE_FORMAT, "Extra data overflow: "
517			    "Need %d bytes but only found %d bytes",
518			    (int)datasize, (int)(extra_length - offset));
519			return ARCHIVE_FAILED;
520		}
521#ifdef DEBUG
522		fprintf(stderr, "Header id 0x%04x, length %d\n",
523		    headerid, datasize);
524#endif
525		switch (headerid) {
526		case 0x0001:
527			/* Zip64 extended information extra field. */
528			zip_entry->flags |= LA_USED_ZIP64;
529			if (zip_entry->uncompressed_size == 0xffffffff) {
530				uint64_t t = 0;
531				if (datasize < 8
532				    || (t = archive_le64dec(p + offset)) >
533				    INT64_MAX) {
534					archive_set_error(&a->archive,
535					    ARCHIVE_ERRNO_FILE_FORMAT,
536					    "Malformed 64-bit "
537					    "uncompressed size");
538					return ARCHIVE_FAILED;
539				}
540				zip_entry->uncompressed_size = t;
541				offset += 8;
542				datasize -= 8;
543			}
544			if (zip_entry->compressed_size == 0xffffffff) {
545				uint64_t t = 0;
546				if (datasize < 8
547				    || (t = archive_le64dec(p + offset)) >
548				    INT64_MAX) {
549					archive_set_error(&a->archive,
550					    ARCHIVE_ERRNO_FILE_FORMAT,
551					    "Malformed 64-bit "
552					    "compressed size");
553					return ARCHIVE_FAILED;
554				}
555				zip_entry->compressed_size = t;
556				offset += 8;
557				datasize -= 8;
558			}
559			if (zip_entry->local_header_offset == 0xffffffff) {
560				uint64_t t = 0;
561				if (datasize < 8
562				    || (t = archive_le64dec(p + offset)) >
563				    INT64_MAX) {
564					archive_set_error(&a->archive,
565					    ARCHIVE_ERRNO_FILE_FORMAT,
566					    "Malformed 64-bit "
567					    "local header offset");
568					return ARCHIVE_FAILED;
569				}
570				zip_entry->local_header_offset = t;
571				offset += 8;
572				datasize -= 8;
573			}
574			/* archive_le32dec(p + offset) gives disk
575			 * on which file starts, but we don't handle
576			 * multi-volume Zip files. */
577			break;
578#ifdef DEBUG
579		case 0x0017:
580		{
581			/* Strong encryption field. */
582			if (archive_le16dec(p + offset) == 2) {
583				unsigned algId =
584					archive_le16dec(p + offset + 2);
585				unsigned bitLen =
586					archive_le16dec(p + offset + 4);
587				int	 flags =
588					archive_le16dec(p + offset + 6);
589				fprintf(stderr, "algId=0x%04x, bitLen=%u, "
590				    "flgas=%d\n", algId, bitLen,flags);
591			}
592			break;
593		}
594#endif
595		case 0x5455:
596		{
597			/* Extended time field "UT". */
598			int flags;
599			if (datasize == 0) {
600				archive_set_error(&a->archive,
601				    ARCHIVE_ERRNO_FILE_FORMAT,
602				    "Incomplete extended time field");
603				return ARCHIVE_FAILED;
604			}
605			flags = p[offset];
606			offset++;
607			datasize--;
608			/* Flag bits indicate which dates are present. */
609			if (flags & 0x01)
610			{
611#ifdef DEBUG
612				fprintf(stderr, "mtime: %lld -> %d\n",
613				    (long long)zip_entry->mtime,
614				    archive_le32dec(p + offset));
615#endif
616				if (datasize < 4)
617					break;
618				zip_entry->mtime = archive_le32dec(p + offset);
619				offset += 4;
620				datasize -= 4;
621			}
622			if (flags & 0x02)
623			{
624				if (datasize < 4)
625					break;
626				zip_entry->atime = archive_le32dec(p + offset);
627				offset += 4;
628				datasize -= 4;
629			}
630			if (flags & 0x04)
631			{
632				if (datasize < 4)
633					break;
634				zip_entry->ctime = archive_le32dec(p + offset);
635				offset += 4;
636				datasize -= 4;
637			}
638			break;
639		}
640		case 0x5855:
641		{
642			/* Info-ZIP Unix Extra Field (old version) "UX". */
643			if (datasize >= 8) {
644				zip_entry->atime = archive_le32dec(p + offset);
645				zip_entry->mtime =
646				    archive_le32dec(p + offset + 4);
647			}
648			if (datasize >= 12) {
649				zip_entry->uid =
650				    archive_le16dec(p + offset + 8);
651				zip_entry->gid =
652				    archive_le16dec(p + offset + 10);
653			}
654			break;
655		}
656		case 0x6c78:
657		{
658			/* Experimental 'xl' field */
659			/*
660			 * Introduced Dec 2013 to provide a way to
661			 * include external file attributes (and other
662			 * fields that ordinarily appear only in
663			 * central directory) in local file header.
664			 * This provides file type and permission
665			 * information necessary to support full
666			 * streaming extraction.  Currently being
667			 * discussed with other Zip developers
668			 * ... subject to change.
669			 *
670			 * Format:
671			 *  The field starts with a bitmap that specifies
672			 *  which additional fields are included.  The
673			 *  bitmap is variable length and can be extended in
674			 *  the future.
675			 *
676			 *  n bytes - feature bitmap: first byte has low-order
677			 *    7 bits.  If high-order bit is set, a subsequent
678			 *    byte holds the next 7 bits, etc.
679			 *
680			 *  if bitmap & 1, 2 byte "version made by"
681			 *  if bitmap & 2, 2 byte "internal file attributes"
682			 *  if bitmap & 4, 4 byte "external file attributes"
683			 *  if bitmap & 8, 2 byte comment length + n byte
684			 *  comment
685			 */
686			int bitmap, bitmap_last;
687
688			if (datasize < 1)
689				break;
690			bitmap_last = bitmap = 0xff & p[offset];
691			offset += 1;
692			datasize -= 1;
693
694			/* We only support first 7 bits of bitmap; skip rest. */
695			while ((bitmap_last & 0x80) != 0
696			    && datasize >= 1) {
697				bitmap_last = p[offset];
698				offset += 1;
699				datasize -= 1;
700			}
701
702			if (bitmap & 1) {
703				/* 2 byte "version made by" */
704				if (datasize < 2)
705					break;
706				zip_entry->system
707				    = archive_le16dec(p + offset) >> 8;
708				offset += 2;
709				datasize -= 2;
710			}
711			if (bitmap & 2) {
712				/* 2 byte "internal file attributes" */
713				uint32_t internal_attributes;
714				if (datasize < 2)
715					break;
716				internal_attributes
717				    = archive_le16dec(p + offset);
718				/* Not used by libarchive at present. */
719				(void)internal_attributes; /* UNUSED */
720				offset += 2;
721				datasize -= 2;
722			}
723			if (bitmap & 4) {
724				/* 4 byte "external file attributes" */
725				uint32_t external_attributes;
726				if (datasize < 4)
727					break;
728				external_attributes
729				    = archive_le32dec(p + offset);
730				if (zip_entry->system == 3) {
731					zip_entry->mode
732					    = external_attributes >> 16;
733				} else if (zip_entry->system == 0) {
734					// Interpret MSDOS directory bit
735					if (0x10 == (external_attributes &
736					    0x10)) {
737						zip_entry->mode =
738						    AE_IFDIR | 0775;
739					} else {
740						zip_entry->mode =
741						    AE_IFREG | 0664;
742					}
743					if (0x01 == (external_attributes &
744					    0x01)) {
745						/* Read-only bit;
746						 * strip write permissions */
747						zip_entry->mode &= 0555;
748					}
749				} else {
750					zip_entry->mode = 0;
751				}
752				offset += 4;
753				datasize -= 4;
754			}
755			if (bitmap & 8) {
756				/* 2 byte comment length + comment */
757				uint32_t comment_length;
758				if (datasize < 2)
759					break;
760				comment_length
761				    = archive_le16dec(p + offset);
762				offset += 2;
763				datasize -= 2;
764
765				if (datasize < comment_length)
766					break;
767				/* Comment is not supported by libarchive */
768				offset += comment_length;
769				datasize -= comment_length;
770			}
771			break;
772		}
773		case 0x7075:
774		{
775			/* Info-ZIP Unicode Path Extra Field. */
776			if (datasize < 5 || entry == NULL)
777				break;
778			offset += 5;
779			datasize -= 5;
780
781			/* The path name in this field is always encoded
782			 * in UTF-8. */
783			if (zip->sconv_utf8 == NULL) {
784				zip->sconv_utf8 =
785					archive_string_conversion_from_charset(
786					&a->archive, "UTF-8", 1);
787				/* If the converter from UTF-8 is not
788				 * available, then the path name from the main
789				 * field will more likely be correct. */
790				if (zip->sconv_utf8 == NULL)
791					break;
792			}
793
794			/* Make sure the CRC32 of the filename matches. */
795			if (!zip->ignore_crc32) {
796				const char *cp = archive_entry_pathname(entry);
797				if (cp) {
798					unsigned long file_crc =
799					    zip->crc32func(0, cp, strlen(cp));
800					unsigned long utf_crc =
801					    archive_le32dec(p + offset - 4);
802					if (file_crc != utf_crc) {
803#ifdef DEBUG
804						fprintf(stderr,
805						    "CRC filename mismatch; "
806						    "CDE is %lx, but UTF8 "
807						    "is outdated with %lx\n",
808						    file_crc, utf_crc);
809#endif
810						break;
811					}
812				}
813			}
814
815			if (archive_entry_copy_pathname_l(entry,
816			    p + offset, datasize, zip->sconv_utf8) != 0) {
817				/* Ignore the error, and fallback to the path
818				 * name from the main field. */
819#ifdef DEBUG
820				fprintf(stderr, "Failed to read the ZIP "
821				    "0x7075 extra field path.\n");
822#endif
823			}
824			break;
825		}
826		case 0x7855:
827			/* Info-ZIP Unix Extra Field (type 2) "Ux". */
828#ifdef DEBUG
829			fprintf(stderr, "uid %d gid %d\n",
830			    archive_le16dec(p + offset),
831			    archive_le16dec(p + offset + 2));
832#endif
833			if (datasize >= 2)
834				zip_entry->uid = archive_le16dec(p + offset);
835			if (datasize >= 4)
836				zip_entry->gid =
837				    archive_le16dec(p + offset + 2);
838			break;
839		case 0x7875:
840		{
841			/* Info-Zip Unix Extra Field (type 3) "ux". */
842			int uidsize = 0, gidsize = 0;
843
844			/* TODO: support arbitrary uidsize/gidsize. */
845			if (datasize >= 1 && p[offset] == 1) {/* version=1 */
846				if (datasize >= 4) {
847					/* get a uid size. */
848					uidsize = 0xff & (int)p[offset+1];
849					if (uidsize == 2)
850						zip_entry->uid =
851						    archive_le16dec(
852						        p + offset + 2);
853					else if (uidsize == 4 && datasize >= 6)
854						zip_entry->uid =
855						    archive_le32dec(
856						        p + offset + 2);
857				}
858				if (datasize >= (2 + uidsize + 3)) {
859					/* get a gid size. */
860					gidsize = 0xff &
861					    (int)p[offset+2+uidsize];
862					if (gidsize == 2)
863						zip_entry->gid =
864						    archive_le16dec(
865						        p+offset+2+uidsize+1);
866					else if (gidsize == 4 &&
867					    datasize >= (2 + uidsize + 5))
868						zip_entry->gid =
869						    archive_le32dec(
870						        p+offset+2+uidsize+1);
871				}
872			}
873			break;
874		}
875		case 0x9901:
876			/* WinZip AES extra data field. */
877			if (datasize < 6) {
878				archive_set_error(&a->archive,
879				    ARCHIVE_ERRNO_FILE_FORMAT,
880				    "Incomplete AES field");
881				return ARCHIVE_FAILED;
882			}
883			if (p[offset + 2] == 'A' && p[offset + 3] == 'E') {
884				/* Vendor version. */
885				zip_entry->aes_extra.vendor =
886				    archive_le16dec(p + offset);
887				/* AES encryption strength. */
888				zip_entry->aes_extra.strength = p[offset + 4];
889				/* Actual compression method. */
890				zip_entry->aes_extra.compression =
891				    p[offset + 5];
892			}
893			break;
894		default:
895			break;
896		}
897		offset += datasize;
898	}
899	return ARCHIVE_OK;
900}
901
902/*
903 * Assumes file pointer is at beginning of local file header.
904 */
905static int
906zip_read_local_file_header(struct archive_read *a, struct archive_entry *entry,
907    struct zip *zip)
908{
909	const char *p;
910	const void *h;
911	const wchar_t *wp;
912	const char *cp;
913	size_t len, filename_length, extra_length;
914	struct archive_string_conv *sconv;
915	struct zip_entry *zip_entry = zip->entry;
916	struct zip_entry zip_entry_central_dir;
917	int ret = ARCHIVE_OK;
918	char version;
919
920	/* Save a copy of the original for consistency checks. */
921	zip_entry_central_dir = *zip_entry;
922
923	zip->decompress_init = 0;
924	zip->end_of_entry = 0;
925	zip->entry_uncompressed_bytes_read = 0;
926	zip->entry_compressed_bytes_read = 0;
927	zip->entry_crc32 = zip->crc32func(0, NULL, 0);
928
929	/* Setup default conversion. */
930	if (zip->sconv == NULL && !zip->init_default_conversion) {
931		zip->sconv_default =
932		    archive_string_default_conversion_for_read(&(a->archive));
933		zip->init_default_conversion = 1;
934	}
935
936	if ((p = __archive_read_ahead(a, 30, NULL)) == NULL) {
937		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
938		    "Truncated ZIP file header");
939		return (ARCHIVE_FATAL);
940	}
941
942	if (memcmp(p, "PK\003\004", 4) != 0) {
943		archive_set_error(&a->archive, -1, "Damaged Zip archive");
944		return ARCHIVE_FATAL;
945	}
946	version = p[4];
947	zip_entry->system = p[5];
948	zip_entry->zip_flags = archive_le16dec(p + 6);
949	if (zip_entry->zip_flags & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)) {
950		zip->has_encrypted_entries = 1;
951		archive_entry_set_is_data_encrypted(entry, 1);
952		if (zip_entry->zip_flags & ZIP_CENTRAL_DIRECTORY_ENCRYPTED &&
953			zip_entry->zip_flags & ZIP_ENCRYPTED &&
954			zip_entry->zip_flags & ZIP_STRONG_ENCRYPTED) {
955			archive_entry_set_is_metadata_encrypted(entry, 1);
956			return ARCHIVE_FATAL;
957		}
958	}
959	zip->init_decryption = (zip_entry->zip_flags & ZIP_ENCRYPTED);
960	zip_entry->compression = (char)archive_le16dec(p + 8);
961	zip_entry->mtime = zip_time(p + 10);
962	zip_entry->crc32 = archive_le32dec(p + 14);
963	if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
964		zip_entry->decdat = p[11];
965	else
966		zip_entry->decdat = p[17];
967	zip_entry->compressed_size = archive_le32dec(p + 18);
968	zip_entry->uncompressed_size = archive_le32dec(p + 22);
969	filename_length = archive_le16dec(p + 26);
970	extra_length = archive_le16dec(p + 28);
971
972	__archive_read_consume(a, 30);
973
974	/* Read the filename. */
975	if ((h = __archive_read_ahead(a, filename_length, NULL)) == NULL) {
976		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
977		    "Truncated ZIP file header");
978		return (ARCHIVE_FATAL);
979	}
980	if (zip_entry->zip_flags & ZIP_UTF8_NAME) {
981		/* The filename is stored to be UTF-8. */
982		if (zip->sconv_utf8 == NULL) {
983			zip->sconv_utf8 =
984			    archive_string_conversion_from_charset(
985				&a->archive, "UTF-8", 1);
986			if (zip->sconv_utf8 == NULL)
987				return (ARCHIVE_FATAL);
988		}
989		sconv = zip->sconv_utf8;
990	} else if (zip->sconv != NULL)
991		sconv = zip->sconv;
992	else
993		sconv = zip->sconv_default;
994
995	if (archive_entry_copy_pathname_l(entry,
996	    h, filename_length, sconv) != 0) {
997		if (errno == ENOMEM) {
998			archive_set_error(&a->archive, ENOMEM,
999			    "Can't allocate memory for Pathname");
1000			return (ARCHIVE_FATAL);
1001		}
1002		archive_set_error(&a->archive,
1003		    ARCHIVE_ERRNO_FILE_FORMAT,
1004		    "Pathname cannot be converted "
1005		    "from %s to current locale.",
1006		    archive_string_conversion_charset_name(sconv));
1007		ret = ARCHIVE_WARN;
1008	}
1009	__archive_read_consume(a, filename_length);
1010
1011	/* Read the extra data. */
1012	if ((h = __archive_read_ahead(a, extra_length, NULL)) == NULL) {
1013		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1014		    "Truncated ZIP file header");
1015		return (ARCHIVE_FATAL);
1016	}
1017
1018	if (ARCHIVE_OK != process_extra(a, entry, h, extra_length,
1019	    zip_entry)) {
1020		return ARCHIVE_FATAL;
1021	}
1022	__archive_read_consume(a, extra_length);
1023
1024	/* Work around a bug in Info-Zip: When reading from a pipe, it
1025	 * stats the pipe instead of synthesizing a file entry. */
1026	if ((zip_entry->mode & AE_IFMT) == AE_IFIFO) {
1027		zip_entry->mode &= ~ AE_IFMT;
1028		zip_entry->mode |= AE_IFREG;
1029	}
1030
1031	/* If the mode is totally empty, set some sane default. */
1032	if (zip_entry->mode == 0) {
1033		zip_entry->mode |= 0664;
1034	}
1035
1036	/* Windows archivers sometimes use backslash as the directory
1037	 * separator. Normalize to slash. */
1038	if (zip_entry->system == 0 &&
1039	    (wp = archive_entry_pathname_w(entry)) != NULL) {
1040		if (wcschr(wp, L'/') == NULL && wcschr(wp, L'\\') != NULL) {
1041			size_t i;
1042			struct archive_wstring s;
1043			archive_string_init(&s);
1044			archive_wstrcpy(&s, wp);
1045			for (i = 0; i < archive_strlen(&s); i++) {
1046				if (s.s[i] == '\\')
1047					s.s[i] = '/';
1048			}
1049			archive_entry_copy_pathname_w(entry, s.s);
1050			archive_wstring_free(&s);
1051		}
1052	}
1053
1054	/* Make sure that entries with a trailing '/' are marked as directories
1055	 * even if the External File Attributes contains bogus values.  If this
1056	 * is not a directory and there is no type, assume a regular file. */
1057	if ((zip_entry->mode & AE_IFMT) != AE_IFDIR) {
1058		int has_slash;
1059
1060		wp = archive_entry_pathname_w(entry);
1061		if (wp != NULL) {
1062			len = wcslen(wp);
1063			has_slash = len > 0 && wp[len - 1] == L'/';
1064		} else {
1065			cp = archive_entry_pathname(entry);
1066			len = (cp != NULL)?strlen(cp):0;
1067			has_slash = len > 0 && cp[len - 1] == '/';
1068		}
1069		/* Correct file type as needed. */
1070		if (has_slash) {
1071			zip_entry->mode &= ~AE_IFMT;
1072			zip_entry->mode |= AE_IFDIR;
1073			zip_entry->mode |= 0111;
1074		} else if ((zip_entry->mode & AE_IFMT) == 0) {
1075			zip_entry->mode |= AE_IFREG;
1076		}
1077	}
1078
1079	/* Make sure directories end in '/' */
1080	if ((zip_entry->mode & AE_IFMT) == AE_IFDIR) {
1081		wp = archive_entry_pathname_w(entry);
1082		if (wp != NULL) {
1083			len = wcslen(wp);
1084			if (len > 0 && wp[len - 1] != L'/') {
1085				struct archive_wstring s;
1086				archive_string_init(&s);
1087				archive_wstrcat(&s, wp);
1088				archive_wstrappend_wchar(&s, L'/');
1089				archive_entry_copy_pathname_w(entry, s.s);
1090				archive_wstring_free(&s);
1091			}
1092		} else {
1093			cp = archive_entry_pathname(entry);
1094			len = (cp != NULL)?strlen(cp):0;
1095			if (len > 0 && cp[len - 1] != '/') {
1096				struct archive_string s;
1097				archive_string_init(&s);
1098				archive_strcat(&s, cp);
1099				archive_strappend_char(&s, '/');
1100				archive_entry_set_pathname(entry, s.s);
1101				archive_string_free(&s);
1102			}
1103		}
1104	}
1105
1106	if (zip_entry->flags & LA_FROM_CENTRAL_DIRECTORY) {
1107		/* If this came from the central dir, its size info
1108		 * is definitive, so ignore the length-at-end flag. */
1109		zip_entry->zip_flags &= ~ZIP_LENGTH_AT_END;
1110		/* If local header is missing a value, use the one from
1111		   the central directory.  If both have it, warn about
1112		   mismatches. */
1113		if (zip_entry->crc32 == 0) {
1114			zip_entry->crc32 = zip_entry_central_dir.crc32;
1115		} else if (!zip->ignore_crc32
1116		    && zip_entry->crc32 != zip_entry_central_dir.crc32) {
1117			archive_set_error(&a->archive,
1118			    ARCHIVE_ERRNO_FILE_FORMAT,
1119			    "Inconsistent CRC32 values");
1120			ret = ARCHIVE_WARN;
1121		}
1122		if (zip_entry->compressed_size == 0) {
1123			zip_entry->compressed_size
1124			    = zip_entry_central_dir.compressed_size;
1125		} else if (zip_entry->compressed_size
1126		    != zip_entry_central_dir.compressed_size) {
1127			archive_set_error(&a->archive,
1128			    ARCHIVE_ERRNO_FILE_FORMAT,
1129			    "Inconsistent compressed size: "
1130			    "%jd in central directory, %jd in local header",
1131			    (intmax_t)zip_entry_central_dir.compressed_size,
1132			    (intmax_t)zip_entry->compressed_size);
1133			ret = ARCHIVE_WARN;
1134		}
1135		if (zip_entry->uncompressed_size == 0) {
1136			zip_entry->uncompressed_size
1137			    = zip_entry_central_dir.uncompressed_size;
1138		} else if (zip_entry->uncompressed_size
1139		    != zip_entry_central_dir.uncompressed_size) {
1140			archive_set_error(&a->archive,
1141			    ARCHIVE_ERRNO_FILE_FORMAT,
1142			    "Inconsistent uncompressed size: "
1143			    "%jd in central directory, %jd in local header",
1144			    (intmax_t)zip_entry_central_dir.uncompressed_size,
1145			    (intmax_t)zip_entry->uncompressed_size);
1146			ret = ARCHIVE_WARN;
1147		}
1148	}
1149
1150	/* Populate some additional entry fields: */
1151	archive_entry_set_mode(entry, zip_entry->mode);
1152	archive_entry_set_uid(entry, zip_entry->uid);
1153	archive_entry_set_gid(entry, zip_entry->gid);
1154	archive_entry_set_mtime(entry, zip_entry->mtime, 0);
1155	archive_entry_set_ctime(entry, zip_entry->ctime, 0);
1156	archive_entry_set_atime(entry, zip_entry->atime, 0);
1157
1158	if ((zip->entry->mode & AE_IFMT) == AE_IFLNK) {
1159		size_t linkname_length;
1160
1161		if (zip_entry->compressed_size > 64 * 1024) {
1162			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1163			    "Zip file with oversized link entry");
1164			return ARCHIVE_FATAL;
1165		}
1166
1167		linkname_length = (size_t)zip_entry->compressed_size;
1168
1169		archive_entry_set_size(entry, 0);
1170		p = __archive_read_ahead(a, linkname_length, NULL);
1171		if (p == NULL) {
1172			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1173			    "Truncated Zip file");
1174			return ARCHIVE_FATAL;
1175		}
1176
1177		sconv = zip->sconv;
1178		if (sconv == NULL && (zip->entry->zip_flags & ZIP_UTF8_NAME))
1179			sconv = zip->sconv_utf8;
1180		if (sconv == NULL)
1181			sconv = zip->sconv_default;
1182		if (archive_entry_copy_symlink_l(entry, p, linkname_length,
1183		    sconv) != 0) {
1184			if (errno != ENOMEM && sconv == zip->sconv_utf8 &&
1185			    (zip->entry->zip_flags & ZIP_UTF8_NAME))
1186			    archive_entry_copy_symlink_l(entry, p,
1187				linkname_length, NULL);
1188			if (errno == ENOMEM) {
1189				archive_set_error(&a->archive, ENOMEM,
1190				    "Can't allocate memory for Symlink");
1191				return (ARCHIVE_FATAL);
1192			}
1193			/*
1194			 * Since there is no character-set regulation for
1195			 * symlink name, do not report the conversion error
1196			 * in an automatic conversion.
1197			 */
1198			if (sconv != zip->sconv_utf8 ||
1199			    (zip->entry->zip_flags & ZIP_UTF8_NAME) == 0) {
1200				archive_set_error(&a->archive,
1201				    ARCHIVE_ERRNO_FILE_FORMAT,
1202				    "Symlink cannot be converted "
1203				    "from %s to current locale.",
1204				    archive_string_conversion_charset_name(
1205					sconv));
1206				ret = ARCHIVE_WARN;
1207			}
1208		}
1209		zip_entry->uncompressed_size = zip_entry->compressed_size = 0;
1210
1211		if (__archive_read_consume(a, linkname_length) < 0) {
1212			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1213			    "Read error skipping symlink target name");
1214			return ARCHIVE_FATAL;
1215		}
1216	} else if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
1217	    || zip_entry->uncompressed_size > 0) {
1218		/* Set the size only if it's meaningful. */
1219		archive_entry_set_size(entry, zip_entry->uncompressed_size);
1220	}
1221	zip->entry_bytes_remaining = zip_entry->compressed_size;
1222
1223	/* If there's no body, force read_data() to return EOF immediately. */
1224	if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
1225	    && zip->entry_bytes_remaining < 1)
1226		zip->end_of_entry = 1;
1227
1228	/* Set up a more descriptive format name. */
1229        archive_string_empty(&zip->format_name);
1230	archive_string_sprintf(&zip->format_name, "ZIP %d.%d (%s)",
1231	    version / 10, version % 10,
1232	    compression_name(zip->entry->compression));
1233	a->archive.archive_format_name = zip->format_name.s;
1234
1235	return (ret);
1236}
1237
1238static int
1239check_authentication_code(struct archive_read *a, const void *_p)
1240{
1241	struct zip *zip = (struct zip *)(a->format->data);
1242
1243	/* Check authentication code. */
1244	if (zip->hctx_valid) {
1245		const void *p;
1246		uint8_t hmac[20];
1247		size_t hmac_len = 20;
1248		int cmp;
1249
1250		archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
1251		if (_p == NULL) {
1252			/* Read authentication code. */
1253			p = __archive_read_ahead(a, AUTH_CODE_SIZE, NULL);
1254			if (p == NULL) {
1255				archive_set_error(&a->archive,
1256				    ARCHIVE_ERRNO_FILE_FORMAT,
1257				    "Truncated ZIP file data");
1258				return (ARCHIVE_FATAL);
1259			}
1260		} else {
1261			p = _p;
1262		}
1263		cmp = memcmp(hmac, p, AUTH_CODE_SIZE);
1264		__archive_read_consume(a, AUTH_CODE_SIZE);
1265		if (cmp != 0) {
1266			archive_set_error(&a->archive,
1267			    ARCHIVE_ERRNO_MISC,
1268			    "ZIP bad Authentication code");
1269			return (ARCHIVE_WARN);
1270		}
1271	}
1272	return (ARCHIVE_OK);
1273}
1274
1275/*
1276 * Read "uncompressed" data.  There are three cases:
1277 *  1) We know the size of the data.  This is always true for the
1278 * seeking reader (we've examined the Central Directory already).
1279 *  2) ZIP_LENGTH_AT_END was set, but only the CRC was deferred.
1280 * Info-ZIP seems to do this; we know the size but have to grab
1281 * the CRC from the data descriptor afterwards.
1282 *  3) We're streaming and ZIP_LENGTH_AT_END was specified and
1283 * we have no size information.  In this case, we can do pretty
1284 * well by watching for the data descriptor record.  The data
1285 * descriptor is 16 bytes and includes a computed CRC that should
1286 * provide a strong check.
1287 *
1288 * TODO: Technically, the PK\007\010 signature is optional.
1289 * In the original spec, the data descriptor contained CRC
1290 * and size fields but had no leading signature.  In practice,
1291 * newer writers seem to provide the signature pretty consistently.
1292 *
1293 * For uncompressed data, the PK\007\010 marker seems essential
1294 * to be sure we've actually seen the end of the entry.
1295 *
1296 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
1297 * zip->end_of_entry if it consumes all of the data.
1298 */
1299static int
1300zip_read_data_none(struct archive_read *a, const void **_buff,
1301    size_t *size, int64_t *offset)
1302{
1303	struct zip *zip;
1304	const char *buff;
1305	ssize_t bytes_avail;
1306	int r;
1307
1308	(void)offset; /* UNUSED */
1309
1310	zip = (struct zip *)(a->format->data);
1311
1312	if (zip->entry->zip_flags & ZIP_LENGTH_AT_END) {
1313		const char *p;
1314		ssize_t grabbing_bytes = 24;
1315
1316		if (zip->hctx_valid)
1317			grabbing_bytes += AUTH_CODE_SIZE;
1318		/* Grab at least 24 bytes. */
1319		buff = __archive_read_ahead(a, grabbing_bytes, &bytes_avail);
1320		if (bytes_avail < grabbing_bytes) {
1321			/* Zip archives have end-of-archive markers
1322			   that are longer than this, so a failure to get at
1323			   least 24 bytes really does indicate a truncated
1324			   file. */
1325			archive_set_error(&a->archive,
1326			    ARCHIVE_ERRNO_FILE_FORMAT,
1327			    "Truncated ZIP file data");
1328			return (ARCHIVE_FATAL);
1329		}
1330		/* Check for a complete PK\007\010 signature, followed
1331		 * by the correct 4-byte CRC. */
1332		p = buff;
1333		if (zip->hctx_valid)
1334			p += AUTH_CODE_SIZE;
1335		if (p[0] == 'P' && p[1] == 'K'
1336		    && p[2] == '\007' && p[3] == '\010'
1337		    && (archive_le32dec(p + 4) == zip->entry_crc32
1338			|| zip->ignore_crc32
1339			|| (zip->hctx_valid
1340			 && zip->entry->aes_extra.vendor == AES_VENDOR_AE_2))) {
1341			if (zip->entry->flags & LA_USED_ZIP64) {
1342				uint64_t compressed, uncompressed;
1343				zip->entry->crc32 = archive_le32dec(p + 4);
1344				compressed = archive_le64dec(p + 8);
1345				uncompressed = archive_le64dec(p + 16);
1346				if (compressed > INT64_MAX || uncompressed >
1347				    INT64_MAX) {
1348					archive_set_error(&a->archive,
1349					    ARCHIVE_ERRNO_FILE_FORMAT,
1350					    "Overflow of 64-bit file sizes");
1351					return ARCHIVE_FAILED;
1352				}
1353				zip->entry->compressed_size = compressed;
1354				zip->entry->uncompressed_size = uncompressed;
1355				zip->unconsumed = 24;
1356			} else {
1357				zip->entry->crc32 = archive_le32dec(p + 4);
1358				zip->entry->compressed_size =
1359					archive_le32dec(p + 8);
1360				zip->entry->uncompressed_size =
1361					archive_le32dec(p + 12);
1362				zip->unconsumed = 16;
1363			}
1364			if (zip->hctx_valid) {
1365				r = check_authentication_code(a, buff);
1366				if (r != ARCHIVE_OK)
1367					return (r);
1368			}
1369			zip->end_of_entry = 1;
1370			return (ARCHIVE_OK);
1371		}
1372		/* If not at EOF, ensure we consume at least one byte. */
1373		++p;
1374
1375		/* Scan forward until we see where a PK\007\010 signature
1376		 * might be. */
1377		/* Return bytes up until that point.  On the next call,
1378		 * the code above will verify the data descriptor. */
1379		while (p < buff + bytes_avail - 4) {
1380			if (p[3] == 'P') { p += 3; }
1381			else if (p[3] == 'K') { p += 2; }
1382			else if (p[3] == '\007') { p += 1; }
1383			else if (p[3] == '\010' && p[2] == '\007'
1384			    && p[1] == 'K' && p[0] == 'P') {
1385				if (zip->hctx_valid)
1386					p -= AUTH_CODE_SIZE;
1387				break;
1388			} else { p += 4; }
1389		}
1390		bytes_avail = p - buff;
1391	} else {
1392		if (zip->entry_bytes_remaining == 0) {
1393			zip->end_of_entry = 1;
1394			if (zip->hctx_valid) {
1395				r = check_authentication_code(a, NULL);
1396				if (r != ARCHIVE_OK)
1397					return (r);
1398			}
1399			return (ARCHIVE_OK);
1400		}
1401		/* Grab a bunch of bytes. */
1402		buff = __archive_read_ahead(a, 1, &bytes_avail);
1403		if (bytes_avail <= 0) {
1404			archive_set_error(&a->archive,
1405			    ARCHIVE_ERRNO_FILE_FORMAT,
1406			    "Truncated ZIP file data");
1407			return (ARCHIVE_FATAL);
1408		}
1409		if (bytes_avail > zip->entry_bytes_remaining)
1410			bytes_avail = (ssize_t)zip->entry_bytes_remaining;
1411	}
1412	if (zip->tctx_valid || zip->cctx_valid) {
1413		size_t dec_size = bytes_avail;
1414
1415		if (dec_size > zip->decrypted_buffer_size)
1416			dec_size = zip->decrypted_buffer_size;
1417		if (zip->tctx_valid) {
1418			trad_enc_decrypt_update(&zip->tctx,
1419			    (const uint8_t *)buff, dec_size,
1420			    zip->decrypted_buffer, dec_size);
1421		} else {
1422			size_t dsize = dec_size;
1423			archive_hmac_sha1_update(&zip->hctx,
1424			    (const uint8_t *)buff, dec_size);
1425			archive_decrypto_aes_ctr_update(&zip->cctx,
1426			    (const uint8_t *)buff, dec_size,
1427			    zip->decrypted_buffer, &dsize);
1428		}
1429		bytes_avail = dec_size;
1430		buff = (const char *)zip->decrypted_buffer;
1431	}
1432	*size = bytes_avail;
1433	zip->entry_bytes_remaining -= bytes_avail;
1434	zip->entry_uncompressed_bytes_read += bytes_avail;
1435	zip->entry_compressed_bytes_read += bytes_avail;
1436	zip->unconsumed += bytes_avail;
1437	*_buff = buff;
1438	return (ARCHIVE_OK);
1439}
1440
1441static int
1442consume_optional_marker(struct archive_read *a, struct zip *zip)
1443{
1444	if (zip->end_of_entry && (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) {
1445		const char *p;
1446
1447		if (NULL == (p = __archive_read_ahead(a, 24, NULL))) {
1448			archive_set_error(&a->archive,
1449			    ARCHIVE_ERRNO_FILE_FORMAT,
1450			    "Truncated ZIP end-of-file record");
1451			return (ARCHIVE_FATAL);
1452		}
1453		/* Consume the optional PK\007\010 marker. */
1454		if (p[0] == 'P' && p[1] == 'K' &&
1455		    p[2] == '\007' && p[3] == '\010') {
1456			p += 4;
1457			zip->unconsumed = 4;
1458		}
1459		if (zip->entry->flags & LA_USED_ZIP64) {
1460			uint64_t compressed, uncompressed;
1461			zip->entry->crc32 = archive_le32dec(p);
1462			compressed = archive_le64dec(p + 4);
1463			uncompressed = archive_le64dec(p + 12);
1464			if (compressed > INT64_MAX ||
1465			    uncompressed > INT64_MAX) {
1466				archive_set_error(&a->archive,
1467				    ARCHIVE_ERRNO_FILE_FORMAT,
1468				    "Overflow of 64-bit file sizes");
1469				return ARCHIVE_FAILED;
1470			}
1471			zip->entry->compressed_size = compressed;
1472			zip->entry->uncompressed_size = uncompressed;
1473			zip->unconsumed += 20;
1474		} else {
1475			zip->entry->crc32 = archive_le32dec(p);
1476			zip->entry->compressed_size = archive_le32dec(p + 4);
1477			zip->entry->uncompressed_size = archive_le32dec(p + 8);
1478			zip->unconsumed += 12;
1479		}
1480	}
1481
1482    return (ARCHIVE_OK);
1483}
1484
1485#if HAVE_LZMA_H && HAVE_LIBLZMA
1486static int
1487zipx_xz_init(struct archive_read *a, struct zip *zip)
1488{
1489	lzma_ret r;
1490
1491	if(zip->zipx_lzma_valid) {
1492		lzma_end(&zip->zipx_lzma_stream);
1493		zip->zipx_lzma_valid = 0;
1494	}
1495
1496	memset(&zip->zipx_lzma_stream, 0, sizeof(zip->zipx_lzma_stream));
1497	r = lzma_stream_decoder(&zip->zipx_lzma_stream, UINT64_MAX, 0);
1498	if (r != LZMA_OK) {
1499		archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1500		    "xz initialization failed(%d)",
1501		    r);
1502
1503		return (ARCHIVE_FAILED);
1504	}
1505
1506	zip->zipx_lzma_valid = 1;
1507
1508	free(zip->uncompressed_buffer);
1509
1510	zip->uncompressed_buffer_size = 256 * 1024;
1511	zip->uncompressed_buffer =
1512	    (uint8_t*) malloc(zip->uncompressed_buffer_size);
1513	if (zip->uncompressed_buffer == NULL) {
1514		archive_set_error(&a->archive, ENOMEM,
1515		    "No memory for xz decompression");
1516		    return (ARCHIVE_FATAL);
1517	}
1518
1519	zip->decompress_init = 1;
1520	return (ARCHIVE_OK);
1521}
1522
1523static int
1524zipx_lzma_alone_init(struct archive_read *a, struct zip *zip)
1525{
1526	lzma_ret r;
1527	const uint8_t* p;
1528
1529#pragma pack(push)
1530#pragma pack(1)
1531	struct _alone_header {
1532	    uint8_t bytes[5];
1533	    uint64_t uncompressed_size;
1534	} alone_header;
1535#pragma pack(pop)
1536
1537	if(zip->zipx_lzma_valid) {
1538		lzma_end(&zip->zipx_lzma_stream);
1539		zip->zipx_lzma_valid = 0;
1540	}
1541
1542	/* To unpack ZIPX's "LZMA" (id 14) stream we can use standard liblzma
1543	 * that is a part of XZ Utils. The stream format stored inside ZIPX
1544	 * file is a modified "lzma alone" file format, that was used by the
1545	 * `lzma` utility which was later deprecated in favour of `xz` utility. 	 * Since those formats are nearly the same, we can use a standard
1546	 * "lzma alone" decoder from XZ Utils. */
1547
1548	memset(&zip->zipx_lzma_stream, 0, sizeof(zip->zipx_lzma_stream));
1549	r = lzma_alone_decoder(&zip->zipx_lzma_stream, UINT64_MAX);
1550	if (r != LZMA_OK) {
1551		archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1552		    "lzma initialization failed(%d)", r);
1553
1554		return (ARCHIVE_FAILED);
1555	}
1556
1557	/* Flag the cleanup function that we want our lzma-related structures
1558	 * to be freed later. */
1559	zip->zipx_lzma_valid = 1;
1560
1561	/* The "lzma alone" file format and the stream format inside ZIPx are
1562	 * almost the same. Here's an example of a structure of "lzma alone"
1563	 * format:
1564	 *
1565	 * $ cat /bin/ls | lzma | xxd | head -n 1
1566	 * 00000000: 5d00 0080 00ff ffff ffff ffff ff00 2814
1567	 *
1568	 *    5 bytes        8 bytes        n bytes
1569	 * <lzma_params><uncompressed_size><data...>
1570	 *
1571	 * lzma_params is a 5-byte blob that has to be decoded to extract
1572	 * parameters of this LZMA stream. The uncompressed_size field is an
1573	 * uint64_t value that contains information about the size of the
1574	 * uncompressed file, or UINT64_MAX if this value is unknown.
1575	 * The <data...> part is the actual lzma-compressed data stream.
1576	 *
1577	 * Now here's the structure of the stream inside the ZIPX file:
1578	 *
1579	 * $ cat stream_inside_zipx | xxd | head -n 1
1580	 * 00000000: 0914 0500 5d00 8000 0000 2814 .... ....
1581	 *
1582	 *  2byte   2byte    5 bytes     n bytes
1583	 * <magic1><magic2><lzma_params><data...>
1584	 *
1585	 * This means that the ZIPX file contains an additional magic1 and
1586	 * magic2 headers, the lzma_params field contains the same parameter
1587	 * set as in the "lzma alone" format, and the <data...> field is the
1588	 * same as in the "lzma alone" format as well. Note that also the zipx
1589	 * format is missing the uncompressed_size field.
1590	 *
1591	 * So, in order to use the "lzma alone" decoder for the zipx lzma
1592	 * stream, we simply need to shuffle around some fields, prepare a new
1593	 * lzma alone header, feed it into lzma alone decoder so it will
1594	 * initialize itself properly, and then we can start feeding normal
1595	 * zipx lzma stream into the decoder.
1596	 */
1597
1598	/* Read magic1,magic2,lzma_params from the ZIPX stream. */
1599	if((p = __archive_read_ahead(a, 9, NULL)) == NULL) {
1600		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1601		    "Truncated lzma data");
1602		return (ARCHIVE_FATAL);
1603	}
1604
1605	if(p[2] != 0x05 || p[3] != 0x00) {
1606		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1607		    "Invalid lzma data");
1608		return (ARCHIVE_FATAL);
1609	}
1610
1611	/* Prepare an lzma alone header: copy the lzma_params blob into
1612	 * a proper place into the lzma alone header. */
1613	memcpy(&alone_header.bytes[0], p + 4, 5);
1614
1615	/* Initialize the 'uncompressed size' field to unknown; we'll manually
1616	 * monitor how many bytes there are still to be uncompressed. */
1617	alone_header.uncompressed_size = UINT64_MAX;
1618
1619	if(!zip->uncompressed_buffer) {
1620		zip->uncompressed_buffer_size = 256 * 1024;
1621		zip->uncompressed_buffer =
1622			(uint8_t*) malloc(zip->uncompressed_buffer_size);
1623
1624		if (zip->uncompressed_buffer == NULL) {
1625			archive_set_error(&a->archive, ENOMEM,
1626			    "No memory for lzma decompression");
1627			return (ARCHIVE_FATAL);
1628		}
1629	}
1630
1631	zip->zipx_lzma_stream.next_in = (void*) &alone_header;
1632	zip->zipx_lzma_stream.avail_in = sizeof(alone_header);
1633	zip->zipx_lzma_stream.total_in = 0;
1634	zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
1635	zip->zipx_lzma_stream.avail_out = zip->uncompressed_buffer_size;
1636	zip->zipx_lzma_stream.total_out = 0;
1637
1638	/* Feed only the header into the lzma alone decoder. This will
1639	 * effectively initialize the decoder, and will not produce any
1640	 * output bytes yet. */
1641	r = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
1642	if (r != LZMA_OK) {
1643		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1644		    "lzma stream initialization error");
1645		return ARCHIVE_FATAL;
1646	}
1647
1648	/* We've already consumed some bytes, so take this into account. */
1649	__archive_read_consume(a, 9);
1650	zip->entry_bytes_remaining -= 9;
1651	zip->entry_compressed_bytes_read += 9;
1652
1653	zip->decompress_init = 1;
1654	return (ARCHIVE_OK);
1655}
1656
1657static int
1658zip_read_data_zipx_xz(struct archive_read *a, const void **buff,
1659	size_t *size, int64_t *offset)
1660{
1661	struct zip* zip = (struct zip *)(a->format->data);
1662	int ret;
1663	lzma_ret lz_ret;
1664	const void* compressed_buf;
1665	ssize_t bytes_avail, in_bytes, to_consume = 0;
1666
1667	(void) offset; /* UNUSED */
1668
1669	/* Initialize decompressor if not yet initialized. */
1670	if (!zip->decompress_init) {
1671		ret = zipx_xz_init(a, zip);
1672		if (ret != ARCHIVE_OK)
1673			return (ret);
1674	}
1675
1676	compressed_buf = __archive_read_ahead(a, 1, &bytes_avail);
1677	if (bytes_avail < 0) {
1678		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1679		    "Truncated xz file body");
1680		return (ARCHIVE_FATAL);
1681	}
1682
1683	in_bytes = zipmin(zip->entry_bytes_remaining, bytes_avail);
1684	zip->zipx_lzma_stream.next_in = compressed_buf;
1685	zip->zipx_lzma_stream.avail_in = in_bytes;
1686	zip->zipx_lzma_stream.total_in = 0;
1687	zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
1688	zip->zipx_lzma_stream.avail_out = zip->uncompressed_buffer_size;
1689	zip->zipx_lzma_stream.total_out = 0;
1690
1691	/* Perform the decompression. */
1692	lz_ret = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
1693	switch(lz_ret) {
1694		case LZMA_DATA_ERROR:
1695			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1696			    "xz data error (error %d)", (int) lz_ret);
1697			return (ARCHIVE_FATAL);
1698
1699		case LZMA_NO_CHECK:
1700		case LZMA_OK:
1701			break;
1702
1703		default:
1704			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1705			    "xz unknown error %d", (int) lz_ret);
1706			return (ARCHIVE_FATAL);
1707
1708		case LZMA_STREAM_END:
1709			lzma_end(&zip->zipx_lzma_stream);
1710			zip->zipx_lzma_valid = 0;
1711
1712			if((int64_t) zip->zipx_lzma_stream.total_in !=
1713			    zip->entry_bytes_remaining)
1714			{
1715				archive_set_error(&a->archive,
1716				    ARCHIVE_ERRNO_MISC,
1717				    "xz premature end of stream");
1718				return (ARCHIVE_FATAL);
1719			}
1720
1721			zip->end_of_entry = 1;
1722			break;
1723	}
1724
1725	to_consume = zip->zipx_lzma_stream.total_in;
1726
1727	__archive_read_consume(a, to_consume);
1728	zip->entry_bytes_remaining -= to_consume;
1729	zip->entry_compressed_bytes_read += to_consume;
1730	zip->entry_uncompressed_bytes_read += zip->zipx_lzma_stream.total_out;
1731
1732	*size = zip->zipx_lzma_stream.total_out;
1733	*buff = zip->uncompressed_buffer;
1734
1735	ret = consume_optional_marker(a, zip);
1736	if (ret != ARCHIVE_OK)
1737		return (ret);
1738
1739	return (ARCHIVE_OK);
1740}
1741
1742static int
1743zip_read_data_zipx_lzma_alone(struct archive_read *a, const void **buff,
1744    size_t *size, int64_t *offset)
1745{
1746	struct zip* zip = (struct zip *)(a->format->data);
1747	int ret;
1748	lzma_ret lz_ret;
1749	const void* compressed_buf;
1750	ssize_t bytes_avail, in_bytes, to_consume;
1751
1752	(void) offset; /* UNUSED */
1753
1754	/* Initialize decompressor if not yet initialized. */
1755	if (!zip->decompress_init) {
1756		ret = zipx_lzma_alone_init(a, zip);
1757		if (ret != ARCHIVE_OK)
1758			return (ret);
1759	}
1760
1761	/* Fetch more compressed data. The same note as in deflate handler
1762	 * applies here as well:
1763	 *
1764	 * Note: '1' here is a performance optimization. Recall that the
1765	 * decompression layer returns a count of available bytes; asking for
1766	 * more than that forces the decompressor to combine reads by copying
1767	 * data.
1768	 */
1769	compressed_buf = __archive_read_ahead(a, 1, &bytes_avail);
1770	if (bytes_avail < 0) {
1771		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1772		    "Truncated lzma file body");
1773		return (ARCHIVE_FATAL);
1774	}
1775
1776	/* Set decompressor parameters. */
1777	in_bytes = zipmin(zip->entry_bytes_remaining, bytes_avail);
1778
1779	zip->zipx_lzma_stream.next_in = compressed_buf;
1780	zip->zipx_lzma_stream.avail_in = in_bytes;
1781	zip->zipx_lzma_stream.total_in = 0;
1782	zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer;
1783	zip->zipx_lzma_stream.avail_out =
1784		/* These lzma_alone streams lack end of stream marker, so let's
1785		 * make sure the unpacker won't try to unpack more than it's
1786		 * supposed to. */
1787		zipmin((int64_t) zip->uncompressed_buffer_size,
1788		    zip->entry->uncompressed_size -
1789		    zip->entry_uncompressed_bytes_read);
1790	zip->zipx_lzma_stream.total_out = 0;
1791
1792	/* Perform the decompression. */
1793	lz_ret = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN);
1794	switch(lz_ret) {
1795		case LZMA_DATA_ERROR:
1796			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1797			    "lzma data error (error %d)", (int) lz_ret);
1798			return (ARCHIVE_FATAL);
1799
1800		case LZMA_OK:
1801			break;
1802
1803		default:
1804			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1805			    "lzma unknown error %d", (int) lz_ret);
1806			return (ARCHIVE_FATAL);
1807	}
1808
1809	to_consume = zip->zipx_lzma_stream.total_in;
1810
1811	/* Update pointers. */
1812	__archive_read_consume(a, to_consume);
1813	zip->entry_bytes_remaining -= to_consume;
1814	zip->entry_compressed_bytes_read += to_consume;
1815	zip->entry_uncompressed_bytes_read += zip->zipx_lzma_stream.total_out;
1816
1817	if(zip->entry_bytes_remaining == 0) {
1818		zip->end_of_entry = 1;
1819	}
1820
1821	/* Return values. */
1822	*size = zip->zipx_lzma_stream.total_out;
1823	*buff = zip->uncompressed_buffer;
1824
1825	/* Behave the same way as during deflate decompression. */
1826	ret = consume_optional_marker(a, zip);
1827	if (ret != ARCHIVE_OK)
1828		return (ret);
1829
1830	/* Free lzma decoder handle because we'll no longer need it. */
1831	if(zip->end_of_entry) {
1832		lzma_end(&zip->zipx_lzma_stream);
1833		zip->zipx_lzma_valid = 0;
1834	}
1835
1836	/* If we're here, then we're good! */
1837	return (ARCHIVE_OK);
1838}
1839#endif /* HAVE_LZMA_H && HAVE_LIBLZMA */
1840
1841static int
1842zipx_ppmd8_init(struct archive_read *a, struct zip *zip)
1843{
1844	const void* p;
1845	uint32_t val;
1846	uint32_t order;
1847	uint32_t mem;
1848	uint32_t restore_method;
1849
1850	/* Remove previous decompression context if it exists. */
1851	if(zip->ppmd8_valid) {
1852		__archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8);
1853		zip->ppmd8_valid = 0;
1854	}
1855
1856	/* Create a new decompression context. */
1857	__archive_ppmd8_functions.Ppmd8_Construct(&zip->ppmd8);
1858	zip->ppmd8_stream_failed = 0;
1859
1860	/* Setup function pointers required by Ppmd8 decompressor. The
1861	 * 'ppmd_read' function will feed new bytes to the decompressor,
1862	 * and will increment the 'zip->zipx_ppmd_read_compressed' counter. */
1863	zip->ppmd8.Stream.In = &zip->zipx_ppmd_stream;
1864	zip->zipx_ppmd_stream.a = a;
1865	zip->zipx_ppmd_stream.Read = &ppmd_read;
1866
1867	/* Reset number of read bytes to 0. */
1868	zip->zipx_ppmd_read_compressed = 0;
1869
1870	/* Read Ppmd8 header (2 bytes). */
1871	p = __archive_read_ahead(a, 2, NULL);
1872	if(!p) {
1873		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1874		    "Truncated file data in PPMd8 stream");
1875		return (ARCHIVE_FATAL);
1876	}
1877	__archive_read_consume(a, 2);
1878
1879	/* Decode the stream's compression parameters. */
1880	val = archive_le16dec(p);
1881	order = (val & 15) + 1;
1882	mem = ((val >> 4) & 0xff) + 1;
1883	restore_method = (val >> 12);
1884
1885	if(order < 2 || restore_method > 2) {
1886		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1887		    "Invalid parameter set in PPMd8 stream (order=%d, "
1888		    "restore=%d)", order, restore_method);
1889		return (ARCHIVE_FAILED);
1890	}
1891
1892	/* Allocate the memory needed to properly decompress the file. */
1893	if(!__archive_ppmd8_functions.Ppmd8_Alloc(&zip->ppmd8, mem << 20)) {
1894		archive_set_error(&a->archive, ENOMEM,
1895		    "Unable to allocate memory for PPMd8 stream: %d bytes",
1896		    mem << 20);
1897		return (ARCHIVE_FATAL);
1898	}
1899
1900	/* Signal the cleanup function to release Ppmd8 context in the
1901	 * cleanup phase. */
1902	zip->ppmd8_valid = 1;
1903
1904	/* Perform further Ppmd8 initialization. */
1905	if(!__archive_ppmd8_functions.Ppmd8_RangeDec_Init(&zip->ppmd8)) {
1906		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1907		    "PPMd8 stream range decoder initialization error");
1908		return (ARCHIVE_FATAL);
1909	}
1910
1911	__archive_ppmd8_functions.Ppmd8_Init(&zip->ppmd8, order,
1912	    restore_method);
1913
1914	/* Allocate the buffer that will hold uncompressed data. */
1915	free(zip->uncompressed_buffer);
1916
1917	zip->uncompressed_buffer_size = 256 * 1024;
1918	zip->uncompressed_buffer =
1919	    (uint8_t*) malloc(zip->uncompressed_buffer_size);
1920
1921	if(zip->uncompressed_buffer == NULL) {
1922		archive_set_error(&a->archive, ENOMEM,
1923		    "No memory for PPMd8 decompression");
1924		return ARCHIVE_FATAL;
1925	}
1926
1927	/* Ppmd8 initialization is done. */
1928	zip->decompress_init = 1;
1929
1930	/* We've already read 2 bytes in the output stream. Additionally,
1931	 * Ppmd8 initialization code could read some data as well. So we
1932	 * are advancing the stream by 2 bytes plus whatever number of
1933	 * bytes Ppmd8 init function used. */
1934	zip->entry_compressed_bytes_read += 2 + zip->zipx_ppmd_read_compressed;
1935
1936	return ARCHIVE_OK;
1937}
1938
1939static int
1940zip_read_data_zipx_ppmd(struct archive_read *a, const void **buff,
1941    size_t *size, int64_t *offset)
1942{
1943	struct zip* zip = (struct zip *)(a->format->data);
1944	int ret;
1945	size_t consumed_bytes = 0;
1946	ssize_t bytes_avail = 0;
1947
1948	(void) offset; /* UNUSED */
1949
1950	/* If we're here for the first time, initialize Ppmd8 decompression
1951	 * context first. */
1952	if(!zip->decompress_init) {
1953		ret = zipx_ppmd8_init(a, zip);
1954		if(ret != ARCHIVE_OK)
1955			return ret;
1956	}
1957
1958	/* Fetch for more data. We're reading 1 byte here, but libarchive
1959	 * should prefetch more bytes. */
1960	(void) __archive_read_ahead(a, 1, &bytes_avail);
1961	if(bytes_avail < 0) {
1962		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1963		    "Truncated PPMd8 file body");
1964		return (ARCHIVE_FATAL);
1965	}
1966
1967	/* This counter will be updated inside ppmd_read(), which at one
1968	 * point will be called by Ppmd8_DecodeSymbol. */
1969	zip->zipx_ppmd_read_compressed = 0;
1970
1971	/* Decompression loop. */
1972	do {
1973		int sym = __archive_ppmd8_functions.Ppmd8_DecodeSymbol(
1974		    &zip->ppmd8);
1975		if(sym < 0) {
1976			zip->end_of_entry = 1;
1977			break;
1978		}
1979
1980		/* This field is set by ppmd_read() when there was no more data
1981		 * to be read. */
1982		if(zip->ppmd8_stream_failed) {
1983			archive_set_error(&a->archive,
1984			    ARCHIVE_ERRNO_FILE_FORMAT,
1985			    "Truncated PPMd8 file body");
1986			return (ARCHIVE_FATAL);
1987		}
1988
1989		zip->uncompressed_buffer[consumed_bytes] = (uint8_t) sym;
1990		++consumed_bytes;
1991	} while(consumed_bytes < zip->uncompressed_buffer_size);
1992
1993	/* Update pointers for libarchive. */
1994	*buff = zip->uncompressed_buffer;
1995	*size = consumed_bytes;
1996
1997	/* Update pointers so we can continue decompression in another call. */
1998	zip->entry_bytes_remaining -= zip->zipx_ppmd_read_compressed;
1999	zip->entry_compressed_bytes_read += zip->zipx_ppmd_read_compressed;
2000	zip->entry_uncompressed_bytes_read += consumed_bytes;
2001
2002	/* If we're at the end of stream, deinitialize Ppmd8 context. */
2003	if(zip->end_of_entry) {
2004		__archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8);
2005		zip->ppmd8_valid = 0;
2006	}
2007
2008	/* Seek for optional marker, same way as in each zip entry. */
2009	ret = consume_optional_marker(a, zip);
2010	if (ret != ARCHIVE_OK)
2011		return ret;
2012
2013	return ARCHIVE_OK;
2014}
2015
2016#ifdef HAVE_BZLIB_H
2017static int
2018zipx_bzip2_init(struct archive_read *a, struct zip *zip)
2019{
2020	int r;
2021
2022	/* Deallocate already existing BZ2 decompression context if it
2023	 * exists. */
2024	if(zip->bzstream_valid) {
2025		BZ2_bzDecompressEnd(&zip->bzstream);
2026		zip->bzstream_valid = 0;
2027	}
2028
2029	/* Allocate a new BZ2 decompression context. */
2030	memset(&zip->bzstream, 0, sizeof(bz_stream));
2031	r = BZ2_bzDecompressInit(&zip->bzstream, 0, 1);
2032	if(r != BZ_OK) {
2033		archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
2034		    "bzip2 initialization failed(%d)",
2035		    r);
2036
2037		return ARCHIVE_FAILED;
2038	}
2039
2040	/* Mark the bzstream field to be released in cleanup phase. */
2041	zip->bzstream_valid = 1;
2042
2043	/* (Re)allocate the buffer that will contain decompressed bytes. */
2044	free(zip->uncompressed_buffer);
2045
2046	zip->uncompressed_buffer_size = 256 * 1024;
2047	zip->uncompressed_buffer =
2048	    (uint8_t*) malloc(zip->uncompressed_buffer_size);
2049	if (zip->uncompressed_buffer == NULL) {
2050		archive_set_error(&a->archive, ENOMEM,
2051		    "No memory for bzip2 decompression");
2052		    return ARCHIVE_FATAL;
2053	}
2054
2055	/* Initialization done. */
2056	zip->decompress_init = 1;
2057	return ARCHIVE_OK;
2058}
2059
2060static int
2061zip_read_data_zipx_bzip2(struct archive_read *a, const void **buff,
2062    size_t *size, int64_t *offset)
2063{
2064	struct zip *zip = (struct zip *)(a->format->data);
2065	ssize_t bytes_avail = 0, in_bytes, to_consume;
2066	const void *compressed_buff;
2067	int r;
2068	uint64_t total_out;
2069
2070	(void) offset; /* UNUSED */
2071
2072	/* Initialize decompression context if we're here for the first time. */
2073	if(!zip->decompress_init) {
2074		r = zipx_bzip2_init(a, zip);
2075		if(r != ARCHIVE_OK)
2076			return r;
2077	}
2078
2079	/* Fetch more compressed bytes. */
2080	compressed_buff = __archive_read_ahead(a, 1, &bytes_avail);
2081	if(bytes_avail < 0) {
2082		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2083		    "Truncated bzip2 file body");
2084		return (ARCHIVE_FATAL);
2085	}
2086
2087	in_bytes = zipmin(zip->entry_bytes_remaining, bytes_avail);
2088	if(in_bytes < 1) {
2089		/* libbz2 doesn't complain when caller feeds avail_in == 0.
2090		 * It will actually return success in this case, which is
2091		 * undesirable. This is why we need to make this check
2092		 * manually. */
2093
2094		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2095		    "Truncated bzip2 file body");
2096		return (ARCHIVE_FATAL);
2097	}
2098
2099	/* Setup buffer boundaries. */
2100	zip->bzstream.next_in = (char*)(uintptr_t) compressed_buff;
2101	zip->bzstream.avail_in = in_bytes;
2102	zip->bzstream.total_in_hi32 = 0;
2103	zip->bzstream.total_in_lo32 = 0;
2104	zip->bzstream.next_out = (char*) zip->uncompressed_buffer;
2105	zip->bzstream.avail_out = zip->uncompressed_buffer_size;
2106	zip->bzstream.total_out_hi32 = 0;
2107	zip->bzstream.total_out_lo32 = 0;
2108
2109	/* Perform the decompression. */
2110	r = BZ2_bzDecompress(&zip->bzstream);
2111	switch(r) {
2112		case BZ_STREAM_END:
2113			/* If we're at the end of the stream, deinitialize the
2114			 * decompression context now. */
2115			switch(BZ2_bzDecompressEnd(&zip->bzstream)) {
2116				case BZ_OK:
2117					break;
2118				default:
2119					archive_set_error(&a->archive,
2120					    ARCHIVE_ERRNO_MISC,
2121					    "Failed to clean up bzip2 "
2122					    "decompressor");
2123					return ARCHIVE_FATAL;
2124			}
2125
2126			zip->end_of_entry = 1;
2127			break;
2128		case BZ_OK:
2129			/* The decompressor has successfully decoded this
2130			 * chunk of data, but more data is still in queue. */
2131			break;
2132		default:
2133			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2134			    "bzip2 decompression failed");
2135			return ARCHIVE_FATAL;
2136	}
2137
2138	/* Update the pointers so decompressor can continue decoding. */
2139	to_consume = zip->bzstream.total_in_lo32;
2140	__archive_read_consume(a, to_consume);
2141
2142	total_out = ((uint64_t) zip->bzstream.total_out_hi32 << 32) +
2143	    zip->bzstream.total_out_lo32;
2144
2145	zip->entry_bytes_remaining -= to_consume;
2146	zip->entry_compressed_bytes_read += to_consume;
2147	zip->entry_uncompressed_bytes_read += total_out;
2148
2149	/* Give libarchive its due. */
2150	*size = total_out;
2151	*buff = zip->uncompressed_buffer;
2152
2153	/* Seek for optional marker, like in other entries. */
2154	r = consume_optional_marker(a, zip);
2155	if(r != ARCHIVE_OK)
2156		return r;
2157
2158	return ARCHIVE_OK;
2159}
2160
2161#endif
2162
2163#ifdef HAVE_ZLIB_H
2164static int
2165zip_deflate_init(struct archive_read *a, struct zip *zip)
2166{
2167	int r;
2168
2169	/* If we haven't yet read any data, initialize the decompressor. */
2170	if (!zip->decompress_init) {
2171		if (zip->stream_valid)
2172			r = inflateReset(&zip->stream);
2173		else
2174			r = inflateInit2(&zip->stream,
2175			    -15 /* Don't check for zlib header */);
2176		if (r != Z_OK) {
2177			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2178			    "Can't initialize ZIP decompression.");
2179			return (ARCHIVE_FATAL);
2180		}
2181		/* Stream structure has been set up. */
2182		zip->stream_valid = 1;
2183		/* We've initialized decompression for this stream. */
2184		zip->decompress_init = 1;
2185	}
2186	return (ARCHIVE_OK);
2187}
2188
2189static int
2190zip_read_data_deflate(struct archive_read *a, const void **buff,
2191    size_t *size, int64_t *offset)
2192{
2193	struct zip *zip;
2194	ssize_t bytes_avail;
2195	const void *compressed_buff, *sp;
2196	int r;
2197
2198	(void)offset; /* UNUSED */
2199
2200	zip = (struct zip *)(a->format->data);
2201
2202	/* If the buffer hasn't been allocated, allocate it now. */
2203	if (zip->uncompressed_buffer == NULL) {
2204		zip->uncompressed_buffer_size = 256 * 1024;
2205		zip->uncompressed_buffer
2206		    = (unsigned char *)malloc(zip->uncompressed_buffer_size);
2207		if (zip->uncompressed_buffer == NULL) {
2208			archive_set_error(&a->archive, ENOMEM,
2209			    "No memory for ZIP decompression");
2210			return (ARCHIVE_FATAL);
2211		}
2212	}
2213
2214	r = zip_deflate_init(a, zip);
2215	if (r != ARCHIVE_OK)
2216		return (r);
2217
2218	/*
2219	 * Note: '1' here is a performance optimization.
2220	 * Recall that the decompression layer returns a count of
2221	 * available bytes; asking for more than that forces the
2222	 * decompressor to combine reads by copying data.
2223	 */
2224	compressed_buff = sp = __archive_read_ahead(a, 1, &bytes_avail);
2225	if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
2226	    && bytes_avail > zip->entry_bytes_remaining) {
2227		bytes_avail = (ssize_t)zip->entry_bytes_remaining;
2228	}
2229	if (bytes_avail < 0) {
2230		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2231		    "Truncated ZIP file body");
2232		return (ARCHIVE_FATAL);
2233	}
2234
2235	if (zip->tctx_valid || zip->cctx_valid) {
2236		if (zip->decrypted_bytes_remaining < (size_t)bytes_avail) {
2237			size_t buff_remaining =
2238			    (zip->decrypted_buffer +
2239			    zip->decrypted_buffer_size)
2240			    - (zip->decrypted_ptr +
2241			    zip->decrypted_bytes_remaining);
2242
2243			if (buff_remaining > (size_t)bytes_avail)
2244				buff_remaining = (size_t)bytes_avail;
2245
2246			if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) &&
2247			      zip->entry_bytes_remaining > 0) {
2248				if ((int64_t)(zip->decrypted_bytes_remaining
2249				    + buff_remaining)
2250				      > zip->entry_bytes_remaining) {
2251					if (zip->entry_bytes_remaining <
2252					    (int64_t)zip->decrypted_bytes_remaining)
2253						buff_remaining = 0;
2254					else
2255						buff_remaining =
2256						    (size_t)zip->entry_bytes_remaining
2257						    - zip->decrypted_bytes_remaining;
2258				}
2259			}
2260			if (buff_remaining > 0) {
2261				if (zip->tctx_valid) {
2262					trad_enc_decrypt_update(&zip->tctx,
2263					    compressed_buff, buff_remaining,
2264					    zip->decrypted_ptr
2265					      + zip->decrypted_bytes_remaining,
2266					    buff_remaining);
2267				} else {
2268					size_t dsize = buff_remaining;
2269					archive_decrypto_aes_ctr_update(
2270					    &zip->cctx,
2271					    compressed_buff, buff_remaining,
2272					    zip->decrypted_ptr
2273					      + zip->decrypted_bytes_remaining,
2274					    &dsize);
2275				}
2276				zip->decrypted_bytes_remaining +=
2277				    buff_remaining;
2278			}
2279		}
2280		bytes_avail = zip->decrypted_bytes_remaining;
2281		compressed_buff = (const char *)zip->decrypted_ptr;
2282	}
2283
2284	/*
2285	 * A bug in zlib.h: stream.next_in should be marked 'const'
2286	 * but isn't (the library never alters data through the
2287	 * next_in pointer, only reads it).  The result: this ugly
2288	 * cast to remove 'const'.
2289	 */
2290	zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
2291	zip->stream.avail_in = (uInt)bytes_avail;
2292	zip->stream.total_in = 0;
2293	zip->stream.next_out = zip->uncompressed_buffer;
2294	zip->stream.avail_out = (uInt)zip->uncompressed_buffer_size;
2295	zip->stream.total_out = 0;
2296
2297	r = inflate(&zip->stream, 0);
2298	switch (r) {
2299	case Z_OK:
2300		break;
2301	case Z_STREAM_END:
2302		zip->end_of_entry = 1;
2303		break;
2304	case Z_MEM_ERROR:
2305		archive_set_error(&a->archive, ENOMEM,
2306		    "Out of memory for ZIP decompression");
2307		return (ARCHIVE_FATAL);
2308	default:
2309		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2310		    "ZIP decompression failed (%d)", r);
2311		return (ARCHIVE_FATAL);
2312	}
2313
2314	/* Consume as much as the compressor actually used. */
2315	bytes_avail = zip->stream.total_in;
2316	if (zip->tctx_valid || zip->cctx_valid) {
2317		zip->decrypted_bytes_remaining -= bytes_avail;
2318		if (zip->decrypted_bytes_remaining == 0)
2319			zip->decrypted_ptr = zip->decrypted_buffer;
2320		else
2321			zip->decrypted_ptr += bytes_avail;
2322	}
2323	/* Calculate compressed data as much as we used.*/
2324	if (zip->hctx_valid)
2325		archive_hmac_sha1_update(&zip->hctx, sp, bytes_avail);
2326	__archive_read_consume(a, bytes_avail);
2327	zip->entry_bytes_remaining -= bytes_avail;
2328	zip->entry_compressed_bytes_read += bytes_avail;
2329
2330	*size = zip->stream.total_out;
2331	zip->entry_uncompressed_bytes_read += zip->stream.total_out;
2332	*buff = zip->uncompressed_buffer;
2333
2334	if (zip->end_of_entry && zip->hctx_valid) {
2335		r = check_authentication_code(a, NULL);
2336		if (r != ARCHIVE_OK)
2337			return (r);
2338	}
2339
2340	r = consume_optional_marker(a, zip);
2341	if (r != ARCHIVE_OK)
2342		return (r);
2343
2344	return (ARCHIVE_OK);
2345}
2346#endif
2347
2348static int
2349read_decryption_header(struct archive_read *a)
2350{
2351	struct zip *zip = (struct zip *)(a->format->data);
2352	const char *p;
2353	unsigned int remaining_size;
2354	unsigned int ts;
2355
2356	/*
2357	 * Read an initialization vector data field.
2358	 */
2359	p = __archive_read_ahead(a, 2, NULL);
2360	if (p == NULL)
2361		goto truncated;
2362	ts = zip->iv_size;
2363	zip->iv_size = archive_le16dec(p);
2364	__archive_read_consume(a, 2);
2365	if (ts < zip->iv_size) {
2366		free(zip->iv);
2367		zip->iv = NULL;
2368	}
2369	p = __archive_read_ahead(a, zip->iv_size, NULL);
2370	if (p == NULL)
2371		goto truncated;
2372	if (zip->iv == NULL) {
2373		zip->iv = malloc(zip->iv_size);
2374		if (zip->iv == NULL)
2375			goto nomem;
2376	}
2377	memcpy(zip->iv, p, zip->iv_size);
2378	__archive_read_consume(a, zip->iv_size);
2379
2380	/*
2381	 * Read a size of remaining decryption header field.
2382	 */
2383	p = __archive_read_ahead(a, 14, NULL);
2384	if (p == NULL)
2385		goto truncated;
2386	remaining_size = archive_le32dec(p);
2387	if (remaining_size < 16 || remaining_size > (1 << 18))
2388		goto corrupted;
2389
2390	/* Check if format version is supported. */
2391	if (archive_le16dec(p+4) != 3) {
2392		archive_set_error(&a->archive,
2393		    ARCHIVE_ERRNO_FILE_FORMAT,
2394		    "Unsupported encryption format version: %u",
2395		    archive_le16dec(p+4));
2396		return (ARCHIVE_FAILED);
2397	}
2398
2399	/*
2400	 * Read an encryption algorithm field.
2401	 */
2402	zip->alg_id = archive_le16dec(p+6);
2403	switch (zip->alg_id) {
2404	case 0x6601:/* DES */
2405	case 0x6602:/* RC2 */
2406	case 0x6603:/* 3DES 168 */
2407	case 0x6609:/* 3DES 112 */
2408	case 0x660E:/* AES 128 */
2409	case 0x660F:/* AES 192 */
2410	case 0x6610:/* AES 256 */
2411	case 0x6702:/* RC2 (version >= 5.2) */
2412	case 0x6720:/* Blowfish */
2413	case 0x6721:/* Twofish */
2414	case 0x6801:/* RC4 */
2415		/* Supported encryption algorithm. */
2416		break;
2417	default:
2418		archive_set_error(&a->archive,
2419		    ARCHIVE_ERRNO_FILE_FORMAT,
2420		    "Unknown encryption algorithm: %u", zip->alg_id);
2421		return (ARCHIVE_FAILED);
2422	}
2423
2424	/*
2425	 * Read a bit length field.
2426	 */
2427	zip->bit_len = archive_le16dec(p+8);
2428
2429	/*
2430	 * Read a flags field.
2431	 */
2432	zip->flags = archive_le16dec(p+10);
2433	switch (zip->flags & 0xf000) {
2434	case 0x0001: /* Password is required to decrypt. */
2435	case 0x0002: /* Certificates only. */
2436	case 0x0003: /* Password or certificate required to decrypt. */
2437		break;
2438	default:
2439		archive_set_error(&a->archive,
2440		    ARCHIVE_ERRNO_FILE_FORMAT,
2441		    "Unknown encryption flag: %u", zip->flags);
2442		return (ARCHIVE_FAILED);
2443	}
2444	if ((zip->flags & 0xf000) == 0 ||
2445	    (zip->flags & 0xf000) == 0x4000) {
2446		archive_set_error(&a->archive,
2447		    ARCHIVE_ERRNO_FILE_FORMAT,
2448		    "Unknown encryption flag: %u", zip->flags);
2449		return (ARCHIVE_FAILED);
2450	}
2451
2452	/*
2453	 * Read an encrypted random data field.
2454	 */
2455	ts = zip->erd_size;
2456	zip->erd_size = archive_le16dec(p+12);
2457	__archive_read_consume(a, 14);
2458	if ((zip->erd_size & 0xf) != 0 ||
2459	    (zip->erd_size + 16) > remaining_size ||
2460	    (zip->erd_size + 16) < zip->erd_size)
2461		goto corrupted;
2462
2463	if (ts < zip->erd_size) {
2464		free(zip->erd);
2465		zip->erd = NULL;
2466	}
2467	p = __archive_read_ahead(a, zip->erd_size, NULL);
2468	if (p == NULL)
2469		goto truncated;
2470	if (zip->erd == NULL) {
2471		zip->erd = malloc(zip->erd_size);
2472		if (zip->erd == NULL)
2473			goto nomem;
2474	}
2475	memcpy(zip->erd, p, zip->erd_size);
2476	__archive_read_consume(a, zip->erd_size);
2477
2478	/*
2479	 * Read a reserved data field.
2480	 */
2481	p = __archive_read_ahead(a, 4, NULL);
2482	if (p == NULL)
2483		goto truncated;
2484	/* Reserved data size should be zero. */
2485	if (archive_le32dec(p) != 0)
2486		goto corrupted;
2487	__archive_read_consume(a, 4);
2488
2489	/*
2490	 * Read a password validation data field.
2491	 */
2492	p = __archive_read_ahead(a, 2, NULL);
2493	if (p == NULL)
2494		goto truncated;
2495	ts = zip->v_size;
2496	zip->v_size = archive_le16dec(p);
2497	__archive_read_consume(a, 2);
2498	if ((zip->v_size & 0x0f) != 0 ||
2499	    (zip->erd_size + zip->v_size + 16) > remaining_size ||
2500	    (zip->erd_size + zip->v_size + 16) < (zip->erd_size + zip->v_size))
2501		goto corrupted;
2502	if (ts < zip->v_size) {
2503		free(zip->v_data);
2504		zip->v_data = NULL;
2505	}
2506	p = __archive_read_ahead(a, zip->v_size, NULL);
2507	if (p == NULL)
2508		goto truncated;
2509	if (zip->v_data == NULL) {
2510		zip->v_data = malloc(zip->v_size);
2511		if (zip->v_data == NULL)
2512			goto nomem;
2513	}
2514	memcpy(zip->v_data, p, zip->v_size);
2515	__archive_read_consume(a, zip->v_size);
2516
2517	p = __archive_read_ahead(a, 4, NULL);
2518	if (p == NULL)
2519		goto truncated;
2520	zip->v_crc32 = archive_le32dec(p);
2521	__archive_read_consume(a, 4);
2522
2523	/*return (ARCHIVE_OK);
2524	 * This is not fully implemented yet.*/
2525	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2526	    "Encrypted file is unsupported");
2527	return (ARCHIVE_FAILED);
2528truncated:
2529	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2530	    "Truncated ZIP file data");
2531	return (ARCHIVE_FATAL);
2532corrupted:
2533	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2534	    "Corrupted ZIP file data");
2535	return (ARCHIVE_FATAL);
2536nomem:
2537	archive_set_error(&a->archive, ENOMEM,
2538	    "No memory for ZIP decryption");
2539	return (ARCHIVE_FATAL);
2540}
2541
2542static int
2543zip_alloc_decryption_buffer(struct archive_read *a)
2544{
2545	struct zip *zip = (struct zip *)(a->format->data);
2546	size_t bs = 256 * 1024;
2547
2548	if (zip->decrypted_buffer == NULL) {
2549		zip->decrypted_buffer_size = bs;
2550		zip->decrypted_buffer = malloc(bs);
2551		if (zip->decrypted_buffer == NULL) {
2552			archive_set_error(&a->archive, ENOMEM,
2553			    "No memory for ZIP decryption");
2554			return (ARCHIVE_FATAL);
2555		}
2556	}
2557	zip->decrypted_ptr = zip->decrypted_buffer;
2558	return (ARCHIVE_OK);
2559}
2560
2561static int
2562init_traditional_PKWARE_decryption(struct archive_read *a)
2563{
2564	struct zip *zip = (struct zip *)(a->format->data);
2565	const void *p;
2566	int retry;
2567	int r;
2568
2569	if (zip->tctx_valid)
2570		return (ARCHIVE_OK);
2571
2572	/*
2573	   Read the 12 bytes encryption header stored at
2574	   the start of the data area.
2575	 */
2576#define ENC_HEADER_SIZE	12
2577	if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
2578	    && zip->entry_bytes_remaining < ENC_HEADER_SIZE) {
2579		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2580		    "Truncated Zip encrypted body: only %jd bytes available",
2581		    (intmax_t)zip->entry_bytes_remaining);
2582		return (ARCHIVE_FATAL);
2583	}
2584
2585	p = __archive_read_ahead(a, ENC_HEADER_SIZE, NULL);
2586	if (p == NULL) {
2587		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2588		    "Truncated ZIP file data");
2589		return (ARCHIVE_FATAL);
2590	}
2591
2592	for (retry = 0;; retry++) {
2593		const char *passphrase;
2594		uint8_t crcchk;
2595
2596		passphrase = __archive_read_next_passphrase(a);
2597		if (passphrase == NULL) {
2598			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2599			    (retry > 0)?
2600				"Incorrect passphrase":
2601				"Passphrase required for this entry");
2602			return (ARCHIVE_FAILED);
2603		}
2604
2605		/*
2606		 * Initialize ctx for Traditional PKWARE Decryption.
2607		 */
2608		r = trad_enc_init(&zip->tctx, passphrase, strlen(passphrase),
2609			p, ENC_HEADER_SIZE, &crcchk);
2610		if (r == 0 && crcchk == zip->entry->decdat)
2611			break;/* The passphrase is OK. */
2612		if (retry > 10000) {
2613			/* Avoid infinity loop. */
2614			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2615			    "Too many incorrect passphrases");
2616			return (ARCHIVE_FAILED);
2617		}
2618	}
2619
2620	__archive_read_consume(a, ENC_HEADER_SIZE);
2621	zip->tctx_valid = 1;
2622	if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) {
2623	    zip->entry_bytes_remaining -= ENC_HEADER_SIZE;
2624	}
2625	/*zip->entry_uncompressed_bytes_read += ENC_HEADER_SIZE;*/
2626	zip->entry_compressed_bytes_read += ENC_HEADER_SIZE;
2627	zip->decrypted_bytes_remaining = 0;
2628
2629	return (zip_alloc_decryption_buffer(a));
2630#undef ENC_HEADER_SIZE
2631}
2632
2633static int
2634init_WinZip_AES_decryption(struct archive_read *a)
2635{
2636	struct zip *zip = (struct zip *)(a->format->data);
2637	const void *p;
2638	const uint8_t *pv;
2639	size_t key_len, salt_len;
2640	uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
2641	int retry;
2642	int r;
2643
2644	if (zip->cctx_valid || zip->hctx_valid)
2645		return (ARCHIVE_OK);
2646
2647	switch (zip->entry->aes_extra.strength) {
2648	case 1: salt_len = 8;  key_len = 16; break;
2649	case 2: salt_len = 12; key_len = 24; break;
2650	case 3: salt_len = 16; key_len = 32; break;
2651	default: goto corrupted;
2652	}
2653	p = __archive_read_ahead(a, salt_len + 2, NULL);
2654	if (p == NULL)
2655		goto truncated;
2656
2657	for (retry = 0;; retry++) {
2658		const char *passphrase;
2659
2660		passphrase = __archive_read_next_passphrase(a);
2661		if (passphrase == NULL) {
2662			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2663			    (retry > 0)?
2664				"Incorrect passphrase":
2665				"Passphrase required for this entry");
2666			return (ARCHIVE_FAILED);
2667		}
2668		memset(derived_key, 0, sizeof(derived_key));
2669		r = archive_pbkdf2_sha1(passphrase, strlen(passphrase),
2670		    p, salt_len, 1000, derived_key, key_len * 2 + 2);
2671		if (r != 0) {
2672			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2673			    "Decryption is unsupported due to lack of "
2674			    "crypto library");
2675			return (ARCHIVE_FAILED);
2676		}
2677
2678		/* Check password verification value. */
2679		pv = ((const uint8_t *)p) + salt_len;
2680		if (derived_key[key_len * 2] == pv[0] &&
2681		    derived_key[key_len * 2 + 1] == pv[1])
2682			break;/* The passphrase is OK. */
2683		if (retry > 10000) {
2684			/* Avoid infinity loop. */
2685			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2686			    "Too many incorrect passphrases");
2687			return (ARCHIVE_FAILED);
2688		}
2689	}
2690
2691	r = archive_decrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
2692	if (r != 0) {
2693		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2694		    "Decryption is unsupported due to lack of crypto library");
2695		return (ARCHIVE_FAILED);
2696	}
2697	r = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len, key_len);
2698	if (r != 0) {
2699		archive_decrypto_aes_ctr_release(&zip->cctx);
2700		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2701		    "Failed to initialize HMAC-SHA1");
2702		return (ARCHIVE_FAILED);
2703	}
2704	zip->cctx_valid = zip->hctx_valid = 1;
2705	__archive_read_consume(a, salt_len + 2);
2706	zip->entry_bytes_remaining -= salt_len + 2 + AUTH_CODE_SIZE;
2707	if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
2708	    && zip->entry_bytes_remaining < 0)
2709		goto corrupted;
2710	zip->entry_compressed_bytes_read += salt_len + 2 + AUTH_CODE_SIZE;
2711	zip->decrypted_bytes_remaining = 0;
2712
2713	zip->entry->compression = zip->entry->aes_extra.compression;
2714	return (zip_alloc_decryption_buffer(a));
2715
2716truncated:
2717	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2718	    "Truncated ZIP file data");
2719	return (ARCHIVE_FATAL);
2720corrupted:
2721	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2722	    "Corrupted ZIP file data");
2723	return (ARCHIVE_FATAL);
2724}
2725
2726static int
2727archive_read_format_zip_read_data(struct archive_read *a,
2728    const void **buff, size_t *size, int64_t *offset)
2729{
2730	int r;
2731	struct zip *zip = (struct zip *)(a->format->data);
2732
2733	if (zip->has_encrypted_entries ==
2734			ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
2735		zip->has_encrypted_entries = 0;
2736	}
2737
2738	*offset = zip->entry_uncompressed_bytes_read;
2739	*size = 0;
2740	*buff = NULL;
2741
2742	/* If we hit end-of-entry last time, return ARCHIVE_EOF. */
2743	if (zip->end_of_entry)
2744		return (ARCHIVE_EOF);
2745
2746	/* Return EOF immediately if this is a non-regular file. */
2747	if (AE_IFREG != (zip->entry->mode & AE_IFMT))
2748		return (ARCHIVE_EOF);
2749
2750	__archive_read_consume(a, zip->unconsumed);
2751	zip->unconsumed = 0;
2752
2753	if (zip->init_decryption) {
2754		zip->has_encrypted_entries = 1;
2755		if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED)
2756			r = read_decryption_header(a);
2757		else if (zip->entry->compression == WINZIP_AES_ENCRYPTION)
2758			r = init_WinZip_AES_decryption(a);
2759		else
2760			r = init_traditional_PKWARE_decryption(a);
2761		if (r != ARCHIVE_OK)
2762			return (r);
2763		zip->init_decryption = 0;
2764	}
2765
2766	switch(zip->entry->compression) {
2767	case 0:  /* No compression. */
2768		r =  zip_read_data_none(a, buff, size, offset);
2769		break;
2770#ifdef HAVE_BZLIB_H
2771	case 12: /* ZIPx bzip2 compression. */
2772		r = zip_read_data_zipx_bzip2(a, buff, size, offset);
2773		break;
2774#endif
2775#if HAVE_LZMA_H && HAVE_LIBLZMA
2776	case 14: /* ZIPx LZMA compression. */
2777		r = zip_read_data_zipx_lzma_alone(a, buff, size, offset);
2778		break;
2779	case 95: /* ZIPx XZ compression. */
2780		r = zip_read_data_zipx_xz(a, buff, size, offset);
2781		break;
2782#endif
2783	/* PPMd support is built-in, so we don't need any #if guards. */
2784	case 98: /* ZIPx PPMd compression. */
2785		r = zip_read_data_zipx_ppmd(a, buff, size, offset);
2786		break;
2787
2788#ifdef HAVE_ZLIB_H
2789	case 8: /* Deflate compression. */
2790		r =  zip_read_data_deflate(a, buff, size, offset);
2791		break;
2792#endif
2793	default: /* Unsupported compression. */
2794		/* Return a warning. */
2795		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2796		    "Unsupported ZIP compression method (%d: %s)",
2797		    zip->entry->compression, compression_name(zip->entry->compression));
2798		/* We can't decompress this entry, but we will
2799		 * be able to skip() it and try the next entry. */
2800		return (ARCHIVE_FAILED);
2801		break;
2802	}
2803	if (r != ARCHIVE_OK)
2804		return (r);
2805	/* Update checksum */
2806	if (*size)
2807		zip->entry_crc32 = zip->crc32func(zip->entry_crc32, *buff,
2808		    (unsigned)*size);
2809	/* If we hit the end, swallow any end-of-data marker. */
2810	if (zip->end_of_entry) {
2811		/* Check file size, CRC against these values. */
2812		if (zip->entry->compressed_size !=
2813		    zip->entry_compressed_bytes_read) {
2814			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2815			    "ZIP compressed data is wrong size "
2816			    "(read %jd, expected %jd)",
2817			    (intmax_t)zip->entry_compressed_bytes_read,
2818			    (intmax_t)zip->entry->compressed_size);
2819			return (ARCHIVE_WARN);
2820		}
2821		/* Size field only stores the lower 32 bits of the actual
2822		 * size. */
2823		if ((zip->entry->uncompressed_size & UINT32_MAX)
2824		    != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
2825			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2826			    "ZIP uncompressed data is wrong size "
2827			    "(read %jd, expected %jd)\n",
2828			    (intmax_t)zip->entry_uncompressed_bytes_read,
2829			    (intmax_t)zip->entry->uncompressed_size);
2830			return (ARCHIVE_WARN);
2831		}
2832		/* Check computed CRC against header */
2833		if ((!zip->hctx_valid ||
2834		      zip->entry->aes_extra.vendor != AES_VENDOR_AE_2) &&
2835		   zip->entry->crc32 != zip->entry_crc32
2836		    && !zip->ignore_crc32) {
2837			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2838			    "ZIP bad CRC: 0x%lx should be 0x%lx",
2839			    (unsigned long)zip->entry_crc32,
2840			    (unsigned long)zip->entry->crc32);
2841			return (ARCHIVE_WARN);
2842		}
2843	}
2844
2845	return (ARCHIVE_OK);
2846}
2847
2848static int
2849archive_read_format_zip_cleanup(struct archive_read *a)
2850{
2851	struct zip *zip;
2852	struct zip_entry *zip_entry, *next_zip_entry;
2853
2854	zip = (struct zip *)(a->format->data);
2855
2856#ifdef HAVE_ZLIB_H
2857	if (zip->stream_valid)
2858		inflateEnd(&zip->stream);
2859#endif
2860
2861#if HAVE_LZMA_H && HAVE_LIBLZMA
2862    if (zip->zipx_lzma_valid) {
2863		lzma_end(&zip->zipx_lzma_stream);
2864	}
2865#endif
2866
2867#ifdef HAVE_BZLIB_H
2868	if (zip->bzstream_valid) {
2869		BZ2_bzDecompressEnd(&zip->bzstream);
2870	}
2871#endif
2872
2873	free(zip->uncompressed_buffer);
2874
2875	if (zip->ppmd8_valid)
2876		__archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8);
2877
2878	if (zip->zip_entries) {
2879		zip_entry = zip->zip_entries;
2880		while (zip_entry != NULL) {
2881			next_zip_entry = zip_entry->next;
2882			archive_string_free(&zip_entry->rsrcname);
2883			free(zip_entry);
2884			zip_entry = next_zip_entry;
2885		}
2886	}
2887	free(zip->decrypted_buffer);
2888	if (zip->cctx_valid)
2889		archive_decrypto_aes_ctr_release(&zip->cctx);
2890	if (zip->hctx_valid)
2891		archive_hmac_sha1_cleanup(&zip->hctx);
2892	free(zip->iv);
2893	free(zip->erd);
2894	free(zip->v_data);
2895	archive_string_free(&zip->format_name);
2896	free(zip);
2897	(a->format->data) = NULL;
2898	return (ARCHIVE_OK);
2899}
2900
2901static int
2902archive_read_format_zip_has_encrypted_entries(struct archive_read *_a)
2903{
2904	if (_a && _a->format) {
2905		struct zip * zip = (struct zip *)_a->format->data;
2906		if (zip) {
2907			return zip->has_encrypted_entries;
2908		}
2909	}
2910	return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
2911}
2912
2913static int
2914archive_read_format_zip_options(struct archive_read *a,
2915    const char *key, const char *val)
2916{
2917	struct zip *zip;
2918	int ret = ARCHIVE_FAILED;
2919
2920	zip = (struct zip *)(a->format->data);
2921	if (strcmp(key, "compat-2x")  == 0) {
2922		/* Handle filenames as libarchive 2.x */
2923		zip->init_default_conversion = (val != NULL) ? 1 : 0;
2924		return (ARCHIVE_OK);
2925	} else if (strcmp(key, "hdrcharset")  == 0) {
2926		if (val == NULL || val[0] == 0)
2927			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2928			    "zip: hdrcharset option needs a character-set name"
2929			);
2930		else {
2931			zip->sconv = archive_string_conversion_from_charset(
2932			    &a->archive, val, 0);
2933			if (zip->sconv != NULL) {
2934				if (strcmp(val, "UTF-8") == 0)
2935					zip->sconv_utf8 = zip->sconv;
2936				ret = ARCHIVE_OK;
2937			} else
2938				ret = ARCHIVE_FATAL;
2939		}
2940		return (ret);
2941	} else if (strcmp(key, "ignorecrc32") == 0) {
2942		/* Mostly useful for testing. */
2943		if (val == NULL || val[0] == 0) {
2944			zip->crc32func = real_crc32;
2945			zip->ignore_crc32 = 0;
2946		} else {
2947			zip->crc32func = fake_crc32;
2948			zip->ignore_crc32 = 1;
2949		}
2950		return (ARCHIVE_OK);
2951	} else if (strcmp(key, "mac-ext") == 0) {
2952		zip->process_mac_extensions = (val != NULL && val[0] != 0);
2953		return (ARCHIVE_OK);
2954	}
2955
2956	/* Note: The "warn" return is just to inform the options
2957	 * supervisor that we didn't handle it.  It will generate
2958	 * a suitable error if no one used this option. */
2959	return (ARCHIVE_WARN);
2960}
2961
2962int
2963archive_read_support_format_zip(struct archive *a)
2964{
2965	int r;
2966	r = archive_read_support_format_zip_streamable(a);
2967	if (r != ARCHIVE_OK)
2968		return r;
2969	return (archive_read_support_format_zip_seekable(a));
2970}
2971
2972/* ------------------------------------------------------------------------ */
2973
2974/*
2975 * Streaming-mode support
2976 */
2977
2978
2979static int
2980archive_read_support_format_zip_capabilities_streamable(struct archive_read * a)
2981{
2982	(void)a; /* UNUSED */
2983	return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
2984		ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
2985}
2986
2987static int
2988archive_read_format_zip_streamable_bid(struct archive_read *a, int best_bid)
2989{
2990	const char *p;
2991
2992	(void)best_bid; /* UNUSED */
2993
2994	if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
2995		return (-1);
2996
2997	/*
2998	 * Bid of 29 here comes from:
2999	 *  + 16 bits for "PK",
3000	 *  + next 16-bit field has 6 options so contributes
3001	 *    about 16 - log_2(6) ~= 16 - 2.6 ~= 13 bits
3002	 *
3003	 * So we've effectively verified ~29 total bits of check data.
3004	 */
3005	if (p[0] == 'P' && p[1] == 'K') {
3006		if ((p[2] == '\001' && p[3] == '\002')
3007		    || (p[2] == '\003' && p[3] == '\004')
3008		    || (p[2] == '\005' && p[3] == '\006')
3009		    || (p[2] == '\006' && p[3] == '\006')
3010		    || (p[2] == '\007' && p[3] == '\010')
3011		    || (p[2] == '0' && p[3] == '0'))
3012			return (29);
3013	}
3014
3015	/* TODO: It's worth looking ahead a little bit for a valid
3016	 * PK signature.  In particular, that would make it possible
3017	 * to read some UUEncoded SFX files or SFX files coming from
3018	 * a network socket. */
3019
3020	return (0);
3021}
3022
3023static int
3024archive_read_format_zip_streamable_read_header(struct archive_read *a,
3025    struct archive_entry *entry)
3026{
3027	struct zip *zip;
3028
3029	a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
3030	if (a->archive.archive_format_name == NULL)
3031		a->archive.archive_format_name = "ZIP";
3032
3033	zip = (struct zip *)(a->format->data);
3034
3035	/*
3036	 * It should be sufficient to call archive_read_next_header() for
3037	 * a reader to determine if an entry is encrypted or not. If the
3038	 * encryption of an entry is only detectable when calling
3039	 * archive_read_data(), so be it. We'll do the same check there
3040	 * as well.
3041	 */
3042	if (zip->has_encrypted_entries ==
3043			ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
3044		zip->has_encrypted_entries = 0;
3045
3046	/* Make sure we have a zip_entry structure to use. */
3047	if (zip->zip_entries == NULL) {
3048		zip->zip_entries = malloc(sizeof(struct zip_entry));
3049		if (zip->zip_entries == NULL) {
3050			archive_set_error(&a->archive, ENOMEM,
3051			    "Out  of memory");
3052			return ARCHIVE_FATAL;
3053		}
3054	}
3055	zip->entry = zip->zip_entries;
3056	memset(zip->entry, 0, sizeof(struct zip_entry));
3057
3058	if (zip->cctx_valid)
3059		archive_decrypto_aes_ctr_release(&zip->cctx);
3060	if (zip->hctx_valid)
3061		archive_hmac_sha1_cleanup(&zip->hctx);
3062	zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
3063	__archive_read_reset_passphrase(a);
3064
3065	/* Search ahead for the next local file header. */
3066	__archive_read_consume(a, zip->unconsumed);
3067	zip->unconsumed = 0;
3068	for (;;) {
3069		int64_t skipped = 0;
3070		const char *p, *end;
3071		ssize_t bytes;
3072
3073		p = __archive_read_ahead(a, 4, &bytes);
3074		if (p == NULL)
3075			return (ARCHIVE_FATAL);
3076		end = p + bytes;
3077
3078		while (p + 4 <= end) {
3079			if (p[0] == 'P' && p[1] == 'K') {
3080				if (p[2] == '\003' && p[3] == '\004') {
3081					/* Regular file entry. */
3082					__archive_read_consume(a, skipped);
3083					return zip_read_local_file_header(a,
3084					    entry, zip);
3085				}
3086
3087                              /*
3088                               * TODO: We cannot restore permissions
3089                               * based only on the local file headers.
3090                               * Consider scanning the central
3091                               * directory and returning additional
3092                               * entries for at least directories.
3093                               * This would allow us to properly set
3094                               * directory permissions.
3095			       *
3096			       * This won't help us fix symlinks
3097			       * and may not help with regular file
3098			       * permissions, either.  <sigh>
3099                               */
3100                              if (p[2] == '\001' && p[3] == '\002') {
3101                                      return (ARCHIVE_EOF);
3102                              }
3103
3104                              /* End of central directory?  Must be an
3105                               * empty archive. */
3106                              if ((p[2] == '\005' && p[3] == '\006')
3107                                  || (p[2] == '\006' && p[3] == '\006'))
3108                                      return (ARCHIVE_EOF);
3109			}
3110			++p;
3111			++skipped;
3112		}
3113		__archive_read_consume(a, skipped);
3114	}
3115}
3116
3117static int
3118archive_read_format_zip_read_data_skip_streamable(struct archive_read *a)
3119{
3120	struct zip *zip;
3121	int64_t bytes_skipped;
3122
3123	zip = (struct zip *)(a->format->data);
3124	bytes_skipped = __archive_read_consume(a, zip->unconsumed);
3125	zip->unconsumed = 0;
3126	if (bytes_skipped < 0)
3127		return (ARCHIVE_FATAL);
3128
3129	/* If we've already read to end of data, we're done. */
3130	if (zip->end_of_entry)
3131		return (ARCHIVE_OK);
3132
3133	/* So we know we're streaming... */
3134	if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
3135	    || zip->entry->compressed_size > 0) {
3136		/* We know the compressed length, so we can just skip. */
3137		bytes_skipped = __archive_read_consume(a,
3138					zip->entry_bytes_remaining);
3139		if (bytes_skipped < 0)
3140			return (ARCHIVE_FATAL);
3141		return (ARCHIVE_OK);
3142	}
3143
3144	if (zip->init_decryption) {
3145		int r;
3146
3147		zip->has_encrypted_entries = 1;
3148		if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED)
3149			r = read_decryption_header(a);
3150		else if (zip->entry->compression == WINZIP_AES_ENCRYPTION)
3151			r = init_WinZip_AES_decryption(a);
3152		else
3153			r = init_traditional_PKWARE_decryption(a);
3154		if (r != ARCHIVE_OK)
3155			return (r);
3156		zip->init_decryption = 0;
3157	}
3158
3159	/* We're streaming and we don't know the length. */
3160	/* If the body is compressed and we know the format, we can
3161	 * find an exact end-of-entry by decompressing it. */
3162	switch (zip->entry->compression) {
3163#ifdef HAVE_ZLIB_H
3164	case 8: /* Deflate compression. */
3165		while (!zip->end_of_entry) {
3166			int64_t offset = 0;
3167			const void *buff = NULL;
3168			size_t size = 0;
3169			int r;
3170			r =  zip_read_data_deflate(a, &buff, &size, &offset);
3171			if (r != ARCHIVE_OK)
3172				return (r);
3173		}
3174		return ARCHIVE_OK;
3175#endif
3176	default: /* Uncompressed or unknown. */
3177		/* Scan for a PK\007\010 signature. */
3178		for (;;) {
3179			const char *p, *buff;
3180			ssize_t bytes_avail;
3181			buff = __archive_read_ahead(a, 16, &bytes_avail);
3182			if (bytes_avail < 16) {
3183				archive_set_error(&a->archive,
3184				    ARCHIVE_ERRNO_FILE_FORMAT,
3185				    "Truncated ZIP file data");
3186				return (ARCHIVE_FATAL);
3187			}
3188			p = buff;
3189			while (p <= buff + bytes_avail - 16) {
3190				if (p[3] == 'P') { p += 3; }
3191				else if (p[3] == 'K') { p += 2; }
3192				else if (p[3] == '\007') { p += 1; }
3193				else if (p[3] == '\010' && p[2] == '\007'
3194				    && p[1] == 'K' && p[0] == 'P') {
3195					if (zip->entry->flags & LA_USED_ZIP64)
3196						__archive_read_consume(a,
3197						    p - buff + 24);
3198					else
3199						__archive_read_consume(a,
3200						    p - buff + 16);
3201					return ARCHIVE_OK;
3202				} else { p += 4; }
3203			}
3204			__archive_read_consume(a, p - buff);
3205		}
3206	}
3207}
3208
3209int
3210archive_read_support_format_zip_streamable(struct archive *_a)
3211{
3212	struct archive_read *a = (struct archive_read *)_a;
3213	struct zip *zip;
3214	int r;
3215
3216	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
3217	    ARCHIVE_STATE_NEW, "archive_read_support_format_zip");
3218
3219	zip = (struct zip *)calloc(1, sizeof(*zip));
3220	if (zip == NULL) {
3221		archive_set_error(&a->archive, ENOMEM,
3222		    "Can't allocate zip data");
3223		return (ARCHIVE_FATAL);
3224	}
3225
3226	/* Streamable reader doesn't support mac extensions. */
3227	zip->process_mac_extensions = 0;
3228
3229	/*
3230	 * Until enough data has been read, we cannot tell about
3231	 * any encrypted entries yet.
3232	 */
3233	zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
3234	zip->crc32func = real_crc32;
3235
3236	r = __archive_read_register_format(a,
3237	    zip,
3238	    "zip",
3239	    archive_read_format_zip_streamable_bid,
3240	    archive_read_format_zip_options,
3241	    archive_read_format_zip_streamable_read_header,
3242	    archive_read_format_zip_read_data,
3243	    archive_read_format_zip_read_data_skip_streamable,
3244	    NULL,
3245	    archive_read_format_zip_cleanup,
3246	    archive_read_support_format_zip_capabilities_streamable,
3247	    archive_read_format_zip_has_encrypted_entries);
3248
3249	if (r != ARCHIVE_OK)
3250		free(zip);
3251	return (ARCHIVE_OK);
3252}
3253
3254/* ------------------------------------------------------------------------ */
3255
3256/*
3257 * Seeking-mode support
3258 */
3259
3260static int
3261archive_read_support_format_zip_capabilities_seekable(struct archive_read * a)
3262{
3263	(void)a; /* UNUSED */
3264	return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
3265		ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
3266}
3267
3268/*
3269 * TODO: This is a performance sink because it forces the read core to
3270 * drop buffered data from the start of file, which will then have to
3271 * be re-read again if this bidder loses.
3272 *
3273 * We workaround this a little by passing in the best bid so far so
3274 * that later bidders can do nothing if they know they'll never
3275 * outbid.  But we can certainly do better...
3276 */
3277static int
3278read_eocd(struct zip *zip, const char *p, int64_t current_offset)
3279{
3280	/* Sanity-check the EOCD we've found. */
3281
3282	/* This must be the first volume. */
3283	if (archive_le16dec(p + 4) != 0)
3284		return 0;
3285	/* Central directory must be on this volume. */
3286	if (archive_le16dec(p + 4) != archive_le16dec(p + 6))
3287		return 0;
3288	/* All central directory entries must be on this volume. */
3289	if (archive_le16dec(p + 10) != archive_le16dec(p + 8))
3290		return 0;
3291	/* Central directory can't extend beyond start of EOCD record. */
3292	if (archive_le32dec(p + 16) + archive_le32dec(p + 12)
3293	    > current_offset)
3294		return 0;
3295
3296	/* Save the central directory location for later use. */
3297	zip->central_directory_offset = archive_le32dec(p + 16);
3298
3299	/* This is just a tiny bit higher than the maximum
3300	   returned by the streaming Zip bidder.  This ensures
3301	   that the more accurate seeking Zip parser wins
3302	   whenever seek is available. */
3303	return 32;
3304}
3305
3306/*
3307 * Examine Zip64 EOCD locator:  If it's valid, store the information
3308 * from it.
3309 */
3310static int
3311read_zip64_eocd(struct archive_read *a, struct zip *zip, const char *p)
3312{
3313	int64_t eocd64_offset;
3314	int64_t eocd64_size;
3315
3316	/* Sanity-check the locator record. */
3317
3318	/* Central dir must be on first volume. */
3319	if (archive_le32dec(p + 4) != 0)
3320		return 0;
3321	/* Must be only a single volume. */
3322	if (archive_le32dec(p + 16) != 1)
3323		return 0;
3324
3325	/* Find the Zip64 EOCD record. */
3326	eocd64_offset = archive_le64dec(p + 8);
3327	if (__archive_read_seek(a, eocd64_offset, SEEK_SET) < 0)
3328		return 0;
3329	if ((p = __archive_read_ahead(a, 56, NULL)) == NULL)
3330		return 0;
3331	/* Make sure we can read all of it. */
3332	eocd64_size = archive_le64dec(p + 4) + 12;
3333	if (eocd64_size < 56 || eocd64_size > 16384)
3334		return 0;
3335	if ((p = __archive_read_ahead(a, (size_t)eocd64_size, NULL)) == NULL)
3336		return 0;
3337
3338	/* Sanity-check the EOCD64 */
3339	if (archive_le32dec(p + 16) != 0) /* Must be disk #0 */
3340		return 0;
3341	if (archive_le32dec(p + 20) != 0) /* CD must be on disk #0 */
3342		return 0;
3343	/* CD can't be split. */
3344	if (archive_le64dec(p + 24) != archive_le64dec(p + 32))
3345		return 0;
3346
3347	/* Save the central directory offset for later use. */
3348	zip->central_directory_offset = archive_le64dec(p + 48);
3349
3350	return 32;
3351}
3352
3353static int
3354archive_read_format_zip_seekable_bid(struct archive_read *a, int best_bid)
3355{
3356	struct zip *zip = (struct zip *)a->format->data;
3357	int64_t file_size, current_offset;
3358	const char *p;
3359	int i, tail;
3360
3361	/* If someone has already bid more than 32, then avoid
3362	   trashing the look-ahead buffers with a seek. */
3363	if (best_bid > 32)
3364		return (-1);
3365
3366	file_size = __archive_read_seek(a, 0, SEEK_END);
3367	if (file_size <= 0)
3368		return 0;
3369
3370	/* Search last 16k of file for end-of-central-directory
3371	 * record (which starts with PK\005\006) */
3372	tail = (int)zipmin(1024 * 16, file_size);
3373	current_offset = __archive_read_seek(a, -tail, SEEK_END);
3374	if (current_offset < 0)
3375		return 0;
3376	if ((p = __archive_read_ahead(a, (size_t)tail, NULL)) == NULL)
3377		return 0;
3378	/* Boyer-Moore search backwards from the end, since we want
3379	 * to match the last EOCD in the file (there can be more than
3380	 * one if there is an uncompressed Zip archive as a member
3381	 * within this Zip archive). */
3382	for (i = tail - 22; i > 0;) {
3383		switch (p[i]) {
3384		case 'P':
3385			if (memcmp(p + i, "PK\005\006", 4) == 0) {
3386				int ret = read_eocd(zip, p + i,
3387				    current_offset + i);
3388				/* Zip64 EOCD locator precedes
3389				 * regular EOCD if present. */
3390				if (i >= 20 && memcmp(p + i - 20, "PK\006\007", 4) == 0) {
3391					int ret_zip64 = read_zip64_eocd(a, zip, p + i - 20);
3392					if (ret_zip64 > ret)
3393						ret = ret_zip64;
3394				}
3395				return (ret);
3396			}
3397			i -= 4;
3398			break;
3399		case 'K': i -= 1; break;
3400		case 005: i -= 2; break;
3401		case 006: i -= 3; break;
3402		default: i -= 4; break;
3403		}
3404	}
3405	return 0;
3406}
3407
3408/* The red-black trees are only used in seeking mode to manage
3409 * the in-memory copy of the central directory. */
3410
3411static int
3412cmp_node(const struct archive_rb_node *n1, const struct archive_rb_node *n2)
3413{
3414	const struct zip_entry *e1 = (const struct zip_entry *)n1;
3415	const struct zip_entry *e2 = (const struct zip_entry *)n2;
3416
3417	if (e1->local_header_offset > e2->local_header_offset)
3418		return -1;
3419	if (e1->local_header_offset < e2->local_header_offset)
3420		return 1;
3421	return 0;
3422}
3423
3424static int
3425cmp_key(const struct archive_rb_node *n, const void *key)
3426{
3427	/* This function won't be called */
3428	(void)n; /* UNUSED */
3429	(void)key; /* UNUSED */
3430	return 1;
3431}
3432
3433static const struct archive_rb_tree_ops rb_ops = {
3434	&cmp_node, &cmp_key
3435};
3436
3437static int
3438rsrc_cmp_node(const struct archive_rb_node *n1,
3439    const struct archive_rb_node *n2)
3440{
3441	const struct zip_entry *e1 = (const struct zip_entry *)n1;
3442	const struct zip_entry *e2 = (const struct zip_entry *)n2;
3443
3444	return (strcmp(e2->rsrcname.s, e1->rsrcname.s));
3445}
3446
3447static int
3448rsrc_cmp_key(const struct archive_rb_node *n, const void *key)
3449{
3450	const struct zip_entry *e = (const struct zip_entry *)n;
3451	return (strcmp((const char *)key, e->rsrcname.s));
3452}
3453
3454static const struct archive_rb_tree_ops rb_rsrc_ops = {
3455	&rsrc_cmp_node, &rsrc_cmp_key
3456};
3457
3458static const char *
3459rsrc_basename(const char *name, size_t name_length)
3460{
3461	const char *s, *r;
3462
3463	r = s = name;
3464	for (;;) {
3465		s = memchr(s, '/', name_length - (s - name));
3466		if (s == NULL)
3467			break;
3468		r = ++s;
3469	}
3470	return (r);
3471}
3472
3473static void
3474expose_parent_dirs(struct zip *zip, const char *name, size_t name_length)
3475{
3476	struct archive_string str;
3477	struct zip_entry *dir;
3478	char *s;
3479
3480	archive_string_init(&str);
3481	archive_strncpy(&str, name, name_length);
3482	for (;;) {
3483		s = strrchr(str.s, '/');
3484		if (s == NULL)
3485			break;
3486		*s = '\0';
3487		/* Transfer the parent directory from zip->tree_rsrc RB
3488		 * tree to zip->tree RB tree to expose. */
3489		dir = (struct zip_entry *)
3490		    __archive_rb_tree_find_node(&zip->tree_rsrc, str.s);
3491		if (dir == NULL)
3492			break;
3493		__archive_rb_tree_remove_node(&zip->tree_rsrc, &dir->node);
3494		archive_string_free(&dir->rsrcname);
3495		__archive_rb_tree_insert_node(&zip->tree, &dir->node);
3496	}
3497	archive_string_free(&str);
3498}
3499
3500static int
3501slurp_central_directory(struct archive_read *a, struct archive_entry* entry,
3502    struct zip *zip)
3503{
3504	ssize_t i;
3505	unsigned found;
3506	int64_t correction;
3507	ssize_t bytes_avail;
3508	const char *p;
3509
3510	/*
3511	 * Find the start of the central directory.  The end-of-CD
3512	 * record has our starting point, but there are lots of
3513	 * Zip archives which have had other data prepended to the
3514	 * file, which makes the recorded offsets all too small.
3515	 * So we search forward from the specified offset until we
3516	 * find the real start of the central directory.  Then we
3517	 * know the correction we need to apply to account for leading
3518	 * padding.
3519	 */
3520	if (__archive_read_seek(a, zip->central_directory_offset, SEEK_SET) < 0)
3521		return ARCHIVE_FATAL;
3522
3523	found = 0;
3524	while (!found) {
3525		if ((p = __archive_read_ahead(a, 20, &bytes_avail)) == NULL)
3526			return ARCHIVE_FATAL;
3527		for (found = 0, i = 0; !found && i < bytes_avail - 4;) {
3528			switch (p[i + 3]) {
3529			case 'P': i += 3; break;
3530			case 'K': i += 2; break;
3531			case 001: i += 1; break;
3532			case 002:
3533				if (memcmp(p + i, "PK\001\002", 4) == 0) {
3534					p += i;
3535					found = 1;
3536				} else
3537					i += 4;
3538				break;
3539			case 005: i += 1; break;
3540			case 006:
3541				if (memcmp(p + i, "PK\005\006", 4) == 0) {
3542					p += i;
3543					found = 1;
3544				} else if (memcmp(p + i, "PK\006\006", 4) == 0) {
3545					p += i;
3546					found = 1;
3547				} else
3548					i += 1;
3549				break;
3550			default: i += 4; break;
3551			}
3552		}
3553		__archive_read_consume(a, i);
3554	}
3555	correction = archive_filter_bytes(&a->archive, 0)
3556			- zip->central_directory_offset;
3557
3558	__archive_rb_tree_init(&zip->tree, &rb_ops);
3559	__archive_rb_tree_init(&zip->tree_rsrc, &rb_rsrc_ops);
3560
3561	zip->central_directory_entries_total = 0;
3562	while (1) {
3563		struct zip_entry *zip_entry;
3564		size_t filename_length, extra_length, comment_length;
3565		uint32_t external_attributes;
3566		const char *name, *r;
3567
3568		if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
3569			return ARCHIVE_FATAL;
3570		if (memcmp(p, "PK\006\006", 4) == 0
3571		    || memcmp(p, "PK\005\006", 4) == 0) {
3572			break;
3573		} else if (memcmp(p, "PK\001\002", 4) != 0) {
3574			archive_set_error(&a->archive,
3575			    -1, "Invalid central directory signature");
3576			return ARCHIVE_FATAL;
3577		}
3578		if ((p = __archive_read_ahead(a, 46, NULL)) == NULL)
3579			return ARCHIVE_FATAL;
3580
3581		zip_entry = calloc(1, sizeof(struct zip_entry));
3582		if (zip_entry == NULL) {
3583			archive_set_error(&a->archive, ENOMEM,
3584				"Can't allocate zip entry");
3585			return ARCHIVE_FATAL;
3586		}
3587		zip_entry->next = zip->zip_entries;
3588		zip_entry->flags |= LA_FROM_CENTRAL_DIRECTORY;
3589		zip->zip_entries = zip_entry;
3590		zip->central_directory_entries_total++;
3591
3592		/* version = p[4]; */
3593		zip_entry->system = p[5];
3594		/* version_required = archive_le16dec(p + 6); */
3595		zip_entry->zip_flags = archive_le16dec(p + 8);
3596		if (zip_entry->zip_flags
3597		      & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)){
3598			zip->has_encrypted_entries = 1;
3599		}
3600		zip_entry->compression = (char)archive_le16dec(p + 10);
3601		zip_entry->mtime = zip_time(p + 12);
3602		zip_entry->crc32 = archive_le32dec(p + 16);
3603		if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
3604			zip_entry->decdat = p[13];
3605		else
3606			zip_entry->decdat = p[19];
3607		zip_entry->compressed_size = archive_le32dec(p + 20);
3608		zip_entry->uncompressed_size = archive_le32dec(p + 24);
3609		filename_length = archive_le16dec(p + 28);
3610		extra_length = archive_le16dec(p + 30);
3611		comment_length = archive_le16dec(p + 32);
3612		/* disk_start = archive_le16dec(p + 34);
3613		 *   Better be zero.
3614		 * internal_attributes = archive_le16dec(p + 36);
3615		 *   text bit */
3616		external_attributes = archive_le32dec(p + 38);
3617		zip_entry->local_header_offset =
3618		    archive_le32dec(p + 42) + correction;
3619
3620		/* If we can't guess the mode, leave it zero here;
3621		   when we read the local file header we might get
3622		   more information. */
3623		if (zip_entry->system == 3) {
3624			zip_entry->mode = external_attributes >> 16;
3625		} else if (zip_entry->system == 0) {
3626			// Interpret MSDOS directory bit
3627			if (0x10 == (external_attributes & 0x10)) {
3628				zip_entry->mode = AE_IFDIR | 0775;
3629			} else {
3630				zip_entry->mode = AE_IFREG | 0664;
3631			}
3632			if (0x01 == (external_attributes & 0x01)) {
3633				// Read-only bit; strip write permissions
3634				zip_entry->mode &= 0555;
3635			}
3636		} else {
3637			zip_entry->mode = 0;
3638		}
3639
3640		/* We're done with the regular data; get the filename and
3641		 * extra data. */
3642		__archive_read_consume(a, 46);
3643		p = __archive_read_ahead(a, filename_length + extra_length,
3644			NULL);
3645		if (p == NULL) {
3646			archive_set_error(&a->archive,
3647			    ARCHIVE_ERRNO_FILE_FORMAT,
3648			    "Truncated ZIP file header");
3649			return ARCHIVE_FATAL;
3650		}
3651		if (ARCHIVE_OK != process_extra(a, entry, p + filename_length,
3652		    extra_length, zip_entry)) {
3653			return ARCHIVE_FATAL;
3654		}
3655
3656		/*
3657		 * Mac resource fork files are stored under the
3658		 * "__MACOSX/" directory, so we should check if
3659		 * it is.
3660		 */
3661		if (!zip->process_mac_extensions) {
3662			/* Treat every entry as a regular entry. */
3663			__archive_rb_tree_insert_node(&zip->tree,
3664			    &zip_entry->node);
3665		} else {
3666			name = p;
3667			r = rsrc_basename(name, filename_length);
3668			if (filename_length >= 9 &&
3669			    strncmp("__MACOSX/", name, 9) == 0) {
3670				/* If this file is not a resource fork nor
3671				 * a directory. We should treat it as a non
3672				 * resource fork file to expose it. */
3673				if (name[filename_length-1] != '/' &&
3674				    (r - name < 3 || r[0] != '.' ||
3675				     r[1] != '_')) {
3676					__archive_rb_tree_insert_node(
3677					    &zip->tree, &zip_entry->node);
3678					/* Expose its parent directories. */
3679					expose_parent_dirs(zip, name,
3680					    filename_length);
3681				} else {
3682					/* This file is a resource fork file or
3683					 * a directory. */
3684					archive_strncpy(&(zip_entry->rsrcname),
3685					     name, filename_length);
3686					__archive_rb_tree_insert_node(
3687					    &zip->tree_rsrc, &zip_entry->node);
3688				}
3689			} else {
3690				/* Generate resource fork name to find its
3691				 * resource file at zip->tree_rsrc. */
3692				archive_strcpy(&(zip_entry->rsrcname),
3693				    "__MACOSX/");
3694				archive_strncat(&(zip_entry->rsrcname),
3695				    name, r - name);
3696				archive_strcat(&(zip_entry->rsrcname), "._");
3697				archive_strncat(&(zip_entry->rsrcname),
3698				    name + (r - name),
3699				    filename_length - (r - name));
3700				/* Register an entry to RB tree to sort it by
3701				 * file offset. */
3702				__archive_rb_tree_insert_node(&zip->tree,
3703				    &zip_entry->node);
3704			}
3705		}
3706
3707		/* Skip the comment too ... */
3708		__archive_read_consume(a,
3709		    filename_length + extra_length + comment_length);
3710	}
3711
3712	return ARCHIVE_OK;
3713}
3714
3715static ssize_t
3716zip_get_local_file_header_size(struct archive_read *a, size_t extra)
3717{
3718	const char *p;
3719	ssize_t filename_length, extra_length;
3720
3721	if ((p = __archive_read_ahead(a, extra + 30, NULL)) == NULL) {
3722		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3723		    "Truncated ZIP file header");
3724		return (ARCHIVE_WARN);
3725	}
3726	p += extra;
3727
3728	if (memcmp(p, "PK\003\004", 4) != 0) {
3729		archive_set_error(&a->archive, -1, "Damaged Zip archive");
3730		return ARCHIVE_WARN;
3731	}
3732	filename_length = archive_le16dec(p + 26);
3733	extra_length = archive_le16dec(p + 28);
3734
3735	return (30 + filename_length + extra_length);
3736}
3737
3738static int
3739zip_read_mac_metadata(struct archive_read *a, struct archive_entry *entry,
3740    struct zip_entry *rsrc)
3741{
3742	struct zip *zip = (struct zip *)a->format->data;
3743	unsigned char *metadata, *mp;
3744	int64_t offset = archive_filter_bytes(&a->archive, 0);
3745	size_t remaining_bytes, metadata_bytes;
3746	ssize_t hsize;
3747	int ret = ARCHIVE_OK, eof;
3748
3749	switch(rsrc->compression) {
3750	case 0:  /* No compression. */
3751		if (rsrc->uncompressed_size != rsrc->compressed_size) {
3752			archive_set_error(&a->archive,
3753			    ARCHIVE_ERRNO_FILE_FORMAT,
3754			    "Malformed OS X metadata entry: "
3755			    "inconsistent size");
3756			return (ARCHIVE_FATAL);
3757		}
3758#ifdef HAVE_ZLIB_H
3759	case 8: /* Deflate compression. */
3760#endif
3761		break;
3762	default: /* Unsupported compression. */
3763		/* Return a warning. */
3764		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3765		    "Unsupported ZIP compression method (%s)",
3766		    compression_name(rsrc->compression));
3767		/* We can't decompress this entry, but we will
3768		 * be able to skip() it and try the next entry. */
3769		return (ARCHIVE_WARN);
3770	}
3771
3772	if (rsrc->uncompressed_size > (4 * 1024 * 1024)) {
3773		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3774		    "Mac metadata is too large: %jd > 4M bytes",
3775		    (intmax_t)rsrc->uncompressed_size);
3776		return (ARCHIVE_WARN);
3777	}
3778	if (rsrc->compressed_size > (4 * 1024 * 1024)) {
3779		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
3780		    "Mac metadata is too large: %jd > 4M bytes",
3781		    (intmax_t)rsrc->compressed_size);
3782		return (ARCHIVE_WARN);
3783	}
3784
3785	metadata = malloc((size_t)rsrc->uncompressed_size);
3786	if (metadata == NULL) {
3787		archive_set_error(&a->archive, ENOMEM,
3788		    "Can't allocate memory for Mac metadata");
3789		return (ARCHIVE_FATAL);
3790	}
3791
3792	if (offset < rsrc->local_header_offset)
3793		__archive_read_consume(a, rsrc->local_header_offset - offset);
3794	else if (offset != rsrc->local_header_offset) {
3795		__archive_read_seek(a, rsrc->local_header_offset, SEEK_SET);
3796	}
3797
3798	hsize = zip_get_local_file_header_size(a, 0);
3799	__archive_read_consume(a, hsize);
3800
3801	remaining_bytes = (size_t)rsrc->compressed_size;
3802	metadata_bytes = (size_t)rsrc->uncompressed_size;
3803	mp = metadata;
3804	eof = 0;
3805	while (!eof && remaining_bytes) {
3806		const unsigned char *p;
3807		ssize_t bytes_avail;
3808		size_t bytes_used;
3809
3810		p = __archive_read_ahead(a, 1, &bytes_avail);
3811		if (p == NULL) {
3812			archive_set_error(&a->archive,
3813			    ARCHIVE_ERRNO_FILE_FORMAT,
3814			    "Truncated ZIP file header");
3815			ret = ARCHIVE_WARN;
3816			goto exit_mac_metadata;
3817		}
3818		if ((size_t)bytes_avail > remaining_bytes)
3819			bytes_avail = remaining_bytes;
3820		switch(rsrc->compression) {
3821		case 0:  /* No compression. */
3822			if ((size_t)bytes_avail > metadata_bytes)
3823				bytes_avail = metadata_bytes;
3824			memcpy(mp, p, bytes_avail);
3825			bytes_used = (size_t)bytes_avail;
3826			metadata_bytes -= bytes_used;
3827			mp += bytes_used;
3828			if (metadata_bytes == 0)
3829				eof = 1;
3830			break;
3831#ifdef HAVE_ZLIB_H
3832		case 8: /* Deflate compression. */
3833		{
3834			int r;
3835
3836			ret = zip_deflate_init(a, zip);
3837			if (ret != ARCHIVE_OK)
3838				goto exit_mac_metadata;
3839			zip->stream.next_in =
3840			    (Bytef *)(uintptr_t)(const void *)p;
3841			zip->stream.avail_in = (uInt)bytes_avail;
3842			zip->stream.total_in = 0;
3843			zip->stream.next_out = mp;
3844			zip->stream.avail_out = (uInt)metadata_bytes;
3845			zip->stream.total_out = 0;
3846
3847			r = inflate(&zip->stream, 0);
3848			switch (r) {
3849			case Z_OK:
3850				break;
3851			case Z_STREAM_END:
3852				eof = 1;
3853				break;
3854			case Z_MEM_ERROR:
3855				archive_set_error(&a->archive, ENOMEM,
3856				    "Out of memory for ZIP decompression");
3857				ret = ARCHIVE_FATAL;
3858				goto exit_mac_metadata;
3859			default:
3860				archive_set_error(&a->archive,
3861				    ARCHIVE_ERRNO_MISC,
3862				    "ZIP decompression failed (%d)", r);
3863				ret = ARCHIVE_FATAL;
3864				goto exit_mac_metadata;
3865			}
3866			bytes_used = zip->stream.total_in;
3867			metadata_bytes -= zip->stream.total_out;
3868			mp += zip->stream.total_out;
3869			break;
3870		}
3871#endif
3872		default:
3873			bytes_used = 0;
3874			break;
3875		}
3876		__archive_read_consume(a, bytes_used);
3877		remaining_bytes -= bytes_used;
3878	}
3879	archive_entry_copy_mac_metadata(entry, metadata,
3880	    (size_t)rsrc->uncompressed_size - metadata_bytes);
3881
3882exit_mac_metadata:
3883	__archive_read_seek(a, offset, SEEK_SET);
3884	zip->decompress_init = 0;
3885	free(metadata);
3886	return (ret);
3887}
3888
3889static int
3890archive_read_format_zip_seekable_read_header(struct archive_read *a,
3891	struct archive_entry *entry)
3892{
3893	struct zip *zip = (struct zip *)a->format->data;
3894	struct zip_entry *rsrc;
3895	int64_t offset;
3896	int r, ret = ARCHIVE_OK;
3897
3898	/*
3899	 * It should be sufficient to call archive_read_next_header() for
3900	 * a reader to determine if an entry is encrypted or not. If the
3901	 * encryption of an entry is only detectable when calling
3902	 * archive_read_data(), so be it. We'll do the same check there
3903	 * as well.
3904	 */
3905	if (zip->has_encrypted_entries ==
3906			ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
3907		zip->has_encrypted_entries = 0;
3908
3909	a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
3910	if (a->archive.archive_format_name == NULL)
3911		a->archive.archive_format_name = "ZIP";
3912
3913	if (zip->zip_entries == NULL) {
3914		r = slurp_central_directory(a, entry, zip);
3915		if (r != ARCHIVE_OK)
3916			return r;
3917		/* Get first entry whose local header offset is lower than
3918		 * other entries in the archive file. */
3919		zip->entry =
3920		    (struct zip_entry *)ARCHIVE_RB_TREE_MIN(&zip->tree);
3921	} else if (zip->entry != NULL) {
3922		/* Get next entry in local header offset order. */
3923		zip->entry = (struct zip_entry *)__archive_rb_tree_iterate(
3924		    &zip->tree, &zip->entry->node, ARCHIVE_RB_DIR_RIGHT);
3925	}
3926
3927	if (zip->entry == NULL)
3928		return ARCHIVE_EOF;
3929
3930	if (zip->entry->rsrcname.s)
3931		rsrc = (struct zip_entry *)__archive_rb_tree_find_node(
3932		    &zip->tree_rsrc, zip->entry->rsrcname.s);
3933	else
3934		rsrc = NULL;
3935
3936	if (zip->cctx_valid)
3937		archive_decrypto_aes_ctr_release(&zip->cctx);
3938	if (zip->hctx_valid)
3939		archive_hmac_sha1_cleanup(&zip->hctx);
3940	zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
3941	__archive_read_reset_passphrase(a);
3942
3943	/* File entries are sorted by the header offset, we should mostly
3944	 * use __archive_read_consume to advance a read point to avoid
3945	 * redundant data reading.  */
3946	offset = archive_filter_bytes(&a->archive, 0);
3947	if (offset < zip->entry->local_header_offset)
3948		__archive_read_consume(a,
3949		    zip->entry->local_header_offset - offset);
3950	else if (offset != zip->entry->local_header_offset) {
3951		__archive_read_seek(a, zip->entry->local_header_offset,
3952		    SEEK_SET);
3953	}
3954	zip->unconsumed = 0;
3955	r = zip_read_local_file_header(a, entry, zip);
3956	if (r != ARCHIVE_OK)
3957		return r;
3958	if (rsrc) {
3959		int ret2 = zip_read_mac_metadata(a, entry, rsrc);
3960		if (ret2 < ret)
3961			ret = ret2;
3962	}
3963	return (ret);
3964}
3965
3966/*
3967 * We're going to seek for the next header anyway, so we don't
3968 * need to bother doing anything here.
3969 */
3970static int
3971archive_read_format_zip_read_data_skip_seekable(struct archive_read *a)
3972{
3973	struct zip *zip;
3974	zip = (struct zip *)(a->format->data);
3975
3976	zip->unconsumed = 0;
3977	return (ARCHIVE_OK);
3978}
3979
3980int
3981archive_read_support_format_zip_seekable(struct archive *_a)
3982{
3983	struct archive_read *a = (struct archive_read *)_a;
3984	struct zip *zip;
3985	int r;
3986
3987	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
3988	    ARCHIVE_STATE_NEW, "archive_read_support_format_zip_seekable");
3989
3990	zip = (struct zip *)calloc(1, sizeof(*zip));
3991	if (zip == NULL) {
3992		archive_set_error(&a->archive, ENOMEM,
3993		    "Can't allocate zip data");
3994		return (ARCHIVE_FATAL);
3995	}
3996
3997#ifdef HAVE_COPYFILE_H
3998	/* Set this by default on Mac OS. */
3999	zip->process_mac_extensions = 1;
4000#endif
4001
4002	/*
4003	 * Until enough data has been read, we cannot tell about
4004	 * any encrypted entries yet.
4005	 */
4006	zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
4007	zip->crc32func = real_crc32;
4008
4009	r = __archive_read_register_format(a,
4010	    zip,
4011	    "zip",
4012	    archive_read_format_zip_seekable_bid,
4013	    archive_read_format_zip_options,
4014	    archive_read_format_zip_seekable_read_header,
4015	    archive_read_format_zip_read_data,
4016	    archive_read_format_zip_read_data_skip_seekable,
4017	    NULL,
4018	    archive_read_format_zip_cleanup,
4019	    archive_read_support_format_zip_capabilities_seekable,
4020	    archive_read_format_zip_has_encrypted_entries);
4021
4022	if (r != ARCHIVE_OK)
4023		free(zip);
4024	return (ARCHIVE_OK);
4025}
4026
4027/*# vim:set noet:*/
4028