common.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file common.h
  5. /// \brief Definitions common to the whole liblzma library
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef LZMA_COMMON_H
  11. #define LZMA_COMMON_H
  12. #include "sysdefs.h"
  13. #include "mythread.h"
  14. #include "tuklib_integer.h"
  15. // LZMA_API_EXPORT is used to mark the exported API functions.
  16. // It's used to define the LZMA_API macro.
  17. //
  18. // lzma_attr_visibility_hidden is used for marking *declarations* of extern
  19. // variables that are internal to liblzma (-fvisibility=hidden alone is
  20. // enough to hide the *definitions*). Such markings allow slightly more
  21. // efficient code to accesses those variables in ELF shared libraries.
  22. #if defined(_WIN32) || defined(__CYGWIN__)
  23. # ifdef DLL_EXPORT
  24. # define LZMA_API_EXPORT __declspec(dllexport)
  25. # else
  26. # define LZMA_API_EXPORT
  27. # endif
  28. # define lzma_attr_visibility_hidden
  29. // Don't use ifdef or defined() below.
  30. #elif HAVE_VISIBILITY
  31. # define LZMA_API_EXPORT __attribute__((__visibility__("default")))
  32. # define lzma_attr_visibility_hidden \
  33. __attribute__((__visibility__("hidden")))
  34. #else
  35. # define LZMA_API_EXPORT
  36. # define lzma_attr_visibility_hidden
  37. #endif
  38. #define LZMA_API(type) LZMA_API_EXPORT type LZMA_API_CALL
  39. #include "lzma.h"
  40. // This is for detecting modern GCC and Clang attributes
  41. // like __symver__ in GCC >= 10.
  42. #ifdef __has_attribute
  43. # define lzma_has_attribute(attr) __has_attribute(attr)
  44. #else
  45. # define lzma_has_attribute(attr) 0
  46. #endif
  47. // The extra symbol versioning in the C files may only be used when
  48. // building a shared library. If HAVE_SYMBOL_VERSIONS_LINUX is defined
  49. // to 2 then symbol versioning is done only if also PIC is defined.
  50. // By default Libtool defines PIC when building a shared library and
  51. // doesn't define it when building a static library but it can be
  52. // overridden with --with-pic and --without-pic. configure let's rely
  53. // on PIC if neither --with-pic or --without-pic was used.
  54. #if defined(HAVE_SYMBOL_VERSIONS_LINUX) \
  55. && (HAVE_SYMBOL_VERSIONS_LINUX == 2 && !defined(PIC))
  56. # undef HAVE_SYMBOL_VERSIONS_LINUX
  57. #endif
  58. #ifdef HAVE_SYMBOL_VERSIONS_LINUX
  59. // To keep link-time optimization (LTO, -flto) working with GCC,
  60. // the __symver__ attribute must be used instead of __asm__(".symver ...").
  61. // Otherwise the symbol versions may be lost, resulting in broken liblzma
  62. // that has wrong default versions in the exported symbol list!
  63. // The attribute was added in GCC 10; LTO with older GCC is not supported.
  64. //
  65. // To keep -Wmissing-prototypes happy, use LZMA_SYMVER_API only with function
  66. // declarations (including those with __alias__ attribute) and LZMA_API with
  67. // the function definitions. This means a little bit of silly copy-and-paste
  68. // between declarations and definitions though.
  69. //
  70. // As of GCC 12.2, the __symver__ attribute supports only @ and @@ but the
  71. // very convenient @@@ isn't supported (it's supported by GNU assembler
  72. // since 2000). When using @@ instead of @@@, the internal name must not be
  73. // the same as the external name to avoid problems in some situations. This
  74. // is why "#define foo_52 foo" is needed for the default symbol versions.
  75. //
  76. // __has_attribute is supported before GCC 10 and it is supported in Clang 14
  77. // too (which doesn't support __symver__) so use it to detect if __symver__
  78. // is available. This should be far more reliable than looking at compiler
  79. // version macros as nowadays especially __GNUC__ is defined by many compilers.
  80. # if lzma_has_attribute(__symver__)
  81. # define LZMA_SYMVER_API(extnamever, type, intname) \
  82. extern __attribute__((__symver__(extnamever))) \
  83. LZMA_API(type) intname
  84. # else
  85. # define LZMA_SYMVER_API(extnamever, type, intname) \
  86. __asm__(".symver " #intname "," extnamever); \
  87. extern LZMA_API(type) intname
  88. # endif
  89. #endif
  90. // MSVC has __forceinline which shouldn't be combined with the inline keyword
  91. // (results in a warning).
  92. //
  93. // GCC 3.1 added always_inline attribute so we don't need to check
  94. // for __GNUC__ version. Similarly, all relevant Clang versions
  95. // support it (at least Clang 3.0.0 does already).
  96. // Other compilers might support too which also support __has_attribute
  97. // (Solaris Studio) so do that check too.
  98. #if defined(_MSC_VER)
  99. # define lzma_always_inline __forceinline
  100. #elif defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER) \
  101. || lzma_has_attribute(__always_inline__)
  102. # define lzma_always_inline inline __attribute__((__always_inline__))
  103. #else
  104. # define lzma_always_inline inline
  105. #endif
  106. // These allow helping the compiler in some often-executed branches, whose
  107. // result is almost always the same.
  108. #ifdef __GNUC__
  109. # define likely(expr) __builtin_expect(expr, true)
  110. # define unlikely(expr) __builtin_expect(expr, false)
  111. #else
  112. # define likely(expr) (expr)
  113. # define unlikely(expr) (expr)
  114. #endif
  115. /// Size of temporary buffers needed in some filters
  116. #define LZMA_BUFFER_SIZE 4096
  117. /// Maximum number of worker threads within one multithreaded component.
  118. /// The limit exists solely to make it simpler to prevent integer overflows
  119. /// when allocating structures etc. This should be big enough for now...
  120. /// the code won't scale anywhere close to this number anyway.
  121. #define LZMA_THREADS_MAX 16384
  122. /// Starting value for memory usage estimates. Instead of calculating size
  123. /// of _every_ structure and taking into account malloc() overhead etc., we
  124. /// add a base size to all memory usage estimates. It's not very accurate
  125. /// but should be easily good enough.
  126. #define LZMA_MEMUSAGE_BASE (UINT64_C(1) << 15)
  127. /// Start of internal Filter ID space. These IDs must never be used
  128. /// in Streams.
  129. #define LZMA_FILTER_RESERVED_START (LZMA_VLI_C(1) << 62)
  130. /// Supported flags that can be passed to lzma_stream_decoder(),
  131. /// lzma_auto_decoder(), or lzma_stream_decoder_mt().
  132. #define LZMA_SUPPORTED_FLAGS \
  133. ( LZMA_TELL_NO_CHECK \
  134. | LZMA_TELL_UNSUPPORTED_CHECK \
  135. | LZMA_TELL_ANY_CHECK \
  136. | LZMA_IGNORE_CHECK \
  137. | LZMA_CONCATENATED \
  138. | LZMA_FAIL_FAST )
  139. /// Largest valid lzma_action value as unsigned integer.
  140. #define LZMA_ACTION_MAX ((unsigned int)(LZMA_FULL_BARRIER))
  141. /// Special return value (lzma_ret) to indicate that a timeout was reached
  142. /// and lzma_code() must not return LZMA_BUF_ERROR. This is converted to
  143. /// LZMA_OK in lzma_code().
  144. #define LZMA_TIMED_OUT LZMA_RET_INTERNAL1
  145. /// Special return value (lzma_ret) for use in stream_decoder_mt.c to
  146. /// indicate Index was detected instead of a Block Header.
  147. #define LZMA_INDEX_DETECTED LZMA_RET_INTERNAL2
  148. typedef struct lzma_next_coder_s lzma_next_coder;
  149. typedef struct lzma_filter_info_s lzma_filter_info;
  150. /// Type of a function used to initialize a filter encoder or decoder
  151. typedef lzma_ret (*lzma_init_function)(
  152. lzma_next_coder *next, const lzma_allocator *allocator,
  153. const lzma_filter_info *filters);
  154. /// Type of a function to do some kind of coding work (filters, Stream,
  155. /// Block encoders/decoders etc.). Some special coders use don't use both
  156. /// input and output buffers, but for simplicity they still use this same
  157. /// function prototype.
  158. typedef lzma_ret (*lzma_code_function)(
  159. void *coder, const lzma_allocator *allocator,
  160. const uint8_t *restrict in, size_t *restrict in_pos,
  161. size_t in_size, uint8_t *restrict out,
  162. size_t *restrict out_pos, size_t out_size,
  163. lzma_action action);
  164. /// Type of a function to free the memory allocated for the coder
  165. typedef void (*lzma_end_function)(
  166. void *coder, const lzma_allocator *allocator);
  167. /// Raw coder validates and converts an array of lzma_filter structures to
  168. /// an array of lzma_filter_info structures. This array is used with
  169. /// lzma_next_filter_init to initialize the filter chain.
  170. struct lzma_filter_info_s {
  171. /// Filter ID. This can be used to share the same initiazation
  172. /// function *and* data structures with different Filter IDs
  173. /// (LZMA_FILTER_LZMA1EXT does it), and also by the encoder
  174. /// with lzma_filters_update() if filter chain is updated
  175. /// in the middle of a raw stream or Block (LZMA_SYNC_FLUSH).
  176. lzma_vli id;
  177. /// Pointer to function used to initialize the filter.
  178. /// This is NULL to indicate end of array.
  179. lzma_init_function init;
  180. /// Pointer to filter's options structure
  181. void *options;
  182. };
  183. /// Hold data and function pointers of the next filter in the chain.
  184. struct lzma_next_coder_s {
  185. /// Pointer to coder-specific data
  186. void *coder;
  187. /// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't
  188. /// point to a filter coder.
  189. lzma_vli id;
  190. /// "Pointer" to init function. This is never called here.
  191. /// We need only to detect if we are initializing a coder
  192. /// that was allocated earlier. See lzma_next_coder_init and
  193. /// lzma_next_strm_init macros in this file.
  194. uintptr_t init;
  195. /// Pointer to function to do the actual coding
  196. lzma_code_function code;
  197. /// Pointer to function to free lzma_next_coder.coder. This can
  198. /// be NULL; in that case, lzma_free is called to free
  199. /// lzma_next_coder.coder.
  200. lzma_end_function end;
  201. /// Pointer to a function to get progress information. If this is NULL,
  202. /// lzma_stream.total_in and .total_out are used instead.
  203. void (*get_progress)(void *coder,
  204. uint64_t *progress_in, uint64_t *progress_out);
  205. /// Pointer to function to return the type of the integrity check.
  206. /// Most coders won't support this.
  207. lzma_check (*get_check)(const void *coder);
  208. /// Pointer to function to get and/or change the memory usage limit.
  209. /// If new_memlimit == 0, the limit is not changed.
  210. lzma_ret (*memconfig)(void *coder, uint64_t *memusage,
  211. uint64_t *old_memlimit, uint64_t new_memlimit);
  212. /// Update the filter-specific options or the whole filter chain
  213. /// in the encoder.
  214. lzma_ret (*update)(void *coder, const lzma_allocator *allocator,
  215. const lzma_filter *filters,
  216. const lzma_filter *reversed_filters);
  217. /// Set how many bytes of output this coder may produce at maximum.
  218. /// On success LZMA_OK must be returned.
  219. /// If the filter chain as a whole cannot support this feature,
  220. /// this must return LZMA_OPTIONS_ERROR.
  221. /// If no input has been given to the coder and the requested limit
  222. /// is too small, this must return LZMA_BUF_ERROR. If input has been
  223. /// seen, LZMA_OK is allowed too.
  224. lzma_ret (*set_out_limit)(void *coder, uint64_t *uncomp_size,
  225. uint64_t out_limit);
  226. };
  227. /// Macro to initialize lzma_next_coder structure
  228. #define LZMA_NEXT_CODER_INIT \
  229. (lzma_next_coder){ \
  230. .coder = NULL, \
  231. .init = (uintptr_t)(NULL), \
  232. .id = LZMA_VLI_UNKNOWN, \
  233. .code = NULL, \
  234. .end = NULL, \
  235. .get_progress = NULL, \
  236. .get_check = NULL, \
  237. .memconfig = NULL, \
  238. .update = NULL, \
  239. .set_out_limit = NULL, \
  240. }
  241. /// Internal data for lzma_strm_init, lzma_code, and lzma_end. A pointer to
  242. /// this is stored in lzma_stream.
  243. struct lzma_internal_s {
  244. /// The actual coder that should do something useful
  245. lzma_next_coder next;
  246. /// Track the state of the coder. This is used to validate arguments
  247. /// so that the actual coders can rely on e.g. that LZMA_SYNC_FLUSH
  248. /// is used on every call to lzma_code until next.code has returned
  249. /// LZMA_STREAM_END.
  250. enum {
  251. ISEQ_RUN,
  252. ISEQ_SYNC_FLUSH,
  253. ISEQ_FULL_FLUSH,
  254. ISEQ_FINISH,
  255. ISEQ_FULL_BARRIER,
  256. ISEQ_END,
  257. ISEQ_ERROR,
  258. } sequence;
  259. /// A copy of lzma_stream avail_in. This is used to verify that the
  260. /// amount of input doesn't change once e.g. LZMA_FINISH has been
  261. /// used.
  262. size_t avail_in;
  263. /// Indicates which lzma_action values are allowed by next.code.
  264. bool supported_actions[LZMA_ACTION_MAX + 1];
  265. /// If true, lzma_code will return LZMA_BUF_ERROR if no progress was
  266. /// made (no input consumed and no output produced by next.code).
  267. bool allow_buf_error;
  268. };
  269. /// Allocates memory
  270. lzma_attr_alloc_size(1)
  271. extern void *lzma_alloc(size_t size, const lzma_allocator *allocator);
  272. /// Allocates memory and zeroes it (like calloc()). This can be faster
  273. /// than lzma_alloc() + memzero() while being backward compatible with
  274. /// custom allocators.
  275. lzma_attr_alloc_size(1)
  276. extern void *lzma_alloc_zero(size_t size, const lzma_allocator *allocator);
  277. /// Frees memory
  278. extern void lzma_free(void *ptr, const lzma_allocator *allocator);
  279. /// Allocates strm->internal if it is NULL, and initializes *strm and
  280. /// strm->internal. This function is only called via lzma_next_strm_init macro.
  281. extern lzma_ret lzma_strm_init(lzma_stream *strm);
  282. /// Initializes the next filter in the chain, if any. This takes care of
  283. /// freeing the memory of previously initialized filter if it is different
  284. /// than the filter being initialized now. This way the actual filter
  285. /// initialization functions don't need to use lzma_next_coder_init macro.
  286. extern lzma_ret lzma_next_filter_init(lzma_next_coder *next,
  287. const lzma_allocator *allocator,
  288. const lzma_filter_info *filters);
  289. /// Update the next filter in the chain, if any. This checks that
  290. /// the application is not trying to change the Filter IDs.
  291. extern lzma_ret lzma_next_filter_update(
  292. lzma_next_coder *next, const lzma_allocator *allocator,
  293. const lzma_filter *reversed_filters);
  294. /// Frees the memory allocated for next->coder either using next->end or,
  295. /// if next->end is NULL, using lzma_free.
  296. extern void lzma_next_end(lzma_next_coder *next,
  297. const lzma_allocator *allocator);
  298. /// Copy as much data as possible from in[] to out[] and update *in_pos
  299. /// and *out_pos accordingly. Returns the number of bytes copied.
  300. extern size_t lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
  301. size_t in_size, uint8_t *restrict out,
  302. size_t *restrict out_pos, size_t out_size);
  303. /// \brief Return if expression doesn't evaluate to LZMA_OK
  304. ///
  305. /// There are several situations where we want to return immediately
  306. /// with the value of expr if it isn't LZMA_OK. This macro shortens
  307. /// the code a little.
  308. #define return_if_error(expr) \
  309. do { \
  310. const lzma_ret ret_ = (expr); \
  311. if (ret_ != LZMA_OK) \
  312. return ret_; \
  313. } while (0)
  314. /// If next isn't already initialized, free the previous coder. Then mark
  315. /// that next is _possibly_ initialized for the coder using this macro.
  316. /// "Possibly" means that if e.g. allocation of next->coder fails, the
  317. /// structure isn't actually initialized for this coder, but leaving
  318. /// next->init to func is still OK.
  319. #define lzma_next_coder_init(func, next, allocator) \
  320. do { \
  321. if ((uintptr_t)(func) != (next)->init) \
  322. lzma_next_end(next, allocator); \
  323. (next)->init = (uintptr_t)(func); \
  324. } while (0)
  325. /// Initializes lzma_strm and calls func() to initialize strm->internal->next.
  326. /// (The function being called will use lzma_next_coder_init()). If
  327. /// initialization fails, memory that wasn't freed by func() is freed
  328. /// along strm->internal.
  329. #define lzma_next_strm_init(func, strm, ...) \
  330. do { \
  331. return_if_error(lzma_strm_init(strm)); \
  332. const lzma_ret ret_ = func(&(strm)->internal->next, \
  333. (strm)->allocator, __VA_ARGS__); \
  334. if (ret_ != LZMA_OK) { \
  335. lzma_end(strm); \
  336. return ret_; \
  337. } \
  338. } while (0)
  339. #endif