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