lzma.h 9.6 KB

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