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