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: src/lib/libarchive/archive_read_support_format_zip.c,v 1.24 2008/06/15 05:15:53 kientzle Exp $");
42 #include "archive_entry.h"
43 #include "archive_private.h"
44 #include "archive_read_private.h"
45 #include "archive_endian.h"
48 /* entry_bytes_remaining is the number of bytes we expect. */
49 int64_t entry_bytes_remaining
;
52 /* These count the number of bytes actually read for the entry. */
53 int64_t entry_compressed_bytes_read
;
54 int64_t entry_uncompressed_bytes_read
;
60 const char * compression_name
;
68 /* Flags to mark progress of decompression. */
71 char end_of_entry_cleanup
;
74 ssize_t filename_length
;
76 int64_t uncompressed_size
;
77 int64_t compressed_size
;
79 unsigned char *uncompressed_buffer
;
80 size_t uncompressed_buffer_size
;
86 struct archive_string pathname
;
87 struct archive_string extra
;
91 #define ZIP_LENGTH_AT_END 8
93 struct zip_file_header
{
100 char compressed_size
[4];
101 char uncompressed_size
[4];
102 char filename_length
[2];
103 char extra_length
[2];
106 static const char *compression_names
[] = {
118 static int archive_read_format_zip_bid(struct archive_read
*);
119 static int archive_read_format_zip_cleanup(struct archive_read
*);
120 static int archive_read_format_zip_read_data(struct archive_read
*,
121 const void **, size_t *, off_t
*);
122 static int archive_read_format_zip_read_data_skip(struct archive_read
*a
);
123 static int archive_read_format_zip_read_header(struct archive_read
*,
124 struct archive_entry
*);
125 static int zip_read_data_deflate(struct archive_read
*a
, const void **buff
,
126 size_t *size
, off_t
*offset
);
127 static int zip_read_data_none(struct archive_read
*a
, const void **buff
,
128 size_t *size
, off_t
*offset
);
129 static int zip_read_file_header(struct archive_read
*a
,
130 struct archive_entry
*entry
, struct zip
*zip
);
131 static time_t zip_time(const char *);
132 static void process_extra(const void* extra
, struct zip
* zip
);
135 archive_read_support_format_zip(struct archive
*_a
)
137 struct archive_read
*a
= (struct archive_read
*)_a
;
141 zip
= (struct zip
*)malloc(sizeof(*zip
));
143 archive_set_error(&a
->archive
, ENOMEM
, "Can't allocate zip data");
144 return (ARCHIVE_FATAL
);
146 memset(zip
, 0, sizeof(*zip
));
148 r
= __archive_read_register_format(a
,
150 archive_read_format_zip_bid
,
151 archive_read_format_zip_read_header
,
152 archive_read_format_zip_read_data
,
153 archive_read_format_zip_read_data_skip
,
154 archive_read_format_zip_cleanup
);
163 archive_read_format_zip_bid(struct archive_read
*a
)
169 if ((p
= __archive_read_ahead(a
, 4)) == NULL
)
173 * Bid of 30 here is: 16 bits for "PK",
174 * next 16-bit field has four options (-2 bits).
177 if (p
[0] == 'P' && p
[1] == 'K') {
178 if ((p
[2] == '\001' && p
[3] == '\002')
179 || (p
[2] == '\003' && p
[3] == '\004')
180 || (p
[2] == '\005' && p
[3] == '\006')
181 || (p
[2] == '\007' && p
[3] == '\010')
182 || (p
[2] == '0' && p
[3] == '0'))
187 * Attempt to handle self-extracting archives
188 * by noting a PE header and searching forward
189 * up to 64k for a 'PK\003\004' marker.
191 if (p
[0] == 'M' && p
[1] == 'Z') {
193 * TODO: Additional checks that this really is a PE
194 * file before we invoke the 128k lookahead below.
195 * No point in allocating a bigger lookahead buffer
196 * if we don't need to.
199 * TODO: Of course, the compression layer lookahead
200 * buffers aren't dynamically sized yet; they should be.
202 bytes_avail
= (a
->decompressor
->read_ahead
)(a
, &buff
, 128*1024);
203 p
= (const char *)buff
;
206 * TODO: Optimize by jumping forward based on values
207 * in the PE header. Note that we don't need to be
208 * exact, but we mustn't skip too far. The search
209 * below will compensate if we undershoot. Skipping
210 * will also reduce the chance of false positives
211 * (which is not really all that high to begin with,
212 * so maybe skipping isn't really necessary).
215 while (p
< bytes_avail
+ (const char *)buff
) {
216 if (p
[0] == 'P' && p
[1] == 'K' /* "PK" signature */
217 && p
[2] == 3 && p
[3] == 4 /* File entry */
218 && p
[8] == 8 /* compression == deflate */
219 && p
[9] == 0 /* High byte of compression */
232 * Search forward for a "PK\003\004" file header. This handles the
233 * case of self-extracting archives, where there is an executable
234 * prepended to the ZIP archive.
237 skip_sfx(struct archive_read
*a
)
244 * TODO: We should be able to skip forward by a bunch
245 * by lifting some values from the PE header. We don't
246 * need to be exact (we're still going to search forward
247 * to find the header), but it will speed things up and
248 * reduce the chance of a false positive.
251 bytes
= (a
->decompressor
->read_ahead
)(a
, &h
, 4096);
253 return (ARCHIVE_FATAL
);
258 * Scan ahead until we find something that looks
259 * like the zip header.
264 /* TODO: Additional verification here. */
265 if (memcmp("PK\003\004", p
, 4) == 0) {
266 skip
= p
- (const char *)h
;
267 (a
->decompressor
->consume
)(a
, skip
);
272 case '\003': p
+= 1; break;
273 case 'K': p
+= 2; break;
274 case 'P': p
+= 3; break;
275 default: p
+= 4; break;
278 skip
= p
- (const char *)h
;
279 (a
->decompressor
->consume
)(a
, skip
);
284 archive_read_format_zip_read_header(struct archive_read
*a
,
285 struct archive_entry
*entry
)
288 const char *signature
;
290 int r
= ARCHIVE_OK
, r1
;
292 a
->archive
.archive_format
= ARCHIVE_FORMAT_ZIP
;
293 if (a
->archive
.archive_format_name
== NULL
)
294 a
->archive
.archive_format_name
= "ZIP";
296 zip
= (struct zip
*)(a
->format
->data
);
297 zip
->decompress_init
= 0;
298 zip
->end_of_entry
= 0;
299 zip
->end_of_entry_cleanup
= 0;
300 zip
->entry_uncompressed_bytes_read
= 0;
301 zip
->entry_compressed_bytes_read
= 0;
302 if ((h
= __archive_read_ahead(a
, 4)) == NULL
)
303 return (ARCHIVE_FATAL
);
305 signature
= (const char *)h
;
306 if (signature
[0] == 'M' && signature
[1] == 'Z') {
307 /* This is an executable? Must be self-extracting... */
309 if (r
< ARCHIVE_WARN
)
311 if ((h
= __archive_read_ahead(a
, 4)) == NULL
)
312 return (ARCHIVE_FATAL
);
313 signature
= (const char *)h
;
316 if (signature
[0] != 'P' || signature
[1] != 'K') {
317 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
319 return (ARCHIVE_FATAL
);
323 * "PK00" signature is used for "split" archives that
324 * only have a single segment. This means we can just
325 * skip the PK00; the first real file header should follow.
327 if (signature
[2] == '0' && signature
[3] == '0') {
328 (a
->decompressor
->consume
)(a
, 4);
329 if ((h
= __archive_read_ahead(a
, 4)) == NULL
)
330 return (ARCHIVE_FATAL
);
331 signature
= (const char *)h
;
332 if (signature
[0] != 'P' || signature
[1] != 'K') {
333 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
335 return (ARCHIVE_FATAL
);
339 if (signature
[2] == '\001' && signature
[3] == '\002') {
340 /* Beginning of central directory. */
341 return (ARCHIVE_EOF
);
344 if (signature
[2] == '\003' && signature
[3] == '\004') {
345 /* Regular file entry. */
346 r1
= zip_read_file_header(a
, entry
, zip
);
347 if (r1
!= ARCHIVE_OK
)
352 if (signature
[2] == '\005' && signature
[3] == '\006') {
353 /* End-of-archive record. */
354 return (ARCHIVE_EOF
);
357 if (signature
[2] == '\007' && signature
[3] == '\010') {
359 * We should never encounter this record here;
360 * see ZIP_LENGTH_AT_END handling below for details.
362 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
363 "Bad ZIP file: Unexpected end-of-entry record");
364 return (ARCHIVE_FATAL
);
367 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
368 "Damaged ZIP file or unsupported format variant (%d,%d)",
369 signature
[2], signature
[3]);
370 return (ARCHIVE_FATAL
);
374 zip_read_file_header(struct archive_read
*a
, struct archive_entry
*entry
,
377 const struct zip_file_header
*p
;
380 if ((p
= __archive_read_ahead(a
, sizeof *p
)) == NULL
) {
381 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
382 "Truncated ZIP file header");
383 return (ARCHIVE_FATAL
);
386 zip
->version
= p
->version
[0];
387 zip
->system
= p
->version
[1];
388 zip
->flags
= archive_le16dec(p
->flags
);
389 zip
->compression
= archive_le16dec(p
->compression
);
390 if (zip
->compression
<
391 sizeof(compression_names
)/sizeof(compression_names
[0]))
392 zip
->compression_name
= compression_names
[zip
->compression
];
394 zip
->compression_name
= "??";
395 zip
->mtime
= zip_time(p
->timedate
);
401 zip
->crc32
= archive_le32dec(p
->crc32
);
402 zip
->filename_length
= archive_le16dec(p
->filename_length
);
403 zip
->extra_length
= archive_le16dec(p
->extra_length
);
404 zip
->uncompressed_size
= archive_le32dec(p
->uncompressed_size
);
405 zip
->compressed_size
= archive_le32dec(p
->compressed_size
);
407 (a
->decompressor
->consume
)(a
, sizeof(struct zip_file_header
));
410 /* Read the filename. */
411 if ((h
= __archive_read_ahead(a
, zip
->filename_length
)) == NULL
) {
412 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
413 "Truncated ZIP file header");
414 return (ARCHIVE_FATAL
);
416 if (archive_string_ensure(&zip
->pathname
, zip
->filename_length
) == NULL
)
417 __archive_errx(1, "Out of memory");
418 archive_strncpy(&zip
->pathname
, h
, zip
->filename_length
);
419 (a
->decompressor
->consume
)(a
, zip
->filename_length
);
420 archive_entry_set_pathname(entry
, zip
->pathname
.s
);
422 if (zip
->pathname
.s
[archive_strlen(&zip
->pathname
) - 1] == '/')
423 zip
->mode
= AE_IFDIR
| 0777;
425 zip
->mode
= AE_IFREG
| 0777;
427 /* Read the extra data. */
428 if ((h
= __archive_read_ahead(a
, zip
->extra_length
)) == NULL
) {
429 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
430 "Truncated ZIP file header");
431 return (ARCHIVE_FATAL
);
433 process_extra(h
, zip
);
434 (a
->decompressor
->consume
)(a
, zip
->extra_length
);
436 /* Populate some additional entry fields: */
437 archive_entry_set_mode(entry
, zip
->mode
);
438 archive_entry_set_uid(entry
, zip
->uid
);
439 archive_entry_set_gid(entry
, zip
->gid
);
440 archive_entry_set_mtime(entry
, zip
->mtime
, 0);
441 archive_entry_set_ctime(entry
, zip
->ctime
, 0);
442 archive_entry_set_atime(entry
, zip
->atime
, 0);
443 archive_entry_set_size(entry
, zip
->uncompressed_size
);
445 zip
->entry_bytes_remaining
= zip
->compressed_size
;
446 zip
->entry_offset
= 0;
448 /* If there's no body, force read_data() to return EOF immediately. */
449 if (0 == (zip
->flags
& ZIP_LENGTH_AT_END
)
450 && zip
->entry_bytes_remaining
< 1)
451 zip
->end_of_entry
= 1;
453 /* Set up a more descriptive format name. */
454 sprintf(zip
->format_name
, "ZIP %d.%d (%s)",
455 zip
->version
/ 10, zip
->version
% 10,
456 zip
->compression_name
);
457 a
->archive
.archive_format_name
= zip
->format_name
;
462 /* Convert an MSDOS-style date/time into Unix-style time. */
464 zip_time(const char *p
)
469 msTime
= (0xff & (unsigned)p
[0]) + 256 * (0xff & (unsigned)p
[1]);
470 msDate
= (0xff & (unsigned)p
[2]) + 256 * (0xff & (unsigned)p
[3]);
472 memset(&ts
, 0, sizeof(ts
));
473 ts
.tm_year
= ((msDate
>> 9) & 0x7f) + 80; /* Years since 1900. */
474 ts
.tm_mon
= ((msDate
>> 5) & 0x0f) - 1; /* Month number. */
475 ts
.tm_mday
= msDate
& 0x1f; /* Day of month. */
476 ts
.tm_hour
= (msTime
>> 11) & 0x1f;
477 ts
.tm_min
= (msTime
>> 5) & 0x3f;
478 ts
.tm_sec
= (msTime
<< 1) & 0x3e;
484 archive_read_format_zip_read_data(struct archive_read
*a
,
485 const void **buff
, size_t *size
, off_t
*offset
)
490 zip
= (struct zip
*)(a
->format
->data
);
493 * If we hit end-of-entry last time, clean up and return
494 * ARCHIVE_EOF this time.
496 if (zip
->end_of_entry
) {
497 if (!zip
->end_of_entry_cleanup
) {
498 if (zip
->flags
& ZIP_LENGTH_AT_END
) {
501 if ((p
= __archive_read_ahead(a
, 16)) == NULL
) {
502 archive_set_error(&a
->archive
,
503 ARCHIVE_ERRNO_FILE_FORMAT
,
504 "Truncated ZIP end-of-file record");
505 return (ARCHIVE_FATAL
);
507 zip
->crc32
= archive_le32dec(p
+ 4);
508 zip
->compressed_size
= archive_le32dec(p
+ 8);
509 zip
->uncompressed_size
= archive_le32dec(p
+ 12);
510 (a
->decompressor
->consume
)(a
, 16);
513 /* Check file size, CRC against these values. */
514 if (zip
->compressed_size
!= zip
->entry_compressed_bytes_read
) {
515 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
516 "ZIP compressed data is wrong size");
517 return (ARCHIVE_WARN
);
519 /* Size field only stores the lower 32 bits of the actual size. */
520 if ((zip
->uncompressed_size
& UINT32_MAX
)
521 != (zip
->entry_uncompressed_bytes_read
& UINT32_MAX
)) {
522 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
523 "ZIP uncompressed data is wrong size");
524 return (ARCHIVE_WARN
);
526 /* TODO: Compute CRC. */
528 if (zip->crc32 != zip->entry_crc32_calculated) {
529 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
530 "ZIP data CRC error");
531 return (ARCHIVE_WARN);
534 /* End-of-entry cleanup done. */
535 zip
->end_of_entry_cleanup
= 1;
537 *offset
= zip
->entry_uncompressed_bytes_read
;
540 return (ARCHIVE_EOF
);
543 switch(zip
->compression
) {
544 case 0: /* No compression. */
545 r
= zip_read_data_none(a
, buff
, size
, offset
);
547 case 8: /* Deflate compression. */
548 r
= zip_read_data_deflate(a
, buff
, size
, offset
);
550 default: /* Unsupported compression. */
554 /* Return a warning. */
555 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
556 "Unsupported ZIP compression method (%s)",
557 zip
->compression_name
);
558 if (zip
->flags
& ZIP_LENGTH_AT_END
) {
560 * ZIP_LENGTH_AT_END requires us to
561 * decompress the entry in order to
562 * skip it, but we don't know this
563 * compression method, so we give up.
567 /* We can't decompress this entry, but we will
568 * be able to skip() it and try the next entry. */
577 * Read "uncompressed" data. According to the current specification,
578 * if ZIP_LENGTH_AT_END is specified, then the size fields in the
579 * initial file header are supposed to be set to zero. This would, of
580 * course, make it impossible for us to read the archive, since we
581 * couldn't determine the end of the file data. Info-ZIP seems to
582 * include the real size fields both before and after the data in this
583 * case (the CRC only appears afterwards), so this works as you would
586 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
587 * zip->end_of_entry if it consumes all of the data.
590 zip_read_data_none(struct archive_read
*a
, const void **buff
,
591 size_t *size
, off_t
*offset
)
596 zip
= (struct zip
*)(a
->format
->data
);
598 if (zip
->entry_bytes_remaining
== 0) {
601 *offset
= zip
->entry_offset
;
602 zip
->end_of_entry
= 1;
606 * Note: '1' here is a performance optimization.
607 * Recall that the decompression layer returns a count of
608 * available bytes; asking for more than that forces the
609 * decompressor to combine reads by copying data.
611 bytes_avail
= (a
->decompressor
->read_ahead
)(a
, buff
, 1);
612 if (bytes_avail
<= 0) {
613 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
614 "Truncated ZIP file data");
615 return (ARCHIVE_FATAL
);
617 if (bytes_avail
> zip
->entry_bytes_remaining
)
618 bytes_avail
= zip
->entry_bytes_remaining
;
619 (a
->decompressor
->consume
)(a
, bytes_avail
);
621 *offset
= zip
->entry_offset
;
622 zip
->entry_offset
+= *size
;
623 zip
->entry_bytes_remaining
-= *size
;
624 zip
->entry_uncompressed_bytes_read
+= *size
;
625 zip
->entry_compressed_bytes_read
+= *size
;
631 zip_read_data_deflate(struct archive_read
*a
, const void **buff
,
632 size_t *size
, off_t
*offset
)
636 const void *compressed_buff
;
639 zip
= (struct zip
*)(a
->format
->data
);
641 /* If the buffer hasn't been allocated, allocate it now. */
642 if (zip
->uncompressed_buffer
== NULL
) {
643 zip
->uncompressed_buffer_size
= 32 * 1024;
644 zip
->uncompressed_buffer
645 = (unsigned char *)malloc(zip
->uncompressed_buffer_size
);
646 if (zip
->uncompressed_buffer
== NULL
) {
647 archive_set_error(&a
->archive
, ENOMEM
,
648 "No memory for ZIP decompression");
649 return (ARCHIVE_FATAL
);
653 /* If we haven't yet read any data, initialize the decompressor. */
654 if (!zip
->decompress_init
) {
655 if (zip
->stream_valid
)
656 r
= inflateReset(&zip
->stream
);
658 r
= inflateInit2(&zip
->stream
,
659 -15 /* Don't check for zlib header */);
661 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
662 "Can't initialize ZIP decompression.");
663 return (ARCHIVE_FATAL
);
665 /* Stream structure has been set up. */
666 zip
->stream_valid
= 1;
667 /* We've initialized decompression for this stream. */
668 zip
->decompress_init
= 1;
672 * Note: '1' here is a performance optimization.
673 * Recall that the decompression layer returns a count of
674 * available bytes; asking for more than that forces the
675 * decompressor to combine reads by copying data.
677 bytes_avail
= (a
->decompressor
->read_ahead
)(a
, &compressed_buff
, 1);
678 if (bytes_avail
<= 0) {
679 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
680 "Truncated ZIP file body");
681 return (ARCHIVE_FATAL
);
685 * A bug in zlib.h: stream.next_in should be marked 'const'
686 * but isn't (the library never alters data through the
687 * next_in pointer, only reads it). The result: this ugly
688 * cast to remove 'const'.
690 zip
->stream
.next_in
= (Bytef
*)(uintptr_t)(const void *)compressed_buff
;
691 zip
->stream
.avail_in
= bytes_avail
;
692 zip
->stream
.total_in
= 0;
693 zip
->stream
.next_out
= zip
->uncompressed_buffer
;
694 zip
->stream
.avail_out
= zip
->uncompressed_buffer_size
;
695 zip
->stream
.total_out
= 0;
697 r
= inflate(&zip
->stream
, 0);
702 zip
->end_of_entry
= 1;
705 archive_set_error(&a
->archive
, ENOMEM
,
706 "Out of memory for ZIP decompression");
707 return (ARCHIVE_FATAL
);
709 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
710 "ZIP decompression failed (%d)", r
);
711 return (ARCHIVE_FATAL
);
714 /* Consume as much as the compressor actually used. */
715 bytes_avail
= zip
->stream
.total_in
;
716 (a
->decompressor
->consume
)(a
, bytes_avail
);
717 zip
->entry_bytes_remaining
-= bytes_avail
;
718 zip
->entry_compressed_bytes_read
+= bytes_avail
;
720 *offset
= zip
->entry_offset
;
721 *size
= zip
->stream
.total_out
;
722 zip
->entry_uncompressed_bytes_read
+= *size
;
723 *buff
= zip
->uncompressed_buffer
;
724 zip
->entry_offset
+= *size
;
729 zip_read_data_deflate(struct archive_read
*a
, const void **buff
,
730 size_t *size
, off_t
*offset
)
735 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
736 "libarchive compiled without deflate support (no libz)");
737 return (ARCHIVE_FATAL
);
742 archive_read_format_zip_read_data_skip(struct archive_read
*a
)
745 const void *buff
= NULL
;
748 zip
= (struct zip
*)(a
->format
->data
);
750 /* If we've already read to end of data, we're done. */
751 if (zip
->end_of_entry_cleanup
)
755 * If the length is at the end, we have no choice but
756 * to decompress all the data to find the end marker.
758 if (zip
->flags
& ZIP_LENGTH_AT_END
) {
763 r
= archive_read_format_zip_read_data(a
, &buff
,
765 } while (r
== ARCHIVE_OK
);
770 * If the length is at the beginning, we can skip the
771 * compressed data much more quickly.
773 bytes_skipped
= (a
->decompressor
->skip
)(a
, zip
->entry_bytes_remaining
);
774 if (bytes_skipped
< 0)
775 return (ARCHIVE_FATAL
);
777 /* This entry is finished and done. */
778 zip
->end_of_entry_cleanup
= zip
->end_of_entry
= 1;
783 archive_read_format_zip_cleanup(struct archive_read
*a
)
787 zip
= (struct zip
*)(a
->format
->data
);
789 if (zip
->stream_valid
)
790 inflateEnd(&zip
->stream
);
792 free(zip
->uncompressed_buffer
);
793 archive_string_free(&(zip
->pathname
));
794 archive_string_free(&(zip
->extra
));
796 (a
->format
->data
) = NULL
;
801 * The extra data is stored as a list of
802 * id1+size1+data1 + id2+size2+data2 ...
803 * triplets. id and size are 2 bytes each.
806 process_extra(const void* extra
, struct zip
* zip
)
809 const char *p
= (const char *)extra
;
810 while (offset
< zip
->extra_length
- 4)
812 unsigned short headerid
= archive_le16dec(p
+ offset
);
813 unsigned short datasize
= archive_le16dec(p
+ offset
+ 2);
815 if (offset
+ datasize
> zip
->extra_length
)
818 fprintf(stderr
, "Header id 0x%04x, length %d\n",
823 /* Zip64 extended information extra field. */
825 zip
->uncompressed_size
= archive_le64dec(p
+ offset
);
827 zip
->compressed_size
= archive_le64dec(p
+ offset
+ 8);
831 /* Extended time field "UT". */
832 int flags
= p
[offset
];
835 /* Flag bits indicate which dates are present. */
839 fprintf(stderr
, "mtime: %lld -> %d\n",
840 (long long)zip
->mtime
,
841 archive_le32dec(p
+ offset
));
845 zip
->mtime
= archive_le32dec(p
+ offset
);
853 zip
->atime
= archive_le32dec(p
+ offset
);
861 zip
->ctime
= archive_le32dec(p
+ offset
);
868 /* Info-ZIP Unix Extra Field (type 2) "Ux". */
870 fprintf(stderr
, "uid %d gid %d\n",
871 archive_le16dec(p
+ offset
),
872 archive_le16dec(p
+ offset
+ 2));
875 zip
->uid
= archive_le16dec(p
+ offset
);
877 zip
->gid
= archive_le16dec(p
+ offset
+ 2);
885 if (offset
!= zip
->extra_length
)
888 "Extra data field contents do not match reported size!");