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

Definitions common to the whole liblzma library. More...

#include "sysdefs.h"
#include "mythread.h"
#include "tuklib_integer.h"
#include "lzma.h"

Go to the source code of this file.

Classes

struct  lzma_filter_info_s
 
struct  lzma_next_coder_s
 Hold data and function pointers of the next filter in the chain. More...
 
struct  lzma_internal_s
 

Macros

#define LZMA_API_EXPORT
 
#define LZMA_API(type)   LZMA_API_EXPORT type LZMA_API_CALL
 
#define likely(expr)   (expr)
 
#define unlikely(expr)   (expr)
 
#define LZMA_BUFFER_SIZE   4096
 Size of temporary buffers needed in some filters. More...
 
#define LZMA_THREADS_MAX   16384
 
#define LZMA_MEMUSAGE_BASE   (UINT64_C(1) << 15)
 
#define LZMA_FILTER_RESERVED_START   (LZMA_VLI_C(1) << 62)
 
#define LZMA_SUPPORTED_FLAGS
 
#define LZMA_ACTION_MAX   ((unsigned int)(LZMA_FULL_BARRIER))
 Largest valid lzma_action value as unsigned integer. More...
 
#define LZMA_TIMED_OUT   32
 
#define LZMA_NEXT_CODER_INIT
 Macro to initialize lzma_next_coder structure. More...
 
#define return_if_error(expr)
 Return if expression doesn't evaluate to LZMA_OK. More...
 
#define lzma_next_coder_init(func, next, allocator)
 
#define lzma_next_strm_init(func, strm, ...)
 

Typedefs

typedef struct lzma_next_coder_s lzma_next_coder
 
typedef struct lzma_filter_info_s lzma_filter_info
 
typedef lzma_ret(* lzma_init_function) (lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
 Type of a function used to initialize a filter encoder or decoder. More...
 
typedef lzma_ret(* lzma_code_function) (void *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)
 
typedef void(* lzma_end_function) (void *coder, const lzma_allocator *allocator)
 Type of a function to free the memory allocated for the coder. More...
 

Functions

void * lzma_alloc (size_t size, const lzma_allocator *allocator) lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
 Allocates memory. More...
 
void * lzma_attribute ((__malloc__)) lzma_attr_alloc_size(1) lzma_alloc_zero(size_t size
 
void lzma_free (void *ptr, const lzma_allocator *allocator)
 Frees memory. More...
 
lzma_ret lzma_strm_init (lzma_stream *strm)
 
lzma_ret lzma_next_filter_init (lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
 
lzma_ret lzma_next_filter_update (lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter *reversed_filters)
 
void lzma_next_end (lzma_next_coder *next, const lzma_allocator *allocator)
 
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)
 

Variables

void const lzma_allocatorallocator
 

Detailed Description

Definitions common to the whole liblzma library.

Definition in file common.h.

Macro Definition Documentation

◆ likely

#define likely (   expr)    (expr)

Definition at line 43 of file common.h.

◆ LZMA_ACTION_MAX

#define LZMA_ACTION_MAX   ((unsigned int)(LZMA_FULL_BARRIER))

Largest valid lzma_action value as unsigned integer.

Definition at line 81 of file common.h.

◆ LZMA_API

#define LZMA_API (   type)    LZMA_API_EXPORT type LZMA_API_CALL

Definition at line 33 of file common.h.

◆ LZMA_API_EXPORT

#define LZMA_API_EXPORT

Definition at line 30 of file common.h.

◆ LZMA_BUFFER_SIZE

#define LZMA_BUFFER_SIZE   4096

Size of temporary buffers needed in some filters.

Definition at line 49 of file common.h.

◆ LZMA_FILTER_RESERVED_START

#define LZMA_FILTER_RESERVED_START   (LZMA_VLI_C(1) << 62)

Start of internal Filter ID space. These IDs must never be used in Streams.

Definition at line 67 of file common.h.

◆ LZMA_MEMUSAGE_BASE

#define LZMA_MEMUSAGE_BASE   (UINT64_C(1) << 15)

Starting value for memory usage estimates. Instead of calculating size of every structure and taking into account malloc() overhead etc., we add a base size to all memory usage estimates. It's not very accurate but should be easily good enough.

Definition at line 63 of file common.h.

◆ LZMA_NEXT_CODER_INIT

#define LZMA_NEXT_CODER_INIT
Value:
.coder = NULL, \
.init = (uintptr_t)(NULL), \
.code = NULL, \
.end = NULL, \
.get_progress = NULL, \
.get_check = NULL, \
.memconfig = NULL, \
.update = NULL, \
}
#define NULL
Definition: cris-opc.c:27
_W64 unsigned int uintptr_t
struct lzma_next_coder_s lzma_next_coder
Definition: common.h:91
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39

Macro to initialize lzma_next_coder structure.

Definition at line 180 of file common.h.

◆ lzma_next_coder_init

#define lzma_next_coder_init (   func,
  next,
  allocator 
)
Value:
do { \
if ((uintptr_t)(func) != (next)->init) \
lzma_next_end(next, allocator); \
(next)->init = (uintptr_t)(func); \
} while (0)
bool init
Definition: core.c:77
void const lzma_allocator * allocator
Definition: common.h:236

