using System;
using System.Collections.Generic;
using System.Text;
namespace Renci.SshNet.Common
{
///
/// Base ssh data serialization type.
///
#pragma warning disable CA1001 // Types that own disposable fields should be disposable
public abstract class SshData
#pragma warning restore CA1001 // Types that own disposable fields should be disposable
{
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.
///
///
/// if this instance is end of data; otherwise, .
///
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;
using (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 .
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 .
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 number of bytes available to be read.
protected byte[] ReadBytes(int length)
{
return _stream.ReadBytes(length);
}
///
/// Reads next byte data type from internal buffer.
///
///
/// The read.
///
/// Attempt to read past the end of the stream.
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.
///
/// Attempt to read past the end of the stream.
protected bool ReadBoolean()
{
return ReadByte() != 0;
}
///
/// Reads the next from the internal buffer.
///
///
/// The that was read.
///
/// Attempt to read past the end of the stream.
protected ushort ReadUInt16()
{
return _stream.ReadUInt16();
}
///
/// Reads the next from the internal buffer.
///
///
/// The that was read.
///
/// Attempt to read past the end of the stream.
protected uint ReadUInt32()
{
return _stream.ReadUInt32();
}
///
/// Reads the next from the internal buffer.
///
///
/// The that was read.
///
/// Attempt to read past the end of the stream.
protected ulong ReadUInt64()
{
return _stream.ReadUInt64();
}
///
/// 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 = null)
{
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 Dictionary 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 .
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 .
/// 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 .
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 .
/// is .
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)
{
#if NET || NETSTANDARD2_1_OR_GREATER
Write(string.Join(',', data), Ascii);
#else
Write(string.Join(",", data), Ascii);
#endif // NET || NETSTANDARD2_1_OR_GREATER
}
///
/// 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 .
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 .
/// 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);
}
}
}