xref: /freebsd/contrib/xz/src/liblzma/common/common.h (revision c7a063741720ef81d4caa4613242579d12f1d605)
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 // The extra symbol versioning in the C files may only be used when
38 // building a shared library. If HAVE_SYMBOL_VERSIONS_LINUX is defined
39 // to 2 then symbol versioning is done only if also PIC is defined.
40 // By default Libtool defines PIC when building a shared library and
41 // doesn't define it when building a static library but it can be
42 // overriden with --with-pic and --without-pic. configure let's rely
43 // on PIC if neither --with-pic or --without-pic was used.
44 #if defined(HAVE_SYMBOL_VERSIONS_LINUX) \
45 		&& (HAVE_SYMBOL_VERSIONS_LINUX == 2 && !defined(PIC))
46 #	undef HAVE_SYMBOL_VERSIONS_LINUX
47 #endif
48 
49 #ifdef HAVE_SYMBOL_VERSIONS_LINUX
50 // To keep link-time optimization (LTO, -flto) working with GCC,
51 // the __symver__ attribute must be used instead of __asm__(".symver ...").
52 // Otherwise the symbol versions may be lost, resulting in broken liblzma
53 // that has wrong default versions in the exported symbol list!
54 // The attribute was added in GCC 10; LTO with older GCC is not supported.
55 //
56 // To keep -Wmissing-prototypes happy, use LZMA_SYMVER_API only with function
57 // declarations (including those with __alias__ attribute) and LZMA_API with
58 // the function definitions. This means a little bit of silly copy-and-paste
59 // between declarations and definitions though.
60 //
61 // As of GCC 12.2, the __symver__ attribute supports only @ and @@ but the
62 // very convenient @@@ isn't supported (it's supported by GNU assembler
63 // since 2000). When using @@ instead of @@@, the internal name must not be
64 // the same as the external name to avoid problems in some situations. This
65 // is why "#define foo_52 foo" is needed for the default symbol versions.
66 #	if TUKLIB_GNUC_REQ(10, 0) && !defined(__INTEL_COMPILER)
67 #		define LZMA_SYMVER_API(extnamever, type, intname) \
68 			extern __attribute__((__symver__(extnamever))) \
69 					LZMA_API(type) intname
70 #	else
71 #		define LZMA_SYMVER_API(extnamever, type, intname) \
72 			__asm__(".symver " #intname "," extnamever); \
73 			extern LZMA_API(type) intname
74 #	endif
75 #endif
76 
77 // These allow helping the compiler in some often-executed branches, whose
78 // result is almost always the same.
79 #ifdef __GNUC__
80 #	define likely(expr) __builtin_expect(expr, true)
81 #	define unlikely(expr) __builtin_expect(expr, false)
82 #else
83 #	define likely(expr) (expr)
84 #	define unlikely(expr) (expr)
85 #endif
86 
87 
88 /// Size of temporary buffers needed in some filters
89 #define LZMA_BUFFER_SIZE 4096
90 
91 
92 /// Maximum number of worker threads within one multithreaded component.
93 /// The limit exists solely to make it simpler to prevent integer overflows
94 /// when allocating structures etc. This should be big enough for now...
95 /// the code won't scale anywhere close to this number anyway.
96 #define LZMA_THREADS_MAX 16384
97 
98 
99 /// Starting value for memory usage estimates. Instead of calculating size
100 /// of _every_ structure and taking into account malloc() overhead etc., we
101 /// add a base size to all memory usage estimates. It's not very accurate
102 /// but should be easily good enough.
103 #define LZMA_MEMUSAGE_BASE (UINT64_C(1) << 15)
104 
105 /// Start of internal Filter ID space. These IDs must never be used
106 /// in Streams.
107 #define LZMA_FILTER_RESERVED_START (LZMA_VLI_C(1) << 62)
108 
109 
110 /// Supported flags that can be passed to lzma_stream_decoder()
111 /// or lzma_auto_decoder().
112 #define LZMA_SUPPORTED_FLAGS \
113 	( LZMA_TELL_NO_CHECK \
114 	| LZMA_TELL_UNSUPPORTED_CHECK \
115 	| LZMA_TELL_ANY_CHECK \
116 	| LZMA_IGNORE_CHECK \
117 	| LZMA_CONCATENATED )
118 
119 
120 /// Largest valid lzma_action value as unsigned integer.
121 #define LZMA_ACTION_MAX ((unsigned int)(LZMA_FULL_BARRIER))
122 
123 
124 /// Special return value (lzma_ret) to indicate that a timeout was reached
125 /// and lzma_code() must not return LZMA_BUF_ERROR. This is converted to
126 /// LZMA_OK in lzma_code(). This is not in the lzma_ret enumeration because
127 /// there's no need to have it in the public API.
128 #define LZMA_TIMED_OUT 32
129 
130 
131 typedef struct lzma_next_coder_s lzma_next_coder;
132 
133 typedef struct lzma_filter_info_s lzma_filter_info;
134 
135 
136 /// Type of a function used to initialize a filter encoder or decoder
137 typedef lzma_ret (*lzma_init_function)(
138 		lzma_next_coder *next, const lzma_allocator *allocator,
139 		const lzma_filter_info *filters);
140 
141 /// Type of a function to do some kind of coding work (filters, Stream,
142 /// Block encoders/decoders etc.). Some special coders use don't use both
143 /// input and output buffers, but for simplicity they still use this same
144 /// function prototype.
145 typedef lzma_ret (*lzma_code_function)(
146 		void *coder, const lzma_allocator *allocator,
147 		const uint8_t *restrict in, size_t *restrict in_pos,
148 		size_t in_size, uint8_t *restrict out,
149 		size_t *restrict out_pos, size_t out_size,
150 		lzma_action action);
151 
152 /// Type of a function to free the memory allocated for the coder
153 typedef void (*lzma_end_function)(
154 		void *coder, const lzma_allocator *allocator);
155 
156 
157 /// Raw coder validates and converts an array of lzma_filter structures to
158 /// an array of lzma_filter_info structures. This array is used with
159 /// lzma_next_filter_init to initialize the filter chain.
160 struct lzma_filter_info_s {
161 	/// Filter ID. This is used only by the encoder
162 	/// with lzma_filters_update().
163 	lzma_vli id;
164 
165 	/// Pointer to function used to initialize the filter.
166 	/// This is NULL to indicate end of array.
167 	lzma_init_function init;
168 
169 	/// Pointer to filter's options structure
170 	void *options;
171 };
172 
173 
174 /// Hold data and function pointers of the next filter in the chain.
175 struct lzma_next_coder_s {
176 	/// Pointer to coder-specific data
177 	void *coder;
178 
179 	/// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't
180 	/// point to a filter coder.
181 	lzma_vli id;
182 
183 	/// "Pointer" to init function. This is never called here.
184 	/// We need only to detect if we are initializing a coder
185 	/// that was allocated earlier. See lzma_next_coder_init and
186 	/// lzma_next_strm_init macros in this file.
187 	uintptr_t init;
188 
189 	/// Pointer to function to do the actual coding
190 	lzma_code_function code;
191 
192 	/// Pointer to function to free lzma_next_coder.coder. This can
193 	/// be NULL; in that case, lzma_free is called to free
194 	/// lzma_next_coder.coder.
195 	lzma_end_function end;
196 
197 	/// Pointer to a function to get progress information. If this is NULL,
198 	/// lzma_stream.total_in and .total_out are used instead.
199 	void (*get_progress)(void *coder,
200 			uint64_t *progress_in, uint64_t *progress_out);
201 
202 	/// Pointer to function to return the type of the integrity check.
203 	/// Most coders won't support this.
204 	lzma_check (*get_check)(const void *coder);
205 
206 	/// Pointer to function to get and/or change the memory usage limit.
207 	/// If new_memlimit == 0, the limit is not changed.
208 	lzma_ret (*memconfig)(void *coder, uint64_t *memusage,
209 			uint64_t *old_memlimit, uint64_t new_memlimit);
210 
211 	/// Update the filter-specific options or the whole filter chain
212 	/// in the encoder.
213 	lzma_ret (*update)(void *coder, const lzma_allocator *allocator,
214 			const lzma_filter *filters,
215 			const lzma_filter *reversed_filters);
216 };
217 
218 
219 /// Macro to initialize lzma_next_coder structure
220 #define LZMA_NEXT_CODER_INIT \
221 	(lzma_next_coder){ \
222 		.coder = NULL, \
223 		.init = (uintptr_t)(NULL), \
224 		.id = LZMA_VLI_UNKNOWN, \
225 		.code = NULL, \
226 		.end = NULL, \
227 		.get_progress = NULL, \
228 		.get_check = NULL, \
229 		.memconfig = NULL, \
230 		.update = NULL, \
231 	}
232 
233 
234 /// Internal data for lzma_strm_init, lzma_code, and lzma_end. A pointer to
235 /// this is stored in lzma_stream.
236 struct lzma_internal_s {
237 	/// The actual coder that should do something useful
238 	lzma_next_coder next;
239 
240 	/// Track the state of the coder. This is used to validate arguments
241 	/// so that the actual coders can rely on e.g. that LZMA_SYNC_FLUSH
242 	/// is used on every call to lzma_code until next.code has returned
243 	/// LZMA_STREAM_END.
244 	enum {
245 		ISEQ_RUN,
246 		ISEQ_SYNC_FLUSH,
247 		ISEQ_FULL_FLUSH,
248 		ISEQ_FINISH,
249 		ISEQ_FULL_BARRIER,
250 		ISEQ_END,
251 		ISEQ_ERROR,
252 	} sequence;
253 
254 	/// A copy of lzma_stream avail_in. This is used to verify that the
255 	/// amount of input doesn't change once e.g. LZMA_FINISH has been
256 	/// used.
257 	size_t avail_in;
258 
259 	/// Indicates which lzma_action values are allowed by next.code.
260 	bool supported_actions[LZMA_ACTION_MAX + 1];
261 
262 	/// If true, lzma_code will return LZMA_BUF_ERROR if no progress was
263 	/// made (no input consumed and no output produced by next.code).
264 	bool allow_buf_error;
265 };
266 
267 
268 /// Allocates memory
269 extern void *lzma_alloc(size_t size, const lzma_allocator *allocator)
270 		lzma_attribute((__malloc__)) lzma_attr_alloc_size(1);
271 
272 /// Allocates memory and zeroes it (like calloc()). This can be faster
273 /// than lzma_alloc() + memzero() while being backward compatible with
274 /// custom allocators.
275 extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
276 		lzma_alloc_zero(size_t size, const lzma_allocator *allocator);
277 
278 /// Frees memory
279 extern void lzma_free(void *ptr, const lzma_allocator *allocator);
280 
281 
282 /// Allocates strm->internal if it is NULL, and initializes *strm and
283 /// strm->internal. This function is only called via lzma_next_strm_init macro.
284 extern lzma_ret lzma_strm_init(lzma_stream *strm);
285 
286 /// Initializes the next filter in the chain, if any. This takes care of
287 /// freeing the memory of previously initialized filter if it is different
288 /// than the filter being initialized now. This way the actual filter
289 /// initialization functions don't need to use lzma_next_coder_init macro.
290 extern lzma_ret lzma_next_filter_init(lzma_next_coder *next,
291 		const lzma_allocator *allocator,
292 		const lzma_filter_info *filters);
293 
294 /// Update the next filter in the chain, if any. This checks that
295 /// the application is not trying to change the Filter IDs.
296 extern lzma_ret lzma_next_filter_update(
297 		lzma_next_coder *next, const lzma_allocator *allocator,
298 		const lzma_filter *reversed_filters);
299 
300 /// Frees the memory allocated for next->coder either using next->end or,
301 /// if next->end is NULL, using lzma_free.
302 extern void lzma_next_end(lzma_next_coder *next,
303 		const lzma_allocator *allocator);
304 
305 
306 /// Copy as much data as possible from in[] to out[] and update *in_pos
307 /// and *out_pos accordingly. Returns the number of bytes copied.
308 extern size_t lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
309 		size_t in_size, uint8_t *restrict out,
310 		size_t *restrict out_pos, size_t out_size);
311 
312 
313 /// \brief      Return if expression doesn't evaluate to LZMA_OK
314 ///
315 /// There are several situations where we want to return immediately
316 /// with the value of expr if it isn't LZMA_OK. This macro shortens
317 /// the code a little.
318 #define return_if_error(expr) \
319 do { \
320 	const lzma_ret ret_ = (expr); \
321 	if (ret_ != LZMA_OK) \
322 		return ret_; \
323 } while (0)
324 
325 
326 /// If next isn't already initialized, free the previous coder. Then mark
327 /// that next is _possibly_ initialized for the coder using this macro.
328 /// "Possibly" means that if e.g. allocation of next->coder fails, the
329 /// structure isn't actually initialized for this coder, but leaving
330 /// next->init to func is still OK.
331 #define lzma_next_coder_init(func, next, allocator) \
332 do { \
333 	if ((uintptr_t)(func) != (next)->init) \
334 		lzma_next_end(next, allocator); \
335 	(next)->init = (uintptr_t)(func); \
336 } while (0)
337 
338 
339 /// Initializes lzma_strm and calls func() to initialize strm->internal->next.
340 /// (The function being called will use lzma_next_coder_init()). If
341 /// initialization fails, memory that wasn't freed by func() is freed
342 /// along strm->internal.
343 #define lzma_next_strm_init(func, strm, ...) \
344 do { \
345 	return_if_error(lzma_strm_init(strm)); \
346 	const lzma_ret ret_ = func(&(strm)->internal->next, \
347 			(strm)->allocator, __VA_ARGS__); \
348 	if (ret_ != LZMA_OK) { \
349 		lzma_end(strm); \
350 		return ret_; \
351 	} \
352 } while (0)
353 
354 #endif
355