2016-12-06 14:20:59 +00:00
|
|
|
/*
|
|
|
|
lz4opt.h - Optimal Mode of LZ4
|
2017-03-16 22:10:38 +00:00
|
|
|
Copyright (C) 2015-2017, Przemyslaw Skibinski <inikep@gmail.com>
|
2016-12-06 14:20:59 +00:00
|
|
|
Note : this file is intended to be included within lz4hc.c
|
2016-12-22 17:02:09 +00:00
|
|
|
|
2016-12-06 14:20:59 +00:00
|
|
|
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the following disclaimer
|
|
|
|
in the documentation and/or other materials provided with the
|
|
|
|
distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
You can contact the author at :
|
|
|
|
- LZ4 source repository : https://github.com/lz4/lz4
|
|
|
|
- LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
|
|
|
|
*/
|
|
|
|
|
2016-12-08 14:50:45 +00:00
|
|
|
#define LZ4_OPT_NUM (1<<12)
|
|
|
|
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2017-03-17 22:51:08 +00:00
|
|
|
typedef struct {
|
2016-12-07 10:44:17 +00:00
|
|
|
int off;
|
|
|
|
int len;
|
2016-12-06 14:20:59 +00:00
|
|
|
} LZ4HC_match_t;
|
|
|
|
|
2017-03-17 22:51:08 +00:00
|
|
|
typedef struct {
|
2016-12-07 10:44:17 +00:00
|
|
|
int price;
|
|
|
|
int off;
|
|
|
|
int mlen;
|
|
|
|
int litlen;
|
2016-12-06 14:20:59 +00:00
|
|
|
} LZ4HC_optimal_t;
|
|
|
|
|
|
|
|
|
2017-05-02 05:32:21 +00:00
|
|
|
/* price in bytes */
|
2017-08-24 14:14:20 +00:00
|
|
|
LZ4_FORCE_INLINE size_t LZ4HC_literalsPrice(size_t litlen)
|
2016-12-06 14:20:59 +00:00
|
|
|
{
|
2017-03-17 22:51:08 +00:00
|
|
|
size_t price = litlen;
|
2017-05-02 05:32:21 +00:00
|
|
|
if (litlen >= (size_t)RUN_MASK)
|
|
|
|
price += 1 + (litlen-RUN_MASK)/255;
|
2016-12-06 14:20:59 +00:00
|
|
|
return price;
|
|
|
|
}
|
|
|
|
|
2016-12-07 10:44:17 +00:00
|
|
|
|
2016-12-22 17:02:09 +00:00
|
|
|
/* requires mlen >= MINMATCH */
|
2017-08-24 14:14:20 +00:00
|
|
|
LZ4_FORCE_INLINE size_t LZ4HC_sequencePrice(size_t litlen, size_t mlen)
|
2016-12-06 14:20:59 +00:00
|
|
|
{
|
2017-03-17 22:51:08 +00:00
|
|
|
size_t price = 2 + 1; /* 16-bit offset + token */
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2016-12-22 17:02:09 +00:00
|
|
|
price += LZ4HC_literalsPrice(litlen);
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2017-03-17 23:53:35 +00:00
|
|
|
if (mlen >= (size_t)(ML_MASK+MINMATCH))
|
2017-05-02 05:32:21 +00:00
|
|
|
price+= 1 + (mlen-(ML_MASK+MINMATCH))/255;
|
2016-12-06 14:20:59 +00:00
|
|
|
|
|
|
|
return price;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-09 11:37:17 +00:00
|
|
|
/*-*************************************
|
|
|
|
* Binary Tree search
|
|
|
|
***************************************/
|
2017-08-24 14:14:20 +00:00
|
|
|
LZ4_FORCE_INLINE int LZ4HC_BinTree_InsertAndGetAllMatches (
|
2016-12-06 14:20:59 +00:00
|
|
|
LZ4HC_CCtx_internal* ctx,
|
|
|
|
const BYTE* const ip,
|
|
|
|
const BYTE* const iHighLimit,
|
|
|
|
size_t best_mlen,
|
2016-12-09 14:20:32 +00:00
|
|
|
LZ4HC_match_t* matches,
|
|
|
|
int* matchNum)
|
2016-12-06 14:20:59 +00:00
|
|
|
{
|
|
|
|
U16* const chainTable = ctx->chainTable;
|
|
|
|
U32* const HashTable = ctx->hashTable;
|
|
|
|
const BYTE* const base = ctx->base;
|
|
|
|
const U32 dictLimit = ctx->dictLimit;
|
|
|
|
const U32 current = (U32)(ip - base);
|
|
|
|
const U32 lowLimit = (ctx->lowLimit + MAX_DISTANCE > current) ? ctx->lowLimit : current - (MAX_DISTANCE - 1);
|
|
|
|
const BYTE* const dictBase = ctx->dictBase;
|
|
|
|
const BYTE* match;
|
|
|
|
int nbAttempts = ctx->searchNum;
|
|
|
|
int mnum = 0;
|
2016-12-07 11:59:05 +00:00
|
|
|
U16 *ptr0, *ptr1, delta0, delta1;
|
|
|
|
U32 matchIndex;
|
2016-12-09 11:37:17 +00:00
|
|
|
size_t matchLength = 0;
|
2016-12-06 14:20:59 +00:00
|
|
|
U32* HashPos;
|
2016-12-22 17:02:09 +00:00
|
|
|
|
2016-12-09 14:20:32 +00:00
|
|
|
if (ip + MINMATCH > iHighLimit) return 1;
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2016-12-09 16:16:35 +00:00
|
|
|
/* HC4 match finder */
|
2016-12-06 14:20:59 +00:00
|
|
|
HashPos = &HashTable[LZ4HC_hashPtr(ip)];
|
|
|
|
matchIndex = *HashPos;
|
|
|
|
*HashPos = current;
|
|
|
|
|
2016-12-06 18:11:53 +00:00
|
|
|
ptr0 = &DELTANEXTMAXD(current*2+1);
|
|
|
|
ptr1 = &DELTANEXTMAXD(current*2);
|
2016-12-07 12:07:39 +00:00
|
|
|
delta0 = delta1 = (U16)(current - matchIndex);
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2016-12-07 11:17:24 +00:00
|
|
|
while ((matchIndex < current) && (matchIndex>=lowLimit) && (nbAttempts)) {
|
2016-12-06 14:20:59 +00:00
|
|
|
nbAttempts--;
|
2016-12-07 12:07:39 +00:00
|
|
|
if (matchIndex >= dictLimit) {
|
2016-12-06 14:20:59 +00:00
|
|
|
match = base + matchIndex;
|
2016-12-09 11:37:17 +00:00
|
|
|
matchLength = LZ4_count(ip, match, iHighLimit);
|
2016-12-07 12:07:39 +00:00
|
|
|
} else {
|
2016-12-07 11:49:38 +00:00
|
|
|
const BYTE* vLimit = ip + (dictLimit - matchIndex);
|
2016-12-07 11:59:05 +00:00
|
|
|
match = dictBase + matchIndex;
|
2016-12-07 11:49:38 +00:00
|
|
|
if (vLimit > iHighLimit) vLimit = iHighLimit;
|
2016-12-09 11:37:17 +00:00
|
|
|
matchLength = LZ4_count(ip, match, vLimit);
|
|
|
|
if ((ip+matchLength == vLimit) && (vLimit < iHighLimit))
|
|
|
|
matchLength += LZ4_count(ip+matchLength, base+dictLimit, iHighLimit);
|
2017-06-26 18:29:05 +00:00
|
|
|
if (matchIndex+matchLength >= dictLimit)
|
|
|
|
match = base + matchIndex; /* to prepare for next usage of match[matchLength] */
|
2016-12-06 14:20:59 +00:00
|
|
|
}
|
2016-12-07 12:07:39 +00:00
|
|
|
|
2016-12-09 14:20:32 +00:00
|
|
|
if (matchLength > best_mlen) {
|
2016-12-09 11:37:17 +00:00
|
|
|
best_mlen = matchLength;
|
2016-12-09 14:20:32 +00:00
|
|
|
if (matches) {
|
2016-12-27 14:31:35 +00:00
|
|
|
if (matchIndex >= dictLimit)
|
|
|
|
matches[mnum].off = (int)(ip - match);
|
|
|
|
else
|
|
|
|
matches[mnum].off = (int)(ip - (base + matchIndex)); /* virtual matchpos */
|
2016-12-09 14:20:32 +00:00
|
|
|
matches[mnum].len = (int)matchLength;
|
|
|
|
mnum++;
|
|
|
|
}
|
2016-12-28 14:37:12 +00:00
|
|
|
if (best_mlen > LZ4_OPT_NUM) break;
|
2016-12-07 12:07:39 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 11:37:17 +00:00
|
|
|
if (ip+matchLength >= iHighLimit) /* equal : no way to know if inf or sup */
|
2016-12-28 14:37:12 +00:00
|
|
|
break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt the tree */
|
2016-12-09 11:37:17 +00:00
|
|
|
|
2017-06-26 18:29:05 +00:00
|
|
|
DEBUGLOG(6, "ip :%016llX", (U64)ip);
|
|
|
|
DEBUGLOG(6, "match:%016llX", (U64)match);
|
2016-12-09 11:37:17 +00:00
|
|
|
if (*(ip+matchLength) < *(match+matchLength)) {
|
2016-12-06 14:20:59 +00:00
|
|
|
*ptr0 = delta0;
|
2016-12-06 18:11:53 +00:00
|
|
|
ptr0 = &DELTANEXTMAXD(matchIndex*2);
|
2016-12-28 14:37:12 +00:00
|
|
|
if (*ptr0 == (U16)-1) break;
|
2016-12-06 14:20:59 +00:00
|
|
|
delta0 = *ptr0;
|
|
|
|
delta1 += delta0;
|
|
|
|
matchIndex -= delta0;
|
2016-12-07 11:17:24 +00:00
|
|
|
} else {
|
2016-12-06 14:20:59 +00:00
|
|
|
*ptr1 = delta1;
|
2016-12-06 18:11:53 +00:00
|
|
|
ptr1 = &DELTANEXTMAXD(matchIndex*2+1);
|
2016-12-28 14:37:12 +00:00
|
|
|
if (*ptr1 == (U16)-1) break;
|
2016-12-06 14:20:59 +00:00
|
|
|
delta1 = *ptr1;
|
|
|
|
delta0 += delta1;
|
|
|
|
matchIndex -= delta1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr0 = (U16)-1;
|
|
|
|
*ptr1 = (U16)-1;
|
2016-12-09 14:20:32 +00:00
|
|
|
if (matchNum) *matchNum = mnum;
|
2016-12-09 16:16:35 +00:00
|
|
|
/* if (best_mlen > 8) return best_mlen-8; */
|
2016-12-09 15:09:38 +00:00
|
|
|
if (!matchNum) return 1;
|
2016-12-22 17:02:09 +00:00
|
|
|
return 1;
|
2016-12-06 14:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-24 14:14:20 +00:00
|
|
|
LZ4_FORCE_INLINE void LZ4HC_updateBinTree(LZ4HC_CCtx_internal* ctx, const BYTE* const ip, const BYTE* const iHighLimit)
|
2016-12-09 11:37:17 +00:00
|
|
|
{
|
|
|
|
const BYTE* const base = ctx->base;
|
|
|
|
const U32 target = (U32)(ip - base);
|
2016-12-28 14:38:59 +00:00
|
|
|
U32 idx = ctx->nextToUpdate;
|
2016-12-09 14:20:32 +00:00
|
|
|
while(idx < target)
|
|
|
|
idx += LZ4HC_BinTree_InsertAndGetAllMatches(ctx, base+idx, iHighLimit, 8, NULL, NULL);
|
2016-12-09 16:16:35 +00:00
|
|
|
}
|
2016-12-09 11:37:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** Tree updater, providing best match */
|
2017-08-24 14:14:20 +00:00
|
|
|
LZ4_FORCE_INLINE int LZ4HC_BinTree_GetAllMatches (
|
2016-12-09 11:37:17 +00:00
|
|
|
LZ4HC_CCtx_internal* ctx,
|
|
|
|
const BYTE* const ip, const BYTE* const iHighLimit,
|
2016-12-09 16:16:35 +00:00
|
|
|
size_t best_mlen, LZ4HC_match_t* matches, const int fullUpdate)
|
2016-12-09 11:37:17 +00:00
|
|
|
{
|
2016-12-09 14:20:32 +00:00
|
|
|
int mnum = 0;
|
2016-12-28 14:38:59 +00:00
|
|
|
if (ip < ctx->base + ctx->nextToUpdate) return 0; /* skipped area */
|
2016-12-09 16:16:35 +00:00
|
|
|
if (fullUpdate) LZ4HC_updateBinTree(ctx, ip, iHighLimit);
|
2016-12-09 14:20:32 +00:00
|
|
|
best_mlen = LZ4HC_BinTree_InsertAndGetAllMatches(ctx, ip, iHighLimit, best_mlen, matches, &mnum);
|
2016-12-28 14:38:59 +00:00
|
|
|
ctx->nextToUpdate = (U32)(ip - ctx->base + best_mlen);
|
2016-12-09 11:37:17 +00:00
|
|
|
return mnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-06 14:20:59 +00:00
|
|
|
static int LZ4HC_compress_optimal (
|
|
|
|
LZ4HC_CCtx_internal* ctx,
|
2016-12-06 18:42:47 +00:00
|
|
|
const char* const source,
|
2016-12-06 14:20:59 +00:00
|
|
|
char* dest,
|
|
|
|
int inputSize,
|
|
|
|
int maxOutputSize,
|
|
|
|
limitedOutput_directive limit,
|
2017-03-17 23:53:35 +00:00
|
|
|
size_t sufficient_len,
|
2016-12-09 16:16:35 +00:00
|
|
|
const int fullUpdate
|
2016-12-06 14:20:59 +00:00
|
|
|
)
|
|
|
|
{
|
2017-03-18 00:42:47 +00:00
|
|
|
LZ4HC_optimal_t opt[LZ4_OPT_NUM + 1]; /* this uses a bit too much stack memory to my taste ... */
|
2016-12-07 12:07:39 +00:00
|
|
|
LZ4HC_match_t matches[LZ4_OPT_NUM + 1];
|
2016-12-06 14:20:59 +00:00
|
|
|
|
|
|
|
const BYTE* ip = (const BYTE*) source;
|
|
|
|
const BYTE* anchor = ip;
|
|
|
|
const BYTE* const iend = ip + inputSize;
|
|
|
|
const BYTE* const mflimit = iend - MFLIMIT;
|
|
|
|
const BYTE* const matchlimit = (iend - LASTLITERALS);
|
|
|
|
BYTE* op = (BYTE*) dest;
|
|
|
|
BYTE* const oend = op + maxOutputSize;
|
|
|
|
|
|
|
|
/* init */
|
2017-06-26 18:29:05 +00:00
|
|
|
DEBUGLOG(5, "LZ4HC_compress_optimal");
|
2017-03-17 23:53:35 +00:00
|
|
|
if (sufficient_len >= LZ4_OPT_NUM) sufficient_len = LZ4_OPT_NUM-1;
|
2016-12-06 14:20:59 +00:00
|
|
|
ctx->end += inputSize;
|
|
|
|
ip++;
|
|
|
|
|
|
|
|
/* Main Loop */
|
2016-12-07 11:17:24 +00:00
|
|
|
while (ip < mflimit) {
|
2017-03-20 09:57:41 +00:00
|
|
|
size_t const llen = ip - anchor;
|
2017-10-19 23:43:36 +00:00
|
|
|
size_t last_match_pos = 0;
|
2017-10-20 18:24:56 +00:00
|
|
|
size_t cur, best_mlen, best_off;
|
2017-03-20 09:57:41 +00:00
|
|
|
|
2017-10-20 18:24:56 +00:00
|
|
|
size_t const nb_matches_initial = LZ4HC_BinTree_GetAllMatches(ctx, ip, matchlimit, MINMATCH-1, matches, fullUpdate);
|
|
|
|
if (!nb_matches_initial) { ip++; continue; }
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2017-10-20 18:24:56 +00:00
|
|
|
if ((size_t)matches[nb_matches_initial-1].len > sufficient_len) {
|
2017-03-20 09:57:41 +00:00
|
|
|
/* good enough solution : immediate encoding */
|
2017-10-20 18:24:56 +00:00
|
|
|
int const firstML = (int)matches[nb_matches_initial-1].len;
|
|
|
|
const BYTE* const matchPos = ip - matches[nb_matches_initial-1].off;
|
2017-10-19 23:39:40 +00:00
|
|
|
if ( LZ4HC_encodeSequence(&ip, &op, &anchor, (int)firstML, matchPos, limit, oend) ) /* updates ip, op and anchor */
|
|
|
|
return 0; /* error */
|
|
|
|
continue;
|
2016-12-07 11:49:38 +00:00
|
|
|
}
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2017-10-20 18:00:10 +00:00
|
|
|
/* set prices for first positions (literals) */
|
|
|
|
{ size_t rPos;
|
|
|
|
for (rPos = 0 ; rPos < MINMATCH ; rPos++) {
|
|
|
|
int const cost = (int)LZ4HC_literalsPrice(llen + rPos);
|
|
|
|
opt[rPos].mlen = 1;
|
|
|
|
opt[rPos].off = 0;
|
|
|
|
opt[rPos].litlen = (int)(llen + rPos);
|
|
|
|
opt[rPos].price = cost;
|
|
|
|
DEBUGLOG(7, "rPos:%3u => cost:%3i (litlen=%i)",
|
|
|
|
(U32)rPos, cost, opt[rPos].litlen);
|
|
|
|
} }
|
|
|
|
/* set prices using matches found for rPos = 0 */
|
2017-03-20 09:57:41 +00:00
|
|
|
{ size_t matchNb;
|
2017-10-20 18:24:56 +00:00
|
|
|
for (matchNb = 0; matchNb < nb_matches_initial; matchNb++) {
|
2017-10-20 19:05:00 +00:00
|
|
|
int mlen = (matchNb>0) ? matches[matchNb-1].len+1 : MINMATCH;
|
|
|
|
int const matchML = matches[matchNb].len; /* necessarily < sufficient_len < LZ4_OPT_NUM */
|
|
|
|
int const offset = matches[matchNb].off;
|
|
|
|
assert(matchML < LZ4_OPT_NUM);
|
|
|
|
for ( ; mlen <= matchML ; mlen++) {
|
2017-10-20 18:00:10 +00:00
|
|
|
size_t const cost = LZ4HC_sequencePrice(llen, mlen);
|
2017-10-20 19:05:00 +00:00
|
|
|
opt[mlen].mlen = mlen;
|
|
|
|
opt[mlen].off = offset;
|
|
|
|
opt[mlen].litlen = (int)llen;
|
|
|
|
opt[mlen].price = (int)cost;
|
2017-03-20 09:57:41 +00:00
|
|
|
} } }
|
2017-10-20 19:05:00 +00:00
|
|
|
last_match_pos = matches[nb_matches_initial-1].len;
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2016-12-07 11:49:38 +00:00
|
|
|
/* check further positions */
|
2017-10-19 23:43:36 +00:00
|
|
|
for (cur = 1; cur <= last_match_pos; cur++) {
|
2017-03-20 09:57:41 +00:00
|
|
|
const BYTE* const curPtr = ip + cur;
|
2017-10-20 18:24:56 +00:00
|
|
|
size_t nb_matches;
|
2017-03-20 09:57:41 +00:00
|
|
|
|
|
|
|
/* establish baseline price if cur is literal */
|
2017-10-20 20:32:45 +00:00
|
|
|
{ size_t price;
|
|
|
|
int litlen;
|
2017-03-20 09:57:41 +00:00
|
|
|
if (opt[cur-1].mlen == 1) {
|
|
|
|
/* no match at previous position */
|
|
|
|
litlen = opt[cur-1].litlen + 1;
|
2017-10-20 18:00:10 +00:00
|
|
|
price = opt[cur-1].price - LZ4HC_literalsPrice(litlen-1) + LZ4HC_literalsPrice(litlen);
|
2016-12-07 11:17:24 +00:00
|
|
|
} else {
|
2017-03-20 09:57:41 +00:00
|
|
|
litlen = 1;
|
|
|
|
price = opt[cur - 1].price + LZ4HC_literalsPrice(1);
|
2016-12-06 14:20:59 +00:00
|
|
|
}
|
2017-10-20 20:32:45 +00:00
|
|
|
if (price < (size_t)opt[cur].price) {
|
|
|
|
opt[cur].mlen = 1;
|
|
|
|
opt[cur].off = 0;
|
|
|
|
opt[cur].litlen = litlen;
|
|
|
|
opt[cur].price = (int)price;
|
|
|
|
}
|
2017-03-20 09:57:41 +00:00
|
|
|
}
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2017-10-19 23:43:36 +00:00
|
|
|
if (cur == last_match_pos || curPtr >= mflimit) break;
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2017-10-20 20:32:45 +00:00
|
|
|
//assert(cur+2 <= last_match_pos);
|
|
|
|
//assert(cur+3 <= last_match_pos);
|
|
|
|
|
2017-10-20 18:24:56 +00:00
|
|
|
nb_matches = LZ4HC_BinTree_GetAllMatches(ctx, curPtr, matchlimit, MINMATCH-1, matches, fullUpdate);
|
|
|
|
if ((nb_matches > 0) && (size_t)matches[nb_matches-1].len > sufficient_len) {
|
2017-03-18 01:07:53 +00:00
|
|
|
/* immediate encoding */
|
2017-10-20 18:24:56 +00:00
|
|
|
best_mlen = matches[nb_matches-1].len;
|
|
|
|
best_off = matches[nb_matches-1].off;
|
2017-10-19 23:43:36 +00:00
|
|
|
last_match_pos = cur + 1;
|
2016-12-06 14:20:59 +00:00
|
|
|
goto encode;
|
|
|
|
}
|
|
|
|
|
2016-12-07 11:59:05 +00:00
|
|
|
/* set prices using matches at position = cur */
|
2017-03-20 09:57:41 +00:00
|
|
|
{ size_t matchNb;
|
2017-10-20 18:24:56 +00:00
|
|
|
for (matchNb = 0; matchNb < nb_matches; matchNb++) {
|
2017-10-20 20:44:49 +00:00
|
|
|
int ml = (matchNb>0) ? matches[matchNb-1].len+1 : MINMATCH;
|
|
|
|
int const matchML = (cur + matches[matchNb].len < LZ4_OPT_NUM) ?
|
|
|
|
matches[matchNb].len : (int)(LZ4_OPT_NUM - cur);
|
2017-03-20 09:57:41 +00:00
|
|
|
|
2017-10-20 19:05:00 +00:00
|
|
|
for ( ; ml <= matchML ; ml++) {
|
2017-10-20 20:44:49 +00:00
|
|
|
int const pos = cur + ml;
|
|
|
|
int const offset = matches[matchNb].off;
|
|
|
|
size_t price;
|
|
|
|
int ll;
|
2017-03-20 09:57:41 +00:00
|
|
|
if (opt[cur].mlen == 1) {
|
|
|
|
ll = opt[cur].litlen;
|
2017-10-20 20:44:49 +00:00
|
|
|
price = ((cur > (size_t)ll) ? opt[cur - ll].price : 0) + LZ4HC_sequencePrice(ll, ml);
|
2017-03-20 09:57:41 +00:00
|
|
|
} else {
|
|
|
|
ll = 0;
|
|
|
|
price = opt[cur].price + LZ4HC_sequencePrice(0, ml);
|
|
|
|
}
|
|
|
|
|
2017-10-20 20:44:49 +00:00
|
|
|
if ((size_t)pos > last_match_pos || price < (size_t)opt[pos].price) {
|
|
|
|
while (last_match_pos < (size_t)pos) opt[++last_match_pos].price = 1<<30;
|
|
|
|
opt[pos].mlen = ml;
|
|
|
|
opt[pos].off = offset;
|
|
|
|
opt[pos].litlen = ll;
|
|
|
|
opt[pos].price = (int)price;
|
2017-03-20 09:57:41 +00:00
|
|
|
} } } }
|
2017-10-20 19:05:00 +00:00
|
|
|
} /* for (cur = 1; cur <= last_match_pos; cur++) */
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2017-10-19 23:43:36 +00:00
|
|
|
best_mlen = opt[last_match_pos].mlen;
|
|
|
|
best_off = opt[last_match_pos].off;
|
|
|
|
cur = last_match_pos - best_mlen;
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2017-10-19 23:43:36 +00:00
|
|
|
encode: /* cur, last_match_pos, best_mlen, best_off must be set */
|
2017-10-20 18:32:15 +00:00
|
|
|
assert(cur < LZ4_OPT_NUM);
|
|
|
|
assert(last_match_pos >= 1); /* == 1 when only one candidate */
|
2016-12-06 14:20:59 +00:00
|
|
|
opt[0].mlen = 1;
|
2017-10-20 18:32:15 +00:00
|
|
|
{ int candidate_pos = (int)cur;
|
|
|
|
int selected_matchLength = (int)best_mlen;
|
|
|
|
int selected_offset = (int)best_off;
|
|
|
|
while (1) { /* from end to beginning */
|
|
|
|
int const next_matchLength = opt[candidate_pos].mlen;
|
|
|
|
int const next_offset = opt[candidate_pos].off;
|
|
|
|
assert(next_matchLength > 0); /* note : can be 1, means literal */
|
|
|
|
opt[candidate_pos].mlen = selected_matchLength;
|
|
|
|
opt[candidate_pos].off = selected_offset;
|
|
|
|
selected_matchLength = next_matchLength;
|
|
|
|
selected_offset = next_offset;
|
|
|
|
if (next_matchLength > candidate_pos) break; /* last match elected, first match to encode */
|
|
|
|
candidate_pos -= next_matchLength;
|
|
|
|
} }
|
2016-12-22 17:02:09 +00:00
|
|
|
|
2017-10-20 19:05:00 +00:00
|
|
|
/* encode all recorded sequences in order */
|
|
|
|
{ size_t rPos = 0; /* relative position (to ip) */
|
|
|
|
while (rPos < last_match_pos) {
|
|
|
|
int const ml = opt[rPos].mlen;
|
|
|
|
int const offset = opt[rPos].off;
|
|
|
|
if (ml == 1) { ip++; rPos++; continue; } /* literal */
|
|
|
|
rPos += ml;
|
|
|
|
assert(ml >= MINMATCH);
|
|
|
|
assert((offset >= 1) && (offset <=65535));
|
|
|
|
if ( LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ip - offset, limit, oend) ) /* updates ip, op and anchor */
|
|
|
|
return 0; /* error */
|
|
|
|
} }
|
2017-03-20 09:57:41 +00:00
|
|
|
} /* while (ip < mflimit) */
|
2016-12-06 14:20:59 +00:00
|
|
|
|
|
|
|
/* Encode Last Literals */
|
2016-12-09 15:09:38 +00:00
|
|
|
{ int lastRun = (int)(iend - anchor);
|
2017-10-20 19:05:00 +00:00
|
|
|
if ( (limit)
|
|
|
|
&& (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize))
|
|
|
|
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);
|
2016-12-06 14:20:59 +00:00
|
|
|
memcpy(op, anchor, iend - anchor);
|
|
|
|
op += iend-anchor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End */
|
|
|
|
return (int) ((char*)op-dest);
|
|
|
|
}
|