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 /* 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)
168 if (a->archive.archive_format == ARCHIVE_FORMAT_ZIP)
171 if ((p = __archive_read_ahead(a, 4)) == NULL)
175 * Bid of 30 here is: 16 bits for "PK",
176 * next 16-bit field has four options (-2 bits).
179 if (p[0] == 'P' && p[1] == 'K') {
180 if ((p[2] == '\001' && p[3] == '\002')
181 || (p[2] == '\003' && p[3] == '\004')
182 || (p[2] == '\005' && p[3] == '\006')
183 || (p[2] == '\007' && p[3] == '\010'))
190 archive_read_format_zip_read_header(struct archive_read *a,
191 struct archive_entry *entry)
194 const char *signature;
197 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
198 if (a->archive.archive_format_name == NULL)
199 a->archive.archive_format_name = "ZIP";
201 zip = (struct zip *)(a->format->data);
202 zip->decompress_init = 0;
203 zip->end_of_entry = 0;
204 zip->end_of_entry_cleanup = 0;
205 zip->entry_uncompressed_bytes_read = 0;
206 zip->entry_compressed_bytes_read = 0;
207 if ((h = __archive_read_ahead(a, 4)) == NULL)
208 return (ARCHIVE_FATAL);
210 signature = (const char *)h;
211 if (signature[0] != 'P' || signature[1] != 'K') {
212 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
214 return (ARCHIVE_FATAL);
217 if (signature[2] == '\001' && signature[3] == '\002') {
218 /* Beginning of central directory. */
219 return (ARCHIVE_EOF);
222 if (signature[2] == '\003' && signature[3] == '\004') {
223 /* Regular file entry. */
224 return (zip_read_file_header(a, entry, zip));
227 if (signature[2] == '\005' && signature[3] == '\006') {
228 /* End-of-archive record. */
229 return (ARCHIVE_EOF);
232 if (signature[2] == '\007' && signature[3] == '\010') {
234 * We should never encounter this record here;
235 * see ZIP_LENGTH_AT_END handling below for details.
237 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
238 "Bad ZIP file: Unexpected end-of-entry record");
239 return (ARCHIVE_FATAL);
242 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
243 "Damaged ZIP file or unsupported format variant (%d,%d)",
244 signature[2], signature[3]);
245 return (ARCHIVE_FATAL);
249 zip_read_file_header(struct archive_read *a, struct archive_entry *entry,
252 const struct zip_file_header *p;
255 if ((p = __archive_read_ahead(a, sizeof *p)) == NULL) {
256 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
257 "Truncated ZIP file header");
258 return (ARCHIVE_FATAL);
261 zip->version = p->version[0];
262 zip->system = p->version[1];
263 zip->flags = le16dec(p->flags);
264 zip->compression = le16dec(p->compression);
265 if (zip->compression <
266 sizeof(compression_names)/sizeof(compression_names[0]))
267 zip->compression_name = compression_names[zip->compression];
269 zip->compression_name = "??";
270 zip->mtime = zip_time(p->timedate);
276 zip->crc32 = le32dec(p->crc32);
277 zip->filename_length = le16dec(p->filename_length);
278 zip->extra_length = le16dec(p->extra_length);
279 zip->uncompressed_size = le32dec(p->uncompressed_size);
280 zip->compressed_size = le32dec(p->compressed_size);
282 (a->decompressor->consume)(a, sizeof(struct zip_file_header));
285 /* Read the filename. */
286 if ((h = __archive_read_ahead(a, zip->filename_length)) == NULL) {
287 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
288 "Truncated ZIP file header");
289 return (ARCHIVE_FATAL);
291 if (archive_string_ensure(&zip->pathname, zip->filename_length) == NULL)
292 __archive_errx(1, "Out of memory");
293 archive_strncpy(&zip->pathname, h, zip->filename_length);
294 (a->decompressor->consume)(a, zip->filename_length);
295 archive_entry_set_pathname(entry, zip->pathname.s);
297 if (zip->pathname.s[archive_strlen(&zip->pathname) - 1] == '/')
298 zip->mode = AE_IFDIR | 0777;
300 zip->mode = AE_IFREG | 0777;
302 /* Read the extra data. */
303 if ((h = __archive_read_ahead(a, zip->extra_length)) == NULL) {
304 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
305 "Truncated ZIP file header");
306 return (ARCHIVE_FATAL);
308 process_extra(h, zip);
309 (a->decompressor->consume)(a, zip->extra_length);
311 /* Populate some additional entry fields: */
312 archive_entry_set_mode(entry, zip->mode);
313 archive_entry_set_uid(entry, zip->uid);
314 archive_entry_set_gid(entry, zip->gid);
315 archive_entry_set_mtime(entry, zip->mtime, 0);
316 archive_entry_set_ctime(entry, zip->ctime, 0);
317 archive_entry_set_atime(entry, zip->atime, 0);
318 archive_entry_set_size(entry, zip->uncompressed_size);
320 zip->entry_bytes_remaining = zip->compressed_size;
321 zip->entry_offset = 0;
323 /* If there's no body, force read_data() to return EOF immediately. */
324 if (0 == (zip->flags & ZIP_LENGTH_AT_END)
325 && zip->entry_bytes_remaining < 1)
326 zip->end_of_entry = 1;
328 /* Set up a more descriptive format name. */
329 sprintf(zip->format_name, "ZIP %d.%d (%s)",
330 zip->version / 10, zip->version % 10,
331 zip->compression_name);
332 a->archive.archive_format_name = zip->format_name;
337 /* Convert an MSDOS-style date/time into Unix-style time. */
339 zip_time(const char *p)
344 msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
345 msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
347 memset(&ts, 0, sizeof(ts));
348 ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
349 ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
350 ts.tm_mday = msDate & 0x1f; /* Day of month. */
351 ts.tm_hour = (msTime >> 11) & 0x1f;
352 ts.tm_min = (msTime >> 5) & 0x3f;
353 ts.tm_sec = (msTime << 1) & 0x3e;
359 archive_read_format_zip_read_data(struct archive_read *a,
360 const void **buff, size_t *size, off_t *offset)
365 zip = (struct zip *)(a->format->data);
368 * If we hit end-of-entry last time, clean up and return
369 * ARCHIVE_EOF this time.
371 if (zip->end_of_entry) {
372 if (!zip->end_of_entry_cleanup) {
373 if (zip->flags & ZIP_LENGTH_AT_END) {
376 if ((p = __archive_read_ahead(a, 16)) == NULL) {
377 archive_set_error(&a->archive,
378 ARCHIVE_ERRNO_FILE_FORMAT,
379 "Truncated ZIP end-of-file record");
380 return (ARCHIVE_FATAL);
382 zip->crc32 = le32dec(p + 4);
383 zip->compressed_size = le32dec(p + 8);
384 zip->uncompressed_size = le32dec(p + 12);
385 (a->decompressor->consume)(a, 16);
388 /* Check file size, CRC against these values. */
389 if (zip->compressed_size != zip->entry_compressed_bytes_read) {
390 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
391 "ZIP compressed data is wrong size");
392 return (ARCHIVE_WARN);
394 /* Size field only stores the lower 32 bits of the actual size. */
395 if ((zip->uncompressed_size & UINT32_MAX)
396 != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
397 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
398 "ZIP uncompressed data is wrong size");
399 return (ARCHIVE_WARN);
401 /* TODO: Compute CRC. */
403 if (zip->crc32 != zip->entry_crc32_calculated) {
404 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
405 "ZIP data CRC error");
406 return (ARCHIVE_WARN);
409 /* End-of-entry cleanup done. */
410 zip->end_of_entry_cleanup = 1;
412 *offset = zip->entry_uncompressed_bytes_read;
415 return (ARCHIVE_EOF);
418 switch(zip->compression) {
419 case 0: /* No compression. */
420 r = zip_read_data_none(a, buff, size, offset);
422 case 8: /* Deflate compression. */
423 r = zip_read_data_deflate(a, buff, size, offset);
425 default: /* Unsupported compression. */
429 /* Return a warning. */
430 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
431 "Unsupported ZIP compression method (%s)",
432 zip->compression_name);
433 if (zip->flags & ZIP_LENGTH_AT_END) {
435 * ZIP_LENGTH_AT_END requires us to
436 * decompress the entry in order to
437 * skip it, but we don't know this
438 * compression method, so we give up.
442 /* We know compressed size; just skip it. */
443 archive_read_format_zip_read_data_skip(a);
452 * Read "uncompressed" data. According to the current specification,
453 * if ZIP_LENGTH_AT_END is specified, then the size fields in the
454 * initial file header are supposed to be set to zero. This would, of
455 * course, make it impossible for us to read the archive, since we
456 * couldn't determine the end of the file data. Info-ZIP seems to
457 * include the real size fields both before and after the data in this
458 * case (the CRC only appears afterwards), so this works as you would
461 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
462 * zip->end_of_entry if it consumes all of the data.
465 zip_read_data_none(struct archive_read *a, const void **buff,
466 size_t *size, off_t *offset)
471 zip = (struct zip *)(a->format->data);
473 if (zip->entry_bytes_remaining == 0) {
476 *offset = zip->entry_offset;
477 zip->end_of_entry = 1;
481 * Note: '1' here is a performance optimization.
482 * Recall that the decompression layer returns a count of
483 * available bytes; asking for more than that forces the
484 * decompressor to combine reads by copying data.
486 bytes_avail = (a->decompressor->read_ahead)(a, buff, 1);
487 if (bytes_avail <= 0) {
488 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
489 "Truncated ZIP file data");
490 return (ARCHIVE_FATAL);
492 if (bytes_avail > zip->entry_bytes_remaining)
493 bytes_avail = zip->entry_bytes_remaining;
494 (a->decompressor->consume)(a, bytes_avail);
496 *offset = zip->entry_offset;
497 zip->entry_offset += *size;
498 zip->entry_bytes_remaining -= *size;
499 zip->entry_uncompressed_bytes_read += *size;
500 zip->entry_compressed_bytes_read += *size;
506 zip_read_data_deflate(struct archive_read *a, const void **buff,
507 size_t *size, off_t *offset)
511 const void *compressed_buff;
514 zip = (struct zip *)(a->format->data);
516 /* If the buffer hasn't been allocated, allocate it now. */
517 if (zip->uncompressed_buffer == NULL) {
518 zip->uncompressed_buffer_size = 32 * 1024;
519 zip->uncompressed_buffer
520 = (unsigned char *)malloc(zip->uncompressed_buffer_size);
521 if (zip->uncompressed_buffer == NULL) {
522 archive_set_error(&a->archive, ENOMEM,
523 "No memory for ZIP decompression");
524 return (ARCHIVE_FATAL);
528 /* If we haven't yet read any data, initialize the decompressor. */
529 if (!zip->decompress_init) {
530 if (zip->stream_valid)
531 r = inflateReset(&zip->stream);
533 r = inflateInit2(&zip->stream,
534 -15 /* Don't check for zlib header */);
536 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
537 "Can't initialize ZIP decompression.");
538 return (ARCHIVE_FATAL);
540 /* Stream structure has been set up. */
541 zip->stream_valid = 1;
542 /* We've initialized decompression for this stream. */
543 zip->decompress_init = 1;
547 * Note: '1' here is a performance optimization.
548 * Recall that the decompression layer returns a count of
549 * available bytes; asking for more than that forces the
550 * decompressor to combine reads by copying data.
552 bytes_avail = (a->decompressor->read_ahead)(a, &compressed_buff, 1);
553 if (bytes_avail <= 0) {
554 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
555 "Truncated ZIP file body");
556 return (ARCHIVE_FATAL);
560 * A bug in zlib.h: stream.next_in should be marked 'const'
561 * but isn't (the library never alters data through the
562 * next_in pointer, only reads it). The result: this ugly
563 * cast to remove 'const'.
565 zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
566 zip->stream.avail_in = bytes_avail;
567 zip->stream.total_in = 0;
568 zip->stream.next_out = zip->uncompressed_buffer;
569 zip->stream.avail_out = zip->uncompressed_buffer_size;
570 zip->stream.total_out = 0;
572 r = inflate(&zip->stream, 0);
577 zip->end_of_entry = 1;
580 archive_set_error(&a->archive, ENOMEM,
581 "Out of memory for ZIP decompression");
582 return (ARCHIVE_FATAL);
584 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
585 "ZIP decompression failed (%d)", r);
586 return (ARCHIVE_FATAL);
589 /* Consume as much as the compressor actually used. */
590 bytes_avail = zip->stream.total_in;
591 (a->decompressor->consume)(a, bytes_avail);
592 zip->entry_bytes_remaining -= bytes_avail;
593 zip->entry_compressed_bytes_read += bytes_avail;
595 *offset = zip->entry_offset;
596 *size = zip->stream.total_out;
597 zip->entry_uncompressed_bytes_read += *size;
598 *buff = zip->uncompressed_buffer;
599 zip->entry_offset += *size;
604 zip_read_data_deflate(struct archive_read *a, const void **buff,
605 size_t *size, off_t *offset)
610 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
611 "libarchive compiled without deflate support (no libz)");
612 return (ARCHIVE_FATAL);
617 archive_read_format_zip_read_data_skip(struct archive_read *a)
620 const void *buff = NULL;
623 zip = (struct zip *)(a->format->data);
626 * If the length is at the end, we have no choice but
627 * to decompress all the data to find the end marker.
629 if (zip->flags & ZIP_LENGTH_AT_END) {
634 r = archive_read_format_zip_read_data(a, &buff,
636 } while (r == ARCHIVE_OK);
641 * If the length is at the beginning, we can skip the
642 * compressed data much more quickly.
644 while (zip->entry_bytes_remaining > 0) {
645 bytes_avail = (a->decompressor->read_ahead)(a, &buff, 1);
646 if (bytes_avail <= 0) {
647 archive_set_error(&a->archive,
648 ARCHIVE_ERRNO_FILE_FORMAT,
649 "Truncated ZIP file body");
650 return (ARCHIVE_FATAL);
652 if (bytes_avail > zip->entry_bytes_remaining)
653 bytes_avail = zip->entry_bytes_remaining;
654 (a->decompressor->consume)(a, bytes_avail);
655 zip->entry_bytes_remaining -= bytes_avail;
657 /* This entry is finished and done. */
658 zip->end_of_entry_cleanup = zip->end_of_entry = 1;
663 archive_read_format_zip_cleanup(struct archive_read *a)
667 zip = (struct zip *)(a->format->data);
669 if (zip->stream_valid)
670 inflateEnd(&zip->stream);
672 free(zip->uncompressed_buffer);
673 archive_string_free(&(zip->pathname));
674 archive_string_free(&(zip->extra));
676 (a->format->data) = NULL;
681 * The extra data is stored as a list of
682 * id1+size1+data1 + id2+size2+data2 ...
683 * triplets. id and size are 2 bytes each.
686 process_extra(const void* extra, struct zip* zip)
689 const char *p = (const char *)extra;
690 while (offset < zip->extra_length - 4)
692 unsigned short headerid = le16dec(p + offset);
693 unsigned short datasize = le16dec(p + offset + 2);
695 if (offset + datasize > zip->extra_length)
698 fprintf(stderr, "Header id 0x%04x, length %d\n",
703 /* Zip64 extended information extra field. */
705 zip->uncompressed_size = le64dec(p + offset);
707 zip->compressed_size = le64dec(p + offset + 8);
711 /* Extended time field "UT". */
712 int flags = p[offset];
715 /* Flag bits indicate which dates are present. */
719 fprintf(stderr, "mtime: %lld -> %d\n",
720 (long long)zip->mtime, le32dec(p + offset));
724 zip->mtime = le32dec(p + offset);
732 zip->atime = le32dec(p + offset);
740 zip->ctime = le32dec(p + offset);
747 /* Info-ZIP Unix Extra Field (type 2) "Ux". */
749 fprintf(stderr, "uid %d gid %d\n",
750 le16dec(p + offset), le16dec(p + offset + 2));
753 zip->uid = le16dec(p + offset);
755 zip->gid = le16dec(p + offset + 2);
763 if (offset != zip->extra_length)
766 "Extra data field contents do not match reported size!");