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