archive_read_support_format_7zip.c revision 302001
1/*-
2 * Copyright (c) 2011 Michihiro NAKAJIMA
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "archive_platform.h"
27__FBSDID("$FreeBSD$");
28
29#ifdef HAVE_ERRNO_H
30#include <errno.h>
31#endif
32#ifdef HAVE_STDLIB_H
33#include <stdlib.h>
34#endif
35#ifdef HAVE_BZLIB_H
36#include <bzlib.h>
37#endif
38#ifdef HAVE_LZMA_H
39#include <lzma.h>
40#endif
41#ifdef HAVE_ZLIB_H
42#include <zlib.h>
43#endif
44
45#include "archive.h"
46#include "archive_entry.h"
47#include "archive_entry_locale.h"
48#include "archive_ppmd7_private.h"
49#include "archive_private.h"
50#include "archive_read_private.h"
51#include "archive_endian.h"
52
53#ifndef HAVE_ZLIB_H
54#include "archive_crc32.h"
55#endif
56
57#define _7ZIP_SIGNATURE	"7z\xBC\xAF\x27\x1C"
58#define SFX_MIN_ADDR	0x27000
59#define SFX_MAX_ADDR	0x60000
60
61
62/*
63 * Codec ID
64 */
65#define _7Z_COPY	0
66#define _7Z_LZMA	0x030101
67#define _7Z_LZMA2	0x21
68#define _7Z_DEFLATE	0x040108
69#define _7Z_BZ2		0x040202
70#define _7Z_PPMD	0x030401
71#define _7Z_DELTA	0x03
72#define _7Z_CRYPTO_MAIN_ZIP			0x06F10101 /* Main Zip crypto algo */
73#define _7Z_CRYPTO_RAR_29			0x06F10303 /* Rar29 AES-128 + (modified SHA-1) */
74#define _7Z_CRYPTO_AES_256_SHA_256	0x06F10701 /* AES-256 + SHA-256 */
75
76
77#define _7Z_X86		0x03030103
78#define _7Z_X86_BCJ2	0x0303011B
79#define _7Z_POWERPC	0x03030205
80#define _7Z_IA64	0x03030401
81#define _7Z_ARM		0x03030501
82#define _7Z_ARMTHUMB	0x03030701
83#define _7Z_SPARC	0x03030805
84
85/*
86 * 7-Zip header property IDs.
87 */
88#define kEnd			0x00
89#define kHeader			0x01
90#define kArchiveProperties	0x02
91#define kAdditionalStreamsInfo	0x03
92#define kMainStreamsInfo	0x04
93#define kFilesInfo		0x05
94#define kPackInfo		0x06
95#define kUnPackInfo		0x07
96#define kSubStreamsInfo		0x08
97#define kSize			0x09
98#define kCRC			0x0A
99#define kFolder			0x0B
100#define kCodersUnPackSize	0x0C
101#define kNumUnPackStream	0x0D
102#define kEmptyStream		0x0E
103#define kEmptyFile		0x0F
104#define kAnti			0x10
105#define kName			0x11
106#define kCTime			0x12
107#define kATime			0x13
108#define kMTime			0x14
109#define kAttributes		0x15
110#define kEncodedHeader		0x17
111#define kDummy			0x19
112
113struct _7z_digests {
114	unsigned char	*defineds;
115	uint32_t	*digests;
116};
117
118
119struct _7z_folder {
120	uint64_t		 numCoders;
121	struct _7z_coder {
122		unsigned long	 codec;
123		uint64_t	 numInStreams;
124		uint64_t	 numOutStreams;
125		uint64_t	 propertiesSize;
126		unsigned char	*properties;
127	} *coders;
128	uint64_t		 numBindPairs;
129	struct {
130		uint64_t	 inIndex;
131		uint64_t	 outIndex;
132	} *bindPairs;
133	uint64_t		 numPackedStreams;
134	uint64_t		*packedStreams;
135	uint64_t		 numInStreams;
136	uint64_t		 numOutStreams;
137	uint64_t		*unPackSize;
138	unsigned char		 digest_defined;
139	uint32_t		 digest;
140	uint64_t		 numUnpackStreams;
141	uint32_t		 packIndex;
142	/* Unoperated bytes. */
143	uint64_t		 skipped_bytes;
144};
145
146struct _7z_coders_info {
147	uint64_t		 numFolders;
148	struct _7z_folder	*folders;
149	uint64_t		 dataStreamIndex;
150};
151
152struct _7z_pack_info {
153	uint64_t		 pos;
154	uint64_t		 numPackStreams;
155	uint64_t		*sizes;
156	struct _7z_digests	 digest;
157	/* Calculated from pos and numPackStreams. */
158	uint64_t		*positions;
159};
160
161struct _7z_substream_info {
162	size_t			 unpack_streams;
163	uint64_t		*unpackSizes;
164	unsigned char		*digestsDefined;
165	uint32_t		*digests;
166};
167
168struct _7z_stream_info {
169	struct _7z_pack_info	 pi;
170	struct _7z_coders_info	 ci;
171	struct _7z_substream_info ss;
172};
173
174struct _7z_header_info {
175	uint64_t		 dataIndex;
176
177	unsigned char		*emptyStreamBools;
178	unsigned char		*emptyFileBools;
179	unsigned char		*antiBools;
180	unsigned char		*attrBools;
181};
182
183struct _7zip_entry {
184	size_t			 name_len;
185	unsigned char		*utf16name;
186#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
187	const wchar_t		*wname;
188#endif
189	uint32_t		 folderIndex;
190	uint32_t		 ssIndex;
191	unsigned		 flg;
192#define MTIME_IS_SET	(1<<0)
193#define ATIME_IS_SET	(1<<1)
194#define CTIME_IS_SET	(1<<2)
195#define CRC32_IS_SET	(1<<3)
196#define HAS_STREAM	(1<<4)
197
198	time_t			 mtime;
199	time_t			 atime;
200	time_t			 ctime;
201	long			 mtime_ns;
202	long			 atime_ns;
203	long			 ctime_ns;
204	uint32_t		 mode;
205	uint32_t		 attr;
206};
207
208struct _7zip {
209	/* Structural information about the archive. */
210	struct _7z_stream_info	 si;
211
212	int			 header_is_being_read;
213	int			 header_is_encoded;
214	uint64_t		 header_bytes_remaining;
215	unsigned long		 header_crc32;
216	/* Header offset to check that reading pointes of the file contens
217	 * will not exceed the header. */
218	uint64_t		 header_offset;
219	/* Base offset of the archive file for a seek in case reading SFX. */
220	uint64_t		 seek_base;
221
222	/* List of entries */
223	size_t			 entries_remaining;
224	uint64_t		 numFiles;
225	struct _7zip_entry	*entries;
226	struct _7zip_entry	*entry;
227	unsigned char		*entry_names;
228
229	/* entry_bytes_remaining is the number of bytes we expect. */
230	int64_t			 entry_offset;
231	uint64_t		 entry_bytes_remaining;
232
233	/* Running CRC32 of the decompressed data */
234	unsigned long		 entry_crc32;
235
236	/* Flags to mark progress of decompression. */
237	char			 end_of_entry;
238
239	/* Uncompressed buffer control.  */
240#define UBUFF_SIZE	(64 * 1024)
241	unsigned char 		*uncompressed_buffer;
242	unsigned char 		*uncompressed_buffer_pointer;
243	size_t 			 uncompressed_buffer_size;
244	size_t			 uncompressed_buffer_bytes_remaining;
245
246	/* Offset of the compressed data. */
247	int64_t			 stream_offset;
248
249	/*
250	 * Decompressing control data.
251	 */
252	unsigned		 folder_index;
253	uint64_t		 folder_outbytes_remaining;
254	unsigned		 pack_stream_index;
255	unsigned		 pack_stream_remaining;
256	uint64_t		 pack_stream_inbytes_remaining;
257	size_t			 pack_stream_bytes_unconsumed;
258
259	/* The codec information of a folder. */
260	unsigned long		 codec;
261	unsigned long		 codec2;
262
263	/*
264	 * Decompressor controllers.
265	 */
266	/* Decording LZMA1 and LZMA2 data. */
267#ifdef HAVE_LZMA_H
268	lzma_stream		 lzstream;
269	int			 lzstream_valid;
270#endif
271	/* Decording bzip2 data. */
272#if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
273	bz_stream		 bzstream;
274	int			 bzstream_valid;
275#endif
276	/* Decording deflate data. */
277#ifdef HAVE_ZLIB_H
278	z_stream		 stream;
279	int			 stream_valid;
280#endif
281	/* Decording PPMd data. */
282	int			 ppmd7_stat;
283	CPpmd7			 ppmd7_context;
284	CPpmd7z_RangeDec	 range_dec;
285	IByteIn			 bytein;
286	struct {
287		const unsigned char	*next_in;
288		int64_t			 avail_in;
289		int64_t			 total_in;
290		unsigned char		*next_out;
291		int64_t			 avail_out;
292		int64_t			 total_out;
293		int			 overconsumed;
294	} ppstream;
295	int			 ppmd7_valid;
296
297	/* Decoding BCJ and BCJ2 data. */
298	uint32_t		 bcj_state;
299	size_t			 odd_bcj_size;
300	unsigned char		 odd_bcj[4];
301	/* Decoding BCJ data. */
302	size_t			 bcj_prevPosT;
303	uint32_t		 bcj_prevMask;
304	uint32_t		 bcj_ip;
305
306	/* Decoding BCJ2 data. */
307	size_t			 main_stream_bytes_remaining;
308	unsigned char		*sub_stream_buff[3];
309	size_t			 sub_stream_size[3];
310	size_t			 sub_stream_bytes_remaining[3];
311	unsigned char		*tmp_stream_buff;
312	size_t			 tmp_stream_buff_size;
313	size_t			 tmp_stream_bytes_avail;
314	size_t			 tmp_stream_bytes_remaining;
315#ifdef _LZMA_PROB32
316#define CProb uint32_t
317#else
318#define CProb uint16_t
319#endif
320	CProb			 bcj2_p[256 + 2];
321	uint8_t			 bcj2_prevByte;
322	uint32_t		 bcj2_range;
323	uint32_t		 bcj2_code;
324	uint64_t		 bcj2_outPos;
325
326	/* Filename character-set conversion data. */
327	struct archive_string_conv *sconv;
328
329	char			 format_name[64];
330
331	/* Custom value that is non-zero if this archive contains encrypted entries. */
332	int			 has_encrypted_entries;
333};
334
335/* Maximum entry size. This limitation prevents reading intentional
336 * corrupted 7-zip files on assuming there are not so many entries in
337 * the files. */
338#define UMAX_ENTRY	ARCHIVE_LITERAL_ULL(100000000)
339
340static int	archive_read_format_7zip_has_encrypted_entries(struct archive_read *);
341static int	archive_read_support_format_7zip_capabilities(struct archive_read *a);
342static int	archive_read_format_7zip_bid(struct archive_read *, int);
343static int	archive_read_format_7zip_cleanup(struct archive_read *);
344static int	archive_read_format_7zip_read_data(struct archive_read *,
345		    const void **, size_t *, int64_t *);
346static int	archive_read_format_7zip_read_data_skip(struct archive_read *);
347static int	archive_read_format_7zip_read_header(struct archive_read *,
348		    struct archive_entry *);
349static int	check_7zip_header_in_sfx(const char *);
350static unsigned long decode_codec_id(const unsigned char *, size_t);
351static int	decode_encoded_header_info(struct archive_read *,
352		    struct _7z_stream_info *);
353static int	decompress(struct archive_read *, struct _7zip *,
354		    void *, size_t *, const void *, size_t *);
355static ssize_t	extract_pack_stream(struct archive_read *, size_t);
356static void	fileTimeToUtc(uint64_t, time_t *, long *);
357static uint64_t folder_uncompressed_size(struct _7z_folder *);
358static void	free_CodersInfo(struct _7z_coders_info *);
359static void	free_Digest(struct _7z_digests *);
360static void	free_Folder(struct _7z_folder *);
361static void	free_Header(struct _7z_header_info *);
362static void	free_PackInfo(struct _7z_pack_info *);
363static void	free_StreamsInfo(struct _7z_stream_info *);
364static void	free_SubStreamsInfo(struct _7z_substream_info *);
365static int	free_decompression(struct archive_read *, struct _7zip *);
366static ssize_t	get_uncompressed_data(struct archive_read *, const void **,
367		    size_t, size_t);
368static const unsigned char * header_bytes(struct archive_read *, size_t);
369static int	init_decompression(struct archive_read *, struct _7zip *,
370		    const struct _7z_coder *, const struct _7z_coder *);
371static int	parse_7zip_uint64(struct archive_read *, uint64_t *);
372static int	read_Bools(struct archive_read *, unsigned char *, size_t);
373static int	read_CodersInfo(struct archive_read *,
374		    struct _7z_coders_info *);
375static int	read_Digests(struct archive_read *, struct _7z_digests *,
376		    size_t);
377static int	read_Folder(struct archive_read *, struct _7z_folder *);
378static int	read_Header(struct archive_read *, struct _7z_header_info *,
379		    int);
380static int	read_PackInfo(struct archive_read *, struct _7z_pack_info *);
381static int	read_StreamsInfo(struct archive_read *,
382		    struct _7z_stream_info *);
383static int	read_SubStreamsInfo(struct archive_read *,
384		    struct _7z_substream_info *, struct _7z_folder *, size_t);
385static int	read_Times(struct archive_read *, struct _7z_header_info *,
386		    int);
387static void	read_consume(struct archive_read *);
388static ssize_t	read_stream(struct archive_read *, const void **, size_t,
389		    size_t);
390static int	seek_pack(struct archive_read *);
391static int64_t	skip_stream(struct archive_read *, size_t);
392static int	skip_sfx(struct archive_read *, ssize_t);
393static int	slurp_central_directory(struct archive_read *, struct _7zip *,
394		    struct _7z_header_info *);
395static int	setup_decode_folder(struct archive_read *, struct _7z_folder *,
396		    int);
397static void	x86_Init(struct _7zip *);
398static size_t	x86_Convert(struct _7zip *, uint8_t *, size_t);
399static ssize_t		Bcj2_Decode(struct _7zip *, uint8_t *, size_t);
400
401
402int
403archive_read_support_format_7zip(struct archive *_a)
404{
405	struct archive_read *a = (struct archive_read *)_a;
406	struct _7zip *zip;
407	int r;
408
409	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
410	    ARCHIVE_STATE_NEW, "archive_read_support_format_7zip");
411
412	zip = calloc(1, sizeof(*zip));
413	if (zip == NULL) {
414		archive_set_error(&a->archive, ENOMEM,
415		    "Can't allocate 7zip data");
416		return (ARCHIVE_FATAL);
417	}
418
419	/*
420	 * Until enough data has been read, we cannot tell about
421	 * any encrypted entries yet.
422	 */
423	zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
424
425
426	r = __archive_read_register_format(a,
427	    zip,
428	    "7zip",
429	    archive_read_format_7zip_bid,
430	    NULL,
431	    archive_read_format_7zip_read_header,
432	    archive_read_format_7zip_read_data,
433	    archive_read_format_7zip_read_data_skip,
434	    NULL,
435	    archive_read_format_7zip_cleanup,
436	    archive_read_support_format_7zip_capabilities,
437	    archive_read_format_7zip_has_encrypted_entries);
438
439	if (r != ARCHIVE_OK)
440		free(zip);
441	return (ARCHIVE_OK);
442}
443
444static int
445archive_read_support_format_7zip_capabilities(struct archive_read * a)
446{
447	(void)a; /* UNUSED */
448	return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
449			ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
450}
451
452
453static int
454archive_read_format_7zip_has_encrypted_entries(struct archive_read *_a)
455{
456	if (_a && _a->format) {
457		struct _7zip * zip = (struct _7zip *)_a->format->data;
458		if (zip) {
459			return zip->has_encrypted_entries;
460		}
461	}
462	return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
463}
464
465static int
466archive_read_format_7zip_bid(struct archive_read *a, int best_bid)
467{
468	const char *p;
469
470	/* If someone has already bid more than 32, then avoid
471	   trashing the look-ahead buffers with a seek. */
472	if (best_bid > 32)
473		return (-1);
474
475	if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
476		return (0);
477
478	/* If first six bytes are the 7-Zip signature,
479	 * return the bid right now. */
480	if (memcmp(p, _7ZIP_SIGNATURE, 6) == 0)
481		return (48);
482
483	/*
484	 * It may a 7-Zip SFX archive file. If first two bytes are
485	 * 'M' and 'Z' available on Windows or first four bytes are
486	 * "\x7F\x45LF" available on posix like system, seek the 7-Zip
487	 * signature. Although we will perform a seek when reading
488	 * a header, what we do not use __archive_read_seek() here is
489	 * due to a bidding performance.
490	 */
491	if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
492		ssize_t offset = SFX_MIN_ADDR;
493		ssize_t window = 4096;
494		ssize_t bytes_avail;
495		while (offset + window <= (SFX_MAX_ADDR)) {
496			const char *buff = __archive_read_ahead(a,
497					offset + window, &bytes_avail);
498			if (buff == NULL) {
499				/* Remaining bytes are less than window. */
500				window >>= 1;
501				if (window < 0x40)
502					return (0);
503				continue;
504			}
505			p = buff + offset;
506			while (p + 32 < buff + bytes_avail) {
507				int step = check_7zip_header_in_sfx(p);
508				if (step == 0)
509					return (48);
510				p += step;
511			}
512			offset = p - buff;
513		}
514	}
515	return (0);
516}
517
518static int
519check_7zip_header_in_sfx(const char *p)
520{
521	switch ((unsigned char)p[5]) {
522	case 0x1C:
523		if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0)
524			return (6);
525		/*
526		 * Test the CRC because its extraction code has 7-Zip
527		 * Magic Code, so we should do this in order not to
528		 * make a mis-detection.
529		 */
530		if (crc32(0, (const unsigned char *)p + 12, 20)
531			!= archive_le32dec(p + 8))
532			return (6);
533		/* Hit the header! */
534		return (0);
535	case 0x37: return (5);
536	case 0x7A: return (4);
537	case 0xBC: return (3);
538	case 0xAF: return (2);
539	case 0x27: return (1);
540	default: return (6);
541	}
542}
543
544static int
545skip_sfx(struct archive_read *a, ssize_t bytes_avail)
546{
547	const void *h;
548	const char *p, *q;
549	size_t skip, offset;
550	ssize_t bytes, window;
551
552	/*
553	 * If bytes_avail > SFX_MIN_ADDR we do not have to call
554	 * __archive_read_seek() at this time since we have
555	 * alredy had enough data.
556	 */
557	if (bytes_avail > SFX_MIN_ADDR)
558		__archive_read_consume(a, SFX_MIN_ADDR);
559	else if (__archive_read_seek(a, SFX_MIN_ADDR, SEEK_SET) < 0)
560		return (ARCHIVE_FATAL);
561
562	offset = 0;
563	window = 1;
564	while (offset + window <= SFX_MAX_ADDR - SFX_MIN_ADDR) {
565		h = __archive_read_ahead(a, window, &bytes);
566		if (h == NULL) {
567			/* Remaining bytes are less than window. */
568			window >>= 1;
569			if (window < 0x40)
570				goto fatal;
571			continue;
572		}
573		if (bytes < 6) {
574			/* This case might happen when window == 1. */
575			window = 4096;
576			continue;
577		}
578		p = (const char *)h;
579		q = p + bytes;
580
581		/*
582		 * Scan ahead until we find something that looks
583		 * like the 7-Zip header.
584		 */
585		while (p + 32 < q) {
586			int step = check_7zip_header_in_sfx(p);
587			if (step == 0) {
588				struct _7zip *zip =
589				    (struct _7zip *)a->format->data;
590				skip = p - (const char *)h;
591				__archive_read_consume(a, skip);
592				zip->seek_base = SFX_MIN_ADDR + offset + skip;
593				return (ARCHIVE_OK);
594			}
595			p += step;
596		}
597		skip = p - (const char *)h;
598		__archive_read_consume(a, skip);
599		offset += skip;
600		if (window == 1)
601			window = 4096;
602	}
603fatal:
604	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
605	    "Couldn't find out 7-Zip header");
606	return (ARCHIVE_FATAL);
607}
608
609static int
610archive_read_format_7zip_read_header(struct archive_read *a,
611	struct archive_entry *entry)
612{
613	struct _7zip *zip = (struct _7zip *)a->format->data;
614	struct _7zip_entry *zip_entry;
615	int r, ret = ARCHIVE_OK;
616	struct _7z_folder *folder = 0;
617	uint64_t fidx = 0;
618
619	/*
620	 * It should be sufficient to call archive_read_next_header() for
621	 * a reader to determine if an entry is encrypted or not. If the
622	 * encryption of an entry is only detectable when calling
623	 * archive_read_data(), so be it. We'll do the same check there
624	 * as well.
625	 */
626	if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
627		zip->has_encrypted_entries = 0;
628	}
629
630	a->archive.archive_format = ARCHIVE_FORMAT_7ZIP;
631	if (a->archive.archive_format_name == NULL)
632		a->archive.archive_format_name = "7-Zip";
633
634	if (zip->entries == NULL) {
635		struct _7z_header_info header;
636
637		memset(&header, 0, sizeof(header));
638		r = slurp_central_directory(a, zip, &header);
639		free_Header(&header);
640		if (r != ARCHIVE_OK)
641			return (r);
642		zip->entries_remaining = (size_t)zip->numFiles;
643		zip->entry = zip->entries;
644	} else {
645		++zip->entry;
646	}
647	zip_entry = zip->entry;
648
649	if (zip->entries_remaining <= 0 || zip_entry == NULL)
650		return ARCHIVE_EOF;
651	--zip->entries_remaining;
652
653	zip->entry_offset = 0;
654	zip->end_of_entry = 0;
655	zip->entry_crc32 = crc32(0, NULL, 0);
656
657	/* Setup a string conversion for a filename. */
658	if (zip->sconv == NULL) {
659		zip->sconv = archive_string_conversion_from_charset(
660		    &a->archive, "UTF-16LE", 1);
661		if (zip->sconv == NULL)
662			return (ARCHIVE_FATAL);
663	}
664
665	/* Figure out if the entry is encrypted by looking at the folder
666	   that is associated to the current 7zip entry. If the folder
667	   has a coder with a _7Z_CRYPTO codec then the folder is encrypted.
668	   Hence the entry must also be encrypted. */
669	if (zip_entry && zip_entry->folderIndex < zip->si.ci.numFolders) {
670		folder = &(zip->si.ci.folders[zip_entry->folderIndex]);
671		for (fidx=0; folder && fidx<folder->numCoders; fidx++) {
672			switch(folder->coders[fidx].codec) {
673				case _7Z_CRYPTO_MAIN_ZIP:
674				case _7Z_CRYPTO_RAR_29:
675				case _7Z_CRYPTO_AES_256_SHA_256: {
676					archive_entry_set_is_data_encrypted(entry, 1);
677					zip->has_encrypted_entries = 1;
678					break;
679				}
680			}
681		}
682	}
683
684	/* Now that we've checked for encryption, if there were still no
685	 * encrypted entries found we can say for sure that there are none.
686	 */
687	if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
688		zip->has_encrypted_entries = 0;
689	}
690
691	if (archive_entry_copy_pathname_l(entry,
692	    (const char *)zip_entry->utf16name,
693	    zip_entry->name_len, zip->sconv) != 0) {
694		if (errno == ENOMEM) {
695			archive_set_error(&a->archive, ENOMEM,
696			    "Can't allocate memory for Pathname");
697			return (ARCHIVE_FATAL);
698		}
699		archive_set_error(&a->archive,
700		    ARCHIVE_ERRNO_FILE_FORMAT,
701		    "Pathname cannot be converted "
702		    "from %s to current locale.",
703		    archive_string_conversion_charset_name(zip->sconv));
704		ret = ARCHIVE_WARN;
705	}
706
707	/* Populate some additional entry fields: */
708	archive_entry_set_mode(entry, zip_entry->mode);
709	if (zip_entry->flg & MTIME_IS_SET)
710		archive_entry_set_mtime(entry, zip_entry->mtime,
711			zip_entry->mtime_ns);
712	if (zip_entry->flg & CTIME_IS_SET)
713		archive_entry_set_ctime(entry, zip_entry->ctime,
714		    zip_entry->ctime_ns);
715	if (zip_entry->flg & ATIME_IS_SET)
716		archive_entry_set_atime(entry, zip_entry->atime,
717		    zip_entry->atime_ns);
718	if (zip_entry->ssIndex != (uint32_t)-1) {
719		zip->entry_bytes_remaining =
720		    zip->si.ss.unpackSizes[zip_entry->ssIndex];
721		archive_entry_set_size(entry, zip->entry_bytes_remaining);
722	} else {
723		zip->entry_bytes_remaining = 0;
724		archive_entry_set_size(entry, 0);
725	}
726
727	/* If there's no body, force read_data() to return EOF immediately. */
728	if (zip->entry_bytes_remaining < 1)
729		zip->end_of_entry = 1;
730
731	if ((zip_entry->mode & AE_IFMT) == AE_IFLNK) {
732		unsigned char *symname = NULL;
733		size_t symsize = 0;
734
735		/*
736		 * Symbolic-name is recorded as its contents. We have to
737		 * read the contents at this time.
738		 */
739		while (zip->entry_bytes_remaining > 0) {
740			const void *buff;
741			unsigned char *mem;
742			size_t size;
743			int64_t offset;
744
745			r = archive_read_format_7zip_read_data(a, &buff,
746				&size, &offset);
747			if (r < ARCHIVE_WARN) {
748				free(symname);
749				return (r);
750			}
751			mem = realloc(symname, symsize + size + 1);
752			if (mem == NULL) {
753				free(symname);
754				archive_set_error(&a->archive, ENOMEM,
755				    "Can't allocate memory for Symname");
756				return (ARCHIVE_FATAL);
757			}
758			symname = mem;
759			memcpy(symname+symsize, buff, size);
760			symsize += size;
761		}
762		if (symsize == 0) {
763			/* If there is no synname, handle it as a regular
764			 * file. */
765			zip_entry->mode &= ~AE_IFMT;
766			zip_entry->mode |= AE_IFREG;
767			archive_entry_set_mode(entry, zip_entry->mode);
768		} else {
769			symname[symsize] = '\0';
770			archive_entry_copy_symlink(entry,
771			    (const char *)symname);
772		}
773		free(symname);
774		archive_entry_set_size(entry, 0);
775	}
776
777	/* Set up a more descriptive format name. */
778	sprintf(zip->format_name, "7-Zip");
779	a->archive.archive_format_name = zip->format_name;
780
781	return (ret);
782}
783
784static int
785archive_read_format_7zip_read_data(struct archive_read *a,
786    const void **buff, size_t *size, int64_t *offset)
787{
788	struct _7zip *zip;
789	ssize_t bytes;
790	int ret = ARCHIVE_OK;
791
792	zip = (struct _7zip *)(a->format->data);
793
794	if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
795		zip->has_encrypted_entries = 0;
796	}
797
798	if (zip->pack_stream_bytes_unconsumed)
799		read_consume(a);
800
801	*offset = zip->entry_offset;
802	*size = 0;
803	*buff = NULL;
804	/*
805	 * If we hit end-of-entry last time, clean up and return
806	 * ARCHIVE_EOF this time.
807	 */
808	if (zip->end_of_entry)
809		return (ARCHIVE_EOF);
810
811	bytes = read_stream(a, buff,
812		(size_t)zip->entry_bytes_remaining, 0);
813	if (bytes < 0)
814		return ((int)bytes);
815	if (bytes == 0) {
816		archive_set_error(&a->archive,
817		    ARCHIVE_ERRNO_FILE_FORMAT,
818		    "Truncated 7-Zip file body");
819		return (ARCHIVE_FATAL);
820	}
821	zip->entry_bytes_remaining -= bytes;
822	if (zip->entry_bytes_remaining == 0)
823		zip->end_of_entry = 1;
824
825	/* Update checksum */
826	if ((zip->entry->flg & CRC32_IS_SET) && bytes)
827		zip->entry_crc32 = crc32(zip->entry_crc32, *buff,
828		    (unsigned)bytes);
829
830	/* If we hit the end, swallow any end-of-data marker. */
831	if (zip->end_of_entry) {
832		/* Check computed CRC against file contents. */
833		if ((zip->entry->flg & CRC32_IS_SET) &&
834			zip->si.ss.digests[zip->entry->ssIndex] !=
835		    zip->entry_crc32) {
836			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
837			    "7-Zip bad CRC: 0x%lx should be 0x%lx",
838			    (unsigned long)zip->entry_crc32,
839			    (unsigned long)zip->si.ss.digests[
840			    		zip->entry->ssIndex]);
841			ret = ARCHIVE_WARN;
842		}
843	}
844
845	*size = bytes;
846	*offset = zip->entry_offset;
847	zip->entry_offset += bytes;
848
849	return (ret);
850}
851
852static int
853archive_read_format_7zip_read_data_skip(struct archive_read *a)
854{
855	struct _7zip *zip;
856	int64_t bytes_skipped;
857
858	zip = (struct _7zip *)(a->format->data);
859
860	if (zip->pack_stream_bytes_unconsumed)
861		read_consume(a);
862
863	/* If we've already read to end of data, we're done. */
864	if (zip->end_of_entry)
865		return (ARCHIVE_OK);
866
867	/*
868	 * If the length is at the beginning, we can skip the
869	 * compressed data much more quickly.
870	 */
871	bytes_skipped = skip_stream(a, (size_t)zip->entry_bytes_remaining);
872	if (bytes_skipped < 0)
873		return (ARCHIVE_FATAL);
874	zip->entry_bytes_remaining = 0;
875
876	/* This entry is finished and done. */
877	zip->end_of_entry = 1;
878	return (ARCHIVE_OK);
879}
880
881static int
882archive_read_format_7zip_cleanup(struct archive_read *a)
883{
884	struct _7zip *zip;
885
886	zip = (struct _7zip *)(a->format->data);
887	free_StreamsInfo(&(zip->si));
888	free(zip->entries);
889	free(zip->entry_names);
890	free_decompression(a, zip);
891	free(zip->uncompressed_buffer);
892	free(zip->sub_stream_buff[0]);
893	free(zip->sub_stream_buff[1]);
894	free(zip->sub_stream_buff[2]);
895	free(zip->tmp_stream_buff);
896	free(zip);
897	(a->format->data) = NULL;
898	return (ARCHIVE_OK);
899}
900
901static void
902read_consume(struct archive_read *a)
903{
904	struct _7zip *zip = (struct _7zip *)a->format->data;
905
906	if (zip->pack_stream_bytes_unconsumed) {
907		__archive_read_consume(a, zip->pack_stream_bytes_unconsumed);
908		zip->stream_offset += zip->pack_stream_bytes_unconsumed;
909		zip->pack_stream_bytes_unconsumed = 0;
910	}
911}
912
913#ifdef HAVE_LZMA_H
914
915/*
916 * Set an error code and choose an error message for liblzma.
917 */
918static void
919set_error(struct archive_read *a, int ret)
920{
921
922	switch (ret) {
923	case LZMA_STREAM_END: /* Found end of stream. */
924	case LZMA_OK: /* Decompressor made some progress. */
925		break;
926	case LZMA_MEM_ERROR:
927		archive_set_error(&a->archive, ENOMEM,
928		    "Lzma library error: Cannot allocate memory");
929		break;
930	case LZMA_MEMLIMIT_ERROR:
931		archive_set_error(&a->archive, ENOMEM,
932		    "Lzma library error: Out of memory");
933		break;
934	case LZMA_FORMAT_ERROR:
935		archive_set_error(&a->archive,
936		    ARCHIVE_ERRNO_MISC,
937		    "Lzma library error: format not recognized");
938		break;
939	case LZMA_OPTIONS_ERROR:
940		archive_set_error(&a->archive,
941		    ARCHIVE_ERRNO_MISC,
942		    "Lzma library error: Invalid options");
943		break;
944	case LZMA_DATA_ERROR:
945		archive_set_error(&a->archive,
946		    ARCHIVE_ERRNO_MISC,
947		    "Lzma library error: Corrupted input data");
948		break;
949	case LZMA_BUF_ERROR:
950		archive_set_error(&a->archive,
951		    ARCHIVE_ERRNO_MISC,
952		    "Lzma library error:  No progress is possible");
953		break;
954	default:
955		/* Return an error. */
956		archive_set_error(&a->archive,
957		    ARCHIVE_ERRNO_MISC,
958		    "Lzma decompression failed:  Unknown error");
959		break;
960	}
961}
962
963#endif
964
965static unsigned long
966decode_codec_id(const unsigned char *codecId, size_t id_size)
967{
968	unsigned i;
969	unsigned long id = 0;
970
971	for (i = 0; i < id_size; i++) {
972		id <<= 8;
973		id += codecId[i];
974	}
975	return (id);
976}
977
978static void *
979ppmd_alloc(void *p, size_t size)
980{
981	(void)p;
982	return malloc(size);
983}
984static void
985ppmd_free(void *p, void *address)
986{
987	(void)p;
988	free(address);
989}
990static Byte
991ppmd_read(void *p)
992{
993	struct archive_read *a = ((IByteIn*)p)->a;
994	struct _7zip *zip = (struct _7zip *)(a->format->data);
995	Byte b;
996
997	if (zip->ppstream.avail_in == 0) {
998		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
999		    "Truncated RAR file data");
1000		zip->ppstream.overconsumed = 1;
1001		return (0);
1002	}
1003	b = *zip->ppstream.next_in++;
1004	zip->ppstream.avail_in--;
1005	zip->ppstream.total_in++;
1006	return (b);
1007}
1008
1009static ISzAlloc g_szalloc = { ppmd_alloc, ppmd_free };
1010
1011static int
1012init_decompression(struct archive_read *a, struct _7zip *zip,
1013    const struct _7z_coder *coder1, const struct _7z_coder *coder2)
1014{
1015	int r;
1016
1017	zip->codec = coder1->codec;
1018	zip->codec2 = -1;
1019
1020	switch (zip->codec) {
1021	case _7Z_COPY:
1022	case _7Z_BZ2:
1023	case _7Z_DEFLATE:
1024	case _7Z_PPMD:
1025		if (coder2 != NULL) {
1026			if (coder2->codec != _7Z_X86 &&
1027			    coder2->codec != _7Z_X86_BCJ2) {
1028				archive_set_error(&a->archive,
1029				    ARCHIVE_ERRNO_MISC,
1030				    "Unsupported filter %lx for %lx",
1031				    coder2->codec, coder1->codec);
1032				return (ARCHIVE_FAILED);
1033			}
1034			zip->codec2 = coder2->codec;
1035			zip->bcj_state = 0;
1036			if (coder2->codec == _7Z_X86)
1037				x86_Init(zip);
1038		}
1039		break;
1040	default:
1041		break;
1042	}
1043
1044	switch (zip->codec) {
1045	case _7Z_COPY:
1046		break;
1047
1048	case _7Z_LZMA: case _7Z_LZMA2:
1049#ifdef HAVE_LZMA_H
1050#if LZMA_VERSION_MAJOR >= 5
1051/* Effectively disable the limiter. */
1052#define LZMA_MEMLIMIT   UINT64_MAX
1053#else
1054/* NOTE: This needs to check memory size which running system has. */
1055#define LZMA_MEMLIMIT   (1U << 30)
1056#endif
1057	{
1058		lzma_options_delta delta_opt;
1059		lzma_filter filters[LZMA_FILTERS_MAX];
1060#if LZMA_VERSION < 50010000
1061		lzma_filter *ff;
1062#endif
1063		int fi = 0;
1064
1065		if (zip->lzstream_valid) {
1066			lzma_end(&(zip->lzstream));
1067			zip->lzstream_valid = 0;
1068		}
1069
1070		/*
1071		 * NOTE: liblzma incompletely handle the BCJ+LZMA compressed
1072		 * data made by 7-Zip because 7-Zip does not add End-Of-
1073		 * Payload Marker(EOPM) at the end of LZMA compressed data,
1074		 * and so liblzma cannot know the end of the compressed data
1075		 * without EOPM. So consequently liblzma will not return last
1076		 * three or four bytes of uncompressed data because
1077		 * LZMA_FILTER_X86 filter does not handle input data if its
1078		 * data size is less than five bytes. If liblzma detect EOPM
1079		 * or know the uncompressed data size, liblzma will flush out
1080		 * the remaining that three or four bytes of uncompressed
1081		 * data. That is why we have to use our converting program
1082		 * for BCJ+LZMA. If we were able to tell the uncompressed
1083		 * size to liblzma when using lzma_raw_decoder() liblzma
1084		 * could correctly deal with BCJ+LZMA. But unfortunately
1085		 * there is no way to do that.
1086		 * Discussion about this can be found at XZ Utils forum.
1087		 */
1088		if (coder2 != NULL) {
1089			zip->codec2 = coder2->codec;
1090
1091			filters[fi].options = NULL;
1092			switch (zip->codec2) {
1093			case _7Z_X86:
1094				if (zip->codec == _7Z_LZMA2) {
1095					filters[fi].id = LZMA_FILTER_X86;
1096					fi++;
1097				} else
1098					/* Use our filter. */
1099					x86_Init(zip);
1100				break;
1101			case _7Z_X86_BCJ2:
1102				/* Use our filter. */
1103				zip->bcj_state = 0;
1104				break;
1105			case _7Z_DELTA:
1106				filters[fi].id = LZMA_FILTER_DELTA;
1107				memset(&delta_opt, 0, sizeof(delta_opt));
1108				delta_opt.type = LZMA_DELTA_TYPE_BYTE;
1109				delta_opt.dist = 1;
1110				filters[fi].options = &delta_opt;
1111				fi++;
1112				break;
1113			/* Following filters have not been tested yet. */
1114			case _7Z_POWERPC:
1115				filters[fi].id = LZMA_FILTER_POWERPC;
1116				fi++;
1117				break;
1118			case _7Z_IA64:
1119				filters[fi].id = LZMA_FILTER_IA64;
1120				fi++;
1121				break;
1122			case _7Z_ARM:
1123				filters[fi].id = LZMA_FILTER_ARM;
1124				fi++;
1125				break;
1126			case _7Z_ARMTHUMB:
1127				filters[fi].id = LZMA_FILTER_ARMTHUMB;
1128				fi++;
1129				break;
1130			case _7Z_SPARC:
1131				filters[fi].id = LZMA_FILTER_SPARC;
1132				fi++;
1133				break;
1134			default:
1135				archive_set_error(&a->archive,
1136				    ARCHIVE_ERRNO_MISC,
1137				    "Unexpected codec ID: %lX", zip->codec2);
1138				return (ARCHIVE_FAILED);
1139			}
1140		}
1141
1142		if (zip->codec == _7Z_LZMA2)
1143			filters[fi].id = LZMA_FILTER_LZMA2;
1144		else
1145			filters[fi].id = LZMA_FILTER_LZMA1;
1146		filters[fi].options = NULL;
1147#if LZMA_VERSION < 50010000
1148		ff = &filters[fi];
1149#endif
1150		r = lzma_properties_decode(&filters[fi], NULL,
1151		    coder1->properties, (size_t)coder1->propertiesSize);
1152		if (r != LZMA_OK) {
1153			set_error(a, r);
1154			return (ARCHIVE_FAILED);
1155		}
1156		fi++;
1157
1158		filters[fi].id = LZMA_VLI_UNKNOWN;
1159		filters[fi].options = NULL;
1160		r = lzma_raw_decoder(&(zip->lzstream), filters);
1161#if LZMA_VERSION < 50010000
1162		free(ff->options);
1163#endif
1164		if (r != LZMA_OK) {
1165			set_error(a, r);
1166			return (ARCHIVE_FAILED);
1167		}
1168		zip->lzstream_valid = 1;
1169		zip->lzstream.total_in = 0;
1170		zip->lzstream.total_out = 0;
1171		break;
1172	}
1173#else
1174		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1175		    "LZMA codec is unsupported");
1176		return (ARCHIVE_FAILED);
1177#endif
1178	case _7Z_BZ2:
1179#if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1180		if (zip->bzstream_valid) {
1181			BZ2_bzDecompressEnd(&(zip->bzstream));
1182			zip->bzstream_valid = 0;
1183		}
1184		r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 0);
1185		if (r == BZ_MEM_ERROR)
1186			r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 1);
1187		if (r != BZ_OK) {
1188			int err = ARCHIVE_ERRNO_MISC;
1189			const char *detail = NULL;
1190			switch (r) {
1191			case BZ_PARAM_ERROR:
1192				detail = "invalid setup parameter";
1193				break;
1194			case BZ_MEM_ERROR:
1195				err = ENOMEM;
1196				detail = "out of memory";
1197				break;
1198			case BZ_CONFIG_ERROR:
1199				detail = "mis-compiled library";
1200				break;
1201			}
1202			archive_set_error(&a->archive, err,
1203			    "Internal error initializing decompressor: %s",
1204			    detail != NULL ? detail : "??");
1205			zip->bzstream_valid = 0;
1206			return (ARCHIVE_FAILED);
1207		}
1208		zip->bzstream_valid = 1;
1209		zip->bzstream.total_in_lo32 = 0;
1210		zip->bzstream.total_in_hi32 = 0;
1211		zip->bzstream.total_out_lo32 = 0;
1212		zip->bzstream.total_out_hi32 = 0;
1213		break;
1214#else
1215		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1216		    "BZ2 codec is unsupported");
1217		return (ARCHIVE_FAILED);
1218#endif
1219	case _7Z_DEFLATE:
1220#ifdef HAVE_ZLIB_H
1221		if (zip->stream_valid)
1222			r = inflateReset(&(zip->stream));
1223		else
1224			r = inflateInit2(&(zip->stream),
1225			    -15 /* Don't check for zlib header */);
1226		if (r != Z_OK) {
1227			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1228			    "Couldn't initialize zlib stream.");
1229			return (ARCHIVE_FAILED);
1230		}
1231		zip->stream_valid = 1;
1232		zip->stream.total_in = 0;
1233		zip->stream.total_out = 0;
1234		break;
1235#else
1236		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1237		    "DEFLATE codec is unsupported");
1238		return (ARCHIVE_FAILED);
1239#endif
1240	case _7Z_PPMD:
1241	{
1242		unsigned order;
1243		uint32_t msize;
1244
1245		if (zip->ppmd7_valid) {
1246			__archive_ppmd7_functions.Ppmd7_Free(
1247			    &zip->ppmd7_context, &g_szalloc);
1248			zip->ppmd7_valid = 0;
1249		}
1250
1251		if (coder1->propertiesSize < 5) {
1252			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1253			    "Malformed PPMd parameter");
1254			return (ARCHIVE_FAILED);
1255		}
1256		order = coder1->properties[0];
1257		msize = archive_le32dec(&(coder1->properties[1]));
1258		if (order < PPMD7_MIN_ORDER || order > PPMD7_MAX_ORDER ||
1259		    msize < PPMD7_MIN_MEM_SIZE || msize > PPMD7_MAX_MEM_SIZE) {
1260			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1261			    "Malformed PPMd parameter");
1262			return (ARCHIVE_FAILED);
1263		}
1264		__archive_ppmd7_functions.Ppmd7_Construct(&zip->ppmd7_context);
1265		r = __archive_ppmd7_functions.Ppmd7_Alloc(
1266			&zip->ppmd7_context, msize, &g_szalloc);
1267		if (r == 0) {
1268			archive_set_error(&a->archive, ENOMEM,
1269			    "Coludn't allocate memory for PPMd");
1270			return (ARCHIVE_FATAL);
1271		}
1272		__archive_ppmd7_functions.Ppmd7_Init(
1273			&zip->ppmd7_context, order);
1274		__archive_ppmd7_functions.Ppmd7z_RangeDec_CreateVTable(
1275			&zip->range_dec);
1276		zip->ppmd7_valid = 1;
1277		zip->ppmd7_stat = 0;
1278		zip->ppstream.overconsumed = 0;
1279		zip->ppstream.total_in = 0;
1280		zip->ppstream.total_out = 0;
1281		break;
1282	}
1283	case _7Z_X86:
1284	case _7Z_X86_BCJ2:
1285	case _7Z_POWERPC:
1286	case _7Z_IA64:
1287	case _7Z_ARM:
1288	case _7Z_ARMTHUMB:
1289	case _7Z_SPARC:
1290	case _7Z_DELTA:
1291		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1292		    "Unexpected codec ID: %lX", zip->codec);
1293		return (ARCHIVE_FAILED);
1294	case _7Z_CRYPTO_MAIN_ZIP:
1295	case _7Z_CRYPTO_RAR_29:
1296	case _7Z_CRYPTO_AES_256_SHA_256:
1297		if (a->entry) {
1298			archive_entry_set_is_metadata_encrypted(a->entry, 1);
1299			archive_entry_set_is_data_encrypted(a->entry, 1);
1300			zip->has_encrypted_entries = 1;
1301		}
1302		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1303		    "Crypto codec not supported yet (ID: 0x%lX)", zip->codec);
1304		return (ARCHIVE_FAILED);
1305	default:
1306		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1307		    "Unknown codec ID: %lX", zip->codec);
1308		return (ARCHIVE_FAILED);
1309	}
1310
1311	return (ARCHIVE_OK);
1312}
1313
1314static int
1315decompress(struct archive_read *a, struct _7zip *zip,
1316    void *buff, size_t *outbytes, const void *b, size_t *used)
1317{
1318	const uint8_t *t_next_in;
1319	uint8_t *t_next_out;
1320	size_t o_avail_in, o_avail_out;
1321	size_t t_avail_in, t_avail_out;
1322	uint8_t *bcj2_next_out;
1323	size_t bcj2_avail_out;
1324	int r, ret = ARCHIVE_OK;
1325
1326	t_avail_in = o_avail_in = *used;
1327	t_avail_out = o_avail_out = *outbytes;
1328	t_next_in = b;
1329	t_next_out = buff;
1330
1331	if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
1332		int i;
1333
1334		/* Do not copy out the BCJ remaining bytes when the output
1335		 * buffer size is less than five bytes. */
1336		if (o_avail_in != 0 && t_avail_out < 5 && zip->odd_bcj_size) {
1337			*used = 0;
1338			*outbytes = 0;
1339			return (ret);
1340		}
1341		for (i = 0; zip->odd_bcj_size > 0 && t_avail_out; i++) {
1342			*t_next_out++ = zip->odd_bcj[i];
1343			t_avail_out--;
1344			zip->odd_bcj_size--;
1345		}
1346		if (o_avail_in == 0 || t_avail_out == 0) {
1347			*used = o_avail_in - t_avail_in;
1348			*outbytes = o_avail_out - t_avail_out;
1349			if (o_avail_in == 0)
1350				ret = ARCHIVE_EOF;
1351			return (ret);
1352		}
1353	}
1354
1355	bcj2_next_out = t_next_out;
1356	bcj2_avail_out = t_avail_out;
1357	if (zip->codec2 == _7Z_X86_BCJ2) {
1358		/*
1359		 * Decord a remaining decompressed main stream for BCJ2.
1360		 */
1361		if (zip->tmp_stream_bytes_remaining) {
1362			ssize_t bytes;
1363			size_t remaining = zip->tmp_stream_bytes_remaining;
1364			bytes = Bcj2_Decode(zip, t_next_out, t_avail_out);
1365			if (bytes < 0) {
1366				archive_set_error(&(a->archive),
1367				    ARCHIVE_ERRNO_MISC,
1368				    "BCJ2 conversion Failed");
1369				return (ARCHIVE_FAILED);
1370			}
1371			zip->main_stream_bytes_remaining -=
1372			    remaining - zip->tmp_stream_bytes_remaining;
1373			t_avail_out -= bytes;
1374			if (o_avail_in == 0 || t_avail_out == 0) {
1375				*used = 0;
1376				*outbytes = o_avail_out - t_avail_out;
1377				if (o_avail_in == 0 &&
1378				    zip->tmp_stream_bytes_remaining)
1379					ret = ARCHIVE_EOF;
1380				return (ret);
1381			}
1382			t_next_out += bytes;
1383			bcj2_next_out = t_next_out;
1384			bcj2_avail_out = t_avail_out;
1385		}
1386		t_next_out = zip->tmp_stream_buff;
1387		t_avail_out = zip->tmp_stream_buff_size;
1388	}
1389
1390	switch (zip->codec) {
1391	case _7Z_COPY:
1392	{
1393		size_t bytes =
1394		    (t_avail_in > t_avail_out)?t_avail_out:t_avail_in;
1395
1396		memcpy(t_next_out, t_next_in, bytes);
1397		t_avail_in -= bytes;
1398		t_avail_out -= bytes;
1399		if (o_avail_in == 0)
1400			ret = ARCHIVE_EOF;
1401		break;
1402	}
1403#ifdef HAVE_LZMA_H
1404	case _7Z_LZMA: case _7Z_LZMA2:
1405		zip->lzstream.next_in = t_next_in;
1406		zip->lzstream.avail_in = t_avail_in;
1407		zip->lzstream.next_out = t_next_out;
1408		zip->lzstream.avail_out = t_avail_out;
1409
1410		r = lzma_code(&(zip->lzstream), LZMA_RUN);
1411		switch (r) {
1412		case LZMA_STREAM_END: /* Found end of stream. */
1413			lzma_end(&(zip->lzstream));
1414			zip->lzstream_valid = 0;
1415			ret = ARCHIVE_EOF;
1416			break;
1417		case LZMA_OK: /* Decompressor made some progress. */
1418			break;
1419		default:
1420			archive_set_error(&(a->archive),
1421			    ARCHIVE_ERRNO_MISC,
1422				"Decompression failed(%d)",
1423			    r);
1424			return (ARCHIVE_FAILED);
1425		}
1426		t_avail_in = zip->lzstream.avail_in;
1427		t_avail_out = zip->lzstream.avail_out;
1428		break;
1429#endif
1430#if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1431	case _7Z_BZ2:
1432		zip->bzstream.next_in = (char *)(uintptr_t)t_next_in;
1433		zip->bzstream.avail_in = t_avail_in;
1434		zip->bzstream.next_out = (char *)(uintptr_t)t_next_out;
1435		zip->bzstream.avail_out = t_avail_out;
1436		r = BZ2_bzDecompress(&(zip->bzstream));
1437		switch (r) {
1438		case BZ_STREAM_END: /* Found end of stream. */
1439			switch (BZ2_bzDecompressEnd(&(zip->bzstream))) {
1440			case BZ_OK:
1441				break;
1442			default:
1443				archive_set_error(&(a->archive),
1444				    ARCHIVE_ERRNO_MISC,
1445				    "Failed to clean up decompressor");
1446				return (ARCHIVE_FAILED);
1447			}
1448			zip->bzstream_valid = 0;
1449			ret = ARCHIVE_EOF;
1450			break;
1451		case BZ_OK: /* Decompressor made some progress. */
1452			break;
1453		default:
1454			archive_set_error(&(a->archive),
1455			    ARCHIVE_ERRNO_MISC,
1456			    "bzip decompression failed");
1457			return (ARCHIVE_FAILED);
1458		}
1459		t_avail_in = zip->bzstream.avail_in;
1460		t_avail_out = zip->bzstream.avail_out;
1461		break;
1462#endif
1463#ifdef HAVE_ZLIB_H
1464	case _7Z_DEFLATE:
1465		zip->stream.next_in = (Bytef *)(uintptr_t)t_next_in;
1466		zip->stream.avail_in = (uInt)t_avail_in;
1467		zip->stream.next_out = t_next_out;
1468		zip->stream.avail_out = (uInt)t_avail_out;
1469		r = inflate(&(zip->stream), 0);
1470		switch (r) {
1471		case Z_STREAM_END: /* Found end of stream. */
1472			ret = ARCHIVE_EOF;
1473			break;
1474		case Z_OK: /* Decompressor made some progress.*/
1475			break;
1476		default:
1477			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1478			    "File decompression failed (%d)", r);
1479			return (ARCHIVE_FAILED);
1480		}
1481		t_avail_in = zip->stream.avail_in;
1482		t_avail_out = zip->stream.avail_out;
1483		break;
1484#endif
1485	case _7Z_PPMD:
1486	{
1487		uint64_t flush_bytes;
1488
1489		if (!zip->ppmd7_valid || zip->ppmd7_stat < 0 ||
1490		    t_avail_out <= 0) {
1491			archive_set_error(&(a->archive),
1492			    ARCHIVE_ERRNO_MISC,
1493			    "Decompression internal error");
1494			return (ARCHIVE_FAILED);
1495		}
1496		zip->ppstream.next_in = t_next_in;
1497		zip->ppstream.avail_in = t_avail_in;
1498		zip->ppstream.next_out = t_next_out;
1499		zip->ppstream.avail_out = t_avail_out;
1500		if (zip->ppmd7_stat == 0) {
1501			zip->bytein.a = a;
1502			zip->bytein.Read = &ppmd_read;
1503			zip->range_dec.Stream = &zip->bytein;
1504			r = __archive_ppmd7_functions.Ppmd7z_RangeDec_Init(
1505				&(zip->range_dec));
1506			if (r == 0) {
1507				zip->ppmd7_stat = -1;
1508				archive_set_error(&a->archive,
1509				    ARCHIVE_ERRNO_MISC,
1510				    "Failed to initialize PPMd range decorder");
1511				return (ARCHIVE_FAILED);
1512			}
1513			if (zip->ppstream.overconsumed) {
1514				zip->ppmd7_stat = -1;
1515				return (ARCHIVE_FAILED);
1516			}
1517			zip->ppmd7_stat = 1;
1518		}
1519
1520		if (t_avail_in == 0)
1521			/* XXX Flush out remaining decoded data XXX */
1522			flush_bytes = zip->folder_outbytes_remaining;
1523		else
1524			flush_bytes = 0;
1525
1526		do {
1527			int sym;
1528
1529			sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1530				&(zip->ppmd7_context), &(zip->range_dec.p));
1531			if (sym < 0) {
1532				zip->ppmd7_stat = -1;
1533				archive_set_error(&a->archive,
1534				    ARCHIVE_ERRNO_FILE_FORMAT,
1535				    "Failed to decode PPMd");
1536				return (ARCHIVE_FAILED);
1537			}
1538			if (zip->ppstream.overconsumed) {
1539				zip->ppmd7_stat = -1;
1540				return (ARCHIVE_FAILED);
1541			}
1542			*zip->ppstream.next_out++ = (unsigned char)sym;
1543			zip->ppstream.avail_out--;
1544			zip->ppstream.total_out++;
1545			if (flush_bytes)
1546				flush_bytes--;
1547		} while (zip->ppstream.avail_out &&
1548			(zip->ppstream.avail_in || flush_bytes));
1549
1550		t_avail_in = (size_t)zip->ppstream.avail_in;
1551		t_avail_out = (size_t)zip->ppstream.avail_out;
1552		break;
1553	}
1554	default:
1555		archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1556		    "Decompression internal error");
1557		return (ARCHIVE_FAILED);
1558	}
1559	if (ret != ARCHIVE_OK && ret != ARCHIVE_EOF)
1560		return (ret);
1561
1562	*used = o_avail_in - t_avail_in;
1563	*outbytes = o_avail_out - t_avail_out;
1564
1565	/*
1566	 * Decord BCJ.
1567	 */
1568	if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
1569		size_t l = x86_Convert(zip, buff, *outbytes);
1570		zip->odd_bcj_size = *outbytes - l;
1571		if (zip->odd_bcj_size > 0 && zip->odd_bcj_size <= 4 &&
1572		    o_avail_in && ret != ARCHIVE_EOF) {
1573			memcpy(zip->odd_bcj, ((unsigned char *)buff) + l,
1574			    zip->odd_bcj_size);
1575			*outbytes = l;
1576		} else
1577			zip->odd_bcj_size = 0;
1578	}
1579
1580	/*
1581	 * Decord BCJ2 with a decompressed main stream.
1582	 */
1583	if (zip->codec2 == _7Z_X86_BCJ2) {
1584		ssize_t bytes;
1585
1586		zip->tmp_stream_bytes_avail =
1587		    zip->tmp_stream_buff_size - t_avail_out;
1588		if (zip->tmp_stream_bytes_avail >
1589		      zip->main_stream_bytes_remaining)
1590			zip->tmp_stream_bytes_avail =
1591			    zip->main_stream_bytes_remaining;
1592		zip->tmp_stream_bytes_remaining = zip->tmp_stream_bytes_avail;
1593		bytes = Bcj2_Decode(zip, bcj2_next_out, bcj2_avail_out);
1594		if (bytes < 0) {
1595			archive_set_error(&(a->archive),
1596			    ARCHIVE_ERRNO_MISC, "BCJ2 conversion Failed");
1597			return (ARCHIVE_FAILED);
1598		}
1599		zip->main_stream_bytes_remaining -=
1600		    zip->tmp_stream_bytes_avail
1601		      - zip->tmp_stream_bytes_remaining;
1602		bcj2_avail_out -= bytes;
1603		*outbytes = o_avail_out - bcj2_avail_out;
1604	}
1605
1606	return (ret);
1607}
1608
1609static int
1610free_decompression(struct archive_read *a, struct _7zip *zip)
1611{
1612	int r = ARCHIVE_OK;
1613
1614#if !defined(HAVE_ZLIB_H) &&\
1615	!(defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR))
1616	(void)a;/* UNUSED */
1617#endif
1618#ifdef HAVE_LZMA_H
1619	if (zip->lzstream_valid)
1620		lzma_end(&(zip->lzstream));
1621#endif
1622#if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1623	if (zip->bzstream_valid) {
1624		if (BZ2_bzDecompressEnd(&(zip->bzstream)) != BZ_OK) {
1625			archive_set_error(&a->archive,
1626			    ARCHIVE_ERRNO_MISC,
1627			    "Failed to clean up bzip2 decompressor");
1628			r = ARCHIVE_FATAL;
1629		}
1630		zip->bzstream_valid = 0;
1631	}
1632#endif
1633#ifdef HAVE_ZLIB_H
1634	if (zip->stream_valid) {
1635		if (inflateEnd(&(zip->stream)) != Z_OK) {
1636			archive_set_error(&a->archive,
1637			    ARCHIVE_ERRNO_MISC,
1638			    "Failed to clean up zlib decompressor");
1639			r = ARCHIVE_FATAL;
1640		}
1641		zip->stream_valid = 0;
1642	}
1643#endif
1644	if (zip->ppmd7_valid) {
1645		__archive_ppmd7_functions.Ppmd7_Free(
1646			&zip->ppmd7_context, &g_szalloc);
1647		zip->ppmd7_valid = 0;
1648	}
1649	return (r);
1650}
1651
1652static int
1653parse_7zip_uint64(struct archive_read *a, uint64_t *val)
1654{
1655	const unsigned char *p;
1656	unsigned char avail, mask;
1657	int i;
1658
1659	if ((p = header_bytes(a, 1)) == NULL)
1660		return (-1);
1661	avail = *p;
1662	mask = 0x80;
1663	*val = 0;
1664	for (i = 0; i < 8; i++) {
1665		if (avail & mask) {
1666			if ((p = header_bytes(a, 1)) == NULL)
1667				return (-1);
1668			*val |= ((uint64_t)*p) << (8 * i);
1669			mask >>= 1;
1670			continue;
1671		}
1672		*val += ((uint64_t)(avail & (mask -1))) << (8 * i);
1673		break;
1674	}
1675	return (0);
1676}
1677
1678static int
1679read_Bools(struct archive_read *a, unsigned char *data, size_t num)
1680{
1681	const unsigned char *p;
1682	unsigned i, mask = 0, avail = 0;
1683
1684	for (i = 0; i < num; i++) {
1685		if (mask == 0) {
1686			if ((p = header_bytes(a, 1)) == NULL)
1687				return (-1);
1688			avail = *p;
1689			mask = 0x80;
1690		}
1691		data[i] = (avail & mask)?1:0;
1692		mask >>= 1;
1693	}
1694	return (0);
1695}
1696
1697static void
1698free_Digest(struct _7z_digests *d)
1699{
1700	free(d->defineds);
1701	free(d->digests);
1702}
1703
1704static int
1705read_Digests(struct archive_read *a, struct _7z_digests *d, size_t num)
1706{
1707	const unsigned char *p;
1708	unsigned i;
1709
1710	if (num == 0)
1711		return (-1);
1712	memset(d, 0, sizeof(*d));
1713
1714	d->defineds = malloc(num);
1715	if (d->defineds == NULL)
1716		return (-1);
1717	/*
1718	 * Read Bools.
1719	 */
1720	if ((p = header_bytes(a, 1)) == NULL)
1721		return (-1);
1722	if (*p == 0) {
1723		if (read_Bools(a, d->defineds, num) < 0)
1724			return (-1);
1725	} else
1726		/* All are defined */
1727		memset(d->defineds, 1, num);
1728
1729	d->digests = calloc(num, sizeof(*d->digests));
1730	if (d->digests == NULL)
1731		return (-1);
1732	for (i = 0; i < num; i++) {
1733		if (d->defineds[i]) {
1734			if ((p = header_bytes(a, 4)) == NULL)
1735				return (-1);
1736			d->digests[i] = archive_le32dec(p);
1737		}
1738	}
1739
1740	return (0);
1741}
1742
1743static void
1744free_PackInfo(struct _7z_pack_info *pi)
1745{
1746	free(pi->sizes);
1747	free(pi->positions);
1748	free_Digest(&(pi->digest));
1749}
1750
1751static int
1752read_PackInfo(struct archive_read *a, struct _7z_pack_info *pi)
1753{
1754	const unsigned char *p;
1755	unsigned i;
1756
1757	memset(pi, 0, sizeof(*pi));
1758
1759	/*
1760	 * Read PackPos.
1761	 */
1762	if (parse_7zip_uint64(a, &(pi->pos)) < 0)
1763		return (-1);
1764
1765	/*
1766	 * Read NumPackStreams.
1767	 */
1768	if (parse_7zip_uint64(a, &(pi->numPackStreams)) < 0)
1769		return (-1);
1770	if (pi->numPackStreams == 0)
1771		return (-1);
1772	if (UMAX_ENTRY < pi->numPackStreams)
1773		return (-1);
1774
1775	/*
1776	 * Read PackSizes[num]
1777	 */
1778	if ((p = header_bytes(a, 1)) == NULL)
1779		return (-1);
1780	if (*p == kEnd)
1781		/* PackSizes[num] are not present. */
1782		return (0);
1783	if (*p != kSize)
1784		return (-1);
1785	pi->sizes = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
1786	pi->positions = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
1787	if (pi->sizes == NULL || pi->positions == NULL)
1788		return (-1);
1789
1790	for (i = 0; i < pi->numPackStreams; i++) {
1791		if (parse_7zip_uint64(a, &(pi->sizes[i])) < 0)
1792			return (-1);
1793	}
1794
1795	/*
1796	 * Read PackStreamDigests[num]
1797	 */
1798	if ((p = header_bytes(a, 1)) == NULL)
1799		return (-1);
1800	if (*p == kEnd) {
1801		/* PackStreamDigests[num] are not present. */
1802		pi->digest.defineds =
1803		    calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.defineds));
1804		pi->digest.digests =
1805		    calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.digests));
1806		if (pi->digest.defineds == NULL || pi->digest.digests == NULL)
1807			return (-1);
1808		return (0);
1809	}
1810
1811	if (*p != kSize)
1812		return (-1);
1813
1814	if (read_Digests(a, &(pi->digest), (size_t)pi->numPackStreams) < 0)
1815		return (-1);
1816
1817	/*
1818	 *  Must be marked by kEnd.
1819	 */
1820	if ((p = header_bytes(a, 1)) == NULL)
1821		return (-1);
1822	if (*p != kEnd)
1823		return (-1);
1824	return (0);
1825}
1826
1827static void
1828free_Folder(struct _7z_folder *f)
1829{
1830	unsigned i;
1831
1832	if (f->coders) {
1833		for (i = 0; i< f->numCoders; i++) {
1834			free(f->coders[i].properties);
1835		}
1836		free(f->coders);
1837	}
1838	free(f->bindPairs);
1839	free(f->packedStreams);
1840	free(f->unPackSize);
1841}
1842
1843static int
1844read_Folder(struct archive_read *a, struct _7z_folder *f)
1845{
1846	struct _7zip *zip = (struct _7zip *)a->format->data;
1847	const unsigned char *p;
1848	uint64_t numInStreamsTotal = 0;
1849	uint64_t numOutStreamsTotal = 0;
1850	unsigned i;
1851
1852	memset(f, 0, sizeof(*f));
1853
1854	/*
1855	 * Read NumCoders.
1856	 */
1857	if (parse_7zip_uint64(a, &(f->numCoders)) < 0)
1858		return (-1);
1859	if (f->numCoders > 4)
1860		/* Too many coders. */
1861		return (-1);
1862
1863	f->coders = calloc((size_t)f->numCoders, sizeof(*f->coders));
1864	if (f->coders == NULL)
1865		return (-1);
1866	for (i = 0; i< f->numCoders; i++) {
1867		size_t codec_size;
1868		int simple, attr;
1869
1870		if ((p = header_bytes(a, 1)) == NULL)
1871			return (-1);
1872		/*
1873		 * 0:3 CodecIdSize
1874		 * 4:  0 - IsSimple
1875		 *     1 - Is not Simple
1876		 * 5:  0 - No Attributes
1877		 *     1 - There are Attributes;
1878		 * 7:  Must be zero.
1879		 */
1880		codec_size = *p & 0xf;
1881		simple = (*p & 0x10)?0:1;
1882		attr = *p & 0x20;
1883		if (*p & 0x80)
1884			return (-1);/* Not supported. */
1885
1886		/*
1887		 * Read Decompression Method IDs.
1888		 */
1889		if ((p = header_bytes(a, codec_size)) == NULL)
1890			return (-1);
1891
1892		f->coders[i].codec = decode_codec_id(p, codec_size);
1893
1894		if (simple) {
1895			f->coders[i].numInStreams = 1;
1896			f->coders[i].numOutStreams = 1;
1897		} else {
1898			if (parse_7zip_uint64(
1899			    a, &(f->coders[i].numInStreams)) < 0)
1900				return (-1);
1901			if (UMAX_ENTRY < f->coders[i].numInStreams)
1902				return (-1);
1903			if (parse_7zip_uint64(
1904			    a, &(f->coders[i].numOutStreams)) < 0)
1905				return (-1);
1906			if (UMAX_ENTRY < f->coders[i].numOutStreams)
1907				return (-1);
1908		}
1909
1910		if (attr) {
1911			if (parse_7zip_uint64(
1912			    a, &(f->coders[i].propertiesSize)) < 0)
1913				return (-1);
1914			if ((p = header_bytes(
1915			    a, (size_t)f->coders[i].propertiesSize)) == NULL)
1916				return (-1);
1917			f->coders[i].properties =
1918			    malloc((size_t)f->coders[i].propertiesSize);
1919			if (f->coders[i].properties == NULL)
1920				return (-1);
1921			memcpy(f->coders[i].properties, p,
1922			    (size_t)f->coders[i].propertiesSize);
1923		}
1924
1925		numInStreamsTotal += f->coders[i].numInStreams;
1926		numOutStreamsTotal += f->coders[i].numOutStreams;
1927	}
1928
1929	if (numOutStreamsTotal == 0 ||
1930	    numInStreamsTotal < numOutStreamsTotal-1)
1931		return (-1);
1932
1933	f->numBindPairs = numOutStreamsTotal - 1;
1934	if (zip->header_bytes_remaining < f->numBindPairs)
1935			return (-1);
1936	if (f->numBindPairs > 0) {
1937		f->bindPairs =
1938			calloc((size_t)f->numBindPairs, sizeof(*f->bindPairs));
1939		if (f->bindPairs == NULL)
1940			return (-1);
1941	} else
1942		f->bindPairs = NULL;
1943	for (i = 0; i < f->numBindPairs; i++) {
1944		if (parse_7zip_uint64(a, &(f->bindPairs[i].inIndex)) < 0)
1945			return (-1);
1946		if (UMAX_ENTRY < f->bindPairs[i].inIndex)
1947			return (-1);
1948		if (parse_7zip_uint64(a, &(f->bindPairs[i].outIndex)) < 0)
1949			return (-1);
1950		if (UMAX_ENTRY < f->bindPairs[i].outIndex)
1951			return (-1);
1952	}
1953
1954	f->numPackedStreams = numInStreamsTotal - f->numBindPairs;
1955	f->packedStreams =
1956	    calloc((size_t)f->numPackedStreams, sizeof(*f->packedStreams));
1957	if (f->packedStreams == NULL)
1958		return (-1);
1959	if (f->numPackedStreams == 1) {
1960		for (i = 0; i < numInStreamsTotal; i++) {
1961			unsigned j;
1962			for (j = 0; j < f->numBindPairs; j++) {
1963				if (f->bindPairs[j].inIndex == i)
1964					break;
1965			}
1966			if (j == f->numBindPairs)
1967				break;
1968		}
1969		if (i == numInStreamsTotal)
1970			return (-1);
1971		f->packedStreams[0] = i;
1972	} else {
1973		for (i = 0; i < f->numPackedStreams; i++) {
1974			if (parse_7zip_uint64(a, &(f->packedStreams[i])) < 0)
1975				return (-1);
1976			if (UMAX_ENTRY < f->packedStreams[i])
1977				return (-1);
1978		}
1979	}
1980	f->numInStreams = numInStreamsTotal;
1981	f->numOutStreams = numOutStreamsTotal;
1982
1983	return (0);
1984}
1985
1986static void
1987free_CodersInfo(struct _7z_coders_info *ci)
1988{
1989	unsigned i;
1990
1991	if (ci->folders) {
1992		for (i = 0; i < ci->numFolders; i++)
1993			free_Folder(&(ci->folders[i]));
1994		free(ci->folders);
1995	}
1996}
1997
1998static int
1999read_CodersInfo(struct archive_read *a, struct _7z_coders_info *ci)
2000{
2001	const unsigned char *p;
2002	struct _7z_digests digest;
2003	unsigned i;
2004
2005	memset(ci, 0, sizeof(*ci));
2006	memset(&digest, 0, sizeof(digest));
2007
2008	if ((p = header_bytes(a, 1)) == NULL)
2009		goto failed;
2010	if (*p != kFolder)
2011		goto failed;
2012
2013	/*
2014	 * Read NumFolders.
2015	 */
2016	if (parse_7zip_uint64(a, &(ci->numFolders)) < 0)
2017		goto failed;
2018	if (UMAX_ENTRY < ci->numFolders)
2019		return (-1);
2020
2021	/*
2022	 * Read External.
2023	 */
2024	if ((p = header_bytes(a, 1)) == NULL)
2025		goto failed;
2026	switch (*p) {
2027	case 0:
2028		ci->folders =
2029			calloc((size_t)ci->numFolders, sizeof(*ci->folders));
2030		if (ci->folders == NULL)
2031			return (-1);
2032		for (i = 0; i < ci->numFolders; i++) {
2033			if (read_Folder(a, &(ci->folders[i])) < 0)
2034				goto failed;
2035		}
2036		break;
2037	case 1:
2038		if (parse_7zip_uint64(a, &(ci->dataStreamIndex)) < 0)
2039			return (-1);
2040		if (UMAX_ENTRY < ci->dataStreamIndex)
2041			return (-1);
2042		if (ci->numFolders > 0) {
2043			archive_set_error(&a->archive, -1,
2044			    "Malformed 7-Zip archive");
2045			goto failed;
2046		}
2047		break;
2048	default:
2049		archive_set_error(&a->archive, -1,
2050		    "Malformed 7-Zip archive");
2051		goto failed;
2052	}
2053
2054	if ((p = header_bytes(a, 1)) == NULL)
2055		goto failed;
2056	if (*p != kCodersUnPackSize)
2057		goto failed;
2058
2059	for (i = 0; i < ci->numFolders; i++) {
2060		struct _7z_folder *folder = &(ci->folders[i]);
2061		unsigned j;
2062
2063		folder->unPackSize =
2064		    calloc((size_t)folder->numOutStreams, sizeof(*folder->unPackSize));
2065		if (folder->unPackSize == NULL)
2066			goto failed;
2067		for (j = 0; j < folder->numOutStreams; j++) {
2068			if (parse_7zip_uint64(a, &(folder->unPackSize[j])) < 0)
2069				goto failed;
2070		}
2071	}
2072
2073	/*
2074	 * Read CRCs.
2075	 */
2076	if ((p = header_bytes(a, 1)) == NULL)
2077		goto failed;
2078	if (*p == kEnd)
2079		return (0);
2080	if (*p != kCRC)
2081		goto failed;
2082	if (read_Digests(a, &digest, (size_t)ci->numFolders) < 0)
2083		goto failed;
2084	for (i = 0; i < ci->numFolders; i++) {
2085		ci->folders[i].digest_defined = digest.defineds[i];
2086		ci->folders[i].digest = digest.digests[i];
2087	}
2088
2089	/*
2090	 *  Must be kEnd.
2091	 */
2092	if ((p = header_bytes(a, 1)) == NULL)
2093		goto failed;
2094	if (*p != kEnd)
2095		goto failed;
2096	free_Digest(&digest);
2097	return (0);
2098failed:
2099	free_Digest(&digest);
2100	return (-1);
2101}
2102
2103static uint64_t
2104folder_uncompressed_size(struct _7z_folder *f)
2105{
2106	int n = (int)f->numOutStreams;
2107	unsigned pairs = (unsigned)f->numBindPairs;
2108
2109	while (--n >= 0) {
2110		unsigned i;
2111		for (i = 0; i < pairs; i++) {
2112			if (f->bindPairs[i].outIndex == (uint64_t)n)
2113				break;
2114		}
2115		if (i >= pairs)
2116			return (f->unPackSize[n]);
2117	}
2118	return (0);
2119}
2120
2121static void
2122free_SubStreamsInfo(struct _7z_substream_info *ss)
2123{
2124	free(ss->unpackSizes);
2125	free(ss->digestsDefined);
2126	free(ss->digests);
2127}
2128
2129static int
2130read_SubStreamsInfo(struct archive_read *a, struct _7z_substream_info *ss,
2131    struct _7z_folder *f, size_t numFolders)
2132{
2133	const unsigned char *p;
2134	uint64_t *usizes;
2135	size_t unpack_streams;
2136	int type;
2137	unsigned i;
2138	uint32_t numDigests;
2139
2140	memset(ss, 0, sizeof(*ss));
2141
2142	for (i = 0; i < numFolders; i++)
2143		f[i].numUnpackStreams = 1;
2144
2145	if ((p = header_bytes(a, 1)) == NULL)
2146		return (-1);
2147	type = *p;
2148
2149	if (type == kNumUnPackStream) {
2150		unpack_streams = 0;
2151		for (i = 0; i < numFolders; i++) {
2152			if (parse_7zip_uint64(a, &(f[i].numUnpackStreams)) < 0)
2153				return (-1);
2154			if (UMAX_ENTRY < f[i].numUnpackStreams)
2155				return (-1);
2156			unpack_streams += (size_t)f[i].numUnpackStreams;
2157		}
2158		if ((p = header_bytes(a, 1)) == NULL)
2159			return (-1);
2160		type = *p;
2161	} else
2162		unpack_streams = numFolders;
2163
2164	ss->unpack_streams = unpack_streams;
2165	if (unpack_streams) {
2166		ss->unpackSizes = calloc(unpack_streams,
2167		    sizeof(*ss->unpackSizes));
2168		ss->digestsDefined = calloc(unpack_streams,
2169		    sizeof(*ss->digestsDefined));
2170		ss->digests = calloc(unpack_streams,
2171		    sizeof(*ss->digests));
2172		if (ss->unpackSizes == NULL || ss->digestsDefined == NULL ||
2173		    ss->digests == NULL)
2174			return (-1);
2175	}
2176
2177	usizes = ss->unpackSizes;
2178	for (i = 0; i < numFolders; i++) {
2179		unsigned pack;
2180		uint64_t sum;
2181
2182		if (f[i].numUnpackStreams == 0)
2183			continue;
2184
2185		sum = 0;
2186		if (type == kSize) {
2187			for (pack = 1; pack < f[i].numUnpackStreams; pack++) {
2188				if (parse_7zip_uint64(a, usizes) < 0)
2189					return (-1);
2190				sum += *usizes++;
2191			}
2192		}
2193		*usizes++ = folder_uncompressed_size(&f[i]) - sum;
2194	}
2195
2196	if (type == kSize) {
2197		if ((p = header_bytes(a, 1)) == NULL)
2198			return (-1);
2199		type = *p;
2200	}
2201
2202	for (i = 0; i < unpack_streams; i++) {
2203		ss->digestsDefined[i] = 0;
2204		ss->digests[i] = 0;
2205	}
2206
2207	numDigests = 0;
2208	for (i = 0; i < numFolders; i++) {
2209		if (f[i].numUnpackStreams != 1 || !f[i].digest_defined)
2210			numDigests += (uint32_t)f[i].numUnpackStreams;
2211	}
2212
2213	if (type == kCRC) {
2214		struct _7z_digests tmpDigests;
2215		unsigned char *digestsDefined = ss->digestsDefined;
2216		uint32_t * digests = ss->digests;
2217		int di = 0;
2218
2219		memset(&tmpDigests, 0, sizeof(tmpDigests));
2220		if (read_Digests(a, &(tmpDigests), numDigests) < 0) {
2221			free_Digest(&tmpDigests);
2222			return (-1);
2223		}
2224		for (i = 0; i < numFolders; i++) {
2225			if (f[i].numUnpackStreams == 1 && f[i].digest_defined) {
2226				*digestsDefined++ = 1;
2227				*digests++ = f[i].digest;
2228			} else {
2229				unsigned j;
2230
2231				for (j = 0; j < f[i].numUnpackStreams;
2232				    j++, di++) {
2233					*digestsDefined++ =
2234					    tmpDigests.defineds[di];
2235					*digests++ =
2236					    tmpDigests.digests[di];
2237				}
2238			}
2239		}
2240		free_Digest(&tmpDigests);
2241		if ((p = header_bytes(a, 1)) == NULL)
2242			return (-1);
2243		type = *p;
2244	}
2245
2246	/*
2247	 *  Must be kEnd.
2248	 */
2249	if (type != kEnd)
2250		return (-1);
2251	return (0);
2252}
2253
2254static void
2255free_StreamsInfo(struct _7z_stream_info *si)
2256{
2257	free_PackInfo(&(si->pi));
2258	free_CodersInfo(&(si->ci));
2259	free_SubStreamsInfo(&(si->ss));
2260}
2261
2262static int
2263read_StreamsInfo(struct archive_read *a, struct _7z_stream_info *si)
2264{
2265	struct _7zip *zip = (struct _7zip *)a->format->data;
2266	const unsigned char *p;
2267	unsigned i;
2268
2269	memset(si, 0, sizeof(*si));
2270
2271	if ((p = header_bytes(a, 1)) == NULL)
2272		return (-1);
2273	if (*p == kPackInfo) {
2274		uint64_t packPos;
2275
2276		if (read_PackInfo(a, &(si->pi)) < 0)
2277			return (-1);
2278
2279		if (si->pi.positions == NULL || si->pi.sizes == NULL)
2280			return (-1);
2281		/*
2282		 * Calculate packed stream positions.
2283		 */
2284		packPos = si->pi.pos;
2285		for (i = 0; i < si->pi.numPackStreams; i++) {
2286			si->pi.positions[i] = packPos;
2287			packPos += si->pi.sizes[i];
2288			if (packPos > zip->header_offset)
2289				return (-1);
2290		}
2291		if ((p = header_bytes(a, 1)) == NULL)
2292			return (-1);
2293	}
2294	if (*p == kUnPackInfo) {
2295		uint32_t packIndex;
2296		struct _7z_folder *f;
2297
2298		if (read_CodersInfo(a, &(si->ci)) < 0)
2299			return (-1);
2300
2301		/*
2302		 * Calculate packed stream indexes.
2303		 */
2304		packIndex = 0;
2305		f = si->ci.folders;
2306		for (i = 0; i < si->ci.numFolders; i++) {
2307			f[i].packIndex = packIndex;
2308			packIndex += (uint32_t)f[i].numPackedStreams;
2309			if (packIndex > si->pi.numPackStreams)
2310				return (-1);
2311		}
2312		if ((p = header_bytes(a, 1)) == NULL)
2313			return (-1);
2314	}
2315
2316	if (*p == kSubStreamsInfo) {
2317		if (read_SubStreamsInfo(a, &(si->ss),
2318		    si->ci.folders, (size_t)si->ci.numFolders) < 0)
2319			return (-1);
2320		if ((p = header_bytes(a, 1)) == NULL)
2321			return (-1);
2322	}
2323
2324	/*
2325	 *  Must be kEnd.
2326	 */
2327	if (*p != kEnd)
2328		return (-1);
2329	return (0);
2330}
2331
2332static void
2333free_Header(struct _7z_header_info *h)
2334{
2335	free(h->emptyStreamBools);
2336	free(h->emptyFileBools);
2337	free(h->antiBools);
2338	free(h->attrBools);
2339}
2340
2341static int
2342read_Header(struct archive_read *a, struct _7z_header_info *h,
2343    int check_header_id)
2344{
2345	struct _7zip *zip = (struct _7zip *)a->format->data;
2346	const unsigned char *p;
2347	struct _7z_folder *folders;
2348	struct _7z_stream_info *si = &(zip->si);
2349	struct _7zip_entry *entries;
2350	uint32_t folderIndex, indexInFolder;
2351	unsigned i;
2352	int eindex, empty_streams, sindex;
2353
2354	if (check_header_id) {
2355		/*
2356		 * Read Header.
2357		 */
2358		if ((p = header_bytes(a, 1)) == NULL)
2359			return (-1);
2360		if (*p != kHeader)
2361			return (-1);
2362	}
2363
2364	/*
2365	 * Read ArchiveProperties.
2366	 */
2367	if ((p = header_bytes(a, 1)) == NULL)
2368		return (-1);
2369	if (*p == kArchiveProperties) {
2370		for (;;) {
2371			uint64_t size;
2372			if ((p = header_bytes(a, 1)) == NULL)
2373				return (-1);
2374			if (*p == 0)
2375				break;
2376			if (parse_7zip_uint64(a, &size) < 0)
2377				return (-1);
2378		}
2379		if ((p = header_bytes(a, 1)) == NULL)
2380			return (-1);
2381	}
2382
2383	/*
2384	 * Read MainStreamsInfo.
2385	 */
2386	if (*p == kMainStreamsInfo) {
2387		if (read_StreamsInfo(a, &(zip->si)) < 0)
2388			return (-1);
2389		if ((p = header_bytes(a, 1)) == NULL)
2390			return (-1);
2391	}
2392	if (*p == kEnd)
2393		return (0);
2394
2395	/*
2396	 * Read FilesInfo.
2397	 */
2398	if (*p != kFilesInfo)
2399		return (-1);
2400
2401	if (parse_7zip_uint64(a, &(zip->numFiles)) < 0)
2402		return (-1);
2403	if (UMAX_ENTRY < zip->numFiles)
2404		return (-1);
2405
2406	zip->entries = calloc((size_t)zip->numFiles, sizeof(*zip->entries));
2407	if (zip->entries == NULL)
2408		return (-1);
2409	entries = zip->entries;
2410
2411	empty_streams = 0;
2412	for (;;) {
2413		int type;
2414		uint64_t size;
2415		size_t ll;
2416
2417		if ((p = header_bytes(a, 1)) == NULL)
2418			return (-1);
2419		type = *p;
2420		if (type == kEnd)
2421			break;
2422
2423		if (parse_7zip_uint64(a, &size) < 0)
2424			return (-1);
2425		if (zip->header_bytes_remaining < size)
2426			return (-1);
2427		ll = (size_t)size;
2428
2429		switch (type) {
2430		case kEmptyStream:
2431			h->emptyStreamBools = calloc((size_t)zip->numFiles,
2432			    sizeof(*h->emptyStreamBools));
2433			if (h->emptyStreamBools == NULL)
2434				return (-1);
2435			if (read_Bools(
2436			    a, h->emptyStreamBools, (size_t)zip->numFiles) < 0)
2437				return (-1);
2438			empty_streams = 0;
2439			for (i = 0; i < zip->numFiles; i++) {
2440				if (h->emptyStreamBools[i])
2441					empty_streams++;
2442			}
2443			break;
2444		case kEmptyFile:
2445			if (empty_streams <= 0) {
2446				/* Unexcepted sequence. Skip this. */
2447				if (header_bytes(a, ll) == NULL)
2448					return (-1);
2449				break;
2450			}
2451			h->emptyFileBools = calloc(empty_streams,
2452			    sizeof(*h->emptyFileBools));
2453			if (h->emptyFileBools == NULL)
2454				return (-1);
2455			if (read_Bools(a, h->emptyFileBools, empty_streams) < 0)
2456				return (-1);
2457			break;
2458		case kAnti:
2459			if (empty_streams <= 0) {
2460				/* Unexcepted sequence. Skip this. */
2461				if (header_bytes(a, ll) == NULL)
2462					return (-1);
2463				break;
2464			}
2465			h->antiBools = calloc(empty_streams,
2466			    sizeof(*h->antiBools));
2467			if (h->antiBools == NULL)
2468				return (-1);
2469			if (read_Bools(a, h->antiBools, empty_streams) < 0)
2470				return (-1);
2471			break;
2472		case kCTime:
2473		case kATime:
2474		case kMTime:
2475			if (read_Times(a, h, type) < 0)
2476				return (-1);
2477			break;
2478		case kName:
2479		{
2480			unsigned char *np;
2481			size_t nl, nb;
2482
2483			/* Skip one byte. */
2484			if ((p = header_bytes(a, 1)) == NULL)
2485				return (-1);
2486			ll--;
2487
2488			if ((ll & 1) || ll < zip->numFiles * 4)
2489				return (-1);
2490
2491			zip->entry_names = malloc(ll);
2492			if (zip->entry_names == NULL)
2493				return (-1);
2494			np = zip->entry_names;
2495			nb = ll;
2496			/*
2497			 * Copy whole file names.
2498			 * NOTE: This loop prevents from expanding
2499			 * the uncompressed buffer in order not to
2500			 * use extra memory resource.
2501			 */
2502			while (nb) {
2503				size_t b;
2504				if (nb > UBUFF_SIZE)
2505					b = UBUFF_SIZE;
2506				else
2507					b = nb;
2508				if ((p = header_bytes(a, b)) == NULL)
2509					return (-1);
2510				memcpy(np, p, b);
2511				np += b;
2512				nb -= b;
2513			}
2514			np = zip->entry_names;
2515			nl = ll;
2516
2517			for (i = 0; i < zip->numFiles; i++) {
2518				entries[i].utf16name = np;
2519#if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2520				entries[i].wname = (wchar_t *)np;
2521#endif
2522
2523				/* Find a terminator. */
2524				while (nl >= 2 && (np[0] || np[1])) {
2525					np += 2;
2526					nl -= 2;
2527				}
2528				if (nl < 2)
2529					return (-1);/* Terminator not found */
2530				entries[i].name_len = np - entries[i].utf16name;
2531				np += 2;
2532				nl -= 2;
2533			}
2534			break;
2535		}
2536		case kAttributes:
2537		{
2538			int allAreDefined;
2539
2540			if ((p = header_bytes(a, 2)) == NULL)
2541				return (-1);
2542			allAreDefined = *p;
2543			h->attrBools = calloc((size_t)zip->numFiles,
2544			    sizeof(*h->attrBools));
2545			if (h->attrBools == NULL)
2546				return (-1);
2547			if (allAreDefined)
2548				memset(h->attrBools, 1, (size_t)zip->numFiles);
2549			else {
2550				if (read_Bools(a, h->attrBools,
2551				      (size_t)zip->numFiles) < 0)
2552					return (-1);
2553			}
2554			for (i = 0; i < zip->numFiles; i++) {
2555				if (h->attrBools[i]) {
2556					if ((p = header_bytes(a, 4)) == NULL)
2557						return (-1);
2558					entries[i].attr = archive_le32dec(p);
2559				}
2560			}
2561			break;
2562		}
2563		case kDummy:
2564			if (ll == 0)
2565				break;
2566		default:
2567			if (header_bytes(a, ll) == NULL)
2568				return (-1);
2569			break;
2570		}
2571	}
2572
2573	/*
2574	 * Set up entry's attributes.
2575	 */
2576	folders = si->ci.folders;
2577	eindex = sindex = 0;
2578	folderIndex = indexInFolder = 0;
2579	for (i = 0; i < zip->numFiles; i++) {
2580		if (h->emptyStreamBools == NULL || h->emptyStreamBools[i] == 0)
2581			entries[i].flg |= HAS_STREAM;
2582		/* The high 16 bits of attributes is a posix file mode. */
2583		entries[i].mode = entries[i].attr >> 16;
2584		if (entries[i].flg & HAS_STREAM) {
2585			if ((size_t)sindex >= si->ss.unpack_streams)
2586				return (-1);
2587			if (entries[i].mode == 0)
2588				entries[i].mode = AE_IFREG | 0666;
2589			if (si->ss.digestsDefined[sindex])
2590				entries[i].flg |= CRC32_IS_SET;
2591			entries[i].ssIndex = sindex;
2592			sindex++;
2593		} else {
2594			int dir;
2595			if (h->emptyFileBools == NULL)
2596				dir = 1;
2597			else {
2598				if (h->emptyFileBools[eindex])
2599					dir = 0;
2600				else
2601					dir = 1;
2602				eindex++;
2603			}
2604			if (entries[i].mode == 0) {
2605				if (dir)
2606					entries[i].mode = AE_IFDIR | 0777;
2607				else
2608					entries[i].mode = AE_IFREG | 0666;
2609			} else if (dir &&
2610			    (entries[i].mode & AE_IFMT) != AE_IFDIR) {
2611				entries[i].mode &= ~AE_IFMT;
2612				entries[i].mode |= AE_IFDIR;
2613			}
2614			if ((entries[i].mode & AE_IFMT) == AE_IFDIR &&
2615			    entries[i].name_len >= 2 &&
2616			    (entries[i].utf16name[entries[i].name_len-2] != '/' ||
2617			     entries[i].utf16name[entries[i].name_len-1] != 0)) {
2618				entries[i].utf16name[entries[i].name_len] = '/';
2619				entries[i].utf16name[entries[i].name_len+1] = 0;
2620				entries[i].name_len += 2;
2621			}
2622			entries[i].ssIndex = -1;
2623		}
2624		if (entries[i].attr & 0x01)
2625			entries[i].mode &= ~0222;/* Read only. */
2626
2627		if ((entries[i].flg & HAS_STREAM) == 0 && indexInFolder == 0) {
2628			/*
2629			 * The entry is an empty file or a directory file,
2630			 * those both have no contents.
2631			 */
2632			entries[i].folderIndex = -1;
2633			continue;
2634		}
2635		if (indexInFolder == 0) {
2636			for (;;) {
2637				if (folderIndex >= si->ci.numFolders)
2638					return (-1);
2639				if (folders[folderIndex].numUnpackStreams)
2640					break;
2641				folderIndex++;
2642			}
2643		}
2644		entries[i].folderIndex = folderIndex;
2645		if ((entries[i].flg & HAS_STREAM) == 0)
2646			continue;
2647		indexInFolder++;
2648		if (indexInFolder >= folders[folderIndex].numUnpackStreams) {
2649			folderIndex++;
2650			indexInFolder = 0;
2651		}
2652	}
2653
2654	return (0);
2655}
2656
2657#define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
2658static void
2659fileTimeToUtc(uint64_t fileTime, time_t *timep, long *ns)
2660{
2661
2662	if (fileTime >= EPOC_TIME) {
2663		fileTime -= EPOC_TIME;
2664		/* milli seconds base */
2665		*timep = (time_t)(fileTime / 10000000);
2666		/* nano seconds base */
2667		*ns = (long)(fileTime % 10000000) * 100;
2668	} else {
2669		*timep = 0;
2670		*ns = 0;
2671	}
2672}
2673
2674static int
2675read_Times(struct archive_read *a, struct _7z_header_info *h, int type)
2676{
2677	struct _7zip *zip = (struct _7zip *)a->format->data;
2678	const unsigned char *p;
2679	struct _7zip_entry *entries = zip->entries;
2680	unsigned char *timeBools;
2681	int allAreDefined;
2682	unsigned i;
2683
2684	timeBools = calloc((size_t)zip->numFiles, sizeof(*timeBools));
2685	if (timeBools == NULL)
2686		return (-1);
2687
2688	/* Read allAreDefined. */
2689	if ((p = header_bytes(a, 1)) == NULL)
2690		goto failed;
2691	allAreDefined = *p;
2692	if (allAreDefined)
2693		memset(timeBools, 1, (size_t)zip->numFiles);
2694	else {
2695		if (read_Bools(a, timeBools, (size_t)zip->numFiles) < 0)
2696			goto failed;
2697	}
2698
2699	/* Read external. */
2700	if ((p = header_bytes(a, 1)) == NULL)
2701		goto failed;
2702	if (*p) {
2703		if (parse_7zip_uint64(a, &(h->dataIndex)) < 0)
2704			goto failed;
2705		if (UMAX_ENTRY < h->dataIndex)
2706			goto failed;
2707	}
2708
2709	for (i = 0; i < zip->numFiles; i++) {
2710		if (!timeBools[i])
2711			continue;
2712		if ((p = header_bytes(a, 8)) == NULL)
2713			goto failed;
2714		switch (type) {
2715		case kCTime:
2716			fileTimeToUtc(archive_le64dec(p),
2717			    &(entries[i].ctime),
2718			    &(entries[i].ctime_ns));
2719			entries[i].flg |= CTIME_IS_SET;
2720			break;
2721		case kATime:
2722			fileTimeToUtc(archive_le64dec(p),
2723			    &(entries[i].atime),
2724			    &(entries[i].atime_ns));
2725			entries[i].flg |= ATIME_IS_SET;
2726			break;
2727		case kMTime:
2728			fileTimeToUtc(archive_le64dec(p),
2729			    &(entries[i].mtime),
2730			    &(entries[i].mtime_ns));
2731			entries[i].flg |= MTIME_IS_SET;
2732			break;
2733		}
2734	}
2735
2736	free(timeBools);
2737	return (0);
2738failed:
2739	free(timeBools);
2740	return (-1);
2741}
2742
2743static int
2744decode_encoded_header_info(struct archive_read *a, struct _7z_stream_info *si)
2745{
2746	struct _7zip *zip = (struct _7zip *)a->format->data;
2747
2748	errno = 0;
2749	if (read_StreamsInfo(a, si) < 0) {
2750		if (errno == ENOMEM)
2751			archive_set_error(&a->archive, -1,
2752			    "Couldn't allocate memory");
2753		else
2754			archive_set_error(&a->archive, -1,
2755			    "Malformed 7-Zip archive");
2756		return (ARCHIVE_FATAL);
2757	}
2758
2759	if (si->pi.numPackStreams == 0 || si->ci.numFolders == 0) {
2760		archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
2761		return (ARCHIVE_FATAL);
2762	}
2763
2764	if (zip->header_offset < si->pi.pos + si->pi.sizes[0] ||
2765	    (int64_t)(si->pi.pos + si->pi.sizes[0]) < 0 ||
2766	    si->pi.sizes[0] == 0 || (int64_t)si->pi.pos < 0) {
2767		archive_set_error(&a->archive, -1, "Malformed Header offset");
2768		return (ARCHIVE_FATAL);
2769	}
2770
2771	return (ARCHIVE_OK);
2772}
2773
2774static const unsigned char *
2775header_bytes(struct archive_read *a, size_t rbytes)
2776{
2777	struct _7zip *zip = (struct _7zip *)a->format->data;
2778	const unsigned char *p;
2779
2780	if (zip->header_bytes_remaining < rbytes)
2781		return (NULL);
2782	if (zip->pack_stream_bytes_unconsumed)
2783		read_consume(a);
2784
2785	if (zip->header_is_encoded == 0) {
2786		p = __archive_read_ahead(a, rbytes, NULL);
2787		if (p == NULL)
2788			return (NULL);
2789		zip->header_bytes_remaining -= rbytes;
2790		zip->pack_stream_bytes_unconsumed = rbytes;
2791	} else {
2792		const void *buff;
2793		ssize_t bytes;
2794
2795		bytes = read_stream(a, &buff, rbytes, rbytes);
2796		if (bytes <= 0)
2797			return (NULL);
2798		zip->header_bytes_remaining -= bytes;
2799		p = buff;
2800	}
2801
2802	/* Update checksum */
2803	zip->header_crc32 = crc32(zip->header_crc32, p, (unsigned)rbytes);
2804	return (p);
2805}
2806
2807static int
2808slurp_central_directory(struct archive_read *a, struct _7zip *zip,
2809    struct _7z_header_info *header)
2810{
2811	const unsigned char *p;
2812	uint64_t next_header_offset;
2813	uint64_t next_header_size;
2814	uint32_t next_header_crc;
2815	ssize_t bytes_avail;
2816	int check_header_crc, r;
2817
2818	if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
2819		return (ARCHIVE_FATAL);
2820
2821	if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
2822		/* This is an executable ? Must be self-extracting... */
2823		r = skip_sfx(a, bytes_avail);
2824		if (r < ARCHIVE_WARN)
2825			return (r);
2826		if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
2827			return (ARCHIVE_FATAL);
2828	}
2829	zip->seek_base += 32;
2830
2831	if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0) {
2832		archive_set_error(&a->archive, -1, "Not 7-Zip archive file");
2833		return (ARCHIVE_FATAL);
2834	}
2835
2836	/* CRC check. */
2837	if (crc32(0, (const unsigned char *)p + 12, 20)
2838	    != archive_le32dec(p + 8)) {
2839		archive_set_error(&a->archive, -1, "Header CRC error");
2840		return (ARCHIVE_FATAL);
2841	}
2842
2843	next_header_offset = archive_le64dec(p + 12);
2844	next_header_size = archive_le64dec(p + 20);
2845	next_header_crc = archive_le32dec(p + 28);
2846
2847	if (next_header_size == 0)
2848		/* There is no entry in an archive file. */
2849		return (ARCHIVE_EOF);
2850
2851	if (((int64_t)next_header_offset) < 0) {
2852		archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
2853		return (ARCHIVE_FATAL);
2854	}
2855	__archive_read_consume(a, 32);
2856	if (next_header_offset != 0) {
2857		if (bytes_avail >= (ssize_t)next_header_offset)
2858			__archive_read_consume(a, next_header_offset);
2859		else if (__archive_read_seek(a,
2860		    next_header_offset + zip->seek_base, SEEK_SET) < 0)
2861			return (ARCHIVE_FATAL);
2862	}
2863	zip->stream_offset = next_header_offset;
2864	zip->header_offset = next_header_offset;
2865	zip->header_bytes_remaining = next_header_size;
2866	zip->header_crc32 = 0;
2867	zip->header_is_encoded = 0;
2868	zip->header_is_being_read = 1;
2869	zip->has_encrypted_entries = 0;
2870	check_header_crc = 1;
2871
2872	if ((p = header_bytes(a, 1)) == NULL) {
2873		archive_set_error(&a->archive,
2874		    ARCHIVE_ERRNO_FILE_FORMAT,
2875		    "Truncated 7-Zip file body");
2876		return (ARCHIVE_FATAL);
2877	}
2878	/* Parse ArchiveProperties. */
2879	switch (p[0]) {
2880	case kEncodedHeader:
2881		/*
2882		 * The archive has an encoded header and we have to decode it
2883		 * in order to parse the header correctly.
2884		 */
2885		r = decode_encoded_header_info(a, &(zip->si));
2886
2887		/* Check the EncodedHeader CRC.*/
2888		if (r == 0 && zip->header_crc32 != next_header_crc) {
2889			archive_set_error(&a->archive, -1,
2890			    "Damaged 7-Zip archive");
2891			r = -1;
2892		}
2893		if (r == 0) {
2894			if (zip->si.ci.folders[0].digest_defined)
2895				next_header_crc = zip->si.ci.folders[0].digest;
2896			else
2897				check_header_crc = 0;
2898			if (zip->pack_stream_bytes_unconsumed)
2899				read_consume(a);
2900			r = setup_decode_folder(a, zip->si.ci.folders, 1);
2901			if (r == 0) {
2902				zip->header_bytes_remaining =
2903					zip->folder_outbytes_remaining;
2904				r = seek_pack(a);
2905			}
2906		}
2907		/* Clean up StreamsInfo. */
2908		free_StreamsInfo(&(zip->si));
2909		memset(&(zip->si), 0, sizeof(zip->si));
2910		if (r < 0)
2911			return (ARCHIVE_FATAL);
2912		zip->header_is_encoded = 1;
2913		zip->header_crc32 = 0;
2914		/* FALL THROUGH */
2915	case kHeader:
2916		/*
2917		 * Parse the header.
2918		 */
2919		errno = 0;
2920		r = read_Header(a, header, zip->header_is_encoded);
2921		if (r < 0) {
2922			if (errno == ENOMEM)
2923				archive_set_error(&a->archive, -1,
2924				    "Couldn't allocate memory");
2925			else
2926				archive_set_error(&a->archive, -1,
2927				    "Damaged 7-Zip archive");
2928			return (ARCHIVE_FATAL);
2929		}
2930
2931		/*
2932		 *  Must be kEnd.
2933		 */
2934		if ((p = header_bytes(a, 1)) == NULL ||*p != kEnd) {
2935			archive_set_error(&a->archive, -1,
2936			    "Malformed 7-Zip archive");
2937			return (ARCHIVE_FATAL);
2938		}
2939
2940		/* Check the Header CRC.*/
2941		if (check_header_crc && zip->header_crc32 != next_header_crc) {
2942			archive_set_error(&a->archive, -1,
2943			    "Malformed 7-Zip archive");
2944			return (ARCHIVE_FATAL);
2945		}
2946		break;
2947	default:
2948		archive_set_error(&a->archive, -1,
2949		    "Unexpected Property ID = %X", p[0]);
2950		return (ARCHIVE_FATAL);
2951	}
2952
2953	/* Clean up variables be used for decoding the archive header */
2954	zip->pack_stream_remaining = 0;
2955	zip->pack_stream_index = 0;
2956	zip->folder_outbytes_remaining = 0;
2957	zip->uncompressed_buffer_bytes_remaining = 0;
2958	zip->pack_stream_bytes_unconsumed = 0;
2959	zip->header_is_being_read = 0;
2960
2961	return (ARCHIVE_OK);
2962}
2963
2964static ssize_t
2965get_uncompressed_data(struct archive_read *a, const void **buff, size_t size,
2966    size_t minimum)
2967{
2968	struct _7zip *zip = (struct _7zip *)a->format->data;
2969	ssize_t bytes_avail;
2970
2971	if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
2972		/* Copy mode. */
2973
2974		/*
2975		 * Note: '1' here is a performance optimization.
2976		 * Recall that the decompression layer returns a count of
2977		 * available bytes; asking for more than that forces the
2978		 * decompressor to combine reads by copying data.
2979		 */
2980		*buff = __archive_read_ahead(a, 1, &bytes_avail);
2981		if (bytes_avail <= 0) {
2982			archive_set_error(&a->archive,
2983			    ARCHIVE_ERRNO_FILE_FORMAT,
2984			    "Truncated 7-Zip file data");
2985			return (ARCHIVE_FATAL);
2986		}
2987		if ((size_t)bytes_avail >
2988		    zip->uncompressed_buffer_bytes_remaining)
2989			bytes_avail = (ssize_t)
2990			    zip->uncompressed_buffer_bytes_remaining;
2991		if ((size_t)bytes_avail > size)
2992			bytes_avail = (ssize_t)size;
2993
2994		zip->pack_stream_bytes_unconsumed = bytes_avail;
2995	} else if (zip->uncompressed_buffer_pointer == NULL) {
2996		/* Decompression has failed. */
2997		archive_set_error(&(a->archive),
2998		    ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
2999		return (ARCHIVE_FATAL);
3000	} else {
3001		/* Packed mode. */
3002		if (minimum > zip->uncompressed_buffer_bytes_remaining) {
3003			/*
3004			 * If remaining uncompressed data size is less than
3005			 * the minimum size, fill the buffer up to the
3006			 * minimum size.
3007			 */
3008			if (extract_pack_stream(a, minimum) < 0)
3009				return (ARCHIVE_FATAL);
3010		}
3011		if (size > zip->uncompressed_buffer_bytes_remaining)
3012			bytes_avail = (ssize_t)
3013			    zip->uncompressed_buffer_bytes_remaining;
3014		else
3015			bytes_avail = (ssize_t)size;
3016		*buff = zip->uncompressed_buffer_pointer;
3017		zip->uncompressed_buffer_pointer += bytes_avail;
3018	}
3019	zip->uncompressed_buffer_bytes_remaining -= bytes_avail;
3020	return (bytes_avail);
3021}
3022
3023static ssize_t
3024extract_pack_stream(struct archive_read *a, size_t minimum)
3025{
3026	struct _7zip *zip = (struct _7zip *)a->format->data;
3027	ssize_t bytes_avail;
3028	int r;
3029
3030	if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
3031		if (minimum == 0)
3032			minimum = 1;
3033		if (__archive_read_ahead(a, minimum, &bytes_avail) == NULL
3034		    || bytes_avail <= 0) {
3035			archive_set_error(&a->archive,
3036			    ARCHIVE_ERRNO_FILE_FORMAT,
3037			    "Truncated 7-Zip file body");
3038			return (ARCHIVE_FATAL);
3039		}
3040		if (bytes_avail > (ssize_t)zip->pack_stream_inbytes_remaining)
3041			bytes_avail = (ssize_t)zip->pack_stream_inbytes_remaining;
3042		zip->pack_stream_inbytes_remaining -= bytes_avail;
3043		if (bytes_avail > (ssize_t)zip->folder_outbytes_remaining)
3044			bytes_avail = (ssize_t)zip->folder_outbytes_remaining;
3045		zip->folder_outbytes_remaining -= bytes_avail;
3046		zip->uncompressed_buffer_bytes_remaining = bytes_avail;
3047		return (ARCHIVE_OK);
3048	}
3049
3050	/* If the buffer hasn't been allocated, allocate it now. */
3051	if (zip->uncompressed_buffer == NULL) {
3052		zip->uncompressed_buffer_size = UBUFF_SIZE;
3053		if (zip->uncompressed_buffer_size < minimum) {
3054			zip->uncompressed_buffer_size = minimum + 1023;
3055			zip->uncompressed_buffer_size &= ~0x3ff;
3056		}
3057		zip->uncompressed_buffer =
3058		    malloc(zip->uncompressed_buffer_size);
3059		if (zip->uncompressed_buffer == NULL) {
3060			archive_set_error(&a->archive, ENOMEM,
3061			    "No memory for 7-Zip decompression");
3062			return (ARCHIVE_FATAL);
3063		}
3064		zip->uncompressed_buffer_bytes_remaining = 0;
3065	} else if (zip->uncompressed_buffer_size < minimum ||
3066	    zip->uncompressed_buffer_bytes_remaining < minimum) {
3067		/*
3068		 * Make sure the uncompressed buffer can have bytes
3069		 * at least `minimum' bytes.
3070		 * NOTE: This case happen when reading the header.
3071		 */
3072		size_t used;
3073		if (zip->uncompressed_buffer_pointer != 0)
3074			used = zip->uncompressed_buffer_pointer -
3075				zip->uncompressed_buffer;
3076		else
3077			used = 0;
3078		if (zip->uncompressed_buffer_size < minimum) {
3079			/*
3080			 * Expand the uncompressed buffer up to
3081			 * the minimum size.
3082			 */
3083			void *p;
3084			size_t new_size;
3085
3086			new_size = minimum + 1023;
3087			new_size &= ~0x3ff;
3088			p = realloc(zip->uncompressed_buffer, new_size);
3089			if (p == NULL) {
3090				archive_set_error(&a->archive, ENOMEM,
3091				    "No memory for 7-Zip decompression");
3092				return (ARCHIVE_FATAL);
3093			}
3094			zip->uncompressed_buffer = (unsigned char *)p;
3095			zip->uncompressed_buffer_size = new_size;
3096		}
3097		/*
3098		 * Move unconsumed bytes to the head.
3099		 */
3100		if (used) {
3101			memmove(zip->uncompressed_buffer,
3102				zip->uncompressed_buffer + used,
3103				zip->uncompressed_buffer_bytes_remaining);
3104		}
3105	} else
3106		zip->uncompressed_buffer_bytes_remaining = 0;
3107	zip->uncompressed_buffer_pointer = NULL;
3108	for (;;) {
3109		size_t bytes_in, bytes_out;
3110		const void *buff_in;
3111		unsigned char *buff_out;
3112		int end_of_data;
3113
3114		/*
3115		 * Note: '1' here is a performance optimization.
3116		 * Recall that the decompression layer returns a count of
3117		 * available bytes; asking for more than that forces the
3118		 * decompressor to combine reads by copying data.
3119		 */
3120		buff_in = __archive_read_ahead(a, 1, &bytes_avail);
3121		if (bytes_avail <= 0) {
3122			archive_set_error(&a->archive,
3123			    ARCHIVE_ERRNO_FILE_FORMAT,
3124			    "Truncated 7-Zip file body");
3125			return (ARCHIVE_FATAL);
3126		}
3127
3128		buff_out = zip->uncompressed_buffer
3129			+ zip->uncompressed_buffer_bytes_remaining;
3130		bytes_out = zip->uncompressed_buffer_size
3131			- zip->uncompressed_buffer_bytes_remaining;
3132		bytes_in = bytes_avail;
3133		if (bytes_in > zip->pack_stream_inbytes_remaining)
3134			bytes_in = (size_t)zip->pack_stream_inbytes_remaining;
3135		/* Drive decompression. */
3136		r = decompress(a, zip, buff_out, &bytes_out,
3137			buff_in, &bytes_in);
3138		switch (r) {
3139		case ARCHIVE_OK:
3140			end_of_data = 0;
3141			break;
3142		case ARCHIVE_EOF:
3143			end_of_data = 1;
3144			break;
3145		default:
3146			return (ARCHIVE_FATAL);
3147		}
3148		zip->pack_stream_inbytes_remaining -= bytes_in;
3149		if (bytes_out > zip->folder_outbytes_remaining)
3150			bytes_out = (size_t)zip->folder_outbytes_remaining;
3151		zip->folder_outbytes_remaining -= bytes_out;
3152		zip->uncompressed_buffer_bytes_remaining += bytes_out;
3153		zip->pack_stream_bytes_unconsumed = bytes_in;
3154
3155		/*
3156		 * Continue decompression until uncompressed_buffer is full.
3157		 */
3158		if (zip->uncompressed_buffer_bytes_remaining ==
3159		    zip->uncompressed_buffer_size)
3160			break;
3161		if (zip->codec2 == _7Z_X86 && zip->odd_bcj_size &&
3162		    zip->uncompressed_buffer_bytes_remaining + 5 >
3163		    zip->uncompressed_buffer_size)
3164			break;
3165		if (zip->pack_stream_inbytes_remaining == 0 &&
3166		    zip->folder_outbytes_remaining == 0)
3167			break;
3168		if (end_of_data || (bytes_in == 0 && bytes_out == 0)) {
3169			archive_set_error(&(a->archive),
3170			    ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3171			return (ARCHIVE_FATAL);
3172		}
3173		read_consume(a);
3174	}
3175	if (zip->uncompressed_buffer_bytes_remaining < minimum) {
3176		archive_set_error(&(a->archive),
3177		    ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3178		return (ARCHIVE_FATAL);
3179	}
3180	zip->uncompressed_buffer_pointer = zip->uncompressed_buffer;
3181	return (ARCHIVE_OK);
3182}
3183
3184static int
3185seek_pack(struct archive_read *a)
3186{
3187	struct _7zip *zip = (struct _7zip *)a->format->data;
3188	int64_t pack_offset;
3189
3190	if (zip->pack_stream_remaining <= 0) {
3191		archive_set_error(&(a->archive),
3192		    ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3193		return (ARCHIVE_FATAL);
3194	}
3195	zip->pack_stream_inbytes_remaining =
3196	    zip->si.pi.sizes[zip->pack_stream_index];
3197	pack_offset = zip->si.pi.positions[zip->pack_stream_index];
3198	if (zip->stream_offset != pack_offset) {
3199		if (0 > __archive_read_seek(a, pack_offset + zip->seek_base,
3200		    SEEK_SET))
3201			return (ARCHIVE_FATAL);
3202		zip->stream_offset = pack_offset;
3203	}
3204	zip->pack_stream_index++;
3205	zip->pack_stream_remaining--;
3206	return (ARCHIVE_OK);
3207}
3208
3209static ssize_t
3210read_stream(struct archive_read *a, const void **buff, size_t size,
3211    size_t minimum)
3212{
3213	struct _7zip *zip = (struct _7zip *)a->format->data;
3214	uint64_t skip_bytes = 0;
3215	ssize_t r;
3216
3217	if (zip->uncompressed_buffer_bytes_remaining == 0) {
3218		if (zip->pack_stream_inbytes_remaining > 0) {
3219			r = extract_pack_stream(a, 0);
3220			if (r < 0)
3221				return (r);
3222			return (get_uncompressed_data(a, buff, size, minimum));
3223		} else if (zip->folder_outbytes_remaining > 0) {
3224			/* Extract a remaining pack stream. */
3225			r = extract_pack_stream(a, 0);
3226			if (r < 0)
3227				return (r);
3228			return (get_uncompressed_data(a, buff, size, minimum));
3229		}
3230	} else
3231		return (get_uncompressed_data(a, buff, size, minimum));
3232
3233	/*
3234	 * Current pack stream has been consumed.
3235	 */
3236	if (zip->pack_stream_remaining == 0) {
3237		if (zip->header_is_being_read) {
3238			/* Invalid sequence. This might happen when
3239			 * reading a malformed archive. */
3240			archive_set_error(&(a->archive),
3241			    ARCHIVE_ERRNO_MISC, "Malformed 7-Zip archive");
3242			return (ARCHIVE_FATAL);
3243		}
3244
3245		/*
3246		 * All current folder's pack streams have been
3247		 * consumed. Switch to next folder.
3248		 */
3249		if (zip->folder_index == 0 &&
3250		    (zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
3251		     || zip->folder_index != zip->entry->folderIndex)) {
3252			zip->folder_index = zip->entry->folderIndex;
3253			skip_bytes =
3254			    zip->si.ci.folders[zip->folder_index].skipped_bytes;
3255		}
3256
3257		if (zip->folder_index >= zip->si.ci.numFolders) {
3258			/*
3259			 * We have consumed all folders and its pack streams.
3260			 */
3261			*buff = NULL;
3262			return (0);
3263		}
3264		r = setup_decode_folder(a,
3265			&(zip->si.ci.folders[zip->folder_index]), 0);
3266		if (r != ARCHIVE_OK)
3267			return (ARCHIVE_FATAL);
3268
3269		zip->folder_index++;
3270	}
3271
3272	/*
3273	 * Switch to next pack stream.
3274	 */
3275	r = seek_pack(a);
3276	if (r < 0)
3277		return (r);
3278
3279	/* Extract a new pack stream. */
3280	r = extract_pack_stream(a, 0);
3281	if (r < 0)
3282		return (r);
3283
3284	/*
3285	 * Skip the bytes we alrady has skipped in skip_stream().
3286	 */
3287	while (skip_bytes) {
3288		ssize_t skipped;
3289
3290		if (zip->uncompressed_buffer_bytes_remaining == 0) {
3291			if (zip->pack_stream_inbytes_remaining > 0) {
3292				r = extract_pack_stream(a, 0);
3293				if (r < 0)
3294					return (r);
3295			} else if (zip->folder_outbytes_remaining > 0) {
3296				/* Extract a remaining pack stream. */
3297				r = extract_pack_stream(a, 0);
3298				if (r < 0)
3299					return (r);
3300			} else {
3301				archive_set_error(&a->archive,
3302				    ARCHIVE_ERRNO_FILE_FORMAT,
3303				    "Truncated 7-Zip file body");
3304				return (ARCHIVE_FATAL);
3305			}
3306		}
3307		skipped = get_uncompressed_data(
3308			a, buff, (size_t)skip_bytes, 0);
3309		if (skipped < 0)
3310			return (skipped);
3311		skip_bytes -= skipped;
3312		if (zip->pack_stream_bytes_unconsumed)
3313			read_consume(a);
3314	}
3315
3316	return (get_uncompressed_data(a, buff, size, minimum));
3317}
3318
3319static int
3320setup_decode_folder(struct archive_read *a, struct _7z_folder *folder,
3321    int header)
3322{
3323	struct _7zip *zip = (struct _7zip *)a->format->data;
3324	const struct _7z_coder *coder1, *coder2;
3325	const char *cname = (header)?"archive header":"file content";
3326	unsigned i;
3327	int r, found_bcj2 = 0;
3328
3329	/*
3330	 * Release the memory which the previous folder used for BCJ2.
3331	 */
3332	for (i = 0; i < 3; i++) {
3333		if (zip->sub_stream_buff[i] != NULL)
3334			free(zip->sub_stream_buff[i]);
3335		zip->sub_stream_buff[i] = NULL;
3336	}
3337
3338	/*
3339	 * Initialize a stream reader.
3340	 */
3341	zip->pack_stream_remaining = (unsigned)folder->numPackedStreams;
3342	zip->pack_stream_index = (unsigned)folder->packIndex;
3343	zip->folder_outbytes_remaining = folder_uncompressed_size(folder);
3344	zip->uncompressed_buffer_bytes_remaining = 0;
3345
3346	/*
3347	 * Check coder types.
3348	 */
3349	for (i = 0; i < folder->numCoders; i++) {
3350		switch(folder->coders[i].codec) {
3351			case _7Z_CRYPTO_MAIN_ZIP:
3352			case _7Z_CRYPTO_RAR_29:
3353			case _7Z_CRYPTO_AES_256_SHA_256: {
3354				/* For entry that is associated with this folder, mark
3355				   it as encrypted (data+metadata). */
3356				zip->has_encrypted_entries = 1;
3357				if (a->entry) {
3358					archive_entry_set_is_data_encrypted(a->entry, 1);
3359					archive_entry_set_is_metadata_encrypted(a->entry, 1);
3360				}
3361				archive_set_error(&(a->archive),
3362					ARCHIVE_ERRNO_MISC,
3363					"The %s is encrypted, "
3364					"but currently not supported", cname);
3365				return (ARCHIVE_FATAL);
3366			}
3367			case _7Z_X86_BCJ2: {
3368				found_bcj2++;
3369				break;
3370			}
3371		}
3372	}
3373	/* Now that we've checked for encryption, if there were still no
3374	 * encrypted entries found we can say for sure that there are none.
3375	 */
3376	if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
3377		zip->has_encrypted_entries = 0;
3378	}
3379
3380	if ((folder->numCoders > 2 && !found_bcj2) || found_bcj2 > 1) {
3381		archive_set_error(&(a->archive),
3382		    ARCHIVE_ERRNO_MISC,
3383		    "The %s is encoded with many filters, "
3384		    "but currently not supported", cname);
3385		return (ARCHIVE_FATAL);
3386	}
3387	coder1 = &(folder->coders[0]);
3388	if (folder->numCoders == 2)
3389		coder2 = &(folder->coders[1]);
3390	else
3391		coder2 = NULL;
3392
3393	if (found_bcj2) {
3394		/*
3395		 * Preparation to decode BCJ2.
3396		 * Decoding BCJ2 requires four sources. Those are at least,
3397		 * as far as I know, two types of the storage form.
3398		 */
3399		const struct _7z_coder *fc = folder->coders;
3400		static const struct _7z_coder coder_copy = {0, 1, 1, 0, NULL};
3401		const struct _7z_coder *scoder[3] =
3402			{&coder_copy, &coder_copy, &coder_copy};
3403		const void *buff;
3404		ssize_t bytes;
3405		unsigned char *b[3] = {NULL, NULL, NULL};
3406		uint64_t sunpack[3] ={-1, -1, -1};
3407		size_t s[3] = {0, 0, 0};
3408		int idx[3] = {0, 1, 2};
3409
3410		if (folder->numCoders == 4 && fc[3].codec == _7Z_X86_BCJ2 &&
3411		    folder->numInStreams == 7 && folder->numOutStreams == 4 &&
3412		    zip->pack_stream_remaining == 4) {
3413			/* Source type 1 made by 7zr or 7z with -m options. */
3414			if (folder->bindPairs[0].inIndex == 5) {
3415				/* The form made by 7zr */
3416				idx[0] = 1; idx[1] = 2; idx[2] = 0;
3417				scoder[1] = &(fc[1]);
3418				scoder[2] = &(fc[0]);
3419				sunpack[1] = folder->unPackSize[1];
3420				sunpack[2] = folder->unPackSize[0];
3421				coder1 = &(fc[2]);
3422			} else {
3423				/*
3424				 * NOTE: Some patterns do not work.
3425				 * work:
3426				 *  7z a -m0=BCJ2 -m1=COPY -m2=COPY
3427				 *       -m3=(any)
3428				 *  7z a -m0=BCJ2 -m1=COPY -m2=(any)
3429				 *       -m3=COPY
3430				 *  7z a -m0=BCJ2 -m1=(any) -m2=COPY
3431				 *       -m3=COPY
3432				 * not work:
3433				 *  other patterns.
3434				 *
3435				 * We have to handle this like `pipe' or
3436				 * our libarchive7s filter frame work,
3437				 * decoding the BCJ2 main stream sequentially,
3438				 * m3 -> m2 -> m1 -> BCJ2.
3439				 *
3440				 */
3441				if (fc[0].codec == _7Z_COPY &&
3442				    fc[1].codec == _7Z_COPY)
3443					coder1 = &(folder->coders[2]);
3444				else if (fc[0].codec == _7Z_COPY &&
3445				    fc[2].codec == _7Z_COPY)
3446					coder1 = &(folder->coders[1]);
3447				else if (fc[1].codec == _7Z_COPY &&
3448				    fc[2].codec == _7Z_COPY)
3449					coder1 = &(folder->coders[0]);
3450				else {
3451					archive_set_error(&(a->archive),
3452					    ARCHIVE_ERRNO_MISC,
3453					    "Unsupported form of "
3454					    "BCJ2 streams");
3455					return (ARCHIVE_FATAL);
3456				}
3457			}
3458			coder2 = &(fc[3]);
3459			zip->main_stream_bytes_remaining =
3460				(size_t)folder->unPackSize[2];
3461		} else if (coder2 != NULL && coder2->codec == _7Z_X86_BCJ2 &&
3462		    zip->pack_stream_remaining == 4 &&
3463		    folder->numInStreams == 5 && folder->numOutStreams == 2) {
3464			/* Source type 0 made by 7z */
3465			zip->main_stream_bytes_remaining =
3466				(size_t)folder->unPackSize[0];
3467		} else {
3468			/* We got an unexpected form. */
3469			archive_set_error(&(a->archive),
3470			    ARCHIVE_ERRNO_MISC,
3471			    "Unsupported form of BCJ2 streams");
3472			return (ARCHIVE_FATAL);
3473		}
3474
3475		/* Skip the main stream at this time. */
3476		if ((r = seek_pack(a)) < 0)
3477			return (r);
3478		zip->pack_stream_bytes_unconsumed =
3479		    (size_t)zip->pack_stream_inbytes_remaining;
3480		read_consume(a);
3481
3482		/* Read following three sub streams. */
3483		for (i = 0; i < 3; i++) {
3484			const struct _7z_coder *coder = scoder[i];
3485
3486			if ((r = seek_pack(a)) < 0) {
3487				free(b[0]); free(b[1]); free(b[2]);
3488				return (r);
3489			}
3490
3491			if (sunpack[i] == (uint64_t)-1)
3492				zip->folder_outbytes_remaining =
3493				    zip->pack_stream_inbytes_remaining;
3494			else
3495				zip->folder_outbytes_remaining = sunpack[i];
3496
3497			r = init_decompression(a, zip, coder, NULL);
3498			if (r != ARCHIVE_OK) {
3499				free(b[0]); free(b[1]); free(b[2]);
3500				return (ARCHIVE_FATAL);
3501			}
3502
3503			/* Allocate memory for the decorded data of a sub
3504			 * stream. */
3505			b[i] = malloc((size_t)zip->folder_outbytes_remaining);
3506			if (b[i] == NULL) {
3507				free(b[0]); free(b[1]); free(b[2]);
3508				archive_set_error(&a->archive, ENOMEM,
3509				    "No memory for 7-Zip decompression");
3510				return (ARCHIVE_FATAL);
3511			}
3512
3513			/* Extract a sub stream. */
3514			while (zip->pack_stream_inbytes_remaining > 0) {
3515				r = (int)extract_pack_stream(a, 0);
3516				if (r < 0) {
3517					free(b[0]); free(b[1]); free(b[2]);
3518					return (r);
3519				}
3520				bytes = get_uncompressed_data(a, &buff,
3521				    zip->uncompressed_buffer_bytes_remaining,
3522				    0);
3523				if (bytes < 0) {
3524					free(b[0]); free(b[1]); free(b[2]);
3525					return ((int)bytes);
3526				}
3527				memcpy(b[i]+s[i], buff, bytes);
3528				s[i] += bytes;
3529				if (zip->pack_stream_bytes_unconsumed)
3530					read_consume(a);
3531			}
3532		}
3533
3534		/* Set the sub streams to the right place. */
3535		for (i = 0; i < 3; i++) {
3536			zip->sub_stream_buff[i] = b[idx[i]];
3537			zip->sub_stream_size[i] = s[idx[i]];
3538			zip->sub_stream_bytes_remaining[i] = s[idx[i]];
3539		}
3540
3541		/* Allocate memory used for decoded main stream bytes. */
3542		if (zip->tmp_stream_buff == NULL) {
3543			zip->tmp_stream_buff_size = 32 * 1024;
3544			zip->tmp_stream_buff =
3545			    malloc(zip->tmp_stream_buff_size);
3546			if (zip->tmp_stream_buff == NULL) {
3547				archive_set_error(&a->archive, ENOMEM,
3548				    "No memory for 7-Zip decompression");
3549				return (ARCHIVE_FATAL);
3550			}
3551		}
3552		zip->tmp_stream_bytes_avail = 0;
3553		zip->tmp_stream_bytes_remaining = 0;
3554		zip->odd_bcj_size = 0;
3555		zip->bcj2_outPos = 0;
3556
3557		/*
3558		 * Reset a stream reader in order to read the main stream
3559		 * of BCJ2.
3560		 */
3561		zip->pack_stream_remaining = 1;
3562		zip->pack_stream_index = (unsigned)folder->packIndex;
3563		zip->folder_outbytes_remaining =
3564		    folder_uncompressed_size(folder);
3565		zip->uncompressed_buffer_bytes_remaining = 0;
3566	}
3567
3568	/*
3569	 * Initialize the decompressor for the new folder's pack streams.
3570	 */
3571	r = init_decompression(a, zip, coder1, coder2);
3572	if (r != ARCHIVE_OK)
3573		return (ARCHIVE_FATAL);
3574	return (ARCHIVE_OK);
3575}
3576
3577static int64_t
3578skip_stream(struct archive_read *a, size_t skip_bytes)
3579{
3580	struct _7zip *zip = (struct _7zip *)a->format->data;
3581	const void *p;
3582	int64_t skipped_bytes;
3583	size_t bytes = skip_bytes;
3584
3585	if (zip->folder_index == 0) {
3586		/*
3587		 * Optimization for a list mode.
3588		 * Avoid unncecessary decoding operations.
3589		 */
3590		zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
3591		    += skip_bytes;
3592		return (skip_bytes);
3593	}
3594
3595	while (bytes) {
3596		skipped_bytes = read_stream(a, &p, bytes, 0);
3597		if (skipped_bytes < 0)
3598			return (skipped_bytes);
3599		if (skipped_bytes == 0) {
3600			archive_set_error(&a->archive,
3601			    ARCHIVE_ERRNO_FILE_FORMAT,
3602			    "Truncated 7-Zip file body");
3603			return (ARCHIVE_FATAL);
3604		}
3605		bytes -= (size_t)skipped_bytes;
3606		if (zip->pack_stream_bytes_unconsumed)
3607			read_consume(a);
3608	}
3609	return (skip_bytes);
3610}
3611
3612/*
3613 * Brought from LZMA SDK.
3614 *
3615 * Bra86.c -- Converter for x86 code (BCJ)
3616 * 2008-10-04 : Igor Pavlov : Public domain
3617 *
3618 */
3619
3620#define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
3621
3622static void
3623x86_Init(struct _7zip *zip)
3624{
3625	zip->bcj_state = 0;
3626	zip->bcj_prevPosT = (size_t)0 - 1;
3627	zip->bcj_prevMask = 0;
3628	zip->bcj_ip = 5;
3629}
3630
3631static size_t
3632x86_Convert(struct _7zip *zip, uint8_t *data, size_t size)
3633{
3634	static const uint8_t kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
3635	static const uint8_t kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
3636	size_t bufferPos, prevPosT;
3637	uint32_t ip, prevMask;
3638
3639	if (size < 5)
3640		return 0;
3641
3642	bufferPos = 0;
3643	prevPosT = zip->bcj_prevPosT;
3644	prevMask = zip->bcj_prevMask;
3645	ip = zip->bcj_ip;
3646
3647	for (;;) {
3648		uint8_t *p = data + bufferPos;
3649		uint8_t *limit = data + size - 4;
3650
3651		for (; p < limit; p++)
3652			if ((*p & 0xFE) == 0xE8)
3653				break;
3654		bufferPos = (size_t)(p - data);
3655		if (p >= limit)
3656			break;
3657		prevPosT = bufferPos - prevPosT;
3658		if (prevPosT > 3)
3659			prevMask = 0;
3660		else {
3661			prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
3662			if (prevMask != 0) {
3663				unsigned char b =
3664					p[4 - kMaskToBitNumber[prevMask]];
3665				if (!kMaskToAllowedStatus[prevMask] ||
3666				    Test86MSByte(b)) {
3667					prevPosT = bufferPos;
3668					prevMask = ((prevMask << 1) & 0x7) | 1;
3669					bufferPos++;
3670					continue;
3671				}
3672			}
3673		}
3674		prevPosT = bufferPos;
3675
3676		if (Test86MSByte(p[4])) {
3677			uint32_t src = ((uint32_t)p[4] << 24) |
3678				((uint32_t)p[3] << 16) | ((uint32_t)p[2] << 8) |
3679				((uint32_t)p[1]);
3680			uint32_t dest;
3681			for (;;) {
3682				uint8_t b;
3683				int b_index;
3684
3685				dest = src - (ip + (uint32_t)bufferPos);
3686				if (prevMask == 0)
3687					break;
3688				b_index = kMaskToBitNumber[prevMask] * 8;
3689				b = (uint8_t)(dest >> (24 - b_index));
3690				if (!Test86MSByte(b))
3691					break;
3692				src = dest ^ ((1 << (32 - b_index)) - 1);
3693			}
3694			p[4] = (uint8_t)(~(((dest >> 24) & 1) - 1));
3695			p[3] = (uint8_t)(dest >> 16);
3696			p[2] = (uint8_t)(dest >> 8);
3697			p[1] = (uint8_t)dest;
3698			bufferPos += 5;
3699		} else {
3700			prevMask = ((prevMask << 1) & 0x7) | 1;
3701			bufferPos++;
3702		}
3703	}
3704	zip->bcj_prevPosT = prevPosT;
3705	zip->bcj_prevMask = prevMask;
3706	zip->bcj_ip += (uint32_t)bufferPos;
3707	return (bufferPos);
3708}
3709
3710/*
3711 * Brought from LZMA SDK.
3712 *
3713 * Bcj2.c -- Converter for x86 code (BCJ2)
3714 * 2008-10-04 : Igor Pavlov : Public domain
3715 *
3716 */
3717
3718#define SZ_ERROR_DATA	 ARCHIVE_FAILED
3719
3720#define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
3721#define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
3722
3723#define kNumTopBits 24
3724#define kTopValue ((uint32_t)1 << kNumTopBits)
3725
3726#define kNumBitModelTotalBits 11
3727#define kBitModelTotal (1 << kNumBitModelTotalBits)
3728#define kNumMoveBits 5
3729
3730#define RC_READ_BYTE (*buffer++)
3731#define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; }
3732#define RC_INIT2 zip->bcj2_code = 0; zip->bcj2_range = 0xFFFFFFFF; \
3733  { int ii; for (ii = 0; ii < 5; ii++) { RC_TEST; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }}
3734
3735#define NORMALIZE if (zip->bcj2_range < kTopValue) { RC_TEST; zip->bcj2_range <<= 8; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }
3736
3737#define IF_BIT_0(p) ttt = *(p); bound = (zip->bcj2_range >> kNumBitModelTotalBits) * ttt; if (zip->bcj2_code < bound)
3738#define UPDATE_0(p) zip->bcj2_range = bound; *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); NORMALIZE;
3739#define UPDATE_1(p) zip->bcj2_range -= bound; zip->bcj2_code -= bound; *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); NORMALIZE;
3740
3741static ssize_t
3742Bcj2_Decode(struct _7zip *zip, uint8_t *outBuf, size_t outSize)
3743{
3744	size_t inPos = 0, outPos = 0;
3745	const uint8_t *buf0, *buf1, *buf2, *buf3;
3746	size_t size0, size1, size2, size3;
3747	const uint8_t *buffer, *bufferLim;
3748	unsigned int i, j;
3749
3750	size0 = zip->tmp_stream_bytes_remaining;
3751	buf0 = zip->tmp_stream_buff + zip->tmp_stream_bytes_avail - size0;
3752	size1 = zip->sub_stream_bytes_remaining[0];
3753	buf1 = zip->sub_stream_buff[0] + zip->sub_stream_size[0] - size1;
3754	size2 = zip->sub_stream_bytes_remaining[1];
3755	buf2 = zip->sub_stream_buff[1] + zip->sub_stream_size[1] - size2;
3756	size3 = zip->sub_stream_bytes_remaining[2];
3757	buf3 = zip->sub_stream_buff[2] + zip->sub_stream_size[2] - size3;
3758
3759	buffer = buf3;
3760	bufferLim = buffer + size3;
3761
3762	if (zip->bcj_state == 0) {
3763		/*
3764		 * Initialize.
3765		 */
3766		zip->bcj2_prevByte = 0;
3767		for (i = 0;
3768		    i < sizeof(zip->bcj2_p) / sizeof(zip->bcj2_p[0]); i++)
3769			zip->bcj2_p[i] = kBitModelTotal >> 1;
3770		RC_INIT2;
3771		zip->bcj_state = 1;
3772	}
3773
3774	/*
3775	 * Gather the odd bytes of a previous call.
3776	 */
3777	for (i = 0; zip->odd_bcj_size > 0 && outPos < outSize; i++) {
3778		outBuf[outPos++] = zip->odd_bcj[i];
3779		zip->odd_bcj_size--;
3780	}
3781
3782	if (outSize == 0) {
3783		zip->bcj2_outPos += outPos;
3784		return (outPos);
3785	}
3786
3787	for (;;) {
3788		uint8_t b;
3789		CProb *prob;
3790		uint32_t bound;
3791		uint32_t ttt;
3792
3793		size_t limit = size0 - inPos;
3794		if (outSize - outPos < limit)
3795			limit = outSize - outPos;
3796
3797		if (zip->bcj_state == 1) {
3798			while (limit != 0) {
3799				uint8_t bb = buf0[inPos];
3800				outBuf[outPos++] = bb;
3801				if (IsJ(zip->bcj2_prevByte, bb)) {
3802					zip->bcj_state = 2;
3803					break;
3804				}
3805				inPos++;
3806				zip->bcj2_prevByte = bb;
3807				limit--;
3808			}
3809		}
3810
3811		if (limit == 0 || outPos == outSize)
3812			break;
3813		zip->bcj_state = 1;
3814
3815		b = buf0[inPos++];
3816
3817		if (b == 0xE8)
3818			prob = zip->bcj2_p + zip->bcj2_prevByte;
3819		else if (b == 0xE9)
3820			prob = zip->bcj2_p + 256;
3821		else
3822			prob = zip->bcj2_p + 257;
3823
3824		IF_BIT_0(prob) {
3825			UPDATE_0(prob)
3826			zip->bcj2_prevByte = b;
3827		} else {
3828			uint32_t dest;
3829			const uint8_t *v;
3830			uint8_t out[4];
3831
3832			UPDATE_1(prob)
3833			if (b == 0xE8) {
3834				v = buf1;
3835				if (size1 < 4)
3836					return SZ_ERROR_DATA;
3837				buf1 += 4;
3838				size1 -= 4;
3839			} else {
3840				v = buf2;
3841				if (size2 < 4)
3842					return SZ_ERROR_DATA;
3843				buf2 += 4;
3844				size2 -= 4;
3845			}
3846			dest = (((uint32_t)v[0] << 24) |
3847			    ((uint32_t)v[1] << 16) |
3848			    ((uint32_t)v[2] << 8) |
3849			    ((uint32_t)v[3])) -
3850			    ((uint32_t)zip->bcj2_outPos + (uint32_t)outPos + 4);
3851			out[0] = (uint8_t)dest;
3852			out[1] = (uint8_t)(dest >> 8);
3853			out[2] = (uint8_t)(dest >> 16);
3854			out[3] = zip->bcj2_prevByte = (uint8_t)(dest >> 24);
3855
3856			for (i = 0; i < 4 && outPos < outSize; i++)
3857				outBuf[outPos++] = out[i];
3858			if (i < 4) {
3859				/*
3860				 * Save odd bytes which we could not add into
3861				 * the output buffer because of out of space.
3862				 */
3863				zip->odd_bcj_size = 4 -i;
3864				for (; i < 4; i++) {
3865					j = i - 4 + (unsigned)zip->odd_bcj_size;
3866					zip->odd_bcj[j] = out[i];
3867				}
3868				break;
3869			}
3870		}
3871	}
3872	zip->tmp_stream_bytes_remaining -= inPos;
3873	zip->sub_stream_bytes_remaining[0] = size1;
3874	zip->sub_stream_bytes_remaining[1] = size2;
3875	zip->sub_stream_bytes_remaining[2] = bufferLim - buffer;
3876	zip->bcj2_outPos += outPos;
3877
3878	return ((ssize_t)outPos);
3879}
3880
3881