archive_read_support_compression_bzip2.c [plain text]
#include "archive_platform.h"
__FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_compression_bzip2.c,v 1.19 2008/12/06 06:45:15 kientzle Exp $");
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_BZLIB_H
#include <bzlib.h>
#endif
#include "archive.h"
#include "archive_private.h"
#include "archive_read_private.h"
#if HAVE_BZLIB_H
struct private_data {
bz_stream stream;
char *out_block;
size_t out_block_size;
char valid;
char eof;
};
static ssize_t bzip2_source_read(struct archive_read_source *, const void **);
static int bzip2_source_close(struct archive_read_source *);
#endif
static int bzip2_reader_bid(struct archive_reader *, const void *, size_t);
static struct archive_read_source *bzip2_reader_init(struct archive_read *,
struct archive_reader *, struct archive_read_source *,
const void *, size_t);
static int bzip2_reader_free(struct archive_reader *);
int
archive_read_support_compression_bzip2(struct archive *_a)
{
struct archive_read *a = (struct archive_read *)_a;
struct archive_reader *reader = __archive_read_get_reader(a);
if (reader == NULL)
return (ARCHIVE_FATAL);
reader->data = NULL;
reader->bid = bzip2_reader_bid;
reader->init = bzip2_reader_init;
reader->free = bzip2_reader_free;
return (ARCHIVE_OK);
}
static int
bzip2_reader_free(struct archive_reader *self){
(void)self;
return (ARCHIVE_OK);
}
static int
bzip2_reader_bid(struct archive_reader *self, const void *buff, size_t len)
{
const unsigned char *buffer;
int bits_checked;
(void)self;
if (len < 1)
return (0);
buffer = (const unsigned char *)buff;
bits_checked = 0;
if (buffer[0] != 'B')
return (0);
bits_checked += 8;
if (len < 2)
return (bits_checked);
if (buffer[1] != 'Z')
return (0);
bits_checked += 8;
if (len < 3)
return (bits_checked);
if (buffer[2] != 'h')
return (0);
bits_checked += 8;
if (len < 4)
return (bits_checked);
if (buffer[3] < '1' || buffer[3] > '9')
return (0);
bits_checked += 5;
if (len < 5)
return (bits_checked);
if (buffer[4] == 0x31) {
size_t s = len;
if (s > 10) s = 10;
if (memcmp(buffer + 4, "\x31\x41\x59\x26\x53\x59", s - 4) != 0)
return (0);
bits_checked += 8 * (s - 4);
} else if (buffer[4] == 0x17) {
size_t s = len;
if (s > 10) s = 10;
if (memcmp(buffer + 4, "\x17\x72\x45\x38\x50\x90", s - 4) != 0)
return (0);
bits_checked += 8 * (s - 4);
} else
return (0);
return (bits_checked);
}
#ifndef HAVE_BZLIB_H
static struct archive_read_source *
bzip2_reader_init(struct archive_read *a, struct archive_reader *reader,
struct archive_read_source *upstream, const void *buff, size_t n)
{
(void)a;
(void)reader;
(void)upstream;
(void)buff;
(void)n;
archive_set_error(&a->archive, -1,
"This version of libarchive was compiled without bzip2 support");
return (NULL);
}
#else
static struct archive_read_source *
bzip2_reader_init(struct archive_read *a, struct archive_reader *reader,
struct archive_read_source *upstream, const void *buff, size_t n)
{
static const size_t out_block_size = 64 * 1024;
void *out_block;
struct archive_read_source *self;
struct private_data *state;
(void)reader;
a->archive.compression_code = ARCHIVE_COMPRESSION_BZIP2;
a->archive.compression_name = "bzip2";
self = calloc(sizeof(*self), 1);
state = (struct private_data *)calloc(sizeof(*state), 1);
out_block = (unsigned char *)malloc(out_block_size);
if (self == NULL || state == NULL || out_block == NULL) {
archive_set_error(&a->archive, ENOMEM,
"Can't allocate data for %s decompression",
a->archive.compression_name);
free(out_block);
free(state);
free(self);
return (NULL);
}
self->archive = a;
self->data = state;
state->out_block_size = out_block_size;
state->out_block = out_block;
self->upstream = upstream;
self->read = bzip2_source_read;
self->skip = NULL;
self->close = bzip2_source_close;
state->stream.next_in = (char *)(uintptr_t)(const void *)buff;
state->stream.avail_in = n;
state->stream.next_out = state->out_block;
state->stream.avail_out = state->out_block_size;
return (self);
}
static ssize_t
bzip2_source_read(struct archive_read_source *self, const void **p)
{
struct private_data *state;
size_t read_avail, decompressed;
const void *read_buf;
int ret;
state = (struct private_data *)self->data;
read_avail = 0;
if (state->eof) {
*p = NULL;
return (0);
}
state->stream.next_out = state->out_block;
state->stream.avail_out = state->out_block_size;
for (;;) {
if (state->stream.avail_in == 0) {
ret = (self->upstream->read)(self->upstream,
&read_buf);
state->stream.next_in
= (unsigned char *)(uintptr_t)read_buf;
if (ret < 0)
return (ARCHIVE_FATAL);
if (ret == 0) {
state->eof = 1;
*p = state->out_block;
decompressed = state->stream.next_out
- state->out_block;
return (decompressed);
}
state->stream.avail_in = ret;
}
if (!state->valid) {
if (state->stream.next_in[0] != 'B') {
state->eof = 1;
*p = state->out_block;
decompressed = state->stream.next_out
- state->out_block;
return (decompressed);
}
ret = BZ2_bzDecompressInit(&(state->stream),
0 ,
0 );
if (ret == BZ_MEM_ERROR)
ret = BZ2_bzDecompressInit(&(state->stream),
0 ,
1 );
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;
}
ret = BZ2_bzDecompress(&(state->stream));
switch (ret) {
case BZ_STREAM_END:
switch (BZ2_bzDecompressEnd(&(state->stream))) {
case BZ_OK:
break;
default:
archive_set_error(&(self->archive->archive),
ARCHIVE_ERRNO_MISC,
"Failed to clean up decompressor");
return (ARCHIVE_FATAL);
}
state->valid = 0;
case BZ_OK:
if (state->stream.avail_out == 0) {
*p = state->out_block;
decompressed = state->stream.next_out
- state->out_block;
return (decompressed);
}
break;
default:
archive_set_error(&self->archive->archive,
ARCHIVE_ERRNO_MISC, "bzip decompression failed");
return (ARCHIVE_FATAL);
}
}
}
static int
bzip2_source_close(struct archive_read_source *self)
{
struct private_data *state;
int ret = ARCHIVE_OK;
state = (struct private_data *)self->data;
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);
free(self);
return (ARCHIVE_OK);
}
#endif