/src/samba/lib/tdb/common/hash.c
Line | Count | Source |
1 | | /* |
2 | | Unix SMB/CIFS implementation. |
3 | | |
4 | | trivial database library |
5 | | |
6 | | Copyright (C) Rusty Russell 2010 |
7 | | |
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 |
11 | | |
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. |
16 | | |
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. |
21 | | |
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/>. |
24 | | */ |
25 | | #include "tdb_private.h" |
26 | | |
27 | | /* This is based on the hash algorithm from gdbm */ |
28 | | unsigned int tdb_old_hash(TDB_DATA *key) |
29 | 0 | { |
30 | 0 | uint32_t value; /* Used to compute the hash value. */ |
31 | 0 | uint32_t i; /* Used to cycle through random values. */ |
32 | | |
33 | | /* Set the initial value from the key size. */ |
34 | 0 | for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++) |
35 | 0 | value = (value + (key->dptr[i] << (i*5 % 24))); |
36 | |
|
37 | 0 | return (1103515243 * value + 12345); |
38 | 0 | } |
39 | | |
40 | | #ifndef WORDS_BIGENDIAN |
41 | 0 | # define HASH_LITTLE_ENDIAN 1 |
42 | | # define HASH_BIG_ENDIAN 0 |
43 | | #else |
44 | | # define HASH_LITTLE_ENDIAN 0 |
45 | | # define HASH_BIG_ENDIAN 1 |
46 | | #endif |
47 | | |
48 | | /* |
49 | | ------------------------------------------------------------------------------- |
50 | | lookup3.c, by Bob Jenkins, May 2006, Public Domain. |
51 | | |
52 | | These are functions for producing 32-bit hashes for hash table lookup. |
53 | | hash_word(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() |
54 | | are externally useful functions. Routines to test the hash are included |
55 | | if SELF_TEST is defined. You can use this free for any purpose. It's in |
56 | | the public domain. It has no warranty. |
57 | | |
58 | | You probably want to use hashlittle(). hashlittle() and hashbig() |
59 | | hash byte arrays. hashlittle() is faster than hashbig() on |
60 | | little-endian machines. Intel and AMD are little-endian machines. |
61 | | On second thought, you probably want hashlittle2(), which is identical to |
62 | | hashlittle() except it returns two 32-bit hashes for the price of one. |
63 | | You could implement hashbig2() if you wanted but I haven't bothered here. |
64 | | |
65 | | If you want to find a hash of, say, exactly 7 integers, do |
66 | | a = i1; b = i2; c = i3; |
67 | | mix(a,b,c); |
68 | | a += i4; b += i5; c += i6; |
69 | | mix(a,b,c); |
70 | | a += i7; |
71 | | final(a,b,c); |
72 | | then use c as the hash value. If you have a variable length array of |
73 | | 4-byte integers to hash, use hash_word(). If you have a byte array (like |
74 | | a character string), use hashlittle(). If you have several byte arrays, or |
75 | | a mix of things, see the comments above hashlittle(). |
76 | | |
77 | | Why is this so big? I read 12 bytes at a time into 3 4-byte integers, |
78 | | then mix those integers. This is fast (you can do a lot more thorough |
79 | | mixing with 12*3 instructions on 3 integers than you can with 3 instructions |
80 | | on 1 byte), but shoehorning those bytes into integers efficiently is messy. |
81 | | */ |
82 | | |
83 | | #define hashsize(n) ((uint32_t)1<<(n)) |
84 | | #define hashmask(n) (hashsize(n)-1) |
85 | 0 | #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) |
86 | | |
87 | | /* |
88 | | ------------------------------------------------------------------------------- |
89 | | mix -- mix 3 32-bit values reversibly. |
90 | | |
91 | | This is reversible, so any information in (a,b,c) before mix() is |
92 | | still in (a,b,c) after mix(). |
93 | | |
94 | | If four pairs of (a,b,c) inputs are run through mix(), or through |
95 | | mix() in reverse, there are at least 32 bits of the output that |
96 | | are sometimes the same for one pair and different for another pair. |
97 | | This was tested for: |
98 | | * pairs that differed by one bit, by two bits, in any combination |
99 | | of top bits of (a,b,c), or in any combination of bottom bits of |
100 | | (a,b,c). |
101 | | * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed |
102 | | the output delta to a Gray code (a^(a>>1)) so a string of 1's (as |
103 | | is commonly produced by subtraction) look like a single 1-bit |
104 | | difference. |
105 | | * the base values were pseudorandom, all zero but one bit set, or |
106 | | all zero plus a counter that starts at zero. |
107 | | |
108 | | Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that |
109 | | satisfy this are |
110 | | 4 6 8 16 19 4 |
111 | | 9 15 3 18 27 15 |
112 | | 14 9 3 7 17 3 |
113 | | Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing |
114 | | for "differ" defined as + with a one-bit base and a two-bit delta. I |
115 | | used http://burtleburtle.net/bob/hash/avalanche.html to choose |
116 | | the operations, constants, and arrangements of the variables. |
117 | | |
118 | | This does not achieve avalanche. There are input bits of (a,b,c) |
119 | | that fail to affect some output bits of (a,b,c), especially of a. The |
120 | | most thoroughly mixed value is c, but it doesn't really even achieve |
121 | | avalanche in c. |
122 | | |
123 | | This allows some parallelism. Read-after-writes are good at doubling |
124 | | the number of bits affected, so the goal of mixing pulls in the opposite |
125 | | direction as the goal of parallelism. I did what I could. Rotates |
126 | | seem to cost as much as shifts on every machine I could lay my hands |
127 | | on, and rotates are much kinder to the top and bottom bits, so I used |
128 | | rotates. |
129 | | ------------------------------------------------------------------------------- |
130 | | */ |
131 | 0 | #define mix(a,b,c) \ |
132 | 0 | { \ |
133 | 0 | a -= c; a ^= rot(c, 4); c += b; \ |
134 | 0 | b -= a; b ^= rot(a, 6); a += c; \ |
135 | 0 | c -= b; c ^= rot(b, 8); b += a; \ |
136 | 0 | a -= c; a ^= rot(c,16); c += b; \ |
137 | 0 | b -= a; b ^= rot(a,19); a += c; \ |
138 | 0 | c -= b; c ^= rot(b, 4); b += a; \ |
139 | 0 | } |
140 | | |
141 | | /* |
142 | | ------------------------------------------------------------------------------- |
143 | | final -- final mixing of 3 32-bit values (a,b,c) into c |
144 | | |
145 | | Pairs of (a,b,c) values differing in only a few bits will usually |
146 | | produce values of c that look totally different. This was tested for |
147 | | * pairs that differed by one bit, by two bits, in any combination |
148 | | of top bits of (a,b,c), or in any combination of bottom bits of |
149 | | (a,b,c). |
150 | | * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed |
151 | | the output delta to a Gray code (a^(a>>1)) so a string of 1's (as |
152 | | is commonly produced by subtraction) look like a single 1-bit |
153 | | difference. |
154 | | * the base values were pseudorandom, all zero but one bit set, or |
155 | | all zero plus a counter that starts at zero. |
156 | | |
157 | | These constants passed: |
158 | | 14 11 25 16 4 14 24 |
159 | | 12 14 25 16 4 14 24 |
160 | | and these came close: |
161 | | 4 8 15 26 3 22 24 |
162 | | 10 8 15 26 3 22 24 |
163 | | 11 8 15 26 3 22 24 |
164 | | ------------------------------------------------------------------------------- |
165 | | */ |
166 | 0 | #define final(a,b,c) \ |
167 | 0 | { \ |
168 | 0 | c ^= b; c -= rot(b,14); \ |
169 | 0 | a ^= c; a -= rot(c,11); \ |
170 | 0 | b ^= a; b -= rot(a,25); \ |
171 | 0 | c ^= b; c -= rot(b,16); \ |
172 | 0 | a ^= c; a -= rot(c,4); \ |
173 | 0 | b ^= a; b -= rot(a,14); \ |
174 | 0 | c ^= b; c -= rot(b,24); \ |
175 | 0 | } |
176 | | |
177 | | |
178 | | /* |
179 | | ------------------------------------------------------------------------------- |
180 | | hashlittle() -- hash a variable-length key into a 32-bit value |
181 | | k : the key (the unaligned variable-length array of bytes) |
182 | | length : the length of the key, counting by bytes |
183 | | val2 : IN: can be any 4-byte value OUT: second 32 bit hash. |
184 | | Returns a 32-bit value. Every bit of the key affects every bit of |
185 | | the return value. Two keys differing by one or two bits will have |
186 | | totally different hash values. Note that the return value is better |
187 | | mixed than val2, so use that first. |
188 | | |
189 | | The best hash table sizes are powers of 2. There is no need to do |
190 | | mod a prime (mod is sooo slow!). If you need less than 32 bits, |
191 | | use a bitmask. For example, if you need only 10 bits, do |
192 | | h = (h & hashmask(10)); |
193 | | In which case, the hash table should have hashsize(10) elements. |
194 | | |
195 | | If you are hashing n strings (uint8_t **)k, do it like this: |
196 | | for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h); |
197 | | |
198 | | By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this |
199 | | code any way you wish, private, educational, or commercial. It's free. |
200 | | |
201 | | Use for hash table lookup, or anything where one collision in 2^^32 is |
202 | | acceptable. Do NOT use for cryptographic purposes. |
203 | | ------------------------------------------------------------------------------- |
204 | | */ |
205 | | |
206 | | static uint32_t hashlittle( const void *key, size_t length ) |
207 | 0 | { |
208 | 0 | uint32_t a,b,c; /* internal state */ |
209 | 0 | union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */ |
210 | | |
211 | | /* Set up the internal state */ |
212 | 0 | a = b = c = 0xdeadbeef + ((uint32_t)length); |
213 | |
|
214 | 0 | u.ptr = key; |
215 | 0 | if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) { |
216 | 0 | const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ |
217 | 0 | const uint8_t *k8; |
218 | | |
219 | | /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ |
220 | 0 | while (length > 12) |
221 | 0 | { |
222 | 0 | a += k[0]; |
223 | 0 | b += k[1]; |
224 | 0 | c += k[2]; |
225 | 0 | mix(a,b,c); |
226 | 0 | length -= 12; |
227 | 0 | k += 3; |
228 | 0 | } |
229 | | |
230 | | /*----------------------------- handle the last (probably partial) block */ |
231 | 0 | k8 = (const uint8_t *)k; |
232 | 0 | switch(length) |
233 | 0 | { |
234 | 0 | case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; |
235 | 0 | case 11: c+=((uint32_t)k8[10])<<16; FALL_THROUGH; |
236 | 0 | case 10: c+=((uint32_t)k8[9])<<8; FALL_THROUGH; |
237 | 0 | case 9 : c+=k8[8]; FALL_THROUGH; |
238 | 0 | case 8 : b+=k[1]; a+=k[0]; break; |
239 | 0 | case 7 : b+=((uint32_t)k8[6])<<16; FALL_THROUGH; |
240 | 0 | case 6 : b+=((uint32_t)k8[5])<<8; FALL_THROUGH; |
241 | 0 | case 5 : b+=k8[4]; FALL_THROUGH; |
242 | 0 | case 4 : a+=k[0]; break; |
243 | 0 | case 3 : a+=((uint32_t)k8[2])<<16; FALL_THROUGH; |
244 | 0 | case 2 : a+=((uint32_t)k8[1])<<8; FALL_THROUGH; |
245 | 0 | case 1 : a+=k8[0]; break; |
246 | 0 | case 0 : return c; |
247 | 0 | } |
248 | 0 | } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { |
249 | 0 | const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ |
250 | 0 | const uint8_t *k8; |
251 | | |
252 | | /*--------------- all but last block: aligned reads and different mixing */ |
253 | 0 | while (length > 12) |
254 | 0 | { |
255 | 0 | a += k[0] + (((uint32_t)k[1])<<16); |
256 | 0 | b += k[2] + (((uint32_t)k[3])<<16); |
257 | 0 | c += k[4] + (((uint32_t)k[5])<<16); |
258 | 0 | mix(a,b,c); |
259 | 0 | length -= 12; |
260 | 0 | k += 6; |
261 | 0 | } |
262 | | |
263 | | /*----------------------------- handle the last (probably partial) block */ |
264 | 0 | k8 = (const uint8_t *)k; |
265 | 0 | switch(length) |
266 | 0 | { |
267 | 0 | case 12: c+=k[4]+(((uint32_t)k[5])<<16); |
268 | 0 | b+=k[2]+(((uint32_t)k[3])<<16); |
269 | 0 | a+=k[0]+(((uint32_t)k[1])<<16); |
270 | 0 | break; |
271 | 0 | case 11: c+=((uint32_t)k8[10])<<16; FALL_THROUGH; |
272 | 0 | case 10: c+=k[4]; |
273 | 0 | b+=k[2]+(((uint32_t)k[3])<<16); |
274 | 0 | a+=k[0]+(((uint32_t)k[1])<<16); |
275 | 0 | break; |
276 | 0 | case 9 : c+=k8[8]; FALL_THROUGH; |
277 | 0 | case 8 : b+=k[2]+(((uint32_t)k[3])<<16); |
278 | 0 | a+=k[0]+(((uint32_t)k[1])<<16); |
279 | 0 | break; |
280 | 0 | case 7 : b+=((uint32_t)k8[6])<<16; FALL_THROUGH; |
281 | 0 | case 6 : b+=k[2]; |
282 | 0 | a+=k[0]+(((uint32_t)k[1])<<16); |
283 | 0 | break; |
284 | 0 | case 5 : b+=k8[4]; FALL_THROUGH; |
285 | 0 | case 4 : a+=k[0]+(((uint32_t)k[1])<<16); |
286 | 0 | break; |
287 | 0 | case 3 : a+=((uint32_t)k8[2])<<16; FALL_THROUGH; |
288 | 0 | case 2 : a+=k[0]; |
289 | 0 | break; |
290 | 0 | case 1 : a+=k8[0]; |
291 | 0 | break; |
292 | 0 | case 0 : return c; /* zero length requires no mixing */ |
293 | 0 | } |
294 | |
|
295 | 0 | } else { /* need to read the key one byte at a time */ |
296 | 0 | const uint8_t *k = (const uint8_t *)key; |
297 | | |
298 | | /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ |
299 | 0 | while (length > 12) |
300 | 0 | { |
301 | 0 | a += k[0]; |
302 | 0 | a += ((uint32_t)k[1])<<8; |
303 | 0 | a += ((uint32_t)k[2])<<16; |
304 | 0 | a += ((uint32_t)k[3])<<24; |
305 | 0 | b += k[4]; |
306 | 0 | b += ((uint32_t)k[5])<<8; |
307 | 0 | b += ((uint32_t)k[6])<<16; |
308 | 0 | b += ((uint32_t)k[7])<<24; |
309 | 0 | c += k[8]; |
310 | 0 | c += ((uint32_t)k[9])<<8; |
311 | 0 | c += ((uint32_t)k[10])<<16; |
312 | 0 | c += ((uint32_t)k[11])<<24; |
313 | 0 | mix(a,b,c); |
314 | 0 | length -= 12; |
315 | 0 | k += 12; |
316 | 0 | } |
317 | | |
318 | | /*-------------------------------- last block: affect all 32 bits of (c) */ |
319 | 0 | switch(length) |
320 | 0 | { |
321 | 0 | case 12: c+=((uint32_t)k[11])<<24; FALL_THROUGH; |
322 | 0 | case 11: c+=((uint32_t)k[10])<<16; FALL_THROUGH; |
323 | 0 | case 10: c+=((uint32_t)k[9])<<8; FALL_THROUGH; |
324 | 0 | case 9 : c+=k[8]; FALL_THROUGH; |
325 | 0 | case 8 : b+=((uint32_t)k[7])<<24; FALL_THROUGH; |
326 | 0 | case 7 : b+=((uint32_t)k[6])<<16; FALL_THROUGH; |
327 | 0 | case 6 : b+=((uint32_t)k[5])<<8; FALL_THROUGH; |
328 | 0 | case 5 : b+=k[4]; FALL_THROUGH; |
329 | 0 | case 4 : a+=((uint32_t)k[3])<<24; FALL_THROUGH; |
330 | 0 | case 3 : a+=((uint32_t)k[2])<<16; FALL_THROUGH; |
331 | 0 | case 2 : a+=((uint32_t)k[1])<<8; FALL_THROUGH; |
332 | 0 | case 1 : a+=k[0]; |
333 | 0 | break; |
334 | 0 | case 0 : return c; |
335 | 0 | } |
336 | 0 | } |
337 | | |
338 | 0 | final(a,b,c); |
339 | 0 | return c; |
340 | 0 | } |
341 | | |
342 | | _PUBLIC_ unsigned int tdb_jenkins_hash(TDB_DATA *key) |
343 | 0 | { |
344 | 0 | return hashlittle(key->dptr, key->dsize); |
345 | 0 | } |