2004-02-09 23:22:54 +00:00
|
|
|
/*-
|
2007-01-09 08:05:56 +00:00
|
|
|
* Copyright (c) 2003-2007 Tim Kientzle
|
2004-02-09 23:22:54 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2007-01-09 08:05:56 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-02-09 23:22:54 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-03-09 19:50:41 +00:00
|
|
|
#include "archive_platform.h"
|
2004-07-30 04:14:47 +00:00
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2004-08-14 03:45:45 +00:00
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_ERRNO_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <errno.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <stdlib.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <string.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <unistd.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
2004-08-14 03:45:45 +00:00
|
|
|
#ifdef HAVE_ZLIB_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <zlib.h>
|
2004-08-14 03:45:45 +00:00
|
|
|
#endif
|
2004-03-19 22:37:06 +00:00
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
#include "archive.h"
|
|
|
|
#include "archive_private.h"
|
2007-03-03 07:37:37 +00:00
|
|
|
#include "archive_read_private.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-08-14 03:45:45 +00:00
|
|
|
#ifdef HAVE_ZLIB_H
|
2004-02-09 23:22:54 +00:00
|
|
|
struct private_data {
|
|
|
|
z_stream stream;
|
2009-03-05 02:37:05 +00:00
|
|
|
char in_stream;
|
2008-12-06 06:45:15 +00:00
|
|
|
unsigned char *out_block;
|
|
|
|
size_t out_block_size;
|
2004-02-09 23:22:54 +00:00
|
|
|
int64_t total_out;
|
|
|
|
unsigned long crc;
|
2007-05-29 01:00:21 +00:00
|
|
|
char eof; /* True = found end of compressed data. */
|
2004-02-09 23:22:54 +00:00
|
|
|
};
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
/* Gzip Filter. */
|
|
|
|
static ssize_t gzip_filter_read(struct archive_read_filter *, const void **);
|
|
|
|
static int gzip_filter_close(struct archive_read_filter *);
|
2004-08-14 03:45:45 +00:00
|
|
|
#endif
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
/*
|
|
|
|
* Note that we can detect gzip archives even if we can't decompress
|
|
|
|
* them. (In fact, we like detecting them because we can give better
|
|
|
|
* error messages.) So the bid framework here gets compiled even
|
|
|
|
* if zlib is unavailable.
|
2009-03-05 02:37:05 +00:00
|
|
|
*
|
|
|
|
* TODO: If zlib is unavailable, gzip_bidder_init() should
|
|
|
|
* use the compress_program framework to try to fire up an external
|
|
|
|
* gunzip program.
|
2008-12-06 06:45:15 +00:00
|
|
|
*/
|
2009-03-05 02:37:05 +00:00
|
|
|
static int gzip_bidder_bid(struct archive_read_filter_bidder *,
|
|
|
|
struct archive_read_filter *);
|
2009-03-05 02:19:42 +00:00
|
|
|
static int gzip_bidder_init(struct archive_read_filter *);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_read_support_compression_gzip(struct archive *_a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
2009-03-05 02:19:42 +00:00
|
|
|
struct archive_read_filter_bidder *bidder = __archive_read_get_bidder(a);
|
2008-12-06 06:45:15 +00:00
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
if (bidder == NULL)
|
2008-12-06 06:45:15 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
bidder->data = NULL;
|
|
|
|
bidder->bid = gzip_bidder_bid;
|
|
|
|
bidder->init = gzip_bidder_init;
|
2009-03-06 05:58:56 +00:00
|
|
|
bidder->options = NULL;
|
2009-03-07 02:51:18 +00:00
|
|
|
bidder->free = NULL; /* No data, so no cleanup necessary. */
|
2009-04-17 00:55:52 +00:00
|
|
|
/* Signal the extent of gzip support with the return value here. */
|
|
|
|
#if HAVE_ZLIB_H
|
2008-12-06 06:45:15 +00:00
|
|
|
return (ARCHIVE_OK);
|
2009-04-17 00:55:52 +00:00
|
|
|
#else
|
|
|
|
archive_set_error(_a, ARCHIVE_ERRNO_MISC,
|
|
|
|
"Using external gunzip program");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-03-05 02:37:05 +00:00
|
|
|
* Read and verify the header.
|
2004-02-09 23:22:54 +00:00
|
|
|
*
|
2009-03-05 02:37:05 +00:00
|
|
|
* Returns zero if the header couldn't be validated, else returns
|
|
|
|
* number of bytes in header. If pbits is non-NULL, it receives a
|
|
|
|
* count of bits verified, suitable for use by bidder.
|
2004-02-09 23:22:54 +00:00
|
|
|
*/
|
|
|
|
static int
|
2009-03-05 02:37:05 +00:00
|
|
|
peek_at_header(struct archive_read_filter *filter, int *pbits)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2009-03-05 02:37:05 +00:00
|
|
|
const unsigned char *p;
|
|
|
|
ssize_t avail, len;
|
|
|
|
int bits = 0;
|
|
|
|
int header_flags;
|
|
|
|
|
|
|
|
/* Start by looking at the first ten bytes of the header, which
|
|
|
|
* is all fixed layout. */
|
|
|
|
len = 10;
|
|
|
|
p = __archive_read_filter_ahead(filter, len, &avail);
|
|
|
|
if (p == NULL || avail == 0)
|
2004-02-09 23:22:54 +00:00
|
|
|
return (0);
|
2009-03-05 02:37:05 +00:00
|
|
|
if (p[0] != 037)
|
2004-02-09 23:22:54 +00:00
|
|
|
return (0);
|
2009-03-05 02:37:05 +00:00
|
|
|
bits += 8;
|
|
|
|
if (p[1] != 0213)
|
2004-02-09 23:22:54 +00:00
|
|
|
return (0);
|
2009-03-05 02:37:05 +00:00
|
|
|
bits += 8;
|
|
|
|
if (p[2] != 8) /* We only support deflation. */
|
2004-02-09 23:22:54 +00:00
|
|
|
return (0);
|
2009-03-05 02:37:05 +00:00
|
|
|
bits += 8;
|
|
|
|
if ((p[3] & 0xE0)!= 0) /* No reserved flags set. */
|
|
|
|
return (0);
|
|
|
|
bits += 3;
|
|
|
|
header_flags = p[3];
|
|
|
|
/* Bytes 4-7 are mod time. */
|
|
|
|
/* Byte 8 is deflate flags. */
|
|
|
|
/* XXXX TODO: return deflate flags back to consume_header for use
|
|
|
|
in initializing the decompressor. */
|
|
|
|
/* Byte 9 is OS. */
|
|
|
|
|
|
|
|
/* Optional extra data: 2 byte length plus variable body. */
|
|
|
|
if (header_flags & 4) {
|
|
|
|
p = __archive_read_filter_ahead(filter, len + 2, &avail);
|
|
|
|
if (p == NULL)
|
|
|
|
return (0);
|
|
|
|
len += ((int)p[len + 1] << 8) | (int)p[len];
|
2009-05-23 04:31:05 +00:00
|
|
|
len += 2;
|
2009-03-05 02:37:05 +00:00
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-03-05 02:37:05 +00:00
|
|
|
/* Null-terminated optional filename. */
|
|
|
|
if (header_flags & 8) {
|
|
|
|
do {
|
|
|
|
++len;
|
|
|
|
if (avail < len)
|
|
|
|
p = __archive_read_filter_ahead(filter,
|
|
|
|
len, &avail);
|
|
|
|
if (p == NULL)
|
|
|
|
return (0);
|
|
|
|
} while (p[len - 1] != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Null-terminated optional comment. */
|
|
|
|
if (header_flags & 16) {
|
|
|
|
do {
|
|
|
|
++len;
|
|
|
|
if (avail < len)
|
|
|
|
p = __archive_read_filter_ahead(filter,
|
|
|
|
len, &avail);
|
|
|
|
if (p == NULL)
|
|
|
|
return (0);
|
|
|
|
} while (p[len - 1] != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Optional header CRC */
|
|
|
|
if ((header_flags & 2)) {
|
|
|
|
p = __archive_read_filter_ahead(filter, len + 2, &avail);
|
|
|
|
if (p == NULL)
|
|
|
|
return (0);
|
|
|
|
#if 0
|
|
|
|
int hcrc = ((int)p[len + 1] << 8) | (int)p[len];
|
|
|
|
int crc = /* XXX TODO: Compute header CRC. */;
|
|
|
|
if (crc != hcrc)
|
2004-02-09 23:22:54 +00:00
|
|
|
return (0);
|
2009-03-05 02:37:05 +00:00
|
|
|
bits += 16;
|
|
|
|
#endif
|
|
|
|
len += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pbits != NULL)
|
|
|
|
*pbits = bits;
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bidder just verifies the header and returns the number of verified bits.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
gzip_bidder_bid(struct archive_read_filter_bidder *self,
|
|
|
|
struct archive_read_filter *filter)
|
|
|
|
{
|
|
|
|
int bits_checked;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-03-05 02:37:05 +00:00
|
|
|
(void)self; /* UNUSED */
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-03-05 02:37:05 +00:00
|
|
|
if (peek_at_header(filter, &bits_checked))
|
|
|
|
return (bits_checked);
|
|
|
|
return (0);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2004-08-14 03:45:45 +00:00
|
|
|
|
|
|
|
#ifndef HAVE_ZLIB_H
|
|
|
|
|
|
|
|
/*
|
2009-04-17 00:55:52 +00:00
|
|
|
* If we don't have the library on this system, we can't do the
|
|
|
|
* decompression directly. We can, however, try to run gunzip
|
|
|
|
* in case that's available.
|
2004-08-14 03:45:45 +00:00
|
|
|
*/
|
2009-03-05 02:19:42 +00:00
|
|
|
static int
|
2009-03-07 02:51:18 +00:00
|
|
|
gzip_bidder_init(struct archive_read_filter *self)
|
2004-08-14 03:45:45 +00:00
|
|
|
{
|
2009-03-07 02:51:18 +00:00
|
|
|
int r;
|
2004-08-14 03:45:45 +00:00
|
|
|
|
2009-03-07 02:51:18 +00:00
|
|
|
r = __archive_read_program(self, "gunzip");
|
2009-04-17 00:55:52 +00:00
|
|
|
/* Note: We set the format here even if __archive_read_program()
|
|
|
|
* above fails. We do, after all, know what the format is
|
|
|
|
* even if we weren't able to read it. */
|
2009-03-07 02:51:18 +00:00
|
|
|
self->code = ARCHIVE_COMPRESSION_GZIP;
|
|
|
|
self->name = "gzip";
|
|
|
|
return (r);
|
2004-08-14 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/*
|
2009-03-05 02:19:42 +00:00
|
|
|
* Initialize the filter object.
|
2004-02-09 23:22:54 +00:00
|
|
|
*/
|
2009-03-05 02:19:42 +00:00
|
|
|
static int
|
|
|
|
gzip_bidder_init(struct archive_read_filter *self)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2009-03-05 02:19:42 +00:00
|
|
|
struct private_data *state;
|
2008-12-06 06:45:15 +00:00
|
|
|
static const size_t out_block_size = 64 * 1024;
|
|
|
|
void *out_block;
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
self->code = ARCHIVE_COMPRESSION_GZIP;
|
|
|
|
self->name = "gzip";
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
state = (struct private_data *)calloc(sizeof(*state), 1);
|
|
|
|
out_block = (unsigned char *)malloc(out_block_size);
|
2009-03-05 02:19:42 +00:00
|
|
|
if (state == NULL || out_block == NULL) {
|
2008-12-06 06:45:15 +00:00
|
|
|
free(out_block);
|
|
|
|
free(state);
|
2009-03-05 02:19:42 +00:00
|
|
|
archive_set_error(&self->archive->archive, ENOMEM,
|
2009-03-05 02:37:05 +00:00
|
|
|
"Can't allocate data for gzip decompression");
|
2009-03-05 02:19:42 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2008-12-06 06:45:15 +00:00
|
|
|
|
|
|
|
self->data = state;
|
|
|
|
state->out_block_size = out_block_size;
|
|
|
|
state->out_block = out_block;
|
2009-03-05 02:19:42 +00:00
|
|
|
self->read = gzip_filter_read;
|
2008-12-06 06:45:15 +00:00
|
|
|
self->skip = NULL; /* not supported */
|
2009-03-05 02:19:42 +00:00
|
|
|
self->close = gzip_filter_close;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-03-05 02:37:05 +00:00
|
|
|
state->in_stream = 0; /* We're not actually within a stream yet. */
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
return (ARCHIVE_OK);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
static int
|
2009-03-05 02:37:05 +00:00
|
|
|
consume_header(struct archive_read_filter *self)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
struct private_data *state;
|
2009-03-05 02:37:05 +00:00
|
|
|
ssize_t avail;
|
|
|
|
size_t len;
|
|
|
|
int ret;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
state = (struct private_data *)self->data;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-03-05 02:37:05 +00:00
|
|
|
/* If this is a real header, consume it. */
|
|
|
|
len = peek_at_header(self->upstream, NULL);
|
|
|
|
if (len == 0)
|
|
|
|
return (ARCHIVE_EOF);
|
|
|
|
__archive_read_filter_consume(self->upstream, len);
|
|
|
|
|
|
|
|
/* Initialize CRC accumulator. */
|
|
|
|
state->crc = crc32(0L, NULL, 0);
|
|
|
|
|
|
|
|
/* Initialize compression library. */
|
|
|
|
state->stream.next_in = (unsigned char *)(uintptr_t)
|
|
|
|
__archive_read_filter_ahead(self->upstream, 1, &avail);
|
|
|
|
state->stream.avail_in = avail;
|
|
|
|
ret = inflateInit2(&(state->stream),
|
|
|
|
-15 /* Don't check for zlib header */);
|
|
|
|
|
|
|
|
/* Decipher the error code. */
|
|
|
|
switch (ret) {
|
|
|
|
case Z_OK:
|
|
|
|
state->in_stream = 1;
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
case Z_STREAM_ERROR:
|
|
|
|
archive_set_error(&self->archive->archive,
|
|
|
|
ARCHIVE_ERRNO_MISC,
|
|
|
|
"Internal error initializing compression library: "
|
|
|
|
"invalid setup parameter");
|
2008-12-06 06:45:15 +00:00
|
|
|
break;
|
2009-03-05 02:37:05 +00:00
|
|
|
case Z_MEM_ERROR:
|
|
|
|
archive_set_error(&self->archive->archive, ENOMEM,
|
|
|
|
"Internal error initializing compression library: "
|
|
|
|
"out of memory");
|
2008-12-06 06:45:15 +00:00
|
|
|
break;
|
2009-03-05 02:37:05 +00:00
|
|
|
case Z_VERSION_ERROR:
|
|
|
|
archive_set_error(&self->archive->archive,
|
|
|
|
ARCHIVE_ERRNO_MISC,
|
|
|
|
"Internal error initializing compression library: "
|
|
|
|
"invalid library version");
|
2008-12-06 06:45:15 +00:00
|
|
|
break;
|
2009-03-05 02:37:05 +00:00
|
|
|
default:
|
|
|
|
archive_set_error(&self->archive->archive,
|
|
|
|
ARCHIVE_ERRNO_MISC,
|
|
|
|
"Internal error initializing compression library: "
|
|
|
|
" Zlib error %d", ret);
|
2008-12-06 06:45:15 +00:00
|
|
|
break;
|
2009-03-05 02:37:05 +00:00
|
|
|
}
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
2008-12-06 06:45:15 +00:00
|
|
|
|
2009-03-05 02:37:05 +00:00
|
|
|
static int
|
|
|
|
consume_trailer(struct archive_read_filter *self)
|
|
|
|
{
|
|
|
|
struct private_data *state;
|
|
|
|
const unsigned char *p;
|
|
|
|
ssize_t avail;
|
2008-12-06 06:45:15 +00:00
|
|
|
|
2009-03-05 02:37:05 +00:00
|
|
|
state = (struct private_data *)self->data;
|
|
|
|
|
|
|
|
state->in_stream = 0;
|
|
|
|
switch (inflateEnd(&(state->stream))) {
|
|
|
|
case Z_OK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
archive_set_error(&self->archive->archive,
|
|
|
|
ARCHIVE_ERRNO_MISC,
|
|
|
|
"Failed to clean up gzip decompressor");
|
2008-12-06 06:45:15 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 02:37:05 +00:00
|
|
|
/* GZip trailer is a fixed 8 byte structure. */
|
|
|
|
p = __archive_read_filter_ahead(self->upstream, 8, &avail);
|
|
|
|
if (p == NULL || avail == 0)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
|
|
|
|
/* XXX TODO: Verify the length and CRC. */
|
|
|
|
|
|
|
|
/* We've verified the trailer, so consume it now. */
|
|
|
|
__archive_read_filter_consume(self->upstream, 8);
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
return (ARCHIVE_OK);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
2009-03-05 02:19:42 +00:00
|
|
|
gzip_filter_read(struct archive_read_filter *self, const void **p)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
struct private_data *state;
|
2009-03-05 02:37:05 +00:00
|
|
|
size_t decompressed;
|
|
|
|
ssize_t avail_in;
|
2004-02-09 23:22:54 +00:00
|
|
|
int ret;
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
state = (struct private_data *)self->data;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
/* Empty our output buffer. */
|
|
|
|
state->stream.next_out = state->out_block;
|
|
|
|
state->stream.avail_out = state->out_block_size;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
/* Try to fill the output buffer. */
|
|
|
|
while (state->stream.avail_out > 0 && !state->eof) {
|
2009-03-05 02:37:05 +00:00
|
|
|
/* If we're not in a stream, read a header
|
|
|
|
* and initialize the decompression library. */
|
|
|
|
if (!state->in_stream) {
|
|
|
|
ret = consume_header(self);
|
|
|
|
if (ret == ARCHIVE_EOF) {
|
2008-12-06 06:45:15 +00:00
|
|
|
state->eof = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret < ARCHIVE_OK)
|
|
|
|
return (ret);
|
2009-03-05 02:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Peek at the next available data. */
|
|
|
|
/* ZLib treats stream.next_in as const but doesn't declare
|
|
|
|
* it so, hence this ugly cast. */
|
|
|
|
state->stream.next_in = (unsigned char *)(uintptr_t)
|
|
|
|
__archive_read_filter_ahead(self->upstream, 1, &avail_in);
|
|
|
|
if (state->stream.next_in == NULL)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
state->stream.avail_in = avail_in;
|
|
|
|
|
|
|
|
/* Decompress and consume some of that data. */
|
|
|
|
ret = inflate(&(state->stream), 0);
|
|
|
|
switch (ret) {
|
|
|
|
case Z_OK: /* Decompressor made some progress. */
|
|
|
|
__archive_read_filter_consume(self->upstream,
|
|
|
|
avail_in - state->stream.avail_in);
|
|
|
|
break;
|
|
|
|
case Z_STREAM_END: /* Found end of stream. */
|
|
|
|
__archive_read_filter_consume(self->upstream,
|
|
|
|
avail_in - state->stream.avail_in);
|
|
|
|
/* Consume the stream trailer; release the
|
|
|
|
* decompression library. */
|
|
|
|
ret = consume_trailer(self);
|
2009-12-28 02:05:28 +00:00
|
|
|
if (ret < ARCHIVE_OK)
|
|
|
|
return (ret);
|
2009-03-05 02:37:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Return an error. */
|
|
|
|
archive_set_error(&self->archive->archive,
|
|
|
|
ARCHIVE_ERRNO_MISC,
|
|
|
|
"gzip decompression failed");
|
|
|
|
return (ARCHIVE_FATAL);
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
/* We've read as much as we can. */
|
|
|
|
decompressed = state->stream.next_out - state->out_block;
|
|
|
|
state->total_out += decompressed;
|
|
|
|
if (decompressed == 0)
|
|
|
|
*p = NULL;
|
|
|
|
else
|
|
|
|
*p = state->out_block;
|
|
|
|
return (decompressed);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
/*
|
|
|
|
* Clean up the decompressor.
|
|
|
|
*/
|
|
|
|
static int
|
2009-03-05 02:19:42 +00:00
|
|
|
gzip_filter_close(struct archive_read_filter *self)
|
2008-12-06 06:45:15 +00:00
|
|
|
{
|
|
|
|
struct private_data *state;
|
|
|
|
int ret;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
state = (struct private_data *)self->data;
|
|
|
|
ret = ARCHIVE_OK;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-03-05 02:37:05 +00:00
|
|
|
if (state->in_stream) {
|
2008-12-06 06:45:15 +00:00
|
|
|
switch (inflateEnd(&(state->stream))) {
|
|
|
|
case Z_OK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
archive_set_error(&(self->archive->archive),
|
2009-03-05 02:37:05 +00:00
|
|
|
ARCHIVE_ERRNO_MISC,
|
|
|
|
"Failed to clean up gzip compressor");
|
2008-12-06 06:45:15 +00:00
|
|
|
ret = ARCHIVE_FATAL;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
free(state->out_block);
|
|
|
|
free(state);
|
|
|
|
return (ret);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2004-07-30 04:14:47 +00:00
|
|
|
|
|
|
|
#endif /* HAVE_ZLIB_H */
|