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

Decodes the Index field. More...

#include "index.h"
#include "check.h"

Go to the source code of this file.

Classes

struct  lzma_index_coder
 

Functions

static lzma_ret index_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 lzma_attribute((__unused__)), size_t *restrict out_pos lzma_attribute((__unused__)), size_t out_size lzma_attribute((__unused__)), lzma_action action lzma_attribute((__unused__)))
 
static void index_decoder_end (void *coder_ptr, const lzma_allocator *allocator)
 
static lzma_ret index_decoder_memconfig (void *coder_ptr, uint64_t *memusage, uint64_t *old_memlimit, uint64_t new_memlimit)
 
static lzma_ret index_decoder_reset (lzma_index_coder *coder, const lzma_allocator *allocator, lzma_index **i, uint64_t memlimit)
 
static lzma_ret index_decoder_init (lzma_next_coder *next, const lzma_allocator *allocator, lzma_index **i, uint64_t memlimit)
 
 LZMA_API (lzma_ret)
 

Detailed Description

Decodes the Index field.

Definition in file index_decoder.c.

Function Documentation

◆ index_decode()

static lzma_ret index_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   lzma_attribute(__unused__),
size_t *restrict out_pos   lzma_attribute(__unused__),
size_t out_size   lzma_attribute(__unused__),
lzma_action action   lzma_attribute(__unused__) 
)
static

Definition at line 57 of file index_decoder.c.

