Deflate64Stream.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using SharpCompress.Common.Zip;
  5. using SharpCompress.Compressors.Deflate;
  6. using System;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Runtime.CompilerServices;
  10. namespace SharpCompress.Compressors.Deflate64
  11. {
  12. public sealed class Deflate64Stream : Stream
  13. {
  14. private const int DEFAULT_BUFFER_SIZE = 8192;
  15. private Stream _stream;
  16. private CompressionMode _mode;
  17. private InflaterManaged _inflater;
  18. private byte[] _buffer;
  19. public Deflate64Stream(Stream stream, CompressionMode mode)
  20. {
  21. if (stream == null)
  22. throw new ArgumentNullException(nameof(stream));
  23. if (mode != CompressionMode.Decompress)
  24. throw new NotImplementedException("Deflate64: this implementation only supports decompression");
  25. if (!stream.CanRead)
  26. throw new ArgumentException("Deflate64: input stream is not readable", nameof(stream));
  27. InitializeInflater(stream, ZipCompressionMethod.Deflate64);
  28. }
  29. /// <summary>
  30. /// Sets up this DeflateManagedStream to be used for Inflation/Decompression
  31. /// </summary>
  32. private void InitializeInflater(Stream stream, ZipCompressionMethod method = ZipCompressionMethod.Deflate)
  33. {
  34. Debug.Assert(stream != null);
  35. Debug.Assert(method == ZipCompressionMethod.Deflate || method == ZipCompressionMethod.Deflate64);
  36. if (!stream.CanRead)
  37. throw new ArgumentException("Deflate64: input stream is not readable", nameof(stream));
  38. _inflater = new InflaterManaged(method == ZipCompressionMethod.Deflate64);
  39. _stream = stream;
  40. _mode = CompressionMode.Decompress;
  41. _buffer = new byte[DEFAULT_BUFFER_SIZE];
  42. }
  43. public override bool CanRead
  44. {
  45. get
  46. {
  47. if (_stream == null)
  48. {
  49. return false;
  50. }
  51. return (_mode == CompressionMode.Decompress && _stream.CanRead);
  52. }
  53. }
  54. public override bool CanWrite
  55. {
  56. get
  57. {
  58. if (_stream == null)
  59. {
  60. return false;
  61. }
  62. return (_mode == CompressionMode.Compress && _stream.CanWrite);
  63. }
  64. }
  65. public override bool CanSeek => false;
  66. public override long Length
  67. {
  68. get { throw new NotSupportedException("Deflate64: not supported"); }
  69. }
  70. public override long Position
  71. {
  72. get { throw new NotSupportedException("Deflate64: not supported"); }
  73. set { throw new NotSupportedException("Deflate64: not supported"); }
  74. }
  75. public override void Flush()
  76. {
  77. EnsureNotDisposed();
  78. }
  79. public override long Seek(long offset, SeekOrigin origin)
  80. {
  81. throw new NotSupportedException("Deflate64: not supported");
  82. }
  83. public override void SetLength(long value)
  84. {
  85. throw new NotSupportedException("Deflate64: not supported");
  86. }
  87. public override int Read(byte[] array, int offset, int count)
  88. {
  89. EnsureDecompressionMode();
  90. ValidateParameters(array, offset, count);
  91. EnsureNotDisposed();
  92. int bytesRead;
  93. int currentOffset = offset;
  94. int remainingCount = count;
  95. while (true)
  96. {
  97. bytesRead = _inflater.Inflate(array, currentOffset, remainingCount);
  98. currentOffset += bytesRead;
  99. remainingCount -= bytesRead;
  100. if (remainingCount == 0)
  101. {
  102. break;
  103. }
  104. if (_inflater.Finished())
  105. {
  106. // if we finished decompressing, we can't have anything left in the outputwindow.
  107. Debug.Assert(_inflater.AvailableOutput == 0, "We should have copied all stuff out!");
  108. break;
  109. }
  110. int bytes = _stream.Read(_buffer, 0, _buffer.Length);
  111. if (bytes <= 0)
  112. {
  113. break;
  114. }
  115. else if (bytes > _buffer.Length)
  116. {
  117. // The stream is either malicious or poorly implemented and returned a number of
  118. // bytes larger than the buffer supplied to it.
  119. throw new InvalidDataException("Deflate64: invalid data");
  120. }
  121. _inflater.SetInput(_buffer, 0, bytes);
  122. }
  123. return count - remainingCount;
  124. }
  125. private void ValidateParameters(byte[] array, int offset, int count)
  126. {
  127. if (array == null)
  128. throw new ArgumentNullException(nameof(array));
  129. if (offset < 0)
  130. throw new ArgumentOutOfRangeException(nameof(offset));
  131. if (count < 0)
  132. throw new ArgumentOutOfRangeException(nameof(count));
  133. if (array.Length - offset < count)
  134. throw new ArgumentException("Deflate64: invalid offset/count combination");
  135. }
  136. private void EnsureNotDisposed()
  137. {
  138. if (_stream == null)
  139. ThrowStreamClosedException();
  140. }
  141. [MethodImpl(MethodImplOptions.NoInlining)]
  142. private static void ThrowStreamClosedException()
  143. {
  144. throw new ObjectDisposedException(null, "Deflate64: stream has been disposed");
  145. }
  146. private void EnsureDecompressionMode()
  147. {
  148. if (_mode != CompressionMode.Decompress)
  149. ThrowCannotReadFromDeflateManagedStreamException();
  150. }
  151. [MethodImpl(MethodImplOptions.NoInlining)]
  152. private static void ThrowCannotReadFromDeflateManagedStreamException()
  153. {
  154. throw new InvalidOperationException("Deflate64: cannot read from this stream");
  155. }
  156. private void EnsureCompressionMode()
  157. {
  158. if (_mode != CompressionMode.Compress)
  159. ThrowCannotWriteToDeflateManagedStreamException();
  160. }
  161. [MethodImpl(MethodImplOptions.NoInlining)]
  162. private static void ThrowCannotWriteToDeflateManagedStreamException()
  163. {
  164. throw new InvalidOperationException("Deflate64: cannot write to this stream");
  165. }
  166. public override void Write(byte[] array, int offset, int count)
  167. {
  168. ThrowCannotWriteToDeflateManagedStreamException();
  169. }
  170. // This is called by Dispose:
  171. private void PurgeBuffers(bool disposing)
  172. {
  173. if (!disposing)
  174. return;
  175. if (_stream == null)
  176. return;
  177. Flush();
  178. }
  179. protected override void Dispose(bool disposing)
  180. {
  181. try
  182. {
  183. PurgeBuffers(disposing);
  184. }
  185. finally
  186. {
  187. // Close the underlying stream even if PurgeBuffers threw.
  188. // Stream.Close() may throw here (may or may not be due to the same error).
  189. // In this case, we still need to clean up internal resources, hence the inner finally blocks.
  190. try
  191. {
  192. if (disposing)
  193. {
  194. _stream?.Dispose();
  195. }
  196. }
  197. finally
  198. {
  199. _stream = null;
  200. try
  201. {
  202. _inflater?.Dispose();
  203. }
  204. finally
  205. {
  206. _inflater = null;
  207. base.Dispose(disposing);
  208. }
  209. }
  210. }
  211. }
  212. }
  213. }