From 03026c3b1d34e7220b42cbb33855dd213939ac5d Mon Sep 17 00:00:00 2001 From: shakeelrao Date: Fri, 1 Mar 2019 00:03:50 -0800 Subject: [PATCH] change compressedBound to ULL --- lib/decompress/zstd_decompress.c | 31 ++++++++++++++++--------------- lib/zstd.h | 16 ++++++++-------- tests/fuzzer.c | 6 +++--- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/lib/decompress/zstd_decompress.c b/lib/decompress/zstd_decompress.c index 4b9402d69..630291f08 100644 --- a/lib/decompress/zstd_decompress.c +++ b/lib/decompress/zstd_decompress.c @@ -435,12 +435,13 @@ static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t he /** * Contains the compressed frame size and an upper-bound for the decompressed frame size. - * Note: before using `compressedSize` or `decompressedBound`, - * you must check the field for errors using ZSTD_isError(). + * Note: before using `compressedSize` you must check for errors using ZSTD_isError(). + * similarly, before using `decompressedBound`, you must check for errors using: + * `decompressedBound` != ZSTD_CONTENTSIZE_UNKNOWN */ typedef struct { size_t compressedSize; - size_t decompressedBound; + unsigned long long decompressedBound; } ZSTD_frameSizeInfo; static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize) @@ -451,7 +452,7 @@ static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) if (ZSTD_isLegacy(src, srcSize)) { frameSizeInfo.compressedSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize); - frameSizeInfo.decompressedBound = ERROR(version_unsupported); + frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR; return frameSizeInfo; } #endif @@ -477,7 +478,7 @@ static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize } if (ret > 0) { frameSizeInfo.compressedSize = ERROR(srcSize_wrong); - frameSizeInfo.decompressedBound = ERROR(srcSize_wrong); + frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR; return frameSizeInfo; } } @@ -491,13 +492,13 @@ static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties); if (ZSTD_isError(cBlockSize)) { frameSizeInfo.compressedSize = cBlockSize; - frameSizeInfo.decompressedBound = cBlockSize; + frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR; return frameSizeInfo; } if (ZSTD_blockHeaderSize + cBlockSize > remainingSize) { frameSizeInfo.compressedSize = ERROR(srcSize_wrong); - frameSizeInfo.decompressedBound = ERROR(srcSize_wrong); + frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR; return frameSizeInfo; } @@ -512,7 +513,7 @@ static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize if (zfh.checksumFlag) { if (remainingSize < 4) { frameSizeInfo.compressedSize = ERROR(srcSize_wrong); - frameSizeInfo.decompressedBound = ERROR(srcSize_wrong); + frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR; return frameSizeInfo; } ip += 4; @@ -542,19 +543,19 @@ size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize) * currently incompatible with legacy mode * `src` must point to the start of a ZSTD frame or a skippeable frame * `srcSize` must be at least as large as the frame contained - * @return : maximum decompressed size of the compressed source - * or an error code which can be tested with ZSTD_isError() + * @return : the maximum decompressed size of the compressed source */ -size_t ZSTD_decompressBound(const void* src, size_t srcSize) +unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize) { - size_t bound = 0; + unsigned long long bound = 0; /* Iterate over each frame */ while (srcSize > 0) { ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize); size_t compressedSize = frameSizeInfo.compressedSize; - size_t decompressedBound = frameSizeInfo.decompressedBound; - FORWARD_IF_ERROR(compressedSize); - FORWARD_IF_ERROR(decompressedBound); + unsigned long long decompressedBound = frameSizeInfo.decompressedBound; + if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR) { + return ZSTD_CONTENTSIZE_ERROR; + } src = (const BYTE*)src + compressedSize; srcSize -= compressedSize; bound += decompressedBound; diff --git a/lib/zstd.h b/lib/zstd.h index 6d457cfdc..e49a4048a 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -1107,16 +1107,16 @@ ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t * currently incompatible with legacy mode * `src` must point to the start of a ZSTD frame or a skippeable frame * `srcSize` must be at least as large as the frame contained - * @return : maximum decompressed size of the compressed source - * or an error code which can be tested with ZSTD_isError() + * @return : - the maximum decompressed size of the compressed source + * - if an error occured: ZSTD_CONTENTSIZE_ERROR * - * note 1 : the bound is exact when Frame_Content_Size field is available in EVERY frame of `src`. - * note 2 : when Frame_Content_Size isn't provided, the upper-bound for that frame is calculated by: - * upper-bound = min(128 KB, Window_Size) - * note 3 : we always use Frame_Content_Size to bound the decompressed frame size if it's present. - ** the above formula is only used when Frame_Content_Size is missing. + * note 1 : an error can occur if `src` points to a legacy frame or an invalid/incorrectly formatted frame. + * note 2 : the bound is exact when Frame_Content_Size field is available in EVERY frame of `src`. + * note 3 : when Frame_Content_Size isn't provided, the upper-bound for that frame is calculated by: + * upper-bound = min(128 KB, Window_Size) + * note 4 : we always use Frame_Content_Size to bound the decompressed frame size if it's present. */ -ZSTDLIB_API size_t ZSTD_decompressBound(const void* src, size_t srcSice); +ZSTDLIB_API unsigned long long ZSTD_decompressBound(const void* src, size_t srcSice); /*! ZSTD_frameHeaderSize() : * srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX. diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 2ac1f0f47..9efa232a9 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -378,7 +378,7 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(3, "test%3i : tight ZSTD_decompressBound test : ", testNb++); { - size_t rSize = ZSTD_decompressBound(compressedBuffer, cSize); + unsigned long long rSize = ZSTD_decompressBound(compressedBuffer, cSize); if (rSize != CNBuffSize) goto _output_error; } DISPLAYLEVEL(3, "OK \n"); @@ -909,8 +909,8 @@ static int basicUnitTests(U32 seed, double compressibility) DISPLAYLEVEL(3, "OK \n"); DISPLAYLEVEL(3, "test%3i : get tight decompressed bound of multiple frames : ", testNb++); - { size_t const rSize = ZSTD_decompressBound(compressedBuffer, cSize); - if (rSize != CNBuffSize / 2) goto _output_error; } + { unsigned long long const r = ZSTD_decompressBound(compressedBuffer, cSize); + if (r != CNBuffSize / 2) goto _output_error; } DISPLAYLEVEL(3, "OK \n"); DISPLAYLEVEL(3, "test%3i : decompress multiple frames : ", testNb++);