1*06042735SVincent Franchomme /*
2*06042735SVincent Franchomme * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3*06042735SVincent Franchomme * All rights reserved.
4*06042735SVincent Franchomme *
5*06042735SVincent Franchomme * This source code is licensed under both the BSD-style license (found in the
6*06042735SVincent Franchomme * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*06042735SVincent Franchomme * in the COPYING file in the root directory of this source tree).
8*06042735SVincent Franchomme * You may select, at your option, one of the above-listed licenses.
9*06042735SVincent Franchomme */
10*06042735SVincent Franchomme
11*06042735SVincent Franchomme /* zstd_decompress_block :
12*06042735SVincent Franchomme * this module takes care of decompressing _compressed_ block */
13*06042735SVincent Franchomme
14*06042735SVincent Franchomme /*-*******************************************************
15*06042735SVincent Franchomme * Dependencies
16*06042735SVincent Franchomme *********************************************************/
17*06042735SVincent Franchomme #include <string.h> /* memcpy, memmove, memset */
18*06042735SVincent Franchomme #include "compiler.h" /* prefetch */
19*06042735SVincent Franchomme #include "cpu.h" /* bmi2 */
20*06042735SVincent Franchomme #include "mem.h" /* low level memory routines */
21*06042735SVincent Franchomme #define FSE_STATIC_LINKING_ONLY
22*06042735SVincent Franchomme #include "fse.h"
23*06042735SVincent Franchomme #define HUF_STATIC_LINKING_ONLY
24*06042735SVincent Franchomme #include "huf.h"
25*06042735SVincent Franchomme #include "zstd_internal.h"
26*06042735SVincent Franchomme #include "zstd_decompress_internal.h" /* ZSTD_DCtx */
27*06042735SVincent Franchomme #include "zstd_ddict.h" /* ZSTD_DDictDictContent */
28*06042735SVincent Franchomme #include "zstd_decompress_block.h"
29*06042735SVincent Franchomme
30*06042735SVincent Franchomme /*_*******************************************************
31*06042735SVincent Franchomme * Macros
32*06042735SVincent Franchomme **********************************************************/
33*06042735SVincent Franchomme
34*06042735SVincent Franchomme /* These two optional macros force the use one way or another of the two
35*06042735SVincent Franchomme * ZSTD_decompressSequences implementations. You can't force in both directions
36*06042735SVincent Franchomme * at the same time.
37*06042735SVincent Franchomme */
38*06042735SVincent Franchomme #if defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
39*06042735SVincent Franchomme defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
40*06042735SVincent Franchomme #error "Cannot force the use of the short and the long ZSTD_decompressSequences variants!"
41*06042735SVincent Franchomme #endif
42*06042735SVincent Franchomme
43*06042735SVincent Franchomme
44*06042735SVincent Franchomme /*_*******************************************************
45*06042735SVincent Franchomme * Memory operations
46*06042735SVincent Franchomme **********************************************************/
ZSTD_copy4(void * dst,const void * src)47*06042735SVincent Franchomme static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
48*06042735SVincent Franchomme
49*06042735SVincent Franchomme
50*06042735SVincent Franchomme /*-*************************************************************
51*06042735SVincent Franchomme * Block decoding
52*06042735SVincent Franchomme ***************************************************************/
53*06042735SVincent Franchomme
54*06042735SVincent Franchomme /*! ZSTD_getcBlockSize() :
55*06042735SVincent Franchomme * Provides the size of compressed block from block header `src` */
ZSTD_getcBlockSize(const void * src,size_t srcSize,blockProperties_t * bpPtr)56*06042735SVincent Franchomme size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
57*06042735SVincent Franchomme blockProperties_t* bpPtr)
58*06042735SVincent Franchomme {
59*06042735SVincent Franchomme RETURN_ERROR_IF(srcSize < ZSTD_blockHeaderSize, srcSize_wrong, "");
60*06042735SVincent Franchomme
61*06042735SVincent Franchomme { U32 const cBlockHeader = MEM_readLE24(src);
62*06042735SVincent Franchomme U32 const cSize = cBlockHeader >> 3;
63*06042735SVincent Franchomme bpPtr->lastBlock = cBlockHeader & 1;
64*06042735SVincent Franchomme bpPtr->blockType = (blockType_e)((cBlockHeader >> 1) & 3);
65*06042735SVincent Franchomme bpPtr->origSize = cSize; /* only useful for RLE */
66*06042735SVincent Franchomme if (bpPtr->blockType == bt_rle) return 1;
67*06042735SVincent Franchomme RETURN_ERROR_IF(bpPtr->blockType == bt_reserved, corruption_detected, "");
68*06042735SVincent Franchomme return cSize;
69*06042735SVincent Franchomme }
70*06042735SVincent Franchomme }
71*06042735SVincent Franchomme
72*06042735SVincent Franchomme
73*06042735SVincent Franchomme /* Hidden declaration for fullbench */
74*06042735SVincent Franchomme size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
75*06042735SVincent Franchomme const void* src, size_t srcSize);
76*06042735SVincent Franchomme /*! ZSTD_decodeLiteralsBlock() :
77*06042735SVincent Franchomme * @return : nb of bytes read from src (< srcSize )
78*06042735SVincent Franchomme * note : symbol not declared but exposed for fullbench */
ZSTD_decodeLiteralsBlock(ZSTD_DCtx * dctx,const void * src,size_t srcSize)79*06042735SVincent Franchomme size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
80*06042735SVincent Franchomme const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
81*06042735SVincent Franchomme {
82*06042735SVincent Franchomme DEBUGLOG(5, "ZSTD_decodeLiteralsBlock");
83*06042735SVincent Franchomme RETURN_ERROR_IF(srcSize < MIN_CBLOCK_SIZE, corruption_detected, "");
84*06042735SVincent Franchomme
85*06042735SVincent Franchomme { const BYTE* const istart = (const BYTE*) src;
86*06042735SVincent Franchomme symbolEncodingType_e const litEncType = (symbolEncodingType_e)(istart[0] & 3);
87*06042735SVincent Franchomme
88*06042735SVincent Franchomme switch(litEncType)
89*06042735SVincent Franchomme {
90*06042735SVincent Franchomme case set_repeat:
91*06042735SVincent Franchomme DEBUGLOG(5, "set_repeat flag : re-using stats from previous compressed literals block");
92*06042735SVincent Franchomme RETURN_ERROR_IF(dctx->litEntropy==0, dictionary_corrupted, "");
93*06042735SVincent Franchomme /* fall-through */
94*06042735SVincent Franchomme
95*06042735SVincent Franchomme case set_compressed:
96*06042735SVincent Franchomme RETURN_ERROR_IF(srcSize < 5, corruption_detected, "srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3");
97*06042735SVincent Franchomme { size_t lhSize, litSize, litCSize;
98*06042735SVincent Franchomme U32 singleStream=0;
99*06042735SVincent Franchomme U32 const lhlCode = (istart[0] >> 2) & 3;
100*06042735SVincent Franchomme U32 const lhc = MEM_readLE32(istart);
101*06042735SVincent Franchomme size_t hufSuccess;
102*06042735SVincent Franchomme switch(lhlCode)
103*06042735SVincent Franchomme {
104*06042735SVincent Franchomme case 0: case 1: default: /* note : default is impossible, since lhlCode into [0..3] */
105*06042735SVincent Franchomme /* 2 - 2 - 10 - 10 */
106*06042735SVincent Franchomme singleStream = !lhlCode;
107*06042735SVincent Franchomme lhSize = 3;
108*06042735SVincent Franchomme litSize = (lhc >> 4) & 0x3FF;
109*06042735SVincent Franchomme litCSize = (lhc >> 14) & 0x3FF;
110*06042735SVincent Franchomme break;
111*06042735SVincent Franchomme case 2:
112*06042735SVincent Franchomme /* 2 - 2 - 14 - 14 */
113*06042735SVincent Franchomme lhSize = 4;
114*06042735SVincent Franchomme litSize = (lhc >> 4) & 0x3FFF;
115*06042735SVincent Franchomme litCSize = lhc >> 18;
116*06042735SVincent Franchomme break;
117*06042735SVincent Franchomme case 3:
118*06042735SVincent Franchomme /* 2 - 2 - 18 - 18 */
119*06042735SVincent Franchomme lhSize = 5;
120*06042735SVincent Franchomme litSize = (lhc >> 4) & 0x3FFFF;
121*06042735SVincent Franchomme litCSize = (lhc >> 22) + ((size_t)istart[4] << 10);
122*06042735SVincent Franchomme break;
123*06042735SVincent Franchomme }
124*06042735SVincent Franchomme RETURN_ERROR_IF(litSize > ZSTD_BLOCKSIZE_MAX, corruption_detected, "");
125*06042735SVincent Franchomme RETURN_ERROR_IF(litCSize + lhSize > srcSize, corruption_detected, "");
126*06042735SVincent Franchomme
127*06042735SVincent Franchomme /* prefetch huffman table if cold */
128*06042735SVincent Franchomme if (dctx->ddictIsCold && (litSize > 768 /* heuristic */)) {
129*06042735SVincent Franchomme PREFETCH_AREA(dctx->HUFptr, sizeof(dctx->entropy.hufTable));
130*06042735SVincent Franchomme }
131*06042735SVincent Franchomme
132*06042735SVincent Franchomme if (litEncType==set_repeat) {
133*06042735SVincent Franchomme if (singleStream) {
134*06042735SVincent Franchomme hufSuccess = HUF_decompress1X_usingDTable_bmi2(
135*06042735SVincent Franchomme dctx->litBuffer, litSize, istart+lhSize, litCSize,
136*06042735SVincent Franchomme dctx->HUFptr, dctx->bmi2);
137*06042735SVincent Franchomme } else {
138*06042735SVincent Franchomme hufSuccess = HUF_decompress4X_usingDTable_bmi2(
139*06042735SVincent Franchomme dctx->litBuffer, litSize, istart+lhSize, litCSize,
140*06042735SVincent Franchomme dctx->HUFptr, dctx->bmi2);
141*06042735SVincent Franchomme }
142*06042735SVincent Franchomme } else {
143*06042735SVincent Franchomme if (singleStream) {
144*06042735SVincent Franchomme #if defined(HUF_FORCE_DECOMPRESS_X2)
145*06042735SVincent Franchomme hufSuccess = HUF_decompress1X_DCtx_wksp(
146*06042735SVincent Franchomme dctx->entropy.hufTable, dctx->litBuffer, litSize,
147*06042735SVincent Franchomme istart+lhSize, litCSize, dctx->workspace,
148*06042735SVincent Franchomme sizeof(dctx->workspace));
149*06042735SVincent Franchomme #else
150*06042735SVincent Franchomme hufSuccess = HUF_decompress1X1_DCtx_wksp_bmi2(
151*06042735SVincent Franchomme dctx->entropy.hufTable, dctx->litBuffer, litSize,
152*06042735SVincent Franchomme istart+lhSize, litCSize, dctx->workspace,
153*06042735SVincent Franchomme sizeof(dctx->workspace), dctx->bmi2);
154*06042735SVincent Franchomme #endif
155*06042735SVincent Franchomme } else {
156*06042735SVincent Franchomme hufSuccess = HUF_decompress4X_hufOnly_wksp_bmi2(
157*06042735SVincent Franchomme dctx->entropy.hufTable, dctx->litBuffer, litSize,
158*06042735SVincent Franchomme istart+lhSize, litCSize, dctx->workspace,
159*06042735SVincent Franchomme sizeof(dctx->workspace), dctx->bmi2);
160*06042735SVincent Franchomme }
161*06042735SVincent Franchomme }
162*06042735SVincent Franchomme
163*06042735SVincent Franchomme RETURN_ERROR_IF(HUF_isError(hufSuccess), corruption_detected, "");
164*06042735SVincent Franchomme
165*06042735SVincent Franchomme dctx->litPtr = dctx->litBuffer;
166*06042735SVincent Franchomme dctx->litSize = litSize;
167*06042735SVincent Franchomme dctx->litEntropy = 1;
168*06042735SVincent Franchomme if (litEncType==set_compressed) dctx->HUFptr = dctx->entropy.hufTable;
169*06042735SVincent Franchomme memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
170*06042735SVincent Franchomme return litCSize + lhSize;
171*06042735SVincent Franchomme }
172*06042735SVincent Franchomme
173*06042735SVincent Franchomme case set_basic:
174*06042735SVincent Franchomme { size_t litSize, lhSize;
175*06042735SVincent Franchomme U32 const lhlCode = ((istart[0]) >> 2) & 3;
176*06042735SVincent Franchomme switch(lhlCode)
177*06042735SVincent Franchomme {
178*06042735SVincent Franchomme case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */
179*06042735SVincent Franchomme lhSize = 1;
180*06042735SVincent Franchomme litSize = istart[0] >> 3;
181*06042735SVincent Franchomme break;
182*06042735SVincent Franchomme case 1:
183*06042735SVincent Franchomme lhSize = 2;
184*06042735SVincent Franchomme litSize = MEM_readLE16(istart) >> 4;
185*06042735SVincent Franchomme break;
186*06042735SVincent Franchomme case 3:
187*06042735SVincent Franchomme lhSize = 3;
188*06042735SVincent Franchomme litSize = MEM_readLE24(istart) >> 4;
189*06042735SVincent Franchomme break;
190*06042735SVincent Franchomme }
191*06042735SVincent Franchomme
192*06042735SVincent Franchomme if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */
193*06042735SVincent Franchomme RETURN_ERROR_IF(litSize+lhSize > srcSize, corruption_detected, "");
194*06042735SVincent Franchomme memcpy(dctx->litBuffer, istart+lhSize, litSize);
195*06042735SVincent Franchomme dctx->litPtr = dctx->litBuffer;
196*06042735SVincent Franchomme dctx->litSize = litSize;
197*06042735SVincent Franchomme memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
198*06042735SVincent Franchomme return lhSize+litSize;
199*06042735SVincent Franchomme }
200*06042735SVincent Franchomme /* direct reference into compressed stream */
201*06042735SVincent Franchomme dctx->litPtr = istart+lhSize;
202*06042735SVincent Franchomme dctx->litSize = litSize;
203*06042735SVincent Franchomme return lhSize+litSize;
204*06042735SVincent Franchomme }
205*06042735SVincent Franchomme
206*06042735SVincent Franchomme case set_rle:
207*06042735SVincent Franchomme { U32 const lhlCode = ((istart[0]) >> 2) & 3;
208*06042735SVincent Franchomme size_t litSize, lhSize;
209*06042735SVincent Franchomme switch(lhlCode)
210*06042735SVincent Franchomme {
211*06042735SVincent Franchomme case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */
212*06042735SVincent Franchomme lhSize = 1;
213*06042735SVincent Franchomme litSize = istart[0] >> 3;
214*06042735SVincent Franchomme break;
215*06042735SVincent Franchomme case 1:
216*06042735SVincent Franchomme lhSize = 2;
217*06042735SVincent Franchomme litSize = MEM_readLE16(istart) >> 4;
218*06042735SVincent Franchomme break;
219*06042735SVincent Franchomme case 3:
220*06042735SVincent Franchomme lhSize = 3;
221*06042735SVincent Franchomme litSize = MEM_readLE24(istart) >> 4;
222*06042735SVincent Franchomme RETURN_ERROR_IF(srcSize<4, corruption_detected, "srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4");
223*06042735SVincent Franchomme break;
224*06042735SVincent Franchomme }
225*06042735SVincent Franchomme RETURN_ERROR_IF(litSize > ZSTD_BLOCKSIZE_MAX, corruption_detected, "");
226*06042735SVincent Franchomme memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH);
227*06042735SVincent Franchomme dctx->litPtr = dctx->litBuffer;
228*06042735SVincent Franchomme dctx->litSize = litSize;
229*06042735SVincent Franchomme return lhSize+1;
230*06042735SVincent Franchomme }
231*06042735SVincent Franchomme default:
232*06042735SVincent Franchomme RETURN_ERROR(corruption_detected, "impossible");
233*06042735SVincent Franchomme }
234*06042735SVincent Franchomme }
235*06042735SVincent Franchomme }
236*06042735SVincent Franchomme
237*06042735SVincent Franchomme /* Default FSE distribution tables.
238*06042735SVincent Franchomme * These are pre-calculated FSE decoding tables using default distributions as defined in specification :
239*06042735SVincent Franchomme * https://github.com/facebook/zstd/blob/master/doc/zstd_compression_format.md#default-distributions
240*06042735SVincent Franchomme * They were generated programmatically with following method :
241*06042735SVincent Franchomme * - start from default distributions, present in /lib/common/zstd_internal.h
242*06042735SVincent Franchomme * - generate tables normally, using ZSTD_buildFSETable()
243*06042735SVincent Franchomme * - printout the content of tables
244*06042735SVincent Franchomme * - pretify output, report below, test with fuzzer to ensure it's correct */
245*06042735SVincent Franchomme
246*06042735SVincent Franchomme /* Default FSE distribution table for Literal Lengths */
247*06042735SVincent Franchomme static const ZSTD_seqSymbol LL_defaultDTable[(1<<LL_DEFAULTNORMLOG)+1] = {
248*06042735SVincent Franchomme { 1, 1, 1, LL_DEFAULTNORMLOG}, /* header : fastMode, tableLog */
249*06042735SVincent Franchomme /* nextState, nbAddBits, nbBits, baseVal */
250*06042735SVincent Franchomme { 0, 0, 4, 0}, { 16, 0, 4, 0},
251*06042735SVincent Franchomme { 32, 0, 5, 1}, { 0, 0, 5, 3},
252*06042735SVincent Franchomme { 0, 0, 5, 4}, { 0, 0, 5, 6},
253*06042735SVincent Franchomme { 0, 0, 5, 7}, { 0, 0, 5, 9},
254*06042735SVincent Franchomme { 0, 0, 5, 10}, { 0, 0, 5, 12},
255*06042735SVincent Franchomme { 0, 0, 6, 14}, { 0, 1, 5, 16},
256*06042735SVincent Franchomme { 0, 1, 5, 20}, { 0, 1, 5, 22},
257*06042735SVincent Franchomme { 0, 2, 5, 28}, { 0, 3, 5, 32},
258*06042735SVincent Franchomme { 0, 4, 5, 48}, { 32, 6, 5, 64},
259*06042735SVincent Franchomme { 0, 7, 5, 128}, { 0, 8, 6, 256},
260*06042735SVincent Franchomme { 0, 10, 6, 1024}, { 0, 12, 6, 4096},
261*06042735SVincent Franchomme { 32, 0, 4, 0}, { 0, 0, 4, 1},
262*06042735SVincent Franchomme { 0, 0, 5, 2}, { 32, 0, 5, 4},
263*06042735SVincent Franchomme { 0, 0, 5, 5}, { 32, 0, 5, 7},
264*06042735SVincent Franchomme { 0, 0, 5, 8}, { 32, 0, 5, 10},
265*06042735SVincent Franchomme { 0, 0, 5, 11}, { 0, 0, 6, 13},
266*06042735SVincent Franchomme { 32, 1, 5, 16}, { 0, 1, 5, 18},
267*06042735SVincent Franchomme { 32, 1, 5, 22}, { 0, 2, 5, 24},
268*06042735SVincent Franchomme { 32, 3, 5, 32}, { 0, 3, 5, 40},
269*06042735SVincent Franchomme { 0, 6, 4, 64}, { 16, 6, 4, 64},
270*06042735SVincent Franchomme { 32, 7, 5, 128}, { 0, 9, 6, 512},
271*06042735SVincent Franchomme { 0, 11, 6, 2048}, { 48, 0, 4, 0},
272*06042735SVincent Franchomme { 16, 0, 4, 1}, { 32, 0, 5, 2},
273*06042735SVincent Franchomme { 32, 0, 5, 3}, { 32, 0, 5, 5},
274*06042735SVincent Franchomme { 32, 0, 5, 6}, { 32, 0, 5, 8},
275*06042735SVincent Franchomme { 32, 0, 5, 9}, { 32, 0, 5, 11},
276*06042735SVincent Franchomme { 32, 0, 5, 12}, { 0, 0, 6, 15},
277*06042735SVincent Franchomme { 32, 1, 5, 18}, { 32, 1, 5, 20},
278*06042735SVincent Franchomme { 32, 2, 5, 24}, { 32, 2, 5, 28},
279*06042735SVincent Franchomme { 32, 3, 5, 40}, { 32, 4, 5, 48},
280*06042735SVincent Franchomme { 0, 16, 6,65536}, { 0, 15, 6,32768},
281*06042735SVincent Franchomme { 0, 14, 6,16384}, { 0, 13, 6, 8192},
282*06042735SVincent Franchomme }; /* LL_defaultDTable */
283*06042735SVincent Franchomme
284*06042735SVincent Franchomme /* Default FSE distribution table for Offset Codes */
285*06042735SVincent Franchomme static const ZSTD_seqSymbol OF_defaultDTable[(1<<OF_DEFAULTNORMLOG)+1] = {
286*06042735SVincent Franchomme { 1, 1, 1, OF_DEFAULTNORMLOG}, /* header : fastMode, tableLog */
287*06042735SVincent Franchomme /* nextState, nbAddBits, nbBits, baseVal */
288*06042735SVincent Franchomme { 0, 0, 5, 0}, { 0, 6, 4, 61},
289*06042735SVincent Franchomme { 0, 9, 5, 509}, { 0, 15, 5,32765},
290*06042735SVincent Franchomme { 0, 21, 5,2097149}, { 0, 3, 5, 5},
291*06042735SVincent Franchomme { 0, 7, 4, 125}, { 0, 12, 5, 4093},
292*06042735SVincent Franchomme { 0, 18, 5,262141}, { 0, 23, 5,8388605},
293*06042735SVincent Franchomme { 0, 5, 5, 29}, { 0, 8, 4, 253},
294*06042735SVincent Franchomme { 0, 14, 5,16381}, { 0, 20, 5,1048573},
295*06042735SVincent Franchomme { 0, 2, 5, 1}, { 16, 7, 4, 125},
296*06042735SVincent Franchomme { 0, 11, 5, 2045}, { 0, 17, 5,131069},
297*06042735SVincent Franchomme { 0, 22, 5,4194301}, { 0, 4, 5, 13},
298*06042735SVincent Franchomme { 16, 8, 4, 253}, { 0, 13, 5, 8189},
299*06042735SVincent Franchomme { 0, 19, 5,524285}, { 0, 1, 5, 1},
300*06042735SVincent Franchomme { 16, 6, 4, 61}, { 0, 10, 5, 1021},
301*06042735SVincent Franchomme { 0, 16, 5,65533}, { 0, 28, 5,268435453},
302*06042735SVincent Franchomme { 0, 27, 5,134217725}, { 0, 26, 5,67108861},
303*06042735SVincent Franchomme { 0, 25, 5,33554429}, { 0, 24, 5,16777213},
304*06042735SVincent Franchomme }; /* OF_defaultDTable */
305*06042735SVincent Franchomme
306*06042735SVincent Franchomme
307*06042735SVincent Franchomme /* Default FSE distribution table for Match Lengths */
308*06042735SVincent Franchomme static const ZSTD_seqSymbol ML_defaultDTable[(1<<ML_DEFAULTNORMLOG)+1] = {
309*06042735SVincent Franchomme { 1, 1, 1, ML_DEFAULTNORMLOG}, /* header : fastMode, tableLog */
310*06042735SVincent Franchomme /* nextState, nbAddBits, nbBits, baseVal */
311*06042735SVincent Franchomme { 0, 0, 6, 3}, { 0, 0, 4, 4},
312*06042735SVincent Franchomme { 32, 0, 5, 5}, { 0, 0, 5, 6},
313*06042735SVincent Franchomme { 0, 0, 5, 8}, { 0, 0, 5, 9},
314*06042735SVincent Franchomme { 0, 0, 5, 11}, { 0, 0, 6, 13},
315*06042735SVincent Franchomme { 0, 0, 6, 16}, { 0, 0, 6, 19},
316*06042735SVincent Franchomme { 0, 0, 6, 22}, { 0, 0, 6, 25},
317*06042735SVincent Franchomme { 0, 0, 6, 28}, { 0, 0, 6, 31},
318*06042735SVincent Franchomme { 0, 0, 6, 34}, { 0, 1, 6, 37},
319*06042735SVincent Franchomme { 0, 1, 6, 41}, { 0, 2, 6, 47},
320*06042735SVincent Franchomme { 0, 3, 6, 59}, { 0, 4, 6, 83},
321*06042735SVincent Franchomme { 0, 7, 6, 131}, { 0, 9, 6, 515},
322*06042735SVincent Franchomme { 16, 0, 4, 4}, { 0, 0, 4, 5},
323*06042735SVincent Franchomme { 32, 0, 5, 6}, { 0, 0, 5, 7},
324*06042735SVincent Franchomme { 32, 0, 5, 9}, { 0, 0, 5, 10},
325*06042735SVincent Franchomme { 0, 0, 6, 12}, { 0, 0, 6, 15},
326*06042735SVincent Franchomme { 0, 0, 6, 18}, { 0, 0, 6, 21},
327*06042735SVincent Franchomme { 0, 0, 6, 24}, { 0, 0, 6, 27},
328*06042735SVincent Franchomme { 0, 0, 6, 30}, { 0, 0, 6, 33},
329*06042735SVincent Franchomme { 0, 1, 6, 35}, { 0, 1, 6, 39},
330*06042735SVincent Franchomme { 0, 2, 6, 43}, { 0, 3, 6, 51},
331*06042735SVincent Franchomme { 0, 4, 6, 67}, { 0, 5, 6, 99},
332*06042735SVincent Franchomme { 0, 8, 6, 259}, { 32, 0, 4, 4},
333*06042735SVincent Franchomme { 48, 0, 4, 4}, { 16, 0, 4, 5},
334*06042735SVincent Franchomme { 32, 0, 5, 7}, { 32, 0, 5, 8},
335*06042735SVincent Franchomme { 32, 0, 5, 10}, { 32, 0, 5, 11},
336*06042735SVincent Franchomme { 0, 0, 6, 14}, { 0, 0, 6, 17},
337*06042735SVincent Franchomme { 0, 0, 6, 20}, { 0, 0, 6, 23},
338*06042735SVincent Franchomme { 0, 0, 6, 26}, { 0, 0, 6, 29},
339*06042735SVincent Franchomme { 0, 0, 6, 32}, { 0, 16, 6,65539},
340*06042735SVincent Franchomme { 0, 15, 6,32771}, { 0, 14, 6,16387},
341*06042735SVincent Franchomme { 0, 13, 6, 8195}, { 0, 12, 6, 4099},
342*06042735SVincent Franchomme { 0, 11, 6, 2051}, { 0, 10, 6, 1027},
343*06042735SVincent Franchomme }; /* ML_defaultDTable */
344*06042735SVincent Franchomme
345*06042735SVincent Franchomme
ZSTD_buildSeqTable_rle(ZSTD_seqSymbol * dt,U32 baseValue,U32 nbAddBits)346*06042735SVincent Franchomme static void ZSTD_buildSeqTable_rle(ZSTD_seqSymbol* dt, U32 baseValue, U32 nbAddBits)
347*06042735SVincent Franchomme {
348*06042735SVincent Franchomme void* ptr = dt;
349*06042735SVincent Franchomme ZSTD_seqSymbol_header* const DTableH = (ZSTD_seqSymbol_header*)ptr;
350*06042735SVincent Franchomme ZSTD_seqSymbol* const cell = dt + 1;
351*06042735SVincent Franchomme
352*06042735SVincent Franchomme DTableH->tableLog = 0;
353*06042735SVincent Franchomme DTableH->fastMode = 0;
354*06042735SVincent Franchomme
355*06042735SVincent Franchomme cell->nbBits = 0;
356*06042735SVincent Franchomme cell->nextState = 0;
357*06042735SVincent Franchomme assert(nbAddBits < 255);
358*06042735SVincent Franchomme cell->nbAdditionalBits = (BYTE)nbAddBits;
359*06042735SVincent Franchomme cell->baseValue = baseValue;
360*06042735SVincent Franchomme }
361*06042735SVincent Franchomme
362*06042735SVincent Franchomme
363*06042735SVincent Franchomme /* ZSTD_buildFSETable() :
364*06042735SVincent Franchomme * generate FSE decoding table for one symbol (ll, ml or off)
365*06042735SVincent Franchomme * cannot fail if input is valid =>
366*06042735SVincent Franchomme * all inputs are presumed validated at this stage */
367*06042735SVincent Franchomme void
ZSTD_buildFSETable(ZSTD_seqSymbol * dt,const short * normalizedCounter,unsigned maxSymbolValue,const U32 * baseValue,const U32 * nbAdditionalBits,unsigned tableLog)368*06042735SVincent Franchomme ZSTD_buildFSETable(ZSTD_seqSymbol* dt,
369*06042735SVincent Franchomme const short* normalizedCounter, unsigned maxSymbolValue,
370*06042735SVincent Franchomme const U32* baseValue, const U32* nbAdditionalBits,
371*06042735SVincent Franchomme unsigned tableLog)
372*06042735SVincent Franchomme {
373*06042735SVincent Franchomme ZSTD_seqSymbol* const tableDecode = dt+1;
374*06042735SVincent Franchomme U16 symbolNext[MaxSeq+1];
375*06042735SVincent Franchomme
376*06042735SVincent Franchomme U32 const maxSV1 = maxSymbolValue + 1;
377*06042735SVincent Franchomme U32 const tableSize = 1 << tableLog;
378*06042735SVincent Franchomme U32 highThreshold = tableSize-1;
379*06042735SVincent Franchomme
380*06042735SVincent Franchomme /* Sanity Checks */
381*06042735SVincent Franchomme assert(maxSymbolValue <= MaxSeq);
382*06042735SVincent Franchomme assert(tableLog <= MaxFSELog);
383*06042735SVincent Franchomme
384*06042735SVincent Franchomme /* Init, lay down lowprob symbols */
385*06042735SVincent Franchomme { ZSTD_seqSymbol_header DTableH;
386*06042735SVincent Franchomme DTableH.tableLog = tableLog;
387*06042735SVincent Franchomme DTableH.fastMode = 1;
388*06042735SVincent Franchomme { S16 const largeLimit= (S16)(1 << (tableLog-1));
389*06042735SVincent Franchomme U32 s;
390*06042735SVincent Franchomme for (s=0; s<maxSV1; s++) {
391*06042735SVincent Franchomme if (normalizedCounter[s]==-1) {
392*06042735SVincent Franchomme tableDecode[highThreshold--].baseValue = s;
393*06042735SVincent Franchomme symbolNext[s] = 1;
394*06042735SVincent Franchomme } else {
395*06042735SVincent Franchomme if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;
396*06042735SVincent Franchomme assert(normalizedCounter[s]>=0);
397*06042735SVincent Franchomme symbolNext[s] = (U16)normalizedCounter[s];
398*06042735SVincent Franchomme } } }
399*06042735SVincent Franchomme memcpy(dt, &DTableH, sizeof(DTableH));
400*06042735SVincent Franchomme }
401*06042735SVincent Franchomme
402*06042735SVincent Franchomme /* Spread symbols */
403*06042735SVincent Franchomme { U32 const tableMask = tableSize-1;
404*06042735SVincent Franchomme U32 const step = FSE_TABLESTEP(tableSize);
405*06042735SVincent Franchomme U32 s, position = 0;
406*06042735SVincent Franchomme for (s=0; s<maxSV1; s++) {
407*06042735SVincent Franchomme int i;
408*06042735SVincent Franchomme for (i=0; i<normalizedCounter[s]; i++) {
409*06042735SVincent Franchomme tableDecode[position].baseValue = s;
410*06042735SVincent Franchomme position = (position + step) & tableMask;
411*06042735SVincent Franchomme while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
412*06042735SVincent Franchomme } }
413*06042735SVincent Franchomme assert(position == 0); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
414*06042735SVincent Franchomme }
415*06042735SVincent Franchomme
416*06042735SVincent Franchomme /* Build Decoding table */
417*06042735SVincent Franchomme { U32 u;
418*06042735SVincent Franchomme for (u=0; u<tableSize; u++) {
419*06042735SVincent Franchomme U32 const symbol = tableDecode[u].baseValue;
420*06042735SVincent Franchomme U32 const nextState = symbolNext[symbol]++;
421*06042735SVincent Franchomme tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) );
422*06042735SVincent Franchomme tableDecode[u].nextState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
423*06042735SVincent Franchomme assert(nbAdditionalBits[symbol] < 255);
424*06042735SVincent Franchomme tableDecode[u].nbAdditionalBits = (BYTE)nbAdditionalBits[symbol];
425*06042735SVincent Franchomme tableDecode[u].baseValue = baseValue[symbol];
426*06042735SVincent Franchomme } }
427*06042735SVincent Franchomme }
428*06042735SVincent Franchomme
429*06042735SVincent Franchomme
430*06042735SVincent Franchomme /*! ZSTD_buildSeqTable() :
431*06042735SVincent Franchomme * @return : nb bytes read from src,
432*06042735SVincent Franchomme * or an error code if it fails */
ZSTD_buildSeqTable(ZSTD_seqSymbol * DTableSpace,const ZSTD_seqSymbol ** DTablePtr,symbolEncodingType_e type,unsigned max,U32 maxLog,const void * src,size_t srcSize,const U32 * baseValue,const U32 * nbAdditionalBits,const ZSTD_seqSymbol * defaultTable,U32 flagRepeatTable,int ddictIsCold,int nbSeq)433*06042735SVincent Franchomme static size_t ZSTD_buildSeqTable(ZSTD_seqSymbol* DTableSpace, const ZSTD_seqSymbol** DTablePtr,
434*06042735SVincent Franchomme symbolEncodingType_e type, unsigned max, U32 maxLog,
435*06042735SVincent Franchomme const void* src, size_t srcSize,
436*06042735SVincent Franchomme const U32* baseValue, const U32* nbAdditionalBits,
437*06042735SVincent Franchomme const ZSTD_seqSymbol* defaultTable, U32 flagRepeatTable,
438*06042735SVincent Franchomme int ddictIsCold, int nbSeq)
439*06042735SVincent Franchomme {
440*06042735SVincent Franchomme switch(type)
441*06042735SVincent Franchomme {
442*06042735SVincent Franchomme case set_rle :
443*06042735SVincent Franchomme RETURN_ERROR_IF(!srcSize, srcSize_wrong, "");
444*06042735SVincent Franchomme RETURN_ERROR_IF((*(const BYTE*)src) > max, corruption_detected, "");
445*06042735SVincent Franchomme { U32 const symbol = *(const BYTE*)src;
446*06042735SVincent Franchomme U32 const baseline = baseValue[symbol];
447*06042735SVincent Franchomme U32 const nbBits = nbAdditionalBits[symbol];
448*06042735SVincent Franchomme ZSTD_buildSeqTable_rle(DTableSpace, baseline, nbBits);
449*06042735SVincent Franchomme }
450*06042735SVincent Franchomme *DTablePtr = DTableSpace;
451*06042735SVincent Franchomme return 1;
452*06042735SVincent Franchomme case set_basic :
453*06042735SVincent Franchomme *DTablePtr = defaultTable;
454*06042735SVincent Franchomme return 0;
455*06042735SVincent Franchomme case set_repeat:
456*06042735SVincent Franchomme RETURN_ERROR_IF(!flagRepeatTable, corruption_detected, "");
457*06042735SVincent Franchomme /* prefetch FSE table if used */
458*06042735SVincent Franchomme if (ddictIsCold && (nbSeq > 24 /* heuristic */)) {
459*06042735SVincent Franchomme const void* const pStart = *DTablePtr;
460*06042735SVincent Franchomme size_t const pSize = sizeof(ZSTD_seqSymbol) * (SEQSYMBOL_TABLE_SIZE(maxLog));
461*06042735SVincent Franchomme PREFETCH_AREA(pStart, pSize);
462*06042735SVincent Franchomme }
463*06042735SVincent Franchomme return 0;
464*06042735SVincent Franchomme case set_compressed :
465*06042735SVincent Franchomme { unsigned tableLog;
466*06042735SVincent Franchomme S16 norm[MaxSeq+1];
467*06042735SVincent Franchomme size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize);
468*06042735SVincent Franchomme RETURN_ERROR_IF(FSE_isError(headerSize), corruption_detected, "");
469*06042735SVincent Franchomme RETURN_ERROR_IF(tableLog > maxLog, corruption_detected, "");
470*06042735SVincent Franchomme ZSTD_buildFSETable(DTableSpace, norm, max, baseValue, nbAdditionalBits, tableLog);
471*06042735SVincent Franchomme *DTablePtr = DTableSpace;
472*06042735SVincent Franchomme return headerSize;
473*06042735SVincent Franchomme }
474*06042735SVincent Franchomme default :
475*06042735SVincent Franchomme assert(0);
476*06042735SVincent Franchomme RETURN_ERROR(GENERIC, "impossible");
477*06042735SVincent Franchomme }
478*06042735SVincent Franchomme }
479*06042735SVincent Franchomme
ZSTD_decodeSeqHeaders(ZSTD_DCtx * dctx,int * nbSeqPtr,const void * src,size_t srcSize)480*06042735SVincent Franchomme size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
481*06042735SVincent Franchomme const void* src, size_t srcSize)
482*06042735SVincent Franchomme {
483*06042735SVincent Franchomme const BYTE* const istart = (const BYTE* const)src;
484*06042735SVincent Franchomme const BYTE* const iend = istart + srcSize;
485*06042735SVincent Franchomme const BYTE* ip = istart;
486*06042735SVincent Franchomme int nbSeq;
487*06042735SVincent Franchomme DEBUGLOG(5, "ZSTD_decodeSeqHeaders");
488*06042735SVincent Franchomme
489*06042735SVincent Franchomme /* check */
490*06042735SVincent Franchomme RETURN_ERROR_IF(srcSize < MIN_SEQUENCES_SIZE, srcSize_wrong, "");
491*06042735SVincent Franchomme
492*06042735SVincent Franchomme /* SeqHead */
493*06042735SVincent Franchomme nbSeq = *ip++;
494*06042735SVincent Franchomme if (!nbSeq) {
495*06042735SVincent Franchomme *nbSeqPtr=0;
496*06042735SVincent Franchomme RETURN_ERROR_IF(srcSize != 1, srcSize_wrong, "");
497*06042735SVincent Franchomme return 1;
498*06042735SVincent Franchomme }
499*06042735SVincent Franchomme if (nbSeq > 0x7F) {
500*06042735SVincent Franchomme if (nbSeq == 0xFF) {
501*06042735SVincent Franchomme RETURN_ERROR_IF(ip+2 > iend, srcSize_wrong, "");
502*06042735SVincent Franchomme nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2;
503*06042735SVincent Franchomme } else {
504*06042735SVincent Franchomme RETURN_ERROR_IF(ip >= iend, srcSize_wrong, "");
505*06042735SVincent Franchomme nbSeq = ((nbSeq-0x80)<<8) + *ip++;
506*06042735SVincent Franchomme }
507*06042735SVincent Franchomme }
508*06042735SVincent Franchomme *nbSeqPtr = nbSeq;
509*06042735SVincent Franchomme
510*06042735SVincent Franchomme /* FSE table descriptors */
511*06042735SVincent Franchomme RETURN_ERROR_IF(ip+1 > iend, srcSize_wrong, ""); /* minimum possible size: 1 byte for symbol encoding types */
512*06042735SVincent Franchomme { symbolEncodingType_e const LLtype = (symbolEncodingType_e)(*ip >> 6);
513*06042735SVincent Franchomme symbolEncodingType_e const OFtype = (symbolEncodingType_e)((*ip >> 4) & 3);
514*06042735SVincent Franchomme symbolEncodingType_e const MLtype = (symbolEncodingType_e)((*ip >> 2) & 3);
515*06042735SVincent Franchomme ip++;
516*06042735SVincent Franchomme
517*06042735SVincent Franchomme /* Build DTables */
518*06042735SVincent Franchomme { size_t const llhSize = ZSTD_buildSeqTable(dctx->entropy.LLTable, &dctx->LLTptr,
519*06042735SVincent Franchomme LLtype, MaxLL, LLFSELog,
520*06042735SVincent Franchomme ip, iend-ip,
521*06042735SVincent Franchomme LL_base, LL_bits,
522*06042735SVincent Franchomme LL_defaultDTable, dctx->fseEntropy,
523*06042735SVincent Franchomme dctx->ddictIsCold, nbSeq);
524*06042735SVincent Franchomme RETURN_ERROR_IF(ZSTD_isError(llhSize), corruption_detected, "ZSTD_buildSeqTable failed");
525*06042735SVincent Franchomme ip += llhSize;
526*06042735SVincent Franchomme }
527*06042735SVincent Franchomme
528*06042735SVincent Franchomme { size_t const ofhSize = ZSTD_buildSeqTable(dctx->entropy.OFTable, &dctx->OFTptr,
529*06042735SVincent Franchomme OFtype, MaxOff, OffFSELog,
530*06042735SVincent Franchomme ip, iend-ip,
531*06042735SVincent Franchomme OF_base, OF_bits,
532*06042735SVincent Franchomme OF_defaultDTable, dctx->fseEntropy,
533*06042735SVincent Franchomme dctx->ddictIsCold, nbSeq);
534*06042735SVincent Franchomme RETURN_ERROR_IF(ZSTD_isError(ofhSize), corruption_detected, "ZSTD_buildSeqTable failed");
535*06042735SVincent Franchomme ip += ofhSize;
536*06042735SVincent Franchomme }
537*06042735SVincent Franchomme
538*06042735SVincent Franchomme { size_t const mlhSize = ZSTD_buildSeqTable(dctx->entropy.MLTable, &dctx->MLTptr,
539*06042735SVincent Franchomme MLtype, MaxML, MLFSELog,
540*06042735SVincent Franchomme ip, iend-ip,
541*06042735SVincent Franchomme ML_base, ML_bits,
542*06042735SVincent Franchomme ML_defaultDTable, dctx->fseEntropy,
543*06042735SVincent Franchomme dctx->ddictIsCold, nbSeq);
544*06042735SVincent Franchomme RETURN_ERROR_IF(ZSTD_isError(mlhSize), corruption_detected, "ZSTD_buildSeqTable failed");
545*06042735SVincent Franchomme ip += mlhSize;
546*06042735SVincent Franchomme }
547*06042735SVincent Franchomme }
548*06042735SVincent Franchomme
549*06042735SVincent Franchomme return ip-istart;
550*06042735SVincent Franchomme }
551*06042735SVincent Franchomme
552*06042735SVincent Franchomme
553*06042735SVincent Franchomme typedef struct {
554*06042735SVincent Franchomme size_t litLength;
555*06042735SVincent Franchomme size_t matchLength;
556*06042735SVincent Franchomme size_t offset;
557*06042735SVincent Franchomme const BYTE* match;
558*06042735SVincent Franchomme } seq_t;
559*06042735SVincent Franchomme
560*06042735SVincent Franchomme typedef struct {
561*06042735SVincent Franchomme size_t state;
562*06042735SVincent Franchomme const ZSTD_seqSymbol* table;
563*06042735SVincent Franchomme } ZSTD_fseState;
564*06042735SVincent Franchomme
565*06042735SVincent Franchomme typedef struct {
566*06042735SVincent Franchomme BIT_DStream_t DStream;
567*06042735SVincent Franchomme ZSTD_fseState stateLL;
568*06042735SVincent Franchomme ZSTD_fseState stateOffb;
569*06042735SVincent Franchomme ZSTD_fseState stateML;
570*06042735SVincent Franchomme size_t prevOffset[ZSTD_REP_NUM];
571*06042735SVincent Franchomme const BYTE* prefixStart;
572*06042735SVincent Franchomme const BYTE* dictEnd;
573*06042735SVincent Franchomme size_t pos;
574*06042735SVincent Franchomme } seqState_t;
575*06042735SVincent Franchomme
576*06042735SVincent Franchomme /*! ZSTD_overlapCopy8() :
577*06042735SVincent Franchomme * Copies 8 bytes from ip to op and updates op and ip where ip <= op.
578*06042735SVincent Franchomme * If the offset is < 8 then the offset is spread to at least 8 bytes.
579*06042735SVincent Franchomme *
580*06042735SVincent Franchomme * Precondition: *ip <= *op
581*06042735SVincent Franchomme * Postcondition: *op - *op >= 8
582*06042735SVincent Franchomme */
ZSTD_overlapCopy8(BYTE ** op,BYTE const ** ip,size_t offset)583*06042735SVincent Franchomme HINT_INLINE void ZSTD_overlapCopy8(BYTE** op, BYTE const** ip, size_t offset) {
584*06042735SVincent Franchomme assert(*ip <= *op);
585*06042735SVincent Franchomme if (offset < 8) {
586*06042735SVincent Franchomme /* close range match, overlap */
587*06042735SVincent Franchomme static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
588*06042735SVincent Franchomme static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */
589*06042735SVincent Franchomme int const sub2 = dec64table[offset];
590*06042735SVincent Franchomme (*op)[0] = (*ip)[0];
591*06042735SVincent Franchomme (*op)[1] = (*ip)[1];
592*06042735SVincent Franchomme (*op)[2] = (*ip)[2];
593*06042735SVincent Franchomme (*op)[3] = (*ip)[3];
594*06042735SVincent Franchomme *ip += dec32table[offset];
595*06042735SVincent Franchomme ZSTD_copy4(*op+4, *ip);
596*06042735SVincent Franchomme *ip -= sub2;
597*06042735SVincent Franchomme } else {
598*06042735SVincent Franchomme ZSTD_copy8(*op, *ip);
599*06042735SVincent Franchomme }
600*06042735SVincent Franchomme *ip += 8;
601*06042735SVincent Franchomme *op += 8;
602*06042735SVincent Franchomme assert(*op - *ip >= 8);
603*06042735SVincent Franchomme }
604*06042735SVincent Franchomme
605*06042735SVincent Franchomme /*! ZSTD_safecopy() :
606*06042735SVincent Franchomme * Specialized version of memcpy() that is allowed to READ up to WILDCOPY_OVERLENGTH past the input buffer
607*06042735SVincent Franchomme * and write up to 16 bytes past oend_w (op >= oend_w is allowed).
608*06042735SVincent Franchomme * This function is only called in the uncommon case where the sequence is near the end of the block. It
609*06042735SVincent Franchomme * should be fast for a single long sequence, but can be slow for several short sequences.
610*06042735SVincent Franchomme *
611*06042735SVincent Franchomme * @param ovtype controls the overlap detection
612*06042735SVincent Franchomme * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.
613*06042735SVincent Franchomme * - ZSTD_overlap_src_before_dst: The src and dst may overlap and may be any distance apart.
614*06042735SVincent Franchomme * The src buffer must be before the dst buffer.
615*06042735SVincent Franchomme */
ZSTD_safecopy(BYTE * op,BYTE * const oend_w,BYTE const * ip,ptrdiff_t length,ZSTD_overlap_e ovtype)616*06042735SVincent Franchomme static void ZSTD_safecopy(BYTE* op, BYTE* const oend_w, BYTE const* ip, ptrdiff_t length, ZSTD_overlap_e ovtype) {
617*06042735SVincent Franchomme ptrdiff_t const diff = op - ip;
618*06042735SVincent Franchomme BYTE* const oend = op + length;
619*06042735SVincent Franchomme
620*06042735SVincent Franchomme assert((ovtype == ZSTD_no_overlap && (diff <= -8 || diff >= 8 || op >= oend_w)) ||
621*06042735SVincent Franchomme (ovtype == ZSTD_overlap_src_before_dst && diff >= 0));
622*06042735SVincent Franchomme
623*06042735SVincent Franchomme if (length < 8) {
624*06042735SVincent Franchomme /* Handle short lengths. */
625*06042735SVincent Franchomme while (op < oend) *op++ = *ip++;
626*06042735SVincent Franchomme return;
627*06042735SVincent Franchomme }
628*06042735SVincent Franchomme if (ovtype == ZSTD_overlap_src_before_dst) {
629*06042735SVincent Franchomme /* Copy 8 bytes and ensure the offset >= 8 when there can be overlap. */
630*06042735SVincent Franchomme assert(length >= 8);
631*06042735SVincent Franchomme ZSTD_overlapCopy8(&op, &ip, diff);
632*06042735SVincent Franchomme assert(op - ip >= 8);
633*06042735SVincent Franchomme assert(op <= oend);
634*06042735SVincent Franchomme }
635*06042735SVincent Franchomme
636*06042735SVincent Franchomme if (oend <= oend_w) {
637*06042735SVincent Franchomme /* No risk of overwrite. */
638*06042735SVincent Franchomme ZSTD_wildcopy(op, ip, length, ovtype);
639*06042735SVincent Franchomme return;
640*06042735SVincent Franchomme }
641*06042735SVincent Franchomme if (op <= oend_w) {
642*06042735SVincent Franchomme /* Wildcopy until we get close to the end. */
643*06042735SVincent Franchomme assert(oend > oend_w);
644*06042735SVincent Franchomme ZSTD_wildcopy(op, ip, oend_w - op, ovtype);
645*06042735SVincent Franchomme ip += oend_w - op;
646*06042735SVincent Franchomme op = oend_w;
647*06042735SVincent Franchomme }
648*06042735SVincent Franchomme /* Handle the leftovers. */
649*06042735SVincent Franchomme while (op < oend) *op++ = *ip++;
650*06042735SVincent Franchomme }
651*06042735SVincent Franchomme
652*06042735SVincent Franchomme /* ZSTD_execSequenceEnd():
653*06042735SVincent Franchomme * This version handles cases that are near the end of the output buffer. It requires
654*06042735SVincent Franchomme * more careful checks to make sure there is no overflow. By separating out these hard
655*06042735SVincent Franchomme * and unlikely cases, we can speed up the common cases.
656*06042735SVincent Franchomme *
657*06042735SVincent Franchomme * NOTE: This function needs to be fast for a single long sequence, but doesn't need
658*06042735SVincent Franchomme * to be optimized for many small sequences, since those fall into ZSTD_execSequence().
659*06042735SVincent Franchomme */
660*06042735SVincent Franchomme FORCE_NOINLINE
ZSTD_execSequenceEnd(BYTE * op,BYTE * const oend,seq_t sequence,const BYTE ** litPtr,const BYTE * const litLimit,const BYTE * const prefixStart,const BYTE * const virtualStart,const BYTE * const dictEnd)661*06042735SVincent Franchomme size_t ZSTD_execSequenceEnd(BYTE* op,
662*06042735SVincent Franchomme BYTE* const oend, seq_t sequence,
663*06042735SVincent Franchomme const BYTE** litPtr, const BYTE* const litLimit,
664*06042735SVincent Franchomme const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd)
665*06042735SVincent Franchomme {
666*06042735SVincent Franchomme BYTE* const oLitEnd = op + sequence.litLength;
667*06042735SVincent Franchomme size_t const sequenceLength = sequence.litLength + sequence.matchLength;
668*06042735SVincent Franchomme const BYTE* const iLitEnd = *litPtr + sequence.litLength;
669*06042735SVincent Franchomme const BYTE* match = oLitEnd - sequence.offset;
670*06042735SVincent Franchomme BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH;
671*06042735SVincent Franchomme
672*06042735SVincent Franchomme /* bounds checks : careful of address space overflow in 32-bit mode */
673*06042735SVincent Franchomme RETURN_ERROR_IF(sequenceLength > (size_t)(oend - op), dstSize_tooSmall, "last match must fit within dstBuffer");
674*06042735SVincent Franchomme RETURN_ERROR_IF(sequence.litLength > (size_t)(litLimit - *litPtr), corruption_detected, "try to read beyond literal buffer");
675*06042735SVincent Franchomme assert(op < op + sequenceLength);
676*06042735SVincent Franchomme assert(oLitEnd < op + sequenceLength);
677*06042735SVincent Franchomme
678*06042735SVincent Franchomme /* copy literals */
679*06042735SVincent Franchomme ZSTD_safecopy(op, oend_w, *litPtr, sequence.litLength, ZSTD_no_overlap);
680*06042735SVincent Franchomme op = oLitEnd;
681*06042735SVincent Franchomme *litPtr = iLitEnd;
682*06042735SVincent Franchomme
683*06042735SVincent Franchomme /* copy Match */
684*06042735SVincent Franchomme if (sequence.offset > (size_t)(oLitEnd - prefixStart)) {
685*06042735SVincent Franchomme /* offset beyond prefix */
686*06042735SVincent Franchomme RETURN_ERROR_IF(sequence.offset > (size_t)(oLitEnd - virtualStart), corruption_detected, "");
687*06042735SVincent Franchomme match = dictEnd - (prefixStart-match);
688*06042735SVincent Franchomme if (match + sequence.matchLength <= dictEnd) {
689*06042735SVincent Franchomme memmove(oLitEnd, match, sequence.matchLength);
690*06042735SVincent Franchomme return sequenceLength;
691*06042735SVincent Franchomme }
692*06042735SVincent Franchomme /* span extDict & currentPrefixSegment */
693*06042735SVincent Franchomme { size_t const length1 = dictEnd - match;
694*06042735SVincent Franchomme memmove(oLitEnd, match, length1);
695*06042735SVincent Franchomme op = oLitEnd + length1;
696*06042735SVincent Franchomme sequence.matchLength -= length1;
697*06042735SVincent Franchomme match = prefixStart;
698*06042735SVincent Franchomme } }
699*06042735SVincent Franchomme ZSTD_safecopy(op, oend_w, match, sequence.matchLength, ZSTD_overlap_src_before_dst);
700*06042735SVincent Franchomme return sequenceLength;
701*06042735SVincent Franchomme }
702*06042735SVincent Franchomme
703*06042735SVincent Franchomme HINT_INLINE
ZSTD_execSequence(BYTE * op,BYTE * const oend,seq_t sequence,const BYTE ** litPtr,const BYTE * const litLimit,const BYTE * const prefixStart,const BYTE * const virtualStart,const BYTE * const dictEnd)704*06042735SVincent Franchomme size_t ZSTD_execSequence(BYTE* op,
705*06042735SVincent Franchomme BYTE* const oend, seq_t sequence,
706*06042735SVincent Franchomme const BYTE** litPtr, const BYTE* const litLimit,
707*06042735SVincent Franchomme const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd)
708*06042735SVincent Franchomme {
709*06042735SVincent Franchomme BYTE* const oLitEnd = op + sequence.litLength;
710*06042735SVincent Franchomme size_t const sequenceLength = sequence.litLength + sequence.matchLength;
711*06042735SVincent Franchomme BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
712*06042735SVincent Franchomme BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH; /* risk : address space underflow on oend=NULL */
713*06042735SVincent Franchomme const BYTE* const iLitEnd = *litPtr + sequence.litLength;
714*06042735SVincent Franchomme const BYTE* match = oLitEnd - sequence.offset;
715*06042735SVincent Franchomme
716*06042735SVincent Franchomme assert(op != NULL /* Precondition */);
717*06042735SVincent Franchomme assert(oend_w < oend /* No underflow */);
718*06042735SVincent Franchomme /* Handle edge cases in a slow path:
719*06042735SVincent Franchomme * - Read beyond end of literals
720*06042735SVincent Franchomme * - Match end is within WILDCOPY_OVERLIMIT of oend
721*06042735SVincent Franchomme * - 32-bit mode and the match length overflows
722*06042735SVincent Franchomme */
723*06042735SVincent Franchomme if (UNLIKELY(
724*06042735SVincent Franchomme iLitEnd > litLimit ||
725*06042735SVincent Franchomme oMatchEnd > oend_w ||
726*06042735SVincent Franchomme (MEM_32bits() && (size_t)(oend - op) < sequenceLength + WILDCOPY_OVERLENGTH)))
727*06042735SVincent Franchomme return ZSTD_execSequenceEnd(op, oend, sequence, litPtr, litLimit, prefixStart, virtualStart, dictEnd);
728*06042735SVincent Franchomme
729*06042735SVincent Franchomme /* Assumptions (everything else goes into ZSTD_execSequenceEnd()) */
730*06042735SVincent Franchomme assert(op <= oLitEnd /* No overflow */);
731*06042735SVincent Franchomme assert(oLitEnd < oMatchEnd /* Non-zero match & no overflow */);
732*06042735SVincent Franchomme assert(oMatchEnd <= oend /* No underflow */);
733*06042735SVincent Franchomme assert(iLitEnd <= litLimit /* Literal length is in bounds */);
734*06042735SVincent Franchomme assert(oLitEnd <= oend_w /* Can wildcopy literals */);
735*06042735SVincent Franchomme assert(oMatchEnd <= oend_w /* Can wildcopy matches */);
736*06042735SVincent Franchomme
737*06042735SVincent Franchomme /* Copy Literals:
738*06042735SVincent Franchomme * Split out litLength <= 16 since it is nearly always true. +1.6% on gcc-9.
739*06042735SVincent Franchomme * We likely don't need the full 32-byte wildcopy.
740*06042735SVincent Franchomme */
741*06042735SVincent Franchomme assert(WILDCOPY_OVERLENGTH >= 16);
742*06042735SVincent Franchomme ZSTD_copy16(op, (*litPtr));
743*06042735SVincent Franchomme if (UNLIKELY(sequence.litLength > 16)) {
744*06042735SVincent Franchomme ZSTD_wildcopy(op+16, (*litPtr)+16, sequence.litLength-16, ZSTD_no_overlap);
745*06042735SVincent Franchomme }
746*06042735SVincent Franchomme op = oLitEnd;
747*06042735SVincent Franchomme *litPtr = iLitEnd; /* update for next sequence */
748*06042735SVincent Franchomme
749*06042735SVincent Franchomme /* Copy Match */
750*06042735SVincent Franchomme if (sequence.offset > (size_t)(oLitEnd - prefixStart)) {
751*06042735SVincent Franchomme /* offset beyond prefix -> go into extDict */
752*06042735SVincent Franchomme RETURN_ERROR_IF(UNLIKELY(sequence.offset > (size_t)(oLitEnd - virtualStart)), corruption_detected, "");
753*06042735SVincent Franchomme match = dictEnd + (match - prefixStart);
754*06042735SVincent Franchomme if (match + sequence.matchLength <= dictEnd) {
755*06042735SVincent Franchomme memmove(oLitEnd, match, sequence.matchLength);
756*06042735SVincent Franchomme return sequenceLength;
757*06042735SVincent Franchomme }
758*06042735SVincent Franchomme /* span extDict & currentPrefixSegment */
759*06042735SVincent Franchomme { size_t const length1 = dictEnd - match;
760*06042735SVincent Franchomme memmove(oLitEnd, match, length1);
761*06042735SVincent Franchomme op = oLitEnd + length1;
762*06042735SVincent Franchomme sequence.matchLength -= length1;
763*06042735SVincent Franchomme match = prefixStart;
764*06042735SVincent Franchomme } }
765*06042735SVincent Franchomme /* Match within prefix of 1 or more bytes */
766*06042735SVincent Franchomme assert(op <= oMatchEnd);
767*06042735SVincent Franchomme assert(oMatchEnd <= oend_w);
768*06042735SVincent Franchomme assert(match >= prefixStart);
769*06042735SVincent Franchomme assert(sequence.matchLength >= 1);
770*06042735SVincent Franchomme
771*06042735SVincent Franchomme /* Nearly all offsets are >= WILDCOPY_VECLEN bytes, which means we can use wildcopy
772*06042735SVincent Franchomme * without overlap checking.
773*06042735SVincent Franchomme */
774*06042735SVincent Franchomme if (LIKELY(sequence.offset >= WILDCOPY_VECLEN)) {
775*06042735SVincent Franchomme /* We bet on a full wildcopy for matches, since we expect matches to be
776*06042735SVincent Franchomme * longer than literals (in general). In silesia, ~10% of matches are longer
777*06042735SVincent Franchomme * than 16 bytes.
778*06042735SVincent Franchomme */
779*06042735SVincent Franchomme ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength, ZSTD_no_overlap);
780*06042735SVincent Franchomme return sequenceLength;
781*06042735SVincent Franchomme }
782*06042735SVincent Franchomme assert(sequence.offset < WILDCOPY_VECLEN);
783*06042735SVincent Franchomme
784*06042735SVincent Franchomme /* Copy 8 bytes and spread the offset to be >= 8. */
785*06042735SVincent Franchomme ZSTD_overlapCopy8(&op, &match, sequence.offset);
786*06042735SVincent Franchomme
787*06042735SVincent Franchomme /* If the match length is > 8 bytes, then continue with the wildcopy. */
788*06042735SVincent Franchomme if (sequence.matchLength > 8) {
789*06042735SVincent Franchomme assert(op < oMatchEnd);
790*06042735SVincent Franchomme ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8, ZSTD_overlap_src_before_dst);
791*06042735SVincent Franchomme }
792*06042735SVincent Franchomme return sequenceLength;
793*06042735SVincent Franchomme }
794*06042735SVincent Franchomme
795*06042735SVincent Franchomme static void
ZSTD_initFseState(ZSTD_fseState * DStatePtr,BIT_DStream_t * bitD,const ZSTD_seqSymbol * dt)796*06042735SVincent Franchomme ZSTD_initFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, const ZSTD_seqSymbol* dt)
797*06042735SVincent Franchomme {
798*06042735SVincent Franchomme const void* ptr = dt;
799*06042735SVincent Franchomme const ZSTD_seqSymbol_header* const DTableH = (const ZSTD_seqSymbol_header*)ptr;
800*06042735SVincent Franchomme DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog);
801*06042735SVincent Franchomme DEBUGLOG(6, "ZSTD_initFseState : val=%u using %u bits",
802*06042735SVincent Franchomme (U32)DStatePtr->state, DTableH->tableLog);
803*06042735SVincent Franchomme BIT_reloadDStream(bitD);
804*06042735SVincent Franchomme DStatePtr->table = dt + 1;
805*06042735SVincent Franchomme }
806*06042735SVincent Franchomme
807*06042735SVincent Franchomme FORCE_INLINE_TEMPLATE void
ZSTD_updateFseState(ZSTD_fseState * DStatePtr,BIT_DStream_t * bitD)808*06042735SVincent Franchomme ZSTD_updateFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD)
809*06042735SVincent Franchomme {
810*06042735SVincent Franchomme ZSTD_seqSymbol const DInfo = DStatePtr->table[DStatePtr->state];
811*06042735SVincent Franchomme U32 const nbBits = DInfo.nbBits;
812*06042735SVincent Franchomme size_t const lowBits = BIT_readBits(bitD, nbBits);
813*06042735SVincent Franchomme DStatePtr->state = DInfo.nextState + lowBits;
814*06042735SVincent Franchomme }
815*06042735SVincent Franchomme
816*06042735SVincent Franchomme FORCE_INLINE_TEMPLATE void
ZSTD_updateFseStateWithDInfo(ZSTD_fseState * DStatePtr,BIT_DStream_t * bitD,ZSTD_seqSymbol const DInfo)817*06042735SVincent Franchomme ZSTD_updateFseStateWithDInfo(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, ZSTD_seqSymbol const DInfo)
818*06042735SVincent Franchomme {
819*06042735SVincent Franchomme U32 const nbBits = DInfo.nbBits;
820*06042735SVincent Franchomme size_t const lowBits = BIT_readBits(bitD, nbBits);
821*06042735SVincent Franchomme DStatePtr->state = DInfo.nextState + lowBits;
822*06042735SVincent Franchomme }
823*06042735SVincent Franchomme
824*06042735SVincent Franchomme /* We need to add at most (ZSTD_WINDOWLOG_MAX_32 - 1) bits to read the maximum
825*06042735SVincent Franchomme * offset bits. But we can only read at most (STREAM_ACCUMULATOR_MIN_32 - 1)
826*06042735SVincent Franchomme * bits before reloading. This value is the maximum number of bytes we read
827*06042735SVincent Franchomme * after reloading when we are decoding long offsets.
828*06042735SVincent Franchomme */
829*06042735SVincent Franchomme #define LONG_OFFSETS_MAX_EXTRA_BITS_32 \
830*06042735SVincent Franchomme (ZSTD_WINDOWLOG_MAX_32 > STREAM_ACCUMULATOR_MIN_32 \
831*06042735SVincent Franchomme ? ZSTD_WINDOWLOG_MAX_32 - STREAM_ACCUMULATOR_MIN_32 \
832*06042735SVincent Franchomme : 0)
833*06042735SVincent Franchomme
834*06042735SVincent Franchomme typedef enum { ZSTD_lo_isRegularOffset, ZSTD_lo_isLongOffset=1 } ZSTD_longOffset_e;
835*06042735SVincent Franchomme typedef enum { ZSTD_p_noPrefetch=0, ZSTD_p_prefetch=1 } ZSTD_prefetch_e;
836*06042735SVincent Franchomme
837*06042735SVincent Franchomme FORCE_INLINE_TEMPLATE seq_t
ZSTD_decodeSequence(seqState_t * seqState,const ZSTD_longOffset_e longOffsets,const ZSTD_prefetch_e prefetch)838*06042735SVincent Franchomme ZSTD_decodeSequence(seqState_t* seqState, const ZSTD_longOffset_e longOffsets, const ZSTD_prefetch_e prefetch)
839*06042735SVincent Franchomme {
840*06042735SVincent Franchomme seq_t seq;
841*06042735SVincent Franchomme ZSTD_seqSymbol const llDInfo = seqState->stateLL.table[seqState->stateLL.state];
842*06042735SVincent Franchomme ZSTD_seqSymbol const mlDInfo = seqState->stateML.table[seqState->stateML.state];
843*06042735SVincent Franchomme ZSTD_seqSymbol const ofDInfo = seqState->stateOffb.table[seqState->stateOffb.state];
844*06042735SVincent Franchomme U32 const llBase = llDInfo.baseValue;
845*06042735SVincent Franchomme U32 const mlBase = mlDInfo.baseValue;
846*06042735SVincent Franchomme U32 const ofBase = ofDInfo.baseValue;
847*06042735SVincent Franchomme BYTE const llBits = llDInfo.nbAdditionalBits;
848*06042735SVincent Franchomme BYTE const mlBits = mlDInfo.nbAdditionalBits;
849*06042735SVincent Franchomme BYTE const ofBits = ofDInfo.nbAdditionalBits;
850*06042735SVincent Franchomme BYTE const totalBits = llBits+mlBits+ofBits;
851*06042735SVincent Franchomme
852*06042735SVincent Franchomme /* sequence */
853*06042735SVincent Franchomme { size_t offset;
854*06042735SVincent Franchomme if (ofBits > 1) {
855*06042735SVincent Franchomme ZSTD_STATIC_ASSERT(ZSTD_lo_isLongOffset == 1);
856*06042735SVincent Franchomme ZSTD_STATIC_ASSERT(LONG_OFFSETS_MAX_EXTRA_BITS_32 == 5);
857*06042735SVincent Franchomme assert(ofBits <= MaxOff);
858*06042735SVincent Franchomme if (MEM_32bits() && longOffsets && (ofBits >= STREAM_ACCUMULATOR_MIN_32)) {
859*06042735SVincent Franchomme U32 const extraBits = ofBits - MIN(ofBits, 32 - seqState->DStream.bitsConsumed);
860*06042735SVincent Franchomme offset = ofBase + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << extraBits);
861*06042735SVincent Franchomme BIT_reloadDStream(&seqState->DStream);
862*06042735SVincent Franchomme if (extraBits) offset += BIT_readBitsFast(&seqState->DStream, extraBits);
863*06042735SVincent Franchomme assert(extraBits <= LONG_OFFSETS_MAX_EXTRA_BITS_32); /* to avoid another reload */
864*06042735SVincent Franchomme } else {
865*06042735SVincent Franchomme offset = ofBase + BIT_readBitsFast(&seqState->DStream, ofBits/*>0*/); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */
866*06042735SVincent Franchomme if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream);
867*06042735SVincent Franchomme }
868*06042735SVincent Franchomme seqState->prevOffset[2] = seqState->prevOffset[1];
869*06042735SVincent Franchomme seqState->prevOffset[1] = seqState->prevOffset[0];
870*06042735SVincent Franchomme seqState->prevOffset[0] = offset;
871*06042735SVincent Franchomme } else {
872*06042735SVincent Franchomme U32 const ll0 = (llBase == 0);
873*06042735SVincent Franchomme if (LIKELY((ofBits == 0))) {
874*06042735SVincent Franchomme if (LIKELY(!ll0))
875*06042735SVincent Franchomme offset = seqState->prevOffset[0];
876*06042735SVincent Franchomme else {
877*06042735SVincent Franchomme offset = seqState->prevOffset[1];
878*06042735SVincent Franchomme seqState->prevOffset[1] = seqState->prevOffset[0];
879*06042735SVincent Franchomme seqState->prevOffset[0] = offset;
880*06042735SVincent Franchomme }
881*06042735SVincent Franchomme } else {
882*06042735SVincent Franchomme offset = ofBase + ll0 + BIT_readBitsFast(&seqState->DStream, 1);
883*06042735SVincent Franchomme { size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset];
884*06042735SVincent Franchomme temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */
885*06042735SVincent Franchomme if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1];
886*06042735SVincent Franchomme seqState->prevOffset[1] = seqState->prevOffset[0];
887*06042735SVincent Franchomme seqState->prevOffset[0] = offset = temp;
888*06042735SVincent Franchomme } } }
889*06042735SVincent Franchomme seq.offset = offset;
890*06042735SVincent Franchomme }
891*06042735SVincent Franchomme
892*06042735SVincent Franchomme seq.matchLength = mlBase;
893*06042735SVincent Franchomme if (mlBits > 0)
894*06042735SVincent Franchomme seq.matchLength += BIT_readBitsFast(&seqState->DStream, mlBits/*>0*/);
895*06042735SVincent Franchomme
896*06042735SVincent Franchomme if (MEM_32bits() && (mlBits+llBits >= STREAM_ACCUMULATOR_MIN_32-LONG_OFFSETS_MAX_EXTRA_BITS_32))
897*06042735SVincent Franchomme BIT_reloadDStream(&seqState->DStream);
898*06042735SVincent Franchomme if (MEM_64bits() && UNLIKELY(totalBits >= STREAM_ACCUMULATOR_MIN_64-(LLFSELog+MLFSELog+OffFSELog)))
899*06042735SVincent Franchomme BIT_reloadDStream(&seqState->DStream);
900*06042735SVincent Franchomme /* Ensure there are enough bits to read the rest of data in 64-bit mode. */
901*06042735SVincent Franchomme ZSTD_STATIC_ASSERT(16+LLFSELog+MLFSELog+OffFSELog < STREAM_ACCUMULATOR_MIN_64);
902*06042735SVincent Franchomme
903*06042735SVincent Franchomme seq.litLength = llBase;
904*06042735SVincent Franchomme if (llBits > 0)
905*06042735SVincent Franchomme seq.litLength += BIT_readBitsFast(&seqState->DStream, llBits/*>0*/);
906*06042735SVincent Franchomme
907*06042735SVincent Franchomme if (MEM_32bits())
908*06042735SVincent Franchomme BIT_reloadDStream(&seqState->DStream);
909*06042735SVincent Franchomme
910*06042735SVincent Franchomme DEBUGLOG(6, "seq: litL=%u, matchL=%u, offset=%u",
911*06042735SVincent Franchomme (U32)seq.litLength, (U32)seq.matchLength, (U32)seq.offset);
912*06042735SVincent Franchomme
913*06042735SVincent Franchomme if (prefetch == ZSTD_p_prefetch) {
914*06042735SVincent Franchomme size_t const pos = seqState->pos + seq.litLength;
915*06042735SVincent Franchomme const BYTE* const matchBase = (seq.offset > pos) ? seqState->dictEnd : seqState->prefixStart;
916*06042735SVincent Franchomme seq.match = matchBase + pos - seq.offset; /* note : this operation can overflow when seq.offset is really too large, which can only happen when input is corrupted.
917*06042735SVincent Franchomme * No consequence though : no memory access will occur, offset is only used for prefetching */
918*06042735SVincent Franchomme seqState->pos = pos + seq.matchLength;
919*06042735SVincent Franchomme }
920*06042735SVincent Franchomme
921*06042735SVincent Franchomme /* ANS state update
922*06042735SVincent Franchomme * gcc-9.0.0 does 2.5% worse with ZSTD_updateFseStateWithDInfo().
923*06042735SVincent Franchomme * clang-9.2.0 does 7% worse with ZSTD_updateFseState().
924*06042735SVincent Franchomme * Naturally it seems like ZSTD_updateFseStateWithDInfo() should be the
925*06042735SVincent Franchomme * better option, so it is the default for other compilers. But, if you
926*06042735SVincent Franchomme * measure that it is worse, please put up a pull request.
927*06042735SVincent Franchomme */
928*06042735SVincent Franchomme {
929*06042735SVincent Franchomme #if defined(__GNUC__) && !defined(__clang__)
930*06042735SVincent Franchomme const int kUseUpdateFseState = 1;
931*06042735SVincent Franchomme #else
932*06042735SVincent Franchomme const int kUseUpdateFseState = 0;
933*06042735SVincent Franchomme #endif
934*06042735SVincent Franchomme if (kUseUpdateFseState) {
935*06042735SVincent Franchomme ZSTD_updateFseState(&seqState->stateLL, &seqState->DStream); /* <= 9 bits */
936*06042735SVincent Franchomme ZSTD_updateFseState(&seqState->stateML, &seqState->DStream); /* <= 9 bits */
937*06042735SVincent Franchomme if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */
938*06042735SVincent Franchomme ZSTD_updateFseState(&seqState->stateOffb, &seqState->DStream); /* <= 8 bits */
939*06042735SVincent Franchomme } else {
940*06042735SVincent Franchomme ZSTD_updateFseStateWithDInfo(&seqState->stateLL, &seqState->DStream, llDInfo); /* <= 9 bits */
941*06042735SVincent Franchomme ZSTD_updateFseStateWithDInfo(&seqState->stateML, &seqState->DStream, mlDInfo); /* <= 9 bits */
942*06042735SVincent Franchomme if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */
943*06042735SVincent Franchomme ZSTD_updateFseStateWithDInfo(&seqState->stateOffb, &seqState->DStream, ofDInfo); /* <= 8 bits */
944*06042735SVincent Franchomme }
945*06042735SVincent Franchomme }
946*06042735SVincent Franchomme
947*06042735SVincent Franchomme return seq;
948*06042735SVincent Franchomme }
949*06042735SVincent Franchomme
950*06042735SVincent Franchomme #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
ZSTD_dictionaryIsActive(ZSTD_DCtx const * dctx,BYTE const * prefixStart,BYTE const * oLitEnd)951*06042735SVincent Franchomme static int ZSTD_dictionaryIsActive(ZSTD_DCtx const* dctx, BYTE const* prefixStart, BYTE const* oLitEnd)
952*06042735SVincent Franchomme {
953*06042735SVincent Franchomme size_t const windowSize = dctx->fParams.windowSize;
954*06042735SVincent Franchomme /* No dictionary used. */
955*06042735SVincent Franchomme if (dctx->dictContentEndForFuzzing == NULL) return 0;
956*06042735SVincent Franchomme /* Dictionary is our prefix. */
957*06042735SVincent Franchomme if (prefixStart == dctx->dictContentBeginForFuzzing) return 1;
958*06042735SVincent Franchomme /* Dictionary is not our ext-dict. */
959*06042735SVincent Franchomme if (dctx->dictEnd != dctx->dictContentEndForFuzzing) return 0;
960*06042735SVincent Franchomme /* Dictionary is not within our window size. */
961*06042735SVincent Franchomme if ((size_t)(oLitEnd - prefixStart) >= windowSize) return 0;
962*06042735SVincent Franchomme /* Dictionary is active. */
963*06042735SVincent Franchomme return 1;
964*06042735SVincent Franchomme }
965*06042735SVincent Franchomme
ZSTD_assertValidSequence(ZSTD_DCtx const * dctx,BYTE const * op,BYTE const * oend,seq_t const seq,BYTE const * prefixStart,BYTE const * virtualStart)966*06042735SVincent Franchomme MEM_STATIC void ZSTD_assertValidSequence(
967*06042735SVincent Franchomme ZSTD_DCtx const* dctx,
968*06042735SVincent Franchomme BYTE const* op, BYTE const* oend,
969*06042735SVincent Franchomme seq_t const seq,
970*06042735SVincent Franchomme BYTE const* prefixStart, BYTE const* virtualStart)
971*06042735SVincent Franchomme {
972*06042735SVincent Franchomme size_t const windowSize = dctx->fParams.windowSize;
973*06042735SVincent Franchomme size_t const sequenceSize = seq.litLength + seq.matchLength;
974*06042735SVincent Franchomme BYTE const* const oLitEnd = op + seq.litLength;
975*06042735SVincent Franchomme DEBUGLOG(6, "Checking sequence: litL=%u matchL=%u offset=%u",
976*06042735SVincent Franchomme (U32)seq.litLength, (U32)seq.matchLength, (U32)seq.offset);
977*06042735SVincent Franchomme assert(op <= oend);
978*06042735SVincent Franchomme assert((size_t)(oend - op) >= sequenceSize);
979*06042735SVincent Franchomme assert(sequenceSize <= ZSTD_BLOCKSIZE_MAX);
980*06042735SVincent Franchomme if (ZSTD_dictionaryIsActive(dctx, prefixStart, oLitEnd)) {
981*06042735SVincent Franchomme size_t const dictSize = (size_t)((char const*)dctx->dictContentEndForFuzzing - (char const*)dctx->dictContentBeginForFuzzing);
982*06042735SVincent Franchomme /* Offset must be within the dictionary. */
983*06042735SVincent Franchomme assert(seq.offset <= (size_t)(oLitEnd - virtualStart));
984*06042735SVincent Franchomme assert(seq.offset <= windowSize + dictSize);
985*06042735SVincent Franchomme } else {
986*06042735SVincent Franchomme /* Offset must be within our window. */
987*06042735SVincent Franchomme assert(seq.offset <= windowSize);
988*06042735SVincent Franchomme }
989*06042735SVincent Franchomme }
990*06042735SVincent Franchomme #endif
991*06042735SVincent Franchomme
992*06042735SVincent Franchomme #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
993*06042735SVincent Franchomme FORCE_INLINE_TEMPLATE size_t
994*06042735SVincent Franchomme DONT_VECTORIZE
ZSTD_decompressSequences_body(ZSTD_DCtx * dctx,void * dst,size_t maxDstSize,const void * seqStart,size_t seqSize,int nbSeq,const ZSTD_longOffset_e isLongOffset,const int frame)995*06042735SVincent Franchomme ZSTD_decompressSequences_body( ZSTD_DCtx* dctx,
996*06042735SVincent Franchomme void* dst, size_t maxDstSize,
997*06042735SVincent Franchomme const void* seqStart, size_t seqSize, int nbSeq,
998*06042735SVincent Franchomme const ZSTD_longOffset_e isLongOffset,
999*06042735SVincent Franchomme const int frame)
1000*06042735SVincent Franchomme {
1001*06042735SVincent Franchomme const BYTE* ip = (const BYTE*)seqStart;
1002*06042735SVincent Franchomme const BYTE* const iend = ip + seqSize;
1003*06042735SVincent Franchomme BYTE* const ostart = (BYTE* const)dst;
1004*06042735SVincent Franchomme BYTE* const oend = ostart + maxDstSize;
1005*06042735SVincent Franchomme BYTE* op = ostart;
1006*06042735SVincent Franchomme const BYTE* litPtr = dctx->litPtr;
1007*06042735SVincent Franchomme const BYTE* const litEnd = litPtr + dctx->litSize;
1008*06042735SVincent Franchomme const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart);
1009*06042735SVincent Franchomme const BYTE* const vBase = (const BYTE*) (dctx->virtualStart);
1010*06042735SVincent Franchomme const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
1011*06042735SVincent Franchomme DEBUGLOG(5, "ZSTD_decompressSequences_body");
1012*06042735SVincent Franchomme (void)frame;
1013*06042735SVincent Franchomme
1014*06042735SVincent Franchomme /* Regen sequences */
1015*06042735SVincent Franchomme if (nbSeq) {
1016*06042735SVincent Franchomme seqState_t seqState;
1017*06042735SVincent Franchomme size_t error = 0;
1018*06042735SVincent Franchomme dctx->fseEntropy = 1;
1019*06042735SVincent Franchomme { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; }
1020*06042735SVincent Franchomme RETURN_ERROR_IF(
1021*06042735SVincent Franchomme ERR_isError(BIT_initDStream(&seqState.DStream, ip, iend-ip)),
1022*06042735SVincent Franchomme corruption_detected, "");
1023*06042735SVincent Franchomme ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);
1024*06042735SVincent Franchomme ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);
1025*06042735SVincent Franchomme ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);
1026*06042735SVincent Franchomme assert(dst != NULL);
1027*06042735SVincent Franchomme
1028*06042735SVincent Franchomme ZSTD_STATIC_ASSERT(
1029*06042735SVincent Franchomme BIT_DStream_unfinished < BIT_DStream_completed &&
1030*06042735SVincent Franchomme BIT_DStream_endOfBuffer < BIT_DStream_completed &&
1031*06042735SVincent Franchomme BIT_DStream_completed < BIT_DStream_overflow);
1032*06042735SVincent Franchomme
1033*06042735SVincent Franchomme #if defined(__GNUC__) && defined(__x86_64__)
1034*06042735SVincent Franchomme /* Align the decompression loop to 32 + 16 bytes.
1035*06042735SVincent Franchomme *
1036*06042735SVincent Franchomme * zstd compiled with gcc-9 on an Intel i9-9900k shows 10% decompression
1037*06042735SVincent Franchomme * speed swings based on the alignment of the decompression loop. This
1038*06042735SVincent Franchomme * performance swing is caused by parts of the decompression loop falling
1039*06042735SVincent Franchomme * out of the DSB. The entire decompression loop should fit in the DSB,
1040*06042735SVincent Franchomme * when it can't we get much worse performance. You can measure if you've
1041*06042735SVincent Franchomme * hit the good case or the bad case with this perf command for some
1042*06042735SVincent Franchomme * compressed file test.zst:
1043*06042735SVincent Franchomme *
1044*06042735SVincent Franchomme * perf stat -e cycles -e instructions -e idq.all_dsb_cycles_any_uops \
1045*06042735SVincent Franchomme * -e idq.all_mite_cycles_any_uops -- ./zstd -tq test.zst
1046*06042735SVincent Franchomme *
1047*06042735SVincent Franchomme * If you see most cycles served out of the MITE you've hit the bad case.
1048*06042735SVincent Franchomme * If you see most cycles served out of the DSB you've hit the good case.
1049*06042735SVincent Franchomme * If it is pretty even then you may be in an okay case.
1050*06042735SVincent Franchomme *
1051*06042735SVincent Franchomme * I've been able to reproduce this issue on the following CPUs:
1052*06042735SVincent Franchomme * - Kabylake: Macbook Pro (15-inch, 2019) 2.4 GHz Intel Core i9
1053*06042735SVincent Franchomme * Use Instruments->Counters to get DSB/MITE cycles.
1054*06042735SVincent Franchomme * I never got performance swings, but I was able to
1055*06042735SVincent Franchomme * go from the good case of mostly DSB to half of the
1056*06042735SVincent Franchomme * cycles served from MITE.
1057*06042735SVincent Franchomme * - Coffeelake: Intel i9-9900k
1058*06042735SVincent Franchomme *
1059*06042735SVincent Franchomme * I haven't been able to reproduce the instability or DSB misses on any
1060*06042735SVincent Franchomme * of the following CPUS:
1061*06042735SVincent Franchomme * - Haswell
1062*06042735SVincent Franchomme * - Broadwell: Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GH
1063*06042735SVincent Franchomme * - Skylake
1064*06042735SVincent Franchomme *
1065*06042735SVincent Franchomme * If you are seeing performance stability this script can help test.
1066*06042735SVincent Franchomme * It tests on 4 commits in zstd where I saw performance change.
1067*06042735SVincent Franchomme *
1068*06042735SVincent Franchomme * https://gist.github.com/terrelln/9889fc06a423fd5ca6e99351564473f4
1069*06042735SVincent Franchomme */
1070*06042735SVincent Franchomme __asm__(".p2align 5");
1071*06042735SVincent Franchomme __asm__("nop");
1072*06042735SVincent Franchomme __asm__(".p2align 4");
1073*06042735SVincent Franchomme #endif
1074*06042735SVincent Franchomme for ( ; ; ) {
1075*06042735SVincent Franchomme seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset, ZSTD_p_noPrefetch);
1076*06042735SVincent Franchomme size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, prefixStart, vBase, dictEnd);
1077*06042735SVincent Franchomme #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE)
1078*06042735SVincent Franchomme assert(!ZSTD_isError(oneSeqSize));
1079*06042735SVincent Franchomme if (frame) ZSTD_assertValidSequence(dctx, op, oend, sequence, prefixStart, vBase);
1080*06042735SVincent Franchomme #endif
1081*06042735SVincent Franchomme DEBUGLOG(6, "regenerated sequence size : %u", (U32)oneSeqSize);
1082*06042735SVincent Franchomme BIT_reloadDStream(&(seqState.DStream));
1083*06042735SVincent Franchomme /* gcc and clang both don't like early returns in this loop.
1084*06042735SVincent Franchomme * gcc doesn't like early breaks either.
1085*06042735SVincent Franchomme * Instead save an error and report it at the end.
1086*06042735SVincent Franchomme * When there is an error, don't increment op, so we don't
1087*06042735SVincent Franchomme * overwrite.
1088*06042735SVincent Franchomme */
1089*06042735SVincent Franchomme if (UNLIKELY(ZSTD_isError(oneSeqSize))) error = oneSeqSize;
1090*06042735SVincent Franchomme else op += oneSeqSize;
1091*06042735SVincent Franchomme if (UNLIKELY(!--nbSeq)) break;
1092*06042735SVincent Franchomme }
1093*06042735SVincent Franchomme
1094*06042735SVincent Franchomme /* check if reached exact end */
1095*06042735SVincent Franchomme DEBUGLOG(5, "ZSTD_decompressSequences_body: after decode loop, remaining nbSeq : %i", nbSeq);
1096*06042735SVincent Franchomme if (ZSTD_isError(error)) return error;
1097*06042735SVincent Franchomme RETURN_ERROR_IF(nbSeq, corruption_detected, "");
1098*06042735SVincent Franchomme RETURN_ERROR_IF(BIT_reloadDStream(&seqState.DStream) < BIT_DStream_completed, corruption_detected, "");
1099*06042735SVincent Franchomme /* save reps for next block */
1100*06042735SVincent Franchomme { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); }
1101*06042735SVincent Franchomme }
1102*06042735SVincent Franchomme
1103*06042735SVincent Franchomme /* last literal segment */
1104*06042735SVincent Franchomme { size_t const lastLLSize = litEnd - litPtr;
1105*06042735SVincent Franchomme RETURN_ERROR_IF(lastLLSize > (size_t)(oend-op), dstSize_tooSmall, "");
1106*06042735SVincent Franchomme if (op != NULL) {
1107*06042735SVincent Franchomme memcpy(op, litPtr, lastLLSize);
1108*06042735SVincent Franchomme op += lastLLSize;
1109*06042735SVincent Franchomme }
1110*06042735SVincent Franchomme }
1111*06042735SVincent Franchomme
1112*06042735SVincent Franchomme return op-ostart;
1113*06042735SVincent Franchomme }
1114*06042735SVincent Franchomme
1115*06042735SVincent Franchomme static size_t
ZSTD_decompressSequences_default(ZSTD_DCtx * dctx,void * dst,size_t maxDstSize,const void * seqStart,size_t seqSize,int nbSeq,const ZSTD_longOffset_e isLongOffset,const int frame)1116*06042735SVincent Franchomme ZSTD_decompressSequences_default(ZSTD_DCtx* dctx,
1117*06042735SVincent Franchomme void* dst, size_t maxDstSize,
1118*06042735SVincent Franchomme const void* seqStart, size_t seqSize, int nbSeq,
1119*06042735SVincent Franchomme const ZSTD_longOffset_e isLongOffset,
1120*06042735SVincent Franchomme const int frame)
1121*06042735SVincent Franchomme {
1122*06042735SVincent Franchomme return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
1123*06042735SVincent Franchomme }
1124*06042735SVincent Franchomme #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */
1125*06042735SVincent Franchomme
1126*06042735SVincent Franchomme #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
1127*06042735SVincent Franchomme FORCE_INLINE_TEMPLATE size_t
ZSTD_decompressSequencesLong_body(ZSTD_DCtx * dctx,void * dst,size_t maxDstSize,const void * seqStart,size_t seqSize,int nbSeq,const ZSTD_longOffset_e isLongOffset,const int frame)1128*06042735SVincent Franchomme ZSTD_decompressSequencesLong_body(
1129*06042735SVincent Franchomme ZSTD_DCtx* dctx,
1130*06042735SVincent Franchomme void* dst, size_t maxDstSize,
1131*06042735SVincent Franchomme const void* seqStart, size_t seqSize, int nbSeq,
1132*06042735SVincent Franchomme const ZSTD_longOffset_e isLongOffset,
1133*06042735SVincent Franchomme const int frame)
1134*06042735SVincent Franchomme {
1135*06042735SVincent Franchomme const BYTE* ip = (const BYTE*)seqStart;
1136*06042735SVincent Franchomme const BYTE* const iend = ip + seqSize;
1137*06042735SVincent Franchomme BYTE* const ostart = (BYTE* const)dst;
1138*06042735SVincent Franchomme BYTE* const oend = ostart + maxDstSize;
1139*06042735SVincent Franchomme BYTE* op = ostart;
1140*06042735SVincent Franchomme const BYTE* litPtr = dctx->litPtr;
1141*06042735SVincent Franchomme const BYTE* const litEnd = litPtr + dctx->litSize;
1142*06042735SVincent Franchomme const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart);
1143*06042735SVincent Franchomme const BYTE* const dictStart = (const BYTE*) (dctx->virtualStart);
1144*06042735SVincent Franchomme const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
1145*06042735SVincent Franchomme (void)frame;
1146*06042735SVincent Franchomme
1147*06042735SVincent Franchomme /* Regen sequences */
1148*06042735SVincent Franchomme if (nbSeq) {
1149*06042735SVincent Franchomme #define STORED_SEQS 4
1150*06042735SVincent Franchomme #define STORED_SEQS_MASK (STORED_SEQS-1)
1151*06042735SVincent Franchomme #define ADVANCED_SEQS 4
1152*06042735SVincent Franchomme seq_t sequences[STORED_SEQS];
1153*06042735SVincent Franchomme int const seqAdvance = MIN(nbSeq, ADVANCED_SEQS);
1154*06042735SVincent Franchomme seqState_t seqState;
1155*06042735SVincent Franchomme int seqNb;
1156*06042735SVincent Franchomme dctx->fseEntropy = 1;
1157*06042735SVincent Franchomme { int i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; }
1158*06042735SVincent Franchomme seqState.prefixStart = prefixStart;
1159*06042735SVincent Franchomme seqState.pos = (size_t)(op-prefixStart);
1160*06042735SVincent Franchomme seqState.dictEnd = dictEnd;
1161*06042735SVincent Franchomme assert(dst != NULL);
1162*06042735SVincent Franchomme assert(iend >= ip);
1163*06042735SVincent Franchomme RETURN_ERROR_IF(
1164*06042735SVincent Franchomme ERR_isError(BIT_initDStream(&seqState.DStream, ip, iend-ip)),
1165*06042735SVincent Franchomme corruption_detected, "");
1166*06042735SVincent Franchomme ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);
1167*06042735SVincent Franchomme ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);
1168*06042735SVincent Franchomme ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);
1169*06042735SVincent Franchomme
1170*06042735SVincent Franchomme /* prepare in advance */
1171*06042735SVincent Franchomme for (seqNb=0; (BIT_reloadDStream(&seqState.DStream) <= BIT_DStream_completed) && (seqNb<seqAdvance); seqNb++) {
1172*06042735SVincent Franchomme sequences[seqNb] = ZSTD_decodeSequence(&seqState, isLongOffset, ZSTD_p_prefetch);
1173*06042735SVincent Franchomme PREFETCH_L1(sequences[seqNb].match); PREFETCH_L1(sequences[seqNb].match + sequences[seqNb].matchLength - 1); /* note : it's safe to invoke PREFETCH() on any memory address, including invalid ones */
1174*06042735SVincent Franchomme }
1175*06042735SVincent Franchomme RETURN_ERROR_IF(seqNb<seqAdvance, corruption_detected, "");
1176*06042735SVincent Franchomme
1177*06042735SVincent Franchomme /* decode and decompress */
1178*06042735SVincent Franchomme for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && (seqNb<nbSeq) ; seqNb++) {
1179*06042735SVincent Franchomme seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset, ZSTD_p_prefetch);
1180*06042735SVincent Franchomme size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequences[(seqNb-ADVANCED_SEQS) & STORED_SEQS_MASK], &litPtr, litEnd, prefixStart, dictStart, dictEnd);
1181*06042735SVincent Franchomme #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE)
1182*06042735SVincent Franchomme assert(!ZSTD_isError(oneSeqSize));
1183*06042735SVincent Franchomme if (frame) ZSTD_assertValidSequence(dctx, op, oend, sequences[(seqNb-ADVANCED_SEQS) & STORED_SEQS_MASK], prefixStart, dictStart);
1184*06042735SVincent Franchomme #endif
1185*06042735SVincent Franchomme if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
1186*06042735SVincent Franchomme PREFETCH_L1(sequence.match); PREFETCH_L1(sequence.match + sequence.matchLength - 1); /* note : it's safe to invoke PREFETCH() on any memory address, including invalid ones */
1187*06042735SVincent Franchomme sequences[seqNb & STORED_SEQS_MASK] = sequence;
1188*06042735SVincent Franchomme op += oneSeqSize;
1189*06042735SVincent Franchomme }
1190*06042735SVincent Franchomme RETURN_ERROR_IF(seqNb<nbSeq, corruption_detected, "");
1191*06042735SVincent Franchomme
1192*06042735SVincent Franchomme /* finish queue */
1193*06042735SVincent Franchomme seqNb -= seqAdvance;
1194*06042735SVincent Franchomme for ( ; seqNb<nbSeq ; seqNb++) {
1195*06042735SVincent Franchomme size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequences[seqNb&STORED_SEQS_MASK], &litPtr, litEnd, prefixStart, dictStart, dictEnd);
1196*06042735SVincent Franchomme #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE)
1197*06042735SVincent Franchomme assert(!ZSTD_isError(oneSeqSize));
1198*06042735SVincent Franchomme if (frame) ZSTD_assertValidSequence(dctx, op, oend, sequences[seqNb&STORED_SEQS_MASK], prefixStart, dictStart);
1199*06042735SVincent Franchomme #endif
1200*06042735SVincent Franchomme if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
1201*06042735SVincent Franchomme op += oneSeqSize;
1202*06042735SVincent Franchomme }
1203*06042735SVincent Franchomme
1204*06042735SVincent Franchomme /* save reps for next block */
1205*06042735SVincent Franchomme { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); }
1206*06042735SVincent Franchomme }
1207*06042735SVincent Franchomme
1208*06042735SVincent Franchomme /* last literal segment */
1209*06042735SVincent Franchomme { size_t const lastLLSize = litEnd - litPtr;
1210*06042735SVincent Franchomme RETURN_ERROR_IF(lastLLSize > (size_t)(oend-op), dstSize_tooSmall, "");
1211*06042735SVincent Franchomme if (op != NULL) {
1212*06042735SVincent Franchomme memcpy(op, litPtr, lastLLSize);
1213*06042735SVincent Franchomme op += lastLLSize;
1214*06042735SVincent Franchomme }
1215*06042735SVincent Franchomme }
1216*06042735SVincent Franchomme
1217*06042735SVincent Franchomme return op-ostart;
1218*06042735SVincent Franchomme }
1219*06042735SVincent Franchomme
1220*06042735SVincent Franchomme static size_t
ZSTD_decompressSequencesLong_default(ZSTD_DCtx * dctx,void * dst,size_t maxDstSize,const void * seqStart,size_t seqSize,int nbSeq,const ZSTD_longOffset_e isLongOffset,const int frame)1221*06042735SVincent Franchomme ZSTD_decompressSequencesLong_default(ZSTD_DCtx* dctx,
1222*06042735SVincent Franchomme void* dst, size_t maxDstSize,
1223*06042735SVincent Franchomme const void* seqStart, size_t seqSize, int nbSeq,
1224*06042735SVincent Franchomme const ZSTD_longOffset_e isLongOffset,
1225*06042735SVincent Franchomme const int frame)
1226*06042735SVincent Franchomme {
1227*06042735SVincent Franchomme return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
1228*06042735SVincent Franchomme }
1229*06042735SVincent Franchomme #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */
1230*06042735SVincent Franchomme
1231*06042735SVincent Franchomme
1232*06042735SVincent Franchomme
1233*06042735SVincent Franchomme #if DYNAMIC_BMI2
1234*06042735SVincent Franchomme
1235*06042735SVincent Franchomme #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
1236*06042735SVincent Franchomme static TARGET_ATTRIBUTE("bmi2") size_t
1237*06042735SVincent Franchomme DONT_VECTORIZE
ZSTD_decompressSequences_bmi2(ZSTD_DCtx * dctx,void * dst,size_t maxDstSize,const void * seqStart,size_t seqSize,int nbSeq,const ZSTD_longOffset_e isLongOffset,const int frame)1238*06042735SVincent Franchomme ZSTD_decompressSequences_bmi2(ZSTD_DCtx* dctx,
1239*06042735SVincent Franchomme void* dst, size_t maxDstSize,
1240*06042735SVincent Franchomme const void* seqStart, size_t seqSize, int nbSeq,
1241*06042735SVincent Franchomme const ZSTD_longOffset_e isLongOffset,
1242*06042735SVincent Franchomme const int frame)
1243*06042735SVincent Franchomme {
1244*06042735SVincent Franchomme return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
1245*06042735SVincent Franchomme }
1246*06042735SVincent Franchomme #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */
1247*06042735SVincent Franchomme
1248*06042735SVincent Franchomme #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
1249*06042735SVincent Franchomme static TARGET_ATTRIBUTE("bmi2") size_t
ZSTD_decompressSequencesLong_bmi2(ZSTD_DCtx * dctx,void * dst,size_t maxDstSize,const void * seqStart,size_t seqSize,int nbSeq,const ZSTD_longOffset_e isLongOffset,const int frame)1250*06042735SVincent Franchomme ZSTD_decompressSequencesLong_bmi2(ZSTD_DCtx* dctx,
1251*06042735SVincent Franchomme void* dst, size_t maxDstSize,
1252*06042735SVincent Franchomme const void* seqStart, size_t seqSize, int nbSeq,
1253*06042735SVincent Franchomme const ZSTD_longOffset_e isLongOffset,
1254*06042735SVincent Franchomme const int frame)
1255*06042735SVincent Franchomme {
1256*06042735SVincent Franchomme return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
1257*06042735SVincent Franchomme }
1258*06042735SVincent Franchomme #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */
1259*06042735SVincent Franchomme
1260*06042735SVincent Franchomme #endif /* DYNAMIC_BMI2 */
1261*06042735SVincent Franchomme
1262*06042735SVincent Franchomme typedef size_t (*ZSTD_decompressSequences_t)(
1263*06042735SVincent Franchomme ZSTD_DCtx* dctx,
1264*06042735SVincent Franchomme void* dst, size_t maxDstSize,
1265*06042735SVincent Franchomme const void* seqStart, size_t seqSize, int nbSeq,
1266*06042735SVincent Franchomme const ZSTD_longOffset_e isLongOffset,
1267*06042735SVincent Franchomme const int frame);
1268*06042735SVincent Franchomme
1269*06042735SVincent Franchomme #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
1270*06042735SVincent Franchomme static size_t
ZSTD_decompressSequences(ZSTD_DCtx * dctx,void * dst,size_t maxDstSize,const void * seqStart,size_t seqSize,int nbSeq,const ZSTD_longOffset_e isLongOffset,const int frame)1271*06042735SVincent Franchomme ZSTD_decompressSequences(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize,
1272*06042735SVincent Franchomme const void* seqStart, size_t seqSize, int nbSeq,
1273*06042735SVincent Franchomme const ZSTD_longOffset_e isLongOffset,
1274*06042735SVincent Franchomme const int frame)
1275*06042735SVincent Franchomme {
1276*06042735SVincent Franchomme DEBUGLOG(5, "ZSTD_decompressSequences");
1277*06042735SVincent Franchomme #if DYNAMIC_BMI2
1278*06042735SVincent Franchomme if (dctx->bmi2) {
1279*06042735SVincent Franchomme return ZSTD_decompressSequences_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
1280*06042735SVincent Franchomme }
1281*06042735SVincent Franchomme #endif
1282*06042735SVincent Franchomme return ZSTD_decompressSequences_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
1283*06042735SVincent Franchomme }
1284*06042735SVincent Franchomme #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */
1285*06042735SVincent Franchomme
1286*06042735SVincent Franchomme
1287*06042735SVincent Franchomme #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
1288*06042735SVincent Franchomme /* ZSTD_decompressSequencesLong() :
1289*06042735SVincent Franchomme * decompression function triggered when a minimum share of offsets is considered "long",
1290*06042735SVincent Franchomme * aka out of cache.
1291*06042735SVincent Franchomme * note : "long" definition seems overloaded here, sometimes meaning "wider than bitstream register", and sometimes meaning "farther than memory cache distance".
1292*06042735SVincent Franchomme * This function will try to mitigate main memory latency through the use of prefetching */
1293*06042735SVincent Franchomme static size_t
ZSTD_decompressSequencesLong(ZSTD_DCtx * dctx,void * dst,size_t maxDstSize,const void * seqStart,size_t seqSize,int nbSeq,const ZSTD_longOffset_e isLongOffset,const int frame)1294*06042735SVincent Franchomme ZSTD_decompressSequencesLong(ZSTD_DCtx* dctx,
1295*06042735SVincent Franchomme void* dst, size_t maxDstSize,
1296*06042735SVincent Franchomme const void* seqStart, size_t seqSize, int nbSeq,
1297*06042735SVincent Franchomme const ZSTD_longOffset_e isLongOffset,
1298*06042735SVincent Franchomme const int frame)
1299*06042735SVincent Franchomme {
1300*06042735SVincent Franchomme DEBUGLOG(5, "ZSTD_decompressSequencesLong");
1301*06042735SVincent Franchomme #if DYNAMIC_BMI2
1302*06042735SVincent Franchomme if (dctx->bmi2) {
1303*06042735SVincent Franchomme return ZSTD_decompressSequencesLong_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
1304*06042735SVincent Franchomme }
1305*06042735SVincent Franchomme #endif
1306*06042735SVincent Franchomme return ZSTD_decompressSequencesLong_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);
1307*06042735SVincent Franchomme }
1308*06042735SVincent Franchomme #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */
1309*06042735SVincent Franchomme
1310*06042735SVincent Franchomme
1311*06042735SVincent Franchomme
1312*06042735SVincent Franchomme #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
1313*06042735SVincent Franchomme !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
1314*06042735SVincent Franchomme /* ZSTD_getLongOffsetsShare() :
1315*06042735SVincent Franchomme * condition : offTable must be valid
1316*06042735SVincent Franchomme * @return : "share" of long offsets (arbitrarily defined as > (1<<23))
1317*06042735SVincent Franchomme * compared to maximum possible of (1<<OffFSELog) */
1318*06042735SVincent Franchomme static unsigned
ZSTD_getLongOffsetsShare(const ZSTD_seqSymbol * offTable)1319*06042735SVincent Franchomme ZSTD_getLongOffsetsShare(const ZSTD_seqSymbol* offTable)
1320*06042735SVincent Franchomme {
1321*06042735SVincent Franchomme const void* ptr = offTable;
1322*06042735SVincent Franchomme U32 const tableLog = ((const ZSTD_seqSymbol_header*)ptr)[0].tableLog;
1323*06042735SVincent Franchomme const ZSTD_seqSymbol* table = offTable + 1;
1324*06042735SVincent Franchomme U32 const max = 1 << tableLog;
1325*06042735SVincent Franchomme U32 u, total = 0;
1326*06042735SVincent Franchomme DEBUGLOG(5, "ZSTD_getLongOffsetsShare: (tableLog=%u)", tableLog);
1327*06042735SVincent Franchomme
1328*06042735SVincent Franchomme assert(max <= (1 << OffFSELog)); /* max not too large */
1329*06042735SVincent Franchomme for (u=0; u<max; u++) {
1330*06042735SVincent Franchomme if (table[u].nbAdditionalBits > 22) total += 1;
1331*06042735SVincent Franchomme }
1332*06042735SVincent Franchomme
1333*06042735SVincent Franchomme assert(tableLog <= OffFSELog);
1334*06042735SVincent Franchomme total <<= (OffFSELog - tableLog); /* scale to OffFSELog */
1335*06042735SVincent Franchomme
1336*06042735SVincent Franchomme return total;
1337*06042735SVincent Franchomme }
1338*06042735SVincent Franchomme #endif
1339*06042735SVincent Franchomme
1340*06042735SVincent Franchomme size_t
ZSTD_decompressBlock_internal(ZSTD_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize,const int frame)1341*06042735SVincent Franchomme ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,
1342*06042735SVincent Franchomme void* dst, size_t dstCapacity,
1343*06042735SVincent Franchomme const void* src, size_t srcSize, const int frame)
1344*06042735SVincent Franchomme { /* blockType == blockCompressed */
1345*06042735SVincent Franchomme const BYTE* ip = (const BYTE*)src;
1346*06042735SVincent Franchomme /* isLongOffset must be true if there are long offsets.
1347*06042735SVincent Franchomme * Offsets are long if they are larger than 2^STREAM_ACCUMULATOR_MIN.
1348*06042735SVincent Franchomme * We don't expect that to be the case in 64-bit mode.
1349*06042735SVincent Franchomme * In block mode, window size is not known, so we have to be conservative.
1350*06042735SVincent Franchomme * (note: but it could be evaluated from current-lowLimit)
1351*06042735SVincent Franchomme */
1352*06042735SVincent Franchomme ZSTD_longOffset_e const isLongOffset = (ZSTD_longOffset_e)(MEM_32bits() && (!frame || (dctx->fParams.windowSize > (1ULL << STREAM_ACCUMULATOR_MIN))));
1353*06042735SVincent Franchomme DEBUGLOG(5, "ZSTD_decompressBlock_internal (size : %u)", (U32)srcSize);
1354*06042735SVincent Franchomme
1355*06042735SVincent Franchomme RETURN_ERROR_IF(srcSize >= ZSTD_BLOCKSIZE_MAX, srcSize_wrong, "");
1356*06042735SVincent Franchomme
1357*06042735SVincent Franchomme /* Decode literals section */
1358*06042735SVincent Franchomme { size_t const litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
1359*06042735SVincent Franchomme DEBUGLOG(5, "ZSTD_decodeLiteralsBlock : %u", (U32)litCSize);
1360*06042735SVincent Franchomme if (ZSTD_isError(litCSize)) return litCSize;
1361*06042735SVincent Franchomme ip += litCSize;
1362*06042735SVincent Franchomme srcSize -= litCSize;
1363*06042735SVincent Franchomme }
1364*06042735SVincent Franchomme
1365*06042735SVincent Franchomme /* Build Decoding Tables */
1366*06042735SVincent Franchomme {
1367*06042735SVincent Franchomme /* These macros control at build-time which decompressor implementation
1368*06042735SVincent Franchomme * we use. If neither is defined, we do some inspection and dispatch at
1369*06042735SVincent Franchomme * runtime.
1370*06042735SVincent Franchomme */
1371*06042735SVincent Franchomme #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
1372*06042735SVincent Franchomme !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
1373*06042735SVincent Franchomme int usePrefetchDecoder = dctx->ddictIsCold;
1374*06042735SVincent Franchomme #endif
1375*06042735SVincent Franchomme int nbSeq;
1376*06042735SVincent Franchomme size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, srcSize);
1377*06042735SVincent Franchomme if (ZSTD_isError(seqHSize)) return seqHSize;
1378*06042735SVincent Franchomme ip += seqHSize;
1379*06042735SVincent Franchomme srcSize -= seqHSize;
1380*06042735SVincent Franchomme
1381*06042735SVincent Franchomme RETURN_ERROR_IF(dst == NULL && nbSeq > 0, dstSize_tooSmall, "NULL not handled");
1382*06042735SVincent Franchomme
1383*06042735SVincent Franchomme #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
1384*06042735SVincent Franchomme !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
1385*06042735SVincent Franchomme if ( !usePrefetchDecoder
1386*06042735SVincent Franchomme && (!frame || (dctx->fParams.windowSize > (1<<24)))
1387*06042735SVincent Franchomme && (nbSeq>ADVANCED_SEQS) ) { /* could probably use a larger nbSeq limit */
1388*06042735SVincent Franchomme U32 const shareLongOffsets = ZSTD_getLongOffsetsShare(dctx->OFTptr);
1389*06042735SVincent Franchomme U32 const minShare = MEM_64bits() ? 7 : 20; /* heuristic values, correspond to 2.73% and 7.81% */
1390*06042735SVincent Franchomme usePrefetchDecoder = (shareLongOffsets >= minShare);
1391*06042735SVincent Franchomme }
1392*06042735SVincent Franchomme #endif
1393*06042735SVincent Franchomme
1394*06042735SVincent Franchomme dctx->ddictIsCold = 0;
1395*06042735SVincent Franchomme
1396*06042735SVincent Franchomme #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
1397*06042735SVincent Franchomme !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
1398*06042735SVincent Franchomme if (usePrefetchDecoder)
1399*06042735SVincent Franchomme #endif
1400*06042735SVincent Franchomme #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
1401*06042735SVincent Franchomme return ZSTD_decompressSequencesLong(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset, frame);
1402*06042735SVincent Franchomme #endif
1403*06042735SVincent Franchomme
1404*06042735SVincent Franchomme #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
1405*06042735SVincent Franchomme /* else */
1406*06042735SVincent Franchomme return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset, frame);
1407*06042735SVincent Franchomme #endif
1408*06042735SVincent Franchomme }
1409*06042735SVincent Franchomme }
1410*06042735SVincent Franchomme
1411*06042735SVincent Franchomme
ZSTD_checkContinuity(ZSTD_DCtx * dctx,const void * dst)1412*06042735SVincent Franchomme void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst)
1413*06042735SVincent Franchomme {
1414*06042735SVincent Franchomme if (dst != dctx->previousDstEnd) { /* not contiguous */
1415*06042735SVincent Franchomme dctx->dictEnd = dctx->previousDstEnd;
1416*06042735SVincent Franchomme dctx->virtualStart = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
1417*06042735SVincent Franchomme dctx->prefixStart = dst;
1418*06042735SVincent Franchomme dctx->previousDstEnd = dst;
1419*06042735SVincent Franchomme }
1420*06042735SVincent Franchomme }
1421*06042735SVincent Franchomme
1422*06042735SVincent Franchomme
ZSTD_decompressBlock(ZSTD_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)1423*06042735SVincent Franchomme size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx,
1424*06042735SVincent Franchomme void* dst, size_t dstCapacity,
1425*06042735SVincent Franchomme const void* src, size_t srcSize)
1426*06042735SVincent Franchomme {
1427*06042735SVincent Franchomme size_t dSize;
1428*06042735SVincent Franchomme ZSTD_checkContinuity(dctx, dst);
1429*06042735SVincent Franchomme dSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 0);
1430*06042735SVincent Franchomme dctx->previousDstEnd = (char*)dst + dSize;
1431*06042735SVincent Franchomme return dSize;
1432*06042735SVincent Franchomme }
1433