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

Decodes .xz Streams. More...

#include "stream_decoder.h"
#include "block_decoder.h"

Go to the source code of this file.

Classes

struct  lzma_stream_coder
 

Functions

static lzma_ret stream_decoder_reset (lzma_stream_coder *coder, const lzma_allocator *allocator)
 
static lzma_ret stream_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 stream_decoder_end (void *coder_ptr, const lzma_allocator *allocator)
 
static lzma_check stream_decoder_get_check (const void *coder_ptr)
 
static lzma_ret stream_decoder_memconfig (void *coder_ptr, uint64_t *memusage, uint64_t *old_memlimit, uint64_t new_memlimit)
 
lzma_ret lzma_stream_decoder_init (lzma_next_coder *next, const lzma_allocator *allocator, uint64_t memlimit, uint32_t flags)
 
 LZMA_API (lzma_ret)
 

Detailed Description

Decodes .xz Streams.

Definition in file stream_decoder.c.

Function Documentation

◆ LZMA_API()

LZMA_API ( lzma_ret  )

Definition at line 458 of file stream_decoder.c.

460 {
462 
465 
466  return LZMA_OK;
467 }
uint64_t memlimit
Definition: container.h:537
static lzma_stream strm
Definition: full_flush.c:20
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
lzma_ret lzma_stream_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, uint64_t memlimit, uint32_t flags)
bool supported_actions[LZMA_ACTION_MAX+1]
Indicates which lzma_action values are allowed by next.code.
Definition: common.h:220
lzma_internal * internal
Definition: base.h:505
#define lzma_next_strm_init(func, strm,...)
Definition: common.h:303
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
@ LZMA_FINISH
Finish the coding operation.
Definition: base.h:328
@ LZMA_RUN
Continue coding.
Definition: base.h:251

References flags, lzma_stream::internal, LZMA_FINISH, lzma_next_strm_init, LZMA_OK, LZMA_RUN, lzma_stream_decoder_init(), memlimit, strm, and lzma_internal_s::supported_actions.

◆ lzma_stream_decoder_init()

lzma_ret lzma_stream_decoder_init ( lzma_next_coder next,
const lzma_allocator allocator,
uint64_t  memlimit,
uint32_t  flags 
)

Definition at line 419 of file stream_decoder.c.

422 {
424 
426  return LZMA_OPTIONS_ERROR;
427 
428  lzma_stream_coder *coder = next->coder;
429  if (coder == NULL) {
430  coder = lzma_alloc(sizeof(lzma_stream_coder), allocator);
431  if (coder == NULL)
432  return LZMA_MEM_ERROR;
433 
434  next->coder = coder;
435  next->code = &stream_decode;
436  next->end = &stream_decoder_end;
439 
441  coder->index_hash = NULL;
442  }
443 
444  coder->memlimit = my_max(1, memlimit);
445  coder->memusage = LZMA_MEMUSAGE_BASE;
446  coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
449  coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
450  coder->ignore_check = (flags & LZMA_IGNORE_CHECK) != 0;
451  coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
452  coder->first_stream = true;
453 
454  return stream_decoder_reset(coder, allocator);
455 }
const lzma_allocator * allocator
Definition: block.h:377
#define LZMA_TELL_ANY_CHECK
Definition: container.h:474
#define LZMA_IGNORE_CHECK
Definition: container.h:498
#define LZMA_CONCATENATED
Definition: container.h:515
#define LZMA_TELL_NO_CHECK
Definition: container.h:457
#define LZMA_TELL_UNSUPPORTED_CHECK
Definition: container.h:466
#define NULL
Definition: cris-opc.c:27
static lzma_ret stream_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 stream_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
static lzma_ret stream_decoder_reset(lzma_stream_coder *coder, const lzma_allocator *allocator)
static lzma_check stream_decoder_get_check(const void *coder_ptr)
static lzma_ret stream_decoder_memconfig(void *coder_ptr, uint64_t *memusage, uint64_t *old_memlimit, uint64_t new_memlimit)
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_check(* get_check)(const void *coder)
Definition: common.h:164
lzma_end_function end
Definition: common.h:155
lzma_ret(* memconfig)(void *coder, uint64_t *memusage, uint64_t *old_memlimit, uint64_t new_memlimit)
Definition: common.h:168
lzma_index_hash * index_hash
uint64_t memlimit
Memory usage limit.
lzma_next_coder block_decoder
bool tell_any_check
If true, LZMA_GET_CHECK is returned after decoding Stream Header.
uint64_t memusage
Amount of memory actually needed (only an estimate)
#define LZMA_NEXT_CODER_INIT
Macro to initialize lzma_next_coder structure.
Definition: common.h:180
#define LZMA_SUPPORTED_FLAGS
Definition: common.h:72
#define LZMA_MEMUSAGE_BASE
Definition: common.h:63
#define lzma_next_coder_init(func, next, allocator)
Definition: common.h:291
void * lzma_alloc(size_t size, const lzma_allocator *allocator) lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
Allocates memory.
#define my_max(x, y)
Definition: sysdefs.h:186
@ LZMA_MEM_ERROR
Cannot allocate memory.
Definition: base.h:128
@ LZMA_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160

