/src/tdengine/contrib/TSZ/zstd/decompress/zstd_decompress.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
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 | | |
12 | | /* *************************************************************** |
13 | | * Tuning parameters |
14 | | *****************************************************************/ |
15 | | /*! |
16 | | * HEAPMODE : |
17 | | * Select how default decompression function ZSTD_decompress() allocates its context, |
18 | | * on stack (0), or into heap (1, default; requires malloc()). |
19 | | * Note that functions with explicit context such as ZSTD_decompressDCtx() are unaffected. |
20 | | */ |
21 | | #ifndef ZSTD_HEAPMODE |
22 | | # define ZSTD_HEAPMODE 1 |
23 | | #endif |
24 | | |
25 | | /*! |
26 | | * LEGACY_SUPPORT : |
27 | | * if set to 1+, ZSTD_decompress() can decode older formats (v0.1+) |
28 | | */ |
29 | | #ifndef ZSTD_LEGACY_SUPPORT |
30 | | # define ZSTD_LEGACY_SUPPORT 0 |
31 | | #endif |
32 | | |
33 | | /*! |
34 | | * MAXWINDOWSIZE_DEFAULT : |
35 | | * maximum window size accepted by DStream __by default__. |
36 | | * Frames requiring more memory will be rejected. |
37 | | * It's possible to set a different limit using ZSTD_DCtx_setMaxWindowSize(). |
38 | | */ |
39 | | #ifndef ZSTD_MAXWINDOWSIZE_DEFAULT |
40 | 0 | # define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_DEFAULTMAX) + 1) |
41 | | #endif |
42 | | |
43 | | |
44 | | /*! |
45 | | * NO_FORWARD_PROGRESS_MAX : |
46 | | * maximum allowed nb of calls to ZSTD_decompressStream() and ZSTD_decompress_generic() |
47 | | * without any forward progress |
48 | | * (defined as: no byte read from input, and no byte flushed to output) |
49 | | * before triggering an error. |
50 | | */ |
51 | | #ifndef ZSTD_NO_FORWARD_PROGRESS_MAX |
52 | 0 | # define ZSTD_NO_FORWARD_PROGRESS_MAX 16 |
53 | | #endif |
54 | | |
55 | | /*-******************************************************* |
56 | | * Dependencies |
57 | | *********************************************************/ |
58 | | #include <string.h> /* memcpy, memmove, memset */ |
59 | | #include "cpu.h" |
60 | | #include "mem.h" /* low level memory routines */ |
61 | | #define FSE_STATIC_LINKING_ONLY |
62 | | #include "fse.h" |
63 | | #define HUF_STATIC_LINKING_ONLY |
64 | | #include "huf.h" |
65 | | #include "zstd_internal.h" |
66 | | |
67 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
68 | | # include "zstd_legacy.h" |
69 | | #endif |
70 | | |
71 | | |
72 | | /*-************************************* |
73 | | * Errors |
74 | | ***************************************/ |
75 | 0 | #define ZSTD_isError ERR_isError /* for inlining */ |
76 | 0 | #define FSE_isError ERR_isError |
77 | 0 | #define HUF_isError ERR_isError |
78 | | |
79 | | |
80 | | /*_******************************************************* |
81 | | * Memory operations |
82 | | **********************************************************/ |
83 | 0 | static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); } |
84 | | |
85 | | |
86 | | /*-************************************************************* |
87 | | * Context management |
88 | | ***************************************************************/ |
89 | | typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader, |
90 | | ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock, |
91 | | ZSTDds_decompressLastBlock, ZSTDds_checkChecksum, |
92 | | ZSTDds_decodeSkippableHeader, ZSTDds_skipFrame } ZSTD_dStage; |
93 | | |
94 | | typedef enum { zdss_init=0, zdss_loadHeader, |
95 | | zdss_read, zdss_load, zdss_flush } ZSTD_dStreamStage; |
96 | | |
97 | | |
98 | | typedef struct { |
99 | | U32 fastMode; |
100 | | U32 tableLog; |
101 | | } ZSTD_seqSymbol_header; |
102 | | |
103 | | typedef struct { |
104 | | U16 nextState; |
105 | | BYTE nbAdditionalBits; |
106 | | BYTE nbBits; |
107 | | U32 baseValue; |
108 | | } ZSTD_seqSymbol; |
109 | | |
110 | | #define SEQSYMBOL_TABLE_SIZE(log) (1 + (1 << (log))) |
111 | | |
112 | | typedef struct { |
113 | | ZSTD_seqSymbol LLTable[SEQSYMBOL_TABLE_SIZE(LLFSELog)]; |
114 | | ZSTD_seqSymbol OFTable[SEQSYMBOL_TABLE_SIZE(OffFSELog)]; |
115 | | ZSTD_seqSymbol MLTable[SEQSYMBOL_TABLE_SIZE(MLFSELog)]; |
116 | | HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)]; /* can accommodate HUF_decompress4X */ |
117 | | U32 workspace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; |
118 | | U32 rep[ZSTD_REP_NUM]; |
119 | | } ZSTD_entropyDTables_t; |
120 | | |
121 | | struct ZSTD_DCtx_s |
122 | | { |
123 | | const ZSTD_seqSymbol* LLTptr; |
124 | | const ZSTD_seqSymbol* MLTptr; |
125 | | const ZSTD_seqSymbol* OFTptr; |
126 | | const HUF_DTable* HUFptr; |
127 | | ZSTD_entropyDTables_t entropy; |
128 | | const void* previousDstEnd; /* detect continuity */ |
129 | | const void* prefixStart; /* start of current segment */ |
130 | | const void* virtualStart; /* virtual start of previous segment if it was just before current one */ |
131 | | const void* dictEnd; /* end of previous segment */ |
132 | | size_t expected; |
133 | | ZSTD_frameHeader fParams; |
134 | | U64 decodedSize; |
135 | | blockType_e bType; /* used in ZSTD_decompressContinue(), store blockType between block header decoding and block decompression stages */ |
136 | | ZSTD_dStage stage; |
137 | | U32 litEntropy; |
138 | | U32 fseEntropy; |
139 | | XXH64_state_t xxhState; |
140 | | size_t headerSize; |
141 | | U32 dictID; |
142 | | ZSTD_format_e format; |
143 | | const BYTE* litPtr; |
144 | | ZSTD_customMem customMem; |
145 | | size_t litSize; |
146 | | size_t rleSize; |
147 | | size_t staticSize; |
148 | | int bmi2; /* == 1 if the CPU supports BMI2 and 0 otherwise. CPU support is determined dynamically once per context lifetime. */ |
149 | | |
150 | | /* streaming */ |
151 | | ZSTD_DDict* ddictLocal; |
152 | | const ZSTD_DDict* ddict; |
153 | | ZSTD_dStreamStage streamStage; |
154 | | char* inBuff; |
155 | | size_t inBuffSize; |
156 | | size_t inPos; |
157 | | size_t maxWindowSize; |
158 | | char* outBuff; |
159 | | size_t outBuffSize; |
160 | | size_t outStart; |
161 | | size_t outEnd; |
162 | | size_t lhSize; |
163 | | void* legacyContext; |
164 | | U32 previousLegacyVersion; |
165 | | U32 legacyVersion; |
166 | | U32 hostageByte; |
167 | | int noForwardProgress; |
168 | | |
169 | | /* workspace */ |
170 | | BYTE litBuffer[ZSTD_BLOCKSIZE_MAX + WILDCOPY_OVERLENGTH]; |
171 | | BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX]; |
172 | | }; /* typedef'd to ZSTD_DCtx within "zstd.h" */ |
173 | | |
174 | | size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx) |
175 | 0 | { |
176 | 0 | if (dctx==NULL) return 0; /* support sizeof NULL */ |
177 | 0 | return sizeof(*dctx) |
178 | 0 | + ZSTD_sizeof_DDict(dctx->ddictLocal) |
179 | 0 | + dctx->inBuffSize + dctx->outBuffSize; |
180 | 0 | } |
181 | | |
182 | 0 | size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); } |
183 | | |
184 | | |
185 | | static size_t ZSTD_startingInputLength(ZSTD_format_e format) |
186 | 0 | { |
187 | 0 | size_t const startingInputLength = (format==ZSTD_f_zstd1_magicless) ? |
188 | 0 | ZSTD_frameHeaderSize_prefix - ZSTD_frameIdSize : |
189 | 0 | ZSTD_frameHeaderSize_prefix; |
190 | 0 | ZSTD_STATIC_ASSERT(ZSTD_FRAMEHEADERSIZE_PREFIX >= ZSTD_FRAMEIDSIZE); |
191 | | /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */ |
192 | 0 | assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) ); |
193 | 0 | return startingInputLength; |
194 | 0 | } |
195 | | |
196 | | static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx) |
197 | 0 | { |
198 | 0 | dctx->format = ZSTD_f_zstd1; /* ZSTD_decompressBegin() invokes ZSTD_startingInputLength() with argument dctx->format */ |
199 | 0 | dctx->staticSize = 0; |
200 | 0 | dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT; |
201 | 0 | dctx->ddict = NULL; |
202 | 0 | dctx->ddictLocal = NULL; |
203 | 0 | dctx->inBuff = NULL; |
204 | 0 | dctx->inBuffSize = 0; |
205 | 0 | dctx->outBuffSize = 0; |
206 | 0 | dctx->streamStage = zdss_init; |
207 | 0 | dctx->legacyContext = NULL; |
208 | 0 | dctx->previousLegacyVersion = 0; |
209 | 0 | dctx->noForwardProgress = 0; |
210 | 0 | dctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid()); |
211 | 0 | } |
212 | | |
213 | | ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize) |
214 | 0 | { |
215 | 0 | ZSTD_DCtx* const dctx = (ZSTD_DCtx*) workspace; |
216 | |
|
217 | 0 | if ((size_t)workspace & 7) return NULL; /* 8-aligned */ |
218 | 0 | if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL; /* minimum size */ |
219 | | |
220 | 0 | ZSTD_initDCtx_internal(dctx); |
221 | 0 | dctx->staticSize = workspaceSize; |
222 | 0 | dctx->inBuff = (char*)(dctx+1); |
223 | 0 | return dctx; |
224 | 0 | } |
225 | | |
226 | | ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem) |
227 | 0 | { |
228 | 0 | if (!customMem.customAlloc ^ !customMem.customFree) return NULL; |
229 | | |
230 | 0 | { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(*dctx), customMem); |
231 | 0 | if (!dctx) return NULL; |
232 | 0 | dctx->customMem = customMem; |
233 | 0 | ZSTD_initDCtx_internal(dctx); |
234 | 0 | return dctx; |
235 | 0 | } |
236 | 0 | } |
237 | | |
238 | | ZSTD_DCtx* ZSTD_createDCtx(void) |
239 | 0 | { |
240 | 0 | DEBUGLOG(3, "ZSTD_createDCtx"); |
241 | 0 | return ZSTD_createDCtx_advanced(ZSTD_defaultCMem); |
242 | 0 | } |
243 | | |
244 | | size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx) |
245 | 0 | { |
246 | 0 | if (dctx==NULL) return 0; /* support free on NULL */ |
247 | 0 | if (dctx->staticSize) return ERROR(memory_allocation); /* not compatible with static DCtx */ |
248 | 0 | { ZSTD_customMem const cMem = dctx->customMem; |
249 | 0 | ZSTD_freeDDict(dctx->ddictLocal); |
250 | 0 | dctx->ddictLocal = NULL; |
251 | 0 | ZSTD_free(dctx->inBuff, cMem); |
252 | 0 | dctx->inBuff = NULL; |
253 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
254 | | if (dctx->legacyContext) |
255 | | ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion); |
256 | | #endif |
257 | 0 | ZSTD_free(dctx, cMem); |
258 | 0 | return 0; |
259 | 0 | } |
260 | 0 | } |
261 | | |
262 | | /* no longer useful */ |
263 | | void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx) |
264 | 0 | { |
265 | 0 | size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx); |
266 | 0 | memcpy(dstDCtx, srcDCtx, toCopy); /* no need to copy workspace */ |
267 | 0 | } |
268 | | |
269 | | |
270 | | /*-************************************************************* |
271 | | * Frame header decoding |
272 | | ***************************************************************/ |
273 | | |
274 | | /*! ZSTD_isFrame() : |
275 | | * Tells if the content of `buffer` starts with a valid Frame Identifier. |
276 | | * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. |
277 | | * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. |
278 | | * Note 3 : Skippable Frame Identifiers are considered valid. */ |
279 | | unsigned ZSTD_isFrame(const void* buffer, size_t size) |
280 | 0 | { |
281 | 0 | if (size < ZSTD_frameIdSize) return 0; |
282 | 0 | { U32 const magic = MEM_readLE32(buffer); |
283 | 0 | if (magic == ZSTD_MAGICNUMBER) return 1; |
284 | 0 | if ((magic & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) return 1; |
285 | 0 | } |
286 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
287 | | if (ZSTD_isLegacy(buffer, size)) return 1; |
288 | | #endif |
289 | 0 | return 0; |
290 | 0 | } |
291 | | |
292 | | /** ZSTD_frameHeaderSize_internal() : |
293 | | * srcSize must be large enough to reach header size fields. |
294 | | * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless. |
295 | | * @return : size of the Frame Header |
296 | | * or an error code, which can be tested with ZSTD_isError() */ |
297 | | static size_t ZSTD_frameHeaderSize_internal(const void* src, size_t srcSize, ZSTD_format_e format) |
298 | 0 | { |
299 | 0 | size_t const minInputSize = ZSTD_startingInputLength(format); |
300 | 0 | if (srcSize < minInputSize) return ERROR(srcSize_wrong); |
301 | | |
302 | 0 | { BYTE const fhd = ((const BYTE*)src)[minInputSize-1]; |
303 | 0 | U32 const dictID= fhd & 3; |
304 | 0 | U32 const singleSegment = (fhd >> 5) & 1; |
305 | 0 | U32 const fcsId = fhd >> 6; |
306 | 0 | return minInputSize + !singleSegment |
307 | 0 | + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId] |
308 | 0 | + (singleSegment && !fcsId); |
309 | 0 | } |
310 | 0 | } |
311 | | |
312 | | /** ZSTD_frameHeaderSize() : |
313 | | * srcSize must be >= ZSTD_frameHeaderSize_prefix. |
314 | | * @return : size of the Frame Header, |
315 | | * or an error code (if srcSize is too small) */ |
316 | | size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize) |
317 | 0 | { |
318 | 0 | return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1); |
319 | 0 | } |
320 | | |
321 | | |
322 | | /** ZSTD_getFrameHeader_advanced() : |
323 | | * decode Frame Header, or require larger `srcSize`. |
324 | | * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless |
325 | | * @return : 0, `zfhPtr` is correctly filled, |
326 | | * >0, `srcSize` is too small, value is wanted `srcSize` amount, |
327 | | * or an error code, which can be tested using ZSTD_isError() */ |
328 | | size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format) |
329 | 0 | { |
330 | 0 | const BYTE* ip = (const BYTE*)src; |
331 | 0 | size_t const minInputSize = ZSTD_startingInputLength(format); |
332 | |
|
333 | 0 | if (srcSize < minInputSize) return minInputSize; |
334 | | |
335 | 0 | if ( (format != ZSTD_f_zstd1_magicless) |
336 | 0 | && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) { |
337 | 0 | if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { |
338 | | /* skippable frame */ |
339 | 0 | if (srcSize < ZSTD_skippableHeaderSize) |
340 | 0 | return ZSTD_skippableHeaderSize; /* magic number + frame length */ |
341 | 0 | memset(zfhPtr, 0, sizeof(*zfhPtr)); |
342 | 0 | zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_frameIdSize); |
343 | 0 | zfhPtr->frameType = ZSTD_skippableFrame; |
344 | 0 | return 0; |
345 | 0 | } |
346 | 0 | return ERROR(prefix_unknown); |
347 | 0 | } |
348 | | |
349 | | /* ensure there is enough `srcSize` to fully read/decode frame header */ |
350 | 0 | { size_t const fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format); |
351 | 0 | if (srcSize < fhsize) return fhsize; |
352 | 0 | zfhPtr->headerSize = (U32)fhsize; |
353 | 0 | } |
354 | | |
355 | 0 | { BYTE const fhdByte = ip[minInputSize-1]; |
356 | 0 | size_t pos = minInputSize; |
357 | 0 | U32 const dictIDSizeCode = fhdByte&3; |
358 | 0 | U32 const checksumFlag = (fhdByte>>2)&1; |
359 | 0 | U32 const singleSegment = (fhdByte>>5)&1; |
360 | 0 | U32 const fcsID = fhdByte>>6; |
361 | 0 | U64 windowSize = 0; |
362 | 0 | U32 dictID = 0; |
363 | 0 | U64 frameContentSize = ZSTD_CONTENTSIZE_UNKNOWN; |
364 | 0 | if ((fhdByte & 0x08) != 0) |
365 | 0 | return ERROR(frameParameter_unsupported); /* reserved bits, must be zero */ |
366 | | |
367 | 0 | if (!singleSegment) { |
368 | 0 | BYTE const wlByte = ip[pos++]; |
369 | 0 | U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN; |
370 | 0 | if (windowLog > ZSTD_WINDOWLOG_MAX) |
371 | 0 | return ERROR(frameParameter_windowTooLarge); |
372 | 0 | windowSize = (1ULL << windowLog); |
373 | 0 | windowSize += (windowSize >> 3) * (wlByte&7); |
374 | 0 | } |
375 | 0 | switch(dictIDSizeCode) |
376 | 0 | { |
377 | 0 | default: assert(0); /* impossible */ |
378 | 0 | case 0 : break; |
379 | 0 | case 1 : dictID = ip[pos]; pos++; break; |
380 | 0 | case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break; |
381 | 0 | case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break; |
382 | 0 | } |
383 | 0 | switch(fcsID) |
384 | 0 | { |
385 | 0 | default: assert(0); /* impossible */ |
386 | 0 | case 0 : if (singleSegment) frameContentSize = ip[pos]; break; |
387 | 0 | case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break; |
388 | 0 | case 2 : frameContentSize = MEM_readLE32(ip+pos); break; |
389 | 0 | case 3 : frameContentSize = MEM_readLE64(ip+pos); break; |
390 | 0 | } |
391 | 0 | if (singleSegment) windowSize = frameContentSize; |
392 | |
|
393 | 0 | zfhPtr->frameType = ZSTD_frame; |
394 | 0 | zfhPtr->frameContentSize = frameContentSize; |
395 | 0 | zfhPtr->windowSize = windowSize; |
396 | 0 | zfhPtr->blockSizeMax = (unsigned) MIN(windowSize, ZSTD_BLOCKSIZE_MAX); |
397 | 0 | zfhPtr->dictID = dictID; |
398 | 0 | zfhPtr->checksumFlag = checksumFlag; |
399 | 0 | } |
400 | 0 | return 0; |
401 | 0 | } |
402 | | |
403 | | /** ZSTD_getFrameHeader() : |
404 | | * decode Frame Header, or require larger `srcSize`. |
405 | | * note : this function does not consume input, it only reads it. |
406 | | * @return : 0, `zfhPtr` is correctly filled, |
407 | | * >0, `srcSize` is too small, value is wanted `srcSize` amount, |
408 | | * or an error code, which can be tested using ZSTD_isError() */ |
409 | | size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize) |
410 | 0 | { |
411 | 0 | return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_f_zstd1); |
412 | 0 | } |
413 | | |
414 | | |
415 | | /** ZSTD_getFrameContentSize() : |
416 | | * compatible with legacy mode |
417 | | * @return : decompressed size of the single frame pointed to be `src` if known, otherwise |
418 | | * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined |
419 | | * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */ |
420 | | unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize) |
421 | 0 | { |
422 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
423 | | if (ZSTD_isLegacy(src, srcSize)) { |
424 | | unsigned long long const ret = ZSTD_getDecompressedSize_legacy(src, srcSize); |
425 | | return ret == 0 ? ZSTD_CONTENTSIZE_UNKNOWN : ret; |
426 | | } |
427 | | #endif |
428 | 0 | { ZSTD_frameHeader zfh; |
429 | 0 | if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0) |
430 | 0 | return ZSTD_CONTENTSIZE_ERROR; |
431 | 0 | if (zfh.frameType == ZSTD_skippableFrame) { |
432 | 0 | return 0; |
433 | 0 | } else { |
434 | 0 | return zfh.frameContentSize; |
435 | 0 | } } |
436 | 0 | } |
437 | | |
438 | | /** ZSTD_findDecompressedSize() : |
439 | | * compatible with legacy mode |
440 | | * `srcSize` must be the exact length of some number of ZSTD compressed and/or |
441 | | * skippable frames |
442 | | * @return : decompressed size of the frames contained */ |
443 | | unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize) |
444 | 0 | { |
445 | 0 | unsigned long long totalDstSize = 0; |
446 | |
|
447 | 0 | while (srcSize >= ZSTD_frameHeaderSize_prefix) { |
448 | 0 | U32 const magicNumber = MEM_readLE32(src); |
449 | |
|
450 | 0 | if ((magicNumber & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { |
451 | 0 | size_t skippableSize; |
452 | 0 | if (srcSize < ZSTD_skippableHeaderSize) |
453 | 0 | return ERROR(srcSize_wrong); |
454 | 0 | skippableSize = MEM_readLE32((const BYTE *)src + ZSTD_frameIdSize) |
455 | 0 | + ZSTD_skippableHeaderSize; |
456 | 0 | if (srcSize < skippableSize) { |
457 | 0 | return ZSTD_CONTENTSIZE_ERROR; |
458 | 0 | } |
459 | | |
460 | 0 | src = (const BYTE *)src + skippableSize; |
461 | 0 | srcSize -= skippableSize; |
462 | 0 | continue; |
463 | 0 | } |
464 | | |
465 | 0 | { unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize); |
466 | 0 | if (ret >= ZSTD_CONTENTSIZE_ERROR) return ret; |
467 | | |
468 | | /* check for overflow */ |
469 | 0 | if (totalDstSize + ret < totalDstSize) return ZSTD_CONTENTSIZE_ERROR; |
470 | 0 | totalDstSize += ret; |
471 | 0 | } |
472 | 0 | { size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize); |
473 | 0 | if (ZSTD_isError(frameSrcSize)) { |
474 | 0 | return ZSTD_CONTENTSIZE_ERROR; |
475 | 0 | } |
476 | | |
477 | 0 | src = (const BYTE *)src + frameSrcSize; |
478 | 0 | srcSize -= frameSrcSize; |
479 | 0 | } |
480 | 0 | } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */ |
481 | | |
482 | 0 | if (srcSize) return ZSTD_CONTENTSIZE_ERROR; |
483 | | |
484 | 0 | return totalDstSize; |
485 | 0 | } |
486 | | |
487 | | /** ZSTD_getDecompressedSize() : |
488 | | * compatible with legacy mode |
489 | | * @return : decompressed size if known, 0 otherwise |
490 | | note : 0 can mean any of the following : |
491 | | - frame content is empty |
492 | | - decompressed size field is not present in frame header |
493 | | - frame header unknown / not supported |
494 | | - frame header not complete (`srcSize` too small) */ |
495 | | unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize) |
496 | 0 | { |
497 | 0 | unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize); |
498 | 0 | ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR < ZSTD_CONTENTSIZE_UNKNOWN); |
499 | 0 | return (ret >= ZSTD_CONTENTSIZE_ERROR) ? 0 : ret; |
500 | 0 | } |
501 | | |
502 | | |
503 | | /** ZSTD_decodeFrameHeader() : |
504 | | * `headerSize` must be the size provided by ZSTD_frameHeaderSize(). |
505 | | * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */ |
506 | | static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize) |
507 | 0 | { |
508 | 0 | size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format); |
509 | 0 | if (ZSTD_isError(result)) return result; /* invalid header */ |
510 | 0 | if (result>0) return ERROR(srcSize_wrong); /* headerSize too small */ |
511 | 0 | if (dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID)) |
512 | 0 | return ERROR(dictionary_wrong); |
513 | 0 | if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0); |
514 | 0 | return 0; |
515 | 0 | } |
516 | | |
517 | | |
518 | | /*-************************************************************* |
519 | | * Block decoding |
520 | | ***************************************************************/ |
521 | | |
522 | | /*! ZSTD_getcBlockSize() : |
523 | | * Provides the size of compressed block from block header `src` */ |
524 | | size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, |
525 | | blockProperties_t* bpPtr) |
526 | 0 | { |
527 | 0 | if (srcSize < ZSTD_blockHeaderSize) return ERROR(srcSize_wrong); |
528 | 0 | { U32 const cBlockHeader = MEM_readLE24(src); |
529 | 0 | U32 const cSize = cBlockHeader >> 3; |
530 | 0 | bpPtr->lastBlock = cBlockHeader & 1; |
531 | 0 | bpPtr->blockType = (blockType_e)((cBlockHeader >> 1) & 3); |
532 | 0 | bpPtr->origSize = cSize; /* only useful for RLE */ |
533 | 0 | if (bpPtr->blockType == bt_rle) return 1; |
534 | 0 | if (bpPtr->blockType == bt_reserved) return ERROR(corruption_detected); |
535 | 0 | return cSize; |
536 | 0 | } |
537 | 0 | } |
538 | | |
539 | | |
540 | | static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity, |
541 | | const void* src, size_t srcSize) |
542 | 0 | { |
543 | 0 | if (srcSize > dstCapacity) return ERROR(dstSize_tooSmall); |
544 | 0 | memcpy(dst, src, srcSize); |
545 | 0 | return srcSize; |
546 | 0 | } |
547 | | |
548 | | |
549 | | static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity, |
550 | | const void* src, size_t srcSize, |
551 | | size_t regenSize) |
552 | 0 | { |
553 | 0 | if (srcSize != 1) return ERROR(srcSize_wrong); |
554 | 0 | if (regenSize > dstCapacity) return ERROR(dstSize_tooSmall); |
555 | 0 | memset(dst, *(const BYTE*)src, regenSize); |
556 | 0 | return regenSize; |
557 | 0 | } |
558 | | |
559 | | /*! ZSTD_decodeLiteralsBlock() : |
560 | | * @return : nb of bytes read from src (< srcSize ) |
561 | | * note : symbol not declared but exposed for fullbench */ |
562 | | size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx, |
563 | | const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */ |
564 | 0 | { |
565 | 0 | if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected); |
566 | | |
567 | 0 | { const BYTE* const istart = (const BYTE*) src; |
568 | 0 | symbolEncodingType_e const litEncType = (symbolEncodingType_e)(istart[0] & 3); |
569 | |
|
570 | 0 | switch(litEncType) |
571 | 0 | { |
572 | 0 | case set_repeat: |
573 | 0 | if (dctx->litEntropy==0) return ERROR(dictionary_corrupted); |
574 | | /* fall-through */ |
575 | 0 | case set_compressed: |
576 | 0 | if (srcSize < 5) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3 */ |
577 | 0 | { size_t lhSize, litSize, litCSize; |
578 | 0 | U32 singleStream=0; |
579 | 0 | U32 const lhlCode = (istart[0] >> 2) & 3; |
580 | 0 | U32 const lhc = MEM_readLE32(istart); |
581 | 0 | switch(lhlCode) |
582 | 0 | { |
583 | 0 | case 0: case 1: default: /* note : default is impossible, since lhlCode into [0..3] */ |
584 | | /* 2 - 2 - 10 - 10 */ |
585 | 0 | singleStream = !lhlCode; |
586 | 0 | lhSize = 3; |
587 | 0 | litSize = (lhc >> 4) & 0x3FF; |
588 | 0 | litCSize = (lhc >> 14) & 0x3FF; |
589 | 0 | break; |
590 | 0 | case 2: |
591 | | /* 2 - 2 - 14 - 14 */ |
592 | 0 | lhSize = 4; |
593 | 0 | litSize = (lhc >> 4) & 0x3FFF; |
594 | 0 | litCSize = lhc >> 18; |
595 | 0 | break; |
596 | 0 | case 3: |
597 | | /* 2 - 2 - 18 - 18 */ |
598 | 0 | lhSize = 5; |
599 | 0 | litSize = (lhc >> 4) & 0x3FFFF; |
600 | 0 | litCSize = (lhc >> 22) + (istart[4] << 10); |
601 | 0 | break; |
602 | 0 | } |
603 | 0 | if (litSize > ZSTD_BLOCKSIZE_MAX) return ERROR(corruption_detected); |
604 | 0 | if (litCSize + lhSize > srcSize) return ERROR(corruption_detected); |
605 | | |
606 | 0 | if (HUF_isError((litEncType==set_repeat) ? |
607 | 0 | ( singleStream ? |
608 | 0 | HUF_decompress1X_usingDTable_bmi2(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->HUFptr, dctx->bmi2) : |
609 | 0 | HUF_decompress4X_usingDTable_bmi2(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->HUFptr, dctx->bmi2) ) : |
610 | 0 | ( singleStream ? |
611 | 0 | HUF_decompress1X1_DCtx_wksp_bmi2(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize, |
612 | 0 | dctx->entropy.workspace, sizeof(dctx->entropy.workspace), dctx->bmi2) : |
613 | 0 | HUF_decompress4X_hufOnly_wksp_bmi2(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize, |
614 | 0 | dctx->entropy.workspace, sizeof(dctx->entropy.workspace), dctx->bmi2)))) |
615 | 0 | return ERROR(corruption_detected); |
616 | | |
617 | 0 | dctx->litPtr = dctx->litBuffer; |
618 | 0 | dctx->litSize = litSize; |
619 | 0 | dctx->litEntropy = 1; |
620 | 0 | if (litEncType==set_compressed) dctx->HUFptr = dctx->entropy.hufTable; |
621 | 0 | memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH); |
622 | 0 | return litCSize + lhSize; |
623 | 0 | } |
624 | | |
625 | 0 | case set_basic: |
626 | 0 | { size_t litSize, lhSize; |
627 | 0 | U32 const lhlCode = ((istart[0]) >> 2) & 3; |
628 | 0 | switch(lhlCode) |
629 | 0 | { |
630 | 0 | case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */ |
631 | 0 | lhSize = 1; |
632 | 0 | litSize = istart[0] >> 3; |
633 | 0 | break; |
634 | 0 | case 1: |
635 | 0 | lhSize = 2; |
636 | 0 | litSize = MEM_readLE16(istart) >> 4; |
637 | 0 | break; |
638 | 0 | case 3: |
639 | 0 | lhSize = 3; |
640 | 0 | litSize = MEM_readLE24(istart) >> 4; |
641 | 0 | break; |
642 | 0 | } |
643 | | |
644 | 0 | if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */ |
645 | 0 | if (litSize+lhSize > srcSize) return ERROR(corruption_detected); |
646 | 0 | memcpy(dctx->litBuffer, istart+lhSize, litSize); |
647 | 0 | dctx->litPtr = dctx->litBuffer; |
648 | 0 | dctx->litSize = litSize; |
649 | 0 | memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH); |
650 | 0 | return lhSize+litSize; |
651 | 0 | } |
652 | | /* direct reference into compressed stream */ |
653 | 0 | dctx->litPtr = istart+lhSize; |
654 | 0 | dctx->litSize = litSize; |
655 | 0 | return lhSize+litSize; |
656 | 0 | } |
657 | | |
658 | 0 | case set_rle: |
659 | 0 | { U32 const lhlCode = ((istart[0]) >> 2) & 3; |
660 | 0 | size_t litSize, lhSize; |
661 | 0 | switch(lhlCode) |
662 | 0 | { |
663 | 0 | case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */ |
664 | 0 | lhSize = 1; |
665 | 0 | litSize = istart[0] >> 3; |
666 | 0 | break; |
667 | 0 | case 1: |
668 | 0 | lhSize = 2; |
669 | 0 | litSize = MEM_readLE16(istart) >> 4; |
670 | 0 | break; |
671 | 0 | case 3: |
672 | 0 | lhSize = 3; |
673 | 0 | litSize = MEM_readLE24(istart) >> 4; |
674 | 0 | if (srcSize<4) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4 */ |
675 | 0 | break; |
676 | 0 | } |
677 | 0 | if (litSize > ZSTD_BLOCKSIZE_MAX) return ERROR(corruption_detected); |
678 | 0 | memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH); |
679 | 0 | dctx->litPtr = dctx->litBuffer; |
680 | 0 | dctx->litSize = litSize; |
681 | 0 | return lhSize+1; |
682 | 0 | } |
683 | 0 | default: |
684 | 0 | return ERROR(corruption_detected); /* impossible */ |
685 | 0 | } |
686 | 0 | } |
687 | 0 | } |
688 | | |
689 | | /* Default FSE distribution tables. |
690 | | * These are pre-calculated FSE decoding tables using default distributions as defined in specification : |
691 | | * https://github.com/facebook/zstd/blob/master/doc/zstd_compression_format.md#default-distributions |
692 | | * They were generated programmatically with following method : |
693 | | * - start from default distributions, present in /lib/common/zstd_internal.h |
694 | | * - generate tables normally, using ZSTD_buildFSETable() |
695 | | * - printout the content of tables |
696 | | * - pretify output, report below, test with fuzzer to ensure it's correct */ |
697 | | |
698 | | /* Default FSE distribution table for Literal Lengths */ |
699 | | static const ZSTD_seqSymbol LL_defaultDTable[(1<<LL_DEFAULTNORMLOG)+1] = { |
700 | | { 1, 1, 1, LL_DEFAULTNORMLOG}, /* header : fastMode, tableLog */ |
701 | | /* nextState, nbAddBits, nbBits, baseVal */ |
702 | | { 0, 0, 4, 0}, { 16, 0, 4, 0}, |
703 | | { 32, 0, 5, 1}, { 0, 0, 5, 3}, |
704 | | { 0, 0, 5, 4}, { 0, 0, 5, 6}, |
705 | | { 0, 0, 5, 7}, { 0, 0, 5, 9}, |
706 | | { 0, 0, 5, 10}, { 0, 0, 5, 12}, |
707 | | { 0, 0, 6, 14}, { 0, 1, 5, 16}, |
708 | | { 0, 1, 5, 20}, { 0, 1, 5, 22}, |
709 | | { 0, 2, 5, 28}, { 0, 3, 5, 32}, |
710 | | { 0, 4, 5, 48}, { 32, 6, 5, 64}, |
711 | | { 0, 7, 5, 128}, { 0, 8, 6, 256}, |
712 | | { 0, 10, 6, 1024}, { 0, 12, 6, 4096}, |
713 | | { 32, 0, 4, 0}, { 0, 0, 4, 1}, |
714 | | { 0, 0, 5, 2}, { 32, 0, 5, 4}, |
715 | | { 0, 0, 5, 5}, { 32, 0, 5, 7}, |
716 | | { 0, 0, 5, 8}, { 32, 0, 5, 10}, |
717 | | { 0, 0, 5, 11}, { 0, 0, 6, 13}, |
718 | | { 32, 1, 5, 16}, { 0, 1, 5, 18}, |
719 | | { 32, 1, 5, 22}, { 0, 2, 5, 24}, |
720 | | { 32, 3, 5, 32}, { 0, 3, 5, 40}, |
721 | | { 0, 6, 4, 64}, { 16, 6, 4, 64}, |
722 | | { 32, 7, 5, 128}, { 0, 9, 6, 512}, |
723 | | { 0, 11, 6, 2048}, { 48, 0, 4, 0}, |
724 | | { 16, 0, 4, 1}, { 32, 0, 5, 2}, |
725 | | { 32, 0, 5, 3}, { 32, 0, 5, 5}, |
726 | | { 32, 0, 5, 6}, { 32, 0, 5, 8}, |
727 | | { 32, 0, 5, 9}, { 32, 0, 5, 11}, |
728 | | { 32, 0, 5, 12}, { 0, 0, 6, 15}, |
729 | | { 32, 1, 5, 18}, { 32, 1, 5, 20}, |
730 | | { 32, 2, 5, 24}, { 32, 2, 5, 28}, |
731 | | { 32, 3, 5, 40}, { 32, 4, 5, 48}, |
732 | | { 0, 16, 6,65536}, { 0, 15, 6,32768}, |
733 | | { 0, 14, 6,16384}, { 0, 13, 6, 8192}, |
734 | | }; /* LL_defaultDTable */ |
735 | | |
736 | | /* Default FSE distribution table for Offset Codes */ |
737 | | static const ZSTD_seqSymbol OF_defaultDTable[(1<<OF_DEFAULTNORMLOG)+1] = { |
738 | | { 1, 1, 1, OF_DEFAULTNORMLOG}, /* header : fastMode, tableLog */ |
739 | | /* nextState, nbAddBits, nbBits, baseVal */ |
740 | | { 0, 0, 5, 0}, { 0, 6, 4, 61}, |
741 | | { 0, 9, 5, 509}, { 0, 15, 5,32765}, |
742 | | { 0, 21, 5,2097149}, { 0, 3, 5, 5}, |
743 | | { 0, 7, 4, 125}, { 0, 12, 5, 4093}, |
744 | | { 0, 18, 5,262141}, { 0, 23, 5,8388605}, |
745 | | { 0, 5, 5, 29}, { 0, 8, 4, 253}, |
746 | | { 0, 14, 5,16381}, { 0, 20, 5,1048573}, |
747 | | { 0, 2, 5, 1}, { 16, 7, 4, 125}, |
748 | | { 0, 11, 5, 2045}, { 0, 17, 5,131069}, |
749 | | { 0, 22, 5,4194301}, { 0, 4, 5, 13}, |
750 | | { 16, 8, 4, 253}, { 0, 13, 5, 8189}, |
751 | | { 0, 19, 5,524285}, { 0, 1, 5, 1}, |
752 | | { 16, 6, 4, 61}, { 0, 10, 5, 1021}, |
753 | | { 0, 16, 5,65533}, { 0, 28, 5,268435453}, |
754 | | { 0, 27, 5,134217725}, { 0, 26, 5,67108861}, |
755 | | { 0, 25, 5,33554429}, { 0, 24, 5,16777213}, |
756 | | }; /* OF_defaultDTable */ |
757 | | |
758 | | |
759 | | /* Default FSE distribution table for Match Lengths */ |
760 | | static const ZSTD_seqSymbol ML_defaultDTable[(1<<ML_DEFAULTNORMLOG)+1] = { |
761 | | { 1, 1, 1, ML_DEFAULTNORMLOG}, /* header : fastMode, tableLog */ |
762 | | /* nextState, nbAddBits, nbBits, baseVal */ |
763 | | { 0, 0, 6, 3}, { 0, 0, 4, 4}, |
764 | | { 32, 0, 5, 5}, { 0, 0, 5, 6}, |
765 | | { 0, 0, 5, 8}, { 0, 0, 5, 9}, |
766 | | { 0, 0, 5, 11}, { 0, 0, 6, 13}, |
767 | | { 0, 0, 6, 16}, { 0, 0, 6, 19}, |
768 | | { 0, 0, 6, 22}, { 0, 0, 6, 25}, |
769 | | { 0, 0, 6, 28}, { 0, 0, 6, 31}, |
770 | | { 0, 0, 6, 34}, { 0, 1, 6, 37}, |
771 | | { 0, 1, 6, 41}, { 0, 2, 6, 47}, |
772 | | { 0, 3, 6, 59}, { 0, 4, 6, 83}, |
773 | | { 0, 7, 6, 131}, { 0, 9, 6, 515}, |
774 | | { 16, 0, 4, 4}, { 0, 0, 4, 5}, |
775 | | { 32, 0, 5, 6}, { 0, 0, 5, 7}, |
776 | | { 32, 0, 5, 9}, { 0, 0, 5, 10}, |
777 | | { 0, 0, 6, 12}, { 0, 0, 6, 15}, |
778 | | { 0, 0, 6, 18}, { 0, 0, 6, 21}, |
779 | | { 0, 0, 6, 24}, { 0, 0, 6, 27}, |
780 | | { 0, 0, 6, 30}, { 0, 0, 6, 33}, |
781 | | { 0, 1, 6, 35}, { 0, 1, 6, 39}, |
782 | | { 0, 2, 6, 43}, { 0, 3, 6, 51}, |
783 | | { 0, 4, 6, 67}, { 0, 5, 6, 99}, |
784 | | { 0, 8, 6, 259}, { 32, 0, 4, 4}, |
785 | | { 48, 0, 4, 4}, { 16, 0, 4, 5}, |
786 | | { 32, 0, 5, 7}, { 32, 0, 5, 8}, |
787 | | { 32, 0, 5, 10}, { 32, 0, 5, 11}, |
788 | | { 0, 0, 6, 14}, { 0, 0, 6, 17}, |
789 | | { 0, 0, 6, 20}, { 0, 0, 6, 23}, |
790 | | { 0, 0, 6, 26}, { 0, 0, 6, 29}, |
791 | | { 0, 0, 6, 32}, { 0, 16, 6,65539}, |
792 | | { 0, 15, 6,32771}, { 0, 14, 6,16387}, |
793 | | { 0, 13, 6, 8195}, { 0, 12, 6, 4099}, |
794 | | { 0, 11, 6, 2051}, { 0, 10, 6, 1027}, |
795 | | }; /* ML_defaultDTable */ |
796 | | |
797 | | |
798 | | static void ZSTD_buildSeqTable_rle(ZSTD_seqSymbol* dt, U32 baseValue, U32 nbAddBits) |
799 | 0 | { |
800 | 0 | void* ptr = dt; |
801 | 0 | ZSTD_seqSymbol_header* const DTableH = (ZSTD_seqSymbol_header*)ptr; |
802 | 0 | ZSTD_seqSymbol* const cell = dt + 1; |
803 | |
|
804 | 0 | DTableH->tableLog = 0; |
805 | 0 | DTableH->fastMode = 0; |
806 | |
|
807 | 0 | cell->nbBits = 0; |
808 | 0 | cell->nextState = 0; |
809 | 0 | assert(nbAddBits < 255); |
810 | 0 | cell->nbAdditionalBits = (BYTE)nbAddBits; |
811 | 0 | cell->baseValue = baseValue; |
812 | 0 | } |
813 | | |
814 | | |
815 | | /* ZSTD_buildFSETable() : |
816 | | * generate FSE decoding table for one symbol (ll, ml or off) */ |
817 | | static void |
818 | | ZSTD_buildFSETable(ZSTD_seqSymbol* dt, |
819 | | const short* normalizedCounter, unsigned maxSymbolValue, |
820 | | const U32* baseValue, const U32* nbAdditionalBits, |
821 | | unsigned tableLog) |
822 | 0 | { |
823 | 0 | ZSTD_seqSymbol* const tableDecode = dt+1; |
824 | 0 | U16 symbolNext[MaxSeq+1]; |
825 | |
|
826 | 0 | U32 const maxSV1 = maxSymbolValue + 1; |
827 | 0 | U32 const tableSize = 1 << tableLog; |
828 | 0 | U32 highThreshold = tableSize-1; |
829 | | |
830 | | /* Sanity Checks */ |
831 | 0 | assert(maxSymbolValue <= MaxSeq); |
832 | 0 | assert(tableLog <= MaxFSELog); |
833 | | |
834 | | /* Init, lay down lowprob symbols */ |
835 | 0 | { ZSTD_seqSymbol_header DTableH; |
836 | 0 | DTableH.tableLog = tableLog; |
837 | 0 | DTableH.fastMode = 1; |
838 | 0 | { S16 const largeLimit= (S16)(1 << (tableLog-1)); |
839 | 0 | U32 s; |
840 | 0 | for (s=0; s<maxSV1; s++) { |
841 | 0 | if (normalizedCounter[s]==-1) { |
842 | 0 | tableDecode[highThreshold--].baseValue = s; |
843 | 0 | symbolNext[s] = 1; |
844 | 0 | } else { |
845 | 0 | if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0; |
846 | 0 | symbolNext[s] = normalizedCounter[s]; |
847 | 0 | } } } |
848 | 0 | memcpy(dt, &DTableH, sizeof(DTableH)); |
849 | 0 | } |
850 | | |
851 | | /* Spread symbols */ |
852 | 0 | { U32 const tableMask = tableSize-1; |
853 | 0 | U32 const step = FSE_TABLESTEP(tableSize); |
854 | 0 | U32 s, position = 0; |
855 | 0 | for (s=0; s<maxSV1; s++) { |
856 | 0 | int i; |
857 | 0 | for (i=0; i<normalizedCounter[s]; i++) { |
858 | 0 | tableDecode[position].baseValue = s; |
859 | 0 | position = (position + step) & tableMask; |
860 | 0 | while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */ |
861 | 0 | } } |
862 | 0 | assert(position == 0); /* position must reach all cells once, otherwise normalizedCounter is incorrect */ |
863 | 0 | } |
864 | | |
865 | | /* Build Decoding table */ |
866 | 0 | { U32 u; |
867 | 0 | for (u=0; u<tableSize; u++) { |
868 | 0 | U32 const symbol = tableDecode[u].baseValue; |
869 | 0 | U32 const nextState = symbolNext[symbol]++; |
870 | 0 | tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) ); |
871 | 0 | tableDecode[u].nextState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize); |
872 | 0 | assert(nbAdditionalBits[symbol] < 255); |
873 | 0 | tableDecode[u].nbAdditionalBits = (BYTE)nbAdditionalBits[symbol]; |
874 | 0 | tableDecode[u].baseValue = baseValue[symbol]; |
875 | 0 | } } |
876 | 0 | } |
877 | | |
878 | | |
879 | | /*! ZSTD_buildSeqTable() : |
880 | | * @return : nb bytes read from src, |
881 | | * or an error code if it fails */ |
882 | | static size_t ZSTD_buildSeqTable(ZSTD_seqSymbol* DTableSpace, const ZSTD_seqSymbol** DTablePtr, |
883 | | symbolEncodingType_e type, U32 max, U32 maxLog, |
884 | | const void* src, size_t srcSize, |
885 | | const U32* baseValue, const U32* nbAdditionalBits, |
886 | | const ZSTD_seqSymbol* defaultTable, U32 flagRepeatTable) |
887 | 0 | { |
888 | 0 | switch(type) |
889 | 0 | { |
890 | 0 | case set_rle : |
891 | 0 | if (!srcSize) return ERROR(srcSize_wrong); |
892 | 0 | if ( (*(const BYTE*)src) > max) return ERROR(corruption_detected); |
893 | 0 | { U32 const symbol = *(const BYTE*)src; |
894 | 0 | U32 const baseline = baseValue[symbol]; |
895 | 0 | U32 const nbBits = nbAdditionalBits[symbol]; |
896 | 0 | ZSTD_buildSeqTable_rle(DTableSpace, baseline, nbBits); |
897 | 0 | } |
898 | 0 | *DTablePtr = DTableSpace; |
899 | 0 | return 1; |
900 | 0 | case set_basic : |
901 | 0 | *DTablePtr = defaultTable; |
902 | 0 | return 0; |
903 | 0 | case set_repeat: |
904 | 0 | if (!flagRepeatTable) return ERROR(corruption_detected); |
905 | 0 | return 0; |
906 | 0 | case set_compressed : |
907 | 0 | { U32 tableLog; |
908 | 0 | S16 norm[MaxSeq+1]; |
909 | 0 | size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize); |
910 | 0 | if (FSE_isError(headerSize)) return ERROR(corruption_detected); |
911 | 0 | if (tableLog > maxLog) return ERROR(corruption_detected); |
912 | 0 | ZSTD_buildFSETable(DTableSpace, norm, max, baseValue, nbAdditionalBits, tableLog); |
913 | 0 | *DTablePtr = DTableSpace; |
914 | 0 | return headerSize; |
915 | 0 | } |
916 | 0 | default : /* impossible */ |
917 | 0 | assert(0); |
918 | 0 | return ERROR(GENERIC); |
919 | 0 | } |
920 | 0 | } |
921 | | |
922 | | static const U32 LL_base[MaxLL+1] = { |
923 | | 0, 1, 2, 3, 4, 5, 6, 7, |
924 | | 8, 9, 10, 11, 12, 13, 14, 15, |
925 | | 16, 18, 20, 22, 24, 28, 32, 40, |
926 | | 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, |
927 | | 0x2000, 0x4000, 0x8000, 0x10000 }; |
928 | | |
929 | | static const U32 OF_base[MaxOff+1] = { |
930 | | 0, 1, 1, 5, 0xD, 0x1D, 0x3D, 0x7D, |
931 | | 0xFD, 0x1FD, 0x3FD, 0x7FD, 0xFFD, 0x1FFD, 0x3FFD, 0x7FFD, |
932 | | 0xFFFD, 0x1FFFD, 0x3FFFD, 0x7FFFD, 0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD, |
933 | | 0xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD, 0x1FFFFFFD, 0x3FFFFFFD, 0x7FFFFFFD }; |
934 | | |
935 | | static const U32 OF_bits[MaxOff+1] = { |
936 | | 0, 1, 2, 3, 4, 5, 6, 7, |
937 | | 8, 9, 10, 11, 12, 13, 14, 15, |
938 | | 16, 17, 18, 19, 20, 21, 22, 23, |
939 | | 24, 25, 26, 27, 28, 29, 30, 31 }; |
940 | | |
941 | | static const U32 ML_base[MaxML+1] = { |
942 | | 3, 4, 5, 6, 7, 8, 9, 10, |
943 | | 11, 12, 13, 14, 15, 16, 17, 18, |
944 | | 19, 20, 21, 22, 23, 24, 25, 26, |
945 | | 27, 28, 29, 30, 31, 32, 33, 34, |
946 | | 35, 37, 39, 41, 43, 47, 51, 59, |
947 | | 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803, |
948 | | 0x1003, 0x2003, 0x4003, 0x8003, 0x10003 }; |
949 | | |
950 | | |
951 | | size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr, |
952 | | const void* src, size_t srcSize) |
953 | 0 | { |
954 | 0 | const BYTE* const istart = (const BYTE* const)src; |
955 | 0 | const BYTE* const iend = istart + srcSize; |
956 | 0 | const BYTE* ip = istart; |
957 | 0 | DEBUGLOG(5, "ZSTD_decodeSeqHeaders"); |
958 | | |
959 | | /* check */ |
960 | 0 | if (srcSize < MIN_SEQUENCES_SIZE) return ERROR(srcSize_wrong); |
961 | | |
962 | | /* SeqHead */ |
963 | 0 | { int nbSeq = *ip++; |
964 | 0 | if (!nbSeq) { *nbSeqPtr=0; return 1; } |
965 | 0 | if (nbSeq > 0x7F) { |
966 | 0 | if (nbSeq == 0xFF) { |
967 | 0 | if (ip+2 > iend) return ERROR(srcSize_wrong); |
968 | 0 | nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2; |
969 | 0 | } else { |
970 | 0 | if (ip >= iend) return ERROR(srcSize_wrong); |
971 | 0 | nbSeq = ((nbSeq-0x80)<<8) + *ip++; |
972 | 0 | } |
973 | 0 | } |
974 | 0 | *nbSeqPtr = nbSeq; |
975 | 0 | } |
976 | | |
977 | | /* FSE table descriptors */ |
978 | 0 | if (ip+4 > iend) return ERROR(srcSize_wrong); /* minimum possible size */ |
979 | 0 | { symbolEncodingType_e const LLtype = (symbolEncodingType_e)(*ip >> 6); |
980 | 0 | symbolEncodingType_e const OFtype = (symbolEncodingType_e)((*ip >> 4) & 3); |
981 | 0 | symbolEncodingType_e const MLtype = (symbolEncodingType_e)((*ip >> 2) & 3); |
982 | 0 | ip++; |
983 | | |
984 | | /* Build DTables */ |
985 | 0 | { size_t const llhSize = ZSTD_buildSeqTable(dctx->entropy.LLTable, &dctx->LLTptr, |
986 | 0 | LLtype, MaxLL, LLFSELog, |
987 | 0 | ip, iend-ip, |
988 | 0 | LL_base, LL_bits, |
989 | 0 | LL_defaultDTable, dctx->fseEntropy); |
990 | 0 | if (ZSTD_isError(llhSize)) return ERROR(corruption_detected); |
991 | 0 | ip += llhSize; |
992 | 0 | } |
993 | | |
994 | 0 | { size_t const ofhSize = ZSTD_buildSeqTable(dctx->entropy.OFTable, &dctx->OFTptr, |
995 | 0 | OFtype, MaxOff, OffFSELog, |
996 | 0 | ip, iend-ip, |
997 | 0 | OF_base, OF_bits, |
998 | 0 | OF_defaultDTable, dctx->fseEntropy); |
999 | 0 | if (ZSTD_isError(ofhSize)) return ERROR(corruption_detected); |
1000 | 0 | ip += ofhSize; |
1001 | 0 | } |
1002 | | |
1003 | 0 | { size_t const mlhSize = ZSTD_buildSeqTable(dctx->entropy.MLTable, &dctx->MLTptr, |
1004 | 0 | MLtype, MaxML, MLFSELog, |
1005 | 0 | ip, iend-ip, |
1006 | 0 | ML_base, ML_bits, |
1007 | 0 | ML_defaultDTable, dctx->fseEntropy); |
1008 | 0 | if (ZSTD_isError(mlhSize)) return ERROR(corruption_detected); |
1009 | 0 | ip += mlhSize; |
1010 | 0 | } |
1011 | 0 | } |
1012 | | |
1013 | 0 | return ip-istart; |
1014 | 0 | } |
1015 | | |
1016 | | |
1017 | | typedef struct { |
1018 | | size_t litLength; |
1019 | | size_t matchLength; |
1020 | | size_t offset; |
1021 | | const BYTE* match; |
1022 | | } seq_t; |
1023 | | |
1024 | | typedef struct { |
1025 | | size_t state; |
1026 | | const ZSTD_seqSymbol* table; |
1027 | | } ZSTD_fseState; |
1028 | | |
1029 | | typedef struct { |
1030 | | BIT_DStream_t DStream; |
1031 | | ZSTD_fseState stateLL; |
1032 | | ZSTD_fseState stateOffb; |
1033 | | ZSTD_fseState stateML; |
1034 | | size_t prevOffset[ZSTD_REP_NUM]; |
1035 | | const BYTE* prefixStart; |
1036 | | const BYTE* dictEnd; |
1037 | | size_t pos; |
1038 | | } seqState_t; |
1039 | | |
1040 | | |
1041 | | FORCE_NOINLINE |
1042 | | size_t ZSTD_execSequenceLast7(BYTE* op, |
1043 | | BYTE* const oend, seq_t sequence, |
1044 | | const BYTE** litPtr, const BYTE* const litLimit, |
1045 | | const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd) |
1046 | 0 | { |
1047 | 0 | BYTE* const oLitEnd = op + sequence.litLength; |
1048 | 0 | size_t const sequenceLength = sequence.litLength + sequence.matchLength; |
1049 | 0 | BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ |
1050 | 0 | BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH; |
1051 | 0 | const BYTE* const iLitEnd = *litPtr + sequence.litLength; |
1052 | 0 | const BYTE* match = oLitEnd - sequence.offset; |
1053 | | |
1054 | | /* check */ |
1055 | 0 | if (oMatchEnd>oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */ |
1056 | 0 | if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */ |
1057 | 0 | if (oLitEnd <= oend_w) return ERROR(GENERIC); /* Precondition */ |
1058 | | |
1059 | | /* copy literals */ |
1060 | 0 | if (op < oend_w) { |
1061 | 0 | ZSTD_wildcopy(op, *litPtr, oend_w - op); |
1062 | 0 | *litPtr += oend_w - op; |
1063 | 0 | op = oend_w; |
1064 | 0 | } |
1065 | 0 | while (op < oLitEnd) *op++ = *(*litPtr)++; |
1066 | | |
1067 | | /* copy Match */ |
1068 | 0 | if (sequence.offset > (size_t)(oLitEnd - base)) { |
1069 | | /* offset beyond prefix */ |
1070 | 0 | if (sequence.offset > (size_t)(oLitEnd - vBase)) return ERROR(corruption_detected); |
1071 | 0 | match = dictEnd - (base-match); |
1072 | 0 | if (match + sequence.matchLength <= dictEnd) { |
1073 | 0 | memmove(oLitEnd, match, sequence.matchLength); |
1074 | 0 | return sequenceLength; |
1075 | 0 | } |
1076 | | /* span extDict & currentPrefixSegment */ |
1077 | 0 | { size_t const length1 = dictEnd - match; |
1078 | 0 | memmove(oLitEnd, match, length1); |
1079 | 0 | op = oLitEnd + length1; |
1080 | 0 | sequence.matchLength -= length1; |
1081 | 0 | match = base; |
1082 | 0 | } } |
1083 | 0 | while (op < oMatchEnd) *op++ = *match++; |
1084 | 0 | return sequenceLength; |
1085 | 0 | } |
1086 | | |
1087 | | |
1088 | | HINT_INLINE |
1089 | | size_t ZSTD_execSequence(BYTE* op, |
1090 | | BYTE* const oend, seq_t sequence, |
1091 | | const BYTE** litPtr, const BYTE* const litLimit, |
1092 | | const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd) |
1093 | 0 | { |
1094 | 0 | BYTE* const oLitEnd = op + sequence.litLength; |
1095 | 0 | size_t const sequenceLength = sequence.litLength + sequence.matchLength; |
1096 | 0 | BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ |
1097 | 0 | BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH; |
1098 | 0 | const BYTE* const iLitEnd = *litPtr + sequence.litLength; |
1099 | 0 | const BYTE* match = oLitEnd - sequence.offset; |
1100 | | |
1101 | | /* check */ |
1102 | 0 | if (oMatchEnd>oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */ |
1103 | 0 | if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */ |
1104 | 0 | if (oLitEnd>oend_w) return ZSTD_execSequenceLast7(op, oend, sequence, litPtr, litLimit, prefixStart, virtualStart, dictEnd); |
1105 | | |
1106 | | /* copy Literals */ |
1107 | 0 | ZSTD_copy8(op, *litPtr); |
1108 | 0 | if (sequence.litLength > 8) |
1109 | 0 | ZSTD_wildcopy(op+8, (*litPtr)+8, sequence.litLength - 8); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */ |
1110 | 0 | op = oLitEnd; |
1111 | 0 | *litPtr = iLitEnd; /* update for next sequence */ |
1112 | | |
1113 | | /* copy Match */ |
1114 | 0 | if (sequence.offset > (size_t)(oLitEnd - prefixStart)) { |
1115 | | /* offset beyond prefix -> go into extDict */ |
1116 | 0 | if (sequence.offset > (size_t)(oLitEnd - virtualStart)) |
1117 | 0 | return ERROR(corruption_detected); |
1118 | 0 | match = dictEnd + (match - prefixStart); |
1119 | 0 | if (match + sequence.matchLength <= dictEnd) { |
1120 | 0 | memmove(oLitEnd, match, sequence.matchLength); |
1121 | 0 | return sequenceLength; |
1122 | 0 | } |
1123 | | /* span extDict & currentPrefixSegment */ |
1124 | 0 | { size_t const length1 = dictEnd - match; |
1125 | 0 | memmove(oLitEnd, match, length1); |
1126 | 0 | op = oLitEnd + length1; |
1127 | 0 | sequence.matchLength -= length1; |
1128 | 0 | match = prefixStart; |
1129 | 0 | if (op > oend_w || sequence.matchLength < MINMATCH) { |
1130 | 0 | U32 i; |
1131 | 0 | for (i = 0; i < sequence.matchLength; ++i) op[i] = match[i]; |
1132 | 0 | return sequenceLength; |
1133 | 0 | } |
1134 | 0 | } } |
1135 | | /* Requirement: op <= oend_w && sequence.matchLength >= MINMATCH */ |
1136 | | |
1137 | | /* match within prefix */ |
1138 | 0 | if (sequence.offset < 8) { |
1139 | | /* close range match, overlap */ |
1140 | 0 | static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */ |
1141 | 0 | static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */ |
1142 | 0 | int const sub2 = dec64table[sequence.offset]; |
1143 | 0 | op[0] = match[0]; |
1144 | 0 | op[1] = match[1]; |
1145 | 0 | op[2] = match[2]; |
1146 | 0 | op[3] = match[3]; |
1147 | 0 | match += dec32table[sequence.offset]; |
1148 | 0 | ZSTD_copy4(op+4, match); |
1149 | 0 | match -= sub2; |
1150 | 0 | } else { |
1151 | 0 | ZSTD_copy8(op, match); |
1152 | 0 | } |
1153 | 0 | op += 8; match += 8; |
1154 | |
|
1155 | 0 | if (oMatchEnd > oend-(16-MINMATCH)) { |
1156 | 0 | if (op < oend_w) { |
1157 | 0 | ZSTD_wildcopy(op, match, oend_w - op); |
1158 | 0 | match += oend_w - op; |
1159 | 0 | op = oend_w; |
1160 | 0 | } |
1161 | 0 | while (op < oMatchEnd) *op++ = *match++; |
1162 | 0 | } else { |
1163 | 0 | ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */ |
1164 | 0 | } |
1165 | 0 | return sequenceLength; |
1166 | 0 | } |
1167 | | |
1168 | | |
1169 | | HINT_INLINE |
1170 | | size_t ZSTD_execSequenceLong(BYTE* op, |
1171 | | BYTE* const oend, seq_t sequence, |
1172 | | const BYTE** litPtr, const BYTE* const litLimit, |
1173 | | const BYTE* const prefixStart, const BYTE* const dictStart, const BYTE* const dictEnd) |
1174 | 0 | { |
1175 | 0 | BYTE* const oLitEnd = op + sequence.litLength; |
1176 | 0 | size_t const sequenceLength = sequence.litLength + sequence.matchLength; |
1177 | 0 | BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ |
1178 | 0 | BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH; |
1179 | 0 | const BYTE* const iLitEnd = *litPtr + sequence.litLength; |
1180 | 0 | const BYTE* match = sequence.match; |
1181 | | |
1182 | | /* check */ |
1183 | 0 | if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */ |
1184 | 0 | if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */ |
1185 | 0 | if (oLitEnd > oend_w) return ZSTD_execSequenceLast7(op, oend, sequence, litPtr, litLimit, prefixStart, dictStart, dictEnd); |
1186 | | |
1187 | | /* copy Literals */ |
1188 | 0 | ZSTD_copy8(op, *litPtr); /* note : op <= oLitEnd <= oend_w == oend - 8 */ |
1189 | 0 | if (sequence.litLength > 8) |
1190 | 0 | ZSTD_wildcopy(op+8, (*litPtr)+8, sequence.litLength - 8); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */ |
1191 | 0 | op = oLitEnd; |
1192 | 0 | *litPtr = iLitEnd; /* update for next sequence */ |
1193 | | |
1194 | | /* copy Match */ |
1195 | 0 | if (sequence.offset > (size_t)(oLitEnd - prefixStart)) { |
1196 | | /* offset beyond prefix */ |
1197 | 0 | if (sequence.offset > (size_t)(oLitEnd - dictStart)) return ERROR(corruption_detected); |
1198 | 0 | if (match + sequence.matchLength <= dictEnd) { |
1199 | 0 | memmove(oLitEnd, match, sequence.matchLength); |
1200 | 0 | return sequenceLength; |
1201 | 0 | } |
1202 | | /* span extDict & currentPrefixSegment */ |
1203 | 0 | { size_t const length1 = dictEnd - match; |
1204 | 0 | memmove(oLitEnd, match, length1); |
1205 | 0 | op = oLitEnd + length1; |
1206 | 0 | sequence.matchLength -= length1; |
1207 | 0 | match = prefixStart; |
1208 | 0 | if (op > oend_w || sequence.matchLength < MINMATCH) { |
1209 | 0 | U32 i; |
1210 | 0 | for (i = 0; i < sequence.matchLength; ++i) op[i] = match[i]; |
1211 | 0 | return sequenceLength; |
1212 | 0 | } |
1213 | 0 | } } |
1214 | 0 | assert(op <= oend_w); |
1215 | 0 | assert(sequence.matchLength >= MINMATCH); |
1216 | | |
1217 | | /* match within prefix */ |
1218 | 0 | if (sequence.offset < 8) { |
1219 | | /* close range match, overlap */ |
1220 | 0 | static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */ |
1221 | 0 | static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */ |
1222 | 0 | int const sub2 = dec64table[sequence.offset]; |
1223 | 0 | op[0] = match[0]; |
1224 | 0 | op[1] = match[1]; |
1225 | 0 | op[2] = match[2]; |
1226 | 0 | op[3] = match[3]; |
1227 | 0 | match += dec32table[sequence.offset]; |
1228 | 0 | ZSTD_copy4(op+4, match); |
1229 | 0 | match -= sub2; |
1230 | 0 | } else { |
1231 | 0 | ZSTD_copy8(op, match); |
1232 | 0 | } |
1233 | 0 | op += 8; match += 8; |
1234 | |
|
1235 | 0 | if (oMatchEnd > oend-(16-MINMATCH)) { |
1236 | 0 | if (op < oend_w) { |
1237 | 0 | ZSTD_wildcopy(op, match, oend_w - op); |
1238 | 0 | match += oend_w - op; |
1239 | 0 | op = oend_w; |
1240 | 0 | } |
1241 | 0 | while (op < oMatchEnd) *op++ = *match++; |
1242 | 0 | } else { |
1243 | 0 | ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */ |
1244 | 0 | } |
1245 | 0 | return sequenceLength; |
1246 | 0 | } |
1247 | | |
1248 | | static void |
1249 | | ZSTD_initFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, const ZSTD_seqSymbol* dt) |
1250 | 0 | { |
1251 | 0 | const void* ptr = dt; |
1252 | 0 | const ZSTD_seqSymbol_header* const DTableH = (const ZSTD_seqSymbol_header*)ptr; |
1253 | 0 | DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog); |
1254 | 0 | DEBUGLOG(6, "ZSTD_initFseState : val=%u using %u bits", |
1255 | 0 | (U32)DStatePtr->state, DTableH->tableLog); |
1256 | 0 | BIT_reloadDStream(bitD); |
1257 | 0 | DStatePtr->table = dt + 1; |
1258 | 0 | } |
1259 | | |
1260 | | FORCE_INLINE_TEMPLATE void |
1261 | | ZSTD_updateFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD) |
1262 | 0 | { |
1263 | 0 | ZSTD_seqSymbol const DInfo = DStatePtr->table[DStatePtr->state]; |
1264 | 0 | U32 const nbBits = DInfo.nbBits; |
1265 | 0 | size_t const lowBits = BIT_readBits(bitD, nbBits); |
1266 | 0 | DStatePtr->state = DInfo.nextState + lowBits; |
1267 | 0 | } |
1268 | | |
1269 | | /* We need to add at most (ZSTD_WINDOWLOG_MAX_32 - 1) bits to read the maximum |
1270 | | * offset bits. But we can only read at most (STREAM_ACCUMULATOR_MIN_32 - 1) |
1271 | | * bits before reloading. This value is the maximum number of bytes we read |
1272 | | * after reloading when we are decoding long offets. |
1273 | | */ |
1274 | | #define LONG_OFFSETS_MAX_EXTRA_BITS_32 \ |
1275 | 0 | (ZSTD_WINDOWLOG_MAX_32 > STREAM_ACCUMULATOR_MIN_32 \ |
1276 | 0 | ? ZSTD_WINDOWLOG_MAX_32 - STREAM_ACCUMULATOR_MIN_32 \ |
1277 | 0 | : 0) |
1278 | | |
1279 | | typedef enum { ZSTD_lo_isRegularOffset, ZSTD_lo_isLongOffset=1 } ZSTD_longOffset_e; |
1280 | | |
1281 | | FORCE_INLINE_TEMPLATE seq_t |
1282 | | ZSTD_decodeSequence(seqState_t* seqState, const ZSTD_longOffset_e longOffsets) |
1283 | 0 | { |
1284 | 0 | seq_t seq; |
1285 | 0 | U32 const llBits = seqState->stateLL.table[seqState->stateLL.state].nbAdditionalBits; |
1286 | 0 | U32 const mlBits = seqState->stateML.table[seqState->stateML.state].nbAdditionalBits; |
1287 | 0 | U32 const ofBits = seqState->stateOffb.table[seqState->stateOffb.state].nbAdditionalBits; |
1288 | 0 | U32 const totalBits = llBits+mlBits+ofBits; |
1289 | 0 | U32 const llBase = seqState->stateLL.table[seqState->stateLL.state].baseValue; |
1290 | 0 | U32 const mlBase = seqState->stateML.table[seqState->stateML.state].baseValue; |
1291 | 0 | U32 const ofBase = seqState->stateOffb.table[seqState->stateOffb.state].baseValue; |
1292 | | |
1293 | | /* sequence */ |
1294 | 0 | { size_t offset; |
1295 | 0 | if (!ofBits) |
1296 | 0 | offset = 0; |
1297 | 0 | else { |
1298 | 0 | ZSTD_STATIC_ASSERT(ZSTD_lo_isLongOffset == 1); |
1299 | 0 | ZSTD_STATIC_ASSERT(LONG_OFFSETS_MAX_EXTRA_BITS_32 == 5); |
1300 | 0 | assert(ofBits <= MaxOff); |
1301 | 0 | if (MEM_32bits() && longOffsets && (ofBits >= STREAM_ACCUMULATOR_MIN_32)) { |
1302 | 0 | U32 const extraBits = ofBits - MIN(ofBits, 32 - seqState->DStream.bitsConsumed); |
1303 | 0 | offset = ofBase + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << extraBits); |
1304 | 0 | BIT_reloadDStream(&seqState->DStream); |
1305 | 0 | if (extraBits) offset += BIT_readBitsFast(&seqState->DStream, extraBits); |
1306 | 0 | assert(extraBits <= LONG_OFFSETS_MAX_EXTRA_BITS_32); /* to avoid another reload */ |
1307 | 0 | } else { |
1308 | 0 | offset = ofBase + BIT_readBitsFast(&seqState->DStream, ofBits/*>0*/); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */ |
1309 | 0 | if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); |
1310 | 0 | } |
1311 | 0 | } |
1312 | |
|
1313 | 0 | if (ofBits <= 1) { |
1314 | 0 | offset += (llBase==0); |
1315 | 0 | if (offset) { |
1316 | 0 | size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset]; |
1317 | 0 | temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */ |
1318 | 0 | if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1]; |
1319 | 0 | seqState->prevOffset[1] = seqState->prevOffset[0]; |
1320 | 0 | seqState->prevOffset[0] = offset = temp; |
1321 | 0 | } else { /* offset == 0 */ |
1322 | 0 | offset = seqState->prevOffset[0]; |
1323 | 0 | } |
1324 | 0 | } else { |
1325 | 0 | seqState->prevOffset[2] = seqState->prevOffset[1]; |
1326 | 0 | seqState->prevOffset[1] = seqState->prevOffset[0]; |
1327 | 0 | seqState->prevOffset[0] = offset; |
1328 | 0 | } |
1329 | 0 | seq.offset = offset; |
1330 | 0 | } |
1331 | |
|
1332 | 0 | seq.matchLength = mlBase |
1333 | 0 | + ((mlBits>0) ? BIT_readBitsFast(&seqState->DStream, mlBits/*>0*/) : 0); /* <= 16 bits */ |
1334 | 0 | if (MEM_32bits() && (mlBits+llBits >= STREAM_ACCUMULATOR_MIN_32-LONG_OFFSETS_MAX_EXTRA_BITS_32)) |
1335 | 0 | BIT_reloadDStream(&seqState->DStream); |
1336 | 0 | if (MEM_64bits() && (totalBits >= STREAM_ACCUMULATOR_MIN_64-(LLFSELog+MLFSELog+OffFSELog))) |
1337 | 0 | BIT_reloadDStream(&seqState->DStream); |
1338 | | /* Ensure there are enough bits to read the rest of data in 64-bit mode. */ |
1339 | 0 | ZSTD_STATIC_ASSERT(16+LLFSELog+MLFSELog+OffFSELog < STREAM_ACCUMULATOR_MIN_64); |
1340 | |
|
1341 | 0 | seq.litLength = llBase |
1342 | 0 | + ((llBits>0) ? BIT_readBitsFast(&seqState->DStream, llBits/*>0*/) : 0); /* <= 16 bits */ |
1343 | 0 | if (MEM_32bits()) |
1344 | 0 | BIT_reloadDStream(&seqState->DStream); |
1345 | |
|
1346 | 0 | DEBUGLOG(6, "seq: litL=%u, matchL=%u, offset=%u", |
1347 | 0 | (U32)seq.litLength, (U32)seq.matchLength, (U32)seq.offset); |
1348 | | |
1349 | | /* ANS state update */ |
1350 | 0 | ZSTD_updateFseState(&seqState->stateLL, &seqState->DStream); /* <= 9 bits */ |
1351 | 0 | ZSTD_updateFseState(&seqState->stateML, &seqState->DStream); /* <= 9 bits */ |
1352 | 0 | if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */ |
1353 | 0 | ZSTD_updateFseState(&seqState->stateOffb, &seqState->DStream); /* <= 8 bits */ |
1354 | |
|
1355 | 0 | return seq; |
1356 | 0 | } |
1357 | | |
1358 | | FORCE_INLINE_TEMPLATE size_t |
1359 | | ZSTD_decompressSequences_body( ZSTD_DCtx* dctx, |
1360 | | void* dst, size_t maxDstSize, |
1361 | | const void* seqStart, size_t seqSize, int nbSeq, |
1362 | | const ZSTD_longOffset_e isLongOffset) |
1363 | 0 | { |
1364 | 0 | const BYTE* ip = (const BYTE*)seqStart; |
1365 | 0 | const BYTE* const iend = ip + seqSize; |
1366 | 0 | BYTE* const ostart = (BYTE* const)dst; |
1367 | 0 | BYTE* const oend = ostart + maxDstSize; |
1368 | 0 | BYTE* op = ostart; |
1369 | 0 | const BYTE* litPtr = dctx->litPtr; |
1370 | 0 | const BYTE* const litEnd = litPtr + dctx->litSize; |
1371 | 0 | const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart); |
1372 | 0 | const BYTE* const vBase = (const BYTE*) (dctx->virtualStart); |
1373 | 0 | const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); |
1374 | 0 | DEBUGLOG(5, "ZSTD_decompressSequences_body"); |
1375 | | |
1376 | | /* Regen sequences */ |
1377 | 0 | if (nbSeq) { |
1378 | 0 | seqState_t seqState; |
1379 | 0 | dctx->fseEntropy = 1; |
1380 | 0 | { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; } |
1381 | 0 | CHECK_E(BIT_initDStream(&seqState.DStream, ip, iend-ip), corruption_detected); |
1382 | 0 | ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr); |
1383 | 0 | ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr); |
1384 | 0 | ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr); |
1385 | |
|
1386 | 0 | for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && nbSeq ; ) { |
1387 | 0 | nbSeq--; |
1388 | 0 | { seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset); |
1389 | 0 | size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, prefixStart, vBase, dictEnd); |
1390 | 0 | DEBUGLOG(6, "regenerated sequence size : %u", (U32)oneSeqSize); |
1391 | 0 | if (ZSTD_isError(oneSeqSize)) return oneSeqSize; |
1392 | 0 | op += oneSeqSize; |
1393 | 0 | } } |
1394 | | |
1395 | | /* check if reached exact end */ |
1396 | 0 | DEBUGLOG(5, "ZSTD_decompressSequences_body: after decode loop, remaining nbSeq : %i", nbSeq); |
1397 | 0 | if (nbSeq) return ERROR(corruption_detected); |
1398 | | /* save reps for next block */ |
1399 | 0 | { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); } |
1400 | 0 | } |
1401 | | |
1402 | | /* last literal segment */ |
1403 | 0 | { size_t const lastLLSize = litEnd - litPtr; |
1404 | 0 | if (lastLLSize > (size_t)(oend-op)) return ERROR(dstSize_tooSmall); |
1405 | 0 | memcpy(op, litPtr, lastLLSize); |
1406 | 0 | op += lastLLSize; |
1407 | 0 | } |
1408 | | |
1409 | 0 | return op-ostart; |
1410 | 0 | } |
1411 | | |
1412 | | static size_t |
1413 | | ZSTD_decompressSequences_default(ZSTD_DCtx* dctx, |
1414 | | void* dst, size_t maxDstSize, |
1415 | | const void* seqStart, size_t seqSize, int nbSeq, |
1416 | | const ZSTD_longOffset_e isLongOffset) |
1417 | 0 | { |
1418 | 0 | return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
1419 | 0 | } |
1420 | | |
1421 | | |
1422 | | |
1423 | | FORCE_INLINE_TEMPLATE seq_t |
1424 | | ZSTD_decodeSequenceLong(seqState_t* seqState, ZSTD_longOffset_e const longOffsets) |
1425 | 0 | { |
1426 | 0 | seq_t seq; |
1427 | 0 | U32 const llBits = seqState->stateLL.table[seqState->stateLL.state].nbAdditionalBits; |
1428 | 0 | U32 const mlBits = seqState->stateML.table[seqState->stateML.state].nbAdditionalBits; |
1429 | 0 | U32 const ofBits = seqState->stateOffb.table[seqState->stateOffb.state].nbAdditionalBits; |
1430 | 0 | U32 const totalBits = llBits+mlBits+ofBits; |
1431 | 0 | U32 const llBase = seqState->stateLL.table[seqState->stateLL.state].baseValue; |
1432 | 0 | U32 const mlBase = seqState->stateML.table[seqState->stateML.state].baseValue; |
1433 | 0 | U32 const ofBase = seqState->stateOffb.table[seqState->stateOffb.state].baseValue; |
1434 | | |
1435 | | /* sequence */ |
1436 | 0 | { size_t offset; |
1437 | 0 | if (!ofBits) |
1438 | 0 | offset = 0; |
1439 | 0 | else { |
1440 | 0 | ZSTD_STATIC_ASSERT(ZSTD_lo_isLongOffset == 1); |
1441 | 0 | ZSTD_STATIC_ASSERT(LONG_OFFSETS_MAX_EXTRA_BITS_32 == 5); |
1442 | 0 | assert(ofBits <= MaxOff); |
1443 | 0 | if (MEM_32bits() && longOffsets) { |
1444 | 0 | U32 const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN_32-1); |
1445 | 0 | offset = ofBase + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << extraBits); |
1446 | 0 | if (MEM_32bits() || extraBits) BIT_reloadDStream(&seqState->DStream); |
1447 | 0 | if (extraBits) offset += BIT_readBitsFast(&seqState->DStream, extraBits); |
1448 | 0 | } else { |
1449 | 0 | offset = ofBase + BIT_readBitsFast(&seqState->DStream, ofBits); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */ |
1450 | 0 | if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); |
1451 | 0 | } |
1452 | 0 | } |
1453 | |
|
1454 | 0 | if (ofBits <= 1) { |
1455 | 0 | offset += (llBase==0); |
1456 | 0 | if (offset) { |
1457 | 0 | size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset]; |
1458 | 0 | temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */ |
1459 | 0 | if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1]; |
1460 | 0 | seqState->prevOffset[1] = seqState->prevOffset[0]; |
1461 | 0 | seqState->prevOffset[0] = offset = temp; |
1462 | 0 | } else { |
1463 | 0 | offset = seqState->prevOffset[0]; |
1464 | 0 | } |
1465 | 0 | } else { |
1466 | 0 | seqState->prevOffset[2] = seqState->prevOffset[1]; |
1467 | 0 | seqState->prevOffset[1] = seqState->prevOffset[0]; |
1468 | 0 | seqState->prevOffset[0] = offset; |
1469 | 0 | } |
1470 | 0 | seq.offset = offset; |
1471 | 0 | } |
1472 | |
|
1473 | 0 | seq.matchLength = mlBase + ((mlBits>0) ? BIT_readBitsFast(&seqState->DStream, mlBits) : 0); /* <= 16 bits */ |
1474 | 0 | if (MEM_32bits() && (mlBits+llBits >= STREAM_ACCUMULATOR_MIN_32-LONG_OFFSETS_MAX_EXTRA_BITS_32)) |
1475 | 0 | BIT_reloadDStream(&seqState->DStream); |
1476 | 0 | if (MEM_64bits() && (totalBits >= STREAM_ACCUMULATOR_MIN_64-(LLFSELog+MLFSELog+OffFSELog))) |
1477 | 0 | BIT_reloadDStream(&seqState->DStream); |
1478 | | /* Verify that there is enough bits to read the rest of the data in 64-bit mode. */ |
1479 | 0 | ZSTD_STATIC_ASSERT(16+LLFSELog+MLFSELog+OffFSELog < STREAM_ACCUMULATOR_MIN_64); |
1480 | |
|
1481 | 0 | seq.litLength = llBase + ((llBits>0) ? BIT_readBitsFast(&seqState->DStream, llBits) : 0); /* <= 16 bits */ |
1482 | 0 | if (MEM_32bits()) |
1483 | 0 | BIT_reloadDStream(&seqState->DStream); |
1484 | |
|
1485 | 0 | { size_t const pos = seqState->pos + seq.litLength; |
1486 | 0 | const BYTE* const matchBase = (seq.offset > pos) ? seqState->dictEnd : seqState->prefixStart; |
1487 | 0 | seq.match = matchBase + pos - seq.offset; /* note : this operation can overflow when seq.offset is really too large, which can only happen when input is corrupted. |
1488 | | * No consequence though : no memory access will occur, overly large offset will be detected in ZSTD_execSequenceLong() */ |
1489 | 0 | seqState->pos = pos + seq.matchLength; |
1490 | 0 | } |
1491 | | |
1492 | | /* ANS state update */ |
1493 | 0 | ZSTD_updateFseState(&seqState->stateLL, &seqState->DStream); /* <= 9 bits */ |
1494 | 0 | ZSTD_updateFseState(&seqState->stateML, &seqState->DStream); /* <= 9 bits */ |
1495 | 0 | if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */ |
1496 | 0 | ZSTD_updateFseState(&seqState->stateOffb, &seqState->DStream); /* <= 8 bits */ |
1497 | |
|
1498 | 0 | return seq; |
1499 | 0 | } |
1500 | | |
1501 | | FORCE_INLINE_TEMPLATE size_t |
1502 | | ZSTD_decompressSequencesLong_body( |
1503 | | ZSTD_DCtx* dctx, |
1504 | | void* dst, size_t maxDstSize, |
1505 | | const void* seqStart, size_t seqSize, int nbSeq, |
1506 | | const ZSTD_longOffset_e isLongOffset) |
1507 | 0 | { |
1508 | 0 | const BYTE* ip = (const BYTE*)seqStart; |
1509 | 0 | const BYTE* const iend = ip + seqSize; |
1510 | 0 | BYTE* const ostart = (BYTE* const)dst; |
1511 | 0 | BYTE* const oend = ostart + maxDstSize; |
1512 | 0 | BYTE* op = ostart; |
1513 | 0 | const BYTE* litPtr = dctx->litPtr; |
1514 | 0 | const BYTE* const litEnd = litPtr + dctx->litSize; |
1515 | 0 | const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart); |
1516 | 0 | const BYTE* const dictStart = (const BYTE*) (dctx->virtualStart); |
1517 | 0 | const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); |
1518 | | |
1519 | | /* Regen sequences */ |
1520 | 0 | if (nbSeq) { |
1521 | 0 | #define STORED_SEQS 4 |
1522 | 0 | #define STOSEQ_MASK (STORED_SEQS-1) |
1523 | 0 | #define ADVANCED_SEQS 4 |
1524 | 0 | seq_t sequences[STORED_SEQS]; |
1525 | 0 | int const seqAdvance = MIN(nbSeq, ADVANCED_SEQS); |
1526 | 0 | seqState_t seqState; |
1527 | 0 | int seqNb; |
1528 | 0 | dctx->fseEntropy = 1; |
1529 | 0 | { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; } |
1530 | 0 | seqState.prefixStart = prefixStart; |
1531 | 0 | seqState.pos = (size_t)(op-prefixStart); |
1532 | 0 | seqState.dictEnd = dictEnd; |
1533 | 0 | CHECK_E(BIT_initDStream(&seqState.DStream, ip, iend-ip), corruption_detected); |
1534 | 0 | ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr); |
1535 | 0 | ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr); |
1536 | 0 | ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr); |
1537 | | |
1538 | | /* prepare in advance */ |
1539 | 0 | for (seqNb=0; (BIT_reloadDStream(&seqState.DStream) <= BIT_DStream_completed) && (seqNb<seqAdvance); seqNb++) { |
1540 | 0 | sequences[seqNb] = ZSTD_decodeSequenceLong(&seqState, isLongOffset); |
1541 | 0 | } |
1542 | 0 | if (seqNb<seqAdvance) return ERROR(corruption_detected); |
1543 | | |
1544 | | /* decode and decompress */ |
1545 | 0 | for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && (seqNb<nbSeq) ; seqNb++) { |
1546 | 0 | seq_t const sequence = ZSTD_decodeSequenceLong(&seqState, isLongOffset); |
1547 | 0 | size_t const oneSeqSize = ZSTD_execSequenceLong(op, oend, sequences[(seqNb-ADVANCED_SEQS) & STOSEQ_MASK], &litPtr, litEnd, prefixStart, dictStart, dictEnd); |
1548 | 0 | if (ZSTD_isError(oneSeqSize)) return oneSeqSize; |
1549 | 0 | PREFETCH(sequence.match); /* note : it's safe to invoke PREFETCH() on any memory address, including invalid ones */ |
1550 | 0 | sequences[seqNb&STOSEQ_MASK] = sequence; |
1551 | 0 | op += oneSeqSize; |
1552 | 0 | } |
1553 | 0 | if (seqNb<nbSeq) return ERROR(corruption_detected); |
1554 | | |
1555 | | /* finish queue */ |
1556 | 0 | seqNb -= seqAdvance; |
1557 | 0 | for ( ; seqNb<nbSeq ; seqNb++) { |
1558 | 0 | size_t const oneSeqSize = ZSTD_execSequenceLong(op, oend, sequences[seqNb&STOSEQ_MASK], &litPtr, litEnd, prefixStart, dictStart, dictEnd); |
1559 | 0 | if (ZSTD_isError(oneSeqSize)) return oneSeqSize; |
1560 | 0 | op += oneSeqSize; |
1561 | 0 | } |
1562 | | |
1563 | | /* save reps for next block */ |
1564 | 0 | { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); } |
1565 | 0 | #undef STORED_SEQS |
1566 | 0 | #undef STOSEQ_MASK |
1567 | 0 | #undef ADVANCED_SEQS |
1568 | 0 | } |
1569 | | |
1570 | | /* last literal segment */ |
1571 | 0 | { size_t const lastLLSize = litEnd - litPtr; |
1572 | 0 | if (lastLLSize > (size_t)(oend-op)) return ERROR(dstSize_tooSmall); |
1573 | 0 | memcpy(op, litPtr, lastLLSize); |
1574 | 0 | op += lastLLSize; |
1575 | 0 | } |
1576 | | |
1577 | 0 | return op-ostart; |
1578 | 0 | } |
1579 | | |
1580 | | static size_t |
1581 | | ZSTD_decompressSequencesLong_default(ZSTD_DCtx* dctx, |
1582 | | void* dst, size_t maxDstSize, |
1583 | | const void* seqStart, size_t seqSize, int nbSeq, |
1584 | | const ZSTD_longOffset_e isLongOffset) |
1585 | 0 | { |
1586 | 0 | return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
1587 | 0 | } |
1588 | | |
1589 | | |
1590 | | |
1591 | | #if DYNAMIC_BMI2 |
1592 | | |
1593 | | static TARGET_ATTRIBUTE("bmi2") size_t |
1594 | | ZSTD_decompressSequences_bmi2(ZSTD_DCtx* dctx, |
1595 | | void* dst, size_t maxDstSize, |
1596 | | const void* seqStart, size_t seqSize, int nbSeq, |
1597 | | const ZSTD_longOffset_e isLongOffset) |
1598 | 0 | { |
1599 | 0 | return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
1600 | 0 | } |
1601 | | |
1602 | | static TARGET_ATTRIBUTE("bmi2") size_t |
1603 | | ZSTD_decompressSequencesLong_bmi2(ZSTD_DCtx* dctx, |
1604 | | void* dst, size_t maxDstSize, |
1605 | | const void* seqStart, size_t seqSize, int nbSeq, |
1606 | | const ZSTD_longOffset_e isLongOffset) |
1607 | 0 | { |
1608 | 0 | return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
1609 | 0 | } |
1610 | | |
1611 | | #endif |
1612 | | |
1613 | | typedef size_t (*ZSTD_decompressSequences_t)( |
1614 | | ZSTD_DCtx *dctx, void *dst, size_t maxDstSize, |
1615 | | const void *seqStart, size_t seqSize, int nbSeq, |
1616 | | const ZSTD_longOffset_e isLongOffset); |
1617 | | |
1618 | | static size_t ZSTD_decompressSequences(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize, |
1619 | | const void* seqStart, size_t seqSize, int nbSeq, |
1620 | | const ZSTD_longOffset_e isLongOffset) |
1621 | 0 | { |
1622 | 0 | DEBUGLOG(5, "ZSTD_decompressSequences"); |
1623 | 0 | #if DYNAMIC_BMI2 |
1624 | 0 | if (dctx->bmi2) { |
1625 | 0 | return ZSTD_decompressSequences_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
1626 | 0 | } |
1627 | 0 | #endif |
1628 | 0 | return ZSTD_decompressSequences_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
1629 | 0 | } |
1630 | | |
1631 | | static size_t ZSTD_decompressSequencesLong(ZSTD_DCtx* dctx, |
1632 | | void* dst, size_t maxDstSize, |
1633 | | const void* seqStart, size_t seqSize, int nbSeq, |
1634 | | const ZSTD_longOffset_e isLongOffset) |
1635 | 0 | { |
1636 | 0 | DEBUGLOG(5, "ZSTD_decompressSequencesLong"); |
1637 | 0 | #if DYNAMIC_BMI2 |
1638 | 0 | if (dctx->bmi2) { |
1639 | 0 | return ZSTD_decompressSequencesLong_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
1640 | 0 | } |
1641 | 0 | #endif |
1642 | 0 | return ZSTD_decompressSequencesLong_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); |
1643 | 0 | } |
1644 | | |
1645 | | /* ZSTD_getLongOffsetsShare() : |
1646 | | * condition : offTable must be valid |
1647 | | * @return : "share" of long offsets (arbitrarily defined as > (1<<23)) |
1648 | | * compared to maximum possible of (1<<OffFSELog) */ |
1649 | | static unsigned |
1650 | | ZSTD_getLongOffsetsShare(const ZSTD_seqSymbol* offTable) |
1651 | 0 | { |
1652 | 0 | const void* ptr = offTable; |
1653 | 0 | U32 const tableLog = ((const ZSTD_seqSymbol_header*)ptr)[0].tableLog; |
1654 | 0 | const ZSTD_seqSymbol* table = offTable + 1; |
1655 | 0 | U32 const max = 1 << tableLog; |
1656 | 0 | U32 u, total = 0; |
1657 | 0 | DEBUGLOG(5, "ZSTD_getLongOffsetsShare: (tableLog=%u)", tableLog); |
1658 | |
|
1659 | 0 | assert(max <= (1 << OffFSELog)); /* max not too large */ |
1660 | 0 | for (u=0; u<max; u++) { |
1661 | 0 | if (table[u].nbAdditionalBits > 22) total += 1; |
1662 | 0 | } |
1663 | |
|
1664 | 0 | assert(tableLog <= OffFSELog); |
1665 | 0 | total <<= (OffFSELog - tableLog); /* scale to OffFSELog */ |
1666 | |
|
1667 | 0 | return total; |
1668 | 0 | } |
1669 | | |
1670 | | |
1671 | | static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx, |
1672 | | void* dst, size_t dstCapacity, |
1673 | | const void* src, size_t srcSize, const int frame) |
1674 | 0 | { /* blockType == blockCompressed */ |
1675 | 0 | const BYTE* ip = (const BYTE*)src; |
1676 | | /* isLongOffset must be true if there are long offsets. |
1677 | | * Offsets are long if they are larger than 2^STREAM_ACCUMULATOR_MIN. |
1678 | | * We don't expect that to be the case in 64-bit mode. |
1679 | | * In block mode, window size is not known, so we have to be conservative. (note: but it could be evaluated from current-lowLimit) |
1680 | | */ |
1681 | 0 | ZSTD_longOffset_e const isLongOffset = (ZSTD_longOffset_e)(MEM_32bits() && (!frame || dctx->fParams.windowSize > (1ULL << STREAM_ACCUMULATOR_MIN))); |
1682 | 0 | DEBUGLOG(5, "ZSTD_decompressBlock_internal (size : %u)", (U32)srcSize); |
1683 | |
|
1684 | 0 | if (srcSize >= ZSTD_BLOCKSIZE_MAX) return ERROR(srcSize_wrong); |
1685 | | |
1686 | | /* Decode literals section */ |
1687 | 0 | { size_t const litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize); |
1688 | 0 | DEBUGLOG(5, "ZSTD_decodeLiteralsBlock : %u", (U32)litCSize); |
1689 | 0 | if (ZSTD_isError(litCSize)) return litCSize; |
1690 | 0 | ip += litCSize; |
1691 | 0 | srcSize -= litCSize; |
1692 | 0 | } |
1693 | | |
1694 | | /* Build Decoding Tables */ |
1695 | 0 | { int nbSeq; |
1696 | 0 | size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, srcSize); |
1697 | 0 | if (ZSTD_isError(seqHSize)) return seqHSize; |
1698 | 0 | ip += seqHSize; |
1699 | 0 | srcSize -= seqHSize; |
1700 | |
|
1701 | 0 | if ( (!frame || dctx->fParams.windowSize > (1<<24)) |
1702 | 0 | && (nbSeq>0) ) { /* could probably use a larger nbSeq limit */ |
1703 | 0 | U32 const shareLongOffsets = ZSTD_getLongOffsetsShare(dctx->OFTptr); |
1704 | 0 | U32 const minShare = MEM_64bits() ? 7 : 20; /* heuristic values, correspond to 2.73% and 7.81% */ |
1705 | 0 | if (shareLongOffsets >= minShare) |
1706 | 0 | return ZSTD_decompressSequencesLong(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset); |
1707 | 0 | } |
1708 | | |
1709 | 0 | return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset); |
1710 | 0 | } |
1711 | 0 | } |
1712 | | |
1713 | | |
1714 | | static void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst) |
1715 | 0 | { |
1716 | 0 | if (dst != dctx->previousDstEnd) { /* not contiguous */ |
1717 | 0 | dctx->dictEnd = dctx->previousDstEnd; |
1718 | 0 | dctx->virtualStart = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart)); |
1719 | 0 | dctx->prefixStart = dst; |
1720 | 0 | dctx->previousDstEnd = dst; |
1721 | 0 | } |
1722 | 0 | } |
1723 | | |
1724 | | size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, |
1725 | | void* dst, size_t dstCapacity, |
1726 | | const void* src, size_t srcSize) |
1727 | 0 | { |
1728 | 0 | size_t dSize; |
1729 | 0 | ZSTD_checkContinuity(dctx, dst); |
1730 | 0 | dSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 0); |
1731 | 0 | dctx->previousDstEnd = (char*)dst + dSize; |
1732 | 0 | return dSize; |
1733 | 0 | } |
1734 | | |
1735 | | |
1736 | | /** ZSTD_insertBlock() : |
1737 | | insert `src` block into `dctx` history. Useful to track uncompressed blocks. */ |
1738 | | ZSTDLIB_API size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize) |
1739 | 0 | { |
1740 | 0 | ZSTD_checkContinuity(dctx, blockStart); |
1741 | 0 | dctx->previousDstEnd = (const char*)blockStart + blockSize; |
1742 | 0 | return blockSize; |
1743 | 0 | } |
1744 | | |
1745 | | |
1746 | | static size_t ZSTD_generateNxBytes(void* dst, size_t dstCapacity, BYTE byte, size_t length) |
1747 | 0 | { |
1748 | 0 | if (length > dstCapacity) return ERROR(dstSize_tooSmall); |
1749 | 0 | memset(dst, byte, length); |
1750 | 0 | return length; |
1751 | 0 | } |
1752 | | |
1753 | | /** ZSTD_findFrameCompressedSize() : |
1754 | | * compatible with legacy mode |
1755 | | * `src` must point to the start of a ZSTD frame, ZSTD legacy frame, or skippable frame |
1756 | | * `srcSize` must be at least as large as the frame contained |
1757 | | * @return : the compressed size of the frame starting at `src` */ |
1758 | | size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize) |
1759 | 0 | { |
1760 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
1761 | | if (ZSTD_isLegacy(src, srcSize)) |
1762 | | return ZSTD_findFrameCompressedSizeLegacy(src, srcSize); |
1763 | | #endif |
1764 | 0 | if ( (srcSize >= ZSTD_skippableHeaderSize) |
1765 | 0 | && (MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START ) { |
1766 | 0 | return ZSTD_skippableHeaderSize + MEM_readLE32((const BYTE*)src + ZSTD_frameIdSize); |
1767 | 0 | } else { |
1768 | 0 | const BYTE* ip = (const BYTE*)src; |
1769 | 0 | const BYTE* const ipstart = ip; |
1770 | 0 | size_t remainingSize = srcSize; |
1771 | 0 | ZSTD_frameHeader zfh; |
1772 | | |
1773 | | /* Extract Frame Header */ |
1774 | 0 | { size_t const ret = ZSTD_getFrameHeader(&zfh, src, srcSize); |
1775 | 0 | if (ZSTD_isError(ret)) return ret; |
1776 | 0 | if (ret > 0) return ERROR(srcSize_wrong); |
1777 | 0 | } |
1778 | | |
1779 | 0 | ip += zfh.headerSize; |
1780 | 0 | remainingSize -= zfh.headerSize; |
1781 | | |
1782 | | /* Loop on each block */ |
1783 | 0 | while (1) { |
1784 | 0 | blockProperties_t blockProperties; |
1785 | 0 | size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties); |
1786 | 0 | if (ZSTD_isError(cBlockSize)) return cBlockSize; |
1787 | | |
1788 | 0 | if (ZSTD_blockHeaderSize + cBlockSize > remainingSize) |
1789 | 0 | return ERROR(srcSize_wrong); |
1790 | | |
1791 | 0 | ip += ZSTD_blockHeaderSize + cBlockSize; |
1792 | 0 | remainingSize -= ZSTD_blockHeaderSize + cBlockSize; |
1793 | |
|
1794 | 0 | if (blockProperties.lastBlock) break; |
1795 | 0 | } |
1796 | | |
1797 | 0 | if (zfh.checksumFlag) { /* Final frame content checksum */ |
1798 | 0 | if (remainingSize < 4) return ERROR(srcSize_wrong); |
1799 | 0 | ip += 4; |
1800 | 0 | remainingSize -= 4; |
1801 | 0 | } |
1802 | | |
1803 | 0 | return ip - ipstart; |
1804 | 0 | } |
1805 | 0 | } |
1806 | | |
1807 | | /*! ZSTD_decompressFrame() : |
1808 | | * @dctx must be properly initialized */ |
1809 | | static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx, |
1810 | | void* dst, size_t dstCapacity, |
1811 | | const void** srcPtr, size_t *srcSizePtr) |
1812 | 0 | { |
1813 | 0 | const BYTE* ip = (const BYTE*)(*srcPtr); |
1814 | 0 | BYTE* const ostart = (BYTE* const)dst; |
1815 | 0 | BYTE* const oend = ostart + dstCapacity; |
1816 | 0 | BYTE* op = ostart; |
1817 | 0 | size_t remainingSize = *srcSizePtr; |
1818 | | |
1819 | | /* check */ |
1820 | 0 | if (remainingSize < ZSTD_frameHeaderSize_min+ZSTD_blockHeaderSize) |
1821 | 0 | return ERROR(srcSize_wrong); |
1822 | | |
1823 | | /* Frame Header */ |
1824 | 0 | { size_t const frameHeaderSize = ZSTD_frameHeaderSize(ip, ZSTD_frameHeaderSize_prefix); |
1825 | 0 | if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize; |
1826 | 0 | if (remainingSize < frameHeaderSize+ZSTD_blockHeaderSize) |
1827 | 0 | return ERROR(srcSize_wrong); |
1828 | 0 | CHECK_F( ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize) ); |
1829 | 0 | ip += frameHeaderSize; remainingSize -= frameHeaderSize; |
1830 | 0 | } |
1831 | | |
1832 | | /* Loop on each block */ |
1833 | 0 | while (1) { |
1834 | 0 | size_t decodedSize; |
1835 | 0 | blockProperties_t blockProperties; |
1836 | 0 | size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties); |
1837 | 0 | if (ZSTD_isError(cBlockSize)) return cBlockSize; |
1838 | | |
1839 | 0 | ip += ZSTD_blockHeaderSize; |
1840 | 0 | remainingSize -= ZSTD_blockHeaderSize; |
1841 | 0 | if (cBlockSize > remainingSize) return ERROR(srcSize_wrong); |
1842 | | |
1843 | 0 | switch(blockProperties.blockType) |
1844 | 0 | { |
1845 | 0 | case bt_compressed: |
1846 | 0 | decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize, /* frame */ 1); |
1847 | 0 | break; |
1848 | 0 | case bt_raw : |
1849 | 0 | decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize); |
1850 | 0 | break; |
1851 | 0 | case bt_rle : |
1852 | 0 | decodedSize = ZSTD_generateNxBytes(op, oend-op, *ip, blockProperties.origSize); |
1853 | 0 | break; |
1854 | 0 | case bt_reserved : |
1855 | 0 | default: |
1856 | 0 | return ERROR(corruption_detected); |
1857 | 0 | } |
1858 | | |
1859 | 0 | if (ZSTD_isError(decodedSize)) return decodedSize; |
1860 | 0 | if (dctx->fParams.checksumFlag) |
1861 | 0 | XXH64_update(&dctx->xxhState, op, decodedSize); |
1862 | 0 | op += decodedSize; |
1863 | 0 | ip += cBlockSize; |
1864 | 0 | remainingSize -= cBlockSize; |
1865 | 0 | if (blockProperties.lastBlock) break; |
1866 | 0 | } |
1867 | | |
1868 | 0 | if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) { |
1869 | 0 | if ((U64)(op-ostart) != dctx->fParams.frameContentSize) { |
1870 | 0 | return ERROR(corruption_detected); |
1871 | 0 | } } |
1872 | 0 | if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */ |
1873 | 0 | U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState); |
1874 | 0 | U32 checkRead; |
1875 | 0 | if (remainingSize<4) return ERROR(checksum_wrong); |
1876 | 0 | checkRead = MEM_readLE32(ip); |
1877 | 0 | if (checkRead != checkCalc) return ERROR(checksum_wrong); |
1878 | 0 | ip += 4; |
1879 | 0 | remainingSize -= 4; |
1880 | 0 | } |
1881 | | |
1882 | | /* Allow caller to get size read */ |
1883 | 0 | *srcPtr = ip; |
1884 | 0 | *srcSizePtr = remainingSize; |
1885 | 0 | return op-ostart; |
1886 | 0 | } |
1887 | | |
1888 | | static const void* ZSTD_DDictDictContent(const ZSTD_DDict* ddict); |
1889 | | static size_t ZSTD_DDictDictSize(const ZSTD_DDict* ddict); |
1890 | | |
1891 | | static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx, |
1892 | | void* dst, size_t dstCapacity, |
1893 | | const void* src, size_t srcSize, |
1894 | | const void* dict, size_t dictSize, |
1895 | | const ZSTD_DDict* ddict) |
1896 | 0 | { |
1897 | 0 | void* const dststart = dst; |
1898 | 0 | int moreThan1Frame = 0; |
1899 | 0 | assert(dict==NULL || ddict==NULL); /* either dict or ddict set, not both */ |
1900 | |
|
1901 | 0 | if (ddict) { |
1902 | 0 | dict = ZSTD_DDictDictContent(ddict); |
1903 | 0 | dictSize = ZSTD_DDictDictSize(ddict); |
1904 | 0 | } |
1905 | |
|
1906 | 0 | while (srcSize >= ZSTD_frameHeaderSize_prefix) { |
1907 | |
|
1908 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
1909 | | if (ZSTD_isLegacy(src, srcSize)) { |
1910 | | size_t decodedSize; |
1911 | | size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize); |
1912 | | if (ZSTD_isError(frameSize)) return frameSize; |
1913 | | /* legacy support is not compatible with static dctx */ |
1914 | | if (dctx->staticSize) return ERROR(memory_allocation); |
1915 | | |
1916 | | decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize); |
1917 | | |
1918 | | dst = (BYTE*)dst + decodedSize; |
1919 | | dstCapacity -= decodedSize; |
1920 | | |
1921 | | src = (const BYTE*)src + frameSize; |
1922 | | srcSize -= frameSize; |
1923 | | |
1924 | | continue; |
1925 | | } |
1926 | | #endif |
1927 | |
|
1928 | 0 | { U32 const magicNumber = MEM_readLE32(src); |
1929 | 0 | DEBUGLOG(4, "reading magic number %08X (expecting %08X)", |
1930 | 0 | (U32)magicNumber, (U32)ZSTD_MAGICNUMBER); |
1931 | 0 | if ((magicNumber & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { |
1932 | 0 | size_t skippableSize; |
1933 | 0 | if (srcSize < ZSTD_skippableHeaderSize) |
1934 | 0 | return ERROR(srcSize_wrong); |
1935 | 0 | skippableSize = MEM_readLE32((const BYTE*)src + ZSTD_frameIdSize) |
1936 | 0 | + ZSTD_skippableHeaderSize; |
1937 | 0 | if (srcSize < skippableSize) return ERROR(srcSize_wrong); |
1938 | | |
1939 | 0 | src = (const BYTE *)src + skippableSize; |
1940 | 0 | srcSize -= skippableSize; |
1941 | 0 | continue; |
1942 | 0 | } } |
1943 | | |
1944 | 0 | if (ddict) { |
1945 | | /* we were called from ZSTD_decompress_usingDDict */ |
1946 | 0 | CHECK_F(ZSTD_decompressBegin_usingDDict(dctx, ddict)); |
1947 | 0 | } else { |
1948 | | /* this will initialize correctly with no dict if dict == NULL, so |
1949 | | * use this in all cases but ddict */ |
1950 | 0 | CHECK_F(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize)); |
1951 | 0 | } |
1952 | 0 | ZSTD_checkContinuity(dctx, dst); |
1953 | |
|
1954 | 0 | { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity, |
1955 | 0 | &src, &srcSize); |
1956 | 0 | if ( (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown) |
1957 | 0 | && (moreThan1Frame==1) ) { |
1958 | | /* at least one frame successfully completed, |
1959 | | * but following bytes are garbage : |
1960 | | * it's more likely to be a srcSize error, |
1961 | | * specifying more bytes than compressed size of frame(s). |
1962 | | * This error message replaces ERROR(prefix_unknown), |
1963 | | * which would be confusing, as the first header is actually correct. |
1964 | | * Note that one could be unlucky, it might be a corruption error instead, |
1965 | | * happening right at the place where we expect zstd magic bytes. |
1966 | | * But this is _much_ less likely than a srcSize field error. */ |
1967 | 0 | return ERROR(srcSize_wrong); |
1968 | 0 | } |
1969 | 0 | if (ZSTD_isError(res)) return res; |
1970 | | /* no need to bound check, ZSTD_decompressFrame already has */ |
1971 | 0 | dst = (BYTE*)dst + res; |
1972 | 0 | dstCapacity -= res; |
1973 | 0 | } |
1974 | 0 | moreThan1Frame = 1; |
1975 | 0 | } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */ |
1976 | | |
1977 | 0 | if (srcSize) return ERROR(srcSize_wrong); /* input not entirely consumed */ |
1978 | | |
1979 | 0 | return (BYTE*)dst - (BYTE*)dststart; |
1980 | 0 | } |
1981 | | |
1982 | | size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, |
1983 | | void* dst, size_t dstCapacity, |
1984 | | const void* src, size_t srcSize, |
1985 | | const void* dict, size_t dictSize) |
1986 | 0 | { |
1987 | 0 | return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL); |
1988 | 0 | } |
1989 | | |
1990 | | |
1991 | | size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
1992 | 0 | { |
1993 | 0 | return ZSTD_decompress_usingDict(dctx, dst, dstCapacity, src, srcSize, NULL, 0); |
1994 | 0 | } |
1995 | | |
1996 | | |
1997 | | size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
1998 | 0 | { |
1999 | 0 | #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1) |
2000 | 0 | size_t regenSize; |
2001 | 0 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
2002 | 0 | if (dctx==NULL) return ERROR(memory_allocation); |
2003 | 0 | regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize); |
2004 | 0 | ZSTD_freeDCtx(dctx); |
2005 | 0 | return regenSize; |
2006 | | #else /* stack mode */ |
2007 | | ZSTD_DCtx dctx; |
2008 | | ZSTD_initDCtx_internal(&dctx); |
2009 | | return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize); |
2010 | | #endif |
2011 | 0 | } |
2012 | | |
2013 | | |
2014 | | /*-************************************** |
2015 | | * Advanced Streaming Decompression API |
2016 | | * Bufferless and synchronous |
2017 | | ****************************************/ |
2018 | 0 | size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; } |
2019 | | |
2020 | 0 | ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) { |
2021 | 0 | switch(dctx->stage) |
2022 | 0 | { |
2023 | 0 | default: /* should not happen */ |
2024 | 0 | assert(0); |
2025 | 0 | case ZSTDds_getFrameHeaderSize: |
2026 | 0 | case ZSTDds_decodeFrameHeader: |
2027 | 0 | return ZSTDnit_frameHeader; |
2028 | 0 | case ZSTDds_decodeBlockHeader: |
2029 | 0 | return ZSTDnit_blockHeader; |
2030 | 0 | case ZSTDds_decompressBlock: |
2031 | 0 | return ZSTDnit_block; |
2032 | 0 | case ZSTDds_decompressLastBlock: |
2033 | 0 | return ZSTDnit_lastBlock; |
2034 | 0 | case ZSTDds_checkChecksum: |
2035 | 0 | return ZSTDnit_checksum; |
2036 | 0 | case ZSTDds_decodeSkippableHeader: |
2037 | 0 | case ZSTDds_skipFrame: |
2038 | 0 | return ZSTDnit_skippableFrame; |
2039 | 0 | } |
2040 | 0 | } |
2041 | | |
2042 | 0 | static int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; } |
2043 | | |
2044 | | /** ZSTD_decompressContinue() : |
2045 | | * srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress()) |
2046 | | * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity) |
2047 | | * or an error code, which can be tested using ZSTD_isError() */ |
2048 | | size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
2049 | 0 | { |
2050 | 0 | DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (U32)srcSize); |
2051 | | /* Sanity check */ |
2052 | 0 | if (srcSize != dctx->expected) return ERROR(srcSize_wrong); /* not allowed */ |
2053 | 0 | if (dstCapacity) ZSTD_checkContinuity(dctx, dst); |
2054 | |
|
2055 | 0 | switch (dctx->stage) |
2056 | 0 | { |
2057 | 0 | case ZSTDds_getFrameHeaderSize : |
2058 | 0 | assert(src != NULL); |
2059 | 0 | if (dctx->format == ZSTD_f_zstd1) { /* allows header */ |
2060 | 0 | assert(srcSize >= ZSTD_frameIdSize); /* to read skippable magic number */ |
2061 | 0 | if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */ |
2062 | 0 | memcpy(dctx->headerBuffer, src, srcSize); |
2063 | 0 | dctx->expected = ZSTD_skippableHeaderSize - srcSize; /* remaining to load to get full skippable frame header */ |
2064 | 0 | dctx->stage = ZSTDds_decodeSkippableHeader; |
2065 | 0 | return 0; |
2066 | 0 | } } |
2067 | 0 | dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format); |
2068 | 0 | if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize; |
2069 | 0 | memcpy(dctx->headerBuffer, src, srcSize); |
2070 | 0 | dctx->expected = dctx->headerSize - srcSize; |
2071 | 0 | dctx->stage = ZSTDds_decodeFrameHeader; |
2072 | 0 | return 0; |
2073 | | |
2074 | 0 | case ZSTDds_decodeFrameHeader: |
2075 | 0 | assert(src != NULL); |
2076 | 0 | memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize); |
2077 | 0 | CHECK_F(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize)); |
2078 | 0 | dctx->expected = ZSTD_blockHeaderSize; |
2079 | 0 | dctx->stage = ZSTDds_decodeBlockHeader; |
2080 | 0 | return 0; |
2081 | | |
2082 | 0 | case ZSTDds_decodeBlockHeader: |
2083 | 0 | { blockProperties_t bp; |
2084 | 0 | size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp); |
2085 | 0 | if (ZSTD_isError(cBlockSize)) return cBlockSize; |
2086 | 0 | dctx->expected = cBlockSize; |
2087 | 0 | dctx->bType = bp.blockType; |
2088 | 0 | dctx->rleSize = bp.origSize; |
2089 | 0 | if (cBlockSize) { |
2090 | 0 | dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock; |
2091 | 0 | return 0; |
2092 | 0 | } |
2093 | | /* empty block */ |
2094 | 0 | if (bp.lastBlock) { |
2095 | 0 | if (dctx->fParams.checksumFlag) { |
2096 | 0 | dctx->expected = 4; |
2097 | 0 | dctx->stage = ZSTDds_checkChecksum; |
2098 | 0 | } else { |
2099 | 0 | dctx->expected = 0; /* end of frame */ |
2100 | 0 | dctx->stage = ZSTDds_getFrameHeaderSize; |
2101 | 0 | } |
2102 | 0 | } else { |
2103 | 0 | dctx->expected = ZSTD_blockHeaderSize; /* jump to next header */ |
2104 | 0 | dctx->stage = ZSTDds_decodeBlockHeader; |
2105 | 0 | } |
2106 | 0 | return 0; |
2107 | 0 | } |
2108 | | |
2109 | 0 | case ZSTDds_decompressLastBlock: |
2110 | 0 | case ZSTDds_decompressBlock: |
2111 | 0 | DEBUGLOG(5, "ZSTD_decompressContinue: case ZSTDds_decompressBlock"); |
2112 | 0 | { size_t rSize; |
2113 | 0 | switch(dctx->bType) |
2114 | 0 | { |
2115 | 0 | case bt_compressed: |
2116 | 0 | DEBUGLOG(5, "ZSTD_decompressContinue: case bt_compressed"); |
2117 | 0 | rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 1); |
2118 | 0 | break; |
2119 | 0 | case bt_raw : |
2120 | 0 | rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize); |
2121 | 0 | break; |
2122 | 0 | case bt_rle : |
2123 | 0 | rSize = ZSTD_setRleBlock(dst, dstCapacity, src, srcSize, dctx->rleSize); |
2124 | 0 | break; |
2125 | 0 | case bt_reserved : /* should never happen */ |
2126 | 0 | default: |
2127 | 0 | return ERROR(corruption_detected); |
2128 | 0 | } |
2129 | 0 | if (ZSTD_isError(rSize)) return rSize; |
2130 | 0 | DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (U32)rSize); |
2131 | 0 | dctx->decodedSize += rSize; |
2132 | 0 | if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, dst, rSize); |
2133 | |
|
2134 | 0 | if (dctx->stage == ZSTDds_decompressLastBlock) { /* end of frame */ |
2135 | 0 | DEBUGLOG(4, "ZSTD_decompressContinue: decoded size from frame : %u", (U32)dctx->decodedSize); |
2136 | 0 | if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) { |
2137 | 0 | if (dctx->decodedSize != dctx->fParams.frameContentSize) { |
2138 | 0 | return ERROR(corruption_detected); |
2139 | 0 | } } |
2140 | 0 | if (dctx->fParams.checksumFlag) { /* another round for frame checksum */ |
2141 | 0 | dctx->expected = 4; |
2142 | 0 | dctx->stage = ZSTDds_checkChecksum; |
2143 | 0 | } else { |
2144 | 0 | dctx->expected = 0; /* ends here */ |
2145 | 0 | dctx->stage = ZSTDds_getFrameHeaderSize; |
2146 | 0 | } |
2147 | 0 | } else { |
2148 | 0 | dctx->stage = ZSTDds_decodeBlockHeader; |
2149 | 0 | dctx->expected = ZSTD_blockHeaderSize; |
2150 | 0 | dctx->previousDstEnd = (char*)dst + rSize; |
2151 | 0 | } |
2152 | 0 | return rSize; |
2153 | 0 | } |
2154 | | |
2155 | 0 | case ZSTDds_checkChecksum: |
2156 | 0 | assert(srcSize == 4); /* guaranteed by dctx->expected */ |
2157 | 0 | { U32 const h32 = (U32)XXH64_digest(&dctx->xxhState); |
2158 | 0 | U32 const check32 = MEM_readLE32(src); |
2159 | 0 | DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", h32, check32); |
2160 | 0 | if (check32 != h32) return ERROR(checksum_wrong); |
2161 | 0 | dctx->expected = 0; |
2162 | 0 | dctx->stage = ZSTDds_getFrameHeaderSize; |
2163 | 0 | return 0; |
2164 | 0 | } |
2165 | | |
2166 | 0 | case ZSTDds_decodeSkippableHeader: |
2167 | 0 | assert(src != NULL); |
2168 | 0 | assert(srcSize <= ZSTD_skippableHeaderSize); |
2169 | 0 | memcpy(dctx->headerBuffer + (ZSTD_skippableHeaderSize - srcSize), src, srcSize); /* complete skippable header */ |
2170 | 0 | dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_frameIdSize); /* note : dctx->expected can grow seriously large, beyond local buffer size */ |
2171 | 0 | dctx->stage = ZSTDds_skipFrame; |
2172 | 0 | return 0; |
2173 | | |
2174 | 0 | case ZSTDds_skipFrame: |
2175 | 0 | dctx->expected = 0; |
2176 | 0 | dctx->stage = ZSTDds_getFrameHeaderSize; |
2177 | 0 | return 0; |
2178 | | |
2179 | 0 | default: |
2180 | 0 | return ERROR(GENERIC); /* impossible */ |
2181 | 0 | } |
2182 | 0 | } |
2183 | | |
2184 | | |
2185 | | static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
2186 | 0 | { |
2187 | 0 | dctx->dictEnd = dctx->previousDstEnd; |
2188 | 0 | dctx->virtualStart = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart)); |
2189 | 0 | dctx->prefixStart = dict; |
2190 | 0 | dctx->previousDstEnd = (const char*)dict + dictSize; |
2191 | 0 | return 0; |
2192 | 0 | } |
2193 | | |
2194 | | /* ZSTD_loadEntropy() : |
2195 | | * dict : must point at beginning of a valid zstd dictionary |
2196 | | * @return : size of entropy tables read */ |
2197 | | static size_t ZSTD_loadEntropy(ZSTD_entropyDTables_t* entropy, const void* const dict, size_t const dictSize) |
2198 | 0 | { |
2199 | 0 | const BYTE* dictPtr = (const BYTE*)dict; |
2200 | 0 | const BYTE* const dictEnd = dictPtr + dictSize; |
2201 | |
|
2202 | 0 | if (dictSize <= 8) return ERROR(dictionary_corrupted); |
2203 | 0 | dictPtr += 8; /* skip header = magic + dictID */ |
2204 | | |
2205 | |
|
2206 | 0 | { size_t const hSize = HUF_readDTableX2_wksp( |
2207 | 0 | entropy->hufTable, dictPtr, dictEnd - dictPtr, |
2208 | 0 | entropy->workspace, sizeof(entropy->workspace)); |
2209 | 0 | if (HUF_isError(hSize)) return ERROR(dictionary_corrupted); |
2210 | 0 | dictPtr += hSize; |
2211 | 0 | } |
2212 | | |
2213 | 0 | { short offcodeNCount[MaxOff+1]; |
2214 | 0 | U32 offcodeMaxValue = MaxOff, offcodeLog; |
2215 | 0 | size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr); |
2216 | 0 | if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted); |
2217 | 0 | if (offcodeMaxValue > MaxOff) return ERROR(dictionary_corrupted); |
2218 | 0 | if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted); |
2219 | 0 | ZSTD_buildFSETable(entropy->OFTable, |
2220 | 0 | offcodeNCount, offcodeMaxValue, |
2221 | 0 | OF_base, OF_bits, |
2222 | 0 | offcodeLog); |
2223 | 0 | dictPtr += offcodeHeaderSize; |
2224 | 0 | } |
2225 | | |
2226 | 0 | { short matchlengthNCount[MaxML+1]; |
2227 | 0 | unsigned matchlengthMaxValue = MaxML, matchlengthLog; |
2228 | 0 | size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr); |
2229 | 0 | if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted); |
2230 | 0 | if (matchlengthMaxValue > MaxML) return ERROR(dictionary_corrupted); |
2231 | 0 | if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted); |
2232 | 0 | ZSTD_buildFSETable(entropy->MLTable, |
2233 | 0 | matchlengthNCount, matchlengthMaxValue, |
2234 | 0 | ML_base, ML_bits, |
2235 | 0 | matchlengthLog); |
2236 | 0 | dictPtr += matchlengthHeaderSize; |
2237 | 0 | } |
2238 | | |
2239 | 0 | { short litlengthNCount[MaxLL+1]; |
2240 | 0 | unsigned litlengthMaxValue = MaxLL, litlengthLog; |
2241 | 0 | size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr); |
2242 | 0 | if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted); |
2243 | 0 | if (litlengthMaxValue > MaxLL) return ERROR(dictionary_corrupted); |
2244 | 0 | if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted); |
2245 | 0 | ZSTD_buildFSETable(entropy->LLTable, |
2246 | 0 | litlengthNCount, litlengthMaxValue, |
2247 | 0 | LL_base, LL_bits, |
2248 | 0 | litlengthLog); |
2249 | 0 | dictPtr += litlengthHeaderSize; |
2250 | 0 | } |
2251 | | |
2252 | 0 | if (dictPtr+12 > dictEnd) return ERROR(dictionary_corrupted); |
2253 | 0 | { int i; |
2254 | 0 | size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12)); |
2255 | 0 | for (i=0; i<3; i++) { |
2256 | 0 | U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4; |
2257 | 0 | if (rep==0 || rep >= dictContentSize) return ERROR(dictionary_corrupted); |
2258 | 0 | entropy->rep[i] = rep; |
2259 | 0 | } } |
2260 | | |
2261 | 0 | return dictPtr - (const BYTE*)dict; |
2262 | 0 | } |
2263 | | |
2264 | | static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
2265 | 0 | { |
2266 | 0 | if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize); |
2267 | 0 | { U32 const magic = MEM_readLE32(dict); |
2268 | 0 | if (magic != ZSTD_MAGIC_DICTIONARY) { |
2269 | 0 | return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */ |
2270 | 0 | } } |
2271 | 0 | dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_frameIdSize); |
2272 | | |
2273 | | /* load entropy tables */ |
2274 | 0 | { size_t const eSize = ZSTD_loadEntropy(&dctx->entropy, dict, dictSize); |
2275 | 0 | if (ZSTD_isError(eSize)) return ERROR(dictionary_corrupted); |
2276 | 0 | dict = (const char*)dict + eSize; |
2277 | 0 | dictSize -= eSize; |
2278 | 0 | } |
2279 | 0 | dctx->litEntropy = dctx->fseEntropy = 1; |
2280 | | |
2281 | | /* reference dictionary content */ |
2282 | 0 | return ZSTD_refDictContent(dctx, dict, dictSize); |
2283 | 0 | } |
2284 | | |
2285 | | /* Note : this function cannot fail */ |
2286 | | size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx) |
2287 | 0 | { |
2288 | 0 | assert(dctx != NULL); |
2289 | 0 | dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */ |
2290 | 0 | dctx->stage = ZSTDds_getFrameHeaderSize; |
2291 | 0 | dctx->decodedSize = 0; |
2292 | 0 | dctx->previousDstEnd = NULL; |
2293 | 0 | dctx->prefixStart = NULL; |
2294 | 0 | dctx->virtualStart = NULL; |
2295 | 0 | dctx->dictEnd = NULL; |
2296 | 0 | dctx->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */ |
2297 | 0 | dctx->litEntropy = dctx->fseEntropy = 0; |
2298 | 0 | dctx->dictID = 0; |
2299 | 0 | ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue)); |
2300 | 0 | memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */ |
2301 | 0 | dctx->LLTptr = dctx->entropy.LLTable; |
2302 | 0 | dctx->MLTptr = dctx->entropy.MLTable; |
2303 | 0 | dctx->OFTptr = dctx->entropy.OFTable; |
2304 | 0 | dctx->HUFptr = dctx->entropy.hufTable; |
2305 | 0 | return 0; |
2306 | 0 | } |
2307 | | |
2308 | | size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
2309 | 0 | { |
2310 | 0 | CHECK_F( ZSTD_decompressBegin(dctx) ); |
2311 | 0 | if (dict && dictSize) |
2312 | 0 | CHECK_E(ZSTD_decompress_insertDictionary(dctx, dict, dictSize), dictionary_corrupted); |
2313 | 0 | return 0; |
2314 | 0 | } |
2315 | | |
2316 | | |
2317 | | /* ====== ZSTD_DDict ====== */ |
2318 | | |
2319 | | struct ZSTD_DDict_s { |
2320 | | void* dictBuffer; |
2321 | | const void* dictContent; |
2322 | | size_t dictSize; |
2323 | | ZSTD_entropyDTables_t entropy; |
2324 | | U32 dictID; |
2325 | | U32 entropyPresent; |
2326 | | ZSTD_customMem cMem; |
2327 | | }; /* typedef'd to ZSTD_DDict within "zstd.h" */ |
2328 | | |
2329 | | static const void* ZSTD_DDictDictContent(const ZSTD_DDict* ddict) |
2330 | 0 | { |
2331 | 0 | return ddict->dictContent; |
2332 | 0 | } |
2333 | | |
2334 | | static size_t ZSTD_DDictDictSize(const ZSTD_DDict* ddict) |
2335 | 0 | { |
2336 | 0 | return ddict->dictSize; |
2337 | 0 | } |
2338 | | |
2339 | | size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dstDCtx, const ZSTD_DDict* ddict) |
2340 | 0 | { |
2341 | 0 | CHECK_F( ZSTD_decompressBegin(dstDCtx) ); |
2342 | 0 | if (ddict) { /* support begin on NULL */ |
2343 | 0 | dstDCtx->dictID = ddict->dictID; |
2344 | 0 | dstDCtx->prefixStart = ddict->dictContent; |
2345 | 0 | dstDCtx->virtualStart = ddict->dictContent; |
2346 | 0 | dstDCtx->dictEnd = (const BYTE*)ddict->dictContent + ddict->dictSize; |
2347 | 0 | dstDCtx->previousDstEnd = dstDCtx->dictEnd; |
2348 | 0 | if (ddict->entropyPresent) { |
2349 | 0 | dstDCtx->litEntropy = 1; |
2350 | 0 | dstDCtx->fseEntropy = 1; |
2351 | 0 | dstDCtx->LLTptr = ddict->entropy.LLTable; |
2352 | 0 | dstDCtx->MLTptr = ddict->entropy.MLTable; |
2353 | 0 | dstDCtx->OFTptr = ddict->entropy.OFTable; |
2354 | 0 | dstDCtx->HUFptr = ddict->entropy.hufTable; |
2355 | 0 | dstDCtx->entropy.rep[0] = ddict->entropy.rep[0]; |
2356 | 0 | dstDCtx->entropy.rep[1] = ddict->entropy.rep[1]; |
2357 | 0 | dstDCtx->entropy.rep[2] = ddict->entropy.rep[2]; |
2358 | 0 | } else { |
2359 | 0 | dstDCtx->litEntropy = 0; |
2360 | 0 | dstDCtx->fseEntropy = 0; |
2361 | 0 | } |
2362 | 0 | } |
2363 | 0 | return 0; |
2364 | 0 | } |
2365 | | |
2366 | | static size_t ZSTD_loadEntropy_inDDict(ZSTD_DDict* ddict, ZSTD_dictContentType_e dictContentType) |
2367 | 0 | { |
2368 | 0 | ddict->dictID = 0; |
2369 | 0 | ddict->entropyPresent = 0; |
2370 | 0 | if (dictContentType == ZSTD_dct_rawContent) return 0; |
2371 | | |
2372 | 0 | if (ddict->dictSize < 8) { |
2373 | 0 | if (dictContentType == ZSTD_dct_fullDict) |
2374 | 0 | return ERROR(dictionary_corrupted); /* only accept specified dictionaries */ |
2375 | 0 | return 0; /* pure content mode */ |
2376 | 0 | } |
2377 | 0 | { U32 const magic = MEM_readLE32(ddict->dictContent); |
2378 | 0 | if (magic != ZSTD_MAGIC_DICTIONARY) { |
2379 | 0 | if (dictContentType == ZSTD_dct_fullDict) |
2380 | 0 | return ERROR(dictionary_corrupted); /* only accept specified dictionaries */ |
2381 | 0 | return 0; /* pure content mode */ |
2382 | 0 | } |
2383 | 0 | } |
2384 | 0 | ddict->dictID = MEM_readLE32((const char*)ddict->dictContent + ZSTD_frameIdSize); |
2385 | | |
2386 | | /* load entropy tables */ |
2387 | 0 | CHECK_E( ZSTD_loadEntropy(&ddict->entropy, ddict->dictContent, ddict->dictSize), dictionary_corrupted ); |
2388 | 0 | ddict->entropyPresent = 1; |
2389 | 0 | return 0; |
2390 | 0 | } |
2391 | | |
2392 | | |
2393 | | static size_t ZSTD_initDDict_internal(ZSTD_DDict* ddict, |
2394 | | const void* dict, size_t dictSize, |
2395 | | ZSTD_dictLoadMethod_e dictLoadMethod, |
2396 | | ZSTD_dictContentType_e dictContentType) |
2397 | 0 | { |
2398 | 0 | if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dict) || (!dictSize)) { |
2399 | 0 | ddict->dictBuffer = NULL; |
2400 | 0 | ddict->dictContent = dict; |
2401 | 0 | } else { |
2402 | 0 | void* const internalBuffer = ZSTD_malloc(dictSize, ddict->cMem); |
2403 | 0 | ddict->dictBuffer = internalBuffer; |
2404 | 0 | ddict->dictContent = internalBuffer; |
2405 | 0 | if (!internalBuffer) return ERROR(memory_allocation); |
2406 | 0 | memcpy(internalBuffer, dict, dictSize); |
2407 | 0 | } |
2408 | 0 | ddict->dictSize = dictSize; |
2409 | 0 | ddict->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */ |
2410 | | |
2411 | | /* parse dictionary content */ |
2412 | 0 | CHECK_F( ZSTD_loadEntropy_inDDict(ddict, dictContentType) ); |
2413 | |
|
2414 | 0 | return 0; |
2415 | 0 | } |
2416 | | |
2417 | | ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, |
2418 | | ZSTD_dictLoadMethod_e dictLoadMethod, |
2419 | | ZSTD_dictContentType_e dictContentType, |
2420 | | ZSTD_customMem customMem) |
2421 | 0 | { |
2422 | 0 | if (!customMem.customAlloc ^ !customMem.customFree) return NULL; |
2423 | | |
2424 | 0 | { ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_malloc(sizeof(ZSTD_DDict), customMem); |
2425 | 0 | if (!ddict) return NULL; |
2426 | 0 | ddict->cMem = customMem; |
2427 | |
|
2428 | 0 | if (ZSTD_isError( ZSTD_initDDict_internal(ddict, dict, dictSize, dictLoadMethod, dictContentType) )) { |
2429 | 0 | ZSTD_freeDDict(ddict); |
2430 | 0 | return NULL; |
2431 | 0 | } |
2432 | | |
2433 | 0 | return ddict; |
2434 | 0 | } |
2435 | 0 | } |
2436 | | |
2437 | | /*! ZSTD_createDDict() : |
2438 | | * Create a digested dictionary, to start decompression without startup delay. |
2439 | | * `dict` content is copied inside DDict. |
2440 | | * Consequently, `dict` can be released after `ZSTD_DDict` creation */ |
2441 | | ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize) |
2442 | 0 | { |
2443 | 0 | ZSTD_customMem const allocator = { NULL, NULL, NULL }; |
2444 | 0 | return ZSTD_createDDict_advanced(dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto, allocator); |
2445 | 0 | } |
2446 | | |
2447 | | /*! ZSTD_createDDict_byReference() : |
2448 | | * Create a digested dictionary, to start decompression without startup delay. |
2449 | | * Dictionary content is simply referenced, it will be accessed during decompression. |
2450 | | * Warning : dictBuffer must outlive DDict (DDict must be freed before dictBuffer) */ |
2451 | | ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize) |
2452 | 0 | { |
2453 | 0 | ZSTD_customMem const allocator = { NULL, NULL, NULL }; |
2454 | 0 | return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, allocator); |
2455 | 0 | } |
2456 | | |
2457 | | |
2458 | | const ZSTD_DDict* ZSTD_initStaticDDict( |
2459 | | void* workspace, size_t workspaceSize, |
2460 | | const void* dict, size_t dictSize, |
2461 | | ZSTD_dictLoadMethod_e dictLoadMethod, |
2462 | | ZSTD_dictContentType_e dictContentType) |
2463 | 0 | { |
2464 | 0 | size_t const neededSpace = |
2465 | 0 | sizeof(ZSTD_DDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize); |
2466 | 0 | ZSTD_DDict* const ddict = (ZSTD_DDict*)workspace; |
2467 | 0 | assert(workspace != NULL); |
2468 | 0 | assert(dict != NULL); |
2469 | 0 | if ((size_t)workspace & 7) return NULL; /* 8-aligned */ |
2470 | 0 | if (workspaceSize < neededSpace) return NULL; |
2471 | 0 | if (dictLoadMethod == ZSTD_dlm_byCopy) { |
2472 | 0 | memcpy(ddict+1, dict, dictSize); /* local copy */ |
2473 | 0 | dict = ddict+1; |
2474 | 0 | } |
2475 | 0 | if (ZSTD_isError( ZSTD_initDDict_internal(ddict, dict, dictSize, ZSTD_dlm_byRef, dictContentType) )) |
2476 | 0 | return NULL; |
2477 | 0 | return ddict; |
2478 | 0 | } |
2479 | | |
2480 | | |
2481 | | size_t ZSTD_freeDDict(ZSTD_DDict* ddict) |
2482 | 0 | { |
2483 | 0 | if (ddict==NULL) return 0; /* support free on NULL */ |
2484 | 0 | { ZSTD_customMem const cMem = ddict->cMem; |
2485 | 0 | ZSTD_free(ddict->dictBuffer, cMem); |
2486 | 0 | ZSTD_free(ddict, cMem); |
2487 | 0 | return 0; |
2488 | 0 | } |
2489 | 0 | } |
2490 | | |
2491 | | /*! ZSTD_estimateDDictSize() : |
2492 | | * Estimate amount of memory that will be needed to create a dictionary for decompression. |
2493 | | * Note : dictionary created by reference using ZSTD_dlm_byRef are smaller */ |
2494 | | size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod) |
2495 | 0 | { |
2496 | 0 | return sizeof(ZSTD_DDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize); |
2497 | 0 | } |
2498 | | |
2499 | | size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict) |
2500 | 0 | { |
2501 | 0 | if (ddict==NULL) return 0; /* support sizeof on NULL */ |
2502 | 0 | return sizeof(*ddict) + (ddict->dictBuffer ? ddict->dictSize : 0) ; |
2503 | 0 | } |
2504 | | |
2505 | | /*! ZSTD_getDictID_fromDict() : |
2506 | | * Provides the dictID stored within dictionary. |
2507 | | * if @return == 0, the dictionary is not conformant with Zstandard specification. |
2508 | | * It can still be loaded, but as a content-only dictionary. */ |
2509 | | unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize) |
2510 | 0 | { |
2511 | 0 | if (dictSize < 8) return 0; |
2512 | 0 | if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0; |
2513 | 0 | return MEM_readLE32((const char*)dict + ZSTD_frameIdSize); |
2514 | 0 | } |
2515 | | |
2516 | | /*! ZSTD_getDictID_fromDDict() : |
2517 | | * Provides the dictID of the dictionary loaded into `ddict`. |
2518 | | * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. |
2519 | | * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ |
2520 | | unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict) |
2521 | 0 | { |
2522 | 0 | if (ddict==NULL) return 0; |
2523 | 0 | return ZSTD_getDictID_fromDict(ddict->dictContent, ddict->dictSize); |
2524 | 0 | } |
2525 | | |
2526 | | /*! ZSTD_getDictID_fromFrame() : |
2527 | | * Provides the dictID required to decompresse frame stored within `src`. |
2528 | | * If @return == 0, the dictID could not be decoded. |
2529 | | * This could for one of the following reasons : |
2530 | | * - The frame does not require a dictionary (most common case). |
2531 | | * - The frame was built with dictID intentionally removed. |
2532 | | * Needed dictionary is a hidden information. |
2533 | | * Note : this use case also happens when using a non-conformant dictionary. |
2534 | | * - `srcSize` is too small, and as a result, frame header could not be decoded. |
2535 | | * Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`. |
2536 | | * - This is not a Zstandard frame. |
2537 | | * When identifying the exact failure cause, it's possible to use |
2538 | | * ZSTD_getFrameHeader(), which will provide a more precise error code. */ |
2539 | | unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize) |
2540 | 0 | { |
2541 | 0 | ZSTD_frameHeader zfp = { 0, 0, 0, ZSTD_frame, 0, 0, 0 }; |
2542 | 0 | size_t const hError = ZSTD_getFrameHeader(&zfp, src, srcSize); |
2543 | 0 | if (ZSTD_isError(hError)) return 0; |
2544 | 0 | return zfp.dictID; |
2545 | 0 | } |
2546 | | |
2547 | | |
2548 | | /*! ZSTD_decompress_usingDDict() : |
2549 | | * Decompression using a pre-digested Dictionary |
2550 | | * Use dictionary without significant overhead. */ |
2551 | | size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, |
2552 | | void* dst, size_t dstCapacity, |
2553 | | const void* src, size_t srcSize, |
2554 | | const ZSTD_DDict* ddict) |
2555 | 0 | { |
2556 | | /* pass content and size in case legacy frames are encountered */ |
2557 | 0 | return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, |
2558 | 0 | NULL, 0, |
2559 | 0 | ddict); |
2560 | 0 | } |
2561 | | |
2562 | | |
2563 | | /*===================================== |
2564 | | * Streaming decompression |
2565 | | *====================================*/ |
2566 | | |
2567 | | ZSTD_DStream* ZSTD_createDStream(void) |
2568 | 0 | { |
2569 | 0 | DEBUGLOG(3, "ZSTD_createDStream"); |
2570 | 0 | return ZSTD_createDStream_advanced(ZSTD_defaultCMem); |
2571 | 0 | } |
2572 | | |
2573 | | ZSTD_DStream* ZSTD_initStaticDStream(void *workspace, size_t workspaceSize) |
2574 | 0 | { |
2575 | 0 | return ZSTD_initStaticDCtx(workspace, workspaceSize); |
2576 | 0 | } |
2577 | | |
2578 | | ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem) |
2579 | 0 | { |
2580 | 0 | return ZSTD_createDCtx_advanced(customMem); |
2581 | 0 | } |
2582 | | |
2583 | | size_t ZSTD_freeDStream(ZSTD_DStream* zds) |
2584 | 0 | { |
2585 | 0 | return ZSTD_freeDCtx(zds); |
2586 | 0 | } |
2587 | | |
2588 | | |
2589 | | /* *** Initialization *** */ |
2590 | | |
2591 | 0 | size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; } |
2592 | 0 | size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; } |
2593 | | |
2594 | | size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType) |
2595 | 0 | { |
2596 | 0 | if (dctx->streamStage != zdss_init) return ERROR(stage_wrong); |
2597 | 0 | ZSTD_freeDDict(dctx->ddictLocal); |
2598 | 0 | if (dict && dictSize >= 8) { |
2599 | 0 | dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem); |
2600 | 0 | if (dctx->ddictLocal == NULL) return ERROR(memory_allocation); |
2601 | 0 | } else { |
2602 | 0 | dctx->ddictLocal = NULL; |
2603 | 0 | } |
2604 | 0 | dctx->ddict = dctx->ddictLocal; |
2605 | 0 | return 0; |
2606 | 0 | } |
2607 | | |
2608 | | size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
2609 | 0 | { |
2610 | 0 | return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto); |
2611 | 0 | } |
2612 | | |
2613 | | size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
2614 | 0 | { |
2615 | 0 | return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto); |
2616 | 0 | } |
2617 | | |
2618 | | size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType) |
2619 | 0 | { |
2620 | 0 | return ZSTD_DCtx_loadDictionary_advanced(dctx, prefix, prefixSize, ZSTD_dlm_byRef, dictContentType); |
2621 | 0 | } |
2622 | | |
2623 | | size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize) |
2624 | 0 | { |
2625 | 0 | return ZSTD_DCtx_refPrefix_advanced(dctx, prefix, prefixSize, ZSTD_dct_rawContent); |
2626 | 0 | } |
2627 | | |
2628 | | |
2629 | | /* ZSTD_initDStream_usingDict() : |
2630 | | * return : expected size, aka ZSTD_frameHeaderSize_prefix. |
2631 | | * this function cannot fail */ |
2632 | | size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize) |
2633 | 0 | { |
2634 | 0 | DEBUGLOG(4, "ZSTD_initDStream_usingDict"); |
2635 | 0 | zds->streamStage = zdss_init; |
2636 | 0 | zds->noForwardProgress = 0; |
2637 | 0 | CHECK_F( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) ); |
2638 | 0 | return ZSTD_frameHeaderSize_prefix; |
2639 | 0 | } |
2640 | | |
2641 | | /* note : this variant can't fail */ |
2642 | | size_t ZSTD_initDStream(ZSTD_DStream* zds) |
2643 | 0 | { |
2644 | 0 | DEBUGLOG(4, "ZSTD_initDStream"); |
2645 | 0 | return ZSTD_initDStream_usingDict(zds, NULL, 0); |
2646 | 0 | } |
2647 | | |
2648 | | size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict) |
2649 | 0 | { |
2650 | 0 | if (dctx->streamStage != zdss_init) return ERROR(stage_wrong); |
2651 | 0 | dctx->ddict = ddict; |
2652 | 0 | return 0; |
2653 | 0 | } |
2654 | | |
2655 | | /* ZSTD_initDStream_usingDDict() : |
2656 | | * ddict will just be referenced, and must outlive decompression session |
2657 | | * this function cannot fail */ |
2658 | | size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* dctx, const ZSTD_DDict* ddict) |
2659 | 0 | { |
2660 | 0 | size_t const initResult = ZSTD_initDStream(dctx); |
2661 | 0 | dctx->ddict = ddict; |
2662 | 0 | return initResult; |
2663 | 0 | } |
2664 | | |
2665 | | /* ZSTD_resetDStream() : |
2666 | | * return : expected size, aka ZSTD_frameHeaderSize_prefix. |
2667 | | * this function cannot fail */ |
2668 | | size_t ZSTD_resetDStream(ZSTD_DStream* dctx) |
2669 | 0 | { |
2670 | 0 | DEBUGLOG(4, "ZSTD_resetDStream"); |
2671 | 0 | dctx->streamStage = zdss_loadHeader; |
2672 | 0 | dctx->lhSize = dctx->inPos = dctx->outStart = dctx->outEnd = 0; |
2673 | 0 | dctx->legacyVersion = 0; |
2674 | 0 | dctx->hostageByte = 0; |
2675 | 0 | return ZSTD_frameHeaderSize_prefix; |
2676 | 0 | } |
2677 | | |
2678 | | size_t ZSTD_setDStreamParameter(ZSTD_DStream* dctx, |
2679 | | ZSTD_DStreamParameter_e paramType, unsigned paramValue) |
2680 | 0 | { |
2681 | 0 | if (dctx->streamStage != zdss_init) return ERROR(stage_wrong); |
2682 | 0 | switch(paramType) |
2683 | 0 | { |
2684 | 0 | default : return ERROR(parameter_unsupported); |
2685 | 0 | case DStream_p_maxWindowSize : |
2686 | 0 | DEBUGLOG(4, "setting maxWindowSize = %u KB", paramValue >> 10); |
2687 | 0 | dctx->maxWindowSize = paramValue ? paramValue : (U32)(-1); |
2688 | 0 | break; |
2689 | 0 | } |
2690 | 0 | return 0; |
2691 | 0 | } |
2692 | | |
2693 | | size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize) |
2694 | 0 | { |
2695 | 0 | if (dctx->streamStage != zdss_init) return ERROR(stage_wrong); |
2696 | 0 | dctx->maxWindowSize = maxWindowSize; |
2697 | 0 | return 0; |
2698 | 0 | } |
2699 | | |
2700 | | size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format) |
2701 | 0 | { |
2702 | 0 | DEBUGLOG(4, "ZSTD_DCtx_setFormat : %u", (unsigned)format); |
2703 | 0 | if (dctx->streamStage != zdss_init) return ERROR(stage_wrong); |
2704 | 0 | dctx->format = format; |
2705 | 0 | return 0; |
2706 | 0 | } |
2707 | | |
2708 | | |
2709 | | size_t ZSTD_sizeof_DStream(const ZSTD_DStream* dctx) |
2710 | 0 | { |
2711 | 0 | return ZSTD_sizeof_DCtx(dctx); |
2712 | 0 | } |
2713 | | |
2714 | | size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize) |
2715 | 0 | { |
2716 | 0 | size_t const blockSize = (size_t) MIN(windowSize, ZSTD_BLOCKSIZE_MAX); |
2717 | 0 | unsigned long long const neededRBSize = windowSize + blockSize + (WILDCOPY_OVERLENGTH * 2); |
2718 | 0 | unsigned long long const neededSize = MIN(frameContentSize, neededRBSize); |
2719 | 0 | size_t const minRBSize = (size_t) neededSize; |
2720 | 0 | if ((unsigned long long)minRBSize != neededSize) return ERROR(frameParameter_windowTooLarge); |
2721 | 0 | return minRBSize; |
2722 | 0 | } |
2723 | | |
2724 | | size_t ZSTD_estimateDStreamSize(size_t windowSize) |
2725 | 0 | { |
2726 | 0 | size_t const blockSize = MIN(windowSize, ZSTD_BLOCKSIZE_MAX); |
2727 | 0 | size_t const inBuffSize = blockSize; /* no block can be larger */ |
2728 | 0 | size_t const outBuffSize = ZSTD_decodingBufferSize_min(windowSize, ZSTD_CONTENTSIZE_UNKNOWN); |
2729 | 0 | return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize; |
2730 | 0 | } |
2731 | | |
2732 | | size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize) |
2733 | 0 | { |
2734 | 0 | U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX; /* note : should be user-selectable */ |
2735 | 0 | ZSTD_frameHeader zfh; |
2736 | 0 | size_t const err = ZSTD_getFrameHeader(&zfh, src, srcSize); |
2737 | 0 | if (ZSTD_isError(err)) return err; |
2738 | 0 | if (err>0) return ERROR(srcSize_wrong); |
2739 | 0 | if (zfh.windowSize > windowSizeMax) |
2740 | 0 | return ERROR(frameParameter_windowTooLarge); |
2741 | 0 | return ZSTD_estimateDStreamSize((size_t)zfh.windowSize); |
2742 | 0 | } |
2743 | | |
2744 | | |
2745 | | /* ***** Decompression ***** */ |
2746 | | |
2747 | | MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
2748 | 0 | { |
2749 | 0 | size_t const length = MIN(dstCapacity, srcSize); |
2750 | 0 | memcpy(dst, src, length); |
2751 | 0 | return length; |
2752 | 0 | } |
2753 | | |
2754 | | |
2755 | | size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input) |
2756 | 0 | { |
2757 | 0 | const char* const istart = (const char*)(input->src) + input->pos; |
2758 | 0 | const char* const iend = (const char*)(input->src) + input->size; |
2759 | 0 | const char* ip = istart; |
2760 | 0 | char* const ostart = (char*)(output->dst) + output->pos; |
2761 | 0 | char* const oend = (char*)(output->dst) + output->size; |
2762 | 0 | char* op = ostart; |
2763 | 0 | U32 someMoreWork = 1; |
2764 | |
|
2765 | 0 | DEBUGLOG(5, "ZSTD_decompressStream"); |
2766 | 0 | if (input->pos > input->size) { /* forbidden */ |
2767 | 0 | DEBUGLOG(5, "in: pos: %u vs size: %u", |
2768 | 0 | (U32)input->pos, (U32)input->size); |
2769 | 0 | return ERROR(srcSize_wrong); |
2770 | 0 | } |
2771 | 0 | if (output->pos > output->size) { /* forbidden */ |
2772 | 0 | DEBUGLOG(5, "out: pos: %u vs size: %u", |
2773 | 0 | (U32)output->pos, (U32)output->size); |
2774 | 0 | return ERROR(dstSize_tooSmall); |
2775 | 0 | } |
2776 | 0 | DEBUGLOG(5, "input size : %u", (U32)(input->size - input->pos)); |
2777 | |
|
2778 | 0 | while (someMoreWork) { |
2779 | 0 | switch(zds->streamStage) |
2780 | 0 | { |
2781 | 0 | case zdss_init : |
2782 | 0 | DEBUGLOG(5, "stage zdss_init => transparent reset "); |
2783 | 0 | ZSTD_resetDStream(zds); /* transparent reset on starting decoding a new frame */ |
2784 | | /* fall-through */ |
2785 | |
|
2786 | 0 | case zdss_loadHeader : |
2787 | 0 | DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32)(iend - ip)); |
2788 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
2789 | | if (zds->legacyVersion) { |
2790 | | /* legacy support is incompatible with static dctx */ |
2791 | | if (zds->staticSize) return ERROR(memory_allocation); |
2792 | | { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input); |
2793 | | if (hint==0) zds->streamStage = zdss_init; |
2794 | | return hint; |
2795 | | } } |
2796 | | #endif |
2797 | 0 | { size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format); |
2798 | 0 | DEBUGLOG(5, "header size : %u", (U32)hSize); |
2799 | 0 | if (ZSTD_isError(hSize)) { |
2800 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
2801 | | U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart); |
2802 | | if (legacyVersion) { |
2803 | | const void* const dict = zds->ddict ? zds->ddict->dictContent : NULL; |
2804 | | size_t const dictSize = zds->ddict ? zds->ddict->dictSize : 0; |
2805 | | DEBUGLOG(5, "ZSTD_decompressStream: detected legacy version v0.%u", legacyVersion); |
2806 | | /* legacy support is incompatible with static dctx */ |
2807 | | if (zds->staticSize) return ERROR(memory_allocation); |
2808 | | CHECK_F(ZSTD_initLegacyStream(&zds->legacyContext, |
2809 | | zds->previousLegacyVersion, legacyVersion, |
2810 | | dict, dictSize)); |
2811 | | zds->legacyVersion = zds->previousLegacyVersion = legacyVersion; |
2812 | | { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, legacyVersion, output, input); |
2813 | | if (hint==0) zds->streamStage = zdss_init; /* or stay in stage zdss_loadHeader */ |
2814 | | return hint; |
2815 | | } } |
2816 | | #endif |
2817 | 0 | return hSize; /* error */ |
2818 | 0 | } |
2819 | 0 | if (hSize != 0) { /* need more input */ |
2820 | 0 | size_t const toLoad = hSize - zds->lhSize; /* if hSize!=0, hSize > zds->lhSize */ |
2821 | 0 | size_t const remainingInput = (size_t)(iend-ip); |
2822 | 0 | assert(iend >= ip); |
2823 | 0 | if (toLoad > remainingInput) { /* not enough input to load full header */ |
2824 | 0 | if (remainingInput > 0) { |
2825 | 0 | memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput); |
2826 | 0 | zds->lhSize += remainingInput; |
2827 | 0 | } |
2828 | 0 | input->pos = input->size; |
2829 | 0 | return (MAX(ZSTD_frameHeaderSize_min, hSize) - zds->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */ |
2830 | 0 | } |
2831 | 0 | assert(ip != NULL); |
2832 | 0 | memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad; |
2833 | 0 | break; |
2834 | 0 | } } |
2835 | | |
2836 | | /* check for single-pass mode opportunity */ |
2837 | 0 | if (zds->fParams.frameContentSize && zds->fParams.windowSize /* skippable frame if == 0 */ |
2838 | 0 | && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) { |
2839 | 0 | size_t const cSize = ZSTD_findFrameCompressedSize(istart, iend-istart); |
2840 | 0 | if (cSize <= (size_t)(iend-istart)) { |
2841 | | /* shortcut : using single-pass mode */ |
2842 | 0 | size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, oend-op, istart, cSize, zds->ddict); |
2843 | 0 | if (ZSTD_isError(decompressedSize)) return decompressedSize; |
2844 | 0 | DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()") |
2845 | 0 | ip = istart + cSize; |
2846 | 0 | op += decompressedSize; |
2847 | 0 | zds->expected = 0; |
2848 | 0 | zds->streamStage = zdss_init; |
2849 | 0 | someMoreWork = 0; |
2850 | 0 | break; |
2851 | 0 | } } |
2852 | | |
2853 | | /* Consume header (see ZSTDds_decodeFrameHeader) */ |
2854 | 0 | DEBUGLOG(4, "Consume header"); |
2855 | 0 | CHECK_F(ZSTD_decompressBegin_usingDDict(zds, zds->ddict)); |
2856 | |
|
2857 | 0 | if ((MEM_readLE32(zds->headerBuffer) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */ |
2858 | 0 | zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_frameIdSize); |
2859 | 0 | zds->stage = ZSTDds_skipFrame; |
2860 | 0 | } else { |
2861 | 0 | CHECK_F(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize)); |
2862 | 0 | zds->expected = ZSTD_blockHeaderSize; |
2863 | 0 | zds->stage = ZSTDds_decodeBlockHeader; |
2864 | 0 | } |
2865 | | |
2866 | | /* control buffer memory usage */ |
2867 | 0 | DEBUGLOG(4, "Control max memory usage (%u KB <= max %u KB)", |
2868 | 0 | (U32)(zds->fParams.windowSize >>10), |
2869 | 0 | (U32)(zds->maxWindowSize >> 10) ); |
2870 | 0 | zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN); |
2871 | 0 | if (zds->fParams.windowSize > zds->maxWindowSize) return ERROR(frameParameter_windowTooLarge); |
2872 | | |
2873 | | /* Adapt buffer sizes to frame header instructions */ |
2874 | 0 | { size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */); |
2875 | 0 | size_t const neededOutBuffSize = ZSTD_decodingBufferSize_min(zds->fParams.windowSize, zds->fParams.frameContentSize); |
2876 | 0 | if ((zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize)) { |
2877 | 0 | size_t const bufferSize = neededInBuffSize + neededOutBuffSize; |
2878 | 0 | DEBUGLOG(4, "inBuff : from %u to %u", |
2879 | 0 | (U32)zds->inBuffSize, (U32)neededInBuffSize); |
2880 | 0 | DEBUGLOG(4, "outBuff : from %u to %u", |
2881 | 0 | (U32)zds->outBuffSize, (U32)neededOutBuffSize); |
2882 | 0 | if (zds->staticSize) { /* static DCtx */ |
2883 | 0 | DEBUGLOG(4, "staticSize : %u", (U32)zds->staticSize); |
2884 | 0 | assert(zds->staticSize >= sizeof(ZSTD_DCtx)); /* controlled at init */ |
2885 | 0 | if (bufferSize > zds->staticSize - sizeof(ZSTD_DCtx)) |
2886 | 0 | return ERROR(memory_allocation); |
2887 | 0 | } else { |
2888 | 0 | ZSTD_free(zds->inBuff, zds->customMem); |
2889 | 0 | zds->inBuffSize = 0; |
2890 | 0 | zds->outBuffSize = 0; |
2891 | 0 | zds->inBuff = (char*)ZSTD_malloc(bufferSize, zds->customMem); |
2892 | 0 | if (zds->inBuff == NULL) return ERROR(memory_allocation); |
2893 | 0 | } |
2894 | 0 | zds->inBuffSize = neededInBuffSize; |
2895 | 0 | zds->outBuff = zds->inBuff + zds->inBuffSize; |
2896 | 0 | zds->outBuffSize = neededOutBuffSize; |
2897 | 0 | } } |
2898 | 0 | zds->streamStage = zdss_read; |
2899 | | /* fall-through */ |
2900 | |
|
2901 | 0 | case zdss_read: |
2902 | 0 | DEBUGLOG(5, "stage zdss_read"); |
2903 | 0 | { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds); |
2904 | 0 | DEBUGLOG(5, "neededInSize = %u", (U32)neededInSize); |
2905 | 0 | if (neededInSize==0) { /* end of frame */ |
2906 | 0 | zds->streamStage = zdss_init; |
2907 | 0 | someMoreWork = 0; |
2908 | 0 | break; |
2909 | 0 | } |
2910 | 0 | if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */ |
2911 | 0 | int const isSkipFrame = ZSTD_isSkipFrame(zds); |
2912 | 0 | size_t const decodedSize = ZSTD_decompressContinue(zds, |
2913 | 0 | zds->outBuff + zds->outStart, (isSkipFrame ? 0 : zds->outBuffSize - zds->outStart), |
2914 | 0 | ip, neededInSize); |
2915 | 0 | if (ZSTD_isError(decodedSize)) return decodedSize; |
2916 | 0 | ip += neededInSize; |
2917 | 0 | if (!decodedSize && !isSkipFrame) break; /* this was just a header */ |
2918 | 0 | zds->outEnd = zds->outStart + decodedSize; |
2919 | 0 | zds->streamStage = zdss_flush; |
2920 | 0 | break; |
2921 | 0 | } } |
2922 | 0 | if (ip==iend) { someMoreWork = 0; break; } /* no more input */ |
2923 | 0 | zds->streamStage = zdss_load; |
2924 | | /* fall-through */ |
2925 | |
|
2926 | 0 | case zdss_load: |
2927 | 0 | { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds); |
2928 | 0 | size_t const toLoad = neededInSize - zds->inPos; |
2929 | 0 | int const isSkipFrame = ZSTD_isSkipFrame(zds); |
2930 | 0 | size_t loadedSize; |
2931 | 0 | if (isSkipFrame) { |
2932 | 0 | loadedSize = MIN(toLoad, (size_t)(iend-ip)); |
2933 | 0 | } else { |
2934 | 0 | if (toLoad > zds->inBuffSize - zds->inPos) return ERROR(corruption_detected); /* should never happen */ |
2935 | 0 | loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, iend-ip); |
2936 | 0 | } |
2937 | 0 | ip += loadedSize; |
2938 | 0 | zds->inPos += loadedSize; |
2939 | 0 | if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */ |
2940 | | |
2941 | | /* decode loaded input */ |
2942 | 0 | { size_t const decodedSize = ZSTD_decompressContinue(zds, |
2943 | 0 | zds->outBuff + zds->outStart, zds->outBuffSize - zds->outStart, |
2944 | 0 | zds->inBuff, neededInSize); |
2945 | 0 | if (ZSTD_isError(decodedSize)) return decodedSize; |
2946 | 0 | zds->inPos = 0; /* input is consumed */ |
2947 | 0 | if (!decodedSize && !isSkipFrame) { zds->streamStage = zdss_read; break; } /* this was just a header */ |
2948 | 0 | zds->outEnd = zds->outStart + decodedSize; |
2949 | 0 | } } |
2950 | 0 | zds->streamStage = zdss_flush; |
2951 | | /* fall-through */ |
2952 | |
|
2953 | 0 | case zdss_flush: |
2954 | 0 | { size_t const toFlushSize = zds->outEnd - zds->outStart; |
2955 | 0 | size_t const flushedSize = ZSTD_limitCopy(op, oend-op, zds->outBuff + zds->outStart, toFlushSize); |
2956 | 0 | op += flushedSize; |
2957 | 0 | zds->outStart += flushedSize; |
2958 | 0 | if (flushedSize == toFlushSize) { /* flush completed */ |
2959 | 0 | zds->streamStage = zdss_read; |
2960 | 0 | if ( (zds->outBuffSize < zds->fParams.frameContentSize) |
2961 | 0 | && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize) ) { |
2962 | 0 | DEBUGLOG(5, "restart filling outBuff from beginning (left:%i, needed:%u)", |
2963 | 0 | (int)(zds->outBuffSize - zds->outStart), |
2964 | 0 | (U32)zds->fParams.blockSizeMax); |
2965 | 0 | zds->outStart = zds->outEnd = 0; |
2966 | 0 | } |
2967 | 0 | break; |
2968 | 0 | } } |
2969 | | /* cannot complete flush */ |
2970 | 0 | someMoreWork = 0; |
2971 | 0 | break; |
2972 | | |
2973 | 0 | default: return ERROR(GENERIC); /* impossible */ |
2974 | 0 | } } |
2975 | | |
2976 | | /* result */ |
2977 | 0 | input->pos = (size_t)(ip - (const char*)(input->src)); |
2978 | 0 | output->pos = (size_t)(op - (char*)(output->dst)); |
2979 | 0 | if ((ip==istart) && (op==ostart)) { /* no forward progress */ |
2980 | 0 | zds->noForwardProgress ++; |
2981 | 0 | if (zds->noForwardProgress >= ZSTD_NO_FORWARD_PROGRESS_MAX) { |
2982 | 0 | if (op==oend) return ERROR(dstSize_tooSmall); |
2983 | 0 | if (ip==iend) return ERROR(srcSize_wrong); |
2984 | 0 | assert(0); |
2985 | 0 | } |
2986 | 0 | } else { |
2987 | 0 | zds->noForwardProgress = 0; |
2988 | 0 | } |
2989 | 0 | { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds); |
2990 | 0 | if (!nextSrcSizeHint) { /* frame fully decoded */ |
2991 | 0 | if (zds->outEnd == zds->outStart) { /* output fully flushed */ |
2992 | 0 | if (zds->hostageByte) { |
2993 | 0 | if (input->pos >= input->size) { |
2994 | | /* can't release hostage (not present) */ |
2995 | 0 | zds->streamStage = zdss_read; |
2996 | 0 | return 1; |
2997 | 0 | } |
2998 | 0 | input->pos++; /* release hostage */ |
2999 | 0 | } /* zds->hostageByte */ |
3000 | 0 | return 0; |
3001 | 0 | } /* zds->outEnd == zds->outStart */ |
3002 | 0 | if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */ |
3003 | 0 | input->pos--; /* note : pos > 0, otherwise, impossible to finish reading last block */ |
3004 | 0 | zds->hostageByte=1; |
3005 | 0 | } |
3006 | 0 | return 1; |
3007 | 0 | } /* nextSrcSizeHint==0 */ |
3008 | 0 | nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block); /* preload header of next block */ |
3009 | 0 | assert(zds->inPos <= nextSrcSizeHint); |
3010 | 0 | nextSrcSizeHint -= zds->inPos; /* part already loaded*/ |
3011 | 0 | return nextSrcSizeHint; |
3012 | 0 | } |
3013 | 0 | } |
3014 | | |
3015 | | |
3016 | | size_t ZSTD_decompress_generic(ZSTD_DCtx* dctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input) |
3017 | 0 | { |
3018 | 0 | return ZSTD_decompressStream(dctx, output, input); |
3019 | 0 | } |
3020 | | |
3021 | | size_t ZSTD_decompress_generic_simpleArgs ( |
3022 | | ZSTD_DCtx* dctx, |
3023 | | void* dst, size_t dstCapacity, size_t* dstPos, |
3024 | | const void* src, size_t srcSize, size_t* srcPos) |
3025 | 0 | { |
3026 | 0 | ZSTD_outBuffer output = { dst, dstCapacity, *dstPos }; |
3027 | 0 | ZSTD_inBuffer input = { src, srcSize, *srcPos }; |
3028 | | /* ZSTD_compress_generic() will check validity of dstPos and srcPos */ |
3029 | 0 | size_t const cErr = ZSTD_decompress_generic(dctx, &output, &input); |
3030 | 0 | *dstPos = output.pos; |
3031 | 0 | *srcPos = input.pos; |
3032 | 0 | return cErr; |
3033 | 0 | } |
3034 | | |
3035 | | void ZSTD_DCtx_reset(ZSTD_DCtx* dctx) |
3036 | 0 | { |
3037 | 0 | (void)ZSTD_initDStream(dctx); |
3038 | 0 | dctx->format = ZSTD_f_zstd1; |
3039 | 0 | dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT; |
3040 | 0 | } |