If next isn't already initialized, free the previous coder. Then mark that next is possibly initialized for the coder using this macro. "Possibly" means that if e.g. allocation of next->coder fails, the structure isn't actually initialized for this coder, but leaving next->init to func is still OK.

Definition at line 291 of file common.h.

◆ lzma_next_strm_init

#define lzma_next_strm_init (   func,
  strm,
  ... 
)
Value:
do { \
return_if_error(lzma_strm_init(strm)); \
const lzma_ret ret_ = func(&(strm)->internal->next, \
(strm)->allocator, __VA_ARGS__); \
if (ret_ != LZMA_OK) { \
lzma_end(strm); \
return ret_; \
} \
} while (0)
static lzma_stream strm
Definition: full_flush.c:20
lzma_ret lzma_strm_init(lzma_stream *strm)
Definition: common.c:170
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58

Initializes lzma_strm and calls func() to initialize strm->internal->next. (The function being called will use lzma_next_coder_init()). If initialization fails, memory that wasn't freed by func() is freed along strm->internal.

Definition at line 303 of file common.h.

◆ LZMA_SUPPORTED_FLAGS

#define LZMA_SUPPORTED_FLAGS
Value:
#define LZMA_TELL_ANY_CHECK
Definition: container.h:474
#define LZMA_IGNORE_CHECK
Definition: container.h:498
#define LZMA_CONCATENATED
Definition: container.h:515
#define LZMA_TELL_NO_CHECK
Definition: container.h:457
#define LZMA_TELL_UNSUPPORTED_CHECK
Definition: container.h:466

Supported flags that can be passed to lzma_stream_decoder() or lzma_auto_decoder().

Definition at line 72 of file common.h.

◆ LZMA_THREADS_MAX

#define LZMA_THREADS_MAX   16384

Maximum number of worker threads within one multithreaded component. The limit exists solely to make it simpler to prevent integer overflows when allocating structures etc. This should be big enough for now... the code won't scale anywhere close to this number anyway.

Definition at line 56 of file common.h.

◆ LZMA_TIMED_OUT

#define LZMA_TIMED_OUT   32

Special return value (lzma_ret) to indicate that a timeout was reached and lzma_code() must not return LZMA_BUF_ERROR. This is converted to LZMA_OK in lzma_code(). This is not in the lzma_ret enumeration because there's no need to have it in the public API.

Definition at line 88 of file common.h.

◆ return_if_error

#define return_if_error (   expr)
Value:
do { \
const lzma_ret ret_ = (expr); \
if (ret_ != LZMA_OK) \
return ret_; \
} while (0)
static RzNumCalcValue expr(RzNum *, RzNumCalc *, int)
Definition: calc.c:167

Return if expression doesn't evaluate to LZMA_OK.

There are several situations where we want to return immediately with the value of expr if it isn't LZMA_OK. This macro shortens the code a little.

Definition at line 278 of file common.h.

◆ unlikely

#define unlikely (   expr)    (expr)

Definition at line 44 of file common.h.

Typedef Documentation

◆ lzma_code_function

Type of a function to do some kind of coding work (filters, Stream, Block encoders/decoders etc.). Some special coders use don't use both input and output buffers, but for simplicity they still use this same function prototype.

Definition at line 105 of file common.h.

