common.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. /// Starting value for memory usage estimates. Instead of calculating size
  42. /// of _every_ structure and taking into account malloc() overhead etc., we
  43. /// add a base size to all memory usage estimates. It's not very accurate
  44. /// but should be easily good enough.
  45. #define LZMA_MEMUSAGE_BASE (UINT64_C(1) << 15)
  46. /// Start of internal Filter ID space. These IDs must never be used
  47. /// in Streams.
  48. #define LZMA_FILTER_RESERVED_START (LZMA_VLI_C(1) << 62)
  49. /// Supported flags that can be passed to lzma_stream_decoder()
  50. /// or lzma_auto_decoder().
  51. #define LZMA_SUPPORTED_FLAGS \
  52. ( LZMA_TELL_NO_CHECK \
  53. | LZMA_TELL_UNSUPPORTED_CHECK \
  54. | LZMA_TELL_ANY_CHECK \
  55. | LZMA_CONCATENATED )
  56. /// Type of encoder/decoder specific data; the actual structure is defined
  57. /// differently in different coders.
  58. typedef struct lzma_coder_s lzma_coder;
  59. typedef struct lzma_next_coder_s lzma_next_coder;
  60. typedef struct lzma_filter_info_s lzma_filter_info;
  61. /// Type of a function used to initialize a filter encoder or decoder
  62. typedef lzma_ret (*lzma_init_function)(
  63. lzma_next_coder *next, lzma_allocator *allocator,
  64. const lzma_filter_info *filters);
  65. /// Type of a function to do some kind of coding work (filters, Stream,
  66. /// Block encoders/decoders etc.). Some special coders use don't use both
  67. /// input and output buffers, but for simplicity they still use this same
  68. /// function prototype.
  69. typedef lzma_ret (*lzma_code_function)(
  70. lzma_coder *coder, lzma_allocator *allocator,
  71. const uint8_t *LZMA_RESTRICT in, size_t *LZMA_RESTRICT in_pos,
  72. size_t in_size, uint8_t *LZMA_RESTRICT out,
  73. size_t *LZMA_RESTRICT out_pos, size_t out_size,
  74. lzma_action action);
  75. /// Type of a function to free the memory allocated for the coder
  76. typedef void (*lzma_end_function)(
  77. lzma_coder *coder, lzma_allocator *allocator);
  78. /// Raw coder validates and converts an array of lzma_filter structures to
  79. /// an array of lzma_filter_info structures. This array is used with
  80. /// lzma_next_filter_init to initialize the filter chain.
  81. struct lzma_filter_info_s {
  82. /// Filter ID. This is used only by the encoder
  83. /// with lzma_filters_update().
  84. lzma_vli id;
  85. /// Pointer to function used to initialize the filter.
  86. /// This is NULL to indicate end of array.
  87. lzma_init_function init;
  88. /// Pointer to filter's options structure
  89. void *options;
  90. };
  91. /// Hold data and function pointers of the next filter in the chain.
  92. struct lzma_next_coder_s {
  93. /// Pointer to coder-specific data
  94. lzma_coder *coder;
  95. /// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't
  96. /// point to a filter coder.
  97. lzma_vli id;
  98. /// "Pointer" to init function. This is never called here.
  99. /// We need only to detect if we are initializing a coder
  100. /// that was allocated earlier. See lzma_next_coder_init and
  101. /// lzma_next_strm_init macros in this file.
  102. uintptr_t init;
  103. /// Pointer to function to do the actual coding
  104. lzma_code_function code;
  105. /// Pointer to function to free lzma_next_coder.coder. This can
  106. /// be NULL; in that case, lzma_free is called to free
  107. /// lzma_next_coder.coder.
  108. lzma_end_function end;
  109. /// Pointer to function to return the type of the integrity check.
  110. /// Most coders won't support this.
  111. lzma_check (*get_check)(const lzma_coder *coder);
  112. /// Pointer to function to get and/or change the memory usage limit.
  113. /// If new_memlimit == 0, the limit is not changed.
  114. lzma_ret (*memconfig)(lzma_coder *coder, uint64_t *memusage,
  115. uint64_t *old_memlimit, uint64_t new_memlimit);
  116. /// Update the filter-specific options or the whole filter chain
  117. /// in the encoder.
  118. lzma_ret (*update)(lzma_coder *coder, lzma_allocator *allocator,
  119. const lzma_filter *filters,
  120. const lzma_filter *reversed_filters);
  121. };
  122. /// Constant to initialize lzma_next_coder structure
  123. static const lzma_next_coder LZMA_NEXT_CODER_INIT =
  124. {
  125. NULL,
  126. LZMA_VLI_UNKNOWN,
  127. (uintptr_t)(NULL),
  128. NULL,
  129. NULL,
  130. NULL,
  131. NULL,
  132. NULL,
  133. };
  134. /// Internal data for lzma_strm_init, lzma_code, and lzma_end. A pointer to
  135. /// this is stored in lzma_stream.
  136. struct lzma_internal_s {
  137. /// The actual coder that should do something useful
  138. lzma_next_coder next;
  139. /// Track the state of the coder. This is used to validate arguments
  140. /// so that the actual coders can rely on e.g. that LZMA_SYNC_FLUSH
  141. /// is used on every call to lzma_code until next.code has returned
  142. /// LZMA_STREAM_END.
  143. enum {
  144. ISEQ_RUN,
  145. ISEQ_SYNC_FLUSH,
  146. ISEQ_FULL_FLUSH,
  147. ISEQ_FINISH,
  148. ISEQ_END,
  149. ISEQ_ERROR,
  150. } sequence;
  151. /// A copy of lzma_stream avail_in. This is used to verify that the
  152. /// amount of input doesn't change once e.g. LZMA_FINISH has been
  153. /// used.
  154. size_t avail_in;
  155. /// Indicates which lzma_action values are allowed by next.code.
  156. bool supported_actions[4];
  157. /// If true, lzma_code will return LZMA_BUF_ERROR if no progress was
  158. /// made (no input consumed and no output produced by next.code).
  159. bool allow_buf_error;
  160. };
  161. /// Allocates memory
  162. extern void *lzma_alloc(size_t size, lzma_allocator *allocator)
  163. lzma_attribute((__malloc__)) lzma_attr_alloc_size(1);
  164. /// Frees memory
  165. extern void lzma_free(void *ptr, lzma_allocator *allocator);
  166. /// Allocates strm->internal if it is NULL, and initializes *strm and
  167. /// strm->internal. This function is only called via lzma_next_strm_init2 macro.
  168. extern lzma_ret lzma_strm_init(lzma_stream *strm);
  169. /// Initializes the next filter in the chain, if any. This takes care of
  170. /// freeing the memory of previously initialized filter if it is different
  171. /// than the filter being initialized now. This way the actual filter
  172. /// initialization functions don't need to use lzma_next_coder_init macro.
  173. extern lzma_ret lzma_next_filter_init(lzma_next_coder *next,
  174. lzma_allocator *allocator, const lzma_filter_info *filters);
  175. /// Update the next filter in the chain, if any. This checks that
  176. /// the application is not trying to change the Filter IDs.
  177. extern lzma_ret lzma_next_filter_update(
  178. lzma_next_coder *next, lzma_allocator *allocator,
  179. const lzma_filter *reversed_filters);
  180. /// Frees the memory allocated for next->coder either using next->end or,
  181. /// if next->end is NULL, using lzma_free.
  182. extern void lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator);
  183. /// Copy as much data as possible from in[] to out[] and update *in_pos
  184. /// and *out_pos accordingly. Returns the number of bytes copied.
  185. extern size_t lzma_bufcpy(const uint8_t *LZMA_RESTRICT in, size_t *LZMA_RESTRICT in_pos,
  186. size_t in_size, uint8_t *LZMA_RESTRICT out,
  187. size_t *LZMA_RESTRICT out_pos, size_t out_size);
  188. /// \brief Return if expression doesn't evaluate to LZMA_OK
  189. ///
  190. /// There are several situations where we want to return immediately
  191. /// with the value of expr if it isn't LZMA_OK. This macro shortens
  192. /// the code a little.
  193. #define return_if_error(expr) \
  194. do { \
  195. const lzma_ret ret_ = (expr); \
  196. if (ret_ != LZMA_OK) \
  197. return ret_; \
  198. } while (0)
  199. /// If next isn't already initialized, free the previous coder. Then mark
  200. /// that next is _possibly_ initialized for the coder using this macro.
  201. /// "Possibly" means that if e.g. allocation of next->coder fails, the
  202. /// structure isn't actually initialized for this coder, but leaving
  203. /// next->init to func is still OK.
  204. #define lzma_next_coder_init(func, next, allocator) \
  205. do { \
  206. if ((uintptr_t)(func) != (next)->init) \
  207. lzma_next_end(next, allocator); \
  208. (next)->init = (uintptr_t)(func); \
  209. } while (0)
  210. /// Initializes lzma_strm and calls func() to initialize strm->internal->next.
  211. /// (The function being called will use lzma_next_coder_init()). If
  212. /// initialization fails, memory that wasn't freed by func() is freed
  213. /// along strm->internal.
  214. #define lzma_next_strm_init1(func, strm, arg1) \
  215. do { \
  216. lzma_ret ret_; \
  217. return_if_error(lzma_strm_init(strm)); \
  218. ret_ = func(&(strm)->internal->next, (strm)->allocator, arg1); \
  219. if (ret_ != LZMA_OK) { \
  220. lzma_end(strm); \
  221. return ret_; \
  222. } \
  223. } while (0)
  224. #define lzma_next_strm_init2(func, strm, arg1, arg2) \
  225. do { \
  226. lzma_ret ret_; \
  227. return_if_error(lzma_strm_init(strm)); \
  228. ret_ = func(&(strm)->internal->next, (strm)->allocator, arg1, arg2); \
  229. if (ret_ != LZMA_OK) { \
  230. lzma_end(strm); \
  231. return ret_; \
  232. } \
  233. } while (0)
  234. #define lzma_next_strm_init3(func, strm, arg1, arg2, arg3) \
  235. do { \
  236. lzma_ret ret_; \
  237. return_if_error(lzma_strm_init(strm)); \
  238. ret_ = func(&(strm)->internal->next, (strm)->allocator, arg1, arg2, arg3); \
  239. if (ret_ != LZMA_OK) { \
  240. lzma_end(strm); \
  241. return ret_; \
  242. } \
  243. } while (0)
  244. #endif