1
2
3
4
5
6 namespace System
7 {
8 public class MemoryReader
9 {
10 public nothrow MemoryReader(byte* ptr_, long count_) : ptr(ptr_), pos(ptr), count(count_)
11 {
12 }
13 public byte ReadByte()
14 {
15 if (pos - ptr >= count)
16 {
17 throw Exception("memory reader: unexpected end of data");
18 }
19 return *pos++;
20 }
21 public sbyte ReadSByte()
22 {
23 return cast<sbyte>(ReadByte());
24 }
25 public ushort ReadUShort()
26 {
27 byte b0 = ReadByte();
28 byte b1 = ReadByte();
29 return (cast<ushort>(b0) << 8u) | cast<ushort>(b1);
30 }
31 public short ReadShort()
32 {
33 return cast<short>(ReadUShort());
34 }
35 public uint ReadUInt()
36 {
37 byte b0 = ReadByte();
38 byte b1 = ReadByte();
39 byte b2 = ReadByte();
40 byte b3 = ReadByte();
41 return (cast<uint>(b0) << 24u) | (cast<uint>(b1) << 16u) | (cast<uint>(b2) << 8u) | cast<uint>(b3);
42 }
43 public int ReadInt()
44 {
45 return cast<int>(ReadUInt());
46 }
47 public ulong ReadULong()
48 {
49 byte b0 = ReadByte();
50 byte b1 = ReadByte();
51 byte b2 = ReadByte();
52 byte b3 = ReadByte();
53 byte b4 = ReadByte();
54 byte b5 = ReadByte();
55 byte b6 = ReadByte();
56 byte b7 = ReadByte();
57 return (cast<ulong>(b0) << 56u) | (cast<ulong>(b1) << 48u) | (cast<ulong>(b2) << 40u) | (cast<ulong>(b3) << 32u) |
58 (cast<ulong>(b4) << 24u) | (cast<ulong>(b5) << 16u) | (cast<ulong>(b6) << 8u) | cast<ulong>(b7);
59 }
60 public long ReadLong()
61 {
62 return cast<long>(ReadULong());
63 }
64 public DateTime ReadDateTime()
65 {
66 short year = ReadShort();
67 Month month = cast<Month>(ReadSByte());
68 sbyte day = ReadSByte();
69 Date date(year, month, day);
70 int secs = ReadInt();
71 DateTime dt(date, secs);
72 return dt;
73 }
74 public string ReadString()
75 {
76 string result;
77 byte b = ReadByte();
78 while (b != 0u)
79 {
80 result.Append(cast<char>(b));
81 b = ReadByte();
82 }
83 return result;
84 }
85 private byte* ptr;
86 private byte* pos;
87 private long count;
88 }
89 }