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