forked from Phobos-developers/YRpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRC.h
160 lines (139 loc) · 2.76 KB
/
CRC.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
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
#pragma once
#include <ASMMacros.h>
#include <Helpers/CompileTime.h>
class CRCEngine
{
public:
int operator()() const
{
return Value();
}
// Commented out because Ares reimplemented it already!
//
// void operator()(char datum)
// {
// StagingBuffer.Buffer[Index++] = datum;
//
// if (Index == sizeof(int))
// {
// CRC = Value();
// StagingBuffer.Composite = 0;
// Index = 0;
// }
// }
//
// int operator()(void* buffer, int length)
// {
// return (*this)((const void*)buffer, length);
// }
//
// int operator()(const void* buffer, int length)
// {
// if (buffer != nullptr && length > 0)
// {
// const char* dataptr = (char const*)buffer;
// int bytes_left = length;
//
// while (bytes_left && Buffer_Needs_Data())
// {
// operator()(*dataptr);
// ++dataptr;
// --bytes_left;
// }
//
// const int* intptr = (const int*)dataptr;
// int intcount = bytes_left / sizeof(int);
// while (intcount--)
// {
// CRC = Memory(intptr, 4, CRC);
// ++intptr;
// bytes_left -= sizeof(int);
// }
//
// dataptr = (char const*)intptr;
// while (bytes_left)
// {
// operator()(*dataptr);
// ++dataptr;
// --bytes_left;
// }
// }
//
// return Value();
// }
template<typename T>
int operator()(const T& data)
{
return (*this)((const void*)&data, static_cast<int>(sizeof(data)));
}
operator int() const
{
return Value();
}
void operator()(char datum)
{
JMP_THIS(0x4A1C10);
}
void operator()(bool datum)
{
JMP_THIS(0x4A1CA0);
}
void operator()(short datum)
{
JMP_THIS(0x4A1D30);
}
void operator()(int datum)
{
JMP_THIS(0x4A1D50);
}
void operator()(float datum)
{
JMP_THIS(0x4A1D70);
}
void operator()(double datum)
{
JMP_THIS(0x4A1D90);
}
int operator()(const void* buffer, int length)
{
JMP_THIS(0x4A1DE0);
}
static constexpr reference<unsigned int, 0x81F7B4, 256> const Table {};
static int Memory(const void* data, int bytes, int crc)
{
auto buffer = reinterpret_cast<const unsigned char*>(data);
unsigned int ret = ~crc;
for (int i = 0; i < bytes; ++i)
ret = (ret >> 8) ^ Table[*buffer++ ^ (ret & 0xFF)];
return ~ret;
}
static int String(const char* buffer, int crc)
{
unsigned int ret = ~crc;
while (*buffer)
ret = (ret >> 8) ^ Table[*buffer++ ^ (ret & 0xFF)];
return ~ret;
}
protected:
bool Buffer_Needs_Data() const
{
return Index != 0;
}
int Value() const
{
if (!Buffer_Needs_Data())
return CRC;
(char&)StagingBuffer.Buffer[Index] = (char)Index;
for (int i = Index + 1; i < 4; ++i)
(char&)StagingBuffer.Buffer[i] = this->StagingBuffer.Buffer[0];
return Memory(StagingBuffer.Buffer, 4, CRC);
}
public:
int CRC;
int Index;
union
{
int Composite;
char Buffer[sizeof(int)];
} StagingBuffer;
};