LzBinTree.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* This file is part of SevenZipSharp.
  2. SevenZipSharp is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU Lesser General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. SevenZipSharp is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with SevenZipSharp. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. using System;
  14. using System.IO;
  15. namespace SevenZip.Sdk.Compression.LZ
  16. {
  17. internal class BinTree : InWindow, IMatchFinder
  18. {
  19. private const UInt32 kBT2HashSize = 1 << 16;
  20. private const UInt32 kEmptyHashValue = 0;
  21. private const UInt32 kHash2Size = 1 << 10;
  22. private const UInt32 kHash3Offset = kHash2Size;
  23. private const UInt32 kHash3Size = 1 << 16;
  24. private const UInt32 kMaxValForNormalize = ((UInt32) 1 << 31) - 1;
  25. private const UInt32 kStartMaxLen = 1;
  26. private UInt32 _cutValue = 0xFF;
  27. private UInt32 _cyclicBufferPos;
  28. private UInt32 _cyclicBufferSize;
  29. private UInt32[] _hash;
  30. private UInt32 _hashMask;
  31. private UInt32 _hashSizeSum;
  32. private UInt32 _matchMaxLen;
  33. private UInt32[] _son;
  34. private bool HASH_ARRAY = true;
  35. private UInt32 kFixHashSize = kHash2Size + kHash3Size;
  36. private UInt32 kMinMatchCheck = 4;
  37. private UInt32 kNumHashDirectBytes;
  38. #region IMatchFinder Members
  39. public new void SetStream(Stream stream)
  40. {
  41. base.SetStream(stream);
  42. }
  43. public new void ReleaseStream()
  44. {
  45. base.ReleaseStream();
  46. }
  47. public new void Init()
  48. {
  49. base.Init();
  50. for (UInt32 i = 0; i < _hashSizeSum; i++)
  51. _hash[i] = kEmptyHashValue;
  52. _cyclicBufferPos = 0;
  53. ReduceOffsets(-1);
  54. }
  55. public new Byte GetIndexByte(Int32 index)
  56. {
  57. return base.GetIndexByte(index);
  58. }
  59. public new UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit)
  60. {
  61. return base.GetMatchLen(index, distance, limit);
  62. }
  63. public new UInt32 GetNumAvailableBytes()
  64. {
  65. return base.GetNumAvailableBytes();
  66. }
  67. public void Create(UInt32 historySize, UInt32 keepAddBufferBefore,
  68. UInt32 matchMaxLen, UInt32 keepAddBufferAfter)
  69. {
  70. if (historySize + 256 > kMaxValForNormalize)
  71. {
  72. throw new ArgumentException("historySize + 256 > kMaxValForNormalize", "historySize");
  73. }
  74. _cutValue = 16 + (matchMaxLen >> 1);
  75. UInt32 windowReservSize = (historySize + keepAddBufferBefore +
  76. matchMaxLen + keepAddBufferAfter)/2 + 256;
  77. base.Create(historySize + keepAddBufferBefore, matchMaxLen + keepAddBufferAfter, windowReservSize);
  78. _matchMaxLen = matchMaxLen;
  79. UInt32 cyclicBufferSize = historySize + 1;
  80. if (_cyclicBufferSize != cyclicBufferSize)
  81. _son = new UInt32[(_cyclicBufferSize = cyclicBufferSize)*2];
  82. UInt32 hs = kBT2HashSize;
  83. if (HASH_ARRAY)
  84. {
  85. hs = historySize - 1;
  86. hs |= (hs >> 1);
  87. hs |= (hs >> 2);
  88. hs |= (hs >> 4);
  89. hs |= (hs >> 8);
  90. hs >>= 1;
  91. hs |= 0xFFFF;
  92. if (hs > (1 << 24))
  93. hs >>= 1;
  94. _hashMask = hs;
  95. hs++;
  96. hs += kFixHashSize;
  97. }
  98. if (hs != _hashSizeSum)
  99. _hash = new UInt32[_hashSizeSum = hs];
  100. }
  101. public UInt32 GetMatches(UInt32[] distances)
  102. {
  103. UInt32 lenLimit;
  104. if (_pos + _matchMaxLen <= _streamPos)
  105. lenLimit = _matchMaxLen;
  106. else
  107. {
  108. lenLimit = _streamPos - _pos;
  109. if (lenLimit < kMinMatchCheck)
  110. {
  111. MovePos();
  112. return 0;
  113. }
  114. }
  115. UInt32 offset = 0;
  116. UInt32 matchMinPos = (_pos > _cyclicBufferSize) ? (_pos - _cyclicBufferSize) : 0;
  117. UInt32 cur = _bufferOffset + _pos;
  118. UInt32 maxLen = kStartMaxLen; // to avoid items for len < hashSize;
  119. UInt32 hashValue, hash2Value = 0, hash3Value = 0;
  120. if (HASH_ARRAY)
  121. {
  122. UInt32 temp = CRC.Table[_bufferBase[cur]] ^ _bufferBase[cur + 1];
  123. hash2Value = (temp & (((int) kHash2Size) - 1));
  124. temp ^= (uint) ((_bufferBase[cur + 2]) << 8);
  125. hash3Value = (temp & (((int) kHash3Size) - 1));
  126. hashValue = (temp ^ (CRC.Table[_bufferBase[cur + 3]] << 5)) & _hashMask;
  127. }
  128. else
  129. hashValue = _bufferBase[cur] ^ ((UInt32) (_bufferBase[cur + 1]) << 8);
  130. UInt32 curMatch = _hash[kFixHashSize + hashValue];
  131. if (HASH_ARRAY)
  132. {
  133. UInt32 curMatch2 = _hash[hash2Value];
  134. UInt32 curMatch3 = _hash[kHash3Offset + hash3Value];
  135. _hash[hash2Value] = _pos;
  136. _hash[kHash3Offset + hash3Value] = _pos;
  137. if (curMatch2 > matchMinPos)
  138. if (_bufferBase[_bufferOffset + curMatch2] == _bufferBase[cur])
  139. {
  140. distances[offset++] = maxLen = 2;
  141. distances[offset++] = _pos - curMatch2 - 1;
  142. }
  143. if (curMatch3 > matchMinPos)
  144. if (_bufferBase[_bufferOffset + curMatch3] == _bufferBase[cur])
  145. {
  146. if (curMatch3 == curMatch2)
  147. offset -= 2;
  148. distances[offset++] = maxLen = 3;
  149. distances[offset++] = _pos - curMatch3 - 1;
  150. curMatch2 = curMatch3;
  151. }
  152. if (offset != 0 && curMatch2 == curMatch)
  153. {
  154. offset -= 2;
  155. maxLen = kStartMaxLen;
  156. }
  157. }
  158. _hash[kFixHashSize + hashValue] = _pos;
  159. UInt32 ptr0 = (_cyclicBufferPos << 1) + 1;
  160. UInt32 ptr1 = (_cyclicBufferPos << 1);
  161. UInt32 len0, len1;
  162. len0 = len1 = kNumHashDirectBytes;
  163. if (kNumHashDirectBytes != 0)
  164. {
  165. if (curMatch > matchMinPos)
  166. {
  167. if (_bufferBase[_bufferOffset + curMatch + kNumHashDirectBytes] !=
  168. _bufferBase[cur + kNumHashDirectBytes])
  169. {
  170. distances[offset++] = maxLen = kNumHashDirectBytes;
  171. distances[offset++] = _pos - curMatch - 1;
  172. }
  173. }
  174. }
  175. UInt32 count = _cutValue;
  176. while (true)
  177. {
  178. if (curMatch <= matchMinPos || count-- == 0)
  179. {
  180. _son[ptr0] = _son[ptr1] = kEmptyHashValue;
  181. break;
  182. }
  183. UInt32 delta = _pos - curMatch;
  184. UInt32 cyclicPos = ((delta <= _cyclicBufferPos)
  185. ?
  186. (_cyclicBufferPos - delta)
  187. :
  188. (_cyclicBufferPos - delta + _cyclicBufferSize)) << 1;
  189. UInt32 pby1 = _bufferOffset + curMatch;
  190. UInt32 len = Math.Min(len0, len1);
  191. if (_bufferBase[pby1 + len] == _bufferBase[cur + len])
  192. {
  193. while (++len != lenLimit)
  194. if (_bufferBase[pby1 + len] != _bufferBase[cur + len])
  195. break;
  196. if (maxLen < len)
  197. {
  198. distances[offset++] = maxLen = len;
  199. distances[offset++] = delta - 1;
  200. if (len == lenLimit)
  201. {
  202. _son[ptr1] = _son[cyclicPos];
  203. _son[ptr0] = _son[cyclicPos + 1];
  204. break;
  205. }
  206. }
  207. }
  208. if (_bufferBase[pby1 + len] < _bufferBase[cur + len])
  209. {
  210. _son[ptr1] = curMatch;
  211. ptr1 = cyclicPos + 1;
  212. curMatch = _son[ptr1];
  213. len1 = len;
  214. }
  215. else
  216. {
  217. _son[ptr0] = curMatch;
  218. ptr0 = cyclicPos;
  219. curMatch = _son[ptr0];
  220. len0 = len;
  221. }
  222. }
  223. MovePos();
  224. return offset;
  225. }
  226. public void Skip(UInt32 num)
  227. {
  228. do
  229. {
  230. UInt32 lenLimit;
  231. if (_pos + _matchMaxLen <= _streamPos)
  232. lenLimit = _matchMaxLen;
  233. else
  234. {
  235. lenLimit = _streamPos - _pos;
  236. if (lenLimit < kMinMatchCheck)
  237. {
  238. MovePos();
  239. continue;
  240. }
  241. }
  242. UInt32 matchMinPos = (_pos > _cyclicBufferSize) ? (_pos - _cyclicBufferSize) : 0;
  243. UInt32 cur = _bufferOffset + _pos;
  244. UInt32 hashValue;
  245. if (HASH_ARRAY)
  246. {
  247. UInt32 temp = CRC.Table[_bufferBase[cur]] ^ _bufferBase[cur + 1];
  248. UInt32 hash2Value = (temp & (((int) kHash2Size) - 1));
  249. _hash[hash2Value] = _pos;
  250. temp ^= ((UInt32) (_bufferBase[cur + 2]) << 8);
  251. UInt32 hash3Value = (temp & (((int) kHash3Size) - 1));
  252. _hash[kHash3Offset + hash3Value] = _pos;
  253. hashValue = (temp ^ (CRC.Table[_bufferBase[cur + 3]] << 5)) & _hashMask;
  254. }
  255. else
  256. hashValue = _bufferBase[cur] ^ ((UInt32) (_bufferBase[cur + 1]) << 8);
  257. UInt32 curMatch = _hash[kFixHashSize + hashValue];
  258. _hash[kFixHashSize + hashValue] = _pos;
  259. UInt32 ptr0 = (_cyclicBufferPos << 1) + 1;
  260. UInt32 ptr1 = (_cyclicBufferPos << 1);
  261. UInt32 len0, len1;
  262. len0 = len1 = kNumHashDirectBytes;
  263. UInt32 count = _cutValue;
  264. while (true)
  265. {
  266. if (curMatch <= matchMinPos || count-- == 0)
  267. {
  268. _son[ptr0] = _son[ptr1] = kEmptyHashValue;
  269. break;
  270. }
  271. UInt32 delta = _pos - curMatch;
  272. UInt32 cyclicPos = ((delta <= _cyclicBufferPos)
  273. ?
  274. (_cyclicBufferPos - delta)
  275. :
  276. (_cyclicBufferPos - delta + _cyclicBufferSize)) << 1;
  277. UInt32 pby1 = _bufferOffset + curMatch;
  278. UInt32 len = Math.Min(len0, len1);
  279. if (_bufferBase[pby1 + len] == _bufferBase[cur + len])
  280. {
  281. while (++len != lenLimit)
  282. if (_bufferBase[pby1 + len] != _bufferBase[cur + len])
  283. break;
  284. if (len == lenLimit)
  285. {
  286. _son[ptr1] = _son[cyclicPos];
  287. _son[ptr0] = _son[cyclicPos + 1];
  288. break;
  289. }
  290. }
  291. if (_bufferBase[pby1 + len] < _bufferBase[cur + len])
  292. {
  293. _son[ptr1] = curMatch;
  294. ptr1 = cyclicPos + 1;
  295. curMatch = _son[ptr1];
  296. len1 = len;
  297. }
  298. else
  299. {
  300. _son[ptr0] = curMatch;
  301. ptr0 = cyclicPos;
  302. curMatch = _son[ptr0];
  303. len0 = len;
  304. }
  305. }
  306. MovePos();
  307. } while (--num != 0);
  308. }
  309. #endregion
  310. public void SetType(int numHashBytes)
  311. {
  312. HASH_ARRAY = (numHashBytes > 2);
  313. if (HASH_ARRAY)
  314. {
  315. kNumHashDirectBytes = 0;
  316. kMinMatchCheck = 4;
  317. kFixHashSize = kHash2Size + kHash3Size;
  318. }
  319. else
  320. {
  321. kNumHashDirectBytes = 2;
  322. kMinMatchCheck = 2 + 1;
  323. kFixHashSize = 0;
  324. }
  325. }
  326. public new void MovePos()
  327. {
  328. if (++_cyclicBufferPos >= _cyclicBufferSize)
  329. _cyclicBufferPos = 0;
  330. base.MovePos();
  331. if (_pos == kMaxValForNormalize)
  332. Normalize();
  333. }
  334. private static void NormalizeLinks(UInt32[] items, UInt32 numItems, UInt32 subValue)
  335. {
  336. for (UInt32 i = 0; i < numItems; i++)
  337. {
  338. UInt32 value = items[i];
  339. if (value <= subValue)
  340. value = kEmptyHashValue;
  341. else
  342. value -= subValue;
  343. items[i] = value;
  344. }
  345. }
  346. private void Normalize()
  347. {
  348. UInt32 subValue = _pos - _cyclicBufferSize;
  349. NormalizeLinks(_son, _cyclicBufferSize*2, subValue);
  350. NormalizeLinks(_hash, _hashSizeSum, subValue);
  351. ReduceOffsets((Int32) subValue);
  352. }
  353. //public void SetCutValue(UInt32 cutValue) { _cutValue = cutValue; }
  354. }
  355. }