44
55using System;
66using System.Buffers;
7+ using System.Buffers.Binary;
78using System.Collections.Generic;
89using System.Data;
910using System.Data.SqlTypes;
@@ -1743,14 +1744,10 @@ internal void WriteInt(int v, TdsParserStateObject stateObj)
17431744
17441745 internal static void WriteInt(Span<byte> buffer, int value)
17451746 {
1746- #if NETCOREAPP
1747- BitConverter.TryWriteBytes(buffer, value);
1748- #else
17491747 buffer[0] = (byte)(value & 0xff);
17501748 buffer[1] = (byte)((value >> 8) & 0xff);
17511749 buffer[2] = (byte)((value >> 16) & 0xff);
17521750 buffer[3] = (byte)((value >> 24) & 0xff);
1753- #endif
17541751 }
17551752
17561753 //
@@ -1763,7 +1760,13 @@ internal byte[] SerializeFloat(float v)
17631760 throw ADP.ParameterValueOutOfRange(v.ToString());
17641761 }
17651762
1763+ #if NETCOREAPP
1764+ var bytes = new byte[4];
1765+ BinaryPrimitives.WriteInt32LittleEndian(bytes, BitConverter.SingleToInt32Bits(v));
1766+ return bytes;
1767+ #else
17661768 return BitConverter.GetBytes(v);
1769+ #endif
17671770 }
17681771
17691772 internal void WriteFloat(float v, TdsParserStateObject stateObj)
@@ -1886,7 +1889,13 @@ internal byte[] SerializeDouble(double v)
18861889 throw ADP.ParameterValueOutOfRange(v.ToString());
18871890 }
18881891
1892+ #if NETCOREAPP
1893+ byte[] bytes = new byte[8];
1894+ BinaryPrimitives.WriteInt64LittleEndian(bytes, BitConverter.DoubleToInt64Bits(v));
1895+ return bytes;
1896+ #else
18891897 return BitConverter.GetBytes(v);
1898+ #endif
18901899 }
18911900
18921901 internal void WriteDouble(double v, TdsParserStateObject stateObj)
@@ -3808,8 +3817,13 @@ private bool TryProcessFedAuthInfo(TdsParserStateObject stateObj, int tokenLen,
38083817 uint currentOptionOffset = checked(i * optionSize);
38093818
38103819 byte id = tokenData[currentOptionOffset];
3820+ #if NETCOREAPP
3821+ uint dataLen = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan<byte>(tokenData, checked((int)(currentOptionOffset + 1)), 4));
3822+ uint dataOffset = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan<byte>(tokenData, checked((int)(currentOptionOffset + 5)), 4));
3823+ #else
38113824 uint dataLen = BitConverter.ToUInt32(tokenData, checked((int)(currentOptionOffset + 1)));
38123825 uint dataOffset = BitConverter.ToUInt32(tokenData, checked((int)(currentOptionOffset + 5)));
3826+ #endif
38133827 if (SqlClientEventSource.Log.IsAdvancedTraceOn())
38143828 {
38153829 SqlClientEventSource.Log.AdvancedTraceEvent("<sc.TdsParser.TryProcessFedAuthInfo> FedAuthInfoOpt: ID={0}, DataLen={1}, Offset={2}", id, dataLen.ToString(CultureInfo.InvariantCulture), dataOffset.ToString(CultureInfo.InvariantCulture));
@@ -5771,7 +5785,11 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
57715785 return false;
57725786 }
57735787
5788+ #if NETCOREAPP
5789+ longValue = BinaryPrimitives.ReadInt64LittleEndian(new ReadOnlySpan<byte>(unencryptedBytes, 0, 8));
5790+ #else
57745791 longValue = BitConverter.ToInt64(unencryptedBytes, 0);
5792+ #endif
57755793
57765794 if (tdsType == TdsEnums.SQLBIT ||
57775795 tdsType == TdsEnums.SQLBITN)
@@ -5809,7 +5827,11 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
58095827 return false;
58105828 }
58115829
5830+ #if NETCOREAPP
5831+ singleValue = BitConverter.Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(unencryptedBytes));
5832+ #else
58125833 singleValue = BitConverter.ToSingle(unencryptedBytes, 0);
5834+ #endif
58135835 value.Single = singleValue;
58145836 break;
58155837
@@ -5820,7 +5842,11 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
58205842 return false;
58215843 }
58225844
5845+ #if NETCOREAPP
5846+ doubleValue = BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(unencryptedBytes));
5847+ #else
58235848 doubleValue = BitConverter.ToDouble(unencryptedBytes, 0);
5849+ #endif
58245850 value.Double = doubleValue;
58255851 break;
58265852
@@ -5837,8 +5863,13 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
58375863 return false;
58385864 }
58395865
5866+ #if NETCOREAPP
5867+ mid = BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan<byte>(unencryptedBytes, 0, 4));
5868+ lo = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan<byte>(unencryptedBytes, 4, 4));
5869+ #else
58405870 mid = BitConverter.ToInt32(unencryptedBytes, 0);
58415871 lo = BitConverter.ToUInt32(unencryptedBytes, 4);
5872+ #endif
58425873
58435874 long l = (((long)mid) << 0x20) + ((long)lo);
58445875 value.SetToMoney(l);
@@ -5875,8 +5906,13 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
58755906 return false;
58765907 }
58775908
5909+ #if NETCOREAPP
5910+ daypart = BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan<byte>(unencryptedBytes, 0, 4));
5911+ timepart = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan<byte>(unencryptedBytes, 4, 4));
5912+ #else
58785913 daypart = BitConverter.ToInt32(unencryptedBytes, 0);
58795914 timepart = BitConverter.ToUInt32(unencryptedBytes, 4);
5915+ #endif
58805916 value.SetToDateTime(daypart, (int)timepart);
58815917 break;
58825918
@@ -5922,7 +5958,11 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
59225958 for (int i = 0; i < decLength; i++)
59235959 {
59245960 // up to 16 bytes of data following the sign byte
5961+ #if NETCOREAPP
5962+ bits[i] = BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan<byte>(unencryptedBytes, index, 4));
5963+ #else
59255964 bits[i] = BitConverter.ToInt32(unencryptedBytes, index);
5965+ #endif
59265966 index += 4;
59275967 }
59285968 value.SetToDecimal(md.baseTI.precision, md.baseTI.scale, fPositive, bits);
@@ -7490,7 +7530,20 @@ internal Task WriteString(string s, int length, int offset, TdsParserStateObject
74907530
74917531 private static void CopyCharsToBytes(char[] source, int sourceOffset, byte[] dest, int destOffset, int charLength)
74927532 {
7493- Buffer.BlockCopy(source, sourceOffset, dest, destOffset, charLength * ADP.CharSize);
7533+ if (!BitConverter.IsLittleEndian)
7534+ {
7535+ int desti = 0;
7536+ for(int srci = 0; srci < charLength; srci++)
7537+ {
7538+ dest[desti + destOffset] = (byte)(source[srci + sourceOffset]);
7539+ dest[desti + destOffset+1] = (byte)(source[srci + sourceOffset] >> 8);
7540+ desti += 2;
7541+ }
7542+ }
7543+ else
7544+ {
7545+ Buffer.BlockCopy(source, sourceOffset, dest, destOffset, charLength * ADP.CharSize);
7546+ }
74947547 }
74957548
74967549 private static void CopyStringToBytes(string source, int sourceOffset, byte[] dest, int destOffset, int charLength)
@@ -12571,7 +12624,6 @@ private bool TryReadPlpUnicodeCharsChunk(char[] buff, int offst, int len, TdsPar
1257112624 {
1257212625 charsToRead = (int)(stateObj._longlenleft >> 1);
1257312626 }
12574-
1257512627 if (!stateObj.TryReadChars(buff, offst, charsToRead, out charsRead))
1257612628 {
1257712629 charsRead = 0;
0 commit comments