References allocator, lzma_stream_coder::block_decoder, lzma_next_coder_s::code, lzma_next_coder_s::coder, lzma_stream_coder::concatenated, lzma_next_coder_s::end, lzma_stream_coder::first_stream, flags, lzma_next_coder_s::get_check, lzma_stream_coder::ignore_check, lzma_stream_coder::index_hash, lzma_alloc(), LZMA_CONCATENATED, LZMA_IGNORE_CHECK, LZMA_MEM_ERROR, LZMA_MEMUSAGE_BASE, LZMA_NEXT_CODER_INIT, lzma_next_coder_init, LZMA_OPTIONS_ERROR, LZMA_SUPPORTED_FLAGS, LZMA_TELL_ANY_CHECK, LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, lzma_next_coder_s::memconfig, memlimit, lzma_stream_coder::memlimit, lzma_stream_coder::memusage, my_max, NULL, stream_decode(), stream_decoder_end(), stream_decoder_get_check(), stream_decoder_memconfig(), stream_decoder_reset(), lzma_stream_coder::tell_any_check, lzma_stream_coder::tell_no_check, and lzma_stream_coder::tell_unsupported_check.

Referenced by auto_decode(), and LZMA_API().

◆ stream_decode()

static lzma_ret stream_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 103 of file stream_decoder.c.

