xref: /linux/lib/xz/xz_private.h (revision 7ec462100ef9142344ddbf86f2c3008b97acddbe)
1836d13a6SLasse Collin /* SPDX-License-Identifier: 0BSD */
2836d13a6SLasse Collin 
324fa0402SLasse Collin /*
424fa0402SLasse Collin  * Private includes and definitions
524fa0402SLasse Collin  *
624fa0402SLasse Collin  * Author: Lasse Collin <lasse.collin@tukaani.org>
724fa0402SLasse Collin  */
824fa0402SLasse Collin 
924fa0402SLasse Collin #ifndef XZ_PRIVATE_H
1024fa0402SLasse Collin #define XZ_PRIVATE_H
1124fa0402SLasse Collin 
1224fa0402SLasse Collin #ifdef __KERNEL__
1324fa0402SLasse Collin #	include <linux/xz.h>
1481d67439SLasse Collin #	include <linux/kernel.h>
15*5f60d5f6SAl Viro #	include <linux/unaligned.h>
1624fa0402SLasse Collin 	/* XZ_PREBOOT may be defined only via decompress_unxz.c. */
1724fa0402SLasse Collin #	ifndef XZ_PREBOOT
1824fa0402SLasse Collin #		include <linux/slab.h>
1924fa0402SLasse Collin #		include <linux/vmalloc.h>
2024fa0402SLasse Collin #		include <linux/string.h>
2124fa0402SLasse Collin #		ifdef CONFIG_XZ_DEC_X86
2224fa0402SLasse Collin #			define XZ_DEC_X86
2324fa0402SLasse Collin #		endif
2424fa0402SLasse Collin #		ifdef CONFIG_XZ_DEC_POWERPC
2524fa0402SLasse Collin #			define XZ_DEC_POWERPC
2624fa0402SLasse Collin #		endif
2724fa0402SLasse Collin #		ifdef CONFIG_XZ_DEC_IA64
2824fa0402SLasse Collin #			define XZ_DEC_IA64
2924fa0402SLasse Collin #		endif
3024fa0402SLasse Collin #		ifdef CONFIG_XZ_DEC_ARM
3124fa0402SLasse Collin #			define XZ_DEC_ARM
3224fa0402SLasse Collin #		endif
3324fa0402SLasse Collin #		ifdef CONFIG_XZ_DEC_ARMTHUMB
3424fa0402SLasse Collin #			define XZ_DEC_ARMTHUMB
3524fa0402SLasse Collin #		endif
3624fa0402SLasse Collin #		ifdef CONFIG_XZ_DEC_SPARC
3724fa0402SLasse Collin #			define XZ_DEC_SPARC
3824fa0402SLasse Collin #		endif
394b62813fSLasse Collin #		ifdef CONFIG_XZ_DEC_ARM64
404b62813fSLasse Collin #			define XZ_DEC_ARM64
414b62813fSLasse Collin #		endif
4293d09773SLasse Collin #		ifdef CONFIG_XZ_DEC_RISCV
4393d09773SLasse Collin #			define XZ_DEC_RISCV
4493d09773SLasse Collin #		endif
45aaa2975fSLasse Collin #		ifdef CONFIG_XZ_DEC_MICROLZMA
46aaa2975fSLasse Collin #			define XZ_DEC_MICROLZMA
47aaa2975fSLasse Collin #		endif
4824fa0402SLasse Collin #		define memeq(a, b, size) (memcmp(a, b, size) == 0)
4924fa0402SLasse Collin #		define memzero(buf, size) memset(buf, 0, size)
5024fa0402SLasse Collin #	endif
5124fa0402SLasse Collin #	define get_le32(p) le32_to_cpup((const uint32_t *)(p))
5224fa0402SLasse Collin #else
5324fa0402SLasse Collin 	/*
5424fa0402SLasse Collin 	 * For userspace builds, use a separate header to define the required
5524fa0402SLasse Collin 	 * macros and functions. This makes it easier to adapt the code into
5624fa0402SLasse Collin 	 * different environments and avoids clutter in the Linux kernel tree.
5724fa0402SLasse Collin 	 */
5824fa0402SLasse Collin #	include "xz_config.h"
5924fa0402SLasse Collin #endif
6024fa0402SLasse Collin 
6124fa0402SLasse Collin /* If no specific decoding mode is requested, enable support for all modes. */
6224fa0402SLasse Collin #if !defined(XZ_DEC_SINGLE) && !defined(XZ_DEC_PREALLOC) \
6324fa0402SLasse Collin 		&& !defined(XZ_DEC_DYNALLOC)
6424fa0402SLasse Collin #	define XZ_DEC_SINGLE
6524fa0402SLasse Collin #	define XZ_DEC_PREALLOC
6624fa0402SLasse Collin #	define XZ_DEC_DYNALLOC
6724fa0402SLasse Collin #endif
6824fa0402SLasse Collin 
6924fa0402SLasse Collin /*
7024fa0402SLasse Collin  * The DEC_IS_foo(mode) macros are used in "if" statements. If only some
7124fa0402SLasse Collin  * of the supported modes are enabled, these macros will evaluate to true or
7224fa0402SLasse Collin  * false at compile time and thus allow the compiler to omit unneeded code.
7324fa0402SLasse Collin  */
7424fa0402SLasse Collin #ifdef XZ_DEC_SINGLE
7524fa0402SLasse Collin #	define DEC_IS_SINGLE(mode) ((mode) == XZ_SINGLE)
7624fa0402SLasse Collin #else
7724fa0402SLasse Collin #	define DEC_IS_SINGLE(mode) (false)
7824fa0402SLasse Collin #endif
7924fa0402SLasse Collin 
8024fa0402SLasse Collin #ifdef XZ_DEC_PREALLOC
8124fa0402SLasse Collin #	define DEC_IS_PREALLOC(mode) ((mode) == XZ_PREALLOC)
8224fa0402SLasse Collin #else
8324fa0402SLasse Collin #	define DEC_IS_PREALLOC(mode) (false)
8424fa0402SLasse Collin #endif
8524fa0402SLasse Collin 
8624fa0402SLasse Collin #ifdef XZ_DEC_DYNALLOC
8724fa0402SLasse Collin #	define DEC_IS_DYNALLOC(mode) ((mode) == XZ_DYNALLOC)
8824fa0402SLasse Collin #else
8924fa0402SLasse Collin #	define DEC_IS_DYNALLOC(mode) (false)
9024fa0402SLasse Collin #endif
9124fa0402SLasse Collin 
9224fa0402SLasse Collin #if !defined(XZ_DEC_SINGLE)
9324fa0402SLasse Collin #	define DEC_IS_MULTI(mode) (true)
9424fa0402SLasse Collin #elif defined(XZ_DEC_PREALLOC) || defined(XZ_DEC_DYNALLOC)
9524fa0402SLasse Collin #	define DEC_IS_MULTI(mode) ((mode) != XZ_SINGLE)
9624fa0402SLasse Collin #else
9724fa0402SLasse Collin #	define DEC_IS_MULTI(mode) (false)
9824fa0402SLasse Collin #endif
9924fa0402SLasse Collin 
10024fa0402SLasse Collin /*
10124fa0402SLasse Collin  * If any of the BCJ filter decoders are wanted, define XZ_DEC_BCJ.
10224fa0402SLasse Collin  * XZ_DEC_BCJ is used to enable generic support for BCJ decoders.
10324fa0402SLasse Collin  */
10424fa0402SLasse Collin #ifndef XZ_DEC_BCJ
10524fa0402SLasse Collin #	if defined(XZ_DEC_X86) || defined(XZ_DEC_POWERPC) \
1064b62813fSLasse Collin 			|| defined(XZ_DEC_IA64) \
10724fa0402SLasse Collin 			|| defined(XZ_DEC_ARM) || defined(XZ_DEC_ARMTHUMB) \
10893d09773SLasse Collin 			|| defined(XZ_DEC_SPARC) || defined(XZ_DEC_ARM64) \
10993d09773SLasse Collin 			|| defined(XZ_DEC_RISCV)
11024fa0402SLasse Collin #		define XZ_DEC_BCJ
11124fa0402SLasse Collin #	endif
11224fa0402SLasse Collin #endif
11324fa0402SLasse Collin 
11424fa0402SLasse Collin /*
11524fa0402SLasse Collin  * Allocate memory for LZMA2 decoder. xz_dec_lzma2_reset() must be used
11624fa0402SLasse Collin  * before calling xz_dec_lzma2_run().
11724fa0402SLasse Collin  */
118c6f371baSLasse Collin struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode, uint32_t dict_max);
11924fa0402SLasse Collin 
12024fa0402SLasse Collin /*
12124fa0402SLasse Collin  * Decode the LZMA2 properties (one byte) and reset the decoder. Return
12224fa0402SLasse Collin  * XZ_OK on success, XZ_MEMLIMIT_ERROR if the preallocated dictionary is not
12324fa0402SLasse Collin  * big enough, and XZ_OPTIONS_ERROR if props indicates something that this
12424fa0402SLasse Collin  * decoder doesn't support.
12524fa0402SLasse Collin  */
126c6f371baSLasse Collin enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props);
12724fa0402SLasse Collin 
12824fa0402SLasse Collin /* Decode raw LZMA2 stream from b->in to b->out. */
129c6f371baSLasse Collin enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s, struct xz_buf *b);
13024fa0402SLasse Collin 
13124fa0402SLasse Collin /* Free the memory allocated for the LZMA2 decoder. */
132c6f371baSLasse Collin void xz_dec_lzma2_end(struct xz_dec_lzma2 *s);
13324fa0402SLasse Collin 
13424fa0402SLasse Collin #ifdef XZ_DEC_BCJ
13524fa0402SLasse Collin /*
13624fa0402SLasse Collin  * Allocate memory for BCJ decoders. xz_dec_bcj_reset() must be used before
13724fa0402SLasse Collin  * calling xz_dec_bcj_run().
13824fa0402SLasse Collin  */
139c6f371baSLasse Collin struct xz_dec_bcj *xz_dec_bcj_create(bool single_call);
14024fa0402SLasse Collin 
14124fa0402SLasse Collin /*
14224fa0402SLasse Collin  * Decode the Filter ID of a BCJ filter. This implementation doesn't
14324fa0402SLasse Collin  * support custom start offsets, so no decoding of Filter Properties
14424fa0402SLasse Collin  * is needed. Returns XZ_OK if the given Filter ID is supported.
14524fa0402SLasse Collin  * Otherwise XZ_OPTIONS_ERROR is returned.
14624fa0402SLasse Collin  */
147c6f371baSLasse Collin enum xz_ret xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id);
14824fa0402SLasse Collin 
14924fa0402SLasse Collin /*
15024fa0402SLasse Collin  * Decode raw BCJ + LZMA2 stream. This must be used only if there actually is
15124fa0402SLasse Collin  * a BCJ filter in the chain. If the chain has only LZMA2, xz_dec_lzma2_run()
15224fa0402SLasse Collin  * must be called directly.
15324fa0402SLasse Collin  */
154c6f371baSLasse Collin enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s, struct xz_dec_lzma2 *lzma2,
15524fa0402SLasse Collin 			   struct xz_buf *b);
15624fa0402SLasse Collin 
15724fa0402SLasse Collin /* Free the memory allocated for the BCJ filters. */
15824fa0402SLasse Collin #define xz_dec_bcj_end(s) kfree(s)
15924fa0402SLasse Collin #endif
16024fa0402SLasse Collin 
16124fa0402SLasse Collin #endif
162