Use BinaryPrimitives where possible (#43474)
authorLevi Broderick <GrabYourPitchforks@users.noreply.github.com>
Fri, 30 Oct 2020 04:30:07 +0000 (21:30 -0700)
committerGitHub <noreply@github.com>
Fri, 30 Oct 2020 04:30:07 +0000 (21:30 -0700)
26 files changed:
src/coreclr/src/tools/aot/ILCompiler.ReadyToRun/IBC/MIbcProfileParser.cs
src/coreclr/src/tools/aot/ILCompiler.TypeSystem.ReadyToRun.Tests/StaticFieldLayoutTests.cs
src/coreclr/src/tools/dotnet-pgo/Program.cs
src/libraries/Common/src/System/Net/ByteOrder.cs [deleted file]
src/libraries/Common/src/System/Net/SocketAddress.cs
src/libraries/Common/src/System/Net/SocketAddressPal.Windows.cs
src/libraries/System.Console/src/System/TermInfo.cs
src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj
src/libraries/System.Net.NameResolution/tests/PalTests/System.Net.NameResolution.Pal.Tests.csproj
src/libraries/System.Net.NetworkInformation/src/System.Net.NetworkInformation.csproj
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/PhysicalAddress.cs
src/libraries/System.Net.Ping/src/System.Net.Ping.csproj
src/libraries/System.Net.Primitives/src/System.Net.Primitives.csproj
src/libraries/System.Net.Primitives/src/System/Net/IPAddress.cs
src/libraries/System.Net.Primitives/src/System/Net/IPAddressParser.cs
src/libraries/System.Net.Primitives/tests/PalTests/System.Net.Primitives.Pal.Tests.csproj
src/libraries/System.Net.Primitives/tests/UnitTests/System.Net.Primitives.UnitTests.Tests.csproj
src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs
src/libraries/System.Private.CoreLib/src/System/Resources/ResourceReader.cs
src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.Unix.cs
src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/DataContract.cs
src/libraries/System.Private.Xml/src/System/Xml/BinaryXml/SqlUtils.cs
src/libraries/System.Private.Xml/src/System/Xml/BinaryXml/XmlBinaryReader.cs
src/libraries/System.Private.Xml/src/System/Xml/XmlEncoding.cs
src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/SerializationHeaderRecord.cs

index 6a86e06..cc030b3 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System;
+using System.Buffers.Binary;
 using System.Collections.Generic;
 using System.IO.Compression;
 using System.Reflection;
@@ -78,7 +79,7 @@ namespace ILCompiler.IBC
                         case ILOpcode.ldstr:
                             if (mibcGroupName == "")
                             {
-                                UInt32 userStringToken = (UInt32)(ilBytes[currentOffset + 1] + (ilBytes[currentOffset + 2] << 8) + (ilBytes[currentOffset + 3] << 16) + (ilBytes[currentOffset + 4] << 24));
+                                UInt32 userStringToken = BinaryPrimitives.ReadUInt32LittleEndian(ilBytes.AsSpan(currentOffset + 1));
                                 mibcGroupName = (string)ilBody.GetObject((int)userStringToken);
                             }
                             break;
@@ -110,7 +111,7 @@ namespace ILCompiler.IBC
                             if (!areAllEntriesInVersionBubble)
                                 break;
 
-                            uint token = (uint)(ilBytes[currentOffset + 1] + (ilBytes[currentOffset + 2] << 8) + (ilBytes[currentOffset + 3] << 16) + (ilBytes[currentOffset + 4] << 24));
+                            uint token = BinaryPrimitives.ReadUInt32LittleEndian(ilBytes.AsSpan(currentOffset + 1));
                             loadedMethodProfileData = loadedMethodProfileData.Concat(ReadMIbcGroup(tsc, (EcmaMethod)ilBody.GetObject((int)token)));
                             break;
                         case ILOpcode.pop:
index e0f5eef..421313c 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System;
+using System.Buffers.Binary;
 using System.Linq;
 
 using Internal.TypeSystem;
@@ -280,10 +281,7 @@ namespace TypeSystemTests
 
             Assert.Equal(4, rvaData.Length);
 
-            int value = rvaData[0] |
-                rvaData[1] << 8 |
-                rvaData[2] << 16 |
-                rvaData[3] << 24;
+            int value = BinaryPrimitives.ReadInt32LittleEndian(rvaData);
 
             Assert.Equal(0x78563412, value);
         }
