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$");
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 0x06F10701
73 #define _7Z_X86 0x03030103
74 #define _7Z_X86_BCJ2 0x0303011B
75 #define _7Z_POWERPC 0x03030205
76 #define _7Z_IA64 0x03030401
77 #define _7Z_ARM 0x03030501
78 #define _7Z_ARMTHUMB 0x03030701
79 #define _7Z_SPARC 0x03030805
82 * 7-Zip header property IDs.
86 #define kArchiveProperties 0x02
87 #define kAdditionalStreamsInfo 0x03
88 #define kMainStreamsInfo 0x04
89 #define kFilesInfo 0x05
90 #define kPackInfo 0x06
91 #define kUnPackInfo 0x07
92 #define kSubStreamsInfo 0x08
96 #define kCodersUnPackSize 0x0C
97 #define kNumUnPackStream 0x0D
98 #define kEmptyStream 0x0E
99 #define kEmptyFile 0x0F
105 #define kAttributes 0x15
106 #define kEncodedHeader 0x17
109 unsigned char *defineds;
118 uint64_t numInStreams;
119 uint64_t numOutStreams;
120 uint64_t propertiesSize;
121 unsigned char *properties;
123 uint64_t numBindPairs;
128 uint64_t numPackedStreams;
129 uint64_t *packedStreams;
130 uint64_t numInStreams;
131 uint64_t numOutStreams;
132 uint64_t *unPackSize;
133 unsigned char digest_defined;
135 uint64_t numUnpackStreams;
137 /* Unoperated bytes. */
138 uint64_t skipped_bytes;
141 struct _7z_coders_info {
143 struct _7z_folder *folders;
144 uint64_t dataStreamIndex;
147 struct _7z_pack_info {
149 uint64_t numPackStreams;
151 struct _7z_digests digest;
152 /* Calculated from pos and numPackStreams. */
156 struct _7z_substream_info {
157 size_t unpack_streams;
158 uint64_t *unpackSizes;
159 unsigned char *digestsDefined;
163 struct _7z_stream_info {
164 struct _7z_pack_info pi;
165 struct _7z_coders_info ci;
166 struct _7z_substream_info ss;
169 struct _7z_header_info {
172 unsigned char *emptyStreamBools;
173 unsigned char *emptyFileBools;
174 unsigned char *antiBools;
175 unsigned char *attrBools;
180 unsigned char *utf16name;
181 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
182 const wchar_t *wname;
184 uint32_t folderIndex;
187 #define MTIME_IS_SET (1<<0)
188 #define ATIME_IS_SET (1<<1)
189 #define CTIME_IS_SET (1<<2)
190 #define CRC32_IS_SET (1<<3)
191 #define HAS_STREAM (1<<4)
204 /* Structural information about the archive. */
205 struct _7z_stream_info si;
207 int header_is_being_read;
208 int header_is_encoded;
209 uint64_t header_bytes_remaining;
210 unsigned long header_crc32;
211 /* Header offset to check that reading pointes of the file contens
212 * will not exceed the header. */
213 uint64_t header_offset;
214 /* Base offset of the archive file for a seek in case reading SFX. */
217 /* List of entries */
218 size_t entries_remaining;
220 struct _7zip_entry *entries;
221 struct _7zip_entry *entry;
222 unsigned char *entry_names;
224 /* entry_bytes_remaining is the number of bytes we expect. */
225 int64_t entry_offset;
226 uint64_t entry_bytes_remaining;
228 /* Running CRC32 of the decompressed data */
229 unsigned long entry_crc32;
231 /* Flags to mark progress of decompression. */
234 /* Uncompressed buffer control. */
235 #define UBUFF_SIZE (64 * 1024)
236 unsigned char *uncompressed_buffer;
237 unsigned char *uncompressed_buffer_pointer;
238 size_t uncompressed_buffer_size;
239 size_t uncompressed_buffer_bytes_remaining;
241 /* Offset of the compressed data. */
242 int64_t stream_offset;
245 * Decompressing control data.
247 unsigned folder_index;
248 uint64_t folder_outbytes_remaining;
249 unsigned pack_stream_index;
250 unsigned pack_stream_remaining;
251 uint64_t pack_stream_inbytes_remaining;
252 size_t pack_stream_bytes_unconsumed;
254 /* The codec information of a folder. */
256 unsigned long codec2;
259 * Decompressor controllers.
261 /* Decording LZMA1 and LZMA2 data. */
263 lzma_stream lzstream;
266 /* Decording bzip2 data. */
267 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
271 /* Decording deflate data. */
276 /* Decording PPMd data. */
278 CPpmd7 ppmd7_context;
279 CPpmd7z_RangeDec range_dec;
282 const unsigned char *next_in;
285 unsigned char *next_out;
292 /* Decoding BCJ and BCJ2 data. */
295 unsigned char odd_bcj[4];
296 /* Decoding BCJ data. */
298 uint32_t bcj_prevMask;
301 /* Decoding BCJ2 data. */
302 size_t main_stream_bytes_remaining;
303 unsigned char *sub_stream_buff[3];
304 size_t sub_stream_size[3];
305 size_t sub_stream_bytes_remaining[3];
306 unsigned char *tmp_stream_buff;
307 size_t tmp_stream_buff_size;
308 size_t tmp_stream_bytes_avail;
309 size_t tmp_stream_bytes_remaining;
311 #define CProb uint32_t
313 #define CProb uint16_t
315 CProb bcj2_p[256 + 2];
316 uint8_t bcj2_prevByte;
319 uint64_t bcj2_outPos;
321 /* Filename character-set conversion data. */
322 struct archive_string_conv *sconv;
324 char format_name[64];
327 static int archive_read_format_7zip_bid(struct archive_read *, int);
328 static int archive_read_format_7zip_cleanup(struct archive_read *);
329 static int archive_read_format_7zip_read_data(struct archive_read *,
330 const void **, size_t *, int64_t *);
331 static int archive_read_format_7zip_read_data_skip(struct archive_read *);
332 static int archive_read_format_7zip_read_header(struct archive_read *,
333 struct archive_entry *);
334 static int check_7zip_header_in_sfx(const char *);
335 static unsigned long decode_codec_id(const unsigned char *, size_t);
336 static int decode_encoded_header_info(struct archive_read *,
337 struct _7z_stream_info *);
338 static int decompress(struct archive_read *, struct _7zip *,
339 void *, size_t *, const void *, size_t *);
340 static ssize_t extract_pack_stream(struct archive_read *, size_t);
341 static void fileTimeToUtc(uint64_t, time_t *, long *);
342 static uint64_t folder_uncompressed_size(struct _7z_folder *);
343 static void free_CodersInfo(struct _7z_coders_info *);
344 static void free_Digest(struct _7z_digests *);
345 static void free_Folder(struct _7z_folder *);
346 static void free_Header(struct _7z_header_info *);
347 static void free_PackInfo(struct _7z_pack_info *);
348 static void free_StreamsInfo(struct _7z_stream_info *);
349 static void free_SubStreamsInfo(struct _7z_substream_info *);
350 static int free_decompression(struct archive_read *, struct _7zip *);
351 static ssize_t get_uncompressed_data(struct archive_read *, const void **,
353 static const unsigned char * header_bytes(struct archive_read *, size_t);
354 static int init_decompression(struct archive_read *, struct _7zip *,
355 const struct _7z_coder *, const struct _7z_coder *);
356 static int parse_7zip_uint64(struct archive_read *, uint64_t *);
357 static int read_Bools(struct archive_read *, unsigned char *, size_t);
358 static int read_CodersInfo(struct archive_read *,
359 struct _7z_coders_info *);
360 static int read_Digests(struct archive_read *, struct _7z_digests *,
362 static int read_Folder(struct archive_read *, struct _7z_folder *);
363 static int read_Header(struct archive_read *, struct _7z_header_info *,
365 static int read_PackInfo(struct archive_read *, struct _7z_pack_info *);
366 static int read_StreamsInfo(struct archive_read *,
367 struct _7z_stream_info *);
368 static int read_SubStreamsInfo(struct archive_read *,
369 struct _7z_substream_info *, struct _7z_folder *, size_t);
370 static int read_Times(struct archive_read *, struct _7z_header_info *,
372 static void read_consume(struct archive_read *);
373 static ssize_t read_stream(struct archive_read *, const void **, size_t,
375 static int seek_pack(struct archive_read *);
376 static int64_t skip_stream(struct archive_read *, size_t);
377 static int skip_sfx(struct archive_read *, ssize_t);
378 static int slurp_central_directory(struct archive_read *, struct _7zip *,
379 struct _7z_header_info *);
380 static int setup_decode_folder(struct archive_read *, struct _7z_folder *,
382 static void x86_Init(struct _7zip *);
383 static size_t x86_Convert(struct _7zip *, uint8_t *, size_t);
384 static ssize_t Bcj2_Decode(struct _7zip *, uint8_t *, size_t);
388 archive_read_support_format_7zip(struct archive *_a)
390 struct archive_read *a = (struct archive_read *)_a;
394 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
395 ARCHIVE_STATE_NEW, "archive_read_support_format_7zip");
397 zip = calloc(1, sizeof(*zip));
399 archive_set_error(&a->archive, ENOMEM,
400 "Can't allocate 7zip data");
401 return (ARCHIVE_FATAL);
404 r = __archive_read_register_format(a,
407 archive_read_format_7zip_bid,
409 archive_read_format_7zip_read_header,
410 archive_read_format_7zip_read_data,
411 archive_read_format_7zip_read_data_skip,
413 archive_read_format_7zip_cleanup);
421 archive_read_format_7zip_bid(struct archive_read *a, int best_bid)
425 /* If someone has already bid more than 32, then avoid
426 trashing the look-ahead buffers with a seek. */
430 if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
433 /* If first six bytes are the 7-Zip signature,
434 * return the bid right now. */
435 if (memcmp(p, _7ZIP_SIGNATURE, 6) == 0)
439 * It may a 7-Zip SFX archive file. If first two bytes are
440 * 'M' and 'Z' available on Windows or first four bytes are
441 * "\x7F\x45LF" available on posix like system, seek the 7-Zip
442 * signature. Although we will perform a seek when reading
443 * a header, what we do not use __archive_read_seek() here is
444 * due to a bidding performance.
446 if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
447 ssize_t offset = SFX_MIN_ADDR;
448 ssize_t window = 4096;
450 while (offset + window <= (SFX_MAX_ADDR)) {
451 const char *buff = __archive_read_ahead(a,
452 offset + window, &bytes_avail);
454 /* Remaining bytes are less than window. */
461 while (p + 32 < buff + bytes_avail) {
462 int step = check_7zip_header_in_sfx(p);
474 check_7zip_header_in_sfx(const char *p)
476 switch ((unsigned char)p[5]) {
478 if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0)
481 * Test the CRC because its extraction code has 7-Zip
482 * Magic Code, so we should do this in order not to
483 * make a mis-detection.
485 if (crc32(0, (const unsigned char *)p + 12, 20)
486 != archive_le32dec(p + 8))
488 /* Hit the header! */
490 case 0x37: return (5);
491 case 0x7A: return (4);
492 case 0xBC: return (3);
493 case 0xAF: return (2);
494 case 0x27: return (1);
500 skip_sfx(struct archive_read *a, ssize_t bytes_avail)
505 ssize_t bytes, window;
508 * If bytes_avail > SFX_MIN_ADDR we do not have to call
509 * __archive_read_seek() at this time since we have
510 * alredy had enough data.
512 if (bytes_avail > SFX_MIN_ADDR)
513 __archive_read_consume(a, SFX_MIN_ADDR);
514 else if (__archive_read_seek(a, SFX_MIN_ADDR, SEEK_SET) < 0)
515 return (ARCHIVE_FATAL);
519 while (offset + window <= SFX_MAX_ADDR - SFX_MIN_ADDR) {
520 h = __archive_read_ahead(a, window, &bytes);
522 /* Remaining bytes are less than window. */
529 /* This case might happen when window == 1. */
537 * Scan ahead until we find something that looks
538 * like the 7-Zip header.
541 int step = check_7zip_header_in_sfx(p);
544 (struct _7zip *)a->format->data;
545 skip = p - (const char *)h;
546 __archive_read_consume(a, skip);
547 zip->seek_base = SFX_MIN_ADDR + offset + skip;
552 skip = p - (const char *)h;
553 __archive_read_consume(a, skip);
559 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
560 "Couldn't find out 7-Zip header");
561 return (ARCHIVE_FATAL);
565 archive_read_format_7zip_read_header(struct archive_read *a,
566 struct archive_entry *entry)
568 struct _7zip *zip = (struct _7zip *)a->format->data;
569 struct _7zip_entry *zip_entry;
570 int r, ret = ARCHIVE_OK;
572 a->archive.archive_format = ARCHIVE_FORMAT_7ZIP;
573 if (a->archive.archive_format_name == NULL)
574 a->archive.archive_format_name = "7-Zip";
576 if (zip->entries == NULL) {
577 struct _7z_header_info header;
579 memset(&header, 0, sizeof(header));
580 r = slurp_central_directory(a, zip, &header);
581 free_Header(&header);
584 zip->entries_remaining = (size_t)zip->numFiles;
585 zip->entry = zip->entries;
589 zip_entry = zip->entry;
591 if (zip->entries_remaining <= 0)
593 --zip->entries_remaining;
595 zip->entry_offset = 0;
596 zip->end_of_entry = 0;
597 zip->entry_crc32 = crc32(0, NULL, 0);
599 /* Setup a string conversion for a filename. */
600 if (zip->sconv == NULL) {
601 zip->sconv = archive_string_conversion_from_charset(
602 &a->archive, "UTF-16LE", 1);
603 if (zip->sconv == NULL)
604 return (ARCHIVE_FATAL);
607 if (archive_entry_copy_pathname_l(entry,
608 (const char *)zip_entry->utf16name,
609 zip_entry->name_len, zip->sconv) != 0) {
610 if (errno == ENOMEM) {
611 archive_set_error(&a->archive, ENOMEM,
612 "Can't allocate memory for Pathname");
613 return (ARCHIVE_FATAL);
615 archive_set_error(&a->archive,
616 ARCHIVE_ERRNO_FILE_FORMAT,
617 "Pathname cannot be converted "
618 "from %s to current locale.",
619 archive_string_conversion_charset_name(zip->sconv));
623 /* Populate some additional entry fields: */
624 archive_entry_set_mode(entry, zip_entry->mode);
625 if (zip_entry->flg & MTIME_IS_SET)
626 archive_entry_set_mtime(entry, zip_entry->mtime,
627 zip_entry->mtime_ns);
628 if (zip_entry->flg & CTIME_IS_SET)
629 archive_entry_set_ctime(entry, zip_entry->ctime,
630 zip_entry->ctime_ns);
631 if (zip_entry->flg & ATIME_IS_SET)
632 archive_entry_set_atime(entry, zip_entry->atime,
633 zip_entry->atime_ns);
634 if (zip_entry->ssIndex != (uint32_t)-1) {
635 zip->entry_bytes_remaining =
636 zip->si.ss.unpackSizes[zip_entry->ssIndex];
637 archive_entry_set_size(entry, zip->entry_bytes_remaining);
639 zip->entry_bytes_remaining = 0;
640 archive_entry_set_size(entry, 0);
643 /* If there's no body, force read_data() to return EOF immediately. */
644 if (zip->entry_bytes_remaining < 1)
645 zip->end_of_entry = 1;
647 if ((zip_entry->mode & AE_IFMT) == AE_IFLNK) {
648 unsigned char *symname = NULL;
652 * Symbolic-name is recorded as its contents. We have to
653 * read the contents at this time.
655 while (zip->entry_bytes_remaining > 0) {
661 r = archive_read_format_7zip_read_data(a, &buff,
663 if (r < ARCHIVE_WARN) {
667 mem = realloc(symname, symsize + size + 1);
670 archive_set_error(&a->archive, ENOMEM,
671 "Can't allocate memory for Symname");
672 return (ARCHIVE_FATAL);
675 memcpy(symname+symsize, buff, size);
679 /* If there is no synname, handle it as a regular
681 zip_entry->mode &= ~AE_IFMT;
682 zip_entry->mode |= AE_IFREG;
683 archive_entry_set_mode(entry, zip_entry->mode);
685 symname[symsize] = '\0';
686 archive_entry_copy_symlink(entry,
687 (const char *)symname);
690 archive_entry_set_size(entry, 0);
693 /* Set up a more descriptive format name. */
694 sprintf(zip->format_name, "7-Zip");
695 a->archive.archive_format_name = zip->format_name;
701 archive_read_format_7zip_read_data(struct archive_read *a,
702 const void **buff, size_t *size, int64_t *offset)
706 int ret = ARCHIVE_OK;
708 zip = (struct _7zip *)(a->format->data);
710 if (zip->pack_stream_bytes_unconsumed)
713 *offset = zip->entry_offset;
717 * If we hit end-of-entry last time, clean up and return
718 * ARCHIVE_EOF this time.
720 if (zip->end_of_entry)
721 return (ARCHIVE_EOF);
723 bytes = read_stream(a, buff,
724 (size_t)zip->entry_bytes_remaining, 0);
728 archive_set_error(&a->archive,
729 ARCHIVE_ERRNO_FILE_FORMAT,
730 "Truncated 7-Zip file body");
731 return (ARCHIVE_FATAL);
733 zip->entry_bytes_remaining -= bytes;
734 if (zip->entry_bytes_remaining == 0)
735 zip->end_of_entry = 1;
737 /* Update checksum */
738 if ((zip->entry->flg & CRC32_IS_SET) && bytes)
739 zip->entry_crc32 = crc32(zip->entry_crc32, *buff,
742 /* If we hit the end, swallow any end-of-data marker. */
743 if (zip->end_of_entry) {
744 /* Check computed CRC against file contents. */
745 if ((zip->entry->flg & CRC32_IS_SET) &&
746 zip->si.ss.digests[zip->entry->ssIndex] !=
748 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
749 "7-Zip bad CRC: 0x%lx should be 0x%lx",
750 (unsigned long)zip->entry_crc32,
751 (unsigned long)zip->si.ss.digests[
752 zip->entry->ssIndex]);
758 *offset = zip->entry_offset;
759 zip->entry_offset += bytes;
765 archive_read_format_7zip_read_data_skip(struct archive_read *a)
768 int64_t bytes_skipped;
770 zip = (struct _7zip *)(a->format->data);
772 if (zip->pack_stream_bytes_unconsumed)
775 /* If we've already read to end of data, we're done. */
776 if (zip->end_of_entry)
780 * If the length is at the beginning, we can skip the
781 * compressed data much more quickly.
783 bytes_skipped = skip_stream(a, (size_t)zip->entry_bytes_remaining);
784 if (bytes_skipped < 0)
785 return (ARCHIVE_FATAL);
786 zip->entry_bytes_remaining = 0;
788 /* This entry is finished and done. */
789 zip->end_of_entry = 1;
794 archive_read_format_7zip_cleanup(struct archive_read *a)
798 zip = (struct _7zip *)(a->format->data);
799 free_StreamsInfo(&(zip->si));
801 free(zip->entry_names);
802 free_decompression(a, zip);
803 free(zip->uncompressed_buffer);
804 free(zip->sub_stream_buff[0]);
805 free(zip->sub_stream_buff[1]);
806 free(zip->sub_stream_buff[2]);
807 free(zip->tmp_stream_buff);
809 (a->format->data) = NULL;
814 read_consume(struct archive_read *a)
816 struct _7zip *zip = (struct _7zip *)a->format->data;
818 if (zip->pack_stream_bytes_unconsumed) {
819 __archive_read_consume(a, zip->pack_stream_bytes_unconsumed);
820 zip->stream_offset += zip->pack_stream_bytes_unconsumed;
821 zip->pack_stream_bytes_unconsumed = 0;
828 * Set an error code and choose an error message for liblzma.
831 set_error(struct archive_read *a, int ret)
835 case LZMA_STREAM_END: /* Found end of stream. */
836 case LZMA_OK: /* Decompressor made some progress. */
839 archive_set_error(&a->archive, ENOMEM,
840 "Lzma library error: Cannot allocate memory");
842 case LZMA_MEMLIMIT_ERROR:
843 archive_set_error(&a->archive, ENOMEM,
844 "Lzma library error: Out of memory");
846 case LZMA_FORMAT_ERROR:
847 archive_set_error(&a->archive,
849 "Lzma library error: format not recognized");
851 case LZMA_OPTIONS_ERROR:
852 archive_set_error(&a->archive,
854 "Lzma library error: Invalid options");
856 case LZMA_DATA_ERROR:
857 archive_set_error(&a->archive,
859 "Lzma library error: Corrupted input data");
862 archive_set_error(&a->archive,
864 "Lzma library error: No progress is possible");
867 /* Return an error. */
868 archive_set_error(&a->archive,
870 "Lzma decompression failed: Unknown error");
878 decode_codec_id(const unsigned char *codecId, size_t id_size)
881 unsigned long id = 0;
883 for (i = 0; i < id_size; i++) {
891 ppmd_alloc(void *p, size_t size)
897 ppmd_free(void *p, void *address)
905 struct archive_read *a = ((IByteIn*)p)->a;
906 struct _7zip *zip = (struct _7zip *)(a->format->data);
909 if (zip->ppstream.avail_in == 0) {
910 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
911 "Truncated RAR file data");
912 zip->ppstream.overconsumed = 1;
915 b = *zip->ppstream.next_in++;
916 zip->ppstream.avail_in--;
917 zip->ppstream.total_in++;
921 static ISzAlloc g_szalloc = { ppmd_alloc, ppmd_free };
924 init_decompression(struct archive_read *a, struct _7zip *zip,
925 const struct _7z_coder *coder1, const struct _7z_coder *coder2)
929 zip->codec = coder1->codec;
932 switch (zip->codec) {
937 if (coder2 != NULL) {
938 if (coder2->codec != _7Z_X86 &&
939 coder2->codec != _7Z_X86_BCJ2) {
940 archive_set_error(&a->archive,
942 "Unsupported filter %lx for %lx",
943 coder2->codec, coder1->codec);
944 return (ARCHIVE_FAILED);
946 zip->codec2 = coder2->codec;
948 if (coder2->codec == _7Z_X86)
956 switch (zip->codec) {
960 case _7Z_LZMA: case _7Z_LZMA2:
962 #if LZMA_VERSION_MAJOR >= 5
963 /* Effectively disable the limiter. */
964 #define LZMA_MEMLIMIT UINT64_MAX
966 /* NOTE: This needs to check memory size which running system has. */
967 #define LZMA_MEMLIMIT (1U << 30)
970 lzma_options_delta delta_opt;
971 lzma_filter filters[LZMA_FILTERS_MAX];
972 #if LZMA_VERSION < 50000030
977 if (zip->lzstream_valid) {
978 lzma_end(&(zip->lzstream));
979 zip->lzstream_valid = 0;
983 * NOTE: liblzma incompletely handle the BCJ+LZMA compressed
984 * data made by 7-Zip because 7-Zip does not add End-Of-
985 * Payload Marker(EOPM) at the end of LZMA compressed data,
986 * and so liblzma cannot know the end of the compressed data
987 * without EOPM. So consequently liblzma will not return last
988 * three or four bytes of uncompressed data because
989 * LZMA_FILTER_X86 filter does not handle input data if its
990 * data size is less than five bytes. If liblzma detect EOPM
991 * or know the uncompressed data size, liblzma will flush out
992 * the remaining that three or four bytes of uncompressed
993 * data. That is why we have to use our converting program
994 * for BCJ+LZMA. If we were able to tell the uncompressed
995 * size to liblzma when using lzma_raw_decoder() liblzma
996 * could correctly deal with BCJ+LZMA. But unfortunately
997 * there is no way to do that.
998 * Discussion about this can be found at XZ Utils forum.
1000 if (coder2 != NULL) {
1001 zip->codec2 = coder2->codec;
1003 filters[fi].options = NULL;
1004 switch (zip->codec2) {
1006 if (zip->codec == _7Z_LZMA2) {
1007 filters[fi].id = LZMA_FILTER_X86;
1010 /* Use our filter. */
1014 /* Use our filter. */
1018 filters[fi].id = LZMA_FILTER_DELTA;
1019 memset(&delta_opt, 0, sizeof(delta_opt));
1020 delta_opt.type = LZMA_DELTA_TYPE_BYTE;
1022 filters[fi].options = &delta_opt;
1025 /* Following filters have not been tested yet. */
1027 filters[fi].id = LZMA_FILTER_POWERPC;
1031 filters[fi].id = LZMA_FILTER_IA64;
1035 filters[fi].id = LZMA_FILTER_ARM;
1039 filters[fi].id = LZMA_FILTER_ARMTHUMB;
1043 filters[fi].id = LZMA_FILTER_SPARC;
1047 archive_set_error(&a->archive,
1049 "Unexpected codec ID: %lX", zip->codec2);
1050 return (ARCHIVE_FAILED);
1054 if (zip->codec == _7Z_LZMA2)
1055 filters[fi].id = LZMA_FILTER_LZMA2;
1057 filters[fi].id = LZMA_FILTER_LZMA1;
1058 filters[fi].options = NULL;
1059 #if LZMA_VERSION < 50000030
1062 r = lzma_properties_decode(&filters[fi], NULL,
1063 coder1->properties, (size_t)coder1->propertiesSize);
1066 return (ARCHIVE_FAILED);
1070 filters[fi].id = LZMA_VLI_UNKNOWN;
1071 filters[fi].options = NULL;
1072 r = lzma_raw_decoder(&(zip->lzstream), filters);
1073 #if LZMA_VERSION < 50000030
1078 return (ARCHIVE_FAILED);
1080 zip->lzstream_valid = 1;
1081 zip->lzstream.total_in = 0;
1082 zip->lzstream.total_out = 0;
1086 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1087 "LZMA codec is unsupported");
1088 return (ARCHIVE_FAILED);
1091 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1092 if (zip->bzstream_valid) {
1093 BZ2_bzDecompressEnd(&(zip->bzstream));
1094 zip->bzstream_valid = 0;
1096 r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 0);
1097 if (r == BZ_MEM_ERROR)
1098 r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 1);
1100 int err = ARCHIVE_ERRNO_MISC;
1101 const char *detail = NULL;
1103 case BZ_PARAM_ERROR:
1104 detail = "invalid setup parameter";
1108 detail = "out of memory";
1110 case BZ_CONFIG_ERROR:
1111 detail = "mis-compiled library";
1114 archive_set_error(&a->archive, err,
1115 "Internal error initializing decompressor: %s",
1116 detail == NULL ? "??" : detail);
1117 zip->bzstream_valid = 0;
1118 return (ARCHIVE_FAILED);
1120 zip->bzstream_valid = 1;
1121 zip->bzstream.total_in_lo32 = 0;
1122 zip->bzstream.total_in_hi32 = 0;
1123 zip->bzstream.total_out_lo32 = 0;
1124 zip->bzstream.total_out_hi32 = 0;
1127 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1128 "BZ2 codec is unsupported");
1129 return (ARCHIVE_FAILED);
1133 if (zip->stream_valid)
1134 r = inflateReset(&(zip->stream));
1136 r = inflateInit2(&(zip->stream),
1137 -15 /* Don't check for zlib header */);
1139 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1140 "Couldn't initialize zlib stream.");
1141 return (ARCHIVE_FAILED);
1143 zip->stream_valid = 1;
1144 zip->stream.total_in = 0;
1145 zip->stream.total_out = 0;
1148 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1149 "DEFLATE codec is unsupported");
1150 return (ARCHIVE_FAILED);
1157 if (zip->ppmd7_valid) {
1158 __archive_ppmd7_functions.Ppmd7_Free(
1159 &zip->ppmd7_context, &g_szalloc);
1160 zip->ppmd7_valid = 0;
1163 if (coder1->propertiesSize < 5) {
1164 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1165 "Malformed PPMd parameter");
1166 return (ARCHIVE_FAILED);
1168 order = coder1->properties[0];
1169 msize = archive_le32dec(&(coder1->properties[1]));
1170 if (order < PPMD7_MIN_ORDER || order > PPMD7_MAX_ORDER ||
1171 msize < PPMD7_MIN_MEM_SIZE || msize > PPMD7_MAX_MEM_SIZE) {
1172 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1173 "Malformed PPMd parameter");
1174 return (ARCHIVE_FAILED);
1176 __archive_ppmd7_functions.Ppmd7_Construct(&zip->ppmd7_context);
1177 r = __archive_ppmd7_functions.Ppmd7_Alloc(
1178 &zip->ppmd7_context, msize, &g_szalloc);
1180 archive_set_error(&a->archive, ENOMEM,
1181 "Coludn't allocate memory for PPMd");
1182 return (ARCHIVE_FATAL);
1184 __archive_ppmd7_functions.Ppmd7_Init(
1185 &zip->ppmd7_context, order);
1186 __archive_ppmd7_functions.Ppmd7z_RangeDec_CreateVTable(
1188 zip->ppmd7_valid = 1;
1189 zip->ppmd7_stat = 0;
1190 zip->ppstream.overconsumed = 0;
1191 zip->ppstream.total_in = 0;
1192 zip->ppstream.total_out = 0;
1203 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1204 "Unexpected codec ID: %lX", zip->codec);
1205 return (ARCHIVE_FAILED);
1207 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1208 "Unknown codec ID: %lX", zip->codec);
1209 return (ARCHIVE_FAILED);
1212 return (ARCHIVE_OK);
1216 decompress(struct archive_read *a, struct _7zip *zip,
1217 void *buff, size_t *outbytes, const void *b, size_t *used)
1219 const uint8_t *t_next_in;
1220 uint8_t *t_next_out;
1221 size_t o_avail_in, o_avail_out;
1222 size_t t_avail_in, t_avail_out;
1223 uint8_t *bcj2_next_out;
1224 size_t bcj2_avail_out;
1225 int r, ret = ARCHIVE_OK;
1227 t_avail_in = o_avail_in = *used;
1228 t_avail_out = o_avail_out = *outbytes;
1232 if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
1235 /* Do not copy out the BCJ remaining bytes when the output
1236 * buffer size is less than five bytes. */
1237 if (o_avail_in != 0 && t_avail_out < 5 && zip->odd_bcj_size) {
1242 for (i = 0; zip->odd_bcj_size > 0 && t_avail_out; i++) {
1243 *t_next_out++ = zip->odd_bcj[i];
1245 zip->odd_bcj_size--;
1247 if (o_avail_in == 0 || t_avail_out == 0) {
1248 *used = o_avail_in - t_avail_in;
1249 *outbytes = o_avail_out - t_avail_out;
1250 if (o_avail_in == 0)
1256 bcj2_next_out = t_next_out;
1257 bcj2_avail_out = t_avail_out;
1258 if (zip->codec2 == _7Z_X86_BCJ2) {
1260 * Decord a remaining decompressed main stream for BCJ2.
1262 if (zip->tmp_stream_bytes_remaining) {
1264 size_t remaining = zip->tmp_stream_bytes_remaining;
1265 bytes = Bcj2_Decode(zip, t_next_out, t_avail_out);
1267 archive_set_error(&(a->archive),
1269 "BCJ2 conversion Failed");
1270 return (ARCHIVE_FAILED);
1272 zip->main_stream_bytes_remaining -=
1273 remaining - zip->tmp_stream_bytes_remaining;
1274 t_avail_out -= bytes;
1275 if (o_avail_in == 0 || t_avail_out == 0) {
1277 *outbytes = o_avail_out - t_avail_out;
1278 if (o_avail_in == 0 &&
1279 zip->tmp_stream_bytes_remaining)
1283 t_next_out += bytes;
1284 bcj2_next_out = t_next_out;
1285 bcj2_avail_out = t_avail_out;
1287 t_next_out = zip->tmp_stream_buff;
1288 t_avail_out = zip->tmp_stream_buff_size;
1291 switch (zip->codec) {
1295 (t_avail_in > t_avail_out)?t_avail_out:t_avail_in;
1297 memcpy(t_next_out, t_next_in, bytes);
1298 t_avail_in -= bytes;
1299 t_avail_out -= bytes;
1300 if (o_avail_in == 0)
1305 case _7Z_LZMA: case _7Z_LZMA2:
1306 zip->lzstream.next_in = t_next_in;
1307 zip->lzstream.avail_in = t_avail_in;
1308 zip->lzstream.next_out = t_next_out;
1309 zip->lzstream.avail_out = t_avail_out;
1311 r = lzma_code(&(zip->lzstream), LZMA_RUN);
1313 case LZMA_STREAM_END: /* Found end of stream. */
1314 lzma_end(&(zip->lzstream));
1315 zip->lzstream_valid = 0;
1318 case LZMA_OK: /* Decompressor made some progress. */
1321 archive_set_error(&(a->archive),
1323 "Decompression failed(%d)",
1325 return (ARCHIVE_FAILED);
1327 t_avail_in = zip->lzstream.avail_in;
1328 t_avail_out = zip->lzstream.avail_out;
1331 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1333 zip->bzstream.next_in = (char *)(uintptr_t)t_next_in;
1334 zip->bzstream.avail_in = t_avail_in;
1335 zip->bzstream.next_out = (char *)(uintptr_t)t_next_out;
1336 zip->bzstream.avail_out = t_avail_out;
1337 r = BZ2_bzDecompress(&(zip->bzstream));
1339 case BZ_STREAM_END: /* Found end of stream. */
1340 switch (BZ2_bzDecompressEnd(&(zip->bzstream))) {
1344 archive_set_error(&(a->archive),
1346 "Failed to clean up decompressor");
1347 return (ARCHIVE_FAILED);
1349 zip->bzstream_valid = 0;
1352 case BZ_OK: /* Decompressor made some progress. */
1355 archive_set_error(&(a->archive),
1357 "bzip decompression failed");
1358 return (ARCHIVE_FAILED);
1360 t_avail_in = zip->bzstream.avail_in;
1361 t_avail_out = zip->bzstream.avail_out;
1366 zip->stream.next_in = (Bytef *)(uintptr_t)t_next_in;
1367 zip->stream.avail_in = (uInt)t_avail_in;
1368 zip->stream.next_out = t_next_out;
1369 zip->stream.avail_out = (uInt)t_avail_out;
1370 r = inflate(&(zip->stream), 0);
1372 case Z_STREAM_END: /* Found end of stream. */
1375 case Z_OK: /* Decompressor made some progress.*/
1378 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1379 "File decompression failed (%d)", r);
1380 return (ARCHIVE_FAILED);
1382 t_avail_in = zip->stream.avail_in;
1383 t_avail_out = zip->stream.avail_out;
1388 uint64_t flush_bytes;
1390 if (!zip->ppmd7_valid || zip->ppmd7_stat < 0 ||
1392 archive_set_error(&(a->archive),
1394 "Decompression internal error");
1395 return (ARCHIVE_FAILED);
1397 zip->ppstream.next_in = t_next_in;
1398 zip->ppstream.avail_in = t_avail_in;
1399 zip->ppstream.next_out = t_next_out;
1400 zip->ppstream.avail_out = t_avail_out;
1401 if (zip->ppmd7_stat == 0) {
1403 zip->bytein.Read = &ppmd_read;
1404 zip->range_dec.Stream = &zip->bytein;
1405 r = __archive_ppmd7_functions.Ppmd7z_RangeDec_Init(
1408 zip->ppmd7_stat = -1;
1409 archive_set_error(&a->archive,
1411 "Failed to initialize PPMd range decorder");
1412 return (ARCHIVE_FAILED);
1414 if (zip->ppstream.overconsumed) {
1415 zip->ppmd7_stat = -1;
1416 return (ARCHIVE_FAILED);
1418 zip->ppmd7_stat = 1;
1421 if (t_avail_in == 0)
1422 /* XXX Flush out remaining decoded data XXX */
1423 flush_bytes = zip->folder_outbytes_remaining;
1430 sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1431 &(zip->ppmd7_context), &(zip->range_dec.p));
1433 zip->ppmd7_stat = -1;
1434 archive_set_error(&a->archive,
1435 ARCHIVE_ERRNO_FILE_FORMAT,
1436 "Failed to decode PPMd");
1437 return (ARCHIVE_FAILED);
1439 if (zip->ppstream.overconsumed) {
1440 zip->ppmd7_stat = -1;
1441 return (ARCHIVE_FAILED);
1443 *zip->ppstream.next_out++ = (unsigned char)sym;
1444 zip->ppstream.avail_out--;
1445 zip->ppstream.total_out++;
1448 } while (zip->ppstream.avail_out &&
1449 (zip->ppstream.avail_in || flush_bytes));
1451 t_avail_in = (size_t)zip->ppstream.avail_in;
1452 t_avail_out = (size_t)zip->ppstream.avail_out;
1456 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1457 "Decompression internal error");
1458 return (ARCHIVE_FAILED);
1460 if (ret != ARCHIVE_OK && ret != ARCHIVE_EOF)
1463 *used = o_avail_in - t_avail_in;
1464 *outbytes = o_avail_out - t_avail_out;
1469 if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
1470 size_t l = x86_Convert(zip, buff, *outbytes);
1471 zip->odd_bcj_size = *outbytes - l;
1472 if (zip->odd_bcj_size > 0 && zip->odd_bcj_size <= 4 &&
1473 o_avail_in && ret != ARCHIVE_EOF) {
1474 memcpy(zip->odd_bcj, ((unsigned char *)buff) + l,
1478 zip->odd_bcj_size = 0;
1482 * Decord BCJ2 with a decompressed main stream.
1484 if (zip->codec2 == _7Z_X86_BCJ2) {
1487 zip->tmp_stream_bytes_avail =
1488 zip->tmp_stream_buff_size - t_avail_out;
1489 if (zip->tmp_stream_bytes_avail >
1490 zip->main_stream_bytes_remaining)
1491 zip->tmp_stream_bytes_avail =
1492 zip->main_stream_bytes_remaining;
1493 zip->tmp_stream_bytes_remaining = zip->tmp_stream_bytes_avail;
1494 bytes = Bcj2_Decode(zip, bcj2_next_out, bcj2_avail_out);
1496 archive_set_error(&(a->archive),
1497 ARCHIVE_ERRNO_MISC, "BCJ2 conversion Failed");
1498 return (ARCHIVE_FAILED);
1500 zip->main_stream_bytes_remaining -=
1501 zip->tmp_stream_bytes_avail
1502 - zip->tmp_stream_bytes_remaining;
1503 bcj2_avail_out -= bytes;
1504 *outbytes = o_avail_out - bcj2_avail_out;
1511 free_decompression(struct archive_read *a, struct _7zip *zip)
1515 #if !defined(HAVE_ZLIB_H) &&\
1516 !(defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR))
1517 (void)a;/* UNUSED */
1520 if (zip->lzstream_valid)
1521 lzma_end(&(zip->lzstream));
1523 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1524 if (zip->bzstream_valid) {
1525 if (BZ2_bzDecompressEnd(&(zip->bzstream)) != BZ_OK) {
1526 archive_set_error(&a->archive,
1528 "Failed to clean up bzip2 decompressor");
1531 zip->bzstream_valid = 0;
1535 if (zip->stream_valid) {
1536 if (inflateEnd(&(zip->stream)) != Z_OK) {
1537 archive_set_error(&a->archive,
1539 "Failed to clean up zlib decompressor");
1542 zip->stream_valid = 0;
1545 if (zip->ppmd7_valid) {
1546 __archive_ppmd7_functions.Ppmd7_Free(
1547 &zip->ppmd7_context, &g_szalloc);
1548 zip->ppmd7_valid = 0;
1554 parse_7zip_uint64(struct archive_read *a, uint64_t *val)
1556 const unsigned char *p;
1557 unsigned char avail, mask;
1560 if ((p = header_bytes(a, 1)) == NULL)
1565 for (i = 0; i < 8; i++) {
1567 if ((p = header_bytes(a, 1)) == NULL)
1569 *val |= ((uint64_t)*p) << (8 * i);
1573 *val += (avail & (mask -1)) << (8 * i);
1580 read_Bools(struct archive_read *a, unsigned char *data, size_t num)
1582 const unsigned char *p;
1583 unsigned i, mask = 0, avail = 0;
1585 for (i = 0; i < num; i++) {
1587 if ((p = header_bytes(a, 1)) == NULL)
1592 data[i] = (avail & mask)?1:0;
1599 free_Digest(struct _7z_digests *d)
1606 read_Digests(struct archive_read *a, struct _7z_digests *d, size_t num)
1608 const unsigned char *p;
1613 memset(d, 0, sizeof(*d));
1615 d->defineds = malloc(num);
1616 if (d->defineds == NULL)
1621 if ((p = header_bytes(a, 1)) == NULL)
1624 if (read_Bools(a, d->defineds, num) < 0)
1627 /* All are defined */
1628 memset(d->defineds, 1, num);
1630 d->digests = calloc(num, sizeof(*d->digests));
1631 if (d->digests == NULL)
1633 for (i = 0; i < num; i++) {
1634 if (d->defineds[i]) {
1635 if ((p = header_bytes(a, 4)) == NULL)
1637 d->digests[i] = archive_le32dec(p);
1645 free_PackInfo(struct _7z_pack_info *pi)
1648 free(pi->positions);
1649 free_Digest(&(pi->digest));
1653 read_PackInfo(struct archive_read *a, struct _7z_pack_info *pi)
1655 const unsigned char *p;
1658 memset(pi, 0, sizeof(*pi));
1663 if (parse_7zip_uint64(a, &(pi->pos)) < 0)
1667 * Read NumPackStreams.
1669 if (parse_7zip_uint64(a, &(pi->numPackStreams)) < 0)
1671 if (pi->numPackStreams == 0)
1673 if (1000000 < pi->numPackStreams)
1677 * Read PackSizes[num]
1679 if ((p = header_bytes(a, 1)) == NULL)
1682 /* PackSizes[num] are not present. */
1686 pi->sizes = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
1687 pi->positions = calloc((size_t)pi->numPackStreams, sizeof(uint64_t));
1688 if (pi->sizes == NULL || pi->positions == NULL)
1691 for (i = 0; i < pi->numPackStreams; i++) {
1692 if (parse_7zip_uint64(a, &(pi->sizes[i])) < 0)
1697 * Read PackStreamDigests[num]
1699 if ((p = header_bytes(a, 1)) == NULL)
1702 /* PackStreamDigests[num] are not present. */
1703 pi->digest.defineds =
1704 calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.defineds));
1705 pi->digest.digests =
1706 calloc((size_t)pi->numPackStreams, sizeof(*pi->digest.digests));
1707 if (pi->digest.defineds == NULL || pi->digest.digests == NULL)
1715 if (read_Digests(a, &(pi->digest), (size_t)pi->numPackStreams) < 0)
1719 * Must be marked by kEnd.
1721 if ((p = header_bytes(a, 1)) == NULL)
1729 free_Folder(struct _7z_folder *f)
1734 for (i = 0; i< f->numCoders; i++) {
1735 free(f->coders[i].properties);
1740 free(f->packedStreams);
1741 free(f->unPackSize);
1745 read_Folder(struct archive_read *a, struct _7z_folder *f)
1747 struct _7zip *zip = (struct _7zip *)a->format->data;
1748 const unsigned char *p;
1749 uint64_t numInStreamsTotal = 0;
1750 uint64_t numOutStreamsTotal = 0;
1753 memset(f, 0, sizeof(*f));
1758 if (parse_7zip_uint64(a, &(f->numCoders)) < 0)
1760 if (f->numCoders > 4)
1761 /* Too many coders. */
1764 f->coders = calloc((size_t)f->numCoders, sizeof(*f->coders));
1765 if (f->coders == NULL)
1767 for (i = 0; i< f->numCoders; i++) {
1771 if ((p = header_bytes(a, 1)) == NULL)
1777 * 5: 0 - No Attributes
1778 * 1 - There are Attributes;
1781 codec_size = *p & 0xf;
1782 simple = (*p & 0x10)?0:1;
1785 return (-1);/* Not supported. */
1788 * Read Decompression Method IDs.
1790 if ((p = header_bytes(a, codec_size)) == NULL)
1793 f->coders[i].codec = decode_codec_id(p, codec_size);
1796 f->coders[i].numInStreams = 1;
1797 f->coders[i].numOutStreams = 1;
1799 if (parse_7zip_uint64(
1800 a, &(f->coders[i].numInStreams)) < 0)
1802 if (1000000 < f->coders[i].numInStreams)
1804 if (parse_7zip_uint64(
1805 a, &(f->coders[i].numOutStreams)) < 0)
1807 if (1000000 < f->coders[i].numOutStreams)
1812 if (parse_7zip_uint64(
1813 a, &(f->coders[i].propertiesSize)) < 0)
1815 if ((p = header_bytes(
1816 a, (size_t)f->coders[i].propertiesSize)) == NULL)
1818 f->coders[i].properties =
1819 malloc((size_t)f->coders[i].propertiesSize);
1820 if (f->coders[i].properties == NULL)
1822 memcpy(f->coders[i].properties, p,
1823 (size_t)f->coders[i].propertiesSize);
1826 numInStreamsTotal += f->coders[i].numInStreams;
1827 numOutStreamsTotal += f->coders[i].numOutStreams;
1830 if (numOutStreamsTotal == 0 ||
1831 numInStreamsTotal < numOutStreamsTotal-1)
1834 f->numBindPairs = numOutStreamsTotal - 1;
1835 if (zip->header_bytes_remaining < f->numBindPairs)
1837 if (f->numBindPairs > 0) {
1839 calloc((size_t)f->numBindPairs, sizeof(*f->bindPairs));
1840 if (f->bindPairs == NULL)
1843 f->bindPairs = NULL;
1844 for (i = 0; i < f->numBindPairs; i++) {
1845 if (parse_7zip_uint64(a, &(f->bindPairs[i].inIndex)) < 0)
1847 if (1000000 < f->bindPairs[i].inIndex)
1849 if (parse_7zip_uint64(a, &(f->bindPairs[i].outIndex)) < 0)
1851 if (1000000 < f->bindPairs[i].outIndex)
1855 f->numPackedStreams = numInStreamsTotal - f->numBindPairs;
1857 calloc((size_t)f->numPackedStreams, sizeof(*f->packedStreams));
1858 if (f->packedStreams == NULL)
1860 if (f->numPackedStreams == 1) {
1861 for (i = 0; i < numInStreamsTotal; i++) {
1863 for (j = 0; j < f->numBindPairs; j++) {
1864 if (f->bindPairs[j].inIndex == i)
1867 if (j == f->numBindPairs)
1870 if (i == numInStreamsTotal)
1872 f->packedStreams[0] = i;
1874 for (i = 0; i < f->numPackedStreams; i++) {
1875 if (parse_7zip_uint64(a, &(f->packedStreams[i])) < 0)
1877 if (1000000 < f->packedStreams[i])
1881 f->numInStreams = numInStreamsTotal;
1882 f->numOutStreams = numOutStreamsTotal;
1888 free_CodersInfo(struct _7z_coders_info *ci)
1893 for (i = 0; i < ci->numFolders; i++)
1894 free_Folder(&(ci->folders[i]));
1900 read_CodersInfo(struct archive_read *a, struct _7z_coders_info *ci)
1902 const unsigned char *p;
1903 struct _7z_digests digest;
1906 memset(ci, 0, sizeof(*ci));
1907 memset(&digest, 0, sizeof(digest));
1909 if ((p = header_bytes(a, 1)) == NULL)
1917 if (parse_7zip_uint64(a, &(ci->numFolders)) < 0)
1919 if (1000000 < ci->numFolders)
1925 if ((p = header_bytes(a, 1)) == NULL)
1930 calloc((size_t)ci->numFolders, sizeof(*ci->folders));
1931 if (ci->folders == NULL)
1933 for (i = 0; i < ci->numFolders; i++) {
1934 if (read_Folder(a, &(ci->folders[i])) < 0)
1939 if (parse_7zip_uint64(a, &(ci->dataStreamIndex)) < 0)
1941 if (1000000 < ci->dataStreamIndex)
1946 if ((p = header_bytes(a, 1)) == NULL)
1948 if (*p != kCodersUnPackSize)
1951 for (i = 0; i < ci->numFolders; i++) {
1952 struct _7z_folder *folder = &(ci->folders[i]);
1955 folder->unPackSize =
1956 calloc((size_t)folder->numOutStreams, sizeof(*folder->unPackSize));
1957 if (folder->unPackSize == NULL)
1959 for (j = 0; j < folder->numOutStreams; j++) {
1960 if (parse_7zip_uint64(a, &(folder->unPackSize[j])) < 0)
1968 if ((p = header_bytes(a, 1)) == NULL)
1974 if (read_Digests(a, &digest, (size_t)ci->numFolders) < 0)
1976 for (i = 0; i < ci->numFolders; i++) {
1977 ci->folders[i].digest_defined = digest.defineds[i];
1978 ci->folders[i].digest = digest.digests[i];
1984 if ((p = header_bytes(a, 1)) == NULL)
1988 free_Digest(&digest);
1991 free_Digest(&digest);
1996 folder_uncompressed_size(struct _7z_folder *f)
1998 int n = (int)f->numOutStreams;
1999 unsigned pairs = (unsigned)f->numBindPairs;
2003 for (i = 0; i < pairs; i++) {
2004 if (f->bindPairs[i].outIndex == (uint64_t)n)
2008 return (f->unPackSize[n]);
2014 free_SubStreamsInfo(struct _7z_substream_info *ss)
2016 free(ss->unpackSizes);
2017 free(ss->digestsDefined);
2022 read_SubStreamsInfo(struct archive_read *a, struct _7z_substream_info *ss,
2023 struct _7z_folder *f, size_t numFolders)
2025 const unsigned char *p;
2027 size_t unpack_streams;
2030 uint32_t numDigests;
2032 memset(ss, 0, sizeof(*ss));
2034 for (i = 0; i < numFolders; i++)
2035 f[i].numUnpackStreams = 1;
2037 if ((p = header_bytes(a, 1)) == NULL)
2041 if (type == kNumUnPackStream) {
2043 for (i = 0; i < numFolders; i++) {
2044 if (parse_7zip_uint64(a, &(f[i].numUnpackStreams)) < 0)
2046 if (1000000 < f[i].numUnpackStreams)
2048 unpack_streams += (size_t)f[i].numUnpackStreams;
2050 if ((p = header_bytes(a, 1)) == NULL)
2054 unpack_streams = numFolders;
2056 ss->unpack_streams = unpack_streams;
2057 if (unpack_streams) {
2058 ss->unpackSizes = calloc(unpack_streams,
2059 sizeof(*ss->unpackSizes));
2060 ss->digestsDefined = calloc(unpack_streams,
2061 sizeof(*ss->digestsDefined));
2062 ss->digests = calloc(unpack_streams,
2063 sizeof(*ss->digests));
2064 if (ss->unpackSizes == NULL || ss->digestsDefined == NULL ||
2065 ss->digests == NULL)
2069 usizes = ss->unpackSizes;
2070 for (i = 0; i < numFolders; i++) {
2074 if (f[i].numUnpackStreams == 0)
2078 if (type == kSize) {
2079 for (pack = 1; pack < f[i].numUnpackStreams; pack++) {
2080 if (parse_7zip_uint64(a, usizes) < 0)
2085 *usizes++ = folder_uncompressed_size(&f[i]) - sum;
2088 if (type == kSize) {
2089 if ((p = header_bytes(a, 1)) == NULL)
2094 for (i = 0; i < unpack_streams; i++) {
2095 ss->digestsDefined[i] = 0;
2100 for (i = 0; i < numFolders; i++) {
2101 if (f[i].numUnpackStreams != 1 || !f[i].digest_defined)
2102 numDigests += (uint32_t)f[i].numUnpackStreams;
2106 struct _7z_digests tmpDigests;
2107 unsigned char *digestsDefined = ss->digestsDefined;
2108 uint32_t * digests = ss->digests;
2111 memset(&tmpDigests, 0, sizeof(tmpDigests));
2112 if (read_Digests(a, &(tmpDigests), numDigests) < 0) {
2113 free_Digest(&tmpDigests);
2116 for (i = 0; i < numFolders; i++) {
2117 if (f[i].numUnpackStreams == 1 && f[i].digest_defined) {
2118 *digestsDefined++ = 1;
2119 *digests++ = f[i].digest;
2123 for (j = 0; j < f[i].numUnpackStreams;
2126 tmpDigests.defineds[di];
2128 tmpDigests.digests[di];
2132 free_Digest(&tmpDigests);
2133 if ((p = header_bytes(a, 1)) == NULL)
2147 free_StreamsInfo(struct _7z_stream_info *si)
2149 free_PackInfo(&(si->pi));
2150 free_CodersInfo(&(si->ci));
2151 free_SubStreamsInfo(&(si->ss));
2155 read_StreamsInfo(struct archive_read *a, struct _7z_stream_info *si)
2157 struct _7zip *zip = (struct _7zip *)a->format->data;
2158 const unsigned char *p;
2161 memset(si, 0, sizeof(*si));
2163 if ((p = header_bytes(a, 1)) == NULL)
2165 if (*p == kPackInfo) {
2168 if (read_PackInfo(a, &(si->pi)) < 0)
2171 if (si->pi.positions == NULL || si->pi.sizes == NULL)
2174 * Calculate packed stream positions.
2176 packPos = si->pi.pos;
2177 for (i = 0; i < si->pi.numPackStreams; i++) {
2178 si->pi.positions[i] = packPos;
2179 packPos += si->pi.sizes[i];
2180 if (packPos > zip->header_offset)
2183 if ((p = header_bytes(a, 1)) == NULL)
2186 if (*p == kUnPackInfo) {
2188 struct _7z_folder *f;
2190 if (read_CodersInfo(a, &(si->ci)) < 0)
2194 * Calculate packed stream indexes.
2198 for (i = 0; i < si->ci.numFolders; i++) {
2199 f[i].packIndex = packIndex;
2200 packIndex += (uint32_t)f[i].numPackedStreams;
2201 if (packIndex > si->pi.numPackStreams)
2204 if ((p = header_bytes(a, 1)) == NULL)
2208 if (*p == kSubStreamsInfo) {
2209 if (read_SubStreamsInfo(a, &(si->ss),
2210 si->ci.folders, (size_t)si->ci.numFolders) < 0)
2212 if ((p = header_bytes(a, 1)) == NULL)
2225 free_Header(struct _7z_header_info *h)
2227 free(h->emptyStreamBools);
2228 free(h->emptyFileBools);
2234 read_Header(struct archive_read *a, struct _7z_header_info *h,
2235 int check_header_id)
2237 struct _7zip *zip = (struct _7zip *)a->format->data;
2238 const unsigned char *p;
2239 struct _7z_folder *folders;
2240 struct _7z_stream_info *si = &(zip->si);
2241 struct _7zip_entry *entries;
2242 uint32_t folderIndex, indexInFolder;
2244 int eindex, empty_streams, sindex;
2246 if (check_header_id) {
2250 if ((p = header_bytes(a, 1)) == NULL)
2257 * Read ArchiveProperties.
2259 if ((p = header_bytes(a, 1)) == NULL)
2261 if (*p == kArchiveProperties) {
2264 if ((p = header_bytes(a, 1)) == NULL)
2268 if (parse_7zip_uint64(a, &size) < 0)
2271 if ((p = header_bytes(a, 1)) == NULL)
2276 * Read MainStreamsInfo.
2278 if (*p == kMainStreamsInfo) {
2279 if (read_StreamsInfo(a, &(zip->si)) < 0)
2281 if ((p = header_bytes(a, 1)) == NULL)
2290 if (*p != kFilesInfo)
2293 if (parse_7zip_uint64(a, &(zip->numFiles)) < 0)
2295 if (1000000 < zip->numFiles)
2298 zip->entries = calloc((size_t)zip->numFiles, sizeof(*zip->entries));
2299 if (zip->entries == NULL)
2301 entries = zip->entries;
2309 if ((p = header_bytes(a, 1)) == NULL)
2315 if (parse_7zip_uint64(a, &size) < 0)
2317 if (zip->header_bytes_remaining < size)
2323 h->emptyStreamBools = calloc((size_t)zip->numFiles,
2324 sizeof(*h->emptyStreamBools));
2325 if (h->emptyStreamBools == NULL)
2328 a, h->emptyStreamBools, (size_t)zip->numFiles) < 0)
2331 for (i = 0; i < zip->numFiles; i++) {
2332 if (h->emptyStreamBools[i])
2337 if (empty_streams <= 0) {
2338 /* Unexcepted sequence. Skip this. */
2339 if (header_bytes(a, ll) == NULL)
2343 h->emptyFileBools = calloc(empty_streams,
2344 sizeof(*h->emptyFileBools));
2345 if (h->emptyFileBools == NULL)
2347 if (read_Bools(a, h->emptyFileBools, empty_streams) < 0)
2351 if (empty_streams <= 0) {
2352 /* Unexcepted sequence. Skip this. */
2353 if (header_bytes(a, ll) == NULL)
2357 h->antiBools = calloc(empty_streams,
2358 sizeof(*h->antiBools));
2359 if (h->antiBools == NULL)
2361 if (read_Bools(a, h->antiBools, empty_streams) < 0)
2367 if (read_Times(a, h, type) < 0)
2375 /* Skip one byte. */
2376 if ((p = header_bytes(a, 1)) == NULL)
2380 if ((ll & 1) || ll < zip->numFiles * 4)
2383 zip->entry_names = malloc(ll);
2384 if (zip->entry_names == NULL)
2386 np = zip->entry_names;
2389 * Copy whole file names.
2390 * NOTE: This loop prevents from expanding
2391 * the uncompressed buffer in order not to
2392 * use extra memory resource.
2396 if (nb > UBUFF_SIZE)
2400 if ((p = header_bytes(a, b)) == NULL)
2406 np = zip->entry_names;
2409 for (i = 0; i < zip->numFiles; i++) {
2410 entries[i].utf16name = np;
2411 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2412 entries[i].wname = (wchar_t *)np;
2415 /* Find a terminator. */
2416 while (nl >= 2 && (np[0] || np[1])) {
2421 return (-1);/* Terminator not found */
2422 entries[i].name_len = np - entries[i].utf16name;
2432 if ((p = header_bytes(a, 2)) == NULL)
2435 h->attrBools = calloc((size_t)zip->numFiles,
2436 sizeof(*h->attrBools));
2437 if (h->attrBools == NULL)
2440 memset(h->attrBools, 1, (size_t)zip->numFiles);
2442 if (read_Bools(a, h->attrBools,
2443 (size_t)zip->numFiles) < 0)
2446 for (i = 0; i < zip->numFiles; i++) {
2447 if (h->attrBools[i]) {
2448 if ((p = header_bytes(a, 4)) == NULL)
2450 entries[i].attr = archive_le32dec(p);
2456 if (header_bytes(a, ll) == NULL)
2463 * Set up entry's attributes.
2465 folders = si->ci.folders;
2466 eindex = sindex = 0;
2467 folderIndex = indexInFolder = 0;
2468 for (i = 0; i < zip->numFiles; i++) {
2469 if (h->emptyStreamBools == NULL || h->emptyStreamBools[i] == 0)
2470 entries[i].flg |= HAS_STREAM;
2471 /* The high 16 bits of attributes is a posix file mode. */
2472 entries[i].mode = entries[i].attr >> 16;
2473 if (entries[i].flg & HAS_STREAM) {
2474 if ((size_t)sindex >= si->ss.unpack_streams)
2476 if (entries[i].mode == 0)
2477 entries[i].mode = AE_IFREG | 0666;
2478 if (si->ss.digestsDefined[sindex])
2479 entries[i].flg |= CRC32_IS_SET;
2480 entries[i].ssIndex = sindex;
2484 if (h->emptyFileBools == NULL)
2487 if (h->emptyFileBools[eindex])
2493 if (entries[i].mode == 0) {
2495 entries[i].mode = AE_IFDIR | 0777;
2497 entries[i].mode = AE_IFREG | 0666;
2499 (entries[i].mode & AE_IFMT) != AE_IFDIR) {
2500 entries[i].mode &= ~AE_IFMT;
2501 entries[i].mode |= AE_IFDIR;
2503 if ((entries[i].mode & AE_IFMT) == AE_IFDIR &&
2504 entries[i].name_len >= 2 &&
2505 (entries[i].utf16name[entries[i].name_len-2] != '/' ||
2506 entries[i].utf16name[entries[i].name_len-1] != 0)) {
2507 entries[i].utf16name[entries[i].name_len] = '/';
2508 entries[i].utf16name[entries[i].name_len+1] = 0;
2509 entries[i].name_len += 2;
2511 entries[i].ssIndex = -1;
2513 if (entries[i].attr & 0x01)
2514 entries[i].mode &= ~0222;/* Read only. */
2516 if ((entries[i].flg & HAS_STREAM) == 0 && indexInFolder == 0) {
2518 * The entry is an empty file or a directory file,
2519 * those both have no contents.
2521 entries[i].folderIndex = -1;
2524 if (indexInFolder == 0) {
2526 if (folderIndex >= si->ci.numFolders)
2528 if (folders[folderIndex].numUnpackStreams)
2533 entries[i].folderIndex = folderIndex;
2534 if ((entries[i].flg & HAS_STREAM) == 0)
2537 if (indexInFolder >= folders[folderIndex].numUnpackStreams) {
2546 #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
2548 fileTimeToUtc(uint64_t fileTime, time_t *timep, long *ns)
2551 if (fileTime >= EPOC_TIME) {
2552 fileTime -= EPOC_TIME;
2553 /* milli seconds base */
2554 *timep = (time_t)(fileTime / 10000000);
2555 /* nano seconds base */
2556 *ns = (long)(fileTime % 10000000) * 100;
2564 read_Times(struct archive_read *a, struct _7z_header_info *h, int type)
2566 struct _7zip *zip = (struct _7zip *)a->format->data;
2567 const unsigned char *p;
2568 struct _7zip_entry *entries = zip->entries;
2569 unsigned char *timeBools;
2573 timeBools = calloc((size_t)zip->numFiles, sizeof(*timeBools));
2574 if (timeBools == NULL)
2577 /* Read allAreDefined. */
2578 if ((p = header_bytes(a, 1)) == NULL)
2582 memset(timeBools, 1, (size_t)zip->numFiles);
2584 if (read_Bools(a, timeBools, (size_t)zip->numFiles) < 0)
2588 /* Read external. */
2589 if ((p = header_bytes(a, 1)) == NULL)
2592 if (parse_7zip_uint64(a, &(h->dataIndex)) < 0)
2594 if (1000000 < h->dataIndex)
2598 for (i = 0; i < zip->numFiles; i++) {
2601 if ((p = header_bytes(a, 8)) == NULL)
2605 fileTimeToUtc(archive_le64dec(p),
2606 &(entries[i].ctime),
2607 &(entries[i].ctime_ns));
2608 entries[i].flg |= CTIME_IS_SET;
2611 fileTimeToUtc(archive_le64dec(p),
2612 &(entries[i].atime),
2613 &(entries[i].atime_ns));
2614 entries[i].flg |= ATIME_IS_SET;
2617 fileTimeToUtc(archive_le64dec(p),
2618 &(entries[i].mtime),
2619 &(entries[i].mtime_ns));
2620 entries[i].flg |= MTIME_IS_SET;
2633 decode_encoded_header_info(struct archive_read *a, struct _7z_stream_info *si)
2635 struct _7zip *zip = (struct _7zip *)a->format->data;
2638 if (read_StreamsInfo(a, si) < 0) {
2639 if (errno == ENOMEM)
2640 archive_set_error(&a->archive, -1,
2641 "Couldn't allocate memory");
2643 archive_set_error(&a->archive, -1,
2644 "Malformed 7-Zip archive");
2645 return (ARCHIVE_FATAL);
2648 if (si->pi.numPackStreams == 0 || si->ci.numFolders == 0) {
2649 archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
2650 return (ARCHIVE_FATAL);
2653 if (zip->header_offset < si->pi.pos + si->pi.sizes[0] ||
2654 (int64_t)(si->pi.pos + si->pi.sizes[0]) < 0 ||
2655 si->pi.sizes[0] == 0 || (int64_t)si->pi.pos < 0) {
2656 archive_set_error(&a->archive, -1, "Malformed Header offset");
2657 return (ARCHIVE_FATAL);
2660 return (ARCHIVE_OK);
2663 static const unsigned char *
2664 header_bytes(struct archive_read *a, size_t rbytes)
2666 struct _7zip *zip = (struct _7zip *)a->format->data;
2667 const unsigned char *p;
2669 if (zip->header_bytes_remaining < rbytes)
2671 if (zip->pack_stream_bytes_unconsumed)
2674 if (zip->header_is_encoded == 0) {
2675 p = __archive_read_ahead(a, rbytes, NULL);
2678 zip->header_bytes_remaining -= rbytes;
2679 zip->pack_stream_bytes_unconsumed = rbytes;
2684 bytes = read_stream(a, &buff, rbytes, rbytes);
2687 zip->header_bytes_remaining -= bytes;
2691 /* Update checksum */
2692 zip->header_crc32 = crc32(zip->header_crc32, p, (unsigned)rbytes);
2697 slurp_central_directory(struct archive_read *a, struct _7zip *zip,
2698 struct _7z_header_info *header)
2700 const unsigned char *p;
2701 uint64_t next_header_offset;
2702 uint64_t next_header_size;
2703 uint32_t next_header_crc;
2704 ssize_t bytes_avail;
2705 int check_header_crc, r;
2707 if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
2708 return (ARCHIVE_FATAL);
2710 if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
2711 /* This is an executable ? Must be self-extracting... */
2712 r = skip_sfx(a, bytes_avail);
2713 if (r < ARCHIVE_WARN)
2715 if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
2716 return (ARCHIVE_FATAL);
2718 zip->seek_base += 32;
2720 if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0) {
2721 archive_set_error(&a->archive, -1, "Not 7-Zip archive file");
2722 return (ARCHIVE_FATAL);
2726 if (crc32(0, (const unsigned char *)p + 12, 20)
2727 != archive_le32dec(p + 8)) {
2728 archive_set_error(&a->archive, -1, "Header CRC error");
2729 return (ARCHIVE_FATAL);
2732 next_header_offset = archive_le64dec(p + 12);
2733 next_header_size = archive_le64dec(p + 20);
2734 next_header_crc = archive_le32dec(p + 28);
2736 if (next_header_size == 0)
2737 /* There is no entry in an archive file. */
2738 return (ARCHIVE_EOF);
2740 if (((int64_t)next_header_offset) < 0) {
2741 archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
2742 return (ARCHIVE_FATAL);
2744 __archive_read_consume(a, 32);
2745 if (next_header_offset != 0) {
2746 if (bytes_avail >= (ssize_t)next_header_offset)
2747 __archive_read_consume(a, next_header_offset);
2748 else if (__archive_read_seek(a,
2749 next_header_offset + zip->seek_base, SEEK_SET) < 0)
2750 return (ARCHIVE_FATAL);
2752 zip->stream_offset = next_header_offset;
2753 zip->header_offset = next_header_offset;
2754 zip->header_bytes_remaining = next_header_size;
2755 zip->header_crc32 = 0;
2756 zip->header_is_encoded = 0;
2757 zip->header_is_being_read = 1;
2758 check_header_crc = 1;
2760 if ((p = header_bytes(a, 1)) == NULL) {
2761 archive_set_error(&a->archive,
2762 ARCHIVE_ERRNO_FILE_FORMAT,
2763 "Truncated 7-Zip file body");
2764 return (ARCHIVE_FATAL);
2766 /* Parse ArchiveProperties. */
2768 case kEncodedHeader:
2770 * The archive has an encoded header and we have to decode it
2771 * in order to parse the header correctly.
2773 r = decode_encoded_header_info(a, &(zip->si));
2775 /* Check the EncodedHeader CRC.*/
2776 if (r == 0 && zip->header_crc32 != next_header_crc) {
2777 archive_set_error(&a->archive, -1,
2778 "Damaged 7-Zip archive");
2782 if (zip->si.ci.folders[0].digest_defined)
2783 next_header_crc = zip->si.ci.folders[0].digest;
2785 check_header_crc = 0;
2786 if (zip->pack_stream_bytes_unconsumed)
2788 r = setup_decode_folder(a, zip->si.ci.folders, 1);
2790 zip->header_bytes_remaining =
2791 zip->folder_outbytes_remaining;
2795 /* Clean up StreamsInfo. */
2796 free_StreamsInfo(&(zip->si));
2797 memset(&(zip->si), 0, sizeof(zip->si));
2799 return (ARCHIVE_FATAL);
2800 zip->header_is_encoded = 1;
2801 zip->header_crc32 = 0;
2808 r = read_Header(a, header, zip->header_is_encoded);
2810 if (errno == ENOMEM)
2811 archive_set_error(&a->archive, -1,
2812 "Couldn't allocate memory");
2814 archive_set_error(&a->archive, -1,
2815 "Damaged 7-Zip archive");
2816 return (ARCHIVE_FATAL);
2822 if ((p = header_bytes(a, 1)) == NULL ||*p != kEnd) {
2823 archive_set_error(&a->archive, -1,
2824 "Malformed 7-Zip archive");
2825 return (ARCHIVE_FATAL);
2828 /* Check the Header CRC.*/
2829 if (check_header_crc && zip->header_crc32 != next_header_crc) {
2830 archive_set_error(&a->archive, -1,
2831 "Malformed 7-Zip archive");
2832 return (ARCHIVE_FATAL);
2836 archive_set_error(&a->archive, -1,
2837 "Unexpected Property ID = %X", p[0]);
2838 return (ARCHIVE_FATAL);
2841 /* Clean up variables be used for decoding the archive header */
2842 zip->pack_stream_remaining = 0;
2843 zip->pack_stream_index = 0;
2844 zip->folder_outbytes_remaining = 0;
2845 zip->uncompressed_buffer_bytes_remaining = 0;
2846 zip->pack_stream_bytes_unconsumed = 0;
2847 zip->header_is_being_read = 0;
2849 return (ARCHIVE_OK);
2853 get_uncompressed_data(struct archive_read *a, const void **buff, size_t size,
2856 struct _7zip *zip = (struct _7zip *)a->format->data;
2857 ssize_t bytes_avail;
2859 if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
2863 * Note: '1' here is a performance optimization.
2864 * Recall that the decompression layer returns a count of
2865 * available bytes; asking for more than that forces the
2866 * decompressor to combine reads by copying data.
2868 *buff = __archive_read_ahead(a, 1, &bytes_avail);
2869 if (bytes_avail <= 0) {
2870 archive_set_error(&a->archive,
2871 ARCHIVE_ERRNO_FILE_FORMAT,
2872 "Truncated 7-Zip file data");
2873 return (ARCHIVE_FATAL);
2875 if ((size_t)bytes_avail >
2876 zip->uncompressed_buffer_bytes_remaining)
2877 bytes_avail = (ssize_t)
2878 zip->uncompressed_buffer_bytes_remaining;
2879 if ((size_t)bytes_avail > size)
2880 bytes_avail = (ssize_t)size;
2882 zip->pack_stream_bytes_unconsumed = bytes_avail;
2883 } else if (zip->uncompressed_buffer_pointer == NULL) {
2884 /* Decompression has failed. */
2885 archive_set_error(&(a->archive),
2886 ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
2887 return (ARCHIVE_FATAL);
2890 if (minimum > zip->uncompressed_buffer_bytes_remaining) {
2892 * If remaining uncompressed data size is less than
2893 * the minimum size, fill the buffer up to the
2896 if (extract_pack_stream(a, minimum) < 0)
2897 return (ARCHIVE_FATAL);
2899 if (size > zip->uncompressed_buffer_bytes_remaining)
2900 bytes_avail = (ssize_t)
2901 zip->uncompressed_buffer_bytes_remaining;
2903 bytes_avail = (ssize_t)size;
2904 *buff = zip->uncompressed_buffer_pointer;
2905 zip->uncompressed_buffer_pointer += bytes_avail;
2907 zip->uncompressed_buffer_bytes_remaining -= bytes_avail;
2908 return (bytes_avail);
2912 extract_pack_stream(struct archive_read *a, size_t minimum)
2914 struct _7zip *zip = (struct _7zip *)a->format->data;
2915 ssize_t bytes_avail;
2918 if (zip->codec == _7Z_COPY && zip->codec2 == (unsigned long)-1) {
2921 if (__archive_read_ahead(a, minimum, &bytes_avail) == NULL
2922 || bytes_avail <= 0) {
2923 archive_set_error(&a->archive,
2924 ARCHIVE_ERRNO_FILE_FORMAT,
2925 "Truncated 7-Zip file body");
2926 return (ARCHIVE_FATAL);
2928 if (bytes_avail > (ssize_t)zip->pack_stream_inbytes_remaining)
2929 bytes_avail = (ssize_t)zip->pack_stream_inbytes_remaining;
2930 zip->pack_stream_inbytes_remaining -= bytes_avail;
2931 if (bytes_avail > (ssize_t)zip->folder_outbytes_remaining)
2932 bytes_avail = (ssize_t)zip->folder_outbytes_remaining;
2933 zip->folder_outbytes_remaining -= bytes_avail;
2934 zip->uncompressed_buffer_bytes_remaining = bytes_avail;
2935 return (ARCHIVE_OK);
2938 /* If the buffer hasn't been allocated, allocate it now. */
2939 if (zip->uncompressed_buffer == NULL) {
2940 zip->uncompressed_buffer_size = UBUFF_SIZE;
2941 if (zip->uncompressed_buffer_size < minimum) {
2942 zip->uncompressed_buffer_size = minimum + 1023;
2943 zip->uncompressed_buffer_size &= ~0x3ff;
2945 zip->uncompressed_buffer =
2946 malloc(zip->uncompressed_buffer_size);
2947 if (zip->uncompressed_buffer == NULL) {
2948 archive_set_error(&a->archive, ENOMEM,
2949 "No memory for 7-Zip decompression");
2950 return (ARCHIVE_FATAL);
2952 zip->uncompressed_buffer_bytes_remaining = 0;
2953 } else if (zip->uncompressed_buffer_size < minimum ||
2954 zip->uncompressed_buffer_bytes_remaining < minimum) {
2956 * Make sure the uncompressed buffer can have bytes
2957 * at least `minimum' bytes.
2958 * NOTE: This case happen when reading the header.
2961 if (zip->uncompressed_buffer_pointer != 0)
2962 used = zip->uncompressed_buffer_pointer -
2963 zip->uncompressed_buffer;
2966 if (zip->uncompressed_buffer_size < minimum) {
2968 * Expand the uncompressed buffer up to
2974 new_size = minimum + 1023;
2976 p = realloc(zip->uncompressed_buffer, new_size);
2978 archive_set_error(&a->archive, ENOMEM,
2979 "No memory for 7-Zip decompression");
2980 return (ARCHIVE_FATAL);
2982 zip->uncompressed_buffer = (unsigned char *)p;
2983 zip->uncompressed_buffer_size = new_size;
2986 * Move unconsumed bytes to the head.
2989 memmove(zip->uncompressed_buffer,
2990 zip->uncompressed_buffer + used,
2991 zip->uncompressed_buffer_bytes_remaining);
2994 zip->uncompressed_buffer_bytes_remaining = 0;
2995 zip->uncompressed_buffer_pointer = NULL;
2997 size_t bytes_in, bytes_out;
2998 const void *buff_in;
2999 unsigned char *buff_out;
3003 * Note: '1' here is a performance optimization.
3004 * Recall that the decompression layer returns a count of
3005 * available bytes; asking for more than that forces the
3006 * decompressor to combine reads by copying data.
3008 buff_in = __archive_read_ahead(a, 1, &bytes_avail);
3009 if (bytes_avail <= 0) {
3010 archive_set_error(&a->archive,
3011 ARCHIVE_ERRNO_FILE_FORMAT,
3012 "Truncated 7-Zip file body");
3013 return (ARCHIVE_FATAL);
3016 buff_out = zip->uncompressed_buffer
3017 + zip->uncompressed_buffer_bytes_remaining;
3018 bytes_out = zip->uncompressed_buffer_size
3019 - zip->uncompressed_buffer_bytes_remaining;
3020 bytes_in = bytes_avail;
3021 if (bytes_in > zip->pack_stream_inbytes_remaining)
3022 bytes_in = (size_t)zip->pack_stream_inbytes_remaining;
3023 /* Drive decompression. */
3024 r = decompress(a, zip, buff_out, &bytes_out,
3025 buff_in, &bytes_in);
3034 return (ARCHIVE_FATAL);
3036 zip->pack_stream_inbytes_remaining -= bytes_in;
3037 if (bytes_out > zip->folder_outbytes_remaining)
3038 bytes_out = (size_t)zip->folder_outbytes_remaining;
3039 zip->folder_outbytes_remaining -= bytes_out;
3040 zip->uncompressed_buffer_bytes_remaining += bytes_out;
3041 zip->pack_stream_bytes_unconsumed = bytes_in;
3044 * Continue decompression until uncompressed_buffer is full.
3046 if (zip->uncompressed_buffer_bytes_remaining ==
3047 zip->uncompressed_buffer_size)
3049 if (zip->codec2 == _7Z_X86 && zip->odd_bcj_size &&
3050 zip->uncompressed_buffer_bytes_remaining + 5 >
3051 zip->uncompressed_buffer_size)
3053 if (zip->pack_stream_inbytes_remaining == 0 &&
3054 zip->folder_outbytes_remaining == 0)
3056 if (end_of_data || (bytes_in == 0 && bytes_out == 0)) {
3057 archive_set_error(&(a->archive),
3058 ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3059 return (ARCHIVE_FATAL);
3063 if (zip->uncompressed_buffer_bytes_remaining < minimum) {
3064 archive_set_error(&(a->archive),
3065 ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3066 return (ARCHIVE_FATAL);
3068 zip->uncompressed_buffer_pointer = zip->uncompressed_buffer;
3069 return (ARCHIVE_OK);
3073 seek_pack(struct archive_read *a)
3075 struct _7zip *zip = (struct _7zip *)a->format->data;
3076 int64_t pack_offset;
3078 if (zip->pack_stream_remaining <= 0) {
3079 archive_set_error(&(a->archive),
3080 ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3081 return (ARCHIVE_FATAL);
3083 zip->pack_stream_inbytes_remaining =
3084 zip->si.pi.sizes[zip->pack_stream_index];
3085 pack_offset = zip->si.pi.positions[zip->pack_stream_index];
3086 if (zip->stream_offset != pack_offset) {
3087 if (0 > __archive_read_seek(a, pack_offset + zip->seek_base,
3089 return (ARCHIVE_FATAL);
3090 zip->stream_offset = pack_offset;
3092 zip->pack_stream_index++;
3093 zip->pack_stream_remaining--;
3094 return (ARCHIVE_OK);
3098 read_stream(struct archive_read *a, const void **buff, size_t size,
3101 struct _7zip *zip = (struct _7zip *)a->format->data;
3102 uint64_t skip_bytes = 0;
3105 if (zip->uncompressed_buffer_bytes_remaining == 0) {
3106 if (zip->pack_stream_inbytes_remaining > 0) {
3107 r = extract_pack_stream(a, 0);
3110 return (get_uncompressed_data(a, buff, size, minimum));
3111 } else if (zip->folder_outbytes_remaining > 0) {
3112 /* Extract a remaining pack stream. */
3113 r = extract_pack_stream(a, 0);
3116 return (get_uncompressed_data(a, buff, size, minimum));
3119 return (get_uncompressed_data(a, buff, size, minimum));
3122 * Current pack stream has been consumed.
3124 if (zip->pack_stream_remaining == 0) {
3125 if (zip->header_is_being_read) {
3126 /* Invalid sequence. This might happen when
3127 * reading a malformed archive. */
3128 archive_set_error(&(a->archive),
3129 ARCHIVE_ERRNO_MISC, "Malformed 7-Zip archive");
3130 return (ARCHIVE_FATAL);
3134 * All current folder's pack streams have been
3135 * consumed. Switch to next folder.
3137 if (zip->folder_index == 0 &&
3138 (zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
3139 || zip->folder_index != zip->entry->folderIndex)) {
3140 zip->folder_index = zip->entry->folderIndex;
3142 zip->si.ci.folders[zip->folder_index].skipped_bytes;
3145 if (zip->folder_index >= zip->si.ci.numFolders) {
3147 * We have consumed all folders and its pack streams.
3152 r = setup_decode_folder(a,
3153 &(zip->si.ci.folders[zip->folder_index]), 0);
3154 if (r != ARCHIVE_OK)
3155 return (ARCHIVE_FATAL);
3157 zip->folder_index++;
3161 * Switch to next pack stream.
3167 /* Extract a new pack stream. */
3168 r = extract_pack_stream(a, 0);
3173 * Skip the bytes we alrady has skipped in skip_stream().
3175 while (skip_bytes) {
3178 if (zip->uncompressed_buffer_bytes_remaining == 0) {
3179 if (zip->pack_stream_inbytes_remaining > 0) {
3180 r = extract_pack_stream(a, 0);
3183 } else if (zip->folder_outbytes_remaining > 0) {
3184 /* Extract a remaining pack stream. */
3185 r = extract_pack_stream(a, 0);
3189 archive_set_error(&a->archive,
3190 ARCHIVE_ERRNO_FILE_FORMAT,
3191 "Truncated 7-Zip file body");
3192 return (ARCHIVE_FATAL);
3195 skipped = get_uncompressed_data(
3196 a, buff, (size_t)skip_bytes, 0);
3199 skip_bytes -= skipped;
3200 if (zip->pack_stream_bytes_unconsumed)
3204 return (get_uncompressed_data(a, buff, size, minimum));
3208 setup_decode_folder(struct archive_read *a, struct _7z_folder *folder,
3211 struct _7zip *zip = (struct _7zip *)a->format->data;
3212 const struct _7z_coder *coder1, *coder2;
3213 const char *cname = (header)?"archive header":"file content";
3215 int r, found_bcj2 = 0;
3218 * Release the memory which the previous folder used for BCJ2.
3220 for (i = 0; i < 3; i++) {
3221 if (zip->sub_stream_buff[i] != NULL)
3222 free(zip->sub_stream_buff[i]);
3223 zip->sub_stream_buff[i] = NULL;
3227 * Initialize a stream reader.
3229 zip->pack_stream_remaining = (unsigned)folder->numPackedStreams;
3230 zip->pack_stream_index = (unsigned)folder->packIndex;
3231 zip->folder_outbytes_remaining = folder_uncompressed_size(folder);
3232 zip->uncompressed_buffer_bytes_remaining = 0;
3235 * Check coder types.
3237 for (i = 0; i < folder->numCoders; i++) {
3238 if (folder->coders[i].codec == _7Z_CRYPTO) {
3239 archive_set_error(&(a->archive),
3241 "The %s is encrypted, "
3242 "but currently not supported", cname);
3243 return (ARCHIVE_FATAL);
3245 if (folder->coders[i].codec == _7Z_X86_BCJ2)
3248 if ((folder->numCoders > 2 && !found_bcj2) || found_bcj2 > 1) {
3249 archive_set_error(&(a->archive),
3251 "The %s is encoded with many filters, "
3252 "but currently not supported", cname);
3253 return (ARCHIVE_FATAL);
3255 coder1 = &(folder->coders[0]);
3256 if (folder->numCoders == 2)
3257 coder2 = &(folder->coders[1]);
3263 * Preparation to decode BCJ2.
3264 * Decoding BCJ2 requires four sources. Those are at least,
3265 * as far as I know, two types of the storage form.
3267 const struct _7z_coder *fc = folder->coders;
3268 static const struct _7z_coder coder_copy = {0, 1, 1, 0, NULL};
3269 const struct _7z_coder *scoder[3] =
3270 {&coder_copy, &coder_copy, &coder_copy};
3273 unsigned char *b[3] = {NULL, NULL, NULL};
3274 uint64_t sunpack[3] ={-1, -1, -1};
3275 size_t s[3] = {0, 0, 0};
3276 int idx[3] = {0, 1, 2};
3278 if (folder->numCoders == 4 && fc[3].codec == _7Z_X86_BCJ2 &&
3279 folder->numInStreams == 7 && folder->numOutStreams == 4 &&
3280 zip->pack_stream_remaining == 4) {
3281 /* Source type 1 made by 7zr or 7z with -m options. */
3282 if (folder->bindPairs[0].inIndex == 5) {
3283 /* The form made by 7zr */
3284 idx[0] = 1; idx[1] = 2; idx[2] = 0;
3285 scoder[1] = &(fc[1]);
3286 scoder[2] = &(fc[0]);
3287 sunpack[1] = folder->unPackSize[1];
3288 sunpack[2] = folder->unPackSize[0];
3292 * NOTE: Some patterns do not work.
3294 * 7z a -m0=BCJ2 -m1=COPY -m2=COPY
3296 * 7z a -m0=BCJ2 -m1=COPY -m2=(any)
3298 * 7z a -m0=BCJ2 -m1=(any) -m2=COPY
3303 * We have to handle this like `pipe' or
3304 * our libarchive7s filter frame work,
3305 * decoding the BCJ2 main stream sequentially,
3306 * m3 -> m2 -> m1 -> BCJ2.
3309 if (fc[0].codec == _7Z_COPY &&
3310 fc[1].codec == _7Z_COPY)
3311 coder1 = &(folder->coders[2]);
3312 else if (fc[0].codec == _7Z_COPY &&
3313 fc[2].codec == _7Z_COPY)
3314 coder1 = &(folder->coders[1]);
3315 else if (fc[1].codec == _7Z_COPY &&
3316 fc[2].codec == _7Z_COPY)
3317 coder1 = &(folder->coders[0]);
3319 archive_set_error(&(a->archive),
3321 "Unsupported form of "
3323 return (ARCHIVE_FATAL);
3327 zip->main_stream_bytes_remaining =
3328 (size_t)folder->unPackSize[2];
3329 } else if (coder2 != NULL && coder2->codec == _7Z_X86_BCJ2 &&
3330 zip->pack_stream_remaining == 4 &&
3331 folder->numInStreams == 5 && folder->numOutStreams == 2) {
3332 /* Source type 0 made by 7z */
3333 zip->main_stream_bytes_remaining =
3334 (size_t)folder->unPackSize[0];
3336 /* We got an unexpected form. */
3337 archive_set_error(&(a->archive),
3339 "Unsupported form of BCJ2 streams");
3340 return (ARCHIVE_FATAL);
3343 /* Skip the main stream at this time. */
3344 if ((r = seek_pack(a)) < 0)
3346 zip->pack_stream_bytes_unconsumed =
3347 (size_t)zip->pack_stream_inbytes_remaining;
3350 /* Read following three sub streams. */
3351 for (i = 0; i < 3; i++) {
3352 const struct _7z_coder *coder = scoder[i];
3354 if ((r = seek_pack(a)) < 0) {
3355 free(b[0]); free(b[1]); free(b[2]);
3359 if (sunpack[i] == (uint64_t)-1)
3360 zip->folder_outbytes_remaining =
3361 zip->pack_stream_inbytes_remaining;
3363 zip->folder_outbytes_remaining = sunpack[i];
3365 r = init_decompression(a, zip, coder, NULL);
3366 if (r != ARCHIVE_OK) {
3367 free(b[0]); free(b[1]); free(b[2]);
3368 return (ARCHIVE_FATAL);
3371 /* Allocate memory for the decorded data of a sub
3373 b[i] = malloc((size_t)zip->folder_outbytes_remaining);
3375 free(b[0]); free(b[1]); free(b[2]);
3376 archive_set_error(&a->archive, ENOMEM,
3377 "No memory for 7-Zip decompression");
3378 return (ARCHIVE_FATAL);
3381 /* Extract a sub stream. */
3382 while (zip->pack_stream_inbytes_remaining > 0) {
3383 r = (int)extract_pack_stream(a, 0);
3385 free(b[0]); free(b[1]); free(b[2]);
3388 bytes = get_uncompressed_data(a, &buff,
3389 zip->uncompressed_buffer_bytes_remaining,
3392 free(b[0]); free(b[1]); free(b[2]);
3393 return ((int)bytes);
3395 memcpy(b[i]+s[i], buff, bytes);
3397 if (zip->pack_stream_bytes_unconsumed)
3402 /* Set the sub streams to the right place. */
3403 for (i = 0; i < 3; i++) {
3404 zip->sub_stream_buff[i] = b[idx[i]];
3405 zip->sub_stream_size[i] = s[idx[i]];
3406 zip->sub_stream_bytes_remaining[i] = s[idx[i]];
3409 /* Allocate memory used for decoded main stream bytes. */
3410 if (zip->tmp_stream_buff == NULL) {
3411 zip->tmp_stream_buff_size = 32 * 1024;
3412 zip->tmp_stream_buff =
3413 malloc(zip->tmp_stream_buff_size);
3414 if (zip->tmp_stream_buff == NULL) {
3415 archive_set_error(&a->archive, ENOMEM,
3416 "No memory for 7-Zip decompression");
3417 return (ARCHIVE_FATAL);
3420 zip->tmp_stream_bytes_avail = 0;
3421 zip->tmp_stream_bytes_remaining = 0;
3422 zip->odd_bcj_size = 0;
3423 zip->bcj2_outPos = 0;
3426 * Reset a stream reader in order to read the main stream
3429 zip->pack_stream_remaining = 1;
3430 zip->pack_stream_index = (unsigned)folder->packIndex;
3431 zip->folder_outbytes_remaining =
3432 folder_uncompressed_size(folder);
3433 zip->uncompressed_buffer_bytes_remaining = 0;
3437 * Initialize the decompressor for the new folder's pack streams.
3439 r = init_decompression(a, zip, coder1, coder2);
3440 if (r != ARCHIVE_OK)
3441 return (ARCHIVE_FATAL);
3442 return (ARCHIVE_OK);
3446 skip_stream(struct archive_read *a, size_t skip_bytes)
3448 struct _7zip *zip = (struct _7zip *)a->format->data;
3450 int64_t skipped_bytes;
3451 size_t bytes = skip_bytes;
3453 if (zip->folder_index == 0) {
3455 * Optimization for a list mode.
3456 * Avoid unncecessary decoding operations.
3458 zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
3460 return (skip_bytes);
3464 skipped_bytes = read_stream(a, &p, bytes, 0);
3465 if (skipped_bytes < 0)
3466 return (skipped_bytes);
3467 if (skipped_bytes == 0) {
3468 archive_set_error(&a->archive,
3469 ARCHIVE_ERRNO_FILE_FORMAT,
3470 "Truncated 7-Zip file body");
3471 return (ARCHIVE_FATAL);
3473 bytes -= (size_t)skipped_bytes;
3474 if (zip->pack_stream_bytes_unconsumed)
3477 return (skip_bytes);
3481 * Brought from LZMA SDK.
3483 * Bra86.c -- Converter for x86 code (BCJ)
3484 * 2008-10-04 : Igor Pavlov : Public domain
3488 #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
3491 x86_Init(struct _7zip *zip)
3494 zip->bcj_prevPosT = (size_t)0 - 1;
3495 zip->bcj_prevMask = 0;
3500 x86_Convert(struct _7zip *zip, uint8_t *data, size_t size)
3502 static const uint8_t kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
3503 static const uint8_t kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
3504 size_t bufferPos, prevPosT;
3505 uint32_t ip, prevMask;
3511 prevPosT = zip->bcj_prevPosT;
3512 prevMask = zip->bcj_prevMask;
3516 uint8_t *p = data + bufferPos;
3517 uint8_t *limit = data + size - 4;
3519 for (; p < limit; p++)
3520 if ((*p & 0xFE) == 0xE8)
3522 bufferPos = (size_t)(p - data);
3525 prevPosT = bufferPos - prevPosT;
3529 prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
3530 if (prevMask != 0) {
3532 p[4 - kMaskToBitNumber[prevMask]];
3533 if (!kMaskToAllowedStatus[prevMask] ||
3535 prevPosT = bufferPos;
3536 prevMask = ((prevMask << 1) & 0x7) | 1;
3542 prevPosT = bufferPos;
3544 if (Test86MSByte(p[4])) {
3545 uint32_t src = ((uint32_t)p[4] << 24) |
3546 ((uint32_t)p[3] << 16) | ((uint32_t)p[2] << 8) |
3553 dest = src - (ip + (uint32_t)bufferPos);
3556 b_index = kMaskToBitNumber[prevMask] * 8;
3557 b = (uint8_t)(dest >> (24 - b_index));
3558 if (!Test86MSByte(b))
3560 src = dest ^ ((1 << (32 - b_index)) - 1);
3562 p[4] = (uint8_t)(~(((dest >> 24) & 1) - 1));
3563 p[3] = (uint8_t)(dest >> 16);
3564 p[2] = (uint8_t)(dest >> 8);
3565 p[1] = (uint8_t)dest;
3568 prevMask = ((prevMask << 1) & 0x7) | 1;
3572 zip->bcj_prevPosT = prevPosT;
3573 zip->bcj_prevMask = prevMask;
3574 zip->bcj_ip += (uint32_t)bufferPos;
3579 * Brought from LZMA SDK.
3581 * Bcj2.c -- Converter for x86 code (BCJ2)
3582 * 2008-10-04 : Igor Pavlov : Public domain
3586 #define SZ_ERROR_DATA ARCHIVE_FAILED
3588 #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
3589 #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
3591 #define kNumTopBits 24
3592 #define kTopValue ((uint32_t)1 << kNumTopBits)
3594 #define kNumBitModelTotalBits 11
3595 #define kBitModelTotal (1 << kNumBitModelTotalBits)
3596 #define kNumMoveBits 5
3598 #define RC_READ_BYTE (*buffer++)
3599 #define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; }
3600 #define RC_INIT2 zip->bcj2_code = 0; zip->bcj2_range = 0xFFFFFFFF; \
3601 { int ii; for (ii = 0; ii < 5; ii++) { RC_TEST; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }}
3603 #define NORMALIZE if (zip->bcj2_range < kTopValue) { RC_TEST; zip->bcj2_range <<= 8; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }
3605 #define IF_BIT_0(p) ttt = *(p); bound = (zip->bcj2_range >> kNumBitModelTotalBits) * ttt; if (zip->bcj2_code < bound)
3606 #define UPDATE_0(p) zip->bcj2_range = bound; *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); NORMALIZE;
3607 #define UPDATE_1(p) zip->bcj2_range -= bound; zip->bcj2_code -= bound; *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); NORMALIZE;
3610 Bcj2_Decode(struct _7zip *zip, uint8_t *outBuf, size_t outSize)
3612 size_t inPos = 0, outPos = 0;
3613 const uint8_t *buf0, *buf1, *buf2, *buf3;
3614 size_t size0, size1, size2, size3;
3615 const uint8_t *buffer, *bufferLim;
3618 size0 = zip->tmp_stream_bytes_remaining;
3619 buf0 = zip->tmp_stream_buff + zip->tmp_stream_bytes_avail - size0;
3620 size1 = zip->sub_stream_bytes_remaining[0];
3621 buf1 = zip->sub_stream_buff[0] + zip->sub_stream_size[0] - size1;
3622 size2 = zip->sub_stream_bytes_remaining[1];
3623 buf2 = zip->sub_stream_buff[1] + zip->sub_stream_size[1] - size2;
3624 size3 = zip->sub_stream_bytes_remaining[2];
3625 buf3 = zip->sub_stream_buff[2] + zip->sub_stream_size[2] - size3;
3628 bufferLim = buffer + size3;
3630 if (zip->bcj_state == 0) {
3634 zip->bcj2_prevByte = 0;
3636 i < sizeof(zip->bcj2_p) / sizeof(zip->bcj2_p[0]); i++)
3637 zip->bcj2_p[i] = kBitModelTotal >> 1;
3643 * Gather the odd bytes of a previous call.
3645 for (i = 0; zip->odd_bcj_size > 0 && outPos < outSize; i++) {
3646 outBuf[outPos++] = zip->odd_bcj[i];
3647 zip->odd_bcj_size--;
3651 zip->bcj2_outPos += outPos;
3661 size_t limit = size0 - inPos;
3662 if (outSize - outPos < limit)
3663 limit = outSize - outPos;
3665 if (zip->bcj_state == 1) {
3666 while (limit != 0) {
3667 uint8_t bb = buf0[inPos];
3668 outBuf[outPos++] = bb;
3669 if (IsJ(zip->bcj2_prevByte, bb)) {
3674 zip->bcj2_prevByte = bb;
3679 if (limit == 0 || outPos == outSize)
3686 prob = zip->bcj2_p + zip->bcj2_prevByte;
3688 prob = zip->bcj2_p + 256;
3690 prob = zip->bcj2_p + 257;
3694 zip->bcj2_prevByte = b;
3704 return SZ_ERROR_DATA;
3710 return SZ_ERROR_DATA;
3714 dest = (((uint32_t)v[0] << 24) |
3715 ((uint32_t)v[1] << 16) |
3716 ((uint32_t)v[2] << 8) |
3718 ((uint32_t)zip->bcj2_outPos + (uint32_t)outPos + 4);
3719 out[0] = (uint8_t)dest;
3720 out[1] = (uint8_t)(dest >> 8);
3721 out[2] = (uint8_t)(dest >> 16);
3722 out[3] = zip->bcj2_prevByte = (uint8_t)(dest >> 24);
3724 for (i = 0; i < 4 && outPos < outSize; i++)
3725 outBuf[outPos++] = out[i];
3728 * Save odd bytes which we could not add into
3729 * the output buffer because of out of space.
3731 zip->odd_bcj_size = 4 -i;
3732 for (; i < 4; i++) {
3733 j = i - 4 + (unsigned)zip->odd_bcj_size;
3734 zip->odd_bcj[j] = out[i];
3740 zip->tmp_stream_bytes_remaining -= inPos;
3741 zip->sub_stream_bytes_remaining[0] = size1;
3742 zip->sub_stream_bytes_remaining[1] = size2;
3743 zip->sub_stream_bytes_remaining[2] = bufferLim - buffer;
3744 zip->bcj2_outPos += outPos;
3746 return ((ssize_t)outPos);