Coverage Report

Created: 2026-10-28 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zstd/lib/compress/zstd_compress_internal.h
Line
Count
Source
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
/* This header contains definitions
12
 * that shall **only** be used by modules within lib/compress.
13
 */
14
15
#ifndef ZSTD_COMPRESS_H
16
#define ZSTD_COMPRESS_H
17
18
/*-*************************************
19
*  Dependencies
20
***************************************/
21
#include "../common/zstd_internal.h"
22
#include "zstd_cwksp.h"
23
#ifdef ZSTD_MULTITHREAD
24
#  include "zstdmt_compress.h"
25
#endif
26
#include "../common/bits.h" /* ZSTD_highbit32, ZSTD_NbCommonBytes */
27
#include "zstd_preSplit.h" /* ZSTD_SLIPBLOCK_WORKSPACESIZE */
28
29
/*-*************************************
30
*  Constants
31
***************************************/
32
2.99M
#define kSearchStrength      8
33
5.84k
#define HASH_READ_SIZE       8
34
0
#define ZSTD_DUBT_UNSORTED_MARK 1   /* For btlazy2 strategy, index ZSTD_DUBT_UNSORTED_MARK==1 means "unsorted".
35
                                       It could be confused for a real successor at index "1", if sorted as larger than its predecessor.
36
                                       It's not a big deal though : candidate will just be sorted again.
37
                                       Additionally, candidate position 1 will be lost.
38
                                       But candidate 1 cannot hide a large tree of candidates, so it's a minimal loss.
39
                                       The benefit is that ZSTD_DUBT_UNSORTED_MARK cannot be mishandled after table reuse with a different strategy.
40
                                       This constant is required by ZSTD_compressBlock_btlazy2() and ZSTD_reduceTable_internal() */
41
42
43
/*-*************************************
44
*  Context memory management
45
***************************************/
46
typedef enum { ZSTDcs_created=0, ZSTDcs_init, ZSTDcs_ongoing, ZSTDcs_ending } ZSTD_compressionStage_e;
47
typedef enum { zcss_init=0, zcss_load, zcss_flush } ZSTD_cStreamStage;
48
49
typedef struct ZSTD_prefixDict_s {
50
    const void* dict;
51
    size_t dictSize;
52
    ZSTD_dictContentType_e dictContentType;
53
} ZSTD_prefixDict;
54
55
typedef struct {
56
    void* dictBuffer;
57
    void const* dict;
58
    size_t dictSize;
59
    ZSTD_dictContentType_e dictContentType;
60
    ZSTD_CDict* cdict;
61
} ZSTD_localDict;
62
63
typedef struct {
64
    HUF_CElt CTable[HUF_CTABLE_SIZE_ST(255)];
65
    HUF_repeat repeatMode;
66
} ZSTD_hufCTables_t;
67
68
typedef struct {
69
    FSE_CTable offcodeCTable[FSE_CTABLE_SIZE_U32(OffFSELog, MaxOff)];
70
    FSE_CTable matchlengthCTable[FSE_CTABLE_SIZE_U32(MLFSELog, MaxML)];
71
    FSE_CTable litlengthCTable[FSE_CTABLE_SIZE_U32(LLFSELog, MaxLL)];
72
    FSE_repeat offcode_repeatMode;
73
    FSE_repeat matchlength_repeatMode;
74
    FSE_repeat litlength_repeatMode;
75
} ZSTD_fseCTables_t;
76
77
typedef struct {
78
    ZSTD_hufCTables_t huf;
79
    ZSTD_fseCTables_t fse;
80
} ZSTD_entropyCTables_t;
81
82
/***********************************************
83
*  Sequences *
84
***********************************************/
85
typedef struct SeqDef_s {
86
    U32 offBase;   /* offBase == Offset + ZSTD_REP_NUM, or repcode 1,2,3 */
87
    U16 litLength;
88
    U16 mlBase;    /* mlBase == matchLength - MINMATCH */
89
} SeqDef;
90
91
/* Controls whether seqStore has a single "long" litLength or matchLength. See SeqStore_t. */
92
typedef enum {
93
    ZSTD_llt_none = 0,             /* no longLengthType */
94
    ZSTD_llt_literalLength = 1,    /* represents a long literal */
95
    ZSTD_llt_matchLength = 2       /* represents a long match */
96
} ZSTD_longLengthType_e;
97
98
typedef struct {
99
    SeqDef* sequencesStart;
100
    SeqDef* sequences;      /* ptr to end of sequences */
101
    BYTE*  litStart;
102
    BYTE*  lit;             /* ptr to end of literals */
103
    BYTE*  llCode;
104
    BYTE*  mlCode;
105
    BYTE*  ofCode;
106
    size_t maxNbSeq;
107
    size_t maxNbLit;
108
109
    /* longLengthPos and longLengthType to allow us to represent either a single litLength or matchLength
110
     * in the seqStore that has a value larger than U16 (if it exists). To do so, we increment
111
     * the existing value of the litLength or matchLength by 0x10000.
112
     */
113
    ZSTD_longLengthType_e longLengthType;
114
    U32                   longLengthPos;  /* Index of the sequence to apply long length modification to */
115
} SeqStore_t;
116
117
typedef struct {
118
    U32 litLength;
119
    U32 matchLength;
120
} ZSTD_SequenceLength;
121
122
/**
123
 * Returns the ZSTD_SequenceLength for the given sequences. It handles the decoding of long sequences
124
 * indicated by longLengthPos and longLengthType, and adds MINMATCH back to matchLength.
125
 */
126
MEM_STATIC ZSTD_SequenceLength ZSTD_getSequenceLength(SeqStore_t const* seqStore, SeqDef const* seq)
127
0
{
128
0
    ZSTD_SequenceLength seqLen;
129
0
    seqLen.litLength = seq->litLength;
130
0
    seqLen.matchLength = seq->mlBase + MINMATCH;
131
0
    if (seqStore->longLengthPos == (U32)(seq - seqStore->sequencesStart)) {
132
0
        if (seqStore->longLengthType == ZSTD_llt_literalLength) {
133
0
            seqLen.litLength += 0x10000;
134
0
        }
135
0
        if (seqStore->longLengthType == ZSTD_llt_matchLength) {
136
0
            seqLen.matchLength += 0x10000;
137
0
        }
138
0
    }
139
0
    return seqLen;
140
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_getSequenceLength
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_getSequenceLength
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_getSequenceLength
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_getSequenceLength
Unexecuted instantiation: zstd_double_fast.c:ZSTD_getSequenceLength
Unexecuted instantiation: zstd_fast.c:ZSTD_getSequenceLength
Unexecuted instantiation: zstd_lazy.c:ZSTD_getSequenceLength
Unexecuted instantiation: zstd_ldm.c:ZSTD_getSequenceLength
Unexecuted instantiation: zstd_opt.c:ZSTD_getSequenceLength
Unexecuted instantiation: zstdmt_compress.c:ZSTD_getSequenceLength
141
142
const SeqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);   /* compress & dictBuilder */
143
int ZSTD_seqToCodes(const SeqStore_t* seqStorePtr);   /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
144
145
146
/***********************************************
147
*  Entropy buffer statistics structs and funcs *
148
***********************************************/
149
/** ZSTD_hufCTablesMetadata_t :
150
 *  Stores Literals Block Type for a super-block in hType, and
151
 *  huffman tree description in hufDesBuffer.
152
 *  hufDesSize refers to the size of huffman tree description in bytes.
153
 *  This metadata is populated in ZSTD_buildBlockEntropyStats_literals() */
154
typedef struct {
155
    SymbolEncodingType_e hType;
156
    BYTE hufDesBuffer[ZSTD_MAX_HUF_HEADER_SIZE];
157
    size_t hufDesSize;
158
} ZSTD_hufCTablesMetadata_t;
159
160
/** ZSTD_fseCTablesMetadata_t :
161
 *  Stores symbol compression modes for a super-block in {ll, ol, ml}Type, and
162
 *  fse tables in fseTablesBuffer.
163
 *  fseTablesSize refers to the size of fse tables in bytes.
164
 *  This metadata is populated in ZSTD_buildBlockEntropyStats_sequences() */
165
typedef struct {
166
    SymbolEncodingType_e llType;
167
    SymbolEncodingType_e ofType;
168
    SymbolEncodingType_e mlType;
169
    BYTE fseTablesBuffer[ZSTD_MAX_FSE_HEADERS_SIZE];
170
    size_t fseTablesSize;
171
    size_t lastCountSize; /* This is to account for bug in 1.3.4. More detail in ZSTD_entropyCompressSeqStore_internal() */
172
} ZSTD_fseCTablesMetadata_t;
173
174
typedef struct {
175
    ZSTD_hufCTablesMetadata_t hufMetadata;
176
    ZSTD_fseCTablesMetadata_t fseMetadata;
177
} ZSTD_entropyCTablesMetadata_t;
178
179
/** ZSTD_buildBlockEntropyStats() :
180
 *  Builds entropy for the block.
181
 *  @return : 0 on success or error code */
182
size_t ZSTD_buildBlockEntropyStats(
183
                    const SeqStore_t* seqStorePtr,
184
                    const ZSTD_entropyCTables_t* prevEntropy,
185
                          ZSTD_entropyCTables_t* nextEntropy,
186
                    const ZSTD_CCtx_params* cctxParams,
187
                          ZSTD_entropyCTablesMetadata_t* entropyMetadata,
188
                          void* workspace, size_t wkspSize);
189
190
/*********************************
191
*  Compression internals structs *
192
*********************************/
193
194
typedef struct {
195
    U32 off;            /* Offset sumtype code for the match, using ZSTD_storeSeq() format */
196
    U32 len;            /* Raw length of match */
197
} ZSTD_match_t;
198
199
typedef struct {
200
    U32 offset;         /* Offset of sequence */
201
    U32 litLength;      /* Length of literals prior to match */
202
    U32 matchLength;    /* Raw length of match */
203
} rawSeq;
204
205
typedef struct {
206
  rawSeq* seq;          /* The start of the sequences */
207
  size_t pos;           /* The index in seq where reading stopped. pos <= size. */
208
  size_t posInSequence; /* The position within the sequence at seq[pos] where reading
209
                           stopped. posInSequence <= seq[pos].litLength + seq[pos].matchLength */
210
  size_t size;          /* The number of sequences. <= capacity. */
211
  size_t capacity;      /* The capacity starting from `seq` pointer */
212
} RawSeqStore_t;
213
214
UNUSED_ATTR static const RawSeqStore_t kNullRawSeqStore = {NULL, 0, 0, 0, 0};
215
216
typedef struct {
217
    int price;  /* price from beginning of segment to this position */
218
    U32 off;    /* offset of previous match */
219
    U32 mlen;   /* length of previous match */
220
    U32 litlen; /* nb of literals since previous match */
221
    U32 rep[ZSTD_REP_NUM];  /* offset history after previous match */
222
} ZSTD_optimal_t;
223
224
typedef enum { zop_dynamic=0, zop_predef } ZSTD_OptPrice_e;
225
226
11.6k
#define ZSTD_OPT_SIZE (ZSTD_OPT_NUM+3)
227
typedef struct {
228
    /* All tables are allocated inside cctx->workspace by ZSTD_resetCCtx_internal() */
229
    unsigned* litFreq;           /* table of literals statistics, of size 256 */
230
    unsigned* litLengthFreq;     /* table of litLength statistics, of size (MaxLL+1) */
231
    unsigned* matchLengthFreq;   /* table of matchLength statistics, of size (MaxML+1) */
232
    unsigned* offCodeFreq;       /* table of offCode statistics, of size (MaxOff+1) */
233
    ZSTD_match_t* matchTable;    /* list of found matches, of size ZSTD_OPT_SIZE */
234
    ZSTD_optimal_t* priceTable;  /* All positions tracked by optimal parser, of size ZSTD_OPT_SIZE */
235
236
    U32  litSum;                 /* nb of literals */
237
    U32  litLengthSum;           /* nb of litLength codes */
238
    U32  matchLengthSum;         /* nb of matchLength codes */
239
    U32  offCodeSum;             /* nb of offset codes */
240
    U32  litSumBasePrice;        /* to compare to log2(litfreq) */
241
    U32  litLengthSumBasePrice;  /* to compare to log2(llfreq)  */
242
    U32  matchLengthSumBasePrice;/* to compare to log2(mlfreq)  */
243
    U32  offCodeSumBasePrice;    /* to compare to log2(offreq)  */
244
    ZSTD_OptPrice_e priceType;   /* prices can be determined dynamically, or follow a pre-defined cost structure */
245
    const ZSTD_entropyCTables_t* symbolCosts;  /* pre-calculated dictionary statistics */
246
    ZSTD_ParamSwitch_e literalCompressionMode;
247
} optState_t;
248
249
typedef struct {
250
  ZSTD_entropyCTables_t entropy;
251
  U32 rep[ZSTD_REP_NUM];
252
} ZSTD_compressedBlockState_t;
253
254
typedef struct {
255
    BYTE const* nextSrc;       /* next block here to continue on current prefix */
256
    BYTE const* base;          /* All regular indexes relative to this position */
257
    BYTE const* dictBase;      /* extDict indexes relative to this position */
258
    U32 dictLimit;             /* below that point, need extDict */
259
    U32 lowLimit;              /* below that point, no more valid data */
260
    U32 nbOverflowCorrections; /* Number of times overflow correction has run since
261
                                * ZSTD_window_init(). Useful for debugging coredumps
262
                                * and for ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY.
263
                                */
264
} ZSTD_window_t;
265
266
27.6k
#define ZSTD_WINDOW_START_INDEX 2
267
268
typedef struct ZSTD_MatchState_t ZSTD_MatchState_t;
269
270
479M
#define ZSTD_ROW_HASH_CACHE_SIZE 8       /* Size of prefetching hash cache for row-based matchfinder */
271
272
struct ZSTD_MatchState_t {
273
    ZSTD_window_t window;   /* State for window round buffer management */
274
    U32 loadedDictEnd;      /* index of end of dictionary, within context's referential.
275
                             * When loadedDictEnd != 0, a dictionary is in use, and still valid.
276
                             * This relies on a mechanism to set loadedDictEnd=0 when dictionary is no longer within distance.
277
                             * Such mechanism is provided within ZSTD_window_enforceMaxDist() and ZSTD_checkDictValidity().
278
                             * When dict referential is copied into active context (i.e. not attached),
279
                             * loadedDictEnd == dictSize, since referential starts from zero.
280
                             */
281
    U32 nextToUpdate;       /* index from which to continue table update */
282
    U32 hashLog3;           /* dispatch table for matches of len==3 : larger == faster, more memory */
283
284
    U32 rowHashLog;                          /* For row-based matchfinder: Hashlog based on nb of rows in the hashTable.*/
285
    BYTE* tagTable;                          /* For row-based matchFinder: A row-based table containing the hashes and head index. */
286
    U32 hashCache[ZSTD_ROW_HASH_CACHE_SIZE]; /* For row-based matchFinder: a cache of hashes to improve speed */
287
    U64 hashSalt;                            /* For row-based matchFinder: salts the hash for reuse of tag table */
288
    U32 hashSaltEntropy;                     /* For row-based matchFinder: collects entropy for salt generation */
289
290
    U32* hashTable;
291
    U32* hashTable3;
292
    U32* chainTable;
293
294
    int forceNonContiguous; /* Non-zero if we should force non-contiguous load for the next window update. */
295
296
    int dedicatedDictSearch;  /* Indicates whether this matchState is using the
297
                               * dedicated dictionary search structure.
298
                               */
299
    optState_t opt;         /* optimal parser state */
300
    const ZSTD_MatchState_t* dictMatchState;
301
    ZSTD_compressionParameters cParams;
302
    const RawSeqStore_t* ldmSeqStore;
303
304
    /* Controls prefetching in some dictMatchState matchfinders.
305
     * This behavior is controlled from the cctx ms.
306
     * This parameter has no effect in the cdict ms. */
307
    int prefetchCDictTables;
308
309
    /* When == 0, lazy match finders insert every position.
310
     * When != 0, lazy match finders only insert positions they search.
311
     * This allows them to skip much faster over incompressible data,
312
     * at a small cost to compression ratio.
313
     */
314
    int lazySkipping;
315
};
316
317
typedef struct {
318
    ZSTD_compressedBlockState_t* prevCBlock;
319
    ZSTD_compressedBlockState_t* nextCBlock;
320
    ZSTD_MatchState_t matchState;
321
} ZSTD_blockState_t;
322
323
typedef struct {
324
    U32 offset;
325
    U32 checksum;
326
} ldmEntry_t;
327
328
typedef struct {
329
    BYTE const* split;
330
    U32 hash;
331
    U32 checksum;
332
    ldmEntry_t* bucket;
333
} ldmMatchCandidate_t;
334
335
0
#define LDM_BATCH_SIZE 64
336
337
typedef struct {
338
    ZSTD_window_t window;   /* State for the window round buffer management */
339
    ldmEntry_t* hashTable;
340
    U32 loadedDictEnd;
341
    BYTE* bucketOffsets;    /* Next position in bucket to insert entry */
342
    size_t splitIndices[LDM_BATCH_SIZE];
343
    ldmMatchCandidate_t matchCandidates[LDM_BATCH_SIZE];
344
} ldmState_t;
345
346
typedef struct {
347
    ZSTD_ParamSwitch_e enableLdm; /* ZSTD_ps_enable to enable LDM. ZSTD_ps_auto by default */
348
    U32 hashLog;            /* Log size of hashTable */
349
    U32 bucketSizeLog;      /* Log bucket size for collision resolution, at most 8 */
350
    U32 minMatchLength;     /* Minimum match length */
351
    U32 hashRateLog;       /* Log number of entries to skip */
352
    U32 windowLog;          /* Window log for the LDM */
353
} ldmParams_t;
354
355
typedef struct {
356
    int collectSequences;
357
    ZSTD_Sequence* seqStart;
358
    size_t seqIndex;
359
    size_t maxSequences;
360
} SeqCollector;
361
362
struct ZSTD_CCtx_params_s {
363
    ZSTD_format_e format;
364
    ZSTD_compressionParameters cParams;
365
    ZSTD_frameParameters fParams;
366
367
    int compressionLevel;
368
    int forceWindow;           /* force back-references to respect limit of
369
                                * 1<<wLog, even for dictionary */
370
    size_t targetCBlockSize;   /* Tries to fit compressed block size to be around targetCBlockSize.
371
                                * No target when targetCBlockSize == 0.
372
                                * There is no guarantee on compressed block size */
373
    int srcSizeHint;           /* User's best guess of source size.
374
                                * Hint is not valid when srcSizeHint == 0.
375
                                * There is no guarantee that hint is close to actual source size */
376
377
    ZSTD_dictAttachPref_e attachDictPref;
378
    ZSTD_ParamSwitch_e literalCompressionMode;
379
380
    /* Multithreading: used to pass parameters to mtctx */
381
    int nbWorkers;
382
    size_t jobSize;
383
    int overlapLog;
384
    int rsyncable;
385
386
    /* Long distance matching parameters */
387
    ldmParams_t ldmParams;
388
389
    /* Dedicated dict search algorithm trigger */
390
    int enableDedicatedDictSearch;
391
392
    /* Input/output buffer modes */
393
    ZSTD_bufferMode_e inBufferMode;
394
    ZSTD_bufferMode_e outBufferMode;
395
396
    /* Sequence compression API */
397
    ZSTD_SequenceFormat_e blockDelimiters;
398
    int validateSequences;
399
400
    /* Block splitting
401
     * @postBlockSplitter executes split analysis after sequences are produced,
402
     * it's more accurate but consumes more resources.
403
     * @preBlockSplitter_level splits before knowing sequences,
404
     * it's more approximative but also cheaper.
405
     * Valid @preBlockSplitter_level values range from 0 to 6 (included).
406
     * 0 means auto, 1 means do not split,
407
     * then levels are sorted in increasing cpu budget, from 2 (fastest) to 6 (slowest).
408
     * Highest @preBlockSplitter_level combines well with @postBlockSplitter.
409
     */
410
    ZSTD_ParamSwitch_e postBlockSplitter;
411
    int preBlockSplitter_level;
412
413
    /* Adjust the max block size*/
414
    size_t maxBlockSize;
415
416
    /* Param for deciding whether to use row-based matchfinder */
417
    ZSTD_ParamSwitch_e useRowMatchFinder;
418
419
    /* Always load a dictionary in ext-dict mode (not prefix mode)? */
420
    int deterministicRefPrefix;
421
422
    /* Internal use, for createCCtxParams() and freeCCtxParams() only */
423
    ZSTD_customMem customMem;
424
425
    /* Controls prefetching in some dictMatchState matchfinders */
426
    ZSTD_ParamSwitch_e prefetchCDictTables;
427
428
    /* Controls whether zstd will fall back to an internal matchfinder
429
     * if the external matchfinder returns an error code. */
430
    int enableMatchFinderFallback;
431
432
    /* Parameters for the external sequence producer API.
433
     * Users set these parameters through ZSTD_registerSequenceProducer().
434
     * It is not possible to set these parameters individually through the public API. */
435
    void* extSeqProdState;
436
    ZSTD_sequenceProducer_F extSeqProdFunc;
437
438
    /* Controls repcode search in external sequence parsing */
439
    ZSTD_ParamSwitch_e searchForExternalRepcodes;
440
};  /* typedef'd to ZSTD_CCtx_params within "zstd.h" */
441
442
#define COMPRESS_SEQUENCES_WORKSPACE_SIZE (sizeof(unsigned) * (MaxSeq + 2))
443
#define ENTROPY_WORKSPACE_SIZE (HUF_WORKSPACE_SIZE + COMPRESS_SEQUENCES_WORKSPACE_SIZE)
444
11.9k
#define TMP_WORKSPACE_SIZE (MAX(ENTROPY_WORKSPACE_SIZE, ZSTD_SLIPBLOCK_WORKSPACESIZE))
445
446
/**
447
 * Indicates whether this compression proceeds directly from user-provided
448
 * source buffer to user-provided destination buffer (ZSTDb_not_buffered), or
449
 * whether the context needs to buffer the input/output (ZSTDb_buffered).
450
 */