index 96c346d..ee971bf 100644 (file)
@@ -8,6 +8,7 @@ using Microsoft.Diagnostics.Tracing;
 using Microsoft.Diagnostics.Tracing.Etlx;
 using Microsoft.Diagnostics.Tracing.Parsers.Clr;
 using System;
+using System.Buffers.Binary;
 using System.Collections.Generic;
 using System.IO;
 using System.Reflection;
@@ -1119,7 +1120,7 @@ Example tracing commands used to generate the input to this tool:
                 switch (opcode)
                 {
                     case ILOpcode.ldtoken:
-                        UInt32 token = (UInt32)(ilBytes[currentOffset + 1] + (ilBytes[currentOffset + 2] << 8) + (ilBytes[currentOffset + 3] << 16) + (ilBytes[currentOffset + 4] << 24));
+                        UInt32 token = BinaryPrimitives.ReadUInt32LittleEndian(ilBytes.AsSpan(currentOffset + 1));
 
                         if (metadataObject == null)
                             metadataObject = ilBody.GetObject((int)token);
@@ -1222,7 +1223,7 @@ Example tracing commands used to generate the input to this tool:
                 switch (opcode)
                 {
                     case ILOpcode.ldtoken:
-                        UInt32 token = (UInt32)(ilBytes[currentOffset + 1] + (ilBytes[currentOffset + 2] << 8) + (ilBytes[currentOffset + 3] << 16) + (ilBytes[currentOffset + 4] << 24));
+                        UInt32 token = BinaryPrimitives.ReadUInt32LittleEndian(ilBytes.AsSpan(currentOffset + 1));
                         foreach (var data in ReadMIbcGroup(tsc, (EcmaMethod)ilBody.GetObject((int)token)))
                             yield return data;
                         break;
diff --git a/src/libraries/Common/src/System/Net/ByteOrder.cs b/src/libraries/Common/src/System/Net/ByteOrder.cs
deleted file mode 100644 (file)
index 58ec5ed..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Net
-{
-    internal static class ByteOrder
-    {
-        public static void HostToNetworkBytes(this ushort host, byte[] bytes, int index)
-        {
-            bytes[index] = (byte)(host >> 8);
-            bytes[index + 1] = unchecked((byte)host);
-        }
-
-        public static ushort NetworkBytesToHostUInt16(this ReadOnlySpan<byte> bytes, int index)
-        {
-            return (ushort)(((ushort)bytes[index] << 8) | (ushort)bytes[index + 1]);
-        }
-    }
-}
index b3e58c2..dea146e 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 #nullable enable
+using System.Buffers.Binary;
 using System.Diagnostics;
 using System.Globalization;
 using System.Net.Sockets;
@@ -214,10 +215,7 @@ namespace System.Net.Internals
 
                 for (i = 0; i < size; i += 4)
                 {
-                    _hash ^= (int)Buffer[i]
-                            | ((int)Buffer[i + 1] << 8)
-                            | ((int)Buffer[i + 2] << 16)
-                            | ((int)Buffer[i + 3] << 24);
+                    _hash ^= BinaryPrimitives.ReadInt32LittleEndian(Buffer.AsSpan(i));
                 }
                 if ((Size & 3) != 0)
                 {
index cfa97d7..d9bdfb1 100644 (file)
@@ -1,6 +1,7 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System.Buffers.Binary;
 using System.Net.Sockets;
 
 namespace System.Net
@@ -26,66 +27,44 @@ namespace System.Net
             }
 
 #if BIGENDIAN
-            buffer[0] = unchecked((byte)((int)family >> 8));
-            buffer[1] = unchecked((byte)((int)family));
+            BinaryPrimitives.WriteUInt16BigEndian(buffer, (ushort)family);
 #else
-            buffer[0] = unchecked((byte)((int)family));
-            buffer[1] = unchecked((byte)((int)family >> 8));
+            BinaryPrimitives.WriteUInt16LittleEndian(buffer, (ushort)family);
 #endif
         }
 
-        public static ushort GetPort(ReadOnlySpan<byte> buffer) => buffer.NetworkBytesToHostUInt16(2);
+        public static ushort GetPort(ReadOnlySpan<byte> buffer)
+            => BinaryPrimitives.ReadUInt16BigEndian(buffer.Slice(2));
 
-        public static void SetPort(byte[] buffer, ushort port) => port.HostToNetworkBytes(buffer, 2);
+        public static void SetPort(byte[] buffer, ushort port)
+            => BinaryPrimitives.WriteUInt16BigEndian(buffer.AsSpan(2), port);
 
-        public static uint GetIPv4Address(ReadOnlySpan<byte> buffer) =>
-            (uint)((buffer[4] & 0x000000FF) |
-                   (buffer[5] << 8 & 0x0000FF00) |
-                   (buffer[6] << 16 & 0x00FF0000) |
-                   (buffer[7] << 24));
+        public static uint GetIPv4Address(ReadOnlySpan<byte> buffer)
+            => BinaryPrimitives.ReadUInt32LittleEndian(buffer.Slice(4));
 
         public static void GetIPv6Address(ReadOnlySpan<byte> buffer, Span<byte> address, out uint scope)
         {
-            for (int i = 0; i < address.Length; i++)
-            {
-                address[i] = buffer[8 + i];
-            }
+            buffer.Slice(8, address.Length).CopyTo(address);
 
-            scope = unchecked((uint)(
-                (buffer[27] << 24) +
-                (buffer[26] << 16) +
-                (buffer[25] << 8) +
-                (buffer[24])));
+            scope = BinaryPrimitives.ReadUInt32LittleEndian(buffer.Slice(24));
         }
 
         public static void SetIPv4Address(byte[] buffer, uint address)
         {
             // IPv4 Address serialization
-            buffer[4] = unchecked((byte)(address));
-            buffer[5] = unchecked((byte)(address >> 8));
-            buffer[6] = unchecked((byte)(address >> 16));
-            buffer[7] = unchecked((byte)(address >> 24));
+            BinaryPrimitives.WriteUInt32LittleEndian(buffer.AsSpan(4), address);
         }
 
         public static void SetIPv6Address(byte[] buffer, Span<byte> address, uint scope)
         {
             // No handling for Flow Information
-            buffer[4] = (byte)0;
-            buffer[5] = (byte)0;
-            buffer[6] = (byte)0;
-            buffer[7] = (byte)0;
+            BinaryPrimitives.WriteUInt32LittleEndian(buffer.AsSpan(4), 0);
 
             // Scope serialization
-            buffer[24] = (byte)scope;
-            buffer[25] = (byte)(scope >> 8);
-            buffer[26] = (byte)(scope >> 16);
-            buffer[27] = (byte)(scope >> 24);
+            BinaryPrimitives.WriteUInt32LittleEndian(buffer.AsSpan(24), scope);
 
             // Address serialization
-            for (int i = 0; i < address.Length; i++)
-            {
-                buffer[8 + i] = address[i];
-            }
+            address.CopyTo(buffer.AsSpan(8));
         }
     }
 }
