163dab8eeSAdrian Chadd /* 263dab8eeSAdrian Chadd * XZ decompressor 363dab8eeSAdrian Chadd * 463dab8eeSAdrian Chadd * Authors: Lasse Collin <lasse.collin@tukaani.org> 5*cd3a777bSXin LI * Igor Pavlov <https://7-zip.org/> 663dab8eeSAdrian Chadd * 763dab8eeSAdrian Chadd * This file has been put into the public domain. 863dab8eeSAdrian Chadd * You can do whatever you want with this file. 963dab8eeSAdrian Chadd */ 1063dab8eeSAdrian Chadd 1163dab8eeSAdrian Chadd #ifndef XZ_H 1263dab8eeSAdrian Chadd #define XZ_H 1363dab8eeSAdrian Chadd 1463dab8eeSAdrian Chadd #ifdef __KERNEL__ 1563dab8eeSAdrian Chadd # include <linux/stddef.h> 1663dab8eeSAdrian Chadd # include <linux/types.h> 1763dab8eeSAdrian Chadd #else 18e11f2b82SAleksandr Rybalko #ifdef __FreeBSD__ 19e11f2b82SAleksandr Rybalko # include <sys/stddef.h> 20e11f2b82SAleksandr Rybalko # include <sys/types.h> 21e11f2b82SAleksandr Rybalko #else 2263dab8eeSAdrian Chadd # include <stddef.h> 2363dab8eeSAdrian Chadd # include <stdint.h> 2463dab8eeSAdrian Chadd #endif 25e11f2b82SAleksandr Rybalko #endif 2663dab8eeSAdrian Chadd 2763dab8eeSAdrian Chadd #ifdef __cplusplus 2863dab8eeSAdrian Chadd extern "C" { 2963dab8eeSAdrian Chadd #endif 3063dab8eeSAdrian Chadd 3163dab8eeSAdrian Chadd /* In Linux, this is used to make extern functions static when needed. */ 3263dab8eeSAdrian Chadd #ifndef XZ_EXTERN 3363dab8eeSAdrian Chadd # define XZ_EXTERN extern 3463dab8eeSAdrian Chadd #endif 3563dab8eeSAdrian Chadd 3663dab8eeSAdrian Chadd /** 3763dab8eeSAdrian Chadd * enum xz_mode - Operation mode 3863dab8eeSAdrian Chadd * 3963dab8eeSAdrian Chadd * @XZ_SINGLE: Single-call mode. This uses less RAM than 40*cd3a777bSXin LI * multi-call modes, because the LZMA2 4163dab8eeSAdrian Chadd * dictionary doesn't need to be allocated as 4263dab8eeSAdrian Chadd * part of the decoder state. All required data 4363dab8eeSAdrian Chadd * structures are allocated at initialization, 4463dab8eeSAdrian Chadd * so xz_dec_run() cannot return XZ_MEM_ERROR. 4563dab8eeSAdrian Chadd * @XZ_PREALLOC: Multi-call mode with preallocated LZMA2 4663dab8eeSAdrian Chadd * dictionary buffer. All data structures are 4763dab8eeSAdrian Chadd * allocated at initialization, so xz_dec_run() 4863dab8eeSAdrian Chadd * cannot return XZ_MEM_ERROR. 4963dab8eeSAdrian Chadd * @XZ_DYNALLOC: Multi-call mode. The LZMA2 dictionary is 5063dab8eeSAdrian Chadd * allocated once the required size has been 5163dab8eeSAdrian Chadd * parsed from the stream headers. If the 5263dab8eeSAdrian Chadd * allocation fails, xz_dec_run() will return 5363dab8eeSAdrian Chadd * XZ_MEM_ERROR. 5463dab8eeSAdrian Chadd * 5563dab8eeSAdrian Chadd * It is possible to enable support only for a subset of the above 5663dab8eeSAdrian Chadd * modes at compile time by defining XZ_DEC_SINGLE, XZ_DEC_PREALLOC, 5763dab8eeSAdrian Chadd * or XZ_DEC_DYNALLOC. The xz_dec kernel module is always compiled 5863dab8eeSAdrian Chadd * with support for all operation modes, but the preboot code may 5963dab8eeSAdrian Chadd * be built with fewer features to minimize code size. 6063dab8eeSAdrian Chadd */ 6163dab8eeSAdrian Chadd enum xz_mode { 6263dab8eeSAdrian Chadd XZ_SINGLE, 6363dab8eeSAdrian Chadd XZ_PREALLOC, 6463dab8eeSAdrian Chadd XZ_DYNALLOC 6563dab8eeSAdrian Chadd }; 6663dab8eeSAdrian Chadd 6763dab8eeSAdrian Chadd /** 6863dab8eeSAdrian Chadd * enum xz_ret - Return codes 6963dab8eeSAdrian Chadd * @XZ_OK: Everything is OK so far. More input or more 7063dab8eeSAdrian Chadd * output space is required to continue. This 7163dab8eeSAdrian Chadd * return code is possible only in multi-call mode 7263dab8eeSAdrian Chadd * (XZ_PREALLOC or XZ_DYNALLOC). 7363dab8eeSAdrian Chadd * @XZ_STREAM_END: Operation finished successfully. 7463dab8eeSAdrian Chadd * @XZ_UNSUPPORTED_CHECK: Integrity check type is not supported. Decoding 7563dab8eeSAdrian Chadd * is still possible in multi-call mode by simply 7663dab8eeSAdrian Chadd * calling xz_dec_run() again. 7763dab8eeSAdrian Chadd * Note that this return value is used only if 7863dab8eeSAdrian Chadd * XZ_DEC_ANY_CHECK was defined at build time, 7963dab8eeSAdrian Chadd * which is not used in the kernel. Unsupported 8063dab8eeSAdrian Chadd * check types return XZ_OPTIONS_ERROR if 8163dab8eeSAdrian Chadd * XZ_DEC_ANY_CHECK was not defined at build time. 8263dab8eeSAdrian Chadd * @XZ_MEM_ERROR: Allocating memory failed. This return code is 8363dab8eeSAdrian Chadd * possible only if the decoder was initialized 8463dab8eeSAdrian Chadd * with XZ_DYNALLOC. The amount of memory that was 8563dab8eeSAdrian Chadd * tried to be allocated was no more than the 8663dab8eeSAdrian Chadd * dict_max argument given to xz_dec_init(). 8763dab8eeSAdrian Chadd * @XZ_MEMLIMIT_ERROR: A bigger LZMA2 dictionary would be needed than 8863dab8eeSAdrian Chadd * allowed by the dict_max argument given to 8963dab8eeSAdrian Chadd * xz_dec_init(). This return value is possible 9063dab8eeSAdrian Chadd * only in multi-call mode (XZ_PREALLOC or 9163dab8eeSAdrian Chadd * XZ_DYNALLOC); the single-call mode (XZ_SINGLE) 9263dab8eeSAdrian Chadd * ignores the dict_max argument. 9363dab8eeSAdrian Chadd * @XZ_FORMAT_ERROR: File format was not recognized (wrong magic 9463dab8eeSAdrian Chadd * bytes). 9563dab8eeSAdrian Chadd * @XZ_OPTIONS_ERROR: This implementation doesn't support the requested 9663dab8eeSAdrian Chadd * compression options. In the decoder this means 9763dab8eeSAdrian Chadd * that the header CRC32 matches, but the header 9863dab8eeSAdrian Chadd * itself specifies something that we don't support. 9963dab8eeSAdrian Chadd * @XZ_DATA_ERROR: Compressed data is corrupt. 10063dab8eeSAdrian Chadd * @XZ_BUF_ERROR: Cannot make any progress. Details are slightly 10163dab8eeSAdrian Chadd * different between multi-call and single-call 10263dab8eeSAdrian Chadd * mode; more information below. 10363dab8eeSAdrian Chadd * 10463dab8eeSAdrian Chadd * In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls 10563dab8eeSAdrian Chadd * to XZ code cannot consume any input and cannot produce any new output. 10663dab8eeSAdrian Chadd * This happens when there is no new input available, or the output buffer 10763dab8eeSAdrian Chadd * is full while at least one output byte is still pending. Assuming your 10863dab8eeSAdrian Chadd * code is not buggy, you can get this error only when decoding a compressed 10963dab8eeSAdrian Chadd * stream that is truncated or otherwise corrupt. 11063dab8eeSAdrian Chadd * 11163dab8eeSAdrian Chadd * In single-call mode, XZ_BUF_ERROR is returned only when the output buffer 11263dab8eeSAdrian Chadd * is too small or the compressed input is corrupt in a way that makes the 11363dab8eeSAdrian Chadd * decoder produce more output than the caller expected. When it is 11463dab8eeSAdrian Chadd * (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR 11563dab8eeSAdrian Chadd * is used instead of XZ_BUF_ERROR. 11663dab8eeSAdrian Chadd */ 11763dab8eeSAdrian Chadd enum xz_ret { 11863dab8eeSAdrian Chadd XZ_OK, 11963dab8eeSAdrian Chadd XZ_STREAM_END, 12063dab8eeSAdrian Chadd XZ_UNSUPPORTED_CHECK, 12163dab8eeSAdrian Chadd XZ_MEM_ERROR, 12263dab8eeSAdrian Chadd XZ_MEMLIMIT_ERROR, 12363dab8eeSAdrian Chadd XZ_FORMAT_ERROR, 12463dab8eeSAdrian Chadd XZ_OPTIONS_ERROR, 12563dab8eeSAdrian Chadd XZ_DATA_ERROR, 12663dab8eeSAdrian Chadd XZ_BUF_ERROR 12763dab8eeSAdrian Chadd }; 12863dab8eeSAdrian Chadd 12963dab8eeSAdrian Chadd /** 13063dab8eeSAdrian Chadd * struct xz_buf - Passing input and output buffers to XZ code 13163dab8eeSAdrian Chadd * @in: Beginning of the input buffer. This may be NULL if and only 13263dab8eeSAdrian Chadd * if in_pos is equal to in_size. 13363dab8eeSAdrian Chadd * @in_pos: Current position in the input buffer. This must not exceed 13463dab8eeSAdrian Chadd * in_size. 13563dab8eeSAdrian Chadd * @in_size: Size of the input buffer 13663dab8eeSAdrian Chadd * @out: Beginning of the output buffer. This may be NULL if and only 13763dab8eeSAdrian Chadd * if out_pos is equal to out_size. 13863dab8eeSAdrian Chadd * @out_pos: Current position in the output buffer. This must not exceed 13963dab8eeSAdrian Chadd * out_size. 14063dab8eeSAdrian Chadd * @out_size: Size of the output buffer 14163dab8eeSAdrian Chadd * 14263dab8eeSAdrian Chadd * Only the contents of the output buffer from out[out_pos] onward, and 14363dab8eeSAdrian Chadd * the variables in_pos and out_pos are modified by the XZ code. 14463dab8eeSAdrian Chadd */ 14563dab8eeSAdrian Chadd struct xz_buf { 14663dab8eeSAdrian Chadd const uint8_t *in; 14763dab8eeSAdrian Chadd size_t in_pos; 14863dab8eeSAdrian Chadd size_t in_size; 14963dab8eeSAdrian Chadd 15063dab8eeSAdrian Chadd uint8_t *out; 15163dab8eeSAdrian Chadd size_t out_pos; 15263dab8eeSAdrian Chadd size_t out_size; 15363dab8eeSAdrian Chadd }; 15463dab8eeSAdrian Chadd 15563dab8eeSAdrian Chadd /** 15663dab8eeSAdrian Chadd * struct xz_dec - Opaque type to hold the XZ decoder state 15763dab8eeSAdrian Chadd */ 15863dab8eeSAdrian Chadd struct xz_dec; 15963dab8eeSAdrian Chadd 16063dab8eeSAdrian Chadd /** 16163dab8eeSAdrian Chadd * xz_dec_init() - Allocate and initialize a XZ decoder state 16263dab8eeSAdrian Chadd * @mode: Operation mode 16363dab8eeSAdrian Chadd * @dict_max: Maximum size of the LZMA2 dictionary (history buffer) for 16463dab8eeSAdrian Chadd * multi-call decoding. This is ignored in single-call mode 16563dab8eeSAdrian Chadd * (mode == XZ_SINGLE). LZMA2 dictionary is always 2^n bytes 16663dab8eeSAdrian Chadd * or 2^n + 2^(n-1) bytes (the latter sizes are less common 16763dab8eeSAdrian Chadd * in practice), so other values for dict_max don't make sense. 16863dab8eeSAdrian Chadd * In the kernel, dictionary sizes of 64 KiB, 128 KiB, 256 KiB, 16963dab8eeSAdrian Chadd * 512 KiB, and 1 MiB are probably the only reasonable values, 17063dab8eeSAdrian Chadd * except for kernel and initramfs images where a bigger 17163dab8eeSAdrian Chadd * dictionary can be fine and useful. 17263dab8eeSAdrian Chadd * 17363dab8eeSAdrian Chadd * Single-call mode (XZ_SINGLE): xz_dec_run() decodes the whole stream at 17463dab8eeSAdrian Chadd * once. The caller must provide enough output space or the decoding will 17563dab8eeSAdrian Chadd * fail. The output space is used as the dictionary buffer, which is why 17663dab8eeSAdrian Chadd * there is no need to allocate the dictionary as part of the decoder's 17763dab8eeSAdrian Chadd * internal state. 17863dab8eeSAdrian Chadd * 17963dab8eeSAdrian Chadd * Because the output buffer is used as the workspace, streams encoded using 18063dab8eeSAdrian Chadd * a big dictionary are not a problem in single-call mode. It is enough that 18163dab8eeSAdrian Chadd * the output buffer is big enough to hold the actual uncompressed data; it 18263dab8eeSAdrian Chadd * can be smaller than the dictionary size stored in the stream headers. 18363dab8eeSAdrian Chadd * 18463dab8eeSAdrian Chadd * Multi-call mode with preallocated dictionary (XZ_PREALLOC): dict_max bytes 18563dab8eeSAdrian Chadd * of memory is preallocated for the LZMA2 dictionary. This way there is no 18663dab8eeSAdrian Chadd * risk that xz_dec_run() could run out of memory, since xz_dec_run() will 18763dab8eeSAdrian Chadd * never allocate any memory. Instead, if the preallocated dictionary is too 18863dab8eeSAdrian Chadd * small for decoding the given input stream, xz_dec_run() will return 18963dab8eeSAdrian Chadd * XZ_MEMLIMIT_ERROR. Thus, it is important to know what kind of data will be 19063dab8eeSAdrian Chadd * decoded to avoid allocating excessive amount of memory for the dictionary. 19163dab8eeSAdrian Chadd * 19263dab8eeSAdrian Chadd * Multi-call mode with dynamically allocated dictionary (XZ_DYNALLOC): 19363dab8eeSAdrian Chadd * dict_max specifies the maximum allowed dictionary size that xz_dec_run() 19463dab8eeSAdrian Chadd * may allocate once it has parsed the dictionary size from the stream 19563dab8eeSAdrian Chadd * headers. This way excessive allocations can be avoided while still 19663dab8eeSAdrian Chadd * limiting the maximum memory usage to a sane value to prevent running the 19763dab8eeSAdrian Chadd * system out of memory when decompressing streams from untrusted sources. 19863dab8eeSAdrian Chadd * 19963dab8eeSAdrian Chadd * On success, xz_dec_init() returns a pointer to struct xz_dec, which is 20063dab8eeSAdrian Chadd * ready to be used with xz_dec_run(). If memory allocation fails, 20163dab8eeSAdrian Chadd * xz_dec_init() returns NULL. 20263dab8eeSAdrian Chadd */ 20363dab8eeSAdrian Chadd XZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max); 20463dab8eeSAdrian Chadd 20563dab8eeSAdrian Chadd /** 206*cd3a777bSXin LI * xz_dec_run() - Run the XZ decoder for a single XZ stream 20763dab8eeSAdrian Chadd * @s: Decoder state allocated using xz_dec_init() 20863dab8eeSAdrian Chadd * @b: Input and output buffers 20963dab8eeSAdrian Chadd * 21063dab8eeSAdrian Chadd * The possible return values depend on build options and operation mode. 21163dab8eeSAdrian Chadd * See enum xz_ret for details. 21263dab8eeSAdrian Chadd * 21363dab8eeSAdrian Chadd * Note that if an error occurs in single-call mode (return value is not 21463dab8eeSAdrian Chadd * XZ_STREAM_END), b->in_pos and b->out_pos are not modified and the 21563dab8eeSAdrian Chadd * contents of the output buffer from b->out[b->out_pos] onward are 21663dab8eeSAdrian Chadd * undefined. This is true even after XZ_BUF_ERROR, because with some filter 21763dab8eeSAdrian Chadd * chains, there may be a second pass over the output buffer, and this pass 21863dab8eeSAdrian Chadd * cannot be properly done if the output buffer is truncated. Thus, you 21963dab8eeSAdrian Chadd * cannot give the single-call decoder a too small buffer and then expect to 22063dab8eeSAdrian Chadd * get that amount valid data from the beginning of the stream. You must use 22163dab8eeSAdrian Chadd * the multi-call decoder if you don't want to uncompress the whole stream. 222*cd3a777bSXin LI * 223*cd3a777bSXin LI * Use xz_dec_run() when XZ data is stored inside some other file format. 224*cd3a777bSXin LI * The decoding will stop after one XZ stream has been decompresed. To 225*cd3a777bSXin LI * decompress regular .xz files which might have multiple concatenated 226*cd3a777bSXin LI * streams, use xz_dec_catrun() instead. 22763dab8eeSAdrian Chadd */ 22863dab8eeSAdrian Chadd XZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b); 22963dab8eeSAdrian Chadd 23063dab8eeSAdrian Chadd /** 231*cd3a777bSXin LI * xz_dec_catrun() - Run the XZ decoder with support for concatenated streams 232*cd3a777bSXin LI * @s: Decoder state allocated using xz_dec_init() 233*cd3a777bSXin LI * @b: Input and output buffers 234*cd3a777bSXin LI * @finish: This is an int instead of bool to avoid requiring stdbool.h. 235*cd3a777bSXin LI * As long as more input might be coming, finish must be false. 236*cd3a777bSXin LI * When the caller knows that it has provided all the input to 237*cd3a777bSXin LI * the decoder (some possibly still in b->in), it must set finish 238*cd3a777bSXin LI * to true. Only when finish is true can this function return 239*cd3a777bSXin LI * XZ_STREAM_END to indicate successful decompression of the 240*cd3a777bSXin LI * file. In single-call mode (XZ_SINGLE) finish is assumed to 241*cd3a777bSXin LI * always be true; the caller-provided value is ignored. 242*cd3a777bSXin LI * 243*cd3a777bSXin LI * This is like xz_dec_run() except that this makes it easy to decode .xz 244*cd3a777bSXin LI * files with multiple streams (multiple .xz files concatenated as is). 245*cd3a777bSXin LI * The rarely-used Stream Padding feature is supported too, that is, there 246*cd3a777bSXin LI * can be null bytes after or between the streams. The number of null bytes 247*cd3a777bSXin LI * must be a multiple of four. 248*cd3a777bSXin LI * 249*cd3a777bSXin LI * When finish is false and b->in_pos == b->in_size, it is possible that 250*cd3a777bSXin LI * XZ_BUF_ERROR isn't returned even when no progress is possible (XZ_OK is 251*cd3a777bSXin LI * returned instead). This shouldn't matter because in this situation a 252*cd3a777bSXin LI * reasonable caller will attempt to provide more input or set finish to 253*cd3a777bSXin LI * true for the next xz_dec_catrun() call anyway. 254*cd3a777bSXin LI * 255*cd3a777bSXin LI * For any struct xz_dec that has been initialized for multi-call mode: 256*cd3a777bSXin LI * Once decoding has been started with xz_dec_run() or xz_dec_catrun(), 257*cd3a777bSXin LI * the same function must be used until xz_dec_reset() or xz_dec_end(). 258*cd3a777bSXin LI * Switching between the two decoding functions without resetting results 259*cd3a777bSXin LI * in undefined behavior. 260*cd3a777bSXin LI * 261*cd3a777bSXin LI * xz_dec_catrun() is only available if XZ_DEC_CONCATENATED was defined 262*cd3a777bSXin LI * at compile time. 263*cd3a777bSXin LI */ 264*cd3a777bSXin LI XZ_EXTERN enum xz_ret xz_dec_catrun(struct xz_dec *s, struct xz_buf *b, 265*cd3a777bSXin LI int finish); 266*cd3a777bSXin LI 267*cd3a777bSXin LI /** 26863dab8eeSAdrian Chadd * xz_dec_reset() - Reset an already allocated decoder state 26963dab8eeSAdrian Chadd * @s: Decoder state allocated using xz_dec_init() 27063dab8eeSAdrian Chadd * 27163dab8eeSAdrian Chadd * This function can be used to reset the multi-call decoder state without 27263dab8eeSAdrian Chadd * freeing and reallocating memory with xz_dec_end() and xz_dec_init(). 27363dab8eeSAdrian Chadd * 27463dab8eeSAdrian Chadd * In single-call mode, xz_dec_reset() is always called in the beginning of 27563dab8eeSAdrian Chadd * xz_dec_run(). Thus, explicit call to xz_dec_reset() is useful only in 27663dab8eeSAdrian Chadd * multi-call mode. 27763dab8eeSAdrian Chadd */ 27863dab8eeSAdrian Chadd XZ_EXTERN void xz_dec_reset(struct xz_dec *s); 27963dab8eeSAdrian Chadd 28063dab8eeSAdrian Chadd /** 28163dab8eeSAdrian Chadd * xz_dec_end() - Free the memory allocated for the decoder state 28263dab8eeSAdrian Chadd * @s: Decoder state allocated using xz_dec_init(). If s is NULL, 28363dab8eeSAdrian Chadd * this function does nothing. 28463dab8eeSAdrian Chadd */ 28563dab8eeSAdrian Chadd XZ_EXTERN void xz_dec_end(struct xz_dec *s); 28663dab8eeSAdrian Chadd 28763dab8eeSAdrian Chadd /* 288*cd3a777bSXin LI * Decompressor for MicroLZMA, an LZMA variant with a very minimal header. 289*cd3a777bSXin LI * See xz_dec_microlzma_alloc() below for details. 290*cd3a777bSXin LI * 291*cd3a777bSXin LI * These functions aren't used or available in preboot code and thus aren't 292*cd3a777bSXin LI * marked with XZ_EXTERN. This avoids warnings about static functions that 293*cd3a777bSXin LI * are never defined. 294*cd3a777bSXin LI */ 295*cd3a777bSXin LI /** 296*cd3a777bSXin LI * struct xz_dec_microlzma - Opaque type to hold the MicroLZMA decoder state 297*cd3a777bSXin LI */ 298*cd3a777bSXin LI struct xz_dec_microlzma; 299*cd3a777bSXin LI 300*cd3a777bSXin LI /** 301*cd3a777bSXin LI * xz_dec_microlzma_alloc() - Allocate memory for the MicroLZMA decoder 302*cd3a777bSXin LI * @mode XZ_SINGLE or XZ_PREALLOC 303*cd3a777bSXin LI * @dict_size LZMA dictionary size. This must be at least 4 KiB and 304*cd3a777bSXin LI * at most 3 GiB. 305*cd3a777bSXin LI * 306*cd3a777bSXin LI * In contrast to xz_dec_init(), this function only allocates the memory 307*cd3a777bSXin LI * and remembers the dictionary size. xz_dec_microlzma_reset() must be used 308*cd3a777bSXin LI * before calling xz_dec_microlzma_run(). 309*cd3a777bSXin LI * 310*cd3a777bSXin LI * The amount of allocated memory is a little less than 30 KiB with XZ_SINGLE. 311*cd3a777bSXin LI * With XZ_PREALLOC also a dictionary buffer of dict_size bytes is allocated. 312*cd3a777bSXin LI * 313*cd3a777bSXin LI * On success, xz_dec_microlzma_alloc() returns a pointer to 314*cd3a777bSXin LI * struct xz_dec_microlzma. If memory allocation fails or 315*cd3a777bSXin LI * dict_size is invalid, NULL is returned. 316*cd3a777bSXin LI * 317*cd3a777bSXin LI * The compressed format supported by this decoder is a raw LZMA stream 318*cd3a777bSXin LI * whose first byte (always 0x00) has been replaced with bitwise-negation 319*cd3a777bSXin LI * of the LZMA properties (lc/lp/pb) byte. For example, if lc/lp/pb is 320*cd3a777bSXin LI * 3/0/2, the first byte is 0xA2. This way the first byte can never be 0x00. 321*cd3a777bSXin LI * Just like with LZMA2, lc + lp <= 4 must be true. The LZMA end-of-stream 322*cd3a777bSXin LI * marker must not be used. The unused values are reserved for future use. 323*cd3a777bSXin LI * This MicroLZMA header format was created for use in EROFS but may be used 324*cd3a777bSXin LI * by others too. 325*cd3a777bSXin LI */ 326*cd3a777bSXin LI extern struct xz_dec_microlzma *xz_dec_microlzma_alloc(enum xz_mode mode, 327*cd3a777bSXin LI uint32_t dict_size); 328*cd3a777bSXin LI 329*cd3a777bSXin LI /** 330*cd3a777bSXin LI * xz_dec_microlzma_reset() - Reset the MicroLZMA decoder state 331*cd3a777bSXin LI * @s Decoder state allocated using xz_dec_microlzma_alloc() 332*cd3a777bSXin LI * @comp_size Compressed size of the input stream 333*cd3a777bSXin LI * @uncomp_size Uncompressed size of the input stream. A value smaller 334*cd3a777bSXin LI * than the real uncompressed size of the input stream can 335*cd3a777bSXin LI * be specified if uncomp_size_is_exact is set to false. 336*cd3a777bSXin LI * uncomp_size can never be set to a value larger than the 337*cd3a777bSXin LI * expected real uncompressed size because it would eventually 338*cd3a777bSXin LI * result in XZ_DATA_ERROR. 339*cd3a777bSXin LI * @uncomp_size_is_exact This is an int instead of bool to avoid 340*cd3a777bSXin LI * requiring stdbool.h. This should normally be set to true. 341*cd3a777bSXin LI * When this is set to false, error detection is weaker. 342*cd3a777bSXin LI */ 343*cd3a777bSXin LI extern void xz_dec_microlzma_reset(struct xz_dec_microlzma *s, 344*cd3a777bSXin LI uint32_t comp_size, uint32_t uncomp_size, 345*cd3a777bSXin LI int uncomp_size_is_exact); 346*cd3a777bSXin LI 347*cd3a777bSXin LI /** 348*cd3a777bSXin LI * xz_dec_microlzma_run() - Run the MicroLZMA decoder 349*cd3a777bSXin LI * @s Decoder state initialized using xz_dec_microlzma_reset() 350*cd3a777bSXin LI * @b: Input and output buffers 351*cd3a777bSXin LI * 352*cd3a777bSXin LI * This works similarly to xz_dec_run() with a few important differences. 353*cd3a777bSXin LI * Only the differences are documented here. 354*cd3a777bSXin LI * 355*cd3a777bSXin LI * The only possible return values are XZ_OK, XZ_STREAM_END, and 356*cd3a777bSXin LI * XZ_DATA_ERROR. This function cannot return XZ_BUF_ERROR: if no progress 357*cd3a777bSXin LI * is possible due to lack of input data or output space, this function will 358*cd3a777bSXin LI * keep returning XZ_OK. Thus, the calling code must be written so that it 359*cd3a777bSXin LI * will eventually provide input and output space matching (or exceeding) 360*cd3a777bSXin LI * comp_size and uncomp_size arguments given to xz_dec_microlzma_reset(). 361*cd3a777bSXin LI * If the caller cannot do this (for example, if the input file is truncated 362*cd3a777bSXin LI * or otherwise corrupt), the caller must detect this error by itself to 363*cd3a777bSXin LI * avoid an infinite loop. 364*cd3a777bSXin LI * 365*cd3a777bSXin LI * If the compressed data seems to be corrupt, XZ_DATA_ERROR is returned. 366*cd3a777bSXin LI * This can happen also when incorrect dictionary, uncompressed, or 367*cd3a777bSXin LI * compressed sizes have been specified. 368*cd3a777bSXin LI * 369*cd3a777bSXin LI * With XZ_PREALLOC only: As an extra feature, b->out may be NULL to skip over 370*cd3a777bSXin LI * uncompressed data. This way the caller doesn't need to provide a temporary 371*cd3a777bSXin LI * output buffer for the bytes that will be ignored. 372*cd3a777bSXin LI * 373*cd3a777bSXin LI * With XZ_SINGLE only: In contrast to xz_dec_run(), the return value XZ_OK 374*cd3a777bSXin LI * is also possible and thus XZ_SINGLE is actually a limited multi-call mode. 375*cd3a777bSXin LI * After XZ_OK the bytes decoded so far may be read from the output buffer. 376*cd3a777bSXin LI * It is possible to continue decoding but the variables b->out and b->out_pos 377*cd3a777bSXin LI * MUST NOT be changed by the caller. Increasing the value of b->out_size is 378*cd3a777bSXin LI * allowed to make more output space available; one doesn't need to provide 379*cd3a777bSXin LI * space for the whole uncompressed data on the first call. The input buffer 380*cd3a777bSXin LI * may be changed normally like with XZ_PREALLOC. This way input data can be 381*cd3a777bSXin LI * provided from non-contiguous memory. 382*cd3a777bSXin LI */ 383*cd3a777bSXin LI extern enum xz_ret xz_dec_microlzma_run(struct xz_dec_microlzma *s, 384*cd3a777bSXin LI struct xz_buf *b); 385*cd3a777bSXin LI 386*cd3a777bSXin LI /** 387*cd3a777bSXin LI * xz_dec_microlzma_end() - Free the memory allocated for the decoder state 388*cd3a777bSXin LI * @s: Decoder state allocated using xz_dec_microlzma_alloc(). 389*cd3a777bSXin LI * If s is NULL, this function does nothing. 390*cd3a777bSXin LI */ 391*cd3a777bSXin LI extern void xz_dec_microlzma_end(struct xz_dec_microlzma *s); 392*cd3a777bSXin LI 393*cd3a777bSXin LI /* 39463dab8eeSAdrian Chadd * Standalone build (userspace build or in-kernel build for boot time use) 39563dab8eeSAdrian Chadd * needs a CRC32 implementation. For normal in-kernel use, kernel's own 39663dab8eeSAdrian Chadd * CRC32 module is used instead, and users of this module don't need to 39763dab8eeSAdrian Chadd * care about the functions below. 39863dab8eeSAdrian Chadd */ 39963dab8eeSAdrian Chadd #ifndef XZ_INTERNAL_CRC32 40063dab8eeSAdrian Chadd # ifdef __KERNEL__ 40163dab8eeSAdrian Chadd # define XZ_INTERNAL_CRC32 0 40263dab8eeSAdrian Chadd # else 40363dab8eeSAdrian Chadd # define XZ_INTERNAL_CRC32 1 40463dab8eeSAdrian Chadd # endif 40563dab8eeSAdrian Chadd #endif 40663dab8eeSAdrian Chadd 407f0bd5302SXin LI /* 408f0bd5302SXin LI * If CRC64 support has been enabled with XZ_USE_CRC64, a CRC64 409f0bd5302SXin LI * implementation is needed too. 410f0bd5302SXin LI */ 411f0bd5302SXin LI #ifndef XZ_USE_CRC64 412f0bd5302SXin LI # undef XZ_INTERNAL_CRC64 413f0bd5302SXin LI # define XZ_INTERNAL_CRC64 0 414f0bd5302SXin LI #endif 415f0bd5302SXin LI #ifndef XZ_INTERNAL_CRC64 416f0bd5302SXin LI # ifdef __KERNEL__ 417f0bd5302SXin LI # error Using CRC64 in the kernel has not been implemented. 418f0bd5302SXin LI # else 419f0bd5302SXin LI # define XZ_INTERNAL_CRC64 1 420f0bd5302SXin LI # endif 421f0bd5302SXin LI #endif 422f0bd5302SXin LI 42363dab8eeSAdrian Chadd #if XZ_INTERNAL_CRC32 42463dab8eeSAdrian Chadd /* 42563dab8eeSAdrian Chadd * This must be called before any other xz_* function to initialize 42663dab8eeSAdrian Chadd * the CRC32 lookup table. 42763dab8eeSAdrian Chadd */ 42863dab8eeSAdrian Chadd XZ_EXTERN void xz_crc32_init(void); 42963dab8eeSAdrian Chadd 43063dab8eeSAdrian Chadd /* 43163dab8eeSAdrian Chadd * Update CRC32 value using the polynomial from IEEE-802.3. To start a new 43263dab8eeSAdrian Chadd * calculation, the third argument must be zero. To continue the calculation, 43363dab8eeSAdrian Chadd * the previously returned value is passed as the third argument. 43463dab8eeSAdrian Chadd */ 43563dab8eeSAdrian Chadd XZ_EXTERN uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc); 43663dab8eeSAdrian Chadd #endif 43763dab8eeSAdrian Chadd 438f0bd5302SXin LI #if XZ_INTERNAL_CRC64 439f0bd5302SXin LI /* 440f0bd5302SXin LI * This must be called before any other xz_* function (except xz_crc32_init()) 441f0bd5302SXin LI * to initialize the CRC64 lookup table. 442f0bd5302SXin LI */ 443f0bd5302SXin LI XZ_EXTERN void xz_crc64_init(void); 444f0bd5302SXin LI 445f0bd5302SXin LI /* 446f0bd5302SXin LI * Update CRC64 value using the polynomial from ECMA-182. To start a new 447f0bd5302SXin LI * calculation, the third argument must be zero. To continue the calculation, 448f0bd5302SXin LI * the previously returned value is passed as the third argument. 449f0bd5302SXin LI */ 450f0bd5302SXin LI XZ_EXTERN uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc); 451f0bd5302SXin LI #endif 452f0bd5302SXin LI 45363dab8eeSAdrian Chadd #ifdef __cplusplus 45463dab8eeSAdrian Chadd } 45563dab8eeSAdrian Chadd #endif 45663dab8eeSAdrian Chadd 45763dab8eeSAdrian Chadd #endif 458