Rizin
unix-like reverse engineering framework and cli tools
lzma_encoder.c File Reference

LZMA encoder. More...

#include "lzma2_encoder.h"
#include "lzma_encoder_private.h"
#include "fastpos.h"

Go to the source code of this file.

Macros

#define LOOP_INPUT_MAX   (OPTS + 1)
 

Functions

static void literal_matched (lzma_range_encoder *rc, probability *subcoder, uint32_t match_byte, uint32_t symbol)
 
static void literal (lzma_lzma1_encoder *coder, lzma_mf *mf, uint32_t position)
 
static void length_update_prices (lzma_length_encoder *lc, const uint32_t pos_state)
 
static void length (lzma_range_encoder *rc, lzma_length_encoder *lc, const uint32_t pos_state, uint32_t len, const bool fast_mode)
 
static void match (lzma_lzma1_encoder *coder, const uint32_t pos_state, const uint32_t distance, const uint32_t len)
 
static void rep_match (lzma_lzma1_encoder *coder, const uint32_t pos_state, const uint32_t rep, const uint32_t len)
 
static void encode_symbol (lzma_lzma1_encoder *coder, lzma_mf *mf, uint32_t back, uint32_t len, uint32_t position)
 
static bool encode_init (lzma_lzma1_encoder *coder, lzma_mf *mf)
 
static void encode_eopm (lzma_lzma1_encoder *coder, uint32_t position)
 
lzma_ret lzma_lzma_encode (lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf, uint8_t *restrict out, size_t *restrict out_pos, size_t out_size, uint32_t limit)
 
static lzma_ret lzma_encode (void *coder, lzma_mf *restrict mf, uint8_t *restrict out, size_t *restrict out_pos, size_t out_size)
 
static bool is_options_valid (const lzma_options_lzma *options)
 
static void set_lz_options (lzma_lz_options *lz_options, const lzma_options_lzma *options)
 
static void length_encoder_reset (lzma_length_encoder *lencoder, const uint32_t num_pos_states, const bool fast_mode)
 
lzma_ret lzma_lzma_encoder_reset (lzma_lzma1_encoder *coder, const lzma_options_lzma *options)
 
lzma_ret lzma_lzma_encoder_create (void **coder_ptr, const lzma_allocator *allocator, const lzma_options_lzma *options, lzma_lz_options *lz_options)
 
static lzma_ret lzma_encoder_init (lzma_lz_encoder *lz, const lzma_allocator *allocator, const void *options, lzma_lz_options *lz_options)
 
lzma_ret lzma_lzma_encoder_init (lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
 
uint64_t lzma_lzma_encoder_memusage (const void *options)
 
bool lzma_lzma_lclppb_encode (const lzma_options_lzma *options, uint8_t *byte)
 Encodes lc/lp/pb into one byte. Returns false on success and true on error. More...
 
 LZMA_API (lzma_bool)
 

Detailed Description

LZMA encoder.

Definition in file lzma_encoder.c.

Macro Definition Documentation

◆ LOOP_INPUT_MAX

#define LOOP_INPUT_MAX   (OPTS + 1)

Number of bytes that a single encoding loop in lzma_lzma_encode() can consume from the dictionary. This limit comes from lzma_lzma_optimum() and may need to be updated if that function is significantly modified.

Definition at line 308 of file lzma_encoder.c.

Function Documentation

◆ encode_eopm()

static void encode_eopm ( lzma_lzma1_encoder coder,
uint32_t  position 
)
static

Definition at line 296 of file lzma_encoder.c.

297 {
298  const uint32_t pos_state = position & coder->pos_mask;
299  rc_bit(&coder->rc, &coder->is_match[coder->state][pos_state], 1);
300  rc_bit(&coder->rc, &coder->is_rep[coder->state], 0);
301  match(coder, pos_state, UINT32_MAX, MATCH_LEN_MIN);
302 }
#define MATCH_LEN_MIN
Definition: lzma_common.h:150
static void match(lzma_lzma1_encoder *coder, const uint32_t pos_state, const uint32_t distance, const uint32_t len)
Definition: lzma_encoder.c:143
#define rc_bit(prob, action0, action1, seq)
unsigned int uint32_t
Definition: sftypes.h:29
#define UINT32_MAX
uint32_t pos_mask
(1 << pos_bits) - 1
lzma_range_encoder rc
Range encoder.
probability is_rep[STATES]
lzma_lzma_state state
State.
probability is_match[STATES][POS_STATES_MAX]

References lzma_lzma1_encoder_s::is_match, lzma_lzma1_encoder_s::is_rep, match(), MATCH_LEN_MIN, lzma_lzma1_encoder_s::pos_mask, lzma_lzma1_encoder_s::rc, rc_bit, lzma_lzma1_encoder_s::state, and UINT32_MAX.

Referenced by lzma_lzma_encode().

◆ encode_init()

static bool encode_init ( lzma_lzma1_encoder coder,
lzma_mf mf 
)
static

Definition at line 268 of file lzma_encoder.c.

269 {
270  assert(mf_position(mf) == 0);
271 
272  if (mf->read_pos == mf->read_limit) {
273  if (mf->action == LZMA_RUN)
274  return false; // We cannot do anything.
275 
276  // We are finishing (we cannot get here when flushing).
277  assert(mf->write_pos == mf->read_pos);
278  assert(mf->action == LZMA_FINISH);
279  } else {
280  // Do the actual initialization. The first LZMA symbol must
281  // always be a literal.
282  mf_skip(mf, 1);
283  mf->read_ahead = 0;
284  rc_bit(&coder->rc, &coder->is_match[0][0], 0);
285  rc_bittree(&coder->rc, coder->literal[0], 8, mf->buffer[0]);
286  }
287 
288  // Initialization is done (except if empty file).
289  coder->is_initialized = true;
290 
291  return true;
292 }
static uint32_t mf_position(const lzma_mf *mf)
Definition: lz_encoder.h:252
static void mf_skip(lzma_mf *mf, uint32_t amount)
Definition: lz_encoder.h:267
assert(limit<=UINT32_MAX/2)
static void rc_bittree(lzma_range_encoder *rc, probability *probs, uint32_t bit_count, uint32_t symbol)
Definition: range_encoder.h:76
probability literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE]
uint32_t read_pos
Definition: lz_encoder.h:63
uint8_t * buffer
Pointer to buffer with data to be compressed.
Definition: lz_encoder.h:35
lzma_action action
Definition: lz_encoder.h:119
uint32_t read_limit
Definition: lz_encoder.h:75
uint32_t read_ahead
Definition: lz_encoder.h:67
uint32_t write_pos
Definition: lz_encoder.h:80
@ LZMA_FINISH
Finish the coding operation.
Definition: base.h:328
@ LZMA_RUN
Continue coding.
Definition: base.h:251

