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