xref: /freebsd/contrib/xz/src/liblzma/api/lzma.h (revision 3b35e7ee8de9b0260149a2b77e87a2b9c7a36244)
1 /* SPDX-License-Identifier: 0BSD */
2 
3 /**
4  * \file        api/lzma.h
5  * \brief       The public API of liblzma data compression library
6  * \mainpage
7  *
8  * liblzma is a general-purpose data compression library with a zlib-like API.
9  * The native file format is .xz, but also the old .lzma format and raw (no
10  * headers) streams are supported. Multiple compression algorithms (filters)
11  * are supported. Currently LZMA2 is the primary filter.
12  *
13  * liblzma is part of XZ Utils <https://tukaani.org/xz/>. XZ Utils
14  * includes a gzip-like command line tool named xz and some other tools.
15  * XZ Utils is developed and maintained by Lasse Collin.
16  *
17  * Major parts of liblzma are based on code written by Igor Pavlov,
18  * specifically the LZMA SDK <https://7-zip.org/sdk.html>.
19  *
20  * The SHA-256 implementation in liblzma is based on code written by
21  * Wei Dai in Crypto++ Library <https://www.cryptopp.com/>.
22  *
23  * liblzma is distributed under the BSD Zero Clause License (0BSD).
24  */
25 
26 /*
27  * Author: Lasse Collin
28  */
29 
30 #ifndef LZMA_H
31 #define LZMA_H
32 
33 /*****************************
34  * Required standard headers *
35  *****************************/
36 
37 /*
38  * liblzma API headers need some standard types and macros. To allow
39  * including lzma.h without requiring the application to include other
40  * headers first, lzma.h includes the required standard headers unless
41  * they already seem to be included already or if LZMA_MANUAL_HEADERS
42  * has been defined.
43  *
44  * Here's what types and macros are needed and from which headers:
45  *  - stddef.h: size_t, NULL
46  *  - stdint.h: uint8_t, uint32_t, uint64_t, UINT32_C(n), uint64_C(n),
47  *    UINT32_MAX, UINT64_MAX
48  *
49  * However, inttypes.h is a little more portable than stdint.h, although
50  * inttypes.h declares some unneeded things compared to plain stdint.h.
51  *
52  * The hacks below aren't perfect, specifically they assume that inttypes.h
53  * exists and that it typedefs at least uint8_t, uint32_t, and uint64_t,
54  * and that, in case of incomplete inttypes.h, unsigned int is 32-bit.
55  * If the application already takes care of setting up all the types and
56  * macros properly (for example by using gnulib's stdint.h or inttypes.h),
57  * we try to detect that the macros are already defined and don't include
58  * inttypes.h here again. However, you may define LZMA_MANUAL_HEADERS to
59  * force this file to never include any system headers.
60  *
61  * Some could argue that liblzma API should provide all the required types,
62  * for example lzma_uint64, LZMA_UINT64_C(n), and LZMA_UINT64_MAX. This was
63  * seen as an unnecessary mess, since most systems already provide all the
64  * necessary types and macros in the standard headers.
65  *
66  * Note that liblzma API still has lzma_bool, because using stdbool.h would
67  * break C89 and C++ programs on many systems. sizeof(bool) in C99 isn't
68  * necessarily the same as sizeof(bool) in C++.
69  */
70 
71 #ifndef LZMA_MANUAL_HEADERS
72 	/*
73 	 * I suppose this works portably also in C++. Note that in C++,
74 	 * we need to get size_t into the global namespace.
75 	 */
76 #	include <stddef.h>
77 
78 	/*
79 	 * Skip inttypes.h if we already have all the required macros. If we
80 	 * have the macros, we assume that we have the matching typedefs too.
81 	 */
82 #	if !defined(UINT32_C) || !defined(UINT64_C) \
83 			|| !defined(UINT32_MAX) || !defined(UINT64_MAX)
84 		/*
85 		 * MSVC versions older than 2013 have no C99 support, and
86 		 * thus they cannot be used to compile liblzma. Using an
87 		 * existing liblzma.dll with old MSVC can work though(*),
88 		 * but we need to define the required standard integer
89 		 * types here in a MSVC-specific way.
90 		 *
91 		 * (*) If you do this, the existing liblzma.dll probably uses
92 		 *     a different runtime library than your MSVC-built
93 		 *     application. Mixing runtimes is generally bad, but
94 		 *     in this case it should work as long as you avoid
95 		 *     the few rarely-needed liblzma functions that allocate
96 		 *     memory and expect the caller to free it using free().
97 		 */
98 #		if defined(_WIN32) && defined(_MSC_VER) && _MSC_VER < 1800
99 			typedef unsigned __int8 uint8_t;
100 			typedef unsigned __int32 uint32_t;
101 			typedef unsigned __int64 uint64_t;
102 #		else
103 			/* Use the standard inttypes.h. */
104 #			ifdef __cplusplus
105 				/*
106 				 * C99 sections 7.18.2 and 7.18.4 specify
107 				 * that C++ implementations define the limit
108 				 * and constant macros only if specifically
109 				 * requested. Note that if you want the
110 				 * format macros (PRIu64 etc.) too, you need
111 				 * to define __STDC_FORMAT_MACROS before
112 				 * including lzma.h, since re-including
113 				 * inttypes.h with __STDC_FORMAT_MACROS
114 				 * defined doesn't necessarily work.
115 				 */
116 #				ifndef __STDC_LIMIT_MACROS
117 #					define __STDC_LIMIT_MACROS 1
118 #				endif
119 #				ifndef __STDC_CONSTANT_MACROS
120 #					define __STDC_CONSTANT_MACROS 1
121 #				endif
122 #			endif
123 
124 #			include <inttypes.h>
125 #		endif
126 
127 		/*
128 		 * Some old systems have only the typedefs in inttypes.h, and
129 		 * lack all the macros. For those systems, we need a few more
130 		 * hacks. We assume that unsigned int is 32-bit and unsigned
131 		 * long is either 32-bit or 64-bit. If these hacks aren't
132 		 * enough, the application has to setup the types manually
133 		 * before including lzma.h.
134 		 */
135 #		ifndef UINT32_C
136 #			if defined(_WIN32) && defined(_MSC_VER)
137 #				define UINT32_C(n) n ## UI32
138 #			else
139 #				define UINT32_C(n) n ## U
140 #			endif
141 #		endif
142 
143 #		ifndef UINT64_C
144 #			if defined(_WIN32) && defined(_MSC_VER)
145 #				define UINT64_C(n) n ## UI64
146 #			else
147 				/* Get ULONG_MAX. */
148 #				include <limits.h>
149 #				if ULONG_MAX == 4294967295UL
150 #					define UINT64_C(n) n ## ULL
151 #				else
152 #					define UINT64_C(n) n ## UL
153 #				endif
154 #			endif
155 #		endif
156 
157 #		ifndef UINT32_MAX
158 #			define UINT32_MAX (UINT32_C(4294967295))
159 #		endif
160 
161 #		ifndef UINT64_MAX
162 #			define UINT64_MAX (UINT64_C(18446744073709551615))
163 #		endif
164 #	endif
165 #endif /* ifdef LZMA_MANUAL_HEADERS */
166 
167 
168 /******************
169  * LZMA_API macro *
170  ******************/
171 
172 /*
173  * Some systems require that the functions and function pointers are
174  * declared specially in the headers. LZMA_API_IMPORT is for importing
175  * symbols and LZMA_API_CALL is to specify the calling convention.
176  *
177  * By default it is assumed that the application will link dynamically
178  * against liblzma. #define LZMA_API_STATIC in your application if you
179  * want to link against static liblzma. If you don't care about portability
180  * to operating systems like Windows, or at least don't care about linking
181  * against static liblzma on them, don't worry about LZMA_API_STATIC. That
182  * is, most developers will never need to use LZMA_API_STATIC.
183  *
184  * The GCC variants are a special case on Windows (Cygwin and MinGW-w64).
185  * We rely on GCC doing the right thing with its auto-import feature,
186  * and thus don't use __declspec(dllimport). This way developers don't
187  * need to worry about LZMA_API_STATIC. Also the calling convention is
188  * omitted on Cygwin but not on MinGW-w64.
189  */
190 #ifndef LZMA_API_IMPORT
191 #	if !defined(LZMA_API_STATIC) && defined(_WIN32) && !defined(__GNUC__)
192 #		define LZMA_API_IMPORT __declspec(dllimport)
193 #	else
194 #		define LZMA_API_IMPORT
195 #	endif
196 #endif
197 
198 #ifndef LZMA_API_CALL
199 #	if defined(_WIN32) && !defined(__CYGWIN__)
200 #		define LZMA_API_CALL __cdecl
201 #	else
202 #		define LZMA_API_CALL
203 #	endif
204 #endif
205 
206 #ifndef LZMA_API
207 #	define LZMA_API(type) LZMA_API_IMPORT type LZMA_API_CALL
208 #endif
209 
210 
211 /***********
212  * nothrow *
213  ***********/
214 
215 /*
216  * None of the functions in liblzma may throw an exception. Even
217  * the functions that use callback functions won't throw exceptions,
218  * because liblzma would break if a callback function threw an exception.
219  */
220 #ifndef lzma_nothrow
221 #	if defined(__cplusplus)
222 #		if __cplusplus >= 201103L || (defined(_MSVC_LANG) \
223 				&& _MSVC_LANG >= 201103L)
224 #			define lzma_nothrow noexcept
225 #		else
226 #			define lzma_nothrow throw()
227 #		endif
228 #	elif defined(__GNUC__) && (__GNUC__ > 3 \
229 			|| (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))
230 #		define lzma_nothrow __attribute__((__nothrow__))
231 #	else
232 #		define lzma_nothrow
233 #	endif
234 #endif
235 
236 
237 /********************
238  * GNU C extensions *
239  ********************/
240 
241 /*
242  * GNU C extensions are used conditionally in the public API. It doesn't
243  * break anything if these are sometimes enabled and sometimes not, only
244  * affects warnings and optimizations.
245  */
246 #if defined(__GNUC__) && __GNUC__ >= 3
247 #	ifndef lzma_attribute
248 #		define lzma_attribute(attr) __attribute__(attr)
249 #	endif
250 
251 	/* warn_unused_result was added in GCC 3.4. */
252 #	ifndef lzma_attr_warn_unused_result
253 #		if __GNUC__ == 3 && __GNUC_MINOR__ < 4
254 #			define lzma_attr_warn_unused_result
255 #		endif
256 #	endif
257 
258 #else
259 #	ifndef lzma_attribute
260 #		define lzma_attribute(attr)
261 #	endif
262 #endif
263 
264 
265 #ifndef lzma_attr_pure
266 #	define lzma_attr_pure lzma_attribute((__pure__))
267 #endif
268 
269 #ifndef lzma_attr_const
270 #	define lzma_attr_const lzma_attribute((__const__))
271 #endif
272 
273 #ifndef lzma_attr_warn_unused_result
274 #	define lzma_attr_warn_unused_result \
275 		lzma_attribute((__warn_unused_result__))
276 #endif
277 
278 
279 /**************
280  * Subheaders *
281  **************/
282 
283 #ifdef __cplusplus
284 extern "C" {
285 #endif
286 
287 /*
288  * Subheaders check that this is defined. It is to prevent including
289  * them directly from applications.
290  */
291 #define LZMA_H_INTERNAL 1
292 
293 /* Basic features */
294 #include "lzma/version.h"
295 #include "lzma/base.h"
296 #include "lzma/vli.h"
297 #include "lzma/check.h"
298 
299 /* Filters */
300 #include "lzma/filter.h"
301 #include "lzma/bcj.h"
302 #include "lzma/delta.h"
303 #include "lzma/lzma12.h"
304 
305 /* Container formats */
306 #include "lzma/container.h"
307 
308 /* Advanced features */
309 #include "lzma/stream_flags.h"
310 #include "lzma/block.h"
311 #include "lzma/index.h"
312 #include "lzma/index_hash.h"
313 
314 /* Hardware information */
315 #include "lzma/hardware.h"
316 
317 /*
318  * All subheaders included. Undefine LZMA_H_INTERNAL to prevent applications
319  * re-including the subheaders.
320  */
321 #undef LZMA_H_INTERNAL
322 
323 #ifdef __cplusplus
324 }
325 #endif
326 
327 #endif /* ifndef LZMA_H */
328