index 1b0b41d..d8e954d 100644 (file)
@@ -1,6 +1,7 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System.Buffers.Binary;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics.CodeAnalysis;
@@ -481,12 +482,7 @@ namespace System
             /// <param name="pos">The position at which to read.</param>
             /// <returns>The 32-bit value read.</returns>
             private static int ReadInt32(byte[] buffer, int pos)
-            {
-                return (int)((buffer[pos] & 0xff) |
-                             buffer[pos + 1] << 8 |
-                             buffer[pos + 2] << 16 |
-                             buffer[pos + 3] << 24);
-            }
+                => BinaryPrimitives.ReadInt32LittleEndian(buffer.AsSpan(pos));
 
             /// <summary>Reads a string from the buffer starting at the specified position.</summary>
             /// <param name="buffer">The buffer from which to read.</param>
index 7286317..3119914 100644 (file)
@@ -31,8 +31,6 @@
              Link="Common\System\Net\IPAddressParserStatics.cs" />
     <Compile Include="$(CommonPath)System\Net\IPEndPointStatics.cs"
              Link="Common\System\Net\IPEndPointStatics.cs" />
-    <Compile Include="$(CommonPath)System\Net\ByteOrder.cs"
-             Link="Common\System\Net\ByteOrder.cs" />
   </ItemGroup>
   <ItemGroup Condition=" '$(TargetsWindows)' == 'true'">
     <Compile Include="System\Net\NameResolutionPal.Windows.cs" />
