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

LZMA2 decoder. More...

#include "lzma2_decoder.h"
#include "lz_decoder.h"
#include "lzma_decoder.h"

Go to the source code of this file.

Classes

struct  lzma_lzma2_coder
 

Functions

static lzma_ret lzma2_decode (void *coder_ptr, lzma_dict *restrict dict, const uint8_t *restrict in, size_t *restrict in_pos, size_t in_size)
 
static void lzma2_decoder_end (void *coder_ptr, const lzma_allocator *allocator)
 
static lzma_ret lzma2_decoder_init (lzma_lz_decoder *lz, const lzma_allocator *allocator, const void *opt, lzma_lz_options *lz_options)
 
lzma_ret lzma_lzma2_decoder_init (lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
 
uint64_t lzma_lzma2_decoder_memusage (const void *options)
 
lzma_ret lzma_lzma2_props_decode (void **options, const lzma_allocator *allocator, const uint8_t *props, size_t props_size)
 

Detailed Description

LZMA2 decoder.

Definition in file lzma2_decoder.c.

Function Documentation

◆ lzma2_decode()

static lzma_ret lzma2_decode ( void *  coder_ptr,
lzma_dict *restrict  dict,
const uint8_t *restrict  in,
size_t *restrict  in_pos,
size_t  in_size 
)
static

Definition at line 57 of file lzma2_decoder.c.

60 {
61  lzma_lzma2_coder *restrict coder = coder_ptr;
62 
63  // With SEQ_LZMA it is possible that no new input is needed to do
64  // some progress. The rest of the sequences assume that there is
65  // at least one byte of input.
66  while (*in_pos < in_size || coder->sequence == SEQ_LZMA)
67  switch (coder->sequence) {
68  case SEQ_CONTROL: {
69  const uint32_t control = in[*in_pos];
70  ++*in_pos;
71 
72  // End marker
73  if (control == 0x00)
74  return LZMA_STREAM_END;
75 
76  if (control >= 0xE0 || control == 1) {
77  // Dictionary reset implies that next LZMA chunk has
78  // to set new properties.
79  coder->need_properties = true;
80  coder->need_dictionary_reset = true;
81  } else if (coder->need_dictionary_reset) {
82  return LZMA_DATA_ERROR;
83  }
84 
85  if (control >= 0x80) {
86  // LZMA chunk. The highest five bits of the
87  // uncompressed size are taken from the control byte.
88  coder->uncompressed_size = (control & 0x1F) << 16;
89  coder->sequence = SEQ_UNCOMPRESSED_1;
90 
91  // See if there are new properties or if we need to
92  // reset the state.
93  if (control >= 0xC0) {
94  // When there are new properties, state reset
95  // is done at SEQ_PROPERTIES.
96  coder->need_properties = false;
97  coder->next_sequence = SEQ_PROPERTIES;
98 
99  } else if (coder->need_properties) {
100  return LZMA_DATA_ERROR;
101 
102  } else {
103  coder->next_sequence = SEQ_LZMA;
104 
105  // If only state reset is wanted with old
106  // properties, do the resetting here for
107  // simplicity.
108  if (control >= 0xA0)
109  coder->lzma.reset(coder->lzma.coder,
110  &coder->options);
111  }
112  } else {
113  // Invalid control values
114  if (control > 2)
115  return LZMA_DATA_ERROR;
116 
117  // It's uncompressed chunk
118  coder->sequence = SEQ_COMPRESSED_0;
119  coder->next_sequence = SEQ_COPY;
120  }
121 
122  if (coder->need_dictionary_reset) {
123  // Finish the dictionary reset and let the caller
124  // flush the dictionary to the actual output buffer.
125  coder->need_dictionary_reset = false;
126  dict_reset(dict);
127  return LZMA_OK;
128  }
129 
130  break;
131  }
132 
133  case SEQ_UNCOMPRESSED_1:
134  coder->uncompressed_size += (uint32_t)(in[(*in_pos)++]) << 8;
135  coder->sequence = SEQ_UNCOMPRESSED_2;
136  break;
137 
138  case SEQ_UNCOMPRESSED_2:
139  coder->uncompressed_size += in[(*in_pos)++] + 1U;
140  coder->sequence = SEQ_COMPRESSED_0;
141  coder->lzma.set_uncompressed(coder->lzma.coder,
142  coder->uncompressed_size);
143  break;
144 
145  case SEQ_COMPRESSED_0:
146  coder->compressed_size = (uint32_t)(in[(*in_pos)++]) << 8;
147  coder->sequence = SEQ_COMPRESSED_1;
148  break;
149 
150  case SEQ_COMPRESSED_1:
151  coder->compressed_size += in[(*in_pos)++] + 1U;
152  coder->sequence = coder->next_sequence;
153  break;
154 
155  case SEQ_PROPERTIES:
156  if (lzma_lzma_lclppb_decode(&coder->options, in[(*in_pos)++]))
157  return LZMA_DATA_ERROR;
158 
159  coder->lzma.reset(coder->lzma.coder, &coder->options);
160 
161  coder->sequence = SEQ_LZMA;
162  break;
163 
164  case SEQ_LZMA: {
165  // Store the start offset so that we can update
166  // coder->compressed_size later.
167  const size_t in_start = *in_pos;
168 
169  // Decode from in[] to *dict.
170  const lzma_ret ret = coder->lzma.code(coder->lzma.coder,
171  dict, in, in_pos, in_size);
172 
173  // Validate and update coder->compressed_size.
174  const size_t in_used = *in_pos - in_start;
175  if (in_used > coder->compressed_size)
176  return LZMA_DATA_ERROR;
177 
178  coder->compressed_size -= in_used;
179 
180  // Return if we didn't finish the chunk, or an error occurred.
181  if (ret != LZMA_STREAM_END)
182  return ret;
183 
184  // The LZMA decoder must have consumed the whole chunk now.
185  // We don't need to worry about uncompressed size since it
186  // is checked by the LZMA decoder.
187  if (coder->compressed_size != 0)
188  return LZMA_DATA_ERROR;
189 
190  coder->sequence = SEQ_CONTROL;
191  break;
192  }
193 
194  case SEQ_COPY: {
195  // Copy from input to the dictionary as is.
196  dict_write(dict, in, in_pos, in_size, &coder->compressed_size);
197  if (coder->compressed_size != 0)
198  return LZMA_OK;
199 
200  coder->sequence = SEQ_CONTROL;
201  break;
202  }
203 
204  default:
205  assert(0);
206  return LZMA_PROG_ERROR;
207  }
208 
209  return LZMA_OK;
210 }
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
#define restrict
static void dict_reset(lzma_dict *dict)
Definition: lz_decoder.h:228
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.
Definition: lz_decoder.h:203
bool lzma_lzma_lclppb_decode(lzma_options_lzma *options, uint8_t byte)
Decodes the LZMA Properties byte (lc/lp/pb)
assert(limit<=UINT32_MAX/2)
unsigned int uint32_t
Definition: sftypes.h:29
control
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218
@ LZMA_DATA_ERROR
Data is corrupt.
Definition: base.h:172
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58

References assert(), control, dict_reset(), dict_write(), in, in_pos, in_size, LZMA_DATA_ERROR, lzma_lzma_lclppb_decode(), LZMA_OK, LZMA_PROG_ERROR, LZMA_STREAM_END, and restrict.

Referenced by lzma2_decoder_init().

◆ lzma2_decoder_end()

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

Definition at line 214 of file lzma2_decoder.c.

215 {
216  lzma_lzma2_coder *coder = coder_ptr;
217 
218  assert(coder->lzma.end == NULL);
219  lzma_free(coder->lzma.coder, allocator);
220 
221  lzma_free(coder, allocator);
222 
223  return;
224 }
const lzma_allocator * allocator
Definition: block.h:377
#define NULL
Definition: cris-opc.c:27
void * coder
Data specific to the LZ-based decoder.
Definition: lz_decoder.h:56
void(* end)(void *coder, const lzma_allocator *allocator)
Free allocated resources.
Definition: lz_decoder.h:69
lzma_lz_decoder lzma
LZMA decoder.
Definition: lzma2_decoder.c:35
void lzma_free(void *ptr, const lzma_allocator *allocator)
Frees memory.
Definition: common.c:78

References allocator, assert(), lzma_lz_decoder::coder, lzma_lz_decoder::end, lzma_lzma2_coder::lzma, lzma_free(), and NULL.

Referenced by lzma2_decoder_init().

◆ lzma2_decoder_init()

static lzma_ret lzma2_decoder_init ( lzma_lz_decoder lz,
const lzma_allocator allocator,
const void *  opt,
lzma_lz_options lz_options 
)
static

Definition at line 228 of file lzma2_decoder.c.

230 {
231  lzma_lzma2_coder *coder = lz->coder;
232  if (coder == NULL) {
233  coder = lzma_alloc(sizeof(lzma_lzma2_coder), allocator);
234  if (coder == NULL)
235  return LZMA_MEM_ERROR;
236 
237  lz->coder = coder;
238  lz->code = &lzma2_decode;
239  lz->end = &lzma2_decoder_end;
240 
241  coder->lzma = LZMA_LZ_DECODER_INIT;
242  }
243 
244  const lzma_options_lzma *options = opt;
245 
246  coder->sequence = SEQ_CONTROL;
247  coder->need_properties = true;
248  coder->need_dictionary_reset = options->preset_dict == NULL
249  || options->preset_dict_size == 0;
250 
251  return lzma_lzma_decoder_create(&coder->lzma,
252  allocator, options, lz_options);
253 }
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
#define LZMA_LZ_DECODER_INIT
Definition: lz_decoder.h:74
static lzma_ret lzma2_decode(void *coder_ptr, lzma_dict *restrict dict, const uint8_t *restrict in, size_t *restrict in_pos, size_t in_size)
Definition: lzma2_decoder.c:57
static void lzma2_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
lzma_ret lzma_lzma_decoder_create(lzma_lz_decoder *lz, const lzma_allocator *allocator, const void *opt, lzma_lz_options *lz_options)
Definition: lzma_decoder.c:942
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
bool need_dictionary_reset
Definition: lzma2_decoder.c:50
Options specific to the LZMA1 and LZMA2 filters.
Definition: lzma12.h:185
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_lz_decoder::code, lzma_lz_decoder::coder, lzma_lz_decoder::end, lzma_lzma2_coder::lzma, lzma2_decode(), lzma2_decoder_end(), lzma_alloc(), LZMA_LZ_DECODER_INIT, lzma_lzma_decoder_create(), LZMA_MEM_ERROR, lzma_lzma2_coder::need_dictionary_reset, lzma_lzma2_coder::need_properties, NULL, and options.

Referenced by lzma_lzma2_decoder_init().

◆ lzma_lzma2_decoder_init()

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

Definition at line 257 of file lzma2_decoder.c.

259 {
260  // LZMA2 can only be the last filter in the chain. This is enforced
261  // by the raw_decoder initialization.
262  assert(filters[1].init == NULL);
263 
264  return lzma_lz_decoder_init(next, allocator, filters,
266 }
const lzma_filter * filters
Definition: container.h:315
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 lzma2_decoder_init(lzma_lz_decoder *lz, const lzma_allocator *allocator, const void *opt, lzma_lz_options *lz_options)
bool init
Definition: core.c:77

References allocator, assert(), filters, init, lzma2_decoder_init(), lzma_lz_decoder_init(), and NULL.

◆ lzma_lzma2_decoder_memusage()

uint64_t lzma_lzma2_decoder_memusage ( const void *  options)

Definition at line 270 of file lzma2_decoder.c.

271 {
272  return sizeof(lzma_lzma2_coder)
274 }
uint64_t lzma_lzma_decoder_memusage_nocheck(const void *options)

References lzma_lzma_decoder_memusage_nocheck(), and options.

◆ lzma_lzma2_props_decode()

lzma_ret lzma_lzma2_props_decode ( void **  options,
const lzma_allocator allocator,
const uint8_t props,
size_t  props_size 
)

Definition at line 278 of file lzma2_decoder.c.

280 {
281  if (props_size != 1)
282  return LZMA_OPTIONS_ERROR;
283 
284  // Check that reserved bits are unset.
285  if (props[0] & 0xC0)
286  return LZMA_OPTIONS_ERROR;
287 
288  // Decode the dictionary size.
289  if (props[0] > 40)
290  return LZMA_OPTIONS_ERROR;
291 
293  sizeof(lzma_options_lzma), allocator);
294  if (opt == NULL)
295  return LZMA_MEM_ERROR;
296 
297  if (props[0] == 40) {
298  opt->dict_size = UINT32_MAX;
299  } else {
300  opt->dict_size = 2 | (props[0] & 1U);
301  opt->dict_size <<= props[0] / 2U + 11;
302  }
303 
304  opt->preset_dict = NULL;
305  opt->preset_dict_size = 0;
306 
307  *options = opt;
308 
309  return LZMA_OK;
310 }
const lzma_allocator const uint8_t * props
Definition: filter.h:362
#define UINT32_MAX
const uint8_t * preset_dict
Pointer to an initial dictionary.
Definition: lzma12.h:240
uint32_t preset_dict_size
Size of the preset dictionary.
Definition: lzma12.h:254
uint32_t dict_size
Dictionary size in bytes.
Definition: lzma12.h:217
@ LZMA_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160