Rizin
unix-like reverse engineering framework and cli tools
filter_buffer_encoder.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 "filter_encoder.h"
14 
15 
17 lzma_raw_buffer_encode(
19  const uint8_t *in, size_t in_size,
20  uint8_t *out, size_t *out_pos, size_t out_size)
21 {
22  // Validate what isn't validated later in filter_common.c.
23  if ((in == NULL && in_size != 0) || out == NULL
24  || out_pos == NULL || *out_pos > out_size)
25  return LZMA_PROG_ERROR;
26 
27  // Initialize the encoder
30 
31  // Store the output position so that we can restore it if
32  // something goes wrong.
33  const size_t out_start = *out_pos;
34 
35  // Do the actual encoding and free coder's memory.
36  size_t in_pos = 0;
37  lzma_ret ret = next.code(next.coder, allocator, in, &in_pos, in_size,
38  out, out_pos, out_size, LZMA_FINISH);
39  lzma_next_end(&next, allocator);
40 
41  if (ret == LZMA_STREAM_END) {
42  ret = LZMA_OK;
43  } else {
44  if (ret == LZMA_OK) {
45  // Output buffer was too small.
46  assert(*out_pos == out_size);
47  ret = LZMA_BUF_ERROR;
48  }
49 
50  // Restore the output position.
51  *out_pos = out_start;
52  }
53 
54  return ret;
55 }
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_API(lzma_ret)
lzma_ret lzma_raw_encoder_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
Custom functions for memory handling.
Definition: base.h:372
Filter options.
Definition: filter.h:43
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_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