Skip to content

Commit

Permalink
Change several static readonly byte[] fields to be `ReadOnlySpan<by…
Browse files Browse the repository at this point in the history
…te>` properties (dotnet#32464)
  • Loading branch information
stephentoub authored Feb 18, 2020
1 parent 3701909 commit 21582d2
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace System.Security.Cryptography
{
internal sealed class RsaPaddingProcessor
{
private static readonly byte[] s_eightZeros = new byte[8];

private static readonly ConcurrentDictionary<HashAlgorithmName, RsaPaddingProcessor> s_lookup =
new ConcurrentDictionary<HashAlgorithmName, RsaPaddingProcessor>();

Expand All @@ -30,6 +28,8 @@ internal static int BytesRequiredForBitCount(int keySizeInBits)
return (int)(((uint)keySizeInBits + 7) / 8);
}

private static ReadOnlySpan<byte> EightZeros => new byte[8]; // rely on C# compiler optimization to eliminate allocation

internal int HashLength => _hLen;

internal static RsaPaddingProcessor OpenProcessor(HashAlgorithmName hashAlgorithmName)
Expand Down Expand Up @@ -314,7 +314,7 @@ internal void EncodePss(ReadOnlySpan<byte> mHash, Span<byte> destination, int ke
// 5. Let M' = an octet string of 8 zeros concat mHash concat salt
// 6. Let H = Hash(M')

hasher.AppendData(s_eightZeros);
hasher.AppendData(EightZeros);
hasher.AppendData(mHash);
hasher.AppendData(salt);

Expand Down Expand Up @@ -435,7 +435,7 @@ internal bool VerifyPss(ReadOnlySpan<byte> mHash, ReadOnlySpan<byte> em, int key
ReadOnlySpan<byte> salt = dbMask.Slice(dbMask.Length - sLen);

// 12/13. Let H' = Hash(eight zeros || mHash || salt)
hasher.AppendData(s_eightZeros);
hasher.AppendData(EightZeros);
hasher.AppendData(mHash);
hasher.AppendData(salt);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1881,7 +1881,7 @@ private void ZeroToMaxLen(int cUI4sCur) {
// The array in Shiloh. Listed here for comparison.
//private static readonly byte[] rgCLenFromPrec = new byte[] {5,5,5,5,5,5,5,5,5,9,9,9,9,9,
// 9,9,9,9,9,13,13,13,13,13,13,13,13,13,17,17,17,17,17,17,17,17,17,17};
private static readonly byte[] s_rgCLenFromPrec = new byte[]
private static ReadOnlySpan<byte> RgCLenFromPrec => new byte[] // rely on C# compiler optimization to eliminate allocation
{
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
Expand All @@ -1892,7 +1892,7 @@ private static byte CLenFromPrec(byte bPrec)
{
Debug.Assert(bPrec <= MaxPrecision && bPrec > 0, "bPrec <= MaxPrecision && bPrec > 0",
"Invalid numeric precision");
return s_rgCLenFromPrec[bPrec - 1];
return RgCLenFromPrec[bPrec - 1];
}

// check whether is zero
Expand Down
44 changes: 22 additions & 22 deletions src/libraries/System.Net.Mail/src/System/Net/Base64Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace System.Net
{
internal sealed class Base64Stream : DelegatedStream, IEncodableStream
{
private static readonly byte[] s_base64DecodeMap = new byte[]
private static ReadOnlySpan<byte> Base64DecodeMap => new byte[] // rely on C# compiler optimization to eliminate allocation
{
//0 1 2 3 4 5 6 7 8 9 A B C D E F
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 0
Expand All @@ -32,7 +32,7 @@ internal sealed class Base64Stream : DelegatedStream, IEncodableStream
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // F
};

private static readonly byte[] s_base64EncodeMap = new byte[]
private static ReadOnlySpan<byte> Base64EncodeMap => new byte[]
{
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102,
Expand Down Expand Up @@ -122,10 +122,10 @@ public override void Close()
switch (WriteState.Padding)
{
case 1:
WriteState.Append(s_base64EncodeMap[WriteState.LastBits], s_base64EncodeMap[64]);
WriteState.Append(Base64EncodeMap[WriteState.LastBits], Base64EncodeMap[64]);
break;
case 2:
WriteState.Append(s_base64EncodeMap[WriteState.LastBits], s_base64EncodeMap[64], s_base64EncodeMap[64]);
WriteState.Append(Base64EncodeMap[WriteState.LastBits], Base64EncodeMap[64], Base64EncodeMap[64]);
break;
}
WriteState.Padding = 0;
Expand Down Expand Up @@ -153,7 +153,7 @@ public unsafe int DecodeBytes(byte[] buffer, int offset, int count)
continue;
}

byte s = s_base64DecodeMap[*source];
byte s = Base64DecodeMap[*source];

if (s == InvalidBase64Value)
{
Expand Down Expand Up @@ -204,23 +204,23 @@ internal int EncodeBytes(byte[] buffer, int offset, int count, bool dontDeferFin
switch (WriteState.Padding)
{
case 2:
WriteState.Append(s_base64EncodeMap[WriteState.LastBits | ((buffer[cur] & 0xf0) >> 4)]);
WriteState.Append(Base64EncodeMap[WriteState.LastBits | ((buffer[cur] & 0xf0) >> 4)]);
if (count == 1)
{
WriteState.LastBits = (byte)((buffer[cur] & 0x0f) << 2);
WriteState.Padding = 1;
cur++;
return cur - offset;
}
WriteState.Append(s_base64EncodeMap[((buffer[cur] & 0x0f) << 2) | ((buffer[cur + 1] & 0xc0) >> 6)]);
WriteState.Append(s_base64EncodeMap[(buffer[cur + 1] & 0x3f)]);
WriteState.Append(Base64EncodeMap[((buffer[cur] & 0x0f) << 2) | ((buffer[cur + 1] & 0xc0) >> 6)]);
WriteState.Append(Base64EncodeMap[(buffer[cur + 1] & 0x3f)]);
cur += 2;
count -= 2;
WriteState.Padding = 0;
break;
case 1:
WriteState.Append(s_base64EncodeMap[WriteState.LastBits | ((buffer[cur] & 0xc0) >> 6)]);
WriteState.Append(s_base64EncodeMap[(buffer[cur] & 0x3f)]);
WriteState.Append(Base64EncodeMap[WriteState.LastBits | ((buffer[cur] & 0xc0) >> 6)]);
WriteState.Append(Base64EncodeMap[(buffer[cur] & 0x3f)]);
cur++;
count--;
WriteState.Padding = 0;
Expand All @@ -242,10 +242,10 @@ internal int EncodeBytes(byte[] buffer, int offset, int count, bool dontDeferFin
//this means that three bytes of data will be encoded as four base64 characters. It also means that to encode
//a character, we must have three bytes to encode so if the number of bytes is not divisible by three, we
//must pad the buffer (this happens below)
WriteState.Append(s_base64EncodeMap[(buffer[cur] & 0xfc) >> 2]);
WriteState.Append(s_base64EncodeMap[((buffer[cur] & 0x03) << 4) | ((buffer[cur + 1] & 0xf0) >> 4)]);
WriteState.Append(s_base64EncodeMap[((buffer[cur + 1] & 0x0f) << 2) | ((buffer[cur + 2] & 0xc0) >> 6)]);
WriteState.Append(s_base64EncodeMap[(buffer[cur + 2] & 0x3f)]);
WriteState.Append(Base64EncodeMap[(buffer[cur] & 0xfc) >> 2]);
WriteState.Append(Base64EncodeMap[((buffer[cur] & 0x03) << 4) | ((buffer[cur + 1] & 0xf0) >> 4)]);
WriteState.Append(Base64EncodeMap[((buffer[cur + 1] & 0x0f) << 2) | ((buffer[cur + 2] & 0xc0) >> 6)]);
WriteState.Append(Base64EncodeMap[(buffer[cur + 2] & 0x3f)]);
}

cur = calcLength; //Where we left off before
Expand All @@ -261,12 +261,12 @@ internal int EncodeBytes(byte[] buffer, int offset, int count, bool dontDeferFin
switch (count % 3)
{
case 2: //One character padding needed
WriteState.Append(s_base64EncodeMap[(buffer[cur] & 0xFC) >> 2]);
WriteState.Append(s_base64EncodeMap[((buffer[cur] & 0x03) << 4) | ((buffer[cur + 1] & 0xf0) >> 4)]);
WriteState.Append(Base64EncodeMap[(buffer[cur] & 0xFC) >> 2]);
WriteState.Append(Base64EncodeMap[((buffer[cur] & 0x03) << 4) | ((buffer[cur + 1] & 0xf0) >> 4)]);
if (dontDeferFinalBytes)
{
WriteState.Append(s_base64EncodeMap[((buffer[cur + 1] & 0x0f) << 2)]);
WriteState.Append(s_base64EncodeMap[64]);
WriteState.Append(Base64EncodeMap[((buffer[cur + 1] & 0x0f) << 2)]);
WriteState.Append(Base64EncodeMap[64]);
WriteState.Padding = 0;
}
else
Expand All @@ -278,12 +278,12 @@ internal int EncodeBytes(byte[] buffer, int offset, int count, bool dontDeferFin
break;

case 1: // Two character padding needed
WriteState.Append(s_base64EncodeMap[(buffer[cur] & 0xFC) >> 2]);
WriteState.Append(Base64EncodeMap[(buffer[cur] & 0xFC) >> 2]);
if (dontDeferFinalBytes)
{
WriteState.Append(s_base64EncodeMap[(byte)((buffer[cur] & 0x03) << 4)]);
WriteState.Append(s_base64EncodeMap[64]);
WriteState.Append(s_base64EncodeMap[64]);
WriteState.Append(Base64EncodeMap[(byte)((buffer[cur] & 0x03) << 4)]);
WriteState.Append(Base64EncodeMap[64]);
WriteState.Append(Base64EncodeMap[64]);
WriteState.Padding = 0;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class QEncodedStream : DelegatedStream, IEncodableStream
//folding takes up 3 characters "\r\n "
private const int SizeOfFoldingCRLF = 3;

private static readonly byte[] s_hexDecodeMap = new byte[]
private static ReadOnlySpan<byte> HexDecodeMap => new byte[] // rely on C# compiler optimization to eliminate allocation
{
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 0
Expand Down Expand Up @@ -107,8 +107,8 @@ public unsafe int DecodeBytes(byte[] buffer, int offset, int count)
// '=\r\n' means a soft (aka. invisible) CRLF sequence...
if (source[0] != '\r' || source[1] != '\n')
{
byte b1 = s_hexDecodeMap[source[0]];
byte b2 = s_hexDecodeMap[source[1]];
byte b1 = HexDecodeMap[source[0]];
byte b2 = HexDecodeMap[source[1]];
if (b1 == 255)
throw new FormatException(SR.Format(SR.InvalidHexDigit, b1));
if (b2 == 255)
Expand All @@ -124,8 +124,8 @@ public unsafe int DecodeBytes(byte[] buffer, int offset, int count)
// '=\r\n' means a soft (aka. invisible) CRLF sequence...
if (ReadState.Byte != '\r' || *source != '\n')
{
byte b1 = s_hexDecodeMap[ReadState.Byte];
byte b2 = s_hexDecodeMap[*source];
byte b1 = HexDecodeMap[ReadState.Byte];
byte b2 = HexDecodeMap[*source];
if (b1 == 255)
throw new FormatException(SR.Format(SR.InvalidHexDigit, b1));
if (b2 == 255)
Expand Down Expand Up @@ -177,8 +177,8 @@ public unsafe int DecodeBytes(byte[] buffer, int offset, int count)
default:
if (source[1] != '\r' || source[2] != '\n')
{
byte b1 = s_hexDecodeMap[source[1]];
byte b2 = s_hexDecodeMap[source[2]];
byte b1 = HexDecodeMap[source[1]];
byte b2 = HexDecodeMap[source[2]];
if (b1 == 255)
throw new FormatException(SR.Format(SR.InvalidHexDigit, b1));
if (b2 == 255)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class QuotedPrintableStream : DelegatedStream, IEncodableStream
//if we aren't encoding CRLF then it occupies two chars
private const int SizeOfNonEncodedCRLF = 2;

private static readonly byte[] s_hexDecodeMap = new byte[]
private static ReadOnlySpan<byte> HexDecodeMap => new byte[] // rely on C# compiler optimization to eliminate allocation
{
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 0
Expand All @@ -55,7 +55,7 @@ internal class QuotedPrintableStream : DelegatedStream, IEncodableStream
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // F
};

private static readonly byte[] s_hexEncodeMap = new byte[] { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70 };
private static ReadOnlySpan<byte> HexEncodeMap => new byte[] { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70 };

private readonly int _lineLength;
private ReadStateInfo _readState;
Expand Down Expand Up @@ -140,8 +140,8 @@ public unsafe int DecodeBytes(byte[] buffer, int offset, int count)
// '=\r\n' means a soft (aka. invisible) CRLF sequence...
if (source[0] != '\r' || source[1] != '\n')
{
byte b1 = s_hexDecodeMap[source[0]];
byte b2 = s_hexDecodeMap[source[1]];
byte b1 = HexDecodeMap[source[0]];
byte b2 = HexDecodeMap[source[1]];
if (b1 == 255)
throw new FormatException(SR.Format(SR.InvalidHexDigit, b1));
if (b2 == 255)
Expand All @@ -157,8 +157,8 @@ public unsafe int DecodeBytes(byte[] buffer, int offset, int count)
// '=\r\n' means a soft (aka. invisible) CRLF sequence...
if (ReadState.Byte != '\r' || *source != '\n')
{
byte b1 = s_hexDecodeMap[ReadState.Byte];
byte b2 = s_hexDecodeMap[*source];
byte b1 = HexDecodeMap[ReadState.Byte];
byte b2 = HexDecodeMap[*source];
if (b1 == 255)
throw new FormatException(SR.Format(SR.InvalidHexDigit, b1));
if (b2 == 255)
Expand Down Expand Up @@ -202,8 +202,8 @@ public unsafe int DecodeBytes(byte[] buffer, int offset, int count)
default:
if (source[1] != '\r' || source[2] != '\n')
{
byte b1 = s_hexDecodeMap[source[1]];
byte b2 = s_hexDecodeMap[source[2]];
byte b1 = HexDecodeMap[source[1]];
byte b2 = HexDecodeMap[source[2]];
if (b1 == 255)
throw new FormatException(SR.Format(SR.InvalidHexDigit, b1));
if (b2 == 255)
Expand Down Expand Up @@ -276,9 +276,9 @@ public int EncodeBytes(byte[] buffer, int offset, int count)
//append an = to indicate an encoded character
WriteState.Append((byte)'=');
//shift 4 to get the first four bytes only and look up the hex digit
WriteState.Append(s_hexEncodeMap[buffer[cur] >> 4]);
WriteState.Append(HexEncodeMap[buffer[cur] >> 4]);
//clear the first four bytes to get the last four and look up the hex digit
WriteState.Append(s_hexEncodeMap[buffer[cur] & 0xF]);
WriteState.Append(HexEncodeMap[buffer[cur] & 0xF]);
}
else
{
Expand All @@ -299,9 +299,9 @@ public int EncodeBytes(byte[] buffer, int offset, int count)
//append an = to indicate an encoded character
WriteState.Append((byte)'=');
//shift 4 to get the first four bytes only and look up the hex digit
WriteState.Append(s_hexEncodeMap[buffer[cur] >> 4]);
WriteState.Append(HexEncodeMap[buffer[cur] >> 4]);
//clear the first four bytes to get the last four and look up the hex digit
WriteState.Append(s_hexEncodeMap[buffer[cur] & 0xF]);
WriteState.Append(HexEncodeMap[buffer[cur] & 0xF]);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class XmlJsonReader : XmlBaseReader, IXmlJsonReaderInitializer
{
private const int MaxTextChunk = 2048;

private static readonly byte[] s_charType = new byte[256]
private static ReadOnlySpan<byte> CharTypes => new byte[256] // rely on C# compiler optimization to eliminate allocation
{
CharType.None, // 0 (.)
CharType.None, // 1 (.)
Expand Down Expand Up @@ -1127,7 +1127,7 @@ private void ParseAndSetLocalName()
elementNode.BufferOffset = actualOffset;

int currentCharacter = (int)BufferReader.GetByte(elementNode.NameOffset);
if ((s_charType[currentCharacter] & CharType.FirstName) == 0)
if ((CharTypes[currentCharacter] & CharType.FirstName) == 0)
{
SetJsonNameWithMapping(elementNode);
}
Expand All @@ -1136,7 +1136,7 @@ private void ParseAndSetLocalName()
for (int i = 0, offset = elementNode.NameOffset; i < elementNode.NameLength; i++, offset++)
{
currentCharacter = (int)BufferReader.GetByte(offset);
if ((s_charType[currentCharacter] & CharType.Name) == 0 || currentCharacter >= 0x80)
if ((CharTypes[currentCharacter] & CharType.Name) == 0 || currentCharacter >= 0x80)
{
SetJsonNameWithMapping(elementNode);
break;
Expand Down
Loading

0 comments on commit 21582d2

Please sign in to comment.