2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
48 #include "archive_private.h"
49 #include "archive_read_private.h"
55 unsigned char *out_block;
56 size_t out_block_size;
59 char eof; /* True = found end of compressed data. */
63 static ssize_t gzip_filter_read(struct archive_read_filter *, const void **);
64 static int gzip_filter_close(struct archive_read_filter *);
68 * Note that we can detect gzip archives even if we can't decompress
69 * them. (In fact, we like detecting them because we can give better
70 * error messages.) So the bid framework here gets compiled even
71 * if zlib is unavailable.
73 * TODO: If zlib is unavailable, gzip_bidder_init() should
74 * use the compress_program framework to try to fire up an external
77 static int gzip_bidder_bid(struct archive_read_filter_bidder *,
78 struct archive_read_filter *);
79 static int gzip_bidder_init(struct archive_read_filter *);
81 #if ARCHIVE_VERSION_NUMBER < 4000000
82 /* Deprecated; remove in libarchive 4.0 */
84 archive_read_support_compression_gzip(struct archive *a)
86 return archive_read_support_filter_gzip(a);
91 archive_read_support_filter_gzip(struct archive *_a)
93 struct archive_read *a = (struct archive_read *)_a;
94 struct archive_read_filter_bidder *bidder;
96 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
97 ARCHIVE_STATE_NEW, "archive_read_support_filter_gzip");
99 if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
100 return (ARCHIVE_FATAL);
103 bidder->name = "gzip";
104 bidder->bid = gzip_bidder_bid;
105 bidder->init = gzip_bidder_init;
106 bidder->options = NULL;
107 bidder->free = NULL; /* No data, so no cleanup necessary. */
108 /* Signal the extent of gzip support with the return value here. */
112 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
113 "Using external gzip program");
114 return (ARCHIVE_WARN);
119 * Read and verify the header.
121 * Returns zero if the header couldn't be validated, else returns
122 * number of bytes in header. If pbits is non-NULL, it receives a
123 * count of bits verified, suitable for use by bidder.
126 peek_at_header(struct archive_read_filter *filter, int *pbits)
128 const unsigned char *p;
133 /* Start by looking at the first ten bytes of the header, which
134 * is all fixed layout. */
136 p = __archive_read_filter_ahead(filter, len, &avail);
137 if (p == NULL || avail == 0)
139 /* We only support deflation- third byte must be 0x08. */
140 if (memcmp(p, "\x1F\x8B\x08", 3) != 0)
143 if ((p[3] & 0xE0)!= 0) /* No reserved flags set. */
147 /* Bytes 4-7 are mod time. */
148 /* Byte 8 is deflate flags. */
149 /* XXXX TODO: return deflate flags back to consume_header for use
150 in initializing the decompressor. */
153 /* Optional extra data: 2 byte length plus variable body. */
154 if (header_flags & 4) {
155 p = __archive_read_filter_ahead(filter, len + 2, &avail);
158 len += ((int)p[len + 1] << 8) | (int)p[len];
162 /* Null-terminated optional filename. */
163 if (header_flags & 8) {
167 p = __archive_read_filter_ahead(filter,
171 } while (p[len - 1] != 0);
174 /* Null-terminated optional comment. */
175 if (header_flags & 16) {
179 p = __archive_read_filter_ahead(filter,
183 } while (p[len - 1] != 0);
186 /* Optional header CRC */
187 if ((header_flags & 2)) {
188 p = __archive_read_filter_ahead(filter, len + 2, &avail);
192 int hcrc = ((int)p[len + 1] << 8) | (int)p[len];
193 int crc = /* XXX TODO: Compute header CRC. */;
207 * Bidder just verifies the header and returns the number of verified bits.
210 gzip_bidder_bid(struct archive_read_filter_bidder *self,
211 struct archive_read_filter *filter)
215 (void)self; /* UNUSED */
217 if (peek_at_header(filter, &bits_checked))
218 return (bits_checked);
226 * If we don't have the library on this system, we can't do the
227 * decompression directly. We can, however, try to run "gzip -d"
228 * in case that's available.
231 gzip_bidder_init(struct archive_read_filter *self)
235 r = __archive_read_program(self, "gzip -d");
236 /* Note: We set the format here even if __archive_read_program()
237 * above fails. We do, after all, know what the format is
238 * even if we weren't able to read it. */
239 self->code = ARCHIVE_FILTER_GZIP;
247 * Initialize the filter object.
250 gzip_bidder_init(struct archive_read_filter *self)
252 struct private_data *state;
253 static const size_t out_block_size = 64 * 1024;
256 self->code = ARCHIVE_FILTER_GZIP;
259 state = (struct private_data *)calloc(sizeof(*state), 1);
260 out_block = (unsigned char *)malloc(out_block_size);
261 if (state == NULL || out_block == NULL) {
264 archive_set_error(&self->archive->archive, ENOMEM,
265 "Can't allocate data for gzip decompression");
266 return (ARCHIVE_FATAL);
270 state->out_block_size = out_block_size;
271 state->out_block = out_block;
272 self->read = gzip_filter_read;
273 self->skip = NULL; /* not supported */
274 self->close = gzip_filter_close;
276 state->in_stream = 0; /* We're not actually within a stream yet. */
282 consume_header(struct archive_read_filter *self)
284 struct private_data *state;
289 state = (struct private_data *)self->data;
291 /* If this is a real header, consume it. */
292 len = peek_at_header(self->upstream, NULL);
294 return (ARCHIVE_EOF);
295 __archive_read_filter_consume(self->upstream, len);
297 /* Initialize CRC accumulator. */
298 state->crc = crc32(0L, NULL, 0);
300 /* Initialize compression library. */
301 state->stream.next_in = (unsigned char *)(uintptr_t)
302 __archive_read_filter_ahead(self->upstream, 1, &avail);
303 state->stream.avail_in = (uInt)avail;
304 ret = inflateInit2(&(state->stream),
305 -15 /* Don't check for zlib header */);
307 /* Decipher the error code. */
310 state->in_stream = 1;
313 archive_set_error(&self->archive->archive,
315 "Internal error initializing compression library: "
316 "invalid setup parameter");
319 archive_set_error(&self->archive->archive, ENOMEM,
320 "Internal error initializing compression library: "
323 case Z_VERSION_ERROR:
324 archive_set_error(&self->archive->archive,
326 "Internal error initializing compression library: "
327 "invalid library version");
330 archive_set_error(&self->archive->archive,
332 "Internal error initializing compression library: "
333 " Zlib error %d", ret);
336 return (ARCHIVE_FATAL);
340 consume_trailer(struct archive_read_filter *self)
342 struct private_data *state;
343 const unsigned char *p;
346 state = (struct private_data *)self->data;
348 state->in_stream = 0;
349 switch (inflateEnd(&(state->stream))) {
353 archive_set_error(&self->archive->archive,
355 "Failed to clean up gzip decompressor");
356 return (ARCHIVE_FATAL);
359 /* GZip trailer is a fixed 8 byte structure. */
360 p = __archive_read_filter_ahead(self->upstream, 8, &avail);
361 if (p == NULL || avail == 0)
362 return (ARCHIVE_FATAL);
364 /* XXX TODO: Verify the length and CRC. */
366 /* We've verified the trailer, so consume it now. */
367 __archive_read_filter_consume(self->upstream, 8);
373 gzip_filter_read(struct archive_read_filter *self, const void **p)
375 struct private_data *state;
380 state = (struct private_data *)self->data;
382 /* Empty our output buffer. */
383 state->stream.next_out = state->out_block;
384 state->stream.avail_out = (uInt)state->out_block_size;
386 /* Try to fill the output buffer. */
387 while (state->stream.avail_out > 0 && !state->eof) {
388 /* If we're not in a stream, read a header
389 * and initialize the decompression library. */
390 if (!state->in_stream) {
391 ret = consume_header(self);
392 if (ret == ARCHIVE_EOF) {
396 if (ret < ARCHIVE_OK)
400 /* Peek at the next available data. */
401 /* ZLib treats stream.next_in as const but doesn't declare
402 * it so, hence this ugly cast. */
403 state->stream.next_in = (unsigned char *)(uintptr_t)
404 __archive_read_filter_ahead(self->upstream, 1, &avail_in);
405 if (state->stream.next_in == NULL) {
406 archive_set_error(&self->archive->archive,
408 "truncated gzip input");
409 return (ARCHIVE_FATAL);
411 state->stream.avail_in = (uInt)avail_in;
413 /* Decompress and consume some of that data. */
414 ret = inflate(&(state->stream), 0);
416 case Z_OK: /* Decompressor made some progress. */
417 __archive_read_filter_consume(self->upstream,
418 avail_in - state->stream.avail_in);
420 case Z_STREAM_END: /* Found end of stream. */
421 __archive_read_filter_consume(self->upstream,
422 avail_in - state->stream.avail_in);
423 /* Consume the stream trailer; release the
424 * decompression library. */
425 ret = consume_trailer(self);
426 if (ret < ARCHIVE_OK)
430 /* Return an error. */
431 archive_set_error(&self->archive->archive,
433 "gzip decompression failed");
434 return (ARCHIVE_FATAL);
438 /* We've read as much as we can. */
439 decompressed = state->stream.next_out - state->out_block;
440 state->total_out += decompressed;
441 if (decompressed == 0)
444 *p = state->out_block;
445 return (decompressed);
449 * Clean up the decompressor.
452 gzip_filter_close(struct archive_read_filter *self)
454 struct private_data *state;
457 state = (struct private_data *)self->data;
460 if (state->in_stream) {
461 switch (inflateEnd(&(state->stream))) {
465 archive_set_error(&(self->archive->archive),
467 "Failed to clean up gzip compressor");
472 free(state->out_block);
477 #endif /* HAVE_ZLIB_H */