Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:14

0001 /*
0002  * XZ decompressor
0003  *
0004  * Authors: Lasse Collin <lasse.collin@tukaani.org>
0005  *          Igor Pavlov <http://7-zip.org/>
0006  *
0007  * This file has been put into the public domain.
0008  * You can do whatever you want with this file.
0009  */
0010 
0011 #ifndef XZ_H
0012 #define XZ_H
0013 
0014 #ifdef __KERNEL__
0015 #   include <linux/stddef.h>
0016 #   include <linux/types.h>
0017 #else
0018 #   include <stddef.h>
0019 #   include <stdint.h>
0020 #endif
0021 
0022 #ifdef __cplusplus
0023 extern "C" {
0024 #endif
0025 
0026 /* In Linux, this is used to make extern functions static when needed. */
0027 #ifndef XZ_EXTERN
0028 #   define XZ_EXTERN extern
0029 #endif
0030 
0031 /**
0032  * enum xz_mode - Operation mode
0033  *
0034  * @XZ_SINGLE:              Single-call mode. This uses less RAM than
0035  *                          than multi-call modes, because the LZMA2
0036  *                          dictionary doesn't need to be allocated as
0037  *                          part of the decoder state. All required data
0038  *                          structures are allocated at initialization,
0039  *                          so xz_dec_run() cannot return XZ_MEM_ERROR.
0040  * @XZ_PREALLOC:            Multi-call mode with preallocated LZMA2
0041  *                          dictionary buffer. All data structures are
0042  *                          allocated at initialization, so xz_dec_run()
0043  *                          cannot return XZ_MEM_ERROR.
0044  * @XZ_DYNALLOC:            Multi-call mode. The LZMA2 dictionary is
0045  *                          allocated once the required size has been
0046  *                          parsed from the stream headers. If the
0047  *                          allocation fails, xz_dec_run() will return
0048  *                          XZ_MEM_ERROR.
0049  *
0050  * It is possible to enable support only for a subset of the above
0051  * modes at compile time by defining XZ_DEC_SINGLE, XZ_DEC_PREALLOC,
0052  * or XZ_DEC_DYNALLOC. The xz_dec kernel module is always compiled
0053  * with support for all operation modes, but the preboot code may
0054  * be built with fewer features to minimize code size.
0055  */
0056 enum xz_mode {
0057     XZ_SINGLE,
0058     XZ_PREALLOC,
0059     XZ_DYNALLOC
0060 };
0061 
0062 /**
0063  * enum xz_ret - Return codes
0064  * @XZ_OK:                  Everything is OK so far. More input or more
0065  *                          output space is required to continue. This
0066  *                          return code is possible only in multi-call mode
0067  *                          (XZ_PREALLOC or XZ_DYNALLOC).
0068  * @XZ_STREAM_END:          Operation finished successfully.
0069  * @XZ_UNSUPPORTED_CHECK:   Integrity check type is not supported. Decoding
0070  *                          is still possible in multi-call mode by simply
0071  *                          calling xz_dec_run() again.
0072  *                          Note that this return value is used only if
0073  *                          XZ_DEC_ANY_CHECK was defined at build time,
0074  *                          which is not used in the kernel. Unsupported
0075  *                          check types return XZ_OPTIONS_ERROR if
0076  *                          XZ_DEC_ANY_CHECK was not defined at build time.
0077  * @XZ_MEM_ERROR:           Allocating memory failed. This return code is
0078  *                          possible only if the decoder was initialized
0079  *                          with XZ_DYNALLOC. The amount of memory that was
0080  *                          tried to be allocated was no more than the
0081  *                          dict_max argument given to xz_dec_init().
0082  * @XZ_MEMLIMIT_ERROR:      A bigger LZMA2 dictionary would be needed than
0083  *                          allowed by the dict_max argument given to
0084  *                          xz_dec_init(). This return value is possible
0085  *                          only in multi-call mode (XZ_PREALLOC or
0086  *                          XZ_DYNALLOC); the single-call mode (XZ_SINGLE)
0087  *                          ignores the dict_max argument.
0088  * @XZ_FORMAT_ERROR:        File format was not recognized (wrong magic
0089  *                          bytes).
0090  * @XZ_OPTIONS_ERROR:       This implementation doesn't support the requested
0091  *                          compression options. In the decoder this means
0092  *                          that the header CRC32 matches, but the header
0093  *                          itself specifies something that we don't support.
0094  * @XZ_DATA_ERROR:          Compressed data is corrupt.
0095  * @XZ_BUF_ERROR:           Cannot make any progress. Details are slightly
0096  *                          different between multi-call and single-call
0097  *                          mode; more information below.
0098  *
0099  * In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls
0100  * to XZ code cannot consume any input and cannot produce any new output.
0101  * This happens when there is no new input available, or the output buffer
0102  * is full while at least one output byte is still pending. Assuming your
0103  * code is not buggy, you can get this error only when decoding a compressed
0104  * stream that is truncated or otherwise corrupt.
0105  *
0106  * In single-call mode, XZ_BUF_ERROR is returned only when the output buffer
0107  * is too small or the compressed input is corrupt in a way that makes the
0108  * decoder produce more output than the caller expected. When it is
0109  * (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR
0110  * is used instead of XZ_BUF_ERROR.
0111  */
0112 enum xz_ret {
0113     XZ_OK,
0114     XZ_STREAM_END,
0115     XZ_UNSUPPORTED_CHECK,
0116     XZ_MEM_ERROR,
0117     XZ_MEMLIMIT_ERROR,
0118     XZ_FORMAT_ERROR,
0119     XZ_OPTIONS_ERROR,
0120     XZ_DATA_ERROR,
0121     XZ_BUF_ERROR
0122 };
0123 
0124 /**
0125  * struct xz_buf - Passing input and output buffers to XZ code
0126  * @in:         Beginning of the input buffer. This may be NULL if and only
0127  *              if in_pos is equal to in_size.
0128  * @in_pos:     Current position in the input buffer. This must not exceed
0129  *              in_size.
0130  * @in_size:    Size of the input buffer
0131  * @out:        Beginning of the output buffer. This may be NULL if and only
0132  *              if out_pos is equal to out_size.
0133  * @out_pos:    Current position in the output buffer. This must not exceed
0134  *              out_size.
0135  * @out_size:   Size of the output buffer
0136  *
0137  * Only the contents of the output buffer from out[out_pos] onward, and
0138  * the variables in_pos and out_pos are modified by the XZ code.
0139  */
0140 struct xz_buf {
0141     const uint8_t *in;
0142     size_t in_pos;
0143     size_t in_size;
0144 
0145     uint8_t *out;
0146     size_t out_pos;
0147     size_t out_size;
0148 };
0149 
0150 /**
0151  * struct xz_dec - Opaque type to hold the XZ decoder state
0152  */
0153 struct xz_dec;
0154 
0155 /**
0156  * xz_dec_init() - Allocate and initialize a XZ decoder state
0157  * @mode:       Operation mode
0158  * @dict_max:   Maximum size of the LZMA2 dictionary (history buffer) for
0159  *              multi-call decoding. This is ignored in single-call mode
0160  *              (mode == XZ_SINGLE). LZMA2 dictionary is always 2^n bytes
0161  *              or 2^n + 2^(n-1) bytes (the latter sizes are less common
0162  *              in practice), so other values for dict_max don't make sense.
0163  *              In the kernel, dictionary sizes of 64 KiB, 128 KiB, 256 KiB,
0164  *              512 KiB, and 1 MiB are probably the only reasonable values,
0165  *              except for kernel and initramfs images where a bigger
0166  *              dictionary can be fine and useful.
0167  *
0168  * Single-call mode (XZ_SINGLE): xz_dec_run() decodes the whole stream at
0169  * once. The caller must provide enough output space or the decoding will
0170  * fail. The output space is used as the dictionary buffer, which is why
0171  * there is no need to allocate the dictionary as part of the decoder's
0172  * internal state.
0173  *
0174  * Because the output buffer is used as the workspace, streams encoded using
0175  * a big dictionary are not a problem in single-call mode. It is enough that
0176  * the output buffer is big enough to hold the actual uncompressed data; it
0177  * can be smaller than the dictionary size stored in the stream headers.
0178  *
0179  * Multi-call mode with preallocated dictionary (XZ_PREALLOC): dict_max bytes
0180  * of memory is preallocated for the LZMA2 dictionary. This way there is no
0181  * risk that xz_dec_run() could run out of memory, since xz_dec_run() will
0182  * never allocate any memory. Instead, if the preallocated dictionary is too
0183  * small for decoding the given input stream, xz_dec_run() will return
0184  * XZ_MEMLIMIT_ERROR. Thus, it is important to know what kind of data will be
0185  * decoded to avoid allocating excessive amount of memory for the dictionary.
0186  *
0187  * Multi-call mode with dynamically allocated dictionary (XZ_DYNALLOC):
0188  * dict_max specifies the maximum allowed dictionary size that xz_dec_run()
0189  * may allocate once it has parsed the dictionary size from the stream
0190  * headers. This way excessive allocations can be avoided while still
0191  * limiting the maximum memory usage to a sane value to prevent running the
0192  * system out of memory when decompressing streams from untrusted sources.
0193  *
0194  * On success, xz_dec_init() returns a pointer to struct xz_dec, which is
0195  * ready to be used with xz_dec_run(). If memory allocation fails,
0196  * xz_dec_init() returns NULL.
0197  */
0198 XZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max);
0199 
0200 /**
0201  * xz_dec_run() - Run the XZ decoder
0202  * @s:          Decoder state allocated using xz_dec_init()
0203  * @b:          Input and output buffers
0204  *
0205  * The possible return values depend on build options and operation mode.
0206  * See enum xz_ret for details.
0207  *
0208  * Note that if an error occurs in single-call mode (return value is not
0209  * XZ_STREAM_END), b->in_pos and b->out_pos are not modified and the
0210  * contents of the output buffer from b->out[b->out_pos] onward are
0211  * undefined. This is true even after XZ_BUF_ERROR, because with some filter
0212  * chains, there may be a second pass over the output buffer, and this pass
0213  * cannot be properly done if the output buffer is truncated. Thus, you
0214  * cannot give the single-call decoder a too small buffer and then expect to
0215  * get that amount valid data from the beginning of the stream. You must use
0216  * the multi-call decoder if you don't want to uncompress the whole stream.
0217  */
0218 XZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b);
0219 
0220 /**
0221  * xz_dec_reset() - Reset an already allocated decoder state
0222  * @s:          Decoder state allocated using xz_dec_init()
0223  *
0224  * This function can be used to reset the multi-call decoder state without
0225  * freeing and reallocating memory with xz_dec_end() and xz_dec_init().
0226  *
0227  * In single-call mode, xz_dec_reset() is always called in the beginning of
0228  * xz_dec_run(). Thus, explicit call to xz_dec_reset() is useful only in
0229  * multi-call mode.
0230  */
0231 XZ_EXTERN void xz_dec_reset(struct xz_dec *s);
0232 
0233 /**
0234  * xz_dec_end() - Free the memory allocated for the decoder state
0235  * @s:          Decoder state allocated using xz_dec_init(). If s is NULL,
0236  *              this function does nothing.
0237  */
0238 XZ_EXTERN void xz_dec_end(struct xz_dec *s);
0239 
0240 /*
0241  * Standalone build (userspace build or in-kernel build for boot time use)
0242  * needs a CRC32 implementation. For normal in-kernel use, kernel's own
0243  * CRC32 module is used instead, and users of this module don't need to
0244  * care about the functions below.
0245  */
0246 #ifndef XZ_INTERNAL_CRC32
0247 #   ifdef __KERNEL__
0248 #       define XZ_INTERNAL_CRC32 0
0249 #   else
0250 #       define XZ_INTERNAL_CRC32 1
0251 #   endif
0252 #endif
0253 
0254 /*
0255  * If CRC64 support has been enabled with XZ_USE_CRC64, a CRC64
0256  * implementation is needed too.
0257  */
0258 #ifndef XZ_USE_CRC64
0259 #   undef XZ_INTERNAL_CRC64
0260 #   define XZ_INTERNAL_CRC64 0
0261 #endif
0262 #ifndef XZ_INTERNAL_CRC64
0263 #   ifdef __KERNEL__
0264 #       error Using CRC64 in the kernel has not been implemented.
0265 #   else
0266 #       define XZ_INTERNAL_CRC64 1
0267 #   endif
0268 #endif
0269 
0270 #if XZ_INTERNAL_CRC32
0271 /*
0272  * This must be called before any other xz_* function to initialize
0273  * the CRC32 lookup table.
0274  */
0275 XZ_EXTERN void xz_crc32_init(void);
0276 
0277 /*
0278  * Update CRC32 value using the polynomial from IEEE-802.3. To start a new
0279  * calculation, the third argument must be zero. To continue the calculation,
0280  * the previously returned value is passed as the third argument.
0281  */
0282 XZ_EXTERN uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc);
0283 #endif
0284 
0285 #if XZ_INTERNAL_CRC64
0286 /*
0287  * This must be called before any other xz_* function (except xz_crc32_init())
0288  * to initialize the CRC64 lookup table.
0289  */
0290 XZ_EXTERN void xz_crc64_init(void);
0291 
0292 /*
0293  * Update CRC64 value using the polynomial from ECMA-182. To start a new
0294  * calculation, the third argument must be zero. To continue the calculation,
0295  * the previously returned value is passed as the third argument.
0296  */
0297 XZ_EXTERN uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc);
0298 #endif
0299 
0300 #ifdef __cplusplus
0301 }
0302 #endif
0303 
0304 #endif