Skip to content

Commit cc40bbf

Browse files
committed
Small cleanup
1 parent 67643b7 commit cc40bbf

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

src/transform/LZCodec.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/transform/LZCodec.hpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ namespace kanzi {
113113

114114
static void emitLiterals(const byte src[], byte dst[], int len);
115115

116+
static int findMatch(const byte block[], const int pos, const int ref, const int maxMatch);
117+
116118
static int readLength(const byte block[], int& pos);
117119

118120
static int32 hash(const byte* p);
119-
120-
static int findMatch(byte block[], const int pos, const int ref, int maxMatch);
121121
};
122122

123123
class LZPCodec : public Transform<byte> {
@@ -160,6 +160,8 @@ namespace kanzi {
160160

161161
int32* _hashes;
162162
int _hashSize;
163+
164+
static int findMatch(const byte block[], const int pos, const int ref, const int maxMatch);
163165
};
164166

165167
template <bool T>
@@ -225,12 +227,9 @@ namespace kanzi {
225227
}
226228

227229
template <bool T>
228-
inline int LZXCodec<T>::findMatch(byte src[], const int srcIdx, const int ref, int maxMatch)
230+
inline int LZXCodec<T>::findMatch(const byte src[], const int srcIdx, const int ref, const int maxMatch)
229231
{
230-
if (memcmp(&src[ref], &src[srcIdx], 4) != 0)
231-
return 0;
232-
233-
int bestLen = 4;
232+
int bestLen = 0;
234233

235234
while ((bestLen + 4 < maxMatch) && (memcmp(&src[ref + bestLen], &src[srcIdx + bestLen], 4) == 0))
236235
bestLen += 4;
@@ -240,5 +239,23 @@ namespace kanzi {
240239

241240
return bestLen;
242241
}
242+
243+
244+
245+
inline int LZPCodec::findMatch(const byte src[], const int srcIdx, const int ref, const int maxMatch)
246+
{
247+
int bestLen = 0;
248+
249+
while ((bestLen + 4 < maxMatch) && (memcmp(&src[ref + bestLen], &src[srcIdx + bestLen], 4) == 0))
250+
bestLen += 4;
251+
252+
if (bestLen > MIN_MATCH - 4) {
253+
while ((bestLen < maxMatch) && (src[ref + bestLen] == src[srcIdx + bestLen]))
254+
bestLen++;
255+
}
256+
257+
return bestLen;
258+
}
259+
243260
}
244-
#endif
261+
#endif

0 commit comments

Comments
 (0)