forked from simontime/Resead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seadHash.cpp
108 lines (82 loc) · 1.66 KB
/
seadHash.cpp
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
#include "seadHash.h"
namespace sead
{
bool HashCRC16::sInitialized;
u16 HashCRC16::sTable[256];
void HashCRC16::initialize()
{
for (int i = 0; i < 256; i++)
{
u16 val = i;
for (int j = 0; j < 8; j++)
{
if (val & 1)
{
val >>= 1;
val ^= 0xA001;
}
else
val >>= 1;
}
sTable[i] = val;
}
sInitialized = true;
}
u16 HashCRC16::calcHash(void const *data, u32 length)
{
if (!sInitialized)
initialize();
u16 hash = 0;
for (u32 i = 0; i < length; i++)
hash = sTable[((u8 *)data)[i] ^ (hash & 0xFF)] ^ (hash >> 8);
return hash;
}
u16 HashCRC16::calcStringHash(char const *string)
{
if (!sInitialized)
initialize();
u16 hash = 0;
while (*string)
hash = sTable[*string++ ^ (hash & 0xFF)] ^ (hash >> 8);
return hash;
}
bool HashCRC32::sInitialized;
u32 HashCRC32::sTable[256];
void HashCRC32::initialize()
{
for (int i = 0; i < 256; i++)
{
u32 val = i;
for (int j = 0; j < 8; j++)
{
if (val & 1)
{
val >>= 1;
val ^= 0xEDB88320;
}
else
val >>= 1;
}
sTable[i] = val;
}
sInitialized = true;
}
u32 HashCRC32::calcHash(void const *data, u32 length)
{
if (!sInitialized)
initialize();
u32 hash = ~0;
for (u32 i = 0; i < length; i++)
hash = sTable[((u8 *)data)[i] ^ (hash & 0xFF)] ^ (hash >> 8);
return ~hash;
}
u32 HashCRC32::calcStringHash(char const *string)
{
if (!sInitialized)
initialize();
u32 hash = ~0;
while (*string)
hash = sTable[*string++ ^ (hash & 0xFF)] ^ (hash >> 8);
return ~hash;
}
}