References lzma_mf_s::action, assert(), lzma_mf_s::buffer, lzma_lzma1_encoder_s::is_initialized, lzma_lzma1_encoder_s::is_match, lzma_lzma1_encoder_s::literal, LZMA_FINISH, LZMA_RUN, mf_position(), mf_skip(), lzma_lzma1_encoder_s::rc, rc_bit, rc_bittree(), lzma_mf_s::read_ahead, lzma_mf_s::read_limit, lzma_mf_s::read_pos, and lzma_mf_s::write_pos.

Referenced by lzma_lzma_encode().

◆ encode_symbol()

static void encode_symbol ( lzma_lzma1_encoder coder,
lzma_mf mf,
uint32_t  back,
uint32_t  len,
uint32_t  position 
)
static

Definition at line 234 of file lzma_encoder.c.

236 {
237  const uint32_t pos_state = position & coder->pos_mask;
238 
239  if (back == UINT32_MAX) {
240  // Literal i.e. eight-bit byte
241  assert(len == 1);
242  rc_bit(&coder->rc,
243  &coder->is_match[coder->state][pos_state], 0);
244  literal(coder, mf, position);
245  } else {
246  // Some type of match
247  rc_bit(&coder->rc,
248  &coder->is_match[coder->state][pos_state], 1);
249 
250  if (back < REPS) {
251  // It's a repeated match i.e. the same distance
252  // has been used earlier.
253  rc_bit(&coder->rc, &coder->is_rep[coder->state], 1);
254  rep_match(coder, pos_state, back, len);
255  } else {
256  // Normal match
257  rc_bit(&coder->rc, &coder->is_rep[coder->state], 0);
258  match(coder, pos_state, back - REPS, len);
259  }
260  }
261 
262  assert(mf->read_ahead >= len);
263  mf->read_ahead -= len;
264 }
size_t len
Definition: 6502dis.c:15
#define REPS
Definition: lzma_common.h:223
static void literal(lzma_lzma1_encoder *coder, lzma_mf *mf, uint32_t position)
Definition: lzma_encoder.c:46
static void rep_match(lzma_lzma1_encoder *coder, const uint32_t pos_state, const uint32_t rep, const uint32_t len)
Definition: lzma_encoder.c:190

References assert(), lzma_lzma1_encoder_s::is_match, lzma_lzma1_encoder_s::is_rep, len, literal(), match(), lzma_lzma1_encoder_s::pos_mask, lzma_lzma1_encoder_s::rc, rc_bit, lzma_mf_s::read_ahead, rep_match(), REPS, lzma_lzma1_encoder_s::state, and UINT32_MAX.

Referenced by lzma_lzma_encode().

◆ is_options_valid()

static bool is_options_valid ( const lzma_options_lzma options)
static

Definition at line 422 of file lzma_encoder.c.

423 {
424  // Validate some of the options. LZ encoder validates nice_len too
425  // but we need a valid value here earlier.
426  return is_lclppb_valid(options)
427  && options->nice_len >= MATCH_LEN_MIN
428  && options->nice_len <= MATCH_LEN_MAX
429  && (options->mode == LZMA_MODE_FAST
430  || options->mode == LZMA_MODE_NORMAL);
431 }
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
@ LZMA_MODE_FAST
Fast compression.
Definition: lzma12.h:139
@ LZMA_MODE_NORMAL
Normal compression.
Definition: lzma12.h:147
#define MATCH_LEN_MAX
Definition: lzma_common.h:168
static bool is_lclppb_valid(const lzma_options_lzma *options)
Validates lc, lp, and pb.
Definition: lzma_common.h:33

References is_lclppb_valid(), LZMA_MODE_FAST, LZMA_MODE_NORMAL, MATCH_LEN_MAX, MATCH_LEN_MIN, and options.

Referenced by lzma_lzma_encoder_memusage(), and lzma_lzma_encoder_reset().

◆ length()

static void length ( lzma_range_encoder rc,
lzma_length_encoder lc,
const uint32_t  pos_state,
uint32_t  len,
const bool  fast_mode 
)
inlinestatic

Definition at line 107 of file lzma_encoder.c.

109 {
111  len -= MATCH_LEN_MIN;
112 
113  if (len < LEN_LOW_SYMBOLS) {
114  rc_bit(rc, &lc->choice, 0);
115  rc_bittree(rc, lc->low[pos_state], LEN_LOW_BITS, len);
116  } else {
117  rc_bit(rc, &lc->choice, 1);
118  len -= LEN_LOW_SYMBOLS;
119 
120  if (len < LEN_MID_SYMBOLS) {
121  rc_bit(rc, &lc->choice2, 0);
122  rc_bittree(rc, lc->mid[pos_state], LEN_MID_BITS, len);
123  } else {
124  rc_bit(rc, &lc->choice2, 1);
125  len -= LEN_MID_SYMBOLS;
126  rc_bittree(rc, lc->high, LEN_HIGH_BITS, len);
127  }
128  }
129 
130  // Only getoptimum uses the prices so don't update the table when
131  // in fast mode.
132  if (!fast_mode)
133  if (--lc->counters[pos_state] == 0)
134  length_update_prices(lc, pos_state);
135 }
#define LEN_LOW_SYMBOLS
Definition: lzma_common.h:159
#define LEN_MID_SYMBOLS
Definition: lzma_common.h:161
#define LEN_MID_BITS
Definition: lzma_common.h:160
#define LEN_LOW_BITS
Definition: lzma_common.h:158
#define LEN_HIGH_BITS
Definition: lzma_common.h:162
static void length_update_prices(lzma_length_encoder *lc, const uint32_t pos_state)
Definition: lzma_encoder.c:78
probability high[LEN_HIGH_SYMBOLS]
probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS]
uint32_t counters[POS_STATES_MAX]
probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS]

