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->bid = gzip_bidder_bid;
104 bidder->init = gzip_bidder_init;
105 bidder->options = NULL;
106 bidder->free = NULL; /* No data, so no cleanup necessary. */
107 /* Signal the extent of gzip support with the return value here. */
111 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
112 "Using external gunzip program");
113 return (ARCHIVE_WARN);
118 * Read and verify the header.
120 * Returns zero if the header couldn't be validated, else returns
121 * number of bytes in header. If pbits is non-NULL, it receives a
122 * count of bits verified, suitable for use by bidder.
125 peek_at_header(struct archive_read_filter *filter, int *pbits)
127 const unsigned char *p;
132 /* Start by looking at the first ten bytes of the header, which
133 * is all fixed layout. */
135 p = __archive_read_filter_ahead(filter, len, &avail);
136 if (p == NULL || avail == 0)
138 /* We only support deflation- third byte must be 0x08. */
139 if (memcmp(p, "\x1F\x8B\x08", 3) != 0)
142 if ((p[3] & 0xE0)!= 0) /* No reserved flags set. */
146 /* Bytes 4-7 are mod time. */
147 /* Byte 8 is deflate flags. */
148 /* XXXX TODO: return deflate flags back to consume_header for use
149 in initializing the decompressor. */
152 /* Optional extra data: 2 byte length plus variable body. */
153 if (header_flags & 4) {
154 p = __archive_read_filter_ahead(filter, len + 2, &avail);
157 len += ((int)p[len + 1] << 8) | (int)p[len];
161 /* Null-terminated optional filename. */
162 if (header_flags & 8) {
166 p = __archive_read_filter_ahead(filter,
170 } while (p[len - 1] != 0);
173 /* Null-terminated optional comment. */
174 if (header_flags & 16) {
178 p = __archive_read_filter_ahead(filter,
182 } while (p[len - 1] != 0);
185 /* Optional header CRC */
186 if ((header_flags & 2)) {
187 p = __archive_read_filter_ahead(filter, len + 2, &avail);
191 int hcrc = ((int)p[len + 1] << 8) | (int)p[len];
192 int crc = /* XXX TODO: Compute header CRC. */;
206 * Bidder just verifies the header and returns the number of verified bits.
209 gzip_bidder_bid(struct archive_read_filter_bidder *self,
210 struct archive_read_filter *filter)
214 (void)self; /* UNUSED */
216 if (peek_at_header(filter, &bits_checked))
217 return (bits_checked);
225 * If we don't have the library on this system, we can't do the
226 * decompression directly. We can, however, try to run gunzip
227 * in case that's available.
230 gzip_bidder_init(struct archive_read_filter *self)
234 r = __archive_read_program(self, "gunzip");
235 /* Note: We set the format here even if __archive_read_program()
236 * above fails. We do, after all, know what the format is
237 * even if we weren't able to read it. */
238 self->code = ARCHIVE_COMPRESSION_GZIP;
246 * Initialize the filter object.
249 gzip_bidder_init(struct archive_read_filter *self)
251 struct private_data *state;
252 static const size_t out_block_size = 64 * 1024;
255 self->code = ARCHIVE_COMPRESSION_GZIP;
258 state = (struct private_data *)calloc(sizeof(*state), 1);
259 out_block = (unsigned char *)malloc(out_block_size);
260 if (state == NULL || out_block == NULL) {
263 archive_set_error(&self->archive->archive, ENOMEM,
264 "Can't allocate data for gzip decompression");
265 return (ARCHIVE_FATAL);
269 state->out_block_size = out_block_size;
270 state->out_block = out_block;
271 self->read = gzip_filter_read;
272 self->skip = NULL; /* not supported */
273 self->close = gzip_filter_close;
275 state->in_stream = 0; /* We're not actually within a stream yet. */
281 consume_header(struct archive_read_filter *self)
283 struct private_data *state;
288 state = (struct private_data *)self->data;
290 /* If this is a real header, consume it. */
291 len = peek_at_header(self->upstream, NULL);
293 return (ARCHIVE_EOF);
294 __archive_read_filter_consume(self->upstream, len);
296 /* Initialize CRC accumulator. */
297 state->crc = crc32(0L, NULL, 0);
299 /* Initialize compression library. */
300 state->stream.next_in = (unsigned char *)(uintptr_t)
301 __archive_read_filter_ahead(self->upstream, 1, &avail);
302 state->stream.avail_in = avail;
303 ret = inflateInit2(&(state->stream),
304 -15 /* Don't check for zlib header */);
306 /* Decipher the error code. */
309 state->in_stream = 1;
312 archive_set_error(&self->archive->archive,
314 "Internal error initializing compression library: "
315 "invalid setup parameter");
318 archive_set_error(&self->archive->archive, ENOMEM,
319 "Internal error initializing compression library: "
322 case Z_VERSION_ERROR:
323 archive_set_error(&self->archive->archive,
325 "Internal error initializing compression library: "
326 "invalid library version");
329 archive_set_error(&self->archive->archive,
331 "Internal error initializing compression library: "
332 " Zlib error %d", ret);
335 return (ARCHIVE_FATAL);
339 consume_trailer(struct archive_read_filter *self)
341 struct private_data *state;
342 const unsigned char *p;
345 state = (struct private_data *)self->data;
347 state->in_stream = 0;
348 switch (inflateEnd(&(state->stream))) {
352 archive_set_error(&self->archive->archive,
354 "Failed to clean up gzip decompressor");
355 return (ARCHIVE_FATAL);
358 /* GZip trailer is a fixed 8 byte structure. */
359 p = __archive_read_filter_ahead(self->upstream, 8, &avail);
360 if (p == NULL || avail == 0)
361 return (ARCHIVE_FATAL);
363 /* XXX TODO: Verify the length and CRC. */
365 /* We've verified the trailer, so consume it now. */
366 __archive_read_filter_consume(self->upstream, 8);
372 gzip_filter_read(struct archive_read_filter *self, const void **p)
374 struct private_data *state;
379 state = (struct private_data *)self->data;
381 /* Empty our output buffer. */
382 state->stream.next_out = state->out_block;
383 state->stream.avail_out = state->out_block_size;
385 /* Try to fill the output buffer. */
386 while (state->stream.avail_out > 0 && !state->eof) {
387 /* If we're not in a stream, read a header
388 * and initialize the decompression library. */
389 if (!state->in_stream) {
390 ret = consume_header(self);
391 if (ret == ARCHIVE_EOF) {
395 if (ret < ARCHIVE_OK)
399 /* Peek at the next available data. */
400 /* ZLib treats stream.next_in as const but doesn't declare
401 * it so, hence this ugly cast. */
402 state->stream.next_in = (unsigned char *)(uintptr_t)
403 __archive_read_filter_ahead(self->upstream, 1, &avail_in);
404 if (state->stream.next_in == NULL) {
405 archive_set_error(&self->archive->archive,
407 "truncated gzip input");
408 return (ARCHIVE_FATAL);
410 state->stream.avail_in = avail_in;
412 /* Decompress and consume some of that data. */
413 ret = inflate(&(state->stream), 0);
415 case Z_OK: /* Decompressor made some progress. */
416 __archive_read_filter_consume(self->upstream,
417 avail_in - state->stream.avail_in);
419 case Z_STREAM_END: /* Found end of stream. */
420 __archive_read_filter_consume(self->upstream,
421 avail_in - state->stream.avail_in);
422 /* Consume the stream trailer; release the
423 * decompression library. */
424 ret = consume_trailer(self);
425 if (ret < ARCHIVE_OK)
429 /* Return an error. */
430 archive_set_error(&self->archive->archive,
432 "gzip decompression failed");
433 return (ARCHIVE_FATAL);
437 /* We've read as much as we can. */
438 decompressed = state->stream.next_out - state->out_block;
439 state->total_out += decompressed;
440 if (decompressed == 0)
443 *p = state->out_block;
444 return (decompressed);
448 * Clean up the decompressor.
451 gzip_filter_close(struct archive_read_filter *self)
453 struct private_data *state;
456 state = (struct private_data *)self->data;
459 if (state->in_stream) {
460 switch (inflateEnd(&(state->stream))) {
464 archive_set_error(&(self->archive->archive),
466 "Failed to clean up gzip compressor");
471 free(state->out_block);
476 #endif /* HAVE_ZLIB_H */