Import libarchive 2.4.0 which brings some performance enhancements.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_read_support_format_zip.c
1 /*-
2  * Copyright (c) 2004 Tim Kientzle
3  * All rights reserved.
4  *
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.
13  *
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.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_zip.c,v 1.15 2007/10/12 04:08:28 kientzle Exp $");
28
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
40
41 #include "archive.h"
42 #include "archive_entry.h"
43 #include "archive_private.h"
44 #include "archive_read_private.h"
45
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;
50
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;
54
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;
66
67         /* Flags to mark progress of decompression. */
68         char                    decompress_init;
69         char                    end_of_entry;
70         char                    end_of_entry_cleanup;
71
72         long                    crc32;
73         ssize_t                 filename_length;
74         ssize_t                 extra_length;
75         int64_t                 uncompressed_size;
76         int64_t                 compressed_size;
77
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
84
85         struct archive_string   pathname;
86         struct archive_string   extra;
87         char    format_name[64];
88 };
89
90 #define ZIP_LENGTH_AT_END       8
91
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];
103 };
104
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"
115 };
116
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);
137
138 int
139 archive_read_support_format_zip(struct archive *_a)
140 {
141         struct archive_read *a = (struct archive_read *)_a;
142         struct zip *zip;
143         int r;
144
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);
149         }
150         memset(zip, 0, sizeof(*zip));
151
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);
159
160         if (r != ARCHIVE_OK)
161                 free(zip);
162         return (ARCHIVE_OK);
163 }
164
165
166 static int
167 archive_read_format_zip_bid(struct archive_read *a)
168 {
169         int bytes_read;
170         int bid = 0;
171         const void *h;
172         const char *p;
173
174         if (a->archive.archive_format == ARCHIVE_FORMAT_ZIP)
175                 bid += 1;
176
177         bytes_read = (a->decompressor->read_ahead)(a, &h, 4);
178         if (bytes_read < 4)
179             return (-1);
180         p = (const char *)h;
181
182         /*
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.
186          */
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);
193         }
194         return (0);
195 }
196
197 static int
198 archive_read_format_zip_read_header(struct archive_read *a,
199     struct archive_entry *entry)
200 {
201         int bytes_read;
202         const void *h;
203         const char *signature;
204         struct zip *zip;
205
206         a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
207         if (a->archive.archive_format_name == NULL)
208                 a->archive.archive_format_name = "ZIP";
209
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);
219
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);
225         }
226
227         if (signature[2] == '\001' && signature[3] == '\002') {
228                 /* Beginning of central directory. */
229                 return (ARCHIVE_EOF);
230         }
231
232         if (signature[2] == '\003' && signature[3] == '\004') {
233                 /* Regular file entry. */
234                 return (zip_read_file_header(a, entry, zip));
235         }
236
237         if (signature[2] == '\005' && signature[3] == '\006') {
238                 /* End-of-archive record. */
239                 return (ARCHIVE_EOF);
240         }
241
242         if (signature[2] == '\007' && signature[3] == '\010') {
243                 /*
244                  * We should never encounter this record here;
245                  * see ZIP_LENGTH_AT_END handling below for details.
246                  */
247                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
248                     "Bad ZIP file: Unexpected end-of-entry record");
249                 return (ARCHIVE_FATAL);
250         }
251
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);
256 }
257
258 int
259 zip_read_file_header(struct archive_read *a, struct archive_entry *entry,
260     struct zip *zip)
261 {
262         const struct zip_file_header *p;
263         const void *h;
264         int bytes_read;
265
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);
272         }
273         p = (const struct zip_file_header *)h;
274
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);
295
296         (a->decompressor->consume)(a, sizeof(struct zip_file_header));
297
298
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);
305         }
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);
311
312         if (zip->pathname.s[archive_strlen(&zip->pathname) - 1] == '/')
313                 zip->mode = AE_IFDIR | 0777;
314         else
315                 zip->mode = AE_IFREG | 0777;
316
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);
323         }
324         process_extra(h, zip);
325         (a->decompressor->consume)(a, zip->extra_length);
326
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);
335
336         zip->entry_bytes_remaining = zip->compressed_size;
337         zip->entry_offset = 0;
338
339         /* If there's no body, force read_data() to return EOF immediately. */
340         if (zip->entry_bytes_remaining < 1)
341                 zip->end_of_entry = 1;
342
343         /* Set up a more descriptive format name. */
344         sprintf(zip->format_name, "ZIP %d.%d (%s)",
345             zip->version / 10, zip->version % 10,
346             zip->compression_name);
347         a->archive.archive_format_name = zip->format_name;
348
349         return (ARCHIVE_OK);
350 }
351
352 /* Convert an MSDOS-style date/time into Unix-style time. */
353 static time_t
354 zip_time(const char *p)
355 {
356         int msTime, msDate;
357         struct tm ts;
358
359         msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
360         msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
361
362         memset(&ts, 0, sizeof(ts));
363         ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
364         ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
365         ts.tm_mday = msDate & 0x1f; /* Day of month. */
366         ts.tm_hour = (msTime >> 11) & 0x1f;
367         ts.tm_min = (msTime >> 5) & 0x3f;
368         ts.tm_sec = (msTime << 1) & 0x3e;
369         ts.tm_isdst = -1;
370         return mktime(&ts);
371 }
372
373 static int
374 archive_read_format_zip_read_data(struct archive_read *a,
375     const void **buff, size_t *size, off_t *offset)
376 {
377         int r;
378         struct zip *zip;
379
380         zip = (struct zip *)(a->format->data);
381
382         /*
383          * If we hit end-of-entry last time, clean up and return
384          * ARCHIVE_EOF this time.
385          */
386         if (zip->end_of_entry) {
387                 if (!zip->end_of_entry_cleanup) {
388                         if (zip->flags & ZIP_LENGTH_AT_END) {
389                                 const void *h;
390                                 const char *p;
391                                 int bytes_read =
392                                     (a->decompressor->read_ahead)(a, &h, 16);
393                                 if (bytes_read < 16) {
394                                         archive_set_error(&a->archive,
395                                             ARCHIVE_ERRNO_FILE_FORMAT,
396                                             "Truncated ZIP end-of-file record");
397                                         return (ARCHIVE_FATAL);
398                                 }
399                                 p = (const char *)h;
400                                 zip->crc32 = i4(p + 4);
401                                 zip->compressed_size = u4(p + 8);
402                                 zip->uncompressed_size = u4(p + 12);
403                                 bytes_read = (a->decompressor->consume)(a, 16);
404                         }
405
406                         /* Check file size, CRC against these values. */
407                         if (zip->compressed_size != zip->entry_compressed_bytes_read) {
408                                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
409                                     "ZIP compressed data is wrong size");
410                                 return (ARCHIVE_WARN);
411                         }
412                         /* Size field only stores the lower 32 bits of the actual size. */
413                         if ((zip->uncompressed_size & UINT32_MAX)
414                             != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
415                                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
416                                     "ZIP uncompressed data is wrong size");
417                                 return (ARCHIVE_WARN);
418                         }
419 /* TODO: Compute CRC. */
420 /*
421                         if (zip->crc32 != zip->entry_crc32_calculated) {
422                                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
423                                     "ZIP data CRC error");
424                                 return (ARCHIVE_WARN);
425                         }
426 */
427                         /* End-of-entry cleanup done. */
428                         zip->end_of_entry_cleanup = 1;
429                 }
430                 *offset = zip->entry_uncompressed_bytes_read;
431                 *size = 0;
432                 *buff = NULL;
433                 return (ARCHIVE_EOF);
434         }
435
436         switch(zip->compression) {
437         case 0:  /* No compression. */
438                 r =  zip_read_data_none(a, buff, size, offset);
439                 break;
440         case 8: /* Deflate compression. */
441                 r =  zip_read_data_deflate(a, buff, size, offset);
442                 break;
443         default: /* Unsupported compression. */
444                 *buff = NULL;
445                 *size = 0;
446                 *offset = 0;
447                 /* Return a warning. */
448                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
449                     "Unsupported ZIP compression method (%s)",
450                     zip->compression_name);
451                 if (zip->flags & ZIP_LENGTH_AT_END) {
452                         /*
453                          * ZIP_LENGTH_AT_END requires us to
454                          * decompress the entry in order to
455                          * skip it, but we don't know this
456                          * compression method, so we give up.
457                          */
458                         r = ARCHIVE_FATAL;
459                 } else {
460                         /* We know compressed size; just skip it. */
461                         archive_read_format_zip_read_data_skip(a);
462                         r = ARCHIVE_WARN;
463                 }
464                 break;
465         }
466         return (r);
467 }
468
469 /*
470  * Read "uncompressed" data.  According to the current specification,
471  * if ZIP_LENGTH_AT_END is specified, then the size fields in the
472  * initial file header are supposed to be set to zero.  This would, of
473  * course, make it impossible for us to read the archive, since we
474  * couldn't determine the end of the file data.  Info-ZIP seems to
475  * include the real size fields both before and after the data in this
476  * case (the CRC only appears afterwards), so this works as you would
477  * expect.
478  *
479  * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
480  * zip->end_of_entry if it consumes all of the data.
481  */
482 static int
483 zip_read_data_none(struct archive_read *a, const void **buff,
484     size_t *size, off_t *offset)
485 {
486         struct zip *zip;
487         ssize_t bytes_avail;
488
489         zip = (struct zip *)(a->format->data);
490
491         if (zip->entry_bytes_remaining == 0) {
492                 *buff = NULL;
493                 *size = 0;
494                 *offset = zip->entry_offset;
495                 zip->end_of_entry = 1;
496                 return (ARCHIVE_OK);
497         }
498         /*
499          * Note: '1' here is a performance optimization.
500          * Recall that the decompression layer returns a count of
501          * available bytes; asking for more than that forces the
502          * decompressor to combine reads by copying data.
503          */
504         bytes_avail = (a->decompressor->read_ahead)(a, buff, 1);
505         if (bytes_avail <= 0) {
506                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
507                     "Truncated ZIP file data");
508                 return (ARCHIVE_FATAL);
509         }
510         if (bytes_avail > zip->entry_bytes_remaining)
511                 bytes_avail = zip->entry_bytes_remaining;
512         (a->decompressor->consume)(a, bytes_avail);
513         *size = bytes_avail;
514         *offset = zip->entry_offset;
515         zip->entry_offset += *size;
516         zip->entry_bytes_remaining -= *size;
517         zip->entry_uncompressed_bytes_read += *size;
518         zip->entry_compressed_bytes_read += *size;
519         return (ARCHIVE_OK);
520 }
521
522 #ifdef HAVE_ZLIB_H
523 static int
524 zip_read_data_deflate(struct archive_read *a, const void **buff,
525     size_t *size, off_t *offset)
526 {
527         struct zip *zip;
528         ssize_t bytes_avail;
529         const void *compressed_buff;
530         int r;
531
532         zip = (struct zip *)(a->format->data);
533
534         /* If the buffer hasn't been allocated, allocate it now. */
535         if (zip->uncompressed_buffer == NULL) {
536                 zip->uncompressed_buffer_size = 32 * 1024;
537                 zip->uncompressed_buffer
538                     = (unsigned char *)malloc(zip->uncompressed_buffer_size);
539                 if (zip->uncompressed_buffer == NULL) {
540                         archive_set_error(&a->archive, ENOMEM,
541                             "No memory for ZIP decompression");
542                         return (ARCHIVE_FATAL);
543                 }
544         }
545
546         /* If we haven't yet read any data, initialize the decompressor. */
547         if (!zip->decompress_init) {
548                 if (zip->stream_valid)
549                         r = inflateReset(&zip->stream);
550                 else
551                         r = inflateInit2(&zip->stream,
552                             -15 /* Don't check for zlib header */);
553                 if (r != Z_OK) {
554                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
555                             "Can't initialize ZIP decompression.");
556                         return (ARCHIVE_FATAL);
557                 }
558                 /* Stream structure has been set up. */
559                 zip->stream_valid = 1;
560                 /* We've initialized decompression for this stream. */
561                 zip->decompress_init = 1;
562         }
563
564         /*
565          * Note: '1' here is a performance optimization.
566          * Recall that the decompression layer returns a count of
567          * available bytes; asking for more than that forces the
568          * decompressor to combine reads by copying data.
569          */
570         bytes_avail = (a->decompressor->read_ahead)(a, &compressed_buff, 1);
571         if (bytes_avail <= 0) {
572                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
573                     "Truncated ZIP file body");
574                 return (ARCHIVE_FATAL);
575         }
576
577         /*
578          * A bug in zlib.h: stream.next_in should be marked 'const'
579          * but isn't (the library never alters data through the
580          * next_in pointer, only reads it).  The result: this ugly
581          * cast to remove 'const'.
582          */
583         zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
584         zip->stream.avail_in = bytes_avail;
585         zip->stream.total_in = 0;
586         zip->stream.next_out = zip->uncompressed_buffer;
587         zip->stream.avail_out = zip->uncompressed_buffer_size;
588         zip->stream.total_out = 0;
589
590         r = inflate(&zip->stream, 0);
591         switch (r) {
592         case Z_OK:
593                 break;
594         case Z_STREAM_END:
595                 zip->end_of_entry = 1;
596                 break;
597         case Z_MEM_ERROR:
598                 archive_set_error(&a->archive, ENOMEM,
599                     "Out of memory for ZIP decompression");
600                 return (ARCHIVE_FATAL);
601         default:
602                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
603                     "ZIP decompression failed (%d)", r);
604                 return (ARCHIVE_FATAL);
605         }
606
607         /* Consume as much as the compressor actually used. */
608         bytes_avail = zip->stream.total_in;
609         (a->decompressor->consume)(a, bytes_avail);
610         zip->entry_bytes_remaining -= bytes_avail;
611         zip->entry_compressed_bytes_read += bytes_avail;
612
613         *offset = zip->entry_offset;
614         *size = zip->stream.total_out;
615         zip->entry_uncompressed_bytes_read += *size;
616         *buff = zip->uncompressed_buffer;
617         zip->entry_offset += *size;
618         return (ARCHIVE_OK);
619 }
620 #else
621 static int
622 zip_read_data_deflate(struct archive_read *a, const void **buff,
623     size_t *size, off_t *offset)
624 {
625         *buff = NULL;
626         *size = 0;
627         *offset = 0;
628         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
629             "libarchive compiled without deflate support (no libz)");
630         return (ARCHIVE_FATAL);
631 }
632 #endif
633
634 static int
635 archive_read_format_zip_read_data_skip(struct archive_read *a)
636 {
637         struct zip *zip;
638         const void *buff = NULL;
639         ssize_t bytes_avail;
640
641         zip = (struct zip *)(a->format->data);
642
643         /*
644          * If the length is at the end, we have no choice but
645          * to decompress all the data to find the end marker.
646          */
647         if (zip->flags & ZIP_LENGTH_AT_END) {
648                 size_t size;
649                 off_t offset;
650                 int r;
651                 do {
652                         r = archive_read_format_zip_read_data(a, &buff,
653                             &size, &offset);
654                 } while (r == ARCHIVE_OK);
655                 return (r);
656         }
657
658         /*
659          * If the length is at the beginning, we can skip the
660          * compressed data much more quickly.
661          */
662         while (zip->entry_bytes_remaining > 0) {
663                 bytes_avail = (a->decompressor->read_ahead)(a, &buff, 1);
664                 if (bytes_avail <= 0) {
665                         archive_set_error(&a->archive,
666                             ARCHIVE_ERRNO_FILE_FORMAT,
667                             "Truncated ZIP file body");
668                         return (ARCHIVE_FATAL);
669                 }
670                 if (bytes_avail > zip->entry_bytes_remaining)
671                         bytes_avail = zip->entry_bytes_remaining;
672                 (a->decompressor->consume)(a, bytes_avail);
673                 zip->entry_bytes_remaining -= bytes_avail;
674         }
675         /* This entry is finished and done. */
676         zip->end_of_entry_cleanup = zip->end_of_entry = 1;
677         return (ARCHIVE_OK);
678 }
679
680 static int
681 archive_read_format_zip_cleanup(struct archive_read *a)
682 {
683         struct zip *zip;
684
685         zip = (struct zip *)(a->format->data);
686 #ifdef HAVE_ZLIB_H
687         if (zip->stream_valid)
688                 inflateEnd(&zip->stream);
689 #endif
690         free(zip->uncompressed_buffer);
691         archive_string_free(&(zip->pathname));
692         archive_string_free(&(zip->extra));
693         free(zip);
694         (a->format->data) = NULL;
695         return (ARCHIVE_OK);
696 }
697
698 static int
699 i2(const char *p)
700 {
701         return ((0xff & (int)p[0]) + 256 * (0xff & (int)p[1]));
702 }
703
704
705 static int
706 i4(const char *p)
707 {
708         return ((0xffff & i2(p)) + 0x10000 * (0xffff & i2(p+2)));
709 }
710
711 static unsigned int
712 u2(const char *p)
713 {
714         return ((0xff & (unsigned int)p[0]) + 256 * (0xff & (unsigned int)p[1]));
715 }
716
717 static unsigned int
718 u4(const char *p)
719 {
720         return u2(p) + 0x10000 * u2(p+2);
721 }
722
723 static uint64_t
724 u8(const char *p)
725 {
726         return u4(p) + 0x100000000LL * u4(p+4);
727 }
728
729 /*
730  * The extra data is stored as a list of
731  *      id1+size1+data1 + id2+size2+data2 ...
732  *  triplets.  id and size are 2 bytes each.
733  */
734 static void
735 process_extra(const void* extra, struct zip* zip)
736 {
737         int offset = 0;
738         const char *p = (const char *)extra;
739         while (offset < zip->extra_length - 4)
740         {
741                 unsigned short headerid = u2(p + offset);
742                 unsigned short datasize = u2(p + offset + 2);
743                 offset += 4;
744                 if (offset + datasize > zip->extra_length)
745                         break;
746 #ifdef DEBUG
747                 fprintf(stderr, "Header id 0x%04x, length %d\n",
748                     headerid, datasize);
749 #endif
750                 switch (headerid) {
751                 case 0x0001:
752                         /* Zip64 extended information extra field. */
753                         if (datasize >= 8)
754                                 zip->uncompressed_size = u8(p + offset);
755                         if (datasize >= 16)
756                                 zip->compressed_size = u8(p + offset + 8);
757                         break;
758                 case 0x5455:
759                 {
760                         /* Extended time field "UT". */
761                         int flags = p[offset];
762                         offset++;
763                         datasize--;
764                         /* Flag bits indicate which dates are present. */
765                         if (flags & 0x01)
766                         {
767 #ifdef DEBUG
768                                 fprintf(stderr, "mtime: %lld -> %d\n",
769                                     (long long)zip->mtime, i4(p + offset));
770 #endif
771                                 if (datasize < 4)
772                                         break;
773                                 zip->mtime = i4(p + offset);
774                                 offset += 4;
775                                 datasize -= 4;
776                         }
777                         if (flags & 0x02)
778                         {
779                                 if (datasize < 4)
780                                         break;
781                                 zip->atime = i4(p + offset);
782                                 offset += 4;
783                                 datasize -= 4;
784                         }
785                         if (flags & 0x04)
786                         {
787                                 if (datasize < 4)
788                                         break;
789                                 zip->ctime = i4(p + offset);
790                                 offset += 4;
791                                 datasize -= 4;
792                         }
793                         break;
794                 }
795                 case 0x7855:
796                         /* Info-ZIP Unix Extra Field (type 2) "Ux". */
797 #ifdef DEBUG
798                         fprintf(stderr, "uid %d gid %d\n",
799                             i2(p + offset), i2(p + offset + 2));
800 #endif
801                         if (datasize >= 2)
802                                 zip->uid = i2(p + offset);
803                         if (datasize >= 4)
804                                 zip->gid = i2(p + offset + 2);
805                         break;
806                 default:
807                         break;
808                 }
809                 offset += datasize;
810         }
811 #ifdef DEBUG
812         if (offset != zip->extra_length)
813         {
814                 fprintf(stderr,
815                     "Extra data field contents do not match reported size!");
816         }
817 #endif
818 }