LzmaDecoder.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. using System;
  2. using System.IO;
  3. using SharpCompress.Compressors.LZMA.LZ;
  4. using SharpCompress.Compressors.LZMA.RangeCoder;
  5. namespace SharpCompress.Compressors.LZMA
  6. {
  7. internal class Decoder : ICoder, ISetDecoderProperties // ,System.IO.Stream
  8. {
  9. private class LenDecoder
  10. {
  11. private BitDecoder _choice = new BitDecoder();
  12. private BitDecoder _choice2 = new BitDecoder();
  13. private readonly BitTreeDecoder[] _lowCoder = new BitTreeDecoder[Base.K_NUM_POS_STATES_MAX];
  14. private readonly BitTreeDecoder[] _midCoder = new BitTreeDecoder[Base.K_NUM_POS_STATES_MAX];
  15. private BitTreeDecoder _highCoder = new BitTreeDecoder(Base.K_NUM_HIGH_LEN_BITS);
  16. private uint _numPosStates;
  17. public void Create(uint numPosStates)
  18. {
  19. for (uint posState = _numPosStates; posState < numPosStates; posState++)
  20. {
  21. _lowCoder[posState] = new BitTreeDecoder(Base.K_NUM_LOW_LEN_BITS);
  22. _midCoder[posState] = new BitTreeDecoder(Base.K_NUM_MID_LEN_BITS);
  23. }
  24. _numPosStates = numPosStates;
  25. }
  26. public void Init()
  27. {
  28. _choice.Init();
  29. for (uint posState = 0; posState < _numPosStates; posState++)
  30. {
  31. _lowCoder[posState].Init();
  32. _midCoder[posState].Init();
  33. }
  34. _choice2.Init();
  35. _highCoder.Init();
  36. }
  37. public uint Decode(RangeCoder.Decoder rangeDecoder, uint posState)
  38. {
  39. if (_choice.Decode(rangeDecoder) == 0)
  40. {
  41. return _lowCoder[posState].Decode(rangeDecoder);
  42. }
  43. uint symbol = Base.K_NUM_LOW_LEN_SYMBOLS;
  44. if (_choice2.Decode(rangeDecoder) == 0)
  45. {
  46. symbol += _midCoder[posState].Decode(rangeDecoder);
  47. }
  48. else
  49. {
  50. symbol += Base.K_NUM_MID_LEN_SYMBOLS;
  51. symbol += _highCoder.Decode(rangeDecoder);
  52. }
  53. return symbol;
  54. }
  55. }
  56. private class LiteralDecoder
  57. {
  58. private struct Decoder2
  59. {
  60. private BitDecoder[] _decoders;
  61. public void Create()
  62. {
  63. _decoders = new BitDecoder[0x300];
  64. }
  65. public void Init()
  66. {
  67. for (int i = 0; i < 0x300; i++)
  68. {
  69. _decoders[i].Init();
  70. }
  71. }
  72. public byte DecodeNormal(RangeCoder.Decoder rangeDecoder)
  73. {
  74. uint symbol = 1;
  75. do
  76. {
  77. symbol = (symbol << 1) | _decoders[symbol].Decode(rangeDecoder);
  78. }
  79. while (symbol < 0x100);
  80. return (byte)symbol;
  81. }
  82. public byte DecodeWithMatchByte(RangeCoder.Decoder rangeDecoder, byte matchByte)
  83. {
  84. uint symbol = 1;
  85. do
  86. {
  87. uint matchBit = (uint)(matchByte >> 7) & 1;
  88. matchByte <<= 1;
  89. uint bit = _decoders[((1 + matchBit) << 8) + symbol].Decode(rangeDecoder);
  90. symbol = (symbol << 1) | bit;
  91. if (matchBit != bit)
  92. {
  93. while (symbol < 0x100)
  94. {
  95. symbol = (symbol << 1) | _decoders[symbol].Decode(rangeDecoder);
  96. }
  97. break;
  98. }
  99. }
  100. while (symbol < 0x100);
  101. return (byte)symbol;
  102. }
  103. }
  104. private Decoder2[] _coders;
  105. private int _numPrevBits;
  106. private int _numPosBits;
  107. private uint _posMask;
  108. public void Create(int numPosBits, int numPrevBits)
  109. {
  110. if (_coders != null && _numPrevBits == numPrevBits &&
  111. _numPosBits == numPosBits)
  112. {
  113. return;
  114. }
  115. _numPosBits = numPosBits;
  116. _posMask = ((uint)1 << numPosBits) - 1;
  117. _numPrevBits = numPrevBits;
  118. uint numStates = (uint)1 << (_numPrevBits + _numPosBits);
  119. _coders = new Decoder2[numStates];
  120. for (uint i = 0; i < numStates; i++)
  121. {
  122. _coders[i].Create();
  123. }
  124. }
  125. public void Init()
  126. {
  127. uint numStates = (uint)1 << (_numPrevBits + _numPosBits);
  128. for (uint i = 0; i < numStates; i++)
  129. {
  130. _coders[i].Init();
  131. }
  132. }
  133. private uint GetState(uint pos, byte prevByte)
  134. {
  135. return ((pos & _posMask) << _numPrevBits) + (uint)(prevByte >> (8 - _numPrevBits));
  136. }
  137. public byte DecodeNormal(RangeCoder.Decoder rangeDecoder, uint pos, byte prevByte)
  138. {
  139. return _coders[GetState(pos, prevByte)].DecodeNormal(rangeDecoder);
  140. }
  141. public byte DecodeWithMatchByte(RangeCoder.Decoder rangeDecoder, uint pos, byte prevByte, byte matchByte)
  142. {
  143. return _coders[GetState(pos, prevByte)].DecodeWithMatchByte(rangeDecoder, matchByte);
  144. }
  145. }
  146. private OutWindow _outWindow;
  147. private readonly BitDecoder[] _isMatchDecoders = new BitDecoder[Base.K_NUM_STATES << Base.K_NUM_POS_STATES_BITS_MAX];
  148. private readonly BitDecoder[] _isRepDecoders = new BitDecoder[Base.K_NUM_STATES];
  149. private readonly BitDecoder[] _isRepG0Decoders = new BitDecoder[Base.K_NUM_STATES];
  150. private readonly BitDecoder[] _isRepG1Decoders = new BitDecoder[Base.K_NUM_STATES];
  151. private readonly BitDecoder[] _isRepG2Decoders = new BitDecoder[Base.K_NUM_STATES];
  152. private readonly BitDecoder[] _isRep0LongDecoders = new BitDecoder[Base.K_NUM_STATES << Base.K_NUM_POS_STATES_BITS_MAX];
  153. private readonly BitTreeDecoder[] _posSlotDecoder = new BitTreeDecoder[Base.K_NUM_LEN_TO_POS_STATES];
  154. private readonly BitDecoder[] _posDecoders = new BitDecoder[Base.K_NUM_FULL_DISTANCES - Base.K_END_POS_MODEL_INDEX];
  155. private BitTreeDecoder _posAlignDecoder = new BitTreeDecoder(Base.K_NUM_ALIGN_BITS);
  156. private readonly LenDecoder _lenDecoder = new LenDecoder();
  157. private readonly LenDecoder _repLenDecoder = new LenDecoder();
  158. private readonly LiteralDecoder _literalDecoder = new LiteralDecoder();
  159. private int _dictionarySize;
  160. private uint _posStateMask;
  161. private Base.State _state = new Base.State();
  162. private uint _rep0, _rep1, _rep2, _rep3;
  163. public Decoder()
  164. {
  165. _dictionarySize = -1;
  166. for (int i = 0; i < Base.K_NUM_LEN_TO_POS_STATES; i++)
  167. {
  168. _posSlotDecoder[i] = new BitTreeDecoder(Base.K_NUM_POS_SLOT_BITS);
  169. }
  170. }
  171. private void CreateDictionary()
  172. {
  173. if (_dictionarySize < 0)
  174. {
  175. throw new InvalidParamException();
  176. }
  177. _outWindow = new OutWindow();
  178. int blockSize = Math.Max(_dictionarySize, (1 << 12));
  179. _outWindow.Create(blockSize);
  180. }
  181. private void SetLiteralProperties(int lp, int lc)
  182. {
  183. if (lp > 8)
  184. {
  185. throw new InvalidParamException();
  186. }
  187. if (lc > 8)
  188. {
  189. throw new InvalidParamException();
  190. }
  191. _literalDecoder.Create(lp, lc);
  192. }
  193. private void SetPosBitsProperties(int pb)
  194. {
  195. if (pb > Base.K_NUM_POS_STATES_BITS_MAX)
  196. {
  197. throw new InvalidParamException();
  198. }
  199. uint numPosStates = (uint)1 << pb;
  200. _lenDecoder.Create(numPosStates);
  201. _repLenDecoder.Create(numPosStates);
  202. _posStateMask = numPosStates - 1;
  203. }
  204. private void Init()
  205. {
  206. uint i;
  207. for (i = 0; i < Base.K_NUM_STATES; i++)
  208. {
  209. for (uint j = 0; j <= _posStateMask; j++)
  210. {
  211. uint index = (i << Base.K_NUM_POS_STATES_BITS_MAX) + j;
  212. _isMatchDecoders[index].Init();
  213. _isRep0LongDecoders[index].Init();
  214. }
  215. _isRepDecoders[i].Init();
  216. _isRepG0Decoders[i].Init();
  217. _isRepG1Decoders[i].Init();
  218. _isRepG2Decoders[i].Init();
  219. }
  220. _literalDecoder.Init();
  221. for (i = 0; i < Base.K_NUM_LEN_TO_POS_STATES; i++)
  222. {
  223. _posSlotDecoder[i].Init();
  224. }
  225. // _PosSpecDecoder.Init();
  226. for (i = 0; i < Base.K_NUM_FULL_DISTANCES - Base.K_END_POS_MODEL_INDEX; i++)
  227. {
  228. _posDecoders[i].Init();
  229. }
  230. _lenDecoder.Init();
  231. _repLenDecoder.Init();
  232. _posAlignDecoder.Init();
  233. _state.Init();
  234. _rep0 = 0;
  235. _rep1 = 0;
  236. _rep2 = 0;
  237. _rep3 = 0;
  238. }
  239. public void Code(Stream inStream, Stream outStream,
  240. Int64 inSize, Int64 outSize, ICodeProgress progress)
  241. {
  242. if (_outWindow == null)
  243. {
  244. CreateDictionary();
  245. }
  246. _outWindow.Init(outStream);
  247. if (outSize > 0)
  248. {
  249. _outWindow.SetLimit(outSize);
  250. }
  251. else
  252. {
  253. _outWindow.SetLimit(Int64.MaxValue - _outWindow._total);
  254. }
  255. RangeCoder.Decoder rangeDecoder = new RangeCoder.Decoder();
  256. rangeDecoder.Init(inStream);
  257. Code(_dictionarySize, _outWindow, rangeDecoder);
  258. _outWindow.ReleaseStream();
  259. rangeDecoder.ReleaseStream();
  260. if (!rangeDecoder.IsFinished || (inSize > 0 && rangeDecoder._total != inSize))
  261. {
  262. throw new DataErrorException();
  263. }
  264. if (_outWindow.HasPending)
  265. {
  266. throw new DataErrorException();
  267. }
  268. _outWindow = null;
  269. }
  270. internal bool Code(int dictionarySize, OutWindow outWindow, RangeCoder.Decoder rangeDecoder)
  271. {
  272. int dictionarySizeCheck = Math.Max(dictionarySize, 1);
  273. outWindow.CopyPending();
  274. while (outWindow.HasSpace)
  275. {
  276. uint posState = (uint)outWindow._total & _posStateMask;
  277. if (_isMatchDecoders[(_state._index << Base.K_NUM_POS_STATES_BITS_MAX) + posState].Decode(rangeDecoder) == 0)
  278. {
  279. byte b;
  280. byte prevByte = outWindow.GetByte(0);
  281. if (!_state.IsCharState())
  282. {
  283. b = _literalDecoder.DecodeWithMatchByte(rangeDecoder,
  284. (uint)outWindow._total, prevByte,
  285. outWindow.GetByte((int)_rep0));
  286. }
  287. else
  288. {
  289. b = _literalDecoder.DecodeNormal(rangeDecoder, (uint)outWindow._total, prevByte);
  290. }
  291. outWindow.PutByte(b);
  292. _state.UpdateChar();
  293. }
  294. else
  295. {
  296. uint len;
  297. if (_isRepDecoders[_state._index].Decode(rangeDecoder) == 1)
  298. {
  299. if (_isRepG0Decoders[_state._index].Decode(rangeDecoder) == 0)
  300. {
  301. if (
  302. _isRep0LongDecoders[(_state._index << Base.K_NUM_POS_STATES_BITS_MAX) + posState].Decode(
  303. rangeDecoder) == 0)
  304. {
  305. _state.UpdateShortRep();
  306. outWindow.PutByte(outWindow.GetByte((int)_rep0));
  307. continue;
  308. }
  309. }
  310. else
  311. {
  312. UInt32 distance;
  313. if (_isRepG1Decoders[_state._index].Decode(rangeDecoder) == 0)
  314. {
  315. distance = _rep1;
  316. }
  317. else
  318. {
  319. if (_isRepG2Decoders[_state._index].Decode(rangeDecoder) == 0)
  320. {
  321. distance = _rep2;
  322. }
  323. else
  324. {
  325. distance = _rep3;
  326. _rep3 = _rep2;
  327. }
  328. _rep2 = _rep1;
  329. }
  330. _rep1 = _rep0;
  331. _rep0 = distance;
  332. }
  333. len = _repLenDecoder.Decode(rangeDecoder, posState) + Base.K_MATCH_MIN_LEN;
  334. _state.UpdateRep();
  335. }
  336. else
  337. {
  338. _rep3 = _rep2;
  339. _rep2 = _rep1;
  340. _rep1 = _rep0;
  341. len = Base.K_MATCH_MIN_LEN + _lenDecoder.Decode(rangeDecoder, posState);
  342. _state.UpdateMatch();
  343. uint posSlot = _posSlotDecoder[Base.GetLenToPosState(len)].Decode(rangeDecoder);
  344. if (posSlot >= Base.K_START_POS_MODEL_INDEX)
  345. {
  346. int numDirectBits = (int)((posSlot >> 1) - 1);
  347. _rep0 = ((2 | (posSlot & 1)) << numDirectBits);
  348. if (posSlot < Base.K_END_POS_MODEL_INDEX)
  349. {
  350. _rep0 += BitTreeDecoder.ReverseDecode(_posDecoders,
  351. _rep0 - posSlot - 1, rangeDecoder, numDirectBits);
  352. }
  353. else
  354. {
  355. _rep0 += (rangeDecoder.DecodeDirectBits(
  356. numDirectBits - Base.K_NUM_ALIGN_BITS) << Base.K_NUM_ALIGN_BITS);
  357. _rep0 += _posAlignDecoder.ReverseDecode(rangeDecoder);
  358. }
  359. }
  360. else
  361. {
  362. _rep0 = posSlot;
  363. }
  364. }
  365. if (_rep0 >= outWindow._total || _rep0 >= dictionarySizeCheck)
  366. {
  367. if (_rep0 == 0xFFFFFFFF)
  368. {
  369. return true;
  370. }
  371. throw new DataErrorException();
  372. }
  373. outWindow.CopyBlock((int)_rep0, (int)len);
  374. }
  375. }
  376. return false;
  377. }
  378. public void SetDecoderProperties(byte[] properties)
  379. {
  380. if (properties.Length < 1)
  381. {
  382. throw new InvalidParamException();
  383. }
  384. int lc = properties[0] % 9;
  385. int remainder = properties[0] / 9;
  386. int lp = remainder % 5;
  387. int pb = remainder / 5;
  388. if (pb > Base.K_NUM_POS_STATES_BITS_MAX)
  389. {
  390. throw new InvalidParamException();
  391. }
  392. SetLiteralProperties(lp, lc);
  393. SetPosBitsProperties(pb);
  394. Init();
  395. if (properties.Length >= 5)
  396. {
  397. _dictionarySize = 0;
  398. for (int i = 0; i < 4; i++)
  399. {
  400. _dictionarySize += properties[1 + i] << (i * 8);
  401. }
  402. }
  403. }
  404. public void Train(Stream stream)
  405. {
  406. if (_outWindow == null)
  407. {
  408. CreateDictionary();
  409. }
  410. _outWindow.Train(stream);
  411. }
  412. /*
  413. public override bool CanRead { get { return true; }}
  414. public override bool CanWrite { get { return true; }}
  415. public override bool CanSeek { get { return true; }}
  416. public override long Length { get { return 0; }}
  417. public override long Position
  418. {
  419. get { return 0; }
  420. set { }
  421. }
  422. public override void Flush() { }
  423. public override int Read(byte[] buffer, int offset, int count)
  424. {
  425. return 0;
  426. }
  427. public override void Write(byte[] buffer, int offset, int count)
  428. {
  429. }
  430. public override long Seek(long offset, System.IO.SeekOrigin origin)
  431. {
  432. return 0;
  433. }
  434. public override void SetLength(long value) {}
  435. */
  436. }
  437. }