lz4frame : control lz4 context creation success

This commit is contained in:
Yann Collet 2017-04-08 18:27:31 -07:00
parent e2827775ee
commit e169edac05

View File

@ -316,11 +316,11 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity, const void* srcBu
BYTE* dstPtr = dstStart; BYTE* dstPtr = dstStart;
BYTE* const dstEnd = dstStart + dstCapacity; BYTE* const dstEnd = dstStart + dstCapacity;
memset(&cctxI, 0, sizeof(cctxI)); /* works because no allocation */ memset(&cctxI, 0, sizeof(cctxI));
memset(&options, 0, sizeof(options)); memset(&options, 0, sizeof(options));
cctxI.version = LZ4F_VERSION; cctxI.version = LZ4F_VERSION;
cctxI.maxBufferSize = 5 MB; /* mess with real buffer size to prevent allocation; works because autoflush==1 & stableSrc==1 */ cctxI.maxBufferSize = 5 MB; /* mess with real buffer size to prevent dynamic allocation; works because autoflush==1 & stableSrc==1 */
if (preferencesPtr!=NULL) if (preferencesPtr!=NULL)
prefs = *preferencesPtr; prefs = *preferencesPtr;
@ -332,7 +332,7 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity, const void* srcBu
if (prefs.compressionLevel < LZ4HC_CLEVEL_MIN) { if (prefs.compressionLevel < LZ4HC_CLEVEL_MIN) {
cctxI.lz4CtxPtr = &lz4ctx; cctxI.lz4CtxPtr = &lz4ctx;
cctxI.lz4CtxLevel = 1; cctxI.lz4CtxLevel = 1;
} } /* otherwise : will be created within LZ4F_compressBegin */
prefs.frameInfo.blockSizeID = LZ4F_optimalBSID(prefs.frameInfo.blockSizeID, srcSize); prefs.frameInfo.blockSizeID = LZ4F_optimalBSID(prefs.frameInfo.blockSizeID, srcSize);
prefs.autoFlush = 1; prefs.autoFlush = 1;
@ -341,7 +341,7 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity, const void* srcBu
options.stableSrc = 1; options.stableSrc = 1;
if (dstCapacity < LZ4F_compressFrameBound(srcSize, &prefs)) if (dstCapacity < LZ4F_compressFrameBound(srcSize, &prefs)) /* condition to guarantee success */
return err0r(LZ4F_ERROR_dstMaxSize_tooSmall); return err0r(LZ4F_ERROR_dstMaxSize_tooSmall);
{ size_t const headerSize = LZ4F_compressBegin(&cctxI, dstBuffer, dstCapacity, &prefs); /* write header */ { size_t const headerSize = LZ4F_compressBegin(&cctxI, dstBuffer, dstCapacity, &prefs); /* write header */
@ -356,7 +356,7 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity, const void* srcBu
if (LZ4F_isError(tailSize)) return tailSize; if (LZ4F_isError(tailSize)) return tailSize;
dstPtr += tailSize; } dstPtr += tailSize; }
if (prefs.compressionLevel >= LZ4HC_CLEVEL_MIN) /* no allocation done with lz4 fast */ if (prefs.compressionLevel >= LZ4HC_CLEVEL_MIN) /* Ctx allocation only for lz4hc */
FREEMEM(cctxI.lz4CtxPtr); FREEMEM(cctxI.lz4CtxPtr);
return (dstPtr - dstStart); return (dstPtr - dstStart);
@ -423,7 +423,7 @@ size_t LZ4F_compressBegin(LZ4F_cctx* cctxPtr, void* dstBuffer, size_t dstCapacit
if (preferencesPtr == NULL) preferencesPtr = &prefNull; if (preferencesPtr == NULL) preferencesPtr = &prefNull;
cctxPtr->prefs = *preferencesPtr; cctxPtr->prefs = *preferencesPtr;
/* ctx Management */ /* Ctx Management */
{ U32 const tableID = (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) ? 1 : 2; /* 0:nothing ; 1:LZ4 table ; 2:HC tables */ { U32 const tableID = (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) ? 1 : 2; /* 0:nothing ; 1:LZ4 table ; 2:HC tables */
if (cctxPtr->lz4CtxLevel < tableID) { if (cctxPtr->lz4CtxLevel < tableID) {
FREEMEM(cctxPtr->lz4CtxPtr); FREEMEM(cctxPtr->lz4CtxPtr);
@ -431,6 +431,7 @@ size_t LZ4F_compressBegin(LZ4F_cctx* cctxPtr, void* dstBuffer, size_t dstCapacit
cctxPtr->lz4CtxPtr = (void*)LZ4_createStream(); cctxPtr->lz4CtxPtr = (void*)LZ4_createStream();
else else
cctxPtr->lz4CtxPtr = (void*)LZ4_createStreamHC(); cctxPtr->lz4CtxPtr = (void*)LZ4_createStreamHC();
if (cctxPtr->lz4CtxPtr == NULL) return err0r(LZ4F_ERROR_allocation_failed);
cctxPtr->lz4CtxLevel = tableID; cctxPtr->lz4CtxLevel = tableID;
} }
} }