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

LZ out window. More...

#include "common.h"

Go to the source code of this file.

Classes

struct  lzma_dict
 
struct  lzma_lz_options
 
struct  lzma_lz_decoder
 

Macros

#define LZMA_LZ_DECODER_INIT
 

Functions

lzma_ret lzma_lz_decoder_init (lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters, lzma_ret(*lz_init)(lzma_lz_decoder *lz, const lzma_allocator *allocator, const void *options, lzma_lz_options *lz_options))
 
uint64_t lzma_lz_decoder_memusage (size_t dictionary_size)
 
void lzma_lz_decoder_uncompressed (void *coder, lzma_vli uncompressed_size)
 
static uint8_t dict_get (const lzma_dict *const dict, const uint32_t distance)
 Get a byte from the history buffer. More...
 
static bool dict_is_empty (const lzma_dict *const dict)
 Test if dictionary is empty. More...
 
static bool dict_is_distance_valid (const lzma_dict *const dict, const size_t distance)
 Validate the match distance. More...
 
static bool dict_repeat (lzma_dict *dict, uint32_t distance, uint32_t *len)
 Repeat *len bytes at distance. More...
 
static bool dict_put (lzma_dict *dict, uint8_t byte)
 
static void dict_write (lzma_dict *restrict dict, const uint8_t *restrict in, size_t *restrict in_pos, size_t in_size, size_t *restrict left)
 Copies arbitrary amount of data into the dictionary. More...
 
static void dict_reset (lzma_dict *dict)
 

Detailed Description

LZ out window.

Definition in file lz_decoder.h.

Macro Definition Documentation

◆ LZMA_LZ_DECODER_INIT

#define LZMA_LZ_DECODER_INIT
Value:
.coder = NULL, \
.code = NULL, \
.reset = NULL, \
.set_uncompressed = NULL, \
.end = NULL, \
}
#define NULL
Definition: cris-opc.c:27

Definition at line 74 of file lz_decoder.h.

Function Documentation

◆ dict_get()

static uint8_t dict_get ( const lzma_dict *const  dict,
const uint32_t  distance 
)
inlinestatic

Get a byte from the history buffer.

Definition at line 103 of file lz_decoder.h.

104 {
105  return dict->buf[dict->pos - distance - 1
106  + (distance < dict->pos ? 0 : dict->size)];
107 }
size_t size
Size of the dictionary.
Definition: lz_decoder.h:39
uint8_t * buf
Definition: lz_decoder.h:24
size_t pos
Definition: lz_decoder.h:28

References lzma_dict::buf, lzma_dict::pos, and lzma_dict::size.

Referenced by dict_repeat(), and lzma_decode().

◆ dict_is_distance_valid()

static bool dict_is_distance_valid ( const lzma_dict *const  dict,
const size_t  distance 
)
inlinestatic

Validate the match distance.

Definition at line 120 of file lz_decoder.h.

121 {
122  return dict->full > distance;
123 }
size_t full
Definition: lz_decoder.h:33

References lzma_dict::full.

Referenced by lzma_decode().

◆ dict_is_empty()

static bool dict_is_empty ( const lzma_dict *const  dict)
inlinestatic

Test if dictionary is empty.

Definition at line 112 of file lz_decoder.h.

113 {
114  return dict->full == 0;
115 }

References lzma_dict::full.

◆ dict_put()

static bool dict_put ( lzma_dict dict,
uint8_t  byte 
)
inlinestatic

Puts one byte into the dictionary. Returns true if the dictionary was already full and the byte couldn't be added.

Definition at line 187 of file lz_decoder.h.

188 {
189  if (unlikely(dict->pos == dict->limit))
190  return true;
191 
192  dict->buf[dict->pos++] = byte;
193 
194  if (dict->pos > dict->full)
195  dict->full = dict->pos;
196 
197  return false;
198 }
#define unlikely(expr)
Definition: lz4.c:177
size_t limit
Write limit.
Definition: lz_decoder.h:36

References lzma_dict::buf, lzma_dict::full, lzma_dict::limit, lzma_dict::pos, and unlikely.

Referenced by lzma_decode().

◆ dict_repeat()

