using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Renci.SshNet.Abstractions;
using Renci.SshNet.Messages;
namespace Renci.SshNet.Common
{
///
/// Collection of different extension methods.
///
internal static partial class Extensions
{
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(string.Format("Service name '{0}' is not supported.", serviceName));
}
}
internal static ServiceName ToServiceName(this byte[] data)
{
var sshServiceName = SshData.Ascii.GetString(data, 0, data.Length);
switch (sshServiceName)
{
case "ssh-userauth":
return ServiceName.UserAuthentication;
case "ssh-connection":
return ServiceName.Connection;
default:
throw new NotSupportedException(string.Format("Service name '{0}' is not supported.", sshServiceName));
}
}
internal static BigInteger ToBigInteger(this byte[] data)
{
var reversed = new byte[data.Length];
Buffer.BlockCopy(data, 0, reversed, 0, data.Length);
return new BigInteger(reversed.Reverse());
}
///
/// Initializes a new instance of the structure using the SSH BigNum2 Format.
///
public static BigInteger ToBigInteger2(this byte[] data)
{
if ((data[0] & (1 << 7)) != 0)
{
var buf = new byte[data.Length + 1];
Buffer.BlockCopy(data, 0, buf, 1, data.Length);
data = buf;
}
return data.ToBigInteger();
}
///
/// Reverses the sequence of the elements in the entire one-dimensional .
///
/// The one-dimensional to reverse.
///
/// The with its elements reversed.
///
internal static T[] Reverse(this T[] array)
{
Array.Reverse(array);
return array;
}
///
/// Prints out the specified bytes.
///
/// The bytes.
internal static void DebugPrint(this IEnumerable bytes)
{
var sb = new StringBuilder();
foreach (var b in bytes)
{
_ = sb.AppendFormat(CultureInfo.CurrentCulture, "0x{0:x2}, ", b);
}
Debug.WriteLine(sb.ToString());
}
internal static void ValidatePort(this uint value, string argument)
{
if (value > IPEndPoint.MaxPort)
{
throw new ArgumentOutOfRangeException(argument,
string.Format(CultureInfo.InvariantCulture, "Specified value cannot be greater than {0}.", IPEndPoint.MaxPort));
}
}
internal static void ValidatePort(this int value, string argument)
{
if (value < IPEndPoint.MinPort)
{
throw new ArgumentOutOfRangeException(argument, string.Format(CultureInfo.InvariantCulture, "Specified value cannot be less than {0}.", IPEndPoint.MinPort));
}
if (value > IPEndPoint.MaxPort)
{
throw new ArgumentOutOfRangeException(argument, string.Format(CultureInfo.InvariantCulture, "Specified value cannot be greater than {0}.", IPEndPoint.MaxPort));
}
}
///
/// Returns a specified number of contiguous bytes from a given offset.
///
/// The array to return a number of bytes from.
/// The zero-based offset in at which to begin taking bytes.
/// The number of bytes to take from .
///
/// A array that contains the specified number of bytes at the specified offset
/// of the input array.
///
/// is .
///
/// When is zero and equals the length of ,
/// then is returned.
///
public static byte[] Take(this byte[] value, int offset, int count)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
if (count == 0)
{
return Array.Empty();
}
if (offset == 0 && value.Length == count)
{
return value;
}
var taken = new byte[count];
Buffer.BlockCopy(value, offset, taken, 0, count);
return taken;
}
///
/// Returns a specified number of contiguous bytes from the start of the specified byte array.
///
/// The array to return a number of bytes from.
/// The number of bytes to take from .
///
/// A array that contains the specified number of bytes at the start of the input array.
///
/// is .
///
/// When equals the length of , then
/// is returned.
///
public static byte[] Take(this byte[] value, int count)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
if (count == 0)
{
return Array.Empty();
}
if (value.Length == count)
{
return value;
}
var taken = new byte[count];
Buffer.BlockCopy(value, 0, taken, 0, count);
return taken;
}
public static bool IsEqualTo(this byte[] left, byte[] right)
{
if (left is null)
{
throw new ArgumentNullException(nameof(left));
}
if (right is null)
{
throw new ArgumentNullException(nameof(right));
}
if (left == right)
{
return true;
}
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
return left.AsSpan().SequenceEqual(right);
#else
if (left.Length != right.Length)
{
return false;
}
for (var i = 0; i < left.Length; i++)
{
if (left[i] != right[i])
{
return false;
}
}
return true;
#endif
}
///
/// Trims the leading zero from a byte array.
///
/// The value.
///
/// without leading zeros.
///
public static byte[] TrimLeadingZeros(this byte[] value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
for (var i = 0; i < value.Length; i++)
{
if (value[i] == 0)
{
continue;
}
// if the first byte is non-zero, then we return the byte array as is
if (i == 0)
{
return value;
}
var remainingBytes = value.Length - i;
var cleaned = new byte[remainingBytes];
Buffer.BlockCopy(value, i, cleaned, 0, remainingBytes);
return cleaned;
}
return value;
}
///
/// Pads with leading zeros if needed.
///
/// The data.
/// The length to pad to.
public static byte[] Pad(this byte[] data, int length)
{
if (length <= data.Length)
{
return data;
}
var newData = new byte[length];
Buffer.BlockCopy(data, 0, newData, newData.Length - data.Length, data.Length);
return newData;
}
public static byte[] Concat(this byte[] first, byte[] second)
{
if (first is null || first.Length == 0)
{
return second;
}
if (second is null || second.Length == 0)
{
return first;
}
var concat = new byte[first.Length + second.Length];
Buffer.BlockCopy(first, 0, concat, 0, first.Length);
Buffer.BlockCopy(second, 0, concat, first.Length, second.Length);
return concat;
}
internal static bool CanRead(this Socket socket)
{
return SocketAbstraction.CanRead(socket);
}
internal static bool CanWrite(this Socket socket)
{
return SocketAbstraction.CanWrite(socket);
}
internal static bool IsConnected(this Socket socket)
{
if (socket is null)
{
return false;
}
return socket.Connected;
}
}
}