64 {
65  lzma_index_coder *coder = coder_ptr;
66 
67  // Similar optimization as in index_encoder.c
68  const size_t in_start = *in_pos;
69  lzma_ret ret = LZMA_OK;
70 
71  while (*in_pos < in_size)
72  switch (coder->sequence) {
73  case SEQ_INDICATOR:
74  // Return LZMA_DATA_ERROR instead of e.g. LZMA_PROG_ERROR or
75  // LZMA_FORMAT_ERROR, because a typical usage case for Index
76  // decoder is when parsing the Stream backwards. If seeking
77  // backward from the Stream Footer gives us something that
78  // doesn't begin with Index Indicator, the file is considered
79  // corrupt, not "programming error" or "unrecognized file
80  // format". One could argue that the application should
81  // verify the Index Indicator before trying to decode the
82  // Index, but well, I suppose it is simpler this way.
83  if (in[(*in_pos)++] != 0x00)
84  return LZMA_DATA_ERROR;
85 
86  coder->sequence = SEQ_COUNT;
87  break;
88 
89  case SEQ_COUNT:
90  ret = lzma_vli_decode(&coder->count, &coder->pos,
91  in, in_pos, in_size);
92  if (ret != LZMA_STREAM_END)
93  goto out;
94 
95  coder->pos = 0;
96  coder->sequence = SEQ_MEMUSAGE;
97 
98  // Fall through
99 
100  case SEQ_MEMUSAGE:
101  if (lzma_index_memusage(1, coder->count) > coder->memlimit) {
102  ret = LZMA_MEMLIMIT_ERROR;
103  goto out;
104  }
105 
106  // Tell the Index handling code how many Records this
107  // Index has to allow it to allocate memory more efficiently.
108  lzma_index_prealloc(coder->index, coder->count);
109 
110  ret = LZMA_OK;
111  coder->sequence = coder->count == 0
112  ? SEQ_PADDING_INIT : SEQ_UNPADDED;
113  break;
114 
115  case SEQ_UNPADDED:
116  case SEQ_UNCOMPRESSED: {
117  lzma_vli *size = coder->sequence == SEQ_UNPADDED
118  ? &coder->unpadded_size
119  : &coder->uncompressed_size;
120 
121  ret = lzma_vli_decode(size, &coder->pos,
122  in, in_pos, in_size);
123  if (ret != LZMA_STREAM_END)
124  goto out;
125 
126  ret = LZMA_OK;
127  coder->pos = 0;
128 
129  if (coder->sequence == SEQ_UNPADDED) {
130  // Validate that encoded Unpadded Size isn't too small
131  // or too big.
132  if (coder->unpadded_size < UNPADDED_SIZE_MIN
133  || coder->unpadded_size
135  return LZMA_DATA_ERROR;
136 
137  coder->sequence = SEQ_UNCOMPRESSED;
138  } else {
139  // Add the decoded Record to the Index.
140  return_if_error(lzma_index_append(
141  coder->index, allocator,
142  coder->unpadded_size,
143  coder->uncompressed_size));
144 
145  // Check if this was the last Record.
146  coder->sequence = --coder->count == 0
147  ? SEQ_PADDING_INIT
148  : SEQ_UNPADDED;
149  }
150 
151  break;
152  }
153 
154  case SEQ_PADDING_INIT:
155  coder->pos = lzma_index_padding_size(coder->index);
156  coder->sequence = SEQ_PADDING;
157 
158  // Fall through
159 
160  case SEQ_PADDING:
161  if (coder->pos > 0) {
162  --coder->pos;
163  if (in[(*in_pos)++] != 0x00)
164  return LZMA_DATA_ERROR;
165 
166  break;
167  }
168 
169  // Finish the CRC32 calculation.
170  coder->crc32 = lzma_crc32(in + in_start,
171  *in_pos - in_start, coder->crc32);
172 
173  coder->sequence = SEQ_CRC32;
174 
175  // Fall through
176 
177  case SEQ_CRC32:
178  do {
179  if (*in_pos == in_size)
180  return LZMA_OK;
181 
182  if (((coder->crc32 >> (coder->pos * 8)) & 0xFF)
183  != in[(*in_pos)++])
184  return LZMA_DATA_ERROR;
185 
186  } while (++coder->pos < 4);
187 
188  // Decoding was successful, now we can let the application
189  // see the decoded Index.
190  *coder->index_ptr = coder->index;
191 
192  // Make index NULL so we don't free it unintentionally.
193  coder->index = NULL;
194 
195  return LZMA_STREAM_END;
196 
197  default:
198  assert(0);
199  return LZMA_PROG_ERROR;
200  }
201 
202 out:
203  // Update the CRC32,
204  coder->crc32 = lzma_crc32(in + in_start,
205  *in_pos - in_start, coder->crc32);
206 
207  return ret;
208 }
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 UNPADDED_SIZE_MAX
Maximum Unpadded Size.
Definition: index.h:23
#define UNPADDED_SIZE_MIN
Minimum Unpadded Size.
Definition: index.h:20
#define NULL
Definition: cris-opc.c:27
void lzma_index_prealloc(lzma_index *i, lzma_vli records)
Definition: index.c:431
uint32_t lzma_index_padding_size(const lzma_index *i)
Definition: index.c:593
voidpf void uLong size
Definition: ioapi.h:138
assert(limit<=UINT32_MAX/2)
uint64_t memlimit
Memory usage limit.
Definition: index_decoder.c:30
lzma_vli unpadded_size
The most recent Unpadded Size field.
Definition: index_decoder.c:43
lzma_vli count
Number of Records left to decode.
Definition: index_decoder.c:40
lzma_vli uncompressed_size
The most recent Uncompressed Size field.
Definition: index_decoder.c:46
lzma_index * index
Target Index.
Definition: index_decoder.c:33
lzma_index ** index_ptr
Definition: index_decoder.c:37
enum lzma_index_coder::@649 sequence
size_t pos
Position in integers.
Definition: index_decoder.c:49
uint32_t crc32
CRC32 of the List of Records field.
Definition: index_decoder.c:52
#define return_if_error(expr)
Return if expression doesn't evaluate to LZMA_OK.
Definition: common.h:278
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_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_MEMLIMIT_ERROR
Definition: base.h:140
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58

References allocator, assert(), lzma_index_coder::count, lzma_index_coder::crc32, in, in_pos, in_size, lzma_index_coder::index, lzma_index_coder::index_ptr, LZMA_DATA_ERROR, lzma_index_padding_size(), lzma_index_prealloc(), LZMA_MEMLIMIT_ERROR, LZMA_OK, LZMA_PROG_ERROR, LZMA_STREAM_END, lzma_index_coder::memlimit, NULL, out, lzma_index_coder::pos, return_if_error, lzma_index_coder::sequence, lzma_index_coder::uncompressed_size, lzma_index_coder::unpadded_size, UNPADDED_SIZE_MAX, and UNPADDED_SIZE_MIN.

Referenced by index_decoder_init().

◆ index_decoder_end()

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

Definition at line 212 of file index_decoder.c.

213 {
214  lzma_index_coder *coder = coder_ptr;
215  lzma_index_end(coder->index, allocator);
216  lzma_free(coder, allocator);
217  return;
218 }
void lzma_free(void *ptr, const lzma_allocator *allocator)
Frees memory.
Definition: common.c:78

References allocator, lzma_index_coder::index, and lzma_free().

Referenced by index_decoder_init().

◆ index_decoder_init()

static lzma_ret index_decoder_init ( lzma_next_coder next,
const lzma_allocator allocator,
lzma_index **  i,
uint64_t  memlimit 
)
static

Definition at line 269 of file index_decoder.c.

271 {
273 
274  if (i == NULL)
275  return LZMA_PROG_ERROR;
276 
277  lzma_index_coder *coder = next->coder;
278  if (coder == NULL) {
279  coder = lzma_alloc(sizeof(lzma_index_coder), allocator);
280  if (coder == NULL)
281  return LZMA_MEM_ERROR;
282 
283  next->coder = coder;
284  next->code = &index_decode;
285  next->end = &index_decoder_end;
287  coder->index = NULL;
288  } else {
289  lzma_index_end(coder->index, allocator);
290  }
291 
292  return index_decoder_reset(coder, allocator, i, memlimit);
293 }
lzma_index ** i
Definition: index.h:629
uint64_t memlimit
Definition: container.h:537
static lzma_ret index_decoder_memconfig(void *coder_ptr, uint64_t *memusage, uint64_t *old_memlimit, uint64_t new_memlimit)
static lzma_ret index_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 lzma_attribute((__unused__)), size_t *restrict out_pos lzma_attribute((__unused__)), size_t out_size lzma_attribute((__unused__)), lzma_action action lzma_attribute((__unused__)))
Definition: index_decoder.c:57
static lzma_ret index_decoder_reset(lzma_index_coder *coder, const lzma_allocator *allocator, lzma_index **i, uint64_t memlimit)
static void index_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
static lzma_ret index_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, lzma_index **i, uint64_t 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_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(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.
@ LZMA_MEM_ERROR
Cannot allocate memory.
Definition: base.h:128

References allocator, lzma_next_coder_s::code, lzma_next_coder_s::coder, lzma_next_coder_s::end, i, lzma_index_coder::index, index_decode(), index_decoder_end(), index_decoder_memconfig(), index_decoder_reset(), lzma_alloc(), LZMA_MEM_ERROR, lzma_next_coder_init, LZMA_PROG_ERROR, lzma_next_coder_s::memconfig, memlimit, and NULL.

Referenced by LZMA_API().

◆ index_decoder_memconfig()

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

Definition at line 222 of file index_decoder.c.

224 {
225  lzma_index_coder *coder = coder_ptr;
226 
227  *memusage = lzma_index_memusage(1, coder->count);
228  *old_memlimit = coder->memlimit;
229 
230  if (new_memlimit != 0) {
231  if (new_memlimit < *memusage)
232  return LZMA_MEMLIMIT_ERROR;
233 
234  coder->memlimit = new_memlimit;
235  }
236 
237  return LZMA_OK;
238 }

References lzma_index_coder::count, LZMA_MEMLIMIT_ERROR, LZMA_OK, and lzma_index_coder::memlimit.

Referenced by index_decoder_init().

◆ index_decoder_reset()

static lzma_ret index_decoder_reset ( lzma_index_coder coder,
const lzma_allocator allocator,
lzma_index **  i,
uint64_t  memlimit 
)
static

Definition at line 242 of file index_decoder.c.

244 {
245  // Remember the pointer given by the application. We will set it
246  // to point to the decoded Index only if decoding is successful.
247  // Before that, keep it NULL so that applications can always safely
248  // pass it to lzma_index_end() no matter did decoding succeed or not.
249  coder->index_ptr = i;
250  *i = NULL;
251 
252  // We always allocate a new lzma_index.
253  coder->index = lzma_index_init(allocator);
254  if (coder->index == NULL)
255  return LZMA_MEM_ERROR;
256 
257  // Initialize the rest.
258  coder->sequence = SEQ_INDICATOR;
259  coder->memlimit = my_max(1, memlimit);
260  coder->count = 0; // Needs to be initialized due to _memconfig().
261  coder->pos = 0;
262  coder->crc32 = 0;
263 
264  return LZMA_OK;
265 }
#define my_max(x, y)
Definition: sysdefs.h:186

References allocator, lzma_index_coder::count, lzma_index_coder::crc32, i, lzma_index_coder::index, lzma_index_coder::index_ptr, LZMA_MEM_ERROR, LZMA_OK, memlimit, lzma_index_coder::memlimit, my_max, NULL, lzma_index_coder::pos, and lzma_index_coder::sequence.

Referenced by index_decoder_init().

◆ LZMA_API()

LZMA_API ( lzma_ret  )

Definition at line 296 of file index_decoder.c.

298 {
300 
303 
304  return LZMA_OK;
305 }
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_FINISH
Finish the coding operation.
Definition: base.h:328
@ LZMA_RUN
Continue coding.
Definition: base.h:251

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