References assert(), lzma_length_encoder::choice, lzma_length_encoder::choice2, lzma_length_encoder::counters, lzma_length_encoder::high, len, LEN_HIGH_BITS, LEN_LOW_BITS, LEN_LOW_SYMBOLS, LEN_MID_BITS, LEN_MID_SYMBOLS, length_update_prices(), lzma_length_encoder::low, MATCH_LEN_MAX, MATCH_LEN_MIN, lzma_length_encoder::mid, rc_bit, and rc_bittree().

Referenced by match(), and rep_match().

◆ length_encoder_reset()

static void length_encoder_reset ( lzma_length_encoder lencoder,
const uint32_t  num_pos_states,
const bool  fast_mode 
)
static

Definition at line 453 of file lzma_encoder.c.

455 {
456  bit_reset(lencoder->choice);
457  bit_reset(lencoder->choice2);
458 
459  for (size_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {
460  bittree_reset(lencoder->low[pos_state], LEN_LOW_BITS);
461  bittree_reset(lencoder->mid[pos_state], LEN_MID_BITS);
462  }
463 
464  bittree_reset(lencoder->high, LEN_HIGH_BITS);
465 
466  if (!fast_mode)
467  for (uint32_t pos_state = 0; pos_state < num_pos_states;
468  ++pos_state)
469  length_update_prices(lencoder, pos_state);
470 
471  return;
472 }
#define bittree_reset(probs, bit_levels)
Definition: range_common.h:42
#define bit_reset(prob)
Definition: range_common.h:37

References bit_reset, bittree_reset, lzma_length_encoder::choice, lzma_length_encoder::choice2, lzma_length_encoder::high, LEN_HIGH_BITS, LEN_LOW_BITS, LEN_MID_BITS, length_update_prices(), lzma_length_encoder::low, and lzma_length_encoder::mid.

Referenced by lzma_lzma_encoder_reset().

◆ length_update_prices()

static void length_update_prices ( lzma_length_encoder lc,
const uint32_t  pos_state 
)
static

Definition at line 78 of file lzma_encoder.c.

79 {
80  const uint32_t table_size = lc->table_size;
81  lc->counters[pos_state] = table_size;
82 
83  const uint32_t a0 = rc_bit_0_price(lc->choice);
84  const uint32_t a1 = rc_bit_1_price(lc->choice);
85  const uint32_t b0 = a1 + rc_bit_0_price(lc->choice2);
86  const uint32_t b1 = a1 + rc_bit_1_price(lc->choice2);
87  uint32_t *const prices = lc->prices[pos_state];
88 
89  uint32_t i;
90  for (i = 0; i < table_size && i < LEN_LOW_SYMBOLS; ++i)
91  prices[i] = a0 + rc_bittree_price(lc->low[pos_state],
92  LEN_LOW_BITS, i);
93 
94  for (; i < table_size && i < LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS; ++i)
95  prices[i] = b0 + rc_bittree_price(lc->mid[pos_state],
97 
98  for (; i < table_size; ++i)
99  prices[i] = b1 + rc_bittree_price(lc->high, LEN_HIGH_BITS,
101 
102  return;
103 }
lzma_index ** i
Definition: index.h:629
a0
Definition: insn-good.s.cs:704
static uint32_t rc_bittree_price(const probability *const probs, const uint32_t bit_levels, uint32_t symbol)
Definition: price.h:52
static uint32_t rc_bit_0_price(const probability prob)
Definition: price.h:37
static uint32_t rc_bit_1_price(const probability prob)
Definition: price.h:44
uint32_t prices[POS_STATES_MAX][LEN_SYMBOLS]

References a0, b1, lzma_length_encoder::choice, lzma_length_encoder::choice2, lzma_length_encoder::counters, lzma_length_encoder::high, i, LEN_HIGH_BITS, LEN_LOW_BITS, LEN_LOW_SYMBOLS, LEN_MID_BITS, LEN_MID_SYMBOLS, lzma_length_encoder::low, lzma_length_encoder::mid, lzma_length_encoder::prices, rc_bit_0_price(), rc_bit_1_price(), rc_bittree_price(), and lzma_length_encoder::table_size.

Referenced by length(), and length_encoder_reset().

◆ literal()

static void literal ( lzma_lzma1_encoder coder,
lzma_mf mf,
uint32_t  position 
)
inlinestatic

Definition at line 46 of file lzma_encoder.c.

47 {
48  // Locate the literal byte to be encoded and the subcoder.
49  const uint8_t cur_byte = mf->buffer[
50  mf->read_pos - mf->read_ahead];
51  probability *subcoder = literal_subcoder(coder->literal,
53  position, mf->buffer[mf->read_pos - mf->read_ahead - 1]);
54 
55  if (is_literal_state(coder->state)) {
56  // Previous LZMA-symbol was a literal. Encode a normal
57  // literal without a match byte.
58  rc_bittree(&coder->rc, subcoder, 8, cur_byte);
59  } else {
60  // Previous LZMA-symbol was a match. Use the last byte of
61  // the match as a "match byte". That is, compare the bits
62  // of the current literal and the match byte.
63  const uint8_t match_byte = mf->buffer[
64  mf->read_pos - coder->reps[0] - 1
65  - mf->read_ahead];
66  literal_matched(&coder->rc, subcoder, match_byte, cur_byte);
67  }
68 
69  update_literal(coder->state);
70 }
#define update_literal(state)
Indicate that the latest state was a literal.
Definition: lzma_common.h:80
#define is_literal_state(state)
Test if the previous state was a literal.
Definition: lzma_common.h:100
#define literal_subcoder(probs, lc, lp_mask, pos, prev_byte)
Definition: lzma_common.h:124
static void literal_matched(lzma_range_encoder *rc, probability *subcoder, uint32_t match_byte, uint32_t symbol)
Definition: lzma_encoder.c:24
uint16_t probability
Type of probabilities used with range coder.
Definition: range_common.h:69
unsigned char uint8_t
Definition: sftypes.h:31
uint32_t reps[REPS]
The four most recent match distances.

References lzma_mf_s::buffer, is_literal_state, lzma_lzma1_encoder_s::literal, lzma_lzma1_encoder_s::literal_context_bits, literal_matched(), lzma_lzma1_encoder_s::literal_pos_mask, literal_subcoder, lzma_lzma1_encoder_s::rc, rc_bittree(), lzma_mf_s::read_ahead, lzma_mf_s::read_pos, lzma_lzma1_encoder_s::reps, lzma_lzma1_encoder_s::state, and update_literal.

Referenced by encode_symbol().

◆ literal_matched()

static void literal_matched ( lzma_range_encoder rc,
probability subcoder,
uint32_t  match_byte,
uint32_t  symbol 
)
inlinestatic

Definition at line 24 of file lzma_encoder.c.

26 {
27  uint32_t offset = 0x100;
28  symbol += UINT32_C(1) << 8;
29 
30  do {
31  match_byte <<= 1;
32  const uint32_t match_bit = match_byte & offset;
33  const uint32_t subcoder_index
34  = offset + match_bit + (symbol >> 8);
35  const uint32_t bit = (symbol >> 7) & 1;
36  rc_bit(rc, &subcoder[subcoder_index], bit);
37 
38  symbol <<= 1;
39  offset &= ~(match_byte ^ symbol);
40 
41  } while (symbol < (UINT32_C(1) << 16));
42 }
RzCryptoSelector bit
Definition: crypto.c:16
voidpf uLong offset
Definition: ioapi.h:144
#define UINT32_C(val)

References bit, rc_bit, and UINT32_C.

Referenced by literal().

◆ LZMA_API()

LZMA_API ( lzma_bool  )

Definition at line 673 of file lzma_encoder.c.

675 {
676  return mode == LZMA_MODE_FAST || mode == LZMA_MODE_NORMAL;
677 }
const char int mode
Definition: ioapi.h:137

References LZMA_MODE_FAST, and LZMA_MODE_NORMAL.

◆ lzma_encode()

static lzma_ret lzma_encode ( void *  coder,
lzma_mf *restrict  mf,
uint8_t *restrict  out,
size_t *restrict  out_pos,
size_t  out_size 
)
static

Definition at line 405 of file lzma_encoder.c.

408 {
409  // Plain LZMA has no support for sync-flushing.
410  if (unlikely(mf->action == LZMA_SYNC_FLUSH))
411  return LZMA_OPTIONS_ERROR;
412 
413  return lzma_lzma_encode(coder, mf, out, out_pos, out_size, UINT32_MAX);
414 }
const lzma_allocator const uint8_t size_t uint8_t size_t * out_pos
Definition: block.h:528
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define unlikely(expr)
Definition: lz4.c:177
lzma_ret lzma_lzma_encode(lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf, uint8_t *restrict out, size_t *restrict out_pos, size_t out_size, uint32_t limit)
Definition: lzma_encoder.c:312
@ LZMA_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160
@ LZMA_SYNC_FLUSH
Make all the input available at output.
Definition: base.h:265

References lzma_lzma_encode(), LZMA_OPTIONS_ERROR, LZMA_SYNC_FLUSH, out, out_pos, UINT32_MAX, and unlikely.

Referenced by lzma_encoder_init().

◆ lzma_encoder_init()

static lzma_ret lzma_encoder_init ( lzma_lz_encoder lz,
const lzma_allocator allocator,
const void *  options,
lzma_lz_options lz_options 
)
static

Definition at line 609 of file lzma_encoder.c.

611 {
612  lz->code = &lzma_encode;
614  &lz->coder, allocator, options, lz_options);
615 }
const lzma_allocator * allocator
Definition: block.h:377
static lzma_ret lzma_encode(void *coder, lzma_mf *restrict mf, uint8_t *restrict out, size_t *restrict out_pos, size_t out_size)
Definition: lzma_encoder.c:405
lzma_ret lzma_lzma_encoder_create(void **coder_ptr, const lzma_allocator *allocator, const lzma_options_lzma *options, lzma_lz_options *lz_options)
Definition: lzma_encoder.c:549
lzma_ret(* code)(void *coder, lzma_mf *restrict mf, uint8_t *restrict out, size_t *restrict out_pos, size_t out_size)
Function to encode from *dict to out[].
Definition: lz_encoder.h:197
void * coder
Data specific to the LZ-based encoder.
Definition: lz_encoder.h:194

References allocator, lzma_lz_encoder::code, lzma_lz_encoder::coder, lzma_encode(), lzma_lzma_encoder_create(), and options.

Referenced by lzma_lzma_encoder_init().

◆ lzma_lzma_encode()

lzma_ret lzma_lzma_encode ( lzma_lzma1_encoder *restrict  coder,
lzma_mf *restrict  mf,
uint8_t *restrict  out,
size_t *restrict  out_pos,
size_t  out_size,
uint32_t  limit 
)

Definition at line 312 of file lzma_encoder.c.

315 {
316  // Initialize the stream if no data has been encoded yet.
317  if (!coder->is_initialized && !encode_init(coder, mf))
318  return LZMA_OK;
319 
320  // Get the lowest bits of the uncompressed offset from the LZ layer.
321  uint32_t position = mf_position(mf);
322 
323  while (true) {
324  // Encode pending bits, if any. Calling this before encoding
325  // the next symbol is needed only with plain LZMA, since
326  // LZMA2 always provides big enough buffer to flush
327  // everything out from the range encoder. For the same reason,
328  // rc_encode() never returns true when this function is used
329  // as part of LZMA2 encoder.
330  if (rc_encode(&coder->rc, out, out_pos, out_size)) {
331  assert(limit == UINT32_MAX);
332  return LZMA_OK;
333  }
334 
335  // With LZMA2 we need to take care that compressed size of
336  // a chunk doesn't get too big.
337  // FIXME? Check if this could be improved.
338  if (limit != UINT32_MAX
339  && (mf->read_pos - mf->read_ahead >= limit
340  || *out_pos + rc_pending(&coder->rc)
341  >= LZMA2_CHUNK_MAX
342  - LOOP_INPUT_MAX))
343  break;
344 
345  // Check that there is some input to process.
346  if (mf->read_pos >= mf->read_limit) {
347  if (mf->action == LZMA_RUN)
348  return LZMA_OK;
349 
350  if (mf->read_ahead == 0)
351  break;
352  }
353 
354  // Get optimal match (repeat position and length).
355  // Value ranges for pos:
356  // - [0, REPS): repeated match
357  // - [REPS, UINT32_MAX):
358  // match at (pos - REPS)
359  // - UINT32_MAX: not a match but a literal
360  // Value ranges for len:
361  // - [MATCH_LEN_MIN, MATCH_LEN_MAX]
362  uint32_t len;
363  uint32_t back;
364 
365  if (coder->fast_mode)
366  lzma_lzma_optimum_fast(coder, mf, &back, &len);
367  else
369  coder, mf, &back, &len, position);
370 
371  encode_symbol(coder, mf, back, len, position);
372 
373  position += len;
374  }
375 
376  if (!coder->is_flushed) {
377  coder->is_flushed = true;
378 
379  // We don't support encoding plain LZMA streams without EOPM,
380  // and LZMA2 doesn't use EOPM at LZMA level.
381  if (limit == UINT32_MAX)
382  encode_eopm(coder, position);
383 
384  // Flush the remaining bytes from the range encoder.
385  rc_flush(&coder->rc);
386 
387  // Copy the remaining bytes to the output buffer. If there
388  // isn't enough output space, we will copy out the remaining
389  // bytes on the next call to this function by using
390  // the rc_encode() call in the encoding loop above.
391  if (rc_encode(&coder->rc, out, out_pos, out_size)) {
392  assert(limit == UINT32_MAX);
393  return LZMA_OK;
394  }
395  }
396 
397  // Make it ready for the next LZMA2 chunk.
398  coder->is_flushed = false;
399 
400  return LZMA_STREAM_END;
401 }
#define LZMA2_CHUNK_MAX
Maximum number of bytes of actual data per chunk (no headers)
Definition: lzma2_encoder.h:21
#define LOOP_INPUT_MAX
Definition: lzma_encoder.c:308
static void encode_symbol(lzma_lzma1_encoder *coder, lzma_mf *mf, uint32_t back, uint32_t len, uint32_t position)
Definition: lzma_encoder.c:234
static bool encode_init(lzma_lzma1_encoder *coder, lzma_mf *mf)
Definition: lzma_encoder.c:268
static void encode_eopm(lzma_lzma1_encoder *coder, uint32_t position)
Definition: lzma_encoder.c:296
void lzma_lzma_optimum_fast(lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf, uint32_t *restrict back_res, uint32_t *restrict len_res)
void lzma_lzma_optimum_normal(lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf, uint32_t *restrict back_res, uint32_t *restrict len_res, uint32_t position)
static uint32_t const uint8_t uint32_t uint32_t limit
Definition: memcmplen.h:45
static bool rc_encode(lzma_range_encoder *rc, uint8_t *out, size_t *out_pos, size_t out_size)
static uint64_t rc_pending(const lzma_range_encoder *rc)
static void rc_flush(lzma_range_encoder *rc)
bool fast_mode
True if using getoptimumfast.
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58

References assert(), encode_eopm(), encode_init(), encode_symbol(), len, limit, LOOP_INPUT_MAX, LZMA2_CHUNK_MAX, lzma_lzma_optimum_fast(), lzma_lzma_optimum_normal(), LZMA_OK, LZMA_RUN, LZMA_STREAM_END, mf_position(), out, out_pos, rc_encode(), rc_flush(), rc_pending(), and UINT32_MAX.

Referenced by lzma2_encode(), and lzma_encode().

◆ lzma_lzma_encoder_create()

lzma_ret lzma_lzma_encoder_create ( void **  coder_ptr,
const lzma_allocator allocator,
const lzma_options_lzma options,
lzma_lz_options lz_options 
)

Definition at line 549 of file lzma_encoder.c.

552 {
553  // Allocate lzma_lzma1_encoder if it wasn't already allocated.
554  if (*coder_ptr == NULL) {
555  *coder_ptr = lzma_alloc(sizeof(lzma_lzma1_encoder), allocator);
556  if (*coder_ptr == NULL)
557  return LZMA_MEM_ERROR;
558  }
559 
560  lzma_lzma1_encoder *coder = *coder_ptr;
561 
562  // Set compression mode. We haven't validates the options yet,
563  // but it's OK here, since nothing bad happens with invalid
564  // options in the code below, and they will get rejected by
565  // lzma_lzma_encoder_reset() call at the end of this function.
566  switch (options->mode) {
567  case LZMA_MODE_FAST:
568  coder->fast_mode = true;
569  break;
570 
571  case LZMA_MODE_NORMAL: {
572  coder->fast_mode = false;
573 
574  // Set dist_table_size.
575  // Round the dictionary size up to next 2^n.
576  uint32_t log_size = 0;
577  while ((UINT32_C(1) << log_size) < options->dict_size)
578  ++log_size;
579 
580  coder->dist_table_size = log_size * 2;
581 
582  // Length encoders' price table size
584  = options->nice_len + 1 - MATCH_LEN_MIN;
586  = options->nice_len + 1 - MATCH_LEN_MIN;
587  break;
588  }
589 
590  default:
591  return LZMA_OPTIONS_ERROR;
592  }
593 
594  // We don't need to write the first byte as literal if there is
595  // a non-empty preset dictionary. encode_init() wouldn't even work
596  // if there is a non-empty preset dictionary, because encode_init()
597  // assumes that position is zero and previous byte is also zero.
598  coder->is_initialized = options->preset_dict != NULL
599  && options->preset_dict_size > 0;
600  coder->is_flushed = false;
601 
602  set_lz_options(lz_options, options);
603 
604  return lzma_lzma_encoder_reset(coder, options);
605 }
#define NULL
Definition: cris-opc.c:27
static void set_lz_options(lzma_lz_options *lz_options, const lzma_options_lzma *options)
Definition: lzma_encoder.c:435
lzma_ret lzma_lzma_encoder_reset(lzma_lzma1_encoder *coder, const lzma_options_lzma *options)
Definition: lzma_encoder.c:476
lzma_length_encoder match_len_encoder
lzma_length_encoder rep_len_encoder
void * lzma_alloc(size_t size, const lzma_allocator *allocator) lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
Allocates memory.
@ LZMA_MEM_ERROR
Cannot allocate memory.
Definition: base.h:128

References allocator, lzma_lzma1_encoder_s::dist_table_size, lzma_lzma1_encoder_s::fast_mode, lzma_lzma1_encoder_s::is_flushed, lzma_lzma1_encoder_s::is_initialized, lzma_alloc(), lzma_lzma_encoder_reset(), LZMA_MEM_ERROR, LZMA_MODE_FAST, LZMA_MODE_NORMAL, LZMA_OPTIONS_ERROR, lzma_lzma1_encoder_s::match_len_encoder, MATCH_LEN_MIN, NULL, options, lzma_lzma1_encoder_s::rep_len_encoder, set_lz_options(), lzma_length_encoder::table_size, and UINT32_C.

Referenced by lzma2_encoder_init(), and lzma_encoder_init().

◆ lzma_lzma_encoder_init()

lzma_ret lzma_lzma_encoder_init ( lzma_next_coder next,
const lzma_allocator allocator,
const lzma_filter_info filters 
)

Definition at line 619 of file lzma_encoder.c.

621 {
622  return lzma_lz_encoder_init(
624 }
const lzma_filter * filters
Definition: container.h:315
lzma_ret lzma_lz_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters, lzma_ret(*lz_init)(lzma_lz_encoder *lz, const lzma_allocator *allocator, const void *options, lzma_lz_options *lz_options))
Definition: lz_encoder.c:525
static lzma_ret lzma_encoder_init(lzma_lz_encoder *lz, const lzma_allocator *allocator, const void *options, lzma_lz_options *lz_options)
Definition: lzma_encoder.c:609

References allocator, filters, lzma_encoder_init(), and lzma_lz_encoder_init().

Referenced by alone_encoder_init().

◆ lzma_lzma_encoder_memusage()

uint64_t lzma_lzma_encoder_memusage ( const void *  options)

Definition at line 628 of file lzma_encoder.c.

629 {
631  return UINT64_MAX;
632 
633  lzma_lz_options lz_options;
634  set_lz_options(&lz_options, options);
635 
636  const uint64_t lz_memusage = lzma_lz_encoder_memusage(&lz_options);
637  if (lz_memusage == UINT64_MAX)
638  return UINT64_MAX;
639 
640  return (uint64_t)(sizeof(lzma_lzma1_encoder)) + lz_memusage;
641 }
uint64_t lzma_lz_encoder_memusage(const lzma_lz_options *lz_options)
Definition: lz_encoder.c:464
static bool is_options_valid(const lzma_options_lzma *options)
Definition: lzma_encoder.c:422
struct lzma_lzma1_encoder_s lzma_lzma1_encoder
Definition: lzma_encoder.h:20
unsigned long uint64_t
Definition: sftypes.h:28
#define UINT64_MAX

References is_options_valid(), lzma_lz_encoder_memusage(), options, set_lz_options(), and UINT64_MAX.

Referenced by lzma_lzma2_encoder_memusage().

◆ lzma_lzma_encoder_reset()

lzma_ret lzma_lzma_encoder_reset ( lzma_lzma1_encoder coder,
const lzma_options_lzma options 
)

Definition at line 476 of file lzma_encoder.c.

478 {
480  return LZMA_OPTIONS_ERROR;
481 
482  coder->pos_mask = (1U << options->pb) - 1;
483  coder->literal_context_bits = options->lc;
484  coder->literal_pos_mask = (1U << options->lp) - 1;
485 
486  // Range coder
487  rc_reset(&coder->rc);
488 
489  // State
490  coder->state = STATE_LIT_LIT;
491  for (size_t i = 0; i < REPS; ++i)
492  coder->reps[i] = 0;
493 
494  literal_init(coder->literal, options->lc, options->lp);
495 
496  // Bit encoders
497  for (size_t i = 0; i < STATES; ++i) {
498  for (size_t j = 0; j <= coder->pos_mask; ++j) {
499  bit_reset(coder->is_match[i][j]);
500  bit_reset(coder->is_rep0_long[i][j]);
501  }
502 
503  bit_reset(coder->is_rep[i]);
504  bit_reset(coder->is_rep0[i]);
505  bit_reset(coder->is_rep1[i]);
506  bit_reset(coder->is_rep2[i]);
507  }
508 
509  for (size_t i = 0; i < FULL_DISTANCES - DIST_MODEL_END; ++i)
510  bit_reset(coder->dist_special[i]);
511 
512  // Bit tree encoders
513  for (size_t i = 0; i < DIST_STATES; ++i)
515 
517 
518  // Length encoders
520  1U << options->pb, coder->fast_mode);
521 
523  1U << options->pb, coder->fast_mode);
524 
525  // Price counts are incremented every time appropriate probabilities
526  // are changed. price counts are set to zero when the price tables
527  // are updated, which is done when the appropriate price counts have
528  // big enough value, and lzma_mf.read_ahead == 0 which happens at
529  // least every OPTS (a few thousand) possible price count increments.
530  //
531  // By resetting price counts to UINT32_MAX / 2, we make sure that the
532  // price tables will be initialized before they will be used (since
533  // the value is definitely big enough), and that it is OK to increment
534  // price counts without risk of integer overflow (since UINT32_MAX / 2
535  // is small enough). The current code doesn't increment price counts
536  // before initializing price tables, but it maybe done in future if
537  // we add support for saving the state between LZMA2 chunks.
538  coder->match_price_count = UINT32_MAX / 2;
539  coder->align_price_count = UINT32_MAX / 2;
540 
541  coder->opts_end_index = 0;
542  coder->opts_current_index = 0;
543 
544  return LZMA_OK;
545 }
@ STATE_LIT_LIT
Definition: lzma_common.h:57
#define ALIGN_BITS
Definition: lzma_common.h:217
#define DIST_SLOT_BITS
Definition: lzma_common.h:189
#define DIST_STATES
Definition: lzma_common.h:179
#define FULL_DISTANCES
Definition: lzma_common.h:213
static void literal_init(probability(*probs)[LITERAL_CODER_SIZE], uint32_t lc, uint32_t lp)
Definition: lzma_common.h:130
#define DIST_MODEL_END
Definition: lzma_common.h:209
#define STATES
Total number of states.
Definition: lzma_common.h:73
static void length_encoder_reset(lzma_length_encoder *lencoder, const uint32_t num_pos_states, const bool fast_mode)
Definition: lzma_encoder.c:453
#define rc_reset(range_decoder)
Resets the range decoder structure.
Definition: range_decoder.h:69
probability is_rep0_long[STATES][POS_STATES_MAX]
probability is_rep1[STATES]
probability is_rep0[STATES]
probability is_rep2[STATES]
probability dist_special[FULL_DISTANCES - DIST_MODEL_END]
probability dist_slot[DIST_STATES][DIST_SLOTS]
probability dist_align[ALIGN_SIZE]

