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$");
51 #include "archive_entry.h"
52 #include "archive_endian.h"
53 #include "archive_private.h"
54 #include "archive_read_private.h"
60 unsigned char *out_block;
61 size_t out_block_size;
66 char eof; /* True = found end of compressed data. */
70 static ssize_t gzip_filter_read(struct archive_read_filter *, const void **);
71 static int gzip_filter_close(struct archive_read_filter *);
75 * Note that we can detect gzip archives even if we can't decompress
76 * them. (In fact, we like detecting them because we can give better
77 * error messages.) So the bid framework here gets compiled even
78 * if zlib is unavailable.
80 * TODO: If zlib is unavailable, gzip_bidder_init() should
81 * use the compress_program framework to try to fire up an external
84 static int gzip_bidder_bid(struct archive_read_filter_bidder *,
85 struct archive_read_filter *);
86 static int gzip_bidder_init(struct archive_read_filter *);
88 #if ARCHIVE_VERSION_NUMBER < 4000000
89 /* Deprecated; remove in libarchive 4.0 */
91 archive_read_support_compression_gzip(struct archive *a)
93 return archive_read_support_filter_gzip(a);
97 static const struct archive_read_filter_bidder_vtable
98 gzip_bidder_vtable = {
99 .bid = gzip_bidder_bid,
100 .init = gzip_bidder_init,
104 archive_read_support_filter_gzip(struct archive *_a)
106 struct archive_read *a = (struct archive_read *)_a;
108 if (__archive_read_register_bidder(a, NULL, "gzip",
109 &gzip_bidder_vtable) != ARCHIVE_OK)
110 return (ARCHIVE_FATAL);
112 /* Signal the extent of gzip support with the return value here. */
116 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
117 "Using external gzip program");
118 return (ARCHIVE_WARN);
123 * Read and verify the header.
125 * Returns zero if the header couldn't be validated, else returns
126 * number of bytes in header. If pbits is non-NULL, it receives a
127 * count of bits verified, suitable for use by bidder.
130 peek_at_header(struct archive_read_filter *filter, int *pbits,
132 struct private_data *state
138 const unsigned char *p;
143 (void)state; /* UNUSED */
146 /* Start by looking at the first ten bytes of the header, which
147 * is all fixed layout. */
149 p = __archive_read_filter_ahead(filter, len, &avail);
150 if (p == NULL || avail == 0)
152 /* We only support deflation- third byte must be 0x08. */
153 if (memcmp(p, "\x1F\x8B\x08", 3) != 0)
156 if ((p[3] & 0xE0)!= 0) /* No reserved flags set. */
160 /* Bytes 4-7 are mod time in little endian. */
163 state->mtime = archive_le32dec(p + 4);
165 /* Byte 8 is deflate flags. */
166 /* XXXX TODO: return deflate flags back to consume_header for use
167 in initializing the decompressor. */
170 /* Optional extra data: 2 byte length plus variable body. */
171 if (header_flags & 4) {
172 p = __archive_read_filter_ahead(filter, len + 2, &avail);
175 len += ((int)p[len + 1] << 8) | (int)p[len];
179 /* Null-terminated optional filename. */
180 if (header_flags & 8) {
182 ssize_t file_start = len;
187 p = __archive_read_filter_ahead(filter,
191 } while (p[len - 1] != 0);
195 /* Reset the name in case of repeat header reads. */
197 state->name = strdup((const char *)&p[file_start]);
202 /* Null-terminated optional comment. */
203 if (header_flags & 16) {
207 p = __archive_read_filter_ahead(filter,
211 } while (p[len - 1] != 0);
214 /* Optional header CRC */
215 if ((header_flags & 2)) {
216 p = __archive_read_filter_ahead(filter, len + 2, &avail);
220 int hcrc = ((int)p[len + 1] << 8) | (int)p[len];
221 int crc = /* XXX TODO: Compute header CRC. */;
235 * Bidder just verifies the header and returns the number of verified bits.
238 gzip_bidder_bid(struct archive_read_filter_bidder *self,
239 struct archive_read_filter *filter)
243 (void)self; /* UNUSED */
245 if (peek_at_header(filter, &bits_checked, NULL))
246 return (bits_checked);
253 * If we don't have the library on this system, we can't do the
254 * decompression directly. We can, however, try to run "gzip -d"
255 * in case that's available.
258 gzip_bidder_init(struct archive_read_filter *self)
262 r = __archive_read_program(self, "gzip -d");
263 /* Note: We set the format here even if __archive_read_program()
264 * above fails. We do, after all, know what the format is
265 * even if we weren't able to read it. */
266 self->code = ARCHIVE_FILTER_GZIP;
274 gzip_read_header(struct archive_read_filter *self, struct archive_entry *entry)
276 struct private_data *state;
278 state = (struct private_data *)self->data;
280 /* A mtime of 0 is considered invalid/missing. */
281 if (state->mtime != 0)
282 archive_entry_set_mtime(entry, state->mtime, 0);
284 /* If the name is available, extract it. */
286 archive_entry_set_pathname(entry, state->name);
291 static const struct archive_read_filter_vtable
292 gzip_reader_vtable = {
293 .read = gzip_filter_read,
294 .close = gzip_filter_close,
296 .read_header = gzip_read_header,
301 * Initialize the filter object.
304 gzip_bidder_init(struct archive_read_filter *self)
306 struct private_data *state;
307 static const size_t out_block_size = 64 * 1024;
310 self->code = ARCHIVE_FILTER_GZIP;
313 state = (struct private_data *)calloc(sizeof(*state), 1);
314 out_block = (unsigned char *)malloc(out_block_size);
315 if (state == NULL || out_block == NULL) {
318 archive_set_error(&self->archive->archive, ENOMEM,
319 "Can't allocate data for gzip decompression");
320 return (ARCHIVE_FATAL);
324 state->out_block_size = out_block_size;
325 state->out_block = out_block;
326 self->vtable = &gzip_reader_vtable;
328 state->in_stream = 0; /* We're not actually within a stream yet. */
334 consume_header(struct archive_read_filter *self)
336 struct private_data *state;
341 state = (struct private_data *)self->data;
343 /* If this is a real header, consume it. */
344 len = peek_at_header(self->upstream, NULL, state);
346 return (ARCHIVE_EOF);
347 __archive_read_filter_consume(self->upstream, len);
349 /* Initialize CRC accumulator. */
350 state->crc = crc32(0L, NULL, 0);
352 /* Initialize compression library. */
353 state->stream.next_in = (unsigned char *)(uintptr_t)
354 __archive_read_filter_ahead(self->upstream, 1, &avail);
355 state->stream.avail_in = (uInt)avail;
356 ret = inflateInit2(&(state->stream),
357 -15 /* Don't check for zlib header */);
359 /* Decipher the error code. */
362 state->in_stream = 1;
365 archive_set_error(&self->archive->archive,
367 "Internal error initializing compression library: "
368 "invalid setup parameter");
371 archive_set_error(&self->archive->archive, ENOMEM,
372 "Internal error initializing compression library: "
375 case Z_VERSION_ERROR:
376 archive_set_error(&self->archive->archive,
378 "Internal error initializing compression library: "
379 "invalid library version");
382 archive_set_error(&self->archive->archive,
384 "Internal error initializing compression library: "
385 " Zlib error %d", ret);
388 return (ARCHIVE_FATAL);
392 consume_trailer(struct archive_read_filter *self)
394 struct private_data *state;
395 const unsigned char *p;
398 state = (struct private_data *)self->data;
400 state->in_stream = 0;
401 switch (inflateEnd(&(state->stream))) {
405 archive_set_error(&self->archive->archive,
407 "Failed to clean up gzip decompressor");
408 return (ARCHIVE_FATAL);
411 /* GZip trailer is a fixed 8 byte structure. */
412 p = __archive_read_filter_ahead(self->upstream, 8, &avail);
413 if (p == NULL || avail == 0)
414 return (ARCHIVE_FATAL);
416 /* XXX TODO: Verify the length and CRC. */
418 /* We've verified the trailer, so consume it now. */
419 __archive_read_filter_consume(self->upstream, 8);
425 gzip_filter_read(struct archive_read_filter *self, const void **p)
427 struct private_data *state;
429 ssize_t avail_in, max_in;
432 state = (struct private_data *)self->data;
434 /* Empty our output buffer. */
435 state->stream.next_out = state->out_block;
436 state->stream.avail_out = (uInt)state->out_block_size;
438 /* Try to fill the output buffer. */
439 while (state->stream.avail_out > 0 && !state->eof) {
440 /* If we're not in a stream, read a header
441 * and initialize the decompression library. */
442 if (!state->in_stream) {
443 ret = consume_header(self);
444 if (ret == ARCHIVE_EOF) {
448 if (ret < ARCHIVE_OK)
452 /* Peek at the next available data. */
453 /* ZLib treats stream.next_in as const but doesn't declare
454 * it so, hence this ugly cast. */
455 state->stream.next_in = (unsigned char *)(uintptr_t)
456 __archive_read_filter_ahead(self->upstream, 1, &avail_in);
457 if (state->stream.next_in == NULL) {
458 archive_set_error(&self->archive->archive,
460 "truncated gzip input");
461 return (ARCHIVE_FATAL);
463 if (UINT_MAX >= SSIZE_MAX)
467 if (avail_in > max_in)
469 state->stream.avail_in = (uInt)avail_in;
471 /* Decompress and consume some of that data. */
472 ret = inflate(&(state->stream), 0);
474 case Z_OK: /* Decompressor made some progress. */
475 __archive_read_filter_consume(self->upstream,
476 avail_in - state->stream.avail_in);
478 case Z_STREAM_END: /* Found end of stream. */
479 __archive_read_filter_consume(self->upstream,
480 avail_in - state->stream.avail_in);
481 /* Consume the stream trailer; release the
482 * decompression library. */
483 ret = consume_trailer(self);
484 if (ret < ARCHIVE_OK)
488 /* Return an error. */
489 archive_set_error(&self->archive->archive,
491 "gzip decompression failed");
492 return (ARCHIVE_FATAL);
496 /* We've read as much as we can. */
497 decompressed = state->stream.next_out - state->out_block;
498 state->total_out += decompressed;
499 if (decompressed == 0)
502 *p = state->out_block;
503 return (decompressed);
507 * Clean up the decompressor.
510 gzip_filter_close(struct archive_read_filter *self)
512 struct private_data *state;
515 state = (struct private_data *)self->data;
518 if (state->in_stream) {
519 switch (inflateEnd(&(state->stream))) {
523 archive_set_error(&(self->archive->archive),
525 "Failed to clean up gzip compressor");
531 free(state->out_block);
536 #endif /* HAVE_ZLIB_H */