summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorYann Collet <cyan@fb.com>2017-12-22 11:47:59 (GMT)
committerYann Collet <cyan@fb.com>2017-12-22 11:47:59 (GMT)
commit7d2f30c7d1519a3a4a199e398e13d9baa21bf84e (patch)
tree68e816073fbde339262fdee0ebcb35222357ab07 /lib
parentfdde4311fb47332d81f2fa84a06e5605e6b68600 (diff)
downloadlz4-7d2f30c7d1519a3a4a199e398e13d9baa21bf84e.zip
lz4-7d2f30c7d1519a3a4a199e398e13d9baa21bf84e.tar.gz
lz4-7d2f30c7d1519a3a4a199e398e13d9baa21bf84e.tar.bz2
lz4opt supports _destSize
no longer limited to level 9
Diffstat (limited to 'lib')
-rw-r--r--lib/lz4hc.c14
-rw-r--r--lib/lz4opt.h61
2 files changed, 49 insertions, 26 deletions
diff --git a/lib/lz4hc.c b/lib/lz4hc.c
index 388eb40..e6d496c 100644
--- a/lib/lz4hc.c
+++ b/lib/lz4hc.c
@@ -425,10 +425,7 @@ static int LZ4HC_compress_hashChain (
/* init */
*srcSizePtr = 0;
- if (limit == limitedDestSize && maxOutputSize < 1) return 0; /* Impossible to store anything */
- if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported input size, too large (or negative) */
-
- if (limit == limitedDestSize) oend -= LASTLITERALS; /* Hack for support limitations LZ4 decompressor */
+ if (limit == limitedDestSize) oend -= LASTLITERALS; /* Hack for support LZ4 format restriction */
if (inputSize < LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */
/* Main Loop */
@@ -641,11 +638,12 @@ static int LZ4HC_compress_generic (
{ lz4opt,8192, LZ4_OPT_NUM }, /* 12==LZ4HC_CLEVEL_MAX */
};
+ if (limit == limitedDestSize && dstCapacity < 1) return 0; /* Impossible to store anything */
+ if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported input size (too large or negative) */
+
ctx->end += *srcSizePtr;
if (cLevel < 1) cLevel = LZ4HC_CLEVEL_DEFAULT; /* note : convention is different from lz4frame, maybe something to review */
cLevel = MIN(LZ4HC_CLEVEL_MAX, cLevel);
- if (limit == limitedDestSize)
- cLevel = MIN(LZ4HC_CLEVEL_OPT_MIN-1, cLevel); /* no limitedDestSize variant for lz4opt */
assert(cLevel >= 0);
assert(cLevel <= LZ4HC_CLEVEL_MAX);
{ cParams_t const cParam = clTable[cLevel];
@@ -655,8 +653,8 @@ static int LZ4HC_compress_generic (
cParam.nbSearches, limit);
assert(cParam.strat == lz4opt);
return LZ4HC_compress_optimal(ctx,
- src, dst, *srcSizePtr, dstCapacity, limit,
- cParam.nbSearches, cParam.targetLength,
+ src, dst, srcSizePtr, dstCapacity,
+ cParam.nbSearches, cParam.targetLength, limit,
cLevel == LZ4HC_CLEVEL_MAX); /* ultra mode */
}
}
diff --git a/lib/lz4opt.h b/lib/lz4opt.h
index 9917851..58068bf 100644
--- a/lib/lz4opt.h
+++ b/lib/lz4opt.h
@@ -97,11 +97,11 @@ static int LZ4HC_compress_optimal (
LZ4HC_CCtx_internal* ctx,
const char* const source,
char* dst,
- int inputSize,
+ int* srcSizePtr,
int dstCapacity,
- limitedOutput_directive limit,
int const nbSearches,
size_t sufficient_len,
+ limitedOutput_directive limit,
int const fullUpdate
)
{
@@ -110,14 +110,17 @@ static int LZ4HC_compress_optimal (
const BYTE* ip = (const BYTE*) source;
const BYTE* anchor = ip;
- const BYTE* const iend = ip + inputSize;
+ const BYTE* const iend = ip + *srcSizePtr;
const BYTE* const mflimit = iend - MFLIMIT;
const BYTE* const matchlimit = iend - LASTLITERALS;
BYTE* op = (BYTE*) dst;
- BYTE* const oend = op + dstCapacity;
+ BYTE* opSaved = (BYTE*) dst;
+ BYTE* oend = op + dstCapacity;
/* init */
DEBUGLOG(5, "LZ4HC_compress_optimal");
+ *srcSizePtr = 0;
+ if (limit == limitedDestSize) oend -= LASTLITERALS; /* Hack for support LZ4 format restriction */
if (sufficient_len >= LZ4_OPT_NUM) sufficient_len = LZ4_OPT_NUM-1;
/* Main Loop */
@@ -134,8 +137,9 @@ static int LZ4HC_compress_optimal (
/* good enough solution : immediate encoding */
int const firstML = firstMatch.len;
const BYTE* const matchPos = ip - firstMatch.off;
+ opSaved = op;
if ( LZ4HC_encodeSequence(&ip, &op, &anchor, firstML, matchPos, limit, oend) ) /* updates ip, op and anchor */
- return 0; /* error */
+ goto _dest_overflow;
continue;
}
@@ -304,26 +308,47 @@ encode: /* cur, last_match_pos, best_mlen, best_off must be set */
rPos += ml;
assert(ml >= MINMATCH);
assert((offset >= 1) && (offset <= MAX_DISTANCE));
+ opSaved = op;
if ( LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ip - offset, limit, oend) ) /* updates ip, op and anchor */
- return 0; /* error */
+ goto _dest_overflow;
} }
} /* while (ip < mflimit) */
+_last_literals:
/* Encode Last Literals */
- { int lastRun = (int)(iend - anchor);
- if ( (limit)
- && (((char*)op - dst) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)dstCapacity))
- return 0; /* Check output limit */
- if (lastRun >= (int)RUN_MASK) {
- *op++=(RUN_MASK<<ML_BITS);
- lastRun-=RUN_MASK;
- for (; lastRun > 254 ; lastRun-=255) *op++ = 255;
- *op++ = (BYTE) lastRun;
- } else *op++ = (BYTE)(lastRun<<ML_BITS);
- memcpy(op, anchor, iend - anchor);
- op += iend-anchor;
+ { size_t lastRunSize = (size_t)(iend - anchor); /* literals */
+ size_t litLength = (lastRunSize + 255 - RUN_MASK) / 255;
+ size_t const totalSize = 1 + litLength + lastRunSize;
+ if (limit == limitedDestSize) oend += LASTLITERALS; /* restore correct value */
+ if (limit && (op + totalSize > oend)) {
+ if (limit == limitedOutput) return 0; /* Check output limit */
+ /* adapt lastRunSize to fill 'dst' */
+ lastRunSize = (size_t)(oend - op) - 1;
+ litLength = (lastRunSize + 255 - RUN_MASK) / 255;
+ lastRunSize -= litLength;
+ }
+ ip = anchor + lastRunSize;
+
+ if (lastRunSize >= RUN_MASK) {
+ size_t accumulator = lastRunSize - RUN_MASK;
+ *op++ = (RUN_MASK << ML_BITS);
+ for(; accumulator >= 255 ; accumulator -= 255) *op++ = 255;
+ *op++ = (BYTE) accumulator;
+ } else {
+ *op++ = (BYTE)(lastRunSize << ML_BITS);
+ }
+ memcpy(op, anchor, lastRunSize);
+ op += lastRunSize;
}
/* End */
+ *srcSizePtr = (int) (((const char*)ip) - source);
return (int) ((char*)op-dst);
+
+_dest_overflow:
+ if (limit == limitedDestSize) {
+ op = opSaved; /* restore correct out pointer */
+ goto _last_literals;
+ }
+ return 0;
}