1 /* SPDX-License-Identifier: 0BSD */ 2 3 /* 4 * Private includes and definitions 5 * 6 * Author: Lasse Collin <lasse.collin@tukaani.org> 7 */ 8 9 #ifndef XZ_PRIVATE_H 10 #define XZ_PRIVATE_H 11 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_PREBOOT 18 # include <linux/slab.h> 19 # include <linux/vmalloc.h> 20 # include <linux/string.h> 21 # ifdef CONFIG_XZ_DEC_X86 22 # define XZ_DEC_X86 23 # endif 24 # ifdef CONFIG_XZ_DEC_POWERPC 25 # define XZ_DEC_POWERPC 26 # endif 27 # ifdef CONFIG_XZ_DEC_ARM 28 # define XZ_DEC_ARM 29 # endif 30 # ifdef CONFIG_XZ_DEC_ARMTHUMB 31 # define XZ_DEC_ARMTHUMB 32 # endif 33 # ifdef CONFIG_XZ_DEC_SPARC 34 # define XZ_DEC_SPARC 35 # endif 36 # ifdef CONFIG_XZ_DEC_ARM64 37 # define XZ_DEC_ARM64 38 # endif 39 # ifdef CONFIG_XZ_DEC_RISCV 40 # define XZ_DEC_RISCV 41 # endif 42 # ifdef CONFIG_XZ_DEC_MICROLZMA 43 # define XZ_DEC_MICROLZMA 44 # endif 45 # define memeq(a, b, size) (memcmp(a, b, size) == 0) 46 # define memzero(buf, size) memset(buf, 0, size) 47 # endif 48 # define get_le32(p) le32_to_cpup((const uint32_t *)(p)) 49 #else 50 /* 51 * For userspace builds, use a separate header to define the required 52 * macros and functions. This makes it easier to adapt the code into 53 * different environments and avoids clutter in the Linux kernel tree. 54 */ 55 # include "xz_config.h" 56 #endif 57 58 /* If no specific decoding mode is requested, enable support for all modes. */ 59 #if !defined(XZ_DEC_SINGLE) && !defined(XZ_DEC_PREALLOC) \ 60 && !defined(XZ_DEC_DYNALLOC) 61 # define XZ_DEC_SINGLE 62 # define XZ_DEC_PREALLOC 63 # define XZ_DEC_DYNALLOC 64 #endif 65 66 /* 67 * The DEC_IS_foo(mode) macros are used in "if" statements. If only some 68 * of the supported modes are enabled, these macros will evaluate to true or 69 * false at compile time and thus allow the compiler to omit unneeded code. 70 */ 71 #ifdef XZ_DEC_SINGLE 72 # define DEC_IS_SINGLE(mode) ((mode) == XZ_SINGLE) 73 #else 74 # define DEC_IS_SINGLE(mode) (false) 75 #endif 76 77 #ifdef XZ_DEC_PREALLOC 78 # define DEC_IS_PREALLOC(mode) ((mode) == XZ_PREALLOC) 79 #else 80 # define DEC_IS_PREALLOC(mode) (false) 81 #endif 82 83 #ifdef XZ_DEC_DYNALLOC 84 # define DEC_IS_DYNALLOC(mode) ((mode) == XZ_DYNALLOC) 85 #else 86 # define DEC_IS_DYNALLOC(mode) (false) 87 #endif 88 89 #if !defined(XZ_DEC_SINGLE) 90 # define DEC_IS_MULTI(mode) (true) 91 #elif defined(XZ_DEC_PREALLOC) || defined(XZ_DEC_DYNALLOC) 92 # define DEC_IS_MULTI(mode) ((mode) != XZ_SINGLE) 93 #else 94 # define DEC_IS_MULTI(mode) (false) 95 #endif 96 97 /* 98 * If any of the BCJ filter decoders are wanted, define XZ_DEC_BCJ. 99 * XZ_DEC_BCJ is used to enable generic support for BCJ decoders. 100 */ 101 #ifndef XZ_DEC_BCJ 102 # if defined(XZ_DEC_X86) || defined(XZ_DEC_POWERPC) \ 103 || defined(XZ_DEC_ARM) || defined(XZ_DEC_ARMTHUMB) \ 104 || defined(XZ_DEC_SPARC) || defined(XZ_DEC_ARM64) \ 105 || defined(XZ_DEC_RISCV) 106 # define XZ_DEC_BCJ 107 # endif 108 #endif 109 110 /* 111 * Allocate memory for LZMA2 decoder. xz_dec_lzma2_reset() must be used 112 * before calling xz_dec_lzma2_run(). 113 */ 114 struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode, uint32_t dict_max); 115 116 /* 117 * Decode the LZMA2 properties (one byte) and reset the decoder. Return 118 * XZ_OK on success, XZ_MEMLIMIT_ERROR if the preallocated dictionary is not 119 * big enough, and XZ_OPTIONS_ERROR if props indicates something that this 120 * decoder doesn't support. 121 */ 122 enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props); 123 124 /* Decode raw LZMA2 stream from b->in to b->out. */ 125 enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s, struct xz_buf *b); 126 127 /* Free the memory allocated for the LZMA2 decoder. */ 128 void xz_dec_lzma2_end(struct xz_dec_lzma2 *s); 129 130 #ifdef XZ_DEC_BCJ 131 /* 132 * Allocate memory for BCJ decoders. xz_dec_bcj_reset() must be used before 133 * calling xz_dec_bcj_run(). 134 */ 135 struct xz_dec_bcj *xz_dec_bcj_create(bool single_call); 136 137 /* 138 * Decode the Filter ID of a BCJ filter. This implementation doesn't 139 * support custom start offsets, so no decoding of Filter Properties 140 * is needed. Returns XZ_OK if the given Filter ID is supported. 141 * Otherwise XZ_OPTIONS_ERROR is returned. 142 */ 143 enum xz_ret xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id); 144 145 /* 146 * Decode raw BCJ + LZMA2 stream. This must be used only if there actually is 147 * a BCJ filter in the chain. If the chain has only LZMA2, xz_dec_lzma2_run() 148 * must be called directly. 149 */ 150 enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s, struct xz_dec_lzma2 *lzma2, 151 struct xz_buf *b); 152 153 /* Free the memory allocated for the BCJ filters. */ 154 #define xz_dec_bcj_end(s) kfree(s) 155 #endif 156 157 #endif 158