index 7a5b3c3..810e46d 100644 (file)
@@ -32,8 +32,6 @@
              Link="Common\System\Net\Configuration.cs" />
     <Compile Include="$(CommonTestPath)System\Net\Configuration.Http.cs"
              Link="Common\System\Net\Configuration.Http.cs" />
-    <Compile Include="$(CommonPath)System\Net\ByteOrder.cs"
-             Link="Common\System\Net\ByteOrder.cs" />
   </ItemGroup>
   <ItemGroup Condition=" '$(TargetsWindows)' == 'true' ">
     <Compile Include="..\..\src\System\Net\NameResolutionPal.Windows.cs"
index f0aa757..21f4f94 100644 (file)
@@ -80,7 +80,6 @@
     <Compile Include="System\Net\NetworkInformation\SystemUnicastIPAddressInformation.cs" />
     <Compile Include="System\Net\NetworkInformation\TeredoHelper.cs" />
     <!-- System.Net common -->
-    <Compile Include="$(CommonPath)System\Net\ByteOrder.cs" Link="Common\System\Net\ByteOrder.cs" />
     <Compile Include="$(CommonPath)System\Net\IPAddressParserStatics.cs" Link="Common\System\Net\IPAddressParserStatics.cs" />
     <Compile Include="$(CommonPath)System\Net\SocketAddress.cs" Link="Common\System\Net\SocketAddress.cs" />
     <Compile Include="$(CommonPath)System\Net\SocketAddressPal.Windows.cs" Link="Common\System\Net\SocketAddressPal.Windows.cs" />
index f2203d5..7a238f7 100644 (file)
@@ -1,6 +1,7 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System.Buffers.Binary;
 using System.Diagnostics.CodeAnalysis;
 
 namespace System.Net.NetworkInformation
@@ -28,10 +29,7 @@ namespace System.Net.NetworkInformation
 
                 for (i = 0; i < size; i += 4)
                 {
-                    hash ^= (int)_address[i]
-                            | ((int)_address[i + 1] << 8)
-                            | ((int)_address[i + 2] << 16)
-                            | ((int)_address[i + 3] << 24);
+                    hash ^= BinaryPrimitives.ReadInt32LittleEndian(_address.AsSpan(i));
                 }
 
                 if ((_address.Length & 3) != 0)
index 394becd..e289d5c 100644 (file)
@@ -16,8 +16,6 @@
     <Compile Include="System\Net\NetworkInformation\PingOptions.cs" />
     <Compile Include="System\Net\NetworkInformation\PingReply.cs" />
     <!-- System.Net Common -->
