xz.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * XZ decompressor
  3. *
  4. * Authors: Lasse Collin <[email protected]>
  5. * Igor Pavlov <https://7-zip.org/>
  6. *
  7. * This file has been put into the public domain.
  8. * You can do whatever you want with this file.
  9. */
  10. #ifndef XZ_H
  11. #define XZ_H
  12. #ifdef __KERNEL__
  13. # include <linux/stddef.h>
  14. # include <linux/types.h>
  15. #else
  16. # include <stddef.h>
  17. # include <stdint.h>
  18. #endif
  19. /* In Linux, this is used to make extern functions static when needed. */
  20. #ifndef XZ_EXTERN
  21. # define XZ_EXTERN extern
  22. #endif
  23. /**
  24. * enum xz_mode - Operation mode
  25. *
  26. * @XZ_SINGLE: Single-call mode. This uses less RAM than
  27. * multi-call modes, because the LZMA2
  28. * dictionary doesn't need to be allocated as
  29. * part of the decoder state. All required data
  30. * structures are allocated at initialization,
  31. * so xz_dec_run() cannot return XZ_MEM_ERROR.
  32. * @XZ_PREALLOC: Multi-call mode with preallocated LZMA2
  33. * dictionary buffer. All data structures are
  34. * allocated at initialization, so xz_dec_run()
  35. * cannot return XZ_MEM_ERROR.
  36. * @XZ_DYNALLOC: Multi-call mode. The LZMA2 dictionary is
  37. * allocated once the required size has been
  38. * parsed from the stream headers. If the
  39. * allocation fails, xz_dec_run() will return
  40. * XZ_MEM_ERROR.
  41. *
  42. * It is possible to enable support only for a subset of the above
  43. * modes at compile time by defining XZ_DEC_SINGLE, XZ_DEC_PREALLOC,
  44. * or XZ_DEC_DYNALLOC. The xz_dec kernel module is always compiled
  45. * with support for all operation modes, but the preboot code may
  46. * be built with fewer features to minimize code size.
  47. */
  48. enum xz_mode {
  49. XZ_SINGLE,
  50. XZ_PREALLOC,
  51. XZ_DYNALLOC
  52. };
  53. /**
  54. * enum xz_ret - Return codes
  55. * @XZ_OK: Everything is OK so far. More input or more
  56. * output space is required to continue. This
  57. * return code is possible only in multi-call mode
  58. * (XZ_PREALLOC or XZ_DYNALLOC).
  59. * @XZ_STREAM_END: Operation finished successfully.
  60. * @XZ_UNSUPPORTED_CHECK: Integrity check type is not supported. Decoding
  61. * is still possible in multi-call mode by simply
  62. * calling xz_dec_run() again.
  63. * Note that this return value is used only if
  64. * XZ_DEC_ANY_CHECK was defined at build time,
  65. * which is not used in the kernel. Unsupported
  66. * check types return XZ_OPTIONS_ERROR if
  67. * XZ_DEC_ANY_CHECK was not defined at build time.
  68. * @XZ_MEM_ERROR: Allocating memory failed. This return code is
  69. * possible only if the decoder was initialized
  70. * with XZ_DYNALLOC. The amount of memory that was
  71. * tried to be allocated was no more than the
  72. * dict_max argument given to xz_dec_init().
  73. * @XZ_MEMLIMIT_ERROR: A bigger LZMA2 dictionary would be needed than
  74. * allowed by the dict_max argument given to
  75. * xz_dec_init(). This return value is possible
  76. * only in multi-call mode (XZ_PREALLOC or
  77. * XZ_DYNALLOC); the single-call mode (XZ_SINGLE)
  78. * ignores the dict_max argument.
  79. * @XZ_FORMAT_ERROR: File format was not recognized (wrong magic
  80. * bytes).
  81. * @XZ_OPTIONS_ERROR: This implementation doesn't support the requested
  82. * compression options. In the decoder this means
  83. * that the header CRC32 matches, but the header
  84. * itself specifies something that we don't support.
  85. * @XZ_DATA_ERROR: Compressed data is corrupt.
  86. * @XZ_BUF_ERROR: Cannot make any progress. Details are slightly
  87. * different between multi-call and single-call
  88. * mode; more information below.
  89. *
  90. * In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls
  91. * to XZ code cannot consume any input and cannot produce any new output.
  92. * This happens when there is no new input available, or the output buffer
  93. * is full while at least one output byte is still pending. Assuming your
  94. * code is not buggy, you can get this error only when decoding a compressed
  95. * stream that is truncated or otherwise corrupt.
  96. *
  97. * In single-call mode, XZ_BUF_ERROR is returned only when the output buffer
  98. * is too small or the compressed input is corrupt in a way that makes the
  99. * decoder produce more output than the caller expected. When it is
  100. * (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR
  101. * is used instead of XZ_BUF_ERROR.
  102. */
  103. enum xz_ret {
  104. XZ_OK,
  105. XZ_STREAM_END,
  106. XZ_UNSUPPORTED_CHECK,
  107. XZ_MEM_ERROR,
  108. XZ_MEMLIMIT_ERROR,
  109. XZ_FORMAT_ERROR,
  110. XZ_OPTIONS_ERROR,
  111. XZ_DATA_ERROR,
  112. XZ_BUF_ERROR
  113. };
  114. /**
  115. * struct xz_buf - Passing input and output buffers to XZ code
  116. * @in: Beginning of the input buffer. This may be NULL if and only
  117. * if in_pos is equal to in_size.
  118. * @in_pos: Current position in the input buffer. This must not exceed
  119. * in_size.
  120. * @in_size: Size of the input buffer
  121. * @out: Beginning of the output buffer. This may be NULL if and only
  122. * if out_pos is equal to out_size.
  123. * @out_pos: Current position in the output buffer. This must not exceed
  124. * out_size.
  125. * @out_size: Size of the output buffer
  126. *
  127. * Only the contents of the output buffer from out[out_pos] onward, and
  128. * the variables in_pos and out_pos are modified by the XZ code.
  129. */
  130. struct xz_buf {
  131. const uint8_t *in;
  132. size_t in_pos;
  133. size_t in_size;
  134. uint8_t *out;
  135. size_t out_pos;
  136. size_t out_size;
  137. };
  138. /**
  139. * struct xz_dec - Opaque type to hold the XZ decoder state
  140. */
  141. struct xz_dec;
  142. /**
  143. * xz_dec_init() - Allocate and initialize a XZ decoder state
  144. * @mode: Operation mode
  145. * @dict_max: Maximum size of the LZMA2 dictionary (history buffer) for
  146. * multi-call decoding. This is ignored in single-call mode
  147. * (mode == XZ_SINGLE). LZMA2 dictionary is always 2^n bytes
  148. * or 2^n + 2^(n-1) bytes (the latter sizes are less common
  149. * in practice), so other values for dict_max don't make sense.
  150. * In the kernel, dictionary sizes of 64 KiB, 128 KiB, 256 KiB,
  151. * 512 KiB, and 1 MiB are probably the only reasonable values,
  152. * except for kernel and initramfs images where a bigger
  153. * dictionary can be fine and useful.
  154. *
  155. * Single-call mode (XZ_SINGLE): xz_dec_run() decodes the whole stream at
  156. * once. The caller must provide enough output space or the decoding will
  157. * fail. The output space is used as the dictionary buffer, which is why
  158. * there is no need to allocate the dictionary as part of the decoder's
  159. * internal state.
  160. *
  161. * Because the output buffer is used as the workspace, streams encoded using
  162. * a big dictionary are not a problem in single-call mode. It is enough that
  163. * the output buffer is big enough to hold the actual uncompressed data; it
  164. * can be smaller than the dictionary size stored in the stream headers.
  165. *
  166. * Multi-call mode with preallocated dictionary (XZ_PREALLOC): dict_max bytes
  167. * of memory is preallocated for the LZMA2 dictionary. This way there is no
  168. * risk that xz_dec_run() could run out of memory, since xz_dec_run() will
  169. * never allocate any memory. Instead, if the preallocated dictionary is too
  170. * small for decoding the given input stream, xz_dec_run() will return
  171. * XZ_MEMLIMIT_ERROR. Thus, it is important to know what kind of data will be
  172. * decoded to avoid allocating excessive amount of memory for the dictionary.
  173. *
  174. * Multi-call mode with dynamically allocated dictionary (XZ_DYNALLOC):
  175. * dict_max specifies the maximum allowed dictionary size that xz_dec_run()
  176. * may allocate once it has parsed the dictionary size from the stream
  177. * headers. This way excessive allocations can be avoided while still
  178. * limiting the maximum memory usage to a sane value to prevent running the
  179. * system out of memory when decompressing streams from untrusted sources.
  180. *
  181. * On success, xz_dec_init() returns a pointer to struct xz_dec, which is
  182. * ready to be used with xz_dec_run(). If memory allocation fails,
  183. * xz_dec_init() returns NULL.
  184. */
  185. XZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max);
  186. /**
  187. * xz_dec_run() - Run the XZ decoder
  188. * @s: Decoder state allocated using xz_dec_init()
  189. * @b: Input and output buffers
  190. *
  191. * The possible return values depend on build options and operation mode.
  192. * See enum xz_ret for details.
  193. *
  194. * Note that if an error occurs in single-call mode (return value is not
  195. * XZ_STREAM_END), b->in_pos and b->out_pos are not modified and the
  196. * contents of the output buffer from b->out[b->out_pos] onward are
  197. * undefined. This is true even after XZ_BUF_ERROR, because with some filter
  198. * chains, there may be a second pass over the output buffer, and this pass
  199. * cannot be properly done if the output buffer is truncated. Thus, you
  200. * cannot give the single-call decoder a too small buffer and then expect to
  201. * get that amount valid data from the beginning of the stream. You must use
  202. * the multi-call decoder if you don't want to uncompress the whole stream.
  203. */
  204. XZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b);
  205. /**
  206. * xz_dec_reset() - Reset an already allocated decoder state
  207. * @s: Decoder state allocated using xz_dec_init()
  208. *
  209. * This function can be used to reset the multi-call decoder state without
  210. * freeing and reallocating memory with xz_dec_end() and xz_dec_init().
  211. *
  212. * In single-call mode, xz_dec_reset() is always called in the beginning of
  213. * xz_dec_run(). Thus, explicit call to xz_dec_reset() is useful only in
  214. * multi-call mode.
  215. */
  216. XZ_EXTERN void xz_dec_reset(struct xz_dec *s);
  217. /**
  218. * xz_dec_end() - Free the memory allocated for the decoder state
  219. * @s: Decoder state allocated using xz_dec_init(). If s is NULL,
  220. * this function does nothing.
  221. */
  222. XZ_EXTERN void xz_dec_end(struct xz_dec *s);
  223. /*
  224. * Decompressor for MicroLZMA, an LZMA variant with a very minimal header.
  225. * See xz_dec_microlzma_alloc() below for details.
  226. *
  227. * These functions aren't used or available in preboot code and thus aren't
  228. * marked with XZ_EXTERN. This avoids warnings about static functions that
  229. * are never defined.
  230. */
  231. /**
  232. * struct xz_dec_microlzma - Opaque type to hold the MicroLZMA decoder state
  233. */
  234. struct xz_dec_microlzma;
  235. /**
  236. * xz_dec_microlzma_alloc() - Allocate memory for the MicroLZMA decoder
  237. * @mode XZ_SINGLE or XZ_PREALLOC
  238. * @dict_size LZMA dictionary size. This must be at least 4 KiB and
  239. * at most 3 GiB.
  240. *
  241. * In contrast to xz_dec_init(), this function only allocates the memory
  242. * and remembers the dictionary size. xz_dec_microlzma_reset() must be used
  243. * before calling xz_dec_microlzma_run().
  244. *
  245. * The amount of allocated memory is a little less than 30 KiB with XZ_SINGLE.
  246. * With XZ_PREALLOC also a dictionary buffer of dict_size bytes is allocated.
  247. *
  248. * On success, xz_dec_microlzma_alloc() returns a pointer to
  249. * struct xz_dec_microlzma. If memory allocation fails or
  250. * dict_size is invalid, NULL is returned.
  251. *
  252. * The compressed format supported by this decoder is a raw LZMA stream
  253. * whose first byte (always 0x00) has been replaced with bitwise-negation
  254. * of the LZMA properties (lc/lp/pb) byte. For example, if lc/lp/pb is
  255. * 3/0/2, the first byte is 0xA2. This way the first byte can never be 0x00.
  256. * Just like with LZMA2, lc + lp <= 4 must be true. The LZMA end-of-stream
  257. * marker must not be used. The unused values are reserved for future use.
  258. * This MicroLZMA header format was created for use in EROFS but may be used
  259. * by others too.
  260. */
  261. extern struct xz_dec_microlzma *xz_dec_microlzma_alloc(enum xz_mode mode,
  262. uint32_t dict_size);
  263. /**
  264. * xz_dec_microlzma_reset() - Reset the MicroLZMA decoder state
  265. * @s Decoder state allocated using xz_dec_microlzma_alloc()
  266. * @comp_size Compressed size of the input stream
  267. * @uncomp_size Uncompressed size of the input stream. A value smaller
  268. * than the real uncompressed size of the input stream can
  269. * be specified if uncomp_size_is_exact is set to false.
  270. * uncomp_size can never be set to a value larger than the
  271. * expected real uncompressed size because it would eventually
  272. * result in XZ_DATA_ERROR.
  273. * @uncomp_size_is_exact This is an int instead of bool to avoid
  274. * requiring stdbool.h. This should normally be set to true.
  275. * When this is set to false, error detection is weaker.
  276. */
  277. extern void xz_dec_microlzma_reset(struct xz_dec_microlzma *s,
  278. uint32_t comp_size, uint32_t uncomp_size,
  279. int uncomp_size_is_exact);
  280. /**
  281. * xz_dec_microlzma_run() - Run the MicroLZMA decoder
  282. * @s Decoder state initialized using xz_dec_microlzma_reset()
  283. * @b: Input and output buffers
  284. *
  285. * This works similarly to xz_dec_run() with a few important differences.
  286. * Only the differences are documented here.
  287. *
  288. * The only possible return values are XZ_OK, XZ_STREAM_END, and
  289. * XZ_DATA_ERROR. This function cannot return XZ_BUF_ERROR: if no progress
  290. * is possible due to lack of input data or output space, this function will
  291. * keep returning XZ_OK. Thus, the calling code must be written so that it
  292. * will eventually provide input and output space matching (or exceeding)
  293. * comp_size and uncomp_size arguments given to xz_dec_microlzma_reset().
  294. * If the caller cannot do this (for example, if the input file is truncated
  295. * or otherwise corrupt), the caller must detect this error by itself to
  296. * avoid an infinite loop.
  297. *
  298. * If the compressed data seems to be corrupt, XZ_DATA_ERROR is returned.
  299. * This can happen also when incorrect dictionary, uncompressed, or
  300. * compressed sizes have been specified.
  301. *
  302. * With XZ_PREALLOC only: As an extra feature, b->out may be NULL to skip over
  303. * uncompressed data. This way the caller doesn't need to provide a temporary
  304. * output buffer for the bytes that will be ignored.
  305. *
  306. * With XZ_SINGLE only: In contrast to xz_dec_run(), the return value XZ_OK
  307. * is also possible and thus XZ_SINGLE is actually a limited multi-call mode.
  308. * After XZ_OK the bytes decoded so far may be read from the output buffer.
  309. * It is possible to continue decoding but the variables b->out and b->out_pos
  310. * MUST NOT be changed by the caller. Increasing the value of b->out_size is
  311. * allowed to make more output space available; one doesn't need to provide
  312. * space for the whole uncompressed data on the first call. The input buffer
  313. * may be changed normally like with XZ_PREALLOC. This way input data can be
  314. * provided from non-contiguous memory.
  315. */
  316. extern enum xz_ret xz_dec_microlzma_run(struct xz_dec_microlzma *s,
  317. struct xz_buf *b);
  318. /**
  319. * xz_dec_microlzma_end() - Free the memory allocated for the decoder state
  320. * @s: Decoder state allocated using xz_dec_microlzma_alloc().
  321. * If s is NULL, this function does nothing.
  322. */
  323. extern void xz_dec_microlzma_end(struct xz_dec_microlzma *s);
  324. /*
  325. * Standalone build (userspace build or in-kernel build for boot time use)
  326. * needs a CRC32 implementation. For normal in-kernel use, kernel's own
  327. * CRC32 module is used instead, and users of this module don't need to
  328. * care about the functions below.
  329. */
  330. #ifndef XZ_INTERNAL_CRC32
  331. # ifdef __KERNEL__
  332. # define XZ_INTERNAL_CRC32 0
  333. # else
  334. # define XZ_INTERNAL_CRC32 1
  335. # endif
  336. #endif
  337. #if XZ_INTERNAL_CRC32
  338. /*
  339. * This must be called before any other xz_* function to initialize
  340. * the CRC32 lookup table.
  341. */
  342. XZ_EXTERN void xz_crc32_init(void);
  343. /*
  344. * Update CRC32 value using the polynomial from IEEE-802.3. To start a new
  345. * calculation, the third argument must be zero. To continue the calculation,
  346. * the previously returned value is passed as the third argument.
  347. */
  348. XZ_EXTERN uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc);
  349. #endif
  350. #endif