static bool dict_repeat ( lzma_dict dict,
uint32_t  distance,
uint32_t len 
)
inlinestatic

Repeat *len bytes at distance.

Definition at line 128 of file lz_decoder.h.

129 {
130  // Don't write past the end of the dictionary.
131  const size_t dict_avail = dict->limit - dict->pos;
132  uint32_t left = my_min(dict_avail, *len);
133  *len -= left;
134 
135  // Repeat a block of data from the history. Because memcpy() is faster
136  // than copying byte by byte in a loop, the copying process gets split
137  // into three cases.
138  if (distance < left) {
139  // Source and target areas overlap, thus we can't use
140  // memcpy() nor even memmove() safely.
141  do {
142  dict->buf[dict->pos] = dict_get(dict, distance);
143  ++dict->pos;
144  } while (--left > 0);
145 
146  } else if (distance < dict->pos) {
147  // The easiest and fastest case
148  memcpy(dict->buf + dict->pos,
149  dict->buf + dict->pos - distance - 1,
150  left);
151  dict->pos += left;
152 
153  } else {
154  // The bigger the dictionary, the more rare this
155  // case occurs. We need to "wrap" the dict, thus
156  // we might need two memcpy() to copy all the data.
157  assert(dict->full == dict->size);
158  const uint32_t copy_pos
159  = dict->pos - distance - 1 + dict->size;
160  uint32_t copy_size = dict->size - copy_pos;
161 
162  if (copy_size < left) {
163  memmove(dict->buf + dict->pos, dict->buf + copy_pos,
164  copy_size);
165  dict->pos += copy_size;
166  copy_size = left - copy_size;
167  memcpy(dict->buf + dict->pos, dict->buf, copy_size);
168  dict->pos += copy_size;
169  } else {
170  memmove(dict->buf + dict->pos, dict->buf + copy_pos,
171  left);
172  dict->pos += left;
173  }
174  }
175 
176  // Update how full the dictionary is.
177  if (dict->full < dict->pos)
178  dict->full = dict->pos;
179 
180  return unlikely(*len != 0);
181 }
size_t len
Definition: 6502dis.c:15
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static uint8_t dict_get(const lzma_dict *const dict, const uint32_t distance)
Get a byte from the history buffer.
Definition: lz_decoder.h:103
assert(limit<=UINT32_MAX/2)
unsigned int uint32_t
Definition: sftypes.h:29
int pos
Definition: main.c:11
#define my_min(x, y)
Definition: sysdefs.h:185

References assert(), lzma_dict::buf, dict_get(), lzma_dict::full, len, lzma_dict::limit, memcpy(), my_min, pos, lzma_dict::pos, lzma_dict::size, and unlikely.

Referenced by lzma_decode().

◆ dict_reset()

static void dict_reset ( lzma_dict dict)
inlinestatic

Definition at line 228 of file lz_decoder.h.

229 {
230  dict->need_reset = true;
231  return;
232 }
bool need_reset
True when dictionary should be reset before decoding more data.
Definition: lz_decoder.h:42

References lzma_dict::need_reset.

Referenced by lzma2_decode().

◆ dict_write()

static void dict_write ( lzma_dict *restrict  dict,
const uint8_t *restrict  in,
size_t *restrict  in_pos,
size_t  in_size,
size_t *restrict  left 
)
inlinestatic

Copies arbitrary amount of data into the dictionary.

Definition at line 203 of file lz_decoder.h.

206 {
207  // NOTE: If we are being given more data than the size of the
208  // dictionary, it could be possible to optimize the LZ decoder
209  // so that not everything needs to go through the dictionary.
210  // This shouldn't be very common thing in practice though, and
211  // the slowdown of one extra memcpy() isn't bad compared to how
212  // much time it would have taken if the data were compressed.
213 
214  if (in_size - *in_pos > *left)
215  in_size = *in_pos + *left;
216 
217  *left -= lzma_bufcpy(in, in_pos, in_size,
218  dict->buf, &dict->pos, dict->limit);
219 
220  if (dict->pos > dict->full)
221  dict->full = dict->pos;
222 
223  return;
224 }
const lzma_allocator const uint8_t size_t * in_pos
Definition: block.h:579
const lzma_allocator const uint8_t size_t in_size
Definition: block.h:527
const lzma_allocator const uint8_t * in
Definition: block.h:527
size_t lzma_bufcpy(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)
Definition: common.c:94

