-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBinaryReader.cs
More file actions
63 lines (56 loc) · 1.89 KB
/
BinaryReader.cs
File metadata and controls
63 lines (56 loc) · 1.89 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
60
61
62
63
using Atlasd.Battlenet.Exceptions;
using System.IO;
using System.Text;
namespace Atlasd.Battlenet
{
class BinaryReader : System.IO.BinaryReader
{
private readonly object _lock = new object();
public BinaryReader(Stream input) : base(input, Encoding.UTF8) { }
public BinaryReader(Stream input, Encoding encoding) : base(input, encoding) { }
public BinaryReader(Stream input, Encoding encoding, bool leaveOpen) : base(input, encoding, leaveOpen) { }
public long GetNextNull()
{
lock (_lock)
{
long lastPosition = BaseStream.Position;
while (BaseStream.Position < BaseStream.Length)
{
if (ReadByte() == 0)
{
long r = BaseStream.Position;
BaseStream.Position = lastPosition;
return r;
}
}
BaseStream.Position = lastPosition;
return -1;
}
}
public byte[] ReadByteString()
{
lock (_lock)
{
var nullPos = GetNextNull();
if (nullPos < 0)
{
throw new GameProtocolViolationException(null,
$"Truncated string field at stream position {BaseStream.Position}: missing null terminator");
}
var size = nullPos - BaseStream.Position;
return ReadBytes((int)size)[..^1];
}
}
public override string ReadString()
{
lock (_lock)
{
string str = "";
char chr;
while (BaseStream.Position < BaseStream.Length && (int)(chr = ReadChar()) != 0)
str += chr;
return str;
}
}
}
}