2 * Copyright (c) 2011 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
36 #include <cm3p/bzlib.h>
39 #include <cm3p/lzma.h>
42 #include <cm3p/zlib.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"
54 #include "archive_crc32.h"
57 #define _7ZIP_SIGNATURE "7z\xBC\xAF\x27\x1C"
58 #define SFX_MIN_ADDR 0x27000
59 #define SFX_MAX_ADDR 0x60000
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 */
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
86 * 7-Zip header property IDs.
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
100 #define kCodersUnPackSize 0x0C
101 #define kNumUnPackStream 0x0D
102 #define kEmptyStream 0x0E
103 #define kEmptyFile 0x0F
109 #define kAttributes 0x15
110 #define kEncodedHeader 0x17
114 unsigned char *defineds;
123 uint64_t numInStreams;
124 uint64_t numOutStreams;
125 uint64_t propertiesSize;
126 unsigned char *properties;
128 uint64_t numBindPairs;
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;
140 uint64_t numUnpackStreams;
142 /* Unoperated bytes. */
143 uint64_t skipped_bytes;
146 struct _7z_coders_info {
148 struct _7z_folder *folders;
149 uint64_t dataStreamIndex;
152 struct _7z_pack_info {
154 uint64_t numPackStreams;
156 struct _7z_digests digest;
157 /* Calculated from pos and numPackStreams. */
161 struct _7z_substream_info {
162 size_t unpack_streams;
163 uint64_t *unpackSizes;
164 unsigned char *digestsDefined;
168 struct _7z_stream_info {
169 struct _7z_pack_info pi;
170 struct _7z_coders_info ci;
171 struct _7z_substream_info ss;
174 struct _7z_header_info {
177 unsigned char *emptyStreamBools;
178 unsigned char *emptyFileBools;
179 unsigned char *antiBools;
180 unsigned char *attrBools;
185 unsigned char *utf16name;
186 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
187 const wchar_t *wname;
189 uint32_t folderIndex;
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)
209 /* Structural information about the archive. */
210 struct _7z_stream_info si;
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. */
222 /* List of entries */
223 size_t entries_remaining;
225 struct _7zip_entry *entries;
226 struct _7zip_entry *entry;
227 unsigned char *entry_names;
229 /* entry_bytes_remaining is the number of bytes we expect. */
230 int64_t entry_offset;
231 uint64_t entry_bytes_remaining;
233 /* Running CRC32 of the decompressed data */
234 unsigned long entry_crc32;
236 /* Flags to mark progress of decompression. */
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;
246 /* Offset of the compressed data. */
247 int64_t stream_offset;
250 * Decompressing control data.
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;
259 /* The codec information of a folder. */
261 unsigned long codec2;
264 * Decompressor controllers.
266 /* Decoding LZMA1 and LZMA2 data. */
268 lzma_stream lzstream;
271 /* Decoding bzip2 data. */
272 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
276 /* Decoding deflate data. */
281 /* Decoding PPMd data. */
283 CPpmd7 ppmd7_context;
284 CPpmd7z_RangeDec range_dec;
287 const unsigned char *next_in;
290 unsigned char *next_out;
297 /* Decoding BCJ and BCJ2 data. */
300 unsigned char odd_bcj[4];
301 /* Decoding BCJ data. */
303 uint32_t bcj_prevMask;
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;
316 #define CProb uint32_t
318 #define CProb uint16_t
320 CProb bcj2_p[256 + 2];
321 uint8_t bcj2_prevByte;
324 uint64_t bcj2_outPos;
326 /* Filename character-set conversion data. */
327 struct archive_string_conv *sconv;
329 char format_name[64];
331 /* Custom value that is non-zero if this archive contains encrypted entries. */
332 int has_encrypted_entries;
335 /* Maximum entry size. This limitation prevents reading intentional
336 * corrupted 7-zip files on assuming there are not so many entries in
338 #define UMAX_ENTRY ARCHIVE_LITERAL_ULL(100000000)
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 **,
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 *,
377 static int read_Folder(struct archive_read *, struct _7z_folder *);
378 static int read_Header(struct archive_read *, struct _7z_header_info *,
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 *,
387 static void read_consume(struct archive_read *);
388 static ssize_t read_stream(struct archive_read *, const void **, 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 *,
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);
403 archive_read_support_format_7zip(struct archive *_a)
405 struct archive_read *a = (struct archive_read *)_a;
409 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
410 ARCHIVE_STATE_NEW, "archive_read_support_format_7zip");
412 zip = calloc(1, sizeof(*zip));
414 archive_set_error(&a->archive, ENOMEM,
415 "Can't allocate 7zip data");
416 return (ARCHIVE_FATAL);
420 * Until enough data has been read, we cannot tell about
421 * any encrypted entries yet.
423 zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
426 r = __archive_read_register_format(a,
429 archive_read_format_7zip_bid,
431 archive_read_format_7zip_read_header,
432 archive_read_format_7zip_read_data,
433 archive_read_format_7zip_read_data_skip,
435 archive_read_format_7zip_cleanup,
436 archive_read_support_format_7zip_capabilities,
437 archive_read_format_7zip_has_encrypted_entries);
445 archive_read_support_format_7zip_capabilities(struct archive_read * a)
447 (void)a; /* UNUSED */
448 return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
449 ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
454 archive_read_format_7zip_has_encrypted_entries(struct archive_read *_a)
456 if (_a && _a->format) {
457 struct _7zip * zip = (struct _7zip *)_a->format->data;
459 return zip->has_encrypted_entries;
462 return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
466 archive_read_format_7zip_bid(struct archive_read *a, int best_bid)
470 /* If someone has already bid more than 32, then avoid
471 trashing the look-ahead buffers with a seek. */
475 if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
478 /* If first six bytes are the 7-Zip signature,
479 * return the bid right now. */
480 if (memcmp(p, _7ZIP_SIGNATURE, 6) == 0)
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.
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;
495 while (offset + window <= (SFX_MAX_ADDR)) {
496 const char *buff = __archive_read_ahead(a,
497 offset + window, &bytes_avail);
499 /* Remaining bytes are less than window. */
506 while (p + 32 < buff + bytes_avail) {
507 int step = check_7zip_header_in_sfx(p);
519 check_7zip_header_in_sfx(const char *p)
521 switch ((unsigned char)p[5]) {
523 if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0)
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.
530 if (crc32(0, (const unsigned char *)p + 12, 20)
531 != archive_le32dec(p + 8))
533 /* Hit the header! */
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);
545 skip_sfx(struct archive_read *a, ssize_t bytes_avail)
550 ssize_t bytes, window;
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.
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);
564 while (offset + window <= SFX_MAX_ADDR - SFX_MIN_ADDR) {
565 h = __archive_read_ahead(a, window, &bytes);
567 /* Remaining bytes are less than window. */
574 /* This case might happen when window == 1. */
582 * Scan ahead until we find something that looks
583 * like the 7-Zip header.
586 int step = check_7zip_header_in_sfx(p);
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;
597 skip = p - (const char *)h;
598 __archive_read_consume(a, skip);
604 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
605 "Couldn't find out 7-Zip header");
606 return (ARCHIVE_FATAL);
610 archive_read_format_7zip_read_header(struct archive_read *a,
611 struct archive_entry *entry)
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;
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
626 if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
627 zip->has_encrypted_entries = 0;
630 a->archive.archive_format = ARCHIVE_FORMAT_7ZIP;
631 if (a->archive.archive_format_name == NULL)
632 a->archive.archive_format_name = "7-Zip";
634 if (zip->entries == NULL) {
635 struct _7z_header_info header;
637 memset(&header, 0, sizeof(header));
638 r = slurp_central_directory(a, zip, &header);
639 free_Header(&header);
642 zip->entries_remaining = (size_t)zip->numFiles;
643 zip->entry = zip->entries;
647 zip_entry = zip->entry;
649 if (zip->entries_remaining <= 0 || zip_entry == NULL)
651 --zip->entries_remaining;
653 zip->entry_offset = 0;
654 zip->end_of_entry = 0;
655 zip->entry_crc32 = crc32(0, NULL, 0);
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);
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;
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.
687 if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
688 zip->has_encrypted_entries = 0;
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);
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));
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);
723 zip->entry_bytes_remaining = 0;
724 archive_entry_set_size(entry, 0);
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;
731 if ((zip_entry->mode & AE_IFMT) == AE_IFLNK) {
732 unsigned char *symname = NULL;
736 * Symbolic-name is recorded as its contents. We have to
737 * read the contents at this time.
739 while (zip->entry_bytes_remaining > 0) {
745 r = archive_read_format_7zip_read_data(a, &buff,
747 if (r < ARCHIVE_WARN) {
751 mem = realloc(symname, symsize + size + 1);
754 archive_set_error(&a->archive, ENOMEM,
755 "Can't allocate memory for Symname");
756 return (ARCHIVE_FATAL);
759 memcpy(symname+symsize, buff, size);
763 /* If there is no symname, handle it as a regular
765 zip_entry->mode &= ~AE_IFMT;
766 zip_entry->mode |= AE_IFREG;
767 archive_entry_set_mode(entry, zip_entry->mode);
769 symname[symsize] = '\0';
770 archive_entry_copy_symlink(entry,
771 (const char *)symname);
774 archive_entry_set_size(entry, 0);
777 /* Set up a more descriptive format name. */
778 sprintf(zip->format_name, "7-Zip");
779 a->archive.archive_format_name = zip->format_name;
785 archive_read_format_7zip_read_data(struct archive_read *a,
786 const void **buff, size_t *size, int64_t *offset)
790 int ret = ARCHIVE_OK;
792 zip = (struct _7zip *)(a->format->data);
794 if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
795 zip->has_encrypted_entries = 0;
798 if (zip->pack_stream_bytes_unconsumed)
801 *offset = zip->entry_offset;
805 * If we hit end-of-entry last time, clean up and return
806 * ARCHIVE_EOF this time.
808 if (zip->end_of_entry)
809 return (ARCHIVE_EOF);
811 const uint64_t max_read_size = 16 * 1024 * 1024; // Don't try to read more than 16 MB at a time
812 size_t bytes_to_read = max_read_size;
813 if ((uint64_t)bytes_to_read > zip->entry_bytes_remaining) {
814 bytes_to_read = zip->entry_bytes_remaining;
816 bytes = read_stream(a, buff, bytes_to_read, 0);
820 archive_set_error(&a->archive,
821 ARCHIVE_ERRNO_FILE_FORMAT,
822 "Truncated 7-Zip file body");
823 return (ARCHIVE_FATAL);
825 zip->entry_bytes_remaining -= bytes;
826 if (zip->entry_bytes_remaining == 0)
827 zip->end_of_entry = 1;
829 /* Update checksum */
830 if ((zip->entry->flg & CRC32_IS_SET) && bytes)
831 zip->entry_crc32 = crc32(zip->entry_crc32, *buff,
834 /* If we hit the end, swallow any end-of-data marker. */
835 if (zip->end_of_entry) {
836 /* Check computed CRC against file contents. */
837 if ((zip->entry->flg & CRC32_IS_SET) &&
838 zip->si.ss.digests[zip->entry->ssIndex] !=
840 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
841 "7-Zip bad CRC: 0x%lx should be 0x%lx",
842 (unsigned long)zip->entry_crc32,
843 (unsigned long)zip->si.ss.digests[
844 zip->entry->ssIndex]);
850 *offset = zip->entry_offset;
851 zip->entry_offset += bytes;
857 archive_read_format_7zip_read_data_skip(struct archive_read *a)
860 int64_t bytes_skipped;
862 zip = (struct _7zip *)(a->format->data);
864 if (zip->pack_stream_bytes_unconsumed)
867 /* If we've already read to end of data, we're done. */
868 if (zip->end_of_entry)
872 * If the length is at the beginning, we can skip the
873 * compressed data much more quickly.
875 bytes_skipped = skip_stream(a, (size_t)zip->entry_bytes_remaining);
876 if (bytes_skipped < 0)
877 return (ARCHIVE_FATAL);
878 zip->entry_bytes_remaining = 0;
880 /* This entry is finished and done. */
881 zip->end_of_entry = 1;
886 archive_read_format_7zip_cleanup(struct archive_read *a)
890 zip = (struct _7zip *)(a->format->data);
891 free_StreamsInfo(&(zip->si));
893 free(zip->entry_names);
894 free_decompression(a, zip);
895 free(zip->uncompressed_buffer);
896 free(zip->sub_stream_buff[0]);
897 free(zip->sub_stream_buff[1]);
898 free(zip->sub_stream_buff[2]);
899 free(zip->tmp_stream_buff);
901 (a->format->data) = NULL;
906 read_consume(struct archive_read *a)
908 struct _7zip *zip = (struct _7zip *)a->format->data;
910 if (zip->pack_stream_bytes_unconsumed) {
911 __archive_read_consume(a, zip->pack_stream_bytes_unconsumed);
912 zip->stream_offset += zip->pack_stream_bytes_unconsumed;
913 zip->pack_stream_bytes_unconsumed = 0;
920 * Set an error code and choose an error message for liblzma.
923 set_error(struct archive_read *a, int ret)
927 case LZMA_STREAM_END: /* Found end of stream. */
928 case LZMA_OK: /* Decompressor made some progress. */
931 archive_set_error(&a->archive, ENOMEM,
932 "Lzma library error: Cannot allocate memory");
934 case LZMA_MEMLIMIT_ERROR:
935 archive_set_error(&a->archive, ENOMEM,
936 "Lzma library error: Out of memory");
938 case LZMA_FORMAT_ERROR:
939 archive_set_error(&a->archive,
941 "Lzma library error: format not recognized");
943 case LZMA_OPTIONS_ERROR:
944 archive_set_error(&a->archive,
946 "Lzma library error: Invalid options");
948 case LZMA_DATA_ERROR:
949 archive_set_error(&a->archive,
951 "Lzma library error: Corrupted input data");
954 archive_set_error(&a->archive,
956 "Lzma library error: No progress is possible");
959 /* Return an error. */
960 archive_set_error(&a->archive,
962 "Lzma decompression failed: Unknown error");
970 decode_codec_id(const unsigned char *codecId, size_t id_size)
973 unsigned long id = 0;
975 for (i = 0; i < id_size; i++) {
985 struct archive_read *a = ((IByteIn*)p)->a;
986 struct _7zip *zip = (struct _7zip *)(a->format->data);
989 if (zip->ppstream.avail_in == 0) {
990 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
991 "Truncated RAR file data");
992 zip->ppstream.overconsumed = 1;
995 b = *zip->ppstream.next_in++;
996 zip->ppstream.avail_in--;
997 zip->ppstream.total_in++;
1002 init_decompression(struct archive_read *a, struct _7zip *zip,
1003 const struct _7z_coder *coder1, const struct _7z_coder *coder2)
1007 zip->codec = coder1->codec;
1010 switch (zip->codec) {
1015 if (coder2 != NULL) {
1016 if (coder2->codec != _7Z_X86 &&
1017 coder2->codec != _7Z_X86_BCJ2) {
1018 archive_set_error(&a->archive,
1020 "Unsupported filter %lx for %lx",
1021 coder2->codec, coder1->codec);
1022 return (ARCHIVE_FAILED);
1024 zip->codec2 = coder2->codec;
1026 if (coder2->codec == _7Z_X86)
1034 switch (zip->codec) {
1038 case _7Z_LZMA: case _7Z_LZMA2:
1040 #if LZMA_VERSION_MAJOR >= 5
1041 /* Effectively disable the limiter. */
1042 #define LZMA_MEMLIMIT UINT64_MAX
1044 /* NOTE: This needs to check memory size which running system has. */
1045 #define LZMA_MEMLIMIT (1U << 30)
1048 lzma_options_delta delta_opt;
1049 lzma_filter filters[LZMA_FILTERS_MAX], *ff;
1052 if (zip->lzstream_valid) {
1053 lzma_end(&(zip->lzstream));
1054 zip->lzstream_valid = 0;
1058 * NOTE: liblzma incompletely handle the BCJ+LZMA compressed
1059 * data made by 7-Zip because 7-Zip does not add End-Of-
1060 * Payload Marker(EOPM) at the end of LZMA compressed data,
1061 * and so liblzma cannot know the end of the compressed data
1062 * without EOPM. So consequently liblzma will not return last
1063 * three or four bytes of uncompressed data because
1064 * LZMA_FILTER_X86 filter does not handle input data if its
1065 * data size is less than five bytes. If liblzma detect EOPM
1066 * or know the uncompressed data size, liblzma will flush out
1067 * the remaining that three or four bytes of uncompressed
1068 * data. That is why we have to use our converting program
1069 * for BCJ+LZMA. If we were able to tell the uncompressed
1070 * size to liblzma when using lzma_raw_decoder() liblzma
1071 * could correctly deal with BCJ+LZMA. But unfortunately
1072 * there is no way to do that.
1073 * Discussion about this can be found at XZ Utils forum.
1075 if (coder2 != NULL) {
1076 zip->codec2 = coder2->codec;
1078 filters[fi].options = NULL;
1079 switch (zip->codec2) {
1081 if (zip->codec == _7Z_LZMA2) {
1082 filters[fi].id = LZMA_FILTER_X86;
1085 /* Use our filter. */
1089 /* Use our filter. */
1093 if (coder2->propertiesSize != 1) {
1094 archive_set_error(&a->archive,
1096 "Invalid Delta parameter");
1097 return (ARCHIVE_FAILED);
1099 filters[fi].id = LZMA_FILTER_DELTA;
1100 memset(&delta_opt, 0, sizeof(delta_opt));
1101 delta_opt.type = LZMA_DELTA_TYPE_BYTE;
1103 (uint32_t)coder2->properties[0] + 1;
1104 filters[fi].options = &delta_opt;
1107 /* Following filters have not been tested yet. */
1109 filters[fi].id = LZMA_FILTER_POWERPC;
1113 filters[fi].id = LZMA_FILTER_IA64;
1117 filters[fi].id = LZMA_FILTER_ARM;
1121 filters[fi].id = LZMA_FILTER_ARMTHUMB;
1125 filters[fi].id = LZMA_FILTER_SPARC;
1129 archive_set_error(&a->archive,
1131 "Unexpected codec ID: %lX", zip->codec2);
1132 return (ARCHIVE_FAILED);
1136 if (zip->codec == _7Z_LZMA2)
1137 filters[fi].id = LZMA_FILTER_LZMA2;
1139 filters[fi].id = LZMA_FILTER_LZMA1;
1140 filters[fi].options = NULL;
1142 r = lzma_properties_decode(&filters[fi], NULL,
1143 coder1->properties, (size_t)coder1->propertiesSize);
1146 return (ARCHIVE_FAILED);
1150 filters[fi].id = LZMA_VLI_UNKNOWN;
1151 filters[fi].options = NULL;
1152 r = lzma_raw_decoder(&(zip->lzstream), filters);
1156 return (ARCHIVE_FAILED);
1158 zip->lzstream_valid = 1;
1159 zip->lzstream.total_in = 0;
1160 zip->lzstream.total_out = 0;
1164 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1165 "LZMA codec is unsupported");
1166 return (ARCHIVE_FAILED);
1169 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1170 if (zip->bzstream_valid) {
1171 BZ2_bzDecompressEnd(&(zip->bzstream));
1172 zip->bzstream_valid = 0;
1174 r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 0);
1175 if (r == BZ_MEM_ERROR)
1176 r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 1);
1178 int err = ARCHIVE_ERRNO_MISC;
1179 const char *detail = NULL;
1181 case BZ_PARAM_ERROR:
1182 detail = "invalid setup parameter";
1186 detail = "out of memory";
1188 case BZ_CONFIG_ERROR:
1189 detail = "mis-compiled library";
1192 archive_set_error(&a->archive, err,
1193 "Internal error initializing decompressor: %s",
1194 detail != NULL ? detail : "??");
1195 zip->bzstream_valid = 0;
1196 return (ARCHIVE_FAILED);
1198 zip->bzstream_valid = 1;
1199 zip->bzstream.total_in_lo32 = 0;
1200 zip->bzstream.total_in_hi32 = 0;
1201 zip->bzstream.total_out_lo32 = 0;
1202 zip->bzstream.total_out_hi32 = 0;
1205 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1206 "BZ2 codec is unsupported");
1207 return (ARCHIVE_FAILED);
1211 if (zip->stream_valid)
1212 r = inflateReset(&(zip->stream));
1214 r = inflateInit2(&(zip->stream),
1215 -15 /* Don't check for zlib header */);
1217 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1218 "Couldn't initialize zlib stream.");
1219 return (ARCHIVE_FAILED);
1221 zip->stream_valid = 1;
1222 zip->stream.total_in = 0;
1223 zip->stream.total_out = 0;
1226 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1227 "DEFLATE codec is unsupported");
1228 return (ARCHIVE_FAILED);
1235 if (zip->ppmd7_valid) {
1236 __archive_ppmd7_functions.Ppmd7_Free(
1237 &zip->ppmd7_context);
1238 zip->ppmd7_valid = 0;
1241 if (coder1->propertiesSize < 5) {
1242 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1243 "Malformed PPMd parameter");
1244 return (ARCHIVE_FAILED);
1246 order = coder1->properties[0];
1247 msize = archive_le32dec(&(coder1->properties[1]));
1248 if (order < PPMD7_MIN_ORDER || order > PPMD7_MAX_ORDER ||
1249 msize < PPMD7_MIN_MEM_SIZE || msize > PPMD7_MAX_MEM_SIZE) {
1250 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1251 "Malformed PPMd parameter");
1252 return (ARCHIVE_FAILED);
1254 __archive_ppmd7_functions.Ppmd7_Construct(&zip->ppmd7_context);
1255 r = __archive_ppmd7_functions.Ppmd7_Alloc(
1256 &zip->ppmd7_context, msize);
1258 archive_set_error(&a->archive, ENOMEM,
1259 "Coludn't allocate memory for PPMd");
1260 return (ARCHIVE_FATAL);
1262 __archive_ppmd7_functions.Ppmd7_Init(
1263 &zip->ppmd7_context, order);
1264 __archive_ppmd7_functions.Ppmd7z_RangeDec_CreateVTable(
1266 zip->ppmd7_valid = 1;
1267 zip->ppmd7_stat = 0;
1268 zip->ppstream.overconsumed = 0;
1269 zip->ppstream.total_in = 0;
1270 zip->ppstream.total_out = 0;
1281 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1282 "Unexpected codec ID: %lX", zip->codec);
1283 return (ARCHIVE_FAILED);
1284 case _7Z_CRYPTO_MAIN_ZIP:
1285 case _7Z_CRYPTO_RAR_29:
1286 case _7Z_CRYPTO_AES_256_SHA_256:
1288 archive_entry_set_is_metadata_encrypted(a->entry, 1);
1289 archive_entry_set_is_data_encrypted(a->entry, 1);
1290 zip->has_encrypted_entries = 1;
1292 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1293 "Crypto codec not supported yet (ID: 0x%lX)", zip->codec);
1294 return (ARCHIVE_FAILED);
1296 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1297 "Unknown codec ID: %lX", zip->codec);
1298 return (ARCHIVE_FAILED);
1301 return (ARCHIVE_OK);
1305 decompress(struct archive_read *a, struct _7zip *zip,
1306 void *buff, size_t *outbytes, const void *b, size_t *used)
1308 const uint8_t *t_next_in;
1309 uint8_t *t_next_out;
1310 size_t o_avail_in, o_avail_out;
1311 size_t t_avail_in, t_avail_out;
1312 uint8_t *bcj2_next_out;
1313 size_t bcj2_avail_out;
1314 int r, ret = ARCHIVE_OK;
1316 t_avail_in = o_avail_in = *used;
1317 t_avail_out = o_avail_out = *outbytes;
1321 if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
1324 /* Do not copy out the BCJ remaining bytes when the output
1325 * buffer size is less than five bytes. */
1326 if (o_avail_in != 0 && t_avail_out < 5 && zip->odd_bcj_size) {
1331 for (i = 0; zip->odd_bcj_size > 0 && t_avail_out; i++) {
1332 *t_next_out++ = zip->odd_bcj[i];
1334 zip->odd_bcj_size--;
1336 if (o_avail_in == 0 || t_avail_out == 0) {
1337 *used = o_avail_in - t_avail_in;
1338 *outbytes = o_avail_out - t_avail_out;
1339 if (o_avail_in == 0)
1345 bcj2_next_out = t_next_out;
1346 bcj2_avail_out = t_avail_out;
1347 if (zip->codec2 == _7Z_X86_BCJ2) {
1349 * Decord a remaining decompressed main stream for BCJ2.
1351 if (zip->tmp_stream_bytes_remaining) {
1353 size_t remaining = zip->tmp_stream_bytes_remaining;
1354 bytes = Bcj2_Decode(zip, t_next_out, t_avail_out);
1356 archive_set_error(&(a->archive),
1358 "BCJ2 conversion Failed");
1359 return (ARCHIVE_FAILED);
1361 zip->main_stream_bytes_remaining -=
1362 remaining - zip->tmp_stream_bytes_remaining;
1363 t_avail_out -= bytes;
1364 if (o_avail_in == 0 || t_avail_out == 0) {
1366 *outbytes = o_avail_out - t_avail_out;
1367 if (o_avail_in == 0 &&
1368 zip->tmp_stream_bytes_remaining)
1372 t_next_out += bytes;
1373 bcj2_next_out = t_next_out;
1374 bcj2_avail_out = t_avail_out;
1376 t_next_out = zip->tmp_stream_buff;
1377 t_avail_out = zip->tmp_stream_buff_size;
1380 switch (zip->codec) {
1384 (t_avail_in > t_avail_out)?t_avail_out:t_avail_in;
1386 memcpy(t_next_out, t_next_in, bytes);
1387 t_avail_in -= bytes;
1388 t_avail_out -= bytes;
1389 if (o_avail_in == 0)
1394 case _7Z_LZMA: case _7Z_LZMA2:
1395 zip->lzstream.next_in = t_next_in;
1396 zip->lzstream.avail_in = t_avail_in;
1397 zip->lzstream.next_out = t_next_out;
1398 zip->lzstream.avail_out = t_avail_out;
1400 r = lzma_code(&(zip->lzstream), LZMA_RUN);
1402 case LZMA_STREAM_END: /* Found end of stream. */
1403 lzma_end(&(zip->lzstream));
1404 zip->lzstream_valid = 0;
1407 case LZMA_OK: /* Decompressor made some progress. */
1410 archive_set_error(&(a->archive),
1412 "Decompression failed(%d)",
1414 return (ARCHIVE_FAILED);
1416 t_avail_in = zip->lzstream.avail_in;
1417 t_avail_out = zip->lzstream.avail_out;
1420 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1422 zip->bzstream.next_in = (char *)(uintptr_t)t_next_in;
1423 zip->bzstream.avail_in = t_avail_in;
1424 zip->bzstream.next_out = (char *)(uintptr_t)t_next_out;
1425 zip->bzstream.avail_out = t_avail_out;
1426 r = BZ2_bzDecompress(&(zip->bzstream));
1428 case BZ_STREAM_END: /* Found end of stream. */
1429 switch (BZ2_bzDecompressEnd(&(zip->bzstream))) {
1433 archive_set_error(&(a->archive),
1435 "Failed to clean up decompressor");
1436 return (ARCHIVE_FAILED);
1438 zip->bzstream_valid = 0;
1441 case BZ_OK: /* Decompressor made some progress. */
1444 archive_set_error(&(a->archive),
1446 "bzip decompression failed");
1447 return (ARCHIVE_FAILED);
1449 t_avail_in = zip->bzstream.avail_in;
1450 t_avail_out = zip->bzstream.avail_out;
1455 zip->stream.next_in = (Bytef *)(uintptr_t)t_next_in;
1456 zip->stream.avail_in = (uInt)t_avail_in;
1457 zip->stream.next_out = t_next_out;
1458 zip->stream.avail_out = (uInt)t_avail_out;
1459 r = inflate(&(zip->stream), 0);
1461 case Z_STREAM_END: /* Found end of stream. */
1464 case Z_OK: /* Decompressor made some progress.*/
1467 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1468 "File decompression failed (%d)", r);
1469 return (ARCHIVE_FAILED);
1471 t_avail_in = zip->stream.avail_in;
1472 t_avail_out = zip->stream.avail_out;
1477 uint64_t flush_bytes;
1479 if (!zip->ppmd7_valid || zip->ppmd7_stat < 0 ||
1481 archive_set_error(&(a->archive),
1483 "Decompression internal error");
1484 return (ARCHIVE_FAILED);
1486 zip->ppstream.next_in = t_next_in;
1487 zip->ppstream.avail_in = t_avail_in;
1488 zip->ppstream.next_out = t_next_out;
1489 zip->ppstream.avail_out = t_avail_out;
1490 if (zip->ppmd7_stat == 0) {
1492 zip->bytein.Read = &ppmd_read;
1493 zip->range_dec.Stream = &zip->bytein;
1494 r = __archive_ppmd7_functions.Ppmd7z_RangeDec_Init(
1497 zip->ppmd7_stat = -1;
1498 archive_set_error(&a->archive,
1500 "Failed to initialize PPMd range decoder");
1501 return (ARCHIVE_FAILED);
1503 if (zip->ppstream.overconsumed) {
1504 zip->ppmd7_stat = -1;
1505 return (ARCHIVE_FAILED);
1507 zip->ppmd7_stat = 1;
1510 if (t_avail_in == 0)
1511 /* XXX Flush out remaining decoded data XXX */
1512 flush_bytes = zip->folder_outbytes_remaining;
1519 sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1520 &(zip->ppmd7_context), &(zip->range_dec.p));
1522 zip->ppmd7_stat = -1;
1523 archive_set_error(&a->archive,
1524 ARCHIVE_ERRNO_FILE_FORMAT,
1525 "Failed to decode PPMd");
1526 return (ARCHIVE_FAILED);
1528 if (zip->ppstream.overconsumed) {
1529 zip->ppmd7_stat = -1;
1530 return (ARCHIVE_FAILED);
1532 *zip->ppstream.next_out++ = (unsigned char)sym;
1533 zip->ppstream.avail_out--;
1534 zip->ppstream.total_out++;
1537 } while (zip->ppstream.avail_out &&
1538 (zip->ppstream.avail_in || flush_bytes));
1540 t_avail_in = (size_t)zip->ppstream.avail_in;
1541 t_avail_out = (size_t)zip->ppstream.avail_out;
1545 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1546 "Decompression internal error");
1547 return (ARCHIVE_FAILED);
1549 if (ret != ARCHIVE_OK && ret != ARCHIVE_EOF)
1552 *used = o_avail_in - t_avail_in;
1553 *outbytes = o_avail_out - t_avail_out;
1558 if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
1559 size_t l = x86_Convert(zip, buff, *outbytes);
1560 zip->odd_bcj_size = *outbytes - l;
1561 if (zip->odd_bcj_size > 0 && zip->odd_bcj_size <= 4 &&
1562 o_avail_in && ret != ARCHIVE_EOF) {
1563 memcpy(zip->odd_bcj, ((unsigned char *)buff) + l,
1567 zip->odd_bcj_size = 0;
1571 * Decord BCJ2 with a decompressed main stream.
1573 if (zip->codec2 == _7Z_X86_BCJ2) {
1576 zip->tmp_stream_bytes_avail =
1577 zip->tmp_stream_buff_size - t_avail_out;
1578 if (zip->tmp_stream_bytes_avail >
1579 zip->main_stream_bytes_remaining)
1580 zip->tmp_stream_bytes_avail =
1581 zip->main_stream_bytes_remaining;
1582 zip->tmp_stream_bytes_remaining = zip->tmp_stream_bytes_avail;
1583 bytes = Bcj2_Decode(zip, bcj2_next_out, bcj2_avail_out);
1585 archive_set_error(&(a->archive),
1586 ARCHIVE_ERRNO_MISC, "BCJ2 conversion Failed");
1587 return (ARCHIVE_FAILED);
1589 zip->main_stream_bytes_remaining -=
1590 zip->tmp_stream_bytes_avail
1591 - zip->tmp_stream_bytes_remaining;
1592 bcj2_avail_out -= bytes;
1593 *outbytes = o_avail_out - bcj2_avail_out;
1600 free_decompression(struct archive_read *a, struct _7zip *zip)
1604 #if !defined(HAVE_ZLIB_H) &&\
1605 !(defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR))
1606 (void)a;/* UNUSED */
1609 if (zip->lzstream_valid)
1610 lzma_end(&(zip->lzstream));
1612 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1613 if (zip->bzstream_valid) {
1614 if (BZ2_bzDecompressEnd(&(zip->bzstream)) != BZ_OK) {
1615 archive_set_error(&a->archive,
1617 "Failed to clean up bzip2 decompressor");
1620 zip->bzstream_valid = 0;
1624 if (zip->stream_valid) {
1625 if (inflateEnd(&(zip->stream)) != Z_OK) {
1626 archive_set_error(&a->archive,
1628 "Failed to clean up zlib decompressor");
1631 zip->stream_valid = 0;
1634 if (zip->ppmd7_valid) {
1635 __archive_ppmd7_functions.Ppmd7_Free(
1636 &zip->ppmd7_context);
1637 zip->ppmd7_valid = 0;
1643 parse_7zip_uint64(struct archive_read *a, uint64_t *val)
1645 const unsigned char *p;
1646 unsigned char avail, mask;
1649 if ((p = header_bytes(a, 1)) == NULL)
1654 for (i = 0; i < 8; i++) {
1656 if ((p = header_bytes(a, 1)) == NULL)
1658 *val |= ((uint64_t)*p) << (8 * i);
1662 *val += ((uint64_t)(avail & (mask -1))) << (8 * i);
1669 read_Bools(struct archive_read *a, unsigned char *data, size_t num)
1671 const unsigned char *p;
1672 unsigned i, mask = 0, avail = 0;
1674 for (i = 0; i < num; i++) {
1676 if ((p = header_bytes(a, 1)) == NULL)
1681 data[i] = (avail & mask)?1:0;
1688 free_Digest(struct _7z_digests *d)
1695 read_Digests(struct archive_read *a, struct _7z_digests *d, size_t num)
1697 const unsigned char *p;
1702 memset(d, 0, sizeof(*d));
1704 d->defineds = malloc(num);
1705 if (d->defineds == NULL)
1710 if ((p = header_bytes(a, 1)) == NULL)
1713 if (read_Bools(a, d->defineds, num) < 0)
1716 /* All are defined */
1717 memset(d->defineds, 1, num);
1719 d->digests = calloc(num, sizeof(*d->digests));
1720 if (d->digests == NULL)
1722 for (i = 0; i < num; i++) {
1723 if (d->defineds[i]) {
1724 if ((p = header_bytes(a, 4)) == NULL)
1726 d->digests[i] = archive_le32dec(p);
1734 free_PackInfo(struct _7z_pack_info *pi)
1737 free(pi->positions);
1738 free_Digest(&(pi->digest));
1742 read_PackInfo(struct archive_read *a, struct _7z_pack_info *pi)
1744 const unsigned char *p;
1747 memset(pi, 0, sizeof(*pi));
1752 if (parse_7zip_uint64(a, &(pi->pos)) < 0)
1756 * Read NumPackStreams.
1758 if (parse_7zip_uint64(a, &(pi->numPackStreams)) < 0)
1760 if (pi->numPackStreams == 0)
1762 if (UMAX_ENTRY < pi->numPackStreams)
1766 * Read PackSizes[num]
1768 if ((p = header_bytes(a, 1)) == NULL)
1771 /* PackSizes[num] are not present. */
1775 pi->sizes = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
1776 pi->positions = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
1777 if (pi->sizes == NULL || pi->positions == NULL)
1780 for (i = 0; i < pi->numPackStreams; i++) {
1781 if (parse_7zip_uint64(a, &(pi->sizes[i])) < 0)
1786 * Read PackStreamDigests[num]
1788 if ((p = header_bytes(a, 1)) == NULL)
1791 /* PackStreamDigests[num] are not present. */
1792 pi->digest.defineds =
1793 calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.defineds));
1794 pi->digest.digests =
1795 calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.digests));
1796 if (pi->digest.defineds == NULL || pi->digest.digests == NULL)
1804 if (read_Digests(a, &(pi->digest), (size_t)pi->numPackStreams) < 0)
1808 * Must be marked by kEnd.
1810 if ((p = header_bytes(a, 1)) == NULL)
1818 free_Folder(struct _7z_folder *f)
1823 for (i = 0; i< f->numCoders; i++) {
1824 free(f->coders[i].properties);
1829 free(f->packedStreams);
1830 free(f->unPackSize);
1834 read_Folder(struct archive_read *a, struct _7z_folder *f)
1836 struct _7zip *zip = (struct _7zip *)a->format->data;
1837 const unsigned char *p;
1838 uint64_t numInStreamsTotal = 0;
1839 uint64_t numOutStreamsTotal = 0;
1842 memset(f, 0, sizeof(*f));
1847 if (parse_7zip_uint64(a, &(f->numCoders)) < 0)
1849 if (f->numCoders > 4)
1850 /* Too many coders. */
1853 f->coders = calloc((size_t)f->numCoders, sizeof(*f->coders));
1854 if (f->coders == NULL)
1856 for (i = 0; i< f->numCoders; i++) {
1860 if ((p = header_bytes(a, 1)) == NULL)
1866 * 5: 0 - No Attributes
1867 * 1 - There are Attributes;
1870 codec_size = *p & 0xf;
1871 simple = (*p & 0x10)?0:1;
1874 return (-1);/* Not supported. */
1877 * Read Decompression Method IDs.
1879 if ((p = header_bytes(a, codec_size)) == NULL)
1882 f->coders[i].codec = decode_codec_id(p, codec_size);
1885 f->coders[i].numInStreams = 1;
1886 f->coders[i].numOutStreams = 1;
1888 if (parse_7zip_uint64(
1889 a, &(f->coders[i].numInStreams)) < 0)
1891 if (UMAX_ENTRY < f->coders[i].numInStreams)
1893 if (parse_7zip_uint64(
1894 a, &(f->coders[i].numOutStreams)) < 0)
1896 if (UMAX_ENTRY < f->coders[i].numOutStreams)
1901 if (parse_7zip_uint64(
1902 a, &(f->coders[i].propertiesSize)) < 0)
1904 if ((p = header_bytes(
1905 a, (size_t)f->coders[i].propertiesSize)) == NULL)
1907 f->coders[i].properties =
1908 malloc((size_t)f->coders[i].propertiesSize);
1909 if (f->coders[i].properties == NULL)
1911 memcpy(f->coders[i].properties, p,
1912 (size_t)f->coders[i].propertiesSize);
1915 numInStreamsTotal += f->coders[i].numInStreams;
1916 numOutStreamsTotal += f->coders[i].numOutStreams;
1919 if (numOutStreamsTotal == 0 ||
1920 numInStreamsTotal < numOutStreamsTotal-1)
1923 f->numBindPairs = numOutStreamsTotal - 1;
1924 if (zip->header_bytes_remaining < f->numBindPairs)
1926 if (f->numBindPairs > 0) {
1928 calloc((size_t)f->numBindPairs, sizeof(*f->bindPairs));
1929 if (f->bindPairs == NULL)
1932 f->bindPairs = NULL;
1933 for (i = 0; i < f->numBindPairs; i++) {
1934 if (parse_7zip_uint64(a, &(f->bindPairs[i].inIndex)) < 0)
1936 if (UMAX_ENTRY < f->bindPairs[i].inIndex)
1938 if (parse_7zip_uint64(a, &(f->bindPairs[i].outIndex)) < 0)
1940 if (UMAX_ENTRY < f->bindPairs[i].outIndex)
1944 f->numPackedStreams = numInStreamsTotal - f->numBindPairs;
1946 calloc((size_t)f->numPackedStreams, sizeof(*f->packedStreams));
1947 if (f->packedStreams == NULL)
1949 if (f->numPackedStreams == 1) {
1950 for (i = 0; i < numInStreamsTotal; i++) {
1952 for (j = 0; j < f->numBindPairs; j++) {
1953 if (f->bindPairs[j].inIndex == i)
1956 if (j == f->numBindPairs)
1959 if (i == numInStreamsTotal)
1961 f->packedStreams[0] = i;
1963 for (i = 0; i < f->numPackedStreams; i++) {
1964 if (parse_7zip_uint64(a, &(f->packedStreams[i])) < 0)
1966 if (UMAX_ENTRY < f->packedStreams[i])
1970 f->numInStreams = numInStreamsTotal;
1971 f->numOutStreams = numOutStreamsTotal;
1977 free_CodersInfo(struct _7z_coders_info *ci)
1982 for (i = 0; i < ci->numFolders; i++)
1983 free_Folder(&(ci->folders[i]));
1989 read_CodersInfo(struct archive_read *a, struct _7z_coders_info *ci)
1991 const unsigned char *p;
1992 struct _7z_digests digest;
1995 memset(ci, 0, sizeof(*ci));
1996 memset(&digest, 0, sizeof(digest));
1998 if ((p = header_bytes(a, 1)) == NULL)
2006 if (parse_7zip_uint64(a, &(ci->numFolders)) < 0)
2008 if (UMAX_ENTRY < ci->numFolders)
2014 if ((p = header_bytes(a, 1)) == NULL)
2019 calloc((size_t)ci->numFolders, sizeof(*ci->folders));
2020 if (ci->folders == NULL)
2022 for (i = 0; i < ci->numFolders; i++) {
2023 if (read_Folder(a, &(ci->folders[i])) < 0)
2028 if (parse_7zip_uint64(a, &(ci->dataStreamIndex)) < 0)
2030 if (UMAX_ENTRY < ci->dataStreamIndex)
2032 if (ci->numFolders > 0) {
2033 archive_set_error(&a->archive, -1,
2034 "Malformed 7-Zip archive");
2039 archive_set_error(&a->archive, -1,
2040 "Malformed 7-Zip archive");
2044 if ((p = header_bytes(a, 1)) == NULL)
2046 if (*p != kCodersUnPackSize)
2049 for (i = 0; i < ci->numFolders; i++) {
2050 struct _7z_folder *folder = &(ci->folders[i]);
2053 folder->unPackSize =
2054 calloc((size_t)folder->numOutStreams, sizeof(*folder->unPackSize));
2055 if (folder->unPackSize == NULL)
2057 for (j = 0; j < folder->numOutStreams; j++) {
2058 if (parse_7zip_uint64(a, &(folder->unPackSize[j])) < 0)
2066 if ((p = header_bytes(a, 1)) == NULL)
2072 if (read_Digests(a, &digest, (size_t)ci->numFolders) < 0)
2074 for (i = 0; i < ci->numFolders; i++) {
2075 ci->folders[i].digest_defined = digest.defineds[i];
2076 ci->folders[i].digest = digest.digests[i];
2082 if ((p = header_bytes(a, 1)) == NULL)
2086 free_Digest(&digest);
2089 free_Digest(&digest);
2094 folder_uncompressed_size(struct _7z_folder *f)
2096 int n = (int)f->numOutStreams;
2097 unsigned pairs = (unsigned)f->numBindPairs;
2101 for (i = 0; i < pairs; i++) {
2102 if (f->bindPairs[i].outIndex == (uint64_t)n)
2106 return (f->unPackSize[n]);
2112 free_SubStreamsInfo(struct _7z_substream_info *ss)
2114 free(ss->unpackSizes);
2115 free(ss->digestsDefined);
2120 read_SubStreamsInfo(struct archive_read *a, struct _7z_substream_info *ss,
2121 struct _7z_folder *f, size_t numFolders)
2123 const unsigned char *p;
2125 size_t unpack_streams;
2128 uint32_t numDigests;
2130 memset(ss, 0, sizeof(*ss));
2132 for (i = 0; i < numFolders; i++)
2133 f[i].numUnpackStreams = 1;
2135 if ((p = header_bytes(a, 1)) == NULL)
2139 if (type == kNumUnPackStream) {
2141 for (i = 0; i < numFolders; i++) {
2142 if (parse_7zip_uint64(a, &(f[i].numUnpackStreams)) < 0)
2144 if (UMAX_ENTRY < f[i].numUnpackStreams)
2146 if (unpack_streams > SIZE_MAX - UMAX_ENTRY) {
2149 unpack_streams += (size_t)f[i].numUnpackStreams;
2151 if ((p = header_bytes(a, 1)) == NULL)
2155 unpack_streams = numFolders;
2157 ss->unpack_streams = unpack_streams;
2158 if (unpack_streams) {
2159 ss->unpackSizes = calloc(unpack_streams,
2160 sizeof(*ss->unpackSizes));
2161 ss->digestsDefined = calloc(unpack_streams,
2162 sizeof(*ss->digestsDefined));
2163 ss->digests = calloc(unpack_streams,
2164 sizeof(*ss->digests));
2165 if (ss->unpackSizes == NULL || ss->digestsDefined == NULL ||
2166 ss->digests == NULL)
2170 usizes = ss->unpackSizes;
2171 for (i = 0; i < numFolders; i++) {
2175 if (f[i].numUnpackStreams == 0)
2179 if (type == kSize) {
2180 for (pack = 1; pack < f[i].numUnpackStreams; pack++) {
2181 if (parse_7zip_uint64(a, usizes) < 0)
2186 *usizes++ = folder_uncompressed_size(&f[i]) - sum;
2189 if (type == kSize) {
2190 if ((p = header_bytes(a, 1)) == NULL)
2195 for (i = 0; i < unpack_streams; i++) {
2196 ss->digestsDefined[i] = 0;
2201 for (i = 0; i < numFolders; i++) {
2202 if (f[i].numUnpackStreams != 1 || !f[i].digest_defined)
2203 numDigests += (uint32_t)f[i].numUnpackStreams;
2207 struct _7z_digests tmpDigests;
2208 unsigned char *digestsDefined = ss->digestsDefined;
2209 uint32_t * digests = ss->digests;
2212 memset(&tmpDigests, 0, sizeof(tmpDigests));
2213 if (read_Digests(a, &(tmpDigests), numDigests) < 0) {
2214 free_Digest(&tmpDigests);
2217 for (i = 0; i < numFolders; i++) {
2218 if (f[i].numUnpackStreams == 1 && f[i].digest_defined) {
2219 *digestsDefined++ = 1;
2220 *digests++ = f[i].digest;
2224 for (j = 0; j < f[i].numUnpackStreams;
2227 tmpDigests.defineds[di];
2229 tmpDigests.digests[di];
2233 free_Digest(&tmpDigests);
2234 if ((p = header_bytes(a, 1)) == NULL)
2248 free_StreamsInfo(struct _7z_stream_info *si)
2250 free_PackInfo(&(si->pi));
2251 free_CodersInfo(&(si->ci));
2252 free_SubStreamsInfo(&(si->ss));
2256 read_StreamsInfo(struct archive_read *a, struct _7z_stream_info *si)
2258 struct _7zip *zip = (struct _7zip *)a->format->data;
2259 const unsigned char *p;
2262 memset(si, 0, sizeof(*si));
2264 if ((p = header_bytes(a, 1)) == NULL)
2266 if (*p == kPackInfo) {
2269 if (read_PackInfo(a, &(si->pi)) < 0)
2272 if (si->pi.positions == NULL || si->pi.sizes == NULL)
2275 * Calculate packed stream positions.
2277 packPos = si->pi.pos;
2278 for (i = 0; i < si->pi.numPackStreams; i++) {
2279 si->pi.positions[i] = packPos;
2280 packPos += si->pi.sizes[i];
2281 if (packPos > zip->header_offset)
2284 if ((p = header_bytes(a, 1)) == NULL)
2287 if (*p == kUnPackInfo) {
2289 struct _7z_folder *f;
2291 if (read_CodersInfo(a, &(si->ci)) < 0)
2295 * Calculate packed stream indexes.
2299 for (i = 0; i < si->ci.numFolders; i++) {
2300 f[i].packIndex = packIndex;
2301 packIndex += (uint32_t)f[i].numPackedStreams;
2302 if (packIndex > si->pi.numPackStreams)
2305 if ((p = header_bytes(a, 1)) == NULL)
2309 if (*p == kSubStreamsInfo) {
2310 if (read_SubStreamsInfo(a, &(si->ss),
2311 si->ci.folders, (size_t)si->ci.numFolders) < 0)
2313 if ((p = header_bytes(a, 1)) == NULL)
2326 free_Header(struct _7z_header_info *h)
2328 free(h->emptyStreamBools);
2329 free(h->emptyFileBools);
2335 read_Header(struct archive_read *a, struct _7z_header_info *h,
2336 int check_header_id)
2338 struct _7zip *zip = (struct _7zip *)a->format->data;
2339 const unsigned char *p;
2340 struct _7z_folder *folders;
2341 struct _7z_stream_info *si = &(zip->si);
2342 struct _7zip_entry *entries;
2343 uint32_t folderIndex, indexInFolder;
2345 int eindex, empty_streams, sindex;
2347 if (check_header_id) {
2351 if ((p = header_bytes(a, 1)) == NULL)
2358 * Read ArchiveProperties.
2360 if ((p = header_bytes(a, 1)) == NULL)
2362 if (*p == kArchiveProperties) {
2365 if ((p = header_bytes(a, 1)) == NULL)
2369 if (parse_7zip_uint64(a, &size) < 0)
2372 if ((p = header_bytes(a, 1)) == NULL)
2377 * Read MainStreamsInfo.
2379 if (*p == kMainStreamsInfo) {
2380 if (read_StreamsInfo(a, &(zip->si)) < 0)
2382 if ((p = header_bytes(a, 1)) == NULL)
2391 if (*p != kFilesInfo)
2394 if (parse_7zip_uint64(a, &(zip->numFiles)) < 0)
2396 if (UMAX_ENTRY < zip->numFiles)
2399 zip->entries = calloc((size_t)zip->numFiles, sizeof(*zip->entries));
2400 if (zip->entries == NULL)
2402 entries = zip->entries;
2410 if ((p = header_bytes(a, 1)) == NULL)
2416 if (parse_7zip_uint64(a, &size) < 0)
2418 if (zip->header_bytes_remaining < size)
2424 if (h->emptyStreamBools != NULL)
2426 h->emptyStreamBools = calloc((size_t)zip->numFiles,
2427 sizeof(*h->emptyStreamBools));
2428 if (h->emptyStreamBools == NULL)
2431 a, h->emptyStreamBools, (size_t)zip->numFiles) < 0)
2434 for (i = 0; i < zip->numFiles; i++) {
2435 if (h->emptyStreamBools[i])
2440 if (empty_streams <= 0) {
2441 /* Unexcepted sequence. Skip this. */
2442 if (header_bytes(a, ll) == NULL)
2446 if (h->emptyFileBools != NULL)
2448 h->emptyFileBools = calloc(empty_streams,
2449 sizeof(*h->emptyFileBools));
2450 if (h->emptyFileBools == NULL)
2452 if (read_Bools(a, h->emptyFileBools, empty_streams) < 0)
2456 if (empty_streams <= 0) {
2457 /* Unexcepted sequence. Skip this. */
2458 if (header_bytes(a, ll) == NULL)
2462 if (h->antiBools != NULL)
2464 h->antiBools = calloc(empty_streams,
2465 sizeof(*h->antiBools));
2466 if (h->antiBools == NULL)
2468 if (read_Bools(a, h->antiBools, empty_streams) < 0)
2474 if (read_Times(a, h, type) < 0)
2482 /* Skip one byte. */
2483 if ((p = header_bytes(a, 1)) == NULL)
2487 if ((ll & 1) || ll < zip->numFiles * 4)
2490 if (zip->entry_names != NULL)
2492 zip->entry_names = malloc(ll);
2493 if (zip->entry_names == NULL)
2495 np = zip->entry_names;
2498 * Copy whole file names.
2499 * NOTE: This loop prevents from expanding
2500 * the uncompressed buffer in order not to
2501 * use extra memory resource.
2505 if (nb > UBUFF_SIZE)
2509 if ((p = header_bytes(a, b)) == NULL)
2515 np = zip->entry_names;
2518 for (i = 0; i < zip->numFiles; i++) {
2519 entries[i].utf16name = np;
2520 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2521 entries[i].wname = (wchar_t *)np;
2524 /* Find a terminator. */
2525 while (nl >= 2 && (np[0] || np[1])) {
2530 return (-1);/* Terminator not found */
2531 entries[i].name_len = np - entries[i].utf16name;
2541 if ((p = header_bytes(a, 2)) == NULL)
2544 if (h->attrBools != NULL)
2546 h->attrBools = calloc((size_t)zip->numFiles,
2547 sizeof(*h->attrBools));
2548 if (h->attrBools == NULL)
2551 memset(h->attrBools, 1, (size_t)zip->numFiles);
2553 if (read_Bools(a, h->attrBools,
2554 (size_t)zip->numFiles) < 0)
2557 for (i = 0; i < zip->numFiles; i++) {
2558 if (h->attrBools[i]) {
2559 if ((p = header_bytes(a, 4)) == NULL)
2561 entries[i].attr = archive_le32dec(p);
2571 if (header_bytes(a, ll) == NULL)
2578 * Set up entry's attributes.
2580 folders = si->ci.folders;
2581 eindex = sindex = 0;
2582 folderIndex = indexInFolder = 0;
2583 for (i = 0; i < zip->numFiles; i++) {
2584 if (h->emptyStreamBools == NULL || h->emptyStreamBools[i] == 0)
2585 entries[i].flg |= HAS_STREAM;
2586 /* The high 16 bits of attributes is a posix file mode. */
2587 entries[i].mode = entries[i].attr >> 16;
2588 if (entries[i].flg & HAS_STREAM) {
2589 if ((size_t)sindex >= si->ss.unpack_streams)
2591 if (entries[i].mode == 0)
2592 entries[i].mode = AE_IFREG | 0666;
2593 if (si->ss.digestsDefined[sindex])
2594 entries[i].flg |= CRC32_IS_SET;
2595 entries[i].ssIndex = sindex;
2599 if (h->emptyFileBools == NULL)
2602 if (h->emptyFileBools[eindex])
2608 if (entries[i].mode == 0) {
2610 entries[i].mode = AE_IFDIR | 0777;
2612 entries[i].mode = AE_IFREG | 0666;
2614 (entries[i].mode & AE_IFMT) != AE_IFDIR) {
2615 entries[i].mode &= ~AE_IFMT;
2616 entries[i].mode |= AE_IFDIR;
2618 if ((entries[i].mode & AE_IFMT) == AE_IFDIR &&
2619 entries[i].name_len >= 2 &&
2620 (entries[i].utf16name[entries[i].name_len-2] != '/' ||
2621 entries[i].utf16name[entries[i].name_len-1] != 0)) {
2622 entries[i].utf16name[entries[i].name_len] = '/';
2623 entries[i].utf16name[entries[i].name_len+1] = 0;
2624 entries[i].name_len += 2;
2626 entries[i].ssIndex = -1;
2628 if (entries[i].attr & 0x01)
2629 entries[i].mode &= ~0222;/* Read only. */
2631 if ((entries[i].flg & HAS_STREAM) == 0 && indexInFolder == 0) {
2633 * The entry is an empty file or a directory file,
2634 * those both have no contents.
2636 entries[i].folderIndex = -1;
2639 if (indexInFolder == 0) {
2641 if (folderIndex >= si->ci.numFolders)
2643 if (folders[folderIndex].numUnpackStreams)
2648 entries[i].folderIndex = folderIndex;
2649 if ((entries[i].flg & HAS_STREAM) == 0)
2652 if (indexInFolder >= folders[folderIndex].numUnpackStreams) {
2661 #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
2663 fileTimeToUtc(uint64_t fileTime, time_t *timep, long *ns)
2666 if (fileTime >= EPOC_TIME) {
2667 fileTime -= EPOC_TIME;
2668 /* milli seconds base */
2669 *timep = (time_t)(fileTime / 10000000);
2670 /* nano seconds base */
2671 *ns = (long)(fileTime % 10000000) * 100;
2679 read_Times(struct archive_read *a, struct _7z_header_info *h, int type)
2681 struct _7zip *zip = (struct _7zip *)a->format->data;
2682 const unsigned char *p;
2683 struct _7zip_entry *entries = zip->entries;
2684 unsigned char *timeBools;
2688 timeBools = calloc((size_t)zip->numFiles, sizeof(*timeBools));
2689 if (timeBools == NULL)
2692 /* Read allAreDefined. */
2693 if ((p = header_bytes(a, 1)) == NULL)
2697 memset(timeBools, 1, (size_t)zip->numFiles);
2699 if (read_Bools(a, timeBools, (size_t)zip->numFiles) < 0)
2703 /* Read external. */
2704 if ((p = header_bytes(a, 1)) == NULL)
2707 if (parse_7zip_uint64(a, &(h->dataIndex)) < 0)
2709 if (UMAX_ENTRY < h->dataIndex)
2713 for (i = 0; i < zip->numFiles; i++) {
2716 if ((p = header_bytes(a, 8)) == NULL)
2720 fileTimeToUtc(archive_le64dec(p),
2721 &(entries[i].ctime),
2722 &(entries[i].ctime_ns));
2723 entries[i].flg |= CTIME_IS_SET;
2726 fileTimeToUtc(archive_le64dec(p),
2727 &(entries[i].atime),
2728 &(entries[i].atime_ns));
2729 entries[i].flg |= ATIME_IS_SET;
2732 fileTimeToUtc(archive_le64dec(p),
2733 &(entries[i].mtime),
2734 &(entries[i].mtime_ns));
2735 entries[i].flg |= MTIME_IS_SET;
2748 decode_encoded_header_info(struct archive_read *a, struct _7z_stream_info *si)
2750 struct _7zip *zip = (struct _7zip *)a->format->data;
2753 if (read_StreamsInfo(a, si) < 0) {
2754 if (errno == ENOMEM)
2755 archive_set_error(&a->archive, -1,
2756 "Couldn't allocate memory");
2758 archive_set_error(&a->archive, -1,
2759 "Malformed 7-Zip archive");
2760 return (ARCHIVE_FATAL);
2763 if (si->pi.numPackStreams == 0 || si->ci.numFolders == 0) {
2764 archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
2765 return (ARCHIVE_FATAL);
2768 if (zip->header_offset < si->pi.pos + si->pi.sizes[0] ||
2769 (int64_t)(si->pi.pos + si->pi.sizes[0]) < 0 ||
2770 si->pi.sizes[0] == 0 || (int64_t)si->pi.pos < 0) {
2771 archive_set_error(&a->archive, -1, "Malformed Header offset");
2772 return (ARCHIVE_FATAL);
2775 return (ARCHIVE_OK);
2778 static const unsigned char *
2779 header_bytes(struct archive_read *a, size_t rbytes)
2781 struct _7zip *zip = (struct _7zip *)a->format->data;
2782 const unsigned char *p;
2784 if (zip->header_bytes_remaining < rbytes)
2786 if (zip->pack_stream_bytes_unconsumed)
2789 if (zip->header_is_encoded == 0) {
2790 p = __archive_read_ahead(a, rbytes, NULL);
2793 zip->header_bytes_remaining -= rbytes;
2794 zip->pack_stream_bytes_unconsumed = rbytes;
2799 bytes = read_stream(a, &buff, rbytes, rbytes);
2802 zip->header_bytes_remaining -= bytes;
2806 /* Update checksum */
2807 zip->header_crc32 = crc32(zip->header_crc32, p, (unsigned)rbytes);
2812 slurp_central_directory(struct archive_read *a, struct _7zip *zip,
2813 struct _7z_header_info *header)
2815 const unsigned char *p;
2816 uint64_t next_header_offset;
2817 uint64_t next_header_size;
2818 uint32_t next_header_crc;
2819 ssize_t bytes_avail;
2820 int check_header_crc, r;
2822 if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
2823 return (ARCHIVE_FATAL);
2825 if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
2826 /* This is an executable ? Must be self-extracting... */
2827 r = skip_sfx(a, bytes_avail);
2828 if (r < ARCHIVE_WARN)
2830 if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
2831 return (ARCHIVE_FATAL);
2833 zip->seek_base += 32;
2835 if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0) {
2836 archive_set_error(&a->archive, -1, "Not 7-Zip archive file");
2837 return (ARCHIVE_FATAL);
2841 if (crc32(0, (const unsigned char *)p + 12, 20)
2842 != archive_le32dec(p + 8)) {
2843 archive_set_error(&a->archive, -1, "Header CRC error");
2844 return (ARCHIVE_FATAL);
2847 next_header_offset = archive_le64dec(p + 12);
2848 next_header_size = archive_le64dec(p + 20);
2849 next_header_crc = archive_le32dec(p + 28);
2851 if (next_header_size == 0)
2852 /* There is no entry in an archive file. */
2853 return (ARCHIVE_EOF);
2855 if (((int64_t)next_header_offset) < 0) {
2856 archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
2857 return (ARCHIVE_FATAL);
2859 __archive_read_consume(a, 32);
2860 if (next_header_offset != 0) {
2861 if (bytes_avail >= (ssize_t)next_header_offset)
2862 __archive_read_consume(a, next_header_offset);
2863 else if (__archive_read_seek(a,
2864 next_header_offset + zip->seek_base, SEEK_SET) < 0)
2865 return (ARCHIVE_FATAL);
2867 zip->stream_offset = next_header_offset;
2868 zip->header_offset = next_header_offset;
2869 zip->header_bytes_remaining = next_header_size;
2870 zip->header_crc32 = 0;
2871 zip->header_is_encoded = 0;
2872 zip->header_is_being_read = 1;
2873 zip->has_encrypted_entries = 0;
2874 check_header_crc = 1;
2876 if ((p = header_bytes(a, 1)) == NULL) {
2877 archive_set_error(&a->archive,
2878 ARCHIVE_ERRNO_FILE_FORMAT,
2879 "Truncated 7-Zip file body");
2880 return (ARCHIVE_FATAL);
2882 /* Parse ArchiveProperties. */
2884 case kEncodedHeader:
2886 * The archive has an encoded header and we have to decode it
2887 * in order to parse the header correctly.
2889 r = decode_encoded_header_info(a, &(zip->si));
2891 /* Check the EncodedHeader CRC.*/
2892 if (r == 0 && zip->header_crc32 != next_header_crc) {
2893 archive_set_error(&a->archive, -1,
2894 "Damaged 7-Zip archive");
2898 if (zip->si.ci.folders[0].digest_defined)
2899 next_header_crc = zip->si.ci.folders[0].digest;
2901 check_header_crc = 0;
2902 if (zip->pack_stream_bytes_unconsumed)
2904 r = setup_decode_folder(a, zip->si.ci.folders, 1);
2906 zip->header_bytes_remaining =
2907 zip->folder_outbytes_remaining;
2911 /* Clean up StreamsInfo. */
2912 free_StreamsInfo(&(zip->si));
2913 memset(&(zip->si), 0, sizeof(zip->si));
2915 return (ARCHIVE_FATAL);
2916 zip->header_is_encoded = 1;
2917 zip->header_crc32 = 0;
2924 r = read_Header(a, header, zip->header_is_encoded);
2926 if (errno == ENOMEM)
2927 archive_set_error(&a->archive, -1,
2928 "Couldn't allocate memory");
2930 archive_set_error(&a->archive, -1,
2931 "Damaged 7-Zip archive");
2932 return (ARCHIVE_FATAL);
2938 if ((p = header_bytes(a, 1)) == NULL ||*p != kEnd) {
2939 archive_set_error(&a->archive, -1,
2940 "Malformed 7-Zip archive");
2941 return (ARCHIVE_FATAL);
2944 /* Check the Header CRC.*/
2945 if (check_header_crc && zip->header_crc32 != next_header_crc) {
2946 archive_set_error(&a->archive, -1,
2947 "Malformed 7-Zip archive");
2948 return (ARCHIVE_FATAL);
2952 archive_set_error(&a->archive, -1,
2953 "Unexpected Property ID = %X", p[0]);
2954 return (ARCHIVE_FATAL);
2957 /* Clean up variables be used for decoding the archive header */
2958 zip->pack_stream_remaining = 0;
2959 zip->pack_stream_index = 0;
2960 zip->folder_outbytes_remaining = 0;
2961 zip->uncompressed_buffer_bytes_remaining = 0;
2962 zip->pack_stream_bytes_unconsumed = 0;
2963 zip->header_is_being_read = 0;
2965 return (ARCHIVE_OK);
2969 get_uncompressed_data(struct archive_read *a, const void **buff, size_t size,
2972 struct _7zip *zip = (struct _7zip *)a->format->data;
2973 ssize_t bytes_avail;
2975 if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
2978 *buff = __archive_read_ahead(a, minimum, &bytes_avail);
2979 if (bytes_avail <= 0) {
2980 archive_set_error(&a->archive,
2981 ARCHIVE_ERRNO_FILE_FORMAT,
2982 "Truncated 7-Zip file data");
2983 return (ARCHIVE_FATAL);
2985 if ((size_t)bytes_avail >
2986 zip->uncompressed_buffer_bytes_remaining)
2987 bytes_avail = (ssize_t)
2988 zip->uncompressed_buffer_bytes_remaining;
2989 if ((size_t)bytes_avail > size)
2990 bytes_avail = (ssize_t)size;
2992 zip->pack_stream_bytes_unconsumed = bytes_avail;
2993 } else if (zip->uncompressed_buffer_pointer == NULL) {
2994 /* Decompression has failed. */
2995 archive_set_error(&(a->archive),
2996 ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
2997 return (ARCHIVE_FATAL);
3000 if (minimum > zip->uncompressed_buffer_bytes_remaining) {
3002 * If remaining uncompressed data size is less than
3003 * the minimum size, fill the buffer up to the
3006 if (extract_pack_stream(a, minimum) < 0)
3007 return (ARCHIVE_FATAL);
3009 if (size > zip->uncompressed_buffer_bytes_remaining)
3010 bytes_avail = (ssize_t)
3011 zip->uncompressed_buffer_bytes_remaining;
3013 bytes_avail = (ssize_t)size;
3014 *buff = zip->uncompressed_buffer_pointer;
3015 zip->uncompressed_buffer_pointer += bytes_avail;
3017 zip->uncompressed_buffer_bytes_remaining -= bytes_avail;
3018 return (bytes_avail);
3022 extract_pack_stream(struct archive_read *a, size_t minimum)
3024 struct _7zip *zip = (struct _7zip *)a->format->data;
3025 ssize_t bytes_avail;
3028 if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
3031 if (__archive_read_ahead(a, minimum, &bytes_avail) == NULL
3032 || bytes_avail <= 0) {
3033 archive_set_error(&a->archive,
3034 ARCHIVE_ERRNO_FILE_FORMAT,
3035 "Truncated 7-Zip file body");
3036 return (ARCHIVE_FATAL);
3038 if ((uint64_t)bytes_avail > zip->pack_stream_inbytes_remaining)
3039 bytes_avail = (ssize_t)zip->pack_stream_inbytes_remaining;
3040 zip->pack_stream_inbytes_remaining -= bytes_avail;
3041 if ((uint64_t)bytes_avail > zip->folder_outbytes_remaining)
3042 bytes_avail = (ssize_t)zip->folder_outbytes_remaining;
3043 zip->folder_outbytes_remaining -= bytes_avail;
3044 zip->uncompressed_buffer_bytes_remaining = bytes_avail;
3045 return (ARCHIVE_OK);
3048 /* If the buffer hasn't been allocated, allocate it now. */
3049 if (zip->uncompressed_buffer == NULL) {
3050 zip->uncompressed_buffer_size = UBUFF_SIZE;
3051 if (zip->uncompressed_buffer_size < minimum) {
3052 zip->uncompressed_buffer_size = minimum + 1023;
3053 zip->uncompressed_buffer_size &= ~0x3ff;
3055 zip->uncompressed_buffer =
3056 malloc(zip->uncompressed_buffer_size);
3057 if (zip->uncompressed_buffer == NULL) {
3058 archive_set_error(&a->archive, ENOMEM,
3059 "No memory for 7-Zip decompression");
3060 return (ARCHIVE_FATAL);
3062 zip->uncompressed_buffer_bytes_remaining = 0;
3063 } else if (zip->uncompressed_buffer_size < minimum ||
3064 zip->uncompressed_buffer_bytes_remaining < minimum) {
3066 * Make sure the uncompressed buffer can have bytes
3067 * at least `minimum' bytes.
3068 * NOTE: This case happen when reading the header.
3071 if (zip->uncompressed_buffer_pointer != 0)
3072 used = zip->uncompressed_buffer_pointer -
3073 zip->uncompressed_buffer;
3076 if (zip->uncompressed_buffer_size < minimum) {
3078 * Expand the uncompressed buffer up to
3084 new_size = minimum + 1023;
3086 p = realloc(zip->uncompressed_buffer, new_size);
3088 archive_set_error(&a->archive, ENOMEM,
3089 "No memory for 7-Zip decompression");
3090 return (ARCHIVE_FATAL);
3092 zip->uncompressed_buffer = (unsigned char *)p;
3093 zip->uncompressed_buffer_size = new_size;
3096 * Move unconsumed bytes to the head.
3099 memmove(zip->uncompressed_buffer,
3100 zip->uncompressed_buffer + used,
3101 zip->uncompressed_buffer_bytes_remaining);
3104 zip->uncompressed_buffer_bytes_remaining = 0;
3105 zip->uncompressed_buffer_pointer = NULL;
3107 size_t bytes_in, bytes_out;
3108 const void *buff_in;
3109 unsigned char *buff_out;
3113 * Note: '1' here is a performance optimization.
3114 * Recall that the decompression layer returns a count of
3115 * available bytes; asking for more than that forces the
3116 * decompressor to combine reads by copying data.
3118 buff_in = __archive_read_ahead(a, 1, &bytes_avail);
3119 if (bytes_avail <= 0) {
3120 archive_set_error(&a->archive,
3121 ARCHIVE_ERRNO_FILE_FORMAT,
3122 "Truncated 7-Zip file body");
3123 return (ARCHIVE_FATAL);
3126 buff_out = zip->uncompressed_buffer
3127 + zip->uncompressed_buffer_bytes_remaining;
3128 bytes_out = zip->uncompressed_buffer_size
3129 - zip->uncompressed_buffer_bytes_remaining;
3130 bytes_in = bytes_avail;
3131 if (bytes_in > zip->pack_stream_inbytes_remaining)
3132 bytes_in = (size_t)zip->pack_stream_inbytes_remaining;
3133 /* Drive decompression. */
3134 r = decompress(a, zip, buff_out, &bytes_out,
3135 buff_in, &bytes_in);
3144 return (ARCHIVE_FATAL);
3146 zip->pack_stream_inbytes_remaining -= bytes_in;
3147 if (bytes_out > zip->folder_outbytes_remaining)
3148 bytes_out = (size_t)zip->folder_outbytes_remaining;
3149 zip->folder_outbytes_remaining -= bytes_out;
3150 zip->uncompressed_buffer_bytes_remaining += bytes_out;
3151 zip->pack_stream_bytes_unconsumed = bytes_in;
3154 * Continue decompression until uncompressed_buffer is full.
3156 if (zip->uncompressed_buffer_bytes_remaining ==
3157 zip->uncompressed_buffer_size)
3159 if (zip->codec2 == _7Z_X86 && zip->odd_bcj_size &&
3160 zip->uncompressed_buffer_bytes_remaining + 5 >
3161 zip->uncompressed_buffer_size)
3163 if (zip->pack_stream_inbytes_remaining == 0 &&
3164 zip->folder_outbytes_remaining == 0)
3166 if (end_of_data || (bytes_in == 0 && bytes_out == 0)) {
3167 archive_set_error(&(a->archive),
3168 ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3169 return (ARCHIVE_FATAL);
3173 if (zip->uncompressed_buffer_bytes_remaining < minimum) {
3174 archive_set_error(&(a->archive),
3175 ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3176 return (ARCHIVE_FATAL);
3178 zip->uncompressed_buffer_pointer = zip->uncompressed_buffer;
3179 return (ARCHIVE_OK);
3183 seek_pack(struct archive_read *a)
3185 struct _7zip *zip = (struct _7zip *)a->format->data;
3186 int64_t pack_offset;
3188 if (zip->pack_stream_remaining <= 0) {
3189 archive_set_error(&(a->archive),
3190 ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3191 return (ARCHIVE_FATAL);
3193 zip->pack_stream_inbytes_remaining =
3194 zip->si.pi.sizes[zip->pack_stream_index];
3195 pack_offset = zip->si.pi.positions[zip->pack_stream_index];
3196 if (zip->stream_offset != pack_offset) {
3197 if (0 > __archive_read_seek(a, pack_offset + zip->seek_base,
3199 return (ARCHIVE_FATAL);
3200 zip->stream_offset = pack_offset;
3202 zip->pack_stream_index++;
3203 zip->pack_stream_remaining--;
3204 return (ARCHIVE_OK);
3208 read_stream(struct archive_read *a, const void **buff, size_t size,
3211 struct _7zip *zip = (struct _7zip *)a->format->data;
3212 uint64_t skip_bytes = 0;
3215 if (zip->uncompressed_buffer_bytes_remaining == 0) {
3216 if (zip->pack_stream_inbytes_remaining > 0) {
3217 r = extract_pack_stream(a, 0);
3220 return (get_uncompressed_data(a, buff, size, minimum));
3221 } else if (zip->folder_outbytes_remaining > 0) {
3222 /* Extract a remaining pack stream. */
3223 r = extract_pack_stream(a, 0);
3226 return (get_uncompressed_data(a, buff, size, minimum));
3229 return (get_uncompressed_data(a, buff, size, minimum));
3232 * Current pack stream has been consumed.
3234 if (zip->pack_stream_remaining == 0) {
3235 if (zip->header_is_being_read) {
3236 /* Invalid sequence. This might happen when
3237 * reading a malformed archive. */
3238 archive_set_error(&(a->archive),
3239 ARCHIVE_ERRNO_MISC, "Malformed 7-Zip archive");
3240 return (ARCHIVE_FATAL);
3244 * All current folder's pack streams have been
3245 * consumed. Switch to next folder.
3247 if (zip->folder_index == 0 &&
3248 (zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
3249 || zip->folder_index != zip->entry->folderIndex)) {
3250 zip->folder_index = zip->entry->folderIndex;
3252 zip->si.ci.folders[zip->folder_index].skipped_bytes;
3255 if (zip->folder_index >= zip->si.ci.numFolders) {
3257 * We have consumed all folders and its pack streams.
3262 r = setup_decode_folder(a,
3263 &(zip->si.ci.folders[zip->folder_index]), 0);
3264 if (r != ARCHIVE_OK)
3265 return (ARCHIVE_FATAL);
3267 zip->folder_index++;
3271 * Switch to next pack stream.
3277 /* Extract a new pack stream. */
3278 r = extract_pack_stream(a, 0);
3283 * Skip the bytes we already has skipped in skip_stream().
3285 while (skip_bytes) {
3288 if (zip->uncompressed_buffer_bytes_remaining == 0) {
3289 if (zip->pack_stream_inbytes_remaining > 0) {
3290 r = extract_pack_stream(a, 0);
3293 } else if (zip->folder_outbytes_remaining > 0) {
3294 /* Extract a remaining pack stream. */
3295 r = extract_pack_stream(a, 0);
3299 archive_set_error(&a->archive,
3300 ARCHIVE_ERRNO_FILE_FORMAT,
3301 "Truncated 7-Zip file body");
3302 return (ARCHIVE_FATAL);
3305 skipped = get_uncompressed_data(
3306 a, buff, (size_t)skip_bytes, 0);
3309 skip_bytes -= skipped;
3310 if (zip->pack_stream_bytes_unconsumed)
3314 return (get_uncompressed_data(a, buff, size, minimum));
3318 setup_decode_folder(struct archive_read *a, struct _7z_folder *folder,
3321 struct _7zip *zip = (struct _7zip *)a->format->data;
3322 const struct _7z_coder *coder1, *coder2;
3323 const char *cname = (header)?"archive header":"file content";
3325 int r, found_bcj2 = 0;
3328 * Release the memory which the previous folder used for BCJ2.
3330 for (i = 0; i < 3; i++) {
3331 free(zip->sub_stream_buff[i]);
3332 zip->sub_stream_buff[i] = NULL;
3336 * Initialize a stream reader.
3338 zip->pack_stream_remaining = (unsigned)folder->numPackedStreams;
3339 zip->pack_stream_index = (unsigned)folder->packIndex;
3340 zip->folder_outbytes_remaining = folder_uncompressed_size(folder);
3341 zip->uncompressed_buffer_bytes_remaining = 0;
3344 * Check coder types.
3346 for (i = 0; i < folder->numCoders; i++) {
3347 switch(folder->coders[i].codec) {
3348 case _7Z_CRYPTO_MAIN_ZIP:
3349 case _7Z_CRYPTO_RAR_29:
3350 case _7Z_CRYPTO_AES_256_SHA_256: {
3351 /* For entry that is associated with this folder, mark
3352 it as encrypted (data+metadata). */
3353 zip->has_encrypted_entries = 1;
3355 archive_entry_set_is_data_encrypted(a->entry, 1);
3356 archive_entry_set_is_metadata_encrypted(a->entry, 1);
3358 archive_set_error(&(a->archive),
3360 "The %s is encrypted, "
3361 "but currently not supported", cname);
3362 return (ARCHIVE_FATAL);
3364 case _7Z_X86_BCJ2: {
3370 /* Now that we've checked for encryption, if there were still no
3371 * encrypted entries found we can say for sure that there are none.
3373 if (zip->has_encrypted_entries == ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
3374 zip->has_encrypted_entries = 0;
3377 if ((folder->numCoders > 2 && !found_bcj2) || found_bcj2 > 1) {
3378 archive_set_error(&(a->archive),
3380 "The %s is encoded with many filters, "
3381 "but currently not supported", cname);
3382 return (ARCHIVE_FATAL);
3384 coder1 = &(folder->coders[0]);
3385 if (folder->numCoders == 2)
3386 coder2 = &(folder->coders[1]);
3392 * Preparation to decode BCJ2.
3393 * Decoding BCJ2 requires four sources. Those are at least,
3394 * as far as I know, two types of the storage form.
3396 const struct _7z_coder *fc = folder->coders;
3397 static const struct _7z_coder coder_copy = {0, 1, 1, 0, NULL};
3398 const struct _7z_coder *scoder[3] =
3399 {&coder_copy, &coder_copy, &coder_copy};
3402 unsigned char *b[3] = {NULL, NULL, NULL};
3403 uint64_t sunpack[3] ={-1, -1, -1};
3404 size_t s[3] = {0, 0, 0};
3405 int idx[3] = {0, 1, 2};
3407 if (folder->numCoders == 4 && fc[3].codec == _7Z_X86_BCJ2 &&
3408 folder->numInStreams == 7 && folder->numOutStreams == 4 &&
3409 zip->pack_stream_remaining == 4) {
3410 /* Source type 1 made by 7zr or 7z with -m options. */
3411 if (folder->bindPairs[0].inIndex == 5) {
3412 /* The form made by 7zr */
3413 idx[0] = 1; idx[1] = 2; idx[2] = 0;
3414 scoder[1] = &(fc[1]);
3415 scoder[2] = &(fc[0]);
3416 sunpack[1] = folder->unPackSize[1];
3417 sunpack[2] = folder->unPackSize[0];
3421 * NOTE: Some patterns do not work.
3423 * 7z a -m0=BCJ2 -m1=COPY -m2=COPY
3425 * 7z a -m0=BCJ2 -m1=COPY -m2=(any)
3427 * 7z a -m0=BCJ2 -m1=(any) -m2=COPY
3432 * We have to handle this like `pipe' or
3433 * our libarchive7s filter frame work,
3434 * decoding the BCJ2 main stream sequentially,
3435 * m3 -> m2 -> m1 -> BCJ2.
3438 if (fc[0].codec == _7Z_COPY &&
3439 fc[1].codec == _7Z_COPY)
3440 coder1 = &(folder->coders[2]);
3441 else if (fc[0].codec == _7Z_COPY &&
3442 fc[2].codec == _7Z_COPY)
3443 coder1 = &(folder->coders[1]);
3444 else if (fc[1].codec == _7Z_COPY &&
3445 fc[2].codec == _7Z_COPY)
3446 coder1 = &(folder->coders[0]);
3448 archive_set_error(&(a->archive),
3450 "Unsupported form of "
3452 return (ARCHIVE_FATAL);
3456 zip->main_stream_bytes_remaining =
3457 (size_t)folder->unPackSize[2];
3458 } else if (coder2 != NULL && coder2->codec == _7Z_X86_BCJ2 &&
3459 zip->pack_stream_remaining == 4 &&
3460 folder->numInStreams == 5 && folder->numOutStreams == 2) {
3461 /* Source type 0 made by 7z */
3462 zip->main_stream_bytes_remaining =
3463 (size_t)folder->unPackSize[0];
3465 /* We got an unexpected form. */
3466 archive_set_error(&(a->archive),
3468 "Unsupported form of BCJ2 streams");
3469 return (ARCHIVE_FATAL);
3472 /* Skip the main stream at this time. */
3473 if ((r = seek_pack(a)) < 0)
3475 zip->pack_stream_bytes_unconsumed =
3476 (size_t)zip->pack_stream_inbytes_remaining;
3479 /* Read following three sub streams. */
3480 for (i = 0; i < 3; i++) {
3481 const struct _7z_coder *coder = scoder[i];
3483 if ((r = seek_pack(a)) < 0) {
3484 free(b[0]); free(b[1]); free(b[2]);
3488 if (sunpack[i] == (uint64_t)-1)
3489 zip->folder_outbytes_remaining =
3490 zip->pack_stream_inbytes_remaining;
3492 zip->folder_outbytes_remaining = sunpack[i];
3494 r = init_decompression(a, zip, coder, NULL);
3495 if (r != ARCHIVE_OK) {
3496 free(b[0]); free(b[1]); free(b[2]);
3497 return (ARCHIVE_FATAL);
3500 /* Allocate memory for the decoded data of a sub
3502 b[i] = malloc((size_t)zip->folder_outbytes_remaining);
3504 free(b[0]); free(b[1]); free(b[2]);
3505 archive_set_error(&a->archive, ENOMEM,
3506 "No memory for 7-Zip decompression");
3507 return (ARCHIVE_FATAL);
3510 /* Extract a sub stream. */
3511 while (zip->pack_stream_inbytes_remaining > 0) {
3512 r = (int)extract_pack_stream(a, 0);
3514 free(b[0]); free(b[1]); free(b[2]);
3517 bytes = get_uncompressed_data(a, &buff,
3518 zip->uncompressed_buffer_bytes_remaining,
3521 free(b[0]); free(b[1]); free(b[2]);
3522 return ((int)bytes);
3524 memcpy(b[i]+s[i], buff, bytes);
3526 if (zip->pack_stream_bytes_unconsumed)
3531 /* Set the sub streams to the right place. */
3532 for (i = 0; i < 3; i++) {
3533 zip->sub_stream_buff[i] = b[idx[i]];
3534 zip->sub_stream_size[i] = s[idx[i]];
3535 zip->sub_stream_bytes_remaining[i] = s[idx[i]];
3538 /* Allocate memory used for decoded main stream bytes. */
3539 if (zip->tmp_stream_buff == NULL) {
3540 zip->tmp_stream_buff_size = 32 * 1024;
3541 zip->tmp_stream_buff =
3542 malloc(zip->tmp_stream_buff_size);
3543 if (zip->tmp_stream_buff == NULL) {
3544 archive_set_error(&a->archive, ENOMEM,
3545 "No memory for 7-Zip decompression");
3546 return (ARCHIVE_FATAL);
3549 zip->tmp_stream_bytes_avail = 0;
3550 zip->tmp_stream_bytes_remaining = 0;
3551 zip->odd_bcj_size = 0;
3552 zip->bcj2_outPos = 0;
3555 * Reset a stream reader in order to read the main stream
3558 zip->pack_stream_remaining = 1;
3559 zip->pack_stream_index = (unsigned)folder->packIndex;
3560 zip->folder_outbytes_remaining =
3561 folder_uncompressed_size(folder);
3562 zip->uncompressed_buffer_bytes_remaining = 0;
3566 * Initialize the decompressor for the new folder's pack streams.
3568 r = init_decompression(a, zip, coder1, coder2);
3569 if (r != ARCHIVE_OK)
3570 return (ARCHIVE_FATAL);
3571 return (ARCHIVE_OK);
3575 skip_stream(struct archive_read *a, size_t skip_bytes)
3577 struct _7zip *zip = (struct _7zip *)a->format->data;
3579 int64_t skipped_bytes;
3580 size_t bytes = skip_bytes;
3582 if (zip->folder_index == 0) {
3584 * Optimization for a list mode.
3585 * Avoid unnecessary decoding operations.
3587 zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
3589 return (skip_bytes);
3593 skipped_bytes = read_stream(a, &p, bytes, 0);
3594 if (skipped_bytes < 0)
3595 return (skipped_bytes);
3596 if (skipped_bytes == 0) {
3597 archive_set_error(&a->archive,
3598 ARCHIVE_ERRNO_FILE_FORMAT,
3599 "Truncated 7-Zip file body");
3600 return (ARCHIVE_FATAL);
3602 bytes -= (size_t)skipped_bytes;
3603 if (zip->pack_stream_bytes_unconsumed)
3606 return (skip_bytes);
3610 * Brought from LZMA SDK.
3612 * Bra86.c -- Converter for x86 code (BCJ)
3613 * 2008-10-04 : Igor Pavlov : Public domain
3617 #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
3620 x86_Init(struct _7zip *zip)
3623 zip->bcj_prevPosT = (size_t)0 - 1;
3624 zip->bcj_prevMask = 0;
3629 x86_Convert(struct _7zip *zip, uint8_t *data, size_t size)
3631 static const uint8_t kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
3632 static const uint8_t kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
3633 size_t bufferPos, prevPosT;
3634 uint32_t ip, prevMask;
3640 prevPosT = zip->bcj_prevPosT;
3641 prevMask = zip->bcj_prevMask;
3645 uint8_t *p = data + bufferPos;
3646 uint8_t *limit = data + size - 4;
3648 for (; p < limit; p++)
3649 if ((*p & 0xFE) == 0xE8)
3651 bufferPos = (size_t)(p - data);
3654 prevPosT = bufferPos - prevPosT;
3658 prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
3659 if (prevMask != 0) {
3661 p[4 - kMaskToBitNumber[prevMask]];
3662 if (!kMaskToAllowedStatus[prevMask] ||
3664 prevPosT = bufferPos;
3665 prevMask = ((prevMask << 1) & 0x7) | 1;
3671 prevPosT = bufferPos;
3673 if (Test86MSByte(p[4])) {
3674 uint32_t src = ((uint32_t)p[4] << 24) |
3675 ((uint32_t)p[3] << 16) | ((uint32_t)p[2] << 8) |
3682 dest = src - (ip + (uint32_t)bufferPos);
3685 b_index = kMaskToBitNumber[prevMask] * 8;
3686 b = (uint8_t)(dest >> (24 - b_index));
3687 if (!Test86MSByte(b))
3689 src = dest ^ ((1 << (32 - b_index)) - 1);
3691 p[4] = (uint8_t)(~(((dest >> 24) & 1) - 1));
3692 p[3] = (uint8_t)(dest >> 16);
3693 p[2] = (uint8_t)(dest >> 8);
3694 p[1] = (uint8_t)dest;
3697 prevMask = ((prevMask << 1) & 0x7) | 1;
3701 zip->bcj_prevPosT = prevPosT;
3702 zip->bcj_prevMask = prevMask;
3703 zip->bcj_ip += (uint32_t)bufferPos;
3708 * Brought from LZMA SDK.
3710 * Bcj2.c -- Converter for x86 code (BCJ2)
3711 * 2008-10-04 : Igor Pavlov : Public domain
3715 #define SZ_ERROR_DATA ARCHIVE_FAILED
3717 #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
3718 #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
3720 #define kNumTopBits 24
3721 #define kTopValue ((uint32_t)1 << kNumTopBits)
3723 #define kNumBitModelTotalBits 11
3724 #define kBitModelTotal (1 << kNumBitModelTotalBits)
3725 #define kNumMoveBits 5
3727 #define RC_READ_BYTE (*buffer++)
3728 #define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; }
3729 #define RC_INIT2 zip->bcj2_code = 0; zip->bcj2_range = 0xFFFFFFFF; \
3730 { int ii; for (ii = 0; ii < 5; ii++) { RC_TEST; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }}
3732 #define NORMALIZE if (zip->bcj2_range < kTopValue) { RC_TEST; zip->bcj2_range <<= 8; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }
3734 #define IF_BIT_0(p) ttt = *(p); bound = (zip->bcj2_range >> kNumBitModelTotalBits) * ttt; if (zip->bcj2_code < bound)
3735 #define UPDATE_0(p) zip->bcj2_range = bound; *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); NORMALIZE;
3736 #define UPDATE_1(p) zip->bcj2_range -= bound; zip->bcj2_code -= bound; *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); NORMALIZE;
3739 Bcj2_Decode(struct _7zip *zip, uint8_t *outBuf, size_t outSize)
3741 size_t inPos = 0, outPos = 0;
3742 const uint8_t *buf0, *buf1, *buf2, *buf3;
3743 size_t size0, size1, size2, size3;
3744 const uint8_t *buffer, *bufferLim;
3747 size0 = zip->tmp_stream_bytes_remaining;
3748 buf0 = zip->tmp_stream_buff + zip->tmp_stream_bytes_avail - size0;
3749 size1 = zip->sub_stream_bytes_remaining[0];
3750 buf1 = zip->sub_stream_buff[0] + zip->sub_stream_size[0] - size1;
3751 size2 = zip->sub_stream_bytes_remaining[1];
3752 buf2 = zip->sub_stream_buff[1] + zip->sub_stream_size[1] - size2;
3753 size3 = zip->sub_stream_bytes_remaining[2];
3754 buf3 = zip->sub_stream_buff[2] + zip->sub_stream_size[2] - size3;
3757 bufferLim = buffer + size3;
3759 if (zip->bcj_state == 0) {
3763 zip->bcj2_prevByte = 0;
3765 i < sizeof(zip->bcj2_p) / sizeof(zip->bcj2_p[0]); i++)
3766 zip->bcj2_p[i] = kBitModelTotal >> 1;
3772 * Gather the odd bytes of a previous call.
3774 for (i = 0; zip->odd_bcj_size > 0 && outPos < outSize; i++) {
3775 outBuf[outPos++] = zip->odd_bcj[i];
3776 zip->odd_bcj_size--;
3780 zip->bcj2_outPos += outPos;
3790 size_t limit = size0 - inPos;
3791 if (outSize - outPos < limit)
3792 limit = outSize - outPos;
3794 if (zip->bcj_state == 1) {
3795 while (limit != 0) {
3796 uint8_t bb = buf0[inPos];
3797 outBuf[outPos++] = bb;
3798 if (IsJ(zip->bcj2_prevByte, bb)) {
3803 zip->bcj2_prevByte = bb;
3808 if (limit == 0 || outPos == outSize)
3815 prob = zip->bcj2_p + zip->bcj2_prevByte;
3817 prob = zip->bcj2_p + 256;
3819 prob = zip->bcj2_p + 257;
3823 zip->bcj2_prevByte = b;
3833 return SZ_ERROR_DATA;
3839 return SZ_ERROR_DATA;
3843 dest = (((uint32_t)v[0] << 24) |
3844 ((uint32_t)v[1] << 16) |
3845 ((uint32_t)v[2] << 8) |
3847 ((uint32_t)zip->bcj2_outPos + (uint32_t)outPos + 4);
3848 out[0] = (uint8_t)dest;
3849 out[1] = (uint8_t)(dest >> 8);
3850 out[2] = (uint8_t)(dest >> 16);
3851 out[3] = zip->bcj2_prevByte = (uint8_t)(dest >> 24);
3853 for (i = 0; i < 4 && outPos < outSize; i++)
3854 outBuf[outPos++] = out[i];
3857 * Save odd bytes which we could not add into
3858 * the output buffer because of out of space.
3860 zip->odd_bcj_size = 4 -i;
3861 for (; i < 4; i++) {
3862 j = i - 4 + (unsigned)zip->odd_bcj_size;
3863 zip->odd_bcj[j] = out[i];
3869 zip->tmp_stream_bytes_remaining -= inPos;
3870 zip->sub_stream_bytes_remaining[0] = size1;
3871 zip->sub_stream_bytes_remaining[1] = size2;
3872 zip->sub_stream_bytes_remaining[2] = bufferLim - buffer;
3873 zip->bcj2_outPos += outPos;
3875 return ((ssize_t)outPos);