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