-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
BinaryReaderHelpers.cs
63 lines (50 loc) · 1.94 KB
/
BinaryReaderHelpers.cs
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
60
61
62
63
using System;
using System.IO;
namespace LibCpp2IL;
public static class BinaryReaderHelpers
{
public static byte[] Reverse(this byte[] b)
{
Array.Reverse(b);
return b;
}
public static ushort ReadUInt16WithReversedBits(this BinaryReader binRdr)
{
return BitConverter.ToUInt16(binRdr.ReadBytesRequired(sizeof(ushort)).Reverse(), 0);
}
public static short ReadInt16WithReversedBits(this BinaryReader binRdr)
{
return BitConverter.ToInt16(binRdr.ReadBytesRequired(sizeof(short)).Reverse(), 0);
}
public static uint ReadUInt32WithReversedBits(this BinaryReader binRdr)
{
return BitConverter.ToUInt32(binRdr.ReadBytesRequired(sizeof(uint)).Reverse(), 0);
}
public static int ReadInt32WithReversedBits(this BinaryReader binRdr)
{
return BitConverter.ToInt32(binRdr.ReadBytesRequired(sizeof(int)).Reverse(), 0);
}
public static ulong ReadUInt64WithReversedBits(this BinaryReader binRdr)
{
return BitConverter.ToUInt64(binRdr.ReadBytesRequired(sizeof(ulong)).Reverse(), 0);
}
public static long ReadInt64WithReversedBits(this BinaryReader binRdr)
{
return BitConverter.ToInt64(binRdr.ReadBytesRequired(sizeof(long)).Reverse(), 0);
}
public static float ReadSingleWithReversedBits(this BinaryReader binRdr)
{
return BitConverter.ToSingle(binRdr.ReadBytesRequired(sizeof(float)).Reverse(), 0);
}
public static double ReadDoubleWithReversedBits(this BinaryReader binRdr)
{
return BitConverter.ToDouble(binRdr.ReadBytesRequired(sizeof(double)).Reverse(), 0);
}
private static byte[] ReadBytesRequired(this BinaryReader binRdr, int byteCount)
{
var result = binRdr.ReadBytes(byteCount);
if (result.Length != byteCount)
throw new EndOfStreamException($"{byteCount} bytes required from stream, but only {result.Length} returned.");
return result;
}
}