-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
EndianAwareBinaryReader.cs
154 lines (111 loc) · 3.45 KB
/
EndianAwareBinaryReader.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.IO;
using System.Text;
namespace LibCpp2IL;
public class EndianAwareBinaryReader : BinaryReader
{
protected bool ShouldReverseArrays = !BitConverter.IsLittleEndian; //Default to LE mode, so on LE systems, don't invert.
public bool IsBigEndian { get; private set; } = false;
private int _numBytesReadSinceLastCall = 0;
public EndianAwareBinaryReader(Stream input) : base(input)
{
}
public EndianAwareBinaryReader(Stream input, Encoding encoding) : base(input, encoding)
{
}
public EndianAwareBinaryReader(Stream input, Encoding encoding, bool leaveOpen) : base(input, encoding, leaveOpen)
{
}
public void SetBigEndian()
{
ShouldReverseArrays = BitConverter.IsLittleEndian; //Set to BE mode, so on LE systems we invert.
IsBigEndian = true;
}
public override bool ReadBoolean()
{
_numBytesReadSinceLastCall += 1;
return base.ReadBoolean();
}
public sealed override byte ReadByte()
{
_numBytesReadSinceLastCall += 1;
return base.ReadByte();
}
public sealed override byte[] ReadBytes(int count)
{
_numBytesReadSinceLastCall += count;
return base.ReadBytes(count);
}
public sealed override char ReadChar()
{
_numBytesReadSinceLastCall += 2;
return base.ReadChar();
}
public sealed override char[] ReadChars(int count)
{
_numBytesReadSinceLastCall += 2 * count;
return base.ReadChars(count);
}
public sealed override short ReadInt16()
{
_numBytesReadSinceLastCall += 2;
if (!ShouldReverseArrays)
return base.ReadInt16();
return this.ReadInt16WithReversedBits();
}
public sealed override int ReadInt32()
{
_numBytesReadSinceLastCall += 4;
if (!ShouldReverseArrays)
return base.ReadInt32();
return this.ReadInt32WithReversedBits();
}
public sealed override long ReadInt64()
{
_numBytesReadSinceLastCall += 8;
if (!ShouldReverseArrays)
return base.ReadInt64();
return this.ReadInt64WithReversedBits();
}
public sealed override ushort ReadUInt16()
{
_numBytesReadSinceLastCall += 2;
if (!ShouldReverseArrays)
return base.ReadUInt16();
return this.ReadUInt16WithReversedBits();
}
public sealed override uint ReadUInt32()
{
_numBytesReadSinceLastCall += 4;
if (!ShouldReverseArrays)
return base.ReadUInt32();
return this.ReadUInt32WithReversedBits();
}
public sealed override ulong ReadUInt64()
{
_numBytesReadSinceLastCall += 8;
if (!ShouldReverseArrays)
return base.ReadUInt64();
return this.ReadUInt64WithReversedBits();
}
public sealed override float ReadSingle()
{
_numBytesReadSinceLastCall += 4;
if (!ShouldReverseArrays)
return base.ReadSingle();
return this.ReadSingleWithReversedBits();
}
public sealed override double ReadDouble()
{
_numBytesReadSinceLastCall += 8;
if (!ShouldReverseArrays)
return base.ReadDouble();
return this.ReadDoubleWithReversedBits();
}
protected int GetNumBytesReadSinceLastCallAndClear()
{
var numBytesRead = _numBytesReadSinceLastCall;
_numBytesReadSinceLastCall = 0;
return numBytesRead;
}
}