diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs index b93ba0749c..75798dcd5d 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Buffers.Binary; using System.Diagnostics; using System.Net; using System.Net.Security; @@ -59,10 +60,17 @@ public void Read(byte[] bytes) { SMID = bytes[0]; flags = bytes[1]; +#if NETCOREAPP + sessionId = BinaryPrimitives.ReadUInt16LittleEndian(new ReadOnlySpan(bytes, 2, 2)); + length = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan(bytes, 4, 4)) - SNISMUXHeader.HEADER_LENGTH; + sequenceNumber = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan(bytes, 8, 4)); + highwater = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan(bytes, 12, 4)); +#else sessionId = BitConverter.ToUInt16(bytes, 2); length = BitConverter.ToUInt32(bytes, 4) - SNISMUXHeader.HEADER_LENGTH; sequenceNumber = BitConverter.ToUInt32(bytes, 8); highwater = BitConverter.ToUInt32(bytes, 12); +#endif } public void Write(Span bytes) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.NetCoreApp.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.NetCoreApp.cs index d8026b36e3..f43c978c18 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.NetCoreApp.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.NetCoreApp.cs @@ -4,6 +4,7 @@ using System; using System.Diagnostics; +using System.Buffers.Binary; namespace Microsoft.Data.SqlClient { @@ -11,9 +12,9 @@ internal sealed partial class TdsParser { internal static void FillGuidBytes(Guid guid, Span buffer) => guid.TryWriteBytes(buffer); - internal static void FillDoubleBytes(double value, Span buffer) => BitConverter.TryWriteBytes(buffer, value); + internal static void FillDoubleBytes(double value, Span buffer) => BinaryPrimitives.WriteDoubleLittleEndian(buffer, value); - internal static void FillFloatBytes(float v, Span buffer) => BitConverter.TryWriteBytes(buffer, v); + internal static void FillFloatBytes(float v, Span buffer) => BinaryPrimitives.WriteSingleLittleEndian(buffer, v); internal static Guid ConstructGuid(ReadOnlySpan bytes) { diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index bde99cda5e..4004a6b1df 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/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; @@ -1743,14 +1744,10 @@ internal void WriteInt(int v, TdsParserStateObject stateObj) internal static void WriteInt(Span buffer, int value) { -#if NETCOREAPP - BitConverter.TryWriteBytes(buffer, value); -#else buffer[0] = (byte)(value & 0xff); buffer[1] = (byte)((value >> 8) & 0xff); buffer[2] = (byte)((value >> 16) & 0xff); buffer[3] = (byte)((value >> 24) & 0xff); -#endif } // @@ -1763,7 +1760,13 @@ internal byte[] SerializeFloat(float v) throw ADP.ParameterValueOutOfRange(v.ToString()); } +#if NETCOREAPP + var bytes = new byte[4]; + BinaryPrimitives.WriteInt32LittleEndian(bytes, BitConverter.SingleToInt32Bits(v)); + return bytes; +#else return BitConverter.GetBytes(v); +#endif } internal void WriteFloat(float v, TdsParserStateObject stateObj) @@ -1886,7 +1889,13 @@ internal byte[] SerializeDouble(double v) throw ADP.ParameterValueOutOfRange(v.ToString()); } +#if NETCOREAPP + byte[] bytes = new byte[8]; + BinaryPrimitives.WriteInt64LittleEndian(bytes, BitConverter.DoubleToInt64Bits(v)); + return bytes; +#else return BitConverter.GetBytes(v); +#endif } internal void WriteDouble(double v, TdsParserStateObject stateObj) @@ -3808,8 +3817,13 @@ private bool TryProcessFedAuthInfo(TdsParserStateObject stateObj, int tokenLen, uint currentOptionOffset = checked(i * optionSize); byte id = tokenData[currentOptionOffset]; +#if NETCOREAPP + uint dataLen = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan(tokenData, checked((int)(currentOptionOffset + 1)), 4)); + uint dataOffset = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan(tokenData, checked((int)(currentOptionOffset + 5)), 4)); +#else uint dataLen = BitConverter.ToUInt32(tokenData, checked((int)(currentOptionOffset + 1))); uint dataOffset = BitConverter.ToUInt32(tokenData, checked((int)(currentOptionOffset + 5))); +#endif if (SqlClientEventSource.Log.IsAdvancedTraceOn()) { SqlClientEventSource.Log.AdvancedTraceEvent(" 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 return false; } +#if NETCOREAPP + longValue = BinaryPrimitives.ReadInt64LittleEndian(new ReadOnlySpan(unencryptedBytes, 0, 8)); +#else longValue = BitConverter.ToInt64(unencryptedBytes, 0); +#endif if (tdsType == TdsEnums.SQLBIT || tdsType == TdsEnums.SQLBITN) @@ -5809,7 +5827,11 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt return false; } +#if NETCOREAPP + singleValue = BitConverter.Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(unencryptedBytes)); +#else singleValue = BitConverter.ToSingle(unencryptedBytes, 0); +#endif value.Single = singleValue; break; @@ -5820,7 +5842,11 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt return false; } +#if NETCOREAPP + doubleValue = BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(unencryptedBytes)); +#else doubleValue = BitConverter.ToDouble(unencryptedBytes, 0); +#endif value.Double = doubleValue; break; @@ -5837,8 +5863,13 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt return false; } +#if NETCOREAPP + mid = BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan(unencryptedBytes, 0, 4)); + lo = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan(unencryptedBytes, 4, 4)); +#else mid = BitConverter.ToInt32(unencryptedBytes, 0); lo = BitConverter.ToUInt32(unencryptedBytes, 4); +#endif long l = (((long)mid) << 0x20) + ((long)lo); value.SetToMoney(l); @@ -5875,8 +5906,13 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt return false; } +#if NETCOREAPP + daypart = BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan(unencryptedBytes, 0, 4)); + timepart = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan(unencryptedBytes, 4, 4)); +#else daypart = BitConverter.ToInt32(unencryptedBytes, 0); timepart = BitConverter.ToUInt32(unencryptedBytes, 4); +#endif value.SetToDateTime(daypart, (int)timepart); break; @@ -5922,7 +5958,11 @@ internal bool DeserializeUnencryptedValue(SqlBuffer value, byte[] unencryptedByt for (int i = 0; i < decLength; i++) { // up to 16 bytes of data following the sign byte +#if NETCOREAPP + bits[i] = BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan(unencryptedBytes, index, 4)); +#else bits[i] = BitConverter.ToInt32(unencryptedBytes, index); +#endif index += 4; } value.SetToDecimal(md.baseTI.precision, md.baseTI.scale, fPositive, bits); @@ -7490,7 +7530,20 @@ internal Task WriteString(string s, int length, int offset, TdsParserStateObject private static void CopyCharsToBytes(char[] source, int sourceOffset, byte[] dest, int destOffset, int charLength) { - Buffer.BlockCopy(source, sourceOffset, dest, destOffset, charLength * ADP.CharSize); + if (!BitConverter.IsLittleEndian) + { + int desti = 0; + for(int srci = 0; srci < charLength; srci++) + { + dest[desti + destOffset] = (byte)(source[srci + sourceOffset]); + dest[desti + destOffset+1] = (byte)(source[srci + sourceOffset] >> 8); + desti += 2; + } + } + else + { + Buffer.BlockCopy(source, sourceOffset, dest, destOffset, charLength * ADP.CharSize); + } } 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 { charsToRead = (int)(stateObj._longlenleft >> 1); } - if (!stateObj.TryReadChars(buff, offst, charsToRead, out charsRead)) { charsRead = 0; diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs index 9ac485cbbb..eb534f43e7 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs @@ -4,6 +4,7 @@ using System; using System.Diagnostics; +using System.Buffers.Binary; using System.Runtime.InteropServices; using System.Security; using System.Threading; @@ -527,7 +528,11 @@ internal bool TryReadInt64(out long value) Debug.Assert(_bTmpRead + bytesRead == 8, "TryReadByteArray returned true without reading all data required"); _bTmpRead = 0; AssertValidState(); +#if NETCOREAPP + value = BinaryPrimitives.ReadInt64LittleEndian(_bTmp); +#else value = BitConverter.ToInt64(_bTmp, 0); +#endif return true; } } @@ -536,8 +541,12 @@ internal bool TryReadInt64(out long value) // The entire long is in the packet and in the buffer, so just return it // and take care of the counters. +#if NETCOREAPP + value = BinaryPrimitives.ReadInt64LittleEndian(new ReadOnlySpan(_inBuff, _inBytesUsed, 8)); +#else value = BitConverter.ToInt64(_inBuff, _inBytesUsed); +#endif _inBytesUsed += 8; _inBytesPacket -= 8; @@ -605,7 +614,11 @@ internal bool TryReadUInt32(out uint value) Debug.Assert(_bTmpRead + bytesRead == 4, "TryReadByteArray returned true without reading all data required"); _bTmpRead = 0; AssertValidState(); +#if NETCOREAPP + value = BinaryPrimitives.ReadUInt32LittleEndian(_bTmp); +#else value = BitConverter.ToUInt32(_bTmp, 0); +#endif return true; } } @@ -614,7 +627,11 @@ internal bool TryReadUInt32(out uint value) // The entire int is in the packet and in the buffer, so just return it // and take care of the counters. +#if NETCOREAPP + value = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan(_inBuff, _inBytesUsed, 4)); +#else value = BitConverter.ToUInt32(_inBuff, _inBytesUsed); +#endif _inBytesUsed += 4; _inBytesPacket -= 4; @@ -639,7 +656,11 @@ internal bool TryReadSingle(out float value) } AssertValidState(); +#if NETCOREAPP + value = BitConverter.Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(_bTmp)); +#else value = BitConverter.ToSingle(_bTmp, 0); +#endif return true; } else @@ -647,8 +668,12 @@ internal bool TryReadSingle(out float value) // The entire float is in the packet and in the buffer, so just return it // and take care of the counters. +#if NETCOREAPP + value = BitConverter.Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan(_inBuff, _inBytesUsed, 4))); +#else value = BitConverter.ToSingle(_inBuff, _inBytesUsed); +#endif _inBytesUsed += 4; _inBytesPacket -= 4; @@ -672,7 +697,11 @@ internal bool TryReadDouble(out double value) } AssertValidState(); +#if NETCOREAPP + value = BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(_bTmp)); +#else value = BitConverter.ToDouble(_bTmp, 0); +#endif return true; } else @@ -680,8 +709,12 @@ internal bool TryReadDouble(out double value) // The entire double is in the packet and in the buffer, so just return it // and take care of the counters. +#if NETCOREAPP + value = BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(new ReadOnlySpan(_inBuff, _inBytesUsed, 8))); +#else value = BitConverter.ToDouble(_inBuff, _inBytesUsed); +#endif _inBytesUsed += 8; _inBytesPacket -= 8; @@ -1793,6 +1826,16 @@ private void SetBufferSecureStrings() str = Marshal.SecureStringToBSTR(_securePasswords[i]); byte[] data = new byte[_securePasswords[i].Length * 2]; Marshal.Copy(str, data, 0, _securePasswords[i].Length * 2); + if (!BitConverter.IsLittleEndian) + { + byte temp; + for (int ii = 0; ii < _securePasswords[i].Length * 2; ii += 2) + { + temp = (byte)data[ii]; + data[ii] = (byte)data[ii + 1]; + data[ii + 1] = (byte)temp; + } + } TdsParserStaticMethods.ObfuscatePassword(data); data.CopyTo(_outBuff, _securePasswordOffsetsInBuffer[i]); } @@ -2059,7 +2102,6 @@ internal void WriteSecureString(SecureString secureString) Debug.Assert(_securePasswords[0] == null || _securePasswords[1] == null, "There are more than two secure passwords"); int index = _securePasswords[0] != null ? 1 : 0; - _securePasswords[index] = secureString; _securePasswordOffsetsInBuffer[index] = _outBytesUsed; @@ -2294,7 +2336,12 @@ internal Task WritePacket(byte flushMode, bool canAccumulate = false) // So we need to avoid this check prior to login completing state == TdsParserState.OpenLoggedIn && !_bulkCopyOpperationInProgress // ignore the condition checking for bulk copy - && _outBytesUsed == (_outputHeaderLen + BitConverter.ToInt32(_outBuff, _outputHeaderLen)) + && _outBytesUsed == (_outputHeaderLen + +#if NETCOREAPP + BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan(_outBuff, _outputHeaderLen, 4))) +#else + BitConverter.ToInt32(_outBuff, _outputHeaderLen)) +#endif && _outputPacketCount == 0 || _outBytesUsed == _outputHeaderLen && _outputPacketCount == 0) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlSequentialTextReader.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlSequentialTextReader.cs index 436ad67c8e..4070cccc0c 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlSequentialTextReader.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlSequentialTextReader.cs @@ -514,6 +514,10 @@ public override void Convert(byte[] bytes, int byteIndex, int byteCount, char[] completed = (bytesUsed == byteCount); // BlockCopy uses offsets\length measured in bytes, not the actual array index + if (!BitConverter.IsLittleEndian) + { + bytes = Encoding.Unicode.GetBytes(Encoding.BigEndianUnicode.GetString(bytes)); + } Buffer.BlockCopy(bytes, byteIndex, chars, charIndex * 2, bytesUsed); } } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs index 7147c10e5a..8d75b62523 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Buffers.Binary; using System.Collections.Generic; using System.Diagnostics; using System.Security; @@ -488,6 +489,19 @@ internal bool TryStartNewRow(bool isNullCompressed, int nullBitmapColumnsCount = internal bool TryReadChars(char[] chars, int charsOffset, int charsCount, out int charsCopied) { charsCopied = 0; + if (!BitConverter.IsLittleEndian) + { + charsCopied = charsCount; + + for (int ii = 0; ii < charsCopied; ii++) + { + if (!TryReadChar(out chars[charsOffset + ii])) + { + return false; + } + } + return true; + } while (charsCopied < charsCount) { // check if the current buffer contains some bytes we need to copy and copy them diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsValueSetter.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsValueSetter.cs index fd50f68a0b..c3db15cd58 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsValueSetter.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsValueSetter.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Buffers.Binary; using System.Data; using System.Data.SqlTypes; using System.Diagnostics; @@ -586,9 +587,17 @@ internal void SetDateTime(DateTime value) if (SqlDbType.DateTime2 == _metaData.SqlDbType) { long time = value.TimeOfDay.Ticks / TdsEnums.TICKS_FROM_SCALE[_metaData.Scale]; +#if NETCOREAPP + _stateObj.WriteByteArray(BitConverter.GetBytes(BinaryPrimitives.ReadInt64LittleEndian(BitConverter.GetBytes(time))), (int)_metaData.MaxLength - 3, 0); +#else _stateObj.WriteByteArray(BitConverter.GetBytes(time), (int)_metaData.MaxLength - 3, 0); +#endif } +#if NETCOREAPP + _stateObj.WriteByteArray(BitConverter.GetBytes(BinaryPrimitives.ReadInt32LittleEndian(BitConverter.GetBytes(days))), 3, 0); +#else _stateObj.WriteByteArray(BitConverter.GetBytes(days), 3, 0); +#endif } } } @@ -646,7 +655,11 @@ internal void SetTimeSpan(TimeSpan value) _stateObj.WriteByte(length); } long time = value.Ticks / TdsEnums.TICKS_FROM_SCALE[scale]; +#if NETCOREAPP + _stateObj.WriteByteArray(BitConverter.GetBytes(BinaryPrimitives.ReadInt64LittleEndian(BitConverter.GetBytes(time))), length, 0); +#else _stateObj.WriteByteArray(BitConverter.GetBytes(time), length, 0); +#endif } // valid for DateTimeOffset @@ -677,8 +690,13 @@ internal void SetDateTimeOffset(DateTimeOffset value) int days = utcDateTime.Subtract(DateTime.MinValue).Days; short offset = (short)value.Offset.TotalMinutes; +#if NETCOREAPP + _stateObj.WriteByteArray(BitConverter.GetBytes(BinaryPrimitives.ReadInt64LittleEndian(BitConverter.GetBytes(time))), length - 5, 0); // time + _stateObj.WriteByteArray(BitConverter.GetBytes(BinaryPrimitives.ReadInt32LittleEndian(BitConverter.GetBytes(days))), 3, 0); // date +#else _stateObj.WriteByteArray(BitConverter.GetBytes(time), length - 5, 0); // time _stateObj.WriteByteArray(BitConverter.GetBytes(days), 3, 0); // date +#endif _stateObj.WriteByte((byte)(offset & 0xff)); // offset byte 1 _stateObj.WriteByte((byte)((offset >> 8) & 0xff)); // offset byte 2 }