common.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file common.h
  4. /// \brief Definitions common to the whole liblzma library
  5. //
  6. // Author: Lasse Collin
  7. //
  8. // This file has been put into the public domain.
  9. // You can do whatever you want with this file.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #ifndef LZMA_COMMON_H
  13. #define LZMA_COMMON_H
  14. #include "sysdefs.h"
  15. #include "tuklib_integer.h"
  16. #if defined(_WIN32) || defined(__CYGWIN__)
  17. # ifdef DLL_EXPORT
  18. # define LZMA_API_EXPORT __declspec(dllexport)
  19. # else
  20. # define LZMA_API_EXPORT
  21. # endif
  22. // Don't use ifdef or defined() below.
  23. #elif HAVE_VISIBILITY
  24. # define LZMA_API_EXPORT __attribute__((__visibility__("default")))
  25. #else
  26. # define LZMA_API_EXPORT
  27. #endif
  28. #define LZMA_API(type) LZMA_API_EXPORT type LZMA_API_CALL
  29. #include "lzma.h"
  30. // These allow helping the compiler in some often-executed branches, whose
  31. // result is almost always the same.
  32. #ifdef __GNUC__
  33. # define likely(expr) __builtin_expect(expr, true)
  34. # define unlikely(expr) __builtin_expect(expr, false)
  35. #else
  36. # define likely(expr) (expr)
  37. # define unlikely(expr) (expr)
  38. #endif
  39. /// Size of temporary buffers needed in some filters
  40. #define LZMA_BUFFER_SIZE 4096
  41. /// Maximum number of worker threads within one multithreaded component.
  42. /// The limit exists solely to make it simpler to prevent integer overflows
  43. /// when allocating structures etc. This should be big enough for now...
  44. /// the code won't scale anywhere close to this number anyway.
  45. #define LZMA_THREADS_MAX 16384
  46. /// Starting value for memory usage estimates. Instead of calculating size
  47. /// of _every_ structure and taking into account malloc() overhead etc., we
  48. /// add a base size to all memory usage estimates. It's not very accurate
  49. /// but should be easily good enough.
  50. #define LZMA_MEMUSAGE_BASE (UINT64_C(1) << 15)
  51. /// Start of internal Filter ID space. These IDs must never be used
  52. /// in Streams.
  53. #define LZMA_FILTER_RESERVED_START (LZMA_VLI_C(1) << 62)
  54. /// Supported flags that can be passed to lzma_stream_decoder()
  55. /// or lzma_auto_decoder().
  56. #define LZMA_SUPPORTED_FLAGS \
  57. ( LZMA_TELL_NO_CHECK \
  58. | LZMA_TELL_UNSUPPORTED_CHECK \
  59. | LZMA_TELL_ANY_CHECK \
  60. | LZMA_IGNORE_CHECK \
  61. | LZMA_CONCATENATED )
  62. /// Largest valid lzma_action value as unsigned integer.
  63. #define LZMA_ACTION_MAX ((unsigned int)(LZMA_FULL_BARRIER))
  64. /// Special return value (lzma_ret) to indicate that a timeout was reached
  65. /// and lzma_code() must not return LZMA_BUF_ERROR. This is converted to
  66. /// LZMA_OK in lzma_code(). This is not in the lzma_ret enumeration because
  67. /// there's no need to have it in the public API.
  68. #define LZMA_TIMED_OUT 32
  69. typedef struct lzma_next_coder_s lzma_next_coder;
  70. typedef struct lzma_filter_info_s lzma_filter_info;
  71. /// Type of a function used to initialize a filter encoder or decoder
  72. typedef lzma_ret (*lzma_init_function)(
  73. lzma_next_coder *next, const lzma_allocator *allocator,
  74. const lzma_filter_info *filters);
  75. /// Type of a function to do some kind of coding work (filters, Stream,
  76. /// Block encoders/decoders etc.). Some special coders use don't use both
  77. /// input and output buffers, but for simplicity they still use this same
  78. /// function prototype.
  79. typedef lzma_ret (*lzma_code_function)(
  80. void *coder, const lzma_allocator *allocator,
  81. const uint8_t *restrict in, size_t *restrict in_pos,
  82. size_t in_size, uint8_t *restrict out,
  83. size_t *restrict out_pos, size_t out_size,
  84. lzma_action action);
  85. /// Type of a function to free the memory allocated for the coder
  86. typedef void (*lzma_end_function)(
  87. void *coder, const lzma_allocator *allocator);
  88. /// Raw coder validates and converts an array of lzma_filter structures to
  89. /// an array of lzma_filter_info structures. This array is used with
  90. /// lzma_next_filter_init to initialize the filter chain.
  91. struct lzma_filter_info_s {
  92. /// Filter ID. This is used only by the encoder
  93. /// with lzma_filters_update().
  94. lzma_vli id;
  95. /// Pointer to function used to initialize the filter.
  96. /// This is NULL to indicate end of array.
  97. lzma_init_function init;
  98. /// Pointer to filter's options structure
  99. void *options;
  100. };
  101. /// Hold data and function pointers of the next filter in the chain.
  102. struct lzma_next_coder_s {
  103. /// Pointer to coder-specific data
  104. void *coder;
  105. /// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't
  106. /// point to a filter coder.
  107. lzma_vli id;
  108. /// "Pointer" to init function. This is never called here.
  109. /// We need only to detect if we are initializing a coder
  110. /// that was allocated earlier. See lzma_next_coder_init and
  111. /// lzma_next_strm_init macros in this file.
  112. uintptr_t init;
  113. /// Pointer to function to do the actual coding
  114. lzma_code_function code;
  115. /// Pointer to function to free lzma_next_coder.coder. This can
  116. /// be NULL; in that case, lzma_free is called to free
  117. /// lzma_next_coder.coder.
  118. lzma_end_function end;
  119. /// Pointer to a function to get progress information. If this is NULL,
  120. /// lzma_stream.total_in and .total_out are used instead.
  121. void (*get_progress)(void *coder,
  122. uint64_t *progress_in, uint64_t *progress_out);
  123. /// Pointer to function to return the type of the integrity check.
  124. /// Most coders won't support this.
  125. lzma_check (*get_check)(const void *coder);
  126. /// Pointer to function to get and/or change the memory usage limit.
  127. /// If new_memlimit == 0, the limit is not changed.
  128. lzma_ret (*memconfig)(void *coder, uint64_t *memusage,
  129. uint64_t *old_memlimit, uint64_t new_memlimit);
  130. /// Update the filter-specific options or the whole filter chain
  131. /// in the encoder.
  132. lzma_ret (*update)(void *coder, const lzma_allocator *allocator,
  133. const lzma_filter *filters,
  134. const lzma_filter *reversed_filters);
  135. };
  136. /// Macro to initialize lzma_next_coder structure
  137. #define LZMA_NEXT_CODER_INIT \
  138. (lzma_next_coder){ \
  139. .coder = NULL, \
  140. .init = (uintptr_t)(NULL), \
  141. .id = LZMA_VLI_UNKNOWN, \
  142. .code = NULL, \
  143. .end = NULL, \
  144. .get_progress = NULL, \
  145. .get_check = NULL, \
  146. .memconfig = NULL, \
  147. .update = NULL, \
  148. }
  149. /// Internal data for lzma_strm_init, lzma_code, and lzma_end. A pointer to
  150. /// this is stored in lzma_stream.
  151. struct lzma_internal_s {
  152. /// The actual coder that should do something useful
  153. lzma_next_coder next;
  154. /// Track the state of the coder. This is used to validate arguments
  155. /// so that the actual coders can rely on e.g. that LZMA_SYNC_FLUSH
  156. /// is used on every call to lzma_code until next.code has returned
  157. /// LZMA_STREAM_END.
  158. enum {
  159. ISEQ_RUN,
  160. ISEQ_SYNC_FLUSH,
  161. ISEQ_FULL_FLUSH,
  162. ISEQ_FINISH,
  163. ISEQ_FULL_BARRIER,
  164. ISEQ_END,
  165. ISEQ_ERROR,
  166. } sequence;
  167. /// A copy of lzma_stream avail_in. This is used to verify that the
  168. /// amount of input doesn't change once e.g. LZMA_FINISH has been
  169. /// used.
  170. size_t avail_in;
  171. /// Indicates which lzma_action values are allowed by next.code.
  172. bool supported_actions[LZMA_ACTION_MAX + 1];
  173. /// If true, lzma_code will return LZMA_BUF_ERROR if no progress was
  174. /// made (no input consumed and no output produced by next.code).
  175. bool allow_buf_error;
  176. };
  177. /// Allocates memory
  178. extern void *lzma_alloc(size_t size, const lzma_allocator *allocator)
  179. lzma_attribute((__malloc__)) lzma_attr_alloc_size(1);
  180. /// Allocates memory and zeroes it (like calloc()). This can be faster
  181. /// than lzma_alloc() + memzero() while being backward compatible with
  182. /// custom allocators.
  183. extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
  184. lzma_alloc_zero(size_t size, const lzma_allocator *allocator);
  185. /// Frees memory
  186. extern void lzma_free(void *ptr, const lzma_allocator *allocator);
  187. /// Allocates strm->internal if it is NULL, and initializes *strm and
  188. /// strm->internal. This function is only called via lzma_next_strm_init macro.
  189. extern lzma_ret lzma_strm_init(lzma_stream *strm);
  190. /// Initializes the next filter in the chain, if any. This takes care of
  191. /// freeing the memory of previously initialized filter if it is different
  192. /// than the filter being initialized now. This way the actual filter
  193. /// initialization functions don't need to use lzma_next_coder_init macro.
  194. extern lzma_ret lzma_next_filter_init(lzma_next_coder *next,
  195. const lzma_allocator *allocator,
  196. const lzma_filter_info *filters);
  197. /// Update the next filter in the chain, if any. This checks that
  198. /// the application is not trying to change the Filter IDs.
  199. extern lzma_ret lzma_next_filter_update(
  200. lzma_next_coder *next, const lzma_allocator *allocator,
  201. const lzma_filter *reversed_filters);
  202. /// Frees the memory allocated for next->coder either using next->end or,
  203. /// if next->end is NULL, using lzma_free.
  204. extern void lzma_next_end(lzma_next_coder *next,
  205. const lzma_allocator *allocator);
  206. /// Copy as much data as possible from in[] to out[] and update *in_pos
  207. /// and *out_pos accordingly. Returns the number of bytes copied.
  208. extern size_t lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
  209. size_t in_size, uint8_t *restrict out,
  210. size_t *restrict out_pos, size_t out_size);
  211. /// \brief Return if expression doesn't evaluate to LZMA_OK
  212. ///
  213. /// There are several situations where we want to return immediately
  214. /// with the value of expr if it isn't LZMA_OK. This macro shortens
  215. /// the code a little.
  216. #define return_if_error(expr) \
  217. do { \
  218. const lzma_ret ret_ = (expr); \
  219. if (ret_ != LZMA_OK) \
  220. return ret_; \
  221. } while (0)
  222. /// If next isn't already initialized, free the previous coder. Then mark
  223. /// that next is _possibly_ initialized for the coder using this macro.
  224. /// "Possibly" means that if e.g. allocation of next->coder fails, the
  225. /// structure isn't actually initialized for this coder, but leaving
  226. /// next->init to func is still OK.
  227. #define lzma_next_coder_init(func, next, allocator) \
  228. do { \
  229. if ((uintptr_t)(func) != (next)->init) \
  230. lzma_next_end(next, allocator); \
  231. (next)->init = (uintptr_t)(func); \
  232. } while (0)
  233. /// Initializes lzma_strm and calls func() to initialize strm->internal->next.
  234. /// (The function being called will use lzma_next_coder_init()). If
  235. /// initialization fails, memory that wasn't freed by func() is freed
  236. /// along strm->internal.
  237. #define lzma_next_strm_init(func, strm, ...) \
  238. do { \
  239. return_if_error(lzma_strm_init(strm)); \
  240. const lzma_ret ret_ = func(&(strm)->internal->next, \
  241. (strm)->allocator, __VA_ARGS__); \
  242. if (ret_ != LZMA_OK) { \
  243. lzma_end(strm); \
  244. return ret_; \
  245. } \
  246. } while (0)
  247. #endif