2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file lz_encoder.h
|
|
|
|
/// \brief LZ in window and match finder API
|
2009-04-13 10:27:40 +02:00
|
|
|
///
|
|
|
|
// Authors: Igor Pavlov
|
|
|
|
// Lasse Collin
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef LZMA_LZ_ENCODER_H
|
|
|
|
#define LZMA_LZ_ENCODER_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// A table of these is used by the LZ-based encoder to hold
|
|
|
|
/// the length-distance pairs found by the match finder.
|
|
|
|
typedef struct {
|
|
|
|
uint32_t len;
|
|
|
|
uint32_t dist;
|
|
|
|
} lzma_match;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
typedef struct lzma_mf_s lzma_mf;
|
|
|
|
struct lzma_mf_s {
|
2007-12-08 23:42:33 +01:00
|
|
|
///////////////
|
|
|
|
// In Window //
|
|
|
|
///////////////
|
|
|
|
|
|
|
|
/// Pointer to buffer with data to be compressed
|
|
|
|
uint8_t *buffer;
|
|
|
|
|
|
|
|
/// Total size of the allocated buffer (that is, including all
|
|
|
|
/// the extra space)
|
2008-08-28 21:53:15 +02:00
|
|
|
uint32_t size;
|
|
|
|
|
|
|
|
/// Number of bytes that must be kept available in our input history.
|
|
|
|
/// That is, once keep_size_before bytes have been processed,
|
|
|
|
/// buffer[read_pos - keep_size_before] is the oldest byte that
|
|
|
|
/// must be available for reading.
|
|
|
|
uint32_t keep_size_before;
|
|
|
|
|
|
|
|
/// Number of bytes that must be kept in buffer after read_pos.
|
|
|
|
/// That is, read_pos <= write_pos - keep_size_after as long as
|
2008-09-02 10:45:39 +02:00
|
|
|
/// action is LZMA_RUN; when action != LZMA_RUN, read_pos is allowed
|
|
|
|
/// to reach write_pos so that the last bytes get encoded too.
|
2008-08-28 21:53:15 +02:00
|
|
|
uint32_t keep_size_after;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
/// Match finders store locations of matches using 32-bit integers.
|
|
|
|
/// To avoid adjusting several megabytes of integers every time the
|
2008-09-02 10:45:39 +02:00
|
|
|
/// input window is moved with move_window, we only adjust the
|
|
|
|
/// offset of the buffer. Thus, buffer[value_in_hash_table - offset]
|
|
|
|
/// is the byte pointed by value_in_hash_table.
|
2008-08-28 21:53:15 +02:00
|
|
|
uint32_t offset;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-09-02 10:45:39 +02:00
|
|
|
/// buffer[read_pos] is the next byte to run through the match
|
|
|
|
/// finder. This is incremented in the match finder once the byte
|
|
|
|
/// has been processed.
|
2008-08-28 21:53:15 +02:00
|
|
|
uint32_t read_pos;
|
|
|
|
|
|
|
|
/// Number of bytes that have been ran through the match finder, but
|
|
|
|
/// which haven't been encoded by the LZ-based encoder yet.
|
|
|
|
uint32_t read_ahead;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
/// As long as read_pos is less than read_limit, there is enough
|
|
|
|
/// input available in buffer for at least one encoding loop.
|
|
|
|
///
|
|
|
|
/// Because of the stateful API, read_limit may and will get greater
|
|
|
|
/// than read_pos quite often. This is taken into account when
|
|
|
|
/// calculating the value for keep_size_after.
|
2008-08-28 21:53:15 +02:00
|
|
|
uint32_t read_limit;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
/// buffer[write_pos] is the first byte that doesn't contain valid
|
|
|
|
/// uncompressed data; that is, the next input byte will be copied
|
|
|
|
/// to buffer[write_pos].
|
2008-08-28 21:53:15 +02:00
|
|
|
uint32_t write_pos;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-04-24 17:38:00 +02:00
|
|
|
/// Number of bytes not hashed before read_pos. This is needed to
|
|
|
|
/// restart the match finder after LZMA_SYNC_FLUSH.
|
2008-08-28 21:53:15 +02:00
|
|
|
uint32_t pending;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
//////////////////
|
|
|
|
// Match Finder //
|
|
|
|
//////////////////
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Find matches. Returns the number of distance-length pairs written
|
2008-09-06 22:42:50 +02:00
|
|
|
/// to the matches array. This is called only via lzma_mf_find().
|
2008-08-28 21:53:15 +02:00
|
|
|
uint32_t (*find)(lzma_mf *mf, lzma_match *matches);
|
|
|
|
|
|
|
|
/// Skips num bytes. This is like find() but doesn't make the
|
|
|
|
/// distance-length pairs available, thus being a little faster.
|
2008-09-06 22:42:50 +02:00
|
|
|
/// This is called only via mf_skip().
|
2008-08-28 21:53:15 +02:00
|
|
|
void (*skip)(lzma_mf *mf, uint32_t num);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
uint32_t *hash;
|
|
|
|
uint32_t *son;
|
2008-09-02 10:45:39 +02:00
|
|
|
uint32_t cyclic_pos;
|
|
|
|
uint32_t cyclic_size; // Must be dictionary size + 1.
|
2007-12-08 23:42:33 +01:00
|
|
|
uint32_t hash_mask;
|
2008-08-28 21:53:15 +02:00
|
|
|
|
|
|
|
/// Maximum number of loops in the match finder
|
2008-09-27 18:09:21 +02:00
|
|
|
uint32_t depth;
|
2008-08-28 21:53:15 +02:00
|
|
|
|
|
|
|
/// Maximum length of a match that the match finder will try to find.
|
2008-09-27 18:09:21 +02:00
|
|
|
uint32_t nice_len;
|
2008-08-28 21:53:15 +02:00
|
|
|
|
|
|
|
/// Maximum length of a match supported by the LZ-based encoder.
|
2008-09-27 18:09:21 +02:00
|
|
|
/// If the longest match found by the match finder is nice_len,
|
2008-09-06 22:42:50 +02:00
|
|
|
/// mf_find() tries to expand it up to match_len_max bytes.
|
2008-08-28 21:53:15 +02:00
|
|
|
uint32_t match_len_max;
|
|
|
|
|
|
|
|
/// When running out of input, binary tree match finders need to know
|
|
|
|
/// if it is due to flushing or finishing. The action is used also
|
|
|
|
/// by the LZ-based encoders themselves.
|
|
|
|
lzma_action action;
|
|
|
|
|
|
|
|
/// Number of elements in hash[]
|
2007-12-08 23:42:33 +01:00
|
|
|
uint32_t hash_size_sum;
|
2008-08-28 21:53:15 +02:00
|
|
|
|
|
|
|
/// Number of elements in son[]
|
|
|
|
uint32_t sons_count;
|
2007-12-08 23:42:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
typedef struct {
|
|
|
|
/// Extra amount of data to keep available before the "actual"
|
|
|
|
/// dictionary.
|
|
|
|
size_t before_size;
|
|
|
|
|
|
|
|
/// Size of the history buffer
|
2008-09-27 18:09:21 +02:00
|
|
|
size_t dict_size;
|
2008-08-28 21:53:15 +02:00
|
|
|
|
|
|
|
/// Extra amount of data to keep available after the "actual"
|
|
|
|
/// dictionary.
|
|
|
|
size_t after_size;
|
|
|
|
|
|
|
|
/// Maximum length of a match that the LZ-based encoder can accept.
|
2008-09-27 18:09:21 +02:00
|
|
|
/// This is used to extend matches of length nice_len to the
|
2008-08-28 21:53:15 +02:00
|
|
|
/// maximum possible length.
|
|
|
|
size_t match_len_max;
|
|
|
|
|
|
|
|
/// Match finder will search matches of at maximum of this length.
|
|
|
|
/// This must be less than or equal to match_len_max.
|
2008-09-27 18:09:21 +02:00
|
|
|
size_t nice_len;
|
2008-08-28 21:53:15 +02:00
|
|
|
|
|
|
|
/// Type of the match finder to use
|
|
|
|
lzma_match_finder match_finder;
|
|
|
|
|
2008-09-27 18:09:21 +02:00
|
|
|
/// Maximum search depth
|
|
|
|
uint32_t depth;
|
2008-08-28 21:53:15 +02:00
|
|
|
|
|
|
|
/// TODO: Comment
|
2008-09-27 18:09:21 +02:00
|
|
|
const uint8_t *preset_dict;
|
2008-08-28 21:53:15 +02:00
|
|
|
|
2008-09-27 18:09:21 +02:00
|
|
|
uint32_t preset_dict_size;
|
2008-08-28 21:53:15 +02:00
|
|
|
|
|
|
|
} lzma_lz_options;
|
|
|
|
|
|
|
|
|
|
|
|
// The total usable buffer space at any moment outside the match finder:
|
2008-09-27 18:09:21 +02:00
|
|
|
// before_size + dict_size + after_size + match_len_max
|
2008-08-28 21:53:15 +02:00
|
|
|
//
|
|
|
|
// In reality, there's some extra space allocated to prevent the number of
|
2008-09-27 18:09:21 +02:00
|
|
|
// memmove() calls reasonable. The bigger the dict_size is, the bigger
|
2008-08-28 21:53:15 +02:00
|
|
|
// this extra buffer will be since with bigger dictionaries memmove() would
|
|
|
|
// also take longer.
|
|
|
|
//
|
|
|
|
// A single encoder loop in the LZ-based encoder may call the match finder
|
2008-09-06 22:42:50 +02:00
|
|
|
// (mf_find() or mf_skip()) at maximum of after_size times.
|
|
|
|
// In other words, a single encoder loop may advance lzma_mf.read_pos at
|
2008-08-28 21:53:15 +02:00
|
|
|
// maximum of after_size times. Since matches are looked up to
|
2008-09-06 22:42:50 +02:00
|
|
|
// lzma_mf.buffer[lzma_mf.read_pos + match_len_max - 1], the total
|
2008-09-27 18:09:21 +02:00
|
|
|
// amount of extra buffer needed after dict_size becomes
|
2008-08-28 21:53:15 +02:00
|
|
|
// after_size + match_len_max.
|
|
|
|
//
|
|
|
|
// before_size has two uses. The first one is to keep literals available
|
|
|
|
// in cases when the LZ-based encoder has made some read ahead.
|
|
|
|
// TODO: Maybe this could be changed by making the LZ-based encoders to
|
|
|
|
// store the actual literals as they do with length-distance pairs.
|
|
|
|
//
|
|
|
|
// Alrogithms such as LZMA2 first try to compress a chunk, and then check
|
|
|
|
// if the encoded result is smaller than the uncompressed one. If the chunk
|
|
|
|
// was uncompressible, it is better to store it in uncompressed form in
|
|
|
|
// the output stream. To do this, the whole uncompressed chunk has to be
|
|
|
|
// still available in the history buffer. before_size achieves that.
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
/// Data specific to the LZ-based encoder
|
|
|
|
lzma_coder *coder;
|
|
|
|
|
|
|
|
/// Function to encode from *dict to out[]
|
|
|
|
lzma_ret (*code)(lzma_coder *restrict coder,
|
|
|
|
lzma_mf *restrict mf, uint8_t *restrict out,
|
|
|
|
size_t *restrict out_pos, size_t out_size);
|
|
|
|
|
|
|
|
/// Free allocated resources
|
|
|
|
void (*end)(lzma_coder *coder, lzma_allocator *allocator);
|
|
|
|
|
|
|
|
} lzma_lz_encoder;
|
|
|
|
|
|
|
|
|
|
|
|
// Basic steps:
|
|
|
|
// 1. Input gets copied into the dictionary.
|
|
|
|
// 2. Data in dictionary gets run through the match finder byte by byte.
|
|
|
|
// 3. The literals and matches are encoded using e.g. LZMA.
|
|
|
|
//
|
|
|
|
// The bytes that have been ran through the match finder, but not encoded yet,
|
|
|
|
// are called `read ahead'.
|
|
|
|
|
|
|
|
|
|
|
|
/// Get pointer to the first byte not ran through the match finder
|
|
|
|
static inline const uint8_t *
|
|
|
|
mf_ptr(const lzma_mf *mf)
|
|
|
|
{
|
|
|
|
return mf->buffer + mf->read_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Get the number of bytes that haven't been ran through the match finder yet.
|
|
|
|
static inline uint32_t
|
|
|
|
mf_avail(const lzma_mf *mf)
|
|
|
|
{
|
|
|
|
return mf->write_pos - mf->read_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Get the number of bytes that haven't been encoded yet (some of these
|
|
|
|
/// bytes may have been ran through the match finder though).
|
|
|
|
static inline uint32_t
|
|
|
|
mf_unencoded(const lzma_mf *mf)
|
|
|
|
{
|
|
|
|
return mf->write_pos - mf->read_pos - mf->read_ahead;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Calculate the absolute offset from the beginning of the most recent
|
|
|
|
/// dictionary reset. Only the lowest four bits are important, so there's no
|
|
|
|
/// problem that we don't know the 64-bit size of the data encoded so far.
|
|
|
|
///
|
|
|
|
/// NOTE: When moving the input window, we need to do it so that the lowest
|
|
|
|
/// bits of dict->read_pos are not modified to keep this macro working
|
|
|
|
/// as intended.
|
|
|
|
static inline uint32_t
|
|
|
|
mf_position(const lzma_mf *mf)
|
|
|
|
{
|
|
|
|
return mf->read_pos - mf->read_ahead;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Since everything else begins with mf_, use it also for lzma_mf_find().
|
|
|
|
#define mf_find lzma_mf_find
|
|
|
|
|
|
|
|
|
|
|
|
/// Skip the given number of bytes. This is used when a good match was found.
|
|
|
|
/// For example, if mf_find() finds a match of 200 bytes long, the first byte
|
|
|
|
/// of that match was already consumed by mf_find(), and the rest 199 bytes
|
|
|
|
/// have to be skipped with mf_skip(mf, 199).
|
|
|
|
static inline void
|
|
|
|
mf_skip(lzma_mf *mf, uint32_t amount)
|
|
|
|
{
|
|
|
|
if (amount != 0) {
|
|
|
|
mf->skip(mf, amount);
|
|
|
|
mf->read_ahead += amount;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
2008-08-28 21:53:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Copies at maximum of *left amount of bytes from the history buffer
|
|
|
|
/// to out[]. This is needed by LZMA2 to encode uncompressed chunks.
|
|
|
|
static inline void
|
|
|
|
mf_read(lzma_mf *mf, uint8_t *out, size_t *out_pos, size_t out_size,
|
|
|
|
size_t *left)
|
|
|
|
{
|
|
|
|
const size_t out_avail = out_size - *out_pos;
|
|
|
|
const size_t copy_size = MIN(out_avail, *left);
|
|
|
|
|
|
|
|
assert(mf->read_ahead == 0);
|
|
|
|
assert(mf->read_pos >= *left);
|
|
|
|
|
|
|
|
memcpy(out + *out_pos, mf->buffer + mf->read_pos - *left,
|
|
|
|
copy_size);
|
|
|
|
|
|
|
|
*out_pos += copy_size;
|
|
|
|
*left -= copy_size;
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
|
|
extern uint64_t lzma_lz_encoder_memusage(const lzma_lz_options *lz_options);
|
|
|
|
|
|
|
|
|
|
|
|
// These are only for LZ encoder's internal use.
|
|
|
|
extern uint32_t lzma_mf_find(
|
|
|
|
lzma_mf *mf, uint32_t *count, lzma_match *matches);
|
|
|
|
|
|
|
|
extern uint32_t lzma_mf_hc3_find(lzma_mf *dict, lzma_match *matches);
|
|
|
|
extern void lzma_mf_hc3_skip(lzma_mf *dict, uint32_t amount);
|
|
|
|
|
|
|
|
extern uint32_t lzma_mf_hc4_find(lzma_mf *dict, lzma_match *matches);
|
|
|
|
extern void lzma_mf_hc4_skip(lzma_mf *dict, uint32_t amount);
|
|
|
|
|
|
|
|
extern uint32_t lzma_mf_bt2_find(lzma_mf *dict, lzma_match *matches);
|
|
|
|
extern void lzma_mf_bt2_skip(lzma_mf *dict, uint32_t amount);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
extern uint32_t lzma_mf_bt3_find(lzma_mf *dict, lzma_match *matches);
|
|
|
|
extern void lzma_mf_bt3_skip(lzma_mf *dict, uint32_t amount);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
extern uint32_t lzma_mf_bt4_find(lzma_mf *dict, lzma_match *matches);
|
|
|
|
extern void lzma_mf_bt4_skip(lzma_mf *dict, uint32_t amount);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
#endif
|