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

Wrapper for simple filters. More...

#include "simple_private.h"

Go to the source code of this file.

Functions

static lzma_ret copy_or_code (lzma_simple_coder *coder, 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)
 Copied or encodes/decodes more data to out[]. More...
 
static size_t call_filter (lzma_simple_coder *coder, uint8_t *buffer, size_t size)
 
static lzma_ret simple_code (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 simple_coder_end (void *coder_ptr, const lzma_allocator *allocator)
 
static lzma_ret simple_coder_update (void *coder_ptr, const lzma_allocator *allocator, const lzma_filter *filters_null lzma_attribute((__unused__)), const lzma_filter *reversed_filters)
 
lzma_ret lzma_simple_coder_init (lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters, size_t(*filter)(void *simple, uint32_t now_pos, bool is_encoder, uint8_t *buffer, size_t size), size_t simple_size, size_t unfiltered_max, uint32_t alignment, bool is_encoder)
 

Detailed Description

Wrapper for simple filters.

Simple filters don't change the size of the data i.e. number of bytes in equals the number of bytes out.

Definition in file simple_coder.c.

Function Documentation

◆ call_filter()

static size_t call_filter ( lzma_simple_coder coder,
uint8_t buffer,
size_t  size 
)
static

Definition at line 58 of file simple_coder.c.

59 {
60  const size_t filtered = coder->filter(coder->simple,
61  coder->now_pos, coder->is_encoder,
62  buffer, size);
63  coder->now_pos += filtered;
64  return filtered;
65 }
voidpf void uLong size
Definition: ioapi.h:138
Definition: buffer.h:15
size_t(* filter)(void *simple, uint32_t now_pos, bool is_encoder, uint8_t *buffer, size_t size)

References lzma_simple_coder::filter, lzma_simple_coder::is_encoder, lzma_simple_coder::now_pos, and lzma_simple_coder::simple.

Referenced by simple_code().

◆ copy_or_code()

static lzma_ret copy_or_code ( lzma_simple_coder coder,
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

Copied or encodes/decodes more data to out[].

Definition at line 21 of file simple_coder.c.

25 {
26  assert(!coder->end_was_reached);
27 
28  if (coder->next.code == NULL) {
29  lzma_bufcpy(in, in_pos, in_size, out, out_pos, out_size);
30 
31  // Check if end of stream was reached.
32  if (coder->is_encoder && action == LZMA_FINISH
33  && *in_pos == in_size)
34  coder->end_was_reached = true;
35 
36  } else {
37  // Call the next coder in the chain to provide us some data.
38  const lzma_ret ret = coder->next.code(
39  coder->next.coder, allocator,
40  in, in_pos, in_size,
41  out, out_pos, out_size, action);
42 
43  if (ret == LZMA_STREAM_END) {
44  assert(!coder->is_encoder
45  || action == LZMA_FINISH);
46  coder->end_was_reached = true;
47 
48  } else if (ret != LZMA_OK) {
49  return ret;
50  }
51  }
52 
53  return LZMA_OK;
54 }
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 NULL
Definition: cris-opc.c:27
assert(limit<=UINT32_MAX/2)
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_next_coder next
Next filter in the chain.
bool end_was_reached
True if the next coder in the chain has returned LZMA_STREAM_END.
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
@ LZMA_FINISH
Finish the coding operation.
Definition: base.h:328
size_t lzma_bufcpy(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)
Definition: common.c:94

References test-lz4-speed::action, allocator, assert(), lzma_next_coder_s::code, lzma_next_coder_s::coder, lzma_simple_coder::end_was_reached, in, in_pos, in_size, lzma_simple_coder::is_encoder, lzma_bufcpy(), LZMA_FINISH, LZMA_OK, LZMA_STREAM_END, lzma_simple_coder::next, NULL, out, and out_pos.

Referenced by simple_code().

◆ lzma_simple_coder_init()

lzma_ret lzma_simple_coder_init ( lzma_next_coder next,
const lzma_allocator allocator,
const lzma_filter_info filters,
size_t(*)(void *simple, uint32_t now_pos, bool is_encoder, uint8_t *buffer, size_t size filter,
size_t  simple_size,
size_t  unfiltered_max,
uint32_t  alignment,
bool  is_encoder 
)

Definition at line 235 of file simple_coder.c.

241 {
242  // Allocate memory for the lzma_simple_coder structure if needed.
243  lzma_simple_coder *coder = next->coder;
244  if (coder == NULL) {
245  // Here we allocate space also for the temporary buffer. We
246  // need twice the size of unfiltered_max, because then it
247  // is always possible to filter at least unfiltered_max bytes
248  // more data in coder->buffer[] if it can be filled completely.
249  coder = lzma_alloc(sizeof(lzma_simple_coder)
250  + 2 * unfiltered_max, allocator);
251  if (coder == NULL)
252  return LZMA_MEM_ERROR;
253 
254  next->coder = coder;
255  next->code = &simple_code;
256  next->end = &simple_coder_end;
257  next->update = &simple_coder_update;
258 
259  coder->next = LZMA_NEXT_CODER_INIT;
260  coder->filter = filter;
261  coder->allocated = 2 * unfiltered_max;
262 
263  // Allocate memory for filter-specific data structure.
264  if (simple_size > 0) {
265  coder->simple = lzma_alloc(simple_size, allocator);
266  if (coder->simple == NULL)
267  return LZMA_MEM_ERROR;
268  } else {
269  coder->simple = NULL;
270  }
271  }
272 
273  if (filters[0].options != NULL) {
274  const lzma_options_bcj *simple = filters[0].options;
275  coder->now_pos = simple->start_offset;
276  if (coder->now_pos & (alignment - 1))
277  return LZMA_OPTIONS_ERROR;
278  } else {
279  coder->now_pos = 0;
280  }
281 
282  // Reset variables.
283  coder->is_encoder = is_encoder;
284  coder->end_was_reached = false;
285  coder->pos = 0;
286  coder->filtered = 0;
287  coder->size = 0;
288 
289  return lzma_next_filter_init(&coder->next, allocator, filters + 1);
290 }
const lzma_filter * filters
Definition: container.h:315
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
static bool filter(RzParse *p, ut64 addr, RzFlag *f, RzAnalysisHint *hint, char *data, char *str, int len, bool big_endian)
Definition: filter.c:185
static lzma_ret simple_coder_update(void *coder_ptr, const lzma_allocator *allocator, const lzma_filter *filters_null lzma_attribute((__unused__)), const lzma_filter *reversed_filters)
Definition: simple_coder.c:222
static lzma_ret simple_code(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: simple_coder.c:69
static void simple_coder_end(void *coder_ptr, const lzma_allocator *allocator)
Definition: simple_coder.c:211
void * options
Pointer to filter-specific options structure.
Definition: filter.h:63
lzma_end_function end
Definition: common.h:155
lzma_ret(* update)(void *coder, const lzma_allocator *allocator, const lzma_filter *filters, const lzma_filter *reversed_filters)
Definition: common.h:173
Options for BCJ filters.
Definition: bcj.h:73
uint32_t start_offset
Start offset for conversions.
Definition: bcj.h:88
size_t allocated
Size of the memory allocated for the buffer.
#define LZMA_NEXT_CODER_INIT
Macro to initialize lzma_next_coder structure.
Definition: common.h:180
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
@ LZMA_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160
lzma_ret lzma_next_filter_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
Definition: common.c:116

References lzma_simple_coder::allocated, allocator, lzma_next_coder_s::code, lzma_next_coder_s::coder, lzma_next_coder_s::end, lzma_simple_coder::end_was_reached, filter(), lzma_simple_coder::filter, lzma_simple_coder::filtered, filters, lzma_simple_coder::is_encoder, lzma_alloc(), LZMA_MEM_ERROR, LZMA_NEXT_CODER_INIT, lzma_next_filter_init(), LZMA_OPTIONS_ERROR, lzma_simple_coder::next, lzma_simple_coder::now_pos, NULL, options, lzma_filter::options, lzma_simple_coder::pos, lzma_simple_coder::simple, simple_code(), simple_coder_end(), simple_coder_update(), lzma_simple_coder::size, lzma_options_bcj::start_offset, and lzma_next_coder_s::update.

Referenced by arm_coder_init(), armthumb_coder_init(), ia64_coder_init(), powerpc_coder_init(), sparc_coder_init(), and x86_coder_init().

◆ simple_code()

static lzma_ret simple_code ( 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 69 of file simple_coder.c.

73 {
74  lzma_simple_coder *coder = coder_ptr;
75 
76  // TODO: Add partial support for LZMA_SYNC_FLUSH. We can support it
77  // in cases when the filter is able to filter everything. With most
78  // simple filters it can be done at offset that is a multiple of 2,
79  // 4, or 16. With x86 filter, it needs good luck, and thus cannot
80  // be made to work predictably.
81  if (action == LZMA_SYNC_FLUSH)
82  return LZMA_OPTIONS_ERROR;
83 
84  // Flush already filtered data from coder->buffer[] to out[].
85  if (coder->pos < coder->filtered) {
86  lzma_bufcpy(coder->buffer, &coder->pos, coder->filtered,
87  out, out_pos, out_size);
88 
89  // If we couldn't flush all the filtered data, return to
90  // application immediately.
91  if (coder->pos < coder->filtered)
92  return LZMA_OK;
93 
94  if (coder->end_was_reached) {
95  assert(coder->filtered == coder->size);
96  return LZMA_STREAM_END;
97  }
98  }
99 
100  // If we get here, there is no filtered data left in the buffer.
101  coder->filtered = 0;
102 
103  assert(!coder->end_was_reached);
104 
105  // If there is more output space left than there is unfiltered data
106  // in coder->buffer[], flush coder->buffer[] to out[], and copy/code
107  // more data to out[] hopefully filling it completely. Then filter
108  // the data in out[]. This step is where most of the data gets
109  // filtered if the buffer sizes used by the application are reasonable.
110  const size_t out_avail = out_size - *out_pos;
111  const size_t buf_avail = coder->size - coder->pos;
112  if (out_avail > buf_avail || buf_avail == 0) {
113  // Store the old position so that we know from which byte
114  // to start filtering.
115  const size_t out_start = *out_pos;
116 
117  // Flush data from coder->buffer[] to out[], but don't reset
118  // coder->pos and coder->size yet. This way the coder can be
119  // restarted if the next filter in the chain returns e.g.
120  // LZMA_MEM_ERROR.
121  //
122  // Do the memcpy() conditionally because out can be NULL
123  // (in which case buf_avail is always 0). Calling memcpy()
124  // with a null-pointer is undefined even if the third
125  // argument is 0.
126  if (buf_avail > 0)
127  memcpy(out + *out_pos, coder->buffer + coder->pos,
128  buf_avail);
129 
130  *out_pos += buf_avail;
131 
132  // Copy/Encode/Decode more data to out[].
133  {
134  const lzma_ret ret = copy_or_code(coder, allocator,
135  in, in_pos, in_size,
136  out, out_pos, out_size, action);
137  assert(ret != LZMA_STREAM_END);
138  if (ret != LZMA_OK)
139  return ret;
140  }
141 
142  // Filter out[].
143  const size_t size = *out_pos - out_start;
144  const size_t filtered = call_filter(
145  coder, out + out_start, size);
146 
147  const size_t unfiltered = size - filtered;
148  assert(unfiltered <= coder->allocated / 2);
149 
150  // Now we can update coder->pos and coder->size, because
151  // the next coder in the chain (if any) was successful.
152  coder->pos = 0;
153  coder->size = unfiltered;
154 
155  if (coder->end_was_reached) {
156  // The last byte has been copied to out[] already.
157  // They are left as is.
158  coder->size = 0;
159 
160  } else if (unfiltered > 0) {
161  // There is unfiltered data left in out[]. Copy it to
162  // coder->buffer[] and rewind *out_pos appropriately.
163  *out_pos -= unfiltered;
164  memcpy(coder->buffer, out + *out_pos, unfiltered);
165  }
166  } else if (coder->pos > 0) {
167  memmove(coder->buffer, coder->buffer + coder->pos, buf_avail);
168  coder->size -= coder->pos;
169  coder->pos = 0;
170  }
171 
172  assert(coder->pos == 0);
173 
174  // If coder->buffer[] isn't empty, try to fill it by copying/decoding
175  // more data. Then filter coder->buffer[] and copy the successfully
176  // filtered data to out[]. It is probable, that some filtered and
177  // unfiltered data will be left to coder->buffer[].
178  if (coder->size > 0) {
179  {
180  const lzma_ret ret = copy_or_code(coder, allocator,
181  in, in_pos, in_size,
182  coder->buffer, &coder->size,
183  coder->allocated, action);
184  assert(ret != LZMA_STREAM_END);
185  if (ret != LZMA_OK)
186  return ret;
187  }
188 
189  coder->filtered = call_filter(
190  coder, coder->buffer, coder->size);
191 
192  // Everything is considered to be filtered if coder->buffer[]
193  // contains the last bytes of the data.
194  if (coder->end_was_reached)
195  coder->filtered = coder->size;
196 
197  // Flush as much as possible.
198  lzma_bufcpy(coder->buffer, &coder->pos, coder->filtered,
199  out, out_pos, out_size);
200  }
201 
202  // Check if we got everything done.
203  if (coder->end_was_reached && coder->pos == coder->size)
204  return LZMA_STREAM_END;
205 
206  return LZMA_OK;
207 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static size_t call_filter(lzma_simple_coder *coder, uint8_t *buffer, size_t size)
Definition: simple_coder.c:58
static lzma_ret copy_or_code(lzma_simple_coder *coder, 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)
Copied or encodes/decodes more data to out[].
Definition: simple_coder.c:21
uint8_t buffer[]
Temporary buffer.
@ LZMA_SYNC_FLUSH
Make all the input available at output.
Definition: base.h:265

References test-lz4-speed::action, lzma_simple_coder::allocated, allocator, assert(), lzma_simple_coder::buffer, call_filter(), copy_or_code(), lzma_simple_coder::end_was_reached, lzma_simple_coder::filtered, in, in_pos, in_size, lzma_bufcpy(), LZMA_OK, LZMA_OPTIONS_ERROR, LZMA_STREAM_END, LZMA_SYNC_FLUSH, memcpy(), out, out_pos, lzma_simple_coder::pos, and lzma_simple_coder::size.

Referenced by lzma_simple_coder_init().

◆ simple_coder_end()

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

Definition at line 211 of file simple_coder.c.

212 {
213  lzma_simple_coder *coder = coder_ptr;
214  lzma_next_end(&coder->next, allocator);
215  lzma_free(coder->simple, allocator);
216  lzma_free(coder, allocator);
217  return;
218 }
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(), lzma_simple_coder::next, and lzma_simple_coder::simple.

Referenced by lzma_simple_coder_init().

◆ simple_coder_update()

static lzma_ret simple_coder_update ( void *  coder_ptr,
const lzma_allocator allocator,
const lzma_filter *filters_null   lzma_attribute(__unused__),
const lzma_filter reversed_filters 
)
static

Definition at line 222 of file simple_coder.c.

225 {
226  lzma_simple_coder *coder = coder_ptr;
227 
228  // No update support, just call the next filter in the chain.
230  &coder->next, allocator, reversed_filters + 1);
231 }
lzma_ret lzma_next_filter_update(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter *reversed_filters)
Definition: common.c:127

References allocator, lzma_next_filter_update(), and lzma_simple_coder::next.

Referenced by lzma_simple_coder_init().