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 
113 struct _7z_digests {
114 	unsigned char	*defineds;
115 	uint32_t	*digests;
116 };
117 
118 
119 struct _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 
146 struct _7z_coders_info {
147 	uint64_t		 numFolders;
148 	struct _7z_folder	*folders;
149 	uint64_t		 dataStreamIndex;
150 };
151 
152 struct _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 
161 struct _7z_substream_info {
162 	size_t			 unpack_streams;
163 	uint64_t		*unpackSizes;
164 	unsigned char		*digestsDefined;
165 	uint32_t		*digests;
166 };
167 
168 struct _7z_stream_info {
169 	struct _7z_pack_info	 pi;
170 	struct _7z_coders_info	 ci;
171 	struct _7z_substream_info ss;
172 };
173 
174 struct _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 
183 struct _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 
208 struct _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 points of the file contents
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 	/* Decoding LZMA1 and LZMA2 data. */
267 #ifdef HAVE_LZMA_H
268 	lzma_stream		 lzstream;
269 	int			 lzstream_valid;
270 #endif
271 	/* Decoding bzip2 data. */
272 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
273 	bz_stream		 bzstream;
274 	int			 bzstream_valid;
275 #endif
276 	/* Decoding deflate data. */
277 #ifdef HAVE_ZLIB_H
278 	z_stream		 stream;
279 	int			 stream_valid;
280 #endif
281 	/* Decoding 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 
340 static int	archive_read_format_7zip_has_encrypted_entries(struct archive_read *);
341 static int	archive_read_support_format_7zip_capabilities(struct archive_read *a);
342 static int	archive_read_format_7zip_bid(struct archive_read *, int);
343 static int	archive_read_format_7zip_cleanup(struct archive_read *);
344 static int	archive_read_format_7zip_read_data(struct archive_read *,
345 		    const void **, size_t *, int64_t *);
346 static int	archive_read_format_7zip_read_data_skip(struct archive_read *);
347 static int	archive_read_format_7zip_read_header(struct archive_read *,
348 		    struct archive_entry *);
349 static int	check_7zip_header_in_sfx(const char *);
350 static unsigned long decode_codec_id(const unsigned char *, size_t);
351 static int	decode_encoded_header_info(struct archive_read *,
352 		    struct _7z_stream_info *);
353 static int	decompress(struct archive_read *, struct _7zip *,
354 		    void *, size_t *, const void *, size_t *);
355 static ssize_t	extract_pack_stream(struct archive_read *, size_t);
356 static void	fileTimeToUtc(uint64_t, time_t *, long *);
357 static uint64_t folder_uncompressed_size(struct _7z_folder *);
358 static void	free_CodersInfo(struct _7z_coders_info *);
359 static void	free_Digest(struct _7z_digests *);
360 static void	free_Folder(struct _7z_folder *);
361 static void	free_Header(struct _7z_header_info *);
362 static void	free_PackInfo(struct _7z_pack_info *);
363 static void	free_StreamsInfo(struct _7z_stream_info *);
364 static void	free_SubStreamsInfo(struct _7z_substream_info *);
365 static int	free_decompression(struct archive_read *, struct _7zip *);
366 static ssize_t	get_uncompressed_data(struct archive_read *, const void **,
367 		    size_t, size_t);
368 static const unsigned char * header_bytes(struct archive_read *, size_t);
369 static int	init_decompression(struct archive_read *, struct _7zip *,
370 		    const struct _7z_coder *, const struct _7z_coder *);
371 static int	parse_7zip_uint64(struct archive_read *, uint64_t *);
372 static int	read_Bools(struct archive_read *, unsigned char *, size_t);
373 static int	read_CodersInfo(struct archive_read *,
374 		    struct _7z_coders_info *);
375 static int	read_Digests(struct archive_read *, struct _7z_digests *,
376 		    size_t);
377 static int	read_Folder(struct archive_read *, struct _7z_folder *);
378 static int	read_Header(struct archive_read *, struct _7z_header_info *,
379 		    int);
380 static int	read_PackInfo(struct archive_read *, struct _7z_pack_info *);
381 static int	read_StreamsInfo(struct archive_read *,
382 		    struct _7z_stream_info *);
383 static int	read_SubStreamsInfo(struct archive_read *,
384 		    struct _7z_substream_info *, struct _7z_folder *, size_t);
385 static int	read_Times(struct archive_read *, struct _7z_header_info *,
386 		    int);
387 static void	read_consume(struct archive_read *);
388 static ssize_t	read_stream(struct archive_read *, const void **, size_t,
389 		    size_t);
390 static int	seek_pack(struct archive_read *);
391 static int64_t	skip_stream(struct archive_read *, size_t);
392 static int	skip_sfx(struct archive_read *, ssize_t);
393 static int	slurp_central_directory(struct archive_read *, struct _7zip *,
394 		    struct _7z_header_info *);
395 static int	setup_decode_folder(struct archive_read *, struct _7z_folder *,
396 		    int);
397 static void	x86_Init(struct _7zip *);
398 static size_t	x86_Convert(struct _7zip *, uint8_t *, size_t);
399 static ssize_t		Bcj2_Decode(struct _7zip *, uint8_t *, size_t);
400 
401 
402 int
403 archive_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 
444 static int
445 archive_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 
453 static int
454 archive_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 
465 static int
466 archive_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 
518 static int
519 check_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 
544 static int
545 skip_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 	 * already 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 	}
603 fatal:
604 	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
605 	    "Couldn't find out 7-Zip header");
606 	return (ARCHIVE_FATAL);
607 }
608 
609 static int
610 archive_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 symname, 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 
784 static int
785 archive_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 
852 static int
853 archive_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 
881 static int
882 archive_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 
901 static void
902 read_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  */
918 static void
919 set_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 
965 static unsigned long
966 decode_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 
978 static Byte
979 ppmd_read(void *p)
980 {
981 	struct archive_read *a = ((IByteIn*)p)->a;
982 	struct _7zip *zip = (struct _7zip *)(a->format->data);
983 	Byte b;
984 
985 	if (zip->ppstream.avail_in == 0) {
986 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
987 		    "Truncated RAR file data");
988 		zip->ppstream.overconsumed = 1;
989 		return (0);
990 	}
991 	b = *zip->ppstream.next_in++;
992 	zip->ppstream.avail_in--;
993 	zip->ppstream.total_in++;
994 	return (b);
995 }
996 
997 static int
998 init_decompression(struct archive_read *a, struct _7zip *zip,
999     const struct _7z_coder *coder1, const struct _7z_coder *coder2)
1000 {
1001 	int r;
1002 
1003 	zip->codec = coder1->codec;
1004 	zip->codec2 = -1;
1005 
1006 	switch (zip->codec) {
1007 	case _7Z_COPY:
1008 	case _7Z_BZ2:
1009 	case _7Z_DEFLATE:
1010 	case _7Z_PPMD:
1011 		if (coder2 != NULL) {
1012 			if (coder2->codec != _7Z_X86 &&
1013 			    coder2->codec != _7Z_X86_BCJ2) {
1014 				archive_set_error(&a->archive,
1015 				    ARCHIVE_ERRNO_MISC,
1016 				    "Unsupported filter %lx for %lx",
1017 				    coder2->codec, coder1->codec);
1018 				return (ARCHIVE_FAILED);
1019 			}
1020 			zip->codec2 = coder2->codec;
1021 			zip->bcj_state = 0;
1022 			if (coder2->codec == _7Z_X86)
1023 				x86_Init(zip);
1024 		}
1025 		break;
1026 	default:
1027 		break;
1028 	}
1029 
1030 	switch (zip->codec) {
1031 	case _7Z_COPY:
1032 		break;
1033 
1034 	case _7Z_LZMA: case _7Z_LZMA2:
1035 #ifdef HAVE_LZMA_H
1036 #if LZMA_VERSION_MAJOR >= 5
1037 /* Effectively disable the limiter. */
1038 #define LZMA_MEMLIMIT   UINT64_MAX
1039 #else
1040 /* NOTE: This needs to check memory size which running system has. */
1041 #define LZMA_MEMLIMIT   (1U << 30)
1042 #endif
1043 	{
1044 		lzma_options_delta delta_opt;
1045 		lzma_filter filters[LZMA_FILTERS_MAX], *ff;
1046 		int fi = 0;
1047 
1048 		if (zip->lzstream_valid) {
1049 			lzma_end(&(zip->lzstream));
1050 			zip->lzstream_valid = 0;
1051 		}
1052 
1053 		/*
1054 		 * NOTE: liblzma incompletely handle the BCJ+LZMA compressed
1055 		 * data made by 7-Zip because 7-Zip does not add End-Of-
1056 		 * Payload Marker(EOPM) at the end of LZMA compressed data,
1057 		 * and so liblzma cannot know the end of the compressed data
1058 		 * without EOPM. So consequently liblzma will not return last
1059 		 * three or four bytes of uncompressed data because
1060 		 * LZMA_FILTER_X86 filter does not handle input data if its
1061 		 * data size is less than five bytes. If liblzma detect EOPM
1062 		 * or know the uncompressed data size, liblzma will flush out
1063 		 * the remaining that three or four bytes of uncompressed
1064 		 * data. That is why we have to use our converting program
1065 		 * for BCJ+LZMA. If we were able to tell the uncompressed
1066 		 * size to liblzma when using lzma_raw_decoder() liblzma
1067 		 * could correctly deal with BCJ+LZMA. But unfortunately
1068 		 * there is no way to do that.
1069 		 * Discussion about this can be found at XZ Utils forum.
1070 		 */
1071 		if (coder2 != NULL) {
1072 			zip->codec2 = coder2->codec;
1073 
1074 			filters[fi].options = NULL;
1075 			switch (zip->codec2) {
1076 			case _7Z_X86:
1077 				if (zip->codec == _7Z_LZMA2) {
1078 					filters[fi].id = LZMA_FILTER_X86;
1079 					fi++;
1080 				} else
1081 					/* Use our filter. */
1082 					x86_Init(zip);
1083 				break;
1084 			case _7Z_X86_BCJ2:
1085 				/* Use our filter. */
1086 				zip->bcj_state = 0;
1087 				break;
1088 			case _7Z_DELTA:
1089 				filters[fi].id = LZMA_FILTER_DELTA;
1090 				memset(&delta_opt, 0, sizeof(delta_opt));
1091 				delta_opt.type = LZMA_DELTA_TYPE_BYTE;
1092 				delta_opt.dist = 1;
1093 				filters[fi].options = &delta_opt;
1094 				fi++;
1095 				break;
1096 			/* Following filters have not been tested yet. */
1097 			case _7Z_POWERPC:
1098 				filters[fi].id = LZMA_FILTER_POWERPC;
1099 				fi++;
1100 				break;
1101 			case _7Z_IA64:
1102 				filters[fi].id = LZMA_FILTER_IA64;
1103 				fi++;
1104 				break;
1105 			case _7Z_ARM:
1106 				filters[fi].id = LZMA_FILTER_ARM;
1107 				fi++;
1108 				break;
1109 			case _7Z_ARMTHUMB:
1110 				filters[fi].id = LZMA_FILTER_ARMTHUMB;
1111 				fi++;
1112 				break;
1113 			case _7Z_SPARC:
1114 				filters[fi].id = LZMA_FILTER_SPARC;
1115 				fi++;
1116 				break;
1117 			default:
1118 				archive_set_error(&a->archive,
1119 				    ARCHIVE_ERRNO_MISC,
1120 				    "Unexpected codec ID: %lX", zip->codec2);
1121 				return (ARCHIVE_FAILED);
1122 			}
1123 		}
1124 
1125 		if (zip->codec == _7Z_LZMA2)
1126 			filters[fi].id = LZMA_FILTER_LZMA2;
1127 		else
1128 			filters[fi].id = LZMA_FILTER_LZMA1;
1129 		filters[fi].options = NULL;
1130 		ff = &filters[fi];
1131 		r = lzma_properties_decode(&filters[fi], NULL,
1132 		    coder1->properties, (size_t)coder1->propertiesSize);
1133 		if (r != LZMA_OK) {
1134 			set_error(a, r);
1135 			return (ARCHIVE_FAILED);
1136 		}
1137 		fi++;
1138 
1139 		filters[fi].id = LZMA_VLI_UNKNOWN;
1140 		filters[fi].options = NULL;
1141 		r = lzma_raw_decoder(&(zip->lzstream), filters);
1142 		free(ff->options);
1143 		if (r != LZMA_OK) {
1144 			set_error(a, r);
1145 			return (ARCHIVE_FAILED);
1146 		}
1147 		zip->lzstream_valid = 1;
1148 		zip->lzstream.total_in = 0;
1149 		zip->lzstream.total_out = 0;
1150 		break;
1151 	}
1152 #else
1153 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1154 		    "LZMA codec is unsupported");
1155 		return (ARCHIVE_FAILED);
1156 #endif
1157 	case _7Z_BZ2:
1158 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1159 		if (zip->bzstream_valid) {
1160 			BZ2_bzDecompressEnd(&(zip->bzstream));
1161 			zip->bzstream_valid = 0;
1162 		}
1163 		r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 0);
1164 		if (r == BZ_MEM_ERROR)
1165 			r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 1);
1166 		if (r != BZ_OK) {
1167 			int err = ARCHIVE_ERRNO_MISC;
1168 			const char *detail = NULL;
1169 			switch (r) {
1170 			case BZ_PARAM_ERROR:
1171 				detail = "invalid setup parameter";
1172 				break;
1173 			case BZ_MEM_ERROR:
1174 				err = ENOMEM;
1175 				detail = "out of memory";
1176 				break;
1177 			case BZ_CONFIG_ERROR:
1178 				detail = "mis-compiled library";
1179 				break;
1180 			}
1181 			archive_set_error(&a->archive, err,
1182 			    "Internal error initializing decompressor: %s",
1183 			    detail != NULL ? detail : "??");
1184 			zip->bzstream_valid = 0;
1185 			return (ARCHIVE_FAILED);
1186 		}
1187 		zip->bzstream_valid = 1;
1188 		zip->bzstream.total_in_lo32 = 0;
1189 		zip->bzstream.total_in_hi32 = 0;
1190 		zip->bzstream.total_out_lo32 = 0;
1191 		zip->bzstream.total_out_hi32 = 0;
1192 		break;
1193 #else
1194 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1195 		    "BZ2 codec is unsupported");
1196 		return (ARCHIVE_FAILED);
1197 #endif
1198 	case _7Z_DEFLATE:
1199 #ifdef HAVE_ZLIB_H
1200 		if (zip->stream_valid)
1201 			r = inflateReset(&(zip->stream));
1202 		else
1203 			r = inflateInit2(&(zip->stream),
1204 			    -15 /* Don't check for zlib header */);
1205 		if (r != Z_OK) {
1206 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1207 			    "Couldn't initialize zlib stream.");
1208 			return (ARCHIVE_FAILED);
1209 		}
1210 		zip->stream_valid = 1;
1211 		zip->stream.total_in = 0;
1212 		zip->stream.total_out = 0;
1213 		break;
1214 #else
1215 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1216 		    "DEFLATE codec is unsupported");
1217 		return (ARCHIVE_FAILED);
1218 #endif
1219 	case _7Z_PPMD:
1220 	{
1221 		unsigned order;
1222 		uint32_t msize;
1223 
1224 		if (zip->ppmd7_valid) {
1225 			__archive_ppmd7_functions.Ppmd7_Free(
1226 			    &zip->ppmd7_context);
1227 			zip->ppmd7_valid = 0;
1228 		}
1229 
1230 		if (coder1->propertiesSize < 5) {
1231 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1232 			    "Malformed PPMd parameter");
1233 			return (ARCHIVE_FAILED);
1234 		}
1235 		order = coder1->properties[0];
1236 		msize = archive_le32dec(&(coder1->properties[1]));
1237 		if (order < PPMD7_MIN_ORDER || order > PPMD7_MAX_ORDER ||
1238 		    msize < PPMD7_MIN_MEM_SIZE || msize > PPMD7_MAX_MEM_SIZE) {
1239 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1240 			    "Malformed PPMd parameter");
1241 			return (ARCHIVE_FAILED);
1242 		}
1243 		__archive_ppmd7_functions.Ppmd7_Construct(&zip->ppmd7_context);
1244 		r = __archive_ppmd7_functions.Ppmd7_Alloc(
1245 			&zip->ppmd7_context, msize);
1246 		if (r == 0) {
1247 			archive_set_error(&a->archive, ENOMEM,
1248 			    "Coludn't allocate memory for PPMd");
1249 			return (ARCHIVE_FATAL);
1250 		}
1251 		__archive_ppmd7_functions.Ppmd7_Init(
1252 			&zip->ppmd7_context, order);
1253 		__archive_ppmd7_functions.Ppmd7z_RangeDec_CreateVTable(
1254 			&zip->range_dec);
1255 		zip->ppmd7_valid = 1;
1256 		zip->ppmd7_stat = 0;
1257 		zip->ppstream.overconsumed = 0;
1258 		zip->ppstream.total_in = 0;
1259 		zip->ppstream.total_out = 0;
1260 		break;
1261 	}
1262 	case _7Z_X86:
1263 	case _7Z_X86_BCJ2:
1264 	case _7Z_POWERPC:
1265 	case _7Z_IA64:
1266 	case _7Z_ARM:
1267 	case _7Z_ARMTHUMB:
1268 	case _7Z_SPARC:
1269 	case _7Z_DELTA:
1270 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1271 		    "Unexpected codec ID: %lX", zip->codec);
1272 		return (ARCHIVE_FAILED);
1273 	case _7Z_CRYPTO_MAIN_ZIP:
1274 	case _7Z_CRYPTO_RAR_29:
1275 	case _7Z_CRYPTO_AES_256_SHA_256:
1276 		if (a->entry) {
1277 			archive_entry_set_is_metadata_encrypted(a->entry, 1);
1278 			archive_entry_set_is_data_encrypted(a->entry, 1);
1279 			zip->has_encrypted_entries = 1;
1280 		}
1281 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1282 		    "Crypto codec not supported yet (ID: 0x%lX)", zip->codec);
1283 		return (ARCHIVE_FAILED);
1284 	default:
1285 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1286 		    "Unknown codec ID: %lX", zip->codec);
1287 		return (ARCHIVE_FAILED);
1288 	}
1289 
1290 	return (ARCHIVE_OK);
1291 }
1292 
1293 static int
1294 decompress(struct archive_read *a, struct _7zip *zip,
1295     void *buff, size_t *outbytes, const void *b, size_t *used)
1296 {
1297 	const uint8_t *t_next_in;
1298 	uint8_t *t_next_out;
1299 	size_t o_avail_in, o_avail_out;
1300 	size_t t_avail_in, t_avail_out;
1301 	uint8_t *bcj2_next_out;
1302 	size_t bcj2_avail_out;
1303 	int r, ret = ARCHIVE_OK;
1304 
1305 	t_avail_in = o_avail_in = *used;
1306 	t_avail_out = o_avail_out = *outbytes;
1307 	t_next_in = b;
1308 	t_next_out = buff;
1309 
1310 	if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
1311 		int i;
1312 
1313 		/* Do not copy out the BCJ remaining bytes when the output
1314 		 * buffer size is less than five bytes. */
1315 		if (o_avail_in != 0 && t_avail_out < 5 && zip->odd_bcj_size) {
1316 			*used = 0;
1317 			*outbytes = 0;
1318 			return (ret);
1319 		}
1320 		for (i = 0; zip->odd_bcj_size > 0 && t_avail_out; i++) {
1321 			*t_next_out++ = zip->odd_bcj[i];
1322 			t_avail_out--;
1323 			zip->odd_bcj_size--;
1324 		}
1325 		if (o_avail_in == 0 || t_avail_out == 0) {
1326 			*used = o_avail_in - t_avail_in;
1327 			*outbytes = o_avail_out - t_avail_out;
1328 			if (o_avail_in == 0)
1329 				ret = ARCHIVE_EOF;
1330 			return (ret);
1331 		}
1332 	}
1333 
1334 	bcj2_next_out = t_next_out;
1335 	bcj2_avail_out = t_avail_out;
1336 	if (zip->codec2 == _7Z_X86_BCJ2) {
1337 		/*
1338 		 * Decord a remaining decompressed main stream for BCJ2.
1339 		 */
1340 		if (zip->tmp_stream_bytes_remaining) {
1341 			ssize_t bytes;
1342 			size_t remaining = zip->tmp_stream_bytes_remaining;
1343 			bytes = Bcj2_Decode(zip, t_next_out, t_avail_out);
1344 			if (bytes < 0) {
1345 				archive_set_error(&(a->archive),
1346 				    ARCHIVE_ERRNO_MISC,
1347 				    "BCJ2 conversion Failed");
1348 				return (ARCHIVE_FAILED);
1349 			}
1350 			zip->main_stream_bytes_remaining -=
1351 			    remaining - zip->tmp_stream_bytes_remaining;
1352 			t_avail_out -= bytes;
1353 			if (o_avail_in == 0 || t_avail_out == 0) {
1354 				*used = 0;
1355 				*outbytes = o_avail_out - t_avail_out;
1356 				if (o_avail_in == 0 &&
1357 				    zip->tmp_stream_bytes_remaining)
1358 					ret = ARCHIVE_EOF;
1359 				return (ret);
1360 			}
1361 			t_next_out += bytes;
1362 			bcj2_next_out = t_next_out;
1363 			bcj2_avail_out = t_avail_out;
1364 		}
1365 		t_next_out = zip->tmp_stream_buff;
1366 		t_avail_out = zip->tmp_stream_buff_size;
1367 	}
1368 
1369 	switch (zip->codec) {
1370 	case _7Z_COPY:
1371 	{
1372 		size_t bytes =
1373 		    (t_avail_in > t_avail_out)?t_avail_out:t_avail_in;
1374 
1375 		memcpy(t_next_out, t_next_in, bytes);
1376 		t_avail_in -= bytes;
1377 		t_avail_out -= bytes;
1378 		if (o_avail_in == 0)
1379 			ret = ARCHIVE_EOF;
1380 		break;
1381 	}
1382 #ifdef HAVE_LZMA_H
1383 	case _7Z_LZMA: case _7Z_LZMA2:
1384 		zip->lzstream.next_in = t_next_in;
1385 		zip->lzstream.avail_in = t_avail_in;
1386 		zip->lzstream.next_out = t_next_out;
1387 		zip->lzstream.avail_out = t_avail_out;
1388 
1389 		r = lzma_code(&(zip->lzstream), LZMA_RUN);
1390 		switch (r) {
1391 		case LZMA_STREAM_END: /* Found end of stream. */
1392 			lzma_end(&(zip->lzstream));
1393 			zip->lzstream_valid = 0;
1394 			ret = ARCHIVE_EOF;
1395 			break;
1396 		case LZMA_OK: /* Decompressor made some progress. */
1397 			break;
1398 		default:
1399 			archive_set_error(&(a->archive),
1400 			    ARCHIVE_ERRNO_MISC,
1401 				"Decompression failed(%d)",
1402 			    r);
1403 			return (ARCHIVE_FAILED);
1404 		}
1405 		t_avail_in = zip->lzstream.avail_in;
1406 		t_avail_out = zip->lzstream.avail_out;
1407 		break;
1408 #endif
1409 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1410 	case _7Z_BZ2:
1411 		zip->bzstream.next_in = (char *)(uintptr_t)t_next_in;
1412 		zip->bzstream.avail_in = t_avail_in;
1413 		zip->bzstream.next_out = (char *)(uintptr_t)t_next_out;
1414 		zip->bzstream.avail_out = t_avail_out;
1415 		r = BZ2_bzDecompress(&(zip->bzstream));
1416 		switch (r) {
1417 		case BZ_STREAM_END: /* Found end of stream. */
1418 			switch (BZ2_bzDecompressEnd(&(zip->bzstream))) {
1419 			case BZ_OK:
1420 				break;
1421 			default:
1422 				archive_set_error(&(a->archive),
1423 				    ARCHIVE_ERRNO_MISC,
1424 				    "Failed to clean up decompressor");
1425 				return (ARCHIVE_FAILED);
1426 			}
1427 			zip->bzstream_valid = 0;
1428 			ret = ARCHIVE_EOF;
1429 			break;
1430 		case BZ_OK: /* Decompressor made some progress. */
1431 			break;
1432 		default:
1433 			archive_set_error(&(a->archive),
1434 			    ARCHIVE_ERRNO_MISC,
1435 			    "bzip decompression failed");
1436 			return (ARCHIVE_FAILED);
1437 		}
1438 		t_avail_in = zip->bzstream.avail_in;
1439 		t_avail_out = zip->bzstream.avail_out;
1440 		break;
1441 #endif
1442 #ifdef HAVE_ZLIB_H
1443 	case _7Z_DEFLATE:
1444 		zip->stream.next_in = (Bytef *)(uintptr_t)t_next_in;
1445 		zip->stream.avail_in = (uInt)t_avail_in;
1446 		zip->stream.next_out = t_next_out;
1447 		zip->stream.avail_out = (uInt)t_avail_out;
1448 		r = inflate(&(zip->stream), 0);
1449 		switch (r) {
1450 		case Z_STREAM_END: /* Found end of stream. */
1451 			ret = ARCHIVE_EOF;
1452 			break;
1453 		case Z_OK: /* Decompressor made some progress.*/
1454 			break;
1455 		default:
1456 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1457 			    "File decompression failed (%d)", r);
1458 			return (ARCHIVE_FAILED);
1459 		}
1460 		t_avail_in = zip->stream.avail_in;
1461 		t_avail_out = zip->stream.avail_out;
1462 		break;
1463 #endif
1464 	case _7Z_PPMD:
1465 	{
1466 		uint64_t flush_bytes;
1467 
1468 		if (!zip->ppmd7_valid || zip->ppmd7_stat < 0 ||
1469 		    t_avail_out <= 0) {
1470 			archive_set_error(&(a->archive),
1471 			    ARCHIVE_ERRNO_MISC,
1472 			    "Decompression internal error");
1473 			return (ARCHIVE_FAILED);
1474 		}
1475 		zip->ppstream.next_in = t_next_in;
1476 		zip->ppstream.avail_in = t_avail_in;
1477 		zip->ppstream.next_out = t_next_out;
1478 		zip->ppstream.avail_out = t_avail_out;
1479 		if (zip->ppmd7_stat == 0) {
1480 			zip->bytein.a = a;
1481 			zip->bytein.Read = &ppmd_read;
1482 			zip->range_dec.Stream = &zip->bytein;
1483 			r = __archive_ppmd7_functions.Ppmd7z_RangeDec_Init(
1484 				&(zip->range_dec));
1485 			if (r == 0) {
1486 				zip->ppmd7_stat = -1;
1487 				archive_set_error(&a->archive,
1488 				    ARCHIVE_ERRNO_MISC,
1489 				    "Failed to initialize PPMd range decorder");
1490 				return (ARCHIVE_FAILED);
1491 			}
1492 			if (zip->ppstream.overconsumed) {
1493 				zip->ppmd7_stat = -1;
1494 				return (ARCHIVE_FAILED);
1495 			}
1496 			zip->ppmd7_stat = 1;
1497 		}
1498 
1499 		if (t_avail_in == 0)
1500 			/* XXX Flush out remaining decoded data XXX */
1501 			flush_bytes = zip->folder_outbytes_remaining;
1502 		else
1503 			flush_bytes = 0;
1504 
1505 		do {
1506 			int sym;
1507 
1508 			sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1509 				&(zip->ppmd7_context), &(zip->range_dec.p));
1510 			if (sym < 0) {
1511 				zip->ppmd7_stat = -1;
1512 				archive_set_error(&a->archive,
1513 				    ARCHIVE_ERRNO_FILE_FORMAT,
1514 				    "Failed to decode PPMd");
1515 				return (ARCHIVE_FAILED);
1516 			}
1517 			if (zip->ppstream.overconsumed) {
1518 				zip->ppmd7_stat = -1;
1519 				return (ARCHIVE_FAILED);
1520 			}
1521 			*zip->ppstream.next_out++ = (unsigned char)sym;
1522 			zip->ppstream.avail_out--;
1523 			zip->ppstream.total_out++;
1524 			if (flush_bytes)
1525 				flush_bytes--;
1526 		} while (zip->ppstream.avail_out &&
1527 			(zip->ppstream.avail_in || flush_bytes));
1528 
1529 		t_avail_in = (size_t)zip->ppstream.avail_in;
1530 		t_avail_out = (size_t)zip->ppstream.avail_out;
1531 		break;
1532 	}
1533 	default:
1534 		archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1535 		    "Decompression internal error");
1536 		return (ARCHIVE_FAILED);
1537 	}
1538 	if (ret != ARCHIVE_OK && ret != ARCHIVE_EOF)
1539 		return (ret);
1540 
1541 	*used = o_avail_in - t_avail_in;
1542 	*outbytes = o_avail_out - t_avail_out;
1543 
1544 	/*
1545 	 * Decord BCJ.
1546 	 */
1547 	if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
1548 		size_t l = x86_Convert(zip, buff, *outbytes);
1549 		zip->odd_bcj_size = *outbytes - l;
1550 		if (zip->odd_bcj_size > 0 && zip->odd_bcj_size <= 4 &&
1551 		    o_avail_in && ret != ARCHIVE_EOF) {
1552 			memcpy(zip->odd_bcj, ((unsigned char *)buff) + l,
1553 			    zip->odd_bcj_size);
1554 			*outbytes = l;
1555 		} else
1556 			zip->odd_bcj_size = 0;
1557 	}
1558 
1559 	/*
1560 	 * Decord BCJ2 with a decompressed main stream.
1561 	 */
1562 	if (zip->codec2 == _7Z_X86_BCJ2) {
1563 		ssize_t bytes;
1564 
1565 		zip->tmp_stream_bytes_avail =
1566 		    zip->tmp_stream_buff_size - t_avail_out;
1567 		if (zip->tmp_stream_bytes_avail >
1568 		      zip->main_stream_bytes_remaining)
1569 			zip->tmp_stream_bytes_avail =
1570 			    zip->main_stream_bytes_remaining;
1571 		zip->tmp_stream_bytes_remaining = zip->tmp_stream_bytes_avail;
1572 		bytes = Bcj2_Decode(zip, bcj2_next_out, bcj2_avail_out);
1573 		if (bytes < 0) {
1574 			archive_set_error(&(a->archive),
1575 			    ARCHIVE_ERRNO_MISC, "BCJ2 conversion Failed");
1576 			return (ARCHIVE_FAILED);
1577 		}
1578 		zip->main_stream_bytes_remaining -=
1579 		    zip->tmp_stream_bytes_avail
1580 		      - zip->tmp_stream_bytes_remaining;
1581 		bcj2_avail_out -= bytes;
1582 		*outbytes = o_avail_out - bcj2_avail_out;
1583 	}
1584 
1585 	return (ret);
1586 }
1587 
1588 static int
1589 free_decompression(struct archive_read *a, struct _7zip *zip)
1590 {
1591 	int r = ARCHIVE_OK;
1592 
1593 #if !defined(HAVE_ZLIB_H) &&\
1594 	!(defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR))
1595 	(void)a;/* UNUSED */
1596 #endif
1597 #ifdef HAVE_LZMA_H
1598 	if (zip->lzstream_valid)
1599 		lzma_end(&(zip->lzstream));
1600 #endif
1601 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1602 	if (zip->bzstream_valid) {
1603 		if (BZ2_bzDecompressEnd(&(zip->bzstream)) != BZ_OK) {
1604 			archive_set_error(&a->archive,
1605 			    ARCHIVE_ERRNO_MISC,
1606 			    "Failed to clean up bzip2 decompressor");
1607 			r = ARCHIVE_FATAL;
1608 		}
1609 		zip->bzstream_valid = 0;
1610 	}
1611 #endif
1612 #ifdef HAVE_ZLIB_H
1613 	if (zip->stream_valid) {
1614 		if (inflateEnd(&(zip->stream)) != Z_OK) {
1615 			archive_set_error(&a->archive,
1616 			    ARCHIVE_ERRNO_MISC,
1617 			    "Failed to clean up zlib decompressor");
1618 			r = ARCHIVE_FATAL;
1619 		}
1620 		zip->stream_valid = 0;
1621 	}
1622 #endif
1623 	if (zip->ppmd7_valid) {
1624 		__archive_ppmd7_functions.Ppmd7_Free(
1625 			&zip->ppmd7_context);
1626 		zip->ppmd7_valid = 0;
1627 	}
1628 	return (r);
1629 }
1630 
1631 static int
1632 parse_7zip_uint64(struct archive_read *a, uint64_t *val)
1633 {
1634 	const unsigned char *p;
1635 	unsigned char avail, mask;
1636 	int i;
1637 
1638 	if ((p = header_bytes(a, 1)) == NULL)
1639 		return (-1);
1640 	avail = *p;
1641 	mask = 0x80;
1642 	*val = 0;
1643 	for (i = 0; i < 8; i++) {
1644 		if (avail & mask) {
1645 			if ((p = header_bytes(a, 1)) == NULL)
1646 				return (-1);
1647 			*val |= ((uint64_t)*p) << (8 * i);
1648 			mask >>= 1;
1649 			continue;
1650 		}
1651 		*val += ((uint64_t)(avail & (mask -1))) << (8 * i);
1652 		break;
1653 	}
1654 	return (0);
1655 }
1656 
1657 static int
1658 read_Bools(struct archive_read *a, unsigned char *data, size_t num)
1659 {
1660 	const unsigned char *p;
1661 	unsigned i, mask = 0, avail = 0;
1662 
1663 	for (i = 0; i < num; i++) {
1664 		if (mask == 0) {
1665 			if ((p = header_bytes(a, 1)) == NULL)
1666 				return (-1);
1667 			avail = *p;
1668 			mask = 0x80;
1669 		}
1670 		data[i] = (avail & mask)?1:0;
1671 		mask >>= 1;
1672 	}
1673 	return (0);
1674 }
1675 
1676 static void
1677 free_Digest(struct _7z_digests *d)
1678 {
1679 	free(d->defineds);
1680 	free(d->digests);
1681 }
1682 
1683 static int
1684 read_Digests(struct archive_read *a, struct _7z_digests *d, size_t num)
1685 {
1686 	const unsigned char *p;
1687 	unsigned i;
1688 
1689 	if (num == 0)
1690 		return (-1);
1691 	memset(d, 0, sizeof(*d));
1692 
1693 	d->defineds = malloc(num);
1694 	if (d->defineds == NULL)
1695 		return (-1);
1696 	/*
1697 	 * Read Bools.
1698 	 */
1699 	if ((p = header_bytes(a, 1)) == NULL)
1700 		return (-1);
1701 	if (*p == 0) {
1702 		if (read_Bools(a, d->defineds, num) < 0)
1703 			return (-1);
1704 	} else
1705 		/* All are defined */
1706 		memset(d->defineds, 1, num);
1707 
1708 	d->digests = calloc(num, sizeof(*d->digests));
1709 	if (d->digests == NULL)
1710 		return (-1);
1711 	for (i = 0; i < num; i++) {
1712 		if (d->defineds[i]) {
1713 			if ((p = header_bytes(a, 4)) == NULL)
1714 				return (-1);
1715 			d->digests[i] = archive_le32dec(p);
1716 		}
1717 	}
1718 
1719 	return (0);
1720 }
1721 
1722 static void
1723 free_PackInfo(struct _7z_pack_info *pi)
1724 {
1725 	free(pi->sizes);
1726 	free(pi->positions);
1727 	free_Digest(&(pi->digest));
1728 }
1729 
1730 static int
1731 read_PackInfo(struct archive_read *a, struct _7z_pack_info *pi)
1732 {
1733 	const unsigned char *p;
1734 	unsigned i;
1735 
1736 	memset(pi, 0, sizeof(*pi));
1737 
1738 	/*
1739 	 * Read PackPos.
1740 	 */
1741 	if (parse_7zip_uint64(a, &(pi->pos)) < 0)
1742 		return (-1);
1743 
1744 	/*
1745 	 * Read NumPackStreams.
1746 	 */
1747 	if (parse_7zip_uint64(a, &(pi->numPackStreams)) < 0)
1748 		return (-1);
1749 	if (pi->numPackStreams == 0)
1750 		return (-1);
1751 	if (UMAX_ENTRY < pi->numPackStreams)
1752 		return (-1);
1753 
1754 	/*
1755 	 * Read PackSizes[num]
1756 	 */
1757 	if ((p = header_bytes(a, 1)) == NULL)
1758 		return (-1);
1759 	if (*p == kEnd)
1760 		/* PackSizes[num] are not present. */
1761 		return (0);
1762 	if (*p != kSize)
1763 		return (-1);
1764 	pi->sizes = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
1765 	pi->positions = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
1766 	if (pi->sizes == NULL || pi->positions == NULL)
1767 		return (-1);
1768 
1769 	for (i = 0; i < pi->numPackStreams; i++) {
1770 		if (parse_7zip_uint64(a, &(pi->sizes[i])) < 0)
1771 			return (-1);
1772 	}
1773 
1774 	/*
1775 	 * Read PackStreamDigests[num]
1776 	 */
1777 	if ((p = header_bytes(a, 1)) == NULL)
1778 		return (-1);
1779 	if (*p == kEnd) {
1780 		/* PackStreamDigests[num] are not present. */
1781 		pi->digest.defineds =
1782 		    calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.defineds));
1783 		pi->digest.digests =
1784 		    calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.digests));
1785 		if (pi->digest.defineds == NULL || pi->digest.digests == NULL)
1786 			return (-1);
1787 		return (0);
1788 	}
1789 
1790 	if (*p != kSize)
1791 		return (-1);
1792 
1793 	if (read_Digests(a, &(pi->digest), (size_t)pi->numPackStreams) < 0)
1794 		return (-1);
1795 
1796 	/*
1797 	 *  Must be marked by kEnd.
1798 	 */
1799 	if ((p = header_bytes(a, 1)) == NULL)
1800 		return (-1);
1801 	if (*p != kEnd)
1802 		return (-1);
1803 	return (0);
1804 }
1805 
1806 static void
1807 free_Folder(struct _7z_folder *f)
1808 {
1809 	unsigned i;
1810 
1811 	if (f->coders) {
1812 		for (i = 0; i< f->numCoders; i++) {
1813 			free(f->coders[i].properties);
1814 		}
1815 		free(f->coders);
1816 	}
1817 	free(f->bindPairs);
1818 	free(f->packedStreams);
1819 	free(f->unPackSize);
1820 }
1821 
1822 static int
1823 read_Folder(struct archive_read *a, struct _7z_folder *f)
1824 {
1825 	struct _7zip *zip = (struct _7zip *)a->format->data;
1826 	const unsigned char *p;
1827 	uint64_t numInStreamsTotal = 0;
1828 	uint64_t numOutStreamsTotal = 0;
1829 	unsigned i;
1830 
1831 	memset(f, 0, sizeof(*f));
1832 
1833 	/*
1834 	 * Read NumCoders.
1835 	 */
1836 	if (parse_7zip_uint64(a, &(f->numCoders)) < 0)
1837 		return (-1);
1838 	if (f->numCoders > 4)
1839 		/* Too many coders. */
1840 		return (-1);
1841 
1842 	f->coders = calloc((size_t)f->numCoders, sizeof(*f->coders));
1843 	if (f->coders == NULL)
1844 		return (-1);
1845 	for (i = 0; i< f->numCoders; i++) {
1846 		size_t codec_size;
1847 		int simple, attr;
1848 
1849 		if ((p = header_bytes(a, 1)) == NULL)
1850 			return (-1);
1851 		/*
1852 		 * 0:3 CodecIdSize
1853 		 * 4:  0 - IsSimple
1854 		 *     1 - Is not Simple
1855 		 * 5:  0 - No Attributes
1856 		 *     1 - There are Attributes;
1857 		 * 7:  Must be zero.
1858 		 */
1859 		codec_size = *p & 0xf;
1860 		simple = (*p & 0x10)?0:1;
1861 		attr = *p & 0x20;
1862 		if (*p & 0x80)
1863 			return (-1);/* Not supported. */
1864 
1865 		/*
1866 		 * Read Decompression Method IDs.
1867 		 */
1868 		if ((p = header_bytes(a, codec_size)) == NULL)
1869 			return (-1);
1870 
1871 		f->coders[i].codec = decode_codec_id(p, codec_size);
1872 
1873 		if (simple) {
1874 			f->coders[i].numInStreams = 1;
1875 			f->coders[i].numOutStreams = 1;
1876 		} else {
1877 			if (parse_7zip_uint64(
1878 			    a, &(f->coders[i].numInStreams)) < 0)
1879 				return (-1);
1880 			if (UMAX_ENTRY < f->coders[i].numInStreams)
1881 				return (-1);
1882 			if (parse_7zip_uint64(
1883 			    a, &(f->coders[i].numOutStreams)) < 0)
1884 				return (-1);
1885 			if (UMAX_ENTRY < f->coders[i].numOutStreams)
1886 				return (-1);
1887 		}
1888 
1889 		if (attr) {
1890 			if (parse_7zip_uint64(
1891 			    a, &(f->coders[i].propertiesSize)) < 0)
1892 				return (-1);
1893 			if ((p = header_bytes(
1894 			    a, (size_t)f->coders[i].propertiesSize)) == NULL)
1895 				return (-1);
1896 			f->coders[i].properties =
1897 			    malloc((size_t)f->coders[i].propertiesSize);
1898 			if (f->coders[i].properties == NULL)
1899 				return (-1);
1900 			memcpy(f->coders[i].properties, p,
1901 			    (size_t)f->coders[i].propertiesSize);
1902 		}
1903 
1904 		numInStreamsTotal += f->coders[i].numInStreams;
1905 		numOutStreamsTotal += f->coders[i].numOutStreams;
1906 	}
1907 
1908 	if (numOutStreamsTotal == 0 ||
1909 	    numInStreamsTotal < numOutStreamsTotal-1)
1910 		return (-1);
1911 
1912 	f->numBindPairs = numOutStreamsTotal - 1;
1913 	if (zip->header_bytes_remaining < f->numBindPairs)
1914 			return (-1);
1915 	if (f->numBindPairs > 0) {
1916 		f->bindPairs =
1917 			calloc((size_t)f->numBindPairs, sizeof(*f->bindPairs));
1918 		if (f->bindPairs == NULL)
1919 			return (-1);
1920 	} else
1921 		f->bindPairs = NULL;
1922 	for (i = 0; i < f->numBindPairs; i++) {
1923 		if (parse_7zip_uint64(a, &(f->bindPairs[i].inIndex)) < 0)
1924 			return (-1);
1925 		if (UMAX_ENTRY < f->bindPairs[i].inIndex)
1926 			return (-1);
1927 		if (parse_7zip_uint64(a, &(f->bindPairs[i].outIndex)) < 0)
1928 			return (-1);
1929 		if (UMAX_ENTRY < f->bindPairs[i].outIndex)
1930 			return (-1);
1931 	}
1932 
1933 	f->numPackedStreams = numInStreamsTotal - f->numBindPairs;
1934 	f->packedStreams =
1935 	    calloc((size_t)f->numPackedStreams, sizeof(*f->packedStreams));
1936 	if (f->packedStreams == NULL)
1937 		return (-1);
1938 	if (f->numPackedStreams == 1) {
1939 		for (i = 0; i < numInStreamsTotal; i++) {
1940 			unsigned j;
1941 			for (j = 0; j < f->numBindPairs; j++) {
1942 				if (f->bindPairs[j].inIndex == i)
1943 					break;
1944 			}
1945 			if (j == f->numBindPairs)
1946 				break;
1947 		}
1948 		if (i == numInStreamsTotal)
1949 			return (-1);
1950 		f->packedStreams[0] = i;
1951 	} else {
1952 		for (i = 0; i < f->numPackedStreams; i++) {
1953 			if (parse_7zip_uint64(a, &(f->packedStreams[i])) < 0)
1954 				return (-1);
1955 			if (UMAX_ENTRY < f->packedStreams[i])
1956 				return (-1);
1957 		}
1958 	}
1959 	f->numInStreams = numInStreamsTotal;
1960 	f->numOutStreams = numOutStreamsTotal;
1961 
1962 	return (0);
1963 }
1964 
1965 static void
1966 free_CodersInfo(struct _7z_coders_info *ci)
1967 {
1968 	unsigned i;
1969 
1970 	if (ci->folders) {
1971 		for (i = 0; i < ci->numFolders; i++)
1972 			free_Folder(&(ci->folders[i]));
1973 		free(ci->folders);
1974 	}
1975 }
1976 
1977 static int
1978 read_CodersInfo(struct archive_read *a, struct _7z_coders_info *ci)
1979 {
1980 	const unsigned char *p;
1981 	struct _7z_digests digest;
1982 	unsigned i;
1983 
1984 	memset(ci, 0, sizeof(*ci));
1985 	memset(&digest, 0, sizeof(digest));
1986 
1987 	if ((p = header_bytes(a, 1)) == NULL)
1988 		goto failed;
1989 	if (*p != kFolder)
1990 		goto failed;
1991 
1992 	/*
1993 	 * Read NumFolders.
1994 	 */
1995 	if (parse_7zip_uint64(a, &(ci->numFolders)) < 0)
1996 		goto failed;
1997 	if (UMAX_ENTRY < ci->numFolders)
1998 		return (-1);
1999 
2000 	/*
2001 	 * Read External.
2002 	 */
2003 	if ((p = header_bytes(a, 1)) == NULL)
2004 		goto failed;
2005 	switch (*p) {
2006 	case 0:
2007 		ci->folders =
2008 			calloc((size_t)ci->numFolders, sizeof(*ci->folders));
2009 		if (ci->folders == NULL)
2010 			return (-1);
2011 		for (i = 0; i < ci->numFolders; i++) {
2012 			if (read_Folder(a, &(ci->folders[i])) < 0)
2013 				goto failed;
2014 		}
2015 		break;
2016 	case 1:
2017 		if (parse_7zip_uint64(a, &(ci->dataStreamIndex)) < 0)
2018 			return (-1);
2019 		if (UMAX_ENTRY < ci->dataStreamIndex)
2020 			return (-1);
2021 		if (ci->numFolders > 0) {
2022 			archive_set_error(&a->archive, -1,
2023 			    "Malformed 7-Zip archive");
2024 			goto failed;
2025 		}
2026 		break;
2027 	default:
2028 		archive_set_error(&a->archive, -1,
2029 		    "Malformed 7-Zip archive");
2030 		goto failed;
2031 	}
2032 
2033 	if ((p = header_bytes(a, 1)) == NULL)
2034 		goto failed;
2035 	if (*p != kCodersUnPackSize)
2036 		goto failed;
2037 
2038 	for (i = 0; i < ci->numFolders; i++) {
2039 		struct _7z_folder *folder = &(ci->folders[i]);
2040 		unsigned j;
2041 
2042 		folder->unPackSize =
2043 		    calloc((size_t)folder->numOutStreams, sizeof(*folder->unPackSize));
2044 		if (folder->unPackSize == NULL)
2045 			goto failed;
2046 		for (j = 0; j < folder->numOutStreams; j++) {
2047 			if (parse_7zip_uint64(a, &(folder->unPackSize[j])) < 0)
2048 				goto failed;
2049 		}
2050 	}
2051 
2052 	/*
2053 	 * Read CRCs.
2054 	 */
2055 	if ((p = header_bytes(a, 1)) == NULL)
2056 		goto failed;
2057 	if (*p == kEnd)
2058 		return (0);
2059 	if (*p != kCRC)
2060 		goto failed;
2061 	if (read_Digests(a, &digest, (size_t)ci->numFolders) < 0)
2062 		goto failed;
2063 	for (i = 0; i < ci->numFolders; i++) {
2064 		ci->folders[i].digest_defined = digest.defineds[i];
2065 		ci->folders[i].digest = digest.digests[i];
2066 	}
2067 
2068 	/*
2069 	 *  Must be kEnd.
2070 	 */
2071 	if ((p = header_bytes(a, 1)) == NULL)
2072 		goto failed;
2073 	if (*p != kEnd)
2074 		goto failed;
2075 	free_Digest(&digest);
2076 	return (0);
2077 failed:
2078 	free_Digest(&digest);
2079 	return (-1);
2080 }
2081 
2082 static uint64_t
2083 folder_uncompressed_size(struct _7z_folder *f)
2084 {
2085 	int n = (int)f->numOutStreams;
2086 	unsigned pairs = (unsigned)f->numBindPairs;
2087 
2088 	while (--n >= 0) {
2089 		unsigned i;
2090 		for (i = 0; i < pairs; i++) {
2091 			if (f->bindPairs[i].outIndex == (uint64_t)n)
2092 				break;
2093 		}
2094 		if (i >= pairs)
2095 			return (f->unPackSize[n]);
2096 	}
2097 	return (0);
2098 }
2099 
2100 static void
2101 free_SubStreamsInfo(struct _7z_substream_info *ss)
2102 {
2103 	free(ss->unpackSizes);
2104 	free(ss->digestsDefined);
2105 	free(ss->digests);
2106 }
2107 
2108 static int
2109 read_SubStreamsInfo(struct archive_read *a, struct _7z_substream_info *ss,
2110     struct _7z_folder *f, size_t numFolders)
2111 {
2112 	const unsigned char *p;
2113 	uint64_t *usizes;
2114 	size_t unpack_streams;
2115 	int type;
2116 	unsigned i;
2117 	uint32_t numDigests;
2118 
2119 	memset(ss, 0, sizeof(*ss));
2120 
2121 	for (i = 0; i < numFolders; i++)
2122 		f[i].numUnpackStreams = 1;
2123 
2124 	if ((p = header_bytes(a, 1)) == NULL)
2125 		return (-1);
2126 	type = *p;
2127 
2128 	if (type == kNumUnPackStream) {
2129 		unpack_streams = 0;
2130 		for (i = 0; i < numFolders; i++) {
2131 			if (parse_7zip_uint64(a, &(f[i].numUnpackStreams)) < 0)
2132 				return (-1);
2133 			if (UMAX_ENTRY < f[i].numUnpackStreams)
2134 				return (-1);
2135 			if (unpack_streams > SIZE_MAX - UMAX_ENTRY) {
2136 				return (-1);
2137 			}
2138 			unpack_streams += (size_t)f[i].numUnpackStreams;
2139 		}
2140 		if ((p = header_bytes(a, 1)) == NULL)
2141 			return (-1);
2142 		type = *p;
2143 	} else
2144 		unpack_streams = numFolders;
2145 
2146 	ss->unpack_streams = unpack_streams;
2147 	if (unpack_streams) {
2148 		ss->unpackSizes = calloc(unpack_streams,
2149 		    sizeof(*ss->unpackSizes));
2150 		ss->digestsDefined = calloc(unpack_streams,
2151 		    sizeof(*ss->digestsDefined));
2152 		ss->digests = calloc(unpack_streams,
2153 		    sizeof(*ss->digests));
2154 		if (ss->unpackSizes == NULL || ss->digestsDefined == NULL ||
2155 		    ss->digests == NULL)
2156 			return (-1);
2157 	}
2158 
2159 	usizes = ss->unpackSizes;
2160 	for (i = 0; i < numFolders; i++) {
2161 		unsigned pack;
2162 		uint64_t sum;
2163 
2164 		if (f[i].numUnpackStreams == 0)
2165 			continue;
2166 
2167 		sum = 0;
2168 		if (type == kSize) {
2169 			for (pack = 1; pack < f[i].numUnpackStreams; pack++) {
2170 				if (parse_7zip_uint64(a, usizes) < 0)
2171 					return (-1);
2172 				sum += *usizes++;
2173 			}
2174 		}
2175 		*usizes++ = folder_uncompressed_size(&f[i]) - sum;
2176 	}
2177 
2178 	if (type == kSize) {
2179 		if ((p = header_bytes(a, 1)) == NULL)
2180 			return (-1);
2181 		type = *p;
2182 	}
2183 
2184 	for (i = 0; i < unpack_streams; i++) {
2185 		ss->digestsDefined[i] = 0;
2186 		ss->digests[i] = 0;
2187 	}
2188 
2189 	numDigests = 0;
2190 	for (i = 0; i < numFolders; i++) {
2191 		if (f[i].numUnpackStreams != 1 || !f[i].digest_defined)
2192 			numDigests += (uint32_t)f[i].numUnpackStreams;
2193 	}
2194 
2195 	if (type == kCRC) {
2196 		struct _7z_digests tmpDigests;
2197 		unsigned char *digestsDefined = ss->digestsDefined;
2198 		uint32_t * digests = ss->digests;
2199 		int di = 0;
2200 
2201 		memset(&tmpDigests, 0, sizeof(tmpDigests));
2202 		if (read_Digests(a, &(tmpDigests), numDigests) < 0) {
2203 			free_Digest(&tmpDigests);
2204 			return (-1);
2205 		}
2206 		for (i = 0; i < numFolders; i++) {
2207 			if (f[i].numUnpackStreams == 1 && f[i].digest_defined) {
2208 				*digestsDefined++ = 1;
2209 				*digests++ = f[i].digest;
2210 			} else {
2211 				unsigned j;
2212 
2213 				for (j = 0; j < f[i].numUnpackStreams;
2214 				    j++, di++) {
2215 					*digestsDefined++ =
2216 					    tmpDigests.defineds[di];
2217 					*digests++ =
2218 					    tmpDigests.digests[di];
2219 				}
2220 			}
2221 		}
2222 		free_Digest(&tmpDigests);
2223 		if ((p = header_bytes(a, 1)) == NULL)
2224 			return (-1);
2225 		type = *p;
2226 	}
2227 
2228 	/*
2229 	 *  Must be kEnd.
2230 	 */
2231 	if (type != kEnd)
2232 		return (-1);
2233 	return (0);
2234 }
2235 
2236 static void
2237 free_StreamsInfo(struct _7z_stream_info *si)
2238 {
2239 	free_PackInfo(&(si->pi));
2240 	free_CodersInfo(&(si->ci));
2241 	free_SubStreamsInfo(&(si->ss));
2242 }
2243 
2244 static int
2245 read_StreamsInfo(struct archive_read *a, struct _7z_stream_info *si)
2246 {
2247 	struct _7zip *zip = (struct _7zip *)a->format->data;
2248 	const unsigned char *p;
2249 	unsigned i;
2250 
2251 	memset(si, 0, sizeof(*si));
2252 
2253 	if ((p = header_bytes(a, 1)) == NULL)
2254 		return (-1);
2255 	if (*p == kPackInfo) {
2256 		uint64_t packPos;
2257 
2258 		if (read_PackInfo(a, &(si->pi)) < 0)
2259 			return (-1);
2260 
2261 		if (si->pi.positions == NULL || si->pi.sizes == NULL)
2262 			return (-1);
2263 		/*
2264 		 * Calculate packed stream positions.
2265 		 */
2266 		packPos = si->pi.pos;
2267 		for (i = 0; i < si->pi.numPackStreams; i++) {
2268 			si->pi.positions[i] = packPos;
2269 			packPos += si->pi.sizes[i];
2270 			if (packPos > zip->header_offset)
2271 				return (-1);
2272 		}
2273 		if ((p = header_bytes(a, 1)) == NULL)
2274 			return (-1);
2275 	}
2276 	if (*p == kUnPackInfo) {
2277 		uint32_t packIndex;
2278 		struct _7z_folder *f;
2279 
2280 		if (read_CodersInfo(a, &(si->ci)) < 0)
2281 			return (-1);
2282 
2283 		/*
2284 		 * Calculate packed stream indexes.
2285 		 */
2286 		packIndex = 0;
2287 		f = si->ci.folders;
2288 		for (i = 0; i < si->ci.numFolders; i++) {
2289 			f[i].packIndex = packIndex;
2290 			packIndex += (uint32_t)f[i].numPackedStreams;
2291 			if (packIndex > si->pi.numPackStreams)
2292 				return (-1);
2293 		}
2294 		if ((p = header_bytes(a, 1)) == NULL)
2295 			return (-1);
2296 	}
2297 
2298 	if (*p == kSubStreamsInfo) {
2299 		if (read_SubStreamsInfo(a, &(si->ss),
2300 		    si->ci.folders, (size_t)si->ci.numFolders) < 0)
2301 			return (-1);
2302 		if ((p = header_bytes(a, 1)) == NULL)
2303 			return (-1);
2304 	}
2305 
2306 	/*
2307 	 *  Must be kEnd.
2308 	 */
2309 	if (*p != kEnd)
2310 		return (-1);
2311 	return (0);
2312 }
2313 
2314 static void
2315 free_Header(struct _7z_header_info *h)
2316 {
2317 	free(h->emptyStreamBools);
2318 	free(h->emptyFileBools);
2319 	free(h->antiBools);
2320 	free(h->attrBools);
2321 }
2322 
2323 static int
2324 read_Header(struct archive_read *a, struct _7z_header_info *h,
2325     int check_header_id)
2326 {
2327 	struct _7zip *zip = (struct _7zip *)a->format->data;
2328 	const unsigned char *p;
2329 	struct _7z_folder *folders;
2330 	struct _7z_stream_info *si = &(zip->si);
2331 	struct _7zip_entry *entries;
2332 	uint32_t folderIndex, indexInFolder;
2333 	unsigned i;
2334 	int eindex, empty_streams, sindex;
2335 
2336 	if (check_header_id) {
2337 		/*
2338 		 * Read Header.
2339 		 */
2340 		if ((p = header_bytes(a, 1)) == NULL)
2341 			return (-1);
2342 		if (*p != kHeader)
2343 			return (-1);
2344 	}
2345 
2346 	/*
2347 	 * Read ArchiveProperties.
2348 	 */
2349 	if ((p = header_bytes(a, 1)) == NULL)
2350 		return (-1);
2351 	if (*p == kArchiveProperties) {
2352 		for (;;) {
2353 			uint64_t size;
2354 			if ((p = header_bytes(a, 1)) == NULL)
2355 				return (-1);
2356 			if (*p == 0)
2357 				break;
2358 			if (parse_7zip_uint64(a, &size) < 0)
2359 				return (-1);
2360 		}
2361 		if ((p = header_bytes(a, 1)) == NULL)
2362 			return (-1);
2363 	}
2364 
2365 	/*
2366 	 * Read MainStreamsInfo.
2367 	 */
2368 	if (*p == kMainStreamsInfo) {
2369 		if (read_StreamsInfo(a, &(zip->si)) < 0)
2370 			return (-1);
2371 		if ((p = header_bytes(a, 1)) == NULL)
2372 			return (-1);
2373 	}
2374 	if (*p == kEnd)
2375 		return (0);
2376 
2377 	/*
2378 	 * Read FilesInfo.
2379 	 */
2380 	if (*p != kFilesInfo)
2381 		return (-1);
2382 
2383 	if (parse_7zip_uint64(a, &(zip->numFiles)) < 0)
2384 		return (-1);
2385 	if (UMAX_ENTRY < zip->numFiles)
2386 		return (-1);
2387 
2388 	zip->entries = calloc((size_t)zip->numFiles, sizeof(*zip->entries));
2389 	if (zip->entries == NULL)
2390 		return (-1);
2391 	entries = zip->entries;
2392 
2393 	empty_streams = 0;
2394 	for (;;) {
2395 		int type;
2396 		uint64_t size;
2397 		size_t ll;
2398 
2399 		if ((p = header_bytes(a, 1)) == NULL)
2400 			return (-1);
2401 		type = *p;
2402 		if (type == kEnd)
2403 			break;
2404 
2405 		if (parse_7zip_uint64(a, &size) < 0)
2406 			return (-1);
2407 		if (zip->header_bytes_remaining < size)
2408 			return (-1);
2409 		ll = (size_t)size;
2410 
2411 		switch (type) {
2412 		case kEmptyStream:
2413 			if (h->emptyStreamBools != NULL)
2414 				return (-1);
2415 			h->emptyStreamBools = calloc((size_t)zip->numFiles,
2416 			    sizeof(*h->emptyStreamBools));
2417 			if (h->emptyStreamBools == NULL)
2418 				return (-1);
2419 			if (read_Bools(
2420 			    a, h->emptyStreamBools, (size_t)zip->numFiles) < 0)
2421 				return (-1);
2422 			empty_streams = 0;
2423 			for (i = 0; i < zip->numFiles; i++) {
2424 				if (h->emptyStreamBools[i])
2425 					empty_streams++;
2426 			}
2427 			break;
2428 		case kEmptyFile:
2429 			if (empty_streams <= 0) {
2430 				/* Unexcepted sequence. Skip this. */
2431 				if (header_bytes(a, ll) == NULL)
2432 					return (-1);
2433 				break;
2434 			}
2435 			if (h->emptyFileBools != NULL)
2436 				return (-1);
2437 			h->emptyFileBools = calloc(empty_streams,
2438 			    sizeof(*h->emptyFileBools));
2439 			if (h->emptyFileBools == NULL)
2440 				return (-1);
2441 			if (read_Bools(a, h->emptyFileBools, empty_streams) < 0)
2442 				return (-1);
2443 			break;
2444 		case kAnti:
2445 			if (empty_streams <= 0) {
2446 				/* Unexcepted sequence. Skip this. */
2447 				if (header_bytes(a, ll) == NULL)
2448 					return (-1);
2449 				break;
2450 			}
2451 			if (h->antiBools != NULL)
2452 				return (-1);
2453 			h->antiBools = calloc(empty_streams,
2454 			    sizeof(*h->antiBools));
2455 			if (h->antiBools == NULL)
2456 				return (-1);
2457 			if (read_Bools(a, h->antiBools, empty_streams) < 0)
2458 				return (-1);
2459 			break;
2460 		case kCTime:
2461 		case kATime:
2462 		case kMTime:
2463 			if (read_Times(a, h, type) < 0)
2464 				return (-1);
2465 			break;
2466 		case kName:
2467 		{
2468 			unsigned char *np;
2469 			size_t nl, nb;
2470 
2471 			/* Skip one byte. */
2472 			if ((p = header_bytes(a, 1)) == NULL)
2473 				return (-1);
2474 			ll--;
2475 
2476 			if ((ll & 1) || ll < zip->numFiles * 4)
2477 				return (-1);
2478 
2479 			if (zip->entry_names != NULL)
2480 				return (-1);
2481 			zip->entry_names = malloc(ll);
2482 			if (zip->entry_names == NULL)
2483 				return (-1);
2484 			np = zip->entry_names;
2485 			nb = ll;
2486 			/*
2487 			 * Copy whole file names.
2488 			 * NOTE: This loop prevents from expanding
2489 			 * the uncompressed buffer in order not to
2490 			 * use extra memory resource.
2491 			 */
2492 			while (nb) {
2493 				size_t b;
2494 				if (nb > UBUFF_SIZE)
2495 					b = UBUFF_SIZE;
2496 				else
2497 					b = nb;
2498 				if ((p = header_bytes(a, b)) == NULL)
2499 					return (-1);
2500 				memcpy(np, p, b);
2501 				np += b;
2502 				nb -= b;
2503 			}
2504 			np = zip->entry_names;
2505 			nl = ll;
2506 
2507 			for (i = 0; i < zip->numFiles; i++) {
2508 				entries[i].utf16name = np;
2509 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2510 				entries[i].wname = (wchar_t *)np;
2511 #endif
2512 
2513 				/* Find a terminator. */
2514 				while (nl >= 2 && (np[0] || np[1])) {
2515 					np += 2;
2516 					nl -= 2;
2517 				}
2518 				if (nl < 2)
2519 					return (-1);/* Terminator not found */
2520 				entries[i].name_len = np - entries[i].utf16name;
2521 				np += 2;
2522 				nl -= 2;
2523 			}
2524 			break;
2525 		}
2526 		case kAttributes:
2527 		{
2528 			int allAreDefined;
2529 
2530 			if ((p = header_bytes(a, 2)) == NULL)
2531 				return (-1);
2532 			allAreDefined = *p;
2533 			if (h->attrBools != NULL)
2534 				return (-1);
2535 			h->attrBools = calloc((size_t)zip->numFiles,
2536 			    sizeof(*h->attrBools));
2537 			if (h->attrBools == NULL)
2538 				return (-1);
2539 			if (allAreDefined)
2540 				memset(h->attrBools, 1, (size_t)zip->numFiles);
2541 			else {
2542 				if (read_Bools(a, h->attrBools,
2543 				      (size_t)zip->numFiles) < 0)
2544 					return (-1);
2545 			}
2546 			for (i = 0; i < zip->numFiles; i++) {
2547 				if (h->attrBools[i]) {
2548 					if ((p = header_bytes(a, 4)) == NULL)
2549 						return (-1);
2550 					entries[i].attr = archive_le32dec(p);
2551 				}
2552 			}
2553 			break;
2554 		}
2555 		case kDummy:
2556 			if (ll == 0)
2557 				break;
2558 			__LA_FALLTHROUGH;
2559 		default:
2560 			if (header_bytes(a, ll) == NULL)
2561 				return (-1);
2562 			break;
2563 		}
2564 	}
2565 
2566 	/*
2567 	 * Set up entry's attributes.
2568 	 */
2569 	folders = si->ci.folders;
2570 	eindex = sindex = 0;
2571 	folderIndex = indexInFolder = 0;
2572 	for (i = 0; i < zip->numFiles; i++) {
2573 		if (h->emptyStreamBools == NULL || h->emptyStreamBools[i] == 0)
2574 			entries[i].flg |= HAS_STREAM;
2575 		/* The high 16 bits of attributes is a posix file mode. */
2576 		entries[i].mode = entries[i].attr >> 16;
2577 		if (entries[i].flg & HAS_STREAM) {
2578 			if ((size_t)sindex >= si->ss.unpack_streams)
2579 				return (-1);
2580 			if (entries[i].mode == 0)
2581 				entries[i].mode = AE_IFREG | 0666;
2582 			if (si->ss.digestsDefined[sindex])
2583 				entries[i].flg |= CRC32_IS_SET;
2584 			entries[i].ssIndex = sindex;
2585 			sindex++;
2586 		} else {
2587 			int dir;
2588 			if (h->emptyFileBools == NULL)
2589 				dir = 1;
2590 			else {
2591 				if (h->emptyFileBools[eindex])
2592 					dir = 0;
2593 				else
2594 					dir = 1;
2595 				eindex++;
2596 			}
2597 			if (entries[i].mode == 0) {
2598 				if (dir)
2599 					entries[i].mode = AE_IFDIR | 0777;
2600 				else
2601 					entries[i].mode = AE_IFREG | 0666;
2602 			} else if (dir &&
2603 			    (entries[i].mode & AE_IFMT) != AE_IFDIR) {
2604 				entries[i].mode &= ~AE_IFMT;
2605 				entries[i].mode |= AE_IFDIR;
2606 			}
2607 			if ((entries[i].mode & AE_IFMT) == AE_IFDIR &&
2608 			    entries[i].name_len >= 2 &&
2609 			    (entries[i].utf16name[entries[i].name_len-2] != '/' ||
2610 			     entries[i].utf16name[entries[i].name_len-1] != 0)) {
2611 				entries[i].utf16name[entries[i].name_len] = '/';
2612 				entries[i].utf16name[entries[i].name_len+1] = 0;
2613 				entries[i].name_len += 2;
2614 			}
2615 			entries[i].ssIndex = -1;
2616 		}
2617 		if (entries[i].attr & 0x01)
2618 			entries[i].mode &= ~0222;/* Read only. */
2619 
2620 		if ((entries[i].flg & HAS_STREAM) == 0 && indexInFolder == 0) {
2621 			/*
2622 			 * The entry is an empty file or a directory file,
2623 			 * those both have no contents.
2624 			 */
2625 			entries[i].folderIndex = -1;
2626 			continue;
2627 		}
2628 		if (indexInFolder == 0) {
2629 			for (;;) {
2630 				if (folderIndex >= si->ci.numFolders)
2631 					return (-1);
2632 				if (folders[folderIndex].numUnpackStreams)
2633 					break;
2634 				folderIndex++;
2635 			}
2636 		}
2637 		entries[i].folderIndex = folderIndex;
2638 		if ((entries[i].flg & HAS_STREAM) == 0)
2639 			continue;
2640 		indexInFolder++;
2641 		if (indexInFolder >= folders[folderIndex].numUnpackStreams) {
2642 			folderIndex++;
2643 			indexInFolder = 0;
2644 		}
2645 	}
2646 
2647 	return (0);
2648 }
2649 
2650 #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
2651 static void
2652 fileTimeToUtc(uint64_t fileTime, time_t *timep, long *ns)
2653 {
2654 
2655 	if (fileTime >= EPOC_TIME) {
2656 		fileTime -= EPOC_TIME;
2657 		/* milli seconds base */
2658 		*timep = (time_t)(fileTime / 10000000);
2659 		/* nano seconds base */
2660 		*ns = (long)(fileTime % 10000000) * 100;
2661 	} else {
2662 		*timep = 0;
2663 		*ns = 0;
2664 	}
2665 }
2666 
2667 static int
2668 read_Times(struct archive_read *a, struct _7z_header_info *h, int type)
2669 {
2670 	struct _7zip *zip = (struct _7zip *)a->format->data;
2671 	const unsigned char *p;
2672 	struct _7zip_entry *entries = zip->entries;
2673 	unsigned char *timeBools;
2674 	int allAreDefined;
2675 	unsigned i;
2676 
2677 	timeBools = calloc((size_t)zip->numFiles, sizeof(*timeBools));
2678 	if (timeBools == NULL)
2679 		return (-1);
2680 
2681 	/* Read allAreDefined. */
2682 	if ((p = header_bytes(a, 1)) == NULL)
2683 		goto failed;
2684 	allAreDefined = *p;
2685 	if (allAreDefined)
2686 		memset(timeBools, 1, (size_t)zip->numFiles);
2687 	else {
2688 		if (read_Bools(a, timeBools, (size_t)zip->numFiles) < 0)
2689 			goto failed;
2690 	}
2691 
2692 	/* Read external. */
2693 	if ((p = header_bytes(a, 1)) == NULL)
2694 		goto failed;
2695 	if (*p) {
2696 		if (parse_7zip_uint64(a, &(h->dataIndex)) < 0)
2697 			goto failed;
2698 		if (UMAX_ENTRY < h->dataIndex)
2699 			goto failed;
2700 	}
2701 
2702 	for (i = 0; i < zip->numFiles; i++) {
2703 		if (!timeBools[i])
2704 			continue;
2705 		if ((p = header_bytes(a, 8)) == NULL)
2706 			goto failed;
2707 		switch (type) {
2708 		case kCTime:
2709 			fileTimeToUtc(archive_le64dec(p),
2710 			    &(entries[i].ctime),
2711 			    &(entries[i].ctime_ns));
2712 			entries[i].flg |= CTIME_IS_SET;
2713 			break;
2714 		case kATime:
2715 			fileTimeToUtc(archive_le64dec(p),
2716 			    &(entries[i].atime),
2717 			    &(entries[i].atime_ns));
2718 			entries[i].flg |= ATIME_IS_SET;
2719 			break;
2720 		case kMTime:
2721 			fileTimeToUtc(archive_le64dec(p),
2722 			    &(entries[i].mtime),
2723 			    &(entries[i].mtime_ns));
2724 			entries[i].flg |= MTIME_IS_SET;
2725 			break;
2726 		}
2727 	}
2728 
2729 	free(timeBools);
2730 	return (0);
2731 failed:
2732 	free(timeBools);
2733 	return (-1);
2734 }
2735 
2736 static int
2737 decode_encoded_header_info(struct archive_read *a, struct _7z_stream_info *si)
2738 {
2739 	struct _7zip *zip = (struct _7zip *)a->format->data;
2740 
2741 	errno = 0;
2742 	if (read_StreamsInfo(a, si) < 0) {
2743 		if (errno == ENOMEM)
2744 			archive_set_error(&a->archive, -1,
2745 			    "Couldn't allocate memory");
2746 		else
2747 			archive_set_error(&a->archive, -1,
2748 			    "Malformed 7-Zip archive");
2749 		return (ARCHIVE_FATAL);
2750 	}
2751 
2752 	if (si->pi.numPackStreams == 0 || si->ci.numFolders == 0) {
2753 		archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
2754 		return (ARCHIVE_FATAL);
2755 	}
2756 
2757 	if (zip->header_offset < si->pi.pos + si->pi.sizes[0] ||
2758 	    (int64_t)(si->pi.pos + si->pi.sizes[0]) < 0 ||
2759 	    si->pi.sizes[0] == 0 || (int64_t)si->pi.pos < 0) {
2760 		archive_set_error(&a->archive, -1, "Malformed Header offset");
2761 		return (ARCHIVE_FATAL);
2762 	}
2763 
2764 	return (ARCHIVE_OK);
2765 }
2766 
2767 static const unsigned char *
2768 header_bytes(struct archive_read *a, size_t rbytes)
2769 {
2770 	struct _7zip *zip = (struct _7zip *)a->format->data;
2771 	const unsigned char *p;
2772 
2773 	if (zip->header_bytes_remaining < rbytes)
2774 		return (NULL);
2775 	if (zip->pack_stream_bytes_unconsumed)
2776 		read_consume(a);
2777 
2778 	if (zip->header_is_encoded == 0) {
2779 		p = __archive_read_ahead(a, rbytes, NULL);
2780 		if (p == NULL)
2781 			return (NULL);
2782 		zip->header_bytes_remaining -= rbytes;
2783 		zip->pack_stream_bytes_unconsumed = rbytes;
2784 	} else {
2785 		const void *buff;
2786 		ssize_t bytes;
2787 
2788 		bytes = read_stream(a, &buff, rbytes, rbytes);
2789 		if (bytes <= 0)
2790 			return (NULL);
2791 		zip->header_bytes_remaining -= bytes;
2792 		p = buff;
2793 	}
2794 
2795 	/* Update checksum */
2796 	zip->header_crc32 = crc32(zip->header_crc32, p, (unsigned)rbytes);
2797 	return (p);
2798 }
2799 
2800 static int
2801 slurp_central_directory(struct archive_read *a, struct _7zip *zip,
2802     struct _7z_header_info *header)
2803 {
2804 	const unsigned char *p;
2805 	uint64_t next_header_offset;
2806 	uint64_t next_header_size;
2807 	uint32_t next_header_crc;
2808 	ssize_t bytes_avail;
2809 	int check_header_crc, r;
2810 
2811 	if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
2812 		return (ARCHIVE_FATAL);
2813 
2814 	if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
2815 		/* This is an executable ? Must be self-extracting... */
2816 		r = skip_sfx(a, bytes_avail);
2817 		if (r < ARCHIVE_WARN)
2818 			return (r);
2819 		if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
2820 			return (ARCHIVE_FATAL);
2821 	}
2822 	zip->seek_base += 32;
2823 
2824 	if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0) {
2825 		archive_set_error(&a->archive, -1, "Not 7-Zip archive file");
2826 		return (ARCHIVE_FATAL);
2827 	}
2828 
2829 	/* CRC check. */
2830 	if (crc32(0, (const unsigned char *)p + 12, 20)
2831 	    != archive_le32dec(p + 8)) {
2832 		archive_set_error(&a->archive, -1, "Header CRC error");
2833 		return (ARCHIVE_FATAL);
2834 	}
2835 
2836 	next_header_offset = archive_le64dec(p + 12);
2837 	next_header_size = archive_le64dec(p + 20);
2838 	next_header_crc = archive_le32dec(p + 28);
2839 
2840 	if (next_header_size == 0)
2841 		/* There is no entry in an archive file. */
2842 		return (ARCHIVE_EOF);
2843 
2844 	if (((int64_t)next_header_offset) < 0) {
2845 		archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
2846 		return (ARCHIVE_FATAL);
2847 	}
2848 	__archive_read_consume(a, 32);
2849 	if (next_header_offset != 0) {
2850 		if (bytes_avail >= (ssize_t)next_header_offset)
2851 			__archive_read_consume(a, next_header_offset);
2852 		else if (__archive_read_seek(a,
2853 		    next_header_offset + zip->seek_base, SEEK_SET) < 0)
2854 			return (ARCHIVE_FATAL);
2855 	}
2856 	zip->stream_offset = next_header_offset;
2857 	zip->header_offset = next_header_offset;
2858 	zip->header_bytes_remaining = next_header_size;
2859 	zip->header_crc32 = 0;
2860 	zip->header_is_encoded = 0;
2861 	zip->header_is_being_read = 1;
2862 	zip->has_encrypted_entries = 0;
2863 	check_header_crc = 1;
2864 
2865 	if ((p = header_bytes(a, 1)) == NULL) {
2866 		archive_set_error(&a->archive,
2867 		    ARCHIVE_ERRNO_FILE_FORMAT,
2868 		    "Truncated 7-Zip file body");
2869 		return (ARCHIVE_FATAL);
2870 	}
2871 	/* Parse ArchiveProperties. */
2872 	switch (p[0]) {
2873 	case kEncodedHeader:
2874 		/*
2875 		 * The archive has an encoded header and we have to decode it
2876 		 * in order to parse the header correctly.
2877 		 */
2878 		r = decode_encoded_header_info(a, &(zip->si));
2879 
2880 		/* Check the EncodedHeader CRC.*/
2881 		if (r == 0 && zip->header_crc32 != next_header_crc) {
2882 			archive_set_error(&a->archive, -1,
2883 			    "Damaged 7-Zip archive");
2884 			r = -1;
2885 		}
2886 		if (r == 0) {
2887 			if (zip->si.ci.folders[0].digest_defined)
2888 				next_header_crc = zip->si.ci.folders[0].digest;
2889 			else
2890 				check_header_crc = 0;
2891 			if (zip->pack_stream_bytes_unconsumed)
2892 				read_consume(a);
2893 			r = setup_decode_folder(a, zip->si.ci.folders, 1);
2894 			if (r == 0) {
2895 				zip->header_bytes_remaining =
2896 					zip->folder_outbytes_remaining;
2897 				r = seek_pack(a);
2898 			}
2899 		}
2900 		/* Clean up StreamsInfo. */
2901 		free_StreamsInfo(&(zip->si));
2902 		memset(&(zip->si), 0, sizeof(zip->si));
2903 		if (r < 0)
2904 			return (ARCHIVE_FATAL);
2905 		zip->header_is_encoded = 1;
2906 		zip->header_crc32 = 0;
2907 		/* FALL THROUGH */
2908 	case kHeader:
2909 		/*
2910 		 * Parse the header.
2911 		 */
2912 		errno = 0;
2913 		r = read_Header(a, header, zip->header_is_encoded);
2914 		if (r < 0) {
2915 			if (errno == ENOMEM)
2916 				archive_set_error(&a->archive, -1,
2917 				    "Couldn't allocate memory");
2918 			else
2919 				archive_set_error(&a->archive, -1,
2920 				    "Damaged 7-Zip archive");
2921 			return (ARCHIVE_FATAL);
2922 		}
2923 
2924 		/*
2925 		 *  Must be kEnd.
2926 		 */
2927 		if ((p = header_bytes(a, 1)) == NULL ||*p != kEnd) {
2928 			archive_set_error(&a->archive, -1,
2929 			    "Malformed 7-Zip archive");
2930 			return (ARCHIVE_FATAL);
2931 		}
2932 
2933 		/* Check the Header CRC.*/
2934 		if (check_header_crc && zip->header_crc32 != next_header_crc) {
2935 			archive_set_error(&a->archive, -1,
2936 			    "Malformed 7-Zip archive");
2937 			return (ARCHIVE_FATAL);
2938 		}
2939 		break;
2940 	default:
2941 		archive_set_error(&a->archive, -1,
2942 		    "Unexpected Property ID = %X", p[0]);
2943 		return (ARCHIVE_FATAL);
2944 	}
2945 
2946 	/* Clean up variables be used for decoding the archive header */
2947 	zip->pack_stream_remaining = 0;
2948 	zip->pack_stream_index = 0;
2949 	zip->folder_outbytes_remaining = 0;
2950 	zip->uncompressed_buffer_bytes_remaining = 0;
2951 	zip->pack_stream_bytes_unconsumed = 0;
2952 	zip->header_is_being_read = 0;
2953 
2954 	return (ARCHIVE_OK);
2955 }
2956 
2957 static ssize_t
2958 get_uncompressed_data(struct archive_read *a, const void **buff, size_t size,
2959     size_t minimum)
2960 {
2961 	struct _7zip *zip = (struct _7zip *)a->format->data;
2962 	ssize_t bytes_avail;
2963 
2964 	if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
2965 		/* Copy mode. */
2966 
2967 		/*
2968 		 * Note: '1' here is a performance optimization.
2969 		 * Recall that the decompression layer returns a count of
2970 		 * available bytes; asking for more than that forces the
2971 		 * decompressor to combine reads by copying data.
2972 		 */
2973 		*buff = __archive_read_ahead(a, 1, &bytes_avail);
2974 		if (bytes_avail <= 0) {
2975 			archive_set_error(&a->archive,
2976 			    ARCHIVE_ERRNO_FILE_FORMAT,
2977 			    "Truncated 7-Zip file data");
2978 			return (ARCHIVE_FATAL);
2979 		}
2980 		if ((size_t)bytes_avail >
2981 		    zip->uncompressed_buffer_bytes_remaining)
2982 			bytes_avail = (ssize_t)
2983 			    zip->uncompressed_buffer_bytes_remaining;
2984 		if ((size_t)bytes_avail > size)
2985 			bytes_avail = (ssize_t)size;
2986 
2987 		zip->pack_stream_bytes_unconsumed = bytes_avail;
2988 	} else if (zip->uncompressed_buffer_pointer == NULL) {
2989 		/* Decompression has failed. */
2990 		archive_set_error(&(a->archive),
2991 		    ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
2992 		return (ARCHIVE_FATAL);
2993 	} else {
2994 		/* Packed mode. */
2995 		if (minimum > zip->uncompressed_buffer_bytes_remaining) {
2996 			/*
2997 			 * If remaining uncompressed data size is less than
2998 			 * the minimum size, fill the buffer up to the
2999 			 * minimum size.
3000 			 */
3001 			if (extract_pack_stream(a, minimum) < 0)
3002 				return (ARCHIVE_FATAL);
3003 		}
3004 		if (size > zip->uncompressed_buffer_bytes_remaining)
3005 			bytes_avail = (ssize_t)
3006 			    zip->uncompressed_buffer_bytes_remaining;
3007 		else
3008 			bytes_avail = (ssize_t)size;
3009 		*buff = zip->uncompressed_buffer_pointer;
3010 		zip->uncompressed_buffer_pointer += bytes_avail;
3011 	}
3012 	zip->uncompressed_buffer_bytes_remaining -= bytes_avail;
3013 	return (bytes_avail);
3014 }
3015 
3016 static ssize_t
3017 extract_pack_stream(struct archive_read *a, size_t minimum)
3018 {
3019 	struct _7zip *zip = (struct _7zip *)a->format->data;
3020 	ssize_t bytes_avail;
3021 	int r;
3022 
3023 	if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
3024 		if (minimum == 0)
3025 			minimum = 1;
3026 		if (__archive_read_ahead(a, minimum, &bytes_avail) == NULL
3027 		    || bytes_avail <= 0) {
3028 			archive_set_error(&a->archive,
3029 			    ARCHIVE_ERRNO_FILE_FORMAT,
3030 			    "Truncated 7-Zip file body");
3031 			return (ARCHIVE_FATAL);
3032 		}
3033 		if (bytes_avail > (ssize_t)zip->pack_stream_inbytes_remaining)
3034 			bytes_avail = (ssize_t)zip->pack_stream_inbytes_remaining;
3035 		zip->pack_stream_inbytes_remaining -= bytes_avail;
3036 		if (bytes_avail > (ssize_t)zip->folder_outbytes_remaining)
3037 			bytes_avail = (ssize_t)zip->folder_outbytes_remaining;
3038 		zip->folder_outbytes_remaining -= bytes_avail;
3039 		zip->uncompressed_buffer_bytes_remaining = bytes_avail;
3040 		return (ARCHIVE_OK);
3041 	}
3042 
3043 	/* If the buffer hasn't been allocated, allocate it now. */
3044 	if (zip->uncompressed_buffer == NULL) {
3045 		zip->uncompressed_buffer_size = UBUFF_SIZE;
3046 		if (zip->uncompressed_buffer_size < minimum) {
3047 			zip->uncompressed_buffer_size = minimum + 1023;
3048 			zip->uncompressed_buffer_size &= ~0x3ff;
3049 		}
3050 		zip->uncompressed_buffer =
3051 		    malloc(zip->uncompressed_buffer_size);
3052 		if (zip->uncompressed_buffer == NULL) {
3053 			archive_set_error(&a->archive, ENOMEM,
3054 			    "No memory for 7-Zip decompression");
3055 			return (ARCHIVE_FATAL);
3056 		}
3057 		zip->uncompressed_buffer_bytes_remaining = 0;
3058 	} else if (zip->uncompressed_buffer_size < minimum ||
3059 	    zip->uncompressed_buffer_bytes_remaining < minimum) {
3060 		/*
3061 		 * Make sure the uncompressed buffer can have bytes
3062 		 * at least `minimum' bytes.
3063 		 * NOTE: This case happen when reading the header.
3064 		 */
3065 		size_t used;
3066 		if (zip->uncompressed_buffer_pointer != 0)
3067 			used = zip->uncompressed_buffer_pointer -
3068 				zip->uncompressed_buffer;
3069 		else
3070 			used = 0;
3071 		if (zip->uncompressed_buffer_size < minimum) {
3072 			/*
3073 			 * Expand the uncompressed buffer up to
3074 			 * the minimum size.
3075 			 */
3076 			void *p;
3077 			size_t new_size;
3078 
3079 			new_size = minimum + 1023;
3080 			new_size &= ~0x3ff;
3081 			p = realloc(zip->uncompressed_buffer, new_size);
3082 			if (p == NULL) {
3083 				archive_set_error(&a->archive, ENOMEM,
3084 				    "No memory for 7-Zip decompression");
3085 				return (ARCHIVE_FATAL);
3086 			}
3087 			zip->uncompressed_buffer = (unsigned char *)p;
3088 			zip->uncompressed_buffer_size = new_size;
3089 		}
3090 		/*
3091 		 * Move unconsumed bytes to the head.
3092 		 */
3093 		if (used) {
3094 			memmove(zip->uncompressed_buffer,
3095 				zip->uncompressed_buffer + used,
3096 				zip->uncompressed_buffer_bytes_remaining);
3097 		}
3098 	} else
3099 		zip->uncompressed_buffer_bytes_remaining = 0;
3100 	zip->uncompressed_buffer_pointer = NULL;
3101 	for (;;) {
3102 		size_t bytes_in, bytes_out;
3103 		const void *buff_in;
3104 		unsigned char *buff_out;
3105 		int end_of_data;
3106 
3107 		/*
3108 		 * Note: '1' here is a performance optimization.
3109 		 * Recall that the decompression layer returns a count of
3110 		 * available bytes; asking for more than that forces the
3111 		 * decompressor to combine reads by copying data.
3112 		 */
3113 		buff_in = __archive_read_ahead(a, 1, &bytes_avail);
3114 		if (bytes_avail <= 0) {
3115 			archive_set_error(&a->archive,
3116 			    ARCHIVE_ERRNO_FILE_FORMAT,
3117 			    "Truncated 7-Zip file body");
3118 			return (ARCHIVE_FATAL);
3119 		}
3120 
3121 		buff_out = zip->uncompressed_buffer
3122 			+ zip->uncompressed_buffer_bytes_remaining;
3123 		bytes_out = zip->uncompressed_buffer_size
3124 			- zip->uncompressed_buffer_bytes_remaining;
3125 		bytes_in = bytes_avail;
3126 		if (bytes_in > zip->pack_stream_inbytes_remaining)
3127 			bytes_in = (size_t)zip->pack_stream_inbytes_remaining;
3128 		/* Drive decompression. */
3129 		r = decompress(a, zip, buff_out, &bytes_out,
3130 			buff_in, &bytes_in);
3131 		switch (r) {
3132 		case ARCHIVE_OK:
3133 			end_of_data = 0;
3134 			break;
3135 		case ARCHIVE_EOF:
3136 			end_of_data = 1;
3137 			break;
3138 		default:
3139 			return (ARCHIVE_FATAL);
3140 		}
3141 		zip->pack_stream_inbytes_remaining -= bytes_in;
3142 		if (bytes_out > zip->folder_outbytes_remaining)
3143 			bytes_out = (size_t)zip->folder_outbytes_remaining;
3144 		zip->folder_outbytes_remaining -= bytes_out;
3145 		zip->uncompressed_buffer_bytes_remaining += bytes_out;
3146 		zip->pack_stream_bytes_unconsumed = bytes_in;
3147 
3148 		/*
3149 		 * Continue decompression until uncompressed_buffer is full.
3150 		 */
3151 		if (zip->uncompressed_buffer_bytes_remaining ==
3152 		    zip->uncompressed_buffer_size)
3153 			break;
3154 		if (zip->codec2 == _7Z_X86 && zip->odd_bcj_size &&
3155 		    zip->uncompressed_buffer_bytes_remaining + 5 >
3156 		    zip->uncompressed_buffer_size)
3157 			break;
3158 		if (zip->pack_stream_inbytes_remaining == 0 &&
3159 		    zip->folder_outbytes_remaining == 0)
3160 			break;
3161 		if (end_of_data || (bytes_in == 0 && bytes_out == 0)) {
3162 			archive_set_error(&(a->archive),
3163 			    ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3164 			return (ARCHIVE_FATAL);
3165 		}
3166 		read_consume(a);
3167 	}
3168 	if (zip->uncompressed_buffer_bytes_remaining < minimum) {
3169 		archive_set_error(&(a->archive),
3170 		    ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3171 		return (ARCHIVE_FATAL);
3172 	}
3173 	zip->uncompressed_buffer_pointer = zip->uncompressed_buffer;
3174 	return (ARCHIVE_OK);
3175 }
3176 
3177 static int
3178 seek_pack(struct archive_read *a)
3179 {
3180 	struct _7zip *zip = (struct _7zip *)a->format->data;
3181 	int64_t pack_offset;
3182 
3183 	if (zip->pack_stream_remaining <= 0) {
3184 		archive_set_error(&(a->archive),
3185 		    ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3186 		return (ARCHIVE_FATAL);
3187 	}
3188 	zip->pack_stream_inbytes_remaining =
3189 	    zip->si.pi.sizes[zip->pack_stream_index];
3190 	pack_offset = zip->si.pi.positions[zip->pack_stream_index];
3191 	if (zip->stream_offset != pack_offset) {
3192 		if (0 > __archive_read_seek(a, pack_offset + zip->seek_base,
3193 		    SEEK_SET))
3194 			return (ARCHIVE_FATAL);
3195 		zip->stream_offset = pack_offset;
3196 	}
3197 	zip->pack_stream_index++;
3198 	zip->pack_stream_remaining--;
3199 	return (ARCHIVE_OK);
3200 }
3201 
3202 static ssize_t
3203 read_stream(struct archive_read *a, const void **buff, size_t size,
3204     size_t minimum)
3205 {
3206 	struct _7zip *zip = (struct _7zip *)a->format->data;
3207 	uint64_t skip_bytes = 0;
3208 	ssize_t r;
3209 
3210 	if (zip->uncompressed_buffer_bytes_remaining == 0) {
3211 		if (zip->pack_stream_inbytes_remaining > 0) {
3212 			r = extract_pack_stream(a, 0);
3213 			if (r < 0)
3214 				return (r);
3215 			return (get_uncompressed_data(a, buff, size, minimum));
3216 		} else if (zip->folder_outbytes_remaining > 0) {
3217 			/* Extract a remaining pack stream. */
3218 			r = extract_pack_stream(a, 0);
3219 			if (r < 0)
3220 				return (r);
3221 			return (get_uncompressed_data(a, buff, size, minimum));
3222 		}
3223 	} else
3224 		return (get_uncompressed_data(a, buff, size, minimum));
3225 
3226 	/*
3227 	 * Current pack stream has been consumed.
3228 	 */
3229 	if (zip->pack_stream_remaining == 0) {
3230 		if (zip->header_is_being_read) {
3231 			/* Invalid sequence. This might happen when
3232 			 * reading a malformed archive. */
3233 			archive_set_error(&(a->archive),
3234 			    ARCHIVE_ERRNO_MISC, "Malformed 7-Zip archive");
3235 			return (ARCHIVE_FATAL);
3236 		}
3237 
3238 		/*
3239 		 * All current folder's pack streams have been
3240 		 * consumed. Switch to next folder.
3241 		 */
3242 		if (zip->folder_index == 0 &&
3243 		    (zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
3244 		     || zip->folder_index != zip->entry->folderIndex)) {
3245 			zip->folder_index = zip->entry->folderIndex;
3246 			skip_bytes =
3247 			    zip->si.ci.folders[zip->folder_index].skipped_bytes;
3248 		}
3249 
3250 		if (zip->folder_index >= zip->si.ci.numFolders) {
3251 			/*
3252 			 * We have consumed all folders and its pack streams.
3253 			 */
3254 			*buff = NULL;
3255 			return (0);
3256 		}
3257 		r = setup_decode_folder(a,
3258 			&(zip->si.ci.folders[zip->folder_index]), 0);
3259 		if (r != ARCHIVE_OK)
3260 			return (ARCHIVE_FATAL);
3261 
3262 		zip->folder_index++;
3263 	}
3264 
3265 	/*
3266 	 * Switch to next pack stream.
3267 	 */
3268 	r = seek_pack(a);
3269 	if (r < 0)
3270 		return (r);
3271 
3272 	/* Extract a new pack stream. */
3273 	r = extract_pack_stream(a, 0);
3274 	if (r < 0)
3275 		return (r);
3276 
3277 	/*
3278 	 * Skip the bytes we already has skipped in skip_stream().
3279 	 */
3280 	while (skip_bytes) {
3281 		ssize_t skipped;
3282 
3283 		if (zip->uncompressed_buffer_bytes_remaining == 0) {
3284 			if (zip->pack_stream_inbytes_remaining > 0) {
3285 				r = extract_pack_stream(a, 0);
3286 				if (r < 0)
3287 					return (r);
3288 			} else if (zip->folder_outbytes_remaining > 0) {
3289 				/* Extract a remaining pack stream. */
3290 				r = extract_pack_stream(a, 0);
3291 				if (r < 0)
3292 					return (r);
3293 			} else {
3294 				archive_set_error(&a->archive,
3295 				    ARCHIVE_ERRNO_FILE_FORMAT,
3296 				    "Truncated 7-Zip file body");
3297 				return (ARCHIVE_FATAL);
3298 			}
3299 		}
3300 		skipped = get_uncompressed_data(
3301 			a, buff, (size_t)skip_bytes, 0);
3302 		if (skipped < 0)
3303 			return (skipped);
3304 		skip_bytes -= skipped;
3305 		if (zip->pack_stream_bytes_unconsumed)
3306 			read_consume(a);
3307 	}
3308 
3309 	return (get_uncompressed_data(a, buff, size, minimum));
3310 }
3311 
3312 static int
3313 setup_decode_folder(struct archive_read *a, struct _7z_folder *folder,
3314     int header)
3315 {
3316 	struct _7zip *zip = (struct _7zip *)a->format->data;
3317 	const struct _7z_coder *coder1, *coder2;
3318 	const char *cname = (header)?"archive header":"file content";
3319 	unsigned i;
3320 	int r, found_bcj2 = 0;
3321 
3322 	/*
3323 	 * Release the memory which the previous folder used for BCJ2.
3324 	 */
3325 	for (i = 0; i < 3; i++) {
3326 		if (zip->sub_stream_buff[i] != NULL)
3327 			free(zip->sub_stream_buff[i]);
3328 		zip->sub_stream_buff[i] = NULL;
3329 	}
3330 
3331 	/*
3332 	 * Initialize a stream reader.
3333 	 */
3334 	zip->pack_stream_remaining = (unsigned)folder->numPackedStreams;
3335 	zip->pack_stream_index = (unsigned)folder->packIndex;
3336 	zip->folder_outbytes_remaining = folder_uncompressed_size(folder);
3337 	zip->uncompressed_buffer_bytes_remaining = 0;
3338 
3339 	/*
3340 	 * Check coder types.
3341 	 */
3342 	for (i = 0; i < folder->numCoders; i++) {
3343 		switch(folder->coders[i].codec) {
3344 			case _7Z_CRYPTO_MAIN_ZIP:
3345 			case _7Z_CRYPTO_RAR_29:
3346 			case _7Z_CRYPTO_AES_256_SHA_256: {
3347 				/* For entry that is associated with this folder, mark
3348 				   it as encrypted (data+metadata). */
3349 				zip->has_encrypted_entries = 1;
3350 				if (a->entry) {
3351 					archive_entry_set_is_data_encrypted(a->entry, 1);
3352 					archive_entry_set_is_metadata_encrypted(a->entry, 1);
3353 				}
3354 				archive_set_error(&(a->archive),
3355 					ARCHIVE_ERRNO_MISC,
3356 					"The %s is encrypted, "
3357 					"but currently not supported", cname);
3358 				return (ARCHIVE_FATAL);
3359 			}
3360 			case _7Z_X86_BCJ2: {
3361 				found_bcj2++;
3362 				break;
3363 			}
3364 		}
3365 	}
3366 	/* Now that we've checked for encryption, if there were still no
3367 	 * encrypted entries found we can say for sure that there are none.
3368 	 */
3369 	if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
3370 		zip->has_encrypted_entries = 0;
3371 	}
3372 
3373 	if ((folder->numCoders > 2 && !found_bcj2) || found_bcj2 > 1) {
3374 		archive_set_error(&(a->archive),
3375 		    ARCHIVE_ERRNO_MISC,
3376 		    "The %s is encoded with many filters, "
3377 		    "but currently not supported", cname);
3378 		return (ARCHIVE_FATAL);
3379 	}
3380 	coder1 = &(folder->coders[0]);
3381 	if (folder->numCoders == 2)
3382 		coder2 = &(folder->coders[1]);
3383 	else
3384 		coder2 = NULL;
3385 
3386 	if (found_bcj2) {
3387 		/*
3388 		 * Preparation to decode BCJ2.
3389 		 * Decoding BCJ2 requires four sources. Those are at least,
3390 		 * as far as I know, two types of the storage form.
3391 		 */
3392 		const struct _7z_coder *fc = folder->coders;
3393 		static const struct _7z_coder coder_copy = {0, 1, 1, 0, NULL};
3394 		const struct _7z_coder *scoder[3] =
3395 			{&coder_copy, &coder_copy, &coder_copy};
3396 		const void *buff;
3397 		ssize_t bytes;
3398 		unsigned char *b[3] = {NULL, NULL, NULL};
3399 		uint64_t sunpack[3] ={-1, -1, -1};
3400 		size_t s[3] = {0, 0, 0};
3401 		int idx[3] = {0, 1, 2};
3402 
3403 		if (folder->numCoders == 4 && fc[3].codec == _7Z_X86_BCJ2 &&
3404 		    folder->numInStreams == 7 && folder->numOutStreams == 4 &&
3405 		    zip->pack_stream_remaining == 4) {
3406 			/* Source type 1 made by 7zr or 7z with -m options. */
3407 			if (folder->bindPairs[0].inIndex == 5) {
3408 				/* The form made by 7zr */
3409 				idx[0] = 1; idx[1] = 2; idx[2] = 0;
3410 				scoder[1] = &(fc[1]);
3411 				scoder[2] = &(fc[0]);
3412 				sunpack[1] = folder->unPackSize[1];
3413 				sunpack[2] = folder->unPackSize[0];
3414 				coder1 = &(fc[2]);
3415 			} else {
3416 				/*
3417 				 * NOTE: Some patterns do not work.
3418 				 * work:
3419 				 *  7z a -m0=BCJ2 -m1=COPY -m2=COPY
3420 				 *       -m3=(any)
3421 				 *  7z a -m0=BCJ2 -m1=COPY -m2=(any)
3422 				 *       -m3=COPY
3423 				 *  7z a -m0=BCJ2 -m1=(any) -m2=COPY
3424 				 *       -m3=COPY
3425 				 * not work:
3426 				 *  other patterns.
3427 				 *
3428 				 * We have to handle this like `pipe' or
3429 				 * our libarchive7s filter frame work,
3430 				 * decoding the BCJ2 main stream sequentially,
3431 				 * m3 -> m2 -> m1 -> BCJ2.
3432 				 *
3433 				 */
3434 				if (fc[0].codec == _7Z_COPY &&
3435 				    fc[1].codec == _7Z_COPY)
3436 					coder1 = &(folder->coders[2]);
3437 				else if (fc[0].codec == _7Z_COPY &&
3438 				    fc[2].codec == _7Z_COPY)
3439 					coder1 = &(folder->coders[1]);
3440 				else if (fc[1].codec == _7Z_COPY &&
3441 				    fc[2].codec == _7Z_COPY)
3442 					coder1 = &(folder->coders[0]);
3443 				else {
3444 					archive_set_error(&(a->archive),
3445 					    ARCHIVE_ERRNO_MISC,
3446 					    "Unsupported form of "
3447 					    "BCJ2 streams");
3448 					return (ARCHIVE_FATAL);
3449 				}
3450 			}
3451 			coder2 = &(fc[3]);
3452 			zip->main_stream_bytes_remaining =
3453 				(size_t)folder->unPackSize[2];
3454 		} else if (coder2 != NULL && coder2->codec == _7Z_X86_BCJ2 &&
3455 		    zip->pack_stream_remaining == 4 &&
3456 		    folder->numInStreams == 5 && folder->numOutStreams == 2) {
3457 			/* Source type 0 made by 7z */
3458 			zip->main_stream_bytes_remaining =
3459 				(size_t)folder->unPackSize[0];
3460 		} else {
3461 			/* We got an unexpected form. */
3462 			archive_set_error(&(a->archive),
3463 			    ARCHIVE_ERRNO_MISC,
3464 			    "Unsupported form of BCJ2 streams");
3465 			return (ARCHIVE_FATAL);
3466 		}
3467 
3468 		/* Skip the main stream at this time. */
3469 		if ((r = seek_pack(a)) < 0)
3470 			return (r);
3471 		zip->pack_stream_bytes_unconsumed =
3472 		    (size_t)zip->pack_stream_inbytes_remaining;
3473 		read_consume(a);
3474 
3475 		/* Read following three sub streams. */
3476 		for (i = 0; i < 3; i++) {
3477 			const struct _7z_coder *coder = scoder[i];
3478 
3479 			if ((r = seek_pack(a)) < 0) {
3480 				free(b[0]); free(b[1]); free(b[2]);
3481 				return (r);
3482 			}
3483 
3484 			if (sunpack[i] == (uint64_t)-1)
3485 				zip->folder_outbytes_remaining =
3486 				    zip->pack_stream_inbytes_remaining;
3487 			else
3488 				zip->folder_outbytes_remaining = sunpack[i];
3489 
3490 			r = init_decompression(a, zip, coder, NULL);
3491 			if (r != ARCHIVE_OK) {
3492 				free(b[0]); free(b[1]); free(b[2]);
3493 				return (ARCHIVE_FATAL);
3494 			}
3495 
3496 			/* Allocate memory for the decoded data of a sub
3497 			 * stream. */
3498 			b[i] = malloc((size_t)zip->folder_outbytes_remaining);
3499 			if (b[i] == NULL) {
3500 				free(b[0]); free(b[1]); free(b[2]);
3501 				archive_set_error(&a->archive, ENOMEM,
3502 				    "No memory for 7-Zip decompression");
3503 				return (ARCHIVE_FATAL);
3504 			}
3505 
3506 			/* Extract a sub stream. */
3507 			while (zip->pack_stream_inbytes_remaining > 0) {
3508 				r = (int)extract_pack_stream(a, 0);
3509 				if (r < 0) {
3510 					free(b[0]); free(b[1]); free(b[2]);
3511 					return (r);
3512 				}
3513 				bytes = get_uncompressed_data(a, &buff,
3514 				    zip->uncompressed_buffer_bytes_remaining,
3515 				    0);
3516 				if (bytes < 0) {
3517 					free(b[0]); free(b[1]); free(b[2]);
3518 					return ((int)bytes);
3519 				}
3520 				memcpy(b[i]+s[i], buff, bytes);
3521 				s[i] += bytes;
3522 				if (zip->pack_stream_bytes_unconsumed)
3523 					read_consume(a);
3524 			}
3525 		}
3526 
3527 		/* Set the sub streams to the right place. */
3528 		for (i = 0; i < 3; i++) {
3529 			zip->sub_stream_buff[i] = b[idx[i]];
3530 			zip->sub_stream_size[i] = s[idx[i]];
3531 			zip->sub_stream_bytes_remaining[i] = s[idx[i]];
3532 		}
3533 
3534 		/* Allocate memory used for decoded main stream bytes. */
3535 		if (zip->tmp_stream_buff == NULL) {
3536 			zip->tmp_stream_buff_size = 32 * 1024;
3537 			zip->tmp_stream_buff =
3538 			    malloc(zip->tmp_stream_buff_size);
3539 			if (zip->tmp_stream_buff == NULL) {
3540 				archive_set_error(&a->archive, ENOMEM,
3541 				    "No memory for 7-Zip decompression");
3542 				return (ARCHIVE_FATAL);
3543 			}
3544 		}
3545 		zip->tmp_stream_bytes_avail = 0;
3546 		zip->tmp_stream_bytes_remaining = 0;
3547 		zip->odd_bcj_size = 0;
3548 		zip->bcj2_outPos = 0;
3549 
3550 		/*
3551 		 * Reset a stream reader in order to read the main stream
3552 		 * of BCJ2.
3553 		 */
3554 		zip->pack_stream_remaining = 1;
3555 		zip->pack_stream_index = (unsigned)folder->packIndex;
3556 		zip->folder_outbytes_remaining =
3557 		    folder_uncompressed_size(folder);
3558 		zip->uncompressed_buffer_bytes_remaining = 0;
3559 	}
3560 
3561 	/*
3562 	 * Initialize the decompressor for the new folder's pack streams.
3563 	 */
3564 	r = init_decompression(a, zip, coder1, coder2);
3565 	if (r != ARCHIVE_OK)
3566 		return (ARCHIVE_FATAL);
3567 	return (ARCHIVE_OK);
3568 }
3569 
3570 static int64_t
3571 skip_stream(struct archive_read *a, size_t skip_bytes)
3572 {
3573 	struct _7zip *zip = (struct _7zip *)a->format->data;
3574 	const void *p;
3575 	int64_t skipped_bytes;
3576 	size_t bytes = skip_bytes;
3577 
3578 	if (zip->folder_index == 0) {
3579 		/*
3580 		 * Optimization for a list mode.
3581 		 * Avoid unnecessary decoding operations.
3582 		 */
3583 		zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
3584 		    += skip_bytes;
3585 		return (skip_bytes);
3586 	}
3587 
3588 	while (bytes) {
3589 		skipped_bytes = read_stream(a, &p, bytes, 0);
3590 		if (skipped_bytes < 0)
3591 			return (skipped_bytes);
3592 		if (skipped_bytes == 0) {
3593 			archive_set_error(&a->archive,
3594 			    ARCHIVE_ERRNO_FILE_FORMAT,
3595 			    "Truncated 7-Zip file body");
3596 			return (ARCHIVE_FATAL);
3597 		}
3598 		bytes -= (size_t)skipped_bytes;
3599 		if (zip->pack_stream_bytes_unconsumed)
3600 			read_consume(a);
3601 	}
3602 	return (skip_bytes);
3603 }
3604 
3605 /*
3606  * Brought from LZMA SDK.
3607  *
3608  * Bra86.c -- Converter for x86 code (BCJ)
3609  * 2008-10-04 : Igor Pavlov : Public domain
3610  *
3611  */
3612 
3613 #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
3614 
3615 static void
3616 x86_Init(struct _7zip *zip)
3617 {
3618 	zip->bcj_state = 0;
3619 	zip->bcj_prevPosT = (size_t)0 - 1;
3620 	zip->bcj_prevMask = 0;
3621 	zip->bcj_ip = 5;
3622 }
3623 
3624 static size_t
3625 x86_Convert(struct _7zip *zip, uint8_t *data, size_t size)
3626 {
3627 	static const uint8_t kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
3628 	static const uint8_t kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
3629 	size_t bufferPos, prevPosT;
3630 	uint32_t ip, prevMask;
3631 
3632 	if (size < 5)
3633 		return 0;
3634 
3635 	bufferPos = 0;
3636 	prevPosT = zip->bcj_prevPosT;
3637 	prevMask = zip->bcj_prevMask;
3638 	ip = zip->bcj_ip;
3639 
3640 	for (;;) {
3641 		uint8_t *p = data + bufferPos;
3642 		uint8_t *limit = data + size - 4;
3643 
3644 		for (; p < limit; p++)
3645 			if ((*p & 0xFE) == 0xE8)
3646 				break;
3647 		bufferPos = (size_t)(p - data);
3648 		if (p >= limit)
3649 			break;
3650 		prevPosT = bufferPos - prevPosT;
3651 		if (prevPosT > 3)
3652 			prevMask = 0;
3653 		else {
3654 			prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
3655 			if (prevMask != 0) {
3656 				unsigned char b =
3657 					p[4 - kMaskToBitNumber[prevMask]];
3658 				if (!kMaskToAllowedStatus[prevMask] ||
3659 				    Test86MSByte(b)) {
3660 					prevPosT = bufferPos;
3661 					prevMask = ((prevMask << 1) & 0x7) | 1;
3662 					bufferPos++;
3663 					continue;
3664 				}
3665 			}
3666 		}
3667 		prevPosT = bufferPos;
3668 
3669 		if (Test86MSByte(p[4])) {
3670 			uint32_t src = ((uint32_t)p[4] << 24) |
3671 				((uint32_t)p[3] << 16) | ((uint32_t)p[2] << 8) |
3672 				((uint32_t)p[1]);
3673 			uint32_t dest;
3674 			for (;;) {
3675 				uint8_t b;
3676 				int b_index;
3677 
3678 				dest = src - (ip + (uint32_t)bufferPos);
3679 				if (prevMask == 0)
3680 					break;
3681 				b_index = kMaskToBitNumber[prevMask] * 8;
3682 				b = (uint8_t)(dest >> (24 - b_index));
3683 				if (!Test86MSByte(b))
3684 					break;
3685 				src = dest ^ ((1 << (32 - b_index)) - 1);
3686 			}
3687 			p[4] = (uint8_t)(~(((dest >> 24) & 1) - 1));
3688 			p[3] = (uint8_t)(dest >> 16);
3689 			p[2] = (uint8_t)(dest >> 8);
3690 			p[1] = (uint8_t)dest;
3691 			bufferPos += 5;
3692 		} else {
3693 			prevMask = ((prevMask << 1) & 0x7) | 1;
3694 			bufferPos++;
3695 		}
3696 	}
3697 	zip->bcj_prevPosT = prevPosT;
3698 	zip->bcj_prevMask = prevMask;
3699 	zip->bcj_ip += (uint32_t)bufferPos;
3700 	return (bufferPos);
3701 }
3702 
3703 /*
3704  * Brought from LZMA SDK.
3705  *
3706  * Bcj2.c -- Converter for x86 code (BCJ2)
3707  * 2008-10-04 : Igor Pavlov : Public domain
3708  *
3709  */
3710 
3711 #define SZ_ERROR_DATA	 ARCHIVE_FAILED
3712 
3713 #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
3714 #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
3715 
3716 #define kNumTopBits 24
3717 #define kTopValue ((uint32_t)1 << kNumTopBits)
3718 
3719 #define kNumBitModelTotalBits 11
3720 #define kBitModelTotal (1 << kNumBitModelTotalBits)
3721 #define kNumMoveBits 5
3722 
3723 #define RC_READ_BYTE (*buffer++)
3724 #define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; }
3725 #define RC_INIT2 zip->bcj2_code = 0; zip->bcj2_range = 0xFFFFFFFF; \
3726   { int ii; for (ii = 0; ii < 5; ii++) { RC_TEST; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }}
3727 
3728 #define NORMALIZE if (zip->bcj2_range < kTopValue) { RC_TEST; zip->bcj2_range <<= 8; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }
3729 
3730 #define IF_BIT_0(p) ttt = *(p); bound = (zip->bcj2_range >> kNumBitModelTotalBits) * ttt; if (zip->bcj2_code < bound)
3731 #define UPDATE_0(p) zip->bcj2_range = bound; *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); NORMALIZE;
3732 #define UPDATE_1(p) zip->bcj2_range -= bound; zip->bcj2_code -= bound; *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); NORMALIZE;
3733 
3734 static ssize_t
3735 Bcj2_Decode(struct _7zip *zip, uint8_t *outBuf, size_t outSize)
3736 {
3737 	size_t inPos = 0, outPos = 0;
3738 	const uint8_t *buf0, *buf1, *buf2, *buf3;
3739 	size_t size0, size1, size2, size3;
3740 	const uint8_t *buffer, *bufferLim;
3741 	unsigned int i, j;
3742 
3743 	size0 = zip->tmp_stream_bytes_remaining;
3744 	buf0 = zip->tmp_stream_buff + zip->tmp_stream_bytes_avail - size0;
3745 	size1 = zip->sub_stream_bytes_remaining[0];
3746 	buf1 = zip->sub_stream_buff[0] + zip->sub_stream_size[0] - size1;
3747 	size2 = zip->sub_stream_bytes_remaining[1];
3748 	buf2 = zip->sub_stream_buff[1] + zip->sub_stream_size[1] - size2;
3749 	size3 = zip->sub_stream_bytes_remaining[2];
3750 	buf3 = zip->sub_stream_buff[2] + zip->sub_stream_size[2] - size3;
3751 
3752 	buffer = buf3;
3753 	bufferLim = buffer + size3;
3754 
3755 	if (zip->bcj_state == 0) {
3756 		/*
3757 		 * Initialize.
3758 		 */
3759 		zip->bcj2_prevByte = 0;
3760 		for (i = 0;
3761 		    i < sizeof(zip->bcj2_p) / sizeof(zip->bcj2_p[0]); i++)
3762 			zip->bcj2_p[i] = kBitModelTotal >> 1;
3763 		RC_INIT2;
3764 		zip->bcj_state = 1;
3765 	}
3766 
3767 	/*
3768 	 * Gather the odd bytes of a previous call.
3769 	 */
3770 	for (i = 0; zip->odd_bcj_size > 0 && outPos < outSize; i++) {
3771 		outBuf[outPos++] = zip->odd_bcj[i];
3772 		zip->odd_bcj_size--;
3773 	}
3774 
3775 	if (outSize == 0) {
3776 		zip->bcj2_outPos += outPos;
3777 		return (outPos);
3778 	}
3779 
3780 	for (;;) {
3781 		uint8_t b;
3782 		CProb *prob;
3783 		uint32_t bound;
3784 		uint32_t ttt;
3785 
3786 		size_t limit = size0 - inPos;
3787 		if (outSize - outPos < limit)
3788 			limit = outSize - outPos;
3789 
3790 		if (zip->bcj_state == 1) {
3791 			while (limit != 0) {
3792 				uint8_t bb = buf0[inPos];
3793 				outBuf[outPos++] = bb;
3794 				if (IsJ(zip->bcj2_prevByte, bb)) {
3795 					zip->bcj_state = 2;
3796 					break;
3797 				}
3798 				inPos++;
3799 				zip->bcj2_prevByte = bb;
3800 				limit--;
3801 			}
3802 		}
3803 
3804 		if (limit == 0 || outPos == outSize)
3805 			break;
3806 		zip->bcj_state = 1;
3807 
3808 		b = buf0[inPos++];
3809 
3810 		if (b == 0xE8)
3811 			prob = zip->bcj2_p + zip->bcj2_prevByte;
3812 		else if (b == 0xE9)
3813 			prob = zip->bcj2_p + 256;
3814 		else
3815 			prob = zip->bcj2_p + 257;
3816 
3817 		IF_BIT_0(prob) {
3818 			UPDATE_0(prob)
3819 			zip->bcj2_prevByte = b;
3820 		} else {
3821 			uint32_t dest;
3822 			const uint8_t *v;
3823 			uint8_t out[4];
3824 
3825 			UPDATE_1(prob)
3826 			if (b == 0xE8) {
3827 				v = buf1;
3828 				if (size1 < 4)
3829 					return SZ_ERROR_DATA;
3830 				buf1 += 4;
3831 				size1 -= 4;
3832 			} else {
3833 				v = buf2;
3834 				if (size2 < 4)
3835 					return SZ_ERROR_DATA;
3836 				buf2 += 4;
3837 				size2 -= 4;
3838 			}
3839 			dest = (((uint32_t)v[0] << 24) |
3840 			    ((uint32_t)v[1] << 16) |
3841 			    ((uint32_t)v[2] << 8) |
3842 			    ((uint32_t)v[3])) -
3843 			    ((uint32_t)zip->bcj2_outPos + (uint32_t)outPos + 4);
3844 			out[0] = (uint8_t)dest;
3845 			out[1] = (uint8_t)(dest >> 8);
3846 			out[2] = (uint8_t)(dest >> 16);
3847 			out[3] = zip->bcj2_prevByte = (uint8_t)(dest >> 24);
3848 
3849 			for (i = 0; i < 4 && outPos < outSize; i++)
3850 				outBuf[outPos++] = out[i];
3851 			if (i < 4) {
3852 				/*
3853 				 * Save odd bytes which we could not add into
3854 				 * the output buffer because of out of space.
3855 				 */
3856 				zip->odd_bcj_size = 4 -i;
3857 				for (; i < 4; i++) {
3858 					j = i - 4 + (unsigned)zip->odd_bcj_size;
3859 					zip->odd_bcj[j] = out[i];
3860 				}
3861 				break;
3862 			}
3863 		}
3864 	}
3865 	zip->tmp_stream_bytes_remaining -= inPos;
3866 	zip->sub_stream_bytes_remaining[0] = size1;
3867 	zip->sub_stream_bytes_remaining[1] = size2;
3868 	zip->sub_stream_bytes_remaining[2] = bufferLim - buffer;
3869 	zip->bcj2_outPos += outPos;
3870 
3871 	return ((ssize_t)outPos);
3872 }
3873 
3874