-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryStream.cs
351 lines (292 loc) · 11.4 KB
/
BinaryStream.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using System.Text;
using System.Runtime.CompilerServices;
namespace Binary_Stream;
/// <summary>
/// A stream of data with support for endianness, seeking, aligning and better string reading/writing.
/// </summary>
public class BinaryStream : MemoryStream {
/// <summary>
/// The native endianness of your computer architecture.
/// </summary>
public readonly static Endian Native = BitConverter.IsLittleEndian ? Endian.Little : Endian.Big;
/// <summary>
/// If the read bytes should be reversed to get the proper endianness.
/// </summary>
public bool Reverse => Endian != Native;
/// <summary>
/// The endianness of this stream. Set to <seealso cref="Native"/> by default.
/// </summary>
public Endian Endian { get; set; } = Native;
/// <summary>
/// The default encoding for strings parsed from this stream. Set to <seealso cref="Encoding.UTF8"/> by default.
/// </summary>
public Encoding Encoding { get; set; } = Encoding.UTF8;
/// <summary>
/// Creates a <see cref="BinaryStream"/>.
/// </summary>
public BinaryStream() : base() { }
/// <summary>
/// Creates a <see cref="BinaryStream"/> with the specified endianness.
/// </summary>
/// <param name="endian">The endianness of this stream, will default to the <see cref="Native"/> endianness.</param>
/// <param name="encoding">The encoding of this stream, will default to the <see cref="Encoding.UTF8"/> encoding.</param>
public BinaryStream(Endian? endian, Encoding? encoding = null) : base() {
Endian = endian ?? Native;
Encoding = encoding ?? Encoding;
}
/// <summary>
/// Creates a <see cref="BinaryStream"/> with the specified capacity.
/// </summary>
/// <param name="capacity">The capacity of this stream.</param>
/// <param name="endian">The endianness of this stream, will default to the <see cref="Native"/> endianness.</param>
/// <param name="encoding">The encoding of this stream, will default to the <see cref="Encoding.UTF8"/> encoding.</param>
public BinaryStream(int capacity, Endian? endian = null, Encoding? encoding = null) : base(capacity) {
Endian = endian ?? Native;
Encoding = encoding ?? Encoding;
}
/// <summary>
/// Creates a <see cref="BinaryStream"/> from a <see cref="ReadOnlySpan{byte}"/> of bytes.
/// </summary>
/// <param name="bytes">The bytes to copy to this stream.</param>
/// <param name="endian">The endianness of this stream, will default to the <see cref="Native"/> endianness.</param>
/// <param name="encoding">The encoding of this stream, will default to the <see cref="Encoding.UTF8"/> encoding.</param>
public BinaryStream(ReadOnlySpan<byte> bytes, Endian? endian = null, Encoding? encoding = null) : base(bytes.Length) {
Write(bytes);
Position = 0;
Endian = endian ?? Native;
Encoding = encoding ?? Encoding;
}
/// <summary>
/// Creates a <see cref="BinaryStream"/> from an existing stream.
/// </summary>
/// <param name="sourceStream">The stream to copy the data from.</param>
/// <param name="endian">The endianness of this stream, will default to the <see cref="Native"/> endianness.</param>
/// <param name="encoding">The encoding of this stream, will default to the <see cref="Encoding.UTF8"/> encoding.</param>
public BinaryStream(Stream sourceStream, Endian? endian = null, Encoding? encoding = null) : base((int)sourceStream.Length) {
long oldpos = sourceStream.Position;
sourceStream.Position = 0;
sourceStream.CopyTo(this);
sourceStream.Position = oldpos;
Position = 0;
Endian = endian ?? Native;
Encoding = encoding ?? Encoding;
}
#region // ----- Unmanaged Reading/Writing ----- //
/// <summary>
/// Reads an unmanaged type from this stream.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T ReadUnmanaged<T>() where T : unmanaged {
T result = new();
var span = Util.SpanFromRef(ref result);
ReadExactly(span);
if (Reverse) {
span.Reverse();
}
return result;
}
/// <summary>
/// Reads an unmanaged type and sets the value into a variable.
/// </summary>
/// <remarks>
/// Due to the nature of unmanaged reading works, structs might be read incorrectly.
/// </remarks>
/// <param name="value">The variable to set the value to.</param>
public BinaryStream ReadUnmanaged<T>(ref T value) where T : unmanaged {
value = ReadUnmanaged<T>();
return this;
}
/// <summary>
/// Writes an unmanaged type to this stream.
/// </summary>
/// <param name="value">The value to be written.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public BinaryStream WriteUnmanaged<T>(T value) where T : unmanaged {
var bytes = Util.SpanFromRef(ref value);
if (Reverse) {
bytes.Reverse();
}
Write(bytes);
return this;
}
/// <summary>
/// Writes multiple unmanaged values to this stream.
/// </summary>
/// <param name="values">The values to be written.</param>
public BinaryStream WriteUnmanaged<T>(params T[] values) where T : unmanaged {
foreach (var value in values) {
WriteUnmanaged(value);
}
return this;
}
/// <summary>
/// Reads a number of bytes from the current position.
/// </summary>
public byte[] ReadBytes(int count) {
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(count, nameof(count));
byte[] buffer = new byte[count];
Read(buffer);
return buffer;
}
public BinaryStream CreateSubStream(long size) {
Span<byte> bytes = new byte[size];
Read(bytes);
return new BinaryStream(bytes, Endian) { Encoding = Encoding };
}
public bool TryRead<T>(out T output) where T : IRead, new()
{
output = new();
try
{
output.Read(this);
return true;
} catch
{
return false;
}
}
public bool TryWrite<T>(T item) where T : IWrite
{
try
{
item.Write(this);
return true;
} catch
{
return false;
}
}
#endregion
#region // ----- String Reading/Writing ----- //
/// <summary>
/// Reads a string with the specified length and encoding.
/// </summary>
public string ReadString(int length, Encoding? enc = null) {
byte[] data = new byte[length];
Read(data);
enc ??= Encoding;
return enc.GetString(data);
}
/// <summary>
/// Reads a null-terminated string.
/// </summary>
public string ReadNTString(Encoding? enc = null, byte capacity = 127) {
var bytes = new List<byte>(capacity);
for (var b = ReadUInt8(); b != 0; b = ReadUInt8()) {
bytes.Add(b);
}
enc ??= Encoding;
return enc.GetString([.. bytes]);
}
/// <summary>
/// Reads a null-terminated string at a specific position.
/// </summary>
public string ReadNTStringAt(long offset, Encoding? enc = null) {
return this.SeekTask(offset, x => x.ReadNTString(enc));
}
/// <summary>
/// Reads a null-terminated string at a specific position.
/// </summary>
public string ReadNTStringAt(long offset, SeekOrigin origin, Encoding? enc = null) {
return this.SeekTask(offset, origin, x => x.ReadNTString(enc));
}
/// <summary>
/// Writes a string to the file with a specific encoding.
/// </summary>
public BinaryStream WriteString(string value, Encoding? enc = null) {
enc ??= Encoding;
Write(enc.GetBytes(value));
return this;
}
/// <summary>
/// Writes a null-terminated string with a specific encoding.
/// </summary>
public BinaryStream WriteNTString(string value, Encoding? enc = null) {
WriteString(value + '\0', enc);
return this;
}
/// <summary>
/// Writes multiple null-terminated strings with a specific encoding.
/// </summary>
public BinaryStream WriteNTStrings(Encoding? enc, params string[] values) {
WriteString(string.Join('\0', values) + '\0', enc);
return this;
}
#endregion
#region // ----- Items Reading/Writing ----- //
/// <summary>
/// Reads a type that implements <see cref="IRead"/>. Type must have a default ctor.
/// </summary>
public T ReadItem<T>() where T : IRead, new() {
T res = new();
res.Read(this);
return res;
}
/// <summary>
/// Reads a type that implements <see cref="IRead"/> into a variable.
/// </summary>
public BinaryStream ReadItem<T>(ref T item) where T : IRead {
item.Read(this);
return this;
}
/// <summary>
/// Writes a variable that implements <see cref="IWrite"/>.
/// </summary>
public BinaryStream WriteItem<T>(T item) where T : IWrite {
item.Write(this);
return this;
}
#endregion
#region // ----- Positioning ----- //
public Seek<BinaryStream> TemporarySeek(long offset, SeekOrigin origin = SeekOrigin.Begin)
=> new(this, offset, origin);
/// <summary>
/// Skips a number of bytes from the current position.
/// </summary>
public BinaryStream Skip(long count) {
Seek(count, SeekOrigin.Current);
return this;
}
/// <summary>
/// Aligns the stream position to the specified <paramref name="alignment"/>.
/// </summary>
public BinaryStream AlignTo(int alignment) {
Seek(alignment - (Position % alignment), SeekOrigin.Current);
return this;
}
/// <summary>
/// Writes <paramref name="value"/> until the stream position is aligned to <paramref name="alignment"/>.
/// </summary>
public BinaryStream WriteUntilAligned(int alignment, byte value) {
var writeCount = alignment - (Position % alignment);
for (long i = 0; i < writeCount; i++) {
WriteByte(value);
}
return this;
}
#endregion
#region // ----- Reading/Writing Utilities ----- //
public bool ReadBool() => ReadByte() != 0;
public sbyte ReadInt8() => (sbyte)ReadByte();
public byte ReadUInt8() => (byte)ReadByte();
public short ReadInt16() => ReadUnmanaged<short>();
public ushort ReadUInt16() => ReadUnmanaged<ushort>();
public int ReadInt32() => ReadUnmanaged<int>();
public uint ReadUInt32() => ReadUnmanaged<uint>();
public long ReadInt64() => ReadUnmanaged<long>();
public ulong ReadUInt64() => ReadUnmanaged<ulong>();
public float ReadSingle() => ReadUnmanaged<float>();
public double ReadDouble() => ReadUnmanaged<double>();
public void WriteBool(bool value) => WriteByte(value ? (byte)1 : (byte)0);
public void WriteInt8(sbyte value) => WriteUnmanaged(value);
public void WriteUInt8(byte value) => WriteByte(value);
public void WriteInt16(short value) => WriteUnmanaged(value);
public void WriteUInt16(ushort value) => WriteUnmanaged(value);
public void WriteInt32(int value) => WriteUnmanaged(value);
public void WriteUInt32(uint value) => WriteUnmanaged(value);
public void WriteInt64(long value) => WriteUnmanaged(value);
public void WriteUInt64(ulong value) => WriteUnmanaged(value);
public void WriteSingle(float value) => WriteUnmanaged(value);
public void WriteDouble(double value) => WriteUnmanaged(value);
#endregion
}