xref: /freebsd/sys/contrib/openzfs/module/zstd/lib/compress/zstd_compress_internal.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only
2 /*
3  * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
4  * All rights reserved.
5  *
6  * This source code is licensed under both the BSD-style license (found in the
7  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
8  * in the COPYING file in the root directory of this source tree).
9  * You may select, at your option, one of the above-listed licenses.
10  */
11 
12 /* This header contains definitions
13  * that shall **only** be used by modules within lib/compress.
14  */
15 
16 #ifndef ZSTD_COMPRESS_H
17 #define ZSTD_COMPRESS_H
18 
19 /*-*************************************
20 *  Dependencies
21 ***************************************/
22 #include "../common/zstd_internal.h"
23 #include "zstd_cwksp.h"
24 #ifdef ZSTD_MULTITHREAD
25 #  include "zstdmt_compress.h"
26 #endif
27 
28 #if defined (__cplusplus)
29 extern "C" {
30 #endif
31 
32 
33 /*-*************************************
34 *  Constants
35 ***************************************/
36 #define kSearchStrength      8
37 #define HASH_READ_SIZE       8
38 #define ZSTD_DUBT_UNSORTED_MARK 1   /* For btlazy2 strategy, index ZSTD_DUBT_UNSORTED_MARK==1 means "unsorted".
39                                        It could be confused for a real successor at index "1", if sorted as larger than its predecessor.
40                                        It's not a big deal though : candidate will just be sorted again.
41                                        Additionally, candidate position 1 will be lost.
42                                        But candidate 1 cannot hide a large tree of candidates, so it's a minimal loss.
43                                        The benefit is that ZSTD_DUBT_UNSORTED_MARK cannot be mishandled after table re-use with a different strategy.
44                                        This constant is required by ZSTD_compressBlock_btlazy2() and ZSTD_reduceTable_internal() */
45 
46 
47 /*-*************************************
48 *  Context memory management
49 ***************************************/
50 typedef enum { ZSTDcs_created=0, ZSTDcs_init, ZSTDcs_ongoing, ZSTDcs_ending } ZSTD_compressionStage_e;
51 typedef enum { zcss_init=0, zcss_load, zcss_flush } ZSTD_cStreamStage;
52 
53 typedef struct ZSTD_prefixDict_s {
54     const void* dict;
55     size_t dictSize;
56     ZSTD_dictContentType_e dictContentType;
57 } ZSTD_prefixDict;
58 
59 typedef struct {
60     void* dictBuffer;
61     void const* dict;
62     size_t dictSize;
63     ZSTD_dictContentType_e dictContentType;
64     ZSTD_CDict* cdict;
65 } ZSTD_localDict;
66 
67 typedef struct {
68     U32 CTable[HUF_CTABLE_SIZE_U32(255)];
69     HUF_repeat repeatMode;
70 } ZSTD_hufCTables_t;
71 
72 typedef struct {
73     FSE_CTable offcodeCTable[FSE_CTABLE_SIZE_U32(OffFSELog, MaxOff)];
74     FSE_CTable matchlengthCTable[FSE_CTABLE_SIZE_U32(MLFSELog, MaxML)];
75     FSE_CTable litlengthCTable[FSE_CTABLE_SIZE_U32(LLFSELog, MaxLL)];
76     FSE_repeat offcode_repeatMode;
77     FSE_repeat matchlength_repeatMode;
78     FSE_repeat litlength_repeatMode;
79 } ZSTD_fseCTables_t;
80 
81 typedef struct {
82     ZSTD_hufCTables_t huf;
83     ZSTD_fseCTables_t fse;
84 } ZSTD_entropyCTables_t;
85 
86 typedef struct {
87     U32 off;
88     U32 len;
89 } ZSTD_match_t;
90 
91 typedef struct {
92     int price;
93     U32 off;
94     U32 mlen;
95     U32 litlen;
96     U32 rep[ZSTD_REP_NUM];
97 } ZSTD_optimal_t;
98 
99 typedef enum { zop_dynamic=0, zop_predef } ZSTD_OptPrice_e;
100 
101 typedef struct {
102     /* All tables are allocated inside cctx->workspace by ZSTD_resetCCtx_internal() */
103     unsigned* litFreq;           /* table of literals statistics, of size 256 */
104     unsigned* litLengthFreq;     /* table of litLength statistics, of size (MaxLL+1) */
105     unsigned* matchLengthFreq;   /* table of matchLength statistics, of size (MaxML+1) */
106     unsigned* offCodeFreq;       /* table of offCode statistics, of size (MaxOff+1) */
107     ZSTD_match_t* matchTable;    /* list of found matches, of size ZSTD_OPT_NUM+1 */
108     ZSTD_optimal_t* priceTable;  /* All positions tracked by optimal parser, of size ZSTD_OPT_NUM+1 */
109 
110     U32  litSum;                 /* nb of literals */
111     U32  litLengthSum;           /* nb of litLength codes */
112     U32  matchLengthSum;         /* nb of matchLength codes */
113     U32  offCodeSum;             /* nb of offset codes */
114     U32  litSumBasePrice;        /* to compare to log2(litfreq) */
115     U32  litLengthSumBasePrice;  /* to compare to log2(llfreq)  */
116     U32  matchLengthSumBasePrice;/* to compare to log2(mlfreq)  */
117     U32  offCodeSumBasePrice;    /* to compare to log2(offreq)  */
118     ZSTD_OptPrice_e priceType;   /* prices can be determined dynamically, or follow a pre-defined cost structure */
119     const ZSTD_entropyCTables_t* symbolCosts;  /* pre-calculated dictionary statistics */
120     ZSTD_literalCompressionMode_e literalCompressionMode;
121 } optState_t;
122 
123 typedef struct {
124   ZSTD_entropyCTables_t entropy;
125   U32 rep[ZSTD_REP_NUM];
126 } ZSTD_compressedBlockState_t;
127 
128 typedef struct {
129     BYTE const* nextSrc;    /* next block here to continue on current prefix */
130     BYTE const* base;       /* All regular indexes relative to this position */
131     BYTE const* dictBase;   /* extDict indexes relative to this position */
132     U32 dictLimit;          /* below that point, need extDict */
133     U32 lowLimit;           /* below that point, no more valid data */
134 } ZSTD_window_t;
135 
136 typedef struct ZSTD_matchState_t ZSTD_matchState_t;
137 struct ZSTD_matchState_t {
138     ZSTD_window_t window;   /* State for window round buffer management */
139     U32 loadedDictEnd;      /* index of end of dictionary, within context's referential.
140                              * When loadedDictEnd != 0, a dictionary is in use, and still valid.
141                              * This relies on a mechanism to set loadedDictEnd=0 when dictionary is no longer within distance.
142                              * Such mechanism is provided within ZSTD_window_enforceMaxDist() and ZSTD_checkDictValidity().
143                              * When dict referential is copied into active context (i.e. not attached),
144                              * loadedDictEnd == dictSize, since referential starts from zero.
145                              */
146     U32 nextToUpdate;       /* index from which to continue table update */
147     U32 hashLog3;           /* dispatch table for matches of len==3 : larger == faster, more memory */
148     U32* hashTable;
149     U32* hashTable3;
150     U32* chainTable;
151     optState_t opt;         /* optimal parser state */
152     const ZSTD_matchState_t* dictMatchState;
153     ZSTD_compressionParameters cParams;
154 };
155 
156 typedef struct {
157     ZSTD_compressedBlockState_t* prevCBlock;
158     ZSTD_compressedBlockState_t* nextCBlock;
159     ZSTD_matchState_t matchState;
160 } ZSTD_blockState_t;
161 
162 typedef struct {
163     U32 offset;
164     U32 checksum;
165 } ldmEntry_t;
166 
167 typedef struct {
168     ZSTD_window_t window;   /* State for the window round buffer management */
169     ldmEntry_t* hashTable;
170     U32 loadedDictEnd;
171     BYTE* bucketOffsets;    /* Next position in bucket to insert entry */
172     U64 hashPower;          /* Used to compute the rolling hash.
173                              * Depends on ldmParams.minMatchLength */
174 } ldmState_t;
175 
176 typedef struct {
177     U32 enableLdm;          /* 1 if enable long distance matching */
178     U32 hashLog;            /* Log size of hashTable */
179     U32 bucketSizeLog;      /* Log bucket size for collision resolution, at most 8 */
180     U32 minMatchLength;     /* Minimum match length */
181     U32 hashRateLog;       /* Log number of entries to skip */
182     U32 windowLog;          /* Window log for the LDM */
183 } ldmParams_t;
184 
185 typedef struct {
186     U32 offset;
187     U32 litLength;
188     U32 matchLength;
189 } rawSeq;
190 
191 typedef struct {
192   rawSeq* seq;     /* The start of the sequences */
193   size_t pos;      /* The position where reading stopped. <= size. */
194   size_t size;     /* The number of sequences. <= capacity. */
195   size_t capacity; /* The capacity starting from `seq` pointer */
196 } rawSeqStore_t;
197 
198 typedef struct {
199     int collectSequences;
200     ZSTD_Sequence* seqStart;
201     size_t seqIndex;
202     size_t maxSequences;
203 } SeqCollector;
204 
205 struct ZSTD_CCtx_params_s {
206     ZSTD_format_e format;
207     ZSTD_compressionParameters cParams;
208     ZSTD_frameParameters fParams;
209 
210     int compressionLevel;
211     int forceWindow;           /* force back-references to respect limit of
212                                 * 1<<wLog, even for dictionary */
213     size_t targetCBlockSize;   /* Tries to fit compressed block size to be around targetCBlockSize.
214                                 * No target when targetCBlockSize == 0.
215                                 * There is no guarantee on compressed block size */
216     int srcSizeHint;           /* User's best guess of source size.
217                                 * Hint is not valid when srcSizeHint == 0.
218                                 * There is no guarantee that hint is close to actual source size */
219 
220     ZSTD_dictAttachPref_e attachDictPref;
221     ZSTD_literalCompressionMode_e literalCompressionMode;
222 
223     /* Multithreading: used to pass parameters to mtctx */
224     int nbWorkers;
225     size_t jobSize;
226     int overlapLog;
227     int rsyncable;
228 
229     /* Long distance matching parameters */
230     ldmParams_t ldmParams;
231 
232     /* Internal use, for createCCtxParams() and freeCCtxParams() only */
233     ZSTD_customMem customMem;
234 };  /* typedef'd to ZSTD_CCtx_params within "zstd.h" */
235 
236 struct ZSTD_CCtx_s {
237     ZSTD_compressionStage_e stage;
238     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. */
239     int bmi2;                            /* == 1 if the CPU supports BMI2 and 0 otherwise. CPU support is determined dynamically once per context lifetime. */
240     ZSTD_CCtx_params requestedParams;
241     ZSTD_CCtx_params appliedParams;
242     U32   dictID;
243 
244     ZSTD_cwksp workspace; /* manages buffer for dynamic allocations */
245     size_t blockSize;
246     unsigned long long pledgedSrcSizePlusOne;  /* this way, 0 (default) == unknown */
247     unsigned long long consumedSrcSize;
248     unsigned long long producedCSize;
249     XXH64_state_t xxhState;
250     ZSTD_customMem customMem;
251     size_t staticSize;
252     SeqCollector seqCollector;
253     int isFirstBlock;
254     int initialized;
255 
256     seqStore_t seqStore;      /* sequences storage ptrs */
257     ldmState_t ldmState;      /* long distance matching state */
258     rawSeq* ldmSequences;     /* Storage for the ldm output sequences */
259     size_t maxNbLdmSequences;
260     rawSeqStore_t externSeqStore; /* Mutable reference to external sequences */
261     ZSTD_blockState_t blockState;
262     U32* entropyWorkspace;  /* entropy workspace of HUF_WORKSPACE_SIZE bytes */
263 
264     /* streaming */
265     char*  inBuff;
266     size_t inBuffSize;
267     size_t inToCompress;
268     size_t inBuffPos;
269     size_t inBuffTarget;
270     char*  outBuff;
271     size_t outBuffSize;
272     size_t outBuffContentSize;
273     size_t outBuffFlushedSize;
274     ZSTD_cStreamStage streamStage;
275     U32    frameEnded;
276 
277     /* Dictionary */
278     ZSTD_localDict localDict;
279     const ZSTD_CDict* cdict;
280     ZSTD_prefixDict prefixDict;   /* single-usage dictionary */
281 
282     /* Multi-threading */
283 #ifdef ZSTD_MULTITHREAD
284     ZSTDMT_CCtx* mtctx;
285 #endif
286 };
287 
288 typedef enum { ZSTD_dtlm_fast, ZSTD_dtlm_full } ZSTD_dictTableLoadMethod_e;
289 
290 typedef enum { ZSTD_noDict = 0, ZSTD_extDict = 1, ZSTD_dictMatchState = 2 } ZSTD_dictMode_e;
291 
292 
293 typedef size_t (*ZSTD_blockCompressor) (
294         ZSTD_matchState_t* bs, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
295         void const* src, size_t srcSize);
296 ZSTD_blockCompressor ZSTD_selectBlockCompressor(ZSTD_strategy strat, ZSTD_dictMode_e dictMode);
297 
298 
ZSTD_LLcode(U32 litLength)299 MEM_STATIC U32 ZSTD_LLcode(U32 litLength)
300 {
301     static const BYTE LL_Code[64] = {  0,  1,  2,  3,  4,  5,  6,  7,
302                                        8,  9, 10, 11, 12, 13, 14, 15,
303                                       16, 16, 17, 17, 18, 18, 19, 19,
304                                       20, 20, 20, 20, 21, 21, 21, 21,
305                                       22, 22, 22, 22, 22, 22, 22, 22,
306                                       23, 23, 23, 23, 23, 23, 23, 23,
307                                       24, 24, 24, 24, 24, 24, 24, 24,
308                                       24, 24, 24, 24, 24, 24, 24, 24 };
309     static const U32 LL_deltaCode = 19;
310     return (litLength > 63) ? ZSTD_highbit32(litLength) + LL_deltaCode : LL_Code[litLength];
311 }
312 
313 /* ZSTD_MLcode() :
314  * note : mlBase = matchLength - MINMATCH;
315  *        because it's the format it's stored in seqStore->sequences */
ZSTD_MLcode(U32 mlBase)316 MEM_STATIC U32 ZSTD_MLcode(U32 mlBase)
317 {
318     static const BYTE ML_Code[128] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
319                                       16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
320                                       32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37,
321                                       38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39,
322                                       40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
323                                       41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
324                                       42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
325                                       42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 };
326     static const U32 ML_deltaCode = 36;
327     return (mlBase > 127) ? ZSTD_highbit32(mlBase) + ML_deltaCode : ML_Code[mlBase];
328 }
329 
330 typedef struct repcodes_s {
331     U32 rep[3];
332 } repcodes_t;
333 
ZSTD_updateRep(U32 const rep[3],U32 const offset,U32 const ll0)334 MEM_STATIC repcodes_t ZSTD_updateRep(U32 const rep[3], U32 const offset, U32 const ll0)
335 {
336     repcodes_t newReps;
337     if (offset >= ZSTD_REP_NUM) {  /* full offset */
338         newReps.rep[2] = rep[1];
339         newReps.rep[1] = rep[0];
340         newReps.rep[0] = offset - ZSTD_REP_MOVE;
341     } else {   /* repcode */
342         U32 const repCode = offset + ll0;
343         if (repCode > 0) {  /* note : if repCode==0, no change */
344             U32 const currentOffset = (repCode==ZSTD_REP_NUM) ? (rep[0] - 1) : rep[repCode];
345             newReps.rep[2] = (repCode >= 2) ? rep[1] : rep[2];
346             newReps.rep[1] = rep[0];
347             newReps.rep[0] = currentOffset;
348         } else {   /* repCode == 0 */
349             memcpy(&newReps, rep, sizeof(newReps));
350         }
351     }
352     return newReps;
353 }
354 
355 /* ZSTD_cParam_withinBounds:
356  * @return 1 if value is within cParam bounds,
357  * 0 otherwise */
ZSTD_cParam_withinBounds(ZSTD_cParameter cParam,int value)358 MEM_STATIC int ZSTD_cParam_withinBounds(ZSTD_cParameter cParam, int value)
359 {
360     ZSTD_bounds const bounds = ZSTD_cParam_getBounds(cParam);
361     if (ZSTD_isError(bounds.error)) return 0;
362     if (value < bounds.lowerBound) return 0;
363     if (value > bounds.upperBound) return 0;
364     return 1;
365 }
366 
367 /* ZSTD_noCompressBlock() :
368  * Writes uncompressed block to dst buffer from given src.
369  * Returns the size of the block */
ZSTD_noCompressBlock(void * dst,size_t dstCapacity,const void * src,size_t srcSize,U32 lastBlock)370 MEM_STATIC size_t ZSTD_noCompressBlock (void* dst, size_t dstCapacity, const void* src, size_t srcSize, U32 lastBlock)
371 {
372     U32 const cBlockHeader24 = lastBlock + (((U32)bt_raw)<<1) + (U32)(srcSize << 3);
373     RETURN_ERROR_IF(srcSize + ZSTD_blockHeaderSize > dstCapacity,
374                     dstSize_tooSmall, "dst buf too small for uncompressed block");
375     MEM_writeLE24(dst, cBlockHeader24);
376     memcpy((BYTE*)dst + ZSTD_blockHeaderSize, src, srcSize);
377     return ZSTD_blockHeaderSize + srcSize;
378 }
379 
ZSTD_rleCompressBlock(void * dst,size_t dstCapacity,BYTE src,size_t srcSize,U32 lastBlock)380 MEM_STATIC size_t ZSTD_rleCompressBlock (void* dst, size_t dstCapacity, BYTE src, size_t srcSize, U32 lastBlock)
381 {
382     BYTE* const op = (BYTE*)dst;
383     U32 const cBlockHeader = lastBlock + (((U32)bt_rle)<<1) + (U32)(srcSize << 3);
384     RETURN_ERROR_IF(dstCapacity < 4, dstSize_tooSmall, "");
385     MEM_writeLE24(op, cBlockHeader);
386     op[3] = src;
387     return 4;
388 }
389 
390 
391 /* ZSTD_minGain() :
392  * minimum compression required
393  * to generate a compress block or a compressed literals section.
394  * note : use same formula for both situations */
ZSTD_minGain(size_t srcSize,ZSTD_strategy strat)395 MEM_STATIC size_t ZSTD_minGain(size_t srcSize, ZSTD_strategy strat)
396 {
397     U32 const minlog = (strat>=ZSTD_btultra) ? (U32)(strat) - 1 : 6;
398     ZSTD_STATIC_ASSERT(ZSTD_btultra == 8);
399     assert(ZSTD_cParam_withinBounds(ZSTD_c_strategy, strat));
400     return (srcSize >> minlog) + 2;
401 }
402 
ZSTD_disableLiteralsCompression(const ZSTD_CCtx_params * cctxParams)403 MEM_STATIC int ZSTD_disableLiteralsCompression(const ZSTD_CCtx_params* cctxParams)
404 {
405     switch (cctxParams->literalCompressionMode) {
406     case ZSTD_lcm_huffman:
407         return 0;
408     case ZSTD_lcm_uncompressed:
409         return 1;
410     default:
411         assert(0 /* impossible: pre-validated */);
412         /* fall-through */
413     case ZSTD_lcm_auto:
414         return (cctxParams->cParams.strategy == ZSTD_fast) && (cctxParams->cParams.targetLength > 0);
415     }
416 }
417 
418 /*! ZSTD_safecopyLiterals() :
419  *  memcpy() function that won't read beyond more than WILDCOPY_OVERLENGTH bytes past ilimit_w.
420  *  Only called when the sequence ends past ilimit_w, so it only needs to be optimized for single
421  *  large copies.
422  */
ZSTD_safecopyLiterals(BYTE * op,BYTE const * ip,BYTE const * const iend,BYTE const * ilimit_w)423 static void ZSTD_safecopyLiterals(BYTE* op, BYTE const* ip, BYTE const* const iend, BYTE const* ilimit_w) {
424     assert(iend > ilimit_w);
425     if (ip <= ilimit_w) {
426         ZSTD_wildcopy(op, ip, ilimit_w - ip, ZSTD_no_overlap);
427         op += ilimit_w - ip;
428         ip = ilimit_w;
429     }
430     while (ip < iend) *op++ = *ip++;
431 }
432 
433 /*! ZSTD_storeSeq() :
434  *  Store a sequence (litlen, litPtr, offCode and mlBase) into seqStore_t.
435  *  `offCode` : distance to match + ZSTD_REP_MOVE (values <= ZSTD_REP_MOVE are repCodes).
436  *  `mlBase` : matchLength - MINMATCH
437  *  Allowed to overread literals up to litLimit.
438 */
439 HINT_INLINE UNUSED_ATTR
ZSTD_storeSeq(seqStore_t * seqStorePtr,size_t litLength,const BYTE * literals,const BYTE * litLimit,U32 offCode,size_t mlBase)440 void ZSTD_storeSeq(seqStore_t* seqStorePtr, size_t litLength, const BYTE* literals, const BYTE* litLimit, U32 offCode, size_t mlBase)
441 {
442     BYTE const* const litLimit_w = litLimit - WILDCOPY_OVERLENGTH;
443     BYTE const* const litEnd = literals + litLength;
444 #if defined(DEBUGLEVEL) && (DEBUGLEVEL >= 6)
445     static const BYTE* g_start = NULL;
446     if (g_start==NULL) g_start = (const BYTE*)literals;  /* note : index only works for compression within a single segment */
447     {   U32 const pos = (U32)((const BYTE*)literals - g_start);
448         DEBUGLOG(6, "Cpos%7u :%3u literals, match%4u bytes at offCode%7u",
449                pos, (U32)litLength, (U32)mlBase+MINMATCH, (U32)offCode);
450     }
451 #endif
452     assert((size_t)(seqStorePtr->sequences - seqStorePtr->sequencesStart) < seqStorePtr->maxNbSeq);
453     /* copy Literals */
454     assert(seqStorePtr->maxNbLit <= 128 KB);
455     assert(seqStorePtr->lit + litLength <= seqStorePtr->litStart + seqStorePtr->maxNbLit);
456     assert(literals + litLength <= litLimit);
457     if (litEnd <= litLimit_w) {
458         /* Common case we can use wildcopy.
459 	 * First copy 16 bytes, because literals are likely short.
460 	 */
461         assert(WILDCOPY_OVERLENGTH >= 16);
462         ZSTD_copy16(seqStorePtr->lit, literals);
463         if (litLength > 16) {
464             ZSTD_wildcopy(seqStorePtr->lit+16, literals+16, (ptrdiff_t)litLength-16, ZSTD_no_overlap);
465         }
466     } else {
467         ZSTD_safecopyLiterals(seqStorePtr->lit, literals, litEnd, litLimit_w);
468     }
469     seqStorePtr->lit += litLength;
470 
471     /* literal Length */
472     if (litLength>0xFFFF) {
473         assert(seqStorePtr->longLengthID == 0); /* there can only be a single long length */
474         seqStorePtr->longLengthID = 1;
475         seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
476     }
477     seqStorePtr->sequences[0].litLength = (U16)litLength;
478 
479     /* match offset */
480     seqStorePtr->sequences[0].offset = offCode + 1;
481 
482     /* match Length */
483     if (mlBase>0xFFFF) {
484         assert(seqStorePtr->longLengthID == 0); /* there can only be a single long length */
485         seqStorePtr->longLengthID = 2;
486         seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
487     }
488     seqStorePtr->sequences[0].matchLength = (U16)mlBase;
489 
490     seqStorePtr->sequences++;
491 }
492 
493 
494 /*-*************************************
495 *  Match length counter
496 ***************************************/
ZSTD_NbCommonBytes(size_t val)497 static unsigned ZSTD_NbCommonBytes (size_t val)
498 {
499     if (MEM_isLittleEndian()) {
500         if (MEM_64bits()) {
501 #       if defined(_MSC_VER) && defined(_WIN64)
502             unsigned long r = 0;
503             return _BitScanForward64( &r, (U64)val ) ? (unsigned)(r >> 3) : 0;
504 #       elif defined(__GNUC__) && (__GNUC__ >= 4)
505             return (__builtin_ctzll((U64)val) >> 3);
506 #       else
507             static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2,
508                                                      0, 3, 1, 3, 1, 4, 2, 7,
509                                                      0, 2, 3, 6, 1, 5, 3, 5,
510                                                      1, 3, 4, 4, 2, 5, 6, 7,
511                                                      7, 0, 1, 2, 3, 3, 4, 6,
512                                                      2, 6, 5, 5, 3, 4, 5, 6,
513                                                      7, 1, 2, 4, 6, 4, 4, 5,
514                                                      7, 2, 6, 5, 7, 6, 7, 7 };
515             return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
516 #       endif
517         } else { /* 32 bits */
518 #       if defined(_MSC_VER)
519             unsigned long r=0;
520             return _BitScanForward( &r, (U32)val ) ? (unsigned)(r >> 3) : 0;
521 #       elif defined(__GNUC__) && (__GNUC__ >= 3)
522             return (__builtin_ctz((U32)val) >> 3);
523 #       else
524             static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0,
525                                                      3, 2, 2, 1, 3, 2, 0, 1,
526                                                      3, 3, 1, 2, 2, 2, 2, 0,
527                                                      3, 1, 2, 0, 1, 0, 1, 1 };
528             return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
529 #       endif
530         }
531     } else {  /* Big Endian CPU */
532         if (MEM_64bits()) {
533 #       if defined(_MSC_VER) && defined(_WIN64)
534             unsigned long r = 0;
535             return _BitScanReverse64( &r, val ) ? (unsigned)(r >> 3) : 0;
536 #       elif defined(__GNUC__) && (__GNUC__ >= 4)
537             return (__builtin_clzll(val) >> 3);
538 #       else
539             unsigned r;
540             const unsigned n32 = sizeof(size_t)*4;   /* calculate this way due to compiler complaining in 32-bits mode */
541             if (!(val>>n32)) { r=4; } else { r=0; val>>=n32; }
542             if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
543             r += (!val);
544             return r;
545 #       endif
546         } else { /* 32 bits */
547 #       if defined(_MSC_VER)
548             unsigned long r = 0;
549             return _BitScanReverse( &r, (unsigned long)val ) ? (unsigned)(r >> 3) : 0;
550 #       elif defined(__GNUC__) && (__GNUC__ >= 3)
551             return (__builtin_clz((U32)val) >> 3);
552 #       else
553             unsigned r;
554             if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
555             r += (!val);
556             return r;
557 #       endif
558     }   }
559 }
560 
561 
ZSTD_count(const BYTE * pIn,const BYTE * pMatch,const BYTE * const pInLimit)562 MEM_STATIC size_t ZSTD_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* const pInLimit)
563 {
564     const BYTE* const pStart = pIn;
565     const BYTE* const pInLoopLimit = pInLimit - (sizeof(size_t)-1);
566 
567     if (pIn < pInLoopLimit) {
568         { size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
569           if (diff) return ZSTD_NbCommonBytes(diff); }
570         pIn+=sizeof(size_t); pMatch+=sizeof(size_t);
571         while (pIn < pInLoopLimit) {
572             size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn);
573             if (!diff) { pIn+=sizeof(size_t); pMatch+=sizeof(size_t); continue; }
574             pIn += ZSTD_NbCommonBytes(diff);
575             return (size_t)(pIn - pStart);
576     }   }
577     if (MEM_64bits() && (pIn<(pInLimit-3)) && (MEM_read32(pMatch) == MEM_read32(pIn))) { pIn+=4; pMatch+=4; }
578     if ((pIn<(pInLimit-1)) && (MEM_read16(pMatch) == MEM_read16(pIn))) { pIn+=2; pMatch+=2; }
579     if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
580     return (size_t)(pIn - pStart);
581 }
582 
583 /** ZSTD_count_2segments() :
584  *  can count match length with `ip` & `match` in 2 different segments.
585  *  convention : on reaching mEnd, match count continue starting from iStart
586  */
587 MEM_STATIC size_t
ZSTD_count_2segments(const BYTE * ip,const BYTE * match,const BYTE * iEnd,const BYTE * mEnd,const BYTE * iStart)588 ZSTD_count_2segments(const BYTE* ip, const BYTE* match,
589                      const BYTE* iEnd, const BYTE* mEnd, const BYTE* iStart)
590 {
591     const BYTE* const vEnd = MIN( ip + (mEnd - match), iEnd);
592     size_t const matchLength = ZSTD_count(ip, match, vEnd);
593     if (match + matchLength != mEnd) return matchLength;
594     DEBUGLOG(7, "ZSTD_count_2segments: found a 2-parts match (current length==%zu)", matchLength);
595     DEBUGLOG(7, "distance from match beginning to end dictionary = %zi", mEnd - match);
596     DEBUGLOG(7, "distance from current pos to end buffer = %zi", iEnd - ip);
597     DEBUGLOG(7, "next byte : ip==%02X, istart==%02X", ip[matchLength], *iStart);
598     DEBUGLOG(7, "final match length = %zu", matchLength + ZSTD_count(ip+matchLength, iStart, iEnd));
599     return matchLength + ZSTD_count(ip+matchLength, iStart, iEnd);
600 }
601 
602 
603 /*-*************************************
604  *  Hashes
605  ***************************************/
606 static const U32 prime3bytes = 506832829U;
ZSTD_hash3(U32 u,U32 h)607 static U32    ZSTD_hash3(U32 u, U32 h) { return ((u << (32-24)) * prime3bytes)  >> (32-h) ; }
ZSTD_hash3Ptr(const void * ptr,U32 h)608 MEM_STATIC size_t ZSTD_hash3Ptr(const void* ptr, U32 h) { return ZSTD_hash3(MEM_readLE32(ptr), h); } /* only in zstd_opt.h */
609 
610 static const U32 prime4bytes = 2654435761U;
ZSTD_hash4(U32 u,U32 h)611 static U32    ZSTD_hash4(U32 u, U32 h) { return (u * prime4bytes) >> (32-h) ; }
ZSTD_hash4Ptr(const void * ptr,U32 h)612 static size_t ZSTD_hash4Ptr(const void* ptr, U32 h) { return ZSTD_hash4(MEM_read32(ptr), h); }
613 
614 static const U64 prime5bytes = 889523592379ULL;
ZSTD_hash5(U64 u,U32 h)615 static size_t ZSTD_hash5(U64 u, U32 h) { return (size_t)(((u  << (64-40)) * prime5bytes) >> (64-h)) ; }
ZSTD_hash5Ptr(const void * p,U32 h)616 static size_t ZSTD_hash5Ptr(const void* p, U32 h) { return ZSTD_hash5(MEM_readLE64(p), h); }
617 
618 static const U64 prime6bytes = 227718039650203ULL;
ZSTD_hash6(U64 u,U32 h)619 static size_t ZSTD_hash6(U64 u, U32 h) { return (size_t)(((u  << (64-48)) * prime6bytes) >> (64-h)) ; }
ZSTD_hash6Ptr(const void * p,U32 h)620 static size_t ZSTD_hash6Ptr(const void* p, U32 h) { return ZSTD_hash6(MEM_readLE64(p), h); }
621 
622 static const U64 prime7bytes = 58295818150454627ULL;
ZSTD_hash7(U64 u,U32 h)623 static size_t ZSTD_hash7(U64 u, U32 h) { return (size_t)(((u  << (64-56)) * prime7bytes) >> (64-h)) ; }
ZSTD_hash7Ptr(const void * p,U32 h)624 static size_t ZSTD_hash7Ptr(const void* p, U32 h) { return ZSTD_hash7(MEM_readLE64(p), h); }
625 
626 static const U64 prime8bytes = 0xCF1BBCDCB7A56463ULL;
ZSTD_hash8(U64 u,U32 h)627 static size_t ZSTD_hash8(U64 u, U32 h) { return (size_t)(((u) * prime8bytes) >> (64-h)) ; }
ZSTD_hash8Ptr(const void * p,U32 h)628 static size_t ZSTD_hash8Ptr(const void* p, U32 h) { return ZSTD_hash8(MEM_readLE64(p), h); }
629 
ZSTD_hashPtr(const void * p,U32 hBits,U32 mls)630 MEM_STATIC size_t ZSTD_hashPtr(const void* p, U32 hBits, U32 mls)
631 {
632     switch(mls)
633     {
634     default:
635     case 4: return ZSTD_hash4Ptr(p, hBits);
636     case 5: return ZSTD_hash5Ptr(p, hBits);
637     case 6: return ZSTD_hash6Ptr(p, hBits);
638     case 7: return ZSTD_hash7Ptr(p, hBits);
639     case 8: return ZSTD_hash8Ptr(p, hBits);
640     }
641 }
642 
643 /** ZSTD_ipow() :
644  * Return base^exponent.
645  */
ZSTD_ipow(U64 base,U64 exponent)646 static U64 ZSTD_ipow(U64 base, U64 exponent)
647 {
648     U64 power = 1;
649     while (exponent) {
650       if (exponent & 1) power *= base;
651       exponent >>= 1;
652       base *= base;
653     }
654     return power;
655 }
656 
657 #define ZSTD_ROLL_HASH_CHAR_OFFSET 10
658 
659 /** ZSTD_rollingHash_append() :
660  * Add the buffer to the hash value.
661  */
ZSTD_rollingHash_append(U64 hash,void const * buf,size_t size)662 static U64 ZSTD_rollingHash_append(U64 hash, void const* buf, size_t size)
663 {
664     BYTE const* istart = (BYTE const*)buf;
665     size_t pos;
666     for (pos = 0; pos < size; ++pos) {
667         hash *= prime8bytes;
668         hash += istart[pos] + ZSTD_ROLL_HASH_CHAR_OFFSET;
669     }
670     return hash;
671 }
672 
673 /** ZSTD_rollingHash_compute() :
674  * Compute the rolling hash value of the buffer.
675  */
ZSTD_rollingHash_compute(void const * buf,size_t size)676 MEM_STATIC U64 ZSTD_rollingHash_compute(void const* buf, size_t size)
677 {
678     return ZSTD_rollingHash_append(0, buf, size);
679 }
680 
681 /** ZSTD_rollingHash_primePower() :
682  * Compute the primePower to be passed to ZSTD_rollingHash_rotate() for a hash
683  * over a window of length bytes.
684  */
ZSTD_rollingHash_primePower(U32 length)685 MEM_STATIC U64 ZSTD_rollingHash_primePower(U32 length)
686 {
687     return ZSTD_ipow(prime8bytes, length - 1);
688 }
689 
690 /** ZSTD_rollingHash_rotate() :
691  * Rotate the rolling hash by one byte.
692  */
ZSTD_rollingHash_rotate(U64 hash,BYTE toRemove,BYTE toAdd,U64 primePower)693 MEM_STATIC U64 ZSTD_rollingHash_rotate(U64 hash, BYTE toRemove, BYTE toAdd, U64 primePower)
694 {
695     hash -= (toRemove + ZSTD_ROLL_HASH_CHAR_OFFSET) * primePower;
696     hash *= prime8bytes;
697     hash += toAdd + ZSTD_ROLL_HASH_CHAR_OFFSET;
698     return hash;
699 }
700 
701 /*-*************************************
702 *  Round buffer management
703 ***************************************/
704 #if (ZSTD_WINDOWLOG_MAX_64 > 31)
705 # error "ZSTD_WINDOWLOG_MAX is too large : would overflow ZSTD_CURRENT_MAX"
706 #endif
707 /* Max current allowed */
708 #define ZSTD_CURRENT_MAX ((3U << 29) + (1U << ZSTD_WINDOWLOG_MAX))
709 /* Maximum chunk size before overflow correction needs to be called again */
710 #define ZSTD_CHUNKSIZE_MAX                                                     \
711     ( ((U32)-1)                  /* Maximum ending current index */            \
712     - ZSTD_CURRENT_MAX)          /* Maximum beginning lowLimit */
713 
714 /**
715  * ZSTD_window_clear():
716  * Clears the window containing the history by simply setting it to empty.
717  */
ZSTD_window_clear(ZSTD_window_t * window)718 MEM_STATIC void ZSTD_window_clear(ZSTD_window_t* window)
719 {
720     size_t const endT = (size_t)(window->nextSrc - window->base);
721     U32 const end = (U32)endT;
722 
723     window->lowLimit = end;
724     window->dictLimit = end;
725 }
726 
727 /**
728  * ZSTD_window_hasExtDict():
729  * Returns non-zero if the window has a non-empty extDict.
730  */
ZSTD_window_hasExtDict(ZSTD_window_t const window)731 MEM_STATIC U32 ZSTD_window_hasExtDict(ZSTD_window_t const window)
732 {
733     return window.lowLimit < window.dictLimit;
734 }
735 
736 /**
737  * ZSTD_matchState_dictMode():
738  * Inspects the provided matchState and figures out what dictMode should be
739  * passed to the compressor.
740  */
ZSTD_matchState_dictMode(const ZSTD_matchState_t * ms)741 MEM_STATIC ZSTD_dictMode_e ZSTD_matchState_dictMode(const ZSTD_matchState_t *ms)
742 {
743     return ZSTD_window_hasExtDict(ms->window) ?
744         ZSTD_extDict :
745         ms->dictMatchState != NULL ?
746             ZSTD_dictMatchState :
747             ZSTD_noDict;
748 }
749 
750 /**
751  * ZSTD_window_needOverflowCorrection():
752  * Returns non-zero if the indices are getting too large and need overflow
753  * protection.
754  */
ZSTD_window_needOverflowCorrection(ZSTD_window_t const window,void const * srcEnd)755 MEM_STATIC U32 ZSTD_window_needOverflowCorrection(ZSTD_window_t const window,
756                                                   void const* srcEnd)
757 {
758     U32 const current = (U32)((BYTE const*)srcEnd - window.base);
759     return current > ZSTD_CURRENT_MAX;
760 }
761 
762 /**
763  * ZSTD_window_correctOverflow():
764  * Reduces the indices to protect from index overflow.
765  * Returns the correction made to the indices, which must be applied to every
766  * stored index.
767  *
768  * The least significant cycleLog bits of the indices must remain the same,
769  * which may be 0. Every index up to maxDist in the past must be valid.
770  * NOTE: (maxDist & cycleMask) must be zero.
771  */
ZSTD_window_correctOverflow(ZSTD_window_t * window,U32 cycleLog,U32 maxDist,void const * src)772 MEM_STATIC U32 ZSTD_window_correctOverflow(ZSTD_window_t* window, U32 cycleLog,
773                                            U32 maxDist, void const* src)
774 {
775     /* preemptive overflow correction:
776      * 1. correction is large enough:
777      *    lowLimit > (3<<29) ==> current > 3<<29 + 1<<windowLog
778      *    1<<windowLog <= newCurrent < 1<<chainLog + 1<<windowLog
779      *
780      *    current - newCurrent
781      *    > (3<<29 + 1<<windowLog) - (1<<windowLog + 1<<chainLog)
782      *    > (3<<29) - (1<<chainLog)
783      *    > (3<<29) - (1<<30)             (NOTE: chainLog <= 30)
784      *    > 1<<29
785      *
786      * 2. (ip+ZSTD_CHUNKSIZE_MAX - cctx->base) doesn't overflow:
787      *    After correction, current is less than (1<<chainLog + 1<<windowLog).
788      *    In 64-bit mode we are safe, because we have 64-bit ptrdiff_t.
789      *    In 32-bit mode we are safe, because (chainLog <= 29), so
790      *    ip+ZSTD_CHUNKSIZE_MAX - cctx->base < 1<<32.
791      * 3. (cctx->lowLimit + 1<<windowLog) < 1<<32:
792      *    windowLog <= 31 ==> 3<<29 + 1<<windowLog < 7<<29 < 1<<32.
793      */
794     U32 const cycleMask = (1U << cycleLog) - 1;
795     U32 const current = (U32)((BYTE const*)src - window->base);
796     U32 const currentCycle0 = current & cycleMask;
797     /* Exclude zero so that newCurrent - maxDist >= 1. */
798     U32 const currentCycle1 = currentCycle0 == 0 ? (1U << cycleLog) : currentCycle0;
799     U32 const newCurrent = currentCycle1 + maxDist;
800     U32 const correction = current - newCurrent;
801     assert((maxDist & cycleMask) == 0);
802     assert(current > newCurrent);
803     /* Loose bound, should be around 1<<29 (see above) */
804     assert(correction > 1<<28);
805 
806     window->base += correction;
807     window->dictBase += correction;
808     if (window->lowLimit <= correction) window->lowLimit = 1;
809     else window->lowLimit -= correction;
810     if (window->dictLimit <= correction) window->dictLimit = 1;
811     else window->dictLimit -= correction;
812 
813     /* Ensure we can still reference the full window. */
814     assert(newCurrent >= maxDist);
815     assert(newCurrent - maxDist >= 1);
816     /* Ensure that lowLimit and dictLimit didn't underflow. */
817     assert(window->lowLimit <= newCurrent);
818     assert(window->dictLimit <= newCurrent);
819 
820     DEBUGLOG(4, "Correction of 0x%x bytes to lowLimit=0x%x", correction,
821              window->lowLimit);
822     return correction;
823 }
824 
825 /**
826  * ZSTD_window_enforceMaxDist():
827  * Updates lowLimit so that:
828  *    (srcEnd - base) - lowLimit == maxDist + loadedDictEnd
829  *
830  * It ensures index is valid as long as index >= lowLimit.
831  * This must be called before a block compression call.
832  *
833  * loadedDictEnd is only defined if a dictionary is in use for current compression.
834  * As the name implies, loadedDictEnd represents the index at end of dictionary.
835  * The value lies within context's referential, it can be directly compared to blockEndIdx.
836  *
837  * If loadedDictEndPtr is NULL, no dictionary is in use, and we use loadedDictEnd == 0.
838  * If loadedDictEndPtr is not NULL, we set it to zero after updating lowLimit.
839  * This is because dictionaries are allowed to be referenced fully
840  * as long as the last byte of the dictionary is in the window.
841  * Once input has progressed beyond window size, dictionary cannot be referenced anymore.
842  *
843  * In normal dict mode, the dictionary lies between lowLimit and dictLimit.
844  * In dictMatchState mode, lowLimit and dictLimit are the same,
845  * and the dictionary is below them.
846  * forceWindow and dictMatchState are therefore incompatible.
847  */
848 MEM_STATIC void
ZSTD_window_enforceMaxDist(ZSTD_window_t * window,const void * blockEnd,U32 maxDist,U32 * loadedDictEndPtr,const ZSTD_matchState_t ** dictMatchStatePtr)849 ZSTD_window_enforceMaxDist(ZSTD_window_t* window,
850                      const void* blockEnd,
851                            U32   maxDist,
852                            U32*  loadedDictEndPtr,
853                      const ZSTD_matchState_t** dictMatchStatePtr)
854 {
855     U32 const blockEndIdx = (U32)((BYTE const*)blockEnd - window->base);
856     U32 const loadedDictEnd = (loadedDictEndPtr != NULL) ? *loadedDictEndPtr : 0;
857     DEBUGLOG(5, "ZSTD_window_enforceMaxDist: blockEndIdx=%u, maxDist=%u, loadedDictEnd=%u",
858                 (unsigned)blockEndIdx, (unsigned)maxDist, (unsigned)loadedDictEnd);
859 
860     /* - When there is no dictionary : loadedDictEnd == 0.
861          In which case, the test (blockEndIdx > maxDist) is merely to avoid
862          overflowing next operation `newLowLimit = blockEndIdx - maxDist`.
863        - When there is a standard dictionary :
864          Index referential is copied from the dictionary,
865          which means it starts from 0.
866          In which case, loadedDictEnd == dictSize,
867          and it makes sense to compare `blockEndIdx > maxDist + dictSize`
868          since `blockEndIdx` also starts from zero.
869        - When there is an attached dictionary :
870          loadedDictEnd is expressed within the referential of the context,
871          so it can be directly compared against blockEndIdx.
872     */
873     if (blockEndIdx > maxDist + loadedDictEnd) {
874         U32 const newLowLimit = blockEndIdx - maxDist;
875         if (window->lowLimit < newLowLimit) window->lowLimit = newLowLimit;
876         if (window->dictLimit < window->lowLimit) {
877             DEBUGLOG(5, "Update dictLimit to match lowLimit, from %u to %u",
878                         (unsigned)window->dictLimit, (unsigned)window->lowLimit);
879             window->dictLimit = window->lowLimit;
880         }
881         /* On reaching window size, dictionaries are invalidated */
882         if (loadedDictEndPtr) *loadedDictEndPtr = 0;
883         if (dictMatchStatePtr) *dictMatchStatePtr = NULL;
884     }
885 }
886 
887 /* Similar to ZSTD_window_enforceMaxDist(),
888  * but only invalidates dictionary
889  * when input progresses beyond window size.
890  * assumption : loadedDictEndPtr and dictMatchStatePtr are valid (non NULL)
891  *              loadedDictEnd uses same referential as window->base
892  *              maxDist is the window size */
893 MEM_STATIC void
ZSTD_checkDictValidity(const ZSTD_window_t * window,const void * blockEnd,U32 maxDist,U32 * loadedDictEndPtr,const ZSTD_matchState_t ** dictMatchStatePtr)894 ZSTD_checkDictValidity(const ZSTD_window_t* window,
895                        const void* blockEnd,
896                              U32   maxDist,
897                              U32*  loadedDictEndPtr,
898                        const ZSTD_matchState_t** dictMatchStatePtr)
899 {
900     assert(loadedDictEndPtr != NULL);
901     assert(dictMatchStatePtr != NULL);
902     {   U32 const blockEndIdx = (U32)((BYTE const*)blockEnd - window->base);
903         U32 const loadedDictEnd = *loadedDictEndPtr;
904         DEBUGLOG(5, "ZSTD_checkDictValidity: blockEndIdx=%u, maxDist=%u, loadedDictEnd=%u",
905                     (unsigned)blockEndIdx, (unsigned)maxDist, (unsigned)loadedDictEnd);
906         assert(blockEndIdx >= loadedDictEnd);
907 
908         if (blockEndIdx > loadedDictEnd + maxDist) {
909             /* On reaching window size, dictionaries are invalidated.
910              * For simplification, if window size is reached anywhere within next block,
911              * the dictionary is invalidated for the full block.
912              */
913             DEBUGLOG(6, "invalidating dictionary for current block (distance > windowSize)");
914             *loadedDictEndPtr = 0;
915             *dictMatchStatePtr = NULL;
916         } else {
917             if (*loadedDictEndPtr != 0) {
918                 DEBUGLOG(6, "dictionary considered valid for current block");
919     }   }   }
920 }
921 
ZSTD_window_init(ZSTD_window_t * window)922 MEM_STATIC void ZSTD_window_init(ZSTD_window_t* window) {
923     memset(window, 0, sizeof(*window));
924     window->base = (BYTE const*)"";
925     window->dictBase = (BYTE const*)"";
926     window->dictLimit = 1;    /* start from 1, so that 1st position is valid */
927     window->lowLimit = 1;     /* it ensures first and later CCtx usages compress the same */
928     window->nextSrc = window->base + 1;   /* see issue #1241 */
929 }
930 
931 /**
932  * ZSTD_window_update():
933  * Updates the window by appending [src, src + srcSize) to the window.
934  * If it is not contiguous, the current prefix becomes the extDict, and we
935  * forget about the extDict. Handles overlap of the prefix and extDict.
936  * Returns non-zero if the segment is contiguous.
937  */
ZSTD_window_update(ZSTD_window_t * window,void const * src,size_t srcSize)938 MEM_STATIC U32 ZSTD_window_update(ZSTD_window_t* window,
939                                   void const* src, size_t srcSize)
940 {
941     BYTE const* const ip = (BYTE const*)src;
942     U32 contiguous = 1;
943     DEBUGLOG(5, "ZSTD_window_update");
944     if (srcSize == 0)
945         return contiguous;
946     assert(window->base != NULL);
947     assert(window->dictBase != NULL);
948     /* Check if blocks follow each other */
949     if (src != window->nextSrc) {
950         /* not contiguous */
951         size_t const distanceFromBase = (size_t)(window->nextSrc - window->base);
952         DEBUGLOG(5, "Non contiguous blocks, new segment starts at %u", window->dictLimit);
953         window->lowLimit = window->dictLimit;
954         assert(distanceFromBase == (size_t)(U32)distanceFromBase);  /* should never overflow */
955         window->dictLimit = (U32)distanceFromBase;
956         window->dictBase = window->base;
957         window->base = ip - distanceFromBase;
958         /* ms->nextToUpdate = window->dictLimit; */
959         if (window->dictLimit - window->lowLimit < HASH_READ_SIZE) window->lowLimit = window->dictLimit;   /* too small extDict */
960         contiguous = 0;
961     }
962     window->nextSrc = ip + srcSize;
963     /* if input and dictionary overlap : reduce dictionary (area presumed modified by input) */
964     if ( (ip+srcSize > window->dictBase + window->lowLimit)
965        & (ip < window->dictBase + window->dictLimit)) {
966         ptrdiff_t const highInputIdx = (ip + srcSize) - window->dictBase;
967         U32 const lowLimitMax = (highInputIdx > (ptrdiff_t)window->dictLimit) ? window->dictLimit : (U32)highInputIdx;
968         window->lowLimit = lowLimitMax;
969         DEBUGLOG(5, "Overlapping extDict and input : new lowLimit = %u", window->lowLimit);
970     }
971     return contiguous;
972 }
973 
974 /**
975  * Returns the lowest allowed match index. It may either be in the ext-dict or the prefix.
976  */
ZSTD_getLowestMatchIndex(const ZSTD_matchState_t * ms,U32 current,unsigned windowLog)977 MEM_STATIC U32 ZSTD_getLowestMatchIndex(const ZSTD_matchState_t* ms, U32 current, unsigned windowLog)
978 {
979     U32    const maxDistance = 1U << windowLog;
980     U32    const lowestValid = ms->window.lowLimit;
981     U32    const withinWindow = (current - lowestValid > maxDistance) ? current - maxDistance : lowestValid;
982     U32    const isDictionary = (ms->loadedDictEnd != 0);
983     U32    const matchLowest = isDictionary ? lowestValid : withinWindow;
984     return matchLowest;
985 }
986 
987 /**
988  * Returns the lowest allowed match index in the prefix.
989  */
ZSTD_getLowestPrefixIndex(const ZSTD_matchState_t * ms,U32 current,unsigned windowLog)990 MEM_STATIC U32 ZSTD_getLowestPrefixIndex(const ZSTD_matchState_t* ms, U32 current, unsigned windowLog)
991 {
992     U32    const maxDistance = 1U << windowLog;
993     U32    const lowestValid = ms->window.dictLimit;
994     U32    const withinWindow = (current - lowestValid > maxDistance) ? current - maxDistance : lowestValid;
995     U32    const isDictionary = (ms->loadedDictEnd != 0);
996     U32    const matchLowest = isDictionary ? lowestValid : withinWindow;
997     return matchLowest;
998 }
999 
1000 
1001 
1002 /* debug functions */
1003 #if (DEBUGLEVEL>=2)
1004 
ZSTD_fWeight(U32 rawStat)1005 MEM_STATIC double ZSTD_fWeight(U32 rawStat)
1006 {
1007     U32 const fp_accuracy = 8;
1008     U32 const fp_multiplier = (1 << fp_accuracy);
1009     U32 const newStat = rawStat + 1;
1010     U32 const hb = ZSTD_highbit32(newStat);
1011     U32 const BWeight = hb * fp_multiplier;
1012     U32 const FWeight = (newStat << fp_accuracy) >> hb;
1013     U32 const weight = BWeight + FWeight;
1014     assert(hb + fp_accuracy < 31);
1015     return (double)weight / fp_multiplier;
1016 }
1017 
1018 /* display a table content,
1019  * listing each element, its frequency, and its predicted bit cost */
ZSTD_debugTable(const U32 * table,U32 max)1020 MEM_STATIC void ZSTD_debugTable(const U32* table, U32 max)
1021 {
1022     unsigned u, sum;
1023     for (u=0, sum=0; u<=max; u++) sum += table[u];
1024     DEBUGLOG(2, "total nb elts: %u", sum);
1025     for (u=0; u<=max; u++) {
1026         DEBUGLOG(2, "%2u: %5u  (%.2f)",
1027                 u, table[u], ZSTD_fWeight(sum) - ZSTD_fWeight(table[u]) );
1028     }
1029 }
1030 
1031 #endif
1032 
1033 
1034 #if defined (__cplusplus)
1035 }
1036 #endif
1037 
1038 /* ===============================================================
1039  * Shared internal declarations
1040  * These prototypes may be called from sources not in lib/compress
1041  * =============================================================== */
1042 
1043 /* ZSTD_loadCEntropy() :
1044  * dict : must point at beginning of a valid zstd dictionary.
1045  * return : size of dictionary header (size of magic number + dict ID + entropy tables)
1046  * assumptions : magic number supposed already checked
1047  *               and dictSize >= 8 */
1048 size_t ZSTD_loadCEntropy(ZSTD_compressedBlockState_t* bs, void* workspace,
1049                          short* offcodeNCount, unsigned* offcodeMaxValue,
1050                          const void* const dict, size_t dictSize);
1051 
1052 void ZSTD_reset_compressedBlockState(ZSTD_compressedBlockState_t* bs);
1053 
1054 /* ==============================================================
1055  * Private declarations
1056  * These prototypes shall only be called from within lib/compress
1057  * ============================================================== */
1058 
1059 /* ZSTD_getCParamsFromCCtxParams() :
1060  * cParams are built depending on compressionLevel, src size hints,
1061  * LDM and manually set compression parameters.
1062  * Note: srcSizeHint == 0 means 0!
1063  */
1064 ZSTD_compressionParameters ZSTD_getCParamsFromCCtxParams(
1065         const ZSTD_CCtx_params* CCtxParams, U64 srcSizeHint, size_t dictSize);
1066 
1067 /*! ZSTD_initCStream_internal() :
1068  *  Private use only. Init streaming operation.
1069  *  expects params to be valid.
1070  *  must receive dict, or cdict, or none, but not both.
1071  *  @return : 0, or an error code */
1072 size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs,
1073                      const void* dict, size_t dictSize,
1074                      const ZSTD_CDict* cdict,
1075                      const ZSTD_CCtx_params* params, unsigned long long pledgedSrcSize);
1076 
1077 void ZSTD_resetSeqStore(seqStore_t* ssPtr);
1078 
1079 /*! ZSTD_getCParamsFromCDict() :
1080  *  as the name implies */
1081 ZSTD_compressionParameters ZSTD_getCParamsFromCDict(const ZSTD_CDict* cdict);
1082 
1083 /* ZSTD_compressBegin_advanced_internal() :
1084  * Private use only. To be called from zstdmt_compress.c. */
1085 size_t ZSTD_compressBegin_advanced_internal(ZSTD_CCtx* cctx,
1086                                     const void* dict, size_t dictSize,
1087                                     ZSTD_dictContentType_e dictContentType,
1088                                     ZSTD_dictTableLoadMethod_e dtlm,
1089                                     const ZSTD_CDict* cdict,
1090                                     const ZSTD_CCtx_params* params,
1091                                     unsigned long long pledgedSrcSize);
1092 
1093 /* ZSTD_compress_advanced_internal() :
1094  * Private use only. To be called from zstdmt_compress.c. */
1095 size_t ZSTD_compress_advanced_internal(ZSTD_CCtx* cctx,
1096                                        void* dst, size_t dstCapacity,
1097                                  const void* src, size_t srcSize,
1098                                  const void* dict,size_t dictSize,
1099                                  const ZSTD_CCtx_params* params);
1100 
1101 
1102 /* ZSTD_writeLastEmptyBlock() :
1103  * output an empty Block with end-of-frame mark to complete a frame
1104  * @return : size of data written into `dst` (== ZSTD_blockHeaderSize (defined in zstd_internal.h))
1105  *           or an error code if `dstCapacity` is too small (<ZSTD_blockHeaderSize)
1106  */
1107 size_t ZSTD_writeLastEmptyBlock(void* dst, size_t dstCapacity);
1108 
1109 
1110 /* ZSTD_referenceExternalSequences() :
1111  * Must be called before starting a compression operation.
1112  * seqs must parse a prefix of the source.
1113  * This cannot be used when long range matching is enabled.
1114  * Zstd will use these sequences, and pass the literals to a secondary block
1115  * compressor.
1116  * @return : An error code on failure.
1117  * NOTE: seqs are not verified! Invalid sequences can cause out-of-bounds memory
1118  * access and data corruption.
1119  */
1120 size_t ZSTD_referenceExternalSequences(ZSTD_CCtx* cctx, rawSeq* seq, size_t nbSeq);
1121 
1122 /** ZSTD_cycleLog() :
1123  *  condition for correct operation : hashLog > 1 */
1124 U32 ZSTD_cycleLog(U32 hashLog, ZSTD_strategy strat);
1125 
1126 #endif /* ZSTD_COMPRESS_H */
1127