1
0
mirror of https://github.com/facebook/zstd.git synced 2025-07-30 22:23:13 +03:00

added test with ZSTD_decompress_generic() + ZSTD_DCtx_refPrefix()

also :
clarified stage condition to accept new parameters,
fixed initializers correspondingly.
This commit is contained in:
Yann Collet
2018-03-20 16:16:13 -07:00
parent 2af41592ea
commit 6cda8c932c
2 changed files with 32 additions and 33 deletions

View File

@ -2608,7 +2608,7 @@ size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t di
DEBUGLOG(4, "ZSTD_initDStream_usingDict"); DEBUGLOG(4, "ZSTD_initDStream_usingDict");
zds->streamStage = zdss_init; zds->streamStage = zdss_init;
CHECK_F( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) ); CHECK_F( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) );
return ZSTD_resetDStream(zds); return ZSTD_frameHeaderSize_prefix;
} }
/* note : this variant can't fail */ /* note : this variant can't fail */
@ -2628,38 +2628,36 @@ size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
/* ZSTD_initDStream_usingDDict() : /* ZSTD_initDStream_usingDDict() :
* ddict will just be referenced, and must outlive decompression session * ddict will just be referenced, and must outlive decompression session
* this function cannot fail */ * this function cannot fail */
size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict) size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* dctx, const ZSTD_DDict* ddict)
{ {
size_t const initResult = ZSTD_initDStream(zds); size_t const initResult = ZSTD_initDStream(dctx);
zds->ddict = ddict; dctx->ddict = ddict;
return initResult; return initResult;
} }
/* ZSTD_resetDStream() : /* ZSTD_resetDStream() :
* return : expected size, aka ZSTD_frameHeaderSize_prefix. * return : expected size, aka ZSTD_frameHeaderSize_prefix.
* this function cannot fail */ * this function cannot fail */
size_t ZSTD_resetDStream(ZSTD_DStream* zds) size_t ZSTD_resetDStream(ZSTD_DStream* dctx)
{ {
DEBUGLOG(4, "ZSTD_resetDStream"); DEBUGLOG(4, "ZSTD_resetDStream");
zds->streamStage = zdss_loadHeader; dctx->streamStage = zdss_loadHeader;
zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0; dctx->lhSize = dctx->inPos = dctx->outStart = dctx->outEnd = 0;
zds->legacyVersion = 0; dctx->legacyVersion = 0;
zds->hostageByte = 0; dctx->hostageByte = 0;
return ZSTD_frameHeaderSize_prefix; return ZSTD_frameHeaderSize_prefix;
} }
size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, size_t ZSTD_setDStreamParameter(ZSTD_DStream* dctx,
ZSTD_DStreamParameter_e paramType, unsigned paramValue) ZSTD_DStreamParameter_e paramType, unsigned paramValue)
{ {
ZSTD_STATIC_ASSERT((unsigned)zdss_loadHeader >= (unsigned)zdss_init); if (dctx->streamStage != zdss_init) return ERROR(stage_wrong);
if ((unsigned)zds->streamStage > (unsigned)zdss_loadHeader)
return ERROR(stage_wrong);
switch(paramType) switch(paramType)
{ {
default : return ERROR(parameter_unsupported); default : return ERROR(parameter_unsupported);
case DStream_p_maxWindowSize : case DStream_p_maxWindowSize :
DEBUGLOG(4, "setting maxWindowSize = %u KB", paramValue >> 10); DEBUGLOG(4, "setting maxWindowSize = %u KB", paramValue >> 10);
zds->maxWindowSize = paramValue ? paramValue : (U32)(-1); dctx->maxWindowSize = paramValue ? paramValue : (U32)(-1);
break; break;
} }
return 0; return 0;
@ -2667,9 +2665,7 @@ size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds,
size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize) size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize)
{ {
ZSTD_STATIC_ASSERT((unsigned)zdss_loadHeader >= (unsigned)zdss_init); if (dctx->streamStage != zdss_init) return ERROR(stage_wrong);
if ((unsigned)dctx->streamStage > (unsigned)zdss_loadHeader)
return ERROR(stage_wrong);
dctx->maxWindowSize = maxWindowSize; dctx->maxWindowSize = maxWindowSize;
return 0; return 0;
} }
@ -2677,17 +2673,15 @@ size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize)
size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format) size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format)
{ {
DEBUGLOG(4, "ZSTD_DCtx_setFormat : %u", (unsigned)format); DEBUGLOG(4, "ZSTD_DCtx_setFormat : %u", (unsigned)format);
ZSTD_STATIC_ASSERT((unsigned)zdss_loadHeader >= (unsigned)zdss_init); if (dctx->streamStage != zdss_init) return ERROR(stage_wrong);
if ((unsigned)dctx->streamStage > (unsigned)zdss_loadHeader)
return ERROR(stage_wrong);
dctx->format = format; dctx->format = format;
return 0; return 0;
} }
size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds) size_t ZSTD_sizeof_DStream(const ZSTD_DStream* dctx)
{ {
return ZSTD_sizeof_DCtx(zds); return ZSTD_sizeof_DCtx(dctx);
} }
size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize) size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize)
@ -2968,8 +2962,8 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
return 1; return 1;
} /* nextSrcSizeHint==0 */ } /* nextSrcSizeHint==0 */
nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block); /* preload header of next block */ nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block); /* preload header of next block */
if (zds->inPos > nextSrcSizeHint) return ERROR(GENERIC); /* should never happen */ assert(zds->inPos <= nextSrcSizeHint);
nextSrcSizeHint -= zds->inPos; /* already loaded*/ nextSrcSizeHint -= zds->inPos; /* part already loaded*/
return nextSrcSizeHint; return nextSrcSizeHint;
} }
} }

