RCSID updates, warning fixes
[dfdiff.git] / contrib / libarchive-2 / libarchive / archive_read_support_format_zip.c
blob7dcc4ea2b1c198bbd1b6bac7e6348df35a9b30b4
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.16 2007/12/04 06:32:12 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"
46 struct zip {
47 /* entry_bytes_remaining is the number of bytes we expect. */
48 int64_t entry_bytes_remaining;
49 int64_t entry_offset;
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;
55 unsigned version;
56 unsigned system;
57 unsigned flags;
58 unsigned compression;
59 const char * compression_name;
60 time_t mtime;
61 time_t ctime;
62 time_t atime;
63 mode_t mode;
64 uid_t uid;
65 gid_t gid;
67 /* Flags to mark progress of decompression. */
68 char decompress_init;
69 char end_of_entry;
70 char end_of_entry_cleanup;
72 long crc32;
73 ssize_t filename_length;
74 ssize_t extra_length;
75 int64_t uncompressed_size;
76 int64_t compressed_size;
78 unsigned char *uncompressed_buffer;
79 size_t uncompressed_buffer_size;
80 #ifdef HAVE_ZLIB_H
81 z_stream stream;
82 char stream_valid;
83 #endif
85 struct archive_string pathname;
86 struct archive_string extra;
87 char format_name[64];
90 #define ZIP_LENGTH_AT_END 8
92 struct zip_file_header {
93 char signature[4];
94 char version[2];
95 char flags[2];
96 char compression[2];
97 char timedate[4];
98 char crc32[4];
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[] = {
106 "uncompressed",
107 "shrinking",
108 "reduced-1",
109 "reduced-2",
110 "reduced-3",
111 "reduced-4",
112 "imploded",
113 "reserved",
114 "deflation"
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;
142 struct zip *zip;
143 int r;
145 zip = (struct zip *)malloc(sizeof(*zip));
146 if (zip == NULL) {
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,
153 zip,
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);
160 if (r != ARCHIVE_OK)
161 free(zip);
162 return (ARCHIVE_OK);
166 static int
167 archive_read_format_zip_bid(struct archive_read *a)
169 int bytes_read;
170 int bid = 0;
171 const void *h;
172 const char *p;
174 if (a->archive.archive_format == ARCHIVE_FORMAT_ZIP)
175 bid += 1;
177 bytes_read = (a->decompressor->read_ahead)(a, &h, 4);
178 if (bytes_read < 4)
179 return (-1);
180 p = (const char *)h;
183 * Bid of 30 here is: 16 bits for "PK",
184 * next 16-bit field has four options (-2 bits).
185 * 16 + 16-2 = 30.
187 if (p[0] == 'P' && p[1] == 'K') {
188 if ((p[2] == '\001' && p[3] == '\002')
189 || (p[2] == '\003' && p[3] == '\004')
190 || (p[2] == '\005' && p[3] == '\006')
191 || (p[2] == '\007' && p[3] == '\010'))
192 return (30);
194 return (0);
197 static int
198 archive_read_format_zip_read_header(struct archive_read *a,
199 struct archive_entry *entry)
201 int bytes_read;
202 const void *h;
203 const char *signature;
204 struct zip *zip;
206 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
207 if (a->archive.archive_format_name == NULL)
208 a->archive.archive_format_name = "ZIP";
210 zip = (struct zip *)(a->format->data);
211 zip->decompress_init = 0;
212 zip->end_of_entry = 0;
213 zip->end_of_entry_cleanup = 0;
214 zip->entry_uncompressed_bytes_read = 0;
215 zip->entry_compressed_bytes_read = 0;
216 bytes_read = (a->decompressor->read_ahead)(a, &h, 4);
217 if (bytes_read < 4)
218 return (ARCHIVE_FATAL);
220 signature = (const char *)h;
221 if (signature[0] != 'P' || signature[1] != 'K') {
222 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
223 "Bad ZIP file");
224 return (ARCHIVE_FATAL);
227 if (signature[2] == '\001' && signature[3] == '\002') {
228 /* Beginning of central directory. */
229 return (ARCHIVE_EOF);
232 if (signature[2] == '\003' && signature[3] == '\004') {
233 /* Regular file entry. */
234 return (zip_read_file_header(a, entry, zip));
237 if (signature[2] == '\005' && signature[3] == '\006') {
238 /* End-of-archive record. */
239 return (ARCHIVE_EOF);
242 if (signature[2] == '\007' && signature[3] == '\010') {
244 * We should never encounter this record here;
245 * see ZIP_LENGTH_AT_END handling below for details.
247 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
248 "Bad ZIP file: Unexpected end-of-entry record");
249 return (ARCHIVE_FATAL);
252 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
253 "Damaged ZIP file or unsupported format variant (%d,%d)",
254 signature[2], signature[3]);
255 return (ARCHIVE_FATAL);
259 zip_read_file_header(struct archive_read *a, struct archive_entry *entry,
260 struct zip *zip)
262 const struct zip_file_header *p;
263 const void *h;
264 int bytes_read;
266 bytes_read =
267 (a->decompressor->read_ahead)(a, &h, sizeof(struct zip_file_header));
268 if (bytes_read < (int)sizeof(struct zip_file_header)) {
269 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
270 "Truncated ZIP file header");
271 return (ARCHIVE_FATAL);
273 p = (const struct zip_file_header *)h;
275 zip->version = p->version[0];
276 zip->system = p->version[1];
277 zip->flags = i2(p->flags);
278 zip->compression = i2(p->compression);
279 if (zip->compression <
280 sizeof(compression_names)/sizeof(compression_names[0]))
281 zip->compression_name = compression_names[zip->compression];
282 else
283 zip->compression_name = "??";
284 zip->mtime = zip_time(p->timedate);
285 zip->ctime = 0;
286 zip->atime = 0;
287 zip->mode = 0;
288 zip->uid = 0;
289 zip->gid = 0;
290 zip->crc32 = i4(p->crc32);
291 zip->filename_length = i2(p->filename_length);
292 zip->extra_length = i2(p->extra_length);
293 zip->uncompressed_size = u4(p->uncompressed_size);
294 zip->compressed_size = u4(p->compressed_size);
296 (a->decompressor->consume)(a, sizeof(struct zip_file_header));
299 /* Read the filename. */
300 bytes_read = (a->decompressor->read_ahead)(a, &h, zip->filename_length);
301 if (bytes_read < zip->filename_length) {
302 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
303 "Truncated ZIP file header");
304 return (ARCHIVE_FATAL);
306 if (archive_string_ensure(&zip->pathname, zip->filename_length) == NULL)
307 __archive_errx(1, "Out of memory");
308 archive_strncpy(&zip->pathname, (const char *)h, zip->filename_length);
309 (a->decompressor->consume)(a, zip->filename_length);
310 archive_entry_set_pathname(entry, zip->pathname.s);
312 if (zip->pathname.s[archive_strlen(&zip->pathname) - 1] == '/')
313 zip->mode = AE_IFDIR | 0777;
314 else
315 zip->mode = AE_IFREG | 0777;
317 /* Read the extra data. */
318 bytes_read = (a->decompressor->read_ahead)(a, &h, zip->extra_length);
319 if (bytes_read < zip->extra_length) {
320 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
321 "Truncated ZIP file header");
322 return (ARCHIVE_FATAL);
324 process_extra(h, zip);
325 (a->decompressor->consume)(a, zip->extra_length);
327 /* Populate some additional entry fields: */
328 archive_entry_set_mode(entry, zip->mode);
329 archive_entry_set_uid(entry, zip->uid);
330 archive_entry_set_gid(entry, zip->gid);
331 archive_entry_set_mtime(entry, zip->mtime, 0);
332 archive_entry_set_ctime(entry, zip->ctime, 0);
333 archive_entry_set_atime(entry, zip->atime, 0);
334 archive_entry_set_size(entry, zip->uncompressed_size);
336 zip->entry_bytes_remaining = zip->compressed_size;
337 zip->entry_offset = 0;
339 /* If there's no body, force read_data() to return EOF immediately. */
340 if (0 == (zip->flags & ZIP_LENGTH_AT_END)
341 && zip->entry_bytes_remaining < 1)
342 zip->end_of_entry = 1;
344 /* Set up a more descriptive format name. */
345 sprintf(zip->format_name, "ZIP %d.%d (%s)",
346 zip->version / 10, zip->version % 10,
347 zip->compression_name);
348 a->archive.archive_format_name = zip->format_name;
350 return (ARCHIVE_OK);
353 /* Convert an MSDOS-style date/time into Unix-style time. */
354 static time_t
355 zip_time(const char *p)
357 int msTime, msDate;
358 struct tm ts;
360 msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
361 msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
363 memset(&ts, 0, sizeof(ts));
364 ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
365 ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
366 ts.tm_mday = msDate & 0x1f; /* Day of month. */
367 ts.tm_hour = (msTime >> 11) & 0x1f;
368 ts.tm_min = (msTime >> 5) & 0x3f;
369 ts.tm_sec = (msTime << 1) & 0x3e;
370 ts.tm_isdst = -1;
371 return mktime(&ts);
374 static int
375 archive_read_format_zip_read_data(struct archive_read *a,
376 const void **buff, size_t *size, off_t *offset)
378 int r;
379 struct zip *zip;
381 zip = (struct zip *)(a->format->data);
384 * If we hit end-of-entry last time, clean up and return
385 * ARCHIVE_EOF this time.
387 if (zip->end_of_entry) {
388 if (!zip->end_of_entry_cleanup) {
389 if (zip->flags & ZIP_LENGTH_AT_END) {
390 const void *h;
391 const char *p;
392 int bytes_read =
393 (a->decompressor->read_ahead)(a, &h, 16);
394 if (bytes_read < 16) {
395 archive_set_error(&a->archive,
396 ARCHIVE_ERRNO_FILE_FORMAT,
397 "Truncated ZIP end-of-file record");
398 return (ARCHIVE_FATAL);
400 p = (const char *)h;
401 zip->crc32 = i4(p + 4);
402 zip->compressed_size = u4(p + 8);
403 zip->uncompressed_size = u4(p + 12);
404 bytes_read = (a->decompressor->consume)(a, 16);
407 /* Check file size, CRC against these values. */
408 if (zip->compressed_size != zip->entry_compressed_bytes_read) {
409 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
410 "ZIP compressed data is wrong size");
411 return (ARCHIVE_WARN);
413 /* Size field only stores the lower 32 bits of the actual size. */
414 if ((zip->uncompressed_size & UINT32_MAX)
415 != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
416 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
417 "ZIP uncompressed data is wrong size");
418 return (ARCHIVE_WARN);
420 /* TODO: Compute CRC. */
422 if (zip->crc32 != zip->entry_crc32_calculated) {
423 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
424 "ZIP data CRC error");
425 return (ARCHIVE_WARN);
428 /* End-of-entry cleanup done. */
429 zip->end_of_entry_cleanup = 1;
431 *offset = zip->entry_uncompressed_bytes_read;
432 *size = 0;
433 *buff = NULL;
434 return (ARCHIVE_EOF);
437 switch(zip->compression) {
438 case 0: /* No compression. */
439 r = zip_read_data_none(a, buff, size, offset);
440 break;
441 case 8: /* Deflate compression. */
442 r = zip_read_data_deflate(a, buff, size, offset);
443 break;
444 default: /* Unsupported compression. */
445 *buff = NULL;
446 *size = 0;
447 *offset = 0;
448 /* Return a warning. */
449 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
450 "Unsupported ZIP compression method (%s)",
451 zip->compression_name);
452 if (zip->flags & ZIP_LENGTH_AT_END) {
454 * ZIP_LENGTH_AT_END requires us to
455 * decompress the entry in order to
456 * skip it, but we don't know this
457 * compression method, so we give up.
459 r = ARCHIVE_FATAL;
460 } else {
461 /* We know compressed size; just skip it. */
462 archive_read_format_zip_read_data_skip(a);
463 r = ARCHIVE_WARN;
465 break;
467 return (r);
471 * Read "uncompressed" data. According to the current specification,
472 * if ZIP_LENGTH_AT_END is specified, then the size fields in the
473 * initial file header are supposed to be set to zero. This would, of
474 * course, make it impossible for us to read the archive, since we
475 * couldn't determine the end of the file data. Info-ZIP seems to
476 * include the real size fields both before and after the data in this
477 * case (the CRC only appears afterwards), so this works as you would
478 * expect.
480 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
481 * zip->end_of_entry if it consumes all of the data.
483 static int
484 zip_read_data_none(struct archive_read *a, const void **buff,
485 size_t *size, off_t *offset)
487 struct zip *zip;
488 ssize_t bytes_avail;
490 zip = (struct zip *)(a->format->data);
492 if (zip->entry_bytes_remaining == 0) {
493 *buff = NULL;
494 *size = 0;
495 *offset = zip->entry_offset;
496 zip->end_of_entry = 1;
497 return (ARCHIVE_OK);
500 * Note: '1' here is a performance optimization.
501 * Recall that the decompression layer returns a count of
502 * available bytes; asking for more than that forces the
503 * decompressor to combine reads by copying data.
505 bytes_avail = (a->decompressor->read_ahead)(a, buff, 1);
506 if (bytes_avail <= 0) {
507 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
508 "Truncated ZIP file data");
509 return (ARCHIVE_FATAL);
511 if (bytes_avail > zip->entry_bytes_remaining)
512 bytes_avail = zip->entry_bytes_remaining;
513 (a->decompressor->consume)(a, bytes_avail);
514 *size = bytes_avail;
515 *offset = zip->entry_offset;
516 zip->entry_offset += *size;
517 zip->entry_bytes_remaining -= *size;
518 zip->entry_uncompressed_bytes_read += *size;
519 zip->entry_compressed_bytes_read += *size;
520 return (ARCHIVE_OK);
523 #ifdef HAVE_ZLIB_H
524 static int
525 zip_read_data_deflate(struct archive_read *a, const void **buff,
526 size_t *size, off_t *offset)
528 struct zip *zip;
529 ssize_t bytes_avail;
530 const void *compressed_buff;
531 int r;
533 zip = (struct zip *)(a->format->data);
535 /* If the buffer hasn't been allocated, allocate it now. */
536 if (zip->uncompressed_buffer == NULL) {
537 zip->uncompressed_buffer_size = 32 * 1024;
538 zip->uncompressed_buffer
539 = (unsigned char *)malloc(zip->uncompressed_buffer_size);
540 if (zip->uncompressed_buffer == NULL) {
541 archive_set_error(&a->archive, ENOMEM,
542 "No memory for ZIP decompression");
543 return (ARCHIVE_FATAL);
547 /* If we haven't yet read any data, initialize the decompressor. */
548 if (!zip->decompress_init) {
549 if (zip->stream_valid)
550 r = inflateReset(&zip->stream);
551 else
552 r = inflateInit2(&zip->stream,
553 -15 /* Don't check for zlib header */);
554 if (r != Z_OK) {
555 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
556 "Can't initialize ZIP decompression.");
557 return (ARCHIVE_FATAL);
559 /* Stream structure has been set up. */
560 zip->stream_valid = 1;
561 /* We've initialized decompression for this stream. */
562 zip->decompress_init = 1;
566 * Note: '1' here is a performance optimization.
567 * Recall that the decompression layer returns a count of
568 * available bytes; asking for more than that forces the
569 * decompressor to combine reads by copying data.
571 bytes_avail = (a->decompressor->read_ahead)(a, &compressed_buff, 1);
572 if (bytes_avail <= 0) {
573 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
574 "Truncated ZIP file body");
575 return (ARCHIVE_FATAL);
579 * A bug in zlib.h: stream.next_in should be marked 'const'
580 * but isn't (the library never alters data through the
581 * next_in pointer, only reads it). The result: this ugly
582 * cast to remove 'const'.
584 zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
585 zip->stream.avail_in = bytes_avail;
586 zip->stream.total_in = 0;
587 zip->stream.next_out = zip->uncompressed_buffer;
588 zip->stream.avail_out = zip->uncompressed_buffer_size;
589 zip->stream.total_out = 0;
591 r = inflate(&zip->stream, 0);
592 switch (r) {
593 case Z_OK:
594 break;
595 case Z_STREAM_END:
596 zip->end_of_entry = 1;
597 break;
598 case Z_MEM_ERROR:
599 archive_set_error(&a->archive, ENOMEM,
600 "Out of memory for ZIP decompression");
601 return (ARCHIVE_FATAL);
602 default:
603 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
604 "ZIP decompression failed (%d)", r);
605 return (ARCHIVE_FATAL);
608 /* Consume as much as the compressor actually used. */
609 bytes_avail = zip->stream.total_in;
610 (a->decompressor->consume)(a, bytes_avail);
611 zip->entry_bytes_remaining -= bytes_avail;
612 zip->entry_compressed_bytes_read += bytes_avail;
614 *offset = zip->entry_offset;
615 *size = zip->stream.total_out;
616 zip->entry_uncompressed_bytes_read += *size;
617 *buff = zip->uncompressed_buffer;
618 zip->entry_offset += *size;
619 return (ARCHIVE_OK);
621 #else
622 static int
623 zip_read_data_deflate(struct archive_read *a, const void **buff,
624 size_t *size, off_t *offset)
626 *buff = NULL;
627 *size = 0;
628 *offset = 0;
629 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
630 "libarchive compiled without deflate support (no libz)");
631 return (ARCHIVE_FATAL);
633 #endif
635 static int
636 archive_read_format_zip_read_data_skip(struct archive_read *a)
638 struct zip *zip;
639 const void *buff = NULL;
640 ssize_t bytes_avail;
642 zip = (struct zip *)(a->format->data);
645 * If the length is at the end, we have no choice but
646 * to decompress all the data to find the end marker.
648 if (zip->flags & ZIP_LENGTH_AT_END) {
649 size_t size;
650 off_t offset;
651 int r;
652 do {
653 r = archive_read_format_zip_read_data(a, &buff,
654 &size, &offset);
655 } while (r == ARCHIVE_OK);
656 return (r);
660 * If the length is at the beginning, we can skip the
661 * compressed data much more quickly.
663 while (zip->entry_bytes_remaining > 0) {
664 bytes_avail = (a->decompressor->read_ahead)(a, &buff, 1);
665 if (bytes_avail <= 0) {
666 archive_set_error(&a->archive,
667 ARCHIVE_ERRNO_FILE_FORMAT,
668 "Truncated ZIP file body");
669 return (ARCHIVE_FATAL);
671 if (bytes_avail > zip->entry_bytes_remaining)
672 bytes_avail = zip->entry_bytes_remaining;
673 (a->decompressor->consume)(a, bytes_avail);
674 zip->entry_bytes_remaining -= bytes_avail;
676 /* This entry is finished and done. */
677 zip->end_of_entry_cleanup = zip->end_of_entry = 1;
678 return (ARCHIVE_OK);
681 static int
682 archive_read_format_zip_cleanup(struct archive_read *a)
684 struct zip *zip;
686 zip = (struct zip *)(a->format->data);
687 #ifdef HAVE_ZLIB_H
688 if (zip->stream_valid)
689 inflateEnd(&zip->stream);
690 #endif
691 free(zip->uncompressed_buffer);
692 archive_string_free(&(zip->pathname));
693 archive_string_free(&(zip->extra));
694 free(zip);
695 (a->format->data) = NULL;
696 return (ARCHIVE_OK);
699 static int
700 i2(const char *p)
702 return ((0xff & (int)p[0]) + 256 * (0xff & (int)p[1]));
706 static int
707 i4(const char *p)
709 return ((0xffff & i2(p)) + 0x10000 * (0xffff & i2(p+2)));
712 static unsigned int
713 u2(const char *p)
715 return ((0xff & (unsigned int)p[0]) + 256 * (0xff & (unsigned int)p[1]));
718 static unsigned int
719 u4(const char *p)
721 return u2(p) + 0x10000 * u2(p+2);
724 static uint64_t
725 u8(const char *p)
727 return u4(p) + 0x100000000LL * u4(p+4);
731 * The extra data is stored as a list of
732 * id1+size1+data1 + id2+size2+data2 ...
733 * triplets. id and size are 2 bytes each.
735 static void
736 process_extra(const void* extra, struct zip* zip)
738 int offset = 0;
739 const char *p = (const char *)extra;
740 while (offset < zip->extra_length - 4)
742 unsigned short headerid = u2(p + offset);
743 unsigned short datasize = u2(p + offset + 2);
744 offset += 4;
745 if (offset + datasize > zip->extra_length)
746 break;
747 #ifdef DEBUG
748 fprintf(stderr, "Header id 0x%04x, length %d\n",
749 headerid, datasize);
750 #endif
751 switch (headerid) {
752 case 0x0001:
753 /* Zip64 extended information extra field. */
754 if (datasize >= 8)
755 zip->uncompressed_size = u8(p + offset);
756 if (datasize >= 16)
757 zip->compressed_size = u8(p + offset + 8);
758 break;
759 case 0x5455:
761 /* Extended time field "UT". */
762 int flags = p[offset];
763 offset++;
764 datasize--;
765 /* Flag bits indicate which dates are present. */
766 if (flags & 0x01)
768 #ifdef DEBUG
769 fprintf(stderr, "mtime: %lld -> %d\n",
770 (long long)zip->mtime, i4(p + offset));
771 #endif
772 if (datasize < 4)
773 break;
774 zip->mtime = i4(p + offset);
775 offset += 4;
776 datasize -= 4;
778 if (flags & 0x02)
780 if (datasize < 4)
781 break;
782 zip->atime = i4(p + offset);
783 offset += 4;
784 datasize -= 4;
786 if (flags & 0x04)
788 if (datasize < 4)
789 break;
790 zip->ctime = i4(p + offset);
791 offset += 4;
792 datasize -= 4;
794 break;
796 case 0x7855:
797 /* Info-ZIP Unix Extra Field (type 2) "Ux". */
798 #ifdef DEBUG
799 fprintf(stderr, "uid %d gid %d\n",
800 i2(p + offset), i2(p + offset + 2));
801 #endif
802 if (datasize >= 2)
803 zip->uid = i2(p + offset);
804 if (datasize >= 4)
805 zip->gid = i2(p + offset + 2);
806 break;
807 default:
808 break;
810 offset += datasize;
812 #ifdef DEBUG
813 if (offset != zip->extra_length)
815 fprintf(stderr,
816 "Extra data field contents do not match reported size!");
818 #endif