]> 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 r175256,
[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                         return (30);
185         }
186         return (0);
187 }
188
189 static int
190 archive_read_format_zip_read_header(struct archive_read *a,
191     struct archive_entry *entry)
192 {
193         const void *h;
194         const char *signature;
195         struct zip *zip;
196
197         a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
198         if (a->archive.archive_format_name == NULL)
199                 a->archive.archive_format_name = "ZIP";
200
201         zip = (struct zip *)(a->format->data);
202         zip->decompress_init = 0;
203         zip->end_of_entry = 0;
204         zip->end_of_entry_cleanup = 0;
205         zip->entry_uncompressed_bytes_read = 0;
206         zip->entry_compressed_bytes_read = 0;
207         if ((h = __archive_read_ahead(a, 4)) == NULL)
208                 return (ARCHIVE_FATAL);
209
210         signature = (const char *)h;
211         if (signature[0] != 'P' || signature[1] != 'K') {
212                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
213                     "Bad ZIP file");
214                 return (ARCHIVE_FATAL);
215         }
216
217         if (signature[2] == '\001' && signature[3] == '\002') {
218                 /* Beginning of central directory. */
219                 return (ARCHIVE_EOF);
220         }
221
222         if (signature[2] == '\003' && signature[3] == '\004') {
223                 /* Regular file entry. */
224                 return (zip_read_file_header(a, entry, zip));
225         }
226
227         if (signature[2] == '\005' && signature[3] == '\006') {
228                 /* End-of-archive record. */
229                 return (ARCHIVE_EOF);
230         }
231
232         if (signature[2] == '\007' && signature[3] == '\010') {
233                 /*
234                  * We should never encounter this record here;
235                  * see ZIP_LENGTH_AT_END handling below for details.
236                  */
237                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
238                     "Bad ZIP file: Unexpected end-of-entry record");
239                 return (ARCHIVE_FATAL);
240         }
241
242         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
243             "Damaged ZIP file or unsupported format variant (%d,%d)",
244             signature[2], signature[3]);
245         return (ARCHIVE_FATAL);
246 }
247
248 int
249 zip_read_file_header(struct archive_read *a, struct archive_entry *entry,
250     struct zip *zip)
251 {
252         const struct zip_file_header *p;
253         const void *h;
254
255         if ((p = __archive_read_ahead(a, sizeof *p)) == NULL) {
256                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
257                     "Truncated ZIP file header");
258                 return (ARCHIVE_FATAL);
259         }
260
261         zip->version = p->version[0];
262         zip->system = p->version[1];
263         zip->flags = le16dec(p->flags);
264         zip->compression = le16dec(p->compression);
265         if (zip->compression <
266             sizeof(compression_names)/sizeof(compression_names[0]))
267                 zip->compression_name = compression_names[zip->compression];
268         else
269                 zip->compression_name = "??";
270         zip->mtime = zip_time(p->timedate);
271         zip->ctime = 0;
272         zip->atime = 0;
273         zip->mode = 0;
274         zip->uid = 0;
275         zip->gid = 0;
276         zip->crc32 = le32dec(p->crc32);
277         zip->filename_length = le16dec(p->filename_length);
278         zip->extra_length = le16dec(p->extra_length);
279         zip->uncompressed_size = le32dec(p->uncompressed_size);
280         zip->compressed_size = le32dec(p->compressed_size);
281
282         (a->decompressor->consume)(a, sizeof(struct zip_file_header));
283
284
285         /* Read the filename. */
286         if ((h = __archive_read_ahead(a, zip->filename_length)) == NULL) {
287                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
288                     "Truncated ZIP file header");
289                 return (ARCHIVE_FATAL);
290         }
291         if (archive_string_ensure(&zip->pathname, zip->filename_length) == NULL)
292                 __archive_errx(1, "Out of memory");
293         archive_strncpy(&zip->pathname, h, zip->filename_length);
294         (a->decompressor->consume)(a, zip->filename_length);
295         archive_entry_set_pathname(entry, zip->pathname.s);
296
297         if (zip->pathname.s[archive_strlen(&zip->pathname) - 1] == '/')
298                 zip->mode = AE_IFDIR | 0777;
299         else
300                 zip->mode = AE_IFREG | 0777;
301
302         /* Read the extra data. */
303         if ((h = __archive_read_ahead(a, zip->extra_length)) == NULL) {
304                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
305                     "Truncated ZIP file header");
306                 return (ARCHIVE_FATAL);
307         }
308         process_extra(h, zip);
309         (a->decompressor->consume)(a, zip->extra_length);
310
311         /* Populate some additional entry fields: */
312         archive_entry_set_mode(entry, zip->mode);
313         archive_entry_set_uid(entry, zip->uid);
314         archive_entry_set_gid(entry, zip->gid);
315         archive_entry_set_mtime(entry, zip->mtime, 0);
316         archive_entry_set_ctime(entry, zip->ctime, 0);
317         archive_entry_set_atime(entry, zip->atime, 0);
318         archive_entry_set_size(entry, zip->uncompressed_size);
319
320         zip->entry_bytes_remaining = zip->compressed_size;
321         zip->entry_offset = 0;
322
323         /* If there's no body, force read_data() to return EOF immediately. */
324         if (0 == (zip->flags & ZIP_LENGTH_AT_END)
325             && zip->entry_bytes_remaining < 1)
326                 zip->end_of_entry = 1;
327
328         /* Set up a more descriptive format name. */
329         sprintf(zip->format_name, "ZIP %d.%d (%s)",
330             zip->version / 10, zip->version % 10,
331             zip->compression_name);
332         a->archive.archive_format_name = zip->format_name;
333
334         return (ARCHIVE_OK);
335 }
336
337 /* Convert an MSDOS-style date/time into Unix-style time. */
338 static time_t
339 zip_time(const char *p)
340 {
341         int msTime, msDate;
342         struct tm ts;
343
344         msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
345         msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
346
347         memset(&ts, 0, sizeof(ts));
348         ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
349         ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
350         ts.tm_mday = msDate & 0x1f; /* Day of month. */
351         ts.tm_hour = (msTime >> 11) & 0x1f;
352         ts.tm_min = (msTime >> 5) & 0x3f;
353         ts.tm_sec = (msTime << 1) & 0x3e;
354         ts.tm_isdst = -1;
355         return mktime(&ts);
356 }
357
358 static int
359 archive_read_format_zip_read_data(struct archive_read *a,
360     const void **buff, size_t *size, off_t *offset)
361 {
362         int r;
363         struct zip *zip;
364
365         zip = (struct zip *)(a->format->data);
366
367         /*
368          * If we hit end-of-entry last time, clean up and return
369          * ARCHIVE_EOF this time.
370          */
371         if (zip->end_of_entry) {
372                 if (!zip->end_of_entry_cleanup) {
373                         if (zip->flags & ZIP_LENGTH_AT_END) {
374                                 const char *p;
375
376                                 if ((p = __archive_read_ahead(a, 16)) == NULL) {
377                                         archive_set_error(&a->archive,
378                                             ARCHIVE_ERRNO_FILE_FORMAT,
379                                             "Truncated ZIP end-of-file record");
380                                         return (ARCHIVE_FATAL);
381                                 }
382                                 zip->crc32 = le32dec(p + 4);
383                                 zip->compressed_size = le32dec(p + 8);
384                                 zip->uncompressed_size = le32dec(p + 12);
385                                 (a->decompressor->consume)(a, 16);
386                         }
387
388                         /* Check file size, CRC against these values. */
389                         if (zip->compressed_size != zip->entry_compressed_bytes_read) {
390                                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
391                                     "ZIP compressed data is wrong size");
392                                 return (ARCHIVE_WARN);
393                         }
394                         /* Size field only stores the lower 32 bits of the actual size. */
395                         if ((zip->uncompressed_size & UINT32_MAX)
396                             != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
397                                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
398                                     "ZIP uncompressed data is wrong size");
399                                 return (ARCHIVE_WARN);
400                         }
401 /* TODO: Compute CRC. */
402 /*
403                         if (zip->crc32 != zip->entry_crc32_calculated) {
404                                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
405                                     "ZIP data CRC error");
406                                 return (ARCHIVE_WARN);
407                         }
408 */
409                         /* End-of-entry cleanup done. */
410                         zip->end_of_entry_cleanup = 1;
411                 }
412                 *offset = zip->entry_uncompressed_bytes_read;
413                 *size = 0;
414                 *buff = NULL;
415                 return (ARCHIVE_EOF);
416         }
417
418         switch(zip->compression) {
419         case 0:  /* No compression. */
420                 r =  zip_read_data_none(a, buff, size, offset);
421                 break;
422         case 8: /* Deflate compression. */
423                 r =  zip_read_data_deflate(a, buff, size, offset);
424                 break;
425         default: /* Unsupported compression. */
426                 *buff = NULL;
427                 *size = 0;
428                 *offset = 0;
429                 /* Return a warning. */
430                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
431                     "Unsupported ZIP compression method (%s)",
432                     zip->compression_name);
433                 if (zip->flags & ZIP_LENGTH_AT_END) {
434                         /*
435                          * ZIP_LENGTH_AT_END requires us to
436                          * decompress the entry in order to
437                          * skip it, but we don't know this
438                          * compression method, so we give up.
439                          */
440                         r = ARCHIVE_FATAL;
441                 } else {
442                         /* We know compressed size; just skip it. */
443                         archive_read_format_zip_read_data_skip(a);
444                         r = ARCHIVE_WARN;
445                 }
446                 break;
447         }
448         return (r);
449 }
450
451 /*
452  * Read "uncompressed" data.  According to the current specification,
453  * if ZIP_LENGTH_AT_END is specified, then the size fields in the
454  * initial file header are supposed to be set to zero.  This would, of
455  * course, make it impossible for us to read the archive, since we
456  * couldn't determine the end of the file data.  Info-ZIP seems to
457  * include the real size fields both before and after the data in this
458  * case (the CRC only appears afterwards), so this works as you would
459  * expect.
460  *
461  * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
462  * zip->end_of_entry if it consumes all of the data.
463  */
464 static int
465 zip_read_data_none(struct archive_read *a, const void **buff,
466     size_t *size, off_t *offset)
467 {
468         struct zip *zip;
469         ssize_t bytes_avail;
470
471         zip = (struct zip *)(a->format->data);
472
473         if (zip->entry_bytes_remaining == 0) {
474                 *buff = NULL;
475                 *size = 0;
476                 *offset = zip->entry_offset;
477                 zip->end_of_entry = 1;
478                 return (ARCHIVE_OK);
479         }
480         /*
481          * Note: '1' here is a performance optimization.
482          * Recall that the decompression layer returns a count of
483          * available bytes; asking for more than that forces the
484          * decompressor to combine reads by copying data.
485          */
486         bytes_avail = (a->decompressor->read_ahead)(a, buff, 1);
487         if (bytes_avail <= 0) {
488                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
489                     "Truncated ZIP file data");
490                 return (ARCHIVE_FATAL);
491         }
492         if (bytes_avail > zip->entry_bytes_remaining)
493                 bytes_avail = zip->entry_bytes_remaining;
494         (a->decompressor->consume)(a, bytes_avail);
495         *size = bytes_avail;
496         *offset = zip->entry_offset;
497         zip->entry_offset += *size;
498         zip->entry_bytes_remaining -= *size;
499         zip->entry_uncompressed_bytes_read += *size;
500         zip->entry_compressed_bytes_read += *size;
501         return (ARCHIVE_OK);
502 }
503
504 #ifdef HAVE_ZLIB_H
505 static int
506 zip_read_data_deflate(struct archive_read *a, const void **buff,
507     size_t *size, off_t *offset)
508 {
509         struct zip *zip;
510         ssize_t bytes_avail;
511         const void *compressed_buff;
512         int r;
513
514         zip = (struct zip *)(a->format->data);
515
516         /* If the buffer hasn't been allocated, allocate it now. */
517         if (zip->uncompressed_buffer == NULL) {
518                 zip->uncompressed_buffer_size = 32 * 1024;
519                 zip->uncompressed_buffer
520                     = (unsigned char *)malloc(zip->uncompressed_buffer_size);
521                 if (zip->uncompressed_buffer == NULL) {
522                         archive_set_error(&a->archive, ENOMEM,
523                             "No memory for ZIP decompression");
524                         return (ARCHIVE_FATAL);
525                 }
526         }
527
528         /* If we haven't yet read any data, initialize the decompressor. */
529         if (!zip->decompress_init) {
530                 if (zip->stream_valid)
531                         r = inflateReset(&zip->stream);
532                 else
533                         r = inflateInit2(&zip->stream,
534                             -15 /* Don't check for zlib header */);
535                 if (r != Z_OK) {
536                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
537                             "Can't initialize ZIP decompression.");
538                         return (ARCHIVE_FATAL);
539                 }
540                 /* Stream structure has been set up. */
541                 zip->stream_valid = 1;
542                 /* We've initialized decompression for this stream. */
543                 zip->decompress_init = 1;
544         }
545
546         /*
547          * Note: '1' here is a performance optimization.
548          * Recall that the decompression layer returns a count of
549          * available bytes; asking for more than that forces the
550          * decompressor to combine reads by copying data.
551          */
552         bytes_avail = (a->decompressor->read_ahead)(a, &compressed_buff, 1);
553         if (bytes_avail <= 0) {
554                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
555                     "Truncated ZIP file body");
556                 return (ARCHIVE_FATAL);
557         }
558
559         /*
560          * A bug in zlib.h: stream.next_in should be marked 'const'
561          * but isn't (the library never alters data through the
562          * next_in pointer, only reads it).  The result: this ugly
563          * cast to remove 'const'.
564          */
565         zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
566         zip->stream.avail_in = bytes_avail;
567         zip->stream.total_in = 0;
568         zip->stream.next_out = zip->uncompressed_buffer;
569         zip->stream.avail_out = zip->uncompressed_buffer_size;
570         zip->stream.total_out = 0;
571
572         r = inflate(&zip->stream, 0);
573         switch (r) {
574         case Z_OK:
575                 break;
576         case Z_STREAM_END:
577                 zip->end_of_entry = 1;
578                 break;
579         case Z_MEM_ERROR:
580                 archive_set_error(&a->archive, ENOMEM,
581                     "Out of memory for ZIP decompression");
582                 return (ARCHIVE_FATAL);
583         default:
584                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
585                     "ZIP decompression failed (%d)", r);
586                 return (ARCHIVE_FATAL);
587         }
588
589         /* Consume as much as the compressor actually used. */
590         bytes_avail = zip->stream.total_in;
591         (a->decompressor->consume)(a, bytes_avail);
592         zip->entry_bytes_remaining -= bytes_avail;
593         zip->entry_compressed_bytes_read += bytes_avail;
594
595         *offset = zip->entry_offset;
596         *size = zip->stream.total_out;
597         zip->entry_uncompressed_bytes_read += *size;
598         *buff = zip->uncompressed_buffer;
599         zip->entry_offset += *size;
600         return (ARCHIVE_OK);
601 }
602 #else
603 static int
604 zip_read_data_deflate(struct archive_read *a, const void **buff,
605     size_t *size, off_t *offset)
606 {
607         *buff = NULL;
608         *size = 0;
609         *offset = 0;
610         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
611             "libarchive compiled without deflate support (no libz)");
612         return (ARCHIVE_FATAL);
613 }
614 #endif
615
616 static int
617 archive_read_format_zip_read_data_skip(struct archive_read *a)
618 {
619         struct zip *zip;
620         const void *buff = NULL;
621         ssize_t bytes_avail;
622
623         zip = (struct zip *)(a->format->data);
624
625         /*
626          * If the length is at the end, we have no choice but
627          * to decompress all the data to find the end marker.
628          */
629         if (zip->flags & ZIP_LENGTH_AT_END) {
630                 size_t size;
631                 off_t offset;
632                 int r;
633                 do {
634                         r = archive_read_format_zip_read_data(a, &buff,
635                             &size, &offset);
636                 } while (r == ARCHIVE_OK);
637                 return (r);
638         }
639
640         /*
641          * If the length is at the beginning, we can skip the
642          * compressed data much more quickly.
643          */
644         while (zip->entry_bytes_remaining > 0) {
645                 bytes_avail = (a->decompressor->read_ahead)(a, &buff, 1);
646                 if (bytes_avail <= 0) {
647                         archive_set_error(&a->archive,
648                             ARCHIVE_ERRNO_FILE_FORMAT,
649                             "Truncated ZIP file body");
650                         return (ARCHIVE_FATAL);
651                 }
652                 if (bytes_avail > zip->entry_bytes_remaining)
653                         bytes_avail = zip->entry_bytes_remaining;
654                 (a->decompressor->consume)(a, bytes_avail);
655                 zip->entry_bytes_remaining -= bytes_avail;
656         }
657         /* This entry is finished and done. */
658         zip->end_of_entry_cleanup = zip->end_of_entry = 1;
659         return (ARCHIVE_OK);
660 }
661
662 static int
663 archive_read_format_zip_cleanup(struct archive_read *a)
664 {
665         struct zip *zip;
666
667         zip = (struct zip *)(a->format->data);
668 #ifdef HAVE_ZLIB_H
669         if (zip->stream_valid)
670                 inflateEnd(&zip->stream);
671 #endif
672         free(zip->uncompressed_buffer);
673         archive_string_free(&(zip->pathname));
674         archive_string_free(&(zip->extra));
675         free(zip);
676         (a->format->data) = NULL;
677         return (ARCHIVE_OK);
678 }
679
680 /*
681  * The extra data is stored as a list of
682  *      id1+size1+data1 + id2+size2+data2 ...
683  *  triplets.  id and size are 2 bytes each.
684  */
685 static void
686 process_extra(const void* extra, struct zip* zip)
687 {
688         int offset = 0;
689         const char *p = (const char *)extra;
690         while (offset < zip->extra_length - 4)
691         {
692                 unsigned short headerid = le16dec(p + offset);
693                 unsigned short datasize = le16dec(p + offset + 2);
694                 offset += 4;
695                 if (offset + datasize > zip->extra_length)
696                         break;
697 #ifdef DEBUG
698                 fprintf(stderr, "Header id 0x%04x, length %d\n",
699                     headerid, datasize);
700 #endif
701                 switch (headerid) {
702                 case 0x0001:
703                         /* Zip64 extended information extra field. */
704                         if (datasize >= 8)
705                                 zip->uncompressed_size = le64dec(p + offset);
706                         if (datasize >= 16)
707                                 zip->compressed_size = le64dec(p + offset + 8);
708                         break;
709                 case 0x5455:
710                 {
711                         /* Extended time field "UT". */
712                         int flags = p[offset];
713                         offset++;
714                         datasize--;
715                         /* Flag bits indicate which dates are present. */
716                         if (flags & 0x01)
717                         {
718 #ifdef DEBUG
719                                 fprintf(stderr, "mtime: %lld -> %d\n",
720                                     (long long)zip->mtime, le32dec(p + offset));
721 #endif
722                                 if (datasize < 4)
723                                         break;
724                                 zip->mtime = le32dec(p + offset);
725                                 offset += 4;
726                                 datasize -= 4;
727                         }
728                         if (flags & 0x02)
729                         {
730                                 if (datasize < 4)
731                                         break;
732                                 zip->atime = le32dec(p + offset);
733                                 offset += 4;
734                                 datasize -= 4;
735                         }
736                         if (flags & 0x04)
737                         {
738                                 if (datasize < 4)
739                                         break;
740                                 zip->ctime = le32dec(p + offset);
741                                 offset += 4;
742                                 datasize -= 4;
743                         }
744                         break;
745                 }
746                 case 0x7855:
747                         /* Info-ZIP Unix Extra Field (type 2) "Ux". */
748 #ifdef DEBUG
749                         fprintf(stderr, "uid %d gid %d\n",
750                             le16dec(p + offset), le16dec(p + offset + 2));
751 #endif
752                         if (datasize >= 2)
753                                 zip->uid = le16dec(p + offset);
754                         if (datasize >= 4)
755                                 zip->gid = le16dec(p + offset + 2);
756                         break;
757                 default:
758                         break;
759                 }
760                 offset += datasize;
761         }
762 #ifdef DEBUG
763         if (offset != zip->extra_length)
764         {
765                 fprintf(stderr,
766                     "Extra data field contents do not match reported size!");
767         }
768 #endif
769 }