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