1836d13a6SLasse Collin /* SPDX-License-Identifier: 0BSD */ 2836d13a6SLasse Collin 324fa0402SLasse Collin /* 424fa0402SLasse Collin * XZ decompressor 524fa0402SLasse Collin * 624fa0402SLasse Collin * Authors: Lasse Collin <[email protected]> 77f317d34SAlexander A. Klimov * Igor Pavlov <https://7-zip.org/> 824fa0402SLasse Collin */ 924fa0402SLasse Collin 1024fa0402SLasse Collin #ifndef XZ_H 1124fa0402SLasse Collin #define XZ_H 1224fa0402SLasse Collin 1324fa0402SLasse Collin #ifdef __KERNEL__ 1424fa0402SLasse Collin # include <linux/stddef.h> 1524fa0402SLasse Collin # include <linux/types.h> 1624fa0402SLasse Collin #else 1724fa0402SLasse Collin # include <stddef.h> 1824fa0402SLasse Collin # include <stdint.h> 1924fa0402SLasse Collin #endif 2024fa0402SLasse Collin 2124fa0402SLasse Collin /** 2224fa0402SLasse Collin * enum xz_mode - Operation mode 2324fa0402SLasse Collin * 2424fa0402SLasse Collin * @XZ_SINGLE: Single-call mode. This uses less RAM than 25f48ff83eSRandy Dunlap * multi-call modes, because the LZMA2 2624fa0402SLasse Collin * dictionary doesn't need to be allocated as 2724fa0402SLasse Collin * part of the decoder state. All required data 2824fa0402SLasse Collin * structures are allocated at initialization, 2924fa0402SLasse Collin * so xz_dec_run() cannot return XZ_MEM_ERROR. 3024fa0402SLasse Collin * @XZ_PREALLOC: Multi-call mode with preallocated LZMA2 3124fa0402SLasse Collin * dictionary buffer. All data structures are 3224fa0402SLasse Collin * allocated at initialization, so xz_dec_run() 3324fa0402SLasse Collin * cannot return XZ_MEM_ERROR. 3424fa0402SLasse Collin * @XZ_DYNALLOC: Multi-call mode. The LZMA2 dictionary is 3524fa0402SLasse Collin * allocated once the required size has been 3624fa0402SLasse Collin * parsed from the stream headers. If the 3724fa0402SLasse Collin * allocation fails, xz_dec_run() will return 3824fa0402SLasse Collin * XZ_MEM_ERROR. 3924fa0402SLasse Collin * 4024fa0402SLasse Collin * It is possible to enable support only for a subset of the above 4124fa0402SLasse Collin * modes at compile time by defining XZ_DEC_SINGLE, XZ_DEC_PREALLOC, 4224fa0402SLasse Collin * or XZ_DEC_DYNALLOC. The xz_dec kernel module is always compiled 4324fa0402SLasse Collin * with support for all operation modes, but the preboot code may 4424fa0402SLasse Collin * be built with fewer features to minimize code size. 4524fa0402SLasse Collin */ 4624fa0402SLasse Collin enum xz_mode { 4724fa0402SLasse Collin XZ_SINGLE, 4824fa0402SLasse Collin XZ_PREALLOC, 4924fa0402SLasse Collin XZ_DYNALLOC 5024fa0402SLasse Collin }; 5124fa0402SLasse Collin 5224fa0402SLasse Collin /** 5324fa0402SLasse Collin * enum xz_ret - Return codes 5424fa0402SLasse Collin * @XZ_OK: Everything is OK so far. More input or more 5524fa0402SLasse Collin * output space is required to continue. This 5624fa0402SLasse Collin * return code is possible only in multi-call mode 5724fa0402SLasse Collin * (XZ_PREALLOC or XZ_DYNALLOC). 5824fa0402SLasse Collin * @XZ_STREAM_END: Operation finished successfully. 5924fa0402SLasse Collin * @XZ_UNSUPPORTED_CHECK: Integrity check type is not supported. Decoding 6024fa0402SLasse Collin * is still possible in multi-call mode by simply 6124fa0402SLasse Collin * calling xz_dec_run() again. 6224fa0402SLasse Collin * Note that this return value is used only if 6324fa0402SLasse Collin * XZ_DEC_ANY_CHECK was defined at build time, 6424fa0402SLasse Collin * which is not used in the kernel. Unsupported 6524fa0402SLasse Collin * check types return XZ_OPTIONS_ERROR if 6624fa0402SLasse Collin * XZ_DEC_ANY_CHECK was not defined at build time. 6724fa0402SLasse Collin * @XZ_MEM_ERROR: Allocating memory failed. This return code is 6824fa0402SLasse Collin * possible only if the decoder was initialized 6924fa0402SLasse Collin * with XZ_DYNALLOC. The amount of memory that was 7024fa0402SLasse Collin * tried to be allocated was no more than the 7124fa0402SLasse Collin * dict_max argument given to xz_dec_init(). 7224fa0402SLasse Collin * @XZ_MEMLIMIT_ERROR: A bigger LZMA2 dictionary would be needed than 7324fa0402SLasse Collin * allowed by the dict_max argument given to 7424fa0402SLasse Collin * xz_dec_init(). This return value is possible 7524fa0402SLasse Collin * only in multi-call mode (XZ_PREALLOC or 7624fa0402SLasse Collin * XZ_DYNALLOC); the single-call mode (XZ_SINGLE) 7724fa0402SLasse Collin * ignores the dict_max argument. 7824fa0402SLasse Collin * @XZ_FORMAT_ERROR: File format was not recognized (wrong magic 7924fa0402SLasse Collin * bytes). 8024fa0402SLasse Collin * @XZ_OPTIONS_ERROR: This implementation doesn't support the requested 8124fa0402SLasse Collin * compression options. In the decoder this means 8224fa0402SLasse Collin * that the header CRC32 matches, but the header 8324fa0402SLasse Collin * itself specifies something that we don't support. 8424fa0402SLasse Collin * @XZ_DATA_ERROR: Compressed data is corrupt. 8524fa0402SLasse Collin * @XZ_BUF_ERROR: Cannot make any progress. Details are slightly 8624fa0402SLasse Collin * different between multi-call and single-call 8724fa0402SLasse Collin * mode; more information below. 8824fa0402SLasse Collin * 8924fa0402SLasse Collin * In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls 9024fa0402SLasse Collin * to XZ code cannot consume any input and cannot produce any new output. 9124fa0402SLasse Collin * This happens when there is no new input available, or the output buffer 9224fa0402SLasse Collin * is full while at least one output byte is still pending. Assuming your 9324fa0402SLasse Collin * code is not buggy, you can get this error only when decoding a compressed 9424fa0402SLasse Collin * stream that is truncated or otherwise corrupt. 9524fa0402SLasse Collin * 9624fa0402SLasse Collin * In single-call mode, XZ_BUF_ERROR is returned only when the output buffer 9724fa0402SLasse Collin * is too small or the compressed input is corrupt in a way that makes the 9824fa0402SLasse Collin * decoder produce more output than the caller expected. When it is 9924fa0402SLasse Collin * (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR 10024fa0402SLasse Collin * is used instead of XZ_BUF_ERROR. 10124fa0402SLasse Collin */ 10224fa0402SLasse Collin enum xz_ret { 10324fa0402SLasse Collin XZ_OK, 10424fa0402SLasse Collin XZ_STREAM_END, 10524fa0402SLasse Collin XZ_UNSUPPORTED_CHECK, 10624fa0402SLasse Collin XZ_MEM_ERROR, 10724fa0402SLasse Collin XZ_MEMLIMIT_ERROR, 10824fa0402SLasse Collin XZ_FORMAT_ERROR, 10924fa0402SLasse Collin XZ_OPTIONS_ERROR, 11024fa0402SLasse Collin XZ_DATA_ERROR, 11124fa0402SLasse Collin XZ_BUF_ERROR 11224fa0402SLasse Collin }; 11324fa0402SLasse Collin 11424fa0402SLasse Collin /** 11524fa0402SLasse Collin * struct xz_buf - Passing input and output buffers to XZ code 11624fa0402SLasse Collin * @in: Beginning of the input buffer. This may be NULL if and only 11724fa0402SLasse Collin * if in_pos is equal to in_size. 11824fa0402SLasse Collin * @in_pos: Current position in the input buffer. This must not exceed 11924fa0402SLasse Collin * in_size. 12024fa0402SLasse Collin * @in_size: Size of the input buffer 12124fa0402SLasse Collin * @out: Beginning of the output buffer. This may be NULL if and only 12224fa0402SLasse Collin * if out_pos is equal to out_size. 12324fa0402SLasse Collin * @out_pos: Current position in the output buffer. This must not exceed 12424fa0402SLasse Collin * out_size. 12524fa0402SLasse Collin * @out_size: Size of the output buffer 12624fa0402SLasse Collin * 12724fa0402SLasse Collin * Only the contents of the output buffer from out[out_pos] onward, and 12824fa0402SLasse Collin * the variables in_pos and out_pos are modified by the XZ code. 12924fa0402SLasse Collin */ 13024fa0402SLasse Collin struct xz_buf { 13124fa0402SLasse Collin const uint8_t *in; 13224fa0402SLasse Collin size_t in_pos; 13324fa0402SLasse Collin size_t in_size; 13424fa0402SLasse Collin 13524fa0402SLasse Collin uint8_t *out; 13624fa0402SLasse Collin size_t out_pos; 13724fa0402SLasse Collin size_t out_size; 13824fa0402SLasse Collin }; 13924fa0402SLasse Collin 140ad8c67b8SLasse Collin /* 14124fa0402SLasse Collin * struct xz_dec - Opaque type to hold the XZ decoder state 14224fa0402SLasse Collin */ 14324fa0402SLasse Collin struct xz_dec; 14424fa0402SLasse Collin 14524fa0402SLasse Collin /** 14624fa0402SLasse Collin * xz_dec_init() - Allocate and initialize a XZ decoder state 14724fa0402SLasse Collin * @mode: Operation mode 14824fa0402SLasse Collin * @dict_max: Maximum size of the LZMA2 dictionary (history buffer) for 14924fa0402SLasse Collin * multi-call decoding. This is ignored in single-call mode 15024fa0402SLasse Collin * (mode == XZ_SINGLE). LZMA2 dictionary is always 2^n bytes 15124fa0402SLasse Collin * or 2^n + 2^(n-1) bytes (the latter sizes are less common 15224fa0402SLasse Collin * in practice), so other values for dict_max don't make sense. 15324fa0402SLasse Collin * In the kernel, dictionary sizes of 64 KiB, 128 KiB, 256 KiB, 15424fa0402SLasse Collin * 512 KiB, and 1 MiB are probably the only reasonable values, 15524fa0402SLasse Collin * except for kernel and initramfs images where a bigger 15624fa0402SLasse Collin * dictionary can be fine and useful. 15724fa0402SLasse Collin * 15824fa0402SLasse Collin * Single-call mode (XZ_SINGLE): xz_dec_run() decodes the whole stream at 15924fa0402SLasse Collin * once. The caller must provide enough output space or the decoding will 16024fa0402SLasse Collin * fail. The output space is used as the dictionary buffer, which is why 16124fa0402SLasse Collin * there is no need to allocate the dictionary as part of the decoder's 16224fa0402SLasse Collin * internal state. 16324fa0402SLasse Collin * 16424fa0402SLasse Collin * Because the output buffer is used as the workspace, streams encoded using 16524fa0402SLasse Collin * a big dictionary are not a problem in single-call mode. It is enough that 16624fa0402SLasse Collin * the output buffer is big enough to hold the actual uncompressed data; it 16724fa0402SLasse Collin * can be smaller than the dictionary size stored in the stream headers. 16824fa0402SLasse Collin * 16924fa0402SLasse Collin * Multi-call mode with preallocated dictionary (XZ_PREALLOC): dict_max bytes 17024fa0402SLasse Collin * of memory is preallocated for the LZMA2 dictionary. This way there is no 17124fa0402SLasse Collin * risk that xz_dec_run() could run out of memory, since xz_dec_run() will 17224fa0402SLasse Collin * never allocate any memory. Instead, if the preallocated dictionary is too 17324fa0402SLasse Collin * small for decoding the given input stream, xz_dec_run() will return 17424fa0402SLasse Collin * XZ_MEMLIMIT_ERROR. Thus, it is important to know what kind of data will be 17524fa0402SLasse Collin * decoded to avoid allocating excessive amount of memory for the dictionary. 17624fa0402SLasse Collin * 17724fa0402SLasse Collin * Multi-call mode with dynamically allocated dictionary (XZ_DYNALLOC): 17824fa0402SLasse Collin * dict_max specifies the maximum allowed dictionary size that xz_dec_run() 17924fa0402SLasse Collin * may allocate once it has parsed the dictionary size from the stream 18024fa0402SLasse Collin * headers. This way excessive allocations can be avoided while still 18124fa0402SLasse Collin * limiting the maximum memory usage to a sane value to prevent running the 18224fa0402SLasse Collin * system out of memory when decompressing streams from untrusted sources. 18324fa0402SLasse Collin * 18424fa0402SLasse Collin * On success, xz_dec_init() returns a pointer to struct xz_dec, which is 18524fa0402SLasse Collin * ready to be used with xz_dec_run(). If memory allocation fails, 18624fa0402SLasse Collin * xz_dec_init() returns NULL. 18724fa0402SLasse Collin */ 188*c6f371baSLasse Collin struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max); 18924fa0402SLasse Collin 19024fa0402SLasse Collin /** 19124fa0402SLasse Collin * xz_dec_run() - Run the XZ decoder 19224fa0402SLasse Collin * @s: Decoder state allocated using xz_dec_init() 19324fa0402SLasse Collin * @b: Input and output buffers 19424fa0402SLasse Collin * 19524fa0402SLasse Collin * The possible return values depend on build options and operation mode. 19624fa0402SLasse Collin * See enum xz_ret for details. 19724fa0402SLasse Collin * 19824fa0402SLasse Collin * Note that if an error occurs in single-call mode (return value is not 19924fa0402SLasse Collin * XZ_STREAM_END), b->in_pos and b->out_pos are not modified and the 20024fa0402SLasse Collin * contents of the output buffer from b->out[b->out_pos] onward are 20124fa0402SLasse Collin * undefined. This is true even after XZ_BUF_ERROR, because with some filter 20224fa0402SLasse Collin * chains, there may be a second pass over the output buffer, and this pass 20324fa0402SLasse Collin * cannot be properly done if the output buffer is truncated. Thus, you 20424fa0402SLasse Collin * cannot give the single-call decoder a too small buffer and then expect to 20524fa0402SLasse Collin * get that amount valid data from the beginning of the stream. You must use 20624fa0402SLasse Collin * the multi-call decoder if you don't want to uncompress the whole stream. 20724fa0402SLasse Collin */ 208*c6f371baSLasse Collin enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b); 20924fa0402SLasse Collin 21024fa0402SLasse Collin /** 21124fa0402SLasse Collin * xz_dec_reset() - Reset an already allocated decoder state 21224fa0402SLasse Collin * @s: Decoder state allocated using xz_dec_init() 21324fa0402SLasse Collin * 21424fa0402SLasse Collin * This function can be used to reset the multi-call decoder state without 21524fa0402SLasse Collin * freeing and reallocating memory with xz_dec_end() and xz_dec_init(). 21624fa0402SLasse Collin * 21724fa0402SLasse Collin * In single-call mode, xz_dec_reset() is always called in the beginning of 21824fa0402SLasse Collin * xz_dec_run(). Thus, explicit call to xz_dec_reset() is useful only in 21924fa0402SLasse Collin * multi-call mode. 22024fa0402SLasse Collin */ 221*c6f371baSLasse Collin void xz_dec_reset(struct xz_dec *s); 22224fa0402SLasse Collin 22324fa0402SLasse Collin /** 22424fa0402SLasse Collin * xz_dec_end() - Free the memory allocated for the decoder state 22524fa0402SLasse Collin * @s: Decoder state allocated using xz_dec_init(). If s is NULL, 22624fa0402SLasse Collin * this function does nothing. 22724fa0402SLasse Collin */ 228*c6f371baSLasse Collin void xz_dec_end(struct xz_dec *s); 22924fa0402SLasse Collin 2300f2c5996SLasse Collin /** 2310f2c5996SLasse Collin * DOC: MicroLZMA decompressor 2320f2c5996SLasse Collin * 2330f2c5996SLasse Collin * This MicroLZMA header format was created for use in EROFS but may be used 2340f2c5996SLasse Collin * by others too. **In most cases one needs the XZ APIs above instead.** 2350f2c5996SLasse Collin * 2360f2c5996SLasse Collin * The compressed format supported by this decoder is a raw LZMA stream 2370f2c5996SLasse Collin * whose first byte (always 0x00) has been replaced with bitwise-negation 2380f2c5996SLasse Collin * of the LZMA properties (lc/lp/pb) byte. For example, if lc/lp/pb is 2390f2c5996SLasse Collin * 3/0/2, the first byte is 0xA2. This way the first byte can never be 0x00. 2400f2c5996SLasse Collin * Just like with LZMA2, lc + lp <= 4 must be true. The LZMA end-of-stream 2410f2c5996SLasse Collin * marker must not be used. The unused values are reserved for future use. 242aaa2975fSLasse Collin */ 243ad8c67b8SLasse Collin 244ad8c67b8SLasse Collin /* 245aaa2975fSLasse Collin * struct xz_dec_microlzma - Opaque type to hold the MicroLZMA decoder state 246aaa2975fSLasse Collin */ 247aaa2975fSLasse Collin struct xz_dec_microlzma; 248aaa2975fSLasse Collin 249aaa2975fSLasse Collin /** 250aaa2975fSLasse Collin * xz_dec_microlzma_alloc() - Allocate memory for the MicroLZMA decoder 251ad8c67b8SLasse Collin * @mode: XZ_SINGLE or XZ_PREALLOC 252ad8c67b8SLasse Collin * @dict_size: LZMA dictionary size. This must be at least 4 KiB and 253aaa2975fSLasse Collin * at most 3 GiB. 254aaa2975fSLasse Collin * 255aaa2975fSLasse Collin * In contrast to xz_dec_init(), this function only allocates the memory 256aaa2975fSLasse Collin * and remembers the dictionary size. xz_dec_microlzma_reset() must be used 257aaa2975fSLasse Collin * before calling xz_dec_microlzma_run(). 258aaa2975fSLasse Collin * 259aaa2975fSLasse Collin * The amount of allocated memory is a little less than 30 KiB with XZ_SINGLE. 260aaa2975fSLasse Collin * With XZ_PREALLOC also a dictionary buffer of dict_size bytes is allocated. 261aaa2975fSLasse Collin * 262aaa2975fSLasse Collin * On success, xz_dec_microlzma_alloc() returns a pointer to 263aaa2975fSLasse Collin * struct xz_dec_microlzma. If memory allocation fails or 264aaa2975fSLasse Collin * dict_size is invalid, NULL is returned. 265aaa2975fSLasse Collin */ 266*c6f371baSLasse Collin struct xz_dec_microlzma *xz_dec_microlzma_alloc(enum xz_mode mode, 267aaa2975fSLasse Collin uint32_t dict_size); 268aaa2975fSLasse Collin 269aaa2975fSLasse Collin /** 270aaa2975fSLasse Collin * xz_dec_microlzma_reset() - Reset the MicroLZMA decoder state 271ad8c67b8SLasse Collin * @s: Decoder state allocated using xz_dec_microlzma_alloc() 272ad8c67b8SLasse Collin * @comp_size: Compressed size of the input stream 273ad8c67b8SLasse Collin * @uncomp_size: Uncompressed size of the input stream. A value smaller 274aaa2975fSLasse Collin * than the real uncompressed size of the input stream can 275aaa2975fSLasse Collin * be specified if uncomp_size_is_exact is set to false. 276aaa2975fSLasse Collin * uncomp_size can never be set to a value larger than the 277aaa2975fSLasse Collin * expected real uncompressed size because it would eventually 278aaa2975fSLasse Collin * result in XZ_DATA_ERROR. 279ad8c67b8SLasse Collin * @uncomp_size_is_exact: This is an int instead of bool to avoid 280aaa2975fSLasse Collin * requiring stdbool.h. This should normally be set to true. 281aaa2975fSLasse Collin * When this is set to false, error detection is weaker. 282aaa2975fSLasse Collin */ 283*c6f371baSLasse Collin void xz_dec_microlzma_reset(struct xz_dec_microlzma *s, uint32_t comp_size, 284*c6f371baSLasse Collin uint32_t uncomp_size, int uncomp_size_is_exact); 285aaa2975fSLasse Collin 286aaa2975fSLasse Collin /** 287aaa2975fSLasse Collin * xz_dec_microlzma_run() - Run the MicroLZMA decoder 288ad8c67b8SLasse Collin * @s: Decoder state initialized using xz_dec_microlzma_reset() 289aaa2975fSLasse Collin * @b: Input and output buffers 290aaa2975fSLasse Collin * 291aaa2975fSLasse Collin * This works similarly to xz_dec_run() with a few important differences. 292aaa2975fSLasse Collin * Only the differences are documented here. 293aaa2975fSLasse Collin * 294aaa2975fSLasse Collin * The only possible return values are XZ_OK, XZ_STREAM_END, and 295aaa2975fSLasse Collin * XZ_DATA_ERROR. This function cannot return XZ_BUF_ERROR: if no progress 296aaa2975fSLasse Collin * is possible due to lack of input data or output space, this function will 297aaa2975fSLasse Collin * keep returning XZ_OK. Thus, the calling code must be written so that it 298aaa2975fSLasse Collin * will eventually provide input and output space matching (or exceeding) 299aaa2975fSLasse Collin * comp_size and uncomp_size arguments given to xz_dec_microlzma_reset(). 300aaa2975fSLasse Collin * If the caller cannot do this (for example, if the input file is truncated 301aaa2975fSLasse Collin * or otherwise corrupt), the caller must detect this error by itself to 302aaa2975fSLasse Collin * avoid an infinite loop. 303aaa2975fSLasse Collin * 304aaa2975fSLasse Collin * If the compressed data seems to be corrupt, XZ_DATA_ERROR is returned. 305aaa2975fSLasse Collin * This can happen also when incorrect dictionary, uncompressed, or 306aaa2975fSLasse Collin * compressed sizes have been specified. 307aaa2975fSLasse Collin * 308aaa2975fSLasse Collin * With XZ_PREALLOC only: As an extra feature, b->out may be NULL to skip over 309aaa2975fSLasse Collin * uncompressed data. This way the caller doesn't need to provide a temporary 310aaa2975fSLasse Collin * output buffer for the bytes that will be ignored. 311aaa2975fSLasse Collin * 312aaa2975fSLasse Collin * With XZ_SINGLE only: In contrast to xz_dec_run(), the return value XZ_OK 313aaa2975fSLasse Collin * is also possible and thus XZ_SINGLE is actually a limited multi-call mode. 314aaa2975fSLasse Collin * After XZ_OK the bytes decoded so far may be read from the output buffer. 315aaa2975fSLasse Collin * It is possible to continue decoding but the variables b->out and b->out_pos 316aaa2975fSLasse Collin * MUST NOT be changed by the caller. Increasing the value of b->out_size is 317aaa2975fSLasse Collin * allowed to make more output space available; one doesn't need to provide 318aaa2975fSLasse Collin * space for the whole uncompressed data on the first call. The input buffer 319aaa2975fSLasse Collin * may be changed normally like with XZ_PREALLOC. This way input data can be 320aaa2975fSLasse Collin * provided from non-contiguous memory. 321aaa2975fSLasse Collin */ 322*c6f371baSLasse Collin enum xz_ret xz_dec_microlzma_run(struct xz_dec_microlzma *s, struct xz_buf *b); 323aaa2975fSLasse Collin 324aaa2975fSLasse Collin /** 325aaa2975fSLasse Collin * xz_dec_microlzma_end() - Free the memory allocated for the decoder state 326aaa2975fSLasse Collin * @s: Decoder state allocated using xz_dec_microlzma_alloc(). 327aaa2975fSLasse Collin * If s is NULL, this function does nothing. 328aaa2975fSLasse Collin */ 329*c6f371baSLasse Collin void xz_dec_microlzma_end(struct xz_dec_microlzma *s); 330aaa2975fSLasse Collin 331aaa2975fSLasse Collin /* 33224fa0402SLasse Collin * Standalone build (userspace build or in-kernel build for boot time use) 33324fa0402SLasse Collin * needs a CRC32 implementation. For normal in-kernel use, kernel's own 33424fa0402SLasse Collin * CRC32 module is used instead, and users of this module don't need to 33524fa0402SLasse Collin * care about the functions below. 33624fa0402SLasse Collin */ 33724fa0402SLasse Collin #ifndef XZ_INTERNAL_CRC32 33824fa0402SLasse Collin # ifdef __KERNEL__ 33924fa0402SLasse Collin # define XZ_INTERNAL_CRC32 0 34024fa0402SLasse Collin # else 34124fa0402SLasse Collin # define XZ_INTERNAL_CRC32 1 34224fa0402SLasse Collin # endif 34324fa0402SLasse Collin #endif 34424fa0402SLasse Collin 34524fa0402SLasse Collin #if XZ_INTERNAL_CRC32 34624fa0402SLasse Collin /* 34724fa0402SLasse Collin * This must be called before any other xz_* function to initialize 34824fa0402SLasse Collin * the CRC32 lookup table. 34924fa0402SLasse Collin */ 350*c6f371baSLasse Collin void xz_crc32_init(void); 35124fa0402SLasse Collin 35224fa0402SLasse Collin /* 35324fa0402SLasse Collin * Update CRC32 value using the polynomial from IEEE-802.3. To start a new 35424fa0402SLasse Collin * calculation, the third argument must be zero. To continue the calculation, 35524fa0402SLasse Collin * the previously returned value is passed as the third argument. 35624fa0402SLasse Collin */ 357*c6f371baSLasse Collin uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc); 35824fa0402SLasse Collin #endif 35924fa0402SLasse Collin #endif 360