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;
internal static readonly Encoding Ascii = Encoding.ASCII;
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;
}
}
///
/// 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.
///
///
/// A 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();
}
///
/// Loads data from specified bytes.
///
/// Bytes array.
/// is null.
public void Load(byte[] data)
{
if (data is null)
{
throw new ArgumentNullException(nameof(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 is null)
{
throw new ArgumentNullException(nameof(data));
}
LoadInternal(data, offset, count);
}
private void LoadInternal(byte[] value, int offset, int count)
{
_stream = new SshDataStream(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();
///
/// 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(nameof(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 the next from the internal buffer.
///
///
/// The that was read.
///
protected bool ReadBoolean()
{
return ReadByte() != 0;
}
///
/// Reads the next from the internal buffer.
///
///
/// The that was read.
///
protected ushort ReadUInt16()
{
return Pack.BigEndianToUInt16(ReadBytes(2));
}
///
/// Reads the next from the internal buffer.
///
///
/// The that was read.
///
protected uint ReadUInt32()
{
return Pack.BigEndianToUInt32(ReadBytes(4));
}
///
/// Reads the next from the internal buffer.
///
///
/// The that was read.
///
protected ulong ReadUInt64()
{
return Pack.BigEndianToUInt64(ReadBytes(8));
}
///
/// Reads the next from the internal buffer using the specified encoding.
///
/// The character encoding to use.
///
/// The that was 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 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);
}
}
///
/// 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);
}
}
}