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);
98 archive_read_support_filter_gzip(struct archive *_a)
100 struct archive_read *a = (struct archive_read *)_a;
101 struct archive_read_filter_bidder *bidder;
103 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
104 ARCHIVE_STATE_NEW, "archive_read_support_filter_gzip");
106 if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
107 return (ARCHIVE_FATAL);
110 bidder->name = "gzip";
111 bidder->bid = gzip_bidder_bid;
112 bidder->init = gzip_bidder_init;
113 bidder->options = NULL;
114 bidder->free = NULL; /* No data, so no cleanup necessary. */
115 /* Signal the extent of gzip support with the return value here. */
119 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
120 "Using external gzip program");
121 return (ARCHIVE_WARN);
126 * Read and verify the header.
128 * Returns zero if the header couldn't be validated, else returns
129 * number of bytes in header. If pbits is non-NULL, it receives a
130 * count of bits verified, suitable for use by bidder.
133 peek_at_header(struct archive_read_filter *filter, int *pbits,
135 struct private_data *state
141 const unsigned char *p;
146 (void)state; /* UNUSED */
149 /* Start by looking at the first ten bytes of the header, which
150 * is all fixed layout. */
152 p = __archive_read_filter_ahead(filter, len, &avail);
153 if (p == NULL || avail == 0)
155 /* We only support deflation- third byte must be 0x08. */
156 if (memcmp(p, "\x1F\x8B\x08", 3) != 0)
159 if ((p[3] & 0xE0)!= 0) /* No reserved flags set. */
163 /* Bytes 4-7 are mod time in little endian. */
166 state->mtime = archive_le32dec(p + 4);
168 /* Byte 8 is deflate flags. */
169 /* XXXX TODO: return deflate flags back to consume_header for use
170 in initializing the decompressor. */
173 /* Optional extra data: 2 byte length plus variable body. */
174 if (header_flags & 4) {
175 p = __archive_read_filter_ahead(filter, len + 2, &avail);
178 len += ((int)p[len + 1] << 8) | (int)p[len];
182 /* Null-terminated optional filename. */
183 if (header_flags & 8) {
185 ssize_t file_start = len;
190 p = __archive_read_filter_ahead(filter,
194 } while (p[len - 1] != 0);
198 /* Reset the name in case of repeat header reads. */
200 state->name = strdup((const char *)&p[file_start]);
205 /* Null-terminated optional comment. */
206 if (header_flags & 16) {
210 p = __archive_read_filter_ahead(filter,
214 } while (p[len - 1] != 0);
217 /* Optional header CRC */
218 if ((header_flags & 2)) {
219 p = __archive_read_filter_ahead(filter, len + 2, &avail);
223 int hcrc = ((int)p[len + 1] << 8) | (int)p[len];
224 int crc = /* XXX TODO: Compute header CRC. */;
238 * Bidder just verifies the header and returns the number of verified bits.
241 gzip_bidder_bid(struct archive_read_filter_bidder *self,
242 struct archive_read_filter *filter)
246 (void)self; /* UNUSED */
248 if (peek_at_header(filter, &bits_checked, NULL))
249 return (bits_checked);
256 * If we don't have the library on this system, we can't do the
257 * decompression directly. We can, however, try to run "gzip -d"
258 * in case that's available.
261 gzip_bidder_init(struct archive_read_filter *self)
265 r = __archive_read_program(self, "gzip -d");
266 /* Note: We set the format here even if __archive_read_program()
267 * above fails. We do, after all, know what the format is
268 * even if we weren't able to read it. */
269 self->code = ARCHIVE_FILTER_GZIP;
277 gzip_read_header(struct archive_read_filter *self, struct archive_entry *entry)
279 struct private_data *state;
281 state = (struct private_data *)self->data;
283 /* A mtime of 0 is considered invalid/missing. */
284 if (state->mtime != 0)
285 archive_entry_set_mtime(entry, state->mtime, 0);
287 /* If the name is available, extract it. */
289 archive_entry_set_pathname(entry, state->name);
295 * Initialize the filter object.
298 gzip_bidder_init(struct archive_read_filter *self)
300 struct private_data *state;
301 static const size_t out_block_size = 64 * 1024;
304 self->code = ARCHIVE_FILTER_GZIP;
307 state = (struct private_data *)calloc(sizeof(*state), 1);
308 out_block = (unsigned char *)malloc(out_block_size);
309 if (state == NULL || out_block == NULL) {
312 archive_set_error(&self->archive->archive, ENOMEM,
313 "Can't allocate data for gzip decompression");
314 return (ARCHIVE_FATAL);
318 state->out_block_size = out_block_size;
319 state->out_block = out_block;
320 self->read = gzip_filter_read;
321 self->skip = NULL; /* not supported */
322 self->close = gzip_filter_close;
324 self->read_header = gzip_read_header;
327 state->in_stream = 0; /* We're not actually within a stream yet. */
333 consume_header(struct archive_read_filter *self)
335 struct private_data *state;
340 state = (struct private_data *)self->data;
342 /* If this is a real header, consume it. */
343 len = peek_at_header(self->upstream, NULL, state);
345 return (ARCHIVE_EOF);
346 __archive_read_filter_consume(self->upstream, len);
348 /* Initialize CRC accumulator. */
349 state->crc = crc32(0L, NULL, 0);
351 /* Initialize compression library. */
352 state->stream.next_in = (unsigned char *)(uintptr_t)
353 __archive_read_filter_ahead(self->upstream, 1, &avail);
354 state->stream.avail_in = (uInt)avail;
355 ret = inflateInit2(&(state->stream),
356 -15 /* Don't check for zlib header */);
358 /* Decipher the error code. */
361 state->in_stream = 1;
364 archive_set_error(&self->archive->archive,
366 "Internal error initializing compression library: "
367 "invalid setup parameter");
370 archive_set_error(&self->archive->archive, ENOMEM,
371 "Internal error initializing compression library: "
374 case Z_VERSION_ERROR:
375 archive_set_error(&self->archive->archive,
377 "Internal error initializing compression library: "
378 "invalid library version");
381 archive_set_error(&self->archive->archive,
383 "Internal error initializing compression library: "
384 " Zlib error %d", ret);
387 return (ARCHIVE_FATAL);
391 consume_trailer(struct archive_read_filter *self)
393 struct private_data *state;
394 const unsigned char *p;
397 state = (struct private_data *)self->data;
399 state->in_stream = 0;
400 switch (inflateEnd(&(state->stream))) {
404 archive_set_error(&self->archive->archive,
406 "Failed to clean up gzip decompressor");
407 return (ARCHIVE_FATAL);
410 /* GZip trailer is a fixed 8 byte structure. */
411 p = __archive_read_filter_ahead(self->upstream, 8, &avail);
412 if (p == NULL || avail == 0)
413 return (ARCHIVE_FATAL);
415 /* XXX TODO: Verify the length and CRC. */
417 /* We've verified the trailer, so consume it now. */
418 __archive_read_filter_consume(self->upstream, 8);
424 gzip_filter_read(struct archive_read_filter *self, const void **p)
426 struct private_data *state;
428 ssize_t avail_in, max_in;
431 state = (struct private_data *)self->data;
433 /* Empty our output buffer. */
434 state->stream.next_out = state->out_block;
435 state->stream.avail_out = (uInt)state->out_block_size;
437 /* Try to fill the output buffer. */
438 while (state->stream.avail_out > 0 && !state->eof) {
439 /* If we're not in a stream, read a header
440 * and initialize the decompression library. */
441 if (!state->in_stream) {
442 ret = consume_header(self);
443 if (ret == ARCHIVE_EOF) {
447 if (ret < ARCHIVE_OK)
451 /* Peek at the next available data. */
452 /* ZLib treats stream.next_in as const but doesn't declare
453 * it so, hence this ugly cast. */
454 state->stream.next_in = (unsigned char *)(uintptr_t)
455 __archive_read_filter_ahead(self->upstream, 1, &avail_in);
456 if (state->stream.next_in == NULL) {
457 archive_set_error(&self->archive->archive,
459 "truncated gzip input");
460 return (ARCHIVE_FATAL);
462 if (UINT_MAX >= SSIZE_MAX)
466 if (avail_in > max_in)
468 state->stream.avail_in = (uInt)avail_in;
470 /* Decompress and consume some of that data. */
471 ret = inflate(&(state->stream), 0);
473 case Z_OK: /* Decompressor made some progress. */
474 __archive_read_filter_consume(self->upstream,
475 avail_in - state->stream.avail_in);
477 case Z_STREAM_END: /* Found end of stream. */
478 __archive_read_filter_consume(self->upstream,
479 avail_in - state->stream.avail_in);
480 /* Consume the stream trailer; release the
481 * decompression library. */
482 ret = consume_trailer(self);
483 if (ret < ARCHIVE_OK)
487 /* Return an error. */
488 archive_set_error(&self->archive->archive,
490 "gzip decompression failed");
491 return (ARCHIVE_FATAL);
495 /* We've read as much as we can. */
496 decompressed = state->stream.next_out - state->out_block;
497 state->total_out += decompressed;
498 if (decompressed == 0)
501 *p = state->out_block;
502 return (decompressed);
506 * Clean up the decompressor.
509 gzip_filter_close(struct archive_read_filter *self)
511 struct private_data *state;
514 state = (struct private_data *)self->data;
517 if (state->in_stream) {
518 switch (inflateEnd(&(state->stream))) {
522 archive_set_error(&(self->archive->archive),
524 "Failed to clean up gzip compressor");
530 free(state->out_block);
535 #endif /* HAVE_ZLIB_H */