xz.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * XZ decompressor
  3. *
  4. * Authors: Lasse Collin <lasse.collin@tukaani.org>
  5. * Igor Pavlov <http://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. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /* In Linux, this is used to make extern functions static when needed. */
  23. #ifndef XZ_EXTERN
  24. # define XZ_EXTERN extern
  25. #endif
  26. /* In Linux, this is used to mark the functions with __init when needed. */
  27. #ifndef XZ_FUNC
  28. # define XZ_FUNC
  29. #endif
  30. /**
  31. * enum xz_mode - Operation mode
  32. *
  33. * @XZ_SINGLE: Single-call mode. This uses less RAM than
  34. * than multi-call modes, because the LZMA2
  35. * dictionary doesn't need to be allocated as
  36. * part of the decoder state. All required data
  37. * structures are allocated at initialization,
  38. * so xz_dec_run() cannot return XZ_MEM_ERROR.
  39. * @XZ_PREALLOC: Multi-call mode with preallocated LZMA2
  40. * dictionary buffer. All data structures are
  41. * allocated at initialization, so xz_dec_run()
  42. * cannot return XZ_MEM_ERROR.
  43. * @XZ_DYNALLOC: Multi-call mode. The LZMA2 dictionary is
  44. * allocated once the required size has been
  45. * parsed from the stream headers. If the
  46. * allocation fails, xz_dec_run() will return
  47. * XZ_MEM_ERROR.
  48. *
  49. * It is possible to enable support only for a subset of the above
  50. * modes at compile time by defining XZ_DEC_SINGLE, XZ_DEC_PREALLOC,
  51. * or XZ_DEC_DYNALLOC. The xz_dec kernel module is always compiled
  52. * with support for all operation modes, but the preboot code may
  53. * be built with fewer features to minimize code size.
  54. */
  55. enum xz_mode {
  56. XZ_SINGLE,
  57. XZ_PREALLOC,
  58. XZ_DYNALLOC
  59. };
  60. /**
  61. * enum xz_ret - Return codes
  62. * @XZ_OK: Everything is OK so far. More input or more
  63. * output space is required to continue. This
  64. * return code is possible only in multi-call mode
  65. * (XZ_PREALLOC or XZ_DYNALLOC).
  66. * @XZ_STREAM_END: Operation finished successfully.
  67. * @XZ_UNSUPPORTED_CHECK: Integrity check type is not supported. Decoding
  68. * is still possible in multi-call mode by simply
  69. * calling xz_dec_run() again.
  70. * Note that this return value is used only if
  71. * XZ_DEC_ANY_CHECK was defined at build time,
  72. * which is not used in the kernel. Unsupported
  73. * check types return XZ_OPTIONS_ERROR if
  74. * XZ_DEC_ANY_CHECK was not defined at build time.
  75. * @XZ_MEM_ERROR: Allocating memory failed. This return code is
  76. * possible only if the decoder was initialized
  77. * with XZ_DYNALLOC. The amount of memory that was
  78. * tried to be allocated was no more than the
  79. * dict_max argument given to xz_dec_init().
  80. * @XZ_MEMLIMIT_ERROR: A bigger LZMA2 dictionary would be needed than
  81. * allowed by the dict_max argument given to
  82. * xz_dec_init(). This return value is possible
  83. * only in multi-call mode (XZ_PREALLOC or
  84. * XZ_DYNALLOC); the single-call mode (XZ_SINGLE)
  85. * ignores the dict_max argument.
  86. * @XZ_FORMAT_ERROR: File format was not recognized (wrong magic
  87. * bytes).
  88. * @XZ_OPTIONS_ERROR: This implementation doesn't support the requested
  89. * compression options. In the decoder this means
  90. * that the header CRC32 matches, but the header
  91. * itself specifies something that we don't support.
  92. * @XZ_DATA_ERROR: Compressed data is corrupt.
  93. * @XZ_BUF_ERROR: Cannot make any progress. Details are slightly
  94. * different between multi-call and single-call
  95. * mode; more information below.
  96. *
  97. * In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls
  98. * to XZ code cannot consume any input and cannot produce any new output.
  99. * This happens when there is no new input available, or the output buffer
  100. * is full while at least one output byte is still pending. Assuming your
  101. * code is not buggy, you can get this error only when decoding a compressed
  102. * stream that is truncated or otherwise corrupt.
  103. *
  104. * In single-call mode, XZ_BUF_ERROR is returned only when the output buffer
  105. * is too small or the compressed input is corrupt in a way that makes the
  106. * decoder produce more output than the caller expected. When it is
  107. * (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR
  108. * is used instead of XZ_BUF_ERROR.
  109. */
  110. enum xz_ret {
  111. XZ_OK,
  112. XZ_STREAM_END,
  113. XZ_UNSUPPORTED_CHECK,
  114. XZ_MEM_ERROR,
  115. XZ_MEMLIMIT_ERROR,
  116. XZ_FORMAT_ERROR,
  117. XZ_OPTIONS_ERROR,
  118. XZ_DATA_ERROR,
  119. XZ_BUF_ERROR
  120. };
  121. /**
  122. * struct xz_buf - Passing input and output buffers to XZ code
  123. * @in: Beginning of the input buffer. This may be NULL if and only
  124. * if in_pos is equal to in_size.
  125. * @in_pos: Current position in the input buffer. This must not exceed
  126. * in_size.
  127. * @in_size: Size of the input buffer
  128. * @out: Beginning of the output buffer. This may be NULL if and only
  129. * if out_pos is equal to out_size.
  130. * @out_pos: Current position in the output buffer. This must not exceed
  131. * out_size.
  132. * @out_size: Size of the output buffer
  133. *
  134. * Only the contents of the output buffer from out[out_pos] onward, and
  135. * the variables in_pos and out_pos are modified by the XZ code.
  136. */
  137. struct xz_buf {
  138. const uint8_t *in;
  139. size_t in_pos;
  140. size_t in_size;
  141. uint8_t *out;
  142. size_t out_pos;
  143. size_t out_size;
  144. };
  145. /**
  146. * struct xz_dec - Opaque type to hold the XZ decoder state
  147. */
  148. struct xz_dec;
  149. /**
  150. * xz_dec_init() - Allocate and initialize a XZ decoder state
  151. * @mode: Operation mode
  152. * @dict_max: Maximum size of the LZMA2 dictionary (history buffer) for
  153. * multi-call decoding. This is ignored in single-call mode
  154. * (mode == XZ_SINGLE). LZMA2 dictionary is always 2^n bytes
  155. * or 2^n + 2^(n-1) bytes (the latter sizes are less common
  156. * in practice), so other values for dict_max don't make sense.
  157. * In the kernel, dictionary sizes of 64 KiB, 128 KiB, 256 KiB,
  158. * 512 KiB, and 1 MiB are probably the only reasonable values,
  159. * except for kernel and initramfs images where a bigger
  160. * dictionary can be fine and useful.
  161. *
  162. * Single-call mode (XZ_SINGLE): xz_dec_run() decodes the whole stream at
  163. * once. The caller must provide enough output space or the decoding will
  164. * fail. The output space is used as the dictionary buffer, which is why
  165. * there is no need to allocate the dictionary as part of the decoder's
  166. * internal state.
  167. *
  168. * Because the output buffer is used as the workspace, streams encoded using
  169. * a big dictionary are not a problem in single-call mode. It is enough that
  170. * the output buffer is big enough to hold the actual uncompressed data; it
  171. * can be smaller than the dictionary size stored in the stream headers.
  172. *
  173. * Multi-call mode with preallocated dictionary (XZ_PREALLOC): dict_max bytes
  174. * of memory is preallocated for the LZMA2 dictionary. This way there is no
  175. * risk that xz_dec_run() could run out of memory, since xz_dec_run() will
  176. * never allocate any memory. Instead, if the preallocated dictionary is too
  177. * small for decoding the given input stream, xz_dec_run() will return
  178. * XZ_MEMLIMIT_ERROR. Thus, it is important to know what kind of data will be
  179. * decoded to avoid allocating excessive amount of memory for the dictionary.
  180. *
  181. * Multi-call mode with dynamically allocated dictionary (XZ_DYNALLOC):
  182. * dict_max specifies the maximum allowed dictionary size that xz_dec_run()
  183. * may allocate once it has parsed the dictionary size from the stream
  184. * headers. This way excessive allocations can be avoided while still
  185. * limiting the maximum memory usage to a sane value to prevent running the
  186. * system out of memory when decompressing streams from untrusted sources.
  187. *
  188. * On success, xz_dec_init() returns a pointer to struct xz_dec, which is
  189. * ready to be used with xz_dec_run(). If memory allocation fails,
  190. * xz_dec_init() returns NULL.
  191. */
  192. XZ_EXTERN struct xz_dec * XZ_FUNC xz_dec_init(
  193. enum xz_mode mode, uint32_t dict_max);
  194. /**
  195. * xz_dec_run() - Run the XZ decoder
  196. * @s: Decoder state allocated using xz_dec_init()
  197. * @b: Input and output buffers
  198. *
  199. * The possible return values depend on build options and operation mode.
  200. * See enum xz_ret for details.
  201. *
  202. * Note that if an error occurs in single-call mode (return value is not
  203. * XZ_STREAM_END), b->in_pos and b->out_pos are not modified and the
  204. * contents of the output buffer from b->out[b->out_pos] onward are
  205. * undefined. This is true even after XZ_BUF_ERROR, because with some filter
  206. * chains, there may be a second pass over the output buffer, and this pass
  207. * cannot be properly done if the output buffer is truncated. Thus, you
  208. * cannot give the single-call decoder a too small buffer and then expect to
  209. * get that amount valid data from the beginning of the stream. You must use
  210. * the multi-call decoder if you don't want to uncompress the whole stream.
  211. */
  212. XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_run(struct xz_dec *s, struct xz_buf *b);
  213. /**
  214. * xz_dec_reset() - Reset an already allocated decoder state
  215. * @s: Decoder state allocated using xz_dec_init()
  216. *
  217. * This function can be used to reset the multi-call decoder state without
  218. * freeing and reallocating memory with xz_dec_end() and xz_dec_init().
  219. *
  220. * In single-call mode, xz_dec_reset() is always called in the beginning of
  221. * xz_dec_run(). Thus, explicit call to xz_dec_reset() is useful only in
  222. * multi-call mode.
  223. */
  224. XZ_EXTERN void XZ_FUNC xz_dec_reset(struct xz_dec *s);
  225. /**
  226. * xz_dec_end() - Free the memory allocated for the decoder state
  227. * @s: Decoder state allocated using xz_dec_init(). If s is NULL,
  228. * this function does nothing.
  229. */
  230. XZ_EXTERN void XZ_FUNC xz_dec_end(struct xz_dec *s);
  231. /*
  232. * Standalone build (userspace build or in-kernel build for boot time use)
  233. * needs a CRC32 implementation. For normal in-kernel use, kernel's own
  234. * CRC32 module is used instead, and users of this module don't need to
  235. * care about the functions below.
  236. */
  237. #ifndef XZ_INTERNAL_CRC32
  238. # ifdef __KERNEL__
  239. # define XZ_INTERNAL_CRC32 0
  240. # else
  241. # define XZ_INTERNAL_CRC32 1
  242. # endif
  243. #endif
  244. #if XZ_INTERNAL_CRC32
  245. /*
  246. * This must be called before any other xz_* function to initialize
  247. * the CRC32 lookup table.
  248. */
  249. XZ_EXTERN void XZ_FUNC xz_crc32_init(void);
  250. /*
  251. * Update CRC32 value using the polynomial from IEEE-802.3. To start a new
  252. * calculation, the third argument must be zero. To continue the calculation,
  253. * the previously returned value is passed as the third argument.
  254. */
  255. XZ_EXTERN uint32_t XZ_FUNC xz_crc32(
  256. const uint8_t *buf, size_t size, uint32_t crc);
  257. #endif
  258. #ifdef __cplusplus
  259. }
  260. #endif
  261. #endif