xref: /kernel/linux/linux-5.10/lib/xz/xz_private.h (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Private includes and definitions
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Author: Lasse Collin <lasse.collin@tukaani.org>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This file has been put into the public domain.
78c2ecf20Sopenharmony_ci * You can do whatever you want with this file.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef XZ_PRIVATE_H
118c2ecf20Sopenharmony_ci#define XZ_PRIVATE_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#ifdef __KERNEL__
148c2ecf20Sopenharmony_ci#	include <linux/xz.h>
158c2ecf20Sopenharmony_ci#	include <linux/kernel.h>
168c2ecf20Sopenharmony_ci#	include <asm/unaligned.h>
178c2ecf20Sopenharmony_ci	/* XZ_PREBOOT may be defined only via decompress_unxz.c. */
188c2ecf20Sopenharmony_ci#	ifndef XZ_PREBOOT
198c2ecf20Sopenharmony_ci#		include <linux/slab.h>
208c2ecf20Sopenharmony_ci#		include <linux/vmalloc.h>
218c2ecf20Sopenharmony_ci#		include <linux/string.h>
228c2ecf20Sopenharmony_ci#		ifdef CONFIG_XZ_DEC_X86
238c2ecf20Sopenharmony_ci#			define XZ_DEC_X86
248c2ecf20Sopenharmony_ci#		endif
258c2ecf20Sopenharmony_ci#		ifdef CONFIG_XZ_DEC_POWERPC
268c2ecf20Sopenharmony_ci#			define XZ_DEC_POWERPC
278c2ecf20Sopenharmony_ci#		endif
288c2ecf20Sopenharmony_ci#		ifdef CONFIG_XZ_DEC_IA64
298c2ecf20Sopenharmony_ci#			define XZ_DEC_IA64
308c2ecf20Sopenharmony_ci#		endif
318c2ecf20Sopenharmony_ci#		ifdef CONFIG_XZ_DEC_ARM
328c2ecf20Sopenharmony_ci#			define XZ_DEC_ARM
338c2ecf20Sopenharmony_ci#		endif
348c2ecf20Sopenharmony_ci#		ifdef CONFIG_XZ_DEC_ARMTHUMB
358c2ecf20Sopenharmony_ci#			define XZ_DEC_ARMTHUMB
368c2ecf20Sopenharmony_ci#		endif
378c2ecf20Sopenharmony_ci#		ifdef CONFIG_XZ_DEC_SPARC
388c2ecf20Sopenharmony_ci#			define XZ_DEC_SPARC
398c2ecf20Sopenharmony_ci#		endif
408c2ecf20Sopenharmony_ci#		define memeq(a, b, size) (memcmp(a, b, size) == 0)
418c2ecf20Sopenharmony_ci#		define memzero(buf, size) memset(buf, 0, size)
428c2ecf20Sopenharmony_ci#	endif
438c2ecf20Sopenharmony_ci#	define get_le32(p) le32_to_cpup((const uint32_t *)(p))
448c2ecf20Sopenharmony_ci#else
458c2ecf20Sopenharmony_ci	/*
468c2ecf20Sopenharmony_ci	 * For userspace builds, use a separate header to define the required
478c2ecf20Sopenharmony_ci	 * macros and functions. This makes it easier to adapt the code into
488c2ecf20Sopenharmony_ci	 * different environments and avoids clutter in the Linux kernel tree.
498c2ecf20Sopenharmony_ci	 */
508c2ecf20Sopenharmony_ci#	include "xz_config.h"
518c2ecf20Sopenharmony_ci#endif
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* If no specific decoding mode is requested, enable support for all modes. */
548c2ecf20Sopenharmony_ci#if !defined(XZ_DEC_SINGLE) && !defined(XZ_DEC_PREALLOC) \
558c2ecf20Sopenharmony_ci		&& !defined(XZ_DEC_DYNALLOC)
568c2ecf20Sopenharmony_ci#	define XZ_DEC_SINGLE
578c2ecf20Sopenharmony_ci#	define XZ_DEC_PREALLOC
588c2ecf20Sopenharmony_ci#	define XZ_DEC_DYNALLOC
598c2ecf20Sopenharmony_ci#endif
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/*
628c2ecf20Sopenharmony_ci * The DEC_IS_foo(mode) macros are used in "if" statements. If only some
638c2ecf20Sopenharmony_ci * of the supported modes are enabled, these macros will evaluate to true or
648c2ecf20Sopenharmony_ci * false at compile time and thus allow the compiler to omit unneeded code.
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_ci#ifdef XZ_DEC_SINGLE
678c2ecf20Sopenharmony_ci#	define DEC_IS_SINGLE(mode) ((mode) == XZ_SINGLE)
688c2ecf20Sopenharmony_ci#else
698c2ecf20Sopenharmony_ci#	define DEC_IS_SINGLE(mode) (false)
708c2ecf20Sopenharmony_ci#endif
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#ifdef XZ_DEC_PREALLOC
738c2ecf20Sopenharmony_ci#	define DEC_IS_PREALLOC(mode) ((mode) == XZ_PREALLOC)
748c2ecf20Sopenharmony_ci#else
758c2ecf20Sopenharmony_ci#	define DEC_IS_PREALLOC(mode) (false)
768c2ecf20Sopenharmony_ci#endif
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#ifdef XZ_DEC_DYNALLOC
798c2ecf20Sopenharmony_ci#	define DEC_IS_DYNALLOC(mode) ((mode) == XZ_DYNALLOC)
808c2ecf20Sopenharmony_ci#else
818c2ecf20Sopenharmony_ci#	define DEC_IS_DYNALLOC(mode) (false)
828c2ecf20Sopenharmony_ci#endif
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#if !defined(XZ_DEC_SINGLE)
858c2ecf20Sopenharmony_ci#	define DEC_IS_MULTI(mode) (true)
868c2ecf20Sopenharmony_ci#elif defined(XZ_DEC_PREALLOC) || defined(XZ_DEC_DYNALLOC)
878c2ecf20Sopenharmony_ci#	define DEC_IS_MULTI(mode) ((mode) != XZ_SINGLE)
888c2ecf20Sopenharmony_ci#else
898c2ecf20Sopenharmony_ci#	define DEC_IS_MULTI(mode) (false)
908c2ecf20Sopenharmony_ci#endif
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/*
938c2ecf20Sopenharmony_ci * If any of the BCJ filter decoders are wanted, define XZ_DEC_BCJ.
948c2ecf20Sopenharmony_ci * XZ_DEC_BCJ is used to enable generic support for BCJ decoders.
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_ci#ifndef XZ_DEC_BCJ
978c2ecf20Sopenharmony_ci#	if defined(XZ_DEC_X86) || defined(XZ_DEC_POWERPC) \
988c2ecf20Sopenharmony_ci			|| defined(XZ_DEC_IA64) || defined(XZ_DEC_ARM) \
998c2ecf20Sopenharmony_ci			|| defined(XZ_DEC_ARM) || defined(XZ_DEC_ARMTHUMB) \
1008c2ecf20Sopenharmony_ci			|| defined(XZ_DEC_SPARC)
1018c2ecf20Sopenharmony_ci#		define XZ_DEC_BCJ
1028c2ecf20Sopenharmony_ci#	endif
1038c2ecf20Sopenharmony_ci#endif
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#ifndef CRC32_POLY_LE
1068c2ecf20Sopenharmony_ci#define CRC32_POLY_LE 0xedb88320
1078c2ecf20Sopenharmony_ci#endif
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/*
1108c2ecf20Sopenharmony_ci * Allocate memory for LZMA2 decoder. xz_dec_lzma2_reset() must be used
1118c2ecf20Sopenharmony_ci * before calling xz_dec_lzma2_run().
1128c2ecf20Sopenharmony_ci */
1138c2ecf20Sopenharmony_ciXZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode,
1148c2ecf20Sopenharmony_ci						   uint32_t dict_max);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/*
1178c2ecf20Sopenharmony_ci * Decode the LZMA2 properties (one byte) and reset the decoder. Return
1188c2ecf20Sopenharmony_ci * XZ_OK on success, XZ_MEMLIMIT_ERROR if the preallocated dictionary is not
1198c2ecf20Sopenharmony_ci * big enough, and XZ_OPTIONS_ERROR if props indicates something that this
1208c2ecf20Sopenharmony_ci * decoder doesn't support.
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_ciXZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s,
1238c2ecf20Sopenharmony_ci					 uint8_t props);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/* Decode raw LZMA2 stream from b->in to b->out. */
1268c2ecf20Sopenharmony_ciXZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s,
1278c2ecf20Sopenharmony_ci				       struct xz_buf *b);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/* Free the memory allocated for the LZMA2 decoder. */
1308c2ecf20Sopenharmony_ciXZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#ifdef XZ_DEC_BCJ
1338c2ecf20Sopenharmony_ci/*
1348c2ecf20Sopenharmony_ci * Allocate memory for BCJ decoders. xz_dec_bcj_reset() must be used before
1358c2ecf20Sopenharmony_ci * calling xz_dec_bcj_run().
1368c2ecf20Sopenharmony_ci */
1378c2ecf20Sopenharmony_ciXZ_EXTERN struct xz_dec_bcj *xz_dec_bcj_create(bool single_call);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/*
1408c2ecf20Sopenharmony_ci * Decode the Filter ID of a BCJ filter. This implementation doesn't
1418c2ecf20Sopenharmony_ci * support custom start offsets, so no decoding of Filter Properties
1428c2ecf20Sopenharmony_ci * is needed. Returns XZ_OK if the given Filter ID is supported.
1438c2ecf20Sopenharmony_ci * Otherwise XZ_OPTIONS_ERROR is returned.
1448c2ecf20Sopenharmony_ci */
1458c2ecf20Sopenharmony_ciXZ_EXTERN enum xz_ret xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci/*
1488c2ecf20Sopenharmony_ci * Decode raw BCJ + LZMA2 stream. This must be used only if there actually is
1498c2ecf20Sopenharmony_ci * a BCJ filter in the chain. If the chain has only LZMA2, xz_dec_lzma2_run()
1508c2ecf20Sopenharmony_ci * must be called directly.
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_ciXZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s,
1538c2ecf20Sopenharmony_ci				     struct xz_dec_lzma2 *lzma2,
1548c2ecf20Sopenharmony_ci				     struct xz_buf *b);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci/* Free the memory allocated for the BCJ filters. */
1578c2ecf20Sopenharmony_ci#define xz_dec_bcj_end(s) kfree(s)
1588c2ecf20Sopenharmony_ci#endif
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#endif
161