Coverage Report

Created: 2026-06-09 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tdengine/contrib/TSZ/zstd/common/zstd_internal.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3
 * All rights reserved.
4
 *
5
 * This source code is licensed under both the BSD-style license (found in the
6
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
 * in the COPYING file in the root directory of this source tree).
8
 * You may select, at your option, one of the above-listed licenses.
9
 */
10
11
#ifndef ZSTD_CCOMMON_H_MODULE
12
#define ZSTD_CCOMMON_H_MODULE
13
14
/* this module contains definitions which must be identical
15
 * across compression, decompression and dictBuilder.
16
 * It also contains a few functions useful to at least 2 of them
17
 * and which benefit from being inlined */
18
19
/*-*************************************
20
*  Dependencies
21
***************************************/
22
#include "compiler.h"
23
#include "mem.h"
24
#include "debug.h"                 /* assert, DEBUGLOG, RAWLOG, g_debuglevel */
25
#include "error_private.h"
26
#define ZSTD_STATIC_LINKING_ONLY
27
#include "zstd.h"
28
#define FSE_STATIC_LINKING_ONLY
29
#include "fse.h"
30
#define HUF_STATIC_LINKING_ONLY
31
#include "huf.h"
32
#ifndef XXH_STATIC_LINKING_ONLY
33
#  define XXH_STATIC_LINKING_ONLY  /* XXH64_state_t */
34
#endif
35
#include "xxhash.h"                /* XXH_reset, update, digest */
36
37
38
#if defined (__cplusplus)
39
extern "C" {
40
#endif
41
42
/* ---- static assert (debug) --- */
43
0
#define ZSTD_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)
44
45
46
/*-*************************************
47
*  shared macros
48
***************************************/
49
#undef MIN
50
#undef MAX
51
0
#define MIN(a,b) ((a)<(b) ? (a) : (b))
52
0
#define MAX(a,b) ((a)>(b) ? (a) : (b))
53
0
#define CHECK_F(f) { size_t const errcod = f; if (ERR_isError(errcod)) return errcod; }  /* check and Forward error code */
54
0
#define CHECK_E(f, e) { size_t const errcod = f; if (ERR_isError(errcod)) return ERROR(e); }  /* check and send Error code */
55
56
57
/*-*************************************
58
*  Common constants
59
***************************************/
60
0
#define ZSTD_OPT_NUM    (1<<12)
61
62
0
#define ZSTD_REP_NUM      3                 /* number of repcodes */
63
0
#define ZSTD_REP_MOVE     (ZSTD_REP_NUM-1)
64
static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
65
66
0
#define KB *(1 <<10)
67
#define MB *(1 <<20)
68
#define GB *(1U<<30)
69
70
#define BIT7 128
71
#define BIT6  64
72
#define BIT5  32
73
#define BIT4  16
74
#define BIT1   2
75
#define BIT0   1
76
77
0
#define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
78
0
#define ZSTD_WINDOWLOG_DEFAULTMAX 27 /* Default maximum allowed window log */
79
static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
80
static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
81
82
#define ZSTD_FRAMEIDSIZE 4
83
static const size_t ZSTD_frameIdSize = ZSTD_FRAMEIDSIZE;  /* magic number size */
84
85
0
#define ZSTD_BLOCKHEADERSIZE 3   /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
86
static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
87
typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
88
89
0
#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
90
0
#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */)   /* for a non-null block */
91
92
0
#define HufLog 12
93
typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
94
95
0
#define LONGNBSEQ 0x7F00
96
97
0
#define MINMATCH 3
98
99
0
#define Litbits  8
100
0
#define MaxLit ((1<<Litbits) - 1)
101
0
#define MaxML   52
102
0
#define MaxLL   35
103
0
#define DefaultMaxOff 28
104
0
#define MaxOff  31
105
#define MaxSeq MAX(MaxLL, MaxML)   /* Assumption : MaxOff < MaxLL,MaxML */
106
0
#define MLFSELog    9
107
0
#define LLFSELog    9
108
0
#define OffFSELog   8
109
#define MaxFSELog  MAX(MAX(MLFSELog, LLFSELog), OffFSELog)
110
111
static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
112
                                      0, 0, 0, 0, 0, 0, 0, 0,
113
                                      1, 1, 1, 1, 2, 2, 3, 3,
114
                                      4, 6, 7, 8, 9,10,11,12,
115
                                     13,14,15,16 };