107 {
108  lzma_stream_coder *coder = coder_ptr;
109 
110  // When decoding the actual Block, it may be able to produce more
111  // output even if we don't give it any new input.
112  while (true)
113  switch (coder->sequence) {
114  case SEQ_STREAM_HEADER: {
115  // Copy the Stream Header to the internal buffer.
116  lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
118 
119  // Return if we didn't get the whole Stream Header yet.
120  if (coder->pos < LZMA_STREAM_HEADER_SIZE)
121  return LZMA_OK;
122 
123  coder->pos = 0;
124 
125  // Decode the Stream Header.
126  const lzma_ret ret = lzma_stream_header_decode(
127  &coder->stream_flags, coder->buffer);
128  if (ret != LZMA_OK)
129  return ret == LZMA_FORMAT_ERROR && !coder->first_stream
130  ? LZMA_DATA_ERROR : ret;
131 
132  // If we are decoding concatenated Streams, and the later
133  // Streams have invalid Header Magic Bytes, we give
134  // LZMA_DATA_ERROR instead of LZMA_FORMAT_ERROR.
135  coder->first_stream = false;
136 
137  // Copy the type of the Check so that Block Header and Block
138  // decoders see it.
139  coder->block_options.check = coder->stream_flags.check;
140 
141  // Even if we return LZMA_*_CHECK below, we want
142  // to continue from Block Header decoding.
143  coder->sequence = SEQ_BLOCK_HEADER;
144 
145  // Detect if there's no integrity check or if it is
146  // unsupported if those were requested by the application.
147  if (coder->tell_no_check && coder->stream_flags.check
148  == LZMA_CHECK_NONE)
149  return LZMA_NO_CHECK;
150 
151  if (coder->tell_unsupported_check
152  && !lzma_check_is_supported(
153  coder->stream_flags.check))
154  return LZMA_UNSUPPORTED_CHECK;
155 
156  if (coder->tell_any_check)
157  return LZMA_GET_CHECK;
158  }
159 
160  // Fall through
161 
162  case SEQ_BLOCK_HEADER: {
163  if (*in_pos >= in_size)
164  return LZMA_OK;
165 
166  if (coder->pos == 0) {
167  // Detect if it's Index.
168  if (in[*in_pos] == 0x00) {
169  coder->sequence = SEQ_INDEX;
170  break;
171  }
172 
173  // Calculate the size of the Block Header. Note that
174  // Block Header decoder wants to see this byte too
175  // so don't advance *in_pos.
178  in[*in_pos]);
179  }
180 
181  // Copy the Block Header to the internal buffer.
182  lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
183  coder->block_options.header_size);
184 
185  // Return if we didn't get the whole Block Header yet.
186  if (coder->pos < coder->block_options.header_size)
187  return LZMA_OK;
188 
189  coder->pos = 0;
190 
191  // Version 1 is needed to support the .ignore_check option.
192  coder->block_options.version = 1;
193 
194  // Set up a buffer to hold the filter chain. Block Header
195  // decoder will initialize all members of this array so
196  // we don't need to do it here.
198  coder->block_options.filters = filters;
199 
200  // Decode the Block Header.
201  return_if_error(lzma_block_header_decode(&coder->block_options,
202  allocator, coder->buffer));
203 
204  // If LZMA_IGNORE_CHECK was used, this flag needs to be set.
205  // It has to be set after lzma_block_header_decode() because
206  // it always resets this to false.
207  coder->block_options.ignore_check = coder->ignore_check;
208 
209  // Check the memory usage limit.
210  const uint64_t memusage = lzma_raw_decoder_memusage(filters);
211  lzma_ret ret;
212 
213  if (memusage == UINT64_MAX) {
214  // One or more unknown Filter IDs.
215  ret = LZMA_OPTIONS_ERROR;
216  } else {
217  // Now we can set coder->memusage since we know that
218  // the filter chain is valid. We don't want
219  // lzma_memusage() to return UINT64_MAX in case of
220  // invalid filter chain.
221  coder->memusage = memusage;
222 
223  if (memusage > coder->memlimit) {
224  // The chain would need too much memory.
225  ret = LZMA_MEMLIMIT_ERROR;
226  } else {
227  // Memory usage is OK.
228  // Initialize the Block decoder.
230  &coder->block_decoder,
231  allocator,
232  &coder->block_options);
233  }
234  }
235 
236  // Free the allocated filter options since they are needed
237  // only to initialize the Block decoder.
238  for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i)
240 
241  coder->block_options.filters = NULL;
242 
243  // Check if memory usage calculation and Block enocoder
244  // initialization succeeded.
245  if (ret != LZMA_OK)
246  return ret;
247 
248  coder->sequence = SEQ_BLOCK;
249  }
250 
251  // Fall through
252 
253  case SEQ_BLOCK: {
254  const lzma_ret ret = coder->block_decoder.code(
255  coder->block_decoder.coder, allocator,
256  in, in_pos, in_size, out, out_pos, out_size,
257  action);
258 
259  if (ret != LZMA_STREAM_END)
260  return ret;
261 
262  // Block decoded successfully. Add the new size pair to
263  // the Index hash.
264  return_if_error(lzma_index_hash_append(coder->index_hash,
265  lzma_block_unpadded_size(
266  &coder->block_options),
268 
269  coder->sequence = SEQ_BLOCK_HEADER;
270  break;
271  }
272 
273  case SEQ_INDEX: {
274  // If we don't have any input, don't call
275  // lzma_index_hash_decode() since it would return
276  // LZMA_BUF_ERROR, which we must not do here.
277  if (*in_pos >= in_size)
278  return LZMA_OK;
279 
280  // Decode the Index and compare it to the hash calculated
281  // from the sizes of the Blocks (if any).
282  const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
283  in, in_pos, in_size);
284  if (ret != LZMA_STREAM_END)
285  return ret;
286 
287  coder->sequence = SEQ_STREAM_FOOTER;
288  }
289 
290  // Fall through
291 
292  case SEQ_STREAM_FOOTER: {
293  // Copy the Stream Footer to the internal buffer.
294  lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
296 
297  // Return if we didn't get the whole Stream Footer yet.
298  if (coder->pos < LZMA_STREAM_HEADER_SIZE)
299  return LZMA_OK;
300 
301  coder->pos = 0;
302 
303  // Decode the Stream Footer. The decoder gives
304  // LZMA_FORMAT_ERROR if the magic bytes don't match,
305  // so convert that return code to LZMA_DATA_ERROR.
306  lzma_stream_flags footer_flags;
307  const lzma_ret ret = lzma_stream_footer_decode(
308  &footer_flags, coder->buffer);
309  if (ret != LZMA_OK)
310  return ret == LZMA_FORMAT_ERROR
311  ? LZMA_DATA_ERROR : ret;
312 
313  // Check that Index Size stored in the Stream Footer matches
314  // the real size of the Index field.
315  if (lzma_index_hash_size(coder->index_hash)
316  != footer_flags.backward_size)
317  return LZMA_DATA_ERROR;
318 
319  // Compare that the Stream Flags fields are identical in
320  // both Stream Header and Stream Footer.
321  return_if_error(lzma_stream_flags_compare(
322  &coder->stream_flags, &footer_flags));
323 
324  if (!coder->concatenated)
325  return LZMA_STREAM_END;
326 
327  coder->sequence = SEQ_STREAM_PADDING;
328  }
329 
330  // Fall through
331 
332  case SEQ_STREAM_PADDING:
333  assert(coder->concatenated);
334 
335  // Skip over possible Stream Padding.
336  while (true) {
337  if (*in_pos >= in_size) {
338  // Unless LZMA_FINISH was used, we cannot
339  // know if there's more input coming later.
340  if (action != LZMA_FINISH)
341  return LZMA_OK;
342 
343  // Stream Padding must be a multiple of
344  // four bytes.
345  return coder->pos == 0
347  : LZMA_DATA_ERROR;
348  }
349 
350  // If the byte is not zero, it probably indicates
351  // beginning of a new Stream (or the file is corrupt).
352  if (in[*in_pos] != 0x00)
353  break;
354 
355  ++*in_pos;
356  coder->pos = (coder->pos + 1) & 3;
357  }
358 
359  // Stream Padding must be a multiple of four bytes (empty
360  // Stream Padding is OK).
361  if (coder->pos != 0) {
362  ++*in_pos;
363  return LZMA_DATA_ERROR;
364  }
365 
366  // Prepare to decode the next Stream.
368  break;
369 
370  default:
371  assert(0);
372  return LZMA_PROG_ERROR;
373  }
374 
375  // Never reached
376 }
@ LZMA_CHECK_NONE
Definition: check.h:28
lzma_index ** i
Definition: index.h:629
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
#define lzma_block_header_size_decode(b)
Decode the Block Header Size field.
Definition: block.h:285
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
lzma_ret lzma_block_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, lzma_block *block)
const lzma_filter * filters
Definition: container.h:315
#define LZMA_FILTERS_MAX
Maximum number of filters in a chain.
Definition: filter.h:26
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
assert(limit<=UINT32_MAX/2)
unsigned long uint64_t
Definition: sftypes.h:28
#define UINT64_MAX
#define LZMA_STREAM_HEADER_SIZE
Size of Stream Header and Stream Footer.
Definition: stream_flags.h:27
lzma_vli uncompressed_size
Uncompressed Size in bytes.
Definition: block.h:172
lzma_bool ignore_check
A flag to Block decoder to not verify the Check field.
Definition: block.h:261
lzma_filter * filters
Array of filters.
Definition: block.h:200
uint32_t header_size
Size of the Block Header field.
Definition: block.h:72
lzma_check check
Type of integrity Check.
Definition: block.h:93
uint32_t version
Block format version.
Definition: block.h:52
Filter options.
Definition: filter.h:43
uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX]
lzma_block block_options
Options for the Block encoder.
size_t pos
Write position in buffer[] and position in Stream Padding.
enum lzma_stream_coder::@652 sequence
lzma_stream_flags stream_flags
Stream Flags from Stream Header.
Options for encoding/decoding Stream Header and Stream Footer.
Definition: stream_flags.h:33
lzma_vli backward_size
Backward Size.
Definition: stream_flags.h:69
lzma_check check
Check ID.
Definition: stream_flags.h:79
#define return_if_error(expr)
Return if expression doesn't evaluate to LZMA_OK.
Definition: common.h:278
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_FORMAT_ERROR
Memory usage limit was reached.
Definition: base.h:150
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_UNSUPPORTED_CHECK
Cannot calculate the integrity check.
Definition: base.h:90
@ LZMA_MEMLIMIT_ERROR
Definition: base.h:140
@ LZMA_GET_CHECK
Integrity check type is now available.
Definition: base.h:115
@ LZMA_NO_CHECK
Input stream has no integrity check.
Definition: base.h:75
void lzma_free(void *ptr, const lzma_allocator *allocator)
Frees memory.
Definition: common.c:78
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 test-lz4-speed::action, allocator, assert(), lzma_stream_flags::backward_size, lzma_stream_coder::block_decoder, lzma_stream_coder::block_options, lzma_stream_coder::buffer, lzma_block::check, lzma_stream_flags::check, lzma_next_coder_s::code, lzma_next_coder_s::coder, lzma_stream_coder::concatenated, lzma_block::filters, filters, lzma_stream_coder::first_stream, lzma_block::header_size, i, lzma_block::ignore_check, lzma_stream_coder::ignore_check, in, in_pos, in_size, lzma_stream_coder::index_hash, lzma_block_decoder_init(), lzma_block_header_size_decode, lzma_bufcpy(), LZMA_CHECK_NONE, LZMA_DATA_ERROR, LZMA_FILTERS_MAX, LZMA_FINISH, LZMA_FORMAT_ERROR, lzma_free(), LZMA_GET_CHECK, LZMA_MEMLIMIT_ERROR, LZMA_NO_CHECK, LZMA_OK, LZMA_OPTIONS_ERROR, LZMA_PROG_ERROR, LZMA_STREAM_END, LZMA_STREAM_HEADER_SIZE, LZMA_UNSUPPORTED_CHECK, lzma_stream_coder::memlimit, lzma_stream_coder::memusage, NULL, options, out, out_pos, lzma_stream_coder::pos, return_if_error, lzma_stream_coder::sequence, stream_decoder_reset(), lzma_stream_coder::stream_flags, lzma_stream_coder::tell_any_check, lzma_stream_coder::tell_no_check, lzma_stream_coder::tell_unsupported_check, UINT64_MAX, lzma_block::uncompressed_size, and lzma_block::version.

