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