Coverage Report

Created: 2026-02-26 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/flatbuffers/include/flatbuffers/hash.h
Line
Count
Source
1
/*
2
 * Copyright 2015 Google Inc. All rights reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#ifndef FLATBUFFERS_HASH_H_
18
#define FLATBUFFERS_HASH_H_
19
20
#include <cstdint>
21
#include <cstring>
22
23
#include "flatbuffers/flatbuffers.h"
24
25
namespace flatbuffers {
26
27
template <typename T>
28
struct FnvTraits {
29
  static const T kFnvPrime;
30
  static const T kOffsetBasis;
31
};
32
33
template <>
34
struct FnvTraits<uint32_t> {
35
  static const uint32_t kFnvPrime = 0x01000193;
36
  static const uint32_t kOffsetBasis = 0x811C9DC5;
37
};
38
39
template <>
40
struct FnvTraits<uint64_t> {
41
  static const uint64_t kFnvPrime = 0x00000100000001b3ULL;
42
  static const uint64_t kOffsetBasis = 0xcbf29ce484222645ULL;
43
};
44
45
template <typename T>
46
0
T HashFnv1(const char* input) {
47
0
  T hash = FnvTraits<T>::kOffsetBasis;
48
0
  for (const char* c = input; *c; ++c) {
49
0
    hash *= FnvTraits<T>::kFnvPrime;
50
0
    hash ^= static_cast<unsigned char>(*c);
51
0
  }
52
0
  return hash;
53
0
}
Unexecuted instantiation: unsigned int flatbuffers::HashFnv1<unsigned int>(char const*)
Unexecuted instantiation: unsigned long flatbuffers::HashFnv1<unsigned long>(char const*)
54
55
template <typename T>
56
0
T HashFnv1a(const char* input) {
57
0
  T hash = FnvTraits<T>::kOffsetBasis;
58
0
  for (const char* c = input; *c; ++c) {
59
0
    hash ^= static_cast<unsigned char>(*c);
60
0
    hash *= FnvTraits<T>::kFnvPrime;
61
0
  }
62
0
  return hash;
63
0
}
Unexecuted instantiation: unsigned int flatbuffers::HashFnv1a<unsigned int>(char const*)
Unexecuted instantiation: unsigned long flatbuffers::HashFnv1a<unsigned long>(char const*)
64
65
template <>
66
0
inline uint16_t HashFnv1<uint16_t>(const char* input) {
67
0
  uint32_t hash = HashFnv1<uint32_t>(input);
68
0
  return (hash >> 16) ^ (hash & 0xffff);
69
0
}
70
71
template <>
72
0
inline uint16_t HashFnv1a<uint16_t>(const char* input) {
73
0
  uint32_t hash = HashFnv1a<uint32_t>(input);
74
0
  return (hash >> 16) ^ (hash & 0xffff);
75
0
}
76
77
template <typename T>
78
struct NamedHashFunction {
79
  const char* name;
80
81
  typedef T (*HashFunction)(const char*);
82
  HashFunction function;
83
};
84
85
const NamedHashFunction<uint16_t> kHashFunctions16[] = {
86
    {"fnv1_16", HashFnv1<uint16_t>},
87
    {"fnv1a_16", HashFnv1a<uint16_t>},
88
};
89
90
const NamedHashFunction<uint32_t> kHashFunctions32[] = {
91
    {"fnv1_32", HashFnv1<uint32_t>},
92
    {"fnv1a_32", HashFnv1a<uint32_t>},
93
};
94
95
const NamedHashFunction<uint64_t> kHashFunctions64[] = {
96
    {"fnv1_64", HashFnv1<uint64_t>},
97
    {"fnv1a_64", HashFnv1a<uint64_t>},
98
};
99
100
inline NamedHashFunction<uint16_t>::HashFunction FindHashFunction16(
101
0
    const char* name) {
102
0
  std::size_t size = sizeof(kHashFunctions16) / sizeof(kHashFunctions16[0]);
103
0
  for (std::size_t i = 0; i < size; ++i) {
104
0
    if (std::strcmp(name, kHashFunctions16[i].name) == 0) {
105
0
      return kHashFunctions16[i].function;
106
0
    }
107
0
  }
108
0
  return nullptr;
109
0
}
110
111
inline NamedHashFunction<uint32_t>::HashFunction FindHashFunction32(
112
0
    const char* name) {
113
0
  std::size_t size = sizeof(kHashFunctions32) / sizeof(kHashFunctions32[0]);
114
0
  for (std::size_t i = 0; i < size; ++i) {
115
0
    if (std::strcmp(name, kHashFunctions32[i].name) == 0) {
116
0
      return kHashFunctions32[i].function;
117
0
    }
118
0
  }
119
0
  return nullptr;
120
0
}
121
122
inline NamedHashFunction<uint64_t>::HashFunction FindHashFunction64(
123
0
    const char* name) {
124
0
  std::size_t size = sizeof(kHashFunctions64) / sizeof(kHashFunctions64[0]);
125
0
  for (std::size_t i = 0; i < size; ++i) {
126
0
    if (std::strcmp(name, kHashFunctions64[i].name) == 0) {
127
0
      return kHashFunctions64[i].function;
128
0
    }
129
0
  }
130
0
  return nullptr;
131
0
}
132
133
}  // namespace flatbuffers
134
135
#endif  // FLATBUFFERS_HASH_H_