1 /* 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 40 # 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 52 #endif 53 54 55 /*-******************************************************* 56 * Dependencies 57 *********************************************************/ 58 #include <string.h> /* memcpy, memmove, memset */ 59 #include "cpu.h" /* bmi2 */ 60 #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" 65 #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 */ 69 70 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) 71 # include "zstd_legacy.h" 72 #endif 73 74 75 /*-************************************************************* 76 * Context management 77 ***************************************************************/ 78 size_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 86 size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); } 87 88 89 static size_t ZSTD_startingInputLength(ZSTD_format_e format) 90 { 91 size_t const startingInputLength = ZSTD_FRAMEHEADERSIZE_PREFIX(format); 92 /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */ 93 assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) ); 94 return startingInputLength; 95 } 96 97 static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx) 98 { 99 dctx->format = ZSTD_f_zstd1; /* ZSTD_decompressBegin() invokes ZSTD_startingInputLength() with argument dctx->format */ 100 dctx->staticSize = 0; 101 dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT; 102 dctx->ddict = NULL; 103 dctx->ddictLocal = NULL; 104 dctx->dictEnd = NULL; 105 dctx->ddictIsCold = 0; 106 dctx->dictUses = ZSTD_dont_use; 107 dctx->inBuff = NULL; 108 dctx->inBuffSize = 0; 109 dctx->outBuffSize = 0; 110 dctx->streamStage = zdss_init; 111 dctx->legacyContext = NULL; 112 dctx->previousLegacyVersion = 0; 113 dctx->noForwardProgress = 0; 114 dctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid()); 115 } 116 117 ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize) 118 { 119 ZSTD_DCtx* const dctx = (ZSTD_DCtx*) workspace; 120 121 if ((size_t)workspace & 7) return NULL; /* 8-aligned */ 122 if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL; /* minimum size */ 123 124 ZSTD_initDCtx_internal(dctx); 125 dctx->staticSize = workspaceSize; 126 dctx->inBuff = (char*)(dctx+1); 127 return dctx; 128 } 129 130 ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem) 131 { 132 if (!customMem.customAlloc ^ !customMem.customFree) return NULL; 133 134 { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(*dctx), customMem); 135 if (!dctx) return NULL; 136 dctx->customMem = customMem; 137 ZSTD_initDCtx_internal(dctx); 138 return dctx; 139 } 140 } 141 142 ZSTD_DCtx* ZSTD_createDCtx(void) 143 { 144 DEBUGLOG(3, "ZSTD_createDCtx"); 145 return ZSTD_createDCtx_advanced(ZSTD_defaultCMem); 146 } 147 148 static void ZSTD_clearDict(ZSTD_DCtx* dctx) 149 { 150 ZSTD_freeDDict(dctx->ddictLocal); 151 dctx->ddictLocal = NULL; 152 dctx->ddict = NULL; 153 dctx->dictUses = ZSTD_dont_use; 154 } 155 156 size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx) 157 { 158 if (dctx==NULL) return 0; /* support free on NULL */ 159 RETURN_ERROR_IF(dctx->staticSize, memory_allocation, "not compatible with static DCtx"); 160 { ZSTD_customMem const cMem = dctx->customMem; 161 ZSTD_clearDict(dctx); 162 ZSTD_free(dctx->inBuff, cMem); 163 dctx->inBuff = NULL; 164 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) 165 if (dctx->legacyContext) 166 ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion); 167 #endif 168 ZSTD_free(dctx, cMem); 169 return 0; 170 } 171 } 172 173 /* no longer useful */ 174 void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx) 175 { 176 size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx); 177 memcpy(dstDCtx, srcDCtx, toCopy); /* no need to copy workspace */ 178 } 179 180 181 /*-************************************************************* 182 * Frame header decoding 183 ***************************************************************/ 184 185 /*! ZSTD_isFrame() : 186 * Tells if the content of `buffer` starts with a valid Frame Identifier. 187 * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. 188 * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. 189 * Note 3 : Skippable Frame Identifiers are considered valid. */ 190 unsigned ZSTD_isFrame(const void* buffer, size_t size) 191 { 192 if (size < ZSTD_FRAMEIDSIZE) return 0; 193 { U32 const magic = MEM_readLE32(buffer); 194 if (magic == ZSTD_MAGICNUMBER) return 1; 195 if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1; 196 } 197 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) 198 if (ZSTD_isLegacy(buffer, size)) return 1; 199 #endif 200 return 0; 201 } 202 203 /** ZSTD_frameHeaderSize_internal() : 204 * srcSize must be large enough to reach header size fields. 205 * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless. 206 * @return : size of the Frame Header 207 * or an error code, which can be tested with ZSTD_isError() */ 208 static size_t ZSTD_frameHeaderSize_internal(const void* src, size_t srcSize, ZSTD_format_e format) 209 { 210 size_t const minInputSize = ZSTD_startingInputLength(format); 211 RETURN_ERROR_IF(srcSize < minInputSize, srcSize_wrong); 212 213 { BYTE const fhd = ((const BYTE*)src)[minInputSize-1]; 214 U32 const dictID= fhd & 3; 215 U32 const singleSegment = (fhd >> 5) & 1; 216 U32 const fcsId = fhd >> 6; 217 return minInputSize + !singleSegment 218 + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId] 219 + (singleSegment && !fcsId); 220 } 221 } 222 223 /** ZSTD_frameHeaderSize() : 224 * srcSize must be >= ZSTD_frameHeaderSize_prefix. 225 * @return : size of the Frame Header, 226 * or an error code (if srcSize is too small) */ 227 size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize) 228 { 229 return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1); 230 } 231 232 233 /** ZSTD_getFrameHeader_advanced() : 234 * decode Frame Header, or require larger `srcSize`. 235 * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless 236 * @return : 0, `zfhPtr` is correctly filled, 237 * >0, `srcSize` is too small, value is wanted `srcSize` amount, 238 * or an error code, which can be tested using ZSTD_isError() */ 239 size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format) 240 { 241 const BYTE* ip = (const BYTE*)src; 242 size_t const minInputSize = ZSTD_startingInputLength(format); 243 244 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 */ 245 if (srcSize < minInputSize) return minInputSize; 246 RETURN_ERROR_IF(src==NULL, GENERIC, "invalid parameter"); 247 248 if ( (format != ZSTD_f_zstd1_magicless) 249 && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) { 250 if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { 251 /* skippable frame */ 252 if (srcSize < ZSTD_SKIPPABLEHEADERSIZE) 253 return ZSTD_SKIPPABLEHEADERSIZE; /* magic number + frame length */ 254 memset(zfhPtr, 0, sizeof(*zfhPtr)); 255 zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_FRAMEIDSIZE); 256 zfhPtr->frameType = ZSTD_skippableFrame; 257 return 0; 258 } 259 RETURN_ERROR(prefix_unknown); 260 } 261 262 /* ensure there is enough `srcSize` to fully read/decode frame header */ 263 { size_t const fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format); 264 if (srcSize < fhsize) return fhsize; 265 zfhPtr->headerSize = (U32)fhsize; 266 } 267 268 { BYTE const fhdByte = ip[minInputSize-1]; 269 size_t pos = minInputSize; 270 U32 const dictIDSizeCode = fhdByte&3; 271 U32 const checksumFlag = (fhdByte>>2)&1; 272 U32 const singleSegment = (fhdByte>>5)&1; 273 U32 const fcsID = fhdByte>>6; 274 U64 windowSize = 0; 275 U32 dictID = 0; 276 U64 frameContentSize = ZSTD_CONTENTSIZE_UNKNOWN; 277 RETURN_ERROR_IF((fhdByte & 0x08) != 0, frameParameter_unsupported, 278 "reserved bits, must be zero"); 279 280 if (!singleSegment) { 281 BYTE const wlByte = ip[pos++]; 282 U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN; 283 RETURN_ERROR_IF(windowLog > ZSTD_WINDOWLOG_MAX, frameParameter_windowTooLarge); 284 windowSize = (1ULL << windowLog); 285 windowSize += (windowSize >> 3) * (wlByte&7); 286 } 287 switch(dictIDSizeCode) 288 { 289 default: assert(0); /* impossible */ 290 case 0 : break; 291 case 1 : dictID = ip[pos]; pos++; break; 292 case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break; 293 case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break; 294 } 295 switch(fcsID) 296 { 297 default: assert(0); /* impossible */ 298 case 0 : if (singleSegment) frameContentSize = ip[pos]; break; 299 case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break; 300 case 2 : frameContentSize = MEM_readLE32(ip+pos); break; 301 case 3 : frameContentSize = MEM_readLE64(ip+pos); break; 302 } 303 if (singleSegment) windowSize = frameContentSize; 304 305 zfhPtr->frameType = ZSTD_frame; 306 zfhPtr->frameContentSize = frameContentSize; 307 zfhPtr->windowSize = windowSize; 308 zfhPtr->blockSizeMax = (unsigned) MIN(windowSize, ZSTD_BLOCKSIZE_MAX); 309 zfhPtr->dictID = dictID; 310 zfhPtr->checksumFlag = checksumFlag; 311 } 312 return 0; 313 } 314 315 /** ZSTD_getFrameHeader() : 316 * decode Frame Header, or require larger `srcSize`. 317 * note : this function does not consume input, it only reads it. 318 * @return : 0, `zfhPtr` is correctly filled, 319 * >0, `srcSize` is too small, value is wanted `srcSize` amount, 320 * or an error code, which can be tested using ZSTD_isError() */ 321 size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize) 322 { 323 return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_f_zstd1); 324 } 325 326 327 /** ZSTD_getFrameContentSize() : 328 * compatible with legacy mode 329 * @return : decompressed size of the single frame pointed to be `src` if known, otherwise 330 * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined 331 * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */ 332 unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize) 333 { 334 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) 335 if (ZSTD_isLegacy(src, srcSize)) { 336 unsigned long long const ret = ZSTD_getDecompressedSize_legacy(src, srcSize); 337 return ret == 0 ? ZSTD_CONTENTSIZE_UNKNOWN : ret; 338 } 339 #endif 340 { ZSTD_frameHeader zfh; 341 if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0) 342 return ZSTD_CONTENTSIZE_ERROR; 343 if (zfh.frameType == ZSTD_skippableFrame) { 344 return 0; 345 } else { 346 return zfh.frameContentSize; 347 } } 348 } 349 350 static size_t readSkippableFrameSize(void const* src, size_t srcSize) 351 { 352 size_t const skippableHeaderSize = ZSTD_SKIPPABLEHEADERSIZE; 353 U32 sizeU32; 354 355 RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong); 356 357 sizeU32 = MEM_readLE32((BYTE const*)src + ZSTD_FRAMEIDSIZE); 358 RETURN_ERROR_IF((U32)(sizeU32 + ZSTD_SKIPPABLEHEADERSIZE) < sizeU32, 359 frameParameter_unsupported); 360 { 361 size_t const skippableSize = skippableHeaderSize + sizeU32; 362 RETURN_ERROR_IF(skippableSize > srcSize, srcSize_wrong); 363 return skippableSize; 364 } 365 } 366 367 /** 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 */ 372 unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize) 373 { 374 unsigned long long totalDstSize = 0; 375 376 while (srcSize >= ZSTD_startingInputLength(ZSTD_f_zstd1)) { 377 U32 const magicNumber = MEM_readLE32(src); 378 379 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 ZSTD_CONTENTSIZE_ERROR; 383 } 384 assert(skippableSize <= srcSize); 385 386 src = (const BYTE *)src + skippableSize; 387 srcSize -= skippableSize; 388 continue; 389 } 390 391 { unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize); 392 if (ret >= ZSTD_CONTENTSIZE_ERROR) return ret; 393 394 /* check for overflow */ 395 if (totalDstSize + ret < totalDstSize) return ZSTD_CONTENTSIZE_ERROR; 396 totalDstSize += ret; 397 } 398 { size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize); 399 if (ZSTD_isError(frameSrcSize)) { 400 return ZSTD_CONTENTSIZE_ERROR; 401 } 402 403 src = (const BYTE *)src + frameSrcSize; 404 srcSize -= frameSrcSize; 405 } 406 } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */ 407 408 if (srcSize) return ZSTD_CONTENTSIZE_ERROR; 409 410 return totalDstSize; 411 } 412 413 /** ZSTD_getDecompressedSize() : 414 * compatible with legacy mode 415 * @return : decompressed size if known, 0 otherwise 416 note : 0 can mean any of the following : 417 - frame content is empty 418 - decompressed size field is not present in frame header 419 - frame header unknown / not supported 420 - frame header not complete (`srcSize` too small) */ 421 unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize) 422 { 423 unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize); 424 ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR < ZSTD_CONTENTSIZE_UNKNOWN); 425 return (ret >= ZSTD_CONTENTSIZE_ERROR) ? 0 : ret; 426 } 427 428 429 /** ZSTD_decodeFrameHeader() : 430 * `headerSize` must be the size provided by ZSTD_frameHeaderSize(). 431 * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */ 432 static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize) 433 { 434 size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format); 435 if (ZSTD_isError(result)) return result; /* invalid header */ 436 RETURN_ERROR_IF(result>0, srcSize_wrong, "headerSize too small"); 437 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 438 /* Skip the dictID check in fuzzing mode, because it makes the search 439 * harder. 440 */ 441 RETURN_ERROR_IF(dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID), 442 dictionary_wrong); 443 #endif 444 if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0); 445 return 0; 446 } 447 448 static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret) 449 { 450 ZSTD_frameSizeInfo frameSizeInfo; 451 frameSizeInfo.compressedSize = ret; 452 frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR; 453 return frameSizeInfo; 454 } 455 456 static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize) 457 { 458 ZSTD_frameSizeInfo frameSizeInfo; 459 memset(&frameSizeInfo, 0, sizeof(ZSTD_frameSizeInfo)); 460 461 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) 462 if (ZSTD_isLegacy(src, srcSize)) 463 return ZSTD_findFrameSizeInfoLegacy(src, srcSize); 464 #endif 465 466 if ((srcSize >= ZSTD_SKIPPABLEHEADERSIZE) 467 && (MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { 468 frameSizeInfo.compressedSize = readSkippableFrameSize(src, srcSize); 469 assert(ZSTD_isError(frameSizeInfo.compressedSize) || 470 frameSizeInfo.compressedSize <= srcSize); 471 return frameSizeInfo; 472 } else { 473 const BYTE* ip = (const BYTE*)src; 474 const BYTE* const ipstart = ip; 475 size_t remainingSize = srcSize; 476 size_t nbBlocks = 0; 477 ZSTD_frameHeader zfh; 478 479 /* Extract Frame Header */ 480 { size_t const ret = ZSTD_getFrameHeader(&zfh, src, srcSize); 481 if (ZSTD_isError(ret)) 482 return ZSTD_errorFrameSizeInfo(ret); 483 if (ret > 0) 484 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong)); 485 } 486 487 ip += zfh.headerSize; 488 remainingSize -= zfh.headerSize; 489 490 /* Iterate over each block */ 491 while (1) { 492 blockProperties_t blockProperties; 493 size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties); 494 if (ZSTD_isError(cBlockSize)) 495 return ZSTD_errorFrameSizeInfo(cBlockSize); 496 497 if (ZSTD_blockHeaderSize + cBlockSize > remainingSize) 498 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong)); 499 500 ip += ZSTD_blockHeaderSize + cBlockSize; 501 remainingSize -= ZSTD_blockHeaderSize + cBlockSize; 502 nbBlocks++; 503 504 if (blockProperties.lastBlock) break; 505 } 506 507 /* Final frame content checksum */ 508 if (zfh.checksumFlag) { 509 if (remainingSize < 4) 510 return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong)); 511 ip += 4; 512 } 513 514 frameSizeInfo.compressedSize = ip - ipstart; 515 frameSizeInfo.decompressedBound = (zfh.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) 516 ? zfh.frameContentSize 517 : nbBlocks * zfh.blockSizeMax; 518 return frameSizeInfo; 519 } 520 } 521 522 /** ZSTD_findFrameCompressedSize() : 523 * compatible with legacy mode 524 * `src` must point to the start of a ZSTD frame, ZSTD legacy frame, or skippable frame 525 * `srcSize` must be at least as large as the frame contained 526 * @return : the compressed size of the frame starting at `src` */ 527 size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize) 528 { 529 ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize); 530 return frameSizeInfo.compressedSize; 531 } 532 533 /** 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 */ 539 unsigned 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 assert(srcSize >= compressedSize); 550 src = (const BYTE*)src + compressedSize; 551 srcSize -= compressedSize; 552 bound += decompressedBound; 553 } 554 return bound; 555 } 556 557 558 /*-************************************************************* 559 * Frame decoding 560 ***************************************************************/ 561 562 563 void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst) 564 { 565 if (dst != dctx->previousDstEnd) { /* not contiguous */ 566 dctx->dictEnd = dctx->previousDstEnd; 567 dctx->virtualStart = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart)); 568 dctx->prefixStart = dst; 569 dctx->previousDstEnd = dst; 570 } 571 } 572 573 /** ZSTD_insertBlock() : 574 * insert `src` block into `dctx` history. Useful to track uncompressed blocks. */ 575 size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize) 576 { 577 DEBUGLOG(5, "ZSTD_insertBlock: %u bytes", (unsigned)blockSize); 578 ZSTD_checkContinuity(dctx, blockStart); 579 dctx->previousDstEnd = (const char*)blockStart + blockSize; 580 return blockSize; 581 } 582 583 584 static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity, 585 const void* src, size_t srcSize) 586 { 587 DEBUGLOG(5, "ZSTD_copyRawBlock"); 588 if (dst == NULL) { 589 if (srcSize == 0) return 0; 590 RETURN_ERROR(dstBuffer_null); 591 } 592 RETURN_ERROR_IF(srcSize > dstCapacity, dstSize_tooSmall); 593 memcpy(dst, src, srcSize); 594 return srcSize; 595 } 596 597 static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity, 598 BYTE b, 599 size_t regenSize) 600 { 601 if (dst == NULL) { 602 if (regenSize == 0) return 0; 603 RETURN_ERROR(dstBuffer_null); 604 } 605 RETURN_ERROR_IF(regenSize > dstCapacity, dstSize_tooSmall); 606 memset(dst, b, regenSize); 607 return regenSize; 608 } 609 610 611 /*! ZSTD_decompressFrame() : 612 * @dctx must be properly initialized 613 * will update *srcPtr and *srcSizePtr, 614 * to make *srcPtr progress by one frame. */ 615 static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx, 616 void* dst, size_t dstCapacity, 617 const void** srcPtr, size_t *srcSizePtr) 618 { 619 const BYTE* ip = (const BYTE*)(*srcPtr); 620 BYTE* const ostart = (BYTE* const)dst; 621 BYTE* const oend = ostart + dstCapacity; 622 BYTE* op = ostart; 623 size_t remainingSrcSize = *srcSizePtr; 624 625 DEBUGLOG(4, "ZSTD_decompressFrame (srcSize:%i)", (int)*srcSizePtr); 626 627 /* check */ 628 RETURN_ERROR_IF( 629 remainingSrcSize < ZSTD_FRAMEHEADERSIZE_MIN(dctx->format)+ZSTD_blockHeaderSize, 630 srcSize_wrong); 631 632 /* Frame Header */ 633 { size_t const frameHeaderSize = ZSTD_frameHeaderSize_internal( 634 ip, ZSTD_FRAMEHEADERSIZE_PREFIX(dctx->format), dctx->format); 635 if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize; 636 RETURN_ERROR_IF(remainingSrcSize < frameHeaderSize+ZSTD_blockHeaderSize, 637 srcSize_wrong); 638 FORWARD_IF_ERROR( ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize) ); 639 ip += frameHeaderSize; remainingSrcSize -= frameHeaderSize; 640 } 641 642 /* Loop on each block */ 643 while (1) { 644 size_t decodedSize; 645 blockProperties_t blockProperties; 646 size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSrcSize, &blockProperties); 647 if (ZSTD_isError(cBlockSize)) return cBlockSize; 648 649 ip += ZSTD_blockHeaderSize; 650 remainingSrcSize -= ZSTD_blockHeaderSize; 651 RETURN_ERROR_IF(cBlockSize > remainingSrcSize, srcSize_wrong); 652 653 switch(blockProperties.blockType) 654 { 655 case bt_compressed: 656 decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize, /* frame */ 1); 657 break; 658 case bt_raw : 659 decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize); 660 break; 661 case bt_rle : 662 decodedSize = ZSTD_setRleBlock(op, oend-op, *ip, blockProperties.origSize); 663 break; 664 case bt_reserved : 665 default: 666 RETURN_ERROR(corruption_detected); 667 } 668 669 if (ZSTD_isError(decodedSize)) return decodedSize; 670 if (dctx->fParams.checksumFlag) 671 XXH64_update(&dctx->xxhState, op, decodedSize); 672 op += decodedSize; 673 ip += cBlockSize; 674 remainingSrcSize -= cBlockSize; 675 if (blockProperties.lastBlock) break; 676 } 677 678 if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) { 679 RETURN_ERROR_IF((U64)(op-ostart) != dctx->fParams.frameContentSize, 680 corruption_detected); 681 } 682 if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */ 683 U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState); 684 U32 checkRead; 685 RETURN_ERROR_IF(remainingSrcSize<4, checksum_wrong); 686 checkRead = MEM_readLE32(ip); 687 RETURN_ERROR_IF(checkRead != checkCalc, checksum_wrong); 688 ip += 4; 689 remainingSrcSize -= 4; 690 } 691 692 /* Allow caller to get size read */ 693 *srcPtr = ip; 694 *srcSizePtr = remainingSrcSize; 695 return op-ostart; 696 } 697 698 static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx, 699 void* dst, size_t dstCapacity, 700 const void* src, size_t srcSize, 701 const void* dict, size_t dictSize, 702 const ZSTD_DDict* ddict) 703 { 704 void* const dststart = dst; 705 int moreThan1Frame = 0; 706 707 DEBUGLOG(5, "ZSTD_decompressMultiFrame"); 708 assert(dict==NULL || ddict==NULL); /* either dict or ddict set, not both */ 709 710 if (ddict) { 711 dict = ZSTD_DDict_dictContent(ddict); 712 dictSize = ZSTD_DDict_dictSize(ddict); 713 } 714 715 while (srcSize >= ZSTD_startingInputLength(dctx->format)) { 716 717 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) 718 if (ZSTD_isLegacy(src, srcSize)) { 719 size_t decodedSize; 720 size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize); 721 if (ZSTD_isError(frameSize)) return frameSize; 722 RETURN_ERROR_IF(dctx->staticSize, memory_allocation, 723 "legacy support is not compatible with static dctx"); 724 725 decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize); 726 if (ZSTD_isError(decodedSize)) return decodedSize; 727 728 assert(decodedSize <=- dstCapacity); 729 dst = (BYTE*)dst + decodedSize; 730 dstCapacity -= decodedSize; 731 732 src = (const BYTE*)src + frameSize; 733 srcSize -= frameSize; 734 735 continue; 736 } 737 #endif 738 739 { U32 const magicNumber = MEM_readLE32(src); 740 DEBUGLOG(4, "reading magic number %08X (expecting %08X)", 741 (unsigned)magicNumber, ZSTD_MAGICNUMBER); 742 if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { 743 size_t const skippableSize = readSkippableFrameSize(src, srcSize); 744 FORWARD_IF_ERROR(skippableSize); 745 assert(skippableSize <= srcSize); 746 747 src = (const BYTE *)src + skippableSize; 748 srcSize -= skippableSize; 749 continue; 750 } } 751 752 if (ddict) { 753 /* we were called from ZSTD_decompress_usingDDict */ 754 FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(dctx, ddict)); 755 } else { 756 /* this will initialize correctly with no dict if dict == NULL, so 757 * use this in all cases but ddict */ 758 FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize)); 759 } 760 ZSTD_checkContinuity(dctx, dst); 761 762 { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity, 763 &src, &srcSize); 764 RETURN_ERROR_IF( 765 (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown) 766 && (moreThan1Frame==1), 767 srcSize_wrong, 768 "at least one frame successfully completed, but following " 769 "bytes are garbage: it's more likely to be a srcSize error, " 770 "specifying more bytes than compressed size of frame(s). This " 771 "error message replaces ERROR(prefix_unknown), which would be " 772 "confusing, as the first header is actually correct. Note that " 773 "one could be unlucky, it might be a corruption error instead, " 774 "happening right at the place where we expect zstd magic " 775 "bytes. But this is _much_ less likely than a srcSize field " 776 "error."); 777 if (ZSTD_isError(res)) return res; 778 assert(res <= dstCapacity); 779 dst = (BYTE*)dst + res; 780 dstCapacity -= res; 781 } 782 moreThan1Frame = 1; 783 } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */ 784 785 RETURN_ERROR_IF(srcSize, srcSize_wrong, "input not entirely consumed"); 786 787 return (BYTE*)dst - (BYTE*)dststart; 788 } 789 790 size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, 791 void* dst, size_t dstCapacity, 792 const void* src, size_t srcSize, 793 const void* dict, size_t dictSize) 794 { 795 return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL); 796 } 797 798 799 static ZSTD_DDict const* ZSTD_getDDict(ZSTD_DCtx* dctx) 800 { 801 switch (dctx->dictUses) { 802 default: 803 assert(0 /* Impossible */); 804 /* fall-through */ 805 case ZSTD_dont_use: 806 ZSTD_clearDict(dctx); 807 return NULL; 808 case ZSTD_use_indefinitely: 809 return dctx->ddict; 810 case ZSTD_use_once: 811 dctx->dictUses = ZSTD_dont_use; 812 return dctx->ddict; 813 } 814 } 815 816 size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) 817 { 818 return ZSTD_decompress_usingDDict(dctx, dst, dstCapacity, src, srcSize, ZSTD_getDDict(dctx)); 819 } 820 821 822 size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize) 823 { 824 #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1) 825 size_t regenSize; 826 ZSTD_DCtx* const dctx = ZSTD_createDCtx(); 827 RETURN_ERROR_IF(dctx==NULL, memory_allocation); 828 regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize); 829 ZSTD_freeDCtx(dctx); 830 return regenSize; 831 #else /* stack mode */ 832 ZSTD_DCtx dctx; 833 ZSTD_initDCtx_internal(&dctx); 834 return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize); 835 #endif 836 } 837 838 839 /*-************************************** 840 * Advanced Streaming Decompression API 841 * Bufferless and synchronous 842 ****************************************/ 843 size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; } 844 845 ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) { 846 switch(dctx->stage) 847 { 848 default: /* should not happen */ 849 assert(0); 850 case ZSTDds_getFrameHeaderSize: 851 case ZSTDds_decodeFrameHeader: 852 return ZSTDnit_frameHeader; 853 case ZSTDds_decodeBlockHeader: 854 return ZSTDnit_blockHeader; 855 case ZSTDds_decompressBlock: 856 return ZSTDnit_block; 857 case ZSTDds_decompressLastBlock: 858 return ZSTDnit_lastBlock; 859 case ZSTDds_checkChecksum: 860 return ZSTDnit_checksum; 861 case ZSTDds_decodeSkippableHeader: 862 case ZSTDds_skipFrame: 863 return ZSTDnit_skippableFrame; 864 } 865 } 866 867 static int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; } 868 869 /** ZSTD_decompressContinue() : 870 * srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress()) 871 * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity) 872 * or an error code, which can be tested using ZSTD_isError() */ 873 size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) 874 { 875 DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (unsigned)srcSize); 876 /* Sanity check */ 877 RETURN_ERROR_IF(srcSize != dctx->expected, srcSize_wrong, "not allowed"); 878 if (dstCapacity) ZSTD_checkContinuity(dctx, dst); 879 880 switch (dctx->stage) 881 { 882 case ZSTDds_getFrameHeaderSize : 883 assert(src != NULL); 884 if (dctx->format == ZSTD_f_zstd1) { /* allows header */ 885 assert(srcSize >= ZSTD_FRAMEIDSIZE); /* to read skippable magic number */ 886 if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */ 887 memcpy(dctx->headerBuffer, src, srcSize); 888 dctx->expected = ZSTD_SKIPPABLEHEADERSIZE - srcSize; /* remaining to load to get full skippable frame header */ 889 dctx->stage = ZSTDds_decodeSkippableHeader; 890 return 0; 891 } } 892 dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format); 893 if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize; 894 memcpy(dctx->headerBuffer, src, srcSize); 895 dctx->expected = dctx->headerSize - srcSize; 896 dctx->stage = ZSTDds_decodeFrameHeader; 897 return 0; 898 899 case ZSTDds_decodeFrameHeader: 900 assert(src != NULL); 901 memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize); 902 FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize)); 903 dctx->expected = ZSTD_blockHeaderSize; 904 dctx->stage = ZSTDds_decodeBlockHeader; 905 return 0; 906 907 case ZSTDds_decodeBlockHeader: 908 { blockProperties_t bp; 909 size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp); 910 if (ZSTD_isError(cBlockSize)) return cBlockSize; 911 RETURN_ERROR_IF(cBlockSize > dctx->fParams.blockSizeMax, corruption_detected, "Block Size Exceeds Maximum"); 912 dctx->expected = cBlockSize; 913 dctx->bType = bp.blockType; 914 dctx->rleSize = bp.origSize; 915 if (cBlockSize) { 916 dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock; 917 return 0; 918 } 919 /* empty block */ 920 if (bp.lastBlock) { 921 if (dctx->fParams.checksumFlag) { 922 dctx->expected = 4; 923 dctx->stage = ZSTDds_checkChecksum; 924 } else { 925 dctx->expected = 0; /* end of frame */ 926 dctx->stage = ZSTDds_getFrameHeaderSize; 927 } 928 } else { 929 dctx->expected = ZSTD_blockHeaderSize; /* jump to next header */ 930 dctx->stage = ZSTDds_decodeBlockHeader; 931 } 932 return 0; 933 } 934 935 case ZSTDds_decompressLastBlock: 936 case ZSTDds_decompressBlock: 937 DEBUGLOG(5, "ZSTD_decompressContinue: case ZSTDds_decompressBlock"); 938 { size_t rSize; 939 switch(dctx->bType) 940 { 941 case bt_compressed: 942 DEBUGLOG(5, "ZSTD_decompressContinue: case bt_compressed"); 943 rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 1); 944 break; 945 case bt_raw : 946 rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize); 947 break; 948 case bt_rle : 949 rSize = ZSTD_setRleBlock(dst, dstCapacity, *(const BYTE*)src, dctx->rleSize); 950 break; 951 case bt_reserved : /* should never happen */ 952 default: 953 RETURN_ERROR(corruption_detected); 954 } 955 if (ZSTD_isError(rSize)) return rSize; 956 RETURN_ERROR_IF(rSize > dctx->fParams.blockSizeMax, corruption_detected, "Decompressed Block Size Exceeds Maximum"); 957 DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (unsigned)rSize); 958 dctx->decodedSize += rSize; 959 if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, dst, rSize); 960 961 if (dctx->stage == ZSTDds_decompressLastBlock) { /* end of frame */ 962 DEBUGLOG(4, "ZSTD_decompressContinue: decoded size from frame : %u", (unsigned)dctx->decodedSize); 963 RETURN_ERROR_IF( 964 dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN 965 && dctx->decodedSize != dctx->fParams.frameContentSize, 966 corruption_detected); 967 if (dctx->fParams.checksumFlag) { /* another round for frame checksum */ 968 dctx->expected = 4; 969 dctx->stage = ZSTDds_checkChecksum; 970 } else { 971 dctx->expected = 0; /* ends here */ 972 dctx->stage = ZSTDds_getFrameHeaderSize; 973 } 974 } else { 975 dctx->stage = ZSTDds_decodeBlockHeader; 976 dctx->expected = ZSTD_blockHeaderSize; 977 dctx->previousDstEnd = (char*)dst + rSize; 978 } 979 return rSize; 980 } 981 982 case ZSTDds_checkChecksum: 983 assert(srcSize == 4); /* guaranteed by dctx->expected */ 984 { U32 const h32 = (U32)XXH64_digest(&dctx->xxhState); 985 U32 const check32 = MEM_readLE32(src); 986 DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", (unsigned)h32, (unsigned)check32); 987 RETURN_ERROR_IF(check32 != h32, checksum_wrong); 988 dctx->expected = 0; 989 dctx->stage = ZSTDds_getFrameHeaderSize; 990 return 0; 991 } 992 993 case ZSTDds_decodeSkippableHeader: 994 assert(src != NULL); 995 assert(srcSize <= ZSTD_SKIPPABLEHEADERSIZE); 996 memcpy(dctx->headerBuffer + (ZSTD_SKIPPABLEHEADERSIZE - srcSize), src, srcSize); /* complete skippable header */ 997 dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_FRAMEIDSIZE); /* note : dctx->expected can grow seriously large, beyond local buffer size */ 998 dctx->stage = ZSTDds_skipFrame; 999 return 0; 1000 1001 case ZSTDds_skipFrame: 1002 dctx->expected = 0; 1003 dctx->stage = ZSTDds_getFrameHeaderSize; 1004 return 0; 1005 1006 default: 1007 assert(0); /* impossible */ 1008 RETURN_ERROR(GENERIC); /* some compiler require default to do something */ 1009 } 1010 } 1011 1012 1013 static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) 1014 { 1015 dctx->dictEnd = dctx->previousDstEnd; 1016 dctx->virtualStart = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart)); 1017 dctx->prefixStart = dict; 1018 dctx->previousDstEnd = (const char*)dict + dictSize; 1019 return 0; 1020 } 1021 1022 /*! ZSTD_loadDEntropy() : 1023 * dict : must point at beginning of a valid zstd dictionary. 1024 * @return : size of entropy tables read */ 1025 size_t 1026 ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy, 1027 const void* const dict, size_t const dictSize) 1028 { 1029 const BYTE* dictPtr = (const BYTE*)dict; 1030 const BYTE* const dictEnd = dictPtr + dictSize; 1031 1032 RETURN_ERROR_IF(dictSize <= 8, dictionary_corrupted); 1033 assert(MEM_readLE32(dict) == ZSTD_MAGIC_DICTIONARY); /* dict must be valid */ 1034 dictPtr += 8; /* skip header = magic + dictID */ 1035 1036 ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, OFTable) == offsetof(ZSTD_entropyDTables_t, LLTable) + sizeof(entropy->LLTable)); 1037 ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, MLTable) == offsetof(ZSTD_entropyDTables_t, OFTable) + sizeof(entropy->OFTable)); 1038 ZSTD_STATIC_ASSERT(sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable) >= HUF_DECOMPRESS_WORKSPACE_SIZE); 1039 { void* const workspace = &entropy->LLTable; /* use fse tables as temporary workspace; implies fse tables are grouped together */ 1040 size_t const workspaceSize = sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable); 1041 #ifdef HUF_FORCE_DECOMPRESS_X1 1042 /* in minimal huffman, we always use X1 variants */ 1043 size_t const hSize = HUF_readDTableX1_wksp(entropy->hufTable, 1044 dictPtr, dictEnd - dictPtr, 1045 workspace, workspaceSize); 1046 #else 1047 size_t const hSize = HUF_readDTableX2_wksp(entropy->hufTable, 1048 dictPtr, dictEnd - dictPtr, 1049 workspace, workspaceSize); 1050 #endif 1051 RETURN_ERROR_IF(HUF_isError(hSize), dictionary_corrupted); 1052 dictPtr += hSize; 1053 } 1054 1055 { short offcodeNCount[MaxOff+1]; 1056 unsigned offcodeMaxValue = MaxOff, offcodeLog; 1057 size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr); 1058 RETURN_ERROR_IF(FSE_isError(offcodeHeaderSize), dictionary_corrupted); 1059 RETURN_ERROR_IF(offcodeMaxValue > MaxOff, dictionary_corrupted); 1060 RETURN_ERROR_IF(offcodeLog > OffFSELog, dictionary_corrupted); 1061 ZSTD_buildFSETable( entropy->OFTable, 1062 offcodeNCount, offcodeMaxValue, 1063 OF_base, OF_bits, 1064 offcodeLog); 1065 dictPtr += offcodeHeaderSize; 1066 } 1067 1068 { short matchlengthNCount[MaxML+1]; 1069 unsigned matchlengthMaxValue = MaxML, matchlengthLog; 1070 size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr); 1071 RETURN_ERROR_IF(FSE_isError(matchlengthHeaderSize), dictionary_corrupted); 1072 RETURN_ERROR_IF(matchlengthMaxValue > MaxML, dictionary_corrupted); 1073 RETURN_ERROR_IF(matchlengthLog > MLFSELog, dictionary_corrupted); 1074 ZSTD_buildFSETable( entropy->MLTable, 1075 matchlengthNCount, matchlengthMaxValue, 1076 ML_base, ML_bits, 1077 matchlengthLog); 1078 dictPtr += matchlengthHeaderSize; 1079 } 1080 1081 { short litlengthNCount[MaxLL+1]; 1082 unsigned litlengthMaxValue = MaxLL, litlengthLog; 1083 size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr); 1084 RETURN_ERROR_IF(FSE_isError(litlengthHeaderSize), dictionary_corrupted); 1085 RETURN_ERROR_IF(litlengthMaxValue > MaxLL, dictionary_corrupted); 1086 RETURN_ERROR_IF(litlengthLog > LLFSELog, dictionary_corrupted); 1087 ZSTD_buildFSETable( entropy->LLTable, 1088 litlengthNCount, litlengthMaxValue, 1089 LL_base, LL_bits, 1090 litlengthLog); 1091 dictPtr += litlengthHeaderSize; 1092 } 1093 1094 RETURN_ERROR_IF(dictPtr+12 > dictEnd, dictionary_corrupted); 1095 { int i; 1096 size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12)); 1097 for (i=0; i<3; i++) { 1098 U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4; 1099 RETURN_ERROR_IF(rep==0 || rep > dictContentSize, 1100 dictionary_corrupted); 1101 entropy->rep[i] = rep; 1102 } } 1103 1104 return dictPtr - (const BYTE*)dict; 1105 } 1106 1107 static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) 1108 { 1109 if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize); 1110 { U32 const magic = MEM_readLE32(dict); 1111 if (magic != ZSTD_MAGIC_DICTIONARY) { 1112 return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */ 1113 } } 1114 dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE); 1115 1116 /* load entropy tables */ 1117 { size_t const eSize = ZSTD_loadDEntropy(&dctx->entropy, dict, dictSize); 1118 RETURN_ERROR_IF(ZSTD_isError(eSize), dictionary_corrupted); 1119 dict = (const char*)dict + eSize; 1120 dictSize -= eSize; 1121 } 1122 dctx->litEntropy = dctx->fseEntropy = 1; 1123 1124 /* reference dictionary content */ 1125 return ZSTD_refDictContent(dctx, dict, dictSize); 1126 } 1127 1128 size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx) 1129 { 1130 assert(dctx != NULL); 1131 dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */ 1132 dctx->stage = ZSTDds_getFrameHeaderSize; 1133 dctx->decodedSize = 0; 1134 dctx->previousDstEnd = NULL; 1135 dctx->prefixStart = NULL; 1136 dctx->virtualStart = NULL; 1137 dctx->dictEnd = NULL; 1138 dctx->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */ 1139 dctx->litEntropy = dctx->fseEntropy = 0; 1140 dctx->dictID = 0; 1141 ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue)); 1142 memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */ 1143 dctx->LLTptr = dctx->entropy.LLTable; 1144 dctx->MLTptr = dctx->entropy.MLTable; 1145 dctx->OFTptr = dctx->entropy.OFTable; 1146 dctx->HUFptr = dctx->entropy.hufTable; 1147 return 0; 1148 } 1149 1150 size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) 1151 { 1152 FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) ); 1153 if (dict && dictSize) 1154 RETURN_ERROR_IF( 1155 ZSTD_isError(ZSTD_decompress_insertDictionary(dctx, dict, dictSize)), 1156 dictionary_corrupted); 1157 return 0; 1158 } 1159 1160 1161 /* ====== ZSTD_DDict ====== */ 1162 1163 size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict) 1164 { 1165 DEBUGLOG(4, "ZSTD_decompressBegin_usingDDict"); 1166 assert(dctx != NULL); 1167 if (ddict) { 1168 const char* const dictStart = (const char*)ZSTD_DDict_dictContent(ddict); 1169 size_t const dictSize = ZSTD_DDict_dictSize(ddict); 1170 const void* const dictEnd = dictStart + dictSize; 1171 dctx->ddictIsCold = (dctx->dictEnd != dictEnd); 1172 DEBUGLOG(4, "DDict is %s", 1173 dctx->ddictIsCold ? "~cold~" : "hot!"); 1174 } 1175 FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) ); 1176 if (ddict) { /* NULL ddict is equivalent to no dictionary */ 1177 ZSTD_copyDDictParameters(dctx, ddict); 1178 } 1179 return 0; 1180 } 1181 1182 /*! ZSTD_getDictID_fromDict() : 1183 * Provides the dictID stored within dictionary. 1184 * if @return == 0, the dictionary is not conformant with Zstandard specification. 1185 * It can still be loaded, but as a content-only dictionary. */ 1186 unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize) 1187 { 1188 if (dictSize < 8) return 0; 1189 if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0; 1190 return MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE); 1191 } 1192 1193 /*! ZSTD_getDictID_fromFrame() : 1194 * Provides the dictID required to decompress frame stored within `src`. 1195 * If @return == 0, the dictID could not be decoded. 1196 * This could for one of the following reasons : 1197 * - The frame does not require a dictionary (most common case). 1198 * - The frame was built with dictID intentionally removed. 1199 * Needed dictionary is a hidden information. 1200 * Note : this use case also happens when using a non-conformant dictionary. 1201 * - `srcSize` is too small, and as a result, frame header could not be decoded. 1202 * Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`. 1203 * - This is not a Zstandard frame. 1204 * When identifying the exact failure cause, it's possible to use 1205 * ZSTD_getFrameHeader(), which will provide a more precise error code. */ 1206 unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize) 1207 { 1208 ZSTD_frameHeader zfp = { 0, 0, 0, ZSTD_frame, 0, 0, 0 }; 1209 size_t const hError = ZSTD_getFrameHeader(&zfp, src, srcSize); 1210 if (ZSTD_isError(hError)) return 0; 1211 return zfp.dictID; 1212 } 1213 1214 1215 /*! ZSTD_decompress_usingDDict() : 1216 * Decompression using a pre-digested Dictionary 1217 * Use dictionary without significant overhead. */ 1218 size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, 1219 void* dst, size_t dstCapacity, 1220 const void* src, size_t srcSize, 1221 const ZSTD_DDict* ddict) 1222 { 1223 /* pass content and size in case legacy frames are encountered */ 1224 return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, 1225 NULL, 0, 1226 ddict); 1227 } 1228 1229 1230 /*===================================== 1231 * Streaming decompression 1232 *====================================*/ 1233 1234 ZSTD_DStream* ZSTD_createDStream(void) 1235 { 1236 DEBUGLOG(3, "ZSTD_createDStream"); 1237 return ZSTD_createDStream_advanced(ZSTD_defaultCMem); 1238 } 1239 1240 ZSTD_DStream* ZSTD_initStaticDStream(void *workspace, size_t workspaceSize) 1241 { 1242 return ZSTD_initStaticDCtx(workspace, workspaceSize); 1243 } 1244 1245 ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem) 1246 { 1247 return ZSTD_createDCtx_advanced(customMem); 1248 } 1249 1250 size_t ZSTD_freeDStream(ZSTD_DStream* zds) 1251 { 1252 return ZSTD_freeDCtx(zds); 1253 } 1254 1255 1256 /* *** Initialization *** */ 1257 1258 size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; } 1259 size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; } 1260 1261 size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, 1262 const void* dict, size_t dictSize, 1263 ZSTD_dictLoadMethod_e dictLoadMethod, 1264 ZSTD_dictContentType_e dictContentType) 1265 { 1266 RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong); 1267 ZSTD_clearDict(dctx); 1268 if (dict && dictSize != 0) { 1269 dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem); 1270 RETURN_ERROR_IF(dctx->ddictLocal == NULL, memory_allocation); 1271 dctx->ddict = dctx->ddictLocal; 1272 dctx->dictUses = ZSTD_use_indefinitely; 1273 } 1274 return 0; 1275 } 1276 1277 size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) 1278 { 1279 return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto); 1280 } 1281 1282 size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) 1283 { 1284 return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto); 1285 } 1286 1287 size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType) 1288 { 1289 FORWARD_IF_ERROR(ZSTD_DCtx_loadDictionary_advanced(dctx, prefix, prefixSize, ZSTD_dlm_byRef, dictContentType)); 1290 dctx->dictUses = ZSTD_use_once; 1291 return 0; 1292 } 1293 1294 size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize) 1295 { 1296 return ZSTD_DCtx_refPrefix_advanced(dctx, prefix, prefixSize, ZSTD_dct_rawContent); 1297 } 1298 1299 1300 /* ZSTD_initDStream_usingDict() : 1301 * return : expected size, aka ZSTD_startingInputLength(). 1302 * this function cannot fail */ 1303 size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize) 1304 { 1305 DEBUGLOG(4, "ZSTD_initDStream_usingDict"); 1306 FORWARD_IF_ERROR( ZSTD_DCtx_reset(zds, ZSTD_reset_session_only) ); 1307 FORWARD_IF_ERROR( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) ); 1308 return ZSTD_startingInputLength(zds->format); 1309 } 1310 1311 /* note : this variant can't fail */ 1312 size_t ZSTD_initDStream(ZSTD_DStream* zds) 1313 { 1314 DEBUGLOG(4, "ZSTD_initDStream"); 1315 return ZSTD_initDStream_usingDDict(zds, NULL); 1316 } 1317 1318 /* ZSTD_initDStream_usingDDict() : 1319 * ddict will just be referenced, and must outlive decompression session 1320 * this function cannot fail */ 1321 size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* dctx, const ZSTD_DDict* ddict) 1322 { 1323 FORWARD_IF_ERROR( ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only) ); 1324 FORWARD_IF_ERROR( ZSTD_DCtx_refDDict(dctx, ddict) ); 1325 return ZSTD_startingInputLength(dctx->format); 1326 } 1327 1328 /* ZSTD_resetDStream() : 1329 * return : expected size, aka ZSTD_startingInputLength(). 1330 * this function cannot fail */ 1331 size_t ZSTD_resetDStream(ZSTD_DStream* dctx) 1332 { 1333 FORWARD_IF_ERROR(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only)); 1334 return ZSTD_startingInputLength(dctx->format); 1335 } 1336 1337 1338 size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict) 1339 { 1340 RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong); 1341 ZSTD_clearDict(dctx); 1342 if (ddict) { 1343 dctx->ddict = ddict; 1344 dctx->dictUses = ZSTD_use_indefinitely; 1345 } 1346 return 0; 1347 } 1348 1349 /* ZSTD_DCtx_setMaxWindowSize() : 1350 * note : no direct equivalence in ZSTD_DCtx_setParameter, 1351 * since this version sets windowSize, and the other sets windowLog */ 1352 size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize) 1353 { 1354 ZSTD_bounds const bounds = ZSTD_dParam_getBounds(ZSTD_d_windowLogMax); 1355 size_t const min = (size_t)1 << bounds.lowerBound; 1356 size_t const max = (size_t)1 << bounds.upperBound; 1357 RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong); 1358 RETURN_ERROR_IF(maxWindowSize < min, parameter_outOfBound); 1359 RETURN_ERROR_IF(maxWindowSize > max, parameter_outOfBound); 1360 dctx->maxWindowSize = maxWindowSize; 1361 return 0; 1362 } 1363 1364 size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format) 1365 { 1366 return ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, format); 1367 } 1368 1369 ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam) 1370 { 1371 ZSTD_bounds bounds = { 0, 0, 0 }; 1372 switch(dParam) { 1373 case ZSTD_d_windowLogMax: 1374 bounds.lowerBound = ZSTD_WINDOWLOG_ABSOLUTEMIN; 1375 bounds.upperBound = ZSTD_WINDOWLOG_MAX; 1376 return bounds; 1377 case ZSTD_d_format: 1378 bounds.lowerBound = (int)ZSTD_f_zstd1; 1379 bounds.upperBound = (int)ZSTD_f_zstd1_magicless; 1380 ZSTD_STATIC_ASSERT(ZSTD_f_zstd1 < ZSTD_f_zstd1_magicless); 1381 return bounds; 1382 default:; 1383 } 1384 bounds.error = ERROR(parameter_unsupported); 1385 return bounds; 1386 } 1387 1388 /* ZSTD_dParam_withinBounds: 1389 * @return 1 if value is within dParam bounds, 1390 * 0 otherwise */ 1391 static int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam, int value) 1392 { 1393 ZSTD_bounds const bounds = ZSTD_dParam_getBounds(dParam); 1394 if (ZSTD_isError(bounds.error)) return 0; 1395 if (value < bounds.lowerBound) return 0; 1396 if (value > bounds.upperBound) return 0; 1397 return 1; 1398 } 1399 1400 #define CHECK_DBOUNDS(p,v) { \ 1401 RETURN_ERROR_IF(!ZSTD_dParam_withinBounds(p, v), parameter_outOfBound); \ 1402 } 1403 1404 size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value) 1405 { 1406 RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong); 1407 switch(dParam) { 1408 case ZSTD_d_windowLogMax: 1409 if (value == 0) value = ZSTD_WINDOWLOG_LIMIT_DEFAULT; 1410 CHECK_DBOUNDS(ZSTD_d_windowLogMax, value); 1411 dctx->maxWindowSize = ((size_t)1) << value; 1412 return 0; 1413 case ZSTD_d_format: 1414 CHECK_DBOUNDS(ZSTD_d_format, value); 1415 dctx->format = (ZSTD_format_e)value; 1416 return 0; 1417 default:; 1418 } 1419 RETURN_ERROR(parameter_unsupported); 1420 } 1421 1422 size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset) 1423 { 1424 if ( (reset == ZSTD_reset_session_only) 1425 || (reset == ZSTD_reset_session_and_parameters) ) { 1426 dctx->streamStage = zdss_init; 1427 dctx->noForwardProgress = 0; 1428 } 1429 if ( (reset == ZSTD_reset_parameters) 1430 || (reset == ZSTD_reset_session_and_parameters) ) { 1431 RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong); 1432 ZSTD_clearDict(dctx); 1433 dctx->format = ZSTD_f_zstd1; 1434 dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT; 1435 } 1436 return 0; 1437 } 1438 1439 1440 size_t ZSTD_sizeof_DStream(const ZSTD_DStream* dctx) 1441 { 1442 return ZSTD_sizeof_DCtx(dctx); 1443 } 1444 1445 size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize) 1446 { 1447 size_t const blockSize = (size_t) MIN(windowSize, ZSTD_BLOCKSIZE_MAX); 1448 unsigned long long const neededRBSize = windowSize + blockSize + (WILDCOPY_OVERLENGTH * 2); 1449 unsigned long long const neededSize = MIN(frameContentSize, neededRBSize); 1450 size_t const minRBSize = (size_t) neededSize; 1451 RETURN_ERROR_IF((unsigned long long)minRBSize != neededSize, 1452 frameParameter_windowTooLarge); 1453 return minRBSize; 1454 } 1455 1456 size_t ZSTD_estimateDStreamSize(size_t windowSize) 1457 { 1458 size_t const blockSize = MIN(windowSize, ZSTD_BLOCKSIZE_MAX); 1459 size_t const inBuffSize = blockSize; /* no block can be larger */ 1460 size_t const outBuffSize = ZSTD_decodingBufferSize_min(windowSize, ZSTD_CONTENTSIZE_UNKNOWN); 1461 return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize; 1462 } 1463 1464 size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize) 1465 { 1466 U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX; /* note : should be user-selectable, but requires an additional parameter (or a dctx) */ 1467 ZSTD_frameHeader zfh; 1468 size_t const err = ZSTD_getFrameHeader(&zfh, src, srcSize); 1469 if (ZSTD_isError(err)) return err; 1470 RETURN_ERROR_IF(err>0, srcSize_wrong); 1471 RETURN_ERROR_IF(zfh.windowSize > windowSizeMax, 1472 frameParameter_windowTooLarge); 1473 return ZSTD_estimateDStreamSize((size_t)zfh.windowSize); 1474 } 1475 1476 1477 /* ***** Decompression ***** */ 1478 1479 MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize) 1480 { 1481 size_t const length = MIN(dstCapacity, srcSize); 1482 memcpy(dst, src, length); 1483 return length; 1484 } 1485 1486 1487 size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input) 1488 { 1489 const char* const istart = (const char*)(input->src) + input->pos; 1490 const char* const iend = (const char*)(input->src) + input->size; 1491 const char* ip = istart; 1492 char* const ostart = (char*)(output->dst) + output->pos; 1493 char* const oend = (char*)(output->dst) + output->size; 1494 char* op = ostart; 1495 U32 someMoreWork = 1; 1496 1497 DEBUGLOG(5, "ZSTD_decompressStream"); 1498 RETURN_ERROR_IF( 1499 input->pos > input->size, 1500 srcSize_wrong, 1501 "forbidden. in: pos: %u vs size: %u", 1502 (U32)input->pos, (U32)input->size); 1503 RETURN_ERROR_IF( 1504 output->pos > output->size, 1505 dstSize_tooSmall, 1506 "forbidden. out: pos: %u vs size: %u", 1507 (U32)output->pos, (U32)output->size); 1508 DEBUGLOG(5, "input size : %u", (U32)(input->size - input->pos)); 1509 1510 while (someMoreWork) { 1511 switch(zds->streamStage) 1512 { 1513 case zdss_init : 1514 DEBUGLOG(5, "stage zdss_init => transparent reset "); 1515 zds->streamStage = zdss_loadHeader; 1516 zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0; 1517 zds->legacyVersion = 0; 1518 zds->hostageByte = 0; 1519 /* fall-through */ 1520 1521 case zdss_loadHeader : 1522 DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32)(iend - ip)); 1523 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) 1524 if (zds->legacyVersion) { 1525 RETURN_ERROR_IF(zds->staticSize, memory_allocation, 1526 "legacy support is incompatible with static dctx"); 1527 { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input); 1528 if (hint==0) zds->streamStage = zdss_init; 1529 return hint; 1530 } } 1531 #endif 1532 { size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format); 1533 DEBUGLOG(5, "header size : %u", (U32)hSize); 1534 if (ZSTD_isError(hSize)) { 1535 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) 1536 U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart); 1537 if (legacyVersion) { 1538 ZSTD_DDict const* const ddict = ZSTD_getDDict(zds); 1539 const void* const dict = ddict ? ZSTD_DDict_dictContent(ddict) : NULL; 1540 size_t const dictSize = ddict ? ZSTD_DDict_dictSize(ddict) : 0; 1541 DEBUGLOG(5, "ZSTD_decompressStream: detected legacy version v0.%u", legacyVersion); 1542 RETURN_ERROR_IF(zds->staticSize, memory_allocation, 1543 "legacy support is incompatible with static dctx"); 1544 FORWARD_IF_ERROR(ZSTD_initLegacyStream(&zds->legacyContext, 1545 zds->previousLegacyVersion, legacyVersion, 1546 dict, dictSize)); 1547 zds->legacyVersion = zds->previousLegacyVersion = legacyVersion; 1548 { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, legacyVersion, output, input); 1549 if (hint==0) zds->streamStage = zdss_init; /* or stay in stage zdss_loadHeader */ 1550 return hint; 1551 } } 1552 #endif 1553 return hSize; /* error */ 1554 } 1555 if (hSize != 0) { /* need more input */ 1556 size_t const toLoad = hSize - zds->lhSize; /* if hSize!=0, hSize > zds->lhSize */ 1557 size_t const remainingInput = (size_t)(iend-ip); 1558 assert(iend >= ip); 1559 if (toLoad > remainingInput) { /* not enough input to load full header */ 1560 if (remainingInput > 0) { 1561 memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput); 1562 zds->lhSize += remainingInput; 1563 } 1564 input->pos = input->size; 1565 return (MAX((size_t)ZSTD_FRAMEHEADERSIZE_MIN(zds->format), hSize) - zds->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */ 1566 } 1567 assert(ip != NULL); 1568 memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad; 1569 break; 1570 } } 1571 1572 /* check for single-pass mode opportunity */ 1573 if (zds->fParams.frameContentSize && zds->fParams.windowSize /* skippable frame if == 0 */ 1574 && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) { 1575 size_t const cSize = ZSTD_findFrameCompressedSize(istart, iend-istart); 1576 if (cSize <= (size_t)(iend-istart)) { 1577 /* shortcut : using single-pass mode */ 1578 size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, oend-op, istart, cSize, ZSTD_getDDict(zds)); 1579 if (ZSTD_isError(decompressedSize)) return decompressedSize; 1580 DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()") 1581 ip = istart + cSize; 1582 op += decompressedSize; 1583 zds->expected = 0; 1584 zds->streamStage = zdss_init; 1585 someMoreWork = 0; 1586 break; 1587 } } 1588 1589 /* Consume header (see ZSTDds_decodeFrameHeader) */ 1590 DEBUGLOG(4, "Consume header"); 1591 FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(zds, ZSTD_getDDict(zds))); 1592 1593 if ((MEM_readLE32(zds->headerBuffer) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */ 1594 zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_FRAMEIDSIZE); 1595 zds->stage = ZSTDds_skipFrame; 1596 } else { 1597 FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize)); 1598 zds->expected = ZSTD_blockHeaderSize; 1599 zds->stage = ZSTDds_decodeBlockHeader; 1600 } 1601 1602 /* control buffer memory usage */ 1603 DEBUGLOG(4, "Control max memory usage (%u KB <= max %u KB)", 1604 (U32)(zds->fParams.windowSize >>10), 1605 (U32)(zds->maxWindowSize >> 10) ); 1606 zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN); 1607 RETURN_ERROR_IF(zds->fParams.windowSize > zds->maxWindowSize, 1608 frameParameter_windowTooLarge); 1609 1610 /* Adapt buffer sizes to frame header instructions */ 1611 { size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */); 1612 size_t const neededOutBuffSize = ZSTD_decodingBufferSize_min(zds->fParams.windowSize, zds->fParams.frameContentSize); 1613 if ((zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize)) { 1614 size_t const bufferSize = neededInBuffSize + neededOutBuffSize; 1615 DEBUGLOG(4, "inBuff : from %u to %u", 1616 (U32)zds->inBuffSize, (U32)neededInBuffSize); 1617 DEBUGLOG(4, "outBuff : from %u to %u", 1618 (U32)zds->outBuffSize, (U32)neededOutBuffSize); 1619 if (zds->staticSize) { /* static DCtx */ 1620 DEBUGLOG(4, "staticSize : %u", (U32)zds->staticSize); 1621 assert(zds->staticSize >= sizeof(ZSTD_DCtx)); /* controlled at init */ 1622 RETURN_ERROR_IF( 1623 bufferSize > zds->staticSize - sizeof(ZSTD_DCtx), 1624 memory_allocation); 1625 } else { 1626 ZSTD_free(zds->inBuff, zds->customMem); 1627 zds->inBuffSize = 0; 1628 zds->outBuffSize = 0; 1629 zds->inBuff = (char*)ZSTD_malloc(bufferSize, zds->customMem); 1630 RETURN_ERROR_IF(zds->inBuff == NULL, memory_allocation); 1631 } 1632 zds->inBuffSize = neededInBuffSize; 1633 zds->outBuff = zds->inBuff + zds->inBuffSize; 1634 zds->outBuffSize = neededOutBuffSize; 1635 } } 1636 zds->streamStage = zdss_read; 1637 /* fall-through */ 1638 1639 case zdss_read: 1640 DEBUGLOG(5, "stage zdss_read"); 1641 { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds); 1642 DEBUGLOG(5, "neededInSize = %u", (U32)neededInSize); 1643 if (neededInSize==0) { /* end of frame */ 1644 zds->streamStage = zdss_init; 1645 someMoreWork = 0; 1646 break; 1647 } 1648 if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */ 1649 int const isSkipFrame = ZSTD_isSkipFrame(zds); 1650 size_t const decodedSize = ZSTD_decompressContinue(zds, 1651 zds->outBuff + zds->outStart, (isSkipFrame ? 0 : zds->outBuffSize - zds->outStart), 1652 ip, neededInSize); 1653 if (ZSTD_isError(decodedSize)) return decodedSize; 1654 ip += neededInSize; 1655 if (!decodedSize && !isSkipFrame) break; /* this was just a header */ 1656 zds->outEnd = zds->outStart + decodedSize; 1657 zds->streamStage = zdss_flush; 1658 break; 1659 } } 1660 if (ip==iend) { someMoreWork = 0; break; } /* no more input */ 1661 zds->streamStage = zdss_load; 1662 /* fall-through */ 1663 1664 case zdss_load: 1665 { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds); 1666 size_t const toLoad = neededInSize - zds->inPos; 1667 int const isSkipFrame = ZSTD_isSkipFrame(zds); 1668 size_t loadedSize; 1669 if (isSkipFrame) { 1670 loadedSize = MIN(toLoad, (size_t)(iend-ip)); 1671 } else { 1672 RETURN_ERROR_IF(toLoad > zds->inBuffSize - zds->inPos, 1673 corruption_detected, 1674 "should never happen"); 1675 loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, iend-ip); 1676 } 1677 ip += loadedSize; 1678 zds->inPos += loadedSize; 1679 if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */ 1680 1681 /* decode loaded input */ 1682 { size_t const decodedSize = ZSTD_decompressContinue(zds, 1683 zds->outBuff + zds->outStart, zds->outBuffSize - zds->outStart, 1684 zds->inBuff, neededInSize); 1685 if (ZSTD_isError(decodedSize)) return decodedSize; 1686 zds->inPos = 0; /* input is consumed */ 1687 if (!decodedSize && !isSkipFrame) { zds->streamStage = zdss_read; break; } /* this was just a header */ 1688 zds->outEnd = zds->outStart + decodedSize; 1689 } } 1690 zds->streamStage = zdss_flush; 1691 /* fall-through */ 1692 1693 case zdss_flush: 1694 { size_t const toFlushSize = zds->outEnd - zds->outStart; 1695 size_t const flushedSize = ZSTD_limitCopy(op, oend-op, zds->outBuff + zds->outStart, toFlushSize); 1696 op += flushedSize; 1697 zds->outStart += flushedSize; 1698 if (flushedSize == toFlushSize) { /* flush completed */ 1699 zds->streamStage = zdss_read; 1700 if ( (zds->outBuffSize < zds->fParams.frameContentSize) 1701 && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize) ) { 1702 DEBUGLOG(5, "restart filling outBuff from beginning (left:%i, needed:%u)", 1703 (int)(zds->outBuffSize - zds->outStart), 1704 (U32)zds->fParams.blockSizeMax); 1705 zds->outStart = zds->outEnd = 0; 1706 } 1707 break; 1708 } } 1709 /* cannot complete flush */ 1710 someMoreWork = 0; 1711 break; 1712 1713 default: 1714 assert(0); /* impossible */ 1715 RETURN_ERROR(GENERIC); /* some compiler require default to do something */ 1716 } } 1717 1718 /* result */ 1719 input->pos = (size_t)(ip - (const char*)(input->src)); 1720 output->pos = (size_t)(op - (char*)(output->dst)); 1721 if ((ip==istart) && (op==ostart)) { /* no forward progress */ 1722 zds->noForwardProgress ++; 1723 if (zds->noForwardProgress >= ZSTD_NO_FORWARD_PROGRESS_MAX) { 1724 RETURN_ERROR_IF(op==oend, dstSize_tooSmall); 1725 RETURN_ERROR_IF(ip==iend, srcSize_wrong); 1726 assert(0); 1727 } 1728 } else { 1729 zds->noForwardProgress = 0; 1730 } 1731 { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds); 1732 if (!nextSrcSizeHint) { /* frame fully decoded */ 1733 if (zds->outEnd == zds->outStart) { /* output fully flushed */ 1734 if (zds->hostageByte) { 1735 if (input->pos >= input->size) { 1736 /* can't release hostage (not present) */ 1737 zds->streamStage = zdss_read; 1738 return 1; 1739 } 1740 input->pos++; /* release hostage */ 1741 } /* zds->hostageByte */ 1742 return 0; 1743 } /* zds->outEnd == zds->outStart */ 1744 if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */ 1745 input->pos--; /* note : pos > 0, otherwise, impossible to finish reading last block */ 1746 zds->hostageByte=1; 1747 } 1748 return 1; 1749 } /* nextSrcSizeHint==0 */ 1750 nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block); /* preload header of next block */ 1751 assert(zds->inPos <= nextSrcSizeHint); 1752 nextSrcSizeHint -= zds->inPos; /* part already loaded*/ 1753 return nextSrcSizeHint; 1754 } 1755 } 1756 1757 size_t ZSTD_decompressStream_simpleArgs ( 1758 ZSTD_DCtx* dctx, 1759 void* dst, size_t dstCapacity, size_t* dstPos, 1760 const void* src, size_t srcSize, size_t* srcPos) 1761 { 1762 ZSTD_outBuffer output = { dst, dstCapacity, *dstPos }; 1763 ZSTD_inBuffer input = { src, srcSize, *srcPos }; 1764 /* ZSTD_compress_generic() will check validity of dstPos and srcPos */ 1765 size_t const cErr = ZSTD_decompressStream(dctx, &output, &input); 1766 *dstPos = output.pos; 1767 *srcPos = input.pos; 1768 return cErr; 1769 } 1770