SshData
Base ssh data serialization type
using System;
using System.Collections.Generic;
using System.Text;
namespace Renci.SshNet.Common
{
public abstract class SshData
{
internal const int DefaultCapacity = 64;
internal static readonly Encoding Ascii = new ASCIIEncoding();
internal static readonly Encoding Utf8 = Encoding.UTF8;
private SshDataStream _stream;
private byte[] _loadedData;
private int _offset;
protected SshDataStream DataStream => _stream;
protected bool IsEndOfData => _stream.Position >= _stream.Length;
protected virtual int ZeroReaderIndex => 0;
protected virtual int BufferCapacity => 0;
public byte[] GetBytes()
{
int bufferCapacity = BufferCapacity;
SshDataStream sshDataStream = new SshDataStream((bufferCapacity != -1) ? bufferCapacity : 64);
WriteBytes(sshDataStream);
return sshDataStream.ToArray();
}
protected virtual void WriteBytes(SshDataStream stream)
{
_stream = stream;
SaveData();
}
internal T OfType<T>() where T : SshData, new
{
T val = new T();
val.LoadBytes(_loadedData, _offset);
val.LoadData();
return val;
}
public void Load(byte[] value)
{
Load(value, 0);
}
public void Load(byte[] value, int offset)
{
LoadBytes(value, offset);
LoadData();
}
protected abstract void LoadData();
protected abstract void SaveData();
protected void LoadBytes(byte[] bytes)
{
LoadBytes(bytes, 0);
}
protected void LoadBytes(byte[] bytes, int offset)
{
if (bytes == null)
throw new ArgumentNullException("bytes");
_loadedData = bytes;
_offset = offset;
_stream = new SshDataStream(bytes);
ResetReader();
}
protected void ResetReader()
{
_stream.Position = ZeroReaderIndex + _offset;
}
protected byte[] ReadBytes()
{
int num = (int)(_stream.Length - _stream.Position);
byte[] array = new byte[num];
_stream.Read(array, 0, num);
return array;
}
protected byte[] ReadBytes(int length)
{
byte[] array = new byte[length];
if (_stream.Read(array, 0, length) < length)
throw new ArgumentOutOfRangeException("length");
return array;
}
protected byte ReadByte()
{
int num = _stream.ReadByte();
if (num == -1)
throw new InvalidOperationException("Attempt to read past the end of the SSH data stream.");
return (byte)num;
}
protected bool ReadBoolean()
{
return ReadByte() != 0;
}
protected ushort ReadUInt16()
{
byte[] array = ReadBytes(2);
return (ushort)((array[0] << 8) | array[1]);
}
protected uint ReadUInt32()
{
byte[] array = ReadBytes(4);
return (uint)((array[0] << 24) | (array[1] << 16) | (array[2] << 8) | array[3]);
}
protected ulong ReadUInt64()
{
byte[] array = ReadBytes(8);
return ((ulong)array[0] << 56) | ((ulong)array[1] << 48) | ((ulong)array[2] << 40) | ((ulong)array[3] << 32) | ((ulong)array[4] << 24) | ((ulong)array[5] << 16) | ((ulong)array[6] << 8) | array[7];
}
protected string ReadString(Encoding encoding)
{
return _stream.ReadString(encoding);
}
protected byte[] ReadBinary()
{
return _stream.ReadBinary();
}
protected string[] ReadNamesList()
{
return ReadString(Ascii).Split(new char[1] {
','
});
}
protected IDictionary<string, string> ReadExtensionPair()
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
while (!IsEndOfData) {
string key = ReadString(Ascii);
string value = ReadString(Ascii);
dictionary.Add(key, value);
}
return dictionary;
}
protected void Write(byte[] data)
{
_stream.Write(data);
}
protected void Write(byte[] buffer, int offset, int count)
{
_stream.Write(buffer, offset, count);
}
protected void Write(byte data)
{
_stream.WriteByte(data);
}
protected void Write(bool data)
{
Write((byte)(data ? 1 : 0));
}
protected void Write(uint data)
{
_stream.Write(data);
}
protected void Write(ulong data)
{
_stream.Write(data);
}
protected void Write(string data)
{
Write(data, Utf8);
}
protected void Write(string data, Encoding encoding)
{
_stream.Write(data, encoding);
}
protected void WriteBinaryString(byte[] buffer)
{
_stream.WriteBinary(buffer);
}
protected void WriteBinary(byte[] buffer, int offset, int count)
{
_stream.WriteBinary(buffer, offset, count);
}
protected void Write(BigInteger data)
{
_stream.Write(data);
}
protected void Write(string[] data)
{
Write(string.Join(",", data), Ascii);
}
protected void Write(IDictionary<string, string> data)
{
foreach (KeyValuePair<string, string> datum in data) {
Write(datum.Key, Ascii);
Write(datum.Value, Ascii);
}
}
}
}