]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/archive_read_support_format_zip.c
This commit was generated by cvs2svn to compensate for changes in r178382,
[FreeBSD/FreeBSD.git] / lib / 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$");
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 #include "archive_endian.h"
46
47 struct zip {
48         /* entry_bytes_remaining is the number of bytes we expect. */
49         int64_t                 entry_bytes_remaining;
50         int64_t                 entry_offset;
51
52         /* These count the number of bytes actually read for the entry. */
53         int64_t                 entry_compressed_bytes_read;
54         int64_t                 entry_uncompressed_bytes_read;
55
56         unsigned                version;
57         unsigned                system;
58         unsigned                flags;
59         unsigned                compression;
60         const char *            compression_name;
61         time_t                  mtime;
62         time_t                  ctime;
63         time_t                  atime;
64         mode_t                  mode;
65         uid_t                   uid;
66         gid_t                   gid;
67
68         /* Flags to mark progress of decompression. */
69         char                    decompress_init;
70         char                    end_of_entry;
71         char                    end_of_entry_cleanup;
72
73         long                    crc32;
74         ssize_t                 filename_length;
75         ssize_t                 extra_length;
76         int64_t                 uncompressed_size;
77         int64_t                 compressed_size;
78
79         unsigned char           *uncompressed_buffer;
80         size_t                  uncompressed_buffer_size;
81 #ifdef HAVE_ZLIB_H
82         z_stream                stream;
83         char                    stream_valid;
84 #endif
85
86         struct archive_string   pathname;
87         struct archive_string   extra;
88         char    format_name[64];
89 };
90
91 #define ZIP_LENGTH_AT_END       8
92
93 struct zip_file_header {
94         char    signature[4];
95         char    version[2];
96         char    flags[2];
97         char    compression[2];
98         char    timedate[4];
99         char    crc32[4];
100         char    compressed_size[4];
101         char    uncompressed_size[4];
102         char    filename_length[2];
103         char    extra_length[2];
104 };
105
106 static const char *compression_names[] = {
107         "uncompressed",
108         "shrinking",
109         "reduced-1",
110         "reduced-2",
111         "reduced-3",
112         "reduced-4",
113         "imploded",
114         "reserved",
115         "deflation"
116 };
117
118 static int      archive_read_format_zip_bid(struct archive_read *);
119 static int      archive_read_format_zip_cleanup(struct archive_read *);
120 static int      archive_read_format_zip_read_data(struct archive_read *,
121                     const void **, size_t *, off_t *);
122 static int      archive_read_format_zip_read_data_skip(struct archive_read *a);
123 static int      archive_read_format_zip_read_header(struct archive_read *,
124                     struct archive_entry *);
125 static int      zip_read_data_deflate(struct archive_read *a, const void **buff,
126                     size_t *size, off_t *offset);
127 static int      zip_read_data_none(struct archive_read *a, const void **buff,
128                     size_t *size, off_t *offset);
129 static int      zip_read_file_header(struct archive_read *a,
130                     struct archive_entry *entry, struct zip *zip);
131 static time_t   zip_time(const char *);
132 static void process_extra(const void* extra, struct zip* zip);
133
134 int
135 archive_read_support_format_zip(struct archive *_a)
136 {
137         struct archive_read *a = (struct archive_read *)_a;
138         struct zip *zip;
139         int r;
140
141         zip = (struct zip *)malloc(sizeof(*zip));
142         if (zip == NULL) {
143                 archive_set_error(&a->archive, ENOMEM, "Can't allocate zip data");
144                 return (ARCHIVE_FATAL);
145         }
146         memset(zip, 0, sizeof(*zip));
147
148         r = __archive_read_register_format(a,
149             zip,
150             archive_read_format_zip_bid,
151             archive_read_format_zip_read_header,
152             archive_read_format_zip_read_data,
153             archive_read_format_zip_read_data_skip,
154             archive_read_format_zip_cleanup);
155
156         if (r != ARCHIVE_OK)
157                 free(zip);
158         return (ARCHIVE_OK);
159 }
160
161
162 static int
163 archive_read_format_zip_bid(struct archive_read *a)
164 {
165         int bid = 0;
166         const char *p;
167
168         if (a->archive.archive_format == ARCHIVE_FORMAT_ZIP)
169                 bid += 1;
170
171         if ((p = __archive_read_ahead(a, 4)) == NULL)
172                 return (-1);
173
174         /*
175          * Bid of 30 here is: 16 bits for "PK",
176          * next 16-bit field has four options (-2 bits).
177          * 16 + 16-2 = 30.
178          */
179         if (p[0] == 'P' && p[1] == 'K') {
180                 if ((p[2] == '\001' && p[3] == '\002')
181                     || (p[2] == '\003' && p[3] == '\004')
182                     || (p[2] == '\005' && p[3] == '\006')
183                     || (p[2] == '\007' && p[3] == '\010')
184                     || (p[2] == '0' && p[3] == '0'))
185                         return (30);
186         }
187         return (0);
188 }
189
190 static int
191 archive_read_format_zip_read_header(struct archive_read *a,
192     struct archive_entry *entry)
193 {
194         const void *h;
195         const char *signature;
196         struct zip *zip;
197
198         a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
199         if (a->archive.archive_format_name == NULL)
200                 a->archive.archive_format_name = "ZIP";
201
202         zip = (struct zip *)(a->format->data);
203         zip->decompress_init = 0;
204         zip->end_of_entry = 0;
205         zip->end_of_entry_cleanup = 0;
206         zip->entry_uncompressed_bytes_read = 0;
207         zip->entry_compressed_bytes_read = 0;
208         if ((h = __archive_read_ahead(a, 4)) == NULL)
209                 return (ARCHIVE_FATAL);
210
211         signature = (const char *)h;
212         if (signature[0] != 'P' || signature[1] != 'K') {
213                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
214                     "Bad ZIP file");
215                 return (ARCHIVE_FATAL);
216         }
217
218         /*
219          * "PK00" signature is used for "split" archives that
220          * only have a single segment.  This means we can just
221          * skip the PK00; the first real file header should follow.
222          */
223         if (signature[2] == '0' && signature[3] == '0') {
224                 (a->decompressor->consume)(a, 4);
225                 if ((h = __archive_read_ahead(a, 4)) == NULL)
226                         return (ARCHIVE_FATAL);
227                 signature = (const char *)h;
228                 if (signature[0] != 'P' || signature[1] != 'K') {
229                         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
230                             "Bad ZIP file");
231                         return (ARCHIVE_FATAL);
232                 }
233         }
234
235         if (signature[2] == '\001' && signature[3] == '\002') {
236                 /* Beginning of central directory. */
237                 return (ARCHIVE_EOF);
238         }
239
240         if (signature[2] == '\003' && signature[3] == '\004') {
241                 /* Regular file entry. */
242                 return (zip_read_file_header(a, entry, zip));
243         }
244
245         if (signature[2] == '\005' && signature[3] == '\006') {
246                 /* End-of-archive record. */
247                 return (ARCHIVE_EOF);
248         }
249
250         if (signature[2] == '\007' && signature[3] == '\010') {
251                 /*
252                  * We should never encounter this record here;
253                  * see ZIP_LENGTH_AT_END handling below for details.
254                  */
255                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
256                     "Bad ZIP file: Unexpected end-of-entry record");
257                 return (ARCHIVE_FATAL);
258         }
259
260         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
261             "Damaged ZIP file or unsupported format variant (%d,%d)",
262             signature[2], signature[3]);
263         return (ARCHIVE_FATAL);
264 }
265
266 int
267 zip_read_file_header(struct archive_read *a, struct archive_entry *entry,
268     struct zip *zip)
269 {
270         const struct zip_file_header *p;
271         const void *h;
272
273         if ((p = __archive_read_ahead(a, sizeof *p)) == NULL) {
274                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
275                     "Truncated ZIP file header");
276                 return (ARCHIVE_FATAL);
277         }
278
279         zip->version = p->version[0];
280         zip->system = p->version[1];
281         zip->flags = archive_le16dec(p->flags);
282         zip->compression = archive_le16dec(p->compression);
283         if (zip->compression <
284             sizeof(compression_names)/sizeof(compression_names[0]))
285                 zip->compression_name = compression_names[zip->compression];
286         else
287                 zip->compression_name = "??";
288         zip->mtime = zip_time(p->timedate);
289         zip->ctime = 0;
290         zip->atime = 0;
291         zip->mode = 0;
292         zip->uid = 0;
293         zip->gid = 0;
294         zip->crc32 = archive_le32dec(p->crc32);
295         zip->filename_length = archive_le16dec(p->filename_length);
296         zip->extra_length = archive_le16dec(p->extra_length);
297         zip->uncompressed_size = archive_le32dec(p->uncompressed_size);
298         zip->compressed_size = archive_le32dec(p->compressed_size);
299
300         (a->decompressor->consume)(a, sizeof(struct zip_file_header));
301
302
303         /* Read the filename. */
304         if ((h = __archive_read_ahead(a, zip->filename_length)) == NULL) {
305                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
306                     "Truncated ZIP file header");
307                 return (ARCHIVE_FATAL);
308         }
309         if (archive_string_ensure(&zip->pathname, zip->filename_length) == NULL)
310                 __archive_errx(1, "Out of memory");
311         archive_strncpy(&zip->pathname, h, zip->filename_length);
312         (a->decompressor->consume)(a, zip->filename_length);
313         archive_entry_set_pathname(entry, zip->pathname.s);
314
315         if (zip->pathname.s[archive_strlen(&zip->pathname) - 1] == '/')
316                 zip->mode = AE_IFDIR | 0777;
317         else
318                 zip->mode = AE_IFREG | 0777;
319
320         /* Read the extra data. */
321         if ((h = __archive_read_ahead(a, zip->extra_length)) == NULL) {
322                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
323                     "Truncated ZIP file header");
324                 return (ARCHIVE_FATAL);
325         }
326         process_extra(h, zip);
327         (a->decompressor->consume)(a, zip->extra_length);
328
329         /* Populate some additional entry fields: */
330         archive_entry_set_mode(entry, zip->mode);
331         archive_entry_set_uid(entry, zip->uid);
332         archive_entry_set_gid(entry, zip->gid);
333         archive_entry_set_mtime(entry, zip->mtime, 0);
334         archive_entry_set_ctime(entry, zip->ctime, 0);
335         archive_entry_set_atime(entry, zip->atime, 0);
336         archive_entry_set_size(entry, zip->uncompressed_size);
337
338         zip->entry_bytes_remaining = zip->compressed_size;
339         zip->entry_offset = 0;
340
341         /* If there's no body, force read_data() to return EOF immediately. */
342         if (0 == (zip->flags & ZIP_LENGTH_AT_END)
343             && zip->entry_bytes_remaining < 1)
344                 zip->end_of_entry = 1;
345
346         /* Set up a more descriptive format name. */
347         sprintf(zip->format_name, "ZIP %d.%d (%s)",
348             zip->version / 10, zip->version % 10,
349             zip->compression_name);
350         a->archive.archive_format_name = zip->format_name;
351
352         return (ARCHIVE_OK);
353 }
354
355 /* Convert an MSDOS-style date/time into Unix-style time. */
356 static time_t
357 zip_time(const char *p)
358 {
359         int msTime, msDate;
360         struct tm ts;
361
362         msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
363         msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
364
365         memset(&ts, 0, sizeof(ts));
366         ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
367         ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
368         ts.tm_mday = msDate & 0x1f; /* Day of month. */
369         ts.tm_hour = (msTime >> 11) & 0x1f;
370         ts.tm_min = (msTime >> 5) & 0x3f;
371         ts.tm_sec = (msTime << 1) & 0x3e;
372         ts.tm_isdst = -1;
373         return mktime(&ts);
374 }
375
376 static int
377 archive_read_format_zip_read_data(struct archive_read *a,
378     const void **buff, size_t *size, off_t *offset)
379 {
380         int r;
381         struct zip *zip;
382
383         zip = (struct zip *)(a->format->data);
384
385         /*
386          * If we hit end-of-entry last time, clean up and return
387          * ARCHIVE_EOF this time.
388          */
389         if (zip->end_of_entry) {
390                 if (!zip->end_of_entry_cleanup) {
391                         if (zip->flags & ZIP_LENGTH_AT_END) {
392                                 const char *p;
393
394                                 if ((p = __archive_read_ahead(a, 16)) == NULL) {
395                                         archive_set_error(&a->archive,
396                                             ARCHIVE_ERRNO_FILE_FORMAT,
397                                             "Truncated ZIP end-of-file record");
398                                         return (ARCHIVE_FATAL);
399                                 }
400                                 zip->crc32 = archive_le32dec(p + 4);
401                                 zip->compressed_size = archive_le32dec(p + 8);
402                                 zip->uncompressed_size = archive_le32dec(p + 12);
403                                 (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         off_t bytes_skipped;
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         bytes_skipped = (a->decompressor->skip)(a, zip->entry_bytes_remaining);
663         if (bytes_skipped < 0)
664                 return (ARCHIVE_FATAL);
665
666         /* This entry is finished and done. */
667         zip->end_of_entry_cleanup = zip->end_of_entry = 1;
668         return (ARCHIVE_OK);
669 }
670
671 static int
672 archive_read_format_zip_cleanup(struct archive_read *a)
673 {
674         struct zip *zip;
675
676         zip = (struct zip *)(a->format->data);
677 #ifdef HAVE_ZLIB_H
678         if (zip->stream_valid)
679                 inflateEnd(&zip->stream);
680 #endif
681         free(zip->uncompressed_buffer);
682         archive_string_free(&(zip->pathname));
683         archive_string_free(&(zip->extra));
684         free(zip);
685         (a->format->data) = NULL;
686         return (ARCHIVE_OK);
687 }
688
689 /*
690  * The extra data is stored as a list of
691  *      id1+size1+data1 + id2+size2+data2 ...
692  *  triplets.  id and size are 2 bytes each.
693  */
694 static void
695 process_extra(const void* extra, struct zip* zip)
696 {
697         int offset = 0;
698         const char *p = (const char *)extra;
699         while (offset < zip->extra_length - 4)
700         {
701                 unsigned short headerid = archive_le16dec(p + offset);
702                 unsigned short datasize = archive_le16dec(p + offset + 2);
703                 offset += 4;
704                 if (offset + datasize > zip->extra_length)
705                         break;
706 #ifdef DEBUG
707                 fprintf(stderr, "Header id 0x%04x, length %d\n",
708                     headerid, datasize);
709 #endif
710                 switch (headerid) {
711                 case 0x0001:
712                         /* Zip64 extended information extra field. */
713                         if (datasize >= 8)
714                                 zip->uncompressed_size = archive_le64dec(p + offset);
715                         if (datasize >= 16)
716                                 zip->compressed_size = archive_le64dec(p + offset + 8);
717                         break;
718                 case 0x5455:
719                 {
720                         /* Extended time field "UT". */
721                         int flags = p[offset];
722                         offset++;
723                         datasize--;
724                         /* Flag bits indicate which dates are present. */
725                         if (flags & 0x01)
726                         {
727 #ifdef DEBUG
728                                 fprintf(stderr, "mtime: %lld -> %d\n",
729                                     (long long)zip->mtime,
730                                     archive_le32dec(p + offset));
731 #endif
732                                 if (datasize < 4)
733                                         break;
734                                 zip->mtime = archive_le32dec(p + offset);
735                                 offset += 4;
736                                 datasize -= 4;
737                         }
738                         if (flags & 0x02)
739                         {
740                                 if (datasize < 4)
741                                         break;
742                                 zip->atime = archive_le32dec(p + offset);
743                                 offset += 4;
744                                 datasize -= 4;
745                         }
746                         if (flags & 0x04)
747                         {
748                                 if (datasize < 4)
749                                         break;
750                                 zip->ctime = archive_le32dec(p + offset);
751                                 offset += 4;
752                                 datasize -= 4;
753                         }
754                         break;
755                 }
756                 case 0x7855:
757                         /* Info-ZIP Unix Extra Field (type 2) "Ux". */
758 #ifdef DEBUG
759                         fprintf(stderr, "uid %d gid %d\n",
760                             archive_le16dec(p + offset),
761                             archive_le16dec(p + offset + 2));
762 #endif
763                         if (datasize >= 2)
764                                 zip->uid = archive_le16dec(p + offset);
765                         if (datasize >= 4)
766                                 zip->gid = archive_le16dec(p + offset + 2);
767                         break;
768                 default:
769                         break;
770                 }
771                 offset += datasize;
772         }
773 #ifdef DEBUG
774         if (offset != zip->extra_length)
775         {
776                 fprintf(stderr,
777                     "Extra data field contents do not match reported size!");
778         }
779 #endif
780 }