Blame libarchive/archive_write_add_filter_bzip2.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2003-2007 Tim Kientzle
Packit Service 1d0348
 * Copyright (c) 2012 Michihiro NAKAJIMA
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit Service 1d0348
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 1d0348
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit Service 1d0348
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 1d0348
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 1d0348
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 1d0348
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 1d0348
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 1d0348
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit Service 1d0348
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
#include "archive_platform.h"
Packit Service 1d0348
Packit Service 1d0348
__FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_compression_bzip2.c 201091 2009-12-28 02:22:41Z kientzle $");
Packit Service 1d0348
Packit Service 1d0348
#ifdef HAVE_ERRNO_H
Packit Service 1d0348
#include <errno.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#include <stdio.h>
Packit Service 1d0348
#ifdef HAVE_STDLIB_H
Packit Service 1d0348
#include <stdlib.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STRING_H
Packit Service 1d0348
#include <string.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_BZLIB_H
Packit Service 1d0348
#include <bzlib.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
#include "archive.h"
Packit Service 1d0348
#include "archive_private.h"
Packit Service 1d0348
#include "archive_write_private.h"
Packit Service 1d0348
Packit Service 1d0348
#if ARCHIVE_VERSION_NUMBER < 4000000
Packit Service 1d0348
int
Packit Service 1d0348
archive_write_set_compression_bzip2(struct archive *a)
Packit Service 1d0348
{
Packit Service 1d0348
	__archive_write_filters_free(a);
Packit Service 1d0348
	return (archive_write_add_filter_bzip2(a));
Packit Service 1d0348
}
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
struct private_data {
Packit Service 1d0348
	int		 compression_level;
Packit Service 1d0348
#if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
Packit Service 1d0348
	bz_stream	 stream;
Packit Service 1d0348
	int64_t		 total_in;
Packit Service 1d0348
	char		*compressed;
Packit Service 1d0348
	size_t		 compressed_buffer_size;
Packit Service 1d0348
#else
Packit Service 1d0348
	struct archive_write_program_data *pdata;
Packit Service 1d0348
#endif
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
static int archive_compressor_bzip2_close(struct archive_write_filter *);
Packit Service 1d0348
static int archive_compressor_bzip2_free(struct archive_write_filter *);
Packit Service 1d0348
static int archive_compressor_bzip2_open(struct archive_write_filter *);
Packit Service 1d0348
static int archive_compressor_bzip2_options(struct archive_write_filter *,
Packit Service 1d0348
		    const char *, const char *);
Packit Service 1d0348
static int archive_compressor_bzip2_write(struct archive_write_filter *,
Packit Service 1d0348
		    const void *, size_t);
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Add a bzip2 compression filter to this write handle.
Packit Service 1d0348
 */
Packit Service 1d0348
int
Packit Service 1d0348
archive_write_add_filter_bzip2(struct archive *_a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_write *a = (struct archive_write *)_a;
Packit Service 1d0348
	struct archive_write_filter *f = __archive_write_allocate_filter(_a);
Packit Service 1d0348
	struct private_data *data;
Packit Service 1d0348
Packit Service 1d0348
	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
Packit Service 1d0348
	    ARCHIVE_STATE_NEW, "archive_write_add_filter_bzip2");
Packit Service 1d0348
Packit Service 1d0348
	data = calloc(1, sizeof(*data));
Packit Service 1d0348
	if (data == NULL) {
Packit Service 1d0348
		archive_set_error(&a->archive, ENOMEM, "Out of memory");
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	data->compression_level = 9; /* default */
Packit Service 1d0348
Packit Service 1d0348
	f->data = data;
Packit Service 1d0348
	f->options = &archive_compressor_bzip2_options;
Packit Service 1d0348
	f->close = &archive_compressor_bzip2_close;
Packit Service 1d0348
	f->free = &archive_compressor_bzip2_free;
Packit Service 1d0348
	f->open = &archive_compressor_bzip2_open;
Packit Service 1d0348
	f->code = ARCHIVE_FILTER_BZIP2;
Packit Service 1d0348
	f->name = "bzip2";
Packit Service 1d0348
#if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
#else
Packit Service 1d0348
	data->pdata = __archive_write_program_allocate("bzip2");
Packit Service 1d0348
	if (data->pdata == NULL) {
Packit Service 1d0348
		free(data);
Packit Service 1d0348
		archive_set_error(&a->archive, ENOMEM, "Out of memory");
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	data->compression_level = 0;
Packit Service 1d0348
	archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
	    "Using external bzip2 program");
Packit Service 1d0348
	return (ARCHIVE_WARN);
Packit Service 1d0348
#endif
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Set write options.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
archive_compressor_bzip2_options(struct archive_write_filter *f,
Packit Service 1d0348
    const char *key, const char *value)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *data = (struct private_data *)f->data;
Packit Service 1d0348
Packit Service 1d0348
	if (strcmp(key, "compression-level") == 0) {
Packit Service 1d0348
		if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
Packit Service 1d0348
		    value[1] != '\0')
Packit Service 1d0348
			return (ARCHIVE_WARN);
Packit Service 1d0348
		data->compression_level = value[0] - '0';
Packit Service 1d0348
		/* Make '0' be a synonym for '1'. */
Packit Service 1d0348
		/* This way, bzip2 compressor supports the same 0..9
Packit Service 1d0348
		 * range of levels as gzip. */
Packit Service 1d0348
		if (data->compression_level < 1)
Packit Service 1d0348
			data->compression_level = 1;
Packit Service 1d0348
		return (ARCHIVE_OK);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Note: The "warn" return is just to inform the options
Packit Service 1d0348
	 * supervisor that we didn't handle it.  It will generate
Packit Service 1d0348
	 * a suitable error if no one used this option. */
Packit Service 1d0348
	return (ARCHIVE_WARN);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
#if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
Packit Service 1d0348
/* Don't compile this if we don't have bzlib. */
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Yuck.  bzlib.h is not const-correct, so I need this one bit
Packit Service 1d0348
 * of ugly hackery to convert a const * pointer to a non-const pointer.
Packit Service 1d0348
 */
Packit Service 1d0348
#define	SET_NEXT_IN(st,src)					\
Packit Service 1d0348
	(st)->stream.next_in = (char *)(uintptr_t)(const void *)(src)
Packit Service 1d0348
static int drive_compressor(struct archive_write_filter *,
Packit Service 1d0348
		    struct private_data *, int finishing);
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Setup callback.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
archive_compressor_bzip2_open(struct archive_write_filter *f)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *data = (struct private_data *)f->data;
Packit Service 1d0348
	int ret;
Packit Service 1d0348
Packit Service 1d0348
	ret = __archive_write_open_filter(f->next_filter);
Packit Service 1d0348
	if (ret != 0)
Packit Service 1d0348
		return (ret);
Packit Service 1d0348
Packit Service 1d0348
	if (data->compressed == NULL) {
Packit Service 1d0348
		size_t bs = 65536, bpb;
Packit Service 1d0348
		if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
Packit Service 1d0348
			/* Buffer size should be a multiple number of the of bytes
Packit Service 1d0348
			 * per block for performance. */
Packit Service 1d0348
			bpb = archive_write_get_bytes_per_block(f->archive);
Packit Service 1d0348
			if (bpb > bs)
Packit Service 1d0348
				bs = bpb;
Packit Service 1d0348
			else if (bpb != 0)
Packit Service 1d0348
				bs -= bs % bpb;
Packit Service 1d0348
		}
Packit Service 1d0348
		data->compressed_buffer_size = bs;
Packit Service 1d0348
		data->compressed
Packit Service 1d0348
		    = (char *)malloc(data->compressed_buffer_size);
Packit Service 1d0348
		if (data->compressed == NULL) {
Packit Service 1d0348
			archive_set_error(f->archive, ENOMEM,
Packit Service 1d0348
			    "Can't allocate data for compression buffer");
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	memset(&data->stream, 0, sizeof(data->stream));
Packit Service 1d0348
	data->stream.next_out = data->compressed;
Packit Service 1d0348
	data->stream.avail_out = data->compressed_buffer_size;
Packit Service 1d0348
	f->write = archive_compressor_bzip2_write;
Packit Service 1d0348
Packit Service 1d0348
	/* Initialize compression library */
Packit Service 1d0348
	ret = BZ2_bzCompressInit(&(data->stream),
Packit Service 1d0348
	    data->compression_level, 0, 30);
Packit Service 1d0348
	if (ret == BZ_OK) {
Packit Service 1d0348
		f->data = data;
Packit Service 1d0348
		return (ARCHIVE_OK);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Library setup failed: clean up. */
Packit Service 1d0348
	archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
	    "Internal error initializing compression library");
Packit Service 1d0348
Packit Service 1d0348
	/* Override the error message if we know what really went wrong. */
Packit Service 1d0348
	switch (ret) {
Packit Service 1d0348
	case BZ_PARAM_ERROR:
Packit Service 1d0348
		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Internal error initializing compression library: "
Packit Service 1d0348
		    "invalid setup parameter");
Packit Service 1d0348
		break;
Packit Service 1d0348
	case BZ_MEM_ERROR:
Packit Service 1d0348
		archive_set_error(f->archive, ENOMEM,
Packit Service 1d0348
		    "Internal error initializing compression library: "
Packit Service 1d0348
		    "out of memory");
Packit Service 1d0348
		break;
Packit Service 1d0348
	case BZ_CONFIG_ERROR:
Packit Service 1d0348
		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Internal error initializing compression library: "
Packit Service 1d0348
		    "mis-compiled library");
Packit Service 1d0348
		break;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	return (ARCHIVE_FATAL);
Packit Service 1d0348
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Write data to the compressed stream.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Returns ARCHIVE_OK if all data written, error otherwise.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
archive_compressor_bzip2_write(struct archive_write_filter *f,
Packit Service 1d0348
    const void *buff, size_t length)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *data = (struct private_data *)f->data;
Packit Service 1d0348
Packit Service 1d0348
	/* Update statistics */
Packit Service 1d0348
	data->total_in += length;
Packit Service 1d0348
Packit Service 1d0348
	/* Compress input data to output buffer */
Packit Service 1d0348
	SET_NEXT_IN(data, buff);
Packit Service 1d0348
	data->stream.avail_in = length;
Packit Service 1d0348
	if (drive_compressor(f, data, 0))
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Finish the compression.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
archive_compressor_bzip2_close(struct archive_write_filter *f)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *data = (struct private_data *)f->data;
Packit Service 1d0348
	int ret, r1;
Packit Service 1d0348
Packit Service 1d0348
	/* Finish compression cycle. */
Packit Service 1d0348
	ret = drive_compressor(f, data, 1);
Packit Service 1d0348
	if (ret == ARCHIVE_OK) {
Packit Service 1d0348
		/* Write the last block */
Packit Service 1d0348
		ret = __archive_write_filter(f->next_filter,
Packit Service 1d0348
		    data->compressed,
Packit Service 1d0348
		    data->compressed_buffer_size - data->stream.avail_out);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	switch (BZ2_bzCompressEnd(&(data->stream))) {
Packit Service 1d0348
	case BZ_OK:
Packit Service 1d0348
		break;
Packit Service 1d0348
	default:
Packit Service 1d0348
		archive_set_error(f->archive, ARCHIVE_ERRNO_PROGRAMMER,
Packit Service 1d0348
		    "Failed to clean up compressor");
Packit Service 1d0348
		ret = ARCHIVE_FATAL;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	r1 = __archive_write_close_filter(f->next_filter);
Packit Service 1d0348
	return (r1 < ret ? r1 : ret);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_compressor_bzip2_free(struct archive_write_filter *f)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *data = (struct private_data *)f->data;
Packit Service 1d0348
	free(data->compressed);
Packit Service 1d0348
	free(data);
Packit Service 1d0348
	f->data = NULL;
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Utility function to push input data through compressor, writing
Packit Service 1d0348
 * full output blocks as necessary.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Note that this handles both the regular write case (finishing ==
Packit Service 1d0348
 * false) and the end-of-archive case (finishing == true).
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
drive_compressor(struct archive_write_filter *f,
Packit Service 1d0348
    struct private_data *data, int finishing)
Packit Service 1d0348
{
Packit Service 1d0348
	int ret;
Packit Service 1d0348
Packit Service 1d0348
	for (;;) {
Packit Service 1d0348
		if (data->stream.avail_out == 0) {
Packit Service 1d0348
			ret = __archive_write_filter(f->next_filter,
Packit Service 1d0348
			    data->compressed,
Packit Service 1d0348
			    data->compressed_buffer_size);
Packit Service 1d0348
			if (ret != ARCHIVE_OK) {
Packit Service 1d0348
				/* TODO: Handle this write failure */
Packit Service 1d0348
				return (ARCHIVE_FATAL);
Packit Service 1d0348
			}
Packit Service 1d0348
			data->stream.next_out = data->compressed;
Packit Service 1d0348
			data->stream.avail_out = data->compressed_buffer_size;
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		/* If there's nothing to do, we're done. */
Packit Service 1d0348
		if (!finishing && data->stream.avail_in == 0)
Packit Service 1d0348
			return (ARCHIVE_OK);
Packit Service 1d0348
Packit Service 1d0348
		ret = BZ2_bzCompress(&(data->stream),
Packit Service 1d0348
		    finishing ? BZ_FINISH : BZ_RUN);
Packit Service 1d0348
Packit Service 1d0348
		switch (ret) {
Packit Service 1d0348
		case BZ_RUN_OK:
Packit Service 1d0348
			/* In non-finishing case, did compressor
Packit Service 1d0348
			 * consume everything? */
Packit Service 1d0348
			if (!finishing && data->stream.avail_in == 0)
Packit Service 1d0348
				return (ARCHIVE_OK);
Packit Service 1d0348
			break;
Packit Service 1d0348
		case BZ_FINISH_OK:  /* Finishing: There's more work to do */
Packit Service 1d0348
			break;
Packit Service 1d0348
		case BZ_STREAM_END: /* Finishing: all done */
Packit Service 1d0348
			/* Only occurs in finishing case */
Packit Service 1d0348
			return (ARCHIVE_OK);
Packit Service 1d0348
		default:
Packit Service 1d0348
			/* Any other return value indicates an error */
Packit Service 1d0348
			archive_set_error(f->archive,
Packit Service 1d0348
			    ARCHIVE_ERRNO_PROGRAMMER,
Packit Service 1d0348
			    "Bzip2 compression failed;"
Packit Service 1d0348
			    " BZ2_bzCompress() returned %d",
Packit Service 1d0348
			    ret);
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
#else /* HAVE_BZLIB_H && BZ_CONFIG_ERROR */
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_compressor_bzip2_open(struct archive_write_filter *f)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *data = (struct private_data *)f->data;
Packit Service 1d0348
	struct archive_string as;
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	archive_string_init(&as);
Packit Service 1d0348
	archive_strcpy(&as, "bzip2");
Packit Service 1d0348
Packit Service 1d0348
	/* Specify compression level. */
Packit Service 1d0348
	if (data->compression_level > 0) {
Packit Service 1d0348
		archive_strcat(&as, " -");
Packit Service 1d0348
		archive_strappend_char(&as, '0' + data->compression_level);
Packit Service 1d0348
	}
Packit Service 1d0348
	f->write = archive_compressor_bzip2_write;
Packit Service 1d0348
Packit Service 1d0348
	r = __archive_write_program_open(f, data->pdata, as.s);
Packit Service 1d0348
	archive_string_free(&as);
Packit Service 1d0348
	return (r);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_compressor_bzip2_write(struct archive_write_filter *f, const void *buff,
Packit Service 1d0348
    size_t length)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *data = (struct private_data *)f->data;
Packit Service 1d0348
Packit Service 1d0348
	return __archive_write_program_write(f, data->pdata, buff, length);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_compressor_bzip2_close(struct archive_write_filter *f)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *data = (struct private_data *)f->data;
Packit Service 1d0348
Packit Service 1d0348
	return __archive_write_program_close(f, data->pdata);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_compressor_bzip2_free(struct archive_write_filter *f)
Packit Service 1d0348
{
Packit Service 1d0348
	struct private_data *data = (struct private_data *)f->data;
Packit Service 1d0348
Packit Service 1d0348
	__archive_write_program_free(data->pdata);
Packit Service 1d0348
	free(data);
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
#endif /* HAVE_BZLIB_H && BZ_CONFIG_ERROR */