using System;
using System.Collections.Generic;
using System.Text;
namespace Renci.SshNet.Common
{
///
/// Base ssh data serialization type
///
public abstract class SshData
{
internal const int DefaultCapacity = 64;
#if FEATURE_ENCODING_ASCII
internal static readonly Encoding Ascii = Encoding.ASCII;
#else
internal static readonly Encoding Ascii = new ASCIIEncoding();
#endif
internal static readonly Encoding Utf8 = Encoding.UTF8;
private SshDataStream _stream;
///
/// Gets the underlying that is used for reading and writing SSH data.
///
///
/// The underlying that is used for reading and writing SSH data.
///
protected SshDataStream DataStream
{
get { return _stream; }
}
///
/// Gets a value indicating whether all data from the buffer has been read.
///
///
/// true if this instance is end of data; otherwise, false.
///
protected bool IsEndOfData
{
get
{
return _stream.Position >= _stream.Length;
}
}
private byte[] _loadedData;
private int _offset;
private int _count;
///
/// Gets the size of the message in bytes.
///
///
/// The size of the messages in bytes.
///
protected virtual int BufferCapacity
{
get { return 0; }
}
///
/// Gets data bytes array
///
/// Byte array representation of data structure.
public byte[] GetBytes()
{
var messageLength = BufferCapacity;
var capacity = messageLength != -1 ? messageLength : DefaultCapacity;
var dataStream = new SshDataStream(capacity);
WriteBytes(dataStream);
return dataStream.ToArray();
}
///
/// Writes the current message to the specified .
///
/// The to write the message to.
protected virtual void WriteBytes(SshDataStream stream)
{
_stream = stream;
SaveData();
}
internal T OfType() where T : SshData, new()
{
var result = new T();
result.Load(_loadedData, _offset, _count);
return result;
}
///
/// Loads data from specified bytes.
///
/// Bytes array.
/// is null.
public void Load(byte[] data)
{
if (data == null)
throw new ArgumentNullException("data");
LoadInternal(data, 0, data.Length);
}
///
/// Loads data from the specified buffer.
///
/// Bytes array.
/// The zero-based offset in at which to begin reading SSH data.
/// The number of bytes to load.
/// is null.
public void Load(byte[] data, int offset, int count)
{
if (data == null)
throw new ArgumentNullException("data");
LoadInternal(data, offset, count);
}
private void LoadInternal(byte[] value, int offset, int count)
{
LoadBytes(value, offset, count);
LoadData();
}
///
/// Called when type specific data need to be loaded.
///
protected abstract void LoadData();
///
/// Called when type specific data need to be saved.
///
protected abstract void SaveData();
///
/// Loads data bytes into internal buffer.
///
/// The bytes.
/// The zero-based offset in at which to begin reading SSH data.
/// The number of bytes to load.
private void LoadBytes(byte[] bytes, int offset, int count)
{
_loadedData = bytes;
_offset = offset;
_count = count;
_stream = new SshDataStream(bytes, _offset, count);
}
///
/// Reads all data left in internal buffer at current position.
///
/// An array of bytes containing the remaining data in the internal buffer.
protected byte[] ReadBytes()
{
var bytesLength = (int) (_stream.Length - _stream.Position);
var data = new byte[bytesLength];
_stream.Read(data, 0, bytesLength);
return data;
}
///
/// Reads next specified number of bytes data type from internal buffer.
///
/// Number of bytes to read.
/// An array of bytes that was read from the internal buffer.
/// is greater than the internal buffer size.
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.
var data = new byte[length];
var bytesRead = _stream.Read(data, 0, length);
if (bytesRead < length)
throw new ArgumentOutOfRangeException("length");
return data;
}
///
/// Reads next byte data type from internal buffer.
///
/// Byte read.
protected byte ReadByte()
{
var byteRead = _stream.ReadByte();
if (byteRead == -1)
throw new InvalidOperationException("Attempt to read past the end of the SSH data stream.");
return (byte) byteRead;
}
///
/// Reads next boolean data type from internal buffer.
///
/// Boolean read.
protected bool ReadBoolean()
{
return ReadByte() != 0;
}
///
/// Reads next uint16 data type from internal buffer.
///
/// uint16 read
protected ushort ReadUInt16()
{
var data = ReadBytes(2);
return (ushort)(data[0] << 8 | data[1]);
}
///
/// Reads next uint32 data type from internal buffer.
///
/// uint32 read
protected uint ReadUInt32()
{
var data = ReadBytes(4);
return (uint)(data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]);
}
///
/// Reads next uint64 data type from internal buffer.
///
/// uint64 read
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]);
}
///
/// Reads next string data type from internal buffer using the specific encoding.
///
///
/// The read.
///
protected string ReadString(Encoding encoding)
{
return _stream.ReadString(encoding);
}
///
/// Reads next data type as byte array from internal buffer.
///
///
/// The bytes read.
///
protected byte[] ReadBinary()
{
return _stream.ReadBinary();
}
///
/// Reads next name-list data type from internal buffer.
///
///
/// String array or read data.
///
protected string[] ReadNamesList()
{
var namesList = ReadString(Ascii);
return namesList.Split(',');
}
///
/// Reads next extension-pair data type from internal buffer.
///
/// Extensions pair dictionary.
protected IDictionary ReadExtensionPair()
{
var result = new Dictionary();
while (!IsEndOfData)
{
var extensionName = ReadString(Ascii);
var extensionData = ReadString(Ascii);
result.Add(extensionName, extensionData);
}
return result;
}
///
/// Writes bytes array data into internal buffer.
///
/// Byte array data to write.
/// is null.
protected void Write(byte[] data)
{
_stream.Write(data);
}
///
/// 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.
///
/// An array of bytes. This method write bytes from buffer to the current SSH data stream.
/// The zero-based offset in at which to begin writing bytes to the SSH data stream.
/// The number of bytes to be written to the current SSH data stream.
/// is null.
/// The sum of and is greater than the buffer length.
/// or is negative.
protected void Write(byte[] buffer, int offset, int count)
{
_stream.Write(buffer, offset, count);
}
///
/// Writes data into internal buffer.
///
/// data to write.
protected void Write(byte data)
{
_stream.WriteByte(data);
}
///
/// Writes into internal buffer.
///
/// data to write.
protected void Write(bool data)
{
Write(data ? (byte) 1 : (byte) 0);
}
///
/// Writes data into internal buffer.
///
/// data to write.
protected void Write(uint data)
{
_stream.Write(data);
}
///
/// Writes data into internal buffer.
///
/// data to write.
protected void Write(ulong data)
{
_stream.Write(data);
}
///
/// Writes data into internal buffer using default encoding.
///
/// data to write.
/// is null.
protected void Write(string data)
{
Write(data, Utf8);
}
///
/// Writes data into internal buffer using the specified encoding.
///
/// data to write.
/// The character encoding to use.
/// is null.
/// is null.
protected void Write(string data, Encoding encoding)
{
_stream.Write(data, encoding);
}
///
/// Writes data into internal buffer.
///
/// The data to write.
/// is null.
protected void WriteBinaryString(byte[] buffer)
{
_stream.WriteBinary(buffer);
}
///
/// Writes data into internal buffer.
///
/// An array of bytes. This method write bytes from buffer to the current SSH data stream.
/// The zero-based byte offset in at which to begin writing bytes to the SSH data stream.
/// The number of bytes to be written to the current SSH data stream.
/// is null.
/// The sum of and is greater than the buffer length.
/// or is negative.
protected void WriteBinary(byte[] buffer, int offset, int count)
{
_stream.WriteBinary(buffer, offset, count);
}
///
/// Writes mpint data into internal buffer.
///
/// mpint data to write.
protected void Write(BigInteger data)
{
_stream.Write(data);
}
///
/// Writes name-list data into internal buffer.
///
/// name-list data to write.
protected void Write(string[] data)
{
Write(string.Join(",", data), Ascii);
}
///
/// Writes extension-pair data into internal buffer.
///
/// extension-pair data to write.
protected void Write(IDictionary data)
{
foreach (var item in data)
{
Write(item.Key, Ascii);
Write(item.Value, Ascii);
}
}
}
}