Referenced by lzma_stream_decoder_init().

◆ stream_decoder_end()

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

Definition at line 380 of file stream_decoder.c.

381 {
382  lzma_stream_coder *coder = coder_ptr;
384  lzma_index_hash_end(coder->index_hash, allocator);
385  lzma_free(coder, allocator);
386  return;
387 }
void lzma_next_end(lzma_next_coder *next, const lzma_allocator *allocator)
Definition: common.c:145

References allocator, lzma_stream_coder::block_decoder, lzma_stream_coder::index_hash, lzma_free(), and lzma_next_end().

Referenced by lzma_stream_decoder_init().

◆ stream_decoder_get_check()

static lzma_check stream_decoder_get_check ( const void *  coder_ptr)
static

Definition at line 391 of file stream_decoder.c.

392 {
393  const lzma_stream_coder *coder = coder_ptr;
394  return coder->stream_flags.check;
395 }

References lzma_stream_flags::check, and lzma_stream_coder::stream_flags.

Referenced by lzma_stream_decoder_init().

◆ stream_decoder_memconfig()

static lzma_ret stream_decoder_memconfig ( void *  coder_ptr,
uint64_t memusage,
uint64_t old_memlimit,
uint64_t  new_memlimit 
)
static

Definition at line 399 of file stream_decoder.c.

