LzmaDecoder.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. using SevenZip.Sdk.Compression.LZ;
  16. using SevenZip.Sdk.Compression.RangeCoder;
  17. namespace SevenZip.Sdk.Compression.Lzma
  18. {
  19. /// <summary>
  20. /// The LZMA decoder class
  21. /// </summary>
  22. public class Decoder : ICoder, ISetDecoderProperties // ,System.IO.Stream
  23. {
  24. private readonly BitDecoder[] m_IsMatchDecoders = new BitDecoder[Base.kNumStates << Base.kNumPosStatesBitsMax];
  25. private readonly BitDecoder[] m_IsRep0LongDecoders =
  26. new BitDecoder[Base.kNumStates << Base.kNumPosStatesBitsMax];
  27. private readonly BitDecoder[] m_IsRepDecoders = new BitDecoder[Base.kNumStates];
  28. private readonly BitDecoder[] m_IsRepG0Decoders = new BitDecoder[Base.kNumStates];
  29. private readonly BitDecoder[] m_IsRepG1Decoders = new BitDecoder[Base.kNumStates];
  30. private readonly BitDecoder[] m_IsRepG2Decoders = new BitDecoder[Base.kNumStates];
  31. private readonly LenDecoder m_LenDecoder = new LenDecoder();
  32. private readonly LiteralDecoder m_LiteralDecoder = new LiteralDecoder();
  33. private readonly OutWindow m_OutWindow = new OutWindow();
  34. private readonly BitDecoder[] m_PosDecoders = new BitDecoder[Base.kNumFullDistances - Base.kEndPosModelIndex];
  35. private readonly BitTreeDecoder[] m_PosSlotDecoder = new BitTreeDecoder[Base.kNumLenToPosStates];
  36. private readonly RangeCoder.Decoder m_RangeDecoder = new RangeCoder.Decoder();
  37. private readonly LenDecoder m_RepLenDecoder = new LenDecoder();
  38. private bool _solid;
  39. private uint m_DictionarySize;
  40. private uint m_DictionarySizeCheck;
  41. private BitTreeDecoder m_PosAlignDecoder = new BitTreeDecoder(Base.kNumAlignBits);
  42. private uint m_PosStateMask;
  43. /// <summary>
  44. /// Initializes the Lzma Decoder class.
  45. /// </summary>
  46. public Decoder()
  47. {
  48. m_DictionarySize = 0xFFFFFFFF;
  49. for (int i = 0; i < Base.kNumLenToPosStates; i++)
  50. m_PosSlotDecoder[i] = new BitTreeDecoder(Base.kNumPosSlotBits);
  51. }
  52. #region ICoder Members
  53. /// <summary>
  54. /// Codes a stream with LZMA algorithm to an output stream
  55. /// </summary>
  56. /// <param name="inStream">The input stream</param>
  57. /// <param name="inSize">The input size</param>
  58. /// <param name="outSize">The output size</param>
  59. /// <param name="outStream">The output stream</param>
  60. /// <param name="progress">Progress interface</param>
  61. public void Code(Stream inStream, Stream outStream,
  62. Int64 inSize, Int64 outSize, ICodeProgress progress)
  63. {
  64. Init(inStream, outStream);
  65. var state = new Base.State();
  66. state.Init();
  67. uint rep0 = 0, rep1 = 0, rep2 = 0, rep3 = 0;
  68. UInt64 nowPos64 = 0;
  69. var outSize64 = (UInt64) outSize;
  70. if (nowPos64 < outSize64)
  71. {
  72. if (m_IsMatchDecoders[state.Index << Base.kNumPosStatesBitsMax].Decode(m_RangeDecoder) != 0)
  73. throw new DataErrorException();
  74. state.UpdateChar();
  75. byte b = m_LiteralDecoder.DecodeNormal(m_RangeDecoder, 0, 0);
  76. m_OutWindow.PutByte(b);
  77. nowPos64++;
  78. }
  79. while (nowPos64 < outSize64)
  80. {
  81. // UInt64 next = Math.Min(nowPos64 + (1 << 18), outSize64);
  82. // while(nowPos64 < next)
  83. {
  84. uint posState = (uint) nowPos64 & m_PosStateMask;
  85. if (
  86. m_IsMatchDecoders[(state.Index << Base.kNumPosStatesBitsMax) + posState].Decode(m_RangeDecoder) ==
  87. 0)
  88. {
  89. byte b;
  90. byte prevByte = m_OutWindow.GetByte(0);
  91. if (!state.IsCharState())
  92. b = m_LiteralDecoder.DecodeWithMatchByte(m_RangeDecoder,
  93. (uint) nowPos64, prevByte,
  94. m_OutWindow.GetByte(rep0));
  95. else
  96. b = m_LiteralDecoder.DecodeNormal(m_RangeDecoder, (uint) nowPos64, prevByte);
  97. m_OutWindow.PutByte(b);
  98. state.UpdateChar();
  99. nowPos64++;
  100. }
  101. else
  102. {
  103. uint len;
  104. if (m_IsRepDecoders[state.Index].Decode(m_RangeDecoder) == 1)
  105. {
  106. if (m_IsRepG0Decoders[state.Index].Decode(m_RangeDecoder) == 0)
  107. {
  108. if (
  109. m_IsRep0LongDecoders[(state.Index << Base.kNumPosStatesBitsMax) + posState].Decode(
  110. m_RangeDecoder) == 0)
  111. {
  112. state.UpdateShortRep();
  113. m_OutWindow.PutByte(m_OutWindow.GetByte(rep0));
  114. nowPos64++;
  115. continue;
  116. }
  117. }
  118. else
  119. {
  120. UInt32 distance;
  121. if (m_IsRepG1Decoders[state.Index].Decode(m_RangeDecoder) == 0)
  122. {
  123. distance = rep1;
  124. }
  125. else
  126. {
  127. if (m_IsRepG2Decoders[state.Index].Decode(m_RangeDecoder) == 0)
  128. distance = rep2;
  129. else
  130. {
  131. distance = rep3;
  132. rep3 = rep2;
  133. }
  134. rep2 = rep1;
  135. }
  136. rep1 = rep0;
  137. rep0 = distance;
  138. }
  139. len = m_RepLenDecoder.Decode(m_RangeDecoder, posState) + Base.kMatchMinLen;
  140. state.UpdateRep();
  141. }
  142. else
  143. {
  144. rep3 = rep2;
  145. rep2 = rep1;
  146. rep1 = rep0;
  147. len = Base.kMatchMinLen + m_LenDecoder.Decode(m_RangeDecoder, posState);
  148. state.UpdateMatch();
  149. uint posSlot = m_PosSlotDecoder[Base.GetLenToPosState(len)].Decode(m_RangeDecoder);
  150. if (posSlot >= Base.kStartPosModelIndex)
  151. {
  152. var numDirectBits = (int) ((posSlot >> 1) - 1);
  153. rep0 = ((2 | (posSlot & 1)) << numDirectBits);
  154. if (posSlot < Base.kEndPosModelIndex)
  155. rep0 += BitTreeDecoder.ReverseDecode(m_PosDecoders,
  156. rep0 - posSlot - 1, m_RangeDecoder,
  157. numDirectBits);
  158. else
  159. {
  160. rep0 += (m_RangeDecoder.DecodeDirectBits(
  161. numDirectBits - Base.kNumAlignBits) << Base.kNumAlignBits);
  162. rep0 += m_PosAlignDecoder.ReverseDecode(m_RangeDecoder);
  163. }
  164. }
  165. else
  166. rep0 = posSlot;
  167. }
  168. if (rep0 >= m_OutWindow.TrainSize + nowPos64 || rep0 >= m_DictionarySizeCheck)
  169. {
  170. if (rep0 == 0xFFFFFFFF)
  171. break;
  172. throw new DataErrorException();
  173. }
  174. m_OutWindow.CopyBlock(rep0, len);
  175. nowPos64 += len;
  176. }
  177. }
  178. }
  179. m_OutWindow.Flush();
  180. m_OutWindow.ReleaseStream();
  181. m_RangeDecoder.ReleaseStream();
  182. }
  183. #endregion
  184. #region ISetDecoderProperties Members
  185. /// <summary>
  186. /// Sets decoder properties
  187. /// </summary>
  188. /// <param name="properties">Array of byte properties</param>
  189. public void SetDecoderProperties(byte[] properties)
  190. {
  191. if (properties.Length < 5)
  192. throw new InvalidParamException();
  193. int lc = properties[0]%9;
  194. int remainder = properties[0]/9;
  195. int lp = remainder%5;
  196. int pb = remainder/5;
  197. if (pb > Base.kNumPosStatesBitsMax)
  198. throw new InvalidParamException();
  199. UInt32 dictionarySize = 0;
  200. for (int i = 0; i < 4; i++)
  201. dictionarySize += ((UInt32) (properties[1 + i])) << (i*8);
  202. SetDictionarySize(dictionarySize);
  203. SetLiteralProperties(lp, lc);
  204. SetPosBitsProperties(pb);
  205. }
  206. #endregion
  207. private void SetDictionarySize(uint dictionarySize)
  208. {
  209. if (m_DictionarySize != dictionarySize)
  210. {
  211. m_DictionarySize = dictionarySize;
  212. m_DictionarySizeCheck = Math.Max(m_DictionarySize, 1);
  213. uint blockSize = Math.Max(m_DictionarySizeCheck, (1 << 12));
  214. m_OutWindow.Create(blockSize);
  215. }
  216. }
  217. private void SetLiteralProperties(int lp, int lc)
  218. {
  219. if (lp > 8)
  220. throw new InvalidParamException();
  221. if (lc > 8)
  222. throw new InvalidParamException();
  223. m_LiteralDecoder.Create(lp, lc);
  224. }
  225. private void SetPosBitsProperties(int pb)
  226. {
  227. if (pb > Base.kNumPosStatesBitsMax)
  228. throw new InvalidParamException();
  229. uint numPosStates = (uint) 1 << pb;
  230. m_LenDecoder.Create(numPosStates);
  231. m_RepLenDecoder.Create(numPosStates);
  232. m_PosStateMask = numPosStates - 1;
  233. }
  234. private void Init(Stream inStream, Stream outStream)
  235. {
  236. m_RangeDecoder.Init(inStream);
  237. m_OutWindow.Init(outStream, _solid);
  238. uint i;
  239. for (i = 0; i < Base.kNumStates; i++)
  240. {
  241. for (uint j = 0; j <= m_PosStateMask; j++)
  242. {
  243. uint index = (i << Base.kNumPosStatesBitsMax) + j;
  244. m_IsMatchDecoders[index].Init();
  245. m_IsRep0LongDecoders[index].Init();
  246. }
  247. m_IsRepDecoders[i].Init();
  248. m_IsRepG0Decoders[i].Init();
  249. m_IsRepG1Decoders[i].Init();
  250. m_IsRepG2Decoders[i].Init();
  251. }
  252. m_LiteralDecoder.Init();
  253. for (i = 0; i < Base.kNumLenToPosStates; i++)
  254. m_PosSlotDecoder[i].Init();
  255. // m_PosSpecDecoder.Init();
  256. for (i = 0; i < Base.kNumFullDistances - Base.kEndPosModelIndex; i++)
  257. m_PosDecoders[i].Init();
  258. m_LenDecoder.Init();
  259. m_RepLenDecoder.Init();
  260. m_PosAlignDecoder.Init();
  261. }
  262. /// <summary>
  263. /// Trains a stream
  264. /// </summary>
  265. /// <param name="stream">The stream to train.</param>
  266. /// <returns>true if Ok; otherwise, false.</returns>
  267. public bool Train(Stream stream)
  268. {
  269. _solid = true;
  270. return m_OutWindow.Train(stream);
  271. }
  272. #region Nested type: LenDecoder
  273. private class LenDecoder
  274. {
  275. private readonly BitTreeDecoder[] m_LowCoder = new BitTreeDecoder[Base.kNumPosStatesMax];
  276. private readonly BitTreeDecoder[] m_MidCoder = new BitTreeDecoder[Base.kNumPosStatesMax];
  277. private BitDecoder m_Choice;
  278. private BitDecoder m_Choice2;
  279. private BitTreeDecoder m_HighCoder = new BitTreeDecoder(Base.kNumHighLenBits);
  280. private uint m_NumPosStates;
  281. internal void Create(uint numPosStates)
  282. {
  283. for (uint posState = m_NumPosStates; posState < numPosStates; posState++)
  284. {
  285. m_LowCoder[posState] = new BitTreeDecoder(Base.kNumLowLenBits);
  286. m_MidCoder[posState] = new BitTreeDecoder(Base.kNumMidLenBits);
  287. }
  288. m_NumPosStates = numPosStates;
  289. }
  290. internal void Init()
  291. {
  292. m_Choice.Init();
  293. for (uint posState = 0; posState < m_NumPosStates; posState++)
  294. {
  295. m_LowCoder[posState].Init();
  296. m_MidCoder[posState].Init();
  297. }
  298. m_Choice2.Init();
  299. m_HighCoder.Init();
  300. }
  301. /// <summary>
  302. /// Decodes the stream
  303. /// </summary>
  304. /// <param name="rangeDecoder">The specified RangeCoder</param>
  305. /// <param name="posState">The position state</param>
  306. /// <returns></returns>
  307. public uint Decode(RangeCoder.Decoder rangeDecoder, uint posState)
  308. {
  309. if (m_Choice.Decode(rangeDecoder) == 0)
  310. return m_LowCoder[posState].Decode(rangeDecoder);
  311. else
  312. {
  313. uint symbol = Base.kNumLowLenSymbols;
  314. if (m_Choice2.Decode(rangeDecoder) == 0)
  315. symbol += m_MidCoder[posState].Decode(rangeDecoder);
  316. else
  317. {
  318. symbol += Base.kNumMidLenSymbols;
  319. symbol += m_HighCoder.Decode(rangeDecoder);
  320. }
  321. return symbol;
  322. }
  323. }
  324. }
  325. #endregion
  326. #region Nested type: LiteralDecoder
  327. private class LiteralDecoder
  328. {
  329. private Decoder2[] m_Coders;
  330. private int m_NumPosBits;
  331. private int m_NumPrevBits;
  332. private uint m_PosMask;
  333. public void Create(int numPosBits, int numPrevBits)
  334. {
  335. if (m_Coders != null && m_NumPrevBits == numPrevBits &&
  336. m_NumPosBits == numPosBits)
  337. return;
  338. m_NumPosBits = numPosBits;
  339. m_PosMask = ((uint) 1 << numPosBits) - 1;
  340. m_NumPrevBits = numPrevBits;
  341. uint numStates = (uint) 1 << (m_NumPrevBits + m_NumPosBits);
  342. m_Coders = new Decoder2[numStates];
  343. for (uint i = 0; i < numStates; i++)
  344. m_Coders[i].Create();
  345. }
  346. public void Init()
  347. {
  348. uint numStates = (uint) 1 << (m_NumPrevBits + m_NumPosBits);
  349. for (uint i = 0; i < numStates; i++)
  350. m_Coders[i].Init();
  351. }
  352. private uint GetState(uint pos, byte prevByte)
  353. {
  354. return ((pos & m_PosMask) << m_NumPrevBits) + (uint) (prevByte >> (8 - m_NumPrevBits));
  355. }
  356. public byte DecodeNormal(RangeCoder.Decoder rangeDecoder, uint pos, byte prevByte)
  357. {
  358. return m_Coders[GetState(pos, prevByte)].DecodeNormal(rangeDecoder);
  359. }
  360. public byte DecodeWithMatchByte(RangeCoder.Decoder rangeDecoder, uint pos, byte prevByte, byte matchByte)
  361. {
  362. return m_Coders[GetState(pos, prevByte)].DecodeWithMatchByte(rangeDecoder, matchByte);
  363. }
  364. #region Nested type: Decoder2
  365. private struct Decoder2
  366. {
  367. private BitDecoder[] m_Decoders;
  368. public void Create()
  369. {
  370. m_Decoders = new BitDecoder[0x300];
  371. }
  372. public void Init()
  373. {
  374. for (int i = 0; i < 0x300; i++) m_Decoders[i].Init();
  375. }
  376. public byte DecodeNormal(RangeCoder.Decoder rangeDecoder)
  377. {
  378. uint symbol = 1;
  379. do
  380. symbol = (symbol << 1) | m_Decoders[symbol].Decode(rangeDecoder); while (symbol < 0x100);
  381. return (byte) symbol;
  382. }
  383. public byte DecodeWithMatchByte(RangeCoder.Decoder rangeDecoder, byte matchByte)
  384. {
  385. uint symbol = 1;
  386. do
  387. {
  388. uint matchBit = (uint) (matchByte >> 7) & 1;
  389. matchByte <<= 1;
  390. uint bit = m_Decoders[((1 + matchBit) << 8) + symbol].Decode(rangeDecoder);
  391. symbol = (symbol << 1) | bit;
  392. if (matchBit != bit)
  393. {
  394. while (symbol < 0x100)
  395. symbol = (symbol << 1) | m_Decoders[symbol].Decode(rangeDecoder);
  396. break;
  397. }
  398. } while (symbol < 0x100);
  399. return (byte) symbol;
  400. }
  401. }
  402. #endregion
  403. } ;
  404. #endregion
  405. /*
  406. public override bool CanRead { get { return true; }}
  407. public override bool CanWrite { get { return true; }}
  408. public override bool CanSeek { get { return true; }}
  409. public override long Length { get { return 0; }}
  410. public override long Position
  411. {
  412. get { return 0; }
  413. set { }
  414. }
  415. public override void Flush() { }
  416. public override int Read(byte[] buffer, int offset, int count)
  417. {
  418. return 0;
  419. }
  420. public override void Write(byte[] buffer, int offset, int count)
  421. {
  422. }
  423. public override long Seek(long offset, System.IO.SeekOrigin origin)
  424. {
  425. return 0;
  426. }
  427. public override void SetLength(long value) {}
  428. */
  429. }
  430. }