451
typedef enum {
452
    ZSTDb_not_buffered,
453
    ZSTDb_buffered
454
} ZSTD_buffered_policy_e;
455
456
/**
457
 * Struct that contains all elements of block splitter that should be allocated
458
 * in a wksp.
459
 */
460
0
#define ZSTD_MAX_NB_BLOCK_SPLITS 196
461
typedef struct {
462
    SeqStore_t fullSeqStoreChunk;
463
    SeqStore_t firstHalfSeqStore;
464
    SeqStore_t secondHalfSeqStore;
465
    SeqStore_t currSeqStore;
466
    SeqStore_t nextSeqStore;
467
468
    U32 partitions[ZSTD_MAX_NB_BLOCK_SPLITS];
469
    ZSTD_entropyCTablesMetadata_t entropyMetadata;
470
} ZSTD_blockSplitCtx;
471
472
struct ZSTD_CCtx_s {
473
    ZSTD_compressionStage_e stage;
474
    int cParamsChanged;                  /* == 1 if cParams(except wlog) or compression level are changed in requestedParams. Triggers transmission of new params to ZSTDMT (if available) then reset to 0. */
475
#if DYNAMIC_BMI2
476
    int bmi2;                            /* == 1 if the CPU supports BMI2 and 0 otherwise when CPU state
477
                                          * is cached per context. Never read this directly: use ZSTD_CCtx_get_bmi2(),
478
                                          * which also handles builds
479
                                          * where cached CPU state is not used. */
480
#endif
481
    ZSTD_CCtx_params requestedParams;
482
    ZSTD_CCtx_params appliedParams;
483
    ZSTD_CCtx_params simpleApiParams;    /* Param storage used by the simple API - not sticky. Must only be used in top-level simple API functions for storage. */
484
    U32   dictID;
485
    size_t dictContentSize;
486
487
    ZSTD_cwksp workspace; /* manages buffer for dynamic allocations */
488
    size_t blockSizeMax;
489
    unsigned long long pledgedSrcSizePlusOne;  /* this way, 0 (default) == unknown */
490
    unsigned long long consumedSrcSize;
491
    unsigned long long producedCSize;
492
    XXH64_state_t xxhState;
493
    ZSTD_customMem customMem;
494
    ZSTD_threadPool* pool;
495
    size_t staticSize;
496
    SeqCollector seqCollector;
497
    int isFirstBlock;
498
    int initialized;
499
500
    SeqStore_t seqStore;      /* sequences storage ptrs */
501
    ldmState_t ldmState;      /* long distance matching state */
502
    rawSeq* ldmSequences;     /* Storage for the ldm output sequences */
503
    size_t maxNbLdmSequences;
504
    RawSeqStore_t externSeqStore; /* Mutable reference to external sequences */
505
    ZSTD_blockState_t blockState;
506
    void* tmpWorkspace;  /* used as substitute of stack space - must be aligned for S64 type */
507
    size_t tmpWkspSize;
508
509
    /* Whether we are streaming or not */
510
    ZSTD_buffered_policy_e bufferedPolicy;
511
512
    /* streaming */
513
    char*  inBuff;
514
    size_t inBuffSize;
515
    size_t inToCompress;
516
    size_t inBuffPos;
517
    size_t inBuffTarget;
518
    char*  outBuff;
519
    size_t outBuffSize;
520
    size_t outBuffContentSize;
521
    size_t outBuffFlushedSize;
522
    ZSTD_cStreamStage streamStage;
523
    U32    frameEnded;
524
525
    /* Stable in/out buffer verification */
526
    ZSTD_inBuffer expectedInBuffer;
527
    size_t stableIn_notConsumed; /* nb bytes within stable input buffer that are said to be consumed but are not */
528
    size_t expectedOutBufferSize;
529
530
    /* Dictionary */
531
    ZSTD_localDict localDict;
532
    const ZSTD_CDict* cdict;
533
    ZSTD_prefixDict prefixDict;   /* single-usage dictionary */
534
535
    /* Multi-threading */
536
#ifdef ZSTD_MULTITHREAD
537
    ZSTDMT_CCtx* mtctx;
538
#endif
539
540
    /* Tracing */
541
#if ZSTD_TRACE
542
    ZSTD_TraceCtx traceCtx;
543
#endif
544
545
    /* Workspace for block splitter */
546
    ZSTD_blockSplitCtx blockSplitCtx;
547
548
    /* Buffer for output from external sequence producer */
549
    ZSTD_Sequence* extSeqBuf;
550
    size_t extSeqBufCapacity;
551
};
552
553
/**
554
 * @returns the cached per-context BMI2 dispatch hint, or 0 when the build does
555
 *          not use cached CPU state.
556
 *
557
 * Beware: this is *not* a "does the CPU support BMI2" test. When BMI2 is enabled
558
 * at compile time, DYNAMIC_BMI2 is 0: the whole library is already compiled with
559
 * BMI2, no separate variant exists, and this returns 0 even though the CPU does
560
 * support BMI2. Only use the result as a dispatch hint passed to a lower-level
561
 * BMI2/default selector. Integrations may ignore the cached hint and consult
562
 * their own CPU-feature policy at that selector.
563
 */
