forked from CSGOLeaks/supremacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.h
68 lines (52 loc) · 1.84 KB
/
hash.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
#pragma once
// todo - dex; check arg type for macros?
// hash type.
using hash32_t = uint32_t;
// compile-time hash.
#define HASH( str ) \
[]() { \
constexpr hash32_t out{ FNV1a::get( str ) }; \
\
return out; \
}()
// compile-time const hash.
#define CONST_HASH( str ) FNV1a::get( str )
class FNV1a {
private:
// fnv1a constants.
enum : uint32_t {
PRIME = 0x1000193u,
BASIS = 0x811C9DC5u
};
// compile-time strlen.
// todo - dex; move this?
static __forceinline constexpr size_t ct_strlen( const char *str, bool include_nullchar = false ) {
size_t out{};
// count number of characters.
while( str[ ++out ] != '\0' );
// add one for null character if desired.
if( include_nullchar )
++out;
return out;
}
public:
// hash data ( compile-time or run-time ).
static __forceinline constexpr hash32_t get( const uint8_t *data, const size_t len ) {
hash32_t out{ BASIS };
for( size_t i{}; i < len; ++i )
out = ( out ^ data[ i ] ) * PRIME;
return out;
}
// hash c-style string ( compile-time or run-time ).
static __forceinline constexpr hash32_t get( const char *str ) {
hash32_t out{ BASIS };
size_t len{ ct_strlen( str ) };
for( size_t i{}; i < len; ++i )
out = ( out ^ str[ i ] ) * PRIME;
return out;
}
// hash c++-style string ( run-time only ).
static __forceinline hash32_t get( const std::string &str ) {
return get( (uint8_t *)str.c_str( ), str.size( ) );
}
};