Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Avoid a length check introduced by the use of BinaryPrimitives.
  • Loading branch information
teo-tsirpanis committed Oct 27, 2022
commit 8ebbbfa6cd59e759dbd0495465e982b54311b737
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Reflection.Internal;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace System.Reflection
{
Expand All @@ -19,20 +20,12 @@ public static void WriteBytes(this byte[] buffer, int start, byte value, int byt

public static void WriteDouble(this byte[] buffer, int start, double value)
{
#if NETCOREAPP
BinaryPrimitives.WriteDoubleLittleEndian(buffer.AsSpan(start), value);
#else
WriteUInt64(buffer, start, *(ulong*)&value);
#endif
}

public static void WriteSingle(this byte[] buffer, int start, float value)
{
#if NETCOREAPP
BinaryPrimitives.WriteSingleLittleEndian(buffer.AsSpan(start), value);
#else
WriteUInt32(buffer, start, *(uint*)&value);
#endif
}

public static void WriteByte(this byte[] buffer, int start, byte value)
Expand All @@ -43,27 +36,47 @@ public static void WriteByte(this byte[] buffer, int start, byte value)

public static void WriteUInt16(this byte[] buffer, int start, ushort value)
{
BinaryPrimitives.WriteUInt16LittleEndian(buffer.AsSpan(start), value);
if (!BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Unsafe.WriteUnaligned(ref buffer[start], value);
}

public static void WriteUInt16BE(this byte[] buffer, int start, ushort value)
{
BinaryPrimitives.WriteUInt16BigEndian(buffer.AsSpan(start), value);
if (BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Unsafe.WriteUnaligned(ref buffer[start], value);
}

public static void WriteUInt32BE(this byte[] buffer, int start, uint value)
{
BinaryPrimitives.WriteUInt32BigEndian(buffer.AsSpan(start), value);
if (BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Unsafe.WriteUnaligned(ref buffer[start], value);
}

public static void WriteUInt32(this byte[] buffer, int start, uint value)
{
BinaryPrimitives.WriteUInt32LittleEndian(buffer.AsSpan(start), value);
if (!BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Unsafe.WriteUnaligned(ref buffer[start], value);
}

public static void WriteUInt64(this byte[] buffer, int start, ulong value)
{
BinaryPrimitives.WriteUInt64LittleEndian(buffer.AsSpan(start), value);
if (!BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Unsafe.WriteUnaligned(ref buffer[start], value);
}

public const int SizeOfSerializedDecimal = sizeof(byte) + 3 * sizeof(uint);
Expand Down