| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using System;
- using System.Globalization;
- using System.IO;
- using System.Text;
- namespace Renci.SshNet.Common
- {
- public class SshDataStream : MemoryStream
- {
- public SshDataStream(int capacity)
- : base(capacity)
- {
- }
- public SshDataStream(byte[] buffer)
- : base(buffer)
- {
- }
- /// <summary>
- /// Gets a value indicating whether all data from the SSH data stream has been read.
- /// </summary>
- /// <value>
- /// <c>true</c> if this instance is end of data; otherwise, <c>false</c>.
- /// </value>
- public bool IsEndOfData
- {
- get
- {
- return Position >= Length;
- }
- }
- /// <summary>
- /// Writes an <see cref="uint"/> to the SSH data stream.
- /// </summary>
- /// <param name="value"><see cref="uint"/> data to write.</param>
- public void Write(uint value)
- {
- var bytes = value.GetBytes();
- Write(bytes, 0, bytes.Length);
- }
- /// <summary>
- /// Writes an <see cref="ulong"/> to the SSH data stream.
- /// </summary>
- /// <param name="value"><see cref="ulong"/> data to write.</param>
- public void Write(ulong value)
- {
- var bytes = value.GetBytes();
- Write(bytes, 0, bytes.Length);
- }
- /// <summary>
- /// Writes a <see cref="BigInteger"/> into the SSH data stream.
- /// </summary>
- /// <param name="data">The <see cref="BigInteger" /> to write.</param>
- public void Write(BigInteger data)
- {
- var bytes = data.ToByteArray().Reverse();
- WriteBinary(bytes, 0, bytes.Length);
- }
- /// <summary>
- /// Writes bytes array data into the SSH data stream.
- /// </summary>
- /// <param name="data">Byte array data to write.</param>
- /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception>
- public void Write(byte[] data)
- {
- if (data == null)
- throw new ArgumentNullException("data");
- Write(data, 0, data.Length);
- }
- /// <summary>
- /// Reads a byte array from the SSH data stream.
- /// </summary>
- /// <returns>
- /// The byte array read from the SSH data stream.
- /// </returns>
- public byte[] ReadBinary()
- {
- var length = ReadUInt32();
- if (length > int.MaxValue)
- {
- throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not supported.", int.MaxValue));
- }
- return ReadBytes((int)length);
- }
- /// <summary>
- /// Writes a buffer preceded by its length into the SSH data stream.
- /// </summary>
- /// <param name="buffer">The data to write.</param>
- /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
- public void WriteBinary(byte[] buffer)
- {
- if (buffer == null)
- throw new ArgumentNullException("buffer");
- WriteBinary(buffer, 0, buffer.Length);
- }
- /// <summary>
- /// Writes a buffer preceded by its length into the SSH data stream.
- /// </summary>
- /// <param name="buffer">An array of bytes. This method write <paramref name="count"/> bytes from buffer to the current SSH data stream.</param>
- /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin writing bytes to the SSH data stream.</param>
- /// <param name="count">The number of bytes to be written to the current SSH data stream.</param>
- /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
- /// <exception cref="ArgumentException">The sum of <paramref name="offset"/> and <paramref name="count"/> is greater than the buffer length.</exception>
- /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="count"/> is negative.</exception>
- public void WriteBinary(byte[] buffer, int offset, int count)
- {
- Write((uint) count);
- Write(buffer, offset, count);
- }
- /// <summary>
- /// Writes string data to the SSH data stream using the specified encoding.
- /// </summary>
- /// <param name="s">The string data to write.</param>
- /// <param name="encoding">The character encoding to use.</param>
- /// <exception cref="ArgumentNullException"><paramref name="s"/> is null.</exception>
- /// <exception cref="ArgumentNullException"><paramref name="encoding"/> is null.</exception>
- public void Write(string s, Encoding encoding)
- {
- if (encoding == null)
- throw new ArgumentNullException("encoding");
- var bytes = encoding.GetBytes(s);
- WriteBinary(bytes, 0, bytes.Length);
- }
- /// <summary>
- /// Reads a <see cref="BigInteger"/> from the SSH datastream.
- /// </summary>
- /// <returns>
- /// The <see cref="BigInteger"/> read from the SSH data stream.
- /// </returns>
- public BigInteger ReadBigInt()
- {
- var length = ReadUInt32();
- var data = ReadBytes((int) length);
- return new BigInteger(data.Reverse());
- }
- /// <summary>
- /// Reads the next <see cref="uint"/> data type from the SSH data stream.
- /// </summary>
- /// <returns>
- /// The <see cref="uint"/> read from the SSH data stream.
- /// </returns>
- public uint ReadUInt32()
- {
- var data = ReadBytes(4);
- return (uint) (data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]);
- }
- /// <summary>
- /// Reads the next <see cref="ulong"/> data type from the SSH data stream.
- /// </summary>
- /// <returns>
- /// The <see cref="ulong"/> read from the SSH data stream.
- /// </returns>
- public ulong ReadUInt64()
- {
- var data = ReadBytes(8);
- return ((ulong) data[0] << 56 | (ulong) data[1] << 48 | (ulong) data[2] << 40 | (ulong) data[3] << 32 |
- (ulong) data[4] << 24 | (ulong) data[5] << 16 | (ulong) data[6] << 8 | data[7]);
- }
- /// <summary>
- /// Reads the next <see cref="string"/> data type from the SSH data stream.
- /// </summary>
- /// <returns>
- /// The <see cref="string"/> read from the SSH data stream.
- /// </returns>
- public string ReadString(Encoding encoding)
- {
- var length = ReadUInt32();
- if (length > int.MaxValue)
- {
- throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} is not supported.", int.MaxValue));
- }
- var bytes = ReadBytes((int) length);
- return encoding.GetString(bytes, 0, bytes.Length);
- }
- /// <summary>
- /// Reads next specified number of bytes data type from internal buffer.
- /// </summary>
- /// <param name="length">Number of bytes to read.</param>
- /// <returns>An array of bytes that was read from the internal buffer.</returns>
- /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is greater than the internal buffer size.</exception>
- private byte[] ReadBytes(int length)
- {
- var data = new byte[length];
- var bytesRead = Read(data, 0, length);
- if (bytesRead < length)
- throw new ArgumentOutOfRangeException("length");
- return data;
- }
- /// <summary>
- /// Writes the stream contents to a byte array, regardless of the <see cref="MemoryStream.Position"/>.
- /// </summary>
- /// <returns>
- /// This method returns the contents of the <see cref="SshDataStream"/> as a byte array.
- /// </returns>
- /// <remarks>
- /// If the current instance was constructed on a provided byte array, a copy of the section of the array
- /// to which this instance has access is returned.
- /// </remarks>
- public override byte[] ToArray()
- {
- if (Capacity == Length)
- {
- #if FEATURE_MEMORYSTREAM_GETBUFFER
- return GetBuffer();
- #elif FEATURE_MEMORYSTREAM_TRYGETBUFFER
- ArraySegment<byte> buffer;
- if (TryGetBuffer(out buffer))
- return buffer.Array;
- #endif
- }
- return base.ToArray();
- }
- }
- }
|