Extensions
Collection of different extension method
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
using Renci.SshNet.Messages;
using Renci.SshNet.Messages.Connection;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Sockets;
using System.Text;
namespace Renci.SshNet
{
internal static class Extensions
{
public static bool IsNullOrWhiteSpace(this string value)
{
if (string.IsNullOrEmpty(value))
return true;
return value.All(char.IsWhiteSpace);
}
internal static byte[] ToArray(this GlobalRequestName globalRequestName)
{
switch (globalRequestName) {
case GlobalRequestName.TcpIpForward:
return SshData.Ascii.GetBytes("tcpip-forward");
case GlobalRequestName.CancelTcpIpForward:
return SshData.Ascii.GetBytes("cancel-tcpip-forward");
default:
throw new NotSupportedException($"""{globalRequestName}""");
}
}
internal static byte[] ToArray(this ServiceName serviceName)
{
switch (serviceName) {
case ServiceName.UserAuthentication:
return SshData.Ascii.GetBytes("ssh-userauth");
case ServiceName.Connection:
return SshData.Ascii.GetBytes("ssh-connection");
default:
throw new NotSupportedException($"""{serviceName}""");
}
}
internal static ServiceName ToServiceName(this byte[] data)
{
string string = SshData.Ascii.GetString(data, 0, data.Length);
if (string == "ssh-userauth")
return ServiceName.UserAuthentication;
if (string == "ssh-connection")
return ServiceName.Connection;
throw new NotSupportedException($"""{string}""");
}
internal static GlobalRequestName ToGlobalRequestName(this byte[] data)
{
string string = SshData.Ascii.GetString(data, 0, data.Length);
if (string == "tcpip-forward")
return GlobalRequestName.TcpIpForward;
if (string == "cancel-tcpip-forward")
return GlobalRequestName.CancelTcpIpForward;
throw new NotSupportedException($"""{string}""");
}
internal static BigInteger ToBigInteger(this byte[] data)
{
byte[] array = new byte[data.Length];
Buffer.BlockCopy(data, 0, array, 0, data.Length);
return new BigInteger(array.Reverse());
}
internal static T[] Reverse<T>(this T[] array)
{
Array.Reverse((Array)array);
return array;
}
internal static void DebugPrint(this IEnumerable<byte> bytes)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (byte byte in bytes) {
stringBuilder.AppendFormat(CultureInfo.CurrentCulture, "0x{0:x2}, ", new object[1] {
byte
});
}
}
internal static T CreateInstance<T>(this Type type) where T : class
{
if ((object)type == null)
return null;
return Activator.CreateInstance(type) as T;
}
internal static byte[] GetBytes(this ushort value)
{
return new byte[2] {
(byte)(value >> 8),
(byte)(value & 255)
};
}
internal static byte[] GetBytes(this uint value)
{
byte[] array = new byte[4];
value.Write(array, 0);
return array;
}
internal static void Write(this uint value, byte[] buffer, int offset)
{
buffer[offset++] = (byte)(value >> 24);
buffer[offset++] = (byte)(value >> 16);
buffer[offset++] = (byte)(value >> 8);
buffer[offset] = (byte)(value & 255);
}
internal static byte[] GetBytes(this ulong value)
{
return new byte[8] {
(byte)(value >> 56),
(byte)(value >> 48),
(byte)(value >> 40),
(byte)(value >> 32),
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)(value & 255)
};
}
internal static byte[] GetBytes(this long value)
{
return new byte[8] {
(byte)(value >> 56),
(byte)(value >> 48),
(byte)(value >> 40),
(byte)(value >> 32),
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)(value & 255)
};
}
internal static void ValidatePort(this uint value, string argument)
{
if (value > 65535)
throw new ArgumentOutOfRangeException(argument, string.Format(CultureInfo.InvariantCulture, "Specified value cannot be greater than {0}.", new object[1] {
65535
}));
}
internal static void ValidatePort(this int value, string argument)
{
if (value < 0)
throw new ArgumentOutOfRangeException(argument, string.Format(CultureInfo.InvariantCulture, "Specified value cannot be less than {0}.", new object[1] {
0
}));
if (value > 65535)
throw new ArgumentOutOfRangeException(argument, string.Format(CultureInfo.InvariantCulture, "Specified value cannot be greater than {0}.", new object[1] {
65535
}));
}
public static byte[] Take(this byte[] value, int offset, int count)
{
if (value == null)
throw new ArgumentNullException("value");
if (count == 0)
return Array<byte>.Empty;
if (offset == 0 && value.Length == count)
return value;
byte[] array = new byte[count];
Buffer.BlockCopy(value, offset, array, 0, count);
return array;
}
public static byte[] Take(this byte[] value, int count)
{
if (value == null)
throw new ArgumentNullException("value");
if (count == 0)
return Array<byte>.Empty;
if (value.Length == count)
return value;
byte[] array = new byte[count];
Buffer.BlockCopy(value, 0, array, 0, count);
return array;
}
public static bool IsEqualTo(this byte[] left, byte[] right)
{
if (left == null)
throw new ArgumentNullException("left");
if (right == null)
throw new ArgumentNullException("right");
if (left == right)
return true;
if (left.Length != right.Length)
return false;
for (int i = 0; i < left.Length; i++) {
if (left[i] != right[i])
return false;
}
return true;
}
public static byte[] TrimLeadingZeros(this byte[] value)
{
if (value == null)
throw new ArgumentNullException("value");
for (int i = 0; i < value.Length; i++) {
if (value[i] != 0) {
if (i == 0)
return value;
int num = value.Length - i;
byte[] array = new byte[num];
Buffer.BlockCopy(value, i, array, 0, num);
return array;
}
}
return value;
}
public static byte[] Concat(this byte[] first, byte[] second)
{
if (first == null || first.Length == 0)
return second;
if (second == null || second.Length == 0)
return first;
byte[] array = new byte[first.Length + second.Length];
Buffer.BlockCopy(first, 0, array, 0, first.Length);
Buffer.BlockCopy(second, 0, array, first.Length, second.Length);
return array;
}
internal static bool CanRead(this Socket socket)
{
return SocketAbstraction.CanRead(socket);
}
internal static bool CanWrite(this Socket socket)
{
return SocketAbstraction.CanWrite(socket);
}
}
}