564
18.1k
MEM_STATIC int ZSTD_CCtx_get_bmi2(const struct ZSTD_CCtx_s* cctx) {
565
18.1k
#if DYNAMIC_BMI2
566
18.1k
    return cctx->bmi2;
567
#else
568
    (void)cctx;
569
    return 0;
570
#endif
571
18.1k
}
zstd_compress.c:ZSTD_CCtx_get_bmi2
Line
Count
Source
564
18.1k
MEM_STATIC int ZSTD_CCtx_get_bmi2(const struct ZSTD_CCtx_s* cctx) {
565
18.1k
#if DYNAMIC_BMI2
566
18.1k
    return cctx->bmi2;
567
#else
568
    (void)cctx;
569
    return 0;
570
#endif
571
18.1k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_CCtx_get_bmi2
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_CCtx_get_bmi2
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_CCtx_get_bmi2
Unexecuted instantiation: zstd_double_fast.c:ZSTD_CCtx_get_bmi2
Unexecuted instantiation: zstd_fast.c:ZSTD_CCtx_get_bmi2
Unexecuted instantiation: zstd_lazy.c:ZSTD_CCtx_get_bmi2
Unexecuted instantiation: zstd_ldm.c:ZSTD_CCtx_get_bmi2
Unexecuted instantiation: zstd_opt.c:ZSTD_CCtx_get_bmi2
Unexecuted instantiation: zstdmt_compress.c:ZSTD_CCtx_get_bmi2
572
573
typedef enum { ZSTD_dtlm_fast, ZSTD_dtlm_full } ZSTD_dictTableLoadMethod_e;
574
typedef enum { ZSTD_tfp_forCCtx, ZSTD_tfp_forCDict } ZSTD_tableFillPurpose_e;
575
576
typedef enum {
577
    ZSTD_noDict = 0,
578
    ZSTD_extDict = 1,
579
    ZSTD_dictMatchState = 2,
580
    ZSTD_dedicatedDictSearch = 3
581
} ZSTD_dictMode_e;
582
583
typedef enum {
584
    ZSTD_cpm_noAttachDict = 0,  /* Compression with ZSTD_noDict or ZSTD_extDict.
585
                                 * In this mode we use both the srcSize and the dictSize
586
                                 * when selecting and adjusting parameters.
587
                                 */
588
    ZSTD_cpm_attachDict = 1,    /* Compression with ZSTD_dictMatchState or ZSTD_dedicatedDictSearch.
589
                                 * In this mode we only take the srcSize into account when selecting
590
                                 * and adjusting parameters.
591
                                 */
592
    ZSTD_cpm_createCDict = 2,   /* Creating a CDict.
593
                                 * In this mode we take both the source size and the dictionary size
594
                                 * into account when selecting and adjusting the parameters.
595
                                 */
596
    ZSTD_cpm_unknown = 3        /* ZSTD_getCParams, ZSTD_getParams, ZSTD_adjustParams.
597
                                 * We don't know what these parameters are for. We default to the legacy
598
                                 * behavior of taking both the source size and the dict size into account
599
                                 * when selecting and adjusting parameters.
600
                                 */
601
} ZSTD_CParamMode_e;
602
603
typedef size_t (*ZSTD_BlockCompressor_f) (
604
        ZSTD_MatchState_t* bs, SeqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
605
        void const* src, size_t srcSize);
606
ZSTD_BlockCompressor_f ZSTD_selectBlockCompressor(ZSTD_strategy strat, ZSTD_ParamSwitch_e rowMatchfinderMode, ZSTD_dictMode_e dictMode);
607
608
609
MEM_STATIC U32 ZSTD_LLcode(U32 litLength)
610
7.72M
{
611
7.72M
    static const BYTE LL_Code[64] = {  0,  1,  2,  3,  4,  5,  6,  7,
612
7.72M
                                       8,  9, 10, 11, 12, 13, 14, 15,
613
7.72M
                                      16, 16, 17, 17, 18, 18, 19, 19,
614
7.72M
                                      20, 20, 20, 20, 21, 21, 21, 21,
615
7.72M
                                      22, 22, 22, 22, 22, 22, 22, 22,
616
7.72M
                                      23, 23, 23, 23, 23, 23, 23, 23,
617
7.72M
                                      24, 24, 24, 24, 24, 24, 24, 24,
618
7.72M
                                      24, 24, 24, 24, 24, 24, 24, 24 };
619
7.72M
    static const U32 LL_deltaCode = 19;
620
7.72M
    return (litLength > 63) ? ZSTD_highbit32(litLength) + LL_deltaCode : LL_Code[litLength];
621
7.72M
}
zstd_compress.c:ZSTD_LLcode
Line
Count
Source
610
7.72M
{
611
7.72M
    static const BYTE LL_Code[64] = {  0,  1,  2,  3,  4,  5,  6,  7,
612
7.72M
                                       8,  9, 10, 11, 12, 13, 14, 15,
613
7.72M
                                      16, 16, 17, 17, 18, 18, 19, 19,
614
7.72M
                                      20, 20, 20, 20, 21, 21, 21, 21,
615
7.72M
                                      22, 22, 22, 22, 22, 22, 22, 22,
616
7.72M
                                      23, 23, 23, 23, 23, 23, 23, 23,
617
7.72M
                                      24, 24, 24, 24, 24, 24, 24, 24,
618
7.72M
                                      24, 24, 24, 24, 24, 24, 24, 24 };
619
7.72M
    static const U32 LL_deltaCode = 19;
620
7.72M
    return (litLength > 63) ? ZSTD_highbit32(litLength) + LL_deltaCode : LL_Code[litLength];
621
7.72M
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_LLcode
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_LLcode
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_LLcode
Unexecuted instantiation: zstd_double_fast.c:ZSTD_LLcode
Unexecuted instantiation: zstd_fast.c:ZSTD_LLcode
Unexecuted instantiation: zstd_lazy.c:ZSTD_LLcode
Unexecuted instantiation: zstd_ldm.c:ZSTD_LLcode
Unexecuted instantiation: zstd_opt.c:ZSTD_LLcode
Unexecuted instantiation: zstdmt_compress.c:ZSTD_LLcode
622
623
/* ZSTD_MLcode() :
624
 * note : mlBase = matchLength - MINMATCH;
625
 *        because it's the format it's stored in seqStore->sequences */
626
MEM_STATIC U32 ZSTD_MLcode(U32 mlBase)
627
7.72M
{
628
7.72M
    static const BYTE ML_Code[128] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
629
7.72M
                                      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
630
7.72M
                                      32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37,
631
7.72M
                                      38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39,
632
7.72M
                                      40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
633
7.72M
                                      41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
634
7.72M
                                      42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
635
7.72M
                                      42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 };
636
7.72M
    static const U32 ML_deltaCode = 36;
637
7.72M
    return (mlBase > 127) ? ZSTD_highbit32(mlBase) + ML_deltaCode : ML_Code[mlBase];
638
7.72M
}
zstd_compress.c:ZSTD_MLcode
Line
Count
Source
627
7.72M
{
628
7.72M
    static const BYTE ML_Code[128] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
629
7.72M
                                      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
630
7.72M
                                      32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37,
631
7.72M
                                      38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39,
632
7.72M
                                      40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
633
7.72M
                                      41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
634
7.72M
                                      42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
635
7.72M
                                      42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 };
636
7.72M
    static const U32 ML_deltaCode = 36;
637
7.72M
    return (mlBase > 127) ? ZSTD_highbit32(mlBase) + ML_deltaCode : ML_Code[mlBase];
638
7.72M
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_MLcode
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_MLcode
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_MLcode
Unexecuted instantiation: zstd_double_fast.c:ZSTD_MLcode
Unexecuted instantiation: zstd_fast.c:ZSTD_MLcode
Unexecuted instantiation: zstd_lazy.c:ZSTD_MLcode
Unexecuted instantiation: zstd_ldm.c:ZSTD_MLcode
Unexecuted instantiation: zstd_opt.c:ZSTD_MLcode
Unexecuted instantiation: zstdmt_compress.c:ZSTD_MLcode
639
640
/* ZSTD_cParam_withinBounds:
641
 * @return 1 if value is within cParam bounds,
642
 * 0 otherwise */
643
MEM_STATIC int ZSTD_cParam_withinBounds(ZSTD_cParameter cParam, int value)
644
0
{
645
0
    ZSTD_bounds const bounds = ZSTD_cParam_getBounds(cParam);
646
0
    if (ZSTD_isError(bounds.error)) return 0;
647
0
    if (value < bounds.lowerBound) return 0;
648
0
    if (value > bounds.upperBound) return 0;
649
0
    return 1;
650
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_cParam_withinBounds
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_cParam_withinBounds
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_cParam_withinBounds
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_cParam_withinBounds
Unexecuted instantiation: zstd_double_fast.c:ZSTD_cParam_withinBounds
Unexecuted instantiation: zstd_fast.c:ZSTD_cParam_withinBounds
Unexecuted instantiation: zstd_lazy.c:ZSTD_cParam_withinBounds
Unexecuted instantiation: zstd_ldm.c:ZSTD_cParam_withinBounds
Unexecuted instantiation: zstd_opt.c:ZSTD_cParam_withinBounds
Unexecuted instantiation: zstdmt_compress.c:ZSTD_cParam_withinBounds
651
652
/* ZSTD_selectAddr:
653
 * @return index >= lowLimit ? candidate : backup,
654
 * tries to force branchless codegen. */
655
MEM_STATIC const BYTE*
656
ZSTD_selectAddr(U32 index, U32 lowLimit, const BYTE* candidate, const BYTE* backup)
657
0
{
658
0
#if defined(__GNUC__) && defined(__x86_64__)
659
0
    __asm__ (
660
0
        "cmp %1, %2\n"
661
0
        "cmova %3, %0\n"
662
0
        : "+r"(candidate)
663
0
        : "r"(index), "r"(lowLimit), "r"(backup)
664
0
        );
665
0
    return candidate;
666
#else
667
    return index >= lowLimit ? candidate : backup;
668
#endif
669
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_selectAddr
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_selectAddr
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_selectAddr
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_selectAddr
Unexecuted instantiation: zstd_double_fast.c:ZSTD_selectAddr
Unexecuted instantiation: zstd_fast.c:ZSTD_selectAddr
Unexecuted instantiation: zstd_lazy.c:ZSTD_selectAddr
Unexecuted instantiation: zstd_ldm.c:ZSTD_selectAddr
Unexecuted instantiation: zstd_opt.c:ZSTD_selectAddr
Unexecuted instantiation: zstdmt_compress.c:ZSTD_selectAddr
670
671
/* ZSTD_noCompressBlock() :
672
 * Writes uncompressed block to dst buffer from given src.
673
 * Returns the size of the block */
674
MEM_STATIC size_t
675
ZSTD_noCompressBlock(void* dst, size_t dstCapacity, const void* src, size_t srcSize, U32 lastBlock)
676
93
{
677
93
    U32 const cBlockHeader24 = lastBlock + (((U32)bt_raw)<<1) + (U32)(srcSize << 3);
678
93
    DEBUGLOG(5, "ZSTD_noCompressBlock (srcSize=%zu, dstCapacity=%zu)", srcSize, dstCapacity);
679
93
    RETURN_ERROR_IF(srcSize + ZSTD_blockHeaderSize > dstCapacity,
680
93
                    dstSize_tooSmall, "dst buf too small for uncompressed block");
681
93
    MEM_writeLE24(dst, cBlockHeader24);
682
93
    ZSTD_memcpy((BYTE*)dst + ZSTD_blockHeaderSize, src, srcSize);
683
93
    return ZSTD_blockHeaderSize + srcSize;
684
93
}
zstd_compress.c:ZSTD_noCompressBlock
Line
Count
Source
676
93
{
677
93
    U32 const cBlockHeader24 = lastBlock + (((U32)bt_raw)<<1) + (U32)(srcSize << 3);
678
93
    DEBUGLOG(5, "ZSTD_noCompressBlock (srcSize=%zu, dstCapacity=%zu)", srcSize, dstCapacity);
679
93
    RETURN_ERROR_IF(srcSize + ZSTD_blockHeaderSize > dstCapacity,
680
93
                    dstSize_tooSmall, "dst buf too small for uncompressed block");
681
93
    MEM_writeLE24(dst, cBlockHeader24);
682
93
    ZSTD_memcpy((BYTE*)dst + ZSTD_blockHeaderSize, src, srcSize);
683
93
    return ZSTD_blockHeaderSize + srcSize;
684
93
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_noCompressBlock
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_noCompressBlock
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_noCompressBlock
Unexecuted instantiation: zstd_double_fast.c:ZSTD_noCompressBlock
Unexecuted instantiation: zstd_fast.c:ZSTD_noCompressBlock
Unexecuted instantiation: zstd_lazy.c:ZSTD_noCompressBlock
Unexecuted instantiation: zstd_ldm.c:ZSTD_noCompressBlock
Unexecuted instantiation: zstd_opt.c:ZSTD_noCompressBlock
Unexecuted instantiation: zstdmt_compress.c:ZSTD_noCompressBlock
685
686
MEM_STATIC size_t
687
ZSTD_rleCompressBlock(void* dst, size_t dstCapacity, BYTE src, size_t srcSize, U32 lastBlock)
688
0
{
689
0
    BYTE* const op = (BYTE*)dst;
690
0
    U32 const cBlockHeader = lastBlock + (((U32)bt_rle)<<1) + (U32)(srcSize << 3);
691
0
    RETURN_ERROR_IF(dstCapacity < 4, dstSize_tooSmall, "");
692
0
    MEM_writeLE24(op, cBlockHeader);
693
0
    op[3] = src;
694
0
    return 4;
695
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_rleCompressBlock
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_rleCompressBlock
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_rleCompressBlock
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_rleCompressBlock
Unexecuted instantiation: zstd_double_fast.c:ZSTD_rleCompressBlock
Unexecuted instantiation: zstd_fast.c:ZSTD_rleCompressBlock
Unexecuted instantiation: zstd_lazy.c:ZSTD_rleCompressBlock
Unexecuted instantiation: zstd_ldm.c:ZSTD_rleCompressBlock
Unexecuted instantiation: zstd_opt.c:ZSTD_rleCompressBlock
Unexecuted instantiation: zstdmt_compress.c:ZSTD_rleCompressBlock
696
697
698
/* ZSTD_minGain() :
699
 * minimum compression required
700
 * to generate a compress block or a compressed literals section.
701
 * note : use same formula for both situations */
702
MEM_STATIC size_t ZSTD_minGain(size_t srcSize, ZSTD_strategy strat)
703
23.6k
{
704
23.6k
    U32 const minlog = (strat>=ZSTD_btultra) ? (U32)(strat) - 1 : 6;
705
23.6k
    ZSTD_STATIC_ASSERT(ZSTD_btultra == 8);
706
23.6k
    assert(ZSTD_cParam_withinBounds(ZSTD_c_strategy, (int)strat));
707
23.6k
    return (srcSize >> minlog) + 2;
708
23.6k
}
zstd_compress.c:ZSTD_minGain
Line
Count
Source
703
18.1k
{
704
18.1k
    U32 const minlog = (strat>=ZSTD_btultra) ? (U32)(strat) - 1 : 6;
705
18.1k
    ZSTD_STATIC_ASSERT(ZSTD_btultra == 8);
706
18.1k
    assert(ZSTD_cParam_withinBounds(ZSTD_c_strategy, (int)strat));
707
18.1k
    return (srcSize >> minlog) + 2;
708
18.1k
}
zstd_compress_literals.c:ZSTD_minGain
Line
Count
Source
703
5.48k
{
704
5.48k
    U32 const minlog = (strat>=ZSTD_btultra) ? (U32)(strat) - 1 : 6;
705
5.48k
    ZSTD_STATIC_ASSERT(ZSTD_btultra == 8);
706
5.48k
    assert(ZSTD_cParam_withinBounds(ZSTD_c_strategy, (int)strat));
707
5.48k
    return (srcSize >> minlog) + 2;
708
5.48k
}
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_minGain
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_minGain
Unexecuted instantiation: zstd_double_fast.c:ZSTD_minGain
Unexecuted instantiation: zstd_fast.c:ZSTD_minGain
Unexecuted instantiation: zstd_lazy.c:ZSTD_minGain
Unexecuted instantiation: zstd_ldm.c:ZSTD_minGain
Unexecuted instantiation: zstd_opt.c:ZSTD_minGain
Unexecuted instantiation: zstdmt_compress.c:ZSTD_minGain
709
710
MEM_STATIC int ZSTD_literalsCompressionIsDisabled(const ZSTD_CCtx_params* cctxParams)
711
18.1k
{
712
18.1k
    switch (cctxParams->literalCompressionMode) {
713
0
    case ZSTD_ps_enable:
714
0
        return 0;
715
0
    case ZSTD_ps_disable:
716
0
        return 1;
717
0
    default:
718
0
        assert(0 /* impossible: pre-validated */);
719
0
        ZSTD_FALLTHROUGH;
720
18.1k
    case ZSTD_ps_auto:
721
18.1k
        return (cctxParams->cParams.strategy == ZSTD_fast) && (cctxParams->cParams.targetLength > 0);
722
18.1k
    }
723
18.1k
}
zstd_compress.c:ZSTD_literalsCompressionIsDisabled
Line
Count
Source
711
18.1k
{
712
18.1k
    switch (cctxParams->literalCompressionMode) {
713
0
    case ZSTD_ps_enable:
714
0
        return 0;
715
0
    case ZSTD_ps_disable:
716
0
        return 1;
717
0
    default:
718
0
        assert(0 /* impossible: pre-validated */);
719
0
        ZSTD_FALLTHROUGH;
720
18.1k
    case ZSTD_ps_auto:
721
18.1k
        return (cctxParams->cParams.strategy == ZSTD_fast) && (cctxParams->cParams.targetLength > 0);
722
18.1k
    }
723
18.1k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_literalsCompressionIsDisabled
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_literalsCompressionIsDisabled
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_literalsCompressionIsDisabled
Unexecuted instantiation: zstd_double_fast.c:ZSTD_literalsCompressionIsDisabled
Unexecuted instantiation: zstd_fast.c:ZSTD_literalsCompressionIsDisabled
Unexecuted instantiation: zstd_lazy.c:ZSTD_literalsCompressionIsDisabled
Unexecuted instantiation: zstd_ldm.c:ZSTD_literalsCompressionIsDisabled
Unexecuted instantiation: zstd_opt.c:ZSTD_literalsCompressionIsDisabled
Unexecuted instantiation: zstdmt_compress.c:ZSTD_literalsCompressionIsDisabled
724
725
/*! ZSTD_safecopyLiterals() :
726
 *  memcpy() function that won't read beyond more than WILDCOPY_OVERLENGTH bytes past ilimit_w.
727
 *  Only called when the sequence ends past ilimit_w, so it only needs to be optimized for single
728
 *  large copies.
729
 */
730
static void
731
ZSTD_safecopyLiterals(BYTE* op, BYTE const* ip, BYTE const* const iend, BYTE const* ilimit_w)
732
3.22k
{
733
3.22k
    assert(iend > ilimit_w);
734
3.22k
    if (ip <= ilimit_w) {
735
445
        ZSTD_wildcopy(op, ip, (size_t)(ilimit_w - ip), ZSTD_no_overlap);
736
445
        op += ilimit_w - ip;
737
445
        ip = ilimit_w;
738
445
    }
739
7.86k
    while (ip < iend) *op++ = *ip++;
740
3.22k
}
Unexecuted instantiation: zstd_compress.c:ZSTD_safecopyLiterals
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_safecopyLiterals
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_safecopyLiterals
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_safecopyLiterals
Unexecuted instantiation: zstd_double_fast.c:ZSTD_safecopyLiterals
Unexecuted instantiation: zstd_fast.c:ZSTD_safecopyLiterals
zstd_lazy.c:ZSTD_safecopyLiterals
Line
Count
Source
732
3.22k
{
733
3.22k
    assert(iend > ilimit_w);
734
3.22k
    if (ip <= ilimit_w) {
735
445
        ZSTD_wildcopy(op, ip, (size_t)(ilimit_w - ip), ZSTD_no_overlap);
736
445
        op += ilimit_w - ip;
737
445
        ip = ilimit_w;
738
445
    }
739
7.86k
    while (ip < iend) *op++ = *ip++;
740
3.22k
}
Unexecuted instantiation: zstd_ldm.c:ZSTD_safecopyLiterals
Unexecuted instantiation: zstd_opt.c:ZSTD_safecopyLiterals
Unexecuted instantiation: zstdmt_compress.c:ZSTD_safecopyLiterals
741
742
743
10.8M
#define REPCODE1_TO_OFFBASE REPCODE_TO_OFFBASE(1)
744
#define REPCODE2_TO_OFFBASE REPCODE_TO_OFFBASE(2)
745
0
#define REPCODE3_TO_OFFBASE REPCODE_TO_OFFBASE(3)
746
10.8M
#define REPCODE_TO_OFFBASE(r) (assert((r)>=1), assert((r)<=ZSTD_REP_NUM), (r)) /* accepts IDs 1,2,3 */
747
55.7M
#define OFFSET_TO_OFFBASE(o)  (assert((o)>0), o + ZSTD_REP_NUM)
748
6.04M
#define OFFBASE_IS_OFFSET(o)  ((o) > ZSTD_REP_NUM)
749
0
#define OFFBASE_IS_REPCODE(o) ( 1 <= (o) && (o) <= ZSTD_REP_NUM)
750
14.2M
#define OFFBASE_TO_OFFSET(o)  (assert(OFFBASE_IS_OFFSET(o)), (o) - ZSTD_REP_NUM)
751
0
#define OFFBASE_TO_REPCODE(o) (assert(OFFBASE_IS_REPCODE(o)), (o))  /* returns ID 1,2,3 */
752
753
/*! ZSTD_storeSeqOnly() :
754
 *  Store a sequence (litlen, litPtr, offBase and matchLength) into SeqStore_t.
755
 *  Literals themselves are not copied, but @litPtr is updated.
756
 *  @offBase : Users should employ macros REPCODE_TO_OFFBASE() and OFFSET_TO_OFFBASE().
757
 *  @matchLength : must be >= MINMATCH
758
*/
759
HINT_INLINE UNUSED_ATTR void
760
ZSTD_storeSeqOnly(SeqStore_t* seqStorePtr,
761
              size_t litLength,
762
              U32 offBase,
763
              size_t matchLength)
764
7.72M
{
765
7.72M
    assert((size_t)(seqStorePtr->sequences - seqStorePtr->sequencesStart) < seqStorePtr->maxNbSeq);
766
767
    /* literal Length */
768
7.72M
    assert(litLength <= ZSTD_BLOCKSIZE_MAX);
769
7.72M
    if (UNLIKELY(litLength>0xFFFF)) {
770
0
        assert(seqStorePtr->longLengthType == ZSTD_llt_none); /* there can only be a single long length */
771
0
        seqStorePtr->longLengthType = ZSTD_llt_literalLength;
772
0
        seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
773
0
    }
774
7.72M
    seqStorePtr->sequences[0].litLength = (U16)litLength;
775
776
    /* match offset */
777
7.72M
    seqStorePtr->sequences[0].offBase = offBase;
778
779
    /* match Length */
780
7.72M
    assert(matchLength <= ZSTD_BLOCKSIZE_MAX);
781
7.72M
    assert(matchLength >= MINMATCH);
782
7.72M
    {   size_t const mlBase = matchLength - MINMATCH;
783
7.72M
        if (UNLIKELY(mlBase>0xFFFF)) {
784
7.38k
            assert(seqStorePtr->longLengthType == ZSTD_llt_none); /* there can only be a single long length */
785
7.38k
            seqStorePtr->longLengthType = ZSTD_llt_matchLength;
786
7.38k
            seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
787
7.38k
        }
788
7.72M
        seqStorePtr->sequences[0].mlBase = (U16)mlBase;
789
7.72M
    }
790
791
7.72M
    seqStorePtr->sequences++;
792
7.72M
}
Unexecuted instantiation: zstd_compress.c:ZSTD_storeSeqOnly
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_storeSeqOnly
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_storeSeqOnly
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_storeSeqOnly
Unexecuted instantiation: zstd_double_fast.c:ZSTD_storeSeqOnly
Unexecuted instantiation: zstd_fast.c:ZSTD_storeSeqOnly
zstd_lazy.c:ZSTD_storeSeqOnly
Line
Count
Source
764
7.72M
{
765
7.72M
    assert((size_t)(seqStorePtr->sequences - seqStorePtr->sequencesStart) < seqStorePtr->maxNbSeq);
766
767
    /* literal Length */
768
7.72M
    assert(litLength <= ZSTD_BLOCKSIZE_MAX);
769
7.72M
    if (UNLIKELY(litLength>0xFFFF)) {
770
0
        assert(seqStorePtr->longLengthType == ZSTD_llt_none); /* there can only be a single long length */
771
0
        seqStorePtr->longLengthType = ZSTD_llt_literalLength;
772
0
        seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
773
0
    }
774
7.72M
    seqStorePtr->sequences[0].litLength = (U16)litLength;
775
776
    /* match offset */
777
7.72M
    seqStorePtr->sequences[0].offBase = offBase;
778
779
    /* match Length */
780
7.72M
    assert(matchLength <= ZSTD_BLOCKSIZE_MAX);
781
7.72M
    assert(matchLength >= MINMATCH);
782
7.72M
    {   size_t const mlBase = matchLength - MINMATCH;
783
7.72M
        if (UNLIKELY(mlBase>0xFFFF)) {
784
7.38k
            assert(seqStorePtr->longLengthType == ZSTD_llt_none); /* there can only be a single long length */
785
7.38k
            seqStorePtr->longLengthType = ZSTD_llt_matchLength;
786
7.38k
            seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
787
7.38k
        }
788
7.72M
        seqStorePtr->sequences[0].mlBase = (U16)mlBase;
789
7.72M
    }
790
791
7.72M
    seqStorePtr->sequences++;
792
7.72M
}
Unexecuted instantiation: zstd_ldm.c:ZSTD_storeSeqOnly
Unexecuted instantiation: zstd_opt.c:ZSTD_storeSeqOnly
Unexecuted instantiation: zstdmt_compress.c:ZSTD_storeSeqOnly
793
794
/*! ZSTD_storeSeq() :
795
 *  Store a sequence (litlen, litPtr, offBase and matchLength) into SeqStore_t.
796
 *  @offBase : Users should employ macros REPCODE_TO_OFFBASE() and OFFSET_TO_OFFBASE().
797
 *  @matchLength : must be >= MINMATCH
798
 *  Allowed to over-read literals up to litLimit.
799
*/
800
HINT_INLINE UNUSED_ATTR void
801
ZSTD_storeSeq(SeqStore_t* seqStorePtr,
802
              size_t litLength, const BYTE* literals, const BYTE* litLimit,
803
              U32 offBase,
804
              size_t matchLength)
805
7.72M
{
806
7.72M
    BYTE const* const litLimit_w = litLimit - WILDCOPY_OVERLENGTH;
807
7.72M
    BYTE const* const litEnd = literals + litLength;
808
#if defined(DEBUGLEVEL) && (DEBUGLEVEL >= 6)
809
    static const BYTE* g_start = NULL;
810
    if (g_start==NULL) g_start = (const BYTE*)literals;  /* note : index only works for compression within a single segment */
811
    {   U32 const pos = (U32)((const BYTE*)literals - g_start);
812
        DEBUGLOG(6, "Cpos%7u :%3u literals, match%4u bytes at offBase%7u",
813
               pos, (U32)litLength, (U32)matchLength, (U32)offBase);
814
    }
815
#endif
816
7.72M
    assert((size_t)(seqStorePtr->sequences - seqStorePtr->sequencesStart) < seqStorePtr->maxNbSeq);
817
    /* copy Literals */
818
7.72M
    assert(seqStorePtr->maxNbLit <= 128 KB);
819
7.72M
    assert(seqStorePtr->lit + litLength <= seqStorePtr->litStart + seqStorePtr->maxNbLit);
820
7.72M
    assert(literals + litLength <= litLimit);
821
7.72M
    if (litEnd <= litLimit_w) {
822
        /* Common case we can use wildcopy.
823
         * First copy 16 bytes, because literals are likely short.
824
         */
825
7.72M
        ZSTD_STATIC_ASSERT(WILDCOPY_OVERLENGTH >= 16);
826
7.72M
        ZSTD_copy16(seqStorePtr->lit, literals);
827
7.72M
        if (litLength > 16) {
828
29.6k
            ZSTD_wildcopy(seqStorePtr->lit+16, literals+16, litLength-16, ZSTD_no_overlap);
829
29.6k
        }
830
7.72M
    } else {
831
3.22k
        ZSTD_safecopyLiterals(seqStorePtr->lit, literals, litEnd, litLimit_w);
832
3.22k
    }
833
7.72M
    seqStorePtr->lit += litLength;
834
835
7.72M
    ZSTD_storeSeqOnly(seqStorePtr, litLength, offBase, matchLength);
836
7.72M
}
Unexecuted instantiation: zstd_compress.c:ZSTD_storeSeq
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_storeSeq
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_storeSeq
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_storeSeq
Unexecuted instantiation: zstd_double_fast.c:ZSTD_storeSeq
Unexecuted instantiation: zstd_fast.c:ZSTD_storeSeq
zstd_lazy.c:ZSTD_storeSeq
Line
Count
Source
805
7.72M
{
806
7.72M
    BYTE const* const litLimit_w = litLimit - WILDCOPY_OVERLENGTH;
807
7.72M
    BYTE const* const litEnd = literals + litLength;
808
#if defined(DEBUGLEVEL) && (DEBUGLEVEL >= 6)
809
    static const BYTE* g_start = NULL;
810
    if (g_start==NULL) g_start = (const BYTE*)literals;  /* note : index only works for compression within a single segment */
811
    {   U32 const pos = (U32)((const BYTE*)literals - g_start);
812
        DEBUGLOG(6, "Cpos%7u :%3u literals, match%4u bytes at offBase%7u",
813
               pos, (U32)litLength, (U32)matchLength, (U32)offBase);
814
    }
815
#endif
816
7.72M
    assert((size_t)(seqStorePtr->sequences - seqStorePtr->sequencesStart) < seqStorePtr->maxNbSeq);
817
    /* copy Literals */
818
7.72M
    assert(seqStorePtr->maxNbLit <= 128 KB);
819
7.72M
    assert(seqStorePtr->lit + litLength <= seqStorePtr->litStart + seqStorePtr->maxNbLit);
820
7.72M
    assert(literals + litLength <= litLimit);
821
7.72M
    if (litEnd <= litLimit_w) {
822
        /* Common case we can use wildcopy.
823
         * First copy 16 bytes, because literals are likely short.
824
         */
825
7.72M
        ZSTD_STATIC_ASSERT(WILDCOPY_OVERLENGTH >= 16);
826
7.72M
        ZSTD_copy16(seqStorePtr->lit, literals);
827
7.72M
        if (litLength > 16) {
828
29.6k
            ZSTD_wildcopy(seqStorePtr->lit+16, literals+16, litLength-16, ZSTD_no_overlap);
829
29.6k
        }
830
7.72M
    } else {
831
3.22k
        ZSTD_safecopyLiterals(seqStorePtr->lit, literals, litEnd, litLimit_w);
832
3.22k
    }
833
7.72M
    seqStorePtr->lit += litLength;
834
835
7.72M
    ZSTD_storeSeqOnly(seqStorePtr, litLength, offBase, matchLength);
836
7.72M
}
Unexecuted instantiation: zstd_ldm.c:ZSTD_storeSeq
Unexecuted instantiation: zstd_opt.c:ZSTD_storeSeq
Unexecuted instantiation: zstdmt_compress.c:ZSTD_storeSeq
837
838
/* ZSTD_updateRep() :
839
 * updates in-place @rep (array of repeat offsets)
840
 * @offBase : sum-type, using numeric representation of ZSTD_storeSeq()
841
 */
842
MEM_STATIC void
843
ZSTD_updateRep(U32 rep[ZSTD_REP_NUM], U32 const offBase, U32 const ll0)
844
0
{
845
0
    if (OFFBASE_IS_OFFSET(offBase)) {  /* full offset */
846
0
        rep[2] = rep[1];
847
0
        rep[1] = rep[0];
848
0
        rep[0] = OFFBASE_TO_OFFSET(offBase);
849
0
    } else {   /* repcode */
850
0
        U32 const repCode = OFFBASE_TO_REPCODE(offBase) - 1 + ll0;
851
0
        if (repCode > 0) {  /* note : if repCode==0, no change */
852
0
            U32 const currentOffset = (repCode==ZSTD_REP_NUM) ? (rep[0] - 1) : rep[repCode];
853
0
            rep[2] = (repCode >= 2) ? rep[1] : rep[2];
854
0
            rep[1] = rep[0];
855
0
            rep[0] = currentOffset;
856
0
        } else {   /* repCode == 0 */
857
            /* nothing to do */
858
0
        }
859
0
    }
860
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_updateRep
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_updateRep
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_updateRep
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_updateRep
Unexecuted instantiation: zstd_double_fast.c:ZSTD_updateRep
Unexecuted instantiation: zstd_fast.c:ZSTD_updateRep
Unexecuted instantiation: zstd_lazy.c:ZSTD_updateRep
Unexecuted instantiation: zstd_ldm.c:ZSTD_updateRep
Unexecuted instantiation: zstd_opt.c:ZSTD_updateRep
Unexecuted instantiation: zstdmt_compress.c:ZSTD_updateRep
861
862
typedef struct repcodes_s {
863
    U32 rep[3];
864
} Repcodes_t;
865
866
MEM_STATIC Repcodes_t
867
ZSTD_newRep(U32 const rep[ZSTD_REP_NUM], U32 const offBase, U32 const ll0)
868
0
{
869
0
    Repcodes_t newReps;
870
0
    ZSTD_memcpy(&newReps, rep, sizeof(newReps));
871
0
    ZSTD_updateRep(newReps.rep, offBase, ll0);
872
0
    return newReps;
873
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_newRep
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_newRep
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_newRep
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_newRep
Unexecuted instantiation: zstd_double_fast.c:ZSTD_newRep
Unexecuted instantiation: zstd_fast.c:ZSTD_newRep
Unexecuted instantiation: zstd_lazy.c:ZSTD_newRep
Unexecuted instantiation: zstd_ldm.c:ZSTD_newRep
Unexecuted instantiation: zstd_opt.c:ZSTD_newRep
Unexecuted instantiation: zstdmt_compress.c:ZSTD_newRep
874
875
876
/*-*************************************
877
*  Match length counter
878
***************************************/
879
MEM_STATIC size_t ZSTD_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* const pInLimit)
880
69.3M
{
881
69.3M
    const BYTE* const pStart = pIn;
882
69.3M
    const BYTE* const pInLoopLimit = pInLimit - (sizeof(size_t)-1);
883
884
69.3M
    if (pIn < pInLoopLimit) {
885
69.3M
        { size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
886
69.3M
          if (diff) return ZSTD_NbCommonBytes(diff); }
887
36.4M
        pIn+=sizeof(size_t); pMatch+=sizeof(size_t);
888
764M
        while (pIn < pInLoopLimit) {
889
764M
            size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
890
764M
            if (!diff) { pIn+=sizeof(size_t); pMatch+=sizeof(size_t); continue; }
891
36.4M
            pIn += ZSTD_NbCommonBytes(diff);
892
36.4M
            return (size_t)(pIn - pStart);
893
764M
    }   }
894
36.9k
    if (MEM_64bits() && (pIn<(pInLimit-3)) && (MEM_read32(pMatch) == MEM_read32(pIn))) { pIn+=4; pMatch+=4; }
895
36.9k
    if ((pIn<(pInLimit-1)) && (MEM_read16(pMatch) == MEM_read16(pIn))) { pIn+=2; pMatch+=2; }
896
36.9k
    if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
897
36.9k
    return (size_t)(pIn - pStart);
898
69.3M
}
zstd_compress.c:ZSTD_count
Line
Count
Source
880
1.13k
{
881
1.13k
    const BYTE* const pStart = pIn;
882
1.13k
    const BYTE* const pInLoopLimit = pInLimit - (sizeof(size_t)-1);
883
884
1.13k
    if (pIn < pInLoopLimit) {
885
743
        { size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
886
743
          if (diff) return ZSTD_NbCommonBytes(diff); }
887
226
        pIn+=sizeof(size_t); pMatch+=sizeof(size_t);
888
366
        while (pIn < pInLoopLimit) {
889
184
            size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
890
184
            if (!diff) { pIn+=sizeof(size_t); pMatch+=sizeof(size_t); continue; }
891
44
            pIn += ZSTD_NbCommonBytes(diff);
892
44
            return (size_t)(pIn - pStart);
893
184
    }   }
894
576
    if (MEM_64bits() && (pIn<(pInLimit-3)) && (MEM_read32(pMatch) == MEM_read32(pIn))) { pIn+=4; pMatch+=4; }
895
576
    if ((pIn<(pInLimit-1)) && (MEM_read16(pMatch) == MEM_read16(pIn))) { pIn+=2; pMatch+=2; }
896
576
    if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
897
576
    return (size_t)(pIn - pStart);
898
1.13k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_count
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_count
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_count
Unexecuted instantiation: zstd_double_fast.c:ZSTD_count
Unexecuted instantiation: zstd_fast.c:ZSTD_count
zstd_lazy.c:ZSTD_count
Line
Count
Source
880
69.3M
{
881
69.3M
    const BYTE* const pStart = pIn;
882
69.3M
    const BYTE* const pInLoopLimit = pInLimit - (sizeof(size_t)-1);
883
884
69.3M
    if (pIn < pInLoopLimit) {
885
69.3M
        { size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
886
69.3M
          if (diff) return ZSTD_NbCommonBytes(diff); }
887
36.4M
        pIn+=sizeof(size_t); pMatch+=sizeof(size_t);
888
764M
        while (pIn < pInLoopLimit) {
889
764M
            size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
890
764M
            if (!diff) { pIn+=sizeof(size_t); pMatch+=sizeof(size_t); continue; }
891
36.4M
            pIn += ZSTD_NbCommonBytes(diff);
892
36.4M
            return (size_t)(pIn - pStart);
893
764M
    }   }
894
36.3k
    if (MEM_64bits() && (pIn<(pInLimit-3)) && (MEM_read32(pMatch) == MEM_read32(pIn))) { pIn+=4; pMatch+=4; }
895
36.3k
    if ((pIn<(pInLimit-1)) && (MEM_read16(pMatch) == MEM_read16(pIn))) { pIn+=2; pMatch+=2; }
896
36.3k
    if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
897
36.3k
    return (size_t)(pIn - pStart);
898
69.3M
}
Unexecuted instantiation: zstd_ldm.c:ZSTD_count
Unexecuted instantiation: zstd_opt.c:ZSTD_count
Unexecuted instantiation: zstdmt_compress.c:ZSTD_count
899
900
/** ZSTD_count_2segments() :
901
 *  can count match length with `ip` & `match` in 2 different segments.
902
 *  convention : on reaching mEnd, match count continue starting from iStart
903
 */
904
MEM_STATIC size_t
905
ZSTD_count_2segments(const BYTE* ip, const BYTE* match,
906
                     const BYTE* iEnd, const BYTE* mEnd, const BYTE* iStart)
907
0
{
908
0
    const BYTE* const vEnd = MIN( ip + (mEnd - match), iEnd);
909
0
    size_t const matchLength = ZSTD_count(ip, match, vEnd);
910
0
    if (match + matchLength != mEnd) return matchLength;
911
0
    DEBUGLOG(7, "ZSTD_count_2segments: found a 2-parts match (current length==%zu)", matchLength);
912
0
    DEBUGLOG(7, "distance from match beginning to end dictionary = %i", (int)(mEnd - match));
913
0
    DEBUGLOG(7, "distance from current pos to end buffer = %i", (int)(iEnd - ip));
914
0
    DEBUGLOG(7, "next byte : ip==%02X, istart==%02X", ip[matchLength], *iStart);
915
0
    DEBUGLOG(7, "final match length = %zu", matchLength + ZSTD_count(ip+matchLength, iStart, iEnd));
916
0
    return matchLength + ZSTD_count(ip+matchLength, iStart, iEnd);
917
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_count_2segments
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_count_2segments
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_count_2segments
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_count_2segments
Unexecuted instantiation: zstd_double_fast.c:ZSTD_count_2segments
Unexecuted instantiation: zstd_fast.c:ZSTD_count_2segments
Unexecuted instantiation: zstd_lazy.c:ZSTD_count_2segments
Unexecuted instantiation: zstd_ldm.c:ZSTD_count_2segments
Unexecuted instantiation: zstd_opt.c:ZSTD_count_2segments
Unexecuted instantiation: zstdmt_compress.c:ZSTD_count_2segments
918
919
920
/*-*************************************
921
 *  Hashes
922
 ***************************************/
923
static const U32 prime3bytes = 506832829U;
924
0
static U32    ZSTD_hash3(U32 u, U32 h, U32 s) { assert(h <= 32); return (((u << (32-24)) * prime3bytes) ^ s)  >> (32-h) ; }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash3
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash3
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash3
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash3
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash3
Unexecuted instantiation: zstd_fast.c:ZSTD_hash3
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash3
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash3
Unexecuted instantiation: zstd_opt.c:ZSTD_hash3
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash3
925
0
MEM_STATIC size_t ZSTD_hash3Ptr(const void* ptr, U32 h) { return ZSTD_hash3(MEM_readLE32(ptr), h, 0); } /* only in zstd_opt.h */
Unexecuted instantiation: zstd_compress.c:ZSTD_hash3Ptr
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash3Ptr
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash3Ptr
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash3Ptr
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash3Ptr
Unexecuted instantiation: zstd_fast.c:ZSTD_hash3Ptr
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash3Ptr
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash3Ptr
Unexecuted instantiation: zstd_opt.c:ZSTD_hash3Ptr
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash3Ptr
926
0
MEM_STATIC size_t ZSTD_hash3PtrS(const void* ptr, U32 h, U32 s) { return ZSTD_hash3(MEM_readLE32(ptr), h, s); }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash3PtrS
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash3PtrS
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash3PtrS
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash3PtrS
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash3PtrS
Unexecuted instantiation: zstd_fast.c:ZSTD_hash3PtrS
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash3PtrS
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash3PtrS
Unexecuted instantiation: zstd_opt.c:ZSTD_hash3PtrS
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash3PtrS
927
928
static const U32 prime4bytes = 2654435761U;
929
0
static U32    ZSTD_hash4(U32 u, U32 h, U32 s) { assert(h <= 32); return ((u * prime4bytes) ^ s) >> (32-h) ; }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash4
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash4
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash4
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash4
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash4
Unexecuted instantiation: zstd_fast.c:ZSTD_hash4
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash4
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash4
Unexecuted instantiation: zstd_opt.c:ZSTD_hash4
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash4
930
0
static size_t ZSTD_hash4Ptr(const void* ptr, U32 h) { return ZSTD_hash4(MEM_readLE32(ptr), h, 0); }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash4Ptr
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash4Ptr
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash4Ptr
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash4Ptr
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash4Ptr
Unexecuted instantiation: zstd_fast.c:ZSTD_hash4Ptr
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash4Ptr
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash4Ptr
Unexecuted instantiation: zstd_opt.c:ZSTD_hash4Ptr
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash4Ptr
931
0
static size_t ZSTD_hash4PtrS(const void* ptr, U32 h, U32 s) { return ZSTD_hash4(MEM_readLE32(ptr), h, s); }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash4PtrS
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash4PtrS
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash4PtrS
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash4PtrS
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash4PtrS
Unexecuted instantiation: zstd_fast.c:ZSTD_hash4PtrS
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash4PtrS
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash4PtrS
Unexecuted instantiation: zstd_opt.c:ZSTD_hash4PtrS
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash4PtrS
932
933
static const U64 prime5bytes = 889523592379ULL;
934
160M
static size_t ZSTD_hash5(U64 u, U32 h, U64 s) { assert(h <= 64); return (size_t)((((u  << (64-40)) * prime5bytes) ^ s) >> (64-h)) ; }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash5
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash5
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash5
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash5
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash5
Unexecuted instantiation: zstd_fast.c:ZSTD_hash5
zstd_lazy.c:ZSTD_hash5
Line
Count
Source
934
160M
static size_t ZSTD_hash5(U64 u, U32 h, U64 s) { assert(h <= 64); return (size_t)((((u  << (64-40)) * prime5bytes) ^ s) >> (64-h)) ; }
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash5
Unexecuted instantiation: zstd_opt.c:ZSTD_hash5
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash5
935
0
static size_t ZSTD_hash5Ptr(const void* p, U32 h) { return ZSTD_hash5(MEM_readLE64(p), h, 0); }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash5Ptr
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash5Ptr
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash5Ptr
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash5Ptr
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash5Ptr
Unexecuted instantiation: zstd_fast.c:ZSTD_hash5Ptr
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash5Ptr
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash5Ptr
Unexecuted instantiation: zstd_opt.c:ZSTD_hash5Ptr
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash5Ptr
936
160M
static size_t ZSTD_hash5PtrS(const void* p, U32 h, U64 s) { return ZSTD_hash5(MEM_readLE64(p), h, s); }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash5PtrS
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash5PtrS
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash5PtrS
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash5PtrS
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash5PtrS
Unexecuted instantiation: zstd_fast.c:ZSTD_hash5PtrS
zstd_lazy.c:ZSTD_hash5PtrS
Line
Count
Source
936
160M
static size_t ZSTD_hash5PtrS(const void* p, U32 h, U64 s) { return ZSTD_hash5(MEM_readLE64(p), h, s); }
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash5PtrS
Unexecuted instantiation: zstd_opt.c:ZSTD_hash5PtrS
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash5PtrS
937
938
static const U64 prime6bytes = 227718039650203ULL;
939
0
static size_t ZSTD_hash6(U64 u, U32 h, U64 s) { assert(h <= 64); return (size_t)((((u  << (64-48)) * prime6bytes) ^ s) >> (64-h)) ; }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash6
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash6
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash6
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash6
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash6
Unexecuted instantiation: zstd_fast.c:ZSTD_hash6
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash6
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash6
Unexecuted instantiation: zstd_opt.c:ZSTD_hash6
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash6
940
0
static size_t ZSTD_hash6Ptr(const void* p, U32 h) { return ZSTD_hash6(MEM_readLE64(p), h, 0); }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash6Ptr
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash6Ptr
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash6Ptr
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash6Ptr
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash6Ptr
Unexecuted instantiation: zstd_fast.c:ZSTD_hash6Ptr
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash6Ptr
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash6Ptr
Unexecuted instantiation: zstd_opt.c:ZSTD_hash6Ptr
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash6Ptr
941
0
static size_t ZSTD_hash6PtrS(const void* p, U32 h, U64 s) { return ZSTD_hash6(MEM_readLE64(p), h, s); }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash6PtrS
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash6PtrS
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash6PtrS
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash6PtrS
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash6PtrS
Unexecuted instantiation: zstd_fast.c:ZSTD_hash6PtrS
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash6PtrS
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash6PtrS
Unexecuted instantiation: zstd_opt.c:ZSTD_hash6PtrS
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash6PtrS
942
943
static const U64 prime7bytes = 58295818150454627ULL;
944
0
static size_t ZSTD_hash7(U64 u, U32 h, U64 s) { assert(h <= 64); return (size_t)((((u  << (64-56)) * prime7bytes) ^ s) >> (64-h)) ; }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash7
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash7
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash7
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash7
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash7
Unexecuted instantiation: zstd_fast.c:ZSTD_hash7
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash7
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash7
Unexecuted instantiation: zstd_opt.c:ZSTD_hash7
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash7
945
0
static size_t ZSTD_hash7Ptr(const void* p, U32 h) { return ZSTD_hash7(MEM_readLE64(p), h, 0); }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash7Ptr
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash7Ptr
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash7Ptr
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash7Ptr
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash7Ptr
Unexecuted instantiation: zstd_fast.c:ZSTD_hash7Ptr
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash7Ptr
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash7Ptr
Unexecuted instantiation: zstd_opt.c:ZSTD_hash7Ptr
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash7Ptr
946
0
static size_t ZSTD_hash7PtrS(const void* p, U32 h, U64 s) { return ZSTD_hash7(MEM_readLE64(p), h, s); }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash7PtrS
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash7PtrS
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash7PtrS
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash7PtrS
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash7PtrS
Unexecuted instantiation: zstd_fast.c:ZSTD_hash7PtrS
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash7PtrS
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash7PtrS
Unexecuted instantiation: zstd_opt.c:ZSTD_hash7PtrS
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash7PtrS
947
948
static const U64 prime8bytes = 0xCF1BBCDCB7A56463ULL;
949
0
static size_t ZSTD_hash8(U64 u, U32 h, U64 s) { assert(h <= 64); return (size_t)((((u) * prime8bytes)  ^ s) >> (64-h)) ; }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash8
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash8
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash8
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash8
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash8
Unexecuted instantiation: zstd_fast.c:ZSTD_hash8
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash8
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash8
Unexecuted instantiation: zstd_opt.c:ZSTD_hash8
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash8
950
0
static size_t ZSTD_hash8Ptr(const void* p, U32 h) { return ZSTD_hash8(MEM_readLE64(p), h, 0); }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash8Ptr
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash8Ptr
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash8Ptr
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash8Ptr
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash8Ptr
Unexecuted instantiation: zstd_fast.c:ZSTD_hash8Ptr
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash8Ptr
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash8Ptr
Unexecuted instantiation: zstd_opt.c:ZSTD_hash8Ptr
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash8Ptr
951
0
static size_t ZSTD_hash8PtrS(const void* p, U32 h, U64 s) { return ZSTD_hash8(MEM_readLE64(p), h, s); }
Unexecuted instantiation: zstd_compress.c:ZSTD_hash8PtrS
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hash8PtrS
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hash8PtrS
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hash8PtrS
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hash8PtrS
Unexecuted instantiation: zstd_fast.c:ZSTD_hash8PtrS
Unexecuted instantiation: zstd_lazy.c:ZSTD_hash8PtrS
Unexecuted instantiation: zstd_ldm.c:ZSTD_hash8PtrS
Unexecuted instantiation: zstd_opt.c:ZSTD_hash8PtrS
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hash8PtrS
952
953
954
MEM_STATIC FORCE_INLINE_ATTR
955
size_t ZSTD_hashPtr(const void* p, U32 hBits, U32 mls)
956
0
{
957
    /* Although some of these hashes do support hBits up to 64, some do not.
958
     * To be on the safe side, always avoid hBits > 32. */
959
0
    assert(hBits <= 32);
960
961
0
    switch(mls)
962
0
    {
963
0
    default:
964
0
    case 4: return ZSTD_hash4Ptr(p, hBits);
965
0
    case 5: return ZSTD_hash5Ptr(p, hBits);
966
0
    case 6: return ZSTD_hash6Ptr(p, hBits);
967
0
    case 7: return ZSTD_hash7Ptr(p, hBits);
968
0
    case 8: return ZSTD_hash8Ptr(p, hBits);
969
0
    }
970
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_hashPtr
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hashPtr
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hashPtr
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hashPtr
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hashPtr
Unexecuted instantiation: zstd_fast.c:ZSTD_hashPtr
Unexecuted instantiation: zstd_lazy.c:ZSTD_hashPtr
Unexecuted instantiation: zstd_ldm.c:ZSTD_hashPtr
Unexecuted instantiation: zstd_opt.c:ZSTD_hashPtr
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hashPtr
971
972
MEM_STATIC FORCE_INLINE_ATTR
973
160M
size_t ZSTD_hashPtrSalted(const void* p, U32 hBits, U32 mls, const U64 hashSalt) {
974
    /* Although some of these hashes do support hBits up to 64, some do not.
975
     * To be on the safe side, always avoid hBits > 32. */
976
160M
    assert(hBits <= 32);
977
978
160M
    switch(mls)
979
160M
    {
980
0
        default:
981
0
        case 4: return ZSTD_hash4PtrS(p, hBits, (U32)hashSalt);
982
160M
        case 5: return ZSTD_hash5PtrS(p, hBits, hashSalt);
983
0
        case 6: return ZSTD_hash6PtrS(p, hBits, hashSalt);
984
0
        case 7: return ZSTD_hash7PtrS(p, hBits, hashSalt);
985
0
        case 8: return ZSTD_hash8PtrS(p, hBits, hashSalt);
986
160M
    }
987
160M
}
Unexecuted instantiation: zstd_compress.c:ZSTD_hashPtrSalted
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hashPtrSalted
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hashPtrSalted
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hashPtrSalted
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hashPtrSalted
Unexecuted instantiation: zstd_fast.c:ZSTD_hashPtrSalted
zstd_lazy.c:ZSTD_hashPtrSalted
Line
Count
Source
973
160M
size_t ZSTD_hashPtrSalted(const void* p, U32 hBits, U32 mls, const U64 hashSalt) {
974
    /* Although some of these hashes do support hBits up to 64, some do not.
975
     * To be on the safe side, always avoid hBits > 32. */
976
160M
    assert(hBits <= 32);
977
978
160M
    switch(mls)
979
160M
    {
980
0
        default:
981
0
        case 4: return ZSTD_hash4PtrS(p, hBits, (U32)hashSalt);
982
160M
        case 5: return ZSTD_hash5PtrS(p, hBits, hashSalt);
983
0
        case 6: return ZSTD_hash6PtrS(p, hBits, hashSalt);
984
0
        case 7: return ZSTD_hash7PtrS(p, hBits, hashSalt);
985
0
        case 8: return ZSTD_hash8PtrS(p, hBits, hashSalt);
986
160M
    }
987
160M
}
Unexecuted instantiation: zstd_ldm.c:ZSTD_hashPtrSalted
Unexecuted instantiation: zstd_opt.c:ZSTD_hashPtrSalted
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hashPtrSalted
988
989
990
/** ZSTD_ipow() :
991
 * Return base^exponent.
992
 */
993
static U64 ZSTD_ipow(U64 base, U64 exponent)
994
0
{
995
0
    U64 power = 1;
996
0
    while (exponent) {
997
0
      if (exponent & 1) power *= base;
998
0
      exponent >>= 1;
999
0
      base *= base;
1000
0
    }
1001
0
    return power;
1002
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_ipow
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_ipow
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_ipow
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_ipow
Unexecuted instantiation: zstd_double_fast.c:ZSTD_ipow
Unexecuted instantiation: zstd_fast.c:ZSTD_ipow
Unexecuted instantiation: zstd_lazy.c:ZSTD_ipow
Unexecuted instantiation: zstd_ldm.c:ZSTD_ipow
Unexecuted instantiation: zstd_opt.c:ZSTD_ipow
Unexecuted instantiation: zstdmt_compress.c:ZSTD_ipow
1003
1004
0
#define ZSTD_ROLL_HASH_CHAR_OFFSET 10
1005
1006
/** ZSTD_rollingHash_append() :
1007
 * Add the buffer to the hash value.
1008
 */
1009
static U64 ZSTD_rollingHash_append(U64 hash, void const* buf, size_t size)
1010
0
{
1011
0
    BYTE const* istart = (BYTE const*)buf;
1012
0
    size_t pos;
1013
0
    for (pos = 0; pos < size; ++pos) {
1014
0
        hash *= prime8bytes;
1015
0
        hash += istart[pos] + ZSTD_ROLL_HASH_CHAR_OFFSET;
1016
0
    }
1017
0
    return hash;
1018
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_rollingHash_append
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_rollingHash_append
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_rollingHash_append
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_rollingHash_append
Unexecuted instantiation: zstd_double_fast.c:ZSTD_rollingHash_append
Unexecuted instantiation: zstd_fast.c:ZSTD_rollingHash_append
Unexecuted instantiation: zstd_lazy.c:ZSTD_rollingHash_append
Unexecuted instantiation: zstd_ldm.c:ZSTD_rollingHash_append
Unexecuted instantiation: zstd_opt.c:ZSTD_rollingHash_append
Unexecuted instantiation: zstdmt_compress.c:ZSTD_rollingHash_append
1019
1020
/** ZSTD_rollingHash_compute() :
1021
 * Compute the rolling hash value of the buffer.
1022
 */
1023
MEM_STATIC U64 ZSTD_rollingHash_compute(void const* buf, size_t size)
1024
0
{
1025
0
    return ZSTD_rollingHash_append(0, buf, size);
1026
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_rollingHash_compute
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_rollingHash_compute
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_rollingHash_compute
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_rollingHash_compute
Unexecuted instantiation: zstd_double_fast.c:ZSTD_rollingHash_compute
Unexecuted instantiation: zstd_fast.c:ZSTD_rollingHash_compute
Unexecuted instantiation: zstd_lazy.c:ZSTD_rollingHash_compute
Unexecuted instantiation: zstd_ldm.c:ZSTD_rollingHash_compute
Unexecuted instantiation: zstd_opt.c:ZSTD_rollingHash_compute
Unexecuted instantiation: zstdmt_compress.c:ZSTD_rollingHash_compute
1027
1028
/** ZSTD_rollingHash_primePower() :
1029
 * Compute the primePower to be passed to ZSTD_rollingHash_rotate() for a hash
1030
 * over a window of length bytes.
1031
 */
1032
MEM_STATIC U64 ZSTD_rollingHash_primePower(U32 length)
1033
0
{
1034
0
    return ZSTD_ipow(prime8bytes, length - 1);
1035
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_rollingHash_primePower
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_rollingHash_primePower
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_rollingHash_primePower
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_rollingHash_primePower
Unexecuted instantiation: zstd_double_fast.c:ZSTD_rollingHash_primePower
Unexecuted instantiation: zstd_fast.c:ZSTD_rollingHash_primePower
Unexecuted instantiation: zstd_lazy.c:ZSTD_rollingHash_primePower
Unexecuted instantiation: zstd_ldm.c:ZSTD_rollingHash_primePower
Unexecuted instantiation: zstd_opt.c:ZSTD_rollingHash_primePower
Unexecuted instantiation: zstdmt_compress.c:ZSTD_rollingHash_primePower
1036
1037
/** ZSTD_rollingHash_rotate() :
1038
 * Rotate the rolling hash by one byte.
1039
 */
1040
MEM_STATIC U64 ZSTD_rollingHash_rotate(U64 hash, BYTE toRemove, BYTE toAdd, U64 primePower)
1041
0
{
1042
0
    hash -= (toRemove + ZSTD_ROLL_HASH_CHAR_OFFSET) * primePower;
1043
0
    hash *= prime8bytes;
1044
0
    hash += toAdd + ZSTD_ROLL_HASH_CHAR_OFFSET;
1045
0
    return hash;
1046
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_rollingHash_rotate
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_rollingHash_rotate
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_rollingHash_rotate
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_rollingHash_rotate
Unexecuted instantiation: zstd_double_fast.c:ZSTD_rollingHash_rotate
Unexecuted instantiation: zstd_fast.c:ZSTD_rollingHash_rotate
Unexecuted instantiation: zstd_lazy.c:ZSTD_rollingHash_rotate
Unexecuted instantiation: zstd_ldm.c:ZSTD_rollingHash_rotate
Unexecuted instantiation: zstd_opt.c:ZSTD_rollingHash_rotate
Unexecuted instantiation: zstdmt_compress.c:ZSTD_rollingHash_rotate
1047
1048
/*-*************************************
1049
*  Round buffer management
1050
***************************************/
1051
/* Max @current value allowed:
1052
 * In 32-bit mode: we want to avoid crossing the 2 GB limit,
1053
 *                 reducing risks of side effects in case of signed operations on indexes.
1054
 * In 64-bit mode: we want to ensure that adding the maximum job size (512 MB)
1055
 *                 doesn't overflow U32 index capacity (4 GB) */
1056
29.7k
#define ZSTD_CURRENT_MAX (MEM_64bits() ? 3500U MB : 2000U MB)
1057
/* Maximum chunk size before overflow correction needs to be called again */
1058
#define ZSTD_CHUNKSIZE_MAX                                                     \
1059
5.84k
    ( ((U32)-1)                  /* Maximum ending current index */            \
1060
5.84k
    - ZSTD_CURRENT_MAX)          /* Maximum beginning lowLimit */
1061
1062
/**
1063
 * ZSTD_window_clear():
1064
 * Clears the window containing the history by simply setting it to empty.
1065
 */
1066
MEM_STATIC void ZSTD_window_clear(ZSTD_window_t* window)
1067
5.84k
{
1068
5.84k
    size_t const endT = (size_t)(window->nextSrc - window->base);
1069
5.84k
    U32 const end = (U32)endT;
1070
1071
5.84k
    window->lowLimit = end;
1072
5.84k
    window->dictLimit = end;
1073
5.84k
}
zstd_compress.c:ZSTD_window_clear
Line
Count
Source
1067
5.84k
{
1068
5.84k
    size_t const endT = (size_t)(window->nextSrc - window->base);
1069
5.84k
    U32 const end = (U32)endT;
1070
1071
5.84k
    window->lowLimit = end;
1072
5.84k
    window->dictLimit = end;
1073
5.84k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_window_clear
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_window_clear
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_window_clear
Unexecuted instantiation: zstd_double_fast.c:ZSTD_window_clear
Unexecuted instantiation: zstd_fast.c:ZSTD_window_clear
Unexecuted instantiation: zstd_lazy.c:ZSTD_window_clear
Unexecuted instantiation: zstd_ldm.c:ZSTD_window_clear
Unexecuted instantiation: zstd_opt.c:ZSTD_window_clear
Unexecuted instantiation: zstdmt_compress.c:ZSTD_window_clear
1074
1075
MEM_STATIC U32 ZSTD_window_isEmpty(ZSTD_window_t const window)
1076
0
{
1077
0
    return window.dictLimit == ZSTD_WINDOW_START_INDEX &&
1078
0
           window.lowLimit == ZSTD_WINDOW_START_INDEX &&
1079
0
           (window.nextSrc - window.base) == ZSTD_WINDOW_START_INDEX;
1080
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_window_isEmpty
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_window_isEmpty
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_window_isEmpty
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_window_isEmpty
Unexecuted instantiation: zstd_double_fast.c:ZSTD_window_isEmpty
Unexecuted instantiation: zstd_fast.c:ZSTD_window_isEmpty
Unexecuted instantiation: zstd_lazy.c:ZSTD_window_isEmpty
Unexecuted instantiation: zstd_ldm.c:ZSTD_window_isEmpty
Unexecuted instantiation: zstd_opt.c:ZSTD_window_isEmpty
Unexecuted instantiation: zstdmt_compress.c:ZSTD_window_isEmpty
1081
1082
/**
1083
 * ZSTD_window_hasExtDict():
1084
 * Returns non-zero if the window has a non-empty extDict.
1085
 */
1086
MEM_STATIC U32 ZSTD_window_hasExtDict(ZSTD_window_t const window)
1087
18.1k
{
1088
18.1k
    return window.lowLimit < window.dictLimit;
1089
18.1k
}
zstd_compress.c:ZSTD_window_hasExtDict
Line
Count
Source
1087
18.1k
{
1088
18.1k
    return window.lowLimit < window.dictLimit;
1089
18.1k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_window_hasExtDict
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_window_hasExtDict
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_window_hasExtDict
Unexecuted instantiation: zstd_double_fast.c:ZSTD_window_hasExtDict
Unexecuted instantiation: zstd_fast.c:ZSTD_window_hasExtDict
Unexecuted instantiation: zstd_lazy.c:ZSTD_window_hasExtDict
Unexecuted instantiation: zstd_ldm.c:ZSTD_window_hasExtDict
Unexecuted instantiation: zstd_opt.c:ZSTD_window_hasExtDict
Unexecuted instantiation: zstdmt_compress.c:ZSTD_window_hasExtDict
1090
1091
/**
1092
 * ZSTD_matchState_dictMode():
1093
 * Inspects the provided matchState and figures out what dictMode should be
1094
 * passed to the compressor.
1095
 */
1096
MEM_STATIC ZSTD_dictMode_e ZSTD_matchState_dictMode(const ZSTD_MatchState_t *ms)
1097
18.1k
{
1098
18.1k
    return ZSTD_window_hasExtDict(ms->window) ?
1099
0
        ZSTD_extDict :
1100
18.1k
        ms->dictMatchState != NULL ?
1101
0
            (ms->dictMatchState->dedicatedDictSearch ? ZSTD_dedicatedDictSearch : ZSTD_dictMatchState) :
1102
18.1k
            ZSTD_noDict;
1103
18.1k
}
zstd_compress.c:ZSTD_matchState_dictMode
Line
Count
Source
1097
18.1k
{
1098
18.1k
    return ZSTD_window_hasExtDict(ms->window) ?
1099
0
        ZSTD_extDict :
1100
18.1k
        ms->dictMatchState != NULL ?
1101
0
            (ms->dictMatchState->dedicatedDictSearch ? ZSTD_dedicatedDictSearch : ZSTD_dictMatchState) :
1102
18.1k
            ZSTD_noDict;
1103
18.1k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_matchState_dictMode
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_matchState_dictMode
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_matchState_dictMode
Unexecuted instantiation: zstd_double_fast.c:ZSTD_matchState_dictMode
Unexecuted instantiation: zstd_fast.c:ZSTD_matchState_dictMode
Unexecuted instantiation: zstd_lazy.c:ZSTD_matchState_dictMode
Unexecuted instantiation: zstd_ldm.c:ZSTD_matchState_dictMode
Unexecuted instantiation: zstd_opt.c:ZSTD_matchState_dictMode
Unexecuted instantiation: zstdmt_compress.c:ZSTD_matchState_dictMode
1104
1105
/* Defining this macro to non-zero tells zstd to run the overflow correction
1106
 * code much more frequently. This is very inefficient, and should only be
1107
 * used for tests and fuzzers.
1108
 */
1109
#ifndef ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY
1110
#  ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1111
18.2k
#    define ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY 1
1112
#  else
1113
#    define ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY 0
1114
#  endif
1115
#endif
1116
1117
/**
1118
 * ZSTD_window_canOverflowCorrect():
1119
 * Returns non-zero if the indices are large enough for overflow correction
1120
 * to work correctly without impacting compression ratio.
1121
 */
1122
MEM_STATIC U32 ZSTD_window_canOverflowCorrect(ZSTD_window_t const window,
1123
                                              U32 cycleLog,
1124
                                              U32 maxDist,
1125
                                              U32 loadedDictEnd,
1126
                                              void const* src)
1127
18.1k
{
1128
18.1k
    U32 const cycleSize = 1u << cycleLog;
1129
18.1k
    U32 const curr = (U32)((BYTE const*)src - window.base);
1130
18.1k
    U32 const minIndexToOverflowCorrect = cycleSize
1131
18.1k
                                        + MAX(maxDist, cycleSize)
1132
18.1k
                                        + ZSTD_WINDOW_START_INDEX;
1133
1134
    /* Adjust the min index to backoff the overflow correction frequency,
1135
     * so we don't waste too much CPU in overflow correction. If this
1136
     * computation overflows we don't really care, we just need to make
1137
     * sure it is at least minIndexToOverflowCorrect.
1138
     */
1139
18.1k
    U32 const adjustment = window.nbOverflowCorrections + 1;
1140
18.1k
    U32 const adjustedIndex = MAX(minIndexToOverflowCorrect * adjustment,
1141
18.1k
                                  minIndexToOverflowCorrect);
1142
18.1k
    U32 const indexLargeEnough = curr > adjustedIndex;
1143
1144
    /* Only overflow correct early if the dictionary is invalidated already,
1145
     * so we don't hurt compression ratio.
1146
     */
1147
18.1k
    U32 const dictionaryInvalidated = curr > maxDist + loadedDictEnd;
1148
1149
18.1k
    return indexLargeEnough && dictionaryInvalidated;
1150
18.1k
}
zstd_compress.c:ZSTD_window_canOverflowCorrect
Line
Count
Source
1127
18.1k
{
1128
18.1k
    U32 const cycleSize = 1u << cycleLog;
1129
18.1k
    U32 const curr = (U32)((BYTE const*)src - window.base);
1130
18.1k
    U32 const minIndexToOverflowCorrect = cycleSize
1131
18.1k
                                        + MAX(maxDist, cycleSize)
1132
18.1k
                                        + ZSTD_WINDOW_START_INDEX;
1133
1134
    /* Adjust the min index to backoff the overflow correction frequency,
1135
     * so we don't waste too much CPU in overflow correction. If this
1136
     * computation overflows we don't really care, we just need to make
1137
     * sure it is at least minIndexToOverflowCorrect.
1138
     */
1139
18.1k
    U32 const adjustment = window.nbOverflowCorrections + 1;
1140
18.1k
    U32 const adjustedIndex = MAX(minIndexToOverflowCorrect * adjustment,
1141
18.1k
                                  minIndexToOverflowCorrect);
1142
18.1k
    U32 const indexLargeEnough = curr > adjustedIndex;
1143
1144
    /* Only overflow correct early if the dictionary is invalidated already,
1145
     * so we don't hurt compression ratio.
1146
     */
1147
18.1k
    U32 const dictionaryInvalidated = curr > maxDist + loadedDictEnd;
1148
1149
18.1k
    return indexLargeEnough && dictionaryInvalidated;
1150
18.1k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_window_canOverflowCorrect
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_window_canOverflowCorrect
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_window_canOverflowCorrect
Unexecuted instantiation: zstd_double_fast.c:ZSTD_window_canOverflowCorrect
Unexecuted instantiation: zstd_fast.c:ZSTD_window_canOverflowCorrect
Unexecuted instantiation: zstd_lazy.c:ZSTD_window_canOverflowCorrect
Unexecuted instantiation: zstd_ldm.c:ZSTD_window_canOverflowCorrect
Unexecuted instantiation: zstd_opt.c:ZSTD_window_canOverflowCorrect
Unexecuted instantiation: zstdmt_compress.c:ZSTD_window_canOverflowCorrect
1151
1152
/**
1153
 * ZSTD_window_needOverflowCorrection():
1154
 * Returns non-zero if the indices are getting too large and need overflow
1155
 * protection.
1156
 */
1157
MEM_STATIC U32 ZSTD_window_needOverflowCorrection(ZSTD_window_t const window,
1158
                                                  U32 cycleLog,
1159
                                                  U32 maxDist,
1160
                                                  U32 loadedDictEnd,
1161
                                                  void const* src,
1162
                                                  void const* srcEnd)
1163
18.1k
{
1164
18.1k
    U32 const curr = (U32)((BYTE const*)srcEnd - window.base);
1165
18.1k
    if (ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY) {
1166
18.1k
        if (ZSTD_window_canOverflowCorrect(window, cycleLog, maxDist, loadedDictEnd, src)) {
1167
96
            return 1;
1168
96
        }
1169
18.1k
    }
1170
18.0k
    return curr > ZSTD_CURRENT_MAX;
1171
18.1k
}
zstd_compress.c:ZSTD_window_needOverflowCorrection
Line
Count
Source
1163
18.1k
{
1164
18.1k
    U32 const curr = (U32)((BYTE const*)srcEnd - window.base);
1165
18.1k
    if (ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY) {
1166
18.1k
        if (ZSTD_window_canOverflowCorrect(window, cycleLog, maxDist, loadedDictEnd, src)) {
1167
96
            return 1;
1168
96
        }
1169
18.1k
    }
1170
18.0k
    return curr > ZSTD_CURRENT_MAX;
1171
18.1k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_window_needOverflowCorrection
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_window_needOverflowCorrection
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_window_needOverflowCorrection
Unexecuted instantiation: zstd_double_fast.c:ZSTD_window_needOverflowCorrection
Unexecuted instantiation: zstd_fast.c:ZSTD_window_needOverflowCorrection
Unexecuted instantiation: zstd_lazy.c:ZSTD_window_needOverflowCorrection
Unexecuted instantiation: zstd_ldm.c:ZSTD_window_needOverflowCorrection
Unexecuted instantiation: zstd_opt.c:ZSTD_window_needOverflowCorrection
Unexecuted instantiation: zstdmt_compress.c:ZSTD_window_needOverflowCorrection
1172
1173
/**
1174
 * ZSTD_window_correctOverflow():
1175
 * Reduces the indices to protect from index overflow.
1176
 * Returns the correction made to the indices, which must be applied to every
1177
 * stored index.
1178
 *
1179
 * The least significant cycleLog bits of the indices must remain the same,
1180
 * which may be 0. Every index up to maxDist in the past must be valid.
1181
 */
1182
MEM_STATIC
1183
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
1184
U32 ZSTD_window_correctOverflow(ZSTD_window_t* window, U32 cycleLog,
1185
                                           U32 maxDist, void const* src)
1186
96
{
1187
    /* preemptive overflow correction:
1188
     * 1. correction is large enough:
1189
     *    lowLimit > (3<<29) ==> current > 3<<29 + 1<<windowLog
1190
     *    1<<windowLog <= newCurrent < 1<<chainLog + 1<<windowLog
1191
     *
1192
     *    current - newCurrent
1193
     *    > (3<<29 + 1<<windowLog) - (1<<windowLog + 1<<chainLog)
1194
     *    > (3<<29) - (1<<chainLog)
1195
     *    > (3<<29) - (1<<30)             (NOTE: chainLog <= 30)
1196
     *    > 1<<29
1197
     *
1198
     * 2. (ip+ZSTD_CHUNKSIZE_MAX - cctx->base) doesn't overflow:
1199
     *    After correction, current is less than (1<<chainLog + 1<<windowLog).
1200
     *    In 64-bit mode we are safe, because we have 64-bit ptrdiff_t.
1201
     *    In 32-bit mode we are safe, because (chainLog <= 29), so
1202
     *    ip+ZSTD_CHUNKSIZE_MAX - cctx->base < 1<<32.
1203
     * 3. (cctx->lowLimit + 1<<windowLog) < 1<<32:
1204
     *    windowLog <= 31 ==> 3<<29 + 1<<windowLog < 7<<29 < 1<<32.
1205
     */
1206
96
    U32 const cycleSize = 1u << cycleLog;
1207
96
    U32 const cycleMask = cycleSize - 1;
1208
96
    U32 const curr = (U32)((BYTE const*)src - window->base);
1209
96
    U32 const currentCycle = curr & cycleMask;
1210
    /* Ensure newCurrent - maxDist >= ZSTD_WINDOW_START_INDEX. */
1211
96
    U32 const currentCycleCorrection = currentCycle < ZSTD_WINDOW_START_INDEX
1212
96
                                     ? MAX(cycleSize, ZSTD_WINDOW_START_INDEX)
1213
96
                                     : 0;
1214
96
    U32 const newCurrent = currentCycle
1215
96
                         + currentCycleCorrection
1216
96
                         + MAX(maxDist, cycleSize);
1217
96
    U32 const correction = curr - newCurrent;
1218
    /* maxDist must be a power of two so that:
1219
     *   (newCurrent & cycleMask) == (curr & cycleMask)
1220
     * This is required to not corrupt the chains / binary tree.
1221
     */
1222
96
    assert((maxDist & (maxDist - 1)) == 0);
1223
96
    assert((curr & cycleMask) == (newCurrent & cycleMask));
1224
96
    assert(curr > newCurrent);
1225
96
    if (!ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY) {
1226
        /* Loose bound, should be around 1<<29 (see above) */
1227
0
        assert(correction > 1<<28);
1228
0
    }
1229
1230
96
    window->base += correction;
1231
96
    window->dictBase += correction;
1232
96
    if (window->lowLimit < correction + ZSTD_WINDOW_START_INDEX) {
1233
0
        window->lowLimit = ZSTD_WINDOW_START_INDEX;
1234
96
    } else {
1235
96
        window->lowLimit -= correction;
1236
96
    }
1237
96
    if (window->dictLimit < correction + ZSTD_WINDOW_START_INDEX) {
1238
0
        window->dictLimit = ZSTD_WINDOW_START_INDEX;
1239
96
    } else {
1240
96
        window->dictLimit -= correction;
1241
96
    }
1242
1243
    /* Ensure we can still reference the full window. */
1244
96
    assert(newCurrent >= maxDist);
1245
96
    assert(newCurrent - maxDist >= ZSTD_WINDOW_START_INDEX);
1246
    /* Ensure that lowLimit and dictLimit didn't underflow. */
1247
96
    assert(window->lowLimit <= newCurrent);
1248
96
    assert(window->dictLimit <= newCurrent);
1249
1250
96
    ++window->nbOverflowCorrections;
1251
1252
96
    DEBUGLOG(4, "Correction of 0x%x bytes to lowLimit=0x%x", correction,
1253
96
             window->lowLimit);
1254
96
    return correction;
1255
96
}
zstd_compress.c:ZSTD_window_correctOverflow
Line
Count
Source
1186
96
{
1187
    /* preemptive overflow correction:
1188
     * 1. correction is large enough:
1189
     *    lowLimit > (3<<29) ==> current > 3<<29 + 1<<windowLog
1190
     *    1<<windowLog <= newCurrent < 1<<chainLog + 1<<windowLog
1191
     *
1192
     *    current - newCurrent
1193
     *    > (3<<29 + 1<<windowLog) - (1<<windowLog + 1<<chainLog)
1194
     *    > (3<<29) - (1<<chainLog)
1195
     *    > (3<<29) - (1<<30)             (NOTE: chainLog <= 30)
1196
     *    > 1<<29
1197
     *
1198
     * 2. (ip+ZSTD_CHUNKSIZE_MAX - cctx->base) doesn't overflow:
1199
     *    After correction, current is less than (1<<chainLog + 1<<windowLog).
1200
     *    In 64-bit mode we are safe, because we have 64-bit ptrdiff_t.
1201
     *    In 32-bit mode we are safe, because (chainLog <= 29), so
1202
     *    ip+ZSTD_CHUNKSIZE_MAX - cctx->base < 1<<32.
1203
     * 3. (cctx->lowLimit + 1<<windowLog) < 1<<32:
1204
     *    windowLog <= 31 ==> 3<<29 + 1<<windowLog < 7<<29 < 1<<32.
1205
     */
1206
96
    U32 const cycleSize = 1u << cycleLog;
1207
96
    U32 const cycleMask = cycleSize - 1;
1208
96
    U32 const curr = (U32)((BYTE const*)src - window->base);
1209
96
    U32 const currentCycle = curr & cycleMask;
1210
    /* Ensure newCurrent - maxDist >= ZSTD_WINDOW_START_INDEX. */
1211
96
    U32 const currentCycleCorrection = currentCycle < ZSTD_WINDOW_START_INDEX
1212
96
                                     ? MAX(cycleSize, ZSTD_WINDOW_START_INDEX)
1213
96
                                     : 0;
1214
96
    U32 const newCurrent = currentCycle
1215
96
                         + currentCycleCorrection
1216
96
                         + MAX(maxDist, cycleSize);
1217
96
    U32 const correction = curr - newCurrent;
1218
    /* maxDist must be a power of two so that:
1219
     *   (newCurrent & cycleMask) == (curr & cycleMask)
1220
     * This is required to not corrupt the chains / binary tree.
1221
     */
1222
96
    assert((maxDist & (maxDist - 1)) == 0);
1223
96
    assert((curr & cycleMask) == (newCurrent & cycleMask));
1224
96
    assert(curr > newCurrent);
1225
96
    if (!ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY) {
1226
        /* Loose bound, should be around 1<<29 (see above) */
1227
0
        assert(correction > 1<<28);
1228
0
    }
1229
1230
96
    window->base += correction;
1231
96
    window->dictBase += correction;
1232
96
    if (window->lowLimit < correction + ZSTD_WINDOW_START_INDEX) {
1233
0
        window->lowLimit = ZSTD_WINDOW_START_INDEX;
1234
96
    } else {
1235
96
        window->lowLimit -= correction;
1236
96
    }
1237
96
    if (window->dictLimit < correction + ZSTD_WINDOW_START_INDEX) {
1238
0
        window->dictLimit = ZSTD_WINDOW_START_INDEX;
1239
96
    } else {
1240
96
        window->dictLimit -= correction;
1241
96
    }
1242
1243
    /* Ensure we can still reference the full window. */
1244
96
    assert(newCurrent >= maxDist);
1245
96
    assert(newCurrent - maxDist >= ZSTD_WINDOW_START_INDEX);
1246
    /* Ensure that lowLimit and dictLimit didn't underflow. */
1247
96
    assert(window->lowLimit <= newCurrent);
1248
96
    assert(window->dictLimit <= newCurrent);
1249
1250
96
    ++window->nbOverflowCorrections;
1251
1252
96
    DEBUGLOG(4, "Correction of 0x%x bytes to lowLimit=0x%x", correction,
1253
96
             window->lowLimit);
1254
96
    return correction;
1255
96
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_window_correctOverflow
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_window_correctOverflow
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_window_correctOverflow
Unexecuted instantiation: zstd_double_fast.c:ZSTD_window_correctOverflow
Unexecuted instantiation: zstd_fast.c:ZSTD_window_correctOverflow
Unexecuted instantiation: zstd_lazy.c:ZSTD_window_correctOverflow
Unexecuted instantiation: zstd_ldm.c:ZSTD_window_correctOverflow
Unexecuted instantiation: zstd_opt.c:ZSTD_window_correctOverflow
Unexecuted instantiation: zstdmt_compress.c:ZSTD_window_correctOverflow
1256
1257
/**
1258
 * ZSTD_window_enforceMaxDist():
1259
 * Updates lowLimit so that:
1260
 *    (srcEnd - base) - lowLimit == maxDist + loadedDictEnd
1261
 *
1262
 * It ensures index is valid as long as index >= lowLimit.
1263
 * This must be called before a block compression call.
1264
 *
1265
 * loadedDictEnd is only defined if a dictionary is in use for current compression.
1266
 * As the name implies, loadedDictEnd represents the index at end of dictionary.
1267
 * The value lies within context's referential, it can be directly compared to blockEndIdx.
1268
 *
1269
 * If loadedDictEndPtr is NULL, no dictionary is in use, and we use loadedDictEnd == 0.
1270
 * If loadedDictEndPtr is not NULL, we set it to zero after updating lowLimit.
1271
 * This is because dictionaries are allowed to be referenced fully
1272
 * as long as the last byte of the dictionary is in the window.
1273
 * Once input has progressed beyond window size, dictionary cannot be referenced anymore.
1274
 *
1275
 * In normal dict mode, the dictionary lies between lowLimit and dictLimit.
1276
 * In dictMatchState mode, lowLimit and dictLimit are the same,
1277
 * and the dictionary is below them.
1278
 * forceWindow and dictMatchState are therefore incompatible.
1279
 */
1280
MEM_STATIC void
1281
ZSTD_window_enforceMaxDist(ZSTD_window_t* window,
1282
                     const void* blockEnd,
1283
                           U32   maxDist,
1284
                           U32*  loadedDictEndPtr,
1285
                     const ZSTD_MatchState_t** dictMatchStatePtr)
1286
18.1k
{
1287
18.1k
    U32 const blockEndIdx = (U32)((BYTE const*)blockEnd - window->base);
1288
18.1k
    U32 const loadedDictEnd = (loadedDictEndPtr != NULL) ? *loadedDictEndPtr : 0;
1289
18.1k
    DEBUGLOG(5, "ZSTD_window_enforceMaxDist: blockEndIdx=%u, maxDist=%u, loadedDictEnd=%u",
1290
18.1k
                (unsigned)blockEndIdx, (unsigned)maxDist, (unsigned)loadedDictEnd);
1291
1292
    /* - When there is no dictionary : loadedDictEnd == 0.
1293
         In which case, the test (blockEndIdx > maxDist) is merely to avoid
1294
         overflowing next operation `newLowLimit = blockEndIdx - maxDist`.
1295
       - When there is a standard dictionary :
1296
         Index referential is copied from the dictionary,
1297
         which means it starts from 0.
1298
         In which case, loadedDictEnd == dictSize,
1299
         and it makes sense to compare `blockEndIdx > maxDist + dictSize`
1300
         since `blockEndIdx` also starts from zero.
1301
       - When there is an attached dictionary :
1302
         loadedDictEnd is expressed within the referential of the context,
1303
         so it can be directly compared against blockEndIdx.
1304
    */
1305
18.1k
    if (blockEndIdx > maxDist + loadedDictEnd) {
1306
1.70k
        U32 const newLowLimit = blockEndIdx - maxDist;
1307
1.70k
        if (window->lowLimit < newLowLimit) window->lowLimit = newLowLimit;
1308
1.70k
        if (window->dictLimit < window->lowLimit) {
1309
0
            DEBUGLOG(5, "Update dictLimit to match lowLimit, from %u to %u",
1310
0
                        (unsigned)window->dictLimit, (unsigned)window->lowLimit);
1311
0
            window->dictLimit = window->lowLimit;
1312
0
        }
1313
        /* On reaching window size, dictionaries are invalidated */
1314
1.70k
        if (loadedDictEndPtr) *loadedDictEndPtr = 0;
1315
1.70k
        if (dictMatchStatePtr) *dictMatchStatePtr = NULL;
1316
1.70k
    }
1317
18.1k
}
zstd_compress.c:ZSTD_window_enforceMaxDist
Line
Count
Source
1286
18.1k
{
1287
18.1k
    U32 const blockEndIdx = (U32)((BYTE const*)blockEnd - window->base);
1288
18.1k
    U32 const loadedDictEnd = (loadedDictEndPtr != NULL) ? *loadedDictEndPtr : 0;
1289
18.1k
    DEBUGLOG(5, "ZSTD_window_enforceMaxDist: blockEndIdx=%u, maxDist=%u, loadedDictEnd=%u",
1290
18.1k
                (unsigned)blockEndIdx, (unsigned)maxDist, (unsigned)loadedDictEnd);
1291
1292
    /* - When there is no dictionary : loadedDictEnd == 0.
1293
         In which case, the test (blockEndIdx > maxDist) is merely to avoid
1294
         overflowing next operation `newLowLimit = blockEndIdx - maxDist`.
1295
       - When there is a standard dictionary :
1296
         Index referential is copied from the dictionary,
1297
         which means it starts from 0.
1298
         In which case, loadedDictEnd == dictSize,
1299
         and it makes sense to compare `blockEndIdx > maxDist + dictSize`
1300
         since `blockEndIdx` also starts from zero.
1301
       - When there is an attached dictionary :
1302
         loadedDictEnd is expressed within the referential of the context,
1303
         so it can be directly compared against blockEndIdx.
1304
    */
1305
18.1k
    if (blockEndIdx > maxDist + loadedDictEnd) {
1306
1.70k
        U32 const newLowLimit = blockEndIdx - maxDist;
1307
1.70k
        if (window->lowLimit < newLowLimit) window->lowLimit = newLowLimit;
1308
1.70k
        if (window->dictLimit < window->lowLimit) {
1309
0
            DEBUGLOG(5, "Update dictLimit to match lowLimit, from %u to %u",
1310
0
                        (unsigned)window->dictLimit, (unsigned)window->lowLimit);
1311
0
            window->dictLimit = window->lowLimit;
1312
0
        }
1313
        /* On reaching window size, dictionaries are invalidated */
1314
1.70k
        if (loadedDictEndPtr) *loadedDictEndPtr = 0;
1315
1.70k
        if (dictMatchStatePtr) *dictMatchStatePtr = NULL;
1316
1.70k
    }
1317
18.1k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_window_enforceMaxDist
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_window_enforceMaxDist
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_window_enforceMaxDist
Unexecuted instantiation: zstd_double_fast.c:ZSTD_window_enforceMaxDist
Unexecuted instantiation: zstd_fast.c:ZSTD_window_enforceMaxDist
Unexecuted instantiation: zstd_lazy.c:ZSTD_window_enforceMaxDist
Unexecuted instantiation: zstd_ldm.c:ZSTD_window_enforceMaxDist
Unexecuted instantiation: zstd_opt.c:ZSTD_window_enforceMaxDist
Unexecuted instantiation: zstdmt_compress.c:ZSTD_window_enforceMaxDist
1318
1319
/* Similar to ZSTD_window_enforceMaxDist(),
1320
 * but only invalidates dictionary
1321
 * when input progresses beyond window size.
1322
 * assumption : loadedDictEndPtr and dictMatchStatePtr are valid (non NULL)
1323
 *              loadedDictEnd uses same referential as window->base
1324
 *              maxDist is the window size */
1325
MEM_STATIC void
1326
ZSTD_checkDictValidity(const ZSTD_window_t* window,
1327
                       const void* blockEnd,
1328
                             U32   maxDist,
1329
                             U32*  loadedDictEndPtr,
1330
                       const ZSTD_MatchState_t** dictMatchStatePtr)
1331
18.1k
{
1332
18.1k
    assert(loadedDictEndPtr != NULL);
1333
18.1k
    assert(dictMatchStatePtr != NULL);
1334
18.1k
    {   U32 const blockEndIdx = (U32)((BYTE const*)blockEnd - window->base);
1335
18.1k
        U32 const loadedDictEnd = *loadedDictEndPtr;
1336
18.1k
        DEBUGLOG(5, "ZSTD_checkDictValidity: blockEndIdx=%u, maxDist=%u, loadedDictEnd=%u",
1337
18.1k
                    (unsigned)blockEndIdx, (unsigned)maxDist, (unsigned)loadedDictEnd);
1338
18.1k
        assert(blockEndIdx >= loadedDictEnd);
1339
1340
18.1k
        if (blockEndIdx > loadedDictEnd + maxDist || loadedDictEnd != window->dictLimit) {
1341
            /* On reaching window size, dictionaries are invalidated.
1342
             * For simplification, if window size is reached anywhere within next block,
1343
             * the dictionary is invalidated for the full block.
1344
             *
1345
             * We also have to invalidate the dictionary if ZSTD_window_update() has detected
1346
             * non-contiguous segments, which means that loadedDictEnd != window->dictLimit.
1347
             * loadedDictEnd may be 0, if forceWindow is true, but in that case we never use
1348
             * dictMatchState, so setting it to NULL is not a problem.
1349
             */
1350
18.1k
            DEBUGLOG(6, "invalidating dictionary for current block (distance > windowSize)");
1351
18.1k
            *loadedDictEndPtr = 0;
1352
18.1k
            *dictMatchStatePtr = NULL;
1353
18.1k
        } else {
1354
0
            if (*loadedDictEndPtr != 0) {
1355
0
                DEBUGLOG(6, "dictionary considered valid for current block");
1356
0
    }   }   }
1357
18.1k
}
zstd_compress.c:ZSTD_checkDictValidity
Line
Count
Source
1331
18.1k
{
1332
18.1k
    assert(loadedDictEndPtr != NULL);
1333
18.1k
    assert(dictMatchStatePtr != NULL);
1334
18.1k
    {   U32 const blockEndIdx = (U32)((BYTE const*)blockEnd - window->base);
1335
18.1k
        U32 const loadedDictEnd = *loadedDictEndPtr;
1336
18.1k
        DEBUGLOG(5, "ZSTD_checkDictValidity: blockEndIdx=%u, maxDist=%u, loadedDictEnd=%u",
1337
18.1k
                    (unsigned)blockEndIdx, (unsigned)maxDist, (unsigned)loadedDictEnd);
1338
18.1k
        assert(blockEndIdx >= loadedDictEnd);
1339
1340
18.1k
        if (blockEndIdx > loadedDictEnd + maxDist || loadedDictEnd != window->dictLimit) {
1341
            /* On reaching window size, dictionaries are invalidated.
1342
             * For simplification, if window size is reached anywhere within next block,
1343
             * the dictionary is invalidated for the full block.
1344
             *
1345
             * We also have to invalidate the dictionary if ZSTD_window_update() has detected
1346
             * non-contiguous segments, which means that loadedDictEnd != window->dictLimit.
1347
             * loadedDictEnd may be 0, if forceWindow is true, but in that case we never use
1348
             * dictMatchState, so setting it to NULL is not a problem.
1349
             */
1350
18.1k
            DEBUGLOG(6, "invalidating dictionary for current block (distance > windowSize)");
1351
18.1k
            *loadedDictEndPtr = 0;
1352
18.1k
            *dictMatchStatePtr = NULL;
1353
18.1k
        } else {
1354
0
            if (*loadedDictEndPtr != 0) {
1355
0
                DEBUGLOG(6, "dictionary considered valid for current block");
1356
0
    }   }   }
1357
18.1k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_checkDictValidity
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_checkDictValidity
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_checkDictValidity
Unexecuted instantiation: zstd_double_fast.c:ZSTD_checkDictValidity
Unexecuted instantiation: zstd_fast.c:ZSTD_checkDictValidity
Unexecuted instantiation: zstd_lazy.c:ZSTD_checkDictValidity
Unexecuted instantiation: zstd_ldm.c:ZSTD_checkDictValidity
Unexecuted instantiation: zstd_opt.c:ZSTD_checkDictValidity
Unexecuted instantiation: zstdmt_compress.c:ZSTD_checkDictValidity
1358
1359
3.03k
MEM_STATIC void ZSTD_window_init(ZSTD_window_t* window) {
1360
3.03k
    ZSTD_memset(window, 0, sizeof(*window));
1361
3.03k
    window->base = (BYTE const*)" ";
1362
3.03k
    window->dictBase = (BYTE const*)" ";
1363
3.03k
    ZSTD_STATIC_ASSERT(ZSTD_DUBT_UNSORTED_MARK < ZSTD_WINDOW_START_INDEX); /* Start above ZSTD_DUBT_UNSORTED_MARK */
1364
3.03k
    window->dictLimit = ZSTD_WINDOW_START_INDEX;    /* start from >0, so that 1st position is valid */
1365
3.03k
    window->lowLimit = ZSTD_WINDOW_START_INDEX;     /* it ensures first and later CCtx usages compress the same */
1366
3.03k
    window->nextSrc = window->base + ZSTD_WINDOW_START_INDEX;   /* see issue #1241 */
1367
3.03k
    window->nbOverflowCorrections = 0;
1368
3.03k
}
zstd_compress.c:ZSTD_window_init
Line
Count
Source
1359
3.03k
MEM_STATIC void ZSTD_window_init(ZSTD_window_t* window) {
1360
3.03k
    ZSTD_memset(window, 0, sizeof(*window));
1361
3.03k
    window->base = (BYTE const*)" ";
1362
3.03k
    window->dictBase = (BYTE const*)" ";
1363
3.03k
    ZSTD_STATIC_ASSERT(ZSTD_DUBT_UNSORTED_MARK < ZSTD_WINDOW_START_INDEX); /* Start above ZSTD_DUBT_UNSORTED_MARK */
1364
3.03k
    window->dictLimit = ZSTD_WINDOW_START_INDEX;    /* start from >0, so that 1st position is valid */
1365
3.03k
    window->lowLimit = ZSTD_WINDOW_START_INDEX;     /* it ensures first and later CCtx usages compress the same */
1366
3.03k
    window->nextSrc = window->base + ZSTD_WINDOW_START_INDEX;   /* see issue #1241 */
1367
3.03k
    window->nbOverflowCorrections = 0;
1368
3.03k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_window_init
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_window_init
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_window_init
Unexecuted instantiation: zstd_double_fast.c:ZSTD_window_init
Unexecuted instantiation: zstd_fast.c:ZSTD_window_init
Unexecuted instantiation: zstd_lazy.c:ZSTD_window_init
Unexecuted instantiation: zstd_ldm.c:ZSTD_window_init
Unexecuted instantiation: zstd_opt.c:ZSTD_window_init
Unexecuted instantiation: zstdmt_compress.c:ZSTD_window_init
1369
1370
/**
1371
 * ZSTD_window_update():
1372
 * Updates the window by appending [src, src + srcSize) to the window.
1373
 * If it is not contiguous, the current prefix becomes the extDict, and we
1374
 * forget about the extDict. Handles overlap of the prefix and extDict.
1375
 * Returns non-zero if the segment is contiguous.
1376
 */
1377
MEM_STATIC
1378
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
1379
U32 ZSTD_window_update(ZSTD_window_t* window,
1380
                 const void* src, size_t srcSize,
1381
                       int forceNonContiguous)
1382
16.9k
{
1383
16.9k
    BYTE const* const ip = (BYTE const*)src;
1384
16.9k
    U32 contiguous = 1;
1385
16.9k
    DEBUGLOG(5, "ZSTD_window_update");
1386
16.9k
    if (srcSize == 0)
1387
0
        return contiguous;
1388
16.9k
    assert(window->base != NULL);
1389
16.9k
    assert(window->dictBase != NULL);
1390
    /* Check if blocks follow each other */
1391
16.9k
    if (src != window->nextSrc || forceNonContiguous) {
1392
        /* not contiguous */
1393
5.84k
        size_t const distanceFromBase = (size_t)(window->nextSrc - window->base);
1394
5.84k
        DEBUGLOG(5, "Non contiguous blocks, new segment starts at %u", window->dictLimit);
1395
5.84k
        window->lowLimit = window->dictLimit;
1396
5.84k
        assert(distanceFromBase == (size_t)(U32)distanceFromBase);  /* should never overflow */
1397
5.84k
        window->dictLimit = (U32)distanceFromBase;
1398
5.84k
        window->dictBase = window->base;
1399
5.84k
        window->base = ip - distanceFromBase;
1400
        /* ms->nextToUpdate = window->dictLimit; */
1401
5.84k
        if (window->dictLimit - window->lowLimit < HASH_READ_SIZE) window->lowLimit = window->dictLimit;   /* too small extDict */
1402
5.84k
        contiguous = 0;
1403
5.84k
    }
1404
16.9k
    window->nextSrc = ip + srcSize;
1405
    /* if input and dictionary overlap : reduce dictionary (area presumed modified by input) */
1406
16.9k
    if ( (ip+srcSize > window->dictBase + window->lowLimit)
1407
16.9k
       & (ip < window->dictBase + window->dictLimit)) {
1408
214
        size_t const highInputIdx = (size_t)((ip + srcSize) - window->dictBase);
1409
214
        U32 const lowLimitMax = (highInputIdx > (size_t)window->dictLimit) ? window->dictLimit : (U32)highInputIdx;
1410
214
        assert(highInputIdx < UINT_MAX);
1411
214
        window->lowLimit = lowLimitMax;
1412
214
        DEBUGLOG(5, "Overlapping extDict and input : new lowLimit = %u", window->lowLimit);
1413
214
    }
1414
16.9k
    return contiguous;
1415
16.9k
}
zstd_compress.c:ZSTD_window_update
Line
Count
Source
1382
16.9k
{
1383
16.9k
    BYTE const* const ip = (BYTE const*)src;
1384
16.9k
    U32 contiguous = 1;
1385
16.9k
    DEBUGLOG(5, "ZSTD_window_update");
1386
16.9k
    if (srcSize == 0)
1387
0
        return contiguous;
1388
16.9k
    assert(window->base != NULL);
1389
16.9k
    assert(window->dictBase != NULL);
1390
    /* Check if blocks follow each other */
1391
16.9k
    if (src != window->nextSrc || forceNonContiguous) {
1392
        /* not contiguous */
1393
5.84k
        size_t const distanceFromBase = (size_t)(window->nextSrc - window->base);
1394
5.84k
        DEBUGLOG(5, "Non contiguous blocks, new segment starts at %u", window->dictLimit);
1395
5.84k
        window->lowLimit = window->dictLimit;
1396
5.84k
        assert(distanceFromBase == (size_t)(U32)distanceFromBase);  /* should never overflow */
1397
5.84k
        window->dictLimit = (U32)distanceFromBase;
1398
5.84k
        window->dictBase = window->base;
1399
5.84k
        window->base = ip - distanceFromBase;
1400
        /* ms->nextToUpdate = window->dictLimit; */
1401
5.84k
        if (window->dictLimit - window->lowLimit < HASH_READ_SIZE) window->lowLimit = window->dictLimit;   /* too small extDict */
1402
5.84k
        contiguous = 0;
1403
5.84k
    }
1404
16.9k
    window->nextSrc = ip + srcSize;
1405
    /* if input and dictionary overlap : reduce dictionary (area presumed modified by input) */
1406
16.9k
    if ( (ip+srcSize > window->dictBase + window->lowLimit)
1407
16.9k
       & (ip < window->dictBase + window->dictLimit)) {
1408
214
        size_t const highInputIdx = (size_t)((ip + srcSize) - window->dictBase);
1409
214
        U32 const lowLimitMax = (highInputIdx > (size_t)window->dictLimit) ? window->dictLimit : (U32)highInputIdx;
1410
214
        assert(highInputIdx < UINT_MAX);
1411
214
        window->lowLimit = lowLimitMax;
1412
214
        DEBUGLOG(5, "Overlapping extDict and input : new lowLimit = %u", window->lowLimit);
1413
214
    }
1414
16.9k
    return contiguous;
1415
16.9k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_window_update
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_window_update
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_window_update
Unexecuted instantiation: zstd_double_fast.c:ZSTD_window_update
Unexecuted instantiation: zstd_fast.c:ZSTD_window_update
Unexecuted instantiation: zstd_lazy.c:ZSTD_window_update
Unexecuted instantiation: zstd_ldm.c:ZSTD_window_update
Unexecuted instantiation: zstd_opt.c:ZSTD_window_update
Unexecuted instantiation: zstdmt_compress.c:ZSTD_window_update
1416
1417
/**
1418
 * Returns the lowest allowed match index. It may either be in the ext-dict or the prefix.
1419
 */
1420
MEM_STATIC U32 ZSTD_getLowestMatchIndex(const ZSTD_MatchState_t* ms, U32 curr, unsigned windowLog)
1421
0
{
1422
0
    U32 const maxDistance = 1U << windowLog;
1423
0
    U32 const lowestValid = ms->window.lowLimit;
1424
0
    U32 const withinWindow = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid;
1425
0
    U32 const isDictionary = (ms->loadedDictEnd != 0);
1426
    /* When using a dictionary the entire dictionary is valid if a single byte of the dictionary
1427
     * is within the window. We invalidate the dictionary (and set loadedDictEnd to 0) when it isn't
1428
     * valid for the entire block. So this check is sufficient to find the lowest valid match index.
1429
     */
1430
0
    U32 const matchLowest = isDictionary ? lowestValid : withinWindow;
1431
0
    return matchLowest;
1432
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_getLowestMatchIndex
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_getLowestMatchIndex
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_getLowestMatchIndex
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_getLowestMatchIndex
Unexecuted instantiation: zstd_double_fast.c:ZSTD_getLowestMatchIndex
Unexecuted instantiation: zstd_fast.c:ZSTD_getLowestMatchIndex
Unexecuted instantiation: zstd_lazy.c:ZSTD_getLowestMatchIndex
Unexecuted instantiation: zstd_ldm.c:ZSTD_getLowestMatchIndex
Unexecuted instantiation: zstd_opt.c:ZSTD_getLowestMatchIndex
Unexecuted instantiation: zstdmt_compress.c:ZSTD_getLowestMatchIndex
1433
1434
/**
1435
 * Returns the lowest allowed match index in the prefix.
1436
 */
1437
MEM_STATIC U32 ZSTD_getLowestPrefixIndex(const ZSTD_MatchState_t* ms, U32 curr, unsigned windowLog)
1438
18.1k
{
1439
18.1k
    U32    const maxDistance = 1U << windowLog;
1440
18.1k
    U32    const lowestValid = ms->window.dictLimit;
1441
18.1k
    U32    const withinWindow = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid;
1442
18.1k
    U32    const isDictionary = (ms->loadedDictEnd != 0);
1443
    /* When computing the lowest prefix index we need to take the dictionary into account to handle
1444
     * the edge case where the dictionary and the source are contiguous in memory.
1445
     */
1446
18.1k
    U32    const matchLowest = isDictionary ? lowestValid : withinWindow;
1447
18.1k
    return matchLowest;
1448
18.1k
}
Unexecuted instantiation: zstd_compress.c:ZSTD_getLowestPrefixIndex
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_getLowestPrefixIndex
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_getLowestPrefixIndex
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_getLowestPrefixIndex
Unexecuted instantiation: zstd_double_fast.c:ZSTD_getLowestPrefixIndex
Unexecuted instantiation: zstd_fast.c:ZSTD_getLowestPrefixIndex
zstd_lazy.c:ZSTD_getLowestPrefixIndex
Line
Count
Source
1438
18.1k
{
1439
18.1k
    U32    const maxDistance = 1U << windowLog;
1440
18.1k
    U32    const lowestValid = ms->window.dictLimit;
1441
18.1k
    U32    const withinWindow = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid;
1442
18.1k
    U32    const isDictionary = (ms->loadedDictEnd != 0);
1443
    /* When computing the lowest prefix index we need to take the dictionary into account to handle
1444
     * the edge case where the dictionary and the source are contiguous in memory.
1445
     */
1446
18.1k
    U32    const matchLowest = isDictionary ? lowestValid : withinWindow;
1447
18.1k
    return matchLowest;
1448
18.1k
}
Unexecuted instantiation: zstd_ldm.c:ZSTD_getLowestPrefixIndex
Unexecuted instantiation: zstd_opt.c:ZSTD_getLowestPrefixIndex
Unexecuted instantiation: zstdmt_compress.c:ZSTD_getLowestPrefixIndex
1449
1450
/* index_safety_check:
1451
 * intentional underflow : ensure repIndex isn't overlapping dict + prefix
1452
 * @return 1 if values are not overlapping,
1453
 * 0 otherwise */
1454
0
MEM_STATIC int ZSTD_index_overlap_check(const U32 prefixLowestIndex, const U32 repIndex) {
1455
0
    return ((U32)((prefixLowestIndex-1)  - repIndex) >= 3);
1456
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_index_overlap_check
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_index_overlap_check
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_index_overlap_check
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_index_overlap_check
Unexecuted instantiation: zstd_double_fast.c:ZSTD_index_overlap_check
Unexecuted instantiation: zstd_fast.c:ZSTD_index_overlap_check
Unexecuted instantiation: zstd_lazy.c:ZSTD_index_overlap_check
Unexecuted instantiation: zstd_ldm.c:ZSTD_index_overlap_check
Unexecuted instantiation: zstd_opt.c:ZSTD_index_overlap_check
Unexecuted instantiation: zstdmt_compress.c:ZSTD_index_overlap_check
1457
1458
1459
/* debug functions */
1460
#if (DEBUGLEVEL>=2)
1461
1462
MEM_STATIC double ZSTD_fWeight(U32 rawStat)
1463
{
1464
    U32 const fp_accuracy = 8;
1465
    U32 const fp_multiplier = (1 << fp_accuracy);
1466
    U32 const newStat = rawStat + 1;
1467
    U32 const hb = ZSTD_highbit32(newStat);
1468
    U32 const BWeight = hb * fp_multiplier;
1469
    U32 const FWeight = (newStat << fp_accuracy) >> hb;
1470
    U32 const weight = BWeight + FWeight;
1471
    assert(hb + fp_accuracy < 31);
1472
    return (double)weight / fp_multiplier;
1473
}
1474
1475
/* display a table content,
1476
 * listing each element, its frequency, and its predicted bit cost */
1477
MEM_STATIC void ZSTD_debugTable(const U32* table, U32 max)
1478
{
1479
    unsigned u, sum;
1480
    for (u=0, sum=0; u<=max; u++) sum += table[u];
1481
    DEBUGLOG(2, "total nb elts: %u", sum);
1482
    for (u=0; u<=max; u++) {
1483
        DEBUGLOG(2, "%2u: %5u  (%.2f)",
1484
                u, table[u], ZSTD_fWeight(sum) - ZSTD_fWeight(table[u]) );
1485
    }
1486
}
1487
1488
#endif
1489
1490
/* Short Cache */
1491
1492
/* Normally, zstd matchfinders follow this flow:
1493
 *     1. Compute hash at ip
1494
 *     2. Load index from hashTable[hash]
1495
 *     3. Check if *ip == *(base + index)
1496
 * In dictionary compression, loading *(base + index) is often an L2 or even L3 miss.
1497
 *
1498
 * Short cache is an optimization which allows us to avoid step 3 most of the time
1499
 * when the data doesn't actually match. With short cache, the flow becomes:
1500
 *     1. Compute (hash, currentTag) at ip. currentTag is an 8-bit independent hash at ip.
1501
 *     2. Load (index, matchTag) from hashTable[hash]. See ZSTD_writeTaggedIndex to understand how this works.
1502
 *     3. Only if currentTag == matchTag, check *ip == *(base + index). Otherwise, continue.
1503
 *
1504
 * Currently, short cache is only implemented in CDict hashtables. Thus, its use is limited to
1505
 * dictMatchState matchfinders.
1506
 */
1507
0
#define ZSTD_SHORT_CACHE_TAG_BITS 8
1508
0
#define ZSTD_SHORT_CACHE_TAG_MASK ((1u << ZSTD_SHORT_CACHE_TAG_BITS) - 1)
1509
1510
/* Helper function for ZSTD_fillHashTable and ZSTD_fillDoubleHashTable.
1511
 * Unpacks hashAndTag into (hash, tag), then packs (index, tag) into hashTable[hash]. */
1512
0
MEM_STATIC void ZSTD_writeTaggedIndex(U32* const hashTable, size_t hashAndTag, U32 index) {
1513
0
    size_t const hash = hashAndTag >> ZSTD_SHORT_CACHE_TAG_BITS;
1514
0
    U32 const tag = (U32)(hashAndTag & ZSTD_SHORT_CACHE_TAG_MASK);
1515
0
    assert(index >> (32 - ZSTD_SHORT_CACHE_TAG_BITS) == 0);
1516
0
    hashTable[hash] = (index << ZSTD_SHORT_CACHE_TAG_BITS) | tag;
1517
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_writeTaggedIndex
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_writeTaggedIndex
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_writeTaggedIndex
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_writeTaggedIndex
Unexecuted instantiation: zstd_double_fast.c:ZSTD_writeTaggedIndex
Unexecuted instantiation: zstd_fast.c:ZSTD_writeTaggedIndex
Unexecuted instantiation: zstd_lazy.c:ZSTD_writeTaggedIndex
Unexecuted instantiation: zstd_ldm.c:ZSTD_writeTaggedIndex
Unexecuted instantiation: zstd_opt.c:ZSTD_writeTaggedIndex
Unexecuted instantiation: zstdmt_compress.c:ZSTD_writeTaggedIndex
1518
1519
/* Helper function for short cache matchfinders.
1520
 * Unpacks tag1 and tag2 from lower bits of packedTag1 and packedTag2, then checks if the tags match. */
1521
0
MEM_STATIC int ZSTD_comparePackedTags(size_t packedTag1, size_t packedTag2) {
1522
0
    U32 const tag1 = packedTag1 & ZSTD_SHORT_CACHE_TAG_MASK;
1523
0
    U32 const tag2 = packedTag2 & ZSTD_SHORT_CACHE_TAG_MASK;
1524
0
    return tag1 == tag2;
1525
0
}
Unexecuted instantiation: zstd_compress.c:ZSTD_comparePackedTags
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_comparePackedTags
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_comparePackedTags
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_comparePackedTags
Unexecuted instantiation: zstd_double_fast.c:ZSTD_comparePackedTags
Unexecuted instantiation: zstd_fast.c:ZSTD_comparePackedTags
Unexecuted instantiation: zstd_lazy.c:ZSTD_comparePackedTags
Unexecuted instantiation: zstd_ldm.c:ZSTD_comparePackedTags
Unexecuted instantiation: zstd_opt.c:ZSTD_comparePackedTags
Unexecuted instantiation: zstdmt_compress.c:ZSTD_comparePackedTags
1526
1527
/* ===============================================================
1528
 * Shared internal declarations
1529
 * These prototypes may be called from sources not in lib/compress
1530
 * =============================================================== */
1531
1532
/* ZSTD_loadCEntropy() :
1533
 * dict : must point at beginning of a valid zstd dictionary.
1534
 * return : size of dictionary header (size of magic number + dict ID + entropy tables)
1535
 * assumptions : magic number supposed already checked
1536
 *               and dictSize >= 8 */
1537
size_t ZSTD_loadCEntropy(ZSTD_compressedBlockState_t* bs, void* workspace,
1538
                         const void* const dict, size_t dictSize);
1539
1540
void ZSTD_reset_compressedBlockState(ZSTD_compressedBlockState_t* bs);
1541
1542
typedef struct {
1543
    U32 idx;            /* Index in array of ZSTD_Sequence */
1544
    U32 posInSequence;  /* Position within sequence at idx */
1545
    size_t posInSrc;    /* Number of bytes given by sequences provided so far */
1546
} ZSTD_SequencePosition;
1547
1548
/* for benchmark */
1549
size_t ZSTD_convertBlockSequences(ZSTD_CCtx* cctx,
1550
                        const ZSTD_Sequence* const inSeqs, size_t nbSequences,
1551
                        int repcodeResolution);
1552
1553
typedef struct {
1554
    size_t nbSequences;
1555
    size_t blockSize;
1556
    size_t litSize;
1557
} BlockSummary;
1558
1559
BlockSummary ZSTD_get1BlockSummary(const ZSTD_Sequence* seqs, size_t nbSeqs);
1560
1561
/* ==============================================================
1562
 * Private declarations
1563
 * These prototypes shall only be called from within lib/compress
1564
 * ============================================================== */
1565
1566
/* ZSTD_getCParamsFromCCtxParams() :
1567
 * cParams are built depending on compressionLevel, src size hints,
1568
 * LDM and manually set compression parameters.
1569
 * Note: srcSizeHint == 0 means 0!
1570
 */
1571
ZSTD_compressionParameters ZSTD_getCParamsFromCCtxParams(
1572
        const ZSTD_CCtx_params* CCtxParams, U64 srcSizeHint, size_t dictSize, ZSTD_CParamMode_e mode);
1573
1574
/*! ZSTD_initCStream_internal() :
1575
 *  Private use only. Init streaming operation.
1576
 *  expects params to be valid.
1577
 *  must receive dict, or cdict, or none, but not both.
1578
 *  @return : 0, or an error code */
1579
size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs,
1580
                     const void* dict, size_t dictSize,
1581
                     const ZSTD_CDict* cdict,
1582
                     const ZSTD_CCtx_params* params, unsigned long long pledgedSrcSize);
1583
1584
void ZSTD_resetSeqStore(SeqStore_t* ssPtr);
1585
1586
/*! ZSTD_getCParamsFromCDict() :
1587
 *  as the name implies */
1588
ZSTD_compressionParameters ZSTD_getCParamsFromCDict(const ZSTD_CDict* cdict);
1589
1590
/* ZSTD_compressBegin_advanced_internal() :
1591
 * Private use only. To be called from zstdmt_compress.c. */
1592
size_t ZSTD_compressBegin_advanced_internal(ZSTD_CCtx* cctx,
1593
                                    const void* dict, size_t dictSize,
1594
                                    ZSTD_dictContentType_e dictContentType,
1595
                                    ZSTD_dictTableLoadMethod_e dtlm,
1596
                                    const ZSTD_CDict* cdict,
1597
                                    const ZSTD_CCtx_params* params,
1598
                                    unsigned long long pledgedSrcSize);
1599
1600
/* ZSTD_compress_advanced_internal() :
1601
 * Private use only. To be called from zstdmt_compress.c. */
1602
size_t ZSTD_compress_advanced_internal(ZSTD_CCtx* cctx,
1603
                                       void* dst, size_t dstCapacity,
1604
                                 const void* src, size_t srcSize,
1605
                                 const void* dict,size_t dictSize,
1606
                                 const ZSTD_CCtx_params* params);
1607
1608
1609
/* ZSTD_writeLastEmptyBlock() :
1610
 * output an empty Block with end-of-frame mark to complete a frame
1611
 * @return : size of data written into `dst` (== ZSTD_blockHeaderSize (defined in zstd_internal.h))
1612
 *           or an error code if `dstCapacity` is too small (<ZSTD_blockHeaderSize)
1613
 */
1614
size_t ZSTD_writeLastEmptyBlock(void* dst, size_t dstCapacity);
1615
1616
1617
/* ZSTD_referenceExternalSequences() :
1618
 * Must be called before starting a compression operation.
1619
 * seqs must parse a prefix of the source.
1620
 * This cannot be used when long range matching is enabled.
1621
 * Zstd will use these sequences, and pass the literals to a secondary block
1622
 * compressor.
1623
 * NOTE: seqs are not verified! Invalid sequences can cause out-of-bounds memory
1624
 * access and data corruption.
1625
 */
1626
void ZSTD_referenceExternalSequences(ZSTD_CCtx* cctx, rawSeq* seq, size_t nbSeq);
1627
1628
/** ZSTD_cycleLog() :
1629
 *  condition for correct operation : hashLog > 1 */
1630
U32 ZSTD_cycleLog(U32 hashLog, ZSTD_strategy strat);
1631
1632
/** ZSTD_CCtx_trace() :
1633
 *  Trace the end of a compression call.
1634
 */
1635
void ZSTD_CCtx_trace(ZSTD_CCtx* cctx, size_t extraCSize);
1636
1637
/* Returns 1 if an external sequence producer is registered, otherwise returns 0. */
1638
41.4k
MEM_STATIC int ZSTD_hasExtSeqProd(const ZSTD_CCtx_params* params) {
1639
    return params->extSeqProdFunc != NULL;
1640
41.4k
}
zstd_compress.c:ZSTD_hasExtSeqProd
Line
Count
Source
1638
41.4k
MEM_STATIC int ZSTD_hasExtSeqProd(const ZSTD_CCtx_params* params) {
1639
    return params->extSeqProdFunc != NULL;
1640
41.4k
}
Unexecuted instantiation: zstd_compress_literals.c:ZSTD_hasExtSeqProd
Unexecuted instantiation: zstd_compress_sequences.c:ZSTD_hasExtSeqProd
Unexecuted instantiation: zstd_compress_superblock.c:ZSTD_hasExtSeqProd
Unexecuted instantiation: zstd_double_fast.c:ZSTD_hasExtSeqProd
Unexecuted instantiation: zstd_fast.c:ZSTD_hasExtSeqProd
Unexecuted instantiation: zstd_lazy.c:ZSTD_hasExtSeqProd
Unexecuted instantiation: zstd_ldm.c:ZSTD_hasExtSeqProd
Unexecuted instantiation: zstd_opt.c:ZSTD_hasExtSeqProd
Unexecuted instantiation: zstdmt_compress.c:ZSTD_hasExtSeqProd
1641
1642
/* ===============================================================
1643
 * Deprecated definitions that are still used internally to avoid
1644
 * deprecation warnings. These functions are exactly equivalent to
1645
 * their public variants, but avoid the deprecation warnings.
1646
 * =============================================================== */
1647
1648
size_t ZSTD_compressBegin_usingCDict_deprecated(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict);
1649
1650
size_t ZSTD_compressContinue_public(ZSTD_CCtx* cctx,
1651
                                    void* dst, size_t dstCapacity,
1652
                              const void* src, size_t srcSize);
1653
1654
size_t ZSTD_compressEnd_public(ZSTD_CCtx* cctx,
1655
                               void* dst, size_t dstCapacity,
1656
                         const void* src, size_t srcSize);
1657
1658
size_t ZSTD_compressBlock_deprecated(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1659
1660
1661
#endif /* ZSTD_COMPRESS_H */