116
static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2,
117
                                             2, 2, 2, 2, 2, 1, 1, 1,
118
                                             2, 2, 2, 2, 2, 2, 2, 2,
119
                                             2, 3, 2, 1, 1, 1, 1, 1,
120
                                            -1,-1,-1,-1 };
121
#define LL_DEFAULTNORMLOG 6  /* for static allocation */
122
static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
123
124
static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
125
                                      0, 0, 0, 0, 0, 0, 0, 0,
126
                                      0, 0, 0, 0, 0, 0, 0, 0,
127
                                      0, 0, 0, 0, 0, 0, 0, 0,
128
                                      1, 1, 1, 1, 2, 2, 3, 3,
129
                                      4, 4, 5, 7, 8, 9,10,11,
130
                                     12,13,14,15,16 };
131
static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2,
132
                                             2, 1, 1, 1, 1, 1, 1, 1,
133
                                             1, 1, 1, 1, 1, 1, 1, 1,
134
                                             1, 1, 1, 1, 1, 1, 1, 1,
135
                                             1, 1, 1, 1, 1, 1, 1, 1,
136
                                             1, 1, 1, 1, 1, 1,-1,-1,
137
                                            -1,-1,-1,-1,-1 };
138
#define ML_DEFAULTNORMLOG 6  /* for static allocation */
139
static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
140
141
static const S16 OF_defaultNorm[DefaultMaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2,
142
                                                     2, 1, 1, 1, 1, 1, 1, 1,
143
                                                     1, 1, 1, 1, 1, 1, 1, 1,
144
                                                    -1,-1,-1,-1,-1 };
145
#define OF_DEFAULTNORMLOG 5  /* for static allocation */
146
static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
147
148
149
/*-*******************************************
150
*  Shared functions to include for inlining
151
*********************************************/
152
0
static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
Unexecuted instantiation: zstd_common.c:ZSTD_copy8
Unexecuted instantiation: zstd_compress.c:ZSTD_copy8
Unexecuted instantiation: zstd_double_fast.c:ZSTD_copy8
Unexecuted instantiation: zstd_fast.c:ZSTD_copy8
Unexecuted instantiation: zstd_lazy.c:ZSTD_copy8
Unexecuted instantiation: zstd_ldm.c:ZSTD_copy8
Unexecuted instantiation: zstd_opt.c:ZSTD_copy8
Unexecuted instantiation: zstd_decompress.c:ZSTD_copy8
153
0
#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
154
155
/*! ZSTD_wildcopy() :
156
 *  custom version of memcpy(), can overwrite up to WILDCOPY_OVERLENGTH bytes (if length==0) */
