LzmaStream.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using System.IO;
  3. using SharpCompress.Compressors.LZMA.LZ;
  4. using SharpCompress.Converters;
  5. namespace SharpCompress.Compressors.LZMA
  6. {
  7. public class LzmaStream : Stream
  8. {
  9. private readonly Stream _inputStream;
  10. private readonly long _inputSize;
  11. private readonly long _outputSize;
  12. private readonly int _dictionarySize;
  13. private readonly OutWindow _outWindow = new OutWindow();
  14. private readonly RangeCoder.Decoder _rangeDecoder = new RangeCoder.Decoder();
  15. private Decoder _decoder;
  16. private long _position;
  17. private bool _endReached;
  18. private long _availableBytes;
  19. private long _rangeDecoderLimit;
  20. private long _inputPosition;
  21. // LZMA2
  22. private readonly bool _isLzma2;
  23. private bool _uncompressedChunk;
  24. private bool _needDictReset = true;
  25. private bool _needProps = true;
  26. private readonly Encoder _encoder;
  27. private bool _isDisposed;
  28. public LzmaStream(byte[] properties, Stream inputStream)
  29. : this(properties, inputStream, -1, -1, null, properties.Length < 5)
  30. {
  31. }
  32. public LzmaStream(byte[] properties, Stream inputStream, long inputSize)
  33. : this(properties, inputStream, inputSize, -1, null, properties.Length < 5)
  34. {
  35. }
  36. public LzmaStream(byte[] properties, Stream inputStream, long inputSize, long outputSize)
  37. : this(properties, inputStream, inputSize, outputSize, null, properties.Length < 5)
  38. {
  39. }
  40. public LzmaStream(byte[] properties, Stream inputStream, long inputSize, long outputSize,
  41. Stream presetDictionary, bool isLzma2)
  42. {
  43. _inputStream = inputStream;
  44. _inputSize = inputSize;
  45. _outputSize = outputSize;
  46. _isLzma2 = isLzma2;
  47. if (!isLzma2)
  48. {
  49. _dictionarySize = DataConverter.LittleEndian.GetInt32(properties, 1);
  50. _outWindow.Create(_dictionarySize);
  51. if (presetDictionary != null)
  52. {
  53. _outWindow.Train(presetDictionary);
  54. }
  55. _rangeDecoder.Init(inputStream);
  56. _decoder = new Decoder();
  57. _decoder.SetDecoderProperties(properties);
  58. Properties = properties;
  59. _availableBytes = outputSize < 0 ? long.MaxValue : outputSize;
  60. _rangeDecoderLimit = inputSize;
  61. }
  62. else
  63. {
  64. _dictionarySize = 2 | (properties[0] & 1);
  65. _dictionarySize <<= (properties[0] >> 1) + 11;
  66. _outWindow.Create(_dictionarySize);
  67. if (presetDictionary != null)
  68. {
  69. _outWindow.Train(presetDictionary);
  70. _needDictReset = false;
  71. }
  72. Properties = new byte[1];
  73. _availableBytes = 0;
  74. }
  75. }
  76. public LzmaStream(LzmaEncoderProperties properties, bool isLzma2, Stream outputStream)
  77. : this(properties, isLzma2, null, outputStream)
  78. {
  79. }
  80. public LzmaStream(LzmaEncoderProperties properties, bool isLzma2, Stream presetDictionary, Stream outputStream)
  81. {
  82. _isLzma2 = isLzma2;
  83. _availableBytes = 0;
  84. _endReached = true;
  85. if (isLzma2)
  86. {
  87. throw new NotImplementedException();
  88. }
  89. _encoder = new Encoder();
  90. _encoder.SetCoderProperties(properties._propIDs, properties._properties);
  91. MemoryStream propStream = new MemoryStream(5);
  92. _encoder.WriteCoderProperties(propStream);
  93. Properties = propStream.ToArray();
  94. _encoder.SetStreams(null, outputStream, -1, -1);
  95. if (presetDictionary != null)
  96. {
  97. _encoder.Train(presetDictionary);
  98. }
  99. }
  100. public override bool CanRead => _encoder == null;
  101. public override bool CanSeek => false;
  102. public override bool CanWrite => _encoder != null;
  103. public override void Flush()
  104. {
  105. }
  106. protected override void Dispose(bool disposing)
  107. {
  108. if (_isDisposed)
  109. {
  110. return;
  111. }
  112. _isDisposed = true;
  113. if (disposing)
  114. {
  115. if (_encoder != null)
  116. {
  117. _position = _encoder.Code(null, true);
  118. }
  119. _inputStream?.Dispose();
  120. }
  121. base.Dispose(disposing);
  122. }
  123. public override long Length => _position + _availableBytes;
  124. public override long Position { get => _position; set => throw new NotSupportedException(); }
  125. public override int Read(byte[] buffer, int offset, int count)
  126. {
  127. if (_endReached)
  128. {
  129. return 0;
  130. }
  131. int total = 0;
  132. while (total < count)
  133. {
  134. if (_availableBytes == 0)
  135. {
  136. if (_isLzma2)
  137. {
  138. DecodeChunkHeader();
  139. }
  140. else
  141. {
  142. _endReached = true;
  143. }
  144. if (_endReached)
  145. {
  146. break;
  147. }
  148. }
  149. int toProcess = count - total;
  150. if (toProcess > _availableBytes)
  151. {
  152. toProcess = (int)_availableBytes;
  153. }
  154. _outWindow.SetLimit(toProcess);
  155. if (_uncompressedChunk)
  156. {
  157. _inputPosition += _outWindow.CopyStream(_inputStream, toProcess);
  158. }
  159. else if (_decoder.Code(_dictionarySize, _outWindow, _rangeDecoder)
  160. && _outputSize < 0)
  161. {
  162. _availableBytes = _outWindow.AvailableBytes;
  163. }
  164. int read = _outWindow.Read(buffer, offset, toProcess);
  165. total += read;
  166. offset += read;
  167. _position += read;
  168. _availableBytes -= read;
  169. if (_availableBytes == 0 && !_uncompressedChunk)
  170. {
  171. _rangeDecoder.ReleaseStream();
  172. if (!_rangeDecoder.IsFinished || (_rangeDecoderLimit >= 0 && _rangeDecoder._total != _rangeDecoderLimit))
  173. {
  174. throw new DataErrorException();
  175. }
  176. _inputPosition += _rangeDecoder._total;
  177. if (_outWindow.HasPending)
  178. {
  179. throw new DataErrorException();
  180. }
  181. }
  182. }
  183. if (_endReached)
  184. {
  185. if (_inputSize >= 0 && _inputPosition != _inputSize)
  186. {
  187. throw new DataErrorException();
  188. }
  189. if (_outputSize >= 0 && _position != _outputSize)
  190. {
  191. throw new DataErrorException();
  192. }
  193. }
  194. return total;
  195. }
  196. private void DecodeChunkHeader()
  197. {
  198. int control = _inputStream.ReadByte();
  199. _inputPosition++;
  200. if (control == 0x00)
  201. {
  202. _endReached = true;
  203. return;
  204. }
  205. if (control >= 0xE0 || control == 0x01)
  206. {
  207. _needProps = true;
  208. _needDictReset = false;
  209. _outWindow.Reset();
  210. }
  211. else if (_needDictReset)
  212. {
  213. throw new DataErrorException();
  214. }
  215. if (control >= 0x80)
  216. {
  217. _uncompressedChunk = false;
  218. _availableBytes = (control & 0x1F) << 16;
  219. _availableBytes += (_inputStream.ReadByte() << 8) + _inputStream.ReadByte() + 1;
  220. _inputPosition += 2;
  221. _rangeDecoderLimit = (_inputStream.ReadByte() << 8) + _inputStream.ReadByte() + 1;
  222. _inputPosition += 2;
  223. if (control >= 0xC0)
  224. {
  225. _needProps = false;
  226. Properties[0] = (byte)_inputStream.ReadByte();
  227. _inputPosition++;
  228. _decoder = new Decoder();
  229. _decoder.SetDecoderProperties(Properties);
  230. }
  231. else if (_needProps)
  232. {
  233. throw new DataErrorException();
  234. }
  235. else if (control >= 0xA0)
  236. {
  237. _decoder = new Decoder();
  238. _decoder.SetDecoderProperties(Properties);
  239. }
  240. _rangeDecoder.Init(_inputStream);
  241. }
  242. else if (control > 0x02)
  243. {
  244. throw new DataErrorException();
  245. }
  246. else
  247. {
  248. _uncompressedChunk = true;
  249. _availableBytes = (_inputStream.ReadByte() << 8) + _inputStream.ReadByte() + 1;
  250. _inputPosition += 2;
  251. }
  252. }
  253. public override long Seek(long offset, SeekOrigin origin)
  254. {
  255. throw new NotSupportedException();
  256. }
  257. public override void SetLength(long value)
  258. {
  259. throw new NotSupportedException();
  260. }
  261. public override void Write(byte[] buffer, int offset, int count)
  262. {
  263. if (_encoder != null)
  264. {
  265. _position = _encoder.Code(new MemoryStream(buffer, offset, count), false);
  266. }
  267. }
  268. public byte[] Properties { get; } = new byte[5];
  269. }
  270. }