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

Single-call raw decoding. More...

#include "filter_decoder.h"

Go to the source code of this file.

Functions

 LZMA_API (lzma_ret)
 

Detailed Description

Single-call raw decoding.

Definition in file filter_buffer_decoder.c.

Function Documentation

◆ LZMA_API()

LZMA_API ( lzma_ret  )

Definition at line 16 of file filter_buffer_decoder.c.

21 {
22  // Validate what isn't validated later in filter_common.c.
23  if (in == NULL || in_pos == NULL || *in_pos > in_size || out == NULL
24  || out_pos == NULL || *out_pos > out_size)
25  return LZMA_PROG_ERROR;
26 
27  // Initialize the decoer.
30 
31  // Store the positions so that we can restore them if something
32  // goes wrong.
33  const size_t in_start = *in_pos;
34  const size_t out_start = *out_pos;
35 
36  // Do the actual decoding and free decoder's memory.
37  lzma_ret ret = next.code(next.coder, allocator, in, in_pos, in_size,
38  out, out_pos, out_size, LZMA_FINISH);
39 
40  if (ret == LZMA_STREAM_END) {
41  ret = LZMA_OK;
42  } else {
43  if (ret == LZMA_OK) {
44  // Either the input was truncated or the
45  // output buffer was too small.
46  assert(*in_pos == in_size || *out_pos == out_size);
47 
48  if (*in_pos != in_size) {
49  // Since input wasn't consumed completely,
50  // the output buffer became full and is
51  // too small.
52  ret = LZMA_BUF_ERROR;
53 
54  } else if (*out_pos != out_size) {
55  // Since output didn't became full, the input
56  // has to be truncated.
57  ret = LZMA_DATA_ERROR;
58 
59  } else {
60  // All the input was consumed and output
61  // buffer is full. Now we don't immediately
62  // know the reason for the error. Try
63  // decoding one more byte. If it succeeds,
64  // then the output buffer was too small. If
65  // we cannot get a new output byte, the input
66  // is truncated.
67  uint8_t tmp[1];
68  size_t tmp_pos = 0;
69  (void)next.code(next.coder, allocator,
70  in, in_pos, in_size,
71  tmp, &tmp_pos, 1, LZMA_FINISH);
72 
73  if (tmp_pos == 1)
74  ret = LZMA_BUF_ERROR;
75  else
76  ret = LZMA_DATA_ERROR;
77  }
78  }
79 
80  // Restore the positions.
81  *in_pos = in_start;
82  *out_pos = out_start;
83  }
84 
85  lzma_next_end(&next, allocator);
86 
87  return ret;
88 }
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
lzma_ret lzma_raw_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter *options)
assert(limit<=UINT32_MAX/2)
unsigned char uint8_t
Definition: sftypes.h:31
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
#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
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_BUF_ERROR
No progress is possible.
Definition: base.h:191
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
@ LZMA_FINISH
Finish the coding operation.
Definition: base.h:328
void lzma_next_end(lzma_next_coder *next, const lzma_allocator *allocator)
Definition: common.c:145

References allocator, assert(), lzma_next_coder_s::code, lzma_next_coder_s::coder, filters, in, in_pos, in_size, LZMA_BUF_ERROR, LZMA_DATA_ERROR, LZMA_FINISH, LZMA_NEXT_CODER_INIT, lzma_next_end(), LZMA_OK, LZMA_PROG_ERROR, lzma_raw_decoder_init(), LZMA_STREAM_END, NULL, out, out_pos, return_if_error, and autogen_x86imm::tmp.