Rizin
unix-like reverse engineering framework and cli tools
lz_decoder.c
Go to the documentation of this file.
1 //
6 // Authors: Igor Pavlov
7 // Lasse Collin
8 //
9 // This file has been put into the public domain.
10 // You can do whatever you want with this file.
11 //
13 
14 // liblzma supports multiple LZ77-based filters. The LZ part is shared
15 // between these filters. The LZ code takes care of dictionary handling
16 // and passing the data between filters in the chain. The filter-specific
17 // part decodes from the input buffer to the dictionary.
18 
19 
20 #include "lz_decoder.h"
21 
22 
23 typedef struct {
26 
29 
34 
37 
41 
46  struct {
47  size_t pos;
48  size_t size;
50  } temp;
51 } lzma_coder;
52 
53 
54 static void
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 }
63 
64 
65 static lzma_ret
67  const uint8_t *restrict in, size_t *restrict in_pos,
68  size_t in_size, uint8_t *restrict out,
69  size_t *restrict out_pos, size_t out_size)
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 }
131 
132 
133 static lzma_ret
134 lz_decode(void *coder_ptr, const lzma_allocator *allocator,
135  const uint8_t *restrict in, size_t *restrict in_pos,
136  size_t in_size, uint8_t *restrict out,
137  size_t *restrict out_pos, size_t out_size,
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 }
191 
192 
193 static void
194 lz_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
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 }
209 
210 
211 extern lzma_ret
213  const lzma_filter_info *filters,
214  lzma_ret (*lz_init)(lzma_lz_decoder *lz,
215  const lzma_allocator *allocator, const void *options,
216  lzma_lz_options *lz_options))
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 }
297 
298 
299 extern uint64_t
300 lzma_lz_decoder_memusage(size_t dictionary_size)
301 {
302  return sizeof(lzma_coder) + (uint64_t)(dictionary_size);
303 }
304 
305 
306 extern void
308 {
309  lzma_coder *coder = coder_ptr;
310  coder->lz.set_uncompressed(coder->lz.coder, uncompressed_size);
311 }
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 * allocator
Definition: block.h:377
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
const lzma_filter * filters
Definition: container.h:315
#define NULL
Definition: cris-opc.c:27
voidpf uLong offset
Definition: ioapi.h:144
#define restrict
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
void lzma_lz_decoder_uncompressed(void *coder_ptr, lzma_vli uncompressed_size)
Definition: lz_decoder.c:307
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))
Definition: lz_decoder.c:212
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 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
static void lz_decoder_reset(lzma_coder *coder)
Definition: lz_decoder.c:55
uint64_t lzma_lz_decoder_memusage(size_t dictionary_size)
Definition: lz_decoder.c:300
static void lz_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
Definition: lz_decoder.c:194
LZ out window.
#define LZMA_LZ_DECODER_INIT
Definition: lz_decoder.h:74
assert(limit<=UINT32_MAX/2)
unsigned long uint64_t
Definition: sftypes.h:28
unsigned char uint8_t
Definition: sftypes.h:31
#define SIZE_MAX
Definition: buffer.h:15
Custom functions for memory handling.
Definition: base.h:372
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
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
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 full
Definition: lz_decoder.h:33
size_t pos
Definition: lz_decoder.h:28
void * options
Pointer to filter-specific options structure.
Definition: filter.h:63
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
void(* end)(void *coder, const lzma_allocator *allocator)
Free allocated resources.
Definition: lz_decoder.h:69
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
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
Hold data and function pointers of the next filter in the chain.
Definition: common.h:135
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_BUFFER_SIZE
Size of temporary buffers needed in some filters.
Definition: common.h:49
#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.
uint64_t uncompressed_size
Definition: list.c:106
#define my_min(x, y)
Definition: sysdefs.h:185
uint64_t lzma_vli
Variable-length integer type.
Definition: vli.h:63
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_DATA_ERROR
Data is corrupt.
Definition: base.h:172
@ LZMA_MEM_ERROR
Cannot allocate memory.
Definition: base.h:128
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
lzma_action
The ‘action’ argument for lzma_code()
Definition: base.h:250
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
void lzma_next_end(lzma_next_coder *next, const lzma_allocator *allocator)
Definition: common.c:145