References ALIGN_BITS, lzma_lzma1_encoder_s::align_price_count, bit_reset, bittree_reset, lzma_lzma1_encoder_s::dist_align, DIST_MODEL_END, lzma_lzma1_encoder_s::dist_slot, DIST_SLOT_BITS, lzma_lzma1_encoder_s::dist_special, DIST_STATES, lzma_lzma1_encoder_s::fast_mode, FULL_DISTANCES, i, lzma_lzma1_encoder_s::is_match, is_options_valid(), lzma_lzma1_encoder_s::is_rep, lzma_lzma1_encoder_s::is_rep0, lzma_lzma1_encoder_s::is_rep0_long, lzma_lzma1_encoder_s::is_rep1, lzma_lzma1_encoder_s::is_rep2, length_encoder_reset(), lzma_lzma1_encoder_s::literal, lzma_lzma1_encoder_s::literal_context_bits, literal_init(), lzma_lzma1_encoder_s::literal_pos_mask, LZMA_OK, LZMA_OPTIONS_ERROR, lzma_lzma1_encoder_s::match_len_encoder, lzma_lzma1_encoder_s::match_price_count, options, lzma_lzma1_encoder_s::opts_current_index, lzma_lzma1_encoder_s::opts_end_index, lzma_lzma1_encoder_s::pos_mask, lzma_lzma1_encoder_s::rc, rc_reset, lzma_lzma1_encoder_s::rep_len_encoder, REPS, lzma_lzma1_encoder_s::reps, lzma_lzma1_encoder_s::state, STATE_LIT_LIT, STATES, and UINT32_MAX.

