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.14 2007/04/05 05:18:16 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         size_t read_avail, was_avail;
262         int ret;
263
264         state = (struct private_data *)a->compression_data;
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 (read_avail < min &&              /* Haven't satisfied min. */
284             read_avail < state->uncompressed_buffer_size) { /* !full */
285                 was_avail = read_avail;
286                 if ((ret = drive_decompressor(a, state)) != ARCHIVE_OK)
287                         return (ret);
288                 read_avail = state->stream.next_out - state->read_next;
289                 if (was_avail == read_avail) /* No progress? */
290                         break;
291         }
292
293         *p = state->read_next;
294         return (read_avail);
295 }
296
297 /*
298  * Mark a previously-returned block of data as read.
299  */
300 static ssize_t
301 read_consume(struct archive_read *a, size_t n)
302 {
303         struct private_data *state;
304
305         state = (struct private_data *)a->compression_data;
306         a->archive.file_position += n;
307         state->read_next += n;
308         if (state->read_next > state->stream.next_out)
309                 __archive_errx(1, "Request to consume too many "
310                     "bytes from gzip decompressor");
311         return (n);
312 }
313
314 /*
315  * Clean up the decompressor.
316  */
317 static int
318 finish(struct archive_read *a)
319 {
320         struct private_data *state;
321         int ret;
322
323         state = (struct private_data *)a->compression_data;
324         ret = ARCHIVE_OK;
325         switch (inflateEnd(&(state->stream))) {
326         case Z_OK:
327                 break;
328         default:
329                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
330                     "Failed to clean up %s compressor",
331                     a->archive.compression_name);
332                 ret = ARCHIVE_FATAL;
333         }
334
335         free(state->uncompressed_buffer);
336         free(state);
337
338         a->compression_data = NULL;
339         if (a->client_closer != NULL)
340                 (a->client_closer)(&a->archive, a->client_data);
341
342         return (ret);
343 }
344
345 /*
346  * Utility function to pull data through decompressor, reading input
347  * blocks as necessary.
348  */
349 static int
350 drive_decompressor(struct archive_read *a, struct private_data *state)
351 {
352         ssize_t ret;
353         size_t decompressed, total_decompressed;
354         int count, flags, header_state;
355         unsigned char *output;
356         unsigned char b;
357         const void *read_buf;
358
359         flags = 0;
360         count = 0;
361         header_state = 0;
362         total_decompressed = 0;
363         for (;;) {
364                 if (state->stream.avail_in == 0) {
365                         read_buf = state->stream.next_in;
366                         ret = (a->client_reader)(&a->archive, a->client_data,
367                             &read_buf);
368                         state->stream.next_in = (unsigned char *)(uintptr_t)read_buf;
369                         if (ret < 0) {
370                                 /*
371                                  * TODO: Find a better way to handle
372                                  * this read failure.
373                                  */
374                                 goto fatal;
375                         }
376                         if (ret == 0  &&  total_decompressed == 0) {
377                                 archive_set_error(&a->archive, EIO,
378                                     "Premature end of %s compressed data",
379                                     a->archive.compression_name);
380                                 return (ARCHIVE_FATAL);
381                         }
382                         a->archive.raw_position += ret;
383                         state->stream.avail_in = ret;
384                 }
385
386                 if (!state->header_done) {
387                         /*
388                          * If still parsing the header, interpret the
389                          * next byte.
390                          */
391                         b = *(state->stream.next_in++);
392                         state->stream.avail_in--;
393
394                         /*
395                          * Yes, this is somewhat crude, but it works,
396                          * GZip format isn't likely to change anytime
397                          * in the near future, and header parsing is
398                          * certainly not a performance issue, so
399                          * there's little point in making this more
400                          * elegant.  Of course, if you see an easy way
401                          * to make this more elegant, please let me
402                          * know.. ;-)
403                          */
404                         switch (header_state) {
405                         case 0: /* First byte of signature. */
406                                 if (b != 037)
407                                         goto fatal;
408                                 header_state = 1;
409                                 break;
410                         case 1: /* Second byte of signature. */
411                                 if (b != 0213)
412                                         goto fatal;
413                                 header_state = 2;
414                                 break;
415                         case 2: /* Compression type must be 8. */
416                                 if (b != 8)
417                                         goto fatal;
418                                 header_state = 3;
419                                 break;
420                         case 3: /* GZip flags. */
421                                 flags = b;
422                                 header_state = 4;
423                                 break;
424                         case 4: case 5: case 6: case 7: /* Mod time. */
425                                 header_state++;
426                                 break;
427                         case 8: /* Deflate flags. */
428                                 header_state = 9;
429                                 break;
430                         case 9: /* OS. */
431                                 header_state = 10;
432                                 break;
433                         case 10: /* Optional Extra: First byte of Length. */
434                                 if ((flags & 4)) {
435                                         count = 255 & (int)b;
436                                         header_state = 11;
437                                         break;
438                                 }
439                                 /*
440                                  * Fall through if there is no
441                                  * Optional Extra field.
442                                  */
443                         case 11: /* Optional Extra: Second byte of Length. */
444                                 if ((flags & 4)) {
445                                         count = (0xff00 & ((int)b << 8)) | count;
446                                         header_state = 12;
447                                         break;
448                                 }
449                                 /*
450                                  * Fall through if there is no
451                                  * Optional Extra field.
452                                  */
453                         case 12: /* Optional Extra Field: counted length. */
454                                 if ((flags & 4)) {
455                                         --count;
456                                         if (count == 0) header_state = 13;
457                                         else header_state = 12;
458                                         break;
459                                 }
460                                 /*
461                                  * Fall through if there is no
462                                  * Optional Extra field.
463                                  */
464                         case 13: /* Optional Original Filename. */
465                                 if ((flags & 8)) {
466                                         if (b == 0) header_state = 14;
467                                         else header_state = 13;
468                                         break;
469                                 }
470                                 /*
471                                  * Fall through if no Optional
472                                  * Original Filename.
473                                  */
474                         case 14: /* Optional Comment. */
475                                 if ((flags & 16)) {
476                                         if (b == 0) header_state = 15;
477                                         else header_state = 14;
478                                         break;
479                                 }
480                                 /* Fall through if no Optional Comment. */
481                         case 15: /* Optional Header CRC: First byte. */
482                                 if ((flags & 2)) {
483                                         header_state = 16;
484                                         break;
485                                 }
486                                 /* Fall through if no Optional Header CRC. */
487                         case 16: /* Optional Header CRC: Second byte. */
488                                 if ((flags & 2)) {
489                                         header_state = 17;
490                                         break;
491                                 }
492                                 /* Fall through if no Optional Header CRC. */
493                         case 17: /* First byte of compressed data. */
494                                 state->header_done = 1; /* done with header */
495                                 state->stream.avail_in++;
496                                 state->stream.next_in--;
497                         }
498
499                         /*
500                          * TODO: Consider moving the inflateInit2 call
501                          * here so it can include the compression type
502                          * from the header?
503                          */
504                 } else {
505                         output = state->stream.next_out;
506
507                         /* Decompress some data. */
508                         ret = inflate(&(state->stream), 0);
509                         decompressed = state->stream.next_out - output;
510
511                         /* Accumulate the CRC of the uncompressed data. */
512                         state->crc = crc32(state->crc, output, decompressed);
513
514                         /* Accumulate the total bytes of output. */
515                         state->total_out += decompressed;
516                         total_decompressed += decompressed;
517
518                         switch (ret) {
519                         case Z_OK: /* Decompressor made some progress. */
520                                 if (decompressed > 0)
521                                         return (ARCHIVE_OK);
522                                 break;
523                         case Z_STREAM_END: /* Found end of stream. */
524                                 /*
525                                  * TODO: Verify gzip trailer
526                                  * (uncompressed length and CRC).
527                                  */
528                                 return (ARCHIVE_OK);
529                         default:
530                                 /* Any other return value is an error. */
531                                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
532                                     "gzip decompression failed (%s)",
533                                     state->stream.msg);
534                                 goto fatal;
535                         }
536                 }
537         }
538         return (ARCHIVE_OK);
539
540         /* Return a fatal error. */
541 fatal:
542         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
543             "%s decompression failed", a->archive.compression_name);
544         return (ARCHIVE_FATAL);
545 }
546
547 #endif /* HAVE_ZLIB_H */