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.11 2007/03/03 07:37:36 kientzle Exp $");
29 #ifdef HAVE_SYS_STAT_H
45 #include "archive_entry.h"
46 #include "archive_private.h"
47 #include "archive_read_private.h"
50 /* entry_bytes_remaining is the number of bytes we expect. */
51 int64_t entry_bytes_remaining;
54 /* These count the number of bytes actually read for the entry. */
55 int64_t entry_compressed_bytes_read;
56 int64_t entry_uncompressed_bytes_read;
62 const char * compression_name;
70 /* Flags to mark progress of decompression. */
73 char end_of_entry_cleanup;
76 ssize_t filename_length;
78 int64_t uncompressed_size;
79 int64_t compressed_size;
81 unsigned char *uncompressed_buffer;
82 size_t uncompressed_buffer_size;
87 struct archive_string pathname;
88 struct archive_string extra;
92 #define ZIP_LENGTH_AT_END 8
94 struct zip_file_header {
101 char compressed_size[4];
102 char uncompressed_size[4];
103 char filename_length[2];
104 char extra_length[2];
107 static const char *compression_names[] = {
119 static int archive_read_format_zip_bid(struct archive_read *);
120 static int archive_read_format_zip_cleanup(struct archive_read *);
121 static int archive_read_format_zip_read_data(struct archive_read *,
122 const void **, size_t *, off_t *);
123 static int archive_read_format_zip_read_data_skip(struct archive_read *a);
124 static int archive_read_format_zip_read_header(struct archive_read *,
125 struct archive_entry *);
126 static int i2(const char *);
127 static int i4(const char *);
128 static unsigned int u2(const char *);
129 static unsigned int u4(const char *);
130 static uint64_t u8(const char *);
131 static int zip_read_data_deflate(struct archive_read *a, const void **buff,
132 size_t *size, off_t *offset);
133 static int zip_read_data_none(struct archive_read *a, const void **buff,
134 size_t *size, off_t *offset);
135 static int zip_read_file_header(struct archive_read *a,
136 struct archive_entry *entry, struct zip *zip);
137 static time_t zip_time(const char *);
138 static void process_extra(const void* extra, struct zip* zip);
140 /* Largest 32-bit unsigned value, stored in a 64-bit constant. */
141 static const uint64_t max_uint32 = (((uint64_t)1) << 32) - 1;
144 archive_read_support_format_zip(struct archive *_a)
146 struct archive_read *a = (struct archive_read *)_a;
150 zip = (struct zip *)malloc(sizeof(*zip));
152 archive_set_error(&a->archive, ENOMEM, "Can't allocate zip data");
153 return (ARCHIVE_FATAL);
155 memset(zip, 0, sizeof(*zip));
157 r = __archive_read_register_format(a,
159 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)
179 if (a->archive.archive_format == ARCHIVE_FORMAT_ZIP)
182 bytes_read = (a->compression_read_ahead)(a, &h, 4);
187 if (p[0] == 'P' && p[1] == 'K') {
189 if (p[2] == '\001' && p[3] == '\002')
191 else if (p[2] == '\003' && p[3] == '\004')
193 else if (p[2] == '\005' && p[3] == '\006')
195 else if (p[2] == '\007' && p[3] == '\010')
202 archive_read_format_zip_read_header(struct archive_read *a,
203 struct archive_entry *entry)
207 const char *signature;
210 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
211 if (a->archive.archive_format_name == NULL)
212 a->archive.archive_format_name = "ZIP";
214 zip = (struct zip *)*(a->pformat_data);
215 zip->decompress_init = 0;
216 zip->end_of_entry = 0;
217 zip->end_of_entry_cleanup = 0;
218 zip->entry_uncompressed_bytes_read = 0;
219 zip->entry_compressed_bytes_read = 0;
220 bytes_read = (a->compression_read_ahead)(a, &h, 4);
222 return (ARCHIVE_FATAL);
224 signature = (const char *)h;
225 if (signature[0] != 'P' || signature[1] != 'K') {
226 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
228 return (ARCHIVE_FATAL);
231 if (signature[2] == '\001' && signature[3] == '\002') {
232 /* Beginning of central directory. */
233 return (ARCHIVE_EOF);
236 if (signature[2] == '\003' && signature[3] == '\004') {
237 /* Regular file entry. */
238 return (zip_read_file_header(a, entry, zip));
241 if (signature[2] == '\005' && signature[3] == '\006') {
242 /* End-of-archive record. */
243 return (ARCHIVE_EOF);
246 if (signature[2] == '\007' && signature[3] == '\010') {
248 * We should never encounter this record here;
249 * see ZIP_LENGTH_AT_END handling below for details.
251 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
252 "Bad ZIP file: Unexpected end-of-entry record");
253 return (ARCHIVE_FATAL);
256 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
257 "Damaged ZIP file or unsupported format variant (%d,%d)",
258 signature[2], signature[3]);
259 return (ARCHIVE_FATAL);
263 zip_read_file_header(struct archive_read *a, struct archive_entry *entry,
266 const struct zip_file_header *p;
272 (a->compression_read_ahead)(a, &h, sizeof(struct zip_file_header));
273 if (bytes_read < (int)sizeof(struct zip_file_header)) {
274 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
275 "Truncated ZIP file header");
276 return (ARCHIVE_FATAL);
278 p = (const struct zip_file_header *)h;
280 zip->version = p->version[0];
281 zip->system = p->version[1];
282 zip->flags = i2(p->flags);
283 zip->compression = i2(p->compression);
284 if (zip->compression <
285 sizeof(compression_names)/sizeof(compression_names[0]))
286 zip->compression_name = compression_names[zip->compression];
288 zip->compression_name = "??";
289 zip->mtime = zip_time(p->timedate);
295 zip->crc32 = i4(p->crc32);
296 zip->filename_length = i2(p->filename_length);
297 zip->extra_length = i2(p->extra_length);
298 zip->uncompressed_size = u4(p->uncompressed_size);
299 zip->compressed_size = u4(p->compressed_size);
301 (a->compression_read_consume)(a, sizeof(struct zip_file_header));
304 /* Read the filename. */
305 bytes_read = (a->compression_read_ahead)(a, &h, zip->filename_length);
306 if (bytes_read < zip->filename_length) {
307 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
308 "Truncated ZIP file header");
309 return (ARCHIVE_FATAL);
311 archive_string_ensure(&zip->pathname, zip->filename_length);
312 archive_strncpy(&zip->pathname, (const char *)h, zip->filename_length);
313 (a->compression_read_consume)(a, zip->filename_length);
314 archive_entry_set_pathname(entry, zip->pathname.s);
316 if (zip->pathname.s[archive_strlen(&zip->pathname) - 1] == '/')
317 zip->mode = S_IFDIR | 0777;
319 zip->mode = S_IFREG | 0777;
321 /* Read the extra data. */
322 bytes_read = (a->compression_read_ahead)(a, &h, zip->extra_length);
323 if (bytes_read < zip->extra_length) {
324 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
325 "Truncated ZIP file header");
326 return (ARCHIVE_FATAL);
328 process_extra(h, zip);
329 (a->compression_read_consume)(a, zip->extra_length);
331 /* Populate some additional entry fields: */
332 memset(&st, 0, sizeof(st));
333 st.st_mode = zip->mode;
334 st.st_uid = zip->uid;
335 st.st_gid = zip->gid;
336 st.st_mtime = zip->mtime;
337 st.st_ctime = zip->ctime;
338 st.st_atime = zip->atime;
339 st.st_size = zip->uncompressed_size;
340 archive_entry_copy_stat(entry, &st);
342 zip->entry_bytes_remaining = zip->compressed_size;
343 zip->entry_offset = 0;
345 /* Set up a more descriptive format name. */
346 sprintf(zip->format_name, "ZIP %d.%d (%s)",
347 zip->version / 10, zip->version % 10,
348 zip->compression_name);
349 a->archive.archive_format_name = zip->format_name;
354 /* Convert an MSDOS-style date/time into Unix-style time. */
356 zip_time(const char *p)
361 msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
362 msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
364 memset(&ts, 0, sizeof(ts));
365 ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
366 ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
367 ts.tm_mday = msDate & 0x1f; /* Day of month. */
368 ts.tm_hour = (msTime >> 11) & 0x1f;
369 ts.tm_min = (msTime >> 5) & 0x3f;
370 ts.tm_sec = (msTime << 1) & 0x3e;
376 archive_read_format_zip_read_data(struct archive_read *a,
377 const void **buff, size_t *size, off_t *offset)
382 zip = (struct zip *)*(a->pformat_data);
385 * If we hit end-of-entry last time, clean up and return
386 * ARCHIVE_EOF this time.
388 if (zip->end_of_entry) {
389 if (!zip->end_of_entry_cleanup) {
390 if (zip->flags & ZIP_LENGTH_AT_END) {
394 (a->compression_read_ahead)(a, &h, 16);
395 if (bytes_read < 16) {
396 archive_set_error(&a->archive,
397 ARCHIVE_ERRNO_FILE_FORMAT,
398 "Truncated ZIP end-of-file record");
399 return (ARCHIVE_FATAL);
402 zip->crc32 = i4(p + 4);
403 zip->compressed_size = u4(p + 8);
404 zip->uncompressed_size = u4(p + 12);
405 bytes_read = (a->compression_read_consume)(a, 16);
408 /* Check file size, CRC against these values. */
409 if (zip->compressed_size != zip->entry_compressed_bytes_read) {
410 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
411 "ZIP compressed data is wrong size");
412 return (ARCHIVE_WARN);
414 /* Size field only stores the lower 32 bits of the actual size. */
415 if ((zip->uncompressed_size & max_uint32)
416 != (zip->entry_uncompressed_bytes_read & max_uint32)) {
417 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
418 "ZIP uncompressed data is wrong size");
419 return (ARCHIVE_WARN);
421 /* TODO: Compute CRC. */
423 if (zip->crc32 != zip->entry_crc32_calculated) {
424 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
425 "ZIP data CRC error");
426 return (ARCHIVE_WARN);
429 /* End-of-entry cleanup done. */
430 zip->end_of_entry_cleanup = 1;
432 return (ARCHIVE_EOF);
435 switch(zip->compression) {
436 case 0: /* No compression. */
437 r = zip_read_data_none(a, buff, size, offset);
439 case 8: /* Deflate compression. */
440 r = zip_read_data_deflate(a, buff, size, offset);
442 default: /* Unsupported compression. */
446 /* Return a warning. */
447 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
448 "Unsupported ZIP compression method (%s)",
449 zip->compression_name);
450 if (zip->flags & ZIP_LENGTH_AT_END) {
452 * ZIP_LENGTH_AT_END requires us to
453 * decompress the entry in order to
454 * skip it, but we don't know this
455 * compression method, so we give up.
459 /* We know compressed size; just skip it. */
460 archive_read_format_zip_read_data_skip(a);
469 * Read "uncompressed" data. According to the current specification,
470 * if ZIP_LENGTH_AT_END is specified, then the size fields in the
471 * initial file header are supposed to be set to zero. This would, of
472 * course, make it impossible for us to read the archive, since we
473 * couldn't determine the end of the file data. Info-ZIP seems to
474 * include the real size fields both before and after the data in this
475 * case (the CRC only appears afterwards), so this works as you would
478 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
479 * zip->end_of_entry if it consumes all of the data.
482 zip_read_data_none(struct archive_read *a, const void **buff,
483 size_t *size, off_t *offset)
488 zip = (struct zip *)*(a->pformat_data);
490 if (zip->entry_bytes_remaining == 0) {
493 *offset = zip->entry_offset;
494 zip->end_of_entry = 1;
498 * Note: '1' here is a performance optimization.
499 * Recall that the decompression layer returns a count of
500 * available bytes; asking for more than that forces the
501 * decompressor to combine reads by copying data.
503 bytes_avail = (a->compression_read_ahead)(a, buff, 1);
504 if (bytes_avail <= 0) {
505 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
506 "Truncated ZIP file data");
507 return (ARCHIVE_FATAL);
509 if (bytes_avail > zip->entry_bytes_remaining)
510 bytes_avail = zip->entry_bytes_remaining;
511 (a->compression_read_consume)(a, bytes_avail);
513 *offset = zip->entry_offset;
514 zip->entry_offset += *size;
515 zip->entry_bytes_remaining -= *size;
516 zip->entry_uncompressed_bytes_read += *size;
517 zip->entry_compressed_bytes_read += *size;
523 zip_read_data_deflate(struct archive_read *a, const void **buff,
524 size_t *size, off_t *offset)
528 const void *compressed_buff;
531 zip = (struct zip *)*(a->pformat_data);
533 /* If the buffer hasn't been allocated, allocate it now. */
534 if (zip->uncompressed_buffer == NULL) {
535 zip->uncompressed_buffer_size = 32 * 1024;
536 zip->uncompressed_buffer
537 = (unsigned char *)malloc(zip->uncompressed_buffer_size);
538 if (zip->uncompressed_buffer == NULL) {
539 archive_set_error(&a->archive, ENOMEM,
540 "No memory for ZIP decompression");
541 return (ARCHIVE_FATAL);
545 /* If we haven't yet read any data, initialize the decompressor. */
546 if (!zip->decompress_init) {
547 r = inflateInit2(&zip->stream,
548 -15 /* Don't check for zlib header */);
550 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
551 "Can't initialize ZIP decompression.");
552 return (ARCHIVE_FATAL);
554 zip->decompress_init = 1;
558 * Note: '1' here is a performance optimization.
559 * Recall that the decompression layer returns a count of
560 * available bytes; asking for more than that forces the
561 * decompressor to combine reads by copying data.
563 bytes_avail = (a->compression_read_ahead)(a, &compressed_buff, 1);
564 if (bytes_avail <= 0) {
565 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
566 "Truncated ZIP file body");
567 return (ARCHIVE_FATAL);
571 * A bug in zlib.h: stream.next_in should be marked 'const'
572 * but isn't (the library never alters data through the
573 * next_in pointer, only reads it). The result: this ugly
574 * cast to remove 'const'.
576 zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
577 zip->stream.avail_in = bytes_avail;
578 zip->stream.total_in = 0;
579 zip->stream.next_out = zip->uncompressed_buffer;
580 zip->stream.avail_out = zip->uncompressed_buffer_size;
581 zip->stream.total_out = 0;
583 r = inflate(&zip->stream, 0);
588 zip->end_of_entry = 1;
591 archive_set_error(&a->archive, ENOMEM,
592 "Out of memory for ZIP decompression");
593 return (ARCHIVE_FATAL);
595 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
596 "ZIP decompression failed (%d)", r);
597 return (ARCHIVE_FATAL);
600 /* Consume as much as the compressor actually used. */
601 bytes_avail = zip->stream.total_in;
602 (a->compression_read_consume)(a, bytes_avail);
603 zip->entry_bytes_remaining -= bytes_avail;
604 zip->entry_compressed_bytes_read += bytes_avail;
606 *offset = zip->entry_offset;
607 *size = zip->stream.total_out;
608 zip->entry_uncompressed_bytes_read += *size;
609 *buff = zip->uncompressed_buffer;
610 zip->entry_offset += *size;
615 zip_read_data_deflate(struct archive_read *a, const void **buff,
616 size_t *size, off_t *offset)
621 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
622 "libarchive compiled without deflate support (no libz)");
623 return (ARCHIVE_FATAL);
628 archive_read_format_zip_read_data_skip(struct archive_read *a)
631 const void *buff = NULL;
634 zip = (struct zip *)*(a->pformat_data);
637 * If the length is at the end, we have no choice but
638 * to decompress all the data to find the end marker.
640 if (zip->flags & ZIP_LENGTH_AT_END) {
645 r = archive_read_format_zip_read_data(a, &buff,
647 } while (r == ARCHIVE_OK);
652 * If the length is at the beginning, we can skip the
653 * compressed data much more quickly.
655 while (zip->entry_bytes_remaining > 0) {
656 bytes_avail = (a->compression_read_ahead)(a, &buff, 1);
657 if (bytes_avail <= 0) {
658 archive_set_error(&a->archive,
659 ARCHIVE_ERRNO_FILE_FORMAT,
660 "Truncated ZIP file body");
661 return (ARCHIVE_FATAL);
663 if (bytes_avail > zip->entry_bytes_remaining)
664 bytes_avail = zip->entry_bytes_remaining;
665 (a->compression_read_consume)(a, bytes_avail);
666 zip->entry_bytes_remaining -= bytes_avail;
668 /* This entry is finished and done. */
669 zip->end_of_entry_cleanup = zip->end_of_entry = 1;
674 archive_read_format_zip_cleanup(struct archive_read *a)
678 zip = (struct zip *)*(a->pformat_data);
679 if (zip->uncompressed_buffer != NULL)
680 free(zip->uncompressed_buffer);
681 archive_string_free(&(zip->pathname));
682 archive_string_free(&(zip->extra));
684 *(a->pformat_data) = NULL;
691 return ((0xff & (int)p[0]) + 256 * (0xff & (int)p[1]));
698 return ((0xffff & i2(p)) + 0x10000 * (0xffff & i2(p+2)));
704 return ((0xff & (unsigned int)p[0]) + 256 * (0xff & (unsigned int)p[1]));
710 return u2(p) + 0x10000 * u2(p+2);
716 return u4(p) + 0x100000000LL * u4(p+4);
720 * The extra data is stored as a list of
721 * id1+size1+data1 + id2+size2+data2 ...
722 * triplets. id and size are 2 bytes each.
725 process_extra(const void* extra, struct zip* zip)
728 const char *p = (const char *)extra;
729 while (offset < zip->extra_length - 4)
731 unsigned short headerid = u2(p + offset);
732 unsigned short datasize = u2(p + offset + 2);
734 if (offset + datasize > zip->extra_length)
737 fprintf(stderr, "Header id 0x%04x, length %d\n",
742 /* Zip64 extended information extra field. */
744 zip->uncompressed_size = u8(p + offset);
746 zip->compressed_size = u8(p + offset + 8);
750 /* Extended time field "UT". */
751 int flags = p[offset];
754 /* Flag bits indicate which dates are present. */
758 fprintf(stderr, "mtime: %d -> %d\n",
759 zip->mtime, i4(p + offset));
763 zip->mtime = i4(p + offset);
771 zip->atime = i4(p + offset);
779 zip->ctime = i4(p + offset);
786 /* Info-ZIP Unix Extra Field (type 2) "Ux". */
788 fprintf(stderr, "uid %d gid %d\n",
789 i2(p + offset), i2(p + offset + 2));
792 zip->uid = i2(p + offset);
794 zip->gid = i2(p + offset + 2);
802 if (offset != zip->extra_length)
805 "Extra data field contents do not match reported size!");