1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
-
5
4
using System;
6
5
using System.Buffers;
6
+ using System.Buffers.Binary;
7
7
using System.Collections.Generic;
8
8
using System.Data;
9
9
using System.Data.SqlTypes;
@@ -1743,14 +1743,10 @@ internal void WriteInt(int v, TdsParserStateObject stateObj)
1743
1743
1744
1744
internal static void WriteInt(Span<byte> buffer, int value)
1745
1745
{
1746
- #if NETCOREAPP
1747
- BitConverter.TryWriteBytes(buffer, value);
1748
- #else
1749
1746
buffer[0] = (byte)(value & 0xff);
1750
1747
buffer[1] = (byte)((value >> 8) & 0xff);
1751
1748
buffer[2] = (byte)((value >> 16) & 0xff);
1752
1749
buffer[3] = (byte)((value >> 24) & 0xff);
1753
- #endif
1754
1750
}
1755
1751
1756
1752
//
@@ -1763,7 +1759,9 @@ internal byte[] SerializeFloat(float v)
1763
1759
throw ADP.ParameterValueOutOfRange(v.ToString());
1764
1760
}
1765
1761
1766
- return BitConverter.GetBytes(v);
1762
+ var bytes = new byte[4];
1763
+ BinaryPrimitives.WriteInt32LittleEndian(bytes, BitConverterCompat.SingleToInt32Bits(v));
1764
+ return bytes;
1767
1765
}
1768
1766
1769
1767
internal void WriteFloat(float v, TdsParserStateObject stateObj)
@@ -1886,7 +1884,9 @@ internal byte[] SerializeDouble(double v)
1886
1884
throw ADP.ParameterValueOutOfRange(v.ToString());
1887
1885
}
1888
1886
1889
- return BitConverter.GetBytes(v);
1887
+ var bytes = new byte[8];
1888
+ BinaryPrimitives.WriteInt64LittleEndian(bytes, BitConverter.DoubleToInt64Bits(v));
1889
+ return bytes;
1890
1890
}
1891
1891
1892
1892
internal void WriteDouble(double v, TdsParserStateObject stateObj)
@@ -3808,8 +3808,8 @@ private bool TryProcessFedAuthInfo(TdsParserStateObject stateObj, int tokenLen,
3808
3808
uint currentOptionOffset = checked(i * optionSize);
3809
3809
3810
3810
byte id = tokenData[currentOptionOffset];
3811
- uint dataLen = BitConverter.ToUInt32( tokenData, checked((int)(currentOptionOffset + 1)));
3812
- uint dataOffset = BitConverter.ToUInt32( tokenData, checked((int)(currentOptionOffset + 5)));
3811
+ uint dataLen = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan<byte>( tokenData, checked((int)(currentOptionOffset + 1)), 4 ));
3812
+ uint dataOffset = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan<byte>( tokenData, checked((int)(currentOptionOffset + 5)), 4 ));
3813
3813
if (SqlClientEventSource.Log.IsAdvancedTraceOn())
3814
3814
{
3815
3815
SqlClientEventSource.Log.AdvancedTraceEvent("<sc.TdsParser.TryProcessFedAuthInfo> FedAuthInfoOpt: ID={0}, DataLen={1}, Offset={2}", id, dataLen.ToString(CultureInfo.InvariantCulture), dataOffset.ToString(CultureInfo.InvariantCulture));
@@ -5771,7 +5771,7 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
5771
5771
return false;
5772
5772
}
5773
5773
5774
- longValue = BitConverter.ToInt64( unencryptedBytes, 0);
5774
+ longValue = BinaryPrimitives.ReadInt64LittleEndian(new ReadOnlySpan<byte>( unencryptedBytes, 0, 8) );
5775
5775
5776
5776
if (tdsType == TdsEnums.SQLBIT ||
5777
5777
tdsType == TdsEnums.SQLBITN)
@@ -5808,8 +5808,7 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
5808
5808
{
5809
5809
return false;
5810
5810
}
5811
-
5812
- singleValue = BitConverter.ToSingle(unencryptedBytes, 0);
5811
+ singleValue = BitConverterCompat.Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(unencryptedBytes));
5813
5812
value.Single = singleValue;
5814
5813
break;
5815
5814
@@ -5820,7 +5819,7 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
5820
5819
return false;
5821
5820
}
5822
5821
5823
- doubleValue = BitConverter.ToDouble( unencryptedBytes, 0 );
5822
+ doubleValue = BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian( unencryptedBytes) );
5824
5823
value.Double = doubleValue;
5825
5824
break;
5826
5825
@@ -5837,8 +5836,8 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
5837
5836
return false;
5838
5837
}
5839
5838
5840
- mid = BitConverter.ToInt32( unencryptedBytes, 0);
5841
- lo = BitConverter.ToUInt32( unencryptedBytes, 4);
5839
+ mid = BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan<byte>( unencryptedBytes, 0, 4) );
5840
+ lo = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan<byte>( unencryptedBytes, 4, 4) );
5842
5841
5843
5842
long l = (((long)mid) << 0x20) + ((long)lo);
5844
5843
value.SetToMoney(l);
@@ -5875,8 +5874,8 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
5875
5874
return false;
5876
5875
}
5877
5876
5878
- daypart = BitConverter.ToInt32( unencryptedBytes, 0);
5879
- timepart = BitConverter.ToUInt32( unencryptedBytes, 4);
5877
+ daypart = BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan<byte>( unencryptedBytes, 0, 4) );
5878
+ timepart = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan<byte>( unencryptedBytes, 4, 4) );
5880
5879
value.SetToDateTime(daypart, (int)timepart);
5881
5880
break;
5882
5881
@@ -5922,7 +5921,7 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt
5922
5921
for (int i = 0; i < decLength; i++)
5923
5922
{
5924
5923
// up to 16 bytes of data following the sign byte
5925
- bits[i] = BitConverter.ToInt32( unencryptedBytes, index);
5924
+ bits[i] = BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan<byte>( unencryptedBytes, index, 4) );
5926
5925
index += 4;
5927
5926
}
5928
5927
value.SetToDecimal(md.baseTI.precision, md.baseTI.scale, fPositive, bits);
@@ -7490,7 +7489,20 @@ internal Task WriteString(string s, int length, int offset, TdsParserStateObject
7490
7489
7491
7490
private static void CopyCharsToBytes(char[] source, int sourceOffset, byte[] dest, int destOffset, int charLength)
7492
7491
{
7493
- Buffer.BlockCopy(source, sourceOffset, dest, destOffset, charLength * ADP.CharSize);
7492
+ if (!BitConverter.IsLittleEndian)
7493
+ {
7494
+ int desti = 0;
7495
+ for(int srci = 0; srci < charLength; srci++)
7496
+ {
7497
+ dest[desti + destOffset] = (byte)(source[srci + sourceOffset]);
7498
+ dest[desti + destOffset+1] = (byte)(source[srci + sourceOffset] >> 8);
7499
+ desti += 2;
7500
+ }
7501
+ }
7502
+ else
7503
+ {
7504
+ Buffer.BlockCopy(source, sourceOffset, dest, destOffset, charLength * ADP.CharSize);
7505
+ }
7494
7506
}
7495
7507
7496
7508
private static void CopyStringToBytes(string source, int sourceOffset, byte[] dest, int destOffset, int charLength)
@@ -12571,7 +12583,6 @@ private bool TryReadPlpUnicodeCharsChunk(char[] buff, int offst, int len, TdsPar
12571
12583
{
12572
12584
charsToRead = (int)(stateObj._longlenleft >> 1);
12573
12585
}
12574
-
12575
12586
if (!stateObj.TryReadChars(buff, offst, charsToRead, out charsRead))
12576
12587
{
12577
12588
charsRead = 0;
0 commit comments