Rizin
unix-like reverse engineering framework and cli tools
range_encoder.h File Reference

Range Encoder. More...

#include "range_common.h"
#include "price.h"

Go to the source code of this file.

Classes

struct  lzma_range_encoder
 

Macros

#define RC_SYMBOLS_MAX   58
 

Functions

static void rc_reset (lzma_range_encoder *rc)
 
static void rc_bit (lzma_range_encoder *rc, probability *prob, uint32_t bit)
 
static void rc_bittree (lzma_range_encoder *rc, probability *probs, uint32_t bit_count, uint32_t symbol)
 
static void rc_bittree_reverse (lzma_range_encoder *rc, probability *probs, uint32_t bit_count, uint32_t symbol)
 
static void rc_direct (lzma_range_encoder *rc, uint32_t value, uint32_t bit_count)
 
static void rc_flush (lzma_range_encoder *rc)
 
static bool rc_shift_low (lzma_range_encoder *rc, uint8_t *out, size_t *out_pos, size_t out_size)
 
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)
 

Detailed Description

Range Encoder.

Definition in file range_encoder.h.

Macro Definition Documentation

◆ RC_SYMBOLS_MAX

#define RC_SYMBOLS_MAX   58

Maximum number of symbols that can be put pending into lzma_range_encoder structure between calls to lzma_rc_encode(). For LZMA, 52+5 is enough (match with big distance and length followed by range encoder flush).

Definition at line 24 of file range_encoder.h.

Function Documentation

◆ rc_bit()

static void rc_bit ( lzma_range_encoder rc,
probability prob,
uint32_t  bit 
)
inlinestatic

Definition at line 67 of file range_encoder.h.

68 {
69  rc->symbols[rc->count] = bit;
70  rc->probs[rc->count] = prob;
71  ++rc->count;
72 }
RzCryptoSelector bit
Definition: crypto.c:16
enum lzma_range_encoder::@658 symbols[RC_SYMBOLS_MAX]
Symbols to encode.
probability * probs[RC_SYMBOLS_MAX]
Probabilities associated with RC_BIT_0 or RC_BIT_1.
Definition: range_encoder.h:49
size_t count
Number of symbols in the tables.
Definition: range_encoder.h:34

References bit, lzma_range_encoder::count, lzma_range_encoder::probs, and lzma_range_encoder::symbols.

Referenced by rc_bittree(), and rc_bittree_reverse().

◆ rc_bittree()

static void rc_bittree ( lzma_range_encoder rc,
probability probs,
uint32_t  bit_count,
uint32_t  symbol 
)
inlinestatic

Definition at line 76 of file range_encoder.h.

78 {
79  uint32_t model_index = 1;
80 
81  do {
82  const uint32_t bit = (symbol >> --bit_count) & 1;
83  rc_bit(rc, &probs[model_index], bit);
84  model_index = (model_index << 1) + bit;
85  } while (bit_count != 0);
86 }
static void rc_bit(lzma_range_encoder *rc, probability *prob, uint32_t bit)
Definition: range_encoder.h:67
unsigned int uint32_t
Definition: sftypes.h:29

References bit, and rc_bit().

Referenced by encode_init(), length(), literal(), and match().

◆ rc_bittree_reverse()

static void rc_bittree_reverse ( lzma_range_encoder rc,
probability probs,
uint32_t  bit_count,
uint32_t  symbol 
)
inlinestatic

Definition at line 90 of file range_encoder.h.

92 {
93  uint32_t model_index = 1;
94 
95  do {
96  const uint32_t bit = symbol & 1;
97  symbol >>= 1;
98  rc_bit(rc, &probs[model_index], bit);
99  model_index = (model_index << 1) + bit;
100  } while (--bit_count != 0);
101 }

References bit, and rc_bit().

Referenced by match().

◆ rc_direct()

static void rc_direct ( lzma_range_encoder rc,
uint32_t  value,
uint32_t  bit_count 
)
inlinestatic

Definition at line 105 of file range_encoder.h.

107 {
108  do {
109  rc->symbols[rc->count++]
110  = RC_DIRECT_0 + ((value >> --bit_count) & 1);
111  } while (bit_count != 0);
112 }
static int value
Definition: cmd_api.c:93

References lzma_range_encoder::count, lzma_range_encoder::symbols, and value.

◆ rc_encode()

static bool rc_encode ( lzma_range_encoder rc,
uint8_t out,
size_t out_pos,
size_t  out_size 
)
inlinestatic

Definition at line 150 of file range_encoder.h.

