~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/zstd/decompress/zstd_decompress.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /lib/zstd/decompress/zstd_decompress.c (Architecture sparc64) and /lib/zstd/decompress/zstd_decompress.c (Architecture i386)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php