LzBinTree.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // LzBinTree.cs
  2. using System;
  3. namespace SevenZip.Compression.LZ
  4. {
  5. public class BinTree : InWindow, IMatchFinder
  6. {
  7. UInt32 _cyclicBufferPos;
  8. UInt32 _cyclicBufferSize = 0;
  9. UInt32 _matchMaxLen;
  10. UInt32[] _son;
  11. UInt32[] _hash;
  12. UInt32 _cutValue = 0xFF;
  13. UInt32 _hashMask;
  14. UInt32 _hashSizeSum = 0;
  15. bool HASH_ARRAY = true;
  16. const UInt32 kHash2Size = 1 << 10;
  17. const UInt32 kHash3Size = 1 << 16;
  18. const UInt32 kBT2HashSize = 1 << 16;
  19. const UInt32 kStartMaxLen = 1;
  20. const UInt32 kHash3Offset = kHash2Size;
  21. const UInt32 kEmptyHashValue = 0;
  22. const UInt32 kMaxValForNormalize = ((UInt32)1 << 31) - 1;
  23. UInt32 kNumHashDirectBytes = 0;
  24. UInt32 kMinMatchCheck = 4;
  25. UInt32 kFixHashSize = kHash2Size + kHash3Size;
  26. public void SetType(int numHashBytes)
  27. {
  28. HASH_ARRAY = (numHashBytes > 2);
  29. if (HASH_ARRAY)
  30. {
  31. kNumHashDirectBytes = 0;
  32. kMinMatchCheck = 4;
  33. kFixHashSize = kHash2Size + kHash3Size;
  34. }
  35. else
  36. {
  37. kNumHashDirectBytes = 2;
  38. kMinMatchCheck = 2 + 1;
  39. kFixHashSize = 0;
  40. }
  41. }
  42. public new void SetStream(System.IO.Stream stream) { base.SetStream(stream); }
  43. public new void ReleaseStream() { base.ReleaseStream(); }
  44. public new void Init()
  45. {
  46. base.Init();
  47. for (UInt32 i = 0; i < _hashSizeSum; i++)
  48. _hash[i] = kEmptyHashValue;
  49. _cyclicBufferPos = 0;
  50. ReduceOffsets(-1);
  51. }
  52. public new void MovePos()
  53. {
  54. if (++_cyclicBufferPos >= _cyclicBufferSize)
  55. _cyclicBufferPos = 0;
  56. base.MovePos();
  57. if (_pos == kMaxValForNormalize)
  58. Normalize();
  59. }
  60. public new Byte GetIndexByte(Int32 index) { return base.GetIndexByte(index); }
  61. public new UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit)
  62. { return base.GetMatchLen(index, distance, limit); }
  63. public new UInt32 GetNumAvailableBytes() { return base.GetNumAvailableBytes(); }
  64. public void Create(UInt32 historySize, UInt32 keepAddBufferBefore,
  65. UInt32 matchMaxLen, UInt32 keepAddBufferAfter)
  66. {
  67. if (historySize > kMaxValForNormalize - 256)
  68. throw new Exception();
  69. _cutValue = 16 + (matchMaxLen >> 1);
  70. UInt32 windowReservSize = (historySize + keepAddBufferBefore +
  71. matchMaxLen + keepAddBufferAfter) / 2 + 256;
  72. base.Create(historySize + keepAddBufferBefore, matchMaxLen + keepAddBufferAfter, windowReservSize);
  73. _matchMaxLen = matchMaxLen;
  74. UInt32 cyclicBufferSize = historySize + 1;
  75. if (_cyclicBufferSize != cyclicBufferSize)
  76. _son = new UInt32[(_cyclicBufferSize = cyclicBufferSize) * 2];
  77. UInt32 hs = kBT2HashSize;
  78. if (HASH_ARRAY)
  79. {
  80. hs = historySize - 1;
  81. hs |= (hs >> 1);
  82. hs |= (hs >> 2);
  83. hs |= (hs >> 4);
  84. hs |= (hs >> 8);
  85. hs >>= 1;
  86. hs |= 0xFFFF;
  87. if (hs > (1 << 24))
  88. hs >>= 1;
  89. _hashMask = hs;
  90. hs++;
  91. hs += kFixHashSize;
  92. }
  93. if (hs != _hashSizeSum)
  94. _hash = new UInt32[_hashSizeSum = hs];
  95. }
  96. public UInt32 GetMatches(UInt32[] distances)
  97. {
  98. UInt32 lenLimit;
  99. if (_pos + _matchMaxLen <= _streamPos)
  100. lenLimit = _matchMaxLen;
  101. else
  102. {
  103. lenLimit = _streamPos - _pos;
  104. if (lenLimit < kMinMatchCheck)
  105. {
  106. MovePos();
  107. return 0;
  108. }
  109. }
  110. UInt32 offset = 0;
  111. UInt32 matchMinPos = (_pos > _cyclicBufferSize) ? (_pos - _cyclicBufferSize) : 0;
  112. UInt32 cur = _bufferOffset + _pos;
  113. UInt32 maxLen = kStartMaxLen; // to avoid items for len < hashSize;
  114. UInt32 hashValue, hash2Value = 0, hash3Value = 0;
  115. if (HASH_ARRAY)
  116. {
  117. UInt32 temp = CRC.Table[_bufferBase[cur]] ^ _bufferBase[cur + 1];
  118. hash2Value = temp & (kHash2Size - 1);
  119. temp ^= ((UInt32)(_bufferBase[cur + 2]) << 8);
  120. hash3Value = temp & (kHash3Size - 1);
  121. hashValue = (temp ^ (CRC.Table[_bufferBase[cur + 3]] << 5)) & _hashMask;
  122. }
  123. else
  124. hashValue = _bufferBase[cur] ^ ((UInt32)(_bufferBase[cur + 1]) << 8);
  125. UInt32 curMatch = _hash[kFixHashSize + hashValue];
  126. if (HASH_ARRAY)
  127. {
  128. UInt32 curMatch2 = _hash[hash2Value];
  129. UInt32 curMatch3 = _hash[kHash3Offset + hash3Value];
  130. _hash[hash2Value] = _pos;
  131. _hash[kHash3Offset + hash3Value] = _pos;
  132. if (curMatch2 > matchMinPos)
  133. if (_bufferBase[_bufferOffset + curMatch2] == _bufferBase[cur])
  134. {
  135. distances[offset++] = maxLen = 2;
  136. distances[offset++] = _pos - curMatch2 - 1;
  137. }
  138. if (curMatch3 > matchMinPos)
  139. if (_bufferBase[_bufferOffset + curMatch3] == _bufferBase[cur])
  140. {
  141. if (curMatch3 == curMatch2)
  142. offset -= 2;
  143. distances[offset++] = maxLen = 3;
  144. distances[offset++] = _pos - curMatch3 - 1;
  145. curMatch2 = curMatch3;
  146. }
  147. if (offset != 0 && curMatch2 == curMatch)
  148. {
  149. offset -= 2;
  150. maxLen = kStartMaxLen;
  151. }
  152. }
  153. _hash[kFixHashSize + hashValue] = _pos;
  154. UInt32 ptr0 = (_cyclicBufferPos << 1) + 1;
  155. UInt32 ptr1 = (_cyclicBufferPos << 1);
  156. UInt32 len0, len1;
  157. len0 = len1 = kNumHashDirectBytes;
  158. if (kNumHashDirectBytes != 0)
  159. {
  160. if (curMatch > matchMinPos)
  161. {
  162. if (_bufferBase[_bufferOffset + curMatch + kNumHashDirectBytes] !=
  163. _bufferBase[cur + kNumHashDirectBytes])
  164. {
  165. distances[offset++] = maxLen = kNumHashDirectBytes;
  166. distances[offset++] = _pos - curMatch - 1;
  167. }
  168. }
  169. }
  170. UInt32 count = _cutValue;
  171. while(true)
  172. {
  173. if(curMatch <= matchMinPos || count-- == 0)
  174. {
  175. _son[ptr0] = _son[ptr1] = kEmptyHashValue;
  176. break;
  177. }
  178. UInt32 delta = _pos - curMatch;
  179. UInt32 cyclicPos = ((delta <= _cyclicBufferPos) ?
  180. (_cyclicBufferPos - delta) :
  181. (_cyclicBufferPos - delta + _cyclicBufferSize)) << 1;
  182. UInt32 pby1 = _bufferOffset + curMatch;
  183. UInt32 len = Math.Min(len0, len1);
  184. if (_bufferBase[pby1 + len] == _bufferBase[cur + len])
  185. {
  186. while(++len != lenLimit)
  187. if (_bufferBase[pby1 + len] != _bufferBase[cur + len])
  188. break;
  189. if (maxLen < len)
  190. {
  191. distances[offset++] = maxLen = len;
  192. distances[offset++] = delta - 1;
  193. if (len == lenLimit)
  194. {
  195. _son[ptr1] = _son[cyclicPos];
  196. _son[ptr0] = _son[cyclicPos + 1];
  197. break;
  198. }
  199. }
  200. }
  201. if (_bufferBase[pby1 + len] < _bufferBase[cur + len])
  202. {
  203. _son[ptr1] = curMatch;
  204. ptr1 = cyclicPos + 1;
  205. curMatch = _son[ptr1];
  206. len1 = len;
  207. }
  208. else
  209. {
  210. _son[ptr0] = curMatch;
  211. ptr0 = cyclicPos;
  212. curMatch = _son[ptr0];
  213. len0 = len;
  214. }
  215. }
  216. MovePos();
  217. return offset;
  218. }
  219. public void Skip(UInt32 num)
  220. {
  221. do
  222. {
  223. UInt32 lenLimit;
  224. if (_pos + _matchMaxLen <= _streamPos)
  225. lenLimit = _matchMaxLen;
  226. else
  227. {
  228. lenLimit = _streamPos - _pos;
  229. if (lenLimit < kMinMatchCheck)
  230. {
  231. MovePos();
  232. continue;
  233. }
  234. }
  235. UInt32 matchMinPos = (_pos > _cyclicBufferSize) ? (_pos - _cyclicBufferSize) : 0;
  236. UInt32 cur = _bufferOffset + _pos;
  237. UInt32 hashValue;
  238. if (HASH_ARRAY)
  239. {
  240. UInt32 temp = CRC.Table[_bufferBase[cur]] ^ _bufferBase[cur + 1];
  241. UInt32 hash2Value = temp & (kHash2Size - 1);
  242. _hash[hash2Value] = _pos;
  243. temp ^= ((UInt32)(_bufferBase[cur + 2]) << 8);
  244. UInt32 hash3Value = temp & (kHash3Size - 1);
  245. _hash[kHash3Offset + hash3Value] = _pos;
  246. hashValue = (temp ^ (CRC.Table[_bufferBase[cur + 3]] << 5)) & _hashMask;
  247. }
  248. else
  249. hashValue = _bufferBase[cur] ^ ((UInt32)(_bufferBase[cur + 1]) << 8);
  250. UInt32 curMatch = _hash[kFixHashSize + hashValue];
  251. _hash[kFixHashSize + hashValue] = _pos;
  252. UInt32 ptr0 = (_cyclicBufferPos << 1) + 1;
  253. UInt32 ptr1 = (_cyclicBufferPos << 1);
  254. UInt32 len0, len1;
  255. len0 = len1 = kNumHashDirectBytes;
  256. UInt32 count = _cutValue;
  257. while (true)
  258. {
  259. if (curMatch <= matchMinPos || count-- == 0)
  260. {
  261. _son[ptr0] = _son[ptr1] = kEmptyHashValue;
  262. break;
  263. }
  264. UInt32 delta = _pos - curMatch;
  265. UInt32 cyclicPos = ((delta <= _cyclicBufferPos) ?
  266. (_cyclicBufferPos - delta) :
  267. (_cyclicBufferPos - delta + _cyclicBufferSize)) << 1;
  268. UInt32 pby1 = _bufferOffset + curMatch;
  269. UInt32 len = Math.Min(len0, len1);
  270. if (_bufferBase[pby1 + len] == _bufferBase[cur + len])
  271. {
  272. while (++len != lenLimit)
  273. if (_bufferBase[pby1 + len] != _bufferBase[cur + len])
  274. break;
  275. if (len == lenLimit)
  276. {
  277. _son[ptr1] = _son[cyclicPos];
  278. _son[ptr0] = _son[cyclicPos + 1];
  279. break;
  280. }
  281. }
  282. if (_bufferBase[pby1 + len] < _bufferBase[cur + len])
  283. {
  284. _son[ptr1] = curMatch;
  285. ptr1 = cyclicPos + 1;
  286. curMatch = _son[ptr1];
  287. len1 = len;
  288. }
  289. else
  290. {
  291. _son[ptr0] = curMatch;
  292. ptr0 = cyclicPos;
  293. curMatch = _son[ptr0];
  294. len0 = len;
  295. }
  296. }
  297. MovePos();
  298. }
  299. while (--num != 0);
  300. }
  301. void NormalizeLinks(UInt32[] items, UInt32 numItems, UInt32 subValue)
  302. {
  303. for (UInt32 i = 0; i < numItems; i++)
  304. {
  305. UInt32 value = items[i];
  306. if (value <= subValue)
  307. value = kEmptyHashValue;
  308. else
  309. value -= subValue;
  310. items[i] = value;
  311. }
  312. }
  313. void Normalize()
  314. {
  315. UInt32 subValue = _pos - _cyclicBufferSize;
  316. NormalizeLinks(_son, _cyclicBufferSize * 2, subValue);
  317. NormalizeLinks(_hash, _hashSizeSum, subValue);
  318. ReduceOffsets((Int32)subValue);
  319. }
  320. public void SetCutValue(UInt32 cutValue) { _cutValue = cutValue; }
  321. }
  322. }