Expose frameSrcSize to experimental API

This commit is contained in:
Sean Purcell 2017-02-10 11:38:57 -08:00
parent 5069b6c2c3
commit d7bfcac18a
17 changed files with 89 additions and 80 deletions

View File

@ -306,8 +306,6 @@ size_t ZSTD_getFrameParams(ZSTD_frameParams* fparamsPtr, const void* src, size_t
return 0; return 0;
} }
static size_t ZSTD_frameSrcSize(const void* src, size_t srcSize);
/** ZSTD_getFrameContentSize() : /** ZSTD_getFrameContentSize() :
* compatible with legacy mode * compatible with legacy mode
* @return : decompressed size of the single frame pointed to be `src` if known, otherwise * @return : decompressed size of the single frame pointed to be `src` if known, otherwise
@ -371,17 +369,7 @@ unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize)
totalDstSize += ret; totalDstSize += ret;
} }
{ {
size_t frameSrcSize; size_t const frameSrcSize = ZSTD_getFrameCompressedSize(src, srcSize);
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
if (ZSTD_isLegacy(src, srcSize))
{
frameSrcSize = ZSTD_frameSrcSizeLegacy(src, srcSize);
}
else
#endif
{
frameSrcSize = ZSTD_frameSrcSize(src, srcSize);
}
if (ZSTD_isError(frameSrcSize)) { if (ZSTD_isError(frameSrcSize)) {
return ZSTD_CONTENTSIZE_ERROR; return ZSTD_CONTENTSIZE_ERROR;
} }
@ -1449,47 +1437,57 @@ size_t ZSTD_generateNxBytes(void* dst, size_t dstCapacity, BYTE byte, size_t len
return length; return length;
} }
static size_t ZSTD_frameSrcSize(const void *src, size_t srcSize) /** ZSTD_getFrameCompressedSize() :
* compatible with legacy mode
* `src` must point to the start of a ZSTD or ZSTD legacy frame
* `srcSize` must be at least as large as the frame contained
* @return : the compressed size of the frame starting at `src` */
size_t ZSTD_getFrameCompressedSize(const void *src, size_t srcSize)
{ {
const BYTE* ip = (const BYTE*)src; #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
const BYTE* const ipstart = ip; if (ZSTD_isLegacy(src, srcSize)) return ZSTD_getFrameCompressedSizeLegacy(src, srcSize);
size_t remainingSize = srcSize; #endif
ZSTD_frameParams fParams;
size_t const headerSize = ZSTD_frameHeaderSize(ip, remainingSize);
if (ZSTD_isError(headerSize)) return headerSize;
/* Frame Header */
{ {
size_t const ret = ZSTD_getFrameParams(&fParams, ip, remainingSize); const BYTE* ip = (const BYTE*)src;
if (ZSTD_isError(ret)) return ret; const BYTE* const ipstart = ip;
if (ret > 0) return ERROR(srcSize_wrong); size_t remainingSize = srcSize;
ZSTD_frameParams fParams;
size_t const headerSize = ZSTD_frameHeaderSize(ip, remainingSize);
if (ZSTD_isError(headerSize)) return headerSize;
/* Frame Header */
{
size_t const ret = ZSTD_getFrameParams(&fParams, ip, remainingSize);
if (ZSTD_isError(ret)) return ret;
if (ret > 0) return ERROR(srcSize_wrong);
}
ip += headerSize;
remainingSize -= headerSize;
/* Loop on each block */
while (1) {
blockProperties_t blockProperties;
size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
if (ZSTD_isError(cBlockSize)) return cBlockSize;
if (ZSTD_blockHeaderSize + cBlockSize > remainingSize) return ERROR(srcSize_wrong);
ip += ZSTD_blockHeaderSize + cBlockSize;
remainingSize -= ZSTD_blockHeaderSize + cBlockSize;
if (blockProperties.lastBlock) break;
}
if (fParams.checksumFlag) { /* Frame content checksum */
if (remainingSize < 4) return ERROR(srcSize_wrong);
ip += 4;
remainingSize -= 4;
}
return ip - ipstart;
} }
ip += headerSize;
remainingSize -= headerSize;
/* Loop on each block */
while (1) {
blockProperties_t blockProperties;
size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
if (ZSTD_isError(cBlockSize)) return cBlockSize;
if (ZSTD_blockHeaderSize + cBlockSize > remainingSize) return ERROR(srcSize_wrong);
ip += ZSTD_blockHeaderSize + cBlockSize;
remainingSize -= ZSTD_blockHeaderSize + cBlockSize;
if (blockProperties.lastBlock) break;
}
if (fParams.checksumFlag) { /* Frame content checksum verification */
if (remainingSize < 4) return ERROR(srcSize_wrong);
ip += 4;
remainingSize -= 4;
}
return ip - ipstart;
} }
/*! ZSTD_decompressFrame() : /*! ZSTD_decompressFrame() :
@ -1578,7 +1576,7 @@ static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
if (ZSTD_isLegacy(src, srcSize)) { if (ZSTD_isLegacy(src, srcSize)) {
size_t const frameSize = ZSTD_frameSrcSizeLegacy(src, srcSize); size_t const frameSize = ZSTD_getFrameCompressedSizeLegacy(src, srcSize);
size_t decodedSize; size_t decodedSize;
if (ZSTD_isError(frameSize)) return frameSize; if (ZSTD_isError(frameSize)) return frameSize;

View File

@ -123,26 +123,26 @@ MEM_STATIC size_t ZSTD_decompressLegacy(
} }
} }
MEM_STATIC size_t ZSTD_frameSrcSizeLegacy(const void *src, MEM_STATIC size_t ZSTD_getFrameCompressedSizeLegacy(const void *src,
size_t compressedSize) size_t compressedSize)
{ {
U32 const version = ZSTD_isLegacy(src, compressedSize); U32 const version = ZSTD_isLegacy(src, compressedSize);
switch(version) switch(version)
{ {
case 1 : case 1 :
return ZSTDv01_frameSrcSize(src, compressedSize); return ZSTDv01_getFrameCompressedSize(src, compressedSize);
case 2 : case 2 :
return ZSTDv02_frameSrcSize(src, compressedSize); return ZSTDv02_getFrameCompressedSize(src, compressedSize);
case 3 : case 3 :
return ZSTDv03_frameSrcSize(src, compressedSize); return ZSTDv03_getFrameCompressedSize(src, compressedSize);
case 4 : case 4 :
return ZSTDv04_frameSrcSize(src, compressedSize); return ZSTDv04_getFrameCompressedSize(src, compressedSize);
case 5 : case 5 :
return ZSTDv05_frameSrcSize(src, compressedSize); return ZSTDv05_getFrameCompressedSize(src, compressedSize);
case 6 : case 6 :
return ZSTDv06_frameSrcSize(src, compressedSize); return ZSTDv06_getFrameCompressedSize(src, compressedSize);
case 7 : case 7 :
return ZSTDv07_frameSrcSize(src, compressedSize); return ZSTDv07_getFrameCompressedSize(src, compressedSize);
default : default :
return ERROR(prefix_unknown); return ERROR(prefix_unknown);
} }

View File

@ -1992,7 +1992,7 @@ size_t ZSTDv01_decompress(void* dst, size_t maxDstSize, const void* src, size_t
return ZSTDv01_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize); return ZSTDv01_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);
} }
size_t ZSTDv01_frameSrcSize(const void* src, size_t srcSize) size_t ZSTDv01_getFrameCompressedSize(const void* src, size_t srcSize)
{ {
const BYTE* ip = (const BYTE*)src; const BYTE* ip = (const BYTE*)src;
size_t remainingSize = srcSize; size_t remainingSize = srcSize;

View File

@ -40,7 +40,7 @@ ZSTDv01_getFrameSrcSize() : get the source length of a ZSTD frame compliant with
return : the number of bytes that would be read to decompress this frame return : the number of bytes that would be read to decompress this frame
or an errorCode if it fails (which can be tested using ZSTDv01_isError()) or an errorCode if it fails (which can be tested using ZSTDv01_isError())
*/ */
size_t ZSTDv01_frameSrcSize(const void* src, size_t compressedSize); size_t ZSTDv01_getFrameCompressedSize(const void* src, size_t compressedSize);
/** /**
ZSTDv01_isError() : tells if the result of ZSTDv01_decompress() is an error ZSTDv01_isError() : tells if the result of ZSTDv01_decompress() is an error

View File

@ -3378,7 +3378,7 @@ static size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, siz
return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize); return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);
} }
static size_t ZSTD_frameSrcSize(const void *src, size_t srcSize) static size_t ZSTD_getFrameCompressedSize(const void *src, size_t srcSize)
{ {
const BYTE* ip = (const BYTE*)src; const BYTE* ip = (const BYTE*)src;
@ -3524,9 +3524,9 @@ size_t ZSTDv02_decompress( void* dst, size_t maxOriginalSize,
return ZSTD_decompress(dst, maxOriginalSize, src, compressedSize); return ZSTD_decompress(dst, maxOriginalSize, src, compressedSize);
} }
size_t ZSTDv02_frameSrcSize(const void *src, size_t compressedSize) size_t ZSTDv02_getFrameCompressedSize(const void *src, size_t compressedSize)
{ {
return ZSTD_frameSrcSize(src, compressedSize); return ZSTD_getFrameCompressedSize(src, compressedSize);
} }
ZSTDv02_Dctx* ZSTDv02_createDCtx(void) ZSTDv02_Dctx* ZSTDv02_createDCtx(void)

View File

@ -40,7 +40,7 @@ ZSTDv02_getFrameSrcSize() : get the source length of a ZSTD frame compliant with
return : the number of bytes that would be read to decompress this frame return : the number of bytes that would be read to decompress this frame
or an errorCode if it fails (which can be tested using ZSTDv02_isError()) or an errorCode if it fails (which can be tested using ZSTDv02_isError())
*/ */
size_t ZSTDv02_frameSrcSize(const void* src, size_t compressedSize); size_t ZSTDv02_getFrameCompressedSize(const void* src, size_t compressedSize);
/** /**
ZSTDv02_isError() : tells if the result of ZSTDv02_decompress() is an error ZSTDv02_isError() : tells if the result of ZSTDv02_decompress() is an error

View File

@ -3019,7 +3019,7 @@ static size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, siz
return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize); return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);
} }
static size_t ZSTD_frameSrcSize(const void* src, size_t srcSize) static size_t ZSTD_getFrameCompressedSize(const void* src, size_t srcSize)
{ {
const BYTE* ip = (const BYTE*)src; const BYTE* ip = (const BYTE*)src;
size_t remainingSize = srcSize; size_t remainingSize = srcSize;
@ -3165,9 +3165,9 @@ size_t ZSTDv03_decompress( void* dst, size_t maxOriginalSize,
return ZSTD_decompress(dst, maxOriginalSize, src, compressedSize); return ZSTD_decompress(dst, maxOriginalSize, src, compressedSize);
} }
size_t ZSTDv03_frameSrcSize(const void* src, size_t srcSize) size_t ZSTDv03_getFrameCompressedSize(const void* src, size_t srcSize)
{ {
return ZSTD_frameSrcSize(src, srcSize); return ZSTD_getFrameCompressedSize(src, srcSize);
} }
ZSTDv03_Dctx* ZSTDv03_createDCtx(void) ZSTDv03_Dctx* ZSTDv03_createDCtx(void)

View File

@ -40,7 +40,7 @@ ZSTDv03_getFrameSrcSize() : get the source length of a ZSTD frame compliant with
return : the number of bytes that would be read to decompress this frame return : the number of bytes that would be read to decompress this frame
or an errorCode if it fails (which can be tested using ZSTDv03_isError()) or an errorCode if it fails (which can be tested using ZSTDv03_isError())
*/ */
size_t ZSTDv03_frameSrcSize(const void* src, size_t compressedSize); size_t ZSTDv03_getFrameCompressedSize(const void* src, size_t compressedSize);
/** /**
ZSTDv03_isError() : tells if the result of ZSTDv03_decompress() is an error ZSTDv03_isError() : tells if the result of ZSTDv03_decompress() is an error

View File

@ -3326,7 +3326,7 @@ static size_t ZSTD_decompress_usingDict(ZSTD_DCtx* ctx,
return op-ostart; return op-ostart;
} }
static size_t ZSTD_frameSrcSize(const void* src, size_t srcSize) static size_t ZSTD_getFrameCompressedSize(const void* src, size_t srcSize)
{ {
const BYTE* ip = (const BYTE*)src; const BYTE* ip = (const BYTE*)src;
size_t remainingSize = srcSize; size_t remainingSize = srcSize;
@ -3782,9 +3782,9 @@ size_t ZSTDv04_decompress(void* dst, size_t maxDstSize, const void* src, size_t
#endif #endif
} }
size_t ZSTDv04_frameSrcSize(const void* src, size_t srcSize) size_t ZSTDv04_getFrameCompressedSize(const void* src, size_t srcSize)
{ {
return ZSTD_frameSrcSize(src, srcSize); return ZSTD_getFrameCompressedSize(src, srcSize);
} }
size_t ZSTDv04_resetDCtx(ZSTDv04_Dctx* dctx) { return ZSTD_resetDCtx(dctx); } size_t ZSTDv04_resetDCtx(ZSTDv04_Dctx* dctx) { return ZSTD_resetDCtx(dctx); }

View File

@ -40,7 +40,7 @@ ZSTDv04_getFrameSrcSize() : get the source length of a ZSTD frame compliant with
return : the number of bytes that would be read to decompress this frame return : the number of bytes that would be read to decompress this frame
or an errorCode if it fails (which can be tested using ZSTDv04_isError()) or an errorCode if it fails (which can be tested using ZSTDv04_isError())
*/ */
size_t ZSTDv04_frameSrcSize(const void* src, size_t compressedSize); size_t ZSTDv04_getFrameCompressedSize(const void* src, size_t compressedSize);
/** /**
ZSTDv04_isError() : tells if the result of ZSTDv04_decompress() is an error ZSTDv04_isError() : tells if the result of ZSTDv04_decompress() is an error

View File

@ -3583,7 +3583,7 @@ size_t ZSTDv05_decompress(void* dst, size_t maxDstSize, const void* src, size_t
#endif #endif
} }
size_t ZSTDv05_frameSrcSize(const void *src, size_t srcSize) size_t ZSTDv05_getFrameCompressedSize(const void *src, size_t srcSize)
{ {
const BYTE* ip = (const BYTE*)src; const BYTE* ip = (const BYTE*)src;
size_t remainingSize = srcSize; size_t remainingSize = srcSize;

View File

@ -38,7 +38,7 @@ ZSTDv05_getFrameSrcSize() : get the source length of a ZSTD frame
return : the number of bytes that would be read to decompress this frame return : the number of bytes that would be read to decompress this frame
or an errorCode if it fails (which can be tested using ZSTDv05_isError()) or an errorCode if it fails (which can be tested using ZSTDv05_isError())
*/ */
size_t ZSTDv05_frameSrcSize(const void* src, size_t compressedSize); size_t ZSTDv05_getFrameCompressedSize(const void* src, size_t compressedSize);
/* ************************************* /* *************************************
* Helper functions * Helper functions

View File

@ -3729,7 +3729,7 @@ size_t ZSTDv06_decompress(void* dst, size_t dstCapacity, const void* src, size_t
#endif #endif
} }
size_t ZSTDv06_frameSrcSize(const void* src, size_t srcSize) size_t ZSTDv06_getFrameCompressedSize(const void* src, size_t srcSize)
{ {
const BYTE* ip = (const BYTE*)src; const BYTE* ip = (const BYTE*)src;
size_t remainingSize = srcSize; size_t remainingSize = srcSize;

View File

@ -47,7 +47,7 @@ ZSTDv06_getFrameSrcSize() : get the source length of a ZSTD frame
return : the number of bytes that would be read to decompress this frame return : the number of bytes that would be read to decompress this frame
or an errorCode if it fails (which can be tested using ZSTDv06_isError()) or an errorCode if it fails (which can be tested using ZSTDv06_isError())
*/ */
size_t ZSTDv06_frameSrcSize(const void* src, size_t compressedSize); size_t ZSTDv06_getFrameCompressedSize(const void* src, size_t compressedSize);
/* ************************************* /* *************************************
* Helper functions * Helper functions

View File

@ -3968,7 +3968,7 @@ size_t ZSTDv07_decompress(void* dst, size_t dstCapacity, const void* src, size_t
#endif #endif
} }
size_t ZSTDv07_frameSrcSize(const void* src, size_t srcSize) size_t ZSTDv07_getFrameCompressedSize(const void* src, size_t srcSize)
{ {
const BYTE* ip = (const BYTE*)src; const BYTE* ip = (const BYTE*)src;
size_t remainingSize = srcSize; size_t remainingSize = srcSize;

View File

@ -54,7 +54,7 @@ ZSTDv07_getFrameSrcSize() : get the source length of a ZSTD frame
return : the number of bytes that would be read to decompress this frame return : the number of bytes that would be read to decompress this frame
or an errorCode if it fails (which can be tested using ZSTDv07_isError()) or an errorCode if it fails (which can be tested using ZSTDv07_isError())
*/ */
size_t ZSTDv07_frameSrcSize(const void* src, size_t compressedSize); size_t ZSTDv07_getFrameCompressedSize(const void* src, size_t compressedSize);
/*====== Helper functions ======*/ /*====== Helper functions ======*/
ZSTDLIBv07_API unsigned ZSTDv07_isError(size_t code); /*!< tells if a `size_t` function result is an error code */ ZSTDLIBv07_API unsigned ZSTDv07_isError(size_t code); /*!< tells if a `size_t` function result is an error code */

View File

@ -396,6 +396,17 @@ typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
typedef void (*ZSTD_freeFunction) (void* opaque, void* address); typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem; typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
/***************************************
* Compressed size functions
***************************************/
/*! ZSTD_getFrameCompressedSize() :
* `src` should point to the start of a ZSTD encoded frame
* `srcSize` must be at least as large as the frame
* @return : the compressed size of the frame pointed to by `src`, suitable to pass to
* `ZSTD_decompress` or similar, or an error code if given invalid input. */
ZSTDLIB_API size_t ZSTD_getFrameCompressedSize(const void* src, size_t srcSize);
/*************************************** /***************************************
* Decompressed size functions * Decompressed size functions
***************************************/ ***************************************/