xz.h 11 KB

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