Blame libarchive/archive_write_add_filter_compress.c

Packit 08bd4c
/*-
Packit 08bd4c
 * Copyright (c) 2008 Joerg Sonnenberger
Packit 08bd4c
 * All rights reserved.
Packit 08bd4c
 *
Packit 08bd4c
 * Redistribution and use in source and binary forms, with or without
Packit 08bd4c
 * modification, are permitted provided that the following conditions
Packit 08bd4c
 * are met:
Packit 08bd4c
 * 1. Redistributions of source code must retain the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer.
Packit 08bd4c
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer in the
Packit 08bd4c
 *    documentation and/or other materials provided with the distribution.
Packit 08bd4c
 *
Packit 08bd4c
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit 08bd4c
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 08bd4c
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit 08bd4c
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 08bd4c
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit 08bd4c
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 08bd4c
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 08bd4c
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 08bd4c
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit 08bd4c
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
/*-
Packit 08bd4c
 * Copyright (c) 1985, 1986, 1992, 1993
Packit 08bd4c
 *	The Regents of the University of California.  All rights reserved.
Packit 08bd4c
 *
Packit 08bd4c
 * This code is derived from software contributed to Berkeley by
Packit 08bd4c
 * Diomidis Spinellis and James A. Woods, derived from original
Packit 08bd4c
 * work by Spencer Thomas and Joseph Orost.
Packit 08bd4c
 *
Packit 08bd4c
 * Redistribution and use in source and binary forms, with or without
Packit 08bd4c
 * modification, are permitted provided that the following conditions
Packit 08bd4c
 * are met:
Packit 08bd4c
 * 1. Redistributions of source code must retain the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer.
Packit 08bd4c
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer in the
Packit 08bd4c
 *    documentation and/or other materials provided with the distribution.
Packit 08bd4c
 * 3. Neither the name of the University nor the names of its contributors
Packit 08bd4c
 *    may be used to endorse or promote products derived from this software
Packit 08bd4c
 *    without specific prior written permission.
Packit 08bd4c
 *
Packit 08bd4c
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 08bd4c
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 08bd4c
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 08bd4c
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 08bd4c
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 08bd4c
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 08bd4c
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 08bd4c
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 08bd4c
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 08bd4c
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 08bd4c
 * SUCH DAMAGE.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
#include "archive_platform.h"
Packit 08bd4c
Packit 08bd4c
__FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_compression_compress.c 201111 2009-12-28 03:33:05Z kientzle $");
Packit 08bd4c
Packit 08bd4c
#ifdef HAVE_ERRNO_H
Packit 08bd4c
#include <errno.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_STDLIB_H
Packit 08bd4c
#include <stdlib.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_STRING_H
Packit 08bd4c
#include <string.h>
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
#include "archive.h"
Packit 08bd4c
#include "archive_private.h"
Packit 08bd4c
#include "archive_write_private.h"
Packit 08bd4c
Packit 08bd4c
#define	HSIZE		69001	/* 95% occupancy */
Packit 08bd4c
#define	HSHIFT		8	/* 8 - trunc(log2(HSIZE / 65536)) */
Packit 08bd4c
#define	CHECK_GAP 10000		/* Ratio check interval. */
Packit 08bd4c
Packit 08bd4c
#define	MAXCODE(bits)	((1 << (bits)) - 1)
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * the next two codes should not be changed lightly, as they must not
Packit 08bd4c
 * lie within the contiguous general code space.
Packit 08bd4c
 */
Packit 08bd4c
#define	FIRST	257		/* First free entry. */
Packit 08bd4c
#define	CLEAR	256		/* Table clear output code. */
Packit 08bd4c
Packit 08bd4c
struct private_data {
Packit 08bd4c
	int64_t in_count, out_count, checkpoint;
Packit 08bd4c
Packit 08bd4c
	int code_len;			/* Number of bits/code. */
Packit 08bd4c
	int cur_maxcode;		/* Maximum code, given n_bits. */
Packit 08bd4c
	int max_maxcode;		/* Should NEVER generate this code. */
Packit 08bd4c
	int hashtab [HSIZE];
Packit 08bd4c
	unsigned short codetab [HSIZE];
Packit 08bd4c
	int first_free;		/* First unused entry. */
Packit 08bd4c
	int compress_ratio;
Packit 08bd4c
Packit 08bd4c
	int cur_code, cur_fcode;
Packit 08bd4c
Packit 08bd4c
	int bit_offset;
Packit 08bd4c
	unsigned char bit_buf;
Packit 08bd4c
Packit 08bd4c
	unsigned char	*compressed;
Packit 08bd4c
	size_t		 compressed_buffer_size;
Packit 08bd4c
	size_t		 compressed_offset;
Packit 08bd4c
};
Packit 08bd4c
Packit 08bd4c
static int archive_compressor_compress_open(struct archive_write_filter *);
Packit 08bd4c
static int archive_compressor_compress_write(struct archive_write_filter *,
Packit 08bd4c
		    const void *, size_t);