View File

@ -576,10 +576,11 @@ static int basicUnitTests(U32 seed, double compressibility)
{ size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */ if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); } DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); }
ZSTD_DCtx_reset(zd); /* leave zd in good shape for next tests */
DISPLAYLEVEL(3, "test%3i : dictionary source size and level : ", testNb++); DISPLAYLEVEL(3, "test%3i : dictionary source size and level : ", testNb++);
{ ZSTD_DCtx* const dctx = ZSTD_createDCtx(); { ZSTD_DCtx* const dctx = ZSTD_createDCtx();
int const maxLevel = 15; int const maxLevel = 16; /* first level with zstd_opt */
int level; int level;
assert(maxLevel < ZSTD_maxCLevel()); assert(maxLevel < ZSTD_maxCLevel());
CHECK_Z( ZSTD_DCtx_loadDictionary_byReference(dctx, dictionary.start, dictionary.filled) ); CHECK_Z( ZSTD_DCtx_loadDictionary_byReference(dctx, dictionary.start, dictionary.filled) );
@ -662,14 +663,18 @@ static int basicUnitTests(U32 seed, double compressibility)
cSize = outBuff.pos; cSize = outBuff.pos;
DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100); DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
DISPLAYLEVEL(3, "test%3i : decompress with dictionary : ", testNb++); DISPLAYLEVEL(3, "test%3i : decompress with ZSTD_DCtx_refPrefix : ", testNb++);
{ size_t const r = ZSTD_decompress_usingDict(zd, CHECK_Z( ZSTD_DCtx_refPrefix(zd, dictionary.start, dictionary.filled) );
decodedBuffer, CNBufferSize, outBuff.dst = decodedBuffer;
compressedBuffer, cSize, outBuff.size = CNBufferSize;
dictionary.start, dictionary.filled); outBuff.pos = 0;
if (ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */ inBuff.src = compressedBuffer;
DISPLAYLEVEL(3, "OK \n"); inBuff.size = cSize;
} inBuff.pos = 0;
CHECK_Z( ZSTD_decompress_generic(zd, &outBuff, &inBuff) );
if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
if (outBuff.pos != CNBufferSize) goto _output_error; /* must regenerate whole input */
DISPLAYLEVEL(3, "OK \n");
DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should fail): ", testNb++); DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should fail): ", testNb++);
{ size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize); { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);