-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTools.cs
185 lines (155 loc) · 4.19 KB
/
BinaryTools.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace MsbtEditor
{
public enum ByteOrder
{
LittleEndian = 0,
BigEndian = 1
}
class BinaryReaderX : BinaryReader
{
public ByteOrder ByteOrder { get; set; }
public BinaryReaderX(Stream input, ByteOrder byteOrder = ByteOrder.LittleEndian)
: base(input)
{
ByteOrder = byteOrder;
}
public BinaryReaderX(Stream input, Encoding encoding, ByteOrder byteOrder = ByteOrder.LittleEndian)
: base(input, encoding)
{
ByteOrder = byteOrder;
}
public override short ReadInt16()
{
if (ByteOrder == ByteOrder.LittleEndian)
return base.ReadInt16();
else
return BitConverter.ToInt16(base.ReadBytes(2).Reverse().ToArray(), 0);
}
public override int ReadInt32()
{
if (ByteOrder == ByteOrder.LittleEndian)
return base.ReadInt32();
else
return BitConverter.ToInt32(base.ReadBytes(4).Reverse().ToArray(), 0);
}
public override long ReadInt64()
{
if (ByteOrder == ByteOrder.LittleEndian)
return base.ReadInt64();
else
return BitConverter.ToInt64(base.ReadBytes(8).Reverse().ToArray(), 0);
}
public override ushort ReadUInt16()
{
if (ByteOrder == ByteOrder.LittleEndian)
return base.ReadUInt16();
else
return BitConverter.ToUInt16(base.ReadBytes(2).Reverse().ToArray(), 0);
}
public override uint ReadUInt32()
{
if (ByteOrder == ByteOrder.LittleEndian)
return base.ReadUInt32();
else
return BitConverter.ToUInt32(base.ReadBytes(4).Reverse().ToArray(), 0);
}
public override ulong ReadUInt64()
{
if (ByteOrder == ByteOrder.LittleEndian)
return base.ReadUInt64();
else
return BitConverter.ToUInt64(base.ReadBytes(8).Reverse().ToArray(), 0);
}
public string ReadString(int length)
{
return Encoding.ASCII.GetString(ReadBytes(length)).TrimEnd('\0');
}
public string ReadString(int length, Encoding encoding)
{
return encoding.GetString(ReadBytes(length)).TrimEnd('\0');
}
public string PeekString(int length = 4)
{
List<byte> bytes = new List<byte>();
long startOffset = BaseStream.Position;
for (int i = 0; i < length; i++)
bytes.Add(ReadByte());
BaseStream.Seek(startOffset, SeekOrigin.Begin);
return Encoding.ASCII.GetString(bytes.ToArray());
}
public string PeekString(int length, Encoding encoding)
{
List<byte> bytes = new List<byte>();
long startOffset = BaseStream.Position;
for (int i = 0; i < length; i++)
bytes.Add(ReadByte());
BaseStream.Seek(startOffset, SeekOrigin.Begin);
return encoding.GetString(bytes.ToArray());
}
}
class BinaryWriterX : BinaryWriter
{
public ByteOrder ByteOrder { get; set; }
public BinaryWriterX(Stream input, ByteOrder byteOrder = ByteOrder.LittleEndian)
: base(input)
{
ByteOrder = byteOrder;
}
public BinaryWriterX(Stream input, Encoding encoding, ByteOrder byteOrder = ByteOrder.LittleEndian)
: base(input, encoding)
{
ByteOrder = byteOrder;
}
public override void Write(short value)
{
if (ByteOrder == ByteOrder.LittleEndian)
base.Write(value);
else
base.Write(BitConverter.GetBytes(value).Reverse().ToArray());
}
public override void Write(int value)
{
if (ByteOrder == ByteOrder.LittleEndian)
base.Write(value);
else
base.Write(BitConverter.GetBytes(value).Reverse().ToArray());
}
public override void Write(long value)
{
if (ByteOrder == ByteOrder.LittleEndian)
base.Write(value);
else
base.Write(BitConverter.GetBytes(value).Reverse().ToArray());
}
public override void Write(ushort value)
{
if (ByteOrder == ByteOrder.LittleEndian)
base.Write(value);
else
base.Write(BitConverter.GetBytes(value).Reverse().ToArray());
}
public override void Write(uint value)
{
if (ByteOrder == ByteOrder.LittleEndian)
base.Write(value);
else
base.Write(BitConverter.GetBytes(value).Reverse().ToArray());
}
public override void Write(ulong value)
{
if (ByteOrder == ByteOrder.LittleEndian)
base.Write(value);
else
base.Write(BitConverter.GetBytes(value).Reverse().ToArray());
}
public void WriteASCII(string value)
{
base.Write(Encoding.ASCII.GetBytes(value));
}
}
}