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

Autodetect between .xz Stream and .lzma (LZMA_Alone) formats. More...

#include "stream_decoder.h"
#include "alone_decoder.h"

Go to the source code of this file.

Classes

struct  lzma_auto_coder
 

Functions

static lzma_ret auto_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 auto_decoder_end (void *coder_ptr, const lzma_allocator *allocator)
 
static lzma_check auto_decoder_get_check (const void *coder_ptr)
 
static lzma_ret auto_decoder_memconfig (void *coder_ptr, uint64_t *memusage, uint64_t *old_memlimit, uint64_t new_memlimit)
 
static lzma_ret auto_decoder_init (lzma_next_coder *next, const lzma_allocator *allocator, uint64_t memlimit, uint32_t flags)
 
 LZMA_API (lzma_ret)
 

Detailed Description

Autodetect between .xz Stream and .lzma (LZMA_Alone) formats.

Definition in file auto_decoder.c.

Function Documentation

◆ auto_decode()

static lzma_ret auto_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 33 of file auto_decoder.c.

37 {
38  lzma_auto_coder *coder = coder_ptr;
39 
40  switch (coder->sequence) {
41  case SEQ_INIT:
42  if (*in_pos >= in_size)
43  return LZMA_OK;
44 
45  // Update the sequence now, because we want to continue from
46  // SEQ_CODE even if we return some LZMA_*_CHECK.
47  coder->sequence = SEQ_CODE;
48 
49  // Detect the file format. For now this is simple, since if
50  // it doesn't start with 0xFD (the first magic byte of the
51  // new format), it has to be LZMA_Alone, or something that
52  // we don't support at all.
53  if (in[*in_pos] == 0xFD) {
55  &coder->next, allocator,
56  coder->memlimit, coder->flags));
57  } else {
59  allocator, coder->memlimit, true));
60 
61  // If the application wants to know about missing
62  // integrity check or about the check in general, we
63  // need to handle it here, because LZMA_Alone decoder
64  // doesn't accept any flags.
65  if (coder->flags & LZMA_TELL_NO_CHECK)
66  return LZMA_NO_CHECK;
67 
68  if (coder->flags & LZMA_TELL_ANY_CHECK)
69  return LZMA_GET_CHECK;
70  }
71 
72  // Fall through
73 
74  case SEQ_CODE: {
75  const lzma_ret ret = coder->next.code(
76  coder->next.coder, allocator,
77  in, in_pos, in_size,
78  out, out_pos, out_size, action);
79  if (ret != LZMA_STREAM_END
80  || (coder->flags & LZMA_CONCATENATED) == 0)
81  return ret;
82 
83  coder->sequence = SEQ_FINISH;
84  }
85 
86  // Fall through
87 
88  case SEQ_FINISH:
89  // When LZMA_DECODE_CONCATENATED was used and we were decoding
90  // LZMA_Alone file, we need to check check that there is no
91  // trailing garbage and wait for LZMA_FINISH.
92  if (*in_pos < in_size)
93  return LZMA_DATA_ERROR;
94 
96 
97  default:
98  assert(0);
99  return LZMA_PROG_ERROR;
100  }
101 }
lzma_ret lzma_alone_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, uint64_t memlimit, bool picky)
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
#define LZMA_TELL_ANY_CHECK
Definition: container.h:474
#define LZMA_CONCATENATED
Definition: container.h:515
#define LZMA_TELL_NO_CHECK
Definition: container.h:457
assert(limit<=UINT32_MAX/2)
lzma_ret lzma_stream_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, uint64_t memlimit, uint32_t flags)
uint32_t flags
Definition: auto_decoder.c:22
enum lzma_auto_coder::@642 sequence
lzma_next_coder next
Stream decoder or LZMA_Alone decoder.
Definition: auto_decoder.c:19
uint64_t memlimit
Definition: auto_decoder.c:21
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 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_STREAM_END
End of stream was reached.
Definition: base.h:63
@ 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
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
@ LZMA_FINISH
Finish the coding operation.
Definition: base.h:328

References test-lz4-speed::action, allocator, assert(), lzma_next_coder_s::code, lzma_next_coder_s::coder, lzma_auto_coder::flags, in, in_pos, in_size, lzma_alone_decoder_init(), LZMA_CONCATENATED, LZMA_DATA_ERROR, LZMA_FINISH, LZMA_GET_CHECK, LZMA_NO_CHECK, LZMA_OK, LZMA_PROG_ERROR, lzma_stream_decoder_init(), LZMA_STREAM_END, LZMA_TELL_ANY_CHECK, LZMA_TELL_NO_CHECK, lzma_auto_coder::memlimit, lzma_auto_coder::next, out, out_pos, return_if_error, and lzma_auto_coder::sequence.

Referenced by auto_decoder_init().

◆ auto_decoder_end()

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

Definition at line 105 of file auto_decoder.c.

