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