-
-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathBinaryReaderHelpers.cs
More file actions
59 lines (49 loc) · 1.7 KB
/
BinaryReaderHelpers.cs
File metadata and controls
59 lines (49 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Buffers.Binary;
using System.IO;
using System.Runtime.CompilerServices;
namespace LibCpp2IL;
public static class BinaryReaderHelpers
{
public static ushort ReadUInt16WithReversedBits(this BinaryReader binRdr)
{
return BinaryPrimitives.ReverseEndianness(binRdr.ReadUInt16());
}
public static short ReadInt16WithReversedBits(this BinaryReader binRdr)
{
return BinaryPrimitives.ReverseEndianness(binRdr.ReadInt16());
}
public static uint ReadUInt32WithReversedBits(this BinaryReader binRdr)
{
return BinaryPrimitives.ReverseEndianness(binRdr.ReadUInt32());
}
public static int ReadInt32WithReversedBits(this BinaryReader binRdr)
{
return BinaryPrimitives.ReverseEndianness(binRdr.ReadInt32());
}
public static ulong ReadUInt64WithReversedBits(this BinaryReader binRdr)
{
return BinaryPrimitives.ReverseEndianness(binRdr.ReadUInt64());
}
public static long ReadInt64WithReversedBits(this BinaryReader binRdr)
{
return BinaryPrimitives.ReverseEndianness(binRdr.ReadInt64());
}
public static float ReadSingleWithReversedBits(this BinaryReader binRdr)
{
return BitCast<uint, float>(binRdr.ReadUInt32WithReversedBits());
}
public static double ReadDoubleWithReversedBits(this BinaryReader binRdr)
{
return BitCast<ulong, double>(binRdr.ReadUInt64WithReversedBits());
}
private static TTo BitCast<TFrom, TTo>(TFrom source)
where TFrom : unmanaged
where TTo : unmanaged
{
#if NET8_0_OR_GREATER
return Unsafe.BitCast<TFrom, TTo>(source);
#else
return Unsafe.ReadUnaligned<TTo>(ref Unsafe.As<TFrom, byte>(ref source));
#endif
}
}