| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638 |
- using System;
- using System.Collections.Generic;
- #if !TUNING
- using System.Linq;
- #endif
- using System.Text;
- using System.Globalization;
- namespace Renci.SshNet.Common
- {
- /// <summary>
- /// Base ssh data serialization type
- /// </summary>
- public abstract class SshData
- {
- internal const int DefaultCapacity = 64;
- internal static readonly Encoding Ascii = new ASCIIEncoding();
- #if SILVERLIGHT
- internal static readonly Encoding Utf8 = Encoding.UTF8;
- #else
- internal static readonly Encoding Utf8 = Encoding.Default;
- #endif
- #if TUNING
- private SshDataStream _stream;
- protected SshDataStream DataStream
- {
- get { return _stream; }
- }
- #else
- /// <summary>
- /// Data byte array that hold message unencrypted data
- /// </summary>
- private List<byte> _data;
- private int _readerIndex;
- #endif
- /// <summary>
- /// Gets a value indicating whether all data from the buffer has been read.
- /// </summary>
- /// <value>
- /// <c>true</c> if this instance is end of data; otherwise, <c>false</c>.
- /// </value>
- protected bool IsEndOfData
- {
- get
- {
- #if TUNING
- return _stream.Position >= _stream.Length;
- #else
- return _readerIndex >= _data.Count();
- #endif
- }
- }
- private byte[] _loadedData;
- #if TUNING
- private int _offset;
- #endif
- /// <summary>
- /// Gets the index that represents zero in current data type.
- /// </summary>
- /// <value>
- /// The index of the zero reader.
- /// </value>
- protected virtual int ZeroReaderIndex
- {
- get
- {
- return 0;
- }
- }
- #if TUNING
- /// <summary>
- /// Gets the size of the message in bytes.
- /// </summary>
- /// <value>
- /// The size of the messages in bytes.
- /// </value>
- protected virtual int BufferCapacity
- {
- get { return 0; }
- }
- #endif
- /// <summary>
- /// Gets data bytes array
- /// </summary>
- /// <returns>Byte array representation of data structure.</returns>
- public
- #if !TUNING
- virtual
- #endif
- byte[] GetBytes()
- {
- #if TUNING
- var messageLength = BufferCapacity;
- var capacity = messageLength != -1 ? messageLength : DefaultCapacity;
- var dataStream = new SshDataStream(capacity);
- WriteBytes(dataStream);
- return dataStream.ToArray();
- #else
- _data = new List<byte>();
- SaveData();
- return _data.ToArray();
- #endif
- }
- #if TUNING
- /// <summary>
- /// Writes the current message to the specified <see cref="SshDataStream"/>.
- /// </summary>
- /// <param name="stream">The <see cref="SshDataStream"/> to write the message to.</param>
- protected virtual void WriteBytes(SshDataStream stream)
- {
- _stream = stream;
- SaveData();
- }
- #endif
- internal T OfType<T>() where T : SshData, new()
- {
- var result = new T();
- #if TUNING
- result.LoadBytes(_loadedData, _offset);
- #else
- result.LoadBytes(_loadedData);
- #endif
- result.LoadData();
- return result;
- }
- /// <summary>
- /// Loads data from specified bytes.
- /// </summary>
- /// <param name="value">Bytes array.</param>
- /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
- public void Load(byte[] value)
- {
- #if TUNING
- Load(value, 0);
- #else
- if (value == null)
- throw new ArgumentNullException("value");
- LoadBytes(value);
- LoadData();
- #endif
- }
- #if TUNING
- /// <summary>
- /// Loads data from the specified buffer.
- /// </summary>
- /// <param name="value">Bytes array.</param>
- /// <param name="offset">The zero-based offset in <paramref name="value"/> at which to begin reading SSH data.</param>
- /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
- public void Load(byte[] value, int offset)
- {
- LoadBytes(value, offset);
- LoadData();
- }
- #endif
- /// <summary>
- /// Called when type specific data need to be loaded.
- /// </summary>
- protected abstract void LoadData();
- /// <summary>
- /// Called when type specific data need to be saved.
- /// </summary>
- protected abstract void SaveData();
- /// <summary>
- /// Loads data bytes into internal buffer.
- /// </summary>
- /// <param name="bytes">The bytes.</param>
- /// <exception cref="ArgumentNullException"><paramref name="bytes"/> is null.</exception>
- protected void LoadBytes(byte[] bytes)
- {
- #if TUNING
- LoadBytes(bytes, 0);
- #else
- // Note about why I check for null here, and in Load(byte[]) in this class.
- // This method is called by several other classes, such as SshNet.Messages.Message, SshNet.Sftp.SftpMessage.
- if (bytes == null)
- throw new ArgumentNullException("bytes");
- ResetReader();
- _loadedData = bytes;
- _data = new List<byte>(bytes);
- #endif
- }
- #if TUNING
- /// <summary>
- /// Loads data bytes into internal buffer.
- /// </summary>
- /// <param name="bytes">The bytes.</param>
- /// <param name="offset">The zero-based offset in <paramref name="bytes"/> at which to begin reading SSH data.</param>
- /// <exception cref="ArgumentNullException"><paramref name="bytes"/> is null.</exception>
- protected void LoadBytes(byte[] bytes, int offset)
- {
- if (bytes == null)
- throw new ArgumentNullException("bytes");
- _loadedData = bytes;
- _offset = offset;
- _stream = new SshDataStream(bytes);
- ResetReader();
- }
- #endif
- /// <summary>
- /// Resets internal data reader index.
- /// </summary>
- protected void ResetReader()
- {
- #if TUNING
- _stream.Position = ZeroReaderIndex + _offset;
- #else
- _readerIndex = ZeroReaderIndex; // Set to 1 to skip first byte which specifies message type
- #endif
- }
- /// <summary>
- /// Reads all data left in internal buffer at current position.
- /// </summary>
- /// <returns>An array of bytes containing the remaining data in the internal buffer.</returns>
- protected byte[] ReadBytes()
- {
- #if TUNING
- var bytesLength = (int) (_stream.Length - _stream.Position);
- var data = new byte[bytesLength];
- _stream.Read(data, 0, bytesLength);
- return data;
- #else
- var data = new byte[_data.Count - _readerIndex];
- _data.CopyTo(_readerIndex, data, 0, data.Length);
- return data;
- #endif
- }
- /// <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>
- protected byte[] ReadBytes(int length)
- {
- // Note that this also prevents allocating non-relevant lengths, such as if length is greater than _data.Count but less than int.MaxValue.
- // For the nerds, the condition translates to: if (length > data.Count && length < int.MaxValue)
- // Which probably would cause all sorts of exception, most notably OutOfMemoryException.
- #if TUNING
- var data = new byte[length];
- var bytesRead = _stream.Read(data, 0, length);
- if (bytesRead < length)
- throw new ArgumentOutOfRangeException("length");
- return data;
- #else
- if (length > _data.Count)
- throw new ArgumentOutOfRangeException("length");
- var result = new byte[length];
- _data.CopyTo(_readerIndex, result, 0, length);
- _readerIndex += length;
- return result;
- #endif
- }
- /// <summary>
- /// Reads next byte data type from internal buffer.
- /// </summary>
- /// <returns>Byte read.</returns>
- protected byte ReadByte()
- {
- #if TUNING
- var byteRead = _stream.ReadByte();
- if (byteRead == -1)
- throw new InvalidOperationException("Attempt to read past the end of the SSH data stream.");
- return (byte) byteRead;
- #else
- return ReadBytes(1).FirstOrDefault();
- #endif
- }
- /// <summary>
- /// Reads next boolean data type from internal buffer.
- /// </summary>
- /// <returns>Boolean read.</returns>
- protected bool ReadBoolean()
- {
- return ReadByte() != 0;
- }
- /// <summary>
- /// Reads next uint16 data type from internal buffer.
- /// </summary>
- /// <returns>uint16 read</returns>
- protected ushort ReadUInt16()
- {
- var data = ReadBytes(2);
- return (ushort)(data[0] << 8 | data[1]);
- }
- /// <summary>
- /// Reads next uint32 data type from internal buffer.
- /// </summary>
- /// <returns>uint32 read</returns>
- protected uint ReadUInt32()
- {
- var data = ReadBytes(4);
- return (uint)(data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]);
- }
- /// <summary>
- /// Reads next uint64 data type from internal buffer.
- /// </summary>
- /// <returns>uint64 read</returns>
- protected 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]);
- }
- #if !TUNING
- /// <summary>
- /// Reads next string data type from internal buffer.
- /// </summary>
- /// <returns>string read</returns>
- protected string ReadAsciiString()
- {
- var length = ReadUInt32();
- if (length > int.MaxValue)
- {
- throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} is not supported.", int.MaxValue));
- }
- return Ascii.GetString(ReadBytes((int)length), 0, (int)length);
- }
- #endif
- /// <summary>
- /// Reads next string data type from internal buffer.
- /// </summary>
- /// <returns>string read</returns>
- protected string ReadString()
- {
- return ReadString(Utf8);
- }
- /// <summary>
- /// Reads next string data type from internal buffer.
- /// </summary>
- /// <returns>string read</returns>
- protected string ReadString(Encoding encoding)
- {
- #if TUNING
- return _stream.ReadString(encoding);
- #else
- var length = ReadUInt32();
- if (length > int.MaxValue)
- {
- throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} is not supported.", int.MaxValue));
- }
- return encoding.GetString(ReadBytes((int)length), 0, (int)length);
- #endif
- }
- #if TUNING
- /// <summary>
- /// Reads next data type as byte array from internal buffer.
- /// </summary>
- /// <returns>
- /// The bytes read.
- /// </returns>
- protected byte[] ReadBinary()
- {
- return _stream.ReadBinary();
- }
- #else
- /// <summary>
- /// Reads next string data type from internal buffer.
- /// </summary>
- /// <returns>string read</returns>
- protected byte[] ReadBinaryString()
- {
- var length = ReadUInt32();
- if (length > int.MaxValue)
- {
- throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} is not supported.", int.MaxValue));
- }
- return ReadBytes((int)length);
- }
- #endif
- /// <summary>
- /// Reads next name-list data type from internal buffer.
- /// </summary>
- /// <returns>String array or read data..</returns>
- protected string[] ReadNamesList()
- {
- var namesList = ReadString();
- return namesList.Split(',');
- }
- /// <summary>
- /// Reads next extension-pair data type from internal buffer.
- /// </summary>
- /// <returns>Extensions pair dictionary.</returns>
- protected IDictionary<string, string> ReadExtensionPair()
- {
- var result = new Dictionary<string, string>();
- while (!IsEndOfData)
- {
- var extensionName = ReadString();
- var extensionData = ReadString();
- result.Add(extensionName, extensionData);
- }
- return result;
- }
- #if TUNING
- /// <summary>
- /// Writes bytes array data into internal buffer.
- /// </summary>
- /// <param name="data">Byte array data to write.</param>
- /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception>
- protected void Write(byte[] data)
- {
- _stream.Write(data);
- }
- #else
- /// <summary>
- /// Writes bytes array data into internal buffer.
- /// </summary>
- /// <param name="data">Byte array data to write.</param>
- /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception>
- protected void Write(IEnumerable<byte> data)
- {
- _data.AddRange(data);
- }
- #endif
- #if TUNING
- /// <summary>
- /// Writes a sequence of bytes to the current SSH data stream and advances the current position
- /// within this stream by the number of bytes written.
- /// </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 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>
- protected void Write(byte[] buffer, int offset, int count)
- {
- _stream.Write(buffer, offset, count);
- }
- #endif
- /// <summary>
- /// Writes byte data into internal buffer.
- /// </summary>
- /// <param name="data">Byte data to write.</param>
- protected void Write(byte data)
- {
- #if TUNING
- _stream.WriteByte(data);
- #else
- _data.Add(data);
- #endif
- }
- /// <summary>
- /// Writes boolean data into internal buffer.
- /// </summary>
- /// <param name="data">Boolean data to write.</param>
- protected void Write(bool data)
- {
- Write(data ? (byte) 1 : (byte) 0);
- }
- /// <summary>
- /// Writes uint32 data into internal buffer.
- /// </summary>
- /// <param name="data">uint32 data to write.</param>
- protected void Write(uint data)
- {
- #if TUNING
- _stream.Write(data);
- #else
- Write(data.GetBytes());
- #endif
- }
- /// <summary>
- /// Writes uint64 data into internal buffer.
- /// </summary>
- /// <param name="data">uint64 data to write.</param>
- protected void Write(ulong data)
- {
- #if TUNING
- _stream.Write(data);
- #else
- Write(data.GetBytes());
- #endif
- }
- /// <summary>
- /// Writes string data into internal buffer using default encoding.
- /// </summary>
- /// <param name="data">string data to write.</param>
- /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception>
- protected void Write(string data)
- {
- Write(data, Utf8);
- }
- /// <summary>
- /// Writes string data into internal buffer using the specified encoding.
- /// </summary>
- /// <param name="data">string data to write.</param>
- /// <param name="encoding">The character encoding to use.</param>
- /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception>
- /// <exception cref="ArgumentNullException"><paramref name="encoding"/> is null.</exception>
- protected void Write(string data, Encoding encoding)
- {
- #if TUNING
- _stream.Write(data, encoding);
- #else
- if (data == null)
- throw new ArgumentNullException("data");
- if (encoding == null)
- throw new ArgumentNullException("encoding");
- var bytes = encoding.GetBytes(data);
- Write((uint)bytes.Length);
- Write(bytes);
- #endif
- }
- #if TUNING
- /// <summary>
- /// Writes data into internal buffer.
- /// </summary>
- /// <param name="buffer">The data to write.</param>
- /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
- protected void WriteBinaryString(byte[] buffer)
- {
- _stream.WriteBinary(buffer);
- }
- /// <summary>
- /// Writes data into internal buffer.
- /// </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>
- protected void WriteBinary(byte[] buffer, int offset, int count)
- {
- _stream.WriteBinary(buffer, offset, count);
- }
- #else
- /// <summary>
- /// Writes string data into internal buffer.
- /// </summary>
- /// <param name="data">string data to write.</param>
- /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception>
- protected void WriteBinaryString(byte[] data)
- {
- if (data == null)
- throw new ArgumentNullException("data");
- Write((uint)data.Length);
- _data.AddRange(data);
- }
- #endif
- /// <summary>
- /// Writes mpint data into internal buffer.
- /// </summary>
- /// <param name="data">mpint data to write.</param>
- protected void Write(BigInteger data)
- {
- #if TUNING
- _stream.Write(data);
- #else
- var bytes = data.ToByteArray().Reverse().ToList();
- Write((uint)bytes.Count);
- Write(bytes);
- #endif
- }
- /// <summary>
- /// Writes name-list data into internal buffer.
- /// </summary>
- /// <param name="data">name-list data to write.</param>
- protected void Write(string[] data)
- {
- Write(string.Join(",", data), Ascii);
- }
- /// <summary>
- /// Writes extension-pair data into internal buffer.
- /// </summary>
- /// <param name="data">extension-pair data to write.</param>
- protected void Write(IDictionary<string, string> data)
- {
- foreach (var item in data)
- {
- Write(item.Key, Ascii);
- Write(item.Value, Ascii);
- }
- }
- }
- }
|