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: head/lib/libarchive/archive_read_support_format_zip.c 201102 2009-12-28 03:11:36Z kientzle $");
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. */
184 unsigned char *decrypted_buffer
;
185 unsigned char *decrypted_ptr
;
186 size_t decrypted_buffer_size
;
187 size_t decrypted_bytes_remaining
;
188 size_t decrypted_unconsumed_bytes
;
190 /* Traditional PKWARE decryption. */
191 struct trad_enc_ctx tctx
;
194 /* WinZip AES decyption. */
195 /* Contexts used for AES decryption. */
196 archive_crypto_ctx cctx
;
198 archive_hmac_sha1_ctx hctx
;
201 /* Strong encryption's decryption header information. */
214 /* Many systems define min or MIN, but not all. */
215 #define zipmin(a,b) ((a) < (b) ? (a) : (b))
217 /* ------------------------------------------------------------------------ */
220 Traditional PKWARE Decryption functions.
224 trad_enc_update_keys(struct trad_enc_ctx
*ctx
, uint8_t c
)
227 #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
229 ctx
->keys
[0] = CRC32(ctx
->keys
[0], c
);
230 ctx
->keys
[1] = (ctx
->keys
[1] + (ctx
->keys
[0] & 0xff)) * 134775813L + 1;
231 t
= (ctx
->keys
[1] >> 24) & 0xff;
232 ctx
->keys
[2] = CRC32(ctx
->keys
[2], t
);
237 trad_enc_decypt_byte(struct trad_enc_ctx
*ctx
)
239 unsigned temp
= ctx
->keys
[2] | 2;
240 return (uint8_t)((temp
* (temp
^ 1)) >> 8) & 0xff;
244 trad_enc_decrypt_update(struct trad_enc_ctx
*ctx
, const uint8_t *in
,
245 size_t in_len
, uint8_t *out
, size_t out_len
)
249 max
= (unsigned)((in_len
< out_len
)? in_len
: out_len
);
251 for (i
= 0; i
< max
; i
++) {
252 uint8_t t
= in
[i
] ^ trad_enc_decypt_byte(ctx
);
254 trad_enc_update_keys(ctx
, t
);
259 trad_enc_init(struct trad_enc_ctx
*ctx
, const char *pw
, size_t pw_len
,
260 const uint8_t *key
, size_t key_len
, uint8_t *crcchk
)
269 ctx
->keys
[0] = 305419896L;
270 ctx
->keys
[1] = 591751049L;
271 ctx
->keys
[2] = 878082192L;
273 for (;pw_len
; --pw_len
)
274 trad_enc_update_keys(ctx
, *pw
++);
276 trad_enc_decrypt_update(ctx
, key
, 12, header
, 12);
277 /* Return the last byte for CRC check. */
278 *crcchk
= header
[11];
284 crypt_derive_key_sha1(const void *p
, int size
, unsigned char *key
,
288 archive_sha1_ctx ctx
;
289 unsigned char md1
[MD_SIZE
];
290 unsigned char md2
[MD_SIZE
* 2];
291 unsigned char mkb
[64];
294 archive_sha1_init(&ctx
);
295 archive_sha1_update(&ctx
, p
, size
);
296 archive_sha1_final(&ctx
, md1
);
298 memset(mkb
, 0x36, sizeof(mkb
));
299 for (i
= 0; i
< MD_SIZE
; i
++)
301 archive_sha1_init(&ctx
);
302 archive_sha1_update(&ctx
, mkb
, sizeof(mkb
));
303 archive_sha1_final(&ctx
, md2
);
305 memset(mkb
, 0x5C, sizeof(mkb
));
306 for (i
= 0; i
< MD_SIZE
; i
++)
308 archive_sha1_init(&ctx
);
309 archive_sha1_update(&ctx
, mkb
, sizeof(mkb
));
310 archive_sha1_final(&ctx
, md2
+ MD_SIZE
);
314 memcpy(key
, md2
, key_size
);
320 * Common code for streaming or seeking modes.
322 * Includes code to read local file headers, decompress data
323 * from entry bodies, and common API.
327 real_crc32(unsigned long crc
, const void *buff
, size_t len
)
329 return crc32(crc
, buff
, (unsigned int)len
);
332 /* Used by "ignorecrc32" option to speed up tests. */
334 fake_crc32(unsigned long crc
, const void *buff
, size_t len
)
336 (void)crc
; /* UNUSED */
337 (void)buff
; /* UNUSED */
338 (void)len
; /* UNUSED */
345 } compression_methods
[] = {
346 {0, "uncompressed"}, /* The file is stored (no compression) */
347 {1, "shrinking"}, /* The file is Shrunk */
348 {2, "reduced-1"}, /* The file is Reduced with compression factor 1 */
349 {3, "reduced-2"}, /* The file is Reduced with compression factor 2 */
350 {4, "reduced-3"}, /* The file is Reduced with compression factor 3 */
351 {5, "reduced-4"}, /* The file is Reduced with compression factor 4 */
352 {6, "imploded"}, /* The file is Imploded */
353 {7, "reserved"}, /* Reserved for Tokenizing compression algorithm */
354 {8, "deflation"}, /* The file is Deflated */
355 {9, "deflation-64-bit"}, /* Enhanced Deflating using Deflate64(tm) */
356 {10, "ibm-terse"},/* PKWARE Data Compression Library Imploding
358 {11, "reserved"}, /* Reserved by PKWARE */
359 {12, "bzip"}, /* File is compressed using BZIP2 algorithm */
360 {13, "reserved"}, /* Reserved by PKWARE */
361 {14, "lzma"}, /* LZMA (EFS) */
362 {15, "reserved"}, /* Reserved by PKWARE */
363 {16, "reserved"}, /* Reserved by PKWARE */
364 {17, "reserved"}, /* Reserved by PKWARE */
365 {18, "ibm-terse-new"}, /* File is compressed using IBM TERSE (new) */
366 {19, "ibm-lz777"},/* IBM LZ77 z Architecture (PFS) */
367 {97, "wav-pack"}, /* WavPack compressed data */
368 {98, "ppmd-1"}, /* PPMd version I, Rev 1 */
369 {99, "aes"} /* WinZip AES encryption */
373 compression_name(const int compression
)
375 static const int num_compression_methods
=
376 sizeof(compression_methods
)/sizeof(compression_methods
[0]);
379 while(compression
>= 0 && i
< num_compression_methods
) {
380 if (compression_methods
[i
].id
== compression
)
381 return compression_methods
[i
].name
;
387 /* Convert an MSDOS-style date/time into Unix-style time. */
389 zip_time(const char *p
)
394 msTime
= (0xff & (unsigned)p
[0]) + 256 * (0xff & (unsigned)p
[1]);
395 msDate
= (0xff & (unsigned)p
[2]) + 256 * (0xff & (unsigned)p
[3]);
397 memset(&ts
, 0, sizeof(ts
));
398 ts
.tm_year
= ((msDate
>> 9) & 0x7f) + 80; /* Years since 1900. */
399 ts
.tm_mon
= ((msDate
>> 5) & 0x0f) - 1; /* Month number. */
400 ts
.tm_mday
= msDate
& 0x1f; /* Day of month. */
401 ts
.tm_hour
= (msTime
>> 11) & 0x1f;
402 ts
.tm_min
= (msTime
>> 5) & 0x3f;
403 ts
.tm_sec
= (msTime
<< 1) & 0x3e;
409 * The extra data is stored as a list of
410 * id1+size1+data1 + id2+size2+data2 ...
411 * triplets. id and size are 2 bytes each.
414 process_extra(const char *p
, size_t extra_length
, struct zip_entry
* zip_entry
)
418 while (offset
< extra_length
- 4) {
419 unsigned short headerid
= archive_le16dec(p
+ offset
);
420 unsigned short datasize
= archive_le16dec(p
+ offset
+ 2);
423 if (offset
+ datasize
> extra_length
) {
427 fprintf(stderr
, "Header id 0x%04x, length %d\n",
432 /* Zip64 extended information extra field. */
433 zip_entry
->flags
|= LA_USED_ZIP64
;
434 if (zip_entry
->uncompressed_size
== 0xffffffff) {
437 zip_entry
->uncompressed_size
=
438 archive_le64dec(p
+ offset
);
442 if (zip_entry
->compressed_size
== 0xffffffff) {
445 zip_entry
->compressed_size
=
446 archive_le64dec(p
+ offset
);
450 if (zip_entry
->local_header_offset
== 0xffffffff) {
453 zip_entry
->local_header_offset
=
454 archive_le64dec(p
+ offset
);
458 /* archive_le32dec(p + offset) gives disk
459 * on which file starts, but we don't handle
460 * multi-volume Zip files. */
465 /* Strong encryption field. */
466 if (archive_le16dec(p
+ offset
) == 2) {
468 archive_le16dec(p
+ offset
+ 2);
470 archive_le16dec(p
+ offset
+ 4);
472 archive_le16dec(p
+ offset
+ 6);
473 fprintf(stderr
, "algId=0x%04x, bitLen=%u, "
474 "flgas=%d\n", algId
, bitLen
,flags
);
481 /* Extended time field "UT". */
482 int flags
= p
[offset
];
485 /* Flag bits indicate which dates are present. */
489 fprintf(stderr
, "mtime: %lld -> %d\n",
490 (long long)zip_entry
->mtime
,
491 archive_le32dec(p
+ offset
));
495 zip_entry
->mtime
= archive_le32dec(p
+ offset
);
503 zip_entry
->atime
= archive_le32dec(p
+ offset
);
511 zip_entry
->ctime
= archive_le32dec(p
+ offset
);
519 /* Info-ZIP Unix Extra Field (old version) "UX". */
521 zip_entry
->atime
= archive_le32dec(p
+ offset
);
523 archive_le32dec(p
+ offset
+ 4);
525 if (datasize
>= 12) {
527 archive_le16dec(p
+ offset
+ 8);
529 archive_le16dec(p
+ offset
+ 10);
535 /* Experimental 'xl' field */
537 * Introduced Dec 2013 to provide a way to
538 * include external file attributes (and other
539 * fields that ordinarily appear only in
540 * central directory) in local file header.
541 * This provides file type and permission
542 * information necessary to support full
543 * streaming extraction. Currently being
544 * discussed with other Zip developers
545 * ... subject to change.
548 * The field starts with a bitmap that specifies
549 * which additional fields are included. The
550 * bitmap is variable length and can be extended in
553 * n bytes - feature bitmap: first byte has low-order
554 * 7 bits. If high-order bit is set, a subsequent
555 * byte holds the next 7 bits, etc.
557 * if bitmap & 1, 2 byte "version made by"
558 * if bitmap & 2, 2 byte "internal file attributes"
559 * if bitmap & 4, 4 byte "external file attributes"
560 * if bitmap & 8, 2 byte comment length + n byte comment
562 int bitmap
, bitmap_last
;
566 bitmap_last
= bitmap
= 0xff & p
[offset
];
570 /* We only support first 7 bits of bitmap; skip rest. */
571 while ((bitmap_last
& 0x80) != 0
573 bitmap_last
= p
[offset
];
579 /* 2 byte "version made by" */
583 = archive_le16dec(p
+ offset
) >> 8;
588 /* 2 byte "internal file attributes" */
589 uint32_t internal_attributes
;
593 = archive_le16dec(p
+ offset
);
594 /* Not used by libarchive at present. */
595 (void)internal_attributes
; /* UNUSED */
600 /* 4 byte "external file attributes" */
601 uint32_t external_attributes
;
605 = archive_le32dec(p
+ offset
);
606 if (zip_entry
->system
== 3) {
608 = external_attributes
>> 16;
609 } else if (zip_entry
->system
== 0) {
610 // Interpret MSDOS directory bit
611 if (0x10 == (external_attributes
& 0x10)) {
612 zip_entry
->mode
= AE_IFDIR
| 0775;
614 zip_entry
->mode
= AE_IFREG
| 0664;
616 if (0x01 == (external_attributes
& 0x01)) {
617 // Read-only bit; strip write permissions
618 zip_entry
->mode
&= 0555;
627 /* 2 byte comment length + comment */
628 uint32_t comment_length
;
632 = archive_le16dec(p
+ offset
);
636 if (datasize
< comment_length
)
638 /* Comment is not supported by libarchive */
639 offset
+= comment_length
;
640 datasize
-= comment_length
;
645 /* Info-ZIP Unix Extra Field (type 2) "Ux". */
647 fprintf(stderr
, "uid %d gid %d\n",
648 archive_le16dec(p
+ offset
),
649 archive_le16dec(p
+ offset
+ 2));
652 zip_entry
->uid
= archive_le16dec(p
+ offset
);
655 archive_le16dec(p
+ offset
+ 2);
659 /* Info-Zip Unix Extra Field (type 3) "ux". */
660 int uidsize
= 0, gidsize
= 0;
662 /* TODO: support arbitrary uidsize/gidsize. */
663 if (datasize
>= 1 && p
[offset
] == 1) {/* version=1 */
665 /* get a uid size. */
666 uidsize
= 0xff & (int)p
[offset
+1];
671 else if (uidsize
== 4 && datasize
>= 6)
676 if (datasize
>= (2 + uidsize
+ 3)) {
677 /* get a gid size. */
678 gidsize
= 0xff & (int)p
[offset
+2+uidsize
];
682 p
+offset
+2+uidsize
+1);
683 else if (gidsize
== 4 &&
684 datasize
>= (2 + uidsize
+ 5))
687 p
+offset
+2+uidsize
+1);
693 /* WinZIp AES extra data field. */
694 if (p
[offset
+ 2] == 'A' && p
[offset
+ 3] == 'E') {
695 /* Vendor version. */
696 zip_entry
->aes_extra
.vendor
=
697 archive_le16dec(p
+ offset
);
698 /* AES encryption strength. */
699 zip_entry
->aes_extra
.strength
= p
[offset
+ 4];
700 /* Actual compression method. */
701 zip_entry
->aes_extra
.compression
=
711 if (offset
!= extra_length
)
714 "Extra data field contents do not match reported size!\n");
720 * Assumes file pointer is at beginning of local file header.
723 zip_read_local_file_header(struct archive_read
*a
, struct archive_entry
*entry
,
730 size_t len
, filename_length
, extra_length
;
731 struct archive_string_conv
*sconv
;
732 struct zip_entry
*zip_entry
= zip
->entry
;
733 struct zip_entry zip_entry_central_dir
;
734 int ret
= ARCHIVE_OK
;
737 /* Save a copy of the original for consistency checks. */
738 zip_entry_central_dir
= *zip_entry
;
740 zip
->decompress_init
= 0;
741 zip
->end_of_entry
= 0;
742 zip
->entry_uncompressed_bytes_read
= 0;
743 zip
->entry_compressed_bytes_read
= 0;
744 zip
->entry_crc32
= zip
->crc32func(0, NULL
, 0);
746 /* Setup default conversion. */
747 if (zip
->sconv
== NULL
&& !zip
->init_default_conversion
) {
749 archive_string_default_conversion_for_read(&(a
->archive
));
750 zip
->init_default_conversion
= 1;
753 if ((p
= __archive_read_ahead(a
, 30, NULL
)) == NULL
) {
754 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
755 "Truncated ZIP file header");
756 return (ARCHIVE_FATAL
);
759 if (memcmp(p
, "PK\003\004", 4) != 0) {
760 archive_set_error(&a
->archive
, -1, "Damaged Zip archive");
761 return ARCHIVE_FATAL
;
764 zip_entry
->system
= p
[5];
765 zip_entry
->zip_flags
= archive_le16dec(p
+ 6);
766 if (zip_entry
->zip_flags
& (ZIP_ENCRYPTED
| ZIP_STRONG_ENCRYPTED
)) {
767 zip
->has_encrypted_entries
= 1;
768 archive_entry_set_is_data_encrypted(entry
, 1);
769 if (zip_entry
->zip_flags
& ZIP_CENTRAL_DIRECTORY_ENCRYPTED
&&
770 zip_entry
->zip_flags
& ZIP_ENCRYPTED
&&
771 zip_entry
->zip_flags
& ZIP_STRONG_ENCRYPTED
) {
772 archive_entry_set_is_metadata_encrypted(entry
, 1);
773 return ARCHIVE_FATAL
;
776 zip
->init_decryption
= (zip_entry
->zip_flags
& ZIP_ENCRYPTED
);
777 zip_entry
->compression
= (char)archive_le16dec(p
+ 8);
778 zip_entry
->mtime
= zip_time(p
+ 10);
779 zip_entry
->crc32
= archive_le32dec(p
+ 14);
780 if (zip_entry
->zip_flags
& ZIP_LENGTH_AT_END
)
781 zip_entry
->decdat
= p
[11];
783 zip_entry
->decdat
= p
[17];
784 zip_entry
->compressed_size
= archive_le32dec(p
+ 18);
785 zip_entry
->uncompressed_size
= archive_le32dec(p
+ 22);
786 filename_length
= archive_le16dec(p
+ 26);
787 extra_length
= archive_le16dec(p
+ 28);
789 __archive_read_consume(a
, 30);
791 /* Read the filename. */
792 if ((h
= __archive_read_ahead(a
, filename_length
, NULL
)) == NULL
) {
793 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
794 "Truncated ZIP file header");
795 return (ARCHIVE_FATAL
);
797 if (zip_entry
->zip_flags
& ZIP_UTF8_NAME
) {
798 /* The filename is stored to be UTF-8. */
799 if (zip
->sconv_utf8
== NULL
) {
801 archive_string_conversion_from_charset(
802 &a
->archive
, "UTF-8", 1);
803 if (zip
->sconv_utf8
== NULL
)
804 return (ARCHIVE_FATAL
);
806 sconv
= zip
->sconv_utf8
;
807 } else if (zip
->sconv
!= NULL
)
810 sconv
= zip
->sconv_default
;
812 if (archive_entry_copy_pathname_l(entry
,
813 h
, filename_length
, sconv
) != 0) {
814 if (errno
== ENOMEM
) {
815 archive_set_error(&a
->archive
, ENOMEM
,
816 "Can't allocate memory for Pathname");
817 return (ARCHIVE_FATAL
);
819 archive_set_error(&a
->archive
,
820 ARCHIVE_ERRNO_FILE_FORMAT
,
821 "Pathname cannot be converted "
822 "from %s to current locale.",
823 archive_string_conversion_charset_name(sconv
));
826 __archive_read_consume(a
, filename_length
);
828 /* Read the extra data. */
829 if ((h
= __archive_read_ahead(a
, extra_length
, NULL
)) == NULL
) {
830 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
831 "Truncated ZIP file header");
832 return (ARCHIVE_FATAL
);
835 process_extra(h
, extra_length
, zip_entry
);
836 __archive_read_consume(a
, extra_length
);
838 /* Work around a bug in Info-Zip: When reading from a pipe, it
839 * stats the pipe instead of synthesizing a file entry. */
840 if ((zip_entry
->mode
& AE_IFMT
) == AE_IFIFO
) {
841 zip_entry
->mode
&= ~ AE_IFMT
;
842 zip_entry
->mode
|= AE_IFREG
;
845 if ((zip_entry
->mode
& AE_IFMT
) == 0) {
846 /* Especially in streaming mode, we can end up
847 here without having seen proper mode information.
848 Guess from the filename. */
849 wp
= archive_entry_pathname_w(entry
);
852 if (len
> 0 && wp
[len
- 1] == L
'/')
853 zip_entry
->mode
|= AE_IFDIR
;
855 zip_entry
->mode
|= AE_IFREG
;
857 cp
= archive_entry_pathname(entry
);
858 len
= (cp
!= NULL
)?strlen(cp
):0;
859 if (len
> 0 && cp
[len
- 1] == '/')
860 zip_entry
->mode
|= AE_IFDIR
;
862 zip_entry
->mode
|= AE_IFREG
;
864 if (zip_entry
->mode
== AE_IFDIR
) {
865 zip_entry
->mode
|= 0775;
866 } else if (zip_entry
->mode
== AE_IFREG
) {
867 zip_entry
->mode
|= 0664;
871 /* Make sure directories end in '/' */
872 if ((zip_entry
->mode
& AE_IFMT
) == AE_IFDIR
) {
873 wp
= archive_entry_pathname_w(entry
);
876 if (len
> 0 && wp
[len
- 1] != L
'/') {
877 struct archive_wstring s
;
878 archive_string_init(&s
);
879 archive_wstrcat(&s
, wp
);
880 archive_wstrappend_wchar(&s
, L
'/');
881 archive_entry_copy_pathname_w(entry
, s
.s
);
884 cp
= archive_entry_pathname(entry
);
885 len
= (cp
!= NULL
)?strlen(cp
):0;
886 if (len
> 0 && cp
[len
- 1] != '/') {
887 struct archive_string s
;
888 archive_string_init(&s
);
889 archive_strcat(&s
, cp
);
890 archive_strappend_char(&s
, '/');
891 archive_entry_set_pathname(entry
, s
.s
);
896 if (zip_entry
->flags
& LA_FROM_CENTRAL_DIRECTORY
) {
897 /* If this came from the central dir, it's size info
898 * is definitive, so ignore the length-at-end flag. */
899 zip_entry
->zip_flags
&= ~ZIP_LENGTH_AT_END
;
900 /* If local header is missing a value, use the one from
901 the central directory. If both have it, warn about
903 if (zip_entry
->crc32
== 0) {
904 zip_entry
->crc32
= zip_entry_central_dir
.crc32
;
905 } else if (!zip
->ignore_crc32
906 && zip_entry
->crc32
!= zip_entry_central_dir
.crc32
) {
907 archive_set_error(&a
->archive
,
908 ARCHIVE_ERRNO_FILE_FORMAT
,
909 "Inconsistent CRC32 values");
912 if (zip_entry
->compressed_size
== 0) {
913 zip_entry
->compressed_size
914 = zip_entry_central_dir
.compressed_size
;
915 } else if (zip_entry
->compressed_size
916 != zip_entry_central_dir
.compressed_size
) {
917 archive_set_error(&a
->archive
,
918 ARCHIVE_ERRNO_FILE_FORMAT
,
919 "Inconsistent compressed size: "
920 "%jd in central directory, %jd in local header",
921 (intmax_t)zip_entry_central_dir
.compressed_size
,
922 (intmax_t)zip_entry
->compressed_size
);
925 if (zip_entry
->uncompressed_size
== 0) {
926 zip_entry
->uncompressed_size
927 = zip_entry_central_dir
.uncompressed_size
;
928 } else if (zip_entry
->uncompressed_size
929 != zip_entry_central_dir
.uncompressed_size
) {
930 archive_set_error(&a
->archive
,
931 ARCHIVE_ERRNO_FILE_FORMAT
,
932 "Inconsistent uncompressed size: "
933 "%jd in central directory, %jd in local header",
934 (intmax_t)zip_entry_central_dir
.uncompressed_size
,
935 (intmax_t)zip_entry
->uncompressed_size
);
940 /* Populate some additional entry fields: */
941 archive_entry_set_mode(entry
, zip_entry
->mode
);
942 archive_entry_set_uid(entry
, zip_entry
->uid
);
943 archive_entry_set_gid(entry
, zip_entry
->gid
);
944 archive_entry_set_mtime(entry
, zip_entry
->mtime
, 0);
945 archive_entry_set_ctime(entry
, zip_entry
->ctime
, 0);
946 archive_entry_set_atime(entry
, zip_entry
->atime
, 0);
948 if ((zip
->entry
->mode
& AE_IFMT
) == AE_IFLNK
) {
949 size_t linkname_length
;
951 if (zip_entry
->compressed_size
> 64 * 1024) {
952 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
953 "Zip file with oversized link entry");
954 return ARCHIVE_FATAL
;
957 linkname_length
= (size_t)zip_entry
->compressed_size
;
959 archive_entry_set_size(entry
, 0);
960 p
= __archive_read_ahead(a
, linkname_length
, NULL
);
962 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
963 "Truncated Zip file");
964 return ARCHIVE_FATAL
;
968 if (sconv
== NULL
&& (zip
->entry
->zip_flags
& ZIP_UTF8_NAME
))
969 sconv
= zip
->sconv_utf8
;
971 sconv
= zip
->sconv_default
;
972 if (archive_entry_copy_symlink_l(entry
, p
, linkname_length
,
974 if (errno
!= ENOMEM
&& sconv
== zip
->sconv_utf8
&&
975 (zip
->entry
->zip_flags
& ZIP_UTF8_NAME
))
976 archive_entry_copy_symlink_l(entry
, p
,
977 linkname_length
, NULL
);
978 if (errno
== ENOMEM
) {
979 archive_set_error(&a
->archive
, ENOMEM
,
980 "Can't allocate memory for Symlink");
981 return (ARCHIVE_FATAL
);
984 * Since there is no character-set regulation for
985 * symlink name, do not report the conversion error
986 * in an automatic conversion.
988 if (sconv
!= zip
->sconv_utf8
||
989 (zip
->entry
->zip_flags
& ZIP_UTF8_NAME
) == 0) {
990 archive_set_error(&a
->archive
,
991 ARCHIVE_ERRNO_FILE_FORMAT
,
992 "Symlink cannot be converted "
993 "from %s to current locale.",
994 archive_string_conversion_charset_name(
999 zip_entry
->uncompressed_size
= zip_entry
->compressed_size
= 0;
1001 if (__archive_read_consume(a
, linkname_length
) < 0) {
1002 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1003 "Read error skipping symlink target name");
1004 return ARCHIVE_FATAL
;
1006 } else if (0 == (zip_entry
->zip_flags
& ZIP_LENGTH_AT_END
)
1007 || zip_entry
->uncompressed_size
> 0) {
1008 /* Set the size only if it's meaningful. */
1009 archive_entry_set_size(entry
, zip_entry
->uncompressed_size
);
1011 zip
->entry_bytes_remaining
= zip_entry
->compressed_size
;
1013 /* If there's no body, force read_data() to return EOF immediately. */
1014 if (0 == (zip_entry
->zip_flags
& ZIP_LENGTH_AT_END
)
1015 && zip
->entry_bytes_remaining
< 1)
1016 zip
->end_of_entry
= 1;
1018 /* Set up a more descriptive format name. */
1019 archive_string_sprintf(&zip
->format_name
, "ZIP %d.%d (%s)",
1020 version
/ 10, version
% 10,
1021 compression_name(zip
->entry
->compression
));
1022 a
->archive
.archive_format_name
= zip
->format_name
.s
;
1028 check_authentication_code(struct archive_read
*a
, const void *_p
)
1030 struct zip
*zip
= (struct zip
*)(a
->format
->data
);
1032 /* Check authentication code. */
1033 if (zip
->hctx_valid
) {
1036 size_t hmac_len
= 20;
1039 archive_hmac_sha1_final(&zip
->hctx
, hmac
, &hmac_len
);
1041 /* Read authentication code. */
1042 p
= __archive_read_ahead(a
, AUTH_CODE_SIZE
, NULL
);
1044 archive_set_error(&a
->archive
,
1045 ARCHIVE_ERRNO_FILE_FORMAT
,
1046 "Truncated ZIP file data");
1047 return (ARCHIVE_FATAL
);
1052 cmp
= memcmp(hmac
, p
, AUTH_CODE_SIZE
);
1053 __archive_read_consume(a
, AUTH_CODE_SIZE
);
1055 archive_set_error(&a
->archive
,
1057 "ZIP bad Authentication code");
1058 return (ARCHIVE_WARN
);
1061 return (ARCHIVE_OK
);
1065 * Read "uncompressed" data. There are three cases:
1066 * 1) We know the size of the data. This is always true for the
1067 * seeking reader (we've examined the Central Directory already).
1068 * 2) ZIP_LENGTH_AT_END was set, but only the CRC was deferred.
1069 * Info-ZIP seems to do this; we know the size but have to grab
1070 * the CRC from the data descriptor afterwards.
1071 * 3) We're streaming and ZIP_LENGTH_AT_END was specified and
1072 * we have no size information. In this case, we can do pretty
1073 * well by watching for the data descriptor record. The data
1074 * descriptor is 16 bytes and includes a computed CRC that should
1075 * provide a strong check.
1077 * TODO: Technically, the PK\007\010 signature is optional.
1078 * In the original spec, the data descriptor contained CRC
1079 * and size fields but had no leading signature. In practice,
1080 * newer writers seem to provide the signature pretty consistently.
1082 * For uncompressed data, the PK\007\010 marker seems essential
1083 * to be sure we've actually seen the end of the entry.
1085 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
1086 * zip->end_of_entry if it consumes all of the data.
1089 zip_read_data_none(struct archive_read
*a
, const void **_buff
,
1090 size_t *size
, int64_t *offset
)
1094 ssize_t bytes_avail
;
1097 (void)offset
; /* UNUSED */
1099 zip
= (struct zip
*)(a
->format
->data
);
1101 if (zip
->entry
->zip_flags
& ZIP_LENGTH_AT_END
) {
1103 ssize_t grabbing_bytes
= 24;
1105 if (zip
->hctx_valid
)
1106 grabbing_bytes
+= AUTH_CODE_SIZE
;
1107 /* Grab at least 24 bytes. */
1108 buff
= __archive_read_ahead(a
, grabbing_bytes
, &bytes_avail
);
1109 if (bytes_avail
< grabbing_bytes
) {
1110 /* Zip archives have end-of-archive markers
1111 that are longer than this, so a failure to get at
1112 least 24 bytes really does indicate a truncated
1114 archive_set_error(&a
->archive
,
1115 ARCHIVE_ERRNO_FILE_FORMAT
,
1116 "Truncated ZIP file data");
1117 return (ARCHIVE_FATAL
);
1119 /* Check for a complete PK\007\010 signature, followed
1120 * by the correct 4-byte CRC. */
1122 if (zip
->hctx_valid
)
1123 p
+= AUTH_CODE_SIZE
;
1124 if (p
[0] == 'P' && p
[1] == 'K'
1125 && p
[2] == '\007' && p
[3] == '\010'
1126 && (archive_le32dec(p
+ 4) == zip
->entry_crc32
1127 || zip
->ignore_crc32
1129 && zip
->entry
->aes_extra
.vendor
== AES_VENDOR_AE_2
))) {
1130 if (zip
->entry
->flags
& LA_USED_ZIP64
) {
1131 zip
->entry
->crc32
= archive_le32dec(p
+ 4);
1132 zip
->entry
->compressed_size
=
1133 archive_le64dec(p
+ 8);
1134 zip
->entry
->uncompressed_size
=
1135 archive_le64dec(p
+ 16);
1136 zip
->unconsumed
= 24;
1138 zip
->entry
->crc32
= archive_le32dec(p
+ 4);
1139 zip
->entry
->compressed_size
=
1140 archive_le32dec(p
+ 8);
1141 zip
->entry
->uncompressed_size
=
1142 archive_le32dec(p
+ 12);
1143 zip
->unconsumed
= 16;
1145 if (zip
->hctx_valid
) {
1146 r
= check_authentication_code(a
, buff
);
1147 if (r
!= ARCHIVE_OK
)
1150 zip
->end_of_entry
= 1;
1151 return (ARCHIVE_OK
);
1153 /* If not at EOF, ensure we consume at least one byte. */
1156 /* Scan forward until we see where a PK\007\010 signature
1158 /* Return bytes up until that point. On the next call,
1159 * the code above will verify the data descriptor. */
1160 while (p
< buff
+ bytes_avail
- 4) {
1161 if (p
[3] == 'P') { p
+= 3; }
1162 else if (p
[3] == 'K') { p
+= 2; }
1163 else if (p
[3] == '\007') { p
+= 1; }
1164 else if (p
[3] == '\010' && p
[2] == '\007'
1165 && p
[1] == 'K' && p
[0] == 'P') {
1166 if (zip
->hctx_valid
)
1167 p
-= AUTH_CODE_SIZE
;
1171 bytes_avail
= p
- buff
;
1173 if (zip
->entry_bytes_remaining
== 0) {
1174 zip
->end_of_entry
= 1;
1175 if (zip
->hctx_valid
) {
1176 r
= check_authentication_code(a
, NULL
);
1177 if (r
!= ARCHIVE_OK
)
1180 return (ARCHIVE_OK
);
1182 /* Grab a bunch of bytes. */
1183 buff
= __archive_read_ahead(a
, 1, &bytes_avail
);
1184 if (bytes_avail
<= 0) {
1185 archive_set_error(&a
->archive
,
1186 ARCHIVE_ERRNO_FILE_FORMAT
,
1187 "Truncated ZIP file data");
1188 return (ARCHIVE_FATAL
);
1190 if (bytes_avail
> zip
->entry_bytes_remaining
)
1191 bytes_avail
= (ssize_t
)zip
->entry_bytes_remaining
;
1193 if (zip
->tctx_valid
|| zip
->cctx_valid
) {
1194 size_t dec_size
= bytes_avail
;
1196 if (dec_size
> zip
->decrypted_buffer_size
)
1197 dec_size
= zip
->decrypted_buffer_size
;
1198 if (zip
->tctx_valid
) {
1199 trad_enc_decrypt_update(&zip
->tctx
,
1200 (const uint8_t *)buff
, dec_size
,
1201 zip
->decrypted_buffer
, dec_size
);
1203 size_t dsize
= dec_size
;
1204 archive_hmac_sha1_update(&zip
->hctx
,
1205 (const uint8_t *)buff
, dec_size
);
1206 archive_decrypto_aes_ctr_update(&zip
->cctx
,
1207 (const uint8_t *)buff
, dec_size
,
1208 zip
->decrypted_buffer
, &dsize
);
1210 bytes_avail
= dec_size
;
1211 buff
= (const char *)zip
->decrypted_buffer
;
1213 *size
= bytes_avail
;
1214 zip
->entry_bytes_remaining
-= bytes_avail
;
1215 zip
->entry_uncompressed_bytes_read
+= bytes_avail
;
1216 zip
->entry_compressed_bytes_read
+= bytes_avail
;
1217 zip
->unconsumed
+= bytes_avail
;
1219 return (ARCHIVE_OK
);
1224 zip_deflate_init(struct archive_read
*a
, struct zip
*zip
)
1228 /* If we haven't yet read any data, initialize the decompressor. */
1229 if (!zip
->decompress_init
) {
1230 if (zip
->stream_valid
)
1231 r
= inflateReset(&zip
->stream
);
1233 r
= inflateInit2(&zip
->stream
,
1234 -15 /* Don't check for zlib header */);
1236 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1237 "Can't initialize ZIP decompression.");
1238 return (ARCHIVE_FATAL
);
1240 /* Stream structure has been set up. */
1241 zip
->stream_valid
= 1;
1242 /* We've initialized decompression for this stream. */
1243 zip
->decompress_init
= 1;
1245 return (ARCHIVE_OK
);
1249 zip_read_data_deflate(struct archive_read
*a
, const void **buff
,
1250 size_t *size
, int64_t *offset
)
1253 ssize_t bytes_avail
;
1254 const void *compressed_buff
, *sp
;
1257 (void)offset
; /* UNUSED */
1259 zip
= (struct zip
*)(a
->format
->data
);
1261 /* If the buffer hasn't been allocated, allocate it now. */
1262 if (zip
->uncompressed_buffer
== NULL
) {
1263 zip
->uncompressed_buffer_size
= 256 * 1024;
1264 zip
->uncompressed_buffer
1265 = (unsigned char *)malloc(zip
->uncompressed_buffer_size
);
1266 if (zip
->uncompressed_buffer
== NULL
) {
1267 archive_set_error(&a
->archive
, ENOMEM
,
1268 "No memory for ZIP decompression");
1269 return (ARCHIVE_FATAL
);
1273 r
= zip_deflate_init(a
, zip
);
1274 if (r
!= ARCHIVE_OK
)
1278 * Note: '1' here is a performance optimization.
1279 * Recall that the decompression layer returns a count of
1280 * available bytes; asking for more than that forces the
1281 * decompressor to combine reads by copying data.
1283 compressed_buff
= sp
= __archive_read_ahead(a
, 1, &bytes_avail
);
1284 if (0 == (zip
->entry
->zip_flags
& ZIP_LENGTH_AT_END
)
1285 && bytes_avail
> zip
->entry_bytes_remaining
) {
1286 bytes_avail
= (ssize_t
)zip
->entry_bytes_remaining
;
1288 if (bytes_avail
<= 0) {
1289 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1290 "Truncated ZIP file body");
1291 return (ARCHIVE_FATAL
);
1294 if (zip
->tctx_valid
|| zip
->cctx_valid
) {
1295 if (zip
->decrypted_bytes_remaining
< (size_t)bytes_avail
) {
1296 size_t buff_remaining
= zip
->decrypted_buffer_size
1297 - (zip
->decrypted_ptr
- zip
->decrypted_buffer
);
1299 if (buff_remaining
> (size_t)bytes_avail
)
1300 buff_remaining
= (size_t)bytes_avail
;
1302 if (0 == (zip
->entry
->zip_flags
& ZIP_LENGTH_AT_END
) &&
1303 zip
->entry_bytes_remaining
> 0) {
1304 if ((int64_t)(zip
->decrypted_bytes_remaining
1306 > zip
->entry_bytes_remaining
) {
1307 if (zip
->entry_bytes_remaining
<
1308 (int64_t)zip
->decrypted_bytes_remaining
)
1312 (size_t)zip
->entry_bytes_remaining
1313 - zip
->decrypted_bytes_remaining
;
1316 if (buff_remaining
> 0) {
1317 if (zip
->tctx_valid
) {
1318 trad_enc_decrypt_update(&zip
->tctx
,
1319 compressed_buff
, buff_remaining
,
1321 + zip
->decrypted_bytes_remaining
,
1324 size_t dsize
= buff_remaining
;
1325 archive_decrypto_aes_ctr_update(
1327 compressed_buff
, buff_remaining
,
1329 + zip
->decrypted_bytes_remaining
,
1332 zip
->decrypted_bytes_remaining
+= buff_remaining
;
1335 bytes_avail
= zip
->decrypted_bytes_remaining
;
1336 compressed_buff
= (const char *)zip
->decrypted_ptr
;
1340 * A bug in zlib.h: stream.next_in should be marked 'const'
1341 * but isn't (the library never alters data through the
1342 * next_in pointer, only reads it). The result: this ugly
1343 * cast to remove 'const'.
1345 zip
->stream
.next_in
= (Bytef
*)(uintptr_t)(const void *)compressed_buff
;
1346 zip
->stream
.avail_in
= (uInt
)bytes_avail
;
1347 zip
->stream
.total_in
= 0;
1348 zip
->stream
.next_out
= zip
->uncompressed_buffer
;
1349 zip
->stream
.avail_out
= (uInt
)zip
->uncompressed_buffer_size
;
1350 zip
->stream
.total_out
= 0;
1352 r
= inflate(&zip
->stream
, 0);
1357 zip
->end_of_entry
= 1;
1360 archive_set_error(&a
->archive
, ENOMEM
,
1361 "Out of memory for ZIP decompression");
1362 return (ARCHIVE_FATAL
);
1364 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1365 "ZIP decompression failed (%d)", r
);
1366 return (ARCHIVE_FATAL
);
1369 /* Consume as much as the compressor actually used. */
1370 bytes_avail
= zip
->stream
.total_in
;
1371 if (zip
->tctx_valid
|| zip
->cctx_valid
) {
1372 zip
->decrypted_bytes_remaining
-= bytes_avail
;
1373 if (zip
->decrypted_bytes_remaining
== 0)
1374 zip
->decrypted_ptr
= zip
->decrypted_buffer
;
1376 zip
->decrypted_ptr
+= bytes_avail
;
1378 /* Calculate compressed data as much as we used.*/
1379 if (zip
->hctx_valid
)
1380 archive_hmac_sha1_update(&zip
->hctx
, sp
, bytes_avail
);
1381 __archive_read_consume(a
, bytes_avail
);
1382 zip
->entry_bytes_remaining
-= bytes_avail
;
1383 zip
->entry_compressed_bytes_read
+= bytes_avail
;
1385 *size
= zip
->stream
.total_out
;
1386 zip
->entry_uncompressed_bytes_read
+= zip
->stream
.total_out
;
1387 *buff
= zip
->uncompressed_buffer
;
1389 if (zip
->end_of_entry
&& zip
->hctx_valid
) {
1390 r
= check_authentication_code(a
, NULL
);
1391 if (r
!= ARCHIVE_OK
)
1395 if (zip
->end_of_entry
&& (zip
->entry
->zip_flags
& ZIP_LENGTH_AT_END
)) {
1398 if (NULL
== (p
= __archive_read_ahead(a
, 24, NULL
))) {
1399 archive_set_error(&a
->archive
,
1400 ARCHIVE_ERRNO_FILE_FORMAT
,
1401 "Truncated ZIP end-of-file record");
1402 return (ARCHIVE_FATAL
);
1404 /* Consume the optional PK\007\010 marker. */
1405 if (p
[0] == 'P' && p
[1] == 'K' &&
1406 p
[2] == '\007' && p
[3] == '\010') {
1408 zip
->unconsumed
= 4;
1410 if (zip
->entry
->flags
& LA_USED_ZIP64
) {
1411 zip
->entry
->crc32
= archive_le32dec(p
);
1412 zip
->entry
->compressed_size
= archive_le64dec(p
+ 4);
1413 zip
->entry
->uncompressed_size
= archive_le64dec(p
+ 12);
1414 zip
->unconsumed
+= 20;
1416 zip
->entry
->crc32
= archive_le32dec(p
);
1417 zip
->entry
->compressed_size
= archive_le32dec(p
+ 4);
1418 zip
->entry
->uncompressed_size
= archive_le32dec(p
+ 8);
1419 zip
->unconsumed
+= 12;
1423 return (ARCHIVE_OK
);
1428 read_decryption_header(struct archive_read
*a
)
1430 struct zip
*zip
= (struct zip
*)(a
->format
->data
);
1432 unsigned int remaining_size
;
1436 * Read an initialization vector data field.
1438 p
= __archive_read_ahead(a
, 2, NULL
);
1442 zip
->iv_size
= archive_le16dec(p
);
1443 __archive_read_consume(a
, 2);
1444 if (ts
< zip
->iv_size
) {
1448 p
= __archive_read_ahead(a
, zip
->iv_size
, NULL
);
1451 if (zip
->iv
== NULL
) {
1452 zip
->iv
= malloc(zip
->iv_size
);
1453 if (zip
->iv
== NULL
)
1456 memcpy(zip
->iv
, p
, zip
->iv_size
);
1457 __archive_read_consume(a
, zip
->iv_size
);
1460 * Read a size of remaining decryption header field.
1462 p
= __archive_read_ahead(a
, 14, NULL
);
1465 remaining_size
= archive_le32dec(p
);
1466 if (remaining_size
< 16 || remaining_size
> (1 << 18))
1469 /* Check if format version is supported. */
1470 if (archive_le16dec(p
+4) != 3) {
1471 archive_set_error(&a
->archive
,
1472 ARCHIVE_ERRNO_FILE_FORMAT
,
1473 "Unsupported encryption format version: %u",
1474 archive_le16dec(p
+4));
1475 return (ARCHIVE_FAILED
);
1479 * Read an encryption algorithm field.
1481 zip
->alg_id
= archive_le16dec(p
+6);
1482 switch (zip
->alg_id
) {
1483 case 0x6601:/* DES */
1484 case 0x6602:/* RC2 */
1485 case 0x6603:/* 3DES 168 */
1486 case 0x6609:/* 3DES 112 */
1487 case 0x660E:/* AES 128 */
1488 case 0x660F:/* AES 192 */
1489 case 0x6610:/* AES 256 */
1490 case 0x6702:/* RC2 (version >= 5.2) */
1491 case 0x6720:/* Blowfish */
1492 case 0x6721:/* Twofish */
1493 case 0x6801:/* RC4 */
1494 /* Suuported encryption algorithm. */
1497 archive_set_error(&a
->archive
,
1498 ARCHIVE_ERRNO_FILE_FORMAT
,
1499 "Unknown encryption algorithm: %u", zip
->alg_id
);
1500 return (ARCHIVE_FAILED
);
1504 * Read a bit length field.
1506 zip
->bit_len
= archive_le16dec(p
+8);
1509 * Read a flags field.
1511 zip
->flags
= archive_le16dec(p
+10);
1512 switch (zip
->flags
& 0xf000) {
1513 case 0x0001: /* Password is required to decrypt. */
1514 case 0x0002: /* Certificates only. */
1515 case 0x0003: /* Password or certificate required to decrypt. */
1518 archive_set_error(&a
->archive
,
1519 ARCHIVE_ERRNO_FILE_FORMAT
,
1520 "Unknown encryption flag: %u", zip
->flags
);
1521 return (ARCHIVE_FAILED
);
1523 if ((zip
->flags
& 0xf000) == 0 ||
1524 (zip
->flags
& 0xf000) == 0x4000) {
1525 archive_set_error(&a
->archive
,
1526 ARCHIVE_ERRNO_FILE_FORMAT
,
1527 "Unknown encryption flag: %u", zip
->flags
);
1528 return (ARCHIVE_FAILED
);
1532 * Read an encrypted random data field.
1535 zip
->erd_size
= archive_le16dec(p
+12);
1536 __archive_read_consume(a
, 14);
1537 if ((zip
->erd_size
& 0xf) != 0 ||
1538 (zip
->erd_size
+ 16) > remaining_size
||
1539 (zip
->erd_size
+ 16) < zip
->erd_size
)
1542 if (ts
< zip
->erd_size
) {
1546 p
= __archive_read_ahead(a
, zip
->erd_size
, NULL
);
1549 if (zip
->erd
== NULL
) {
1550 zip
->erd
= malloc(zip
->erd_size
);
1551 if (zip
->erd
== NULL
)
1554 memcpy(zip
->erd
, p
, zip
->erd_size
);
1555 __archive_read_consume(a
, zip
->erd_size
);
1558 * Read a reserved data field.
1560 p
= __archive_read_ahead(a
, 4, NULL
);
1563 /* Reserved data size should be zero. */
1564 if (archive_le32dec(p
) != 0)
1566 __archive_read_consume(a
, 4);
1569 * Read a password validation data field.
1571 p
= __archive_read_ahead(a
, 2, NULL
);
1575 zip
->v_size
= archive_le16dec(p
);
1576 __archive_read_consume(a
, 2);
1577 if ((zip
->v_size
& 0x0f) != 0 ||
1578 (zip
->erd_size
+ zip
->v_size
+ 16) > remaining_size
||
1579 (zip
->erd_size
+ zip
->v_size
+ 16) < (zip
->erd_size
+ zip
->v_size
))
1581 if (ts
< zip
->v_size
) {
1585 p
= __archive_read_ahead(a
, zip
->v_size
, NULL
);
1588 if (zip
->v_data
== NULL
) {
1589 zip
->v_data
= malloc(zip
->v_size
);
1590 if (zip
->v_data
== NULL
)
1593 memcpy(zip
->v_data
, p
, zip
->v_size
);
1594 __archive_read_consume(a
, zip
->v_size
);
1596 p
= __archive_read_ahead(a
, 4, NULL
);
1599 zip
->v_crc32
= archive_le32dec(p
);
1600 __archive_read_consume(a
, 4);
1602 /*return (ARCHIVE_OK);
1603 * This is not fully implemnted yet.*/
1604 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1605 "Encrypted file is unsupported");
1606 return (ARCHIVE_FAILED
);
1608 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1609 "Truncated ZIP file data");
1610 return (ARCHIVE_FATAL
);
1612 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1613 "Corrupted ZIP file data");
1614 return (ARCHIVE_FATAL
);
1616 archive_set_error(&a
->archive
, ENOMEM
,
1617 "No memory for ZIP decryption");
1618 return (ARCHIVE_FATAL
);
1622 zip_alloc_decryption_buffer(struct archive_read
*a
)
1624 struct zip
*zip
= (struct zip
*)(a
->format
->data
);
1625 size_t bs
= 256 * 1024;
1627 if (zip
->decrypted_buffer
== NULL
) {
1628 zip
->decrypted_buffer_size
= bs
;
1629 zip
->decrypted_buffer
= malloc(bs
);
1630 if (zip
->decrypted_buffer
== NULL
) {
1631 archive_set_error(&a
->archive
, ENOMEM
,
1632 "No memory for ZIP decryption");
1633 return (ARCHIVE_FATAL
);
1636 zip
->decrypted_ptr
= zip
->decrypted_buffer
;
1637 return (ARCHIVE_OK
);
1641 init_traditional_PKWARE_decryption(struct archive_read
*a
)
1643 struct zip
*zip
= (struct zip
*)(a
->format
->data
);
1648 if (zip
->tctx_valid
)
1649 return (ARCHIVE_OK
);
1652 Read the 12 bytes encryption header stored at
1653 the start of the data area.
1655 #define ENC_HEADER_SIZE 12
1656 if (0 == (zip
->entry
->zip_flags
& ZIP_LENGTH_AT_END
)
1657 && zip
->entry_bytes_remaining
< ENC_HEADER_SIZE
) {
1658 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1659 "Truncated Zip encrypted body: only %jd bytes available",
1660 (intmax_t)zip
->entry_bytes_remaining
);
1661 return (ARCHIVE_FATAL
);
1664 p
= __archive_read_ahead(a
, ENC_HEADER_SIZE
, NULL
);
1666 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1667 "Truncated ZIP file data");
1668 return (ARCHIVE_FATAL
);
1671 for (retry
= 0;; retry
++) {
1672 const char *passphrase
;
1675 passphrase
= __archive_read_next_passphrase(a
);
1676 if (passphrase
== NULL
) {
1677 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1679 "Incorrect passphrase":
1680 "Passphrase required for this entry");
1681 return (ARCHIVE_FAILED
);
1685 * Initialize ctx for Traditional PKWARE Decyption.
1687 r
= trad_enc_init(&zip
->tctx
, passphrase
, strlen(passphrase
),
1688 p
, ENC_HEADER_SIZE
, &crcchk
);
1689 if (r
== 0 && crcchk
== zip
->entry
->decdat
)
1690 break;/* The passphrase is OK. */
1691 if (retry
> 10000) {
1692 /* Avoid infinity loop. */
1693 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1694 "Too many incorrect passphrases");
1695 return (ARCHIVE_FAILED
);
1699 __archive_read_consume(a
, ENC_HEADER_SIZE
);
1700 zip
->tctx_valid
= 1;
1701 if (0 == (zip
->entry
->zip_flags
& ZIP_LENGTH_AT_END
)) {
1702 zip
->entry_bytes_remaining
-= ENC_HEADER_SIZE
;
1704 /*zip->entry_uncompressed_bytes_read += ENC_HEADER_SIZE;*/
1705 zip
->entry_compressed_bytes_read
+= ENC_HEADER_SIZE
;
1706 zip
->decrypted_bytes_remaining
= 0;
1708 return (zip_alloc_decryption_buffer(a
));
1709 #undef ENC_HEADER_SIZE
1713 init_WinZip_AES_decryption(struct archive_read
*a
)
1715 struct zip
*zip
= (struct zip
*)(a
->format
->data
);
1718 size_t key_len
, salt_len
;
1719 uint8_t derived_key
[MAX_DERIVED_KEY_BUF_SIZE
];
1723 if (zip
->cctx_valid
|| zip
->hctx_valid
)
1724 return (ARCHIVE_OK
);
1726 switch (zip
->entry
->aes_extra
.strength
) {
1727 case 1: salt_len
= 8; key_len
= 16; break;
1728 case 2: salt_len
= 12; key_len
= 24; break;
1729 case 3: salt_len
= 16; key_len
= 32; break;
1730 default: goto corrupted
;
1732 p
= __archive_read_ahead(a
, salt_len
+ 2, NULL
);
1736 for (retry
= 0;; retry
++) {
1737 const char *passphrase
;
1739 passphrase
= __archive_read_next_passphrase(a
);
1740 if (passphrase
== NULL
) {
1741 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1743 "Incorrect passphrase":
1744 "Passphrase required for this entry");
1745 return (ARCHIVE_FAILED
);
1747 memset(derived_key
, 0, sizeof(derived_key
));
1748 r
= archive_pbkdf2_sha1(passphrase
, strlen(passphrase
),
1749 p
, salt_len
, 1000, derived_key
, key_len
* 2 + 2);
1751 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1752 "Decryption is unsupported due to lack of "
1754 return (ARCHIVE_FAILED
);
1757 /* Check password verification value. */
1758 pv
= ((const uint8_t *)p
) + salt_len
;
1759 if (derived_key
[key_len
* 2] == pv
[0] &&
1760 derived_key
[key_len
* 2 + 1] == pv
[1])
1761 break;/* The passphrase is OK. */
1762 if (retry
> 10000) {
1763 /* Avoid infinity loop. */
1764 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1765 "Too many incorrect passphrases");
1766 return (ARCHIVE_FAILED
);
1770 r
= archive_decrypto_aes_ctr_init(&zip
->cctx
, derived_key
, key_len
);
1772 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1773 "Decryption is unsupported due to lack of crypto library");
1774 return (ARCHIVE_FAILED
);
1776 r
= archive_hmac_sha1_init(&zip
->hctx
, derived_key
+ key_len
, key_len
);
1778 archive_decrypto_aes_ctr_release(&zip
->cctx
);
1779 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1780 "Failed to initialize HMAC-SHA1");
1781 return (ARCHIVE_FAILED
);
1783 zip
->cctx_valid
= zip
->hctx_valid
= 1;
1784 __archive_read_consume(a
, salt_len
+ 2);
1785 zip
->entry_bytes_remaining
-= salt_len
+ 2 + AUTH_CODE_SIZE
;
1786 if (0 == (zip
->entry
->zip_flags
& ZIP_LENGTH_AT_END
)
1787 && zip
->entry_bytes_remaining
< 0)
1789 zip
->entry_compressed_bytes_read
+= salt_len
+ 2 + AUTH_CODE_SIZE
;
1790 zip
->decrypted_bytes_remaining
= 0;
1792 zip
->entry
->compression
= zip
->entry
->aes_extra
.compression
;
1793 return (zip_alloc_decryption_buffer(a
));
1796 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1797 "Truncated ZIP file data");
1798 return (ARCHIVE_FATAL
);
1800 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1801 "Corrupted ZIP file data");
1802 return (ARCHIVE_FATAL
);
1806 archive_read_format_zip_read_data(struct archive_read
*a
,
1807 const void **buff
, size_t *size
, int64_t *offset
)
1810 struct zip
*zip
= (struct zip
*)(a
->format
->data
);
1812 if (zip
->has_encrypted_entries
==
1813 ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW
) {
1814 zip
->has_encrypted_entries
= 0;
1817 *offset
= zip
->entry_uncompressed_bytes_read
;
1821 /* If we hit end-of-entry last time, return ARCHIVE_EOF. */
1822 if (zip
->end_of_entry
)
1823 return (ARCHIVE_EOF
);
1825 /* Return EOF immediately if this is a non-regular file. */
1826 if (AE_IFREG
!= (zip
->entry
->mode
& AE_IFMT
))
1827 return (ARCHIVE_EOF
);
1829 __archive_read_consume(a
, zip
->unconsumed
);
1830 zip
->unconsumed
= 0;
1832 if (zip
->init_decryption
) {
1833 zip
->has_encrypted_entries
= 1;
1834 if (zip
->entry
->zip_flags
& ZIP_STRONG_ENCRYPTED
)
1835 r
= read_decryption_header(a
);
1836 else if (zip
->entry
->compression
== WINZIP_AES_ENCRYPTION
)
1837 r
= init_WinZip_AES_decryption(a
);
1839 r
= init_traditional_PKWARE_decryption(a
);
1840 if (r
!= ARCHIVE_OK
)
1842 zip
->init_decryption
= 0;
1845 switch(zip
->entry
->compression
) {
1846 case 0: /* No compression. */
1847 r
= zip_read_data_none(a
, buff
, size
, offset
);
1850 case 8: /* Deflate compression. */
1851 r
= zip_read_data_deflate(a
, buff
, size
, offset
);
1854 default: /* Unsupported compression. */
1855 /* Return a warning. */
1856 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1857 "Unsupported ZIP compression method (%s)",
1858 compression_name(zip
->entry
->compression
));
1859 /* We can't decompress this entry, but we will
1860 * be able to skip() it and try the next entry. */
1861 return (ARCHIVE_FAILED
);
1864 if (r
!= ARCHIVE_OK
)
1866 /* Update checksum */
1868 zip
->entry_crc32
= zip
->crc32func(zip
->entry_crc32
, *buff
,
1870 /* If we hit the end, swallow any end-of-data marker. */
1871 if (zip
->end_of_entry
) {
1872 /* Check file size, CRC against these values. */
1873 if (zip
->entry
->compressed_size
!=
1874 zip
->entry_compressed_bytes_read
) {
1875 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1876 "ZIP compressed data is wrong size "
1877 "(read %jd, expected %jd)",
1878 (intmax_t)zip
->entry_compressed_bytes_read
,
1879 (intmax_t)zip
->entry
->compressed_size
);
1880 return (ARCHIVE_WARN
);
1882 /* Size field only stores the lower 32 bits of the actual
1884 if ((zip
->entry
->uncompressed_size
& UINT32_MAX
)
1885 != (zip
->entry_uncompressed_bytes_read
& UINT32_MAX
)) {
1886 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1887 "ZIP uncompressed data is wrong size "
1888 "(read %jd, expected %jd)\n",
1889 (intmax_t)zip
->entry_uncompressed_bytes_read
,
1890 (intmax_t)zip
->entry
->uncompressed_size
);
1891 return (ARCHIVE_WARN
);
1893 /* Check computed CRC against header */
1894 if ((!zip
->hctx_valid
||
1895 zip
->entry
->aes_extra
.vendor
!= AES_VENDOR_AE_2
) &&
1896 zip
->entry
->crc32
!= zip
->entry_crc32
1897 && !zip
->ignore_crc32
) {
1898 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1899 "ZIP bad CRC: 0x%lx should be 0x%lx",
1900 (unsigned long)zip
->entry_crc32
,
1901 (unsigned long)zip
->entry
->crc32
);
1902 return (ARCHIVE_WARN
);
1906 return (ARCHIVE_OK
);
1910 archive_read_format_zip_cleanup(struct archive_read
*a
)
1913 struct zip_entry
*zip_entry
, *next_zip_entry
;
1915 zip
= (struct zip
*)(a
->format
->data
);
1917 if (zip
->stream_valid
)
1918 inflateEnd(&zip
->stream
);
1919 free(zip
->uncompressed_buffer
);
1921 if (zip
->zip_entries
) {
1922 zip_entry
= zip
->zip_entries
;
1923 while (zip_entry
!= NULL
) {
1924 next_zip_entry
= zip_entry
->next
;
1925 archive_string_free(&zip_entry
->rsrcname
);
1927 zip_entry
= next_zip_entry
;
1930 free(zip
->decrypted_buffer
);
1931 if (zip
->cctx_valid
)
1932 archive_decrypto_aes_ctr_release(&zip
->cctx
);
1933 if (zip
->hctx_valid
)
1934 archive_hmac_sha1_cleanup(&zip
->hctx
);
1938 archive_string_free(&zip
->format_name
);
1940 (a
->format
->data
) = NULL
;
1941 return (ARCHIVE_OK
);
1945 archive_read_format_zip_has_encrypted_entries(struct archive_read
*_a
)
1947 if (_a
&& _a
->format
) {
1948 struct zip
* zip
= (struct zip
*)_a
->format
->data
;
1950 return zip
->has_encrypted_entries
;
1953 return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW
;
1957 archive_read_format_zip_options(struct archive_read
*a
,
1958 const char *key
, const char *val
)
1961 int ret
= ARCHIVE_FAILED
;
1963 zip
= (struct zip
*)(a
->format
->data
);
1964 if (strcmp(key
, "compat-2x") == 0) {
1965 /* Handle filenames as libarchive 2.x */
1966 zip
->init_default_conversion
= (val
!= NULL
) ? 1 : 0;
1967 return (ARCHIVE_OK
);
1968 } else if (strcmp(key
, "hdrcharset") == 0) {
1969 if (val
== NULL
|| val
[0] == 0)
1970 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1971 "zip: hdrcharset option needs a character-set name"
1974 zip
->sconv
= archive_string_conversion_from_charset(
1975 &a
->archive
, val
, 0);
1976 if (zip
->sconv
!= NULL
) {
1977 if (strcmp(val
, "UTF-8") == 0)
1978 zip
->sconv_utf8
= zip
->sconv
;
1981 ret
= ARCHIVE_FATAL
;
1984 } else if (strcmp(key
, "ignorecrc32") == 0) {
1985 /* Mostly useful for testing. */
1986 if (val
== NULL
|| val
[0] == 0) {
1987 zip
->crc32func
= real_crc32
;
1988 zip
->ignore_crc32
= 0;
1990 zip
->crc32func
= fake_crc32
;
1991 zip
->ignore_crc32
= 1;
1993 return (ARCHIVE_OK
);
1994 } else if (strcmp(key
, "mac-ext") == 0) {
1995 zip
->process_mac_extensions
= (val
!= NULL
&& val
[0] != 0);
1996 return (ARCHIVE_OK
);
1999 /* Note: The "warn" return is just to inform the options
2000 * supervisor that we didn't handle it. It will generate
2001 * a suitable error if no one used this option. */
2002 return (ARCHIVE_WARN
);
2006 archive_read_support_format_zip(struct archive
*a
)
2009 r
= archive_read_support_format_zip_streamable(a
);
2010 if (r
!= ARCHIVE_OK
)
2012 return (archive_read_support_format_zip_seekable(a
));
2015 /* ------------------------------------------------------------------------ */
2018 * Streaming-mode support
2023 archive_read_support_format_zip_capabilities_streamable(struct archive_read
* a
)
2025 (void)a
; /* UNUSED */
2026 return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA
|
2027 ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA
);
2031 archive_read_format_zip_streamable_bid(struct archive_read
*a
, int best_bid
)
2035 (void)best_bid
; /* UNUSED */
2037 if ((p
= __archive_read_ahead(a
, 4, NULL
)) == NULL
)
2041 * Bid of 29 here comes from:
2042 * + 16 bits for "PK",
2043 * + next 16-bit field has 6 options so contributes
2044 * about 16 - log_2(6) ~= 16 - 2.6 ~= 13 bits
2046 * So we've effectively verified ~29 total bits of check data.
2048 if (p
[0] == 'P' && p
[1] == 'K') {
2049 if ((p
[2] == '\001' && p
[3] == '\002')
2050 || (p
[2] == '\003' && p
[3] == '\004')
2051 || (p
[2] == '\005' && p
[3] == '\006')
2052 || (p
[2] == '\006' && p
[3] == '\006')
2053 || (p
[2] == '\007' && p
[3] == '\010')
2054 || (p
[2] == '0' && p
[3] == '0'))
2058 /* TODO: It's worth looking ahead a little bit for a valid
2059 * PK signature. In particular, that would make it possible
2060 * to read some UUEncoded SFX files or SFX files coming from
2061 * a network socket. */
2067 archive_read_format_zip_streamable_read_header(struct archive_read
*a
,
2068 struct archive_entry
*entry
)
2072 a
->archive
.archive_format
= ARCHIVE_FORMAT_ZIP
;
2073 if (a
->archive
.archive_format_name
== NULL
)
2074 a
->archive
.archive_format_name
= "ZIP";
2076 zip
= (struct zip
*)(a
->format
->data
);
2079 * It should be sufficient to call archive_read_next_header() for
2080 * a reader to determine if an entry is encrypted or not. If the
2081 * encryption of an entry is only detectable when calling
2082 * archive_read_data(), so be it. We'll do the same check there
2085 if (zip
->has_encrypted_entries
==
2086 ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW
)
2087 zip
->has_encrypted_entries
= 0;
2089 /* Make sure we have a zip_entry structure to use. */
2090 if (zip
->zip_entries
== NULL
) {
2091 zip
->zip_entries
= malloc(sizeof(struct zip_entry
));
2092 if (zip
->zip_entries
== NULL
) {
2093 archive_set_error(&a
->archive
, ENOMEM
,
2095 return ARCHIVE_FATAL
;
2098 zip
->entry
= zip
->zip_entries
;
2099 memset(zip
->entry
, 0, sizeof(struct zip_entry
));
2101 if (zip
->cctx_valid
)
2102 archive_decrypto_aes_ctr_release(&zip
->cctx
);
2103 if (zip
->hctx_valid
)
2104 archive_hmac_sha1_cleanup(&zip
->hctx
);
2105 zip
->tctx_valid
= zip
->cctx_valid
= zip
->hctx_valid
= 0;
2106 __archive_read_reset_passphrase(a
);
2108 /* Search ahead for the next local file header. */
2109 __archive_read_consume(a
, zip
->unconsumed
);
2110 zip
->unconsumed
= 0;
2112 int64_t skipped
= 0;
2113 const char *p
, *end
;
2116 p
= __archive_read_ahead(a
, 4, &bytes
);
2118 return (ARCHIVE_FATAL
);
2121 while (p
+ 4 <= end
) {
2122 if (p
[0] == 'P' && p
[1] == 'K') {
2123 if (p
[2] == '\003' && p
[3] == '\004') {
2124 /* Regular file entry. */
2125 __archive_read_consume(a
, skipped
);
2126 return zip_read_local_file_header(a
,
2131 * TODO: We cannot restore permissions
2132 * based only on the local file headers.
2133 * Consider scanning the central
2134 * directory and returning additional
2135 * entries for at least directories.
2136 * This would allow us to properly set
2137 * directory permissions.
2139 * This won't help us fix symlinks
2140 * and may not help with regular file
2141 * permissions, either. <sigh>
2143 if (p
[2] == '\001' && p
[3] == '\002') {
2144 return (ARCHIVE_EOF
);
2147 /* End of central directory? Must be an
2149 if ((p
[2] == '\005' && p
[3] == '\006')
2150 || (p
[2] == '\006' && p
[3] == '\006'))
2151 return (ARCHIVE_EOF
);
2156 __archive_read_consume(a
, skipped
);
2161 archive_read_format_zip_read_data_skip_streamable(struct archive_read
*a
)
2164 int64_t bytes_skipped
;
2166 zip
= (struct zip
*)(a
->format
->data
);
2167 bytes_skipped
= __archive_read_consume(a
, zip
->unconsumed
);
2168 zip
->unconsumed
= 0;
2169 if (bytes_skipped
< 0)
2170 return (ARCHIVE_FATAL
);
2172 /* If we've already read to end of data, we're done. */
2173 if (zip
->end_of_entry
)
2174 return (ARCHIVE_OK
);
2176 /* So we know we're streaming... */
2177 if (0 == (zip
->entry
->zip_flags
& ZIP_LENGTH_AT_END
)
2178 || zip
->entry
->compressed_size
> 0) {
2179 /* We know the compressed length, so we can just skip. */
2180 bytes_skipped
= __archive_read_consume(a
,
2181 zip
->entry_bytes_remaining
);
2182 if (bytes_skipped
< 0)
2183 return (ARCHIVE_FATAL
);
2184 return (ARCHIVE_OK
);
2187 if (zip
->init_decryption
) {
2190 zip
->has_encrypted_entries
= 1;
2191 if (zip
->entry
->zip_flags
& ZIP_STRONG_ENCRYPTED
)
2192 r
= read_decryption_header(a
);
2193 else if (zip
->entry
->compression
== WINZIP_AES_ENCRYPTION
)
2194 r
= init_WinZip_AES_decryption(a
);
2196 r
= init_traditional_PKWARE_decryption(a
);
2197 if (r
!= ARCHIVE_OK
)
2199 zip
->init_decryption
= 0;
2202 /* We're streaming and we don't know the length. */
2203 /* If the body is compressed and we know the format, we can
2204 * find an exact end-of-entry by decompressing it. */
2205 switch (zip
->entry
->compression
) {
2207 case 8: /* Deflate compression. */
2208 while (!zip
->end_of_entry
) {
2210 const void *buff
= NULL
;
2213 r
= zip_read_data_deflate(a
, &buff
, &size
, &offset
);
2214 if (r
!= ARCHIVE_OK
)
2219 default: /* Uncompressed or unknown. */
2220 /* Scan for a PK\007\010 signature. */
2222 const char *p
, *buff
;
2223 ssize_t bytes_avail
;
2224 buff
= __archive_read_ahead(a
, 16, &bytes_avail
);
2225 if (bytes_avail
< 16) {
2226 archive_set_error(&a
->archive
,
2227 ARCHIVE_ERRNO_FILE_FORMAT
,
2228 "Truncated ZIP file data");
2229 return (ARCHIVE_FATAL
);
2232 while (p
<= buff
+ bytes_avail
- 16) {
2233 if (p
[3] == 'P') { p
+= 3; }
2234 else if (p
[3] == 'K') { p
+= 2; }
2235 else if (p
[3] == '\007') { p
+= 1; }
2236 else if (p
[3] == '\010' && p
[2] == '\007'
2237 && p
[1] == 'K' && p
[0] == 'P') {
2238 if (zip
->entry
->flags
& LA_USED_ZIP64
)
2239 __archive_read_consume(a
,
2242 __archive_read_consume(a
,
2247 __archive_read_consume(a
, p
- buff
);
2253 archive_read_support_format_zip_streamable(struct archive
*_a
)
2255 struct archive_read
*a
= (struct archive_read
*)_a
;
2259 archive_check_magic(_a
, ARCHIVE_READ_MAGIC
,
2260 ARCHIVE_STATE_NEW
, "archive_read_support_format_zip");
2262 zip
= (struct zip
*)calloc(1, sizeof(*zip
));
2264 archive_set_error(&a
->archive
, ENOMEM
,
2265 "Can't allocate zip data");
2266 return (ARCHIVE_FATAL
);
2269 /* Streamable reader doesn't support mac extensions. */
2270 zip
->process_mac_extensions
= 0;
2273 * Until enough data has been read, we cannot tell about
2274 * any encrypted entries yet.
2276 zip
->has_encrypted_entries
= ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW
;
2277 zip
->crc32func
= real_crc32
;
2279 r
= __archive_read_register_format(a
,
2282 archive_read_format_zip_streamable_bid
,
2283 archive_read_format_zip_options
,
2284 archive_read_format_zip_streamable_read_header
,
2285 archive_read_format_zip_read_data
,
2286 archive_read_format_zip_read_data_skip_streamable
,
2288 archive_read_format_zip_cleanup
,
2289 archive_read_support_format_zip_capabilities_streamable
,
2290 archive_read_format_zip_has_encrypted_entries
);
2292 if (r
!= ARCHIVE_OK
)
2294 return (ARCHIVE_OK
);
2297 /* ------------------------------------------------------------------------ */
2300 * Seeking-mode support
2304 archive_read_support_format_zip_capabilities_seekable(struct archive_read
* a
)
2306 (void)a
; /* UNUSED */
2307 return (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA
|
2308 ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA
);
2312 * TODO: This is a performance sink because it forces the read core to
2313 * drop buffered data from the start of file, which will then have to
2314 * be re-read again if this bidder loses.
2316 * We workaround this a little by passing in the best bid so far so
2317 * that later bidders can do nothing if they know they'll never
2318 * outbid. But we can certainly do better...
2321 read_eocd(struct zip
*zip
, const char *p
, int64_t current_offset
)
2323 /* Sanity-check the EOCD we've found. */
2325 /* This must be the first volume. */
2326 if (archive_le16dec(p
+ 4) != 0)
2328 /* Central directory must be on this volume. */
2329 if (archive_le16dec(p
+ 4) != archive_le16dec(p
+ 6))
2331 /* All central directory entries must be on this volume. */
2332 if (archive_le16dec(p
+ 10) != archive_le16dec(p
+ 8))
2334 /* Central directory can't extend beyond start of EOCD record. */
2335 if (archive_le32dec(p
+ 16) + archive_le32dec(p
+ 12)
2339 /* Save the central directory location for later use. */
2340 zip
->central_directory_offset
= archive_le32dec(p
+ 16);
2342 /* This is just a tiny bit higher than the maximum
2343 returned by the streaming Zip bidder. This ensures
2344 that the more accurate seeking Zip parser wins
2345 whenever seek is available. */
2350 * Examine Zip64 EOCD locator: If it's valid, store the information
2354 read_zip64_eocd(struct archive_read
*a
, struct zip
*zip
, const char *p
)
2356 int64_t eocd64_offset
;
2357 int64_t eocd64_size
;
2359 /* Sanity-check the locator record. */
2361 /* Central dir must be on first volume. */
2362 if (archive_le32dec(p
+ 4) != 0)
2364 /* Must be only a single volume. */
2365 if (archive_le32dec(p
+ 16) != 1)
2368 /* Find the Zip64 EOCD record. */
2369 eocd64_offset
= archive_le64dec(p
+ 8);
2370 if (__archive_read_seek(a
, eocd64_offset
, SEEK_SET
) < 0)
2372 if ((p
= __archive_read_ahead(a
, 56, NULL
)) == NULL
)
2374 /* Make sure we can read all of it. */
2375 eocd64_size
= archive_le64dec(p
+ 4) + 12;
2376 if (eocd64_size
< 56 || eocd64_size
> 16384)
2378 if ((p
= __archive_read_ahead(a
, (size_t)eocd64_size
, NULL
)) == NULL
)
2381 /* Sanity-check the EOCD64 */
2382 if (archive_le32dec(p
+ 16) != 0) /* Must be disk #0 */
2384 if (archive_le32dec(p
+ 20) != 0) /* CD must be on disk #0 */
2386 /* CD can't be split. */
2387 if (archive_le64dec(p
+ 24) != archive_le64dec(p
+ 32))
2390 /* Save the central directory offset for later use. */
2391 zip
->central_directory_offset
= archive_le64dec(p
+ 48);
2395 archive_read_format_zip_seekable_bid(struct archive_read
*a
, int best_bid
)
2397 struct zip
*zip
= (struct zip
*)a
->format
->data
;
2398 int64_t file_size
, current_offset
;
2402 /* If someone has already bid more than 32, then avoid
2403 trashing the look-ahead buffers with a seek. */
2407 file_size
= __archive_read_seek(a
, 0, SEEK_END
);
2411 /* Search last 16k of file for end-of-central-directory
2412 * record (which starts with PK\005\006) */
2413 tail
= (int)zipmin(1024 * 16, file_size
);
2414 current_offset
= __archive_read_seek(a
, -tail
, SEEK_END
);
2415 if (current_offset
< 0)
2417 if ((p
= __archive_read_ahead(a
, (size_t)tail
, NULL
)) == NULL
)
2419 /* Boyer-Moore search backwards from the end, since we want
2420 * to match the last EOCD in the file (there can be more than
2421 * one if there is an uncompressed Zip archive as a member
2422 * within this Zip archive). */
2423 for (i
= tail
- 22; i
> 0;) {
2426 if (memcmp(p
+ i
, "PK\005\006", 4) == 0) {
2427 int ret
= read_eocd(zip
, p
+ i
,
2428 current_offset
+ i
);
2430 /* Zip64 EOCD locator precedes
2431 * regular EOCD if present. */
2433 && memcmp(p
+ i
- 20, "PK\006\007", 4) == 0) {
2434 read_zip64_eocd(a
, zip
, p
+ i
- 20);
2441 case 'K': i
-= 1; break;
2442 case 005: i
-= 2; break;
2443 case 006: i
-= 3; break;
2444 default: i
-= 4; break;
2450 /* The red-black trees are only used in seeking mode to manage
2451 * the in-memory copy of the central directory. */
2454 cmp_node(const struct archive_rb_node
*n1
, const struct archive_rb_node
*n2
)
2456 const struct zip_entry
*e1
= (const struct zip_entry
*)n1
;
2457 const struct zip_entry
*e2
= (const struct zip_entry
*)n2
;
2459 if (e1
->local_header_offset
> e2
->local_header_offset
)
2461 if (e1
->local_header_offset
< e2
->local_header_offset
)
2467 cmp_key(const struct archive_rb_node
*n
, const void *key
)
2469 /* This function won't be called */
2470 (void)n
; /* UNUSED */
2471 (void)key
; /* UNUSED */
2475 static const struct archive_rb_tree_ops rb_ops
= {
2480 rsrc_cmp_node(const struct archive_rb_node
*n1
,
2481 const struct archive_rb_node
*n2
)
2483 const struct zip_entry
*e1
= (const struct zip_entry
*)n1
;
2484 const struct zip_entry
*e2
= (const struct zip_entry
*)n2
;
2486 return (strcmp(e2
->rsrcname
.s
, e1
->rsrcname
.s
));
2490 rsrc_cmp_key(const struct archive_rb_node
*n
, const void *key
)
2492 const struct zip_entry
*e
= (const struct zip_entry
*)n
;
2493 return (strcmp((const char *)key
, e
->rsrcname
.s
));
2496 static const struct archive_rb_tree_ops rb_rsrc_ops
= {
2497 &rsrc_cmp_node
, &rsrc_cmp_key
2501 rsrc_basename(const char *name
, size_t name_length
)
2507 s
= memchr(s
, '/', name_length
- (s
- name
));
2516 expose_parent_dirs(struct zip
*zip
, const char *name
, size_t name_length
)
2518 struct archive_string str
;
2519 struct zip_entry
*dir
;
2522 archive_string_init(&str
);
2523 archive_strncpy(&str
, name
, name_length
);
2525 s
= strrchr(str
.s
, '/');
2529 /* Transfer the parent directory from zip->tree_rsrc RB
2530 * tree to zip->tree RB tree to expose. */
2531 dir
= (struct zip_entry
*)
2532 __archive_rb_tree_find_node(&zip
->tree_rsrc
, str
.s
);
2535 __archive_rb_tree_remove_node(&zip
->tree_rsrc
, &dir
->node
);
2536 archive_string_free(&dir
->rsrcname
);
2537 __archive_rb_tree_insert_node(&zip
->tree
, &dir
->node
);
2539 archive_string_free(&str
);
2543 slurp_central_directory(struct archive_read
*a
, struct zip
*zip
)
2548 ssize_t bytes_avail
;
2552 * Find the start of the central directory. The end-of-CD
2553 * record has our starting point, but there are lots of
2554 * Zip archives which have had other data prepended to the
2555 * file, which makes the recorded offsets all too small.
2556 * So we search forward from the specified offset until we
2557 * find the real start of the central directory. Then we
2558 * know the correction we need to apply to account for leading
2561 if (__archive_read_seek(a
, zip
->central_directory_offset
, SEEK_SET
) < 0)
2562 return ARCHIVE_FATAL
;
2566 if ((p
= __archive_read_ahead(a
, 20, &bytes_avail
)) == NULL
)
2567 return ARCHIVE_FATAL
;
2568 for (found
= 0, i
= 0; !found
&& i
< bytes_avail
- 4;) {
2570 case 'P': i
+= 3; break;
2571 case 'K': i
+= 2; break;
2572 case 001: i
+= 1; break;
2574 if (memcmp(p
+ i
, "PK\001\002", 4) == 0) {
2580 case 005: i
+= 1; break;
2582 if (memcmp(p
+ i
, "PK\005\006", 4) == 0) {
2585 } else if (memcmp(p
+ i
, "PK\006\006", 4) == 0) {
2591 default: i
+= 4; break;
2594 __archive_read_consume(a
, i
);
2596 correction
= archive_filter_bytes(&a
->archive
, 0)
2597 - zip
->central_directory_offset
;
2599 __archive_rb_tree_init(&zip
->tree
, &rb_ops
);
2600 __archive_rb_tree_init(&zip
->tree_rsrc
, &rb_rsrc_ops
);
2602 zip
->central_directory_entries_total
= 0;
2604 struct zip_entry
*zip_entry
;
2605 size_t filename_length
, extra_length
, comment_length
;
2606 uint32_t external_attributes
;
2607 const char *name
, *r
;
2609 if ((p
= __archive_read_ahead(a
, 4, NULL
)) == NULL
)
2610 return ARCHIVE_FATAL
;
2611 if (memcmp(p
, "PK\006\006", 4) == 0
2612 || memcmp(p
, "PK\005\006", 4) == 0) {
2614 } else if (memcmp(p
, "PK\001\002", 4) != 0) {
2615 archive_set_error(&a
->archive
,
2616 -1, "Invalid central directory signature");
2617 return ARCHIVE_FATAL
;
2619 if ((p
= __archive_read_ahead(a
, 46, NULL
)) == NULL
)
2620 return ARCHIVE_FATAL
;
2622 zip_entry
= calloc(1, sizeof(struct zip_entry
));
2623 zip_entry
->next
= zip
->zip_entries
;
2624 zip_entry
->flags
|= LA_FROM_CENTRAL_DIRECTORY
;
2625 zip
->zip_entries
= zip_entry
;
2626 zip
->central_directory_entries_total
++;
2628 /* version = p[4]; */
2629 zip_entry
->system
= p
[5];
2630 /* version_required = archive_le16dec(p + 6); */
2631 zip_entry
->zip_flags
= archive_le16dec(p
+ 8);
2632 if (zip_entry
->zip_flags
2633 & (ZIP_ENCRYPTED
| ZIP_STRONG_ENCRYPTED
)){
2634 zip
->has_encrypted_entries
= 1;
2636 zip_entry
->compression
= (char)archive_le16dec(p
+ 10);
2637 zip_entry
->mtime
= zip_time(p
+ 12);
2638 zip_entry
->crc32
= archive_le32dec(p
+ 16);
2639 if (zip_entry
->zip_flags
& ZIP_LENGTH_AT_END
)
2640 zip_entry
->decdat
= p
[13];
2642 zip_entry
->decdat
= p
[19];
2643 zip_entry
->compressed_size
= archive_le32dec(p
+ 20);
2644 zip_entry
->uncompressed_size
= archive_le32dec(p
+ 24);
2645 filename_length
= archive_le16dec(p
+ 28);
2646 extra_length
= archive_le16dec(p
+ 30);
2647 comment_length
= archive_le16dec(p
+ 32);
2648 /* disk_start = archive_le16dec(p + 34); */ /* Better be zero. */
2649 /* internal_attributes = archive_le16dec(p + 36); */ /* text bit */
2650 external_attributes
= archive_le32dec(p
+ 38);
2651 zip_entry
->local_header_offset
=
2652 archive_le32dec(p
+ 42) + correction
;
2654 /* If we can't guess the mode, leave it zero here;
2655 when we read the local file header we might get
2656 more information. */
2657 if (zip_entry
->system
== 3) {
2658 zip_entry
->mode
= external_attributes
>> 16;
2659 } else if (zip_entry
->system
== 0) {
2660 // Interpret MSDOS directory bit
2661 if (0x10 == (external_attributes
& 0x10)) {
2662 zip_entry
->mode
= AE_IFDIR
| 0775;
2664 zip_entry
->mode
= AE_IFREG
| 0664;
2666 if (0x01 == (external_attributes
& 0x01)) {
2667 // Read-only bit; strip write permissions
2668 zip_entry
->mode
&= 0555;
2671 zip_entry
->mode
= 0;
2674 /* We're done with the regular data; get the filename and
2676 __archive_read_consume(a
, 46);
2677 p
= __archive_read_ahead(a
, filename_length
+ extra_length
,
2680 archive_set_error(&a
->archive
,
2681 ARCHIVE_ERRNO_FILE_FORMAT
,
2682 "Truncated ZIP file header");
2683 return ARCHIVE_FATAL
;
2685 process_extra(p
+ filename_length
, extra_length
, zip_entry
);
2688 * Mac resource fork files are stored under the
2689 * "__MACOSX/" directory, so we should check if
2692 if (!zip
->process_mac_extensions
) {
2693 /* Treat every entry as a regular entry. */
2694 __archive_rb_tree_insert_node(&zip
->tree
,
2698 r
= rsrc_basename(name
, filename_length
);
2699 if (filename_length
>= 9 &&
2700 strncmp("__MACOSX/", name
, 9) == 0) {
2701 /* If this file is not a resource fork nor
2702 * a directory. We should treat it as a non
2703 * resource fork file to expose it. */
2704 if (name
[filename_length
-1] != '/' &&
2705 (r
- name
< 3 || r
[0] != '.' || r
[1] != '_')) {
2706 __archive_rb_tree_insert_node(
2707 &zip
->tree
, &zip_entry
->node
);
2708 /* Expose its parent directories. */
2709 expose_parent_dirs(zip
, name
,
2712 /* This file is a resource fork file or
2714 archive_strncpy(&(zip_entry
->rsrcname
),
2715 name
, filename_length
);
2716 __archive_rb_tree_insert_node(
2717 &zip
->tree_rsrc
, &zip_entry
->node
);
2720 /* Generate resource fork name to find its
2721 * resource file at zip->tree_rsrc. */
2722 archive_strcpy(&(zip_entry
->rsrcname
),
2724 archive_strncat(&(zip_entry
->rsrcname
),
2726 archive_strcat(&(zip_entry
->rsrcname
), "._");
2727 archive_strncat(&(zip_entry
->rsrcname
),
2729 filename_length
- (r
- name
));
2730 /* Register an entry to RB tree to sort it by
2732 __archive_rb_tree_insert_node(&zip
->tree
,
2737 /* Skip the comment too ... */
2738 __archive_read_consume(a
,
2739 filename_length
+ extra_length
+ comment_length
);
2746 zip_get_local_file_header_size(struct archive_read
*a
, size_t extra
)
2749 ssize_t filename_length
, extra_length
;
2751 if ((p
= __archive_read_ahead(a
, extra
+ 30, NULL
)) == NULL
) {
2752 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
2753 "Truncated ZIP file header");
2754 return (ARCHIVE_WARN
);
2758 if (memcmp(p
, "PK\003\004", 4) != 0) {
2759 archive_set_error(&a
->archive
, -1, "Damaged Zip archive");
2760 return ARCHIVE_WARN
;
2762 filename_length
= archive_le16dec(p
+ 26);
2763 extra_length
= archive_le16dec(p
+ 28);
2765 return (30 + filename_length
+ extra_length
);
2769 zip_read_mac_metadata(struct archive_read
*a
, struct archive_entry
*entry
,
2770 struct zip_entry
*rsrc
)
2772 struct zip
*zip
= (struct zip
*)a
->format
->data
;
2773 unsigned char *metadata
, *mp
;
2774 int64_t offset
= archive_filter_bytes(&a
->archive
, 0);
2775 size_t remaining_bytes
, metadata_bytes
;
2777 int ret
= ARCHIVE_OK
, eof
;
2779 switch(rsrc
->compression
) {
2780 case 0: /* No compression. */
2781 if (rsrc
->uncompressed_size
!= rsrc
->compressed_size
) {
2782 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
2783 "Malformed OS X metadata entry: inconsistent size");
2784 return (ARCHIVE_FATAL
);
2787 case 8: /* Deflate compression. */
2790 default: /* Unsupported compression. */
2791 /* Return a warning. */
2792 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
2793 "Unsupported ZIP compression method (%s)",
2794 compression_name(rsrc
->compression
));
2795 /* We can't decompress this entry, but we will
2796 * be able to skip() it and try the next entry. */
2797 return (ARCHIVE_WARN
);
2800 if (rsrc
->uncompressed_size
> (4 * 1024 * 1024)) {
2801 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
2802 "Mac metadata is too large: %jd > 4M bytes",
2803 (intmax_t)rsrc
->uncompressed_size
);
2804 return (ARCHIVE_WARN
);
2806 if (rsrc
->compressed_size
> (4 * 1024 * 1024)) {
2807 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
2808 "Mac metadata is too large: %jd > 4M bytes",
2809 (intmax_t)rsrc
->compressed_size
);
2810 return (ARCHIVE_WARN
);
2813 metadata
= malloc((size_t)rsrc
->uncompressed_size
);
2814 if (metadata
== NULL
) {
2815 archive_set_error(&a
->archive
, ENOMEM
,
2816 "Can't allocate memory for Mac metadata");
2817 return (ARCHIVE_FATAL
);
2820 if (offset
< rsrc
->local_header_offset
)
2821 __archive_read_consume(a
, rsrc
->local_header_offset
- offset
);
2822 else if (offset
!= rsrc
->local_header_offset
) {
2823 __archive_read_seek(a
, rsrc
->local_header_offset
, SEEK_SET
);
2826 hsize
= zip_get_local_file_header_size(a
, 0);
2827 __archive_read_consume(a
, hsize
);
2829 remaining_bytes
= (size_t)rsrc
->compressed_size
;
2830 metadata_bytes
= (size_t)rsrc
->uncompressed_size
;
2833 while (!eof
&& remaining_bytes
) {
2834 const unsigned char *p
;
2835 ssize_t bytes_avail
;
2838 p
= __archive_read_ahead(a
, 1, &bytes_avail
);
2840 archive_set_error(&a
->archive
,
2841 ARCHIVE_ERRNO_FILE_FORMAT
,
2842 "Truncated ZIP file header");
2844 goto exit_mac_metadata
;
2846 if ((size_t)bytes_avail
> remaining_bytes
)
2847 bytes_avail
= remaining_bytes
;
2848 switch(rsrc
->compression
) {
2849 case 0: /* No compression. */
2850 if ((size_t)bytes_avail
> metadata_bytes
)
2851 bytes_avail
= metadata_bytes
;
2852 memcpy(mp
, p
, bytes_avail
);
2853 bytes_used
= (size_t)bytes_avail
;
2854 metadata_bytes
-= bytes_used
;
2856 if (metadata_bytes
== 0)
2860 case 8: /* Deflate compression. */
2864 ret
= zip_deflate_init(a
, zip
);
2865 if (ret
!= ARCHIVE_OK
)
2866 goto exit_mac_metadata
;
2867 zip
->stream
.next_in
=
2868 (Bytef
*)(uintptr_t)(const void *)p
;
2869 zip
->stream
.avail_in
= (uInt
)bytes_avail
;
2870 zip
->stream
.total_in
= 0;
2871 zip
->stream
.next_out
= mp
;
2872 zip
->stream
.avail_out
= (uInt
)metadata_bytes
;
2873 zip
->stream
.total_out
= 0;
2875 r
= inflate(&zip
->stream
, 0);
2883 archive_set_error(&a
->archive
, ENOMEM
,
2884 "Out of memory for ZIP decompression");
2885 ret
= ARCHIVE_FATAL
;
2886 goto exit_mac_metadata
;
2888 archive_set_error(&a
->archive
,
2890 "ZIP decompression failed (%d)", r
);
2891 ret
= ARCHIVE_FATAL
;
2892 goto exit_mac_metadata
;
2894 bytes_used
= zip
->stream
.total_in
;
2895 metadata_bytes
-= zip
->stream
.total_out
;
2896 mp
+= zip
->stream
.total_out
;
2904 __archive_read_consume(a
, bytes_used
);
2905 remaining_bytes
-= bytes_used
;
2907 archive_entry_copy_mac_metadata(entry
, metadata
,
2908 (size_t)rsrc
->uncompressed_size
- metadata_bytes
);
2911 __archive_read_seek(a
, offset
, SEEK_SET
);
2912 zip
->decompress_init
= 0;
2918 archive_read_format_zip_seekable_read_header(struct archive_read
*a
,
2919 struct archive_entry
*entry
)
2921 struct zip
*zip
= (struct zip
*)a
->format
->data
;
2922 struct zip_entry
*rsrc
;
2924 int r
, ret
= ARCHIVE_OK
;
2927 * It should be sufficient to call archive_read_next_header() for
2928 * a reader to determine if an entry is encrypted or not. If the
2929 * encryption of an entry is only detectable when calling
2930 * archive_read_data(), so be it. We'll do the same check there
2933 if (zip
->has_encrypted_entries
==
2934 ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW
)
2935 zip
->has_encrypted_entries
= 0;
2937 a
->archive
.archive_format
= ARCHIVE_FORMAT_ZIP
;
2938 if (a
->archive
.archive_format_name
== NULL
)
2939 a
->archive
.archive_format_name
= "ZIP";
2941 if (zip
->zip_entries
== NULL
) {
2942 r
= slurp_central_directory(a
, zip
);
2943 if (r
!= ARCHIVE_OK
)
2945 /* Get first entry whose local header offset is lower than
2946 * other entries in the archive file. */
2948 (struct zip_entry
*)ARCHIVE_RB_TREE_MIN(&zip
->tree
);
2949 } else if (zip
->entry
!= NULL
) {
2950 /* Get next entry in local header offset order. */
2951 zip
->entry
= (struct zip_entry
*)__archive_rb_tree_iterate(
2952 &zip
->tree
, &zip
->entry
->node
, ARCHIVE_RB_DIR_RIGHT
);
2955 if (zip
->entry
== NULL
)
2958 if (zip
->entry
->rsrcname
.s
)
2959 rsrc
= (struct zip_entry
*)__archive_rb_tree_find_node(
2960 &zip
->tree_rsrc
, zip
->entry
->rsrcname
.s
);
2964 if (zip
->cctx_valid
)
2965 archive_decrypto_aes_ctr_release(&zip
->cctx
);
2966 if (zip
->hctx_valid
)
2967 archive_hmac_sha1_cleanup(&zip
->hctx
);
2968 zip
->tctx_valid
= zip
->cctx_valid
= zip
->hctx_valid
= 0;
2969 __archive_read_reset_passphrase(a
);
2971 /* File entries are sorted by the header offset, we should mostly
2972 * use __archive_read_consume to advance a read point to avoid redundant
2974 offset
= archive_filter_bytes(&a
->archive
, 0);
2975 if (offset
< zip
->entry
->local_header_offset
)
2976 __archive_read_consume(a
,
2977 zip
->entry
->local_header_offset
- offset
);
2978 else if (offset
!= zip
->entry
->local_header_offset
) {
2979 __archive_read_seek(a
, zip
->entry
->local_header_offset
,
2982 zip
->unconsumed
= 0;
2983 r
= zip_read_local_file_header(a
, entry
, zip
);
2984 if (r
!= ARCHIVE_OK
)
2987 int ret2
= zip_read_mac_metadata(a
, entry
, rsrc
);
2995 * We're going to seek for the next header anyway, so we don't
2996 * need to bother doing anything here.
2999 archive_read_format_zip_read_data_skip_seekable(struct archive_read
*a
)
3002 zip
= (struct zip
*)(a
->format
->data
);
3004 zip
->unconsumed
= 0;
3005 return (ARCHIVE_OK
);
3009 archive_read_support_format_zip_seekable(struct archive
*_a
)
3011 struct archive_read
*a
= (struct archive_read
*)_a
;
3015 archive_check_magic(_a
, ARCHIVE_READ_MAGIC
,
3016 ARCHIVE_STATE_NEW
, "archive_read_support_format_zip_seekable");
3018 zip
= (struct zip
*)calloc(1, sizeof(*zip
));
3020 archive_set_error(&a
->archive
, ENOMEM
,
3021 "Can't allocate zip data");
3022 return (ARCHIVE_FATAL
);
3025 #ifdef HAVE_COPYFILE_H
3026 /* Set this by default on Mac OS. */
3027 zip
->process_mac_extensions
= 1;
3031 * Until enough data has been read, we cannot tell about
3032 * any encrypted entries yet.
3034 zip
->has_encrypted_entries
= ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW
;
3035 zip
->crc32func
= real_crc32
;
3037 r
= __archive_read_register_format(a
,
3040 archive_read_format_zip_seekable_bid
,
3041 archive_read_format_zip_options
,
3042 archive_read_format_zip_seekable_read_header
,
3043 archive_read_format_zip_read_data
,
3044 archive_read_format_zip_read_data_skip_seekable
,
3046 archive_read_format_zip_cleanup
,
3047 archive_read_support_format_zip_capabilities_seekable
,
3048 archive_read_format_zip_has_encrypted_entries
);
3050 if (r
!= ARCHIVE_OK
)
3052 return (ARCHIVE_OK
);