xref: /linux/lib/zstd/decompress/zstd_decompress.c (revision e61f33273ca755b3e2ebee4520a76097199dc7a8)
1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3  * Copyright (c) Meta Platforms, Inc. and affiliates.
4  * All rights reserved.
5  *
6  * This source code is licensed under both the BSD-style license (found in the
7  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
8  * in the COPYING file in the root directory of this source tree).
9  * You may select, at your option, one of the above-listed licenses.
10  */
11 
12 
13 /* ***************************************************************
14 *  Tuning parameters
15 *****************************************************************/
16 /*!
17  * HEAPMODE :
18  * Select how default decompression function ZSTD_decompress() allocates its context,
19  * on stack (0), or into heap (1, default; requires malloc()).
20  * Note that functions with explicit context such as ZSTD_decompressDCtx() are unaffected.
21  */
22 #ifndef ZSTD_HEAPMODE
23 #  define ZSTD_HEAPMODE 1
24 #endif
25 
26 /*!
27 *  LEGACY_SUPPORT :
28 *  if set to 1+, ZSTD_decompress() can decode older formats (v0.1+)
29 */
30 
31 /*!
32  *  MAXWINDOWSIZE_DEFAULT :
33  *  maximum window size accepted by DStream __by default__.
34  *  Frames requiring more memory will be rejected.
35  *  It's possible to set a different limit using ZSTD_DCtx_setMaxWindowSize().
36  */
37 #ifndef ZSTD_MAXWINDOWSIZE_DEFAULT
38 #  define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) + 1)
39 #endif
40 
41 /*!
42  *  NO_FORWARD_PROGRESS_MAX :
43  *  maximum allowed nb of calls to ZSTD_decompressStream()
44  *  without any forward progress
45  *  (defined as: no byte read from input, and no byte flushed to output)
46  *  before triggering an error.
47  */
48 #ifndef ZSTD_NO_FORWARD_PROGRESS_MAX
49 #  define ZSTD_NO_FORWARD_PROGRESS_MAX 16
50 #endif
51 
52 
53 /*-*******************************************************
54 *  Dependencies
55 *********************************************************/
56 #include "../common/zstd_deps.h"   /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */
57 #include "../common/allocations.h"  /* ZSTD_customMalloc, ZSTD_customCalloc, ZSTD_customFree */
58 #include "../common/error_private.h"
59 #include "../common/zstd_internal.h"  /* blockProperties_t */
60 #include "../common/mem.h"         /* low level memory routines */
61 #include "../common/bits.h"  /* ZSTD_highbit32 */
62 #define FSE_STATIC_LINKING_ONLY
63 #include "../common/fse.h"
64 #include "../common/huf.h"
65 #include <linux/xxhash.h> /* xxh64_reset, xxh64_update, xxh64_digest, XXH64 */
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 
71 
72 
73 /* ***********************************
74  * Multiple DDicts Hashset internals *
75  *************************************/
76 
77 #define DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT 4
78 #define DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT 3  /* These two constants represent SIZE_MULT/COUNT_MULT load factor without using a float.
79                                                     * Currently, that means a 0.75 load factor.
80                                                     * So, if count * COUNT_MULT / size * SIZE_MULT != 0, then we've exceeded
81                                                     * the load factor of the ddict hash set.
82                                                     */
83 
84 #define DDICT_HASHSET_TABLE_BASE_SIZE 64
85 #define DDICT_HASHSET_RESIZE_FACTOR 2
86 
87 /* Hash function to determine starting position of dict insertion within the table
88  * Returns an index between [0, hashSet->ddictPtrTableSize]
89  */
ZSTD_DDictHashSet_getIndex(const ZSTD_DDictHashSet * hashSet,U32 dictID)90 static size_t ZSTD_DDictHashSet_getIndex(const ZSTD_DDictHashSet* hashSet, U32 dictID) {
91     const U64 hash = xxh64(&dictID, sizeof(U32), 0);
92     /* DDict ptr table size is a multiple of 2, use size - 1 as mask to get index within [0, hashSet->ddictPtrTableSize) */
93     return hash & (hashSet->ddictPtrTableSize - 1);
94 }
95 
96 /* Adds DDict to a hashset without resizing it.
97  * If inserting a DDict with a dictID that already exists in the set, replaces the one in the set.
98  * Returns 0 if successful, or a zstd error code if something went wrong.
99  */
ZSTD_DDictHashSet_emplaceDDict(ZSTD_DDictHashSet * hashSet,const ZSTD_DDict * ddict)100 static size_t ZSTD_DDictHashSet_emplaceDDict(ZSTD_DDictHashSet* hashSet, const ZSTD_DDict* ddict) {
101     const U32 dictID = ZSTD_getDictID_fromDDict(ddict);
102     size_t idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID);
103     const size_t idxRangeMask = hashSet->ddictPtrTableSize - 1;
104     RETURN_ERROR_IF(hashSet->ddictPtrCount == hashSet->ddictPtrTableSize, GENERIC, "Hash set is full!");
105     DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID, idx);
106     while (hashSet->ddictPtrTable[idx] != NULL) {
107         /* Replace existing ddict if inserting ddict with same dictID */
108         if (ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]) == dictID) {
109             DEBUGLOG(4, "DictID already exists, replacing rather than adding");
110             hashSet->ddictPtrTable[idx] = ddict;
111             return 0;
112         }
113         idx &= idxRangeMask;
114         idx++;
115     }
116     DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID, idx);
117     hashSet->ddictPtrTable[idx] = ddict;
118     hashSet->ddictPtrCount++;
119     return 0;
120 }
121 
122 /* Expands hash table by factor of DDICT_HASHSET_RESIZE_FACTOR and
123  * rehashes all values, allocates new table, frees old table.
124  * Returns 0 on success, otherwise a zstd error code.
125  */
ZSTD_DDictHashSet_expand(ZSTD_DDictHashSet * hashSet,ZSTD_customMem customMem)126 static size_t ZSTD_DDictHashSet_expand(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) {
127     size_t newTableSize = hashSet->ddictPtrTableSize * DDICT_HASHSET_RESIZE_FACTOR;
128     const ZSTD_DDict** newTable = (const ZSTD_DDict**)ZSTD_customCalloc(sizeof(ZSTD_DDict*) * newTableSize, customMem);
129     const ZSTD_DDict** oldTable = hashSet->ddictPtrTable;
130     size_t oldTableSize = hashSet->ddictPtrTableSize;
131     size_t i;
132 
133     DEBUGLOG(4, "Expanding DDict hash table! Old size: %zu new size: %zu", oldTableSize, newTableSize);
134     RETURN_ERROR_IF(!newTable, memory_allocation, "Expanded hashset allocation failed!");
135     hashSet->ddictPtrTable = newTable;
136     hashSet->ddictPtrTableSize = newTableSize;
137     hashSet->ddictPtrCount = 0;
138     for (i = 0; i < oldTableSize; ++i) {
139         if (oldTable[i] != NULL) {
140             FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet, oldTable[i]), "");
141         }
142     }
143     ZSTD_customFree((void*)oldTable, customMem);
144     DEBUGLOG(4, "Finished re-hash");
145     return 0;
146 }
147 
148 /* Fetches a DDict with the given dictID
149  * Returns the ZSTD_DDict* with the requested dictID. If it doesn't exist, then returns NULL.
150  */
ZSTD_DDictHashSet_getDDict(ZSTD_DDictHashSet * hashSet,U32 dictID)151 static const ZSTD_DDict* ZSTD_DDictHashSet_getDDict(ZSTD_DDictHashSet* hashSet, U32 dictID) {
152     size_t idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID);
153     const size_t idxRangeMask = hashSet->ddictPtrTableSize - 1;
154     DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID, idx);
155     for (;;) {
156         size_t currDictID = ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]);
157         if (currDictID == dictID || currDictID == 0) {
158             /* currDictID == 0 implies a NULL ddict entry */
159             break;
160         } else {
161             idx &= idxRangeMask;    /* Goes to start of table when we reach the end */
162             idx++;
163         }
164     }
165     DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID, idx);
166     return hashSet->ddictPtrTable[idx];
167 }
168 
169 /* Allocates space for and returns a ddict hash set
170  * The hash set's ZSTD_DDict* table has all values automatically set to NULL to begin with.
171  * Returns NULL if allocation failed.
172  */
ZSTD_createDDictHashSet(ZSTD_customMem customMem)173 static ZSTD_DDictHashSet* ZSTD_createDDictHashSet(ZSTD_customMem customMem) {
174     ZSTD_DDictHashSet* ret = (ZSTD_DDictHashSet*)ZSTD_customMalloc(sizeof(ZSTD_DDictHashSet), customMem);
175     DEBUGLOG(4, "Allocating new hash set");
176     if (!ret)
177         return NULL;
178     ret->ddictPtrTable = (const ZSTD_DDict**)ZSTD_customCalloc(DDICT_HASHSET_TABLE_BASE_SIZE * sizeof(ZSTD_DDict*), customMem);
179     if (!ret->ddictPtrTable) {
180         ZSTD_customFree(ret, customMem);
181         return NULL;
182     }
183     ret->ddictPtrTableSize = DDICT_HASHSET_TABLE_BASE_SIZE;
184     ret->ddictPtrCount = 0;
185     return ret;
186 }
187 
188 /* Frees the table of ZSTD_DDict* within a hashset, then frees the hashset itself.
189  * Note: The ZSTD_DDict* within the table are NOT freed.
190  */
ZSTD_freeDDictHashSet(ZSTD_DDictHashSet * hashSet,ZSTD_customMem customMem)191 static void ZSTD_freeDDictHashSet(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) {
192     DEBUGLOG(4, "Freeing ddict hash set");
193     if (hashSet && hashSet->ddictPtrTable) {
194         ZSTD_customFree((void*)hashSet->ddictPtrTable, customMem);
195     }
196     if (hashSet) {
197         ZSTD_customFree(hashSet, customMem);
198     }
199 }
200 
201 /* Public function: Adds a DDict into the ZSTD_DDictHashSet, possibly triggering a resize of the hash set.
202  * Returns 0 on success, or a ZSTD error.
203  */
ZSTD_DDictHashSet_addDDict(ZSTD_DDictHashSet * hashSet,const ZSTD_DDict * ddict,ZSTD_customMem customMem)204 static size_t ZSTD_DDictHashSet_addDDict(ZSTD_DDictHashSet* hashSet, const ZSTD_DDict* ddict, ZSTD_customMem customMem) {
205     DEBUGLOG(4, "Adding dict ID: %u to hashset with - Count: %zu Tablesize: %zu", ZSTD_getDictID_fromDDict(ddict), hashSet->ddictPtrCount, hashSet->ddictPtrTableSize);
206     if (hashSet->ddictPtrCount * DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT / hashSet->ddictPtrTableSize * DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT != 0) {
207         FORWARD_IF_ERROR(ZSTD_DDictHashSet_expand(hashSet, customMem), "");
208     }
209     FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet, ddict), "");
210     return 0;
211 }
212 
213 /*-*************************************************************
214 *   Context management
215 ***************************************************************/
ZSTD_sizeof_DCtx(const ZSTD_DCtx * dctx)216 size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx)
217 {
218     if (dctx==NULL) return 0;   /* support sizeof NULL */
219     return sizeof(*dctx)
220            + ZSTD_sizeof_DDict(dctx->ddictLocal)
221            + dctx->inBuffSize + dctx->outBuffSize;
222 }
223 
ZSTD_estimateDCtxSize(void)224 size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); }
225 
226 
ZSTD_startingInputLength(ZSTD_format_e format)227 static size_t ZSTD_startingInputLength(ZSTD_format_e format)
228 {
229     size_t const startingInputLength = ZSTD_FRAMEHEADERSIZE_PREFIX(format);
230     /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */
231     assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) );
232     return startingInputLength;
233 }
234 
ZSTD_DCtx_resetParameters(ZSTD_DCtx * dctx)235 static void ZSTD_DCtx_resetParameters(ZSTD_DCtx* dctx)
236 {
237     assert(dctx->streamStage == zdss_init);
238     dctx->format = ZSTD_f_zstd1;
239     dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
240     dctx->outBufferMode = ZSTD_bm_buffered;
241     dctx->forceIgnoreChecksum = ZSTD_d_validateChecksum;
242     dctx->refMultipleDDicts = ZSTD_rmd_refSingleDDict;
243     dctx->disableHufAsm = 0;
244     dctx->maxBlockSizeParam = 0;
245 }
246 
ZSTD_initDCtx_internal(ZSTD_DCtx * dctx)247 static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
248 {
249     dctx->staticSize  = 0;
250     dctx->ddict       = NULL;
251     dctx->ddictLocal  = NULL;
252     dctx->dictEnd     = NULL;
253     dctx->ddictIsCold = 0;
254     dctx->dictUses = ZSTD_dont_use;
255     dctx->inBuff      = NULL;
256     dctx->inBuffSize  = 0;
257     dctx->outBuffSize = 0;
258     dctx->streamStage = zdss_init;
259     dctx->noForwardProgress = 0;
260     dctx->oversizedDuration = 0;
261     dctx->isFrameDecompression = 1;
262 #if DYNAMIC_BMI2
263     dctx->bmi2 = ZSTD_cpuSupportsBmi2();
264 #endif
265     dctx->ddictSet = NULL;
266     ZSTD_DCtx_resetParameters(dctx);
267 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
268     dctx->dictContentEndForFuzzing = NULL;
269 #endif
270 }
271 
ZSTD_initStaticDCtx(void * workspace,size_t workspaceSize)272 ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize)
273 {
274     ZSTD_DCtx* const dctx = (ZSTD_DCtx*) workspace;
275 
276     if ((size_t)workspace & 7) return NULL;  /* 8-aligned */
277     if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL;  /* minimum size */
278 
279     ZSTD_initDCtx_internal(dctx);
280     dctx->staticSize = workspaceSize;
281     dctx->inBuff = (char*)(dctx+1);
282     return dctx;
283 }
284 
ZSTD_createDCtx_internal(ZSTD_customMem customMem)285 static ZSTD_DCtx* ZSTD_createDCtx_internal(ZSTD_customMem customMem) {
286     if ((!customMem.customAlloc) ^ (!customMem.customFree)) return NULL;
287 
288     {   ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_customMalloc(sizeof(*dctx), customMem);
289         if (!dctx) return NULL;
290         dctx->customMem = customMem;
291         ZSTD_initDCtx_internal(dctx);
292         return dctx;
293     }
294 }
295 
ZSTD_createDCtx_advanced(ZSTD_customMem customMem)296 ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
297 {
298     return ZSTD_createDCtx_internal(customMem);
299 }
300 
ZSTD_createDCtx(void)301 ZSTD_DCtx* ZSTD_createDCtx(void)
302 {
303     DEBUGLOG(3, "ZSTD_createDCtx");
304     return ZSTD_createDCtx_internal(ZSTD_defaultCMem);
305 }
306 
ZSTD_clearDict(ZSTD_DCtx * dctx)307 static void ZSTD_clearDict(ZSTD_DCtx* dctx)
308 {
309     ZSTD_freeDDict(dctx->ddictLocal);
310     dctx->ddictLocal = NULL;
311     dctx->ddict = NULL;
312     dctx->dictUses = ZSTD_dont_use;
313 }
314 
ZSTD_freeDCtx(ZSTD_DCtx * dctx)315 size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
316 {
317     if (dctx==NULL) return 0;   /* support free on NULL */
318     RETURN_ERROR_IF(dctx->staticSize, memory_allocation, "not compatible with static DCtx");
319     {   ZSTD_customMem const cMem = dctx->customMem;
320         ZSTD_clearDict(dctx);
321         ZSTD_customFree(dctx->inBuff, cMem);
322         dctx->inBuff = NULL;
323         if (dctx->ddictSet) {
324             ZSTD_freeDDictHashSet(dctx->ddictSet, cMem);
325             dctx->ddictSet = NULL;
326         }
327         ZSTD_customFree(dctx, cMem);
328         return 0;
329     }
330 }
331 
332 /* no longer useful */
ZSTD_copyDCtx(ZSTD_DCtx * dstDCtx,const ZSTD_DCtx * srcDCtx)333 void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)
334 {
335     size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx);
336     ZSTD_memcpy(dstDCtx, srcDCtx, toCopy);  /* no need to copy workspace */
337 }
338 
339 /* Given a dctx with a digested frame params, re-selects the correct ZSTD_DDict based on
340  * the requested dict ID from the frame. If there exists a reference to the correct ZSTD_DDict, then
341  * accordingly sets the ddict to be used to decompress the frame.
342  *
343  * If no DDict is found, then no action is taken, and the ZSTD_DCtx::ddict remains as-is.
344  *
345  * ZSTD_d_refMultipleDDicts must be enabled for this function to be called.
346  */
ZSTD_DCtx_selectFrameDDict(ZSTD_DCtx * dctx)347 static void ZSTD_DCtx_selectFrameDDict(ZSTD_DCtx* dctx) {
348     assert(dctx->refMultipleDDicts && dctx->ddictSet);
349     DEBUGLOG(4, "Adjusting DDict based on requested dict ID from frame");
350     if (dctx->ddict) {
351         const ZSTD_DDict* frameDDict = ZSTD_DDictHashSet_getDDict(dctx->ddictSet, dctx->fParams.dictID);
352         if (frameDDict) {
353             DEBUGLOG(4, "DDict found!");
354             ZSTD_clearDict(dctx);
355             dctx->dictID = dctx->fParams.dictID;
356             dctx->ddict = frameDDict;
357             dctx->dictUses = ZSTD_use_indefinitely;
358         }
359     }
360 }
361 
362 
363 /*-*************************************************************
364  *   Frame header decoding
365  ***************************************************************/
366 
367 /*! ZSTD_isFrame() :
368  *  Tells if the content of `buffer` starts with a valid Frame Identifier.
369  *  Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
370  *  Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
371  *  Note 3 : Skippable Frame Identifiers are considered valid. */
ZSTD_isFrame(const void * buffer,size_t size)372 unsigned ZSTD_isFrame(const void* buffer, size_t size)
373 {
374     if (size < ZSTD_FRAMEIDSIZE) return 0;
375     {   U32 const magic = MEM_readLE32(buffer);
376         if (magic == ZSTD_MAGICNUMBER) return 1;
377         if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1;
378     }
379     return 0;
380 }
381 
382 /*! ZSTD_isSkippableFrame() :
383  *  Tells if the content of `buffer` starts with a valid Frame Identifier for a skippable frame.
384  *  Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
385  */
ZSTD_isSkippableFrame(const void * buffer,size_t size)386 unsigned ZSTD_isSkippableFrame(const void* buffer, size_t size)
387 {
388     if (size < ZSTD_FRAMEIDSIZE) return 0;
389     {   U32 const magic = MEM_readLE32(buffer);
390         if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1;
391     }
392     return 0;
393 }
394 
395 /* ZSTD_frameHeaderSize_internal() :
396  *  srcSize must be large enough to reach header size fields.
397  *  note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless.
398  * @return : size of the Frame Header
399  *           or an error code, which can be tested with ZSTD_isError() */
ZSTD_frameHeaderSize_internal(const void * src,size_t srcSize,ZSTD_format_e format)400 static size_t ZSTD_frameHeaderSize_internal(const void* src, size_t srcSize, ZSTD_format_e format)
401 {
402     size_t const minInputSize = ZSTD_startingInputLength(format);
403     RETURN_ERROR_IF(srcSize < minInputSize, srcSize_wrong, "");
404 
405     {   BYTE const fhd = ((const BYTE*)src)[minInputSize-1];
406         U32 const dictID= fhd & 3;
407         U32 const singleSegment = (fhd >> 5) & 1;
408         U32 const fcsId = fhd >> 6;
409         return minInputSize + !singleSegment
410              + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId]
411              + (singleSegment && !fcsId);
412     }
413 }
414 
415 /* ZSTD_frameHeaderSize() :
416  *  srcSize must be >= ZSTD_frameHeaderSize_prefix.
417  * @return : size of the Frame Header,
418  *           or an error code (if srcSize is too small) */
ZSTD_frameHeaderSize(const void * src,size_t srcSize)419 size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize)
420 {
421     return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1);
422 }
423 
424 
425 /* ZSTD_getFrameHeader_advanced() :
426  *  decode Frame Header, or require larger `srcSize`.
427  *  note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless
428  * @return : 0, `zfhPtr` is correctly filled,
429  *          >0, `srcSize` is too small, value is wanted `srcSize` amount,
430 **           or an error code, which can be tested using ZSTD_isError() */
ZSTD_getFrameHeader_advanced(ZSTD_FrameHeader * zfhPtr,const void * src,size_t srcSize,ZSTD_format_e format)431 size_t ZSTD_getFrameHeader_advanced(ZSTD_FrameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format)
432 {
433     const BYTE* ip = (const BYTE*)src;
434     size_t const minInputSize = ZSTD_startingInputLength(format);
435 
436     DEBUGLOG(5, "ZSTD_getFrameHeader_advanced: minInputSize = %zu, srcSize = %zu", minInputSize, srcSize);
437 
438     if (srcSize > 0) {
439         /* note : technically could be considered an assert(), since it's an invalid entry */
440         RETURN_ERROR_IF(src==NULL, GENERIC, "invalid parameter : src==NULL, but srcSize>0");
441     }
442     if (srcSize < minInputSize) {
443         if (srcSize > 0 && format != ZSTD_f_zstd1_magicless) {
444             /* when receiving less than @minInputSize bytes,
445              * control these bytes at least correspond to a supported magic number
446              * in order to error out early if they don't.
447             **/
448             size_t const toCopy = MIN(4, srcSize);
449             unsigned char hbuf[4]; MEM_writeLE32(hbuf, ZSTD_MAGICNUMBER);
450             assert(src != NULL);
451             ZSTD_memcpy(hbuf, src, toCopy);
452             if ( MEM_readLE32(hbuf) != ZSTD_MAGICNUMBER ) {
453                 /* not a zstd frame : let's check if it's a skippable frame */
454                 MEM_writeLE32(hbuf, ZSTD_MAGIC_SKIPPABLE_START);
455                 ZSTD_memcpy(hbuf, src, toCopy);
456                 if ((MEM_readLE32(hbuf) & ZSTD_MAGIC_SKIPPABLE_MASK) != ZSTD_MAGIC_SKIPPABLE_START) {
457                     RETURN_ERROR(prefix_unknown,
458                                 "first bytes don't correspond to any supported magic number");
459         }   }   }
460         return minInputSize;
461     }
462 
463     ZSTD_memset(zfhPtr, 0, sizeof(*zfhPtr));   /* not strictly necessary, but static analyzers may not understand that zfhPtr will be read only if return value is zero, since they are 2 different signals */
464     if ( (format != ZSTD_f_zstd1_magicless)
465       && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) {
466         if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
467             /* skippable frame */
468             if (srcSize < ZSTD_SKIPPABLEHEADERSIZE)
469                 return ZSTD_SKIPPABLEHEADERSIZE; /* magic number + frame length */
470             ZSTD_memset(zfhPtr, 0, sizeof(*zfhPtr));
471             zfhPtr->frameType = ZSTD_skippableFrame;
472             zfhPtr->dictID = MEM_readLE32(src) - ZSTD_MAGIC_SKIPPABLE_START;
473             zfhPtr->headerSize = ZSTD_SKIPPABLEHEADERSIZE;
474             zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_FRAMEIDSIZE);
475             return 0;
476         }
477         RETURN_ERROR(prefix_unknown, "");
478     }
479 
480     /* ensure there is enough `srcSize` to fully read/decode frame header */
481     {   size_t const fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format);
482         if (srcSize < fhsize) return fhsize;
483         zfhPtr->headerSize = (U32)fhsize;
484     }
485 
486     {   BYTE const fhdByte = ip[minInputSize-1];
487         size_t pos = minInputSize;
488         U32 const dictIDSizeCode = fhdByte&3;
489         U32 const checksumFlag = (fhdByte>>2)&1;
490         U32 const singleSegment = (fhdByte>>5)&1;
491         U32 const fcsID = fhdByte>>6;
492         U64 windowSize = 0;
493         U32 dictID = 0;
494         U64 frameContentSize = ZSTD_CONTENTSIZE_UNKNOWN;
495         RETURN_ERROR_IF((fhdByte & 0x08) != 0, frameParameter_unsupported,
496                         "reserved bits, must be zero");
497 
498         if (!singleSegment) {
499             BYTE const wlByte = ip[pos++];
500             U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN;
501             RETURN_ERROR_IF(windowLog > ZSTD_WINDOWLOG_MAX, frameParameter_windowTooLarge, "");
502             windowSize = (1ULL << windowLog);
503             windowSize += (windowSize >> 3) * (wlByte&7);
504         }
505         switch(dictIDSizeCode)
506         {
507             default:
508                 assert(0);  /* impossible */
509                 ZSTD_FALLTHROUGH;
510             case 0 : break;
511             case 1 : dictID = ip[pos]; pos++; break;
512             case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break;
513             case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break;
514         }
515         switch(fcsID)
516         {
517             default:
518                 assert(0);  /* impossible */
519                 ZSTD_FALLTHROUGH;
520             case 0 : if (singleSegment) frameContentSize = ip[pos]; break;
521             case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break;
522             case 2 : frameContentSize = MEM_readLE32(ip+pos); break;
523             case 3 : frameContentSize = MEM_readLE64(ip+pos); break;
524         }
525         if (singleSegment) windowSize = frameContentSize;
526 
527         zfhPtr->frameType = ZSTD_frame;
528         zfhPtr->frameContentSize = frameContentSize;
529         zfhPtr->windowSize = windowSize;
530         zfhPtr->blockSizeMax = (unsigned) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
531         zfhPtr->dictID = dictID;
532         zfhPtr->checksumFlag = checksumFlag;
533     }
534     return 0;
535 }
536 
537 /* ZSTD_getFrameHeader() :
538  *  decode Frame Header, or require larger `srcSize`.
539  *  note : this function does not consume input, it only reads it.
540  * @return : 0, `zfhPtr` is correctly filled,
541  *          >0, `srcSize` is too small, value is wanted `srcSize` amount,
542  *           or an error code, which can be tested using ZSTD_isError() */
ZSTD_getFrameHeader(ZSTD_FrameHeader * zfhPtr,const void * src,size_t srcSize)543 size_t ZSTD_getFrameHeader(ZSTD_FrameHeader* zfhPtr, const void* src, size_t srcSize)
544 {
545     return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_f_zstd1);
546 }
547 
548 /* ZSTD_getFrameContentSize() :
549  *  compatible with legacy mode
550  * @return : decompressed size of the single frame pointed to be `src` if known, otherwise
551  *         - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
552  *         - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */
ZSTD_getFrameContentSize(const void * src,size_t srcSize)553 unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize)
554 {
555     {   ZSTD_FrameHeader zfh;
556         if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0)
557             return ZSTD_CONTENTSIZE_ERROR;
558         if (zfh.frameType == ZSTD_skippableFrame) {
559             return 0;
560         } else {
561             return zfh.frameContentSize;
562     }   }
563 }
564 
readSkippableFrameSize(void const * src,size_t srcSize)565 static size_t readSkippableFrameSize(void const* src, size_t srcSize)
566 {
567     size_t const skippableHeaderSize = ZSTD_SKIPPABLEHEADERSIZE;
568     U32 sizeU32;
569 
570     RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong, "");
571 
572     sizeU32 = MEM_readLE32((BYTE const*)src + ZSTD_FRAMEIDSIZE);
573     RETURN_ERROR_IF((U32)(sizeU32 + ZSTD_SKIPPABLEHEADERSIZE) < sizeU32,
574                     frameParameter_unsupported, "");
575     {   size_t const skippableSize = skippableHeaderSize + sizeU32;
576         RETURN_ERROR_IF(skippableSize > srcSize, srcSize_wrong, "");
577         return skippableSize;
578     }
579 }
580 
581 /*! ZSTD_readSkippableFrame() :
582  * Retrieves content of a skippable frame, and writes it to dst buffer.
583  *
584  * The parameter magicVariant will receive the magicVariant that was supplied when the frame was written,
585  * i.e. magicNumber - ZSTD_MAGIC_SKIPPABLE_START.  This can be NULL if the caller is not interested
586  * in the magicVariant.
587  *
588  * Returns an error if destination buffer is not large enough, or if this is not a valid skippable frame.
589  *
590  * @return : number of bytes written or a ZSTD error.
591  */
ZSTD_readSkippableFrame(void * dst,size_t dstCapacity,unsigned * magicVariant,const void * src,size_t srcSize)592 size_t ZSTD_readSkippableFrame(void* dst, size_t dstCapacity,
593                                unsigned* magicVariant,  /* optional, can be NULL */
594                          const void* src, size_t srcSize)
595 {
596     RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong, "");
597 
598     {   U32 const magicNumber = MEM_readLE32(src);
599         size_t skippableFrameSize = readSkippableFrameSize(src, srcSize);
600         size_t skippableContentSize = skippableFrameSize - ZSTD_SKIPPABLEHEADERSIZE;
601 
602         /* check input validity */
603         RETURN_ERROR_IF(!ZSTD_isSkippableFrame(src, srcSize), frameParameter_unsupported, "");
604         RETURN_ERROR_IF(skippableFrameSize < ZSTD_SKIPPABLEHEADERSIZE || skippableFrameSize > srcSize, srcSize_wrong, "");
605         RETURN_ERROR_IF(skippableContentSize > dstCapacity, dstSize_tooSmall, "");
606 
607         /* deliver payload */
608         if (skippableContentSize > 0  && dst != NULL)
609             ZSTD_memcpy(dst, (const BYTE *)src + ZSTD_SKIPPABLEHEADERSIZE, skippableContentSize);
610         if (magicVariant != NULL)
611             *magicVariant = magicNumber - ZSTD_MAGIC_SKIPPABLE_START;
612         return skippableContentSize;
613     }
614 }
615 
616 /* ZSTD_findDecompressedSize() :
617  *  `srcSize` must be the exact length of some number of ZSTD compressed and/or
618  *      skippable frames
619  *  note: compatible with legacy mode
620  * @return : decompressed size of the frames contained */
ZSTD_findDecompressedSize(const void * src,size_t srcSize)621 unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize)
622 {
623     unsigned long long totalDstSize = 0;
624 
625     while (srcSize >= ZSTD_startingInputLength(ZSTD_f_zstd1)) {
626         U32 const magicNumber = MEM_readLE32(src);
627 
628         if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
629             size_t const skippableSize = readSkippableFrameSize(src, srcSize);
630             if (ZSTD_isError(skippableSize)) return ZSTD_CONTENTSIZE_ERROR;
631             assert(skippableSize <= srcSize);
632 
633             src = (const BYTE *)src + skippableSize;
634             srcSize -= skippableSize;
635             continue;
636         }
637 
638         {   unsigned long long const fcs = ZSTD_getFrameContentSize(src, srcSize);
639             if (fcs >= ZSTD_CONTENTSIZE_ERROR) return fcs;
640 
641             if (totalDstSize + fcs < totalDstSize)
642                 return ZSTD_CONTENTSIZE_ERROR; /* check for overflow */
643             totalDstSize += fcs;
644         }
645         /* skip to next frame */
646         {   size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize);
647             if (ZSTD_isError(frameSrcSize)) return ZSTD_CONTENTSIZE_ERROR;
648             assert(frameSrcSize <= srcSize);
649 
650             src = (const BYTE *)src + frameSrcSize;
651             srcSize -= frameSrcSize;
652         }
653     }  /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
654 
655     if (srcSize) return ZSTD_CONTENTSIZE_ERROR;
656 
657     return totalDstSize;
658 }
659 
660 /* ZSTD_getDecompressedSize() :
661  *  compatible with legacy mode
662  * @return : decompressed size if known, 0 otherwise
663              note : 0 can mean any of the following :
664                    - frame content is empty
665                    - decompressed size field is not present in frame header
666                    - frame header unknown / not supported
667                    - frame header not complete (`srcSize` too small) */
ZSTD_getDecompressedSize(const void * src,size_t srcSize)668 unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize)
669 {
670     unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
671     ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR < ZSTD_CONTENTSIZE_UNKNOWN);
672     return (ret >= ZSTD_CONTENTSIZE_ERROR) ? 0 : ret;
673 }
674 
675 
676 /* ZSTD_decodeFrameHeader() :
677  * `headerSize` must be the size provided by ZSTD_frameHeaderSize().
678  * If multiple DDict references are enabled, also will choose the correct DDict to use.
679  * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
ZSTD_decodeFrameHeader(ZSTD_DCtx * dctx,const void * src,size_t headerSize)680 static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize)
681 {
682     size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format);
683     if (ZSTD_isError(result)) return result;    /* invalid header */
684     RETURN_ERROR_IF(result>0, srcSize_wrong, "headerSize too small");
685 
686     /* Reference DDict requested by frame if dctx references multiple ddicts */
687     if (dctx->refMultipleDDicts == ZSTD_rmd_refMultipleDDicts && dctx->ddictSet) {
688         ZSTD_DCtx_selectFrameDDict(dctx);
689     }
690 
691 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
692     /* Skip the dictID check in fuzzing mode, because it makes the search
693      * harder.
694      */
695     RETURN_ERROR_IF(dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID),
696                     dictionary_wrong, "");
697 #endif
698     dctx->validateChecksum = (dctx->fParams.checksumFlag && !dctx->forceIgnoreChecksum) ? 1 : 0;
699     if (dctx->validateChecksum) xxh64_reset(&dctx->xxhState, 0);
700     dctx->processedCSize += headerSize;
701     return 0;
702 }
703 
ZSTD_errorFrameSizeInfo(size_t ret)704 static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret)
705 {
706     ZSTD_frameSizeInfo frameSizeInfo;
707     frameSizeInfo.compressedSize = ret;
708     frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
709     return frameSizeInfo;
710 }
711 
ZSTD_findFrameSizeInfo(const void * src,size_t srcSize,ZSTD_format_e format)712 static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize, ZSTD_format_e format)
713 {
714     ZSTD_frameSizeInfo frameSizeInfo;
715     ZSTD_memset(&frameSizeInfo, 0, sizeof(ZSTD_frameSizeInfo));
716 
717 
718     if (format == ZSTD_f_zstd1 && (srcSize >= ZSTD_SKIPPABLEHEADERSIZE)
719         && (MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
720         frameSizeInfo.compressedSize = readSkippableFrameSize(src, srcSize);
721         assert(ZSTD_isError(frameSizeInfo.compressedSize) ||
722                frameSizeInfo.compressedSize <= srcSize);
723         return frameSizeInfo;
724     } else {
725         const BYTE* ip = (const BYTE*)src;
726         const BYTE* const ipstart = ip;
727         size_t remainingSize = srcSize;
728         size_t nbBlocks = 0;
729         ZSTD_FrameHeader zfh;
730 
731         /* Extract Frame Header */
732         {   size_t const ret = ZSTD_getFrameHeader_advanced(&zfh, src, srcSize, format);
733             if (ZSTD_isError(ret))
734                 return ZSTD_errorFrameSizeInfo(ret);
735             if (ret > 0)
736                 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
737         }
738 
739         ip += zfh.headerSize;
740         remainingSize -= zfh.headerSize;
741 
742         /* Iterate over each block */
743         while (1) {
744             blockProperties_t blockProperties;
745             size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
746             if (ZSTD_isError(cBlockSize))
747                 return ZSTD_errorFrameSizeInfo(cBlockSize);
748 
749             if (ZSTD_blockHeaderSize + cBlockSize > remainingSize)
750                 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
751 
752             ip += ZSTD_blockHeaderSize + cBlockSize;
753             remainingSize -= ZSTD_blockHeaderSize + cBlockSize;
754             nbBlocks++;
755 
756             if (blockProperties.lastBlock) break;
757         }
758 
759         /* Final frame content checksum */
760         if (zfh.checksumFlag) {
761             if (remainingSize < 4)
762                 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
763             ip += 4;
764         }
765 
766         frameSizeInfo.nbBlocks = nbBlocks;
767         frameSizeInfo.compressedSize = (size_t)(ip - ipstart);
768         frameSizeInfo.decompressedBound = (zfh.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN)
769                                         ? zfh.frameContentSize
770                                         : (unsigned long long)nbBlocks * zfh.blockSizeMax;
771         return frameSizeInfo;
772     }
773 }
774 
ZSTD_findFrameCompressedSize_advanced(const void * src,size_t srcSize,ZSTD_format_e format)775 static size_t ZSTD_findFrameCompressedSize_advanced(const void *src, size_t srcSize, ZSTD_format_e format) {
776     ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, format);
777     return frameSizeInfo.compressedSize;
778 }
779 
780 /* ZSTD_findFrameCompressedSize() :
781  * See docs in zstd.h
782  * Note: compatible with legacy mode */
ZSTD_findFrameCompressedSize(const void * src,size_t srcSize)783 size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize)
784 {
785     return ZSTD_findFrameCompressedSize_advanced(src, srcSize, ZSTD_f_zstd1);
786 }
787 
788 /* ZSTD_decompressBound() :
789  *  compatible with legacy mode
790  *  `src` must point to the start of a ZSTD frame or a skippable frame
791  *  `srcSize` must be at least as large as the frame contained
792  *  @return : the maximum decompressed size of the compressed source
793  */
ZSTD_decompressBound(const void * src,size_t srcSize)794 unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize)
795 {
796     unsigned long long bound = 0;
797     /* Iterate over each frame */
798     while (srcSize > 0) {
799         ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, ZSTD_f_zstd1);
800         size_t const compressedSize = frameSizeInfo.compressedSize;
801         unsigned long long const decompressedBound = frameSizeInfo.decompressedBound;
802         if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR)
803             return ZSTD_CONTENTSIZE_ERROR;
804         assert(srcSize >= compressedSize);
805         src = (const BYTE*)src + compressedSize;
806         srcSize -= compressedSize;
807         bound += decompressedBound;
808     }
809     return bound;
810 }
811 
ZSTD_decompressionMargin(void const * src,size_t srcSize)812 size_t ZSTD_decompressionMargin(void const* src, size_t srcSize)
813 {
814     size_t margin = 0;
815     unsigned maxBlockSize = 0;
816 
817     /* Iterate over each frame */
818     while (srcSize > 0) {
819         ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, ZSTD_f_zstd1);
820         size_t const compressedSize = frameSizeInfo.compressedSize;
821         unsigned long long const decompressedBound = frameSizeInfo.decompressedBound;
822         ZSTD_FrameHeader zfh;
823 
824         FORWARD_IF_ERROR(ZSTD_getFrameHeader(&zfh, src, srcSize), "");
825         if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR)
826             return ERROR(corruption_detected);
827 
828         if (zfh.frameType == ZSTD_frame) {
829             /* Add the frame header to our margin */
830             margin += zfh.headerSize;
831             /* Add the checksum to our margin */
832             margin += zfh.checksumFlag ? 4 : 0;
833             /* Add 3 bytes per block */
834             margin += 3 * frameSizeInfo.nbBlocks;
835 
836             /* Compute the max block size */
837             maxBlockSize = MAX(maxBlockSize, zfh.blockSizeMax);
838         } else {
839             assert(zfh.frameType == ZSTD_skippableFrame);
840             /* Add the entire skippable frame size to our margin. */
841             margin += compressedSize;
842         }
843 
844         assert(srcSize >= compressedSize);
845         src = (const BYTE*)src + compressedSize;
846         srcSize -= compressedSize;
847     }
848 
849     /* Add the max block size back to the margin. */
850     margin += maxBlockSize;
851 
852     return margin;
853 }
854 
855 /*-*************************************************************
856  *   Frame decoding
857  ***************************************************************/
858 
859 /* ZSTD_insertBlock() :
860  *  insert `src` block into `dctx` history. Useful to track uncompressed blocks. */
ZSTD_insertBlock(ZSTD_DCtx * dctx,const void * blockStart,size_t blockSize)861 size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize)
862 {
863     DEBUGLOG(5, "ZSTD_insertBlock: %u bytes", (unsigned)blockSize);
864     ZSTD_checkContinuity(dctx, blockStart, blockSize);
865     dctx->previousDstEnd = (const char*)blockStart + blockSize;
866     return blockSize;
867 }
868 
869 
ZSTD_copyRawBlock(void * dst,size_t dstCapacity,const void * src,size_t srcSize)870 static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity,
871                           const void* src, size_t srcSize)
872 {
873     DEBUGLOG(5, "ZSTD_copyRawBlock");
874     RETURN_ERROR_IF(srcSize > dstCapacity, dstSize_tooSmall, "");
875     if (dst == NULL) {
876         if (srcSize == 0) return 0;
877         RETURN_ERROR(dstBuffer_null, "");
878     }
879     ZSTD_memmove(dst, src, srcSize);
880     return srcSize;
881 }
882 
ZSTD_setRleBlock(void * dst,size_t dstCapacity,BYTE b,size_t regenSize)883 static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity,
884                                BYTE b,
885                                size_t regenSize)
886 {
887     RETURN_ERROR_IF(regenSize > dstCapacity, dstSize_tooSmall, "");
888     if (dst == NULL) {
889         if (regenSize == 0) return 0;
890         RETURN_ERROR(dstBuffer_null, "");
891     }
892     ZSTD_memset(dst, b, regenSize);
893     return regenSize;
894 }
895 
ZSTD_DCtx_trace_end(ZSTD_DCtx const * dctx,U64 uncompressedSize,U64 compressedSize,int streaming)896 static void ZSTD_DCtx_trace_end(ZSTD_DCtx const* dctx, U64 uncompressedSize, U64 compressedSize, int streaming)
897 {
898     (void)dctx;
899     (void)uncompressedSize;
900     (void)compressedSize;
901     (void)streaming;
902 }
903 
904 
905 /*! ZSTD_decompressFrame() :
906  * @dctx must be properly initialized
907  *  will update *srcPtr and *srcSizePtr,
908  *  to make *srcPtr progress by one frame. */
ZSTD_decompressFrame(ZSTD_DCtx * dctx,void * dst,size_t dstCapacity,const void ** srcPtr,size_t * srcSizePtr)909 static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
910                                    void* dst, size_t dstCapacity,
911                              const void** srcPtr, size_t *srcSizePtr)
912 {
913     const BYTE* const istart = (const BYTE*)(*srcPtr);
914     const BYTE* ip = istart;
915     BYTE* const ostart = (BYTE*)dst;
916     BYTE* const oend = dstCapacity != 0 ? ostart + dstCapacity : ostart;
917     BYTE* op = ostart;
918     size_t remainingSrcSize = *srcSizePtr;
919 
920     DEBUGLOG(4, "ZSTD_decompressFrame (srcSize:%i)", (int)*srcSizePtr);
921 
922     /* check */
923     RETURN_ERROR_IF(
924         remainingSrcSize < ZSTD_FRAMEHEADERSIZE_MIN(dctx->format)+ZSTD_blockHeaderSize,
925         srcSize_wrong, "");
926 
927     /* Frame Header */
928     {   size_t const frameHeaderSize = ZSTD_frameHeaderSize_internal(
929                 ip, ZSTD_FRAMEHEADERSIZE_PREFIX(dctx->format), dctx->format);
930         if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize;
931         RETURN_ERROR_IF(remainingSrcSize < frameHeaderSize+ZSTD_blockHeaderSize,
932                         srcSize_wrong, "");
933         FORWARD_IF_ERROR( ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize) , "");
934         ip += frameHeaderSize; remainingSrcSize -= frameHeaderSize;
935     }
936 
937     /* Shrink the blockSizeMax if enabled */
938     if (dctx->maxBlockSizeParam != 0)
939         dctx->fParams.blockSizeMax = MIN(dctx->fParams.blockSizeMax, (unsigned)dctx->maxBlockSizeParam);
940 
941     /* Loop on each block */
942     while (1) {
943         BYTE* oBlockEnd = oend;
944         size_t decodedSize;
945         blockProperties_t blockProperties;
946         size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSrcSize, &blockProperties);
947         if (ZSTD_isError(cBlockSize)) return cBlockSize;
948 
949         ip += ZSTD_blockHeaderSize;
950         remainingSrcSize -= ZSTD_blockHeaderSize;
951         RETURN_ERROR_IF(cBlockSize > remainingSrcSize, srcSize_wrong, "");
952 
953         if (ip >= op && ip < oBlockEnd) {
954             /* We are decompressing in-place. Limit the output pointer so that we
955              * don't overwrite the block that we are currently reading. This will
956              * fail decompression if the input & output pointers aren't spaced
957              * far enough apart.
958              *
959              * This is important to set, even when the pointers are far enough
960              * apart, because ZSTD_decompressBlock_internal() can decide to store
961              * literals in the output buffer, after the block it is decompressing.
962              * Since we don't want anything to overwrite our input, we have to tell
963              * ZSTD_decompressBlock_internal to never write past ip.
964              *
965              * See ZSTD_allocateLiteralsBuffer() for reference.
966              */
967             oBlockEnd = op + (ip - op);
968         }
969 
970         switch(blockProperties.blockType)
971         {
972         case bt_compressed:
973             assert(dctx->isFrameDecompression == 1);
974             decodedSize = ZSTD_decompressBlock_internal(dctx, op, (size_t)(oBlockEnd-op), ip, cBlockSize, not_streaming);
975             break;
976         case bt_raw :
977             /* Use oend instead of oBlockEnd because this function is safe to overlap. It uses memmove. */
978             decodedSize = ZSTD_copyRawBlock(op, (size_t)(oend-op), ip, cBlockSize);
979             break;
980         case bt_rle :
981             decodedSize = ZSTD_setRleBlock(op, (size_t)(oBlockEnd-op), *ip, blockProperties.origSize);
982             break;
983         case bt_reserved :
984         default:
985             RETURN_ERROR(corruption_detected, "invalid block type");
986         }
987         FORWARD_IF_ERROR(decodedSize, "Block decompression failure");
988         DEBUGLOG(5, "Decompressed block of dSize = %u", (unsigned)decodedSize);
989         if (dctx->validateChecksum) {
990             xxh64_update(&dctx->xxhState, op, decodedSize);
991         }
992         if (decodedSize) /* support dst = NULL,0 */ {
993             op += decodedSize;
994         }
995         assert(ip != NULL);
996         ip += cBlockSize;
997         remainingSrcSize -= cBlockSize;
998         if (blockProperties.lastBlock) break;
999     }
1000 
1001     if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) {
1002         RETURN_ERROR_IF((U64)(op-ostart) != dctx->fParams.frameContentSize,
1003                         corruption_detected, "");
1004     }
1005     if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */
1006         RETURN_ERROR_IF(remainingSrcSize<4, checksum_wrong, "");
1007         if (!dctx->forceIgnoreChecksum) {
1008             U32 const checkCalc = (U32)xxh64_digest(&dctx->xxhState);
1009             U32 checkRead;
1010             checkRead = MEM_readLE32(ip);
1011             RETURN_ERROR_IF(checkRead != checkCalc, checksum_wrong, "");
1012         }
1013         ip += 4;
1014         remainingSrcSize -= 4;
1015     }
1016     ZSTD_DCtx_trace_end(dctx, (U64)(op-ostart), (U64)(ip-istart), /* streaming */ 0);
1017     /* Allow caller to get size read */
1018     DEBUGLOG(4, "ZSTD_decompressFrame: decompressed frame of size %i, consuming %i bytes of input", (int)(op-ostart), (int)(ip - (const BYTE*)*srcPtr));
1019     *srcPtr = ip;
1020     *srcSizePtr = remainingSrcSize;
1021     return (size_t)(op-ostart);
1022 }
1023 
1024 static
1025 ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_decompressMultiFrame(ZSTD_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize,const void * dict,size_t dictSize,const ZSTD_DDict * ddict)1026 size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
1027                                         void* dst, size_t dstCapacity,
1028                                   const void* src, size_t srcSize,
1029                                   const void* dict, size_t dictSize,
1030                                   const ZSTD_DDict* ddict)
1031 {
1032     void* const dststart = dst;
1033     int moreThan1Frame = 0;
1034 
1035     DEBUGLOG(5, "ZSTD_decompressMultiFrame");
1036     assert(dict==NULL || ddict==NULL);  /* either dict or ddict set, not both */
1037 
1038     if (ddict) {
1039         dict = ZSTD_DDict_dictContent(ddict);
1040         dictSize = ZSTD_DDict_dictSize(ddict);
1041     }
1042 
1043     while (srcSize >= ZSTD_startingInputLength(dctx->format)) {
1044 
1045 
1046         if (dctx->format == ZSTD_f_zstd1 && srcSize >= 4) {
1047             U32 const magicNumber = MEM_readLE32(src);
1048             DEBUGLOG(5, "reading magic number %08X", (unsigned)magicNumber);
1049             if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
1050                 /* skippable frame detected : skip it */
1051                 size_t const skippableSize = readSkippableFrameSize(src, srcSize);
1052                 FORWARD_IF_ERROR(skippableSize, "invalid skippable frame");
1053                 assert(skippableSize <= srcSize);
1054 
1055                 src = (const BYTE *)src + skippableSize;
1056                 srcSize -= skippableSize;
1057                 continue; /* check next frame */
1058         }   }
1059 
1060         if (ddict) {
1061             /* we were called from ZSTD_decompress_usingDDict */
1062             FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(dctx, ddict), "");
1063         } else {
1064             /* this will initialize correctly with no dict if dict == NULL, so
1065              * use this in all cases but ddict */
1066             FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize), "");
1067         }
1068         ZSTD_checkContinuity(dctx, dst, dstCapacity);
1069 
1070         {   const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity,
1071                                                     &src, &srcSize);
1072             RETURN_ERROR_IF(
1073                 (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown)
1074              && (moreThan1Frame==1),
1075                 srcSize_wrong,
1076                 "At least one frame successfully completed, "
1077                 "but following bytes are garbage: "
1078                 "it's more likely to be a srcSize error, "
1079                 "specifying more input bytes than size of frame(s). "
1080                 "Note: one could be unlucky, it might be a corruption error instead, "
1081                 "happening right at the place where we expect zstd magic bytes. "
1082                 "But this is _much_ less likely than a srcSize field error.");
1083             if (ZSTD_isError(res)) return res;
1084             assert(res <= dstCapacity);
1085             if (res != 0)
1086                 dst = (BYTE*)dst + res;
1087             dstCapacity -= res;
1088         }
1089         moreThan1Frame = 1;
1090     }  /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
1091 
1092     RETURN_ERROR_IF(srcSize, srcSize_wrong, "input not entirely consumed");
1093 
1094     return (size_t)((BYTE*)dst - (BYTE*)dststart);
1095 }
1096 
ZSTD_decompress_usingDict(ZSTD_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize,const void * dict,size_t dictSize)1097 size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
1098                                  void* dst, size_t dstCapacity,
1099                            const void* src, size_t srcSize,
1100                            const void* dict, size_t dictSize)
1101 {
1102     return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL);
1103 }
1104 
1105 
ZSTD_getDDict(ZSTD_DCtx * dctx)1106 static ZSTD_DDict const* ZSTD_getDDict(ZSTD_DCtx* dctx)
1107 {
1108     switch (dctx->dictUses) {
1109     default:
1110         assert(0 /* Impossible */);
1111         ZSTD_FALLTHROUGH;
1112     case ZSTD_dont_use:
1113         ZSTD_clearDict(dctx);
1114         return NULL;
1115     case ZSTD_use_indefinitely:
1116         return dctx->ddict;
1117     case ZSTD_use_once:
1118         dctx->dictUses = ZSTD_dont_use;
1119         return dctx->ddict;
1120     }
1121 }
1122 
ZSTD_decompressDCtx(ZSTD_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)1123 size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
1124 {
1125     return ZSTD_decompress_usingDDict(dctx, dst, dstCapacity, src, srcSize, ZSTD_getDDict(dctx));
1126 }
1127 
1128 
ZSTD_decompress(void * dst,size_t dstCapacity,const void * src,size_t srcSize)1129 size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
1130 {
1131 #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1)
1132     size_t regenSize;
1133     ZSTD_DCtx* const dctx =  ZSTD_createDCtx_internal(ZSTD_defaultCMem);
1134     RETURN_ERROR_IF(dctx==NULL, memory_allocation, "NULL pointer!");
1135     regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize);
1136     ZSTD_freeDCtx(dctx);
1137     return regenSize;
1138 #else   /* stack mode */
1139     ZSTD_DCtx dctx;
1140     ZSTD_initDCtx_internal(&dctx);
1141     return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);
1142 #endif
1143 }
1144 
1145 
1146 /*-**************************************
1147 *   Advanced Streaming Decompression API
1148 *   Bufferless and synchronous
1149 ****************************************/
ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx * dctx)1150 size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; }
1151 
1152 /*
1153  * Similar to ZSTD_nextSrcSizeToDecompress(), but when a block input can be streamed, we
1154  * allow taking a partial block as the input. Currently only raw uncompressed blocks can
1155  * be streamed.
1156  *
1157  * For blocks that can be streamed, this allows us to reduce the latency until we produce
1158  * output, and avoid copying the input.
1159  *
1160  * @param inputSize - The total amount of input that the caller currently has.
1161  */
ZSTD_nextSrcSizeToDecompressWithInputSize(ZSTD_DCtx * dctx,size_t inputSize)1162 static size_t ZSTD_nextSrcSizeToDecompressWithInputSize(ZSTD_DCtx* dctx, size_t inputSize) {
1163     if (!(dctx->stage == ZSTDds_decompressBlock || dctx->stage == ZSTDds_decompressLastBlock))
1164         return dctx->expected;
1165     if (dctx->bType != bt_raw)
1166         return dctx->expected;
1167     return BOUNDED(1, inputSize, dctx->expected);
1168 }
1169 
ZSTD_nextInputType(ZSTD_DCtx * dctx)1170 ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) {
1171     switch(dctx->stage)
1172     {
1173     default:   /* should not happen */
1174         assert(0);
1175         ZSTD_FALLTHROUGH;
1176     case ZSTDds_getFrameHeaderSize:
1177         ZSTD_FALLTHROUGH;
1178     case ZSTDds_decodeFrameHeader:
1179         return ZSTDnit_frameHeader;
1180     case ZSTDds_decodeBlockHeader:
1181         return ZSTDnit_blockHeader;
1182     case ZSTDds_decompressBlock:
1183         return ZSTDnit_block;
1184     case ZSTDds_decompressLastBlock:
1185         return ZSTDnit_lastBlock;
1186     case ZSTDds_checkChecksum:
1187         return ZSTDnit_checksum;
1188     case ZSTDds_decodeSkippableHeader:
1189         ZSTD_FALLTHROUGH;
1190     case ZSTDds_skipFrame:
1191         return ZSTDnit_skippableFrame;
1192     }
1193 }
1194 
ZSTD_isSkipFrame(ZSTD_DCtx * dctx)1195 static int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; }
1196 
1197 /* ZSTD_decompressContinue() :
1198  *  srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress())
1199  *  @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)
1200  *            or an error code, which can be tested using ZSTD_isError() */
ZSTD_decompressContinue(ZSTD_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)1201 size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
1202 {
1203     DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (unsigned)srcSize);
1204     /* Sanity check */
1205     RETURN_ERROR_IF(srcSize != ZSTD_nextSrcSizeToDecompressWithInputSize(dctx, srcSize), srcSize_wrong, "not allowed");
1206     ZSTD_checkContinuity(dctx, dst, dstCapacity);
1207 
1208     dctx->processedCSize += srcSize;
1209 
1210     switch (dctx->stage)
1211     {
1212     case ZSTDds_getFrameHeaderSize :
1213         assert(src != NULL);
1214         if (dctx->format == ZSTD_f_zstd1) {  /* allows header */
1215             assert(srcSize >= ZSTD_FRAMEIDSIZE);  /* to read skippable magic number */
1216             if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {        /* skippable frame */
1217                 ZSTD_memcpy(dctx->headerBuffer, src, srcSize);
1218                 dctx->expected = ZSTD_SKIPPABLEHEADERSIZE - srcSize;  /* remaining to load to get full skippable frame header */
1219                 dctx->stage = ZSTDds_decodeSkippableHeader;
1220                 return 0;
1221         }   }
1222         dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format);
1223         if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize;
1224         ZSTD_memcpy(dctx->headerBuffer, src, srcSize);
1225         dctx->expected = dctx->headerSize - srcSize;
1226         dctx->stage = ZSTDds_decodeFrameHeader;
1227         return 0;
1228 
1229     case ZSTDds_decodeFrameHeader:
1230         assert(src != NULL);
1231         ZSTD_memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize);
1232         FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize), "");
1233         dctx->expected = ZSTD_blockHeaderSize;
1234         dctx->stage = ZSTDds_decodeBlockHeader;
1235         return 0;
1236 
1237     case ZSTDds_decodeBlockHeader:
1238         {   blockProperties_t bp;
1239             size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
1240             if (ZSTD_isError(cBlockSize)) return cBlockSize;
1241             RETURN_ERROR_IF(cBlockSize > dctx->fParams.blockSizeMax, corruption_detected, "Block Size Exceeds Maximum");
1242             dctx->expected = cBlockSize;
1243             dctx->bType = bp.blockType;
1244             dctx->rleSize = bp.origSize;
1245             if (cBlockSize) {
1246                 dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock;
1247                 return 0;
1248             }
1249             /* empty block */
1250             if (bp.lastBlock) {
1251                 if (dctx->fParams.checksumFlag) {
1252                     dctx->expected = 4;
1253                     dctx->stage = ZSTDds_checkChecksum;
1254                 } else {
1255                     dctx->expected = 0; /* end of frame */
1256                     dctx->stage = ZSTDds_getFrameHeaderSize;
1257                 }
1258             } else {
1259                 dctx->expected = ZSTD_blockHeaderSize;  /* jump to next header */
1260                 dctx->stage = ZSTDds_decodeBlockHeader;
1261             }
1262             return 0;
1263         }
1264 
1265     case ZSTDds_decompressLastBlock:
1266     case ZSTDds_decompressBlock:
1267         DEBUGLOG(5, "ZSTD_decompressContinue: case ZSTDds_decompressBlock");
1268         {   size_t rSize;
1269             switch(dctx->bType)
1270             {
1271             case bt_compressed:
1272                 DEBUGLOG(5, "ZSTD_decompressContinue: case bt_compressed");
1273                 assert(dctx->isFrameDecompression == 1);
1274                 rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, is_streaming);
1275                 dctx->expected = 0;  /* Streaming not supported */
1276                 break;
1277             case bt_raw :
1278                 assert(srcSize <= dctx->expected);
1279                 rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize);
1280                 FORWARD_IF_ERROR(rSize, "ZSTD_copyRawBlock failed");
1281                 assert(rSize == srcSize);
1282                 dctx->expected -= rSize;
1283                 break;
1284             case bt_rle :
1285                 rSize = ZSTD_setRleBlock(dst, dstCapacity, *(const BYTE*)src, dctx->rleSize);
1286                 dctx->expected = 0;  /* Streaming not supported */
1287                 break;
1288             case bt_reserved :   /* should never happen */
1289             default:
1290                 RETURN_ERROR(corruption_detected, "invalid block type");
1291             }
1292             FORWARD_IF_ERROR(rSize, "");
1293             RETURN_ERROR_IF(rSize > dctx->fParams.blockSizeMax, corruption_detected, "Decompressed Block Size Exceeds Maximum");
1294             DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (unsigned)rSize);
1295             dctx->decodedSize += rSize;
1296             if (dctx->validateChecksum) xxh64_update(&dctx->xxhState, dst, rSize);
1297             dctx->previousDstEnd = (char*)dst + rSize;
1298 
1299             /* Stay on the same stage until we are finished streaming the block. */
1300             if (dctx->expected > 0) {
1301                 return rSize;
1302             }
1303 
1304             if (dctx->stage == ZSTDds_decompressLastBlock) {   /* end of frame */
1305                 DEBUGLOG(4, "ZSTD_decompressContinue: decoded size from frame : %u", (unsigned)dctx->decodedSize);
1306                 RETURN_ERROR_IF(
1307                     dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
1308                  && dctx->decodedSize != dctx->fParams.frameContentSize,
1309                     corruption_detected, "");
1310                 if (dctx->fParams.checksumFlag) {  /* another round for frame checksum */
1311                     dctx->expected = 4;
1312                     dctx->stage = ZSTDds_checkChecksum;
1313                 } else {
1314                     ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, /* streaming */ 1);
1315                     dctx->expected = 0;   /* ends here */
1316                     dctx->stage = ZSTDds_getFrameHeaderSize;
1317                 }
1318             } else {
1319                 dctx->stage = ZSTDds_decodeBlockHeader;
1320                 dctx->expected = ZSTD_blockHeaderSize;
1321             }
1322             return rSize;
1323         }
1324 
1325     case ZSTDds_checkChecksum:
1326         assert(srcSize == 4);  /* guaranteed by dctx->expected */
1327         {
1328             if (dctx->validateChecksum) {
1329                 U32 const h32 = (U32)xxh64_digest(&dctx->xxhState);
1330                 U32 const check32 = MEM_readLE32(src);
1331                 DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", (unsigned)h32, (unsigned)check32);
1332                 RETURN_ERROR_IF(check32 != h32, checksum_wrong, "");
1333             }
1334             ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, /* streaming */ 1);
1335             dctx->expected = 0;
1336             dctx->stage = ZSTDds_getFrameHeaderSize;
1337             return 0;
1338         }
1339 
1340     case ZSTDds_decodeSkippableHeader:
1341         assert(src != NULL);
1342         assert(srcSize <= ZSTD_SKIPPABLEHEADERSIZE);
1343         assert(dctx->format != ZSTD_f_zstd1_magicless);
1344         ZSTD_memcpy(dctx->headerBuffer + (ZSTD_SKIPPABLEHEADERSIZE - srcSize), src, srcSize);   /* complete skippable header */
1345         dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_FRAMEIDSIZE);   /* note : dctx->expected can grow seriously large, beyond local buffer size */
1346         dctx->stage = ZSTDds_skipFrame;
1347         return 0;
1348 
1349     case ZSTDds_skipFrame:
1350         dctx->expected = 0;
1351         dctx->stage = ZSTDds_getFrameHeaderSize;
1352         return 0;
1353 
1354     default:
1355         assert(0);   /* impossible */
1356         RETURN_ERROR(GENERIC, "impossible to reach");   /* some compilers require default to do something */
1357     }
1358 }
1359 
1360 
ZSTD_refDictContent(ZSTD_DCtx * dctx,const void * dict,size_t dictSize)1361 static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1362 {
1363     dctx->dictEnd = dctx->previousDstEnd;
1364     dctx->virtualStart = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
1365     dctx->prefixStart = dict;
1366     dctx->previousDstEnd = (const char*)dict + dictSize;
1367 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1368     dctx->dictContentBeginForFuzzing = dctx->prefixStart;
1369     dctx->dictContentEndForFuzzing = dctx->previousDstEnd;
1370 #endif
1371     return 0;
1372 }
1373 
1374 /*! ZSTD_loadDEntropy() :
1375  *  dict : must point at beginning of a valid zstd dictionary.
1376  * @return : size of entropy tables read */
1377 size_t
ZSTD_loadDEntropy(ZSTD_entropyDTables_t * entropy,const void * const dict,size_t const dictSize)1378 ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
1379                   const void* const dict, size_t const dictSize)
1380 {
1381     const BYTE* dictPtr = (const BYTE*)dict;
1382     const BYTE* const dictEnd = dictPtr + dictSize;
1383 
1384     RETURN_ERROR_IF(dictSize <= 8, dictionary_corrupted, "dict is too small");
1385     assert(MEM_readLE32(dict) == ZSTD_MAGIC_DICTIONARY);   /* dict must be valid */
1386     dictPtr += 8;   /* skip header = magic + dictID */
1387 
1388     ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, OFTable) == offsetof(ZSTD_entropyDTables_t, LLTable) + sizeof(entropy->LLTable));
1389     ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, MLTable) == offsetof(ZSTD_entropyDTables_t, OFTable) + sizeof(entropy->OFTable));
1390     ZSTD_STATIC_ASSERT(sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable) >= HUF_DECOMPRESS_WORKSPACE_SIZE);
1391     {   void* const workspace = &entropy->LLTable;   /* use fse tables as temporary workspace; implies fse tables are grouped together */
1392         size_t const workspaceSize = sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable);
1393 #ifdef HUF_FORCE_DECOMPRESS_X1
1394         /* in minimal huffman, we always use X1 variants */
1395         size_t const hSize = HUF_readDTableX1_wksp(entropy->hufTable,
1396                                                 dictPtr, dictEnd - dictPtr,
1397                                                 workspace, workspaceSize, /* flags */ 0);
1398 #else
1399         size_t const hSize = HUF_readDTableX2_wksp(entropy->hufTable,
1400                                                 dictPtr, (size_t)(dictEnd - dictPtr),
1401                                                 workspace, workspaceSize, /* flags */ 0);
1402 #endif
1403         RETURN_ERROR_IF(HUF_isError(hSize), dictionary_corrupted, "");
1404         dictPtr += hSize;
1405     }
1406 
1407     {   short offcodeNCount[MaxOff+1];
1408         unsigned offcodeMaxValue = MaxOff, offcodeLog;
1409         size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, (size_t)(dictEnd-dictPtr));
1410         RETURN_ERROR_IF(FSE_isError(offcodeHeaderSize), dictionary_corrupted, "");
1411         RETURN_ERROR_IF(offcodeMaxValue > MaxOff, dictionary_corrupted, "");
1412         RETURN_ERROR_IF(offcodeLog > OffFSELog, dictionary_corrupted, "");
1413         ZSTD_buildFSETable( entropy->OFTable,
1414                             offcodeNCount, offcodeMaxValue,
1415                             OF_base, OF_bits,
1416                             offcodeLog,
1417                             entropy->workspace, sizeof(entropy->workspace),
1418                             /* bmi2 */0);
1419         dictPtr += offcodeHeaderSize;
1420     }
1421 
1422     {   short matchlengthNCount[MaxML+1];
1423         unsigned matchlengthMaxValue = MaxML, matchlengthLog;
1424         size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, (size_t)(dictEnd-dictPtr));
1425         RETURN_ERROR_IF(FSE_isError(matchlengthHeaderSize), dictionary_corrupted, "");
1426         RETURN_ERROR_IF(matchlengthMaxValue > MaxML, dictionary_corrupted, "");
1427         RETURN_ERROR_IF(matchlengthLog > MLFSELog, dictionary_corrupted, "");
1428         ZSTD_buildFSETable( entropy->MLTable,
1429                             matchlengthNCount, matchlengthMaxValue,
1430                             ML_base, ML_bits,
1431                             matchlengthLog,
1432                             entropy->workspace, sizeof(entropy->workspace),
1433                             /* bmi2 */ 0);
1434         dictPtr += matchlengthHeaderSize;
1435     }
1436 
1437     {   short litlengthNCount[MaxLL+1];
1438         unsigned litlengthMaxValue = MaxLL, litlengthLog;
1439         size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, (size_t)(dictEnd-dictPtr));
1440         RETURN_ERROR_IF(FSE_isError(litlengthHeaderSize), dictionary_corrupted, "");
1441         RETURN_ERROR_IF(litlengthMaxValue > MaxLL, dictionary_corrupted, "");
1442         RETURN_ERROR_IF(litlengthLog > LLFSELog, dictionary_corrupted, "");
1443         ZSTD_buildFSETable( entropy->LLTable,
1444                             litlengthNCount, litlengthMaxValue,
1445                             LL_base, LL_bits,
1446                             litlengthLog,
1447                             entropy->workspace, sizeof(entropy->workspace),
1448                             /* bmi2 */ 0);
1449         dictPtr += litlengthHeaderSize;
1450     }
1451 
1452     RETURN_ERROR_IF(dictPtr+12 > dictEnd, dictionary_corrupted, "");
1453     {   int i;
1454         size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12));
1455         for (i=0; i<3; i++) {
1456             U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4;
1457             RETURN_ERROR_IF(rep==0 || rep > dictContentSize,
1458                             dictionary_corrupted, "");
1459             entropy->rep[i] = rep;
1460     }   }
1461 
1462     return (size_t)(dictPtr - (const BYTE*)dict);
1463 }
1464 
ZSTD_decompress_insertDictionary(ZSTD_DCtx * dctx,const void * dict,size_t dictSize)1465 static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1466 {
1467     if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize);
1468     {   U32 const magic = MEM_readLE32(dict);
1469         if (magic != ZSTD_MAGIC_DICTIONARY) {
1470             return ZSTD_refDictContent(dctx, dict, dictSize);   /* pure content mode */
1471     }   }
1472     dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
1473 
1474     /* load entropy tables */
1475     {   size_t const eSize = ZSTD_loadDEntropy(&dctx->entropy, dict, dictSize);
1476         RETURN_ERROR_IF(ZSTD_isError(eSize), dictionary_corrupted, "");
1477         dict = (const char*)dict + eSize;
1478         dictSize -= eSize;
1479     }
1480     dctx->litEntropy = dctx->fseEntropy = 1;
1481 
1482     /* reference dictionary content */
1483     return ZSTD_refDictContent(dctx, dict, dictSize);
1484 }
1485 
ZSTD_decompressBegin(ZSTD_DCtx * dctx)1486 size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
1487 {
1488     assert(dctx != NULL);
1489     dctx->expected = ZSTD_startingInputLength(dctx->format);  /* dctx->format must be properly set */
1490     dctx->stage = ZSTDds_getFrameHeaderSize;
1491     dctx->processedCSize = 0;
1492     dctx->decodedSize = 0;
1493     dctx->previousDstEnd = NULL;
1494     dctx->prefixStart = NULL;
1495     dctx->virtualStart = NULL;
1496     dctx->dictEnd = NULL;
1497     dctx->entropy.hufTable[0] = (HUF_DTable)((ZSTD_HUFFDTABLE_CAPACITY_LOG)*0x1000001);  /* cover both little and big endian */
1498     dctx->litEntropy = dctx->fseEntropy = 0;
1499     dctx->dictID = 0;
1500     dctx->bType = bt_reserved;
1501     dctx->isFrameDecompression = 1;
1502     ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue));
1503     ZSTD_memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue));  /* initial repcodes */
1504     dctx->LLTptr = dctx->entropy.LLTable;
1505     dctx->MLTptr = dctx->entropy.MLTable;
1506     dctx->OFTptr = dctx->entropy.OFTable;
1507     dctx->HUFptr = dctx->entropy.hufTable;
1508     return 0;
1509 }
1510 
ZSTD_decompressBegin_usingDict(ZSTD_DCtx * dctx,const void * dict,size_t dictSize)1511 size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1512 {
1513     FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , "");
1514     if (dict && dictSize)
1515         RETURN_ERROR_IF(
1516             ZSTD_isError(ZSTD_decompress_insertDictionary(dctx, dict, dictSize)),
1517             dictionary_corrupted, "");
1518     return 0;
1519 }
1520 
1521 
1522 /* ======   ZSTD_DDict   ====== */
1523 
ZSTD_decompressBegin_usingDDict(ZSTD_DCtx * dctx,const ZSTD_DDict * ddict)1524 size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
1525 {
1526     DEBUGLOG(4, "ZSTD_decompressBegin_usingDDict");
1527     assert(dctx != NULL);
1528     if (ddict) {
1529         const char* const dictStart = (const char*)ZSTD_DDict_dictContent(ddict);
1530         size_t const dictSize = ZSTD_DDict_dictSize(ddict);
1531         const void* const dictEnd = dictStart + dictSize;
1532         dctx->ddictIsCold = (dctx->dictEnd != dictEnd);
1533         DEBUGLOG(4, "DDict is %s",
1534                     dctx->ddictIsCold ? "~cold~" : "hot!");
1535     }
1536     FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , "");
1537     if (ddict) {   /* NULL ddict is equivalent to no dictionary */
1538         ZSTD_copyDDictParameters(dctx, ddict);
1539     }
1540     return 0;
1541 }
1542 
1543 /*! ZSTD_getDictID_fromDict() :
1544  *  Provides the dictID stored within dictionary.
1545  *  if @return == 0, the dictionary is not conformant with Zstandard specification.
1546  *  It can still be loaded, but as a content-only dictionary. */
ZSTD_getDictID_fromDict(const void * dict,size_t dictSize)1547 unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize)
1548 {
1549     if (dictSize < 8) return 0;
1550     if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0;
1551     return MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
1552 }
1553 
1554 /*! ZSTD_getDictID_fromFrame() :
1555  *  Provides the dictID required to decompress frame stored within `src`.
1556  *  If @return == 0, the dictID could not be decoded.
1557  *  This could for one of the following reasons :
1558  *  - The frame does not require a dictionary (most common case).
1559  *  - The frame was built with dictID intentionally removed.
1560  *    Needed dictionary is a hidden piece of information.
1561  *    Note : this use case also happens when using a non-conformant dictionary.
1562  *  - `srcSize` is too small, and as a result, frame header could not be decoded.
1563  *    Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`.
1564  *  - This is not a Zstandard frame.
1565  *  When identifying the exact failure cause, it's possible to use
1566  *  ZSTD_getFrameHeader(), which will provide a more precise error code. */
ZSTD_getDictID_fromFrame(const void * src,size_t srcSize)1567 unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize)
1568 {
1569     ZSTD_FrameHeader zfp = { 0, 0, 0, ZSTD_frame, 0, 0, 0, 0, 0 };
1570     size_t const hError = ZSTD_getFrameHeader(&zfp, src, srcSize);
1571     if (ZSTD_isError(hError)) return 0;
1572     return zfp.dictID;
1573 }
1574 
1575 
1576 /*! ZSTD_decompress_usingDDict() :
1577 *   Decompression using a pre-digested Dictionary
1578 *   Use dictionary without significant overhead. */
ZSTD_decompress_usingDDict(ZSTD_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize,const ZSTD_DDict * ddict)1579 size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
1580                                   void* dst, size_t dstCapacity,
1581                             const void* src, size_t srcSize,
1582                             const ZSTD_DDict* ddict)
1583 {
1584     /* pass content and size in case legacy frames are encountered */
1585     return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize,
1586                                      NULL, 0,
1587                                      ddict);
1588 }
1589 
1590 
1591 /*=====================================
1592 *   Streaming decompression
1593 *====================================*/
1594 
ZSTD_createDStream(void)1595 ZSTD_DStream* ZSTD_createDStream(void)
1596 {
1597     DEBUGLOG(3, "ZSTD_createDStream");
1598     return ZSTD_createDCtx_internal(ZSTD_defaultCMem);
1599 }
1600 
ZSTD_initStaticDStream(void * workspace,size_t workspaceSize)1601 ZSTD_DStream* ZSTD_initStaticDStream(void *workspace, size_t workspaceSize)
1602 {
1603     return ZSTD_initStaticDCtx(workspace, workspaceSize);
1604 }
1605 
ZSTD_createDStream_advanced(ZSTD_customMem customMem)1606 ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem)
1607 {
1608     return ZSTD_createDCtx_internal(customMem);
1609 }
1610 
ZSTD_freeDStream(ZSTD_DStream * zds)1611 size_t ZSTD_freeDStream(ZSTD_DStream* zds)
1612 {
1613     return ZSTD_freeDCtx(zds);
1614 }
1615 
1616 
1617 /* ***  Initialization  *** */
1618 
ZSTD_DStreamInSize(void)1619 size_t ZSTD_DStreamInSize(void)  { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; }
ZSTD_DStreamOutSize(void)1620 size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; }
1621 
ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx * dctx,const void * dict,size_t dictSize,ZSTD_dictLoadMethod_e dictLoadMethod,ZSTD_dictContentType_e dictContentType)1622 size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx,
1623                                    const void* dict, size_t dictSize,
1624                                          ZSTD_dictLoadMethod_e dictLoadMethod,
1625                                          ZSTD_dictContentType_e dictContentType)
1626 {
1627     RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1628     ZSTD_clearDict(dctx);
1629     if (dict && dictSize != 0) {
1630         dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem);
1631         RETURN_ERROR_IF(dctx->ddictLocal == NULL, memory_allocation, "NULL pointer!");
1632         dctx->ddict = dctx->ddictLocal;
1633         dctx->dictUses = ZSTD_use_indefinitely;
1634     }
1635     return 0;
1636 }
1637 
ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx * dctx,const void * dict,size_t dictSize)1638 size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1639 {
1640     return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto);
1641 }
1642 
ZSTD_DCtx_loadDictionary(ZSTD_DCtx * dctx,const void * dict,size_t dictSize)1643 size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1644 {
1645     return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto);
1646 }
1647 
ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx * dctx,const void * prefix,size_t prefixSize,ZSTD_dictContentType_e dictContentType)1648 size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType)
1649 {
1650     FORWARD_IF_ERROR(ZSTD_DCtx_loadDictionary_advanced(dctx, prefix, prefixSize, ZSTD_dlm_byRef, dictContentType), "");
1651     dctx->dictUses = ZSTD_use_once;
1652     return 0;
1653 }
1654 
ZSTD_DCtx_refPrefix(ZSTD_DCtx * dctx,const void * prefix,size_t prefixSize)1655 size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize)
1656 {
1657     return ZSTD_DCtx_refPrefix_advanced(dctx, prefix, prefixSize, ZSTD_dct_rawContent);
1658 }
1659 
1660 
1661 /* ZSTD_initDStream_usingDict() :
1662  * return : expected size, aka ZSTD_startingInputLength().
1663  * this function cannot fail */
ZSTD_initDStream_usingDict(ZSTD_DStream * zds,const void * dict,size_t dictSize)1664 size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize)
1665 {
1666     DEBUGLOG(4, "ZSTD_initDStream_usingDict");
1667     FORWARD_IF_ERROR( ZSTD_DCtx_reset(zds, ZSTD_reset_session_only) , "");
1668     FORWARD_IF_ERROR( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) , "");
1669     return ZSTD_startingInputLength(zds->format);
1670 }
1671 
1672 /* note : this variant can't fail */
ZSTD_initDStream(ZSTD_DStream * zds)1673 size_t ZSTD_initDStream(ZSTD_DStream* zds)
1674 {
1675     DEBUGLOG(4, "ZSTD_initDStream");
1676     FORWARD_IF_ERROR(ZSTD_DCtx_reset(zds, ZSTD_reset_session_only), "");
1677     FORWARD_IF_ERROR(ZSTD_DCtx_refDDict(zds, NULL), "");
1678     return ZSTD_startingInputLength(zds->format);
1679 }
1680 
1681 /* ZSTD_initDStream_usingDDict() :
1682  * ddict will just be referenced, and must outlive decompression session
1683  * this function cannot fail */
ZSTD_initDStream_usingDDict(ZSTD_DStream * dctx,const ZSTD_DDict * ddict)1684 size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* dctx, const ZSTD_DDict* ddict)
1685 {
1686     DEBUGLOG(4, "ZSTD_initDStream_usingDDict");
1687     FORWARD_IF_ERROR( ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only) , "");
1688     FORWARD_IF_ERROR( ZSTD_DCtx_refDDict(dctx, ddict) , "");
1689     return ZSTD_startingInputLength(dctx->format);
1690 }
1691 
1692 /* ZSTD_resetDStream() :
1693  * return : expected size, aka ZSTD_startingInputLength().
1694  * this function cannot fail */
ZSTD_resetDStream(ZSTD_DStream * dctx)1695 size_t ZSTD_resetDStream(ZSTD_DStream* dctx)
1696 {
1697     DEBUGLOG(4, "ZSTD_resetDStream");
1698     FORWARD_IF_ERROR(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only), "");
1699     return ZSTD_startingInputLength(dctx->format);
1700 }
1701 
1702 
ZSTD_DCtx_refDDict(ZSTD_DCtx * dctx,const ZSTD_DDict * ddict)1703 size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
1704 {
1705     RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1706     ZSTD_clearDict(dctx);
1707     if (ddict) {
1708         dctx->ddict = ddict;
1709         dctx->dictUses = ZSTD_use_indefinitely;
1710         if (dctx->refMultipleDDicts == ZSTD_rmd_refMultipleDDicts) {
1711             if (dctx->ddictSet == NULL) {
1712                 dctx->ddictSet = ZSTD_createDDictHashSet(dctx->customMem);
1713                 if (!dctx->ddictSet) {
1714                     RETURN_ERROR(memory_allocation, "Failed to allocate memory for hash set!");
1715                 }
1716             }
1717             assert(!dctx->staticSize);  /* Impossible: ddictSet cannot have been allocated if static dctx */
1718             FORWARD_IF_ERROR(ZSTD_DDictHashSet_addDDict(dctx->ddictSet, ddict, dctx->customMem), "");
1719         }
1720     }
1721     return 0;
1722 }
1723 
1724 /* ZSTD_DCtx_setMaxWindowSize() :
1725  * note : no direct equivalence in ZSTD_DCtx_setParameter,
1726  * since this version sets windowSize, and the other sets windowLog */
ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx * dctx,size_t maxWindowSize)1727 size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize)
1728 {
1729     ZSTD_bounds const bounds = ZSTD_dParam_getBounds(ZSTD_d_windowLogMax);
1730     size_t const min = (size_t)1 << bounds.lowerBound;
1731     size_t const max = (size_t)1 << bounds.upperBound;
1732     RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1733     RETURN_ERROR_IF(maxWindowSize < min, parameter_outOfBound, "");
1734     RETURN_ERROR_IF(maxWindowSize > max, parameter_outOfBound, "");
1735     dctx->maxWindowSize = maxWindowSize;
1736     return 0;
1737 }
1738 
ZSTD_DCtx_setFormat(ZSTD_DCtx * dctx,ZSTD_format_e format)1739 size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format)
1740 {
1741     return ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, (int)format);
1742 }
1743 
ZSTD_dParam_getBounds(ZSTD_dParameter dParam)1744 ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam)
1745 {
1746     ZSTD_bounds bounds = { 0, 0, 0 };
1747     switch(dParam) {
1748         case ZSTD_d_windowLogMax:
1749             bounds.lowerBound = ZSTD_WINDOWLOG_ABSOLUTEMIN;
1750             bounds.upperBound = ZSTD_WINDOWLOG_MAX;
1751             return bounds;
1752         case ZSTD_d_format:
1753             bounds.lowerBound = (int)ZSTD_f_zstd1;
1754             bounds.upperBound = (int)ZSTD_f_zstd1_magicless;
1755             ZSTD_STATIC_ASSERT(ZSTD_f_zstd1 < ZSTD_f_zstd1_magicless);
1756             return bounds;
1757         case ZSTD_d_stableOutBuffer:
1758             bounds.lowerBound = (int)ZSTD_bm_buffered;
1759             bounds.upperBound = (int)ZSTD_bm_stable;
1760             return bounds;
1761         case ZSTD_d_forceIgnoreChecksum:
1762             bounds.lowerBound = (int)ZSTD_d_validateChecksum;
1763             bounds.upperBound = (int)ZSTD_d_ignoreChecksum;
1764             return bounds;
1765         case ZSTD_d_refMultipleDDicts:
1766             bounds.lowerBound = (int)ZSTD_rmd_refSingleDDict;
1767             bounds.upperBound = (int)ZSTD_rmd_refMultipleDDicts;
1768             return bounds;
1769         case ZSTD_d_disableHuffmanAssembly:
1770             bounds.lowerBound = 0;
1771             bounds.upperBound = 1;
1772             return bounds;
1773         case ZSTD_d_maxBlockSize:
1774             bounds.lowerBound = ZSTD_BLOCKSIZE_MAX_MIN;
1775             bounds.upperBound = ZSTD_BLOCKSIZE_MAX;
1776             return bounds;
1777 
1778         default:;
1779     }
1780     bounds.error = ERROR(parameter_unsupported);
1781     return bounds;
1782 }
1783 
1784 /* ZSTD_dParam_withinBounds:
1785  * @return 1 if value is within dParam bounds,
1786  * 0 otherwise */
ZSTD_dParam_withinBounds(ZSTD_dParameter dParam,int value)1787 static int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam, int value)
1788 {
1789     ZSTD_bounds const bounds = ZSTD_dParam_getBounds(dParam);
1790     if (ZSTD_isError(bounds.error)) return 0;
1791     if (value < bounds.lowerBound) return 0;
1792     if (value > bounds.upperBound) return 0;
1793     return 1;
1794 }
1795 
1796 #define CHECK_DBOUNDS(p,v) {                \
1797     RETURN_ERROR_IF(!ZSTD_dParam_withinBounds(p, v), parameter_outOfBound, ""); \
1798 }
1799 
ZSTD_DCtx_getParameter(ZSTD_DCtx * dctx,ZSTD_dParameter param,int * value)1800 size_t ZSTD_DCtx_getParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int* value)
1801 {
1802     switch (param) {
1803         case ZSTD_d_windowLogMax:
1804             *value = (int)ZSTD_highbit32((U32)dctx->maxWindowSize);
1805             return 0;
1806         case ZSTD_d_format:
1807             *value = (int)dctx->format;
1808             return 0;
1809         case ZSTD_d_stableOutBuffer:
1810             *value = (int)dctx->outBufferMode;
1811             return 0;
1812         case ZSTD_d_forceIgnoreChecksum:
1813             *value = (int)dctx->forceIgnoreChecksum;
1814             return 0;
1815         case ZSTD_d_refMultipleDDicts:
1816             *value = (int)dctx->refMultipleDDicts;
1817             return 0;
1818         case ZSTD_d_disableHuffmanAssembly:
1819             *value = (int)dctx->disableHufAsm;
1820             return 0;
1821         case ZSTD_d_maxBlockSize:
1822             *value = dctx->maxBlockSizeParam;
1823             return 0;
1824         default:;
1825     }
1826     RETURN_ERROR(parameter_unsupported, "");
1827 }
1828 
ZSTD_DCtx_setParameter(ZSTD_DCtx * dctx,ZSTD_dParameter dParam,int value)1829 size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value)
1830 {
1831     RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1832     switch(dParam) {
1833         case ZSTD_d_windowLogMax:
1834             if (value == 0) value = ZSTD_WINDOWLOG_LIMIT_DEFAULT;
1835             CHECK_DBOUNDS(ZSTD_d_windowLogMax, value);
1836             dctx->maxWindowSize = ((size_t)1) << value;
1837             return 0;
1838         case ZSTD_d_format:
1839             CHECK_DBOUNDS(ZSTD_d_format, value);
1840             dctx->format = (ZSTD_format_e)value;
1841             return 0;
1842         case ZSTD_d_stableOutBuffer:
1843             CHECK_DBOUNDS(ZSTD_d_stableOutBuffer, value);
1844             dctx->outBufferMode = (ZSTD_bufferMode_e)value;
1845             return 0;
1846         case ZSTD_d_forceIgnoreChecksum:
1847             CHECK_DBOUNDS(ZSTD_d_forceIgnoreChecksum, value);
1848             dctx->forceIgnoreChecksum = (ZSTD_forceIgnoreChecksum_e)value;
1849             return 0;
1850         case ZSTD_d_refMultipleDDicts:
1851             CHECK_DBOUNDS(ZSTD_d_refMultipleDDicts, value);
1852             if (dctx->staticSize != 0) {
1853                 RETURN_ERROR(parameter_unsupported, "Static dctx does not support multiple DDicts!");
1854             }
1855             dctx->refMultipleDDicts = (ZSTD_refMultipleDDicts_e)value;
1856             return 0;
1857         case ZSTD_d_disableHuffmanAssembly:
1858             CHECK_DBOUNDS(ZSTD_d_disableHuffmanAssembly, value);
1859             dctx->disableHufAsm = value != 0;
1860             return 0;
1861         case ZSTD_d_maxBlockSize:
1862             if (value != 0) CHECK_DBOUNDS(ZSTD_d_maxBlockSize, value);
1863             dctx->maxBlockSizeParam = value;
1864             return 0;
1865         default:;
1866     }
1867     RETURN_ERROR(parameter_unsupported, "");
1868 }
1869 
ZSTD_DCtx_reset(ZSTD_DCtx * dctx,ZSTD_ResetDirective reset)1870 size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset)
1871 {
1872     if ( (reset == ZSTD_reset_session_only)
1873       || (reset == ZSTD_reset_session_and_parameters) ) {
1874         dctx->streamStage = zdss_init;
1875         dctx->noForwardProgress = 0;
1876         dctx->isFrameDecompression = 1;
1877     }
1878     if ( (reset == ZSTD_reset_parameters)
1879       || (reset == ZSTD_reset_session_and_parameters) ) {
1880         RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1881         ZSTD_clearDict(dctx);
1882         ZSTD_DCtx_resetParameters(dctx);
1883     }
1884     return 0;
1885 }
1886 
1887 
ZSTD_sizeof_DStream(const ZSTD_DStream * dctx)1888 size_t ZSTD_sizeof_DStream(const ZSTD_DStream* dctx)
1889 {
1890     return ZSTD_sizeof_DCtx(dctx);
1891 }
1892 
ZSTD_decodingBufferSize_internal(unsigned long long windowSize,unsigned long long frameContentSize,size_t blockSizeMax)1893 static size_t ZSTD_decodingBufferSize_internal(unsigned long long windowSize, unsigned long long frameContentSize, size_t blockSizeMax)
1894 {
1895     size_t const blockSize = MIN((size_t)MIN(windowSize, ZSTD_BLOCKSIZE_MAX), blockSizeMax);
1896     /* We need blockSize + WILDCOPY_OVERLENGTH worth of buffer so that if a block
1897      * ends at windowSize + WILDCOPY_OVERLENGTH + 1 bytes, we can start writing
1898      * the block at the beginning of the output buffer, and maintain a full window.
1899      *
1900      * We need another blockSize worth of buffer so that we can store split
1901      * literals at the end of the block without overwriting the extDict window.
1902      */
1903     unsigned long long const neededRBSize = windowSize + (blockSize * 2) + (WILDCOPY_OVERLENGTH * 2);
1904     unsigned long long const neededSize = MIN(frameContentSize, neededRBSize);
1905     size_t const minRBSize = (size_t) neededSize;
1906     RETURN_ERROR_IF((unsigned long long)minRBSize != neededSize,
1907                     frameParameter_windowTooLarge, "");
1908     return minRBSize;
1909 }
1910 
ZSTD_decodingBufferSize_min(unsigned long long windowSize,unsigned long long frameContentSize)1911 size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize)
1912 {
1913     return ZSTD_decodingBufferSize_internal(windowSize, frameContentSize, ZSTD_BLOCKSIZE_MAX);
1914 }
1915 
ZSTD_estimateDStreamSize(size_t windowSize)1916 size_t ZSTD_estimateDStreamSize(size_t windowSize)
1917 {
1918     size_t const blockSize = MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
1919     size_t const inBuffSize = blockSize;  /* no block can be larger */
1920     size_t const outBuffSize = ZSTD_decodingBufferSize_min(windowSize, ZSTD_CONTENTSIZE_UNKNOWN);
1921     return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize;
1922 }
1923 
ZSTD_estimateDStreamSize_fromFrame(const void * src,size_t srcSize)1924 size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize)
1925 {
1926     U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX;   /* note : should be user-selectable, but requires an additional parameter (or a dctx) */
1927     ZSTD_FrameHeader zfh;
1928     size_t const err = ZSTD_getFrameHeader(&zfh, src, srcSize);
1929     if (ZSTD_isError(err)) return err;
1930     RETURN_ERROR_IF(err>0, srcSize_wrong, "");
1931     RETURN_ERROR_IF(zfh.windowSize > windowSizeMax,
1932                     frameParameter_windowTooLarge, "");
1933     return ZSTD_estimateDStreamSize((size_t)zfh.windowSize);
1934 }
1935 
1936 
1937 /* *****   Decompression   ***** */
1938 
ZSTD_DCtx_isOverflow(ZSTD_DStream * zds,size_t const neededInBuffSize,size_t const neededOutBuffSize)1939 static int ZSTD_DCtx_isOverflow(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize)
1940 {
1941     return (zds->inBuffSize + zds->outBuffSize) >= (neededInBuffSize + neededOutBuffSize) * ZSTD_WORKSPACETOOLARGE_FACTOR;
1942 }
1943 
ZSTD_DCtx_updateOversizedDuration(ZSTD_DStream * zds,size_t const neededInBuffSize,size_t const neededOutBuffSize)1944 static void ZSTD_DCtx_updateOversizedDuration(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize)
1945 {
1946     if (ZSTD_DCtx_isOverflow(zds, neededInBuffSize, neededOutBuffSize))
1947         zds->oversizedDuration++;
1948     else
1949         zds->oversizedDuration = 0;
1950 }
1951 
ZSTD_DCtx_isOversizedTooLong(ZSTD_DStream * zds)1952 static int ZSTD_DCtx_isOversizedTooLong(ZSTD_DStream* zds)
1953 {
1954     return zds->oversizedDuration >= ZSTD_WORKSPACETOOLARGE_MAXDURATION;
1955 }
1956 
1957 /* Checks that the output buffer hasn't changed if ZSTD_obm_stable is used. */
ZSTD_checkOutBuffer(ZSTD_DStream const * zds,ZSTD_outBuffer const * output)1958 static size_t ZSTD_checkOutBuffer(ZSTD_DStream const* zds, ZSTD_outBuffer const* output)
1959 {
1960     ZSTD_outBuffer const expect = zds->expectedOutBuffer;
1961     /* No requirement when ZSTD_obm_stable is not enabled. */
1962     if (zds->outBufferMode != ZSTD_bm_stable)
1963         return 0;
1964     /* Any buffer is allowed in zdss_init, this must be the same for every other call until
1965      * the context is reset.
1966      */
1967     if (zds->streamStage == zdss_init)
1968         return 0;
1969     /* The buffer must match our expectation exactly. */
1970     if (expect.dst == output->dst && expect.pos == output->pos && expect.size == output->size)
1971         return 0;
1972     RETURN_ERROR(dstBuffer_wrong, "ZSTD_d_stableOutBuffer enabled but output differs!");
1973 }
1974 
1975 /* Calls ZSTD_decompressContinue() with the right parameters for ZSTD_decompressStream()
1976  * and updates the stage and the output buffer state. This call is extracted so it can be
1977  * used both when reading directly from the ZSTD_inBuffer, and in buffered input mode.
1978  * NOTE: You must break after calling this function since the streamStage is modified.
1979  */
ZSTD_decompressContinueStream(ZSTD_DStream * zds,char ** op,char * oend,void const * src,size_t srcSize)1980 static size_t ZSTD_decompressContinueStream(
1981             ZSTD_DStream* zds, char** op, char* oend,
1982             void const* src, size_t srcSize) {
1983     int const isSkipFrame = ZSTD_isSkipFrame(zds);
1984     if (zds->outBufferMode == ZSTD_bm_buffered) {
1985         size_t const dstSize = isSkipFrame ? 0 : zds->outBuffSize - zds->outStart;
1986         size_t const decodedSize = ZSTD_decompressContinue(zds,
1987                 zds->outBuff + zds->outStart, dstSize, src, srcSize);
1988         FORWARD_IF_ERROR(decodedSize, "");
1989         if (!decodedSize && !isSkipFrame) {
1990             zds->streamStage = zdss_read;
1991         } else {
1992             zds->outEnd = zds->outStart + decodedSize;
1993             zds->streamStage = zdss_flush;
1994         }
1995     } else {
1996         /* Write directly into the output buffer */
1997         size_t const dstSize = isSkipFrame ? 0 : (size_t)(oend - *op);
1998         size_t const decodedSize = ZSTD_decompressContinue(zds, *op, dstSize, src, srcSize);
1999         FORWARD_IF_ERROR(decodedSize, "");
2000         *op += decodedSize;
2001         /* Flushing is not needed. */
2002         zds->streamStage = zdss_read;
2003         assert(*op <= oend);
2004         assert(zds->outBufferMode == ZSTD_bm_stable);
2005     }
2006     return 0;
2007 }
2008 
ZSTD_decompressStream(ZSTD_DStream * zds,ZSTD_outBuffer * output,ZSTD_inBuffer * input)2009 size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
2010 {
2011     const char* const src = (const char*)input->src;
2012     const char* const istart = input->pos != 0 ? src + input->pos : src;
2013     const char* const iend = input->size != 0 ? src + input->size : src;
2014     const char* ip = istart;
2015     char* const dst = (char*)output->dst;
2016     char* const ostart = output->pos != 0 ? dst + output->pos : dst;
2017     char* const oend = output->size != 0 ? dst + output->size : dst;
2018     char* op = ostart;
2019     U32 someMoreWork = 1;
2020 
2021     DEBUGLOG(5, "ZSTD_decompressStream");
2022     assert(zds != NULL);
2023     RETURN_ERROR_IF(
2024         input->pos > input->size,
2025         srcSize_wrong,
2026         "forbidden. in: pos: %u   vs size: %u",
2027         (U32)input->pos, (U32)input->size);
2028     RETURN_ERROR_IF(
2029         output->pos > output->size,
2030         dstSize_tooSmall,
2031         "forbidden. out: pos: %u   vs size: %u",
2032         (U32)output->pos, (U32)output->size);
2033     DEBUGLOG(5, "input size : %u", (U32)(input->size - input->pos));
2034     FORWARD_IF_ERROR(ZSTD_checkOutBuffer(zds, output), "");
2035 
2036     while (someMoreWork) {
2037         switch(zds->streamStage)
2038         {
2039         case zdss_init :
2040             DEBUGLOG(5, "stage zdss_init => transparent reset ");
2041             zds->streamStage = zdss_loadHeader;
2042             zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
2043             zds->hostageByte = 0;
2044             zds->expectedOutBuffer = *output;
2045             ZSTD_FALLTHROUGH;
2046 
2047         case zdss_loadHeader :
2048             DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32)(iend - ip));
2049             {   size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format);
2050                 if (zds->refMultipleDDicts && zds->ddictSet) {
2051                     ZSTD_DCtx_selectFrameDDict(zds);
2052                 }
2053                 if (ZSTD_isError(hSize)) {
2054                     return hSize;   /* error */
2055                 }
2056                 if (hSize != 0) {   /* need more input */
2057                     size_t const toLoad = hSize - zds->lhSize;   /* if hSize!=0, hSize > zds->lhSize */
2058                     size_t const remainingInput = (size_t)(iend-ip);
2059                     assert(iend >= ip);
2060                     if (toLoad > remainingInput) {   /* not enough input to load full header */
2061                         if (remainingInput > 0) {
2062                             ZSTD_memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput);
2063                             zds->lhSize += remainingInput;
2064                         }
2065                         input->pos = input->size;
2066                         /* check first few bytes */
2067                         FORWARD_IF_ERROR(
2068                             ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format),
2069                             "First few bytes detected incorrect" );
2070                         /* return hint input size */
2071                         return (MAX((size_t)ZSTD_FRAMEHEADERSIZE_MIN(zds->format), hSize) - zds->lhSize) + ZSTD_blockHeaderSize;   /* remaining header bytes + next block header */
2072                     }
2073                     assert(ip != NULL);
2074                     ZSTD_memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad;
2075                     break;
2076             }   }
2077 
2078             /* check for single-pass mode opportunity */
2079             if (zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
2080                 && zds->fParams.frameType != ZSTD_skippableFrame
2081                 && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) {
2082                 size_t const cSize = ZSTD_findFrameCompressedSize_advanced(istart, (size_t)(iend-istart), zds->format);
2083                 if (cSize <= (size_t)(iend-istart)) {
2084                     /* shortcut : using single-pass mode */
2085                     size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, (size_t)(oend-op), istart, cSize, ZSTD_getDDict(zds));
2086                     if (ZSTD_isError(decompressedSize)) return decompressedSize;
2087                     DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()");
2088                     assert(istart != NULL);
2089                     ip = istart + cSize;
2090                     op = op ? op + decompressedSize : op; /* can occur if frameContentSize = 0 (empty frame) */
2091                     zds->expected = 0;
2092                     zds->streamStage = zdss_init;
2093                     someMoreWork = 0;
2094                     break;
2095             }   }
2096 
2097             /* Check output buffer is large enough for ZSTD_odm_stable. */
2098             if (zds->outBufferMode == ZSTD_bm_stable
2099                 && zds->fParams.frameType != ZSTD_skippableFrame
2100                 && zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
2101                 && (U64)(size_t)(oend-op) < zds->fParams.frameContentSize) {
2102                 RETURN_ERROR(dstSize_tooSmall, "ZSTD_obm_stable passed but ZSTD_outBuffer is too small");
2103             }
2104 
2105             /* Consume header (see ZSTDds_decodeFrameHeader) */
2106             DEBUGLOG(4, "Consume header");
2107             FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(zds, ZSTD_getDDict(zds)), "");
2108 
2109             if (zds->format == ZSTD_f_zstd1
2110                 && (MEM_readLE32(zds->headerBuffer) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {  /* skippable frame */
2111                 zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_FRAMEIDSIZE);
2112                 zds->stage = ZSTDds_skipFrame;
2113             } else {
2114                 FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize), "");
2115                 zds->expected = ZSTD_blockHeaderSize;
2116                 zds->stage = ZSTDds_decodeBlockHeader;
2117             }
2118 
2119             /* control buffer memory usage */
2120             DEBUGLOG(4, "Control max memory usage (%u KB <= max %u KB)",
2121                         (U32)(zds->fParams.windowSize >>10),
2122                         (U32)(zds->maxWindowSize >> 10) );
2123             zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN);
2124             RETURN_ERROR_IF(zds->fParams.windowSize > zds->maxWindowSize,
2125                             frameParameter_windowTooLarge, "");
2126             if (zds->maxBlockSizeParam != 0)
2127                 zds->fParams.blockSizeMax = MIN(zds->fParams.blockSizeMax, (unsigned)zds->maxBlockSizeParam);
2128 
2129             /* Adapt buffer sizes to frame header instructions */
2130             {   size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */);
2131                 size_t const neededOutBuffSize = zds->outBufferMode == ZSTD_bm_buffered
2132                         ? ZSTD_decodingBufferSize_internal(zds->fParams.windowSize, zds->fParams.frameContentSize, zds->fParams.blockSizeMax)
2133                         : 0;
2134 
2135                 ZSTD_DCtx_updateOversizedDuration(zds, neededInBuffSize, neededOutBuffSize);
2136 
2137                 {   int const tooSmall = (zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize);
2138                     int const tooLarge = ZSTD_DCtx_isOversizedTooLong(zds);
2139 
2140                     if (tooSmall || tooLarge) {
2141                         size_t const bufferSize = neededInBuffSize + neededOutBuffSize;
2142                         DEBUGLOG(4, "inBuff  : from %u to %u",
2143                                     (U32)zds->inBuffSize, (U32)neededInBuffSize);
2144                         DEBUGLOG(4, "outBuff : from %u to %u",
2145                                     (U32)zds->outBuffSize, (U32)neededOutBuffSize);
2146                         if (zds->staticSize) {  /* static DCtx */
2147                             DEBUGLOG(4, "staticSize : %u", (U32)zds->staticSize);
2148                             assert(zds->staticSize >= sizeof(ZSTD_DCtx));  /* controlled at init */
2149                             RETURN_ERROR_IF(
2150                                 bufferSize > zds->staticSize - sizeof(ZSTD_DCtx),
2151                                 memory_allocation, "");
2152                         } else {
2153                             ZSTD_customFree(zds->inBuff, zds->customMem);
2154                             zds->inBuffSize = 0;
2155                             zds->outBuffSize = 0;
2156                             zds->inBuff = (char*)ZSTD_customMalloc(bufferSize, zds->customMem);
2157                             RETURN_ERROR_IF(zds->inBuff == NULL, memory_allocation, "");
2158                         }
2159                         zds->inBuffSize = neededInBuffSize;
2160                         zds->outBuff = zds->inBuff + zds->inBuffSize;
2161                         zds->outBuffSize = neededOutBuffSize;
2162             }   }   }
2163             zds->streamStage = zdss_read;
2164             ZSTD_FALLTHROUGH;
2165 
2166         case zdss_read:
2167             DEBUGLOG(5, "stage zdss_read");
2168             {   size_t const neededInSize = ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (size_t)(iend - ip));
2169                 DEBUGLOG(5, "neededInSize = %u", (U32)neededInSize);
2170                 if (neededInSize==0) {  /* end of frame */
2171                     zds->streamStage = zdss_init;
2172                     someMoreWork = 0;
2173                     break;
2174                 }
2175                 if ((size_t)(iend-ip) >= neededInSize) {  /* decode directly from src */
2176                     FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, ip, neededInSize), "");
2177                     assert(ip != NULL);
2178                     ip += neededInSize;
2179                     /* Function modifies the stage so we must break */
2180                     break;
2181             }   }
2182             if (ip==iend) { someMoreWork = 0; break; }   /* no more input */
2183             zds->streamStage = zdss_load;
2184             ZSTD_FALLTHROUGH;
2185 
2186         case zdss_load:
2187             {   size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds);
2188                 size_t const toLoad = neededInSize - zds->inPos;
2189                 int const isSkipFrame = ZSTD_isSkipFrame(zds);
2190                 size_t loadedSize;
2191                 /* At this point we shouldn't be decompressing a block that we can stream. */
2192                 assert(neededInSize == ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (size_t)(iend - ip)));
2193                 if (isSkipFrame) {
2194                     loadedSize = MIN(toLoad, (size_t)(iend-ip));
2195                 } else {
2196                     RETURN_ERROR_IF(toLoad > zds->inBuffSize - zds->inPos,
2197                                     corruption_detected,
2198                                     "should never happen");
2199                     loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, (size_t)(iend-ip));
2200                 }
2201                 if (loadedSize != 0) {
2202                     /* ip may be NULL */
2203                     ip += loadedSize;
2204                     zds->inPos += loadedSize;
2205                 }
2206                 if (loadedSize < toLoad) { someMoreWork = 0; break; }   /* not enough input, wait for more */
2207 
2208                 /* decode loaded input */
2209                 zds->inPos = 0;   /* input is consumed */
2210                 FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, zds->inBuff, neededInSize), "");
2211                 /* Function modifies the stage so we must break */
2212                 break;
2213             }
2214         case zdss_flush:
2215             {
2216                 size_t const toFlushSize = zds->outEnd - zds->outStart;
2217                 size_t const flushedSize = ZSTD_limitCopy(op, (size_t)(oend-op), zds->outBuff + zds->outStart, toFlushSize);
2218 
2219                 op = op ? op + flushedSize : op;
2220 
2221                 zds->outStart += flushedSize;
2222                 if (flushedSize == toFlushSize) {  /* flush completed */
2223                     zds->streamStage = zdss_read;
2224                     if ( (zds->outBuffSize < zds->fParams.frameContentSize)
2225                         && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize) ) {
2226                         DEBUGLOG(5, "restart filling outBuff from beginning (left:%i, needed:%u)",
2227                                 (int)(zds->outBuffSize - zds->outStart),
2228                                 (U32)zds->fParams.blockSizeMax);
2229                         zds->outStart = zds->outEnd = 0;
2230                     }
2231                     break;
2232             }   }
2233             /* cannot complete flush */
2234             someMoreWork = 0;
2235             break;
2236 
2237         default:
2238             assert(0);    /* impossible */
2239             RETURN_ERROR(GENERIC, "impossible to reach");   /* some compilers require default to do something */
2240     }   }
2241 
2242     /* result */
2243     input->pos = (size_t)(ip - (const char*)(input->src));
2244     output->pos = (size_t)(op - (char*)(output->dst));
2245 
2246     /* Update the expected output buffer for ZSTD_obm_stable. */
2247     zds->expectedOutBuffer = *output;
2248 
2249     if ((ip==istart) && (op==ostart)) {  /* no forward progress */
2250         zds->noForwardProgress ++;
2251         if (zds->noForwardProgress >= ZSTD_NO_FORWARD_PROGRESS_MAX) {
2252             RETURN_ERROR_IF(op==oend, noForwardProgress_destFull, "");
2253             RETURN_ERROR_IF(ip==iend, noForwardProgress_inputEmpty, "");
2254             assert(0);
2255         }
2256     } else {
2257         zds->noForwardProgress = 0;
2258     }
2259     {   size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds);
2260         if (!nextSrcSizeHint) {   /* frame fully decoded */
2261             if (zds->outEnd == zds->outStart) {  /* output fully flushed */
2262                 if (zds->hostageByte) {
2263                     if (input->pos >= input->size) {
2264                         /* can't release hostage (not present) */
2265                         zds->streamStage = zdss_read;
2266                         return 1;
2267                     }
2268                     input->pos++;  /* release hostage */
2269                 }   /* zds->hostageByte */
2270                 return 0;
2271             }  /* zds->outEnd == zds->outStart */
2272             if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */
2273                 input->pos--;   /* note : pos > 0, otherwise, impossible to finish reading last block */
2274                 zds->hostageByte=1;
2275             }
2276             return 1;
2277         }  /* nextSrcSizeHint==0 */
2278         nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block);   /* preload header of next block */
2279         assert(zds->inPos <= nextSrcSizeHint);
2280         nextSrcSizeHint -= zds->inPos;   /* part already loaded*/
2281         return nextSrcSizeHint;
2282     }
2283 }
2284 
ZSTD_decompressStream_simpleArgs(ZSTD_DCtx * dctx,void * dst,size_t dstCapacity,size_t * dstPos,const void * src,size_t srcSize,size_t * srcPos)2285 size_t ZSTD_decompressStream_simpleArgs (
2286                             ZSTD_DCtx* dctx,
2287                             void* dst, size_t dstCapacity, size_t* dstPos,
2288                       const void* src, size_t srcSize, size_t* srcPos)
2289 {
2290     ZSTD_outBuffer output;
2291     ZSTD_inBuffer  input;
2292     output.dst = dst;
2293     output.size = dstCapacity;
2294     output.pos = *dstPos;
2295     input.src = src;
2296     input.size = srcSize;
2297     input.pos = *srcPos;
2298     {   size_t const cErr = ZSTD_decompressStream(dctx, &output, &input);
2299         *dstPos = output.pos;
2300         *srcPos = input.pos;
2301         return cErr;
2302     }
2303 }
2304