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

LZ out window. More...

#include "lz_decoder.h"

Go to the source code of this file.

Classes

struct  lzma_coder
 

Functions

static void lz_decoder_reset (lzma_coder *coder)
 
static lzma_ret decode_buffer (lzma_coder *coder, 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)
 
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)
 
static void lz_decoder_end (void *coder_ptr, const lzma_allocator *allocator)
 
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_ptr, lzma_vli uncompressed_size)
 

Detailed Description

LZ out window.

Definition in file lz_decoder.c.

Function Documentation

◆ decode_buffer()

static lzma_ret decode_buffer ( lzma_coder coder,
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 
)
static

Definition at line 66 of file lz_decoder.c.

70 {
71  while (true) {
72  // Wrap the dictionary if needed.
73  if (coder->dict.pos == coder->dict.size)
74  coder->dict.pos = 0;
75 
76  // Store the current dictionary position. It is needed to know
77  // where to start copying to the out[] buffer.
78  const size_t dict_start = coder->dict.pos;
79 
80  // Calculate how much we allow coder->lz.code() to decode.
81  // It must not decode past the end of the dictionary
82  // buffer, and we don't want it to decode more than is
83  // actually needed to fill the out[] buffer.
84  coder->dict.limit = coder->dict.pos
85  + my_min(out_size - *out_pos,
86  coder->dict.size - coder->dict.pos);
87 
88  // Call the coder->lz.code() to do the actual decoding.
89  const lzma_ret ret = coder->lz.code(
90  coder->lz.coder, &coder->dict,
91  in, in_pos, in_size);
92 
93  // Copy the decoded data from the dictionary to the out[]
94  // buffer. Do it conditionally because out can be NULL
95  // (in which case copy_size is always 0). Calling memcpy()
96  // with a null-pointer is undefined even if the third
97  // argument is 0.
98  const size_t copy_size = coder->dict.pos - dict_start;
99  assert(copy_size <= out_size - *out_pos);
100 
101  if (copy_size > 0)
102  memcpy(out + *out_pos, coder->dict.buf + dict_start,
103  copy_size);
104 
105  *out_pos += copy_size;
106 
107  // Reset the dictionary if so requested by coder->lz.code().
108  if (coder->dict.need_reset) {
109  lz_decoder_reset(coder);
110 
111  // Since we reset dictionary, we don't check if
112  // dictionary became full.
113  if (ret != LZMA_OK || *out_pos == out_size)
114  return ret;
115  } else {
116  // Return if everything got decoded or an error
117  // occurred, or if there's no more data to decode.
118  //
119  // Note that detecting if there's something to decode
120  // is done by looking if dictionary become full
121  // instead of looking if *in_pos == in_size. This
122  // is because it is possible that all the input was
123  // consumed already but some data is pending to be
124  // written to the dictionary.
125  if (ret != LZMA_OK || *out_pos == out_size
126  || coder->dict.pos < coder->dict.size)
127  return ret;
128  }
129  }
130 }
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 * 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
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static void lz_decoder_reset(lzma_coder *coder)
Definition: lz_decoder.c:55
assert(limit<=UINT32_MAX/2)
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 need_reset
True when dictionary should be reset before decoding more data.
Definition: lz_decoder.h:42
size_t size
Size of the dictionary.
Definition: lz_decoder.h:39
size_t limit
Write limit.
Definition: lz_decoder.h:36
uint8_t * buf
Definition: lz_decoder.h:24
size_t pos
Definition: lz_decoder.h:28
void * coder
Data specific to the LZ-based decoder.
Definition: lz_decoder.h:56
lzma_ret(* code)(void *coder, lzma_dict *restrict dict, const uint8_t *restrict in, size_t *restrict in_pos, size_t in_size)
Function to decode from in[] to *dict.
Definition: lz_decoder.h:59
#define my_min(x, y)
Definition: sysdefs.h:185
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58

References assert(), lzma_dict::buf, lzma_lz_decoder::code, lzma_lz_decoder::coder, lzma_coder::dict, in, in_pos, in_size, lzma_dict::limit, lzma_coder::lz, lz_decoder_reset(), LZMA_OK, memcpy(), my_min, lzma_dict::need_reset, out, out_pos, lzma_dict::pos, and lzma_dict::size.

Referenced by lz_decode().

◆ lz_decode()

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 
)
static

Definition at line 134 of file lz_decoder.c.

