common.h 10 KB

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