Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
PEBinaryReader.cs
Go to the documentation of this file.
1using System.IO;
2using System.Text;
3
5
6internal readonly struct PEBinaryReader
7{
8 private readonly long _startOffset;
9
10 private readonly long _maxOffset;
11
12 private readonly BinaryReader _reader;
13
15
16 public PEBinaryReader(Stream stream, int size)
17 {
18 _startOffset = stream.Position;
19 _maxOffset = _startOffset + size;
20 _reader = new BinaryReader(stream, Encoding.UTF8, leaveOpen: true);
21 }
22
23 public void Seek(int offset)
24 {
27 }
28
29 public byte[] ReadBytes(int count)
30 {
32 return _reader.ReadBytes(count);
33 }
34
35 public byte ReadByte()
36 {
37 CheckBounds(1u);
38 return _reader.ReadByte();
39 }
40
41 public short ReadInt16()
42 {
43 CheckBounds(2u);
44 return _reader.ReadInt16();
45 }
46
47 public ushort ReadUInt16()
48 {
49 CheckBounds(2u);
50 return _reader.ReadUInt16();
51 }
52
53 public int ReadInt32()
54 {
55 CheckBounds(4u);
56 return _reader.ReadInt32();
57 }
58
59 public uint ReadUInt32()
60 {
61 CheckBounds(4u);
62 return _reader.ReadUInt32();
63 }
64
65 public ulong ReadUInt64()
66 {
67 CheckBounds(8u);
68 return _reader.ReadUInt64();
69 }
70
71 public string ReadNullPaddedUTF8(int byteCount)
72 {
73 byte[] array = ReadBytes(byteCount);
74 int count = 0;
75 for (int num = array.Length; num > 0; num--)
76 {
77 if (array[num - 1] != 0)
78 {
79 count = num;
80 break;
81 }
82 }
83 return Encoding.UTF8.GetString(array, 0, count);
84 }
85
86 private void CheckBounds(uint count)
87 {
88 if ((ulong)(_reader.BaseStream.Position + count) > (ulong)_maxOffset)
89 {
91 }
92 }
93
94 private void CheckBounds(long startPosition, int count)
95 {
96 if ((ulong)(startPosition + (uint)count) > (ulong)_maxOffset)
97 {
99 }
100 }
101}
virtual byte ReadByte()
virtual ulong ReadUInt64()
virtual ushort ReadUInt16()
virtual int ReadInt32()
virtual Stream BaseStream
virtual short ReadInt16()
virtual byte[] ReadBytes(int count)
virtual uint ReadUInt32()
long Seek(long offset, SeekOrigin origin)
static void ImageTooSmallOrContainsInvalidOffsetOrCount()
Definition Throw.cs:236
static void ImageTooSmall()
Definition Throw.cs:229
static Encoding UTF8
Definition Encoding.cs:526
void CheckBounds(long startPosition, int count)