162 lines · c
1/* SPDX-License-Identifier: 0BSD */2 3/*4 * Private includes and definitions5 *6 * Author: Lasse Collin <lasse.collin@tukaani.org>7 */8 9#ifndef XZ_PRIVATE_H10#define XZ_PRIVATE_H11 12#ifdef __KERNEL__13# include <linux/xz.h>14# include <linux/kernel.h>15# include <linux/unaligned.h>16 /* XZ_PREBOOT may be defined only via decompress_unxz.c. */17# ifndef XZ_PREBOOT18# include <linux/slab.h>19# include <linux/vmalloc.h>20# include <linux/string.h>21# ifdef CONFIG_XZ_DEC_X8622# define XZ_DEC_X8623# endif24# ifdef CONFIG_XZ_DEC_POWERPC25# define XZ_DEC_POWERPC26# endif27# ifdef CONFIG_XZ_DEC_IA6428# define XZ_DEC_IA6429# endif30# ifdef CONFIG_XZ_DEC_ARM31# define XZ_DEC_ARM32# endif33# ifdef CONFIG_XZ_DEC_ARMTHUMB34# define XZ_DEC_ARMTHUMB35# endif36# ifdef CONFIG_XZ_DEC_SPARC37# define XZ_DEC_SPARC38# endif39# ifdef CONFIG_XZ_DEC_ARM6440# define XZ_DEC_ARM6441# endif42# ifdef CONFIG_XZ_DEC_RISCV43# define XZ_DEC_RISCV44# endif45# ifdef CONFIG_XZ_DEC_MICROLZMA46# define XZ_DEC_MICROLZMA47# endif48# define memeq(a, b, size) (memcmp(a, b, size) == 0)49# define memzero(buf, size) memset(buf, 0, size)50# endif51# define get_le32(p) le32_to_cpup((const uint32_t *)(p))52#else53 /*54 * For userspace builds, use a separate header to define the required55 * macros and functions. This makes it easier to adapt the code into56 * different environments and avoids clutter in the Linux kernel tree.57 */58# include "xz_config.h"59#endif60 61/* If no specific decoding mode is requested, enable support for all modes. */62#if !defined(XZ_DEC_SINGLE) && !defined(XZ_DEC_PREALLOC) \63 && !defined(XZ_DEC_DYNALLOC)64# define XZ_DEC_SINGLE65# define XZ_DEC_PREALLOC66# define XZ_DEC_DYNALLOC67#endif68 69/*70 * The DEC_IS_foo(mode) macros are used in "if" statements. If only some71 * of the supported modes are enabled, these macros will evaluate to true or72 * false at compile time and thus allow the compiler to omit unneeded code.73 */74#ifdef XZ_DEC_SINGLE75# define DEC_IS_SINGLE(mode) ((mode) == XZ_SINGLE)76#else77# define DEC_IS_SINGLE(mode) (false)78#endif79 80#ifdef XZ_DEC_PREALLOC81# define DEC_IS_PREALLOC(mode) ((mode) == XZ_PREALLOC)82#else83# define DEC_IS_PREALLOC(mode) (false)84#endif85 86#ifdef XZ_DEC_DYNALLOC87# define DEC_IS_DYNALLOC(mode) ((mode) == XZ_DYNALLOC)88#else89# define DEC_IS_DYNALLOC(mode) (false)90#endif91 92#if !defined(XZ_DEC_SINGLE)93# define DEC_IS_MULTI(mode) (true)94#elif defined(XZ_DEC_PREALLOC) || defined(XZ_DEC_DYNALLOC)95# define DEC_IS_MULTI(mode) ((mode) != XZ_SINGLE)96#else97# define DEC_IS_MULTI(mode) (false)98#endif99 100/*101 * If any of the BCJ filter decoders are wanted, define XZ_DEC_BCJ.102 * XZ_DEC_BCJ is used to enable generic support for BCJ decoders.103 */104#ifndef XZ_DEC_BCJ105# if defined(XZ_DEC_X86) || defined(XZ_DEC_POWERPC) \106 || defined(XZ_DEC_IA64) \107 || defined(XZ_DEC_ARM) || defined(XZ_DEC_ARMTHUMB) \108 || defined(XZ_DEC_SPARC) || defined(XZ_DEC_ARM64) \109 || defined(XZ_DEC_RISCV)110# define XZ_DEC_BCJ111# endif112#endif113 114/*115 * Allocate memory for LZMA2 decoder. xz_dec_lzma2_reset() must be used116 * before calling xz_dec_lzma2_run().117 */118struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode, uint32_t dict_max);119 120/*121 * Decode the LZMA2 properties (one byte) and reset the decoder. Return122 * XZ_OK on success, XZ_MEMLIMIT_ERROR if the preallocated dictionary is not123 * big enough, and XZ_OPTIONS_ERROR if props indicates something that this124 * decoder doesn't support.125 */126enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props);127 128/* Decode raw LZMA2 stream from b->in to b->out. */129enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s, struct xz_buf *b);130 131/* Free the memory allocated for the LZMA2 decoder. */132void xz_dec_lzma2_end(struct xz_dec_lzma2 *s);133 134#ifdef XZ_DEC_BCJ135/*136 * Allocate memory for BCJ decoders. xz_dec_bcj_reset() must be used before137 * calling xz_dec_bcj_run().138 */139struct xz_dec_bcj *xz_dec_bcj_create(bool single_call);140 141/*142 * Decode the Filter ID of a BCJ filter. This implementation doesn't143 * support custom start offsets, so no decoding of Filter Properties144 * is needed. Returns XZ_OK if the given Filter ID is supported.145 * Otherwise XZ_OPTIONS_ERROR is returned.146 */147enum xz_ret xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id);148 149/*150 * Decode raw BCJ + LZMA2 stream. This must be used only if there actually is151 * a BCJ filter in the chain. If the chain has only LZMA2, xz_dec_lzma2_run()152 * must be called directly.153 */154enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s, struct xz_dec_lzma2 *lzma2,155 struct xz_buf *b);156 157/* Free the memory allocated for the BCJ filters. */158#define xz_dec_bcj_end(s) kfree(s)159#endif160 161#endif162