Rizin
unix-like reverse engineering framework and cli tools
block_buffer_decoder.c
Go to the documentation of this file.
1 //
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
10 //
12 
13 #include "block_decoder.h"
14 
15 
17 lzma_block_buffer_decode(lzma_block *block, const lzma_allocator *allocator,
18  const uint8_t *in, size_t *in_pos, size_t in_size,
19  uint8_t *out, size_t *out_pos, size_t out_size)
20 {
21  if (in_pos == NULL || (in == NULL && *in_pos != in_size)
22  || *in_pos > in_size || out_pos == NULL
23  || (out == NULL && *out_pos != out_size)
24  || *out_pos > out_size)
25  return LZMA_PROG_ERROR;
26 
27  // Initialize the Block decoder.
28  lzma_next_coder block_decoder = LZMA_NEXT_CODER_INIT;
30  &block_decoder, allocator, block);
31 
32  if (ret == LZMA_OK) {
33  // Save the positions so that we can restore them in case
34  // an error occurs.
35  const size_t in_start = *in_pos;
36  const size_t out_start = *out_pos;
37 
38  // Do the actual decoding.
39  ret = block_decoder.code(block_decoder.coder, allocator,
40  in, in_pos, in_size, out, out_pos, out_size,
41  LZMA_FINISH);
42 
43  if (ret == LZMA_STREAM_END) {
44  ret = LZMA_OK;
45  } else {
46  if (ret == LZMA_OK) {
47  // Either the input was truncated or the
48  // output buffer was too small.
50  || *out_pos == out_size);
51 
52  // If all the input was consumed, then the
53  // input is truncated, even if the output
54  // buffer is also full. This is because
55  // processing the last byte of the Block
56  // never produces output.
57  //
58  // NOTE: This assumption may break when new
59  // filters are added, if the end marker of
60  // the filter doesn't consume at least one
61  // complete byte.
62  if (*in_pos == in_size)
63  ret = LZMA_DATA_ERROR;
64  else
65  ret = LZMA_BUF_ERROR;
66  }
67 
68  // Restore the positions.
69  *in_pos = in_start;
70  *out_pos = out_start;
71  }
72  }
73 
74  // Free the decoder memory. This needs to be done even if
75  // initialization fails, because the internal API doesn't
76  // require the initialization function to free its memory on error.
77  lzma_next_end(&block_decoder, allocator);
78 
79  return ret;
80 }
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
LZMA_API(lzma_ret)
lzma_ret lzma_block_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, lzma_block *block)
Decodes .xz Blocks.
#define NULL
Definition: cris-opc.c:27
assert(limit<=UINT32_MAX/2)
unsigned char uint8_t
Definition: sftypes.h:31
Custom functions for memory handling.
Definition: base.h:372
Options for the Block and Block Header encoders and decoders.
Definition: block.h:30
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
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