139 {
140  lzma_coder *coder = coder_ptr;
141 
142  if (coder->next.code == NULL)
143  return decode_buffer(coder, in, in_pos, in_size,
144  out, out_pos, out_size);
145 
146  // We aren't the last coder in the chain, we need to decode
147  // our input to a temporary buffer.
148  while (*out_pos < out_size) {
149  // Fill the temporary buffer if it is empty.
150  if (!coder->next_finished
151  && coder->temp.pos == coder->temp.size) {
152  coder->temp.pos = 0;
153  coder->temp.size = 0;
154 
155  const lzma_ret ret = coder->next.code(
156  coder->next.coder,
158  coder->temp.buffer, &coder->temp.size,
160 
161  if (ret == LZMA_STREAM_END)
162  coder->next_finished = true;
163  else if (ret != LZMA_OK || coder->temp.size == 0)
164  return ret;
165  }
166 
167  if (coder->this_finished) {
168  if (coder->temp.size != 0)
169  return LZMA_DATA_ERROR;
170 
171  if (coder->next_finished)
172  return LZMA_STREAM_END;
173 
174  return LZMA_OK;
175  }
176 
177  const lzma_ret ret = decode_buffer(coder, coder->temp.buffer,
178  &coder->temp.pos, coder->temp.size,
179  out, out_pos, out_size);
180 
181  if (ret == LZMA_STREAM_END)
182  coder->this_finished = true;
183  else if (ret != LZMA_OK)
184  return ret;
185  else if (coder->next_finished && *out_pos < out_size)
186  return LZMA_DATA_ERROR;
187  }
188 
189  return LZMA_OK;
190 }
const lzma_allocator * allocator
Definition: block.h:377
#define NULL
Definition: cris-opc.c:27
static lzma_ret decode_buffer(lzma_coder *coder, 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: lz_decoder.c:66
size_t pos
Definition: lz_decoder.c:47
struct lzma_coder::@655 temp
uint8_t buffer[LZMA_BUFFER_SIZE]
Definition: lz_decoder.c:49
lzma_next_coder next
Next coder in the chain.
Definition: lz_decoder.c:33
size_t size
Definition: lz_decoder.c:48
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
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
#define LZMA_BUFFER_SIZE
Size of temporary buffers needed in some filters.
Definition: common.h:49
@ LZMA_DATA_ERROR
Data is corrupt.
Definition: base.h:172
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63

References test-lz4-speed::action, allocator, lzma_coder::buffer, lzma_next_coder_s::code, lzma_next_coder_s::coder, decode_buffer(), in, in_pos, in_size, LZMA_BUFFER_SIZE, LZMA_DATA_ERROR, LZMA_OK, LZMA_STREAM_END, lzma_coder::next, lzma_coder::next_finished, NULL, out, out_pos, lzma_coder::pos, lzma_coder::size, lzma_coder::temp, and lzma_coder::this_finished.

Referenced by lzma_lz_decoder_init().

◆ lz_decoder_end()

static void lz_decoder_end ( void *  coder_ptr,
const lzma_allocator allocator 
)
static

Definition at line 194 of file lz_decoder.c.

195 {
196  lzma_coder *coder = coder_ptr;
197 
198  lzma_next_end(&coder->next, allocator);
199  lzma_free(coder->dict.buf, allocator);
200 
201  if (coder->lz.end != NULL)
202  coder->lz.end(coder->lz.coder, allocator);
203  else
204  lzma_free(coder->lz.coder, allocator);
205 
206  lzma_free(coder, allocator);
207  return;
208 }
void(* end)(void *coder, const lzma_allocator *allocator)
Free allocated resources.
Definition: lz_decoder.h:69
void lzma_free(void *ptr, const lzma_allocator *allocator)
Frees memory.
Definition: common.c:78
void lzma_next_end(lzma_next_coder *next, const lzma_allocator *allocator)
Definition: common.c:145

References allocator, lzma_dict::buf, lzma_lz_decoder::coder, lzma_coder::dict, lzma_lz_decoder::end, lzma_coder::lz, lzma_free(), lzma_next_end(), lzma_coder::next, and NULL.

Referenced by lzma_lz_decoder_init().

◆ lz_decoder_reset()

static void lz_decoder_reset ( lzma_coder coder)
static

Definition at line 55 of file lz_decoder.c.

56 {
57  coder->dict.pos = 0;
58  coder->dict.full = 0;
59  coder->dict.buf[coder->dict.size - 1] = '\0';
60  coder->dict.need_reset = false;
61  return;
62 }
size_t full
Definition: lz_decoder.h:33

References lzma_dict::buf, lzma_coder::dict, lzma_dict::full, lzma_dict::need_reset, lzma_dict::pos, and lzma_dict::size.

Referenced by decode_buffer(), and lzma_lz_decoder_init().

◆ 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_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_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
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_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

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_ptr,
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(* 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().