2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
27 inline void burtlemix(uint32_t& a, uint32_t& b, uint32_t& c)
58 inline uint32_t burtle(const unsigned char* k, uint32_t length, uint32_t initval)
60 uint32_t a, b, c, len;
62 /* Set up the internal state */
64 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
65 c = initval; /* the previous hash value */
67 /*---------------------------------------- handle most of the key */
69 a += (k[0] + ((uint32_t)k[1] << 8) + ((uint32_t)k[2] << 16) + ((uint32_t)k[3] << 24));
70 b += (k[4] + ((uint32_t)k[5] << 8) + ((uint32_t)k[6] << 16) + ((uint32_t)k[7] << 24));
71 c += (k[8] + ((uint32_t)k[9] << 8) + ((uint32_t)k[10] << 16) + ((uint32_t)k[11] << 24));
77 /*------------------------------------- handle the last 11 bytes */
79 switch (len) { /* all the case statements fall through */
81 c += ((uint32_t)k[10] << 24);
84 c += ((uint32_t)k[9] << 16);
87 c += ((uint32_t)k[8] << 8);
88 /* the first byte of c is reserved for the length */
91 b += ((uint32_t)k[7] << 24);
94 b += ((uint32_t)k[6] << 16);
97 b += ((uint32_t)k[5] << 8);
103 a += ((uint32_t)k[3] << 24);
106 a += ((uint32_t)k[2] << 16);
109 a += ((uint32_t)k[1] << 8);
113 /* case 0: nothing left to add */
116 /*-------------------------------------------- report the result */
120 inline uint32_t burtleCI(const unsigned char* k, uint32_t length, uint32_t initval)
122 uint32_t a, b, c, len;
124 /* Set up the internal state */
126 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
127 c = initval; /* the previous hash value */
129 /*---------------------------------------- handle most of the key */
131 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 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 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));
139 /*------------------------------------- handle the last 11 bytes */
141 switch (len) { /* all the case statements fall through */
143 c += ((uint32_t)dns_tolower(k[10]) << 24);
146 c += ((uint32_t)dns_tolower(k[9]) << 16);
149 c += ((uint32_t)dns_tolower(k[8]) << 8);
150 /* the first byte of c is reserved for the length */
153 b += ((uint32_t)dns_tolower(k[7]) << 24);
156 b += ((uint32_t)dns_tolower(k[6]) << 16);
159 b += ((uint32_t)dns_tolower(k[5]) << 8);
162 b += dns_tolower(k[4]);
165 a += ((uint32_t)dns_tolower(k[3]) << 24);
168 a += ((uint32_t)dns_tolower(k[2]) << 16);
171 a += ((uint32_t)dns_tolower(k[1]) << 8);
174 a += dns_tolower(k[0]);
175 /* case 0: nothing left to add */
178 /*-------------------------------------------- report the result */