1 /**
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of https://github.com/facebook/zstd.
7 *
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License version 2 as published by the
10 * Free Software Foundation. This program is dual-licensed; you may select
11 * either version 2 of the GNU General Public License ("GPL") or BSD license
12 * ("BSD").
13 */
14
15 #ifndef ZSTD_CCOMMON_H_MODULE
16 #define ZSTD_CCOMMON_H_MODULE
17
18 /*-*******************************************************
19 * Compiler specifics
20 *********************************************************/
21 #define FORCE_INLINE static __always_inline
22 #define FORCE_NOINLINE static noinline
23
24 /*-*************************************
25 * Dependencies
26 ***************************************/
27 #include "error_private.h"
28 #include "mem.h"
29 #include <linux/compiler.h>
30 #include <linux/kernel.h>
31 #include <linux/xxhash.h>
32 #include <linux/zstd.h>
33
34 /*-*************************************
35 * shared macros
36 ***************************************/
37 #define MIN(a, b) ((a) < (b) ? (a) : (b))
38 #define MAX(a, b) ((a) > (b) ? (a) : (b))
39 #define CHECK_F(f) \
40 { \
41 size_t const errcod = f; \
42 if (ERR_isError(errcod)) \
43 return errcod; \
44 } /* check and Forward error code */
45 #define CHECK_E(f, e) \
46 { \
47 size_t const errcod = f; \
48 if (ERR_isError(errcod)) \
49 return ERROR(e); \
50 } /* check and send Error code */
51 #define ZSTD_STATIC_ASSERT(c) \
52 { \
53 enum { ZSTD_static_assert = 1 / (int)(!!(c)) }; \
54 }
55
56 /*-*************************************
57 * Common constants
58 ***************************************/
59 #define ZSTD_OPT_NUM (1 << 12)
60 #define ZSTD_DICT_MAGIC 0xEC30A437 /* v0.7+ */
61
62 #define ZSTD_REP_NUM 3 /* number of repcodes */
63 #define ZSTD_REP_CHECK (ZSTD_REP_NUM) /* number of repcodes to check by the optimal parser */
64 #define ZSTD_REP_MOVE (ZSTD_REP_NUM - 1)
65 #define ZSTD_REP_MOVE_OPT (ZSTD_REP_NUM)
66 static const U32 repStartValue[ZSTD_REP_NUM] = {1, 4, 8};
67
68 #define KB *(1 << 10)
69 #define MB *(1 << 20)
70 #define GB *(1U << 30)
71
72 #define BIT7 128
73 #define BIT6 64
74 #define BIT5 32
75 #define BIT4 16
76 #define BIT1 2
77 #define BIT0 1
78
79 #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
80 static const size_t ZSTD_fcs_fieldSize[4] = {0, 2, 4, 8};
81 static const size_t ZSTD_did_fieldSize[4] = {0, 1, 2, 4};
82
83 #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
84 static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
85 typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
86
87 #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
88 #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
89
90 #define HufLog 12
91 typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
92
93 #define LONGNBSEQ 0x7F00
94
95 #define MINMATCH 3
96 #define EQUAL_READ32 4
97
98 #define Litbits 8
99 #define MaxLit ((1 << Litbits) - 1)
100 #define MaxML 52
101 #define MaxLL 35
102 #define MaxOff 28
103 #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
104 #define MLFSELog 9
105 #define LLFSELog 9
106 #define OffFSELog 8
107
108 static 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};
109 static 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};
110 #define LL_DEFAULTNORMLOG 6 /* for static allocation */
111 static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
112
113 static 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,
114 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};
115 static 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,
116 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};
117 #define ML_DEFAULTNORMLOG 6 /* for static allocation */
118 static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
119
120 static 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};
121 #define OF_DEFAULTNORMLOG 5 /* for static allocation */
122 static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
123
124 /*-*******************************************
125 * Shared functions to include for inlining
126 *********************************************/
ZSTD_copy8(void * dst,const void * src)127 ZSTD_STATIC void ZSTD_copy8(void *dst, const void *src) {
128 memcpy(dst, src, 8);
129 }
130 /*! ZSTD_wildcopy() :
131 * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
132 #define WILDCOPY_OVERLENGTH 8
ZSTD_wildcopy(void * dst,const void * src,ptrdiff_t length)133 ZSTD_STATIC void ZSTD_wildcopy(void *dst, const void *src, ptrdiff_t length)
134 {
135 const BYTE* ip = (const BYTE*)src;
136 BYTE* op = (BYTE*)dst;
137 BYTE* const oend = op + length;
138 /* Work around https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81388.
139 * Avoid the bad case where the loop only runs once by handling the
140 * special case separately. This doesn't trigger the bug because it
141 * doesn't involve pointer/integer overflow.
142 */
143 if (length <= 8)
144 return ZSTD_copy8(dst, src);
145 do {
146 ZSTD_copy8(op, ip);
147 op += 8;
148 ip += 8;
149 } while (op < oend);
150 }
151
152 /*-*******************************************
153 * Private interfaces
154 *********************************************/
155 typedef struct ZSTD_stats_s ZSTD_stats_t;
156
157 typedef struct {
158 U32 off;
159 U32 len;
160 } ZSTD_match_t;
161
162 typedef struct {
163 U32 price;
164 U32 off;
165 U32 mlen;
166 U32 litlen;
167 U32 rep[ZSTD_REP_NUM];
168 } ZSTD_optimal_t;
169
170 typedef struct seqDef_s {
171 U32 offset;
172 U16 litLength;
173 U16 matchLength;
174 } seqDef;
175
176 typedef struct {
177 seqDef *sequencesStart;
178 seqDef *sequences;
179 BYTE *litStart;
180 BYTE *lit;
181 BYTE *llCode;
182 BYTE *mlCode;
183 BYTE *ofCode;
184 U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
185 U32 longLengthPos;
186 /* opt */
187 ZSTD_optimal_t *priceTable;
188 ZSTD_match_t *matchTable;
189 U32 *matchLengthFreq;
190 U32 *litLengthFreq;
191 U32 *litFreq;
192 U32 *offCodeFreq;
193 U32 matchLengthSum;
194 U32 matchSum;
195 U32 litLengthSum;
196 U32 litSum;
197 U32 offCodeSum;
198 U32 log2matchLengthSum;
199 U32 log2matchSum;
200 U32 log2litLengthSum;
201 U32 log2litSum;
202 U32 log2offCodeSum;
203 U32 factor;
204 U32 staticPrices;
205 U32 cachedPrice;
206 U32 cachedLitLength;
207 const BYTE *cachedLiterals;
208 } seqStore_t;
209
210 const seqStore_t *ZSTD_getSeqStore(const ZSTD_CCtx *ctx);
211 void ZSTD_seqToCodes(const seqStore_t *seqStorePtr);
212 int ZSTD_isSkipFrame(ZSTD_DCtx *dctx);
213
214 /*= Custom memory allocation functions */
215 typedef void *(*ZSTD_allocFunction)(void *opaque, size_t size);
216 typedef void (*ZSTD_freeFunction)(void *opaque, void *address);
217 typedef struct {
218 ZSTD_allocFunction customAlloc;
219 ZSTD_freeFunction customFree;
220 void *opaque;
221 } ZSTD_customMem;
222
223 void *ZSTD_malloc(size_t size, ZSTD_customMem customMem);
224 void ZSTD_free(void *ptr, ZSTD_customMem customMem);
225
226 /*====== stack allocation ======*/
227
228 typedef struct {
229 void *ptr;
230 const void *end;
231 } ZSTD_stack;
232
233 #define ZSTD_ALIGN(x) ALIGN(x, sizeof(size_t))
234 #define ZSTD_PTR_ALIGN(p) PTR_ALIGN(p, sizeof(size_t))
235
236 ZSTD_customMem ZSTD_initStack(void *workspace, size_t workspaceSize);
237
238 void *ZSTD_stackAllocAll(void *opaque, size_t *size);
239 void *ZSTD_stackAlloc(void *opaque, size_t size);
240 void ZSTD_stackFree(void *opaque, void *address);
241
242 /*====== common function ======*/
243
ZSTD_highbit32(U32 val)244 ZSTD_STATIC U32 ZSTD_highbit32(U32 val) { return 31 - __builtin_clz(val); }
245
246 /* hidden functions */
247
248 /* ZSTD_invalidateRepCodes() :
249 * ensures next compression will not use repcodes from previous block.
250 * Note : only works with regular variant;
251 * do not use with extDict variant ! */
252 void ZSTD_invalidateRepCodes(ZSTD_CCtx *cctx);
253
254 size_t ZSTD_freeCCtx(ZSTD_CCtx *cctx);
255 size_t ZSTD_freeDCtx(ZSTD_DCtx *dctx);
256 size_t ZSTD_freeCDict(ZSTD_CDict *cdict);
257 size_t ZSTD_freeDDict(ZSTD_DDict *cdict);
258 size_t ZSTD_freeCStream(ZSTD_CStream *zcs);
259 size_t ZSTD_freeDStream(ZSTD_DStream *zds);
260
261 #endif /* ZSTD_CCOMMON_H_MODULE */
262