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