1 /* Copyright 2019 Guido Vranken
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject
9 * to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #pragma once
25
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <utility>
29 #include <map>
30
31 namespace fuzzing {
32 namespace datasource {
33
34 /* From: https://gist.github.com/underscorediscovery/81308642d0325fd386237cfa3b44785c */
hash_64_fnv1a(const void * key,const uint64_t len)35 inline uint64_t hash_64_fnv1a(const void* key, const uint64_t len) {
36
37 const char* data = (char*)key;
38 uint64_t hash = 0xcbf29ce484222325;
39 uint64_t prime = 0x100000001b3;
40
41 for(uint64_t i = 0; i < len; ++i) {
42 uint8_t value = data[i];
43 hash = hash ^ value;
44 hash *= prime;
45 }
46
47 return hash;
48
49 } //hash_64_fnv1a
50
51 // FNV1a c++11 constexpr compile time hash functions, 32 and 64 bit
52 // str should be a null terminated string literal, value should be left out
53 // e.g hash_32_fnv1a_const("example")
54 // code license: public domain or equivalent
55 // post: https://notes.underscorediscovery.com/constexpr-fnv1a/
56
57 constexpr uint32_t val_32_const = 0x811c9dc5;
58 constexpr uint32_t prime_32_const = 0x1000193;
59 constexpr uint64_t val_64_const = 0xcbf29ce484222325;
60 constexpr uint64_t prime_64_const = 0x100000001b3;
61
62
ID(const char * const str,const uint64_t value=val_64_const)63 inline constexpr uint64_t ID(const char* const str, const uint64_t value = val_64_const) noexcept {
64 auto ret = (str[0] == '\0') ? value : ID(&str[1], (value ^ uint64_t(str[0])) * prime_64_const);
65 return ret;
66 }
67
IDPair(const char * const str,const uint64_t value=val_64_const)68 inline constexpr std::pair<const char*, uint64_t> IDPair(const char* const str, const uint64_t value = val_64_const) noexcept {
69 return {str, ID(str, value)};
70 }
71
72 using IDMap = std::map<const char*, uint64_t>;
73
74 } /* namespace datasource */
75 } /* namespace fuzzing */
76