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$");
|
|
|
|
|
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
|
2004-12-22 06:30:14 +00:00
|
|
|
#include <stdio.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#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_BZLIB_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <bzlib.h>
|
2004-08-14 03:45:45 +00:00
|
|
|
#endif
|
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
|
|
|
#if HAVE_BZLIB_H
|
2004-02-09 23:22:54 +00:00
|
|
|
struct private_data {
|
|
|
|
bz_stream stream;
|
2008-12-06 06:45:15 +00:00
|
|
|
char *out_block;
|
|
|
|
size_t out_block_size;
|
|
|
|
char valid; /* True = decompressor is initialized */
|
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
|
|
|
/* Bzip2 filter */
|
|
|
|
static ssize_t bzip2_filter_read(struct archive_read_filter *, const void **);
|
|
|
|
static int bzip2_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 bzip2 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 bzlib is unavailable.
|
|
|
|
*/
|
2009-03-05 02:19:42 +00:00
|
|
|
static int bzip2_reader_bid(struct archive_read_filter_bidder *, struct archive_read_filter *);
|
|
|
|
static int bzip2_reader_init(struct archive_read_filter *);
|
|
|
|
static int bzip2_reader_free(struct archive_read_filter_bidder *);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_read_support_compression_bzip2(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 *reader = __archive_read_get_bidder(a);
|
2008-12-06 06:45:15 +00:00
|
|
|
|
|
|
|
if (reader == NULL)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
|
|
|
|
reader->data = NULL;
|
|
|
|
reader->bid = bzip2_reader_bid;
|
|
|
|
reader->init = bzip2_reader_init;
|
2009-03-06 05:58:56 +00:00
|
|
|
reader->options = NULL;
|
2008-12-06 06:45:15 +00:00
|
|
|
reader->free = bzip2_reader_free;
|
2009-04-17 00:55:52 +00:00
|
|
|
#if HAVE_BZLIB_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 bunzip2 program");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
#endif
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-03-05 02:19:42 +00:00
|
|
|
bzip2_reader_free(struct archive_read_filter_bidder *self){
|
2008-12-06 06:45:15 +00:00
|
|
|
(void)self; /* UNUSED */
|
|
|
|
return (ARCHIVE_OK);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test whether we can handle this data.
|
|
|
|
*
|
|
|
|
* This logic returns zero if any part of the signature fails. It
|
|
|
|
* also tries to Do The Right Thing if a very short buffer prevents us
|
|
|
|
* from verifying as much as we would like.
|
|
|
|
*/
|
|
|
|
static int
|
2009-03-05 02:19:42 +00:00
|
|
|
bzip2_reader_bid(struct archive_read_filter_bidder *self, struct archive_read_filter *filter)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
const unsigned char *buffer;
|
2009-03-05 06:26:08 +00:00
|
|
|
ssize_t avail;
|
2004-02-09 23:22:54 +00:00
|
|
|
int bits_checked;
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
(void)self; /* UNUSED */
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
/* Minimal bzip2 archive is 14 bytes. */
|
|
|
|
buffer = __archive_read_filter_ahead(filter, 14, &avail);
|
|
|
|
if (buffer == NULL)
|
2004-02-09 23:22:54 +00:00
|
|
|
return (0);
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
/* First three bytes must be "BZh" */
|
2004-02-09 23:22:54 +00:00
|
|
|
bits_checked = 0;
|
2009-03-05 02:19:42 +00:00
|
|
|
if (buffer[0] != 'B' || buffer[1] != 'Z' || buffer[2] != 'h')
|
2004-02-09 23:22:54 +00:00
|
|
|
return (0);
|
2009-03-05 02:19:42 +00:00
|
|
|
bits_checked += 24;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
/* Next follows a compression flag which must be an ASCII digit. */
|
2004-02-09 23:22:54 +00:00
|
|
|
if (buffer[3] < '1' || buffer[3] > '9')
|
|
|
|
return (0);
|
|
|
|
bits_checked += 5;
|
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
/* After BZh[1-9], there must be either a data block
|
|
|
|
* which begins with 0x314159265359 or an end-of-data
|
|
|
|
* marker of 0x177245385090. */
|
2009-03-05 02:19:42 +00:00
|
|
|
if (memcmp(buffer + 4, "\x31\x41\x59\x26\x53\x59", 6) == 0)
|
|
|
|
bits_checked += 48;
|
|
|
|
else if (memcmp(buffer + 4, "\x17\x72\x45\x38\x50\x90", 6) == 0)
|
|
|
|
bits_checked += 48;
|
|
|
|
else
|
2008-05-26 17:00:24 +00:00
|
|
|
return (0);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
return (bits_checked);
|
|
|
|
}
|
|
|
|
|
2004-08-14 03:45:45 +00:00
|
|
|
#ifndef HAVE_BZLIB_H
|
|
|
|
|
|
|
|
/*
|
2007-05-29 01:00:21 +00:00
|
|
|
* If we don't have the library on this system, we can't actually do the
|
|
|
|
* decompression. We can, however, still detect compressed archives
|
|
|
|
* and emit a useful message.
|
2004-08-14 03:45:45 +00:00
|
|
|
*/
|
2009-03-05 02:19:42 +00:00
|
|
|
static int
|
|
|
|
bzip2_reader_init(struct archive_read_filter *self)
|
2004-08-14 03:45:45 +00:00
|
|
|
{
|
2009-04-17 00:55:52 +00:00
|
|
|
int r;
|
2004-08-14 03:45:45 +00:00
|
|
|
|
2009-04-17 00:55:52 +00:00
|
|
|
r = __archive_read_program(self, "bunzip2");
|
|
|
|
/* 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. */
|
|
|
|
self->code = ARCHIVE_COMPRESSION_BZIP2;
|
|
|
|
self->name = "bzip2";
|
|
|
|
return (r);
|
2004-08-14 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/*
|
|
|
|
* Setup the callbacks.
|
|
|
|
*/
|
2009-03-05 02:19:42 +00:00
|
|
|
static int
|
|
|
|
bzip2_reader_init(struct archive_read_filter *self)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2008-12-06 06:45:15 +00:00
|
|
|
static const size_t out_block_size = 64 * 1024;
|
|
|
|
void *out_block;
|
2004-02-09 23:22:54 +00:00
|
|
|
struct private_data *state;
|
2008-12-06 06:45:15 +00:00
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
self->code = ARCHIVE_COMPRESSION_BZIP2;
|
|
|
|
self->name = "bzip2";
|
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);
|
|
|
|
if (self == NULL || state == NULL || out_block == NULL) {
|
2009-03-05 02:19:42 +00:00
|
|
|
archive_set_error(&self->archive->archive, ENOMEM,
|
|
|
|
"Can't allocate data for bzip2 decompression");
|
2008-12-06 06:45:15 +00:00
|
|
|
free(out_block);
|
|
|
|
free(state);
|
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 = bzip2_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 = bzip2_filter_close;
|
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
|
|
|
* Return the next block of decompressed data.
|
2004-02-09 23:22:54 +00:00
|
|
|
*/
|
|
|
|
static ssize_t
|
2009-03-05 02:19:42 +00:00
|
|
|
bzip2_filter_read(struct archive_read_filter *self, const void **p)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
struct private_data *state;
|
2008-12-06 06:45:15 +00:00
|
|
|
size_t read_avail, decompressed;
|
2009-03-07 02:29:43 +00:00
|
|
|
const char *read_buf;
|
2009-03-05 06:26:08 +00:00
|
|
|
ssize_t ret;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
state = (struct private_data *)self->data;
|
|
|
|
read_avail = 0;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
if (state->eof) {
|
|
|
|
*p = NULL;
|
|
|
|
return (0);
|
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. */
|
2004-02-09 23:22:54 +00:00
|
|
|
for (;;) {
|
2008-12-06 06:45:15 +00:00
|
|
|
if (!state->valid) {
|
2009-03-05 02:19:42 +00:00
|
|
|
if (bzip2_reader_bid(self->bidder, self->upstream) == 0) {
|
2008-12-06 06:45:15 +00:00
|
|
|
state->eof = 1;
|
|
|
|
*p = state->out_block;
|
|
|
|
decompressed = state->stream.next_out
|
|
|
|
- state->out_block;
|
|
|
|
return (decompressed);
|
|
|
|
}
|
|
|
|
/* Initialize compression library. */
|
|
|
|
ret = BZ2_bzDecompressInit(&(state->stream),
|
|
|
|
0 /* library verbosity */,
|
|
|
|
0 /* don't use low-mem algorithm */);
|
|
|
|
|
|
|
|
/* If init fails, try low-memory algorithm instead. */
|
|
|
|
if (ret == BZ_MEM_ERROR)
|
|
|
|
ret = BZ2_bzDecompressInit(&(state->stream),
|
|
|
|
0 /* library verbosity */,
|
|
|
|
1 /* do use low-mem algo */);
|
|
|
|
|
|
|
|
if (ret != BZ_OK) {
|
|
|
|
const char *detail = NULL;
|
|
|
|
int err = ARCHIVE_ERRNO_MISC;
|
|
|
|
switch (ret) {
|
|
|
|
case BZ_PARAM_ERROR:
|
|
|
|
detail = "invalid setup parameter";
|
|
|
|
break;
|
|
|
|
case BZ_MEM_ERROR:
|
|
|
|
err = ENOMEM;
|
|
|
|
detail = "out of memory";
|
|
|
|
break;
|
|
|
|
case BZ_CONFIG_ERROR:
|
|
|
|
detail = "mis-compiled library";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
archive_set_error(&self->archive->archive, err,
|
|
|
|
"Internal error initializing decompressor%s%s",
|
|
|
|
detail == NULL ? "" : ": ",
|
|
|
|
detail);
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
state->valid = 1;
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
/* stream.next_in is really const, but bzlib
|
|
|
|
* doesn't declare it so. <sigh> */
|
2009-03-07 02:29:43 +00:00
|
|
|
read_buf =
|
2009-03-05 02:19:42 +00:00
|
|
|
__archive_read_filter_ahead(self->upstream, 1, &ret);
|
|
|
|
if (read_buf == NULL)
|
|
|
|
return (ARCHIVE_FATAL);
|
2009-03-07 02:29:43 +00:00
|
|
|
state->stream.next_in = (char *)(uintptr_t)read_buf;
|
2009-03-05 02:19:42 +00:00
|
|
|
state->stream.avail_in = ret;
|
|
|
|
/* There is no more data, return whatever we have. */
|
|
|
|
if (ret == 0) {
|
|
|
|
state->eof = 1;
|
|
|
|
*p = state->out_block;
|
|
|
|
decompressed = state->stream.next_out
|
|
|
|
- state->out_block;
|
|
|
|
return (decompressed);
|
|
|
|
}
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
/* Decompress as much as we can in one pass. */
|
|
|
|
ret = BZ2_bzDecompress(&(state->stream));
|
2009-03-05 02:19:42 +00:00
|
|
|
__archive_read_filter_consume(self->upstream,
|
2009-03-07 02:29:43 +00:00
|
|
|
state->stream.next_in - read_buf);
|
2009-03-05 02:19:42 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
switch (ret) {
|
|
|
|
case BZ_STREAM_END: /* Found end of stream. */
|
|
|
|
switch (BZ2_bzDecompressEnd(&(state->stream))) {
|
|
|
|
case BZ_OK:
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
default:
|
2008-12-06 06:45:15 +00:00
|
|
|
archive_set_error(&(self->archive->archive),
|
|
|
|
ARCHIVE_ERRNO_MISC,
|
|
|
|
"Failed to clean up decompressor");
|
|
|
|
return (ARCHIVE_FATAL);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2008-12-06 06:45:15 +00:00
|
|
|
state->valid = 0;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case BZ_OK: /* Decompressor made some progress. */
|
|
|
|
/* If we filled our buffer, update stats and return. */
|
|
|
|
if (state->stream.avail_out == 0) {
|
|
|
|
*p = state->out_block;
|
|
|
|
decompressed = state->stream.next_out
|
|
|
|
- state->out_block;
|
|
|
|
return (decompressed);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: /* Return an error. */
|
|
|
|
archive_set_error(&self->archive->archive,
|
|
|
|
ARCHIVE_ERRNO_MISC, "bzip decompression failed");
|
|
|
|
return (ARCHIVE_FATAL);
|
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
|
|
|
bzip2_filter_close(struct archive_read_filter *self)
|
2008-12-06 06:45:15 +00:00
|
|
|
{
|
|
|
|
struct private_data *state;
|
|
|
|
int ret = ARCHIVE_OK;
|
|
|
|
|
|
|
|
state = (struct private_data *)self->data;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
if (state->valid) {
|
|
|
|
switch (BZ2_bzDecompressEnd(&state->stream)) {
|
|
|
|
case BZ_OK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
archive_set_error(&self->archive->archive,
|
|
|
|
ARCHIVE_ERRNO_MISC,
|
|
|
|
"Failed to clean up decompressor");
|
|
|
|
ret = ARCHIVE_FATAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(state->out_block);
|
|
|
|
free(state);
|
|
|
|
return (ARCHIVE_OK);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2004-07-30 04:14:47 +00:00
|
|
|
|
|
|
|
#endif /* HAVE_BZLIB_H */
|