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"
47 /* entry_bytes_remaining is the number of bytes we expect. */
48 int64_t entry_bytes_remaining;
51 /* These count the number of bytes actually read for the entry. */
52 int64_t entry_compressed_bytes_read;
53 int64_t entry_uncompressed_bytes_read;
59 const char * compression_name;
67 /* Flags to mark progress of decompression. */
70 char end_of_entry_cleanup;
73 ssize_t filename_length;
75 int64_t uncompressed_size;
76 int64_t compressed_size;
78 unsigned char *uncompressed_buffer;
79 size_t uncompressed_buffer_size;
85 struct archive_string pathname;
86 struct archive_string extra;
90 #define ZIP_LENGTH_AT_END 8
92 struct zip_file_header {
99 char compressed_size[4];
100 char uncompressed_size[4];
101 char filename_length[2];
102 char extra_length[2];
105 static const char *compression_names[] = {
117 static int archive_read_format_zip_bid(struct archive_read *);
118 static int archive_read_format_zip_cleanup(struct archive_read *);
119 static int archive_read_format_zip_read_data(struct archive_read *,
120 const void **, size_t *, off_t *);
121 static int archive_read_format_zip_read_data_skip(struct archive_read *a);
122 static int archive_read_format_zip_read_header(struct archive_read *,
123 struct archive_entry *);
124 static int i2(const char *);
125 static int i4(const char *);
126 static unsigned int u2(const char *);
127 static unsigned int u4(const char *);
128 static uint64_t u8(const char *);
129 static int zip_read_data_deflate(struct archive_read *a, const void **buff,
130 size_t *size, off_t *offset);
131 static int zip_read_data_none(struct archive_read *a, const void **buff,
132 size_t *size, off_t *offset);
133 static int zip_read_file_header(struct archive_read *a,
134 struct archive_entry *entry, struct zip *zip);
135 static time_t zip_time(const char *);
136 static void process_extra(const void* extra, struct zip* zip);
139 archive_read_support_format_zip(struct archive *_a)
141 struct archive_read *a = (struct archive_read *)_a;
145 zip = (struct zip *)malloc(sizeof(*zip));
147 archive_set_error(&a->archive, ENOMEM, "Can't allocate zip data");
148 return (ARCHIVE_FATAL);
150 memset(zip, 0, sizeof(*zip));
152 r = __archive_read_register_format(a,
154 archive_read_format_zip_bid,
155 archive_read_format_zip_read_header,
156 archive_read_format_zip_read_data,
157 archive_read_format_zip_read_data_skip,
158 archive_read_format_zip_cleanup);
167 archive_read_format_zip_bid(struct archive_read *a)
174 if (a->archive.archive_format == ARCHIVE_FORMAT_ZIP)
177 bytes_read = (a->decompressor->read_ahead)(a, &h, 4);
182 if (p[0] == 'P' && p[1] == 'K') {
184 if (p[2] == '\001' && p[3] == '\002')
186 else if (p[2] == '\003' && p[3] == '\004')
188 else if (p[2] == '\005' && p[3] == '\006')
190 else if (p[2] == '\007' && p[3] == '\010')
197 archive_read_format_zip_read_header(struct archive_read *a,
198 struct archive_entry *entry)
202 const char *signature;
205 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
206 if (a->archive.archive_format_name == NULL)
207 a->archive.archive_format_name = "ZIP";
209 zip = (struct zip *)(a->format->data);
210 zip->decompress_init = 0;
211 zip->end_of_entry = 0;
212 zip->end_of_entry_cleanup = 0;
213 zip->entry_uncompressed_bytes_read = 0;
214 zip->entry_compressed_bytes_read = 0;
215 bytes_read = (a->decompressor->read_ahead)(a, &h, 4);
217 return (ARCHIVE_FATAL);
219 signature = (const char *)h;
220 if (signature[0] != 'P' || signature[1] != 'K') {
221 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
223 return (ARCHIVE_FATAL);
226 if (signature[2] == '\001' && signature[3] == '\002') {
227 /* Beginning of central directory. */
228 return (ARCHIVE_EOF);
231 if (signature[2] == '\003' && signature[3] == '\004') {
232 /* Regular file entry. */
233 return (zip_read_file_header(a, entry, zip));
236 if (signature[2] == '\005' && signature[3] == '\006') {
237 /* End-of-archive record. */
238 return (ARCHIVE_EOF);
241 if (signature[2] == '\007' && signature[3] == '\010') {
243 * We should never encounter this record here;
244 * see ZIP_LENGTH_AT_END handling below for details.
246 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
247 "Bad ZIP file: Unexpected end-of-entry record");
248 return (ARCHIVE_FATAL);
251 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
252 "Damaged ZIP file or unsupported format variant (%d,%d)",
253 signature[2], signature[3]);
254 return (ARCHIVE_FATAL);
258 zip_read_file_header(struct archive_read *a, struct archive_entry *entry,
261 const struct zip_file_header *p;
266 (a->decompressor->read_ahead)(a, &h, sizeof(struct zip_file_header));
267 if (bytes_read < (int)sizeof(struct zip_file_header)) {
268 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
269 "Truncated ZIP file header");
270 return (ARCHIVE_FATAL);
272 p = (const struct zip_file_header *)h;
274 zip->version = p->version[0];
275 zip->system = p->version[1];
276 zip->flags = i2(p->flags);
277 zip->compression = i2(p->compression);
278 if (zip->compression <
279 sizeof(compression_names)/sizeof(compression_names[0]))
280 zip->compression_name = compression_names[zip->compression];
282 zip->compression_name = "??";
283 zip->mtime = zip_time(p->timedate);
289 zip->crc32 = i4(p->crc32);
290 zip->filename_length = i2(p->filename_length);
291 zip->extra_length = i2(p->extra_length);
292 zip->uncompressed_size = u4(p->uncompressed_size);
293 zip->compressed_size = u4(p->compressed_size);
295 (a->decompressor->consume)(a, sizeof(struct zip_file_header));
298 /* Read the filename. */
299 bytes_read = (a->decompressor->read_ahead)(a, &h, zip->filename_length);
300 if (bytes_read < zip->filename_length) {
301 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
302 "Truncated ZIP file header");
303 return (ARCHIVE_FATAL);
305 if (archive_string_ensure(&zip->pathname, zip->filename_length) == NULL)
306 __archive_errx(1, "Out of memory");
307 archive_strncpy(&zip->pathname, (const char *)h, zip->filename_length);
308 (a->decompressor->consume)(a, zip->filename_length);
309 archive_entry_set_pathname(entry, zip->pathname.s);
311 if (zip->pathname.s[archive_strlen(&zip->pathname) - 1] == '/')
312 zip->mode = AE_IFDIR | 0777;
314 zip->mode = AE_IFREG | 0777;
316 /* Read the extra data. */
317 bytes_read = (a->decompressor->read_ahead)(a, &h, zip->extra_length);
318 if (bytes_read < zip->extra_length) {
319 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
320 "Truncated ZIP file header");
321 return (ARCHIVE_FATAL);
323 process_extra(h, zip);
324 (a->decompressor->consume)(a, zip->extra_length);
326 /* Populate some additional entry fields: */
327 archive_entry_set_mode(entry, zip->mode);
328 archive_entry_set_uid(entry, zip->uid);
329 archive_entry_set_gid(entry, zip->gid);
330 archive_entry_set_mtime(entry, zip->mtime, 0);
331 archive_entry_set_ctime(entry, zip->ctime, 0);
332 archive_entry_set_atime(entry, zip->atime, 0);
333 archive_entry_set_size(entry, zip->uncompressed_size);
335 zip->entry_bytes_remaining = zip->compressed_size;
336 zip->entry_offset = 0;
338 /* If there's no body, force read_data() to return EOF immediately. */
339 if (zip->entry_bytes_remaining < 1)
340 zip->end_of_entry = 1;
342 /* Set up a more descriptive format name. */
343 sprintf(zip->format_name, "ZIP %d.%d (%s)",
344 zip->version / 10, zip->version % 10,
345 zip->compression_name);
346 a->archive.archive_format_name = zip->format_name;
351 /* Convert an MSDOS-style date/time into Unix-style time. */
353 zip_time(const char *p)
358 msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
359 msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
361 memset(&ts, 0, sizeof(ts));
362 ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
363 ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
364 ts.tm_mday = msDate & 0x1f; /* Day of month. */
365 ts.tm_hour = (msTime >> 11) & 0x1f;
366 ts.tm_min = (msTime >> 5) & 0x3f;
367 ts.tm_sec = (msTime << 1) & 0x3e;
373 archive_read_format_zip_read_data(struct archive_read *a,
374 const void **buff, size_t *size, off_t *offset)
379 zip = (struct zip *)(a->format->data);
382 * If we hit end-of-entry last time, clean up and return
383 * ARCHIVE_EOF this time.
385 if (zip->end_of_entry) {
386 if (!zip->end_of_entry_cleanup) {
387 if (zip->flags & ZIP_LENGTH_AT_END) {
391 (a->decompressor->read_ahead)(a, &h, 16);
392 if (bytes_read < 16) {
393 archive_set_error(&a->archive,
394 ARCHIVE_ERRNO_FILE_FORMAT,
395 "Truncated ZIP end-of-file record");
396 return (ARCHIVE_FATAL);
399 zip->crc32 = i4(p + 4);
400 zip->compressed_size = u4(p + 8);
401 zip->uncompressed_size = u4(p + 12);
402 bytes_read = (a->decompressor->consume)(a, 16);
405 /* Check file size, CRC against these values. */
406 if (zip->compressed_size != zip->entry_compressed_bytes_read) {
407 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
408 "ZIP compressed data is wrong size");
409 return (ARCHIVE_WARN);
411 /* Size field only stores the lower 32 bits of the actual size. */
412 if ((zip->uncompressed_size & UINT32_MAX)
413 != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
414 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
415 "ZIP uncompressed data is wrong size");
416 return (ARCHIVE_WARN);
418 /* TODO: Compute CRC. */
420 if (zip->crc32 != zip->entry_crc32_calculated) {
421 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
422 "ZIP data CRC error");
423 return (ARCHIVE_WARN);
426 /* End-of-entry cleanup done. */
427 zip->end_of_entry_cleanup = 1;
429 *offset = zip->entry_uncompressed_bytes_read;
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->format->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->decompressor->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->decompressor->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->format->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 if (zip->stream_valid)
548 r = inflateReset(&zip->stream);
550 r = inflateInit2(&zip->stream,
551 -15 /* Don't check for zlib header */);
553 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
554 "Can't initialize ZIP decompression.");
555 return (ARCHIVE_FATAL);
557 /* Stream structure has been set up. */
558 zip->stream_valid = 1;
559 /* We've initialized decompression for this stream. */
560 zip->decompress_init = 1;
564 * Note: '1' here is a performance optimization.
565 * Recall that the decompression layer returns a count of
566 * available bytes; asking for more than that forces the
567 * decompressor to combine reads by copying data.
569 bytes_avail = (a->decompressor->read_ahead)(a, &compressed_buff, 1);
570 if (bytes_avail <= 0) {
571 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
572 "Truncated ZIP file body");
573 return (ARCHIVE_FATAL);
577 * A bug in zlib.h: stream.next_in should be marked 'const'
578 * but isn't (the library never alters data through the
579 * next_in pointer, only reads it). The result: this ugly
580 * cast to remove 'const'.
582 zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
583 zip->stream.avail_in = bytes_avail;
584 zip->stream.total_in = 0;
585 zip->stream.next_out = zip->uncompressed_buffer;
586 zip->stream.avail_out = zip->uncompressed_buffer_size;
587 zip->stream.total_out = 0;
589 r = inflate(&zip->stream, 0);
594 zip->end_of_entry = 1;
597 archive_set_error(&a->archive, ENOMEM,
598 "Out of memory for ZIP decompression");
599 return (ARCHIVE_FATAL);
601 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
602 "ZIP decompression failed (%d)", r);
603 return (ARCHIVE_FATAL);
606 /* Consume as much as the compressor actually used. */
607 bytes_avail = zip->stream.total_in;
608 (a->decompressor->consume)(a, bytes_avail);
609 zip->entry_bytes_remaining -= bytes_avail;
610 zip->entry_compressed_bytes_read += bytes_avail;
612 *offset = zip->entry_offset;
613 *size = zip->stream.total_out;
614 zip->entry_uncompressed_bytes_read += *size;
615 *buff = zip->uncompressed_buffer;
616 zip->entry_offset += *size;
621 zip_read_data_deflate(struct archive_read *a, const void **buff,
622 size_t *size, off_t *offset)
627 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
628 "libarchive compiled without deflate support (no libz)");
629 return (ARCHIVE_FATAL);
634 archive_read_format_zip_read_data_skip(struct archive_read *a)
637 const void *buff = NULL;
640 zip = (struct zip *)(a->format->data);
643 * If the length is at the end, we have no choice but
644 * to decompress all the data to find the end marker.
646 if (zip->flags & ZIP_LENGTH_AT_END) {
651 r = archive_read_format_zip_read_data(a, &buff,
653 } while (r == ARCHIVE_OK);
658 * If the length is at the beginning, we can skip the
659 * compressed data much more quickly.
661 while (zip->entry_bytes_remaining > 0) {
662 bytes_avail = (a->decompressor->read_ahead)(a, &buff, 1);
663 if (bytes_avail <= 0) {
664 archive_set_error(&a->archive,
665 ARCHIVE_ERRNO_FILE_FORMAT,
666 "Truncated ZIP file body");
667 return (ARCHIVE_FATAL);
669 if (bytes_avail > zip->entry_bytes_remaining)
670 bytes_avail = zip->entry_bytes_remaining;
671 (a->decompressor->consume)(a, bytes_avail);
672 zip->entry_bytes_remaining -= bytes_avail;
674 /* This entry is finished and done. */
675 zip->end_of_entry_cleanup = zip->end_of_entry = 1;
680 archive_read_format_zip_cleanup(struct archive_read *a)
684 zip = (struct zip *)(a->format->data);
686 if (zip->stream_valid)
687 inflateEnd(&zip->stream);
689 free(zip->uncompressed_buffer);
690 archive_string_free(&(zip->pathname));
691 archive_string_free(&(zip->extra));
693 (a->format->data) = NULL;
700 return ((0xff & (int)p[0]) + 256 * (0xff & (int)p[1]));
707 return ((0xffff & i2(p)) + 0x10000 * (0xffff & i2(p+2)));
713 return ((0xff & (unsigned int)p[0]) + 256 * (0xff & (unsigned int)p[1]));
719 return u2(p) + 0x10000 * u2(p+2);
725 return u4(p) + 0x100000000LL * u4(p+4);
729 * The extra data is stored as a list of
730 * id1+size1+data1 + id2+size2+data2 ...
731 * triplets. id and size are 2 bytes each.
734 process_extra(const void* extra, struct zip* zip)
737 const char *p = (const char *)extra;
738 while (offset < zip->extra_length - 4)
740 unsigned short headerid = u2(p + offset);
741 unsigned short datasize = u2(p + offset + 2);
743 if (offset + datasize > zip->extra_length)
746 fprintf(stderr, "Header id 0x%04x, length %d\n",
751 /* Zip64 extended information extra field. */
753 zip->uncompressed_size = u8(p + offset);
755 zip->compressed_size = u8(p + offset + 8);
759 /* Extended time field "UT". */
760 int flags = p[offset];
763 /* Flag bits indicate which dates are present. */
767 fprintf(stderr, "mtime: %d -> %d\n",
768 zip->mtime, i4(p + offset));
772 zip->mtime = i4(p + offset);
780 zip->atime = i4(p + offset);
788 zip->ctime = i4(p + offset);
795 /* Info-ZIP Unix Extra Field (type 2) "Ux". */
797 fprintf(stderr, "uid %d gid %d\n",
798 i2(p + offset), i2(p + offset + 2));
801 zip->uid = i2(p + offset);
803 zip->gid = i2(p + offset + 2);
811 if (offset != zip->extra_length)
814 "Extra data field contents do not match reported size!");