Coverage Report

Created: 2023-03-26 07:17

/src/pdns/pdns/burtle.hh
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
 */
22
23
#pragma once
24
25
#include <cinttypes>
26
27
inline void burtlemix(uint32_t& a, uint32_t& b, uint32_t& c)
28
755k
{
29
755k
  a -= b;
30
755k
  a -= c;
31
755k
  a ^= (c >> 13);
32
755k
  b -= c;
33
755k
  b -= a;
34
755k
  b ^= (a << 8);
35
755k
  c -= a;
36
755k
  c -= b;
37
755k
  c ^= (b >> 13);
38
755k
  a -= b;
39
755k
  a -= c;
40
755k
  a ^= (c >> 12);
41
755k
  b -= c;
42
755k
  b -= a;
43
755k
  b ^= (a << 16);
44
755k
  c -= a;
45
755k
  c -= b;
46
755k
  c ^= (b >> 5);
47
755k
  a -= b;
48
755k
  a -= c;
49
755k
  a ^= (c >> 3);
50
755k
  b -= c;
51
755k
  b -= a;
52
755k
  b ^= (a << 10);
53
755k
  c -= a;
54
755k
  c -= b;
55
755k
  c ^= (b >> 15);
56
755k
}
57
58
inline uint32_t burtle(const unsigned char* k, uint32_t length, uint32_t initval)
59
27.5k
{
60
27.5k
  uint32_t a, b, c, len;
61
62
  /* Set up the internal state */
63
27.5k
  len = length;
64
27.5k
  a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
65
27.5k
  c = initval; /* the previous hash value */
66
67
  /*---------------------------------------- handle most of the key */
68
412k
  while (len >= 12) {
69
384k
    a += (k[0] + ((uint32_t)k[1] << 8) + ((uint32_t)k[2] << 16) + ((uint32_t)k[3] << 24));
70
384k
    b += (k[4] + ((uint32_t)k[5] << 8) + ((uint32_t)k[6] << 16) + ((uint32_t)k[7] << 24));
71
384k
    c += (k[8] + ((uint32_t)k[9] << 8) + ((uint32_t)k[10] << 16) + ((uint32_t)k[11] << 24));
72
384k
    burtlemix(a, b, c);
73
384k
    k += 12;
74
384k
    len -= 12;
75
384k
  }
76
77
  /*------------------------------------- handle the last 11 bytes */
78
27.5k
  c += length;
79
27.5k
  switch (len) { /* all the case statements fall through */
80
465
  case 11:
81
465
    c += ((uint32_t)k[10] << 24);
82
    /* fall-through */
83
2.97k
  case 10:
84
2.97k
    c += ((uint32_t)k[9] << 16);
85
    /* fall-through */
86
5.60k
  case 9:
87
5.60k
    c += ((uint32_t)k[8] << 8);
88
    /* the first byte of c is reserved for the length */
89
    /* fall-through */
90
6.75k
  case 8:
91
6.75k
    b += ((uint32_t)k[7] << 24);
92
    /* fall-through */
93
7.07k
  case 7:
94
7.07k
    b += ((uint32_t)k[6] << 16);
95
    /* fall-through */
96
7.54k
  case 6:
97
7.54k
    b += ((uint32_t)k[5] << 8);
98
    /* fall-through */
99
8.86k
  case 5:
100
8.86k
    b += k[4];
101
    /* fall-through */
102
22.1k
  case 4:
103
22.1k
    a += ((uint32_t)k[3] << 24);
104
    /* fall-through */
105
24.7k
  case 3:
106
24.7k
    a += ((uint32_t)k[2] << 16);
107
    /* fall-through */
108
25.7k
  case 2:
109
25.7k
    a += ((uint32_t)k[1] << 8);
110
    /* fall-through */
111
26.3k
  case 1:
112
26.3k
    a += k[0];
113
    /* case 0: nothing left to add */
114
27.5k
  }
115
27.5k
  burtlemix(a, b, c);
116
  /*-------------------------------------------- report the result */
117
27.5k
  return c;
118
27.5k
}
119
120
inline uint32_t burtleCI(const unsigned char* k, uint32_t length, uint32_t initval)
121
1.79k
{
122
1.79k
  uint32_t a, b, c, len;
123
124
  /* Set up the internal state */
125
1.79k
  len = length;
126
1.79k
  a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
127
1.79k
  c = initval; /* the previous hash value */
128
129
  /*---------------------------------------- handle most of the key */
130
343k
  while (len >= 12) {
131
341k
    a += (dns_tolower(k[0]) + ((uint32_t)dns_tolower(k[1]) << 8) + ((uint32_t)dns_tolower(k[2]) << 16) + ((uint32_t)dns_tolower(k[3]) << 24));
132
341k
    b += (dns_tolower(k[4]) + ((uint32_t)dns_tolower(k[5]) << 8) + ((uint32_t)dns_tolower(k[6]) << 16) + ((uint32_t)dns_tolower(k[7]) << 24));
133
341k
    c += (dns_tolower(k[8]) + ((uint32_t)dns_tolower(k[9]) << 8) + ((uint32_t)dns_tolower(k[10]) << 16) + ((uint32_t)dns_tolower(k[11]) << 24));
134
341k
    burtlemix(a, b, c);
135
341k
    k += 12;
136
341k
    len -= 12;
137
341k
  }
138
139
  /*------------------------------------- handle the last 11 bytes */
140
1.79k
  c += length;
141
1.79k
  switch (len) { /* all the case statements fall through */
142
60
  case 11:
143
60
    c += ((uint32_t)dns_tolower(k[10]) << 24);
144
    /* fall-through */
145
140
  case 10:
146
140
    c += ((uint32_t)dns_tolower(k[9]) << 16);
147
    /* fall-through */
148
182
  case 9:
149
182
    c += ((uint32_t)dns_tolower(k[8]) << 8);
150
    /* the first byte of c is reserved for the length */
151
    /* fall-through */
152
264
  case 8:
153
264
    b += ((uint32_t)dns_tolower(k[7]) << 24);
154
    /* fall-through */
155
342
  case 7:
156
342
    b += ((uint32_t)dns_tolower(k[6]) << 16);
157
    /* fall-through */
158
424
  case 6:
159
424
    b += ((uint32_t)dns_tolower(k[5]) << 8);
160
    /* fall-through */
161
502
  case 5:
162
502
    b += dns_tolower(k[4]);
163
    /* fall-through */
164
598
  case 4:
165
598
    a += ((uint32_t)dns_tolower(k[3]) << 24);
166
    /* fall-through */
167
678
  case 3:
168
678
    a += ((uint32_t)dns_tolower(k[2]) << 16);
169
    /* fall-through */
170
760
  case 2:
171
760
    a += ((uint32_t)dns_tolower(k[1]) << 8);
172
    /* fall-through */
173
1.70k
  case 1:
174
1.70k
    a += dns_tolower(k[0]);
175
    /* case 0: nothing left to add */
176
1.79k
  }
177
1.79k
  burtlemix(a, b, c);
178
  /*-------------------------------------------- report the result */
179
1.79k
  return c;
180
1.79k
}