Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / contrib / libarchive-2.0 / libarchive / archive_read_support_compression_gzip.c
1 /*-
2  * Copyright (c) 2003-2007 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
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_compression_gzip.c,v 1.13 2007/03/03 07:37:36 kientzle Exp $");
29
30
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 #ifdef HAVE_ZLIB_H
44 #include <zlib.h>
45 #endif
46
47 #include "archive.h"
48 #include "archive_private.h"
49 #include "archive_read_private.h"
50
51 #ifdef HAVE_ZLIB_H
52 struct private_data {
53         z_stream         stream;
54         unsigned char   *uncompressed_buffer;
55         size_t           uncompressed_buffer_size;
56         unsigned char   *read_next;
57         int64_t          total_out;
58         unsigned long    crc;
59         char             header_done;
60 };
61
62 static int      finish(struct archive_read *);
63 static ssize_t  read_ahead(struct archive_read *, const void **, size_t);
64 static ssize_t  read_consume(struct archive_read *, size_t);
65 static int      drive_decompressor(struct archive_read *a, struct private_data *);
66 #endif
67
68 /* These two functions are defined even if we lack zlib.  See below. */
69 static int      bid(const void *, size_t);
70 static int      init(struct archive_read *, const void *, size_t);
71
72 int
73 archive_read_support_compression_gzip(struct archive *_a)
74 {
75         struct archive_read *a = (struct archive_read *)_a;
76         return (__archive_read_register_compression(a, bid, init));
77 }
78
79 /*
80  * Test whether we can handle this data.
81  *
82  * This logic returns zero if any part of the signature fails.  It
83  * also tries to Do The Right Thing if a very short buffer prevents us
84  * from verifying as much as we would like.
85  */
86 static int
87 bid(const void *buff, size_t len)
88 {
89         const unsigned char *buffer;
90         int bits_checked;
91
92         if (len < 1)
93                 return (0);
94
95         buffer = (const unsigned char *)buff;
96         bits_checked = 0;
97         if (buffer[0] != 037)   /* Verify first ID byte. */
98                 return (0);
99         bits_checked += 8;
100         if (len < 2)
101                 return (bits_checked);
102
103         if (buffer[1] != 0213)  /* Verify second ID byte. */
104                 return (0);
105         bits_checked += 8;
106         if (len < 3)
107                 return (bits_checked);
108
109         if (buffer[2] != 8)     /* Compression must be 'deflate'. */
110                 return (0);
111         bits_checked += 8;
112         if (len < 4)
113                 return (bits_checked);
114
115         if ((buffer[3] & 0xE0)!= 0)     /* No reserved flags set. */
116                 return (0);
117         bits_checked += 3;
118         if (len < 5)
119                 return (bits_checked);
120
121         /*
122          * TODO: Verify more; in particular, gzip has an optional
123          * header CRC, which would give us 16 more verified bits.  We
124          * may also be able to verify certain constraints on other
125          * fields.
126          */
127
128         return (bits_checked);
129 }
130
131
132 #ifndef HAVE_ZLIB_H
133
134 /*
135  * If we don't have zlib on this system, we can't actually do the
136  * decompression.  We can, however, still detect gzip-compressed
137  * archives and emit a useful message.
138  */
139 static int
140 init(struct archive_read *a, const void *buff, size_t n)
141 {
142         (void)a;        /* UNUSED */
143         (void)buff;     /* UNUSED */
144         (void)n;        /* UNUSED */
145
146         archive_set_error(a, -1,
147             "This version of libarchive was compiled without gzip support");
148         return (ARCHIVE_FATAL);
149 }
150
151
152 #else
153
154 /*
155  * Setup the callbacks.
156  */
157 static int
158 init(struct archive_read *a, const void *buff, size_t n)
159 {
160         struct private_data *state;
161         int ret;
162
163         a->archive.compression_code = ARCHIVE_COMPRESSION_GZIP;
164         a->archive.compression_name = "gzip";
165
166         state = (struct private_data *)malloc(sizeof(*state));
167         if (state == NULL) {
168                 archive_set_error(&a->archive, ENOMEM,
169                     "Can't allocate data for %s decompression",
170                     a->archive.compression_name);
171                 return (ARCHIVE_FATAL);
172         }
173         memset(state, 0, sizeof(*state));
174
175         state->crc = crc32(0L, NULL, 0);
176         state->header_done = 0; /* We've not yet begun to parse header... */
177
178         state->uncompressed_buffer_size = 64 * 1024;
179         state->uncompressed_buffer = (unsigned char *)malloc(state->uncompressed_buffer_size);
180         state->stream.next_out = state->uncompressed_buffer;
181         state->read_next = state->uncompressed_buffer;
182         state->stream.avail_out = state->uncompressed_buffer_size;
183
184         if (state->uncompressed_buffer == NULL) {
185                 archive_set_error(&a->archive, ENOMEM,
186                     "Can't allocate %s decompression buffers",
187                     a->archive.compression_name);
188                 free(state);
189                 return (ARCHIVE_FATAL);
190         }
191
192         /*
193          * A bug in zlib.h: stream.next_in should be marked 'const'
194          * but isn't (the library never alters data through the
195          * next_in pointer, only reads it).  The result: this ugly
196          * cast to remove 'const'.
197          */
198         state->stream.next_in = (Bytef *)(uintptr_t)(const void *)buff;
199         state->stream.avail_in = n;
200
201         a->compression_read_ahead = read_ahead;
202         a->compression_read_consume = read_consume;
203         a->compression_skip = NULL; /* not supported */
204         a->compression_finish = finish;
205
206         /*
207          * TODO: Do I need to parse the gzip header before calling
208          * inflateInit2()?  In particular, one of the header bytes
209          * marks "best compression" or "fastest", which may be
210          * appropriate for setting the second parameter here.
211          * However, I think the only penalty for not setting it
212          * correctly is wasted memory.  If this is necessary, it
213          * should probably go into drive_decompressor() below.
214          */
215
216         /* Initialize compression library. */
217         ret = inflateInit2(&(state->stream),
218             -15 /* Don't check for zlib header */);
219         if (ret == Z_OK) {
220                 a->compression_data = state;
221                 return (ARCHIVE_OK);
222         }
223
224         /* Library setup failed: Clean up. */
225         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
226             "Internal error initializing %s library",
227             a->archive.compression_name);
228         free(state->uncompressed_buffer);
229         free(state);
230
231         /* Override the error message if we know what really went wrong. */
232         switch (ret) {
233         case Z_STREAM_ERROR:
234                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
235                     "Internal error initializing compression library: "
236                     "invalid setup parameter");
237                 break;
238         case Z_MEM_ERROR:
239                 archive_set_error(&a->archive, ENOMEM,
240                     "Internal error initializing compression library: "
241                     "out of memory");
242                 break;
243         case Z_VERSION_ERROR:
244                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
245                     "Internal error initializing compression library: "
246                     "invalid library version");
247                 break;
248         }
249
250         return (ARCHIVE_FATAL);
251 }
252
253 /*
254  * Return a block of data from the decompression buffer.  Decompress more
255  * as necessary.
256  */
257 static ssize_t
258 read_ahead(struct archive_read *a, const void **p, size_t min)
259 {
260         struct private_data *state;
261         int read_avail, was_avail, ret;
262
263         state = (struct private_data *)a->compression_data;
264         was_avail = -1;
265         if (!a->client_reader) {
266                 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
267                     "No read callback is registered?  "
268                     "This is probably an internal programming error.");
269                 return (ARCHIVE_FATAL);
270         }
271
272         read_avail = state->stream.next_out - state->read_next;
273
274         if (read_avail + state->stream.avail_out < min) {
275                 memmove(state->uncompressed_buffer, state->read_next,
276                     read_avail);
277                 state->read_next = state->uncompressed_buffer;
278                 state->stream.next_out = state->read_next + read_avail;
279                 state->stream.avail_out
280                     = state->uncompressed_buffer_size - read_avail;
281         }
282
283         while (was_avail < read_avail &&        /* Made some progress. */
284             read_avail < (int)min &&            /* Haven't satisfied min. */
285             read_avail < (int)state->uncompressed_buffer_size) { /* !full */
286                 if ((ret = drive_decompressor(a, state)) != ARCHIVE_OK)
287                         return (ret);
288                 was_avail = read_avail;
289                 read_avail = state->stream.next_out - state->read_next;
290         }
291
292         *p = state->read_next;
293         return (read_avail);
294 }
295
296 /*
297  * Mark a previously-returned block of data as read.
298  */
299 static ssize_t
300 read_consume(struct archive_read *a, size_t n)
301 {
302         struct private_data *state;
303
304         state = (struct private_data *)a->compression_data;
305         a->archive.file_position += n;
306         state->read_next += n;
307         if (state->read_next > state->stream.next_out)
308                 __archive_errx(1, "Request to consume too many "
309                     "bytes from gzip decompressor");
310         return (n);
311 }
312
313 /*
314  * Clean up the decompressor.
315  */
316 static int
317 finish(struct archive_read *a)
318 {
319         struct private_data *state;
320         int ret;
321
322         state = (struct private_data *)a->compression_data;
323         ret = ARCHIVE_OK;
324         switch (inflateEnd(&(state->stream))) {
325         case Z_OK:
326                 break;
327         default:
328                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
329                     "Failed to clean up %s compressor",
330                     a->archive.compression_name);
331                 ret = ARCHIVE_FATAL;
332         }
333
334         free(state->uncompressed_buffer);
335         free(state);
336
337         a->compression_data = NULL;
338         if (a->client_closer != NULL)
339                 (a->client_closer)(&a->archive, a->client_data);
340
341         return (ret);
342 }
343
344 /*
345  * Utility function to pull data through decompressor, reading input
346  * blocks as necessary.
347  */
348 static int
349 drive_decompressor(struct archive_read *a, struct private_data *state)
350 {
351         ssize_t ret;
352         int decompressed, total_decompressed;
353         int count, flags, header_state;
354         unsigned char *output;
355         unsigned char b;
356
357         flags = 0;
358         count = 0;
359         header_state = 0;
360         total_decompressed = 0;
361         for (;;) {
362                 if (state->stream.avail_in == 0) {
363                         ret = (a->client_reader)(&a->archive, a->client_data,
364                             (const void **)&state->stream.next_in);
365                         if (ret < 0) {
366                                 /*
367                                  * TODO: Find a better way to handle
368                                  * this read failure.
369                                  */
370                                 goto fatal;
371                         }
372                         if (ret == 0  &&  total_decompressed == 0) {
373                                 archive_set_error(&a->archive, EIO,
374                                     "Premature end of %s compressed data",
375                                     a->archive.compression_name);
376                                 return (ARCHIVE_FATAL);
377                         }
378                         a->archive.raw_position += ret;
379                         state->stream.avail_in = ret;
380                 }
381
382                 if (!state->header_done) {
383                         /*
384                          * If still parsing the header, interpret the
385                          * next byte.
386                          */
387                         b = *(state->stream.next_in++);
388                         state->stream.avail_in--;
389
390                         /*
391                          * Yes, this is somewhat crude, but it works,
392                          * GZip format isn't likely to change anytime
393                          * in the near future, and header parsing is
394                          * certainly not a performance issue, so
395                          * there's little point in making this more
396                          * elegant.  Of course, if you see an easy way
397                          * to make this more elegant, please let me
398                          * know.. ;-)
399                          */
400                         switch (header_state) {
401                         case 0: /* First byte of signature. */
402                                 if (b != 037)
403                                         goto fatal;
404                                 header_state = 1;
405                                 break;
406                         case 1: /* Second byte of signature. */
407                                 if (b != 0213)
408                                         goto fatal;
409                                 header_state = 2;
410                                 break;
411                         case 2: /* Compression type must be 8. */
412                                 if (b != 8)
413                                         goto fatal;
414                                 header_state = 3;
415                                 break;
416                         case 3: /* GZip flags. */
417                                 flags = b;
418                                 header_state = 4;
419                                 break;
420                         case 4: case 5: case 6: case 7: /* Mod time. */
421                                 header_state++;
422                                 break;
423                         case 8: /* Deflate flags. */
424                                 header_state = 9;
425                                 break;
426                         case 9: /* OS. */
427                                 header_state = 10;
428                                 break;
429                         case 10: /* Optional Extra: First byte of Length. */
430                                 if ((flags & 4)) {
431                                         count = 255 & (int)b;
432                                         header_state = 11;
433                                         break;
434                                 }
435                                 /*
436                                  * Fall through if there is no
437                                  * Optional Extra field.
438                                  */
439                         case 11: /* Optional Extra: Second byte of Length. */
440                                 if ((flags & 4)) {
441                                         count = (0xff00 & ((int)b << 8)) | count;
442                                         header_state = 12;
443                                         break;
444                                 }
445                                 /*
446                                  * Fall through if there is no
447                                  * Optional Extra field.
448                                  */
449                         case 12: /* Optional Extra Field: counted length. */
450                                 if ((flags & 4)) {
451                                         --count;
452                                         if (count == 0) header_state = 13;
453                                         else header_state = 12;
454                                         break;
455                                 }
456                                 /*
457                                  * Fall through if there is no
458                                  * Optional Extra field.
459                                  */
460                         case 13: /* Optional Original Filename. */
461                                 if ((flags & 8)) {
462                                         if (b == 0) header_state = 14;
463                                         else header_state = 13;
464                                         break;
465                                 }
466                                 /*
467                                  * Fall through if no Optional
468                                  * Original Filename.
469                                  */
470                         case 14: /* Optional Comment. */
471                                 if ((flags & 16)) {
472                                         if (b == 0) header_state = 15;
473                                         else header_state = 14;
474                                         break;
475                                 }
476                                 /* Fall through if no Optional Comment. */
477                         case 15: /* Optional Header CRC: First byte. */
478                                 if ((flags & 2)) {
479                                         header_state = 16;
480                                         break;
481                                 }
482                                 /* Fall through if no Optional Header CRC. */
483                         case 16: /* Optional Header CRC: Second byte. */
484                                 if ((flags & 2)) {
485                                         header_state = 17;
486                                         break;
487                                 }
488                                 /* Fall through if no Optional Header CRC. */
489                         case 17: /* First byte of compressed data. */
490                                 state->header_done = 1; /* done with header */
491                                 state->stream.avail_in++;
492                                 state->stream.next_in--;
493                         }
494
495                         /*
496                          * TODO: Consider moving the inflateInit2 call
497                          * here so it can include the compression type
498                          * from the header?
499                          */
500                 } else {
501                         output = state->stream.next_out;
502
503                         /* Decompress some data. */
504                         ret = inflate(&(state->stream), 0);
505                         decompressed = state->stream.next_out - output;
506
507                         /* Accumulate the CRC of the uncompressed data. */
508                         state->crc = crc32(state->crc, output, decompressed);
509
510                         /* Accumulate the total bytes of output. */
511                         state->total_out += decompressed;
512                         total_decompressed += decompressed;
513
514                         switch (ret) {
515                         case Z_OK: /* Decompressor made some progress. */
516                                 if (decompressed > 0)
517                                         return (ARCHIVE_OK);
518                                 break;
519                         case Z_STREAM_END: /* Found end of stream. */
520                                 /*
521                                  * TODO: Verify gzip trailer
522                                  * (uncompressed length and CRC).
523                                  */
524                                 return (ARCHIVE_OK);
525                         default:
526                                 /* Any other return value is an error. */
527                                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
528                                     "gzip decompression failed (%s)",
529                                     state->stream.msg);
530                                 goto fatal;
531                         }
532                 }
533         }
534         return (ARCHIVE_OK);
535
536         /* Return a fatal error. */
537 fatal:
538         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
539             "%s decompression failed", a->archive.compression_name);
540         return (ARCHIVE_FATAL);
541 }
542
543 #endif /* HAVE_ZLIB_H */