Sfoglia il codice sorgente

Remove some #ifdefs with System.Memory (#1434)

Rob Hague 1 anno fa
parent
commit
ffd43dade7

+ 0 - 60
src/Renci.SshNet/Common/Extensions.cs

@@ -235,29 +235,7 @@ namespace Renci.SshNet.Common
                 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
         }
 
         /// <summary>
@@ -297,44 +275,6 @@ namespace Renci.SshNet.Common
             return value;
         }
 
-#if NETFRAMEWORK || NETSTANDARD2_0
-        public static int IndexOf(this byte[] array, byte[] value, int startIndex, int count)
-        {
-            if (value.Length > count)
-            {
-                return -1;
-            }
-
-            if (value.Length == 0)
-            {
-                return 0;
-            }
-
-            for (var i = startIndex; i < startIndex + count - value.Length + 1; i++)
-            {
-                if (MatchesAtIndex(i))
-                {
-                    return i - startIndex;
-                }
-            }
-
-            return -1;
-
-            bool MatchesAtIndex(int i)
-            {
-                for (var j = 0; j < value.Length; j++)
-                {
-                    if (array[i + j] != value[j])
-                    {
-                        return false;
-                    }
-                }
-
-                return true;
-            }
-        }
-#endif
-
         /// <summary>
         /// Pads with leading zeros if needed.
         /// </summary>

+ 0 - 94
src/Renci.SshNet/Security/Cryptography/Ciphers/AesCipher.CtrImpl.cs

@@ -1,8 +1,6 @@
 using System;
-#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
 using System.Buffers.Binary;
 using System.Numerics;
-#endif
 using System.Security.Cryptography;
 
 namespace Renci.SshNet.Security.Cryptography.Ciphers
@@ -15,13 +13,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
 
             private readonly ICryptoTransform _encryptor;
 
-#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
             private ulong _ivUpper; // The upper 64 bits of the IV
             private ulong _ivLower; // The lower 64 bits of the IV
-#else
-            // The same on netfx
-            private readonly uint[] _packedIV;
-#endif
 
             public CtrImpl(
                 byte[] key,
@@ -35,12 +28,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
                 _aes = aes;
                 _encryptor = aes.CreateEncryptor();
 
-#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
                 _ivLower = BinaryPrimitives.ReadUInt64BigEndian(iv.AsSpan(8));
                 _ivUpper = BinaryPrimitives.ReadUInt64BigEndian(iv);
-#else
-                _packedIV = GetPackedIV(iv);
-#endif
             }
 
             public override byte[] Encrypt(byte[] input, int offset, int length)
@@ -85,8 +74,6 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
                 return buffer;
             }
 
-#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
-
             // creates the Counter array filled with incrementing copies of IV
             private void CTRCreateCounterArray(byte[] buffer)
             {
@@ -118,87 +105,6 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
                 }
             }
 