401 {
402  lzma_stream_coder *coder = coder_ptr;
403 
404  *memusage = coder->memusage;
405  *old_memlimit = coder->memlimit;
406 
407  if (new_memlimit != 0) {
408  if (new_memlimit < coder->memusage)
409  return LZMA_MEMLIMIT_ERROR;
410 
411  coder->memlimit = new_memlimit;
412  }
413 
414  return LZMA_OK;
415 }

References LZMA_MEMLIMIT_ERROR, LZMA_OK, lzma_stream_coder::memlimit, and lzma_stream_coder::memusage.

Referenced by lzma_stream_decoder_init().

◆ stream_decoder_reset()

static lzma_ret stream_decoder_reset ( lzma_stream_coder coder,
const lzma_allocator allocator 
)
static

Definition at line 87 of file stream_decoder.c.

88 {
89  // Initialize the Index hash used to verify the Index.
90  coder->index_hash = lzma_index_hash_init(coder->index_hash, allocator);
91  if (coder->index_hash == NULL)
92  return LZMA_MEM_ERROR;
93 
94  // Reset the rest of the variables.
95  coder->sequence = SEQ_STREAM_HEADER;
96  coder->pos = 0;
97 
98  return LZMA_OK;
99 }

References allocator, lzma_stream_coder::index_hash, LZMA_MEM_ERROR, LZMA_OK, NULL, lzma_stream_coder::pos, and lzma_stream_coder::sequence.

Referenced by lzma_stream_decoder_init(), and stream_decode().