2 * Copyright (c) Yann Collet, Facebook, Inc.
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.
12 /* ***************************************************************
14 *****************************************************************/
17 * Select how default decompression function ZSTD_decompress() allocates its context,
18 * on stack (0), or into heap (1, default; requires malloc()).
19 * Note that functions with explicit context such as ZSTD_decompressDCtx() are unaffected.
22 # define ZSTD_HEAPMODE 1
27 * if set to 1+, ZSTD_decompress() can decode older formats (v0.1+)
31 * MAXWINDOWSIZE_DEFAULT :
32 * maximum window size accepted by DStream __by default__.
33 * Frames requiring more memory will be rejected.
34 * It's possible to set a different limit using ZSTD_DCtx_setMaxWindowSize().
36 #ifndef ZSTD_MAXWINDOWSIZE_DEFAULT
37 # define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) + 1)
41 * NO_FORWARD_PROGRESS_MAX :
42 * maximum allowed nb of calls to ZSTD_decompressStream()
43 * without any forward progress
44 * (defined as: no byte read from input, and no byte flushed to output)
45 * before triggering an error.
47 #ifndef ZSTD_NO_FORWARD_PROGRESS_MAX
48 # define ZSTD_NO_FORWARD_PROGRESS_MAX 16
52 /*-*******************************************************
54 *********************************************************/
55 #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */
56 #include "../common/mem.h" /* low level memory routines */
57 #define FSE_STATIC_LINKING_ONLY
58 #include "../common/fse.h"
59 #define HUF_STATIC_LINKING_ONLY
60 #include "../common/huf.h"
61 #include <linux/xxhash.h> /* xxh64_reset, xxh64_update, xxh64_digest, XXH64 */
62 #include "../common/zstd_internal.h" /* blockProperties_t */
63 #include "zstd_decompress_internal.h" /* ZSTD_DCtx */
64 #include "zstd_ddict.h" /* ZSTD_DDictDictContent */
65 #include "zstd_decompress_block.h" /* ZSTD_decompressBlock_internal */
70 /* ***********************************
71 * Multiple DDicts Hashset internals *
72 *************************************/
74 #define DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT 4
75 #define DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT 3 /* These two constants represent SIZE_MULT/COUNT_MULT load factor without using a float.
76 * Currently, that means a 0.75 load factor.
77 * So, if count * COUNT_MULT / size * SIZE_MULT != 0, then we've exceeded
78 * the load factor of the ddict hash set.
81 #define DDICT_HASHSET_TABLE_BASE_SIZE 64
82 #define DDICT_HASHSET_RESIZE_FACTOR 2
84 /* Hash function to determine starting position of dict insertion within the table
85 * Returns an index between [0, hashSet->ddictPtrTableSize]
87 static size_t ZSTD_DDictHashSet_getIndex(const ZSTD_DDictHashSet
* hashSet
, U32 dictID
) {
88 const U64 hash
= xxh64(&dictID
, sizeof(U32
), 0);
89 /* DDict ptr table size is a multiple of 2, use size - 1 as mask to get index within [0, hashSet->ddictPtrTableSize) */
90 return hash
& (hashSet
->ddictPtrTableSize
- 1);
93 /* Adds DDict to a hashset without resizing it.
94 * If inserting a DDict with a dictID that already exists in the set, replaces the one in the set.
95 * Returns 0 if successful, or a zstd error code if something went wrong.
97 static size_t ZSTD_DDictHashSet_emplaceDDict(ZSTD_DDictHashSet
* hashSet
, const ZSTD_DDict
* ddict
) {
98 const U32 dictID
= ZSTD_getDictID_fromDDict(ddict
);
99 size_t idx
= ZSTD_DDictHashSet_getIndex(hashSet
, dictID
);
100 const size_t idxRangeMask
= hashSet
->ddictPtrTableSize
- 1;
101 RETURN_ERROR_IF(hashSet
->ddictPtrCount
== hashSet
->ddictPtrTableSize
, GENERIC
, "Hash set is full!");
102 DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID
, idx
);
103 while (hashSet
->ddictPtrTable
[idx
] != NULL
) {
104 /* Replace existing ddict if inserting ddict with same dictID */
105 if (ZSTD_getDictID_fromDDict(hashSet
->ddictPtrTable
[idx
]) == dictID
) {
106 DEBUGLOG(4, "DictID already exists, replacing rather than adding");
107 hashSet
->ddictPtrTable
[idx
] = ddict
;
113 DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID
, idx
);
114 hashSet
->ddictPtrTable
[idx
] = ddict
;
115 hashSet
->ddictPtrCount
++;
119 /* Expands hash table by factor of DDICT_HASHSET_RESIZE_FACTOR and
120 * rehashes all values, allocates new table, frees old table.
121 * Returns 0 on success, otherwise a zstd error code.
123 static size_t ZSTD_DDictHashSet_expand(ZSTD_DDictHashSet
* hashSet
, ZSTD_customMem customMem
) {
124 size_t newTableSize
= hashSet
->ddictPtrTableSize
* DDICT_HASHSET_RESIZE_FACTOR
;
125 const ZSTD_DDict
** newTable
= (const ZSTD_DDict
**)ZSTD_customCalloc(sizeof(ZSTD_DDict
*) * newTableSize
, customMem
);
126 const ZSTD_DDict
** oldTable
= hashSet
->ddictPtrTable
;
127 size_t oldTableSize
= hashSet
->ddictPtrTableSize
;
130 DEBUGLOG(4, "Expanding DDict hash table! Old size: %zu new size: %zu", oldTableSize
, newTableSize
);
131 RETURN_ERROR_IF(!newTable
, memory_allocation
, "Expanded hashset allocation failed!");
132 hashSet
->ddictPtrTable
= newTable
;
133 hashSet
->ddictPtrTableSize
= newTableSize
;
134 hashSet
->ddictPtrCount
= 0;
135 for (i
= 0; i
< oldTableSize
; ++i
) {
136 if (oldTable
[i
] != NULL
) {
137 FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet
, oldTable
[i
]), "");
140 ZSTD_customFree((void*)oldTable
, customMem
);
141 DEBUGLOG(4, "Finished re-hash");
145 /* Fetches a DDict with the given dictID
146 * Returns the ZSTD_DDict* with the requested dictID. If it doesn't exist, then returns NULL.
148 static const ZSTD_DDict
* ZSTD_DDictHashSet_getDDict(ZSTD_DDictHashSet
* hashSet
, U32 dictID
) {
149 size_t idx
= ZSTD_DDictHashSet_getIndex(hashSet
, dictID
);
150 const size_t idxRangeMask
= hashSet
->ddictPtrTableSize
- 1;
151 DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID
, idx
);
153 size_t currDictID
= ZSTD_getDictID_fromDDict(hashSet
->ddictPtrTable
[idx
]);
154 if (currDictID
== dictID
|| currDictID
== 0) {
155 /* currDictID == 0 implies a NULL ddict entry */
158 idx
&= idxRangeMask
; /* Goes to start of table when we reach the end */
162 DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID
, idx
);
163 return hashSet
->ddictPtrTable
[idx
];
166 /* Allocates space for and returns a ddict hash set
167 * The hash set's ZSTD_DDict* table has all values automatically set to NULL to begin with.
168 * Returns NULL if allocation failed.
170 static ZSTD_DDictHashSet
* ZSTD_createDDictHashSet(ZSTD_customMem customMem
) {
171 ZSTD_DDictHashSet
* ret
= (ZSTD_DDictHashSet
*)ZSTD_customMalloc(sizeof(ZSTD_DDictHashSet
), customMem
);
172 DEBUGLOG(4, "Allocating new hash set");
175 ret
->ddictPtrTable
= (const ZSTD_DDict
**)ZSTD_customCalloc(DDICT_HASHSET_TABLE_BASE_SIZE
* sizeof(ZSTD_DDict
*), customMem
);
176 if (!ret
->ddictPtrTable
) {
177 ZSTD_customFree(ret
, customMem
);
180 ret
->ddictPtrTableSize
= DDICT_HASHSET_TABLE_BASE_SIZE
;
181 ret
->ddictPtrCount
= 0;
185 /* Frees the table of ZSTD_DDict* within a hashset, then frees the hashset itself.
186 * Note: The ZSTD_DDict* within the table are NOT freed.
188 static void ZSTD_freeDDictHashSet(ZSTD_DDictHashSet
* hashSet
, ZSTD_customMem customMem
) {
189 DEBUGLOG(4, "Freeing ddict hash set");
190 if (hashSet
&& hashSet
->ddictPtrTable
) {
191 ZSTD_customFree((void*)hashSet
->ddictPtrTable
, customMem
);
194 ZSTD_customFree(hashSet
, customMem
);
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.
201 static size_t ZSTD_DDictHashSet_addDDict(ZSTD_DDictHashSet
* hashSet
, const ZSTD_DDict
* ddict
, ZSTD_customMem customMem
) {
202 DEBUGLOG(4, "Adding dict ID: %u to hashset with - Count: %zu Tablesize: %zu", ZSTD_getDictID_fromDDict(ddict
), hashSet
->ddictPtrCount
, hashSet
->ddictPtrTableSize
);
203 if (hashSet
->ddictPtrCount
* DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT
/ hashSet
->ddictPtrTableSize
* DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT
!= 0) {
204 FORWARD_IF_ERROR(ZSTD_DDictHashSet_expand(hashSet
, customMem
), "");
206 FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet
, ddict
), "");
210 /*-*************************************************************
212 ***************************************************************/
213 size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx
* dctx
)
215 if (dctx
==NULL
) return 0; /* support sizeof NULL */
217 + ZSTD_sizeof_DDict(dctx
->ddictLocal
)
218 + dctx
->inBuffSize
+ dctx
->outBuffSize
;
221 size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx
); }
224 static size_t ZSTD_startingInputLength(ZSTD_format_e format
)
226 size_t const startingInputLength
= ZSTD_FRAMEHEADERSIZE_PREFIX(format
);
227 /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */
228 assert( (format
== ZSTD_f_zstd1
) || (format
== ZSTD_f_zstd1_magicless
) );
229 return startingInputLength
;
232 static void ZSTD_DCtx_resetParameters(ZSTD_DCtx
* dctx
)
234 assert(dctx
->streamStage
== zdss_init
);
235 dctx
->format
= ZSTD_f_zstd1
;
236 dctx
->maxWindowSize
= ZSTD_MAXWINDOWSIZE_DEFAULT
;
237 dctx
->outBufferMode
= ZSTD_bm_buffered
;
238 dctx
->forceIgnoreChecksum
= ZSTD_d_validateChecksum
;
239 dctx
->refMultipleDDicts
= ZSTD_rmd_refSingleDDict
;
242 static void ZSTD_initDCtx_internal(ZSTD_DCtx
* dctx
)
244 dctx
->staticSize
= 0;
246 dctx
->ddictLocal
= NULL
;
247 dctx
->dictEnd
= NULL
;
248 dctx
->ddictIsCold
= 0;
249 dctx
->dictUses
= ZSTD_dont_use
;
251 dctx
->inBuffSize
= 0;
252 dctx
->outBuffSize
= 0;
253 dctx
->streamStage
= zdss_init
;
254 dctx
->noForwardProgress
= 0;
255 dctx
->oversizedDuration
= 0;
257 dctx
->bmi2
= ZSTD_cpuSupportsBmi2();
259 dctx
->ddictSet
= NULL
;
260 ZSTD_DCtx_resetParameters(dctx
);
261 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
262 dctx
->dictContentEndForFuzzing
= NULL
;
266 ZSTD_DCtx
* ZSTD_initStaticDCtx(void *workspace
, size_t workspaceSize
)
268 ZSTD_DCtx
* const dctx
= (ZSTD_DCtx
*) workspace
;
270 if ((size_t)workspace
& 7) return NULL
; /* 8-aligned */
271 if (workspaceSize
< sizeof(ZSTD_DCtx
)) return NULL
; /* minimum size */
273 ZSTD_initDCtx_internal(dctx
);
274 dctx
->staticSize
= workspaceSize
;
275 dctx
->inBuff
= (char*)(dctx
+1);
279 static ZSTD_DCtx
* ZSTD_createDCtx_internal(ZSTD_customMem customMem
) {
280 if ((!customMem
.customAlloc
) ^ (!customMem
.customFree
)) return NULL
;
282 { ZSTD_DCtx
* const dctx
= (ZSTD_DCtx
*)ZSTD_customMalloc(sizeof(*dctx
), customMem
);
283 if (!dctx
) return NULL
;
284 dctx
->customMem
= customMem
;
285 ZSTD_initDCtx_internal(dctx
);
290 ZSTD_DCtx
* ZSTD_createDCtx_advanced(ZSTD_customMem customMem
)
292 return ZSTD_createDCtx_internal(customMem
);
295 ZSTD_DCtx
* ZSTD_createDCtx(void)
297 DEBUGLOG(3, "ZSTD_createDCtx");
298 return ZSTD_createDCtx_internal(ZSTD_defaultCMem
);
301 static void ZSTD_clearDict(ZSTD_DCtx
* dctx
)
303 ZSTD_freeDDict(dctx
->ddictLocal
);
304 dctx
->ddictLocal
= NULL
;
306 dctx
->dictUses
= ZSTD_dont_use
;
309 size_t ZSTD_freeDCtx(ZSTD_DCtx
* dctx
)
311 if (dctx
==NULL
) return 0; /* support free on NULL */
312 RETURN_ERROR_IF(dctx
->staticSize
, memory_allocation
, "not compatible with static DCtx");
313 { ZSTD_customMem
const cMem
= dctx
->customMem
;
314 ZSTD_clearDict(dctx
);
315 ZSTD_customFree(dctx
->inBuff
, cMem
);
317 if (dctx
->ddictSet
) {
318 ZSTD_freeDDictHashSet(dctx
->ddictSet
, cMem
);
319 dctx
->ddictSet
= NULL
;
321 ZSTD_customFree(dctx
, cMem
);
326 /* no longer useful */
327 void ZSTD_copyDCtx(ZSTD_DCtx
* dstDCtx
, const ZSTD_DCtx
* srcDCtx
)
329 size_t const toCopy
= (size_t)((char*)(&dstDCtx
->inBuff
) - (char*)dstDCtx
);
330 ZSTD_memcpy(dstDCtx
, srcDCtx
, toCopy
); /* no need to copy workspace */
333 /* Given a dctx with a digested frame params, re-selects the correct ZSTD_DDict based on
334 * the requested dict ID from the frame. If there exists a reference to the correct ZSTD_DDict, then
335 * accordingly sets the ddict to be used to decompress the frame.
337 * If no DDict is found, then no action is taken, and the ZSTD_DCtx::ddict remains as-is.
339 * ZSTD_d_refMultipleDDicts must be enabled for this function to be called.
341 static void ZSTD_DCtx_selectFrameDDict(ZSTD_DCtx
* dctx
) {
342 assert(dctx
->refMultipleDDicts
&& dctx
->ddictSet
);
343 DEBUGLOG(4, "Adjusting DDict based on requested dict ID from frame");
345 const ZSTD_DDict
* frameDDict
= ZSTD_DDictHashSet_getDDict(dctx
->ddictSet
, dctx
->fParams
.dictID
);
347 DEBUGLOG(4, "DDict found!");
348 ZSTD_clearDict(dctx
);
349 dctx
->dictID
= dctx
->fParams
.dictID
;
350 dctx
->ddict
= frameDDict
;
351 dctx
->dictUses
= ZSTD_use_indefinitely
;
357 /*-*************************************************************
358 * Frame header decoding
359 ***************************************************************/
362 * Tells if the content of `buffer` starts with a valid Frame Identifier.
363 * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
364 * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
365 * Note 3 : Skippable Frame Identifiers are considered valid. */
366 unsigned ZSTD_isFrame(const void* buffer
, size_t size
)
368 if (size
< ZSTD_FRAMEIDSIZE
) return 0;
369 { U32
const magic
= MEM_readLE32(buffer
);
370 if (magic
== ZSTD_MAGICNUMBER
) return 1;
371 if ((magic
& ZSTD_MAGIC_SKIPPABLE_MASK
) == ZSTD_MAGIC_SKIPPABLE_START
) return 1;
376 /*! ZSTD_isSkippableFrame() :
377 * Tells if the content of `buffer` starts with a valid Frame Identifier for a skippable frame.
378 * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
380 unsigned ZSTD_isSkippableFrame(const void* buffer
, size_t size
)
382 if (size
< ZSTD_FRAMEIDSIZE
) return 0;
383 { U32
const magic
= MEM_readLE32(buffer
);
384 if ((magic
& ZSTD_MAGIC_SKIPPABLE_MASK
) == ZSTD_MAGIC_SKIPPABLE_START
) return 1;
389 /* ZSTD_frameHeaderSize_internal() :
390 * srcSize must be large enough to reach header size fields.
391 * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless.
392 * @return : size of the Frame Header
393 * or an error code, which can be tested with ZSTD_isError() */
394 static size_t ZSTD_frameHeaderSize_internal(const void* src
, size_t srcSize
, ZSTD_format_e format
)
396 size_t const minInputSize
= ZSTD_startingInputLength(format
);
397 RETURN_ERROR_IF(srcSize
< minInputSize
, srcSize_wrong
, "");
399 { BYTE
const fhd
= ((const BYTE
*)src
)[minInputSize
-1];
400 U32
const dictID
= fhd
& 3;
401 U32
const singleSegment
= (fhd
>> 5) & 1;
402 U32
const fcsId
= fhd
>> 6;
403 return minInputSize
+ !singleSegment
404 + ZSTD_did_fieldSize
[dictID
] + ZSTD_fcs_fieldSize
[fcsId
]
405 + (singleSegment
&& !fcsId
);
409 /* ZSTD_frameHeaderSize() :
410 * srcSize must be >= ZSTD_frameHeaderSize_prefix.
411 * @return : size of the Frame Header,
412 * or an error code (if srcSize is too small) */
413 size_t ZSTD_frameHeaderSize(const void* src
, size_t srcSize
)
415 return ZSTD_frameHeaderSize_internal(src
, srcSize
, ZSTD_f_zstd1
);
419 /* ZSTD_getFrameHeader_advanced() :
420 * decode Frame Header, or require larger `srcSize`.
421 * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless
422 * @return : 0, `zfhPtr` is correctly filled,
423 * >0, `srcSize` is too small, value is wanted `srcSize` amount,
424 * or an error code, which can be tested using ZSTD_isError() */
425 size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader
* zfhPtr
, const void* src
, size_t srcSize
, ZSTD_format_e format
)
427 const BYTE
* ip
= (const BYTE
*)src
;
428 size_t const minInputSize
= ZSTD_startingInputLength(format
);
430 ZSTD_memset(zfhPtr
, 0, sizeof(*zfhPtr
)); /* not strictly necessary, but static analyzer do not understand that zfhPtr is only going to be read only if return value is zero, since they are 2 different signals */
431 if (srcSize
< minInputSize
) return minInputSize
;
432 RETURN_ERROR_IF(src
==NULL
, GENERIC
, "invalid parameter");
434 if ( (format
!= ZSTD_f_zstd1_magicless
)
435 && (MEM_readLE32(src
) != ZSTD_MAGICNUMBER
) ) {
436 if ((MEM_readLE32(src
) & ZSTD_MAGIC_SKIPPABLE_MASK
) == ZSTD_MAGIC_SKIPPABLE_START
) {
437 /* skippable frame */
438 if (srcSize
< ZSTD_SKIPPABLEHEADERSIZE
)
439 return ZSTD_SKIPPABLEHEADERSIZE
; /* magic number + frame length */
440 ZSTD_memset(zfhPtr
, 0, sizeof(*zfhPtr
));
441 zfhPtr
->frameContentSize
= MEM_readLE32((const char *)src
+ ZSTD_FRAMEIDSIZE
);
442 zfhPtr
->frameType
= ZSTD_skippableFrame
;
445 RETURN_ERROR(prefix_unknown
, "");
448 /* ensure there is enough `srcSize` to fully read/decode frame header */
449 { size_t const fhsize
= ZSTD_frameHeaderSize_internal(src
, srcSize
, format
);
450 if (srcSize
< fhsize
) return fhsize
;
451 zfhPtr
->headerSize
= (U32
)fhsize
;
454 { BYTE
const fhdByte
= ip
[minInputSize
-1];
455 size_t pos
= minInputSize
;
456 U32
const dictIDSizeCode
= fhdByte
&3;
457 U32
const checksumFlag
= (fhdByte
>>2)&1;
458 U32
const singleSegment
= (fhdByte
>>5)&1;
459 U32
const fcsID
= fhdByte
>>6;
462 U64 frameContentSize
= ZSTD_CONTENTSIZE_UNKNOWN
;
463 RETURN_ERROR_IF((fhdByte
& 0x08) != 0, frameParameter_unsupported
,
464 "reserved bits, must be zero");
466 if (!singleSegment
) {
467 BYTE
const wlByte
= ip
[pos
++];
468 U32
const windowLog
= (wlByte
>> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN
;
469 RETURN_ERROR_IF(windowLog
> ZSTD_WINDOWLOG_MAX
, frameParameter_windowTooLarge
, "");
470 windowSize
= (1ULL << windowLog
);
471 windowSize
+= (windowSize
>> 3) * (wlByte
&7);
473 switch(dictIDSizeCode
)
476 assert(0); /* impossible */
479 case 1 : dictID
= ip
[pos
]; pos
++; break;
480 case 2 : dictID
= MEM_readLE16(ip
+pos
); pos
+=2; break;
481 case 3 : dictID
= MEM_readLE32(ip
+pos
); pos
+=4; break;
486 assert(0); /* impossible */
488 case 0 : if (singleSegment
) frameContentSize
= ip
[pos
]; break;
489 case 1 : frameContentSize
= MEM_readLE16(ip
+pos
)+256; break;
490 case 2 : frameContentSize
= MEM_readLE32(ip
+pos
); break;
491 case 3 : frameContentSize
= MEM_readLE64(ip
+pos
); break;
493 if (singleSegment
) windowSize
= frameContentSize
;
495 zfhPtr
->frameType
= ZSTD_frame
;
496 zfhPtr
->frameContentSize
= frameContentSize
;
497 zfhPtr
->windowSize
= windowSize
;
498 zfhPtr
->blockSizeMax
= (unsigned) MIN(windowSize
, ZSTD_BLOCKSIZE_MAX
);
499 zfhPtr
->dictID
= dictID
;
500 zfhPtr
->checksumFlag
= checksumFlag
;
505 /* ZSTD_getFrameHeader() :
506 * decode Frame Header, or require larger `srcSize`.
507 * note : this function does not consume input, it only reads it.
508 * @return : 0, `zfhPtr` is correctly filled,
509 * >0, `srcSize` is too small, value is wanted `srcSize` amount,
510 * or an error code, which can be tested using ZSTD_isError() */
511 size_t ZSTD_getFrameHeader(ZSTD_frameHeader
* zfhPtr
, const void* src
, size_t srcSize
)
513 return ZSTD_getFrameHeader_advanced(zfhPtr
, src
, srcSize
, ZSTD_f_zstd1
);
516 /* ZSTD_getFrameContentSize() :
517 * compatible with legacy mode
518 * @return : decompressed size of the single frame pointed to be `src` if known, otherwise
519 * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
520 * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */
521 unsigned long long ZSTD_getFrameContentSize(const void *src
, size_t srcSize
)
523 { ZSTD_frameHeader zfh
;
524 if (ZSTD_getFrameHeader(&zfh
, src
, srcSize
) != 0)
525 return ZSTD_CONTENTSIZE_ERROR
;
526 if (zfh
.frameType
== ZSTD_skippableFrame
) {
529 return zfh
.frameContentSize
;
533 static size_t readSkippableFrameSize(void const* src
, size_t srcSize
)
535 size_t const skippableHeaderSize
= ZSTD_SKIPPABLEHEADERSIZE
;
538 RETURN_ERROR_IF(srcSize
< ZSTD_SKIPPABLEHEADERSIZE
, srcSize_wrong
, "");
540 sizeU32
= MEM_readLE32((BYTE
const*)src
+ ZSTD_FRAMEIDSIZE
);
541 RETURN_ERROR_IF((U32
)(sizeU32
+ ZSTD_SKIPPABLEHEADERSIZE
) < sizeU32
,
542 frameParameter_unsupported
, "");
544 size_t const skippableSize
= skippableHeaderSize
+ sizeU32
;
545 RETURN_ERROR_IF(skippableSize
> srcSize
, srcSize_wrong
, "");
546 return skippableSize
;
550 /*! ZSTD_readSkippableFrame() :
551 * Retrieves a zstd skippable frame containing data given by src, and writes it to dst buffer.
553 * The parameter magicVariant will receive the magicVariant that was supplied when the frame was written,
554 * i.e. magicNumber - ZSTD_MAGIC_SKIPPABLE_START. This can be NULL if the caller is not interested
555 * in the magicVariant.
557 * Returns an error if destination buffer is not large enough, or if the frame is not skippable.
559 * @return : number of bytes written or a ZSTD error.
561 ZSTDLIB_API
size_t ZSTD_readSkippableFrame(void* dst
, size_t dstCapacity
, unsigned* magicVariant
,
562 const void* src
, size_t srcSize
)
564 U32
const magicNumber
= MEM_readLE32(src
);
565 size_t skippableFrameSize
= readSkippableFrameSize(src
, srcSize
);
566 size_t skippableContentSize
= skippableFrameSize
- ZSTD_SKIPPABLEHEADERSIZE
;
568 /* check input validity */
569 RETURN_ERROR_IF(!ZSTD_isSkippableFrame(src
, srcSize
), frameParameter_unsupported
, "");
570 RETURN_ERROR_IF(skippableFrameSize
< ZSTD_SKIPPABLEHEADERSIZE
|| skippableFrameSize
> srcSize
, srcSize_wrong
, "");
571 RETURN_ERROR_IF(skippableContentSize
> dstCapacity
, dstSize_tooSmall
, "");
573 /* deliver payload */
574 if (skippableContentSize
> 0 && dst
!= NULL
)
575 ZSTD_memcpy(dst
, (const BYTE
*)src
+ ZSTD_SKIPPABLEHEADERSIZE
, skippableContentSize
);
576 if (magicVariant
!= NULL
)
577 *magicVariant
= magicNumber
- ZSTD_MAGIC_SKIPPABLE_START
;
578 return skippableContentSize
;
581 /* ZSTD_findDecompressedSize() :
582 * compatible with legacy mode
583 * `srcSize` must be the exact length of some number of ZSTD compressed and/or
585 * @return : decompressed size of the frames contained */
586 unsigned long long ZSTD_findDecompressedSize(const void* src
, size_t srcSize
)
588 unsigned long long totalDstSize
= 0;
590 while (srcSize
>= ZSTD_startingInputLength(ZSTD_f_zstd1
)) {
591 U32
const magicNumber
= MEM_readLE32(src
);
593 if ((magicNumber
& ZSTD_MAGIC_SKIPPABLE_MASK
) == ZSTD_MAGIC_SKIPPABLE_START
) {
594 size_t const skippableSize
= readSkippableFrameSize(src
, srcSize
);
595 if (ZSTD_isError(skippableSize
)) {
596 return ZSTD_CONTENTSIZE_ERROR
;
598 assert(skippableSize
<= srcSize
);
600 src
= (const BYTE
*)src
+ skippableSize
;
601 srcSize
-= skippableSize
;
605 { unsigned long long const ret
= ZSTD_getFrameContentSize(src
, srcSize
);
606 if (ret
>= ZSTD_CONTENTSIZE_ERROR
) return ret
;
608 /* check for overflow */
609 if (totalDstSize
+ ret
< totalDstSize
) return ZSTD_CONTENTSIZE_ERROR
;
612 { size_t const frameSrcSize
= ZSTD_findFrameCompressedSize(src
, srcSize
);
613 if (ZSTD_isError(frameSrcSize
)) {
614 return ZSTD_CONTENTSIZE_ERROR
;
617 src
= (const BYTE
*)src
+ frameSrcSize
;
618 srcSize
-= frameSrcSize
;
620 } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
622 if (srcSize
) return ZSTD_CONTENTSIZE_ERROR
;
627 /* ZSTD_getDecompressedSize() :
628 * compatible with legacy mode
629 * @return : decompressed size if known, 0 otherwise
630 note : 0 can mean any of the following :
631 - frame content is empty
632 - decompressed size field is not present in frame header
633 - frame header unknown / not supported
634 - frame header not complete (`srcSize` too small) */
635 unsigned long long ZSTD_getDecompressedSize(const void* src
, size_t srcSize
)
637 unsigned long long const ret
= ZSTD_getFrameContentSize(src
, srcSize
);
638 ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR
< ZSTD_CONTENTSIZE_UNKNOWN
);
639 return (ret
>= ZSTD_CONTENTSIZE_ERROR
) ? 0 : ret
;
643 /* ZSTD_decodeFrameHeader() :
644 * `headerSize` must be the size provided by ZSTD_frameHeaderSize().
645 * If multiple DDict references are enabled, also will choose the correct DDict to use.
646 * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
647 static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx
* dctx
, const void* src
, size_t headerSize
)
649 size_t const result
= ZSTD_getFrameHeader_advanced(&(dctx
->fParams
), src
, headerSize
, dctx
->format
);
650 if (ZSTD_isError(result
)) return result
; /* invalid header */
651 RETURN_ERROR_IF(result
>0, srcSize_wrong
, "headerSize too small");
653 /* Reference DDict requested by frame if dctx references multiple ddicts */
654 if (dctx
->refMultipleDDicts
== ZSTD_rmd_refMultipleDDicts
&& dctx
->ddictSet
) {
655 ZSTD_DCtx_selectFrameDDict(dctx
);
658 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
659 /* Skip the dictID check in fuzzing mode, because it makes the search
662 RETURN_ERROR_IF(dctx
->fParams
.dictID
&& (dctx
->dictID
!= dctx
->fParams
.dictID
),
663 dictionary_wrong
, "");
665 dctx
->validateChecksum
= (dctx
->fParams
.checksumFlag
&& !dctx
->forceIgnoreChecksum
) ? 1 : 0;
666 if (dctx
->validateChecksum
) xxh64_reset(&dctx
->xxhState
, 0);
667 dctx
->processedCSize
+= headerSize
;
671 static ZSTD_frameSizeInfo
ZSTD_errorFrameSizeInfo(size_t ret
)
673 ZSTD_frameSizeInfo frameSizeInfo
;
674 frameSizeInfo
.compressedSize
= ret
;
675 frameSizeInfo
.decompressedBound
= ZSTD_CONTENTSIZE_ERROR
;
676 return frameSizeInfo
;
679 static ZSTD_frameSizeInfo
ZSTD_findFrameSizeInfo(const void* src
, size_t srcSize
)
681 ZSTD_frameSizeInfo frameSizeInfo
;
682 ZSTD_memset(&frameSizeInfo
, 0, sizeof(ZSTD_frameSizeInfo
));
685 if ((srcSize
>= ZSTD_SKIPPABLEHEADERSIZE
)
686 && (MEM_readLE32(src
) & ZSTD_MAGIC_SKIPPABLE_MASK
) == ZSTD_MAGIC_SKIPPABLE_START
) {
687 frameSizeInfo
.compressedSize
= readSkippableFrameSize(src
, srcSize
);
688 assert(ZSTD_isError(frameSizeInfo
.compressedSize
) ||
689 frameSizeInfo
.compressedSize
<= srcSize
);
690 return frameSizeInfo
;
692 const BYTE
* ip
= (const BYTE
*)src
;
693 const BYTE
* const ipstart
= ip
;
694 size_t remainingSize
= srcSize
;
696 ZSTD_frameHeader zfh
;
698 /* Extract Frame Header */
699 { size_t const ret
= ZSTD_getFrameHeader(&zfh
, src
, srcSize
);
700 if (ZSTD_isError(ret
))
701 return ZSTD_errorFrameSizeInfo(ret
);
703 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong
));
706 ip
+= zfh
.headerSize
;
707 remainingSize
-= zfh
.headerSize
;
709 /* Iterate over each block */
711 blockProperties_t blockProperties
;
712 size_t const cBlockSize
= ZSTD_getcBlockSize(ip
, remainingSize
, &blockProperties
);
713 if (ZSTD_isError(cBlockSize
))
714 return ZSTD_errorFrameSizeInfo(cBlockSize
);
716 if (ZSTD_blockHeaderSize
+ cBlockSize
> remainingSize
)
717 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong
));
719 ip
+= ZSTD_blockHeaderSize
+ cBlockSize
;
720 remainingSize
-= ZSTD_blockHeaderSize
+ cBlockSize
;
723 if (blockProperties
.lastBlock
) break;
726 /* Final frame content checksum */
727 if (zfh
.checksumFlag
) {
728 if (remainingSize
< 4)
729 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong
));
733 frameSizeInfo
.compressedSize
= (size_t)(ip
- ipstart
);
734 frameSizeInfo
.decompressedBound
= (zfh
.frameContentSize
!= ZSTD_CONTENTSIZE_UNKNOWN
)
735 ? zfh
.frameContentSize
736 : nbBlocks
* zfh
.blockSizeMax
;
737 return frameSizeInfo
;
741 /* ZSTD_findFrameCompressedSize() :
742 * compatible with legacy mode
743 * `src` must point to the start of a ZSTD frame, ZSTD legacy frame, or skippable frame
744 * `srcSize` must be at least as large as the frame contained
745 * @return : the compressed size of the frame starting at `src` */
746 size_t ZSTD_findFrameCompressedSize(const void *src
, size_t srcSize
)
748 ZSTD_frameSizeInfo
const frameSizeInfo
= ZSTD_findFrameSizeInfo(src
, srcSize
);
749 return frameSizeInfo
.compressedSize
;
752 /* ZSTD_decompressBound() :
753 * compatible with legacy mode
754 * `src` must point to the start of a ZSTD frame or a skippeable frame
755 * `srcSize` must be at least as large as the frame contained
756 * @return : the maximum decompressed size of the compressed source
758 unsigned long long ZSTD_decompressBound(const void* src
, size_t srcSize
)
760 unsigned long long bound
= 0;
761 /* Iterate over each frame */
762 while (srcSize
> 0) {
763 ZSTD_frameSizeInfo
const frameSizeInfo
= ZSTD_findFrameSizeInfo(src
, srcSize
);
764 size_t const compressedSize
= frameSizeInfo
.compressedSize
;
765 unsigned long long const decompressedBound
= frameSizeInfo
.decompressedBound
;
766 if (ZSTD_isError(compressedSize
) || decompressedBound
== ZSTD_CONTENTSIZE_ERROR
)
767 return ZSTD_CONTENTSIZE_ERROR
;
768 assert(srcSize
>= compressedSize
);
769 src
= (const BYTE
*)src
+ compressedSize
;
770 srcSize
-= compressedSize
;
771 bound
+= decompressedBound
;
777 /*-*************************************************************
779 ***************************************************************/
781 /* ZSTD_insertBlock() :
782 * insert `src` block into `dctx` history. Useful to track uncompressed blocks. */
783 size_t ZSTD_insertBlock(ZSTD_DCtx
* dctx
, const void* blockStart
, size_t blockSize
)
785 DEBUGLOG(5, "ZSTD_insertBlock: %u bytes", (unsigned)blockSize
);
786 ZSTD_checkContinuity(dctx
, blockStart
, blockSize
);
787 dctx
->previousDstEnd
= (const char*)blockStart
+ blockSize
;
792 static size_t ZSTD_copyRawBlock(void* dst
, size_t dstCapacity
,
793 const void* src
, size_t srcSize
)
795 DEBUGLOG(5, "ZSTD_copyRawBlock");
796 RETURN_ERROR_IF(srcSize
> dstCapacity
, dstSize_tooSmall
, "");
798 if (srcSize
== 0) return 0;
799 RETURN_ERROR(dstBuffer_null
, "");
801 ZSTD_memmove(dst
, src
, srcSize
);
805 static size_t ZSTD_setRleBlock(void* dst
, size_t dstCapacity
,
809 RETURN_ERROR_IF(regenSize
> dstCapacity
, dstSize_tooSmall
, "");
811 if (regenSize
== 0) return 0;
812 RETURN_ERROR(dstBuffer_null
, "");
814 ZSTD_memset(dst
, b
, regenSize
);
818 static void ZSTD_DCtx_trace_end(ZSTD_DCtx
const* dctx
, U64 uncompressedSize
, U64 compressedSize
, unsigned streaming
)
821 (void)uncompressedSize
;
822 (void)compressedSize
;
827 /*! ZSTD_decompressFrame() :
828 * @dctx must be properly initialized
829 * will update *srcPtr and *srcSizePtr,
830 * to make *srcPtr progress by one frame. */
831 static size_t ZSTD_decompressFrame(ZSTD_DCtx
* dctx
,
832 void* dst
, size_t dstCapacity
,
833 const void** srcPtr
, size_t *srcSizePtr
)
835 const BYTE
* const istart
= (const BYTE
*)(*srcPtr
);
836 const BYTE
* ip
= istart
;
837 BYTE
* const ostart
= (BYTE
*)dst
;
838 BYTE
* const oend
= dstCapacity
!= 0 ? ostart
+ dstCapacity
: ostart
;
840 size_t remainingSrcSize
= *srcSizePtr
;
842 DEBUGLOG(4, "ZSTD_decompressFrame (srcSize:%i)", (int)*srcSizePtr
);
846 remainingSrcSize
< ZSTD_FRAMEHEADERSIZE_MIN(dctx
->format
)+ZSTD_blockHeaderSize
,
850 { size_t const frameHeaderSize
= ZSTD_frameHeaderSize_internal(
851 ip
, ZSTD_FRAMEHEADERSIZE_PREFIX(dctx
->format
), dctx
->format
);
852 if (ZSTD_isError(frameHeaderSize
)) return frameHeaderSize
;
853 RETURN_ERROR_IF(remainingSrcSize
< frameHeaderSize
+ZSTD_blockHeaderSize
,
855 FORWARD_IF_ERROR( ZSTD_decodeFrameHeader(dctx
, ip
, frameHeaderSize
) , "");
856 ip
+= frameHeaderSize
; remainingSrcSize
-= frameHeaderSize
;
859 /* Loop on each block */
861 BYTE
* oBlockEnd
= oend
;
863 blockProperties_t blockProperties
;
864 size_t const cBlockSize
= ZSTD_getcBlockSize(ip
, remainingSrcSize
, &blockProperties
);
865 if (ZSTD_isError(cBlockSize
)) return cBlockSize
;
867 ip
+= ZSTD_blockHeaderSize
;
868 remainingSrcSize
-= ZSTD_blockHeaderSize
;
869 RETURN_ERROR_IF(cBlockSize
> remainingSrcSize
, srcSize_wrong
, "");
871 if (ip
>= op
&& ip
< oBlockEnd
) {
872 /* We are decompressing in-place. Limit the output pointer so that we
873 * don't overwrite the block that we are currently reading. This will
874 * fail decompression if the input & output pointers aren't spaced
877 * This is important to set, even when the pointers are far enough
878 * apart, because ZSTD_decompressBlock_internal() can decide to store
879 * literals in the output buffer, after the block it is decompressing.
880 * Since we don't want anything to overwrite our input, we have to tell
881 * ZSTD_decompressBlock_internal to never write past ip.
883 * See ZSTD_allocateLiteralsBuffer() for reference.
885 oBlockEnd
= op
+ (ip
- op
);
888 switch(blockProperties
.blockType
)
891 decodedSize
= ZSTD_decompressBlock_internal(dctx
, op
, (size_t)(oBlockEnd
-op
), ip
, cBlockSize
, /* frame */ 1, not_streaming
);
894 /* Use oend instead of oBlockEnd because this function is safe to overlap. It uses memmove. */
895 decodedSize
= ZSTD_copyRawBlock(op
, (size_t)(oend
-op
), ip
, cBlockSize
);
898 decodedSize
= ZSTD_setRleBlock(op
, (size_t)(oBlockEnd
-op
), *ip
, blockProperties
.origSize
);
902 RETURN_ERROR(corruption_detected
, "invalid block type");
905 if (ZSTD_isError(decodedSize
)) return decodedSize
;
906 if (dctx
->validateChecksum
)
907 xxh64_update(&dctx
->xxhState
, op
, decodedSize
);
908 if (decodedSize
!= 0)
912 remainingSrcSize
-= cBlockSize
;
913 if (blockProperties
.lastBlock
) break;
916 if (dctx
->fParams
.frameContentSize
!= ZSTD_CONTENTSIZE_UNKNOWN
) {
917 RETURN_ERROR_IF((U64
)(op
-ostart
) != dctx
->fParams
.frameContentSize
,
918 corruption_detected
, "");
920 if (dctx
->fParams
.checksumFlag
) { /* Frame content checksum verification */
921 RETURN_ERROR_IF(remainingSrcSize
<4, checksum_wrong
, "");
922 if (!dctx
->forceIgnoreChecksum
) {
923 U32
const checkCalc
= (U32
)xxh64_digest(&dctx
->xxhState
);
925 checkRead
= MEM_readLE32(ip
);
926 RETURN_ERROR_IF(checkRead
!= checkCalc
, checksum_wrong
, "");
929 remainingSrcSize
-= 4;
931 ZSTD_DCtx_trace_end(dctx
, (U64
)(op
-ostart
), (U64
)(ip
-istart
), /* streaming */ 0);
932 /* Allow caller to get size read */
934 *srcSizePtr
= remainingSrcSize
;
935 return (size_t)(op
-ostart
);
938 static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx
* dctx
,
939 void* dst
, size_t dstCapacity
,
940 const void* src
, size_t srcSize
,
941 const void* dict
, size_t dictSize
,
942 const ZSTD_DDict
* ddict
)
944 void* const dststart
= dst
;
945 int moreThan1Frame
= 0;
947 DEBUGLOG(5, "ZSTD_decompressMultiFrame");
948 assert(dict
==NULL
|| ddict
==NULL
); /* either dict or ddict set, not both */
951 dict
= ZSTD_DDict_dictContent(ddict
);
952 dictSize
= ZSTD_DDict_dictSize(ddict
);
955 while (srcSize
>= ZSTD_startingInputLength(dctx
->format
)) {
958 { U32
const magicNumber
= MEM_readLE32(src
);
959 DEBUGLOG(4, "reading magic number %08X (expecting %08X)",
960 (unsigned)magicNumber
, ZSTD_MAGICNUMBER
);
961 if ((magicNumber
& ZSTD_MAGIC_SKIPPABLE_MASK
) == ZSTD_MAGIC_SKIPPABLE_START
) {
962 size_t const skippableSize
= readSkippableFrameSize(src
, srcSize
);
963 FORWARD_IF_ERROR(skippableSize
, "readSkippableFrameSize failed");
964 assert(skippableSize
<= srcSize
);
966 src
= (const BYTE
*)src
+ skippableSize
;
967 srcSize
-= skippableSize
;
972 /* we were called from ZSTD_decompress_usingDDict */
973 FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(dctx
, ddict
), "");
975 /* this will initialize correctly with no dict if dict == NULL, so
976 * use this in all cases but ddict */
977 FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDict(dctx
, dict
, dictSize
), "");
979 ZSTD_checkContinuity(dctx
, dst
, dstCapacity
);
981 { const size_t res
= ZSTD_decompressFrame(dctx
, dst
, dstCapacity
,
984 (ZSTD_getErrorCode(res
) == ZSTD_error_prefix_unknown
)
985 && (moreThan1Frame
==1),
987 "At least one frame successfully completed, "
988 "but following bytes are garbage: "
989 "it's more likely to be a srcSize error, "
990 "specifying more input bytes than size of frame(s). "
991 "Note: one could be unlucky, it might be a corruption error instead, "
992 "happening right at the place where we expect zstd magic bytes. "
993 "But this is _much_ less likely than a srcSize field error.");
994 if (ZSTD_isError(res
)) return res
;
995 assert(res
<= dstCapacity
);
997 dst
= (BYTE
*)dst
+ res
;
1001 } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
1003 RETURN_ERROR_IF(srcSize
, srcSize_wrong
, "input not entirely consumed");
1005 return (size_t)((BYTE
*)dst
- (BYTE
*)dststart
);
1008 size_t ZSTD_decompress_usingDict(ZSTD_DCtx
* dctx
,
1009 void* dst
, size_t dstCapacity
,
1010 const void* src
, size_t srcSize
,
1011 const void* dict
, size_t dictSize
)
1013 return ZSTD_decompressMultiFrame(dctx
, dst
, dstCapacity
, src
, srcSize
, dict
, dictSize
, NULL
);
1017 static ZSTD_DDict
const* ZSTD_getDDict(ZSTD_DCtx
* dctx
)
1019 switch (dctx
->dictUses
) {
1021 assert(0 /* Impossible */);
1024 ZSTD_clearDict(dctx
);
1026 case ZSTD_use_indefinitely
:
1029 dctx
->dictUses
= ZSTD_dont_use
;
1034 size_t ZSTD_decompressDCtx(ZSTD_DCtx
* dctx
, void* dst
, size_t dstCapacity
, const void* src
, size_t srcSize
)
1036 return ZSTD_decompress_usingDDict(dctx
, dst
, dstCapacity
, src
, srcSize
, ZSTD_getDDict(dctx
));
1040 size_t ZSTD_decompress(void* dst
, size_t dstCapacity
, const void* src
, size_t srcSize
)
1042 #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1)
1044 ZSTD_DCtx
* const dctx
= ZSTD_createDCtx_internal(ZSTD_defaultCMem
);
1045 RETURN_ERROR_IF(dctx
==NULL
, memory_allocation
, "NULL pointer!");
1046 regenSize
= ZSTD_decompressDCtx(dctx
, dst
, dstCapacity
, src
, srcSize
);
1047 ZSTD_freeDCtx(dctx
);
1049 #else /* stack mode */
1051 ZSTD_initDCtx_internal(&dctx
);
1052 return ZSTD_decompressDCtx(&dctx
, dst
, dstCapacity
, src
, srcSize
);
1057 /*-**************************************
1058 * Advanced Streaming Decompression API
1059 * Bufferless and synchronous
1060 ****************************************/
1061 size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx
* dctx
) { return dctx
->expected
; }
1064 * Similar to ZSTD_nextSrcSizeToDecompress(), but when a block input can be streamed,
1065 * we allow taking a partial block as the input. Currently only raw uncompressed blocks can
1068 * For blocks that can be streamed, this allows us to reduce the latency until we produce
1069 * output, and avoid copying the input.
1071 * @param inputSize - The total amount of input that the caller currently has.
1073 static size_t ZSTD_nextSrcSizeToDecompressWithInputSize(ZSTD_DCtx
* dctx
, size_t inputSize
) {
1074 if (!(dctx
->stage
== ZSTDds_decompressBlock
|| dctx
->stage
== ZSTDds_decompressLastBlock
))
1075 return dctx
->expected
;
1076 if (dctx
->bType
!= bt_raw
)
1077 return dctx
->expected
;
1078 return BOUNDED(1, inputSize
, dctx
->expected
);
1081 ZSTD_nextInputType_e
ZSTD_nextInputType(ZSTD_DCtx
* dctx
) {
1084 default: /* should not happen */
1087 case ZSTDds_getFrameHeaderSize
:
1089 case ZSTDds_decodeFrameHeader
:
1090 return ZSTDnit_frameHeader
;
1091 case ZSTDds_decodeBlockHeader
:
1092 return ZSTDnit_blockHeader
;
1093 case ZSTDds_decompressBlock
:
1094 return ZSTDnit_block
;
1095 case ZSTDds_decompressLastBlock
:
1096 return ZSTDnit_lastBlock
;
1097 case ZSTDds_checkChecksum
:
1098 return ZSTDnit_checksum
;
1099 case ZSTDds_decodeSkippableHeader
:
1101 case ZSTDds_skipFrame
:
1102 return ZSTDnit_skippableFrame
;
1106 static int ZSTD_isSkipFrame(ZSTD_DCtx
* dctx
) { return dctx
->stage
== ZSTDds_skipFrame
; }
1108 /* ZSTD_decompressContinue() :
1109 * srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress())
1110 * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)
1111 * or an error code, which can be tested using ZSTD_isError() */
1112 size_t ZSTD_decompressContinue(ZSTD_DCtx
* dctx
, void* dst
, size_t dstCapacity
, const void* src
, size_t srcSize
)
1114 DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (unsigned)srcSize
);
1116 RETURN_ERROR_IF(srcSize
!= ZSTD_nextSrcSizeToDecompressWithInputSize(dctx
, srcSize
), srcSize_wrong
, "not allowed");
1117 ZSTD_checkContinuity(dctx
, dst
, dstCapacity
);
1119 dctx
->processedCSize
+= srcSize
;
1121 switch (dctx
->stage
)
1123 case ZSTDds_getFrameHeaderSize
:
1124 assert(src
!= NULL
);
1125 if (dctx
->format
== ZSTD_f_zstd1
) { /* allows header */
1126 assert(srcSize
>= ZSTD_FRAMEIDSIZE
); /* to read skippable magic number */
1127 if ((MEM_readLE32(src
) & ZSTD_MAGIC_SKIPPABLE_MASK
) == ZSTD_MAGIC_SKIPPABLE_START
) { /* skippable frame */
1128 ZSTD_memcpy(dctx
->headerBuffer
, src
, srcSize
);
1129 dctx
->expected
= ZSTD_SKIPPABLEHEADERSIZE
- srcSize
; /* remaining to load to get full skippable frame header */
1130 dctx
->stage
= ZSTDds_decodeSkippableHeader
;
1133 dctx
->headerSize
= ZSTD_frameHeaderSize_internal(src
, srcSize
, dctx
->format
);
1134 if (ZSTD_isError(dctx
->headerSize
)) return dctx
->headerSize
;
1135 ZSTD_memcpy(dctx
->headerBuffer
, src
, srcSize
);
1136 dctx
->expected
= dctx
->headerSize
- srcSize
;
1137 dctx
->stage
= ZSTDds_decodeFrameHeader
;
1140 case ZSTDds_decodeFrameHeader
:
1141 assert(src
!= NULL
);
1142 ZSTD_memcpy(dctx
->headerBuffer
+ (dctx
->headerSize
- srcSize
), src
, srcSize
);
1143 FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(dctx
, dctx
->headerBuffer
, dctx
->headerSize
), "");
1144 dctx
->expected
= ZSTD_blockHeaderSize
;
1145 dctx
->stage
= ZSTDds_decodeBlockHeader
;
1148 case ZSTDds_decodeBlockHeader
:
1149 { blockProperties_t bp
;
1150 size_t const cBlockSize
= ZSTD_getcBlockSize(src
, ZSTD_blockHeaderSize
, &bp
);
1151 if (ZSTD_isError(cBlockSize
)) return cBlockSize
;
1152 RETURN_ERROR_IF(cBlockSize
> dctx
->fParams
.blockSizeMax
, corruption_detected
, "Block Size Exceeds Maximum");
1153 dctx
->expected
= cBlockSize
;
1154 dctx
->bType
= bp
.blockType
;
1155 dctx
->rleSize
= bp
.origSize
;
1157 dctx
->stage
= bp
.lastBlock
? ZSTDds_decompressLastBlock
: ZSTDds_decompressBlock
;
1162 if (dctx
->fParams
.checksumFlag
) {
1164 dctx
->stage
= ZSTDds_checkChecksum
;
1166 dctx
->expected
= 0; /* end of frame */
1167 dctx
->stage
= ZSTDds_getFrameHeaderSize
;
1170 dctx
->expected
= ZSTD_blockHeaderSize
; /* jump to next header */
1171 dctx
->stage
= ZSTDds_decodeBlockHeader
;
1176 case ZSTDds_decompressLastBlock
:
1177 case ZSTDds_decompressBlock
:
1178 DEBUGLOG(5, "ZSTD_decompressContinue: case ZSTDds_decompressBlock");
1183 DEBUGLOG(5, "ZSTD_decompressContinue: case bt_compressed");
1184 rSize
= ZSTD_decompressBlock_internal(dctx
, dst
, dstCapacity
, src
, srcSize
, /* frame */ 1, is_streaming
);
1185 dctx
->expected
= 0; /* Streaming not supported */
1188 assert(srcSize
<= dctx
->expected
);
1189 rSize
= ZSTD_copyRawBlock(dst
, dstCapacity
, src
, srcSize
);
1190 FORWARD_IF_ERROR(rSize
, "ZSTD_copyRawBlock failed");
1191 assert(rSize
== srcSize
);
1192 dctx
->expected
-= rSize
;
1195 rSize
= ZSTD_setRleBlock(dst
, dstCapacity
, *(const BYTE
*)src
, dctx
->rleSize
);
1196 dctx
->expected
= 0; /* Streaming not supported */
1198 case bt_reserved
: /* should never happen */
1200 RETURN_ERROR(corruption_detected
, "invalid block type");
1202 FORWARD_IF_ERROR(rSize
, "");
1203 RETURN_ERROR_IF(rSize
> dctx
->fParams
.blockSizeMax
, corruption_detected
, "Decompressed Block Size Exceeds Maximum");
1204 DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (unsigned)rSize
);
1205 dctx
->decodedSize
+= rSize
;
1206 if (dctx
->validateChecksum
) xxh64_update(&dctx
->xxhState
, dst
, rSize
);
1207 dctx
->previousDstEnd
= (char*)dst
+ rSize
;
1209 /* Stay on the same stage until we are finished streaming the block. */
1210 if (dctx
->expected
> 0) {
1214 if (dctx
->stage
== ZSTDds_decompressLastBlock
) { /* end of frame */
1215 DEBUGLOG(4, "ZSTD_decompressContinue: decoded size from frame : %u", (unsigned)dctx
->decodedSize
);
1217 dctx
->fParams
.frameContentSize
!= ZSTD_CONTENTSIZE_UNKNOWN
1218 && dctx
->decodedSize
!= dctx
->fParams
.frameContentSize
,
1219 corruption_detected
, "");
1220 if (dctx
->fParams
.checksumFlag
) { /* another round for frame checksum */
1222 dctx
->stage
= ZSTDds_checkChecksum
;
1224 ZSTD_DCtx_trace_end(dctx
, dctx
->decodedSize
, dctx
->processedCSize
, /* streaming */ 1);
1225 dctx
->expected
= 0; /* ends here */
1226 dctx
->stage
= ZSTDds_getFrameHeaderSize
;
1229 dctx
->stage
= ZSTDds_decodeBlockHeader
;
1230 dctx
->expected
= ZSTD_blockHeaderSize
;
1235 case ZSTDds_checkChecksum
:
1236 assert(srcSize
== 4); /* guaranteed by dctx->expected */
1238 if (dctx
->validateChecksum
) {
1239 U32
const h32
= (U32
)xxh64_digest(&dctx
->xxhState
);
1240 U32
const check32
= MEM_readLE32(src
);
1241 DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", (unsigned)h32
, (unsigned)check32
);
1242 RETURN_ERROR_IF(check32
!= h32
, checksum_wrong
, "");
1244 ZSTD_DCtx_trace_end(dctx
, dctx
->decodedSize
, dctx
->processedCSize
, /* streaming */ 1);
1246 dctx
->stage
= ZSTDds_getFrameHeaderSize
;
1250 case ZSTDds_decodeSkippableHeader
:
1251 assert(src
!= NULL
);
1252 assert(srcSize
<= ZSTD_SKIPPABLEHEADERSIZE
);
1253 ZSTD_memcpy(dctx
->headerBuffer
+ (ZSTD_SKIPPABLEHEADERSIZE
- srcSize
), src
, srcSize
); /* complete skippable header */
1254 dctx
->expected
= MEM_readLE32(dctx
->headerBuffer
+ ZSTD_FRAMEIDSIZE
); /* note : dctx->expected can grow seriously large, beyond local buffer size */
1255 dctx
->stage
= ZSTDds_skipFrame
;
1258 case ZSTDds_skipFrame
:
1260 dctx
->stage
= ZSTDds_getFrameHeaderSize
;
1264 assert(0); /* impossible */
1265 RETURN_ERROR(GENERIC
, "impossible to reach"); /* some compiler require default to do something */
1270 static size_t ZSTD_refDictContent(ZSTD_DCtx
* dctx
, const void* dict
, size_t dictSize
)
1272 dctx
->dictEnd
= dctx
->previousDstEnd
;
1273 dctx
->virtualStart
= (const char*)dict
- ((const char*)(dctx
->previousDstEnd
) - (const char*)(dctx
->prefixStart
));
1274 dctx
->prefixStart
= dict
;
1275 dctx
->previousDstEnd
= (const char*)dict
+ dictSize
;
1276 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1277 dctx
->dictContentBeginForFuzzing
= dctx
->prefixStart
;
1278 dctx
->dictContentEndForFuzzing
= dctx
->previousDstEnd
;
1283 /*! ZSTD_loadDEntropy() :
1284 * dict : must point at beginning of a valid zstd dictionary.
1285 * @return : size of entropy tables read */
1287 ZSTD_loadDEntropy(ZSTD_entropyDTables_t
* entropy
,
1288 const void* const dict
, size_t const dictSize
)
1290 const BYTE
* dictPtr
= (const BYTE
*)dict
;
1291 const BYTE
* const dictEnd
= dictPtr
+ dictSize
;
1293 RETURN_ERROR_IF(dictSize
<= 8, dictionary_corrupted
, "dict is too small");
1294 assert(MEM_readLE32(dict
) == ZSTD_MAGIC_DICTIONARY
); /* dict must be valid */
1295 dictPtr
+= 8; /* skip header = magic + dictID */
1297 ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t
, OFTable
) == offsetof(ZSTD_entropyDTables_t
, LLTable
) + sizeof(entropy
->LLTable
));
1298 ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t
, MLTable
) == offsetof(ZSTD_entropyDTables_t
, OFTable
) + sizeof(entropy
->OFTable
));
1299 ZSTD_STATIC_ASSERT(sizeof(entropy
->LLTable
) + sizeof(entropy
->OFTable
) + sizeof(entropy
->MLTable
) >= HUF_DECOMPRESS_WORKSPACE_SIZE
);
1300 { void* const workspace
= &entropy
->LLTable
; /* use fse tables as temporary workspace; implies fse tables are grouped together */
1301 size_t const workspaceSize
= sizeof(entropy
->LLTable
) + sizeof(entropy
->OFTable
) + sizeof(entropy
->MLTable
);
1302 #ifdef HUF_FORCE_DECOMPRESS_X1
1303 /* in minimal huffman, we always use X1 variants */
1304 size_t const hSize
= HUF_readDTableX1_wksp(entropy
->hufTable
,
1305 dictPtr
, dictEnd
- dictPtr
,
1306 workspace
, workspaceSize
);
1308 size_t const hSize
= HUF_readDTableX2_wksp(entropy
->hufTable
,
1309 dictPtr
, (size_t)(dictEnd
- dictPtr
),
1310 workspace
, workspaceSize
);
1312 RETURN_ERROR_IF(HUF_isError(hSize
), dictionary_corrupted
, "");
1316 { short offcodeNCount
[MaxOff
+1];
1317 unsigned offcodeMaxValue
= MaxOff
, offcodeLog
;
1318 size_t const offcodeHeaderSize
= FSE_readNCount(offcodeNCount
, &offcodeMaxValue
, &offcodeLog
, dictPtr
, (size_t)(dictEnd
-dictPtr
));
1319 RETURN_ERROR_IF(FSE_isError(offcodeHeaderSize
), dictionary_corrupted
, "");
1320 RETURN_ERROR_IF(offcodeMaxValue
> MaxOff
, dictionary_corrupted
, "");
1321 RETURN_ERROR_IF(offcodeLog
> OffFSELog
, dictionary_corrupted
, "");
1322 ZSTD_buildFSETable( entropy
->OFTable
,
1323 offcodeNCount
, offcodeMaxValue
,
1326 entropy
->workspace
, sizeof(entropy
->workspace
),
1328 dictPtr
+= offcodeHeaderSize
;
1331 { short matchlengthNCount
[MaxML
+1];
1332 unsigned matchlengthMaxValue
= MaxML
, matchlengthLog
;
1333 size_t const matchlengthHeaderSize
= FSE_readNCount(matchlengthNCount
, &matchlengthMaxValue
, &matchlengthLog
, dictPtr
, (size_t)(dictEnd
-dictPtr
));
1334 RETURN_ERROR_IF(FSE_isError(matchlengthHeaderSize
), dictionary_corrupted
, "");
1335 RETURN_ERROR_IF(matchlengthMaxValue
> MaxML
, dictionary_corrupted
, "");
1336 RETURN_ERROR_IF(matchlengthLog
> MLFSELog
, dictionary_corrupted
, "");
1337 ZSTD_buildFSETable( entropy
->MLTable
,
1338 matchlengthNCount
, matchlengthMaxValue
,
1341 entropy
->workspace
, sizeof(entropy
->workspace
),
1343 dictPtr
+= matchlengthHeaderSize
;
1346 { short litlengthNCount
[MaxLL
+1];
1347 unsigned litlengthMaxValue
= MaxLL
, litlengthLog
;
1348 size_t const litlengthHeaderSize
= FSE_readNCount(litlengthNCount
, &litlengthMaxValue
, &litlengthLog
, dictPtr
, (size_t)(dictEnd
-dictPtr
));
1349 RETURN_ERROR_IF(FSE_isError(litlengthHeaderSize
), dictionary_corrupted
, "");
1350 RETURN_ERROR_IF(litlengthMaxValue
> MaxLL
, dictionary_corrupted
, "");
1351 RETURN_ERROR_IF(litlengthLog
> LLFSELog
, dictionary_corrupted
, "");
1352 ZSTD_buildFSETable( entropy
->LLTable
,
1353 litlengthNCount
, litlengthMaxValue
,
1356 entropy
->workspace
, sizeof(entropy
->workspace
),
1358 dictPtr
+= litlengthHeaderSize
;
1361 RETURN_ERROR_IF(dictPtr
+12 > dictEnd
, dictionary_corrupted
, "");
1363 size_t const dictContentSize
= (size_t)(dictEnd
- (dictPtr
+12));
1364 for (i
=0; i
<3; i
++) {
1365 U32
const rep
= MEM_readLE32(dictPtr
); dictPtr
+= 4;
1366 RETURN_ERROR_IF(rep
==0 || rep
> dictContentSize
,
1367 dictionary_corrupted
, "");
1368 entropy
->rep
[i
] = rep
;
1371 return (size_t)(dictPtr
- (const BYTE
*)dict
);
1374 static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx
* dctx
, const void* dict
, size_t dictSize
)
1376 if (dictSize
< 8) return ZSTD_refDictContent(dctx
, dict
, dictSize
);
1377 { U32
const magic
= MEM_readLE32(dict
);
1378 if (magic
!= ZSTD_MAGIC_DICTIONARY
) {
1379 return ZSTD_refDictContent(dctx
, dict
, dictSize
); /* pure content mode */
1381 dctx
->dictID
= MEM_readLE32((const char*)dict
+ ZSTD_FRAMEIDSIZE
);
1383 /* load entropy tables */
1384 { size_t const eSize
= ZSTD_loadDEntropy(&dctx
->entropy
, dict
, dictSize
);
1385 RETURN_ERROR_IF(ZSTD_isError(eSize
), dictionary_corrupted
, "");
1386 dict
= (const char*)dict
+ eSize
;
1389 dctx
->litEntropy
= dctx
->fseEntropy
= 1;
1391 /* reference dictionary content */
1392 return ZSTD_refDictContent(dctx
, dict
, dictSize
);
1395 size_t ZSTD_decompressBegin(ZSTD_DCtx
* dctx
)
1397 assert(dctx
!= NULL
);
1398 dctx
->expected
= ZSTD_startingInputLength(dctx
->format
); /* dctx->format must be properly set */
1399 dctx
->stage
= ZSTDds_getFrameHeaderSize
;
1400 dctx
->processedCSize
= 0;
1401 dctx
->decodedSize
= 0;
1402 dctx
->previousDstEnd
= NULL
;
1403 dctx
->prefixStart
= NULL
;
1404 dctx
->virtualStart
= NULL
;
1405 dctx
->dictEnd
= NULL
;
1406 dctx
->entropy
.hufTable
[0] = (HUF_DTable
)((HufLog
)*0x1000001); /* cover both little and big endian */
1407 dctx
->litEntropy
= dctx
->fseEntropy
= 0;
1409 dctx
->bType
= bt_reserved
;
1410 ZSTD_STATIC_ASSERT(sizeof(dctx
->entropy
.rep
) == sizeof(repStartValue
));
1411 ZSTD_memcpy(dctx
->entropy
.rep
, repStartValue
, sizeof(repStartValue
)); /* initial repcodes */
1412 dctx
->LLTptr
= dctx
->entropy
.LLTable
;
1413 dctx
->MLTptr
= dctx
->entropy
.MLTable
;
1414 dctx
->OFTptr
= dctx
->entropy
.OFTable
;
1415 dctx
->HUFptr
= dctx
->entropy
.hufTable
;
1419 size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx
* dctx
, const void* dict
, size_t dictSize
)
1421 FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx
) , "");
1422 if (dict
&& dictSize
)
1424 ZSTD_isError(ZSTD_decompress_insertDictionary(dctx
, dict
, dictSize
)),
1425 dictionary_corrupted
, "");
1430 /* ====== ZSTD_DDict ====== */
1432 size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx
* dctx
, const ZSTD_DDict
* ddict
)
1434 DEBUGLOG(4, "ZSTD_decompressBegin_usingDDict");
1435 assert(dctx
!= NULL
);
1437 const char* const dictStart
= (const char*)ZSTD_DDict_dictContent(ddict
);
1438 size_t const dictSize
= ZSTD_DDict_dictSize(ddict
);
1439 const void* const dictEnd
= dictStart
+ dictSize
;
1440 dctx
->ddictIsCold
= (dctx
->dictEnd
!= dictEnd
);
1441 DEBUGLOG(4, "DDict is %s",
1442 dctx
->ddictIsCold
? "~cold~" : "hot!");
1444 FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx
) , "");
1445 if (ddict
) { /* NULL ddict is equivalent to no dictionary */
1446 ZSTD_copyDDictParameters(dctx
, ddict
);
1451 /*! ZSTD_getDictID_fromDict() :
1452 * Provides the dictID stored within dictionary.
1453 * if @return == 0, the dictionary is not conformant with Zstandard specification.
1454 * It can still be loaded, but as a content-only dictionary. */
1455 unsigned ZSTD_getDictID_fromDict(const void* dict
, size_t dictSize
)
1457 if (dictSize
< 8) return 0;
1458 if (MEM_readLE32(dict
) != ZSTD_MAGIC_DICTIONARY
) return 0;
1459 return MEM_readLE32((const char*)dict
+ ZSTD_FRAMEIDSIZE
);
1462 /*! ZSTD_getDictID_fromFrame() :
1463 * Provides the dictID required to decompress frame stored within `src`.
1464 * If @return == 0, the dictID could not be decoded.
1465 * This could for one of the following reasons :
1466 * - The frame does not require a dictionary (most common case).
1467 * - The frame was built with dictID intentionally removed.
1468 * Needed dictionary is a hidden information.
1469 * Note : this use case also happens when using a non-conformant dictionary.
1470 * - `srcSize` is too small, and as a result, frame header could not be decoded.
1471 * Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`.
1472 * - This is not a Zstandard frame.
1473 * When identifying the exact failure cause, it's possible to use
1474 * ZSTD_getFrameHeader(), which will provide a more precise error code. */
1475 unsigned ZSTD_getDictID_fromFrame(const void* src
, size_t srcSize
)
1477 ZSTD_frameHeader zfp
= { 0, 0, 0, ZSTD_frame
, 0, 0, 0 };
1478 size_t const hError
= ZSTD_getFrameHeader(&zfp
, src
, srcSize
);
1479 if (ZSTD_isError(hError
)) return 0;
1484 /*! ZSTD_decompress_usingDDict() :
1485 * Decompression using a pre-digested Dictionary
1486 * Use dictionary without significant overhead. */
1487 size_t ZSTD_decompress_usingDDict(ZSTD_DCtx
* dctx
,
1488 void* dst
, size_t dstCapacity
,
1489 const void* src
, size_t srcSize
,
1490 const ZSTD_DDict
* ddict
)
1492 /* pass content and size in case legacy frames are encountered */
1493 return ZSTD_decompressMultiFrame(dctx
, dst
, dstCapacity
, src
, srcSize
,
1499 /*=====================================
1500 * Streaming decompression
1501 *====================================*/
1503 ZSTD_DStream
* ZSTD_createDStream(void)
1505 DEBUGLOG(3, "ZSTD_createDStream");
1506 return ZSTD_createDCtx_internal(ZSTD_defaultCMem
);
1509 ZSTD_DStream
* ZSTD_initStaticDStream(void *workspace
, size_t workspaceSize
)
1511 return ZSTD_initStaticDCtx(workspace
, workspaceSize
);
1514 ZSTD_DStream
* ZSTD_createDStream_advanced(ZSTD_customMem customMem
)
1516 return ZSTD_createDCtx_internal(customMem
);
1519 size_t ZSTD_freeDStream(ZSTD_DStream
* zds
)
1521 return ZSTD_freeDCtx(zds
);
1525 /* *** Initialization *** */
1527 size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX
+ ZSTD_blockHeaderSize
; }
1528 size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX
; }
1530 size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx
* dctx
,
1531 const void* dict
, size_t dictSize
,
1532 ZSTD_dictLoadMethod_e dictLoadMethod
,
1533 ZSTD_dictContentType_e dictContentType
)
1535 RETURN_ERROR_IF(dctx
->streamStage
!= zdss_init
, stage_wrong
, "");
1536 ZSTD_clearDict(dctx
);
1537 if (dict
&& dictSize
!= 0) {
1538 dctx
->ddictLocal
= ZSTD_createDDict_advanced(dict
, dictSize
, dictLoadMethod
, dictContentType
, dctx
->customMem
);
1539 RETURN_ERROR_IF(dctx
->ddictLocal
== NULL
, memory_allocation
, "NULL pointer!");
1540 dctx
->ddict
= dctx
->ddictLocal
;
1541 dctx
->dictUses
= ZSTD_use_indefinitely
;
1546 size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx
* dctx
, const void* dict
, size_t dictSize
)
1548 return ZSTD_DCtx_loadDictionary_advanced(dctx
, dict
, dictSize
, ZSTD_dlm_byRef
, ZSTD_dct_auto
);
1551 size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx
* dctx
, const void* dict
, size_t dictSize
)
1553 return ZSTD_DCtx_loadDictionary_advanced(dctx
, dict
, dictSize
, ZSTD_dlm_byCopy
, ZSTD_dct_auto
);
1556 size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx
* dctx
, const void* prefix
, size_t prefixSize
, ZSTD_dictContentType_e dictContentType
)
1558 FORWARD_IF_ERROR(ZSTD_DCtx_loadDictionary_advanced(dctx
, prefix
, prefixSize
, ZSTD_dlm_byRef
, dictContentType
), "");
1559 dctx
->dictUses
= ZSTD_use_once
;
1563 size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx
* dctx
, const void* prefix
, size_t prefixSize
)
1565 return ZSTD_DCtx_refPrefix_advanced(dctx
, prefix
, prefixSize
, ZSTD_dct_rawContent
);
1569 /* ZSTD_initDStream_usingDict() :
1570 * return : expected size, aka ZSTD_startingInputLength().
1571 * this function cannot fail */
1572 size_t ZSTD_initDStream_usingDict(ZSTD_DStream
* zds
, const void* dict
, size_t dictSize
)
1574 DEBUGLOG(4, "ZSTD_initDStream_usingDict");
1575 FORWARD_IF_ERROR( ZSTD_DCtx_reset(zds
, ZSTD_reset_session_only
) , "");
1576 FORWARD_IF_ERROR( ZSTD_DCtx_loadDictionary(zds
, dict
, dictSize
) , "");
1577 return ZSTD_startingInputLength(zds
->format
);
1580 /* note : this variant can't fail */
1581 size_t ZSTD_initDStream(ZSTD_DStream
* zds
)
1583 DEBUGLOG(4, "ZSTD_initDStream");
1584 return ZSTD_initDStream_usingDDict(zds
, NULL
);
1587 /* ZSTD_initDStream_usingDDict() :
1588 * ddict will just be referenced, and must outlive decompression session
1589 * this function cannot fail */
1590 size_t ZSTD_initDStream_usingDDict(ZSTD_DStream
* dctx
, const ZSTD_DDict
* ddict
)
1592 FORWARD_IF_ERROR( ZSTD_DCtx_reset(dctx
, ZSTD_reset_session_only
) , "");
1593 FORWARD_IF_ERROR( ZSTD_DCtx_refDDict(dctx
, ddict
) , "");
1594 return ZSTD_startingInputLength(dctx
->format
);
1597 /* ZSTD_resetDStream() :
1598 * return : expected size, aka ZSTD_startingInputLength().
1599 * this function cannot fail */
1600 size_t ZSTD_resetDStream(ZSTD_DStream
* dctx
)
1602 FORWARD_IF_ERROR(ZSTD_DCtx_reset(dctx
, ZSTD_reset_session_only
), "");
1603 return ZSTD_startingInputLength(dctx
->format
);
1607 size_t ZSTD_DCtx_refDDict(ZSTD_DCtx
* dctx
, const ZSTD_DDict
* ddict
)
1609 RETURN_ERROR_IF(dctx
->streamStage
!= zdss_init
, stage_wrong
, "");
1610 ZSTD_clearDict(dctx
);
1612 dctx
->ddict
= ddict
;
1613 dctx
->dictUses
= ZSTD_use_indefinitely
;
1614 if (dctx
->refMultipleDDicts
== ZSTD_rmd_refMultipleDDicts
) {
1615 if (dctx
->ddictSet
== NULL
) {
1616 dctx
->ddictSet
= ZSTD_createDDictHashSet(dctx
->customMem
);
1617 if (!dctx
->ddictSet
) {
1618 RETURN_ERROR(memory_allocation
, "Failed to allocate memory for hash set!");
1621 assert(!dctx
->staticSize
); /* Impossible: ddictSet cannot have been allocated if static dctx */
1622 FORWARD_IF_ERROR(ZSTD_DDictHashSet_addDDict(dctx
->ddictSet
, ddict
, dctx
->customMem
), "");
1628 /* ZSTD_DCtx_setMaxWindowSize() :
1629 * note : no direct equivalence in ZSTD_DCtx_setParameter,
1630 * since this version sets windowSize, and the other sets windowLog */
1631 size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx
* dctx
, size_t maxWindowSize
)
1633 ZSTD_bounds
const bounds
= ZSTD_dParam_getBounds(ZSTD_d_windowLogMax
);
1634 size_t const min
= (size_t)1 << bounds
.lowerBound
;
1635 size_t const max
= (size_t)1 << bounds
.upperBound
;
1636 RETURN_ERROR_IF(dctx
->streamStage
!= zdss_init
, stage_wrong
, "");
1637 RETURN_ERROR_IF(maxWindowSize
< min
, parameter_outOfBound
, "");
1638 RETURN_ERROR_IF(maxWindowSize
> max
, parameter_outOfBound
, "");
1639 dctx
->maxWindowSize
= maxWindowSize
;
1643 size_t ZSTD_DCtx_setFormat(ZSTD_DCtx
* dctx
, ZSTD_format_e format
)
1645 return ZSTD_DCtx_setParameter(dctx
, ZSTD_d_format
, (int)format
);
1648 ZSTD_bounds
ZSTD_dParam_getBounds(ZSTD_dParameter dParam
)
1650 ZSTD_bounds bounds
= { 0, 0, 0 };
1652 case ZSTD_d_windowLogMax
:
1653 bounds
.lowerBound
= ZSTD_WINDOWLOG_ABSOLUTEMIN
;
1654 bounds
.upperBound
= ZSTD_WINDOWLOG_MAX
;
1657 bounds
.lowerBound
= (int)ZSTD_f_zstd1
;
1658 bounds
.upperBound
= (int)ZSTD_f_zstd1_magicless
;
1659 ZSTD_STATIC_ASSERT(ZSTD_f_zstd1
< ZSTD_f_zstd1_magicless
);
1661 case ZSTD_d_stableOutBuffer
:
1662 bounds
.lowerBound
= (int)ZSTD_bm_buffered
;
1663 bounds
.upperBound
= (int)ZSTD_bm_stable
;
1665 case ZSTD_d_forceIgnoreChecksum
:
1666 bounds
.lowerBound
= (int)ZSTD_d_validateChecksum
;
1667 bounds
.upperBound
= (int)ZSTD_d_ignoreChecksum
;
1669 case ZSTD_d_refMultipleDDicts
:
1670 bounds
.lowerBound
= (int)ZSTD_rmd_refSingleDDict
;
1671 bounds
.upperBound
= (int)ZSTD_rmd_refMultipleDDicts
;
1675 bounds
.error
= ERROR(parameter_unsupported
);
1679 /* ZSTD_dParam_withinBounds:
1680 * @return 1 if value is within dParam bounds,
1682 static int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam
, int value
)
1684 ZSTD_bounds
const bounds
= ZSTD_dParam_getBounds(dParam
);
1685 if (ZSTD_isError(bounds
.error
)) return 0;
1686 if (value
< bounds
.lowerBound
) return 0;
1687 if (value
> bounds
.upperBound
) return 0;
1691 #define CHECK_DBOUNDS(p,v) { \
1692 RETURN_ERROR_IF(!ZSTD_dParam_withinBounds(p, v), parameter_outOfBound, ""); \
1695 size_t ZSTD_DCtx_getParameter(ZSTD_DCtx
* dctx
, ZSTD_dParameter param
, int* value
)
1698 case ZSTD_d_windowLogMax
:
1699 *value
= (int)ZSTD_highbit32((U32
)dctx
->maxWindowSize
);
1702 *value
= (int)dctx
->format
;
1704 case ZSTD_d_stableOutBuffer
:
1705 *value
= (int)dctx
->outBufferMode
;
1707 case ZSTD_d_forceIgnoreChecksum
:
1708 *value
= (int)dctx
->forceIgnoreChecksum
;
1710 case ZSTD_d_refMultipleDDicts
:
1711 *value
= (int)dctx
->refMultipleDDicts
;
1715 RETURN_ERROR(parameter_unsupported
, "");
1718 size_t ZSTD_DCtx_setParameter(ZSTD_DCtx
* dctx
, ZSTD_dParameter dParam
, int value
)
1720 RETURN_ERROR_IF(dctx
->streamStage
!= zdss_init
, stage_wrong
, "");
1722 case ZSTD_d_windowLogMax
:
1723 if (value
== 0) value
= ZSTD_WINDOWLOG_LIMIT_DEFAULT
;
1724 CHECK_DBOUNDS(ZSTD_d_windowLogMax
, value
);
1725 dctx
->maxWindowSize
= ((size_t)1) << value
;
1728 CHECK_DBOUNDS(ZSTD_d_format
, value
);
1729 dctx
->format
= (ZSTD_format_e
)value
;
1731 case ZSTD_d_stableOutBuffer
:
1732 CHECK_DBOUNDS(ZSTD_d_stableOutBuffer
, value
);
1733 dctx
->outBufferMode
= (ZSTD_bufferMode_e
)value
;
1735 case ZSTD_d_forceIgnoreChecksum
:
1736 CHECK_DBOUNDS(ZSTD_d_forceIgnoreChecksum
, value
);
1737 dctx
->forceIgnoreChecksum
= (ZSTD_forceIgnoreChecksum_e
)value
;
1739 case ZSTD_d_refMultipleDDicts
:
1740 CHECK_DBOUNDS(ZSTD_d_refMultipleDDicts
, value
);
1741 if (dctx
->staticSize
!= 0) {
1742 RETURN_ERROR(parameter_unsupported
, "Static dctx does not support multiple DDicts!");
1744 dctx
->refMultipleDDicts
= (ZSTD_refMultipleDDicts_e
)value
;
1748 RETURN_ERROR(parameter_unsupported
, "");
1751 size_t ZSTD_DCtx_reset(ZSTD_DCtx
* dctx
, ZSTD_ResetDirective reset
)
1753 if ( (reset
== ZSTD_reset_session_only
)
1754 || (reset
== ZSTD_reset_session_and_parameters
) ) {
1755 dctx
->streamStage
= zdss_init
;
1756 dctx
->noForwardProgress
= 0;
1758 if ( (reset
== ZSTD_reset_parameters
)
1759 || (reset
== ZSTD_reset_session_and_parameters
) ) {
1760 RETURN_ERROR_IF(dctx
->streamStage
!= zdss_init
, stage_wrong
, "");
1761 ZSTD_clearDict(dctx
);
1762 ZSTD_DCtx_resetParameters(dctx
);
1768 size_t ZSTD_sizeof_DStream(const ZSTD_DStream
* dctx
)
1770 return ZSTD_sizeof_DCtx(dctx
);
1773 size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize
, unsigned long long frameContentSize
)
1775 size_t const blockSize
= (size_t) MIN(windowSize
, ZSTD_BLOCKSIZE_MAX
);
1776 /* space is needed to store the litbuffer after the output of a given block without stomping the extDict of a previous run, as well as to cover both windows against wildcopy*/
1777 unsigned long long const neededRBSize
= windowSize
+ blockSize
+ ZSTD_BLOCKSIZE_MAX
+ (WILDCOPY_OVERLENGTH
* 2);
1778 unsigned long long const neededSize
= MIN(frameContentSize
, neededRBSize
);
1779 size_t const minRBSize
= (size_t) neededSize
;
1780 RETURN_ERROR_IF((unsigned long long)minRBSize
!= neededSize
,
1781 frameParameter_windowTooLarge
, "");
1785 size_t ZSTD_estimateDStreamSize(size_t windowSize
)
1787 size_t const blockSize
= MIN(windowSize
, ZSTD_BLOCKSIZE_MAX
);
1788 size_t const inBuffSize
= blockSize
; /* no block can be larger */
1789 size_t const outBuffSize
= ZSTD_decodingBufferSize_min(windowSize
, ZSTD_CONTENTSIZE_UNKNOWN
);
1790 return ZSTD_estimateDCtxSize() + inBuffSize
+ outBuffSize
;
1793 size_t ZSTD_estimateDStreamSize_fromFrame(const void* src
, size_t srcSize
)
1795 U32
const windowSizeMax
= 1U << ZSTD_WINDOWLOG_MAX
; /* note : should be user-selectable, but requires an additional parameter (or a dctx) */
1796 ZSTD_frameHeader zfh
;
1797 size_t const err
= ZSTD_getFrameHeader(&zfh
, src
, srcSize
);
1798 if (ZSTD_isError(err
)) return err
;
1799 RETURN_ERROR_IF(err
>0, srcSize_wrong
, "");
1800 RETURN_ERROR_IF(zfh
.windowSize
> windowSizeMax
,
1801 frameParameter_windowTooLarge
, "");
1802 return ZSTD_estimateDStreamSize((size_t)zfh
.windowSize
);
1806 /* ***** Decompression ***** */
1808 static int ZSTD_DCtx_isOverflow(ZSTD_DStream
* zds
, size_t const neededInBuffSize
, size_t const neededOutBuffSize
)
1810 return (zds
->inBuffSize
+ zds
->outBuffSize
) >= (neededInBuffSize
+ neededOutBuffSize
) * ZSTD_WORKSPACETOOLARGE_FACTOR
;
1813 static void ZSTD_DCtx_updateOversizedDuration(ZSTD_DStream
* zds
, size_t const neededInBuffSize
, size_t const neededOutBuffSize
)
1815 if (ZSTD_DCtx_isOverflow(zds
, neededInBuffSize
, neededOutBuffSize
))
1816 zds
->oversizedDuration
++;
1818 zds
->oversizedDuration
= 0;
1821 static int ZSTD_DCtx_isOversizedTooLong(ZSTD_DStream
* zds
)
1823 return zds
->oversizedDuration
>= ZSTD_WORKSPACETOOLARGE_MAXDURATION
;
1826 /* Checks that the output buffer hasn't changed if ZSTD_obm_stable is used. */
1827 static size_t ZSTD_checkOutBuffer(ZSTD_DStream
const* zds
, ZSTD_outBuffer
const* output
)
1829 ZSTD_outBuffer
const expect
= zds
->expectedOutBuffer
;
1830 /* No requirement when ZSTD_obm_stable is not enabled. */
1831 if (zds
->outBufferMode
!= ZSTD_bm_stable
)
1833 /* Any buffer is allowed in zdss_init, this must be the same for every other call until
1834 * the context is reset.
1836 if (zds
->streamStage
== zdss_init
)
1838 /* The buffer must match our expectation exactly. */
1839 if (expect
.dst
== output
->dst
&& expect
.pos
== output
->pos
&& expect
.size
== output
->size
)
1841 RETURN_ERROR(dstBuffer_wrong
, "ZSTD_d_stableOutBuffer enabled but output differs!");
1844 /* Calls ZSTD_decompressContinue() with the right parameters for ZSTD_decompressStream()
1845 * and updates the stage and the output buffer state. This call is extracted so it can be
1846 * used both when reading directly from the ZSTD_inBuffer, and in buffered input mode.
1847 * NOTE: You must break after calling this function since the streamStage is modified.
1849 static size_t ZSTD_decompressContinueStream(
1850 ZSTD_DStream
* zds
, char** op
, char* oend
,
1851 void const* src
, size_t srcSize
) {
1852 int const isSkipFrame
= ZSTD_isSkipFrame(zds
);
1853 if (zds
->outBufferMode
== ZSTD_bm_buffered
) {
1854 size_t const dstSize
= isSkipFrame
? 0 : zds
->outBuffSize
- zds
->outStart
;
1855 size_t const decodedSize
= ZSTD_decompressContinue(zds
,
1856 zds
->outBuff
+ zds
->outStart
, dstSize
, src
, srcSize
);
1857 FORWARD_IF_ERROR(decodedSize
, "");
1858 if (!decodedSize
&& !isSkipFrame
) {
1859 zds
->streamStage
= zdss_read
;
1861 zds
->outEnd
= zds
->outStart
+ decodedSize
;
1862 zds
->streamStage
= zdss_flush
;
1865 /* Write directly into the output buffer */
1866 size_t const dstSize
= isSkipFrame
? 0 : (size_t)(oend
- *op
);
1867 size_t const decodedSize
= ZSTD_decompressContinue(zds
, *op
, dstSize
, src
, srcSize
);
1868 FORWARD_IF_ERROR(decodedSize
, "");
1870 /* Flushing is not needed. */
1871 zds
->streamStage
= zdss_read
;
1872 assert(*op
<= oend
);
1873 assert(zds
->outBufferMode
== ZSTD_bm_stable
);
1878 size_t ZSTD_decompressStream(ZSTD_DStream
* zds
, ZSTD_outBuffer
* output
, ZSTD_inBuffer
* input
)
1880 const char* const src
= (const char*)input
->src
;
1881 const char* const istart
= input
->pos
!= 0 ? src
+ input
->pos
: src
;
1882 const char* const iend
= input
->size
!= 0 ? src
+ input
->size
: src
;
1883 const char* ip
= istart
;
1884 char* const dst
= (char*)output
->dst
;
1885 char* const ostart
= output
->pos
!= 0 ? dst
+ output
->pos
: dst
;
1886 char* const oend
= output
->size
!= 0 ? dst
+ output
->size
: dst
;
1888 U32 someMoreWork
= 1;
1890 DEBUGLOG(5, "ZSTD_decompressStream");
1892 input
->pos
> input
->size
,
1894 "forbidden. in: pos: %u vs size: %u",
1895 (U32
)input
->pos
, (U32
)input
->size
);
1897 output
->pos
> output
->size
,
1899 "forbidden. out: pos: %u vs size: %u",
1900 (U32
)output
->pos
, (U32
)output
->size
);
1901 DEBUGLOG(5, "input size : %u", (U32
)(input
->size
- input
->pos
));
1902 FORWARD_IF_ERROR(ZSTD_checkOutBuffer(zds
, output
), "");
1904 while (someMoreWork
) {
1905 switch(zds
->streamStage
)
1908 DEBUGLOG(5, "stage zdss_init => transparent reset ");
1909 zds
->streamStage
= zdss_loadHeader
;
1910 zds
->lhSize
= zds
->inPos
= zds
->outStart
= zds
->outEnd
= 0;
1911 zds
->hostageByte
= 0;
1912 zds
->expectedOutBuffer
= *output
;
1915 case zdss_loadHeader
:
1916 DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32
)(iend
- ip
));
1917 { size_t const hSize
= ZSTD_getFrameHeader_advanced(&zds
->fParams
, zds
->headerBuffer
, zds
->lhSize
, zds
->format
);
1918 if (zds
->refMultipleDDicts
&& zds
->ddictSet
) {
1919 ZSTD_DCtx_selectFrameDDict(zds
);
1921 DEBUGLOG(5, "header size : %u", (U32
)hSize
);
1922 if (ZSTD_isError(hSize
)) {
1923 return hSize
; /* error */
1925 if (hSize
!= 0) { /* need more input */
1926 size_t const toLoad
= hSize
- zds
->lhSize
; /* if hSize!=0, hSize > zds->lhSize */
1927 size_t const remainingInput
= (size_t)(iend
-ip
);
1929 if (toLoad
> remainingInput
) { /* not enough input to load full header */
1930 if (remainingInput
> 0) {
1931 ZSTD_memcpy(zds
->headerBuffer
+ zds
->lhSize
, ip
, remainingInput
);
1932 zds
->lhSize
+= remainingInput
;
1934 input
->pos
= input
->size
;
1935 return (MAX((size_t)ZSTD_FRAMEHEADERSIZE_MIN(zds
->format
), hSize
) - zds
->lhSize
) + ZSTD_blockHeaderSize
; /* remaining header bytes + next block header */
1938 ZSTD_memcpy(zds
->headerBuffer
+ zds
->lhSize
, ip
, toLoad
); zds
->lhSize
= hSize
; ip
+= toLoad
;
1942 /* check for single-pass mode opportunity */
1943 if (zds
->fParams
.frameContentSize
!= ZSTD_CONTENTSIZE_UNKNOWN
1944 && zds
->fParams
.frameType
!= ZSTD_skippableFrame
1945 && (U64
)(size_t)(oend
-op
) >= zds
->fParams
.frameContentSize
) {
1946 size_t const cSize
= ZSTD_findFrameCompressedSize(istart
, (size_t)(iend
-istart
));
1947 if (cSize
<= (size_t)(iend
-istart
)) {
1948 /* shortcut : using single-pass mode */
1949 size_t const decompressedSize
= ZSTD_decompress_usingDDict(zds
, op
, (size_t)(oend
-op
), istart
, cSize
, ZSTD_getDDict(zds
));
1950 if (ZSTD_isError(decompressedSize
)) return decompressedSize
;
1951 DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()")
1952 ip
= istart
+ cSize
;
1953 op
+= decompressedSize
;
1955 zds
->streamStage
= zdss_init
;
1960 /* Check output buffer is large enough for ZSTD_odm_stable. */
1961 if (zds
->outBufferMode
== ZSTD_bm_stable
1962 && zds
->fParams
.frameType
!= ZSTD_skippableFrame
1963 && zds
->fParams
.frameContentSize
!= ZSTD_CONTENTSIZE_UNKNOWN
1964 && (U64
)(size_t)(oend
-op
) < zds
->fParams
.frameContentSize
) {
1965 RETURN_ERROR(dstSize_tooSmall
, "ZSTD_obm_stable passed but ZSTD_outBuffer is too small");
1968 /* Consume header (see ZSTDds_decodeFrameHeader) */
1969 DEBUGLOG(4, "Consume header");
1970 FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(zds
, ZSTD_getDDict(zds
)), "");
1972 if ((MEM_readLE32(zds
->headerBuffer
) & ZSTD_MAGIC_SKIPPABLE_MASK
) == ZSTD_MAGIC_SKIPPABLE_START
) { /* skippable frame */
1973 zds
->expected
= MEM_readLE32(zds
->headerBuffer
+ ZSTD_FRAMEIDSIZE
);
1974 zds
->stage
= ZSTDds_skipFrame
;
1976 FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(zds
, zds
->headerBuffer
, zds
->lhSize
), "");
1977 zds
->expected
= ZSTD_blockHeaderSize
;
1978 zds
->stage
= ZSTDds_decodeBlockHeader
;
1981 /* control buffer memory usage */
1982 DEBUGLOG(4, "Control max memory usage (%u KB <= max %u KB)",
1983 (U32
)(zds
->fParams
.windowSize
>>10),
1984 (U32
)(zds
->maxWindowSize
>> 10) );
1985 zds
->fParams
.windowSize
= MAX(zds
->fParams
.windowSize
, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN
);
1986 RETURN_ERROR_IF(zds
->fParams
.windowSize
> zds
->maxWindowSize
,
1987 frameParameter_windowTooLarge
, "");
1989 /* Adapt buffer sizes to frame header instructions */
1990 { size_t const neededInBuffSize
= MAX(zds
->fParams
.blockSizeMax
, 4 /* frame checksum */);
1991 size_t const neededOutBuffSize
= zds
->outBufferMode
== ZSTD_bm_buffered
1992 ? ZSTD_decodingBufferSize_min(zds
->fParams
.windowSize
, zds
->fParams
.frameContentSize
)
1995 ZSTD_DCtx_updateOversizedDuration(zds
, neededInBuffSize
, neededOutBuffSize
);
1997 { int const tooSmall
= (zds
->inBuffSize
< neededInBuffSize
) || (zds
->outBuffSize
< neededOutBuffSize
);
1998 int const tooLarge
= ZSTD_DCtx_isOversizedTooLong(zds
);
2000 if (tooSmall
|| tooLarge
) {
2001 size_t const bufferSize
= neededInBuffSize
+ neededOutBuffSize
;
2002 DEBUGLOG(4, "inBuff : from %u to %u",
2003 (U32
)zds
->inBuffSize
, (U32
)neededInBuffSize
);
2004 DEBUGLOG(4, "outBuff : from %u to %u",
2005 (U32
)zds
->outBuffSize
, (U32
)neededOutBuffSize
);
2006 if (zds
->staticSize
) { /* static DCtx */
2007 DEBUGLOG(4, "staticSize : %u", (U32
)zds
->staticSize
);
2008 assert(zds
->staticSize
>= sizeof(ZSTD_DCtx
)); /* controlled at init */
2010 bufferSize
> zds
->staticSize
- sizeof(ZSTD_DCtx
),
2011 memory_allocation
, "");
2013 ZSTD_customFree(zds
->inBuff
, zds
->customMem
);
2014 zds
->inBuffSize
= 0;
2015 zds
->outBuffSize
= 0;
2016 zds
->inBuff
= (char*)ZSTD_customMalloc(bufferSize
, zds
->customMem
);
2017 RETURN_ERROR_IF(zds
->inBuff
== NULL
, memory_allocation
, "");
2019 zds
->inBuffSize
= neededInBuffSize
;
2020 zds
->outBuff
= zds
->inBuff
+ zds
->inBuffSize
;
2021 zds
->outBuffSize
= neededOutBuffSize
;
2023 zds
->streamStage
= zdss_read
;
2027 DEBUGLOG(5, "stage zdss_read");
2028 { size_t const neededInSize
= ZSTD_nextSrcSizeToDecompressWithInputSize(zds
, (size_t)(iend
- ip
));
2029 DEBUGLOG(5, "neededInSize = %u", (U32
)neededInSize
);
2030 if (neededInSize
==0) { /* end of frame */
2031 zds
->streamStage
= zdss_init
;
2035 if ((size_t)(iend
-ip
) >= neededInSize
) { /* decode directly from src */
2036 FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds
, &op
, oend
, ip
, neededInSize
), "");
2038 /* Function modifies the stage so we must break */
2041 if (ip
==iend
) { someMoreWork
= 0; break; } /* no more input */
2042 zds
->streamStage
= zdss_load
;
2046 { size_t const neededInSize
= ZSTD_nextSrcSizeToDecompress(zds
);
2047 size_t const toLoad
= neededInSize
- zds
->inPos
;
2048 int const isSkipFrame
= ZSTD_isSkipFrame(zds
);
2050 /* At this point we shouldn't be decompressing a block that we can stream. */
2051 assert(neededInSize
== ZSTD_nextSrcSizeToDecompressWithInputSize(zds
, iend
- ip
));
2053 loadedSize
= MIN(toLoad
, (size_t)(iend
-ip
));
2055 RETURN_ERROR_IF(toLoad
> zds
->inBuffSize
- zds
->inPos
,
2056 corruption_detected
,
2057 "should never happen");
2058 loadedSize
= ZSTD_limitCopy(zds
->inBuff
+ zds
->inPos
, toLoad
, ip
, (size_t)(iend
-ip
));
2061 zds
->inPos
+= loadedSize
;
2062 if (loadedSize
< toLoad
) { someMoreWork
= 0; break; } /* not enough input, wait for more */
2064 /* decode loaded input */
2065 zds
->inPos
= 0; /* input is consumed */
2066 FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds
, &op
, oend
, zds
->inBuff
, neededInSize
), "");
2067 /* Function modifies the stage so we must break */
2071 { size_t const toFlushSize
= zds
->outEnd
- zds
->outStart
;
2072 size_t const flushedSize
= ZSTD_limitCopy(op
, (size_t)(oend
-op
), zds
->outBuff
+ zds
->outStart
, toFlushSize
);
2074 zds
->outStart
+= flushedSize
;
2075 if (flushedSize
== toFlushSize
) { /* flush completed */
2076 zds
->streamStage
= zdss_read
;
2077 if ( (zds
->outBuffSize
< zds
->fParams
.frameContentSize
)
2078 && (zds
->outStart
+ zds
->fParams
.blockSizeMax
> zds
->outBuffSize
) ) {
2079 DEBUGLOG(5, "restart filling outBuff from beginning (left:%i, needed:%u)",
2080 (int)(zds
->outBuffSize
- zds
->outStart
),
2081 (U32
)zds
->fParams
.blockSizeMax
);
2082 zds
->outStart
= zds
->outEnd
= 0;
2086 /* cannot complete flush */
2091 assert(0); /* impossible */
2092 RETURN_ERROR(GENERIC
, "impossible to reach"); /* some compiler require default to do something */
2096 input
->pos
= (size_t)(ip
- (const char*)(input
->src
));
2097 output
->pos
= (size_t)(op
- (char*)(output
->dst
));
2099 /* Update the expected output buffer for ZSTD_obm_stable. */
2100 zds
->expectedOutBuffer
= *output
;
2102 if ((ip
==istart
) && (op
==ostart
)) { /* no forward progress */
2103 zds
->noForwardProgress
++;
2104 if (zds
->noForwardProgress
>= ZSTD_NO_FORWARD_PROGRESS_MAX
) {
2105 RETURN_ERROR_IF(op
==oend
, dstSize_tooSmall
, "");
2106 RETURN_ERROR_IF(ip
==iend
, srcSize_wrong
, "");
2110 zds
->noForwardProgress
= 0;
2112 { size_t nextSrcSizeHint
= ZSTD_nextSrcSizeToDecompress(zds
);
2113 if (!nextSrcSizeHint
) { /* frame fully decoded */
2114 if (zds
->outEnd
== zds
->outStart
) { /* output fully flushed */
2115 if (zds
->hostageByte
) {
2116 if (input
->pos
>= input
->size
) {
2117 /* can't release hostage (not present) */
2118 zds
->streamStage
= zdss_read
;
2121 input
->pos
++; /* release hostage */
2122 } /* zds->hostageByte */
2124 } /* zds->outEnd == zds->outStart */
2125 if (!zds
->hostageByte
) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */
2126 input
->pos
--; /* note : pos > 0, otherwise, impossible to finish reading last block */
2130 } /* nextSrcSizeHint==0 */
2131 nextSrcSizeHint
+= ZSTD_blockHeaderSize
* (ZSTD_nextInputType(zds
) == ZSTDnit_block
); /* preload header of next block */
2132 assert(zds
->inPos
<= nextSrcSizeHint
);
2133 nextSrcSizeHint
-= zds
->inPos
; /* part already loaded*/
2134 return nextSrcSizeHint
;
2138 size_t ZSTD_decompressStream_simpleArgs (
2140 void* dst
, size_t dstCapacity
, size_t* dstPos
,
2141 const void* src
, size_t srcSize
, size_t* srcPos
)
2143 ZSTD_outBuffer output
= { dst
, dstCapacity
, *dstPos
};
2144 ZSTD_inBuffer input
= { src
, srcSize
, *srcPos
};
2145 /* ZSTD_compress_generic() will check validity of dstPos and srcPos */
2146 size_t const cErr
= ZSTD_decompressStream(dctx
, &output
, &input
);
2147 *dstPos
= output
.pos
;
2148 *srcPos
= input
.pos
;