5 namespace SevenZip.Compression.LZ
7 public class BinTree : InWindow, IMatchFinder
9 UInt32 _cyclicBufferPos;
10 UInt32 _cyclicBufferSize = 0;
16 UInt32 _cutValue = 0xFF;
18 UInt32 _hashSizeSum = 0;
20 bool HASH_ARRAY = true;
22 const UInt32 kHash2Size = 1 << 10;
23 const UInt32 kHash3Size = 1 << 16;
24 const UInt32 kBT2HashSize = 1 << 16;
25 const UInt32 kStartMaxLen = 1;
26 const UInt32 kHash3Offset = kHash2Size;
27 const UInt32 kEmptyHashValue = 0;
28 const UInt32 kMaxValForNormalize = ((UInt32)1 << 31) - 1;
30 UInt32 kNumHashDirectBytes = 0;
31 UInt32 kMinMatchCheck = 4;
32 UInt32 kFixHashSize = kHash2Size + kHash3Size;
34 public void SetType(int numHashBytes)
36 HASH_ARRAY = (numHashBytes > 2);
39 kNumHashDirectBytes = 0;
41 kFixHashSize = kHash2Size + kHash3Size;
45 kNumHashDirectBytes = 2;
46 kMinMatchCheck = 2 + 1;
51 public new void SetStream(System.IO.Stream stream) { base.SetStream(stream); }
52 public new void ReleaseStream() { base.ReleaseStream(); }
54 public new void Init()
57 for (UInt32 i = 0; i < _hashSizeSum; i++)
58 _hash[i] = kEmptyHashValue;
63 public new void MovePos()
65 if (++_cyclicBufferPos >= _cyclicBufferSize)
68 if (_pos == kMaxValForNormalize)
72 public new Byte GetIndexByte(Int32 index) { return base.GetIndexByte(index); }
74 public new UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit)
75 { return base.GetMatchLen(index, distance, limit); }
77 public new UInt32 GetNumAvailableBytes() { return base.GetNumAvailableBytes(); }
79 public void Create(UInt32 historySize, UInt32 keepAddBufferBefore,
80 UInt32 matchMaxLen, UInt32 keepAddBufferAfter)
82 if (historySize > kMaxValForNormalize - 256)
83 throw new Exception();
84 _cutValue = 16 + (matchMaxLen >> 1);
86 UInt32 windowReservSize = (historySize + keepAddBufferBefore +
87 matchMaxLen + keepAddBufferAfter) / 2 + 256;
89 base.Create(historySize + keepAddBufferBefore, matchMaxLen + keepAddBufferAfter, windowReservSize);
91 _matchMaxLen = matchMaxLen;
93 UInt32 cyclicBufferSize = historySize + 1;
94 if (_cyclicBufferSize != cyclicBufferSize)
95 _son = new UInt32[(_cyclicBufferSize = cyclicBufferSize) * 2];
97 UInt32 hs = kBT2HashSize;
101 hs = historySize - 1;
114 if (hs != _hashSizeSum)
115 _hash = new UInt32[_hashSizeSum = hs];
118 public UInt32 GetMatches(UInt32[] distances)
121 if (_pos + _matchMaxLen <= _streamPos)
122 lenLimit = _matchMaxLen;
125 lenLimit = _streamPos - _pos;
126 if (lenLimit < kMinMatchCheck)
134 UInt32 matchMinPos = (_pos > _cyclicBufferSize) ? (_pos - _cyclicBufferSize) : 0;
135 UInt32 cur = _bufferOffset + _pos;
136 UInt32 maxLen = kStartMaxLen; // to avoid items for len < hashSize;
137 UInt32 hashValue, hash2Value = 0, hash3Value = 0;
141 UInt32 temp = CRC.Table[_bufferBase[cur]] ^ _bufferBase[cur + 1];
142 hash2Value = temp & (kHash2Size - 1);
143 temp ^= ((UInt32)(_bufferBase[cur + 2]) << 8);
144 hash3Value = temp & (kHash3Size - 1);
145 hashValue = (temp ^ (CRC.Table[_bufferBase[cur + 3]] << 5)) & _hashMask;
148 hashValue = _bufferBase[cur] ^ ((UInt32)(_bufferBase[cur + 1]) << 8);
150 UInt32 curMatch = _hash[kFixHashSize + hashValue];
153 UInt32 curMatch2 = _hash[hash2Value];
154 UInt32 curMatch3 = _hash[kHash3Offset + hash3Value];
155 _hash[hash2Value] = _pos;
156 _hash[kHash3Offset + hash3Value] = _pos;
157 if (curMatch2 > matchMinPos)
158 if (_bufferBase[_bufferOffset + curMatch2] == _bufferBase[cur])
160 distances[offset++] = maxLen = 2;
161 distances[offset++] = _pos - curMatch2 - 1;
163 if (curMatch3 > matchMinPos)
164 if (_bufferBase[_bufferOffset + curMatch3] == _bufferBase[cur])
166 if (curMatch3 == curMatch2)
168 distances[offset++] = maxLen = 3;
169 distances[offset++] = _pos - curMatch3 - 1;
170 curMatch2 = curMatch3;
172 if (offset != 0 && curMatch2 == curMatch)
175 maxLen = kStartMaxLen;
179 _hash[kFixHashSize + hashValue] = _pos;
181 UInt32 ptr0 = (_cyclicBufferPos << 1) + 1;
182 UInt32 ptr1 = (_cyclicBufferPos << 1);
185 len0 = len1 = kNumHashDirectBytes;
187 if (kNumHashDirectBytes != 0)
189 if (curMatch > matchMinPos)
191 if (_bufferBase[_bufferOffset + curMatch + kNumHashDirectBytes] !=
192 _bufferBase[cur + kNumHashDirectBytes])
194 distances[offset++] = maxLen = kNumHashDirectBytes;
195 distances[offset++] = _pos - curMatch - 1;
200 UInt32 count = _cutValue;
204 if(curMatch <= matchMinPos || count-- == 0)
206 _son[ptr0] = _son[ptr1] = kEmptyHashValue;
209 UInt32 delta = _pos - curMatch;
210 UInt32 cyclicPos = ((delta <= _cyclicBufferPos) ?
211 (_cyclicBufferPos - delta) :
212 (_cyclicBufferPos - delta + _cyclicBufferSize)) << 1;
214 UInt32 pby1 = _bufferOffset + curMatch;
215 UInt32 len = Math.Min(len0, len1);
216 if (_bufferBase[pby1 + len] == _bufferBase[cur + len])
218 while(++len != lenLimit)
219 if (_bufferBase[pby1 + len] != _bufferBase[cur + len])
223 distances[offset++] = maxLen = len;
224 distances[offset++] = delta - 1;
227 _son[ptr1] = _son[cyclicPos];
228 _son[ptr0] = _son[cyclicPos + 1];
233 if (_bufferBase[pby1 + len] < _bufferBase[cur + len])
235 _son[ptr1] = curMatch;
236 ptr1 = cyclicPos + 1;
237 curMatch = _son[ptr1];
242 _son[ptr0] = curMatch;
244 curMatch = _son[ptr0];
252 public void Skip(UInt32 num)
257 if (_pos + _matchMaxLen <= _streamPos)
258 lenLimit = _matchMaxLen;
261 lenLimit = _streamPos - _pos;
262 if (lenLimit < kMinMatchCheck)
269 UInt32 matchMinPos = (_pos > _cyclicBufferSize) ? (_pos - _cyclicBufferSize) : 0;
270 UInt32 cur = _bufferOffset + _pos;
276 UInt32 temp = CRC.Table[_bufferBase[cur]] ^ _bufferBase[cur + 1];
277 UInt32 hash2Value = temp & (kHash2Size - 1);
278 _hash[hash2Value] = _pos;
279 temp ^= ((UInt32)(_bufferBase[cur + 2]) << 8);
280 UInt32 hash3Value = temp & (kHash3Size - 1);
281 _hash[kHash3Offset + hash3Value] = _pos;
282 hashValue = (temp ^ (CRC.Table[_bufferBase[cur + 3]] << 5)) & _hashMask;
285 hashValue = _bufferBase[cur] ^ ((UInt32)(_bufferBase[cur + 1]) << 8);
287 UInt32 curMatch = _hash[kFixHashSize + hashValue];
288 _hash[kFixHashSize + hashValue] = _pos;
290 UInt32 ptr0 = (_cyclicBufferPos << 1) + 1;
291 UInt32 ptr1 = (_cyclicBufferPos << 1);
294 len0 = len1 = kNumHashDirectBytes;
296 UInt32 count = _cutValue;
299 if (curMatch <= matchMinPos || count-- == 0)
301 _son[ptr0] = _son[ptr1] = kEmptyHashValue;
305 UInt32 delta = _pos - curMatch;
306 UInt32 cyclicPos = ((delta <= _cyclicBufferPos) ?
307 (_cyclicBufferPos - delta) :
308 (_cyclicBufferPos - delta + _cyclicBufferSize)) << 1;
310 UInt32 pby1 = _bufferOffset + curMatch;
311 UInt32 len = Math.Min(len0, len1);
312 if (_bufferBase[pby1 + len] == _bufferBase[cur + len])
314 while (++len != lenLimit)
315 if (_bufferBase[pby1 + len] != _bufferBase[cur + len])
319 _son[ptr1] = _son[cyclicPos];
320 _son[ptr0] = _son[cyclicPos + 1];
324 if (_bufferBase[pby1 + len] < _bufferBase[cur + len])
326 _son[ptr1] = curMatch;
327 ptr1 = cyclicPos + 1;
328 curMatch = _son[ptr1];
333 _son[ptr0] = curMatch;
335 curMatch = _son[ptr0];
344 void NormalizeLinks(UInt32[] items, UInt32 numItems, UInt32 subValue)
346 for (UInt32 i = 0; i < numItems; i++)
348 UInt32 value = items[i];
349 if (value <= subValue)
350 value = kEmptyHashValue;
359 UInt32 subValue = _pos - _cyclicBufferSize;
360 NormalizeLinks(_son, _cyclicBufferSize * 2, subValue);
361 NormalizeLinks(_hash, _hashSizeSum, subValue);
362 ReduceOffsets((Int32)subValue);
365 public void SetCutValue(UInt32 cutValue) { _cutValue = cutValue; }