comparison contrib/python-zstandard/zstd/decompress/zstd_decompress.c @ 40121:73fef626dae3

zstandard: vendor python-zstandard 0.10.1 This was just released. The upstream source distribution from PyPI was extracted. Unwanted files were removed. The clang-format ignore list was updated to reflect the new source of files. setup.py was updated to pass a new argument to python-zstandard's function for returning an Extension instance. Upstream had to change to use relative paths because Python 3.7's packaging doesn't seem to like absolute paths when defining sources, includes, etc. The default relative path calculation is relative to setup_zstd.py which is different from the directory of Mercurial's setup.py. The project contains a vendored copy of zstandard 1.3.6. The old version was 1.3.4. The API should be backwards compatible and nothing in core should need adjusted. However, there is a new "chunker" API that we may find useful in places where we want to emit compressed chunks of a fixed size. There are a pair of bug fixes in 0.10.0 with regards to compressobj() and decompressobj() when block flushing is used. I actually found these bugs when introducing these APIs in Mercurial! But existing Mercurial code is not affected because we don't perform block flushing. # no-check-commit because 3rd party code has different style guidelines Differential Revision: https://phab.mercurial-scm.org/D4911
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 08 Oct 2018 16:27:40 -0700
parents b1fb341d8a61
children 675775c33ab6
comparison
equal deleted inserted replaced
40120:89742f1fa6cb 40121:73fef626dae3
38 */ 38 */
39 #ifndef ZSTD_MAXWINDOWSIZE_DEFAULT 39 #ifndef ZSTD_MAXWINDOWSIZE_DEFAULT
40 # define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_DEFAULTMAX) + 1) 40 # define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_DEFAULTMAX) + 1)
41 #endif 41 #endif
42 42
43 /*!
44 * NO_FORWARD_PROGRESS_MAX :
45 * maximum allowed nb of calls to ZSTD_decompressStream() and ZSTD_decompress_generic()
46 * without any forward progress
47 * (defined as: no byte read from input, and no byte flushed to output)
48 * before triggering an error.
49 */
50 #ifndef ZSTD_NO_FORWARD_PROGRESS_MAX
51 # define ZSTD_NO_FORWARD_PROGRESS_MAX 16
52 #endif
53
43 54
44 /*-******************************************************* 55 /*-*******************************************************
45 * Dependencies 56 * Dependencies
46 *********************************************************/ 57 *********************************************************/
47 #include <string.h> /* memcpy, memmove, memset */ 58 #include <string.h> /* memcpy, memmove, memset */
48 #include "cpu.h" 59 #include "compiler.h" /* prefetch */
60 #include "cpu.h" /* bmi2 */
49 #include "mem.h" /* low level memory routines */ 61 #include "mem.h" /* low level memory routines */
50 #define FSE_STATIC_LINKING_ONLY 62 #define FSE_STATIC_LINKING_ONLY
51 #include "fse.h" 63 #include "fse.h"
52 #define HUF_STATIC_LINKING_ONLY 64 #define HUF_STATIC_LINKING_ONLY
53 #include "huf.h" 65 #include "huf.h"
54 #include "zstd_internal.h" 66 #include "zstd_internal.h"
55 67
56 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) 68 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
57 # include "zstd_legacy.h" 69 # include "zstd_legacy.h"
58 #endif 70 #endif
71
72 static const void* ZSTD_DDictDictContent(const ZSTD_DDict* ddict);
73 static size_t ZSTD_DDictDictSize(const ZSTD_DDict* ddict);
59 74
60 75
61 /*-************************************* 76 /*-*************************************
62 * Errors 77 * Errors
63 ***************************************/ 78 ***************************************/
97 } ZSTD_seqSymbol; 112 } ZSTD_seqSymbol;
98 113
99 #define SEQSYMBOL_TABLE_SIZE(log) (1 + (1 << (log))) 114 #define SEQSYMBOL_TABLE_SIZE(log) (1 + (1 << (log)))
100 115
101 typedef struct { 116 typedef struct {
102 ZSTD_seqSymbol LLTable[SEQSYMBOL_TABLE_SIZE(LLFSELog)]; 117 ZSTD_seqSymbol LLTable[SEQSYMBOL_TABLE_SIZE(LLFSELog)]; /* Note : Space reserved for FSE Tables */
103 ZSTD_seqSymbol OFTable[SEQSYMBOL_TABLE_SIZE(OffFSELog)]; 118 ZSTD_seqSymbol OFTable[SEQSYMBOL_TABLE_SIZE(OffFSELog)]; /* is also used as temporary workspace while building hufTable during DDict creation */
104 ZSTD_seqSymbol MLTable[SEQSYMBOL_TABLE_SIZE(MLFSELog)]; 119 ZSTD_seqSymbol MLTable[SEQSYMBOL_TABLE_SIZE(MLFSELog)]; /* and therefore must be at least HUF_DECOMPRESS_WORKSPACE_SIZE large */
105 HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)]; /* can accommodate HUF_decompress4X */ 120 HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)]; /* can accommodate HUF_decompress4X */
106 U32 workspace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
107 U32 rep[ZSTD_REP_NUM]; 121 U32 rep[ZSTD_REP_NUM];
108 } ZSTD_entropyDTables_t; 122 } ZSTD_entropyDTables_t;
109 123
110 struct ZSTD_DCtx_s 124 struct ZSTD_DCtx_s
111 { 125 {
112 const ZSTD_seqSymbol* LLTptr; 126 const ZSTD_seqSymbol* LLTptr;
113 const ZSTD_seqSymbol* MLTptr; 127 const ZSTD_seqSymbol* MLTptr;
114 const ZSTD_seqSymbol* OFTptr; 128 const ZSTD_seqSymbol* OFTptr;
115 const HUF_DTable* HUFptr; 129 const HUF_DTable* HUFptr;
116 ZSTD_entropyDTables_t entropy; 130 ZSTD_entropyDTables_t entropy;
131 U32 workspace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; /* space needed when building huffman tables */
117 const void* previousDstEnd; /* detect continuity */ 132 const void* previousDstEnd; /* detect continuity */
118 const void* base; /* start of current segment */ 133 const void* prefixStart; /* start of current segment */
119 const void* vBase; /* virtual start of previous segment if it was just before current one */ 134 const void* virtualStart; /* virtual start of previous segment if it was just before current one */
120 const void* dictEnd; /* end of previous segment */ 135 const void* dictEnd; /* end of previous segment */
121 size_t expected; 136 size_t expected;
122 ZSTD_frameHeader fParams; 137 ZSTD_frameHeader fParams;
123 U64 decodedSize; 138 U64 decodedSize;
124 blockType_e bType; /* used in ZSTD_decompressContinue(), store blockType between block header decoding and block decompression stages */ 139 blockType_e bType; /* used in ZSTD_decompressContinue(), store blockType between block header decoding and block decompression stages */
125 ZSTD_dStage stage; 140 ZSTD_dStage stage;
126 U32 litEntropy; 141 U32 litEntropy;
127 U32 fseEntropy; 142 U32 fseEntropy;
128 XXH64_state_t xxhState; 143 XXH64_state_t xxhState;
129 size_t headerSize; 144 size_t headerSize;
130 U32 dictID;
131 ZSTD_format_e format; 145 ZSTD_format_e format;
132 const BYTE* litPtr; 146 const BYTE* litPtr;
133 ZSTD_customMem customMem; 147 ZSTD_customMem customMem;
134 size_t litSize; 148 size_t litSize;
135 size_t rleSize; 149 size_t rleSize;
136 size_t staticSize; 150 size_t staticSize;
137 int bmi2; /* == 1 if the CPU supports BMI2 and 0 otherwise. CPU support is determined dynamically once per context lifetime. */ 151 int bmi2; /* == 1 if the CPU supports BMI2 and 0 otherwise. CPU support is determined dynamically once per context lifetime. */
138 152
153 /* dictionary */
154 ZSTD_DDict* ddictLocal;
155 const ZSTD_DDict* ddict; /* set by ZSTD_initDStream_usingDDict(), or ZSTD_DCtx_refDDict() */
156 U32 dictID;
157 int ddictIsCold; /* if == 1 : dictionary is "new" for working context, and presumed "cold" (not in cpu cache) */
158
139 /* streaming */ 159 /* streaming */
140 ZSTD_DDict* ddictLocal;
141 const ZSTD_DDict* ddict;
142 ZSTD_dStreamStage streamStage; 160 ZSTD_dStreamStage streamStage;
143 char* inBuff; 161 char* inBuff;
144 size_t inBuffSize; 162 size_t inBuffSize;
145 size_t inPos; 163 size_t inPos;
146 size_t maxWindowSize; 164 size_t maxWindowSize;
151 size_t lhSize; 169 size_t lhSize;
152 void* legacyContext; 170 void* legacyContext;
153 U32 previousLegacyVersion; 171 U32 previousLegacyVersion;
154 U32 legacyVersion; 172 U32 legacyVersion;
155 U32 hostageByte; 173 U32 hostageByte;
174 int noForwardProgress;
156 175
157 /* workspace */ 176 /* workspace */
158 BYTE litBuffer[ZSTD_BLOCKSIZE_MAX + WILDCOPY_OVERLENGTH]; 177 BYTE litBuffer[ZSTD_BLOCKSIZE_MAX + WILDCOPY_OVERLENGTH];
159 BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX]; 178 BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
160 }; /* typedef'd to ZSTD_DCtx within "zstd.h" */ 179 }; /* typedef'd to ZSTD_DCtx within "zstd.h" */
171 190
172 191
173 static size_t ZSTD_startingInputLength(ZSTD_format_e format) 192 static size_t ZSTD_startingInputLength(ZSTD_format_e format)
174 { 193 {
175 size_t const startingInputLength = (format==ZSTD_f_zstd1_magicless) ? 194 size_t const startingInputLength = (format==ZSTD_f_zstd1_magicless) ?
176 ZSTD_frameHeaderSize_prefix - ZSTD_frameIdSize : 195 ZSTD_frameHeaderSize_prefix - ZSTD_FRAMEIDSIZE :
177 ZSTD_frameHeaderSize_prefix; 196 ZSTD_frameHeaderSize_prefix;
178 ZSTD_STATIC_ASSERT(ZSTD_FRAMEHEADERSIZE_PREFIX >= ZSTD_FRAMEIDSIZE); 197 ZSTD_STATIC_ASSERT(ZSTD_FRAMEHEADERSIZE_PREFIX >= ZSTD_FRAMEIDSIZE);
179 /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */ 198 /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */
180 assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) ); 199 assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) );
181 return startingInputLength; 200 return startingInputLength;
186 dctx->format = ZSTD_f_zstd1; /* ZSTD_decompressBegin() invokes ZSTD_startingInputLength() with argument dctx->format */ 205 dctx->format = ZSTD_f_zstd1; /* ZSTD_decompressBegin() invokes ZSTD_startingInputLength() with argument dctx->format */
187 dctx->staticSize = 0; 206 dctx->staticSize = 0;
188 dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT; 207 dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
189 dctx->ddict = NULL; 208 dctx->ddict = NULL;
190 dctx->ddictLocal = NULL; 209 dctx->ddictLocal = NULL;
210 dctx->dictEnd = NULL;
211 dctx->ddictIsCold = 0;
191 dctx->inBuff = NULL; 212 dctx->inBuff = NULL;
192 dctx->inBuffSize = 0; 213 dctx->inBuffSize = 0;
193 dctx->outBuffSize = 0; 214 dctx->outBuffSize = 0;
194 dctx->streamStage = zdss_init; 215 dctx->streamStage = zdss_init;
216 dctx->legacyContext = NULL;
217 dctx->previousLegacyVersion = 0;
218 dctx->noForwardProgress = 0;
195 dctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid()); 219 dctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid());
196 } 220 }
197 221
198 ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize) 222 ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize)
199 { 223 {
213 if (!customMem.customAlloc ^ !customMem.customFree) return NULL; 237 if (!customMem.customAlloc ^ !customMem.customFree) return NULL;
214 238
215 { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(*dctx), customMem); 239 { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(*dctx), customMem);
216 if (!dctx) return NULL; 240 if (!dctx) return NULL;
217 dctx->customMem = customMem; 241 dctx->customMem = customMem;
218 dctx->legacyContext = NULL;
219 dctx->previousLegacyVersion = 0;
220 ZSTD_initDCtx_internal(dctx); 242 ZSTD_initDCtx_internal(dctx);
221 return dctx; 243 return dctx;
222 } 244 }
223 } 245 }
224 246
263 * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. 285 * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
264 * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. 286 * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
265 * Note 3 : Skippable Frame Identifiers are considered valid. */ 287 * Note 3 : Skippable Frame Identifiers are considered valid. */
266 unsigned ZSTD_isFrame(const void* buffer, size_t size) 288 unsigned ZSTD_isFrame(const void* buffer, size_t size)
267 { 289 {
268 if (size < ZSTD_frameIdSize) return 0; 290 if (size < ZSTD_FRAMEIDSIZE) return 0;
269 { U32 const magic = MEM_readLE32(buffer); 291 { U32 const magic = MEM_readLE32(buffer);
270 if (magic == ZSTD_MAGICNUMBER) return 1; 292 if (magic == ZSTD_MAGICNUMBER) return 1;
271 if ((magic & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) return 1; 293 if ((magic & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) return 1;
272 } 294 }
273 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) 295 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
296 } 318 }
297 } 319 }
298 320
299 /** ZSTD_frameHeaderSize() : 321 /** ZSTD_frameHeaderSize() :
300 * srcSize must be >= ZSTD_frameHeaderSize_prefix. 322 * srcSize must be >= ZSTD_frameHeaderSize_prefix.
301 * @return : size of the Frame Header */ 323 * @return : size of the Frame Header,
324 * or an error code (if srcSize is too small) */
302 size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize) 325 size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize)
303 { 326 {
304 return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1); 327 return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1);
305 } 328 }
306 329
307 330
308 /** ZSTD_getFrameHeader_internal() : 331 /** ZSTD_getFrameHeader_advanced() :
309 * decode Frame Header, or require larger `srcSize`. 332 * decode Frame Header, or require larger `srcSize`.
310 * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless 333 * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless
311 * @return : 0, `zfhPtr` is correctly filled, 334 * @return : 0, `zfhPtr` is correctly filled,
312 * >0, `srcSize` is too small, value is wanted `srcSize` amount, 335 * >0, `srcSize` is too small, value is wanted `srcSize` amount,
313 * or an error code, which can be tested using ZSTD_isError() */ 336 * or an error code, which can be tested using ZSTD_isError() */
314 static size_t ZSTD_getFrameHeader_internal(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format) 337 size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format)
315 { 338 {
316 const BYTE* ip = (const BYTE*)src; 339 const BYTE* ip = (const BYTE*)src;
317 size_t const minInputSize = ZSTD_startingInputLength(format); 340 size_t const minInputSize = ZSTD_startingInputLength(format);
318 341
342 memset(zfhPtr, 0, sizeof(*zfhPtr)); /* not strictly necessary, but static analyzer do not understand that zfhPtr is only going to be read only if return value is zero, since they are 2 different signals */
319 if (srcSize < minInputSize) return minInputSize; 343 if (srcSize < minInputSize) return minInputSize;
344 if (src==NULL) return ERROR(GENERIC); /* invalid parameter */
320 345
321 if ( (format != ZSTD_f_zstd1_magicless) 346 if ( (format != ZSTD_f_zstd1_magicless)
322 && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) { 347 && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) {
323 if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { 348 if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) {
324 /* skippable frame */ 349 /* skippable frame */
325 if (srcSize < ZSTD_skippableHeaderSize) 350 if (srcSize < ZSTD_skippableHeaderSize)
326 return ZSTD_skippableHeaderSize; /* magic number + frame length */ 351 return ZSTD_skippableHeaderSize; /* magic number + frame length */
327 memset(zfhPtr, 0, sizeof(*zfhPtr)); 352 memset(zfhPtr, 0, sizeof(*zfhPtr));
328 zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_frameIdSize); 353 zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_FRAMEIDSIZE);
329 zfhPtr->frameType = ZSTD_skippableFrame; 354 zfhPtr->frameType = ZSTD_skippableFrame;
330 return 0; 355 return 0;
331 } 356 }
332 return ERROR(prefix_unknown); 357 return ERROR(prefix_unknown);
333 } 358 }
392 * @return : 0, `zfhPtr` is correctly filled, 417 * @return : 0, `zfhPtr` is correctly filled,
393 * >0, `srcSize` is too small, value is wanted `srcSize` amount, 418 * >0, `srcSize` is too small, value is wanted `srcSize` amount,
394 * or an error code, which can be tested using ZSTD_isError() */ 419 * or an error code, which can be tested using ZSTD_isError() */
395 size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize) 420 size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize)
396 { 421 {
397 return ZSTD_getFrameHeader_internal(zfhPtr, src, srcSize, ZSTD_f_zstd1); 422 return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_f_zstd1);
398 } 423 }
399 424
400 425
401 /** ZSTD_getFrameContentSize() : 426 /** ZSTD_getFrameContentSize() :
402 * compatible with legacy mode 427 * compatible with legacy mode
435 460
436 if ((magicNumber & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { 461 if ((magicNumber & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) {
437 size_t skippableSize; 462 size_t skippableSize;
438 if (srcSize < ZSTD_skippableHeaderSize) 463 if (srcSize < ZSTD_skippableHeaderSize)
439 return ERROR(srcSize_wrong); 464 return ERROR(srcSize_wrong);
440 skippableSize = MEM_readLE32((const BYTE *)src + ZSTD_frameIdSize) 465 skippableSize = MEM_readLE32((const BYTE *)src + ZSTD_FRAMEIDSIZE)
441 + ZSTD_skippableHeaderSize; 466 + ZSTD_skippableHeaderSize;
442 if (srcSize < skippableSize) { 467 if (srcSize < skippableSize) {
443 return ZSTD_CONTENTSIZE_ERROR; 468 return ZSTD_CONTENTSIZE_ERROR;
444 } 469 }
445 470
489 /** ZSTD_decodeFrameHeader() : 514 /** ZSTD_decodeFrameHeader() :
490 * `headerSize` must be the size provided by ZSTD_frameHeaderSize(). 515 * `headerSize` must be the size provided by ZSTD_frameHeaderSize().
491 * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */ 516 * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
492 static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize) 517 static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize)
493 { 518 {
494 size_t const result = ZSTD_getFrameHeader_internal(&(dctx->fParams), src, headerSize, dctx->format); 519 size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format);
495 if (ZSTD_isError(result)) return result; /* invalid header */ 520 if (ZSTD_isError(result)) return result; /* invalid header */
496 if (result>0) return ERROR(srcSize_wrong); /* headerSize too small */ 521 if (result>0) return ERROR(srcSize_wrong); /* headerSize too small */
497 if (dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID)) 522 if (dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID))
498 return ERROR(dictionary_wrong); 523 return ERROR(dictionary_wrong);
499 if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0); 524 if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0);
524 549
525 550
526 static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity, 551 static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity,
527 const void* src, size_t srcSize) 552 const void* src, size_t srcSize)
528 { 553 {
554 if (dst==NULL) return ERROR(dstSize_tooSmall);
529 if (srcSize > dstCapacity) return ERROR(dstSize_tooSmall); 555 if (srcSize > dstCapacity) return ERROR(dstSize_tooSmall);
530 memcpy(dst, src, srcSize); 556 memcpy(dst, src, srcSize);
531 return srcSize; 557 return srcSize;
532 } 558 }
533 559
540 if (regenSize > dstCapacity) return ERROR(dstSize_tooSmall); 566 if (regenSize > dstCapacity) return ERROR(dstSize_tooSmall);
541 memset(dst, *(const BYTE*)src, regenSize); 567 memset(dst, *(const BYTE*)src, regenSize);
542 return regenSize; 568 return regenSize;
543 } 569 }
544 570
571 /* Hidden declaration for fullbench */
572 size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
573 const void* src, size_t srcSize);
545 /*! ZSTD_decodeLiteralsBlock() : 574 /*! ZSTD_decodeLiteralsBlock() :
546 * @return : nb of bytes read from src (< srcSize ) 575 * @return : nb of bytes read from src (< srcSize )
547 * note : symbol not declared but exposed for fullbench */ 576 * note : symbol not declared but exposed for fullbench */
548 size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx, 577 size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
549 const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */ 578 const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
556 switch(litEncType) 585 switch(litEncType)
557 { 586 {
558 case set_repeat: 587 case set_repeat:
559 if (dctx->litEntropy==0) return ERROR(dictionary_corrupted); 588 if (dctx->litEntropy==0) return ERROR(dictionary_corrupted);
560 /* fall-through */ 589 /* fall-through */
590
561 case set_compressed: 591 case set_compressed:
562 if (srcSize < 5) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3 */ 592 if (srcSize < 5) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3 */
563 { size_t lhSize, litSize, litCSize; 593 { size_t lhSize, litSize, litCSize;
564 U32 singleStream=0; 594 U32 singleStream=0;
565 U32 const lhlCode = (istart[0] >> 2) & 3; 595 U32 const lhlCode = (istart[0] >> 2) & 3;
587 break; 617 break;
588 } 618 }
589 if (litSize > ZSTD_BLOCKSIZE_MAX) return ERROR(corruption_detected); 619 if (litSize > ZSTD_BLOCKSIZE_MAX) return ERROR(corruption_detected);
590 if (litCSize + lhSize > srcSize) return ERROR(corruption_detected); 620 if (litCSize + lhSize > srcSize) return ERROR(corruption_detected);
591 621
622 /* prefetch huffman table if cold */
623 if (dctx->ddictIsCold && (litSize > 768 /* heuristic */)) {
624 PREFETCH_AREA(dctx->HUFptr, sizeof(dctx->entropy.hufTable));
625 }
626
592 if (HUF_isError((litEncType==set_repeat) ? 627 if (HUF_isError((litEncType==set_repeat) ?
593 ( singleStream ? 628 ( singleStream ?
594 HUF_decompress1X_usingDTable_bmi2(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->HUFptr, dctx->bmi2) : 629 HUF_decompress1X_usingDTable_bmi2(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->HUFptr, dctx->bmi2) :
595 HUF_decompress4X_usingDTable_bmi2(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->HUFptr, dctx->bmi2) ) : 630 HUF_decompress4X_usingDTable_bmi2(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->HUFptr, dctx->bmi2) ) :
596 ( singleStream ? 631 ( singleStream ?
597 HUF_decompress1X2_DCtx_wksp_bmi2(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize, 632 HUF_decompress1X1_DCtx_wksp_bmi2(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize,
598 dctx->entropy.workspace, sizeof(dctx->entropy.workspace), dctx->bmi2) : 633 dctx->workspace, sizeof(dctx->workspace), dctx->bmi2) :
599 HUF_decompress4X_hufOnly_wksp_bmi2(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize, 634 HUF_decompress4X_hufOnly_wksp_bmi2(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize,
600 dctx->entropy.workspace, sizeof(dctx->entropy.workspace), dctx->bmi2)))) 635 dctx->workspace, sizeof(dctx->workspace), dctx->bmi2))))
601 return ERROR(corruption_detected); 636 return ERROR(corruption_detected);
602 637
603 dctx->litPtr = dctx->litBuffer; 638 dctx->litPtr = dctx->litBuffer;
604 dctx->litSize = litSize; 639 dctx->litSize = litSize;
605 dctx->litEntropy = 1; 640 dctx->litEntropy = 1;
867 * or an error code if it fails */ 902 * or an error code if it fails */
868 static size_t ZSTD_buildSeqTable(ZSTD_seqSymbol* DTableSpace, const ZSTD_seqSymbol** DTablePtr, 903 static size_t ZSTD_buildSeqTable(ZSTD_seqSymbol* DTableSpace, const ZSTD_seqSymbol** DTablePtr,
869 symbolEncodingType_e type, U32 max, U32 maxLog, 904 symbolEncodingType_e type, U32 max, U32 maxLog,
870 const void* src, size_t srcSize, 905 const void* src, size_t srcSize,
871 const U32* baseValue, const U32* nbAdditionalBits, 906 const U32* baseValue, const U32* nbAdditionalBits,
872 const ZSTD_seqSymbol* defaultTable, U32 flagRepeatTable) 907 const ZSTD_seqSymbol* defaultTable, U32 flagRepeatTable,
908 int ddictIsCold, int nbSeq)
873 { 909 {
874 switch(type) 910 switch(type)
875 { 911 {
876 case set_rle : 912 case set_rle :
877 if (!srcSize) return ERROR(srcSize_wrong); 913 if (!srcSize) return ERROR(srcSize_wrong);
886 case set_basic : 922 case set_basic :
887 *DTablePtr = defaultTable; 923 *DTablePtr = defaultTable;
888 return 0; 924 return 0;
889 case set_repeat: 925 case set_repeat:
890 if (!flagRepeatTable) return ERROR(corruption_detected); 926 if (!flagRepeatTable) return ERROR(corruption_detected);
927 /* prefetch FSE table if used */
928 if (ddictIsCold && (nbSeq > 24 /* heuristic */)) {
929 const void* const pStart = *DTablePtr;
930 size_t const pSize = sizeof(ZSTD_seqSymbol) * (SEQSYMBOL_TABLE_SIZE(maxLog));
931 PREFETCH_AREA(pStart, pSize);
932 }
891 return 0; 933 return 0;
892 case set_compressed : 934 case set_compressed :
893 { U32 tableLog; 935 { U32 tableLog;
894 S16 norm[MaxSeq+1]; 936 S16 norm[MaxSeq+1];
895 size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize); 937 size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize);
931 27, 28, 29, 30, 31, 32, 33, 34, 973 27, 28, 29, 30, 31, 32, 33, 34,
932 35, 37, 39, 41, 43, 47, 51, 59, 974 35, 37, 39, 41, 43, 47, 51, 59,
933 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803, 975 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803,
934 0x1003, 0x2003, 0x4003, 0x8003, 0x10003 }; 976 0x1003, 0x2003, 0x4003, 0x8003, 0x10003 };
935 977
978 /* Hidden delcaration for fullbench */
979 size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
980 const void* src, size_t srcSize);
936 981
937 size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr, 982 size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
938 const void* src, size_t srcSize) 983 const void* src, size_t srcSize)
939 { 984 {
940 const BYTE* const istart = (const BYTE* const)src; 985 const BYTE* const istart = (const BYTE* const)src;
941 const BYTE* const iend = istart + srcSize; 986 const BYTE* const iend = istart + srcSize;
942 const BYTE* ip = istart; 987 const BYTE* ip = istart;
988 int nbSeq;
943 DEBUGLOG(5, "ZSTD_decodeSeqHeaders"); 989 DEBUGLOG(5, "ZSTD_decodeSeqHeaders");
944 990
945 /* check */ 991 /* check */
946 if (srcSize < MIN_SEQUENCES_SIZE) return ERROR(srcSize_wrong); 992 if (srcSize < MIN_SEQUENCES_SIZE) return ERROR(srcSize_wrong);
947 993
948 /* SeqHead */ 994 /* SeqHead */
949 { int nbSeq = *ip++; 995 nbSeq = *ip++;
950 if (!nbSeq) { *nbSeqPtr=0; return 1; } 996 if (!nbSeq) { *nbSeqPtr=0; return 1; }
951 if (nbSeq > 0x7F) { 997 if (nbSeq > 0x7F) {
952 if (nbSeq == 0xFF) { 998 if (nbSeq == 0xFF) {
953 if (ip+2 > iend) return ERROR(srcSize_wrong); 999 if (ip+2 > iend) return ERROR(srcSize_wrong);
954 nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2; 1000 nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2;
955 } else { 1001 } else {
956 if (ip >= iend) return ERROR(srcSize_wrong); 1002 if (ip >= iend) return ERROR(srcSize_wrong);
957 nbSeq = ((nbSeq-0x80)<<8) + *ip++; 1003 nbSeq = ((nbSeq-0x80)<<8) + *ip++;
958 } 1004 }
959 } 1005 }
960 *nbSeqPtr = nbSeq; 1006 *nbSeqPtr = nbSeq;
961 }
962 1007
963 /* FSE table descriptors */ 1008 /* FSE table descriptors */
964 if (ip+4 > iend) return ERROR(srcSize_wrong); /* minimum possible size */ 1009 if (ip+4 > iend) return ERROR(srcSize_wrong); /* minimum possible size */
965 { symbolEncodingType_e const LLtype = (symbolEncodingType_e)(*ip >> 6); 1010 { symbolEncodingType_e const LLtype = (symbolEncodingType_e)(*ip >> 6);
966 symbolEncodingType_e const OFtype = (symbolEncodingType_e)((*ip >> 4) & 3); 1011 symbolEncodingType_e const OFtype = (symbolEncodingType_e)((*ip >> 4) & 3);
970 /* Build DTables */ 1015 /* Build DTables */
971 { size_t const llhSize = ZSTD_buildSeqTable(dctx->entropy.LLTable, &dctx->LLTptr, 1016 { size_t const llhSize = ZSTD_buildSeqTable(dctx->entropy.LLTable, &dctx->LLTptr,
972 LLtype, MaxLL, LLFSELog, 1017 LLtype, MaxLL, LLFSELog,
973 ip, iend-ip, 1018 ip, iend-ip,
974 LL_base, LL_bits, 1019 LL_base, LL_bits,
975 LL_defaultDTable, dctx->fseEntropy); 1020 LL_defaultDTable, dctx->fseEntropy,
1021 dctx->ddictIsCold, nbSeq);
976 if (ZSTD_isError(llhSize)) return ERROR(corruption_detected); 1022 if (ZSTD_isError(llhSize)) return ERROR(corruption_detected);
977 ip += llhSize; 1023 ip += llhSize;
978 } 1024 }
979 1025
980 { size_t const ofhSize = ZSTD_buildSeqTable(dctx->entropy.OFTable, &dctx->OFTptr, 1026 { size_t const ofhSize = ZSTD_buildSeqTable(dctx->entropy.OFTable, &dctx->OFTptr,
981 OFtype, MaxOff, OffFSELog, 1027 OFtype, MaxOff, OffFSELog,
982 ip, iend-ip, 1028 ip, iend-ip,
983 OF_base, OF_bits, 1029 OF_base, OF_bits,
984 OF_defaultDTable, dctx->fseEntropy); 1030 OF_defaultDTable, dctx->fseEntropy,
1031 dctx->ddictIsCold, nbSeq);
985 if (ZSTD_isError(ofhSize)) return ERROR(corruption_detected); 1032 if (ZSTD_isError(ofhSize)) return ERROR(corruption_detected);
986 ip += ofhSize; 1033 ip += ofhSize;
987 } 1034 }
988 1035
989 { size_t const mlhSize = ZSTD_buildSeqTable(dctx->entropy.MLTable, &dctx->MLTptr, 1036 { size_t const mlhSize = ZSTD_buildSeqTable(dctx->entropy.MLTable, &dctx->MLTptr,
990 MLtype, MaxML, MLFSELog, 1037 MLtype, MaxML, MLFSELog,
991 ip, iend-ip, 1038 ip, iend-ip,
992 ML_base, ML_bits, 1039 ML_base, ML_bits,
993 ML_defaultDTable, dctx->fseEntropy); 1040 ML_defaultDTable, dctx->fseEntropy,
1041 dctx->ddictIsCold, nbSeq);
994 if (ZSTD_isError(mlhSize)) return ERROR(corruption_detected); 1042 if (ZSTD_isError(mlhSize)) return ERROR(corruption_detected);
995 ip += mlhSize; 1043 ip += mlhSize;
996 } 1044 }
1045 }
1046
1047 /* prefetch dictionary content */
1048 if (dctx->ddictIsCold) {
1049 size_t const dictSize = (const char*)dctx->prefixStart - (const char*)dctx->virtualStart;
1050 size_t const psmin = MIN(dictSize, (size_t)(64*nbSeq) /* heuristic */ );
1051 size_t const pSize = MIN(psmin, 128 KB /* protection */ );
1052 const void* const pStart = (const char*)dctx->dictEnd - pSize;
1053 PREFETCH_AREA(pStart, pSize);
1054 dctx->ddictIsCold = 0;
997 } 1055 }
998 1056
999 return ip-istart; 1057 return ip-istart;
1000 } 1058 }
1001 1059
1073 1131
1074 HINT_INLINE 1132 HINT_INLINE
1075 size_t ZSTD_execSequence(BYTE* op, 1133 size_t ZSTD_execSequence(BYTE* op,
1076 BYTE* const oend, seq_t sequence, 1134 BYTE* const oend, seq_t sequence,
1077 const BYTE** litPtr, const BYTE* const litLimit, 1135 const BYTE** litPtr, const BYTE* const litLimit,
1078 const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd) 1136 const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd)
1079 { 1137 {
1080 BYTE* const oLitEnd = op + sequence.litLength; 1138 BYTE* const oLitEnd = op + sequence.litLength;
1081 size_t const sequenceLength = sequence.litLength + sequence.matchLength; 1139 size_t const sequenceLength = sequence.litLength + sequence.matchLength;
1082 BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ 1140 BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
1083 BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH; 1141 BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH;
1085 const BYTE* match = oLitEnd - sequence.offset; 1143 const BYTE* match = oLitEnd - sequence.offset;
1086 1144
1087 /* check */ 1145 /* check */
1088 if (oMatchEnd>oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */ 1146 if (oMatchEnd>oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */
1089 if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */ 1147 if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */
1090 if (oLitEnd>oend_w) return ZSTD_execSequenceLast7(op, oend, sequence, litPtr, litLimit, base, vBase, dictEnd); 1148 if (oLitEnd>oend_w) return ZSTD_execSequenceLast7(op, oend, sequence, litPtr, litLimit, prefixStart, virtualStart, dictEnd);
1091 1149
1092 /* copy Literals */ 1150 /* copy Literals */
1093 ZSTD_copy8(op, *litPtr); 1151 ZSTD_copy8(op, *litPtr);
1094 if (sequence.litLength > 8) 1152 if (sequence.litLength > 8)
1095 ZSTD_wildcopy(op+8, (*litPtr)+8, sequence.litLength - 8); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */ 1153 ZSTD_wildcopy(op+8, (*litPtr)+8, sequence.litLength - 8); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */
1096 op = oLitEnd; 1154 op = oLitEnd;
1097 *litPtr = iLitEnd; /* update for next sequence */ 1155 *litPtr = iLitEnd; /* update for next sequence */
1098 1156
1099 /* copy Match */ 1157 /* copy Match */
1100 if (sequence.offset > (size_t)(oLitEnd - base)) { 1158 if (sequence.offset > (size_t)(oLitEnd - prefixStart)) {
1101 /* offset beyond prefix -> go into extDict */ 1159 /* offset beyond prefix -> go into extDict */
1102 if (sequence.offset > (size_t)(oLitEnd - vBase)) 1160 if (sequence.offset > (size_t)(oLitEnd - virtualStart))
1103 return ERROR(corruption_detected); 1161 return ERROR(corruption_detected);
1104 match = dictEnd + (match - base); 1162 match = dictEnd + (match - prefixStart);
1105 if (match + sequence.matchLength <= dictEnd) { 1163 if (match + sequence.matchLength <= dictEnd) {
1106 memmove(oLitEnd, match, sequence.matchLength); 1164 memmove(oLitEnd, match, sequence.matchLength);
1107 return sequenceLength; 1165 return sequenceLength;
1108 } 1166 }
1109 /* span extDict & currentPrefixSegment */ 1167 /* span extDict & currentPrefixSegment */
1110 { size_t const length1 = dictEnd - match; 1168 { size_t const length1 = dictEnd - match;
1111 memmove(oLitEnd, match, length1); 1169 memmove(oLitEnd, match, length1);
1112 op = oLitEnd + length1; 1170 op = oLitEnd + length1;
1113 sequence.matchLength -= length1; 1171 sequence.matchLength -= length1;
1114 match = base; 1172 match = prefixStart;
1115 if (op > oend_w || sequence.matchLength < MINMATCH) { 1173 if (op > oend_w || sequence.matchLength < MINMATCH) {
1116 U32 i; 1174 U32 i;
1117 for (i = 0; i < sequence.matchLength; ++i) op[i] = match[i]; 1175 for (i = 0; i < sequence.matchLength; ++i) op[i] = match[i];
1118 return sequenceLength; 1176 return sequenceLength;
1119 } 1177 }
1352 BYTE* const ostart = (BYTE* const)dst; 1410 BYTE* const ostart = (BYTE* const)dst;
1353 BYTE* const oend = ostart + maxDstSize; 1411 BYTE* const oend = ostart + maxDstSize;
1354 BYTE* op = ostart; 1412 BYTE* op = ostart;
1355 const BYTE* litPtr = dctx->litPtr; 1413 const BYTE* litPtr = dctx->litPtr;
1356 const BYTE* const litEnd = litPtr + dctx->litSize; 1414 const BYTE* const litEnd = litPtr + dctx->litSize;
1357 const BYTE* const base = (const BYTE*) (dctx->base); 1415 const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart);
1358 const BYTE* const vBase = (const BYTE*) (dctx->vBase); 1416 const BYTE* const vBase = (const BYTE*) (dctx->virtualStart);
1359 const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); 1417 const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
1360 DEBUGLOG(5, "ZSTD_decompressSequences"); 1418 DEBUGLOG(5, "ZSTD_decompressSequences_body");
1361 1419
1362 /* Regen sequences */ 1420 /* Regen sequences */
1363 if (nbSeq) { 1421 if (nbSeq) {
1364 seqState_t seqState; 1422 seqState_t seqState;
1365 dctx->fseEntropy = 1; 1423 dctx->fseEntropy = 1;
1370 ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr); 1428 ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);
1371 1429
1372 for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && nbSeq ; ) { 1430 for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && nbSeq ; ) {
1373 nbSeq--; 1431 nbSeq--;
1374 { seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset); 1432 { seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset);
1375 size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, base, vBase, dictEnd); 1433 size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, prefixStart, vBase, dictEnd);
1376 DEBUGLOG(6, "regenerated sequence size : %u", (U32)oneSeqSize); 1434 DEBUGLOG(6, "regenerated sequence size : %u", (U32)oneSeqSize);
1377 if (ZSTD_isError(oneSeqSize)) return oneSeqSize; 1435 if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
1378 op += oneSeqSize; 1436 op += oneSeqSize;
1379 } } 1437 } }
1380 1438
1381 /* check if reached exact end */ 1439 /* check if reached exact end */
1382 DEBUGLOG(5, "ZSTD_decompressSequences: after decode loop, remaining nbSeq : %i", nbSeq); 1440 DEBUGLOG(5, "ZSTD_decompressSequences_body: after decode loop, remaining nbSeq : %i", nbSeq);
1383 if (nbSeq) return ERROR(corruption_detected); 1441 if (nbSeq) return ERROR(corruption_detected);
1384 /* save reps for next block */ 1442 /* save reps for next block */
1385 { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); } 1443 { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); }
1386 } 1444 }
1387 1445
1496 BYTE* const ostart = (BYTE* const)dst; 1554 BYTE* const ostart = (BYTE* const)dst;
1497 BYTE* const oend = ostart + maxDstSize; 1555 BYTE* const oend = ostart + maxDstSize;
1498 BYTE* op = ostart; 1556 BYTE* op = ostart;
1499 const BYTE* litPtr = dctx->litPtr; 1557 const BYTE* litPtr = dctx->litPtr;
1500 const BYTE* const litEnd = litPtr + dctx->litSize; 1558 const BYTE* const litEnd = litPtr + dctx->litSize;
1501 const BYTE* const prefixStart = (const BYTE*) (dctx->base); 1559 const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart);
1502 const BYTE* const dictStart = (const BYTE*) (dctx->vBase); 1560 const BYTE* const dictStart = (const BYTE*) (dctx->virtualStart);
1503 const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); 1561 const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
1504 1562
1505 /* Regen sequences */ 1563 /* Regen sequences */
1506 if (nbSeq) { 1564 if (nbSeq) {
1507 #define STORED_SEQS 4 1565 #define STORED_SEQS 4
1660 { /* blockType == blockCompressed */ 1718 { /* blockType == blockCompressed */
1661 const BYTE* ip = (const BYTE*)src; 1719 const BYTE* ip = (const BYTE*)src;
1662 /* isLongOffset must be true if there are long offsets. 1720 /* isLongOffset must be true if there are long offsets.
1663 * Offsets are long if they are larger than 2^STREAM_ACCUMULATOR_MIN. 1721 * Offsets are long if they are larger than 2^STREAM_ACCUMULATOR_MIN.
1664 * We don't expect that to be the case in 64-bit mode. 1722 * We don't expect that to be the case in 64-bit mode.
1665 * In block mode, window size is not known, so we have to be conservative. (note: but it could be evaluated from current-lowLimit) 1723 * In block mode, window size is not known, so we have to be conservative.
1724 * (note: but it could be evaluated from current-lowLimit)
1666 */ 1725 */
1667 ZSTD_longOffset_e const isLongOffset = (ZSTD_longOffset_e)(MEM_32bits() && (!frame || dctx->fParams.windowSize > (1ULL << STREAM_ACCUMULATOR_MIN))); 1726 ZSTD_longOffset_e const isLongOffset = (ZSTD_longOffset_e)(MEM_32bits() && (!frame || dctx->fParams.windowSize > (1ULL << STREAM_ACCUMULATOR_MIN)));
1668 DEBUGLOG(5, "ZSTD_decompressBlock_internal (size : %u)", (U32)srcSize); 1727 DEBUGLOG(5, "ZSTD_decompressBlock_internal (size : %u)", (U32)srcSize);
1669 1728
1670 if (srcSize >= ZSTD_BLOCKSIZE_MAX) return ERROR(srcSize_wrong); 1729 if (srcSize >= ZSTD_BLOCKSIZE_MAX) return ERROR(srcSize_wrong);
1699 1758
1700 static void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst) 1759 static void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst)
1701 { 1760 {
1702 if (dst != dctx->previousDstEnd) { /* not contiguous */ 1761 if (dst != dctx->previousDstEnd) { /* not contiguous */
1703 dctx->dictEnd = dctx->previousDstEnd; 1762 dctx->dictEnd = dctx->previousDstEnd;
1704 dctx->vBase = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base)); 1763 dctx->virtualStart = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
1705 dctx->base = dst; 1764 dctx->prefixStart = dst;
1706 dctx->previousDstEnd = dst; 1765 dctx->previousDstEnd = dst;
1707 } 1766 }
1708 } 1767 }
1709 1768
1710 size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, 1769 size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx,
1727 dctx->previousDstEnd = (const char*)blockStart + blockSize; 1786 dctx->previousDstEnd = (const char*)blockStart + blockSize;
1728 return blockSize; 1787 return blockSize;
1729 } 1788 }
1730 1789
1731 1790
1732 static size_t ZSTD_generateNxBytes(void* dst, size_t dstCapacity, BYTE byte, size_t length) 1791 static size_t ZSTD_generateNxBytes(void* dst, size_t dstCapacity, BYTE value, size_t length)
1733 { 1792 {
1734 if (length > dstCapacity) return ERROR(dstSize_tooSmall); 1793 if (length > dstCapacity) return ERROR(dstSize_tooSmall);
1735 memset(dst, byte, length); 1794 memset(dst, value, length);
1736 return length; 1795 return length;
1737 } 1796 }
1738 1797
1739 /** ZSTD_findFrameCompressedSize() : 1798 /** ZSTD_findFrameCompressedSize() :
1740 * compatible with legacy mode 1799 * compatible with legacy mode
1747 if (ZSTD_isLegacy(src, srcSize)) 1806 if (ZSTD_isLegacy(src, srcSize))
1748 return ZSTD_findFrameCompressedSizeLegacy(src, srcSize); 1807 return ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
1749 #endif 1808 #endif
1750 if ( (srcSize >= ZSTD_skippableHeaderSize) 1809 if ( (srcSize >= ZSTD_skippableHeaderSize)
1751 && (MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START ) { 1810 && (MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START ) {
1752 return ZSTD_skippableHeaderSize + MEM_readLE32((const BYTE*)src + ZSTD_frameIdSize); 1811 return ZSTD_skippableHeaderSize + MEM_readLE32((const BYTE*)src + ZSTD_FRAMEIDSIZE);
1753 } else { 1812 } else {
1754 const BYTE* ip = (const BYTE*)src; 1813 const BYTE* ip = (const BYTE*)src;
1755 const BYTE* const ipstart = ip; 1814 const BYTE* const ipstart = ip;
1756 size_t remainingSize = srcSize; 1815 size_t remainingSize = srcSize;
1757 ZSTD_frameHeader zfh; 1816 ZSTD_frameHeader zfh;
1781 } 1840 }
1782 1841
1783 if (zfh.checksumFlag) { /* Final frame content checksum */ 1842 if (zfh.checksumFlag) { /* Final frame content checksum */
1784 if (remainingSize < 4) return ERROR(srcSize_wrong); 1843 if (remainingSize < 4) return ERROR(srcSize_wrong);
1785 ip += 4; 1844 ip += 4;
1786 remainingSize -= 4;
1787 } 1845 }
1788 1846
1789 return ip - ipstart; 1847 return ip - ipstart;
1790 } 1848 }
1791 } 1849 }
1869 *srcPtr = ip; 1927 *srcPtr = ip;
1870 *srcSizePtr = remainingSize; 1928 *srcSizePtr = remainingSize;
1871 return op-ostart; 1929 return op-ostart;
1872 } 1930 }
1873 1931
1874 static const void* ZSTD_DDictDictContent(const ZSTD_DDict* ddict);
1875 static size_t ZSTD_DDictDictSize(const ZSTD_DDict* ddict);
1876
1877 static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx, 1932 static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
1878 void* dst, size_t dstCapacity, 1933 void* dst, size_t dstCapacity,
1879 const void* src, size_t srcSize, 1934 const void* src, size_t srcSize,
1880 const void* dict, size_t dictSize, 1935 const void* dict, size_t dictSize,
1881 const ZSTD_DDict* ddict) 1936 const ZSTD_DDict* ddict)
1882 { 1937 {
1883 void* const dststart = dst; 1938 void* const dststart = dst;
1939 int moreThan1Frame = 0;
1940
1941 DEBUGLOG(5, "ZSTD_decompressMultiFrame");
1884 assert(dict==NULL || ddict==NULL); /* either dict or ddict set, not both */ 1942 assert(dict==NULL || ddict==NULL); /* either dict or ddict set, not both */
1885 1943
1886 if (ddict) { 1944 if (ddict) {
1887 dict = ZSTD_DDictDictContent(ddict); 1945 dict = ZSTD_DDictDictContent(ddict);
1888 dictSize = ZSTD_DDictDictSize(ddict); 1946 dictSize = ZSTD_DDictDictSize(ddict);
1889 } 1947 }
1890 1948
1891 while (srcSize >= ZSTD_frameHeaderSize_prefix) { 1949 while (srcSize >= ZSTD_frameHeaderSize_prefix) {
1892 U32 magicNumber;
1893 1950
1894 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) 1951 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
1895 if (ZSTD_isLegacy(src, srcSize)) { 1952 if (ZSTD_isLegacy(src, srcSize)) {
1896 size_t decodedSize; 1953 size_t decodedSize;
1897 size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize); 1954 size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
1909 1966
1910 continue; 1967 continue;
1911 } 1968 }
1912 #endif 1969 #endif
1913 1970
1914 magicNumber = MEM_readLE32(src); 1971 { U32 const magicNumber = MEM_readLE32(src);
1915 DEBUGLOG(4, "reading magic number %08X (expecting %08X)", 1972 DEBUGLOG(4, "reading magic number %08X (expecting %08X)",
1916 (U32)magicNumber, (U32)ZSTD_MAGICNUMBER); 1973 (U32)magicNumber, (U32)ZSTD_MAGICNUMBER);
1917 if (magicNumber != ZSTD_MAGICNUMBER) {
1918 if ((magicNumber & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { 1974 if ((magicNumber & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) {
1919 size_t skippableSize; 1975 size_t skippableSize;
1920 if (srcSize < ZSTD_skippableHeaderSize) 1976 if (srcSize < ZSTD_skippableHeaderSize)
1921 return ERROR(srcSize_wrong); 1977 return ERROR(srcSize_wrong);
1922 skippableSize = MEM_readLE32((const BYTE*)src + ZSTD_frameIdSize) 1978 skippableSize = MEM_readLE32((const BYTE*)src + ZSTD_FRAMEIDSIZE)
1923 + ZSTD_skippableHeaderSize; 1979 + ZSTD_skippableHeaderSize;
1924 if (srcSize < skippableSize) return ERROR(srcSize_wrong); 1980 if (srcSize < skippableSize) return ERROR(srcSize_wrong);
1925 1981
1926 src = (const BYTE *)src + skippableSize; 1982 src = (const BYTE *)src + skippableSize;
1927 srcSize -= skippableSize; 1983 srcSize -= skippableSize;
1928 continue; 1984 continue;
1929 } 1985 } }
1930 return ERROR(prefix_unknown);
1931 }
1932 1986
1933 if (ddict) { 1987 if (ddict) {
1934 /* we were called from ZSTD_decompress_usingDDict */ 1988 /* we were called from ZSTD_decompress_usingDDict */
1935 CHECK_F(ZSTD_decompressBegin_usingDDict(dctx, ddict)); 1989 CHECK_F(ZSTD_decompressBegin_usingDDict(dctx, ddict));
1936 } else { 1990 } else {
1940 } 1994 }
1941 ZSTD_checkContinuity(dctx, dst); 1995 ZSTD_checkContinuity(dctx, dst);
1942 1996
1943 { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity, 1997 { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity,
1944 &src, &srcSize); 1998 &src, &srcSize);
1999 if ( (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown)
2000 && (moreThan1Frame==1) ) {
2001 /* at least one frame successfully completed,
2002 * but following bytes are garbage :
2003 * it's more likely to be a srcSize error,
2004 * specifying more bytes than compressed size of frame(s).
2005 * This error message replaces ERROR(prefix_unknown),
2006 * which would be confusing, as the first header is actually correct.
2007 * Note that one could be unlucky, it might be a corruption error instead,
2008 * happening right at the place where we expect zstd magic bytes.
2009 * But this is _much_ less likely than a srcSize field error. */
2010 return ERROR(srcSize_wrong);
2011 }
1945 if (ZSTD_isError(res)) return res; 2012 if (ZSTD_isError(res)) return res;
1946 /* no need to bound check, ZSTD_decompressFrame already has */ 2013 /* no need to bound check, ZSTD_decompressFrame already has */
1947 dst = (BYTE*)dst + res; 2014 dst = (BYTE*)dst + res;
1948 dstCapacity -= res; 2015 dstCapacity -= res;
1949 } 2016 }
2017 moreThan1Frame = 1;
1950 } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */ 2018 } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
1951 2019
1952 if (srcSize) return ERROR(srcSize_wrong); /* input not entirely consumed */ 2020 if (srcSize) return ERROR(srcSize_wrong); /* input not entirely consumed */
1953 2021
1954 return (BYTE*)dst - (BYTE*)dststart; 2022 return (BYTE*)dst - (BYTE*)dststart;
1978 regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize); 2046 regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize);
1979 ZSTD_freeDCtx(dctx); 2047 ZSTD_freeDCtx(dctx);
1980 return regenSize; 2048 return regenSize;
1981 #else /* stack mode */ 2049 #else /* stack mode */
1982 ZSTD_DCtx dctx; 2050 ZSTD_DCtx dctx;
2051 ZSTD_initDCtx_internal(&dctx);
1983 return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize); 2052 return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);
1984 #endif 2053 #endif
1985 } 2054 }
1986 2055
1987 2056
2029 switch (dctx->stage) 2098 switch (dctx->stage)
2030 { 2099 {
2031 case ZSTDds_getFrameHeaderSize : 2100 case ZSTDds_getFrameHeaderSize :
2032 assert(src != NULL); 2101 assert(src != NULL);
2033 if (dctx->format == ZSTD_f_zstd1) { /* allows header */ 2102 if (dctx->format == ZSTD_f_zstd1) { /* allows header */
2034 assert(srcSize >= ZSTD_frameIdSize); /* to read skippable magic number */ 2103 assert(srcSize >= ZSTD_FRAMEIDSIZE); /* to read skippable magic number */
2035 if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */ 2104 if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
2036 memcpy(dctx->headerBuffer, src, srcSize); 2105 memcpy(dctx->headerBuffer, src, srcSize);
2037 dctx->expected = ZSTD_skippableHeaderSize - srcSize; /* remaining to load to get full skippable frame header */ 2106 dctx->expected = ZSTD_skippableHeaderSize - srcSize; /* remaining to load to get full skippable frame header */
2038 dctx->stage = ZSTDds_decodeSkippableHeader; 2107 dctx->stage = ZSTDds_decodeSkippableHeader;
2039 return 0; 2108 return 0;
2139 2208
2140 case ZSTDds_decodeSkippableHeader: 2209 case ZSTDds_decodeSkippableHeader:
2141 assert(src != NULL); 2210 assert(src != NULL);
2142 assert(srcSize <= ZSTD_skippableHeaderSize); 2211 assert(srcSize <= ZSTD_skippableHeaderSize);
2143 memcpy(dctx->headerBuffer + (ZSTD_skippableHeaderSize - srcSize), src, srcSize); /* complete skippable header */ 2212 memcpy(dctx->headerBuffer + (ZSTD_skippableHeaderSize - srcSize), src, srcSize); /* complete skippable header */
2144 dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_frameIdSize); /* note : dctx->expected can grow seriously large, beyond local buffer size */ 2213 dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_FRAMEIDSIZE); /* note : dctx->expected can grow seriously large, beyond local buffer size */
2145 dctx->stage = ZSTDds_skipFrame; 2214 dctx->stage = ZSTDds_skipFrame;
2146 return 0; 2215 return 0;
2147 2216
2148 case ZSTDds_skipFrame: 2217 case ZSTDds_skipFrame:
2149 dctx->expected = 0; 2218 dctx->expected = 0;
2157 2226
2158 2227
2159 static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) 2228 static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
2160 { 2229 {
2161 dctx->dictEnd = dctx->previousDstEnd; 2230 dctx->dictEnd = dctx->previousDstEnd;
2162 dctx->vBase = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base)); 2231 dctx->virtualStart = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
2163 dctx->base = dict; 2232 dctx->prefixStart = dict;
2164 dctx->previousDstEnd = (const char*)dict + dictSize; 2233 dctx->previousDstEnd = (const char*)dict + dictSize;
2165 return 0; 2234 return 0;
2166 } 2235 }
2167 2236
2168 /* ZSTD_loadEntropy() : 2237 /*! ZSTD_loadEntropy() :
2169 * dict : must point at beginning of a valid zstd dictionary 2238 * dict : must point at beginning of a valid zstd dictionary.
2170 * @return : size of entropy tables read */ 2239 * @return : size of entropy tables read */
2171 static size_t ZSTD_loadEntropy(ZSTD_entropyDTables_t* entropy, const void* const dict, size_t const dictSize) 2240 static size_t ZSTD_loadEntropy(ZSTD_entropyDTables_t* entropy,
2241 const void* const dict, size_t const dictSize)
2172 { 2242 {
2173 const BYTE* dictPtr = (const BYTE*)dict; 2243 const BYTE* dictPtr = (const BYTE*)dict;
2174 const BYTE* const dictEnd = dictPtr + dictSize; 2244 const BYTE* const dictEnd = dictPtr + dictSize;
2175 2245
2176 if (dictSize <= 8) return ERROR(dictionary_corrupted); 2246 if (dictSize <= 8) return ERROR(dictionary_corrupted);
2247 assert(MEM_readLE32(dict) == ZSTD_MAGIC_DICTIONARY); /* dict must be valid */
2177 dictPtr += 8; /* skip header = magic + dictID */ 2248 dictPtr += 8; /* skip header = magic + dictID */
2178 2249
2179 2250 ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, OFTable) == offsetof(ZSTD_entropyDTables_t, LLTable) + sizeof(entropy->LLTable));
2180 { size_t const hSize = HUF_readDTableX4_wksp( 2251 ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, MLTable) == offsetof(ZSTD_entropyDTables_t, OFTable) + sizeof(entropy->OFTable));
2181 entropy->hufTable, dictPtr, dictEnd - dictPtr, 2252 ZSTD_STATIC_ASSERT(sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable) >= HUF_DECOMPRESS_WORKSPACE_SIZE);
2182 entropy->workspace, sizeof(entropy->workspace)); 2253 { void* const workspace = &entropy->LLTable; /* use fse tables as temporary workspace; implies fse tables are grouped together */
2254 size_t const workspaceSize = sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable);
2255 size_t const hSize = HUF_readDTableX2_wksp(entropy->hufTable,
2256 dictPtr, dictEnd - dictPtr,
2257 workspace, workspaceSize);
2183 if (HUF_isError(hSize)) return ERROR(dictionary_corrupted); 2258 if (HUF_isError(hSize)) return ERROR(dictionary_corrupted);
2184 dictPtr += hSize; 2259 dictPtr += hSize;
2185 } 2260 }
2186 2261
2187 { short offcodeNCount[MaxOff+1]; 2262 { short offcodeNCount[MaxOff+1];
2188 U32 offcodeMaxValue = MaxOff, offcodeLog; 2263 U32 offcodeMaxValue = MaxOff, offcodeLog;
2189 size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr); 2264 size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr);
2190 if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted); 2265 if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted);
2191 if (offcodeMaxValue > MaxOff) return ERROR(dictionary_corrupted); 2266 if (offcodeMaxValue > MaxOff) return ERROR(dictionary_corrupted);
2192 if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted); 2267 if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted);
2193 ZSTD_buildFSETable(entropy->OFTable, 2268 ZSTD_buildFSETable( entropy->OFTable,
2194 offcodeNCount, offcodeMaxValue, 2269 offcodeNCount, offcodeMaxValue,
2195 OF_base, OF_bits, 2270 OF_base, OF_bits,
2196 offcodeLog); 2271 offcodeLog);
2197 dictPtr += offcodeHeaderSize; 2272 dictPtr += offcodeHeaderSize;
2198 } 2273 }
2201 unsigned matchlengthMaxValue = MaxML, matchlengthLog; 2276 unsigned matchlengthMaxValue = MaxML, matchlengthLog;
2202 size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr); 2277 size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr);
2203 if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted); 2278 if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted);
2204 if (matchlengthMaxValue > MaxML) return ERROR(dictionary_corrupted); 2279 if (matchlengthMaxValue > MaxML) return ERROR(dictionary_corrupted);
2205 if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted); 2280 if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted);
2206 ZSTD_buildFSETable(entropy->MLTable, 2281 ZSTD_buildFSETable( entropy->MLTable,
2207 matchlengthNCount, matchlengthMaxValue, 2282 matchlengthNCount, matchlengthMaxValue,
2208 ML_base, ML_bits, 2283 ML_base, ML_bits,
2209 matchlengthLog); 2284 matchlengthLog);
2210 dictPtr += matchlengthHeaderSize; 2285 dictPtr += matchlengthHeaderSize;
2211 } 2286 }
2214 unsigned litlengthMaxValue = MaxLL, litlengthLog; 2289 unsigned litlengthMaxValue = MaxLL, litlengthLog;
2215 size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr); 2290 size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr);
2216 if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted); 2291 if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted);
2217 if (litlengthMaxValue > MaxLL) return ERROR(dictionary_corrupted); 2292 if (litlengthMaxValue > MaxLL) return ERROR(dictionary_corrupted);
2218 if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted); 2293 if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted);
2219 ZSTD_buildFSETable(entropy->LLTable, 2294 ZSTD_buildFSETable( entropy->LLTable,
2220 litlengthNCount, litlengthMaxValue, 2295 litlengthNCount, litlengthMaxValue,
2221 LL_base, LL_bits, 2296 LL_base, LL_bits,
2222 litlengthLog); 2297 litlengthLog);
2223 dictPtr += litlengthHeaderSize; 2298 dictPtr += litlengthHeaderSize;
2224 } 2299 }
2240 if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize); 2315 if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize);
2241 { U32 const magic = MEM_readLE32(dict); 2316 { U32 const magic = MEM_readLE32(dict);
2242 if (magic != ZSTD_MAGIC_DICTIONARY) { 2317 if (magic != ZSTD_MAGIC_DICTIONARY) {
2243 return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */ 2318 return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */
2244 } } 2319 } }
2245 dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_frameIdSize); 2320 dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
2246 2321
2247 /* load entropy tables */ 2322 /* load entropy tables */
2248 { size_t const eSize = ZSTD_loadEntropy(&dctx->entropy, dict, dictSize); 2323 { size_t const eSize = ZSTD_loadEntropy(&dctx->entropy, dict, dictSize);
2249 if (ZSTD_isError(eSize)) return ERROR(dictionary_corrupted); 2324 if (ZSTD_isError(eSize)) return ERROR(dictionary_corrupted);
2250 dict = (const char*)dict + eSize; 2325 dict = (const char*)dict + eSize;
2254 2329
2255 /* reference dictionary content */ 2330 /* reference dictionary content */
2256 return ZSTD_refDictContent(dctx, dict, dictSize); 2331 return ZSTD_refDictContent(dctx, dict, dictSize);
2257 } 2332 }
2258 2333
2259 /* Note : this function cannot fail */
2260 size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx) 2334 size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
2261 { 2335 {
2262 assert(dctx != NULL); 2336 assert(dctx != NULL);
2263 dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */ 2337 dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */
2264 dctx->stage = ZSTDds_getFrameHeaderSize; 2338 dctx->stage = ZSTDds_getFrameHeaderSize;
2265 dctx->decodedSize = 0; 2339 dctx->decodedSize = 0;
2266 dctx->previousDstEnd = NULL; 2340 dctx->previousDstEnd = NULL;
2267 dctx->base = NULL; 2341 dctx->prefixStart = NULL;
2268 dctx->vBase = NULL; 2342 dctx->virtualStart = NULL;
2269 dctx->dictEnd = NULL; 2343 dctx->dictEnd = NULL;
2270 dctx->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */ 2344 dctx->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */
2271 dctx->litEntropy = dctx->fseEntropy = 0; 2345 dctx->litEntropy = dctx->fseEntropy = 0;
2272 dctx->dictID = 0; 2346 dctx->dictID = 0;
2273 ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue)); 2347 ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue));
2300 ZSTD_customMem cMem; 2374 ZSTD_customMem cMem;
2301 }; /* typedef'd to ZSTD_DDict within "zstd.h" */ 2375 }; /* typedef'd to ZSTD_DDict within "zstd.h" */
2302 2376
2303 static const void* ZSTD_DDictDictContent(const ZSTD_DDict* ddict) 2377 static const void* ZSTD_DDictDictContent(const ZSTD_DDict* ddict)
2304 { 2378 {
2379 assert(ddict != NULL);
2305 return ddict->dictContent; 2380 return ddict->dictContent;
2306 } 2381 }
2307 2382
2308 static size_t ZSTD_DDictDictSize(const ZSTD_DDict* ddict) 2383 static size_t ZSTD_DDictDictSize(const ZSTD_DDict* ddict)
2309 { 2384 {
2385 assert(ddict != NULL);
2310 return ddict->dictSize; 2386 return ddict->dictSize;
2311 } 2387 }
2312 2388
2313 size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dstDCtx, const ZSTD_DDict* ddict) 2389 size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
2314 { 2390 {
2315 CHECK_F( ZSTD_decompressBegin(dstDCtx) ); 2391 DEBUGLOG(4, "ZSTD_decompressBegin_usingDDict");
2316 if (ddict) { /* support begin on NULL */ 2392 assert(dctx != NULL);
2317 dstDCtx->dictID = ddict->dictID; 2393 if (ddict) {
2318 dstDCtx->base = ddict->dictContent; 2394 dctx->ddictIsCold = (dctx->dictEnd != (const char*)ddict->dictContent + ddict->dictSize);
2319 dstDCtx->vBase = ddict->dictContent; 2395 DEBUGLOG(4, "DDict is %s",
2320 dstDCtx->dictEnd = (const BYTE*)ddict->dictContent + ddict->dictSize; 2396 dctx->ddictIsCold ? "~cold~" : "hot!");
2321 dstDCtx->previousDstEnd = dstDCtx->dictEnd; 2397 }
2398 CHECK_F( ZSTD_decompressBegin(dctx) );
2399 if (ddict) { /* NULL ddict is equivalent to no dictionary */
2400 dctx->dictID = ddict->dictID;
2401 dctx->prefixStart = ddict->dictContent;
2402 dctx->virtualStart = ddict->dictContent;
2403 dctx->dictEnd = (const BYTE*)ddict->dictContent + ddict->dictSize;
2404 dctx->previousDstEnd = dctx->dictEnd;
2322 if (ddict->entropyPresent) { 2405 if (ddict->entropyPresent) {
2323 dstDCtx->litEntropy = 1; 2406 dctx->litEntropy = 1;
2324 dstDCtx->fseEntropy = 1; 2407 dctx->fseEntropy = 1;
2325 dstDCtx->LLTptr = ddict->entropy.LLTable; 2408 dctx->LLTptr = ddict->entropy.LLTable;
2326 dstDCtx->MLTptr = ddict->entropy.MLTable; 2409 dctx->MLTptr = ddict->entropy.MLTable;
2327 dstDCtx->OFTptr = ddict->entropy.OFTable; 2410 dctx->OFTptr = ddict->entropy.OFTable;
2328 dstDCtx->HUFptr = ddict->entropy.hufTable; 2411 dctx->HUFptr = ddict->entropy.hufTable;
2329 dstDCtx->entropy.rep[0] = ddict->entropy.rep[0]; 2412 dctx->entropy.rep[0] = ddict->entropy.rep[0];
2330 dstDCtx->entropy.rep[1] = ddict->entropy.rep[1]; 2413 dctx->entropy.rep[1] = ddict->entropy.rep[1];
2331 dstDCtx->entropy.rep[2] = ddict->entropy.rep[2]; 2414 dctx->entropy.rep[2] = ddict->entropy.rep[2];
2332 } else { 2415 } else {
2333 dstDCtx->litEntropy = 0; 2416 dctx->litEntropy = 0;
2334 dstDCtx->fseEntropy = 0; 2417 dctx->fseEntropy = 0;
2335 } 2418 }
2336 } 2419 }
2337 return 0; 2420 return 0;
2338 } 2421 }
2339 2422
2340 static size_t ZSTD_loadEntropy_inDDict(ZSTD_DDict* ddict, ZSTD_dictContentType_e dictContentType) 2423 static size_t
2424 ZSTD_loadEntropy_inDDict(ZSTD_DDict* ddict,
2425 ZSTD_dictContentType_e dictContentType)
2341 { 2426 {
2342 ddict->dictID = 0; 2427 ddict->dictID = 0;
2343 ddict->entropyPresent = 0; 2428 ddict->entropyPresent = 0;
2344 if (dictContentType == ZSTD_dct_rawContent) return 0; 2429 if (dictContentType == ZSTD_dct_rawContent) return 0;
2345 2430
2353 if (dictContentType == ZSTD_dct_fullDict) 2438 if (dictContentType == ZSTD_dct_fullDict)
2354 return ERROR(dictionary_corrupted); /* only accept specified dictionaries */ 2439 return ERROR(dictionary_corrupted); /* only accept specified dictionaries */
2355 return 0; /* pure content mode */ 2440 return 0; /* pure content mode */
2356 } 2441 }
2357 } 2442 }
2358 ddict->dictID = MEM_readLE32((const char*)ddict->dictContent + ZSTD_frameIdSize); 2443 ddict->dictID = MEM_readLE32((const char*)ddict->dictContent + ZSTD_FRAMEIDSIZE);
2359 2444
2360 /* load entropy tables */ 2445 /* load entropy tables */
2361 CHECK_E( ZSTD_loadEntropy(&ddict->entropy, ddict->dictContent, ddict->dictSize), dictionary_corrupted ); 2446 CHECK_E( ZSTD_loadEntropy(&ddict->entropy,
2447 ddict->dictContent, ddict->dictSize),
2448 dictionary_corrupted );
2362 ddict->entropyPresent = 1; 2449 ddict->entropyPresent = 1;
2363 return 0; 2450 return 0;
2364 } 2451 }
2365 2452
2366 2453
2370 ZSTD_dictContentType_e dictContentType) 2457 ZSTD_dictContentType_e dictContentType)
2371 { 2458 {
2372 if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dict) || (!dictSize)) { 2459 if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dict) || (!dictSize)) {
2373 ddict->dictBuffer = NULL; 2460 ddict->dictBuffer = NULL;
2374 ddict->dictContent = dict; 2461 ddict->dictContent = dict;
2462 if (!dict) dictSize = 0;
2375 } else { 2463 } else {
2376 void* const internalBuffer = ZSTD_malloc(dictSize, ddict->cMem); 2464 void* const internalBuffer = ZSTD_malloc(dictSize, ddict->cMem);
2377 ddict->dictBuffer = internalBuffer; 2465 ddict->dictBuffer = internalBuffer;
2378 ddict->dictContent = internalBuffer; 2466 ddict->dictContent = internalBuffer;
2379 if (!internalBuffer) return ERROR(memory_allocation); 2467 if (!internalBuffer) return ERROR(memory_allocation);
2394 ZSTD_customMem customMem) 2482 ZSTD_customMem customMem)
2395 { 2483 {
2396 if (!customMem.customAlloc ^ !customMem.customFree) return NULL; 2484 if (!customMem.customAlloc ^ !customMem.customFree) return NULL;
2397 2485
2398 { ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_malloc(sizeof(ZSTD_DDict), customMem); 2486 { ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_malloc(sizeof(ZSTD_DDict), customMem);
2399 if (!ddict) return NULL; 2487 if (ddict == NULL) return NULL;
2400 ddict->cMem = customMem; 2488 ddict->cMem = customMem;
2401 2489 { size_t const initResult = ZSTD_initDDict_internal(ddict,
2402 if (ZSTD_isError( ZSTD_initDDict_internal(ddict, dict, dictSize, dictLoadMethod, dictContentType) )) { 2490 dict, dictSize,
2403 ZSTD_freeDDict(ddict); 2491 dictLoadMethod, dictContentType);
2404 return NULL; 2492 if (ZSTD_isError(initResult)) {
2405 } 2493 ZSTD_freeDDict(ddict);
2406 2494 return NULL;
2495 } }
2407 return ddict; 2496 return ddict;
2408 } 2497 }
2409 } 2498 }
2410 2499
2411 /*! ZSTD_createDDict() : 2500 /*! ZSTD_createDDict() :
2428 return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, allocator); 2517 return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, allocator);
2429 } 2518 }
2430 2519
2431 2520
2432 const ZSTD_DDict* ZSTD_initStaticDDict( 2521 const ZSTD_DDict* ZSTD_initStaticDDict(
2433 void* workspace, size_t workspaceSize, 2522 void* sBuffer, size_t sBufferSize,
2434 const void* dict, size_t dictSize, 2523 const void* dict, size_t dictSize,
2435 ZSTD_dictLoadMethod_e dictLoadMethod, 2524 ZSTD_dictLoadMethod_e dictLoadMethod,
2436 ZSTD_dictContentType_e dictContentType) 2525 ZSTD_dictContentType_e dictContentType)
2437 { 2526 {
2438 size_t const neededSpace = 2527 size_t const neededSpace = sizeof(ZSTD_DDict)
2439 sizeof(ZSTD_DDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize); 2528 + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);
2440 ZSTD_DDict* const ddict = (ZSTD_DDict*)workspace; 2529 ZSTD_DDict* const ddict = (ZSTD_DDict*)sBuffer;
2441 assert(workspace != NULL); 2530 assert(sBuffer != NULL);
2442 assert(dict != NULL); 2531 assert(dict != NULL);
2443 if ((size_t)workspace & 7) return NULL; /* 8-aligned */ 2532 if ((size_t)sBuffer & 7) return NULL; /* 8-aligned */
2444 if (workspaceSize < neededSpace) return NULL; 2533 if (sBufferSize < neededSpace) return NULL;
2445 if (dictLoadMethod == ZSTD_dlm_byCopy) { 2534 if (dictLoadMethod == ZSTD_dlm_byCopy) {
2446 memcpy(ddict+1, dict, dictSize); /* local copy */ 2535 memcpy(ddict+1, dict, dictSize); /* local copy */
2447 dict = ddict+1; 2536 dict = ddict+1;
2448 } 2537 }
2449 if (ZSTD_isError( ZSTD_initDDict_internal(ddict, dict, dictSize, ZSTD_dlm_byRef, dictContentType) )) 2538 if (ZSTD_isError( ZSTD_initDDict_internal(ddict,
2539 dict, dictSize,
2540 ZSTD_dlm_byRef, dictContentType) ))
2450 return NULL; 2541 return NULL;
2451 return ddict; 2542 return ddict;
2452 } 2543 }
2453 2544
2454 2545
2482 * It can still be loaded, but as a content-only dictionary. */ 2573 * It can still be loaded, but as a content-only dictionary. */
2483 unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize) 2574 unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize)
2484 { 2575 {
2485 if (dictSize < 8) return 0; 2576 if (dictSize < 8) return 0;
2486 if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0; 2577 if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0;
2487 return MEM_readLE32((const char*)dict + ZSTD_frameIdSize); 2578 return MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
2488 } 2579 }
2489 2580
2490 /*! ZSTD_getDictID_fromDDict() : 2581 /*! ZSTD_getDictID_fromDDict() :
2491 * Provides the dictID of the dictionary loaded into `ddict`. 2582 * Provides the dictID of the dictionary loaded into `ddict`.
2492 * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. 2583 * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
2558 { 2649 {
2559 return ZSTD_freeDCtx(zds); 2650 return ZSTD_freeDCtx(zds);
2560 } 2651 }
2561 2652
2562 2653
2563 /* *** Initialization *** */ 2654 /* *** Initialization *** */
2564 2655
2565 size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; } 2656 size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; }
2566 size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; } 2657 size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; }
2567 2658
2568 size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType) 2659 size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx,
2660 const void* dict, size_t dictSize,
2661 ZSTD_dictLoadMethod_e dictLoadMethod,
2662 ZSTD_dictContentType_e dictContentType)
2569 { 2663 {
2570 if (dctx->streamStage != zdss_init) return ERROR(stage_wrong); 2664 if (dctx->streamStage != zdss_init) return ERROR(stage_wrong);
2571 ZSTD_freeDDict(dctx->ddictLocal); 2665 ZSTD_freeDDict(dctx->ddictLocal);
2572 if (dict && dictSize >= 8) { 2666 if (dict && dictSize >= 8) {
2573 dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem); 2667 dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem);
2605 * this function cannot fail */ 2699 * this function cannot fail */
2606 size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize) 2700 size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize)
2607 { 2701 {
2608 DEBUGLOG(4, "ZSTD_initDStream_usingDict"); 2702 DEBUGLOG(4, "ZSTD_initDStream_usingDict");
2609 zds->streamStage = zdss_init; 2703 zds->streamStage = zdss_init;
2704 zds->noForwardProgress = 0;
2610 CHECK_F( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) ); 2705 CHECK_F( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) );
2611 return ZSTD_frameHeaderSize_prefix; 2706 return ZSTD_frameHeaderSize_prefix;
2612 } 2707 }
2613 2708
2614 /* note : this variant can't fail */ 2709 /* note : this variant can't fail */
2615 size_t ZSTD_initDStream(ZSTD_DStream* zds) 2710 size_t ZSTD_initDStream(ZSTD_DStream* zds)
2616 { 2711 {
2617 DEBUGLOG(4, "ZSTD_initDStream"); 2712 DEBUGLOG(4, "ZSTD_initDStream");
2618 return ZSTD_initDStream_usingDict(zds, NULL, 0); 2713 return ZSTD_initDStream_usingDict(zds, NULL, 0);
2619 }
2620
2621 size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
2622 {
2623 if (dctx->streamStage != zdss_init) return ERROR(stage_wrong);
2624 dctx->ddict = ddict;
2625 return 0;
2626 } 2714 }
2627 2715
2628 /* ZSTD_initDStream_usingDDict() : 2716 /* ZSTD_initDStream_usingDDict() :
2629 * ddict will just be referenced, and must outlive decompression session 2717 * ddict will just be referenced, and must outlive decompression session
2630 * this function cannot fail */ 2718 * this function cannot fail */
2658 case DStream_p_maxWindowSize : 2746 case DStream_p_maxWindowSize :
2659 DEBUGLOG(4, "setting maxWindowSize = %u KB", paramValue >> 10); 2747 DEBUGLOG(4, "setting maxWindowSize = %u KB", paramValue >> 10);
2660 dctx->maxWindowSize = paramValue ? paramValue : (U32)(-1); 2748 dctx->maxWindowSize = paramValue ? paramValue : (U32)(-1);
2661 break; 2749 break;
2662 } 2750 }
2751 return 0;
2752 }
2753
2754 size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
2755 {
2756 if (dctx->streamStage != zdss_init) return ERROR(stage_wrong);
2757 dctx->ddict = ddict;
2663 return 0; 2758 return 0;
2664 } 2759 }
2665 2760
2666 size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize) 2761 size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize)
2667 { 2762 {
2765 { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input); 2860 { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input);
2766 if (hint==0) zds->streamStage = zdss_init; 2861 if (hint==0) zds->streamStage = zdss_init;
2767 return hint; 2862 return hint;
2768 } } 2863 } }
2769 #endif 2864 #endif
2770 { size_t const hSize = ZSTD_getFrameHeader_internal(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format); 2865 { size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format);
2771 DEBUGLOG(5, "header size : %u", (U32)hSize); 2866 DEBUGLOG(5, "header size : %u", (U32)hSize);
2772 if (ZSTD_isError(hSize)) { 2867 if (ZSTD_isError(hSize)) {
2773 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) 2868 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
2774 U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart); 2869 U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart);
2775 if (legacyVersion) { 2870 if (legacyVersion) {
2826 /* Consume header (see ZSTDds_decodeFrameHeader) */ 2921 /* Consume header (see ZSTDds_decodeFrameHeader) */
2827 DEBUGLOG(4, "Consume header"); 2922 DEBUGLOG(4, "Consume header");
2828 CHECK_F(ZSTD_decompressBegin_usingDDict(zds, zds->ddict)); 2923 CHECK_F(ZSTD_decompressBegin_usingDDict(zds, zds->ddict));
2829 2924
2830 if ((MEM_readLE32(zds->headerBuffer) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */ 2925 if ((MEM_readLE32(zds->headerBuffer) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
2831 zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_frameIdSize); 2926 zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_FRAMEIDSIZE);
2832 zds->stage = ZSTDds_skipFrame; 2927 zds->stage = ZSTDds_skipFrame;
2833 } else { 2928 } else {
2834 CHECK_F(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize)); 2929 CHECK_F(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize));
2835 zds->expected = ZSTD_blockHeaderSize; 2930 zds->expected = ZSTD_blockHeaderSize;
2836 zds->stage = ZSTDds_decodeBlockHeader; 2931 zds->stage = ZSTDds_decodeBlockHeader;
2945 3040
2946 default: return ERROR(GENERIC); /* impossible */ 3041 default: return ERROR(GENERIC); /* impossible */
2947 } } 3042 } }
2948 3043
2949 /* result */ 3044 /* result */
2950 input->pos += (size_t)(ip-istart); 3045 input->pos = (size_t)(ip - (const char*)(input->src));
2951 output->pos += (size_t)(op-ostart); 3046 output->pos = (size_t)(op - (char*)(output->dst));
3047 if ((ip==istart) && (op==ostart)) { /* no forward progress */
3048 zds->noForwardProgress ++;
3049 if (zds->noForwardProgress >= ZSTD_NO_FORWARD_PROGRESS_MAX) {
3050 if (op==oend) return ERROR(dstSize_tooSmall);
3051 if (ip==iend) return ERROR(srcSize_wrong);
3052 assert(0);
3053 }
3054 } else {
3055 zds->noForwardProgress = 0;
3056 }
2952 { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds); 3057 { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds);
2953 if (!nextSrcSizeHint) { /* frame fully decoded */ 3058 if (!nextSrcSizeHint) { /* frame fully decoded */
2954 if (zds->outEnd == zds->outStart) { /* output fully flushed */ 3059 if (zds->outEnd == zds->outStart) { /* output fully flushed */
2955 if (zds->hostageByte) { 3060 if (zds->hostageByte) {
2956 if (input->pos >= input->size) { 3061 if (input->pos >= input->size) {