1/*- 2 * Copyright (c) 2004-2013 Tim Kientzle 3 * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA 4 * Copyright (c) 2013 Konrad Kleine 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include "archive_platform.h" 29 30/* 31 * The definitive documentation of the Zip file format is: 32 * http://www.pkware.com/documents/casestudies/APPNOTE.TXT 33 * 34 * The Info-Zip project has pioneered various extensions to better 35 * support Zip on Unix, including the 0x5455 "UT", 0x5855 "UX", 0x7855 36 * "Ux", and 0x7875 "ux" extensions for time and ownership 37 * information. 38 * 39 * History of this code: The streaming Zip reader was first added to 40 * libarchive in January 2005. Support for seekable input sources was 41 * added in Nov 2011. Zip64 support (including a significant code 42 * refactoring) was added in 2014. 43 */ 44 45#ifdef HAVE_ERRNO_H 46#include <errno.h> 47#endif 48#ifdef HAVE_STDLIB_H 49#include <stdlib.h> 50#endif 51#ifdef HAVE_ZLIB_H 52#include <zlib.h> 53#endif 54#ifdef HAVE_BZLIB_H 55#include <bzlib.h> 56#endif 57#ifdef HAVE_LZMA_H 58#include <lzma.h> 59#endif 60#ifdef HAVE_ZSTD_H 61#include <zstd.h> 62#endif 63 64#include "archive.h" 65#include "archive_digest_private.h" 66#include "archive_cryptor_private.h" 67#include "archive_endian.h" 68#include "archive_entry.h" 69#include "archive_entry_locale.h" 70#include "archive_hmac_private.h" 71#include "archive_private.h" 72#include "archive_rb.h" 73#include "archive_read_private.h" 74#include "archive_ppmd8_private.h" 75 76#ifndef HAVE_ZLIB_H 77#include "archive_crc32.h" 78#endif 79 80struct zip_entry { 81 struct archive_rb_node node; 82 struct zip_entry *next; 83 int64_t local_header_offset; 84 int64_t compressed_size; 85 int64_t uncompressed_size; 86 int64_t gid; 87 int64_t uid; 88 struct archive_string rsrcname; 89 time_t mtime; 90 time_t atime; 91 time_t ctime; 92 uint32_t crc32; 93 uint16_t mode; 94 uint16_t zip_flags; /* From GP Flags Field */ 95 unsigned char compression; 96 unsigned char system; /* From "version written by" */ 97 unsigned char flags; /* Our extra markers. */ 98 unsigned char decdat;/* Used for Decryption check */ 99 100 /* WinZip AES encryption extra field should be available 101 * when compression is 99. */ 102 struct { 103 /* Vendor version: AE-1 - 0x0001, AE-2 - 0x0002 */ 104 unsigned vendor; 105#define AES_VENDOR_AE_1 0x0001 106#define AES_VENDOR_AE_2 0x0002 107 /* AES encryption strength: 108 * 1 - 128 bits, 2 - 192 bits, 2 - 256 bits. */ 109 unsigned strength; 110 /* Actual compression method. */ 111 unsigned char compression; 112 } aes_extra; 113}; 114 115struct trad_enc_ctx { 116 uint32_t keys[3]; 117}; 118 119/* Bits used in zip_flags. */ 120#define ZIP_ENCRYPTED (1 << 0) 121#define ZIP_LENGTH_AT_END (1 << 3) /* Also called "Streaming bit" */ 122#define ZIP_STRONG_ENCRYPTED (1 << 6) 123#define ZIP_UTF8_NAME (1 << 11) 124/* See "7.2 Single Password Symmetric Encryption Method" 125 in http://www.pkware.com/documents/casestudies/APPNOTE.TXT */ 126#define ZIP_CENTRAL_DIRECTORY_ENCRYPTED (1 << 13) 127 128/* Bits used in flags. */ 129#define LA_USED_ZIP64 (1 << 0) 130#define LA_FROM_CENTRAL_DIRECTORY (1 << 1) 131 132/* 133 * See "WinZip - AES Encryption Information" 134 * http://www.winzip.com/aes_info.htm 135 */ 136/* Value used in compression method. */ 137#define WINZIP_AES_ENCRYPTION 99 138/* Authentication code size. */ 139#define AUTH_CODE_SIZE 10 140/**/ 141#define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2) 142 143struct zip { 144 /* Structural information about the archive. */ 145 struct archive_string format_name; 146 int64_t central_directory_offset; 147 int64_t central_directory_offset_adjusted; 148 size_t central_directory_entries_total; 149 size_t central_directory_entries_on_this_disk; 150 int has_encrypted_entries; 151 152 /* List of entries (seekable Zip only) */ 153 struct zip_entry *zip_entries; 154 struct archive_rb_tree tree; 155 struct archive_rb_tree tree_rsrc; 156 157 /* Bytes read but not yet consumed via __archive_read_consume() */ 158 size_t unconsumed; 159 160 /* Information about entry we're currently reading. */ 161 struct zip_entry *entry; 162 int64_t entry_bytes_remaining; 163 164 /* These count the number of bytes actually read for the entry. */ 165 int64_t entry_compressed_bytes_read; 166 int64_t entry_uncompressed_bytes_read; 167 168 /* Running CRC32 of the decompressed and decrypted data */ 169 unsigned long computed_crc32; 170 unsigned long (*crc32func)(unsigned long, const void *, 171 size_t); 172 char ignore_crc32; 173 174 /* Flags to mark progress of decompression. */ 175 char decompress_init; 176 char end_of_entry; 177 178 unsigned char *uncompressed_buffer; 179 size_t uncompressed_buffer_size; 180 181#ifdef HAVE_ZLIB_H 182 z_stream stream; 183 char stream_valid; 184#endif 185 186#if HAVE_LZMA_H && HAVE_LIBLZMA 187 lzma_stream zipx_lzma_stream; 188 char zipx_lzma_valid; 189#endif 190 191#ifdef HAVE_BZLIB_H 192 bz_stream bzstream; 193 char bzstream_valid; 194#endif 195 196#if HAVE_ZSTD_H && HAVE_LIBZSTD 197 ZSTD_DStream *zstdstream; 198 char zstdstream_valid; 199#endif 200 201 IByteIn zipx_ppmd_stream; 202 ssize_t zipx_ppmd_read_compressed; 203 CPpmd8 ppmd8; 204 char ppmd8_valid; 205 char ppmd8_stream_failed; 206 207 struct archive_string_conv *sconv; 208 struct archive_string_conv *sconv_default; 209 struct archive_string_conv *sconv_utf8; 210 int init_default_conversion; 211 int process_mac_extensions; 212 213 char init_decryption; 214 215 /* Decryption buffer. */ 216 /* 217 * The decrypted data starts at decrypted_ptr and 218 * extends for decrypted_bytes_remaining. Decryption 219 * adds new data to the end of this block, data is returned 220 * to clients from the beginning. When the block hits the 221 * end of decrypted_buffer, it has to be shuffled back to 222 * the beginning of the buffer. 223 */ 224 unsigned char *decrypted_buffer; 225 unsigned char *decrypted_ptr; 226 size_t decrypted_buffer_size; 227 size_t decrypted_bytes_remaining; 228 size_t decrypted_unconsumed_bytes; 229 230 /* Traditional PKWARE decryption. */ 231 struct trad_enc_ctx tctx; 232 char tctx_valid; 233 234 /* WinZip AES decryption. */ 235 /* Contexts used for AES decryption. */ 236 archive_crypto_ctx cctx; 237 char cctx_valid; 238 archive_hmac_sha1_ctx hctx; 239 char hctx_valid; 240 241 /* Strong encryption's decryption header information. */ 242 unsigned iv_size; 243 unsigned alg_id; 244 unsigned bit_len; 245 unsigned flags; 246 unsigned erd_size; 247 unsigned v_size; 248 unsigned v_crc32; 249 uint8_t *iv; 250 uint8_t *erd; 251 uint8_t *v_data; 252}; 253 254/* Many systems define min or MIN, but not all. */ 255#define zipmin(a,b) ((a) < (b) ? (a) : (b)) 256 257#ifdef HAVE_ZLIB_H 258static int 259zip_read_data_deflate(struct archive_read *a, const void **buff, 260 size_t *size, int64_t *offset); 261#endif 262#if HAVE_LZMA_H && HAVE_LIBLZMA 263static int 264zip_read_data_zipx_lzma_alone(struct archive_read *a, const void **buff, 265 size_t *size, int64_t *offset); 266#endif 267 268/* This function is used by Ppmd8_DecodeSymbol during decompression of Ppmd8 269 * streams inside ZIP files. It has 2 purposes: one is to fetch the next 270 * compressed byte from the stream, second one is to increase the counter how 271 * many compressed bytes were read. */ 272static Byte 273ppmd_read(void* p) { 274 /* Get the handle to current decompression context. */ 275 struct archive_read *a = ((IByteIn*)p)->a; 276 struct zip *zip = (struct zip*) a->format->data; 277 ssize_t bytes_avail = 0; 278 279 /* Fetch next byte. */ 280 const uint8_t* data = __archive_read_ahead(a, 1, &bytes_avail); 281 if(bytes_avail < 1) { 282 zip->ppmd8_stream_failed = 1; 283 return 0; 284 } 285 286 __archive_read_consume(a, 1); 287 288 /* Increment the counter. */ 289 ++zip->zipx_ppmd_read_compressed; 290 291 /* Return the next compressed byte. */ 292 return data[0]; 293} 294 295/* ------------------------------------------------------------------------ */ 296 297/* 298 Traditional PKWARE Decryption functions. 299 */ 300 301static void 302trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c) 303{ 304 uint8_t t; 305#define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL) 306 307 ctx->keys[0] = CRC32(ctx->keys[0], c); 308 ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1; 309 t = (ctx->keys[1] >> 24) & 0xff; 310 ctx->keys[2] = CRC32(ctx->keys[2], t); 311#undef CRC32 312} 313 314static uint8_t 315trad_enc_decrypt_byte(struct trad_enc_ctx *ctx) 316{ 317 unsigned temp = ctx->keys[2] | 2; 318 return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff; 319} 320 321static void 322trad_enc_decrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in, 323 size_t in_len, uint8_t *out, size_t out_len) 324{ 325 unsigned i, max; 326 327 max = (unsigned)((in_len < out_len)? in_len: out_len); 328 329 for (i = 0; i < max; i++) { 330 uint8_t t = in[i] ^ trad_enc_decrypt_byte(ctx); 331 out[i] = t; 332 trad_enc_update_keys(ctx, t); 333 } 334} 335 336static int 337trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len, 338 const uint8_t *key, size_t key_len, uint8_t *crcchk) 339{ 340 uint8_t header[12]; 341 342 if (key_len < 12) { 343 *crcchk = 0xff; 344 return -1; 345 } 346 347 ctx->keys[0] = 305419896L; 348 ctx->keys[1] = 591751049L; 349 ctx->keys[2] = 878082192L; 350 351 for (;pw_len; --pw_len) 352 trad_enc_update_keys(ctx, *pw++); 353 354 trad_enc_decrypt_update(ctx, key, 12, header, 12); 355 /* Return the last byte for CRC check. */ 356 *crcchk = header[11]; 357 return 0; 358} 359 360#if 0 361static void 362crypt_derive_key_sha1(const void *p, int size, unsigned char *key, 363 int key_size) 364{ 365#define MD_SIZE 20 366 archive_sha1_ctx ctx; 367 unsigned char md1[MD_SIZE]; 368 unsigned char md2[MD_SIZE * 2]; 369 unsigned char mkb[64]; 370 int i; 371 372 archive_sha1_init(&ctx); 373 archive_sha1_update(&ctx, p, size); 374 archive_sha1_final(&ctx, md1); 375 376 memset(mkb, 0x36, sizeof(mkb)); 377 for (i = 0; i < MD_SIZE; i++) 378 mkb[i] ^= md1[i]; 379 archive_sha1_init(&ctx); 380 archive_sha1_update(&ctx, mkb, sizeof(mkb)); 381 archive_sha1_final(&ctx, md2); 382 383 memset(mkb, 0x5C, sizeof(mkb)); 384 for (i = 0; i < MD_SIZE; i++) 385 mkb[i] ^= md1[i]; 386 archive_sha1_init(&ctx); 387 archive_sha1_update(&ctx, mkb, sizeof(mkb)); 388 archive_sha1_final(&ctx, md2 + MD_SIZE); 389 390 if (key_size > 32) 391 key_size = 32; 392 memcpy(key, md2, key_size); 393#undef MD_SIZE 394} 395#endif 396 397/* 398 * Common code for streaming or seeking modes. 399 * 400 * Includes code to read local file headers, decompress data 401 * from entry bodies, and common API. 402 */ 403 404static unsigned long 405real_crc32(unsigned long crc, const void *buff, size_t len) 406{ 407 return crc32(crc, buff, (unsigned int)len); 408} 409 410/* Used by "ignorecrc32" option to speed up tests. */ 411static unsigned long 412fake_crc32(unsigned long crc, const void *buff, size_t len) 413{ 414 (void)crc; /* UNUSED */ 415 (void)buff; /* UNUSED */ 416 (void)len; /* UNUSED */ 417 return 0; 418} 419 420static const struct { 421 int id; 422 const char * name; 423} compression_methods[] = { 424 {0, "uncompressed"}, /* The file is stored (no compression) */ 425 {1, "shrinking"}, /* The file is Shrunk */ 426 {2, "reduced-1"}, /* The file is Reduced with compression factor 1 */ 427 {3, "reduced-2"}, /* The file is Reduced with compression factor 2 */ 428 {4, "reduced-3"}, /* The file is Reduced with compression factor 3 */ 429 {5, "reduced-4"}, /* The file is Reduced with compression factor 4 */ 430 {6, "imploded"}, /* The file is Imploded */ 431 {7, "reserved"}, /* Reserved for Tokenizing compression algorithm */ 432 {8, "deflation"}, /* The file is Deflated */ 433 {9, "deflation-64-bit"}, /* Enhanced Deflating using Deflate64(tm) */ 434 {10, "ibm-terse"},/* PKWARE Data Compression Library Imploding 435 * (old IBM TERSE) */ 436 {11, "reserved"}, /* Reserved by PKWARE */ 437 {12, "bzip"}, /* File is compressed using BZIP2 algorithm */ 438 {13, "reserved"}, /* Reserved by PKWARE */ 439 {14, "lzma"}, /* LZMA (EFS) */ 440 {15, "reserved"}, /* Reserved by PKWARE */ 441 {16, "reserved"}, /* Reserved by PKWARE */ 442 {17, "reserved"}, /* Reserved by PKWARE */ 443 {18, "ibm-terse-new"}, /* File is compressed using IBM TERSE (new) */ 444 {19, "ibm-lz777"},/* IBM LZ77 z Architecture (PFS) */ 445 {93, "zstd"}, /* Zstandard (zstd) Compression */ 446 {95, "xz"}, /* XZ compressed data */ 447 {96, "jpeg"}, /* JPEG compressed data */ 448 {97, "wav-pack"}, /* WavPack compressed data */ 449 {98, "ppmd-1"}, /* PPMd version I, Rev 1 */ 450 {99, "aes"} /* WinZip AES encryption */ 451}; 452 453static const char * 454compression_name(const int compression) 455{ 456 static const int num_compression_methods = 457 sizeof(compression_methods)/sizeof(compression_methods[0]); 458 int i=0; 459 460 while(compression >= 0 && i < num_compression_methods) { 461 if (compression_methods[i].id == compression) 462 return compression_methods[i].name; 463 i++; 464 } 465 return "??"; 466} 467 468/* Convert an MSDOS-style date/time into Unix-style time. */ 469static time_t 470zip_time(const char *p) 471{ 472 int msTime, msDate; 473 struct tm ts; 474 475 msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]); 476 msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]); 477 478 memset(&ts, 0, sizeof(ts)); 479 ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */ 480 ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */ 481 ts.tm_mday = msDate & 0x1f; /* Day of month. */ 482 ts.tm_hour = (msTime >> 11) & 0x1f; 483 ts.tm_min = (msTime >> 5) & 0x3f; 484 ts.tm_sec = (msTime << 1) & 0x3e; 485 ts.tm_isdst = -1; 486 return mktime(&ts); 487} 488 489/* 490 * The extra data is stored as a list of 491 * id1+size1+data1 + id2+size2+data2 ... 492 * triplets. id and size are 2 bytes each. 493 */ 494static int 495process_extra(struct archive_read *a, struct archive_entry *entry, 496 const char *p, size_t extra_length, struct zip_entry* zip_entry) 497{ 498 unsigned offset = 0; 499 struct zip *zip = (struct zip *)(a->format->data); 500 501 if (extra_length == 0) { 502 return ARCHIVE_OK; 503 } 504 505 if (extra_length < 4) { 506 size_t i = 0; 507 /* Some ZIP files may have trailing 0 bytes. Let's check they 508 * are all 0 and ignore them instead of returning an error. 509 * 510 * This is not technically correct, but some ZIP files look 511 * like this and other tools support those files - so let's 512 * also support them. 513 */ 514 for (; i < extra_length; i++) { 515 if (p[i] != 0) { 516 archive_set_error(&a->archive, 517 ARCHIVE_ERRNO_FILE_FORMAT, 518 "Too-small extra data: " 519 "Need at least 4 bytes, " 520 "but only found %d bytes", 521 (int)extra_length); 522 return ARCHIVE_FAILED; 523 } 524 } 525 526 return ARCHIVE_OK; 527 } 528 529 while (offset <= extra_length - 4) { 530 unsigned short headerid = archive_le16dec(p + offset); 531 unsigned short datasize = archive_le16dec(p + offset + 2); 532 533 offset += 4; 534 if (offset + datasize > extra_length) { 535 archive_set_error(&a->archive, 536 ARCHIVE_ERRNO_FILE_FORMAT, "Extra data overflow: " 537 "Need %d bytes but only found %d bytes", 538 (int)datasize, (int)(extra_length - offset)); 539 return ARCHIVE_FAILED; 540 } 541#ifdef DEBUG 542 fprintf(stderr, "Header id 0x%04x, length %d\n", 543 headerid, datasize); 544#endif 545 switch (headerid) { 546 case 0x0001: 547 /* Zip64 extended information extra field. */ 548 zip_entry->flags |= LA_USED_ZIP64; 549 if (zip_entry->uncompressed_size == 0xffffffff) { 550 uint64_t t = 0; 551 if (datasize < 8 552 || (t = archive_le64dec(p + offset)) > 553 INT64_MAX) { 554 archive_set_error(&a->archive, 555 ARCHIVE_ERRNO_FILE_FORMAT, 556 "Malformed 64-bit " 557 "uncompressed size"); 558 return ARCHIVE_FAILED; 559 } 560 zip_entry->uncompressed_size = t; 561 offset += 8; 562 datasize -= 8; 563 } 564 if (zip_entry->compressed_size == 0xffffffff) { 565 uint64_t t = 0; 566 if (datasize < 8 567 || (t = archive_le64dec(p + offset)) > 568 INT64_MAX) { 569 archive_set_error(&a->archive, 570 ARCHIVE_ERRNO_FILE_FORMAT, 571 "Malformed 64-bit " 572 "compressed size"); 573 return ARCHIVE_FAILED; 574 } 575 zip_entry->compressed_size = t; 576 offset += 8; 577 datasize -= 8; 578 } 579 if (zip_entry->local_header_offset == 0xffffffff) { 580 uint64_t t = 0; 581 if (datasize < 8 582 || (t = archive_le64dec(p + offset)) > 583 INT64_MAX) { 584 archive_set_error(&a->archive, 585 ARCHIVE_ERRNO_FILE_FORMAT, 586 "Malformed 64-bit " 587 "local header offset"); 588 return ARCHIVE_FAILED; 589 } 590 zip_entry->local_header_offset = t; 591 offset += 8; 592 datasize -= 8; 593 } 594 /* archive_le32dec(p + offset) gives disk 595 * on which file starts, but we don't handle 596 * multi-volume Zip files. */ 597 break; 598#ifdef DEBUG 599 case 0x0017: 600 { 601 /* Strong encryption field. */ 602 if (archive_le16dec(p + offset) == 2) { 603 unsigned algId = 604 archive_le16dec(p + offset + 2); 605 unsigned bitLen = 606 archive_le16dec(p + offset + 4); 607 int flags = 608 archive_le16dec(p + offset + 6); 609 fprintf(stderr, "algId=0x%04x, bitLen=%u, " 610 "flgas=%d\n", algId, bitLen,flags); 611 } 612 break; 613 } 614#endif 615 case 0x5455: 616 { 617 /* Extended time field "UT". */ 618 int flags; 619 if (datasize == 0) { 620 archive_set_error(&a->archive, 621 ARCHIVE_ERRNO_FILE_FORMAT, 622 "Incomplete extended time field"); 623 return ARCHIVE_FAILED; 624 } 625 flags = p[offset]; 626 offset++; 627 datasize--; 628 /* Flag bits indicate which dates are present. */ 629 if (flags & 0x01) 630 { 631#ifdef DEBUG 632 fprintf(stderr, "mtime: %lld -> %d\n", 633 (long long)zip_entry->mtime, 634 archive_le32dec(p + offset)); 635#endif 636 if (datasize < 4) 637 break; 638 zip_entry->mtime = archive_le32dec(p + offset); 639 offset += 4; 640 datasize -= 4; 641 } 642 if (flags & 0x02) 643 { 644 if (datasize < 4) 645 break; 646 zip_entry->atime = archive_le32dec(p + offset); 647 offset += 4; 648 datasize -= 4; 649 } 650 if (flags & 0x04) 651 { 652 if (datasize < 4) 653 break; 654 zip_entry->ctime = archive_le32dec(p + offset); 655 offset += 4; 656 datasize -= 4; 657 } 658 break; 659 } 660 case 0x5855: 661 { 662 /* Info-ZIP Unix Extra Field (old version) "UX". */ 663 if (datasize >= 8) { 664 zip_entry->atime = archive_le32dec(p + offset); 665 zip_entry->mtime = 666 archive_le32dec(p + offset + 4); 667 } 668 if (datasize >= 12) { 669 zip_entry->uid = 670 archive_le16dec(p + offset + 8); 671 zip_entry->gid = 672 archive_le16dec(p + offset + 10); 673 } 674 break; 675 } 676 case 0x6c78: 677 { 678 /* Experimental 'xl' field */ 679 /* 680 * Introduced Dec 2013 to provide a way to 681 * include external file attributes (and other 682 * fields that ordinarily appear only in 683 * central directory) in local file header. 684 * This provides file type and permission 685 * information necessary to support full 686 * streaming extraction. Currently being 687 * discussed with other Zip developers 688 * ... subject to change. 689 * 690 * Format: 691 * The field starts with a bitmap that specifies 692 * which additional fields are included. The 693 * bitmap is variable length and can be extended in 694 * the future. 695 * 696 * n bytes - feature bitmap: first byte has low-order 697 * 7 bits. If high-order bit is set, a subsequent 698 * byte holds the next 7 bits, etc. 699 * 700 * if bitmap & 1, 2 byte "version made by" 701 * if bitmap & 2, 2 byte "internal file attributes" 702 * if bitmap & 4, 4 byte "external file attributes" 703 * if bitmap & 8, 2 byte comment length + n byte 704 * comment 705 */ 706 int bitmap, bitmap_last; 707 708 if (datasize < 1) 709 break; 710 bitmap_last = bitmap = 0xff & p[offset]; 711 offset += 1; 712 datasize -= 1; 713 714 /* We only support first 7 bits of bitmap; skip rest. */ 715 while ((bitmap_last & 0x80) != 0 716 && datasize >= 1) { 717 bitmap_last = p[offset]; 718 offset += 1; 719 datasize -= 1; 720 } 721 722 if (bitmap & 1) { 723 /* 2 byte "version made by" */ 724 if (datasize < 2) 725 break; 726 zip_entry->system 727 = archive_le16dec(p + offset) >> 8; 728 offset += 2; 729 datasize -= 2; 730 } 731 if (bitmap & 2) { 732 /* 2 byte "internal file attributes" */ 733 uint32_t internal_attributes; 734 if (datasize < 2) 735 break; 736 internal_attributes 737 = archive_le16dec(p + offset); 738 /* Not used by libarchive at present. */ 739 (void)internal_attributes; /* UNUSED */ 740 offset += 2; 741 datasize -= 2; 742 } 743 if (bitmap & 4) { 744 /* 4 byte "external file attributes" */ 745 uint32_t external_attributes; 746 if (datasize < 4) 747 break; 748 external_attributes 749 = archive_le32dec(p + offset); 750 if (zip_entry->system == 3) { 751 zip_entry->mode 752 = external_attributes >> 16; 753 } else if (zip_entry->system == 0) { 754 // Interpret MSDOS directory bit 755 if (0x10 == (external_attributes & 756 0x10)) { 757 zip_entry->mode = 758 AE_IFDIR | 0775; 759 } else { 760 zip_entry->mode = 761 AE_IFREG | 0664; 762 } 763 if (0x01 == (external_attributes & 764 0x01)) { 765 /* Read-only bit; 766 * strip write permissions */ 767 zip_entry->mode &= 0555; 768 } 769 } else { 770 zip_entry->mode = 0; 771 } 772 offset += 4; 773 datasize -= 4; 774 } 775 if (bitmap & 8) { 776 /* 2 byte comment length + comment */ 777 uint32_t comment_length; 778 if (datasize < 2) 779 break; 780 comment_length 781 = archive_le16dec(p + offset); 782 offset += 2; 783 datasize -= 2; 784 785 if (datasize < comment_length) 786 break; 787 /* Comment is not supported by libarchive */ 788 offset += comment_length; 789 datasize -= comment_length; 790 } 791 break; 792 } 793 case 0x7075: 794 { 795 /* Info-ZIP Unicode Path Extra Field. */ 796 if (datasize < 5 || entry == NULL) 797 break; 798 offset += 5; 799 datasize -= 5; 800 801 /* The path name in this field is always encoded 802 * in UTF-8. */ 803 if (zip->sconv_utf8 == NULL) { 804 zip->sconv_utf8 = 805 archive_string_conversion_from_charset( 806 &a->archive, "UTF-8", 1); 807 /* If the converter from UTF-8 is not 808 * available, then the path name from the main 809 * field will more likely be correct. */ 810 if (zip->sconv_utf8 == NULL) 811 break; 812 } 813 814 /* Make sure the CRC32 of the filename matches. */ 815 if (!zip->ignore_crc32) { 816 const char *cp = archive_entry_pathname(entry); 817 if (cp) { 818 unsigned long file_crc = 819 zip->crc32func(0, cp, strlen(cp)); 820 unsigned long utf_crc = 821 archive_le32dec(p + offset - 4); 822 if (file_crc != utf_crc) { 823#ifdef DEBUG 824 fprintf(stderr, 825 "CRC filename mismatch; " 826 "CDE is %lx, but UTF8 " 827 "is outdated with %lx\n", 828 file_crc, utf_crc); 829#endif 830 break; 831 } 832 } 833 } 834 835 if (archive_entry_copy_pathname_l(entry, 836 p + offset, datasize, zip->sconv_utf8) != 0) { 837 /* Ignore the error, and fallback to the path 838 * name from the main field. */ 839#ifdef DEBUG 840 fprintf(stderr, "Failed to read the ZIP " 841 "0x7075 extra field path.\n"); 842#endif 843 } 844 break; 845 } 846 case 0x7855: 847 /* Info-ZIP Unix Extra Field (type 2) "Ux". */ 848#ifdef DEBUG 849 fprintf(stderr, "uid %d gid %d\n", 850 archive_le16dec(p + offset), 851 archive_le16dec(p + offset + 2)); 852#endif 853 if (datasize >= 2) 854 zip_entry->uid = archive_le16dec(p + offset); 855 if (datasize >= 4) 856 zip_entry->gid = 857 archive_le16dec(p + offset + 2); 858 break; 859 case 0x7875: 860 { 861 /* Info-Zip Unix Extra Field (type 3) "ux". */ 862 int uidsize = 0, gidsize = 0; 863 864 /* TODO: support arbitrary uidsize/gidsize. */ 865 if (datasize >= 1 && p[offset] == 1) {/* version=1 */ 866 if (datasize >= 4) { 867 /* get a uid size. */ 868 uidsize = 0xff & (int)p[offset+1]; 869 if (uidsize == 2) 870 zip_entry->uid = 871 archive_le16dec( 872 p + offset + 2); 873 else if (uidsize == 4 && datasize >= 6) 874 zip_entry->uid = 875 archive_le32dec( 876 p + offset + 2); 877 } 878 if (datasize >= (2 + uidsize + 3)) { 879 /* get a gid size. */ 880 gidsize = 0xff & 881 (int)p[offset+2+uidsize]; 882 if (gidsize == 2) 883 zip_entry->gid = 884 archive_le16dec( 885 p+offset+2+uidsize+1); 886 else if (gidsize == 4 && 887 datasize >= (2 + uidsize + 5)) 888 zip_entry->gid = 889 archive_le32dec( 890 p+offset+2+uidsize+1); 891 } 892 } 893 break; 894 } 895 case 0x9901: 896 /* WinZip AES extra data field. */ 897 if (datasize < 6) { 898 archive_set_error(&a->archive, 899 ARCHIVE_ERRNO_FILE_FORMAT, 900 "Incomplete AES field"); 901 return ARCHIVE_FAILED; 902 } 903 if (p[offset + 2] == 'A' && p[offset + 3] == 'E') { 904 /* Vendor version. */ 905 zip_entry->aes_extra.vendor = 906 archive_le16dec(p + offset); 907 /* AES encryption strength. */ 908 zip_entry->aes_extra.strength = p[offset + 4]; 909 /* Actual compression method. */ 910 zip_entry->aes_extra.compression = 911 p[offset + 5]; 912 } 913 break; 914 default: 915 break; 916 } 917 offset += datasize; 918 } 919 return ARCHIVE_OK; 920} 921 922/* 923 * Assumes file pointer is at beginning of local file header. 924 */ 925static int 926zip_read_local_file_header(struct archive_read *a, struct archive_entry *entry, 927 struct zip *zip) 928{ 929 const char *p; 930 const void *h; 931 const wchar_t *wp; 932 const char *cp; 933 size_t len, filename_length, extra_length; 934 struct archive_string_conv *sconv; 935 struct zip_entry *zip_entry = zip->entry; 936 struct zip_entry zip_entry_central_dir; 937 int ret = ARCHIVE_OK; 938 char version; 939 940 /* Save a copy of the original for consistency checks. */ 941 zip_entry_central_dir = *zip_entry; 942 943 zip->decompress_init = 0; 944 zip->end_of_entry = 0; 945 zip->entry_uncompressed_bytes_read = 0; 946 zip->entry_compressed_bytes_read = 0; 947 zip->computed_crc32 = zip->crc32func(0, NULL, 0); 948 949 /* Setup default conversion. */ 950 if (zip->sconv == NULL && !zip->init_default_conversion) { 951 zip->sconv_default = 952 archive_string_default_conversion_for_read(&(a->archive)); 953 zip->init_default_conversion = 1; 954 } 955 956 if ((p = __archive_read_ahead(a, 30, NULL)) == NULL) { 957 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 958 "Truncated ZIP file header"); 959 return (ARCHIVE_FATAL); 960 } 961 962 if (memcmp(p, "PK\003\004", 4) != 0) { 963 archive_set_error(&a->archive, -1, "Damaged Zip archive"); 964 return ARCHIVE_FATAL; 965 } 966 version = p[4]; 967 zip_entry->system = p[5]; 968 zip_entry->zip_flags = archive_le16dec(p + 6); 969 if (zip_entry->zip_flags & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)) { 970 zip->has_encrypted_entries = 1; 971 archive_entry_set_is_data_encrypted(entry, 1); 972 if (zip_entry->zip_flags & ZIP_CENTRAL_DIRECTORY_ENCRYPTED && 973 zip_entry->zip_flags & ZIP_ENCRYPTED && 974 zip_entry->zip_flags & ZIP_STRONG_ENCRYPTED) { 975 archive_entry_set_is_metadata_encrypted(entry, 1); 976 return ARCHIVE_FATAL; 977 } 978 } 979 zip->init_decryption = (zip_entry->zip_flags & ZIP_ENCRYPTED); 980 zip_entry->compression = (char)archive_le16dec(p + 8); 981 zip_entry->mtime = zip_time(p + 10); 982 zip_entry->crc32 = archive_le32dec(p + 14); 983 if (zip_entry->zip_flags & ZIP_LENGTH_AT_END) 984 zip_entry->decdat = p[11]; 985 else 986 zip_entry->decdat = p[17]; 987 zip_entry->compressed_size = archive_le32dec(p + 18); 988 zip_entry->uncompressed_size = archive_le32dec(p + 22); 989 filename_length = archive_le16dec(p + 26); 990 extra_length = archive_le16dec(p + 28); 991 992 __archive_read_consume(a, 30); 993 994 /* Read the filename. */ 995 if ((h = __archive_read_ahead(a, filename_length, NULL)) == NULL) { 996 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 997 "Truncated ZIP file header"); 998 return (ARCHIVE_FATAL); 999 } 1000 if (zip_entry->zip_flags & ZIP_UTF8_NAME) { 1001 /* The filename is stored to be UTF-8. */ 1002 if (zip->sconv_utf8 == NULL) { 1003 zip->sconv_utf8 = 1004 archive_string_conversion_from_charset( 1005 &a->archive, "UTF-8", 1); 1006 if (zip->sconv_utf8 == NULL) 1007 return (ARCHIVE_FATAL); 1008 } 1009 sconv = zip->sconv_utf8; 1010 } else if (zip->sconv != NULL) 1011 sconv = zip->sconv; 1012 else 1013 sconv = zip->sconv_default; 1014 1015 if (archive_entry_copy_pathname_l(entry, 1016 h, filename_length, sconv) != 0) { 1017 if (errno == ENOMEM) { 1018 archive_set_error(&a->archive, ENOMEM, 1019 "Can't allocate memory for Pathname"); 1020 return (ARCHIVE_FATAL); 1021 } 1022 archive_set_error(&a->archive, 1023 ARCHIVE_ERRNO_FILE_FORMAT, 1024 "Pathname cannot be converted " 1025 "from %s to current locale.", 1026 archive_string_conversion_charset_name(sconv)); 1027 ret = ARCHIVE_WARN; 1028 } 1029 __archive_read_consume(a, filename_length); 1030 1031 /* Read the extra data. */ 1032 if ((h = __archive_read_ahead(a, extra_length, NULL)) == NULL) { 1033 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 1034 "Truncated ZIP file header"); 1035 return (ARCHIVE_FATAL); 1036 } 1037 1038 if (ARCHIVE_OK != process_extra(a, entry, h, extra_length, 1039 zip_entry)) { 1040 return ARCHIVE_FATAL; 1041 } 1042 __archive_read_consume(a, extra_length); 1043 1044 /* Work around a bug in Info-Zip: When reading from a pipe, it 1045 * stats the pipe instead of synthesizing a file entry. */ 1046 if ((zip_entry->mode & AE_IFMT) == AE_IFIFO) { 1047 zip_entry->mode &= ~ AE_IFMT; 1048 zip_entry->mode |= AE_IFREG; 1049 } 1050 1051 /* If the mode is totally empty, set some sane default. */ 1052 if (zip_entry->mode == 0) { 1053 zip_entry->mode |= 0664; 1054 } 1055 1056 /* Windows archivers sometimes use backslash as the directory 1057 * separator. Normalize to slash. */ 1058 if (zip_entry->system == 0 && 1059 (wp = archive_entry_pathname_w(entry)) != NULL) { 1060 if (wcschr(wp, L'/') == NULL && wcschr(wp, L'\\') != NULL) { 1061 size_t i; 1062 struct archive_wstring s; 1063 archive_string_init(&s); 1064 archive_wstrcpy(&s, wp); 1065 for (i = 0; i < archive_strlen(&s); i++) { 1066 if (s.s[i] == '\\') 1067 s.s[i] = '/'; 1068 } 1069 archive_entry_copy_pathname_w(entry, s.s); 1070 archive_wstring_free(&s); 1071 } 1072 } 1073 1074 /* Make sure that entries with a trailing '/' are marked as directories 1075 * even if the External File Attributes contains bogus values. If this 1076 * is not a directory and there is no type, assume a regular file. */ 1077 if ((zip_entry->mode & AE_IFMT) != AE_IFDIR) { 1078 int has_slash; 1079 1080 wp = archive_entry_pathname_w(entry); 1081 if (wp != NULL) { 1082 len = wcslen(wp); 1083 has_slash = len > 0 && wp[len - 1] == L'/'; 1084 } else { 1085 cp = archive_entry_pathname(entry); 1086 len = (cp != NULL)?strlen(cp):0; 1087 has_slash = len > 0 && cp[len - 1] == '/'; 1088 } 1089 /* Correct file type as needed. */ 1090 if (has_slash) { 1091 zip_entry->mode &= ~AE_IFMT; 1092 zip_entry->mode |= AE_IFDIR; 1093 zip_entry->mode |= 0111; 1094 } else if ((zip_entry->mode & AE_IFMT) == 0) { 1095 zip_entry->mode |= AE_IFREG; 1096 } 1097 } 1098 1099 /* Make sure directories end in '/' */ 1100 if ((zip_entry->mode & AE_IFMT) == AE_IFDIR) { 1101 wp = archive_entry_pathname_w(entry); 1102 if (wp != NULL) { 1103 len = wcslen(wp); 1104 if (len > 0 && wp[len - 1] != L'/') { 1105 struct archive_wstring s; 1106 archive_string_init(&s); 1107 archive_wstrcat(&s, wp); 1108 archive_wstrappend_wchar(&s, L'/'); 1109 archive_entry_copy_pathname_w(entry, s.s); 1110 archive_wstring_free(&s); 1111 } 1112 } else { 1113 cp = archive_entry_pathname(entry); 1114 len = (cp != NULL)?strlen(cp):0; 1115 if (len > 0 && cp[len - 1] != '/') { 1116 struct archive_string s; 1117 archive_string_init(&s); 1118 archive_strcat(&s, cp); 1119 archive_strappend_char(&s, '/'); 1120 archive_entry_set_pathname(entry, s.s); 1121 archive_string_free(&s); 1122 } 1123 } 1124 } 1125 1126 if (zip_entry->flags & LA_FROM_CENTRAL_DIRECTORY) { 1127 /* If this came from the central dir, its size info 1128 * is definitive, so ignore the length-at-end flag. */ 1129 zip_entry->zip_flags &= ~ZIP_LENGTH_AT_END; 1130 /* If local header is missing a value, use the one from 1131 the central directory. If both have it, warn about 1132 mismatches. */ 1133 if (zip_entry->crc32 == 0) { 1134 zip_entry->crc32 = zip_entry_central_dir.crc32; 1135 } else if (!zip->ignore_crc32 1136 && zip_entry->crc32 != zip_entry_central_dir.crc32) { 1137 archive_set_error(&a->archive, 1138 ARCHIVE_ERRNO_FILE_FORMAT, 1139 "Inconsistent CRC32 values"); 1140 ret = ARCHIVE_WARN; 1141 } 1142 if (zip_entry->compressed_size == 0 1143 || zip_entry->compressed_size == 0xffffffff) { 1144 zip_entry->compressed_size 1145 = zip_entry_central_dir.compressed_size; 1146 } else if (zip_entry->compressed_size 1147 != zip_entry_central_dir.compressed_size) { 1148 archive_set_error(&a->archive, 1149 ARCHIVE_ERRNO_FILE_FORMAT, 1150 "Inconsistent compressed size: " 1151 "%jd in central directory, %jd in local header", 1152 (intmax_t)zip_entry_central_dir.compressed_size, 1153 (intmax_t)zip_entry->compressed_size); 1154 ret = ARCHIVE_WARN; 1155 } 1156 if (zip_entry->uncompressed_size == 0 || 1157 zip_entry->uncompressed_size == 0xffffffff) { 1158 zip_entry->uncompressed_size 1159 = zip_entry_central_dir.uncompressed_size; 1160 } else if (zip_entry->uncompressed_size 1161 != zip_entry_central_dir.uncompressed_size) { 1162 archive_set_error(&a->archive, 1163 ARCHIVE_ERRNO_FILE_FORMAT, 1164 "Inconsistent uncompressed size: " 1165 "%jd in central directory, %jd in local header", 1166 (intmax_t)zip_entry_central_dir.uncompressed_size, 1167 (intmax_t)zip_entry->uncompressed_size); 1168 ret = ARCHIVE_WARN; 1169 } 1170 } 1171 1172 /* Populate some additional entry fields: */ 1173 archive_entry_set_mode(entry, zip_entry->mode); 1174 archive_entry_set_uid(entry, zip_entry->uid); 1175 archive_entry_set_gid(entry, zip_entry->gid); 1176 archive_entry_set_mtime(entry, zip_entry->mtime, 0); 1177 archive_entry_set_ctime(entry, zip_entry->ctime, 0); 1178 archive_entry_set_atime(entry, zip_entry->atime, 0); 1179 1180 if ((zip->entry->mode & AE_IFMT) == AE_IFLNK) { 1181 size_t linkname_length; 1182 1183 if (zip_entry->compressed_size > 64 * 1024) { 1184 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1185 "Zip file with oversized link entry"); 1186 return ARCHIVE_FATAL; 1187 } 1188 1189 linkname_length = (size_t)zip_entry->compressed_size; 1190 1191 archive_entry_set_size(entry, 0); 1192 1193 // take into account link compression if any 1194 size_t linkname_full_length = linkname_length; 1195 if (zip->entry->compression != 0) 1196 { 1197 // symlink target string appeared to be compressed 1198 int status = ARCHIVE_FATAL; 1199 const void *uncompressed_buffer = NULL; 1200 1201 switch (zip->entry->compression) 1202 { 1203#if HAVE_ZLIB_H 1204 case 8: /* Deflate compression. */ 1205 zip->entry_bytes_remaining = zip_entry->compressed_size; 1206 status = zip_read_data_deflate(a, &uncompressed_buffer, 1207 &linkname_full_length, NULL); 1208 break; 1209#endif 1210#if HAVE_LZMA_H && HAVE_LIBLZMA 1211 case 14: /* ZIPx LZMA compression. */ 1212 /*(see zip file format specification, section 4.4.5)*/ 1213 zip->entry_bytes_remaining = zip_entry->compressed_size; 1214 status = zip_read_data_zipx_lzma_alone(a, &uncompressed_buffer, 1215 &linkname_full_length, NULL); 1216 break; 1217#endif 1218 default: /* Unsupported compression. */ 1219 break; 1220 } 1221 if (status == ARCHIVE_OK) 1222 { 1223 p = uncompressed_buffer; 1224 } 1225 else 1226 { 1227 archive_set_error(&a->archive, 1228 ARCHIVE_ERRNO_FILE_FORMAT, 1229 "Unsupported ZIP compression method " 1230 "during decompression of link entry (%d: %s)", 1231 zip->entry->compression, 1232 compression_name(zip->entry->compression)); 1233 return ARCHIVE_FAILED; 1234 } 1235 } 1236 else 1237 { 1238 p = __archive_read_ahead(a, linkname_length, NULL); 1239 } 1240 1241 if (p == NULL) { 1242 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1243 "Truncated Zip file"); 1244 return ARCHIVE_FATAL; 1245 } 1246 1247 sconv = zip->sconv; 1248 if (sconv == NULL && (zip->entry->zip_flags & ZIP_UTF8_NAME)) 1249 sconv = zip->sconv_utf8; 1250 if (sconv == NULL) 1251 sconv = zip->sconv_default; 1252 if (archive_entry_copy_symlink_l(entry, p, linkname_full_length, 1253 sconv) != 0) { 1254 if (errno != ENOMEM && sconv == zip->sconv_utf8 && 1255 (zip->entry->zip_flags & ZIP_UTF8_NAME)) 1256 archive_entry_copy_symlink_l(entry, p, 1257 linkname_full_length, NULL); 1258 if (errno == ENOMEM) { 1259 archive_set_error(&a->archive, ENOMEM, 1260 "Can't allocate memory for Symlink"); 1261 return (ARCHIVE_FATAL); 1262 } 1263 /* 1264 * Since there is no character-set regulation for 1265 * symlink name, do not report the conversion error 1266 * in an automatic conversion. 1267 */ 1268 if (sconv != zip->sconv_utf8 || 1269 (zip->entry->zip_flags & ZIP_UTF8_NAME) == 0) { 1270 archive_set_error(&a->archive, 1271 ARCHIVE_ERRNO_FILE_FORMAT, 1272 "Symlink cannot be converted " 1273 "from %s to current locale.", 1274 archive_string_conversion_charset_name( 1275 sconv)); 1276 ret = ARCHIVE_WARN; 1277 } 1278 } 1279 zip_entry->uncompressed_size = zip_entry->compressed_size = 0; 1280 1281 if (__archive_read_consume(a, linkname_length) < 0) { 1282 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1283 "Read error skipping symlink target name"); 1284 return ARCHIVE_FATAL; 1285 } 1286 } else if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END) 1287 || (zip_entry->uncompressed_size > 0 1288 && zip_entry->uncompressed_size != 0xffffffff)) { 1289 /* Set the size only if it's meaningful. */ 1290 archive_entry_set_size(entry, zip_entry->uncompressed_size); 1291 } 1292 zip->entry_bytes_remaining = zip_entry->compressed_size; 1293 1294 /* If there's no body, force read_data() to return EOF immediately. */ 1295 if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END) 1296 && zip->entry_bytes_remaining < 1) 1297 zip->end_of_entry = 1; 1298 1299 /* Set up a more descriptive format name. */ 1300 archive_string_empty(&zip->format_name); 1301 archive_string_sprintf(&zip->format_name, "ZIP %d.%d (%s)", 1302 version / 10, version % 10, 1303 compression_name(zip->entry->compression)); 1304 a->archive.archive_format_name = zip->format_name.s; 1305 1306 return (ret); 1307} 1308 1309static int 1310check_authentication_code(struct archive_read *a, const void *_p) 1311{ 1312 struct zip *zip = (struct zip *)(a->format->data); 1313 1314 /* Check authentication code. */ 1315 if (zip->hctx_valid) { 1316 const void *p; 1317 uint8_t hmac[20]; 1318 size_t hmac_len = 20; 1319 int cmp; 1320 1321 archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len); 1322 if (_p == NULL) { 1323 /* Read authentication code. */ 1324 p = __archive_read_ahead(a, AUTH_CODE_SIZE, NULL); 1325 if (p == NULL) { 1326 archive_set_error(&a->archive, 1327 ARCHIVE_ERRNO_FILE_FORMAT, 1328 "Truncated ZIP file data"); 1329 return (ARCHIVE_FATAL); 1330 } 1331 } else { 1332 p = _p; 1333 } 1334 cmp = memcmp(hmac, p, AUTH_CODE_SIZE); 1335 __archive_read_consume(a, AUTH_CODE_SIZE); 1336 if (cmp != 0) { 1337 archive_set_error(&a->archive, 1338 ARCHIVE_ERRNO_MISC, 1339 "ZIP bad Authentication code"); 1340 return (ARCHIVE_WARN); 1341 } 1342 } 1343 return (ARCHIVE_OK); 1344} 1345 1346/* 1347 * The Zip end-of-file marker is inherently ambiguous. The specification 1348 * in APPNOTE.TXT allows any of four possible formats, and there is no 1349 * guaranteed-correct way for a reader to know a priori which one the writer 1350 * will have used. The four formats are: 1351 * 1. 32-bit format with an initial PK78 marker 1352 * 2. 32-bit format without that marker 1353 * 3. 64-bit format with the marker 1354 * 4. 64-bit format without the marker 1355 * 1356 * Mark Adler's `sunzip` streaming unzip program solved this ambiguity 1357 * by just looking at every possible combination and accepting the 1358 * longest one that matches the expected values. His approach always 1359 * consumes the longest possible matching EOF marker, based on an 1360 * analysis of all the possible failures and how the values could 1361 * overlap. 1362 * 1363 * For example, suppose both of the first two formats listed 1364 * above match. In that case, we know the next four 1365 * 32-bit words match this pattern: 1366 * ``` 1367 * [PK\07\08] [CRC32] [compressed size] [uncompressed size] 1368 * ``` 1369 * but we know they must also match this pattern: 1370 * ``` 1371 * [CRC32] [compressed size] [uncompressed size] [other PK marker] 1372 * ``` 1373 * 1374 * Since the first word here matches both the PK78 signature in the 1375 * first form and the CRC32 in the second, we know those two values 1376 * are equal, the CRC32 must be exactly 0x08074b50. Similarly, the 1377 * compressed and uncompressed size must also be exactly this value. 1378 * So we know these four words are all 0x08074b50. If we were to 1379 * accept the shorter pattern, it would be immediately followed by 1380 * another PK78 marker, which is not possible in a well-formed ZIP 1381 * archive unless there is garbage between entries. This implies we 1382 * should not accept the shorter form in such a case; we should accept 1383 * the longer form. 1384 * 1385 * If the second and third possibilities above both match, we 1386 * have a slightly different situation. The following words 1387 * must match both the 32-bit format 1388 * ``` 1389 * [CRC32] [compressed size] [uncompressed size] [other PK marker] 1390 * ``` 1391 * and the 64-bit format 1392 * ``` 1393 * [CRC32] [compressed low] [compressed high] [uncompressed low] [uncompressed high] [other PK marker] 1394 * ``` 1395 * Since the 32-bit and 64-bit compressed sizes both match, the 1396 * actual size must fit in 32 bits, which implies the high-order 1397 * word of the compressed size is zero. So we know the uncompressed 1398 * low word is zero, which again implies that if we accept the shorter 1399 * format, there will not be a valid PK marker following it. 1400 * 1401 * Similar considerations rule out the shorter form in every other 1402 * possibly-ambiguous pair. So if two of the four possible formats 1403 * match, we should accept the longer option. 1404 * 1405 * If none of the four formats matches, we know the archive must be 1406 * corrupted in some fashion. In particular, it's possible that the 1407 * length-at-end bit was incorrect and we should not really be looking 1408 * for an EOF marker at all. To allow for this possibility, we 1409 * evaluate the following words to collect data for a later error 1410 * report but do not consume any bytes. We instead rely on the later 1411 * search for a new PK marker to re-sync to the next well-formed 1412 * entry. 1413 */ 1414static void 1415consume_end_of_file_marker(struct archive_read *a, struct zip *zip) 1416{ 1417 const char *marker; 1418 const char *p; 1419 uint64_t compressed32, uncompressed32; 1420 uint64_t compressed64, uncompressed64; 1421 uint64_t compressed_actual, uncompressed_actual; 1422 uint32_t crc32_actual; 1423 const uint32_t PK78 = 0x08074B50ULL; 1424 uint8_t crc32_ignored, crc32_may_be_zero; 1425 1426 /* If there shouldn't be a marker, don't consume it. */ 1427 if ((zip->entry->zip_flags & ZIP_LENGTH_AT_END) == 0) { 1428 return; 1429 } 1430 1431 /* The longest Zip end-of-file record is 24 bytes. Since an 1432 * end-of-file record can never appear at the end of the 1433 * archive, we know 24 bytes will be available unless 1434 * the archive is severely truncated. */ 1435 if (NULL == (marker = __archive_read_ahead(a, 24, NULL))) { 1436 return; 1437 } 1438 p = marker; 1439 1440 /* The end-of-file record comprises: 1441 * = Optional PK\007\010 marker 1442 * = 4-byte CRC32 1443 * = Compressed size 1444 * = Uncompressed size 1445 * 1446 * The last two fields are either both 32 bits or both 64 1447 * bits. We check all possible layouts and accept any one 1448 * that gives us a complete match, else we make a best-effort 1449 * attempt to parse out the pieces. 1450 */ 1451 1452 /* CRC32 checking can be tricky: 1453 * * Test suites sometimes ignore the CRC32 1454 * * AES AE-2 always writes zero for the CRC32 1455 * * AES AE-1 sometimes writes zero for the CRC32 1456 */ 1457 crc32_ignored = zip->ignore_crc32; 1458 crc32_may_be_zero = 0; 1459 crc32_actual = zip->computed_crc32; 1460 if (zip->hctx_valid) { 1461 switch (zip->entry->aes_extra.vendor) { 1462 case AES_VENDOR_AE_2: 1463 crc32_actual = 0; 1464 break; 1465 case AES_VENDOR_AE_1: 1466 default: 1467 crc32_may_be_zero = 1; 1468 break; 1469 } 1470 } 1471 1472 /* Values computed from the actual data in the archive. */ 1473 compressed_actual = (uint64_t)zip->entry_compressed_bytes_read; 1474 uncompressed_actual = (uint64_t)zip->entry_uncompressed_bytes_read; 1475 1476 1477 /* Longest: PK78 marker, all 64-bit fields (24 bytes total) */ 1478 if (archive_le32dec(p) == PK78 1479 && ((archive_le32dec(p + 4) == crc32_actual) 1480 || (crc32_may_be_zero && (archive_le32dec(p + 4) == 0)) 1481 || crc32_ignored) 1482 && (archive_le64dec(p + 8) == compressed_actual) 1483 && (archive_le64dec(p + 16) == uncompressed_actual)) { 1484 if (!crc32_ignored) { 1485 zip->entry->crc32 = crc32_actual; 1486 } 1487 zip->entry->compressed_size = compressed_actual; 1488 zip->entry->uncompressed_size = uncompressed_actual; 1489 zip->unconsumed += 24; 1490 return; 1491 } 1492 1493 /* No PK78 marker, 64-bit fields (20 bytes total) */ 1494 if (((archive_le32dec(p) == crc32_actual) 1495 || (crc32_may_be_zero && (archive_le32dec(p + 4) == 0)) 1496 || crc32_ignored) 1497 && (archive_le64dec(p + 4) == compressed_actual) 1498 && (archive_le64dec(p + 12) == uncompressed_actual)) { 1499 if (!crc32_ignored) { 1500 zip->entry->crc32 = crc32_actual; 1501 } 1502 zip->entry->compressed_size = compressed_actual; 1503 zip->entry->uncompressed_size = uncompressed_actual; 1504 zip->unconsumed += 20; 1505 return; 1506 } 1507 1508 /* PK78 marker and 32-bit fields (16 bytes total) */ 1509 if (archive_le32dec(p) == PK78 1510 && ((archive_le32dec(p + 4) == crc32_actual) 1511 || (crc32_may_be_zero && (archive_le32dec(p + 4) == 0)) 1512 || crc32_ignored) 1513 && (archive_le32dec(p + 8) == compressed_actual) 1514 && (archive_le32dec(p + 12) == uncompressed_actual)) { 1515 if (!crc32_ignored) { 1516 zip->entry->crc32 = crc32_actual; 1517 } 1518 zip->entry->compressed_size = compressed_actual; 1519 zip->entry->uncompressed_size = uncompressed_actual; 1520 zip->unconsumed += 16; 1521 return; 1522 } 1523 1524 /* Shortest: No PK78 marker, all 32-bit fields (12 bytes total) */ 1525 if (((archive_le32dec(p) == crc32_actual) 1526 || (crc32_may_be_zero && (archive_le32dec(p + 4) == 0)) 1527 || crc32_ignored) 1528 && (archive_le32dec(p + 4) == compressed_actual) 1529 && (archive_le32dec(p + 8) == uncompressed_actual)) { 1530 if (!crc32_ignored) { 1531 zip->entry->crc32 = crc32_actual; 1532 } 1533 zip->entry->compressed_size = compressed_actual; 1534 zip->entry->uncompressed_size = uncompressed_actual; 1535 zip->unconsumed += 12; 1536 return; 1537 } 1538 1539 /* If none of the above patterns gives us a full exact match, 1540 * then there's something definitely amiss. The fallback code 1541 * below will parse out some plausible values for error 1542 * reporting purposes. Note that this won't actually 1543 * consume anything: 1544 * 1545 * = If there really is a marker here, the logic to resync to 1546 * the next entry will suffice to skip it. 1547 * 1548 * = There might not really be a marker: Corruption or bugs 1549 * may have set the length-at-end bit without a marker ever 1550 * having actually been written. In this case, we 1551 * explicitly should not consume any bytes, since that would 1552 * prevent us from correctly reading the next entry. 1553 */ 1554 if (archive_le32dec(p) == PK78) { 1555 p += 4; /* Ignore PK78 if it appears to be present */ 1556 } 1557 zip->entry->crc32 = archive_le32dec(p); /* Parse CRC32 */ 1558 p += 4; 1559 1560 /* Consider both 32- and 64-bit interpretations */ 1561 compressed32 = archive_le32dec(p); 1562 uncompressed32 = archive_le32dec(p + 4); 1563 compressed64 = archive_le64dec(p); 1564 uncompressed64 = archive_le64dec(p + 8); 1565 1566 /* The earlier patterns may have failed because of CRC32 1567 * mismatch, so it's still possible that both sizes match. 1568 * Try to match as many as we can... 1569 */ 1570 if (compressed32 == compressed_actual 1571 && uncompressed32 == uncompressed_actual) { 1572 /* Both 32-bit fields match */ 1573 zip->entry->compressed_size = compressed32; 1574 zip->entry->uncompressed_size = uncompressed32; 1575 } else if (compressed64 == compressed_actual 1576 || uncompressed64 == uncompressed_actual) { 1577 /* One or both 64-bit fields match */ 1578 zip->entry->compressed_size = compressed64; 1579 zip->entry->uncompressed_size = uncompressed64; 1580 } else { 1581 /* Zero or one 32-bit fields match */ 1582 zip->entry->compressed_size = compressed32; 1583 zip->entry->uncompressed_size = uncompressed32; 1584 } 1585} 1586 1587/* 1588 * Read "uncompressed" data. 1589 * 1590 * This is straightforward if we know the size of the data. This is 1591 * always true for the seeking reader (we've examined the Central 1592 * Directory already), and will often be true for the streaming reader 1593 * (the writer was writing uncompressed so probably knows the size). 1594 * 1595 * If we don't know the size, then life is more interesting. Note 1596 * that a careful reading of the Zip specification says that a writer 1597 * must use ZIP_LENGTH_AT_END if it cannot write the CRC into the 1598 * local header. And if it uses ZIP_LENGTH_AT_END, then it is 1599 * prohibited from storing the sizes in the local header. This 1600 * prevents fully-compliant streaming writers from providing any size 1601 * clues to a streaming reader. In this case, we have to scan the 1602 * data as we read to try to locate the end-of-file marker. 1603 * 1604 * We assume here that the end-of-file marker always has the 1605 * PK\007\010 signature. Although it's technically optional, newer 1606 * writers seem to provide it pretty consistently, and it's not clear 1607 * how to efficiently recognize an end-of-file marker that lacks it. 1608 * 1609 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets 1610 * zip->end_of_entry if it consumes all of the data. 1611 */ 1612static int 1613zip_read_data_none(struct archive_read *a, const void **_buff, 1614 size_t *size, int64_t *offset) 1615{ 1616 struct zip *zip; 1617 const char *buff; 1618 ssize_t bytes_avail; 1619 ssize_t trailing_extra; 1620 int r; 1621 1622 (void)offset; /* UNUSED */ 1623 1624 zip = (struct zip *)(a->format->data); 1625 trailing_extra = zip->hctx_valid ? AUTH_CODE_SIZE : 0; 1626 1627 if (zip->entry->zip_flags & ZIP_LENGTH_AT_END) { 1628 const char *p; 1629 ssize_t grabbing_bytes = 24 + trailing_extra; 1630 1631 /* Grab at least 24 bytes. */ 1632 buff = __archive_read_ahead(a, grabbing_bytes, &bytes_avail); 1633 if (bytes_avail < grabbing_bytes) { 1634 /* Zip archives have end-of-archive markers 1635 that are longer than this, so a failure to get at 1636 least 24 bytes really does indicate a truncated 1637 file. */ 1638 archive_set_error(&a->archive, 1639 ARCHIVE_ERRNO_FILE_FORMAT, 1640 "Truncated ZIP file data"); 1641 return (ARCHIVE_FATAL); 1642 } 1643 /* Check for a complete PK\007\010 signature, followed 1644 * by the correct 4-byte CRC. */ 1645 p = buff + trailing_extra; 1646 if (p[0] == 'P' && p[1] == 'K' 1647 && p[2] == '\007' && p[3] == '\010' 1648 && (archive_le32dec(p + 4) == zip->computed_crc32 1649 || zip->ignore_crc32 1650 || (zip->hctx_valid 1651 && zip->entry->aes_extra.vendor == AES_VENDOR_AE_2))) { 1652 zip->end_of_entry = 1; 1653 if (zip->hctx_valid) { 1654 r = check_authentication_code(a, buff); 1655 if (r != ARCHIVE_OK) 1656 return (r); 1657 } 1658 return (ARCHIVE_OK); 1659 } 1660 /* If not at EOF, ensure we consume at least one byte. */ 1661 ++p; 1662 1663 /* Scan forward until we see where a PK\007\010 signature 1664 * might be. */ 1665 /* Return bytes up until that point. On the next call, 1666 * the code above will verify the data descriptor. */ 1667 while (p < buff + bytes_avail - 4) { 1668 if (p[3] == 'P') { p += 3; } 1669 else if (p[3] == 'K') { p += 2; } 1670 else if (p[3] == '\007') { p += 1; } 1671 else if (p[3] == '\010' && p[2] == '\007' 1672 && p[1] == 'K' && p[0] == 'P') { 1673 break; 1674 } else { p += 4; } 1675 } 1676 p -= trailing_extra; 1677 bytes_avail = p - buff; 1678 } else { 1679 if (zip->entry_bytes_remaining == 0) { 1680 zip->end_of_entry = 1; 1681 if (zip->hctx_valid) { 1682 r = check_authentication_code(a, NULL); 1683 if (r != ARCHIVE_OK) 1684 return (r); 1685 } 1686 return (ARCHIVE_OK); 1687 } 1688 /* Grab a bunch of bytes. */ 1689 buff = __archive_read_ahead(a, 1, &bytes_avail); 1690 if (bytes_avail <= 0) { 1691 archive_set_error(&a->archive, 1692 ARCHIVE_ERRNO_FILE_FORMAT, 1693 "Truncated ZIP file data"); 1694 return (ARCHIVE_FATAL); 1695 } 1696 if (bytes_avail > zip->entry_bytes_remaining) 1697 bytes_avail = (ssize_t)zip->entry_bytes_remaining; 1698 } 1699 if (zip->tctx_valid || zip->cctx_valid) { 1700 size_t dec_size = bytes_avail; 1701 1702 if (dec_size > zip->decrypted_buffer_size) 1703 dec_size = zip->decrypted_buffer_size; 1704 if (zip->tctx_valid) { 1705 trad_enc_decrypt_update(&zip->tctx, 1706 (const uint8_t *)buff, dec_size, 1707 zip->decrypted_buffer, dec_size); 1708 } else { 1709 size_t dsize = dec_size; 1710 archive_hmac_sha1_update(&zip->hctx, 1711 (const uint8_t *)buff, dec_size); 1712 archive_decrypto_aes_ctr_update(&zip->cctx, 1713 (const uint8_t *)buff, dec_size, 1714 zip->decrypted_buffer, &dsize); 1715 } 1716 bytes_avail = dec_size; 1717 buff = (const char *)zip->decrypted_buffer; 1718 } 1719 zip->entry_bytes_remaining -= bytes_avail; 1720 zip->entry_uncompressed_bytes_read += bytes_avail; 1721 zip->entry_compressed_bytes_read += bytes_avail; 1722 zip->unconsumed += bytes_avail; 1723 *size = bytes_avail; 1724 *_buff = buff; 1725 return (ARCHIVE_OK); 1726} 1727 1728#if HAVE_LZMA_H && HAVE_LIBLZMA 1729static int 1730zipx_xz_init(struct archive_read *a, struct zip *zip) 1731{ 1732 lzma_ret r; 1733 1734 if(zip->zipx_lzma_valid) { 1735 lzma_end(&zip->zipx_lzma_stream); 1736 zip->zipx_lzma_valid = 0; 1737 } 1738 1739 memset(&zip->zipx_lzma_stream, 0, sizeof(zip->zipx_lzma_stream)); 1740 r = lzma_stream_decoder(&zip->zipx_lzma_stream, UINT64_MAX, 0); 1741 if (r != LZMA_OK) { 1742 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC, 1743 "xz initialization failed(%d)", 1744 r); 1745 1746 return (ARCHIVE_FAILED); 1747 } 1748 1749 zip->zipx_lzma_valid = 1; 1750 1751 free(zip->uncompressed_buffer); 1752 1753 zip->uncompressed_buffer_size = 256 * 1024; 1754 zip->uncompressed_buffer = 1755 (uint8_t*) malloc(zip->uncompressed_buffer_size); 1756 if (zip->uncompressed_buffer == NULL) { 1757 archive_set_error(&a->archive, ENOMEM, 1758 "No memory for xz decompression"); 1759 return (ARCHIVE_FATAL); 1760 } 1761 1762 zip->decompress_init = 1; 1763 return (ARCHIVE_OK); 1764} 1765 1766static int 1767zipx_lzma_alone_init(struct archive_read *a, struct zip *zip) 1768{ 1769 lzma_ret r; 1770 const uint8_t* p; 1771 1772#pragma pack(push) 1773#pragma pack(1) 1774 struct _alone_header { 1775 uint8_t bytes[5]; 1776 uint64_t uncompressed_size; 1777 } alone_header; 1778#pragma pack(pop) 1779 1780 if(zip->zipx_lzma_valid) { 1781 lzma_end(&zip->zipx_lzma_stream); 1782 zip->zipx_lzma_valid = 0; 1783 } 1784 1785 /* To unpack ZIPX's "LZMA" (id 14) stream we can use standard liblzma 1786 * that is a part of XZ Utils. The stream format stored inside ZIPX 1787 * file is a modified "lzma alone" file format, that was used by the 1788 * `lzma` utility which was later deprecated in favour of `xz` utility. 1789 * Since those formats are nearly the same, we can use a standard 1790 * "lzma alone" decoder from XZ Utils. */ 1791 1792 memset(&zip->zipx_lzma_stream, 0, sizeof(zip->zipx_lzma_stream)); 1793 r = lzma_alone_decoder(&zip->zipx_lzma_stream, UINT64_MAX); 1794 if (r != LZMA_OK) { 1795 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC, 1796 "lzma initialization failed(%d)", r); 1797 1798 return (ARCHIVE_FAILED); 1799 } 1800 1801 /* Flag the cleanup function that we want our lzma-related structures 1802 * to be freed later. */ 1803 zip->zipx_lzma_valid = 1; 1804 1805 /* The "lzma alone" file format and the stream format inside ZIPx are 1806 * almost the same. Here's an example of a structure of "lzma alone" 1807 * format: 1808 * 1809 * $ cat /bin/ls | lzma | xxd | head -n 1 1810 * 00000000: 5d00 0080 00ff ffff ffff ffff ff00 2814 1811 * 1812 * 5 bytes 8 bytes n bytes 1813 * <lzma_params><uncompressed_size><data...> 1814 * 1815 * lzma_params is a 5-byte blob that has to be decoded to extract 1816 * parameters of this LZMA stream. The uncompressed_size field is an 1817 * uint64_t value that contains information about the size of the 1818 * uncompressed file, or UINT64_MAX if this value is unknown. 1819 * The <data...> part is the actual lzma-compressed data stream. 1820 * 1821 * Now here's the structure of the stream inside the ZIPX file: 1822 * 1823 * $ cat stream_inside_zipx | xxd | head -n 1 1824 * 00000000: 0914 0500 5d00 8000 0000 2814 .... .... 1825 * 1826 * 2byte 2byte 5 bytes n bytes 1827 * <magic1><magic2><lzma_params><data...> 1828 * 1829 * This means that the ZIPX file contains an additional magic1 and 1830 * magic2 headers, the lzma_params field contains the same parameter 1831 * set as in the "lzma alone" format, and the <data...> field is the 1832 * same as in the "lzma alone" format as well. Note that also the zipx 1833 * format is missing the uncompressed_size field. 1834 * 1835 * So, in order to use the "lzma alone" decoder for the zipx lzma 1836 * stream, we simply need to shuffle around some fields, prepare a new 1837 * lzma alone header, feed it into lzma alone decoder so it will 1838 * initialize itself properly, and then we can start feeding normal 1839 * zipx lzma stream into the decoder. 1840 */ 1841 1842 /* Read magic1,magic2,lzma_params from the ZIPX stream. */ 1843 if(zip->entry_bytes_remaining < 9 || (p = __archive_read_ahead(a, 9, NULL)) == NULL) { 1844 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 1845 "Truncated lzma data"); 1846 return (ARCHIVE_FATAL); 1847 } 1848 1849 if(p[2] != 0x05 || p[3] != 0x00) { 1850 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 1851 "Invalid lzma data"); 1852 return (ARCHIVE_FATAL); 1853 } 1854 1855 /* Prepare an lzma alone header: copy the lzma_params blob into 1856 * a proper place into the lzma alone header. */ 1857 memcpy(&alone_header.bytes[0], p + 4, 5); 1858 1859 /* Initialize the 'uncompressed size' field to unknown; we'll manually 1860 * monitor how many bytes there are still to be uncompressed. */ 1861 alone_header.uncompressed_size = UINT64_MAX; 1862 1863 if(!zip->uncompressed_buffer) { 1864 zip->uncompressed_buffer_size = 256 * 1024; 1865 zip->uncompressed_buffer = 1866 (uint8_t*) malloc(zip->uncompressed_buffer_size); 1867 1868 if (zip->uncompressed_buffer == NULL) { 1869 archive_set_error(&a->archive, ENOMEM, 1870 "No memory for lzma decompression"); 1871 return (ARCHIVE_FATAL); 1872 } 1873 } 1874 1875 zip->zipx_lzma_stream.next_in = (void*) &alone_header; 1876 zip->zipx_lzma_stream.avail_in = sizeof(alone_header); 1877 zip->zipx_lzma_stream.total_in = 0; 1878 zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer; 1879 zip->zipx_lzma_stream.avail_out = zip->uncompressed_buffer_size; 1880 zip->zipx_lzma_stream.total_out = 0; 1881 1882 /* Feed only the header into the lzma alone decoder. This will 1883 * effectively initialize the decoder, and will not produce any 1884 * output bytes yet. */ 1885 r = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN); 1886 if (r != LZMA_OK) { 1887 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, 1888 "lzma stream initialization error"); 1889 return ARCHIVE_FATAL; 1890 } 1891 1892 /* We've already consumed some bytes, so take this into account. */ 1893 __archive_read_consume(a, 9); 1894 zip->entry_bytes_remaining -= 9; 1895 zip->entry_compressed_bytes_read += 9; 1896 1897 zip->decompress_init = 1; 1898 return (ARCHIVE_OK); 1899} 1900 1901static int 1902zip_read_data_zipx_xz(struct archive_read *a, const void **buff, 1903 size_t *size, int64_t *offset) 1904{ 1905 struct zip* zip = (struct zip *)(a->format->data); 1906 int ret; 1907 lzma_ret lz_ret; 1908 const void* compressed_buf; 1909 ssize_t bytes_avail, in_bytes, to_consume = 0; 1910 1911 (void) offset; /* UNUSED */ 1912 1913 /* Initialize decompressor if not yet initialized. */ 1914 if (!zip->decompress_init) { 1915 ret = zipx_xz_init(a, zip); 1916 if (ret != ARCHIVE_OK) 1917 return (ret); 1918 } 1919 1920 compressed_buf = __archive_read_ahead(a, 1, &bytes_avail); 1921 if (bytes_avail < 0) { 1922 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 1923 "Truncated xz file body"); 1924 return (ARCHIVE_FATAL); 1925 } 1926 1927 in_bytes = zipmin(zip->entry_bytes_remaining, bytes_avail); 1928 zip->zipx_lzma_stream.next_in = compressed_buf; 1929 zip->zipx_lzma_stream.avail_in = in_bytes; 1930 zip->zipx_lzma_stream.total_in = 0; 1931 zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer; 1932 zip->zipx_lzma_stream.avail_out = zip->uncompressed_buffer_size; 1933 zip->zipx_lzma_stream.total_out = 0; 1934 1935 /* Perform the decompression. */ 1936 lz_ret = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN); 1937 switch(lz_ret) { 1938 case LZMA_DATA_ERROR: 1939 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1940 "xz data error (error %d)", (int) lz_ret); 1941 return (ARCHIVE_FATAL); 1942 1943 case LZMA_NO_CHECK: 1944 case LZMA_OK: 1945 break; 1946 1947 default: 1948 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1949 "xz unknown error %d", (int) lz_ret); 1950 return (ARCHIVE_FATAL); 1951 1952 case LZMA_STREAM_END: 1953 lzma_end(&zip->zipx_lzma_stream); 1954 zip->zipx_lzma_valid = 0; 1955 1956 if((int64_t) zip->zipx_lzma_stream.total_in != 1957 zip->entry_bytes_remaining) 1958 { 1959 archive_set_error(&a->archive, 1960 ARCHIVE_ERRNO_MISC, 1961 "xz premature end of stream"); 1962 return (ARCHIVE_FATAL); 1963 } 1964 1965 zip->end_of_entry = 1; 1966 break; 1967 } 1968 1969 to_consume = zip->zipx_lzma_stream.total_in; 1970 1971 __archive_read_consume(a, to_consume); 1972 zip->entry_bytes_remaining -= to_consume; 1973 zip->entry_compressed_bytes_read += to_consume; 1974 zip->entry_uncompressed_bytes_read += zip->zipx_lzma_stream.total_out; 1975 1976 *size = zip->zipx_lzma_stream.total_out; 1977 *buff = zip->uncompressed_buffer; 1978 1979 return (ARCHIVE_OK); 1980} 1981 1982static int 1983zip_read_data_zipx_lzma_alone(struct archive_read *a, const void **buff, 1984 size_t *size, int64_t *offset) 1985{ 1986 struct zip* zip = (struct zip *)(a->format->data); 1987 int ret; 1988 lzma_ret lz_ret; 1989 const void* compressed_buf; 1990 ssize_t bytes_avail, in_bytes, to_consume; 1991 1992 (void) offset; /* UNUSED */ 1993 1994 /* Initialize decompressor if not yet initialized. */ 1995 if (!zip->decompress_init) { 1996 ret = zipx_lzma_alone_init(a, zip); 1997 if (ret != ARCHIVE_OK) 1998 return (ret); 1999 } 2000 2001 /* Fetch more compressed data. The same note as in deflate handler 2002 * applies here as well: 2003 * 2004 * Note: '1' here is a performance optimization. Recall that the 2005 * decompression layer returns a count of available bytes; asking for 2006 * more than that forces the decompressor to combine reads by copying 2007 * data. 2008 */ 2009 compressed_buf = __archive_read_ahead(a, 1, &bytes_avail); 2010 if (bytes_avail < 0) { 2011 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2012 "Truncated lzma file body"); 2013 return (ARCHIVE_FATAL); 2014 } 2015 2016 /* Set decompressor parameters. */ 2017 in_bytes = zipmin(zip->entry_bytes_remaining, bytes_avail); 2018 2019 zip->zipx_lzma_stream.next_in = compressed_buf; 2020 zip->zipx_lzma_stream.avail_in = in_bytes; 2021 zip->zipx_lzma_stream.total_in = 0; 2022 zip->zipx_lzma_stream.next_out = zip->uncompressed_buffer; 2023 zip->zipx_lzma_stream.avail_out = 2024 /* These lzma_alone streams lack end of stream marker, so let's 2025 * make sure the unpacker won't try to unpack more than it's 2026 * supposed to. */ 2027 zipmin((int64_t) zip->uncompressed_buffer_size, 2028 zip->entry->uncompressed_size - 2029 zip->entry_uncompressed_bytes_read); 2030 zip->zipx_lzma_stream.total_out = 0; 2031 2032 /* Perform the decompression. */ 2033 lz_ret = lzma_code(&zip->zipx_lzma_stream, LZMA_RUN); 2034 switch(lz_ret) { 2035 case LZMA_DATA_ERROR: 2036 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2037 "lzma data error (error %d)", (int) lz_ret); 2038 return (ARCHIVE_FATAL); 2039 2040 /* This case is optional in lzma alone format. It can happen, 2041 * but most of the files don't have it. (GitHub #1257) */ 2042 case LZMA_STREAM_END: 2043 if((int64_t) zip->zipx_lzma_stream.total_in != 2044 zip->entry_bytes_remaining) 2045 { 2046 archive_set_error(&a->archive, 2047 ARCHIVE_ERRNO_MISC, 2048 "lzma alone premature end of stream"); 2049 return (ARCHIVE_FATAL); 2050 } 2051 2052 zip->end_of_entry = 1; 2053 break; 2054 2055 case LZMA_OK: 2056 break; 2057 2058 default: 2059 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2060 "lzma unknown error %d", (int) lz_ret); 2061 return (ARCHIVE_FATAL); 2062 } 2063 2064 to_consume = zip->zipx_lzma_stream.total_in; 2065 2066 /* Update pointers. */ 2067 __archive_read_consume(a, to_consume); 2068 zip->entry_bytes_remaining -= to_consume; 2069 zip->entry_compressed_bytes_read += to_consume; 2070 zip->entry_uncompressed_bytes_read += zip->zipx_lzma_stream.total_out; 2071 2072 if(zip->entry_bytes_remaining == 0) { 2073 zip->end_of_entry = 1; 2074 } 2075 2076 /* Free lzma decoder handle because we'll no longer need it. */ 2077 /* This cannot be folded into LZMA_STREAM_END handling above 2078 * because the stream end marker is not required in this format. */ 2079 if(zip->end_of_entry) { 2080 lzma_end(&zip->zipx_lzma_stream); 2081 zip->zipx_lzma_valid = 0; 2082 } 2083 2084 /* Return values. */ 2085 *size = zip->zipx_lzma_stream.total_out; 2086 *buff = zip->uncompressed_buffer; 2087 2088 /* If we're here, then we're good! */ 2089 return (ARCHIVE_OK); 2090} 2091#endif /* HAVE_LZMA_H && HAVE_LIBLZMA */ 2092 2093static int 2094zipx_ppmd8_init(struct archive_read *a, struct zip *zip) 2095{ 2096 const void* p; 2097 uint32_t val; 2098 uint32_t order; 2099 uint32_t mem; 2100 uint32_t restore_method; 2101 2102 /* Remove previous decompression context if it exists. */ 2103 if(zip->ppmd8_valid) { 2104 __archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8); 2105 zip->ppmd8_valid = 0; 2106 } 2107 2108 /* Create a new decompression context. */ 2109 __archive_ppmd8_functions.Ppmd8_Construct(&zip->ppmd8); 2110 zip->ppmd8_stream_failed = 0; 2111 2112 /* Setup function pointers required by Ppmd8 decompressor. The 2113 * 'ppmd_read' function will feed new bytes to the decompressor, 2114 * and will increment the 'zip->zipx_ppmd_read_compressed' counter. */ 2115 zip->ppmd8.Stream.In = &zip->zipx_ppmd_stream; 2116 zip->zipx_ppmd_stream.a = a; 2117 zip->zipx_ppmd_stream.Read = &ppmd_read; 2118 2119 /* Reset number of read bytes to 0. */ 2120 zip->zipx_ppmd_read_compressed = 0; 2121 2122 /* Read Ppmd8 header (2 bytes). */ 2123 p = __archive_read_ahead(a, 2, NULL); 2124 if(!p) { 2125 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2126 "Truncated file data in PPMd8 stream"); 2127 return (ARCHIVE_FATAL); 2128 } 2129 __archive_read_consume(a, 2); 2130 2131 /* Decode the stream's compression parameters. */ 2132 val = archive_le16dec(p); 2133 order = (val & 15) + 1; 2134 mem = ((val >> 4) & 0xff) + 1; 2135 restore_method = (val >> 12); 2136 2137 if(order < 2 || restore_method > 2) { 2138 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2139 "Invalid parameter set in PPMd8 stream (order=%" PRId32 ", " 2140 "restore=%" PRId32 ")", order, restore_method); 2141 return (ARCHIVE_FAILED); 2142 } 2143 2144 /* Allocate the memory needed to properly decompress the file. */ 2145 if(!__archive_ppmd8_functions.Ppmd8_Alloc(&zip->ppmd8, mem << 20)) { 2146 archive_set_error(&a->archive, ENOMEM, 2147 "Unable to allocate memory for PPMd8 stream: %" PRId32 " bytes", 2148 mem << 20); 2149 return (ARCHIVE_FATAL); 2150 } 2151 2152 /* Signal the cleanup function to release Ppmd8 context in the 2153 * cleanup phase. */ 2154 zip->ppmd8_valid = 1; 2155 2156 /* Perform further Ppmd8 initialization. */ 2157 if(!__archive_ppmd8_functions.Ppmd8_RangeDec_Init(&zip->ppmd8)) { 2158 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, 2159 "PPMd8 stream range decoder initialization error"); 2160 return (ARCHIVE_FATAL); 2161 } 2162 2163 __archive_ppmd8_functions.Ppmd8_Init(&zip->ppmd8, order, 2164 restore_method); 2165 2166 /* Allocate the buffer that will hold uncompressed data. */ 2167 free(zip->uncompressed_buffer); 2168 2169 zip->uncompressed_buffer_size = 256 * 1024; 2170 zip->uncompressed_buffer = 2171 (uint8_t*) malloc(zip->uncompressed_buffer_size); 2172 2173 if(zip->uncompressed_buffer == NULL) { 2174 archive_set_error(&a->archive, ENOMEM, 2175 "No memory for PPMd8 decompression"); 2176 return ARCHIVE_FATAL; 2177 } 2178 2179 /* Ppmd8 initialization is done. */ 2180 zip->decompress_init = 1; 2181 2182 /* We've already read 2 bytes in the output stream. Additionally, 2183 * Ppmd8 initialization code could read some data as well. So we 2184 * are advancing the stream by 2 bytes plus whatever number of 2185 * bytes Ppmd8 init function used. */ 2186 zip->entry_compressed_bytes_read += 2 + zip->zipx_ppmd_read_compressed; 2187 2188 return ARCHIVE_OK; 2189} 2190 2191static int 2192zip_read_data_zipx_ppmd(struct archive_read *a, const void **buff, 2193 size_t *size, int64_t *offset) 2194{ 2195 struct zip* zip = (struct zip *)(a->format->data); 2196 int ret; 2197 size_t consumed_bytes = 0; 2198 ssize_t bytes_avail = 0; 2199 2200 (void) offset; /* UNUSED */ 2201 2202 /* If we're here for the first time, initialize Ppmd8 decompression 2203 * context first. */ 2204 if(!zip->decompress_init) { 2205 ret = zipx_ppmd8_init(a, zip); 2206 if(ret != ARCHIVE_OK) 2207 return ret; 2208 } 2209 2210 /* Fetch for more data. We're reading 1 byte here, but libarchive 2211 * should prefetch more bytes. */ 2212 (void) __archive_read_ahead(a, 1, &bytes_avail); 2213 if(bytes_avail < 0) { 2214 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2215 "Truncated PPMd8 file body"); 2216 return (ARCHIVE_FATAL); 2217 } 2218 2219 /* This counter will be updated inside ppmd_read(), which at one 2220 * point will be called by Ppmd8_DecodeSymbol. */ 2221 zip->zipx_ppmd_read_compressed = 0; 2222 2223 /* Decompression loop. */ 2224 do { 2225 int sym = __archive_ppmd8_functions.Ppmd8_DecodeSymbol( 2226 &zip->ppmd8); 2227 if(sym < 0) { 2228 zip->end_of_entry = 1; 2229 break; 2230 } 2231 2232 /* This field is set by ppmd_read() when there was no more data 2233 * to be read. */ 2234 if(zip->ppmd8_stream_failed) { 2235 archive_set_error(&a->archive, 2236 ARCHIVE_ERRNO_FILE_FORMAT, 2237 "Truncated PPMd8 file body"); 2238 return (ARCHIVE_FATAL); 2239 } 2240 2241 zip->uncompressed_buffer[consumed_bytes] = (uint8_t) sym; 2242 ++consumed_bytes; 2243 } while(consumed_bytes < zip->uncompressed_buffer_size); 2244 2245 /* Update pointers so we can continue decompression in another call. */ 2246 zip->entry_bytes_remaining -= zip->zipx_ppmd_read_compressed; 2247 zip->entry_compressed_bytes_read += zip->zipx_ppmd_read_compressed; 2248 zip->entry_uncompressed_bytes_read += consumed_bytes; 2249 2250 /* If we're at the end of stream, deinitialize Ppmd8 context. */ 2251 if(zip->end_of_entry) { 2252 __archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8); 2253 zip->ppmd8_valid = 0; 2254 } 2255 2256 /* Update pointers for libarchive. */ 2257 *buff = zip->uncompressed_buffer; 2258 *size = consumed_bytes; 2259 2260 return ARCHIVE_OK; 2261} 2262 2263#ifdef HAVE_BZLIB_H 2264static int 2265zipx_bzip2_init(struct archive_read *a, struct zip *zip) 2266{ 2267 int r; 2268 2269 /* Deallocate already existing BZ2 decompression context if it 2270 * exists. */ 2271 if(zip->bzstream_valid) { 2272 BZ2_bzDecompressEnd(&zip->bzstream); 2273 zip->bzstream_valid = 0; 2274 } 2275 2276 /* Allocate a new BZ2 decompression context. */ 2277 memset(&zip->bzstream, 0, sizeof(bz_stream)); 2278 r = BZ2_bzDecompressInit(&zip->bzstream, 0, 1); 2279 if(r != BZ_OK) { 2280 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC, 2281 "bzip2 initialization failed(%d)", 2282 r); 2283 2284 return ARCHIVE_FAILED; 2285 } 2286 2287 /* Mark the bzstream field to be released in cleanup phase. */ 2288 zip->bzstream_valid = 1; 2289 2290 /* (Re)allocate the buffer that will contain decompressed bytes. */ 2291 free(zip->uncompressed_buffer); 2292 2293 zip->uncompressed_buffer_size = 256 * 1024; 2294 zip->uncompressed_buffer = 2295 (uint8_t*) malloc(zip->uncompressed_buffer_size); 2296 if (zip->uncompressed_buffer == NULL) { 2297 archive_set_error(&a->archive, ENOMEM, 2298 "No memory for bzip2 decompression"); 2299 return ARCHIVE_FATAL; 2300 } 2301 2302 /* Initialization done. */ 2303 zip->decompress_init = 1; 2304 return ARCHIVE_OK; 2305} 2306 2307static int 2308zip_read_data_zipx_bzip2(struct archive_read *a, const void **buff, 2309 size_t *size, int64_t *offset) 2310{ 2311 struct zip *zip = (struct zip *)(a->format->data); 2312 ssize_t bytes_avail = 0, in_bytes, to_consume; 2313 const void *compressed_buff; 2314 int r; 2315 uint64_t total_out; 2316 2317 (void) offset; /* UNUSED */ 2318 2319 /* Initialize decompression context if we're here for the first time. */ 2320 if(!zip->decompress_init) { 2321 r = zipx_bzip2_init(a, zip); 2322 if(r != ARCHIVE_OK) 2323 return r; 2324 } 2325 2326 /* Fetch more compressed bytes. */ 2327 compressed_buff = __archive_read_ahead(a, 1, &bytes_avail); 2328 if(bytes_avail < 0) { 2329 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2330 "Truncated bzip2 file body"); 2331 return (ARCHIVE_FATAL); 2332 } 2333 2334 in_bytes = zipmin(zip->entry_bytes_remaining, bytes_avail); 2335 if(in_bytes < 1) { 2336 /* libbz2 doesn't complain when caller feeds avail_in == 0. 2337 * It will actually return success in this case, which is 2338 * undesirable. This is why we need to make this check 2339 * manually. */ 2340 2341 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2342 "Truncated bzip2 file body"); 2343 return (ARCHIVE_FATAL); 2344 } 2345 2346 /* Setup buffer boundaries. */ 2347 zip->bzstream.next_in = (char*)(uintptr_t) compressed_buff; 2348 zip->bzstream.avail_in = (uint32_t)in_bytes; 2349 zip->bzstream.total_in_hi32 = 0; 2350 zip->bzstream.total_in_lo32 = 0; 2351 zip->bzstream.next_out = (char*) zip->uncompressed_buffer; 2352 zip->bzstream.avail_out = (uint32_t)zip->uncompressed_buffer_size; 2353 zip->bzstream.total_out_hi32 = 0; 2354 zip->bzstream.total_out_lo32 = 0; 2355 2356 /* Perform the decompression. */ 2357 r = BZ2_bzDecompress(&zip->bzstream); 2358 switch(r) { 2359 case BZ_STREAM_END: 2360 /* If we're at the end of the stream, deinitialize the 2361 * decompression context now. */ 2362 switch(BZ2_bzDecompressEnd(&zip->bzstream)) { 2363 case BZ_OK: 2364 break; 2365 default: 2366 archive_set_error(&a->archive, 2367 ARCHIVE_ERRNO_MISC, 2368 "Failed to clean up bzip2 " 2369 "decompressor"); 2370 return ARCHIVE_FATAL; 2371 } 2372 2373 zip->end_of_entry = 1; 2374 break; 2375 case BZ_OK: 2376 /* The decompressor has successfully decoded this 2377 * chunk of data, but more data is still in queue. */ 2378 break; 2379 default: 2380 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2381 "bzip2 decompression failed"); 2382 return ARCHIVE_FATAL; 2383 } 2384 2385 /* Update the pointers so decompressor can continue decoding. */ 2386 to_consume = zip->bzstream.total_in_lo32; 2387 __archive_read_consume(a, to_consume); 2388 2389 total_out = ((uint64_t) zip->bzstream.total_out_hi32 << 32) | 2390 zip->bzstream.total_out_lo32; 2391 2392 zip->entry_bytes_remaining -= to_consume; 2393 zip->entry_compressed_bytes_read += to_consume; 2394 zip->entry_uncompressed_bytes_read += total_out; 2395 2396 /* Give libarchive its due. */ 2397 *size = total_out; 2398 *buff = zip->uncompressed_buffer; 2399 2400 return ARCHIVE_OK; 2401} 2402 2403#endif 2404 2405#if HAVE_ZSTD_H && HAVE_LIBZSTD 2406static int 2407zipx_zstd_init(struct archive_read *a, struct zip *zip) 2408{ 2409 size_t r; 2410 2411 /* Deallocate already existing Zstd decompression context if it 2412 * exists. */ 2413 if(zip->zstdstream_valid) { 2414 ZSTD_freeDStream(zip->zstdstream); 2415 zip->zstdstream_valid = 0; 2416 } 2417 2418 /* Allocate a new Zstd decompression context. */ 2419 zip->zstdstream = ZSTD_createDStream(); 2420 2421 r = ZSTD_initDStream(zip->zstdstream); 2422 if (ZSTD_isError(r)) { 2423 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2424 "Error initializing zstd decompressor: %s", 2425 ZSTD_getErrorName(r)); 2426 2427 return ARCHIVE_FAILED; 2428 } 2429 2430 /* Mark the zstdstream field to be released in cleanup phase. */ 2431 zip->zstdstream_valid = 1; 2432 2433 /* (Re)allocate the buffer that will contain decompressed bytes. */ 2434 free(zip->uncompressed_buffer); 2435 2436 zip->uncompressed_buffer_size = ZSTD_DStreamOutSize(); 2437 zip->uncompressed_buffer = 2438 (uint8_t*) malloc(zip->uncompressed_buffer_size); 2439 if (zip->uncompressed_buffer == NULL) { 2440 archive_set_error(&a->archive, ENOMEM, 2441 "No memory for Zstd decompression"); 2442 2443 return ARCHIVE_FATAL; 2444 } 2445 2446 /* Initialization done. */ 2447 zip->decompress_init = 1; 2448 return ARCHIVE_OK; 2449} 2450 2451static int 2452zip_read_data_zipx_zstd(struct archive_read *a, const void **buff, 2453 size_t *size, int64_t *offset) 2454{ 2455 struct zip *zip = (struct zip *)(a->format->data); 2456 ssize_t bytes_avail = 0, in_bytes, to_consume; 2457 const void *compressed_buff; 2458 int r; 2459 size_t ret; 2460 uint64_t total_out; 2461 ZSTD_outBuffer out; 2462 ZSTD_inBuffer in; 2463 2464 (void) offset; /* UNUSED */ 2465 2466 /* Initialize decompression context if we're here for the first time. */ 2467 if(!zip->decompress_init) { 2468 r = zipx_zstd_init(a, zip); 2469 if(r != ARCHIVE_OK) 2470 return r; 2471 } 2472 2473 /* Fetch more compressed bytes */ 2474 compressed_buff = __archive_read_ahead(a, 1, &bytes_avail); 2475 if(bytes_avail < 0) { 2476 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2477 "Truncated zstd file body"); 2478 return (ARCHIVE_FATAL); 2479 } 2480 2481 in_bytes = zipmin(zip->entry_bytes_remaining, bytes_avail); 2482 if(in_bytes < 1) { 2483 /* zstd doesn't complain when caller feeds avail_in == 0. 2484 * It will actually return success in this case, which is 2485 * undesirable. This is why we need to make this check 2486 * manually. */ 2487 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2488 "Truncated zstd file body"); 2489 return (ARCHIVE_FATAL); 2490 } 2491 2492 /* Setup buffer boundaries */ 2493 in.src = compressed_buff; 2494 in.size = in_bytes; 2495 in.pos = 0; 2496 out = (ZSTD_outBuffer) { zip->uncompressed_buffer, zip->uncompressed_buffer_size, 0 }; 2497 2498 /* Perform the decompression. */ 2499 ret = ZSTD_decompressStream(zip->zstdstream, &out, &in); 2500 if (ZSTD_isError(ret)) { 2501 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2502 "Error during zstd decompression: %s", 2503 ZSTD_getErrorName(ret)); 2504 return (ARCHIVE_FATAL); 2505 } 2506 2507 /* Check end of the stream. */ 2508 if (ret == 0) { 2509 if ((in.pos == in.size) && (out.pos < out.size)) { 2510 zip->end_of_entry = 1; 2511 ZSTD_freeDStream(zip->zstdstream); 2512 zip->zstdstream_valid = 0; 2513 } 2514 } 2515 2516 /* Update the pointers so decompressor can continue decoding. */ 2517 to_consume = in.pos; 2518 __archive_read_consume(a, to_consume); 2519 2520 total_out = out.pos; 2521 2522 zip->entry_bytes_remaining -= to_consume; 2523 zip->entry_compressed_bytes_read += to_consume; 2524 zip->entry_uncompressed_bytes_read += total_out; 2525 2526 /* Give libarchive its due. */ 2527 *size = total_out; 2528 *buff = zip->uncompressed_buffer; 2529 2530 return ARCHIVE_OK; 2531} 2532#endif 2533 2534#ifdef HAVE_ZLIB_H 2535static int 2536zip_deflate_init(struct archive_read *a, struct zip *zip) 2537{ 2538 int r; 2539 2540 /* If we haven't yet read any data, initialize the decompressor. */ 2541 if (!zip->decompress_init) { 2542 if (zip->stream_valid) 2543 r = inflateReset(&zip->stream); 2544 else 2545 r = inflateInit2(&zip->stream, 2546 -15 /* Don't check for zlib header */); 2547 if (r != Z_OK) { 2548 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2549 "Can't initialize ZIP decompression."); 2550 return (ARCHIVE_FATAL); 2551 } 2552 /* Stream structure has been set up. */ 2553 zip->stream_valid = 1; 2554 /* We've initialized decompression for this stream. */ 2555 zip->decompress_init = 1; 2556 } 2557 return (ARCHIVE_OK); 2558} 2559 2560static int 2561zip_read_data_deflate(struct archive_read *a, const void **buff, 2562 size_t *size, int64_t *offset) 2563{ 2564 struct zip *zip; 2565 ssize_t bytes_avail, to_consume = 0; 2566 const void *compressed_buff, *sp; 2567 int r; 2568 2569 (void)offset; /* UNUSED */ 2570 2571 zip = (struct zip *)(a->format->data); 2572 2573 /* If the buffer hasn't been allocated, allocate it now. */ 2574 if (zip->uncompressed_buffer == NULL) { 2575 zip->uncompressed_buffer_size = 256 * 1024; 2576 zip->uncompressed_buffer 2577 = (unsigned char *)malloc(zip->uncompressed_buffer_size); 2578 if (zip->uncompressed_buffer == NULL) { 2579 archive_set_error(&a->archive, ENOMEM, 2580 "No memory for ZIP decompression"); 2581 return (ARCHIVE_FATAL); 2582 } 2583 } 2584 2585 r = zip_deflate_init(a, zip); 2586 if (r != ARCHIVE_OK) 2587 return (r); 2588 2589 /* 2590 * Note: '1' here is a performance optimization. 2591 * Recall that the decompression layer returns a count of 2592 * available bytes; asking for more than that forces the 2593 * decompressor to combine reads by copying data. 2594 */ 2595 compressed_buff = sp = __archive_read_ahead(a, 1, &bytes_avail); 2596 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) 2597 && bytes_avail > zip->entry_bytes_remaining) { 2598 bytes_avail = (ssize_t)zip->entry_bytes_remaining; 2599 } 2600 if (bytes_avail < 0) { 2601 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2602 "Truncated ZIP file body"); 2603 return (ARCHIVE_FATAL); 2604 } 2605 2606 if (zip->tctx_valid || zip->cctx_valid) { 2607 if (zip->decrypted_bytes_remaining < (size_t)bytes_avail) { 2608 size_t buff_remaining = 2609 (zip->decrypted_buffer + 2610 zip->decrypted_buffer_size) 2611 - (zip->decrypted_ptr + 2612 zip->decrypted_bytes_remaining); 2613 2614 if (buff_remaining > (size_t)bytes_avail) 2615 buff_remaining = (size_t)bytes_avail; 2616 2617 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) && 2618 zip->entry_bytes_remaining > 0) { 2619 if ((int64_t)(zip->decrypted_bytes_remaining 2620 + buff_remaining) 2621 > zip->entry_bytes_remaining) { 2622 if (zip->entry_bytes_remaining < 2623 (int64_t)zip->decrypted_bytes_remaining) 2624 buff_remaining = 0; 2625 else 2626 buff_remaining = 2627 (size_t)zip->entry_bytes_remaining 2628 - zip->decrypted_bytes_remaining; 2629 } 2630 } 2631 if (buff_remaining > 0) { 2632 if (zip->tctx_valid) { 2633 trad_enc_decrypt_update(&zip->tctx, 2634 compressed_buff, buff_remaining, 2635 zip->decrypted_ptr 2636 + zip->decrypted_bytes_remaining, 2637 buff_remaining); 2638 } else { 2639 size_t dsize = buff_remaining; 2640 archive_decrypto_aes_ctr_update( 2641 &zip->cctx, 2642 compressed_buff, buff_remaining, 2643 zip->decrypted_ptr 2644 + zip->decrypted_bytes_remaining, 2645 &dsize); 2646 } 2647 zip->decrypted_bytes_remaining += 2648 buff_remaining; 2649 } 2650 } 2651 bytes_avail = zip->decrypted_bytes_remaining; 2652 compressed_buff = (const char *)zip->decrypted_ptr; 2653 } 2654 2655 /* 2656 * A bug in zlib.h: stream.next_in should be marked 'const' 2657 * but isn't (the library never alters data through the 2658 * next_in pointer, only reads it). The result: this ugly 2659 * cast to remove 'const'. 2660 */ 2661 zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff; 2662 zip->stream.avail_in = (uInt)bytes_avail; 2663 zip->stream.total_in = 0; 2664 zip->stream.next_out = zip->uncompressed_buffer; 2665 zip->stream.avail_out = (uInt)zip->uncompressed_buffer_size; 2666 zip->stream.total_out = 0; 2667 2668 r = inflate(&zip->stream, 0); 2669 switch (r) { 2670 case Z_OK: 2671 break; 2672 case Z_STREAM_END: 2673 zip->end_of_entry = 1; 2674 break; 2675 case Z_MEM_ERROR: 2676 archive_set_error(&a->archive, ENOMEM, 2677 "Out of memory for ZIP decompression"); 2678 return (ARCHIVE_FATAL); 2679 default: 2680 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2681 "ZIP decompression failed (%d)", r); 2682 return (ARCHIVE_FATAL); 2683 } 2684 2685 /* Consume as much as the compressor actually used. */ 2686 to_consume = zip->stream.total_in; 2687 __archive_read_consume(a, to_consume); 2688 zip->entry_bytes_remaining -= to_consume; 2689 zip->entry_compressed_bytes_read += to_consume; 2690 zip->entry_uncompressed_bytes_read += zip->stream.total_out; 2691 2692 if (zip->tctx_valid || zip->cctx_valid) { 2693 zip->decrypted_bytes_remaining -= to_consume; 2694 if (zip->decrypted_bytes_remaining == 0) 2695 zip->decrypted_ptr = zip->decrypted_buffer; 2696 else 2697 zip->decrypted_ptr += to_consume; 2698 } 2699 if (zip->hctx_valid) 2700 archive_hmac_sha1_update(&zip->hctx, sp, to_consume); 2701 2702 if (zip->end_of_entry) { 2703 if (zip->hctx_valid) { 2704 r = check_authentication_code(a, NULL); 2705 if (r != ARCHIVE_OK) { 2706 return (r); 2707 } 2708 } 2709 } 2710 2711 *size = zip->stream.total_out; 2712 *buff = zip->uncompressed_buffer; 2713 2714 return (ARCHIVE_OK); 2715} 2716#endif 2717 2718static int 2719read_decryption_header(struct archive_read *a) 2720{ 2721 struct zip *zip = (struct zip *)(a->format->data); 2722 const char *p; 2723 unsigned int remaining_size; 2724 unsigned int ts; 2725 2726 /* 2727 * Read an initialization vector data field. 2728 */ 2729 p = __archive_read_ahead(a, 2, NULL); 2730 if (p == NULL) 2731 goto truncated; 2732 ts = zip->iv_size; 2733 zip->iv_size = archive_le16dec(p); 2734 __archive_read_consume(a, 2); 2735 if (ts < zip->iv_size) { 2736 free(zip->iv); 2737 zip->iv = NULL; 2738 } 2739 p = __archive_read_ahead(a, zip->iv_size, NULL); 2740 if (p == NULL) 2741 goto truncated; 2742 if (zip->iv == NULL) { 2743 zip->iv = malloc(zip->iv_size); 2744 if (zip->iv == NULL) 2745 goto nomem; 2746 } 2747 memcpy(zip->iv, p, zip->iv_size); 2748 __archive_read_consume(a, zip->iv_size); 2749 2750 /* 2751 * Read a size of remaining decryption header field. 2752 */ 2753 p = __archive_read_ahead(a, 14, NULL); 2754 if (p == NULL) 2755 goto truncated; 2756 remaining_size = archive_le32dec(p); 2757 if (remaining_size < 16 || remaining_size > (1 << 18)) 2758 goto corrupted; 2759 2760 /* Check if format version is supported. */ 2761 if (archive_le16dec(p+4) != 3) { 2762 archive_set_error(&a->archive, 2763 ARCHIVE_ERRNO_FILE_FORMAT, 2764 "Unsupported encryption format version: %u", 2765 archive_le16dec(p+4)); 2766 return (ARCHIVE_FAILED); 2767 } 2768 2769 /* 2770 * Read an encryption algorithm field. 2771 */ 2772 zip->alg_id = archive_le16dec(p+6); 2773 switch (zip->alg_id) { 2774 case 0x6601:/* DES */ 2775 case 0x6602:/* RC2 */ 2776 case 0x6603:/* 3DES 168 */ 2777 case 0x6609:/* 3DES 112 */ 2778 case 0x660E:/* AES 128 */ 2779 case 0x660F:/* AES 192 */ 2780 case 0x6610:/* AES 256 */ 2781 case 0x6702:/* RC2 (version >= 5.2) */ 2782 case 0x6720:/* Blowfish */ 2783 case 0x6721:/* Twofish */ 2784 case 0x6801:/* RC4 */ 2785 /* Supported encryption algorithm. */ 2786 break; 2787 default: 2788 archive_set_error(&a->archive, 2789 ARCHIVE_ERRNO_FILE_FORMAT, 2790 "Unknown encryption algorithm: %u", zip->alg_id); 2791 return (ARCHIVE_FAILED); 2792 } 2793 2794 /* 2795 * Read a bit length field. 2796 */ 2797 zip->bit_len = archive_le16dec(p+8); 2798 2799 /* 2800 * Read a flags field. 2801 */ 2802 zip->flags = archive_le16dec(p+10); 2803 switch (zip->flags & 0xf000) { 2804 case 0x0001: /* Password is required to decrypt. */ 2805 case 0x0002: /* Certificates only. */ 2806 case 0x0003: /* Password or certificate required to decrypt. */ 2807 break; 2808 default: 2809 archive_set_error(&a->archive, 2810 ARCHIVE_ERRNO_FILE_FORMAT, 2811 "Unknown encryption flag: %u", zip->flags); 2812 return (ARCHIVE_FAILED); 2813 } 2814 if ((zip->flags & 0xf000) == 0 || 2815 (zip->flags & 0xf000) == 0x4000) { 2816 archive_set_error(&a->archive, 2817 ARCHIVE_ERRNO_FILE_FORMAT, 2818 "Unknown encryption flag: %u", zip->flags); 2819 return (ARCHIVE_FAILED); 2820 } 2821 2822 /* 2823 * Read an encrypted random data field. 2824 */ 2825 ts = zip->erd_size; 2826 zip->erd_size = archive_le16dec(p+12); 2827 __archive_read_consume(a, 14); 2828 if ((zip->erd_size & 0xf) != 0 || 2829 (zip->erd_size + 16) > remaining_size || 2830 (zip->erd_size + 16) < zip->erd_size) 2831 goto corrupted; 2832 2833 if (ts < zip->erd_size) { 2834 free(zip->erd); 2835 zip->erd = NULL; 2836 } 2837 p = __archive_read_ahead(a, zip->erd_size, NULL); 2838 if (p == NULL) 2839 goto truncated; 2840 if (zip->erd == NULL) { 2841 zip->erd = malloc(zip->erd_size); 2842 if (zip->erd == NULL) 2843 goto nomem; 2844 } 2845 memcpy(zip->erd, p, zip->erd_size); 2846 __archive_read_consume(a, zip->erd_size); 2847 2848 /* 2849 * Read a reserved data field. 2850 */ 2851 p = __archive_read_ahead(a, 4, NULL); 2852 if (p == NULL) 2853 goto truncated; 2854 /* Reserved data size should be zero. */ 2855 if (archive_le32dec(p) != 0) 2856 goto corrupted; 2857 __archive_read_consume(a, 4); 2858 2859 /* 2860 * Read a password validation data field. 2861 */ 2862 p = __archive_read_ahead(a, 2, NULL); 2863 if (p == NULL) 2864 goto truncated; 2865 ts = zip->v_size; 2866 zip->v_size = archive_le16dec(p); 2867 __archive_read_consume(a, 2); 2868 if ((zip->v_size & 0x0f) != 0 || 2869 (zip->erd_size + zip->v_size + 16) > remaining_size || 2870 (zip->erd_size + zip->v_size + 16) < (zip->erd_size + zip->v_size)) 2871 goto corrupted; 2872 if (ts < zip->v_size) { 2873 free(zip->v_data); 2874 zip->v_data = NULL; 2875 } 2876 p = __archive_read_ahead(a, zip->v_size, NULL); 2877 if (p == NULL) 2878 goto truncated; 2879 if (zip->v_data == NULL) { 2880 zip->v_data = malloc(zip->v_size); 2881 if (zip->v_data == NULL) 2882 goto nomem; 2883 } 2884 memcpy(zip->v_data, p, zip->v_size); 2885 __archive_read_consume(a, zip->v_size); 2886 2887 p = __archive_read_ahead(a, 4, NULL); 2888 if (p == NULL) 2889 goto truncated; 2890 zip->v_crc32 = archive_le32dec(p); 2891 __archive_read_consume(a, 4); 2892 2893 /*return (ARCHIVE_OK); 2894 * This is not fully implemented yet.*/ 2895 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2896 "Encrypted file is unsupported"); 2897 return (ARCHIVE_FAILED); 2898truncated: 2899 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2900 "Truncated ZIP file data"); 2901 return (ARCHIVE_FATAL); 2902corrupted: 2903 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2904 "Corrupted ZIP file data"); 2905 return (ARCHIVE_FATAL); 2906nomem: 2907 archive_set_error(&a->archive, ENOMEM, 2908 "No memory for ZIP decryption"); 2909 return (ARCHIVE_FATAL); 2910} 2911 2912static int 2913zip_alloc_decryption_buffer(struct archive_read *a) 2914{ 2915 struct zip *zip = (struct zip *)(a->format->data); 2916 size_t bs = 256 * 1024; 2917 2918 if (zip->decrypted_buffer == NULL) { 2919 zip->decrypted_buffer_size = bs; 2920 zip->decrypted_buffer = malloc(bs); 2921 if (zip->decrypted_buffer == NULL) { 2922 archive_set_error(&a->archive, ENOMEM, 2923 "No memory for ZIP decryption"); 2924 return (ARCHIVE_FATAL); 2925 } 2926 } 2927 zip->decrypted_ptr = zip->decrypted_buffer; 2928 return (ARCHIVE_OK); 2929} 2930 2931static int 2932init_traditional_PKWARE_decryption(struct archive_read *a) 2933{ 2934 struct zip *zip = (struct zip *)(a->format->data); 2935 const void *p; 2936 int retry; 2937 int r; 2938 2939 if (zip->tctx_valid) 2940 return (ARCHIVE_OK); 2941 2942 /* 2943 Read the 12 bytes encryption header stored at 2944 the start of the data area. 2945 */ 2946#define ENC_HEADER_SIZE 12 2947 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) 2948 && zip->entry_bytes_remaining < ENC_HEADER_SIZE) { 2949 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2950 "Truncated Zip encrypted body: only %jd bytes available", 2951 (intmax_t)zip->entry_bytes_remaining); 2952 return (ARCHIVE_FATAL); 2953 } 2954 2955 p = __archive_read_ahead(a, ENC_HEADER_SIZE, NULL); 2956 if (p == NULL) { 2957 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2958 "Truncated ZIP file data"); 2959 return (ARCHIVE_FATAL); 2960 } 2961 2962 for (retry = 0;; retry++) { 2963 const char *passphrase; 2964 uint8_t crcchk; 2965 2966 passphrase = __archive_read_next_passphrase(a); 2967 if (passphrase == NULL) { 2968 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2969 (retry > 0)? 2970 "Incorrect passphrase": 2971 "Passphrase required for this entry"); 2972 return (ARCHIVE_FAILED); 2973 } 2974 2975 /* 2976 * Initialize ctx for Traditional PKWARE Decryption. 2977 */ 2978 r = trad_enc_init(&zip->tctx, passphrase, strlen(passphrase), 2979 p, ENC_HEADER_SIZE, &crcchk); 2980 if (r == 0 && crcchk == zip->entry->decdat) 2981 break;/* The passphrase is OK. */ 2982 if (retry > 10000) { 2983 /* Avoid infinity loop. */ 2984 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2985 "Too many incorrect passphrases"); 2986 return (ARCHIVE_FAILED); 2987 } 2988 } 2989 2990 __archive_read_consume(a, ENC_HEADER_SIZE); 2991 zip->tctx_valid = 1; 2992 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) { 2993 zip->entry_bytes_remaining -= ENC_HEADER_SIZE; 2994 } 2995 /*zip->entry_uncompressed_bytes_read += ENC_HEADER_SIZE;*/ 2996 zip->entry_compressed_bytes_read += ENC_HEADER_SIZE; 2997 zip->decrypted_bytes_remaining = 0; 2998 2999 return (zip_alloc_decryption_buffer(a)); 3000#undef ENC_HEADER_SIZE 3001} 3002 3003static int 3004init_WinZip_AES_decryption(struct archive_read *a) 3005{ 3006 struct zip *zip = (struct zip *)(a->format->data); 3007 const void *p; 3008 const uint8_t *pv; 3009 size_t key_len, salt_len; 3010 uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE]; 3011 int retry; 3012 int r; 3013 3014 if (zip->cctx_valid || zip->hctx_valid) 3015 return (ARCHIVE_OK); 3016 3017 switch (zip->entry->aes_extra.strength) { 3018 case 1: salt_len = 8; key_len = 16; break; 3019 case 2: salt_len = 12; key_len = 24; break; 3020 case 3: salt_len = 16; key_len = 32; break; 3021 default: goto corrupted; 3022 } 3023 p = __archive_read_ahead(a, salt_len + 2, NULL); 3024 if (p == NULL) 3025 goto truncated; 3026 3027 for (retry = 0;; retry++) { 3028 const char *passphrase; 3029 3030 passphrase = __archive_read_next_passphrase(a); 3031 if (passphrase == NULL) { 3032 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 3033 (retry > 0)? 3034 "Incorrect passphrase": 3035 "Passphrase required for this entry"); 3036 return (ARCHIVE_FAILED); 3037 } 3038 memset(derived_key, 0, sizeof(derived_key)); 3039 r = archive_pbkdf2_sha1(passphrase, strlen(passphrase), 3040 p, salt_len, 1000, derived_key, key_len * 2 + 2); 3041 if (r != 0) { 3042 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 3043 "Decryption is unsupported due to lack of " 3044 "crypto library"); 3045 return (ARCHIVE_FAILED); 3046 } 3047 3048 /* Check password verification value. */ 3049 pv = ((const uint8_t *)p) + salt_len; 3050 if (derived_key[key_len * 2] == pv[0] && 3051 derived_key[key_len * 2 + 1] == pv[1]) 3052 break;/* The passphrase is OK. */ 3053 if (retry > 10000) { 3054 /* Avoid infinity loop. */ 3055 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 3056 "Too many incorrect passphrases"); 3057 return (ARCHIVE_FAILED); 3058 } 3059 } 3060 3061 r = archive_decrypto_aes_ctr_init(&zip->cctx, derived_key, key_len); 3062 if (r != 0) { 3063 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 3064 "Decryption is unsupported due to lack of crypto library"); 3065 return (ARCHIVE_FAILED); 3066 } 3067 r = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len, key_len); 3068 if (r != 0) { 3069 archive_decrypto_aes_ctr_release(&zip->cctx); 3070 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 3071 "Failed to initialize HMAC-SHA1"); 3072 return (ARCHIVE_FAILED); 3073 } 3074 zip->cctx_valid = zip->hctx_valid = 1; 3075 __archive_read_consume(a, salt_len + 2); 3076 zip->entry_bytes_remaining -= salt_len + 2 + AUTH_CODE_SIZE; 3077 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) 3078 && zip->entry_bytes_remaining < 0) 3079 goto corrupted; 3080 zip->entry_compressed_bytes_read += salt_len + 2 + AUTH_CODE_SIZE; 3081 zip->decrypted_bytes_remaining = 0; 3082 3083 zip->entry->compression = zip->entry->aes_extra.compression; 3084 return (zip_alloc_decryption_buffer(a)); 3085 3086truncated: 3087 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 3088 "Truncated ZIP file data"); 3089 return (ARCHIVE_FATAL); 3090corrupted: 3091 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 3092 "Corrupted ZIP file data"); 3093 return (ARCHIVE_FATAL); 3094} 3095 3096static int 3097archive_read_format_zip_read_data(struct archive_read *a, 3098 const void **buff, size_t *size, int64_t *offset) 3099{ 3100 int r; 3101 struct zip *zip = (struct zip *)(a->format->data); 3102 3103 if (zip->has_encrypted_entries == 3104 ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) { 3105 zip->has_encrypted_entries = 0; 3106 } 3107 3108 *offset = zip->entry_uncompressed_bytes_read; 3109 *size = 0; 3110 *buff = NULL; 3111 3112 /* If we hit end-of-entry last time, return ARCHIVE_EOF. */ 3113 if (zip->end_of_entry) 3114 return (ARCHIVE_EOF); 3115 3116 /* Return EOF immediately if this is a non-regular file. */ 3117 if (AE_IFREG != (zip->entry->mode & AE_IFMT)) 3118 return (ARCHIVE_EOF); 3119 3120 __archive_read_consume(a, zip->unconsumed); 3121 zip->unconsumed = 0; 3122 3123 if (zip->init_decryption) { 3124 zip->has_encrypted_entries = 1; 3125 if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED) 3126 r = read_decryption_header(a); 3127 else if (zip->entry->compression == WINZIP_AES_ENCRYPTION) 3128 r = init_WinZip_AES_decryption(a); 3129 else 3130 r = init_traditional_PKWARE_decryption(a); 3131 if (r != ARCHIVE_OK) 3132 return (r); 3133 zip->init_decryption = 0; 3134 } 3135 3136 switch(zip->entry->compression) { 3137 case 0: /* No compression. */ 3138 r = zip_read_data_none(a, buff, size, offset); 3139 break; 3140#ifdef HAVE_BZLIB_H 3141 case 12: /* ZIPx bzip2 compression. */ 3142 r = zip_read_data_zipx_bzip2(a, buff, size, offset); 3143 break; 3144#endif 3145#if HAVE_LZMA_H && HAVE_LIBLZMA 3146 case 14: /* ZIPx LZMA compression. */ 3147 r = zip_read_data_zipx_lzma_alone(a, buff, size, offset); 3148 break; 3149 case 95: /* ZIPx XZ compression. */ 3150 r = zip_read_data_zipx_xz(a, buff, size, offset); 3151 break; 3152#endif 3153#if HAVE_ZSTD_H && HAVE_LIBZSTD 3154 case 93: /* ZIPx Zstd compression. */ 3155 r = zip_read_data_zipx_zstd(a, buff, size, offset); 3156 break; 3157#endif 3158 /* PPMd support is built-in, so we don't need any #if guards. */ 3159 case 98: /* ZIPx PPMd compression. */ 3160 r = zip_read_data_zipx_ppmd(a, buff, size, offset); 3161 break; 3162 3163#ifdef HAVE_ZLIB_H 3164 case 8: /* Deflate compression. */ 3165 r = zip_read_data_deflate(a, buff, size, offset); 3166 break; 3167#endif 3168 default: /* Unsupported compression. */ 3169 /* Return a warning. */ 3170 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 3171 "Unsupported ZIP compression method (%d: %s)", 3172 zip->entry->compression, compression_name(zip->entry->compression)); 3173 /* We can't decompress this entry, but we will 3174 * be able to skip() it and try the next entry. */ 3175 return (ARCHIVE_FAILED); 3176 break; 3177 } 3178 if (r != ARCHIVE_OK) 3179 return (r); 3180 if (*size > 0) { 3181 zip->computed_crc32 = zip->crc32func(zip->computed_crc32, *buff, 3182 (unsigned)*size); 3183 } 3184 /* If we hit the end, swallow any end-of-data marker and 3185 * verify the final check values. */ 3186 if (zip->end_of_entry) { 3187 consume_end_of_file_marker(a, zip); 3188 3189 /* Check computed CRC against header */ 3190 if ((!zip->hctx_valid || 3191 zip->entry->aes_extra.vendor != AES_VENDOR_AE_2) && 3192 zip->entry->crc32 != zip->computed_crc32 3193 && !zip->ignore_crc32) { 3194 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 3195 "ZIP bad CRC: 0x%lx should be 0x%lx", 3196 (unsigned long)zip->computed_crc32, 3197 (unsigned long)zip->entry->crc32); 3198 return (ARCHIVE_FAILED); 3199 } 3200 /* Check file size against header. */ 3201 if (zip->entry->compressed_size != 3202 zip->entry_compressed_bytes_read) { 3203 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 3204 "ZIP compressed data is wrong size " 3205 "(read %jd, expected %jd)", 3206 (intmax_t)zip->entry_compressed_bytes_read, 3207 (intmax_t)zip->entry->compressed_size); 3208 return (ARCHIVE_FAILED); 3209 } 3210 /* Size field only stores the lower 32 bits of the actual 3211 * size. */ 3212 if ((zip->entry->uncompressed_size & UINT32_MAX) 3213 != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) { 3214 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 3215 "ZIP uncompressed data is wrong size " 3216 "(read %jd, expected %jd)\n", 3217 (intmax_t)zip->entry_uncompressed_bytes_read, 3218 (intmax_t)zip->entry->uncompressed_size); 3219 return (ARCHIVE_FAILED); 3220 } 3221 } 3222 3223 return (ARCHIVE_OK); 3224} 3225 3226static int 3227archive_read_format_zip_cleanup(struct archive_read *a) 3228{ 3229 struct zip *zip; 3230 struct zip_entry *zip_entry, *next_zip_entry; 3231 3232 zip = (struct zip *)(a->format->data); 3233 3234#ifdef HAVE_ZLIB_H 3235 if (zip->stream_valid) 3236 inflateEnd(&zip->stream); 3237#endif 3238 3239#if HAVE_LZMA_H && HAVE_LIBLZMA 3240 if (zip->zipx_lzma_valid) { 3241 lzma_end(&zip->zipx_lzma_stream); 3242 } 3243#endif 3244 3245#ifdef HAVE_BZLIB_H 3246 if (zip->bzstream_valid) { 3247 BZ2_bzDecompressEnd(&zip->bzstream); 3248 } 3249#endif 3250 3251#if HAVE_ZSTD_H && HAVE_LIBZSTD 3252 if (zip->zstdstream_valid) { 3253 ZSTD_freeDStream(zip->zstdstream); 3254 } 3255#endif 3256 3257 free(zip->uncompressed_buffer); 3258 3259 if (zip->ppmd8_valid) 3260 __archive_ppmd8_functions.Ppmd8_Free(&zip->ppmd8); 3261 3262 if (zip->zip_entries) { 3263 zip_entry = zip->zip_entries; 3264 while (zip_entry != NULL) { 3265 next_zip_entry = zip_entry->next; 3266 archive_string_free(&zip_entry->rsrcname); 3267 free(zip_entry); 3268 zip_entry = next_zip_entry; 3269 } 3270 } 3271 free(zip->decrypted_buffer); 3272 if (zip->cctx_valid) 3273 archive_decrypto_aes_ctr_release(&zip->cctx); 3274 if (zip->hctx_valid) 3275 archive_hmac_sha1_cleanup(&zip->hctx); 3276 free(zip->iv); 3277 free(zip->erd); 3278 free(zip->v_data); 3279 archive_string_free(&zip->format_name); 3280 free(zip); 3281 (a->format->data) = NULL; 3282 return (ARCHIVE_OK); 3283} 3284 3285static int 3286archive_read_format_zip_has_encrypted_entries(struct archive_read *_a) 3287{ 3288 if (_a && _a->format) { 3289 struct zip * zip = (struct zip *)_a->format->data; 3290 if (zip) { 3291 return zip->has_encrypted_entries; 3292 } 3293 } 3294 return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW; 3295} 3296 3297static int 3298archive_read_format_zip_options(struct archive_read *a, 3299 const char *key, const char *val) 3300{ 3301 struct zip *zip; 3302 int ret = ARCHIVE_FAILED; 3303 3304 zip = (struct zip *)(a->format->data); 3305 if (strcmp(key, "compat-2x") == 0) { 3306 /* Handle filenames as libarchive 2.x */ 3307 zip->init_default_conversion = (val != NULL) ? 1 : 0; 3308 return (ARCHIVE_OK); 3309 } else if (strcmp(key, "hdrcharset") == 0) { 3310 if (val == NULL || val[0] == 0) 3311 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 3312 "zip: hdrcharset option needs a character-set name" 3313 ); 3314 else { 3315 zip->sconv = archive_string_conversion_from_charset( 3316 &a->archive, val, 0); 3317 if (zip->sconv != NULL) { 3318 if (strcmp(val, "UTF-8") == 0) 3319 zip->sconv_utf8 = zip->sconv; 3320 ret = ARCHIVE_OK; 3321 } else 3322 ret = ARCHIVE_FATAL; 3323 } 3324 return (ret); 3325 } else if (strcmp(key, "ignorecrc32") == 0) { 3326 /* Mostly useful for testing. */ 3327 if (val == NULL || val[0] == 0) { 3328 zip->crc32func = real_crc32; 3329 zip->ignore_crc32 = 0; 3330 } else { 3331 zip->crc32func = fake_crc32; 3332 zip->ignore_crc32 = 1; 3333 } 3334 return (ARCHIVE_OK); 3335 } else if (strcmp(key, "mac-ext") == 0) { 3336 zip->process_mac_extensions = (val != NULL && val[0] != 0); 3337 return (ARCHIVE_OK); 3338 } 3339 3340 /* Note: The "warn" return is just to inform the options 3341 * supervisor that we didn't handle it. It will generate 3342 * a suitable error if no one used this option. */ 3343 return (ARCHIVE_WARN); 3344} 3345 3346int 3347archive_read_support_format_zip(struct archive *a) 3348{ 3349 int r; 3350 r = archive_read_support_format_zip_streamable(a); 3351 if (r != ARCHIVE_OK) 3352 return r; 3353 return (archive_read_support_format_zip_seekable(a)); 3354} 3355 3356/* ------------------------------------------------------------------------ */ 3357 3358/* 3359 * Streaming-mode support 3360 */ 3361 3362 3363static int 3364archive_read_support_format_zip_capabilities_streamable(struct archive_read * a) 3365{ 3366 (void)a; /* UNUSED */ 3367 return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | 3368 ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA); 3369} 3370 3371static int 3372archive_read_format_zip_streamable_bid(struct archive_read *a, int best_bid) 3373{ 3374 const char *p; 3375 3376 (void)best_bid; /* UNUSED */ 3377 3378 if ((p = __archive_read_ahead(a, 4, NULL)) == NULL) 3379 return (-1); 3380 3381 /* 3382 * Bid of 29 here comes from: 3383 * + 16 bits for "PK", 3384 * + next 16-bit field has 6 options so contributes 3385 * about 16 - log_2(6) ~= 16 - 2.6 ~= 13 bits 3386 * 3387 * So we've effectively verified ~29 total bits of check data. 3388 */ 3389 if (p[0] == 'P' && p[1] == 'K') { 3390 if ((p[2] == '\001' && p[3] == '\002') 3391 || (p[2] == '\003' && p[3] == '\004') 3392 || (p[2] == '\005' && p[3] == '\006') 3393 || (p[2] == '\006' && p[3] == '\006') 3394 || (p[2] == '\007' && p[3] == '\010') 3395 || (p[2] == '0' && p[3] == '0')) 3396 return (29); 3397 } 3398 3399 /* TODO: It's worth looking ahead a little bit for a valid 3400 * PK signature. In particular, that would make it possible 3401 * to read some UUEncoded SFX files or SFX files coming from 3402 * a network socket. */ 3403 3404 return (0); 3405} 3406 3407static int 3408archive_read_format_zip_streamable_read_header(struct archive_read *a, 3409 struct archive_entry *entry) 3410{ 3411 struct zip *zip; 3412 3413 a->archive.archive_format = ARCHIVE_FORMAT_ZIP; 3414 if (a->archive.archive_format_name == NULL) 3415 a->archive.archive_format_name = "ZIP"; 3416 3417 zip = (struct zip *)(a->format->data); 3418 3419 /* 3420 * It should be sufficient to call archive_read_next_header() for 3421 * a reader to determine if an entry is encrypted or not. If the 3422 * encryption of an entry is only detectable when calling 3423 * archive_read_data(), so be it. We'll do the same check there 3424 * as well. 3425 */ 3426 if (zip->has_encrypted_entries == 3427 ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) 3428 zip->has_encrypted_entries = 0; 3429 3430 /* Make sure we have a zip_entry structure to use. */ 3431 if (zip->zip_entries == NULL) { 3432 zip->zip_entries = malloc(sizeof(struct zip_entry)); 3433 if (zip->zip_entries == NULL) { 3434 archive_set_error(&a->archive, ENOMEM, 3435 "Out of memory"); 3436 return ARCHIVE_FATAL; 3437 } 3438 } 3439 zip->entry = zip->zip_entries; 3440 memset(zip->entry, 0, sizeof(struct zip_entry)); 3441 3442 if (zip->cctx_valid) 3443 archive_decrypto_aes_ctr_release(&zip->cctx); 3444 if (zip->hctx_valid) 3445 archive_hmac_sha1_cleanup(&zip->hctx); 3446 zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0; 3447 __archive_read_reset_passphrase(a); 3448 3449 /* Search ahead for the next local file header. */ 3450 __archive_read_consume(a, zip->unconsumed); 3451 zip->unconsumed = 0; 3452 for (;;) { 3453 int64_t skipped = 0; 3454 const char *p, *end; 3455 ssize_t bytes; 3456 3457 p = __archive_read_ahead(a, 4, &bytes); 3458 if (p == NULL) 3459 return (ARCHIVE_FATAL); 3460 end = p + bytes; 3461 3462 while (p + 4 <= end) { 3463 if (p[0] == 'P' && p[1] == 'K') { 3464 if (p[2] == '\003' && p[3] == '\004') { 3465 /* Regular file entry. */ 3466 __archive_read_consume(a, skipped); 3467 return zip_read_local_file_header(a, 3468 entry, zip); 3469 } 3470 3471 /* 3472 * TODO: We cannot restore permissions 3473 * based only on the local file headers. 3474 * Consider scanning the central 3475 * directory and returning additional 3476 * entries for at least directories. 3477 * This would allow us to properly set 3478 * directory permissions. 3479 * 3480 * This won't help us fix symlinks 3481 * and may not help with regular file 3482 * permissions, either. <sigh> 3483 */ 3484 if (p[2] == '\001' && p[3] == '\002') { 3485 return (ARCHIVE_EOF); 3486 } 3487 3488 /* End of central directory? Must be an 3489 * empty archive. */ 3490 if ((p[2] == '\005' && p[3] == '\006') 3491 || (p[2] == '\006' && p[3] == '\006')) 3492 return (ARCHIVE_EOF); 3493 } 3494 ++p; 3495 ++skipped; 3496 } 3497 __archive_read_consume(a, skipped); 3498 } 3499} 3500 3501static int 3502archive_read_format_zip_read_data_skip_streamable(struct archive_read *a) 3503{ 3504 struct zip *zip; 3505 int64_t bytes_skipped; 3506 3507 zip = (struct zip *)(a->format->data); 3508 bytes_skipped = __archive_read_consume(a, zip->unconsumed); 3509 zip->unconsumed = 0; 3510 if (bytes_skipped < 0) 3511 return (ARCHIVE_FATAL); 3512 3513 /* If we've already read to end of data, we're done. */ 3514 if (zip->end_of_entry) 3515 return (ARCHIVE_OK); 3516 3517 /* So we know we're streaming... */ 3518 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) 3519 || zip->entry->compressed_size > 0) { 3520 /* We know the compressed length, so we can just skip. */ 3521 bytes_skipped = __archive_read_consume(a, 3522 zip->entry_bytes_remaining); 3523 if (bytes_skipped < 0) 3524 return (ARCHIVE_FATAL); 3525 return (ARCHIVE_OK); 3526 } 3527 3528 if (zip->init_decryption) { 3529 int r; 3530 3531 zip->has_encrypted_entries = 1; 3532 if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED) 3533 r = read_decryption_header(a); 3534 else if (zip->entry->compression == WINZIP_AES_ENCRYPTION) 3535 r = init_WinZip_AES_decryption(a); 3536 else 3537 r = init_traditional_PKWARE_decryption(a); 3538 if (r != ARCHIVE_OK) 3539 return (r); 3540 zip->init_decryption = 0; 3541 } 3542 3543 /* We're streaming and we don't know the length. */ 3544 /* If the body is compressed and we know the format, we can 3545 * find an exact end-of-entry by decompressing it. */ 3546 switch (zip->entry->compression) { 3547#ifdef HAVE_ZLIB_H 3548 case 8: /* Deflate compression. */ 3549 while (!zip->end_of_entry) { 3550 int64_t offset = 0; 3551 const void *buff = NULL; 3552 size_t size = 0; 3553 int r; 3554 r = zip_read_data_deflate(a, &buff, &size, &offset); 3555 if (r != ARCHIVE_OK) 3556 return (r); 3557 } 3558 return ARCHIVE_OK; 3559#endif 3560 default: /* Uncompressed or unknown. */ 3561 /* Scan for a PK\007\010 signature. */ 3562 for (;;) { 3563 const char *p, *buff; 3564 ssize_t bytes_avail; 3565 buff = __archive_read_ahead(a, 16, &bytes_avail); 3566 if (bytes_avail < 16) { 3567 archive_set_error(&a->archive, 3568 ARCHIVE_ERRNO_FILE_FORMAT, 3569 "Truncated ZIP file data"); 3570 return (ARCHIVE_FATAL); 3571 } 3572 p = buff; 3573 while (p <= buff + bytes_avail - 16) { 3574 if (p[3] == 'P') { p += 3; } 3575 else if (p[3] == 'K') { p += 2; } 3576 else if (p[3] == '\007') { p += 1; } 3577 else if (p[3] == '\010' && p[2] == '\007' 3578 && p[1] == 'K' && p[0] == 'P') { 3579 if (zip->entry->flags & LA_USED_ZIP64) 3580 __archive_read_consume(a, 3581 p - buff + 24); 3582 else 3583 __archive_read_consume(a, 3584 p - buff + 16); 3585 return ARCHIVE_OK; 3586 } else { p += 4; } 3587 } 3588 __archive_read_consume(a, p - buff); 3589 } 3590 } 3591} 3592 3593int 3594archive_read_support_format_zip_streamable(struct archive *_a) 3595{ 3596 struct archive_read *a = (struct archive_read *)_a; 3597 struct zip *zip; 3598 int r; 3599 3600 archive_check_magic(_a, ARCHIVE_READ_MAGIC, 3601 ARCHIVE_STATE_NEW, "archive_read_support_format_zip"); 3602 3603 zip = (struct zip *)calloc(1, sizeof(*zip)); 3604 if (zip == NULL) { 3605 archive_set_error(&a->archive, ENOMEM, 3606 "Can't allocate zip data"); 3607 return (ARCHIVE_FATAL); 3608 } 3609 3610 /* Streamable reader doesn't support mac extensions. */ 3611 zip->process_mac_extensions = 0; 3612 3613 /* 3614 * Until enough data has been read, we cannot tell about 3615 * any encrypted entries yet. 3616 */ 3617 zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW; 3618 zip->crc32func = real_crc32; 3619 3620 r = __archive_read_register_format(a, 3621 zip, 3622 "zip", 3623 archive_read_format_zip_streamable_bid, 3624 archive_read_format_zip_options, 3625 archive_read_format_zip_streamable_read_header, 3626 archive_read_format_zip_read_data, 3627 archive_read_format_zip_read_data_skip_streamable, 3628 NULL, 3629 archive_read_format_zip_cleanup, 3630 archive_read_support_format_zip_capabilities_streamable, 3631 archive_read_format_zip_has_encrypted_entries); 3632 3633 if (r != ARCHIVE_OK) 3634 free(zip); 3635 return (ARCHIVE_OK); 3636} 3637 3638/* ------------------------------------------------------------------------ */ 3639 3640/* 3641 * Seeking-mode support 3642 */ 3643 3644static int 3645archive_read_support_format_zip_capabilities_seekable(struct archive_read * a) 3646{ 3647 (void)a; /* UNUSED */ 3648 return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | 3649 ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA); 3650} 3651 3652/* 3653 * TODO: This is a performance sink because it forces the read core to 3654 * drop buffered data from the start of file, which will then have to 3655 * be re-read again if this bidder loses. 3656 * 3657 * We workaround this a little by passing in the best bid so far so 3658 * that later bidders can do nothing if they know they'll never 3659 * outbid. But we can certainly do better... 3660 */ 3661static int 3662read_eocd(struct zip *zip, const char *p, int64_t current_offset) 3663{ 3664 uint16_t disk_num; 3665 uint32_t cd_size, cd_offset; 3666 3667 disk_num = archive_le16dec(p + 4); 3668 cd_size = archive_le32dec(p + 12); 3669 cd_offset = archive_le32dec(p + 16); 3670 3671 /* Sanity-check the EOCD we've found. */ 3672 3673 /* This must be the first volume. */ 3674 if (disk_num != 0) 3675 return 0; 3676 /* Central directory must be on this volume. */ 3677 if (disk_num != archive_le16dec(p + 6)) 3678 return 0; 3679 /* All central directory entries must be on this volume. */ 3680 if (archive_le16dec(p + 10) != archive_le16dec(p + 8)) 3681 return 0; 3682 /* Central directory can't extend beyond start of EOCD record. */ 3683 if (cd_offset + cd_size > current_offset) 3684 return 0; 3685 3686 /* Save the central directory location for later use. */ 3687 zip->central_directory_offset = cd_offset; 3688 zip->central_directory_offset_adjusted = current_offset - cd_size; 3689 3690 /* This is just a tiny bit higher than the maximum 3691 returned by the streaming Zip bidder. This ensures 3692 that the more accurate seeking Zip parser wins 3693 whenever seek is available. */ 3694 return 32; 3695} 3696 3697/* 3698 * Examine Zip64 EOCD locator: If it's valid, store the information 3699 * from it. 3700 */ 3701static int 3702read_zip64_eocd(struct archive_read *a, struct zip *zip, const char *p) 3703{ 3704 int64_t eocd64_offset; 3705 int64_t eocd64_size; 3706 3707 /* Sanity-check the locator record. */ 3708 3709 /* Central dir must be on first volume. */ 3710 if (archive_le32dec(p + 4) != 0) 3711 return 0; 3712 /* Must be only a single volume. */ 3713 if (archive_le32dec(p + 16) != 1) 3714 return 0; 3715 3716 /* Find the Zip64 EOCD record. */ 3717 eocd64_offset = archive_le64dec(p + 8); 3718 if (__archive_read_seek(a, eocd64_offset, SEEK_SET) < 0) 3719 return 0; 3720 if ((p = __archive_read_ahead(a, 56, NULL)) == NULL) 3721 return 0; 3722 /* Make sure we can read all of it. */ 3723 eocd64_size = archive_le64dec(p + 4) + 12; 3724 if (eocd64_size < 56 || eocd64_size > 16384) 3725 return 0; 3726 if ((p = __archive_read_ahead(a, (size_t)eocd64_size, NULL)) == NULL) 3727 return 0; 3728 3729 /* Sanity-check the EOCD64 */ 3730 if (archive_le32dec(p + 16) != 0) /* Must be disk #0 */ 3731 return 0; 3732 if (archive_le32dec(p + 20) != 0) /* CD must be on disk #0 */ 3733 return 0; 3734 /* CD can't be split. */ 3735 if (archive_le64dec(p + 24) != archive_le64dec(p + 32)) 3736 return 0; 3737 3738 /* Save the central directory offset for later use. */ 3739 zip->central_directory_offset = archive_le64dec(p + 48); 3740 /* TODO: Needs scanning backwards to find the eocd64 instead of assuming */ 3741 zip->central_directory_offset_adjusted = zip->central_directory_offset; 3742 3743 return 32; 3744} 3745 3746static int 3747archive_read_format_zip_seekable_bid(struct archive_read *a, int best_bid) 3748{ 3749 struct zip *zip = (struct zip *)a->format->data; 3750 int64_t file_size, current_offset; 3751 const char *p; 3752 int i, tail; 3753 3754 /* If someone has already bid more than 32, then avoid 3755 trashing the look-ahead buffers with a seek. */ 3756 if (best_bid > 32) 3757 return (-1); 3758 3759 file_size = __archive_read_seek(a, 0, SEEK_END); 3760 if (file_size <= 0) 3761 return 0; 3762 3763 /* Search last 16k of file for end-of-central-directory 3764 * record (which starts with PK\005\006) */ 3765 tail = (int)zipmin(1024 * 16, file_size); 3766 current_offset = __archive_read_seek(a, -tail, SEEK_END); 3767 if (current_offset < 0) 3768 return 0; 3769 if ((p = __archive_read_ahead(a, (size_t)tail, NULL)) == NULL) 3770 return 0; 3771 /* Boyer-Moore search backwards from the end, since we want 3772 * to match the last EOCD in the file (there can be more than 3773 * one if there is an uncompressed Zip archive as a member 3774 * within this Zip archive). */ 3775 for (i = tail - 22; i > 0;) { 3776 switch (p[i]) { 3777 case 'P': 3778 if (memcmp(p + i, "PK\005\006", 4) == 0) { 3779 int ret = read_eocd(zip, p + i, 3780 current_offset + i); 3781 /* Zip64 EOCD locator precedes 3782 * regular EOCD if present. */ 3783 if (i >= 20 && memcmp(p + i - 20, "PK\006\007", 4) == 0) { 3784 int ret_zip64 = read_zip64_eocd(a, zip, p + i - 20); 3785 if (ret_zip64 > ret) 3786 ret = ret_zip64; 3787 } 3788 return (ret); 3789 } 3790 i -= 4; 3791 break; 3792 case 'K': i -= 1; break; 3793 case 005: i -= 2; break; 3794 case 006: i -= 3; break; 3795 default: i -= 4; break; 3796 } 3797 } 3798 return 0; 3799} 3800 3801/* The red-black trees are only used in seeking mode to manage 3802 * the in-memory copy of the central directory. */ 3803 3804static int 3805cmp_node(const struct archive_rb_node *n1, const struct archive_rb_node *n2) 3806{ 3807 const struct zip_entry *e1 = (const struct zip_entry *)n1; 3808 const struct zip_entry *e2 = (const struct zip_entry *)n2; 3809 3810 if (e1->local_header_offset > e2->local_header_offset) 3811 return -1; 3812 if (e1->local_header_offset < e2->local_header_offset) 3813 return 1; 3814 return 0; 3815} 3816 3817static int 3818cmp_key(const struct archive_rb_node *n, const void *key) 3819{ 3820 /* This function won't be called */ 3821 (void)n; /* UNUSED */ 3822 (void)key; /* UNUSED */ 3823 return 1; 3824} 3825 3826static const struct archive_rb_tree_ops rb_ops = { 3827 &cmp_node, &cmp_key 3828}; 3829 3830static int 3831rsrc_cmp_node(const struct archive_rb_node *n1, 3832 const struct archive_rb_node *n2) 3833{ 3834 const struct zip_entry *e1 = (const struct zip_entry *)n1; 3835 const struct zip_entry *e2 = (const struct zip_entry *)n2; 3836 3837 return (strcmp(e2->rsrcname.s, e1->rsrcname.s)); 3838} 3839 3840static int 3841rsrc_cmp_key(const struct archive_rb_node *n, const void *key) 3842{ 3843 const struct zip_entry *e = (const struct zip_entry *)n; 3844 return (strcmp((const char *)key, e->rsrcname.s)); 3845} 3846 3847static const struct archive_rb_tree_ops rb_rsrc_ops = { 3848 &rsrc_cmp_node, &rsrc_cmp_key 3849}; 3850 3851static const char * 3852rsrc_basename(const char *name, size_t name_length) 3853{ 3854 const char *s, *r; 3855 3856 r = s = name; 3857 for (;;) { 3858 s = memchr(s, '/', name_length - (s - name)); 3859 if (s == NULL) 3860 break; 3861 r = ++s; 3862 } 3863 return (r); 3864} 3865 3866static void 3867expose_parent_dirs(struct zip *zip, const char *name, size_t name_length) 3868{ 3869 struct archive_string str; 3870 struct zip_entry *dir; 3871 char *s; 3872 3873 archive_string_init(&str); 3874 archive_strncpy(&str, name, name_length); 3875 for (;;) { 3876 s = strrchr(str.s, '/'); 3877 if (s == NULL) 3878 break; 3879 *s = '\0'; 3880 /* Transfer the parent directory from zip->tree_rsrc RB 3881 * tree to zip->tree RB tree to expose. */ 3882 dir = (struct zip_entry *) 3883 __archive_rb_tree_find_node(&zip->tree_rsrc, str.s); 3884 if (dir == NULL) 3885 break; 3886 __archive_rb_tree_remove_node(&zip->tree_rsrc, &dir->node); 3887 archive_string_free(&dir->rsrcname); 3888 __archive_rb_tree_insert_node(&zip->tree, &dir->node); 3889 } 3890 archive_string_free(&str); 3891} 3892 3893static int 3894slurp_central_directory(struct archive_read *a, struct archive_entry* entry, 3895 struct zip *zip) 3896{ 3897 ssize_t i; 3898 unsigned found; 3899 int64_t correction; 3900 ssize_t bytes_avail; 3901 const char *p; 3902 3903 /* 3904 * Find the start of the central directory. The end-of-CD 3905 * record has our starting point, but there are lots of 3906 * Zip archives which have had other data prepended to the 3907 * file, which makes the recorded offsets all too small. 3908 * So we search forward from the specified offset until we 3909 * find the real start of the central directory. Then we 3910 * know the correction we need to apply to account for leading 3911 * padding. 3912 */ 3913 if (__archive_read_seek(a, zip->central_directory_offset_adjusted, SEEK_SET) 3914 < 0) 3915 return ARCHIVE_FATAL; 3916 3917 found = 0; 3918 while (!found) { 3919 if ((p = __archive_read_ahead(a, 20, &bytes_avail)) == NULL) 3920 return ARCHIVE_FATAL; 3921 for (found = 0, i = 0; !found && i < bytes_avail - 4;) { 3922 switch (p[i + 3]) { 3923 case 'P': i += 3; break; 3924 case 'K': i += 2; break; 3925 case 001: i += 1; break; 3926 case 002: 3927 if (memcmp(p + i, "PK\001\002", 4) == 0) { 3928 p += i; 3929 found = 1; 3930 } else 3931 i += 4; 3932 break; 3933 case 005: i += 1; break; 3934 case 006: 3935 if (memcmp(p + i, "PK\005\006", 4) == 0) { 3936 p += i; 3937 found = 1; 3938 } else if (memcmp(p + i, "PK\006\006", 4) == 0) { 3939 p += i; 3940 found = 1; 3941 } else 3942 i += 1; 3943 break; 3944 default: i += 4; break; 3945 } 3946 } 3947 __archive_read_consume(a, i); 3948 } 3949 correction = archive_filter_bytes(&a->archive, 0) 3950 - zip->central_directory_offset; 3951 3952 __archive_rb_tree_init(&zip->tree, &rb_ops); 3953 __archive_rb_tree_init(&zip->tree_rsrc, &rb_rsrc_ops); 3954 3955 zip->central_directory_entries_total = 0; 3956 while (1) { 3957 struct zip_entry *zip_entry; 3958 size_t filename_length, extra_length, comment_length; 3959 uint32_t external_attributes; 3960 const char *name, *r; 3961 3962 if ((p = __archive_read_ahead(a, 4, NULL)) == NULL) 3963 return ARCHIVE_FATAL; 3964 if (memcmp(p, "PK\006\006", 4) == 0 3965 || memcmp(p, "PK\005\006", 4) == 0) { 3966 break; 3967 } else if (memcmp(p, "PK\001\002", 4) != 0) { 3968 archive_set_error(&a->archive, 3969 -1, "Invalid central directory signature"); 3970 return ARCHIVE_FATAL; 3971 } 3972 if ((p = __archive_read_ahead(a, 46, NULL)) == NULL) 3973 return ARCHIVE_FATAL; 3974 3975 zip_entry = calloc(1, sizeof(struct zip_entry)); 3976 if (zip_entry == NULL) { 3977 archive_set_error(&a->archive, ENOMEM, 3978 "Can't allocate zip entry"); 3979 return ARCHIVE_FATAL; 3980 } 3981 zip_entry->next = zip->zip_entries; 3982 zip_entry->flags |= LA_FROM_CENTRAL_DIRECTORY; 3983 zip->zip_entries = zip_entry; 3984 zip->central_directory_entries_total++; 3985 3986 /* version = p[4]; */ 3987 zip_entry->system = p[5]; 3988 /* version_required = archive_le16dec(p + 6); */ 3989 zip_entry->zip_flags = archive_le16dec(p + 8); 3990 if (zip_entry->zip_flags 3991 & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)){ 3992 zip->has_encrypted_entries = 1; 3993 } 3994 zip_entry->compression = (char)archive_le16dec(p + 10); 3995 zip_entry->mtime = zip_time(p + 12); 3996 zip_entry->crc32 = archive_le32dec(p + 16); 3997 if (zip_entry->zip_flags & ZIP_LENGTH_AT_END) 3998 zip_entry->decdat = p[13]; 3999 else 4000 zip_entry->decdat = p[19]; 4001 zip_entry->compressed_size = archive_le32dec(p + 20); 4002 zip_entry->uncompressed_size = archive_le32dec(p + 24); 4003 filename_length = archive_le16dec(p + 28); 4004 extra_length = archive_le16dec(p + 30); 4005 comment_length = archive_le16dec(p + 32); 4006 /* disk_start = archive_le16dec(p + 34); 4007 * Better be zero. 4008 * internal_attributes = archive_le16dec(p + 36); 4009 * text bit */ 4010 external_attributes = archive_le32dec(p + 38); 4011 zip_entry->local_header_offset = 4012 archive_le32dec(p + 42) + correction; 4013 4014 /* If we can't guess the mode, leave it zero here; 4015 when we read the local file header we might get 4016 more information. */ 4017 if (zip_entry->system == 3) { 4018 zip_entry->mode = external_attributes >> 16; 4019 } else if (zip_entry->system == 0) { 4020 // Interpret MSDOS directory bit 4021 if (0x10 == (external_attributes & 0x10)) { 4022 zip_entry->mode = AE_IFDIR | 0775; 4023 } else { 4024 zip_entry->mode = AE_IFREG | 0664; 4025 } 4026 if (0x01 == (external_attributes & 0x01)) { 4027 // Read-only bit; strip write permissions 4028 zip_entry->mode &= 0555; 4029 } 4030 } else { 4031 zip_entry->mode = 0; 4032 } 4033 4034 /* We're done with the regular data; get the filename and 4035 * extra data. */ 4036 __archive_read_consume(a, 46); 4037 p = __archive_read_ahead(a, filename_length + extra_length, 4038 NULL); 4039 if (p == NULL) { 4040 archive_set_error(&a->archive, 4041 ARCHIVE_ERRNO_FILE_FORMAT, 4042 "Truncated ZIP file header"); 4043 return ARCHIVE_FATAL; 4044 } 4045 if (ARCHIVE_OK != process_extra(a, entry, p + filename_length, 4046 extra_length, zip_entry)) { 4047 return ARCHIVE_FATAL; 4048 } 4049 4050 /* 4051 * Mac resource fork files are stored under the 4052 * "__MACOSX/" directory, so we should check if 4053 * it is. 4054 */ 4055 if (!zip->process_mac_extensions) { 4056 /* Treat every entry as a regular entry. */ 4057 __archive_rb_tree_insert_node(&zip->tree, 4058 &zip_entry->node); 4059 } else { 4060 name = p; 4061 r = rsrc_basename(name, filename_length); 4062 if (filename_length >= 9 && 4063 strncmp("__MACOSX/", name, 9) == 0) { 4064 /* If this file is not a resource fork nor 4065 * a directory. We should treat it as a non 4066 * resource fork file to expose it. */ 4067 if (name[filename_length-1] != '/' && 4068 (r - name < 3 || r[0] != '.' || 4069 r[1] != '_')) { 4070 __archive_rb_tree_insert_node( 4071 &zip->tree, &zip_entry->node); 4072 /* Expose its parent directories. */ 4073 expose_parent_dirs(zip, name, 4074 filename_length); 4075 } else { 4076 /* This file is a resource fork file or 4077 * a directory. */ 4078 archive_strncpy(&(zip_entry->rsrcname), 4079 name, filename_length); 4080 __archive_rb_tree_insert_node( 4081 &zip->tree_rsrc, &zip_entry->node); 4082 } 4083 } else { 4084 /* Generate resource fork name to find its 4085 * resource file at zip->tree_rsrc. */ 4086 4087 /* If this is an entry ending with slash, 4088 * make the resource for name slash-less 4089 * as the actual resource fork doesn't end with '/'. 4090 */ 4091 size_t tmp_length = filename_length; 4092 if (tmp_length > 0 && name[tmp_length - 1] == '/') { 4093 tmp_length--; 4094 r = rsrc_basename(name, tmp_length); 4095 } 4096 4097 archive_strcpy(&(zip_entry->rsrcname), 4098 "__MACOSX/"); 4099 archive_strncat(&(zip_entry->rsrcname), 4100 name, r - name); 4101 archive_strcat(&(zip_entry->rsrcname), "._"); 4102 archive_strncat(&(zip_entry->rsrcname), 4103 name + (r - name), 4104 tmp_length - (r - name)); 4105 /* Register an entry to RB tree to sort it by 4106 * file offset. */ 4107 __archive_rb_tree_insert_node(&zip->tree, 4108 &zip_entry->node); 4109 } 4110 } 4111 4112 /* Skip the comment too ... */ 4113 __archive_read_consume(a, 4114 filename_length + extra_length + comment_length); 4115 } 4116 4117 return ARCHIVE_OK; 4118} 4119 4120static ssize_t 4121zip_get_local_file_header_size(struct archive_read *a, size_t extra) 4122{ 4123 const char *p; 4124 ssize_t filename_length, extra_length; 4125 4126 if ((p = __archive_read_ahead(a, extra + 30, NULL)) == NULL) { 4127 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 4128 "Truncated ZIP file header"); 4129 return (ARCHIVE_WARN); 4130 } 4131 p += extra; 4132 4133 if (memcmp(p, "PK\003\004", 4) != 0) { 4134 archive_set_error(&a->archive, -1, "Damaged Zip archive"); 4135 return ARCHIVE_WARN; 4136 } 4137 filename_length = archive_le16dec(p + 26); 4138 extra_length = archive_le16dec(p + 28); 4139 4140 return (30 + filename_length + extra_length); 4141} 4142 4143static int 4144zip_read_mac_metadata(struct archive_read *a, struct archive_entry *entry, 4145 struct zip_entry *rsrc) 4146{ 4147 struct zip *zip = (struct zip *)a->format->data; 4148 unsigned char *metadata, *mp; 4149 int64_t offset = archive_filter_bytes(&a->archive, 0); 4150 size_t remaining_bytes, metadata_bytes; 4151 ssize_t hsize; 4152 int ret = ARCHIVE_OK, eof; 4153 4154 switch(rsrc->compression) { 4155 case 0: /* No compression. */ 4156 if (rsrc->uncompressed_size != rsrc->compressed_size) { 4157 archive_set_error(&a->archive, 4158 ARCHIVE_ERRNO_FILE_FORMAT, 4159 "Malformed OS X metadata entry: " 4160 "inconsistent size"); 4161 return (ARCHIVE_FATAL); 4162 } 4163#ifdef HAVE_ZLIB_H 4164 case 8: /* Deflate compression. */ 4165#endif 4166 break; 4167 default: /* Unsupported compression. */ 4168 /* Return a warning. */ 4169 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 4170 "Unsupported ZIP compression method (%s)", 4171 compression_name(rsrc->compression)); 4172 /* We can't decompress this entry, but we will 4173 * be able to skip() it and try the next entry. */ 4174 return (ARCHIVE_WARN); 4175 } 4176 4177 if (rsrc->uncompressed_size > (4 * 1024 * 1024)) { 4178 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 4179 "Mac metadata is too large: %jd > 4M bytes", 4180 (intmax_t)rsrc->uncompressed_size); 4181 return (ARCHIVE_WARN); 4182 } 4183 if (rsrc->compressed_size > (4 * 1024 * 1024)) { 4184 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 4185 "Mac metadata is too large: %jd > 4M bytes", 4186 (intmax_t)rsrc->compressed_size); 4187 return (ARCHIVE_WARN); 4188 } 4189 4190 metadata = malloc((size_t)rsrc->uncompressed_size); 4191 if (metadata == NULL) { 4192 archive_set_error(&a->archive, ENOMEM, 4193 "Can't allocate memory for Mac metadata"); 4194 return (ARCHIVE_FATAL); 4195 } 4196 4197 if (offset < rsrc->local_header_offset) 4198 __archive_read_consume(a, rsrc->local_header_offset - offset); 4199 else if (offset != rsrc->local_header_offset) { 4200 __archive_read_seek(a, rsrc->local_header_offset, SEEK_SET); 4201 } 4202 4203 hsize = zip_get_local_file_header_size(a, 0); 4204 __archive_read_consume(a, hsize); 4205 4206 remaining_bytes = (size_t)rsrc->compressed_size; 4207 metadata_bytes = (size_t)rsrc->uncompressed_size; 4208 mp = metadata; 4209 eof = 0; 4210 while (!eof && remaining_bytes) { 4211 const unsigned char *p; 4212 ssize_t bytes_avail; 4213 size_t bytes_used; 4214 4215 p = __archive_read_ahead(a, 1, &bytes_avail); 4216 if (p == NULL) { 4217 archive_set_error(&a->archive, 4218 ARCHIVE_ERRNO_FILE_FORMAT, 4219 "Truncated ZIP file header"); 4220 ret = ARCHIVE_WARN; 4221 goto exit_mac_metadata; 4222 } 4223 if ((size_t)bytes_avail > remaining_bytes) 4224 bytes_avail = remaining_bytes; 4225 switch(rsrc->compression) { 4226 case 0: /* No compression. */ 4227 if ((size_t)bytes_avail > metadata_bytes) 4228 bytes_avail = metadata_bytes; 4229 memcpy(mp, p, bytes_avail); 4230 bytes_used = (size_t)bytes_avail; 4231 metadata_bytes -= bytes_used; 4232 mp += bytes_used; 4233 if (metadata_bytes == 0) 4234 eof = 1; 4235 break; 4236#ifdef HAVE_ZLIB_H 4237 case 8: /* Deflate compression. */ 4238 { 4239 int r; 4240 4241 ret = zip_deflate_init(a, zip); 4242 if (ret != ARCHIVE_OK) 4243 goto exit_mac_metadata; 4244 zip->stream.next_in = 4245 (Bytef *)(uintptr_t)(const void *)p; 4246 zip->stream.avail_in = (uInt)bytes_avail; 4247 zip->stream.total_in = 0; 4248 zip->stream.next_out = mp; 4249 zip->stream.avail_out = (uInt)metadata_bytes; 4250 zip->stream.total_out = 0; 4251 4252 r = inflate(&zip->stream, 0); 4253 switch (r) { 4254 case Z_OK: 4255 break; 4256 case Z_STREAM_END: 4257 eof = 1; 4258 break; 4259 case Z_MEM_ERROR: 4260 archive_set_error(&a->archive, ENOMEM, 4261 "Out of memory for ZIP decompression"); 4262 ret = ARCHIVE_FATAL; 4263 goto exit_mac_metadata; 4264 default: 4265 archive_set_error(&a->archive, 4266 ARCHIVE_ERRNO_MISC, 4267 "ZIP decompression failed (%d)", r); 4268 ret = ARCHIVE_FATAL; 4269 goto exit_mac_metadata; 4270 } 4271 bytes_used = zip->stream.total_in; 4272 metadata_bytes -= zip->stream.total_out; 4273 mp += zip->stream.total_out; 4274 break; 4275 } 4276#endif 4277 default: 4278 bytes_used = 0; 4279 break; 4280 } 4281 __archive_read_consume(a, bytes_used); 4282 remaining_bytes -= bytes_used; 4283 } 4284 archive_entry_copy_mac_metadata(entry, metadata, 4285 (size_t)rsrc->uncompressed_size - metadata_bytes); 4286 4287exit_mac_metadata: 4288 __archive_read_seek(a, offset, SEEK_SET); 4289 zip->decompress_init = 0; 4290 free(metadata); 4291 return (ret); 4292} 4293 4294static int 4295archive_read_format_zip_seekable_read_header(struct archive_read *a, 4296 struct archive_entry *entry) 4297{ 4298 struct zip *zip = (struct zip *)a->format->data; 4299 struct zip_entry *rsrc; 4300 int64_t offset; 4301 int r, ret = ARCHIVE_OK; 4302 4303 /* 4304 * It should be sufficient to call archive_read_next_header() for 4305 * a reader to determine if an entry is encrypted or not. If the 4306 * encryption of an entry is only detectable when calling 4307 * archive_read_data(), so be it. We'll do the same check there 4308 * as well. 4309 */ 4310 if (zip->has_encrypted_entries == 4311 ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) 4312 zip->has_encrypted_entries = 0; 4313 4314 a->archive.archive_format = ARCHIVE_FORMAT_ZIP; 4315 if (a->archive.archive_format_name == NULL) 4316 a->archive.archive_format_name = "ZIP"; 4317 4318 if (zip->zip_entries == NULL) { 4319 r = slurp_central_directory(a, entry, zip); 4320 if (r != ARCHIVE_OK) 4321 return r; 4322 /* Get first entry whose local header offset is lower than 4323 * other entries in the archive file. */ 4324 zip->entry = 4325 (struct zip_entry *)ARCHIVE_RB_TREE_MIN(&zip->tree); 4326 } else if (zip->entry != NULL) { 4327 /* Get next entry in local header offset order. */ 4328 zip->entry = (struct zip_entry *)__archive_rb_tree_iterate( 4329 &zip->tree, &zip->entry->node, ARCHIVE_RB_DIR_RIGHT); 4330 } 4331 4332 if (zip->entry == NULL) 4333 return ARCHIVE_EOF; 4334 4335 if (zip->entry->rsrcname.s) 4336 rsrc = (struct zip_entry *)__archive_rb_tree_find_node( 4337 &zip->tree_rsrc, zip->entry->rsrcname.s); 4338 else 4339 rsrc = NULL; 4340 4341 if (zip->cctx_valid) 4342 archive_decrypto_aes_ctr_release(&zip->cctx); 4343 if (zip->hctx_valid) 4344 archive_hmac_sha1_cleanup(&zip->hctx); 4345 zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0; 4346 __archive_read_reset_passphrase(a); 4347 4348 /* File entries are sorted by the header offset, we should mostly 4349 * use __archive_read_consume to advance a read point to avoid 4350 * redundant data reading. */ 4351 offset = archive_filter_bytes(&a->archive, 0); 4352 if (offset < zip->entry->local_header_offset) 4353 __archive_read_consume(a, 4354 zip->entry->local_header_offset - offset); 4355 else if (offset != zip->entry->local_header_offset) { 4356 __archive_read_seek(a, zip->entry->local_header_offset, 4357 SEEK_SET); 4358 } 4359 zip->unconsumed = 0; 4360 r = zip_read_local_file_header(a, entry, zip); 4361 if (r != ARCHIVE_OK) 4362 return r; 4363 if (rsrc) { 4364 int ret2 = zip_read_mac_metadata(a, entry, rsrc); 4365 if (ret2 < ret) 4366 ret = ret2; 4367 } 4368 return (ret); 4369} 4370 4371/* 4372 * We're going to seek for the next header anyway, so we don't 4373 * need to bother doing anything here. 4374 */ 4375static int 4376archive_read_format_zip_read_data_skip_seekable(struct archive_read *a) 4377{ 4378 struct zip *zip; 4379 zip = (struct zip *)(a->format->data); 4380 4381 zip->unconsumed = 0; 4382 return (ARCHIVE_OK); 4383} 4384 4385int 4386archive_read_support_format_zip_seekable(struct archive *_a) 4387{ 4388 struct archive_read *a = (struct archive_read *)_a; 4389 struct zip *zip; 4390 int r; 4391 4392 archive_check_magic(_a, ARCHIVE_READ_MAGIC, 4393 ARCHIVE_STATE_NEW, "archive_read_support_format_zip_seekable"); 4394 4395 zip = (struct zip *)calloc(1, sizeof(*zip)); 4396 if (zip == NULL) { 4397 archive_set_error(&a->archive, ENOMEM, 4398 "Can't allocate zip data"); 4399 return (ARCHIVE_FATAL); 4400 } 4401 4402#ifdef HAVE_COPYFILE_H 4403 /* Set this by default on Mac OS. */ 4404 zip->process_mac_extensions = 1; 4405#endif 4406 4407 /* 4408 * Until enough data has been read, we cannot tell about 4409 * any encrypted entries yet. 4410 */ 4411 zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW; 4412 zip->crc32func = real_crc32; 4413 4414 r = __archive_read_register_format(a, 4415 zip, 4416 "zip", 4417 archive_read_format_zip_seekable_bid, 4418 archive_read_format_zip_options, 4419 archive_read_format_zip_seekable_read_header, 4420 archive_read_format_zip_read_data, 4421 archive_read_format_zip_read_data_skip_seekable, 4422 NULL, 4423 archive_read_format_zip_cleanup, 4424 archive_read_support_format_zip_capabilities_seekable, 4425 archive_read_format_zip_has_encrypted_entries); 4426 4427 if (r != ARCHIVE_OK) 4428 free(zip); 4429 return (ARCHIVE_OK); 4430} 4431 4432/*# vim:set noet:*/ 4433