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 convertion 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 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,
412 archive_read_format_7zip_cleanup);
420 archive_read_format_7zip_bid(struct archive_read *a, int best_bid)
424 /* If someone has already bid more than 32, then avoid
425 trashing the look-ahead buffers with a seek. */
429 if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
432 /* If first six bytes are the 7-Zip signature,
433 * return the bid right now. */
434 if (memcmp(p, _7ZIP_SIGNATURE, 6) == 0)
438 * It may a 7-Zip SFX archive file. If first two bytes are
439 * 'M' and 'Z' available on Windows or first four bytes are
440 * "\x7F\x45LF" available on posix like system, seek the 7-Zip
441 * signature. Although we will perform a seek when reading
442 * a header, what we do not use __archive_read_seek() here is
443 * due to a bidding performance.
445 if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
446 ssize_t offset = SFX_MIN_ADDR;
447 ssize_t window = 4096;
449 while (offset + window <= (SFX_MAX_ADDR)) {
450 const char *buff = __archive_read_ahead(a,
451 offset + window, &bytes_avail);
453 /* Remaining bytes are less than window. */
460 while (p + 32 < buff + bytes_avail) {
461 int step = check_7zip_header_in_sfx(p);
473 check_7zip_header_in_sfx(const char *p)
475 switch ((unsigned char)p[5]) {
477 if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0)
480 * Test the CRC because its extraction code has 7-Zip
481 * Magic Code, so we should do this in order not to
482 * make a mis-detection.
484 if (crc32(0, (unsigned char *)p + 12, 20)
485 != archive_le32dec(p + 8))
487 /* Hit the header! */
489 case 0x37: return (5);
490 case 0x7A: return (4);
491 case 0xBC: return (3);
492 case 0xAF: return (2);
493 case 0x27: return (1);
499 skip_sfx(struct archive_read *a, ssize_t bytes_avail)
504 ssize_t bytes, window;
507 * If bytes_avail > SFX_MIN_ADDR we do not have to call
508 * __archive_read_seek() at this time since we have
509 * alredy had enough data.
511 if (bytes_avail > SFX_MIN_ADDR)
512 __archive_read_consume(a, SFX_MIN_ADDR);
513 else if (__archive_read_seek(a, SFX_MIN_ADDR, SEEK_SET) < 0)
514 return (ARCHIVE_FATAL);
518 while (offset + window <= SFX_MAX_ADDR - SFX_MIN_ADDR) {
519 h = __archive_read_ahead(a, window, &bytes);
521 /* Remaining bytes are less than window. */
528 /* This case might happen when window == 1. */
536 * Scan ahead until we find something that looks
537 * like the 7-Zip header.
540 int step = check_7zip_header_in_sfx(p);
543 (struct _7zip *)a->format->data;
544 skip = p - (const char *)h;
545 __archive_read_consume(a, skip);
546 zip->seek_base = SFX_MIN_ADDR + offset + skip;
551 skip = p - (const char *)h;
552 __archive_read_consume(a, skip);
558 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
559 "Couldn't find out 7-Zip header");
560 return (ARCHIVE_FATAL);
564 archive_read_format_7zip_read_header(struct archive_read *a,
565 struct archive_entry *entry)
567 struct _7zip *zip = (struct _7zip *)a->format->data;
568 struct _7zip_entry *zip_entry;
569 int r, ret = ARCHIVE_OK;
571 a->archive.archive_format = ARCHIVE_FORMAT_7ZIP;
572 if (a->archive.archive_format_name == NULL)
573 a->archive.archive_format_name = "7-Zip";
575 if (zip->entries == NULL) {
576 struct _7z_header_info header;
578 memset(&header, 0, sizeof(header));
579 r = slurp_central_directory(a, zip, &header);
580 free_Header(&header);
583 zip->entries_remaining = zip->numFiles;
584 zip->entry = zip->entries;
588 zip_entry = zip->entry;
590 if (zip->entries_remaining <= 0)
592 --zip->entries_remaining;
594 zip->entry_offset = 0;
595 zip->end_of_entry = 0;
596 zip->entry_crc32 = crc32(0, NULL, 0);
598 /* Setup a string conversion for a filename. */
599 if (zip->sconv == NULL) {
600 zip->sconv = archive_string_conversion_from_charset(
601 &a->archive, "UTF-16LE", 1);
602 if (zip->sconv == NULL)
603 return (ARCHIVE_FATAL);
606 if (archive_entry_copy_pathname_l(entry,
607 (const char *)zip_entry->utf16name,
608 zip_entry->name_len, zip->sconv) != 0) {
609 if (errno == ENOMEM) {
610 archive_set_error(&a->archive, ENOMEM,
611 "Can't allocate memory for Pathname");
612 return (ARCHIVE_FATAL);
614 archive_set_error(&a->archive,
615 ARCHIVE_ERRNO_FILE_FORMAT,
616 "Pathname cannot be converted "
617 "from %s to current locale.",
618 archive_string_conversion_charset_name(zip->sconv));
622 /* Populate some additional entry fields: */
623 archive_entry_set_mode(entry, zip_entry->mode);
624 if (zip_entry->flg & MTIME_IS_SET)
625 archive_entry_set_mtime(entry, zip_entry->mtime,
626 zip_entry->mtime_ns);
627 if (zip_entry->flg & CTIME_IS_SET)
628 archive_entry_set_ctime(entry, zip_entry->ctime,
629 zip_entry->ctime_ns);
630 if (zip_entry->flg & ATIME_IS_SET)
631 archive_entry_set_atime(entry, zip_entry->atime,
632 zip_entry->atime_ns);
633 if (zip_entry->ssIndex != -1) {
634 zip->entry_bytes_remaining =
635 zip->si.ss.unpackSizes[zip_entry->ssIndex];
636 archive_entry_set_size(entry, zip->entry_bytes_remaining);
638 zip->entry_bytes_remaining = 0;
639 archive_entry_set_size(entry, 0);
642 /* If there's no body, force read_data() to return EOF immediately. */
643 if (zip->entry_bytes_remaining < 1)
644 zip->end_of_entry = 1;
646 if ((zip_entry->mode & AE_IFMT) == AE_IFLNK) {
647 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) {
660 r = archive_read_format_7zip_read_data(a, &buff,
662 if (r < ARCHIVE_WARN)
664 symname = realloc(symname, symsize + size + 1);
665 if (symname == NULL) {
666 archive_set_error(&a->archive, ENOMEM,
667 "Can't allocate memory for Symname");
668 return (ARCHIVE_FATAL);
670 memcpy(symname+symsize, buff, size);
674 /* If there is no synname, handle it as a regular
676 zip_entry->mode &= ~AE_IFMT;
677 zip_entry->mode |= AE_IFREG;
678 archive_entry_set_mode(entry, zip_entry->mode);
680 symname[symsize] = '\0';
681 archive_entry_copy_symlink(entry,
682 (const char *)symname);
685 archive_entry_set_size(entry, 0);
688 /* Set up a more descriptive format name. */
689 sprintf(zip->format_name, "7-Zip");
690 a->archive.archive_format_name = zip->format_name;
696 archive_read_format_7zip_read_data(struct archive_read *a,
697 const void **buff, size_t *size, int64_t *offset)
701 int ret = ARCHIVE_OK;
703 zip = (struct _7zip *)(a->format->data);
705 if (zip->pack_stream_bytes_unconsumed)
709 * If we hit end-of-entry last time, clean up and return
710 * ARCHIVE_EOF this time.
712 if (zip->end_of_entry) {
713 *offset = zip->entry_offset;
716 return (ARCHIVE_EOF);
719 bytes = read_stream(a, buff, zip->entry_bytes_remaining, 0);
723 archive_set_error(&a->archive,
724 ARCHIVE_ERRNO_FILE_FORMAT,
725 "Truncated 7-Zip file body");
726 return (ARCHIVE_FATAL);
728 zip->entry_bytes_remaining -= bytes;
729 if (zip->entry_bytes_remaining == 0)
730 zip->end_of_entry = 1;
732 /* Update checksum */
733 if ((zip->entry->flg & CRC32_IS_SET) && bytes)
734 zip->entry_crc32 = crc32(zip->entry_crc32, *buff, bytes);
736 /* If we hit the end, swallow any end-of-data marker. */
737 if (zip->end_of_entry) {
738 /* Check computed CRC against file contents. */
739 if ((zip->entry->flg & CRC32_IS_SET) &&
740 zip->si.ss.digests[zip->entry->ssIndex] !=
742 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
743 "7-Zip bad CRC: 0x%lx should be 0x%lx",
744 (unsigned long)zip->entry_crc32,
745 (unsigned long)zip->si.ss.digests[
746 zip->entry->ssIndex]);
752 *offset = zip->entry_offset;
753 zip->entry_offset += bytes;
759 archive_read_format_7zip_read_data_skip(struct archive_read *a)
762 int64_t bytes_skipped;
764 zip = (struct _7zip *)(a->format->data);
766 if (zip->pack_stream_bytes_unconsumed)
769 /* If we've already read to end of data, we're done. */
770 if (zip->end_of_entry)
774 * If the length is at the beginning, we can skip the
775 * compressed data much more quickly.
777 bytes_skipped = skip_stream(a, zip->entry_bytes_remaining);
778 if (bytes_skipped < 0)
779 return (ARCHIVE_FATAL);
780 zip->entry_bytes_remaining = 0;
782 /* This entry is finished and done. */
783 zip->end_of_entry = 1;
788 archive_read_format_7zip_cleanup(struct archive_read *a)
792 zip = (struct _7zip *)(a->format->data);
793 free_StreamsInfo(&(zip->si));
795 free(zip->entry_names);
796 free_decompression(a, zip);
797 free(zip->uncompressed_buffer);
798 free(zip->sub_stream_buff[0]);
799 free(zip->sub_stream_buff[1]);
800 free(zip->sub_stream_buff[2]);
801 free(zip->tmp_stream_buff);
803 (a->format->data) = NULL;
808 read_consume(struct archive_read *a)
810 struct _7zip *zip = (struct _7zip *)a->format->data;
812 if (zip->pack_stream_bytes_unconsumed) {
813 __archive_read_consume(a, zip->pack_stream_bytes_unconsumed);
814 zip->stream_offset += zip->pack_stream_bytes_unconsumed;
815 zip->pack_stream_bytes_unconsumed = 0;
822 * Set an error code and choose an error message for liblzma.
825 set_error(struct archive_read *a, int ret)
829 case LZMA_STREAM_END: /* Found end of stream. */
830 case LZMA_OK: /* Decompressor made some progress. */
833 archive_set_error(&a->archive, ENOMEM,
834 "Lzma library error: Cannot allocate memory");
836 case LZMA_MEMLIMIT_ERROR:
837 archive_set_error(&a->archive, ENOMEM,
838 "Lzma library error: Out of memory");
840 case LZMA_FORMAT_ERROR:
841 archive_set_error(&a->archive,
843 "Lzma library error: format not recognized");
845 case LZMA_OPTIONS_ERROR:
846 archive_set_error(&a->archive,
848 "Lzma library error: Invalid options");
850 case LZMA_DATA_ERROR:
851 archive_set_error(&a->archive,
853 "Lzma library error: Corrupted input data");
856 archive_set_error(&a->archive,
858 "Lzma library error: No progress is possible");
861 /* Return an error. */
862 archive_set_error(&a->archive,
864 "Lzma decompression failed: Unknown error");
872 decode_codec_id(const unsigned char *codecId, size_t id_size)
875 unsigned long id = 0;
877 for (i = 0; i < id_size; i++) {
885 ppmd_alloc(void *p, size_t size)
891 ppmd_free(void *p, void *address)
899 struct archive_read *a = ((IByteIn*)p)->a;
900 struct _7zip *zip = (struct _7zip *)(a->format->data);
903 if (zip->ppstream.avail_in == 0) {
904 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
905 "Truncated RAR file data");
906 zip->ppstream.overconsumed = 1;
909 b = *zip->ppstream.next_in++;
910 zip->ppstream.avail_in--;
911 zip->ppstream.total_in++;
915 static ISzAlloc g_szalloc = { ppmd_alloc, ppmd_free };
918 init_decompression(struct archive_read *a, struct _7zip *zip,
919 const struct _7z_coder *coder1, const struct _7z_coder *coder2)
923 zip->codec = coder1->codec;
926 switch (zip->codec) {
931 if (coder2 != NULL) {
932 if (coder2->codec != _7Z_X86 &&
933 coder2->codec != _7Z_X86_BCJ2) {
934 archive_set_error(&a->archive,
936 "Unsupported filter %lx for %lx",
937 coder2->codec, coder1->codec);
938 return (ARCHIVE_FAILED);
940 zip->codec2 = coder2->codec;
942 if (coder2->codec == _7Z_X86)
950 switch (zip->codec) {
954 case _7Z_LZMA: case _7Z_LZMA2:
956 #if LZMA_VERSION_MAJOR >= 5
957 /* Effectively disable the limiter. */
958 #define LZMA_MEMLIMIT UINT64_MAX
960 /* NOTE: This needs to check memory size which running system has. */
961 #define LZMA_MEMLIMIT (1U << 30)
964 lzma_options_delta delta_opt;
965 lzma_filter filters[LZMA_FILTERS_MAX];
966 #if LZMA_VERSION < 50000030
971 if (zip->lzstream_valid) {
972 lzma_end(&(zip->lzstream));
973 zip->lzstream_valid = 0;
977 * NOTE: liblzma incompletely handle the BCJ+LZMA compressed
978 * data made by 7-Zip because 7-Zip does not add End-Of-
979 * Payload Marker(EOPM) at the end of LZMA compressed data,
980 * and so liblzma cannot know the end of the compressed data
981 * without EOPM. So consequently liblzma will not return last
982 * three or four bytes of uncompressed data because
983 * LZMA_FILTER_X86 filter does not handle input data if its
984 * data size is less than five bytes. If liblzma detect EOPM
985 * or know the uncompressed data size, liblzma will flush out
986 * the remaining that three or four bytes of uncompressed
987 * data. That is why we have to use our converting program
988 * for BCJ+LZMA. If we were able to tell the uncompressed
989 * size to liblzma when using lzma_raw_decoder() liblzma
990 * could correctly deal with BCJ+LZMA. But unfortunately
991 * there is no way to do that.
992 * Discussion about this can be found at XZ Utils forum.
994 if (coder2 != NULL) {
995 zip->codec2 = coder2->codec;
997 filters[fi].options = NULL;
998 switch (zip->codec2) {
1000 if (zip->codec == _7Z_LZMA2) {
1001 filters[fi].id = LZMA_FILTER_X86;
1004 /* Use our filter. */
1008 /* Use our filter. */
1012 filters[fi].id = LZMA_FILTER_DELTA;
1013 memset(&delta_opt, 0, sizeof(delta_opt));
1014 delta_opt.type = LZMA_DELTA_TYPE_BYTE;
1016 filters[fi].options = &delta_opt;
1019 /* Following filters have not been tested yet. */
1021 filters[fi].id = LZMA_FILTER_POWERPC;
1025 filters[fi].id = LZMA_FILTER_IA64;
1029 filters[fi].id = LZMA_FILTER_ARM;
1033 filters[fi].id = LZMA_FILTER_ARMTHUMB;
1037 filters[fi].id = LZMA_FILTER_SPARC;
1041 archive_set_error(&a->archive,
1043 "Unexpected codec ID: %lX", zip->codec2);
1044 return (ARCHIVE_FAILED);
1048 if (zip->codec == _7Z_LZMA2)
1049 filters[fi].id = LZMA_FILTER_LZMA2;
1051 filters[fi].id = LZMA_FILTER_LZMA1;
1052 filters[fi].options = NULL;
1053 #if LZMA_VERSION < 50000030
1056 r = lzma_properties_decode(&filters[fi], NULL,
1057 coder1->properties, coder1->propertiesSize);
1060 return (ARCHIVE_FAILED);
1064 filters[fi].id = LZMA_VLI_UNKNOWN;
1065 filters[fi].options = NULL;
1066 r = lzma_raw_decoder(&(zip->lzstream), filters);
1067 #if LZMA_VERSION < 50000030
1072 return (ARCHIVE_FAILED);
1074 zip->lzstream_valid = 1;
1075 zip->lzstream.total_in = 0;
1076 zip->lzstream.total_out = 0;
1080 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1081 "LZMA codec is unsupported");
1082 return (ARCHIVE_FAILED);
1085 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1086 if (zip->bzstream_valid) {
1087 BZ2_bzDecompressEnd(&(zip->bzstream));
1088 zip->bzstream_valid = 0;
1090 r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 0);
1091 if (r == BZ_MEM_ERROR)
1092 r = BZ2_bzDecompressInit(&(zip->bzstream), 0, 1);
1094 int err = ARCHIVE_ERRNO_MISC;
1095 const char *detail = NULL;
1097 case BZ_PARAM_ERROR:
1098 detail = "invalid setup parameter";
1102 detail = "out of memory";
1104 case BZ_CONFIG_ERROR:
1105 detail = "mis-compiled library";
1108 archive_set_error(&a->archive, err,
1109 "Internal error initializing decompressor: %s",
1110 detail == NULL ? "??" : detail);
1111 zip->bzstream_valid = 0;
1112 return (ARCHIVE_FAILED);
1114 zip->bzstream_valid = 1;
1115 zip->bzstream.total_in_lo32 = 0;
1116 zip->bzstream.total_in_hi32 = 0;
1117 zip->bzstream.total_out_lo32 = 0;
1118 zip->bzstream.total_out_hi32 = 0;
1121 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1122 "BZ2 codec is unsupported");
1123 return (ARCHIVE_FAILED);
1127 if (zip->stream_valid)
1128 r = inflateReset(&(zip->stream));
1130 r = inflateInit2(&(zip->stream),
1131 -15 /* Don't check for zlib header */);
1133 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1134 "Couldn't initialize zlib stream.");
1135 return (ARCHIVE_FAILED);
1137 zip->stream_valid = 1;
1138 zip->stream.total_in = 0;
1139 zip->stream.total_out = 0;
1142 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1143 "DEFLATE codec is unsupported");
1144 return (ARCHIVE_FAILED);
1151 if (zip->ppmd7_valid) {
1152 __archive_ppmd7_functions.Ppmd7_Free(
1153 &zip->ppmd7_context, &g_szalloc);
1154 zip->ppmd7_valid = 0;
1157 if (coder1->propertiesSize < 5) {
1158 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1159 "Malformed PPMd parameter");
1160 return (ARCHIVE_FAILED);
1162 order = coder1->properties[0];
1163 msize = archive_le32dec(&(coder1->properties[1]));
1164 if (order < PPMD7_MIN_ORDER || order > PPMD7_MAX_ORDER ||
1165 msize < PPMD7_MIN_MEM_SIZE || msize > PPMD7_MAX_MEM_SIZE) {
1166 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1167 "Malformed PPMd parameter");
1168 return (ARCHIVE_FAILED);
1170 __archive_ppmd7_functions.Ppmd7_Construct(&zip->ppmd7_context);
1171 r = __archive_ppmd7_functions.Ppmd7_Alloc(
1172 &zip->ppmd7_context, msize, &g_szalloc);
1174 archive_set_error(&a->archive, ENOMEM,
1175 "Coludn't allocate memory for PPMd");
1176 return (ARCHIVE_FATAL);
1178 __archive_ppmd7_functions.Ppmd7_Init(
1179 &zip->ppmd7_context, order);
1180 __archive_ppmd7_functions.Ppmd7z_RangeDec_CreateVTable(
1182 zip->ppmd7_valid = 1;
1183 zip->ppmd7_stat = 0;
1184 zip->ppstream.overconsumed = 0;
1185 zip->ppstream.total_in = 0;
1186 zip->ppstream.total_out = 0;
1197 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1198 "Unexpected codec ID: %lX", zip->codec);
1199 return (ARCHIVE_FAILED);
1201 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1202 "Unknown codec ID: %lX", zip->codec);
1203 return (ARCHIVE_FAILED);
1206 return (ARCHIVE_OK);
1210 decompress(struct archive_read *a, struct _7zip *zip,
1211 void *buff, size_t *outbytes, const void *b, size_t *used)
1213 const uint8_t *t_next_in;
1214 uint8_t *t_next_out;
1215 size_t o_avail_in, o_avail_out;
1216 size_t t_avail_in, t_avail_out;
1217 uint8_t *bcj2_next_out;
1218 size_t bcj2_avail_out;
1219 int r, ret = ARCHIVE_OK;
1221 t_avail_in = o_avail_in = *used;
1222 t_avail_out = o_avail_out = *outbytes;
1226 if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
1229 /* Do not copy out the BCJ remaining bytes when the output
1230 * buffer size is less than five bytes. */
1231 if (o_avail_in != 0 && t_avail_out < 5 && zip->odd_bcj_size) {
1236 for (i = 0; zip->odd_bcj_size > 0 && t_avail_out; i++) {
1237 *t_next_out++ = zip->odd_bcj[i];
1239 zip->odd_bcj_size--;
1241 if (o_avail_in == 0 || t_avail_out == 0) {
1242 *used = o_avail_in - t_avail_in;
1243 *outbytes = o_avail_out - t_avail_out;
1244 if (o_avail_in == 0)
1250 bcj2_next_out = t_next_out;
1251 bcj2_avail_out = t_avail_out;
1252 if (zip->codec2 == _7Z_X86_BCJ2) {
1254 * Decord a remaining decompressed main stream for BCJ2.
1256 if (zip->tmp_stream_bytes_remaining) {
1258 size_t remaining = zip->tmp_stream_bytes_remaining;
1259 bytes = Bcj2_Decode(zip, t_next_out, t_avail_out);
1261 archive_set_error(&(a->archive),
1263 "BCJ2 conversion Failed");
1264 return (ARCHIVE_FAILED);
1266 zip->main_stream_bytes_remaining -=
1267 remaining - zip->tmp_stream_bytes_remaining;
1268 t_avail_out -= bytes;
1269 if (o_avail_in == 0 || t_avail_out == 0) {
1271 *outbytes = o_avail_out - t_avail_out;
1272 if (o_avail_in == 0 &&
1273 zip->tmp_stream_bytes_remaining)
1277 t_next_out += bytes;
1278 bcj2_next_out = t_next_out;
1279 bcj2_avail_out = t_avail_out;
1281 t_next_out = zip->tmp_stream_buff;
1282 t_avail_out = zip->tmp_stream_buff_size;
1285 switch (zip->codec) {
1289 (t_avail_in > t_avail_out)?t_avail_out:t_avail_in;
1291 memcpy(t_next_out, t_next_in, bytes);
1292 t_avail_in -= bytes;
1293 t_avail_out -= bytes;
1294 if (o_avail_in == 0)
1299 case _7Z_LZMA: case _7Z_LZMA2:
1300 zip->lzstream.next_in = t_next_in;
1301 zip->lzstream.avail_in = t_avail_in;
1302 zip->lzstream.next_out = t_next_out;
1303 zip->lzstream.avail_out = t_avail_out;
1305 r = lzma_code(&(zip->lzstream), LZMA_RUN);
1307 case LZMA_STREAM_END: /* Found end of stream. */
1308 lzma_end(&(zip->lzstream));
1309 zip->lzstream_valid = 0;
1312 case LZMA_OK: /* Decompressor made some progress. */
1315 archive_set_error(&(a->archive),
1317 "Decompression failed(%d)",
1319 return (ARCHIVE_FAILED);
1321 t_avail_in = zip->lzstream.avail_in;
1322 t_avail_out = zip->lzstream.avail_out;
1325 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1327 zip->bzstream.next_in = (char *)(uintptr_t)t_next_in;
1328 zip->bzstream.avail_in = t_avail_in;
1329 zip->bzstream.next_out = (char *)(uintptr_t)t_next_out;
1330 zip->bzstream.avail_out = t_avail_out;
1331 r = BZ2_bzDecompress(&(zip->bzstream));
1333 case BZ_STREAM_END: /* Found end of stream. */
1334 switch (BZ2_bzDecompressEnd(&(zip->bzstream))) {
1338 archive_set_error(&(a->archive),
1340 "Failed to clean up decompressor");
1341 return (ARCHIVE_FAILED);
1343 zip->bzstream_valid = 0;
1346 case BZ_OK: /* Decompressor made some progress. */
1349 archive_set_error(&(a->archive),
1351 "bzip decompression failed");
1352 return (ARCHIVE_FAILED);
1354 t_avail_in = zip->bzstream.avail_in;
1355 t_avail_out = zip->bzstream.avail_out;
1360 zip->stream.next_in = (Bytef *)(uintptr_t)t_next_in;
1361 zip->stream.avail_in = t_avail_in;
1362 zip->stream.next_out = t_next_out;
1363 zip->stream.avail_out = t_avail_out;
1364 r = inflate(&(zip->stream), 0);
1366 case Z_STREAM_END: /* Found end of stream. */
1369 case Z_OK: /* Decompressor made some progress.*/
1372 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1373 "File decompression failed (%d)", r);
1374 return (ARCHIVE_FAILED);
1376 t_avail_in = zip->stream.avail_in;
1377 t_avail_out = zip->stream.avail_out;
1382 uint64_t flush_bytes;
1384 if (!zip->ppmd7_valid || zip->ppmd7_stat < 0 ||
1385 t_avail_in < 0 || t_avail_out <= 0) {
1386 archive_set_error(&(a->archive),
1388 "Decompression internal error");
1389 return (ARCHIVE_FAILED);
1391 zip->ppstream.next_in = t_next_in;
1392 zip->ppstream.avail_in = t_avail_in;
1393 zip->ppstream.next_out = t_next_out;
1394 zip->ppstream.avail_out = t_avail_out;
1395 if (zip->ppmd7_stat == 0) {
1397 zip->bytein.Read = &ppmd_read;
1398 zip->range_dec.Stream = &zip->bytein;
1399 r = __archive_ppmd7_functions.Ppmd7z_RangeDec_Init(
1402 zip->ppmd7_stat = -1;
1403 archive_set_error(&a->archive,
1405 "Failed to initialize PPMd range decorder");
1406 return (ARCHIVE_FAILED);
1408 if (zip->ppstream.overconsumed) {
1409 zip->ppmd7_stat = -1;
1410 return (ARCHIVE_FAILED);
1412 zip->ppmd7_stat = 1;
1415 if (t_avail_in == 0)
1416 /* XXX Flush out remaining decoded data XXX */
1417 flush_bytes = zip->folder_outbytes_remaining;
1424 sym = __archive_ppmd7_functions.Ppmd7_DecodeSymbol(
1425 &(zip->ppmd7_context), &(zip->range_dec.p));
1427 zip->ppmd7_stat = -1;
1428 archive_set_error(&a->archive,
1429 ARCHIVE_ERRNO_FILE_FORMAT,
1430 "Failed to decode PPMd");
1431 return (ARCHIVE_FAILED);
1433 if (zip->ppstream.overconsumed) {
1434 zip->ppmd7_stat = -1;
1435 return (ARCHIVE_FAILED);
1437 *zip->ppstream.next_out++ = (unsigned char)sym;
1438 zip->ppstream.avail_out--;
1439 zip->ppstream.total_out++;
1442 } while (zip->ppstream.avail_out &&
1443 (zip->ppstream.avail_in || flush_bytes));
1445 t_avail_in = zip->ppstream.avail_in;
1446 t_avail_out = zip->ppstream.avail_out;
1450 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1451 "Decompression internal error");
1452 return (ARCHIVE_FAILED);
1454 if (ret != ARCHIVE_OK && ret != ARCHIVE_EOF)
1457 *used = o_avail_in - t_avail_in;
1458 *outbytes = o_avail_out - t_avail_out;
1463 if (zip->codec != _7Z_LZMA2 && zip->codec2 == _7Z_X86) {
1464 size_t l = x86_Convert(zip, buff, *outbytes);
1465 zip->odd_bcj_size = *outbytes - l;
1466 if (zip->odd_bcj_size > 0 && zip->odd_bcj_size <= 4 &&
1467 o_avail_in && ret != ARCHIVE_EOF) {
1468 memcpy(zip->odd_bcj, ((unsigned char *)buff) + l,
1472 zip->odd_bcj_size = 0;
1476 * Decord BCJ2 with a decompressed main stream.
1478 if (zip->codec2 == _7Z_X86_BCJ2) {
1481 zip->tmp_stream_bytes_avail =
1482 zip->tmp_stream_buff_size - t_avail_out;
1483 if (zip->tmp_stream_bytes_avail >
1484 zip->main_stream_bytes_remaining)
1485 zip->tmp_stream_bytes_avail =
1486 zip->main_stream_bytes_remaining;
1487 zip->tmp_stream_bytes_remaining = zip->tmp_stream_bytes_avail;
1488 bytes = Bcj2_Decode(zip, bcj2_next_out, bcj2_avail_out);
1490 archive_set_error(&(a->archive),
1491 ARCHIVE_ERRNO_MISC, "BCJ2 conversion Failed");
1492 return (ARCHIVE_FAILED);
1494 zip->main_stream_bytes_remaining -=
1495 zip->tmp_stream_bytes_avail
1496 - zip->tmp_stream_bytes_remaining;
1497 bcj2_avail_out -= bytes;
1498 *outbytes = o_avail_out - bcj2_avail_out;
1505 free_decompression(struct archive_read *a, struct _7zip *zip)
1510 if (zip->lzstream_valid)
1511 lzma_end(&(zip->lzstream));
1513 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1514 if (zip->bzstream_valid) {
1515 if (BZ2_bzDecompressEnd(&(zip->bzstream)) != BZ_OK) {
1516 archive_set_error(&a->archive,
1518 "Failed to clean up bzip2 decompressor");
1521 zip->bzstream_valid = 0;
1525 if (zip->stream_valid) {
1526 if (inflateEnd(&(zip->stream)) != Z_OK) {
1527 archive_set_error(&a->archive,
1529 "Failed to clean up zlib decompressor");
1532 zip->stream_valid = 0;
1535 if (zip->ppmd7_valid) {
1536 __archive_ppmd7_functions.Ppmd7_Free(
1537 &zip->ppmd7_context, &g_szalloc);
1538 zip->ppmd7_valid = 0;
1544 parse_7zip_uint64(struct archive_read *a, uint64_t *val)
1546 const unsigned char *p;
1547 unsigned char avail, mask;
1550 if ((p = header_bytes(a, 1)) == NULL)
1555 for (i = 0; i < 8; i++) {
1557 if ((p = header_bytes(a, 1)) == NULL)
1559 *val |= ((uint64_t)*p) << (8 * i);
1563 *val += (avail & (mask -1)) << (8 * i);
1570 read_Bools(struct archive_read *a, unsigned char *data, size_t num)
1572 const unsigned char *p;
1573 unsigned i, mask = 0, avail;
1575 for (i = 0; i < num; i++) {
1577 if ((p = header_bytes(a, 1)) == NULL)
1582 data[i] = (avail & mask)?1:0;
1589 free_Digest(struct _7z_digests *d)
1596 read_Digests(struct archive_read *a, struct _7z_digests *d, size_t num)
1598 const unsigned char *p;
1601 memset(d, 0, sizeof(*d));
1604 d->defineds = malloc(num);
1605 if (d->defineds == NULL)
1610 if ((p = header_bytes(a, 1)) == NULL)
1613 if (read_Bools(a, d->defineds, num) < 0)
1616 /* All are defined */
1617 memset(d->defineds, 1, num);
1619 d->digests = calloc(num, sizeof(*d->digests));
1620 if (d->digests == NULL)
1622 for (i = 0; i < num; i++) {
1623 if (d->defineds[i]) {
1624 if ((p = header_bytes(a, 4)) == NULL)
1626 d->digests[i] = archive_le32dec(p);
1634 free_PackInfo(struct _7z_pack_info *pi)
1637 free(pi->positions);
1638 free_Digest(&(pi->digest));
1642 read_PackInfo(struct archive_read *a, struct _7z_pack_info *pi)
1644 const unsigned char *p;
1647 memset(pi, 0, sizeof(*pi));
1652 if (parse_7zip_uint64(a, &(pi->pos)) < 0)
1656 * Read NumPackStreams.
1658 if (parse_7zip_uint64(a, &(pi->numPackStreams)) < 0)
1660 if (pi->numPackStreams == 0)
1662 if (1000000 < pi->numPackStreams)
1666 * Read PackSizes[num]
1668 if ((p = header_bytes(a, 1)) == NULL)
1671 /* PackSizes[num] are not present. */
1675 pi->sizes = calloc(pi->numPackStreams, sizeof(uint64_t));
1676 pi->positions = calloc(pi->numPackStreams, sizeof(uint64_t));
1677 if (pi->sizes == NULL || pi->positions == NULL)
1680 for (i = 0; i < pi->numPackStreams; i++) {
1681 if (parse_7zip_uint64(a, &(pi->sizes[i])) < 0)
1686 * Read PackStreamDigests[num]
1688 if ((p = header_bytes(a, 1)) == NULL)
1691 /* PackStreamDigests[num] are not present. */
1692 pi->digest.defineds =
1693 calloc(pi->numPackStreams, sizeof(*pi->digest.defineds));
1694 pi->digest.digests =
1695 calloc(pi->numPackStreams, sizeof(*pi->digest.digests));
1696 if (pi->digest.defineds == NULL || pi->digest.digests == NULL)
1704 if (read_Digests(a, &(pi->digest), pi->numPackStreams) < 0)
1708 * Must be marked by kEnd.
1710 if ((p = header_bytes(a, 1)) == NULL)
1718 free_Folder(struct _7z_folder *f)
1723 for (i = 0; i< f->numCoders; i++) {
1724 free(f->coders[i].properties);
1729 free(f->packedStreams);
1730 free(f->unPackSize);
1734 read_Folder(struct archive_read *a, struct _7z_folder *f)
1736 struct _7zip *zip = (struct _7zip *)a->format->data;
1737 const unsigned char *p;
1738 uint64_t numInStreamsTotal = 0;
1739 uint64_t numOutStreamsTotal = 0;
1742 memset(f, 0, sizeof(*f));
1747 if (parse_7zip_uint64(a, &(f->numCoders)) < 0)
1749 if (f->numCoders > 4)
1750 /* Too many coders. */
1753 f->coders = calloc(f->numCoders, sizeof(*f->coders));
1754 if (f->coders == NULL)
1756 for (i = 0; i< f->numCoders; i++) {
1760 if ((p = header_bytes(a, 1)) == NULL)
1766 * 5: 0 - No Attributes
1767 * 1 - There are Attributes;
1770 codec_size = *p & 0xf;
1771 simple = (*p & 0x10)?0:1;
1774 return (-1);/* Not supported. */
1777 * Read Decompression Method IDs.
1779 if ((p = header_bytes(a, codec_size)) == NULL)
1782 f->coders[i].codec = decode_codec_id(p, codec_size);
1785 f->coders[i].numInStreams = 1;
1786 f->coders[i].numOutStreams = 1;
1788 if (parse_7zip_uint64(
1789 a, &(f->coders[i].numInStreams)) < 0)
1791 if (1000000 < f->coders[i].numInStreams)
1793 if (parse_7zip_uint64(
1794 a, &(f->coders[i].numOutStreams)) < 0)
1796 if (1000000 < f->coders[i].numOutStreams)
1801 if (parse_7zip_uint64(
1802 a, &(f->coders[i].propertiesSize)) < 0)
1804 if ((p = header_bytes(
1805 a, f->coders[i].propertiesSize)) == NULL)
1807 f->coders[i].properties =
1808 malloc(f->coders[i].propertiesSize);
1809 if (f->coders[i].properties == NULL)
1811 memcpy(f->coders[i].properties, p,
1812 f->coders[i].propertiesSize);
1815 numInStreamsTotal += f->coders[i].numInStreams;
1816 numOutStreamsTotal += f->coders[i].numOutStreams;
1819 if (numOutStreamsTotal == 0 ||
1820 numInStreamsTotal < numOutStreamsTotal-1)
1823 f->numBindPairs = numOutStreamsTotal - 1;
1824 if (zip->header_bytes_remaining < f->numBindPairs)
1826 f->bindPairs = calloc(f->numBindPairs, sizeof(*f->bindPairs));
1827 if (f->bindPairs == NULL)
1829 for (i = 0; i < f->numBindPairs; i++) {
1830 if (parse_7zip_uint64(a, &(f->bindPairs[i].inIndex)) < 0)
1832 if (1000000 < f->bindPairs[i].inIndex)
1834 if (parse_7zip_uint64(a, &(f->bindPairs[i].outIndex)) < 0)
1836 if (1000000 < f->bindPairs[i].outIndex)
1840 f->numPackedStreams = numInStreamsTotal - f->numBindPairs;
1842 calloc(f->numPackedStreams, sizeof(*f->packedStreams));
1843 if (f->packedStreams == NULL)
1845 if (f->numPackedStreams == 1) {
1846 for (i = 0; i < numInStreamsTotal; i++) {
1848 for (j = 0; j < f->numBindPairs; j++) {
1849 if (f->bindPairs[j].inIndex == i)
1852 if (j == f->numBindPairs)
1855 if (i == numInStreamsTotal)
1857 f->packedStreams[0] = i;
1859 for (i = 0; i < f->numPackedStreams; i++) {
1860 if (parse_7zip_uint64(a, &(f->packedStreams[i])) < 0)
1862 if (1000000 < f->packedStreams[i])
1866 f->numInStreams = numInStreamsTotal;
1867 f->numOutStreams = numOutStreamsTotal;
1873 free_CodersInfo(struct _7z_coders_info *ci)
1878 for (i = 0; i < ci->numFolders; i++)
1879 free_Folder(&(ci->folders[i]));
1885 read_CodersInfo(struct archive_read *a, struct _7z_coders_info *ci)
1887 const unsigned char *p;
1888 struct _7z_digests digest;
1891 memset(ci, 0, sizeof(*ci));
1892 memset(&digest, 0, sizeof(digest));
1894 if ((p = header_bytes(a, 1)) == NULL)
1902 if (parse_7zip_uint64(a, &(ci->numFolders)) < 0)
1904 if (1000000 < ci->numFolders)
1910 if ((p = header_bytes(a, 1)) == NULL)
1914 ci->folders = calloc(ci->numFolders, sizeof(*ci->folders));
1915 if (ci->folders == NULL)
1917 for (i = 0; i < ci->numFolders; i++) {
1918 if (read_Folder(a, &(ci->folders[i])) < 0)
1923 if (parse_7zip_uint64(a, &(ci->dataStreamIndex)) < 0)
1925 if (1000000 < ci->dataStreamIndex)
1930 if ((p = header_bytes(a, 1)) == NULL)
1932 if (*p != kCodersUnPackSize)
1935 for (i = 0; i < ci->numFolders; i++) {
1936 struct _7z_folder *folder = &(ci->folders[i]);
1939 folder->unPackSize =
1940 calloc(folder->numOutStreams, sizeof(*folder->unPackSize));
1941 if (folder->unPackSize == NULL)
1943 for (j = 0; j < folder->numOutStreams; j++) {
1944 if (parse_7zip_uint64(a, &(folder->unPackSize[j])) < 0)
1952 if ((p = header_bytes(a, 1)) == NULL)
1958 if (read_Digests(a, &digest, ci->numFolders) < 0)
1960 for (i = 0; i < ci->numFolders; i++) {
1961 ci->folders[i].digest_defined = digest.defineds[i];
1962 ci->folders[i].digest = digest.digests[i];
1968 if ((p = header_bytes(a, 1)) == NULL)
1972 free_Digest(&digest);
1975 free_Digest(&digest);
1980 folder_uncompressed_size(struct _7z_folder *f)
1982 int n = f->numOutStreams;
1983 unsigned pairs = f->numBindPairs;
1987 for (i = 0; i < pairs; i++) {
1988 if (f->bindPairs[i].outIndex == n)
1992 return (f->unPackSize[n]);
1998 free_SubStreamsInfo(struct _7z_substream_info *ss)
2000 free(ss->unpackSizes);
2001 free(ss->digestsDefined);
2006 read_SubStreamsInfo(struct archive_read *a, struct _7z_substream_info *ss,
2007 struct _7z_folder *f, size_t numFolders)
2009 const unsigned char *p;
2011 size_t unpack_streams;
2014 uint32_t numDigests;
2016 memset(ss, 0, sizeof(*ss));
2018 for (i = 0; i < numFolders; i++)
2019 f[i].numUnpackStreams = 1;
2021 if ((p = header_bytes(a, 1)) == NULL)
2025 if (type == kNumUnPackStream) {
2027 for (i = 0; i < numFolders; i++) {
2028 if (parse_7zip_uint64(a, &(f[i].numUnpackStreams)) < 0)
2030 if (1000000 < f[i].numUnpackStreams)
2032 unpack_streams += f[i].numUnpackStreams;
2034 if ((p = header_bytes(a, 1)) == NULL)
2038 unpack_streams = numFolders;
2040 ss->unpack_streams = unpack_streams;
2041 if (unpack_streams) {
2042 ss->unpackSizes = calloc(unpack_streams,
2043 sizeof(*ss->unpackSizes));
2044 ss->digestsDefined = calloc(unpack_streams,
2045 sizeof(*ss->digestsDefined));
2046 ss->digests = calloc(unpack_streams,
2047 sizeof(*ss->digests));
2048 if (ss->unpackSizes == NULL || ss->digestsDefined == NULL ||
2049 ss->digests == NULL)
2053 usizes = ss->unpackSizes;
2054 for (i = 0; i < numFolders; i++) {
2058 if (f[i].numUnpackStreams == 0)
2062 if (type == kSize) {
2063 for (pack = 1; pack < f[i].numUnpackStreams; pack++) {
2064 if (parse_7zip_uint64(a, usizes) < 0)
2069 *usizes++ = folder_uncompressed_size(&f[i]) - sum;
2072 if (type == kSize) {
2073 if ((p = header_bytes(a, 1)) == NULL)
2078 for (i = 0; i < unpack_streams; i++) {
2079 ss->digestsDefined[i] = 0;
2084 for (i = 0; i < numFolders; i++) {
2085 if (f[i].numUnpackStreams != 1 || !f[i].digest_defined)
2086 numDigests += f[i].numUnpackStreams;
2090 struct _7z_digests tmpDigests;
2091 unsigned char *digestsDefined = ss->digestsDefined;
2092 uint32_t * digests = ss->digests;
2095 memset(&tmpDigests, 0, sizeof(tmpDigests));
2096 if (read_Digests(a, &(tmpDigests), numDigests) < 0) {
2097 free_Digest(&tmpDigests);
2100 for (i = 0; i < numFolders; i++) {
2101 if (f[i].numUnpackStreams == 1 && f[i].digest_defined) {
2102 *digestsDefined++ = 1;
2103 *digests++ = f[i].digest;
2107 for (j = 0; j < f[i].numUnpackStreams;
2110 tmpDigests.defineds[di];
2112 tmpDigests.digests[di];
2116 free_Digest(&tmpDigests);
2117 if ((p = header_bytes(a, 1)) == NULL)
2131 free_StreamsInfo(struct _7z_stream_info *si)
2133 free_PackInfo(&(si->pi));
2134 free_CodersInfo(&(si->ci));
2135 free_SubStreamsInfo(&(si->ss));
2139 read_StreamsInfo(struct archive_read *a, struct _7z_stream_info *si)
2141 struct _7zip *zip = (struct _7zip *)a->format->data;
2142 const unsigned char *p;
2145 memset(si, 0, sizeof(*si));
2147 if ((p = header_bytes(a, 1)) == NULL)
2149 if (*p == kPackInfo) {
2152 if (read_PackInfo(a, &(si->pi)) < 0)
2155 if (si->pi.positions == NULL || si->pi.sizes == NULL)
2158 * Calculate packed stream positions.
2160 packPos = si->pi.pos;
2161 for (i = 0; i < si->pi.numPackStreams; i++) {
2162 si->pi.positions[i] = packPos;
2163 packPos += si->pi.sizes[i];
2164 if (packPos > zip->header_offset)
2167 if ((p = header_bytes(a, 1)) == NULL)
2170 if (*p == kUnPackInfo) {
2172 struct _7z_folder *f;
2174 if (read_CodersInfo(a, &(si->ci)) < 0)
2178 * Calculate packed stream indexes.
2182 for (i = 0; i < si->ci.numFolders; i++) {
2183 f[i].packIndex = packIndex;
2184 packIndex += f[i].numPackedStreams;
2185 if (packIndex > si->pi.numPackStreams)
2188 if ((p = header_bytes(a, 1)) == NULL)
2192 if (*p == kSubStreamsInfo) {
2193 if (read_SubStreamsInfo(a, &(si->ss),
2194 si->ci.folders, si->ci.numFolders) < 0)
2196 if ((p = header_bytes(a, 1)) == NULL)
2209 free_Header(struct _7z_header_info *h)
2211 free(h->emptyStreamBools);
2212 free(h->emptyFileBools);
2218 read_Header(struct archive_read *a, struct _7z_header_info *h,
2219 int check_header_id)
2221 struct _7zip *zip = (struct _7zip *)a->format->data;
2222 const unsigned char *p;
2223 struct _7z_folder *folders;
2224 struct _7z_stream_info *si = &(zip->si);
2225 struct _7zip_entry *entries;
2226 uint32_t folderIndex, indexInFolder;
2228 int eindex, empty_streams, sindex;
2230 if (check_header_id) {
2234 if ((p = header_bytes(a, 1)) == NULL)
2241 * Read ArchiveProperties.
2243 if ((p = header_bytes(a, 1)) == NULL)
2245 if (*p == kArchiveProperties) {
2248 if ((p = header_bytes(a, 1)) == NULL)
2252 if (parse_7zip_uint64(a, &size) < 0)
2255 if ((p = header_bytes(a, 1)) == NULL)
2260 * Read MainStreamsInfo.
2262 if (*p == kMainStreamsInfo) {
2263 if (read_StreamsInfo(a, &(zip->si)) < 0)
2265 if ((p = header_bytes(a, 1)) == NULL)
2274 if (*p != kFilesInfo)
2277 if (parse_7zip_uint64(a, &(zip->numFiles)) < 0)
2279 if (1000000 < zip->numFiles)
2282 zip->entries = calloc(zip->numFiles, sizeof(*zip->entries));
2283 if (zip->entries == NULL)
2285 entries = zip->entries;
2293 if ((p = header_bytes(a, 1)) == NULL)
2299 if (parse_7zip_uint64(a, &size) < 0)
2301 if (zip->header_bytes_remaining < size)
2307 h->emptyStreamBools = calloc(zip->numFiles,
2308 sizeof(*h->emptyStreamBools));
2309 if (h->emptyStreamBools == NULL)
2312 a, h->emptyStreamBools, zip->numFiles) < 0)
2315 for (i = 0; i < zip->numFiles; i++) {
2316 if (h->emptyStreamBools[i])
2321 h->emptyFileBools = calloc(empty_streams,
2322 sizeof(*h->emptyFileBools));
2323 if (h->emptyFileBools == NULL)
2325 if (read_Bools(a, h->emptyFileBools, empty_streams) < 0)
2329 h->antiBools = calloc(empty_streams,
2330 sizeof(*h->antiBools));
2331 if (h->antiBools == NULL)
2333 if (read_Bools(a, h->antiBools, empty_streams) < 0)
2339 if (read_Times(a, h, type) < 0)
2347 /* Skip one byte. */
2348 if ((p = header_bytes(a, 1)) == NULL)
2352 if ((ll & 1) || ll < zip->numFiles * 4)
2355 zip->entry_names = malloc(ll);
2356 if (zip->entry_names == NULL)
2358 np = zip->entry_names;
2361 * Copy whole file names.
2362 * NOTE: This loop prevents from expanding
2363 * the uncompressed buffer in order not to
2364 * use extra memory resource.
2368 if (nb > UBUFF_SIZE)
2372 if ((p = header_bytes(a, b)) == NULL)
2378 np = zip->entry_names;
2381 for (i = 0; i < zip->numFiles; i++) {
2382 entries[i].utf16name = np;
2383 #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
2384 entries[i].wname = (wchar_t *)np;
2387 /* Find a terminator. */
2388 while (nl >= 2 && (np[0] || np[1])) {
2393 return (-1);/* Terminator not found */
2394 entries[i].name_len = np - entries[i].utf16name;
2404 if ((p = header_bytes(a, 2)) == NULL)
2407 h->attrBools = calloc(zip->numFiles,
2408 sizeof(*h->attrBools));
2409 if (h->attrBools == NULL)
2412 memset(h->attrBools, 1, zip->numFiles);
2414 if (read_Bools(a, h->attrBools,
2418 for (i = 0; i < zip->numFiles; i++) {
2419 if (h->attrBools[i]) {
2420 if ((p = header_bytes(a, 4)) == NULL)
2422 entries[i].attr = archive_le32dec(p);
2428 if (header_bytes(a, ll) == NULL)
2435 * Set up entry's attributes.
2437 folders = si->ci.folders;
2438 eindex = sindex = 0;
2439 folderIndex = indexInFolder = 0;
2440 for (i = 0; i < zip->numFiles; i++) {
2441 if (h->emptyStreamBools == NULL || h->emptyStreamBools[i] == 0)
2442 entries[i].flg |= HAS_STREAM;
2443 /* The high 16 bits of attributes is a posix file mode. */
2444 entries[i].mode = entries[i].attr >> 16;
2445 if (entries[i].flg & HAS_STREAM) {
2446 if ((size_t)sindex >= si->ss.unpack_streams)
2448 if (entries[i].mode == 0)
2449 entries[i].mode = AE_IFREG | 0777;
2450 if (si->ss.digestsDefined[sindex])
2451 entries[i].flg |= CRC32_IS_SET;
2452 entries[i].ssIndex = sindex;
2456 if (h->emptyFileBools == NULL)
2459 if (h->emptyFileBools[eindex])
2465 if (entries[i].mode == 0) {
2467 entries[i].mode = AE_IFDIR | 0777;
2469 entries[i].mode = AE_IFREG | 0777;
2471 (entries[i].mode & AE_IFMT) != AE_IFDIR) {
2472 entries[i].mode &= ~AE_IFMT;
2473 entries[i].mode |= AE_IFDIR;
2475 if ((entries[i].mode & AE_IFMT) == AE_IFDIR &&
2476 entries[i].name_len >= 2 &&
2477 (entries[i].utf16name[entries[i].name_len-2] != '/' ||
2478 entries[i].utf16name[entries[i].name_len-1] != 0)) {
2479 entries[i].utf16name[entries[i].name_len] = '/';
2480 entries[i].utf16name[entries[i].name_len+1] = 0;
2481 entries[i].name_len += 2;
2483 entries[i].ssIndex = -1;
2485 if (entries[i].attr & 0x01)
2486 entries[i].mode &= ~0222;/* Read only. */
2488 if ((entries[i].flg & HAS_STREAM) == 0 && indexInFolder == 0) {
2490 * The entry is an empty file or a directory file,
2491 * those both have no contents.
2493 entries[i].folderIndex = -1;
2496 if (indexInFolder == 0) {
2498 if (folderIndex >= si->ci.numFolders)
2500 if (folders[folderIndex].numUnpackStreams)
2505 entries[i].folderIndex = folderIndex;
2506 if ((entries[i].flg & HAS_STREAM) == 0)
2509 if (indexInFolder >= folders[folderIndex].numUnpackStreams) {
2518 #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
2520 fileTimeToUtc(uint64_t fileTime, time_t *time, long *ns)
2523 if (fileTime >= EPOC_TIME) {
2524 fileTime -= EPOC_TIME;
2525 /* milli seconds base */
2526 *time = (time_t)(fileTime / 10000000);
2527 /* nano seconds base */
2528 *ns = (long)(fileTime % 10000000) * 100;
2536 read_Times(struct archive_read *a, struct _7z_header_info *h, int type)
2538 struct _7zip *zip = (struct _7zip *)a->format->data;
2539 const unsigned char *p;
2540 struct _7zip_entry *entries = zip->entries;
2541 unsigned char *timeBools;
2545 timeBools = calloc(zip->numFiles, sizeof(*timeBools));
2546 if (timeBools == NULL)
2549 /* Read allAreDefined. */
2550 if ((p = header_bytes(a, 1)) == NULL)
2554 memset(timeBools, 1, zip->numFiles);
2556 if (read_Bools(a, timeBools, zip->numFiles) < 0)
2560 /* Read external. */
2561 if ((p = header_bytes(a, 1)) == NULL)
2564 if (parse_7zip_uint64(a, &(h->dataIndex)) < 0)
2566 if (1000000 < h->dataIndex)
2570 for (i = 0; i < zip->numFiles; i++) {
2573 if ((p = header_bytes(a, 8)) == NULL)
2577 fileTimeToUtc(archive_le64dec(p),
2578 &(entries[i].ctime),
2579 &(entries[i].ctime_ns));
2580 entries[i].flg |= CTIME_IS_SET;
2583 fileTimeToUtc(archive_le64dec(p),
2584 &(entries[i].atime),
2585 &(entries[i].atime_ns));
2586 entries[i].flg |= ATIME_IS_SET;
2589 fileTimeToUtc(archive_le64dec(p),
2590 &(entries[i].mtime),
2591 &(entries[i].mtime_ns));
2592 entries[i].flg |= MTIME_IS_SET;
2605 decode_encoded_header_info(struct archive_read *a, struct _7z_stream_info *si)
2607 struct _7zip *zip = (struct _7zip *)a->format->data;
2610 if (read_StreamsInfo(a, si) < 0) {
2611 if (errno == ENOMEM)
2612 archive_set_error(&a->archive, -1,
2613 "Couldn't allocate memory");
2615 archive_set_error(&a->archive, -1,
2616 "Malformed 7-Zip archive");
2617 return (ARCHIVE_FATAL);
2620 if (si->pi.numPackStreams == 0 || si->ci.numFolders == 0) {
2621 archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
2622 return (ARCHIVE_FATAL);
2625 if (zip->header_offset < si->pi.pos + si->pi.sizes[0] ||
2626 (int64_t)(si->pi.pos + si->pi.sizes[0]) < 0 ||
2627 si->pi.sizes[0] == 0 || (int64_t)si->pi.pos < 0) {
2628 archive_set_error(&a->archive, -1, "Malformed Header offset");
2629 return (ARCHIVE_FATAL);
2632 return (ARCHIVE_OK);
2635 static const unsigned char *
2636 header_bytes(struct archive_read *a, size_t rbytes)
2638 struct _7zip *zip = (struct _7zip *)a->format->data;
2639 const unsigned char *p;
2641 if (zip->header_bytes_remaining < rbytes)
2643 if (zip->pack_stream_bytes_unconsumed)
2646 if (zip->header_is_encoded == 0) {
2647 p = __archive_read_ahead(a, rbytes, NULL);
2650 zip->header_bytes_remaining -= rbytes;
2651 zip->pack_stream_bytes_unconsumed = rbytes;
2656 bytes = read_stream(a, &buff, rbytes, rbytes);
2659 zip->header_bytes_remaining -= bytes;
2663 /* Update checksum */
2664 zip->header_crc32 = crc32(zip->header_crc32, p, rbytes);
2669 slurp_central_directory(struct archive_read *a, struct _7zip *zip,
2670 struct _7z_header_info *header)
2672 const unsigned char *p;
2673 uint64_t next_header_offset;
2674 uint64_t next_header_size;
2675 uint32_t next_header_crc;
2676 ssize_t bytes_avail;
2677 int check_header_crc, r;
2679 if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
2680 return (ARCHIVE_FATAL);
2682 if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
2683 /* This is an executable ? Must be self-extracting... */
2684 r = skip_sfx(a, bytes_avail);
2685 if (r < ARCHIVE_WARN)
2687 if ((p = __archive_read_ahead(a, 32, &bytes_avail)) == NULL)
2688 return (ARCHIVE_FATAL);
2690 zip->seek_base += 32;
2692 if (memcmp(p, _7ZIP_SIGNATURE, 6) != 0) {
2693 archive_set_error(&a->archive, -1, "Not 7-Zip archive file");
2694 return (ARCHIVE_FATAL);
2698 if (crc32(0, (unsigned char *)p + 12, 20) != archive_le32dec(p + 8)) {
2699 archive_set_error(&a->archive, -1, "Header CRC error");
2700 return (ARCHIVE_FATAL);
2703 next_header_offset = archive_le64dec(p + 12);
2704 next_header_size = archive_le64dec(p + 20);
2705 next_header_crc = archive_le32dec(p + 28);
2707 if (next_header_size == 0)
2708 /* There is no entry in an archive file. */
2709 return (ARCHIVE_EOF);
2711 if (((int64_t)next_header_offset) < 0) {
2712 archive_set_error(&a->archive, -1, "Malformed 7-Zip archive");
2713 return (ARCHIVE_FATAL);
2715 __archive_read_consume(a, 32);
2716 if (next_header_offset != 0) {
2717 if (bytes_avail >= next_header_offset)
2718 __archive_read_consume(a, next_header_offset);
2719 else if (__archive_read_seek(a,
2720 next_header_offset + zip->seek_base, SEEK_SET) < 0)
2721 return (ARCHIVE_FATAL);
2723 zip->stream_offset = next_header_offset;
2724 zip->header_offset = next_header_offset;
2725 zip->header_bytes_remaining = next_header_size;
2726 zip->header_crc32 = 0;
2727 zip->header_is_encoded = 0;
2728 zip->header_is_being_read = 1;
2729 check_header_crc = 1;
2731 if ((p = header_bytes(a, 1)) == NULL) {
2732 archive_set_error(&a->archive,
2733 ARCHIVE_ERRNO_FILE_FORMAT,
2734 "Truncated 7-Zip file body");
2735 return (ARCHIVE_FATAL);
2737 /* Parse ArchiveProperties. */
2739 case kEncodedHeader:
2741 * The archive has an encoded header and we have to decode it
2742 * in order to parse the header correctly.
2744 r = decode_encoded_header_info(a, &(zip->si));
2746 /* Check the EncodedHeader CRC.*/
2747 if (r == 0 && zip->header_crc32 != next_header_crc) {
2748 archive_set_error(&a->archive, -1,
2749 "Damaged 7-Zip archive");
2753 if (zip->si.ci.folders[0].digest_defined)
2754 next_header_crc = zip->si.ci.folders[0].digest;
2756 check_header_crc = 0;
2757 if (zip->pack_stream_bytes_unconsumed)
2759 r = setup_decode_folder(a, zip->si.ci.folders, 1);
2761 zip->header_bytes_remaining =
2762 zip->folder_outbytes_remaining;
2766 /* Clean up StreamsInfo. */
2767 free_StreamsInfo(&(zip->si));
2768 memset(&(zip->si), 0, sizeof(zip->si));
2770 return (ARCHIVE_FATAL);
2771 zip->header_is_encoded = 1;
2772 zip->header_crc32 = 0;
2779 r = read_Header(a, header, zip->header_is_encoded);
2781 if (errno == ENOMEM)
2782 archive_set_error(&a->archive, -1,
2783 "Couldn't allocate memory");
2785 archive_set_error(&a->archive, -1,
2786 "Damaged 7-Zip archive");
2787 return (ARCHIVE_FATAL);
2793 if ((p = header_bytes(a, 1)) == NULL ||*p != kEnd) {
2794 archive_set_error(&a->archive, -1,
2795 "Malformed 7-Zip archive");
2796 return (ARCHIVE_FATAL);
2799 /* Check the Header CRC.*/
2800 if (check_header_crc && zip->header_crc32 != next_header_crc) {
2801 archive_set_error(&a->archive, -1,
2802 "Malformed 7-Zip archive");
2803 return (ARCHIVE_FATAL);
2807 archive_set_error(&a->archive, -1,
2808 "Unexpected Property ID = %X", p[0]);
2809 return (ARCHIVE_FATAL);
2812 /* Clean up variables be used for decoding the archive header */
2813 zip->pack_stream_remaining = 0;
2814 zip->pack_stream_index = 0;
2815 zip->folder_outbytes_remaining = 0;
2816 zip->uncompressed_buffer_bytes_remaining = 0;
2817 zip->pack_stream_bytes_unconsumed = 0;
2818 zip->header_is_being_read = 0;
2820 return (ARCHIVE_OK);
2824 get_uncompressed_data(struct archive_read *a, const void **buff, size_t size,
2827 struct _7zip *zip = (struct _7zip *)a->format->data;
2828 ssize_t bytes_avail;
2830 if (zip->codec == _7Z_COPY && zip->codec2 == -1) {
2834 * Note: '1' here is a performance optimization.
2835 * Recall that the decompression layer returns a count of
2836 * available bytes; asking for more than that forces the
2837 * decompressor to combine reads by copying data.
2839 *buff = __archive_read_ahead(a, 1, &bytes_avail);
2840 if (bytes_avail <= 0) {
2841 archive_set_error(&a->archive,
2842 ARCHIVE_ERRNO_FILE_FORMAT,
2843 "Truncated 7-Zip file data");
2844 return (ARCHIVE_FATAL);
2846 if ((size_t)bytes_avail >
2847 zip->uncompressed_buffer_bytes_remaining)
2848 bytes_avail = (ssize_t)
2849 zip->uncompressed_buffer_bytes_remaining;
2850 if ((size_t)bytes_avail > size)
2851 bytes_avail = (ssize_t)size;
2853 zip->pack_stream_bytes_unconsumed = bytes_avail;
2854 } else if (zip->uncompressed_buffer_pointer == NULL) {
2855 /* Decompression has failed. */
2856 archive_set_error(&(a->archive),
2857 ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
2858 return (ARCHIVE_FATAL);
2861 if (minimum > zip->uncompressed_buffer_bytes_remaining) {
2863 * If remaining uncompressed data size is less than
2864 * the minimum size, fill the buffer up to the
2867 if (extract_pack_stream(a, minimum) < 0)
2868 return (ARCHIVE_FATAL);
2870 if (size > zip->uncompressed_buffer_bytes_remaining)
2871 bytes_avail = (ssize_t)
2872 zip->uncompressed_buffer_bytes_remaining;
2874 bytes_avail = (ssize_t)size;
2875 *buff = zip->uncompressed_buffer_pointer;
2876 zip->uncompressed_buffer_pointer += bytes_avail;
2878 zip->uncompressed_buffer_bytes_remaining -= bytes_avail;
2879 return (bytes_avail);
2883 extract_pack_stream(struct archive_read *a, size_t minimum)
2885 struct _7zip *zip = (struct _7zip *)a->format->data;
2886 ssize_t bytes_avail;
2889 if (zip->codec == _7Z_COPY && zip->codec2 == -1) {
2892 if (__archive_read_ahead(a, minimum, &bytes_avail) == NULL
2893 || bytes_avail <= 0) {
2894 archive_set_error(&a->archive,
2895 ARCHIVE_ERRNO_FILE_FORMAT,
2896 "Truncated 7-Zip file body");
2897 return (ARCHIVE_FATAL);
2899 if (bytes_avail > zip->pack_stream_inbytes_remaining)
2900 bytes_avail = zip->pack_stream_inbytes_remaining;
2901 zip->pack_stream_inbytes_remaining -= bytes_avail;
2902 if (bytes_avail > zip->folder_outbytes_remaining)
2903 bytes_avail = zip->folder_outbytes_remaining;
2904 zip->folder_outbytes_remaining -= bytes_avail;
2905 zip->uncompressed_buffer_bytes_remaining = bytes_avail;
2906 return (ARCHIVE_OK);
2909 /* If the buffer hasn't been allocated, allocate it now. */
2910 if (zip->uncompressed_buffer == NULL) {
2911 zip->uncompressed_buffer_size = UBUFF_SIZE;
2912 if (zip->uncompressed_buffer_size < minimum) {
2913 zip->uncompressed_buffer_size = minimum + 1023;
2914 zip->uncompressed_buffer_size &= ~0x3ff;
2916 zip->uncompressed_buffer =
2917 malloc(zip->uncompressed_buffer_size);
2918 if (zip->uncompressed_buffer == NULL) {
2919 archive_set_error(&a->archive, ENOMEM,
2920 "No memory for 7-Zip decompression");
2921 return (ARCHIVE_FATAL);
2923 zip->uncompressed_buffer_bytes_remaining = 0;
2924 } else if (zip->uncompressed_buffer_size < minimum ||
2925 zip->uncompressed_buffer_bytes_remaining < minimum) {
2927 * Make sure the uncompressed buffer can have bytes
2928 * at least `minimum' bytes.
2929 * NOTE: This case happen when reading the header.
2932 if (zip->uncompressed_buffer_pointer != 0)
2933 used = zip->uncompressed_buffer_pointer -
2934 zip->uncompressed_buffer;
2937 if (zip->uncompressed_buffer_size < minimum) {
2939 * Expand the uncompressed buffer up to
2942 zip->uncompressed_buffer_size = minimum + 1023;
2943 zip->uncompressed_buffer_size &= ~0x3ff;
2944 zip->uncompressed_buffer =
2945 realloc(zip->uncompressed_buffer,
2946 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);
2954 * Move unconsumed bytes to the head.
2957 memmove(zip->uncompressed_buffer,
2958 zip->uncompressed_buffer + used,
2959 zip->uncompressed_buffer_bytes_remaining);
2962 zip->uncompressed_buffer_bytes_remaining = 0;
2963 zip->uncompressed_buffer_pointer = NULL;
2965 size_t bytes_in, bytes_out;
2966 const void *buff_in;
2967 unsigned char *buff_out;
2971 * Note: '1' here is a performance optimization.
2972 * Recall that the decompression layer returns a count of
2973 * available bytes; asking for more than that forces the
2974 * decompressor to combine reads by copying data.
2976 buff_in = __archive_read_ahead(a, 1, &bytes_avail);
2977 if (bytes_avail <= 0) {
2978 archive_set_error(&a->archive,
2979 ARCHIVE_ERRNO_FILE_FORMAT,
2980 "Truncated 7-Zip file body");
2981 return (ARCHIVE_FATAL);
2984 buff_out = zip->uncompressed_buffer
2985 + zip->uncompressed_buffer_bytes_remaining;
2986 bytes_out = zip->uncompressed_buffer_size
2987 - zip->uncompressed_buffer_bytes_remaining;
2988 bytes_in = bytes_avail;
2989 if (bytes_in > zip->pack_stream_inbytes_remaining)
2990 bytes_in = zip->pack_stream_inbytes_remaining;
2991 /* Drive decompression. */
2992 r = decompress(a, zip, buff_out, &bytes_out,
2993 buff_in, &bytes_in);
3002 return (ARCHIVE_FATAL);
3004 zip->pack_stream_inbytes_remaining -= bytes_in;
3005 if (bytes_out > zip->folder_outbytes_remaining)
3006 bytes_out = zip->folder_outbytes_remaining;
3007 zip->folder_outbytes_remaining -= bytes_out;
3008 zip->uncompressed_buffer_bytes_remaining += bytes_out;
3009 zip->pack_stream_bytes_unconsumed = bytes_in;
3012 * Continue decompression until uncompressed_buffer is full.
3014 if (zip->uncompressed_buffer_bytes_remaining ==
3015 zip->uncompressed_buffer_size)
3017 if (zip->codec2 == _7Z_X86 && zip->odd_bcj_size &&
3018 zip->uncompressed_buffer_bytes_remaining + 5 >
3019 zip->uncompressed_buffer_size)
3021 if (zip->pack_stream_inbytes_remaining == 0 &&
3022 zip->folder_outbytes_remaining == 0)
3024 if (eof || (bytes_in == 0 && bytes_out == 0)) {
3025 archive_set_error(&(a->archive),
3026 ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3027 return (ARCHIVE_FATAL);
3031 if (zip->uncompressed_buffer_bytes_remaining < minimum) {
3032 archive_set_error(&(a->archive),
3033 ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3034 return (ARCHIVE_FATAL);
3036 zip->uncompressed_buffer_pointer = zip->uncompressed_buffer;
3037 return (ARCHIVE_OK);
3041 seek_pack(struct archive_read *a)
3043 struct _7zip *zip = (struct _7zip *)a->format->data;
3044 uint64_t pack_offset;
3046 if (zip->pack_stream_remaining <= 0) {
3047 archive_set_error(&(a->archive),
3048 ARCHIVE_ERRNO_MISC, "Damaged 7-Zip archive");
3049 return (ARCHIVE_FATAL);
3051 zip->pack_stream_inbytes_remaining =
3052 zip->si.pi.sizes[zip->pack_stream_index];
3053 pack_offset = zip->si.pi.positions[zip->pack_stream_index];
3054 if (zip->stream_offset != pack_offset) {
3055 if (0 > __archive_read_seek(a, pack_offset + zip->seek_base,
3057 return (ARCHIVE_FATAL);
3058 zip->stream_offset = pack_offset;
3060 zip->pack_stream_index++;
3061 zip->pack_stream_remaining--;
3062 return (ARCHIVE_OK);
3066 read_stream(struct archive_read *a, const void **buff, size_t size,
3069 struct _7zip *zip = (struct _7zip *)a->format->data;
3070 uint64_t skip_bytes = 0;
3073 if (zip->uncompressed_buffer_bytes_remaining == 0) {
3074 if (zip->pack_stream_inbytes_remaining > 0) {
3075 r = extract_pack_stream(a, 0);
3078 return (get_uncompressed_data(a, buff, size, minimum));
3079 } else if (zip->folder_outbytes_remaining > 0) {
3080 /* Extract a remaining pack stream. */
3081 r = extract_pack_stream(a, 0);
3084 return (get_uncompressed_data(a, buff, size, minimum));
3087 return (get_uncompressed_data(a, buff, size, minimum));
3090 * Current pack stream has been consumed.
3092 if (zip->pack_stream_remaining == 0) {
3093 if (zip->header_is_being_read) {
3094 /* Invalid sequence. This might happen when
3095 * reading a malformed archive. */
3096 archive_set_error(&(a->archive),
3097 ARCHIVE_ERRNO_MISC, "Malformed 7-Zip archive");
3098 return (ARCHIVE_FATAL);
3102 * All current folder's pack streams have been
3103 * consumed. Switch to next folder.
3105 if (zip->folder_index == 0 &&
3106 (zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
3107 || zip->folder_index != zip->entry->folderIndex)) {
3108 zip->folder_index = zip->entry->folderIndex;
3110 zip->si.ci.folders[zip->folder_index].skipped_bytes;
3113 if (zip->folder_index >= zip->si.ci.numFolders) {
3115 * We have consumed all folders and its pack streams.
3120 r = setup_decode_folder(a,
3121 &(zip->si.ci.folders[zip->folder_index]), 0);
3122 if (r != ARCHIVE_OK)
3123 return (ARCHIVE_FATAL);
3125 zip->folder_index++;
3129 * Switch to next pack stream.
3135 /* Extract a new pack stream. */
3136 r = extract_pack_stream(a, 0);
3141 * Skip the bytes we alrady has skipped in skip_stream().
3143 while (skip_bytes) {
3146 if (zip->uncompressed_buffer_bytes_remaining == 0) {
3147 if (zip->pack_stream_inbytes_remaining > 0) {
3148 r = extract_pack_stream(a, 0);
3151 } else if (zip->folder_outbytes_remaining > 0) {
3152 /* Extract a remaining pack stream. */
3153 r = extract_pack_stream(a, 0);
3157 archive_set_error(&a->archive,
3158 ARCHIVE_ERRNO_FILE_FORMAT,
3159 "Truncated 7-Zip file body");
3160 return (ARCHIVE_FATAL);
3163 skipped = get_uncompressed_data(a, buff, skip_bytes, 0);
3166 skip_bytes -= skipped;
3167 if (zip->pack_stream_bytes_unconsumed)
3171 return (get_uncompressed_data(a, buff, size, minimum));
3175 setup_decode_folder(struct archive_read *a, struct _7z_folder *folder,
3178 struct _7zip *zip = (struct _7zip *)a->format->data;
3179 const struct _7z_coder *coder1, *coder2;
3180 const char *cname = (header)?"archive header":"file content";
3182 int r, found_bcj2 = 0;
3185 * Release the memory which the previous folder used for BCJ2.
3187 for (i = 0; i < 3; i++) {
3188 if (zip->sub_stream_buff[i] != NULL)
3189 free(zip->sub_stream_buff[i]);
3190 zip->sub_stream_buff[i] = NULL;
3194 * Initialize a stream reader.
3196 zip->pack_stream_remaining = (unsigned)folder->numPackedStreams;
3197 zip->pack_stream_index = (unsigned)folder->packIndex;
3198 zip->folder_outbytes_remaining = folder_uncompressed_size(folder);
3199 zip->uncompressed_buffer_bytes_remaining = 0;
3202 * Check coder types.
3204 for (i = 0; i < folder->numCoders; i++) {
3205 if (folder->coders[i].codec == _7Z_CRYPTO) {
3206 archive_set_error(&(a->archive),
3208 "The %s is encrypted, "
3209 "but currently not supported", cname);
3210 return (ARCHIVE_FATAL);
3212 if (folder->coders[i].codec == _7Z_X86_BCJ2)
3215 if ((folder->numCoders > 2 && !found_bcj2) || found_bcj2 > 1) {
3216 archive_set_error(&(a->archive),
3218 "The %s is encoded with many filters, "
3219 "but currently not supported", cname);
3220 return (ARCHIVE_FATAL);
3222 coder1 = &(folder->coders[0]);
3223 if (folder->numCoders == 2)
3224 coder2 = &(folder->coders[1]);
3230 * Preparation to decode BCJ2.
3231 * Decoding BCJ2 requires four sources. Those are at least,
3232 * as far as I know, two types of the storage form.
3234 const struct _7z_coder *fc = folder->coders;
3235 static const struct _7z_coder coder_copy = {0, 1, 1, 0, NULL};
3236 const struct _7z_coder *scoder[3] =
3237 {&coder_copy, &coder_copy, &coder_copy};
3240 unsigned char *b[3] = {NULL, NULL, NULL};
3241 uint64_t sunpack[3] ={-1, -1, -1};
3242 size_t s[3] = {0, 0, 0};
3243 int idx[3] = {0, 1, 2};
3245 if (folder->numCoders == 4 && fc[3].codec == _7Z_X86_BCJ2 &&
3246 folder->numInStreams == 7 && folder->numOutStreams == 4 &&
3247 zip->pack_stream_remaining == 4) {
3248 /* Source type 1 made by 7zr or 7z with -m options. */
3249 if (folder->bindPairs[0].inIndex == 5) {
3250 /* The form made by 7zr */
3251 idx[0] = 1; idx[1] = 2; idx[2] = 0;
3252 scoder[1] = &(fc[1]);
3253 scoder[2] = &(fc[0]);
3254 sunpack[1] = folder->unPackSize[1];
3255 sunpack[2] = folder->unPackSize[0];
3259 * NOTE: Some patterns do not work.
3261 * 7z a -m0=BCJ2 -m1=COPY -m2=COPY
3263 * 7z a -m0=BCJ2 -m1=COPY -m2=(any)
3265 * 7z a -m0=BCJ2 -m1=(any) -m2=COPY
3270 * We have to handle this like `pipe' or
3271 * our libarchive7s filter frame work,
3272 * decoding the BCJ2 main stream sequentially,
3273 * m3 -> m2 -> m1 -> BCJ2.
3276 if (fc[0].codec == _7Z_COPY &&
3277 fc[1].codec == _7Z_COPY)
3278 coder1 = &(folder->coders[2]);
3279 else if (fc[0].codec == _7Z_COPY &&
3280 fc[2].codec == _7Z_COPY)
3281 coder1 = &(folder->coders[1]);
3282 else if (fc[1].codec == _7Z_COPY &&
3283 fc[2].codec == _7Z_COPY)
3284 coder1 = &(folder->coders[0]);
3286 archive_set_error(&(a->archive),
3288 "Unsupported form of "
3290 return (ARCHIVE_FATAL);
3294 zip->main_stream_bytes_remaining =
3295 folder->unPackSize[2];
3296 } else if (coder2 != NULL && coder2->codec == _7Z_X86_BCJ2 &&
3297 zip->pack_stream_remaining == 4 &&
3298 folder->numInStreams == 5 && folder->numOutStreams == 2) {
3299 /* Source type 0 made by 7z */
3300 zip->main_stream_bytes_remaining =
3301 folder->unPackSize[0];
3303 /* We got an unexpected form. */
3304 archive_set_error(&(a->archive),
3306 "Unsupported form of BCJ2 streams");
3307 return (ARCHIVE_FATAL);
3310 /* Skip the main stream at this time. */
3311 if ((r = seek_pack(a)) < 0)
3313 zip->pack_stream_bytes_unconsumed =
3314 zip->pack_stream_inbytes_remaining;
3317 /* Read following three sub streams. */
3318 for (i = 0; i < 3; i++) {
3319 const struct _7z_coder *coder = scoder[i];
3321 if ((r = seek_pack(a)) < 0)
3324 if (sunpack[i] == -1)
3325 zip->folder_outbytes_remaining =
3326 zip->pack_stream_inbytes_remaining;
3328 zip->folder_outbytes_remaining = sunpack[i];
3330 r = init_decompression(a, zip, coder, NULL);
3331 if (r != ARCHIVE_OK)
3332 return (ARCHIVE_FATAL);
3334 /* Allocate memory for the decorded data of a sub
3336 b[i] = malloc(zip->folder_outbytes_remaining);
3338 archive_set_error(&a->archive, ENOMEM,
3339 "No memory for 7-Zip decompression");
3340 return (ARCHIVE_FATAL);
3343 /* Extract a sub stream. */
3344 while (zip->pack_stream_inbytes_remaining > 0) {
3345 r = extract_pack_stream(a, 0);
3348 bytes = get_uncompressed_data(a, &buff,
3349 zip->uncompressed_buffer_bytes_remaining,
3352 return ((int)bytes);
3353 memcpy(b[i]+s[i], buff, bytes);
3355 if (zip->pack_stream_bytes_unconsumed)
3360 /* Set the sub streams to the right place. */
3361 for (i = 0; i < 3; i++) {
3362 zip->sub_stream_buff[i] = b[idx[i]];
3363 zip->sub_stream_size[i] = s[idx[i]];
3364 zip->sub_stream_bytes_remaining[i] = s[idx[i]];
3367 /* Allocate memory used for decoded main stream bytes. */
3368 if (zip->tmp_stream_buff == NULL) {
3369 zip->tmp_stream_buff_size = 32 * 1024;
3370 zip->tmp_stream_buff =
3371 malloc(zip->tmp_stream_buff_size);
3372 if (zip->tmp_stream_buff == NULL) {
3373 archive_set_error(&a->archive, ENOMEM,
3374 "No memory for 7-Zip decompression");
3375 return (ARCHIVE_FATAL);
3378 zip->tmp_stream_bytes_avail = 0;
3379 zip->tmp_stream_bytes_remaining = 0;
3380 zip->odd_bcj_size = 0;
3381 zip->bcj2_outPos = 0;
3384 * Reset a stream reader in order to read the main stream
3387 zip->pack_stream_remaining = 1;
3388 zip->pack_stream_index = (unsigned)folder->packIndex;
3389 zip->folder_outbytes_remaining =
3390 folder_uncompressed_size(folder);
3391 zip->uncompressed_buffer_bytes_remaining = 0;
3395 * Initialize the decompressor for the new folder's pack streams.
3397 r = init_decompression(a, zip, coder1, coder2);
3398 if (r != ARCHIVE_OK)
3399 return (ARCHIVE_FATAL);
3400 return (ARCHIVE_OK);
3404 skip_stream(struct archive_read *a, size_t skip_bytes)
3406 struct _7zip *zip = (struct _7zip *)a->format->data;
3408 int64_t skipped_bytes;
3409 size_t bytes = skip_bytes;
3411 if (zip->folder_index == 0) {
3413 * Optimization for a list mode.
3414 * Avoid unncecessary decoding operations.
3416 zip->si.ci.folders[zip->entry->folderIndex].skipped_bytes
3418 return (skip_bytes);
3422 skipped_bytes = read_stream(a, &p, bytes, 0);
3423 if (skipped_bytes < 0)
3424 return (skipped_bytes);
3425 if (skipped_bytes == 0) {
3426 archive_set_error(&a->archive,
3427 ARCHIVE_ERRNO_FILE_FORMAT,
3428 "Truncated 7-Zip file body");
3429 return (ARCHIVE_FATAL);
3431 bytes -= skipped_bytes;
3432 if (zip->pack_stream_bytes_unconsumed)
3435 return (skip_bytes);
3439 * Brought from LZMA SDK.
3441 * Bra86.c -- Converter for x86 code (BCJ)
3442 * 2008-10-04 : Igor Pavlov : Public domain
3446 #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
3449 x86_Init(struct _7zip *zip)
3452 zip->bcj_prevPosT = (size_t)0 - 1;
3453 zip->bcj_prevMask = 0;
3458 x86_Convert(struct _7zip *zip, uint8_t *data, size_t size)
3460 static const uint8_t kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
3461 static const uint8_t kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
3462 size_t bufferPos, prevPosT;
3463 uint32_t ip, prevMask;
3469 prevPosT = zip->bcj_prevPosT;
3470 prevMask = zip->bcj_prevMask;
3474 uint8_t *p = data + bufferPos;
3475 uint8_t *limit = data + size - 4;
3477 for (; p < limit; p++)
3478 if ((*p & 0xFE) == 0xE8)
3480 bufferPos = (size_t)(p - data);
3483 prevPosT = bufferPos - prevPosT;
3487 prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
3488 if (prevMask != 0) {
3490 p[4 - kMaskToBitNumber[prevMask]];
3491 if (!kMaskToAllowedStatus[prevMask] ||
3493 prevPosT = bufferPos;
3494 prevMask = ((prevMask << 1) & 0x7) | 1;
3500 prevPosT = bufferPos;
3502 if (Test86MSByte(p[4])) {
3503 uint32_t src = ((uint32_t)p[4] << 24) |
3504 ((uint32_t)p[3] << 16) | ((uint32_t)p[2] << 8) |
3511 dest = src - (ip + (uint32_t)bufferPos);
3514 index = kMaskToBitNumber[prevMask] * 8;
3515 b = (uint8_t)(dest >> (24 - index));
3516 if (!Test86MSByte(b))
3518 src = dest ^ ((1 << (32 - index)) - 1);
3520 p[4] = (uint8_t)(~(((dest >> 24) & 1) - 1));
3521 p[3] = (uint8_t)(dest >> 16);
3522 p[2] = (uint8_t)(dest >> 8);
3523 p[1] = (uint8_t)dest;
3526 prevMask = ((prevMask << 1) & 0x7) | 1;
3530 zip->bcj_prevPosT = prevPosT;
3531 zip->bcj_prevMask = prevMask;
3532 zip->bcj_ip += bufferPos;
3537 * Brought from LZMA SDK.
3539 * Bcj2.c -- Converter for x86 code (BCJ2)
3540 * 2008-10-04 : Igor Pavlov : Public domain
3544 #define SZ_ERROR_DATA ARCHIVE_FAILED
3546 #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
3547 #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
3549 #define kNumTopBits 24
3550 #define kTopValue ((uint32_t)1 << kNumTopBits)
3552 #define kNumBitModelTotalBits 11
3553 #define kBitModelTotal (1 << kNumBitModelTotalBits)
3554 #define kNumMoveBits 5
3556 #define RC_READ_BYTE (*buffer++)
3557 #define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; }
3558 #define RC_INIT2 zip->bcj2_code = 0; zip->bcj2_range = 0xFFFFFFFF; \
3559 { int i; for (i = 0; i < 5; i++) { RC_TEST; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }}
3561 #define NORMALIZE if (zip->bcj2_range < kTopValue) { RC_TEST; zip->bcj2_range <<= 8; zip->bcj2_code = (zip->bcj2_code << 8) | RC_READ_BYTE; }
3563 #define IF_BIT_0(p) ttt = *(p); bound = (zip->bcj2_range >> kNumBitModelTotalBits) * ttt; if (zip->bcj2_code < bound)
3564 #define UPDATE_0(p) zip->bcj2_range = bound; *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); NORMALIZE;
3565 #define UPDATE_1(p) zip->bcj2_range -= bound; zip->bcj2_code -= bound; *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); NORMALIZE;
3568 Bcj2_Decode(struct _7zip *zip, uint8_t *outBuf, size_t outSize)
3570 size_t inPos = 0, outPos = 0;
3571 const uint8_t *buf0, *buf1, *buf2, *buf3;
3572 size_t size0, size1, size2, size3;
3573 const uint8_t *buffer, *bufferLim;
3576 size0 = zip->tmp_stream_bytes_remaining;
3577 buf0 = zip->tmp_stream_buff + zip->tmp_stream_bytes_avail - size0;
3578 size1 = zip->sub_stream_bytes_remaining[0];
3579 buf1 = zip->sub_stream_buff[0] + zip->sub_stream_size[0] - size1;
3580 size2 = zip->sub_stream_bytes_remaining[1];
3581 buf2 = zip->sub_stream_buff[1] + zip->sub_stream_size[1] - size2;
3582 size3 = zip->sub_stream_bytes_remaining[2];
3583 buf3 = zip->sub_stream_buff[2] + zip->sub_stream_size[2] - size3;
3586 bufferLim = buffer + size3;
3588 if (zip->bcj_state == 0) {
3592 zip->bcj2_prevByte = 0;
3594 i < sizeof(zip->bcj2_p) / sizeof(zip->bcj2_p[0]); i++)
3595 zip->bcj2_p[i] = kBitModelTotal >> 1;
3601 * Gather the odd bytes of a previous call.
3603 for (i = 0; zip->odd_bcj_size > 0 && outPos < outSize; i++) {
3604 outBuf[outPos++] = zip->odd_bcj[i];
3605 zip->odd_bcj_size--;
3609 zip->bcj2_outPos += outPos;
3619 size_t limit = size0 - inPos;
3620 if (outSize - outPos < limit)
3621 limit = outSize - outPos;
3623 if (zip->bcj_state == 1) {
3624 while (limit != 0) {
3625 uint8_t b = buf0[inPos];
3626 outBuf[outPos++] = b;
3627 if (IsJ(zip->bcj2_prevByte, b)) {
3632 zip->bcj2_prevByte = b;
3637 if (limit == 0 || outPos == outSize)
3644 prob = zip->bcj2_p + zip->bcj2_prevByte;
3646 prob = zip->bcj2_p + 256;
3648 prob = zip->bcj2_p + 257;
3652 zip->bcj2_prevByte = b;
3662 return SZ_ERROR_DATA;
3668 return SZ_ERROR_DATA;
3672 dest = (((uint32_t)v[0] << 24) |
3673 ((uint32_t)v[1] << 16) |
3674 ((uint32_t)v[2] << 8) |
3676 ((uint32_t)zip->bcj2_outPos + outPos + 4);
3677 out[0] = (uint8_t)dest;
3678 out[1] = (uint8_t)(dest >> 8);
3679 out[2] = (uint8_t)(dest >> 16);
3680 out[3] = zip->bcj2_prevByte = (uint8_t)(dest >> 24);
3682 for (i = 0; i < 4 && outPos < outSize; i++)
3683 outBuf[outPos++] = out[i];
3686 * Save odd bytes which we could not add into
3687 * the output buffer because of out of space.
3689 zip->odd_bcj_size = 4 -i;
3690 for (; i < 4; i++) {
3691 j = i - 4 + zip->odd_bcj_size;
3692 zip->odd_bcj[j] = out[i];
3698 zip->tmp_stream_bytes_remaining -= inPos;
3699 zip->sub_stream_bytes_remaining[0] = size1;
3700 zip->sub_stream_bytes_remaining[1] = size2;
3701 zip->sub_stream_bytes_remaining[2] = bufferLim - buffer;
3702 zip->bcj2_outPos += outPos;
3704 return ((ssize_t)outPos);