◆ lzma_end_function

typedef void(* lzma_end_function) (void *coder, const lzma_allocator *allocator)

Type of a function to free the memory allocated for the coder.

Definition at line 113 of file common.h.

◆ lzma_filter_info

Definition at line 1 of file common.h.

◆ lzma_init_function

Type of a function used to initialize a filter encoder or decoder.

Definition at line 97 of file common.h.

◆ lzma_next_coder

Definition at line 1 of file common.h.

Function Documentation

◆ lzma_alloc()

◆ lzma_attribute()

void* lzma_attribute ( (__malloc__)  )

Allocates memory and zeroes it (like calloc()). This can be faster than lzma_alloc() + memzero() while being backward compatible with custom allocators.

◆ lzma_bufcpy()

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 
)

Copy as much data as possible from in[] to out[] and update *in_pos and *out_pos accordingly. Returns the number of bytes copied.

Definition at line 94 of file common.c.

97 {
98  const size_t in_avail = in_size - *in_pos;
99  const size_t out_avail = out_size - *out_pos;
100  const size_t copy_size = my_min(in_avail, out_avail);
101 
102  // Call memcpy() only if there is something to copy. If there is
103  // nothing to copy, in or out might be NULL and then the memcpy()
104  // call would trigger undefined behavior.
105  if (copy_size > 0)
106  memcpy(out + *out_pos, in + *in_pos, copy_size);
107 
108  *in_pos += copy_size;
109  *out_pos += copy_size;
110 
111  return copy_size;
112 }
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 const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define my_min(x, y)
Definition: sysdefs.h:185

References in, in_pos, in_size, memcpy(), my_min, out, and out_pos.

Referenced by alone_encode(), block_decode(), block_encode(), copy_or_code(), dict_write(), fill_window(), lzma2_encode(), lzma_outq_read(), simple_code(), stream_decode(), stream_encode(), stream_encode_in(), and stream_encode_mt().

◆ lzma_free()

◆ lzma_next_end()

void lzma_next_end ( lzma_next_coder next,
const lzma_allocator allocator 
)

Frees the memory allocated for next->coder either using next->end or, if next->end is NULL, using lzma_free.

Definition at line 145 of file common.c.

146 {
147  if (next->init != (uintptr_t)(NULL)) {
148  // To avoid tiny end functions that simply call
149  // lzma_free(coder, allocator), we allow leaving next->end
150  // NULL and call lzma_free() here.
151  if (next->end != NULL)
152  next->end(next->coder, allocator);
153  else
154  lzma_free(next->coder, allocator);
155 
156  // Reset the variables so the we don't accidentally think
157  // that it is an already initialized coder.
158  *next = LZMA_NEXT_CODER_INIT;
159  }
160 
161  return;
162 }
void * coder
Pointer to coder-specific data.
Definition: common.h:137
uintptr_t init
Definition: common.h:147
lzma_end_function end
Definition: common.h:155
#define LZMA_NEXT_CODER_INIT
Macro to initialize lzma_next_coder structure.
Definition: common.h:180
void lzma_free(void *ptr, const lzma_allocator *allocator)
Frees memory.
Definition: common.c:78

References allocator, lzma_next_coder_s::coder, lzma_next_coder_s::end, lzma_next_coder_s::init, lzma_free(), LZMA_NEXT_CODER_INIT, and NULL.

Referenced by alone_decoder_end(), alone_encoder_end(), auto_decoder_end(), block_decoder_end(), block_encode_normal(), block_encoder_end(), delta_coder_end(), lz_decoder_end(), lz_encoder_end(), LZMA_API(), lzma_raw_coder_init(), simple_coder_end(), stream_decoder_end(), stream_encoder_end(), stream_encoder_mt_end(), and worker_start().

◆ lzma_next_filter_init()

lzma_ret lzma_next_filter_init ( lzma_next_coder next,
const lzma_allocator allocator,
const lzma_filter_info filters 
)

Initializes the next filter in the chain, if any. This takes care of freeing the memory of previously initialized filter if it is different than the filter being initialized now. This way the actual filter initialization functions don't need to use lzma_next_coder_init macro.

Definition at line 116 of file common.c.

