Commit | Line | Data |
---|---|---|
4d3a042d YO |
1 | /* hash a key |
2 | *-------------------------------------------------------------------------------------- | |
3 | * The "hash seed" feature was added in Perl 5.8.1 to perturb the results | |
4 | * to avoid "algorithmic complexity attacks". | |
5 | * | |
6 | * If USE_HASH_SEED is defined, hash randomisation is done by default | |
7 | * If USE_HASH_SEED_EXPLICIT is defined, hash randomisation is done | |
8 | * only if the environment variable PERL_HASH_SEED is set. | |
9 | * (see also perl.c:perl_parse() and S_init_tls_and_interp() and util.c:get_hash_seed()) | |
10 | */ | |
11 | ||
12 | #ifndef PERL_SEEN_HV_FUNC_H /* compile once */ | |
13 | #define PERL_SEEN_HV_FUNC_H | |
14 | ||
15 | #if !( 0 \ | |
e6b54db6 | 16 | || defined(PERL_HASH_FUNC_SIPHASH) \ |
4d3a042d YO |
17 | || defined(PERL_HASH_FUNC_SDBM) \ |
18 | || defined(PERL_HASH_FUNC_DJB2) \ | |
19 | || defined(PERL_HASH_FUNC_SUPERFAST) \ | |
20 | || defined(PERL_HASH_FUNC_MURMUR3) \ | |
21 | || defined(PERL_HASH_FUNC_ONE_AT_A_TIME) \ | |
b1300a73 | 22 | || defined(PERL_HASH_FUNC_ONE_AT_A_TIME_HARD) \ |
4d3a042d YO |
23 | || defined(PERL_HASH_FUNC_ONE_AT_A_TIME_OLD) \ |
24 | ) | |
b1300a73 | 25 | #define PERL_HASH_FUNC_ONE_AT_A_TIME_HARD |
4d3a042d | 26 | #endif |
4d3a042d YO |
27 | |
28 | #if defined(PERL_HASH_FUNC_SIPHASH) | |
29 | # define PERL_HASH_FUNC "SIPHASH_2_4" | |
30 | # define PERL_HASH_SEED_BYTES 16 | |
3d53a8ea | 31 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_siphash_2_4((seed),(U8*)(str),(len)) |
4d3a042d YO |
32 | #elif defined(PERL_HASH_FUNC_SUPERFAST) |
33 | # define PERL_HASH_FUNC "SUPERFAST" | |
34 | # define PERL_HASH_SEED_BYTES 4 | |
3d53a8ea | 35 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_superfast((seed),(U8*)(str),(len)) |
4d3a042d YO |
36 | #elif defined(PERL_HASH_FUNC_MURMUR3) |
37 | # define PERL_HASH_FUNC "MURMUR3" | |
38 | # define PERL_HASH_SEED_BYTES 4 | |
3d53a8ea | 39 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_murmur3((seed),(U8*)(str),(len)) |
4d3a042d YO |
40 | #elif defined(PERL_HASH_FUNC_DJB2) |
41 | # define PERL_HASH_FUNC "DJB2" | |
42 | # define PERL_HASH_SEED_BYTES 4 | |
3d53a8ea | 43 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_djb2((seed),(U8*)(str),(len)) |
4d3a042d YO |
44 | #elif defined(PERL_HASH_FUNC_SDBM) |
45 | # define PERL_HASH_FUNC "SDBM" | |
46 | # define PERL_HASH_SEED_BYTES 4 | |
3d53a8ea | 47 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_sdbm((seed),(U8*)(str),(len)) |
b1300a73 YO |
48 | #elif defined(PERL_HASH_FUNC_ONE_AT_A_TIME_HARD) |
49 | # define PERL_HASH_FUNC "ONE_AT_A_TIME_HARD" | |
50 | # define PERL_HASH_SEED_BYTES 8 | |
3d53a8ea | 51 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_one_at_a_time_hard((seed),(U8*)(str),(len)) |
4d3a042d YO |
52 | #elif defined(PERL_HASH_FUNC_ONE_AT_A_TIME) |
53 | # define PERL_HASH_FUNC "ONE_AT_A_TIME" | |
54 | # define PERL_HASH_SEED_BYTES 4 | |
3d53a8ea | 55 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_one_at_a_time((seed),(U8*)(str),(len)) |
4d3a042d YO |
56 | #elif defined(PERL_HASH_FUNC_ONE_AT_A_TIME_OLD) |
57 | # define PERL_HASH_FUNC "ONE_AT_A_TIME_OLD" | |
58 | # define PERL_HASH_SEED_BYTES 4 | |
3d53a8ea | 59 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_old_one_at_a_time((seed),(U8*)(str),(len)) |
4d3a042d YO |
60 | #endif |
61 | ||
3d53a8ea | 62 | #ifndef PERL_HASH_WITH_SEED |
4d3a042d YO |
63 | #error "No hash function defined!" |
64 | #endif | |
65 | #ifndef PERL_HASH_SEED_BYTES | |
66 | #error "PERL_HASH_SEED_BYTES not defined" | |
67 | #endif | |
68 | #ifndef PERL_HASH_FUNC | |
69 | #error "PERL_HASH_FUNC not defined" | |
70 | #endif | |
71 | ||
72 | #ifndef PERL_HASH_SEED | |
73 | # if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) | |
74 | # define PERL_HASH_SEED PL_hash_seed | |
75 | # elif PERL_HASH_SEED_BYTES == 4 | |
76 | # define PERL_HASH_SEED "PeRl" | |
77 | # elif PERL_HASH_SEED_BYTES == 16 | |
78 | # define PERL_HASH_SEED "PeRlHaShhAcKpErl" | |
79 | # else | |
80 | # error "No PERL_HASH_SEED definition for " PERL_HASH_FUNC | |
81 | # endif | |
82 | #endif | |
83 | ||
3d53a8ea YO |
84 | #define PERL_HASH(hash,str,len) PERL_HASH_WITH_SEED(PERL_HASH_SEED,hash,str,len) |
85 | ||
4d3a042d YO |
86 | /*----------------------------------------------------------------------------- |
87 | * Endianess, misalignment capabilities and util macros | |
88 | * | |
89 | * The following 3 macros are defined in this section. The other macros defined | |
90 | * are only needed to help derive these 3. | |
91 | * | |
92 | * U8TO32_LE(x) Read a little endian unsigned 32-bit int | |
93 | * UNALIGNED_SAFE Defined if READ_UINT32 works on non-word boundaries | |
94 | * ROTL32(x,r) Rotate x left by r bits | |
95 | */ | |
96 | ||
97 | #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ | |
0a35fd8a | 98 | || defined(_MSC_VER) || defined (__TURBOC__) |
4d3a042d YO |
99 | #define U8TO16_LE(d) (*((const U16 *) (d))) |
100 | #endif | |
101 | ||
102 | #if !defined (U8TO16_LE) | |
103 | #define U8TO16_LE(d) ((((const U8 *)(d))[1] << 8)\ | |
104 | +((const U8 *)(d))[0]) | |
105 | #endif | |
106 | ||
107 | ||
108 | /* Now find best way we can to READ_UINT32 */ | |
109 | #if (BYTEORDER == 0x1234 || BYTEORDER == 0x12345678) && U32SIZE == 4 | |
110 | /* CPU endian matches murmurhash algorithm, so read 32-bit word directly */ | |
111 | #define U8TO32_LE(ptr) (*((U32*)(ptr))) | |
112 | #elif BYTEORDER == 0x4321 || BYTEORDER == 0x87654321 | |
113 | /* TODO: Add additional cases below where a compiler provided bswap32 is available */ | |
114 | #if defined(__GNUC__) && (__GNUC__>4 || (__GNUC__==4 && __GNUC_MINOR__>=3)) | |
115 | #define U8TO32_LE(ptr) (__builtin_bswap32(*((U32*)(ptr)))) | |
116 | #else | |
117 | /* Without a known fast bswap32 we're just as well off doing this */ | |
118 | #define U8TO32_LE(ptr) (ptr[0]|ptr[1]<<8|ptr[2]<<16|ptr[3]<<24) | |
119 | #define UNALIGNED_SAFE | |
120 | #endif | |
121 | #else | |
122 | /* Unknown endianess so last resort is to read individual bytes */ | |
123 | #define U8TO32_LE(ptr) (ptr[0]|ptr[1]<<8|ptr[2]<<16|ptr[3]<<24) | |
124 | /* Since we're not doing word-reads we can skip the messing about with realignment */ | |
125 | #define UNALIGNED_SAFE | |
126 | #endif | |
127 | ||
b716320d YO |
128 | #ifdef HAS_QUAD |
129 | #ifndef U64TYPE | |
130 | /* This probably isn't going to work, but failing with a compiler error due to | |
131 | lack of uint64_t is no worse than failing right now with an #error. */ | |
132 | #define U64TYPE uint64_t | |
133 | #endif | |
134 | #endif | |
135 | ||
136 | /* Find best way to ROTL32/ROTL64 */ | |
4d3a042d YO |
137 | #if defined(_MSC_VER) |
138 | #include <stdlib.h> /* Microsoft put _rotl declaration in here */ | |
139 | #define ROTL32(x,r) _rotl(x,r) | |
b716320d YO |
140 | #ifdef HAS_QUAD |
141 | #define ROTL64(x,r) _rotl64(x,r) | |
142 | #endif | |
4d3a042d YO |
143 | #else |
144 | /* gcc recognises this code and generates a rotate instruction for CPUs with one */ | |
145 | #define ROTL32(x,r) (((U32)x << r) | ((U32)x >> (32 - r))) | |
b716320d YO |
146 | #ifdef HAS_QUAD |
147 | #define ROTL64(x,r) (((U64TYPE)x << r) | ((U64TYPE)x >> (64 - r))) | |
148 | #endif | |
4d3a042d YO |
149 | #endif |
150 | ||
151 | ||
b716320d YO |
152 | #ifdef UV_IS_QUAD |
153 | #define ROTL_UV(x,r) ROTL64(x,r) | |
154 | #else | |
155 | #define ROTL_UV(x,r) ROTL32(x,r) | |
156 | #endif | |
157 | ||
4d3a042d YO |
158 | /* This is SipHash by Jean-Philippe Aumasson and Daniel J. Bernstein. |
159 | * The authors claim it is relatively secure compared to the alternatives | |
160 | * and that performance wise it is a suitable hash for languages like Perl. | |
161 | * See: | |
162 | * | |
163 | * https://www.131002.net/siphash/ | |
164 | * | |
165 | * This implementation seems to perform slightly slower than one-at-a-time for | |
166 | * short keys, but degrades slower for longer keys. Murmur Hash outperforms it | |
167 | * regardless of keys size. | |
168 | * | |
169 | * It is 64 bit only. | |
170 | */ | |
171 | ||
172 | #ifdef HAS_QUAD | |
173 | ||
4d3a042d YO |
174 | #define U8TO64_LE(p) \ |
175 | (((U64TYPE)((p)[0]) ) | \ | |
176 | ((U64TYPE)((p)[1]) << 8) | \ | |
177 | ((U64TYPE)((p)[2]) << 16) | \ | |
178 | ((U64TYPE)((p)[3]) << 24) | \ | |
179 | ((U64TYPE)((p)[4]) << 32) | \ | |
180 | ((U64TYPE)((p)[5]) << 40) | \ | |
181 | ((U64TYPE)((p)[6]) << 48) | \ | |
182 | ((U64TYPE)((p)[7]) << 56)) | |
183 | ||
184 | #define SIPROUND \ | |
185 | do { \ | |
186 | v0 += v1; v1=ROTL64(v1,13); v1 ^= v0; v0=ROTL64(v0,32); \ | |
187 | v2 += v3; v3=ROTL64(v3,16); v3 ^= v2; \ | |
188 | v0 += v3; v3=ROTL64(v3,21); v3 ^= v0; \ | |
189 | v2 += v1; v1=ROTL64(v1,17); v1 ^= v2; v2=ROTL64(v2,32); \ | |
190 | } while(0) | |
191 | ||
192 | /* SipHash-2-4 */ | |
193 | ||
194 | PERL_STATIC_INLINE U32 | |
195 | S_perl_hash_siphash_2_4(const unsigned char * const seed, const unsigned char *in, const STRLEN inlen) { | |
196 | /* "somepseudorandomlygeneratedbytes" */ | |
f4e3fd26 JH |
197 | U64TYPE v0 = UINT64_C(0x736f6d6570736575); |
198 | U64TYPE v1 = UINT64_C(0x646f72616e646f6d); | |
199 | U64TYPE v2 = UINT64_C(0x6c7967656e657261); | |
200 | U64TYPE v3 = UINT64_C(0x7465646279746573); | |
4d3a042d YO |
201 | |
202 | U64TYPE b; | |
203 | U64TYPE k0 = ((U64TYPE*)seed)[0]; | |
204 | U64TYPE k1 = ((U64TYPE*)seed)[1]; | |
205 | U64TYPE m; | |
206 | const int left = inlen & 7; | |
207 | const U8 *end = in + inlen - left; | |
208 | ||
209 | b = ( ( U64TYPE )(inlen) ) << 56; | |
210 | v3 ^= k1; | |
211 | v2 ^= k0; | |
212 | v1 ^= k1; | |
213 | v0 ^= k0; | |
214 | ||
215 | for ( ; in != end; in += 8 ) | |
216 | { | |
217 | m = U8TO64_LE( in ); | |
218 | v3 ^= m; | |
219 | SIPROUND; | |
220 | SIPROUND; | |
221 | v0 ^= m; | |
222 | } | |
223 | ||
224 | switch( left ) | |
225 | { | |
226 | case 7: b |= ( ( U64TYPE )in[ 6] ) << 48; | |
227 | case 6: b |= ( ( U64TYPE )in[ 5] ) << 40; | |
228 | case 5: b |= ( ( U64TYPE )in[ 4] ) << 32; | |
229 | case 4: b |= ( ( U64TYPE )in[ 3] ) << 24; | |
230 | case 3: b |= ( ( U64TYPE )in[ 2] ) << 16; | |
231 | case 2: b |= ( ( U64TYPE )in[ 1] ) << 8; | |
232 | case 1: b |= ( ( U64TYPE )in[ 0] ); break; | |
233 | case 0: break; | |
234 | } | |
235 | ||
236 | v3 ^= b; | |
237 | SIPROUND; | |
238 | SIPROUND; | |
239 | v0 ^= b; | |
240 | ||
241 | v2 ^= 0xff; | |
242 | SIPROUND; | |
243 | SIPROUND; | |
244 | SIPROUND; | |
245 | SIPROUND; | |
246 | b = v0 ^ v1 ^ v2 ^ v3; | |
247 | return (U32)(b & U32_MAX); | |
248 | } | |
249 | #endif /* defined(HAS_QUAD) */ | |
250 | ||
251 | /* FYI: This is the "Super-Fast" algorithm mentioned by Bob Jenkins in | |
252 | * (http://burtleburtle.net/bob/hash/doobs.html) | |
253 | * It is by Paul Hsieh (c) 2004 and is analysed here | |
254 | * http://www.azillionmonkeys.com/qed/hash.html | |
255 | * license terms are here: | |
256 | * http://www.azillionmonkeys.com/qed/weblicense.html | |
257 | */ | |
258 | ||
259 | ||
260 | PERL_STATIC_INLINE U32 | |
261 | S_perl_hash_superfast(const unsigned char * const seed, const unsigned char *str, STRLEN len) { | |
262 | U32 hash = *((U32*)seed) + len; | |
263 | U32 tmp; | |
264 | int rem= len & 3; | |
265 | len >>= 2; | |
266 | ||
267 | for (;len > 0; len--) { | |
268 | hash += U8TO16_LE (str); | |
269 | tmp = (U8TO16_LE (str+2) << 11) ^ hash; | |
270 | hash = (hash << 16) ^ tmp; | |
271 | str += 2 * sizeof (U16); | |
272 | hash += hash >> 11; | |
273 | } | |
274 | ||
275 | /* Handle end cases */ | |
276 | switch (rem) { \ | |
277 | case 3: hash += U8TO16_LE (str); | |
278 | hash ^= hash << 16; | |
279 | hash ^= str[sizeof (U16)] << 18; | |
280 | hash += hash >> 11; | |
281 | break; | |
282 | case 2: hash += U8TO16_LE (str); | |
283 | hash ^= hash << 11; | |
284 | hash += hash >> 17; | |
285 | break; | |
286 | case 1: hash += *str; | |
287 | hash ^= hash << 10; | |
288 | hash += hash >> 1; | |
289 | } | |
290 | /* Force "avalanching" of final 127 bits */ | |
291 | hash ^= hash << 3; | |
292 | hash += hash >> 5; | |
293 | hash ^= hash << 4; | |
294 | hash += hash >> 17; | |
295 | hash ^= hash << 25; | |
296 | return (hash + (hash >> 6)); | |
297 | } | |
298 | ||
299 | ||
300 | /*----------------------------------------------------------------------------- | |
301 | * MurmurHash3 was written by Austin Appleby, and is placed in the public | |
302 | * domain. | |
303 | * | |
304 | * This implementation was originally written by Shane Day, and is also public domain, | |
305 | * and was modified to function as a macro similar to other perl hash functions by | |
306 | * Yves Orton. | |
307 | * | |
308 | * This is a portable ANSI C implementation of MurmurHash3_x86_32 (Murmur3A) | |
309 | * with support for progressive processing. | |
310 | * | |
311 | * If you want to understand the MurmurHash algorithm you would be much better | |
312 | * off reading the original source. Just point your browser at: | |
313 | * http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp | |
314 | * | |
315 | * How does it work? | |
316 | * | |
317 | * We can only process entire 32 bit chunks of input, except for the very end | |
318 | * that may be shorter. | |
319 | * | |
320 | * To handle endianess I simply use a macro that reads a U32 and define | |
321 | * that macro to be a direct read on little endian machines, a read and swap | |
322 | * on big endian machines, or a byte-by-byte read if the endianess is unknown. | |
323 | */ | |
324 | ||
325 | ||
326 | /*----------------------------------------------------------------------------- | |
327 | * Core murmurhash algorithm macros */ | |
328 | ||
329 | #define MURMUR_C1 (0xcc9e2d51) | |
330 | #define MURMUR_C2 (0x1b873593) | |
331 | #define MURMUR_C3 (0xe6546b64) | |
332 | #define MURMUR_C4 (0x85ebca6b) | |
333 | #define MURMUR_C5 (0xc2b2ae35) | |
334 | ||
335 | /* This is the main processing body of the algorithm. It operates | |
336 | * on each full 32-bits of input. */ | |
337 | #define MURMUR_DOBLOCK(h1, k1) STMT_START { \ | |
338 | k1 *= MURMUR_C1; \ | |
339 | k1 = ROTL32(k1,15); \ | |
340 | k1 *= MURMUR_C2; \ | |
341 | \ | |
342 | h1 ^= k1; \ | |
343 | h1 = ROTL32(h1,13); \ | |
344 | h1 = h1 * 5 + MURMUR_C3; \ | |
345 | } STMT_END | |
346 | ||
347 | ||
348 | /* Append unaligned bytes to carry, forcing hash churn if we have 4 bytes */ | |
349 | /* cnt=bytes to process, h1=name of h1 var, c=carry, n=bytes in c, ptr/len=payload */ | |
350 | #define MURMUR_DOBYTES(cnt, h1, c, n, ptr, len) STMT_START { \ | |
351 | int MURMUR_DOBYTES_i = cnt; \ | |
352 | while(MURMUR_DOBYTES_i--) { \ | |
353 | c = c>>8 | *ptr++<<24; \ | |
354 | n++; len--; \ | |
355 | if(n==4) { \ | |
356 | MURMUR_DOBLOCK(h1, c); \ | |
357 | n = 0; \ | |
358 | } \ | |
359 | } \ | |
360 | } STMT_END | |
361 | ||
362 | ||
363 | /* now we create the hash function */ | |
364 | PERL_STATIC_INLINE U32 | |
365 | S_perl_hash_murmur3(const unsigned char * const seed, const unsigned char *ptr, STRLEN len) { | |
366 | U32 h1 = *((U32*)seed); | |
367 | U32 k1; | |
368 | U32 carry = 0; | |
369 | ||
370 | const unsigned char *end; | |
371 | int bytes_in_carry = 0; /* bytes in carry */ | |
372 | I32 total_length= len; | |
373 | ||
374 | #if defined(UNALIGNED_SAFE) | |
375 | /* Handle carry: commented out as its only used in incremental mode - it never fires for us | |
376 | int i = (4-n) & 3; | |
377 | if(i && i <= len) { | |
378 | MURMUR_DOBYTES(i, h1, carry, bytes_in_carry, ptr, len); | |
379 | } | |
380 | */ | |
381 | ||
382 | /* This CPU handles unaligned word access */ | |
383 | /* Process 32-bit chunks */ | |
384 | end = ptr + len/4*4; | |
385 | for( ; ptr < end ; ptr+=4) { | |
386 | k1 = U8TO32_LE(ptr); | |
387 | MURMUR_DOBLOCK(h1, k1); | |
388 | } | |
389 | #else | |
390 | /* This CPU does not handle unaligned word access */ | |
391 | ||
392 | /* Consume enough so that the next data byte is word aligned */ | |
c8523631 RB |
393 | STRLEN i = -PTR2IV(ptr) & 3; |
394 | if(i && i <= len) { | |
4d3a042d YO |
395 | MURMUR_DOBYTES(i, h1, carry, bytes_in_carry, ptr, len); |
396 | } | |
397 | ||
398 | /* We're now aligned. Process in aligned blocks. Specialise for each possible carry count */ | |
399 | end = ptr + len/4*4; | |
400 | switch(bytes_in_carry) { /* how many bytes in carry */ | |
401 | case 0: /* c=[----] w=[3210] b=[3210]=w c'=[----] */ | |
402 | for( ; ptr < end ; ptr+=4) { | |
403 | k1 = U8TO32_LE(ptr); | |
404 | MURMUR_DOBLOCK(h1, k1); | |
405 | } | |
406 | break; | |
407 | case 1: /* c=[0---] w=[4321] b=[3210]=c>>24|w<<8 c'=[4---] */ | |
408 | for( ; ptr < end ; ptr+=4) { | |
409 | k1 = carry>>24; | |
410 | carry = U8TO32_LE(ptr); | |
411 | k1 |= carry<<8; | |
412 | MURMUR_DOBLOCK(h1, k1); | |
413 | } | |
414 | break; | |
415 | case 2: /* c=[10--] w=[5432] b=[3210]=c>>16|w<<16 c'=[54--] */ | |
416 | for( ; ptr < end ; ptr+=4) { | |
417 | k1 = carry>>16; | |
418 | carry = U8TO32_LE(ptr); | |
419 | k1 |= carry<<16; | |
420 | MURMUR_DOBLOCK(h1, k1); | |
421 | } | |
422 | break; | |
423 | case 3: /* c=[210-] w=[6543] b=[3210]=c>>8|w<<24 c'=[654-] */ | |
424 | for( ; ptr < end ; ptr+=4) { | |
425 | k1 = carry>>8; | |
426 | carry = U8TO32_LE(ptr); | |
427 | k1 |= carry<<24; | |
428 | MURMUR_DOBLOCK(h1, k1); | |
429 | } | |
430 | } | |
431 | #endif | |
432 | /* Advance over whole 32-bit chunks, possibly leaving 1..3 bytes */ | |
433 | len -= len/4*4; | |
434 | ||
435 | /* Append any remaining bytes into carry */ | |
436 | MURMUR_DOBYTES(len, h1, carry, bytes_in_carry, ptr, len); | |
437 | ||
438 | if (bytes_in_carry) { | |
439 | k1 = carry >> ( 4 - bytes_in_carry ) * 8; | |
440 | k1 *= MURMUR_C1; | |
441 | k1 = ROTL32(k1,15); | |
442 | k1 *= MURMUR_C2; | |
443 | h1 ^= k1; | |
444 | } | |
445 | h1 ^= total_length; | |
446 | ||
447 | /* fmix */ | |
448 | h1 ^= h1 >> 16; | |
449 | h1 *= MURMUR_C4; | |
450 | h1 ^= h1 >> 13; | |
451 | h1 *= MURMUR_C5; | |
452 | h1 ^= h1 >> 16; | |
453 | return h1; | |
454 | } | |
455 | ||
456 | ||
457 | PERL_STATIC_INLINE U32 | |
458 | S_perl_hash_djb2(const unsigned char * const seed, const unsigned char *str, const STRLEN len) { | |
459 | const unsigned char * const end = (const unsigned char *)str + len; | |
54e07e2b | 460 | U32 hash = *((U32*)seed) + len; |
4d3a042d YO |
461 | while (str < end) { |
462 | hash = ((hash << 5) + hash) + *str++; | |
463 | } | |
464 | return hash; | |
465 | } | |
466 | ||
467 | PERL_STATIC_INLINE U32 | |
468 | S_perl_hash_sdbm(const unsigned char * const seed, const unsigned char *str, const STRLEN len) { | |
469 | const unsigned char * const end = (const unsigned char *)str + len; | |
54e07e2b | 470 | U32 hash = *((U32*)seed) + len; |
4d3a042d YO |
471 | while (str < end) { |
472 | hash = (hash << 6) + (hash << 16) - hash + *str++; | |
473 | } | |
474 | return hash; | |
475 | } | |
476 | ||
fab3d107 DD |
477 | /* - ONE_AT_A_TIME_HARD is the 5.17+ recommend ONE_AT_A_TIME algorithm |
478 | * - ONE_AT_A_TIME_OLD is the unmodified 5.16 and older algorithm | |
479 | * - ONE_AT_A_TIME is a 5.17+ tweak of ONE_AT_A_TIME_OLD to | |
480 | * prevent strings of only \0 but different lengths from colliding | |
481 | * | |
482 | * Security-wise, from best to worst, | |
483 | * ONE_AT_A_TIME_HARD > ONE_AT_A_TIME > ONE_AT_A_TIME_OLD | |
484 | * There is a big drop-off in security between ONE_AT_A_TIME_HARD and | |
485 | * ONE_AT_A_TIME | |
486 | * */ | |
4d3a042d | 487 | |
b1300a73 | 488 | /* This is the "One-at-a-Time" algorithm by Bob Jenkins |
4d3a042d | 489 | * from requirements by Colin Plumb. |
b1300a73 YO |
490 | * (http://burtleburtle.net/bob/hash/doobs.html) |
491 | * With seed/len tweak. | |
492 | * */ | |
4d3a042d YO |
493 | PERL_STATIC_INLINE U32 |
494 | S_perl_hash_one_at_a_time(const unsigned char * const seed, const unsigned char *str, const STRLEN len) { | |
495 | const unsigned char * const end = (const unsigned char *)str + len; | |
496 | U32 hash = *((U32*)seed) + len; | |
497 | while (str < end) { | |
498 | hash += *str++; | |
499 | hash += (hash << 10); | |
500 | hash ^= (hash >> 6); | |
501 | } | |
502 | hash += (hash << 3); | |
503 | hash ^= (hash >> 11); | |
504 | return (hash + (hash << 15)); | |
505 | } | |
506 | ||
b1300a73 YO |
507 | /* Derived from "One-at-a-Time" algorithm by Bob Jenkins */ |
508 | PERL_STATIC_INLINE U32 | |
509 | S_perl_hash_one_at_a_time_hard(const unsigned char * const seed, const unsigned char *str, const STRLEN len) { | |
510 | const unsigned char * const end = (const unsigned char *)str + len; | |
511 | U32 hash = *((U32*)seed) + len; | |
512 | ||
513 | while (str < end) { | |
514 | hash += (hash << 10); | |
515 | hash ^= (hash >> 6); | |
516 | hash += *str++; | |
517 | } | |
518 | ||
519 | hash += (hash << 10); | |
520 | hash ^= (hash >> 6); | |
521 | hash += seed[4]; | |
522 | ||
523 | hash += (hash << 10); | |
524 | hash ^= (hash >> 6); | |
525 | hash += seed[5]; | |
526 | ||
527 | hash += (hash << 10); | |
528 | hash ^= (hash >> 6); | |
529 | hash += seed[6]; | |
530 | ||
531 | hash += (hash << 10); | |
532 | hash ^= (hash >> 6); | |
533 | hash += seed[7]; | |
534 | ||
535 | hash += (hash << 10); | |
536 | hash ^= (hash >> 6); | |
537 | ||
538 | hash += (hash << 3); | |
539 | hash ^= (hash >> 11); | |
540 | return (hash + (hash << 15)); | |
541 | } | |
542 | ||
4d3a042d YO |
543 | PERL_STATIC_INLINE U32 |
544 | S_perl_hash_old_one_at_a_time(const unsigned char * const seed, const unsigned char *str, const STRLEN len) { | |
545 | const unsigned char * const end = (const unsigned char *)str + len; | |
546 | U32 hash = *((U32*)seed); | |
547 | while (str < end) { | |
548 | hash += *str++; | |
549 | hash += (hash << 10); | |
550 | hash ^= (hash >> 6); | |
551 | } | |
552 | hash += (hash << 3); | |
553 | hash ^= (hash >> 11); | |
554 | return (hash + (hash << 15)); | |
555 | } | |
556 | ||
557 | /* legacy - only mod_perl should be doing this. */ | |
558 | #ifdef PERL_HASH_INTERNAL_ACCESS | |
559 | #define PERL_HASH_INTERNAL(hash,str,len) PERL_HASH(hash,str,len) | |
560 | #endif | |
561 | ||
562 | #endif /*compile once*/ | |
563 | ||
564 | /* | |
565 | * Local variables: | |
566 | * c-indentation-style: bsd | |
567 | * c-basic-offset: 4 | |
568 | * indent-tabs-mode: nil | |
569 | * End: | |
570 | * | |
571 | * ex: set ts=8 sts=4 sw=4 et: | |
572 | */ |