blob: 31a814420b0b39d9bebb7f2bf0dc53a29bb2d8ff [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
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#define CONFIG_XZ_DEC_ARMTHUMB
14
15#ifdef __KERNEL__
16#include <common.h>
17#include <linux/types.h>
18#include <asm/byteorder.h>
19#include <asm/unaligned.h>
20#include <malloc.h>
21#include <xz.h>
22 /* XZ_PREBOOT may be defined only via decompress_unxz.c. */
23# ifndef XZ_PREBOOT
24# include <linux/slab.h>
25# include <linux/vmalloc.h>
26# include <linux/string.h>
27# ifdef CONFIG_XZ_DEC_X86
28# define XZ_DEC_X86
29# endif
30# ifdef CONFIG_XZ_DEC_POWERPC
31# define XZ_DEC_POWERPC
32# endif
33# ifdef CONFIG_XZ_DEC_IA64
34# define XZ_DEC_IA64
35# endif
36# ifdef CONFIG_XZ_DEC_ARM
37# define XZ_DEC_ARM
38# endif
39# ifdef CONFIG_XZ_DEC_ARMTHUMB
40# define XZ_DEC_ARMTHUMB
41# endif
42# ifdef CONFIG_XZ_DEC_SPARC
43# define XZ_DEC_SPARC
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# ifdef CONFIG_XZ_DEC_ARMTHUMB
58# define XZ_DEC_ARMTHUMB
59# endif
60
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_SINGLE
65# define XZ_DEC_PREALLOC
66# define XZ_DEC_DYNALLOC
67#endif
68
69/*
70 * The DEC_IS_foo(mode) macros are used in "if" statements. If only some
71 * of the supported modes are enabled, these macros will evaluate to true or
72 * false at compile time and thus allow the compiler to omit unneeded code.
73 */
74#ifdef XZ_DEC_SINGLE
75# define DEC_IS_SINGLE(mode) ((mode) == XZ_SINGLE)
76#else
77# define DEC_IS_SINGLE(mode) (false)
78#endif
79
80#ifdef XZ_DEC_PREALLOC
81# define DEC_IS_PREALLOC(mode) ((mode) == XZ_PREALLOC)
82#else
83# define DEC_IS_PREALLOC(mode) (false)
84#endif
85
86#ifdef XZ_DEC_DYNALLOC
87# define DEC_IS_DYNALLOC(mode) ((mode) == XZ_DYNALLOC)
88#else
89# define DEC_IS_DYNALLOC(mode) (false)
90#endif
91
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#else
97# define DEC_IS_MULTI(mode) (false)
98#endif
99
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_BCJ
105# if defined(XZ_DEC_X86) || defined(XZ_DEC_POWERPC) \
106 || defined(XZ_DEC_IA64) || defined(XZ_DEC_ARM) \
107 || defined(XZ_DEC_ARM) || defined(XZ_DEC_ARMTHUMB) \
108 || defined(XZ_DEC_SPARC)
109# define XZ_DEC_BCJ
110# endif
111#endif
112
113/*
114 * Allocate memory for LZMA2 decoder. xz_dec_lzma2_reset() must be used
115 * before calling xz_dec_lzma2_run().
116 */
117XZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode,
118 uint32_t dict_max);
119
120/*
121 * Decode the LZMA2 properties (one byte) and reset the decoder. Return
122 * XZ_OK on success, XZ_MEMLIMIT_ERROR if the preallocated dictionary is not
123 * big enough, and XZ_OPTIONS_ERROR if props indicates something that this
124 * decoder doesn't support.
125 */
126XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s,
127 uint8_t props);
128
129/* Decode raw LZMA2 stream from b->in to b->out. */
130XZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s,
131 struct xz_buf *b);
132
133/* Free the memory allocated for the LZMA2 decoder. */
134XZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s);
135
136#ifdef XZ_DEC_BCJ
137/*
138 * Allocate memory for BCJ decoders. xz_dec_bcj_reset() must be used before
139 * calling xz_dec_bcj_run().
140 */
141XZ_EXTERN struct xz_dec_bcj *xz_dec_bcj_create(bool single_call);
142
143/*
144 * Decode the Filter ID of a BCJ filter. This implementation doesn't
145 * support custom start offsets, so no decoding of Filter Properties
146 * is needed. Returns XZ_OK if the given Filter ID is supported.
147 * Otherwise XZ_OPTIONS_ERROR is returned.
148 */
149XZ_EXTERN enum xz_ret xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id);
150
151/*
152 * Decode raw BCJ + LZMA2 stream. This must be used only if there actually is
153 * a BCJ filter in the chain. If the chain has only LZMA2, xz_dec_lzma2_run()
154 * must be called directly.
155 */
156XZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s,
157 struct xz_dec_lzma2 *lzma2,
158 struct xz_buf *b);
159
160/* Free the memory allocated for the BCJ filters. */
161#define xz_dec_bcj_end(s) kfree(s)
162#endif
163
164#endif