Referenced by lzma2_encode(), and lzma_lzma_encoder_create().

◆ lzma_lzma_lclppb_encode()

bool lzma_lzma_lclppb_encode ( const lzma_options_lzma options,
uint8_t byte 
)

Encodes lc/lp/pb into one byte. Returns false on success and true on error.

Definition at line 645 of file lzma_encoder.c.

646 {
647  if (!is_lclppb_valid(options))
648  return true;
649 
650  *byte = (options->pb * 5 + options->lp) * 9 + options->lc;
651  assert(*byte <= (4 * 5 + 4) * 9 + 8);
652 
653  return false;
654 }

References assert(), is_lclppb_valid(), and options.

Referenced by alone_encoder_init(), and lzma2_header_lzma().

◆ match()

static void match ( lzma_lzma1_encoder coder,
const uint32_t  pos_state,
const uint32_t  distance,
const uint32_t  len 
)
inlinestatic

Definition at line 143 of file lzma_encoder.c.

145 {
146  update_match(coder->state);
147 
148  length(&coder->rc, &coder->match_len_encoder, pos_state, len,
149  coder->fast_mode);
150 
151  const uint32_t dist_slot = get_dist_slot(distance);
152  const uint32_t dist_state = get_dist_state(len);
153  rc_bittree(&coder->rc, coder->dist_slot[dist_state],
154  DIST_SLOT_BITS, dist_slot);
155 
156  if (dist_slot >= DIST_MODEL_START) {
157  const uint32_t footer_bits = (dist_slot >> 1) - 1;
158  const uint32_t base = (2 | (dist_slot & 1)) << footer_bits;
159  const uint32_t dist_reduced = distance - base;
160 
161  if (dist_slot < DIST_MODEL_END) {
162  // Careful here: base - dist_slot - 1 can be -1, but
163  // rc_bittree_reverse starts at probs[1], not probs[0].
164  rc_bittree_reverse(&coder->rc,
165  coder->dist_special + base - dist_slot - 1,
166  footer_bits, dist_reduced);
167  } else {
168  rc_direct(&coder->rc, dist_reduced >> ALIGN_BITS,
169  footer_bits - ALIGN_BITS);
171  &coder->rc, coder->dist_align,
172  ALIGN_BITS, dist_reduced & ALIGN_MASK);
173  ++coder->align_price_count;
174  }
175  }
176 
177  coder->reps[3] = coder->reps[2];
178  coder->reps[2] = coder->reps[1];
179  coder->reps[1] = coder->reps[0];
180  coder->reps[0] = distance;
181  ++coder->match_price_count;
182 }
static uint32_t get_dist_slot(uint32_t dist)
Definition: fastpos.h:109
#define update_match(state)
Indicate that the latest state was a match.
Definition: lzma_common.h:88
#define get_dist_state(len)
Definition: lzma_common.h:182
#define ALIGN_MASK
Definition: lzma_common.h:219
#define DIST_MODEL_START
Definition: lzma_common.h:198
static void length(lzma_range_encoder *rc, lzma_length_encoder *lc, const uint32_t pos_state, uint32_t len, const bool fast_mode)
Definition: lzma_encoder.c:107
#define rc_direct(dest, seq)
Decode a bit without using a probability.
static void rc_bittree_reverse(lzma_range_encoder *rc, probability *probs, uint32_t bit_count, uint32_t symbol)
Definition: range_encoder.h:90