118 {
120  next->id = filters[0].id;
121  return filters[0].init == NULL
122  ? LZMA_OK : filters[0].init(next, allocator, filters);
123 }
const lzma_filter * filters
Definition: container.h:315
lzma_vli id
Filter ID.
Definition: filter.h:54
lzma_vli id
Definition: common.h:141
#define lzma_next_coder_init(func, next, allocator)
Definition: common.h:291

References allocator, filters, lzma_filter::id, lzma_next_coder_s::id, init, lzma_next_coder_init, LZMA_OK, and NULL.

Referenced by alone_decode(), alone_encoder_init(), lzma_delta_coder_init(), lzma_lz_decoder_init(), lzma_lz_encoder_init(), lzma_raw_coder_init(), and lzma_simple_coder_init().

◆ lzma_next_filter_update()

lzma_ret lzma_next_filter_update ( lzma_next_coder next,
const lzma_allocator allocator,
const lzma_filter reversed_filters 
)

Update the next filter in the chain, if any. This checks that the application is not trying to change the Filter IDs.

Definition at line 127 of file common.c.

129 {
130  // Check that the application isn't trying to change the Filter ID.
131  // End of filters is indicated with LZMA_VLI_UNKNOWN in both
132  // reversed_filters[0].id and next->id.
133  if (reversed_filters[0].id != next->id)
134  return LZMA_PROG_ERROR;
135 
136  if (reversed_filters[0].id == LZMA_VLI_UNKNOWN)
137  return LZMA_OK;
138 
139  assert(next->update != NULL);
140  return next->update(next->coder, allocator, NULL, reversed_filters);
141 }
assert(limit<=UINT32_MAX/2)
lzma_ret(* update)(void *coder, const lzma_allocator *allocator, const lzma_filter *filters, const lzma_filter *reversed_filters)
Definition: common.h:173
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218

References allocator, assert(), lzma_next_coder_s::coder, lzma_next_coder_s::id, LZMA_OK, LZMA_PROG_ERROR, LZMA_VLI_UNKNOWN, NULL, and lzma_next_coder_s::update.

Referenced by block_encoder_update(), delta_encoder_update(), lz_encoder_update(), and simple_coder_update().

◆ lzma_strm_init()

lzma_ret lzma_strm_init ( lzma_stream strm)

Allocates strm->internal if it is NULL, and initializes *strm and strm->internal. This function is only called via lzma_next_strm_init macro.

Definition at line 170 of file common.c.

171 {
172  if (strm == NULL)
173  return LZMA_PROG_ERROR;
174 
175  if (strm->internal == NULL) {
177  strm->allocator);
178  if (strm->internal == NULL)
179  return LZMA_MEM_ERROR;
180 
182  }
183 
185  sizeof(strm->internal->supported_actions));
186  strm->internal->sequence = ISEQ_RUN;
187  strm->internal->allow_buf_error = false;
188 
189  strm->total_in = 0;
190  strm->total_out = 0;
191 
192  return LZMA_OK;
193 }
lzma_next_coder next
The actual coder that should do something useful.
Definition: common.h:198
bool supported_actions[LZMA_ACTION_MAX+1]
Indicates which lzma_action values are allowed by next.code.
Definition: common.h:220
bool allow_buf_error
Definition: common.h:224
enum lzma_internal_s::@645 sequence
uint64_t total_in
Definition: base.h:488
lzma_internal * internal
Definition: base.h:505
const lzma_allocator * allocator
Custom memory allocation functions.
Definition: base.h:502
uint64_t total_out
Definition: base.h:492
void * lzma_alloc(size_t size, const lzma_allocator *allocator) lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
Allocates memory.
#define memzero(s, n)
Definition: sysdefs.h:180
@ LZMA_MEM_ERROR
Cannot allocate memory.
Definition: base.h:128

References lzma_stream::allocator, lzma_internal_s::allow_buf_error, lzma_stream::internal, lzma_alloc(), LZMA_MEM_ERROR, LZMA_NEXT_CODER_INIT, LZMA_OK, LZMA_PROG_ERROR, memzero, lzma_internal_s::next, NULL, lzma_internal_s::sequence, strm, lzma_internal_s::supported_actions, lzma_stream::total_in, and lzma_stream::total_out.

Variable Documentation

◆ allocator

void const lzma_allocator* allocator

Definition at line 236 of file common.h.