Packit 08bd4c
static int archive_compressor_compress_close(struct archive_write_filter *);
Packit 08bd4c
static int archive_compressor_compress_free(struct archive_write_filter *);
Packit 08bd4c
Packit 08bd4c
#if ARCHIVE_VERSION_NUMBER < 4000000
Packit 08bd4c
int
Packit 08bd4c
archive_write_set_compression_compress(struct archive *a)
Packit 08bd4c
{
Packit 08bd4c
	__archive_write_filters_free(a);
Packit 08bd4c
	return (archive_write_add_filter_compress(a));
Packit 08bd4c
}
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Add a compress filter to this write handle.
Packit 08bd4c
 */
Packit 08bd4c
int
Packit 08bd4c
archive_write_add_filter_compress(struct archive *_a)
Packit 08bd4c
{
Packit 08bd4c
	struct archive_write *a = (struct archive_write *)_a;
Packit 08bd4c
	struct archive_write_filter *f = __archive_write_allocate_filter(_a);
Packit 08bd4c
Packit 08bd4c
	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
Packit 08bd4c
	    ARCHIVE_STATE_NEW, "archive_write_add_filter_compress");
Packit 08bd4c
	f->open = &archive_compressor_compress_open;
Packit 08bd4c
	f->code = ARCHIVE_FILTER_COMPRESS;
Packit 08bd4c
	f->name = "compress";
Packit 08bd4c
	return (ARCHIVE_OK);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Setup callback.
Packit 08bd4c
 */
