id.hpp 2.6 KB

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