/src/zstd/lib/decompress/zstd_decompress_block.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * All rights reserved. |
4 | | * |
5 | | * This source code is licensed under both the BSD-style license (found in the |
6 | | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
7 | | * in the COPYING file in the root directory of this source tree). |
8 | | * You may select, at your option, one of the above-listed licenses. |
9 | | */ |
10 | | |
11 | | /* zstd_decompress_block : |
12 | | * this module takes care of decompressing _compressed_ block */ |
13 | | |
14 | | /*-******************************************************* |
15 | | * Dependencies |
16 | | *********************************************************/ |
17 | | #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */ |
18 | | #include "../common/compiler.h" /* prefetch */ |
19 | | #include "../common/mem.h" /* low level memory routines */ |
20 | | #include <stddef.h> |
21 | | #define FSE_STATIC_LINKING_ONLY |
22 | | #include "../common/fse.h" |
23 | | #include "../common/huf.h" |
24 | | #include "../common/zstd_internal.h" |
25 | | #include "zstd_decompress_internal.h" /* ZSTD_DCtx */ |
26 | | #include "zstd_decompress_block.h" |
27 | | #include "../common/bits.h" /* ZSTD_highbit32 */ |
28 | | |
29 | | /*_******************************************************* |
30 | | * Macros |
31 | | **********************************************************/ |
32 | | |
33 | | /* These two optional macros force the use one way or another of the two |
34 | | * ZSTD_decompressSequences implementations. You can't force in both directions |
35 | | * at the same time. |
36 | | */ |
37 | | #if defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \ |
38 | | defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG) |
39 | | #error "Cannot force the use of the short and the long ZSTD_decompressSequences variants!" |
40 | | #endif |
41 | | |
42 | | |
43 | | /*_******************************************************* |
44 | | * Memory operations |
45 | | **********************************************************/ |
46 | 971k | static void ZSTD_copy4(void* dst, const void* src) { ZSTD_memcpy(dst, src, 4); } |
47 | | |
48 | | |
49 | | /*-************************************************************* |
50 | | * Block decoding |
51 | | ***************************************************************/ |
52 | | |
53 | | static size_t ZSTD_blockSizeMax(ZSTD_DCtx const* dctx) |
54 | 10.1k | { |
55 | 10.1k | size_t const blockSizeMax = dctx->isFrameDecompression ? dctx->fParams.blockSizeMax : ZSTD_BLOCKSIZE_MAX; |
56 | 10.1k | assert(blockSizeMax <= ZSTD_BLOCKSIZE_MAX); |
57 | 10.1k | return blockSizeMax; |
58 | 10.1k | } |
59 | | |
60 | | /*! ZSTD_getcBlockSize() : |
61 | | * Provides the size of compressed block from block header `src` */ |
62 | | size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, |
63 | | blockProperties_t* bpPtr) |
64 | 41.4k | { |
65 | 41.4k | RETURN_ERROR_IF(srcSize < ZSTD_blockHeaderSize, srcSize_wrong, ""); |
66 | | |
67 | 41.4k | { U32 const cBlockHeader = MEM_readLE24(src); |
68 | 41.4k | U32 const cSize = cBlockHeader >> 3; |
69 | 41.4k | bpPtr->lastBlock = cBlockHeader & 1; |
70 | 41.4k | bpPtr->blockType = (blockType_e)((cBlockHeader >> 1) & 3); |
71 | 41.4k | bpPtr->origSize = cSize; /* only useful for RLE */ |
72 | 41.4k | if (bpPtr->blockType == bt_rle) return 1; |
73 | 32.1k | RETURN_ERROR_IF(bpPtr->blockType == bt_reserved, corruption_detected, ""); |
74 | 32.1k | return cSize; |
75 | 32.1k | } |
76 | 32.1k | } |
77 | | |
78 | | /* Allocate buffer for literals, either overlapping current dst, or split between dst and litExtraBuffer, or stored entirely within litExtraBuffer */ |
79 | | static void ZSTD_allocateLiteralsBuffer(ZSTD_DCtx* dctx, void* const dst, const size_t dstCapacity, const size_t litSize, |
80 | | const streaming_operation streaming, const size_t expectedWriteSize, const unsigned splitImmediately) |
81 | 2.47k | { |
82 | 2.47k | size_t const blockSizeMax = ZSTD_blockSizeMax(dctx); |
83 | 2.47k | assert(litSize <= blockSizeMax); |
84 | 2.47k | assert(dctx->isFrameDecompression || streaming == not_streaming); |
85 | 2.47k | assert(expectedWriteSize <= blockSizeMax); |
86 | 2.47k | if (streaming == not_streaming && dstCapacity > blockSizeMax + WILDCOPY_OVERLENGTH + litSize + WILDCOPY_OVERLENGTH) { |
87 | | /* If we aren't streaming, we can just put the literals after the output |
88 | | * of the current block. We don't need to worry about overwriting the |
89 | | * extDict of our window, because it doesn't exist. |
90 | | * So if we have space after the end of the block, just put it there. |
91 | | */ |
92 | 48 | dctx->litBuffer = (BYTE*)dst + blockSizeMax + WILDCOPY_OVERLENGTH; |
93 | 48 | dctx->litBufferEnd = dctx->litBuffer + litSize; |
94 | 48 | dctx->litBufferLocation = ZSTD_in_dst; |
95 | 2.42k | } else if (litSize <= ZSTD_LITBUFFEREXTRASIZE) { |
96 | | /* Literals fit entirely within the extra buffer, put them there to avoid |
97 | | * having to split the literals. |
98 | | */ |
99 | 1.69k | dctx->litBuffer = dctx->litExtraBuffer; |
100 | 1.69k | dctx->litBufferEnd = dctx->litBuffer + litSize; |
101 | 1.69k | dctx->litBufferLocation = ZSTD_not_in_dst; |
102 | 1.69k | } else { |
103 | 727 | assert(blockSizeMax > ZSTD_LITBUFFEREXTRASIZE); |
104 | | /* Literals must be split between the output block and the extra lit |
105 | | * buffer. We fill the extra lit buffer with the tail of the literals, |
106 | | * and put the rest of the literals at the end of the block, with |
107 | | * WILDCOPY_OVERLENGTH of buffer room to allow for overreads. |
108 | | * This MUST not write more than our maxBlockSize beyond dst, because in |
109 | | * streaming mode, that could overwrite part of our extDict window. |
110 | | */ |
111 | 727 | if (splitImmediately) { |
112 | | /* won't fit in litExtraBuffer, so it will be split between end of dst and extra buffer */ |
113 | 724 | dctx->litBuffer = (BYTE*)dst + expectedWriteSize - litSize + ZSTD_LITBUFFEREXTRASIZE - WILDCOPY_OVERLENGTH; |
114 | 724 | dctx->litBufferEnd = dctx->litBuffer + litSize - ZSTD_LITBUFFEREXTRASIZE; |
115 | 724 | } else { |
116 | | /* initially this will be stored entirely in dst during huffman decoding, it will partially be shifted to litExtraBuffer after */ |
117 | 3 | dctx->litBuffer = (BYTE*)dst + expectedWriteSize - litSize; |
118 | 3 | dctx->litBufferEnd = (BYTE*)dst + expectedWriteSize; |
119 | 3 | } |
120 | 727 | dctx->litBufferLocation = ZSTD_split; |
121 | 727 | assert(dctx->litBufferEnd <= (BYTE*)dst + expectedWriteSize); |
122 | 727 | } |
123 | 2.47k | } |
124 | | |
125 | | /*! ZSTD_decodeLiteralsBlock() : |
126 | | * Where it is possible to do so without being stomped by the output during decompression, the literals block will be stored |
127 | | * in the dstBuffer. If there is room to do so, it will be stored in full in the excess dst space after where the current |
128 | | * block will be output. Otherwise it will be stored at the end of the current dst blockspace, with a small portion being |
129 | | * stored in dctx->litExtraBuffer to help keep it "ahead" of the current output write. |
130 | | * |
131 | | * @return : nb of bytes read from src (< srcSize ) |
132 | | * note : symbol not declared but exposed for fullbench */ |
133 | | static size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx, |
134 | | const void* src, size_t srcSize, /* note : srcSize < BLOCKSIZE */ |
135 | | void* dst, size_t dstCapacity, const streaming_operation streaming) |
136 | 2.52k | { |
137 | 2.52k | DEBUGLOG(5, "ZSTD_decodeLiteralsBlock"); |
138 | 2.52k | RETURN_ERROR_IF(srcSize < MIN_CBLOCK_SIZE, corruption_detected, ""); |
139 | | |
140 | 2.52k | { const BYTE* const istart = (const BYTE*) src; |
141 | 2.52k | SymbolEncodingType_e const litEncType = (SymbolEncodingType_e)(istart[0] & 3); |
142 | 2.52k | size_t const blockSizeMax = ZSTD_blockSizeMax(dctx); |
143 | | |
144 | 2.52k | switch(litEncType) |
145 | 2.52k | { |
146 | 3 | case set_repeat: |
147 | 3 | DEBUGLOG(5, "set_repeat flag : re-using stats from previous compressed literals block"); |
148 | 3 | RETURN_ERROR_IF(dctx->litEntropy==0, dictionary_corrupted, ""); |
149 | 0 | ZSTD_FALLTHROUGH; |
150 | |
|
151 | 1.29k | case set_compressed: |
152 | 1.29k | RETURN_ERROR_IF(srcSize < 5, corruption_detected, "srcSize >= MIN_CBLOCK_SIZE == 2; here we need up to 5 for case 3"); |
153 | 1.29k | { size_t lhSize, litSize, litCSize; |
154 | 1.29k | U32 singleStream=0; |
155 | 1.29k | U32 const lhlCode = (istart[0] >> 2) & 3; |
156 | 1.29k | U32 const lhc = MEM_readLE32(istart); |
157 | 1.29k | size_t hufSuccess; |
158 | 1.29k | size_t expectedWriteSize = MIN(blockSizeMax, dstCapacity); |
159 | 1.29k | int const flags = 0 |
160 | 1.29k | | (ZSTD_DCtx_get_bmi2(dctx) ? HUF_flags_bmi2 : 0) |
161 | 1.29k | | (dctx->disableHufAsm ? HUF_flags_disableAsm : 0); |
162 | 1.29k | switch(lhlCode) |
163 | 1.29k | { |
164 | 1.24k | case 0: case 1: default: /* note : default is impossible, since lhlCode into [0..3] */ |
165 | | /* 2 - 2 - 10 - 10 */ |
166 | 1.24k | singleStream = !lhlCode; |
167 | 1.24k | lhSize = 3; |
168 | 1.24k | litSize = (lhc >> 4) & 0x3FF; |
169 | 1.24k | litCSize = (lhc >> 14) & 0x3FF; |
170 | 1.24k | break; |
171 | 39 | case 2: |
172 | | /* 2 - 2 - 14 - 14 */ |
173 | 39 | lhSize = 4; |
174 | 39 | litSize = (lhc >> 4) & 0x3FFF; |
175 | 39 | litCSize = lhc >> 18; |
176 | 39 | break; |
177 | 9 | case 3: |
178 | | /* 2 - 2 - 18 - 18 */ |
179 | 9 | lhSize = 5; |
180 | 9 | litSize = (lhc >> 4) & 0x3FFFF; |
181 | 9 | litCSize = (lhc >> 22) + ((size_t)istart[4] << 10); |
182 | 9 | break; |
183 | 1.29k | } |
184 | 1.29k | RETURN_ERROR_IF(litSize > 0 && dst == NULL, dstSize_tooSmall, "NULL not handled"); |
185 | 1.29k | RETURN_ERROR_IF(litSize > blockSizeMax, corruption_detected, ""); |
186 | 1.29k | if (!singleStream) |
187 | 1.18k | RETURN_ERROR_IF(litSize < MIN_LITERALS_FOR_4_STREAMS, literals_headerWrong, |
188 | 1.29k | "Not enough literals (%zu) for the 4-streams mode (min %u)", |
189 | 1.29k | litSize, MIN_LITERALS_FOR_4_STREAMS); |
190 | 1.28k | RETURN_ERROR_IF(litCSize + lhSize > srcSize, corruption_detected, ""); |
191 | 1.28k | RETURN_ERROR_IF(expectedWriteSize < litSize , dstSize_tooSmall, ""); |
192 | 1.27k | ZSTD_allocateLiteralsBuffer(dctx, dst, dstCapacity, litSize, streaming, expectedWriteSize, 0); |
193 | | |
194 | | /* prefetch huffman table if cold */ |
195 | 1.27k | if (dctx->ddictIsCold && (litSize > 768 /* heuristic */)) { |
196 | 0 | PREFETCH_AREA(dctx->HUFptr, sizeof(dctx->entropy.hufTable)); |
197 | 0 | } |
198 | | |
199 | 1.27k | if (litEncType==set_repeat) { |
200 | 0 | if (singleStream) { |
201 | 0 | hufSuccess = HUF_decompress1X_usingDTable( |
202 | 0 | dctx->litBuffer, litSize, istart+lhSize, litCSize, |
203 | 0 | dctx->HUFptr, flags); |
204 | 0 | } else { |
205 | 0 | assert(litSize >= MIN_LITERALS_FOR_4_STREAMS); |
206 | 0 | hufSuccess = HUF_decompress4X_usingDTable( |
207 | 0 | dctx->litBuffer, litSize, istart+lhSize, litCSize, |
208 | 0 | dctx->HUFptr, flags); |
209 | 0 | } |
210 | 1.27k | } else { |
211 | 1.27k | if (singleStream) { |
212 | | #if defined(HUF_FORCE_DECOMPRESS_X2) |
213 | | hufSuccess = HUF_decompress1X_DCtx_wksp( |
214 | | dctx->entropy.hufTable, dctx->litBuffer, litSize, |
215 | | istart+lhSize, litCSize, dctx->workspace, |
216 | | sizeof(dctx->workspace), flags); |
217 | | #else |
218 | 108 | hufSuccess = HUF_decompress1X1_DCtx_wksp( |
219 | 108 | dctx->entropy.hufTable, dctx->litBuffer, litSize, |
220 | 108 | istart+lhSize, litCSize, dctx->workspace, |
221 | 108 | sizeof(dctx->workspace), flags); |
222 | 108 | #endif |
223 | 1.16k | } else { |
224 | 1.16k | hufSuccess = HUF_decompress4X_hufOnly_wksp( |
225 | 1.16k | dctx->entropy.hufTable, dctx->litBuffer, litSize, |
226 | 1.16k | istart+lhSize, litCSize, dctx->workspace, |
227 | 1.16k | sizeof(dctx->workspace), flags); |
228 | 1.16k | } |
229 | 1.27k | } |
230 | 1.27k | if (dctx->litBufferLocation == ZSTD_split) |
231 | 3 | { |
232 | 3 | assert(litSize > ZSTD_LITBUFFEREXTRASIZE); |
233 | 3 | ZSTD_memcpy(dctx->litExtraBuffer, dctx->litBufferEnd - ZSTD_LITBUFFEREXTRASIZE, ZSTD_LITBUFFEREXTRASIZE); |
234 | 3 | ZSTD_memmove(dctx->litBuffer + ZSTD_LITBUFFEREXTRASIZE - WILDCOPY_OVERLENGTH, dctx->litBuffer, litSize - ZSTD_LITBUFFEREXTRASIZE); |
235 | 3 | dctx->litBuffer += ZSTD_LITBUFFEREXTRASIZE - WILDCOPY_OVERLENGTH; |
236 | 3 | dctx->litBufferEnd -= WILDCOPY_OVERLENGTH; |
237 | 3 | assert(dctx->litBufferEnd <= (BYTE*)dst + blockSizeMax); |
238 | 3 | } |
239 | | |
240 | 1.27k | RETURN_ERROR_IF(HUF_isError(hufSuccess), corruption_detected, ""); |
241 | | |
242 | 150 | dctx->litPtr = dctx->litBuffer; |
243 | 150 | dctx->litSize = litSize; |
244 | 150 | dctx->litEntropy = 1; |
245 | 150 | if (litEncType==set_compressed) dctx->HUFptr = dctx->entropy.hufTable; |
246 | 150 | return litCSize + lhSize; |
247 | 1.27k | } |
248 | | |
249 | 179 | case set_basic: |
250 | 179 | { size_t litSize, lhSize; |
251 | 179 | U32 const lhlCode = ((istart[0]) >> 2) & 3; |
252 | 179 | size_t expectedWriteSize = MIN(blockSizeMax, dstCapacity); |
253 | 179 | switch(lhlCode) |
254 | 179 | { |
255 | 162 | case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */ |
256 | 162 | lhSize = 1; |
257 | 162 | litSize = istart[0] >> 3; |
258 | 162 | break; |
259 | 9 | case 1: |
260 | 9 | lhSize = 2; |
261 | 9 | litSize = MEM_readLE16(istart) >> 4; |
262 | 9 | break; |
263 | 8 | case 3: |
264 | 8 | lhSize = 3; |
265 | 8 | RETURN_ERROR_IF(srcSize<3, corruption_detected, "srcSize >= MIN_CBLOCK_SIZE == 2; here we need lhSize = 3"); |
266 | 4 | litSize = MEM_readLE24(istart) >> 4; |
267 | 4 | break; |
268 | 179 | } |
269 | | |
270 | 175 | RETURN_ERROR_IF(litSize > 0 && dst == NULL, dstSize_tooSmall, "NULL not handled"); |
271 | 175 | RETURN_ERROR_IF(litSize > blockSizeMax, corruption_detected, ""); |
272 | 172 | RETURN_ERROR_IF(expectedWriteSize < litSize, dstSize_tooSmall, ""); |
273 | 169 | ZSTD_allocateLiteralsBuffer(dctx, dst, dstCapacity, litSize, streaming, expectedWriteSize, 1); |
274 | 169 | if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */ |
275 | 161 | RETURN_ERROR_IF(litSize+lhSize > srcSize, corruption_detected, ""); |
276 | 156 | if (dctx->litBufferLocation == ZSTD_split) |
277 | 0 | { |
278 | 0 | ZSTD_memcpy(dctx->litBuffer, istart + lhSize, litSize - ZSTD_LITBUFFEREXTRASIZE); |
279 | 0 | ZSTD_memcpy(dctx->litExtraBuffer, istart + lhSize + litSize - ZSTD_LITBUFFEREXTRASIZE, ZSTD_LITBUFFEREXTRASIZE); |
280 | 0 | } |
281 | 156 | else |
282 | 156 | { |
283 | 156 | ZSTD_memcpy(dctx->litBuffer, istart + lhSize, litSize); |
284 | 156 | } |
285 | 156 | dctx->litPtr = dctx->litBuffer; |
286 | 156 | dctx->litSize = litSize; |
287 | 156 | return lhSize+litSize; |
288 | 161 | } |
289 | | /* direct reference into compressed stream */ |
290 | 8 | dctx->litPtr = istart+lhSize; |
291 | 8 | dctx->litSize = litSize; |
292 | 8 | dctx->litBufferEnd = dctx->litPtr + litSize; |
293 | 8 | dctx->litBufferLocation = ZSTD_not_in_dst; |
294 | 8 | return lhSize+litSize; |
295 | 169 | } |
296 | | |
297 | 1.04k | case set_rle: |
298 | 1.04k | { U32 const lhlCode = ((istart[0]) >> 2) & 3; |
299 | 1.04k | size_t litSize, lhSize; |
300 | 1.04k | size_t expectedWriteSize = MIN(blockSizeMax, dstCapacity); |
301 | 1.04k | switch(lhlCode) |
302 | 1.04k | { |
303 | 30 | case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */ |
304 | 30 | lhSize = 1; |
305 | 30 | litSize = istart[0] >> 3; |
306 | 30 | break; |
307 | 34 | case 1: |
308 | 34 | lhSize = 2; |
309 | 34 | RETURN_ERROR_IF(srcSize<3, corruption_detected, "srcSize >= MIN_CBLOCK_SIZE == 2; here we need lhSize+1 = 3"); |
310 | 31 | litSize = MEM_readLE16(istart) >> 4; |
311 | 31 | break; |
312 | 978 | case 3: |
313 | 978 | lhSize = 3; |
314 | 978 | RETURN_ERROR_IF(srcSize<4, corruption_detected, "srcSize >= MIN_CBLOCK_SIZE == 2; here we need lhSize+1 = 4"); |
315 | 975 | litSize = MEM_readLE24(istart) >> 4; |
316 | 975 | break; |
317 | 1.04k | } |
318 | 1.03k | RETURN_ERROR_IF(litSize > 0 && dst == NULL, dstSize_tooSmall, "NULL not handled"); |
319 | 1.03k | RETURN_ERROR_IF(litSize > blockSizeMax, corruption_detected, ""); |
320 | 1.03k | RETURN_ERROR_IF(expectedWriteSize < litSize, dstSize_tooSmall, ""); |
321 | 1.02k | ZSTD_allocateLiteralsBuffer(dctx, dst, dstCapacity, litSize, streaming, expectedWriteSize, 1); |
322 | 1.02k | if (dctx->litBufferLocation == ZSTD_split) |
323 | 723 | { |
324 | 723 | ZSTD_memset(dctx->litBuffer, istart[lhSize], litSize - ZSTD_LITBUFFEREXTRASIZE); |
325 | 723 | ZSTD_memset(dctx->litExtraBuffer, istart[lhSize], ZSTD_LITBUFFEREXTRASIZE); |
326 | 723 | } |
327 | 304 | else |
328 | 304 | { |
329 | 304 | ZSTD_memset(dctx->litBuffer, istart[lhSize], litSize); |
330 | 304 | } |
331 | 1.02k | dctx->litPtr = dctx->litBuffer; |
332 | 1.02k | dctx->litSize = litSize; |
333 | 1.02k | return lhSize+1; |
334 | 1.03k | } |
335 | 0 | default: |
336 | 0 | RETURN_ERROR(corruption_detected, "impossible"); |
337 | 2.52k | } |
338 | 2.52k | } |
339 | 2.52k | } |
340 | | |
341 | | /* Hidden declaration for fullbench */ |
342 | | size_t ZSTD_decodeLiteralsBlock_wrapper(ZSTD_DCtx* dctx, |
343 | | const void* src, size_t srcSize, |
344 | | void* dst, size_t dstCapacity); |
345 | | size_t ZSTD_decodeLiteralsBlock_wrapper(ZSTD_DCtx* dctx, |
346 | | const void* src, size_t srcSize, |
347 | | void* dst, size_t dstCapacity) |
348 | 0 | { |
349 | 0 | dctx->isFrameDecompression = 0; |
350 | 0 | return ZSTD_decodeLiteralsBlock(dctx, src, srcSize, dst, dstCapacity, not_streaming); |
351 | 0 | } |
352 | | |
353 | | /* Default FSE distribution tables. |
354 | | * These are pre-calculated FSE decoding tables using default distributions as defined in specification : |
355 | | * https://github.com/facebook/zstd/blob/release/doc/zstd_compression_format.md#default-distributions |
356 | | * They were generated programmatically with following method : |
357 | | * - start from default distributions, present in /lib/common/zstd_internal.h |
358 | | * - generate tables normally, using ZSTD_buildFSETable() |
359 | | * - printout the content of tables |
360 | | * - prettify output, report below, test with fuzzer to ensure it's correct */ |
361 | | |
362 | | /* Default FSE distribution table for Literal Lengths */ |
363 | | static const ZSTD_seqSymbol LL_defaultDTable[(1<<LL_DEFAULTNORMLOG)+1] = { |
364 | | { 1, 1, 1, LL_DEFAULTNORMLOG}, /* header : fastMode, tableLog */ |
365 | | /* nextState, nbAddBits, nbBits, baseVal */ |
366 | | { 0, 0, 4, 0}, { 16, 0, 4, 0}, |
367 | | { 32, 0, 5, 1}, { 0, 0, 5, 3}, |
368 | | { 0, 0, 5, 4}, { 0, 0, 5, 6}, |
369 | | { 0, 0, 5, 7}, { 0, 0, 5, 9}, |
370 | | { 0, 0, 5, 10}, { 0, 0, 5, 12}, |
371 | | { 0, 0, 6, 14}, { 0, 1, 5, 16}, |
372 | | { 0, 1, 5, 20}, { 0, 1, 5, 22}, |
373 | | { 0, 2, 5, 28}, { 0, 3, 5, 32}, |
374 | | { 0, 4, 5, 48}, { 32, 6, 5, 64}, |
375 | | { 0, 7, 5, 128}, { 0, 8, 6, 256}, |
376 | | { 0, 10, 6, 1024}, { 0, 12, 6, 4096}, |
377 | | { 32, 0, 4, 0}, { 0, 0, 4, 1}, |
378 | | { 0, 0, 5, 2}, { 32, 0, 5, 4}, |
379 | | { 0, 0, 5, 5}, { 32, 0, 5, 7}, |
380 | | { 0, 0, 5, 8}, { 32, 0, 5, 10}, |
381 | | { 0, 0, 5, 11}, { 0, 0, 6, 13}, |
382 | | { 32, 1, 5, 16}, { 0, 1, 5, 18}, |
383 | | { 32, 1, 5, 22}, { 0, 2, 5, 24}, |
384 | | { 32, 3, 5, 32}, { 0, 3, 5, 40}, |
385 | | { 0, 6, 4, 64}, { 16, 6, 4, 64}, |
386 | | { 32, 7, 5, 128}, { 0, 9, 6, 512}, |
387 | | { 0, 11, 6, 2048}, { 48, 0, 4, 0}, |
388 | | { 16, 0, 4, 1}, { 32, 0, 5, 2}, |
389 | | { 32, 0, 5, 3}, { 32, 0, 5, 5}, |
390 | | { 32, 0, 5, 6}, { 32, 0, 5, 8}, |
391 | | { 32, 0, 5, 9}, { 32, 0, 5, 11}, |
392 | | { 32, 0, 5, 12}, { 0, 0, 6, 15}, |
393 | | { 32, 1, 5, 18}, { 32, 1, 5, 20}, |
394 | | { 32, 2, 5, 24}, { 32, 2, 5, 28}, |
395 | | { 32, 3, 5, 40}, { 32, 4, 5, 48}, |
396 | | { 0, 16, 6,65536}, { 0, 15, 6,32768}, |
397 | | { 0, 14, 6,16384}, { 0, 13, 6, 8192}, |
398 | | }; /* LL_defaultDTable */ |
399 | | |
400 | | /* Default FSE distribution table for Offset Codes */ |
401 | | static const ZSTD_seqSymbol OF_defaultDTable[(1<<OF_DEFAULTNORMLOG)+1] = { |
402 | | { 1, 1, 1, OF_DEFAULTNORMLOG}, /* header : fastMode, tableLog */ |
403 | | /* nextState, nbAddBits, nbBits, baseVal */ |
404 | | { 0, 0, 5, 0}, { 0, 6, 4, 61}, |
405 | | { 0, 9, 5, 509}, { 0, 15, 5,32765}, |
406 | | { 0, 21, 5,2097149}, { 0, 3, 5, 5}, |
407 | | { 0, 7, 4, 125}, { 0, 12, 5, 4093}, |
408 | | { 0, 18, 5,262141}, { 0, 23, 5,8388605}, |
409 | | { 0, 5, 5, 29}, { 0, 8, 4, 253}, |
410 | | { 0, 14, 5,16381}, { 0, 20, 5,1048573}, |
411 | | { 0, 2, 5, 1}, { 16, 7, 4, 125}, |
412 | | { 0, 11, 5, 2045}, { 0, 17, 5,131069}, |
413 | | { 0, 22, 5,4194301}, { 0, 4, 5, 13}, |
414 | | { 16, 8, 4, 253}, { 0, 13, 5, 8189}, |
415 | | { 0, 19, 5,524285}, { 0, 1, 5, 1}, |
416 | | { 16, 6, 4, 61}, { 0, 10, 5, 1021}, |
417 | | { 0, 16, 5,65533}, { 0, 28, 5,268435453}, |
418 | | { 0, 27, 5,134217725}, { 0, 26, 5,67108861}, |
419 | | { 0, 25, 5,33554429}, { 0, 24, 5,16777213}, |
420 | | }; /* OF_defaultDTable */ |
421 | | |
422 | | |
423 | | /* Default FSE distribution table for Match Lengths */ |
424 | | static const ZSTD_seqSymbol ML_defaultDTable[(1<<ML_DEFAULTNORMLOG)+1] = { |
425 | | { 1, 1, 1, ML_DEFAULTNORMLOG}, /* header : fastMode, tableLog */ |
426 | | /* nextState, nbAddBits, nbBits, baseVal */ |
427 | | { 0, 0, 6, 3}, { 0, 0, 4, 4}, |
428 | | { 32, 0, 5, 5}, { 0, 0, 5, 6}, |
429 | | { 0, 0, 5, 8}, { 0, 0, 5, 9}, |
430 | | { 0, 0, 5, 11}, { 0, 0, 6, 13}, |
431 | | { 0, 0, 6, 16}, { 0, 0, 6, 19}, |
432 | | { 0, 0, 6, 22}, { 0, 0, 6, 25}, |
433 | | { 0, 0, 6, 28}, { 0, 0, 6, 31}, |
434 | | { 0, 0, 6, 34}, { 0, 1, 6, 37}, |
435 | | { 0, 1, 6, 41}, { 0, 2, 6, 47}, |
436 | | { 0, 3, 6, 59}, { 0, 4, 6, 83}, |
437 | | { 0, 7, 6, 131}, { 0, 9, 6, 515}, |
438 | | { 16, 0, 4, 4}, { 0, 0, 4, 5}, |
439 | | { 32, 0, 5, 6}, { 0, 0, 5, 7}, |
440 | | { 32, 0, 5, 9}, { 0, 0, 5, 10}, |
441 | | { 0, 0, 6, 12}, { 0, 0, 6, 15}, |
442 | | { 0, 0, 6, 18}, { 0, 0, 6, 21}, |
443 | | { 0, 0, 6, 24}, { 0, 0, 6, 27}, |
444 | | { 0, 0, 6, 30}, { 0, 0, 6, 33}, |
445 | | { 0, 1, 6, 35}, { 0, 1, 6, 39}, |
446 | | { 0, 2, 6, 43}, { 0, 3, 6, 51}, |
447 | | { 0, 4, 6, 67}, { 0, 5, 6, 99}, |
448 | | { 0, 8, 6, 259}, { 32, 0, 4, 4}, |
449 | | { 48, 0, 4, 4}, { 16, 0, 4, 5}, |
450 | | { 32, 0, 5, 7}, { 32, 0, 5, 8}, |
451 | | { 32, 0, 5, 10}, { 32, 0, 5, 11}, |
452 | | { 0, 0, 6, 14}, { 0, 0, 6, 17}, |
453 | | { 0, 0, 6, 20}, { 0, 0, 6, 23}, |
454 | | { 0, 0, 6, 26}, { 0, 0, 6, 29}, |
455 | | { 0, 0, 6, 32}, { 0, 16, 6,65539}, |
456 | | { 0, 15, 6,32771}, { 0, 14, 6,16387}, |
457 | | { 0, 13, 6, 8195}, { 0, 12, 6, 4099}, |
458 | | { 0, 11, 6, 2051}, { 0, 10, 6, 1027}, |
459 | | }; /* ML_defaultDTable */ |
460 | | |
461 | | |
462 | | static void ZSTD_buildSeqTable_rle(ZSTD_seqSymbol* dt, U32 baseValue, U8 nbAddBits) |
463 | 317 | { |
464 | 317 | void* ptr = dt; |
465 | 317 | ZSTD_seqSymbol_header* const DTableH = (ZSTD_seqSymbol_header*)ptr; |
466 | 317 | ZSTD_seqSymbol* const cell = dt + 1; |
467 | | |
468 | 317 | DTableH->tableLog = 0; |
469 | 317 | DTableH->fastMode = 0; |
470 | | |
471 | 317 | cell->nbBits = 0; |
472 | 317 | cell->nextState = 0; |
473 | 317 | assert(nbAddBits < 255); |
474 | 317 | cell->nbAdditionalBits = nbAddBits; |
475 | 317 | cell->baseValue = baseValue; |
476 | 317 | } |
477 | | |
478 | | |
479 | | /* ZSTD_buildFSETable() : |
480 | | * generate FSE decoding table for one symbol (ll, ml or off) |
481 | | * cannot fail if input is valid => |
482 | | * all inputs are presumed validated at this stage */ |
483 | | FORCE_INLINE_TEMPLATE |
484 | | void ZSTD_buildFSETable_body(ZSTD_seqSymbol* dt, |
485 | | const short* normalizedCounter, unsigned maxSymbolValue, |
486 | | const U32* baseValue, const U8* nbAdditionalBits, |
487 | | unsigned tableLog, void* wksp, size_t wkspSize) |
488 | 781 | { |
489 | 781 | ZSTD_seqSymbol* const tableDecode = dt+1; |
490 | 781 | U32 const maxSV1 = maxSymbolValue + 1; |
491 | 781 | U32 const tableSize = 1 << tableLog; |
492 | | |
493 | 781 | U16* symbolNext = (U16*)wksp; |
494 | 781 | BYTE* spread = (BYTE*)(symbolNext + MaxSeq + 1); |
495 | 781 | U32 highThreshold = tableSize - 1; |
496 | | |
497 | | |
498 | | /* Sanity Checks */ |
499 | 781 | assert(maxSymbolValue <= MaxSeq); |
500 | 781 | assert(tableLog <= MaxFSELog); |
501 | 781 | assert(wkspSize >= ZSTD_BUILD_FSE_TABLE_WKSP_SIZE); |
502 | 781 | (void)wkspSize; |
503 | | /* Init, lay down lowprob symbols */ |
504 | 781 | { ZSTD_seqSymbol_header DTableH; |
505 | 781 | DTableH.tableLog = tableLog; |
506 | 781 | DTableH.fastMode = 1; |
507 | 781 | { S16 const largeLimit= (S16)(1 << (tableLog-1)); |
508 | 781 | U32 s; |
509 | 10.4k | for (s=0; s<maxSV1; s++) { |
510 | 9.70k | if (normalizedCounter[s]==-1) { |
511 | 3.93k | tableDecode[highThreshold--].baseValue = s; |
512 | 3.93k | symbolNext[s] = 1; |
513 | 5.77k | } else { |
514 | 5.77k | if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0; |
515 | 5.77k | assert(normalizedCounter[s]>=0); |
516 | 5.77k | symbolNext[s] = (U16)normalizedCounter[s]; |
517 | 5.77k | } } } |
518 | 781 | ZSTD_memcpy(dt, &DTableH, sizeof(DTableH)); |
519 | 781 | } |
520 | | |
521 | | /* Spread symbols */ |
522 | 781 | assert(tableSize <= 512); |
523 | | /* Specialized symbol spreading for the case when there are |
524 | | * no low probability (-1 count) symbols. When compressing |
525 | | * small blocks we avoid low probability symbols to hit this |
526 | | * case, since header decoding speed matters more. |
527 | | */ |
528 | 781 | if (highThreshold == tableSize - 1) { |
529 | 95 | size_t const tableMask = tableSize-1; |
530 | 95 | size_t const step = FSE_TABLESTEP(tableSize); |
531 | | /* First lay down the symbols in order. |
532 | | * We use a uint64_t to lay down 8 bytes at a time. This reduces branch |
533 | | * misses since small blocks generally have small table logs, so nearly |
534 | | * all symbols have counts <= 8. We ensure we have 8 bytes at the end of |
535 | | * our buffer to handle the over-write. |
536 | | */ |
537 | 95 | { |
538 | 95 | U64 const add = 0x0101010101010101ull; |
539 | 95 | size_t pos = 0; |
540 | 95 | U64 sv = 0; |
541 | 95 | U32 s; |
542 | 750 | for (s=0; s<maxSV1; ++s, sv += add) { |
543 | 655 | int i; |
544 | 655 | int const n = normalizedCounter[s]; |
545 | 655 | MEM_write64(spread + pos, sv); |
546 | 1.88k | for (i = 8; i < n; i += 8) { |
547 | 1.22k | MEM_write64(spread + pos + i, sv); |
548 | 1.22k | } |
549 | 655 | assert(n>=0); |
550 | 655 | pos += (size_t)n; |
551 | 655 | } |
552 | 95 | } |
553 | | /* Now we spread those positions across the table. |
554 | | * The benefit of doing it in two stages is that we avoid the |
555 | | * variable size inner loop, which caused lots of branch misses. |
556 | | * Now we can run through all the positions without any branch misses. |
557 | | * We unroll the loop twice, since that is what empirically worked best. |
558 | | */ |
559 | 95 | { |
560 | 95 | size_t position = 0; |
561 | 95 | size_t s; |
562 | 95 | size_t const unroll = 2; |
563 | 95 | assert(tableSize % unroll == 0); /* FSE_MIN_TABLELOG is 5 */ |
564 | 6.01k | for (s = 0; s < (size_t)tableSize; s += unroll) { |
565 | 5.92k | size_t u; |
566 | 17.7k | for (u = 0; u < unroll; ++u) { |
567 | 11.8k | size_t const uPosition = (position + (u * step)) & tableMask; |
568 | 11.8k | tableDecode[uPosition].baseValue = spread[s + u]; |
569 | 11.8k | } |
570 | 5.92k | position = (position + (unroll * step)) & tableMask; |
571 | 5.92k | } |
572 | 95 | assert(position == 0); |
573 | 95 | } |
574 | 686 | } else { |
575 | 686 | U32 const tableMask = tableSize-1; |
576 | 686 | U32 const step = FSE_TABLESTEP(tableSize); |
577 | 686 | U32 s, position = 0; |
578 | 9.73k | for (s=0; s<maxSV1; s++) { |
579 | 9.05k | int i; |
580 | 9.05k | int const n = normalizedCounter[s]; |
581 | 46.3k | for (i=0; i<n; i++) { |
582 | 37.2k | tableDecode[position].baseValue = s; |
583 | 37.2k | position = (position + step) & tableMask; |
584 | 41.0k | while (UNLIKELY(position > highThreshold)) position = (position + step) & tableMask; /* lowprob area */ |
585 | 37.2k | } } |
586 | 686 | assert(position == 0); /* position must reach all cells once, otherwise normalizedCounter is incorrect */ |
587 | 686 | } |
588 | | |
589 | | /* Build Decoding table */ |
590 | 781 | { |
591 | 781 | U32 u; |
592 | 53.8k | for (u=0; u<tableSize; u++) { |
593 | 53.0k | U32 const symbol = tableDecode[u].baseValue; |
594 | 53.0k | U32 const nextState = symbolNext[symbol]++; |
595 | 53.0k | tableDecode[u].nbBits = (BYTE) (tableLog - ZSTD_highbit32(nextState) ); |
596 | 53.0k | tableDecode[u].nextState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize); |
597 | 53.0k | assert(nbAdditionalBits[symbol] < 255); |
598 | 53.0k | tableDecode[u].nbAdditionalBits = nbAdditionalBits[symbol]; |
599 | 53.0k | tableDecode[u].baseValue = baseValue[symbol]; |
600 | 53.0k | } |
601 | 781 | } |
602 | 781 | } |
603 | | |
604 | | /* Avoids the FORCE_INLINE of the _body() function. */ |
605 | | static void ZSTD_buildFSETable_body_default(ZSTD_seqSymbol* dt, |
606 | | const short* normalizedCounter, unsigned maxSymbolValue, |
607 | | const U32* baseValue, const U8* nbAdditionalBits, |
608 | | unsigned tableLog, void* wksp, size_t wkspSize) |
609 | 0 | { |
610 | 0 | ZSTD_buildFSETable_body(dt, normalizedCounter, maxSymbolValue, |
611 | 0 | baseValue, nbAdditionalBits, tableLog, wksp, wkspSize); |
612 | 0 | } |
613 | | |
614 | | #if DYNAMIC_BMI2 |
615 | | BMI2_TARGET_ATTRIBUTE static void ZSTD_buildFSETable_body_bmi2(ZSTD_seqSymbol* dt, |
616 | | const short* normalizedCounter, unsigned maxSymbolValue, |
617 | | const U32* baseValue, const U8* nbAdditionalBits, |
618 | | unsigned tableLog, void* wksp, size_t wkspSize) |
619 | 781 | { |
620 | 781 | ZSTD_buildFSETable_body(dt, normalizedCounter, maxSymbolValue, |
621 | 781 | baseValue, nbAdditionalBits, tableLog, wksp, wkspSize); |
622 | 781 | } |
623 | | #endif |
624 | | |
625 | | void ZSTD_buildFSETable(ZSTD_seqSymbol* dt, |
626 | | const short* normalizedCounter, unsigned maxSymbolValue, |
627 | | const U32* baseValue, const U8* nbAdditionalBits, |
628 | | unsigned tableLog, void* wksp, size_t wkspSize, int bmi2) |
629 | 781 | { |
630 | 781 | #if DYNAMIC_BMI2 |
631 | 781 | if (bmi2) { |
632 | 781 | ZSTD_buildFSETable_body_bmi2(dt, normalizedCounter, maxSymbolValue, |
633 | 781 | baseValue, nbAdditionalBits, tableLog, wksp, wkspSize); |
634 | 781 | return; |
635 | 781 | } |
636 | 0 | #endif |
637 | 0 | (void)bmi2; |
638 | 0 | ZSTD_buildFSETable_body_default(dt, normalizedCounter, maxSymbolValue, |
639 | 0 | baseValue, nbAdditionalBits, tableLog, wksp, wkspSize); |
640 | 0 | } |
641 | | |
642 | | |
643 | | /*! ZSTD_buildSeqTable() : |
644 | | * @return : nb bytes read from src, |
645 | | * or an error code if it fails */ |
646 | | static size_t ZSTD_buildSeqTable(ZSTD_seqSymbol* DTableSpace, const ZSTD_seqSymbol** DTablePtr, |
647 | | SymbolEncodingType_e type, unsigned max, U32 maxLog, |
648 | | const void* src, size_t srcSize, |
649 | | const U32* baseValue, const U8* nbAdditionalBits, |
650 | | const ZSTD_seqSymbol* defaultTable, U32 flagRepeatTable, |
651 | | int ddictIsCold, int nbSeq, U32* wksp, size_t wkspSize, |
652 | | int bmi2) |
653 | 3.26k | { |
654 | 3.26k | switch(type) |
655 | 3.26k | { |
656 | 334 | case set_rle : |
657 | 334 | RETURN_ERROR_IF(!srcSize, srcSize_wrong, ""); |
658 | 330 | RETURN_ERROR_IF((*(const BYTE*)src) > max, corruption_detected, ""); |
659 | 317 | { U32 const symbol = *(const BYTE*)src; |
660 | 317 | U32 const baseline = baseValue[symbol]; |
661 | 317 | U8 const nbBits = nbAdditionalBits[symbol]; |
662 | 317 | ZSTD_buildSeqTable_rle(DTableSpace, baseline, nbBits); |
663 | 317 | } |
664 | 317 | *DTablePtr = DTableSpace; |
665 | 317 | return 1; |
666 | 2.07k | case set_basic : |
667 | 2.07k | *DTablePtr = defaultTable; |
668 | 2.07k | return 0; |
669 | 10 | case set_repeat: |
670 | 10 | RETURN_ERROR_IF(!flagRepeatTable, corruption_detected, ""); |
671 | | /* prefetch FSE table if used */ |
672 | 0 | if (ddictIsCold && (nbSeq > 24 /* heuristic */)) { |
673 | 0 | const void* const pStart = *DTablePtr; |
674 | 0 | size_t const pSize = sizeof(ZSTD_seqSymbol) * (SEQSYMBOL_TABLE_SIZE(maxLog)); |
675 | 0 | PREFETCH_AREA(pStart, pSize); |
676 | 0 | } |
677 | 0 | return 0; |
678 | 848 | case set_compressed : |
679 | 848 | { unsigned tableLog; |
680 | 848 | S16 norm[MaxSeq+1]; |
681 | 848 | size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize); |
682 | 848 | RETURN_ERROR_IF(FSE_isError(headerSize), corruption_detected, ""); |
683 | 793 | RETURN_ERROR_IF(tableLog > maxLog, corruption_detected, ""); |
684 | 781 | ZSTD_buildFSETable(DTableSpace, norm, max, baseValue, nbAdditionalBits, tableLog, wksp, wkspSize, bmi2); |
685 | 781 | *DTablePtr = DTableSpace; |
686 | 781 | return headerSize; |
687 | 793 | } |
688 | 0 | default : |
689 | 0 | assert(0); |
690 | 0 | RETURN_ERROR(GENERIC, "impossible"); |
691 | 3.26k | } |
692 | 3.26k | } |
693 | | |
694 | | size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr, |
695 | | const void* src, size_t srcSize) |
696 | 1.34k | { |
697 | 1.34k | const BYTE* const istart = (const BYTE*)src; |
698 | 1.34k | const BYTE* const iend = istart + srcSize; |
699 | 1.34k | const BYTE* ip = istart; |
700 | 1.34k | int nbSeq; |
701 | 1.34k | DEBUGLOG(5, "ZSTD_decodeSeqHeaders"); |
702 | | |
703 | | /* check */ |
704 | 1.34k | RETURN_ERROR_IF(srcSize < MIN_SEQUENCES_SIZE, srcSize_wrong, ""); |
705 | | |
706 | | /* SeqHead */ |
707 | 1.31k | nbSeq = *ip++; |
708 | 1.31k | if (nbSeq > 0x7F) { |
709 | 955 | if (nbSeq == 0xFF) { |
710 | 22 | RETURN_ERROR_IF(ip+2 > iend, srcSize_wrong, ""); |
711 | 19 | nbSeq = MEM_readLE16(ip) + LONGNBSEQ; |
712 | 19 | ip+=2; |
713 | 933 | } else { |
714 | 933 | RETURN_ERROR_IF(ip >= iend, srcSize_wrong, ""); |
715 | 927 | nbSeq = ((nbSeq-0x80)<<8) + *ip++; |
716 | 927 | } |
717 | 955 | } |
718 | 1.30k | *nbSeqPtr = nbSeq; |
719 | | |
720 | 1.30k | if (nbSeq == 0) { |
721 | | /* No sequence : section ends immediately */ |
722 | 135 | RETURN_ERROR_IF(ip != iend, corruption_detected, |
723 | 135 | "extraneous data present in the Sequences section"); |
724 | 96 | return (size_t)(ip - istart); |
725 | 135 | } |
726 | | |
727 | | /* FSE table descriptors */ |
728 | 1.17k | RETURN_ERROR_IF(ip+1 > iend, srcSize_wrong, ""); /* minimum possible size: 1 byte for symbol encoding types */ |
729 | 1.16k | RETURN_ERROR_IF(*ip & 3, corruption_detected, ""); /* The last field, Reserved, must be all-zeroes. */ |
730 | 1.12k | { SymbolEncodingType_e const LLtype = (SymbolEncodingType_e)(*ip >> 6); |
731 | 1.12k | SymbolEncodingType_e const OFtype = (SymbolEncodingType_e)((*ip >> 4) & 3); |
732 | 1.12k | SymbolEncodingType_e const MLtype = (SymbolEncodingType_e)((*ip >> 2) & 3); |
733 | 1.12k | ip++; |
734 | | |
735 | | /* Build DTables */ |
736 | 1.12k | assert(ip <= iend); |
737 | 1.12k | { size_t const llhSize = ZSTD_buildSeqTable(dctx->entropy.LLTable, &dctx->LLTptr, |
738 | 1.12k | LLtype, MaxLL, LLFSELog, |
739 | 1.12k | ip, (size_t)(iend-ip), |
740 | 1.12k | LL_base, LL_bits, |
741 | 1.12k | LL_defaultDTable, dctx->fseEntropy, |
742 | 1.12k | dctx->ddictIsCold, nbSeq, |
743 | 1.12k | dctx->workspace, sizeof(dctx->workspace), |
744 | 1.12k | ZSTD_DCtx_get_bmi2(dctx)); |
745 | 1.12k | RETURN_ERROR_IF(ZSTD_isError(llhSize), corruption_detected, "ZSTD_buildSeqTable failed"); |
746 | 1.10k | ip += llhSize; |
747 | 1.10k | } |
748 | | |
749 | 1.10k | assert(ip <= iend); |
750 | 1.10k | { size_t const ofhSize = ZSTD_buildSeqTable(dctx->entropy.OFTable, &dctx->OFTptr, |
751 | 1.10k | OFtype, MaxOff, OffFSELog, |
752 | 1.10k | ip, (size_t)(iend-ip), |
753 | 1.10k | OF_base, OF_bits, |
754 | 1.10k | OF_defaultDTable, dctx->fseEntropy, |
755 | 1.10k | dctx->ddictIsCold, nbSeq, |
756 | 1.10k | dctx->workspace, sizeof(dctx->workspace), |
757 | 1.10k | ZSTD_DCtx_get_bmi2(dctx)); |
758 | 1.10k | RETURN_ERROR_IF(ZSTD_isError(ofhSize), corruption_detected, "ZSTD_buildSeqTable failed"); |
759 | 1.04k | ip += ofhSize; |
760 | 1.04k | } |
761 | | |
762 | 1.04k | assert(ip <= iend); |
763 | 1.04k | { size_t const mlhSize = ZSTD_buildSeqTable(dctx->entropy.MLTable, &dctx->MLTptr, |
764 | 1.04k | MLtype, MaxML, MLFSELog, |
765 | 1.04k | ip, (size_t)(iend-ip), |
766 | 1.04k | ML_base, ML_bits, |
767 | 1.04k | ML_defaultDTable, dctx->fseEntropy, |
768 | 1.04k | dctx->ddictIsCold, nbSeq, |
769 | 1.04k | dctx->workspace, sizeof(dctx->workspace), |
770 | 1.04k | ZSTD_DCtx_get_bmi2(dctx)); |
771 | 1.04k | RETURN_ERROR_IF(ZSTD_isError(mlhSize), corruption_detected, "ZSTD_buildSeqTable failed"); |
772 | 1.02k | ip += mlhSize; |
773 | 1.02k | } |
774 | 1.02k | } |
775 | | |
776 | 0 | return (size_t)(ip-istart); |
777 | 1.04k | } |
778 | | |
779 | | |
780 | | typedef struct { |
781 | | size_t litLength; |
782 | | size_t matchLength; |
783 | | size_t offset; |
784 | | } seq_t; |
785 | | |
786 | | typedef struct { |
787 | | size_t state; |
788 | | const ZSTD_seqSymbol* table; |
789 | | } ZSTD_fseState; |
790 | | |
791 | | typedef struct { |
792 | | BIT_DStream_t DStream; |
793 | | ZSTD_fseState stateLL; |
794 | | ZSTD_fseState stateOffb; |
795 | | ZSTD_fseState stateML; |
796 | | size_t prevOffset[ZSTD_REP_NUM]; |
797 | | } seqState_t; |
798 | | |
799 | | /*! ZSTD_overlapCopy8() : |
800 | | * Copies 8 bytes from ip to op and updates op and ip where ip <= op. |
801 | | * If the offset is < 8 then the offset is spread to at least 8 bytes. |
802 | | * |
803 | | * Precondition: *ip <= *op |
804 | | * Postcondition: *op - *op >= 8 |
805 | | */ |
806 | | HINT_INLINE void ZSTD_overlapCopy8(BYTE** op, BYTE const** ip, size_t offset) |
807 | 1.37M | { |
808 | 1.37M | assert(*ip <= *op); |
809 | 1.37M | if (offset < 8) { |
810 | | /* close range match, overlap */ |
811 | 971k | static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */ |
812 | 971k | static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */ |
813 | 971k | int const sub2 = dec64table[offset]; |
814 | 971k | (*op)[0] = (*ip)[0]; |
815 | 971k | (*op)[1] = (*ip)[1]; |
816 | 971k | (*op)[2] = (*ip)[2]; |
817 | 971k | (*op)[3] = (*ip)[3]; |
818 | 971k | *ip += dec32table[offset]; |
819 | 971k | ZSTD_copy4(*op+4, *ip); |
820 | 971k | *ip -= sub2; |
821 | 971k | } else { |
822 | 406k | ZSTD_copy8(*op, *ip); |
823 | 406k | } |
824 | 1.37M | *ip += 8; |
825 | 1.37M | *op += 8; |
826 | 1.37M | assert(*op - *ip >= 8); |
827 | 1.37M | } |
828 | | |
829 | | /*! ZSTD_safecopy() : |
830 | | * Specialized version of memcpy() that is allowed to READ up to WILDCOPY_OVERLENGTH past the input buffer |
831 | | * and write up to 16 bytes past oend_w (op >= oend_w is allowed). |
832 | | * This function is only called in the uncommon case where the sequence is near the end of the block. It |
833 | | * should be fast for a single long sequence, but can be slow for several short sequences. |
834 | | * |
835 | | * @param ovtype controls the overlap detection |
836 | | * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart. |
837 | | * - ZSTD_overlap_src_before_dst: The src and dst may overlap and may be any distance apart. |
838 | | * The src buffer must be before the dst buffer. |
839 | | */ |
840 | | static void |
841 | | ZSTD_safecopy(BYTE* op, const BYTE* const oend_w, BYTE const* ip, size_t length, ZSTD_overlap_e ovtype) |
842 | 516k | { |
843 | 516k | ptrdiff_t const diff = op - ip; |
844 | 516k | BYTE* const oend = op + length; |
845 | | |
846 | 516k | assert((ovtype == ZSTD_no_overlap && (diff <= -8 || diff >= 8 || op >= oend_w)) || |
847 | 516k | (ovtype == ZSTD_overlap_src_before_dst && diff >= 0)); |
848 | | |
849 | 516k | if (length < 8) { |
850 | | /* Handle short lengths. */ |
851 | 1.43M | while (op < oend) *op++ = *ip++; |
852 | 323k | return; |
853 | 323k | } |
854 | 193k | if (ovtype == ZSTD_overlap_src_before_dst) { |
855 | | /* Copy 8 bytes and ensure the offset >= 8 when there can be overlap. */ |
856 | 193k | assert(length >= 8); |
857 | 193k | assert(diff > 0); |
858 | 193k | ZSTD_overlapCopy8(&op, &ip, (size_t)diff); |
859 | 193k | length -= 8; |
860 | 193k | assert(op - ip >= 8); |
861 | 193k | assert(op <= oend); |
862 | 193k | } |
863 | | |
864 | 193k | if (oend <= oend_w) { |
865 | | /* No risk of overwrite. */ |
866 | 31 | ZSTD_wildcopy(op, ip, length, ovtype); |
867 | 31 | return; |
868 | 31 | } |
869 | 193k | if (op <= oend_w) { |
870 | | /* Wildcopy until we get close to the end. */ |
871 | 385 | assert(oend > oend_w); |
872 | 385 | ZSTD_wildcopy(op, ip, (size_t)(oend_w - op), ovtype); |
873 | 385 | ip += oend_w - op; |
874 | 385 | op += oend_w - op; |
875 | 385 | } |
876 | | /* Handle the leftovers. */ |
877 | 47.6M | while (op < oend) *op++ = *ip++; |
878 | 193k | } |
879 | | |
880 | | /* ZSTD_safecopyDstBeforeSrc(): |
881 | | * This version allows overlap with dst before src, or handles the non-overlap case with dst after src |
882 | | * Kept separate from more common ZSTD_safecopy case to avoid performance impact to the safecopy common case */ |
883 | | static void ZSTD_safecopyDstBeforeSrc(BYTE* op, const BYTE* ip, size_t length) |
884 | 516k | { |
885 | 516k | ptrdiff_t const diff = op - ip; |
886 | 516k | BYTE* const oend = op + length; |
887 | | |
888 | 516k | if (length < 8 || diff > -8) { |
889 | | /* Handle short lengths, close overlaps, and dst not before src. */ |
890 | 6.64M | while (op < oend) *op++ = *ip++; |
891 | 515k | return; |
892 | 515k | } |
893 | | |
894 | 553 | if (op <= oend - WILDCOPY_OVERLENGTH && diff < -WILDCOPY_VECLEN) { |
895 | 366 | ZSTD_wildcopy(op, ip, (size_t)(oend - WILDCOPY_OVERLENGTH - op), ZSTD_no_overlap); |
896 | 366 | ip += oend - WILDCOPY_OVERLENGTH - op; |
897 | 366 | op += oend - WILDCOPY_OVERLENGTH - op; |
898 | 366 | } |
899 | | |
900 | | /* Handle the leftovers. */ |
901 | 122k | while (op < oend) *op++ = *ip++; |
902 | 553 | } |
903 | | |
904 | | /* ZSTD_execSequenceEnd(): |
905 | | * This version handles cases that are near the end of the output buffer. It requires |
906 | | * more careful checks to make sure there is no overflow. By separating out these hard |
907 | | * and unlikely cases, we can speed up the common cases. |
908 | | * |
909 | | * NOTE: This function needs to be fast for a single long sequence, but doesn't need |
910 | | * to be optimized for many small sequences, since those fall into ZSTD_execSequence(). |
911 | | */ |
912 | | FORCE_NOINLINE |
913 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
914 | | size_t ZSTD_execSequenceEnd(BYTE* op, |
915 | | BYTE* const oend, seq_t sequence, |
916 | | const BYTE** litPtr, const BYTE* const litLimit, |
917 | | const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd) |
918 | 746 | { |
919 | 746 | BYTE* const oLitEnd = op + sequence.litLength; |
920 | 746 | size_t const sequenceLength = sequence.litLength + sequence.matchLength; |
921 | 746 | const BYTE* const iLitEnd = *litPtr + sequence.litLength; |
922 | 746 | const BYTE* match = oLitEnd - sequence.offset; |
923 | 746 | BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH; |
924 | | |
925 | | /* bounds checks : careful of address space overflow in 32-bit mode */ |
926 | 746 | RETURN_ERROR_IF(sequenceLength > (size_t)(oend - op), dstSize_tooSmall, "last match must fit within dstBuffer"); |
927 | 562 | RETURN_ERROR_IF(sequence.litLength > (size_t)(litLimit - *litPtr), corruption_detected, "try to read beyond literal buffer"); |
928 | 262 | assert(op < op + sequenceLength); |
929 | 262 | assert(oLitEnd < op + sequenceLength); |
930 | | |
931 | | /* copy literals */ |
932 | 262 | ZSTD_safecopy(op, oend_w, *litPtr, sequence.litLength, ZSTD_no_overlap); |
933 | 262 | op = oLitEnd; |
934 | 262 | *litPtr = iLitEnd; |
935 | | |
936 | | /* copy Match */ |
937 | 262 | if (sequence.offset > (size_t)(oLitEnd - prefixStart)) { |
938 | | /* offset beyond prefix */ |
939 | 1 | RETURN_ERROR_IF(sequence.offset > (size_t)(oLitEnd - virtualStart), corruption_detected, ""); |
940 | 0 | match = dictEnd - (prefixStart - match); |
941 | 0 | if (match + sequence.matchLength <= dictEnd) { |
942 | 0 | ZSTD_memmove(oLitEnd, match, sequence.matchLength); |
943 | 0 | return sequenceLength; |
944 | 0 | } |
945 | | /* span extDict & currentPrefixSegment */ |
946 | 0 | { size_t const length1 = (size_t)(dictEnd - match); |
947 | 0 | ZSTD_memmove(oLitEnd, match, length1); |
948 | 0 | op = oLitEnd + length1; |
949 | 0 | sequence.matchLength -= length1; |
950 | 0 | match = prefixStart; |
951 | 0 | } |
952 | 0 | } |
953 | 261 | ZSTD_safecopy(op, oend_w, match, sequence.matchLength, ZSTD_overlap_src_before_dst); |
954 | 261 | return sequenceLength; |
955 | 262 | } |
956 | | |
957 | | /* ZSTD_execSequenceEndSplitLitBuffer(): |
958 | | * This version is intended to be used during instances where the litBuffer is still split. It is kept separate to avoid performance impact for the good case. |
959 | | */ |
960 | | FORCE_NOINLINE |
961 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
962 | | size_t ZSTD_execSequenceEndSplitLitBuffer(BYTE* op, |
963 | | BYTE* const oend, const BYTE* const oend_w, seq_t sequence, |
964 | | const BYTE** litPtr, const BYTE* const litLimit, |
965 | | const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd) |
966 | 516k | { |
967 | 516k | BYTE* const oLitEnd = op + sequence.litLength; |
968 | 516k | size_t const sequenceLength = sequence.litLength + sequence.matchLength; |
969 | 516k | const BYTE* const iLitEnd = *litPtr + sequence.litLength; |
970 | 516k | const BYTE* match = oLitEnd - sequence.offset; |
971 | | |
972 | | |
973 | | /* bounds checks : careful of address space overflow in 32-bit mode */ |
974 | 516k | RETURN_ERROR_IF(sequenceLength > (size_t)(oend - op), dstSize_tooSmall, "last match must fit within dstBuffer"); |
975 | 516k | RETURN_ERROR_IF(sequence.litLength > (size_t)(litLimit - *litPtr), corruption_detected, "try to read beyond literal buffer"); |
976 | 516k | assert(op < op + sequenceLength); |
977 | 516k | assert(oLitEnd < op + sequenceLength); |
978 | | |
979 | | /* copy literals */ |
980 | 516k | RETURN_ERROR_IF(op > *litPtr && op < *litPtr + sequence.litLength, dstSize_tooSmall, "output should not catch up to and overwrite literal buffer"); |
981 | 516k | ZSTD_safecopyDstBeforeSrc(op, *litPtr, sequence.litLength); |
982 | 516k | op = oLitEnd; |
983 | 516k | *litPtr = iLitEnd; |
984 | | |
985 | | /* copy Match */ |
986 | 516k | if (sequence.offset > (size_t)(oLitEnd - prefixStart)) { |
987 | | /* offset beyond prefix */ |
988 | 37 | RETURN_ERROR_IF(sequence.offset > (size_t)(oLitEnd - virtualStart), corruption_detected, ""); |
989 | 0 | match = dictEnd - (prefixStart - match); |
990 | 0 | if (match + sequence.matchLength <= dictEnd) { |
991 | 0 | ZSTD_memmove(oLitEnd, match, sequence.matchLength); |
992 | 0 | return sequenceLength; |
993 | 0 | } |
994 | | /* span extDict & currentPrefixSegment */ |
995 | 0 | { size_t const length1 = (size_t)(dictEnd - match); |
996 | 0 | ZSTD_memmove(oLitEnd, match, length1); |
997 | 0 | op = oLitEnd + length1; |
998 | 0 | sequence.matchLength -= length1; |
999 | 0 | match = prefixStart; |
1000 | 0 | } |
1001 | 0 | } |
1002 | 516k | ZSTD_safecopy(op, oend_w, match, sequence.matchLength, ZSTD_overlap_src_before_dst); |
1003 | 516k | return sequenceLength; |
1004 | 516k | } |
1005 | | |
1006 | | HINT_INLINE |
1007 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
1008 | | size_t ZSTD_execSequence(BYTE* op, |
1009 | | BYTE* const oend, seq_t sequence, |
1010 | | const BYTE** litPtr, const BYTE* const litLimit, |
1011 | | const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd) |
1012 | 1.22M | { |
1013 | 1.22M | BYTE* const oLitEnd = op + sequence.litLength; |
1014 | 1.22M | size_t const sequenceLength = sequence.litLength + sequence.matchLength; |
1015 | 1.22M | BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ |
1016 | 1.22M | BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH; /* risk : address space underflow on oend=NULL */ |
1017 | 1.22M | const BYTE* const iLitEnd = *litPtr + sequence.litLength; |
1018 | 1.22M | const BYTE* match = oLitEnd - sequence.offset; |
1019 | | |
1020 | 1.22M | assert(op != NULL /* Precondition */); |
1021 | 1.22M | assert(oend_w < oend /* No underflow */); |
1022 | | |
1023 | | #if defined(__aarch64__) |
1024 | | /* prefetch sequence starting from match that will be used for copy later */ |
1025 | | PREFETCH_L1(match); |
1026 | | #endif |
1027 | | /* Handle edge cases in a slow path: |
1028 | | * - Read beyond end of literals |
1029 | | * - Match end is within WILDCOPY_OVERLIMIT of oend |
1030 | | * - 32-bit mode and the match length overflows |
1031 | | */ |
1032 | 1.22M | if (UNLIKELY( |
1033 | 1.22M | iLitEnd > litLimit || |
1034 | 1.22M | oMatchEnd > oend_w || |
1035 | 1.22M | (MEM_32bits() && (size_t)(oend - op) < sequenceLength + WILDCOPY_OVERLENGTH))) |
1036 | 746 | return ZSTD_execSequenceEnd(op, oend, sequence, litPtr, litLimit, prefixStart, virtualStart, dictEnd); |
1037 | | |
1038 | | /* Assumptions (everything else goes into ZSTD_execSequenceEnd()) */ |
1039 | 1.22M | assert(op <= oLitEnd /* No overflow */); |
1040 | 1.22M | assert(oLitEnd < oMatchEnd /* Non-zero match & no overflow */); |
1041 | 1.22M | assert(oMatchEnd <= oend /* No underflow */); |
1042 | 1.22M | assert(iLitEnd <= litLimit /* Literal length is in bounds */); |
1043 | 1.22M | assert(oLitEnd <= oend_w /* Can wildcopy literals */); |
1044 | 1.22M | assert(oMatchEnd <= oend_w /* Can wildcopy matches */); |
1045 | | |
1046 | | /* Copy Literals: |
1047 | | * Split out litLength <= 16 since it is nearly always true. +1.6% on gcc-9. |
1048 | | * We likely don't need the full 32-byte wildcopy. |
1049 | | */ |
1050 | 1.22M | assert(WILDCOPY_OVERLENGTH >= 16); |
1051 | 1.22M | ZSTD_copy16(op, (*litPtr)); |
1052 | 1.22M | if (UNLIKELY(sequence.litLength > 16)) { |
1053 | 136k | ZSTD_wildcopy(op + 16, (*litPtr) + 16, sequence.litLength - 16, ZSTD_no_overlap); |
1054 | 136k | } |
1055 | 1.22M | op = oLitEnd; |
1056 | 1.22M | *litPtr = iLitEnd; /* update for next sequence */ |
1057 | | |
1058 | | /* Copy Match */ |
1059 | 1.22M | if (sequence.offset > (size_t)(oLitEnd - prefixStart)) { |
1060 | | /* offset beyond prefix -> go into extDict */ |
1061 | 186 | RETURN_ERROR_IF(UNLIKELY(sequence.offset > (size_t)(oLitEnd - virtualStart)), corruption_detected, ""); |
1062 | 0 | match = dictEnd + (match - prefixStart); |
1063 | 0 | if (match + sequence.matchLength <= dictEnd) { |
1064 | 0 | ZSTD_memmove(oLitEnd, match, sequence.matchLength); |
1065 | 0 | return sequenceLength; |
1066 | 0 | } |
1067 | | /* span extDict & currentPrefixSegment */ |
1068 | 0 | { size_t const length1 = (size_t)(dictEnd - match); |
1069 | 0 | ZSTD_memmove(oLitEnd, match, length1); |
1070 | 0 | op = oLitEnd + length1; |
1071 | 0 | sequence.matchLength -= length1; |
1072 | 0 | match = prefixStart; |
1073 | 0 | } |
1074 | 0 | } |
1075 | | /* Match within prefix of 1 or more bytes */ |
1076 | 1.22M | assert(op <= oMatchEnd); |
1077 | 1.22M | assert(oMatchEnd <= oend_w); |
1078 | 1.22M | assert(match >= prefixStart); |
1079 | 1.22M | assert(sequence.matchLength >= 1); |
1080 | | |
1081 | | /* Nearly all offsets are >= WILDCOPY_VECLEN bytes, which means we can use wildcopy |
1082 | | * without overlap checking. |
1083 | | */ |
1084 | 1.22M | if (LIKELY(sequence.offset >= WILDCOPY_VECLEN)) { |
1085 | | /* We bet on a full wildcopy for matches, since we expect matches to be |
1086 | | * longer than literals (in general). In silesia, ~10% of matches are longer |
1087 | | * than 16 bytes. |
1088 | | */ |
1089 | 366k | ZSTD_wildcopy(op, match, sequence.matchLength, ZSTD_no_overlap); |
1090 | 366k | return sequenceLength; |
1091 | 366k | } |
1092 | 856k | assert(sequence.offset < WILDCOPY_VECLEN); |
1093 | | |
1094 | | /* Copy 8 bytes and spread the offset to be >= 8. */ |
1095 | 856k | ZSTD_overlapCopy8(&op, &match, sequence.offset); |
1096 | | |
1097 | | /* If the match length is > 8 bytes, then continue with the wildcopy. */ |
1098 | 856k | if (sequence.matchLength > 8) { |
1099 | 319k | assert(op < oMatchEnd); |
1100 | 319k | ZSTD_wildcopy(op, match, sequence.matchLength - 8, ZSTD_overlap_src_before_dst); |
1101 | 319k | } |
1102 | 856k | return sequenceLength; |
1103 | 1.22M | } |
1104 | | |
1105 | | HINT_INLINE |
1106 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
1107 | | size_t ZSTD_execSequenceSplitLitBuffer(BYTE* op, |
1108 | | BYTE* const oend, const BYTE* const oend_w, seq_t sequence, |
1109 | | const BYTE** litPtr, const BYTE* const litLimit, |
1110 | | const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd) |
1111 | 890k | { |
1112 | 890k | BYTE* const oLitEnd = op + sequence.litLength; |
1113 | 890k | size_t const sequenceLength = sequence.litLength + sequence.matchLength; |
1114 | 890k | BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ |
1115 | 890k | const BYTE* const iLitEnd = *litPtr + sequence.litLength; |
1116 | 890k | const BYTE* match = oLitEnd - sequence.offset; |
1117 | | |
1118 | 890k | assert(op != NULL /* Precondition */); |
1119 | 890k | assert(oend_w < oend /* No underflow */); |
1120 | | /* Handle edge cases in a slow path: |
1121 | | * - Read beyond end of literals |
1122 | | * - Match end is within WILDCOPY_OVERLIMIT of oend |
1123 | | * - 32-bit mode and the match length overflows |
1124 | | */ |
1125 | 890k | if (UNLIKELY( |
1126 | 890k | iLitEnd > litLimit || |
1127 | 890k | oMatchEnd > oend_w || |
1128 | 890k | (MEM_32bits() && (size_t)(oend - op) < sequenceLength + WILDCOPY_OVERLENGTH))) |
1129 | 516k | return ZSTD_execSequenceEndSplitLitBuffer(op, oend, oend_w, sequence, litPtr, litLimit, prefixStart, virtualStart, dictEnd); |
1130 | | |
1131 | | /* Assumptions (everything else goes into ZSTD_execSequenceEnd()) */ |
1132 | 374k | assert(op <= oLitEnd /* No overflow */); |
1133 | 374k | assert(oLitEnd < oMatchEnd /* Non-zero match & no overflow */); |
1134 | 374k | assert(oMatchEnd <= oend /* No underflow */); |
1135 | 374k | assert(iLitEnd <= litLimit /* Literal length is in bounds */); |
1136 | 374k | assert(oLitEnd <= oend_w /* Can wildcopy literals */); |
1137 | 374k | assert(oMatchEnd <= oend_w /* Can wildcopy matches */); |
1138 | | |
1139 | | /* Copy Literals: |
1140 | | * Split out litLength <= 16 since it is nearly always true. +1.6% on gcc-9. |
1141 | | * We likely don't need the full 32-byte wildcopy. |
1142 | | */ |
1143 | 374k | assert(WILDCOPY_OVERLENGTH >= 16); |
1144 | 374k | ZSTD_copy16(op, (*litPtr)); |
1145 | 374k | if (UNLIKELY(sequence.litLength > 16)) { |
1146 | 27.6k | ZSTD_wildcopy(op+16, (*litPtr)+16, sequence.litLength-16, ZSTD_no_overlap); |
1147 | 27.6k | } |
1148 | 374k | op = oLitEnd; |
1149 | 374k | *litPtr = iLitEnd; /* update for next sequence */ |
1150 | | |
1151 | | /* Copy Match */ |
1152 | 374k | if (sequence.offset > (size_t)(oLitEnd - prefixStart)) { |
1153 | | /* offset beyond prefix -> go into extDict */ |
1154 | 73 | RETURN_ERROR_IF(UNLIKELY(sequence.offset > (size_t)(oLitEnd - virtualStart)), corruption_detected, ""); |
1155 | 0 | match = dictEnd + (match - prefixStart); |
1156 | 0 | if (match + sequence.matchLength <= dictEnd) { |
1157 | 0 | ZSTD_memmove(oLitEnd, match, sequence.matchLength); |
1158 | 0 | return sequenceLength; |
1159 | 0 | } |
1160 | | /* span extDict & currentPrefixSegment */ |
1161 | 0 | { size_t const length1 = (size_t)(dictEnd - match); |
1162 | 0 | ZSTD_memmove(oLitEnd, match, length1); |
1163 | 0 | op = oLitEnd + length1; |
1164 | 0 | sequence.matchLength -= length1; |
1165 | 0 | match = prefixStart; |
1166 | 0 | } } |
1167 | | /* Match within prefix of 1 or more bytes */ |
1168 | 374k | assert(op <= oMatchEnd); |
1169 | 374k | assert(oMatchEnd <= oend_w); |
1170 | 374k | assert(match >= prefixStart); |
1171 | 374k | assert(sequence.matchLength >= 1); |
1172 | | |
1173 | | /* Nearly all offsets are >= WILDCOPY_VECLEN bytes, which means we can use wildcopy |
1174 | | * without overlap checking. |
1175 | | */ |
1176 | 374k | if (LIKELY(sequence.offset >= WILDCOPY_VECLEN)) { |
1177 | | /* We bet on a full wildcopy for matches, since we expect matches to be |
1178 | | * longer than literals (in general). In silesia, ~10% of matches are longer |
1179 | | * than 16 bytes. |
1180 | | */ |
1181 | 46.0k | ZSTD_wildcopy(op, match, sequence.matchLength, ZSTD_no_overlap); |
1182 | 46.0k | return sequenceLength; |
1183 | 46.0k | } |
1184 | 327k | assert(sequence.offset < WILDCOPY_VECLEN); |
1185 | | |
1186 | | /* Copy 8 bytes and spread the offset to be >= 8. */ |
1187 | 327k | ZSTD_overlapCopy8(&op, &match, sequence.offset); |
1188 | | |
1189 | | /* If the match length is > 8 bytes, then continue with the wildcopy. */ |
1190 | 327k | if (sequence.matchLength > 8) { |
1191 | 71.0k | assert(op < oMatchEnd); |
1192 | 71.0k | ZSTD_wildcopy(op, match, sequence.matchLength-8, ZSTD_overlap_src_before_dst); |
1193 | 71.0k | } |
1194 | 327k | return sequenceLength; |
1195 | 374k | } |
1196 | | |
1197 | | |
1198 | | static void |
1199 | | ZSTD_initFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, const ZSTD_seqSymbol* dt) |
1200 | 2.95k | { |
1201 | 2.95k | const void* ptr = dt; |
1202 | 2.95k | const ZSTD_seqSymbol_header* const DTableH = (const ZSTD_seqSymbol_header*)ptr; |
1203 | 2.95k | DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog); |
1204 | 2.95k | DEBUGLOG(6, "ZSTD_initFseState : val=%u using %u bits", |
1205 | 2.95k | (U32)DStatePtr->state, DTableH->tableLog); |
1206 | 2.95k | BIT_reloadDStream(bitD); |
1207 | 2.95k | DStatePtr->table = dt + 1; |
1208 | 2.95k | } |
1209 | | |
1210 | | FORCE_INLINE_TEMPLATE void |
1211 | | ZSTD_updateFseStateWithDInfo(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, U16 nextState, U32 nbBits) |
1212 | 6.34M | { |
1213 | 6.34M | size_t const lowBits = BIT_readBits(bitD, nbBits); |
1214 | 6.34M | DStatePtr->state = nextState + lowBits; |
1215 | 6.34M | } |
1216 | | |
1217 | | /* We need to add at most (ZSTD_WINDOWLOG_MAX_32 - 1) bits to read the maximum |
1218 | | * offset bits. But we can only read at most STREAM_ACCUMULATOR_MIN_32 |
1219 | | * bits before reloading. This value is the maximum number of bytes we read |
1220 | | * after reloading when we are decoding long offsets. |
1221 | | */ |
1222 | | #define LONG_OFFSETS_MAX_EXTRA_BITS_32 \ |
1223 | 0 | (ZSTD_WINDOWLOG_MAX_32 > STREAM_ACCUMULATOR_MIN_32 \ |
1224 | 0 | ? ZSTD_WINDOWLOG_MAX_32 - STREAM_ACCUMULATOR_MIN_32 \ |
1225 | 0 | : 0) |
1226 | | |
1227 | | typedef enum { ZSTD_lo_isRegularOffset, ZSTD_lo_isLongOffset=1 } ZSTD_longOffset_e; |
1228 | | |
1229 | | /** |
1230 | | * ZSTD_decodeSequence(): |
1231 | | * @p longOffsets : tells the decoder to reload more bit while decoding large offsets |
1232 | | * only used in 32-bit mode |
1233 | | * @return : Sequence (litL + matchL + offset) |
1234 | | */ |
1235 | | FORCE_INLINE_TEMPLATE seq_t |
1236 | | ZSTD_decodeSequence(seqState_t* seqState, const ZSTD_longOffset_e longOffsets, const int isLastSeq) |
1237 | 2.11M | { |
1238 | 2.11M | seq_t seq; |
1239 | | #if defined(__aarch64__) |
1240 | | size_t prevOffset0 = seqState->prevOffset[0]; |
1241 | | size_t prevOffset1 = seqState->prevOffset[1]; |
1242 | | size_t prevOffset2 = seqState->prevOffset[2]; |
1243 | | /* |
1244 | | * ZSTD_seqSymbol is a 64 bits wide structure. |
1245 | | * It can be loaded in one operation |
1246 | | * and its fields extracted by simply shifting or bit-extracting on aarch64. |
1247 | | * GCC doesn't recognize this and generates more unnecessary ldr/ldrb/ldrh |
1248 | | * operations that cause performance drop. This can be avoided by using this |
1249 | | * ZSTD_memcpy hack. |
1250 | | */ |
1251 | | # if defined(__GNUC__) && !defined(__clang__) |
1252 | | ZSTD_seqSymbol llDInfoS, mlDInfoS, ofDInfoS; |
1253 | | ZSTD_seqSymbol* const llDInfo = &llDInfoS; |
1254 | | ZSTD_seqSymbol* const mlDInfo = &mlDInfoS; |
1255 | | ZSTD_seqSymbol* const ofDInfo = &ofDInfoS; |
1256 | | ZSTD_memcpy(llDInfo, seqState->stateLL.table + seqState->stateLL.state, sizeof(ZSTD_seqSymbol)); |
1257 | | ZSTD_memcpy(mlDInfo, seqState->stateML.table + seqState->stateML.state, sizeof(ZSTD_seqSymbol)); |
1258 | | ZSTD_memcpy(ofDInfo, seqState->stateOffb.table + seqState->stateOffb.state, sizeof(ZSTD_seqSymbol)); |
1259 | | # else |
1260 | | const ZSTD_seqSymbol* const llDInfo = seqState->stateLL.table + seqState->stateLL.state; |
1261 | | const ZSTD_seqSymbol* const mlDInfo = seqState->stateML.table + seqState->stateML.state; |
1262 | | const ZSTD_seqSymbol* const ofDInfo = seqState->stateOffb.table + seqState->stateOffb.state; |
1263 | | # endif |
1264 | | (void)longOffsets; |
1265 | | seq.matchLength = mlDInfo->baseValue; |
1266 | | seq.litLength = llDInfo->baseValue; |
1267 | | { U32 const ofBase = ofDInfo->baseValue; |
1268 | | BYTE const llBits = llDInfo->nbAdditionalBits; |
1269 | | BYTE const mlBits = mlDInfo->nbAdditionalBits; |
1270 | | BYTE const ofBits = ofDInfo->nbAdditionalBits; |
1271 | | BYTE const totalBits = llBits+mlBits+ofBits; |
1272 | | |
1273 | | U16 const llNext = llDInfo->nextState; |
1274 | | U16 const mlNext = mlDInfo->nextState; |
1275 | | U16 const ofNext = ofDInfo->nextState; |
1276 | | U32 const llnbBits = llDInfo->nbBits; |
1277 | | U32 const mlnbBits = mlDInfo->nbBits; |
1278 | | U32 const ofnbBits = ofDInfo->nbBits; |
1279 | | |
1280 | | assert(llBits <= MaxLLBits); |
1281 | | assert(mlBits <= MaxMLBits); |
1282 | | assert(ofBits <= MaxOff); |
1283 | | /* As GCC has better branch and block analyzers, sometimes it is only |
1284 | | * valuable to mark likeliness for Clang. |
1285 | | */ |
1286 | | |
1287 | | /* sequence */ |
1288 | | { size_t offset; |
1289 | | if (ofBits > 1) { |
1290 | | ZSTD_STATIC_ASSERT(ZSTD_lo_isLongOffset == 1); |
1291 | | ZSTD_STATIC_ASSERT(LONG_OFFSETS_MAX_EXTRA_BITS_32 == 5); |
1292 | | ZSTD_STATIC_ASSERT(STREAM_ACCUMULATOR_MIN_32 > LONG_OFFSETS_MAX_EXTRA_BITS_32); |
1293 | | ZSTD_STATIC_ASSERT(STREAM_ACCUMULATOR_MIN_32 - LONG_OFFSETS_MAX_EXTRA_BITS_32 >= MaxMLBits); |
1294 | | offset = ofBase + BIT_readBitsFast(&seqState->DStream, ofBits/*>0*/); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */ |
1295 | | prevOffset2 = prevOffset1; |
1296 | | prevOffset1 = prevOffset0; |
1297 | | prevOffset0 = offset; |
1298 | | } else { |
1299 | | U32 const ll0 = (llDInfo->baseValue == 0); |
1300 | | if (LIKELY((ofBits == 0))) { |
1301 | | if (ll0) { |
1302 | | offset = prevOffset1; |
1303 | | prevOffset1 = prevOffset0; |
1304 | | prevOffset0 = offset; |
1305 | | } else { |
1306 | | offset = prevOffset0; |
1307 | | } |
1308 | | } else { |
1309 | | offset = ofBase + ll0 + BIT_readBitsFast(&seqState->DStream, 1); |
1310 | | { size_t temp = (offset == 1) ? prevOffset1 |
1311 | | : (offset == 3) ? prevOffset0 - 1 |
1312 | | : (offset >= 2) ? prevOffset2 |
1313 | | : prevOffset0; |
1314 | | /* 0 is not valid: input corrupted => force offset to -1 => |
1315 | | * corruption detected at execSequence. |
1316 | | */ |
1317 | | temp -= !temp; |
1318 | | prevOffset2 = (offset == 1) ? prevOffset2 : prevOffset1; |
1319 | | prevOffset1 = prevOffset0; |
1320 | | prevOffset0 = offset = temp; |
1321 | | } } } |
1322 | | seq.offset = offset; |
1323 | | } |
1324 | | |
1325 | | if (mlBits > 0) |
1326 | | seq.matchLength += BIT_readBitsFast(&seqState->DStream, mlBits/*>0*/); |
1327 | | |
1328 | | if (UNLIKELY(totalBits >= STREAM_ACCUMULATOR_MIN_64-(LLFSELog+MLFSELog+OffFSELog))) |
1329 | | BIT_reloadDStream(&seqState->DStream); |
1330 | | |
1331 | | /* Ensure there are enough bits to read the rest of data in 64-bit mode. */ |
1332 | | ZSTD_STATIC_ASSERT(16+LLFSELog+MLFSELog+OffFSELog < STREAM_ACCUMULATOR_MIN_64); |
1333 | | |
1334 | | if (llBits > 0) |
1335 | | seq.litLength += BIT_readBitsFast(&seqState->DStream, llBits/*>0*/); |
1336 | | |
1337 | | DEBUGLOG(6, "seq: litL=%u, matchL=%u, offset=%u", |
1338 | | (U32)seq.litLength, (U32)seq.matchLength, (U32)seq.offset); |
1339 | | |
1340 | | if (!isLastSeq) { |
1341 | | /* Don't update FSE state for last sequence. */ |
1342 | | ZSTD_updateFseStateWithDInfo(&seqState->stateLL, &seqState->DStream, llNext, llnbBits); /* <= 9 bits */ |
1343 | | ZSTD_updateFseStateWithDInfo(&seqState->stateML, &seqState->DStream, mlNext, mlnbBits); /* <= 9 bits */ |
1344 | | ZSTD_updateFseStateWithDInfo(&seqState->stateOffb, &seqState->DStream, ofNext, ofnbBits); /* <= 8 bits */ |
1345 | | BIT_reloadDStream(&seqState->DStream); |
1346 | | } |
1347 | | } |
1348 | | seqState->prevOffset[0] = prevOffset0; |
1349 | | seqState->prevOffset[1] = prevOffset1; |
1350 | | seqState->prevOffset[2] = prevOffset2; |
1351 | | #else /* !defined(__aarch64__) */ |
1352 | 2.11M | const ZSTD_seqSymbol* const llDInfo = seqState->stateLL.table + seqState->stateLL.state; |
1353 | 2.11M | const ZSTD_seqSymbol* const mlDInfo = seqState->stateML.table + seqState->stateML.state; |
1354 | 2.11M | const ZSTD_seqSymbol* const ofDInfo = seqState->stateOffb.table + seqState->stateOffb.state; |
1355 | 2.11M | seq.matchLength = mlDInfo->baseValue; |
1356 | 2.11M | seq.litLength = llDInfo->baseValue; |
1357 | 2.11M | { U32 const ofBase = ofDInfo->baseValue; |
1358 | 2.11M | BYTE const llBits = llDInfo->nbAdditionalBits; |
1359 | 2.11M | BYTE const mlBits = mlDInfo->nbAdditionalBits; |
1360 | 2.11M | BYTE const ofBits = ofDInfo->nbAdditionalBits; |
1361 | 2.11M | BYTE const totalBits = llBits+mlBits+ofBits; |
1362 | | |
1363 | 2.11M | U16 const llNext = llDInfo->nextState; |
1364 | 2.11M | U16 const mlNext = mlDInfo->nextState; |
1365 | 2.11M | U16 const ofNext = ofDInfo->nextState; |
1366 | 2.11M | U32 const llnbBits = llDInfo->nbBits; |
1367 | 2.11M | U32 const mlnbBits = mlDInfo->nbBits; |
1368 | 2.11M | U32 const ofnbBits = ofDInfo->nbBits; |
1369 | | |
1370 | 2.11M | assert(llBits <= MaxLLBits); |
1371 | 2.11M | assert(mlBits <= MaxMLBits); |
1372 | 2.11M | assert(ofBits <= MaxOff); |
1373 | | /* As GCC has better branch and block analyzers, sometimes it is only |
1374 | | * valuable to mark likeliness for Clang. |
1375 | | */ |
1376 | | |
1377 | | /* sequence */ |
1378 | 2.11M | { size_t offset; |
1379 | 2.11M | if (ofBits > 1) { |
1380 | 829k | ZSTD_STATIC_ASSERT(ZSTD_lo_isLongOffset == 1); |
1381 | 829k | ZSTD_STATIC_ASSERT(LONG_OFFSETS_MAX_EXTRA_BITS_32 == 5); |
1382 | 829k | ZSTD_STATIC_ASSERT(STREAM_ACCUMULATOR_MIN_32 > LONG_OFFSETS_MAX_EXTRA_BITS_32); |
1383 | 829k | ZSTD_STATIC_ASSERT(STREAM_ACCUMULATOR_MIN_32 - LONG_OFFSETS_MAX_EXTRA_BITS_32 >= MaxMLBits); |
1384 | 829k | if (MEM_32bits() && longOffsets && (ofBits >= STREAM_ACCUMULATOR_MIN_32)) { |
1385 | | /* Always read extra bits, this keeps the logic simple, |
1386 | | * avoids branches, and avoids accidentally reading 0 bits. |
1387 | | */ |
1388 | 0 | U32 const extraBits = LONG_OFFSETS_MAX_EXTRA_BITS_32; |
1389 | 0 | offset = ofBase + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << extraBits); |
1390 | 0 | BIT_reloadDStream(&seqState->DStream); |
1391 | 0 | offset += BIT_readBitsFast(&seqState->DStream, extraBits); |
1392 | 829k | } else { |
1393 | 829k | offset = ofBase + BIT_readBitsFast(&seqState->DStream, ofBits/*>0*/); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */ |
1394 | 829k | if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); |
1395 | 829k | } |
1396 | 829k | seqState->prevOffset[2] = seqState->prevOffset[1]; |
1397 | 829k | seqState->prevOffset[1] = seqState->prevOffset[0]; |
1398 | 829k | seqState->prevOffset[0] = offset; |
1399 | 1.28M | } else { |
1400 | 1.28M | U32 const ll0 = (llDInfo->baseValue == 0); |
1401 | 1.28M | if (LIKELY((ofBits == 0))) { |
1402 | 591k | offset = seqState->prevOffset[ll0]; |
1403 | 591k | seqState->prevOffset[1] = seqState->prevOffset[!ll0]; |
1404 | 591k | seqState->prevOffset[0] = offset; |
1405 | 693k | } else { |
1406 | 693k | offset = ofBase + ll0 + BIT_readBitsFast(&seqState->DStream, 1); |
1407 | 693k | { size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset]; |
1408 | 693k | temp -= !temp; /* 0 is not valid: input corrupted => force offset to -1 => corruption detected at execSequence */ |
1409 | 693k | if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1]; |
1410 | 693k | seqState->prevOffset[1] = seqState->prevOffset[0]; |
1411 | 693k | seqState->prevOffset[0] = offset = temp; |
1412 | 693k | } } } |
1413 | 2.11M | seq.offset = offset; |
1414 | 2.11M | } |
1415 | | |
1416 | 2.11M | if (mlBits > 0) |
1417 | 191k | seq.matchLength += BIT_readBitsFast(&seqState->DStream, mlBits/*>0*/); |
1418 | | |
1419 | 2.11M | if (MEM_32bits() && (mlBits+llBits >= STREAM_ACCUMULATOR_MIN_32-LONG_OFFSETS_MAX_EXTRA_BITS_32)) |
1420 | 0 | BIT_reloadDStream(&seqState->DStream); |
1421 | 2.11M | if (MEM_64bits() && UNLIKELY(totalBits >= STREAM_ACCUMULATOR_MIN_64-(LLFSELog+MLFSELog+OffFSELog))) |
1422 | 925 | BIT_reloadDStream(&seqState->DStream); |
1423 | | /* Ensure there are enough bits to read the rest of data in 64-bit mode. */ |
1424 | 2.11M | ZSTD_STATIC_ASSERT(16+LLFSELog+MLFSELog+OffFSELog < STREAM_ACCUMULATOR_MIN_64); |
1425 | | |
1426 | 2.11M | if (llBits > 0) |
1427 | 303k | seq.litLength += BIT_readBitsFast(&seqState->DStream, llBits/*>0*/); |
1428 | | |
1429 | 2.11M | if (MEM_32bits()) |
1430 | 0 | BIT_reloadDStream(&seqState->DStream); |
1431 | | |
1432 | 2.11M | DEBUGLOG(6, "seq: litL=%u, matchL=%u, offset=%u", |
1433 | 2.11M | (U32)seq.litLength, (U32)seq.matchLength, (U32)seq.offset); |
1434 | | |
1435 | 2.11M | if (!isLastSeq) { |
1436 | | /* Don't update FSE state for last sequence. */ |
1437 | 2.11M | ZSTD_updateFseStateWithDInfo(&seqState->stateLL, &seqState->DStream, llNext, llnbBits); /* <= 9 bits */ |
1438 | 2.11M | ZSTD_updateFseStateWithDInfo(&seqState->stateML, &seqState->DStream, mlNext, mlnbBits); /* <= 9 bits */ |
1439 | 2.11M | if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */ |
1440 | 2.11M | ZSTD_updateFseStateWithDInfo(&seqState->stateOffb, &seqState->DStream, ofNext, ofnbBits); /* <= 8 bits */ |
1441 | 2.11M | BIT_reloadDStream(&seqState->DStream); |
1442 | 2.11M | } |
1443 | 2.11M | } |
1444 | 2.11M | #endif /* defined(__aarch64__) */ |
1445 | | |
1446 | 2.11M | return seq; |
1447 | 2.11M | } |
1448 | | |
1449 | | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE) |
1450 | | #if DEBUGLEVEL >= 1 |
1451 | | static int ZSTD_dictionaryIsActive(ZSTD_DCtx const* dctx, BYTE const* prefixStart, BYTE const* oLitEnd) |
1452 | | { |
1453 | | size_t const windowSize = dctx->fParams.windowSize; |
1454 | | /* No dictionary used. */ |
1455 | | if (dctx->dictContentEndForFuzzing == NULL) return 0; |
1456 | | /* Dictionary is our prefix. */ |
1457 | | if (prefixStart == dctx->dictContentBeginForFuzzing) return 1; |
1458 | | /* Dictionary is not our ext-dict. */ |
1459 | | if (dctx->dictEnd != dctx->dictContentEndForFuzzing) return 0; |
1460 | | /* Dictionary is not within our window size. */ |
1461 | | if ((size_t)(oLitEnd - prefixStart) >= windowSize) return 0; |
1462 | | /* Dictionary is active. */ |
1463 | | return 1; |
1464 | | } |
1465 | | #endif |
1466 | | |
1467 | | static void ZSTD_assertValidSequence( |
1468 | | ZSTD_DCtx const* dctx, |
1469 | | BYTE const* op, BYTE const* oend, |
1470 | | seq_t const seq, |
1471 | | BYTE const* prefixStart, BYTE const* virtualStart) |
1472 | | { |
1473 | | #if DEBUGLEVEL >= 1 |
1474 | | if (dctx->isFrameDecompression) { |
1475 | | size_t const windowSize = dctx->fParams.windowSize; |
1476 | | size_t const sequenceSize = seq.litLength + seq.matchLength; |
1477 | | BYTE const* const oLitEnd = op + seq.litLength; |
1478 | | DEBUGLOG(6, "Checking sequence: litL=%u matchL=%u offset=%u", |
1479 | | (U32)seq.litLength, (U32)seq.matchLength, (U32)seq.offset); |
1480 | | assert(op <= oend); |
1481 | | assert((size_t)(oend - op) >= sequenceSize); |
1482 | | assert(sequenceSize <= ZSTD_blockSizeMax(dctx)); |
1483 | | if (ZSTD_dictionaryIsActive(dctx, prefixStart, oLitEnd)) { |
1484 | | size_t const dictSize = (size_t)((char const*)dctx->dictContentEndForFuzzing - (char const*)dctx->dictContentBeginForFuzzing); |
1485 | | /* Offset must be within the dictionary. */ |
1486 | | assert(seq.offset <= (size_t)(oLitEnd - virtualStart)); |
1487 | | assert(seq.offset <= windowSize + dictSize); |
1488 | | } else { |
1489 | | /* Offset must be within our window. */ |
1490 | | assert(seq.offset <= windowSize); |
1491 | | } |
1492 | | } |
1493 | | #else |
1494 | | (void)dctx, (void)op, (void)oend, (void)seq, (void)prefixStart, (void)virtualStart; |
1495 | | #endif |
1496 | | } |
1497 | | #endif |
1498 | | |
1499 | | #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG |
1500 | | |
1501 | | |
1502 | | FORCE_INLINE_TEMPLATE size_t |
1503 | | DONT_VECTORIZE |
1504 | | ZSTD_decompressSequences_bodySplitLitBuffer( ZSTD_DCtx* dctx, |
1505 | | void* dst, size_t maxDstSize, |
1506 | | const void* seqStart, size_t seqSize, int nbSeq, |
1507 | | const ZSTD_longOffset_e isLongOffset) |
1508 | 676 | { |
1509 | 676 | BYTE* const ostart = (BYTE*)dst; |
1510 | 676 | BYTE* const oend = (BYTE*)ZSTD_maybeNullPtrAdd(ostart, (ptrdiff_t)maxDstSize); |
1511 | 676 | BYTE* op = ostart; |
1512 | 676 | const BYTE* litPtr = dctx->litPtr; |
1513 | 676 | const BYTE* litBufferEnd = dctx->litBufferEnd; |
1514 | 676 | const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart); |
1515 | 676 | const BYTE* const vBase = (const BYTE*) (dctx->virtualStart); |
1516 | 676 | const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); |
1517 | 676 | DEBUGLOG(5, "ZSTD_decompressSequences_bodySplitLitBuffer (%i seqs)", nbSeq); |
1518 | | |
1519 | | /* Literals are split between internal buffer & output buffer */ |
1520 | 676 | if (nbSeq) { |
1521 | 676 | seqState_t seqState; |
1522 | 676 | dctx->fseEntropy = 1; |
1523 | 2.70k | { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; } |
1524 | 676 | RETURN_ERROR_IF( |
1525 | 676 | ERR_isError(BIT_initDStream(&seqState.DStream, seqStart, seqSize)), |
1526 | 676 | corruption_detected, ""); |
1527 | 662 | ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr); |
1528 | 662 | ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr); |
1529 | 662 | ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr); |
1530 | 662 | assert(dst != NULL); |
1531 | | |
1532 | 662 | ZSTD_STATIC_ASSERT( |
1533 | 662 | BIT_DStream_unfinished < BIT_DStream_completed && |
1534 | 662 | BIT_DStream_endOfBuffer < BIT_DStream_completed && |
1535 | 662 | BIT_DStream_completed < BIT_DStream_overflow); |
1536 | | |
1537 | | /* decompress without overrunning litPtr begins */ |
1538 | 662 | { seq_t sequence = {0,0,0}; /* some static analyzer believe that @sequence is not initialized (it necessarily is, since for(;;) loop as at least one iteration) */ |
1539 | | /* Align the decompression loop to 32 + 16 bytes. |
1540 | | * |
1541 | | * zstd compiled with gcc-9 on an Intel i9-9900k shows 10% decompression |
1542 | | * speed swings based on the alignment of the decompression loop. This |
1543 | | * performance swing is caused by parts of the decompression loop falling |
1544 | | * out of the DSB. The entire decompression loop should fit in the DSB, |
1545 | | * when it can't we get much worse performance. You can measure if you've |
1546 | | * hit the good case or the bad case with this perf command for some |
1547 | | * compressed file test.zst: |
1548 | | * |
1549 | | * perf stat -e cycles -e instructions -e idq.all_dsb_cycles_any_uops \ |
1550 | | * -e idq.all_mite_cycles_any_uops -- ./zstd -tq test.zst |
1551 | | * |
1552 | | * If you see most cycles served out of the MITE you've hit the bad case. |
1553 | | * If you see most cycles served out of the DSB you've hit the good case. |
1554 | | * If it is pretty even then you may be in an okay case. |
1555 | | * |
1556 | | * This issue has been reproduced on the following CPUs: |
1557 | | * - Kabylake: Macbook Pro (15-inch, 2019) 2.4 GHz Intel Core i9 |
1558 | | * Use Instruments->Counters to get DSB/MITE cycles. |
1559 | | * I never got performance swings, but I was able to |
1560 | | * go from the good case of mostly DSB to half of the |
1561 | | * cycles served from MITE. |
1562 | | * - Coffeelake: Intel i9-9900k |
1563 | | * - Coffeelake: Intel i7-9700k |
1564 | | * |
1565 | | * I haven't been able to reproduce the instability or DSB misses on any |
1566 | | * of the following CPUS: |
1567 | | * - Haswell |
1568 | | * - Broadwell: Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GH |
1569 | | * - Skylake |
1570 | | * |
1571 | | * Alignment is done for each of the three major decompression loops: |
1572 | | * - ZSTD_decompressSequences_bodySplitLitBuffer - presplit section of the literal buffer |
1573 | | * - ZSTD_decompressSequences_bodySplitLitBuffer - postsplit section of the literal buffer |
1574 | | * - ZSTD_decompressSequences_body |
1575 | | * Alignment choices are made to minimize large swings on bad cases and influence on performance |
1576 | | * from changes external to this code, rather than to overoptimize on the current commit. |
1577 | | * |
1578 | | * If you are seeing performance stability this script can help test. |
1579 | | * It tests on 4 commits in zstd where I saw performance change. |
1580 | | * |
1581 | | * https://gist.github.com/terrelln/9889fc06a423fd5ca6e99351564473f4 |
1582 | | */ |
1583 | 662 | #if defined(__GNUC__) && defined(__x86_64__) |
1584 | 662 | __asm__(".p2align 6"); |
1585 | | # if __GNUC__ >= 7 |
1586 | | /* good for gcc-7, gcc-9, and gcc-11 */ |
1587 | | __asm__("nop"); |
1588 | | __asm__(".p2align 5"); |
1589 | | __asm__("nop"); |
1590 | | __asm__(".p2align 4"); |
1591 | | # if __GNUC__ == 8 || __GNUC__ == 10 |
1592 | | /* good for gcc-8 and gcc-10 */ |
1593 | | __asm__("nop"); |
1594 | | __asm__(".p2align 3"); |
1595 | | # endif |
1596 | | # endif |
1597 | 662 | #endif |
1598 | | |
1599 | | /* Handle the initial state where litBuffer is currently split between dst and litExtraBuffer */ |
1600 | 890k | for ( ; nbSeq; nbSeq--) { |
1601 | 890k | sequence = ZSTD_decodeSequence(&seqState, isLongOffset, nbSeq==1); |
1602 | 890k | if (litPtr + sequence.litLength > dctx->litBufferEnd) break; |
1603 | 890k | { size_t const oneSeqSize = ZSTD_execSequenceSplitLitBuffer(op, oend, litPtr + sequence.litLength - WILDCOPY_OVERLENGTH, sequence, &litPtr, litBufferEnd, prefixStart, vBase, dictEnd); |
1604 | | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE) |
1605 | | assert(!ZSTD_isError(oneSeqSize)); |
1606 | | ZSTD_assertValidSequence(dctx, op, oend, sequence, prefixStart, vBase); |
1607 | | #endif |
1608 | 890k | if (UNLIKELY(ZSTD_isError(oneSeqSize))) |
1609 | 169 | return oneSeqSize; |
1610 | 890k | DEBUGLOG(6, "regenerated sequence size : %u", (U32)oneSeqSize); |
1611 | 890k | op += oneSeqSize; |
1612 | 890k | } } |
1613 | 493 | DEBUGLOG(6, "reached: (litPtr + sequence.litLength > dctx->litBufferEnd)"); |
1614 | | |
1615 | | /* If there are more sequences, they will need to read literals from litExtraBuffer; copy over the remainder from dst and update litPtr and litEnd */ |
1616 | 493 | if (nbSeq > 0) { |
1617 | 451 | const size_t leftoverLit = (size_t)(dctx->litBufferEnd - litPtr); |
1618 | 451 | assert(dctx->litBufferEnd >= litPtr); |
1619 | 451 | DEBUGLOG(6, "There are %i sequences left, and %zu/%zu literals left in buffer", nbSeq, leftoverLit, sequence.litLength); |
1620 | 451 | if (leftoverLit) { |
1621 | 441 | RETURN_ERROR_IF(leftoverLit > (size_t)(oend - op), dstSize_tooSmall, "remaining lit must fit within dstBuffer"); |
1622 | 438 | ZSTD_safecopyDstBeforeSrc(op, litPtr, leftoverLit); |
1623 | 438 | sequence.litLength -= leftoverLit; |
1624 | 438 | op += leftoverLit; |
1625 | 438 | } |
1626 | 448 | litPtr = dctx->litExtraBuffer; |
1627 | 448 | litBufferEnd = dctx->litExtraBuffer + ZSTD_LITBUFFEREXTRASIZE; |
1628 | 448 | dctx->litBufferLocation = ZSTD_not_in_dst; |
1629 | 448 | { size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litBufferEnd, prefixStart, vBase, dictEnd); |
1630 | | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE) |
1631 | | assert(!ZSTD_isError(oneSeqSize)); |
1632 | | ZSTD_assertValidSequence(dctx, op, oend, sequence, prefixStart, vBase); |
1633 | | #endif |
1634 | 448 | if (UNLIKELY(ZSTD_isError(oneSeqSize))) |
1635 | 19 | return oneSeqSize; |
1636 | 429 | DEBUGLOG(6, "regenerated sequence size : %u", (U32)oneSeqSize); |
1637 | 429 | op += oneSeqSize; |
1638 | 429 | } |
1639 | 0 | nbSeq--; |
1640 | 429 | } |
1641 | 493 | } |
1642 | | |
1643 | 471 | if (nbSeq > 0) { |
1644 | | /* there is remaining lit from extra buffer */ |
1645 | | |
1646 | 428 | #if defined(__GNUC__) && defined(__x86_64__) |
1647 | 428 | __asm__(".p2align 6"); |
1648 | 428 | __asm__("nop"); |
1649 | 428 | # if __GNUC__ != 7 |
1650 | | /* worse for gcc-7 better for gcc-8, gcc-9, and gcc-10 and clang */ |
1651 | 428 | __asm__(".p2align 4"); |
1652 | 428 | __asm__("nop"); |
1653 | 428 | __asm__(".p2align 3"); |
1654 | | # elif __GNUC__ >= 11 |
1655 | | __asm__(".p2align 3"); |
1656 | | # else |
1657 | | __asm__(".p2align 5"); |
1658 | | __asm__("nop"); |
1659 | | __asm__(".p2align 3"); |
1660 | | # endif |
1661 | 428 | #endif |
1662 | | |
1663 | 838k | for ( ; nbSeq ; nbSeq--) { |
1664 | 838k | seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset, nbSeq==1); |
1665 | 838k | size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litBufferEnd, prefixStart, vBase, dictEnd); |
1666 | | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE) |
1667 | | assert(!ZSTD_isError(oneSeqSize)); |
1668 | | ZSTD_assertValidSequence(dctx, op, oend, sequence, prefixStart, vBase); |
1669 | | #endif |
1670 | 838k | if (UNLIKELY(ZSTD_isError(oneSeqSize))) |
1671 | 357 | return oneSeqSize; |
1672 | 838k | DEBUGLOG(6, "regenerated sequence size : %u", (U32)oneSeqSize); |
1673 | 838k | op += oneSeqSize; |
1674 | 838k | } |
1675 | 428 | } |
1676 | | |
1677 | | /* check if reached exact end */ |
1678 | 114 | DEBUGLOG(5, "ZSTD_decompressSequences_bodySplitLitBuffer: after decode loop, remaining nbSeq : %i", nbSeq); |
1679 | 114 | RETURN_ERROR_IF(nbSeq, corruption_detected, ""); |
1680 | 114 | DEBUGLOG(5, "bitStream : start=%p, ptr=%p, bitsConsumed=%u", seqState.DStream.start, seqState.DStream.ptr, seqState.DStream.bitsConsumed); |
1681 | 114 | RETURN_ERROR_IF(!BIT_endOfDStream(&seqState.DStream), corruption_detected, ""); |
1682 | | /* save reps for next block */ |
1683 | 24 | { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); } |
1684 | 6 | } |
1685 | | |
1686 | | /* last literal segment */ |
1687 | 6 | if (dctx->litBufferLocation == ZSTD_split) { |
1688 | | /* split hasn't been reached yet, first get dst then copy litExtraBuffer */ |
1689 | 3 | size_t const lastLLSize = (size_t)(litBufferEnd - litPtr); |
1690 | 3 | DEBUGLOG(6, "copy last literals from segment : %u", (U32)lastLLSize); |
1691 | 3 | RETURN_ERROR_IF(lastLLSize > (size_t)(oend - op), dstSize_tooSmall, ""); |
1692 | 3 | if (op != NULL) { |
1693 | 3 | ZSTD_memmove(op, litPtr, lastLLSize); |
1694 | 3 | op += lastLLSize; |
1695 | 3 | } |
1696 | 3 | litPtr = dctx->litExtraBuffer; |
1697 | 3 | litBufferEnd = dctx->litExtraBuffer + ZSTD_LITBUFFEREXTRASIZE; |
1698 | 3 | dctx->litBufferLocation = ZSTD_not_in_dst; |
1699 | 3 | } |
1700 | | /* copy last literals from internal buffer */ |
1701 | 6 | { size_t const lastLLSize = (size_t)(litBufferEnd - litPtr); |
1702 | 6 | DEBUGLOG(6, "copy last literals from internal buffer : %u", (U32)lastLLSize); |
1703 | 6 | RETURN_ERROR_IF(lastLLSize > (size_t)(oend-op), dstSize_tooSmall, ""); |
1704 | 6 | if (op != NULL) { |
1705 | 6 | ZSTD_memcpy(op, litPtr, lastLLSize); |
1706 | 6 | op += lastLLSize; |
1707 | 6 | } } |
1708 | | |
1709 | 6 | DEBUGLOG(6, "decoded block of size %u bytes", (U32)(op - ostart)); |
1710 | 6 | return (size_t)(op - ostart); |
1711 | 6 | } |
1712 | | |
1713 | | FORCE_INLINE_TEMPLATE size_t |
1714 | | DONT_VECTORIZE |
1715 | | ZSTD_decompressSequences_body(ZSTD_DCtx* dctx, |
1716 | | void* dst, size_t maxDstSize, |
1717 | | const void* seqStart, size_t seqSize, int nbSeq, |
1718 | | const ZSTD_longOffset_e isLongOffset) |
1719 | 441 | { |
1720 | 441 | BYTE* const ostart = (BYTE*)dst; |
1721 | 441 | BYTE* const oend = (dctx->litBufferLocation == ZSTD_not_in_dst) ? |
1722 | 412 | (BYTE*)ZSTD_maybeNullPtrAdd(ostart, (ptrdiff_t)maxDstSize) : |
1723 | 441 | dctx->litBuffer; |
1724 | 441 | BYTE* op = ostart; |
1725 | 441 | const BYTE* litPtr = dctx->litPtr; |
1726 | 441 | const BYTE* const litEnd = litPtr + dctx->litSize; |
1727 | 441 | const BYTE* const prefixStart = (const BYTE*)(dctx->prefixStart); |
1728 | 441 | const BYTE* const vBase = (const BYTE*)(dctx->virtualStart); |
1729 | 441 | const BYTE* const dictEnd = (const BYTE*)(dctx->dictEnd); |
1730 | 441 | DEBUGLOG(5, "ZSTD_decompressSequences_body: nbSeq = %d", nbSeq); |
1731 | | |
1732 | | /* Regen sequences */ |
1733 | 441 | if (nbSeq) { |
1734 | 345 | seqState_t seqState; |
1735 | 345 | dctx->fseEntropy = 1; |
1736 | 1.38k | { U32 i; for (i = 0; i < ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; } |
1737 | 345 | RETURN_ERROR_IF( |
1738 | 345 | ERR_isError(BIT_initDStream(&seqState.DStream, seqStart, seqSize)), |
1739 | 345 | corruption_detected, ""); |
1740 | 323 | ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr); |
1741 | 323 | ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr); |
1742 | 323 | ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr); |
1743 | 323 | assert(dst != NULL); |
1744 | | |
1745 | 323 | #if defined(__GNUC__) && defined(__x86_64__) |
1746 | 323 | __asm__(".p2align 6"); |
1747 | 323 | __asm__("nop"); |
1748 | | # if __GNUC__ >= 7 |
1749 | | __asm__(".p2align 5"); |
1750 | | __asm__("nop"); |
1751 | | __asm__(".p2align 3"); |
1752 | | # else |
1753 | 323 | __asm__(".p2align 4"); |
1754 | 323 | __asm__("nop"); |
1755 | 323 | __asm__(".p2align 3"); |
1756 | 323 | # endif |
1757 | 323 | #endif |
1758 | | |
1759 | 385k | for ( ; nbSeq ; nbSeq--) { |
1760 | 385k | seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset, nbSeq==1); |
1761 | 385k | size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, prefixStart, vBase, dictEnd); |
1762 | | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE) |
1763 | | assert(!ZSTD_isError(oneSeqSize)); |
1764 | | ZSTD_assertValidSequence(dctx, op, oend, sequence, prefixStart, vBase); |
1765 | | #endif |
1766 | 385k | if (UNLIKELY(ZSTD_isError(oneSeqSize))) |
1767 | 295 | return oneSeqSize; |
1768 | 385k | DEBUGLOG(6, "regenerated sequence size : %u", (U32)oneSeqSize); |
1769 | 385k | op += oneSeqSize; |
1770 | 385k | } |
1771 | | |
1772 | | /* check if reached exact end */ |
1773 | 28 | assert(nbSeq == 0); |
1774 | 28 | RETURN_ERROR_IF(!BIT_endOfDStream(&seqState.DStream), corruption_detected, ""); |
1775 | | /* save reps for next block */ |
1776 | 32 | { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); } |
1777 | 8 | } |
1778 | | |
1779 | | /* last literal segment */ |
1780 | 104 | { size_t const lastLLSize = (size_t)(litEnd - litPtr); |
1781 | 104 | DEBUGLOG(6, "copy last literals : %u", (U32)lastLLSize); |
1782 | 104 | RETURN_ERROR_IF(lastLLSize > (size_t)(oend-op), dstSize_tooSmall, ""); |
1783 | 104 | if (op != NULL) { |
1784 | 104 | ZSTD_memcpy(op, litPtr, lastLLSize); |
1785 | 104 | op += lastLLSize; |
1786 | 104 | } } |
1787 | | |
1788 | 104 | DEBUGLOG(6, "decoded block of size %u bytes", (U32)(op - ostart)); |
1789 | 104 | return (size_t)(op - ostart); |
1790 | 104 | } |
1791 | | |
1792 | | static size_t |
1793 | | ZSTD_decompressSequences_default(ZSTD_DCtx* dctx, |
1794 | | void* dst, size_t maxDstSize, |
1795 | | const void* seqStart, size_t seqSize, int nbSeq, |
1796 | | const ZSTD_longOffset_e isLongOffset) |
1797 | 0 | { |
1798 | 0 | return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
1799 | 0 | } |
1800 | | |
1801 | | static size_t |
1802 | | ZSTD_decompressSequencesSplitLitBuffer_default(ZSTD_DCtx* dctx, |
1803 | | void* dst, size_t maxDstSize, |
1804 | | const void* seqStart, size_t seqSize, int nbSeq, |
1805 | | const ZSTD_longOffset_e isLongOffset) |
1806 | 0 | { |
1807 | 0 | return ZSTD_decompressSequences_bodySplitLitBuffer(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
1808 | 0 | } |
1809 | | #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */ |
1810 | | |
1811 | | #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT |
1812 | | |
1813 | | FORCE_INLINE_TEMPLATE |
1814 | | |
1815 | | size_t ZSTD_prefetchMatch(size_t prefetchPos, seq_t const sequence, |
1816 | | const BYTE* const prefixStart, const BYTE* const dictEnd) |
1817 | 0 | { |
1818 | 0 | prefetchPos += sequence.litLength; |
1819 | 0 | { const BYTE* const matchBase = (sequence.offset > prefetchPos) ? dictEnd : prefixStart; |
1820 | | /* note : this operation can overflow when seq.offset is really too large, which can only happen when input is corrupted. |
1821 | | * No consequence though : memory address is only used for prefetching, not for dereferencing */ |
1822 | 0 | const BYTE* const match = (const BYTE*)ZSTD_wrappedPtrSub(ZSTD_wrappedPtrAdd(matchBase, (ptrdiff_t)prefetchPos), (ptrdiff_t)sequence.offset); |
1823 | 0 | PREFETCH_L1(match); PREFETCH_L1(ZSTD_wrappedPtrAdd(match, CACHELINE_SIZE)); /* note : it's safe to invoke PREFETCH() on any memory address, including invalid ones */ |
1824 | 0 | } |
1825 | 0 | return prefetchPos + sequence.matchLength; |
1826 | 0 | } |
1827 | | |
1828 | | /* This decoding function employs prefetching |
1829 | | * to reduce latency impact of cache misses. |
1830 | | * It's generally employed when block contains a significant portion of long-distance matches |
1831 | | * or when coupled with a "cold" dictionary */ |
1832 | | FORCE_INLINE_TEMPLATE size_t |
1833 | | ZSTD_decompressSequencesLong_body( |
1834 | | ZSTD_DCtx* dctx, |
1835 | | void* dst, size_t maxDstSize, |
1836 | | const void* seqStart, size_t seqSize, int nbSeq, |
1837 | | const ZSTD_longOffset_e isLongOffset) |
1838 | 0 | { |
1839 | 0 | BYTE* const ostart = (BYTE*)dst; |
1840 | 0 | BYTE* const oend = (dctx->litBufferLocation == ZSTD_in_dst) ? |
1841 | 0 | dctx->litBuffer : |
1842 | 0 | (BYTE*)ZSTD_maybeNullPtrAdd(ostart, (ptrdiff_t)maxDstSize); |
1843 | 0 | BYTE* op = ostart; |
1844 | 0 | const BYTE* litPtr = dctx->litPtr; |
1845 | 0 | const BYTE* litBufferEnd = dctx->litBufferEnd; |
1846 | 0 | const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart); |
1847 | 0 | const BYTE* const dictStart = (const BYTE*) (dctx->virtualStart); |
1848 | 0 | const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); |
1849 | | |
1850 | | /* Regen sequences */ |
1851 | 0 | if (nbSeq) { |
1852 | 0 | #define STORED_SEQS 8 |
1853 | 0 | #define STORED_SEQS_MASK (STORED_SEQS-1) |
1854 | 0 | #define ADVANCED_SEQS STORED_SEQS |
1855 | 0 | seq_t sequences[STORED_SEQS]; |
1856 | 0 | int const seqAdvance = MIN(nbSeq, ADVANCED_SEQS); |
1857 | 0 | seqState_t seqState; |
1858 | 0 | int seqNb; |
1859 | 0 | size_t prefetchPos = (size_t)(op-prefixStart); /* track position relative to prefixStart */ |
1860 | |
|
1861 | 0 | dctx->fseEntropy = 1; |
1862 | 0 | { int i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; } |
1863 | 0 | assert(dst != NULL); |
1864 | 0 | RETURN_ERROR_IF( |
1865 | 0 | ERR_isError(BIT_initDStream(&seqState.DStream, seqStart, seqSize)), |
1866 | 0 | corruption_detected, ""); |
1867 | 0 | ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr); |
1868 | 0 | ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr); |
1869 | 0 | ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr); |
1870 | | |
1871 | | /* prepare in advance */ |
1872 | 0 | for (seqNb=0; seqNb<seqAdvance; seqNb++) { |
1873 | 0 | seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset, seqNb == nbSeq-1); |
1874 | 0 | prefetchPos = ZSTD_prefetchMatch(prefetchPos, sequence, prefixStart, dictEnd); |
1875 | 0 | sequences[seqNb] = sequence; |
1876 | 0 | } |
1877 | | |
1878 | | /* decompress without stomping litBuffer */ |
1879 | 0 | for (; seqNb < nbSeq; seqNb++) { |
1880 | 0 | seq_t sequence = ZSTD_decodeSequence(&seqState, isLongOffset, seqNb == nbSeq-1); |
1881 | |
|
1882 | 0 | if (dctx->litBufferLocation == ZSTD_split && litPtr + sequences[(seqNb - ADVANCED_SEQS) & STORED_SEQS_MASK].litLength > dctx->litBufferEnd) { |
1883 | | /* lit buffer is reaching split point, empty out the first buffer and transition to litExtraBuffer */ |
1884 | 0 | const size_t leftoverLit = (size_t)(dctx->litBufferEnd - litPtr); |
1885 | 0 | assert(dctx->litBufferEnd >= litPtr); |
1886 | 0 | if (leftoverLit) { |
1887 | 0 | RETURN_ERROR_IF(leftoverLit > (size_t)(oend - op), dstSize_tooSmall, "remaining lit must fit within dstBuffer"); |
1888 | 0 | ZSTD_safecopyDstBeforeSrc(op, litPtr, leftoverLit); |
1889 | 0 | sequences[(seqNb - ADVANCED_SEQS) & STORED_SEQS_MASK].litLength -= leftoverLit; |
1890 | 0 | op += leftoverLit; |
1891 | 0 | } |
1892 | 0 | litPtr = dctx->litExtraBuffer; |
1893 | 0 | litBufferEnd = dctx->litExtraBuffer + ZSTD_LITBUFFEREXTRASIZE; |
1894 | 0 | dctx->litBufferLocation = ZSTD_not_in_dst; |
1895 | 0 | { size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequences[(seqNb - ADVANCED_SEQS) & STORED_SEQS_MASK], &litPtr, litBufferEnd, prefixStart, dictStart, dictEnd); |
1896 | | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE) |
1897 | | assert(!ZSTD_isError(oneSeqSize)); |
1898 | | ZSTD_assertValidSequence(dctx, op, oend, sequences[(seqNb - ADVANCED_SEQS) & STORED_SEQS_MASK], prefixStart, dictStart); |
1899 | | #endif |
1900 | 0 | if (ZSTD_isError(oneSeqSize)) return oneSeqSize; |
1901 | | |
1902 | 0 | prefetchPos = ZSTD_prefetchMatch(prefetchPos, sequence, prefixStart, dictEnd); |
1903 | 0 | sequences[seqNb & STORED_SEQS_MASK] = sequence; |
1904 | 0 | op += oneSeqSize; |
1905 | 0 | } } |
1906 | 0 | else |
1907 | 0 | { |
1908 | | /* lit buffer is either wholly contained in first or second split, or not split at all*/ |
1909 | 0 | size_t const oneSeqSize = dctx->litBufferLocation == ZSTD_split ? |
1910 | 0 | ZSTD_execSequenceSplitLitBuffer(op, oend, litPtr + sequences[(seqNb - ADVANCED_SEQS) & STORED_SEQS_MASK].litLength - WILDCOPY_OVERLENGTH, sequences[(seqNb - ADVANCED_SEQS) & STORED_SEQS_MASK], &litPtr, litBufferEnd, prefixStart, dictStart, dictEnd) : |
1911 | 0 | ZSTD_execSequence(op, oend, sequences[(seqNb - ADVANCED_SEQS) & STORED_SEQS_MASK], &litPtr, litBufferEnd, prefixStart, dictStart, dictEnd); |
1912 | | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE) |
1913 | | assert(!ZSTD_isError(oneSeqSize)); |
1914 | | ZSTD_assertValidSequence(dctx, op, oend, sequences[(seqNb - ADVANCED_SEQS) & STORED_SEQS_MASK], prefixStart, dictStart); |
1915 | | #endif |
1916 | 0 | if (ZSTD_isError(oneSeqSize)) return oneSeqSize; |
1917 | | |
1918 | 0 | prefetchPos = ZSTD_prefetchMatch(prefetchPos, sequence, prefixStart, dictEnd); |
1919 | 0 | sequences[seqNb & STORED_SEQS_MASK] = sequence; |
1920 | 0 | op += oneSeqSize; |
1921 | 0 | } |
1922 | 0 | } |
1923 | 0 | RETURN_ERROR_IF(!BIT_endOfDStream(&seqState.DStream), corruption_detected, ""); |
1924 | | |
1925 | | /* finish queue */ |
1926 | 0 | seqNb -= seqAdvance; |
1927 | 0 | for ( ; seqNb<nbSeq ; seqNb++) { |
1928 | 0 | seq_t *sequence = &(sequences[seqNb&STORED_SEQS_MASK]); |
1929 | 0 | if (dctx->litBufferLocation == ZSTD_split && litPtr + sequence->litLength > dctx->litBufferEnd) { |
1930 | 0 | const size_t leftoverLit = (size_t)(dctx->litBufferEnd - litPtr); |
1931 | 0 | assert(dctx->litBufferEnd >= litPtr); |
1932 | 0 | if (leftoverLit) { |
1933 | 0 | RETURN_ERROR_IF(leftoverLit > (size_t)(oend - op), dstSize_tooSmall, "remaining lit must fit within dstBuffer"); |
1934 | 0 | ZSTD_safecopyDstBeforeSrc(op, litPtr, leftoverLit); |
1935 | 0 | sequence->litLength -= leftoverLit; |
1936 | 0 | op += leftoverLit; |
1937 | 0 | } |
1938 | 0 | litPtr = dctx->litExtraBuffer; |
1939 | 0 | litBufferEnd = dctx->litExtraBuffer + ZSTD_LITBUFFEREXTRASIZE; |
1940 | 0 | dctx->litBufferLocation = ZSTD_not_in_dst; |
1941 | 0 | { size_t const oneSeqSize = ZSTD_execSequence(op, oend, *sequence, &litPtr, litBufferEnd, prefixStart, dictStart, dictEnd); |
1942 | | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE) |
1943 | | assert(!ZSTD_isError(oneSeqSize)); |
1944 | | ZSTD_assertValidSequence(dctx, op, oend, sequences[seqNb&STORED_SEQS_MASK], prefixStart, dictStart); |
1945 | | #endif |
1946 | 0 | if (ZSTD_isError(oneSeqSize)) return oneSeqSize; |
1947 | 0 | op += oneSeqSize; |
1948 | 0 | } |
1949 | 0 | } |
1950 | 0 | else |
1951 | 0 | { |
1952 | 0 | size_t const oneSeqSize = dctx->litBufferLocation == ZSTD_split ? |
1953 | 0 | ZSTD_execSequenceSplitLitBuffer(op, oend, litPtr + sequence->litLength - WILDCOPY_OVERLENGTH, *sequence, &litPtr, litBufferEnd, prefixStart, dictStart, dictEnd) : |
1954 | 0 | ZSTD_execSequence(op, oend, *sequence, &litPtr, litBufferEnd, prefixStart, dictStart, dictEnd); |
1955 | | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE) |
1956 | | assert(!ZSTD_isError(oneSeqSize)); |
1957 | | ZSTD_assertValidSequence(dctx, op, oend, sequences[seqNb&STORED_SEQS_MASK], prefixStart, dictStart); |
1958 | | #endif |
1959 | 0 | if (ZSTD_isError(oneSeqSize)) return oneSeqSize; |
1960 | 0 | op += oneSeqSize; |
1961 | 0 | } |
1962 | 0 | } |
1963 | | |
1964 | | /* save reps for next block */ |
1965 | 0 | { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); } |
1966 | 0 | } |
1967 | | |
1968 | | /* last literal segment */ |
1969 | 0 | if (dctx->litBufferLocation == ZSTD_split) { /* first deplete literal buffer in dst, then copy litExtraBuffer */ |
1970 | 0 | size_t const lastLLSize = (size_t)(litBufferEnd - litPtr); |
1971 | 0 | assert(litBufferEnd >= litPtr); |
1972 | 0 | RETURN_ERROR_IF(lastLLSize > (size_t)(oend - op), dstSize_tooSmall, ""); |
1973 | 0 | if (op != NULL) { |
1974 | 0 | ZSTD_memmove(op, litPtr, lastLLSize); |
1975 | 0 | op += lastLLSize; |
1976 | 0 | } |
1977 | 0 | litPtr = dctx->litExtraBuffer; |
1978 | 0 | litBufferEnd = dctx->litExtraBuffer + ZSTD_LITBUFFEREXTRASIZE; |
1979 | 0 | } |
1980 | 0 | { size_t const lastLLSize = (size_t)(litBufferEnd - litPtr); |
1981 | 0 | assert(litBufferEnd >= litPtr); |
1982 | 0 | RETURN_ERROR_IF(lastLLSize > (size_t)(oend-op), dstSize_tooSmall, ""); |
1983 | 0 | if (op != NULL) { |
1984 | 0 | ZSTD_memmove(op, litPtr, lastLLSize); |
1985 | 0 | op += lastLLSize; |
1986 | 0 | } |
1987 | 0 | } |
1988 | | |
1989 | 0 | return (size_t)(op - ostart); |
1990 | 0 | } |
1991 | | |
1992 | | static size_t |
1993 | | ZSTD_decompressSequencesLong_default(ZSTD_DCtx* dctx, |
1994 | | void* dst, size_t maxDstSize, |
1995 | | const void* seqStart, size_t seqSize, int nbSeq, |
1996 | | const ZSTD_longOffset_e isLongOffset) |
1997 | 0 | { |
1998 | 0 | return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
1999 | 0 | } |
2000 | | #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */ |
2001 | | |
2002 | | |
2003 | | |
2004 | | #if DYNAMIC_BMI2 |
2005 | | |
2006 | | #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG |
2007 | | static BMI2_TARGET_ATTRIBUTE size_t |
2008 | | DONT_VECTORIZE |
2009 | | ZSTD_decompressSequences_bmi2(ZSTD_DCtx* dctx, |
2010 | | void* dst, size_t maxDstSize, |
2011 | | const void* seqStart, size_t seqSize, int nbSeq, |
2012 | | const ZSTD_longOffset_e isLongOffset) |
2013 | 441 | { |
2014 | 441 | return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
2015 | 441 | } |
2016 | | static BMI2_TARGET_ATTRIBUTE size_t |
2017 | | DONT_VECTORIZE |
2018 | | ZSTD_decompressSequencesSplitLitBuffer_bmi2(ZSTD_DCtx* dctx, |
2019 | | void* dst, size_t maxDstSize, |
2020 | | const void* seqStart, size_t seqSize, int nbSeq, |
2021 | | const ZSTD_longOffset_e isLongOffset) |
2022 | 676 | { |
2023 | 676 | return ZSTD_decompressSequences_bodySplitLitBuffer(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
2024 | 676 | } |
2025 | | #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */ |
2026 | | |
2027 | | #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT |
2028 | | static BMI2_TARGET_ATTRIBUTE size_t |
2029 | | ZSTD_decompressSequencesLong_bmi2(ZSTD_DCtx* dctx, |
2030 | | void* dst, size_t maxDstSize, |
2031 | | const void* seqStart, size_t seqSize, int nbSeq, |
2032 | | const ZSTD_longOffset_e isLongOffset) |
2033 | 0 | { |
2034 | 0 | return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
2035 | 0 | } |
2036 | | #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */ |
2037 | | |
2038 | | #endif /* DYNAMIC_BMI2 */ |
2039 | | |
2040 | | #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG |
2041 | | static size_t |
2042 | | ZSTD_decompressSequences(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize, |
2043 | | const void* seqStart, size_t seqSize, int nbSeq, |
2044 | | const ZSTD_longOffset_e isLongOffset) |
2045 | 441 | { |
2046 | 441 | DEBUGLOG(5, "ZSTD_decompressSequences"); |
2047 | 441 | #if DYNAMIC_BMI2 |
2048 | 441 | if (ZSTD_DCtx_get_bmi2(dctx)) { |
2049 | 441 | return ZSTD_decompressSequences_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
2050 | 441 | } |
2051 | 0 | #endif |
2052 | 0 | return ZSTD_decompressSequences_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
2053 | 441 | } |
2054 | | static size_t |
2055 | | ZSTD_decompressSequencesSplitLitBuffer(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize, |
2056 | | const void* seqStart, size_t seqSize, int nbSeq, |
2057 | | const ZSTD_longOffset_e isLongOffset) |
2058 | 676 | { |
2059 | 676 | DEBUGLOG(5, "ZSTD_decompressSequencesSplitLitBuffer"); |
2060 | 676 | #if DYNAMIC_BMI2 |
2061 | 676 | if (ZSTD_DCtx_get_bmi2(dctx)) { |
2062 | 676 | return ZSTD_decompressSequencesSplitLitBuffer_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
2063 | 676 | } |
2064 | 0 | #endif |
2065 | 0 | return ZSTD_decompressSequencesSplitLitBuffer_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
2066 | 676 | } |
2067 | | #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */ |
2068 | | |
2069 | | |
2070 | | #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT |
2071 | | /* ZSTD_decompressSequencesLong() : |
2072 | | * decompression function triggered when a minimum share of offsets is considered "long", |
2073 | | * aka out of cache. |
2074 | | * note : "long" definition seems overloaded here, sometimes meaning "wider than bitstream register", and sometimes meaning "farther than memory cache distance". |
2075 | | * This function will try to mitigate main memory latency through the use of prefetching */ |
2076 | | static size_t |
2077 | | ZSTD_decompressSequencesLong(ZSTD_DCtx* dctx, |
2078 | | void* dst, size_t maxDstSize, |
2079 | | const void* seqStart, size_t seqSize, int nbSeq, |
2080 | | const ZSTD_longOffset_e isLongOffset) |
2081 | 0 | { |
2082 | 0 | DEBUGLOG(5, "ZSTD_decompressSequencesLong"); |
2083 | 0 | #if DYNAMIC_BMI2 |
2084 | 0 | if (ZSTD_DCtx_get_bmi2(dctx)) { |
2085 | 0 | return ZSTD_decompressSequencesLong_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
2086 | 0 | } |
2087 | 0 | #endif |
2088 | 0 | return ZSTD_decompressSequencesLong_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
2089 | 0 | } |
2090 | | #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */ |
2091 | | |
2092 | | |
2093 | | /** |
2094 | | * @returns The total size of the history referenceable by zstd, including |
2095 | | * both the prefix and the extDict. At @p op any offset larger than this |
2096 | | * is invalid. |
2097 | | */ |
2098 | | static size_t ZSTD_totalHistorySize(void* curPtr, const void* virtualStart) |
2099 | 1.34k | { |
2100 | 1.34k | return (size_t)((char*)curPtr - (const char*)virtualStart); |
2101 | 1.34k | } |
2102 | | |
2103 | | typedef struct { |
2104 | | unsigned longOffsetShare; |
2105 | | unsigned maxNbAdditionalBits; |
2106 | | } ZSTD_OffsetInfo; |
2107 | | |
2108 | | /* ZSTD_getOffsetInfo() : |
2109 | | * condition : offTable must be valid |
2110 | | * @return : "share" of long offsets (arbitrarily defined as > (1<<23)) |
2111 | | * compared to maximum possible of (1<<OffFSELog), |
2112 | | * as well as the maximum number additional bits required. |
2113 | | */ |
2114 | | static ZSTD_OffsetInfo |
2115 | | ZSTD_getOffsetInfo(const ZSTD_seqSymbol* offTable, int nbSeq) |
2116 | 0 | { |
2117 | 0 | ZSTD_OffsetInfo info = {0, 0}; |
2118 | | /* If nbSeq == 0, then the offTable is uninitialized, but we have |
2119 | | * no sequences, so both values should be 0. |
2120 | | */ |
2121 | 0 | if (nbSeq != 0) { |
2122 | 0 | const void* ptr = offTable; |
2123 | 0 | U32 const tableLog = ((const ZSTD_seqSymbol_header*)ptr)[0].tableLog; |
2124 | 0 | const ZSTD_seqSymbol* table = offTable + 1; |
2125 | 0 | U32 const max = 1 << tableLog; |
2126 | 0 | U32 u; |
2127 | 0 | DEBUGLOG(5, "ZSTD_getLongOffsetsShare: (tableLog=%u)", tableLog); |
2128 | |
|
2129 | 0 | assert(max <= (1 << OffFSELog)); /* max not too large */ |
2130 | 0 | for (u=0; u<max; u++) { |
2131 | 0 | info.maxNbAdditionalBits = MAX(info.maxNbAdditionalBits, table[u].nbAdditionalBits); |
2132 | 0 | if (table[u].nbAdditionalBits > 22) info.longOffsetShare += 1; |
2133 | 0 | } |
2134 | |
|
2135 | 0 | assert(tableLog <= OffFSELog); |
2136 | 0 | info.longOffsetShare <<= (OffFSELog - tableLog); /* scale to OffFSELog */ |
2137 | 0 | } |
2138 | |
|
2139 | 0 | return info; |
2140 | 0 | } |
2141 | | |
2142 | | /** |
2143 | | * @returns The maximum offset we can decode in one read of our bitstream, without |
2144 | | * reloading more bits in the middle of the offset bits read. Any offsets larger |
2145 | | * than this must use the long offset decoder. |
2146 | | */ |
2147 | | static size_t ZSTD_maxShortOffset(void) |
2148 | 0 | { |
2149 | 0 | if (MEM_64bits()) { |
2150 | | /* We can decode any offset without reloading bits. |
2151 | | * This might change if the max window size grows. |
2152 | | */ |
2153 | 0 | ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX <= 31); |
2154 | 0 | return (size_t)-1; |
2155 | 0 | } else { |
2156 | | /* The maximum offBase is (1 << (STREAM_ACCUMULATOR_MIN + 1)) - 1. |
2157 | | * This offBase would require STREAM_ACCUMULATOR_MIN extra bits. |
2158 | | * Then we have to subtract ZSTD_REP_NUM to get the maximum possible offset. |
2159 | | */ |
2160 | 0 | size_t const maxOffbase = ((size_t)1 << (STREAM_ACCUMULATOR_MIN + 1)) - 1; |
2161 | 0 | size_t const maxOffset = maxOffbase - ZSTD_REP_NUM; |
2162 | 0 | assert(ZSTD_highbit32((U32)maxOffbase) == STREAM_ACCUMULATOR_MIN); |
2163 | 0 | return maxOffset; |
2164 | 0 | } |
2165 | 0 | } |
2166 | | |
2167 | | size_t |
2168 | | ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx, |
2169 | | void* dst, size_t dstCapacity, |
2170 | | const void* src, size_t srcSize, const streaming_operation streaming) |
2171 | 2.53k | { /* blockType == blockCompressed */ |
2172 | 2.53k | const BYTE* ip = (const BYTE*)src; |
2173 | 2.53k | DEBUGLOG(5, "ZSTD_decompressBlock_internal (cSize : %u)", (unsigned)srcSize); |
2174 | | |
2175 | | /* Note : the wording of the specification |
2176 | | * allows compressed block to be sized exactly ZSTD_blockSizeMax(dctx). |
2177 | | * This generally does not happen, as it makes little sense, |
2178 | | * since an uncompressed block would feature same size and have no decompression cost. |
2179 | | * Also, note that decoder from reference libzstd before < v1.5.4 |
2180 | | * would consider this edge case as an error. |
2181 | | * As a consequence, avoid generating compressed blocks of size ZSTD_blockSizeMax(dctx) |
2182 | | * for broader compatibility with the deployed ecosystem of zstd decoders */ |
2183 | 2.53k | RETURN_ERROR_IF(srcSize > ZSTD_blockSizeMax(dctx), srcSize_wrong, ""); |
2184 | | |
2185 | | /* Decode literals section */ |
2186 | 2.52k | { size_t const litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize, dst, dstCapacity, streaming); |
2187 | 2.52k | DEBUGLOG(5, "ZSTD_decodeLiteralsBlock : cSize=%u, nbLiterals=%zu", (U32)litCSize, dctx->litSize); |
2188 | 2.52k | if (ZSTD_isError(litCSize)) return litCSize; |
2189 | 1.34k | ip += litCSize; |
2190 | 1.34k | srcSize -= litCSize; |
2191 | 1.34k | } |
2192 | | |
2193 | | /* Build Decoding Tables */ |
2194 | 0 | { |
2195 | | /* Compute the maximum block size, which must also work when !frame and fParams are unset. |
2196 | | * Additionally, take the min with dstCapacity to ensure that the totalHistorySize fits in a size_t. |
2197 | | */ |
2198 | 1.34k | size_t const blockSizeMax = MIN(dstCapacity, ZSTD_blockSizeMax(dctx)); |
2199 | 1.34k | size_t const totalHistorySize = ZSTD_totalHistorySize(ZSTD_maybeNullPtrAdd(dst, (ptrdiff_t)blockSizeMax), (BYTE const*)dctx->virtualStart); |
2200 | | /* isLongOffset must be true if there are long offsets. |
2201 | | * Offsets are long if they are larger than ZSTD_maxShortOffset(). |
2202 | | * We don't expect that to be the case in 64-bit mode. |
2203 | | * |
2204 | | * We check here to see if our history is large enough to allow long offsets. |
2205 | | * If it isn't, then we can't possible have (valid) long offsets. If the offset |
2206 | | * is invalid, then it is okay to read it incorrectly. |
2207 | | * |
2208 | | * If isLongOffsets is true, then we will later check our decoding table to see |
2209 | | * if it is even possible to generate long offsets. |
2210 | | */ |
2211 | 1.34k | ZSTD_longOffset_e isLongOffset = (ZSTD_longOffset_e)(MEM_32bits() && (totalHistorySize > ZSTD_maxShortOffset())); |
2212 | | /* These macros control at build-time which decompressor implementation |
2213 | | * we use. If neither is defined, we do some inspection and dispatch at |
2214 | | * runtime. |
2215 | | */ |
2216 | 1.34k | #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \ |
2217 | 1.34k | !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG) |
2218 | 1.34k | int usePrefetchDecoder = dctx->ddictIsCold; |
2219 | | #else |
2220 | | /* Set to 1 to avoid computing offset info if we don't need to. |
2221 | | * Otherwise this value is ignored. |
2222 | | */ |
2223 | | int usePrefetchDecoder = 1; |
2224 | | #endif |
2225 | 1.34k | int nbSeq; |
2226 | 1.34k | size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, srcSize); |
2227 | 1.34k | if (ZSTD_isError(seqHSize)) return seqHSize; |
2228 | 1.12k | ip += seqHSize; |
2229 | 1.12k | srcSize -= seqHSize; |
2230 | | |
2231 | 1.12k | RETURN_ERROR_IF((dst == NULL || dstCapacity == 0) && nbSeq > 0, dstSize_tooSmall, "NULL not handled"); |
2232 | 1.11k | RETURN_ERROR_IF(MEM_64bits() && sizeof(size_t) == sizeof(void*) && (size_t)(-1) - (size_t)dst < (size_t)(1 << 20), dstSize_tooSmall, |
2233 | 1.11k | "invalid dst"); |
2234 | | |
2235 | | /* If we could potentially have long offsets, or we might want to use the prefetch decoder, |
2236 | | * compute information about the share of long offsets, and the maximum nbAdditionalBits. |
2237 | | * NOTE: could probably use a larger nbSeq limit |
2238 | | */ |
2239 | 1.11k | if (isLongOffset || (!usePrefetchDecoder && (totalHistorySize > (1u << 24)) && (nbSeq > 8))) { |
2240 | 0 | ZSTD_OffsetInfo const info = ZSTD_getOffsetInfo(dctx->OFTptr, nbSeq); |
2241 | 0 | if (isLongOffset && info.maxNbAdditionalBits <= STREAM_ACCUMULATOR_MIN) { |
2242 | | /* If isLongOffset, but the maximum number of additional bits that we see in our table is small |
2243 | | * enough, then we know it is impossible to have too long an offset in this block, so we can |
2244 | | * use the regular offset decoder. |
2245 | | */ |
2246 | 0 | isLongOffset = ZSTD_lo_isRegularOffset; |
2247 | 0 | } |
2248 | 0 | if (!usePrefetchDecoder) { |
2249 | 0 | U32 const minShare = MEM_64bits() ? 7 : 20; /* heuristic values, correspond to 2.73% and 7.81% */ |
2250 | 0 | usePrefetchDecoder = (info.longOffsetShare >= minShare); |
2251 | 0 | } |
2252 | 0 | } |
2253 | | |
2254 | 1.11k | dctx->ddictIsCold = 0; |
2255 | | |
2256 | 1.11k | #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \ |
2257 | 1.11k | !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG) |
2258 | 1.11k | if (usePrefetchDecoder) { |
2259 | | #else |
2260 | | (void)usePrefetchDecoder; |
2261 | | { |
2262 | | #endif |
2263 | 0 | #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT |
2264 | 0 | return ZSTD_decompressSequencesLong(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset); |
2265 | 0 | #endif |
2266 | 0 | } |
2267 | | |
2268 | 1.11k | #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG |
2269 | | /* else */ |
2270 | 1.11k | if (dctx->litBufferLocation == ZSTD_split) |
2271 | 676 | return ZSTD_decompressSequencesSplitLitBuffer(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset); |
2272 | 441 | else |
2273 | 441 | return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset); |
2274 | 1.11k | #endif |
2275 | 1.11k | } |
2276 | 1.11k | } |
2277 | | |
2278 | | |
2279 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
2280 | | void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst, size_t dstSize) |
2281 | 46.9k | { |
2282 | 46.9k | if (dst != dctx->previousDstEnd && dstSize > 0) { /* not contiguous */ |
2283 | 5.68k | dctx->dictEnd = dctx->previousDstEnd; |
2284 | 5.68k | dctx->virtualStart = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart)); |
2285 | 5.68k | dctx->prefixStart = dst; |
2286 | 5.68k | dctx->previousDstEnd = dst; |
2287 | 5.68k | } |
2288 | 46.9k | } |
2289 | | |
2290 | | |
2291 | | size_t ZSTD_decompressBlock_deprecated(ZSTD_DCtx* dctx, |
2292 | | void* dst, size_t dstCapacity, |
2293 | | const void* src, size_t srcSize) |
2294 | 0 | { |
2295 | 0 | size_t dSize; |
2296 | 0 | dctx->isFrameDecompression = 0; |
2297 | 0 | ZSTD_checkContinuity(dctx, dst, dstCapacity); |
2298 | 0 | dSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, not_streaming); |
2299 | 0 | FORWARD_IF_ERROR(dSize, ""); |
2300 | 0 | dctx->previousDstEnd = (char*)dst + dSize; |
2301 | 0 | return dSize; |
2302 | 0 | } |
2303 | | |
2304 | | |
2305 | | /* NOTE: Must just wrap ZSTD_decompressBlock_deprecated() */ |
2306 | | size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, |
2307 | | void* dst, size_t dstCapacity, |
2308 | | const void* src, size_t srcSize) |
2309 | 0 | { |
2310 | 0 | return ZSTD_decompressBlock_deprecated(dctx, dst, dstCapacity, src, srcSize); |
2311 | 0 | } |