1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#ifndef ZSTD_CCOMMON_H_MODULE
18#define ZSTD_CCOMMON_H_MODULE
19
20
21
22
23#define FORCE_INLINE static __always_inline
24#define FORCE_NOINLINE static noinline
25
26
27
28
29#include "error_private.h"
30#include "mem.h"
31#include <linux/compiler.h>
32#include <linux/kernel.h>
33#include <linux/xxhash.h>
34#include <linux/zstd.h>
35
36
37
38
39#define MIN(a, b) ((a) < (b) ? (a) : (b))
40#define MAX(a, b) ((a) > (b) ? (a) : (b))
41#define CHECK_F(f) \
42 { \
43 size_t const errcod = f; \
44 if (ERR_isError(errcod)) \
45 return errcod; \
46 }
47#define CHECK_E(f, e) \
48 { \
49 size_t const errcod = f; \
50 if (ERR_isError(errcod)) \
51 return ERROR(e); \
52 }
53#define ZSTD_STATIC_ASSERT(c) \
54 { \
55 enum { ZSTD_static_assert = 1 / (int)(!!(c)) }; \
56 }
57
58
59
60
61#define ZSTD_OPT_NUM (1 << 12)
62#define ZSTD_DICT_MAGIC 0xEC30A437
63
64#define ZSTD_REP_NUM 3
65#define ZSTD_REP_CHECK (ZSTD_REP_NUM)
66#define ZSTD_REP_MOVE (ZSTD_REP_NUM - 1)
67#define ZSTD_REP_MOVE_OPT (ZSTD_REP_NUM)
68static const U32 repStartValue[ZSTD_REP_NUM] = {1, 4, 8};
69
70#define KB *(1 << 10)
71#define MB *(1 << 20)
72#define GB *(1U << 30)
73
74#define BIT7 128
75#define BIT6 64
76#define BIT5 32
77#define BIT4 16
78#define BIT1 2
79#define BIT0 1
80
81#define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
82static const size_t ZSTD_fcs_fieldSize[4] = {0, 2, 4, 8};
83static const size_t ZSTD_did_fieldSize[4] = {0, 1, 2, 4};
84
85#define ZSTD_BLOCKHEADERSIZE 3
86static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
87typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
88
89#define MIN_SEQUENCES_SIZE 1
90#define MIN_CBLOCK_SIZE (1 + 1 + MIN_SEQUENCES_SIZE )
91
92#define HufLog 12
93typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
94
95#define LONGNBSEQ 0x7F00
96
97#define MINMATCH 3
98#define EQUAL_READ32 4
99
100#define Litbits 8
101#define MaxLit ((1 << Litbits) - 1)
102#define MaxML 52
103#define MaxLL 35
104#define MaxOff 28
105#define MaxSeq MAX(MaxLL, MaxML)
106#define MLFSELog 9
107#define LLFSELog 9
108#define OffFSELog 8
109
110static const U32 LL_bits[MaxLL + 1] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
111static const S16 LL_defaultNorm[MaxLL + 1] = {4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1, -1, -1, -1, -1};
112#define LL_DEFAULTNORMLOG 6
113static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
114
115static const U32 ML_bits[MaxML + 1] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
116 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
117static const S16 ML_defaultNorm[MaxML + 1] = {1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
118 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1};
119#define ML_DEFAULTNORMLOG 6
120static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
121
122static const S16 OF_defaultNorm[MaxOff + 1] = {1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1};
123#define OF_DEFAULTNORMLOG 5
124static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
125
126
127
128
129ZSTD_STATIC void ZSTD_copy8(void *dst, const void *src) {
130
131
132
133
134
135
136
137 __builtin_memcpy(dst, src, 8);
138}
139
140
141#define WILDCOPY_OVERLENGTH 8
142ZSTD_STATIC void ZSTD_wildcopy(void *dst, const void *src, ptrdiff_t length)
143{
144 const BYTE* ip = (const BYTE*)src;
145 BYTE* op = (BYTE*)dst;
146 BYTE* const oend = op + length;
147#if defined(GCC_VERSION) && GCC_VERSION >= 70000 && GCC_VERSION < 70200
148
149
150
151
152
153
154 if (length <= 8)
155 return ZSTD_copy8(dst, src);
156#endif
157 do {
158 ZSTD_copy8(op, ip);
159 op += 8;
160 ip += 8;
161 } while (op < oend);
162}
163
164
165
166
167typedef struct ZSTD_stats_s ZSTD_stats_t;
168
169typedef struct {
170 U32 off;
171 U32 len;
172} ZSTD_match_t;
173
174typedef struct {
175 U32 price;
176 U32 off;
177 U32 mlen;
178 U32 litlen;
179 U32 rep[ZSTD_REP_NUM];
180} ZSTD_optimal_t;
181
182typedef struct seqDef_s {
183 U32 offset;
184 U16 litLength;
185 U16 matchLength;
186} seqDef;
187
188typedef struct {
189 seqDef *sequencesStart;
190 seqDef *sequences;
191 BYTE *litStart;
192 BYTE *lit;
193 BYTE *llCode;
194 BYTE *mlCode;
195 BYTE *ofCode;
196 U32 longLengthID;
197 U32 longLengthPos;
198
199 ZSTD_optimal_t *priceTable;
200 ZSTD_match_t *matchTable;
201 U32 *matchLengthFreq;
202 U32 *litLengthFreq;
203 U32 *litFreq;
204 U32 *offCodeFreq;
205 U32 matchLengthSum;
206 U32 matchSum;
207 U32 litLengthSum;
208 U32 litSum;
209 U32 offCodeSum;
210 U32 log2matchLengthSum;
211 U32 log2matchSum;
212 U32 log2litLengthSum;
213 U32 log2litSum;
214 U32 log2offCodeSum;
215 U32 factor;
216 U32 staticPrices;
217 U32 cachedPrice;
218 U32 cachedLitLength;
219 const BYTE *cachedLiterals;
220} seqStore_t;
221
222const seqStore_t *ZSTD_getSeqStore(const ZSTD_CCtx *ctx);
223void ZSTD_seqToCodes(const seqStore_t *seqStorePtr);
224int ZSTD_isSkipFrame(ZSTD_DCtx *dctx);
225
226
227typedef void *(*ZSTD_allocFunction)(void *opaque, size_t size);
228typedef void (*ZSTD_freeFunction)(void *opaque, void *address);
229typedef struct {
230 ZSTD_allocFunction customAlloc;
231 ZSTD_freeFunction customFree;
232 void *opaque;
233} ZSTD_customMem;
234
235void *ZSTD_malloc(size_t size, ZSTD_customMem customMem);
236void ZSTD_free(void *ptr, ZSTD_customMem customMem);
237
238
239
240typedef struct {
241 void *ptr;
242 const void *end;
243} ZSTD_stack;
244
245#define ZSTD_ALIGN(x) ALIGN(x, sizeof(size_t))
246#define ZSTD_PTR_ALIGN(p) PTR_ALIGN(p, sizeof(size_t))
247
248ZSTD_customMem ZSTD_initStack(void *workspace, size_t workspaceSize);
249
250void *ZSTD_stackAllocAll(void *opaque, size_t *size);
251void *ZSTD_stackAlloc(void *opaque, size_t size);
252void ZSTD_stackFree(void *opaque, void *address);
253
254
255
256ZSTD_STATIC U32 ZSTD_highbit32(U32 val) { return 31 - __builtin_clz(val); }
257
258
259
260
261
262
263
264void ZSTD_invalidateRepCodes(ZSTD_CCtx *cctx);
265
266size_t ZSTD_freeCCtx(ZSTD_CCtx *cctx);
267size_t ZSTD_freeDCtx(ZSTD_DCtx *dctx);
268size_t ZSTD_freeCDict(ZSTD_CDict *cdict);
269size_t ZSTD_freeDDict(ZSTD_DDict *cdict);
270size_t ZSTD_freeCStream(ZSTD_CStream *zcs);
271size_t ZSTD_freeDStream(ZSTD_DStream *zds);
272
273#endif
274