blob: 675596f5aa9254a45b27b7ec0d8f07974cea48c9 [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -04001/*
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
9 */
10
11
12/* ***************************************************************
13* Tuning parameters
14*****************************************************************/
15/*!
16 * HEAPMODE :
17 * Select how default decompression function ZSTD_decompress() allocates its context,
18 * on stack (0), or into heap (1, default; requires malloc()).
19 * Note that functions with explicit context such as ZSTD_decompressDCtx() are unaffected.
20 */
21#ifndef ZSTD_HEAPMODE
22# define ZSTD_HEAPMODE 1
23#endif
24
25/*!
26* LEGACY_SUPPORT :
27* if set to 1+, ZSTD_decompress() can decode older formats (v0.1+)
28*/
29#ifndef ZSTD_LEGACY_SUPPORT
30# define ZSTD_LEGACY_SUPPORT 0
31#endif
32
33/*!
34 * MAXWINDOWSIZE_DEFAULT :
35 * maximum window size accepted by DStream __by default__.
36 * Frames requiring more memory will be rejected.
37 * It's possible to set a different limit using ZSTD_DCtx_setMaxWindowSize().
38 */
39#ifndef ZSTD_MAXWINDOWSIZE_DEFAULT
Abhilash S.L3b494632019-07-16 15:51:09 +053040# define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) + 1)
41#endif
42
43/*!
44 * NO_FORWARD_PROGRESS_MAX :
45 * maximum allowed nb of calls to ZSTD_decompressStream()
46 * without any forward progress
47 * (defined as: no byte read from input, and no byte flushed to output)
48 * before triggering an error.
49 */
50#ifndef ZSTD_NO_FORWARD_PROGRESS_MAX
51# define ZSTD_NO_FORWARD_PROGRESS_MAX 16
William Kurkianea869482019-04-09 15:16:11 -040052#endif
53
54
55/*-*******************************************************
56* Dependencies
57*********************************************************/
58#include <string.h> /* memcpy, memmove, memset */
Abhilash S.L3b494632019-07-16 15:51:09 +053059#include "cpu.h" /* bmi2 */
William Kurkianea869482019-04-09 15:16:11 -040060#include "mem.h" /* low level memory routines */
61#define FSE_STATIC_LINKING_ONLY
62#include "fse.h"
63#define HUF_STATIC_LINKING_ONLY
64#include "huf.h"
Abhilash S.L3b494632019-07-16 15:51:09 +053065#include "zstd_internal.h" /* blockProperties_t */
66#include "zstd_decompress_internal.h" /* ZSTD_DCtx */
67#include "zstd_ddict.h" /* ZSTD_DDictDictContent */
68#include "zstd_decompress_block.h" /* ZSTD_decompressBlock_internal */
William Kurkianea869482019-04-09 15:16:11 -040069
70#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
71# include "zstd_legacy.h"
72#endif
73
74
William Kurkianea869482019-04-09 15:16:11 -040075/*-*************************************************************
76* Context management
77***************************************************************/
William Kurkianea869482019-04-09 15:16:11 -040078size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx)
79{
80 if (dctx==NULL) return 0; /* support sizeof NULL */
81 return sizeof(*dctx)
82 + ZSTD_sizeof_DDict(dctx->ddictLocal)
83 + dctx->inBuffSize + dctx->outBuffSize;
84}
85
86size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); }
87
88
89static size_t ZSTD_startingInputLength(ZSTD_format_e format)
90{
91 size_t const startingInputLength = (format==ZSTD_f_zstd1_magicless) ?
Abhilash S.L3b494632019-07-16 15:51:09 +053092 ZSTD_FRAMEHEADERSIZE_PREFIX - ZSTD_FRAMEIDSIZE :
93 ZSTD_FRAMEHEADERSIZE_PREFIX;
William Kurkianea869482019-04-09 15:16:11 -040094 ZSTD_STATIC_ASSERT(ZSTD_FRAMEHEADERSIZE_PREFIX >= ZSTD_FRAMEIDSIZE);
95 /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */
96 assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) );
97 return startingInputLength;
98}
99
100static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
101{
102 dctx->format = ZSTD_f_zstd1; /* ZSTD_decompressBegin() invokes ZSTD_startingInputLength() with argument dctx->format */
103 dctx->staticSize = 0;
104 dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
105 dctx->ddict = NULL;
106 dctx->ddictLocal = NULL;
Abhilash S.L3b494632019-07-16 15:51:09 +0530107 dctx->dictEnd = NULL;
108 dctx->ddictIsCold = 0;
109 dctx->dictUses = ZSTD_dont_use;
William Kurkianea869482019-04-09 15:16:11 -0400110 dctx->inBuff = NULL;
111 dctx->inBuffSize = 0;
112 dctx->outBuffSize = 0;
113 dctx->streamStage = zdss_init;
Abhilash S.L3b494632019-07-16 15:51:09 +0530114 dctx->legacyContext = NULL;
115 dctx->previousLegacyVersion = 0;
116 dctx->noForwardProgress = 0;
William Kurkianea869482019-04-09 15:16:11 -0400117 dctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid());
118}
119
120ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize)
121{
122 ZSTD_DCtx* const dctx = (ZSTD_DCtx*) workspace;
123
124 if ((size_t)workspace & 7) return NULL; /* 8-aligned */
125 if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL; /* minimum size */
126
127 ZSTD_initDCtx_internal(dctx);
128 dctx->staticSize = workspaceSize;
129 dctx->inBuff = (char*)(dctx+1);
130 return dctx;
131}
132
133ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
134{
135 if (!customMem.customAlloc ^ !customMem.customFree) return NULL;
136
137 { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(*dctx), customMem);
138 if (!dctx) return NULL;
139 dctx->customMem = customMem;
William Kurkianea869482019-04-09 15:16:11 -0400140 ZSTD_initDCtx_internal(dctx);
141 return dctx;
142 }
143}
144
145ZSTD_DCtx* ZSTD_createDCtx(void)
146{
147 DEBUGLOG(3, "ZSTD_createDCtx");
148 return ZSTD_createDCtx_advanced(ZSTD_defaultCMem);
149}
150
Abhilash S.L3b494632019-07-16 15:51:09 +0530151static void ZSTD_clearDict(ZSTD_DCtx* dctx)
152{
153 ZSTD_freeDDict(dctx->ddictLocal);
154 dctx->ddictLocal = NULL;
155 dctx->ddict = NULL;
156 dctx->dictUses = ZSTD_dont_use;
157}
158
William Kurkianea869482019-04-09 15:16:11 -0400159size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
160{
161 if (dctx==NULL) return 0; /* support free on NULL */
Abhilash S.L3b494632019-07-16 15:51:09 +0530162 RETURN_ERROR_IF(dctx->staticSize, memory_allocation, "not compatible with static DCtx");
William Kurkianea869482019-04-09 15:16:11 -0400163 { ZSTD_customMem const cMem = dctx->customMem;
Abhilash S.L3b494632019-07-16 15:51:09 +0530164 ZSTD_clearDict(dctx);
William Kurkianea869482019-04-09 15:16:11 -0400165 ZSTD_free(dctx->inBuff, cMem);
166 dctx->inBuff = NULL;
167#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
168 if (dctx->legacyContext)
169 ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion);
170#endif
171 ZSTD_free(dctx, cMem);
172 return 0;
173 }
174}
175
176/* no longer useful */
177void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)
178{
179 size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx);
180 memcpy(dstDCtx, srcDCtx, toCopy); /* no need to copy workspace */
181}
182
183
184/*-*************************************************************
185 * Frame header decoding
186 ***************************************************************/
187
188/*! ZSTD_isFrame() :
189 * Tells if the content of `buffer` starts with a valid Frame Identifier.
190 * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
191 * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
192 * Note 3 : Skippable Frame Identifiers are considered valid. */
193unsigned ZSTD_isFrame(const void* buffer, size_t size)
194{
Abhilash S.L3b494632019-07-16 15:51:09 +0530195 if (size < ZSTD_FRAMEIDSIZE) return 0;
William Kurkianea869482019-04-09 15:16:11 -0400196 { U32 const magic = MEM_readLE32(buffer);
197 if (magic == ZSTD_MAGICNUMBER) return 1;
Abhilash S.L3b494632019-07-16 15:51:09 +0530198 if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1;
William Kurkianea869482019-04-09 15:16:11 -0400199 }
200#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
201 if (ZSTD_isLegacy(buffer, size)) return 1;
202#endif
203 return 0;
204}
205
206/** ZSTD_frameHeaderSize_internal() :
207 * srcSize must be large enough to reach header size fields.
208 * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless.
209 * @return : size of the Frame Header
210 * or an error code, which can be tested with ZSTD_isError() */
211static size_t ZSTD_frameHeaderSize_internal(const void* src, size_t srcSize, ZSTD_format_e format)
212{
213 size_t const minInputSize = ZSTD_startingInputLength(format);
Abhilash S.L3b494632019-07-16 15:51:09 +0530214 RETURN_ERROR_IF(srcSize < minInputSize, srcSize_wrong);
William Kurkianea869482019-04-09 15:16:11 -0400215
216 { BYTE const fhd = ((const BYTE*)src)[minInputSize-1];
217 U32 const dictID= fhd & 3;
218 U32 const singleSegment = (fhd >> 5) & 1;
219 U32 const fcsId = fhd >> 6;
220 return minInputSize + !singleSegment
221 + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId]
222 + (singleSegment && !fcsId);
223 }
224}
225
226/** ZSTD_frameHeaderSize() :
227 * srcSize must be >= ZSTD_frameHeaderSize_prefix.
Abhilash S.L3b494632019-07-16 15:51:09 +0530228 * @return : size of the Frame Header,
229 * or an error code (if srcSize is too small) */
William Kurkianea869482019-04-09 15:16:11 -0400230size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize)
231{
232 return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1);
233}
234
235
Abhilash S.L3b494632019-07-16 15:51:09 +0530236/** ZSTD_getFrameHeader_advanced() :
William Kurkianea869482019-04-09 15:16:11 -0400237 * decode Frame Header, or require larger `srcSize`.
238 * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless
239 * @return : 0, `zfhPtr` is correctly filled,
240 * >0, `srcSize` is too small, value is wanted `srcSize` amount,
241 * or an error code, which can be tested using ZSTD_isError() */
Abhilash S.L3b494632019-07-16 15:51:09 +0530242size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format)
William Kurkianea869482019-04-09 15:16:11 -0400243{
244 const BYTE* ip = (const BYTE*)src;
245 size_t const minInputSize = ZSTD_startingInputLength(format);
246
Abhilash S.L3b494632019-07-16 15:51:09 +0530247 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 */
William Kurkianea869482019-04-09 15:16:11 -0400248 if (srcSize < minInputSize) return minInputSize;
Abhilash S.L3b494632019-07-16 15:51:09 +0530249 RETURN_ERROR_IF(src==NULL, GENERIC, "invalid parameter");
William Kurkianea869482019-04-09 15:16:11 -0400250
251 if ( (format != ZSTD_f_zstd1_magicless)
252 && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) {
Abhilash S.L3b494632019-07-16 15:51:09 +0530253 if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
William Kurkianea869482019-04-09 15:16:11 -0400254 /* skippable frame */
Abhilash S.L3b494632019-07-16 15:51:09 +0530255 if (srcSize < ZSTD_SKIPPABLEHEADERSIZE)
256 return ZSTD_SKIPPABLEHEADERSIZE; /* magic number + frame length */
William Kurkianea869482019-04-09 15:16:11 -0400257 memset(zfhPtr, 0, sizeof(*zfhPtr));
Abhilash S.L3b494632019-07-16 15:51:09 +0530258 zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_FRAMEIDSIZE);
William Kurkianea869482019-04-09 15:16:11 -0400259 zfhPtr->frameType = ZSTD_skippableFrame;
260 return 0;
261 }
Abhilash S.L3b494632019-07-16 15:51:09 +0530262 RETURN_ERROR(prefix_unknown);
William Kurkianea869482019-04-09 15:16:11 -0400263 }
264
265 /* ensure there is enough `srcSize` to fully read/decode frame header */
266 { size_t const fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format);
267 if (srcSize < fhsize) return fhsize;
268 zfhPtr->headerSize = (U32)fhsize;
269 }
270
271 { BYTE const fhdByte = ip[minInputSize-1];
272 size_t pos = minInputSize;
273 U32 const dictIDSizeCode = fhdByte&3;
274 U32 const checksumFlag = (fhdByte>>2)&1;
275 U32 const singleSegment = (fhdByte>>5)&1;
276 U32 const fcsID = fhdByte>>6;
277 U64 windowSize = 0;
278 U32 dictID = 0;
279 U64 frameContentSize = ZSTD_CONTENTSIZE_UNKNOWN;
Abhilash S.L3b494632019-07-16 15:51:09 +0530280 RETURN_ERROR_IF((fhdByte & 0x08) != 0, frameParameter_unsupported,
281 "reserved bits, must be zero");
William Kurkianea869482019-04-09 15:16:11 -0400282
283 if (!singleSegment) {
284 BYTE const wlByte = ip[pos++];
285 U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN;
Abhilash S.L3b494632019-07-16 15:51:09 +0530286 RETURN_ERROR_IF(windowLog > ZSTD_WINDOWLOG_MAX, frameParameter_windowTooLarge);
William Kurkianea869482019-04-09 15:16:11 -0400287 windowSize = (1ULL << windowLog);
288 windowSize += (windowSize >> 3) * (wlByte&7);
289 }
290 switch(dictIDSizeCode)
291 {
292 default: assert(0); /* impossible */
293 case 0 : break;
294 case 1 : dictID = ip[pos]; pos++; break;
295 case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break;
296 case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break;
297 }
298 switch(fcsID)
299 {
300 default: assert(0); /* impossible */
301 case 0 : if (singleSegment) frameContentSize = ip[pos]; break;
302 case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break;
303 case 2 : frameContentSize = MEM_readLE32(ip+pos); break;
304 case 3 : frameContentSize = MEM_readLE64(ip+pos); break;
305 }
306 if (singleSegment) windowSize = frameContentSize;
307
308 zfhPtr->frameType = ZSTD_frame;
309 zfhPtr->frameContentSize = frameContentSize;
310 zfhPtr->windowSize = windowSize;
311 zfhPtr->blockSizeMax = (unsigned) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
312 zfhPtr->dictID = dictID;
313 zfhPtr->checksumFlag = checksumFlag;
314 }
315 return 0;
316}
317
318/** ZSTD_getFrameHeader() :
319 * decode Frame Header, or require larger `srcSize`.
320 * note : this function does not consume input, it only reads it.
321 * @return : 0, `zfhPtr` is correctly filled,
322 * >0, `srcSize` is too small, value is wanted `srcSize` amount,
323 * or an error code, which can be tested using ZSTD_isError() */
324size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize)
325{
Abhilash S.L3b494632019-07-16 15:51:09 +0530326 return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_f_zstd1);
William Kurkianea869482019-04-09 15:16:11 -0400327}
328
329
330/** ZSTD_getFrameContentSize() :
331 * compatible with legacy mode
332 * @return : decompressed size of the single frame pointed to be `src` if known, otherwise
333 * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
334 * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */
335unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize)
336{
337#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
338 if (ZSTD_isLegacy(src, srcSize)) {
339 unsigned long long const ret = ZSTD_getDecompressedSize_legacy(src, srcSize);
340 return ret == 0 ? ZSTD_CONTENTSIZE_UNKNOWN : ret;
341 }
342#endif
343 { ZSTD_frameHeader zfh;
344 if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0)
345 return ZSTD_CONTENTSIZE_ERROR;
346 if (zfh.frameType == ZSTD_skippableFrame) {
347 return 0;
348 } else {
349 return zfh.frameContentSize;
350 } }
351}
352
Abhilash S.L3b494632019-07-16 15:51:09 +0530353static size_t readSkippableFrameSize(void const* src, size_t srcSize)
354{
355 size_t const skippableHeaderSize = ZSTD_SKIPPABLEHEADERSIZE;
356 U32 sizeU32;
357
358 RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong);
359
360 sizeU32 = MEM_readLE32((BYTE const*)src + ZSTD_FRAMEIDSIZE);
361 RETURN_ERROR_IF((U32)(sizeU32 + ZSTD_SKIPPABLEHEADERSIZE) < sizeU32,
362 frameParameter_unsupported);
363
364 return skippableHeaderSize + sizeU32;
365}
366
William Kurkianea869482019-04-09 15:16:11 -0400367/** ZSTD_findDecompressedSize() :
368 * compatible with legacy mode
369 * `srcSize` must be the exact length of some number of ZSTD compressed and/or
370 * skippable frames
371 * @return : decompressed size of the frames contained */
372unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize)
373{
374 unsigned long long totalDstSize = 0;
375
Abhilash S.L3b494632019-07-16 15:51:09 +0530376 while (srcSize >= ZSTD_FRAMEHEADERSIZE_PREFIX) {
William Kurkianea869482019-04-09 15:16:11 -0400377 U32 const magicNumber = MEM_readLE32(src);
378
Abhilash S.L3b494632019-07-16 15:51:09 +0530379 if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
380 size_t const skippableSize = readSkippableFrameSize(src, srcSize);
381 if (ZSTD_isError(skippableSize))
382 return skippableSize;
William Kurkianea869482019-04-09 15:16:11 -0400383 if (srcSize < skippableSize) {
384 return ZSTD_CONTENTSIZE_ERROR;
385 }
386
387 src = (const BYTE *)src + skippableSize;
388 srcSize -= skippableSize;
389 continue;
390 }
391
392 { unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
393 if (ret >= ZSTD_CONTENTSIZE_ERROR) return ret;
394
395 /* check for overflow */
396 if (totalDstSize + ret < totalDstSize) return ZSTD_CONTENTSIZE_ERROR;
397 totalDstSize += ret;
398 }
399 { size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize);
400 if (ZSTD_isError(frameSrcSize)) {
401 return ZSTD_CONTENTSIZE_ERROR;
402 }
403
404 src = (const BYTE *)src + frameSrcSize;
405 srcSize -= frameSrcSize;
406 }
407 } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
408
409 if (srcSize) return ZSTD_CONTENTSIZE_ERROR;
410
411 return totalDstSize;
412}
413
414/** ZSTD_getDecompressedSize() :
Abhilash S.L3b494632019-07-16 15:51:09 +0530415 * compatible with legacy mode
416 * @return : decompressed size if known, 0 otherwise
417 note : 0 can mean any of the following :
William Kurkianea869482019-04-09 15:16:11 -0400418 - frame content is empty
419 - decompressed size field is not present in frame header
420 - frame header unknown / not supported
421 - frame header not complete (`srcSize` too small) */
422unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize)
423{
424 unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
425 ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR < ZSTD_CONTENTSIZE_UNKNOWN);
426 return (ret >= ZSTD_CONTENTSIZE_ERROR) ? 0 : ret;
427}
428
429
430/** ZSTD_decodeFrameHeader() :
Abhilash S.L3b494632019-07-16 15:51:09 +0530431 * `headerSize` must be the size provided by ZSTD_frameHeaderSize().
432 * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
William Kurkianea869482019-04-09 15:16:11 -0400433static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize)
434{
Abhilash S.L3b494632019-07-16 15:51:09 +0530435 size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format);
William Kurkianea869482019-04-09 15:16:11 -0400436 if (ZSTD_isError(result)) return result; /* invalid header */
Abhilash S.L3b494632019-07-16 15:51:09 +0530437 RETURN_ERROR_IF(result>0, srcSize_wrong, "headerSize too small");
438#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
439 /* Skip the dictID check in fuzzing mode, because it makes the search
440 * harder.
441 */
442 RETURN_ERROR_IF(dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID),
443 dictionary_wrong);
444#endif
William Kurkianea869482019-04-09 15:16:11 -0400445 if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0);
446 return 0;
447}
448
Abhilash S.L3b494632019-07-16 15:51:09 +0530449static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret)
William Kurkianea869482019-04-09 15:16:11 -0400450{
Abhilash S.L3b494632019-07-16 15:51:09 +0530451 ZSTD_frameSizeInfo frameSizeInfo;
452 frameSizeInfo.compressedSize = ret;
453 frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
454 return frameSizeInfo;
William Kurkianea869482019-04-09 15:16:11 -0400455}
456
Abhilash S.L3b494632019-07-16 15:51:09 +0530457static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize)
William Kurkianea869482019-04-09 15:16:11 -0400458{
Abhilash S.L3b494632019-07-16 15:51:09 +0530459 ZSTD_frameSizeInfo frameSizeInfo;
460 memset(&frameSizeInfo, 0, sizeof(ZSTD_frameSizeInfo));
William Kurkianea869482019-04-09 15:16:11 -0400461
Abhilash S.L3b494632019-07-16 15:51:09 +0530462#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
463 if (ZSTD_isLegacy(src, srcSize))
464 return ZSTD_findFrameSizeInfoLegacy(src, srcSize);
William Kurkianea869482019-04-09 15:16:11 -0400465#endif
466
Abhilash S.L3b494632019-07-16 15:51:09 +0530467 if ((srcSize >= ZSTD_SKIPPABLEHEADERSIZE)
468 && (MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
469 frameSizeInfo.compressedSize = readSkippableFrameSize(src, srcSize);
470 return frameSizeInfo;
471 } else {
472 const BYTE* ip = (const BYTE*)src;
473 const BYTE* const ipstart = ip;
474 size_t remainingSize = srcSize;
475 size_t nbBlocks = 0;
476 ZSTD_frameHeader zfh;
William Kurkianea869482019-04-09 15:16:11 -0400477
Abhilash S.L3b494632019-07-16 15:51:09 +0530478 /* Extract Frame Header */
479 { size_t const ret = ZSTD_getFrameHeader(&zfh, src, srcSize);
480 if (ZSTD_isError(ret))
481 return ZSTD_errorFrameSizeInfo(ret);
482 if (ret > 0)
483 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
William Kurkianea869482019-04-09 15:16:11 -0400484 }
485
Abhilash S.L3b494632019-07-16 15:51:09 +0530486 ip += zfh.headerSize;
487 remainingSize -= zfh.headerSize;
488
489 /* Iterate over each block */
490 while (1) {
491 blockProperties_t blockProperties;
492 size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
493 if (ZSTD_isError(cBlockSize))
494 return ZSTD_errorFrameSizeInfo(cBlockSize);
495
496 if (ZSTD_blockHeaderSize + cBlockSize > remainingSize)
497 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
498
499 ip += ZSTD_blockHeaderSize + cBlockSize;
500 remainingSize -= ZSTD_blockHeaderSize + cBlockSize;
501 nbBlocks++;
502
503 if (blockProperties.lastBlock) break;
504 }
505
506 /* Final frame content checksum */
507 if (zfh.checksumFlag) {
508 if (remainingSize < 4)
509 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
510 ip += 4;
511 }
512
513 frameSizeInfo.compressedSize = ip - ipstart;
514 frameSizeInfo.decompressedBound = (zfh.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN)
515 ? zfh.frameContentSize
516 : nbBlocks * zfh.blockSizeMax;
517 return frameSizeInfo;
William Kurkianea869482019-04-09 15:16:11 -0400518 }
519}
520
William Kurkianea869482019-04-09 15:16:11 -0400521/** ZSTD_findFrameCompressedSize() :
522 * compatible with legacy mode
523 * `src` must point to the start of a ZSTD frame, ZSTD legacy frame, or skippable frame
524 * `srcSize` must be at least as large as the frame contained
525 * @return : the compressed size of the frame starting at `src` */
526size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize)
527{
Abhilash S.L3b494632019-07-16 15:51:09 +0530528 ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize);
529 return frameSizeInfo.compressedSize;
530}
William Kurkianea869482019-04-09 15:16:11 -0400531
William Kurkianea869482019-04-09 15:16:11 -0400532
Abhilash S.L3b494632019-07-16 15:51:09 +0530533/** ZSTD_decompressBound() :
534 * compatible with legacy mode
535 * `src` must point to the start of a ZSTD frame or a skippeable frame
536 * `srcSize` must be at least as large as the frame contained
537 * @return : the maximum decompressed size of the compressed source
538 */
539unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize)
540{
541 unsigned long long bound = 0;
542 /* Iterate over each frame */
543 while (srcSize > 0) {
544 ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize);
545 size_t const compressedSize = frameSizeInfo.compressedSize;
546 unsigned long long const decompressedBound = frameSizeInfo.decompressedBound;
547 if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR)
548 return ZSTD_CONTENTSIZE_ERROR;
549 src = (const BYTE*)src + compressedSize;
550 srcSize -= compressedSize;
551 bound += decompressedBound;
552 }
553 return bound;
554}
William Kurkianea869482019-04-09 15:16:11 -0400555
William Kurkianea869482019-04-09 15:16:11 -0400556
Abhilash S.L3b494632019-07-16 15:51:09 +0530557/*-*************************************************************
558 * Frame decoding
559 ***************************************************************/
William Kurkianea869482019-04-09 15:16:11 -0400560
William Kurkianea869482019-04-09 15:16:11 -0400561
Abhilash S.L3b494632019-07-16 15:51:09 +0530562void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst)
563{
564 if (dst != dctx->previousDstEnd) { /* not contiguous */
565 dctx->dictEnd = dctx->previousDstEnd;
566 dctx->virtualStart = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
567 dctx->prefixStart = dst;
568 dctx->previousDstEnd = dst;
William Kurkianea869482019-04-09 15:16:11 -0400569 }
570}
571
Abhilash S.L3b494632019-07-16 15:51:09 +0530572/** ZSTD_insertBlock() :
573 insert `src` block into `dctx` history. Useful to track uncompressed blocks. */
574size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize)
575{
576 ZSTD_checkContinuity(dctx, blockStart);
577 dctx->previousDstEnd = (const char*)blockStart + blockSize;
578 return blockSize;
579}
580
581
582static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity,
583 const void* src, size_t srcSize)
584{
585 DEBUGLOG(5, "ZSTD_copyRawBlock");
586 if (dst == NULL) {
587 if (srcSize == 0) return 0;
588 RETURN_ERROR(dstBuffer_null);
589 }
590 RETURN_ERROR_IF(srcSize > dstCapacity, dstSize_tooSmall);
591 memcpy(dst, src, srcSize);
592 return srcSize;
593}
594
595static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity,
596 BYTE b,
597 size_t regenSize)
598{
599 if (dst == NULL) {
600 if (regenSize == 0) return 0;
601 RETURN_ERROR(dstBuffer_null);
602 }
603 RETURN_ERROR_IF(regenSize > dstCapacity, dstSize_tooSmall);
604 memset(dst, b, regenSize);
605 return regenSize;
606}
607
608
William Kurkianea869482019-04-09 15:16:11 -0400609/*! ZSTD_decompressFrame() :
Abhilash S.L3b494632019-07-16 15:51:09 +0530610 * @dctx must be properly initialized
611 * will update *srcPtr and *srcSizePtr,
612 * to make *srcPtr progress by one frame. */
William Kurkianea869482019-04-09 15:16:11 -0400613static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
614 void* dst, size_t dstCapacity,
615 const void** srcPtr, size_t *srcSizePtr)
616{
617 const BYTE* ip = (const BYTE*)(*srcPtr);
618 BYTE* const ostart = (BYTE* const)dst;
619 BYTE* const oend = ostart + dstCapacity;
620 BYTE* op = ostart;
Abhilash S.L3b494632019-07-16 15:51:09 +0530621 size_t remainingSrcSize = *srcSizePtr;
622
623 DEBUGLOG(4, "ZSTD_decompressFrame (srcSize:%i)", (int)*srcSizePtr);
William Kurkianea869482019-04-09 15:16:11 -0400624
625 /* check */
Abhilash S.L3b494632019-07-16 15:51:09 +0530626 RETURN_ERROR_IF(
627 remainingSrcSize < ZSTD_FRAMEHEADERSIZE_MIN+ZSTD_blockHeaderSize,
628 srcSize_wrong);
William Kurkianea869482019-04-09 15:16:11 -0400629
630 /* Frame Header */
Abhilash S.L3b494632019-07-16 15:51:09 +0530631 { size_t const frameHeaderSize = ZSTD_frameHeaderSize(ip, ZSTD_FRAMEHEADERSIZE_PREFIX);
William Kurkianea869482019-04-09 15:16:11 -0400632 if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize;
Abhilash S.L3b494632019-07-16 15:51:09 +0530633 RETURN_ERROR_IF(remainingSrcSize < frameHeaderSize+ZSTD_blockHeaderSize,
634 srcSize_wrong);
635 FORWARD_IF_ERROR( ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize) );
636 ip += frameHeaderSize; remainingSrcSize -= frameHeaderSize;
William Kurkianea869482019-04-09 15:16:11 -0400637 }
638
639 /* Loop on each block */
640 while (1) {
641 size_t decodedSize;
642 blockProperties_t blockProperties;
Abhilash S.L3b494632019-07-16 15:51:09 +0530643 size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSrcSize, &blockProperties);
William Kurkianea869482019-04-09 15:16:11 -0400644 if (ZSTD_isError(cBlockSize)) return cBlockSize;
645
646 ip += ZSTD_blockHeaderSize;
Abhilash S.L3b494632019-07-16 15:51:09 +0530647 remainingSrcSize -= ZSTD_blockHeaderSize;
648 RETURN_ERROR_IF(cBlockSize > remainingSrcSize, srcSize_wrong);
William Kurkianea869482019-04-09 15:16:11 -0400649
650 switch(blockProperties.blockType)
651 {
652 case bt_compressed:
653 decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize, /* frame */ 1);
654 break;
655 case bt_raw :
656 decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize);
657 break;
658 case bt_rle :
Abhilash S.L3b494632019-07-16 15:51:09 +0530659 decodedSize = ZSTD_setRleBlock(op, oend-op, *ip, blockProperties.origSize);
William Kurkianea869482019-04-09 15:16:11 -0400660 break;
661 case bt_reserved :
662 default:
Abhilash S.L3b494632019-07-16 15:51:09 +0530663 RETURN_ERROR(corruption_detected);
William Kurkianea869482019-04-09 15:16:11 -0400664 }
665
666 if (ZSTD_isError(decodedSize)) return decodedSize;
667 if (dctx->fParams.checksumFlag)
668 XXH64_update(&dctx->xxhState, op, decodedSize);
669 op += decodedSize;
670 ip += cBlockSize;
Abhilash S.L3b494632019-07-16 15:51:09 +0530671 remainingSrcSize -= cBlockSize;
William Kurkianea869482019-04-09 15:16:11 -0400672 if (blockProperties.lastBlock) break;
673 }
674
675 if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) {
Abhilash S.L3b494632019-07-16 15:51:09 +0530676 RETURN_ERROR_IF((U64)(op-ostart) != dctx->fParams.frameContentSize,
677 corruption_detected);
678 }
William Kurkianea869482019-04-09 15:16:11 -0400679 if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */
680 U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState);
681 U32 checkRead;
Abhilash S.L3b494632019-07-16 15:51:09 +0530682 RETURN_ERROR_IF(remainingSrcSize<4, checksum_wrong);
William Kurkianea869482019-04-09 15:16:11 -0400683 checkRead = MEM_readLE32(ip);
Abhilash S.L3b494632019-07-16 15:51:09 +0530684 RETURN_ERROR_IF(checkRead != checkCalc, checksum_wrong);
William Kurkianea869482019-04-09 15:16:11 -0400685 ip += 4;
Abhilash S.L3b494632019-07-16 15:51:09 +0530686 remainingSrcSize -= 4;
William Kurkianea869482019-04-09 15:16:11 -0400687 }
688
689 /* Allow caller to get size read */
690 *srcPtr = ip;
Abhilash S.L3b494632019-07-16 15:51:09 +0530691 *srcSizePtr = remainingSrcSize;
William Kurkianea869482019-04-09 15:16:11 -0400692 return op-ostart;
693}
694
William Kurkianea869482019-04-09 15:16:11 -0400695static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
696 void* dst, size_t dstCapacity,
697 const void* src, size_t srcSize,
698 const void* dict, size_t dictSize,
699 const ZSTD_DDict* ddict)
700{
701 void* const dststart = dst;
Abhilash S.L3b494632019-07-16 15:51:09 +0530702 int moreThan1Frame = 0;
703
704 DEBUGLOG(5, "ZSTD_decompressMultiFrame");
William Kurkianea869482019-04-09 15:16:11 -0400705 assert(dict==NULL || ddict==NULL); /* either dict or ddict set, not both */
706
707 if (ddict) {
Abhilash S.L3b494632019-07-16 15:51:09 +0530708 dict = ZSTD_DDict_dictContent(ddict);
709 dictSize = ZSTD_DDict_dictSize(ddict);
William Kurkianea869482019-04-09 15:16:11 -0400710 }
711
Abhilash S.L3b494632019-07-16 15:51:09 +0530712 while (srcSize >= ZSTD_FRAMEHEADERSIZE_PREFIX) {
William Kurkianea869482019-04-09 15:16:11 -0400713
714#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
715 if (ZSTD_isLegacy(src, srcSize)) {
716 size_t decodedSize;
717 size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
718 if (ZSTD_isError(frameSize)) return frameSize;
Abhilash S.L3b494632019-07-16 15:51:09 +0530719 RETURN_ERROR_IF(dctx->staticSize, memory_allocation,
720 "legacy support is not compatible with static dctx");
William Kurkianea869482019-04-09 15:16:11 -0400721
722 decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize);
Abhilash S.L3b494632019-07-16 15:51:09 +0530723 if (ZSTD_isError(decodedSize)) return decodedSize;
William Kurkianea869482019-04-09 15:16:11 -0400724
Abhilash S.L3b494632019-07-16 15:51:09 +0530725 assert(decodedSize <=- dstCapacity);
William Kurkianea869482019-04-09 15:16:11 -0400726 dst = (BYTE*)dst + decodedSize;
727 dstCapacity -= decodedSize;
728
729 src = (const BYTE*)src + frameSize;
730 srcSize -= frameSize;
731
732 continue;
733 }
734#endif
735
Abhilash S.L3b494632019-07-16 15:51:09 +0530736 { U32 const magicNumber = MEM_readLE32(src);
737 DEBUGLOG(4, "reading magic number %08X (expecting %08X)",
738 (unsigned)magicNumber, ZSTD_MAGICNUMBER);
739 if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
740 size_t const skippableSize = readSkippableFrameSize(src, srcSize);
741 if (ZSTD_isError(skippableSize))
742 return skippableSize;
743 RETURN_ERROR_IF(srcSize < skippableSize, srcSize_wrong);
William Kurkianea869482019-04-09 15:16:11 -0400744
745 src = (const BYTE *)src + skippableSize;
746 srcSize -= skippableSize;
747 continue;
Abhilash S.L3b494632019-07-16 15:51:09 +0530748 } }
William Kurkianea869482019-04-09 15:16:11 -0400749
750 if (ddict) {
751 /* we were called from ZSTD_decompress_usingDDict */
Abhilash S.L3b494632019-07-16 15:51:09 +0530752 FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(dctx, ddict));
William Kurkianea869482019-04-09 15:16:11 -0400753 } else {
754 /* this will initialize correctly with no dict if dict == NULL, so
755 * use this in all cases but ddict */
Abhilash S.L3b494632019-07-16 15:51:09 +0530756 FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize));
William Kurkianea869482019-04-09 15:16:11 -0400757 }
758 ZSTD_checkContinuity(dctx, dst);
759
760 { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity,
761 &src, &srcSize);
Abhilash S.L3b494632019-07-16 15:51:09 +0530762 RETURN_ERROR_IF(
763 (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown)
764 && (moreThan1Frame==1),
765 srcSize_wrong,
766 "at least one frame successfully completed, but following "
767 "bytes are garbage: it's more likely to be a srcSize error, "
768 "specifying more bytes than compressed size of frame(s). This "
769 "error message replaces ERROR(prefix_unknown), which would be "
770 "confusing, as the first header is actually correct. Note that "
771 "one could be unlucky, it might be a corruption error instead, "
772 "happening right at the place where we expect zstd magic "
773 "bytes. But this is _much_ less likely than a srcSize field "
774 "error.");
William Kurkianea869482019-04-09 15:16:11 -0400775 if (ZSTD_isError(res)) return res;
Abhilash S.L3b494632019-07-16 15:51:09 +0530776 assert(res <= dstCapacity);
William Kurkianea869482019-04-09 15:16:11 -0400777 dst = (BYTE*)dst + res;
778 dstCapacity -= res;
779 }
Abhilash S.L3b494632019-07-16 15:51:09 +0530780 moreThan1Frame = 1;
William Kurkianea869482019-04-09 15:16:11 -0400781 } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
782
Abhilash S.L3b494632019-07-16 15:51:09 +0530783 RETURN_ERROR_IF(srcSize, srcSize_wrong, "input not entirely consumed");
William Kurkianea869482019-04-09 15:16:11 -0400784
785 return (BYTE*)dst - (BYTE*)dststart;
786}
787
788size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
789 void* dst, size_t dstCapacity,
790 const void* src, size_t srcSize,
791 const void* dict, size_t dictSize)
792{
793 return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL);
794}
795
796
Abhilash S.L3b494632019-07-16 15:51:09 +0530797static ZSTD_DDict const* ZSTD_getDDict(ZSTD_DCtx* dctx)
798{
799 switch (dctx->dictUses) {
800 default:
801 assert(0 /* Impossible */);
802 /* fall-through */
803 case ZSTD_dont_use:
804 ZSTD_clearDict(dctx);
805 return NULL;
806 case ZSTD_use_indefinitely:
807 return dctx->ddict;
808 case ZSTD_use_once:
809 dctx->dictUses = ZSTD_dont_use;
810 return dctx->ddict;
811 }
812}
813
William Kurkianea869482019-04-09 15:16:11 -0400814size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
815{
Abhilash S.L3b494632019-07-16 15:51:09 +0530816 return ZSTD_decompress_usingDDict(dctx, dst, dstCapacity, src, srcSize, ZSTD_getDDict(dctx));
William Kurkianea869482019-04-09 15:16:11 -0400817}
818
819
820size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
821{
822#if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1)
823 size_t regenSize;
824 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
Abhilash S.L3b494632019-07-16 15:51:09 +0530825 RETURN_ERROR_IF(dctx==NULL, memory_allocation);
William Kurkianea869482019-04-09 15:16:11 -0400826 regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize);
827 ZSTD_freeDCtx(dctx);
828 return regenSize;
829#else /* stack mode */
830 ZSTD_DCtx dctx;
Abhilash S.L3b494632019-07-16 15:51:09 +0530831 ZSTD_initDCtx_internal(&dctx);
William Kurkianea869482019-04-09 15:16:11 -0400832 return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);
833#endif
834}
835
836
837/*-**************************************
838* Advanced Streaming Decompression API
839* Bufferless and synchronous
840****************************************/
841size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; }
842
843ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) {
844 switch(dctx->stage)
845 {
846 default: /* should not happen */
847 assert(0);
848 case ZSTDds_getFrameHeaderSize:
849 case ZSTDds_decodeFrameHeader:
850 return ZSTDnit_frameHeader;
851 case ZSTDds_decodeBlockHeader:
852 return ZSTDnit_blockHeader;
853 case ZSTDds_decompressBlock:
854 return ZSTDnit_block;
855 case ZSTDds_decompressLastBlock:
856 return ZSTDnit_lastBlock;
857 case ZSTDds_checkChecksum:
858 return ZSTDnit_checksum;
859 case ZSTDds_decodeSkippableHeader:
860 case ZSTDds_skipFrame:
861 return ZSTDnit_skippableFrame;
862 }
863}
864
865static int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; }
866
867/** ZSTD_decompressContinue() :
868 * srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress())
869 * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)
870 * or an error code, which can be tested using ZSTD_isError() */
871size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
872{
Abhilash S.L3b494632019-07-16 15:51:09 +0530873 DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (unsigned)srcSize);
William Kurkianea869482019-04-09 15:16:11 -0400874 /* Sanity check */
Abhilash S.L3b494632019-07-16 15:51:09 +0530875 RETURN_ERROR_IF(srcSize != dctx->expected, srcSize_wrong, "not allowed");
William Kurkianea869482019-04-09 15:16:11 -0400876 if (dstCapacity) ZSTD_checkContinuity(dctx, dst);
877
878 switch (dctx->stage)
879 {
880 case ZSTDds_getFrameHeaderSize :
881 assert(src != NULL);
882 if (dctx->format == ZSTD_f_zstd1) { /* allows header */
Abhilash S.L3b494632019-07-16 15:51:09 +0530883 assert(srcSize >= ZSTD_FRAMEIDSIZE); /* to read skippable magic number */
884 if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
William Kurkianea869482019-04-09 15:16:11 -0400885 memcpy(dctx->headerBuffer, src, srcSize);
Abhilash S.L3b494632019-07-16 15:51:09 +0530886 dctx->expected = ZSTD_SKIPPABLEHEADERSIZE - srcSize; /* remaining to load to get full skippable frame header */
William Kurkianea869482019-04-09 15:16:11 -0400887 dctx->stage = ZSTDds_decodeSkippableHeader;
888 return 0;
889 } }
890 dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format);
891 if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize;
892 memcpy(dctx->headerBuffer, src, srcSize);
893 dctx->expected = dctx->headerSize - srcSize;
894 dctx->stage = ZSTDds_decodeFrameHeader;
895 return 0;
896
897 case ZSTDds_decodeFrameHeader:
898 assert(src != NULL);
899 memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize);
Abhilash S.L3b494632019-07-16 15:51:09 +0530900 FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize));
William Kurkianea869482019-04-09 15:16:11 -0400901 dctx->expected = ZSTD_blockHeaderSize;
902 dctx->stage = ZSTDds_decodeBlockHeader;
903 return 0;
904
905 case ZSTDds_decodeBlockHeader:
906 { blockProperties_t bp;
907 size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
908 if (ZSTD_isError(cBlockSize)) return cBlockSize;
909 dctx->expected = cBlockSize;
910 dctx->bType = bp.blockType;
911 dctx->rleSize = bp.origSize;
912 if (cBlockSize) {
913 dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock;
914 return 0;
915 }
916 /* empty block */
917 if (bp.lastBlock) {
918 if (dctx->fParams.checksumFlag) {
919 dctx->expected = 4;
920 dctx->stage = ZSTDds_checkChecksum;
921 } else {
922 dctx->expected = 0; /* end of frame */
923 dctx->stage = ZSTDds_getFrameHeaderSize;
924 }
925 } else {
926 dctx->expected = ZSTD_blockHeaderSize; /* jump to next header */
927 dctx->stage = ZSTDds_decodeBlockHeader;
928 }
929 return 0;
930 }
931
932 case ZSTDds_decompressLastBlock:
933 case ZSTDds_decompressBlock:
934 DEBUGLOG(5, "ZSTD_decompressContinue: case ZSTDds_decompressBlock");
935 { size_t rSize;
936 switch(dctx->bType)
937 {
938 case bt_compressed:
939 DEBUGLOG(5, "ZSTD_decompressContinue: case bt_compressed");
940 rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 1);
941 break;
942 case bt_raw :
943 rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize);
944 break;
945 case bt_rle :
Abhilash S.L3b494632019-07-16 15:51:09 +0530946 rSize = ZSTD_setRleBlock(dst, dstCapacity, *(const BYTE*)src, dctx->rleSize);
William Kurkianea869482019-04-09 15:16:11 -0400947 break;
948 case bt_reserved : /* should never happen */
949 default:
Abhilash S.L3b494632019-07-16 15:51:09 +0530950 RETURN_ERROR(corruption_detected);
William Kurkianea869482019-04-09 15:16:11 -0400951 }
952 if (ZSTD_isError(rSize)) return rSize;
Abhilash S.L3b494632019-07-16 15:51:09 +0530953 DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (unsigned)rSize);
William Kurkianea869482019-04-09 15:16:11 -0400954 dctx->decodedSize += rSize;
955 if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, dst, rSize);
956
957 if (dctx->stage == ZSTDds_decompressLastBlock) { /* end of frame */
Abhilash S.L3b494632019-07-16 15:51:09 +0530958 DEBUGLOG(4, "ZSTD_decompressContinue: decoded size from frame : %u", (unsigned)dctx->decodedSize);
959 RETURN_ERROR_IF(
960 dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
961 && dctx->decodedSize != dctx->fParams.frameContentSize,
962 corruption_detected);
William Kurkianea869482019-04-09 15:16:11 -0400963 if (dctx->fParams.checksumFlag) { /* another round for frame checksum */
964 dctx->expected = 4;
965 dctx->stage = ZSTDds_checkChecksum;
966 } else {
967 dctx->expected = 0; /* ends here */
968 dctx->stage = ZSTDds_getFrameHeaderSize;
969 }
970 } else {
971 dctx->stage = ZSTDds_decodeBlockHeader;
972 dctx->expected = ZSTD_blockHeaderSize;
973 dctx->previousDstEnd = (char*)dst + rSize;
974 }
975 return rSize;
976 }
977
978 case ZSTDds_checkChecksum:
979 assert(srcSize == 4); /* guaranteed by dctx->expected */
980 { U32 const h32 = (U32)XXH64_digest(&dctx->xxhState);
981 U32 const check32 = MEM_readLE32(src);
Abhilash S.L3b494632019-07-16 15:51:09 +0530982 DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", (unsigned)h32, (unsigned)check32);
983 RETURN_ERROR_IF(check32 != h32, checksum_wrong);
William Kurkianea869482019-04-09 15:16:11 -0400984 dctx->expected = 0;
985 dctx->stage = ZSTDds_getFrameHeaderSize;
986 return 0;
987 }
988
989 case ZSTDds_decodeSkippableHeader:
990 assert(src != NULL);
Abhilash S.L3b494632019-07-16 15:51:09 +0530991 assert(srcSize <= ZSTD_SKIPPABLEHEADERSIZE);
992 memcpy(dctx->headerBuffer + (ZSTD_SKIPPABLEHEADERSIZE - srcSize), src, srcSize); /* complete skippable header */
993 dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_FRAMEIDSIZE); /* note : dctx->expected can grow seriously large, beyond local buffer size */
William Kurkianea869482019-04-09 15:16:11 -0400994 dctx->stage = ZSTDds_skipFrame;
995 return 0;
996
997 case ZSTDds_skipFrame:
998 dctx->expected = 0;
999 dctx->stage = ZSTDds_getFrameHeaderSize;
1000 return 0;
1001
1002 default:
Abhilash S.L3b494632019-07-16 15:51:09 +05301003 assert(0); /* impossible */
1004 RETURN_ERROR(GENERIC); /* some compiler require default to do something */
William Kurkianea869482019-04-09 15:16:11 -04001005 }
1006}
1007
1008
1009static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1010{
1011 dctx->dictEnd = dctx->previousDstEnd;
Abhilash S.L3b494632019-07-16 15:51:09 +05301012 dctx->virtualStart = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
1013 dctx->prefixStart = dict;
William Kurkianea869482019-04-09 15:16:11 -04001014 dctx->previousDstEnd = (const char*)dict + dictSize;
1015 return 0;
1016}
1017
Abhilash S.L3b494632019-07-16 15:51:09 +05301018/*! ZSTD_loadDEntropy() :
1019 * dict : must point at beginning of a valid zstd dictionary.
William Kurkianea869482019-04-09 15:16:11 -04001020 * @return : size of entropy tables read */
Abhilash S.L3b494632019-07-16 15:51:09 +05301021size_t
1022ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
1023 const void* const dict, size_t const dictSize)
William Kurkianea869482019-04-09 15:16:11 -04001024{
1025 const BYTE* dictPtr = (const BYTE*)dict;
1026 const BYTE* const dictEnd = dictPtr + dictSize;
1027
Abhilash S.L3b494632019-07-16 15:51:09 +05301028 RETURN_ERROR_IF(dictSize <= 8, dictionary_corrupted);
1029 assert(MEM_readLE32(dict) == ZSTD_MAGIC_DICTIONARY); /* dict must be valid */
William Kurkianea869482019-04-09 15:16:11 -04001030 dictPtr += 8; /* skip header = magic + dictID */
1031
Abhilash S.L3b494632019-07-16 15:51:09 +05301032 ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, OFTable) == offsetof(ZSTD_entropyDTables_t, LLTable) + sizeof(entropy->LLTable));
1033 ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, MLTable) == offsetof(ZSTD_entropyDTables_t, OFTable) + sizeof(entropy->OFTable));
1034 ZSTD_STATIC_ASSERT(sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable) >= HUF_DECOMPRESS_WORKSPACE_SIZE);
1035 { void* const workspace = &entropy->LLTable; /* use fse tables as temporary workspace; implies fse tables are grouped together */
1036 size_t const workspaceSize = sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable);
1037#ifdef HUF_FORCE_DECOMPRESS_X1
1038 /* in minimal huffman, we always use X1 variants */
1039 size_t const hSize = HUF_readDTableX1_wksp(entropy->hufTable,
1040 dictPtr, dictEnd - dictPtr,
1041 workspace, workspaceSize);
1042#else
1043 size_t const hSize = HUF_readDTableX2_wksp(entropy->hufTable,
1044 dictPtr, dictEnd - dictPtr,
1045 workspace, workspaceSize);
1046#endif
1047 RETURN_ERROR_IF(HUF_isError(hSize), dictionary_corrupted);
William Kurkianea869482019-04-09 15:16:11 -04001048 dictPtr += hSize;
1049 }
1050
1051 { short offcodeNCount[MaxOff+1];
Abhilash S.L3b494632019-07-16 15:51:09 +05301052 unsigned offcodeMaxValue = MaxOff, offcodeLog;
William Kurkianea869482019-04-09 15:16:11 -04001053 size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr);
Abhilash S.L3b494632019-07-16 15:51:09 +05301054 RETURN_ERROR_IF(FSE_isError(offcodeHeaderSize), dictionary_corrupted);
1055 RETURN_ERROR_IF(offcodeMaxValue > MaxOff, dictionary_corrupted);
1056 RETURN_ERROR_IF(offcodeLog > OffFSELog, dictionary_corrupted);
1057 ZSTD_buildFSETable( entropy->OFTable,
William Kurkianea869482019-04-09 15:16:11 -04001058 offcodeNCount, offcodeMaxValue,
1059 OF_base, OF_bits,
1060 offcodeLog);
1061 dictPtr += offcodeHeaderSize;
1062 }
1063
1064 { short matchlengthNCount[MaxML+1];
1065 unsigned matchlengthMaxValue = MaxML, matchlengthLog;
1066 size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr);
Abhilash S.L3b494632019-07-16 15:51:09 +05301067 RETURN_ERROR_IF(FSE_isError(matchlengthHeaderSize), dictionary_corrupted);
1068 RETURN_ERROR_IF(matchlengthMaxValue > MaxML, dictionary_corrupted);
1069 RETURN_ERROR_IF(matchlengthLog > MLFSELog, dictionary_corrupted);
1070 ZSTD_buildFSETable( entropy->MLTable,
William Kurkianea869482019-04-09 15:16:11 -04001071 matchlengthNCount, matchlengthMaxValue,
1072 ML_base, ML_bits,
1073 matchlengthLog);
1074 dictPtr += matchlengthHeaderSize;
1075 }
1076
1077 { short litlengthNCount[MaxLL+1];
1078 unsigned litlengthMaxValue = MaxLL, litlengthLog;
1079 size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr);
Abhilash S.L3b494632019-07-16 15:51:09 +05301080 RETURN_ERROR_IF(FSE_isError(litlengthHeaderSize), dictionary_corrupted);
1081 RETURN_ERROR_IF(litlengthMaxValue > MaxLL, dictionary_corrupted);
1082 RETURN_ERROR_IF(litlengthLog > LLFSELog, dictionary_corrupted);
1083 ZSTD_buildFSETable( entropy->LLTable,
William Kurkianea869482019-04-09 15:16:11 -04001084 litlengthNCount, litlengthMaxValue,
1085 LL_base, LL_bits,
1086 litlengthLog);
1087 dictPtr += litlengthHeaderSize;
1088 }
1089
Abhilash S.L3b494632019-07-16 15:51:09 +05301090 RETURN_ERROR_IF(dictPtr+12 > dictEnd, dictionary_corrupted);
William Kurkianea869482019-04-09 15:16:11 -04001091 { int i;
1092 size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12));
1093 for (i=0; i<3; i++) {
1094 U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4;
Abhilash S.L3b494632019-07-16 15:51:09 +05301095 RETURN_ERROR_IF(rep==0 || rep >= dictContentSize,
1096 dictionary_corrupted);
William Kurkianea869482019-04-09 15:16:11 -04001097 entropy->rep[i] = rep;
1098 } }
1099
1100 return dictPtr - (const BYTE*)dict;
1101}
1102
1103static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1104{
1105 if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize);
1106 { U32 const magic = MEM_readLE32(dict);
1107 if (magic != ZSTD_MAGIC_DICTIONARY) {
1108 return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */
1109 } }
Abhilash S.L3b494632019-07-16 15:51:09 +05301110 dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
William Kurkianea869482019-04-09 15:16:11 -04001111
1112 /* load entropy tables */
Abhilash S.L3b494632019-07-16 15:51:09 +05301113 { size_t const eSize = ZSTD_loadDEntropy(&dctx->entropy, dict, dictSize);
1114 RETURN_ERROR_IF(ZSTD_isError(eSize), dictionary_corrupted);
William Kurkianea869482019-04-09 15:16:11 -04001115 dict = (const char*)dict + eSize;
1116 dictSize -= eSize;
1117 }
1118 dctx->litEntropy = dctx->fseEntropy = 1;
1119
1120 /* reference dictionary content */
1121 return ZSTD_refDictContent(dctx, dict, dictSize);
1122}
1123
William Kurkianea869482019-04-09 15:16:11 -04001124size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
1125{
1126 assert(dctx != NULL);
1127 dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */
1128 dctx->stage = ZSTDds_getFrameHeaderSize;
1129 dctx->decodedSize = 0;
1130 dctx->previousDstEnd = NULL;
Abhilash S.L3b494632019-07-16 15:51:09 +05301131 dctx->prefixStart = NULL;
1132 dctx->virtualStart = NULL;
William Kurkianea869482019-04-09 15:16:11 -04001133 dctx->dictEnd = NULL;
1134 dctx->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */
1135 dctx->litEntropy = dctx->fseEntropy = 0;
1136 dctx->dictID = 0;
1137 ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue));
1138 memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */
1139 dctx->LLTptr = dctx->entropy.LLTable;
1140 dctx->MLTptr = dctx->entropy.MLTable;
1141 dctx->OFTptr = dctx->entropy.OFTable;
1142 dctx->HUFptr = dctx->entropy.hufTable;
1143 return 0;
1144}
1145
1146size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1147{
Abhilash S.L3b494632019-07-16 15:51:09 +05301148 FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) );
William Kurkianea869482019-04-09 15:16:11 -04001149 if (dict && dictSize)
Abhilash S.L3b494632019-07-16 15:51:09 +05301150 RETURN_ERROR_IF(
1151 ZSTD_isError(ZSTD_decompress_insertDictionary(dctx, dict, dictSize)),
1152 dictionary_corrupted);
William Kurkianea869482019-04-09 15:16:11 -04001153 return 0;
1154}
1155
1156
1157/* ====== ZSTD_DDict ====== */
1158
Abhilash S.L3b494632019-07-16 15:51:09 +05301159size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
William Kurkianea869482019-04-09 15:16:11 -04001160{
Abhilash S.L3b494632019-07-16 15:51:09 +05301161 DEBUGLOG(4, "ZSTD_decompressBegin_usingDDict");
1162 assert(dctx != NULL);
1163 if (ddict) {
1164 const char* const dictStart = (const char*)ZSTD_DDict_dictContent(ddict);
1165 size_t const dictSize = ZSTD_DDict_dictSize(ddict);
1166 const void* const dictEnd = dictStart + dictSize;
1167 dctx->ddictIsCold = (dctx->dictEnd != dictEnd);
1168 DEBUGLOG(4, "DDict is %s",
1169 dctx->ddictIsCold ? "~cold~" : "hot!");
1170 }
1171 FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) );
1172 if (ddict) { /* NULL ddict is equivalent to no dictionary */
1173 ZSTD_copyDDictParameters(dctx, ddict);
William Kurkianea869482019-04-09 15:16:11 -04001174 }
1175 return 0;
1176}
1177
William Kurkianea869482019-04-09 15:16:11 -04001178/*! ZSTD_getDictID_fromDict() :
1179 * Provides the dictID stored within dictionary.
1180 * if @return == 0, the dictionary is not conformant with Zstandard specification.
1181 * It can still be loaded, but as a content-only dictionary. */
1182unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize)
1183{
1184 if (dictSize < 8) return 0;
1185 if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0;
Abhilash S.L3b494632019-07-16 15:51:09 +05301186 return MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
William Kurkianea869482019-04-09 15:16:11 -04001187}
1188
1189/*! ZSTD_getDictID_fromFrame() :
Abhilash S.L3b494632019-07-16 15:51:09 +05301190 * Provides the dictID required to decompress frame stored within `src`.
William Kurkianea869482019-04-09 15:16:11 -04001191 * If @return == 0, the dictID could not be decoded.
1192 * This could for one of the following reasons :
1193 * - The frame does not require a dictionary (most common case).
1194 * - The frame was built with dictID intentionally removed.
1195 * Needed dictionary is a hidden information.
1196 * Note : this use case also happens when using a non-conformant dictionary.
1197 * - `srcSize` is too small, and as a result, frame header could not be decoded.
1198 * Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`.
1199 * - This is not a Zstandard frame.
1200 * When identifying the exact failure cause, it's possible to use
1201 * ZSTD_getFrameHeader(), which will provide a more precise error code. */
1202unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize)
1203{
1204 ZSTD_frameHeader zfp = { 0, 0, 0, ZSTD_frame, 0, 0, 0 };
1205 size_t const hError = ZSTD_getFrameHeader(&zfp, src, srcSize);
1206 if (ZSTD_isError(hError)) return 0;
1207 return zfp.dictID;
1208}
1209
1210
1211/*! ZSTD_decompress_usingDDict() :
1212* Decompression using a pre-digested Dictionary
1213* Use dictionary without significant overhead. */
1214size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
1215 void* dst, size_t dstCapacity,
1216 const void* src, size_t srcSize,
1217 const ZSTD_DDict* ddict)
1218{
1219 /* pass content and size in case legacy frames are encountered */
1220 return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize,
1221 NULL, 0,
1222 ddict);
1223}
1224
1225
1226/*=====================================
1227* Streaming decompression
1228*====================================*/
1229
1230ZSTD_DStream* ZSTD_createDStream(void)
1231{
1232 DEBUGLOG(3, "ZSTD_createDStream");
1233 return ZSTD_createDStream_advanced(ZSTD_defaultCMem);
1234}
1235
1236ZSTD_DStream* ZSTD_initStaticDStream(void *workspace, size_t workspaceSize)
1237{
1238 return ZSTD_initStaticDCtx(workspace, workspaceSize);
1239}
1240
1241ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem)
1242{
1243 return ZSTD_createDCtx_advanced(customMem);
1244}
1245
1246size_t ZSTD_freeDStream(ZSTD_DStream* zds)
1247{
1248 return ZSTD_freeDCtx(zds);
1249}
1250
1251
Abhilash S.L3b494632019-07-16 15:51:09 +05301252/* *** Initialization *** */
William Kurkianea869482019-04-09 15:16:11 -04001253
1254size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; }
1255size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; }
1256
Abhilash S.L3b494632019-07-16 15:51:09 +05301257size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx,
1258 const void* dict, size_t dictSize,
1259 ZSTD_dictLoadMethod_e dictLoadMethod,
1260 ZSTD_dictContentType_e dictContentType)
William Kurkianea869482019-04-09 15:16:11 -04001261{
Abhilash S.L3b494632019-07-16 15:51:09 +05301262 RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong);
1263 ZSTD_clearDict(dctx);
William Kurkianea869482019-04-09 15:16:11 -04001264 if (dict && dictSize >= 8) {
1265 dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem);
Abhilash S.L3b494632019-07-16 15:51:09 +05301266 RETURN_ERROR_IF(dctx->ddictLocal == NULL, memory_allocation);
1267 dctx->ddict = dctx->ddictLocal;
1268 dctx->dictUses = ZSTD_use_indefinitely;
William Kurkianea869482019-04-09 15:16:11 -04001269 }
William Kurkianea869482019-04-09 15:16:11 -04001270 return 0;
1271}
1272
1273size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1274{
1275 return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto);
1276}
1277
1278size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
1279{
1280 return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto);
1281}
1282
1283size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType)
1284{
Abhilash S.L3b494632019-07-16 15:51:09 +05301285 FORWARD_IF_ERROR(ZSTD_DCtx_loadDictionary_advanced(dctx, prefix, prefixSize, ZSTD_dlm_byRef, dictContentType));
1286 dctx->dictUses = ZSTD_use_once;
1287 return 0;
William Kurkianea869482019-04-09 15:16:11 -04001288}
1289
1290size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize)
1291{
1292 return ZSTD_DCtx_refPrefix_advanced(dctx, prefix, prefixSize, ZSTD_dct_rawContent);
1293}
1294
1295
1296/* ZSTD_initDStream_usingDict() :
Abhilash S.L3b494632019-07-16 15:51:09 +05301297 * return : expected size, aka ZSTD_FRAMEHEADERSIZE_PREFIX.
William Kurkianea869482019-04-09 15:16:11 -04001298 * this function cannot fail */
1299size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize)
1300{
1301 DEBUGLOG(4, "ZSTD_initDStream_usingDict");
Abhilash S.L3b494632019-07-16 15:51:09 +05301302 FORWARD_IF_ERROR( ZSTD_DCtx_reset(zds, ZSTD_reset_session_only) );
1303 FORWARD_IF_ERROR( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) );
1304 return ZSTD_FRAMEHEADERSIZE_PREFIX;
William Kurkianea869482019-04-09 15:16:11 -04001305}
1306
1307/* note : this variant can't fail */
1308size_t ZSTD_initDStream(ZSTD_DStream* zds)
1309{
1310 DEBUGLOG(4, "ZSTD_initDStream");
Abhilash S.L3b494632019-07-16 15:51:09 +05301311 return ZSTD_initDStream_usingDDict(zds, NULL);
William Kurkianea869482019-04-09 15:16:11 -04001312}
1313
1314/* ZSTD_initDStream_usingDDict() :
1315 * ddict will just be referenced, and must outlive decompression session
1316 * this function cannot fail */
1317size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* dctx, const ZSTD_DDict* ddict)
1318{
Abhilash S.L3b494632019-07-16 15:51:09 +05301319 FORWARD_IF_ERROR( ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only) );
1320 FORWARD_IF_ERROR( ZSTD_DCtx_refDDict(dctx, ddict) );
1321 return ZSTD_FRAMEHEADERSIZE_PREFIX;
William Kurkianea869482019-04-09 15:16:11 -04001322}
1323
1324/* ZSTD_resetDStream() :
Abhilash S.L3b494632019-07-16 15:51:09 +05301325 * return : expected size, aka ZSTD_FRAMEHEADERSIZE_PREFIX.
William Kurkianea869482019-04-09 15:16:11 -04001326 * this function cannot fail */
1327size_t ZSTD_resetDStream(ZSTD_DStream* dctx)
1328{
Abhilash S.L3b494632019-07-16 15:51:09 +05301329 FORWARD_IF_ERROR(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only));
1330 return ZSTD_FRAMEHEADERSIZE_PREFIX;
William Kurkianea869482019-04-09 15:16:11 -04001331}
1332
Abhilash S.L3b494632019-07-16 15:51:09 +05301333
1334size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
William Kurkianea869482019-04-09 15:16:11 -04001335{
Abhilash S.L3b494632019-07-16 15:51:09 +05301336 RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong);
1337 ZSTD_clearDict(dctx);
1338 if (ddict) {
1339 dctx->ddict = ddict;
1340 dctx->dictUses = ZSTD_use_indefinitely;
William Kurkianea869482019-04-09 15:16:11 -04001341 }
1342 return 0;
1343}
1344
Abhilash S.L3b494632019-07-16 15:51:09 +05301345/* ZSTD_DCtx_setMaxWindowSize() :
1346 * note : no direct equivalence in ZSTD_DCtx_setParameter,
1347 * since this version sets windowSize, and the other sets windowLog */
William Kurkianea869482019-04-09 15:16:11 -04001348size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize)
1349{
Abhilash S.L3b494632019-07-16 15:51:09 +05301350 ZSTD_bounds const bounds = ZSTD_dParam_getBounds(ZSTD_d_windowLogMax);
1351 size_t const min = (size_t)1 << bounds.lowerBound;
1352 size_t const max = (size_t)1 << bounds.upperBound;
1353 RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong);
1354 RETURN_ERROR_IF(maxWindowSize < min, parameter_outOfBound);
1355 RETURN_ERROR_IF(maxWindowSize > max, parameter_outOfBound);
William Kurkianea869482019-04-09 15:16:11 -04001356 dctx->maxWindowSize = maxWindowSize;
1357 return 0;
1358}
1359
1360size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format)
1361{
Abhilash S.L3b494632019-07-16 15:51:09 +05301362 return ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, format);
1363}
1364
1365ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam)
1366{
1367 ZSTD_bounds bounds = { 0, 0, 0 };
1368 switch(dParam) {
1369 case ZSTD_d_windowLogMax:
1370 bounds.lowerBound = ZSTD_WINDOWLOG_ABSOLUTEMIN;
1371 bounds.upperBound = ZSTD_WINDOWLOG_MAX;
1372 return bounds;
1373 case ZSTD_d_format:
1374 bounds.lowerBound = (int)ZSTD_f_zstd1;
1375 bounds.upperBound = (int)ZSTD_f_zstd1_magicless;
1376 ZSTD_STATIC_ASSERT(ZSTD_f_zstd1 < ZSTD_f_zstd1_magicless);
1377 return bounds;
1378 default:;
1379 }
1380 bounds.error = ERROR(parameter_unsupported);
1381 return bounds;
1382}
1383
1384/* ZSTD_dParam_withinBounds:
1385 * @return 1 if value is within dParam bounds,
1386 * 0 otherwise */
1387static int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam, int value)
1388{
1389 ZSTD_bounds const bounds = ZSTD_dParam_getBounds(dParam);
1390 if (ZSTD_isError(bounds.error)) return 0;
1391 if (value < bounds.lowerBound) return 0;
1392 if (value > bounds.upperBound) return 0;
1393 return 1;
1394}
1395
1396#define CHECK_DBOUNDS(p,v) { \
1397 RETURN_ERROR_IF(!ZSTD_dParam_withinBounds(p, v), parameter_outOfBound); \
1398}
1399
1400size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value)
1401{
1402 RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong);
1403 switch(dParam) {
1404 case ZSTD_d_windowLogMax:
1405 if (value == 0) value = ZSTD_WINDOWLOG_LIMIT_DEFAULT;
1406 CHECK_DBOUNDS(ZSTD_d_windowLogMax, value);
1407 dctx->maxWindowSize = ((size_t)1) << value;
1408 return 0;
1409 case ZSTD_d_format:
1410 CHECK_DBOUNDS(ZSTD_d_format, value);
1411 dctx->format = (ZSTD_format_e)value;
1412 return 0;
1413 default:;
1414 }
1415 RETURN_ERROR(parameter_unsupported);
1416}
1417
1418size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset)
1419{
1420 if ( (reset == ZSTD_reset_session_only)
1421 || (reset == ZSTD_reset_session_and_parameters) ) {
1422 dctx->streamStage = zdss_init;
1423 dctx->noForwardProgress = 0;
1424 }
1425 if ( (reset == ZSTD_reset_parameters)
1426 || (reset == ZSTD_reset_session_and_parameters) ) {
1427 RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong);
1428 ZSTD_clearDict(dctx);
1429 dctx->format = ZSTD_f_zstd1;
1430 dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
1431 }
William Kurkianea869482019-04-09 15:16:11 -04001432 return 0;
1433}
1434
1435
1436size_t ZSTD_sizeof_DStream(const ZSTD_DStream* dctx)
1437{
1438 return ZSTD_sizeof_DCtx(dctx);
1439}
1440
1441size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize)
1442{
1443 size_t const blockSize = (size_t) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
1444 unsigned long long const neededRBSize = windowSize + blockSize + (WILDCOPY_OVERLENGTH * 2);
1445 unsigned long long const neededSize = MIN(frameContentSize, neededRBSize);
1446 size_t const minRBSize = (size_t) neededSize;
Abhilash S.L3b494632019-07-16 15:51:09 +05301447 RETURN_ERROR_IF((unsigned long long)minRBSize != neededSize,
1448 frameParameter_windowTooLarge);
William Kurkianea869482019-04-09 15:16:11 -04001449 return minRBSize;
1450}
1451
1452size_t ZSTD_estimateDStreamSize(size_t windowSize)
1453{
1454 size_t const blockSize = MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
1455 size_t const inBuffSize = blockSize; /* no block can be larger */
1456 size_t const outBuffSize = ZSTD_decodingBufferSize_min(windowSize, ZSTD_CONTENTSIZE_UNKNOWN);
1457 return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize;
1458}
1459
1460size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize)
1461{
Abhilash S.L3b494632019-07-16 15:51:09 +05301462 U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX; /* note : should be user-selectable, but requires an additional parameter (or a dctx) */
William Kurkianea869482019-04-09 15:16:11 -04001463 ZSTD_frameHeader zfh;
1464 size_t const err = ZSTD_getFrameHeader(&zfh, src, srcSize);
1465 if (ZSTD_isError(err)) return err;
Abhilash S.L3b494632019-07-16 15:51:09 +05301466 RETURN_ERROR_IF(err>0, srcSize_wrong);
1467 RETURN_ERROR_IF(zfh.windowSize > windowSizeMax,
1468 frameParameter_windowTooLarge);
William Kurkianea869482019-04-09 15:16:11 -04001469 return ZSTD_estimateDStreamSize((size_t)zfh.windowSize);
1470}
1471
1472
1473/* ***** Decompression ***** */
1474
1475MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
1476{
1477 size_t const length = MIN(dstCapacity, srcSize);
1478 memcpy(dst, src, length);
1479 return length;
1480}
1481
1482
1483size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
1484{
1485 const char* const istart = (const char*)(input->src) + input->pos;
1486 const char* const iend = (const char*)(input->src) + input->size;
1487 const char* ip = istart;
1488 char* const ostart = (char*)(output->dst) + output->pos;
1489 char* const oend = (char*)(output->dst) + output->size;
1490 char* op = ostart;
1491 U32 someMoreWork = 1;
1492
1493 DEBUGLOG(5, "ZSTD_decompressStream");
Abhilash S.L3b494632019-07-16 15:51:09 +05301494 RETURN_ERROR_IF(
1495 input->pos > input->size,
1496 srcSize_wrong,
1497 "forbidden. in: pos: %u vs size: %u",
1498 (U32)input->pos, (U32)input->size);
1499 RETURN_ERROR_IF(
1500 output->pos > output->size,
1501 dstSize_tooSmall,
1502 "forbidden. out: pos: %u vs size: %u",
1503 (U32)output->pos, (U32)output->size);
William Kurkianea869482019-04-09 15:16:11 -04001504 DEBUGLOG(5, "input size : %u", (U32)(input->size - input->pos));
1505
1506 while (someMoreWork) {
1507 switch(zds->streamStage)
1508 {
1509 case zdss_init :
1510 DEBUGLOG(5, "stage zdss_init => transparent reset ");
Abhilash S.L3b494632019-07-16 15:51:09 +05301511 zds->streamStage = zdss_loadHeader;
1512 zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
1513 zds->legacyVersion = 0;
1514 zds->hostageByte = 0;
William Kurkianea869482019-04-09 15:16:11 -04001515 /* fall-through */
1516
1517 case zdss_loadHeader :
1518 DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32)(iend - ip));
1519#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
1520 if (zds->legacyVersion) {
Abhilash S.L3b494632019-07-16 15:51:09 +05301521 RETURN_ERROR_IF(zds->staticSize, memory_allocation,
1522 "legacy support is incompatible with static dctx");
William Kurkianea869482019-04-09 15:16:11 -04001523 { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input);
1524 if (hint==0) zds->streamStage = zdss_init;
1525 return hint;
1526 } }
1527#endif
Abhilash S.L3b494632019-07-16 15:51:09 +05301528 { size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format);
William Kurkianea869482019-04-09 15:16:11 -04001529 DEBUGLOG(5, "header size : %u", (U32)hSize);
1530 if (ZSTD_isError(hSize)) {
1531#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
1532 U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart);
1533 if (legacyVersion) {
Abhilash S.L3b494632019-07-16 15:51:09 +05301534 ZSTD_DDict const* const ddict = ZSTD_getDDict(zds);
1535 const void* const dict = ddict ? ZSTD_DDict_dictContent(ddict) : NULL;
1536 size_t const dictSize = ddict ? ZSTD_DDict_dictSize(ddict) : 0;
William Kurkianea869482019-04-09 15:16:11 -04001537 DEBUGLOG(5, "ZSTD_decompressStream: detected legacy version v0.%u", legacyVersion);
Abhilash S.L3b494632019-07-16 15:51:09 +05301538 RETURN_ERROR_IF(zds->staticSize, memory_allocation,
1539 "legacy support is incompatible with static dctx");
1540 FORWARD_IF_ERROR(ZSTD_initLegacyStream(&zds->legacyContext,
William Kurkianea869482019-04-09 15:16:11 -04001541 zds->previousLegacyVersion, legacyVersion,
1542 dict, dictSize));
1543 zds->legacyVersion = zds->previousLegacyVersion = legacyVersion;
1544 { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, legacyVersion, output, input);
1545 if (hint==0) zds->streamStage = zdss_init; /* or stay in stage zdss_loadHeader */
1546 return hint;
1547 } }
1548#endif
1549 return hSize; /* error */
1550 }
1551 if (hSize != 0) { /* need more input */
1552 size_t const toLoad = hSize - zds->lhSize; /* if hSize!=0, hSize > zds->lhSize */
1553 size_t const remainingInput = (size_t)(iend-ip);
1554 assert(iend >= ip);
1555 if (toLoad > remainingInput) { /* not enough input to load full header */
1556 if (remainingInput > 0) {
1557 memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput);
1558 zds->lhSize += remainingInput;
1559 }
1560 input->pos = input->size;
Abhilash S.L3b494632019-07-16 15:51:09 +05301561 return (MAX(ZSTD_FRAMEHEADERSIZE_MIN, hSize) - zds->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */
William Kurkianea869482019-04-09 15:16:11 -04001562 }
1563 assert(ip != NULL);
1564 memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad;
1565 break;
1566 } }
1567
1568 /* check for single-pass mode opportunity */
1569 if (zds->fParams.frameContentSize && zds->fParams.windowSize /* skippable frame if == 0 */
1570 && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) {
1571 size_t const cSize = ZSTD_findFrameCompressedSize(istart, iend-istart);
1572 if (cSize <= (size_t)(iend-istart)) {
1573 /* shortcut : using single-pass mode */
Abhilash S.L3b494632019-07-16 15:51:09 +05301574 size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, oend-op, istart, cSize, ZSTD_getDDict(zds));
William Kurkianea869482019-04-09 15:16:11 -04001575 if (ZSTD_isError(decompressedSize)) return decompressedSize;
1576 DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()")
1577 ip = istart + cSize;
1578 op += decompressedSize;
1579 zds->expected = 0;
1580 zds->streamStage = zdss_init;
1581 someMoreWork = 0;
1582 break;
1583 } }
1584
1585 /* Consume header (see ZSTDds_decodeFrameHeader) */
1586 DEBUGLOG(4, "Consume header");
Abhilash S.L3b494632019-07-16 15:51:09 +05301587 FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(zds, ZSTD_getDDict(zds)));
William Kurkianea869482019-04-09 15:16:11 -04001588
Abhilash S.L3b494632019-07-16 15:51:09 +05301589 if ((MEM_readLE32(zds->headerBuffer) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
1590 zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_FRAMEIDSIZE);
William Kurkianea869482019-04-09 15:16:11 -04001591 zds->stage = ZSTDds_skipFrame;
1592 } else {
Abhilash S.L3b494632019-07-16 15:51:09 +05301593 FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize));
William Kurkianea869482019-04-09 15:16:11 -04001594 zds->expected = ZSTD_blockHeaderSize;
1595 zds->stage = ZSTDds_decodeBlockHeader;
1596 }
1597
1598 /* control buffer memory usage */
1599 DEBUGLOG(4, "Control max memory usage (%u KB <= max %u KB)",
1600 (U32)(zds->fParams.windowSize >>10),
1601 (U32)(zds->maxWindowSize >> 10) );
1602 zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN);
Abhilash S.L3b494632019-07-16 15:51:09 +05301603 RETURN_ERROR_IF(zds->fParams.windowSize > zds->maxWindowSize,
1604 frameParameter_windowTooLarge);
William Kurkianea869482019-04-09 15:16:11 -04001605
1606 /* Adapt buffer sizes to frame header instructions */
1607 { size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */);
1608 size_t const neededOutBuffSize = ZSTD_decodingBufferSize_min(zds->fParams.windowSize, zds->fParams.frameContentSize);
1609 if ((zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize)) {
1610 size_t const bufferSize = neededInBuffSize + neededOutBuffSize;
1611 DEBUGLOG(4, "inBuff : from %u to %u",
1612 (U32)zds->inBuffSize, (U32)neededInBuffSize);
1613 DEBUGLOG(4, "outBuff : from %u to %u",
1614 (U32)zds->outBuffSize, (U32)neededOutBuffSize);
1615 if (zds->staticSize) { /* static DCtx */
1616 DEBUGLOG(4, "staticSize : %u", (U32)zds->staticSize);
1617 assert(zds->staticSize >= sizeof(ZSTD_DCtx)); /* controlled at init */
Abhilash S.L3b494632019-07-16 15:51:09 +05301618 RETURN_ERROR_IF(
1619 bufferSize > zds->staticSize - sizeof(ZSTD_DCtx),
1620 memory_allocation);
William Kurkianea869482019-04-09 15:16:11 -04001621 } else {
1622 ZSTD_free(zds->inBuff, zds->customMem);
1623 zds->inBuffSize = 0;
1624 zds->outBuffSize = 0;
1625 zds->inBuff = (char*)ZSTD_malloc(bufferSize, zds->customMem);
Abhilash S.L3b494632019-07-16 15:51:09 +05301626 RETURN_ERROR_IF(zds->inBuff == NULL, memory_allocation);
William Kurkianea869482019-04-09 15:16:11 -04001627 }
1628 zds->inBuffSize = neededInBuffSize;
1629 zds->outBuff = zds->inBuff + zds->inBuffSize;
1630 zds->outBuffSize = neededOutBuffSize;
1631 } }
1632 zds->streamStage = zdss_read;
1633 /* fall-through */
1634
1635 case zdss_read:
1636 DEBUGLOG(5, "stage zdss_read");
1637 { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds);
1638 DEBUGLOG(5, "neededInSize = %u", (U32)neededInSize);
1639 if (neededInSize==0) { /* end of frame */
1640 zds->streamStage = zdss_init;
1641 someMoreWork = 0;
1642 break;
1643 }
1644 if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */
1645 int const isSkipFrame = ZSTD_isSkipFrame(zds);
1646 size_t const decodedSize = ZSTD_decompressContinue(zds,
1647 zds->outBuff + zds->outStart, (isSkipFrame ? 0 : zds->outBuffSize - zds->outStart),
1648 ip, neededInSize);
1649 if (ZSTD_isError(decodedSize)) return decodedSize;
1650 ip += neededInSize;
1651 if (!decodedSize && !isSkipFrame) break; /* this was just a header */
1652 zds->outEnd = zds->outStart + decodedSize;
1653 zds->streamStage = zdss_flush;
1654 break;
1655 } }
1656 if (ip==iend) { someMoreWork = 0; break; } /* no more input */
1657 zds->streamStage = zdss_load;
1658 /* fall-through */
1659
1660 case zdss_load:
1661 { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds);
1662 size_t const toLoad = neededInSize - zds->inPos;
1663 int const isSkipFrame = ZSTD_isSkipFrame(zds);
1664 size_t loadedSize;
1665 if (isSkipFrame) {
1666 loadedSize = MIN(toLoad, (size_t)(iend-ip));
1667 } else {
Abhilash S.L3b494632019-07-16 15:51:09 +05301668 RETURN_ERROR_IF(toLoad > zds->inBuffSize - zds->inPos,
1669 corruption_detected,
1670 "should never happen");
William Kurkianea869482019-04-09 15:16:11 -04001671 loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, iend-ip);
1672 }
1673 ip += loadedSize;
1674 zds->inPos += loadedSize;
1675 if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */
1676
1677 /* decode loaded input */
1678 { size_t const decodedSize = ZSTD_decompressContinue(zds,
1679 zds->outBuff + zds->outStart, zds->outBuffSize - zds->outStart,
1680 zds->inBuff, neededInSize);
1681 if (ZSTD_isError(decodedSize)) return decodedSize;
1682 zds->inPos = 0; /* input is consumed */
1683 if (!decodedSize && !isSkipFrame) { zds->streamStage = zdss_read; break; } /* this was just a header */
1684 zds->outEnd = zds->outStart + decodedSize;
1685 } }
1686 zds->streamStage = zdss_flush;
1687 /* fall-through */
1688
1689 case zdss_flush:
1690 { size_t const toFlushSize = zds->outEnd - zds->outStart;
1691 size_t const flushedSize = ZSTD_limitCopy(op, oend-op, zds->outBuff + zds->outStart, toFlushSize);
1692 op += flushedSize;
1693 zds->outStart += flushedSize;
1694 if (flushedSize == toFlushSize) { /* flush completed */
1695 zds->streamStage = zdss_read;
1696 if ( (zds->outBuffSize < zds->fParams.frameContentSize)
1697 && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize) ) {
1698 DEBUGLOG(5, "restart filling outBuff from beginning (left:%i, needed:%u)",
1699 (int)(zds->outBuffSize - zds->outStart),
1700 (U32)zds->fParams.blockSizeMax);
1701 zds->outStart = zds->outEnd = 0;
1702 }
1703 break;
1704 } }
1705 /* cannot complete flush */
1706 someMoreWork = 0;
1707 break;
1708
Abhilash S.L3b494632019-07-16 15:51:09 +05301709 default:
1710 assert(0); /* impossible */
1711 RETURN_ERROR(GENERIC); /* some compiler require default to do something */
William Kurkianea869482019-04-09 15:16:11 -04001712 } }
1713
1714 /* result */
Abhilash S.L3b494632019-07-16 15:51:09 +05301715 input->pos = (size_t)(ip - (const char*)(input->src));
1716 output->pos = (size_t)(op - (char*)(output->dst));
1717 if ((ip==istart) && (op==ostart)) { /* no forward progress */
1718 zds->noForwardProgress ++;
1719 if (zds->noForwardProgress >= ZSTD_NO_FORWARD_PROGRESS_MAX) {
1720 RETURN_ERROR_IF(op==oend, dstSize_tooSmall);
1721 RETURN_ERROR_IF(ip==iend, srcSize_wrong);
1722 assert(0);
1723 }
1724 } else {
1725 zds->noForwardProgress = 0;
1726 }
William Kurkianea869482019-04-09 15:16:11 -04001727 { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds);
1728 if (!nextSrcSizeHint) { /* frame fully decoded */
1729 if (zds->outEnd == zds->outStart) { /* output fully flushed */
1730 if (zds->hostageByte) {
1731 if (input->pos >= input->size) {
1732 /* can't release hostage (not present) */
1733 zds->streamStage = zdss_read;
1734 return 1;
1735 }
1736 input->pos++; /* release hostage */
1737 } /* zds->hostageByte */
1738 return 0;
1739 } /* zds->outEnd == zds->outStart */
1740 if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */
1741 input->pos--; /* note : pos > 0, otherwise, impossible to finish reading last block */
1742 zds->hostageByte=1;
1743 }
1744 return 1;
1745 } /* nextSrcSizeHint==0 */
1746 nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block); /* preload header of next block */
1747 assert(zds->inPos <= nextSrcSizeHint);
1748 nextSrcSizeHint -= zds->inPos; /* part already loaded*/
1749 return nextSrcSizeHint;
1750 }
1751}
1752
Abhilash S.L3b494632019-07-16 15:51:09 +05301753size_t ZSTD_decompressStream_simpleArgs (
William Kurkianea869482019-04-09 15:16:11 -04001754 ZSTD_DCtx* dctx,
1755 void* dst, size_t dstCapacity, size_t* dstPos,
1756 const void* src, size_t srcSize, size_t* srcPos)
1757{
1758 ZSTD_outBuffer output = { dst, dstCapacity, *dstPos };
1759 ZSTD_inBuffer input = { src, srcSize, *srcPos };
1760 /* ZSTD_compress_generic() will check validity of dstPos and srcPos */
Abhilash S.L3b494632019-07-16 15:51:09 +05301761 size_t const cErr = ZSTD_decompressStream(dctx, &output, &input);
William Kurkianea869482019-04-09 15:16:11 -04001762 *dstPos = output.pos;
1763 *srcPos = input.pos;
1764 return cErr;
1765}