forked from derandark/PhatAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryReader.h
128 lines (103 loc) · 2.52 KB
/
BinaryReader.h
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
#pragma once
#define MAX_MEALSTRING_LEN 0x0800
class BinaryReader
{
public:
BinaryReader(BYTE *pData, DWORD dwSize);
~BinaryReader();
#define BOUND_CHECK(x) \
if ( !(x) ) \
{ \
m_dwErrorCode = 1; \
memset(&returnValue, 0, sizeof(returnValue)); \
return returnValue; \
}
template <typename ReturnType> ReturnType Read()
{
ReturnType returnValue;
BOUND_CHECK((m_pData + sizeof(ReturnType)) <= m_pEnd);
returnValue = *((ReturnType *)m_pData);
m_pData += sizeof(ReturnType);
return returnValue;
}
#define STREAM_OUT(func, type) type func() { return Read<type>(); }
STREAM_OUT(ReadChar, BYTE);
STREAM_OUT(ReadShort, BYTE);
STREAM_OUT(ReadLong, BYTE);
STREAM_OUT(ReadBYTE, BYTE);
STREAM_OUT(ReadWORD, WORD);
STREAM_OUT(ReadDWORD, DWORD);
STREAM_OUT(ReadFloat, float);
STREAM_OUT(ReadDouble, double);
STREAM_OUT(ReadByte, BYTE);
STREAM_OUT(ReadInt8, char);
STREAM_OUT(ReadUInt8, BYTE);
STREAM_OUT(ReadInt16, short);
STREAM_OUT(ReadUInt16, WORD);
STREAM_OUT(ReadInt32, int);
STREAM_OUT(ReadUInt32, DWORD);
STREAM_OUT(ReadSingle, float);
__forceinline DWORD ReadPackedDWORD()
{
DWORD returnValue;
BOUND_CHECK((m_pData + sizeof(WORD)) <= m_pEnd);
returnValue = *((WORD *)m_pData);
if (returnValue & 0x8000)
{
BOUND_CHECK((m_pData + sizeof(DWORD)) <= m_pEnd);
DWORD src = *((DWORD *)m_pData);
returnValue = (((src & 0x3FFF) << 16) | (src >> 16));
m_pData += sizeof(DWORD);
}
else
{
m_pData += sizeof(WORD);
}
return returnValue;
}
template<typename A, typename B> std::map<A, B> ReadMap()
{
std::map<A, B> table;
WORD count = ReadWORD();
ReadWORD();
while (count > 0 && !m_dwErrorCode)
{
A theKey = Read<A>();
B theValue = Read<B>();
table.insert(std::pair<A, B>(theKey, theValue));
count--;
}
return table;
}
template<typename A> std::map<A, std::string> ReadMap()
{
std::map<A, std::string> table;
WORD count = ReadWORD();
ReadWORD();
while (count > 0 && !m_dwErrorCode)
{
A theKey = Read<A>();
std::string theValue = ReadString();
table.insert(std::pair<A, std::string>(theKey, theValue));
count--;
}
return table;
}
void ReadAlign(void);
void *ReadArray(size_t size);
char *ReadString(void);
BYTE *GetDataStart(void);
BYTE *GetDataPtr(void);
BYTE *GetDataEnd(void);
DWORD GetDataLen(void);
DWORD GetOffset(void);
DWORD GetLastError(void);
DWORD GetDataRemaining(void);
void SetOffset(DWORD offset);
private:
DWORD m_dwErrorCode;
BYTE *m_pData;
BYTE *m_pStart;
BYTE *m_pEnd;
std::list<char *> m_lStrings;
};