-    <Compile Include="$(CommonPath)System\Net\ByteOrder.cs"
-             Link="Common\System\Net\ByteOrder.cs" />
     <Compile Include="$(CommonPath)System\Net\IPAddressParserStatics.cs"
              Link="Common\System\Net\IPAddressParserStatics.cs" />
     <Compile Include="$(CommonPath)System\Net\SocketAddress.cs"
index 07f176a..d2c79e1 100644 (file)
@@ -48,8 +48,6 @@
     <Compile Include="System\Security\Authentication\ExtendedProtection\ChannelBindingKind.cs" />
     <Compile Include="$(CommonPath)System\StringExtensions.cs"
              Link="Common\System\StringExtensions.cs" />
-    <Compile Include="$(CommonPath)System\Net\ByteOrder.cs"
-             Link="Common\System\Net\ByteOrder.cs" />
     <Compile Include="$(CommonPath)System\Net\CookieComparer.cs"
              Link="ProductionCode\System\Net\CookieComparer.cs" />
     <Compile Include="$(CommonPath)System\Net\CookieFields.cs"
index b697634..c2b7ec3 100644 (file)
@@ -185,7 +185,7 @@ namespace System.Net
         {
             if (address.Length == IPAddressParserStatics.IPv4AddressBytes)
             {
-                PrivateAddress = (uint)((address[3] << 24 | address[2] << 16 | address[1] << 8 | address[0]) & 0x0FFFFFFFF);
+                PrivateAddress = BinaryPrimitives.ReadUInt32LittleEndian(address);
             }
             else if (address.Length == IPAddressParserStatics.IPv6AddressBytes)
             {
index 6974855..fede4c4 100644 (file)
@@ -10,6 +10,7 @@ using System.Runtime.InteropServices;
 using System.Text;
 using System.Globalization;
 using System.Net.NetworkInformation;
+using System.Buffers.Binary;
 
 namespace System.Net
 {
@@ -181,11 +182,7 @@ namespace System.Net
             {
                 // IPv4AddressHelper.ParseNonCanonical returns the bytes in the inverse order.
                 // Reverse them and return success.
-                address =
-                    ((0xFF000000 & tmpAddr) >> 24) |
-                    ((0x00FF0000 & tmpAddr) >> 8) |
-                    ((0x0000FF00 & tmpAddr) << 8) |
-                    ((0x000000FF & tmpAddr) << 24);
+                address = BinaryPrimitives.ReverseEndianness((uint)tmpAddr);
                 return true;
             }
             else
index 26647c8..8f39f56 100644 (file)
@@ -45,8 +45,6 @@
     <Compile Include="Fakes\NetEventSource.cs" />
     <Compile Include="..\..\src\System\Net\SocketException.cs"
              Link="ProductionCode\System\Net\SocketException.cs" />
-    <Compile Include="$(CommonPath)System\Net\ByteOrder.cs"
-             Link="ProductionCode\Common\System\Net\ByteOrder.cs" />
     <Compile Include="$(CommonPath)System\Net\InternalException.cs"
              Link="ProductionCode\Common\System\Net\InternalException.cs" />
     <Compile Include="$(CommonPath)System\Net\IPAddressParserStatics.cs"
index 4619f2a..9f3b892 100644 (file)
@@ -56,8 +56,6 @@
     <Compile Include="Fakes\IPv6AddressHelper.cs" />
     <Compile Include="$(CommonPath)System\StringExtensions.cs"
              Link="Common\System\StringExtensions.cs" />
-    <Compile Include="$(CommonPath)System\Net\ByteOrder.cs"
-             Link="ProductionCode\Common\System\Net\ByteOrder.cs" />
     <Compile Include="$(CommonPath)System\Net\HttpKnownHeaderNames.cs"
              Link="ProductionCode\Common\System\Net\HttpKnownHeaderNames.cs" />
     <Compile Include="$(CommonPath)System\Net\IPAddressParserStatics.cs"
index bbaaf1b..1311c49 100644 (file)
@@ -64,8 +64,6 @@
              Link="Common\System\Net\ContextAwareResult.cs" />
     <Compile Include="$(CommonPath)System\Net\LazyAsyncResult.cs"
              Link="Common\System\Net\LazyAsyncResult.cs" />
-    <Compile Include="$(CommonPath)System\Net\ByteOrder.cs"
-             Link="Common\System\Net\ByteOrder.cs" />
     <Compile Include="$(CommonPath)System\Net\IPEndPointStatics.cs"
              Link="Common\System\Net\IPEndPointStatics.cs" />
     <Compile Include="$(CommonPath)System\Net\IPAddressParserStatics.cs"
index f111ac0..a48eb9b 100644 (file)
@@ -11,6 +11,10 @@ using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Threading;
 
+#if BIGENDIAN
+using System.Buffers.Binary;
+#endif
+
 namespace System.Net.Sockets
 {
     internal static class SocketPal
@@ -648,17 +652,11 @@ namespace System.Net.Sockets
             }
 
 #if BIGENDIAN
-            ipmr.MulticastAddress = (int) (((uint) ipmr.MulticastAddress << 24) |
-                                           (((uint) ipmr.MulticastAddress & 0x0000FF00) << 8) |
-                                           (((uint) ipmr.MulticastAddress >> 8) & 0x0000FF00) |
-                                           ((uint) ipmr.MulticastAddress >> 24));
+            ipmr.MulticastAddress = BinaryPrimitives.ReverseEndianness(ipmr.MulticastAddress);
 
             if (optionValue.LocalAddress != null)
             {
-                ipmr.InterfaceAddress = (int) (((uint) ipmr.InterfaceAddress << 24) |
-                                           (((uint) ipmr.InterfaceAddress & 0x0000FF00) << 8) |
-                                           (((uint) ipmr.InterfaceAddress >> 8) & 0x0000FF00) |
-                                           ((uint) ipmr.InterfaceAddress >> 24));
+                ipmr.InterfaceAddress = BinaryPrimitives.ReverseEndianness(ipmr.InterfaceAddress);
             }
 #endif
 
@@ -791,14 +789,8 @@ namespace System.Net.Sockets
             }
 
 #if BIGENDIAN
-            ipmr.MulticastAddress = (int) (((uint) ipmr.MulticastAddress << 24) |
-                                           (((uint) ipmr.MulticastAddress & 0x0000FF00) << 8) |
-                                           (((uint) ipmr.MulticastAddress >> 8) & 0x0000FF00) |
-                                           ((uint) ipmr.MulticastAddress >> 24));
-            ipmr.InterfaceAddress = (int) (((uint) ipmr.InterfaceAddress << 24) |
-                                           (((uint) ipmr.InterfaceAddress & 0x0000FF00) << 8) |
-                                           (((uint) ipmr.InterfaceAddress >> 8) & 0x0000FF00) |
-                                           ((uint) ipmr.InterfaceAddress >> 24));
+            ipmr.MulticastAddress = BinaryPrimitives.ReverseEndianness(ipmr.MulticastAddress);
+            ipmr.InterfaceAddress = BinaryPrimitives.ReverseEndianness(ipmr.InterfaceAddress);
 #endif  // BIGENDIAN
 
             IPAddress multicastAddr = new IPAddress(ipmr.MulticastAddress);
index c43899c..e146914 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 #nullable enable
+using System.Buffers.Binary;
 using System.Collections;
 using System.Collections.Generic;
 using System.Diagnostics;
@@ -172,12 +173,9 @@ namespace System.Resources
 
         internal static unsafe int ReadUnalignedI4(int* p)
         {
-            byte* buffer = (byte*)p;
-            // Unaligned, little endian format
-            return buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24);
+            return BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan<byte>(p, sizeof(int)));
         }
 
