xref: /freebsd/contrib/xz/src/liblzma/common/common.h (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
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 account 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 /// Supported flags that can be passed to lzma_stream_decoder()
64 /// or lzma_auto_decoder().
65 #define LZMA_SUPPORTED_FLAGS \
66 	( LZMA_TELL_NO_CHECK \
67 	| LZMA_TELL_UNSUPPORTED_CHECK \
68 	| LZMA_TELL_ANY_CHECK \
69 	| LZMA_CONCATENATED )
70 
71 
72 /// Type of encoder/decoder specific data; the actual structure is defined
73 /// differently in different coders.
74 typedef struct lzma_coder_s lzma_coder;
75 
76 typedef struct lzma_next_coder_s lzma_next_coder;
77 
78 typedef struct lzma_filter_info_s lzma_filter_info;
79 
80 
81 /// Type of a function used to initialize a filter encoder or decoder
82 typedef lzma_ret (*lzma_init_function)(
83 		lzma_next_coder *next, lzma_allocator *allocator,
84 		const lzma_filter_info *filters);
85 
86 /// Type of a function to do some kind of coding work (filters, Stream,
87 /// Block encoders/decoders etc.). Some special coders use don't use both
88 /// input and output buffers, but for simplicity they still use this same
89 /// function prototype.
90 typedef lzma_ret (*lzma_code_function)(
91 		lzma_coder *coder, lzma_allocator *allocator,
92 		const uint8_t *restrict in, size_t *restrict in_pos,
93 		size_t in_size, uint8_t *restrict out,
94 		size_t *restrict out_pos, size_t out_size,
95 		lzma_action action);
96 
97 /// Type of a function to free the memory allocated for the coder
98 typedef void (*lzma_end_function)(
99 		lzma_coder *coder, lzma_allocator *allocator);
100 
101 
102 /// Raw coder validates and converts an array of lzma_filter structures to
103 /// an array of lzma_filter_info structures. This array is used with
104 /// lzma_next_filter_init to initialize the filter chain.
105 struct lzma_filter_info_s {
106 	/// Filter ID. This is used only by the encoder
107 	/// with lzma_filters_update().
108 	lzma_vli id;
109 
110 	/// Pointer to function used to initialize the filter.
111 	/// This is NULL to indicate end of array.
112 	lzma_init_function init;
113 
114 	/// Pointer to filter's options structure
115 	void *options;
116 };
117 
118 
119 /// Hold data and function pointers of the next filter in the chain.
120 struct lzma_next_coder_s {
121 	/// Pointer to coder-specific data
122 	lzma_coder *coder;
123 
124 	/// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't
125 	/// point to a filter coder.
126 	lzma_vli id;
127 
128 	/// "Pointer" to init function. This is never called here.
129 	/// We need only to detect if we are initializing a coder
130 	/// that was allocated earlier. See lzma_next_coder_init and
131 	/// lzma_next_strm_init macros in this file.
132 	uintptr_t init;
133 
134 	/// Pointer to function to do the actual coding
135 	lzma_code_function code;
136 
137 	/// Pointer to function to free lzma_next_coder.coder. This can
138 	/// be NULL; in that case, lzma_free is called to free
139 	/// lzma_next_coder.coder.
140 	lzma_end_function end;
141 
142 	/// Pointer to function to return the type of the integrity check.
143 	/// Most coders won't support this.
144 	lzma_check (*get_check)(const lzma_coder *coder);
145 
146 	/// Pointer to function to get and/or change the memory usage limit.
147 	/// If new_memlimit == 0, the limit is not changed.
148 	lzma_ret (*memconfig)(lzma_coder *coder, uint64_t *memusage,
149 			uint64_t *old_memlimit, uint64_t new_memlimit);
150 
151 	/// Update the filter-specific options or the whole filter chain
152 	/// in the encoder.
153 	lzma_ret (*update)(lzma_coder *coder, lzma_allocator *allocator,
154 			const lzma_filter *filters,
155 			const lzma_filter *reversed_filters);
156 };
157 
158 
159 /// Macro to initialize lzma_next_coder structure
160 #define LZMA_NEXT_CODER_INIT \
161 	(lzma_next_coder){ \
162 		.coder = NULL, \
163 		.init = (uintptr_t)(NULL), \
164 		.id = LZMA_VLI_UNKNOWN, \
165 		.code = NULL, \
166 		.end = NULL, \
167 		.get_check = NULL, \
168 		.memconfig = NULL, \
169 		.update = NULL, \
170 	}
171 
172 
173 /// Internal data for lzma_strm_init, lzma_code, and lzma_end. A pointer to
174 /// this is stored in lzma_stream.
175 struct lzma_internal_s {
176 	/// The actual coder that should do something useful
177 	lzma_next_coder next;
178 
179 	/// Track the state of the coder. This is used to validate arguments
180 	/// so that the actual coders can rely on e.g. that LZMA_SYNC_FLUSH
181 	/// is used on every call to lzma_code until next.code has returned
182 	/// LZMA_STREAM_END.
183 	enum {
184 		ISEQ_RUN,
185 		ISEQ_SYNC_FLUSH,
186 		ISEQ_FULL_FLUSH,
187 		ISEQ_FINISH,
188 		ISEQ_END,
189 		ISEQ_ERROR,
190 	} sequence;
191 
192 	/// A copy of lzma_stream avail_in. This is used to verify that the
193 	/// amount of input doesn't change once e.g. LZMA_FINISH has been
194 	/// used.
195 	size_t avail_in;
196 
197 	/// Indicates which lzma_action values are allowed by next.code.
198 	bool supported_actions[4];
199 
200 	/// If true, lzma_code will return LZMA_BUF_ERROR if no progress was
201 	/// made (no input consumed and no output produced by next.code).
202 	bool allow_buf_error;
203 };
204 
205 
206 /// Allocates memory
207 extern void *lzma_alloc(size_t size, lzma_allocator *allocator)
208 		lzma_attribute((__malloc__)) lzma_attr_alloc_size(1);
209 
210 /// Frees memory
211 extern void lzma_free(void *ptr, lzma_allocator *allocator);
212 
213 
214 /// Allocates strm->internal if it is NULL, and initializes *strm and
215 /// strm->internal. This function is only called via lzma_next_strm_init macro.
216 extern lzma_ret lzma_strm_init(lzma_stream *strm);
217 
218 /// Initializes the next filter in the chain, if any. This takes care of
219 /// freeing the memory of previously initialized filter if it is different
220 /// than the filter being initialized now. This way the actual filter
221 /// initialization functions don't need to use lzma_next_coder_init macro.
222 extern lzma_ret lzma_next_filter_init(lzma_next_coder *next,
223 		lzma_allocator *allocator, const lzma_filter_info *filters);
224 
225 /// Update the next filter in the chain, if any. This checks that
226 /// the application is not trying to change the Filter IDs.
227 extern lzma_ret lzma_next_filter_update(
228 		lzma_next_coder *next, lzma_allocator *allocator,
229 		const lzma_filter *reversed_filters);
230 
231 /// Frees the memory allocated for next->coder either using next->end or,
232 /// if next->end is NULL, using lzma_free.
233 extern void lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator);
234 
235 
236 /// Copy as much data as possible from in[] to out[] and update *in_pos
237 /// and *out_pos accordingly. Returns the number of bytes copied.
238 extern size_t lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
239 		size_t in_size, uint8_t *restrict out,
240 		size_t *restrict out_pos, size_t out_size);
241 
242 
243 /// \brief      Return if expression doesn't evaluate to LZMA_OK
244 ///
245 /// There are several situations where we want to return immediately
246 /// with the value of expr if it isn't LZMA_OK. This macro shortens
247 /// the code a little.
248 #define return_if_error(expr) \
249 do { \
250 	const lzma_ret ret_ = (expr); \
251 	if (ret_ != LZMA_OK) \
252 		return ret_; \
253 } while (0)
254 
255 
256 /// If next isn't already initialized, free the previous coder. Then mark
257 /// that next is _possibly_ initialized for the coder using this macro.
258 /// "Possibly" means that if e.g. allocation of next->coder fails, the
259 /// structure isn't actually initialized for this coder, but leaving
260 /// next->init to func is still OK.
261 #define lzma_next_coder_init(func, next, allocator) \
262 do { \
263 	if ((uintptr_t)(func) != (next)->init) \
264 		lzma_next_end(next, allocator); \
265 	(next)->init = (uintptr_t)(func); \
266 } while (0)
267 
268 
269 /// Initializes lzma_strm and calls func() to initialize strm->internal->next.
270 /// (The function being called will use lzma_next_coder_init()). If
271 /// initialization fails, memory that wasn't freed by func() is freed
272 /// along strm->internal.
273 #define lzma_next_strm_init(func, strm, ...) \
274 do { \
275 	return_if_error(lzma_strm_init(strm)); \
276 	const lzma_ret ret_ = func(&(strm)->internal->next, \
277 			(strm)->allocator, __VA_ARGS__); \
278 	if (ret_ != LZMA_OK) { \
279 		lzma_end(strm); \
280 		return ret_; \
281 	} \
282 } while (0)
283 
284 #endif
285