References ALIGN_BITS, ALIGN_MASK, lzma_lzma1_encoder_s::align_price_count, lzma_lzma1_encoder_s::dist_align, DIST_MODEL_END, DIST_MODEL_START, lzma_lzma1_encoder_s::dist_slot, DIST_SLOT_BITS, lzma_lzma1_encoder_s::dist_special, lzma_lzma1_encoder_s::fast_mode, get_dist_slot(), get_dist_state, len, length(), lzma_lzma1_encoder_s::match_len_encoder, lzma_lzma1_encoder_s::match_price_count, lzma_lzma1_encoder_s::rc, rc_bittree(), rc_bittree_reverse(), rc_direct, lzma_lzma1_encoder_s::reps, lzma_lzma1_encoder_s::state, and update_match.

Referenced by encode_eopm(), and encode_symbol().

◆ rep_match()

static void rep_match ( lzma_lzma1_encoder coder,
const uint32_t  pos_state,
const uint32_t  rep,
const uint32_t  len 
)
inlinestatic

Definition at line 190 of file lzma_encoder.c.

192 {
193  if (rep == 0) {
194  rc_bit(&coder->rc, &coder->is_rep0[coder->state], 0);
195  rc_bit(&coder->rc,
196  &coder->is_rep0_long[coder->state][pos_state],
197  len != 1);
198  } else {
199  const uint32_t distance = coder->reps[rep];
200  rc_bit(&coder->rc, &coder->is_rep0[coder->state], 1);
201 
202  if (rep == 1) {
203  rc_bit(&coder->rc, &coder->is_rep1[coder->state], 0);
204  } else {
205  rc_bit(&coder->rc, &coder->is_rep1[coder->state], 1);
206  rc_bit(&coder->rc, &coder->is_rep2[coder->state],
207  rep - 2);
208 
209  if (rep == 3)
210  coder->reps[3] = coder->reps[2];
211 
212  coder->reps[2] = coder->reps[1];
213  }
214 
215  coder->reps[1] = coder->reps[0];
216  coder->reps[0] = distance;
217  }
218 
219  if (len == 1) {
220  update_short_rep(coder->state);
221  } else {
222  length(&coder->rc, &coder->rep_len_encoder, pos_state, len,
223  coder->fast_mode);
224  update_long_rep(coder->state);
225  }
226 }
#define update_long_rep(state)
Indicate that the latest state was a long repeated match.
Definition: lzma_common.h:92
#define update_short_rep(state)
Indicate that the latest state was a short match.
Definition: lzma_common.h:96