-
         private void SkipString()
         {
             int stringLength = _store.Read7BitEncodedInt();
index 0be8b50..614541a 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System.Buffers;
+using System.Buffers.Binary;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics.CodeAnalysis;
@@ -1433,25 +1434,13 @@ namespace System
 
         // Converts an array of bytes into an int - always using standard byte order (Big Endian)
         // per TZif file standard
-        private static unsafe int TZif_ToInt32(byte[] value, int startIndex)
-        {
-            fixed (byte* pbyte = &value[startIndex])
-            {
-                return (*pbyte << 24) | (*(pbyte + 1) << 16) | (*(pbyte + 2) << 8) | (*(pbyte + 3));
-            }
-        }
+        private static int TZif_ToInt32(byte[] value, int startIndex)
+            => BinaryPrimitives.ReadInt32BigEndian(value.AsSpan(startIndex));
 
         // Converts an array of bytes into a long - always using standard byte order (Big Endian)
         // per TZif file standard
-        private static unsafe long TZif_ToInt64(byte[] value, int startIndex)
-        {
-            fixed (byte* pbyte = &value[startIndex])
-            {
-                int i1 = (*pbyte << 24) | (*(pbyte + 1) << 16) | (*(pbyte + 2) << 8) | (*(pbyte + 3));
-                int i2 = (*(pbyte + 4) << 24) | (*(pbyte + 5) << 16) | (*(pbyte + 6) << 8) | (*(pbyte + 7));
-                return (uint)i2 | ((long)i1 << 32);
-            }
-        }
+        private static long TZif_ToInt64(byte[] value, int startIndex)
+            => BinaryPrimitives.ReadInt64BigEndian(value.AsSpan(startIndex));
 
         private static long TZif_ToUnixTime(byte[] value, int startIndex, TZVersion version) =>
             version != TZVersion.V1 ?
index 2099fc9..d720f0e 100644 (file)
@@ -4,6 +4,7 @@
 namespace System.Runtime.Serialization
 {
     using System;
+    using System.Buffers.Binary;
     using System.Collections.Generic;
     using System.Globalization;
     using System.Reflection;
@@ -1842,7 +1843,7 @@ namespace System.Runtime.Serialization
                     d = c;
                     c = b;
 
-                    b = unchecked(a + f + sines[j] + (uint)(block[g] + (block[g + 1] << 8) + (block[g + 2] << 16) + (block[g + 3] << 24)));
+                    b = unchecked(a + f + sines[j] + BinaryPrimitives.ReadUInt32LittleEndian(block.AsSpan(g)));
                     b = b << shifts[j & 3 | j >> 2 & ~3] | b >> 32 - shifts[j & 3 | j >> 2 & ~3];
                     b = unchecked(b + c);
 
index d8bf112..a88f9cf 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System;
+using System.Buffers.Binary;
 using System.Collections;
 using System.IO;
 using System.Text;
@@ -69,13 +70,7 @@ namespace System.Xml
         }
 
         private static uint UIntFromByteArray(byte[] data, int offset)
-        {
-            int val = (data[offset]) << 0;
-            val |= (data[offset + 1]) << 8;
-            val |= (data[offset + 2]) << 16;
-            val |= (data[offset + 3]) << 24;
-            return unchecked((uint)val);
-        }
+            => BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(offset));
 
         // Multi-precision one super-digit divide in place.
         // U = U / D,
