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