| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Text; |
| | | 4 | | |
| | | 5 | | namespace Renci.SshNet.Common |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Base ssh data serialization type. |
| | | 9 | | /// </summary> |
| | | 10 | | #pragma warning disable CA1001 // Types that own disposable fields should be disposable |
| | | 11 | | public abstract class SshData |
| | | 12 | | #pragma warning restore CA1001 // Types that own disposable fields should be disposable |
| | | 13 | | { |
| | | 14 | | internal const int DefaultCapacity = 64; |
| | | 15 | | |
| | 4 | 16 | | internal static readonly Encoding Ascii = Encoding.ASCII; |
| | | 17 | | |
| | 4 | 18 | | internal static readonly Encoding Utf8 = Encoding.UTF8; |
| | | 19 | | |
| | | 20 | | private SshDataStream _stream; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the underlying <see cref="SshDataStream"/> that is used for reading and writing SSH data. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <value> |
| | | 26 | | /// The underlying <see cref="SshDataStream"/> that is used for reading and writing SSH data. |
| | | 27 | | /// </value> |
| | | 28 | | protected SshDataStream DataStream |
| | | 29 | | { |
| | 66504 | 30 | | get { return _stream; } |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets a value indicating whether all data from the buffer has been read. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <value> |
| | | 37 | | /// <see langword="true"/> if this instance is end of data; otherwise, <see langword="false"/>. |
| | | 38 | | /// </value> |
| | | 39 | | protected bool IsEndOfData |
| | | 40 | | { |
| | | 41 | | get |
| | 26166 | 42 | | { |
| | 26166 | 43 | | return _stream.Position >= _stream.Length; |
| | 26166 | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the size of the message in bytes. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <value> |
| | | 51 | | /// The size of the messages in bytes. |
| | | 52 | | /// </value> |
| | | 53 | | protected virtual int BufferCapacity |
| | | 54 | | { |
| | 52374 | 55 | | get { return 0; } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets data bytes array. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <returns> |
| | | 62 | | /// A <see cref="byte"/> array representation of data structure. |
| | | 63 | | /// </returns> |
| | | 64 | | public byte[] GetBytes() |
| | 52313 | 65 | | { |
| | 52313 | 66 | | var messageLength = BufferCapacity; |
| | 52313 | 67 | | var capacity = messageLength != -1 ? messageLength : DefaultCapacity; |
| | | 68 | | |
| | 52313 | 69 | | using (var dataStream = new SshDataStream(capacity)) |
| | 52313 | 70 | | { |
| | 52313 | 71 | | WriteBytes(dataStream); |
| | 52313 | 72 | | return dataStream.ToArray(); |
| | | 73 | | } |
| | 52313 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Writes the current message to the specified <see cref="SshDataStream"/>. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="stream">The <see cref="SshDataStream"/> to write the message to.</param> |
| | | 80 | | protected virtual void WriteBytes(SshDataStream stream) |
| | 106823 | 81 | | { |
| | 106823 | 82 | | _stream = stream; |
| | 106823 | 83 | | SaveData(); |
| | 106823 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Loads data from specified bytes. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="data">Bytes array.</param> |
| | | 90 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception> |
| | | 91 | | public void Load(byte[] data) |
| | 4271 | 92 | | { |
| | 4271 | 93 | | if (data is null) |
| | 3 | 94 | | { |
| | 3 | 95 | | throw new ArgumentNullException(nameof(data)); |
| | | 96 | | } |
| | | 97 | | |
| | 4268 | 98 | | LoadInternal(data, 0, data.Length); |
| | 4268 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Loads data from the specified buffer. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="data">Bytes array.</param> |
| | | 105 | | /// <param name="offset">The zero-based offset in <paramref name="data"/> at which to begin reading SSH data.</p |
| | | 106 | | /// <param name="count">The number of bytes to load.</param> |
| | | 107 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception> |
| | | 108 | | public void Load(byte[] data, int offset, int count) |
| | 90451 | 109 | | { |
| | 90451 | 110 | | if (data is null) |
| | 0 | 111 | | { |
| | 0 | 112 | | throw new ArgumentNullException(nameof(data)); |
| | | 113 | | } |
| | | 114 | | |
| | 90451 | 115 | | LoadInternal(data, offset, count); |
| | 90448 | 116 | | } |
| | | 117 | | |
| | | 118 | | private void LoadInternal(byte[] value, int offset, int count) |
| | 94719 | 119 | | { |
| | 94719 | 120 | | _stream = new SshDataStream(value, offset, count); |
| | 94719 | 121 | | LoadData(); |
| | 94716 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Called when type specific data need to be loaded. |
| | | 126 | | /// </summary> |
| | | 127 | | protected abstract void LoadData(); |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Called when type specific data need to be saved. |
| | | 131 | | /// </summary> |
| | | 132 | | protected abstract void SaveData(); |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Reads all data left in internal buffer at current position. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <returns> |
| | | 138 | | /// An array of bytes containing the remaining data in the internal buffer. |
| | | 139 | | /// </returns> |
| | | 140 | | protected byte[] ReadBytes() |
| | 1742 | 141 | | { |
| | 1742 | 142 | | var bytesLength = (int) (_stream.Length - _stream.Position); |
| | 1742 | 143 | | var data = new byte[bytesLength]; |
| | 1742 | 144 | | _ = _stream.Read(data, 0, bytesLength); |
| | 1742 | 145 | | return data; |
| | 1742 | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Reads next specified number of bytes data type from internal buffer. |
| | | 150 | | /// </summary> |
| | | 151 | | /// <param name="length">Number of bytes to read.</param> |
| | | 152 | | /// <returns> |
| | | 153 | | /// An array of bytes that was read from the internal buffer. |
| | | 154 | | /// </returns> |
| | | 155 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is greater than the number of bytes |
| | | 156 | | protected byte[] ReadBytes(int length) |
| | 119753 | 157 | | { |
| | 119753 | 158 | | var data = new byte[length]; |
| | 119753 | 159 | | var bytesRead = _stream.Read(data, 0, length); |
| | | 160 | | |
| | 119753 | 161 | | if (bytesRead < length) |
| | 3 | 162 | | { |
| | 3 | 163 | | throw new ArgumentOutOfRangeException(nameof(length)); |
| | | 164 | | } |
| | | 165 | | |
| | 119750 | 166 | | return data; |
| | 119750 | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Reads next byte data type from internal buffer. |
| | | 171 | | /// </summary> |
| | | 172 | | /// <returns> |
| | | 173 | | /// The <see cref="byte"/> read. |
| | | 174 | | /// </returns> |
| | | 175 | | /// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception> |
| | | 176 | | protected byte ReadByte() |
| | 7183 | 177 | | { |
| | 7183 | 178 | | var byteRead = _stream.ReadByte(); |
| | 7183 | 179 | | if (byteRead == -1) |
| | 0 | 180 | | { |
| | 0 | 181 | | throw new InvalidOperationException("Attempt to read past the end of the SSH data stream."); |
| | | 182 | | } |
| | | 183 | | |
| | 7183 | 184 | | return (byte) byteRead; |
| | 7183 | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Reads the next <see cref="bool"/> from the internal buffer. |
| | | 189 | | /// </summary> |
| | | 190 | | /// <returns> |
| | | 191 | | /// The <see cref="bool"/> that was read. |
| | | 192 | | /// </returns> |
| | | 193 | | /// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception> |
| | | 194 | | protected bool ReadBoolean() |
| | 7183 | 195 | | { |
| | 7183 | 196 | | return ReadByte() != 0; |
| | 7183 | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Reads the next <see cref="ushort"/> from the internal buffer. |
| | | 201 | | /// </summary> |
| | | 202 | | /// <returns> |
| | | 203 | | /// The <see cref="ushort"/> that was read. |
| | | 204 | | /// </returns> |
| | | 205 | | /// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception> |
| | | 206 | | protected ushort ReadUInt16() |
| | 0 | 207 | | { |
| | 0 | 208 | | return Pack.BigEndianToUInt16(ReadBytes(2)); |
| | 0 | 209 | | } |
| | | 210 | | |
| | | 211 | | /// <summary> |
| | | 212 | | /// Reads the next <see cref="uint"/> from the internal buffer. |
| | | 213 | | /// </summary> |
| | | 214 | | /// <returns> |
| | | 215 | | /// The <see cref="uint"/> that was read. |
| | | 216 | | /// </returns> |
| | | 217 | | /// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception> |
| | | 218 | | protected uint ReadUInt32() |
| | 117607 | 219 | | { |
| | 117607 | 220 | | return Pack.BigEndianToUInt32(ReadBytes(4)); |
| | 117607 | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Reads the next <see cref="ulong"/> from the internal buffer. |
| | | 225 | | /// </summary> |
| | | 226 | | /// <returns> |
| | | 227 | | /// The <see cref="ulong"/> that was read. |
| | | 228 | | /// </returns> |
| | | 229 | | /// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception> |
| | | 230 | | protected ulong ReadUInt64() |
| | 0 | 231 | | { |
| | 0 | 232 | | return Pack.BigEndianToUInt64(ReadBytes(8)); |
| | 0 | 233 | | } |
| | | 234 | | |
| | | 235 | | /// <summary> |
| | | 236 | | /// Reads the next <see cref="string"/> from the internal buffer using the specified encoding. |
| | | 237 | | /// </summary> |
| | | 238 | | /// <param name="encoding">The character encoding to use.</param> |
| | | 239 | | /// <returns> |
| | | 240 | | /// The <see cref="string"/> that was read. |
| | | 241 | | /// </returns> |
| | | 242 | | protected string ReadString(Encoding encoding) |
| | 106911 | 243 | | { |
| | 106911 | 244 | | return _stream.ReadString(encoding); |
| | 106911 | 245 | | } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Reads next data type as byte array from internal buffer. |
| | | 249 | | /// </summary> |
| | | 250 | | /// <returns> |
| | | 251 | | /// The bytes read. |
| | | 252 | | /// </returns> |
| | | 253 | | protected byte[] ReadBinary() |
| | 57951 | 254 | | { |
| | 57951 | 255 | | return _stream.ReadBinary(); |
| | 57951 | 256 | | } |
| | | 257 | | |
| | | 258 | | /// <summary> |
| | | 259 | | /// Reads next name-list data type from internal buffer. |
| | | 260 | | /// </summary> |
| | | 261 | | /// <returns> |
| | | 262 | | /// String array or read data. |
| | | 263 | | /// </returns> |
| | | 264 | | protected string[] ReadNamesList() |
| | 19393 | 265 | | { |
| | 19393 | 266 | | var namesList = ReadString(Ascii); |
| | 19393 | 267 | | return namesList.Split(','); |
| | 19393 | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Reads next extension-pair data type from internal buffer. |
| | | 272 | | /// </summary> |
| | | 273 | | /// <returns> |
| | | 274 | | /// Extensions pair dictionary. |
| | | 275 | | /// </returns> |
| | | 276 | | protected Dictionary<string, string> ReadExtensionPair() |
| | 641 | 277 | | { |
| | 641 | 278 | | var result = new Dictionary<string, string>(); |
| | | 279 | | |
| | 7434 | 280 | | while (!IsEndOfData) |
| | 6793 | 281 | | { |
| | 6793 | 282 | | var extensionName = ReadString(Ascii); |
| | 6793 | 283 | | var extensionData = ReadString(Ascii); |
| | 6793 | 284 | | result.Add(extensionName, extensionData); |
| | 6793 | 285 | | } |
| | | 286 | | |
| | 641 | 287 | | return result; |
| | 641 | 288 | | } |
| | | 289 | | |
| | | 290 | | /// <summary> |
| | | 291 | | /// Writes bytes array data into internal buffer. |
| | | 292 | | /// </summary> |
| | | 293 | | /// <param name="data">Byte array data to write.</param> |
| | | 294 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception> |
| | | 295 | | protected void Write(byte[] data) |
| | 32600 | 296 | | { |
| | 32600 | 297 | | _stream.Write(data); |
| | 32600 | 298 | | } |
| | | 299 | | |
| | | 300 | | /// <summary> |
| | | 301 | | /// Writes a sequence of bytes to the current SSH data stream and advances the current position |
| | | 302 | | /// within this stream by the number of bytes written. |
| | | 303 | | /// </summary> |
| | | 304 | | /// <param name="buffer">An array of bytes. This method write <paramref name="count"/> bytes from buffer to the |
| | | 305 | | /// <param name="offset">The zero-based offset in <paramref name="buffer"/> at which to begin writing bytes to t |
| | | 306 | | /// <param name="count">The number of bytes to be written to the current SSH data stream.</param> |
| | | 307 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception> |
| | | 308 | | /// <exception cref="ArgumentException">The sum of <paramref name="offset"/> and <paramref name="count"/> is gre |
| | | 309 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="count"/> is negat |
| | | 310 | | protected void Write(byte[] buffer, int offset, int count) |
| | 3 | 311 | | { |
| | 3 | 312 | | _stream.Write(buffer, offset, count); |
| | 3 | 313 | | } |
| | | 314 | | |
| | | 315 | | /// <summary> |
| | | 316 | | /// Writes <see cref="byte"/> data into internal buffer. |
| | | 317 | | /// </summary> |
| | | 318 | | /// <param name="data"><see cref="byte"/> data to write.</param> |
| | | 319 | | protected void Write(byte data) |
| | 48809 | 320 | | { |
| | 48809 | 321 | | _stream.WriteByte(data); |
| | 48809 | 322 | | } |
| | | 323 | | |
| | | 324 | | /// <summary> |
| | | 325 | | /// Writes <see cref="bool"/> into internal buffer. |
| | | 326 | | /// </summary> |
| | | 327 | | /// <param name="data"><see cref="bool" /> data to write.</param> |
| | | 328 | | protected void Write(bool data) |
| | 7845 | 329 | | { |
| | 7845 | 330 | | Write(data ? (byte) 1 : (byte) 0); |
| | 7845 | 331 | | } |
| | | 332 | | |
| | | 333 | | /// <summary> |
| | | 334 | | /// Writes <see cref="uint"/> data into internal buffer. |
| | | 335 | | /// </summary> |
| | | 336 | | /// <param name="data"><see cref="uint"/> data to write.</param> |
| | | 337 | | protected void Write(uint data) |
| | 90067 | 338 | | { |
| | 90067 | 339 | | _stream.Write(data); |
| | 90067 | 340 | | } |
| | | 341 | | |
| | | 342 | | /// <summary> |
| | | 343 | | /// Writes <see cref="ulong" /> data into internal buffer. |
| | | 344 | | /// </summary> |
| | | 345 | | /// <param name="data"><see cref="ulong"/> data to write.</param> |
| | | 346 | | protected void Write(ulong data) |
| | 8046 | 347 | | { |
| | 8046 | 348 | | _stream.Write(data); |
| | 8046 | 349 | | } |
| | | 350 | | |
| | | 351 | | /// <summary> |
| | | 352 | | /// Writes <see cref="string"/> data into internal buffer using default encoding. |
| | | 353 | | /// </summary> |
| | | 354 | | /// <param name="data"><see cref="string"/> data to write.</param> |
| | | 355 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception> |
| | | 356 | | protected void Write(string data) |
| | 22 | 357 | | { |
| | 22 | 358 | | Write(data, Utf8); |
| | 22 | 359 | | } |
| | | 360 | | |
| | | 361 | | /// <summary> |
| | | 362 | | /// Writes <see cref="string"/> data into internal buffer using the specified encoding. |
| | | 363 | | /// </summary> |
| | | 364 | | /// <param name="data"><see cref="string"/> data to write.</param> |
| | | 365 | | /// <param name="encoding">The character encoding to use.</param> |
| | | 366 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception> |
| | | 367 | | /// <exception cref="ArgumentNullException"><paramref name="encoding"/> is <see langword="null"/>.</exception> |
| | | 368 | | protected void Write(string data, Encoding encoding) |
| | 42208 | 369 | | { |
| | 42208 | 370 | | _stream.Write(data, encoding); |
| | 42208 | 371 | | } |
| | | 372 | | |
| | | 373 | | /// <summary> |
| | | 374 | | /// Writes mpint data into internal buffer. |
| | | 375 | | /// </summary> |
| | | 376 | | /// <param name="data">mpint data to write.</param> |
| | | 377 | | protected void Write(BigInteger data) |
| | 0 | 378 | | { |
| | 0 | 379 | | _stream.Write(data); |
| | 0 | 380 | | } |
| | | 381 | | |
| | | 382 | | /// <summary> |
| | | 383 | | /// Writes name-list data into internal buffer. |
| | | 384 | | /// </summary> |
| | | 385 | | /// <param name="data">name-list data to write.</param> |
| | | 386 | | protected void Write(string[] data) |
| | 42150 | 387 | | { |
| | | 388 | | #if NET || NETSTANDARD2_1_OR_GREATER |
| | 40090 | 389 | | Write(string.Join(',', data), Ascii); |
| | | 390 | | #else |
| | 2060 | 391 | | Write(string.Join(",", data), Ascii); |
| | | 392 | | #endif // NET || NETSTANDARD2_1_OR_GREATER |
| | 42150 | 393 | | } |
| | | 394 | | |
| | | 395 | | /// <summary> |
| | | 396 | | /// Writes extension-pair data into internal buffer. |
| | | 397 | | /// </summary> |
| | | 398 | | /// <param name="data">extension-pair data to write.</param> |
| | | 399 | | protected void Write(IDictionary<string, string> data) |
| | 24 | 400 | | { |
| | 84 | 401 | | foreach (var item in data) |
| | 6 | 402 | | { |
| | 6 | 403 | | Write(item.Key, Ascii); |
| | 6 | 404 | | Write(item.Value, Ascii); |
| | 6 | 405 | | } |
| | 24 | 406 | | } |
| | | 407 | | |
| | | 408 | | /// <summary> |
| | | 409 | | /// Writes data into internal buffer. |
| | | 410 | | /// </summary> |
| | | 411 | | /// <param name="buffer">The data to write.</param> |
| | | 412 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception> |
| | | 413 | | protected void WriteBinaryString(byte[] buffer) |
| | 96491 | 414 | | { |
| | 96491 | 415 | | _stream.WriteBinary(buffer); |
| | 96491 | 416 | | } |
| | | 417 | | |
| | | 418 | | /// <summary> |
| | | 419 | | /// Writes data into internal buffer. |
| | | 420 | | /// </summary> |
| | | 421 | | /// <param name="buffer">An array of bytes. This method write <paramref name="count"/> bytes from buffer to the |
| | | 422 | | /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin writing bytes |
| | | 423 | | /// <param name="count">The number of bytes to be written to the current SSH data stream.</param> |
| | | 424 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception> |
| | | 425 | | /// <exception cref="ArgumentException">The sum of <paramref name="offset"/> and <paramref name="count"/> is gre |
| | | 426 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="count"/> is negat |
| | | 427 | | protected void WriteBinary(byte[] buffer, int offset, int count) |
| | 40880 | 428 | | { |
| | 40880 | 429 | | _stream.WriteBinary(buffer, offset, count); |
| | 40880 | 430 | | } |
| | | 431 | | } |
| | | 432 | | } |