From fa68180a6433027fe7d10eb4edea08b0995d87ad Mon Sep 17 00:00:00 2001 From: Robert Hague Date: Thu, 21 Dec 2023 13:23:42 +0100 Subject: [PATCH] Add a few spans on net6.0 or greater --- src/Renci.SshNet/Common/Extensions.cs | 4 +++ src/Renci.SshNet/Common/SshData.cs | 16 ++---------- src/Renci.SshNet/Common/SshDataStream.cs | 33 ++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/Renci.SshNet/Common/Extensions.cs b/src/Renci.SshNet/Common/Extensions.cs index f364dc9f4..1d6ce3ca1 100644 --- a/src/Renci.SshNet/Common/Extensions.cs +++ b/src/Renci.SshNet/Common/Extensions.cs @@ -221,6 +221,9 @@ public static bool IsEqualTo(this byte[] left, byte[] 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; @@ -235,6 +238,7 @@ public static bool IsEqualTo(this byte[] left, byte[] right) } return true; +#endif } /// diff --git a/src/Renci.SshNet/Common/SshData.cs b/src/Renci.SshNet/Common/SshData.cs index f63a6bcb2..54b77c6d9 100644 --- a/src/Renci.SshNet/Common/SshData.cs +++ b/src/Renci.SshNet/Common/SshData.cs @@ -155,19 +155,7 @@ protected byte[] ReadBytes() /// is greater than the number of bytes available to be read. protected byte[] ReadBytes(int length) { - var data = new byte[length]; - var bytesRead = _stream.Read(data, 0, length); - -#if NET8_0_OR_GREATER - ArgumentOutOfRangeException.ThrowIfGreaterThan(length, bytesRead); -#else - if (bytesRead < length) - { - throw new ArgumentOutOfRangeException(nameof(length)); - } -#endif - - return data; + return _stream.ReadBytes(length); } /// @@ -209,7 +197,7 @@ protected bool ReadBoolean() /// Attempt to read past the end of the stream. protected ushort ReadUInt16() { - return Pack.BigEndianToUInt16(ReadBytes(2)); + return _stream.ReadUInt16(); } /// diff --git a/src/Renci.SshNet/Common/SshDataStream.cs b/src/Renci.SshNet/Common/SshDataStream.cs index 715ef29e4..9b64bed3a 100644 --- a/src/Renci.SshNet/Common/SshDataStream.cs +++ b/src/Renci.SshNet/Common/SshDataStream.cs @@ -62,8 +62,14 @@ public bool IsEndOfData /// data to write. public void Write(uint value) { +#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER + Span bytes = stackalloc byte[4]; + System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(bytes, value); + Write(bytes); +#else var bytes = Pack.UInt32ToBigEndian(value); Write(bytes, 0, bytes.Length); +#endif } /// @@ -72,8 +78,14 @@ public void Write(uint value) /// data to write. public void Write(ulong value) { +#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER + Span bytes = stackalloc byte[8]; + System.Buffers.Binary.BinaryPrimitives.WriteUInt64BigEndian(bytes, value); + Write(bytes); +#else var bytes = Pack.UInt64ToBigEndian(value); Write(bytes, 0, bytes.Length); +#endif } /// @@ -180,6 +192,24 @@ public BigInteger ReadBigInt() return new BigInteger(data.Reverse()); } + /// + /// Reads the next data type from the SSH data stream. + /// + /// + /// The read from the SSH data stream. + /// + public ushort ReadUInt16() + { +#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER + Span bytes = stackalloc byte[2]; + ReadBytes(bytes); + return System.Buffers.Binary.BinaryPrimitives.ReadUInt16BigEndian(bytes); +#else + var data = ReadBytes(2); + return Pack.BigEndianToUInt16(data); +#endif + } + /// /// Reads the next data type from the SSH data stream. /// @@ -264,11 +294,10 @@ public override byte[] ToArray() /// An array of bytes that was read from the internal buffer. /// /// is greater than the internal buffer size. - private byte[] ReadBytes(int length) + internal byte[] ReadBytes(int length) { var data = new byte[length]; var bytesRead = Read(data, 0, length); - if (bytesRead < length) { throw new ArgumentOutOfRangeException(nameof(length), string.Format(CultureInfo.InvariantCulture, "The requested length ({0}) is greater than the actual number of bytes read ({1}).", length, bytesRead));