Path: blob/master/Utilities/cmliblzma/liblzma/common/common.h
3153 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file common.h5/// \brief Definitions common to the whole liblzma library6//7// Author: Lasse Collin8//9///////////////////////////////////////////////////////////////////////////////1011#ifndef LZMA_COMMON_H12#define LZMA_COMMON_H1314#include "sysdefs.h"15#include "mythread.h"16#include "tuklib_integer.h"1718// LZMA_API_EXPORT is used to mark the exported API functions.19// It's used to define the LZMA_API macro.20//21// lzma_attr_visibility_hidden is used for marking *declarations* of extern22// variables that are internal to liblzma (-fvisibility=hidden alone is23// enough to hide the *definitions*). Such markings allow slightly more24// efficient code to accesses those variables in ELF shared libraries.25#if defined(_WIN32) || defined(__CYGWIN__)26# ifdef DLL_EXPORT27# define LZMA_API_EXPORT __declspec(dllexport)28# else29# define LZMA_API_EXPORT30# endif31# define lzma_attr_visibility_hidden32// Don't use ifdef or defined() below.33#elif HAVE_VISIBILITY34# define LZMA_API_EXPORT __attribute__((__visibility__("default")))35# define lzma_attr_visibility_hidden \36__attribute__((__visibility__("hidden")))37#else38# define LZMA_API_EXPORT39# define lzma_attr_visibility_hidden40#endif4142#define LZMA_API(type) LZMA_API_EXPORT type LZMA_API_CALL4344#include "lzma.h"4546// This is for detecting modern GCC and Clang attributes47// like __symver__ in GCC >= 10.48#ifdef __has_attribute49# define lzma_has_attribute(attr) __has_attribute(attr)50#else51# define lzma_has_attribute(attr) 052#endif5354// The extra symbol versioning in the C files may only be used when55// building a shared library. If HAVE_SYMBOL_VERSIONS_LINUX is defined56// to 2 then symbol versioning is done only if also PIC is defined.57// By default Libtool defines PIC when building a shared library and58// doesn't define it when building a static library but it can be59// overridden with --with-pic and --without-pic. configure let's rely60// on PIC if neither --with-pic or --without-pic was used.61#if defined(HAVE_SYMBOL_VERSIONS_LINUX) \62&& (HAVE_SYMBOL_VERSIONS_LINUX == 2 && !defined(PIC))63# undef HAVE_SYMBOL_VERSIONS_LINUX64#endif6566#ifdef HAVE_SYMBOL_VERSIONS_LINUX67// To keep link-time optimization (LTO, -flto) working with GCC,68// the __symver__ attribute must be used instead of __asm__(".symver ...").69// Otherwise the symbol versions may be lost, resulting in broken liblzma70// that has wrong default versions in the exported symbol list!71// The attribute was added in GCC 10; LTO with older GCC is not supported.72//73// To keep -Wmissing-prototypes happy, use LZMA_SYMVER_API only with function74// declarations (including those with __alias__ attribute) and LZMA_API with75// the function definitions. This means a little bit of silly copy-and-paste76// between declarations and definitions though.77//78// As of GCC 12.2, the __symver__ attribute supports only @ and @@ but the79// very convenient @@@ isn't supported (it's supported by GNU assembler80// since 2000). When using @@ instead of @@@, the internal name must not be81// the same as the external name to avoid problems in some situations. This82// is why "#define foo_52 foo" is needed for the default symbol versions.83//84// __has_attribute is supported before GCC 10 and it is supported in Clang 1485// too (which doesn't support __symver__) so use it to detect if __symver__86// is available. This should be far more reliable than looking at compiler87// version macros as nowadays especially __GNUC__ is defined by many compilers.88# if lzma_has_attribute(__symver__)89# define LZMA_SYMVER_API(extnamever, type, intname) \90extern __attribute__((__symver__(extnamever))) \91LZMA_API(type) intname92# else93# define LZMA_SYMVER_API(extnamever, type, intname) \94__asm__(".symver " #intname "," extnamever); \95extern LZMA_API(type) intname96# endif97#endif9899// MSVC has __forceinline which shouldn't be combined with the inline keyword100// (results in a warning).101//102// GCC 3.1 added always_inline attribute so we don't need to check103// for __GNUC__ version. Similarly, all relevant Clang versions104// support it (at least Clang 3.0.0 does already).105// Other compilers might support too which also support __has_attribute106// (Solaris Studio) so do that check too.107#if defined(_MSC_VER)108# define lzma_always_inline __forceinline109#elif defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER) \110|| lzma_has_attribute(__always_inline__)111# define lzma_always_inline inline __attribute__((__always_inline__))112#else113# define lzma_always_inline inline114#endif115116// These allow helping the compiler in some often-executed branches, whose117// result is almost always the same.118#ifdef __GNUC__119# define likely(expr) __builtin_expect(expr, true)120# define unlikely(expr) __builtin_expect(expr, false)121#else122# define likely(expr) (expr)123# define unlikely(expr) (expr)124#endif125126127/// Size of temporary buffers needed in some filters128#define LZMA_BUFFER_SIZE 4096129130131/// Maximum number of worker threads within one multithreaded component.132/// The limit exists solely to make it simpler to prevent integer overflows133/// when allocating structures etc. This should be big enough for now...134/// the code won't scale anywhere close to this number anyway.135#define LZMA_THREADS_MAX 16384136137138/// Starting value for memory usage estimates. Instead of calculating size139/// of _every_ structure and taking into account malloc() overhead etc., we140/// add a base size to all memory usage estimates. It's not very accurate141/// but should be easily good enough.142#define LZMA_MEMUSAGE_BASE (UINT64_C(1) << 15)143144/// Start of internal Filter ID space. These IDs must never be used145/// in Streams.146#define LZMA_FILTER_RESERVED_START (LZMA_VLI_C(1) << 62)147148149/// Supported flags that can be passed to lzma_stream_decoder(),150/// lzma_auto_decoder(), or lzma_stream_decoder_mt().151#define LZMA_SUPPORTED_FLAGS \152( LZMA_TELL_NO_CHECK \153| LZMA_TELL_UNSUPPORTED_CHECK \154| LZMA_TELL_ANY_CHECK \155| LZMA_IGNORE_CHECK \156| LZMA_CONCATENATED \157| LZMA_FAIL_FAST )158159160/// Largest valid lzma_action value as unsigned integer.161#define LZMA_ACTION_MAX ((unsigned int)(LZMA_FULL_BARRIER))162163164/// Special return value (lzma_ret) to indicate that a timeout was reached165/// and lzma_code() must not return LZMA_BUF_ERROR. This is converted to166/// LZMA_OK in lzma_code().167#define LZMA_TIMED_OUT LZMA_RET_INTERNAL1168169/// Special return value (lzma_ret) for use in stream_decoder_mt.c to170/// indicate Index was detected instead of a Block Header.171#define LZMA_INDEX_DETECTED LZMA_RET_INTERNAL2172173174typedef struct lzma_next_coder_s lzma_next_coder;175176typedef struct lzma_filter_info_s lzma_filter_info;177178179/// Type of a function used to initialize a filter encoder or decoder180typedef lzma_ret (*lzma_init_function)(181lzma_next_coder *next, const lzma_allocator *allocator,182const lzma_filter_info *filters);183184/// Type of a function to do some kind of coding work (filters, Stream,185/// Block encoders/decoders etc.). Some special coders use don't use both186/// input and output buffers, but for simplicity they still use this same187/// function prototype.188typedef lzma_ret (*lzma_code_function)(189void *coder, const lzma_allocator *allocator,190const uint8_t *restrict in, size_t *restrict in_pos,191size_t in_size, uint8_t *restrict out,192size_t *restrict out_pos, size_t out_size,193lzma_action action);194195/// Type of a function to free the memory allocated for the coder196typedef void (*lzma_end_function)(197void *coder, const lzma_allocator *allocator);198199200/// Raw coder validates and converts an array of lzma_filter structures to201/// an array of lzma_filter_info structures. This array is used with202/// lzma_next_filter_init to initialize the filter chain.203struct lzma_filter_info_s {204/// Filter ID. This can be used to share the same initiazation205/// function *and* data structures with different Filter IDs206/// (LZMA_FILTER_LZMA1EXT does it), and also by the encoder207/// with lzma_filters_update() if filter chain is updated208/// in the middle of a raw stream or Block (LZMA_SYNC_FLUSH).209lzma_vli id;210211/// Pointer to function used to initialize the filter.212/// This is NULL to indicate end of array.213lzma_init_function init;214215/// Pointer to filter's options structure216void *options;217};218219220/// Hold data and function pointers of the next filter in the chain.221struct lzma_next_coder_s {222/// Pointer to coder-specific data223void *coder;224225/// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't226/// point to a filter coder.227lzma_vli id;228229/// "Pointer" to init function. This is never called here.230/// We need only to detect if we are initializing a coder231/// that was allocated earlier. See lzma_next_coder_init and232/// lzma_next_strm_init macros in this file.233uintptr_t init;234235/// Pointer to function to do the actual coding236lzma_code_function code;237238/// Pointer to function to free lzma_next_coder.coder. This can239/// be NULL; in that case, lzma_free is called to free240/// lzma_next_coder.coder.241lzma_end_function end;242243/// Pointer to a function to get progress information. If this is NULL,244/// lzma_stream.total_in and .total_out are used instead.245void (*get_progress)(void *coder,246uint64_t *progress_in, uint64_t *progress_out);247248/// Pointer to function to return the type of the integrity check.249/// Most coders won't support this.250lzma_check (*get_check)(const void *coder);251252/// Pointer to function to get and/or change the memory usage limit.253/// If new_memlimit == 0, the limit is not changed.254lzma_ret (*memconfig)(void *coder, uint64_t *memusage,255uint64_t *old_memlimit, uint64_t new_memlimit);256257/// Update the filter-specific options or the whole filter chain258/// in the encoder.259lzma_ret (*update)(void *coder, const lzma_allocator *allocator,260const lzma_filter *filters,261const lzma_filter *reversed_filters);262263/// Set how many bytes of output this coder may produce at maximum.264/// On success LZMA_OK must be returned.265/// If the filter chain as a whole cannot support this feature,266/// this must return LZMA_OPTIONS_ERROR.267/// If no input has been given to the coder and the requested limit268/// is too small, this must return LZMA_BUF_ERROR. If input has been269/// seen, LZMA_OK is allowed too.270lzma_ret (*set_out_limit)(void *coder, uint64_t *uncomp_size,271uint64_t out_limit);272};273274275/// Macro to initialize lzma_next_coder structure276#define LZMA_NEXT_CODER_INIT \277(lzma_next_coder){ \278.coder = NULL, \279.init = (uintptr_t)(NULL), \280.id = LZMA_VLI_UNKNOWN, \281.code = NULL, \282.end = NULL, \283.get_progress = NULL, \284.get_check = NULL, \285.memconfig = NULL, \286.update = NULL, \287.set_out_limit = NULL, \288}289290291/// Internal data for lzma_strm_init, lzma_code, and lzma_end. A pointer to292/// this is stored in lzma_stream.293struct lzma_internal_s {294/// The actual coder that should do something useful295lzma_next_coder next;296297/// Track the state of the coder. This is used to validate arguments298/// so that the actual coders can rely on e.g. that LZMA_SYNC_FLUSH299/// is used on every call to lzma_code until next.code has returned300/// LZMA_STREAM_END.301enum {302ISEQ_RUN,303ISEQ_SYNC_FLUSH,304ISEQ_FULL_FLUSH,305ISEQ_FINISH,306ISEQ_FULL_BARRIER,307ISEQ_END,308ISEQ_ERROR,309} sequence;310311/// A copy of lzma_stream avail_in. This is used to verify that the312/// amount of input doesn't change once e.g. LZMA_FINISH has been313/// used.314size_t avail_in;315316/// Indicates which lzma_action values are allowed by next.code.317bool supported_actions[LZMA_ACTION_MAX + 1];318319/// If true, lzma_code will return LZMA_BUF_ERROR if no progress was320/// made (no input consumed and no output produced by next.code).321bool allow_buf_error;322};323324325/// Allocates memory326lzma_attr_alloc_size(1)327extern void *lzma_alloc(size_t size, const lzma_allocator *allocator);328329/// Allocates memory and zeroes it (like calloc()). This can be faster330/// than lzma_alloc() + memzero() while being backward compatible with331/// custom allocators.332lzma_attr_alloc_size(1)333extern void *lzma_alloc_zero(size_t size, const lzma_allocator *allocator);334335/// Frees memory336extern void lzma_free(void *ptr, const lzma_allocator *allocator);337338339/// Allocates strm->internal if it is NULL, and initializes *strm and340/// strm->internal. This function is only called via lzma_next_strm_init macro.341extern lzma_ret lzma_strm_init(lzma_stream *strm);342343/// Initializes the next filter in the chain, if any. This takes care of344/// freeing the memory of previously initialized filter if it is different345/// than the filter being initialized now. This way the actual filter346/// initialization functions don't need to use lzma_next_coder_init macro.347extern lzma_ret lzma_next_filter_init(lzma_next_coder *next,348const lzma_allocator *allocator,349const lzma_filter_info *filters);350351/// Update the next filter in the chain, if any. This checks that352/// the application is not trying to change the Filter IDs.353extern lzma_ret lzma_next_filter_update(354lzma_next_coder *next, const lzma_allocator *allocator,355const lzma_filter *reversed_filters);356357/// Frees the memory allocated for next->coder either using next->end or,358/// if next->end is NULL, using lzma_free.359extern void lzma_next_end(lzma_next_coder *next,360const lzma_allocator *allocator);361362363/// Copy as much data as possible from in[] to out[] and update *in_pos364/// and *out_pos accordingly. Returns the number of bytes copied.365extern size_t lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,366size_t in_size, uint8_t *restrict out,367size_t *restrict out_pos, size_t out_size);368369370/// \brief Return if expression doesn't evaluate to LZMA_OK371///372/// There are several situations where we want to return immediately373/// with the value of expr if it isn't LZMA_OK. This macro shortens374/// the code a little.375#define return_if_error(expr) \376do { \377const lzma_ret ret_ = (expr); \378if (ret_ != LZMA_OK) \379return ret_; \380} while (0)381382383/// If next isn't already initialized, free the previous coder. Then mark384/// that next is _possibly_ initialized for the coder using this macro.385/// "Possibly" means that if e.g. allocation of next->coder fails, the386/// structure isn't actually initialized for this coder, but leaving387/// next->init to func is still OK.388#define lzma_next_coder_init(func, next, allocator) \389do { \390if ((uintptr_t)(func) != (next)->init) \391lzma_next_end(next, allocator); \392(next)->init = (uintptr_t)(func); \393} while (0)394395396/// Initializes lzma_strm and calls func() to initialize strm->internal->next.397/// (The function being called will use lzma_next_coder_init()). If398/// initialization fails, memory that wasn't freed by func() is freed399/// along strm->internal.400#define lzma_next_strm_init(func, strm, ...) \401do { \402return_if_error(lzma_strm_init(strm)); \403const lzma_ret ret_ = func(&(strm)->internal->next, \404(strm)->allocator, __VA_ARGS__); \405if (ret_ != LZMA_OK) { \406lzma_end(strm); \407return ret_; \408} \409} while (0)410411#endif412413414