152 {
153  assert(rc->count <= RC_SYMBOLS_MAX);
154 
155  while (rc->pos < rc->count) {
156  // Normalize
157  if (rc->range < RC_TOP_VALUE) {
158  if (rc_shift_low(rc, out, out_pos, out_size))
159  return true;
160 
161  rc->range <<= RC_SHIFT_BITS;
162  }
163 
164  // Encode a bit
165  switch (rc->symbols[rc->pos]) {
166  case RC_BIT_0: {
167  probability prob = *rc->probs[rc->pos];
168  rc->range = (rc->range >> RC_BIT_MODEL_TOTAL_BITS)
169  * prob;
170  prob += (RC_BIT_MODEL_TOTAL - prob) >> RC_MOVE_BITS;
171  *rc->probs[rc->pos] = prob;
172  break;
173  }
174 
175  case RC_BIT_1: {
176  probability prob = *rc->probs[rc->pos];
177  const uint32_t bound = prob * (rc->range
179  rc->low += bound;
180  rc->range -= bound;
181  prob -= prob >> RC_MOVE_BITS;
182  *rc->probs[rc->pos] = prob;
183  break;
184  }
185 
186  case RC_DIRECT_0:
187  rc->range >>= 1;
188  break;
189 
190  case RC_DIRECT_1:
191  rc->range >>= 1;
192  rc->low += rc->range;
193  break;
194 
195  case RC_FLUSH:
196  // Prevent further normalizations.
197  rc->range = UINT32_MAX;
198 
199  // Flush the last five bytes (see rc_flush()).
200  do {
201  if (rc_shift_low(rc, out, out_pos, out_size))
202  return true;
203  } while (++rc->pos < rc->count);
204 
205  // Reset the range encoder so we are ready to continue
206  // encoding if we weren't finishing the stream.
207  rc_reset(rc);
208  return false;
209 
210  default:
211  assert(0);
212  break;
213  }
214 
215  ++rc->pos;
216  }
217 
218  rc->count = 0;
219  rc->pos = 0;
220 
221  return false;
222 }
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
assert(limit<=UINT32_MAX/2)
uint16_t probability
Type of probabilities used with range coder.
Definition: range_common.h:69
#define RC_BIT_MODEL_TOTAL_BITS
Definition: range_common.h:27
#define RC_MOVE_BITS
Definition: range_common.h:29
#define RC_SHIFT_BITS
Definition: range_common.h:24
#define RC_TOP_VALUE
Definition: range_common.h:26
#define RC_BIT_MODEL_TOTAL
Definition: range_common.h:28
static bool rc_shift_low(lzma_range_encoder *rc, uint8_t *out, size_t *out_pos, size_t out_size)
static void rc_reset(lzma_range_encoder *rc)
Definition: range_encoder.h:55
#define RC_SYMBOLS_MAX
Definition: range_encoder.h:24
#define UINT32_MAX
size_t pos
rc_encode()'s position in the tables
Definition: range_encoder.h:37

References assert(), lzma_range_encoder::count, lzma_range_encoder::low, out, out_pos, lzma_range_encoder::pos, lzma_range_encoder::probs, lzma_range_encoder::range, RC_BIT_MODEL_TOTAL, RC_BIT_MODEL_TOTAL_BITS, RC_MOVE_BITS, rc_reset(), RC_SHIFT_BITS, rc_shift_low(), RC_SYMBOLS_MAX, RC_TOP_VALUE, lzma_range_encoder::symbols, and UINT32_MAX.

Referenced by lzma_lzma_encode().

◆ rc_flush()

static void rc_flush ( lzma_range_encoder rc)
inlinestatic

Definition at line 116 of file range_encoder.h.

117 {
118  for (size_t i = 0; i < 5; ++i)
119  rc->symbols[rc->count++] = RC_FLUSH;
120 }
lzma_index ** i
Definition: index.h:629

References lzma_range_encoder::count, i, and lzma_range_encoder::symbols.

Referenced by lzma_lzma_encode().

◆ rc_pending()

static uint64_t rc_pending ( const lzma_range_encoder rc)
inlinestatic

Definition at line 226 of file range_encoder.h.

227 {
228  return rc->cache_size + 5 - 1;
229 }

References lzma_range_encoder::cache_size.

Referenced by lzma_lzma_encode().

◆ rc_reset()

static void rc_reset ( lzma_range_encoder rc)
inlinestatic

Definition at line 55 of file range_encoder.h.

56 {
57  rc->low = 0;
58  rc->cache_size = 1;
59  rc->range = UINT32_MAX;
60  rc->cache = 0;
61  rc->count = 0;
62  rc->pos = 0;
63 }

References lzma_range_encoder::cache, lzma_range_encoder::cache_size, lzma_range_encoder::count, lzma_range_encoder::low, lzma_range_encoder::pos, lzma_range_encoder::range, and UINT32_MAX.

Referenced by rc_encode().

◆ rc_shift_low()

static bool rc_shift_low ( lzma_range_encoder rc,
uint8_t out,
size_t out_pos,
size_t  out_size 
)
inlinestatic

Definition at line 124 of file range_encoder.h.

126 {
127  if ((uint32_t)(rc->low) < (uint32_t)(0xFF000000)
128  || (uint32_t)(rc->low >> 32) != 0) {
129  do {
130  if (*out_pos == out_size)
131  return true;
132 
133  out[*out_pos] = rc->cache + (uint8_t)(rc->low >> 32);
134  ++*out_pos;
135  rc->cache = 0xFF;
136 
137  } while (--rc->cache_size != 0);
138 
139  rc->cache = (rc->low >> 24) & 0xFF;
140  }
141 
142  ++rc->cache_size;
143  rc->low = (rc->low & 0x00FFFFFF) << RC_SHIFT_BITS;
144 
145  return false;
146 }
unsigned char uint8_t
Definition: sftypes.h:31

References lzma_range_encoder::cache, lzma_range_encoder::cache_size, lzma_range_encoder::low, out, out_pos, and RC_SHIFT_BITS.

Referenced by rc_encode().