References lzma_lzma1_encoder_s::fast_mode, lzma_lzma1_encoder_s::is_rep0, lzma_lzma1_encoder_s::is_rep0_long, lzma_lzma1_encoder_s::is_rep1, lzma_lzma1_encoder_s::is_rep2, len, length(), lzma_lzma1_encoder_s::rc, rc_bit, lzma_lzma1_encoder_s::rep_len_encoder, lzma_lzma1_encoder_s::reps, lzma_lzma1_encoder_s::state, update_long_rep, and update_short_rep.

Referenced by encode_symbol().

◆ set_lz_options()

static void set_lz_options ( lzma_lz_options lz_options,
const lzma_options_lzma options 
)
static

Definition at line 435 of file lzma_encoder.c.

436 {
437  // LZ encoder initialization does the validation for these so we
438  // don't need to validate here.
439  lz_options->before_size = OPTS;
440  lz_options->dict_size = options->dict_size;
441  lz_options->after_size = LOOP_INPUT_MAX;
442  lz_options->match_len_max = MATCH_LEN_MAX;
443  lz_options->nice_len = options->nice_len;
444  lz_options->match_finder = options->mf;
445  lz_options->depth = options->depth;
446  lz_options->preset_dict = options->preset_dict;
447  lz_options->preset_dict_size = options->preset_dict_size;
448  return;
449 }
#define OPTS
const uint8_t * preset_dict
TODO: Comment.
Definition: lz_decoder.h:49
size_t after_size
Definition: lz_encoder.h:139
size_t preset_dict_size
Definition: lz_decoder.h:50
size_t match_len_max
Definition: lz_encoder.h:144
uint32_t depth
Maximum search depth.
Definition: lz_encoder.h:154
size_t dict_size
Size of the history buffer.
Definition: lz_decoder.h:48
size_t before_size
Definition: lz_encoder.h:132
lzma_match_finder match_finder
Type of the match finder to use.
Definition: lz_encoder.h:151

References lzma_lz_options::after_size, lzma_lz_options::before_size, lzma_lz_options::depth, lzma_lz_options::dict_size, LOOP_INPUT_MAX, lzma_lz_options::match_finder, lzma_lz_options::match_len_max, MATCH_LEN_MAX, lzma_lz_options::nice_len, options, OPTS, lzma_lz_options::preset_dict, and lzma_lz_options::preset_dict_size.

Referenced by lzma_lzma_encoder_create(), and lzma_lzma_encoder_memusage().