1*4882a593Smuzhiyun /**
2*4882a593Smuzhiyun * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3*4882a593Smuzhiyun * All rights reserved.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This source code is licensed under the BSD-style license found in the
6*4882a593Smuzhiyun * LICENSE file in the root directory of https://github.com/facebook/zstd.
7*4882a593Smuzhiyun * An additional grant of patent rights can be found in the PATENTS file in the
8*4882a593Smuzhiyun * same directory.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify it under
11*4882a593Smuzhiyun * the terms of the GNU General Public License version 2 as published by the
12*4882a593Smuzhiyun * Free Software Foundation. This program is dual-licensed; you may select
13*4882a593Smuzhiyun * either version 2 of the GNU General Public License ("GPL") or BSD license
14*4882a593Smuzhiyun * ("BSD").
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /*-*************************************
18*4882a593Smuzhiyun * Dependencies
19*4882a593Smuzhiyun ***************************************/
20*4882a593Smuzhiyun #include "fse.h"
21*4882a593Smuzhiyun #include "huf.h"
22*4882a593Smuzhiyun #include "mem.h"
23*4882a593Smuzhiyun #include "zstd_internal.h" /* includes zstd.h */
24*4882a593Smuzhiyun #include <linux/kernel.h>
25*4882a593Smuzhiyun #include <linux/module.h>
26*4882a593Smuzhiyun #include <linux/string.h> /* memset */
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /*-*************************************
29*4882a593Smuzhiyun * Constants
30*4882a593Smuzhiyun ***************************************/
31*4882a593Smuzhiyun static const U32 g_searchStrength = 8; /* control skip over incompressible data */
32*4882a593Smuzhiyun #define HASH_READ_SIZE 8
33*4882a593Smuzhiyun typedef enum { ZSTDcs_created = 0, ZSTDcs_init, ZSTDcs_ongoing, ZSTDcs_ending } ZSTD_compressionStage_e;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /*-*************************************
36*4882a593Smuzhiyun * Helper functions
37*4882a593Smuzhiyun ***************************************/
ZSTD_compressBound(size_t srcSize)38*4882a593Smuzhiyun size_t ZSTD_compressBound(size_t srcSize) { return FSE_compressBound(srcSize) + 12; }
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /*-*************************************
41*4882a593Smuzhiyun * Sequence storage
42*4882a593Smuzhiyun ***************************************/
ZSTD_resetSeqStore(seqStore_t * ssPtr)43*4882a593Smuzhiyun static void ZSTD_resetSeqStore(seqStore_t *ssPtr)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun ssPtr->lit = ssPtr->litStart;
46*4882a593Smuzhiyun ssPtr->sequences = ssPtr->sequencesStart;
47*4882a593Smuzhiyun ssPtr->longLengthID = 0;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /*-*************************************
51*4882a593Smuzhiyun * Context memory management
52*4882a593Smuzhiyun ***************************************/
53*4882a593Smuzhiyun struct ZSTD_CCtx_s {
54*4882a593Smuzhiyun const BYTE *nextSrc; /* next block here to continue on curr prefix */
55*4882a593Smuzhiyun const BYTE *base; /* All regular indexes relative to this position */
56*4882a593Smuzhiyun const BYTE *dictBase; /* extDict indexes relative to this position */
57*4882a593Smuzhiyun U32 dictLimit; /* below that point, need extDict */
58*4882a593Smuzhiyun U32 lowLimit; /* below that point, no more data */
59*4882a593Smuzhiyun U32 nextToUpdate; /* index from which to continue dictionary update */
60*4882a593Smuzhiyun U32 nextToUpdate3; /* index from which to continue dictionary update */
61*4882a593Smuzhiyun U32 hashLog3; /* dispatch table : larger == faster, more memory */
62*4882a593Smuzhiyun U32 loadedDictEnd; /* index of end of dictionary */
63*4882a593Smuzhiyun U32 forceWindow; /* force back-references to respect limit of 1<<wLog, even for dictionary */
64*4882a593Smuzhiyun U32 forceRawDict; /* Force loading dictionary in "content-only" mode (no header analysis) */
65*4882a593Smuzhiyun ZSTD_compressionStage_e stage;
66*4882a593Smuzhiyun U32 rep[ZSTD_REP_NUM];
67*4882a593Smuzhiyun U32 repToConfirm[ZSTD_REP_NUM];
68*4882a593Smuzhiyun U32 dictID;
69*4882a593Smuzhiyun ZSTD_parameters params;
70*4882a593Smuzhiyun void *workSpace;
71*4882a593Smuzhiyun size_t workSpaceSize;
72*4882a593Smuzhiyun size_t blockSize;
73*4882a593Smuzhiyun U64 frameContentSize;
74*4882a593Smuzhiyun struct xxh64_state xxhState;
75*4882a593Smuzhiyun ZSTD_customMem customMem;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun seqStore_t seqStore; /* sequences storage ptrs */
78*4882a593Smuzhiyun U32 *hashTable;
79*4882a593Smuzhiyun U32 *hashTable3;
80*4882a593Smuzhiyun U32 *chainTable;
81*4882a593Smuzhiyun HUF_CElt *hufTable;
82*4882a593Smuzhiyun U32 flagStaticTables;
83*4882a593Smuzhiyun HUF_repeat flagStaticHufTable;
84*4882a593Smuzhiyun FSE_CTable offcodeCTable[FSE_CTABLE_SIZE_U32(OffFSELog, MaxOff)];
85*4882a593Smuzhiyun FSE_CTable matchlengthCTable[FSE_CTABLE_SIZE_U32(MLFSELog, MaxML)];
86*4882a593Smuzhiyun FSE_CTable litlengthCTable[FSE_CTABLE_SIZE_U32(LLFSELog, MaxLL)];
87*4882a593Smuzhiyun unsigned tmpCounters[HUF_COMPRESS_WORKSPACE_SIZE_U32];
88*4882a593Smuzhiyun };
89*4882a593Smuzhiyun
ZSTD_CCtxWorkspaceBound(ZSTD_compressionParameters cParams)90*4882a593Smuzhiyun size_t ZSTD_CCtxWorkspaceBound(ZSTD_compressionParameters cParams)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun size_t const blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, (size_t)1 << cParams.windowLog);
93*4882a593Smuzhiyun U32 const divider = (cParams.searchLength == 3) ? 3 : 4;
94*4882a593Smuzhiyun size_t const maxNbSeq = blockSize / divider;
95*4882a593Smuzhiyun size_t const tokenSpace = blockSize + 11 * maxNbSeq;
96*4882a593Smuzhiyun size_t const chainSize = (cParams.strategy == ZSTD_fast) ? 0 : (1 << cParams.chainLog);
97*4882a593Smuzhiyun size_t const hSize = ((size_t)1) << cParams.hashLog;
98*4882a593Smuzhiyun U32 const hashLog3 = (cParams.searchLength > 3) ? 0 : MIN(ZSTD_HASHLOG3_MAX, cParams.windowLog);
99*4882a593Smuzhiyun size_t const h3Size = ((size_t)1) << hashLog3;
100*4882a593Smuzhiyun size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32);
101*4882a593Smuzhiyun size_t const optSpace =
102*4882a593Smuzhiyun ((MaxML + 1) + (MaxLL + 1) + (MaxOff + 1) + (1 << Litbits)) * sizeof(U32) + (ZSTD_OPT_NUM + 1) * (sizeof(ZSTD_match_t) + sizeof(ZSTD_optimal_t));
103*4882a593Smuzhiyun size_t const workspaceSize = tableSpace + (256 * sizeof(U32)) /* huffTable */ + tokenSpace +
104*4882a593Smuzhiyun (((cParams.strategy == ZSTD_btopt) || (cParams.strategy == ZSTD_btopt2)) ? optSpace : 0);
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun return ZSTD_ALIGN(sizeof(ZSTD_stack)) + ZSTD_ALIGN(sizeof(ZSTD_CCtx)) + ZSTD_ALIGN(workspaceSize);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
ZSTD_createCCtx_advanced(ZSTD_customMem customMem)109*4882a593Smuzhiyun static ZSTD_CCtx *ZSTD_createCCtx_advanced(ZSTD_customMem customMem)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun ZSTD_CCtx *cctx;
112*4882a593Smuzhiyun if (!customMem.customAlloc || !customMem.customFree)
113*4882a593Smuzhiyun return NULL;
114*4882a593Smuzhiyun cctx = (ZSTD_CCtx *)ZSTD_malloc(sizeof(ZSTD_CCtx), customMem);
115*4882a593Smuzhiyun if (!cctx)
116*4882a593Smuzhiyun return NULL;
117*4882a593Smuzhiyun memset(cctx, 0, sizeof(ZSTD_CCtx));
118*4882a593Smuzhiyun cctx->customMem = customMem;
119*4882a593Smuzhiyun return cctx;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
ZSTD_initCCtx(void * workspace,size_t workspaceSize)122*4882a593Smuzhiyun ZSTD_CCtx *ZSTD_initCCtx(void *workspace, size_t workspaceSize)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
125*4882a593Smuzhiyun ZSTD_CCtx *cctx = ZSTD_createCCtx_advanced(stackMem);
126*4882a593Smuzhiyun if (cctx) {
127*4882a593Smuzhiyun cctx->workSpace = ZSTD_stackAllocAll(cctx->customMem.opaque, &cctx->workSpaceSize);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun return cctx;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
ZSTD_freeCCtx(ZSTD_CCtx * cctx)132*4882a593Smuzhiyun size_t ZSTD_freeCCtx(ZSTD_CCtx *cctx)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun if (cctx == NULL)
135*4882a593Smuzhiyun return 0; /* support free on NULL */
136*4882a593Smuzhiyun ZSTD_free(cctx->workSpace, cctx->customMem);
137*4882a593Smuzhiyun ZSTD_free(cctx, cctx->customMem);
138*4882a593Smuzhiyun return 0; /* reserved as a potential error code in the future */
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
ZSTD_getSeqStore(const ZSTD_CCtx * ctx)141*4882a593Smuzhiyun const seqStore_t *ZSTD_getSeqStore(const ZSTD_CCtx *ctx) /* hidden interface */ { return &(ctx->seqStore); }
142*4882a593Smuzhiyun
ZSTD_getParamsFromCCtx(const ZSTD_CCtx * cctx)143*4882a593Smuzhiyun static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx *cctx) { return cctx->params; }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /** ZSTD_checkParams() :
146*4882a593Smuzhiyun ensure param values remain within authorized range.
147*4882a593Smuzhiyun @return : 0, or an error code if one value is beyond authorized range */
ZSTD_checkCParams(ZSTD_compressionParameters cParams)148*4882a593Smuzhiyun size_t ZSTD_checkCParams(ZSTD_compressionParameters cParams)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun #define CLAMPCHECK(val, min, max) \
151*4882a593Smuzhiyun { \
152*4882a593Smuzhiyun if ((val < min) | (val > max)) \
153*4882a593Smuzhiyun return ERROR(compressionParameter_unsupported); \
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun CLAMPCHECK(cParams.windowLog, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX);
156*4882a593Smuzhiyun CLAMPCHECK(cParams.chainLog, ZSTD_CHAINLOG_MIN, ZSTD_CHAINLOG_MAX);
157*4882a593Smuzhiyun CLAMPCHECK(cParams.hashLog, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX);
158*4882a593Smuzhiyun CLAMPCHECK(cParams.searchLog, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX);
159*4882a593Smuzhiyun CLAMPCHECK(cParams.searchLength, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX);
160*4882a593Smuzhiyun CLAMPCHECK(cParams.targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX);
161*4882a593Smuzhiyun if ((U32)(cParams.strategy) > (U32)ZSTD_btopt2)
162*4882a593Smuzhiyun return ERROR(compressionParameter_unsupported);
163*4882a593Smuzhiyun return 0;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /** ZSTD_cycleLog() :
167*4882a593Smuzhiyun * condition for correct operation : hashLog > 1 */
ZSTD_cycleLog(U32 hashLog,ZSTD_strategy strat)168*4882a593Smuzhiyun static U32 ZSTD_cycleLog(U32 hashLog, ZSTD_strategy strat)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun U32 const btScale = ((U32)strat >= (U32)ZSTD_btlazy2);
171*4882a593Smuzhiyun return hashLog - btScale;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /** ZSTD_adjustCParams() :
175*4882a593Smuzhiyun optimize `cPar` for a given input (`srcSize` and `dictSize`).
176*4882a593Smuzhiyun mostly downsizing to reduce memory consumption and initialization.
177*4882a593Smuzhiyun Both `srcSize` and `dictSize` are optional (use 0 if unknown),
178*4882a593Smuzhiyun but if both are 0, no optimization can be done.
179*4882a593Smuzhiyun Note : cPar is considered validated at this stage. Use ZSTD_checkParams() to ensure that. */
ZSTD_adjustCParams(ZSTD_compressionParameters cPar,unsigned long long srcSize,size_t dictSize)180*4882a593Smuzhiyun ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun if (srcSize + dictSize == 0)
183*4882a593Smuzhiyun return cPar; /* no size information available : no adjustment */
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /* resize params, to use less memory when necessary */
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun U32 const minSrcSize = (srcSize == 0) ? 500 : 0;
188*4882a593Smuzhiyun U64 const rSize = srcSize + dictSize + minSrcSize;
189*4882a593Smuzhiyun if (rSize < ((U64)1 << ZSTD_WINDOWLOG_MAX)) {
190*4882a593Smuzhiyun U32 const srcLog = MAX(ZSTD_HASHLOG_MIN, ZSTD_highbit32((U32)(rSize)-1) + 1);
191*4882a593Smuzhiyun if (cPar.windowLog > srcLog)
192*4882a593Smuzhiyun cPar.windowLog = srcLog;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun if (cPar.hashLog > cPar.windowLog)
196*4882a593Smuzhiyun cPar.hashLog = cPar.windowLog;
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun U32 const cycleLog = ZSTD_cycleLog(cPar.chainLog, cPar.strategy);
199*4882a593Smuzhiyun if (cycleLog > cPar.windowLog)
200*4882a593Smuzhiyun cPar.chainLog -= (cycleLog - cPar.windowLog);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun if (cPar.windowLog < ZSTD_WINDOWLOG_ABSOLUTEMIN)
204*4882a593Smuzhiyun cPar.windowLog = ZSTD_WINDOWLOG_ABSOLUTEMIN; /* required for frame header */
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun return cPar;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
ZSTD_equivalentParams(ZSTD_parameters param1,ZSTD_parameters param2)209*4882a593Smuzhiyun static U32 ZSTD_equivalentParams(ZSTD_parameters param1, ZSTD_parameters param2)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun return (param1.cParams.hashLog == param2.cParams.hashLog) & (param1.cParams.chainLog == param2.cParams.chainLog) &
212*4882a593Smuzhiyun (param1.cParams.strategy == param2.cParams.strategy) & ((param1.cParams.searchLength == 3) == (param2.cParams.searchLength == 3));
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /*! ZSTD_continueCCtx() :
216*4882a593Smuzhiyun reuse CCtx without reset (note : requires no dictionary) */
ZSTD_continueCCtx(ZSTD_CCtx * cctx,ZSTD_parameters params,U64 frameContentSize)217*4882a593Smuzhiyun static size_t ZSTD_continueCCtx(ZSTD_CCtx *cctx, ZSTD_parameters params, U64 frameContentSize)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun U32 const end = (U32)(cctx->nextSrc - cctx->base);
220*4882a593Smuzhiyun cctx->params = params;
221*4882a593Smuzhiyun cctx->frameContentSize = frameContentSize;
222*4882a593Smuzhiyun cctx->lowLimit = end;
223*4882a593Smuzhiyun cctx->dictLimit = end;
224*4882a593Smuzhiyun cctx->nextToUpdate = end + 1;
225*4882a593Smuzhiyun cctx->stage = ZSTDcs_init;
226*4882a593Smuzhiyun cctx->dictID = 0;
227*4882a593Smuzhiyun cctx->loadedDictEnd = 0;
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun int i;
230*4882a593Smuzhiyun for (i = 0; i < ZSTD_REP_NUM; i++)
231*4882a593Smuzhiyun cctx->rep[i] = repStartValue[i];
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun cctx->seqStore.litLengthSum = 0; /* force reset of btopt stats */
234*4882a593Smuzhiyun xxh64_reset(&cctx->xxhState, 0);
235*4882a593Smuzhiyun return 0;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun typedef enum { ZSTDcrp_continue, ZSTDcrp_noMemset, ZSTDcrp_fullReset } ZSTD_compResetPolicy_e;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun /*! ZSTD_resetCCtx_advanced() :
241*4882a593Smuzhiyun note : `params` must be validated */
ZSTD_resetCCtx_advanced(ZSTD_CCtx * zc,ZSTD_parameters params,U64 frameContentSize,ZSTD_compResetPolicy_e const crp)242*4882a593Smuzhiyun static size_t ZSTD_resetCCtx_advanced(ZSTD_CCtx *zc, ZSTD_parameters params, U64 frameContentSize, ZSTD_compResetPolicy_e const crp)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun if (crp == ZSTDcrp_continue)
245*4882a593Smuzhiyun if (ZSTD_equivalentParams(params, zc->params)) {
246*4882a593Smuzhiyun zc->flagStaticTables = 0;
247*4882a593Smuzhiyun zc->flagStaticHufTable = HUF_repeat_none;
248*4882a593Smuzhiyun return ZSTD_continueCCtx(zc, params, frameContentSize);
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun size_t const blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, (size_t)1 << params.cParams.windowLog);
253*4882a593Smuzhiyun U32 const divider = (params.cParams.searchLength == 3) ? 3 : 4;
254*4882a593Smuzhiyun size_t const maxNbSeq = blockSize / divider;
255*4882a593Smuzhiyun size_t const tokenSpace = blockSize + 11 * maxNbSeq;
256*4882a593Smuzhiyun size_t const chainSize = (params.cParams.strategy == ZSTD_fast) ? 0 : (1 << params.cParams.chainLog);
257*4882a593Smuzhiyun size_t const hSize = ((size_t)1) << params.cParams.hashLog;
258*4882a593Smuzhiyun U32 const hashLog3 = (params.cParams.searchLength > 3) ? 0 : MIN(ZSTD_HASHLOG3_MAX, params.cParams.windowLog);
259*4882a593Smuzhiyun size_t const h3Size = ((size_t)1) << hashLog3;
260*4882a593Smuzhiyun size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32);
261*4882a593Smuzhiyun void *ptr;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /* Check if workSpace is large enough, alloc a new one if needed */
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun size_t const optSpace = ((MaxML + 1) + (MaxLL + 1) + (MaxOff + 1) + (1 << Litbits)) * sizeof(U32) +
266*4882a593Smuzhiyun (ZSTD_OPT_NUM + 1) * (sizeof(ZSTD_match_t) + sizeof(ZSTD_optimal_t));
267*4882a593Smuzhiyun size_t const neededSpace = tableSpace + (256 * sizeof(U32)) /* huffTable */ + tokenSpace +
268*4882a593Smuzhiyun (((params.cParams.strategy == ZSTD_btopt) || (params.cParams.strategy == ZSTD_btopt2)) ? optSpace : 0);
269*4882a593Smuzhiyun if (zc->workSpaceSize < neededSpace) {
270*4882a593Smuzhiyun ZSTD_free(zc->workSpace, zc->customMem);
271*4882a593Smuzhiyun zc->workSpace = ZSTD_malloc(neededSpace, zc->customMem);
272*4882a593Smuzhiyun if (zc->workSpace == NULL)
273*4882a593Smuzhiyun return ERROR(memory_allocation);
274*4882a593Smuzhiyun zc->workSpaceSize = neededSpace;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun if (crp != ZSTDcrp_noMemset)
279*4882a593Smuzhiyun memset(zc->workSpace, 0, tableSpace); /* reset tables only */
280*4882a593Smuzhiyun xxh64_reset(&zc->xxhState, 0);
281*4882a593Smuzhiyun zc->hashLog3 = hashLog3;
282*4882a593Smuzhiyun zc->hashTable = (U32 *)(zc->workSpace);
283*4882a593Smuzhiyun zc->chainTable = zc->hashTable + hSize;
284*4882a593Smuzhiyun zc->hashTable3 = zc->chainTable + chainSize;
285*4882a593Smuzhiyun ptr = zc->hashTable3 + h3Size;
286*4882a593Smuzhiyun zc->hufTable = (HUF_CElt *)ptr;
287*4882a593Smuzhiyun zc->flagStaticTables = 0;
288*4882a593Smuzhiyun zc->flagStaticHufTable = HUF_repeat_none;
289*4882a593Smuzhiyun ptr = ((U32 *)ptr) + 256; /* note : HUF_CElt* is incomplete type, size is simulated using U32 */
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun zc->nextToUpdate = 1;
292*4882a593Smuzhiyun zc->nextSrc = NULL;
293*4882a593Smuzhiyun zc->base = NULL;
294*4882a593Smuzhiyun zc->dictBase = NULL;
295*4882a593Smuzhiyun zc->dictLimit = 0;
296*4882a593Smuzhiyun zc->lowLimit = 0;
297*4882a593Smuzhiyun zc->params = params;
298*4882a593Smuzhiyun zc->blockSize = blockSize;
299*4882a593Smuzhiyun zc->frameContentSize = frameContentSize;
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun int i;
302*4882a593Smuzhiyun for (i = 0; i < ZSTD_REP_NUM; i++)
303*4882a593Smuzhiyun zc->rep[i] = repStartValue[i];
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun if ((params.cParams.strategy == ZSTD_btopt) || (params.cParams.strategy == ZSTD_btopt2)) {
307*4882a593Smuzhiyun zc->seqStore.litFreq = (U32 *)ptr;
308*4882a593Smuzhiyun zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1 << Litbits);
309*4882a593Smuzhiyun zc->seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (MaxLL + 1);
310*4882a593Smuzhiyun zc->seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (MaxML + 1);
311*4882a593Smuzhiyun ptr = zc->seqStore.offCodeFreq + (MaxOff + 1);
312*4882a593Smuzhiyun zc->seqStore.matchTable = (ZSTD_match_t *)ptr;
313*4882a593Smuzhiyun ptr = zc->seqStore.matchTable + ZSTD_OPT_NUM + 1;
314*4882a593Smuzhiyun zc->seqStore.priceTable = (ZSTD_optimal_t *)ptr;
315*4882a593Smuzhiyun ptr = zc->seqStore.priceTable + ZSTD_OPT_NUM + 1;
316*4882a593Smuzhiyun zc->seqStore.litLengthSum = 0;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun zc->seqStore.sequencesStart = (seqDef *)ptr;
319*4882a593Smuzhiyun ptr = zc->seqStore.sequencesStart + maxNbSeq;
320*4882a593Smuzhiyun zc->seqStore.llCode = (BYTE *)ptr;
321*4882a593Smuzhiyun zc->seqStore.mlCode = zc->seqStore.llCode + maxNbSeq;
322*4882a593Smuzhiyun zc->seqStore.ofCode = zc->seqStore.mlCode + maxNbSeq;
323*4882a593Smuzhiyun zc->seqStore.litStart = zc->seqStore.ofCode + maxNbSeq;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun zc->stage = ZSTDcs_init;
326*4882a593Smuzhiyun zc->dictID = 0;
327*4882a593Smuzhiyun zc->loadedDictEnd = 0;
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun return 0;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun /* ZSTD_invalidateRepCodes() :
334*4882a593Smuzhiyun * ensures next compression will not use repcodes from previous block.
335*4882a593Smuzhiyun * Note : only works with regular variant;
336*4882a593Smuzhiyun * do not use with extDict variant ! */
ZSTD_invalidateRepCodes(ZSTD_CCtx * cctx)337*4882a593Smuzhiyun void ZSTD_invalidateRepCodes(ZSTD_CCtx *cctx)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun int i;
340*4882a593Smuzhiyun for (i = 0; i < ZSTD_REP_NUM; i++)
341*4882a593Smuzhiyun cctx->rep[i] = 0;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /*! ZSTD_copyCCtx() :
345*4882a593Smuzhiyun * Duplicate an existing context `srcCCtx` into another one `dstCCtx`.
346*4882a593Smuzhiyun * Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()).
347*4882a593Smuzhiyun * @return : 0, or an error code */
ZSTD_copyCCtx(ZSTD_CCtx * dstCCtx,const ZSTD_CCtx * srcCCtx,unsigned long long pledgedSrcSize)348*4882a593Smuzhiyun size_t ZSTD_copyCCtx(ZSTD_CCtx *dstCCtx, const ZSTD_CCtx *srcCCtx, unsigned long long pledgedSrcSize)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun if (srcCCtx->stage != ZSTDcs_init)
351*4882a593Smuzhiyun return ERROR(stage_wrong);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun memcpy(&dstCCtx->customMem, &srcCCtx->customMem, sizeof(ZSTD_customMem));
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun ZSTD_parameters params = srcCCtx->params;
356*4882a593Smuzhiyun params.fParams.contentSizeFlag = (pledgedSrcSize > 0);
357*4882a593Smuzhiyun ZSTD_resetCCtx_advanced(dstCCtx, params, pledgedSrcSize, ZSTDcrp_noMemset);
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun /* copy tables */
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun size_t const chainSize = (srcCCtx->params.cParams.strategy == ZSTD_fast) ? 0 : (1 << srcCCtx->params.cParams.chainLog);
363*4882a593Smuzhiyun size_t const hSize = ((size_t)1) << srcCCtx->params.cParams.hashLog;
364*4882a593Smuzhiyun size_t const h3Size = (size_t)1 << srcCCtx->hashLog3;
365*4882a593Smuzhiyun size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32);
366*4882a593Smuzhiyun memcpy(dstCCtx->workSpace, srcCCtx->workSpace, tableSpace);
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun /* copy dictionary offsets */
370*4882a593Smuzhiyun dstCCtx->nextToUpdate = srcCCtx->nextToUpdate;
371*4882a593Smuzhiyun dstCCtx->nextToUpdate3 = srcCCtx->nextToUpdate3;
372*4882a593Smuzhiyun dstCCtx->nextSrc = srcCCtx->nextSrc;
373*4882a593Smuzhiyun dstCCtx->base = srcCCtx->base;
374*4882a593Smuzhiyun dstCCtx->dictBase = srcCCtx->dictBase;
375*4882a593Smuzhiyun dstCCtx->dictLimit = srcCCtx->dictLimit;
376*4882a593Smuzhiyun dstCCtx->lowLimit = srcCCtx->lowLimit;
377*4882a593Smuzhiyun dstCCtx->loadedDictEnd = srcCCtx->loadedDictEnd;
378*4882a593Smuzhiyun dstCCtx->dictID = srcCCtx->dictID;
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun /* copy entropy tables */
381*4882a593Smuzhiyun dstCCtx->flagStaticTables = srcCCtx->flagStaticTables;
382*4882a593Smuzhiyun dstCCtx->flagStaticHufTable = srcCCtx->flagStaticHufTable;
383*4882a593Smuzhiyun if (srcCCtx->flagStaticTables) {
384*4882a593Smuzhiyun memcpy(dstCCtx->litlengthCTable, srcCCtx->litlengthCTable, sizeof(dstCCtx->litlengthCTable));
385*4882a593Smuzhiyun memcpy(dstCCtx->matchlengthCTable, srcCCtx->matchlengthCTable, sizeof(dstCCtx->matchlengthCTable));
386*4882a593Smuzhiyun memcpy(dstCCtx->offcodeCTable, srcCCtx->offcodeCTable, sizeof(dstCCtx->offcodeCTable));
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun if (srcCCtx->flagStaticHufTable) {
389*4882a593Smuzhiyun memcpy(dstCCtx->hufTable, srcCCtx->hufTable, 256 * 4);
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun return 0;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun /*! ZSTD_reduceTable() :
396*4882a593Smuzhiyun * reduce table indexes by `reducerValue` */
ZSTD_reduceTable(U32 * const table,U32 const size,U32 const reducerValue)397*4882a593Smuzhiyun static void ZSTD_reduceTable(U32 *const table, U32 const size, U32 const reducerValue)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun U32 u;
400*4882a593Smuzhiyun for (u = 0; u < size; u++) {
401*4882a593Smuzhiyun if (table[u] < reducerValue)
402*4882a593Smuzhiyun table[u] = 0;
403*4882a593Smuzhiyun else
404*4882a593Smuzhiyun table[u] -= reducerValue;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun /*! ZSTD_reduceIndex() :
409*4882a593Smuzhiyun * rescale all indexes to avoid future overflow (indexes are U32) */
ZSTD_reduceIndex(ZSTD_CCtx * zc,const U32 reducerValue)410*4882a593Smuzhiyun static void ZSTD_reduceIndex(ZSTD_CCtx *zc, const U32 reducerValue)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun {
413*4882a593Smuzhiyun U32 const hSize = 1 << zc->params.cParams.hashLog;
414*4882a593Smuzhiyun ZSTD_reduceTable(zc->hashTable, hSize, reducerValue);
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun U32 const chainSize = (zc->params.cParams.strategy == ZSTD_fast) ? 0 : (1 << zc->params.cParams.chainLog);
419*4882a593Smuzhiyun ZSTD_reduceTable(zc->chainTable, chainSize, reducerValue);
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun U32 const h3Size = (zc->hashLog3) ? 1 << zc->hashLog3 : 0;
424*4882a593Smuzhiyun ZSTD_reduceTable(zc->hashTable3, h3Size, reducerValue);
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun /*-*******************************************************
429*4882a593Smuzhiyun * Block entropic compression
430*4882a593Smuzhiyun *********************************************************/
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun /* See doc/zstd_compression_format.md for detailed format description */
433*4882a593Smuzhiyun
ZSTD_noCompressBlock(void * dst,size_t dstCapacity,const void * src,size_t srcSize)434*4882a593Smuzhiyun size_t ZSTD_noCompressBlock(void *dst, size_t dstCapacity, const void *src, size_t srcSize)
435*4882a593Smuzhiyun {
436*4882a593Smuzhiyun if (srcSize + ZSTD_blockHeaderSize > dstCapacity)
437*4882a593Smuzhiyun return ERROR(dstSize_tooSmall);
438*4882a593Smuzhiyun memcpy((BYTE *)dst + ZSTD_blockHeaderSize, src, srcSize);
439*4882a593Smuzhiyun ZSTD_writeLE24(dst, (U32)(srcSize << 2) + (U32)bt_raw);
440*4882a593Smuzhiyun return ZSTD_blockHeaderSize + srcSize;
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun
ZSTD_noCompressLiterals(void * dst,size_t dstCapacity,const void * src,size_t srcSize)443*4882a593Smuzhiyun static size_t ZSTD_noCompressLiterals(void *dst, size_t dstCapacity, const void *src, size_t srcSize)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun BYTE *const ostart = (BYTE * const)dst;
446*4882a593Smuzhiyun U32 const flSize = 1 + (srcSize > 31) + (srcSize > 4095);
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun if (srcSize + flSize > dstCapacity)
449*4882a593Smuzhiyun return ERROR(dstSize_tooSmall);
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun switch (flSize) {
452*4882a593Smuzhiyun case 1: /* 2 - 1 - 5 */ ostart[0] = (BYTE)((U32)set_basic + (srcSize << 3)); break;
453*4882a593Smuzhiyun case 2: /* 2 - 2 - 12 */ ZSTD_writeLE16(ostart, (U16)((U32)set_basic + (1 << 2) + (srcSize << 4))); break;
454*4882a593Smuzhiyun default: /*note : should not be necessary : flSize is within {1,2,3} */
455*4882a593Smuzhiyun case 3: /* 2 - 2 - 20 */ ZSTD_writeLE32(ostart, (U32)((U32)set_basic + (3 << 2) + (srcSize << 4))); break;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun memcpy(ostart + flSize, src, srcSize);
459*4882a593Smuzhiyun return srcSize + flSize;
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun
ZSTD_compressRleLiteralsBlock(void * dst,size_t dstCapacity,const void * src,size_t srcSize)462*4882a593Smuzhiyun static size_t ZSTD_compressRleLiteralsBlock(void *dst, size_t dstCapacity, const void *src, size_t srcSize)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun BYTE *const ostart = (BYTE * const)dst;
465*4882a593Smuzhiyun U32 const flSize = 1 + (srcSize > 31) + (srcSize > 4095);
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun (void)dstCapacity; /* dstCapacity already guaranteed to be >=4, hence large enough */
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun switch (flSize) {
470*4882a593Smuzhiyun case 1: /* 2 - 1 - 5 */ ostart[0] = (BYTE)((U32)set_rle + (srcSize << 3)); break;
471*4882a593Smuzhiyun case 2: /* 2 - 2 - 12 */ ZSTD_writeLE16(ostart, (U16)((U32)set_rle + (1 << 2) + (srcSize << 4))); break;
472*4882a593Smuzhiyun default: /*note : should not be necessary : flSize is necessarily within {1,2,3} */
473*4882a593Smuzhiyun case 3: /* 2 - 2 - 20 */ ZSTD_writeLE32(ostart, (U32)((U32)set_rle + (3 << 2) + (srcSize << 4))); break;
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun ostart[flSize] = *(const BYTE *)src;
477*4882a593Smuzhiyun return flSize + 1;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun
ZSTD_minGain(size_t srcSize)480*4882a593Smuzhiyun static size_t ZSTD_minGain(size_t srcSize) { return (srcSize >> 6) + 2; }
481*4882a593Smuzhiyun
ZSTD_compressLiterals(ZSTD_CCtx * zc,void * dst,size_t dstCapacity,const void * src,size_t srcSize)482*4882a593Smuzhiyun static size_t ZSTD_compressLiterals(ZSTD_CCtx *zc, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun size_t const minGain = ZSTD_minGain(srcSize);
485*4882a593Smuzhiyun size_t const lhSize = 3 + (srcSize >= 1 KB) + (srcSize >= 16 KB);
486*4882a593Smuzhiyun BYTE *const ostart = (BYTE *)dst;
487*4882a593Smuzhiyun U32 singleStream = srcSize < 256;
488*4882a593Smuzhiyun symbolEncodingType_e hType = set_compressed;
489*4882a593Smuzhiyun size_t cLitSize;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun /* small ? don't even attempt compression (speed opt) */
492*4882a593Smuzhiyun #define LITERAL_NOENTROPY 63
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun size_t const minLitSize = zc->flagStaticHufTable == HUF_repeat_valid ? 6 : LITERAL_NOENTROPY;
495*4882a593Smuzhiyun if (srcSize <= minLitSize)
496*4882a593Smuzhiyun return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun if (dstCapacity < lhSize + 1)
500*4882a593Smuzhiyun return ERROR(dstSize_tooSmall); /* not enough space for compression */
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun HUF_repeat repeat = zc->flagStaticHufTable;
503*4882a593Smuzhiyun int const preferRepeat = zc->params.cParams.strategy < ZSTD_lazy ? srcSize <= 1024 : 0;
504*4882a593Smuzhiyun if (repeat == HUF_repeat_valid && lhSize == 3)
505*4882a593Smuzhiyun singleStream = 1;
506*4882a593Smuzhiyun cLitSize = singleStream ? HUF_compress1X_repeat(ostart + lhSize, dstCapacity - lhSize, src, srcSize, 255, 11, zc->tmpCounters,
507*4882a593Smuzhiyun sizeof(zc->tmpCounters), zc->hufTable, &repeat, preferRepeat)
508*4882a593Smuzhiyun : HUF_compress4X_repeat(ostart + lhSize, dstCapacity - lhSize, src, srcSize, 255, 11, zc->tmpCounters,
509*4882a593Smuzhiyun sizeof(zc->tmpCounters), zc->hufTable, &repeat, preferRepeat);
510*4882a593Smuzhiyun if (repeat != HUF_repeat_none) {
511*4882a593Smuzhiyun hType = set_repeat;
512*4882a593Smuzhiyun } /* reused the existing table */
513*4882a593Smuzhiyun else {
514*4882a593Smuzhiyun zc->flagStaticHufTable = HUF_repeat_check;
515*4882a593Smuzhiyun } /* now have a table to reuse */
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun if ((cLitSize == 0) | (cLitSize >= srcSize - minGain)) {
519*4882a593Smuzhiyun zc->flagStaticHufTable = HUF_repeat_none;
520*4882a593Smuzhiyun return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun if (cLitSize == 1) {
523*4882a593Smuzhiyun zc->flagStaticHufTable = HUF_repeat_none;
524*4882a593Smuzhiyun return ZSTD_compressRleLiteralsBlock(dst, dstCapacity, src, srcSize);
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun /* Build header */
528*4882a593Smuzhiyun switch (lhSize) {
529*4882a593Smuzhiyun case 3: /* 2 - 2 - 10 - 10 */
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun U32 const lhc = hType + ((!singleStream) << 2) + ((U32)srcSize << 4) + ((U32)cLitSize << 14);
532*4882a593Smuzhiyun ZSTD_writeLE24(ostart, lhc);
533*4882a593Smuzhiyun break;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun case 4: /* 2 - 2 - 14 - 14 */
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun U32 const lhc = hType + (2 << 2) + ((U32)srcSize << 4) + ((U32)cLitSize << 18);
538*4882a593Smuzhiyun ZSTD_writeLE32(ostart, lhc);
539*4882a593Smuzhiyun break;
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun default: /* should not be necessary, lhSize is only {3,4,5} */
542*4882a593Smuzhiyun case 5: /* 2 - 2 - 18 - 18 */
543*4882a593Smuzhiyun {
544*4882a593Smuzhiyun U32 const lhc = hType + (3 << 2) + ((U32)srcSize << 4) + ((U32)cLitSize << 22);
545*4882a593Smuzhiyun ZSTD_writeLE32(ostart, lhc);
546*4882a593Smuzhiyun ostart[4] = (BYTE)(cLitSize >> 10);
547*4882a593Smuzhiyun break;
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun return lhSize + cLitSize;
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun static const BYTE LL_Code[64] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16, 17, 17, 18, 18,
554*4882a593Smuzhiyun 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23,
555*4882a593Smuzhiyun 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24};
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun static const BYTE ML_Code[128] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
558*4882a593Smuzhiyun 26, 27, 28, 29, 30, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, 38, 38, 38, 38,
559*4882a593Smuzhiyun 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
560*4882a593Smuzhiyun 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42, 42, 42, 42, 42,
561*4882a593Smuzhiyun 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
562*4882a593Smuzhiyun
ZSTD_seqToCodes(const seqStore_t * seqStorePtr)563*4882a593Smuzhiyun void ZSTD_seqToCodes(const seqStore_t *seqStorePtr)
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun BYTE const LL_deltaCode = 19;
566*4882a593Smuzhiyun BYTE const ML_deltaCode = 36;
567*4882a593Smuzhiyun const seqDef *const sequences = seqStorePtr->sequencesStart;
568*4882a593Smuzhiyun BYTE *const llCodeTable = seqStorePtr->llCode;
569*4882a593Smuzhiyun BYTE *const ofCodeTable = seqStorePtr->ofCode;
570*4882a593Smuzhiyun BYTE *const mlCodeTable = seqStorePtr->mlCode;
571*4882a593Smuzhiyun U32 const nbSeq = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
572*4882a593Smuzhiyun U32 u;
573*4882a593Smuzhiyun for (u = 0; u < nbSeq; u++) {
574*4882a593Smuzhiyun U32 const llv = sequences[u].litLength;
575*4882a593Smuzhiyun U32 const mlv = sequences[u].matchLength;
576*4882a593Smuzhiyun llCodeTable[u] = (llv > 63) ? (BYTE)ZSTD_highbit32(llv) + LL_deltaCode : LL_Code[llv];
577*4882a593Smuzhiyun ofCodeTable[u] = (BYTE)ZSTD_highbit32(sequences[u].offset);
578*4882a593Smuzhiyun mlCodeTable[u] = (mlv > 127) ? (BYTE)ZSTD_highbit32(mlv) + ML_deltaCode : ML_Code[mlv];
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun if (seqStorePtr->longLengthID == 1)
581*4882a593Smuzhiyun llCodeTable[seqStorePtr->longLengthPos] = MaxLL;
582*4882a593Smuzhiyun if (seqStorePtr->longLengthID == 2)
583*4882a593Smuzhiyun mlCodeTable[seqStorePtr->longLengthPos] = MaxML;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun
ZSTD_compressSequences_internal(ZSTD_CCtx * zc,void * dst,size_t dstCapacity)586*4882a593Smuzhiyun ZSTD_STATIC size_t ZSTD_compressSequences_internal(ZSTD_CCtx *zc, void *dst, size_t dstCapacity)
587*4882a593Smuzhiyun {
588*4882a593Smuzhiyun const int longOffsets = zc->params.cParams.windowLog > STREAM_ACCUMULATOR_MIN;
589*4882a593Smuzhiyun const seqStore_t *seqStorePtr = &(zc->seqStore);
590*4882a593Smuzhiyun FSE_CTable *CTable_LitLength = zc->litlengthCTable;
591*4882a593Smuzhiyun FSE_CTable *CTable_OffsetBits = zc->offcodeCTable;
592*4882a593Smuzhiyun FSE_CTable *CTable_MatchLength = zc->matchlengthCTable;
593*4882a593Smuzhiyun U32 LLtype, Offtype, MLtype; /* compressed, raw or rle */
594*4882a593Smuzhiyun const seqDef *const sequences = seqStorePtr->sequencesStart;
595*4882a593Smuzhiyun const BYTE *const ofCodeTable = seqStorePtr->ofCode;
596*4882a593Smuzhiyun const BYTE *const llCodeTable = seqStorePtr->llCode;
597*4882a593Smuzhiyun const BYTE *const mlCodeTable = seqStorePtr->mlCode;
598*4882a593Smuzhiyun BYTE *const ostart = (BYTE *)dst;
599*4882a593Smuzhiyun BYTE *const oend = ostart + dstCapacity;
600*4882a593Smuzhiyun BYTE *op = ostart;
601*4882a593Smuzhiyun size_t const nbSeq = seqStorePtr->sequences - seqStorePtr->sequencesStart;
602*4882a593Smuzhiyun BYTE *seqHead;
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun U32 *count;
605*4882a593Smuzhiyun S16 *norm;
606*4882a593Smuzhiyun U32 *workspace;
607*4882a593Smuzhiyun size_t workspaceSize = sizeof(zc->tmpCounters);
608*4882a593Smuzhiyun {
609*4882a593Smuzhiyun size_t spaceUsed32 = 0;
610*4882a593Smuzhiyun count = (U32 *)zc->tmpCounters + spaceUsed32;
611*4882a593Smuzhiyun spaceUsed32 += MaxSeq + 1;
612*4882a593Smuzhiyun norm = (S16 *)((U32 *)zc->tmpCounters + spaceUsed32);
613*4882a593Smuzhiyun spaceUsed32 += ALIGN(sizeof(S16) * (MaxSeq + 1), sizeof(U32)) >> 2;
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun workspace = (U32 *)zc->tmpCounters + spaceUsed32;
616*4882a593Smuzhiyun workspaceSize -= (spaceUsed32 << 2);
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun /* Compress literals */
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun const BYTE *const literals = seqStorePtr->litStart;
622*4882a593Smuzhiyun size_t const litSize = seqStorePtr->lit - literals;
623*4882a593Smuzhiyun size_t const cSize = ZSTD_compressLiterals(zc, op, dstCapacity, literals, litSize);
624*4882a593Smuzhiyun if (ZSTD_isError(cSize))
625*4882a593Smuzhiyun return cSize;
626*4882a593Smuzhiyun op += cSize;
627*4882a593Smuzhiyun }
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun /* Sequences Header */
630*4882a593Smuzhiyun if ((oend - op) < 3 /*max nbSeq Size*/ + 1 /*seqHead */)
631*4882a593Smuzhiyun return ERROR(dstSize_tooSmall);
632*4882a593Smuzhiyun if (nbSeq < 0x7F)
633*4882a593Smuzhiyun *op++ = (BYTE)nbSeq;
634*4882a593Smuzhiyun else if (nbSeq < LONGNBSEQ)
635*4882a593Smuzhiyun op[0] = (BYTE)((nbSeq >> 8) + 0x80), op[1] = (BYTE)nbSeq, op += 2;
636*4882a593Smuzhiyun else
637*4882a593Smuzhiyun op[0] = 0xFF, ZSTD_writeLE16(op + 1, (U16)(nbSeq - LONGNBSEQ)), op += 3;
638*4882a593Smuzhiyun if (nbSeq == 0)
639*4882a593Smuzhiyun return op - ostart;
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun /* seqHead : flags for FSE encoding type */
642*4882a593Smuzhiyun seqHead = op++;
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun #define MIN_SEQ_FOR_DYNAMIC_FSE 64
645*4882a593Smuzhiyun #define MAX_SEQ_FOR_STATIC_FSE 1000
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun /* convert length/distances into codes */
648*4882a593Smuzhiyun ZSTD_seqToCodes(seqStorePtr);
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun /* CTable for Literal Lengths */
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun U32 max = MaxLL;
653*4882a593Smuzhiyun size_t const mostFrequent = FSE_countFast_wksp(count, &max, llCodeTable, nbSeq, workspace);
654*4882a593Smuzhiyun if ((mostFrequent == nbSeq) && (nbSeq > 2)) {
655*4882a593Smuzhiyun *op++ = llCodeTable[0];
656*4882a593Smuzhiyun FSE_buildCTable_rle(CTable_LitLength, (BYTE)max);
657*4882a593Smuzhiyun LLtype = set_rle;
658*4882a593Smuzhiyun } else if ((zc->flagStaticTables) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) {
659*4882a593Smuzhiyun LLtype = set_repeat;
660*4882a593Smuzhiyun } else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (LL_defaultNormLog - 1)))) {
661*4882a593Smuzhiyun FSE_buildCTable_wksp(CTable_LitLength, LL_defaultNorm, MaxLL, LL_defaultNormLog, workspace, workspaceSize);
662*4882a593Smuzhiyun LLtype = set_basic;
663*4882a593Smuzhiyun } else {
664*4882a593Smuzhiyun size_t nbSeq_1 = nbSeq;
665*4882a593Smuzhiyun const U32 tableLog = FSE_optimalTableLog(LLFSELog, nbSeq, max);
666*4882a593Smuzhiyun if (count[llCodeTable[nbSeq - 1]] > 1) {
667*4882a593Smuzhiyun count[llCodeTable[nbSeq - 1]]--;
668*4882a593Smuzhiyun nbSeq_1--;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max);
671*4882a593Smuzhiyun {
672*4882a593Smuzhiyun size_t const NCountSize = FSE_writeNCount(op, oend - op, norm, max, tableLog); /* overflow protected */
673*4882a593Smuzhiyun if (FSE_isError(NCountSize))
674*4882a593Smuzhiyun return NCountSize;
675*4882a593Smuzhiyun op += NCountSize;
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun FSE_buildCTable_wksp(CTable_LitLength, norm, max, tableLog, workspace, workspaceSize);
678*4882a593Smuzhiyun LLtype = set_compressed;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun /* CTable for Offsets */
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun U32 max = MaxOff;
685*4882a593Smuzhiyun size_t const mostFrequent = FSE_countFast_wksp(count, &max, ofCodeTable, nbSeq, workspace);
686*4882a593Smuzhiyun if ((mostFrequent == nbSeq) && (nbSeq > 2)) {
687*4882a593Smuzhiyun *op++ = ofCodeTable[0];
688*4882a593Smuzhiyun FSE_buildCTable_rle(CTable_OffsetBits, (BYTE)max);
689*4882a593Smuzhiyun Offtype = set_rle;
690*4882a593Smuzhiyun } else if ((zc->flagStaticTables) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) {
691*4882a593Smuzhiyun Offtype = set_repeat;
692*4882a593Smuzhiyun } else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (OF_defaultNormLog - 1)))) {
693*4882a593Smuzhiyun FSE_buildCTable_wksp(CTable_OffsetBits, OF_defaultNorm, MaxOff, OF_defaultNormLog, workspace, workspaceSize);
694*4882a593Smuzhiyun Offtype = set_basic;
695*4882a593Smuzhiyun } else {
696*4882a593Smuzhiyun size_t nbSeq_1 = nbSeq;
697*4882a593Smuzhiyun const U32 tableLog = FSE_optimalTableLog(OffFSELog, nbSeq, max);
698*4882a593Smuzhiyun if (count[ofCodeTable[nbSeq - 1]] > 1) {
699*4882a593Smuzhiyun count[ofCodeTable[nbSeq - 1]]--;
700*4882a593Smuzhiyun nbSeq_1--;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max);
703*4882a593Smuzhiyun {
704*4882a593Smuzhiyun size_t const NCountSize = FSE_writeNCount(op, oend - op, norm, max, tableLog); /* overflow protected */
705*4882a593Smuzhiyun if (FSE_isError(NCountSize))
706*4882a593Smuzhiyun return NCountSize;
707*4882a593Smuzhiyun op += NCountSize;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun FSE_buildCTable_wksp(CTable_OffsetBits, norm, max, tableLog, workspace, workspaceSize);
710*4882a593Smuzhiyun Offtype = set_compressed;
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun /* CTable for MatchLengths */
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun U32 max = MaxML;
717*4882a593Smuzhiyun size_t const mostFrequent = FSE_countFast_wksp(count, &max, mlCodeTable, nbSeq, workspace);
718*4882a593Smuzhiyun if ((mostFrequent == nbSeq) && (nbSeq > 2)) {
719*4882a593Smuzhiyun *op++ = *mlCodeTable;
720*4882a593Smuzhiyun FSE_buildCTable_rle(CTable_MatchLength, (BYTE)max);
721*4882a593Smuzhiyun MLtype = set_rle;
722*4882a593Smuzhiyun } else if ((zc->flagStaticTables) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) {
723*4882a593Smuzhiyun MLtype = set_repeat;
724*4882a593Smuzhiyun } else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (ML_defaultNormLog - 1)))) {
725*4882a593Smuzhiyun FSE_buildCTable_wksp(CTable_MatchLength, ML_defaultNorm, MaxML, ML_defaultNormLog, workspace, workspaceSize);
726*4882a593Smuzhiyun MLtype = set_basic;
727*4882a593Smuzhiyun } else {
728*4882a593Smuzhiyun size_t nbSeq_1 = nbSeq;
729*4882a593Smuzhiyun const U32 tableLog = FSE_optimalTableLog(MLFSELog, nbSeq, max);
730*4882a593Smuzhiyun if (count[mlCodeTable[nbSeq - 1]] > 1) {
731*4882a593Smuzhiyun count[mlCodeTable[nbSeq - 1]]--;
732*4882a593Smuzhiyun nbSeq_1--;
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max);
735*4882a593Smuzhiyun {
736*4882a593Smuzhiyun size_t const NCountSize = FSE_writeNCount(op, oend - op, norm, max, tableLog); /* overflow protected */
737*4882a593Smuzhiyun if (FSE_isError(NCountSize))
738*4882a593Smuzhiyun return NCountSize;
739*4882a593Smuzhiyun op += NCountSize;
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun FSE_buildCTable_wksp(CTable_MatchLength, norm, max, tableLog, workspace, workspaceSize);
742*4882a593Smuzhiyun MLtype = set_compressed;
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun *seqHead = (BYTE)((LLtype << 6) + (Offtype << 4) + (MLtype << 2));
747*4882a593Smuzhiyun zc->flagStaticTables = 0;
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun /* Encoding Sequences */
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun BIT_CStream_t blockStream;
752*4882a593Smuzhiyun FSE_CState_t stateMatchLength;
753*4882a593Smuzhiyun FSE_CState_t stateOffsetBits;
754*4882a593Smuzhiyun FSE_CState_t stateLitLength;
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun CHECK_E(BIT_initCStream(&blockStream, op, oend - op), dstSize_tooSmall); /* not enough space remaining */
757*4882a593Smuzhiyun
758*4882a593Smuzhiyun /* first symbols */
759*4882a593Smuzhiyun FSE_initCState2(&stateMatchLength, CTable_MatchLength, mlCodeTable[nbSeq - 1]);
760*4882a593Smuzhiyun FSE_initCState2(&stateOffsetBits, CTable_OffsetBits, ofCodeTable[nbSeq - 1]);
761*4882a593Smuzhiyun FSE_initCState2(&stateLitLength, CTable_LitLength, llCodeTable[nbSeq - 1]);
762*4882a593Smuzhiyun BIT_addBits(&blockStream, sequences[nbSeq - 1].litLength, LL_bits[llCodeTable[nbSeq - 1]]);
763*4882a593Smuzhiyun if (ZSTD_32bits())
764*4882a593Smuzhiyun BIT_flushBits(&blockStream);
765*4882a593Smuzhiyun BIT_addBits(&blockStream, sequences[nbSeq - 1].matchLength, ML_bits[mlCodeTable[nbSeq - 1]]);
766*4882a593Smuzhiyun if (ZSTD_32bits())
767*4882a593Smuzhiyun BIT_flushBits(&blockStream);
768*4882a593Smuzhiyun if (longOffsets) {
769*4882a593Smuzhiyun U32 const ofBits = ofCodeTable[nbSeq - 1];
770*4882a593Smuzhiyun int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN - 1);
771*4882a593Smuzhiyun if (extraBits) {
772*4882a593Smuzhiyun BIT_addBits(&blockStream, sequences[nbSeq - 1].offset, extraBits);
773*4882a593Smuzhiyun BIT_flushBits(&blockStream);
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun BIT_addBits(&blockStream, sequences[nbSeq - 1].offset >> extraBits, ofBits - extraBits);
776*4882a593Smuzhiyun } else {
777*4882a593Smuzhiyun BIT_addBits(&blockStream, sequences[nbSeq - 1].offset, ofCodeTable[nbSeq - 1]);
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun BIT_flushBits(&blockStream);
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun {
782*4882a593Smuzhiyun size_t n;
783*4882a593Smuzhiyun for (n = nbSeq - 2; n < nbSeq; n--) { /* intentional underflow */
784*4882a593Smuzhiyun BYTE const llCode = llCodeTable[n];
785*4882a593Smuzhiyun BYTE const ofCode = ofCodeTable[n];
786*4882a593Smuzhiyun BYTE const mlCode = mlCodeTable[n];
787*4882a593Smuzhiyun U32 const llBits = LL_bits[llCode];
788*4882a593Smuzhiyun U32 const ofBits = ofCode; /* 32b*/ /* 64b*/
789*4882a593Smuzhiyun U32 const mlBits = ML_bits[mlCode];
790*4882a593Smuzhiyun /* (7)*/ /* (7)*/
791*4882a593Smuzhiyun FSE_encodeSymbol(&blockStream, &stateOffsetBits, ofCode); /* 15 */ /* 15 */
792*4882a593Smuzhiyun FSE_encodeSymbol(&blockStream, &stateMatchLength, mlCode); /* 24 */ /* 24 */
793*4882a593Smuzhiyun if (ZSTD_32bits())
794*4882a593Smuzhiyun BIT_flushBits(&blockStream); /* (7)*/
795*4882a593Smuzhiyun FSE_encodeSymbol(&blockStream, &stateLitLength, llCode); /* 16 */ /* 33 */
796*4882a593Smuzhiyun if (ZSTD_32bits() || (ofBits + mlBits + llBits >= 64 - 7 - (LLFSELog + MLFSELog + OffFSELog)))
797*4882a593Smuzhiyun BIT_flushBits(&blockStream); /* (7)*/
798*4882a593Smuzhiyun BIT_addBits(&blockStream, sequences[n].litLength, llBits);
799*4882a593Smuzhiyun if (ZSTD_32bits() && ((llBits + mlBits) > 24))
800*4882a593Smuzhiyun BIT_flushBits(&blockStream);
801*4882a593Smuzhiyun BIT_addBits(&blockStream, sequences[n].matchLength, mlBits);
802*4882a593Smuzhiyun if (ZSTD_32bits())
803*4882a593Smuzhiyun BIT_flushBits(&blockStream); /* (7)*/
804*4882a593Smuzhiyun if (longOffsets) {
805*4882a593Smuzhiyun int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN - 1);
806*4882a593Smuzhiyun if (extraBits) {
807*4882a593Smuzhiyun BIT_addBits(&blockStream, sequences[n].offset, extraBits);
808*4882a593Smuzhiyun BIT_flushBits(&blockStream); /* (7)*/
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun BIT_addBits(&blockStream, sequences[n].offset >> extraBits, ofBits - extraBits); /* 31 */
811*4882a593Smuzhiyun } else {
812*4882a593Smuzhiyun BIT_addBits(&blockStream, sequences[n].offset, ofBits); /* 31 */
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun BIT_flushBits(&blockStream); /* (7)*/
815*4882a593Smuzhiyun }
816*4882a593Smuzhiyun }
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun FSE_flushCState(&blockStream, &stateMatchLength);
819*4882a593Smuzhiyun FSE_flushCState(&blockStream, &stateOffsetBits);
820*4882a593Smuzhiyun FSE_flushCState(&blockStream, &stateLitLength);
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun {
823*4882a593Smuzhiyun size_t const streamSize = BIT_closeCStream(&blockStream);
824*4882a593Smuzhiyun if (streamSize == 0)
825*4882a593Smuzhiyun return ERROR(dstSize_tooSmall); /* not enough space */
826*4882a593Smuzhiyun op += streamSize;
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun return op - ostart;
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun
ZSTD_compressSequences(ZSTD_CCtx * zc,void * dst,size_t dstCapacity,size_t srcSize)832*4882a593Smuzhiyun ZSTD_STATIC size_t ZSTD_compressSequences(ZSTD_CCtx *zc, void *dst, size_t dstCapacity, size_t srcSize)
833*4882a593Smuzhiyun {
834*4882a593Smuzhiyun size_t const cSize = ZSTD_compressSequences_internal(zc, dst, dstCapacity);
835*4882a593Smuzhiyun size_t const minGain = ZSTD_minGain(srcSize);
836*4882a593Smuzhiyun size_t const maxCSize = srcSize - minGain;
837*4882a593Smuzhiyun /* If the srcSize <= dstCapacity, then there is enough space to write a
838*4882a593Smuzhiyun * raw uncompressed block. Since we ran out of space, the block must not
839*4882a593Smuzhiyun * be compressible, so fall back to a raw uncompressed block.
840*4882a593Smuzhiyun */
841*4882a593Smuzhiyun int const uncompressibleError = cSize == ERROR(dstSize_tooSmall) && srcSize <= dstCapacity;
842*4882a593Smuzhiyun int i;
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun if (ZSTD_isError(cSize) && !uncompressibleError)
845*4882a593Smuzhiyun return cSize;
846*4882a593Smuzhiyun if (cSize >= maxCSize || uncompressibleError) {
847*4882a593Smuzhiyun zc->flagStaticHufTable = HUF_repeat_none;
848*4882a593Smuzhiyun return 0;
849*4882a593Smuzhiyun }
850*4882a593Smuzhiyun /* confirm repcodes */
851*4882a593Smuzhiyun for (i = 0; i < ZSTD_REP_NUM; i++)
852*4882a593Smuzhiyun zc->rep[i] = zc->repToConfirm[i];
853*4882a593Smuzhiyun return cSize;
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun /*! ZSTD_storeSeq() :
857*4882a593Smuzhiyun Store a sequence (literal length, literals, offset code and match length code) into seqStore_t.
858*4882a593Smuzhiyun `offsetCode` : distance to match, or 0 == repCode.
859*4882a593Smuzhiyun `matchCode` : matchLength - MINMATCH
860*4882a593Smuzhiyun */
ZSTD_storeSeq(seqStore_t * seqStorePtr,size_t litLength,const void * literals,U32 offsetCode,size_t matchCode)861*4882a593Smuzhiyun ZSTD_STATIC void ZSTD_storeSeq(seqStore_t *seqStorePtr, size_t litLength, const void *literals, U32 offsetCode, size_t matchCode)
862*4882a593Smuzhiyun {
863*4882a593Smuzhiyun /* copy Literals */
864*4882a593Smuzhiyun ZSTD_wildcopy(seqStorePtr->lit, literals, litLength);
865*4882a593Smuzhiyun seqStorePtr->lit += litLength;
866*4882a593Smuzhiyun
867*4882a593Smuzhiyun /* literal Length */
868*4882a593Smuzhiyun if (litLength > 0xFFFF) {
869*4882a593Smuzhiyun seqStorePtr->longLengthID = 1;
870*4882a593Smuzhiyun seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun seqStorePtr->sequences[0].litLength = (U16)litLength;
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun /* match offset */
875*4882a593Smuzhiyun seqStorePtr->sequences[0].offset = offsetCode + 1;
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun /* match Length */
878*4882a593Smuzhiyun if (matchCode > 0xFFFF) {
879*4882a593Smuzhiyun seqStorePtr->longLengthID = 2;
880*4882a593Smuzhiyun seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun seqStorePtr->sequences[0].matchLength = (U16)matchCode;
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun seqStorePtr->sequences++;
885*4882a593Smuzhiyun }
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun /*-*************************************
888*4882a593Smuzhiyun * Match length counter
889*4882a593Smuzhiyun ***************************************/
ZSTD_NbCommonBytes(register size_t val)890*4882a593Smuzhiyun static unsigned ZSTD_NbCommonBytes(register size_t val)
891*4882a593Smuzhiyun {
892*4882a593Smuzhiyun if (ZSTD_isLittleEndian()) {
893*4882a593Smuzhiyun if (ZSTD_64bits()) {
894*4882a593Smuzhiyun return (__builtin_ctzll((U64)val) >> 3);
895*4882a593Smuzhiyun } else { /* 32 bits */
896*4882a593Smuzhiyun return (__builtin_ctz((U32)val) >> 3);
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun } else { /* Big Endian CPU */
899*4882a593Smuzhiyun if (ZSTD_64bits()) {
900*4882a593Smuzhiyun return (__builtin_clzll(val) >> 3);
901*4882a593Smuzhiyun } else { /* 32 bits */
902*4882a593Smuzhiyun return (__builtin_clz((U32)val) >> 3);
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun }
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun
ZSTD_count(const BYTE * pIn,const BYTE * pMatch,const BYTE * const pInLimit)907*4882a593Smuzhiyun static size_t ZSTD_count(const BYTE *pIn, const BYTE *pMatch, const BYTE *const pInLimit)
908*4882a593Smuzhiyun {
909*4882a593Smuzhiyun const BYTE *const pStart = pIn;
910*4882a593Smuzhiyun const BYTE *const pInLoopLimit = pInLimit - (sizeof(size_t) - 1);
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun while (pIn < pInLoopLimit) {
913*4882a593Smuzhiyun size_t const diff = ZSTD_readST(pMatch) ^ ZSTD_readST(pIn);
914*4882a593Smuzhiyun if (!diff) {
915*4882a593Smuzhiyun pIn += sizeof(size_t);
916*4882a593Smuzhiyun pMatch += sizeof(size_t);
917*4882a593Smuzhiyun continue;
918*4882a593Smuzhiyun }
919*4882a593Smuzhiyun pIn += ZSTD_NbCommonBytes(diff);
920*4882a593Smuzhiyun return (size_t)(pIn - pStart);
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun if (ZSTD_64bits())
923*4882a593Smuzhiyun if ((pIn < (pInLimit - 3)) && (ZSTD_read32(pMatch) == ZSTD_read32(pIn))) {
924*4882a593Smuzhiyun pIn += 4;
925*4882a593Smuzhiyun pMatch += 4;
926*4882a593Smuzhiyun }
927*4882a593Smuzhiyun if ((pIn < (pInLimit - 1)) && (ZSTD_read16(pMatch) == ZSTD_read16(pIn))) {
928*4882a593Smuzhiyun pIn += 2;
929*4882a593Smuzhiyun pMatch += 2;
930*4882a593Smuzhiyun }
931*4882a593Smuzhiyun if ((pIn < pInLimit) && (*pMatch == *pIn))
932*4882a593Smuzhiyun pIn++;
933*4882a593Smuzhiyun return (size_t)(pIn - pStart);
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun /** ZSTD_count_2segments() :
937*4882a593Smuzhiyun * can count match length with `ip` & `match` in 2 different segments.
938*4882a593Smuzhiyun * convention : on reaching mEnd, match count continue starting from iStart
939*4882a593Smuzhiyun */
ZSTD_count_2segments(const BYTE * ip,const BYTE * match,const BYTE * iEnd,const BYTE * mEnd,const BYTE * iStart)940*4882a593Smuzhiyun static size_t ZSTD_count_2segments(const BYTE *ip, const BYTE *match, const BYTE *iEnd, const BYTE *mEnd, const BYTE *iStart)
941*4882a593Smuzhiyun {
942*4882a593Smuzhiyun const BYTE *const vEnd = MIN(ip + (mEnd - match), iEnd);
943*4882a593Smuzhiyun size_t const matchLength = ZSTD_count(ip, match, vEnd);
944*4882a593Smuzhiyun if (match + matchLength != mEnd)
945*4882a593Smuzhiyun return matchLength;
946*4882a593Smuzhiyun return matchLength + ZSTD_count(ip + matchLength, iStart, iEnd);
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun
949*4882a593Smuzhiyun /*-*************************************
950*4882a593Smuzhiyun * Hashes
951*4882a593Smuzhiyun ***************************************/
952*4882a593Smuzhiyun static const U32 prime3bytes = 506832829U;
ZSTD_hash3(U32 u,U32 h)953*4882a593Smuzhiyun static U32 ZSTD_hash3(U32 u, U32 h) { return ((u << (32 - 24)) * prime3bytes) >> (32 - h); }
ZSTD_hash3Ptr(const void * ptr,U32 h)954*4882a593Smuzhiyun ZSTD_STATIC size_t ZSTD_hash3Ptr(const void *ptr, U32 h) { return ZSTD_hash3(ZSTD_readLE32(ptr), h); } /* only in zstd_opt.h */
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun static const U32 prime4bytes = 2654435761U;
ZSTD_hash4(U32 u,U32 h)957*4882a593Smuzhiyun static U32 ZSTD_hash4(U32 u, U32 h) { return (u * prime4bytes) >> (32 - h); }
ZSTD_hash4Ptr(const void * ptr,U32 h)958*4882a593Smuzhiyun static size_t ZSTD_hash4Ptr(const void *ptr, U32 h) { return ZSTD_hash4(ZSTD_read32(ptr), h); }
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun static const U64 prime5bytes = 889523592379ULL;
ZSTD_hash5(U64 u,U32 h)961*4882a593Smuzhiyun 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)962*4882a593Smuzhiyun static size_t ZSTD_hash5Ptr(const void *p, U32 h) { return ZSTD_hash5(ZSTD_readLE64(p), h); }
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun static const U64 prime6bytes = 227718039650203ULL;
ZSTD_hash6(U64 u,U32 h)965*4882a593Smuzhiyun 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)966*4882a593Smuzhiyun static size_t ZSTD_hash6Ptr(const void *p, U32 h) { return ZSTD_hash6(ZSTD_readLE64(p), h); }
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun static const U64 prime7bytes = 58295818150454627ULL;
ZSTD_hash7(U64 u,U32 h)969*4882a593Smuzhiyun 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)970*4882a593Smuzhiyun static size_t ZSTD_hash7Ptr(const void *p, U32 h) { return ZSTD_hash7(ZSTD_readLE64(p), h); }
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun static const U64 prime8bytes = 0xCF1BBCDCB7A56463ULL;
ZSTD_hash8(U64 u,U32 h)973*4882a593Smuzhiyun static size_t ZSTD_hash8(U64 u, U32 h) { return (size_t)(((u)*prime8bytes) >> (64 - h)); }
ZSTD_hash8Ptr(const void * p,U32 h)974*4882a593Smuzhiyun static size_t ZSTD_hash8Ptr(const void *p, U32 h) { return ZSTD_hash8(ZSTD_readLE64(p), h); }
975*4882a593Smuzhiyun
ZSTD_hashPtr(const void * p,U32 hBits,U32 mls)976*4882a593Smuzhiyun static size_t ZSTD_hashPtr(const void *p, U32 hBits, U32 mls)
977*4882a593Smuzhiyun {
978*4882a593Smuzhiyun switch (mls) {
979*4882a593Smuzhiyun // case 3: return ZSTD_hash3Ptr(p, hBits);
980*4882a593Smuzhiyun default:
981*4882a593Smuzhiyun case 4: return ZSTD_hash4Ptr(p, hBits);
982*4882a593Smuzhiyun case 5: return ZSTD_hash5Ptr(p, hBits);
983*4882a593Smuzhiyun case 6: return ZSTD_hash6Ptr(p, hBits);
984*4882a593Smuzhiyun case 7: return ZSTD_hash7Ptr(p, hBits);
985*4882a593Smuzhiyun case 8: return ZSTD_hash8Ptr(p, hBits);
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun /*-*************************************
990*4882a593Smuzhiyun * Fast Scan
991*4882a593Smuzhiyun ***************************************/
ZSTD_fillHashTable(ZSTD_CCtx * zc,const void * end,const U32 mls)992*4882a593Smuzhiyun static void ZSTD_fillHashTable(ZSTD_CCtx *zc, const void *end, const U32 mls)
993*4882a593Smuzhiyun {
994*4882a593Smuzhiyun U32 *const hashTable = zc->hashTable;
995*4882a593Smuzhiyun U32 const hBits = zc->params.cParams.hashLog;
996*4882a593Smuzhiyun const BYTE *const base = zc->base;
997*4882a593Smuzhiyun const BYTE *ip = base + zc->nextToUpdate;
998*4882a593Smuzhiyun const BYTE *const iend = ((const BYTE *)end) - HASH_READ_SIZE;
999*4882a593Smuzhiyun const size_t fastHashFillStep = 3;
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun while (ip <= iend) {
1002*4882a593Smuzhiyun hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip - base);
1003*4882a593Smuzhiyun ip += fastHashFillStep;
1004*4882a593Smuzhiyun }
1005*4882a593Smuzhiyun }
1006*4882a593Smuzhiyun
1007*4882a593Smuzhiyun FORCE_INLINE
ZSTD_compressBlock_fast_generic(ZSTD_CCtx * cctx,const void * src,size_t srcSize,const U32 mls)1008*4882a593Smuzhiyun void ZSTD_compressBlock_fast_generic(ZSTD_CCtx *cctx, const void *src, size_t srcSize, const U32 mls)
1009*4882a593Smuzhiyun {
1010*4882a593Smuzhiyun U32 *const hashTable = cctx->hashTable;
1011*4882a593Smuzhiyun U32 const hBits = cctx->params.cParams.hashLog;
1012*4882a593Smuzhiyun seqStore_t *seqStorePtr = &(cctx->seqStore);
1013*4882a593Smuzhiyun const BYTE *const base = cctx->base;
1014*4882a593Smuzhiyun const BYTE *const istart = (const BYTE *)src;
1015*4882a593Smuzhiyun const BYTE *ip = istart;
1016*4882a593Smuzhiyun const BYTE *anchor = istart;
1017*4882a593Smuzhiyun const U32 lowestIndex = cctx->dictLimit;
1018*4882a593Smuzhiyun const BYTE *const lowest = base + lowestIndex;
1019*4882a593Smuzhiyun const BYTE *const iend = istart + srcSize;
1020*4882a593Smuzhiyun const BYTE *const ilimit = iend - HASH_READ_SIZE;
1021*4882a593Smuzhiyun U32 offset_1 = cctx->rep[0], offset_2 = cctx->rep[1];
1022*4882a593Smuzhiyun U32 offsetSaved = 0;
1023*4882a593Smuzhiyun
1024*4882a593Smuzhiyun /* init */
1025*4882a593Smuzhiyun ip += (ip == lowest);
1026*4882a593Smuzhiyun {
1027*4882a593Smuzhiyun U32 const maxRep = (U32)(ip - lowest);
1028*4882a593Smuzhiyun if (offset_2 > maxRep)
1029*4882a593Smuzhiyun offsetSaved = offset_2, offset_2 = 0;
1030*4882a593Smuzhiyun if (offset_1 > maxRep)
1031*4882a593Smuzhiyun offsetSaved = offset_1, offset_1 = 0;
1032*4882a593Smuzhiyun }
1033*4882a593Smuzhiyun
1034*4882a593Smuzhiyun /* Main Search Loop */
1035*4882a593Smuzhiyun while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
1036*4882a593Smuzhiyun size_t mLength;
1037*4882a593Smuzhiyun size_t const h = ZSTD_hashPtr(ip, hBits, mls);
1038*4882a593Smuzhiyun U32 const curr = (U32)(ip - base);
1039*4882a593Smuzhiyun U32 const matchIndex = hashTable[h];
1040*4882a593Smuzhiyun const BYTE *match = base + matchIndex;
1041*4882a593Smuzhiyun hashTable[h] = curr; /* update hash table */
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun if ((offset_1 > 0) & (ZSTD_read32(ip + 1 - offset_1) == ZSTD_read32(ip + 1))) {
1044*4882a593Smuzhiyun mLength = ZSTD_count(ip + 1 + 4, ip + 1 + 4 - offset_1, iend) + 4;
1045*4882a593Smuzhiyun ip++;
1046*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, 0, mLength - MINMATCH);
1047*4882a593Smuzhiyun } else {
1048*4882a593Smuzhiyun U32 offset;
1049*4882a593Smuzhiyun if ((matchIndex <= lowestIndex) || (ZSTD_read32(match) != ZSTD_read32(ip))) {
1050*4882a593Smuzhiyun ip += ((ip - anchor) >> g_searchStrength) + 1;
1051*4882a593Smuzhiyun continue;
1052*4882a593Smuzhiyun }
1053*4882a593Smuzhiyun mLength = ZSTD_count(ip + 4, match + 4, iend) + 4;
1054*4882a593Smuzhiyun offset = (U32)(ip - match);
1055*4882a593Smuzhiyun while (((ip > anchor) & (match > lowest)) && (ip[-1] == match[-1])) {
1056*4882a593Smuzhiyun ip--;
1057*4882a593Smuzhiyun match--;
1058*4882a593Smuzhiyun mLength++;
1059*4882a593Smuzhiyun } /* catch up */
1060*4882a593Smuzhiyun offset_2 = offset_1;
1061*4882a593Smuzhiyun offset_1 = offset;
1062*4882a593Smuzhiyun
1063*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, offset + ZSTD_REP_MOVE, mLength - MINMATCH);
1064*4882a593Smuzhiyun }
1065*4882a593Smuzhiyun
1066*4882a593Smuzhiyun /* match found */
1067*4882a593Smuzhiyun ip += mLength;
1068*4882a593Smuzhiyun anchor = ip;
1069*4882a593Smuzhiyun
1070*4882a593Smuzhiyun if (ip <= ilimit) {
1071*4882a593Smuzhiyun /* Fill Table */
1072*4882a593Smuzhiyun hashTable[ZSTD_hashPtr(base + curr + 2, hBits, mls)] = curr + 2; /* here because curr+2 could be > iend-8 */
1073*4882a593Smuzhiyun hashTable[ZSTD_hashPtr(ip - 2, hBits, mls)] = (U32)(ip - 2 - base);
1074*4882a593Smuzhiyun /* check immediate repcode */
1075*4882a593Smuzhiyun while ((ip <= ilimit) && ((offset_2 > 0) & (ZSTD_read32(ip) == ZSTD_read32(ip - offset_2)))) {
1076*4882a593Smuzhiyun /* store sequence */
1077*4882a593Smuzhiyun size_t const rLength = ZSTD_count(ip + 4, ip + 4 - offset_2, iend) + 4;
1078*4882a593Smuzhiyun {
1079*4882a593Smuzhiyun U32 const tmpOff = offset_2;
1080*4882a593Smuzhiyun offset_2 = offset_1;
1081*4882a593Smuzhiyun offset_1 = tmpOff;
1082*4882a593Smuzhiyun } /* swap offset_2 <=> offset_1 */
1083*4882a593Smuzhiyun hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip - base);
1084*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength - MINMATCH);
1085*4882a593Smuzhiyun ip += rLength;
1086*4882a593Smuzhiyun anchor = ip;
1087*4882a593Smuzhiyun continue; /* faster when present ... (?) */
1088*4882a593Smuzhiyun }
1089*4882a593Smuzhiyun }
1090*4882a593Smuzhiyun }
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun /* save reps for next block */
1093*4882a593Smuzhiyun cctx->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved;
1094*4882a593Smuzhiyun cctx->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved;
1095*4882a593Smuzhiyun
1096*4882a593Smuzhiyun /* Last Literals */
1097*4882a593Smuzhiyun {
1098*4882a593Smuzhiyun size_t const lastLLSize = iend - anchor;
1099*4882a593Smuzhiyun memcpy(seqStorePtr->lit, anchor, lastLLSize);
1100*4882a593Smuzhiyun seqStorePtr->lit += lastLLSize;
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun }
1103*4882a593Smuzhiyun
ZSTD_compressBlock_fast(ZSTD_CCtx * ctx,const void * src,size_t srcSize)1104*4882a593Smuzhiyun static void ZSTD_compressBlock_fast(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
1105*4882a593Smuzhiyun {
1106*4882a593Smuzhiyun const U32 mls = ctx->params.cParams.searchLength;
1107*4882a593Smuzhiyun switch (mls) {
1108*4882a593Smuzhiyun default: /* includes case 3 */
1109*4882a593Smuzhiyun case 4: ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 4); return;
1110*4882a593Smuzhiyun case 5: ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 5); return;
1111*4882a593Smuzhiyun case 6: ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 6); return;
1112*4882a593Smuzhiyun case 7: ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 7); return;
1113*4882a593Smuzhiyun }
1114*4882a593Smuzhiyun }
1115*4882a593Smuzhiyun
ZSTD_compressBlock_fast_extDict_generic(ZSTD_CCtx * ctx,const void * src,size_t srcSize,const U32 mls)1116*4882a593Smuzhiyun static void ZSTD_compressBlock_fast_extDict_generic(ZSTD_CCtx *ctx, const void *src, size_t srcSize, const U32 mls)
1117*4882a593Smuzhiyun {
1118*4882a593Smuzhiyun U32 *hashTable = ctx->hashTable;
1119*4882a593Smuzhiyun const U32 hBits = ctx->params.cParams.hashLog;
1120*4882a593Smuzhiyun seqStore_t *seqStorePtr = &(ctx->seqStore);
1121*4882a593Smuzhiyun const BYTE *const base = ctx->base;
1122*4882a593Smuzhiyun const BYTE *const dictBase = ctx->dictBase;
1123*4882a593Smuzhiyun const BYTE *const istart = (const BYTE *)src;
1124*4882a593Smuzhiyun const BYTE *ip = istart;
1125*4882a593Smuzhiyun const BYTE *anchor = istart;
1126*4882a593Smuzhiyun const U32 lowestIndex = ctx->lowLimit;
1127*4882a593Smuzhiyun const BYTE *const dictStart = dictBase + lowestIndex;
1128*4882a593Smuzhiyun const U32 dictLimit = ctx->dictLimit;
1129*4882a593Smuzhiyun const BYTE *const lowPrefixPtr = base + dictLimit;
1130*4882a593Smuzhiyun const BYTE *const dictEnd = dictBase + dictLimit;
1131*4882a593Smuzhiyun const BYTE *const iend = istart + srcSize;
1132*4882a593Smuzhiyun const BYTE *const ilimit = iend - 8;
1133*4882a593Smuzhiyun U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1];
1134*4882a593Smuzhiyun
1135*4882a593Smuzhiyun /* Search Loop */
1136*4882a593Smuzhiyun while (ip < ilimit) { /* < instead of <=, because (ip+1) */
1137*4882a593Smuzhiyun const size_t h = ZSTD_hashPtr(ip, hBits, mls);
1138*4882a593Smuzhiyun const U32 matchIndex = hashTable[h];
1139*4882a593Smuzhiyun const BYTE *matchBase = matchIndex < dictLimit ? dictBase : base;
1140*4882a593Smuzhiyun const BYTE *match = matchBase + matchIndex;
1141*4882a593Smuzhiyun const U32 curr = (U32)(ip - base);
1142*4882a593Smuzhiyun const U32 repIndex = curr + 1 - offset_1; /* offset_1 expected <= curr +1 */
1143*4882a593Smuzhiyun const BYTE *repBase = repIndex < dictLimit ? dictBase : base;
1144*4882a593Smuzhiyun const BYTE *repMatch = repBase + repIndex;
1145*4882a593Smuzhiyun size_t mLength;
1146*4882a593Smuzhiyun hashTable[h] = curr; /* update hash table */
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun if ((((U32)((dictLimit - 1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex)) &&
1149*4882a593Smuzhiyun (ZSTD_read32(repMatch) == ZSTD_read32(ip + 1))) {
1150*4882a593Smuzhiyun const BYTE *repMatchEnd = repIndex < dictLimit ? dictEnd : iend;
1151*4882a593Smuzhiyun mLength = ZSTD_count_2segments(ip + 1 + EQUAL_READ32, repMatch + EQUAL_READ32, iend, repMatchEnd, lowPrefixPtr) + EQUAL_READ32;
1152*4882a593Smuzhiyun ip++;
1153*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, 0, mLength - MINMATCH);
1154*4882a593Smuzhiyun } else {
1155*4882a593Smuzhiyun if ((matchIndex < lowestIndex) || (ZSTD_read32(match) != ZSTD_read32(ip))) {
1156*4882a593Smuzhiyun ip += ((ip - anchor) >> g_searchStrength) + 1;
1157*4882a593Smuzhiyun continue;
1158*4882a593Smuzhiyun }
1159*4882a593Smuzhiyun {
1160*4882a593Smuzhiyun const BYTE *matchEnd = matchIndex < dictLimit ? dictEnd : iend;
1161*4882a593Smuzhiyun const BYTE *lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr;
1162*4882a593Smuzhiyun U32 offset;
1163*4882a593Smuzhiyun mLength = ZSTD_count_2segments(ip + EQUAL_READ32, match + EQUAL_READ32, iend, matchEnd, lowPrefixPtr) + EQUAL_READ32;
1164*4882a593Smuzhiyun while (((ip > anchor) & (match > lowMatchPtr)) && (ip[-1] == match[-1])) {
1165*4882a593Smuzhiyun ip--;
1166*4882a593Smuzhiyun match--;
1167*4882a593Smuzhiyun mLength++;
1168*4882a593Smuzhiyun } /* catch up */
1169*4882a593Smuzhiyun offset = curr - matchIndex;
1170*4882a593Smuzhiyun offset_2 = offset_1;
1171*4882a593Smuzhiyun offset_1 = offset;
1172*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, offset + ZSTD_REP_MOVE, mLength - MINMATCH);
1173*4882a593Smuzhiyun }
1174*4882a593Smuzhiyun }
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyun /* found a match : store it */
1177*4882a593Smuzhiyun ip += mLength;
1178*4882a593Smuzhiyun anchor = ip;
1179*4882a593Smuzhiyun
1180*4882a593Smuzhiyun if (ip <= ilimit) {
1181*4882a593Smuzhiyun /* Fill Table */
1182*4882a593Smuzhiyun hashTable[ZSTD_hashPtr(base + curr + 2, hBits, mls)] = curr + 2;
1183*4882a593Smuzhiyun hashTable[ZSTD_hashPtr(ip - 2, hBits, mls)] = (U32)(ip - 2 - base);
1184*4882a593Smuzhiyun /* check immediate repcode */
1185*4882a593Smuzhiyun while (ip <= ilimit) {
1186*4882a593Smuzhiyun U32 const curr2 = (U32)(ip - base);
1187*4882a593Smuzhiyun U32 const repIndex2 = curr2 - offset_2;
1188*4882a593Smuzhiyun const BYTE *repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2;
1189*4882a593Smuzhiyun if ((((U32)((dictLimit - 1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */
1190*4882a593Smuzhiyun && (ZSTD_read32(repMatch2) == ZSTD_read32(ip))) {
1191*4882a593Smuzhiyun const BYTE *const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend;
1192*4882a593Smuzhiyun size_t repLength2 =
1193*4882a593Smuzhiyun ZSTD_count_2segments(ip + EQUAL_READ32, repMatch2 + EQUAL_READ32, iend, repEnd2, lowPrefixPtr) + EQUAL_READ32;
1194*4882a593Smuzhiyun U32 tmpOffset = offset_2;
1195*4882a593Smuzhiyun offset_2 = offset_1;
1196*4882a593Smuzhiyun offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
1197*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2 - MINMATCH);
1198*4882a593Smuzhiyun hashTable[ZSTD_hashPtr(ip, hBits, mls)] = curr2;
1199*4882a593Smuzhiyun ip += repLength2;
1200*4882a593Smuzhiyun anchor = ip;
1201*4882a593Smuzhiyun continue;
1202*4882a593Smuzhiyun }
1203*4882a593Smuzhiyun break;
1204*4882a593Smuzhiyun }
1205*4882a593Smuzhiyun }
1206*4882a593Smuzhiyun }
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun /* save reps for next block */
1209*4882a593Smuzhiyun ctx->repToConfirm[0] = offset_1;
1210*4882a593Smuzhiyun ctx->repToConfirm[1] = offset_2;
1211*4882a593Smuzhiyun
1212*4882a593Smuzhiyun /* Last Literals */
1213*4882a593Smuzhiyun {
1214*4882a593Smuzhiyun size_t const lastLLSize = iend - anchor;
1215*4882a593Smuzhiyun memcpy(seqStorePtr->lit, anchor, lastLLSize);
1216*4882a593Smuzhiyun seqStorePtr->lit += lastLLSize;
1217*4882a593Smuzhiyun }
1218*4882a593Smuzhiyun }
1219*4882a593Smuzhiyun
ZSTD_compressBlock_fast_extDict(ZSTD_CCtx * ctx,const void * src,size_t srcSize)1220*4882a593Smuzhiyun static void ZSTD_compressBlock_fast_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
1221*4882a593Smuzhiyun {
1222*4882a593Smuzhiyun U32 const mls = ctx->params.cParams.searchLength;
1223*4882a593Smuzhiyun switch (mls) {
1224*4882a593Smuzhiyun default: /* includes case 3 */
1225*4882a593Smuzhiyun case 4: ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 4); return;
1226*4882a593Smuzhiyun case 5: ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 5); return;
1227*4882a593Smuzhiyun case 6: ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 6); return;
1228*4882a593Smuzhiyun case 7: ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 7); return;
1229*4882a593Smuzhiyun }
1230*4882a593Smuzhiyun }
1231*4882a593Smuzhiyun
1232*4882a593Smuzhiyun /*-*************************************
1233*4882a593Smuzhiyun * Double Fast
1234*4882a593Smuzhiyun ***************************************/
ZSTD_fillDoubleHashTable(ZSTD_CCtx * cctx,const void * end,const U32 mls)1235*4882a593Smuzhiyun static void ZSTD_fillDoubleHashTable(ZSTD_CCtx *cctx, const void *end, const U32 mls)
1236*4882a593Smuzhiyun {
1237*4882a593Smuzhiyun U32 *const hashLarge = cctx->hashTable;
1238*4882a593Smuzhiyun U32 const hBitsL = cctx->params.cParams.hashLog;
1239*4882a593Smuzhiyun U32 *const hashSmall = cctx->chainTable;
1240*4882a593Smuzhiyun U32 const hBitsS = cctx->params.cParams.chainLog;
1241*4882a593Smuzhiyun const BYTE *const base = cctx->base;
1242*4882a593Smuzhiyun const BYTE *ip = base + cctx->nextToUpdate;
1243*4882a593Smuzhiyun const BYTE *const iend = ((const BYTE *)end) - HASH_READ_SIZE;
1244*4882a593Smuzhiyun const size_t fastHashFillStep = 3;
1245*4882a593Smuzhiyun
1246*4882a593Smuzhiyun while (ip <= iend) {
1247*4882a593Smuzhiyun hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip - base);
1248*4882a593Smuzhiyun hashLarge[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip - base);
1249*4882a593Smuzhiyun ip += fastHashFillStep;
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun }
1252*4882a593Smuzhiyun
1253*4882a593Smuzhiyun FORCE_INLINE
ZSTD_compressBlock_doubleFast_generic(ZSTD_CCtx * cctx,const void * src,size_t srcSize,const U32 mls)1254*4882a593Smuzhiyun void ZSTD_compressBlock_doubleFast_generic(ZSTD_CCtx *cctx, const void *src, size_t srcSize, const U32 mls)
1255*4882a593Smuzhiyun {
1256*4882a593Smuzhiyun U32 *const hashLong = cctx->hashTable;
1257*4882a593Smuzhiyun const U32 hBitsL = cctx->params.cParams.hashLog;
1258*4882a593Smuzhiyun U32 *const hashSmall = cctx->chainTable;
1259*4882a593Smuzhiyun const U32 hBitsS = cctx->params.cParams.chainLog;
1260*4882a593Smuzhiyun seqStore_t *seqStorePtr = &(cctx->seqStore);
1261*4882a593Smuzhiyun const BYTE *const base = cctx->base;
1262*4882a593Smuzhiyun const BYTE *const istart = (const BYTE *)src;
1263*4882a593Smuzhiyun const BYTE *ip = istart;
1264*4882a593Smuzhiyun const BYTE *anchor = istart;
1265*4882a593Smuzhiyun const U32 lowestIndex = cctx->dictLimit;
1266*4882a593Smuzhiyun const BYTE *const lowest = base + lowestIndex;
1267*4882a593Smuzhiyun const BYTE *const iend = istart + srcSize;
1268*4882a593Smuzhiyun const BYTE *const ilimit = iend - HASH_READ_SIZE;
1269*4882a593Smuzhiyun U32 offset_1 = cctx->rep[0], offset_2 = cctx->rep[1];
1270*4882a593Smuzhiyun U32 offsetSaved = 0;
1271*4882a593Smuzhiyun
1272*4882a593Smuzhiyun /* init */
1273*4882a593Smuzhiyun ip += (ip == lowest);
1274*4882a593Smuzhiyun {
1275*4882a593Smuzhiyun U32 const maxRep = (U32)(ip - lowest);
1276*4882a593Smuzhiyun if (offset_2 > maxRep)
1277*4882a593Smuzhiyun offsetSaved = offset_2, offset_2 = 0;
1278*4882a593Smuzhiyun if (offset_1 > maxRep)
1279*4882a593Smuzhiyun offsetSaved = offset_1, offset_1 = 0;
1280*4882a593Smuzhiyun }
1281*4882a593Smuzhiyun
1282*4882a593Smuzhiyun /* Main Search Loop */
1283*4882a593Smuzhiyun while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
1284*4882a593Smuzhiyun size_t mLength;
1285*4882a593Smuzhiyun size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
1286*4882a593Smuzhiyun size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
1287*4882a593Smuzhiyun U32 const curr = (U32)(ip - base);
1288*4882a593Smuzhiyun U32 const matchIndexL = hashLong[h2];
1289*4882a593Smuzhiyun U32 const matchIndexS = hashSmall[h];
1290*4882a593Smuzhiyun const BYTE *matchLong = base + matchIndexL;
1291*4882a593Smuzhiyun const BYTE *match = base + matchIndexS;
1292*4882a593Smuzhiyun hashLong[h2] = hashSmall[h] = curr; /* update hash tables */
1293*4882a593Smuzhiyun
1294*4882a593Smuzhiyun if ((offset_1 > 0) & (ZSTD_read32(ip + 1 - offset_1) == ZSTD_read32(ip + 1))) { /* note : by construction, offset_1 <= curr */
1295*4882a593Smuzhiyun mLength = ZSTD_count(ip + 1 + 4, ip + 1 + 4 - offset_1, iend) + 4;
1296*4882a593Smuzhiyun ip++;
1297*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, 0, mLength - MINMATCH);
1298*4882a593Smuzhiyun } else {
1299*4882a593Smuzhiyun U32 offset;
1300*4882a593Smuzhiyun if ((matchIndexL > lowestIndex) && (ZSTD_read64(matchLong) == ZSTD_read64(ip))) {
1301*4882a593Smuzhiyun mLength = ZSTD_count(ip + 8, matchLong + 8, iend) + 8;
1302*4882a593Smuzhiyun offset = (U32)(ip - matchLong);
1303*4882a593Smuzhiyun while (((ip > anchor) & (matchLong > lowest)) && (ip[-1] == matchLong[-1])) {
1304*4882a593Smuzhiyun ip--;
1305*4882a593Smuzhiyun matchLong--;
1306*4882a593Smuzhiyun mLength++;
1307*4882a593Smuzhiyun } /* catch up */
1308*4882a593Smuzhiyun } else if ((matchIndexS > lowestIndex) && (ZSTD_read32(match) == ZSTD_read32(ip))) {
1309*4882a593Smuzhiyun size_t const h3 = ZSTD_hashPtr(ip + 1, hBitsL, 8);
1310*4882a593Smuzhiyun U32 const matchIndex3 = hashLong[h3];
1311*4882a593Smuzhiyun const BYTE *match3 = base + matchIndex3;
1312*4882a593Smuzhiyun hashLong[h3] = curr + 1;
1313*4882a593Smuzhiyun if ((matchIndex3 > lowestIndex) && (ZSTD_read64(match3) == ZSTD_read64(ip + 1))) {
1314*4882a593Smuzhiyun mLength = ZSTD_count(ip + 9, match3 + 8, iend) + 8;
1315*4882a593Smuzhiyun ip++;
1316*4882a593Smuzhiyun offset = (U32)(ip - match3);
1317*4882a593Smuzhiyun while (((ip > anchor) & (match3 > lowest)) && (ip[-1] == match3[-1])) {
1318*4882a593Smuzhiyun ip--;
1319*4882a593Smuzhiyun match3--;
1320*4882a593Smuzhiyun mLength++;
1321*4882a593Smuzhiyun } /* catch up */
1322*4882a593Smuzhiyun } else {
1323*4882a593Smuzhiyun mLength = ZSTD_count(ip + 4, match + 4, iend) + 4;
1324*4882a593Smuzhiyun offset = (U32)(ip - match);
1325*4882a593Smuzhiyun while (((ip > anchor) & (match > lowest)) && (ip[-1] == match[-1])) {
1326*4882a593Smuzhiyun ip--;
1327*4882a593Smuzhiyun match--;
1328*4882a593Smuzhiyun mLength++;
1329*4882a593Smuzhiyun } /* catch up */
1330*4882a593Smuzhiyun }
1331*4882a593Smuzhiyun } else {
1332*4882a593Smuzhiyun ip += ((ip - anchor) >> g_searchStrength) + 1;
1333*4882a593Smuzhiyun continue;
1334*4882a593Smuzhiyun }
1335*4882a593Smuzhiyun
1336*4882a593Smuzhiyun offset_2 = offset_1;
1337*4882a593Smuzhiyun offset_1 = offset;
1338*4882a593Smuzhiyun
1339*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, offset + ZSTD_REP_MOVE, mLength - MINMATCH);
1340*4882a593Smuzhiyun }
1341*4882a593Smuzhiyun
1342*4882a593Smuzhiyun /* match found */
1343*4882a593Smuzhiyun ip += mLength;
1344*4882a593Smuzhiyun anchor = ip;
1345*4882a593Smuzhiyun
1346*4882a593Smuzhiyun if (ip <= ilimit) {
1347*4882a593Smuzhiyun /* Fill Table */
1348*4882a593Smuzhiyun hashLong[ZSTD_hashPtr(base + curr + 2, hBitsL, 8)] = hashSmall[ZSTD_hashPtr(base + curr + 2, hBitsS, mls)] =
1349*4882a593Smuzhiyun curr + 2; /* here because curr+2 could be > iend-8 */
1350*4882a593Smuzhiyun hashLong[ZSTD_hashPtr(ip - 2, hBitsL, 8)] = hashSmall[ZSTD_hashPtr(ip - 2, hBitsS, mls)] = (U32)(ip - 2 - base);
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun /* check immediate repcode */
1353*4882a593Smuzhiyun while ((ip <= ilimit) && ((offset_2 > 0) & (ZSTD_read32(ip) == ZSTD_read32(ip - offset_2)))) {
1354*4882a593Smuzhiyun /* store sequence */
1355*4882a593Smuzhiyun size_t const rLength = ZSTD_count(ip + 4, ip + 4 - offset_2, iend) + 4;
1356*4882a593Smuzhiyun {
1357*4882a593Smuzhiyun U32 const tmpOff = offset_2;
1358*4882a593Smuzhiyun offset_2 = offset_1;
1359*4882a593Smuzhiyun offset_1 = tmpOff;
1360*4882a593Smuzhiyun } /* swap offset_2 <=> offset_1 */
1361*4882a593Smuzhiyun hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip - base);
1362*4882a593Smuzhiyun hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip - base);
1363*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength - MINMATCH);
1364*4882a593Smuzhiyun ip += rLength;
1365*4882a593Smuzhiyun anchor = ip;
1366*4882a593Smuzhiyun continue; /* faster when present ... (?) */
1367*4882a593Smuzhiyun }
1368*4882a593Smuzhiyun }
1369*4882a593Smuzhiyun }
1370*4882a593Smuzhiyun
1371*4882a593Smuzhiyun /* save reps for next block */
1372*4882a593Smuzhiyun cctx->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved;
1373*4882a593Smuzhiyun cctx->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved;
1374*4882a593Smuzhiyun
1375*4882a593Smuzhiyun /* Last Literals */
1376*4882a593Smuzhiyun {
1377*4882a593Smuzhiyun size_t const lastLLSize = iend - anchor;
1378*4882a593Smuzhiyun memcpy(seqStorePtr->lit, anchor, lastLLSize);
1379*4882a593Smuzhiyun seqStorePtr->lit += lastLLSize;
1380*4882a593Smuzhiyun }
1381*4882a593Smuzhiyun }
1382*4882a593Smuzhiyun
ZSTD_compressBlock_doubleFast(ZSTD_CCtx * ctx,const void * src,size_t srcSize)1383*4882a593Smuzhiyun static void ZSTD_compressBlock_doubleFast(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
1384*4882a593Smuzhiyun {
1385*4882a593Smuzhiyun const U32 mls = ctx->params.cParams.searchLength;
1386*4882a593Smuzhiyun switch (mls) {
1387*4882a593Smuzhiyun default: /* includes case 3 */
1388*4882a593Smuzhiyun case 4: ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 4); return;
1389*4882a593Smuzhiyun case 5: ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 5); return;
1390*4882a593Smuzhiyun case 6: ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 6); return;
1391*4882a593Smuzhiyun case 7: ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 7); return;
1392*4882a593Smuzhiyun }
1393*4882a593Smuzhiyun }
1394*4882a593Smuzhiyun
ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx * ctx,const void * src,size_t srcSize,const U32 mls)1395*4882a593Smuzhiyun static void ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx *ctx, const void *src, size_t srcSize, const U32 mls)
1396*4882a593Smuzhiyun {
1397*4882a593Smuzhiyun U32 *const hashLong = ctx->hashTable;
1398*4882a593Smuzhiyun U32 const hBitsL = ctx->params.cParams.hashLog;
1399*4882a593Smuzhiyun U32 *const hashSmall = ctx->chainTable;
1400*4882a593Smuzhiyun U32 const hBitsS = ctx->params.cParams.chainLog;
1401*4882a593Smuzhiyun seqStore_t *seqStorePtr = &(ctx->seqStore);
1402*4882a593Smuzhiyun const BYTE *const base = ctx->base;
1403*4882a593Smuzhiyun const BYTE *const dictBase = ctx->dictBase;
1404*4882a593Smuzhiyun const BYTE *const istart = (const BYTE *)src;
1405*4882a593Smuzhiyun const BYTE *ip = istart;
1406*4882a593Smuzhiyun const BYTE *anchor = istart;
1407*4882a593Smuzhiyun const U32 lowestIndex = ctx->lowLimit;
1408*4882a593Smuzhiyun const BYTE *const dictStart = dictBase + lowestIndex;
1409*4882a593Smuzhiyun const U32 dictLimit = ctx->dictLimit;
1410*4882a593Smuzhiyun const BYTE *const lowPrefixPtr = base + dictLimit;
1411*4882a593Smuzhiyun const BYTE *const dictEnd = dictBase + dictLimit;
1412*4882a593Smuzhiyun const BYTE *const iend = istart + srcSize;
1413*4882a593Smuzhiyun const BYTE *const ilimit = iend - 8;
1414*4882a593Smuzhiyun U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1];
1415*4882a593Smuzhiyun
1416*4882a593Smuzhiyun /* Search Loop */
1417*4882a593Smuzhiyun while (ip < ilimit) { /* < instead of <=, because (ip+1) */
1418*4882a593Smuzhiyun const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
1419*4882a593Smuzhiyun const U32 matchIndex = hashSmall[hSmall];
1420*4882a593Smuzhiyun const BYTE *matchBase = matchIndex < dictLimit ? dictBase : base;
1421*4882a593Smuzhiyun const BYTE *match = matchBase + matchIndex;
1422*4882a593Smuzhiyun
1423*4882a593Smuzhiyun const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
1424*4882a593Smuzhiyun const U32 matchLongIndex = hashLong[hLong];
1425*4882a593Smuzhiyun const BYTE *matchLongBase = matchLongIndex < dictLimit ? dictBase : base;
1426*4882a593Smuzhiyun const BYTE *matchLong = matchLongBase + matchLongIndex;
1427*4882a593Smuzhiyun
1428*4882a593Smuzhiyun const U32 curr = (U32)(ip - base);
1429*4882a593Smuzhiyun const U32 repIndex = curr + 1 - offset_1; /* offset_1 expected <= curr +1 */
1430*4882a593Smuzhiyun const BYTE *repBase = repIndex < dictLimit ? dictBase : base;
1431*4882a593Smuzhiyun const BYTE *repMatch = repBase + repIndex;
1432*4882a593Smuzhiyun size_t mLength;
1433*4882a593Smuzhiyun hashSmall[hSmall] = hashLong[hLong] = curr; /* update hash table */
1434*4882a593Smuzhiyun
1435*4882a593Smuzhiyun if ((((U32)((dictLimit - 1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex)) &&
1436*4882a593Smuzhiyun (ZSTD_read32(repMatch) == ZSTD_read32(ip + 1))) {
1437*4882a593Smuzhiyun const BYTE *repMatchEnd = repIndex < dictLimit ? dictEnd : iend;
1438*4882a593Smuzhiyun mLength = ZSTD_count_2segments(ip + 1 + 4, repMatch + 4, iend, repMatchEnd, lowPrefixPtr) + 4;
1439*4882a593Smuzhiyun ip++;
1440*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, 0, mLength - MINMATCH);
1441*4882a593Smuzhiyun } else {
1442*4882a593Smuzhiyun if ((matchLongIndex > lowestIndex) && (ZSTD_read64(matchLong) == ZSTD_read64(ip))) {
1443*4882a593Smuzhiyun const BYTE *matchEnd = matchLongIndex < dictLimit ? dictEnd : iend;
1444*4882a593Smuzhiyun const BYTE *lowMatchPtr = matchLongIndex < dictLimit ? dictStart : lowPrefixPtr;
1445*4882a593Smuzhiyun U32 offset;
1446*4882a593Smuzhiyun mLength = ZSTD_count_2segments(ip + 8, matchLong + 8, iend, matchEnd, lowPrefixPtr) + 8;
1447*4882a593Smuzhiyun offset = curr - matchLongIndex;
1448*4882a593Smuzhiyun while (((ip > anchor) & (matchLong > lowMatchPtr)) && (ip[-1] == matchLong[-1])) {
1449*4882a593Smuzhiyun ip--;
1450*4882a593Smuzhiyun matchLong--;
1451*4882a593Smuzhiyun mLength++;
1452*4882a593Smuzhiyun } /* catch up */
1453*4882a593Smuzhiyun offset_2 = offset_1;
1454*4882a593Smuzhiyun offset_1 = offset;
1455*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, offset + ZSTD_REP_MOVE, mLength - MINMATCH);
1456*4882a593Smuzhiyun
1457*4882a593Smuzhiyun } else if ((matchIndex > lowestIndex) && (ZSTD_read32(match) == ZSTD_read32(ip))) {
1458*4882a593Smuzhiyun size_t const h3 = ZSTD_hashPtr(ip + 1, hBitsL, 8);
1459*4882a593Smuzhiyun U32 const matchIndex3 = hashLong[h3];
1460*4882a593Smuzhiyun const BYTE *const match3Base = matchIndex3 < dictLimit ? dictBase : base;
1461*4882a593Smuzhiyun const BYTE *match3 = match3Base + matchIndex3;
1462*4882a593Smuzhiyun U32 offset;
1463*4882a593Smuzhiyun hashLong[h3] = curr + 1;
1464*4882a593Smuzhiyun if ((matchIndex3 > lowestIndex) && (ZSTD_read64(match3) == ZSTD_read64(ip + 1))) {
1465*4882a593Smuzhiyun const BYTE *matchEnd = matchIndex3 < dictLimit ? dictEnd : iend;
1466*4882a593Smuzhiyun const BYTE *lowMatchPtr = matchIndex3 < dictLimit ? dictStart : lowPrefixPtr;
1467*4882a593Smuzhiyun mLength = ZSTD_count_2segments(ip + 9, match3 + 8, iend, matchEnd, lowPrefixPtr) + 8;
1468*4882a593Smuzhiyun ip++;
1469*4882a593Smuzhiyun offset = curr + 1 - matchIndex3;
1470*4882a593Smuzhiyun while (((ip > anchor) & (match3 > lowMatchPtr)) && (ip[-1] == match3[-1])) {
1471*4882a593Smuzhiyun ip--;
1472*4882a593Smuzhiyun match3--;
1473*4882a593Smuzhiyun mLength++;
1474*4882a593Smuzhiyun } /* catch up */
1475*4882a593Smuzhiyun } else {
1476*4882a593Smuzhiyun const BYTE *matchEnd = matchIndex < dictLimit ? dictEnd : iend;
1477*4882a593Smuzhiyun const BYTE *lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr;
1478*4882a593Smuzhiyun mLength = ZSTD_count_2segments(ip + 4, match + 4, iend, matchEnd, lowPrefixPtr) + 4;
1479*4882a593Smuzhiyun offset = curr - matchIndex;
1480*4882a593Smuzhiyun while (((ip > anchor) & (match > lowMatchPtr)) && (ip[-1] == match[-1])) {
1481*4882a593Smuzhiyun ip--;
1482*4882a593Smuzhiyun match--;
1483*4882a593Smuzhiyun mLength++;
1484*4882a593Smuzhiyun } /* catch up */
1485*4882a593Smuzhiyun }
1486*4882a593Smuzhiyun offset_2 = offset_1;
1487*4882a593Smuzhiyun offset_1 = offset;
1488*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, offset + ZSTD_REP_MOVE, mLength - MINMATCH);
1489*4882a593Smuzhiyun
1490*4882a593Smuzhiyun } else {
1491*4882a593Smuzhiyun ip += ((ip - anchor) >> g_searchStrength) + 1;
1492*4882a593Smuzhiyun continue;
1493*4882a593Smuzhiyun }
1494*4882a593Smuzhiyun }
1495*4882a593Smuzhiyun
1496*4882a593Smuzhiyun /* found a match : store it */
1497*4882a593Smuzhiyun ip += mLength;
1498*4882a593Smuzhiyun anchor = ip;
1499*4882a593Smuzhiyun
1500*4882a593Smuzhiyun if (ip <= ilimit) {
1501*4882a593Smuzhiyun /* Fill Table */
1502*4882a593Smuzhiyun hashSmall[ZSTD_hashPtr(base + curr + 2, hBitsS, mls)] = curr + 2;
1503*4882a593Smuzhiyun hashLong[ZSTD_hashPtr(base + curr + 2, hBitsL, 8)] = curr + 2;
1504*4882a593Smuzhiyun hashSmall[ZSTD_hashPtr(ip - 2, hBitsS, mls)] = (U32)(ip - 2 - base);
1505*4882a593Smuzhiyun hashLong[ZSTD_hashPtr(ip - 2, hBitsL, 8)] = (U32)(ip - 2 - base);
1506*4882a593Smuzhiyun /* check immediate repcode */
1507*4882a593Smuzhiyun while (ip <= ilimit) {
1508*4882a593Smuzhiyun U32 const curr2 = (U32)(ip - base);
1509*4882a593Smuzhiyun U32 const repIndex2 = curr2 - offset_2;
1510*4882a593Smuzhiyun const BYTE *repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2;
1511*4882a593Smuzhiyun if ((((U32)((dictLimit - 1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */
1512*4882a593Smuzhiyun && (ZSTD_read32(repMatch2) == ZSTD_read32(ip))) {
1513*4882a593Smuzhiyun const BYTE *const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend;
1514*4882a593Smuzhiyun size_t const repLength2 =
1515*4882a593Smuzhiyun ZSTD_count_2segments(ip + EQUAL_READ32, repMatch2 + EQUAL_READ32, iend, repEnd2, lowPrefixPtr) + EQUAL_READ32;
1516*4882a593Smuzhiyun U32 tmpOffset = offset_2;
1517*4882a593Smuzhiyun offset_2 = offset_1;
1518*4882a593Smuzhiyun offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
1519*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2 - MINMATCH);
1520*4882a593Smuzhiyun hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = curr2;
1521*4882a593Smuzhiyun hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = curr2;
1522*4882a593Smuzhiyun ip += repLength2;
1523*4882a593Smuzhiyun anchor = ip;
1524*4882a593Smuzhiyun continue;
1525*4882a593Smuzhiyun }
1526*4882a593Smuzhiyun break;
1527*4882a593Smuzhiyun }
1528*4882a593Smuzhiyun }
1529*4882a593Smuzhiyun }
1530*4882a593Smuzhiyun
1531*4882a593Smuzhiyun /* save reps for next block */
1532*4882a593Smuzhiyun ctx->repToConfirm[0] = offset_1;
1533*4882a593Smuzhiyun ctx->repToConfirm[1] = offset_2;
1534*4882a593Smuzhiyun
1535*4882a593Smuzhiyun /* Last Literals */
1536*4882a593Smuzhiyun {
1537*4882a593Smuzhiyun size_t const lastLLSize = iend - anchor;
1538*4882a593Smuzhiyun memcpy(seqStorePtr->lit, anchor, lastLLSize);
1539*4882a593Smuzhiyun seqStorePtr->lit += lastLLSize;
1540*4882a593Smuzhiyun }
1541*4882a593Smuzhiyun }
1542*4882a593Smuzhiyun
ZSTD_compressBlock_doubleFast_extDict(ZSTD_CCtx * ctx,const void * src,size_t srcSize)1543*4882a593Smuzhiyun static void ZSTD_compressBlock_doubleFast_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
1544*4882a593Smuzhiyun {
1545*4882a593Smuzhiyun U32 const mls = ctx->params.cParams.searchLength;
1546*4882a593Smuzhiyun switch (mls) {
1547*4882a593Smuzhiyun default: /* includes case 3 */
1548*4882a593Smuzhiyun case 4: ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 4); return;
1549*4882a593Smuzhiyun case 5: ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 5); return;
1550*4882a593Smuzhiyun case 6: ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 6); return;
1551*4882a593Smuzhiyun case 7: ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 7); return;
1552*4882a593Smuzhiyun }
1553*4882a593Smuzhiyun }
1554*4882a593Smuzhiyun
1555*4882a593Smuzhiyun /*-*************************************
1556*4882a593Smuzhiyun * Binary Tree search
1557*4882a593Smuzhiyun ***************************************/
1558*4882a593Smuzhiyun /** ZSTD_insertBt1() : add one or multiple positions to tree.
1559*4882a593Smuzhiyun * ip : assumed <= iend-8 .
1560*4882a593Smuzhiyun * @return : nb of positions added */
ZSTD_insertBt1(ZSTD_CCtx * zc,const BYTE * const ip,const U32 mls,const BYTE * const iend,U32 nbCompares,U32 extDict)1561*4882a593Smuzhiyun static U32 ZSTD_insertBt1(ZSTD_CCtx *zc, const BYTE *const ip, const U32 mls, const BYTE *const iend, U32 nbCompares, U32 extDict)
1562*4882a593Smuzhiyun {
1563*4882a593Smuzhiyun U32 *const hashTable = zc->hashTable;
1564*4882a593Smuzhiyun U32 const hashLog = zc->params.cParams.hashLog;
1565*4882a593Smuzhiyun size_t const h = ZSTD_hashPtr(ip, hashLog, mls);
1566*4882a593Smuzhiyun U32 *const bt = zc->chainTable;
1567*4882a593Smuzhiyun U32 const btLog = zc->params.cParams.chainLog - 1;
1568*4882a593Smuzhiyun U32 const btMask = (1 << btLog) - 1;
1569*4882a593Smuzhiyun U32 matchIndex = hashTable[h];
1570*4882a593Smuzhiyun size_t commonLengthSmaller = 0, commonLengthLarger = 0;
1571*4882a593Smuzhiyun const BYTE *const base = zc->base;
1572*4882a593Smuzhiyun const BYTE *const dictBase = zc->dictBase;
1573*4882a593Smuzhiyun const U32 dictLimit = zc->dictLimit;
1574*4882a593Smuzhiyun const BYTE *const dictEnd = dictBase + dictLimit;
1575*4882a593Smuzhiyun const BYTE *const prefixStart = base + dictLimit;
1576*4882a593Smuzhiyun const BYTE *match;
1577*4882a593Smuzhiyun const U32 curr = (U32)(ip - base);
1578*4882a593Smuzhiyun const U32 btLow = btMask >= curr ? 0 : curr - btMask;
1579*4882a593Smuzhiyun U32 *smallerPtr = bt + 2 * (curr & btMask);
1580*4882a593Smuzhiyun U32 *largerPtr = smallerPtr + 1;
1581*4882a593Smuzhiyun U32 dummy32; /* to be nullified at the end */
1582*4882a593Smuzhiyun U32 const windowLow = zc->lowLimit;
1583*4882a593Smuzhiyun U32 matchEndIdx = curr + 8;
1584*4882a593Smuzhiyun size_t bestLength = 8;
1585*4882a593Smuzhiyun
1586*4882a593Smuzhiyun hashTable[h] = curr; /* Update Hash Table */
1587*4882a593Smuzhiyun
1588*4882a593Smuzhiyun while (nbCompares-- && (matchIndex > windowLow)) {
1589*4882a593Smuzhiyun U32 *const nextPtr = bt + 2 * (matchIndex & btMask);
1590*4882a593Smuzhiyun size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
1591*4882a593Smuzhiyun
1592*4882a593Smuzhiyun if ((!extDict) || (matchIndex + matchLength >= dictLimit)) {
1593*4882a593Smuzhiyun match = base + matchIndex;
1594*4882a593Smuzhiyun if (match[matchLength] == ip[matchLength])
1595*4882a593Smuzhiyun matchLength += ZSTD_count(ip + matchLength + 1, match + matchLength + 1, iend) + 1;
1596*4882a593Smuzhiyun } else {
1597*4882a593Smuzhiyun match = dictBase + matchIndex;
1598*4882a593Smuzhiyun matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iend, dictEnd, prefixStart);
1599*4882a593Smuzhiyun if (matchIndex + matchLength >= dictLimit)
1600*4882a593Smuzhiyun match = base + matchIndex; /* to prepare for next usage of match[matchLength] */
1601*4882a593Smuzhiyun }
1602*4882a593Smuzhiyun
1603*4882a593Smuzhiyun if (matchLength > bestLength) {
1604*4882a593Smuzhiyun bestLength = matchLength;
1605*4882a593Smuzhiyun if (matchLength > matchEndIdx - matchIndex)
1606*4882a593Smuzhiyun matchEndIdx = matchIndex + (U32)matchLength;
1607*4882a593Smuzhiyun }
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun if (ip + matchLength == iend) /* equal : no way to know if inf or sup */
1610*4882a593Smuzhiyun break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt the tree */
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun if (match[matchLength] < ip[matchLength]) { /* necessarily within correct buffer */
1613*4882a593Smuzhiyun /* match is smaller than curr */
1614*4882a593Smuzhiyun *smallerPtr = matchIndex; /* update smaller idx */
1615*4882a593Smuzhiyun commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
1616*4882a593Smuzhiyun if (matchIndex <= btLow) {
1617*4882a593Smuzhiyun smallerPtr = &dummy32;
1618*4882a593Smuzhiyun break;
1619*4882a593Smuzhiyun } /* beyond tree size, stop the search */
1620*4882a593Smuzhiyun smallerPtr = nextPtr + 1; /* new "smaller" => larger of match */
1621*4882a593Smuzhiyun matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to curr) */
1622*4882a593Smuzhiyun } else {
1623*4882a593Smuzhiyun /* match is larger than curr */
1624*4882a593Smuzhiyun *largerPtr = matchIndex;
1625*4882a593Smuzhiyun commonLengthLarger = matchLength;
1626*4882a593Smuzhiyun if (matchIndex <= btLow) {
1627*4882a593Smuzhiyun largerPtr = &dummy32;
1628*4882a593Smuzhiyun break;
1629*4882a593Smuzhiyun } /* beyond tree size, stop the search */
1630*4882a593Smuzhiyun largerPtr = nextPtr;
1631*4882a593Smuzhiyun matchIndex = nextPtr[0];
1632*4882a593Smuzhiyun }
1633*4882a593Smuzhiyun }
1634*4882a593Smuzhiyun
1635*4882a593Smuzhiyun *smallerPtr = *largerPtr = 0;
1636*4882a593Smuzhiyun if (bestLength > 384)
1637*4882a593Smuzhiyun return MIN(192, (U32)(bestLength - 384)); /* speed optimization */
1638*4882a593Smuzhiyun if (matchEndIdx > curr + 8)
1639*4882a593Smuzhiyun return matchEndIdx - curr - 8;
1640*4882a593Smuzhiyun return 1;
1641*4882a593Smuzhiyun }
1642*4882a593Smuzhiyun
ZSTD_insertBtAndFindBestMatch(ZSTD_CCtx * zc,const BYTE * const ip,const BYTE * const iend,size_t * offsetPtr,U32 nbCompares,const U32 mls,U32 extDict)1643*4882a593Smuzhiyun static size_t ZSTD_insertBtAndFindBestMatch(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iend, size_t *offsetPtr, U32 nbCompares, const U32 mls,
1644*4882a593Smuzhiyun U32 extDict)
1645*4882a593Smuzhiyun {
1646*4882a593Smuzhiyun U32 *const hashTable = zc->hashTable;
1647*4882a593Smuzhiyun U32 const hashLog = zc->params.cParams.hashLog;
1648*4882a593Smuzhiyun size_t const h = ZSTD_hashPtr(ip, hashLog, mls);
1649*4882a593Smuzhiyun U32 *const bt = zc->chainTable;
1650*4882a593Smuzhiyun U32 const btLog = zc->params.cParams.chainLog - 1;
1651*4882a593Smuzhiyun U32 const btMask = (1 << btLog) - 1;
1652*4882a593Smuzhiyun U32 matchIndex = hashTable[h];
1653*4882a593Smuzhiyun size_t commonLengthSmaller = 0, commonLengthLarger = 0;
1654*4882a593Smuzhiyun const BYTE *const base = zc->base;
1655*4882a593Smuzhiyun const BYTE *const dictBase = zc->dictBase;
1656*4882a593Smuzhiyun const U32 dictLimit = zc->dictLimit;
1657*4882a593Smuzhiyun const BYTE *const dictEnd = dictBase + dictLimit;
1658*4882a593Smuzhiyun const BYTE *const prefixStart = base + dictLimit;
1659*4882a593Smuzhiyun const U32 curr = (U32)(ip - base);
1660*4882a593Smuzhiyun const U32 btLow = btMask >= curr ? 0 : curr - btMask;
1661*4882a593Smuzhiyun const U32 windowLow = zc->lowLimit;
1662*4882a593Smuzhiyun U32 *smallerPtr = bt + 2 * (curr & btMask);
1663*4882a593Smuzhiyun U32 *largerPtr = bt + 2 * (curr & btMask) + 1;
1664*4882a593Smuzhiyun U32 matchEndIdx = curr + 8;
1665*4882a593Smuzhiyun U32 dummy32; /* to be nullified at the end */
1666*4882a593Smuzhiyun size_t bestLength = 0;
1667*4882a593Smuzhiyun
1668*4882a593Smuzhiyun hashTable[h] = curr; /* Update Hash Table */
1669*4882a593Smuzhiyun
1670*4882a593Smuzhiyun while (nbCompares-- && (matchIndex > windowLow)) {
1671*4882a593Smuzhiyun U32 *const nextPtr = bt + 2 * (matchIndex & btMask);
1672*4882a593Smuzhiyun size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
1673*4882a593Smuzhiyun const BYTE *match;
1674*4882a593Smuzhiyun
1675*4882a593Smuzhiyun if ((!extDict) || (matchIndex + matchLength >= dictLimit)) {
1676*4882a593Smuzhiyun match = base + matchIndex;
1677*4882a593Smuzhiyun if (match[matchLength] == ip[matchLength])
1678*4882a593Smuzhiyun matchLength += ZSTD_count(ip + matchLength + 1, match + matchLength + 1, iend) + 1;
1679*4882a593Smuzhiyun } else {
1680*4882a593Smuzhiyun match = dictBase + matchIndex;
1681*4882a593Smuzhiyun matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iend, dictEnd, prefixStart);
1682*4882a593Smuzhiyun if (matchIndex + matchLength >= dictLimit)
1683*4882a593Smuzhiyun match = base + matchIndex; /* to prepare for next usage of match[matchLength] */
1684*4882a593Smuzhiyun }
1685*4882a593Smuzhiyun
1686*4882a593Smuzhiyun if (matchLength > bestLength) {
1687*4882a593Smuzhiyun if (matchLength > matchEndIdx - matchIndex)
1688*4882a593Smuzhiyun matchEndIdx = matchIndex + (U32)matchLength;
1689*4882a593Smuzhiyun if ((4 * (int)(matchLength - bestLength)) > (int)(ZSTD_highbit32(curr - matchIndex + 1) - ZSTD_highbit32((U32)offsetPtr[0] + 1)))
1690*4882a593Smuzhiyun bestLength = matchLength, *offsetPtr = ZSTD_REP_MOVE + curr - matchIndex;
1691*4882a593Smuzhiyun if (ip + matchLength == iend) /* equal : no way to know if inf or sup */
1692*4882a593Smuzhiyun break; /* drop, to guarantee consistency (miss a little bit of compression) */
1693*4882a593Smuzhiyun }
1694*4882a593Smuzhiyun
1695*4882a593Smuzhiyun if (match[matchLength] < ip[matchLength]) {
1696*4882a593Smuzhiyun /* match is smaller than curr */
1697*4882a593Smuzhiyun *smallerPtr = matchIndex; /* update smaller idx */
1698*4882a593Smuzhiyun commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
1699*4882a593Smuzhiyun if (matchIndex <= btLow) {
1700*4882a593Smuzhiyun smallerPtr = &dummy32;
1701*4882a593Smuzhiyun break;
1702*4882a593Smuzhiyun } /* beyond tree size, stop the search */
1703*4882a593Smuzhiyun smallerPtr = nextPtr + 1; /* new "smaller" => larger of match */
1704*4882a593Smuzhiyun matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to curr) */
1705*4882a593Smuzhiyun } else {
1706*4882a593Smuzhiyun /* match is larger than curr */
1707*4882a593Smuzhiyun *largerPtr = matchIndex;
1708*4882a593Smuzhiyun commonLengthLarger = matchLength;
1709*4882a593Smuzhiyun if (matchIndex <= btLow) {
1710*4882a593Smuzhiyun largerPtr = &dummy32;
1711*4882a593Smuzhiyun break;
1712*4882a593Smuzhiyun } /* beyond tree size, stop the search */
1713*4882a593Smuzhiyun largerPtr = nextPtr;
1714*4882a593Smuzhiyun matchIndex = nextPtr[0];
1715*4882a593Smuzhiyun }
1716*4882a593Smuzhiyun }
1717*4882a593Smuzhiyun
1718*4882a593Smuzhiyun *smallerPtr = *largerPtr = 0;
1719*4882a593Smuzhiyun
1720*4882a593Smuzhiyun zc->nextToUpdate = (matchEndIdx > curr + 8) ? matchEndIdx - 8 : curr + 1;
1721*4882a593Smuzhiyun return bestLength;
1722*4882a593Smuzhiyun }
1723*4882a593Smuzhiyun
ZSTD_updateTree(ZSTD_CCtx * zc,const BYTE * const ip,const BYTE * const iend,const U32 nbCompares,const U32 mls)1724*4882a593Smuzhiyun static void ZSTD_updateTree(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iend, const U32 nbCompares, const U32 mls)
1725*4882a593Smuzhiyun {
1726*4882a593Smuzhiyun const BYTE *const base = zc->base;
1727*4882a593Smuzhiyun const U32 target = (U32)(ip - base);
1728*4882a593Smuzhiyun U32 idx = zc->nextToUpdate;
1729*4882a593Smuzhiyun
1730*4882a593Smuzhiyun while (idx < target)
1731*4882a593Smuzhiyun idx += ZSTD_insertBt1(zc, base + idx, mls, iend, nbCompares, 0);
1732*4882a593Smuzhiyun }
1733*4882a593Smuzhiyun
1734*4882a593Smuzhiyun /** ZSTD_BtFindBestMatch() : Tree updater, providing best match */
ZSTD_BtFindBestMatch(ZSTD_CCtx * zc,const BYTE * const ip,const BYTE * const iLimit,size_t * offsetPtr,const U32 maxNbAttempts,const U32 mls)1735*4882a593Smuzhiyun static size_t ZSTD_BtFindBestMatch(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts, const U32 mls)
1736*4882a593Smuzhiyun {
1737*4882a593Smuzhiyun if (ip < zc->base + zc->nextToUpdate)
1738*4882a593Smuzhiyun return 0; /* skipped area */
1739*4882a593Smuzhiyun ZSTD_updateTree(zc, ip, iLimit, maxNbAttempts, mls);
1740*4882a593Smuzhiyun return ZSTD_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls, 0);
1741*4882a593Smuzhiyun }
1742*4882a593Smuzhiyun
ZSTD_BtFindBestMatch_selectMLS(ZSTD_CCtx * zc,const BYTE * ip,const BYTE * const iLimit,size_t * offsetPtr,const U32 maxNbAttempts,const U32 matchLengthSearch)1743*4882a593Smuzhiyun static size_t ZSTD_BtFindBestMatch_selectMLS(ZSTD_CCtx *zc, /* Index table will be updated */
1744*4882a593Smuzhiyun const BYTE *ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts, const U32 matchLengthSearch)
1745*4882a593Smuzhiyun {
1746*4882a593Smuzhiyun switch (matchLengthSearch) {
1747*4882a593Smuzhiyun default: /* includes case 3 */
1748*4882a593Smuzhiyun case 4: return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4);
1749*4882a593Smuzhiyun case 5: return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5);
1750*4882a593Smuzhiyun case 7:
1751*4882a593Smuzhiyun case 6: return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6);
1752*4882a593Smuzhiyun }
1753*4882a593Smuzhiyun }
1754*4882a593Smuzhiyun
ZSTD_updateTree_extDict(ZSTD_CCtx * zc,const BYTE * const ip,const BYTE * const iend,const U32 nbCompares,const U32 mls)1755*4882a593Smuzhiyun static void ZSTD_updateTree_extDict(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iend, const U32 nbCompares, const U32 mls)
1756*4882a593Smuzhiyun {
1757*4882a593Smuzhiyun const BYTE *const base = zc->base;
1758*4882a593Smuzhiyun const U32 target = (U32)(ip - base);
1759*4882a593Smuzhiyun U32 idx = zc->nextToUpdate;
1760*4882a593Smuzhiyun
1761*4882a593Smuzhiyun while (idx < target)
1762*4882a593Smuzhiyun idx += ZSTD_insertBt1(zc, base + idx, mls, iend, nbCompares, 1);
1763*4882a593Smuzhiyun }
1764*4882a593Smuzhiyun
1765*4882a593Smuzhiyun /** Tree updater, providing best match */
ZSTD_BtFindBestMatch_extDict(ZSTD_CCtx * zc,const BYTE * const ip,const BYTE * const iLimit,size_t * offsetPtr,const U32 maxNbAttempts,const U32 mls)1766*4882a593Smuzhiyun static size_t ZSTD_BtFindBestMatch_extDict(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts,
1767*4882a593Smuzhiyun const U32 mls)
1768*4882a593Smuzhiyun {
1769*4882a593Smuzhiyun if (ip < zc->base + zc->nextToUpdate)
1770*4882a593Smuzhiyun return 0; /* skipped area */
1771*4882a593Smuzhiyun ZSTD_updateTree_extDict(zc, ip, iLimit, maxNbAttempts, mls);
1772*4882a593Smuzhiyun return ZSTD_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls, 1);
1773*4882a593Smuzhiyun }
1774*4882a593Smuzhiyun
ZSTD_BtFindBestMatch_selectMLS_extDict(ZSTD_CCtx * zc,const BYTE * ip,const BYTE * const iLimit,size_t * offsetPtr,const U32 maxNbAttempts,const U32 matchLengthSearch)1775*4882a593Smuzhiyun static size_t ZSTD_BtFindBestMatch_selectMLS_extDict(ZSTD_CCtx *zc, /* Index table will be updated */
1776*4882a593Smuzhiyun const BYTE *ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts,
1777*4882a593Smuzhiyun const U32 matchLengthSearch)
1778*4882a593Smuzhiyun {
1779*4882a593Smuzhiyun switch (matchLengthSearch) {
1780*4882a593Smuzhiyun default: /* includes case 3 */
1781*4882a593Smuzhiyun case 4: return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4);
1782*4882a593Smuzhiyun case 5: return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5);
1783*4882a593Smuzhiyun case 7:
1784*4882a593Smuzhiyun case 6: return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6);
1785*4882a593Smuzhiyun }
1786*4882a593Smuzhiyun }
1787*4882a593Smuzhiyun
1788*4882a593Smuzhiyun /* *********************************
1789*4882a593Smuzhiyun * Hash Chain
1790*4882a593Smuzhiyun ***********************************/
1791*4882a593Smuzhiyun #define NEXT_IN_CHAIN(d, mask) chainTable[(d)&mask]
1792*4882a593Smuzhiyun
1793*4882a593Smuzhiyun /* Update chains up to ip (excluded)
1794*4882a593Smuzhiyun Assumption : always within prefix (i.e. not within extDict) */
1795*4882a593Smuzhiyun FORCE_INLINE
ZSTD_insertAndFindFirstIndex(ZSTD_CCtx * zc,const BYTE * ip,U32 mls)1796*4882a593Smuzhiyun U32 ZSTD_insertAndFindFirstIndex(ZSTD_CCtx *zc, const BYTE *ip, U32 mls)
1797*4882a593Smuzhiyun {
1798*4882a593Smuzhiyun U32 *const hashTable = zc->hashTable;
1799*4882a593Smuzhiyun const U32 hashLog = zc->params.cParams.hashLog;
1800*4882a593Smuzhiyun U32 *const chainTable = zc->chainTable;
1801*4882a593Smuzhiyun const U32 chainMask = (1 << zc->params.cParams.chainLog) - 1;
1802*4882a593Smuzhiyun const BYTE *const base = zc->base;
1803*4882a593Smuzhiyun const U32 target = (U32)(ip - base);
1804*4882a593Smuzhiyun U32 idx = zc->nextToUpdate;
1805*4882a593Smuzhiyun
1806*4882a593Smuzhiyun while (idx < target) { /* catch up */
1807*4882a593Smuzhiyun size_t const h = ZSTD_hashPtr(base + idx, hashLog, mls);
1808*4882a593Smuzhiyun NEXT_IN_CHAIN(idx, chainMask) = hashTable[h];
1809*4882a593Smuzhiyun hashTable[h] = idx;
1810*4882a593Smuzhiyun idx++;
1811*4882a593Smuzhiyun }
1812*4882a593Smuzhiyun
1813*4882a593Smuzhiyun zc->nextToUpdate = target;
1814*4882a593Smuzhiyun return hashTable[ZSTD_hashPtr(ip, hashLog, mls)];
1815*4882a593Smuzhiyun }
1816*4882a593Smuzhiyun
1817*4882a593Smuzhiyun /* inlining is important to hardwire a hot branch (template emulation) */
1818*4882a593Smuzhiyun FORCE_INLINE
ZSTD_HcFindBestMatch_generic(ZSTD_CCtx * zc,const BYTE * const ip,const BYTE * const iLimit,size_t * offsetPtr,const U32 maxNbAttempts,const U32 mls,const U32 extDict)1819*4882a593Smuzhiyun size_t ZSTD_HcFindBestMatch_generic(ZSTD_CCtx *zc, /* Index table will be updated */
1820*4882a593Smuzhiyun const BYTE *const ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts, const U32 mls,
1821*4882a593Smuzhiyun const U32 extDict)
1822*4882a593Smuzhiyun {
1823*4882a593Smuzhiyun U32 *const chainTable = zc->chainTable;
1824*4882a593Smuzhiyun const U32 chainSize = (1 << zc->params.cParams.chainLog);
1825*4882a593Smuzhiyun const U32 chainMask = chainSize - 1;
1826*4882a593Smuzhiyun const BYTE *const base = zc->base;
1827*4882a593Smuzhiyun const BYTE *const dictBase = zc->dictBase;
1828*4882a593Smuzhiyun const U32 dictLimit = zc->dictLimit;
1829*4882a593Smuzhiyun const BYTE *const prefixStart = base + dictLimit;
1830*4882a593Smuzhiyun const BYTE *const dictEnd = dictBase + dictLimit;
1831*4882a593Smuzhiyun const U32 lowLimit = zc->lowLimit;
1832*4882a593Smuzhiyun const U32 curr = (U32)(ip - base);
1833*4882a593Smuzhiyun const U32 minChain = curr > chainSize ? curr - chainSize : 0;
1834*4882a593Smuzhiyun int nbAttempts = maxNbAttempts;
1835*4882a593Smuzhiyun size_t ml = EQUAL_READ32 - 1;
1836*4882a593Smuzhiyun
1837*4882a593Smuzhiyun /* HC4 match finder */
1838*4882a593Smuzhiyun U32 matchIndex = ZSTD_insertAndFindFirstIndex(zc, ip, mls);
1839*4882a593Smuzhiyun
1840*4882a593Smuzhiyun for (; (matchIndex > lowLimit) & (nbAttempts > 0); nbAttempts--) {
1841*4882a593Smuzhiyun const BYTE *match;
1842*4882a593Smuzhiyun size_t currMl = 0;
1843*4882a593Smuzhiyun if ((!extDict) || matchIndex >= dictLimit) {
1844*4882a593Smuzhiyun match = base + matchIndex;
1845*4882a593Smuzhiyun if (match[ml] == ip[ml]) /* potentially better */
1846*4882a593Smuzhiyun currMl = ZSTD_count(ip, match, iLimit);
1847*4882a593Smuzhiyun } else {
1848*4882a593Smuzhiyun match = dictBase + matchIndex;
1849*4882a593Smuzhiyun if (ZSTD_read32(match) == ZSTD_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
1850*4882a593Smuzhiyun currMl = ZSTD_count_2segments(ip + EQUAL_READ32, match + EQUAL_READ32, iLimit, dictEnd, prefixStart) + EQUAL_READ32;
1851*4882a593Smuzhiyun }
1852*4882a593Smuzhiyun
1853*4882a593Smuzhiyun /* save best solution */
1854*4882a593Smuzhiyun if (currMl > ml) {
1855*4882a593Smuzhiyun ml = currMl;
1856*4882a593Smuzhiyun *offsetPtr = curr - matchIndex + ZSTD_REP_MOVE;
1857*4882a593Smuzhiyun if (ip + currMl == iLimit)
1858*4882a593Smuzhiyun break; /* best possible, and avoid read overflow*/
1859*4882a593Smuzhiyun }
1860*4882a593Smuzhiyun
1861*4882a593Smuzhiyun if (matchIndex <= minChain)
1862*4882a593Smuzhiyun break;
1863*4882a593Smuzhiyun matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask);
1864*4882a593Smuzhiyun }
1865*4882a593Smuzhiyun
1866*4882a593Smuzhiyun return ml;
1867*4882a593Smuzhiyun }
1868*4882a593Smuzhiyun
ZSTD_HcFindBestMatch_selectMLS(ZSTD_CCtx * zc,const BYTE * ip,const BYTE * const iLimit,size_t * offsetPtr,const U32 maxNbAttempts,const U32 matchLengthSearch)1869*4882a593Smuzhiyun FORCE_INLINE size_t ZSTD_HcFindBestMatch_selectMLS(ZSTD_CCtx *zc, const BYTE *ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts,
1870*4882a593Smuzhiyun const U32 matchLengthSearch)
1871*4882a593Smuzhiyun {
1872*4882a593Smuzhiyun switch (matchLengthSearch) {
1873*4882a593Smuzhiyun default: /* includes case 3 */
1874*4882a593Smuzhiyun case 4: return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4, 0);
1875*4882a593Smuzhiyun case 5: return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5, 0);
1876*4882a593Smuzhiyun case 7:
1877*4882a593Smuzhiyun case 6: return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6, 0);
1878*4882a593Smuzhiyun }
1879*4882a593Smuzhiyun }
1880*4882a593Smuzhiyun
ZSTD_HcFindBestMatch_extDict_selectMLS(ZSTD_CCtx * zc,const BYTE * ip,const BYTE * const iLimit,size_t * offsetPtr,const U32 maxNbAttempts,const U32 matchLengthSearch)1881*4882a593Smuzhiyun FORCE_INLINE size_t ZSTD_HcFindBestMatch_extDict_selectMLS(ZSTD_CCtx *zc, const BYTE *ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts,
1882*4882a593Smuzhiyun const U32 matchLengthSearch)
1883*4882a593Smuzhiyun {
1884*4882a593Smuzhiyun switch (matchLengthSearch) {
1885*4882a593Smuzhiyun default: /* includes case 3 */
1886*4882a593Smuzhiyun case 4: return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4, 1);
1887*4882a593Smuzhiyun case 5: return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5, 1);
1888*4882a593Smuzhiyun case 7:
1889*4882a593Smuzhiyun case 6: return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6, 1);
1890*4882a593Smuzhiyun }
1891*4882a593Smuzhiyun }
1892*4882a593Smuzhiyun
1893*4882a593Smuzhiyun /* *******************************
1894*4882a593Smuzhiyun * Common parser - lazy strategy
1895*4882a593Smuzhiyun *********************************/
1896*4882a593Smuzhiyun FORCE_INLINE
ZSTD_compressBlock_lazy_generic(ZSTD_CCtx * ctx,const void * src,size_t srcSize,const U32 searchMethod,const U32 depth)1897*4882a593Smuzhiyun void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx *ctx, const void *src, size_t srcSize, const U32 searchMethod, const U32 depth)
1898*4882a593Smuzhiyun {
1899*4882a593Smuzhiyun seqStore_t *seqStorePtr = &(ctx->seqStore);
1900*4882a593Smuzhiyun const BYTE *const istart = (const BYTE *)src;
1901*4882a593Smuzhiyun const BYTE *ip = istart;
1902*4882a593Smuzhiyun const BYTE *anchor = istart;
1903*4882a593Smuzhiyun const BYTE *const iend = istart + srcSize;
1904*4882a593Smuzhiyun const BYTE *const ilimit = iend - 8;
1905*4882a593Smuzhiyun const BYTE *const base = ctx->base + ctx->dictLimit;
1906*4882a593Smuzhiyun
1907*4882a593Smuzhiyun U32 const maxSearches = 1 << ctx->params.cParams.searchLog;
1908*4882a593Smuzhiyun U32 const mls = ctx->params.cParams.searchLength;
1909*4882a593Smuzhiyun
1910*4882a593Smuzhiyun typedef size_t (*searchMax_f)(ZSTD_CCtx * zc, const BYTE *ip, const BYTE *iLimit, size_t *offsetPtr, U32 maxNbAttempts, U32 matchLengthSearch);
1911*4882a593Smuzhiyun searchMax_f const searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS : ZSTD_HcFindBestMatch_selectMLS;
1912*4882a593Smuzhiyun U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1], savedOffset = 0;
1913*4882a593Smuzhiyun
1914*4882a593Smuzhiyun /* init */
1915*4882a593Smuzhiyun ip += (ip == base);
1916*4882a593Smuzhiyun ctx->nextToUpdate3 = ctx->nextToUpdate;
1917*4882a593Smuzhiyun {
1918*4882a593Smuzhiyun U32 const maxRep = (U32)(ip - base);
1919*4882a593Smuzhiyun if (offset_2 > maxRep)
1920*4882a593Smuzhiyun savedOffset = offset_2, offset_2 = 0;
1921*4882a593Smuzhiyun if (offset_1 > maxRep)
1922*4882a593Smuzhiyun savedOffset = offset_1, offset_1 = 0;
1923*4882a593Smuzhiyun }
1924*4882a593Smuzhiyun
1925*4882a593Smuzhiyun /* Match Loop */
1926*4882a593Smuzhiyun while (ip < ilimit) {
1927*4882a593Smuzhiyun size_t matchLength = 0;
1928*4882a593Smuzhiyun size_t offset = 0;
1929*4882a593Smuzhiyun const BYTE *start = ip + 1;
1930*4882a593Smuzhiyun
1931*4882a593Smuzhiyun /* check repCode */
1932*4882a593Smuzhiyun if ((offset_1 > 0) & (ZSTD_read32(ip + 1) == ZSTD_read32(ip + 1 - offset_1))) {
1933*4882a593Smuzhiyun /* repcode : we take it */
1934*4882a593Smuzhiyun matchLength = ZSTD_count(ip + 1 + EQUAL_READ32, ip + 1 + EQUAL_READ32 - offset_1, iend) + EQUAL_READ32;
1935*4882a593Smuzhiyun if (depth == 0)
1936*4882a593Smuzhiyun goto _storeSequence;
1937*4882a593Smuzhiyun }
1938*4882a593Smuzhiyun
1939*4882a593Smuzhiyun /* first search (depth 0) */
1940*4882a593Smuzhiyun {
1941*4882a593Smuzhiyun size_t offsetFound = 99999999;
1942*4882a593Smuzhiyun size_t const ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls);
1943*4882a593Smuzhiyun if (ml2 > matchLength)
1944*4882a593Smuzhiyun matchLength = ml2, start = ip, offset = offsetFound;
1945*4882a593Smuzhiyun }
1946*4882a593Smuzhiyun
1947*4882a593Smuzhiyun if (matchLength < EQUAL_READ32) {
1948*4882a593Smuzhiyun ip += ((ip - anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */
1949*4882a593Smuzhiyun continue;
1950*4882a593Smuzhiyun }
1951*4882a593Smuzhiyun
1952*4882a593Smuzhiyun /* let's try to find a better solution */
1953*4882a593Smuzhiyun if (depth >= 1)
1954*4882a593Smuzhiyun while (ip < ilimit) {
1955*4882a593Smuzhiyun ip++;
1956*4882a593Smuzhiyun if ((offset) && ((offset_1 > 0) & (ZSTD_read32(ip) == ZSTD_read32(ip - offset_1)))) {
1957*4882a593Smuzhiyun size_t const mlRep = ZSTD_count(ip + EQUAL_READ32, ip + EQUAL_READ32 - offset_1, iend) + EQUAL_READ32;
1958*4882a593Smuzhiyun int const gain2 = (int)(mlRep * 3);
1959*4882a593Smuzhiyun int const gain1 = (int)(matchLength * 3 - ZSTD_highbit32((U32)offset + 1) + 1);
1960*4882a593Smuzhiyun if ((mlRep >= EQUAL_READ32) && (gain2 > gain1))
1961*4882a593Smuzhiyun matchLength = mlRep, offset = 0, start = ip;
1962*4882a593Smuzhiyun }
1963*4882a593Smuzhiyun {
1964*4882a593Smuzhiyun size_t offset2 = 99999999;
1965*4882a593Smuzhiyun size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls);
1966*4882a593Smuzhiyun int const gain2 = (int)(ml2 * 4 - ZSTD_highbit32((U32)offset2 + 1)); /* raw approx */
1967*4882a593Smuzhiyun int const gain1 = (int)(matchLength * 4 - ZSTD_highbit32((U32)offset + 1) + 4);
1968*4882a593Smuzhiyun if ((ml2 >= EQUAL_READ32) && (gain2 > gain1)) {
1969*4882a593Smuzhiyun matchLength = ml2, offset = offset2, start = ip;
1970*4882a593Smuzhiyun continue; /* search a better one */
1971*4882a593Smuzhiyun }
1972*4882a593Smuzhiyun }
1973*4882a593Smuzhiyun
1974*4882a593Smuzhiyun /* let's find an even better one */
1975*4882a593Smuzhiyun if ((depth == 2) && (ip < ilimit)) {
1976*4882a593Smuzhiyun ip++;
1977*4882a593Smuzhiyun if ((offset) && ((offset_1 > 0) & (ZSTD_read32(ip) == ZSTD_read32(ip - offset_1)))) {
1978*4882a593Smuzhiyun size_t const ml2 = ZSTD_count(ip + EQUAL_READ32, ip + EQUAL_READ32 - offset_1, iend) + EQUAL_READ32;
1979*4882a593Smuzhiyun int const gain2 = (int)(ml2 * 4);
1980*4882a593Smuzhiyun int const gain1 = (int)(matchLength * 4 - ZSTD_highbit32((U32)offset + 1) + 1);
1981*4882a593Smuzhiyun if ((ml2 >= EQUAL_READ32) && (gain2 > gain1))
1982*4882a593Smuzhiyun matchLength = ml2, offset = 0, start = ip;
1983*4882a593Smuzhiyun }
1984*4882a593Smuzhiyun {
1985*4882a593Smuzhiyun size_t offset2 = 99999999;
1986*4882a593Smuzhiyun size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls);
1987*4882a593Smuzhiyun int const gain2 = (int)(ml2 * 4 - ZSTD_highbit32((U32)offset2 + 1)); /* raw approx */
1988*4882a593Smuzhiyun int const gain1 = (int)(matchLength * 4 - ZSTD_highbit32((U32)offset + 1) + 7);
1989*4882a593Smuzhiyun if ((ml2 >= EQUAL_READ32) && (gain2 > gain1)) {
1990*4882a593Smuzhiyun matchLength = ml2, offset = offset2, start = ip;
1991*4882a593Smuzhiyun continue;
1992*4882a593Smuzhiyun }
1993*4882a593Smuzhiyun }
1994*4882a593Smuzhiyun }
1995*4882a593Smuzhiyun break; /* nothing found : store previous solution */
1996*4882a593Smuzhiyun }
1997*4882a593Smuzhiyun
1998*4882a593Smuzhiyun /* NOTE:
1999*4882a593Smuzhiyun * start[-offset+ZSTD_REP_MOVE-1] is undefined behavior.
2000*4882a593Smuzhiyun * (-offset+ZSTD_REP_MOVE-1) is unsigned, and is added to start, which
2001*4882a593Smuzhiyun * overflows the pointer, which is undefined behavior.
2002*4882a593Smuzhiyun */
2003*4882a593Smuzhiyun /* catch up */
2004*4882a593Smuzhiyun if (offset) {
2005*4882a593Smuzhiyun while ((start > anchor) && (start > base + offset - ZSTD_REP_MOVE) &&
2006*4882a593Smuzhiyun (start[-1] == (start-offset+ZSTD_REP_MOVE)[-1])) /* only search for offset within prefix */
2007*4882a593Smuzhiyun {
2008*4882a593Smuzhiyun start--;
2009*4882a593Smuzhiyun matchLength++;
2010*4882a593Smuzhiyun }
2011*4882a593Smuzhiyun offset_2 = offset_1;
2012*4882a593Smuzhiyun offset_1 = (U32)(offset - ZSTD_REP_MOVE);
2013*4882a593Smuzhiyun }
2014*4882a593Smuzhiyun
2015*4882a593Smuzhiyun /* store sequence */
2016*4882a593Smuzhiyun _storeSequence:
2017*4882a593Smuzhiyun {
2018*4882a593Smuzhiyun size_t const litLength = start - anchor;
2019*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, litLength, anchor, (U32)offset, matchLength - MINMATCH);
2020*4882a593Smuzhiyun anchor = ip = start + matchLength;
2021*4882a593Smuzhiyun }
2022*4882a593Smuzhiyun
2023*4882a593Smuzhiyun /* check immediate repcode */
2024*4882a593Smuzhiyun while ((ip <= ilimit) && ((offset_2 > 0) & (ZSTD_read32(ip) == ZSTD_read32(ip - offset_2)))) {
2025*4882a593Smuzhiyun /* store sequence */
2026*4882a593Smuzhiyun matchLength = ZSTD_count(ip + EQUAL_READ32, ip + EQUAL_READ32 - offset_2, iend) + EQUAL_READ32;
2027*4882a593Smuzhiyun offset = offset_2;
2028*4882a593Smuzhiyun offset_2 = offset_1;
2029*4882a593Smuzhiyun offset_1 = (U32)offset; /* swap repcodes */
2030*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, matchLength - MINMATCH);
2031*4882a593Smuzhiyun ip += matchLength;
2032*4882a593Smuzhiyun anchor = ip;
2033*4882a593Smuzhiyun continue; /* faster when present ... (?) */
2034*4882a593Smuzhiyun }
2035*4882a593Smuzhiyun }
2036*4882a593Smuzhiyun
2037*4882a593Smuzhiyun /* Save reps for next block */
2038*4882a593Smuzhiyun ctx->repToConfirm[0] = offset_1 ? offset_1 : savedOffset;
2039*4882a593Smuzhiyun ctx->repToConfirm[1] = offset_2 ? offset_2 : savedOffset;
2040*4882a593Smuzhiyun
2041*4882a593Smuzhiyun /* Last Literals */
2042*4882a593Smuzhiyun {
2043*4882a593Smuzhiyun size_t const lastLLSize = iend - anchor;
2044*4882a593Smuzhiyun memcpy(seqStorePtr->lit, anchor, lastLLSize);
2045*4882a593Smuzhiyun seqStorePtr->lit += lastLLSize;
2046*4882a593Smuzhiyun }
2047*4882a593Smuzhiyun }
2048*4882a593Smuzhiyun
ZSTD_compressBlock_btlazy2(ZSTD_CCtx * ctx,const void * src,size_t srcSize)2049*4882a593Smuzhiyun static void ZSTD_compressBlock_btlazy2(ZSTD_CCtx *ctx, const void *src, size_t srcSize) { ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 1, 2); }
2050*4882a593Smuzhiyun
ZSTD_compressBlock_lazy2(ZSTD_CCtx * ctx,const void * src,size_t srcSize)2051*4882a593Smuzhiyun static void ZSTD_compressBlock_lazy2(ZSTD_CCtx *ctx, const void *src, size_t srcSize) { ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 2); }
2052*4882a593Smuzhiyun
ZSTD_compressBlock_lazy(ZSTD_CCtx * ctx,const void * src,size_t srcSize)2053*4882a593Smuzhiyun static void ZSTD_compressBlock_lazy(ZSTD_CCtx *ctx, const void *src, size_t srcSize) { ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 1); }
2054*4882a593Smuzhiyun
ZSTD_compressBlock_greedy(ZSTD_CCtx * ctx,const void * src,size_t srcSize)2055*4882a593Smuzhiyun static void ZSTD_compressBlock_greedy(ZSTD_CCtx *ctx, const void *src, size_t srcSize) { ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 0); }
2056*4882a593Smuzhiyun
2057*4882a593Smuzhiyun FORCE_INLINE
ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx * ctx,const void * src,size_t srcSize,const U32 searchMethod,const U32 depth)2058*4882a593Smuzhiyun void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx *ctx, const void *src, size_t srcSize, const U32 searchMethod, const U32 depth)
2059*4882a593Smuzhiyun {
2060*4882a593Smuzhiyun seqStore_t *seqStorePtr = &(ctx->seqStore);
2061*4882a593Smuzhiyun const BYTE *const istart = (const BYTE *)src;
2062*4882a593Smuzhiyun const BYTE *ip = istart;
2063*4882a593Smuzhiyun const BYTE *anchor = istart;
2064*4882a593Smuzhiyun const BYTE *const iend = istart + srcSize;
2065*4882a593Smuzhiyun const BYTE *const ilimit = iend - 8;
2066*4882a593Smuzhiyun const BYTE *const base = ctx->base;
2067*4882a593Smuzhiyun const U32 dictLimit = ctx->dictLimit;
2068*4882a593Smuzhiyun const U32 lowestIndex = ctx->lowLimit;
2069*4882a593Smuzhiyun const BYTE *const prefixStart = base + dictLimit;
2070*4882a593Smuzhiyun const BYTE *const dictBase = ctx->dictBase;
2071*4882a593Smuzhiyun const BYTE *const dictEnd = dictBase + dictLimit;
2072*4882a593Smuzhiyun const BYTE *const dictStart = dictBase + ctx->lowLimit;
2073*4882a593Smuzhiyun
2074*4882a593Smuzhiyun const U32 maxSearches = 1 << ctx->params.cParams.searchLog;
2075*4882a593Smuzhiyun const U32 mls = ctx->params.cParams.searchLength;
2076*4882a593Smuzhiyun
2077*4882a593Smuzhiyun typedef size_t (*searchMax_f)(ZSTD_CCtx * zc, const BYTE *ip, const BYTE *iLimit, size_t *offsetPtr, U32 maxNbAttempts, U32 matchLengthSearch);
2078*4882a593Smuzhiyun searchMax_f searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS_extDict : ZSTD_HcFindBestMatch_extDict_selectMLS;
2079*4882a593Smuzhiyun
2080*4882a593Smuzhiyun U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1];
2081*4882a593Smuzhiyun
2082*4882a593Smuzhiyun /* init */
2083*4882a593Smuzhiyun ctx->nextToUpdate3 = ctx->nextToUpdate;
2084*4882a593Smuzhiyun ip += (ip == prefixStart);
2085*4882a593Smuzhiyun
2086*4882a593Smuzhiyun /* Match Loop */
2087*4882a593Smuzhiyun while (ip < ilimit) {
2088*4882a593Smuzhiyun size_t matchLength = 0;
2089*4882a593Smuzhiyun size_t offset = 0;
2090*4882a593Smuzhiyun const BYTE *start = ip + 1;
2091*4882a593Smuzhiyun U32 curr = (U32)(ip - base);
2092*4882a593Smuzhiyun
2093*4882a593Smuzhiyun /* check repCode */
2094*4882a593Smuzhiyun {
2095*4882a593Smuzhiyun const U32 repIndex = (U32)(curr + 1 - offset_1);
2096*4882a593Smuzhiyun const BYTE *const repBase = repIndex < dictLimit ? dictBase : base;
2097*4882a593Smuzhiyun const BYTE *const repMatch = repBase + repIndex;
2098*4882a593Smuzhiyun if (((U32)((dictLimit - 1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
2099*4882a593Smuzhiyun if (ZSTD_read32(ip + 1) == ZSTD_read32(repMatch)) {
2100*4882a593Smuzhiyun /* repcode detected we should take it */
2101*4882a593Smuzhiyun const BYTE *const repEnd = repIndex < dictLimit ? dictEnd : iend;
2102*4882a593Smuzhiyun matchLength =
2103*4882a593Smuzhiyun ZSTD_count_2segments(ip + 1 + EQUAL_READ32, repMatch + EQUAL_READ32, iend, repEnd, prefixStart) + EQUAL_READ32;
2104*4882a593Smuzhiyun if (depth == 0)
2105*4882a593Smuzhiyun goto _storeSequence;
2106*4882a593Smuzhiyun }
2107*4882a593Smuzhiyun }
2108*4882a593Smuzhiyun
2109*4882a593Smuzhiyun /* first search (depth 0) */
2110*4882a593Smuzhiyun {
2111*4882a593Smuzhiyun size_t offsetFound = 99999999;
2112*4882a593Smuzhiyun size_t const ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls);
2113*4882a593Smuzhiyun if (ml2 > matchLength)
2114*4882a593Smuzhiyun matchLength = ml2, start = ip, offset = offsetFound;
2115*4882a593Smuzhiyun }
2116*4882a593Smuzhiyun
2117*4882a593Smuzhiyun if (matchLength < EQUAL_READ32) {
2118*4882a593Smuzhiyun ip += ((ip - anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */
2119*4882a593Smuzhiyun continue;
2120*4882a593Smuzhiyun }
2121*4882a593Smuzhiyun
2122*4882a593Smuzhiyun /* let's try to find a better solution */
2123*4882a593Smuzhiyun if (depth >= 1)
2124*4882a593Smuzhiyun while (ip < ilimit) {
2125*4882a593Smuzhiyun ip++;
2126*4882a593Smuzhiyun curr++;
2127*4882a593Smuzhiyun /* check repCode */
2128*4882a593Smuzhiyun if (offset) {
2129*4882a593Smuzhiyun const U32 repIndex = (U32)(curr - offset_1);
2130*4882a593Smuzhiyun const BYTE *const repBase = repIndex < dictLimit ? dictBase : base;
2131*4882a593Smuzhiyun const BYTE *const repMatch = repBase + repIndex;
2132*4882a593Smuzhiyun if (((U32)((dictLimit - 1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
2133*4882a593Smuzhiyun if (ZSTD_read32(ip) == ZSTD_read32(repMatch)) {
2134*4882a593Smuzhiyun /* repcode detected */
2135*4882a593Smuzhiyun const BYTE *const repEnd = repIndex < dictLimit ? dictEnd : iend;
2136*4882a593Smuzhiyun size_t const repLength =
2137*4882a593Smuzhiyun ZSTD_count_2segments(ip + EQUAL_READ32, repMatch + EQUAL_READ32, iend, repEnd, prefixStart) +
2138*4882a593Smuzhiyun EQUAL_READ32;
2139*4882a593Smuzhiyun int const gain2 = (int)(repLength * 3);
2140*4882a593Smuzhiyun int const gain1 = (int)(matchLength * 3 - ZSTD_highbit32((U32)offset + 1) + 1);
2141*4882a593Smuzhiyun if ((repLength >= EQUAL_READ32) && (gain2 > gain1))
2142*4882a593Smuzhiyun matchLength = repLength, offset = 0, start = ip;
2143*4882a593Smuzhiyun }
2144*4882a593Smuzhiyun }
2145*4882a593Smuzhiyun
2146*4882a593Smuzhiyun /* search match, depth 1 */
2147*4882a593Smuzhiyun {
2148*4882a593Smuzhiyun size_t offset2 = 99999999;
2149*4882a593Smuzhiyun size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls);
2150*4882a593Smuzhiyun int const gain2 = (int)(ml2 * 4 - ZSTD_highbit32((U32)offset2 + 1)); /* raw approx */
2151*4882a593Smuzhiyun int const gain1 = (int)(matchLength * 4 - ZSTD_highbit32((U32)offset + 1) + 4);
2152*4882a593Smuzhiyun if ((ml2 >= EQUAL_READ32) && (gain2 > gain1)) {
2153*4882a593Smuzhiyun matchLength = ml2, offset = offset2, start = ip;
2154*4882a593Smuzhiyun continue; /* search a better one */
2155*4882a593Smuzhiyun }
2156*4882a593Smuzhiyun }
2157*4882a593Smuzhiyun
2158*4882a593Smuzhiyun /* let's find an even better one */
2159*4882a593Smuzhiyun if ((depth == 2) && (ip < ilimit)) {
2160*4882a593Smuzhiyun ip++;
2161*4882a593Smuzhiyun curr++;
2162*4882a593Smuzhiyun /* check repCode */
2163*4882a593Smuzhiyun if (offset) {
2164*4882a593Smuzhiyun const U32 repIndex = (U32)(curr - offset_1);
2165*4882a593Smuzhiyun const BYTE *const repBase = repIndex < dictLimit ? dictBase : base;
2166*4882a593Smuzhiyun const BYTE *const repMatch = repBase + repIndex;
2167*4882a593Smuzhiyun if (((U32)((dictLimit - 1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
2168*4882a593Smuzhiyun if (ZSTD_read32(ip) == ZSTD_read32(repMatch)) {
2169*4882a593Smuzhiyun /* repcode detected */
2170*4882a593Smuzhiyun const BYTE *const repEnd = repIndex < dictLimit ? dictEnd : iend;
2171*4882a593Smuzhiyun size_t repLength = ZSTD_count_2segments(ip + EQUAL_READ32, repMatch + EQUAL_READ32, iend,
2172*4882a593Smuzhiyun repEnd, prefixStart) +
2173*4882a593Smuzhiyun EQUAL_READ32;
2174*4882a593Smuzhiyun int gain2 = (int)(repLength * 4);
2175*4882a593Smuzhiyun int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((U32)offset + 1) + 1);
2176*4882a593Smuzhiyun if ((repLength >= EQUAL_READ32) && (gain2 > gain1))
2177*4882a593Smuzhiyun matchLength = repLength, offset = 0, start = ip;
2178*4882a593Smuzhiyun }
2179*4882a593Smuzhiyun }
2180*4882a593Smuzhiyun
2181*4882a593Smuzhiyun /* search match, depth 2 */
2182*4882a593Smuzhiyun {
2183*4882a593Smuzhiyun size_t offset2 = 99999999;
2184*4882a593Smuzhiyun size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls);
2185*4882a593Smuzhiyun int const gain2 = (int)(ml2 * 4 - ZSTD_highbit32((U32)offset2 + 1)); /* raw approx */
2186*4882a593Smuzhiyun int const gain1 = (int)(matchLength * 4 - ZSTD_highbit32((U32)offset + 1) + 7);
2187*4882a593Smuzhiyun if ((ml2 >= EQUAL_READ32) && (gain2 > gain1)) {
2188*4882a593Smuzhiyun matchLength = ml2, offset = offset2, start = ip;
2189*4882a593Smuzhiyun continue;
2190*4882a593Smuzhiyun }
2191*4882a593Smuzhiyun }
2192*4882a593Smuzhiyun }
2193*4882a593Smuzhiyun break; /* nothing found : store previous solution */
2194*4882a593Smuzhiyun }
2195*4882a593Smuzhiyun
2196*4882a593Smuzhiyun /* catch up */
2197*4882a593Smuzhiyun if (offset) {
2198*4882a593Smuzhiyun U32 const matchIndex = (U32)((start - base) - (offset - ZSTD_REP_MOVE));
2199*4882a593Smuzhiyun const BYTE *match = (matchIndex < dictLimit) ? dictBase + matchIndex : base + matchIndex;
2200*4882a593Smuzhiyun const BYTE *const mStart = (matchIndex < dictLimit) ? dictStart : prefixStart;
2201*4882a593Smuzhiyun while ((start > anchor) && (match > mStart) && (start[-1] == match[-1])) {
2202*4882a593Smuzhiyun start--;
2203*4882a593Smuzhiyun match--;
2204*4882a593Smuzhiyun matchLength++;
2205*4882a593Smuzhiyun } /* catch up */
2206*4882a593Smuzhiyun offset_2 = offset_1;
2207*4882a593Smuzhiyun offset_1 = (U32)(offset - ZSTD_REP_MOVE);
2208*4882a593Smuzhiyun }
2209*4882a593Smuzhiyun
2210*4882a593Smuzhiyun /* store sequence */
2211*4882a593Smuzhiyun _storeSequence : {
2212*4882a593Smuzhiyun size_t const litLength = start - anchor;
2213*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, litLength, anchor, (U32)offset, matchLength - MINMATCH);
2214*4882a593Smuzhiyun anchor = ip = start + matchLength;
2215*4882a593Smuzhiyun }
2216*4882a593Smuzhiyun
2217*4882a593Smuzhiyun /* check immediate repcode */
2218*4882a593Smuzhiyun while (ip <= ilimit) {
2219*4882a593Smuzhiyun const U32 repIndex = (U32)((ip - base) - offset_2);
2220*4882a593Smuzhiyun const BYTE *const repBase = repIndex < dictLimit ? dictBase : base;
2221*4882a593Smuzhiyun const BYTE *const repMatch = repBase + repIndex;
2222*4882a593Smuzhiyun if (((U32)((dictLimit - 1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
2223*4882a593Smuzhiyun if (ZSTD_read32(ip) == ZSTD_read32(repMatch)) {
2224*4882a593Smuzhiyun /* repcode detected we should take it */
2225*4882a593Smuzhiyun const BYTE *const repEnd = repIndex < dictLimit ? dictEnd : iend;
2226*4882a593Smuzhiyun matchLength =
2227*4882a593Smuzhiyun ZSTD_count_2segments(ip + EQUAL_READ32, repMatch + EQUAL_READ32, iend, repEnd, prefixStart) + EQUAL_READ32;
2228*4882a593Smuzhiyun offset = offset_2;
2229*4882a593Smuzhiyun offset_2 = offset_1;
2230*4882a593Smuzhiyun offset_1 = (U32)offset; /* swap offset history */
2231*4882a593Smuzhiyun ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, matchLength - MINMATCH);
2232*4882a593Smuzhiyun ip += matchLength;
2233*4882a593Smuzhiyun anchor = ip;
2234*4882a593Smuzhiyun continue; /* faster when present ... (?) */
2235*4882a593Smuzhiyun }
2236*4882a593Smuzhiyun break;
2237*4882a593Smuzhiyun }
2238*4882a593Smuzhiyun }
2239*4882a593Smuzhiyun
2240*4882a593Smuzhiyun /* Save reps for next block */
2241*4882a593Smuzhiyun ctx->repToConfirm[0] = offset_1;
2242*4882a593Smuzhiyun ctx->repToConfirm[1] = offset_2;
2243*4882a593Smuzhiyun
2244*4882a593Smuzhiyun /* Last Literals */
2245*4882a593Smuzhiyun {
2246*4882a593Smuzhiyun size_t const lastLLSize = iend - anchor;
2247*4882a593Smuzhiyun memcpy(seqStorePtr->lit, anchor, lastLLSize);
2248*4882a593Smuzhiyun seqStorePtr->lit += lastLLSize;
2249*4882a593Smuzhiyun }
2250*4882a593Smuzhiyun }
2251*4882a593Smuzhiyun
ZSTD_compressBlock_greedy_extDict(ZSTD_CCtx * ctx,const void * src,size_t srcSize)2252*4882a593Smuzhiyun void ZSTD_compressBlock_greedy_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize) { ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 0); }
2253*4882a593Smuzhiyun
ZSTD_compressBlock_lazy_extDict(ZSTD_CCtx * ctx,const void * src,size_t srcSize)2254*4882a593Smuzhiyun static void ZSTD_compressBlock_lazy_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
2255*4882a593Smuzhiyun {
2256*4882a593Smuzhiyun ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 1);
2257*4882a593Smuzhiyun }
2258*4882a593Smuzhiyun
ZSTD_compressBlock_lazy2_extDict(ZSTD_CCtx * ctx,const void * src,size_t srcSize)2259*4882a593Smuzhiyun static void ZSTD_compressBlock_lazy2_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
2260*4882a593Smuzhiyun {
2261*4882a593Smuzhiyun ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 2);
2262*4882a593Smuzhiyun }
2263*4882a593Smuzhiyun
ZSTD_compressBlock_btlazy2_extDict(ZSTD_CCtx * ctx,const void * src,size_t srcSize)2264*4882a593Smuzhiyun static void ZSTD_compressBlock_btlazy2_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
2265*4882a593Smuzhiyun {
2266*4882a593Smuzhiyun ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 1, 2);
2267*4882a593Smuzhiyun }
2268*4882a593Smuzhiyun
2269*4882a593Smuzhiyun /* The optimal parser */
2270*4882a593Smuzhiyun #include "zstd_opt.h"
2271*4882a593Smuzhiyun
ZSTD_compressBlock_btopt(ZSTD_CCtx * ctx,const void * src,size_t srcSize)2272*4882a593Smuzhiyun static void ZSTD_compressBlock_btopt(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
2273*4882a593Smuzhiyun {
2274*4882a593Smuzhiyun #ifdef ZSTD_OPT_H_91842398743
2275*4882a593Smuzhiyun ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 0);
2276*4882a593Smuzhiyun #else
2277*4882a593Smuzhiyun (void)ctx;
2278*4882a593Smuzhiyun (void)src;
2279*4882a593Smuzhiyun (void)srcSize;
2280*4882a593Smuzhiyun return;
2281*4882a593Smuzhiyun #endif
2282*4882a593Smuzhiyun }
2283*4882a593Smuzhiyun
ZSTD_compressBlock_btopt2(ZSTD_CCtx * ctx,const void * src,size_t srcSize)2284*4882a593Smuzhiyun static void ZSTD_compressBlock_btopt2(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
2285*4882a593Smuzhiyun {
2286*4882a593Smuzhiyun #ifdef ZSTD_OPT_H_91842398743
2287*4882a593Smuzhiyun ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 1);
2288*4882a593Smuzhiyun #else
2289*4882a593Smuzhiyun (void)ctx;
2290*4882a593Smuzhiyun (void)src;
2291*4882a593Smuzhiyun (void)srcSize;
2292*4882a593Smuzhiyun return;
2293*4882a593Smuzhiyun #endif
2294*4882a593Smuzhiyun }
2295*4882a593Smuzhiyun
ZSTD_compressBlock_btopt_extDict(ZSTD_CCtx * ctx,const void * src,size_t srcSize)2296*4882a593Smuzhiyun static void ZSTD_compressBlock_btopt_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
2297*4882a593Smuzhiyun {
2298*4882a593Smuzhiyun #ifdef ZSTD_OPT_H_91842398743
2299*4882a593Smuzhiyun ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 0);
2300*4882a593Smuzhiyun #else
2301*4882a593Smuzhiyun (void)ctx;
2302*4882a593Smuzhiyun (void)src;
2303*4882a593Smuzhiyun (void)srcSize;
2304*4882a593Smuzhiyun return;
2305*4882a593Smuzhiyun #endif
2306*4882a593Smuzhiyun }
2307*4882a593Smuzhiyun
ZSTD_compressBlock_btopt2_extDict(ZSTD_CCtx * ctx,const void * src,size_t srcSize)2308*4882a593Smuzhiyun static void ZSTD_compressBlock_btopt2_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
2309*4882a593Smuzhiyun {
2310*4882a593Smuzhiyun #ifdef ZSTD_OPT_H_91842398743
2311*4882a593Smuzhiyun ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 1);
2312*4882a593Smuzhiyun #else
2313*4882a593Smuzhiyun (void)ctx;
2314*4882a593Smuzhiyun (void)src;
2315*4882a593Smuzhiyun (void)srcSize;
2316*4882a593Smuzhiyun return;
2317*4882a593Smuzhiyun #endif
2318*4882a593Smuzhiyun }
2319*4882a593Smuzhiyun
2320*4882a593Smuzhiyun typedef void (*ZSTD_blockCompressor)(ZSTD_CCtx *ctx, const void *src, size_t srcSize);
2321*4882a593Smuzhiyun
ZSTD_selectBlockCompressor(ZSTD_strategy strat,int extDict)2322*4882a593Smuzhiyun static ZSTD_blockCompressor ZSTD_selectBlockCompressor(ZSTD_strategy strat, int extDict)
2323*4882a593Smuzhiyun {
2324*4882a593Smuzhiyun static const ZSTD_blockCompressor blockCompressor[2][8] = {
2325*4882a593Smuzhiyun {ZSTD_compressBlock_fast, ZSTD_compressBlock_doubleFast, ZSTD_compressBlock_greedy, ZSTD_compressBlock_lazy, ZSTD_compressBlock_lazy2,
2326*4882a593Smuzhiyun ZSTD_compressBlock_btlazy2, ZSTD_compressBlock_btopt, ZSTD_compressBlock_btopt2},
2327*4882a593Smuzhiyun {ZSTD_compressBlock_fast_extDict, ZSTD_compressBlock_doubleFast_extDict, ZSTD_compressBlock_greedy_extDict, ZSTD_compressBlock_lazy_extDict,
2328*4882a593Smuzhiyun ZSTD_compressBlock_lazy2_extDict, ZSTD_compressBlock_btlazy2_extDict, ZSTD_compressBlock_btopt_extDict, ZSTD_compressBlock_btopt2_extDict}};
2329*4882a593Smuzhiyun
2330*4882a593Smuzhiyun return blockCompressor[extDict][(U32)strat];
2331*4882a593Smuzhiyun }
2332*4882a593Smuzhiyun
ZSTD_compressBlock_internal(ZSTD_CCtx * zc,void * dst,size_t dstCapacity,const void * src,size_t srcSize)2333*4882a593Smuzhiyun static size_t ZSTD_compressBlock_internal(ZSTD_CCtx *zc, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
2334*4882a593Smuzhiyun {
2335*4882a593Smuzhiyun ZSTD_blockCompressor const blockCompressor = ZSTD_selectBlockCompressor(zc->params.cParams.strategy, zc->lowLimit < zc->dictLimit);
2336*4882a593Smuzhiyun const BYTE *const base = zc->base;
2337*4882a593Smuzhiyun const BYTE *const istart = (const BYTE *)src;
2338*4882a593Smuzhiyun const U32 curr = (U32)(istart - base);
2339*4882a593Smuzhiyun if (srcSize < MIN_CBLOCK_SIZE + ZSTD_blockHeaderSize + 1)
2340*4882a593Smuzhiyun return 0; /* don't even attempt compression below a certain srcSize */
2341*4882a593Smuzhiyun ZSTD_resetSeqStore(&(zc->seqStore));
2342*4882a593Smuzhiyun if (curr > zc->nextToUpdate + 384)
2343*4882a593Smuzhiyun zc->nextToUpdate = curr - MIN(192, (U32)(curr - zc->nextToUpdate - 384)); /* update tree not updated after finding very long rep matches */
2344*4882a593Smuzhiyun blockCompressor(zc, src, srcSize);
2345*4882a593Smuzhiyun return ZSTD_compressSequences(zc, dst, dstCapacity, srcSize);
2346*4882a593Smuzhiyun }
2347*4882a593Smuzhiyun
2348*4882a593Smuzhiyun /*! ZSTD_compress_generic() :
2349*4882a593Smuzhiyun * Compress a chunk of data into one or multiple blocks.
2350*4882a593Smuzhiyun * All blocks will be terminated, all input will be consumed.
2351*4882a593Smuzhiyun * Function will issue an error if there is not enough `dstCapacity` to hold the compressed content.
2352*4882a593Smuzhiyun * Frame is supposed already started (header already produced)
2353*4882a593Smuzhiyun * @return : compressed size, or an error code
2354*4882a593Smuzhiyun */
ZSTD_compress_generic(ZSTD_CCtx * cctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize,U32 lastFrameChunk)2355*4882a593Smuzhiyun static size_t ZSTD_compress_generic(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, U32 lastFrameChunk)
2356*4882a593Smuzhiyun {
2357*4882a593Smuzhiyun size_t blockSize = cctx->blockSize;
2358*4882a593Smuzhiyun size_t remaining = srcSize;
2359*4882a593Smuzhiyun const BYTE *ip = (const BYTE *)src;
2360*4882a593Smuzhiyun BYTE *const ostart = (BYTE *)dst;
2361*4882a593Smuzhiyun BYTE *op = ostart;
2362*4882a593Smuzhiyun U32 const maxDist = 1 << cctx->params.cParams.windowLog;
2363*4882a593Smuzhiyun
2364*4882a593Smuzhiyun if (cctx->params.fParams.checksumFlag && srcSize)
2365*4882a593Smuzhiyun xxh64_update(&cctx->xxhState, src, srcSize);
2366*4882a593Smuzhiyun
2367*4882a593Smuzhiyun while (remaining) {
2368*4882a593Smuzhiyun U32 const lastBlock = lastFrameChunk & (blockSize >= remaining);
2369*4882a593Smuzhiyun size_t cSize;
2370*4882a593Smuzhiyun
2371*4882a593Smuzhiyun if (dstCapacity < ZSTD_blockHeaderSize + MIN_CBLOCK_SIZE)
2372*4882a593Smuzhiyun return ERROR(dstSize_tooSmall); /* not enough space to store compressed block */
2373*4882a593Smuzhiyun if (remaining < blockSize)
2374*4882a593Smuzhiyun blockSize = remaining;
2375*4882a593Smuzhiyun
2376*4882a593Smuzhiyun /* preemptive overflow correction */
2377*4882a593Smuzhiyun if (cctx->lowLimit > (3U << 29)) {
2378*4882a593Smuzhiyun U32 const cycleMask = (1 << ZSTD_cycleLog(cctx->params.cParams.hashLog, cctx->params.cParams.strategy)) - 1;
2379*4882a593Smuzhiyun U32 const curr = (U32)(ip - cctx->base);
2380*4882a593Smuzhiyun U32 const newCurr = (curr & cycleMask) + (1 << cctx->params.cParams.windowLog);
2381*4882a593Smuzhiyun U32 const correction = curr - newCurr;
2382*4882a593Smuzhiyun ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX_64 <= 30);
2383*4882a593Smuzhiyun ZSTD_reduceIndex(cctx, correction);
2384*4882a593Smuzhiyun cctx->base += correction;
2385*4882a593Smuzhiyun cctx->dictBase += correction;
2386*4882a593Smuzhiyun cctx->lowLimit -= correction;
2387*4882a593Smuzhiyun cctx->dictLimit -= correction;
2388*4882a593Smuzhiyun if (cctx->nextToUpdate < correction)
2389*4882a593Smuzhiyun cctx->nextToUpdate = 0;
2390*4882a593Smuzhiyun else
2391*4882a593Smuzhiyun cctx->nextToUpdate -= correction;
2392*4882a593Smuzhiyun }
2393*4882a593Smuzhiyun
2394*4882a593Smuzhiyun if ((U32)(ip + blockSize - cctx->base) > cctx->loadedDictEnd + maxDist) {
2395*4882a593Smuzhiyun /* enforce maxDist */
2396*4882a593Smuzhiyun U32 const newLowLimit = (U32)(ip + blockSize - cctx->base) - maxDist;
2397*4882a593Smuzhiyun if (cctx->lowLimit < newLowLimit)
2398*4882a593Smuzhiyun cctx->lowLimit = newLowLimit;
2399*4882a593Smuzhiyun if (cctx->dictLimit < cctx->lowLimit)
2400*4882a593Smuzhiyun cctx->dictLimit = cctx->lowLimit;
2401*4882a593Smuzhiyun }
2402*4882a593Smuzhiyun
2403*4882a593Smuzhiyun cSize = ZSTD_compressBlock_internal(cctx, op + ZSTD_blockHeaderSize, dstCapacity - ZSTD_blockHeaderSize, ip, blockSize);
2404*4882a593Smuzhiyun if (ZSTD_isError(cSize))
2405*4882a593Smuzhiyun return cSize;
2406*4882a593Smuzhiyun
2407*4882a593Smuzhiyun if (cSize == 0) { /* block is not compressible */
2408*4882a593Smuzhiyun U32 const cBlockHeader24 = lastBlock + (((U32)bt_raw) << 1) + (U32)(blockSize << 3);
2409*4882a593Smuzhiyun if (blockSize + ZSTD_blockHeaderSize > dstCapacity)
2410*4882a593Smuzhiyun return ERROR(dstSize_tooSmall);
2411*4882a593Smuzhiyun ZSTD_writeLE32(op, cBlockHeader24); /* no pb, 4th byte will be overwritten */
2412*4882a593Smuzhiyun memcpy(op + ZSTD_blockHeaderSize, ip, blockSize);
2413*4882a593Smuzhiyun cSize = ZSTD_blockHeaderSize + blockSize;
2414*4882a593Smuzhiyun } else {
2415*4882a593Smuzhiyun U32 const cBlockHeader24 = lastBlock + (((U32)bt_compressed) << 1) + (U32)(cSize << 3);
2416*4882a593Smuzhiyun ZSTD_writeLE24(op, cBlockHeader24);
2417*4882a593Smuzhiyun cSize += ZSTD_blockHeaderSize;
2418*4882a593Smuzhiyun }
2419*4882a593Smuzhiyun
2420*4882a593Smuzhiyun remaining -= blockSize;
2421*4882a593Smuzhiyun dstCapacity -= cSize;
2422*4882a593Smuzhiyun ip += blockSize;
2423*4882a593Smuzhiyun op += cSize;
2424*4882a593Smuzhiyun }
2425*4882a593Smuzhiyun
2426*4882a593Smuzhiyun if (lastFrameChunk && (op > ostart))
2427*4882a593Smuzhiyun cctx->stage = ZSTDcs_ending;
2428*4882a593Smuzhiyun return op - ostart;
2429*4882a593Smuzhiyun }
2430*4882a593Smuzhiyun
ZSTD_writeFrameHeader(void * dst,size_t dstCapacity,ZSTD_parameters params,U64 pledgedSrcSize,U32 dictID)2431*4882a593Smuzhiyun static size_t ZSTD_writeFrameHeader(void *dst, size_t dstCapacity, ZSTD_parameters params, U64 pledgedSrcSize, U32 dictID)
2432*4882a593Smuzhiyun {
2433*4882a593Smuzhiyun BYTE *const op = (BYTE *)dst;
2434*4882a593Smuzhiyun U32 const dictIDSizeCode = (dictID > 0) + (dictID >= 256) + (dictID >= 65536); /* 0-3 */
2435*4882a593Smuzhiyun U32 const checksumFlag = params.fParams.checksumFlag > 0;
2436*4882a593Smuzhiyun U32 const windowSize = 1U << params.cParams.windowLog;
2437*4882a593Smuzhiyun U32 const singleSegment = params.fParams.contentSizeFlag && (windowSize >= pledgedSrcSize);
2438*4882a593Smuzhiyun BYTE const windowLogByte = (BYTE)((params.cParams.windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN) << 3);
2439*4882a593Smuzhiyun U32 const fcsCode =
2440*4882a593Smuzhiyun params.fParams.contentSizeFlag ? (pledgedSrcSize >= 256) + (pledgedSrcSize >= 65536 + 256) + (pledgedSrcSize >= 0xFFFFFFFFU) : 0; /* 0-3 */
2441*4882a593Smuzhiyun BYTE const frameHeaderDecriptionByte = (BYTE)(dictIDSizeCode + (checksumFlag << 2) + (singleSegment << 5) + (fcsCode << 6));
2442*4882a593Smuzhiyun size_t pos;
2443*4882a593Smuzhiyun
2444*4882a593Smuzhiyun if (dstCapacity < ZSTD_frameHeaderSize_max)
2445*4882a593Smuzhiyun return ERROR(dstSize_tooSmall);
2446*4882a593Smuzhiyun
2447*4882a593Smuzhiyun ZSTD_writeLE32(dst, ZSTD_MAGICNUMBER);
2448*4882a593Smuzhiyun op[4] = frameHeaderDecriptionByte;
2449*4882a593Smuzhiyun pos = 5;
2450*4882a593Smuzhiyun if (!singleSegment)
2451*4882a593Smuzhiyun op[pos++] = windowLogByte;
2452*4882a593Smuzhiyun switch (dictIDSizeCode) {
2453*4882a593Smuzhiyun default: /* impossible */
2454*4882a593Smuzhiyun case 0: break;
2455*4882a593Smuzhiyun case 1:
2456*4882a593Smuzhiyun op[pos] = (BYTE)(dictID);
2457*4882a593Smuzhiyun pos++;
2458*4882a593Smuzhiyun break;
2459*4882a593Smuzhiyun case 2:
2460*4882a593Smuzhiyun ZSTD_writeLE16(op + pos, (U16)dictID);
2461*4882a593Smuzhiyun pos += 2;
2462*4882a593Smuzhiyun break;
2463*4882a593Smuzhiyun case 3:
2464*4882a593Smuzhiyun ZSTD_writeLE32(op + pos, dictID);
2465*4882a593Smuzhiyun pos += 4;
2466*4882a593Smuzhiyun break;
2467*4882a593Smuzhiyun }
2468*4882a593Smuzhiyun switch (fcsCode) {
2469*4882a593Smuzhiyun default: /* impossible */
2470*4882a593Smuzhiyun case 0:
2471*4882a593Smuzhiyun if (singleSegment)
2472*4882a593Smuzhiyun op[pos++] = (BYTE)(pledgedSrcSize);
2473*4882a593Smuzhiyun break;
2474*4882a593Smuzhiyun case 1:
2475*4882a593Smuzhiyun ZSTD_writeLE16(op + pos, (U16)(pledgedSrcSize - 256));
2476*4882a593Smuzhiyun pos += 2;
2477*4882a593Smuzhiyun break;
2478*4882a593Smuzhiyun case 2:
2479*4882a593Smuzhiyun ZSTD_writeLE32(op + pos, (U32)(pledgedSrcSize));
2480*4882a593Smuzhiyun pos += 4;
2481*4882a593Smuzhiyun break;
2482*4882a593Smuzhiyun case 3:
2483*4882a593Smuzhiyun ZSTD_writeLE64(op + pos, (U64)(pledgedSrcSize));
2484*4882a593Smuzhiyun pos += 8;
2485*4882a593Smuzhiyun break;
2486*4882a593Smuzhiyun }
2487*4882a593Smuzhiyun return pos;
2488*4882a593Smuzhiyun }
2489*4882a593Smuzhiyun
ZSTD_compressContinue_internal(ZSTD_CCtx * cctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize,U32 frame,U32 lastFrameChunk)2490*4882a593Smuzhiyun static size_t ZSTD_compressContinue_internal(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, U32 frame, U32 lastFrameChunk)
2491*4882a593Smuzhiyun {
2492*4882a593Smuzhiyun const BYTE *const ip = (const BYTE *)src;
2493*4882a593Smuzhiyun size_t fhSize = 0;
2494*4882a593Smuzhiyun
2495*4882a593Smuzhiyun if (cctx->stage == ZSTDcs_created)
2496*4882a593Smuzhiyun return ERROR(stage_wrong); /* missing init (ZSTD_compressBegin) */
2497*4882a593Smuzhiyun
2498*4882a593Smuzhiyun if (frame && (cctx->stage == ZSTDcs_init)) {
2499*4882a593Smuzhiyun fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->params, cctx->frameContentSize, cctx->dictID);
2500*4882a593Smuzhiyun if (ZSTD_isError(fhSize))
2501*4882a593Smuzhiyun return fhSize;
2502*4882a593Smuzhiyun dstCapacity -= fhSize;
2503*4882a593Smuzhiyun dst = (char *)dst + fhSize;
2504*4882a593Smuzhiyun cctx->stage = ZSTDcs_ongoing;
2505*4882a593Smuzhiyun }
2506*4882a593Smuzhiyun
2507*4882a593Smuzhiyun /* Check if blocks follow each other */
2508*4882a593Smuzhiyun if (src != cctx->nextSrc) {
2509*4882a593Smuzhiyun /* not contiguous */
2510*4882a593Smuzhiyun ptrdiff_t const delta = cctx->nextSrc - ip;
2511*4882a593Smuzhiyun cctx->lowLimit = cctx->dictLimit;
2512*4882a593Smuzhiyun cctx->dictLimit = (U32)(cctx->nextSrc - cctx->base);
2513*4882a593Smuzhiyun cctx->dictBase = cctx->base;
2514*4882a593Smuzhiyun cctx->base -= delta;
2515*4882a593Smuzhiyun cctx->nextToUpdate = cctx->dictLimit;
2516*4882a593Smuzhiyun if (cctx->dictLimit - cctx->lowLimit < HASH_READ_SIZE)
2517*4882a593Smuzhiyun cctx->lowLimit = cctx->dictLimit; /* too small extDict */
2518*4882a593Smuzhiyun }
2519*4882a593Smuzhiyun
2520*4882a593Smuzhiyun /* if input and dictionary overlap : reduce dictionary (area presumed modified by input) */
2521*4882a593Smuzhiyun if ((ip + srcSize > cctx->dictBase + cctx->lowLimit) & (ip < cctx->dictBase + cctx->dictLimit)) {
2522*4882a593Smuzhiyun ptrdiff_t const highInputIdx = (ip + srcSize) - cctx->dictBase;
2523*4882a593Smuzhiyun U32 const lowLimitMax = (highInputIdx > (ptrdiff_t)cctx->dictLimit) ? cctx->dictLimit : (U32)highInputIdx;
2524*4882a593Smuzhiyun cctx->lowLimit = lowLimitMax;
2525*4882a593Smuzhiyun }
2526*4882a593Smuzhiyun
2527*4882a593Smuzhiyun cctx->nextSrc = ip + srcSize;
2528*4882a593Smuzhiyun
2529*4882a593Smuzhiyun if (srcSize) {
2530*4882a593Smuzhiyun size_t const cSize = frame ? ZSTD_compress_generic(cctx, dst, dstCapacity, src, srcSize, lastFrameChunk)
2531*4882a593Smuzhiyun : ZSTD_compressBlock_internal(cctx, dst, dstCapacity, src, srcSize);
2532*4882a593Smuzhiyun if (ZSTD_isError(cSize))
2533*4882a593Smuzhiyun return cSize;
2534*4882a593Smuzhiyun return cSize + fhSize;
2535*4882a593Smuzhiyun } else
2536*4882a593Smuzhiyun return fhSize;
2537*4882a593Smuzhiyun }
2538*4882a593Smuzhiyun
ZSTD_compressContinue(ZSTD_CCtx * cctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)2539*4882a593Smuzhiyun size_t ZSTD_compressContinue(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
2540*4882a593Smuzhiyun {
2541*4882a593Smuzhiyun return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 1, 0);
2542*4882a593Smuzhiyun }
2543*4882a593Smuzhiyun
ZSTD_getBlockSizeMax(ZSTD_CCtx * cctx)2544*4882a593Smuzhiyun size_t ZSTD_getBlockSizeMax(ZSTD_CCtx *cctx) { return MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, 1 << cctx->params.cParams.windowLog); }
2545*4882a593Smuzhiyun
ZSTD_compressBlock(ZSTD_CCtx * cctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)2546*4882a593Smuzhiyun size_t ZSTD_compressBlock(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
2547*4882a593Smuzhiyun {
2548*4882a593Smuzhiyun size_t const blockSizeMax = ZSTD_getBlockSizeMax(cctx);
2549*4882a593Smuzhiyun if (srcSize > blockSizeMax)
2550*4882a593Smuzhiyun return ERROR(srcSize_wrong);
2551*4882a593Smuzhiyun return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 0, 0);
2552*4882a593Smuzhiyun }
2553*4882a593Smuzhiyun
2554*4882a593Smuzhiyun /*! ZSTD_loadDictionaryContent() :
2555*4882a593Smuzhiyun * @return : 0, or an error code
2556*4882a593Smuzhiyun */
ZSTD_loadDictionaryContent(ZSTD_CCtx * zc,const void * src,size_t srcSize)2557*4882a593Smuzhiyun static size_t ZSTD_loadDictionaryContent(ZSTD_CCtx *zc, const void *src, size_t srcSize)
2558*4882a593Smuzhiyun {
2559*4882a593Smuzhiyun const BYTE *const ip = (const BYTE *)src;
2560*4882a593Smuzhiyun const BYTE *const iend = ip + srcSize;
2561*4882a593Smuzhiyun
2562*4882a593Smuzhiyun /* input becomes curr prefix */
2563*4882a593Smuzhiyun zc->lowLimit = zc->dictLimit;
2564*4882a593Smuzhiyun zc->dictLimit = (U32)(zc->nextSrc - zc->base);
2565*4882a593Smuzhiyun zc->dictBase = zc->base;
2566*4882a593Smuzhiyun zc->base += ip - zc->nextSrc;
2567*4882a593Smuzhiyun zc->nextToUpdate = zc->dictLimit;
2568*4882a593Smuzhiyun zc->loadedDictEnd = zc->forceWindow ? 0 : (U32)(iend - zc->base);
2569*4882a593Smuzhiyun
2570*4882a593Smuzhiyun zc->nextSrc = iend;
2571*4882a593Smuzhiyun if (srcSize <= HASH_READ_SIZE)
2572*4882a593Smuzhiyun return 0;
2573*4882a593Smuzhiyun
2574*4882a593Smuzhiyun switch (zc->params.cParams.strategy) {
2575*4882a593Smuzhiyun case ZSTD_fast: ZSTD_fillHashTable(zc, iend, zc->params.cParams.searchLength); break;
2576*4882a593Smuzhiyun
2577*4882a593Smuzhiyun case ZSTD_dfast: ZSTD_fillDoubleHashTable(zc, iend, zc->params.cParams.searchLength); break;
2578*4882a593Smuzhiyun
2579*4882a593Smuzhiyun case ZSTD_greedy:
2580*4882a593Smuzhiyun case ZSTD_lazy:
2581*4882a593Smuzhiyun case ZSTD_lazy2:
2582*4882a593Smuzhiyun if (srcSize >= HASH_READ_SIZE)
2583*4882a593Smuzhiyun ZSTD_insertAndFindFirstIndex(zc, iend - HASH_READ_SIZE, zc->params.cParams.searchLength);
2584*4882a593Smuzhiyun break;
2585*4882a593Smuzhiyun
2586*4882a593Smuzhiyun case ZSTD_btlazy2:
2587*4882a593Smuzhiyun case ZSTD_btopt:
2588*4882a593Smuzhiyun case ZSTD_btopt2:
2589*4882a593Smuzhiyun if (srcSize >= HASH_READ_SIZE)
2590*4882a593Smuzhiyun ZSTD_updateTree(zc, iend - HASH_READ_SIZE, iend, 1 << zc->params.cParams.searchLog, zc->params.cParams.searchLength);
2591*4882a593Smuzhiyun break;
2592*4882a593Smuzhiyun
2593*4882a593Smuzhiyun default:
2594*4882a593Smuzhiyun return ERROR(GENERIC); /* strategy doesn't exist; impossible */
2595*4882a593Smuzhiyun }
2596*4882a593Smuzhiyun
2597*4882a593Smuzhiyun zc->nextToUpdate = (U32)(iend - zc->base);
2598*4882a593Smuzhiyun return 0;
2599*4882a593Smuzhiyun }
2600*4882a593Smuzhiyun
2601*4882a593Smuzhiyun /* Dictionaries that assign zero probability to symbols that show up causes problems
2602*4882a593Smuzhiyun when FSE encoding. Refuse dictionaries that assign zero probability to symbols
2603*4882a593Smuzhiyun that we may encounter during compression.
2604*4882a593Smuzhiyun NOTE: This behavior is not standard and could be improved in the future. */
ZSTD_checkDictNCount(short * normalizedCounter,unsigned dictMaxSymbolValue,unsigned maxSymbolValue)2605*4882a593Smuzhiyun static size_t ZSTD_checkDictNCount(short *normalizedCounter, unsigned dictMaxSymbolValue, unsigned maxSymbolValue)
2606*4882a593Smuzhiyun {
2607*4882a593Smuzhiyun U32 s;
2608*4882a593Smuzhiyun if (dictMaxSymbolValue < maxSymbolValue)
2609*4882a593Smuzhiyun return ERROR(dictionary_corrupted);
2610*4882a593Smuzhiyun for (s = 0; s <= maxSymbolValue; ++s) {
2611*4882a593Smuzhiyun if (normalizedCounter[s] == 0)
2612*4882a593Smuzhiyun return ERROR(dictionary_corrupted);
2613*4882a593Smuzhiyun }
2614*4882a593Smuzhiyun return 0;
2615*4882a593Smuzhiyun }
2616*4882a593Smuzhiyun
2617*4882a593Smuzhiyun /* Dictionary format :
2618*4882a593Smuzhiyun * See :
2619*4882a593Smuzhiyun * https://github.com/facebook/zstd/blob/master/doc/zstd_compression_format.md#dictionary-format
2620*4882a593Smuzhiyun */
2621*4882a593Smuzhiyun /*! ZSTD_loadZstdDictionary() :
2622*4882a593Smuzhiyun * @return : 0, or an error code
2623*4882a593Smuzhiyun * assumptions : magic number supposed already checked
2624*4882a593Smuzhiyun * dictSize supposed > 8
2625*4882a593Smuzhiyun */
ZSTD_loadZstdDictionary(ZSTD_CCtx * cctx,const void * dict,size_t dictSize)2626*4882a593Smuzhiyun static size_t ZSTD_loadZstdDictionary(ZSTD_CCtx *cctx, const void *dict, size_t dictSize)
2627*4882a593Smuzhiyun {
2628*4882a593Smuzhiyun const BYTE *dictPtr = (const BYTE *)dict;
2629*4882a593Smuzhiyun const BYTE *const dictEnd = dictPtr + dictSize;
2630*4882a593Smuzhiyun short offcodeNCount[MaxOff + 1];
2631*4882a593Smuzhiyun unsigned offcodeMaxValue = MaxOff;
2632*4882a593Smuzhiyun
2633*4882a593Smuzhiyun dictPtr += 4; /* skip magic number */
2634*4882a593Smuzhiyun cctx->dictID = cctx->params.fParams.noDictIDFlag ? 0 : ZSTD_readLE32(dictPtr);
2635*4882a593Smuzhiyun dictPtr += 4;
2636*4882a593Smuzhiyun
2637*4882a593Smuzhiyun {
2638*4882a593Smuzhiyun size_t const hufHeaderSize = HUF_readCTable_wksp(cctx->hufTable, 255, dictPtr, dictEnd - dictPtr, cctx->tmpCounters, sizeof(cctx->tmpCounters));
2639*4882a593Smuzhiyun if (HUF_isError(hufHeaderSize))
2640*4882a593Smuzhiyun return ERROR(dictionary_corrupted);
2641*4882a593Smuzhiyun dictPtr += hufHeaderSize;
2642*4882a593Smuzhiyun }
2643*4882a593Smuzhiyun
2644*4882a593Smuzhiyun {
2645*4882a593Smuzhiyun unsigned offcodeLog;
2646*4882a593Smuzhiyun size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd - dictPtr);
2647*4882a593Smuzhiyun if (FSE_isError(offcodeHeaderSize))
2648*4882a593Smuzhiyun return ERROR(dictionary_corrupted);
2649*4882a593Smuzhiyun if (offcodeLog > OffFSELog)
2650*4882a593Smuzhiyun return ERROR(dictionary_corrupted);
2651*4882a593Smuzhiyun /* Defer checking offcodeMaxValue because we need to know the size of the dictionary content */
2652*4882a593Smuzhiyun CHECK_E(FSE_buildCTable_wksp(cctx->offcodeCTable, offcodeNCount, offcodeMaxValue, offcodeLog, cctx->tmpCounters, sizeof(cctx->tmpCounters)),
2653*4882a593Smuzhiyun dictionary_corrupted);
2654*4882a593Smuzhiyun dictPtr += offcodeHeaderSize;
2655*4882a593Smuzhiyun }
2656*4882a593Smuzhiyun
2657*4882a593Smuzhiyun {
2658*4882a593Smuzhiyun short matchlengthNCount[MaxML + 1];
2659*4882a593Smuzhiyun unsigned matchlengthMaxValue = MaxML, matchlengthLog;
2660*4882a593Smuzhiyun size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd - dictPtr);
2661*4882a593Smuzhiyun if (FSE_isError(matchlengthHeaderSize))
2662*4882a593Smuzhiyun return ERROR(dictionary_corrupted);
2663*4882a593Smuzhiyun if (matchlengthLog > MLFSELog)
2664*4882a593Smuzhiyun return ERROR(dictionary_corrupted);
2665*4882a593Smuzhiyun /* Every match length code must have non-zero probability */
2666*4882a593Smuzhiyun CHECK_F(ZSTD_checkDictNCount(matchlengthNCount, matchlengthMaxValue, MaxML));
2667*4882a593Smuzhiyun CHECK_E(
2668*4882a593Smuzhiyun FSE_buildCTable_wksp(cctx->matchlengthCTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog, cctx->tmpCounters, sizeof(cctx->tmpCounters)),
2669*4882a593Smuzhiyun dictionary_corrupted);
2670*4882a593Smuzhiyun dictPtr += matchlengthHeaderSize;
2671*4882a593Smuzhiyun }
2672*4882a593Smuzhiyun
2673*4882a593Smuzhiyun {
2674*4882a593Smuzhiyun short litlengthNCount[MaxLL + 1];
2675*4882a593Smuzhiyun unsigned litlengthMaxValue = MaxLL, litlengthLog;
2676*4882a593Smuzhiyun size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd - dictPtr);
2677*4882a593Smuzhiyun if (FSE_isError(litlengthHeaderSize))
2678*4882a593Smuzhiyun return ERROR(dictionary_corrupted);
2679*4882a593Smuzhiyun if (litlengthLog > LLFSELog)
2680*4882a593Smuzhiyun return ERROR(dictionary_corrupted);
2681*4882a593Smuzhiyun /* Every literal length code must have non-zero probability */
2682*4882a593Smuzhiyun CHECK_F(ZSTD_checkDictNCount(litlengthNCount, litlengthMaxValue, MaxLL));
2683*4882a593Smuzhiyun CHECK_E(FSE_buildCTable_wksp(cctx->litlengthCTable, litlengthNCount, litlengthMaxValue, litlengthLog, cctx->tmpCounters, sizeof(cctx->tmpCounters)),
2684*4882a593Smuzhiyun dictionary_corrupted);
2685*4882a593Smuzhiyun dictPtr += litlengthHeaderSize;
2686*4882a593Smuzhiyun }
2687*4882a593Smuzhiyun
2688*4882a593Smuzhiyun if (dictPtr + 12 > dictEnd)
2689*4882a593Smuzhiyun return ERROR(dictionary_corrupted);
2690*4882a593Smuzhiyun cctx->rep[0] = ZSTD_readLE32(dictPtr + 0);
2691*4882a593Smuzhiyun cctx->rep[1] = ZSTD_readLE32(dictPtr + 4);
2692*4882a593Smuzhiyun cctx->rep[2] = ZSTD_readLE32(dictPtr + 8);
2693*4882a593Smuzhiyun dictPtr += 12;
2694*4882a593Smuzhiyun
2695*4882a593Smuzhiyun {
2696*4882a593Smuzhiyun size_t const dictContentSize = (size_t)(dictEnd - dictPtr);
2697*4882a593Smuzhiyun U32 offcodeMax = MaxOff;
2698*4882a593Smuzhiyun if (dictContentSize <= ((U32)-1) - 128 KB) {
2699*4882a593Smuzhiyun U32 const maxOffset = (U32)dictContentSize + 128 KB; /* The maximum offset that must be supported */
2700*4882a593Smuzhiyun offcodeMax = ZSTD_highbit32(maxOffset); /* Calculate minimum offset code required to represent maxOffset */
2701*4882a593Smuzhiyun }
2702*4882a593Smuzhiyun /* All offset values <= dictContentSize + 128 KB must be representable */
2703*4882a593Smuzhiyun CHECK_F(ZSTD_checkDictNCount(offcodeNCount, offcodeMaxValue, MIN(offcodeMax, MaxOff)));
2704*4882a593Smuzhiyun /* All repCodes must be <= dictContentSize and != 0*/
2705*4882a593Smuzhiyun {
2706*4882a593Smuzhiyun U32 u;
2707*4882a593Smuzhiyun for (u = 0; u < 3; u++) {
2708*4882a593Smuzhiyun if (cctx->rep[u] == 0)
2709*4882a593Smuzhiyun return ERROR(dictionary_corrupted);
2710*4882a593Smuzhiyun if (cctx->rep[u] > dictContentSize)
2711*4882a593Smuzhiyun return ERROR(dictionary_corrupted);
2712*4882a593Smuzhiyun }
2713*4882a593Smuzhiyun }
2714*4882a593Smuzhiyun
2715*4882a593Smuzhiyun cctx->flagStaticTables = 1;
2716*4882a593Smuzhiyun cctx->flagStaticHufTable = HUF_repeat_valid;
2717*4882a593Smuzhiyun return ZSTD_loadDictionaryContent(cctx, dictPtr, dictContentSize);
2718*4882a593Smuzhiyun }
2719*4882a593Smuzhiyun }
2720*4882a593Smuzhiyun
2721*4882a593Smuzhiyun /** ZSTD_compress_insertDictionary() :
2722*4882a593Smuzhiyun * @return : 0, or an error code */
ZSTD_compress_insertDictionary(ZSTD_CCtx * cctx,const void * dict,size_t dictSize)2723*4882a593Smuzhiyun static size_t ZSTD_compress_insertDictionary(ZSTD_CCtx *cctx, const void *dict, size_t dictSize)
2724*4882a593Smuzhiyun {
2725*4882a593Smuzhiyun if ((dict == NULL) || (dictSize <= 8))
2726*4882a593Smuzhiyun return 0;
2727*4882a593Smuzhiyun
2728*4882a593Smuzhiyun /* dict as pure content */
2729*4882a593Smuzhiyun if ((ZSTD_readLE32(dict) != ZSTD_DICT_MAGIC) || (cctx->forceRawDict))
2730*4882a593Smuzhiyun return ZSTD_loadDictionaryContent(cctx, dict, dictSize);
2731*4882a593Smuzhiyun
2732*4882a593Smuzhiyun /* dict as zstd dictionary */
2733*4882a593Smuzhiyun return ZSTD_loadZstdDictionary(cctx, dict, dictSize);
2734*4882a593Smuzhiyun }
2735*4882a593Smuzhiyun
2736*4882a593Smuzhiyun /*! ZSTD_compressBegin_internal() :
2737*4882a593Smuzhiyun * @return : 0, or an error code */
ZSTD_compressBegin_internal(ZSTD_CCtx * cctx,const void * dict,size_t dictSize,ZSTD_parameters params,U64 pledgedSrcSize)2738*4882a593Smuzhiyun static size_t ZSTD_compressBegin_internal(ZSTD_CCtx *cctx, const void *dict, size_t dictSize, ZSTD_parameters params, U64 pledgedSrcSize)
2739*4882a593Smuzhiyun {
2740*4882a593Smuzhiyun ZSTD_compResetPolicy_e const crp = dictSize ? ZSTDcrp_fullReset : ZSTDcrp_continue;
2741*4882a593Smuzhiyun CHECK_F(ZSTD_resetCCtx_advanced(cctx, params, pledgedSrcSize, crp));
2742*4882a593Smuzhiyun return ZSTD_compress_insertDictionary(cctx, dict, dictSize);
2743*4882a593Smuzhiyun }
2744*4882a593Smuzhiyun
2745*4882a593Smuzhiyun /*! ZSTD_compressBegin_advanced() :
2746*4882a593Smuzhiyun * @return : 0, or an error code */
ZSTD_compressBegin_advanced(ZSTD_CCtx * cctx,const void * dict,size_t dictSize,ZSTD_parameters params,unsigned long long pledgedSrcSize)2747*4882a593Smuzhiyun size_t ZSTD_compressBegin_advanced(ZSTD_CCtx *cctx, const void *dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize)
2748*4882a593Smuzhiyun {
2749*4882a593Smuzhiyun /* compression parameters verification and optimization */
2750*4882a593Smuzhiyun CHECK_F(ZSTD_checkCParams(params.cParams));
2751*4882a593Smuzhiyun return ZSTD_compressBegin_internal(cctx, dict, dictSize, params, pledgedSrcSize);
2752*4882a593Smuzhiyun }
2753*4882a593Smuzhiyun
ZSTD_compressBegin_usingDict(ZSTD_CCtx * cctx,const void * dict,size_t dictSize,int compressionLevel)2754*4882a593Smuzhiyun size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx *cctx, const void *dict, size_t dictSize, int compressionLevel)
2755*4882a593Smuzhiyun {
2756*4882a593Smuzhiyun ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize);
2757*4882a593Smuzhiyun return ZSTD_compressBegin_internal(cctx, dict, dictSize, params, 0);
2758*4882a593Smuzhiyun }
2759*4882a593Smuzhiyun
ZSTD_compressBegin(ZSTD_CCtx * cctx,int compressionLevel)2760*4882a593Smuzhiyun size_t ZSTD_compressBegin(ZSTD_CCtx *cctx, int compressionLevel) { return ZSTD_compressBegin_usingDict(cctx, NULL, 0, compressionLevel); }
2761*4882a593Smuzhiyun
2762*4882a593Smuzhiyun /*! ZSTD_writeEpilogue() :
2763*4882a593Smuzhiyun * Ends a frame.
2764*4882a593Smuzhiyun * @return : nb of bytes written into dst (or an error code) */
ZSTD_writeEpilogue(ZSTD_CCtx * cctx,void * dst,size_t dstCapacity)2765*4882a593Smuzhiyun static size_t ZSTD_writeEpilogue(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity)
2766*4882a593Smuzhiyun {
2767*4882a593Smuzhiyun BYTE *const ostart = (BYTE *)dst;
2768*4882a593Smuzhiyun BYTE *op = ostart;
2769*4882a593Smuzhiyun size_t fhSize = 0;
2770*4882a593Smuzhiyun
2771*4882a593Smuzhiyun if (cctx->stage == ZSTDcs_created)
2772*4882a593Smuzhiyun return ERROR(stage_wrong); /* init missing */
2773*4882a593Smuzhiyun
2774*4882a593Smuzhiyun /* special case : empty frame */
2775*4882a593Smuzhiyun if (cctx->stage == ZSTDcs_init) {
2776*4882a593Smuzhiyun fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->params, 0, 0);
2777*4882a593Smuzhiyun if (ZSTD_isError(fhSize))
2778*4882a593Smuzhiyun return fhSize;
2779*4882a593Smuzhiyun dstCapacity -= fhSize;
2780*4882a593Smuzhiyun op += fhSize;
2781*4882a593Smuzhiyun cctx->stage = ZSTDcs_ongoing;
2782*4882a593Smuzhiyun }
2783*4882a593Smuzhiyun
2784*4882a593Smuzhiyun if (cctx->stage != ZSTDcs_ending) {
2785*4882a593Smuzhiyun /* write one last empty block, make it the "last" block */
2786*4882a593Smuzhiyun U32 const cBlockHeader24 = 1 /* last block */ + (((U32)bt_raw) << 1) + 0;
2787*4882a593Smuzhiyun if (dstCapacity < 4)
2788*4882a593Smuzhiyun return ERROR(dstSize_tooSmall);
2789*4882a593Smuzhiyun ZSTD_writeLE32(op, cBlockHeader24);
2790*4882a593Smuzhiyun op += ZSTD_blockHeaderSize;
2791*4882a593Smuzhiyun dstCapacity -= ZSTD_blockHeaderSize;
2792*4882a593Smuzhiyun }
2793*4882a593Smuzhiyun
2794*4882a593Smuzhiyun if (cctx->params.fParams.checksumFlag) {
2795*4882a593Smuzhiyun U32 const checksum = (U32)xxh64_digest(&cctx->xxhState);
2796*4882a593Smuzhiyun if (dstCapacity < 4)
2797*4882a593Smuzhiyun return ERROR(dstSize_tooSmall);
2798*4882a593Smuzhiyun ZSTD_writeLE32(op, checksum);
2799*4882a593Smuzhiyun op += 4;
2800*4882a593Smuzhiyun }
2801*4882a593Smuzhiyun
2802*4882a593Smuzhiyun cctx->stage = ZSTDcs_created; /* return to "created but no init" status */
2803*4882a593Smuzhiyun return op - ostart;
2804*4882a593Smuzhiyun }
2805*4882a593Smuzhiyun
ZSTD_compressEnd(ZSTD_CCtx * cctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)2806*4882a593Smuzhiyun size_t ZSTD_compressEnd(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
2807*4882a593Smuzhiyun {
2808*4882a593Smuzhiyun size_t endResult;
2809*4882a593Smuzhiyun size_t const cSize = ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 1, 1);
2810*4882a593Smuzhiyun if (ZSTD_isError(cSize))
2811*4882a593Smuzhiyun return cSize;
2812*4882a593Smuzhiyun endResult = ZSTD_writeEpilogue(cctx, (char *)dst + cSize, dstCapacity - cSize);
2813*4882a593Smuzhiyun if (ZSTD_isError(endResult))
2814*4882a593Smuzhiyun return endResult;
2815*4882a593Smuzhiyun return cSize + endResult;
2816*4882a593Smuzhiyun }
2817*4882a593Smuzhiyun
ZSTD_compress_internal(ZSTD_CCtx * cctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize,const void * dict,size_t dictSize,ZSTD_parameters params)2818*4882a593Smuzhiyun static size_t ZSTD_compress_internal(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, const void *dict, size_t dictSize,
2819*4882a593Smuzhiyun ZSTD_parameters params)
2820*4882a593Smuzhiyun {
2821*4882a593Smuzhiyun CHECK_F(ZSTD_compressBegin_internal(cctx, dict, dictSize, params, srcSize));
2822*4882a593Smuzhiyun return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize);
2823*4882a593Smuzhiyun }
2824*4882a593Smuzhiyun
ZSTD_compress_usingDict(ZSTD_CCtx * ctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize,const void * dict,size_t dictSize,ZSTD_parameters params)2825*4882a593Smuzhiyun size_t ZSTD_compress_usingDict(ZSTD_CCtx *ctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, const void *dict, size_t dictSize,
2826*4882a593Smuzhiyun ZSTD_parameters params)
2827*4882a593Smuzhiyun {
2828*4882a593Smuzhiyun return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params);
2829*4882a593Smuzhiyun }
2830*4882a593Smuzhiyun
ZSTD_compressCCtx(ZSTD_CCtx * ctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize,ZSTD_parameters params)2831*4882a593Smuzhiyun size_t ZSTD_compressCCtx(ZSTD_CCtx *ctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, ZSTD_parameters params)
2832*4882a593Smuzhiyun {
2833*4882a593Smuzhiyun return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, NULL, 0, params);
2834*4882a593Smuzhiyun }
2835*4882a593Smuzhiyun
2836*4882a593Smuzhiyun /* ===== Dictionary API ===== */
2837*4882a593Smuzhiyun
2838*4882a593Smuzhiyun struct ZSTD_CDict_s {
2839*4882a593Smuzhiyun void *dictBuffer;
2840*4882a593Smuzhiyun const void *dictContent;
2841*4882a593Smuzhiyun size_t dictContentSize;
2842*4882a593Smuzhiyun ZSTD_CCtx *refContext;
2843*4882a593Smuzhiyun }; /* typedef'd tp ZSTD_CDict within "zstd.h" */
2844*4882a593Smuzhiyun
ZSTD_CDictWorkspaceBound(ZSTD_compressionParameters cParams)2845*4882a593Smuzhiyun size_t ZSTD_CDictWorkspaceBound(ZSTD_compressionParameters cParams) { return ZSTD_CCtxWorkspaceBound(cParams) + ZSTD_ALIGN(sizeof(ZSTD_CDict)); }
2846*4882a593Smuzhiyun
ZSTD_createCDict_advanced(const void * dictBuffer,size_t dictSize,unsigned byReference,ZSTD_parameters params,ZSTD_customMem customMem)2847*4882a593Smuzhiyun static ZSTD_CDict *ZSTD_createCDict_advanced(const void *dictBuffer, size_t dictSize, unsigned byReference, ZSTD_parameters params, ZSTD_customMem customMem)
2848*4882a593Smuzhiyun {
2849*4882a593Smuzhiyun if (!customMem.customAlloc || !customMem.customFree)
2850*4882a593Smuzhiyun return NULL;
2851*4882a593Smuzhiyun
2852*4882a593Smuzhiyun {
2853*4882a593Smuzhiyun ZSTD_CDict *const cdict = (ZSTD_CDict *)ZSTD_malloc(sizeof(ZSTD_CDict), customMem);
2854*4882a593Smuzhiyun ZSTD_CCtx *const cctx = ZSTD_createCCtx_advanced(customMem);
2855*4882a593Smuzhiyun
2856*4882a593Smuzhiyun if (!cdict || !cctx) {
2857*4882a593Smuzhiyun ZSTD_free(cdict, customMem);
2858*4882a593Smuzhiyun ZSTD_freeCCtx(cctx);
2859*4882a593Smuzhiyun return NULL;
2860*4882a593Smuzhiyun }
2861*4882a593Smuzhiyun
2862*4882a593Smuzhiyun if ((byReference) || (!dictBuffer) || (!dictSize)) {
2863*4882a593Smuzhiyun cdict->dictBuffer = NULL;
2864*4882a593Smuzhiyun cdict->dictContent = dictBuffer;
2865*4882a593Smuzhiyun } else {
2866*4882a593Smuzhiyun void *const internalBuffer = ZSTD_malloc(dictSize, customMem);
2867*4882a593Smuzhiyun if (!internalBuffer) {
2868*4882a593Smuzhiyun ZSTD_free(cctx, customMem);
2869*4882a593Smuzhiyun ZSTD_free(cdict, customMem);
2870*4882a593Smuzhiyun return NULL;
2871*4882a593Smuzhiyun }
2872*4882a593Smuzhiyun memcpy(internalBuffer, dictBuffer, dictSize);
2873*4882a593Smuzhiyun cdict->dictBuffer = internalBuffer;
2874*4882a593Smuzhiyun cdict->dictContent = internalBuffer;
2875*4882a593Smuzhiyun }
2876*4882a593Smuzhiyun
2877*4882a593Smuzhiyun {
2878*4882a593Smuzhiyun size_t const errorCode = ZSTD_compressBegin_advanced(cctx, cdict->dictContent, dictSize, params, 0);
2879*4882a593Smuzhiyun if (ZSTD_isError(errorCode)) {
2880*4882a593Smuzhiyun ZSTD_free(cdict->dictBuffer, customMem);
2881*4882a593Smuzhiyun ZSTD_free(cdict, customMem);
2882*4882a593Smuzhiyun ZSTD_freeCCtx(cctx);
2883*4882a593Smuzhiyun return NULL;
2884*4882a593Smuzhiyun }
2885*4882a593Smuzhiyun }
2886*4882a593Smuzhiyun
2887*4882a593Smuzhiyun cdict->refContext = cctx;
2888*4882a593Smuzhiyun cdict->dictContentSize = dictSize;
2889*4882a593Smuzhiyun return cdict;
2890*4882a593Smuzhiyun }
2891*4882a593Smuzhiyun }
2892*4882a593Smuzhiyun
ZSTD_initCDict(const void * dict,size_t dictSize,ZSTD_parameters params,void * workspace,size_t workspaceSize)2893*4882a593Smuzhiyun ZSTD_CDict *ZSTD_initCDict(const void *dict, size_t dictSize, ZSTD_parameters params, void *workspace, size_t workspaceSize)
2894*4882a593Smuzhiyun {
2895*4882a593Smuzhiyun ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
2896*4882a593Smuzhiyun return ZSTD_createCDict_advanced(dict, dictSize, 1, params, stackMem);
2897*4882a593Smuzhiyun }
2898*4882a593Smuzhiyun
ZSTD_freeCDict(ZSTD_CDict * cdict)2899*4882a593Smuzhiyun size_t ZSTD_freeCDict(ZSTD_CDict *cdict)
2900*4882a593Smuzhiyun {
2901*4882a593Smuzhiyun if (cdict == NULL)
2902*4882a593Smuzhiyun return 0; /* support free on NULL */
2903*4882a593Smuzhiyun {
2904*4882a593Smuzhiyun ZSTD_customMem const cMem = cdict->refContext->customMem;
2905*4882a593Smuzhiyun ZSTD_freeCCtx(cdict->refContext);
2906*4882a593Smuzhiyun ZSTD_free(cdict->dictBuffer, cMem);
2907*4882a593Smuzhiyun ZSTD_free(cdict, cMem);
2908*4882a593Smuzhiyun return 0;
2909*4882a593Smuzhiyun }
2910*4882a593Smuzhiyun }
2911*4882a593Smuzhiyun
ZSTD_getParamsFromCDict(const ZSTD_CDict * cdict)2912*4882a593Smuzhiyun static ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict *cdict) { return ZSTD_getParamsFromCCtx(cdict->refContext); }
2913*4882a593Smuzhiyun
ZSTD_compressBegin_usingCDict(ZSTD_CCtx * cctx,const ZSTD_CDict * cdict,unsigned long long pledgedSrcSize)2914*4882a593Smuzhiyun size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx *cctx, const ZSTD_CDict *cdict, unsigned long long pledgedSrcSize)
2915*4882a593Smuzhiyun {
2916*4882a593Smuzhiyun if (cdict->dictContentSize)
2917*4882a593Smuzhiyun CHECK_F(ZSTD_copyCCtx(cctx, cdict->refContext, pledgedSrcSize))
2918*4882a593Smuzhiyun else {
2919*4882a593Smuzhiyun ZSTD_parameters params = cdict->refContext->params;
2920*4882a593Smuzhiyun params.fParams.contentSizeFlag = (pledgedSrcSize > 0);
2921*4882a593Smuzhiyun CHECK_F(ZSTD_compressBegin_advanced(cctx, NULL, 0, params, pledgedSrcSize));
2922*4882a593Smuzhiyun }
2923*4882a593Smuzhiyun return 0;
2924*4882a593Smuzhiyun }
2925*4882a593Smuzhiyun
2926*4882a593Smuzhiyun /*! ZSTD_compress_usingCDict() :
2927*4882a593Smuzhiyun * Compression using a digested Dictionary.
2928*4882a593Smuzhiyun * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times.
2929*4882a593Smuzhiyun * Note that compression level is decided during dictionary creation */
ZSTD_compress_usingCDict(ZSTD_CCtx * cctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize,const ZSTD_CDict * cdict)2930*4882a593Smuzhiyun size_t ZSTD_compress_usingCDict(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, const ZSTD_CDict *cdict)
2931*4882a593Smuzhiyun {
2932*4882a593Smuzhiyun CHECK_F(ZSTD_compressBegin_usingCDict(cctx, cdict, srcSize));
2933*4882a593Smuzhiyun
2934*4882a593Smuzhiyun if (cdict->refContext->params.fParams.contentSizeFlag == 1) {
2935*4882a593Smuzhiyun cctx->params.fParams.contentSizeFlag = 1;
2936*4882a593Smuzhiyun cctx->frameContentSize = srcSize;
2937*4882a593Smuzhiyun } else {
2938*4882a593Smuzhiyun cctx->params.fParams.contentSizeFlag = 0;
2939*4882a593Smuzhiyun }
2940*4882a593Smuzhiyun
2941*4882a593Smuzhiyun return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize);
2942*4882a593Smuzhiyun }
2943*4882a593Smuzhiyun
2944*4882a593Smuzhiyun /* ******************************************************************
2945*4882a593Smuzhiyun * Streaming
2946*4882a593Smuzhiyun ********************************************************************/
2947*4882a593Smuzhiyun
2948*4882a593Smuzhiyun typedef enum { zcss_init, zcss_load, zcss_flush, zcss_final } ZSTD_cStreamStage;
2949*4882a593Smuzhiyun
2950*4882a593Smuzhiyun struct ZSTD_CStream_s {
2951*4882a593Smuzhiyun ZSTD_CCtx *cctx;
2952*4882a593Smuzhiyun ZSTD_CDict *cdictLocal;
2953*4882a593Smuzhiyun const ZSTD_CDict *cdict;
2954*4882a593Smuzhiyun char *inBuff;
2955*4882a593Smuzhiyun size_t inBuffSize;
2956*4882a593Smuzhiyun size_t inToCompress;
2957*4882a593Smuzhiyun size_t inBuffPos;
2958*4882a593Smuzhiyun size_t inBuffTarget;
2959*4882a593Smuzhiyun size_t blockSize;
2960*4882a593Smuzhiyun char *outBuff;
2961*4882a593Smuzhiyun size_t outBuffSize;
2962*4882a593Smuzhiyun size_t outBuffContentSize;
2963*4882a593Smuzhiyun size_t outBuffFlushedSize;
2964*4882a593Smuzhiyun ZSTD_cStreamStage stage;
2965*4882a593Smuzhiyun U32 checksum;
2966*4882a593Smuzhiyun U32 frameEnded;
2967*4882a593Smuzhiyun U64 pledgedSrcSize;
2968*4882a593Smuzhiyun U64 inputProcessed;
2969*4882a593Smuzhiyun ZSTD_parameters params;
2970*4882a593Smuzhiyun ZSTD_customMem customMem;
2971*4882a593Smuzhiyun }; /* typedef'd to ZSTD_CStream within "zstd.h" */
2972*4882a593Smuzhiyun
ZSTD_CStreamWorkspaceBound(ZSTD_compressionParameters cParams)2973*4882a593Smuzhiyun size_t ZSTD_CStreamWorkspaceBound(ZSTD_compressionParameters cParams)
2974*4882a593Smuzhiyun {
2975*4882a593Smuzhiyun size_t const inBuffSize = (size_t)1 << cParams.windowLog;
2976*4882a593Smuzhiyun size_t const blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, inBuffSize);
2977*4882a593Smuzhiyun size_t const outBuffSize = ZSTD_compressBound(blockSize) + 1;
2978*4882a593Smuzhiyun
2979*4882a593Smuzhiyun return ZSTD_CCtxWorkspaceBound(cParams) + ZSTD_ALIGN(sizeof(ZSTD_CStream)) + ZSTD_ALIGN(inBuffSize) + ZSTD_ALIGN(outBuffSize);
2980*4882a593Smuzhiyun }
2981*4882a593Smuzhiyun
ZSTD_createCStream_advanced(ZSTD_customMem customMem)2982*4882a593Smuzhiyun ZSTD_CStream *ZSTD_createCStream_advanced(ZSTD_customMem customMem)
2983*4882a593Smuzhiyun {
2984*4882a593Smuzhiyun ZSTD_CStream *zcs;
2985*4882a593Smuzhiyun
2986*4882a593Smuzhiyun if (!customMem.customAlloc || !customMem.customFree)
2987*4882a593Smuzhiyun return NULL;
2988*4882a593Smuzhiyun
2989*4882a593Smuzhiyun zcs = (ZSTD_CStream *)ZSTD_malloc(sizeof(ZSTD_CStream), customMem);
2990*4882a593Smuzhiyun if (zcs == NULL)
2991*4882a593Smuzhiyun return NULL;
2992*4882a593Smuzhiyun memset(zcs, 0, sizeof(ZSTD_CStream));
2993*4882a593Smuzhiyun memcpy(&zcs->customMem, &customMem, sizeof(ZSTD_customMem));
2994*4882a593Smuzhiyun zcs->cctx = ZSTD_createCCtx_advanced(customMem);
2995*4882a593Smuzhiyun if (zcs->cctx == NULL) {
2996*4882a593Smuzhiyun ZSTD_freeCStream(zcs);
2997*4882a593Smuzhiyun return NULL;
2998*4882a593Smuzhiyun }
2999*4882a593Smuzhiyun return zcs;
3000*4882a593Smuzhiyun }
3001*4882a593Smuzhiyun
ZSTD_freeCStream(ZSTD_CStream * zcs)3002*4882a593Smuzhiyun size_t ZSTD_freeCStream(ZSTD_CStream *zcs)
3003*4882a593Smuzhiyun {
3004*4882a593Smuzhiyun if (zcs == NULL)
3005*4882a593Smuzhiyun return 0; /* support free on NULL */
3006*4882a593Smuzhiyun {
3007*4882a593Smuzhiyun ZSTD_customMem const cMem = zcs->customMem;
3008*4882a593Smuzhiyun ZSTD_freeCCtx(zcs->cctx);
3009*4882a593Smuzhiyun zcs->cctx = NULL;
3010*4882a593Smuzhiyun ZSTD_freeCDict(zcs->cdictLocal);
3011*4882a593Smuzhiyun zcs->cdictLocal = NULL;
3012*4882a593Smuzhiyun ZSTD_free(zcs->inBuff, cMem);
3013*4882a593Smuzhiyun zcs->inBuff = NULL;
3014*4882a593Smuzhiyun ZSTD_free(zcs->outBuff, cMem);
3015*4882a593Smuzhiyun zcs->outBuff = NULL;
3016*4882a593Smuzhiyun ZSTD_free(zcs, cMem);
3017*4882a593Smuzhiyun return 0;
3018*4882a593Smuzhiyun }
3019*4882a593Smuzhiyun }
3020*4882a593Smuzhiyun
3021*4882a593Smuzhiyun /*====== Initialization ======*/
3022*4882a593Smuzhiyun
ZSTD_CStreamInSize(void)3023*4882a593Smuzhiyun size_t ZSTD_CStreamInSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX; }
ZSTD_CStreamOutSize(void)3024*4882a593Smuzhiyun size_t ZSTD_CStreamOutSize(void) { return ZSTD_compressBound(ZSTD_BLOCKSIZE_ABSOLUTEMAX) + ZSTD_blockHeaderSize + 4 /* 32-bits hash */; }
3025*4882a593Smuzhiyun
ZSTD_resetCStream_internal(ZSTD_CStream * zcs,unsigned long long pledgedSrcSize)3026*4882a593Smuzhiyun static size_t ZSTD_resetCStream_internal(ZSTD_CStream *zcs, unsigned long long pledgedSrcSize)
3027*4882a593Smuzhiyun {
3028*4882a593Smuzhiyun if (zcs->inBuffSize == 0)
3029*4882a593Smuzhiyun return ERROR(stage_wrong); /* zcs has not been init at least once => can't reset */
3030*4882a593Smuzhiyun
3031*4882a593Smuzhiyun if (zcs->cdict)
3032*4882a593Smuzhiyun CHECK_F(ZSTD_compressBegin_usingCDict(zcs->cctx, zcs->cdict, pledgedSrcSize))
3033*4882a593Smuzhiyun else
3034*4882a593Smuzhiyun CHECK_F(ZSTD_compressBegin_advanced(zcs->cctx, NULL, 0, zcs->params, pledgedSrcSize));
3035*4882a593Smuzhiyun
3036*4882a593Smuzhiyun zcs->inToCompress = 0;
3037*4882a593Smuzhiyun zcs->inBuffPos = 0;
3038*4882a593Smuzhiyun zcs->inBuffTarget = zcs->blockSize;
3039*4882a593Smuzhiyun zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0;
3040*4882a593Smuzhiyun zcs->stage = zcss_load;
3041*4882a593Smuzhiyun zcs->frameEnded = 0;
3042*4882a593Smuzhiyun zcs->pledgedSrcSize = pledgedSrcSize;
3043*4882a593Smuzhiyun zcs->inputProcessed = 0;
3044*4882a593Smuzhiyun return 0; /* ready to go */
3045*4882a593Smuzhiyun }
3046*4882a593Smuzhiyun
ZSTD_resetCStream(ZSTD_CStream * zcs,unsigned long long pledgedSrcSize)3047*4882a593Smuzhiyun size_t ZSTD_resetCStream(ZSTD_CStream *zcs, unsigned long long pledgedSrcSize)
3048*4882a593Smuzhiyun {
3049*4882a593Smuzhiyun
3050*4882a593Smuzhiyun zcs->params.fParams.contentSizeFlag = (pledgedSrcSize > 0);
3051*4882a593Smuzhiyun
3052*4882a593Smuzhiyun return ZSTD_resetCStream_internal(zcs, pledgedSrcSize);
3053*4882a593Smuzhiyun }
3054*4882a593Smuzhiyun
ZSTD_initCStream_advanced(ZSTD_CStream * zcs,const void * dict,size_t dictSize,ZSTD_parameters params,unsigned long long pledgedSrcSize)3055*4882a593Smuzhiyun static size_t ZSTD_initCStream_advanced(ZSTD_CStream *zcs, const void *dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize)
3056*4882a593Smuzhiyun {
3057*4882a593Smuzhiyun /* allocate buffers */
3058*4882a593Smuzhiyun {
3059*4882a593Smuzhiyun size_t const neededInBuffSize = (size_t)1 << params.cParams.windowLog;
3060*4882a593Smuzhiyun if (zcs->inBuffSize < neededInBuffSize) {
3061*4882a593Smuzhiyun zcs->inBuffSize = neededInBuffSize;
3062*4882a593Smuzhiyun ZSTD_free(zcs->inBuff, zcs->customMem);
3063*4882a593Smuzhiyun zcs->inBuff = (char *)ZSTD_malloc(neededInBuffSize, zcs->customMem);
3064*4882a593Smuzhiyun if (zcs->inBuff == NULL)
3065*4882a593Smuzhiyun return ERROR(memory_allocation);
3066*4882a593Smuzhiyun }
3067*4882a593Smuzhiyun zcs->blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, neededInBuffSize);
3068*4882a593Smuzhiyun }
3069*4882a593Smuzhiyun if (zcs->outBuffSize < ZSTD_compressBound(zcs->blockSize) + 1) {
3070*4882a593Smuzhiyun zcs->outBuffSize = ZSTD_compressBound(zcs->blockSize) + 1;
3071*4882a593Smuzhiyun ZSTD_free(zcs->outBuff, zcs->customMem);
3072*4882a593Smuzhiyun zcs->outBuff = (char *)ZSTD_malloc(zcs->outBuffSize, zcs->customMem);
3073*4882a593Smuzhiyun if (zcs->outBuff == NULL)
3074*4882a593Smuzhiyun return ERROR(memory_allocation);
3075*4882a593Smuzhiyun }
3076*4882a593Smuzhiyun
3077*4882a593Smuzhiyun if (dict && dictSize >= 8) {
3078*4882a593Smuzhiyun ZSTD_freeCDict(zcs->cdictLocal);
3079*4882a593Smuzhiyun zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, 0, params, zcs->customMem);
3080*4882a593Smuzhiyun if (zcs->cdictLocal == NULL)
3081*4882a593Smuzhiyun return ERROR(memory_allocation);
3082*4882a593Smuzhiyun zcs->cdict = zcs->cdictLocal;
3083*4882a593Smuzhiyun } else
3084*4882a593Smuzhiyun zcs->cdict = NULL;
3085*4882a593Smuzhiyun
3086*4882a593Smuzhiyun zcs->checksum = params.fParams.checksumFlag > 0;
3087*4882a593Smuzhiyun zcs->params = params;
3088*4882a593Smuzhiyun
3089*4882a593Smuzhiyun return ZSTD_resetCStream_internal(zcs, pledgedSrcSize);
3090*4882a593Smuzhiyun }
3091*4882a593Smuzhiyun
ZSTD_initCStream(ZSTD_parameters params,unsigned long long pledgedSrcSize,void * workspace,size_t workspaceSize)3092*4882a593Smuzhiyun ZSTD_CStream *ZSTD_initCStream(ZSTD_parameters params, unsigned long long pledgedSrcSize, void *workspace, size_t workspaceSize)
3093*4882a593Smuzhiyun {
3094*4882a593Smuzhiyun ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
3095*4882a593Smuzhiyun ZSTD_CStream *const zcs = ZSTD_createCStream_advanced(stackMem);
3096*4882a593Smuzhiyun if (zcs) {
3097*4882a593Smuzhiyun size_t const code = ZSTD_initCStream_advanced(zcs, NULL, 0, params, pledgedSrcSize);
3098*4882a593Smuzhiyun if (ZSTD_isError(code)) {
3099*4882a593Smuzhiyun return NULL;
3100*4882a593Smuzhiyun }
3101*4882a593Smuzhiyun }
3102*4882a593Smuzhiyun return zcs;
3103*4882a593Smuzhiyun }
3104*4882a593Smuzhiyun
ZSTD_initCStream_usingCDict(const ZSTD_CDict * cdict,unsigned long long pledgedSrcSize,void * workspace,size_t workspaceSize)3105*4882a593Smuzhiyun ZSTD_CStream *ZSTD_initCStream_usingCDict(const ZSTD_CDict *cdict, unsigned long long pledgedSrcSize, void *workspace, size_t workspaceSize)
3106*4882a593Smuzhiyun {
3107*4882a593Smuzhiyun ZSTD_parameters const params = ZSTD_getParamsFromCDict(cdict);
3108*4882a593Smuzhiyun ZSTD_CStream *const zcs = ZSTD_initCStream(params, pledgedSrcSize, workspace, workspaceSize);
3109*4882a593Smuzhiyun if (zcs) {
3110*4882a593Smuzhiyun zcs->cdict = cdict;
3111*4882a593Smuzhiyun if (ZSTD_isError(ZSTD_resetCStream_internal(zcs, pledgedSrcSize))) {
3112*4882a593Smuzhiyun return NULL;
3113*4882a593Smuzhiyun }
3114*4882a593Smuzhiyun }
3115*4882a593Smuzhiyun return zcs;
3116*4882a593Smuzhiyun }
3117*4882a593Smuzhiyun
3118*4882a593Smuzhiyun /*====== Compression ======*/
3119*4882a593Smuzhiyun
3120*4882a593Smuzhiyun typedef enum { zsf_gather, zsf_flush, zsf_end } ZSTD_flush_e;
3121*4882a593Smuzhiyun
ZSTD_limitCopy(void * dst,size_t dstCapacity,const void * src,size_t srcSize)3122*4882a593Smuzhiyun ZSTD_STATIC size_t ZSTD_limitCopy(void *dst, size_t dstCapacity, const void *src, size_t srcSize)
3123*4882a593Smuzhiyun {
3124*4882a593Smuzhiyun size_t const length = MIN(dstCapacity, srcSize);
3125*4882a593Smuzhiyun memcpy(dst, src, length);
3126*4882a593Smuzhiyun return length;
3127*4882a593Smuzhiyun }
3128*4882a593Smuzhiyun
ZSTD_compressStream_generic(ZSTD_CStream * zcs,void * dst,size_t * dstCapacityPtr,const void * src,size_t * srcSizePtr,ZSTD_flush_e const flush)3129*4882a593Smuzhiyun static size_t ZSTD_compressStream_generic(ZSTD_CStream *zcs, void *dst, size_t *dstCapacityPtr, const void *src, size_t *srcSizePtr, ZSTD_flush_e const flush)
3130*4882a593Smuzhiyun {
3131*4882a593Smuzhiyun U32 someMoreWork = 1;
3132*4882a593Smuzhiyun const char *const istart = (const char *)src;
3133*4882a593Smuzhiyun const char *const iend = istart + *srcSizePtr;
3134*4882a593Smuzhiyun const char *ip = istart;
3135*4882a593Smuzhiyun char *const ostart = (char *)dst;
3136*4882a593Smuzhiyun char *const oend = ostart + *dstCapacityPtr;
3137*4882a593Smuzhiyun char *op = ostart;
3138*4882a593Smuzhiyun
3139*4882a593Smuzhiyun while (someMoreWork) {
3140*4882a593Smuzhiyun switch (zcs->stage) {
3141*4882a593Smuzhiyun case zcss_init:
3142*4882a593Smuzhiyun return ERROR(init_missing); /* call ZBUFF_compressInit() first ! */
3143*4882a593Smuzhiyun
3144*4882a593Smuzhiyun case zcss_load:
3145*4882a593Smuzhiyun /* complete inBuffer */
3146*4882a593Smuzhiyun {
3147*4882a593Smuzhiyun size_t const toLoad = zcs->inBuffTarget - zcs->inBuffPos;
3148*4882a593Smuzhiyun size_t const loaded = ZSTD_limitCopy(zcs->inBuff + zcs->inBuffPos, toLoad, ip, iend - ip);
3149*4882a593Smuzhiyun zcs->inBuffPos += loaded;
3150*4882a593Smuzhiyun ip += loaded;
3151*4882a593Smuzhiyun if ((zcs->inBuffPos == zcs->inToCompress) || (!flush && (toLoad != loaded))) {
3152*4882a593Smuzhiyun someMoreWork = 0;
3153*4882a593Smuzhiyun break; /* not enough input to get a full block : stop there, wait for more */
3154*4882a593Smuzhiyun }
3155*4882a593Smuzhiyun }
3156*4882a593Smuzhiyun /* compress curr block (note : this stage cannot be stopped in the middle) */
3157*4882a593Smuzhiyun {
3158*4882a593Smuzhiyun void *cDst;
3159*4882a593Smuzhiyun size_t cSize;
3160*4882a593Smuzhiyun size_t const iSize = zcs->inBuffPos - zcs->inToCompress;
3161*4882a593Smuzhiyun size_t oSize = oend - op;
3162*4882a593Smuzhiyun if (oSize >= ZSTD_compressBound(iSize))
3163*4882a593Smuzhiyun cDst = op; /* compress directly into output buffer (avoid flush stage) */
3164*4882a593Smuzhiyun else
3165*4882a593Smuzhiyun cDst = zcs->outBuff, oSize = zcs->outBuffSize;
3166*4882a593Smuzhiyun cSize = (flush == zsf_end) ? ZSTD_compressEnd(zcs->cctx, cDst, oSize, zcs->inBuff + zcs->inToCompress, iSize)
3167*4882a593Smuzhiyun : ZSTD_compressContinue(zcs->cctx, cDst, oSize, zcs->inBuff + zcs->inToCompress, iSize);
3168*4882a593Smuzhiyun if (ZSTD_isError(cSize))
3169*4882a593Smuzhiyun return cSize;
3170*4882a593Smuzhiyun if (flush == zsf_end)
3171*4882a593Smuzhiyun zcs->frameEnded = 1;
3172*4882a593Smuzhiyun /* prepare next block */
3173*4882a593Smuzhiyun zcs->inBuffTarget = zcs->inBuffPos + zcs->blockSize;
3174*4882a593Smuzhiyun if (zcs->inBuffTarget > zcs->inBuffSize)
3175*4882a593Smuzhiyun zcs->inBuffPos = 0, zcs->inBuffTarget = zcs->blockSize; /* note : inBuffSize >= blockSize */
3176*4882a593Smuzhiyun zcs->inToCompress = zcs->inBuffPos;
3177*4882a593Smuzhiyun if (cDst == op) {
3178*4882a593Smuzhiyun op += cSize;
3179*4882a593Smuzhiyun break;
3180*4882a593Smuzhiyun } /* no need to flush */
3181*4882a593Smuzhiyun zcs->outBuffContentSize = cSize;
3182*4882a593Smuzhiyun zcs->outBuffFlushedSize = 0;
3183*4882a593Smuzhiyun zcs->stage = zcss_flush; /* pass-through to flush stage */
3184*4882a593Smuzhiyun }
3185*4882a593Smuzhiyun /* fall through */
3186*4882a593Smuzhiyun
3187*4882a593Smuzhiyun case zcss_flush: {
3188*4882a593Smuzhiyun size_t const toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize;
3189*4882a593Smuzhiyun size_t const flushed = ZSTD_limitCopy(op, oend - op, zcs->outBuff + zcs->outBuffFlushedSize, toFlush);
3190*4882a593Smuzhiyun op += flushed;
3191*4882a593Smuzhiyun zcs->outBuffFlushedSize += flushed;
3192*4882a593Smuzhiyun if (toFlush != flushed) {
3193*4882a593Smuzhiyun someMoreWork = 0;
3194*4882a593Smuzhiyun break;
3195*4882a593Smuzhiyun } /* dst too small to store flushed data : stop there */
3196*4882a593Smuzhiyun zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0;
3197*4882a593Smuzhiyun zcs->stage = zcss_load;
3198*4882a593Smuzhiyun break;
3199*4882a593Smuzhiyun }
3200*4882a593Smuzhiyun
3201*4882a593Smuzhiyun case zcss_final:
3202*4882a593Smuzhiyun someMoreWork = 0; /* do nothing */
3203*4882a593Smuzhiyun break;
3204*4882a593Smuzhiyun
3205*4882a593Smuzhiyun default:
3206*4882a593Smuzhiyun return ERROR(GENERIC); /* impossible */
3207*4882a593Smuzhiyun }
3208*4882a593Smuzhiyun }
3209*4882a593Smuzhiyun
3210*4882a593Smuzhiyun *srcSizePtr = ip - istart;
3211*4882a593Smuzhiyun *dstCapacityPtr = op - ostart;
3212*4882a593Smuzhiyun zcs->inputProcessed += *srcSizePtr;
3213*4882a593Smuzhiyun if (zcs->frameEnded)
3214*4882a593Smuzhiyun return 0;
3215*4882a593Smuzhiyun {
3216*4882a593Smuzhiyun size_t hintInSize = zcs->inBuffTarget - zcs->inBuffPos;
3217*4882a593Smuzhiyun if (hintInSize == 0)
3218*4882a593Smuzhiyun hintInSize = zcs->blockSize;
3219*4882a593Smuzhiyun return hintInSize;
3220*4882a593Smuzhiyun }
3221*4882a593Smuzhiyun }
3222*4882a593Smuzhiyun
ZSTD_compressStream(ZSTD_CStream * zcs,ZSTD_outBuffer * output,ZSTD_inBuffer * input)3223*4882a593Smuzhiyun size_t ZSTD_compressStream(ZSTD_CStream *zcs, ZSTD_outBuffer *output, ZSTD_inBuffer *input)
3224*4882a593Smuzhiyun {
3225*4882a593Smuzhiyun size_t sizeRead = input->size - input->pos;
3226*4882a593Smuzhiyun size_t sizeWritten = output->size - output->pos;
3227*4882a593Smuzhiyun size_t const result =
3228*4882a593Smuzhiyun ZSTD_compressStream_generic(zcs, (char *)(output->dst) + output->pos, &sizeWritten, (const char *)(input->src) + input->pos, &sizeRead, zsf_gather);
3229*4882a593Smuzhiyun input->pos += sizeRead;
3230*4882a593Smuzhiyun output->pos += sizeWritten;
3231*4882a593Smuzhiyun return result;
3232*4882a593Smuzhiyun }
3233*4882a593Smuzhiyun
3234*4882a593Smuzhiyun /*====== Finalize ======*/
3235*4882a593Smuzhiyun
3236*4882a593Smuzhiyun /*! ZSTD_flushStream() :
3237*4882a593Smuzhiyun * @return : amount of data remaining to flush */
ZSTD_flushStream(ZSTD_CStream * zcs,ZSTD_outBuffer * output)3238*4882a593Smuzhiyun size_t ZSTD_flushStream(ZSTD_CStream *zcs, ZSTD_outBuffer *output)
3239*4882a593Smuzhiyun {
3240*4882a593Smuzhiyun size_t srcSize = 0;
3241*4882a593Smuzhiyun size_t sizeWritten = output->size - output->pos;
3242*4882a593Smuzhiyun size_t const result = ZSTD_compressStream_generic(zcs, (char *)(output->dst) + output->pos, &sizeWritten, &srcSize,
3243*4882a593Smuzhiyun &srcSize, /* use a valid src address instead of NULL */
3244*4882a593Smuzhiyun zsf_flush);
3245*4882a593Smuzhiyun output->pos += sizeWritten;
3246*4882a593Smuzhiyun if (ZSTD_isError(result))
3247*4882a593Smuzhiyun return result;
3248*4882a593Smuzhiyun return zcs->outBuffContentSize - zcs->outBuffFlushedSize; /* remaining to flush */
3249*4882a593Smuzhiyun }
3250*4882a593Smuzhiyun
ZSTD_endStream(ZSTD_CStream * zcs,ZSTD_outBuffer * output)3251*4882a593Smuzhiyun size_t ZSTD_endStream(ZSTD_CStream *zcs, ZSTD_outBuffer *output)
3252*4882a593Smuzhiyun {
3253*4882a593Smuzhiyun BYTE *const ostart = (BYTE *)(output->dst) + output->pos;
3254*4882a593Smuzhiyun BYTE *const oend = (BYTE *)(output->dst) + output->size;
3255*4882a593Smuzhiyun BYTE *op = ostart;
3256*4882a593Smuzhiyun
3257*4882a593Smuzhiyun if ((zcs->pledgedSrcSize) && (zcs->inputProcessed != zcs->pledgedSrcSize))
3258*4882a593Smuzhiyun return ERROR(srcSize_wrong); /* pledgedSrcSize not respected */
3259*4882a593Smuzhiyun
3260*4882a593Smuzhiyun if (zcs->stage != zcss_final) {
3261*4882a593Smuzhiyun /* flush whatever remains */
3262*4882a593Smuzhiyun size_t srcSize = 0;
3263*4882a593Smuzhiyun size_t sizeWritten = output->size - output->pos;
3264*4882a593Smuzhiyun size_t const notEnded =
3265*4882a593Smuzhiyun ZSTD_compressStream_generic(zcs, ostart, &sizeWritten, &srcSize, &srcSize, zsf_end); /* use a valid src address instead of NULL */
3266*4882a593Smuzhiyun size_t const remainingToFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize;
3267*4882a593Smuzhiyun op += sizeWritten;
3268*4882a593Smuzhiyun if (remainingToFlush) {
3269*4882a593Smuzhiyun output->pos += sizeWritten;
3270*4882a593Smuzhiyun return remainingToFlush + ZSTD_BLOCKHEADERSIZE /* final empty block */ + (zcs->checksum * 4);
3271*4882a593Smuzhiyun }
3272*4882a593Smuzhiyun /* create epilogue */
3273*4882a593Smuzhiyun zcs->stage = zcss_final;
3274*4882a593Smuzhiyun zcs->outBuffContentSize = !notEnded ? 0 : ZSTD_compressEnd(zcs->cctx, zcs->outBuff, zcs->outBuffSize, NULL,
3275*4882a593Smuzhiyun 0); /* write epilogue, including final empty block, into outBuff */
3276*4882a593Smuzhiyun }
3277*4882a593Smuzhiyun
3278*4882a593Smuzhiyun /* flush epilogue */
3279*4882a593Smuzhiyun {
3280*4882a593Smuzhiyun size_t const toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize;
3281*4882a593Smuzhiyun size_t const flushed = ZSTD_limitCopy(op, oend - op, zcs->outBuff + zcs->outBuffFlushedSize, toFlush);
3282*4882a593Smuzhiyun op += flushed;
3283*4882a593Smuzhiyun zcs->outBuffFlushedSize += flushed;
3284*4882a593Smuzhiyun output->pos += op - ostart;
3285*4882a593Smuzhiyun if (toFlush == flushed)
3286*4882a593Smuzhiyun zcs->stage = zcss_init; /* end reached */
3287*4882a593Smuzhiyun return toFlush - flushed;
3288*4882a593Smuzhiyun }
3289*4882a593Smuzhiyun }
3290*4882a593Smuzhiyun
3291*4882a593Smuzhiyun /*-===== Pre-defined compression levels =====-*/
3292*4882a593Smuzhiyun
3293*4882a593Smuzhiyun #define ZSTD_DEFAULT_CLEVEL 1
3294*4882a593Smuzhiyun #define ZSTD_MAX_CLEVEL 22
ZSTD_maxCLevel(void)3295*4882a593Smuzhiyun int ZSTD_maxCLevel(void) { return ZSTD_MAX_CLEVEL; }
3296*4882a593Smuzhiyun
3297*4882a593Smuzhiyun static const ZSTD_compressionParameters ZSTD_defaultCParameters[4][ZSTD_MAX_CLEVEL + 1] = {
3298*4882a593Smuzhiyun {
3299*4882a593Smuzhiyun /* "default" */
3300*4882a593Smuzhiyun /* W, C, H, S, L, TL, strat */
3301*4882a593Smuzhiyun {18, 12, 12, 1, 7, 16, ZSTD_fast}, /* level 0 - never used */
3302*4882a593Smuzhiyun {19, 13, 14, 1, 7, 16, ZSTD_fast}, /* level 1 */
3303*4882a593Smuzhiyun {19, 15, 16, 1, 6, 16, ZSTD_fast}, /* level 2 */
3304*4882a593Smuzhiyun {20, 16, 17, 1, 5, 16, ZSTD_dfast}, /* level 3.*/
3305*4882a593Smuzhiyun {20, 18, 18, 1, 5, 16, ZSTD_dfast}, /* level 4.*/
3306*4882a593Smuzhiyun {20, 15, 18, 3, 5, 16, ZSTD_greedy}, /* level 5 */
3307*4882a593Smuzhiyun {21, 16, 19, 2, 5, 16, ZSTD_lazy}, /* level 6 */
3308*4882a593Smuzhiyun {21, 17, 20, 3, 5, 16, ZSTD_lazy}, /* level 7 */
3309*4882a593Smuzhiyun {21, 18, 20, 3, 5, 16, ZSTD_lazy2}, /* level 8 */
3310*4882a593Smuzhiyun {21, 20, 20, 3, 5, 16, ZSTD_lazy2}, /* level 9 */
3311*4882a593Smuzhiyun {21, 19, 21, 4, 5, 16, ZSTD_lazy2}, /* level 10 */
3312*4882a593Smuzhiyun {22, 20, 22, 4, 5, 16, ZSTD_lazy2}, /* level 11 */
3313*4882a593Smuzhiyun {22, 20, 22, 5, 5, 16, ZSTD_lazy2}, /* level 12 */
3314*4882a593Smuzhiyun {22, 21, 22, 5, 5, 16, ZSTD_lazy2}, /* level 13 */
3315*4882a593Smuzhiyun {22, 21, 22, 6, 5, 16, ZSTD_lazy2}, /* level 14 */
3316*4882a593Smuzhiyun {22, 21, 21, 5, 5, 16, ZSTD_btlazy2}, /* level 15 */
3317*4882a593Smuzhiyun {23, 22, 22, 5, 5, 16, ZSTD_btlazy2}, /* level 16 */
3318*4882a593Smuzhiyun {23, 21, 22, 4, 5, 24, ZSTD_btopt}, /* level 17 */
3319*4882a593Smuzhiyun {23, 23, 22, 6, 5, 32, ZSTD_btopt}, /* level 18 */
3320*4882a593Smuzhiyun {23, 23, 22, 6, 3, 48, ZSTD_btopt}, /* level 19 */
3321*4882a593Smuzhiyun {25, 25, 23, 7, 3, 64, ZSTD_btopt2}, /* level 20 */
3322*4882a593Smuzhiyun {26, 26, 23, 7, 3, 256, ZSTD_btopt2}, /* level 21 */
3323*4882a593Smuzhiyun {27, 27, 25, 9, 3, 512, ZSTD_btopt2}, /* level 22 */
3324*4882a593Smuzhiyun },
3325*4882a593Smuzhiyun {
3326*4882a593Smuzhiyun /* for srcSize <= 256 KB */
3327*4882a593Smuzhiyun /* W, C, H, S, L, T, strat */
3328*4882a593Smuzhiyun {0, 0, 0, 0, 0, 0, ZSTD_fast}, /* level 0 - not used */
3329*4882a593Smuzhiyun {18, 13, 14, 1, 6, 8, ZSTD_fast}, /* level 1 */
3330*4882a593Smuzhiyun {18, 14, 13, 1, 5, 8, ZSTD_dfast}, /* level 2 */
3331*4882a593Smuzhiyun {18, 16, 15, 1, 5, 8, ZSTD_dfast}, /* level 3 */
3332*4882a593Smuzhiyun {18, 15, 17, 1, 5, 8, ZSTD_greedy}, /* level 4.*/
3333*4882a593Smuzhiyun {18, 16, 17, 4, 5, 8, ZSTD_greedy}, /* level 5.*/
3334*4882a593Smuzhiyun {18, 16, 17, 3, 5, 8, ZSTD_lazy}, /* level 6.*/
3335*4882a593Smuzhiyun {18, 17, 17, 4, 4, 8, ZSTD_lazy}, /* level 7 */
3336*4882a593Smuzhiyun {18, 17, 17, 4, 4, 8, ZSTD_lazy2}, /* level 8 */
3337*4882a593Smuzhiyun {18, 17, 17, 5, 4, 8, ZSTD_lazy2}, /* level 9 */
3338*4882a593Smuzhiyun {18, 17, 17, 6, 4, 8, ZSTD_lazy2}, /* level 10 */
3339*4882a593Smuzhiyun {18, 18, 17, 6, 4, 8, ZSTD_lazy2}, /* level 11.*/
3340*4882a593Smuzhiyun {18, 18, 17, 7, 4, 8, ZSTD_lazy2}, /* level 12.*/
3341*4882a593Smuzhiyun {18, 19, 17, 6, 4, 8, ZSTD_btlazy2}, /* level 13 */
3342*4882a593Smuzhiyun {18, 18, 18, 4, 4, 16, ZSTD_btopt}, /* level 14.*/
3343*4882a593Smuzhiyun {18, 18, 18, 4, 3, 16, ZSTD_btopt}, /* level 15.*/
3344*4882a593Smuzhiyun {18, 19, 18, 6, 3, 32, ZSTD_btopt}, /* level 16.*/
3345*4882a593Smuzhiyun {18, 19, 18, 8, 3, 64, ZSTD_btopt}, /* level 17.*/
3346*4882a593Smuzhiyun {18, 19, 18, 9, 3, 128, ZSTD_btopt}, /* level 18.*/
3347*4882a593Smuzhiyun {18, 19, 18, 10, 3, 256, ZSTD_btopt}, /* level 19.*/
3348*4882a593Smuzhiyun {18, 19, 18, 11, 3, 512, ZSTD_btopt2}, /* level 20.*/
3349*4882a593Smuzhiyun {18, 19, 18, 12, 3, 512, ZSTD_btopt2}, /* level 21.*/
3350*4882a593Smuzhiyun {18, 19, 18, 13, 3, 512, ZSTD_btopt2}, /* level 22.*/
3351*4882a593Smuzhiyun },
3352*4882a593Smuzhiyun {
3353*4882a593Smuzhiyun /* for srcSize <= 128 KB */
3354*4882a593Smuzhiyun /* W, C, H, S, L, T, strat */
3355*4882a593Smuzhiyun {17, 12, 12, 1, 7, 8, ZSTD_fast}, /* level 0 - not used */
3356*4882a593Smuzhiyun {17, 12, 13, 1, 6, 8, ZSTD_fast}, /* level 1 */
3357*4882a593Smuzhiyun {17, 13, 16, 1, 5, 8, ZSTD_fast}, /* level 2 */
3358*4882a593Smuzhiyun {17, 16, 16, 2, 5, 8, ZSTD_dfast}, /* level 3 */
3359*4882a593Smuzhiyun {17, 13, 15, 3, 4, 8, ZSTD_greedy}, /* level 4 */
3360*4882a593Smuzhiyun {17, 15, 17, 4, 4, 8, ZSTD_greedy}, /* level 5 */
3361*4882a593Smuzhiyun {17, 16, 17, 3, 4, 8, ZSTD_lazy}, /* level 6 */
3362*4882a593Smuzhiyun {17, 15, 17, 4, 4, 8, ZSTD_lazy2}, /* level 7 */
3363*4882a593Smuzhiyun {17, 17, 17, 4, 4, 8, ZSTD_lazy2}, /* level 8 */
3364*4882a593Smuzhiyun {17, 17, 17, 5, 4, 8, ZSTD_lazy2}, /* level 9 */
3365*4882a593Smuzhiyun {17, 17, 17, 6, 4, 8, ZSTD_lazy2}, /* level 10 */
3366*4882a593Smuzhiyun {17, 17, 17, 7, 4, 8, ZSTD_lazy2}, /* level 11 */
3367*4882a593Smuzhiyun {17, 17, 17, 8, 4, 8, ZSTD_lazy2}, /* level 12 */
3368*4882a593Smuzhiyun {17, 18, 17, 6, 4, 8, ZSTD_btlazy2}, /* level 13.*/
3369*4882a593Smuzhiyun {17, 17, 17, 7, 3, 8, ZSTD_btopt}, /* level 14.*/
3370*4882a593Smuzhiyun {17, 17, 17, 7, 3, 16, ZSTD_btopt}, /* level 15.*/
3371*4882a593Smuzhiyun {17, 18, 17, 7, 3, 32, ZSTD_btopt}, /* level 16.*/
3372*4882a593Smuzhiyun {17, 18, 17, 7, 3, 64, ZSTD_btopt}, /* level 17.*/
3373*4882a593Smuzhiyun {17, 18, 17, 7, 3, 256, ZSTD_btopt}, /* level 18.*/
3374*4882a593Smuzhiyun {17, 18, 17, 8, 3, 256, ZSTD_btopt}, /* level 19.*/
3375*4882a593Smuzhiyun {17, 18, 17, 9, 3, 256, ZSTD_btopt2}, /* level 20.*/
3376*4882a593Smuzhiyun {17, 18, 17, 10, 3, 256, ZSTD_btopt2}, /* level 21.*/
3377*4882a593Smuzhiyun {17, 18, 17, 11, 3, 512, ZSTD_btopt2}, /* level 22.*/
3378*4882a593Smuzhiyun },
3379*4882a593Smuzhiyun {
3380*4882a593Smuzhiyun /* for srcSize <= 16 KB */
3381*4882a593Smuzhiyun /* W, C, H, S, L, T, strat */
3382*4882a593Smuzhiyun {14, 12, 12, 1, 7, 6, ZSTD_fast}, /* level 0 - not used */
3383*4882a593Smuzhiyun {14, 14, 14, 1, 6, 6, ZSTD_fast}, /* level 1 */
3384*4882a593Smuzhiyun {14, 14, 14, 1, 4, 6, ZSTD_fast}, /* level 2 */
3385*4882a593Smuzhiyun {14, 14, 14, 1, 4, 6, ZSTD_dfast}, /* level 3.*/
3386*4882a593Smuzhiyun {14, 14, 14, 4, 4, 6, ZSTD_greedy}, /* level 4.*/
3387*4882a593Smuzhiyun {14, 14, 14, 3, 4, 6, ZSTD_lazy}, /* level 5.*/
3388*4882a593Smuzhiyun {14, 14, 14, 4, 4, 6, ZSTD_lazy2}, /* level 6 */
3389*4882a593Smuzhiyun {14, 14, 14, 5, 4, 6, ZSTD_lazy2}, /* level 7 */
3390*4882a593Smuzhiyun {14, 14, 14, 6, 4, 6, ZSTD_lazy2}, /* level 8.*/
3391*4882a593Smuzhiyun {14, 15, 14, 6, 4, 6, ZSTD_btlazy2}, /* level 9.*/
3392*4882a593Smuzhiyun {14, 15, 14, 3, 3, 6, ZSTD_btopt}, /* level 10.*/
3393*4882a593Smuzhiyun {14, 15, 14, 6, 3, 8, ZSTD_btopt}, /* level 11.*/
3394*4882a593Smuzhiyun {14, 15, 14, 6, 3, 16, ZSTD_btopt}, /* level 12.*/
3395*4882a593Smuzhiyun {14, 15, 14, 6, 3, 24, ZSTD_btopt}, /* level 13.*/
3396*4882a593Smuzhiyun {14, 15, 15, 6, 3, 48, ZSTD_btopt}, /* level 14.*/
3397*4882a593Smuzhiyun {14, 15, 15, 6, 3, 64, ZSTD_btopt}, /* level 15.*/
3398*4882a593Smuzhiyun {14, 15, 15, 6, 3, 96, ZSTD_btopt}, /* level 16.*/
3399*4882a593Smuzhiyun {14, 15, 15, 6, 3, 128, ZSTD_btopt}, /* level 17.*/
3400*4882a593Smuzhiyun {14, 15, 15, 6, 3, 256, ZSTD_btopt}, /* level 18.*/
3401*4882a593Smuzhiyun {14, 15, 15, 7, 3, 256, ZSTD_btopt}, /* level 19.*/
3402*4882a593Smuzhiyun {14, 15, 15, 8, 3, 256, ZSTD_btopt2}, /* level 20.*/
3403*4882a593Smuzhiyun {14, 15, 15, 9, 3, 256, ZSTD_btopt2}, /* level 21.*/
3404*4882a593Smuzhiyun {14, 15, 15, 10, 3, 256, ZSTD_btopt2}, /* level 22.*/
3405*4882a593Smuzhiyun },
3406*4882a593Smuzhiyun };
3407*4882a593Smuzhiyun
3408*4882a593Smuzhiyun /*! ZSTD_getCParams() :
3409*4882a593Smuzhiyun * @return ZSTD_compressionParameters structure for a selected compression level, `srcSize` and `dictSize`.
3410*4882a593Smuzhiyun * Size values are optional, provide 0 if not known or unused */
ZSTD_getCParams(int compressionLevel,unsigned long long srcSize,size_t dictSize)3411*4882a593Smuzhiyun ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long srcSize, size_t dictSize)
3412*4882a593Smuzhiyun {
3413*4882a593Smuzhiyun ZSTD_compressionParameters cp;
3414*4882a593Smuzhiyun size_t const addedSize = srcSize ? 0 : 500;
3415*4882a593Smuzhiyun U64 const rSize = srcSize + dictSize ? srcSize + dictSize + addedSize : (U64)-1;
3416*4882a593Smuzhiyun U32 const tableID = (rSize <= 256 KB) + (rSize <= 128 KB) + (rSize <= 16 KB); /* intentional underflow for srcSizeHint == 0 */
3417*4882a593Smuzhiyun if (compressionLevel <= 0)
3418*4882a593Smuzhiyun compressionLevel = ZSTD_DEFAULT_CLEVEL; /* 0 == default; no negative compressionLevel yet */
3419*4882a593Smuzhiyun if (compressionLevel > ZSTD_MAX_CLEVEL)
3420*4882a593Smuzhiyun compressionLevel = ZSTD_MAX_CLEVEL;
3421*4882a593Smuzhiyun cp = ZSTD_defaultCParameters[tableID][compressionLevel];
3422*4882a593Smuzhiyun if (ZSTD_32bits()) { /* auto-correction, for 32-bits mode */
3423*4882a593Smuzhiyun if (cp.windowLog > ZSTD_WINDOWLOG_MAX)
3424*4882a593Smuzhiyun cp.windowLog = ZSTD_WINDOWLOG_MAX;
3425*4882a593Smuzhiyun if (cp.chainLog > ZSTD_CHAINLOG_MAX)
3426*4882a593Smuzhiyun cp.chainLog = ZSTD_CHAINLOG_MAX;
3427*4882a593Smuzhiyun if (cp.hashLog > ZSTD_HASHLOG_MAX)
3428*4882a593Smuzhiyun cp.hashLog = ZSTD_HASHLOG_MAX;
3429*4882a593Smuzhiyun }
3430*4882a593Smuzhiyun cp = ZSTD_adjustCParams(cp, srcSize, dictSize);
3431*4882a593Smuzhiyun return cp;
3432*4882a593Smuzhiyun }
3433*4882a593Smuzhiyun
3434*4882a593Smuzhiyun /*! ZSTD_getParams() :
3435*4882a593Smuzhiyun * same as ZSTD_getCParams(), but @return a `ZSTD_parameters` object (instead of `ZSTD_compressionParameters`).
3436*4882a593Smuzhiyun * All fields of `ZSTD_frameParameters` are set to default (0) */
ZSTD_getParams(int compressionLevel,unsigned long long srcSize,size_t dictSize)3437*4882a593Smuzhiyun ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long srcSize, size_t dictSize)
3438*4882a593Smuzhiyun {
3439*4882a593Smuzhiyun ZSTD_parameters params;
3440*4882a593Smuzhiyun ZSTD_compressionParameters const cParams = ZSTD_getCParams(compressionLevel, srcSize, dictSize);
3441*4882a593Smuzhiyun memset(¶ms, 0, sizeof(params));
3442*4882a593Smuzhiyun params.cParams = cParams;
3443*4882a593Smuzhiyun return params;
3444*4882a593Smuzhiyun }
3445*4882a593Smuzhiyun
3446*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_maxCLevel);
3447*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_compressBound);
3448*4882a593Smuzhiyun
3449*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_CCtxWorkspaceBound);
3450*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_initCCtx);
3451*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_compressCCtx);
3452*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_compress_usingDict);
3453*4882a593Smuzhiyun
3454*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_CDictWorkspaceBound);
3455*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_initCDict);
3456*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_compress_usingCDict);
3457*4882a593Smuzhiyun
3458*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_CStreamWorkspaceBound);
3459*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_initCStream);
3460*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_initCStream_usingCDict);
3461*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_resetCStream);
3462*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_compressStream);
3463*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_flushStream);
3464*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_endStream);
3465*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_CStreamInSize);
3466*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_CStreamOutSize);
3467*4882a593Smuzhiyun
3468*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_getCParams);
3469*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_getParams);
3470*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_checkCParams);
3471*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_adjustCParams);
3472*4882a593Smuzhiyun
3473*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_compressBegin);
3474*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_compressBegin_usingDict);
3475*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_compressBegin_advanced);
3476*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_copyCCtx);
3477*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_compressBegin_usingCDict);
3478*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_compressContinue);
3479*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_compressEnd);
3480*4882a593Smuzhiyun
3481*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_getBlockSizeMax);
3482*4882a593Smuzhiyun EXPORT_SYMBOL(ZSTD_compressBlock);
3483*4882a593Smuzhiyun
3484*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
3485*4882a593Smuzhiyun MODULE_DESCRIPTION("Zstd Compressor");
3486