36 #define LZ4_OPT_NUM (1<<12)
57 size_t price = 8*litlen;
58 if (litlen >= (
size_t)RUN_MASK) price+=8*(1+(litlen-RUN_MASK)/255);
66 size_t price = 16 + 8;
71 if (mlen >= (
size_t)ML_MASK) price+=8*(1+(mlen-ML_MASK)/255);
83 const BYTE*
const iHighLimit,
92 const U32 current = (U32)(ip - base);
93 const U32 lowLimit = (ctx->
lowLimit + MAX_DISTANCE > current) ? ctx->
lowLimit : current - (MAX_DISTANCE - 1);
98 U16 *ptr0, *ptr1, delta0, delta1;
100 size_t matchLength = 0;
103 if (ip + MINMATCH > iHighLimit)
return 1;
106 HashPos = &HashTable[LZ4HC_hashPtr(ip)];
107 matchIndex = *HashPos;
110 ptr0 = &DELTANEXTMAXD(current*2+1);
111 ptr1 = &DELTANEXTMAXD(current*2);
112 delta0 = delta1 = (U16)(current - matchIndex);
114 while ((matchIndex < current) && (matchIndex>=lowLimit) && (nbAttempts)) {
116 if (matchIndex >= dictLimit) {
117 match = base + matchIndex;
118 matchLength = LZ4_count(ip, match, iHighLimit);
120 const BYTE* vLimit = ip + (dictLimit - matchIndex);
121 match = dictBase + matchIndex;
122 if (vLimit > iHighLimit) vLimit = iHighLimit;
123 matchLength = LZ4_count(ip, match, vLimit);
124 if ((ip+matchLength == vLimit) && (vLimit < iHighLimit))
125 matchLength += LZ4_count(ip+matchLength, base+dictLimit, iHighLimit);
128 if (matchLength > best_mlen) {
129 best_mlen = matchLength;
131 if (matchIndex >= dictLimit)
132 matches[mnum].
off = (
int)(ip - match);
134 matches[mnum].
off = (
int)(ip - (base + matchIndex));
135 matches[mnum].
len = (
int)matchLength;
141 if (ip+matchLength >= iHighLimit)
144 if (*(ip+matchLength) < *(match+matchLength)) {
146 ptr0 = &DELTANEXTMAXD(matchIndex*2);
147 if (*ptr0 == (U16)-1)
break;
150 matchIndex -= delta0;
153 ptr1 = &DELTANEXTMAXD(matchIndex*2+1);
154 if (*ptr1 == (U16)-1)
break;
157 matchIndex -= delta1;
163 if (matchNum) *matchNum = mnum;
165 if (!matchNum)
return 1;
173 const U32 target = (U32)(ip - base);
183 const BYTE*
const ip,
const BYTE*
const iHighLimit,
184 size_t best_mlen,
LZ4HC_match_t* matches,
const int fullUpdate)
195 #define SET_PRICE(pos, mlen, offset, litlen, price) \
197 while (last_pos < pos) { opt[last_pos+1].price = 1<<30; last_pos++; } \
198 opt[pos].mlen = (int)mlen; \
199 opt[pos].off = (int)offset; \
200 opt[pos].litlen = (int)litlen; \
201 opt[pos].price = (int)price; \
211 limitedOutput_directive limit,
212 const size_t sufficient_len,
218 const BYTE *inr = NULL;
219 size_t res, cur, cur2;
220 size_t i, llen, litlen, mlen, best_mlen, price, offset, best_off, match_num, last_pos;
222 const BYTE* ip = (
const BYTE*) source;
223 const BYTE* anchor = ip;
225 const BYTE*
const mflimit = iend - MFLIMIT;
226 const BYTE*
const matchlimit = (iend - LASTLITERALS);
235 while (ip < mflimit) {
240 if (!match_num) { ip++;
continue; }
242 if ((
size_t)matches[match_num-1].len > sufficient_len) {
243 best_mlen = matches[match_num-1].
len;
244 best_off = matches[match_num-1].
off;
251 for (i = 0; i < match_num; i++) {
252 mlen = (i>0) ? (
size_t)matches[i-1].
len+1 : MINMATCH;
254 while (mlen <= best_mlen) {
257 SET_PRICE(mlen, mlen, matches[i].off, litlen, price);
262 if (last_pos < MINMATCH) { ip++;
continue; }
266 for (cur = 1; cur <= last_pos; cur++) {
269 if (opt[cur-1].mlen == 1) {
270 litlen = opt[cur-1].
litlen + 1;
283 if (cur > last_pos || price < (
size_t)opt[cur].price)
284 SET_PRICE(cur, mlen, best_mlen, litlen, price);
286 if (cur == last_pos || inr >= mflimit)
break;
289 if (match_num > 0 && (
size_t)matches[match_num-1].len > sufficient_len) {
290 best_mlen = matches[match_num-1].
len;
291 best_off = matches[match_num-1].
off;
297 for (i = 0; i < match_num; i++) {
298 mlen = (i>0) ? (
size_t)matches[i-1].
len+1 : MINMATCH;
302 while (mlen <= best_mlen) {
303 if (opt[cur2].mlen == 1) {
304 litlen = opt[cur2].
litlen;
315 if (cur2 + mlen > last_pos || price < (
size_t)opt[cur2 + mlen].price) {
316 SET_PRICE(cur2 + mlen, mlen, matches[i].off, litlen, price);
323 best_mlen = opt[last_pos].
mlen;
324 best_off = opt[last_pos].
off;
325 cur = last_pos - best_mlen;
330 mlen = opt[cur].
mlen;
331 offset = opt[cur].
off;
332 opt[cur].
mlen = (
int)best_mlen;
333 opt[cur].
off = (
int)best_off;
336 if (mlen > cur)
break;
341 while (cur < last_pos) {
342 mlen = opt[cur].
mlen;
343 if (mlen == 1) { ip++; cur++;
continue; }
344 offset = opt[cur].
off;
347 res = LZ4HC_encodeSequence(&ip, &op, &anchor, (
int)mlen, ip - offset, limit, oend);
353 {
int lastRun = (
int)(iend - anchor);
354 if ((limit) && (((
char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize))
return 0;
355 if (lastRun>=(
int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK;
for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (
BYTE) lastRun; }
356 else *op++ = (
BYTE)(lastRun<<ML_BITS);
357 memcpy(op, anchor, iend - anchor);
362 return (
int) ((
char*)op-dest);
const unsigned char * base
Definition: lz4hc.h:173
char int int maxOutputSize
Definition: lz4.h:441
FORCE_INLINE size_t LZ4HC_literalsPrice(size_t litlen)
Definition: lz4opt.h:55
int litlen
Definition: lz4opt.h:50
int mlen
Definition: lz4opt.h:49
static int LZ4HC_compress_optimal(LZ4HC_CCtx_internal *ctx, const char *const source, char *dest, int inputSize, int maxOutputSize, limitedOutput_directive limit, const size_t sufficient_len, const int fullUpdate)
Definition: lz4opt.h:205
#define LZ4_OPT_NUM
Definition: lz4opt.h:36
const unsigned char * dictBase
Definition: lz4hc.h:174
unsigned int lowLimit
Definition: lz4hc.h:177
FORCE_INLINE void LZ4HC_updateBinTree(LZ4HC_CCtx_internal *ctx, const BYTE *const ip, const BYTE *const iHighLimit)
Definition: lz4opt.h:170
FORCE_INLINE size_t LZ4HC_sequencePrice(size_t litlen, size_t mlen)
Definition: lz4opt.h:64
unsigned int hashTable[LZ4HC_HASHTABLESIZE]
Definition: lz4hc.h:170
int off
Definition: lz4opt.h:48
#define SET_PRICE(pos, mlen, offset, litlen, price)
Definition: lz4opt.h:195
unsigned short chainTable[LZ4HC_MAXD]
Definition: lz4hc.h:171
Definition: rfxImageMessage.h:25
int len
Definition: lz4opt.h:42
const char * source
Definition: lz4.h:442
int price
Definition: lz4opt.h:47
const unsigned char * end
Definition: lz4hc.h:172
const char char int inputSize
Definition: lz4.h:442
unsigned int nextToUpdate
Definition: lz4hc.h:178
unsigned int dictLimit
Definition: lz4hc.h:176
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, const int fullUpdate)
Tree updater, providing best match.
Definition: lz4opt.h:181
int off
Definition: lz4opt.h:41
FORCE_INLINE int LZ4HC_BinTree_InsertAndGetAllMatches(LZ4HC_CCtx_internal *ctx, const BYTE *const ip, const BYTE *const iHighLimit, size_t best_mlen, LZ4HC_match_t *matches, int *matchNum)
Definition: lz4opt.h:80
unsigned int searchNum
Definition: lz4hc.h:179
char * dest
Definition: lz4.h:440