3 package SevenZip.Compression.LZ;
4 import java.io.IOException;
7 public class BinTree extends InWindow
10 int _cyclicBufferSize = 0;
20 boolean HASH_ARRAY = true;
22 static final int kHash2Size = 1 << 10;
23 static final int kHash3Size = 1 << 16;
24 static final int kBT2HashSize = 1 << 16;
25 static final int kStartMaxLen = 1;
26 static final int kHash3Offset = kHash2Size;
27 static final int kEmptyHashValue = 0;
28 static final int kMaxValForNormalize = (1 << 30) - 1;
30 int kNumHashDirectBytes = 0;
31 int kMinMatchCheck = 4;
32 int 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;
54 public void Init() throws IOException
57 for (int i = 0; i < _hashSizeSum; i++)
58 _hash[i] = kEmptyHashValue;
63 public void MovePos() throws IOException
65 if (++_cyclicBufferPos >= _cyclicBufferSize)
68 if (_pos == kMaxValForNormalize)
79 public boolean Create(int historySize, int keepAddBufferBefore,
80 int matchMaxLen, int keepAddBufferAfter)
82 if (historySize > kMaxValForNormalize - 256)
84 _cutValue = 16 + (matchMaxLen >> 1);
86 int windowReservSize = (historySize + keepAddBufferBefore +
87 matchMaxLen + keepAddBufferAfter) / 2 + 256;
89 super.Create(historySize + keepAddBufferBefore, matchMaxLen + keepAddBufferAfter, windowReservSize);
91 _matchMaxLen = matchMaxLen;
93 int cyclicBufferSize = historySize + 1;
94 if (_cyclicBufferSize != cyclicBufferSize)
95 _son = new int[(_cyclicBufferSize = cyclicBufferSize) * 2];
97 int hs = kBT2HashSize;
101 hs = historySize - 1;
114 if (hs != _hashSizeSum)
115 _hash = new int [_hashSizeSum = hs];
118 public int GetMatches(int[] distances) throws IOException
121 if (_pos + _matchMaxLen <= _streamPos)
122 lenLimit = _matchMaxLen;
125 lenLimit = _streamPos - _pos;
126 if (lenLimit < kMinMatchCheck)
134 int matchMinPos = (_pos > _cyclicBufferSize) ? (_pos - _cyclicBufferSize) : 0;
135 int cur = _bufferOffset + _pos;
136 int maxLen = kStartMaxLen; // to avoid items for len < hashSize;
137 int hashValue, hash2Value = 0, hash3Value = 0;
141 int temp = CrcTable[_bufferBase[cur] & 0xFF] ^ (_bufferBase[cur + 1] & 0xFF);
142 hash2Value = temp & (kHash2Size - 1);
143 temp ^= ((int)(_bufferBase[cur + 2] & 0xFF) << 8);
144 hash3Value = temp & (kHash3Size - 1);
145 hashValue = (temp ^ (CrcTable[_bufferBase[cur + 3] & 0xFF] << 5)) & _hashMask;
148 hashValue = ((_bufferBase[cur] & 0xFF) ^ ((int)(_bufferBase[cur + 1] & 0xFF) << 8));
150 int curMatch = _hash[kFixHashSize + hashValue];
153 int curMatch2 = _hash[hash2Value];
154 int 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 int ptr0 = (_cyclicBufferPos << 1) + 1;
182 int 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 int count = _cutValue;
204 if (curMatch <= matchMinPos || count-- == 0)
206 _son[ptr0] = _son[ptr1] = kEmptyHashValue;
209 int delta = _pos - curMatch;
210 int cyclicPos = ((delta <= _cyclicBufferPos) ?
211 (_cyclicBufferPos - delta) :
212 (_cyclicBufferPos - delta + _cyclicBufferSize)) << 1;
214 int pby1 = _bufferOffset + curMatch;
215 int 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] & 0xFF) < (_bufferBase[cur + len] & 0xFF))
235 _son[ptr1] = curMatch;
236 ptr1 = cyclicPos + 1;
237 curMatch = _son[ptr1];
242 _son[ptr0] = curMatch;
244 curMatch = _son[ptr0];
252 public void Skip(int num) throws IOException
257 if (_pos + _matchMaxLen <= _streamPos)
258 lenLimit = _matchMaxLen;
261 lenLimit = _streamPos - _pos;
262 if (lenLimit < kMinMatchCheck)
269 int matchMinPos = (_pos > _cyclicBufferSize) ? (_pos - _cyclicBufferSize) : 0;
270 int cur = _bufferOffset + _pos;
276 int temp = CrcTable[_bufferBase[cur] & 0xFF] ^ (_bufferBase[cur + 1] & 0xFF);
277 int hash2Value = temp & (kHash2Size - 1);
278 _hash[hash2Value] = _pos;
279 temp ^= ((int)(_bufferBase[cur + 2] & 0xFF) << 8);
280 int hash3Value = temp & (kHash3Size - 1);
281 _hash[kHash3Offset + hash3Value] = _pos;
282 hashValue = (temp ^ (CrcTable[_bufferBase[cur + 3] & 0xFF] << 5)) & _hashMask;
285 hashValue = ((_bufferBase[cur] & 0xFF) ^ ((int)(_bufferBase[cur + 1] & 0xFF) << 8));
287 int curMatch = _hash[kFixHashSize + hashValue];
288 _hash[kFixHashSize + hashValue] = _pos;
290 int ptr0 = (_cyclicBufferPos << 1) + 1;
291 int ptr1 = (_cyclicBufferPos << 1);
294 len0 = len1 = kNumHashDirectBytes;
296 int count = _cutValue;
299 if (curMatch <= matchMinPos || count-- == 0)
301 _son[ptr0] = _son[ptr1] = kEmptyHashValue;
305 int delta = _pos - curMatch;
306 int cyclicPos = ((delta <= _cyclicBufferPos) ?
307 (_cyclicBufferPos - delta) :
308 (_cyclicBufferPos - delta + _cyclicBufferSize)) << 1;
310 int pby1 = _bufferOffset + curMatch;
311 int 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] & 0xFF) < (_bufferBase[cur + len] & 0xFF))
326 _son[ptr1] = curMatch;
327 ptr1 = cyclicPos + 1;
328 curMatch = _son[ptr1];
333 _son[ptr0] = curMatch;
335 curMatch = _son[ptr0];
344 void NormalizeLinks(int[] items, int numItems, int subValue)
346 for (int i = 0; i < numItems; i++)
348 int value = items[i];
349 if (value <= subValue)
350 value = kEmptyHashValue;
359 int subValue = _pos - _cyclicBufferSize;
360 NormalizeLinks(_son, _cyclicBufferSize * 2, subValue);
361 NormalizeLinks(_hash, _hashSizeSum, subValue);
362 ReduceOffsets(subValue);
365 public void SetCutValue(int cutValue) { _cutValue = cutValue; }
367 private static final int[] CrcTable = new int[256];
371 for (int i = 0; i < 256; i++)
374 for (int j = 0; j < 8; j++)
376 r = (r >>> 1) ^ 0xEDB88320;