1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Definitions common to the whole liblzma library
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
18 #include "tuklib_integer.h"
20 #if defined(_WIN32) || defined(__CYGWIN__)
22 # define LZMA_API_EXPORT __declspec(dllexport)
24 # define LZMA_API_EXPORT
26 // Don't use ifdef or defined() below.
28 # define LZMA_API_EXPORT __attribute__((__visibility__("default")))
30 # define LZMA_API_EXPORT
33 #define LZMA_API(type) LZMA_API_EXPORT type LZMA_API_CALL
37 // These allow helping the compiler in some often-executed branches, whose
38 // result is almost always the same.
40 # define likely(expr) __builtin_expect(expr, true)
41 # define unlikely(expr) __builtin_expect(expr, false)
43 # define likely(expr) (expr)
44 # define unlikely(expr) (expr)
48 /// Size of temporary buffers needed in some filters
49 #define LZMA_BUFFER_SIZE 4096
52 /// Maximum number of worker threads within one multithreaded component.
53 /// The limit exists solely to make it simpler to prevent integer overflows
54 /// when allocating structures etc. This should be big enough for now...
55 /// the code won't scale anywhere close to this number anyway.
56 #define LZMA_THREADS_MAX 16384
59 /// Starting value for memory usage estimates. Instead of calculating size
60 /// of _every_ structure and taking into account malloc() overhead etc., we
61 /// add a base size to all memory usage estimates. It's not very accurate
62 /// but should be easily good enough.
63 #define LZMA_MEMUSAGE_BASE (UINT64_C(1) << 15)
65 /// Start of internal Filter ID space. These IDs must never be used
67 #define LZMA_FILTER_RESERVED_START (LZMA_VLI_C(1) << 62)
70 /// Supported flags that can be passed to lzma_stream_decoder()
71 /// or lzma_auto_decoder().
72 #define LZMA_SUPPORTED_FLAGS \
73 ( LZMA_TELL_NO_CHECK \
74 | LZMA_TELL_UNSUPPORTED_CHECK \
75 | LZMA_TELL_ANY_CHECK \
80 /// Largest valid lzma_action value as unsigned integer.
81 #define LZMA_ACTION_MAX ((unsigned int)(LZMA_FULL_BARRIER))
84 /// Special return value (lzma_ret) to indicate that a timeout was reached
85 /// and lzma_code() must not return LZMA_BUF_ERROR. This is converted to
86 /// LZMA_OK in lzma_code(). This is not in the lzma_ret enumeration because
87 /// there's no need to have it in the public API.
88 #define LZMA_TIMED_OUT 32
91 typedef struct lzma_next_coder_s lzma_next_coder;
93 typedef struct lzma_filter_info_s lzma_filter_info;
96 /// Type of a function used to initialize a filter encoder or decoder
97 typedef lzma_ret (*lzma_init_function)(
98 lzma_next_coder *next, const lzma_allocator *allocator,
99 const lzma_filter_info *filters);
101 /// Type of a function to do some kind of coding work (filters, Stream,
102 /// Block encoders/decoders etc.). Some special coders use don't use both
103 /// input and output buffers, but for simplicity they still use this same
104 /// function prototype.
105 typedef lzma_ret (*lzma_code_function)(
106 void *coder, const lzma_allocator *allocator,
107 const uint8_t *restrict in, size_t *restrict in_pos,
108 size_t in_size, uint8_t *restrict out,
109 size_t *restrict out_pos, size_t out_size,
112 /// Type of a function to free the memory allocated for the coder
113 typedef void (*lzma_end_function)(
114 void *coder, const lzma_allocator *allocator);
117 /// Raw coder validates and converts an array of lzma_filter structures to
118 /// an array of lzma_filter_info structures. This array is used with
119 /// lzma_next_filter_init to initialize the filter chain.
120 struct lzma_filter_info_s {
121 /// Filter ID. This is used only by the encoder
122 /// with lzma_filters_update().
125 /// Pointer to function used to initialize the filter.
126 /// This is NULL to indicate end of array.
127 lzma_init_function init;
129 /// Pointer to filter's options structure
134 /// Hold data and function pointers of the next filter in the chain.
135 struct lzma_next_coder_s {
136 /// Pointer to coder-specific data
139 /// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't
140 /// point to a filter coder.
143 /// "Pointer" to init function. This is never called here.
144 /// We need only to detect if we are initializing a coder
145 /// that was allocated earlier. See lzma_next_coder_init and
146 /// lzma_next_strm_init macros in this file.
149 /// Pointer to function to do the actual coding
150 lzma_code_function code;
152 /// Pointer to function to free lzma_next_coder.coder. This can
153 /// be NULL; in that case, lzma_free is called to free
154 /// lzma_next_coder.coder.
155 lzma_end_function end;
157 /// Pointer to a function to get progress information. If this is NULL,
158 /// lzma_stream.total_in and .total_out are used instead.
159 void (*get_progress)(void *coder,
160 uint64_t *progress_in, uint64_t *progress_out);
162 /// Pointer to function to return the type of the integrity check.
163 /// Most coders won't support this.
164 lzma_check (*get_check)(const void *coder);
166 /// Pointer to function to get and/or change the memory usage limit.
167 /// If new_memlimit == 0, the limit is not changed.
168 lzma_ret (*memconfig)(void *coder, uint64_t *memusage,
169 uint64_t *old_memlimit, uint64_t new_memlimit);
171 /// Update the filter-specific options or the whole filter chain
173 lzma_ret (*update)(void *coder, const lzma_allocator *allocator,
174 const lzma_filter *filters,
175 const lzma_filter *reversed_filters);
179 /// Macro to initialize lzma_next_coder structure
180 #define LZMA_NEXT_CODER_INIT \
183 .init = (uintptr_t)(NULL), \
184 .id = LZMA_VLI_UNKNOWN, \
187 .get_progress = NULL, \
194 /// Internal data for lzma_strm_init, lzma_code, and lzma_end. A pointer to
195 /// this is stored in lzma_stream.
196 struct lzma_internal_s {
197 /// The actual coder that should do something useful
198 lzma_next_coder next;
200 /// Track the state of the coder. This is used to validate arguments
201 /// so that the actual coders can rely on e.g. that LZMA_SYNC_FLUSH
202 /// is used on every call to lzma_code until next.code has returned
214 /// A copy of lzma_stream avail_in. This is used to verify that the
215 /// amount of input doesn't change once e.g. LZMA_FINISH has been
219 /// Indicates which lzma_action values are allowed by next.code.
220 bool supported_actions[LZMA_ACTION_MAX + 1];
222 /// If true, lzma_code will return LZMA_BUF_ERROR if no progress was
223 /// made (no input consumed and no output produced by next.code).
224 bool allow_buf_error;
229 extern void *lzma_alloc(size_t size, const lzma_allocator *allocator)
230 lzma_attribute((__malloc__)) lzma_attr_alloc_size(1);
232 /// Allocates memory and zeroes it (like calloc()). This can be faster
233 /// than lzma_alloc() + memzero() while being backward compatible with
234 /// custom allocators.
235 extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
236 lzma_alloc_zero(size_t size, const lzma_allocator *allocator);
239 extern void lzma_free(void *ptr, const lzma_allocator *allocator);
242 /// Allocates strm->internal if it is NULL, and initializes *strm and
243 /// strm->internal. This function is only called via lzma_next_strm_init macro.
244 extern lzma_ret lzma_strm_init(lzma_stream *strm);
246 /// Initializes the next filter in the chain, if any. This takes care of
247 /// freeing the memory of previously initialized filter if it is different
248 /// than the filter being initialized now. This way the actual filter
249 /// initialization functions don't need to use lzma_next_coder_init macro.
250 extern lzma_ret lzma_next_filter_init(lzma_next_coder *next,
251 const lzma_allocator *allocator,
252 const lzma_filter_info *filters);
254 /// Update the next filter in the chain, if any. This checks that
255 /// the application is not trying to change the Filter IDs.
256 extern lzma_ret lzma_next_filter_update(
257 lzma_next_coder *next, const lzma_allocator *allocator,
258 const lzma_filter *reversed_filters);
260 /// Frees the memory allocated for next->coder either using next->end or,
261 /// if next->end is NULL, using lzma_free.
262 extern void lzma_next_end(lzma_next_coder *next,
263 const lzma_allocator *allocator);
266 /// Copy as much data as possible from in[] to out[] and update *in_pos
267 /// and *out_pos accordingly. Returns the number of bytes copied.
268 extern size_t lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
269 size_t in_size, uint8_t *restrict out,
270 size_t *restrict out_pos, size_t out_size);
273 /// \brief Return if expression doesn't evaluate to LZMA_OK
275 /// There are several situations where we want to return immediately
276 /// with the value of expr if it isn't LZMA_OK. This macro shortens
277 /// the code a little.
278 #define return_if_error(expr) \
280 const lzma_ret ret_ = (expr); \
281 if (ret_ != LZMA_OK) \
286 /// If next isn't already initialized, free the previous coder. Then mark
287 /// that next is _possibly_ initialized for the coder using this macro.
288 /// "Possibly" means that if e.g. allocation of next->coder fails, the
289 /// structure isn't actually initialized for this coder, but leaving
290 /// next->init to func is still OK.
291 #define lzma_next_coder_init(func, next, allocator) \
293 if ((uintptr_t)(func) != (next)->init) \
294 lzma_next_end(next, allocator); \
295 (next)->init = (uintptr_t)(func); \
299 /// Initializes lzma_strm and calls func() to initialize strm->internal->next.
300 /// (The function being called will use lzma_next_coder_init()). If
301 /// initialization fails, memory that wasn't freed by func() is freed
302 /// along strm->internal.
303 #define lzma_next_strm_init(func, strm, ...) \
305 return_if_error(lzma_strm_init(strm)); \
306 const lzma_ret ret_ = func(&(strm)->internal->next, \
307 (strm)->allocator, __VA_ARGS__); \
308 if (ret_ != LZMA_OK) { \