index 0215d5b..b71e205 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System;
+using System.Buffers.Binary;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
@@ -4421,45 +4422,17 @@ namespace System.Xml
             return value;
         }
 
-        private short GetInt16(int pos)
-        {
-            byte[] data = _data;
-            return (short)(data[pos] | data[pos + 1] << 8);
-        }
+        private short GetInt16(int pos) => BinaryPrimitives.ReadInt16LittleEndian(_data.AsSpan(pos));
 
-        private ushort GetUInt16(int pos)
-        {
-            byte[] data = _data;
-            return (ushort)(data[pos] | data[pos + 1] << 8);
-        }
+        private ushort GetUInt16(int pos) => BinaryPrimitives.ReadUInt16LittleEndian(_data.AsSpan(pos));
 
-        private int GetInt32(int pos)
-        {
-            byte[] data = _data;
-            return (int)(data[pos] | data[pos + 1] << 8 | data[pos + 2] << 16 | data[pos + 3] << 24);
-        }
+        private int GetInt32(int pos) => BinaryPrimitives.ReadInt32LittleEndian(_data.AsSpan(pos));
 
-        private uint GetUInt32(int pos)
-        {
-            byte[] data = _data;
-            return (uint)(data[pos] | data[pos + 1] << 8 | data[pos + 2] << 16 | data[pos + 3] << 24);
-        }
+        private uint GetUInt32(int pos) => BinaryPrimitives.ReadUInt32LittleEndian(_data.AsSpan(pos));
 
