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