2 * Copyright (c) 2004 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
42 #include "archive_entry.h"
43 #include "archive_private.h"
44 #include "archive_read_private.h"
45 #include "archive_endian.h"
48 #include "archive_crc32.h"
52 /* entry_bytes_remaining is the number of bytes we expect. */
53 int64_t entry_bytes_remaining;
56 /* These count the number of bytes actually read for the entry. */
57 int64_t entry_compressed_bytes_read;
58 int64_t entry_uncompressed_bytes_read;
60 /* Running CRC32 of the decompressed data */
61 unsigned long entry_crc32;
67 const char * compression_name;
75 /* Flags to mark progress of decompression. */
80 ssize_t filename_length;
82 int64_t uncompressed_size;
83 int64_t compressed_size;
85 unsigned char *uncompressed_buffer;
86 size_t uncompressed_buffer_size;
92 struct archive_string pathname;
93 struct archive_string extra;
97 #define ZIP_LENGTH_AT_END 8
99 struct zip_file_header {
106 char compressed_size[4];
107 char uncompressed_size[4];
108 char filename_length[2];
109 char extra_length[2];
112 static const char *compression_names[] = {
124 static int archive_read_format_zip_bid(struct archive_read *);
125 static int archive_read_format_zip_cleanup(struct archive_read *);
126 static int archive_read_format_zip_read_data(struct archive_read *,
127 const void **, size_t *, off_t *);
128 static int archive_read_format_zip_read_data_skip(struct archive_read *a);
129 static int archive_read_format_zip_read_header(struct archive_read *,
130 struct archive_entry *);
131 static int search_next_signature(struct archive_read *);
132 static int zip_read_data_deflate(struct archive_read *a, const void **buff,
133 size_t *size, off_t *offset);
134 static int zip_read_data_none(struct archive_read *a, const void **buff,
135 size_t *size, off_t *offset);
136 static int zip_read_file_header(struct archive_read *a,
137 struct archive_entry *entry, struct zip *zip);
138 static time_t zip_time(const char *);
139 static void process_extra(const void* extra, struct zip* zip);
142 archive_read_support_format_zip(struct archive *_a)
144 struct archive_read *a = (struct archive_read *)_a;
148 zip = (struct zip *)malloc(sizeof(*zip));
150 archive_set_error(&a->archive, ENOMEM, "Can't allocate zip data");
151 return (ARCHIVE_FATAL);
153 memset(zip, 0, sizeof(*zip));
155 r = __archive_read_register_format(a,
158 archive_read_format_zip_bid,
160 archive_read_format_zip_read_header,
161 archive_read_format_zip_read_data,
162 archive_read_format_zip_read_data_skip,
163 archive_read_format_zip_cleanup);
172 archive_read_format_zip_bid(struct archive_read *a)
176 ssize_t bytes_avail, offset;
178 if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
182 * Bid of 30 here is: 16 bits for "PK",
183 * next 16-bit field has four options (-2 bits).
186 if (p[0] == 'P' && p[1] == 'K') {
187 if ((p[2] == '\001' && p[3] == '\002')
188 || (p[2] == '\003' && p[3] == '\004')
189 || (p[2] == '\005' && p[3] == '\006')
190 || (p[2] == '\007' && p[3] == '\010')
191 || (p[2] == '0' && p[3] == '0'))
196 * Attempt to handle self-extracting archives
197 * by noting a PE header and searching forward
198 * up to 128k for a 'PK\003\004' marker.
200 if (p[0] == 'M' && p[1] == 'Z') {
202 * TODO: Optimize by initializing 'offset' to an
203 * estimate of the likely start of the archive data
204 * based on values in the PE header. Note that we
205 * don't need to be exact, but we mustn't skip too
206 * far. The search below will compensate if we
210 while (offset < 124000) {
211 /* Get 4k of data beyond where we stopped. */
212 buff = __archive_read_ahead(a, offset + 4096,
214 if (bytes_avail < offset + 1)
216 p = (const char *)buff + offset;
217 while (p + 9 < (const char *)buff + bytes_avail) {
218 if (p[0] == 'P' && p[1] == 'K' /* signature */
219 && p[2] == 3 && p[3] == 4 /* File entry */
220 && p[8] == 8 /* compression == deflate */
221 && p[9] == 0 /* High byte of compression */
228 offset = p - (const char *)buff;
236 * Search forward for a "PK\003\004" file header. This handles the
237 * case of self-extracting archives, where there is an executable
238 * prepended to the ZIP archive.
241 skip_sfx(struct archive_read *a)
249 * TODO: We should be able to skip forward by a bunch
250 * by lifting some values from the PE header. We don't
251 * need to be exact (we're still going to search forward
252 * to find the header), but it will speed things up and
253 * reduce the chance of a false positive.
256 h = __archive_read_ahead(a, 4, &bytes);
258 return (ARCHIVE_FATAL);
263 * Scan ahead until we find something that looks
264 * like the zip header.
269 /* TODO: Additional verification here. */
270 if (memcmp("PK\003\004", p, 4) == 0) {
271 skip = p - (const char *)h;
272 __archive_read_consume(a, skip);
277 case '\003': p += 1; break;
278 case 'K': p += 2; break;
279 case 'P': p += 3; break;
280 default: p += 4; break;
283 skip = p - (const char *)h;
284 __archive_read_consume(a, skip);
289 archive_read_format_zip_read_header(struct archive_read *a,
290 struct archive_entry *entry)
293 const char *signature;
295 int r = ARCHIVE_OK, r1;
297 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
298 if (a->archive.archive_format_name == NULL)
299 a->archive.archive_format_name = "ZIP";
301 zip = (struct zip *)(a->format->data);
302 zip->decompress_init = 0;
303 zip->end_of_entry = 0;
304 zip->entry_uncompressed_bytes_read = 0;
305 zip->entry_compressed_bytes_read = 0;
306 zip->entry_crc32 = crc32(0, NULL, 0);
307 if ((h = __archive_read_ahead(a, 4, NULL)) == NULL)
308 return (ARCHIVE_FATAL);
310 signature = (const char *)h;
311 if (signature[0] == 'M' && signature[1] == 'Z') {
312 /* This is an executable? Must be self-extracting... */
314 if (r < ARCHIVE_WARN)
316 if ((h = __archive_read_ahead(a, 4, NULL)) == NULL)
317 return (ARCHIVE_FATAL);
318 signature = (const char *)h;
321 /* If we don't see a PK signature here, scan forward. */
322 if (signature[0] != 'P' || signature[1] != 'K') {
323 r = search_next_signature(a);
324 if (r != ARCHIVE_OK) {
325 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
327 return (ARCHIVE_FATAL);
329 if ((h = __archive_read_ahead(a, 4, NULL)) == NULL)
330 return (ARCHIVE_FATAL);
331 signature = (const char *)h;
335 * "PK00" signature is used for "split" archives that
336 * only have a single segment. This means we can just
337 * skip the PK00; the first real file header should follow.
339 if (signature[2] == '0' && signature[3] == '0') {
340 __archive_read_consume(a, 4);
341 if ((h = __archive_read_ahead(a, 4, NULL)) == NULL)
342 return (ARCHIVE_FATAL);
343 signature = (const char *)h;
344 if (signature[0] != 'P' || signature[1] != 'K') {
345 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
347 return (ARCHIVE_FATAL);
351 if (signature[2] == '\001' && signature[3] == '\002') {
352 /* Beginning of central directory. */
353 return (ARCHIVE_EOF);
356 if (signature[2] == '\003' && signature[3] == '\004') {
357 /* Regular file entry. */
358 r1 = zip_read_file_header(a, entry, zip);
359 if (r1 != ARCHIVE_OK)
364 if (signature[2] == '\005' && signature[3] == '\006') {
365 /* End-of-archive record. */
366 return (ARCHIVE_EOF);
369 if (signature[2] == '\007' && signature[3] == '\010') {
371 * We should never encounter this record here;
372 * see ZIP_LENGTH_AT_END handling below for details.
374 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
375 "Bad ZIP file: Unexpected end-of-entry record");
376 return (ARCHIVE_FATAL);
379 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
380 "Damaged ZIP file or unsupported format variant (%d,%d)",
381 signature[2], signature[3]);
382 return (ARCHIVE_FATAL);
386 search_next_signature(struct archive_read *a)
395 h = __archive_read_ahead(a, 4, &bytes);
397 return (ARCHIVE_FATAL);
402 if (p[0] == 'P' && p[1] == 'K') {
403 if ((p[2] == '\001' && p[3] == '\002')
404 || (p[2] == '\003' && p[3] == '\004')
405 || (p[2] == '\005' && p[3] == '\006')
406 || (p[2] == '\007' && p[3] == '\010')
407 || (p[2] == '0' && p[3] == '0')) {
408 skip = p - (const char *)h;
409 __archive_read_consume(a, skip);
415 skip = p - (const char *)h;
416 __archive_read_consume(a, skip);
422 zip_read_file_header(struct archive_read *a, struct archive_entry *entry,
425 const struct zip_file_header *p;
428 if ((p = __archive_read_ahead(a, sizeof *p, NULL)) == NULL) {
429 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
430 "Truncated ZIP file header");
431 return (ARCHIVE_FATAL);
434 zip->version = p->version[0];
435 zip->system = p->version[1];
436 zip->flags = archive_le16dec(p->flags);
437 zip->compression = archive_le16dec(p->compression);
438 if (zip->compression <
439 sizeof(compression_names)/sizeof(compression_names[0]))
440 zip->compression_name = compression_names[zip->compression];
442 zip->compression_name = "??";
443 zip->mtime = zip_time(p->timedate);
449 zip->crc32 = archive_le32dec(p->crc32);
450 zip->filename_length = archive_le16dec(p->filename_length);
451 zip->extra_length = archive_le16dec(p->extra_length);
452 zip->uncompressed_size = archive_le32dec(p->uncompressed_size);
453 zip->compressed_size = archive_le32dec(p->compressed_size);
455 __archive_read_consume(a, sizeof(struct zip_file_header));
458 /* Read the filename. */
459 if ((h = __archive_read_ahead(a, zip->filename_length, NULL)) == NULL) {
460 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
461 "Truncated ZIP file header");
462 return (ARCHIVE_FATAL);
464 if (archive_string_ensure(&zip->pathname, zip->filename_length) == NULL)
465 __archive_errx(1, "Out of memory");
466 archive_strncpy(&zip->pathname, h, zip->filename_length);
467 __archive_read_consume(a, zip->filename_length);
468 archive_entry_set_pathname(entry, zip->pathname.s);
470 if (zip->pathname.s[archive_strlen(&zip->pathname) - 1] == '/')
471 zip->mode = AE_IFDIR | 0777;
473 zip->mode = AE_IFREG | 0777;
475 /* Read the extra data. */
476 if ((h = __archive_read_ahead(a, zip->extra_length, NULL)) == NULL) {
477 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
478 "Truncated ZIP file header");
479 return (ARCHIVE_FATAL);
481 process_extra(h, zip);
482 __archive_read_consume(a, zip->extra_length);
484 /* Populate some additional entry fields: */
485 archive_entry_set_mode(entry, zip->mode);
486 archive_entry_set_uid(entry, zip->uid);
487 archive_entry_set_gid(entry, zip->gid);
488 archive_entry_set_mtime(entry, zip->mtime, 0);
489 archive_entry_set_ctime(entry, zip->ctime, 0);
490 archive_entry_set_atime(entry, zip->atime, 0);
491 /* Set the size only if it's meaningful. */
492 if (0 == (zip->flags & ZIP_LENGTH_AT_END))
493 archive_entry_set_size(entry, zip->uncompressed_size);
495 zip->entry_bytes_remaining = zip->compressed_size;
496 zip->entry_offset = 0;
498 /* If there's no body, force read_data() to return EOF immediately. */
499 if (0 == (zip->flags & ZIP_LENGTH_AT_END)
500 && zip->entry_bytes_remaining < 1)
501 zip->end_of_entry = 1;
503 /* Set up a more descriptive format name. */
504 sprintf(zip->format_name, "ZIP %d.%d (%s)",
505 zip->version / 10, zip->version % 10,
506 zip->compression_name);
507 a->archive.archive_format_name = zip->format_name;
512 /* Convert an MSDOS-style date/time into Unix-style time. */
514 zip_time(const char *p)
519 msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
520 msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
522 memset(&ts, 0, sizeof(ts));
523 ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
524 ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
525 ts.tm_mday = msDate & 0x1f; /* Day of month. */
526 ts.tm_hour = (msTime >> 11) & 0x1f;
527 ts.tm_min = (msTime >> 5) & 0x3f;
528 ts.tm_sec = (msTime << 1) & 0x3e;
534 archive_read_format_zip_read_data(struct archive_read *a,
535 const void **buff, size_t *size, off_t *offset)
540 zip = (struct zip *)(a->format->data);
543 * If we hit end-of-entry last time, clean up and return
544 * ARCHIVE_EOF this time.
546 if (zip->end_of_entry) {
547 *offset = zip->entry_uncompressed_bytes_read;
550 return (ARCHIVE_EOF);
553 switch(zip->compression) {
554 case 0: /* No compression. */
555 r = zip_read_data_none(a, buff, size, offset);
557 case 8: /* Deflate compression. */
558 r = zip_read_data_deflate(a, buff, size, offset);
560 default: /* Unsupported compression. */
564 /* Return a warning. */
565 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
566 "Unsupported ZIP compression method (%s)",
567 zip->compression_name);
568 if (zip->flags & ZIP_LENGTH_AT_END) {
570 * ZIP_LENGTH_AT_END requires us to
571 * decompress the entry in order to
572 * skip it, but we don't know this
573 * compression method, so we give up.
577 /* We can't decompress this entry, but we will
578 * be able to skip() it and try the next entry. */
585 /* Update checksum */
587 zip->entry_crc32 = crc32(zip->entry_crc32, *buff, *size);
588 /* If we hit the end, swallow any end-of-data marker. */
589 if (zip->end_of_entry) {
590 if (zip->flags & ZIP_LENGTH_AT_END) {
593 if ((p = __archive_read_ahead(a, 16, NULL)) == NULL) {
594 archive_set_error(&a->archive,
595 ARCHIVE_ERRNO_FILE_FORMAT,
596 "Truncated ZIP end-of-file record");
597 return (ARCHIVE_FATAL);
599 zip->crc32 = archive_le32dec(p + 4);
600 zip->compressed_size = archive_le32dec(p + 8);
601 zip->uncompressed_size = archive_le32dec(p + 12);
602 __archive_read_consume(a, 16);
604 /* Check file size, CRC against these values. */
605 if (zip->compressed_size != zip->entry_compressed_bytes_read) {
606 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
607 "ZIP compressed data is wrong size");
608 return (ARCHIVE_WARN);
610 /* Size field only stores the lower 32 bits of the actual size. */
611 if ((zip->uncompressed_size & UINT32_MAX)
612 != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
613 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
614 "ZIP uncompressed data is wrong size");
615 return (ARCHIVE_WARN);
617 /* Check computed CRC against header */
618 if (zip->crc32 != zip->entry_crc32) {
619 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
620 "ZIP bad CRC: 0x%lx should be 0x%lx",
621 zip->entry_crc32, zip->crc32);
622 return (ARCHIVE_WARN);
626 /* Return EOF immediately if this is a non-regular file. */
627 if (AE_IFREG != (zip->mode & AE_IFMT))
628 return (ARCHIVE_EOF);
633 * Read "uncompressed" data. According to the current specification,
634 * if ZIP_LENGTH_AT_END is specified, then the size fields in the
635 * initial file header are supposed to be set to zero. This would, of
636 * course, make it impossible for us to read the archive, since we
637 * couldn't determine the end of the file data. Info-ZIP seems to
638 * include the real size fields both before and after the data in this
639 * case (the CRC only appears afterwards), so this works as you would
642 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
643 * zip->end_of_entry if it consumes all of the data.
646 zip_read_data_none(struct archive_read *a, const void **buff,
647 size_t *size, off_t *offset)
652 zip = (struct zip *)(a->format->data);
654 if (zip->entry_bytes_remaining == 0) {
657 *offset = zip->entry_offset;
658 zip->end_of_entry = 1;
662 * Note: '1' here is a performance optimization.
663 * Recall that the decompression layer returns a count of
664 * available bytes; asking for more than that forces the
665 * decompressor to combine reads by copying data.
667 *buff = __archive_read_ahead(a, 1, &bytes_avail);
668 if (bytes_avail <= 0) {
669 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
670 "Truncated ZIP file data");
671 return (ARCHIVE_FATAL);
673 if (bytes_avail > zip->entry_bytes_remaining)
674 bytes_avail = zip->entry_bytes_remaining;
675 __archive_read_consume(a, bytes_avail);
677 *offset = zip->entry_offset;
678 zip->entry_offset += *size;
679 zip->entry_bytes_remaining -= *size;
680 zip->entry_uncompressed_bytes_read += *size;
681 zip->entry_compressed_bytes_read += *size;
687 zip_read_data_deflate(struct archive_read *a, const void **buff,
688 size_t *size, off_t *offset)
692 const void *compressed_buff;
695 zip = (struct zip *)(a->format->data);
697 /* If the buffer hasn't been allocated, allocate it now. */
698 if (zip->uncompressed_buffer == NULL) {
699 zip->uncompressed_buffer_size = 32 * 1024;
700 zip->uncompressed_buffer
701 = (unsigned char *)malloc(zip->uncompressed_buffer_size);
702 if (zip->uncompressed_buffer == NULL) {
703 archive_set_error(&a->archive, ENOMEM,
704 "No memory for ZIP decompression");
705 return (ARCHIVE_FATAL);
709 /* If we haven't yet read any data, initialize the decompressor. */
710 if (!zip->decompress_init) {
711 if (zip->stream_valid)
712 r = inflateReset(&zip->stream);
714 r = inflateInit2(&zip->stream,
715 -15 /* Don't check for zlib header */);
717 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
718 "Can't initialize ZIP decompression.");
719 return (ARCHIVE_FATAL);
721 /* Stream structure has been set up. */
722 zip->stream_valid = 1;
723 /* We've initialized decompression for this stream. */
724 zip->decompress_init = 1;
728 * Note: '1' here is a performance optimization.
729 * Recall that the decompression layer returns a count of
730 * available bytes; asking for more than that forces the
731 * decompressor to combine reads by copying data.
733 compressed_buff = __archive_read_ahead(a, 1, &bytes_avail);
734 if (bytes_avail <= 0) {
735 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
736 "Truncated ZIP file body");
737 return (ARCHIVE_FATAL);
741 * A bug in zlib.h: stream.next_in should be marked 'const'
742 * but isn't (the library never alters data through the
743 * next_in pointer, only reads it). The result: this ugly
744 * cast to remove 'const'.
746 zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
747 zip->stream.avail_in = bytes_avail;
748 zip->stream.total_in = 0;
749 zip->stream.next_out = zip->uncompressed_buffer;
750 zip->stream.avail_out = zip->uncompressed_buffer_size;
751 zip->stream.total_out = 0;
753 r = inflate(&zip->stream, 0);
758 zip->end_of_entry = 1;
761 archive_set_error(&a->archive, ENOMEM,
762 "Out of memory for ZIP decompression");
763 return (ARCHIVE_FATAL);
765 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
766 "ZIP decompression failed (%d)", r);
767 return (ARCHIVE_FATAL);
770 /* Consume as much as the compressor actually used. */
771 bytes_avail = zip->stream.total_in;
772 __archive_read_consume(a, bytes_avail);
773 zip->entry_bytes_remaining -= bytes_avail;
774 zip->entry_compressed_bytes_read += bytes_avail;
776 *offset = zip->entry_offset;
777 *size = zip->stream.total_out;
778 zip->entry_uncompressed_bytes_read += *size;
779 *buff = zip->uncompressed_buffer;
780 zip->entry_offset += *size;
785 zip_read_data_deflate(struct archive_read *a, const void **buff,
786 size_t *size, off_t *offset)
791 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
792 "libarchive compiled without deflate support (no libz)");
793 return (ARCHIVE_FATAL);
798 archive_read_format_zip_read_data_skip(struct archive_read *a)
801 const void *buff = NULL;
804 zip = (struct zip *)(a->format->data);
806 /* If we've already read to end of data, we're done. */
807 if (zip->end_of_entry)
811 * If the length is at the end, we have no choice but
812 * to decompress all the data to find the end marker.
814 if (zip->flags & ZIP_LENGTH_AT_END) {
819 r = archive_read_format_zip_read_data(a, &buff,
821 } while (r == ARCHIVE_OK);
826 * If the length is at the beginning, we can skip the
827 * compressed data much more quickly.
829 bytes_skipped = __archive_read_skip(a, zip->entry_bytes_remaining);
830 if (bytes_skipped < 0)
831 return (ARCHIVE_FATAL);
833 /* This entry is finished and done. */
834 zip->end_of_entry = 1;
839 archive_read_format_zip_cleanup(struct archive_read *a)
843 zip = (struct zip *)(a->format->data);
845 if (zip->stream_valid)
846 inflateEnd(&zip->stream);
848 free(zip->uncompressed_buffer);
849 archive_string_free(&(zip->pathname));
850 archive_string_free(&(zip->extra));
852 (a->format->data) = NULL;
857 * The extra data is stored as a list of
858 * id1+size1+data1 + id2+size2+data2 ...
859 * triplets. id and size are 2 bytes each.
862 process_extra(const void* extra, struct zip* zip)
865 const char *p = (const char *)extra;
866 while (offset < zip->extra_length - 4)
868 unsigned short headerid = archive_le16dec(p + offset);
869 unsigned short datasize = archive_le16dec(p + offset + 2);
871 if (offset + datasize > zip->extra_length)
874 fprintf(stderr, "Header id 0x%04x, length %d\n",
879 /* Zip64 extended information extra field. */
881 zip->uncompressed_size = archive_le64dec(p + offset);
883 zip->compressed_size = archive_le64dec(p + offset + 8);
887 /* Extended time field "UT". */
888 int flags = p[offset];
891 /* Flag bits indicate which dates are present. */
895 fprintf(stderr, "mtime: %lld -> %d\n",
896 (long long)zip->mtime,
897 archive_le32dec(p + offset));
901 zip->mtime = archive_le32dec(p + offset);
909 zip->atime = archive_le32dec(p + offset);
917 zip->ctime = archive_le32dec(p + offset);
924 /* Info-ZIP Unix Extra Field (type 2) "Ux". */
926 fprintf(stderr, "uid %d gid %d\n",
927 archive_le16dec(p + offset),
928 archive_le16dec(p + offset + 2));
931 zip->uid = archive_le16dec(p + offset);
933 zip->gid = archive_le16dec(p + offset + 2);
936 /* Info-Zip Unix Extra Field (type 3) "ux". */
944 if (offset != zip->extra_length)
947 "Extra data field contents do not match reported size!");