References in, in_pos, in_size, and lzma_bufcpy().

Referenced by lzma2_decode().

◆ lzma_lz_decoder_init()

lzma_ret lzma_lz_decoder_init ( lzma_next_coder next,
const lzma_allocator allocator,
const lzma_filter_info filters,
lzma_ret(*)(lzma_lz_decoder *lz, const lzma_allocator *allocator, const void *options, lzma_lz_options *lz_options)  lz_init 
)

Definition at line 212 of file lz_decoder.c.

217 {
218  // Allocate the base structure if it isn't already allocated.
219  lzma_coder *coder = next->coder;
220  if (coder == NULL) {
221  coder = lzma_alloc(sizeof(lzma_coder), allocator);
222  if (coder == NULL)
223  return LZMA_MEM_ERROR;
224 
225  next->coder = coder;
226  next->code = &lz_decode;
227  next->end = &lz_decoder_end;
228 
229  coder->dict.buf = NULL;
230  coder->dict.size = 0;
231  coder->lz = LZMA_LZ_DECODER_INIT;
232  coder->next = LZMA_NEXT_CODER_INIT;
233  }
234 
235  // Allocate and initialize the LZ-based decoder. It will also give
236  // us the dictionary size.
237  lzma_lz_options lz_options;
238  return_if_error(lz_init(&coder->lz, allocator,
239  filters[0].options, &lz_options));
240 
241  // If the dictionary size is very small, increase it to 4096 bytes.
242  // This is to prevent constant wrapping of the dictionary, which
243  // would slow things down. The downside is that since we don't check
244  // separately for the real dictionary size, we may happily accept
245  // corrupt files.
246  if (lz_options.dict_size < 4096)
247  lz_options.dict_size = 4096;
248 
249  // Make dictionary size a multiple of 16. Some LZ-based decoders like
250  // LZMA use the lowest bits lzma_dict.pos to know the alignment of the
251  // data. Aligned buffer is also good when memcpying from the
252  // dictionary to the output buffer, since applications are
253  // recommended to give aligned buffers to liblzma.
254  //
255  // Avoid integer overflow.
256  if (lz_options.dict_size > SIZE_MAX - 15)
257  return LZMA_MEM_ERROR;
258 
259  lz_options.dict_size = (lz_options.dict_size + 15) & ~((size_t)(15));
260 
261  // Allocate and initialize the dictionary.
262  if (coder->dict.size != lz_options.dict_size) {
263  lzma_free(coder->dict.buf, allocator);
264  coder->dict.buf
265  = lzma_alloc(lz_options.dict_size, allocator);
266  if (coder->dict.buf == NULL)
267  return LZMA_MEM_ERROR;
268 
269  coder->dict.size = lz_options.dict_size;
270  }
271 
272  lz_decoder_reset(next->coder);
273 
274  // Use the preset dictionary if it was given to us.
275  if (lz_options.preset_dict != NULL
276  && lz_options.preset_dict_size > 0) {
277  // If the preset dictionary is bigger than the actual
278  // dictionary, copy only the tail.
279  const size_t copy_size = my_min(lz_options.preset_dict_size,
280  lz_options.dict_size);
281  const size_t offset = lz_options.preset_dict_size - copy_size;
282  memcpy(coder->dict.buf, lz_options.preset_dict + offset,
283  copy_size);
284  coder->dict.pos = copy_size;
285  coder->dict.full = copy_size;
286  }
287 
288  // Miscellaneous initializations
289  coder->next_finished = false;
290  coder->this_finished = false;
291  coder->temp.pos = 0;
292  coder->temp.size = 0;
293 
294  // Initialize the next filter in the chain, if any.
295  return lzma_next_filter_init(&coder->next, allocator, filters + 1);
296 }
const lzma_allocator * allocator
Definition: block.h:377
const lzma_filter * filters
Definition: container.h:315
voidpf uLong offset
Definition: ioapi.h:144
static lzma_ret lz_decode(void *coder_ptr, const 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)
Definition: lz_decoder.c:134
static void lz_decoder_reset(lzma_coder *coder)
Definition: lz_decoder.c:55
static void lz_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
Definition: lz_decoder.c:194
#define LZMA_LZ_DECODER_INIT
Definition: lz_decoder.h:74
#define SIZE_MAX
size_t pos
Definition: lz_decoder.c:47
struct lzma_coder::@655 temp
lzma_next_coder next
Next coder in the chain.
Definition: lz_decoder.c:33
size_t size
Definition: lz_decoder.c:48
lzma_dict dict
Dictionary (history buffer)
Definition: lz_decoder.c:25
lzma_lz_decoder lz
The actual LZ-based decoder e.g. LZMA.
Definition: lz_decoder.c:28
bool this_finished
Definition: lz_decoder.c:40
bool next_finished
True if the next filter in the chain has returned LZMA_STREAM_END.
Definition: lz_decoder.c:36
void * options
Pointer to filter-specific options structure.
Definition: filter.h:63
const uint8_t * preset_dict
TODO: Comment.
Definition: lz_decoder.h:49
size_t preset_dict_size
Definition: lz_decoder.h:50
size_t dict_size
Size of the history buffer.
Definition: lz_decoder.h:48
lzma_code_function code
Pointer to function to do the actual coding.
Definition: common.h:150
void * coder
Pointer to coder-specific data.
Definition: common.h:137
lzma_end_function end
Definition: common.h:155
#define LZMA_NEXT_CODER_INIT
Macro to initialize lzma_next_coder structure.
Definition: common.h:180
#define return_if_error(expr)
Return if expression doesn't evaluate to LZMA_OK.
Definition: common.h:278
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
lzma_ret lzma_next_filter_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
Definition: common.c:116
void lzma_free(void *ptr, const lzma_allocator *allocator)
Frees memory.
Definition: common.c:78

