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