1 #pragma once
2 
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <utility>
6 #include <map>
7 
8 namespace fuzzing {
9 namespace datasource  {
10 
11 /* From: https://gist.github.com/underscorediscovery/81308642d0325fd386237cfa3b44785c */
hash_64_fnv1a(const void * key,const uint64_t len)12 inline uint64_t hash_64_fnv1a(const void* key, const uint64_t len) {
13 
14     const char* data = (char*)key;
15     uint64_t hash = 0xcbf29ce484222325;
16     uint64_t prime = 0x100000001b3;
17 
18     for(uint64_t i = 0; i < len; ++i) {
19         uint8_t value = data[i];
20         hash = hash ^ value;
21         hash *= prime;
22     }
23 
24     return hash;
25 
26 } //hash_64_fnv1a
27 
28 // FNV1a c++11 constexpr compile time hash functions, 32 and 64 bit
29 // str should be a null terminated string literal, value should be left out
30 // e.g hash_32_fnv1a_const("example")
31 // code license: public domain or equivalent
32 // post: https://notes.underscorediscovery.com/constexpr-fnv1a/
33 
34 constexpr uint32_t val_32_const = 0x811c9dc5;
35 constexpr uint32_t prime_32_const = 0x1000193;
36 constexpr uint64_t val_64_const = 0xcbf29ce484222325;
37 constexpr uint64_t prime_64_const = 0x100000001b3;
38 
39 
ID(const char * const str,const uint64_t value=val_64_const)40 inline constexpr uint64_t ID(const char* const str, const uint64_t value = val_64_const) noexcept {
41     auto ret = (str[0] == '\0') ? value : ID(&str[1], (value ^ uint64_t(str[0])) * prime_64_const);
42     return ret;
43 }
44 
IDPair(const char * const str,const uint64_t value=val_64_const)45 inline constexpr std::pair<const char*, uint64_t> IDPair(const char* const str, const uint64_t value = val_64_const) noexcept {
46     return {str, ID(str, value)};
47 }
48 
49 using IDMap = std::map<const char*, uint64_t>;
50 
51 } /* namespace datasource */
52 } /* namespace fuzzing */
53