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