-
Notifications
You must be signed in to change notification settings - Fork 0
/
FNV1.cpp
79 lines (65 loc) · 2.01 KB
/
FNV1.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
// Copyright 2006-12 HumaNature Studios Inc.
#include "FNV1.h"
namespace core {
U32 FNV1::calculateHash(const void* buffer, I32 bufferSize)
{
// Implementation of FNV-1 Hash. if bufferSize == 0, Assumes null terminated strings
static const U32 kFNVOffsetBasis = 2166136261;
U32 hashValue = kFNVOffsetBasis;
const U8* charBuffer = (const U8*)buffer;
if(bufferSize == 0)
{
const U8* charIter = charBuffer;
while(*charIter != '\0')
{
if(*charIter == '\\')
{
hash(hashValue, (U32)'/');
}
else
{
hash(hashValue, (U32)tolower(*charIter));
}
charIter++;
}
}
else
{
for(I32 i = 0; i < bufferSize; ++i)
{
hash(hashValue, (U32)(charBuffer[i]));
}
}
return hashValue;
}
U32 FNV1::calculateHashUnchanged(const void* buffer, I32 bufferSize)
{
// Implementation of FNV-1 Hash. if bufferSize == 0, Assumes null terminated strings
static const U32 kFNVOffsetBasis = 2166136261;
U32 hashValue = kFNVOffsetBasis;
const U8* charBuffer = (const U8*)buffer;
if(bufferSize == 0)
{
const U8* charIter = charBuffer;
while(*charIter != '\0')
{
hash(hashValue, (U32)*charIter);
charIter++;
}
}
else
{
for(I32 i = 0; i < bufferSize; ++i)
{
hash(hashValue, (U32)(charBuffer[i]));
}
}
return hashValue;
}
void FNV1::hash(U32& hashValue, U32 data)
{
static const U32 kFNVPrime = 16777619;
hashValue *= kFNVPrime;
hashValue ^= data;
}
} // namespace core