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 | 0 | { |
29 | 0 | a -= b; |
30 | 0 | a -= c; |
31 | 0 | a ^= (c >> 13); |
32 | 0 | b -= c; |
33 | 0 | b -= a; |
34 | 0 | b ^= (a << 8); |
35 | 0 | c -= a; |
36 | 0 | c -= b; |
37 | 0 | c ^= (b >> 13); |
38 | 0 | a -= b; |
39 | 0 | a -= c; |
40 | 0 | a ^= (c >> 12); |
41 | 0 | b -= c; |
42 | 0 | b -= a; |
43 | 0 | b ^= (a << 16); |
44 | 0 | c -= a; |
45 | 0 | c -= b; |
46 | 0 | c ^= (b >> 5); |
47 | 0 | a -= b; |
48 | 0 | a -= c; |
49 | 0 | a ^= (c >> 3); |
50 | 0 | b -= c; |
51 | 0 | b -= a; |
52 | 0 | b ^= (a << 10); |
53 | 0 | c -= a; |
54 | 0 | c -= b; |
55 | 0 | c ^= (b >> 15); |
56 | 0 | } |
57 | | |
58 | | inline uint32_t burtle(const unsigned char* k, uint32_t length, uint32_t initval) |
59 | 0 | { |
60 | 0 | uint32_t a, b, c, len; |
61 | 0 |
|
62 | 0 | /* Set up the internal state */ |
63 | 0 | len = length; |
64 | 0 | a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ |
65 | 0 | c = initval; /* the previous hash value */ |
66 | 0 |
|
67 | 0 | /*---------------------------------------- handle most of the key */ |
68 | 0 | while (len >= 12) { |
69 | 0 | a += (k[0] + ((uint32_t)k[1] << 8) + ((uint32_t)k[2] << 16) + ((uint32_t)k[3] << 24)); |
70 | 0 | b += (k[4] + ((uint32_t)k[5] << 8) + ((uint32_t)k[6] << 16) + ((uint32_t)k[7] << 24)); |
71 | 0 | c += (k[8] + ((uint32_t)k[9] << 8) + ((uint32_t)k[10] << 16) + ((uint32_t)k[11] << 24)); |
72 | 0 | burtlemix(a, b, c); |
73 | 0 | k += 12; |
74 | 0 | len -= 12; |
75 | 0 | } |
76 | 0 |
|
77 | 0 | /*------------------------------------- handle the last 11 bytes */ |
78 | 0 | c += length; |
79 | 0 | switch (len) { /* all the case statements fall through */ |
80 | 0 | case 11: |
81 | 0 | c += ((uint32_t)k[10] << 24); |
82 | 0 | /* fall-through */ |
83 | 0 | case 10: |
84 | 0 | c += ((uint32_t)k[9] << 16); |
85 | 0 | /* fall-through */ |
86 | 0 | case 9: |
87 | 0 | c += ((uint32_t)k[8] << 8); |
88 | 0 | /* the first byte of c is reserved for the length */ |
89 | 0 | /* fall-through */ |
90 | 0 | case 8: |
91 | 0 | b += ((uint32_t)k[7] << 24); |
92 | 0 | /* fall-through */ |
93 | 0 | case 7: |
94 | 0 | b += ((uint32_t)k[6] << 16); |
95 | 0 | /* fall-through */ |
96 | 0 | case 6: |
97 | 0 | b += ((uint32_t)k[5] << 8); |
98 | 0 | /* fall-through */ |
99 | 0 | case 5: |
100 | 0 | b += k[4]; |
101 | 0 | /* fall-through */ |
102 | 0 | case 4: |
103 | 0 | a += ((uint32_t)k[3] << 24); |
104 | 0 | /* fall-through */ |
105 | 0 | case 3: |
106 | 0 | a += ((uint32_t)k[2] << 16); |
107 | 0 | /* fall-through */ |
108 | 0 | case 2: |
109 | 0 | a += ((uint32_t)k[1] << 8); |
110 | 0 | /* fall-through */ |
111 | 0 | case 1: |
112 | 0 | a += k[0]; |
113 | 0 | /* case 0: nothing left to add */ |
114 | 0 | } |
115 | 0 | burtlemix(a, b, c); |
116 | 0 | /*-------------------------------------------- report the result */ |
117 | 0 | return c; |
118 | 0 | } |
119 | | |
120 | | inline uint32_t burtleCI(const unsigned char* k, uint32_t length, uint32_t initval) |
121 | 0 | { |
122 | 0 | uint32_t a, b, c, len; |
123 | 0 |
|
124 | 0 | /* Set up the internal state */ |
125 | 0 | len = length; |
126 | 0 | a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ |
127 | 0 | c = initval; /* the previous hash value */ |
128 | 0 |
|
129 | 0 | /*---------------------------------------- handle most of the key */ |
130 | 0 | while (len >= 12) { |
131 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | burtlemix(a, b, c); |
135 | 0 | k += 12; |
136 | 0 | len -= 12; |
137 | 0 | } |
138 | 0 |
|
139 | 0 | /*------------------------------------- handle the last 11 bytes */ |
140 | 0 | c += length; |
141 | 0 | switch (len) { /* all the case statements fall through */ |
142 | 0 | case 11: |
143 | 0 | c += ((uint32_t)dns_tolower(k[10]) << 24); |
144 | 0 | /* fall-through */ |
145 | 0 | case 10: |
146 | 0 | c += ((uint32_t)dns_tolower(k[9]) << 16); |
147 | 0 | /* fall-through */ |
148 | 0 | case 9: |
149 | 0 | c += ((uint32_t)dns_tolower(k[8]) << 8); |
150 | 0 | /* the first byte of c is reserved for the length */ |
151 | 0 | /* fall-through */ |
152 | 0 | case 8: |
153 | 0 | b += ((uint32_t)dns_tolower(k[7]) << 24); |
154 | 0 | /* fall-through */ |
155 | 0 | case 7: |
156 | 0 | b += ((uint32_t)dns_tolower(k[6]) << 16); |
157 | 0 | /* fall-through */ |
158 | 0 | case 6: |
159 | 0 | b += ((uint32_t)dns_tolower(k[5]) << 8); |
160 | 0 | /* fall-through */ |
161 | 0 | case 5: |
162 | 0 | b += dns_tolower(k[4]); |
163 | 0 | /* fall-through */ |
164 | 0 | case 4: |
165 | 0 | a += ((uint32_t)dns_tolower(k[3]) << 24); |
166 | 0 | /* fall-through */ |
167 | 0 | case 3: |
168 | 0 | a += ((uint32_t)dns_tolower(k[2]) << 16); |
169 | 0 | /* fall-through */ |
170 | 0 | case 2: |
171 | 0 | a += ((uint32_t)dns_tolower(k[1]) << 8); |
172 | 0 | /* fall-through */ |
173 | 0 | case 1: |
174 | 0 | a += dns_tolower(k[0]); |
175 | 0 | /* case 0: nothing left to add */ |
176 | 0 | } |
177 | 0 | burtlemix(a, b, c); |
178 | 0 | /*-------------------------------------------- report the result */ |
179 | 0 | return c; |
180 | 0 | } |