archive_write_set_compression_compress.c [plain text]
#include "archive_platform.h"
__FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_compression_compress.c 201111 2009-12-28 03:33:05Z kientzle $");
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "archive.h"
#include "archive_private.h"
#include "archive_write_private.h"
#define HSIZE 69001
#define HSHIFT 8
#define CHECK_GAP 10000
#define MAXCODE(bits) ((1 << (bits)) - 1)
#define FIRST 257
#define CLEAR 256
struct private_data {
off_t in_count, out_count, checkpoint;
int code_len;
int cur_maxcode;
int max_maxcode;
int hashtab [HSIZE];
unsigned short codetab [HSIZE];
int first_free;
int compress_ratio;
int cur_code, cur_fcode;
int bit_offset;
unsigned char bit_buf;
unsigned char *compressed;
size_t compressed_buffer_size;
size_t compressed_offset;
};
static int archive_compressor_compress_finish(struct archive_write *);
static int archive_compressor_compress_init(struct archive_write *);
static int archive_compressor_compress_write(struct archive_write *,
const void *, size_t);
int
archive_write_set_compression_compress(struct archive *_a)
{
struct archive_write *a = (struct archive_write *)_a;
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
ARCHIVE_STATE_NEW, "archive_write_set_compression_compress");
a->compressor.init = &archive_compressor_compress_init;
a->archive.compression_code = ARCHIVE_COMPRESSION_COMPRESS;
a->archive.compression_name = "compress";
return (ARCHIVE_OK);
}
static int
archive_compressor_compress_init(struct archive_write *a)
{
int ret;
struct private_data *state;
a->archive.compression_code = ARCHIVE_COMPRESSION_COMPRESS;
a->archive.compression_name = "compress";
if (a->bytes_per_block < 4) {
archive_set_error(&a->archive, EINVAL,
"Can't write Compress header as single block");
return (ARCHIVE_FATAL);
}
if (a->client_opener != NULL) {
ret = (a->client_opener)(&a->archive, a->client_data);
if (ret != ARCHIVE_OK)
return (ret);
}
state = (struct private_data *)malloc(sizeof(*state));
if (state == NULL) {
archive_set_error(&a->archive, ENOMEM,
"Can't allocate data for compression");
return (ARCHIVE_FATAL);
}
memset(state, 0, sizeof(*state));
state->compressed_buffer_size = a->bytes_per_block;
state->compressed = malloc(state->compressed_buffer_size);
if (state->compressed == NULL) {
archive_set_error(&a->archive, ENOMEM,
"Can't allocate data for compression buffer");
free(state);
return (ARCHIVE_FATAL);
}
a->compressor.write = archive_compressor_compress_write;
a->compressor.finish = archive_compressor_compress_finish;
state->max_maxcode = 0x10000;
state->in_count = 0;
state->bit_buf = 0;
state->bit_offset = 0;
state->out_count = 3;
state->compress_ratio = 0;
state->checkpoint = CHECK_GAP;
state->code_len = 9;
state->cur_maxcode = MAXCODE(state->code_len);
state->first_free = FIRST;
memset(state->hashtab, 0xff, sizeof(state->hashtab));
state->compressed[0] = 0x1f;
state->compressed[1] = 0x9d;
state->compressed[2] = 0x90;
state->compressed_offset = 3;
a->compressor.data = state;
return (0);
}
static unsigned char rmask[9] =
{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
static int
output_byte(struct archive_write *a, unsigned char c)
{
struct private_data *state = a->compressor.data;
ssize_t bytes_written;
state->compressed[state->compressed_offset++] = c;
++state->out_count;
if (state->compressed_buffer_size == state->compressed_offset) {
bytes_written = (a->client_writer)(&a->archive,
a->client_data,
state->compressed, state->compressed_buffer_size);
if (bytes_written <= 0)
return ARCHIVE_FATAL;
a->archive.raw_position += bytes_written;
state->compressed_offset = 0;
}
return ARCHIVE_OK;
}
static int
output_code(struct archive_write *a, int ocode)
{
struct private_data *state = a->compressor.data;
int bits, ret, clear_flg, bit_offset;
clear_flg = ocode == CLEAR;
bit_offset = state->bit_offset % 8;
state->bit_buf |= (ocode << bit_offset) & 0xff;
output_byte(a, state->bit_buf);
bits = state->code_len - (8 - bit_offset);
ocode >>= 8 - bit_offset;
if (bits >= 8) {
output_byte(a, ocode & 0xff);
ocode >>= 8;
bits -= 8;
}
state->bit_offset += state->code_len;
state->bit_buf = ocode & rmask[bits];
if (state->bit_offset == state->code_len * 8)
state->bit_offset = 0;
if (clear_flg || state->first_free > state->cur_maxcode) {
if (state->bit_offset > 0) {
while (state->bit_offset < state->code_len * 8) {
ret = output_byte(a, state->bit_buf);
if (ret != ARCHIVE_OK)
return ret;
state->bit_offset += 8;
state->bit_buf = 0;
}
}
state->bit_buf = 0;
state->bit_offset = 0;
if (clear_flg) {
state->code_len = 9;
state->cur_maxcode = MAXCODE(state->code_len);
} else {
state->code_len++;
if (state->code_len == 16)
state->cur_maxcode = state->max_maxcode;
else
state->cur_maxcode = MAXCODE(state->code_len);
}
}
return (ARCHIVE_OK);
}
static int
output_flush(struct archive_write *a)
{
struct private_data *state = a->compressor.data;
int ret;
if (state->bit_offset % 8) {
state->code_len = (state->bit_offset % 8 + 7) / 8;
ret = output_byte(a, state->bit_buf);
if (ret != ARCHIVE_OK)
return ret;
}
return (ARCHIVE_OK);
}
static int
archive_compressor_compress_write(struct archive_write *a, const void *buff,
size_t length)
{
struct private_data *state;
int i;
int ratio;
int c, disp, ret;
const unsigned char *bp;
state = (struct private_data *)a->compressor.data;
if (a->client_writer == NULL) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
"No write callback is registered? "
"This is probably an internal programming error.");
return (ARCHIVE_FATAL);
}
if (length == 0)
return ARCHIVE_OK;
bp = buff;
if (state->in_count == 0) {
state->cur_code = *bp++;
++state->in_count;
--length;
}
while (length--) {
c = *bp++;
state->in_count++;
state->cur_fcode = (c << 16) + state->cur_code;
i = ((c << HSHIFT) ^ state->cur_code);
if (state->hashtab[i] == state->cur_fcode) {
state->cur_code = state->codetab[i];
continue;
}
if (state->hashtab[i] < 0)
goto nomatch;
if (i == 0)
disp = 1;
else
disp = HSIZE - i;
probe:
if ((i -= disp) < 0)
i += HSIZE;
if (state->hashtab[i] == state->cur_fcode) {
state->cur_code = state->codetab[i];
continue;
}
if (state->hashtab[i] >= 0)
goto probe;
nomatch:
ret = output_code(a, state->cur_code);
if (ret != ARCHIVE_OK)
return ret;
state->cur_code = c;
if (state->first_free < state->max_maxcode) {
state->codetab[i] = state->first_free++;
state->hashtab[i] = state->cur_fcode;
continue;
}
if (state->in_count < state->checkpoint)
continue;
state->checkpoint = state->in_count + CHECK_GAP;
if (state->in_count <= 0x007fffff)
ratio = state->in_count * 256 / state->out_count;
else if ((ratio = state->out_count / 256) == 0)
ratio = 0x7fffffff;
else
ratio = state->in_count / ratio;
if (ratio > state->compress_ratio)
state->compress_ratio = ratio;
else {
state->compress_ratio = 0;
memset(state->hashtab, 0xff, sizeof(state->hashtab));
state->first_free = FIRST;
ret = output_code(a, CLEAR);
if (ret != ARCHIVE_OK)
return ret;
}
}
return (ARCHIVE_OK);
}
static int
archive_compressor_compress_finish(struct archive_write *a)
{
ssize_t block_length, target_block_length, bytes_written;
int ret;
struct private_data *state;
size_t tocopy;
state = (struct private_data *)a->compressor.data;
if (a->client_writer == NULL) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
"No write callback is registered? "
"This is probably an internal programming error.");
ret = ARCHIVE_FATAL;
goto cleanup;
}
if (a->pad_uncompressed) {
while (state->in_count % a->bytes_per_block != 0) {
tocopy = a->bytes_per_block -
(state->in_count % a->bytes_per_block);
if (tocopy > a->null_length)
tocopy = a->null_length;
ret = archive_compressor_compress_write(a, a->nulls,
tocopy);
if (ret != ARCHIVE_OK)
goto cleanup;
}
}
ret = output_code(a, state->cur_code);
if (ret != ARCHIVE_OK)
goto cleanup;
ret = output_flush(a);
if (ret != ARCHIVE_OK)
goto cleanup;
block_length = state->compressed_offset;
if (a->bytes_in_last_block <= 0)
target_block_length = a->bytes_per_block;
else
target_block_length = a->bytes_in_last_block *
( (block_length + a->bytes_in_last_block - 1) /
a->bytes_in_last_block);
if (target_block_length > a->bytes_per_block)
target_block_length = a->bytes_per_block;
if (block_length < target_block_length) {
memset(state->compressed + state->compressed_offset, 0,
target_block_length - block_length);
block_length = target_block_length;
}
bytes_written = (a->client_writer)(&a->archive, a->client_data,
state->compressed, block_length);
if (bytes_written <= 0)
ret = ARCHIVE_FATAL;
else
a->archive.raw_position += bytes_written;
cleanup:
free(state->compressed);
free(state);
return (ret);
}