XZ Utils  5.1.0alpha
Data Structures | Macros | Typedefs | Functions
common.h File Reference

Common functions needed in many places in liblzma. More...

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

Data Structures

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_MEMUSAGE_BASE   (UINT64_C(1) << 15)
 
#define LZMA_FILTER_RESERVED_START   (LZMA_VLI_C(1) << 62)
 
#define LZMA_SUPPORTED_FLAGS
 
#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_coder_s lzma_coder
 
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, 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) (lzma_coder *coder, 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) (lzma_coder *coder, lzma_allocator *allocator)
 Type of a function to free the memory allocated for the coder. More...
 

Functions

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

Detailed Description

Common functions needed in many places in liblzma.

Definitions common to the whole liblzma library.

Macro Definition Documentation

#define LZMA_BUFFER_SIZE   4096

Size of temporary buffers needed in some filters.

#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.

Referenced by lzma_memlimit_set().

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

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

Referenced by lzma_filter_flags_decode(), lzma_filter_flags_encode(), and lzma_filter_flags_size().

#define LZMA_SUPPORTED_FLAGS
Value:
#define LZMA_CONCATENATED
Definition: container.h:321
#define LZMA_TELL_ANY_CHECK
Definition: container.h:304
#define LZMA_TELL_UNSUPPORTED_CHECK
Definition: container.h:296
#define LZMA_TELL_NO_CHECK
Definition: container.h:287

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

#define LZMA_NEXT_CODER_INIT
Value:
.coder = NULL, \
.init = (uintptr_t)(NULL), \
.code = NULL, \
.end = NULL, \
.get_check = NULL, \
.memconfig = NULL, \
.update = NULL, \
}
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39
Hold data and function pointers of the next filter in the chain.
Definition: common.h:120

Macro to initialize lzma_next_coder structure.

Referenced by lzma_block_buffer_decode(), lzma_next_end(), lzma_raw_buffer_decode(), lzma_raw_buffer_encode(), lzma_stream_buffer_decode(), and lzma_strm_init().

#define return_if_error (   expr)
Value:
do { \
const lzma_ret ret_ = (expr); \
if (ret_ != LZMA_OK) \
return ret_; \
} while (0)
Operation completed successfully.
Definition: base.h:58
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57

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.

Referenced by lzma_block_buffer_encode(), lzma_block_header_decode(), lzma_block_header_encode(), lzma_block_header_size(), lzma_decoder_init(), lzma_filter_flags_decode(), lzma_filter_flags_encode(), lzma_filter_flags_size(), lzma_index_buffer_decode(), lzma_index_hash_append(), lzma_index_hash_decode(), lzma_index_stream_flags(), lzma_raw_buffer_decode(), lzma_raw_buffer_encode(), and lzma_stream_buffer_encode().

#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)
void lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator)
Definition: common.c:122

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.

Referenced by lzma_next_filter_init().

#define lzma_next_strm_init (   func,
  strm,
  ... 
)
Value:
do { \
const lzma_ret ret_ = func(&(strm)->internal->next, \
(strm)->allocator, __VA_ARGS__); \
if (ret_ != LZMA_OK) { \
return ret_; \
} \
} while (0)
static lzma_stream strm
Stream used to communicate with liblzma.
Definition: coder.c:32
#define return_if_error(expr)
Return if expression doesn't evaluate to LZMA_OK.
Definition: common.h:254
void lzma_end(lzma_stream *strm) lzma_nothrow
Free memory allocated for the coder data structures.
Definition: common.c:378
Operation completed successfully.
Definition: base.h:58
lzma_ret lzma_strm_init(lzma_stream *strm)
Definition: common.c:188
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57

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.

Referenced by lzma_alone_decoder(), lzma_alone_encoder(), lzma_auto_decoder(), lzma_block_decoder(), lzma_block_encoder(), lzma_index_decoder(), lzma_index_encoder(), lzma_raw_decoder(), lzma_raw_encoder(), lzma_stream_decoder(), and lzma_stream_encoder().

Typedef Documentation

typedef struct lzma_coder_s lzma_coder

Type of encoder/decoder specific data; the actual structure is defined differently in different coders.

typedef lzma_ret(* lzma_init_function) (lzma_next_coder *next, lzma_allocator *allocator, const lzma_filter_info *filters)

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

typedef lzma_ret(* lzma_code_function) (lzma_coder *coder, 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)

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.

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

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

Function Documentation

void* lzma_alloc ( size_t  size,
lzma_allocator allocator 
)
void lzma_free ( void *  ptr,
lzma_allocator allocator 
)
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.

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, lzma_internal_s::next, lzma_internal_s::sequence, lzma_internal_s::supported_actions, lzma_stream::total_in, and lzma_stream::total_out.

lzma_ret lzma_next_filter_init ( lzma_next_coder next,
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.

References lzma_filter_info_s::id, lzma_next_coder_s::id, lzma_filter_info_s::init, lzma_next_coder_init, and LZMA_OK.

lzma_ret lzma_next_filter_update ( lzma_next_coder next,
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.

References lzma_next_coder_s::coder, lzma_next_coder_s::id, LZMA_OK, LZMA_PROG_ERROR, LZMA_VLI_UNKNOWN, and lzma_next_coder_s::update.

void lzma_next_end ( lzma_next_coder next,
lzma_allocator allocator 
)

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

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

Referenced by lzma_block_buffer_decode(), lzma_end(), lzma_raw_buffer_decode(), lzma_raw_buffer_encode(), and lzma_stream_buffer_decode().

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.

Referenced by copy_or_code(), dict_write(), and fill_window().