s3: Fix bug #9085.
[Samba.git] / source3 / lib / jenkins_hash.c
blob09cc1fdb01e190907d0eed8ebd70423365049d2e
1 /*
2 Unix SMB/CIFS implementation.
4 trivial database library
6 Copyright (C) Rusty Russell 2010
8 ** NOTE! The following LGPL license applies to the tdb
9 ** library. This does NOT imply that all of Samba is released
10 ** under the LGPL
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
27 /* This is based on the hash algorithm from gdbm */
30 #ifndef WORDS_BIGENDIAN
31 # define HASH_LITTLE_ENDIAN 1
32 # define HASH_BIG_ENDIAN 0
33 #else
34 # define HASH_LITTLE_ENDIAN 0
35 # define HASH_BIG_ENDIAN 1
36 #endif
39 -------------------------------------------------------------------------------
40 lookup3.c, by Bob Jenkins, May 2006, Public Domain.
42 These are functions for producing 32-bit hashes for hash table lookup.
43 hash_word(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
44 are externally useful functions. Routines to test the hash are included
45 if SELF_TEST is defined. You can use this free for any purpose. It's in
46 the public domain. It has no warranty.
48 You probably want to use hashlittle(). hashlittle() and hashbig()
49 hash byte arrays. hashlittle() is is faster than hashbig() on
50 little-endian machines. Intel and AMD are little-endian machines.
51 On second thought, you probably want hashlittle2(), which is identical to
52 hashlittle() except it returns two 32-bit hashes for the price of one.
53 You could implement hashbig2() if you wanted but I haven't bothered here.
55 If you want to find a hash of, say, exactly 7 integers, do
56 a = i1; b = i2; c = i3;
57 mix(a,b,c);
58 a += i4; b += i5; c += i6;
59 mix(a,b,c);
60 a += i7;
61 final(a,b,c);
62 then use c as the hash value. If you have a variable length array of
63 4-byte integers to hash, use hash_word(). If you have a byte array (like
64 a character string), use hashlittle(). If you have several byte arrays, or
65 a mix of things, see the comments above hashlittle().
67 Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
68 then mix those integers. This is fast (you can do a lot more thorough
69 mixing with 12*3 instructions on 3 integers than you can with 3 instructions
70 on 1 byte), but shoehorning those bytes into integers efficiently is messy.
73 #define hashsize(n) ((uint32_t)1<<(n))
74 #define hashmask(n) (hashsize(n)-1)
75 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
78 -------------------------------------------------------------------------------
79 mix -- mix 3 32-bit values reversibly.
81 This is reversible, so any information in (a,b,c) before mix() is
82 still in (a,b,c) after mix().
84 If four pairs of (a,b,c) inputs are run through mix(), or through
85 mix() in reverse, there are at least 32 bits of the output that
86 are sometimes the same for one pair and different for another pair.
87 This was tested for:
88 * pairs that differed by one bit, by two bits, in any combination
89 of top bits of (a,b,c), or in any combination of bottom bits of
90 (a,b,c).
91 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
92 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
93 is commonly produced by subtraction) look like a single 1-bit
94 difference.
95 * the base values were pseudorandom, all zero but one bit set, or
96 all zero plus a counter that starts at zero.
98 Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
99 satisfy this are
100 4 6 8 16 19 4
101 9 15 3 18 27 15
102 14 9 3 7 17 3
103 Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
104 for "differ" defined as + with a one-bit base and a two-bit delta. I
105 used http://burtleburtle.net/bob/hash/avalanche.html to choose
106 the operations, constants, and arrangements of the variables.
108 This does not achieve avalanche. There are input bits of (a,b,c)
109 that fail to affect some output bits of (a,b,c), especially of a. The
110 most thoroughly mixed value is c, but it doesn't really even achieve
111 avalanche in c.
113 This allows some parallelism. Read-after-writes are good at doubling
114 the number of bits affected, so the goal of mixing pulls in the opposite
115 direction as the goal of parallelism. I did what I could. Rotates
116 seem to cost as much as shifts on every machine I could lay my hands
117 on, and rotates are much kinder to the top and bottom bits, so I used
118 rotates.
119 -------------------------------------------------------------------------------
121 #define mix(a,b,c) \
123 a -= c; a ^= rot(c, 4); c += b; \
124 b -= a; b ^= rot(a, 6); a += c; \
125 c -= b; c ^= rot(b, 8); b += a; \
126 a -= c; a ^= rot(c,16); c += b; \
127 b -= a; b ^= rot(a,19); a += c; \
128 c -= b; c ^= rot(b, 4); b += a; \
132 -------------------------------------------------------------------------------
133 final -- final mixing of 3 32-bit values (a,b,c) into c
135 Pairs of (a,b,c) values differing in only a few bits will usually
136 produce values of c that look totally different. This was tested for
137 * pairs that differed by one bit, by two bits, in any combination
138 of top bits of (a,b,c), or in any combination of bottom bits of
139 (a,b,c).
140 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
141 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
142 is commonly produced by subtraction) look like a single 1-bit
143 difference.
144 * the base values were pseudorandom, all zero but one bit set, or
145 all zero plus a counter that starts at zero.
147 These constants passed:
148 14 11 25 16 4 14 24
149 12 14 25 16 4 14 24
150 and these came close:
151 4 8 15 26 3 22 24
152 10 8 15 26 3 22 24
153 11 8 15 26 3 22 24
154 -------------------------------------------------------------------------------
156 #define final(a,b,c) \
158 c ^= b; c -= rot(b,14); \
159 a ^= c; a -= rot(c,11); \
160 b ^= a; b -= rot(a,25); \
161 c ^= b; c -= rot(b,16); \
162 a ^= c; a -= rot(c,4); \
163 b ^= a; b -= rot(a,14); \
164 c ^= b; c -= rot(b,24); \
169 -------------------------------------------------------------------------------
170 hashlittle() -- hash a variable-length key into a 32-bit value
171 k : the key (the unaligned variable-length array of bytes)
172 length : the length of the key, counting by bytes
173 val2 : IN: can be any 4-byte value OUT: second 32 bit hash.
174 Returns a 32-bit value. Every bit of the key affects every bit of
175 the return value. Two keys differing by one or two bits will have
176 totally different hash values. Note that the return value is better
177 mixed than val2, so use that first.
179 The best hash table sizes are powers of 2. There is no need to do
180 mod a prime (mod is sooo slow!). If you need less than 32 bits,
181 use a bitmask. For example, if you need only 10 bits, do
182 h = (h & hashmask(10));
183 In which case, the hash table should have hashsize(10) elements.
185 If you are hashing n strings (uint8_t **)k, do it like this:
186 for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
188 By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
189 code any way you wish, private, educational, or commercial. It's free.
191 Use for hash table lookup, or anything where one collision in 2^^32 is
192 acceptable. Do NOT use for cryptographic purposes.
193 -------------------------------------------------------------------------------
196 static uint32_t hashlittle( const void *key, size_t length )
198 uint32_t a,b,c; /* internal state */
199 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
201 /* Set up the internal state */
202 a = b = c = 0xdeadbeef + ((uint32_t)length);
204 u.ptr = key;
205 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
206 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
207 #ifdef VALGRIND
208 const uint8_t *k8;
209 #endif
211 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
212 while (length > 12)
214 a += k[0];
215 b += k[1];
216 c += k[2];
217 mix(a,b,c);
218 length -= 12;
219 k += 3;
222 /*----------------------------- handle the last (probably partial) block */
224 * "k[2]&0xffffff" actually reads beyond the end of the string, but
225 * then masks off the part it's not allowed to read. Because the
226 * string is aligned, the masked-off tail is in the same word as the
227 * rest of the string. Every machine with memory protection I've seen
228 * does it on word boundaries, so is OK with this. But VALGRIND will
229 * still catch it and complain. The masking trick does make the hash
230 * noticably faster for short strings (like English words).
232 #ifndef VALGRIND
234 switch(length)
236 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
237 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
238 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
239 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
240 case 8 : b+=k[1]; a+=k[0]; break;
241 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
242 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
243 case 5 : b+=k[1]&0xff; a+=k[0]; break;
244 case 4 : a+=k[0]; break;
245 case 3 : a+=k[0]&0xffffff; break;
246 case 2 : a+=k[0]&0xffff; break;
247 case 1 : a+=k[0]&0xff; break;
248 case 0 : return c; /* zero length strings require no mixing */
251 #else /* make valgrind happy */
253 k8 = (const uint8_t *)k;
254 switch(length)
256 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
257 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
258 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
259 case 9 : c+=k8[8]; /* fall through */
260 case 8 : b+=k[1]; a+=k[0]; break;
261 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
262 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
263 case 5 : b+=k8[4]; /* fall through */
264 case 4 : a+=k[0]; break;
265 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
266 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
267 case 1 : a+=k8[0]; break;
268 case 0 : return c;
271 #endif /* !valgrind */
273 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
274 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
275 const uint8_t *k8;
277 /*--------------- all but last block: aligned reads and different mixing */
278 while (length > 12)
280 a += k[0] + (((uint32_t)k[1])<<16);
281 b += k[2] + (((uint32_t)k[3])<<16);
282 c += k[4] + (((uint32_t)k[5])<<16);
283 mix(a,b,c);
284 length -= 12;
285 k += 6;
288 /*----------------------------- handle the last (probably partial) block */
289 k8 = (const uint8_t *)k;
290 switch(length)
292 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
293 b+=k[2]+(((uint32_t)k[3])<<16);
294 a+=k[0]+(((uint32_t)k[1])<<16);
295 break;
296 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
297 case 10: c+=k[4];
298 b+=k[2]+(((uint32_t)k[3])<<16);
299 a+=k[0]+(((uint32_t)k[1])<<16);
300 break;
301 case 9 : c+=k8[8]; /* fall through */
302 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
303 a+=k[0]+(((uint32_t)k[1])<<16);
304 break;
305 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
306 case 6 : b+=k[2];
307 a+=k[0]+(((uint32_t)k[1])<<16);
308 break;
309 case 5 : b+=k8[4]; /* fall through */
310 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
311 break;
312 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
313 case 2 : a+=k[0];
314 break;
315 case 1 : a+=k8[0];
316 break;
317 case 0 : return c; /* zero length requires no mixing */
320 } else { /* need to read the key one byte at a time */
321 const uint8_t *k = (const uint8_t *)key;
323 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
324 while (length > 12)
326 a += k[0];
327 a += ((uint32_t)k[1])<<8;
328 a += ((uint32_t)k[2])<<16;
329 a += ((uint32_t)k[3])<<24;
330 b += k[4];
331 b += ((uint32_t)k[5])<<8;
332 b += ((uint32_t)k[6])<<16;
333 b += ((uint32_t)k[7])<<24;
334 c += k[8];
335 c += ((uint32_t)k[9])<<8;
336 c += ((uint32_t)k[10])<<16;
337 c += ((uint32_t)k[11])<<24;
338 mix(a,b,c);
339 length -= 12;
340 k += 12;
343 /*-------------------------------- last block: affect all 32 bits of (c) */
344 switch(length) /* all the case statements fall through */
346 case 12: c+=((uint32_t)k[11])<<24;
347 case 11: c+=((uint32_t)k[10])<<16;
348 case 10: c+=((uint32_t)k[9])<<8;
349 case 9 : c+=k[8];
350 case 8 : b+=((uint32_t)k[7])<<24;
351 case 7 : b+=((uint32_t)k[6])<<16;
352 case 6 : b+=((uint32_t)k[5])<<8;
353 case 5 : b+=k[4];
354 case 4 : a+=((uint32_t)k[3])<<24;
355 case 3 : a+=((uint32_t)k[2])<<16;
356 case 2 : a+=((uint32_t)k[1])<<8;
357 case 1 : a+=k[0];
358 break;
359 case 0 : return c;
363 final(a,b,c);
364 return c;
367 unsigned int jenkins_hash(TDB_DATA *key)
369 return hashlittle(key->dptr, key->dsize);