2 * Copyright (c) 2004 Tim Kientzle
3 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
41 #include "archive_endian.h"
42 #include "archive_entry.h"
43 #include "archive_entry_locale.h"
44 #include "archive_private.h"
45 #include "archive_rb.h"
46 #include "archive_read_private.h"
49 #include "archive_crc32.h"
53 struct archive_rb_node node;
54 int64_t local_header_offset;
55 int64_t compressed_size;
56 int64_t uncompressed_size;
59 struct archive_entry *entry;
60 struct archive_string rsrcname;
72 /* Structural information about the archive. */
73 int64_t end_of_central_directory_offset;
74 int64_t central_directory_offset;
75 size_t central_directory_size;
76 size_t central_directory_entries;
77 char have_central_directory;
80 /* List of entries (seekable Zip only) */
81 size_t entries_remaining;
82 struct zip_entry *zip_entries;
83 struct zip_entry *entry;
84 struct archive_rb_tree tree;
85 struct archive_rb_tree tree_rsrc;
89 /* entry_bytes_remaining is the number of bytes we expect. */
90 int64_t entry_bytes_remaining;
92 /* These count the number of bytes actually read for the entry. */
93 int64_t entry_compressed_bytes_read;
94 int64_t entry_uncompressed_bytes_read;
96 /* Running CRC32 of the decompressed data */
97 unsigned long entry_crc32;
99 /* Flags to mark progress of decompression. */
100 char decompress_init;
103 ssize_t filename_length;
104 ssize_t extra_length;
106 unsigned char *uncompressed_buffer;
107 size_t uncompressed_buffer_size;
113 struct archive_string extra;
114 struct archive_string_conv *sconv;
115 struct archive_string_conv *sconv_default;
116 struct archive_string_conv *sconv_utf8;
117 int init_default_conversion;
118 char format_name[64];
121 #define ZIP_LENGTH_AT_END 8
122 #define ZIP_ENCRYPTED (1<<0)
123 #define ZIP_STRONG_ENCRYPTED (1<<6)
124 #define ZIP_UTF8_NAME (1<<11)
126 static int archive_read_format_zip_streamable_bid(struct archive_read *,
128 static int archive_read_format_zip_seekable_bid(struct archive_read *,
130 static int archive_read_format_zip_options(struct archive_read *,
131 const char *, const char *);
132 static int archive_read_format_zip_cleanup(struct archive_read *);
133 static int archive_read_format_zip_read_data(struct archive_read *,
134 const void **, size_t *, int64_t *);
135 static int archive_read_format_zip_read_data_skip(struct archive_read *a);
136 static int archive_read_format_zip_seekable_read_header(
137 struct archive_read *, struct archive_entry *);
138 static int archive_read_format_zip_streamable_read_header(
139 struct archive_read *, struct archive_entry *);
140 static ssize_t zip_get_local_file_header_size(struct archive_read *, size_t);
142 static int zip_deflate_init(struct archive_read *, struct zip *);
143 static int zip_read_data_deflate(struct archive_read *a, const void **buff,
144 size_t *size, int64_t *offset);
146 static int zip_read_data_none(struct archive_read *a, const void **buff,
147 size_t *size, int64_t *offset);
148 static int zip_read_local_file_header(struct archive_read *a,
149 struct archive_entry *entry, struct zip *);
150 static time_t zip_time(const char *);
151 static const char *compression_name(int compression);
152 static void process_extra(const char *, size_t, struct zip_entry *);
154 int archive_read_support_format_zip_streamable(struct archive *);
155 int archive_read_support_format_zip_seekable(struct archive *);
158 archive_read_support_format_zip_streamable(struct archive *_a)
160 struct archive_read *a = (struct archive_read *)_a;
164 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
165 ARCHIVE_STATE_NEW, "archive_read_support_format_zip");
167 zip = (struct zip *)malloc(sizeof(*zip));
169 archive_set_error(&a->archive, ENOMEM,
170 "Can't allocate zip data");
171 return (ARCHIVE_FATAL);
173 memset(zip, 0, sizeof(*zip));
175 r = __archive_read_register_format(a,
178 archive_read_format_zip_streamable_bid,
179 archive_read_format_zip_options,
180 archive_read_format_zip_streamable_read_header,
181 archive_read_format_zip_read_data,
182 archive_read_format_zip_read_data_skip,
184 archive_read_format_zip_cleanup);
192 archive_read_support_format_zip_seekable(struct archive *_a)
194 struct archive_read *a = (struct archive_read *)_a;
198 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
199 ARCHIVE_STATE_NEW, "archive_read_support_format_zip_seekable");
201 zip = (struct zip *)malloc(sizeof(*zip));
203 archive_set_error(&a->archive, ENOMEM,
204 "Can't allocate zip data");
205 return (ARCHIVE_FATAL);
207 memset(zip, 0, sizeof(*zip));
209 r = __archive_read_register_format(a,
212 archive_read_format_zip_seekable_bid,
213 archive_read_format_zip_options,
214 archive_read_format_zip_seekable_read_header,
215 archive_read_format_zip_read_data,
216 archive_read_format_zip_read_data_skip,
218 archive_read_format_zip_cleanup);
226 archive_read_support_format_zip(struct archive *a)
229 r = archive_read_support_format_zip_streamable(a);
232 return (archive_read_support_format_zip_seekable(a));
236 * TODO: This is a performance sink because it forces the read core to
237 * drop buffered data from the start of file, which will then have to
238 * be re-read again if this bidder loses.
240 * We workaround this a little by passing in the best bid so far so
241 * that later bidders can do nothing if they know they'll never
242 * outbid. But we can certainly do better...
245 archive_read_format_zip_seekable_bid(struct archive_read *a, int best_bid)
247 struct zip *zip = (struct zip *)a->format->data;
251 /* If someone has already bid more than 32, then avoid
252 trashing the look-ahead buffers with a seek. */
256 filesize = __archive_read_seek(a, -22, SEEK_END);
257 /* If we can't seek, then we can't bid. */
261 /* TODO: More robust search for end of central directory record. */
262 if ((p = __archive_read_ahead(a, 22, NULL)) == NULL)
264 /* First four bytes are signature for end of central directory
265 record. Four zero bytes ensure this isn't a multi-volume
266 Zip file (which we don't yet support). */
267 if (memcmp(p, "PK\005\006\000\000\000\000", 8) != 0) {
272 * If there is a comment in end of central directory
273 * record, 22 bytes are too short. we have to read more
274 * to properly detect the record. Hopefully, a length
275 * of the comment is not longer than 16362 bytes(16K-22).
277 if (filesize + 22 > 1024 * 16) {
279 filesize = __archive_read_seek(a, tail * -1, SEEK_END);
281 tail = filesize + 22;
282 filesize = __archive_read_seek(a, 0, SEEK_SET);
286 if ((p = __archive_read_ahead(a, (size_t)tail, NULL)) == NULL)
288 for (found = 0, i = 0;!found && i < tail - 22;) {
292 "PK\005\006\000\000\000\000", 8) == 0) {
295 (22 + archive_le16dec(p+20));
300 case 'K': i += 7; break;
301 case 005: i += 6; break;
302 case 006: i += 5; break;
303 default: i += 1; break;
310 /* Since we've already done the hard work of finding the
311 end of central directory record, let's save the important
313 zip->central_directory_entries = archive_le16dec(p + 10);
314 zip->central_directory_size = archive_le32dec(p + 12);
315 zip->central_directory_offset = archive_le32dec(p + 16);
316 zip->end_of_central_directory_offset = filesize;
318 /* Just one volume, so central dir must all be on this volume. */
319 if (zip->central_directory_entries != archive_le16dec(p + 8))
321 /* Central directory can't extend beyond end of this file. */
322 if (zip->central_directory_offset +
323 (int64_t)zip->central_directory_size > filesize)
326 /* This is just a tiny bit higher than the maximum returned by
327 the streaming Zip bidder. This ensures that the more accurate
328 seeking Zip parser wins whenever seek is available. */
333 cmp_node(const struct archive_rb_node *n1, const struct archive_rb_node *n2)
335 const struct zip_entry *e1 = (const struct zip_entry *)n1;
336 const struct zip_entry *e2 = (const struct zip_entry *)n2;
338 return ((int)(e2->local_header_offset - e1->local_header_offset));
342 cmp_key(const struct archive_rb_node *n, const void *key)
344 /* This function won't be called */
345 (void)n; /* UNUSED */
346 (void)key; /* UNUSED */
351 rsrc_cmp_node(const struct archive_rb_node *n1,
352 const struct archive_rb_node *n2)
354 const struct zip_entry *e1 = (const struct zip_entry *)n1;
355 const struct zip_entry *e2 = (const struct zip_entry *)n2;
357 return (strcmp(e2->rsrcname.s, e1->rsrcname.s));
361 rsrc_cmp_key(const struct archive_rb_node *n, const void *key)
363 const struct zip_entry *e = (const struct zip_entry *)n;
364 return (strcmp((const char *)key, e->rsrcname.s));
368 rsrc_basename(const char *name, size_t name_length)
374 s = memchr(s, '/', name_length - (s - name));
383 expose_parent_dirs(struct zip *zip, const char *name, size_t name_length)
385 struct archive_string str;
386 struct zip_entry *dir;
389 archive_string_init(&str);
390 archive_strncpy(&str, name, name_length);
392 s = strrchr(str.s, '/');
396 /* Transfer the parent directory from zip->tree_rsrc RB
397 * tree to zip->tree RB tree to expose. */
398 dir = (struct zip_entry *)
399 __archive_rb_tree_find_node(&zip->tree_rsrc, str.s);
402 __archive_rb_tree_remove_node(&zip->tree_rsrc, &dir->node);
403 archive_string_free(&dir->rsrcname);
404 __archive_rb_tree_insert_node(&zip->tree, &dir->node);
406 archive_string_free(&str);
410 slurp_central_directory(struct archive_read *a, struct zip *zip)
414 static const struct archive_rb_tree_ops rb_ops = {
417 static const struct archive_rb_tree_ops rb_rsrc_ops = {
418 &rsrc_cmp_node, &rsrc_cmp_key
422 * Consider the archive file we are reading may be SFX.
423 * So we have to calculate a SFX header size to revise
424 * ZIP header offsets.
426 correction = zip->end_of_central_directory_offset -
427 (zip->central_directory_offset + zip->central_directory_size);
428 /* The central directory offset is relative value, and so
429 * we revise this offset for SFX. */
430 zip->central_directory_offset += correction;
432 __archive_read_seek(a, zip->central_directory_offset, SEEK_SET);
433 zip->offset = zip->central_directory_offset;
434 __archive_rb_tree_init(&zip->tree, &rb_ops);
435 __archive_rb_tree_init(&zip->tree_rsrc, &rb_rsrc_ops);
437 zip->zip_entries = calloc(zip->central_directory_entries,
438 sizeof(struct zip_entry));
439 for (i = 0; i < zip->central_directory_entries; ++i) {
440 struct zip_entry *zip_entry = &zip->zip_entries[i];
441 size_t filename_length, extra_length, comment_length;
442 uint32_t external_attributes;
443 const char *name, *p, *r;
445 if ((p = __archive_read_ahead(a, 46, NULL)) == NULL)
446 return ARCHIVE_FATAL;
447 if (memcmp(p, "PK\001\002", 4) != 0) {
448 archive_set_error(&a->archive,
449 -1, "Invalid central directory signature");
450 return ARCHIVE_FATAL;
452 zip->have_central_directory = 1;
453 /* version = p[4]; */
454 zip_entry->system = p[5];
455 /* version_required = archive_le16dec(p + 6); */
456 zip_entry->flags = archive_le16dec(p + 8);
457 zip_entry->compression = (char)archive_le16dec(p + 10);
458 zip_entry->mtime = zip_time(p + 12);
459 zip_entry->crc32 = archive_le32dec(p + 16);
460 zip_entry->compressed_size = archive_le32dec(p + 20);
461 zip_entry->uncompressed_size = archive_le32dec(p + 24);
462 filename_length = archive_le16dec(p + 28);
463 extra_length = archive_le16dec(p + 30);
464 comment_length = archive_le16dec(p + 32);
465 /* disk_start = archive_le16dec(p + 34); */ /* Better be zero. */
466 /* internal_attributes = archive_le16dec(p + 36); */ /* text bit */
467 external_attributes = archive_le32dec(p + 38);
468 zip_entry->local_header_offset =
469 archive_le32dec(p + 42) + correction;
471 /* If we can't guess the mode, leave it zero here;
472 when we read the local file header we might get
475 if (zip_entry->system == 3) {
476 zip_entry->mode = external_attributes >> 16;
480 * Mac resource fork files are stored under the
481 * "__MACOSX/" directory, so we should check if
484 /* Make sure we have the file name. */
485 if ((p = __archive_read_ahead(a, 46 + filename_length, NULL))
487 return ARCHIVE_FATAL;
489 r = rsrc_basename(name, filename_length);
490 if (filename_length >= 9 &&
491 strncmp("__MACOSX/", name, 9) == 0) {
492 /* If this file is not a resource fork nor
493 * a directory. We should treat it as a non
494 * resource fork file to expose it. */
495 if (name[filename_length-1] != '/' &&
496 (r - name < 3 || r[0] != '.' || r[1] != '_')) {
497 __archive_rb_tree_insert_node(&zip->tree,
499 /* Expose its parent directories. */
500 expose_parent_dirs(zip, name, filename_length);
502 /* This file is a resource fork file or
504 archive_strncpy(&(zip_entry->rsrcname), name,
506 __archive_rb_tree_insert_node(&zip->tree_rsrc,
510 /* Generate resource fork name to find its resource
511 * file at zip->tree_rsrc. */
512 archive_strcpy(&(zip_entry->rsrcname), "__MACOSX/");
513 archive_strncat(&(zip_entry->rsrcname), name, r - name);
514 archive_strcat(&(zip_entry->rsrcname), "._");
515 archive_strncat(&(zip_entry->rsrcname),
516 name + (r - name), filename_length - (r - name));
517 /* Register an entry to RB tree to sort it by
519 __archive_rb_tree_insert_node(&zip->tree,
523 /* We don't read the filename until we get to the
524 local file header. Reading it here would speed up
525 table-of-contents operations (removing the need to
526 find and read local file header to get the
527 filename) at the cost of requiring a lot of extra
529 /* We don't read the extra block here. We assume it
530 will be duplicated at the local file header. */
531 __archive_read_consume(a,
532 46 + filename_length + extra_length + comment_length);
539 zip_read_consume(struct archive_read *a, int64_t bytes)
541 struct zip *zip = (struct zip *)a->format->data;
544 skip = __archive_read_consume(a, bytes);
551 zip_read_mac_metadata(struct archive_read *a, struct archive_entry *entry,
552 struct zip_entry *rsrc)
554 struct zip *zip = (struct zip *)a->format->data;
555 unsigned char *metadata, *mp;
556 int64_t offset = zip->offset;
557 size_t remaining_bytes, metadata_bytes;
559 int ret = ARCHIVE_OK, eof;
561 switch(rsrc->compression) {
562 case 0: /* No compression. */
564 case 8: /* Deflate compression. */
567 default: /* Unsupported compression. */
568 /* Return a warning. */
569 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
570 "Unsupported ZIP compression method (%s)",
571 compression_name(rsrc->compression));
572 /* We can't decompress this entry, but we will
573 * be able to skip() it and try the next entry. */
574 return (ARCHIVE_WARN);
577 if (rsrc->uncompressed_size > (128 * 1024)) {
578 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
579 "Mac metadata is too large: %jd > 128K bytes",
580 (intmax_t)rsrc->uncompressed_size);
581 return (ARCHIVE_WARN);
584 metadata = malloc((size_t)rsrc->uncompressed_size);
585 if (metadata == NULL) {
586 archive_set_error(&a->archive, ENOMEM,
587 "Can't allocate memory for Mac metadata");
588 return (ARCHIVE_FATAL);
591 if (zip->offset < rsrc->local_header_offset)
592 zip_read_consume(a, rsrc->local_header_offset - zip->offset);
593 else if (zip->offset != rsrc->local_header_offset) {
594 __archive_read_seek(a, rsrc->local_header_offset, SEEK_SET);
595 zip->offset = zip->entry->local_header_offset;
598 hsize = zip_get_local_file_header_size(a, 0);
599 zip_read_consume(a, hsize);
601 remaining_bytes = (size_t)rsrc->compressed_size;
602 metadata_bytes = (size_t)rsrc->uncompressed_size;
605 while (!eof && remaining_bytes) {
606 const unsigned char *p;
610 p = __archive_read_ahead(a, 1, &bytes_avail);
612 archive_set_error(&a->archive,
613 ARCHIVE_ERRNO_FILE_FORMAT,
614 "Truncated ZIP file header");
616 goto exit_mac_metadata;
618 if ((size_t)bytes_avail > remaining_bytes)
619 bytes_avail = remaining_bytes;
620 switch(rsrc->compression) {
621 case 0: /* No compression. */
622 memcpy(mp, p, bytes_avail);
623 bytes_used = (size_t)bytes_avail;
624 metadata_bytes -= bytes_used;
626 if (metadata_bytes == 0)
630 case 8: /* Deflate compression. */
634 ret = zip_deflate_init(a, zip);
635 if (ret != ARCHIVE_OK)
636 goto exit_mac_metadata;
637 zip->stream.next_in =
638 (Bytef *)(uintptr_t)(const void *)p;
639 zip->stream.avail_in = (uInt)bytes_avail;
640 zip->stream.total_in = 0;
641 zip->stream.next_out = mp;
642 zip->stream.avail_out = (uInt)metadata_bytes;
643 zip->stream.total_out = 0;
645 r = inflate(&zip->stream, 0);
653 archive_set_error(&a->archive, ENOMEM,
654 "Out of memory for ZIP decompression");
656 goto exit_mac_metadata;
658 archive_set_error(&a->archive,
660 "ZIP decompression failed (%d)", r);
662 goto exit_mac_metadata;
664 bytes_used = zip->stream.total_in;
665 metadata_bytes -= zip->stream.total_out;
666 mp += zip->stream.total_out;
674 zip_read_consume(a, bytes_used);
675 remaining_bytes -= bytes_used;
677 archive_entry_copy_mac_metadata(entry, metadata,
678 (size_t)rsrc->uncompressed_size - metadata_bytes);
680 __archive_read_seek(a, offset, SEEK_SET);
681 zip->offset = offset;
683 zip->decompress_init = 0;
689 archive_read_format_zip_seekable_read_header(struct archive_read *a,
690 struct archive_entry *entry)
692 struct zip *zip = (struct zip *)a->format->data;
693 struct zip_entry *rsrc;
694 int r, ret = ARCHIVE_OK;
696 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
697 if (a->archive.archive_format_name == NULL)
698 a->archive.archive_format_name = "ZIP";
700 if (zip->zip_entries == NULL) {
701 r = slurp_central_directory(a, zip);
702 zip->entries_remaining = zip->central_directory_entries;
705 /* Get first entry whose local header offset is lower than
706 * other entries in the archive file. */
708 (struct zip_entry *)ARCHIVE_RB_TREE_MIN(&zip->tree);
709 } else if (zip->entry != NULL) {
710 /* Get next entry in local header offset order. */
711 zip->entry = (struct zip_entry *)__archive_rb_tree_iterate(
712 &zip->tree, &zip->entry->node, ARCHIVE_RB_DIR_RIGHT);
715 if (zip->entries_remaining <= 0 || zip->entry == NULL)
717 --zip->entries_remaining;
719 if (zip->entry->rsrcname.s)
720 rsrc = (struct zip_entry *)__archive_rb_tree_find_node(
721 &zip->tree_rsrc, zip->entry->rsrcname.s);
725 /* File entries are sorted by the header offset, we should mostly
726 * use zip_read_consume to advance a read point to avoid redundant
728 if (zip->offset < zip->entry->local_header_offset)
730 zip->entry->local_header_offset - zip->offset);
731 else if (zip->offset != zip->entry->local_header_offset) {
732 __archive_read_seek(a, zip->entry->local_header_offset,
734 zip->offset = zip->entry->local_header_offset;
737 r = zip_read_local_file_header(a, entry, zip);
740 if ((zip->entry->mode & AE_IFMT) == AE_IFLNK) {
742 struct archive_string_conv *sconv;
743 size_t linkname_length = (size_t)archive_entry_size(entry);
745 archive_entry_set_size(entry, 0);
746 p = __archive_read_ahead(a, linkname_length, NULL);
748 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
749 "Truncated Zip file");
750 return ARCHIVE_FATAL;
754 if (sconv == NULL && (zip->entry->flags & ZIP_UTF8_NAME))
755 sconv = zip->sconv_utf8;
757 sconv = zip->sconv_default;
758 if (archive_entry_copy_symlink_l(entry, p, linkname_length,
760 if (errno != ENOMEM && sconv == zip->sconv_utf8 &&
761 (zip->entry->flags & ZIP_UTF8_NAME))
762 archive_entry_copy_symlink_l(entry, p,
763 linkname_length, NULL);
764 if (errno == ENOMEM) {
765 archive_set_error(&a->archive, ENOMEM,
766 "Can't allocate memory for Symlink");
767 return (ARCHIVE_FATAL);
770 * Since there is no character-set regulation for
771 * symlink name, do not report the conversion error
772 * in an automatic conversion.
774 if (sconv != zip->sconv_utf8 ||
775 (zip->entry->flags & ZIP_UTF8_NAME) == 0) {
776 archive_set_error(&a->archive,
777 ARCHIVE_ERRNO_FILE_FORMAT,
778 "Symlink cannot be converted "
779 "from %s to current locale.",
780 archive_string_conversion_charset_name(
787 int ret2 = zip_read_mac_metadata(a, entry, rsrc);
795 archive_read_format_zip_streamable_bid(struct archive_read *a, int best_bid)
799 (void)best_bid; /* UNUSED */
801 if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
805 * Bid of 30 here is: 16 bits for "PK",
806 * next 16-bit field has four options (-2 bits).
809 if (p[0] == 'P' && p[1] == 'K') {
810 if ((p[2] == '\001' && p[3] == '\002')
811 || (p[2] == '\003' && p[3] == '\004')
812 || (p[2] == '\005' && p[3] == '\006')
813 || (p[2] == '\007' && p[3] == '\010')
814 || (p[2] == '0' && p[3] == '0'))
818 /* TODO: It's worth looking ahead a little bit for a valid
819 * PK signature. In particular, that would make it possible
820 * to read some UUEncoded SFX files or SFX files coming from
821 * a network socket. */
827 archive_read_format_zip_options(struct archive_read *a,
828 const char *key, const char *val)
831 int ret = ARCHIVE_FAILED;
833 zip = (struct zip *)(a->format->data);
834 if (strcmp(key, "compat-2x") == 0) {
835 /* Handle filnames as libarchive 2.x */
836 zip->init_default_conversion = (val != NULL) ? 1 : 0;
838 } else if (strcmp(key, "hdrcharset") == 0) {
839 if (val == NULL || val[0] == 0)
840 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
841 "zip: hdrcharset option needs a character-set name"
844 zip->sconv = archive_string_conversion_from_charset(
845 &a->archive, val, 0);
846 if (zip->sconv != NULL) {
847 if (strcmp(val, "UTF-8") == 0)
848 zip->sconv_utf8 = zip->sconv;
856 /* Note: The "warn" return is just to inform the options
857 * supervisor that we didn't handle it. It will generate
858 * a suitable error if no one used this option. */
859 return (ARCHIVE_WARN);
863 archive_read_format_zip_streamable_read_header(struct archive_read *a,
864 struct archive_entry *entry)
868 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
869 if (a->archive.archive_format_name == NULL)
870 a->archive.archive_format_name = "ZIP";
872 zip = (struct zip *)(a->format->data);
874 /* Make sure we have a zip_entry structure to use. */
875 if (zip->zip_entries == NULL) {
876 zip->zip_entries = malloc(sizeof(struct zip_entry));
877 if (zip->zip_entries == NULL) {
878 archive_set_error(&a->archive, ENOMEM,
880 return ARCHIVE_FATAL;
883 zip->entry = zip->zip_entries;
884 memset(zip->entry, 0, sizeof(struct zip_entry));
886 /* Search ahead for the next local file header. */
887 zip_read_consume(a, zip->unconsumed);
894 p = __archive_read_ahead(a, 4, &bytes);
896 return (ARCHIVE_FATAL);
899 while (p + 4 <= end) {
900 if (p[0] == 'P' && p[1] == 'K') {
901 if (p[2] == '\001' && p[3] == '\002')
902 /* Beginning of central directory. */
903 return (ARCHIVE_EOF);
905 if (p[2] == '\003' && p[3] == '\004') {
906 /* Regular file entry. */
907 zip_read_consume(a, skipped);
908 return zip_read_local_file_header(a,
912 if (p[2] == '\005' && p[3] == '\006')
913 /* End of central directory. */
914 return (ARCHIVE_EOF);
919 zip_read_consume(a, skipped);
924 zip_get_local_file_header_size(struct archive_read *a, size_t extra)
927 ssize_t filename_length, extra_length;
929 if ((p = __archive_read_ahead(a, extra + 30, NULL)) == NULL) {
930 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
931 "Truncated ZIP file header");
932 return (ARCHIVE_WARN);
936 if (memcmp(p, "PK\003\004", 4) != 0) {
937 archive_set_error(&a->archive, -1, "Damaged Zip archive");
940 filename_length = archive_le16dec(p + 26);
941 extra_length = archive_le16dec(p + 28);
943 return (30 + filename_length + extra_length);
947 * Assumes file pointer is at beginning of local file header.
950 zip_read_local_file_header(struct archive_read *a, struct archive_entry *entry,
957 size_t len, filename_length, extra_length;
958 struct archive_string_conv *sconv;
959 struct zip_entry *zip_entry = zip->entry;
960 uint32_t local_crc32;
961 int64_t compressed_size, uncompressed_size;
962 int ret = ARCHIVE_OK;
965 zip->decompress_init = 0;
966 zip->end_of_entry = 0;
967 zip->entry_uncompressed_bytes_read = 0;
968 zip->entry_compressed_bytes_read = 0;
969 zip->entry_crc32 = crc32(0, NULL, 0);
971 /* Setup default conversion. */
972 if (zip->sconv == NULL && !zip->init_default_conversion) {
974 archive_string_default_conversion_for_read(&(a->archive));
975 zip->init_default_conversion = 1;
978 if ((p = __archive_read_ahead(a, 30, NULL)) == NULL) {
979 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
980 "Truncated ZIP file header");
981 return (ARCHIVE_FATAL);
984 if (memcmp(p, "PK\003\004", 4) != 0) {
985 archive_set_error(&a->archive, -1, "Damaged Zip archive");
986 return ARCHIVE_FATAL;
989 zip_entry->system = p[5];
990 zip_entry->flags = archive_le16dec(p + 6);
991 zip_entry->compression = (char)archive_le16dec(p + 8);
992 zip_entry->mtime = zip_time(p + 10);
993 local_crc32 = archive_le32dec(p + 14);
994 compressed_size = archive_le32dec(p + 18);
995 uncompressed_size = archive_le32dec(p + 22);
996 filename_length = archive_le16dec(p + 26);
997 extra_length = archive_le16dec(p + 28);
999 zip_read_consume(a, 30);
1001 if (zip->have_central_directory) {
1002 /* If we read the central dir entry, we must have size
1003 * information as well, so ignore the length-at-end flag. */
1004 zip_entry->flags &= ~ZIP_LENGTH_AT_END;
1005 /* If we have values from both the local file header
1006 and the central directory, warn about mismatches
1007 which might indicate a damaged file. But some
1008 writers always put zero in the local header; don't
1009 bother warning about that. */
1010 if (local_crc32 != 0 && local_crc32 != zip_entry->crc32) {
1011 archive_set_error(&a->archive,
1012 ARCHIVE_ERRNO_FILE_FORMAT,
1013 "Inconsistent CRC32 values");
1016 if (compressed_size != 0
1017 && compressed_size != zip_entry->compressed_size) {
1018 archive_set_error(&a->archive,
1019 ARCHIVE_ERRNO_FILE_FORMAT,
1020 "Inconsistent compressed size");
1023 if (uncompressed_size != 0
1024 && uncompressed_size != zip_entry->uncompressed_size) {
1025 archive_set_error(&a->archive,
1026 ARCHIVE_ERRNO_FILE_FORMAT,
1027 "Inconsistent uncompressed size");
1031 /* If we don't have the CD info, use whatever we do have. */
1032 zip_entry->crc32 = local_crc32;
1033 zip_entry->compressed_size = compressed_size;
1034 zip_entry->uncompressed_size = uncompressed_size;
1037 /* Read the filename. */
1038 if ((h = __archive_read_ahead(a, filename_length, NULL)) == NULL) {
1039 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1040 "Truncated ZIP file header");
1041 return (ARCHIVE_FATAL);
1043 if (zip_entry->flags & ZIP_UTF8_NAME) {
1044 /* The filename is stored to be UTF-8. */
1045 if (zip->sconv_utf8 == NULL) {
1047 archive_string_conversion_from_charset(
1048 &a->archive, "UTF-8", 1);
1049 if (zip->sconv_utf8 == NULL)
1050 return (ARCHIVE_FATAL);
1052 sconv = zip->sconv_utf8;
1053 } else if (zip->sconv != NULL)
1056 sconv = zip->sconv_default;
1058 if (archive_entry_copy_pathname_l(entry,
1059 h, filename_length, sconv) != 0) {
1060 if (errno == ENOMEM) {
1061 archive_set_error(&a->archive, ENOMEM,
1062 "Can't allocate memory for Pathname");
1063 return (ARCHIVE_FATAL);
1065 archive_set_error(&a->archive,
1066 ARCHIVE_ERRNO_FILE_FORMAT,
1067 "Pathname cannot be converted "
1068 "from %s to current locale.",
1069 archive_string_conversion_charset_name(sconv));
1072 zip_read_consume(a, filename_length);
1074 if (zip_entry->mode == 0) {
1075 /* Especially in streaming mode, we can end up
1076 here without having seen any mode information.
1077 Guess from the filename. */
1078 wp = archive_entry_pathname_w(entry);
1081 if (len > 0 && wp[len - 1] == L'/')
1082 zip_entry->mode = AE_IFDIR | 0777;
1084 zip_entry->mode = AE_IFREG | 0666;
1086 cp = archive_entry_pathname(entry);
1087 len = (cp != NULL)?strlen(cp):0;
1088 if (len > 0 && cp[len - 1] == '/')
1089 zip_entry->mode = AE_IFDIR | 0777;
1091 zip_entry->mode = AE_IFREG | 0666;
1095 /* Read the extra data. */
1096 if ((h = __archive_read_ahead(a, extra_length, NULL)) == NULL) {
1097 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1098 "Truncated ZIP file header");
1099 return (ARCHIVE_FATAL);
1101 process_extra(h, extra_length, zip_entry);
1102 zip_read_consume(a, extra_length);
1104 /* Populate some additional entry fields: */
1105 archive_entry_set_mode(entry, zip_entry->mode);
1106 archive_entry_set_uid(entry, zip_entry->uid);
1107 archive_entry_set_gid(entry, zip_entry->gid);
1108 archive_entry_set_mtime(entry, zip_entry->mtime, 0);
1109 archive_entry_set_ctime(entry, zip_entry->ctime, 0);
1110 archive_entry_set_atime(entry, zip_entry->atime, 0);
1111 /* Set the size only if it's meaningful. */
1112 if (0 == (zip_entry->flags & ZIP_LENGTH_AT_END))
1113 archive_entry_set_size(entry, zip_entry->uncompressed_size);
1115 zip->entry_bytes_remaining = zip_entry->compressed_size;
1117 /* If there's no body, force read_data() to return EOF immediately. */
1118 if (0 == (zip_entry->flags & ZIP_LENGTH_AT_END)
1119 && zip->entry_bytes_remaining < 1)
1120 zip->end_of_entry = 1;
1122 /* Set up a more descriptive format name. */
1123 sprintf(zip->format_name, "ZIP %d.%d (%s)",
1124 version / 10, version % 10,
1125 compression_name(zip->entry->compression));
1126 a->archive.archive_format_name = zip->format_name;
1132 compression_name(int compression)
1134 static const char *compression_names[] = {
1146 if (0 <= compression && compression <
1147 (int)(sizeof(compression_names)/sizeof(compression_names[0])))
1148 return compression_names[compression];
1153 /* Convert an MSDOS-style date/time into Unix-style time. */
1155 zip_time(const char *p)
1160 msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
1161 msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
1163 memset(&ts, 0, sizeof(ts));
1164 ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
1165 ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
1166 ts.tm_mday = msDate & 0x1f; /* Day of month. */
1167 ts.tm_hour = (msTime >> 11) & 0x1f;
1168 ts.tm_min = (msTime >> 5) & 0x3f;
1169 ts.tm_sec = (msTime << 1) & 0x3e;
1175 archive_read_format_zip_read_data(struct archive_read *a,
1176 const void **buff, size_t *size, int64_t *offset)
1179 struct zip *zip = (struct zip *)(a->format->data);
1181 *offset = zip->entry_uncompressed_bytes_read;
1185 /* If we hit end-of-entry last time, return ARCHIVE_EOF. */
1186 if (zip->end_of_entry)
1187 return (ARCHIVE_EOF);
1189 /* Return EOF immediately if this is a non-regular file. */
1190 if (AE_IFREG != (zip->entry->mode & AE_IFMT))
1191 return (ARCHIVE_EOF);
1193 if (zip->entry->flags & (ZIP_ENCRYPTED | ZIP_STRONG_ENCRYPTED)) {
1194 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1195 "Encrypted file is unsupported");
1196 return (ARCHIVE_FAILED);
1199 zip_read_consume(a, zip->unconsumed);
1200 zip->unconsumed = 0;
1202 switch(zip->entry->compression) {
1203 case 0: /* No compression. */
1204 r = zip_read_data_none(a, buff, size, offset);
1207 case 8: /* Deflate compression. */
1208 r = zip_read_data_deflate(a, buff, size, offset);
1211 default: /* Unsupported compression. */
1212 /* Return a warning. */
1213 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1214 "Unsupported ZIP compression method (%s)",
1215 compression_name(zip->entry->compression));
1216 /* We can't decompress this entry, but we will
1217 * be able to skip() it and try the next entry. */
1218 return (ARCHIVE_FAILED);
1221 if (r != ARCHIVE_OK)
1223 /* Update checksum */
1225 zip->entry_crc32 = crc32(zip->entry_crc32, *buff,
1227 /* If we hit the end, swallow any end-of-data marker. */
1228 if (zip->end_of_entry) {
1229 /* Check file size, CRC against these values. */
1230 if (zip->entry->compressed_size !=
1231 zip->entry_compressed_bytes_read) {
1232 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1233 "ZIP compressed data is wrong size "
1234 "(read %jd, expected %jd)",
1235 (intmax_t)zip->entry_compressed_bytes_read,
1236 (intmax_t)zip->entry->compressed_size);
1237 return (ARCHIVE_WARN);
1239 /* Size field only stores the lower 32 bits of the actual
1241 if ((zip->entry->uncompressed_size & UINT32_MAX)
1242 != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
1243 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1244 "ZIP uncompressed data is wrong size "
1245 "(read %jd, expected %jd)",
1246 (intmax_t)zip->entry_uncompressed_bytes_read,
1247 (intmax_t)zip->entry->uncompressed_size);
1248 return (ARCHIVE_WARN);
1250 /* Check computed CRC against header */
1251 if (zip->entry->crc32 != zip->entry_crc32) {
1252 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1253 "ZIP bad CRC: 0x%lx should be 0x%lx",
1254 (unsigned long)zip->entry_crc32,
1255 (unsigned long)zip->entry->crc32);
1256 return (ARCHIVE_WARN);
1260 return (ARCHIVE_OK);
1264 * Read "uncompressed" data. There are three cases:
1265 * 1) We know the size of the data. This is always true for the
1266 * seeking reader (we've examined the Central Directory already).
1267 * 2) ZIP_LENGTH_AT_END was set, but only the CRC was deferred.
1268 * Info-ZIP seems to do this; we know the size but have to grab
1269 * the CRC from the data descriptor afterwards.
1270 * 3) We're streaming and ZIP_LENGTH_AT_END was specified and
1271 * we have no size information. In this case, we can do pretty
1272 * well by watching for the data descriptor record. The data
1273 * descriptor is 16 bytes and includes a computed CRC that should
1274 * provide a strong check.
1276 * TODO: Technically, the PK\007\010 signature is optional.
1277 * In the original spec, the data descriptor contained CRC
1278 * and size fields but had no leading signature. In practice,
1279 * newer writers seem to provide the signature pretty consistently,
1280 * but we might need to do something more complex here if
1281 * we want to handle older archives that lack that signature.
1283 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
1284 * zip->end_of_entry if it consumes all of the data.
1287 zip_read_data_none(struct archive_read *a, const void **_buff,
1288 size_t *size, int64_t *offset)
1292 ssize_t bytes_avail;
1294 (void)offset; /* UNUSED */
1296 zip = (struct zip *)(a->format->data);
1298 if (zip->entry->flags & ZIP_LENGTH_AT_END) {
1301 /* Grab at least 16 bytes. */
1302 buff = __archive_read_ahead(a, 16, &bytes_avail);
1303 if (bytes_avail < 16) {
1304 /* Zip archives have end-of-archive markers
1305 that are longer than this, so a failure to get at
1306 least 16 bytes really does indicate a truncated
1308 archive_set_error(&a->archive,
1309 ARCHIVE_ERRNO_FILE_FORMAT,
1310 "Truncated ZIP file data");
1311 return (ARCHIVE_FATAL);
1313 /* Check for a complete PK\007\010 signature. */
1315 if (p[0] == 'P' && p[1] == 'K'
1316 && p[2] == '\007' && p[3] == '\010'
1317 && archive_le32dec(p + 4) == zip->entry_crc32
1318 && archive_le32dec(p + 8) ==
1319 zip->entry_compressed_bytes_read
1320 && archive_le32dec(p + 12) ==
1321 zip->entry_uncompressed_bytes_read) {
1322 zip->entry->crc32 = archive_le32dec(p + 4);
1323 zip->entry->compressed_size = archive_le32dec(p + 8);
1324 zip->entry->uncompressed_size = archive_le32dec(p + 12);
1325 zip->end_of_entry = 1;
1326 zip->unconsumed = 16;
1327 return (ARCHIVE_OK);
1329 /* If not at EOF, ensure we consume at least one byte. */
1332 /* Scan forward until we see where a PK\007\010 signature
1334 /* Return bytes up until that point. On the next call,
1335 * the code above will verify the data descriptor. */
1336 while (p < buff + bytes_avail - 4) {
1337 if (p[3] == 'P') { p += 3; }
1338 else if (p[3] == 'K') { p += 2; }
1339 else if (p[3] == '\007') { p += 1; }
1340 else if (p[3] == '\010' && p[2] == '\007'
1341 && p[1] == 'K' && p[0] == 'P') {
1345 bytes_avail = p - buff;
1347 if (zip->entry_bytes_remaining == 0) {
1348 zip->end_of_entry = 1;
1349 return (ARCHIVE_OK);
1351 /* Grab a bunch of bytes. */
1352 buff = __archive_read_ahead(a, 1, &bytes_avail);
1353 if (bytes_avail <= 0) {
1354 archive_set_error(&a->archive,
1355 ARCHIVE_ERRNO_FILE_FORMAT,
1356 "Truncated ZIP file data");
1357 return (ARCHIVE_FATAL);
1359 if (bytes_avail > zip->entry_bytes_remaining)
1360 bytes_avail = (ssize_t)zip->entry_bytes_remaining;
1362 *size = bytes_avail;
1363 zip->entry_bytes_remaining -= bytes_avail;
1364 zip->entry_uncompressed_bytes_read += bytes_avail;
1365 zip->entry_compressed_bytes_read += bytes_avail;
1366 zip->unconsumed += bytes_avail;
1368 return (ARCHIVE_OK);
1373 zip_deflate_init(struct archive_read *a, struct zip *zip)
1377 /* If we haven't yet read any data, initialize the decompressor. */
1378 if (!zip->decompress_init) {
1379 if (zip->stream_valid)
1380 r = inflateReset(&zip->stream);
1382 r = inflateInit2(&zip->stream,
1383 -15 /* Don't check for zlib header */);
1385 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1386 "Can't initialize ZIP decompression.");
1387 return (ARCHIVE_FATAL);
1389 /* Stream structure has been set up. */
1390 zip->stream_valid = 1;
1391 /* We've initialized decompression for this stream. */
1392 zip->decompress_init = 1;
1394 return (ARCHIVE_OK);
1398 zip_read_data_deflate(struct archive_read *a, const void **buff,
1399 size_t *size, int64_t *offset)
1402 ssize_t bytes_avail;
1403 const void *compressed_buff;
1406 (void)offset; /* UNUSED */
1408 zip = (struct zip *)(a->format->data);
1410 /* If the buffer hasn't been allocated, allocate it now. */
1411 if (zip->uncompressed_buffer == NULL) {
1412 zip->uncompressed_buffer_size = 256 * 1024;
1413 zip->uncompressed_buffer
1414 = (unsigned char *)malloc(zip->uncompressed_buffer_size);
1415 if (zip->uncompressed_buffer == NULL) {
1416 archive_set_error(&a->archive, ENOMEM,
1417 "No memory for ZIP decompression");
1418 return (ARCHIVE_FATAL);
1422 r = zip_deflate_init(a, zip);
1423 if (r != ARCHIVE_OK)
1427 * Note: '1' here is a performance optimization.
1428 * Recall that the decompression layer returns a count of
1429 * available bytes; asking for more than that forces the
1430 * decompressor to combine reads by copying data.
1432 compressed_buff = __archive_read_ahead(a, 1, &bytes_avail);
1433 if (0 == (zip->entry->flags & ZIP_LENGTH_AT_END)
1434 && bytes_avail > zip->entry_bytes_remaining) {
1435 bytes_avail = (ssize_t)zip->entry_bytes_remaining;
1437 if (bytes_avail <= 0) {
1438 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1439 "Truncated ZIP file body");
1440 return (ARCHIVE_FATAL);
1444 * A bug in zlib.h: stream.next_in should be marked 'const'
1445 * but isn't (the library never alters data through the
1446 * next_in pointer, only reads it). The result: this ugly
1447 * cast to remove 'const'.
1449 zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
1450 zip->stream.avail_in = (uInt)bytes_avail;
1451 zip->stream.total_in = 0;
1452 zip->stream.next_out = zip->uncompressed_buffer;
1453 zip->stream.avail_out = (uInt)zip->uncompressed_buffer_size;
1454 zip->stream.total_out = 0;
1456 r = inflate(&zip->stream, 0);
1461 zip->end_of_entry = 1;
1464 archive_set_error(&a->archive, ENOMEM,
1465 "Out of memory for ZIP decompression");
1466 return (ARCHIVE_FATAL);
1468 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1469 "ZIP decompression failed (%d)", r);
1470 return (ARCHIVE_FATAL);
1473 /* Consume as much as the compressor actually used. */
1474 bytes_avail = zip->stream.total_in;
1475 zip_read_consume(a, bytes_avail);
1476 zip->entry_bytes_remaining -= bytes_avail;
1477 zip->entry_compressed_bytes_read += bytes_avail;
1479 *size = zip->stream.total_out;
1480 zip->entry_uncompressed_bytes_read += zip->stream.total_out;
1481 *buff = zip->uncompressed_buffer;
1483 if (zip->end_of_entry && (zip->entry->flags & ZIP_LENGTH_AT_END)) {
1486 if (NULL == (p = __archive_read_ahead(a, 16, NULL))) {
1487 archive_set_error(&a->archive,
1488 ARCHIVE_ERRNO_FILE_FORMAT,
1489 "Truncated ZIP end-of-file record");
1490 return (ARCHIVE_FATAL);
1492 /* Consume the optional PK\007\010 marker. */
1493 if (p[0] == 'P' && p[1] == 'K' &&
1494 p[2] == '\007' && p[3] == '\010') {
1495 zip->entry->crc32 = archive_le32dec(p + 4);
1496 zip->entry->compressed_size = archive_le32dec(p + 8);
1497 zip->entry->uncompressed_size = archive_le32dec(p + 12);
1498 zip->unconsumed = 16;
1502 return (ARCHIVE_OK);
1507 archive_read_format_zip_read_data_skip(struct archive_read *a)
1511 zip = (struct zip *)(a->format->data);
1513 /* If we've already read to end of data, we're done. */
1514 if (zip->end_of_entry)
1515 return (ARCHIVE_OK);
1517 /* So we know we're streaming... */
1518 if (0 == (zip->entry->flags & ZIP_LENGTH_AT_END)) {
1519 /* We know the compressed length, so we can just skip. */
1520 int64_t bytes_skipped = zip_read_consume(a,
1521 zip->entry_bytes_remaining + zip->unconsumed);
1522 if (bytes_skipped < 0)
1523 return (ARCHIVE_FATAL);
1524 zip->unconsumed = 0;
1525 return (ARCHIVE_OK);
1528 /* We're streaming and we don't know the length. */
1529 /* If the body is compressed and we know the format, we can
1530 * find an exact end-of-entry by decompressing it. */
1531 switch (zip->entry->compression) {
1533 case 8: /* Deflate compression. */
1534 while (!zip->end_of_entry) {
1536 const void *buff = NULL;
1539 r = zip_read_data_deflate(a, &buff, &size, &offset);
1540 if (r != ARCHIVE_OK)
1545 default: /* Uncompressed or unknown. */
1546 /* Scan for a PK\007\010 signature. */
1547 zip_read_consume(a, zip->unconsumed);
1548 zip->unconsumed = 0;
1550 const char *p, *buff;
1551 ssize_t bytes_avail;
1552 buff = __archive_read_ahead(a, 16, &bytes_avail);
1553 if (bytes_avail < 16) {
1554 archive_set_error(&a->archive,
1555 ARCHIVE_ERRNO_FILE_FORMAT,
1556 "Truncated ZIP file data");
1557 return (ARCHIVE_FATAL);
1560 while (p <= buff + bytes_avail - 16) {
1561 if (p[3] == 'P') { p += 3; }
1562 else if (p[3] == 'K') { p += 2; }
1563 else if (p[3] == '\007') { p += 1; }
1564 else if (p[3] == '\010' && p[2] == '\007'
1565 && p[1] == 'K' && p[0] == 'P') {
1566 zip_read_consume(a, p - buff + 16);
1570 zip_read_consume(a, p - buff);
1576 archive_read_format_zip_cleanup(struct archive_read *a)
1580 zip = (struct zip *)(a->format->data);
1582 if (zip->stream_valid)
1583 inflateEnd(&zip->stream);
1585 if (zip->zip_entries && zip->central_directory_entries) {
1587 for (i = 0; i < zip->central_directory_entries; i++)
1588 archive_string_free(&(zip->zip_entries[i].rsrcname));
1590 free(zip->zip_entries);
1591 free(zip->uncompressed_buffer);
1592 archive_string_free(&(zip->extra));
1594 (a->format->data) = NULL;
1595 return (ARCHIVE_OK);
1599 * The extra data is stored as a list of
1600 * id1+size1+data1 + id2+size2+data2 ...
1601 * triplets. id and size are 2 bytes each.
1604 process_extra(const char *p, size_t extra_length, struct zip_entry* zip_entry)
1606 unsigned offset = 0;
1608 while (offset < extra_length - 4)
1610 unsigned short headerid = archive_le16dec(p + offset);
1611 unsigned short datasize = archive_le16dec(p + offset + 2);
1613 if (offset + datasize > extra_length)
1616 fprintf(stderr, "Header id 0x%x, length %d\n",
1617 headerid, datasize);
1621 /* Zip64 extended information extra field. */
1623 zip_entry->uncompressed_size =
1624 archive_le64dec(p + offset);
1626 zip_entry->compressed_size =
1627 archive_le64dec(p + offset + 8);
1631 /* Extended time field "UT". */
1632 int flags = p[offset];
1635 /* Flag bits indicate which dates are present. */
1639 fprintf(stderr, "mtime: %lld -> %d\n",
1640 (long long)zip_entry->mtime,
1641 archive_le32dec(p + offset));
1645 zip_entry->mtime = archive_le32dec(p + offset);
1653 zip_entry->atime = archive_le32dec(p + offset);
1661 zip_entry->ctime = archive_le32dec(p + offset);
1669 /* Info-ZIP Unix Extra Field (old version) "UX". */
1670 if (datasize >= 8) {
1671 zip_entry->atime = archive_le32dec(p + offset);
1673 archive_le32dec(p + offset + 4);
1675 if (datasize >= 12) {
1677 archive_le16dec(p + offset + 8);
1679 archive_le16dec(p + offset + 10);
1684 /* Info-ZIP Unix Extra Field (type 2) "Ux". */
1686 fprintf(stderr, "uid %d gid %d\n",
1687 archive_le16dec(p + offset),
1688 archive_le16dec(p + offset + 2));
1691 zip_entry->uid = archive_le16dec(p + offset);
1694 archive_le16dec(p + offset + 2);
1698 /* Info-Zip Unix Extra Field (type 3) "ux". */
1699 int uidsize = 0, gidsize = 0;
1701 if (datasize >= 1 && p[offset] == 1) {/* version=1 */
1702 if (datasize >= 4) {
1703 /* get a uid size. */
1704 uidsize = p[offset+1];
1709 else if (uidsize == 4 && datasize >= 6)
1714 if (datasize >= (2 + uidsize + 3)) {
1715 /* get a gid size. */
1716 gidsize = p[offset+2+uidsize];
1720 p+offset+2+uidsize+1);
1721 else if (gidsize == 4 &&
1722 datasize >= (2 + uidsize + 5))
1725 p+offset+2+uidsize+1);
1736 if (offset != extra_length)
1739 "Extra data field contents do not match reported size!\n");