| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Text; |
| | | 5 | | |
| | | 6 | | namespace Renci.SshNet.Common |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Specialized <see cref="MemoryStream"/> for reading and writing data SSH data. |
| | | 10 | | /// </summary> |
| | | 11 | | public class SshDataStream : MemoryStream |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Initializes a new instance of the <see cref="SshDataStream"/> class with an expandable capacity initialized |
| | | 15 | | /// as specified. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="capacity">The initial size of the internal array in bytes.</param> |
| | | 18 | | public SshDataStream(int capacity) |
| | 118069 | 19 | | : base(capacity) |
| | 118069 | 20 | | { |
| | 118069 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="SshDataStream"/> class for the specified byte array. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="buffer">The array of unsigned bytes from which to create the current stream.</param> |
| | | 27 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception> |
| | | 28 | | public SshDataStream(byte[] buffer) |
| | 96 | 29 | | : base(buffer) |
| | 96 | 30 | | { |
| | 96 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="SshDataStream"/> class for the specified byte array. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="buffer">The array of unsigned bytes from which to create the current stream.</param> |
| | | 37 | | /// <param name="offset">The zero-based offset in <paramref name="buffer"/> at which to begin reading SSH data.< |
| | | 38 | | /// <param name="count">The number of bytes to load.</param> |
| | | 39 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception> |
| | | 40 | | public SshDataStream(byte[] buffer, int offset, int count) |
| | 94719 | 41 | | : base(buffer, offset, count) |
| | 94719 | 42 | | { |
| | 94719 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets a value indicating whether all data from the SSH data stream has been read. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <value> |
| | | 49 | | /// <see langword="true"/> if this instance is end of data; otherwise, <see langword="false"/>. |
| | | 50 | | /// </value> |
| | | 51 | | public bool IsEndOfData |
| | | 52 | | { |
| | | 53 | | get |
| | 96 | 54 | | { |
| | 96 | 55 | | return Position >= Length; |
| | 96 | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Writes an <see cref="uint"/> to the SSH data stream. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="value"><see cref="uint"/> data to write.</param> |
| | | 63 | | public void Write(uint value) |
| | 368704 | 64 | | { |
| | 368704 | 65 | | var bytes = Pack.UInt32ToBigEndian(value); |
| | 368704 | 66 | | Write(bytes, 0, bytes.Length); |
| | 368704 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Writes an <see cref="ulong"/> to the SSH data stream. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="value"><see cref="ulong"/> data to write.</param> |
| | | 73 | | public void Write(ulong value) |
| | 8182 | 74 | | { |
| | 8182 | 75 | | var bytes = Pack.UInt64ToBigEndian(value); |
| | 8182 | 76 | | Write(bytes, 0, bytes.Length); |
| | 8182 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Writes a <see cref="BigInteger"/> into the SSH data stream. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="data">The <see cref="BigInteger" /> to write.</param> |
| | | 83 | | public void Write(BigInteger data) |
| | 0 | 84 | | { |
| | 0 | 85 | | var bytes = data.ToByteArray().Reverse(); |
| | 0 | 86 | | WriteBinary(bytes, 0, bytes.Length); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Writes bytes array data into the SSH data stream. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="data">Byte array data to write.</param> |
| | | 93 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception> |
| | | 94 | | public void Write(byte[] data) |
| | 32642 | 95 | | { |
| | 32642 | 96 | | if (data is null) |
| | 0 | 97 | | { |
| | 0 | 98 | | throw new ArgumentNullException(nameof(data)); |
| | | 99 | | } |
| | | 100 | | |
| | 32642 | 101 | | Write(data, 0, data.Length); |
| | 32642 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Writes string data to the SSH data stream using the specified encoding. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="s">The string data to write.</param> |
| | | 108 | | /// <param name="encoding">The character encoding to use.</param> |
| | | 109 | | /// <exception cref="ArgumentNullException"><paramref name="s"/> is <see langword="null"/>.</exception> |
| | | 110 | | /// <exception cref="ArgumentNullException"><paramref name="encoding"/> is <see langword="null"/>.</exception> |
| | | 111 | | public void Write(string s, Encoding encoding) |
| | 42208 | 112 | | { |
| | 42208 | 113 | | if (encoding is null) |
| | 0 | 114 | | { |
| | 0 | 115 | | throw new ArgumentNullException(nameof(encoding)); |
| | | 116 | | } |
| | | 117 | | |
| | 42208 | 118 | | var bytes = encoding.GetBytes(s); |
| | 42208 | 119 | | WriteBinary(bytes, 0, bytes.Length); |
| | 42208 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Reads a byte array from the SSH data stream. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <returns> |
| | | 126 | | /// The byte array read from the SSH data stream. |
| | | 127 | | /// </returns> |
| | | 128 | | public byte[] ReadBinary() |
| | 57951 | 129 | | { |
| | 57951 | 130 | | var length = ReadUInt32(); |
| | | 131 | | |
| | 57951 | 132 | | if (length > int.MaxValue) |
| | 0 | 133 | | { |
| | 0 | 134 | | throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not s |
| | | 135 | | } |
| | | 136 | | |
| | 57951 | 137 | | return ReadBytes((int)length); |
| | 57951 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Writes a buffer preceded by its length into the SSH data stream. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="buffer">The data to write.</param> |
| | | 144 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception> |
| | | 145 | | public void WriteBinary(byte[] buffer) |
| | 97106 | 146 | | { |
| | 97106 | 147 | | if (buffer is null) |
| | 0 | 148 | | { |
| | 0 | 149 | | throw new ArgumentNullException(nameof(buffer)); |
| | | 150 | | } |
| | | 151 | | |
| | 97106 | 152 | | WriteBinary(buffer, 0, buffer.Length); |
| | 97106 | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Writes a buffer preceded by its length into the SSH data stream. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <param name="buffer">An array of bytes. This method write <paramref name="count"/> bytes from buffer to the |
| | | 159 | | /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin writing bytes |
| | | 160 | | /// <param name="count">The number of bytes to be written to the current SSH data stream.</param> |
| | | 161 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception> |
| | | 162 | | /// <exception cref="ArgumentException">The sum of <paramref name="offset"/> and <paramref name="count"/> is gre |
| | | 163 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="count"/> is negat |
| | | 164 | | public void WriteBinary(byte[] buffer, int offset, int count) |
| | 180194 | 165 | | { |
| | 180194 | 166 | | Write((uint) count); |
| | 180194 | 167 | | Write(buffer, offset, count); |
| | 180194 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Reads a <see cref="BigInteger"/> from the SSH datastream. |
| | | 172 | | /// </summary> |
| | | 173 | | /// <returns> |
| | | 174 | | /// The <see cref="BigInteger"/> read from the SSH data stream. |
| | | 175 | | /// </returns> |
| | | 176 | | public BigInteger ReadBigInt() |
| | 0 | 177 | | { |
| | 0 | 178 | | var length = ReadUInt32(); |
| | 0 | 179 | | var data = ReadBytes((int) length); |
| | 0 | 180 | | return new BigInteger(data.Reverse()); |
| | 0 | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Reads the next <see cref="uint"/> data type from the SSH data stream. |
| | | 185 | | /// </summary> |
| | | 186 | | /// <returns> |
| | | 187 | | /// The <see cref="uint"/> read from the SSH data stream. |
| | | 188 | | /// </returns> |
| | | 189 | | public uint ReadUInt32() |
| | 240518 | 190 | | { |
| | 240518 | 191 | | var data = ReadBytes(4); |
| | 240518 | 192 | | return Pack.BigEndianToUInt32(data); |
| | 240518 | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Reads the next <see cref="ulong"/> data type from the SSH data stream. |
| | | 197 | | /// </summary> |
| | | 198 | | /// <returns> |
| | | 199 | | /// The <see cref="ulong"/> read from the SSH data stream. |
| | | 200 | | /// </returns> |
| | | 201 | | public ulong ReadUInt64() |
| | 10778 | 202 | | { |
| | 10778 | 203 | | var data = ReadBytes(8); |
| | 10778 | 204 | | return Pack.BigEndianToUInt64(data); |
| | 10778 | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Reads the next <see cref="string"/> data type from the SSH data stream. |
| | | 209 | | /// </summary> |
| | | 210 | | /// <param name="encoding">The character encoding to use.</param> |
| | | 211 | | /// <returns> |
| | | 212 | | /// The <see cref="string"/> read from the SSH data stream. |
| | | 213 | | /// </returns> |
| | | 214 | | public string ReadString(Encoding encoding) |
| | 106920 | 215 | | { |
| | 106920 | 216 | | var length = ReadUInt32(); |
| | | 217 | | |
| | 106920 | 218 | | if (length > int.MaxValue) |
| | 0 | 219 | | { |
| | 0 | 220 | | throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} is no |
| | | 221 | | } |
| | | 222 | | |
| | 106920 | 223 | | var bytes = ReadBytes((int) length); |
| | 106920 | 224 | | return encoding.GetString(bytes, 0, bytes.Length); |
| | 106920 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Writes the stream contents to a byte array, regardless of the <see cref="MemoryStream.Position"/>. |
| | | 229 | | /// </summary> |
| | | 230 | | /// <returns> |
| | | 231 | | /// This method returns the contents of the <see cref="SshDataStream"/> as a byte array. |
| | | 232 | | /// </returns> |
| | | 233 | | /// <remarks> |
| | | 234 | | /// If the current instance was constructed on a provided byte array, a copy of the section of the array |
| | | 235 | | /// to which this instance has access is returned. |
| | | 236 | | /// </remarks> |
| | | 237 | | public override byte[] ToArray() |
| | 118069 | 238 | | { |
| | 118069 | 239 | | if (Capacity == Length) |
| | 113658 | 240 | | { |
| | 113658 | 241 | | return GetBuffer(); |
| | | 242 | | } |
| | | 243 | | |
| | 4411 | 244 | | return base.ToArray(); |
| | 118069 | 245 | | } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Reads next specified number of bytes data type from internal buffer. |
| | | 249 | | /// </summary> |
| | | 250 | | /// <param name="length">Number of bytes to read.</param> |
| | | 251 | | /// <returns> |
| | | 252 | | /// An array of bytes that was read from the internal buffer. |
| | | 253 | | /// </returns> |
| | | 254 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is greater than the internal buffer |
| | | 255 | | private byte[] ReadBytes(int length) |
| | 416167 | 256 | | { |
| | 416167 | 257 | | var data = new byte[length]; |
| | 416167 | 258 | | var bytesRead = Read(data, 0, length); |
| | | 259 | | |
| | 416167 | 260 | | if (bytesRead < length) |
| | 0 | 261 | | { |
| | 0 | 262 | | throw new ArgumentOutOfRangeException(nameof(length), string.Format(CultureInfo.InvariantCulture, "The r |
| | | 263 | | } |
| | | 264 | | |
| | 416167 | 265 | | return data; |
| | 416167 | 266 | | } |
| | | 267 | | } |
| | | 268 | | } |