36 #define LZ4_OPT_NUM (1<<12) 55 size_t price = litlen;
82 const BYTE*
const iHighLimit,
91 const U32 current = (
U32)(ip - base);
97 U16 *ptr0, *ptr1, delta0, delta1;
99 size_t matchLength = 0;
102 if (ip +
MINMATCH > iHighLimit)
return 1;
106 matchIndex = *HashPos;
111 delta0 = delta1 = (
U16)(current - matchIndex);
113 while ((matchIndex < current) && (matchIndex>=lowLimit) && (nbAttempts)) {
115 if (matchIndex >= dictLimit) {
116 match = base + matchIndex;
117 matchLength =
LZ4_count(ip, match, iHighLimit);
119 const BYTE* vLimit = ip + (dictLimit - matchIndex);
120 match = dictBase + matchIndex;
121 if (vLimit > iHighLimit) vLimit = iHighLimit;
122 matchLength =
LZ4_count(ip, match, vLimit);
123 if ((ip+matchLength == vLimit) && (vLimit < iHighLimit))
124 matchLength +=
LZ4_count(ip+matchLength, base+dictLimit, iHighLimit);
125 if (matchIndex+matchLength >= dictLimit)
126 match = base + matchIndex;
129 if (matchLength > best_mlen) {
130 best_mlen = matchLength;
132 if (matchIndex >= dictLimit)
133 matches[mnum].
off = (int)(ip - match);
135 matches[mnum].
off = (int)(ip - (base + matchIndex));
136 matches[mnum].
len = (int)matchLength;
142 if (ip+matchLength >= iHighLimit)
147 if (*(ip+matchLength) < *(match+matchLength)) {
150 if (*ptr0 == (
U16)-1)
break;
153 matchIndex -= delta0;
157 if (*ptr1 == (
U16)-1)
break;
160 matchIndex -= delta1;
166 if (matchNum) *matchNum = mnum;
168 if (!matchNum)
return 1;
186 const BYTE*
const ip,
const BYTE*
const iHighLimit,
198 #define SET_PRICE(pos, ml, offset, ll, cost) \ 200 while (last_pos < pos) { opt[last_pos+1].price = 1<<30; last_pos++; } \ 201 opt[pos].mlen = (int)ml; \ 202 opt[pos].off = (int)offset; \ 203 opt[pos].litlen = (int)ll; \ 204 opt[pos].price = (int)cost; \ 215 size_t sufficient_len,
222 const BYTE* ip = (
const BYTE*) source;
223 const BYTE* anchor = ip;
231 DEBUGLOG(5,
"LZ4HC_compress_optimal");
237 while (ip < mflimit) {
238 size_t const llen = ip - anchor;
240 size_t match_num, cur, best_mlen, best_off;
244 if (!match_num) { ip++;
continue; }
246 if ((
size_t)matches[match_num-1].
len > sufficient_len) {
248 best_mlen = matches[match_num-1].
len;
249 best_off = matches[match_num-1].
off;
257 for (matchNb = 0; matchNb < match_num; matchNb++) {
258 size_t mlen = (matchNb>0) ? (
size_t)matches[matchNb-1].
len+1 :
MINMATCH;
259 best_mlen = matches[matchNb].
len;
260 for ( ; mlen <= best_mlen ; mlen++) {
262 SET_PRICE(mlen, mlen, matches[matchNb].off, 0, cost);
265 if (last_pos <
MINMATCH) { ip++;
continue; }
269 for (cur = 1; cur <= last_pos; cur++) {
270 const BYTE*
const curPtr = ip + cur;
273 {
size_t price, litlen;
274 if (opt[cur-1].mlen == 1) {
276 litlen = opt[cur-1].
litlen + 1;
287 if (price < (
size_t)opt[cur].price)
291 if (cur == last_pos || curPtr >= mflimit)
break;
294 if ((match_num > 0) && (
size_t)matches[match_num-1].
len > sufficient_len) {
296 best_mlen = matches[match_num-1].
len;
297 best_off = matches[match_num-1].
off;
304 for (matchNb = 0; matchNb < match_num; matchNb++) {
305 size_t ml = (matchNb>0) ? (
size_t)matches[matchNb-1].
len+1 :
MINMATCH;
309 for ( ; ml <= best_mlen ; ml++) {
311 if (opt[cur].mlen == 1) {
322 if (cur + ml > last_pos || price < (
size_t)opt[cur + ml].price) {
323 SET_PRICE(cur + ml, ml, matches[matchNb].off, ll, price);
327 best_mlen = opt[last_pos].
mlen;
328 best_off = opt[last_pos].
off;
329 cur = last_pos - best_mlen;
334 size_t const ml = opt[cur].
mlen;
336 opt[cur].
mlen = (int)best_mlen;
337 opt[cur].
off = (int)best_off;
346 while (cur < last_pos) {
347 int const ml = opt[cur].
mlen;
349 if (ml == 1) { ip++; cur++;
continue; }
356 {
int lastRun = (int)(iend - anchor);
357 if ((limit) && (((
char*)op - dest) + lastRun + 1 + ((lastRun+255-
RUN_MASK)/255) > (
U32)maxOutputSize))
return 0;
360 memcpy(op, anchor, iend - anchor);
365 return (
int) ((
char*)op-dest);
static int LZ4HC_compress_optimal(LZ4HC_CCtx_internal *ctx, const char *const source, char *dest, int inputSize, int maxOutputSize, limitedOutput_directive limit, size_t sufficient_len, const int fullUpdate)
static U32 LZ4HC_hashPtr(const void *ptr)
FORCE_INLINE size_t LZ4HC_literalsPrice(size_t litlen)
FORCE_INLINE int LZ4HC_encodeSequence(const BYTE **ip, BYTE **op, const BYTE **anchor, int matchLength, const BYTE *const match, limitedOutput_directive limit, BYTE *oend)
#define SET_PRICE(pos, ml, offset, ll, cost)
const unsigned char * dictBase
unsigned int hashTable[LZ4HC_HASHTABLESIZE]
FORCE_INLINE void LZ4HC_updateBinTree(LZ4HC_CCtx_internal *ctx, const BYTE *const ip, const BYTE *const iHighLimit)
FORCE_INLINE size_t LZ4HC_sequencePrice(size_t litlen, size_t mlen)
unsigned int nextToUpdate
LZ4LIB_API const char char int inputSize
const unsigned char * base
LZ4LIB_API char int int maxOutputSize
GLsizei GLsizei GLchar * source
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)
unsigned short chainTable[LZ4HC_MAXD]
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)
const unsigned char * end
static unsigned LZ4_count(const BYTE *pIn, const BYTE *pMatch, const BYTE *pInLimit)