xz/src/liblzma/lz/lz_encoder.c

510 lines
14 KiB
C
Raw Normal View History

2007-12-08 22:42:33 +00:00
///////////////////////////////////////////////////////////////////////////////
//
/// \file lz_encoder.c
/// \brief LZ in window
//
// Copyright (C) 1999-2008 Igor Pavlov
// Copyright (C) 2007-2008 Lasse Collin
2007-12-08 22:42:33 +00:00
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////
#include "lz_encoder.h"
#include "lz_encoder_hash.h"
2007-12-08 22:42:33 +00:00
struct lzma_coder_s {
/// LZ-based encoder e.g. LZMA
lzma_lz_encoder lz;
2007-12-08 22:42:33 +00:00
/// History buffer and match finder
lzma_mf mf;
2007-12-08 22:42:33 +00:00
/// Next coder in the chain
lzma_next_coder next;
};
2007-12-08 22:42:33 +00:00
/// \brief Moves the data in the input window to free space for new data
///
/// mf->buffer is a sliding input window, which keeps mf->keep_size_before
/// bytes of input history available all the time. Now and then we need to
/// "slide" the buffer to make space for the new data to the end of the
/// buffer. At the same time, data older than keep_size_before is dropped.
///
static void
move_window(lzma_mf *mf)
2007-12-08 22:42:33 +00:00
{
// Align the move to a multiple of 16 bytes. Some LZ-based encoders
// like LZMA use the lowest bits of mf->read_pos to know the
// alignment of the uncompressed data. We also get better speed
// for memmove() with aligned buffers.
assert(mf->read_pos > mf->keep_size_before);
const uint32_t move_offset
= (mf->read_pos - mf->keep_size_before) & ~UINT32_C(15);
2007-12-08 22:42:33 +00:00
assert(mf->write_pos > move_offset);
const size_t move_size = mf->write_pos - move_offset;
2007-12-08 22:42:33 +00:00
assert(move_offset + move_size <= mf->size);
2007-12-08 22:42:33 +00:00
memmove(mf->buffer, mf->buffer + move_offset, move_size);
2007-12-08 22:42:33 +00:00
mf->offset += move_offset;
mf->read_pos -= move_offset;
mf->read_limit -= move_offset;
mf->write_pos -= move_offset;
return;
}
2007-12-08 22:42:33 +00:00
/// \brief Tries to fill the input window (mf->buffer)
///
/// If we are the last encoder in the chain, our input data is in in[].
/// Otherwise we call the next filter in the chain to process in[] and
/// write its output to mf->buffer.
///
/// This function must not be called once it has returned LZMA_STREAM_END.
///
static lzma_ret
fill_window(lzma_coder *coder, lzma_allocator *allocator, const uint8_t *in,
size_t *in_pos, size_t in_size, lzma_action action)
{
assert(coder->mf.read_pos <= coder->mf.write_pos);
2007-12-08 22:42:33 +00:00
// Move the sliding window if needed.
if (coder->mf.read_pos >= coder->mf.size - coder->mf.keep_size_after)
move_window(&coder->mf);
2007-12-08 22:42:33 +00:00
size_t in_used;
lzma_ret ret;
if (coder->next.code == NULL) {
// Not using a filter, simply memcpy() as much as possible.
in_used = lzma_bufcpy(in, in_pos, in_size, coder->mf.buffer,
&coder->mf.write_pos, coder->mf.size);
2007-12-08 22:42:33 +00:00
ret = action != LZMA_RUN && *in_pos == in_size
? LZMA_STREAM_END : LZMA_OK;
2007-12-08 22:42:33 +00:00
} else {
const size_t in_start = *in_pos;
ret = coder->next.code(coder->next.coder, allocator,
in, in_pos, in_size,
coder->mf.buffer, &coder->mf.write_pos,
coder->mf.size, action);
in_used = *in_pos - in_start;
}
2007-12-08 22:42:33 +00:00
// If end of stream has been reached or flushing completed, we allow
// the encoder to process all the input (that is, read_pos is allowed
// to reach write_pos). Otherwise we keep keep_size_after bytes
// available as prebuffer.
if (ret == LZMA_STREAM_END) {
assert(*in_pos == in_size);
ret = LZMA_OK;
coder->mf.action = action;
coder->mf.read_limit = coder->mf.write_pos;
2007-12-08 22:42:33 +00:00
} else if (coder->mf.write_pos > coder->mf.keep_size_after) {
// This needs to be done conditionally, because if we got
// only little new input, there may be too little input
// to do any encoding yet.
coder->mf.read_limit = coder->mf.write_pos
- coder->mf.keep_size_after;
2007-12-08 22:42:33 +00:00
}
// Restart the match finder after finished LZMA_SYNC_FLUSH.
if (coder->mf.pending > 0
&& coder->mf.read_pos < coder->mf.read_limit) {
// Match finder may update coder->pending and expects it to
// start from zero, so use a temporary variable.
const size_t pending = coder->mf.pending;
coder->mf.pending = 0;
2007-12-08 22:42:33 +00:00
// Rewind read_pos so that the match finder can hash
// the pending bytes.
assert(coder->mf.read_pos >= pending);
coder->mf.read_pos -= pending;
2007-12-08 22:42:33 +00:00
// Call the skip function directly instead of using
// lz_dict_skip(), since we don't want to touch
// mf->read_ahead.
coder->mf.skip(&coder->mf, pending);
}
2007-12-08 22:42:33 +00:00
return ret;
}
2007-12-08 22:42:33 +00:00
static lzma_ret
lz_encode(lzma_coder *coder, lzma_allocator *allocator,
const uint8_t *restrict in, size_t *restrict in_pos,
size_t in_size,
uint8_t *restrict out, size_t *restrict out_pos,
size_t out_size, lzma_action action)
{
while (*out_pos < out_size
&& (*in_pos < in_size || action != LZMA_RUN)) {
// Read more data to coder->mf.buffer if needed.
if (coder->mf.action == LZMA_RUN && coder->mf.read_pos
>= coder->mf.read_limit)
return_if_error(fill_window(coder, allocator,
in, in_pos, in_size, action));
// Encode
const lzma_ret ret = coder->lz.code(coder->lz.coder,
&coder->mf, out, out_pos, out_size);
if (ret != LZMA_OK) {
// Setting this to LZMA_RUN for cases when we are
// flushing. It doesn't matter when finishing or if
// an error occurred.
coder->mf.action = LZMA_RUN;
return ret;
2007-12-08 22:42:33 +00:00
}
}
return LZMA_OK;
}
static bool
lz_encoder_prepare(lzma_mf *mf, lzma_allocator *allocator,
const lzma_lz_options *lz_options)
{
if (lz_options->dictionary_size < LZMA_DICTIONARY_SIZE_MIN
|| lz_options->dictionary_size
> LZMA_DICTIONARY_SIZE_MAX
|| lz_options->find_len_max
> lz_options->match_len_max)
return true;
mf->keep_size_before = lz_options->before_size
+ lz_options->dictionary_size;
2007-12-08 22:42:33 +00:00
mf->keep_size_after = lz_options->after_size
+ lz_options->match_len_max;
2007-12-08 22:42:33 +00:00
// To avoid constant memmove()s, allocate some extra space. Since
// memmove()s become more expensive when the size of the buffer
// increases, we reserve more space when a large dictionary is
// used to make the memmove() calls rarer.
uint32_t reserve = lz_options->dictionary_size / 2;
if (reserve > (UINT32_C(1) << 30))
reserve /= 2;
2007-12-08 22:42:33 +00:00
reserve += (lz_options->before_size + lz_options->match_len_max
+ lz_options->after_size) / 2 + (UINT32_C(1) << 19);
2007-12-08 22:42:33 +00:00
const uint32_t old_size = mf->size;
mf->size = mf->keep_size_before + reserve + mf->keep_size_after;
// FIXME Integer overflows
// Deallocate the old history buffer if it exists but has different
// size than what is needed now.
if (mf->buffer != NULL && old_size != mf->size) {
lzma_free(mf->buffer, allocator);
mf->buffer = NULL;
}
// Match finder options
mf->match_len_max = lz_options->match_len_max;
mf->find_len_max = lz_options->find_len_max;
mf->cyclic_buffer_size = lz_options->dictionary_size + 1;
// Validate the match finder ID and setup the function pointers.
switch (lz_options->match_finder) {
#ifdef HAVE_MF_HC3
2007-12-08 22:42:33 +00:00
case LZMA_MF_HC3:
mf->find = &lzma_mf_hc3_find;
mf->skip = &lzma_mf_hc3_skip;
2007-12-08 22:42:33 +00:00
break;
#endif
#ifdef HAVE_MF_HC4
2007-12-08 22:42:33 +00:00
case LZMA_MF_HC4:
mf->find = &lzma_mf_hc4_find;
mf->skip = &lzma_mf_hc4_skip;
2007-12-08 22:42:33 +00:00
break;
#endif
#ifdef HAVE_MF_BT2
2007-12-08 22:42:33 +00:00
case LZMA_MF_BT2:
mf->find = &lzma_mf_bt2_find;
mf->skip = &lzma_mf_bt2_skip;
2007-12-08 22:42:33 +00:00
break;
#endif
#ifdef HAVE_MF_BT3
2007-12-08 22:42:33 +00:00
case LZMA_MF_BT3:
mf->find = &lzma_mf_bt3_find;
mf->skip = &lzma_mf_bt3_skip;
2007-12-08 22:42:33 +00:00
break;
#endif
#ifdef HAVE_MF_BT4
2007-12-08 22:42:33 +00:00
case LZMA_MF_BT4:
mf->find = &lzma_mf_bt4_find;
mf->skip = &lzma_mf_bt4_skip;
2007-12-08 22:42:33 +00:00
break;
#endif
2007-12-08 22:42:33 +00:00
default:
return true;
2007-12-08 22:42:33 +00:00
}
// Calculate the sizes of mf->hash and mf->son.
const uint32_t hash_bytes = lz_options->match_finder & 0x0F;
const bool is_bt = (lz_options->match_finder & 0x10) != 0;
uint32_t hs;
2007-12-08 22:42:33 +00:00
if (hash_bytes == 2) {
hs = 0xFFFF;
} else {
// Round dictionary size up to the next 2^n - 1 so it can
// be used as a hash mask.
hs = lz_options->dictionary_size - 1;
hs |= hs >> 1;
hs |= hs >> 2;
hs |= hs >> 4;
hs |= hs >> 8;
hs >>= 1;
hs |= 0xFFFF;
2007-12-08 22:42:33 +00:00
if (hs > (UINT32_C(1) << 24)) {
if (hash_bytes == 3)
hs = (UINT32_C(1) << 24) - 1;
else
hs >>= 1;
2007-12-08 22:42:33 +00:00
}
}
2007-12-08 22:42:33 +00:00
mf->hash_mask = hs;
++hs;
if (hash_bytes > 2)
hs += HASH_2_SIZE;
if (hash_bytes > 3)
hs += HASH_3_SIZE;
/*
No match finder uses this at the moment.
if (mf->hash_bytes > 4)
hs += HASH_4_SIZE;
*/
const uint32_t old_count = mf->hash_size_sum + mf->sons_count;
mf->hash_size_sum = hs;
mf->sons_count = mf->cyclic_buffer_size;
if (is_bt)
mf->sons_count *= 2;
const uint32_t new_count = mf->hash_size_sum + mf->sons_count;
// Deallocate the old hash array if it exists and has different size
// than what is needed now.
if (mf->hash != NULL && old_count != new_count) {
lzma_free(mf->hash, allocator);
mf->hash = NULL;
}
2007-12-08 22:42:33 +00:00
// Maximum number of match finder cycles
mf->loops = lz_options->match_finder_cycles;
if (mf->loops == 0) {
mf->loops = 16 + (lz_options->find_len_max / 2);
if (!is_bt)
mf->loops /= 2;
2007-12-08 22:42:33 +00:00
}
return false;
}
2007-12-08 22:42:33 +00:00
static bool
lz_encoder_init(lzma_mf *mf, lzma_allocator *allocator)
{
// Allocate the history buffer.
if (mf->buffer == NULL) {
mf->buffer = lzma_alloc(mf->size, allocator);
if (mf->buffer == NULL)
return true;
2007-12-08 22:42:33 +00:00
}
// Use cyclic_buffer_size as initial mf->offset. This allows
// avoiding a few branches in the match finders. The downside is
// that match finder needs to be normalized more often, which may
// hurt performance with huge dictionaries.
mf->offset = mf->cyclic_buffer_size;
mf->read_pos = 0;
mf->read_ahead = 0;
mf->read_limit = 0;
mf->write_pos = 0;
mf->pending = 0;
2007-12-08 22:42:33 +00:00
// Allocate match finder's hash array.
const size_t alloc_count = mf->hash_size_sum + mf->sons_count;
2007-12-08 22:42:33 +00:00
#if UINT32_MAX >= SIZE_MAX / 4
// Check for integer overflow. (Huge dictionaries are not
// possible on 32-bit CPU.)
if (alloc_count > SIZE_MAX / sizeof(uint32_t))
return true;
#endif
if (mf->hash == NULL) {
mf->hash = lzma_alloc(alloc_count * sizeof(uint32_t),
allocator);
if (mf->hash == NULL)
return true;
2007-12-08 22:42:33 +00:00
}
mf->son = mf->hash + mf->hash_size_sum;
mf->cyclic_buffer_pos = 0;
// Initialize the hash table. Since EMPTY_HASH_VALUE is zero, we
// can use memset().
/*
for (uint32_t i = 0; i < hash_size_sum; ++i)
mf->hash[i] = EMPTY_HASH_VALUE;
*/
memzero(mf->hash, (size_t)(mf->hash_size_sum) * sizeof(uint32_t));
// We don't need to initialize mf->son, but not doing that will
// make Valgrind complain in normalization (see normalize() in
// lz_encoder_mf.c).
//
// Skipping this initialization is *very* good when big dictionary is
// used but only small amount of data gets actually compressed: most
// of the mf->hash won't get actually allocated by the kernel, so
// we avoid wasting RAM and improve initialization speed a lot.
//memzero(mf->son, (size_t)(mf->sons_count) * sizeof(uint32_t));
mf->action = LZMA_RUN;
2007-12-08 22:42:33 +00:00
return false;
2007-12-08 22:42:33 +00:00
}
extern uint64_t
lzma_lz_encoder_memusage(const lzma_lz_options *lz_options)
2007-12-08 22:42:33 +00:00
{
// Old buffers must not exist when calling lz_encoder_prepare().
lzma_mf mf = {
.buffer = NULL,
.hash = NULL,
};
// Setup the size information into mf.
if (lz_encoder_prepare(&mf, NULL, lz_options))
return UINT64_MAX;
// Calculate the memory usage.
return (uint64_t)(mf.hash_size_sum + mf.sons_count)
* sizeof(uint32_t)
+ (uint64_t)(mf.size) + sizeof(lzma_coder);
2007-12-08 22:42:33 +00:00
}
static void
lz_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
2007-12-08 22:42:33 +00:00
{
lzma_next_end(&coder->next, allocator);
2007-12-08 22:42:33 +00:00
lzma_free(coder->mf.hash, allocator);
lzma_free(coder->mf.buffer, allocator);
2007-12-08 22:42:33 +00:00
if (coder->lz.end != NULL)
coder->lz.end(coder->lz.coder, allocator);
else
lzma_free(coder->lz.coder, allocator);
2007-12-08 22:42:33 +00:00
lzma_free(coder, allocator);
2007-12-08 22:42:33 +00:00
return;
}
extern lzma_ret
lzma_lz_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
const lzma_filter_info *filters,
lzma_ret (*lz_init)(lzma_lz_encoder *lz,
lzma_allocator *allocator, const void *options,
lzma_lz_options *lz_options))
2007-12-08 22:42:33 +00:00
{
// Allocate and initialize the base data structure.
if (next->coder == NULL) {
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
if (next->coder == NULL)
return LZMA_MEM_ERROR;
2007-12-08 22:42:33 +00:00
next->code = &lz_encode;
next->end = &lz_encoder_end;
2007-12-08 22:42:33 +00:00
next->coder->lz.coder = NULL;
next->coder->lz.code = NULL;
next->coder->lz.end = NULL;
2007-12-08 22:42:33 +00:00
next->coder->mf.buffer = NULL;
next->coder->mf.hash = NULL;
2007-12-08 22:42:33 +00:00
next->coder->next = LZMA_NEXT_CODER_INIT;
2007-12-08 22:42:33 +00:00
}
// Initialize the LZ-based encoder.
lzma_lz_options lz_options;
return_if_error(lz_init(&next->coder->lz, allocator,
filters[0].options, &lz_options));
// Setup the size information into next->coder->mf and deallocate
// old buffers if they have wrong size.
if (lz_encoder_prepare(&next->coder->mf, allocator, &lz_options))
return LZMA_HEADER_ERROR;
// Allocate new buffers if needed, and do the rest of
// the initialization.
if (lz_encoder_init(&next->coder->mf, allocator))
return LZMA_MEM_ERROR;
// Initialize the next filter in the chain, if any.
return lzma_next_filter_init(&next->coder->next, allocator,
filters + 1);
2007-12-08 22:42:33 +00:00
}
extern LZMA_API lzma_bool
lzma_mf_is_supported(lzma_match_finder mf)
2007-12-08 22:42:33 +00:00
{
bool ret = false;
#ifdef HAVE_MF_HC3
if (mf == LZMA_MF_HC3)
ret = true;
#endif
2007-12-08 22:42:33 +00:00
#ifdef HAVE_MF_HC4
if (mf == LZMA_MF_HC4)
ret = true;
#endif
2007-12-08 22:42:33 +00:00
#ifdef HAVE_MF_BT2
if (mf == LZMA_MF_BT2)
ret = true;
#endif
2007-12-08 22:42:33 +00:00
#ifdef HAVE_MF_BT3
if (mf == LZMA_MF_BT3)
ret = true;
#endif
2007-12-08 22:42:33 +00:00
#ifdef HAVE_MF_BT4
if (mf == LZMA_MF_BT4)
ret = true;
#endif
2007-12-08 22:42:33 +00:00
return ret;
2007-12-08 22:42:33 +00:00
}