From a2a39e40cce387bad3f3abacf2060ca01529f820 Mon Sep 17 00:00:00 2001 From: Michel Zehnder Date: Sat, 2 Nov 2024 15:06:42 +0100 Subject: [PATCH] Align BitConverter/BinaryPrimitives usage netfx/netcore --- .../src/Microsoft/Data/SqlClient/TdsParser.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs index c5500a4da3..f7a32c302c 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -4,6 +4,7 @@ using System; using System.Buffers; +using System.Buffers.Binary; using System.Collections.Generic; using System.Data; using System.Data.SqlTypes; @@ -4396,8 +4397,8 @@ private TdsOperationStatus TryProcessFedAuthInfo(TdsParserStateObject stateObj, uint currentOptionOffset = checked(i * optionSize); byte id = tokenData[currentOptionOffset]; - uint dataLen = BitConverter.ToUInt32(tokenData, checked((int)(currentOptionOffset + 1))); - uint dataOffset = BitConverter.ToUInt32(tokenData, checked((int)(currentOptionOffset + 5))); + uint dataLen = BinaryPrimitives.ReadUInt32LittleEndian(tokenData.AsSpan(checked((int)(currentOptionOffset + 1)))); + uint dataOffset = BinaryPrimitives.ReadUInt32LittleEndian(tokenData.AsSpan(checked((int)(currentOptionOffset + 5)))); if (SqlClientEventSource.Log.IsAdvancedTraceOn()) { SqlClientEventSource.Log.AdvancedTraceEvent(" FedAuthInfoOpt: ID={0}, DataLen={1}, Offset={2}", id, dataLen.ToString(CultureInfo.InvariantCulture), dataOffset.ToString(CultureInfo.InvariantCulture)); @@ -6824,7 +6825,7 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt return false; } - longValue = BitConverter.ToInt64(unencryptedBytes, 0); + longValue = BinaryPrimitives.ReadInt64LittleEndian(unencryptedBytes); if (tdsType == TdsEnums.SQLBIT || tdsType == TdsEnums.SQLBITN) @@ -6862,7 +6863,7 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt return false; } - singleValue = BitConverter.ToSingle(unencryptedBytes, 0); + singleValue = BitConverterCompatible.Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(unencryptedBytes)); value.Single = singleValue; break; @@ -6873,7 +6874,7 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt return false; } - doubleValue = BitConverter.ToDouble(unencryptedBytes, 0); + doubleValue = BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(unencryptedBytes)); value.Double = doubleValue; break; @@ -6890,8 +6891,8 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt return false; } - mid = BitConverter.ToInt32(unencryptedBytes, 0); - lo = BitConverter.ToUInt32(unencryptedBytes, 4); + mid = BinaryPrimitives.ReadInt32LittleEndian(unencryptedBytes); + lo = BinaryPrimitives.ReadUInt32LittleEndian(unencryptedBytes.AsSpan(4)); long l = (((long)mid) << 0x20) + ((long)lo); value.SetToMoney(l); @@ -6928,8 +6929,8 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt return false; } - daypart = BitConverter.ToInt32(unencryptedBytes, 0); - timepart = BitConverter.ToUInt32(unencryptedBytes, 4); + daypart = BinaryPrimitives.ReadInt32LittleEndian(unencryptedBytes); + timepart = BinaryPrimitives.ReadUInt32LittleEndian(unencryptedBytes.AsSpan(4)); value.SetToDateTime(daypart, (int)timepart); break;