LZ4_compress_HC_continue_destSize() now compatible with optimal parser

levels 11+
This commit is contained in:
Yann Collet 2017-11-03 02:01:20 -07:00
parent 3b222d2d96
commit e2eca62046
3 changed files with 13 additions and 12 deletions

View File

@ -642,8 +642,11 @@ _dest_overflow:
static int LZ4HC_getSearchNum(int compressionLevel) static int LZ4HC_getSearchNum(int compressionLevel)
{ {
assert(compressionLevel >= 1);
assert(compressionLevel <= LZ4HC_CLEVEL_MAX);
switch (compressionLevel) { switch (compressionLevel) {
default: return 0; /* unused */ default: return 1 << (compressionLevel-1);
case 10: return 1 << 12;
case 11: return 512; case 11: return 512;
case 12: return 1<<13; case 12: return 1<<13;
} }
@ -709,8 +712,7 @@ int LZ4_compress_HC(const char* src, char* dst, int srcSize, int dstCapacity, in
} }
/* LZ4_compress_HC_destSize() : /* LZ4_compress_HC_destSize() :
* currently, only compatible with Hash Chain implementation, * only compatible with Hash Chain match finder */
* hence limit compression level to LZ4HC_CLEVEL_OPT_MIN-1*/
int LZ4_compress_HC_destSize(void* LZ4HC_Data, const char* source, char* dest, int* sourceSizePtr, int targetDestSize, int cLevel) int LZ4_compress_HC_destSize(void* LZ4HC_Data, const char* source, char* dest, int* sourceSizePtr, int targetDestSize, int cLevel)
{ {
LZ4HC_CCtx_internal* const ctx = &((LZ4_streamHC_t*)LZ4HC_Data)->internal_donotuse; LZ4HC_CCtx_internal* const ctx = &((LZ4_streamHC_t*)LZ4HC_Data)->internal_donotuse;
@ -744,6 +746,7 @@ void LZ4_resetStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel)
void LZ4_setCompressionLevel(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel) void LZ4_setCompressionLevel(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel)
{ {
/* note : 1-10 / 11-12 separation might no longer be necessary since optimal parser uses hash chain too */
int const currentCLevel = LZ4_streamHCPtr->internal_donotuse.compressionLevel; int const currentCLevel = LZ4_streamHCPtr->internal_donotuse.compressionLevel;
int const minCLevel = currentCLevel < LZ4HC_CLEVEL_OPT_MIN ? 1 : LZ4HC_CLEVEL_OPT_MIN; int const minCLevel = currentCLevel < LZ4HC_CLEVEL_OPT_MIN ? 1 : LZ4HC_CLEVEL_OPT_MIN;
int const maxCLevel = currentCLevel < LZ4HC_CLEVEL_OPT_MIN ? LZ4HC_CLEVEL_OPT_MIN-1 : LZ4HC_CLEVEL_MAX; int const maxCLevel = currentCLevel < LZ4HC_CLEVEL_OPT_MIN ? LZ4HC_CLEVEL_OPT_MIN-1 : LZ4HC_CLEVEL_MAX;
@ -824,8 +827,6 @@ int LZ4_compress_HC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* src,
int LZ4_compress_HC_continue_destSize (LZ4_streamHC_t* LZ4_streamHCPtr, const char* src, char* dst, int* srcSizePtr, int targetDestSize) int LZ4_compress_HC_continue_destSize (LZ4_streamHC_t* LZ4_streamHCPtr, const char* src, char* dst, int* srcSizePtr, int targetDestSize)
{ {
LZ4HC_CCtx_internal* const ctxPtr = &LZ4_streamHCPtr->internal_donotuse;
if (ctxPtr->compressionLevel >= LZ4HC_CLEVEL_OPT_MIN) LZ4HC_init(ctxPtr, (const BYTE*)src); /* not compatible with btopt implementation */
return LZ4_compressHC_continue_generic(LZ4_streamHCPtr, src, dst, srcSizePtr, targetDestSize, limitedDestSize); return LZ4_compressHC_continue_generic(LZ4_streamHCPtr, src, dst, srcSizePtr, targetDestSize, limitedDestSize);
} }

View File

@ -266,8 +266,8 @@ int LZ4_compress_HC_continue_destSize(LZ4_streamHC_t* LZ4_streamHCPtr,
int* srcSizePtr, int targetDstSize); int* srcSizePtr, int targetDstSize);
/*! LZ4_setCompressionLevel() : v1.8.0 (experimental) /*! LZ4_setCompressionLevel() : v1.8.0 (experimental)
* It's possible to change compression level after LZ4_resetStreamHC(), between 2 invocations of LZ4_compress_HC_continue*(), * It's possible to change compression level between 2 invocations of LZ4_compress_HC_continue*(),
* but that requires to stay in the same mode (aka 1-10 or 11-12). * but it requires to stay in the same mode (aka 1-10 or 11-12).
* This function ensures this condition. * This function ensures this condition.
*/ */
void LZ4_setCompressionLevel(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel); void LZ4_setCompressionLevel(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel);

View File

@ -160,7 +160,7 @@ static int LZ4HC_compress_optimal (
DEBUGLOG(7, "rPos:%3i => price:%3i (litlen=%i) -- initial setup", DEBUGLOG(7, "rPos:%3i => price:%3i (litlen=%i) -- initial setup",
rPos, cost, opt[rPos].litlen); rPos, cost, opt[rPos].litlen);
} } } }
/* set prices using matches found for rPos = 0 */ /* set prices using initial match */
{ int mlen = MINMATCH; { int mlen = MINMATCH;
int const matchML = firstMatch.len; /* necessarily < sufficient_len < LZ4_OPT_NUM */ int const matchML = firstMatch.len; /* necessarily < sufficient_len < LZ4_OPT_NUM */
int const offset = firstMatch.off; int const offset = firstMatch.off;
@ -215,7 +215,7 @@ static int LZ4HC_compress_optimal (
goto encode; goto encode;
} }
/* before first match : set price with literals at beginning */ /* before match : set price with literals at beginning */
{ int const baseLitlen = opt[cur].litlen; { int const baseLitlen = opt[cur].litlen;
int litlen; int litlen;
for (litlen = 1; litlen < MINMATCH; litlen++) { for (litlen = 1; litlen < MINMATCH; litlen++) {
@ -230,10 +230,10 @@ static int LZ4HC_compress_optimal (
pos, price, opt[pos].litlen); pos, price, opt[pos].litlen);
} } } } } }
/* set prices using matches at position = cur */ /* set prices using match at position = cur */
{ int const matchML = newMatch.len; { int const matchML = newMatch.len;
int ml = MINMATCH; int ml = MINMATCH;
assert(cur + newMatch.len < LZ4_OPT_NUM); assert(cur + newMatch.len < LZ4_OPT_NUM);
for ( ; ml <= matchML ; ml++) { for ( ; ml <= matchML ; ml++) {
int const pos = cur + ml; int const pos = cur + ml;
@ -255,7 +255,7 @@ static int LZ4HC_compress_optimal (
DEBUGLOG(7, "rPos:%3i => price:%3i (matchlen=%i)", DEBUGLOG(7, "rPos:%3i => price:%3i (matchlen=%i)",
pos, price, ml); pos, price, ml);
assert(pos < LZ4_OPT_NUM); assert(pos < LZ4_OPT_NUM);
if ( (ml == matchML) /* last post of last match */ if ( (ml == matchML) /* last pos of last match */
&& (last_match_pos < pos) ) && (last_match_pos < pos) )
last_match_pos = pos; last_match_pos = pos;
opt[pos].mlen = ml; opt[pos].mlen = ml;