// 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;
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;
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:
// The .NET Foundation licenses this file to you under the MIT license.
using System;
+using System.Buffers.Binary;
using System.Linq;
using Internal.TypeSystem;
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);
}
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;
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);
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;
+++ /dev/null
-// 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]);
- }
- }
-}
// 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;
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)
{
// 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
}
#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));
}
}
}
// 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;
/// <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>
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" />
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"
<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" />
// 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
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)
<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"
<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"
{
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)
{
using System.Text;
using System.Globalization;
using System.Net.NetworkInformation;
+using System.Buffers.Binary;
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
<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"
<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"
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"
using System.Runtime.InteropServices;
using System.Threading;
+#if BIGENDIAN
+using System.Buffers.Binary;
+#endif
+
namespace System.Net.Sockets
{
internal static class SocketPal
}
#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
}
#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);
// 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;
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();
// 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;
// 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 ?
namespace System.Runtime.Serialization
{
using System;
+ using System.Buffers.Binary;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
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);
// 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;
}
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,
// 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;
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)
{
// 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;
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);
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);
// 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
}
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)
{