Rizin
unix-like reverse engineering framework and cli tools
filter_common.h File Reference

Filter-specific stuff common for both encoder and decoder. More...

#include "common.h"

Go to the source code of this file.

Classes

struct  lzma_filter_coder
 Both lzma_filter_encoder and lzma_filter_decoder begin with these members. More...
 

Typedefs

typedef const lzma_filter_coder *(* lzma_filter_find) (lzma_vli id)
 

Functions

lzma_ret lzma_raw_coder_init (lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter *filters, lzma_filter_find coder_find, bool is_encoder)
 
uint64_t lzma_raw_coder_memusage (lzma_filter_find coder_find, const lzma_filter *filters)
 

Detailed Description

Filter-specific stuff common for both encoder and decoder.

Definition in file filter_common.h.

Typedef Documentation

◆ lzma_filter_find

typedef const lzma_filter_coder*(* lzma_filter_find) (lzma_vli id)

Definition at line 35 of file filter_common.h.

Function Documentation

◆ lzma_raw_coder_init()

lzma_ret lzma_raw_coder_init ( lzma_next_coder next,
const lzma_allocator allocator,
const lzma_filter filters,
lzma_filter_find  coder_find,
bool  is_encoder 
)

Definition at line 242 of file filter_common.c.

245 {
246  // Do some basic validation and get the number of filters.
247  size_t count;
249 
250  // Set the filter functions and copy the options pointer.
252  if (is_encoder) {
253  for (size_t i = 0; i < count; ++i) {
254  // The order of the filters is reversed in the
255  // encoder. It allows more efficient handling
256  // of the uncompressed data.
257  const size_t j = count - i - 1;
258 
259  const lzma_filter_coder *const fc
260  = coder_find(options[i].id);
261  if (fc == NULL || fc->init == NULL)
262  return LZMA_OPTIONS_ERROR;
263 
264  filters[j].id = options[i].id;
265  filters[j].init = fc->init;
266  filters[j].options = options[i].options;
267  }
268  } else {
269  for (size_t i = 0; i < count; ++i) {
270  const lzma_filter_coder *const fc
271  = coder_find(options[i].id);
272  if (fc == NULL || fc->init == NULL)
273  return LZMA_OPTIONS_ERROR;
274 
275  filters[i].id = options[i].id;
276  filters[i].init = fc->init;
277  filters[i].options = options[i].options;
278  }
279  }
280 
281  // Terminate the array.
283  filters[count].init = NULL;
284 
285  // Initialize the filters.
286  const lzma_ret ret = lzma_next_filter_init(next, allocator, filters);
287  if (ret != LZMA_OK)
288  lzma_next_end(next, allocator);
289 
290  return ret;
291 }
lzma_index ** i
Definition: index.h:629
const lzma_allocator * allocator
Definition: block.h:377
const lzma_filter * filters
Definition: container.h:315
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
#define LZMA_FILTERS_MAX
Maximum number of filters in a chain.
Definition: filter.h:26
static lzma_ret validate_chain(const lzma_filter *filters, size_t *count)
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
Both lzma_filter_encoder and lzma_filter_decoder begin with these members.
Definition: filter_common.h:20
lzma_init_function init
Definition: filter_common.h:26
void * options
Pointer to filter-specific options structure.
Definition: filter.h:63
lzma_vli id
Filter ID.
Definition: filter.h:54
#define return_if_error(expr)
Return if expression doesn't evaluate to LZMA_OK.
Definition: common.h:278
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
lzma_ret lzma_next_filter_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
Definition: common.c:116
void lzma_next_end(lzma_next_coder *next, const lzma_allocator *allocator)
Definition: common.c:145

References allocator, count, filters, i, lzma_filter::id, lzma_filter_coder::init, LZMA_FILTERS_MAX, lzma_next_end(), lzma_next_filter_init(), LZMA_OK, LZMA_OPTIONS_ERROR, LZMA_VLI_UNKNOWN, NULL, options, lzma_filter::options, return_if_error, and validate_chain().

Referenced by lzma_raw_decoder_init(), and lzma_raw_encoder_init().

◆ lzma_raw_coder_memusage()

uint64_t lzma_raw_coder_memusage ( lzma_filter_find  coder_find,
const lzma_filter filters 
)

Definition at line 295 of file filter_common.c.

297 {
298  // The chain has to have at least one filter.
299  {
300  size_t tmp;
301  if (validate_chain(filters, &tmp) != LZMA_OK)
302  return UINT64_MAX;
303  }
304 
305  uint64_t total = 0;
306  size_t i = 0;
307 
308  do {
309  const lzma_filter_coder *const fc
310  = coder_find(filters[i].id);
311  if (fc == NULL)
312  return UINT64_MAX; // Unsupported Filter ID
313 
314  if (fc->memusage == NULL) {
315  // This filter doesn't have a function to calculate
316  // the memory usage and validate the options. Such
317  // filters need only little memory, so we use 1 KiB
318  // as a good estimate. They also accept all possible
319  // options, so there's no need to worry about lack
320  // of validation.
321  total += 1024;
322  } else {
323  // Call the filter-specific memory usage calculation
324  // function.
325  const uint64_t usage
326  = fc->memusage(filters[i].options);
327  if (usage == UINT64_MAX)
328  return UINT64_MAX; // Invalid options
329 
330  total += usage;
331  }
332  } while (filters[++i].id != LZMA_VLI_UNKNOWN);
333 
334  // Add some fixed amount of extra. It's to compensate memory usage
335  // of Stream, Block etc. coders, malloc() overhead, stack etc.
336  return total + LZMA_MEMUSAGE_BASE;
337 }
void usage(const char *message)
unsigned long uint64_t
Definition: sftypes.h:28
#define UINT64_MAX
uint64_t(* memusage)(const void *options)
Definition: filter_common.h:30
#define LZMA_MEMUSAGE_BASE
Definition: common.h:63

References filters, i, LZMA_MEMUSAGE_BASE, LZMA_OK, LZMA_VLI_UNKNOWN, lzma_filter_coder::memusage, NULL, options, autogen_x86imm::tmp, UINT64_MAX, usage(), and validate_chain().

Referenced by LZMA_API().