Import libarchive-2.5.4b.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_read_support_format_zip.c
bloba105297ad4f9766183947c1393ee5320faa02228
1 /*-
2 * Copyright (c) 2004 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.22 2008/02/27 06:05:59 kientzle Exp $");
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #include <stdio.h>
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #include <time.h>
37 #ifdef HAVE_ZLIB_H
38 #include <zlib.h>
39 #endif
41 #include "archive.h"
42 #include "archive_entry.h"
43 #include "archive_private.h"
44 #include "archive_read_private.h"
45 #include "archive_endian.h"
47 struct zip {
48 /* entry_bytes_remaining is the number of bytes we expect. */
49 int64_t entry_bytes_remaining;
50 int64_t entry_offset;
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;
56 unsigned version;
57 unsigned system;
58 unsigned flags;
59 unsigned compression;
60 const char * compression_name;
61 time_t mtime;
62 time_t ctime;
63 time_t atime;
64 mode_t mode;
65 uid_t uid;
66 gid_t gid;
68 /* Flags to mark progress of decompression. */
69 char decompress_init;
70 char end_of_entry;
71 char end_of_entry_cleanup;
73 long crc32;
74 ssize_t filename_length;
75 ssize_t extra_length;
76 int64_t uncompressed_size;
77 int64_t compressed_size;
79 unsigned char *uncompressed_buffer;
80 size_t uncompressed_buffer_size;
81 #ifdef HAVE_ZLIB_H
82 z_stream stream;
83 char stream_valid;
84 #endif
86 struct archive_string pathname;
87 struct archive_string extra;
88 char format_name[64];
91 #define ZIP_LENGTH_AT_END 8
93 struct zip_file_header {
94 char signature[4];
95 char version[2];
96 char flags[2];
97 char compression[2];
98 char timedate[4];
99 char crc32[4];
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[] = {
107 "uncompressed",
108 "shrinking",
109 "reduced-1",
110 "reduced-2",
111 "reduced-3",
112 "reduced-4",
113 "imploded",
114 "reserved",
115 "deflation"
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;
138 struct zip *zip;
139 int r;
141 zip = (struct zip *)malloc(sizeof(*zip));
142 if (zip == NULL) {
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,
149 zip,
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);
156 if (r != ARCHIVE_OK)
157 free(zip);
158 return (ARCHIVE_OK);
162 static int
163 archive_read_format_zip_bid(struct archive_read *a)
165 const char *p;
166 const void *buff;
167 size_t bytes_avail;
169 if ((p = __archive_read_ahead(a, 4)) == NULL)
170 return (-1);
173 * Bid of 30 here is: 16 bits for "PK",
174 * next 16-bit field has four options (-2 bits).
175 * 16 + 16-2 = 30.
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'))
183 return (30);
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 */
222 return (30);
224 ++p;
228 return (0);
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.
236 static int
237 skip_sfx(struct archive_read *a)
239 const void *h;
240 const char *p, *q;
241 size_t skip, bytes;
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.
250 for (;;) {
251 bytes = (a->decompressor->read_ahead)(a, &h, 4096);
252 if (bytes < 4)
253 return (ARCHIVE_FATAL);
254 p = h;
255 q = p + bytes;
258 * Scan ahead until we find something that looks
259 * like the zip header.
261 while (p + 4 < q) {
262 switch (p[3]) {
263 case '\004':
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);
268 return (ARCHIVE_OK);
270 p += 4;
271 break;
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);
283 static int
284 archive_read_format_zip_read_header(struct archive_read *a,
285 struct archive_entry *entry)
287 const void *h;
288 const char *signature;
289 struct zip *zip;
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... */
308 r = skip_sfx(a);
309 if (r < ARCHIVE_WARN)
310 return (r);
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,
318 "Bad ZIP file");
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,
334 "Bad ZIP file");
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)
348 return (r1);
349 return (r);
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,
375 struct zip *zip)
377 const struct zip_file_header *p;
378 const void *h;
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];
393 else
394 zip->compression_name = "??";
395 zip->mtime = zip_time(p->timedate);
396 zip->ctime = 0;
397 zip->atime = 0;
398 zip->mode = 0;
399 zip->uid = 0;
400 zip->gid = 0;
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;
424 else
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;
459 return (ARCHIVE_OK);
462 /* Convert an MSDOS-style date/time into Unix-style time. */
463 static time_t
464 zip_time(const char *p)
466 int msTime, msDate;
467 struct tm ts;
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;
479 ts.tm_isdst = -1;
480 return mktime(&ts);
483 static int
484 archive_read_format_zip_read_data(struct archive_read *a,
485 const void **buff, size_t *size, off_t *offset)
487 int r;
488 struct zip *zip;
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) {
499 const char *p;
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;
538 *size = 0;
539 *buff = NULL;
540 return (ARCHIVE_EOF);
543 switch(zip->compression) {
544 case 0: /* No compression. */
545 r = zip_read_data_none(a, buff, size, offset);
546 break;
547 case 8: /* Deflate compression. */
548 r = zip_read_data_deflate(a, buff, size, offset);
549 break;
550 default: /* Unsupported compression. */
551 *buff = NULL;
552 *size = 0;
553 *offset = 0;
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.
565 r = ARCHIVE_FATAL;
566 } else {
567 /* We know compressed size; just skip it. */
568 archive_read_format_zip_read_data_skip(a);
569 r = ARCHIVE_WARN;
571 break;
573 return (r);
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
584 * expect.
586 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
587 * zip->end_of_entry if it consumes all of the data.
589 static int
590 zip_read_data_none(struct archive_read *a, const void **buff,
591 size_t *size, off_t *offset)
593 struct zip *zip;
594 ssize_t bytes_avail;
596 zip = (struct zip *)(a->format->data);
598 if (zip->entry_bytes_remaining == 0) {
599 *buff = NULL;
600 *size = 0;
601 *offset = zip->entry_offset;
602 zip->end_of_entry = 1;
603 return (ARCHIVE_OK);
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);
620 *size = 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;
626 return (ARCHIVE_OK);
629 #ifdef HAVE_ZLIB_H
630 static int
631 zip_read_data_deflate(struct archive_read *a, const void **buff,
632 size_t *size, off_t *offset)
634 struct zip *zip;
635 ssize_t bytes_avail;
636 const void *compressed_buff;
637 int r;
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);
657 else
658 r = inflateInit2(&zip->stream,
659 -15 /* Don't check for zlib header */);
660 if (r != Z_OK) {
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);
698 switch (r) {
699 case Z_OK:
700 break;
701 case Z_STREAM_END:
702 zip->end_of_entry = 1;
703 break;
704 case Z_MEM_ERROR:
705 archive_set_error(&a->archive, ENOMEM,
706 "Out of memory for ZIP decompression");
707 return (ARCHIVE_FATAL);
708 default:
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;
725 return (ARCHIVE_OK);
727 #else
728 static int
729 zip_read_data_deflate(struct archive_read *a, const void **buff,
730 size_t *size, off_t *offset)
732 *buff = NULL;
733 *size = 0;
734 *offset = 0;
735 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
736 "libarchive compiled without deflate support (no libz)");
737 return (ARCHIVE_FATAL);
739 #endif
741 static int
742 archive_read_format_zip_read_data_skip(struct archive_read *a)
744 struct zip *zip;
745 const void *buff = NULL;
746 off_t bytes_skipped;
748 zip = (struct zip *)(a->format->data);
751 * If the length is at the end, we have no choice but
752 * to decompress all the data to find the end marker.
754 if (zip->flags & ZIP_LENGTH_AT_END) {
755 size_t size;
756 off_t offset;
757 int r;
758 do {
759 r = archive_read_format_zip_read_data(a, &buff,
760 &size, &offset);
761 } while (r == ARCHIVE_OK);
762 return (r);
766 * If the length is at the beginning, we can skip the
767 * compressed data much more quickly.
769 bytes_skipped = (a->decompressor->skip)(a, zip->entry_bytes_remaining);
770 if (bytes_skipped < 0)
771 return (ARCHIVE_FATAL);
773 /* This entry is finished and done. */
774 zip->end_of_entry_cleanup = zip->end_of_entry = 1;
775 return (ARCHIVE_OK);
778 static int
779 archive_read_format_zip_cleanup(struct archive_read *a)
781 struct zip *zip;
783 zip = (struct zip *)(a->format->data);
784 #ifdef HAVE_ZLIB_H
785 if (zip->stream_valid)
786 inflateEnd(&zip->stream);
787 #endif
788 free(zip->uncompressed_buffer);
789 archive_string_free(&(zip->pathname));
790 archive_string_free(&(zip->extra));
791 free(zip);
792 (a->format->data) = NULL;
793 return (ARCHIVE_OK);
797 * The extra data is stored as a list of
798 * id1+size1+data1 + id2+size2+data2 ...
799 * triplets. id and size are 2 bytes each.
801 static void
802 process_extra(const void* extra, struct zip* zip)
804 int offset = 0;
805 const char *p = (const char *)extra;
806 while (offset < zip->extra_length - 4)
808 unsigned short headerid = archive_le16dec(p + offset);
809 unsigned short datasize = archive_le16dec(p + offset + 2);
810 offset += 4;
811 if (offset + datasize > zip->extra_length)
812 break;
813 #ifdef DEBUG
814 fprintf(stderr, "Header id 0x%04x, length %d\n",
815 headerid, datasize);
816 #endif
817 switch (headerid) {
818 case 0x0001:
819 /* Zip64 extended information extra field. */
820 if (datasize >= 8)
821 zip->uncompressed_size = archive_le64dec(p + offset);
822 if (datasize >= 16)
823 zip->compressed_size = archive_le64dec(p + offset + 8);
824 break;
825 case 0x5455:
827 /* Extended time field "UT". */
828 int flags = p[offset];
829 offset++;
830 datasize--;
831 /* Flag bits indicate which dates are present. */
832 if (flags & 0x01)
834 #ifdef DEBUG
835 fprintf(stderr, "mtime: %lld -> %d\n",
836 (long long)zip->mtime,
837 archive_le32dec(p + offset));
838 #endif
839 if (datasize < 4)
840 break;
841 zip->mtime = archive_le32dec(p + offset);
842 offset += 4;
843 datasize -= 4;
845 if (flags & 0x02)
847 if (datasize < 4)
848 break;
849 zip->atime = archive_le32dec(p + offset);
850 offset += 4;
851 datasize -= 4;
853 if (flags & 0x04)
855 if (datasize < 4)
856 break;
857 zip->ctime = archive_le32dec(p + offset);
858 offset += 4;
859 datasize -= 4;
861 break;
863 case 0x7855:
864 /* Info-ZIP Unix Extra Field (type 2) "Ux". */
865 #ifdef DEBUG
866 fprintf(stderr, "uid %d gid %d\n",
867 archive_le16dec(p + offset),
868 archive_le16dec(p + offset + 2));
869 #endif
870 if (datasize >= 2)
871 zip->uid = archive_le16dec(p + offset);
872 if (datasize >= 4)
873 zip->gid = archive_le16dec(p + offset + 2);
874 break;
875 default:
876 break;
878 offset += datasize;
880 #ifdef DEBUG
881 if (offset != zip->extra_length)
883 fprintf(stderr,
884 "Extra data field contents do not match reported size!");
886 #endif