1228753Smm/*- 2228753Smm * Copyright (c) 2008 Joerg Sonnenberger 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm 26228753Smm/*- 27228753Smm * Copyright (c) 1985, 1986, 1992, 1993 28228753Smm * The Regents of the University of California. All rights reserved. 29228753Smm * 30228753Smm * This code is derived from software contributed to Berkeley by 31228753Smm * Diomidis Spinellis and James A. Woods, derived from original 32228753Smm * work by Spencer Thomas and Joseph Orost. 33228753Smm * 34228753Smm * Redistribution and use in source and binary forms, with or without 35228753Smm * modification, are permitted provided that the following conditions 36228753Smm * are met: 37228753Smm * 1. Redistributions of source code must retain the above copyright 38228753Smm * notice, this list of conditions and the following disclaimer. 39228753Smm * 2. Redistributions in binary form must reproduce the above copyright 40228753Smm * notice, this list of conditions and the following disclaimer in the 41228753Smm * documentation and/or other materials provided with the distribution. 42228753Smm * 3. Neither the name of the University nor the names of its contributors 43228753Smm * may be used to endorse or promote products derived from this software 44228753Smm * without specific prior written permission. 45228753Smm * 46228753Smm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 47228753Smm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 48228753Smm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 49228753Smm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 50228753Smm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 51228753Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 52228753Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 53228753Smm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 54228753Smm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 55228753Smm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 56228753Smm * SUCH DAMAGE. 57228753Smm */ 58228753Smm 59228753Smm#include "archive_platform.h" 60228753Smm 61228753Smm__FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_compression_compress.c 201111 2009-12-28 03:33:05Z kientzle $"); 62228753Smm 63228753Smm#ifdef HAVE_ERRNO_H 64228753Smm#include <errno.h> 65228753Smm#endif 66228753Smm#ifdef HAVE_STDLIB_H 67228753Smm#include <stdlib.h> 68228753Smm#endif 69228753Smm#ifdef HAVE_STRING_H 70228753Smm#include <string.h> 71228753Smm#endif 72228753Smm 73228753Smm#include "archive.h" 74228753Smm#include "archive_private.h" 75228753Smm#include "archive_write_private.h" 76228753Smm 77228753Smm#define HSIZE 69001 /* 95% occupancy */ 78228753Smm#define HSHIFT 8 /* 8 - trunc(log2(HSIZE / 65536)) */ 79228753Smm#define CHECK_GAP 10000 /* Ratio check interval. */ 80228753Smm 81228753Smm#define MAXCODE(bits) ((1 << (bits)) - 1) 82228753Smm 83228753Smm/* 84228753Smm * the next two codes should not be changed lightly, as they must not 85228753Smm * lie within the contiguous general code space. 86228753Smm */ 87228753Smm#define FIRST 257 /* First free entry. */ 88228753Smm#define CLEAR 256 /* Table clear output code. */ 89228753Smm 90228753Smmstruct private_data { 91231200Smm int64_t in_count, out_count, checkpoint; 92228753Smm 93228753Smm int code_len; /* Number of bits/code. */ 94228753Smm int cur_maxcode; /* Maximum code, given n_bits. */ 95228753Smm int max_maxcode; /* Should NEVER generate this code. */ 96228753Smm int hashtab [HSIZE]; 97228753Smm unsigned short codetab [HSIZE]; 98228753Smm int first_free; /* First unused entry. */ 99228753Smm int compress_ratio; 100228753Smm 101228753Smm int cur_code, cur_fcode; 102228753Smm 103228753Smm int bit_offset; 104228753Smm unsigned char bit_buf; 105228753Smm 106228753Smm unsigned char *compressed; 107228753Smm size_t compressed_buffer_size; 108228753Smm size_t compressed_offset; 109228753Smm}; 110228753Smm 111231200Smmstatic int archive_compressor_compress_open(struct archive_write_filter *); 112231200Smmstatic int archive_compressor_compress_write(struct archive_write_filter *, 113228753Smm const void *, size_t); 114231200Smmstatic int archive_compressor_compress_close(struct archive_write_filter *); 115231200Smmstatic int archive_compressor_compress_free(struct archive_write_filter *); 116228753Smm 117231200Smm#if ARCHIVE_VERSION_NUMBER < 4000000 118231200Smmint 119231200Smmarchive_write_set_compression_compress(struct archive *a) 120231200Smm{ 121231200Smm __archive_write_filters_free(a); 122231200Smm return (archive_write_add_filter_compress(a)); 123231200Smm} 124231200Smm#endif 125231200Smm 126228753Smm/* 127231200Smm * Add a compress filter to this write handle. 128228753Smm */ 129228753Smmint 130231200Smmarchive_write_add_filter_compress(struct archive *_a) 131228753Smm{ 132228753Smm struct archive_write *a = (struct archive_write *)_a; 133231200Smm struct archive_write_filter *f = __archive_write_allocate_filter(_a); 134231200Smm 135231200Smm archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC, 136231200Smm ARCHIVE_STATE_NEW, "archive_write_add_filter_compress"); 137231200Smm f->open = &archive_compressor_compress_open; 138248616Smm f->code = ARCHIVE_FILTER_COMPRESS; 139231200Smm f->name = "compress"; 140228753Smm return (ARCHIVE_OK); 141228753Smm} 142228753Smm 143228753Smm/* 144228753Smm * Setup callback. 145228753Smm */ 146228753Smmstatic int 147231200Smmarchive_compressor_compress_open(struct archive_write_filter *f) 148228753Smm{ 149228753Smm struct private_data *state; 150238856Smm size_t bs = 65536, bpb; 151228753Smm 152248616Smm f->code = ARCHIVE_FILTER_COMPRESS; 153231200Smm f->name = "compress"; 154228753Smm 155231200Smm state = (struct private_data *)calloc(1, sizeof(*state)); 156228753Smm if (state == NULL) { 157231200Smm archive_set_error(f->archive, ENOMEM, 158228753Smm "Can't allocate data for compression"); 159228753Smm return (ARCHIVE_FATAL); 160228753Smm } 161228753Smm 162238856Smm if (f->archive->magic == ARCHIVE_WRITE_MAGIC) { 163238856Smm /* Buffer size should be a multiple number of the of bytes 164238856Smm * per block for performance. */ 165238856Smm bpb = archive_write_get_bytes_per_block(f->archive); 166238856Smm if (bpb > bs) 167238856Smm bs = bpb; 168238856Smm else if (bpb != 0) 169238856Smm bs -= bs % bpb; 170238856Smm } 171238856Smm state->compressed_buffer_size = bs; 172228753Smm state->compressed = malloc(state->compressed_buffer_size); 173228753Smm 174228753Smm if (state->compressed == NULL) { 175231200Smm archive_set_error(f->archive, ENOMEM, 176228753Smm "Can't allocate data for compression buffer"); 177228753Smm free(state); 178228753Smm return (ARCHIVE_FATAL); 179228753Smm } 180228753Smm 181231200Smm f->write = archive_compressor_compress_write; 182231200Smm f->close = archive_compressor_compress_close; 183231200Smm f->free = archive_compressor_compress_free; 184228753Smm 185228753Smm state->max_maxcode = 0x10000; /* Should NEVER generate this code. */ 186228753Smm state->in_count = 0; /* Length of input. */ 187228753Smm state->bit_buf = 0; 188228753Smm state->bit_offset = 0; 189228753Smm state->out_count = 3; /* Includes 3-byte header mojo. */ 190228753Smm state->compress_ratio = 0; 191228753Smm state->checkpoint = CHECK_GAP; 192228753Smm state->code_len = 9; 193228753Smm state->cur_maxcode = MAXCODE(state->code_len); 194228753Smm state->first_free = FIRST; 195228753Smm 196228753Smm memset(state->hashtab, 0xff, sizeof(state->hashtab)); 197228753Smm 198228753Smm /* Prime output buffer with a gzip header. */ 199228753Smm state->compressed[0] = 0x1f; /* Compress */ 200228753Smm state->compressed[1] = 0x9d; 201228753Smm state->compressed[2] = 0x90; /* Block mode, 16bit max */ 202228753Smm state->compressed_offset = 3; 203228753Smm 204231200Smm f->data = state; 205228753Smm return (0); 206228753Smm} 207228753Smm 208228753Smm/*- 209228753Smm * Output the given code. 210228753Smm * Inputs: 211228753Smm * code: A n_bits-bit integer. If == -1, then EOF. This assumes 212231200Smm * that n_bits <= (long)wordsize - 1. 213228753Smm * Outputs: 214228753Smm * Outputs code to the file. 215228753Smm * Assumptions: 216228753Smm * Chars are 8 bits long. 217228753Smm * Algorithm: 218228753Smm * Maintain a BITS character long buffer (so that 8 codes will 219228753Smm * fit in it exactly). Use the VAX insv instruction to insert each 220228753Smm * code in turn. When the buffer fills up empty it and start over. 221228753Smm */ 222228753Smm 223231200Smmstatic const unsigned char rmask[9] = 224228753Smm {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; 225228753Smm 226228753Smmstatic int 227231200Smmoutput_byte(struct archive_write_filter *f, unsigned char c) 228228753Smm{ 229231200Smm struct private_data *state = f->data; 230228753Smm 231228753Smm state->compressed[state->compressed_offset++] = c; 232228753Smm ++state->out_count; 233228753Smm 234228753Smm if (state->compressed_buffer_size == state->compressed_offset) { 235231200Smm int ret = __archive_write_filter(f->next_filter, 236228753Smm state->compressed, state->compressed_buffer_size); 237231200Smm if (ret != ARCHIVE_OK) 238228753Smm return ARCHIVE_FATAL; 239228753Smm state->compressed_offset = 0; 240228753Smm } 241228753Smm 242228753Smm return ARCHIVE_OK; 243228753Smm} 244228753Smm 245228753Smmstatic int 246231200Smmoutput_code(struct archive_write_filter *f, int ocode) 247228753Smm{ 248231200Smm struct private_data *state = f->data; 249228753Smm int bits, ret, clear_flg, bit_offset; 250228753Smm 251228753Smm clear_flg = ocode == CLEAR; 252228753Smm 253228753Smm /* 254228753Smm * Since ocode is always >= 8 bits, only need to mask the first 255228753Smm * hunk on the left. 256228753Smm */ 257228753Smm bit_offset = state->bit_offset % 8; 258228753Smm state->bit_buf |= (ocode << bit_offset) & 0xff; 259231200Smm output_byte(f, state->bit_buf); 260228753Smm 261228753Smm bits = state->code_len - (8 - bit_offset); 262228753Smm ocode >>= 8 - bit_offset; 263228753Smm /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */ 264228753Smm if (bits >= 8) { 265231200Smm output_byte(f, ocode & 0xff); 266228753Smm ocode >>= 8; 267228753Smm bits -= 8; 268228753Smm } 269228753Smm /* Last bits. */ 270228753Smm state->bit_offset += state->code_len; 271228753Smm state->bit_buf = ocode & rmask[bits]; 272228753Smm if (state->bit_offset == state->code_len * 8) 273228753Smm state->bit_offset = 0; 274228753Smm 275228753Smm /* 276228753Smm * If the next entry is going to be too big for the ocode size, 277228753Smm * then increase it, if possible. 278228753Smm */ 279228753Smm if (clear_flg || state->first_free > state->cur_maxcode) { 280228753Smm /* 281228753Smm * Write the whole buffer, because the input side won't 282228753Smm * discover the size increase until after it has read it. 283228753Smm */ 284228753Smm if (state->bit_offset > 0) { 285228753Smm while (state->bit_offset < state->code_len * 8) { 286231200Smm ret = output_byte(f, state->bit_buf); 287228753Smm if (ret != ARCHIVE_OK) 288228753Smm return ret; 289228753Smm state->bit_offset += 8; 290228753Smm state->bit_buf = 0; 291228753Smm } 292228753Smm } 293228753Smm state->bit_buf = 0; 294228753Smm state->bit_offset = 0; 295228753Smm 296228753Smm if (clear_flg) { 297228753Smm state->code_len = 9; 298228753Smm state->cur_maxcode = MAXCODE(state->code_len); 299228753Smm } else { 300228753Smm state->code_len++; 301228753Smm if (state->code_len == 16) 302228753Smm state->cur_maxcode = state->max_maxcode; 303228753Smm else 304228753Smm state->cur_maxcode = MAXCODE(state->code_len); 305228753Smm } 306228753Smm } 307228753Smm 308228753Smm return (ARCHIVE_OK); 309228753Smm} 310228753Smm 311228753Smmstatic int 312231200Smmoutput_flush(struct archive_write_filter *f) 313228753Smm{ 314231200Smm struct private_data *state = f->data; 315228753Smm int ret; 316228753Smm 317228753Smm /* At EOF, write the rest of the buffer. */ 318228753Smm if (state->bit_offset % 8) { 319228753Smm state->code_len = (state->bit_offset % 8 + 7) / 8; 320231200Smm ret = output_byte(f, state->bit_buf); 321228753Smm if (ret != ARCHIVE_OK) 322228753Smm return ret; 323228753Smm } 324228753Smm 325228753Smm return (ARCHIVE_OK); 326228753Smm} 327228753Smm 328228753Smm/* 329228753Smm * Write data to the compressed stream. 330228753Smm */ 331228753Smmstatic int 332231200Smmarchive_compressor_compress_write(struct archive_write_filter *f, 333231200Smm const void *buff, size_t length) 334228753Smm{ 335231200Smm struct private_data *state = (struct private_data *)f->data; 336228753Smm int i; 337228753Smm int ratio; 338228753Smm int c, disp, ret; 339228753Smm const unsigned char *bp; 340228753Smm 341228753Smm if (length == 0) 342228753Smm return ARCHIVE_OK; 343228753Smm 344228753Smm bp = buff; 345228753Smm 346228753Smm if (state->in_count == 0) { 347228753Smm state->cur_code = *bp++; 348228753Smm ++state->in_count; 349228753Smm --length; 350228753Smm } 351228753Smm 352228753Smm while (length--) { 353228753Smm c = *bp++; 354228753Smm state->in_count++; 355228753Smm state->cur_fcode = (c << 16) + state->cur_code; 356228753Smm i = ((c << HSHIFT) ^ state->cur_code); /* Xor hashing. */ 357228753Smm 358228753Smm if (state->hashtab[i] == state->cur_fcode) { 359228753Smm state->cur_code = state->codetab[i]; 360228753Smm continue; 361228753Smm } 362228753Smm if (state->hashtab[i] < 0) /* Empty slot. */ 363228753Smm goto nomatch; 364228753Smm /* Secondary hash (after G. Knott). */ 365228753Smm if (i == 0) 366228753Smm disp = 1; 367228753Smm else 368228753Smm disp = HSIZE - i; 369231200Smm probe: 370228753Smm if ((i -= disp) < 0) 371228753Smm i += HSIZE; 372228753Smm 373228753Smm if (state->hashtab[i] == state->cur_fcode) { 374228753Smm state->cur_code = state->codetab[i]; 375228753Smm continue; 376228753Smm } 377228753Smm if (state->hashtab[i] >= 0) 378228753Smm goto probe; 379231200Smm nomatch: 380231200Smm ret = output_code(f, state->cur_code); 381228753Smm if (ret != ARCHIVE_OK) 382228753Smm return ret; 383228753Smm state->cur_code = c; 384228753Smm if (state->first_free < state->max_maxcode) { 385228753Smm state->codetab[i] = state->first_free++; /* code -> hashtable */ 386228753Smm state->hashtab[i] = state->cur_fcode; 387228753Smm continue; 388228753Smm } 389228753Smm if (state->in_count < state->checkpoint) 390228753Smm continue; 391228753Smm 392228753Smm state->checkpoint = state->in_count + CHECK_GAP; 393228753Smm 394238856Smm if (state->in_count <= 0x007fffff && state->out_count != 0) 395238856Smm ratio = (int)(state->in_count * 256 / state->out_count); 396238856Smm else if ((ratio = (int)(state->out_count / 256)) == 0) 397228753Smm ratio = 0x7fffffff; 398228753Smm else 399238856Smm ratio = (int)(state->in_count / ratio); 400228753Smm 401228753Smm if (ratio > state->compress_ratio) 402228753Smm state->compress_ratio = ratio; 403228753Smm else { 404228753Smm state->compress_ratio = 0; 405228753Smm memset(state->hashtab, 0xff, sizeof(state->hashtab)); 406228753Smm state->first_free = FIRST; 407231200Smm ret = output_code(f, CLEAR); 408228753Smm if (ret != ARCHIVE_OK) 409228753Smm return ret; 410228753Smm } 411228753Smm } 412228753Smm 413228753Smm return (ARCHIVE_OK); 414228753Smm} 415228753Smm 416228753Smm 417228753Smm/* 418228753Smm * Finish the compression... 419228753Smm */ 420228753Smmstatic int 421231200Smmarchive_compressor_compress_close(struct archive_write_filter *f) 422228753Smm{ 423231200Smm struct private_data *state = (struct private_data *)f->data; 424358090Smm int ret; 425228753Smm 426231200Smm ret = output_code(f, state->cur_code); 427228753Smm if (ret != ARCHIVE_OK) 428358090Smm return ret; 429231200Smm ret = output_flush(f); 430228753Smm if (ret != ARCHIVE_OK) 431358090Smm return ret; 432228753Smm 433228753Smm /* Write the last block */ 434231200Smm ret = __archive_write_filter(f->next_filter, 435231200Smm state->compressed, state->compressed_offset); 436228753Smm return (ret); 437228753Smm} 438231200Smm 439231200Smmstatic int 440231200Smmarchive_compressor_compress_free(struct archive_write_filter *f) 441231200Smm{ 442358090Smm struct private_data *state = (struct private_data *)f->data; 443358090Smm 444358090Smm free(state->compressed); 445358090Smm free(state); 446231200Smm return (ARCHIVE_OK); 447231200Smm} 448