Skip to content

Commit 524ea8d

Browse files
committed
Remove some #ifdefs with System.Memory
1 parent 43d30ff commit 524ea8d

File tree

3 files changed

+4
-173
lines changed

3 files changed

+4
-173
lines changed

src/Renci.SshNet/Common/Extensions.cs

-60
Original file line numberDiff line numberDiff line change
@@ -235,29 +235,7 @@ public static bool IsEqualTo(this byte[] left, byte[] right)
235235
throw new ArgumentNullException(nameof(right));
236236
}
237237

238-
if (left == right)
239-
{
240-
return true;
241-
}
242-
243-
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
244238
return left.AsSpan().SequenceEqual(right);
245-
#else
246-
if (left.Length != right.Length)
247-
{
248-
return false;
249-
}
250-
251-
for (var i = 0; i < left.Length; i++)
252-
{
253-
if (left[i] != right[i])
254-
{
255-
return false;
256-
}
257-
}
258-
259-
return true;
260-
#endif
261239
}
262240

263241
/// <summary>
@@ -297,44 +275,6 @@ public static byte[] TrimLeadingZeros(this byte[] value)
297275
return value;
298276
}
299277

300-
#if NETFRAMEWORK || NETSTANDARD2_0
301-
public static int IndexOf(this byte[] array, byte[] value, int startIndex, int count)
302-
{
303-
if (value.Length > count)
304-
{
305-
return -1;
306-
}
307-
308-
if (value.Length == 0)
309-
{
310-
return 0;
311-
}
312-
313-
for (var i = startIndex; i < startIndex + count - value.Length + 1; i++)
314-
{
315-
if (MatchesAtIndex(i))
316-
{
317-
return i - startIndex;
318-
}
319-
}
320-
321-
return -1;
322-
323-
bool MatchesAtIndex(int i)
324-
{
325-
for (var j = 0; j < value.Length; j++)
326-
{
327-
if (array[i + j] != value[j])
328-
{
329-
return false;
330-
}
331-
}
332-
333-
return true;
334-
}
335-
}
336-
#endif
337-
338278
/// <summary>
339279
/// Pads with leading zeros if needed.
340280
/// </summary>

src/Renci.SshNet/Security/Cryptography/Ciphers/AesCipher.CtrImpl.cs

-94
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
32
using System.Buffers.Binary;
43
using System.Numerics;
5-
#endif
64
using System.Security.Cryptography;
75

86
namespace Renci.SshNet.Security.Cryptography.Ciphers
@@ -15,13 +13,8 @@ private sealed class CtrImpl : BlockCipher, IDisposable
1513

1614
private readonly ICryptoTransform _encryptor;
1715

18-
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
1916
private ulong _ivUpper; // The upper 64 bits of the IV
2017
private ulong _ivLower; // The lower 64 bits of the IV
21-
#else
22-
// The same on netfx
23-
private readonly uint[] _packedIV;
24-
#endif
2518

2619
public CtrImpl(
2720
byte[] key,
@@ -35,12 +28,8 @@ public CtrImpl(
3528
_aes = aes;
3629
_encryptor = aes.CreateEncryptor();
3730

38-
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
3931
_ivLower = BinaryPrimitives.ReadUInt64BigEndian(iv.AsSpan(8));
4032
_ivUpper = BinaryPrimitives.ReadUInt64BigEndian(iv);
41-
#else
42-
_packedIV = GetPackedIV(iv);
43-
#endif
4433
}
4534

4635
public override byte[] Encrypt(byte[] input, int offset, int length)
@@ -85,8 +74,6 @@ private byte[] CTREncryptDecrypt(byte[] data, int offset, int length)
8574
return buffer;
8675
}
8776

88-
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
89-
9077
// creates the Counter array filled with incrementing copies of IV
9178
private void CTRCreateCounterArray(byte[] buffer)
9279
{
@@ -118,87 +105,6 @@ private static void ArrayXOR(byte[] buffer, byte[] data, int offset, int length)
118105
}
119106
}
120107

