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