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