Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * XZ decompressor
0003  *
0004  * Authors: Lasse Collin <lasse.collin@tukaani.org>
0005  *          Igor Pavlov <https://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 /* In Linux, this is used to make extern functions static when needed. */
0023 #ifndef XZ_EXTERN
0024 #   define XZ_EXTERN extern
0025 #endif
0026 
0027 /**
0028  * enum xz_mode - Operation mode
0029  *
0030  * @XZ_SINGLE:              Single-call mode. This uses less RAM than
0031  *                          multi-call modes, because the LZMA2
0032  *                          dictionary doesn't need to be allocated as
0033  *                          part of the decoder state. All required data
0034  *                          structures are allocated at initialization,
0035  *                          so xz_dec_run() cannot return XZ_MEM_ERROR.
0036  * @XZ_PREALLOC:            Multi-call mode with preallocated LZMA2
0037  *                          dictionary buffer. All data structures are
0038  *                          allocated at initialization, so xz_dec_run()
0039  *                          cannot return XZ_MEM_ERROR.
0040  * @XZ_DYNALLOC:            Multi-call mode. The LZMA2 dictionary is
0041  *                          allocated once the required size has been
0042  *                          parsed from the stream headers. If the
0043  *                          allocation fails, xz_dec_run() will return
0044  *                          XZ_MEM_ERROR.
0045  *
0046  * It is possible to enable support only for a subset of the above
0047  * modes at compile time by defining XZ_DEC_SINGLE, XZ_DEC_PREALLOC,
0048  * or XZ_DEC_DYNALLOC. The xz_dec kernel module is always compiled
0049  * with support for all operation modes, but the preboot code may
0050  * be built with fewer features to minimize code size.
0051  */
0052 enum xz_mode {
0053     XZ_SINGLE,
0054     XZ_PREALLOC,
0055     XZ_DYNALLOC
0056 };
0057 
0058 /**
0059  * enum xz_ret - Return codes
0060  * @XZ_OK:                  Everything is OK so far. More input or more
0061  *                          output space is required to continue. This
0062  *                          return code is possible only in multi-call mode
0063  *                          (XZ_PREALLOC or XZ_DYNALLOC).
0064  * @XZ_STREAM_END:          Operation finished successfully.
0065  * @XZ_UNSUPPORTED_CHECK:   Integrity check type is not supported. Decoding
0066  *                          is still possible in multi-call mode by simply
0067  *                          calling xz_dec_run() again.
0068  *                          Note that this return value is used only if
0069  *                          XZ_DEC_ANY_CHECK was defined at build time,
0070  *                          which is not used in the kernel. Unsupported
0071  *                          check types return XZ_OPTIONS_ERROR if
0072  *                          XZ_DEC_ANY_CHECK was not defined at build time.
0073  * @XZ_MEM_ERROR:           Allocating memory failed. This return code is
0074  *                          possible only if the decoder was initialized
0075  *                          with XZ_DYNALLOC. The amount of memory that was
0076  *                          tried to be allocated was no more than the
0077  *                          dict_max argument given to xz_dec_init().
0078  * @XZ_MEMLIMIT_ERROR:      A bigger LZMA2 dictionary would be needed than
0079  *                          allowed by the dict_max argument given to
0080  *                          xz_dec_init(). This return value is possible
0081  *                          only in multi-call mode (XZ_PREALLOC or
0082  *                          XZ_DYNALLOC); the single-call mode (XZ_SINGLE)
0083  *                          ignores the dict_max argument.
0084  * @XZ_FORMAT_ERROR:        File format was not recognized (wrong magic
0085  *                          bytes).
0086  * @XZ_OPTIONS_ERROR:       This implementation doesn't support the requested
0087  *                          compression options. In the decoder this means
0088  *                          that the header CRC32 matches, but the header
0089  *                          itself specifies something that we don't support.
0090  * @XZ_DATA_ERROR:          Compressed data is corrupt.
0091  * @XZ_BUF_ERROR:           Cannot make any progress. Details are slightly
0092  *                          different between multi-call and single-call
0093  *                          mode; more information below.
0094  *
0095  * In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls
0096  * to XZ code cannot consume any input and cannot produce any new output.
0097  * This happens when there is no new input available, or the output buffer
0098  * is full while at least one output byte is still pending. Assuming your
0099  * code is not buggy, you can get this error only when decoding a compressed
0100  * stream that is truncated or otherwise corrupt.
0101  *
0102  * In single-call mode, XZ_BUF_ERROR is returned only when the output buffer
0103  * is too small or the compressed input is corrupt in a way that makes the
0104  * decoder produce more output than the caller expected. When it is
0105  * (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR
0106  * is used instead of XZ_BUF_ERROR.
0107  */
0108 enum xz_ret {
0109     XZ_OK,
0110     XZ_STREAM_END,
0111     XZ_UNSUPPORTED_CHECK,
0112     XZ_MEM_ERROR,
0113     XZ_MEMLIMIT_ERROR,
0114     XZ_FORMAT_ERROR,
0115     XZ_OPTIONS_ERROR,
0116     XZ_DATA_ERROR,
0117     XZ_BUF_ERROR
0118 };
0119 
0120 /**
0121  * struct xz_buf - Passing input and output buffers to XZ code
0122  * @in:         Beginning of the input buffer. This may be NULL if and only
0123  *              if in_pos is equal to in_size.
0124  * @in_pos:     Current position in the input buffer. This must not exceed
0125  *              in_size.
0126  * @in_size:    Size of the input buffer
0127  * @out:        Beginning of the output buffer. This may be NULL if and only
0128  *              if out_pos is equal to out_size.
0129  * @out_pos:    Current position in the output buffer. This must not exceed
0130  *              out_size.
0131  * @out_size:   Size of the output buffer
0132  *
0133  * Only the contents of the output buffer from out[out_pos] onward, and
0134  * the variables in_pos and out_pos are modified by the XZ code.
0135  */
0136 struct xz_buf {
0137     const uint8_t *in;
0138     size_t in_pos;
0139     size_t in_size;
0140 
0141     uint8_t *out;
0142     size_t out_pos;
0143     size_t out_size;
0144 };
0145 
0146 /**
0147  * struct xz_dec - Opaque type to hold the XZ decoder state
0148  */
0149 struct xz_dec;
0150 
0151 /**
0152  * xz_dec_init() - Allocate and initialize a XZ decoder state
0153  * @mode:       Operation mode
0154  * @dict_max:   Maximum size of the LZMA2 dictionary (history buffer) for
0155  *              multi-call decoding. This is ignored in single-call mode
0156  *              (mode == XZ_SINGLE). LZMA2 dictionary is always 2^n bytes
0157  *              or 2^n + 2^(n-1) bytes (the latter sizes are less common
0158  *              in practice), so other values for dict_max don't make sense.
0159  *              In the kernel, dictionary sizes of 64 KiB, 128 KiB, 256 KiB,
0160  *              512 KiB, and 1 MiB are probably the only reasonable values,
0161  *              except for kernel and initramfs images where a bigger
0162  *              dictionary can be fine and useful.
0163  *
0164  * Single-call mode (XZ_SINGLE): xz_dec_run() decodes the whole stream at
0165  * once. The caller must provide enough output space or the decoding will
0166  * fail. The output space is used as the dictionary buffer, which is why
0167  * there is no need to allocate the dictionary as part of the decoder's
0168  * internal state.
0169  *
0170  * Because the output buffer is used as the workspace, streams encoded using
0171  * a big dictionary are not a problem in single-call mode. It is enough that
0172  * the output buffer is big enough to hold the actual uncompressed data; it
0173  * can be smaller than the dictionary size stored in the stream headers.
0174  *
0175  * Multi-call mode with preallocated dictionary (XZ_PREALLOC): dict_max bytes
0176  * of memory is preallocated for the LZMA2 dictionary. This way there is no
0177  * risk that xz_dec_run() could run out of memory, since xz_dec_run() will
0178  * never allocate any memory. Instead, if the preallocated dictionary is too
0179  * small for decoding the given input stream, xz_dec_run() will return
0180  * XZ_MEMLIMIT_ERROR. Thus, it is important to know what kind of data will be
0181  * decoded to avoid allocating excessive amount of memory for the dictionary.
0182  *
0183  * Multi-call mode with dynamically allocated dictionary (XZ_DYNALLOC):
0184  * dict_max specifies the maximum allowed dictionary size that xz_dec_run()
0185  * may allocate once it has parsed the dictionary size from the stream
0186  * headers. This way excessive allocations can be avoided while still
0187  * limiting the maximum memory usage to a sane value to prevent running the
0188  * system out of memory when decompressing streams from untrusted sources.
0189  *
0190  * On success, xz_dec_init() returns a pointer to struct xz_dec, which is
0191  * ready to be used with xz_dec_run(). If memory allocation fails,
0192  * xz_dec_init() returns NULL.
0193  */
0194 XZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max);
0195 
0196 /**
0197  * xz_dec_run() - Run the XZ decoder
0198  * @s:          Decoder state allocated using xz_dec_init()
0199  * @b:          Input and output buffers
0200  *
0201  * The possible return values depend on build options and operation mode.
0202  * See enum xz_ret for details.
0203  *
0204  * Note that if an error occurs in single-call mode (return value is not
0205  * XZ_STREAM_END), b->in_pos and b->out_pos are not modified and the
0206  * contents of the output buffer from b->out[b->out_pos] onward are
0207  * undefined. This is true even after XZ_BUF_ERROR, because with some filter
0208  * chains, there may be a second pass over the output buffer, and this pass
0209  * cannot be properly done if the output buffer is truncated. Thus, you
0210  * cannot give the single-call decoder a too small buffer and then expect to
0211  * get that amount valid data from the beginning of the stream. You must use
0212  * the multi-call decoder if you don't want to uncompress the whole stream.
0213  */
0214 XZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b);
0215 
0216 /**
0217  * xz_dec_reset() - Reset an already allocated decoder state
0218  * @s:          Decoder state allocated using xz_dec_init()
0219  *
0220  * This function can be used to reset the multi-call decoder state without
0221  * freeing and reallocating memory with xz_dec_end() and xz_dec_init().
0222  *
0223  * In single-call mode, xz_dec_reset() is always called in the beginning of
0224  * xz_dec_run(). Thus, explicit call to xz_dec_reset() is useful only in
0225  * multi-call mode.
0226  */
0227 XZ_EXTERN void xz_dec_reset(struct xz_dec *s);
0228 
0229 /**
0230  * xz_dec_end() - Free the memory allocated for the decoder state
0231  * @s:          Decoder state allocated using xz_dec_init(). If s is NULL,
0232  *              this function does nothing.
0233  */
0234 XZ_EXTERN void xz_dec_end(struct xz_dec *s);
0235 
0236 /*
0237  * Decompressor for MicroLZMA, an LZMA variant with a very minimal header.
0238  * See xz_dec_microlzma_alloc() below for details.
0239  *
0240  * These functions aren't used or available in preboot code and thus aren't
0241  * marked with XZ_EXTERN. This avoids warnings about static functions that
0242  * are never defined.
0243  */
0244 /**
0245  * struct xz_dec_microlzma - Opaque type to hold the MicroLZMA decoder state
0246  */
0247 struct xz_dec_microlzma;
0248 
0249 /**
0250  * xz_dec_microlzma_alloc() - Allocate memory for the MicroLZMA decoder
0251  * @mode        XZ_SINGLE or XZ_PREALLOC
0252  * @dict_size   LZMA dictionary size. This must be at least 4 KiB and
0253  *              at most 3 GiB.
0254  *
0255  * In contrast to xz_dec_init(), this function only allocates the memory
0256  * and remembers the dictionary size. xz_dec_microlzma_reset() must be used
0257  * before calling xz_dec_microlzma_run().
0258  *
0259  * The amount of allocated memory is a little less than 30 KiB with XZ_SINGLE.
0260  * With XZ_PREALLOC also a dictionary buffer of dict_size bytes is allocated.
0261  *
0262  * On success, xz_dec_microlzma_alloc() returns a pointer to
0263  * struct xz_dec_microlzma. If memory allocation fails or
0264  * dict_size is invalid, NULL is returned.
0265  *
0266  * The compressed format supported by this decoder is a raw LZMA stream
0267  * whose first byte (always 0x00) has been replaced with bitwise-negation
0268  * of the LZMA properties (lc/lp/pb) byte. For example, if lc/lp/pb is
0269  * 3/0/2, the first byte is 0xA2. This way the first byte can never be 0x00.
0270  * Just like with LZMA2, lc + lp <= 4 must be true. The LZMA end-of-stream
0271  * marker must not be used. The unused values are reserved for future use.
0272  * This MicroLZMA header format was created for use in EROFS but may be used
0273  * by others too.
0274  */
0275 extern struct xz_dec_microlzma *xz_dec_microlzma_alloc(enum xz_mode mode,
0276                                uint32_t dict_size);
0277 
0278 /**
0279  * xz_dec_microlzma_reset() - Reset the MicroLZMA decoder state
0280  * @s           Decoder state allocated using xz_dec_microlzma_alloc()
0281  * @comp_size   Compressed size of the input stream
0282  * @uncomp_size Uncompressed size of the input stream. A value smaller
0283  *              than the real uncompressed size of the input stream can
0284  *              be specified if uncomp_size_is_exact is set to false.
0285  *              uncomp_size can never be set to a value larger than the
0286  *              expected real uncompressed size because it would eventually
0287  *              result in XZ_DATA_ERROR.
0288  * @uncomp_size_is_exact  This is an int instead of bool to avoid
0289  *              requiring stdbool.h. This should normally be set to true.
0290  *              When this is set to false, error detection is weaker.
0291  */
0292 extern void xz_dec_microlzma_reset(struct xz_dec_microlzma *s,
0293                    uint32_t comp_size, uint32_t uncomp_size,
0294                    int uncomp_size_is_exact);
0295 
0296 /**
0297  * xz_dec_microlzma_run() - Run the MicroLZMA decoder
0298  * @s           Decoder state initialized using xz_dec_microlzma_reset()
0299  * @b:          Input and output buffers
0300  *
0301  * This works similarly to xz_dec_run() with a few important differences.
0302  * Only the differences are documented here.
0303  *
0304  * The only possible return values are XZ_OK, XZ_STREAM_END, and
0305  * XZ_DATA_ERROR. This function cannot return XZ_BUF_ERROR: if no progress
0306  * is possible due to lack of input data or output space, this function will
0307  * keep returning XZ_OK. Thus, the calling code must be written so that it
0308  * will eventually provide input and output space matching (or exceeding)
0309  * comp_size and uncomp_size arguments given to xz_dec_microlzma_reset().
0310  * If the caller cannot do this (for example, if the input file is truncated
0311  * or otherwise corrupt), the caller must detect this error by itself to
0312  * avoid an infinite loop.
0313  *
0314  * If the compressed data seems to be corrupt, XZ_DATA_ERROR is returned.
0315  * This can happen also when incorrect dictionary, uncompressed, or
0316  * compressed sizes have been specified.
0317  *
0318  * With XZ_PREALLOC only: As an extra feature, b->out may be NULL to skip over
0319  * uncompressed data. This way the caller doesn't need to provide a temporary
0320  * output buffer for the bytes that will be ignored.
0321  *
0322  * With XZ_SINGLE only: In contrast to xz_dec_run(), the return value XZ_OK
0323  * is also possible and thus XZ_SINGLE is actually a limited multi-call mode.
0324  * After XZ_OK the bytes decoded so far may be read from the output buffer.
0325  * It is possible to continue decoding but the variables b->out and b->out_pos
0326  * MUST NOT be changed by the caller. Increasing the value of b->out_size is
0327  * allowed to make more output space available; one doesn't need to provide
0328  * space for the whole uncompressed data on the first call. The input buffer
0329  * may be changed normally like with XZ_PREALLOC. This way input data can be
0330  * provided from non-contiguous memory.
0331  */
0332 extern enum xz_ret xz_dec_microlzma_run(struct xz_dec_microlzma *s,
0333                     struct xz_buf *b);
0334 
0335 /**
0336  * xz_dec_microlzma_end() - Free the memory allocated for the decoder state
0337  * @s:          Decoder state allocated using xz_dec_microlzma_alloc().
0338  *              If s is NULL, this function does nothing.
0339  */
0340 extern void xz_dec_microlzma_end(struct xz_dec_microlzma *s);
0341 
0342 /*
0343  * Standalone build (userspace build or in-kernel build for boot time use)
0344  * needs a CRC32 implementation. For normal in-kernel use, kernel's own
0345  * CRC32 module is used instead, and users of this module don't need to
0346  * care about the functions below.
0347  */
0348 #ifndef XZ_INTERNAL_CRC32
0349 #   ifdef __KERNEL__
0350 #       define XZ_INTERNAL_CRC32 0
0351 #   else
0352 #       define XZ_INTERNAL_CRC32 1
0353 #   endif
0354 #endif
0355 
0356 #if XZ_INTERNAL_CRC32
0357 /*
0358  * This must be called before any other xz_* function to initialize
0359  * the CRC32 lookup table.
0360  */
0361 XZ_EXTERN void xz_crc32_init(void);
0362 
0363 /*
0364  * Update CRC32 value using the polynomial from IEEE-802.3. To start a new
0365  * calculation, the third argument must be zero. To continue the calculation,
0366  * the previously returned value is passed as the third argument.
0367  */
0368 XZ_EXTERN uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc);
0369 #endif
0370 #endif