2016-12-06 14:20:59 +00:00
|
|
|
/*
|
|
|
|
lz4opt.h - Optimal Mode of LZ4
|
|
|
|
Copyright (C) 2015-2016, Przemyslaw Skibinski <inikep@gmail.com>
|
|
|
|
Note : this file is intended to be included within lz4hc.c
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LZ4_LOG_PARSER(fmt, ...) //printf(fmt, __VA_ARGS__)
|
|
|
|
#define LZ4_LOG_PRICE(fmt, ...) //printf(fmt, __VA_ARGS__)
|
|
|
|
#define LZ4_LOG_ENCODE(fmt, ...) //printf(fmt, __VA_ARGS__)
|
|
|
|
|
2016-12-08 14:50:45 +00:00
|
|
|
#define LZ4_OPT_NUM (1<<12)
|
|
|
|
|
2016-12-06 14:20:59 +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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
2016-12-07 11:49:38 +00:00
|
|
|
FORCE_INLINE size_t LZ4HC_GetLiteralsPrice(size_t litlen)
|
2016-12-06 14:20:59 +00:00
|
|
|
{
|
|
|
|
size_t price = 8*litlen;
|
2016-12-08 14:50:45 +00:00
|
|
|
if (litlen >= (int)RUN_MASK) price+=8*(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-06 18:42:47 +00:00
|
|
|
FORCE_INLINE size_t LZ4HC_get_price(size_t litlen, size_t mlen)
|
2016-12-06 14:20:59 +00:00
|
|
|
{
|
|
|
|
size_t price = 16 + 8; /* 16-bit offset + token */
|
|
|
|
|
|
|
|
price += 8*litlen;
|
2016-12-08 14:50:45 +00:00
|
|
|
if (litlen >= (int)RUN_MASK) price+=8*(1+(litlen-RUN_MASK)/255);
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2016-12-07 11:49:38 +00:00
|
|
|
mlen -= MINMATCH;
|
2016-12-08 14:50:45 +00:00
|
|
|
if (mlen >= (int)ML_MASK) price+=8*(1+(mlen-ML_MASK)/255);
|
2016-12-06 14:20:59 +00:00
|
|
|
|
|
|
|
return price;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-09 11:37:17 +00:00
|
|
|
/*-*************************************
|
|
|
|
* Binary Tree search
|
|
|
|
***************************************/
|
|
|
|
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-09 14:20:32 +00:00
|
|
|
if (ip + MINMATCH > iHighLimit) return 1;
|
2016-12-06 14:20:59 +00:00
|
|
|
|
|
|
|
/* First Match */
|
|
|
|
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);
|
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) {
|
|
|
|
matches[mnum].off = (int)(ip - match);
|
|
|
|
matches[mnum].len = (int)matchLength;
|
|
|
|
mnum++;
|
|
|
|
}
|
2016-12-07 12:07:39 +00:00
|
|
|
if (best_mlen > LZ4_OPT_NUM) break;
|
|
|
|
}
|
|
|
|
|
2016-12-09 11:37:17 +00:00
|
|
|
if (ip+matchLength >= iHighLimit) /* equal : no way to know if inf or sup */
|
|
|
|
break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt the tree */
|
|
|
|
|
|
|
|
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-06 14:20:59 +00:00
|
|
|
if (*ptr0 == (U16)-1) break;
|
|
|
|
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-06 14:20:59 +00:00
|
|
|
if (*ptr1 == (U16)-1) break;
|
|
|
|
delta1 = *ptr1;
|
|
|
|
delta0 += delta1;
|
|
|
|
matchIndex -= delta1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr0 = (U16)-1;
|
|
|
|
*ptr1 = (U16)-1;
|
2016-12-09 14:20:32 +00:00
|
|
|
if (matchNum) *matchNum = mnum;
|
|
|
|
// if (best_mlen > 8) return best_mlen-8;
|
|
|
|
if (!matchNum) return 8;
|
|
|
|
return 1;
|
2016-12-06 14:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-09 14:20:32 +00:00
|
|
|
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);
|
|
|
|
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 11:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Tree updater, providing best match */
|
|
|
|
FORCE_INLINE int LZ4HC_BinTree_GetAllMatches (
|
|
|
|
LZ4HC_CCtx_internal* ctx,
|
|
|
|
const BYTE* const ip, const BYTE* const iHighLimit,
|
|
|
|
size_t best_mlen, LZ4HC_match_t* matches)
|
|
|
|
{
|
2016-12-09 14:20:32 +00:00
|
|
|
int mnum = 0;
|
2016-12-09 11:37:17 +00:00
|
|
|
if (ip < ctx->base + ctx->nextToUpdate) return 0; /* skipped area */
|
2016-12-09 14:20:32 +00:00
|
|
|
LZ4HC_updateBinTree(ctx, ip, iHighLimit);
|
|
|
|
best_mlen = LZ4HC_BinTree_InsertAndGetAllMatches(ctx, ip, iHighLimit, best_mlen, matches, &mnum);
|
|
|
|
ctx->nextToUpdate = (U32)(ip - ctx->base) + best_mlen;
|
2016-12-09 11:37:17 +00:00
|
|
|
return mnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-07 12:07:39 +00:00
|
|
|
#define SET_PRICE(pos, mlen, offset, litlen, price) \
|
|
|
|
{ \
|
|
|
|
while (last_pos < pos) { opt[last_pos+1].price = 1<<30; last_pos++; } \
|
|
|
|
opt[pos].mlen = (int)mlen; \
|
|
|
|
opt[pos].off = (int)offset; \
|
|
|
|
opt[pos].litlen = (int)litlen; \
|
|
|
|
opt[pos].price = (int)price; \
|
|
|
|
LZ4_LOG_PARSER("%d: SET price[%d/%d]=%d litlen=%d len=%d off=%d\n", (int)(inr-source), pos, last_pos, opt[pos].price, opt[pos].litlen, opt[pos].mlen, opt[pos].off); \
|
|
|
|
}
|
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,
|
2016-12-06 18:11:53 +00:00
|
|
|
const size_t sufficient_len
|
2016-12-06 14:20:59 +00:00
|
|
|
)
|
|
|
|
{
|
2016-12-07 12:07:39 +00:00
|
|
|
LZ4HC_optimal_t opt[LZ4_OPT_NUM + 1];
|
|
|
|
LZ4HC_match_t matches[LZ4_OPT_NUM + 1];
|
|
|
|
const BYTE *inr;
|
|
|
|
size_t res, cur, cur2;
|
|
|
|
size_t i, llen, litlen, mlen, best_mlen, price, offset, best_off, match_num, last_pos;
|
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 */
|
|
|
|
ctx->end += inputSize;
|
|
|
|
ip++;
|
|
|
|
|
|
|
|
/* Main Loop */
|
2016-12-07 11:17:24 +00:00
|
|
|
while (ip < mflimit) {
|
2016-12-06 14:20:59 +00:00
|
|
|
memset(opt, 0, sizeof(LZ4HC_optimal_t));
|
|
|
|
last_pos = 0;
|
|
|
|
llen = ip - anchor;
|
2016-12-07 11:49:38 +00:00
|
|
|
match_num = LZ4HC_BinTree_GetAllMatches(ctx, ip, matchlimit, MINMATCH-1, matches);
|
|
|
|
if (!match_num) { ip++; continue; }
|
|
|
|
LZ4_LOG_PARSER("%d: match_num=%d last_pos=%d\n", (int)(ip-source), match_num, last_pos);
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2016-12-07 11:49:38 +00:00
|
|
|
if ((size_t)matches[match_num-1].len > sufficient_len) {
|
2016-12-06 14:20:59 +00:00
|
|
|
best_mlen = matches[match_num-1].len;
|
|
|
|
best_off = matches[match_num-1].off;
|
|
|
|
cur = 0;
|
|
|
|
last_pos = 1;
|
|
|
|
goto encode;
|
2016-12-07 11:49:38 +00:00
|
|
|
}
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2016-12-07 11:49:38 +00:00
|
|
|
/* set prices using matches at position = 0 */
|
|
|
|
for (i = 0; i < match_num; i++) {
|
|
|
|
mlen = (i>0) ? (size_t)matches[i-1].len+1 : MINMATCH;
|
2016-12-06 14:20:59 +00:00
|
|
|
best_mlen = (matches[i].len < LZ4_OPT_NUM) ? matches[i].len : LZ4_OPT_NUM;
|
|
|
|
LZ4_LOG_PARSER("%d: start Found mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(ip-source), matches[i].len, matches[i].off, best_mlen, last_pos);
|
2016-12-07 11:17:24 +00:00
|
|
|
while (mlen <= best_mlen) {
|
2016-12-06 14:20:59 +00:00
|
|
|
litlen = 0;
|
2016-12-08 14:50:45 +00:00
|
|
|
price = LZ4HC_get_price(llen + litlen, mlen) - LZ4HC_GetLiteralsPrice(llen);
|
|
|
|
SET_PRICE(mlen, mlen, matches[i].off, litlen, price);
|
2016-12-06 14:20:59 +00:00
|
|
|
mlen++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_pos < MINMATCH) { ip++; continue; }
|
|
|
|
|
2016-12-07 11:49:38 +00:00
|
|
|
/* check further positions */
|
2016-12-07 11:59:05 +00:00
|
|
|
opt[0].mlen = opt[1].mlen = 1;
|
2016-12-07 11:17:24 +00:00
|
|
|
for (cur = 1; cur <= last_pos; cur++) {
|
|
|
|
inr = ip + cur;
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2016-12-07 11:17:24 +00:00
|
|
|
if (opt[cur-1].mlen == 1) {
|
2016-12-06 14:20:59 +00:00
|
|
|
litlen = opt[cur-1].litlen + 1;
|
2016-12-07 11:17:24 +00:00
|
|
|
if (cur != litlen) {
|
2016-12-07 11:49:38 +00:00
|
|
|
price = opt[cur - litlen].price + LZ4HC_GetLiteralsPrice(litlen);
|
2016-12-06 14:20:59 +00:00
|
|
|
LZ4_LOG_PRICE("%d: TRY1 opt[%d].price=%d price=%d cur=%d litlen=%d\n", (int)(inr-source), cur - litlen, opt[cur - litlen].price, price, cur, litlen);
|
2016-12-07 11:17:24 +00:00
|
|
|
} else {
|
2016-12-08 14:50:45 +00:00
|
|
|
price = LZ4HC_GetLiteralsPrice(llen + litlen) - LZ4HC_GetLiteralsPrice(llen);
|
2016-12-06 14:20:59 +00:00
|
|
|
LZ4_LOG_PRICE("%d: TRY2 price=%d cur=%d litlen=%d llen=%d\n", (int)(inr-source), price, cur, litlen, llen);
|
|
|
|
}
|
2016-12-07 11:17:24 +00:00
|
|
|
} else {
|
2016-12-06 14:20:59 +00:00
|
|
|
litlen = 1;
|
2016-12-07 11:49:38 +00:00
|
|
|
price = opt[cur - 1].price + LZ4HC_GetLiteralsPrice(litlen);
|
|
|
|
LZ4_LOG_PRICE("%d: TRY3 price=%d cur=%d litlen=%d litonly=%d\n", (int)(inr-source), price, cur, litlen, LZ4HC_GetLiteralsPrice(litlen));
|
2016-12-07 11:17:24 +00:00
|
|
|
}
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2016-12-07 11:17:24 +00:00
|
|
|
mlen = 1;
|
|
|
|
best_mlen = 0;
|
|
|
|
LZ4_LOG_PARSER("%d: TRY price=%d opt[%d].price=%d\n", (int)(inr-source), price, cur, opt[cur].price);
|
2016-12-08 14:50:45 +00:00
|
|
|
if (cur > last_pos || price < (size_t)opt[cur].price)
|
2016-12-06 14:20:59 +00:00
|
|
|
SET_PRICE(cur, mlen, best_mlen, litlen, price);
|
|
|
|
|
2016-12-07 11:17:24 +00:00
|
|
|
if (cur == last_pos) break;
|
2016-12-06 14:20:59 +00:00
|
|
|
LZ4_LOG_PARSER("%d: CURRENT price[%d/%d]=%d off=%d mlen=%d litlen=%d\n", (int)(inr-source), cur, last_pos, opt[cur].price, opt[cur].off, opt[cur].mlen, opt[cur].litlen);
|
|
|
|
|
2016-12-07 11:49:38 +00:00
|
|
|
match_num = LZ4HC_BinTree_GetAllMatches(ctx, inr, matchlimit, MINMATCH-1, matches);
|
2016-12-07 10:44:17 +00:00
|
|
|
LZ4_LOG_PARSER("%d: LZ4HC_BinTree_GetAllMatches match_num=%d\n", (int)(inr-source), match_num);
|
2016-12-06 14:20:59 +00:00
|
|
|
|
2016-12-07 11:17:24 +00:00
|
|
|
if (match_num > 0 && (size_t)matches[match_num-1].len > sufficient_len) {
|
2016-12-06 14:20:59 +00:00
|
|
|
best_mlen = matches[match_num-1].len;
|
|
|
|
best_off = matches[match_num-1].off;
|
|
|
|
last_pos = cur + 1;
|
|
|
|
goto encode;
|
|
|
|
}
|
|
|
|
|
2016-12-07 11:59:05 +00:00
|
|
|
/* set prices using matches at position = cur */
|
2016-12-07 11:17:24 +00:00
|
|
|
for (i = 0; i < match_num; i++) {
|
2016-12-07 11:49:38 +00:00
|
|
|
mlen = (i>0) ? (size_t)matches[i-1].len+1 : MINMATCH;
|
|
|
|
cur2 = cur;
|
2016-12-06 14:20:59 +00:00
|
|
|
best_mlen = (cur2 + matches[i].len < LZ4_OPT_NUM) ? (size_t)matches[i].len : LZ4_OPT_NUM - cur2;
|
|
|
|
LZ4_LOG_PARSER("%d: Found1 cur=%d cur2=%d mlen=%d off=%d best_mlen=%d last_pos=%d\n", (int)(inr-source), cur, cur2, matches[i].len, matches[i].off, best_mlen, last_pos);
|
|
|
|
|
2016-12-07 11:17:24 +00:00
|
|
|
while (mlen <= best_mlen) {
|
|
|
|
if (opt[cur2].mlen == 1) {
|
2016-12-06 14:20:59 +00:00
|
|
|
litlen = opt[cur2].litlen;
|
|
|
|
|
|
|
|
if (cur2 != litlen)
|
2016-12-07 11:49:38 +00:00
|
|
|
price = opt[cur2 - litlen].price + LZ4HC_get_price(litlen, mlen);
|
2016-12-06 14:20:59 +00:00
|
|
|
else
|
2016-12-08 14:50:45 +00:00
|
|
|
price = LZ4HC_get_price(llen + litlen, mlen) - LZ4HC_GetLiteralsPrice(llen);
|
2016-12-07 11:17:24 +00:00
|
|
|
} else {
|
2016-12-06 14:20:59 +00:00
|
|
|
litlen = 0;
|
2016-12-07 11:49:38 +00:00
|
|
|
price = opt[cur2].price + LZ4HC_get_price(litlen, mlen);
|
2016-12-06 14:20:59 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 11:49:38 +00:00
|
|
|
LZ4_LOG_PARSER("%d: Found2 mlen=%d best_mlen=%d off=%d price=%d litlen=%d price[%d]=%d\n", (int)(inr-source), mlen, best_mlen, matches[i].off, price, litlen, cur - litlen, opt[cur - litlen].price);
|
2016-12-08 14:50:45 +00:00
|
|
|
if (cur2 + mlen > last_pos || price < (size_t)opt[cur2 + mlen].price) { // || (((int)price == opt[cur2 + mlen].price) && (opt[cur2 + mlen-1].mlen == 1))) {
|
2016-12-06 14:20:59 +00:00
|
|
|
SET_PRICE(cur2 + mlen, mlen, matches[i].off, litlen, price);
|
|
|
|
}
|
|
|
|
mlen++;
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 11:59:05 +00:00
|
|
|
} /* for (cur = 1; cur <= last_pos; cur++) */
|
2016-12-06 14:20:59 +00:00
|
|
|
|
|
|
|
best_mlen = opt[last_pos].mlen;
|
|
|
|
best_off = opt[last_pos].off;
|
|
|
|
cur = last_pos - best_mlen;
|
|
|
|
|
2016-12-07 11:59:05 +00:00
|
|
|
encode: /* cur, last_pos, best_mlen, best_off have to be set */
|
2016-12-07 11:17:24 +00:00
|
|
|
for (i = 1; i <= last_pos; i++) {
|
2016-12-06 14:20:59 +00:00
|
|
|
LZ4_LOG_PARSER("%d: price[%d/%d]=%d off=%d mlen=%d litlen=%d\n", (int)(ip-source+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
LZ4_LOG_PARSER("%d: cur=%d/%d best_mlen=%d best_off=%d\n", (int)(ip-source+cur), cur, last_pos, best_mlen, best_off);
|
|
|
|
|
|
|
|
opt[0].mlen = 1;
|
2016-12-07 11:17:24 +00:00
|
|
|
while (1) {
|
2016-12-06 14:20:59 +00:00
|
|
|
mlen = opt[cur].mlen;
|
|
|
|
offset = opt[cur].off;
|
|
|
|
opt[cur].mlen = (int)best_mlen;
|
|
|
|
opt[cur].off = (int)best_off;
|
|
|
|
best_mlen = mlen;
|
|
|
|
best_off = offset;
|
|
|
|
if (mlen > cur) break;
|
|
|
|
cur -= mlen;
|
|
|
|
}
|
|
|
|
|
2016-12-07 11:17:24 +00:00
|
|
|
for (i = 0; i <= last_pos;) {
|
2016-12-06 14:20:59 +00:00
|
|
|
LZ4_LOG_PARSER("%d: price2[%d/%d]=%d off=%d mlen=%d litlen=%d\n", (int)(ip-source+i), i, last_pos, opt[i].price, opt[i].off, opt[i].mlen, opt[i].litlen);
|
|
|
|
i += opt[i].mlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = 0;
|
2016-12-07 11:17:24 +00:00
|
|
|
while (cur < last_pos) {
|
2016-12-06 14:20:59 +00:00
|
|
|
LZ4_LOG_PARSER("%d: price3[%d/%d]=%d off=%d mlen=%d litlen=%d\n", (int)(ip-source+cur), cur, last_pos, opt[cur].price, opt[cur].off, opt[cur].mlen, opt[cur].litlen);
|
|
|
|
mlen = opt[cur].mlen;
|
|
|
|
if (mlen == 1) { ip++; cur++; continue; }
|
|
|
|
offset = opt[cur].off;
|
|
|
|
cur += mlen;
|
|
|
|
|
|
|
|
LZ4_LOG_ENCODE("%d: ENCODE literals=%d off=%d mlen=%d ", (int)(ip-source), (int)(ip-anchor), (int)(offset), mlen);
|
|
|
|
res = LZ4HC_encodeSequence(&ip, &op, &anchor, (int)mlen, ip - offset, limit, oend);
|
|
|
|
LZ4_LOG_ENCODE("out=%d\n", (int)((char*)op - dest));
|
|
|
|
|
|
|
|
if (res) return 0;
|
|
|
|
|
|
|
|
LZ4_LOG_PARSER("%d: offset=%d\n", (int)(ip-source), offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encode Last Literals */
|
|
|
|
{
|
|
|
|
int lastRun = (int)(iend - anchor);
|
|
|
|
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);
|
|
|
|
LZ4_LOG_ENCODE("%d: ENCODE_LAST literals=%d out=%d\n", (int)(ip-source), (int)(iend-anchor), (int)((char*)op -dest));
|
|
|
|
memcpy(op, anchor, iend - anchor);
|
|
|
|
op += iend-anchor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End */
|
|
|
|
return (int) ((char*)op-dest);
|
|
|
|
}
|