xref: /freebsd/sys/contrib/zstd/lib/decompress/zstd_decompress.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*
2  * Copyright (c) 2016-2020, 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 #  define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) + 1)
41 #endif
42 
43 /*!
44  *  NO_FORWARD_PROGRESS_MAX :
45  *  maximum allowed nb of calls to ZSTD_decompressStream()
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 
54 
55 /*-*******************************************************
56 *  Dependencies
57 *********************************************************/
58 #include "../common/zstd_deps.h"   /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */
59 #include "../common/cpu.h"         /* bmi2 */
60 #include "../common/mem.h"         /* low level memory routines */
61 #define FSE_STATIC_LINKING_ONLY
62 #include "../common/fse.h"
63 #define HUF_STATIC_LINKING_ONLY
64 #include "../common/huf.h"
65 #include "../common/zstd_internal.h"  /* blockProperties_t */
66 #include "zstd_decompress_internal.h"   /* ZSTD_DCtx */
67 #include "zstd_ddict.h"  /* ZSTD_DDictDictContent */
68 #include "zstd_decompress_block.h"   /* ZSTD_decompressBlock_internal */
69 
70 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
71 #  include "../legacy/zstd_legacy.h"
72 #endif
73 
74 
75 /*-*************************************************************
76 *   Context management
77 ***************************************************************/
78 size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx)
79 {
80     if (dctx==NULL) return 0;   /* support sizeof NULL */
81     return sizeof(*dctx)
82            + ZSTD_sizeof_DDict(dctx->ddictLocal)
83            + dctx->inBuffSize + dctx->outBuffSize;
84 }
85 
86 size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); }
87 
88 
89 static size_t ZSTD_startingInputLength(ZSTD_format_e format)
90 {
91     size_t const startingInputLength = ZSTD_FRAMEHEADERSIZE_PREFIX(format);
92     /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */
93     assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) );
94     return startingInputLength;
95 }
96 
97 static void ZSTD_DCtx_resetParameters(ZSTD_DCtx* dctx)
98 {
99     assert(dctx->streamStage == zdss_init);
100     dctx->format = ZSTD_f_zstd1;
101     dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
102     dctx->outBufferMode = ZSTD_bm_buffered;
103     dctx->forceIgnoreChecksum = ZSTD_d_validateChecksum;
104 }
105 
106 static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
107 {
108     dctx->staticSize  = 0;
109     dctx->ddict       = NULL;
110     dctx->ddictLocal  = NULL;
111     dctx->dictEnd     = NULL;
112     dctx->ddictIsCold = 0;
113     dctx->dictUses = ZSTD_dont_use;
114     dctx->inBuff      = NULL;
115     dctx->inBuffSize  = 0;
116     dctx->outBuffSize = 0;
117     dctx->streamStage = zdss_init;
118     dctx->legacyContext = NULL;
119     dctx->previousLegacyVersion = 0;
120     dctx->noForwardProgress = 0;
121     dctx->oversizedDuration = 0;
122     dctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid());
123     ZSTD_DCtx_resetParameters(dctx);
124     dctx->validateChecksum = 1;
125 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
126     dctx->dictContentEndForFuzzing = NULL;
127 #endif
128 }
129 
130 ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize)
131 {
132     ZSTD_DCtx* const dctx = (ZSTD_DCtx*) workspace;
133 
134     if ((size_t)workspace & 7) return NULL;  /* 8-aligned */
135     if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL;  /* minimum size */
136 
137     ZSTD_initDCtx_internal(dctx);
138     dctx->staticSize = workspaceSize;
139     dctx->inBuff = (char*)(dctx+1);
140     return dctx;
141 }
142 
143 ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
144 {
145     if ((!customMem.customAlloc) ^ (!customMem.customFree)) return NULL;
146 
147     {   ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_customMalloc(sizeof(*dctx), customMem);
148         if (!dctx) return NULL;
149         dctx->customMem = customMem;
150         ZSTD_initDCtx_internal(dctx);
151         return dctx;
152     }
153 }
154 
155 ZSTD_DCtx* ZSTD_createDCtx(void)
156 {
157     DEBUGLOG(3, "ZSTD_createDCtx");
158     return ZSTD_createDCtx_advanced(ZSTD_defaultCMem);
159 }
160 
161 static void ZSTD_clearDict(ZSTD_DCtx* dctx)
162 {
163     ZSTD_freeDDict(dctx->ddictLocal);
164     dctx->ddictLocal = NULL;
165     dctx->ddict = NULL;
166     dctx->dictUses = ZSTD_dont_use;
167 }
168 
169 size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
170 {
171     if (dctx==NULL) return 0;   /* support free on NULL */
172     RETURN_ERROR_IF(dctx->staticSize, memory_allocation, "not compatible with static DCtx");
173     {   ZSTD_customMem const cMem = dctx->customMem;
174         ZSTD_clearDict(dctx);
175         ZSTD_customFree(dctx->inBuff, cMem);
176         dctx->inBuff = NULL;
177 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
178         if (dctx->legacyContext)
179             ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion);
180 #endif
181         ZSTD_customFree(dctx, cMem);
182         return 0;
183     }
184 }
185 
186 /* no longer useful */
187 void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)
188 {
189     size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx);
190     ZSTD_memcpy(dstDCtx, srcDCtx, toCopy);  /* no need to copy workspace */
191 }
192 
193 
194 /*-*************************************************************
195  *   Frame header decoding
196  ***************************************************************/
197 
198 /*! ZSTD_isFrame() :
199  *  Tells if the content of `buffer` starts with a valid Frame Identifier.
200  *  Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
201  *  Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
202  *  Note 3 : Skippable Frame Identifiers are considered valid. */
203 unsigned ZSTD_isFrame(const void* buffer, size_t size)
204 {
205     if (size < ZSTD_FRAMEIDSIZE) return 0;
206     {   U32 const magic = MEM_readLE32(buffer);
207         if (magic == ZSTD_MAGICNUMBER) return 1;
208         if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1;
209     }
210 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
211     if (ZSTD_isLegacy(buffer, size)) return 1;
212 #endif
213     return 0;
214 }
215 
216 /** ZSTD_frameHeaderSize_internal() :
217  *  srcSize must be large enough to reach header size fields.
218  *  note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless.
219  * @return : size of the Frame Header
220  *           or an error code, which can be tested with ZSTD_isError() */
221 static size_t ZSTD_frameHeaderSize_internal(const void* src, size_t srcSize, ZSTD_format_e format)
222 {
223     size_t const minInputSize = ZSTD_startingInputLength(format);
224     RETURN_ERROR_IF(srcSize < minInputSize, srcSize_wrong, "");
225 
226     {   BYTE const fhd = ((const BYTE*)src)[minInputSize-1];
227         U32 const dictID= fhd & 3;
228         U32 const singleSegment = (fhd >> 5) & 1;
229         U32 const fcsId = fhd >> 6;
230         return minInputSize + !singleSegment
231              + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId]
232              + (singleSegment && !fcsId);
233     }
234 }
235 
236 /** ZSTD_frameHeaderSize() :
237  *  srcSize must be >= ZSTD_frameHeaderSize_prefix.
238  * @return : size of the Frame Header,
239  *           or an error code (if srcSize is too small) */
240 size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize)
241 {
242     return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1);
243 }
244 
245 
246 /** ZSTD_getFrameHeader_advanced() :
247  *  decode Frame Header, or require larger `srcSize`.
248  *  note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless
249  * @return : 0, `zfhPtr` is correctly filled,
250  *          >0, `srcSize` is too small, value is wanted `srcSize` amount,
251  *           or an error code, which can be tested using ZSTD_isError() */
252 size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format)
253 {
254     const BYTE* ip = (const BYTE*)src;
255     size_t const minInputSize = ZSTD_startingInputLength(format);
256 
257     ZSTD_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 */
258     if (srcSize < minInputSize) return minInputSize;
259     RETURN_ERROR_IF(src==NULL, GENERIC, "invalid parameter");
260 
261     if ( (format != ZSTD_f_zstd1_magicless)
262       && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) {
263         if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
264             /* skippable frame */
265             if (srcSize < ZSTD_SKIPPABLEHEADERSIZE)
266                 return ZSTD_SKIPPABLEHEADERSIZE; /* magic number + frame length */
267             ZSTD_memset(zfhPtr, 0, sizeof(*zfhPtr));
268             zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_FRAMEIDSIZE);
269             zfhPtr->frameType = ZSTD_skippableFrame;
270             return 0;
271         }
272         RETURN_ERROR(prefix_unknown, "");
273     }
274 
275     /* ensure there is enough `srcSize` to fully read/decode frame header */
276     {   size_t const fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format);
277         if (srcSize < fhsize) return fhsize;
278         zfhPtr->headerSize = (U32)fhsize;
279     }
280 
281     {   BYTE const fhdByte = ip[minInputSize-1];
282         size_t pos = minInputSize;
283         U32 const dictIDSizeCode = fhdByte&3;
284         U32 const checksumFlag = (fhdByte>>2)&1;
285         U32 const singleSegment = (fhdByte>>5)&1;
286         U32 const fcsID = fhdByte>>6;
287         U64 windowSize = 0;
288         U32 dictID = 0;
289         U64 frameContentSize = ZSTD_CONTENTSIZE_UNKNOWN;
290         RETURN_ERROR_IF((fhdByte & 0x08) != 0, frameParameter_unsupported,
291                         "reserved bits, must be zero");
292 
293         if (!singleSegment) {
294             BYTE const wlByte = ip[pos++];
295             U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN;
296             RETURN_ERROR_IF(windowLog > ZSTD_WINDOWLOG_MAX, frameParameter_windowTooLarge, "");
297             windowSize = (1ULL << windowLog);
298             windowSize += (windowSize >> 3) * (wlByte&7);
299         }
300         switch(dictIDSizeCode)
301         {
302             default: assert(0);  /* impossible */
303             case 0 : break;
304             case 1 : dictID = ip[pos]; pos++; break;
305             case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break;
306             case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break;
307         }
308         switch(fcsID)
309         {
310             default: assert(0);  /* impossible */
311             case 0 : if (singleSegment) frameContentSize = ip[pos]; break;
312             case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break;
313             case 2 : frameContentSize = MEM_readLE32(ip+pos); break;
314             case 3 : frameContentSize = MEM_readLE64(ip+pos); break;
315         }
316         if (singleSegment) windowSize = frameContentSize;
317 
318         zfhPtr->frameType = ZSTD_frame;
319         zfhPtr->frameContentSize = frameContentSize;
320         zfhPtr->windowSize = windowSize;
321         zfhPtr->blockSizeMax = (unsigned) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
322         zfhPtr->dictID = dictID;
323         zfhPtr->checksumFlag = checksumFlag;
324     }
325     return 0;
326 }
327 
328 /** ZSTD_getFrameHeader() :
329  *  decode Frame Header, or require larger `srcSize`.
330  *  note : this function does not consume input, it only reads it.
331  * @return : 0, `zfhPtr` is correctly filled,
332  *          >0, `srcSize` is too small, value is wanted `srcSize` amount,
333  *           or an error code, which can be tested using ZSTD_isError() */
334 size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize)
335 {
336     return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_f_zstd1);
337 }
338 
339 
340 /** ZSTD_getFrameContentSize() :
341  *  compatible with legacy mode
342  * @return : decompressed size of the single frame pointed to be `src` if known, otherwise
343  *         - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
344  *         - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */
345 unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize)
346 {
347 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
348     if (ZSTD_isLegacy(src, srcSize)) {
349         unsigned long long const ret = ZSTD_getDecompressedSize_legacy(src, srcSize);
350         return ret == 0 ? ZSTD_CONTENTSIZE_UNKNOWN : ret;
351     }
352 #endif
353     {   ZSTD_frameHeader zfh;
354         if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0)
355             return ZSTD_CONTENTSIZE_ERROR;
356         if (zfh.frameType == ZSTD_skippableFrame) {
357             return 0;
358         } else {
359             return zfh.frameContentSize;
360     }   }
361 }
362 
363 static size_t readSkippableFrameSize(void const* src, size_t srcSize)
364 {
365     size_t const skippableHeaderSize = ZSTD_SKIPPABLEHEADERSIZE;
366     U32 sizeU32;
367 
368     RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong, "");
369 
370     sizeU32 = MEM_readLE32((BYTE const*)src + ZSTD_FRAMEIDSIZE);
371     RETURN_ERROR_IF((U32)(sizeU32 + ZSTD_SKIPPABLEHEADERSIZE) < sizeU32,
372                     frameParameter_unsupported, "");
373     {
374         size_t const skippableSize = skippableHeaderSize + sizeU32;
375         RETURN_ERROR_IF(skippableSize > srcSize, srcSize_wrong, "");
376         return skippableSize;
377     }
378 }
379 
380 /** ZSTD_findDecompressedSize() :
381  *  compatible with legacy mode
382  *  `srcSize` must be the exact length of some number of ZSTD compressed and/or
383  *      skippable frames
384  *  @return : decompressed size of the frames contained */
385 unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize)
386 {
387     unsigned long long totalDstSize = 0;
388 
389     while (srcSize >= ZSTD_startingInputLength(ZSTD_f_zstd1)) {
390         U32 const magicNumber = MEM_readLE32(src);
391 
392         if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
393             size_t const skippableSize = readSkippableFrameSize(src, srcSize);
394             if (ZSTD_isError(skippableSize)) {
395                 return ZSTD_CONTENTSIZE_ERROR;
396             }
397             assert(skippableSize <= srcSize);
398 
399             src = (const BYTE *)src + skippableSize;
400             srcSize -= skippableSize;
401             continue;
402         }
403 
404         {   unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
405             if (ret >= ZSTD_CONTENTSIZE_ERROR) return ret;
406 
407             /* check for overflow */
408             if (totalDstSize + ret < totalDstSize) return ZSTD_CONTENTSIZE_ERROR;
409             totalDstSize += ret;
410         }
411         {   size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize);
412             if (ZSTD_isError(frameSrcSize)) {
413                 return ZSTD_CONTENTSIZE_ERROR;
414             }
415 
416             src = (const BYTE *)src + frameSrcSize;
417             srcSize -= frameSrcSize;
418         }
419     }  /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
420 
421     if (srcSize) return ZSTD_CONTENTSIZE_ERROR;
422 
423     return totalDstSize;
424 }
425 
426 /** ZSTD_getDecompressedSize() :
427  *  compatible with legacy mode
428  * @return : decompressed size if known, 0 otherwise
429              note : 0 can mean any of the following :
430                    - frame content is empty
431                    - decompressed size field is not present in frame header
432                    - frame header unknown / not supported
433                    - frame header not complete (`srcSize` too small) */
434 unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize)
435 {
436     unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
437     ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR < ZSTD_CONTENTSIZE_UNKNOWN);
438     return (ret >= ZSTD_CONTENTSIZE_ERROR) ? 0 : ret;
439 }
440 
441 
442 /** ZSTD_decodeFrameHeader() :
443  * `headerSize` must be the size provided by ZSTD_frameHeaderSize().
444  * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
445 static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize)
446 {
447     size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format);
448     if (ZSTD_isError(result)) return result;    /* invalid header */
449     RETURN_ERROR_IF(result>0, srcSize_wrong, "headerSize too small");
450 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
451     /* Skip the dictID check in fuzzing mode, because it makes the search
452      * harder.
453      */
454     RETURN_ERROR_IF(dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID),
455                     dictionary_wrong, "");
456 #endif
457     dctx->validateChecksum = (dctx->fParams.checksumFlag && !dctx->forceIgnoreChecksum) ? 1 : 0;
458     if (dctx->validateChecksum) XXH64_reset(&dctx->xxhState, 0);
459     return 0;
460 }
461 
462 static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret)
463 {
464     ZSTD_frameSizeInfo frameSizeInfo;
465     frameSizeInfo.compressedSize = ret;
466     frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
467     return frameSizeInfo;
468 }
469 
470 static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize)
471 {
472     ZSTD_frameSizeInfo frameSizeInfo;
473     ZSTD_memset(&frameSizeInfo, 0, sizeof(ZSTD_frameSizeInfo));
474 
475 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
476     if (ZSTD_isLegacy(src, srcSize))
477         return ZSTD_findFrameSizeInfoLegacy(src, srcSize);
478 #endif
479 
480     if ((srcSize >= ZSTD_SKIPPABLEHEADERSIZE)
481         && (MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
482         frameSizeInfo.compressedSize = readSkippableFrameSize(src, srcSize);
483         assert(ZSTD_isError(frameSizeInfo.compressedSize) ||
484                frameSizeInfo.compressedSize <= srcSize);
485         return frameSizeInfo;
486     } else {
487         const BYTE* ip = (const BYTE*)src;
488         const BYTE* const ipstart = ip;
489         size_t remainingSize = srcSize;
490         size_t nbBlocks = 0;
491         ZSTD_frameHeader zfh;
492 
493         /* Extract Frame Header */
494         {   size_t const ret = ZSTD_getFrameHeader(&zfh, src, srcSize);
495             if (ZSTD_isError(ret))
496                 return ZSTD_errorFrameSizeInfo(ret);
497             if (ret > 0)
498                 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
499         }
500 
501         ip += zfh.headerSize;
502         remainingSize -= zfh.headerSize;
503 
504         /* Iterate over each block */
505         while (1) {
506             blockProperties_t blockProperties;
507             size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
508             if (ZSTD_isError(cBlockSize))
509                 return ZSTD_errorFrameSizeInfo(cBlockSize);
510 
511             if (ZSTD_blockHeaderSize + cBlockSize > remainingSize)
512                 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
513 
514             ip += ZSTD_blockHeaderSize + cBlockSize;
515             remainingSize -= ZSTD_blockHeaderSize + cBlockSize;
516             nbBlocks++;
517 
518             if (blockProperties.lastBlock) break;
519         }
520 
521         /* Final frame content checksum */
522         if (zfh.checksumFlag) {
523             if (remainingSize < 4)
524                 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
525             ip += 4;
526         }
527 
528         frameSizeInfo.compressedSize = (size_t)(ip - ipstart);
529         frameSizeInfo.decompressedBound = (zfh.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN)
530                                         ? zfh.frameContentSize
531                                         : nbBlocks * zfh.blockSizeMax;
532         return frameSizeInfo;
533     }
534 }
535 
536 /** ZSTD_findFrameCompressedSize() :
537  *  compatible with legacy mode
538  *  `src` must point to the start of a ZSTD frame, ZSTD legacy frame, or skippable frame
539  *  `srcSize` must be at least as large as the frame contained
540  *  @return : the compressed size of the frame starting at `src` */
541 size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize)
542 {
543     ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize);
544     return frameSizeInfo.compressedSize;
545 }
546 
547 /** ZSTD_decompressBound() :
548  *  compatible with legacy mode
549  *  `src` must point to the start of a ZSTD frame or a skippeable frame
550  *  `srcSize` must be at least as large as the frame contained
551  *  @return : the maximum decompressed size of the compressed source
552  */
553 unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize)
554 {
555     unsigned long long bound = 0;
556     /* Iterate over each frame */
557     while (srcSize > 0) {
558         ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize);
559         size_t const compressedSize = frameSizeInfo.compressedSize;
560         unsigned long long const decompressedBound = frameSizeInfo.decompressedBound;
561         if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR)
562             return ZSTD_CONTENTSIZE_ERROR;
563         assert(srcSize >= compressedSize);
564         src = (const BYTE*)src + compressedSize;
565         srcSize -= compressedSize;
566         bound += decompressedBound;
567     }
568     return bound;
569 }
570 
571 
572 /*-*************************************************************
573  *   Frame decoding
574  ***************************************************************/
575 
576 /** ZSTD_insertBlock() :
577  *  insert `src` block into `dctx` history. Useful to track uncompressed blocks. */
578 size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize)
579 {
580     DEBUGLOG(5, "ZSTD_insertBlock: %u bytes", (unsigned)blockSize);
581     ZSTD_checkContinuity(dctx, blockStart);
582     dctx->previousDstEnd = (const char*)blockStart + blockSize;
583     return blockSize;
584 }
585 
586 
587 static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity,
588                           const void* src, size_t srcSize)
589 {
590     DEBUGLOG(5, "ZSTD_copyRawBlock");
591     RETURN_ERROR_IF(srcSize > dstCapacity, dstSize_tooSmall, "");
592     if (dst == NULL) {
593         if (srcSize == 0) return 0;
594         RETURN_ERROR(dstBuffer_null, "");
595     }
596     ZSTD_memcpy(dst, src, srcSize);
597     return srcSize;
598 }
599 
600 static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity,
601                                BYTE b,
602                                size_t regenSize)
603 {
604     RETURN_ERROR_IF(regenSize > dstCapacity, dstSize_tooSmall, "");
605     if (dst == NULL) {
606         if (regenSize == 0) return 0;
607         RETURN_ERROR(dstBuffer_null, "");
608     }
609     ZSTD_memset(dst, b, regenSize);
610     return regenSize;
611 }
612 
613 
614 /*! ZSTD_decompressFrame() :
615  * @dctx must be properly initialized
616  *  will update *srcPtr and *srcSizePtr,
617  *  to make *srcPtr progress by one frame. */
618 static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
619                                    void* dst, size_t dstCapacity,
620                              const void** srcPtr, size_t *srcSizePtr)
621 {
622     const BYTE* ip = (const BYTE*)(*srcPtr);
623     BYTE* const ostart = (BYTE* const)dst;
624     BYTE* const oend = dstCapacity != 0 ? ostart + dstCapacity : ostart;
625     BYTE* op = ostart;
626     size_t remainingSrcSize = *srcSizePtr;
627 
628     DEBUGLOG(4, "ZSTD_decompressFrame (srcSize:%i)", (int)*srcSizePtr);
629 
630     /* check */
631     RETURN_ERROR_IF(
632         remainingSrcSize < ZSTD_FRAMEHEADERSIZE_MIN(dctx->format)+ZSTD_blockHeaderSize,
633         srcSize_wrong, "");
634 
635     /* Frame Header */
636     {   size_t const frameHeaderSize = ZSTD_frameHeaderSize_internal(
637                 ip, ZSTD_FRAMEHEADERSIZE_PREFIX(dctx->format), dctx->format);
638         if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize;
639         RETURN_ERROR_IF(remainingSrcSize < frameHeaderSize+ZSTD_blockHeaderSize,
640                         srcSize_wrong, "");
641         FORWARD_IF_ERROR( ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize) , "");
642         ip += frameHeaderSize; remainingSrcSize -= frameHeaderSize;
643     }
644 
645     /* Loop on each block */
646     while (1) {
647         size_t decodedSize;
648         blockProperties_t blockProperties;
649         size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSrcSize, &blockProperties);
650         if (ZSTD_isError(cBlockSize)) return cBlockSize;
651 
652         ip += ZSTD_blockHeaderSize;
653         remainingSrcSize -= ZSTD_blockHeaderSize;
654         RETURN_ERROR_IF(cBlockSize > remainingSrcSize, srcSize_wrong, "");
655 
656         switch(blockProperties.blockType)
657         {
658         case bt_compressed:
659             decodedSize = ZSTD_decompressBlock_internal(dctx, op, (size_t)(oend-op), ip, cBlockSize, /* frame */ 1);
660             break;
661         case bt_raw :
662             decodedSize = ZSTD_copyRawBlock(op, (size_t)(oend-op), ip, cBlockSize);
663             break;
664         case bt_rle :
665             decodedSize = ZSTD_setRleBlock(op, (size_t)(oend-op), *ip, blockProperties.origSize);
666             break;
667         case bt_reserved :
668         default:
669             RETURN_ERROR(corruption_detected, "invalid block type");
670         }
671 
672         if (ZSTD_isError(decodedSize)) return decodedSize;
673         if (dctx->validateChecksum)
674             XXH64_update(&dctx->xxhState, op, decodedSize);
675         if (decodedSize != 0)
676             op += decodedSize;
677         assert(ip != NULL);
678         ip += cBlockSize;
679         remainingSrcSize -= cBlockSize;
680         if (blockProperties.lastBlock) break;
681     }
682 
683     if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) {
684         RETURN_ERROR_IF((U64)(op-ostart) != dctx->fParams.frameContentSize,
685                         corruption_detected, "");
686     }
687     if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */
688         RETURN_ERROR_IF(remainingSrcSize<4, checksum_wrong, "");
689         if (!dctx->forceIgnoreChecksum) {
690             U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState);
691             U32 checkRead;
692             checkRead = MEM_readLE32(ip);
693             RETURN_ERROR_IF(checkRead != checkCalc, checksum_wrong, "");
694         }
695         ip += 4;
696         remainingSrcSize -= 4;
697     }
698 
699     /* Allow caller to get size read */
700     *srcPtr = ip;
701     *srcSizePtr = remainingSrcSize;
702     return (size_t)(op-ostart);
703 }
704 
705 static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
706                                         void* dst, size_t dstCapacity,
707                                   const void* src, size_t srcSize,
708                                   const void* dict, size_t dictSize,
709                                   const ZSTD_DDict* ddict)
710 {
711     void* const dststart = dst;
712     int moreThan1Frame = 0;
713 
714     DEBUGLOG(5, "ZSTD_decompressMultiFrame");
715     assert(dict==NULL || ddict==NULL);  /* either dict or ddict set, not both */
716 
717     if (ddict) {
718         dict = ZSTD_DDict_dictContent(ddict);
719         dictSize = ZSTD_DDict_dictSize(ddict);
720     }
721 
722     while (srcSize >= ZSTD_startingInputLength(dctx->format)) {
723 
724 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
725         if (ZSTD_isLegacy(src, srcSize)) {
726             size_t decodedSize;
727             size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
728             if (ZSTD_isError(frameSize)) return frameSize;
729             RETURN_ERROR_IF(dctx->staticSize, memory_allocation,
730                 "legacy support is not compatible with static dctx");
731 
732             decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize);
733             if (ZSTD_isError(decodedSize)) return decodedSize;
734 
735             assert(decodedSize <= dstCapacity);
736             dst = (BYTE*)dst + decodedSize;
737             dstCapacity -= decodedSize;
738 
739             src = (const BYTE*)src + frameSize;
740             srcSize -= frameSize;
741 
742             continue;
743         }
744 #endif
745 
746         {   U32 const magicNumber = MEM_readLE32(src);
747             DEBUGLOG(4, "reading magic number %08X (expecting %08X)",
748                         (unsigned)magicNumber, ZSTD_MAGICNUMBER);
749             if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
750                 size_t const skippableSize = readSkippableFrameSize(src, srcSize);
751                 FORWARD_IF_ERROR(skippableSize, "readSkippableFrameSize failed");
752                 assert(skippableSize <= srcSize);
753 
754                 src = (const BYTE *)src + skippableSize;
755                 srcSize -= skippableSize;
756                 continue;
757         }   }
758 
759         if (ddict) {
760             /* we were called from ZSTD_decompress_usingDDict */
761             FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(dctx, ddict), "");
762         } else {
763             /* this will initialize correctly with no dict if dict == NULL, so
764              * use this in all cases but ddict */
765             FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize), "");
766         }
767         ZSTD_checkContinuity(dctx, dst);
768 
769         {   const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity,
770                                                     &src, &srcSize);
771             RETURN_ERROR_IF(
772                 (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown)
773              && (moreThan1Frame==1),
774                 srcSize_wrong,
775                 "At least one frame successfully completed, "
776                 "but following bytes are garbage: "
777                 "it's more likely to be a srcSize error, "
778                 "specifying more input bytes than size of frame(s). "
779                 "Note: one could be unlucky, it might be a corruption error instead, "
780                 "happening right at the place where we expect zstd magic bytes. "
781                 "But this is _much_ less likely than a srcSize field error.");
782             if (ZSTD_isError(res)) return res;
783             assert(res <= dstCapacity);
784             if (res != 0)
785                 dst = (BYTE*)dst + res;
786             dstCapacity -= res;
787         }
788         moreThan1Frame = 1;
789     }  /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
790 
791     RETURN_ERROR_IF(srcSize, srcSize_wrong, "input not entirely consumed");
792 
793     return (size_t)((BYTE*)dst - (BYTE*)dststart);
794 }
795 
796 size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
797                                  void* dst, size_t dstCapacity,
798                            const void* src, size_t srcSize,
799                            const void* dict, size_t dictSize)
800 {
801     return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL);
802 }
803 
804 
805 static ZSTD_DDict const* ZSTD_getDDict(ZSTD_DCtx* dctx)
806 {
807     switch (dctx->dictUses) {
808     default:
809         assert(0 /* Impossible */);
810         /* fall-through */
811     case ZSTD_dont_use:
812         ZSTD_clearDict(dctx);
813         return NULL;
814     case ZSTD_use_indefinitely:
815         return dctx->ddict;
816     case ZSTD_use_once:
817         dctx->dictUses = ZSTD_dont_use;
818         return dctx->ddict;
819     }
820 }
821 
822 size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
823 {
824     return ZSTD_decompress_usingDDict(dctx, dst, dstCapacity, src, srcSize, ZSTD_getDDict(dctx));
825 }
826 
827 
828 size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
829 {
830 #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1)
831     size_t regenSize;
832     ZSTD_DCtx* const dctx = ZSTD_createDCtx();
833     RETURN_ERROR_IF(dctx==NULL, memory_allocation, "NULL pointer!");
834     regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize);
835     ZSTD_freeDCtx(dctx);
836     return regenSize;
837 #else   /* stack mode */
838     ZSTD_DCtx dctx;
839     ZSTD_initDCtx_internal(&dctx);
840     return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);
841 #endif
842 }
843 
844 
845 /*-**************************************
846 *   Advanced Streaming Decompression API
847 *   Bufferless and synchronous
848 ****************************************/
849 size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; }
850 
851 /**
852  * Similar to ZSTD_nextSrcSizeToDecompress(), but when when a block input can be streamed,
853  * we allow taking a partial block as the input. Currently only raw uncompressed blocks can
854  * be streamed.
855  *
856  * For blocks that can be streamed, this allows us to reduce the latency until we produce
857  * output, and avoid copying the input.
858  *
859  * @param inputSize - The total amount of input that the caller currently has.
860  */
861 static size_t ZSTD_nextSrcSizeToDecompressWithInputSize(ZSTD_DCtx* dctx, size_t inputSize) {
862     if (!(dctx->stage == ZSTDds_decompressBlock || dctx->stage == ZSTDds_decompressLastBlock))
863         return dctx->expected;
864     if (dctx->bType != bt_raw)
865         return dctx->expected;
866     return MIN(MAX(inputSize, 1), dctx->expected);
867 }
868 
869 ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) {
870     switch(dctx->stage)
871     {
872     default:   /* should not happen */
873         assert(0);
874     case ZSTDds_getFrameHeaderSize:
875     case ZSTDds_decodeFrameHeader:
876         return ZSTDnit_frameHeader;
877     case ZSTDds_decodeBlockHeader:
878         return ZSTDnit_blockHeader;
879     case ZSTDds_decompressBlock:
880         return ZSTDnit_block;
881     case ZSTDds_decompressLastBlock:
882         return ZSTDnit_lastBlock;
883     case ZSTDds_checkChecksum:
884         return ZSTDnit_checksum;
885     case ZSTDds_decodeSkippableHeader:
886     case ZSTDds_skipFrame:
887         return ZSTDnit_skippableFrame;
888     }
889 }
890 
891 static int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; }
892 
893 /** ZSTD_decompressContinue() :
894  *  srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress())
895  *  @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)
896  *            or an error code, which can be tested using ZSTD_isError() */
897 size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
898 {
899     DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (unsigned)srcSize);
900     /* Sanity check */
901     RETURN_ERROR_IF(srcSize != ZSTD_nextSrcSizeToDecompressWithInputSize(dctx, srcSize), srcSize_wrong, "not allowed");
902     if (dstCapacity) ZSTD_checkContinuity(dctx, dst);
903 
904     switch (dctx->stage)
905     {
906     case ZSTDds_getFrameHeaderSize :
907         assert(src != NULL);
908         if (dctx->format == ZSTD_f_zstd1) {  /* allows header */
909             assert(srcSize >= ZSTD_FRAMEIDSIZE);  /* to read skippable magic number */
910             if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {        /* skippable frame */
911                 ZSTD_memcpy(dctx->headerBuffer, src, srcSize);
912                 dctx->expected = ZSTD_SKIPPABLEHEADERSIZE - srcSize;  /* remaining to load to get full skippable frame header */
913                 dctx->stage = ZSTDds_decodeSkippableHeader;
914                 return 0;
915         }   }
916         dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format);
917         if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize;
918         ZSTD_memcpy(dctx->headerBuffer, src, srcSize);
919         dctx->expected = dctx->headerSize - srcSize;
920         dctx->stage = ZSTDds_decodeFrameHeader;
921         return 0;
922 
923     case ZSTDds_decodeFrameHeader:
924         assert(src != NULL);
925         ZSTD_memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize);
926         FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize), "");
927         dctx->expected = ZSTD_blockHeaderSize;
928         dctx->stage = ZSTDds_decodeBlockHeader;
929         return 0;
930 
931     case ZSTDds_decodeBlockHeader:
932         {   blockProperties_t bp;
933             size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
934             if (ZSTD_isError(cBlockSize)) return cBlockSize;
935             RETURN_ERROR_IF(cBlockSize > dctx->fParams.blockSizeMax, corruption_detected, "Block Size Exceeds Maximum");
936             dctx->expected = cBlockSize;
937             dctx->bType = bp.blockType;
938             dctx->rleSize = bp.origSize;
939             if (cBlockSize) {
940                 dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock;
941                 return 0;
942             }
943             /* empty block */
944             if (bp.lastBlock) {
945                 if (dctx->fParams.checksumFlag) {
946                     dctx->expected = 4;
947                     dctx->stage = ZSTDds_checkChecksum;
948                 } else {
949                     dctx->expected = 0; /* end of frame */
950                     dctx->stage = ZSTDds_getFrameHeaderSize;
951                 }
952             } else {
953                 dctx->expected = ZSTD_blockHeaderSize;  /* jump to next header */
954                 dctx->stage = ZSTDds_decodeBlockHeader;
955             }
956             return 0;
957         }
958 
959     case ZSTDds_decompressLastBlock:
960     case ZSTDds_decompressBlock:
961         DEBUGLOG(5, "ZSTD_decompressContinue: case ZSTDds_decompressBlock");
962         {   size_t rSize;
963             switch(dctx->bType)
964             {
965             case bt_compressed:
966                 DEBUGLOG(5, "ZSTD_decompressContinue: case bt_compressed");
967                 rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 1);
968                 dctx->expected = 0;  /* Streaming not supported */
969                 break;
970             case bt_raw :
971                 assert(srcSize <= dctx->expected);
972                 rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize);
973                 FORWARD_IF_ERROR(rSize, "ZSTD_copyRawBlock failed");
974                 assert(rSize == srcSize);
975                 dctx->expected -= rSize;
976                 break;
977             case bt_rle :
978                 rSize = ZSTD_setRleBlock(dst, dstCapacity, *(const BYTE*)src, dctx->rleSize);
979                 dctx->expected = 0;  /* Streaming not supported */
980                 break;
981             case bt_reserved :   /* should never happen */
982             default:
983                 RETURN_ERROR(corruption_detected, "invalid block type");
984             }
985             FORWARD_IF_ERROR(rSize, "");
986             RETURN_ERROR_IF(rSize > dctx->fParams.blockSizeMax, corruption_detected, "Decompressed Block Size Exceeds Maximum");
987             DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (unsigned)rSize);
988             dctx->decodedSize += rSize;
989             if (dctx->validateChecksum) XXH64_update(&dctx->xxhState, dst, rSize);
990             dctx->previousDstEnd = (char*)dst + rSize;
991 
992             /* Stay on the same stage until we are finished streaming the block. */
993             if (dctx->expected > 0) {
994                 return rSize;
995             }
996 
997             if (dctx->stage == ZSTDds_decompressLastBlock) {   /* end of frame */
998                 DEBUGLOG(4, "ZSTD_decompressContinue: decoded size from frame : %u", (unsigned)dctx->decodedSize);
999                 RETURN_ERROR_IF(
1000                     dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
1001                  && dctx->decodedSize != dctx->fParams.frameContentSize,
1002                     corruption_detected, "");
1003                 if (dctx->fParams.checksumFlag) {  /* another round for frame checksum */
1004                     dctx->expected = 4;
1005                     dctx->stage = ZSTDds_checkChecksum;
1006                 } else {
1007                     dctx->expected = 0;   /* ends here */
1008                     dctx->stage = ZSTDds_getFrameHeaderSize;
1009                 }
1010             } else {
1011                 dctx->stage = ZSTDds_decodeBlockHeader;
1012                 dctx->expected = ZSTD_blockHeaderSize;
1013             }
1014             return rSize;
1015         }
1016 
1017     case ZSTDds_checkChecksum:
1018         assert(srcSize == 4);  /* guaranteed by dctx->expected */
1019         {
1020             if (dctx->validateChecksum) {
1021                 U32 const h32 = (U32)XXH64_digest(&dctx->xxhState);
1022                 U32 const check32 = MEM_readLE32(src);
1023                 DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", (unsigned)h32, (unsigned)check32);
1024                 RETURN_ERROR_IF(check32 != h32, checksum_wrong, "");
1025             }
1026             dctx->expected = 0;
1027             dctx->stage = ZSTDds_getFrameHeaderSize;
1028             return 0;
1029         }
1030 
1031     case ZSTDds_decodeSkippableHeader:
1032         assert(src != NULL);
1033         assert(srcSize <= ZSTD_SKIPPABLEHEADERSIZE);
1034         ZSTD_memcpy(dctx->headerBuffer + (ZSTD_SKIPPABLEHEADERSIZE - srcSize), src, srcSize);   /* complete skippable header */
1035         dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_FRAMEIDSIZE);   /* note : dctx->expected can grow seriously large, beyond local buffer size */
1036         dctx->stage = ZSTDds_skipFrame;
1037         return 0;
1038 
1039     case ZSTDds_skipFrame:
1040         dctx->expected = 0;
1041         dctx->stage = ZSTDds_getFrameHeaderSize;
1042         return 0;
1043 
1044     default:
1045         assert(0);   /* impossible */
1046         RETURN_ERROR(GENERIC, "impossible to reach");   /* some compiler require default to do something */
1047     }
1048 }
1049 
1050 
1051 static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1052 {
1053     dctx->dictEnd = dctx->previousDstEnd;
1054     dctx->virtualStart = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
1055     dctx->prefixStart = dict;
1056     dctx->previousDstEnd = (const char*)dict + dictSize;
1057 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1058     dctx->dictContentBeginForFuzzing = dctx->prefixStart;
1059     dctx->dictContentEndForFuzzing = dctx->previousDstEnd;
1060 #endif
1061     return 0;
1062 }
1063 
1064 /*! ZSTD_loadDEntropy() :
1065  *  dict : must point at beginning of a valid zstd dictionary.
1066  * @return : size of entropy tables read */
1067 size_t
1068 ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
1069                   const void* const dict, size_t const dictSize)
1070 {
1071     const BYTE* dictPtr = (const BYTE*)dict;
1072     const BYTE* const dictEnd = dictPtr + dictSize;
1073 
1074     RETURN_ERROR_IF(dictSize <= 8, dictionary_corrupted, "dict is too small");
1075     assert(MEM_readLE32(dict) == ZSTD_MAGIC_DICTIONARY);   /* dict must be valid */
1076     dictPtr += 8;   /* skip header = magic + dictID */
1077 
1078     ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, OFTable) == offsetof(ZSTD_entropyDTables_t, LLTable) + sizeof(entropy->LLTable));
1079     ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, MLTable) == offsetof(ZSTD_entropyDTables_t, OFTable) + sizeof(entropy->OFTable));
1080     ZSTD_STATIC_ASSERT(sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable) >= HUF_DECOMPRESS_WORKSPACE_SIZE);
1081     {   void* const workspace = &entropy->LLTable;   /* use fse tables as temporary workspace; implies fse tables are grouped together */
1082         size_t const workspaceSize = sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable);
1083 #ifdef HUF_FORCE_DECOMPRESS_X1
1084         /* in minimal huffman, we always use X1 variants */
1085         size_t const hSize = HUF_readDTableX1_wksp(entropy->hufTable,
1086                                                 dictPtr, dictEnd - dictPtr,
1087                                                 workspace, workspaceSize);
1088 #else
1089         size_t const hSize = HUF_readDTableX2_wksp(entropy->hufTable,
1090                                                 dictPtr, (size_t)(dictEnd - dictPtr),
1091                                                 workspace, workspaceSize);
1092 #endif
1093         RETURN_ERROR_IF(HUF_isError(hSize), dictionary_corrupted, "");
1094         dictPtr += hSize;
1095     }
1096 
1097     {   short offcodeNCount[MaxOff+1];
1098         unsigned offcodeMaxValue = MaxOff, offcodeLog;
1099         size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, (size_t)(dictEnd-dictPtr));
1100         RETURN_ERROR_IF(FSE_isError(offcodeHeaderSize), dictionary_corrupted, "");
1101         RETURN_ERROR_IF(offcodeMaxValue > MaxOff, dictionary_corrupted, "");
1102         RETURN_ERROR_IF(offcodeLog > OffFSELog, dictionary_corrupted, "");
1103         ZSTD_buildFSETable( entropy->OFTable,
1104                             offcodeNCount, offcodeMaxValue,
1105                             OF_base, OF_bits,
1106                             offcodeLog,
1107                             entropy->workspace, sizeof(entropy->workspace),
1108                             /* bmi2 */0);
1109         dictPtr += offcodeHeaderSize;
1110     }
1111 
1112     {   short matchlengthNCount[MaxML+1];
1113         unsigned matchlengthMaxValue = MaxML, matchlengthLog;
1114         size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, (size_t)(dictEnd-dictPtr));
1115         RETURN_ERROR_IF(FSE_isError(matchlengthHeaderSize), dictionary_corrupted, "");
1116         RETURN_ERROR_IF(matchlengthMaxValue > MaxML, dictionary_corrupted, "");
1117         RETURN_ERROR_IF(matchlengthLog > MLFSELog, dictionary_corrupted, "");
1118         ZSTD_buildFSETable( entropy->MLTable,
1119                             matchlengthNCount, matchlengthMaxValue,
1120                             ML_base, ML_bits,
1121                             matchlengthLog,
1122                             entropy->workspace, sizeof(entropy->workspace),
1123                             /* bmi2 */ 0);
1124         dictPtr += matchlengthHeaderSize;
1125     }
1126 
1127     {   short litlengthNCount[MaxLL+1];
1128         unsigned litlengthMaxValue = MaxLL, litlengthLog;
1129         size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, (size_t)(dictEnd-dictPtr));
1130         RETURN_ERROR_IF(FSE_isError(litlengthHeaderSize), dictionary_corrupted, "");
1131         RETURN_ERROR_IF(litlengthMaxValue > MaxLL, dictionary_corrupted, "");
1132         RETURN_ERROR_IF(litlengthLog > LLFSELog, dictionary_corrupted, "");
1133         ZSTD_buildFSETable( entropy->LLTable,
1134                             litlengthNCount, litlengthMaxValue,
1135                             LL_base, LL_bits,
1136                             litlengthLog,
1137                             entropy->workspace, sizeof(entropy->workspace),
1138                             /* bmi2 */ 0);
1139         dictPtr += litlengthHeaderSize;
1140     }
1141 
1142     RETURN_ERROR_IF(dictPtr+12 > dictEnd, dictionary_corrupted, "");
1143     {   int i;
1144         size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12));
1145         for (i=0; i<3; i++) {
1146             U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4;
1147             RETURN_ERROR_IF(rep==0 || rep > dictContentSize,
1148                             dictionary_corrupted, "");
1149             entropy->rep[i] = rep;
1150     }   }
1151 
1152     return (size_t)(dictPtr - (const BYTE*)dict);
1153 }
1154 
1155 static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1156 {
1157     if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize);
1158     {   U32 const magic = MEM_readLE32(dict);
1159         if (magic != ZSTD_MAGIC_DICTIONARY) {
1160             return ZSTD_refDictContent(dctx, dict, dictSize);   /* pure content mode */
1161     }   }
1162     dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
1163 
1164     /* load entropy tables */
1165     {   size_t const eSize = ZSTD_loadDEntropy(&dctx->entropy, dict, dictSize);
1166         RETURN_ERROR_IF(ZSTD_isError(eSize), dictionary_corrupted, "");
1167         dict = (const char*)dict + eSize;
1168         dictSize -= eSize;
1169     }
1170     dctx->litEntropy = dctx->fseEntropy = 1;
1171 
1172     /* reference dictionary content */
1173     return ZSTD_refDictContent(dctx, dict, dictSize);
1174 }
1175 
1176 size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
1177 {
1178     assert(dctx != NULL);
1179     dctx->expected = ZSTD_startingInputLength(dctx->format);  /* dctx->format must be properly set */
1180     dctx->stage = ZSTDds_getFrameHeaderSize;
1181     dctx->decodedSize = 0;
1182     dctx->previousDstEnd = NULL;
1183     dctx->prefixStart = NULL;
1184     dctx->virtualStart = NULL;
1185     dctx->dictEnd = NULL;
1186     dctx->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001);  /* cover both little and big endian */
1187     dctx->litEntropy = dctx->fseEntropy = 0;
1188     dctx->dictID = 0;
1189     dctx->bType = bt_reserved;
1190     ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue));
1191     ZSTD_memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue));  /* initial repcodes */
1192     dctx->LLTptr = dctx->entropy.LLTable;
1193     dctx->MLTptr = dctx->entropy.MLTable;
1194     dctx->OFTptr = dctx->entropy.OFTable;
1195     dctx->HUFptr = dctx->entropy.hufTable;
1196     return 0;
1197 }
1198 
1199 size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1200 {
1201     FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , "");
1202     if (dict && dictSize)
1203         RETURN_ERROR_IF(
1204             ZSTD_isError(ZSTD_decompress_insertDictionary(dctx, dict, dictSize)),
1205             dictionary_corrupted, "");
1206     return 0;
1207 }
1208 
1209 
1210 /* ======   ZSTD_DDict   ====== */
1211 
1212 size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
1213 {
1214     DEBUGLOG(4, "ZSTD_decompressBegin_usingDDict");
1215     assert(dctx != NULL);
1216     if (ddict) {
1217         const char* const dictStart = (const char*)ZSTD_DDict_dictContent(ddict);
1218         size_t const dictSize = ZSTD_DDict_dictSize(ddict);
1219         const void* const dictEnd = dictStart + dictSize;
1220         dctx->ddictIsCold = (dctx->dictEnd != dictEnd);
1221         DEBUGLOG(4, "DDict is %s",
1222                     dctx->ddictIsCold ? "~cold~" : "hot!");
1223     }
1224     FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , "");
1225     if (ddict) {   /* NULL ddict is equivalent to no dictionary */
1226         ZSTD_copyDDictParameters(dctx, ddict);
1227     }
1228     return 0;
1229 }
1230 
1231 /*! ZSTD_getDictID_fromDict() :
1232  *  Provides the dictID stored within dictionary.
1233  *  if @return == 0, the dictionary is not conformant with Zstandard specification.
1234  *  It can still be loaded, but as a content-only dictionary. */
1235 unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize)
1236 {
1237     if (dictSize < 8) return 0;
1238     if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0;
1239     return MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
1240 }
1241 
1242 /*! ZSTD_getDictID_fromFrame() :
1243  *  Provides the dictID required to decompress frame stored within `src`.
1244  *  If @return == 0, the dictID could not be decoded.
1245  *  This could for one of the following reasons :
1246  *  - The frame does not require a dictionary (most common case).
1247  *  - The frame was built with dictID intentionally removed.
1248  *    Needed dictionary is a hidden information.
1249  *    Note : this use case also happens when using a non-conformant dictionary.
1250  *  - `srcSize` is too small, and as a result, frame header could not be decoded.
1251  *    Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`.
1252  *  - This is not a Zstandard frame.
1253  *  When identifying the exact failure cause, it's possible to use
1254  *  ZSTD_getFrameHeader(), which will provide a more precise error code. */
1255 unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize)
1256 {
1257     ZSTD_frameHeader zfp = { 0, 0, 0, ZSTD_frame, 0, 0, 0 };
1258     size_t const hError = ZSTD_getFrameHeader(&zfp, src, srcSize);
1259     if (ZSTD_isError(hError)) return 0;
1260     return zfp.dictID;
1261 }
1262 
1263 
1264 /*! ZSTD_decompress_usingDDict() :
1265 *   Decompression using a pre-digested Dictionary
1266 *   Use dictionary without significant overhead. */
1267 size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
1268                                   void* dst, size_t dstCapacity,
1269                             const void* src, size_t srcSize,
1270                             const ZSTD_DDict* ddict)
1271 {
1272     /* pass content and size in case legacy frames are encountered */
1273     return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize,
1274                                      NULL, 0,
1275                                      ddict);
1276 }
1277 
1278 
1279 /*=====================================
1280 *   Streaming decompression
1281 *====================================*/
1282 
1283 ZSTD_DStream* ZSTD_createDStream(void)
1284 {
1285     DEBUGLOG(3, "ZSTD_createDStream");
1286     return ZSTD_createDStream_advanced(ZSTD_defaultCMem);
1287 }
1288 
1289 ZSTD_DStream* ZSTD_initStaticDStream(void *workspace, size_t workspaceSize)
1290 {
1291     return ZSTD_initStaticDCtx(workspace, workspaceSize);
1292 }
1293 
1294 ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem)
1295 {
1296     return ZSTD_createDCtx_advanced(customMem);
1297 }
1298 
1299 size_t ZSTD_freeDStream(ZSTD_DStream* zds)
1300 {
1301     return ZSTD_freeDCtx(zds);
1302 }
1303 
1304 
1305 /* ***  Initialization  *** */
1306 
1307 size_t ZSTD_DStreamInSize(void)  { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; }
1308 size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; }
1309 
1310 size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx,
1311                                    const void* dict, size_t dictSize,
1312                                          ZSTD_dictLoadMethod_e dictLoadMethod,
1313                                          ZSTD_dictContentType_e dictContentType)
1314 {
1315     RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1316     ZSTD_clearDict(dctx);
1317     if (dict && dictSize != 0) {
1318         dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem);
1319         RETURN_ERROR_IF(dctx->ddictLocal == NULL, memory_allocation, "NULL pointer!");
1320         dctx->ddict = dctx->ddictLocal;
1321         dctx->dictUses = ZSTD_use_indefinitely;
1322     }
1323     return 0;
1324 }
1325 
1326 size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1327 {
1328     return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto);
1329 }
1330 
1331 size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1332 {
1333     return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto);
1334 }
1335 
1336 size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType)
1337 {
1338     FORWARD_IF_ERROR(ZSTD_DCtx_loadDictionary_advanced(dctx, prefix, prefixSize, ZSTD_dlm_byRef, dictContentType), "");
1339     dctx->dictUses = ZSTD_use_once;
1340     return 0;
1341 }
1342 
1343 size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize)
1344 {
1345     return ZSTD_DCtx_refPrefix_advanced(dctx, prefix, prefixSize, ZSTD_dct_rawContent);
1346 }
1347 
1348 
1349 /* ZSTD_initDStream_usingDict() :
1350  * return : expected size, aka ZSTD_startingInputLength().
1351  * this function cannot fail */
1352 size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize)
1353 {
1354     DEBUGLOG(4, "ZSTD_initDStream_usingDict");
1355     FORWARD_IF_ERROR( ZSTD_DCtx_reset(zds, ZSTD_reset_session_only) , "");
1356     FORWARD_IF_ERROR( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) , "");
1357     return ZSTD_startingInputLength(zds->format);
1358 }
1359 
1360 /* note : this variant can't fail */
1361 size_t ZSTD_initDStream(ZSTD_DStream* zds)
1362 {
1363     DEBUGLOG(4, "ZSTD_initDStream");
1364     return ZSTD_initDStream_usingDDict(zds, NULL);
1365 }
1366 
1367 /* ZSTD_initDStream_usingDDict() :
1368  * ddict will just be referenced, and must outlive decompression session
1369  * this function cannot fail */
1370 size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* dctx, const ZSTD_DDict* ddict)
1371 {
1372     FORWARD_IF_ERROR( ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only) , "");
1373     FORWARD_IF_ERROR( ZSTD_DCtx_refDDict(dctx, ddict) , "");
1374     return ZSTD_startingInputLength(dctx->format);
1375 }
1376 
1377 /* ZSTD_resetDStream() :
1378  * return : expected size, aka ZSTD_startingInputLength().
1379  * this function cannot fail */
1380 size_t ZSTD_resetDStream(ZSTD_DStream* dctx)
1381 {
1382     FORWARD_IF_ERROR(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only), "");
1383     return ZSTD_startingInputLength(dctx->format);
1384 }
1385 
1386 
1387 size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
1388 {
1389     RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1390     ZSTD_clearDict(dctx);
1391     if (ddict) {
1392         dctx->ddict = ddict;
1393         dctx->dictUses = ZSTD_use_indefinitely;
1394     }
1395     return 0;
1396 }
1397 
1398 /* ZSTD_DCtx_setMaxWindowSize() :
1399  * note : no direct equivalence in ZSTD_DCtx_setParameter,
1400  * since this version sets windowSize, and the other sets windowLog */
1401 size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize)
1402 {
1403     ZSTD_bounds const bounds = ZSTD_dParam_getBounds(ZSTD_d_windowLogMax);
1404     size_t const min = (size_t)1 << bounds.lowerBound;
1405     size_t const max = (size_t)1 << bounds.upperBound;
1406     RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1407     RETURN_ERROR_IF(maxWindowSize < min, parameter_outOfBound, "");
1408     RETURN_ERROR_IF(maxWindowSize > max, parameter_outOfBound, "");
1409     dctx->maxWindowSize = maxWindowSize;
1410     return 0;
1411 }
1412 
1413 size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format)
1414 {
1415     return ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, (int)format);
1416 }
1417 
1418 ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam)
1419 {
1420     ZSTD_bounds bounds = { 0, 0, 0 };
1421     switch(dParam) {
1422         case ZSTD_d_windowLogMax:
1423             bounds.lowerBound = ZSTD_WINDOWLOG_ABSOLUTEMIN;
1424             bounds.upperBound = ZSTD_WINDOWLOG_MAX;
1425             return bounds;
1426         case ZSTD_d_format:
1427             bounds.lowerBound = (int)ZSTD_f_zstd1;
1428             bounds.upperBound = (int)ZSTD_f_zstd1_magicless;
1429             ZSTD_STATIC_ASSERT(ZSTD_f_zstd1 < ZSTD_f_zstd1_magicless);
1430             return bounds;
1431         case ZSTD_d_stableOutBuffer:
1432             bounds.lowerBound = (int)ZSTD_bm_buffered;
1433             bounds.upperBound = (int)ZSTD_bm_stable;
1434             return bounds;
1435         case ZSTD_d_forceIgnoreChecksum:
1436             bounds.lowerBound = (int)ZSTD_d_validateChecksum;
1437             bounds.upperBound = (int)ZSTD_d_ignoreChecksum;
1438             return bounds;
1439         default:;
1440     }
1441     bounds.error = ERROR(parameter_unsupported);
1442     return bounds;
1443 }
1444 
1445 /* ZSTD_dParam_withinBounds:
1446  * @return 1 if value is within dParam bounds,
1447  * 0 otherwise */
1448 static int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam, int value)
1449 {
1450     ZSTD_bounds const bounds = ZSTD_dParam_getBounds(dParam);
1451     if (ZSTD_isError(bounds.error)) return 0;
1452     if (value < bounds.lowerBound) return 0;
1453     if (value > bounds.upperBound) return 0;
1454     return 1;
1455 }
1456 
1457 #define CHECK_DBOUNDS(p,v) {                \
1458     RETURN_ERROR_IF(!ZSTD_dParam_withinBounds(p, v), parameter_outOfBound, ""); \
1459 }
1460 
1461 size_t ZSTD_DCtx_getParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int* value)
1462 {
1463     switch (param) {
1464         case ZSTD_d_windowLogMax:
1465             *value = (int)ZSTD_highbit32((U32)dctx->maxWindowSize);
1466             return 0;
1467         case ZSTD_d_format:
1468             *value = (int)dctx->format;
1469             return 0;
1470         case ZSTD_d_stableOutBuffer:
1471             *value = (int)dctx->outBufferMode;
1472             return 0;
1473         case ZSTD_d_forceIgnoreChecksum:
1474             *value = (int)dctx->forceIgnoreChecksum;
1475             return 0;
1476         default:;
1477     }
1478     RETURN_ERROR(parameter_unsupported, "");
1479 }
1480 
1481 size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value)
1482 {
1483     RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1484     switch(dParam) {
1485         case ZSTD_d_windowLogMax:
1486             if (value == 0) value = ZSTD_WINDOWLOG_LIMIT_DEFAULT;
1487             CHECK_DBOUNDS(ZSTD_d_windowLogMax, value);
1488             dctx->maxWindowSize = ((size_t)1) << value;
1489             return 0;
1490         case ZSTD_d_format:
1491             CHECK_DBOUNDS(ZSTD_d_format, value);
1492             dctx->format = (ZSTD_format_e)value;
1493             return 0;
1494         case ZSTD_d_stableOutBuffer:
1495             CHECK_DBOUNDS(ZSTD_d_stableOutBuffer, value);
1496             dctx->outBufferMode = (ZSTD_bufferMode_e)value;
1497             return 0;
1498         case ZSTD_d_forceIgnoreChecksum:
1499             CHECK_DBOUNDS(ZSTD_d_forceIgnoreChecksum, value);
1500             dctx->forceIgnoreChecksum = (ZSTD_forceIgnoreChecksum_e)value;
1501             return 0;
1502         default:;
1503     }
1504     RETURN_ERROR(parameter_unsupported, "");
1505 }
1506 
1507 size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset)
1508 {
1509     if ( (reset == ZSTD_reset_session_only)
1510       || (reset == ZSTD_reset_session_and_parameters) ) {
1511         dctx->streamStage = zdss_init;
1512         dctx->noForwardProgress = 0;
1513     }
1514     if ( (reset == ZSTD_reset_parameters)
1515       || (reset == ZSTD_reset_session_and_parameters) ) {
1516         RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1517         ZSTD_clearDict(dctx);
1518         ZSTD_DCtx_resetParameters(dctx);
1519     }
1520     return 0;
1521 }
1522 
1523 
1524 size_t ZSTD_sizeof_DStream(const ZSTD_DStream* dctx)
1525 {
1526     return ZSTD_sizeof_DCtx(dctx);
1527 }
1528 
1529 size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize)
1530 {
1531     size_t const blockSize = (size_t) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
1532     unsigned long long const neededRBSize = windowSize + blockSize + (WILDCOPY_OVERLENGTH * 2);
1533     unsigned long long const neededSize = MIN(frameContentSize, neededRBSize);
1534     size_t const minRBSize = (size_t) neededSize;
1535     RETURN_ERROR_IF((unsigned long long)minRBSize != neededSize,
1536                     frameParameter_windowTooLarge, "");
1537     return minRBSize;
1538 }
1539 
1540 size_t ZSTD_estimateDStreamSize(size_t windowSize)
1541 {
1542     size_t const blockSize = MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
1543     size_t const inBuffSize = blockSize;  /* no block can be larger */
1544     size_t const outBuffSize = ZSTD_decodingBufferSize_min(windowSize, ZSTD_CONTENTSIZE_UNKNOWN);
1545     return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize;
1546 }
1547 
1548 size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize)
1549 {
1550     U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX;   /* note : should be user-selectable, but requires an additional parameter (or a dctx) */
1551     ZSTD_frameHeader zfh;
1552     size_t const err = ZSTD_getFrameHeader(&zfh, src, srcSize);
1553     if (ZSTD_isError(err)) return err;
1554     RETURN_ERROR_IF(err>0, srcSize_wrong, "");
1555     RETURN_ERROR_IF(zfh.windowSize > windowSizeMax,
1556                     frameParameter_windowTooLarge, "");
1557     return ZSTD_estimateDStreamSize((size_t)zfh.windowSize);
1558 }
1559 
1560 
1561 /* *****   Decompression   ***** */
1562 
1563 static int ZSTD_DCtx_isOverflow(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize)
1564 {
1565     return (zds->inBuffSize + zds->outBuffSize) >= (neededInBuffSize + neededOutBuffSize) * ZSTD_WORKSPACETOOLARGE_FACTOR;
1566 }
1567 
1568 static void ZSTD_DCtx_updateOversizedDuration(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize)
1569 {
1570     if (ZSTD_DCtx_isOverflow(zds, neededInBuffSize, neededOutBuffSize))
1571         zds->oversizedDuration++;
1572     else
1573         zds->oversizedDuration = 0;
1574 }
1575 
1576 static int ZSTD_DCtx_isOversizedTooLong(ZSTD_DStream* zds)
1577 {
1578     return zds->oversizedDuration >= ZSTD_WORKSPACETOOLARGE_MAXDURATION;
1579 }
1580 
1581 /* Checks that the output buffer hasn't changed if ZSTD_obm_stable is used. */
1582 static size_t ZSTD_checkOutBuffer(ZSTD_DStream const* zds, ZSTD_outBuffer const* output)
1583 {
1584     ZSTD_outBuffer const expect = zds->expectedOutBuffer;
1585     /* No requirement when ZSTD_obm_stable is not enabled. */
1586     if (zds->outBufferMode != ZSTD_bm_stable)
1587         return 0;
1588     /* Any buffer is allowed in zdss_init, this must be the same for every other call until
1589      * the context is reset.
1590      */
1591     if (zds->streamStage == zdss_init)
1592         return 0;
1593     /* The buffer must match our expectation exactly. */
1594     if (expect.dst == output->dst && expect.pos == output->pos && expect.size == output->size)
1595         return 0;
1596     RETURN_ERROR(dstBuffer_wrong, "ZSTD_d_stableOutBuffer enabled but output differs!");
1597 }
1598 
1599 /* Calls ZSTD_decompressContinue() with the right parameters for ZSTD_decompressStream()
1600  * and updates the stage and the output buffer state. This call is extracted so it can be
1601  * used both when reading directly from the ZSTD_inBuffer, and in buffered input mode.
1602  * NOTE: You must break after calling this function since the streamStage is modified.
1603  */
1604 static size_t ZSTD_decompressContinueStream(
1605             ZSTD_DStream* zds, char** op, char* oend,
1606             void const* src, size_t srcSize) {
1607     int const isSkipFrame = ZSTD_isSkipFrame(zds);
1608     if (zds->outBufferMode == ZSTD_bm_buffered) {
1609         size_t const dstSize = isSkipFrame ? 0 : zds->outBuffSize - zds->outStart;
1610         size_t const decodedSize = ZSTD_decompressContinue(zds,
1611                 zds->outBuff + zds->outStart, dstSize, src, srcSize);
1612         FORWARD_IF_ERROR(decodedSize, "");
1613         if (!decodedSize && !isSkipFrame) {
1614             zds->streamStage = zdss_read;
1615         } else {
1616             zds->outEnd = zds->outStart + decodedSize;
1617             zds->streamStage = zdss_flush;
1618         }
1619     } else {
1620         /* Write directly into the output buffer */
1621         size_t const dstSize = isSkipFrame ? 0 : (size_t)(oend - *op);
1622         size_t const decodedSize = ZSTD_decompressContinue(zds, *op, dstSize, src, srcSize);
1623         FORWARD_IF_ERROR(decodedSize, "");
1624         *op += decodedSize;
1625         /* Flushing is not needed. */
1626         zds->streamStage = zdss_read;
1627         assert(*op <= oend);
1628         assert(zds->outBufferMode == ZSTD_bm_stable);
1629     }
1630     return 0;
1631 }
1632 
1633 size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
1634 {
1635     const char* const src = (const char*)input->src;
1636     const char* const istart = input->pos != 0 ? src + input->pos : src;
1637     const char* const iend = input->size != 0 ? src + input->size : src;
1638     const char* ip = istart;
1639     char* const dst = (char*)output->dst;
1640     char* const ostart = output->pos != 0 ? dst + output->pos : dst;
1641     char* const oend = output->size != 0 ? dst + output->size : dst;
1642     char* op = ostart;
1643     U32 someMoreWork = 1;
1644 
1645     DEBUGLOG(5, "ZSTD_decompressStream");
1646     RETURN_ERROR_IF(
1647         input->pos > input->size,
1648         srcSize_wrong,
1649         "forbidden. in: pos: %u   vs size: %u",
1650         (U32)input->pos, (U32)input->size);
1651     RETURN_ERROR_IF(
1652         output->pos > output->size,
1653         dstSize_tooSmall,
1654         "forbidden. out: pos: %u   vs size: %u",
1655         (U32)output->pos, (U32)output->size);
1656     DEBUGLOG(5, "input size : %u", (U32)(input->size - input->pos));
1657     FORWARD_IF_ERROR(ZSTD_checkOutBuffer(zds, output), "");
1658 
1659     while (someMoreWork) {
1660         switch(zds->streamStage)
1661         {
1662         case zdss_init :
1663             DEBUGLOG(5, "stage zdss_init => transparent reset ");
1664             zds->streamStage = zdss_loadHeader;
1665             zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
1666             zds->legacyVersion = 0;
1667             zds->hostageByte = 0;
1668             zds->expectedOutBuffer = *output;
1669             /* fall-through */
1670 
1671         case zdss_loadHeader :
1672             DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32)(iend - ip));
1673 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
1674             if (zds->legacyVersion) {
1675                 RETURN_ERROR_IF(zds->staticSize, memory_allocation,
1676                     "legacy support is incompatible with static dctx");
1677                 {   size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input);
1678                     if (hint==0) zds->streamStage = zdss_init;
1679                     return hint;
1680             }   }
1681 #endif
1682             {   size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format);
1683                 DEBUGLOG(5, "header size : %u", (U32)hSize);
1684                 if (ZSTD_isError(hSize)) {
1685 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
1686                     U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart);
1687                     if (legacyVersion) {
1688                         ZSTD_DDict const* const ddict = ZSTD_getDDict(zds);
1689                         const void* const dict = ddict ? ZSTD_DDict_dictContent(ddict) : NULL;
1690                         size_t const dictSize = ddict ? ZSTD_DDict_dictSize(ddict) : 0;
1691                         DEBUGLOG(5, "ZSTD_decompressStream: detected legacy version v0.%u", legacyVersion);
1692                         RETURN_ERROR_IF(zds->staticSize, memory_allocation,
1693                             "legacy support is incompatible with static dctx");
1694                         FORWARD_IF_ERROR(ZSTD_initLegacyStream(&zds->legacyContext,
1695                                     zds->previousLegacyVersion, legacyVersion,
1696                                     dict, dictSize), "");
1697                         zds->legacyVersion = zds->previousLegacyVersion = legacyVersion;
1698                         {   size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, legacyVersion, output, input);
1699                             if (hint==0) zds->streamStage = zdss_init;   /* or stay in stage zdss_loadHeader */
1700                             return hint;
1701                     }   }
1702 #endif
1703                     return hSize;   /* error */
1704                 }
1705                 if (hSize != 0) {   /* need more input */
1706                     size_t const toLoad = hSize - zds->lhSize;   /* if hSize!=0, hSize > zds->lhSize */
1707                     size_t const remainingInput = (size_t)(iend-ip);
1708                     assert(iend >= ip);
1709                     if (toLoad > remainingInput) {   /* not enough input to load full header */
1710                         if (remainingInput > 0) {
1711                             ZSTD_memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput);
1712                             zds->lhSize += remainingInput;
1713                         }
1714                         input->pos = input->size;
1715                         return (MAX((size_t)ZSTD_FRAMEHEADERSIZE_MIN(zds->format), hSize) - zds->lhSize) + ZSTD_blockHeaderSize;   /* remaining header bytes + next block header */
1716                     }
1717                     assert(ip != NULL);
1718                     ZSTD_memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad;
1719                     break;
1720             }   }
1721 
1722             /* check for single-pass mode opportunity */
1723             if (zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
1724                 && zds->fParams.frameType != ZSTD_skippableFrame
1725                 && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) {
1726                 size_t const cSize = ZSTD_findFrameCompressedSize(istart, (size_t)(iend-istart));
1727                 if (cSize <= (size_t)(iend-istart)) {
1728                     /* shortcut : using single-pass mode */
1729                     size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, (size_t)(oend-op), istart, cSize, ZSTD_getDDict(zds));
1730                     if (ZSTD_isError(decompressedSize)) return decompressedSize;
1731                     DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()")
1732                     ip = istart + cSize;
1733                     op += decompressedSize;
1734                     zds->expected = 0;
1735                     zds->streamStage = zdss_init;
1736                     someMoreWork = 0;
1737                     break;
1738             }   }
1739 
1740             /* Check output buffer is large enough for ZSTD_odm_stable. */
1741             if (zds->outBufferMode == ZSTD_bm_stable
1742                 && zds->fParams.frameType != ZSTD_skippableFrame
1743                 && zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
1744                 && (U64)(size_t)(oend-op) < zds->fParams.frameContentSize) {
1745                 RETURN_ERROR(dstSize_tooSmall, "ZSTD_obm_stable passed but ZSTD_outBuffer is too small");
1746             }
1747 
1748             /* Consume header (see ZSTDds_decodeFrameHeader) */
1749             DEBUGLOG(4, "Consume header");
1750             FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(zds, ZSTD_getDDict(zds)), "");
1751 
1752             if ((MEM_readLE32(zds->headerBuffer) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {  /* skippable frame */
1753                 zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_FRAMEIDSIZE);
1754                 zds->stage = ZSTDds_skipFrame;
1755             } else {
1756                 FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize), "");
1757                 zds->expected = ZSTD_blockHeaderSize;
1758                 zds->stage = ZSTDds_decodeBlockHeader;
1759             }
1760 
1761             /* control buffer memory usage */
1762             DEBUGLOG(4, "Control max memory usage (%u KB <= max %u KB)",
1763                         (U32)(zds->fParams.windowSize >>10),
1764                         (U32)(zds->maxWindowSize >> 10) );
1765             zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN);
1766             RETURN_ERROR_IF(zds->fParams.windowSize > zds->maxWindowSize,
1767                             frameParameter_windowTooLarge, "");
1768 
1769             /* Adapt buffer sizes to frame header instructions */
1770             {   size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */);
1771                 size_t const neededOutBuffSize = zds->outBufferMode == ZSTD_bm_buffered
1772                         ? ZSTD_decodingBufferSize_min(zds->fParams.windowSize, zds->fParams.frameContentSize)
1773                         : 0;
1774 
1775                 ZSTD_DCtx_updateOversizedDuration(zds, neededInBuffSize, neededOutBuffSize);
1776 
1777                 {   int const tooSmall = (zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize);
1778                     int const tooLarge = ZSTD_DCtx_isOversizedTooLong(zds);
1779 
1780                     if (tooSmall || tooLarge) {
1781                         size_t const bufferSize = neededInBuffSize + neededOutBuffSize;
1782                         DEBUGLOG(4, "inBuff  : from %u to %u",
1783                                     (U32)zds->inBuffSize, (U32)neededInBuffSize);
1784                         DEBUGLOG(4, "outBuff : from %u to %u",
1785                                     (U32)zds->outBuffSize, (U32)neededOutBuffSize);
1786                         if (zds->staticSize) {  /* static DCtx */
1787                             DEBUGLOG(4, "staticSize : %u", (U32)zds->staticSize);
1788                             assert(zds->staticSize >= sizeof(ZSTD_DCtx));  /* controlled at init */
1789                             RETURN_ERROR_IF(
1790                                 bufferSize > zds->staticSize - sizeof(ZSTD_DCtx),
1791                                 memory_allocation, "");
1792                         } else {
1793                             ZSTD_customFree(zds->inBuff, zds->customMem);
1794                             zds->inBuffSize = 0;
1795                             zds->outBuffSize = 0;
1796                             zds->inBuff = (char*)ZSTD_customMalloc(bufferSize, zds->customMem);
1797                             RETURN_ERROR_IF(zds->inBuff == NULL, memory_allocation, "");
1798                         }
1799                         zds->inBuffSize = neededInBuffSize;
1800                         zds->outBuff = zds->inBuff + zds->inBuffSize;
1801                         zds->outBuffSize = neededOutBuffSize;
1802             }   }   }
1803             zds->streamStage = zdss_read;
1804             /* fall-through */
1805 
1806         case zdss_read:
1807             DEBUGLOG(5, "stage zdss_read");
1808             {   size_t const neededInSize = ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (size_t)(iend - ip));
1809                 DEBUGLOG(5, "neededInSize = %u", (U32)neededInSize);
1810                 if (neededInSize==0) {  /* end of frame */
1811                     zds->streamStage = zdss_init;
1812                     someMoreWork = 0;
1813                     break;
1814                 }
1815                 if ((size_t)(iend-ip) >= neededInSize) {  /* decode directly from src */
1816                     FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, ip, neededInSize), "");
1817                     ip += neededInSize;
1818                     /* Function modifies the stage so we must break */
1819                     break;
1820             }   }
1821             if (ip==iend) { someMoreWork = 0; break; }   /* no more input */
1822             zds->streamStage = zdss_load;
1823             /* fall-through */
1824 
1825         case zdss_load:
1826             {   size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds);
1827                 size_t const toLoad = neededInSize - zds->inPos;
1828                 int const isSkipFrame = ZSTD_isSkipFrame(zds);
1829                 size_t loadedSize;
1830                 /* At this point we shouldn't be decompressing a block that we can stream. */
1831                 assert(neededInSize == ZSTD_nextSrcSizeToDecompressWithInputSize(zds, iend - ip));
1832                 if (isSkipFrame) {
1833                     loadedSize = MIN(toLoad, (size_t)(iend-ip));
1834                 } else {
1835                     RETURN_ERROR_IF(toLoad > zds->inBuffSize - zds->inPos,
1836                                     corruption_detected,
1837                                     "should never happen");
1838                     loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, (size_t)(iend-ip));
1839                 }
1840                 ip += loadedSize;
1841                 zds->inPos += loadedSize;
1842                 if (loadedSize < toLoad) { someMoreWork = 0; break; }   /* not enough input, wait for more */
1843 
1844                 /* decode loaded input */
1845                 zds->inPos = 0;   /* input is consumed */
1846                 FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, zds->inBuff, neededInSize), "");
1847                 /* Function modifies the stage so we must break */
1848                 break;
1849             }
1850         case zdss_flush:
1851             {   size_t const toFlushSize = zds->outEnd - zds->outStart;
1852                 size_t const flushedSize = ZSTD_limitCopy(op, (size_t)(oend-op), zds->outBuff + zds->outStart, toFlushSize);
1853                 op += flushedSize;
1854                 zds->outStart += flushedSize;
1855                 if (flushedSize == toFlushSize) {  /* flush completed */
1856                     zds->streamStage = zdss_read;
1857                     if ( (zds->outBuffSize < zds->fParams.frameContentSize)
1858                       && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize) ) {
1859                         DEBUGLOG(5, "restart filling outBuff from beginning (left:%i, needed:%u)",
1860                                 (int)(zds->outBuffSize - zds->outStart),
1861                                 (U32)zds->fParams.blockSizeMax);
1862                         zds->outStart = zds->outEnd = 0;
1863                     }
1864                     break;
1865             }   }
1866             /* cannot complete flush */
1867             someMoreWork = 0;
1868             break;
1869 
1870         default:
1871             assert(0);    /* impossible */
1872             RETURN_ERROR(GENERIC, "impossible to reach");   /* some compiler require default to do something */
1873     }   }
1874 
1875     /* result */
1876     input->pos = (size_t)(ip - (const char*)(input->src));
1877     output->pos = (size_t)(op - (char*)(output->dst));
1878 
1879     /* Update the expected output buffer for ZSTD_obm_stable. */
1880     zds->expectedOutBuffer = *output;
1881 
1882     if ((ip==istart) && (op==ostart)) {  /* no forward progress */
1883         zds->noForwardProgress ++;
1884         if (zds->noForwardProgress >= ZSTD_NO_FORWARD_PROGRESS_MAX) {
1885             RETURN_ERROR_IF(op==oend, dstSize_tooSmall, "");
1886             RETURN_ERROR_IF(ip==iend, srcSize_wrong, "");
1887             assert(0);
1888         }
1889     } else {
1890         zds->noForwardProgress = 0;
1891     }
1892     {   size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds);
1893         if (!nextSrcSizeHint) {   /* frame fully decoded */
1894             if (zds->outEnd == zds->outStart) {  /* output fully flushed */
1895                 if (zds->hostageByte) {
1896                     if (input->pos >= input->size) {
1897                         /* can't release hostage (not present) */
1898                         zds->streamStage = zdss_read;
1899                         return 1;
1900                     }
1901                     input->pos++;  /* release hostage */
1902                 }   /* zds->hostageByte */
1903                 return 0;
1904             }  /* zds->outEnd == zds->outStart */
1905             if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */
1906                 input->pos--;   /* note : pos > 0, otherwise, impossible to finish reading last block */
1907                 zds->hostageByte=1;
1908             }
1909             return 1;
1910         }  /* nextSrcSizeHint==0 */
1911         nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block);   /* preload header of next block */
1912         assert(zds->inPos <= nextSrcSizeHint);
1913         nextSrcSizeHint -= zds->inPos;   /* part already loaded*/
1914         return nextSrcSizeHint;
1915     }
1916 }
1917 
1918 size_t ZSTD_decompressStream_simpleArgs (
1919                             ZSTD_DCtx* dctx,
1920                             void* dst, size_t dstCapacity, size_t* dstPos,
1921                       const void* src, size_t srcSize, size_t* srcPos)
1922 {
1923     ZSTD_outBuffer output = { dst, dstCapacity, *dstPos };
1924     ZSTD_inBuffer  input  = { src, srcSize, *srcPos };
1925     /* ZSTD_compress_generic() will check validity of dstPos and srcPos */
1926     size_t const cErr = ZSTD_decompressStream(dctx, &output, &input);
1927     *dstPos = output.pos;
1928     *srcPos = input.pos;
1929     return cErr;
1930 }
1931