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