-        private long GetInt64(int pos)
-        {
-            byte[] data = _data;
-            uint lo = (uint)(data[pos] | data[pos + 1] << 8 | data[pos + 2] << 16 | data[pos + 3] << 24);
-            uint hi = (uint)(data[pos + 4] | data[pos + 5] << 8 | data[pos + 6] << 16 | data[pos + 7] << 24);
-            return (long)((ulong)hi) << 32 | lo;
-        }
+        private long GetInt64(int pos) => BinaryPrimitives.ReadInt64LittleEndian(_data.AsSpan(pos));
 
-        private ulong GetUInt64(int pos)
-        {
-            byte[] data = _data;
-            uint lo = (uint)(data[pos] | data[pos + 1] << 8 | data[pos + 2] << 16 | data[pos + 3] << 24);
-            uint hi = (uint)(data[pos + 4] | data[pos + 5] << 8 | data[pos + 6] << 16 | data[pos + 7] << 24);
-            return (ulong)((ulong)hi) << 32 | lo;
-        }
+        private ulong GetUInt64(int pos) => BinaryPrimitives.ReadUInt64LittleEndian(_data.AsSpan(pos));
 
         private float GetSingle(int offset)
         {
index e65a280..5a2c74a 100644 (file)
@@ -1,6 +1,7 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System.Buffers.Binary;
 using System.Text;
 using System.Diagnostics;
 
@@ -554,7 +555,7 @@ namespace System.Xml
 
             for (i = byteIndex, j = charIndex; i + 3 < byteCount;)
             {
-                code = (uint)((bytes[i + 3] << 24) | (bytes[i + 2] << 16) | (bytes[i + 1] << 8) | bytes[i]);
+                code = BinaryPrimitives.ReadUInt32LittleEndian(bytes.AsSpan(i));
                 if (code > 0x10FFFF)
                 {
                     throw new ArgumentException(SR.Format(SR.Enc_InvalidByteInEncoding, new object[1] { i }), (string?)null);
@@ -595,7 +596,7 @@ namespace System.Xml
 
             for (i = byteIndex, j = charIndex; i + 3 < byteCount;)
             {
-                code = (uint)((bytes[i] << 24) | (bytes[i + 1] << 16) | (bytes[i + 2] << 8) | bytes[i + 3]);
+                code = BinaryPrimitives.ReadUInt32BigEndian(bytes.AsSpan(i));
                 if (code > 0x10FFFF)
                 {
                     throw new ArgumentException(SR.Format(SR.Enc_InvalidByteInEncoding, new object[1] { i }), (string?)null);
index 0343331..34d66ea 100644 (file)
@@ -1,6 +1,7 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System.Buffers.Binary;
 using System.IO;
 
 namespace System.Runtime.Serialization.Formatters.Binary
@@ -40,7 +41,7 @@ namespace System.Runtime.Serialization.Formatters.Binary
         }
 
         private static int GetInt32(byte[] buffer, int index) =>
-            buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
+            BinaryPrimitives.ReadInt32LittleEndian(buffer.AsSpan(index));
 
         public void Read(BinaryParser input)
         {