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 "tuklib_integer.h"
18 
19 #if defined(_WIN32) || defined(__CYGWIN__)
20 #	ifdef DLL_EXPORT
21 #		define LZMA_API_EXPORT __declspec(dllexport)
22 #	else
23 #		define LZMA_API_EXPORT
24 #	endif
25 // Don't use ifdef or defined() below.
26 #elif HAVE_VISIBILITY
27 #	define LZMA_API_EXPORT __attribute__((__visibility__("default")))
28 #else
29 #	define LZMA_API_EXPORT
30 #endif
31 
32 #define LZMA_API(type) LZMA_API_EXPORT type LZMA_API_CALL
33 
34 #include "lzma.h"
35 
36 // These allow helping the compiler in some often-executed branches, whose
37 // result is almost always the same.
38 #ifdef __GNUC__
39 #	define likely(expr) __builtin_expect(expr, true)
40 #	define unlikely(expr) __builtin_expect(expr, false)
41 #else
42 #	define likely(expr) (expr)
43 #	define unlikely(expr) (expr)
44 #endif
45 
46 
47 /// Size of temporary buffers needed in some filters
48 #define LZMA_BUFFER_SIZE 4096
49 
50 
51 /// Maximum number of worker threads within one multithreaded component.
52 /// The limit exists solely to make it simpler to prevent integer overflows
53 /// when allocating structures etc. This should be big enough for now...
54 /// the code won't scale anywhere close to this number anyway.
55 #define LZMA_THREADS_MAX 16384
56 
57 
58 /// Starting value for memory usage estimates. Instead of calculating size
59 /// of _every_ structure and taking into account malloc() overhead etc., we
60 /// add a base size to all memory usage estimates. It's not very accurate
61 /// but should be easily good enough.
62 #define LZMA_MEMUSAGE_BASE (UINT64_C(1) << 15)
63 
64 /// Start of internal Filter ID space. These IDs must never be used
65 /// in Streams.
66 #define LZMA_FILTER_RESERVED_START (LZMA_VLI_C(1) << 62)
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_IGNORE_CHECK \
76 	| LZMA_CONCATENATED )
77 
78 
79 /// Largest valid lzma_action value as unsigned integer.
80 #define LZMA_ACTION_MAX ((unsigned int)(LZMA_FULL_BARRIER))
81 
82 
83 /// Special return value (lzma_ret) to indicate that a timeout was reached
84 /// and lzma_code() must not return LZMA_BUF_ERROR. This is converted to
85 /// LZMA_OK in lzma_code(). This is not in the lzma_ret enumeration because
86 /// there's no need to have it in the public API.
87 #define LZMA_TIMED_OUT 32
88 
89 
90 typedef struct lzma_next_coder_s lzma_next_coder;
91 
92 typedef struct lzma_filter_info_s lzma_filter_info;
93 
94 
95 /// Type of a function used to initialize a filter encoder or decoder
96 typedef lzma_ret (*lzma_init_function)(
97 		lzma_next_coder *next, const lzma_allocator *allocator,
98 		const lzma_filter_info *filters);
99 
100 /// Type of a function to do some kind of coding work (filters, Stream,
101 /// Block encoders/decoders etc.). Some special coders use don't use both
102 /// input and output buffers, but for simplicity they still use this same
103 /// function prototype.
104 typedef lzma_ret (*lzma_code_function)(
105 		void *coder, const lzma_allocator *allocator,
106 		const uint8_t *restrict in, size_t *restrict in_pos,
107 		size_t in_size, uint8_t *restrict out,
108 		size_t *restrict out_pos, size_t out_size,
109 		lzma_action action);
110 
111 /// Type of a function to free the memory allocated for the coder
112 typedef void (*lzma_end_function)(
113 		void *coder, const lzma_allocator *allocator);
114 
115 
116 /// Raw coder validates and converts an array of lzma_filter structures to
117 /// an array of lzma_filter_info structures. This array is used with
118 /// lzma_next_filter_init to initialize the filter chain.
119 struct lzma_filter_info_s {
120 	/// Filter ID. This is used only by the encoder
121 	/// with lzma_filters_update().
122 	lzma_vli id;
123 
124 	/// Pointer to function used to initialize the filter.
125 	/// This is NULL to indicate end of array.
126 	lzma_init_function init;
127 
128 	/// Pointer to filter's options structure
129 	void *options;
130 };
131 
132 
133 /// Hold data and function pointers of the next filter in the chain.
134 struct lzma_next_coder_s {
135 	/// Pointer to coder-specific data
136 	void *coder;
137 
138 	/// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't
139 	/// point to a filter coder.
140 	lzma_vli id;
141 
142 	/// "Pointer" to init function. This is never called here.
143 	/// We need only to detect if we are initializing a coder
144 	/// that was allocated earlier. See lzma_next_coder_init and
145 	/// lzma_next_strm_init macros in this file.
146 	uintptr_t init;
147 
148 	/// Pointer to function to do the actual coding
149 	lzma_code_function code;
150 
151 	/// Pointer to function to free lzma_next_coder.coder. This can
152 	/// be NULL; in that case, lzma_free is called to free
153 	/// lzma_next_coder.coder.
154 	lzma_end_function end;
155 
156 	/// Pointer to a function to get progress information. If this is NULL,
157 	/// lzma_stream.total_in and .total_out are used instead.
158 	void (*get_progress)(void *coder,
159 			uint64_t *progress_in, uint64_t *progress_out);
160 
161 	/// Pointer to function to return the type of the integrity check.
162 	/// Most coders won't support this.
163 	lzma_check (*get_check)(const void *coder);
164 
165 	/// Pointer to function to get and/or change the memory usage limit.
166 	/// If new_memlimit == 0, the limit is not changed.
167 	lzma_ret (*memconfig)(void *coder, uint64_t *memusage,
168 			uint64_t *old_memlimit, uint64_t new_memlimit);
169 
170 	/// Update the filter-specific options or the whole filter chain
171 	/// in the encoder.
172 	lzma_ret (*update)(void *coder, const lzma_allocator *allocator,
173 			const lzma_filter *filters,
174 			const lzma_filter *reversed_filters);
175 };
176 
177 
178 /// Macro to initialize lzma_next_coder structure
179 #define LZMA_NEXT_CODER_INIT \
180 	(lzma_next_coder){ \
181 		.coder = NULL, \
182 		.init = (uintptr_t)(NULL), \
183 		.id = LZMA_VLI_UNKNOWN, \
184 		.code = NULL, \
185 		.end = NULL, \
186 		.get_progress = NULL, \
187 		.get_check = NULL, \
188 		.memconfig = NULL, \
189 		.update = NULL, \
190 	}
191 
192 
193 /// Internal data for lzma_strm_init, lzma_code, and lzma_end. A pointer to
194 /// this is stored in lzma_stream.
195 struct lzma_internal_s {
196 	/// The actual coder that should do something useful
197 	lzma_next_coder next;
198 
199 	/// Track the state of the coder. This is used to validate arguments
200 	/// so that the actual coders can rely on e.g. that LZMA_SYNC_FLUSH
201 	/// is used on every call to lzma_code until next.code has returned
202 	/// LZMA_STREAM_END.
203 	enum {
204 		ISEQ_RUN,
205 		ISEQ_SYNC_FLUSH,
206 		ISEQ_FULL_FLUSH,
207 		ISEQ_FINISH,
208 		ISEQ_FULL_BARRIER,
209 		ISEQ_END,
210 		ISEQ_ERROR,
211 	} sequence;
212 
213 	/// A copy of lzma_stream avail_in. This is used to verify that the
214 	/// amount of input doesn't change once e.g. LZMA_FINISH has been
215 	/// used.
216 	size_t avail_in;
217 
218 	/// Indicates which lzma_action values are allowed by next.code.
219 	bool supported_actions[LZMA_ACTION_MAX + 1];
220 
221 	/// If true, lzma_code will return LZMA_BUF_ERROR if no progress was
222 	/// made (no input consumed and no output produced by next.code).
223 	bool allow_buf_error;
224 };
225 
226 
227 /// Allocates memory
228 extern void *lzma_alloc(size_t size, const lzma_allocator *allocator)
229 		lzma_attribute((__malloc__)) lzma_attr_alloc_size(1);
230 
231 /// Allocates memory and zeroes it (like calloc()). This can be faster
232 /// than lzma_alloc() + memzero() while being backward compatible with
233 /// custom allocators.
234 extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
235 		lzma_alloc_zero(size_t size, const lzma_allocator *allocator);
236 
237 /// Frees memory
238 extern void lzma_free(void *ptr, const lzma_allocator *allocator);
239 
240 
241 /// Allocates strm->internal if it is NULL, and initializes *strm and
242 /// strm->internal. This function is only called via lzma_next_strm_init macro.
243 extern lzma_ret lzma_strm_init(lzma_stream *strm);
244 
245 /// Initializes the next filter in the chain, if any. This takes care of
246 /// freeing the memory of previously initialized filter if it is different
247 /// than the filter being initialized now. This way the actual filter
248 /// initialization functions don't need to use lzma_next_coder_init macro.
249 extern lzma_ret lzma_next_filter_init(lzma_next_coder *next,
250 		const lzma_allocator *allocator,
251 		const lzma_filter_info *filters);
252 
253 /// Update the next filter in the chain, if any. This checks that
254 /// the application is not trying to change the Filter IDs.
255 extern lzma_ret lzma_next_filter_update(
256 		lzma_next_coder *next, const lzma_allocator *allocator,
257 		const lzma_filter *reversed_filters);
258 
259 /// Frees the memory allocated for next->coder either using next->end or,
260 /// if next->end is NULL, using lzma_free.
261 extern void lzma_next_end(lzma_next_coder *next,
262 		const lzma_allocator *allocator);
263 
264 
265 /// Copy as much data as possible from in[] to out[] and update *in_pos
266 /// and *out_pos accordingly. Returns the number of bytes copied.
267 extern size_t lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
268 		size_t in_size, uint8_t *restrict out,
269 		size_t *restrict out_pos, size_t out_size);
270 
271 
272 /// \brief      Return if expression doesn't evaluate to LZMA_OK
273 ///
274 /// There are several situations where we want to return immediately
275 /// with the value of expr if it isn't LZMA_OK. This macro shortens
276 /// the code a little.
277 #define return_if_error(expr) \
278 do { \
279 	const lzma_ret ret_ = (expr); \
280 	if (ret_ != LZMA_OK) \
281 		return ret_; \
282 } while (0)
283 
284 
285 /// If next isn't already initialized, free the previous coder. Then mark
286 /// that next is _possibly_ initialized for the coder using this macro.
287 /// "Possibly" means that if e.g. allocation of next->coder fails, the
288 /// structure isn't actually initialized for this coder, but leaving
289 /// next->init to func is still OK.
290 #define lzma_next_coder_init(func, next, allocator) \
291 do { \
292 	if ((uintptr_t)(func) != (next)->init) \
293 		lzma_next_end(next, allocator); \
294 	(next)->init = (uintptr_t)(func); \
295 } while (0)
296 
297 
298 /// Initializes lzma_strm and calls func() to initialize strm->internal->next.
299 /// (The function being called will use lzma_next_coder_init()). If
300 /// initialization fails, memory that wasn't freed by func() is freed
301 /// along strm->internal.
302 #define lzma_next_strm_init(func, strm, ...) \
303 do { \
304 	return_if_error(lzma_strm_init(strm)); \
305 	const lzma_ret ret_ = func(&(strm)->internal->next, \
306 			(strm)->allocator, __VA_ARGS__); \
307 	if (ret_ != LZMA_OK) { \
308 		lzma_end(strm); \
309 		return ret_; \
310 	} \
311 } while (0)
312 
313 #endif
314