106 {
107  lzma_auto_coder *coder = coder_ptr;
108  lzma_next_end(&coder->next, allocator);
109  lzma_free(coder, allocator);
110  return;
111 }
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_free(), lzma_next_end(), and lzma_auto_coder::next.

Referenced by auto_decoder_init().

◆ auto_decoder_get_check()

static lzma_check auto_decoder_get_check ( const void *  coder_ptr)
static

Definition at line 115 of file auto_decoder.c.

116 {
117  const lzma_auto_coder *coder = coder_ptr;
118 
119  // It is LZMA_Alone if get_check is NULL.
120  return coder->next.get_check == NULL ? LZMA_CHECK_NONE
121  : coder->next.get_check(coder->next.coder);
122 }
@ LZMA_CHECK_NONE
Definition: check.h:28
#define NULL
Definition: cris-opc.c:27
lzma_check(* get_check)(const void *coder)
Definition: common.h:164

References lzma_next_coder_s::coder, lzma_next_coder_s::get_check, LZMA_CHECK_NONE, lzma_auto_coder::next, and NULL.

Referenced by auto_decoder_init().

◆ auto_decoder_init()

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

Definition at line 156 of file auto_decoder.c.

158 {
160 
162  return LZMA_OPTIONS_ERROR;
163 
164  lzma_auto_coder *coder = next->coder;
165  if (coder == NULL) {
166  coder = lzma_alloc(sizeof(lzma_auto_coder), allocator);
167  if (coder == NULL)
168  return LZMA_MEM_ERROR;
169 
170  next->coder = coder;
171  next->code = &auto_decode;
172  next->end = &auto_decoder_end;
175  coder->next = LZMA_NEXT_CODER_INIT;
176  }
177 
178  coder->memlimit = my_max(1, memlimit);
179  coder->flags = flags;
180  coder->sequence = SEQ_INIT;
181 
182  return LZMA_OK;
183 }
static lzma_check auto_decoder_get_check(const void *coder_ptr)
Definition: auto_decoder.c:115
static void auto_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
Definition: auto_decoder.c:105
static lzma_ret auto_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: auto_decoder.c:33
static lzma_ret auto_decoder_memconfig(void *coder_ptr, uint64_t *memusage, uint64_t *old_memlimit, uint64_t new_memlimit)
Definition: auto_decoder.c:126
static lzma_ret auto_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, uint64_t memlimit, uint32_t flags)
Definition: auto_decoder.c:156
uint64_t memlimit
Definition: container.h:537
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
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
#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_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, auto_decode(), auto_decoder_end(), auto_decoder_get_check(), auto_decoder_memconfig(), lzma_next_coder_s::code, lzma_next_coder_s::coder, lzma_next_coder_s::end, flags, lzma_auto_coder::flags, lzma_next_coder_s::get_check, lzma_alloc(), LZMA_MEM_ERROR, LZMA_NEXT_CODER_INIT, lzma_next_coder_init, LZMA_OK, LZMA_OPTIONS_ERROR, LZMA_SUPPORTED_FLAGS, lzma_next_coder_s::memconfig, memlimit, lzma_auto_coder::memlimit, my_max, lzma_auto_coder::next, NULL, and lzma_auto_coder::sequence.

Referenced by LZMA_API().

◆ auto_decoder_memconfig()

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

Definition at line 126 of file auto_decoder.c.

128 {
129  lzma_auto_coder *coder = coder_ptr;
130 
131  lzma_ret ret;
132 
133  if (coder->next.memconfig != NULL) {
134  ret = coder->next.memconfig(coder->next.coder,
135  memusage, old_memlimit, new_memlimit);
136  assert(*old_memlimit == coder->memlimit);
137  } else {
138  // No coder is configured yet. Use the base value as
139  // the current memory usage.
140  *memusage = LZMA_MEMUSAGE_BASE;
141  *old_memlimit = coder->memlimit;
142 
143  ret = LZMA_OK;
144  if (new_memlimit != 0 && new_memlimit < *memusage)
145  ret = LZMA_MEMLIMIT_ERROR;
146  }
147 
148  if (ret == LZMA_OK && new_memlimit != 0)
149  coder->memlimit = new_memlimit;
150 
151  return ret;
152 }
#define LZMA_MEMUSAGE_BASE
Definition: common.h:63
@ LZMA_MEMLIMIT_ERROR
Definition: base.h:140

References assert(), lzma_next_coder_s::coder, LZMA_MEMLIMIT_ERROR, LZMA_MEMUSAGE_BASE, LZMA_OK, lzma_next_coder_s::memconfig, lzma_auto_coder::memlimit, lzma_auto_coder::next, and NULL.

Referenced by auto_decoder_init().

◆ LZMA_API()

LZMA_API ( lzma_ret  )

Definition at line 186 of file auto_decoder.c.

188 {
190 
193 
194  return LZMA_OK;
195 }
static lzma_stream strm
Definition: full_flush.c:20
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_RUN
Continue coding.
Definition: base.h:251

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