Skip to content

Commit 48e6254

Browse files
committed
LZ/LZX return false if compression too low
1 parent 236cf65 commit 48e6254

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/transform/LZCodec.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ bool LZXCodec<T>::forward(SliceArray<byte>& input, SliceArray<byte>& output, int
132132
_hashes = new int32[_hashSize];
133133
}
134134

135+
if (_bufferSize < max(count / 5, 256)) {
136+
_bufferSize = max(count / 5, 256);
137+
delete[] _mLenBuf;
138+
_mLenBuf = new byte[_bufferSize];
139+
delete[] _mBuf;
140+
_mBuf = new byte[_bufferSize];
141+
delete[] _tkBuf;
142+
_tkBuf = new byte[_bufferSize];
143+
}
144+
135145
memset(_hashes, 0, sizeof(int32) * _hashSize);
136146
const int srcEnd = count - 16 - 1;
137147
const byte* src = &input._array[input._index];
@@ -153,16 +163,6 @@ bool LZXCodec<T>::forward(SliceArray<byte>& input, SliceArray<byte>& output, int
153163
}
154164
}
155165

156-
if (_bufferSize < max(count / 5, 256)) {
157-
_bufferSize = max(count / 5, 256);
158-
delete[] _mLenBuf;
159-
_mLenBuf = new byte[_bufferSize];
160-
delete[] _mBuf;
161-
_mBuf = new byte[_bufferSize];
162-
delete[] _tkBuf;
163-
_tkBuf = new byte[_bufferSize];
164-
}
165-
166166
const int minMatch = mm;
167167
const int dThreshold = (maxDist == MAX_DISTANCE1) ? 1 << 8 : 1 << 16;
168168
int srcIdx = 0;
@@ -208,7 +208,7 @@ bool LZXCodec<T>::forward(SliceArray<byte>& input, SliceArray<byte>& output, int
208208
// No good match ?
209209
if (bestLen < minMatch) {
210210
srcIdx++;
211-
srcIdx += (srcInc >> 7);
211+
srcIdx += (srcInc >> 6);
212212
srcInc++;
213213
repIdx = 0;
214214
continue;
@@ -356,7 +356,6 @@ bool LZXCodec<T>::forward(SliceArray<byte>& input, SliceArray<byte>& output, int
356356
const int32 h = hash(&src[srcIdx]);
357357
_hashes[h] = srcIdx;
358358
}
359-
360359
}
361360

362361
// Emit last literals
@@ -388,7 +387,7 @@ bool LZXCodec<T>::forward(SliceArray<byte>& input, SliceArray<byte>& output, int
388387
dstIdx += mLenIdx;
389388
input._index += count;
390389
output._index += dstIdx;
391-
return true;
390+
return dstIdx <= count - (count / 100);
392391
}
393392

394393
template <bool T>
@@ -480,6 +479,7 @@ bool LZXCodec<T>::inverse(SliceArray<byte>& input, SliceArray<byte>& output, int
480479
mIdx += t;
481480
}
482481

482+
prefetchRead(&src[mLenIdx]);
483483
repd1 = repd0;
484484
repd0 = dist;
485485
const int mEnd = dstIdx + mLen;
@@ -491,7 +491,7 @@ bool LZXCodec<T>::inverse(SliceArray<byte>& input, SliceArray<byte>& output, int
491491
goto exit;
492492
}
493493

494-
prefetchRead(&src[mLenIdx]);
494+
prefetchWrite(&dst[dstIdx]);
495495

496496
// Copy match
497497
if (dist >= 16) {
@@ -575,6 +575,7 @@ bool LZPCodec::forward(SliceArray<byte>& input, SliceArray<byte>& output, int co
575575
int dstIdx = 4;
576576

577577
while ((srcIdx < srcEnd - MIN_MATCH) && (dstIdx < dstEnd)) {
578+
prefetchRead(&src[srcIdx + MIN_MATCH]);
578579
const uint32 h = (HASH_SEED * ctx) >> HASH_SHIFT;
579580
const int32 ref = _hashes[h];
580581
_hashes[h] = srcIdx;

0 commit comments

Comments
 (0)