References allocator, lzma_dict::buf, lzma_next_coder_s::code, lzma_next_coder_s::coder, lzma_coder::dict, lzma_lz_options::dict_size, lzma_next_coder_s::end, filters, lzma_dict::full, lzma_coder::lz, lz_decode(), lz_decoder_end(), lz_decoder_reset(), lzma_alloc(), lzma_free(), LZMA_LZ_DECODER_INIT, LZMA_MEM_ERROR, LZMA_NEXT_CODER_INIT, lzma_next_filter_init(), memcpy(), my_min, lzma_coder::next, lzma_coder::next_finished, NULL, lzma_filter::options, lzma_coder::pos, lzma_dict::pos, lzma_lz_options::preset_dict, lzma_lz_options::preset_dict_size, return_if_error, lzma_coder::size, lzma_dict::size, SIZE_MAX, lzma_coder::temp, and lzma_coder::this_finished.

Referenced by lzma_lzma2_decoder_init(), and lzma_lzma_decoder_init().

◆ lzma_lz_decoder_memusage()

uint64_t lzma_lz_decoder_memusage ( size_t  dictionary_size)

Definition at line 300 of file lz_decoder.c.

301 {
302  return sizeof(lzma_coder) + (uint64_t)(dictionary_size);
303 }
unsigned long uint64_t
Definition: sftypes.h:28

Referenced by lzma_lzma_decoder_memusage_nocheck().

◆ lzma_lz_decoder_uncompressed()

void lzma_lz_decoder_uncompressed ( void *  coder,
lzma_vli  uncompressed_size 
)

Definition at line 307 of file lz_decoder.c.

308 {
309  lzma_coder *coder = coder_ptr;
310  coder->lz.set_uncompressed(coder->lz.coder, uncompressed_size);
311 }
void * coder
Data specific to the LZ-based decoder.
Definition: lz_decoder.h:56
void(* set_uncompressed)(void *coder, lzma_vli uncompressed_size)
Set the uncompressed size.
Definition: lz_decoder.h:66
uint64_t uncompressed_size
Definition: list.c:106

References lzma_lz_decoder::coder, lzma_coder::lz, lzma_lz_decoder::set_uncompressed, and uncompressed_size.

Referenced by alone_decode().