2 * Copyright (c) 2004-2013 Tim Kientzle
3 * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
4 * Copyright (c) 2013 Konrad Kleine
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD$");
32 * The definitive documentation of the Zip file format is:
33 * http://www.pkware.com/documents/casestudies/APPNOTE.TXT
35 * The Info-Zip project has pioneered various extensions to better
36 * support Zip on Unix, including the 0x5455 "UT", 0x5855 "UX", 0x7855
37 * "Ux", and 0x7875 "ux" extensions for time and ownership
40 * History of this code: The streaming Zip reader was first added to
41 * libarchive in January 2005. Support for seekable input sources was
42 * added in Nov 2011. Zip64 support (including a significant code
43 * refactoring) was added in 2014.
57 #include "archive_digest_private.h"
58 #include "archive_cryptor_private.h"
59 #include "archive_endian.h"
60 #include "archive_entry.h"
61 #include "archive_entry_locale.h"
62 #include "archive_hmac_private.h"
63 #include "archive_private.h"
64 #include "archive_rb.h"
65 #include "archive_read_private.h"
68 #include "archive_crc32.h"
72 struct archive_rb_node node;
73 struct zip_entry *next;
74 int64_t local_header_offset;
75 int64_t compressed_size;
76 int64_t uncompressed_size;
79 struct archive_string rsrcname;
85 uint16_t zip_flags; /* From GP Flags Field */
86 unsigned char compression;
87 unsigned char system; /* From "version written by" */
88 unsigned char flags; /* Our extra markers. */
89 unsigned char decdat;/* Used for Decryption check */
91 /* WinZip AES encryption extra field should be available
92 * when compression is 99. */
94 /* Vendor version: AE-1 - 0x0001, AE-2 - 0x0002 */
96 #define AES_VENDOR_AE_1 0x0001
97 #define AES_VENDOR_AE_2 0x0002
98 /* AES encryption strength:
99 * 1 - 128 bits, 2 - 192 bits, 2 - 256 bits. */
101 /* Actual compression method. */
102 unsigned char compression;
106 struct trad_enc_ctx {
110 /* Bits used in zip_flags. */
111 #define ZIP_ENCRYPTED (1 << 0)
112 #define ZIP_LENGTH_AT_END (1 << 3)
113 #define ZIP_STRONG_ENCRYPTED (1 << 6)
114 #define ZIP_UTF8_NAME (1 << 11)
115 /* See "7.2 Single Password Symmetric Encryption Method"
116 in http://www.pkware.com/documents/casestudies/APPNOTE.TXT */
117 #define ZIP_CENTRAL_DIRECTORY_ENCRYPTED (1 << 13)
119 /* Bits used in flags. */
120 #define LA_USED_ZIP64 (1 << 0)
121 #define LA_FROM_CENTRAL_DIRECTORY (1 << 1)
124 * See "WinZip - AES Encryption Information"
125 * http://www.winzip.com/aes_info.htm
127 /* Value used in compression method. */
128 #define WINZIP_AES_ENCRYPTION 99
129 /* Authentication code size. */
130 #define AUTH_CODE_SIZE 10
132 #define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2)
135 /* Structural information about the archive. */
136 struct archive_string format_name;
137 int64_t central_directory_offset;
138 size_t central_directory_entries_total;
139 size_t central_directory_entries_on_this_disk;
140 int has_encrypted_entries;
142 /* List of entries (seekable Zip only) */
143 struct zip_entry *zip_entries;
144 struct archive_rb_tree tree;
145 struct archive_rb_tree tree_rsrc;
147 /* Bytes read but not yet consumed via __archive_read_consume() */
150 /* Information about entry we're currently reading. */
151 struct zip_entry *entry;
152 int64_t entry_bytes_remaining;
154 /* These count the number of bytes actually read for the entry. */
155 int64_t entry_compressed_bytes_read;
156 int64_t entry_uncompressed_bytes_read;
158 /* Running CRC32 of the decompressed data */
159 unsigned long entry_crc32;
160 unsigned long (*crc32func)(unsigned long, const void *,
164 /* Flags to mark progress of decompression. */
165 char decompress_init;
169 unsigned char *uncompressed_buffer;
170 size_t uncompressed_buffer_size;
175 struct archive_string_conv *sconv;
176 struct archive_string_conv *sconv_default;
177 struct archive_string_conv *sconv_utf8;
178 int init_default_conversion;
179 int process_mac_extensions;
181 char init_decryption;
183 /* Decryption buffer. */
185 * The decrypted data starts at decrypted_ptr and
186 * extends for decrypted_bytes_remaining. Decryption
187 * adds new data to the end of this block, data is returned
188 * to clients from the beginning. When the block hits the
189 * end of decrypted_buffer, it has to be shuffled back to
190 * the beginning of the buffer.
192 unsigned char *decrypted_buffer;
193 unsigned char *decrypted_ptr;
194 size_t decrypted_buffer_size;
195 size_t decrypted_bytes_remaining;
196 size_t decrypted_unconsumed_bytes;
198 /* Traditional PKWARE decryption. */
199 struct trad_enc_ctx tctx;
202 /* WinZip AES decryption. */
203 /* Contexts used for AES decryption. */
204 archive_crypto_ctx cctx;
206 archive_hmac_sha1_ctx hctx;
209 /* Strong encryption's decryption header information. */
222 /* Many systems define min or MIN, but not all. */
223 #define zipmin(a,b) ((a) < (b) ? (a) : (b))
225 /* ------------------------------------------------------------------------ */
228 Traditional PKWARE Decryption functions.
232 trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
235 #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
237 ctx->keys[0] = CRC32(ctx->keys[0], c);
238 ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
239 t = (ctx->keys[1] >> 24) & 0xff;
240 ctx->keys[2] = CRC32(ctx->keys[2], t);
245 trad_enc_decrypt_byte(struct trad_enc_ctx *ctx)
247 unsigned temp = ctx->keys[2] | 2;
248 return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
252 trad_enc_decrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
253 size_t in_len, uint8_t *out, size_t out_len)
257 max = (unsigned)((in_len < out_len)? in_len: out_len);
259 for (i = 0; i < max; i++) {
260 uint8_t t = in[i] ^ trad_enc_decrypt_byte(ctx);
262 trad_enc_update_keys(ctx, t);
267 trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len,
268 const uint8_t *key, size_t key_len, uint8_t *crcchk)
277 ctx->keys[0] = 305419896L;
278 ctx->keys[1] = 591751049L;
279 ctx->keys[2] = 878082192L;
281 for (;pw_len; --pw_len)
282 trad_enc_update_keys(ctx, *pw++);
284 trad_enc_decrypt_update(ctx, key, 12, header, 12);
285 /* Return the last byte for CRC check. */
286 *crcchk = header[11];
292 crypt_derive_key_sha1(const void *p, int size, unsigned char *key,
296 archive_sha1_ctx ctx;
297 unsigned char md1[MD_SIZE];
298 unsigned char md2[MD_SIZE * 2];
299 unsigned char mkb[64];
302 archive_sha1_init(&ctx);
303 archive_sha1_update(&ctx, p, size);
304 archive_sha1_final(&ctx, md1);
306 memset(mkb, 0x36, sizeof(mkb));
307 for (i = 0; i < MD_SIZE; i++)
309 archive_sha1_init(&ctx);
310 archive_sha1_update(&ctx, mkb, sizeof(mkb));
311 archive_sha1_final(&ctx, md2);
313 memset(mkb, 0x5C, sizeof(mkb));
314 for (i = 0; i < MD_SIZE; i++)
316 archive_sha1_init(&ctx);
317 archive_sha1_update(&ctx, mkb, sizeof(mkb));
318 archive_sha1_final(&ctx, md2 + MD_SIZE);
322 memcpy(key, md2, key_size);
328 * Common code for streaming or seeking modes.
330 * Includes code to read local file headers, decompress data
331 * from entry bodies, and common API.
335 real_crc32(unsigned long crc, const void *buff, size_t len)
337 return crc32(crc, buff, (unsigned int)len);
340 /* Used by "ignorecrc32" option to speed up tests. */
342 fake_crc32(unsigned long crc, const void *buff, size_t len)
344 (void)crc; /* UNUSED */
345 (void)buff; /* UNUSED */
346 (void)len; /* UNUSED */
353 } compression_methods[] = {
354 {0, "uncompressed"}, /* The file is stored (no compression) */
355 {1, "shrinking"}, /* The file is Shrunk */
356 {2, "reduced-1"}, /* The file is Reduced with compression factor 1 */
357 {3, "reduced-2"}, /* The file is Reduced with compression factor 2 */
358 {4, "reduced-3"}, /* The file is Reduced with compression factor 3 */
359 {5, "reduced-4"}, /* The file is Reduced with compression factor 4 */
360 {6, "imploded"}, /* The file is Imploded */
361 {7, "reserved"}, /* Reserved for Tokenizing compression algorithm */
362 {8, "deflation"}, /* The file is Deflated */
363 {9, "deflation-64-bit"}, /* Enhanced Deflating using Deflate64(tm) */
364 {10, "ibm-terse"},/* PKWARE Data Compression Library Imploding
366 {11, "reserved"}, /* Reserved by PKWARE */
367 {12, "bzip"}, /* File is compressed using BZIP2 algorithm */
368 {13, "reserved"}, /* Reserved by PKWARE */
369 {14, "lzma"}, /* LZMA (EFS) */
370 {15, "reserved"}, /* Reserved by PKWARE */
371 {16, "reserved"}, /* Reserved by PKWARE */
372 {17, "reserved"}, /* Reserved by PKWARE */
373 {18, "ibm-terse-new"}, /* File is compressed using IBM TERSE (new) */
374 {19, "ibm-lz777"},/* IBM LZ77 z Architecture (PFS) */
375 {97, "wav-pack"}, /* WavPack compressed data */
376 {98, "ppmd-1"}, /* PPMd version I, Rev 1 */
377 {99, "aes"} /* WinZip AES encryption */
381 compression_name(const int compression)
383 static const int num_compression_methods =
384 sizeof(compression_methods)/sizeof(compression_methods[0]);
387 while(compression >= 0 && i < num_compression_methods) {
388 if (compression_methods[i].id == compression)
389 return compression_methods[i].name;
395 /* Convert an MSDOS-style date/time into Unix-style time. */
397 zip_time(const char *p)
402 msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
403 msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
405 memset(&ts, 0, sizeof(ts));
406 ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
407 ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
408 ts.tm_mday = msDate & 0x1f; /* Day of month. */
409 ts.tm_hour = (msTime >> 11) & 0x1f;
410 ts.tm_min = (msTime >> 5) & 0x3f;
411 ts.tm_sec = (msTime << 1) & 0x3e;
417 * The extra data is stored as a list of
418 * id1+size1+data1 + id2+size2+data2 ...
419 * triplets. id and size are 2 bytes each.
422 process_extra(struct archive_read *a, const char *p, size_t extra_length, struct zip_entry* zip_entry)
426 if (extra_length == 0) {
430 if (extra_length < 4) {
431 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
432 "Too-small extra data: Need at least 4 bytes, but only found %d bytes", (int)extra_length);
433 return ARCHIVE_FAILED;
435 while (offset <= extra_length - 4) {
436 unsigned short headerid = archive_le16dec(p + offset);
437 unsigned short datasize = archive_le16dec(p + offset + 2);
440 if (offset + datasize > extra_length) {
441 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
442 "Extra data overflow: Need %d bytes but only found %d bytes",
443 (int)datasize, (int)(extra_length - offset));
444 return ARCHIVE_FAILED;
447 fprintf(stderr, "Header id 0x%04x, length %d\n",
452 /* Zip64 extended information extra field. */
453 zip_entry->flags |= LA_USED_ZIP64;
454 if (zip_entry->uncompressed_size == 0xffffffff) {
457 zip_entry->uncompressed_size =
458 archive_le64dec(p + offset);
462 if (zip_entry->compressed_size == 0xffffffff) {
465 zip_entry->compressed_size =
466 archive_le64dec(p + offset);
470 if (zip_entry->local_header_offset == 0xffffffff) {
473 zip_entry->local_header_offset =
474 archive_le64dec(p + offset);
478 /* archive_le32dec(p + offset) gives disk
479 * on which file starts, but we don't handle
480 * multi-volume Zip files. */
485 /* Strong encryption field. */
486 if (archive_le16dec(p + offset) == 2) {
488 archive_le16dec(p + offset + 2);
490 archive_le16dec(p + offset + 4);
492 archive_le16dec(p + offset + 6);
493 fprintf(stderr, "algId=0x%04x, bitLen=%u, "
494 "flgas=%d\n", algId, bitLen,flags);
501 /* Extended time field "UT". */
502 int flags = p[offset];
505 /* Flag bits indicate which dates are present. */
509 fprintf(stderr, "mtime: %lld -> %d\n",
510 (long long)zip_entry->mtime,
511 archive_le32dec(p + offset));
515 zip_entry->mtime = archive_le32dec(p + offset);
523 zip_entry->atime = archive_le32dec(p + offset);
531 zip_entry->ctime = archive_le32dec(p + offset);
539 /* Info-ZIP Unix Extra Field (old version) "UX". */
541 zip_entry->atime = archive_le32dec(p + offset);
543 archive_le32dec(p + offset + 4);
545 if (datasize >= 12) {
547 archive_le16dec(p + offset + 8);
549 archive_le16dec(p + offset + 10);
555 /* Experimental 'xl' field */
557 * Introduced Dec 2013 to provide a way to
558 * include external file attributes (and other
559 * fields that ordinarily appear only in
560 * central directory) in local file header.
561 * This provides file type and permission
562 * information necessary to support full
563 * streaming extraction. Currently being
564 * discussed with other Zip developers
565 * ... subject to change.
568 * The field starts with a bitmap that specifies
569 * which additional fields are included. The
570 * bitmap is variable length and can be extended in
573 * n bytes - feature bitmap: first byte has low-order
574 * 7 bits. If high-order bit is set, a subsequent
575 * byte holds the next 7 bits, etc.
577 * if bitmap & 1, 2 byte "version made by"
578 * if bitmap & 2, 2 byte "internal file attributes"
579 * if bitmap & 4, 4 byte "external file attributes"
580 * if bitmap & 8, 2 byte comment length + n byte comment
582 int bitmap, bitmap_last;
586 bitmap_last = bitmap = 0xff & p[offset];
590 /* We only support first 7 bits of bitmap; skip rest. */
591 while ((bitmap_last & 0x80) != 0
593 bitmap_last = p[offset];
599 /* 2 byte "version made by" */
603 = archive_le16dec(p + offset) >> 8;
608 /* 2 byte "internal file attributes" */
609 uint32_t internal_attributes;
613 = archive_le16dec(p + offset);
614 /* Not used by libarchive at present. */
615 (void)internal_attributes; /* UNUSED */
620 /* 4 byte "external file attributes" */
621 uint32_t external_attributes;
625 = archive_le32dec(p + offset);
626 if (zip_entry->system == 3) {
628 = external_attributes >> 16;
629 } else if (zip_entry->system == 0) {
630 // Interpret MSDOS directory bit
631 if (0x10 == (external_attributes & 0x10)) {
632 zip_entry->mode = AE_IFDIR | 0775;
634 zip_entry->mode = AE_IFREG | 0664;
636 if (0x01 == (external_attributes & 0x01)) {
637 // Read-only bit; strip write permissions
638 zip_entry->mode &= 0555;
647 /* 2 byte comment length + comment */
648 uint32_t comment_length;
652 = archive_le16dec(p + offset);
656 if (datasize < comment_length)
658 /* Comment is not supported by libarchive */
659 offset += comment_length;
660 datasize -= comment_length;
665 /* Info-ZIP Unix Extra Field (type 2) "Ux". */
667 fprintf(stderr, "uid %d gid %d\n",
668 archive_le16dec(p + offset),
669 archive_le16dec(p + offset + 2));
672 zip_entry->uid = archive_le16dec(p + offset);
675 archive_le16dec(p + offset + 2);
679 /* Info-Zip Unix Extra Field (type 3) "ux". */
680 int uidsize = 0, gidsize = 0;
682 /* TODO: support arbitrary uidsize/gidsize. */
683 if (datasize >= 1 && p[offset] == 1) {/* version=1 */
685 /* get a uid size. */
686 uidsize = 0xff & (int)p[offset+1];
691 else if (uidsize == 4 && datasize >= 6)
696 if (datasize >= (2 + uidsize + 3)) {
697 /* get a gid size. */
698 gidsize = 0xff & (int)p[offset+2+uidsize];
702 p+offset+2+uidsize+1);
703 else if (gidsize == 4 &&
704 datasize >= (2 + uidsize + 5))
707 p+offset+2+uidsize+1);
713 /* WinZip AES extra data field. */
714 if (p[offset + 2] == 'A' && p[offset + 3] == 'E') {
715 /* Vendor version. */
716 zip_entry->aes_extra.vendor =
717 archive_le16dec(p + offset);
718 /* AES encryption strength. */
719 zip_entry->aes_extra.strength = p[offset + 4];
720 /* Actual compression method. */
721 zip_entry->aes_extra.compression =
730 if (offset != extra_length) {
731 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
732 "Malformed extra data: Consumed %d bytes of %d bytes",
733 (int)offset, (int)extra_length);
734 return ARCHIVE_FAILED;
740 * Assumes file pointer is at beginning of local file header.
743 zip_read_local_file_header(struct archive_read *a, struct archive_entry *entry,
750 size_t len, filename_length, extra_length;
751 struct archive_string_conv *sconv;
752 struct zip_entry *zip_entry = zip->entry;
753 struct zip_entry zip_entry_central_dir;
754 int ret = ARCHIVE_OK;
757 /* Save a copy of the original for consistency checks. */
758 zip_entry_central_dir = *zip_entry;
760 zip->decompress_init = 0;
761 zip->end_of_entry = 0;
762 zip->entry_uncompressed_bytes_read = 0;
763 zip->entry_compressed_bytes_read = 0;
764 zip->entry_crc32 = zip->crc32func(0, NULL, 0);
766 /* Setup default conversion. */
767 if (zip->sconv == NULL && !zip->init_default_conversion) {
769 archive_string_default_conversion_for_read(&(a->archive));
770 zip->init_default_conversion = 1;
773 if ((p = __archive_read_ahead(a, 30, NULL)) == NULL) {
774 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
775 "Truncated ZIP file header");
776 return (ARCHIVE_FATAL);
779 if (memcmp(p, "PK\003\004", 4) != 0) {
780 archive_set_error(&a->archive, -1, "Damaged Zip archive");
781 return ARCHIVE_FATAL;
784 zip_entry->system = p[5];
785 zip_entry->zip_flags = archive_le16dec(p + 6);
786 if (zip_entry->zip_flags & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)) {
787 zip->has_encrypted_entries = 1;
788 archive_entry_set_is_data_encrypted(entry, 1);
789 if (zip_entry->zip_flags & ZIP_CENTRAL_DIRECTORY_ENCRYPTED &&
790 zip_entry->zip_flags & ZIP_ENCRYPTED &&
791 zip_entry->zip_flags & ZIP_STRONG_ENCRYPTED) {
792 archive_entry_set_is_metadata_encrypted(entry, 1);
793 return ARCHIVE_FATAL;
796 zip->init_decryption = (zip_entry->zip_flags & ZIP_ENCRYPTED);
797 zip_entry->compression = (char)archive_le16dec(p + 8);
798 zip_entry->mtime = zip_time(p + 10);
799 zip_entry->crc32 = archive_le32dec(p + 14);
800 if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
801 zip_entry->decdat = p[11];
803 zip_entry->decdat = p[17];
804 zip_entry->compressed_size = archive_le32dec(p + 18);
805 zip_entry->uncompressed_size = archive_le32dec(p + 22);
806 filename_length = archive_le16dec(p + 26);
807 extra_length = archive_le16dec(p + 28);
809 __archive_read_consume(a, 30);
811 /* Read the filename. */
812 if ((h = __archive_read_ahead(a, filename_length, NULL)) == NULL) {
813 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
814 "Truncated ZIP file header");
815 return (ARCHIVE_FATAL);
817 if (zip_entry->zip_flags & ZIP_UTF8_NAME) {
818 /* The filename is stored to be UTF-8. */
819 if (zip->sconv_utf8 == NULL) {
821 archive_string_conversion_from_charset(
822 &a->archive, "UTF-8", 1);
823 if (zip->sconv_utf8 == NULL)
824 return (ARCHIVE_FATAL);
826 sconv = zip->sconv_utf8;
827 } else if (zip->sconv != NULL)
830 sconv = zip->sconv_default;
832 if (archive_entry_copy_pathname_l(entry,
833 h, filename_length, sconv) != 0) {
834 if (errno == ENOMEM) {
835 archive_set_error(&a->archive, ENOMEM,
836 "Can't allocate memory for Pathname");
837 return (ARCHIVE_FATAL);
839 archive_set_error(&a->archive,
840 ARCHIVE_ERRNO_FILE_FORMAT,
841 "Pathname cannot be converted "
842 "from %s to current locale.",
843 archive_string_conversion_charset_name(sconv));
846 __archive_read_consume(a, filename_length);
848 /* Read the extra data. */
849 if ((h = __archive_read_ahead(a, extra_length, NULL)) == NULL) {
850 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
851 "Truncated ZIP file header");
852 return (ARCHIVE_FATAL);
855 if (ARCHIVE_OK != process_extra(a, h, extra_length, zip_entry)) {
856 return ARCHIVE_FATAL;
858 __archive_read_consume(a, extra_length);
860 /* Work around a bug in Info-Zip: When reading from a pipe, it
861 * stats the pipe instead of synthesizing a file entry. */
862 if ((zip_entry->mode & AE_IFMT) == AE_IFIFO) {
863 zip_entry->mode &= ~ AE_IFMT;
864 zip_entry->mode |= AE_IFREG;
867 /* If the mode is totally empty, set some sane default. */
868 if (zip_entry->mode == 0) {
869 zip_entry->mode |= 0664;
872 /* Make sure that entries with a trailing '/' are marked as directories
873 * even if the External File Attributes contains bogus values. If this
874 * is not a directory and there is no type, assume regularfile. */
875 if ((zip_entry->mode & AE_IFMT) != AE_IFDIR) {
878 wp = archive_entry_pathname_w(entry);
881 has_slash = len > 0 && wp[len - 1] == L'/';
883 cp = archive_entry_pathname(entry);
884 len = (cp != NULL)?strlen(cp):0;
885 has_slash = len > 0 && cp[len - 1] == '/';
887 /* Correct file type as needed. */
889 zip_entry->mode &= ~AE_IFMT;
890 zip_entry->mode |= AE_IFDIR;
891 zip_entry->mode |= 0111;
892 } else if ((zip_entry->mode & AE_IFMT) == 0) {
893 zip_entry->mode |= AE_IFREG;
897 /* Make sure directories end in '/' */
898 if ((zip_entry->mode & AE_IFMT) == AE_IFDIR) {
899 wp = archive_entry_pathname_w(entry);
902 if (len > 0 && wp[len - 1] != L'/') {
903 struct archive_wstring s;
904 archive_string_init(&s);
905 archive_wstrcat(&s, wp);
906 archive_wstrappend_wchar(&s, L'/');
907 archive_entry_copy_pathname_w(entry, s.s);
908 archive_wstring_free(&s);
911 cp = archive_entry_pathname(entry);
912 len = (cp != NULL)?strlen(cp):0;
913 if (len > 0 && cp[len - 1] != '/') {
914 struct archive_string s;
915 archive_string_init(&s);
916 archive_strcat(&s, cp);
917 archive_strappend_char(&s, '/');
918 archive_entry_set_pathname(entry, s.s);
919 archive_string_free(&s);
924 if (zip_entry->flags & LA_FROM_CENTRAL_DIRECTORY) {
925 /* If this came from the central dir, it's size info
926 * is definitive, so ignore the length-at-end flag. */
927 zip_entry->zip_flags &= ~ZIP_LENGTH_AT_END;
928 /* If local header is missing a value, use the one from
929 the central directory. If both have it, warn about
931 if (zip_entry->crc32 == 0) {
932 zip_entry->crc32 = zip_entry_central_dir.crc32;
933 } else if (!zip->ignore_crc32
934 && zip_entry->crc32 != zip_entry_central_dir.crc32) {
935 archive_set_error(&a->archive,
936 ARCHIVE_ERRNO_FILE_FORMAT,
937 "Inconsistent CRC32 values");
940 if (zip_entry->compressed_size == 0) {
941 zip_entry->compressed_size
942 = zip_entry_central_dir.compressed_size;
943 } else if (zip_entry->compressed_size
944 != zip_entry_central_dir.compressed_size) {
945 archive_set_error(&a->archive,
946 ARCHIVE_ERRNO_FILE_FORMAT,
947 "Inconsistent compressed size: "
948 "%jd in central directory, %jd in local header",
949 (intmax_t)zip_entry_central_dir.compressed_size,
950 (intmax_t)zip_entry->compressed_size);
953 if (zip_entry->uncompressed_size == 0) {
954 zip_entry->uncompressed_size
955 = zip_entry_central_dir.uncompressed_size;
956 } else if (zip_entry->uncompressed_size
957 != zip_entry_central_dir.uncompressed_size) {
958 archive_set_error(&a->archive,
959 ARCHIVE_ERRNO_FILE_FORMAT,
960 "Inconsistent uncompressed size: "
961 "%jd in central directory, %jd in local header",
962 (intmax_t)zip_entry_central_dir.uncompressed_size,
963 (intmax_t)zip_entry->uncompressed_size);
968 /* Populate some additional entry fields: */
969 archive_entry_set_mode(entry, zip_entry->mode);
970 archive_entry_set_uid(entry, zip_entry->uid);
971 archive_entry_set_gid(entry, zip_entry->gid);
972 archive_entry_set_mtime(entry, zip_entry->mtime, 0);
973 archive_entry_set_ctime(entry, zip_entry->ctime, 0);
974 archive_entry_set_atime(entry, zip_entry->atime, 0);
976 if ((zip->entry->mode & AE_IFMT) == AE_IFLNK) {
977 size_t linkname_length;
979 if (zip_entry->compressed_size > 64 * 1024) {
980 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
981 "Zip file with oversized link entry");
982 return ARCHIVE_FATAL;
985 linkname_length = (size_t)zip_entry->compressed_size;
987 archive_entry_set_size(entry, 0);
988 p = __archive_read_ahead(a, linkname_length, NULL);
990 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
991 "Truncated Zip file");
992 return ARCHIVE_FATAL;
996 if (sconv == NULL && (zip->entry->zip_flags & ZIP_UTF8_NAME))
997 sconv = zip->sconv_utf8;
999 sconv = zip->sconv_default;
1000 if (archive_entry_copy_symlink_l(entry, p, linkname_length,
1002 if (errno != ENOMEM && sconv == zip->sconv_utf8 &&
1003 (zip->entry->zip_flags & ZIP_UTF8_NAME))
1004 archive_entry_copy_symlink_l(entry, p,
1005 linkname_length, NULL);
1006 if (errno == ENOMEM) {
1007 archive_set_error(&a->archive, ENOMEM,
1008 "Can't allocate memory for Symlink");
1009 return (ARCHIVE_FATAL);
1012 * Since there is no character-set regulation for
1013 * symlink name, do not report the conversion error
1014 * in an automatic conversion.
1016 if (sconv != zip->sconv_utf8 ||
1017 (zip->entry->zip_flags & ZIP_UTF8_NAME) == 0) {
1018 archive_set_error(&a->archive,
1019 ARCHIVE_ERRNO_FILE_FORMAT,
1020 "Symlink cannot be converted "
1021 "from %s to current locale.",
1022 archive_string_conversion_charset_name(
1027 zip_entry->uncompressed_size = zip_entry->compressed_size = 0;
1029 if (__archive_read_consume(a, linkname_length) < 0) {
1030 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1031 "Read error skipping symlink target name");
1032 return ARCHIVE_FATAL;
1034 } else if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
1035 || zip_entry->uncompressed_size > 0) {
1036 /* Set the size only if it's meaningful. */
1037 archive_entry_set_size(entry, zip_entry->uncompressed_size);
1039 zip->entry_bytes_remaining = zip_entry->compressed_size;
1041 /* If there's no body, force read_data() to return EOF immediately. */
1042 if (0 == (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
1043 && zip->entry_bytes_remaining < 1)
1044 zip->end_of_entry = 1;
1046 /* Set up a more descriptive format name. */
1047 archive_string_sprintf(&zip->format_name, "ZIP %d.%d (%s)",
1048 version / 10, version % 10,
1049 compression_name(zip->entry->compression));
1050 a->archive.archive_format_name = zip->format_name.s;
1056 check_authentication_code(struct archive_read *a, const void *_p)
1058 struct zip *zip = (struct zip *)(a->format->data);
1060 /* Check authentication code. */
1061 if (zip->hctx_valid) {
1064 size_t hmac_len = 20;
1067 archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
1069 /* Read authentication code. */
1070 p = __archive_read_ahead(a, AUTH_CODE_SIZE, NULL);
1072 archive_set_error(&a->archive,
1073 ARCHIVE_ERRNO_FILE_FORMAT,
1074 "Truncated ZIP file data");
1075 return (ARCHIVE_FATAL);
1080 cmp = memcmp(hmac, p, AUTH_CODE_SIZE);
1081 __archive_read_consume(a, AUTH_CODE_SIZE);
1083 archive_set_error(&a->archive,
1085 "ZIP bad Authentication code");
1086 return (ARCHIVE_WARN);
1089 return (ARCHIVE_OK);
1093 * Read "uncompressed" data. There are three cases:
1094 * 1) We know the size of the data. This is always true for the
1095 * seeking reader (we've examined the Central Directory already).
1096 * 2) ZIP_LENGTH_AT_END was set, but only the CRC was deferred.
1097 * Info-ZIP seems to do this; we know the size but have to grab
1098 * the CRC from the data descriptor afterwards.
1099 * 3) We're streaming and ZIP_LENGTH_AT_END was specified and
1100 * we have no size information. In this case, we can do pretty
1101 * well by watching for the data descriptor record. The data
1102 * descriptor is 16 bytes and includes a computed CRC that should
1103 * provide a strong check.
1105 * TODO: Technically, the PK\007\010 signature is optional.
1106 * In the original spec, the data descriptor contained CRC
1107 * and size fields but had no leading signature. In practice,
1108 * newer writers seem to provide the signature pretty consistently.
1110 * For uncompressed data, the PK\007\010 marker seems essential
1111 * to be sure we've actually seen the end of the entry.
1113 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
1114 * zip->end_of_entry if it consumes all of the data.
1117 zip_read_data_none(struct archive_read *a, const void **_buff,
1118 size_t *size, int64_t *offset)
1122 ssize_t bytes_avail;
1125 (void)offset; /* UNUSED */
1127 zip = (struct zip *)(a->format->data);
1129 if (zip->entry->zip_flags & ZIP_LENGTH_AT_END) {
1131 ssize_t grabbing_bytes = 24;
1133 if (zip->hctx_valid)
1134 grabbing_bytes += AUTH_CODE_SIZE;
1135 /* Grab at least 24 bytes. */
1136 buff = __archive_read_ahead(a, grabbing_bytes, &bytes_avail);
1137 if (bytes_avail < grabbing_bytes) {
1138 /* Zip archives have end-of-archive markers
1139 that are longer than this, so a failure to get at
1140 least 24 bytes really does indicate a truncated
1142 archive_set_error(&a->archive,
1143 ARCHIVE_ERRNO_FILE_FORMAT,
1144 "Truncated ZIP file data");
1145 return (ARCHIVE_FATAL);
1147 /* Check for a complete PK\007\010 signature, followed
1148 * by the correct 4-byte CRC. */
1150 if (zip->hctx_valid)
1151 p += AUTH_CODE_SIZE;
1152 if (p[0] == 'P' && p[1] == 'K'
1153 && p[2] == '\007' && p[3] == '\010'
1154 && (archive_le32dec(p + 4) == zip->entry_crc32
1155 || zip->ignore_crc32
1157 && zip->entry->aes_extra.vendor == AES_VENDOR_AE_2))) {
1158 if (zip->entry->flags & LA_USED_ZIP64) {
1159 zip->entry->crc32 = archive_le32dec(p + 4);
1160 zip->entry->compressed_size =
1161 archive_le64dec(p + 8);
1162 zip->entry->uncompressed_size =
1163 archive_le64dec(p + 16);
1164 zip->unconsumed = 24;
1166 zip->entry->crc32 = archive_le32dec(p + 4);
1167 zip->entry->compressed_size =
1168 archive_le32dec(p + 8);
1169 zip->entry->uncompressed_size =
1170 archive_le32dec(p + 12);
1171 zip->unconsumed = 16;
1173 if (zip->hctx_valid) {
1174 r = check_authentication_code(a, buff);
1175 if (r != ARCHIVE_OK)
1178 zip->end_of_entry = 1;
1179 return (ARCHIVE_OK);
1181 /* If not at EOF, ensure we consume at least one byte. */
1184 /* Scan forward until we see where a PK\007\010 signature
1186 /* Return bytes up until that point. On the next call,
1187 * the code above will verify the data descriptor. */
1188 while (p < buff + bytes_avail - 4) {
1189 if (p[3] == 'P') { p += 3; }
1190 else if (p[3] == 'K') { p += 2; }
1191 else if (p[3] == '\007') { p += 1; }
1192 else if (p[3] == '\010' && p[2] == '\007'
1193 && p[1] == 'K' && p[0] == 'P') {
1194 if (zip->hctx_valid)
1195 p -= AUTH_CODE_SIZE;
1199 bytes_avail = p - buff;
1201 if (zip->entry_bytes_remaining == 0) {
1202 zip->end_of_entry = 1;
1203 if (zip->hctx_valid) {
1204 r = check_authentication_code(a, NULL);
1205 if (r != ARCHIVE_OK)
1208 return (ARCHIVE_OK);
1210 /* Grab a bunch of bytes. */
1211 buff = __archive_read_ahead(a, 1, &bytes_avail);
1212 if (bytes_avail <= 0) {
1213 archive_set_error(&a->archive,
1214 ARCHIVE_ERRNO_FILE_FORMAT,
1215 "Truncated ZIP file data");
1216 return (ARCHIVE_FATAL);
1218 if (bytes_avail > zip->entry_bytes_remaining)
1219 bytes_avail = (ssize_t)zip->entry_bytes_remaining;
1221 if (zip->tctx_valid || zip->cctx_valid) {
1222 size_t dec_size = bytes_avail;
1224 if (dec_size > zip->decrypted_buffer_size)
1225 dec_size = zip->decrypted_buffer_size;
1226 if (zip->tctx_valid) {
1227 trad_enc_decrypt_update(&zip->tctx,
1228 (const uint8_t *)buff, dec_size,
1229 zip->decrypted_buffer, dec_size);
1231 size_t dsize = dec_size;
1232 archive_hmac_sha1_update(&zip->hctx,
1233 (const uint8_t *)buff, dec_size);
1234 archive_decrypto_aes_ctr_update(&zip->cctx,
1235 (const uint8_t *)buff, dec_size,
1236 zip->decrypted_buffer, &dsize);
1238 bytes_avail = dec_size;
1239 buff = (const char *)zip->decrypted_buffer;
1241 *size = bytes_avail;
1242 zip->entry_bytes_remaining -= bytes_avail;
1243 zip->entry_uncompressed_bytes_read += bytes_avail;
1244 zip->entry_compressed_bytes_read += bytes_avail;
1245 zip->unconsumed += bytes_avail;
1247 return (ARCHIVE_OK);
1252 zip_deflate_init(struct archive_read *a, struct zip *zip)
1256 /* If we haven't yet read any data, initialize the decompressor. */
1257 if (!zip->decompress_init) {
1258 if (zip->stream_valid)
1259 r = inflateReset(&zip->stream);
1261 r = inflateInit2(&zip->stream,
1262 -15 /* Don't check for zlib header */);
1264 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1265 "Can't initialize ZIP decompression.");
1266 return (ARCHIVE_FATAL);
1268 /* Stream structure has been set up. */
1269 zip->stream_valid = 1;
1270 /* We've initialized decompression for this stream. */
1271 zip->decompress_init = 1;
1273 return (ARCHIVE_OK);
1277 zip_read_data_deflate(struct archive_read *a, const void **buff,
1278 size_t *size, int64_t *offset)
1281 ssize_t bytes_avail;
1282 const void *compressed_buff, *sp;
1285 (void)offset; /* UNUSED */
1287 zip = (struct zip *)(a->format->data);
1289 /* If the buffer hasn't been allocated, allocate it now. */
1290 if (zip->uncompressed_buffer == NULL) {
1291 zip->uncompressed_buffer_size = 256 * 1024;
1292 zip->uncompressed_buffer
1293 = (unsigned char *)malloc(zip->uncompressed_buffer_size);
1294 if (zip->uncompressed_buffer == NULL) {
1295 archive_set_error(&a->archive, ENOMEM,
1296 "No memory for ZIP decompression");
1297 return (ARCHIVE_FATAL);
1301 r = zip_deflate_init(a, zip);
1302 if (r != ARCHIVE_OK)
1306 * Note: '1' here is a performance optimization.
1307 * Recall that the decompression layer returns a count of
1308 * available bytes; asking for more than that forces the
1309 * decompressor to combine reads by copying data.
1311 compressed_buff = sp = __archive_read_ahead(a, 1, &bytes_avail);
1312 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
1313 && bytes_avail > zip->entry_bytes_remaining) {
1314 bytes_avail = (ssize_t)zip->entry_bytes_remaining;
1316 if (bytes_avail < 0) {
1317 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1318 "Truncated ZIP file body");
1319 return (ARCHIVE_FATAL);
1322 if (zip->tctx_valid || zip->cctx_valid) {
1323 if (zip->decrypted_bytes_remaining < (size_t)bytes_avail) {
1324 size_t buff_remaining =
1325 (zip->decrypted_buffer + zip->decrypted_buffer_size)
1326 - (zip->decrypted_ptr + zip->decrypted_bytes_remaining);
1328 if (buff_remaining > (size_t)bytes_avail)
1329 buff_remaining = (size_t)bytes_avail;
1331 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END) &&
1332 zip->entry_bytes_remaining > 0) {
1333 if ((int64_t)(zip->decrypted_bytes_remaining
1335 > zip->entry_bytes_remaining) {
1336 if (zip->entry_bytes_remaining <
1337 (int64_t)zip->decrypted_bytes_remaining)
1341 (size_t)zip->entry_bytes_remaining
1342 - zip->decrypted_bytes_remaining;
1345 if (buff_remaining > 0) {
1346 if (zip->tctx_valid) {
1347 trad_enc_decrypt_update(&zip->tctx,
1348 compressed_buff, buff_remaining,
1350 + zip->decrypted_bytes_remaining,
1353 size_t dsize = buff_remaining;
1354 archive_decrypto_aes_ctr_update(
1356 compressed_buff, buff_remaining,
1358 + zip->decrypted_bytes_remaining,
1361 zip->decrypted_bytes_remaining += buff_remaining;
1364 bytes_avail = zip->decrypted_bytes_remaining;
1365 compressed_buff = (const char *)zip->decrypted_ptr;
1369 * A bug in zlib.h: stream.next_in should be marked 'const'
1370 * but isn't (the library never alters data through the
1371 * next_in pointer, only reads it). The result: this ugly
1372 * cast to remove 'const'.
1374 zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
1375 zip->stream.avail_in = (uInt)bytes_avail;
1376 zip->stream.total_in = 0;
1377 zip->stream.next_out = zip->uncompressed_buffer;
1378 zip->stream.avail_out = (uInt)zip->uncompressed_buffer_size;
1379 zip->stream.total_out = 0;
1381 r = inflate(&zip->stream, 0);
1386 zip->end_of_entry = 1;
1389 archive_set_error(&a->archive, ENOMEM,
1390 "Out of memory for ZIP decompression");
1391 return (ARCHIVE_FATAL);
1393 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1394 "ZIP decompression failed (%d)", r);
1395 return (ARCHIVE_FATAL);
1398 /* Consume as much as the compressor actually used. */
1399 bytes_avail = zip->stream.total_in;
1400 if (zip->tctx_valid || zip->cctx_valid) {
1401 zip->decrypted_bytes_remaining -= bytes_avail;
1402 if (zip->decrypted_bytes_remaining == 0)
1403 zip->decrypted_ptr = zip->decrypted_buffer;
1405 zip->decrypted_ptr += bytes_avail;
1407 /* Calculate compressed data as much as we used.*/
1408 if (zip->hctx_valid)
1409 archive_hmac_sha1_update(&zip->hctx, sp, bytes_avail);
1410 __archive_read_consume(a, bytes_avail);
1411 zip->entry_bytes_remaining -= bytes_avail;
1412 zip->entry_compressed_bytes_read += bytes_avail;
1414 *size = zip->stream.total_out;
1415 zip->entry_uncompressed_bytes_read += zip->stream.total_out;
1416 *buff = zip->uncompressed_buffer;
1418 if (zip->end_of_entry && zip->hctx_valid) {
1419 r = check_authentication_code(a, NULL);
1420 if (r != ARCHIVE_OK)
1424 if (zip->end_of_entry && (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) {
1427 if (NULL == (p = __archive_read_ahead(a, 24, NULL))) {
1428 archive_set_error(&a->archive,
1429 ARCHIVE_ERRNO_FILE_FORMAT,
1430 "Truncated ZIP end-of-file record");
1431 return (ARCHIVE_FATAL);
1433 /* Consume the optional PK\007\010 marker. */
1434 if (p[0] == 'P' && p[1] == 'K' &&
1435 p[2] == '\007' && p[3] == '\010') {
1437 zip->unconsumed = 4;
1439 if (zip->entry->flags & LA_USED_ZIP64) {
1440 zip->entry->crc32 = archive_le32dec(p);
1441 zip->entry->compressed_size = archive_le64dec(p + 4);
1442 zip->entry->uncompressed_size = archive_le64dec(p + 12);
1443 zip->unconsumed += 20;
1445 zip->entry->crc32 = archive_le32dec(p);
1446 zip->entry->compressed_size = archive_le32dec(p + 4);
1447 zip->entry->uncompressed_size = archive_le32dec(p + 8);
1448 zip->unconsumed += 12;
1452 return (ARCHIVE_OK);
1457 read_decryption_header(struct archive_read *a)
1459 struct zip *zip = (struct zip *)(a->format->data);
1461 unsigned int remaining_size;
1465 * Read an initialization vector data field.
1467 p = __archive_read_ahead(a, 2, NULL);
1471 zip->iv_size = archive_le16dec(p);
1472 __archive_read_consume(a, 2);
1473 if (ts < zip->iv_size) {
1477 p = __archive_read_ahead(a, zip->iv_size, NULL);
1480 if (zip->iv == NULL) {
1481 zip->iv = malloc(zip->iv_size);
1482 if (zip->iv == NULL)
1485 memcpy(zip->iv, p, zip->iv_size);
1486 __archive_read_consume(a, zip->iv_size);
1489 * Read a size of remaining decryption header field.
1491 p = __archive_read_ahead(a, 14, NULL);
1494 remaining_size = archive_le32dec(p);
1495 if (remaining_size < 16 || remaining_size > (1 << 18))
1498 /* Check if format version is supported. */
1499 if (archive_le16dec(p+4) != 3) {
1500 archive_set_error(&a->archive,
1501 ARCHIVE_ERRNO_FILE_FORMAT,
1502 "Unsupported encryption format version: %u",
1503 archive_le16dec(p+4));
1504 return (ARCHIVE_FAILED);
1508 * Read an encryption algorithm field.
1510 zip->alg_id = archive_le16dec(p+6);
1511 switch (zip->alg_id) {
1512 case 0x6601:/* DES */
1513 case 0x6602:/* RC2 */
1514 case 0x6603:/* 3DES 168 */
1515 case 0x6609:/* 3DES 112 */
1516 case 0x660E:/* AES 128 */
1517 case 0x660F:/* AES 192 */
1518 case 0x6610:/* AES 256 */
1519 case 0x6702:/* RC2 (version >= 5.2) */
1520 case 0x6720:/* Blowfish */
1521 case 0x6721:/* Twofish */
1522 case 0x6801:/* RC4 */
1523 /* Supported encryption algorithm. */
1526 archive_set_error(&a->archive,
1527 ARCHIVE_ERRNO_FILE_FORMAT,
1528 "Unknown encryption algorithm: %u", zip->alg_id);
1529 return (ARCHIVE_FAILED);
1533 * Read a bit length field.
1535 zip->bit_len = archive_le16dec(p+8);
1538 * Read a flags field.
1540 zip->flags = archive_le16dec(p+10);
1541 switch (zip->flags & 0xf000) {
1542 case 0x0001: /* Password is required to decrypt. */
1543 case 0x0002: /* Certificates only. */
1544 case 0x0003: /* Password or certificate required to decrypt. */
1547 archive_set_error(&a->archive,
1548 ARCHIVE_ERRNO_FILE_FORMAT,
1549 "Unknown encryption flag: %u", zip->flags);
1550 return (ARCHIVE_FAILED);
1552 if ((zip->flags & 0xf000) == 0 ||
1553 (zip->flags & 0xf000) == 0x4000) {
1554 archive_set_error(&a->archive,
1555 ARCHIVE_ERRNO_FILE_FORMAT,
1556 "Unknown encryption flag: %u", zip->flags);
1557 return (ARCHIVE_FAILED);
1561 * Read an encrypted random data field.
1564 zip->erd_size = archive_le16dec(p+12);
1565 __archive_read_consume(a, 14);
1566 if ((zip->erd_size & 0xf) != 0 ||
1567 (zip->erd_size + 16) > remaining_size ||
1568 (zip->erd_size + 16) < zip->erd_size)
1571 if (ts < zip->erd_size) {
1575 p = __archive_read_ahead(a, zip->erd_size, NULL);
1578 if (zip->erd == NULL) {
1579 zip->erd = malloc(zip->erd_size);
1580 if (zip->erd == NULL)
1583 memcpy(zip->erd, p, zip->erd_size);
1584 __archive_read_consume(a, zip->erd_size);
1587 * Read a reserved data field.
1589 p = __archive_read_ahead(a, 4, NULL);
1592 /* Reserved data size should be zero. */
1593 if (archive_le32dec(p) != 0)
1595 __archive_read_consume(a, 4);
1598 * Read a password validation data field.
1600 p = __archive_read_ahead(a, 2, NULL);
1604 zip->v_size = archive_le16dec(p);
1605 __archive_read_consume(a, 2);
1606 if ((zip->v_size & 0x0f) != 0 ||
1607 (zip->erd_size + zip->v_size + 16) > remaining_size ||
1608 (zip->erd_size + zip->v_size + 16) < (zip->erd_size + zip->v_size))
1610 if (ts < zip->v_size) {
1614 p = __archive_read_ahead(a, zip->v_size, NULL);
1617 if (zip->v_data == NULL) {
1618 zip->v_data = malloc(zip->v_size);
1619 if (zip->v_data == NULL)
1622 memcpy(zip->v_data, p, zip->v_size);
1623 __archive_read_consume(a, zip->v_size);
1625 p = __archive_read_ahead(a, 4, NULL);
1628 zip->v_crc32 = archive_le32dec(p);
1629 __archive_read_consume(a, 4);
1631 /*return (ARCHIVE_OK);
1632 * This is not fully implemented yet.*/
1633 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1634 "Encrypted file is unsupported");
1635 return (ARCHIVE_FAILED);
1637 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1638 "Truncated ZIP file data");
1639 return (ARCHIVE_FATAL);
1641 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1642 "Corrupted ZIP file data");
1643 return (ARCHIVE_FATAL);
1645 archive_set_error(&a->archive, ENOMEM,
1646 "No memory for ZIP decryption");
1647 return (ARCHIVE_FATAL);
1651 zip_alloc_decryption_buffer(struct archive_read *a)
1653 struct zip *zip = (struct zip *)(a->format->data);
1654 size_t bs = 256 * 1024;
1656 if (zip->decrypted_buffer == NULL) {
1657 zip->decrypted_buffer_size = bs;
1658 zip->decrypted_buffer = malloc(bs);
1659 if (zip->decrypted_buffer == NULL) {
1660 archive_set_error(&a->archive, ENOMEM,
1661 "No memory for ZIP decryption");
1662 return (ARCHIVE_FATAL);
1665 zip->decrypted_ptr = zip->decrypted_buffer;
1666 return (ARCHIVE_OK);
1670 init_traditional_PKWARE_decryption(struct archive_read *a)
1672 struct zip *zip = (struct zip *)(a->format->data);
1677 if (zip->tctx_valid)
1678 return (ARCHIVE_OK);
1681 Read the 12 bytes encryption header stored at
1682 the start of the data area.
1684 #define ENC_HEADER_SIZE 12
1685 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
1686 && zip->entry_bytes_remaining < ENC_HEADER_SIZE) {
1687 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1688 "Truncated Zip encrypted body: only %jd bytes available",
1689 (intmax_t)zip->entry_bytes_remaining);
1690 return (ARCHIVE_FATAL);
1693 p = __archive_read_ahead(a, ENC_HEADER_SIZE, NULL);
1695 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1696 "Truncated ZIP file data");
1697 return (ARCHIVE_FATAL);
1700 for (retry = 0;; retry++) {
1701 const char *passphrase;
1704 passphrase = __archive_read_next_passphrase(a);
1705 if (passphrase == NULL) {
1706 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1708 "Incorrect passphrase":
1709 "Passphrase required for this entry");
1710 return (ARCHIVE_FAILED);
1714 * Initialize ctx for Traditional PKWARE Decryption.
1716 r = trad_enc_init(&zip->tctx, passphrase, strlen(passphrase),
1717 p, ENC_HEADER_SIZE, &crcchk);
1718 if (r == 0 && crcchk == zip->entry->decdat)
1719 break;/* The passphrase is OK. */
1720 if (retry > 10000) {
1721 /* Avoid infinity loop. */
1722 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1723 "Too many incorrect passphrases");
1724 return (ARCHIVE_FAILED);
1728 __archive_read_consume(a, ENC_HEADER_SIZE);
1729 zip->tctx_valid = 1;
1730 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)) {
1731 zip->entry_bytes_remaining -= ENC_HEADER_SIZE;
1733 /*zip->entry_uncompressed_bytes_read += ENC_HEADER_SIZE;*/
1734 zip->entry_compressed_bytes_read += ENC_HEADER_SIZE;
1735 zip->decrypted_bytes_remaining = 0;
1737 return (zip_alloc_decryption_buffer(a));
1738 #undef ENC_HEADER_SIZE
1742 init_WinZip_AES_decryption(struct archive_read *a)
1744 struct zip *zip = (struct zip *)(a->format->data);
1747 size_t key_len, salt_len;
1748 uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
1752 if (zip->cctx_valid || zip->hctx_valid)
1753 return (ARCHIVE_OK);
1755 switch (zip->entry->aes_extra.strength) {
1756 case 1: salt_len = 8; key_len = 16; break;
1757 case 2: salt_len = 12; key_len = 24; break;
1758 case 3: salt_len = 16; key_len = 32; break;
1759 default: goto corrupted;
1761 p = __archive_read_ahead(a, salt_len + 2, NULL);
1765 for (retry = 0;; retry++) {
1766 const char *passphrase;
1768 passphrase = __archive_read_next_passphrase(a);
1769 if (passphrase == NULL) {
1770 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1772 "Incorrect passphrase":
1773 "Passphrase required for this entry");
1774 return (ARCHIVE_FAILED);
1776 memset(derived_key, 0, sizeof(derived_key));
1777 r = archive_pbkdf2_sha1(passphrase, strlen(passphrase),
1778 p, salt_len, 1000, derived_key, key_len * 2 + 2);
1780 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1781 "Decryption is unsupported due to lack of "
1783 return (ARCHIVE_FAILED);
1786 /* Check password verification value. */
1787 pv = ((const uint8_t *)p) + salt_len;
1788 if (derived_key[key_len * 2] == pv[0] &&
1789 derived_key[key_len * 2 + 1] == pv[1])
1790 break;/* The passphrase is OK. */
1791 if (retry > 10000) {
1792 /* Avoid infinity loop. */
1793 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1794 "Too many incorrect passphrases");
1795 return (ARCHIVE_FAILED);
1799 r = archive_decrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
1801 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1802 "Decryption is unsupported due to lack of crypto library");
1803 return (ARCHIVE_FAILED);
1805 r = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len, key_len);
1807 archive_decrypto_aes_ctr_release(&zip->cctx);
1808 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1809 "Failed to initialize HMAC-SHA1");
1810 return (ARCHIVE_FAILED);
1812 zip->cctx_valid = zip->hctx_valid = 1;
1813 __archive_read_consume(a, salt_len + 2);
1814 zip->entry_bytes_remaining -= salt_len + 2 + AUTH_CODE_SIZE;
1815 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
1816 && zip->entry_bytes_remaining < 0)
1818 zip->entry_compressed_bytes_read += salt_len + 2 + AUTH_CODE_SIZE;
1819 zip->decrypted_bytes_remaining = 0;
1821 zip->entry->compression = zip->entry->aes_extra.compression;
1822 return (zip_alloc_decryption_buffer(a));
1825 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1826 "Truncated ZIP file data");
1827 return (ARCHIVE_FATAL);
1829 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1830 "Corrupted ZIP file data");
1831 return (ARCHIVE_FATAL);
1835 archive_read_format_zip_read_data(struct archive_read *a,
1836 const void **buff, size_t *size, int64_t *offset)
1839 struct zip *zip = (struct zip *)(a->format->data);
1841 if (zip->has_encrypted_entries ==
1842 ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW) {
1843 zip->has_encrypted_entries = 0;
1846 *offset = zip->entry_uncompressed_bytes_read;
1850 /* If we hit end-of-entry last time, return ARCHIVE_EOF. */
1851 if (zip->end_of_entry)
1852 return (ARCHIVE_EOF);
1854 /* Return EOF immediately if this is a non-regular file. */
1855 if (AE_IFREG != (zip->entry->mode & AE_IFMT))
1856 return (ARCHIVE_EOF);
1858 __archive_read_consume(a, zip->unconsumed);
1859 zip->unconsumed = 0;
1861 if (zip->init_decryption) {
1862 zip->has_encrypted_entries = 1;
1863 if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED)
1864 r = read_decryption_header(a);
1865 else if (zip->entry->compression == WINZIP_AES_ENCRYPTION)
1866 r = init_WinZip_AES_decryption(a);
1868 r = init_traditional_PKWARE_decryption(a);
1869 if (r != ARCHIVE_OK)
1871 zip->init_decryption = 0;
1874 switch(zip->entry->compression) {
1875 case 0: /* No compression. */
1876 r = zip_read_data_none(a, buff, size, offset);
1879 case 8: /* Deflate compression. */
1880 r = zip_read_data_deflate(a, buff, size, offset);
1883 default: /* Unsupported compression. */
1884 /* Return a warning. */
1885 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1886 "Unsupported ZIP compression method (%s)",
1887 compression_name(zip->entry->compression));
1888 /* We can't decompress this entry, but we will
1889 * be able to skip() it and try the next entry. */
1890 return (ARCHIVE_FAILED);
1893 if (r != ARCHIVE_OK)
1895 /* Update checksum */
1897 zip->entry_crc32 = zip->crc32func(zip->entry_crc32, *buff,
1899 /* If we hit the end, swallow any end-of-data marker. */
1900 if (zip->end_of_entry) {
1901 /* Check file size, CRC against these values. */
1902 if (zip->entry->compressed_size !=
1903 zip->entry_compressed_bytes_read) {
1904 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1905 "ZIP compressed data is wrong size "
1906 "(read %jd, expected %jd)",
1907 (intmax_t)zip->entry_compressed_bytes_read,
1908 (intmax_t)zip->entry->compressed_size);
1909 return (ARCHIVE_WARN);
1911 /* Size field only stores the lower 32 bits of the actual
1913 if ((zip->entry->uncompressed_size & UINT32_MAX)
1914 != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
1915 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1916 "ZIP uncompressed data is wrong size "
1917 "(read %jd, expected %jd)\n",
1918 (intmax_t)zip->entry_uncompressed_bytes_read,
1919 (intmax_t)zip->entry->uncompressed_size);
1920 return (ARCHIVE_WARN);
1922 /* Check computed CRC against header */
1923 if ((!zip->hctx_valid ||
1924 zip->entry->aes_extra.vendor != AES_VENDOR_AE_2) &&
1925 zip->entry->crc32 != zip->entry_crc32
1926 && !zip->ignore_crc32) {
1927 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1928 "ZIP bad CRC: 0x%lx should be 0x%lx",
1929 (unsigned long)zip->entry_crc32,
1930 (unsigned long)zip->entry->crc32);
1931 return (ARCHIVE_WARN);
1935 return (ARCHIVE_OK);
1939 archive_read_format_zip_cleanup(struct archive_read *a)
1942 struct zip_entry *zip_entry, *next_zip_entry;
1944 zip = (struct zip *)(a->format->data);
1946 if (zip->stream_valid)
1947 inflateEnd(&zip->stream);
1948 free(zip->uncompressed_buffer);
1950 if (zip->zip_entries) {
1951 zip_entry = zip->zip_entries;
1952 while (zip_entry != NULL) {
1953 next_zip_entry = zip_entry->next;
1954 archive_string_free(&zip_entry->rsrcname);
1956 zip_entry = next_zip_entry;
1959 free(zip->decrypted_buffer);
1960 if (zip->cctx_valid)
1961 archive_decrypto_aes_ctr_release(&zip->cctx);
1962 if (zip->hctx_valid)
1963 archive_hmac_sha1_cleanup(&zip->hctx);
1967 archive_string_free(&zip->format_name);
1969 (a->format->data) = NULL;
1970 return (ARCHIVE_OK);
1974 archive_read_format_zip_has_encrypted_entries(struct archive_read *_a)
1976 if (_a && _a->format) {
1977 struct zip * zip = (struct zip *)_a->format->data;
1979 return zip->has_encrypted_entries;
1982 return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
1986 archive_read_format_zip_options(struct archive_read *a,
1987 const char *key, const char *val)
1990 int ret = ARCHIVE_FAILED;
1992 zip = (struct zip *)(a->format->data);
1993 if (strcmp(key, "compat-2x") == 0) {
1994 /* Handle filenames as libarchive 2.x */
1995 zip->init_default_conversion = (val != NULL) ? 1 : 0;
1996 return (ARCHIVE_OK);
1997 } else if (strcmp(key, "hdrcharset") == 0) {
1998 if (val == NULL || val[0] == 0)
1999 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2000 "zip: hdrcharset option needs a character-set name"
2003 zip->sconv = archive_string_conversion_from_charset(
2004 &a->archive, val, 0);
2005 if (zip->sconv != NULL) {
2006 if (strcmp(val, "UTF-8") == 0)
2007 zip->sconv_utf8 = zip->sconv;
2010 ret = ARCHIVE_FATAL;
2013 } else if (strcmp(key, "ignorecrc32") == 0) {
2014 /* Mostly useful for testing. */
2015 if (val == NULL || val[0] == 0) {
2016 zip->crc32func = real_crc32;
2017 zip->ignore_crc32 = 0;
2019 zip->crc32func = fake_crc32;
2020 zip->ignore_crc32 = 1;
2022 return (ARCHIVE_OK);
2023 } else if (strcmp(key, "mac-ext") == 0) {
2024 zip->process_mac_extensions = (val != NULL && val[0] != 0);
2025 return (ARCHIVE_OK);
2028 /* Note: The "warn" return is just to inform the options
2029 * supervisor that we didn't handle it. It will generate
2030 * a suitable error if no one used this option. */
2031 return (ARCHIVE_WARN);
2035 archive_read_support_format_zip(struct archive *a)
2038 r = archive_read_support_format_zip_streamable(a);
2039 if (r != ARCHIVE_OK)
2041 return (archive_read_support_format_zip_seekable(a));
2044 /* ------------------------------------------------------------------------ */
2047 * Streaming-mode support
2052 archive_read_support_format_zip_capabilities_streamable(struct archive_read * a)
2054 (void)a; /* UNUSED */
2055 return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
2056 ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
2060 archive_read_format_zip_streamable_bid(struct archive_read *a, int best_bid)
2064 (void)best_bid; /* UNUSED */
2066 if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
2070 * Bid of 29 here comes from:
2071 * + 16 bits for "PK",
2072 * + next 16-bit field has 6 options so contributes
2073 * about 16 - log_2(6) ~= 16 - 2.6 ~= 13 bits
2075 * So we've effectively verified ~29 total bits of check data.
2077 if (p[0] == 'P' && p[1] == 'K') {
2078 if ((p[2] == '\001' && p[3] == '\002')
2079 || (p[2] == '\003' && p[3] == '\004')
2080 || (p[2] == '\005' && p[3] == '\006')
2081 || (p[2] == '\006' && p[3] == '\006')
2082 || (p[2] == '\007' && p[3] == '\010')
2083 || (p[2] == '0' && p[3] == '0'))
2087 /* TODO: It's worth looking ahead a little bit for a valid
2088 * PK signature. In particular, that would make it possible
2089 * to read some UUEncoded SFX files or SFX files coming from
2090 * a network socket. */
2096 archive_read_format_zip_streamable_read_header(struct archive_read *a,
2097 struct archive_entry *entry)
2101 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
2102 if (a->archive.archive_format_name == NULL)
2103 a->archive.archive_format_name = "ZIP";
2105 zip = (struct zip *)(a->format->data);
2108 * It should be sufficient to call archive_read_next_header() for
2109 * a reader to determine if an entry is encrypted or not. If the
2110 * encryption of an entry is only detectable when calling
2111 * archive_read_data(), so be it. We'll do the same check there
2114 if (zip->has_encrypted_entries ==
2115 ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
2116 zip->has_encrypted_entries = 0;
2118 /* Make sure we have a zip_entry structure to use. */
2119 if (zip->zip_entries == NULL) {
2120 zip->zip_entries = malloc(sizeof(struct zip_entry));
2121 if (zip->zip_entries == NULL) {
2122 archive_set_error(&a->archive, ENOMEM,
2124 return ARCHIVE_FATAL;
2127 zip->entry = zip->zip_entries;
2128 memset(zip->entry, 0, sizeof(struct zip_entry));
2130 if (zip->cctx_valid)
2131 archive_decrypto_aes_ctr_release(&zip->cctx);
2132 if (zip->hctx_valid)
2133 archive_hmac_sha1_cleanup(&zip->hctx);
2134 zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
2135 __archive_read_reset_passphrase(a);
2137 /* Search ahead for the next local file header. */
2138 __archive_read_consume(a, zip->unconsumed);
2139 zip->unconsumed = 0;
2141 int64_t skipped = 0;
2142 const char *p, *end;
2145 p = __archive_read_ahead(a, 4, &bytes);
2147 return (ARCHIVE_FATAL);
2150 while (p + 4 <= end) {
2151 if (p[0] == 'P' && p[1] == 'K') {
2152 if (p[2] == '\003' && p[3] == '\004') {
2153 /* Regular file entry. */
2154 __archive_read_consume(a, skipped);
2155 return zip_read_local_file_header(a,
2160 * TODO: We cannot restore permissions
2161 * based only on the local file headers.
2162 * Consider scanning the central
2163 * directory and returning additional
2164 * entries for at least directories.
2165 * This would allow us to properly set
2166 * directory permissions.
2168 * This won't help us fix symlinks
2169 * and may not help with regular file
2170 * permissions, either. <sigh>
2172 if (p[2] == '\001' && p[3] == '\002') {
2173 return (ARCHIVE_EOF);
2176 /* End of central directory? Must be an
2178 if ((p[2] == '\005' && p[3] == '\006')
2179 || (p[2] == '\006' && p[3] == '\006'))
2180 return (ARCHIVE_EOF);
2185 __archive_read_consume(a, skipped);
2190 archive_read_format_zip_read_data_skip_streamable(struct archive_read *a)
2193 int64_t bytes_skipped;
2195 zip = (struct zip *)(a->format->data);
2196 bytes_skipped = __archive_read_consume(a, zip->unconsumed);
2197 zip->unconsumed = 0;
2198 if (bytes_skipped < 0)
2199 return (ARCHIVE_FATAL);
2201 /* If we've already read to end of data, we're done. */
2202 if (zip->end_of_entry)
2203 return (ARCHIVE_OK);
2205 /* So we know we're streaming... */
2206 if (0 == (zip->entry->zip_flags & ZIP_LENGTH_AT_END)
2207 || zip->entry->compressed_size > 0) {
2208 /* We know the compressed length, so we can just skip. */
2209 bytes_skipped = __archive_read_consume(a,
2210 zip->entry_bytes_remaining);
2211 if (bytes_skipped < 0)
2212 return (ARCHIVE_FATAL);
2213 return (ARCHIVE_OK);
2216 if (zip->init_decryption) {
2219 zip->has_encrypted_entries = 1;
2220 if (zip->entry->zip_flags & ZIP_STRONG_ENCRYPTED)
2221 r = read_decryption_header(a);
2222 else if (zip->entry->compression == WINZIP_AES_ENCRYPTION)
2223 r = init_WinZip_AES_decryption(a);
2225 r = init_traditional_PKWARE_decryption(a);
2226 if (r != ARCHIVE_OK)
2228 zip->init_decryption = 0;
2231 /* We're streaming and we don't know the length. */
2232 /* If the body is compressed and we know the format, we can
2233 * find an exact end-of-entry by decompressing it. */
2234 switch (zip->entry->compression) {
2236 case 8: /* Deflate compression. */
2237 while (!zip->end_of_entry) {
2239 const void *buff = NULL;
2242 r = zip_read_data_deflate(a, &buff, &size, &offset);
2243 if (r != ARCHIVE_OK)
2248 default: /* Uncompressed or unknown. */
2249 /* Scan for a PK\007\010 signature. */
2251 const char *p, *buff;
2252 ssize_t bytes_avail;
2253 buff = __archive_read_ahead(a, 16, &bytes_avail);
2254 if (bytes_avail < 16) {
2255 archive_set_error(&a->archive,
2256 ARCHIVE_ERRNO_FILE_FORMAT,
2257 "Truncated ZIP file data");
2258 return (ARCHIVE_FATAL);
2261 while (p <= buff + bytes_avail - 16) {
2262 if (p[3] == 'P') { p += 3; }
2263 else if (p[3] == 'K') { p += 2; }
2264 else if (p[3] == '\007') { p += 1; }
2265 else if (p[3] == '\010' && p[2] == '\007'
2266 && p[1] == 'K' && p[0] == 'P') {
2267 if (zip->entry->flags & LA_USED_ZIP64)
2268 __archive_read_consume(a,
2271 __archive_read_consume(a,
2276 __archive_read_consume(a, p - buff);
2282 archive_read_support_format_zip_streamable(struct archive *_a)
2284 struct archive_read *a = (struct archive_read *)_a;
2288 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
2289 ARCHIVE_STATE_NEW, "archive_read_support_format_zip");
2291 zip = (struct zip *)calloc(1, sizeof(*zip));
2293 archive_set_error(&a->archive, ENOMEM,
2294 "Can't allocate zip data");
2295 return (ARCHIVE_FATAL);
2298 /* Streamable reader doesn't support mac extensions. */
2299 zip->process_mac_extensions = 0;
2302 * Until enough data has been read, we cannot tell about
2303 * any encrypted entries yet.
2305 zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
2306 zip->crc32func = real_crc32;
2308 r = __archive_read_register_format(a,
2311 archive_read_format_zip_streamable_bid,
2312 archive_read_format_zip_options,
2313 archive_read_format_zip_streamable_read_header,
2314 archive_read_format_zip_read_data,
2315 archive_read_format_zip_read_data_skip_streamable,
2317 archive_read_format_zip_cleanup,
2318 archive_read_support_format_zip_capabilities_streamable,
2319 archive_read_format_zip_has_encrypted_entries);
2321 if (r != ARCHIVE_OK)
2323 return (ARCHIVE_OK);
2326 /* ------------------------------------------------------------------------ */
2329 * Seeking-mode support
2333 archive_read_support_format_zip_capabilities_seekable(struct archive_read * a)
2335 (void)a; /* UNUSED */
2336 return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA |
2337 ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
2341 * TODO: This is a performance sink because it forces the read core to
2342 * drop buffered data from the start of file, which will then have to
2343 * be re-read again if this bidder loses.
2345 * We workaround this a little by passing in the best bid so far so
2346 * that later bidders can do nothing if they know they'll never
2347 * outbid. But we can certainly do better...
2350 read_eocd(struct zip *zip, const char *p, int64_t current_offset)
2352 /* Sanity-check the EOCD we've found. */
2354 /* This must be the first volume. */
2355 if (archive_le16dec(p + 4) != 0)
2357 /* Central directory must be on this volume. */
2358 if (archive_le16dec(p + 4) != archive_le16dec(p + 6))
2360 /* All central directory entries must be on this volume. */
2361 if (archive_le16dec(p + 10) != archive_le16dec(p + 8))
2363 /* Central directory can't extend beyond start of EOCD record. */
2364 if (archive_le32dec(p + 16) + archive_le32dec(p + 12)
2368 /* Save the central directory location for later use. */
2369 zip->central_directory_offset = archive_le32dec(p + 16);
2371 /* This is just a tiny bit higher than the maximum
2372 returned by the streaming Zip bidder. This ensures
2373 that the more accurate seeking Zip parser wins
2374 whenever seek is available. */
2379 * Examine Zip64 EOCD locator: If it's valid, store the information
2383 read_zip64_eocd(struct archive_read *a, struct zip *zip, const char *p)
2385 int64_t eocd64_offset;
2386 int64_t eocd64_size;
2388 /* Sanity-check the locator record. */
2390 /* Central dir must be on first volume. */
2391 if (archive_le32dec(p + 4) != 0)
2393 /* Must be only a single volume. */
2394 if (archive_le32dec(p + 16) != 1)
2397 /* Find the Zip64 EOCD record. */
2398 eocd64_offset = archive_le64dec(p + 8);
2399 if (__archive_read_seek(a, eocd64_offset, SEEK_SET) < 0)
2401 if ((p = __archive_read_ahead(a, 56, NULL)) == NULL)
2403 /* Make sure we can read all of it. */
2404 eocd64_size = archive_le64dec(p + 4) + 12;
2405 if (eocd64_size < 56 || eocd64_size > 16384)
2407 if ((p = __archive_read_ahead(a, (size_t)eocd64_size, NULL)) == NULL)
2410 /* Sanity-check the EOCD64 */
2411 if (archive_le32dec(p + 16) != 0) /* Must be disk #0 */
2413 if (archive_le32dec(p + 20) != 0) /* CD must be on disk #0 */
2415 /* CD can't be split. */
2416 if (archive_le64dec(p + 24) != archive_le64dec(p + 32))
2419 /* Save the central directory offset for later use. */
2420 zip->central_directory_offset = archive_le64dec(p + 48);
2424 archive_read_format_zip_seekable_bid(struct archive_read *a, int best_bid)
2426 struct zip *zip = (struct zip *)a->format->data;
2427 int64_t file_size, current_offset;
2431 /* If someone has already bid more than 32, then avoid
2432 trashing the look-ahead buffers with a seek. */
2436 file_size = __archive_read_seek(a, 0, SEEK_END);
2440 /* Search last 16k of file for end-of-central-directory
2441 * record (which starts with PK\005\006) */
2442 tail = (int)zipmin(1024 * 16, file_size);
2443 current_offset = __archive_read_seek(a, -tail, SEEK_END);
2444 if (current_offset < 0)
2446 if ((p = __archive_read_ahead(a, (size_t)tail, NULL)) == NULL)
2448 /* Boyer-Moore search backwards from the end, since we want
2449 * to match the last EOCD in the file (there can be more than
2450 * one if there is an uncompressed Zip archive as a member
2451 * within this Zip archive). */
2452 for (i = tail - 22; i > 0;) {
2455 if (memcmp(p + i, "PK\005\006", 4) == 0) {
2456 int ret = read_eocd(zip, p + i,
2457 current_offset + i);
2459 /* Zip64 EOCD locator precedes
2460 * regular EOCD if present. */
2462 && memcmp(p + i - 20, "PK\006\007", 4) == 0) {
2463 read_zip64_eocd(a, zip, p + i - 20);
2470 case 'K': i -= 1; break;
2471 case 005: i -= 2; break;
2472 case 006: i -= 3; break;
2473 default: i -= 4; break;
2479 /* The red-black trees are only used in seeking mode to manage
2480 * the in-memory copy of the central directory. */
2483 cmp_node(const struct archive_rb_node *n1, const struct archive_rb_node *n2)
2485 const struct zip_entry *e1 = (const struct zip_entry *)n1;
2486 const struct zip_entry *e2 = (const struct zip_entry *)n2;
2488 if (e1->local_header_offset > e2->local_header_offset)
2490 if (e1->local_header_offset < e2->local_header_offset)
2496 cmp_key(const struct archive_rb_node *n, const void *key)
2498 /* This function won't be called */
2499 (void)n; /* UNUSED */
2500 (void)key; /* UNUSED */
2504 static const struct archive_rb_tree_ops rb_ops = {
2509 rsrc_cmp_node(const struct archive_rb_node *n1,
2510 const struct archive_rb_node *n2)
2512 const struct zip_entry *e1 = (const struct zip_entry *)n1;
2513 const struct zip_entry *e2 = (const struct zip_entry *)n2;
2515 return (strcmp(e2->rsrcname.s, e1->rsrcname.s));
2519 rsrc_cmp_key(const struct archive_rb_node *n, const void *key)
2521 const struct zip_entry *e = (const struct zip_entry *)n;
2522 return (strcmp((const char *)key, e->rsrcname.s));
2525 static const struct archive_rb_tree_ops rb_rsrc_ops = {
2526 &rsrc_cmp_node, &rsrc_cmp_key
2530 rsrc_basename(const char *name, size_t name_length)
2536 s = memchr(s, '/', name_length - (s - name));
2545 expose_parent_dirs(struct zip *zip, const char *name, size_t name_length)
2547 struct archive_string str;
2548 struct zip_entry *dir;
2551 archive_string_init(&str);
2552 archive_strncpy(&str, name, name_length);
2554 s = strrchr(str.s, '/');
2558 /* Transfer the parent directory from zip->tree_rsrc RB
2559 * tree to zip->tree RB tree to expose. */
2560 dir = (struct zip_entry *)
2561 __archive_rb_tree_find_node(&zip->tree_rsrc, str.s);
2564 __archive_rb_tree_remove_node(&zip->tree_rsrc, &dir->node);
2565 archive_string_free(&dir->rsrcname);
2566 __archive_rb_tree_insert_node(&zip->tree, &dir->node);
2568 archive_string_free(&str);
2572 slurp_central_directory(struct archive_read *a, struct zip *zip)
2577 ssize_t bytes_avail;
2581 * Find the start of the central directory. The end-of-CD
2582 * record has our starting point, but there are lots of
2583 * Zip archives which have had other data prepended to the
2584 * file, which makes the recorded offsets all too small.
2585 * So we search forward from the specified offset until we
2586 * find the real start of the central directory. Then we
2587 * know the correction we need to apply to account for leading
2590 if (__archive_read_seek(a, zip->central_directory_offset, SEEK_SET) < 0)
2591 return ARCHIVE_FATAL;
2595 if ((p = __archive_read_ahead(a, 20, &bytes_avail)) == NULL)
2596 return ARCHIVE_FATAL;
2597 for (found = 0, i = 0; !found && i < bytes_avail - 4;) {
2599 case 'P': i += 3; break;
2600 case 'K': i += 2; break;
2601 case 001: i += 1; break;
2603 if (memcmp(p + i, "PK\001\002", 4) == 0) {
2609 case 005: i += 1; break;
2611 if (memcmp(p + i, "PK\005\006", 4) == 0) {
2614 } else if (memcmp(p + i, "PK\006\006", 4) == 0) {
2620 default: i += 4; break;
2623 __archive_read_consume(a, i);
2625 correction = archive_filter_bytes(&a->archive, 0)
2626 - zip->central_directory_offset;
2628 __archive_rb_tree_init(&zip->tree, &rb_ops);
2629 __archive_rb_tree_init(&zip->tree_rsrc, &rb_rsrc_ops);
2631 zip->central_directory_entries_total = 0;
2633 struct zip_entry *zip_entry;
2634 size_t filename_length, extra_length, comment_length;
2635 uint32_t external_attributes;
2636 const char *name, *r;
2638 if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
2639 return ARCHIVE_FATAL;
2640 if (memcmp(p, "PK\006\006", 4) == 0
2641 || memcmp(p, "PK\005\006", 4) == 0) {
2643 } else if (memcmp(p, "PK\001\002", 4) != 0) {
2644 archive_set_error(&a->archive,
2645 -1, "Invalid central directory signature");
2646 return ARCHIVE_FATAL;
2648 if ((p = __archive_read_ahead(a, 46, NULL)) == NULL)
2649 return ARCHIVE_FATAL;
2651 zip_entry = calloc(1, sizeof(struct zip_entry));
2652 zip_entry->next = zip->zip_entries;
2653 zip_entry->flags |= LA_FROM_CENTRAL_DIRECTORY;
2654 zip->zip_entries = zip_entry;
2655 zip->central_directory_entries_total++;
2657 /* version = p[4]; */
2658 zip_entry->system = p[5];
2659 /* version_required = archive_le16dec(p + 6); */
2660 zip_entry->zip_flags = archive_le16dec(p + 8);
2661 if (zip_entry->zip_flags
2662 & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)){
2663 zip->has_encrypted_entries = 1;
2665 zip_entry->compression = (char)archive_le16dec(p + 10);
2666 zip_entry->mtime = zip_time(p + 12);
2667 zip_entry->crc32 = archive_le32dec(p + 16);
2668 if (zip_entry->zip_flags & ZIP_LENGTH_AT_END)
2669 zip_entry->decdat = p[13];
2671 zip_entry->decdat = p[19];
2672 zip_entry->compressed_size = archive_le32dec(p + 20);
2673 zip_entry->uncompressed_size = archive_le32dec(p + 24);
2674 filename_length = archive_le16dec(p + 28);
2675 extra_length = archive_le16dec(p + 30);
2676 comment_length = archive_le16dec(p + 32);
2677 /* disk_start = archive_le16dec(p + 34); */ /* Better be zero. */
2678 /* internal_attributes = archive_le16dec(p + 36); */ /* text bit */
2679 external_attributes = archive_le32dec(p + 38);
2680 zip_entry->local_header_offset =
2681 archive_le32dec(p + 42) + correction;
2683 /* If we can't guess the mode, leave it zero here;
2684 when we read the local file header we might get
2685 more information. */
2686 if (zip_entry->system == 3) {
2687 zip_entry->mode = external_attributes >> 16;
2688 } else if (zip_entry->system == 0) {
2689 // Interpret MSDOS directory bit
2690 if (0x10 == (external_attributes & 0x10)) {
2691 zip_entry->mode = AE_IFDIR | 0775;
2693 zip_entry->mode = AE_IFREG | 0664;
2695 if (0x01 == (external_attributes & 0x01)) {
2696 // Read-only bit; strip write permissions
2697 zip_entry->mode &= 0555;
2700 zip_entry->mode = 0;
2703 /* We're done with the regular data; get the filename and
2705 __archive_read_consume(a, 46);
2706 p = __archive_read_ahead(a, filename_length + extra_length,
2709 archive_set_error(&a->archive,
2710 ARCHIVE_ERRNO_FILE_FORMAT,
2711 "Truncated ZIP file header");
2712 return ARCHIVE_FATAL;
2714 if (ARCHIVE_OK != process_extra(a, p + filename_length, extra_length, zip_entry)) {
2715 return ARCHIVE_FATAL;
2719 * Mac resource fork files are stored under the
2720 * "__MACOSX/" directory, so we should check if
2723 if (!zip->process_mac_extensions) {
2724 /* Treat every entry as a regular entry. */
2725 __archive_rb_tree_insert_node(&zip->tree,
2729 r = rsrc_basename(name, filename_length);
2730 if (filename_length >= 9 &&
2731 strncmp("__MACOSX/", name, 9) == 0) {
2732 /* If this file is not a resource fork nor
2733 * a directory. We should treat it as a non
2734 * resource fork file to expose it. */
2735 if (name[filename_length-1] != '/' &&
2736 (r - name < 3 || r[0] != '.' || r[1] != '_')) {
2737 __archive_rb_tree_insert_node(
2738 &zip->tree, &zip_entry->node);
2739 /* Expose its parent directories. */
2740 expose_parent_dirs(zip, name,
2743 /* This file is a resource fork file or
2745 archive_strncpy(&(zip_entry->rsrcname),
2746 name, filename_length);
2747 __archive_rb_tree_insert_node(
2748 &zip->tree_rsrc, &zip_entry->node);
2751 /* Generate resource fork name to find its
2752 * resource file at zip->tree_rsrc. */
2753 archive_strcpy(&(zip_entry->rsrcname),
2755 archive_strncat(&(zip_entry->rsrcname),
2757 archive_strcat(&(zip_entry->rsrcname), "._");
2758 archive_strncat(&(zip_entry->rsrcname),
2760 filename_length - (r - name));
2761 /* Register an entry to RB tree to sort it by
2763 __archive_rb_tree_insert_node(&zip->tree,
2768 /* Skip the comment too ... */
2769 __archive_read_consume(a,
2770 filename_length + extra_length + comment_length);
2777 zip_get_local_file_header_size(struct archive_read *a, size_t extra)
2780 ssize_t filename_length, extra_length;
2782 if ((p = __archive_read_ahead(a, extra + 30, NULL)) == NULL) {
2783 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2784 "Truncated ZIP file header");
2785 return (ARCHIVE_WARN);
2789 if (memcmp(p, "PK\003\004", 4) != 0) {
2790 archive_set_error(&a->archive, -1, "Damaged Zip archive");
2791 return ARCHIVE_WARN;
2793 filename_length = archive_le16dec(p + 26);
2794 extra_length = archive_le16dec(p + 28);
2796 return (30 + filename_length + extra_length);
2800 zip_read_mac_metadata(struct archive_read *a, struct archive_entry *entry,
2801 struct zip_entry *rsrc)
2803 struct zip *zip = (struct zip *)a->format->data;
2804 unsigned char *metadata, *mp;
2805 int64_t offset = archive_filter_bytes(&a->archive, 0);
2806 size_t remaining_bytes, metadata_bytes;
2808 int ret = ARCHIVE_OK, eof;
2810 switch(rsrc->compression) {
2811 case 0: /* No compression. */
2812 if (rsrc->uncompressed_size != rsrc->compressed_size) {
2813 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2814 "Malformed OS X metadata entry: inconsistent size");
2815 return (ARCHIVE_FATAL);
2818 case 8: /* Deflate compression. */
2821 default: /* Unsupported compression. */
2822 /* Return a warning. */
2823 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2824 "Unsupported ZIP compression method (%s)",
2825 compression_name(rsrc->compression));
2826 /* We can't decompress this entry, but we will
2827 * be able to skip() it and try the next entry. */
2828 return (ARCHIVE_WARN);
2831 if (rsrc->uncompressed_size > (4 * 1024 * 1024)) {
2832 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2833 "Mac metadata is too large: %jd > 4M bytes",
2834 (intmax_t)rsrc->uncompressed_size);
2835 return (ARCHIVE_WARN);
2837 if (rsrc->compressed_size > (4 * 1024 * 1024)) {
2838 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2839 "Mac metadata is too large: %jd > 4M bytes",
2840 (intmax_t)rsrc->compressed_size);
2841 return (ARCHIVE_WARN);
2844 metadata = malloc((size_t)rsrc->uncompressed_size);
2845 if (metadata == NULL) {
2846 archive_set_error(&a->archive, ENOMEM,
2847 "Can't allocate memory for Mac metadata");
2848 return (ARCHIVE_FATAL);
2851 if (offset < rsrc->local_header_offset)
2852 __archive_read_consume(a, rsrc->local_header_offset - offset);
2853 else if (offset != rsrc->local_header_offset) {
2854 __archive_read_seek(a, rsrc->local_header_offset, SEEK_SET);
2857 hsize = zip_get_local_file_header_size(a, 0);
2858 __archive_read_consume(a, hsize);
2860 remaining_bytes = (size_t)rsrc->compressed_size;
2861 metadata_bytes = (size_t)rsrc->uncompressed_size;
2864 while (!eof && remaining_bytes) {
2865 const unsigned char *p;
2866 ssize_t bytes_avail;
2869 p = __archive_read_ahead(a, 1, &bytes_avail);
2871 archive_set_error(&a->archive,
2872 ARCHIVE_ERRNO_FILE_FORMAT,
2873 "Truncated ZIP file header");
2875 goto exit_mac_metadata;
2877 if ((size_t)bytes_avail > remaining_bytes)
2878 bytes_avail = remaining_bytes;
2879 switch(rsrc->compression) {
2880 case 0: /* No compression. */
2881 if ((size_t)bytes_avail > metadata_bytes)
2882 bytes_avail = metadata_bytes;
2883 memcpy(mp, p, bytes_avail);
2884 bytes_used = (size_t)bytes_avail;
2885 metadata_bytes -= bytes_used;
2887 if (metadata_bytes == 0)
2891 case 8: /* Deflate compression. */
2895 ret = zip_deflate_init(a, zip);
2896 if (ret != ARCHIVE_OK)
2897 goto exit_mac_metadata;
2898 zip->stream.next_in =
2899 (Bytef *)(uintptr_t)(const void *)p;
2900 zip->stream.avail_in = (uInt)bytes_avail;
2901 zip->stream.total_in = 0;
2902 zip->stream.next_out = mp;
2903 zip->stream.avail_out = (uInt)metadata_bytes;
2904 zip->stream.total_out = 0;
2906 r = inflate(&zip->stream, 0);
2914 archive_set_error(&a->archive, ENOMEM,
2915 "Out of memory for ZIP decompression");
2916 ret = ARCHIVE_FATAL;
2917 goto exit_mac_metadata;
2919 archive_set_error(&a->archive,
2921 "ZIP decompression failed (%d)", r);
2922 ret = ARCHIVE_FATAL;
2923 goto exit_mac_metadata;
2925 bytes_used = zip->stream.total_in;
2926 metadata_bytes -= zip->stream.total_out;
2927 mp += zip->stream.total_out;
2935 __archive_read_consume(a, bytes_used);
2936 remaining_bytes -= bytes_used;
2938 archive_entry_copy_mac_metadata(entry, metadata,
2939 (size_t)rsrc->uncompressed_size - metadata_bytes);
2942 __archive_read_seek(a, offset, SEEK_SET);
2943 zip->decompress_init = 0;
2949 archive_read_format_zip_seekable_read_header(struct archive_read *a,
2950 struct archive_entry *entry)
2952 struct zip *zip = (struct zip *)a->format->data;
2953 struct zip_entry *rsrc;
2955 int r, ret = ARCHIVE_OK;
2958 * It should be sufficient to call archive_read_next_header() for
2959 * a reader to determine if an entry is encrypted or not. If the
2960 * encryption of an entry is only detectable when calling
2961 * archive_read_data(), so be it. We'll do the same check there
2964 if (zip->has_encrypted_entries ==
2965 ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW)
2966 zip->has_encrypted_entries = 0;
2968 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
2969 if (a->archive.archive_format_name == NULL)
2970 a->archive.archive_format_name = "ZIP";
2972 if (zip->zip_entries == NULL) {
2973 r = slurp_central_directory(a, zip);
2974 if (r != ARCHIVE_OK)
2976 /* Get first entry whose local header offset is lower than
2977 * other entries in the archive file. */
2979 (struct zip_entry *)ARCHIVE_RB_TREE_MIN(&zip->tree);
2980 } else if (zip->entry != NULL) {
2981 /* Get next entry in local header offset order. */
2982 zip->entry = (struct zip_entry *)__archive_rb_tree_iterate(
2983 &zip->tree, &zip->entry->node, ARCHIVE_RB_DIR_RIGHT);
2986 if (zip->entry == NULL)
2989 if (zip->entry->rsrcname.s)
2990 rsrc = (struct zip_entry *)__archive_rb_tree_find_node(
2991 &zip->tree_rsrc, zip->entry->rsrcname.s);
2995 if (zip->cctx_valid)
2996 archive_decrypto_aes_ctr_release(&zip->cctx);
2997 if (zip->hctx_valid)
2998 archive_hmac_sha1_cleanup(&zip->hctx);
2999 zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
3000 __archive_read_reset_passphrase(a);
3002 /* File entries are sorted by the header offset, we should mostly
3003 * use __archive_read_consume to advance a read point to avoid redundant
3005 offset = archive_filter_bytes(&a->archive, 0);
3006 if (offset < zip->entry->local_header_offset)
3007 __archive_read_consume(a,
3008 zip->entry->local_header_offset - offset);
3009 else if (offset != zip->entry->local_header_offset) {
3010 __archive_read_seek(a, zip->entry->local_header_offset,
3013 zip->unconsumed = 0;
3014 r = zip_read_local_file_header(a, entry, zip);
3015 if (r != ARCHIVE_OK)
3018 int ret2 = zip_read_mac_metadata(a, entry, rsrc);
3026 * We're going to seek for the next header anyway, so we don't
3027 * need to bother doing anything here.
3030 archive_read_format_zip_read_data_skip_seekable(struct archive_read *a)
3033 zip = (struct zip *)(a->format->data);
3035 zip->unconsumed = 0;
3036 return (ARCHIVE_OK);
3040 archive_read_support_format_zip_seekable(struct archive *_a)
3042 struct archive_read *a = (struct archive_read *)_a;
3046 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
3047 ARCHIVE_STATE_NEW, "archive_read_support_format_zip_seekable");
3049 zip = (struct zip *)calloc(1, sizeof(*zip));
3051 archive_set_error(&a->archive, ENOMEM,
3052 "Can't allocate zip data");
3053 return (ARCHIVE_FATAL);
3056 #ifdef HAVE_COPYFILE_H
3057 /* Set this by default on Mac OS. */
3058 zip->process_mac_extensions = 1;
3062 * Until enough data has been read, we cannot tell about
3063 * any encrypted entries yet.
3065 zip->has_encrypted_entries = ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
3066 zip->crc32func = real_crc32;
3068 r = __archive_read_register_format(a,
3071 archive_read_format_zip_seekable_bid,
3072 archive_read_format_zip_options,
3073 archive_read_format_zip_seekable_read_header,
3074 archive_read_format_zip_read_data,
3075 archive_read_format_zip_read_data_skip_seekable,
3077 archive_read_format_zip_cleanup,
3078 archive_read_support_format_zip_capabilities_seekable,
3079 archive_read_format_zip_has_encrypted_entries);
3081 if (r != ARCHIVE_OK)
3083 return (ARCHIVE_OK);