Packit 08bd4c
static int
Packit 08bd4c
archive_compressor_compress_open(struct archive_write_filter *f)
Packit 08bd4c
{
Packit 08bd4c
	int ret;
Packit 08bd4c
	struct private_data *state;
Packit 08bd4c
	size_t bs = 65536, bpb;
Packit 08bd4c
Packit 08bd4c
	f->code = ARCHIVE_FILTER_COMPRESS;
Packit 08bd4c
	f->name = "compress";
Packit 08bd4c
Packit 08bd4c
	ret = __archive_write_open_filter(f->next_filter);
Packit 08bd4c
	if (ret != ARCHIVE_OK)
Packit 08bd4c
		return (ret);
Packit 08bd4c
Packit 08bd4c
	state = (struct private_data *)calloc(1, sizeof(*state));
Packit 08bd4c
	if (state == NULL) {
Packit 08bd4c
		archive_set_error(f->archive, ENOMEM,
Packit 08bd4c
		    "Can't allocate data for compression");
Packit 08bd4c
		return (ARCHIVE_FATAL);
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
Packit 08bd4c
		/* Buffer size should be a multiple number of the of bytes
Packit 08bd4c
		 * per block for performance. */
Packit 08bd4c
		bpb = archive_write_get_bytes_per_block(f->archive);
Packit 08bd4c
		if (bpb > bs)
Packit 08bd4c
			bs = bpb;
Packit 08bd4c
		else if (bpb != 0)
Packit 08bd4c
			bs -= bs % bpb;
Packit 08bd4c
	}
Packit 08bd4c
	state->compressed_buffer_size = bs;
Packit 08bd4c
	state->compressed = malloc(state->compressed_buffer_size);
Packit 08bd4c
Packit 08bd4c
	if (state->compressed == NULL) {
Packit 08bd4c
		archive_set_error(f->archive, ENOMEM,
Packit 08bd4c
		    "Can't allocate data for compression buffer");
Packit 08bd4c
		free(state);
Packit 08bd4c
		return (ARCHIVE_FATAL);
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	f->write = archive_compressor_compress_write;
Packit 08bd4c
	f->close = archive_compressor_compress_close;
Packit 08bd4c
	f->free = archive_compressor_compress_free;
Packit 08bd4c
Packit 08bd4c
	state->max_maxcode = 0x10000;	/* Should NEVER generate this code. */
Packit 08bd4c
	state->in_count = 0;		/* Length of input. */
Packit 08bd4c
	state->bit_buf = 0;
Packit 08bd4c
	state->bit_offset = 0;
Packit 08bd4c
	state->out_count = 3;		/* Includes 3-byte header mojo. */
Packit 08bd4c
	state->compress_ratio = 0;
Packit 08bd4c
	state->checkpoint = CHECK_GAP;
Packit 08bd4c
	state->code_len = 9;
Packit 08bd4c
	state->cur_maxcode = MAXCODE(state->code_len);
Packit 08bd4c
	state->first_free = FIRST;
Packit 08bd4c
Packit 08bd4c
	memset(state->hashtab, 0xff, sizeof(state->hashtab));
Packit 08bd4c
Packit 08bd4c
	/* Prime output buffer with a gzip header. */
Packit 08bd4c
	state->compressed[0] = 0x1f; /* Compress */
Packit 08bd4c
	state->compressed[1] = 0x9d;
Packit 08bd4c
	state->compressed[2] = 0x90; /* Block mode, 16bit max */
Packit 08bd4c
	state->compressed_offset = 3;
Packit 08bd4c
Packit 08bd4c
	f->data = state;
Packit 08bd4c
	return (0);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*-
Packit 08bd4c
 * Output the given code.
Packit 08bd4c
 * Inputs:
Packit 08bd4c
 * 	code:	A n_bits-bit integer.  If == -1, then EOF.  This assumes
Packit 08bd4c
 *		that n_bits <= (long)wordsize - 1.
Packit 08bd4c
 * Outputs:
Packit 08bd4c
 * 	Outputs code to the file.
Packit 08bd4c
 * Assumptions:
Packit 08bd4c
 *	Chars are 8 bits long.
Packit 08bd4c
 * Algorithm:
Packit 08bd4c
 * 	Maintain a BITS character long buffer (so that 8 codes will
Packit 08bd4c
 * fit in it exactly).  Use the VAX insv instruction to insert each
Packit 08bd4c
 * code in turn.  When the buffer fills up empty it and start over.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
static const unsigned char rmask[9] =
Packit 08bd4c
	{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
Packit 08bd4c
Packit 08bd4c
static int
Packit 08bd4c
output_byte(struct archive_write_filter *f, unsigned char c)
Packit 08bd4c
{
Packit 08bd4c
	struct private_data *state = f->data;
Packit 08bd4c
Packit 08bd4c
	state->compressed[state->compressed_offset++] = c;
Packit 08bd4c
	++state->out_count;
Packit 08bd4c
Packit 08bd4c
	if (state->compressed_buffer_size == state->compressed_offset) {
Packit 08bd4c
		int ret = __archive_write_filter(f->next_filter,
Packit 08bd4c
		    state->compressed, state->compressed_buffer_size);
Packit 08bd4c
		if (ret != ARCHIVE_OK)
Packit 08bd4c
			return ARCHIVE_FATAL;
Packit 08bd4c
		state->compressed_offset = 0;
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	return ARCHIVE_OK;
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
static int
Packit 08bd4c
output_code(struct archive_write_filter *f, int ocode)
Packit 08bd4c
{
Packit 08bd4c
	struct private_data *state = f->data;
Packit 08bd4c
	int bits, ret, clear_flg, bit_offset;
Packit 08bd4c
Packit 08bd4c
	clear_flg = ocode == CLEAR;
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Since ocode is always >= 8 bits, only need to mask the first
Packit 08bd4c
	 * hunk on the left.
Packit 08bd4c
	 */
Packit 08bd4c
	bit_offset = state->bit_offset % 8;
Packit 08bd4c
	state->bit_buf |= (ocode << bit_offset) & 0xff;
Packit 08bd4c
	output_byte(f, state->bit_buf);
Packit 08bd4c
Packit 08bd4c
	bits = state->code_len - (8 - bit_offset);
Packit 08bd4c
	ocode >>= 8 - bit_offset;
Packit 08bd4c
	/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
Packit 08bd4c
	if (bits >= 8) {
Packit 08bd4c
		output_byte(f, ocode & 0xff);
Packit 08bd4c
		ocode >>= 8;
Packit 08bd4c
		bits -= 8;
Packit 08bd4c
	}
Packit 08bd4c
	/* Last bits. */
Packit 08bd4c
	state->bit_offset += state->code_len;
Packit 08bd4c
	state->bit_buf = ocode & rmask[bits];
Packit 08bd4c
	if (state->bit_offset == state->code_len * 8)
Packit 08bd4c
		state->bit_offset = 0;
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * If the next entry is going to be too big for the ocode size,
Packit 08bd4c
	 * then increase it, if possible.
Packit 08bd4c
	 */
Packit 08bd4c
	if (clear_flg || state->first_free > state->cur_maxcode) {
Packit 08bd4c
	       /*
Packit 08bd4c
		* Write the whole buffer, because the input side won't
Packit 08bd4c
		* discover the size increase until after it has read it.
Packit 08bd4c
		*/
Packit 08bd4c
		if (state->bit_offset > 0) {
Packit 08bd4c
			while (state->bit_offset < state->code_len * 8) {
Packit 08bd4c
				ret = output_byte(f, state->bit_buf);
Packit 08bd4c
				if (ret != ARCHIVE_OK)
Packit 08bd4c
					return ret;
Packit 08bd4c
				state->bit_offset += 8;
Packit 08bd4c
				state->bit_buf = 0;
Packit 08bd4c
			}
Packit 08bd4c
		}
Packit 08bd4c
		state->bit_buf = 0;
Packit 08bd4c
		state->bit_offset = 0;
Packit 08bd4c
Packit 08bd4c
		if (clear_flg) {
Packit 08bd4c
			state->code_len = 9;
Packit 08bd4c
			state->cur_maxcode = MAXCODE(state->code_len);
Packit 08bd4c
		} else {
Packit 08bd4c
			state->code_len++;
Packit 08bd4c
			if (state->code_len == 16)
Packit 08bd4c
				state->cur_maxcode = state->max_maxcode;
Packit 08bd4c
			else
Packit 08bd4c
				state->cur_maxcode = MAXCODE(state->code_len);
Packit 08bd4c
		}
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	return (ARCHIVE_OK);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
static int
Packit 08bd4c
output_flush(struct archive_write_filter *f)
Packit 08bd4c
{
Packit 08bd4c
	struct private_data *state = f->data;
Packit 08bd4c
	int ret;
Packit 08bd4c
Packit 08bd4c
	/* At EOF, write the rest of the buffer. */
Packit 08bd4c
	if (state->bit_offset % 8) {
Packit 08bd4c
		state->code_len = (state->bit_offset % 8 + 7) / 8;
Packit 08bd4c
		ret = output_byte(f, state->bit_buf);
Packit 08bd4c
		if (ret != ARCHIVE_OK)
Packit 08bd4c
			return ret;
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	return (ARCHIVE_OK);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Write data to the compressed stream.
Packit 08bd4c
 */
Packit 08bd4c
static int
Packit 08bd4c
archive_compressor_compress_write(struct archive_write_filter *f,
Packit 08bd4c
    const void *buff, size_t length)
Packit 08bd4c
{
Packit 08bd4c
	struct private_data *state = (struct private_data *)f->data;
Packit 08bd4c
	int i;
Packit 08bd4c
	int ratio;
Packit 08bd4c
	int c, disp, ret;
Packit 08bd4c
	const unsigned char *bp;
Packit 08bd4c
Packit 08bd4c
	if (length == 0)
Packit 08bd4c
		return ARCHIVE_OK;
Packit 08bd4c
Packit 08bd4c
	bp = buff;
Packit 08bd4c
Packit 08bd4c
	if (state->in_count == 0) {
Packit 08bd4c
		state->cur_code = *bp++;
Packit 08bd4c
		++state->in_count;
Packit 08bd4c
		--length;
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	while (length--) {
Packit 08bd4c
		c = *bp++;
Packit 08bd4c
		state->in_count++;
Packit 08bd4c
		state->cur_fcode = (c << 16) + state->cur_code;
Packit 08bd4c
		i = ((c << HSHIFT) ^ state->cur_code);	/* Xor hashing. */
Packit 08bd4c
Packit 08bd4c
		if (state->hashtab[i] == state->cur_fcode) {
Packit 08bd4c
			state->cur_code = state->codetab[i];
Packit 08bd4c
			continue;
Packit 08bd4c
		}
Packit 08bd4c
		if (state->hashtab[i] < 0)	/* Empty slot. */
Packit 08bd4c
			goto nomatch;
Packit 08bd4c
		/* Secondary hash (after G. Knott). */
Packit 08bd4c
		if (i == 0)
Packit 08bd4c
			disp = 1;
Packit 08bd4c
		else
Packit 08bd4c
			disp = HSIZE - i;
Packit 08bd4c
 probe:
Packit 08bd4c
		if ((i -= disp) < 0)
Packit 08bd4c
			i += HSIZE;
Packit 08bd4c
Packit 08bd4c
		if (state->hashtab[i] == state->cur_fcode) {
Packit 08bd4c
			state->cur_code = state->codetab[i];
Packit 08bd4c
			continue;
Packit 08bd4c
		}
Packit 08bd4c
		if (state->hashtab[i] >= 0)
Packit 08bd4c
			goto probe;
Packit 08bd4c
 nomatch:
Packit 08bd4c
		ret = output_code(f, state->cur_code);
Packit 08bd4c
		if (ret != ARCHIVE_OK)
Packit 08bd4c
			return ret;
Packit 08bd4c
		state->cur_code = c;
Packit 08bd4c
		if (state->first_free < state->max_maxcode) {
Packit 08bd4c
			state->codetab[i] = state->first_free++;	/* code -> hashtable */
Packit 08bd4c
			state->hashtab[i] = state->cur_fcode;
Packit 08bd4c
			continue;
Packit 08bd4c
		}
Packit 08bd4c
		if (state->in_count < state->checkpoint)
Packit 08bd4c
			continue;
Packit 08bd4c
Packit 08bd4c
		state->checkpoint = state->in_count + CHECK_GAP;
Packit 08bd4c
Packit 08bd4c
		if (state->in_count <= 0x007fffff && state->out_count != 0)
Packit 08bd4c
			ratio = (int)(state->in_count * 256 / state->out_count);
Packit 08bd4c
		else if ((ratio = (int)(state->out_count / 256)) == 0)
Packit 08bd4c
			ratio = 0x7fffffff;
Packit 08bd4c
		else
Packit 08bd4c
			ratio = (int)(state->in_count / ratio);
Packit 08bd4c
Packit 08bd4c
		if (ratio > state->compress_ratio)
Packit 08bd4c
			state->compress_ratio = ratio;
Packit 08bd4c
		else {
Packit 08bd4c
			state->compress_ratio = 0;
Packit 08bd4c
			memset(state->hashtab, 0xff, sizeof(state->hashtab));
Packit 08bd4c
			state->first_free = FIRST;
Packit 08bd4c
			ret = output_code(f, CLEAR);
Packit 08bd4c
			if (ret != ARCHIVE_OK)
Packit 08bd4c
				return ret;
Packit 08bd4c
		}
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	return (ARCHIVE_OK);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Finish the compression...
Packit 08bd4c
 */
Packit 08bd4c
static int
Packit 08bd4c
archive_compressor_compress_close(struct archive_write_filter *f)
Packit 08bd4c
{
Packit 08bd4c
	struct private_data *state = (struct private_data *)f->data;
Packit 08bd4c
	int ret, ret2;
Packit 08bd4c
Packit 08bd4c
	ret = output_code(f, state->cur_code);
Packit 08bd4c
	if (ret != ARCHIVE_OK)
Packit 08bd4c
		goto cleanup;
Packit 08bd4c
	ret = output_flush(f);
Packit 08bd4c
	if (ret != ARCHIVE_OK)
Packit 08bd4c
		goto cleanup;
Packit 08bd4c
Packit 08bd4c
	/* Write the last block */
Packit 08bd4c
	ret = __archive_write_filter(f->next_filter,
Packit 08bd4c
	    state->compressed, state->compressed_offset);
Packit 08bd4c
cleanup:
Packit 08bd4c
	ret2 = __archive_write_close_filter(f->next_filter);
Packit 08bd4c
	if (ret > ret2)
Packit 08bd4c
		ret = ret2;
Packit 08bd4c
	free(state->compressed);
Packit 08bd4c
	free(state);
Packit 08bd4c
	return (ret);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
static int
Packit 08bd4c
archive_compressor_compress_free(struct archive_write_filter *f)
Packit 08bd4c
{
Packit 08bd4c
	(void)f; /* UNUSED */
Packit 08bd4c
	return (ARCHIVE_OK);
Packit 08bd4c
}