-#else
-            // creates the Counter array filled with incrementing copies of IV
-            private void CTRCreateCounterArray(byte[] buffer)
-            {
-                // fill array with IV, increment by 1 for each copy
-                var words = buffer.Length / 4;
-                var counter = new uint[words];
-                for (var i = 0; i < words; i += 4)
-                {
-                    // write IV to buffer (big endian)
-                    counter[i] = _packedIV[0];
-                    counter[i + 1] = _packedIV[1];
-                    counter[i + 2] = _packedIV[2];
-                    counter[i + 3] = _packedIV[3];
-
-                    // increment IV (little endian)
-                    if (_packedIV[3] < 0xFF000000u)
-                    {
-                        _packedIV[3] += 0x01000000u;
-                    }
-                    else
-                    {
-                        var j = 3;
-                        do
-                        {
-                            _packedIV[j] = SwapEndianness(SwapEndianness(_packedIV[j]) + 1);
-                        }
-                        while (_packedIV[j] == 0 && --j >= 0);
-                    }
-                }
-
-                // copy uint[] to byte[]
-                Buffer.BlockCopy(counter, 0, buffer, 0, buffer.Length);
-            }
-
-            // XOR 2 arrays using Uint[] and blockcopy
-            private static void ArrayXOR(byte[] buffer, byte[] data, int offset, int length)
-            {
-                var words = length / 4;
-                if (length % 4 != 0)
-                {
-                    words++;
-                }
-
-                // convert original data to words
-                var datawords = new uint[words];
-                Buffer.BlockCopy(data, offset, datawords, 0, length);
-
-                // convert encrypted IV counter to words
-                var bufferwords = new uint[words];
-                Buffer.BlockCopy(buffer, 0, bufferwords, 0, length);
-
-                // XOR encrypted Counter with input data
-                for (var i = 0; i < words; i++)
-                {
-                    bufferwords[i] = bufferwords[i] ^ datawords[i];
-                }
-
-                // copy uint[] to byte[]
-                Buffer.BlockCopy(bufferwords, 0, buffer, 0, length);
-            }
-
-            // pack the IV into an array of uint[4]
-            private static uint[] GetPackedIV(byte[] iv)
-            {
-                var packedIV = new uint[4];
-                packedIV[0] = BitConverter.ToUInt32(iv, 0);
-                packedIV[1] = BitConverter.ToUInt32(iv, 4);
-                packedIV[2] = BitConverter.ToUInt32(iv, 8);
-                packedIV[3] = BitConverter.ToUInt32(iv, 12);
-
-                return packedIV;
-            }
-
-            private static uint SwapEndianness(uint x)
-            {
-                x = (x >> 16) | (x << 16);
-                return ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
-            }
-#endif
-
             private void Dispose(bool disposing)
             {
                 if (disposing)

+ 4 - 19
src/Renci.SshNet/ShellStream.cs

@@ -393,11 +393,7 @@ namespace Renci.SshNet
 
                     Debug.Assert(_readHead <= searchHead && searchHead <= _readTail);
 
-#if NETFRAMEWORK || NETSTANDARD2_0
-                    var indexOfMatch = _readBuffer.IndexOf(expectBytes, searchHead, _readTail - searchHead);
-#else
                     var indexOfMatch = _readBuffer.AsSpan(searchHead, _readTail - searchHead).IndexOf(expectBytes);
-#endif
 
                     if (indexOfMatch >= 0)
                     {
@@ -417,7 +413,7 @@ namespace Renci.SshNet
 
                     if (timeout == Timeout.InfiniteTimeSpan)
                     {
-                        Monitor.Wait(_sync);
+                        _ = Monitor.Wait(_sync);
                     }
                     else
                     {
@@ -665,24 +661,16 @@ namespace Renci.SshNet
                 {
                     AssertValid();
 
-#if NETFRAMEWORK || NETSTANDARD2_0
-                    var indexOfCr = _readBuffer.IndexOf(_carriageReturnBytes, _readHead, _readTail - _readHead);
-#else
                     var indexOfCr = _readBuffer.AsSpan(_readHead, _readTail - _readHead).IndexOf(_carriageReturnBytes);
-#endif
+
                     if (indexOfCr >= 0)
                     {
                         // We have found \r. We only need to search for \n up to and just after the \r
                         // (in order to consume \r\n if we can).
-#if NETFRAMEWORK || NETSTANDARD2_0
-                        var indexOfLf = indexOfCr + _carriageReturnBytes.Length + _lineFeedBytes.Length <= _readTail - _readHead
-                            ? _readBuffer.IndexOf(_lineFeedBytes, _readHead, indexOfCr + _carriageReturnBytes.Length + _lineFeedBytes.Length)
-                            : _readBuffer.IndexOf(_lineFeedBytes, _readHead, indexOfCr);
-#else
                         var indexOfLf = indexOfCr + _carriageReturnBytes.Length + _lineFeedBytes.Length <= _readTail - _readHead
                             ? _readBuffer.AsSpan(_readHead, indexOfCr + _carriageReturnBytes.Length + _lineFeedBytes.Length).IndexOf(_lineFeedBytes)
                             : _readBuffer.AsSpan(_readHead, indexOfCr).IndexOf(_lineFeedBytes);
-#endif
+
                         if (indexOfLf >= 0 && indexOfLf < indexOfCr)
                         {
                             // If there is \n before the \r, then return up to the \n
@@ -720,11 +708,8 @@ namespace Renci.SshNet
                     else
                     {
                         // There is no \r. What about \n?
-#if NETFRAMEWORK || NETSTANDARD2_0
-                        var indexOfLf = _readBuffer.IndexOf(_lineFeedBytes, _readHead, _readTail - _readHead);
-#else
                         var indexOfLf = _readBuffer.AsSpan(_readHead, _readTail - _readHead).IndexOf(_lineFeedBytes);
-#endif
+
                         if (indexOfLf >= 0)
                         {
                             var returnText = _encoding.GetString(_readBuffer, _readHead, indexOfLf);