157
0
#define WILDCOPY_OVERLENGTH 8
158
MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
159
0
{
160
0
    const BYTE* ip = (const BYTE*)src;
161
0
    BYTE* op = (BYTE*)dst;
162
0
    BYTE* const oend = op + length;
163
0
    do
164
0
        COPY8(op, ip)
165
0
    while (op < oend);
166
0
}
Unexecuted instantiation: zstd_common.c:ZSTD_wildcopy
Unexecuted instantiation: zstd_compress.c:ZSTD_wildcopy
Unexecuted instantiation: zstd_double_fast.c:ZSTD_wildcopy
Unexecuted instantiation: zstd_fast.c:ZSTD_wildcopy
Unexecuted instantiation: zstd_lazy.c:ZSTD_wildcopy
Unexecuted instantiation: zstd_ldm.c:ZSTD_wildcopy
Unexecuted instantiation: zstd_opt.c:ZSTD_wildcopy
Unexecuted instantiation: zstd_decompress.c:ZSTD_wildcopy
167
168
MEM_STATIC void ZSTD_wildcopy_e(void* dst, const void* src, void* dstEnd)   /* should be faster for decoding, but strangely, not verified on all platform */
169
0
{
170
0
    const BYTE* ip = (const BYTE*)src;
171
0
    BYTE* op = (BYTE*)dst;
172
0
    BYTE* const oend = (BYTE*)dstEnd;
173
0
    do
174
0
        COPY8(op, ip)
175
0
    while (op < oend);
176
0
}
Unexecuted instantiation: zstd_common.c:ZSTD_wildcopy_e
Unexecuted instantiation: zstd_compress.c:ZSTD_wildcopy_e
Unexecuted instantiation: zstd_double_fast.c:ZSTD_wildcopy_e
Unexecuted instantiation: zstd_fast.c:ZSTD_wildcopy_e
Unexecuted instantiation: zstd_lazy.c:ZSTD_wildcopy_e
Unexecuted instantiation: zstd_ldm.c:ZSTD_wildcopy_e
Unexecuted instantiation: zstd_opt.c:ZSTD_wildcopy_e
Unexecuted instantiation: zstd_decompress.c:ZSTD_wildcopy_e
177
178
179
/*-*******************************************
180
*  Private declarations
181
*********************************************/
182
typedef struct seqDef_s {
183
    U32 offset;
184
    U16 litLength;
185
    U16 matchLength;
186
} seqDef;
187
188
typedef 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;   /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
197
    U32   longLengthPos;
198
} seqStore_t;
199
200
const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);   /* compress & dictBuilder */
201
void ZSTD_seqToCodes(const seqStore_t* seqStorePtr);   /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
202
203
/* custom memory allocation functions */
204
void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
205
void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
206
void ZSTD_free(void* ptr, ZSTD_customMem customMem);
207
208
209
MEM_STATIC U32 ZSTD_highbit32(U32 val)   /* compress, dictBuilder, decodeCorpus */
210
0
{
211
0
    assert(val != 0);
212
0
    {
213
#   if defined(_MSC_VER)   /* Visual */
214
        unsigned long r=0;
215
        _BitScanReverse(&r, val);
216
        return (unsigned)r;
217
#   elif defined(__GNUC__) && (__GNUC__ >= 3)   /* GCC Intrinsic */
218
        return 31 - __builtin_clz(val);
219
#   else   /* Software version */
220
        static const U32 DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
221
        U32 v = val;
222
        v |= v >> 1;
223
        v |= v >> 2;
224
        v |= v >> 4;
225
        v |= v >> 8;
226
        v |= v >> 16;
227
        return DeBruijnClz[(v * 0x07C4ACDDU) >> 27];
228
#   endif
229
0
    }
230
0
}
Unexecuted instantiation: zstd_common.c:ZSTD_highbit32
Unexecuted instantiation: zstd_compress.c:ZSTD_highbit32
Unexecuted instantiation: zstd_double_fast.c:ZSTD_highbit32
Unexecuted instantiation: zstd_fast.c:ZSTD_highbit32
Unexecuted instantiation: zstd_lazy.c:ZSTD_highbit32
Unexecuted instantiation: zstd_ldm.c:ZSTD_highbit32
Unexecuted instantiation: zstd_opt.c:ZSTD_highbit32
Unexecuted instantiation: zstd_decompress.c:ZSTD_highbit32
231
232
233
/* ZSTD_invalidateRepCodes() :
234
 * ensures next compression will not use repcodes from previous block.
235
 * Note : only works with regular variant;
236
 *        do not use with extDict variant ! */
237
void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx);   /* zstdmt, adaptive_compression (shouldn't get this definition from here) */
238
239
240
typedef struct {
241
    blockType_e blockType;
242
    U32 lastBlock;
243
    U32 origSize;
244
} blockProperties_t;
245
246
/*! ZSTD_getcBlockSize() :
247
 *  Provides the size of compressed block from block header `src` */
248
/* Used by: decompress, fullbench (does not get its definition from here) */
249
size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
250
                          blockProperties_t* bpPtr);
251
252
#if defined (__cplusplus)
253
}
254
#endif
255
256
#endif   /* ZSTD_CCOMMON_H_MODULE */