@@ -112,11 +112,16 @@ bool LZXCodec<T>::forward(SliceArray<byte>& input, SliceArray<byte>& output, int
112112 int32 h = hash (&src[srcIdx]);
113113 int ref = _hashes[h];
114114 _hashes[h] = srcIdx;
115+
116+ if (ref <= minRef) {
117+ srcIdx++;
118+ continue ;
119+ }
120+
115121 int bestLen = 0 ;
116122
117- if (ref > minRef) {
118- const int maxMatch = min (srcEnd - srcIdx, MAX_MATCH);
119- bestLen = findMatch (src, srcIdx, ref, maxMatch);
123+ if (memcmp (&src[srcIdx], &src[ref], 4 ) == 0 ) {
124+ bestLen = 4 + findMatch (src, srcIdx + 4 , ref + 4 , min (srcEnd - srcIdx, MAX_MATCH));
120125 }
121126
122127 // No good match ?
@@ -132,8 +137,7 @@ bool LZXCodec<T>::forward(SliceArray<byte>& input, SliceArray<byte>& output, int
132137 int bestLen2 = 0 ;
133138
134139 if (ref2 > minRef + 1 ) {
135- const int maxMatch = min (srcEnd - srcIdx - 1 , MAX_MATCH);
136- bestLen2 = findMatch (src, srcIdx + 1 , ref2, maxMatch);
140+ bestLen2 = findMatch (src, srcIdx + 1 , ref2, min (srcEnd - srcIdx - 1 , MAX_MATCH));
137141 }
138142
139143 // Select best match
@@ -307,7 +311,7 @@ bool LZXCodec<T>::inverse(SliceArray<byte>& input, SliceArray<byte>& output, int
307311 repd = dist;
308312
309313 // Sanity check
310- if ((dstIdx < dist) || (dist > maxDist) || (mEnd > dstEnd + 16 )) {
314+ if ((dstIdx < dist) || (dist > maxDist) || (mEnd > dstEnd + 16 )) {
311315 res = false ;
312316 goto exit;
313317 }
@@ -359,7 +363,7 @@ bool LZPCodec::forward(SliceArray<byte>& input, SliceArray<byte>& output, int co
359363
360364 byte* dst = &output._array [output._index ];
361365 byte* src = &input._array [input._index ];
362- const int srcEnd = count - 8 ;
366+ const int srcEnd = count;
363367 const int dstEnd = output._length - 4 ;
364368
365369 if (_hashSize == 0 ) {
@@ -378,23 +382,15 @@ bool LZPCodec::forward(SliceArray<byte>& input, SliceArray<byte>& output, int co
378382 int dstIdx = 4 ;
379383 int minRef = 4 ;
380384
381- while ((srcIdx < srcEnd) && (dstIdx < dstEnd)) {
385+ while ((srcIdx < srcEnd - MIN_MATCH ) && (dstIdx < dstEnd)) {
382386 const uint32 h = (HASH_SEED * ctx) >> HASH_SHIFT;
383387 const int32 ref = _hashes[h];
384388 _hashes[h] = srcIdx;
385389 int bestLen = 0 ;
386390
387391 // Find a match
388- if ((ref > minRef) && (memcmp (&src[ref], &src[srcIdx], 4 ) == 0 )) {
389- const int maxMatch = srcEnd - srcIdx;
390- bestLen = 4 ;
391-
392- while ((bestLen < maxMatch) && (memcmp (&src[ref + bestLen], &src[srcIdx + bestLen], 4 ) == 0 ))
393- bestLen += 4 ;
394-
395- while ((bestLen < maxMatch) && (src[ref + bestLen] == src[srcIdx + bestLen]))
396- bestLen++;
397- }
392+ if ((ref > minRef) && (memcmp (&src[ref+MIN_MATCH-4 ], &src[srcIdx+MIN_MATCH-4 ], 4 ) == 0 ))
393+ bestLen = findMatch (src, srcIdx, ref, srcEnd - srcIdx);
398394
399395 // No good match ?
400396 if (bestLen < MIN_MATCH) {
@@ -430,7 +426,7 @@ bool LZPCodec::forward(SliceArray<byte>& input, SliceArray<byte>& output, int co
430426 dst[dstIdx++] = byte (bestLen);
431427 }
432428
433- while ((srcIdx < srcEnd + 8 ) && (dstIdx < dstEnd)) {
429+ while ((srcIdx < srcEnd) && (dstIdx < dstEnd)) {
434430 const uint32 h = (HASH_SEED * ctx) >> HASH_SHIFT;
435431 const int ref = _hashes[h];
436432 _hashes[h] = srcIdx;
@@ -511,7 +507,7 @@ bool LZPCodec::inverse(SliceArray<byte>& input, SliceArray<byte>& output, int co
511507 }
512508
513509 if (srcIdx >= srcEnd)
514- break ;
510+ return false ;
515511
516512 mLen += int (src[srcIdx++]);
517513
0 commit comments