121-
#else
122-
// creates the Counter array filled with incrementing copies of IV
123-
private void CTRCreateCounterArray(byte[] buffer)
124-
{
125-
// fill array with IV, increment by 1 for each copy
126-
var words = buffer.Length / 4;
127-
var counter = new uint[words];
128-
for (var i = 0; i < words; i += 4)
129-
{
130-
// write IV to buffer (big endian)
131-
counter[i] = _packedIV[0];
132-
counter[i + 1] = _packedIV[1];
133-
counter[i + 2] = _packedIV[2];
134-
counter[i + 3] = _packedIV[3];
135-
136-
// increment IV (little endian)
137-
if (_packedIV[3] < 0xFF000000u)
138-
{
139-
_packedIV[3] += 0x01000000u;
140-
}
141-
else
142-
{
143-
var j = 3;
144-
do
145-
{
146-
_packedIV[j] = SwapEndianness(SwapEndianness(_packedIV[j]) + 1);
147-
}
148-
while (_packedIV[j] == 0 && --j >= 0);
149-
}
150-
}
151-
152-
// copy uint[] to byte[]
153-
Buffer.BlockCopy(counter, 0, buffer, 0, buffer.Length);
154-
}
155-
156-
// XOR 2 arrays using Uint[] and blockcopy
157-
private static void ArrayXOR(byte[] buffer, byte[] data, int offset, int length)
158-
{
159-
var words = length / 4;
160-
if (length % 4 != 0)
161-
{
162-
words++;
163-
}
164-
165-
// convert original data to words
166-
var datawords = new uint[words];
167-
Buffer.BlockCopy(data, offset, datawords, 0, length);
168-
169-
// convert encrypted IV counter to words
170-
var bufferwords = new uint[words];
171-
Buffer.BlockCopy(buffer, 0, bufferwords, 0, length);
172-
173-
// XOR encrypted Counter with input data
174-
for (var i = 0; i < words; i++)
175-
{
176-
bufferwords[i] = bufferwords[i] ^ datawords[i];
177-
}
178-
179-
// copy uint[] to byte[]
180-
Buffer.BlockCopy(bufferwords, 0, buffer, 0, length);
181-
}
182-
183-
// pack the IV into an array of uint[4]
184-
private static uint[] GetPackedIV(byte[] iv)
185-
{
186-
var packedIV = new uint[4];
187-
packedIV[0] = BitConverter.ToUInt32(iv, 0);
188-
packedIV[1] = BitConverter.ToUInt32(iv, 4);
189-
packedIV[2] = BitConverter.ToUInt32(iv, 8);
190-
packedIV[3] = BitConverter.ToUInt32(iv, 12);
191-
192-
return packedIV;
193-
}
194-
195-
private static uint SwapEndianness(uint x)
196-
{
197-
x = (x >> 16) | (x << 16);
198-
return ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
199-
}
200-
#endif
201-
202108
private void Dispose(bool disposing)
203109
{
204110
if (disposing)

src/Renci.SshNet/ShellStream.cs

+4-19
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,7 @@ public void Expect(TimeSpan timeout, int lookback, params ExpectAction[] expectA
393393

394394
Debug.Assert(_readHead <= searchHead && searchHead <= _readTail);
395395

396-
#if NETFRAMEWORK || NETSTANDARD2_0
397-
var indexOfMatch = _readBuffer.IndexOf(expectBytes, searchHead, _readTail - searchHead);
398-
#else
399396
var indexOfMatch = _readBuffer.AsSpan(searchHead, _readTail - searchHead).IndexOf(expectBytes);
400-
#endif
401397

402398
if (indexOfMatch >= 0)
403399
{
@@ -417,7 +413,7 @@ public void Expect(TimeSpan timeout, int lookback, params ExpectAction[] expectA
417413

418414
if (timeout == Timeout.InfiniteTimeSpan)
419415
{
420-
Monitor.Wait(_sync);
416+
_ = Monitor.Wait(_sync);
421417
}
422418
else
423419
{
@@ -665,24 +661,16 @@ public IAsyncResult BeginExpect(TimeSpan timeout, int lookback, AsyncCallback? c
665661
{
666662
AssertValid();
667663

668-
#if NETFRAMEWORK || NETSTANDARD2_0
669-
var indexOfCr = _readBuffer.IndexOf(_carriageReturnBytes, _readHead, _readTail - _readHead);
670-
#else
671664
var indexOfCr = _readBuffer.AsSpan(_readHead, _readTail - _readHead).IndexOf(_carriageReturnBytes);
672-
#endif
665+
673666
if (indexOfCr >= 0)
674667
{
675668
// We have found \r. We only need to search for \n up to and just after the \r
676669
// (in order to consume \r\n if we can).
677-
#if NETFRAMEWORK || NETSTANDARD2_0
678-
var indexOfLf = indexOfCr + _carriageReturnBytes.Length + _lineFeedBytes.Length <= _readTail - _readHead
679-
? _readBuffer.IndexOf(_lineFeedBytes, _readHead, indexOfCr + _carriageReturnBytes.Length + _lineFeedBytes.Length)
680-
: _readBuffer.IndexOf(_lineFeedBytes, _readHead, indexOfCr);
681-
#else
682670
var indexOfLf = indexOfCr + _carriageReturnBytes.Length + _lineFeedBytes.Length <= _readTail - _readHead
683671
? _readBuffer.AsSpan(_readHead, indexOfCr + _carriageReturnBytes.Length + _lineFeedBytes.Length).IndexOf(_lineFeedBytes)
684672
: _readBuffer.AsSpan(_readHead, indexOfCr).IndexOf(_lineFeedBytes);
685-
#endif
673+
686674
if (indexOfLf >= 0 && indexOfLf < indexOfCr)
687675
{
688676
// If there is \n before the \r, then return up to the \n
@@ -720,11 +708,8 @@ public IAsyncResult BeginExpect(TimeSpan timeout, int lookback, AsyncCallback? c
720708
else
721709
{
722710
// There is no \r. What about \n?
723-
#if NETFRAMEWORK || NETSTANDARD2_0
724-
var indexOfLf = _readBuffer.IndexOf(_lineFeedBytes, _readHead, _readTail - _readHead);
725-
#else
726711
var indexOfLf = _readBuffer.AsSpan(_readHead, _readTail - _readHead).IndexOf(_lineFeedBytes);
727-
#endif
712+
728713
if (indexOfLf >= 0)
729714
{
730715
var returnText = _encoding.GetString(_readBuffer, _readHead, indexOfLf);

0 commit comments

Comments
 (0)