| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | |
| | | 4 | | using Renci.SshNet.Security; |
| | | 5 | | |
| | | 6 | | namespace Renci.SshNet.Compression |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents base class for compression algorithm implementation. |
| | | 10 | | /// </summary> |
| | | 11 | | public abstract class Compressor : Algorithm, IDisposable |
| | | 12 | | { |
| | | 13 | | private readonly ZlibStream _compressor; |
| | | 14 | | private readonly ZlibStream _decompressor; |
| | | 15 | | private MemoryStream _compressorStream; |
| | | 16 | | private MemoryStream _decompressorStream; |
| | | 17 | | private bool _isDisposed; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets or sets a value indicating whether compression is active. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <value> |
| | | 23 | | /// <see langword="true"/> if compression is active; otherwise, <see langword="false"/>. |
| | | 24 | | /// </value> |
| | 0 | 25 | | protected bool IsActive { get; set; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the session. |
| | | 29 | | /// </summary> |
| | 0 | 30 | | protected Session Session { get; private set; } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="Compressor"/> class. |
| | | 34 | | /// </summary> |
| | 0 | 35 | | protected Compressor() |
| | 0 | 36 | | { |
| | 0 | 37 | | _compressorStream = new MemoryStream(); |
| | 0 | 38 | | _decompressorStream = new MemoryStream(); |
| | | 39 | | |
| | 0 | 40 | | _compressor = new ZlibStream(_compressorStream, CompressionMode.Compress); |
| | 0 | 41 | | _decompressor = new ZlibStream(_decompressorStream, CompressionMode.Decompress); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes the algorithm. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="session">The session.</param> |
| | | 48 | | public virtual void Init(Session session) |
| | 0 | 49 | | { |
| | 0 | 50 | | Session = session; |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Compresses the specified data. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="data">Data to compress.</param> |
| | | 57 | | /// <returns> |
| | | 58 | | /// The compressed data. |
| | | 59 | | /// </returns> |
| | | 60 | | public virtual byte[] Compress(byte[] data) |
| | 0 | 61 | | { |
| | 0 | 62 | | return Compress(data, 0, data.Length); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Compresses the specified data. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="data">Data to compress.</param> |
| | | 69 | | /// <param name="offset">The zero-based byte offset in <paramref name="data"/> at which to begin reading the dat |
| | | 70 | | /// <param name="length">The number of bytes to be compressed. </param> |
| | | 71 | | /// <returns> |
| | | 72 | | /// The compressed data. |
| | | 73 | | /// </returns> |
| | | 74 | | public virtual byte[] Compress(byte[] data, int offset, int length) |
| | 0 | 75 | | { |
| | 0 | 76 | | if (!IsActive) |
| | 0 | 77 | | { |
| | 0 | 78 | | if (offset == 0 && length == data.Length) |
| | 0 | 79 | | { |
| | 0 | 80 | | return data; |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | var buffer = new byte[length]; |
| | 0 | 84 | | Buffer.BlockCopy(data, offset, buffer, 0, length); |
| | 0 | 85 | | return buffer; |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | _compressorStream.SetLength(0); |
| | | 89 | | |
| | 0 | 90 | | _compressor.Write(data, offset, length); |
| | | 91 | | |
| | 0 | 92 | | return _compressorStream.ToArray(); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Decompresses the specified data. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="data">Compressed data.</param> |
| | | 99 | | /// <returns> |
| | | 100 | | /// The decompressed data. |
| | | 101 | | /// </returns> |
| | | 102 | | public virtual byte[] Decompress(byte[] data) |
| | 0 | 103 | | { |
| | 0 | 104 | | return Decompress(data, 0, data.Length); |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Decompresses the specified data. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="data">Compressed data.</param> |
| | | 111 | | /// <param name="offset">The zero-based byte offset in <paramref name="data"/> at which to begin reading the dat |
| | | 112 | | /// <param name="length">The number of bytes to be read from the compressed data. </param> |
| | | 113 | | /// <returns> |
| | | 114 | | /// The decompressed data. |
| | | 115 | | /// </returns> |
| | | 116 | | public virtual byte[] Decompress(byte[] data, int offset, int length) |
| | 0 | 117 | | { |
| | 0 | 118 | | if (!IsActive) |
| | 0 | 119 | | { |
| | 0 | 120 | | if (offset == 0 && length == data.Length) |
| | 0 | 121 | | { |
| | 0 | 122 | | return data; |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | var buffer = new byte[length]; |
| | 0 | 126 | | Buffer.BlockCopy(data, offset, buffer, 0, length); |
| | 0 | 127 | | return buffer; |
| | | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | _decompressorStream.SetLength(0); |
| | | 131 | | |
| | 0 | 132 | | _decompressor.Write(data, offset, length); |
| | | 133 | | |
| | 0 | 134 | | return _decompressorStream.ToArray(); |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 139 | | /// </summary> |
| | | 140 | | public void Dispose() |
| | 0 | 141 | | { |
| | 0 | 142 | | Dispose(disposing: true); |
| | 0 | 143 | | GC.SuppressFinalize(this); |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 150 | | protected virtual void Dispose(bool disposing) |
| | 0 | 151 | | { |
| | 0 | 152 | | if (_isDisposed) |
| | 0 | 153 | | { |
| | 0 | 154 | | return; |
| | | 155 | | } |
| | | 156 | | |
| | 0 | 157 | | if (disposing) |
| | 0 | 158 | | { |
| | 0 | 159 | | var compressorStream = _compressorStream; |
| | 0 | 160 | | if (compressorStream != null) |
| | 0 | 161 | | { |
| | 0 | 162 | | compressorStream.Dispose(); |
| | 0 | 163 | | _compressorStream = null; |
| | 0 | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | var decompressorStream = _decompressorStream; |
| | 0 | 167 | | if (decompressorStream != null) |
| | 0 | 168 | | { |
| | 0 | 169 | | decompressorStream.Dispose(); |
| | 0 | 170 | | _decompressorStream = null; |
| | 0 | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | _isDisposed = true; |
| | 0 | 174 | | } |
| | 0 | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Releases unmanaged resources and performs other cleanup operations before the <see cref="Compressor"/> is re |
| | | 179 | | /// by garbage collection. |
| | | 180 | | /// </summary> |
| | | 181 | | ~Compressor() |
| | 0 | 182 | | { |
| | 0 | 183 | | Dispose(disposing: false); |
| | 0 | 184 | | } |
| | | 185 | | } |
| | | 186 | | } |