xz.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* xz.h - XZ decompressor */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * This file is based on code from XZ embedded project
  21. * http://tukaani.org/xz/embedded.html
  22. */
  23. #ifndef XZ_H
  24. #define XZ_H
  25. #include <config.h>
  26. #include <stdint.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include <grub/misc.h>
  30. #include <stdbool.h>
  31. /**
  32. * enum xz_ret - Return codes
  33. * @XZ_OK: Everything is OK so far. More input or more output
  34. * space is required to continue.
  35. * @XZ_STREAM_END: Operation finished successfully.
  36. * @XZ_MEMLIMIT_ERROR: Not enough memory was preallocated at decoder
  37. * initialization time.
  38. * @XZ_FORMAT_ERROR: File format was not recognized (wrong magic bytes).
  39. * @XZ_OPTIONS_ERROR: This implementation doesn't support the requested
  40. * compression options. In the decoder this means that
  41. * the header CRC32 matches, but the header itself
  42. * specifies something that we don't support.
  43. * @XZ_DATA_ERROR: Compressed data is corrupt.
  44. * @XZ_BUF_ERROR: Cannot make any progress. Details are slightly
  45. * different between multi-call and single-call mode;
  46. * more information below.
  47. *
  48. * In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls
  49. * to XZ code cannot consume any input and cannot produce any new output.
  50. * This happens when there is no new input available, or the output buffer
  51. * is full while at least one output byte is still pending. Assuming your
  52. * code is not buggy, you can get this error only when decoding a compressed
  53. * stream that is truncated or otherwise corrupt.
  54. *
  55. * In single-call mode, XZ_BUF_ERROR is returned only when the output buffer
  56. * is too small, or the compressed input is corrupt in a way that makes the
  57. * decoder produce more output than the caller expected. When it is
  58. * (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR
  59. * is used instead of XZ_BUF_ERROR.
  60. */
  61. enum xz_ret {
  62. XZ_OK,
  63. XZ_STREAM_END,
  64. XZ_MEMLIMIT_ERROR,
  65. XZ_FORMAT_ERROR,
  66. XZ_OPTIONS_ERROR,
  67. XZ_DATA_ERROR,
  68. XZ_BUF_ERROR
  69. };
  70. /**
  71. * struct xz_buf - Passing input and output buffers to XZ code
  72. * @in: Beginning of the input buffer. This may be NULL if and only
  73. * if in_pos is equal to in_size.
  74. * @in_pos: Current position in the input buffer. This must not exceed
  75. * in_size.
  76. * @in_size: Size of the input buffer
  77. * @out: Beginning of the output buffer. This may be NULL if and only
  78. * if out_pos is equal to out_size.
  79. * @out_pos: Current position in the output buffer. This must not exceed
  80. * out_size.
  81. * @out_size: Size of the output buffer
  82. *
  83. * Only the contents of the output buffer from out[out_pos] onward, and
  84. * the variables in_pos and out_pos are modified by the XZ code.
  85. */
  86. struct xz_buf {
  87. const uint8_t *in;
  88. size_t in_pos;
  89. size_t in_size;
  90. uint8_t *out;
  91. size_t out_pos;
  92. size_t out_size;
  93. };
  94. /**
  95. * struct xz_dec - Opaque type to hold the XZ decoder state
  96. */
  97. struct xz_dec;
  98. /**
  99. * xz_dec_init() - Allocate and initialize a XZ decoder state
  100. * @dict_max: Maximum size of the LZMA2 dictionary (history buffer) for
  101. * multi-call decoding, or special value of zero to indicate
  102. * single-call decoding mode.
  103. *
  104. * If dict_max > 0, the decoder is initialized to work in multi-call mode.
  105. * dict_max number of bytes of memory is preallocated for the LZMA2
  106. * dictionary. This way there is no risk that xz_dec_run() could run out
  107. * of memory, since xz_dec_run() will never allocate any memory. Instead,
  108. * if the preallocated dictionary is too small for decoding the given input
  109. * stream, xz_dec_run() will return XZ_MEMLIMIT_ERROR. Thus, it is important
  110. * to know what kind of data will be decoded to avoid allocating excessive
  111. * amount of memory for the dictionary.
  112. *
  113. * LZMA2 dictionary is always 2^n bytes or 2^n + 2^(n-1) bytes (the latter
  114. * sizes are less common in practice). In the kernel, dictionary sizes of
  115. * 64 KiB, 128 KiB, 256 KiB, 512 KiB, and 1 MiB are probably the only
  116. * reasonable values.
  117. *
  118. * If dict_max == 0, the decoder is initialized to work in single-call mode.
  119. * In single-call mode, xz_dec_run() decodes the whole stream at once. The
  120. * caller must provide enough output space or the decoding will fail. The
  121. * output space is used as the dictionary buffer, which is why there is
  122. * no need to allocate the dictionary as part of the decoder's internal
  123. * state.
  124. *
  125. * Because the output buffer is used as the workspace, streams encoded using
  126. * a big dictionary are not a problem in single-call. It is enough that the
  127. * output buffer is is big enough to hold the actual uncompressed data; it
  128. * can be smaller than the dictionary size stored in the stream headers.
  129. *
  130. * On success, xz_dec_init() returns a pointer to struct xz_dec, which is
  131. * ready to be used with xz_dec_run(). On error, xz_dec_init() returns NULL.
  132. */
  133. struct xz_dec * xz_dec_init(uint32_t dict_max);
  134. /**
  135. * xz_dec_run() - Run the XZ decoder
  136. * @s: Decoder state allocated using xz_dec_init()
  137. * @b: Input and output buffers
  138. *
  139. * In multi-call mode, this function may return any of the values listed in
  140. * enum xz_ret.
  141. *
  142. * In single-call mode, this function never returns XZ_OK. If an error occurs
  143. * in single-call mode (return value is not XZ_STREAM_END), b->in_pos and
  144. * b->out_pos are not modified, and the contents of the output buffer from
  145. * b->out[b->out_pos] onward are undefined.
  146. *
  147. * NOTE: In single-call mode, the contents of the output buffer are undefined
  148. * also after XZ_BUF_ERROR. This is because with some filter chains, there
  149. * may be a second pass over the output buffer, and this pass cannot be
  150. * properly done if the output buffer is truncated. Thus, you cannot give
  151. * the single-call decoder a too small buffer and then expect to get that
  152. * amount valid data from the beginning of the stream. You must use the
  153. * multi-call decoder if you don't want to uncompress the whole stream.
  154. */
  155. enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b);
  156. /**
  157. * xz_dec_reset() - Reset an already allocated decoder state
  158. * @s: Decoder state allocated using xz_dec_init()
  159. *
  160. * This function can be used to reset the multi-call decoder state without
  161. * freeing and reallocating memory with xz_dec_end() and xz_dec_init().
  162. *
  163. * In single-call mode, xz_dec_reset() is always called in the beginning of
  164. * xz_dec_run(). Thus, explicit call to xz_dec_reset() is useful only in
  165. * multi-call mode.
  166. */
  167. void xz_dec_reset(struct xz_dec *s);
  168. /**
  169. * xz_dec_end() - Free the memory allocated for the decoder state
  170. * @s: Decoder state allocated using xz_dec_init(). If s is NULL,
  171. * this function does nothing.
  172. */
  173. void xz_dec_end(struct xz_dec *s);
  174. #endif