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) \ |
6b026047 YO |
17 | || defined(PERL_HASH_FUNC_SIPHASH13) \ |
18 | || defined(PERL_HASH_FUNC_HYBRID_OAATHU_SIPHASH13) \ | |
4d3a042d YO |
19 | || defined(PERL_HASH_FUNC_SDBM) \ |
20 | || defined(PERL_HASH_FUNC_DJB2) \ | |
21 | || defined(PERL_HASH_FUNC_SUPERFAST) \ | |
22 | || defined(PERL_HASH_FUNC_MURMUR3) \ | |
23 | || defined(PERL_HASH_FUNC_ONE_AT_A_TIME) \ | |
b1300a73 | 24 | || defined(PERL_HASH_FUNC_ONE_AT_A_TIME_HARD) \ |
4d3a042d | 25 | || defined(PERL_HASH_FUNC_ONE_AT_A_TIME_OLD) \ |
7e0dd61b YO |
26 | || defined(PERL_HASH_FUNC_MURMUR_HASH_64A) \ |
27 | || defined(PERL_HASH_FUNC_MURMUR_HASH_64B) \ | |
4d3a042d | 28 | ) |
6b026047 YO |
29 | #ifdef HAS_QUAD |
30 | #define PERL_HASH_FUNC_HYBRID_OAATHU_SIPHASH13 | |
31 | #else | |
b1300a73 | 32 | #define PERL_HASH_FUNC_ONE_AT_A_TIME_HARD |
4d3a042d | 33 | #endif |
6b026047 | 34 | #endif |
4d3a042d YO |
35 | |
36 | #if defined(PERL_HASH_FUNC_SIPHASH) | |
37 | # define PERL_HASH_FUNC "SIPHASH_2_4" | |
38 | # define PERL_HASH_SEED_BYTES 16 | |
3d53a8ea | 39 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_siphash_2_4((seed),(U8*)(str),(len)) |
6b026047 YO |
40 | #elif defined(PERL_HASH_FUNC_SIPHASH13) |
41 | # define PERL_HASH_FUNC "SIPHASH_1_3" | |
42 | # define PERL_HASH_SEED_BYTES 16 | |
43 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_siphash_1_3((seed),(U8*)(str),(len)) | |
44 | #elif defined(PERL_HASH_FUNC_HYBRID_OAATHU_SIPHASH13) | |
45 | # define PERL_HASH_FUNC "HYBRID_OAATHU_SIPHASH_1_3" | |
46 | # define PERL_HASH_SEED_BYTES 24 | |
47 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_oaathu_siphash_1_3((seed),(U8*)(str),(len)) | |
4d3a042d YO |
48 | #elif defined(PERL_HASH_FUNC_SUPERFAST) |
49 | # define PERL_HASH_FUNC "SUPERFAST" | |
50 | # define PERL_HASH_SEED_BYTES 4 | |
3d53a8ea | 51 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_superfast((seed),(U8*)(str),(len)) |
4d3a042d YO |
52 | #elif defined(PERL_HASH_FUNC_MURMUR3) |
53 | # define PERL_HASH_FUNC "MURMUR3" | |
54 | # define PERL_HASH_SEED_BYTES 4 | |
3d53a8ea | 55 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_murmur3((seed),(U8*)(str),(len)) |
4d3a042d YO |
56 | #elif defined(PERL_HASH_FUNC_DJB2) |
57 | # define PERL_HASH_FUNC "DJB2" | |
58 | # define PERL_HASH_SEED_BYTES 4 | |
3d53a8ea | 59 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_djb2((seed),(U8*)(str),(len)) |
4d3a042d YO |
60 | #elif defined(PERL_HASH_FUNC_SDBM) |
61 | # define PERL_HASH_FUNC "SDBM" | |
62 | # define PERL_HASH_SEED_BYTES 4 | |
3d53a8ea | 63 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_sdbm((seed),(U8*)(str),(len)) |
b1300a73 YO |
64 | #elif defined(PERL_HASH_FUNC_ONE_AT_A_TIME_HARD) |
65 | # define PERL_HASH_FUNC "ONE_AT_A_TIME_HARD" | |
66 | # define PERL_HASH_SEED_BYTES 8 | |
3d53a8ea | 67 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_one_at_a_time_hard((seed),(U8*)(str),(len)) |
4d3a042d YO |
68 | #elif defined(PERL_HASH_FUNC_ONE_AT_A_TIME) |
69 | # define PERL_HASH_FUNC "ONE_AT_A_TIME" | |
70 | # define PERL_HASH_SEED_BYTES 4 | |
3d53a8ea | 71 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_one_at_a_time((seed),(U8*)(str),(len)) |
4d3a042d YO |
72 | #elif defined(PERL_HASH_FUNC_ONE_AT_A_TIME_OLD) |
73 | # define PERL_HASH_FUNC "ONE_AT_A_TIME_OLD" | |
74 | # define PERL_HASH_SEED_BYTES 4 | |
3d53a8ea | 75 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_old_one_at_a_time((seed),(U8*)(str),(len)) |
7e0dd61b YO |
76 | #elif defined(PERL_HASH_FUNC_MURMUR_HASH_64A) |
77 | # define PERL_HASH_FUNC "MURMUR_HASH_64A" | |
78 | # define PERL_HASH_SEED_BYTES 8 | |
79 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_murmur_hash_64a((seed),(U8*)(str),(len)) | |
80 | #elif defined(PERL_HASH_FUNC_MURMUR_HASH_64B) | |
81 | # define PERL_HASH_FUNC "MURMUR_HASH_64B" | |
82 | # define PERL_HASH_SEED_BYTES 8 | |
83 | # define PERL_HASH_WITH_SEED(seed,hash,str,len) (hash)= S_perl_hash_murmur_hash_64b((seed),(U8*)(str),(len)) | |
4d3a042d YO |
84 | #endif |
85 | ||
3d53a8ea | 86 | #ifndef PERL_HASH_WITH_SEED |
4d3a042d YO |
87 | #error "No hash function defined!" |
88 | #endif | |
89 | #ifndef PERL_HASH_SEED_BYTES | |
90 | #error "PERL_HASH_SEED_BYTES not defined" | |
91 | #endif | |
92 | #ifndef PERL_HASH_FUNC | |
93 | #error "PERL_HASH_FUNC not defined" | |
94 | #endif | |
95 | ||
96 | #ifndef PERL_HASH_SEED | |
97 | # if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) | |
98 | # define PERL_HASH_SEED PL_hash_seed | |
99 | # elif PERL_HASH_SEED_BYTES == 4 | |
25c1b134 | 100 | # define PERL_HASH_SEED ((const U8 *)"PeRl") |
c2d4ebc6 | 101 | # elif PERL_HASH_SEED_BYTES == 8 |
25c1b134 | 102 | # define PERL_HASH_SEED ((const U8 *)"PeRlHaSh") |
4d3a042d | 103 | # elif PERL_HASH_SEED_BYTES == 16 |
25c1b134 | 104 | # define PERL_HASH_SEED ((const U8 *)"PeRlHaShhAcKpErl") |
4d3a042d YO |
105 | # else |
106 | # error "No PERL_HASH_SEED definition for " PERL_HASH_FUNC | |
107 | # endif | |
108 | #endif | |
109 | ||
3d53a8ea YO |
110 | #define PERL_HASH(hash,str,len) PERL_HASH_WITH_SEED(PERL_HASH_SEED,hash,str,len) |
111 | ||
4d3a042d YO |
112 | /*----------------------------------------------------------------------------- |
113 | * Endianess, misalignment capabilities and util macros | |
114 | * | |
115 | * The following 3 macros are defined in this section. The other macros defined | |
116 | * are only needed to help derive these 3. | |
117 | * | |
118 | * U8TO32_LE(x) Read a little endian unsigned 32-bit int | |
8757ed9b | 119 | * UNALIGNED_SAFE Defined if unaligned access is safe |
4d3a042d YO |
120 | * ROTL32(x,r) Rotate x left by r bits |
121 | */ | |
122 | ||
123 | #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ | |
0a35fd8a | 124 | || defined(_MSC_VER) || defined (__TURBOC__) |
4d3a042d YO |
125 | #define U8TO16_LE(d) (*((const U16 *) (d))) |
126 | #endif | |
127 | ||
128 | #if !defined (U8TO16_LE) | |
129 | #define U8TO16_LE(d) ((((const U8 *)(d))[1] << 8)\ | |
130 | +((const U8 *)(d))[0]) | |
131 | #endif | |
132 | ||
4d3a042d YO |
133 | #if (BYTEORDER == 0x1234 || BYTEORDER == 0x12345678) && U32SIZE == 4 |
134 | /* CPU endian matches murmurhash algorithm, so read 32-bit word directly */ | |
463ddf34 | 135 | #define U8TO32_LE(ptr) (*((const U32*)(ptr))) |
4d3a042d YO |
136 | #elif BYTEORDER == 0x4321 || BYTEORDER == 0x87654321 |
137 | /* TODO: Add additional cases below where a compiler provided bswap32 is available */ | |
138 | #if defined(__GNUC__) && (__GNUC__>4 || (__GNUC__==4 && __GNUC_MINOR__>=3)) | |
139 | #define U8TO32_LE(ptr) (__builtin_bswap32(*((U32*)(ptr)))) | |
140 | #else | |
141 | /* Without a known fast bswap32 we're just as well off doing this */ | |
142 | #define U8TO32_LE(ptr) (ptr[0]|ptr[1]<<8|ptr[2]<<16|ptr[3]<<24) | |
143 | #define UNALIGNED_SAFE | |
144 | #endif | |
145 | #else | |
146 | /* Unknown endianess so last resort is to read individual bytes */ | |
147 | #define U8TO32_LE(ptr) (ptr[0]|ptr[1]<<8|ptr[2]<<16|ptr[3]<<24) | |
148 | /* Since we're not doing word-reads we can skip the messing about with realignment */ | |
149 | #define UNALIGNED_SAFE | |
150 | #endif | |
151 | ||
b716320d YO |
152 | #ifdef HAS_QUAD |
153 | #ifndef U64TYPE | |
154 | /* This probably isn't going to work, but failing with a compiler error due to | |
155 | lack of uint64_t is no worse than failing right now with an #error. */ | |
702c92eb | 156 | #define U64 uint64_t |
b716320d YO |
157 | #endif |
158 | #endif | |
159 | ||
160 | /* Find best way to ROTL32/ROTL64 */ | |
4d3a042d YO |
161 | #if defined(_MSC_VER) |
162 | #include <stdlib.h> /* Microsoft put _rotl declaration in here */ | |
163 | #define ROTL32(x,r) _rotl(x,r) | |
b716320d YO |
164 | #ifdef HAS_QUAD |
165 | #define ROTL64(x,r) _rotl64(x,r) | |
166 | #endif | |
4d3a042d YO |
167 | #else |
168 | /* gcc recognises this code and generates a rotate instruction for CPUs with one */ | |
169 | #define ROTL32(x,r) (((U32)x << r) | ((U32)x >> (32 - r))) | |
b716320d | 170 | #ifdef HAS_QUAD |
702c92eb | 171 | #define ROTL64(x,r) (((U64)x << r) | ((U64)x >> (64 - r))) |
b716320d | 172 | #endif |
4d3a042d YO |
173 | #endif |
174 | ||
175 | ||
b716320d YO |
176 | #ifdef UV_IS_QUAD |
177 | #define ROTL_UV(x,r) ROTL64(x,r) | |
178 | #else | |
179 | #define ROTL_UV(x,r) ROTL32(x,r) | |
180 | #endif | |
181 | ||
4d3a042d YO |
182 | /* This is SipHash by Jean-Philippe Aumasson and Daniel J. Bernstein. |
183 | * The authors claim it is relatively secure compared to the alternatives | |
184 | * and that performance wise it is a suitable hash for languages like Perl. | |
185 | * See: | |
186 | * | |
187 | * https://www.131002.net/siphash/ | |
188 | * | |
189 | * This implementation seems to perform slightly slower than one-at-a-time for | |
190 | * short keys, but degrades slower for longer keys. Murmur Hash outperforms it | |
191 | * regardless of keys size. | |
192 | * | |
193 | * It is 64 bit only. | |
194 | */ | |
195 | ||
196 | #ifdef HAS_QUAD | |
197 | ||
4d3a042d | 198 | #define U8TO64_LE(p) \ |
702c92eb JH |
199 | (((U64)((p)[0]) ) | \ |
200 | ((U64)((p)[1]) << 8) | \ | |
201 | ((U64)((p)[2]) << 16) | \ | |
202 | ((U64)((p)[3]) << 24) | \ | |
203 | ((U64)((p)[4]) << 32) | \ | |
204 | ((U64)((p)[5]) << 40) | \ | |
205 | ((U64)((p)[6]) << 48) | \ | |
206 | ((U64)((p)[7]) << 56)) | |
4d3a042d YO |
207 | |
208 | #define SIPROUND \ | |
6b026047 | 209 | STMT_START { \ |
4d3a042d YO |
210 | v0 += v1; v1=ROTL64(v1,13); v1 ^= v0; v0=ROTL64(v0,32); \ |
211 | v2 += v3; v3=ROTL64(v3,16); v3 ^= v2; \ | |
212 | v0 += v3; v3=ROTL64(v3,21); v3 ^= v0; \ | |
213 | v2 += v1; v1=ROTL64(v1,17); v1 ^= v2; v2=ROTL64(v2,32); \ | |
6b026047 | 214 | } STMT_END |
4d3a042d YO |
215 | |
216 | /* SipHash-2-4 */ | |
217 | ||
6b026047 YO |
218 | |
219 | #define PERL_SIPHASH_FNC(FNC,SIP_ROUNDS,SIP_FINAL_ROUNDS) \ | |
220 | PERL_STATIC_INLINE U32 \ | |
221 | FNC(const unsigned char * const seed, const unsigned char *in, const STRLEN inlen) { \ | |
222 | /* "somepseudorandomlygeneratedbytes" */ \ | |
223 | U64 v0 = UINT64_C(0x736f6d6570736575); \ | |
224 | U64 v1 = UINT64_C(0x646f72616e646f6d); \ | |
225 | U64 v2 = UINT64_C(0x6c7967656e657261); \ | |
226 | U64 v3 = UINT64_C(0x7465646279746573); \ | |
227 | \ | |
228 | U64 b; \ | |
229 | U64 k0 = ((const U64*)seed)[0]; \ | |
230 | U64 k1 = ((const U64*)seed)[1]; \ | |
231 | U64 m; \ | |
232 | const int left = inlen & 7; \ | |
233 | const U8 *end = in + inlen - left; \ | |
234 | \ | |
235 | b = ( ( U64 )(inlen) ) << 56; \ | |
236 | v3 ^= k1; \ | |
237 | v2 ^= k0; \ | |
238 | v1 ^= k1; \ | |
239 | v0 ^= k0; \ | |
240 | \ | |
241 | for ( ; in != end; in += 8 ) \ | |
242 | { \ | |
243 | m = U8TO64_LE( in ); \ | |
244 | v3 ^= m; \ | |
245 | \ | |
246 | SIP_ROUNDS; \ | |
247 | \ | |
248 | v0 ^= m; \ | |
249 | } \ | |
250 | \ | |
251 | switch( left ) \ | |
252 | { \ | |
253 | case 7: b |= ( ( U64 )in[ 6] ) << 48; \ | |
254 | case 6: b |= ( ( U64 )in[ 5] ) << 40; \ | |
255 | case 5: b |= ( ( U64 )in[ 4] ) << 32; \ | |
256 | case 4: b |= ( ( U64 )in[ 3] ) << 24; \ | |
257 | case 3: b |= ( ( U64 )in[ 2] ) << 16; \ | |
258 | case 2: b |= ( ( U64 )in[ 1] ) << 8; \ | |
259 | case 1: b |= ( ( U64 )in[ 0] ); break; \ | |
260 | case 0: break; \ | |
261 | } \ | |
262 | \ | |
263 | v3 ^= b; \ | |
264 | \ | |
265 | SIP_ROUNDS; \ | |
266 | \ | |
267 | v0 ^= b; \ | |
268 | \ | |
269 | v2 ^= 0xff; \ | |
270 | \ | |
271 | SIP_FINAL_ROUNDS \ | |
272 | \ | |
273 | b = v0 ^ v1 ^ v2 ^ v3; \ | |
274 | return (U32)(b & U32_MAX); \ | |
4d3a042d | 275 | } |
6b026047 YO |
276 | |
277 | PERL_SIPHASH_FNC( | |
278 | S_perl_hash_siphash_1_3 | |
279 | ,SIPROUND; | |
280 | ,SIPROUND;SIPROUND;SIPROUND; | |
281 | ) | |
282 | ||
283 | PERL_SIPHASH_FNC( | |
284 | S_perl_hash_siphash_2_4 | |
285 | ,SIPROUND;SIPROUND; | |
286 | ,SIPROUND;SIPROUND;SIPROUND;SIPROUND; | |
287 | ) | |
288 | ||
4d3a042d YO |
289 | #endif /* defined(HAS_QUAD) */ |
290 | ||
6b026047 | 291 | |
4d3a042d YO |
292 | /* FYI: This is the "Super-Fast" algorithm mentioned by Bob Jenkins in |
293 | * (http://burtleburtle.net/bob/hash/doobs.html) | |
294 | * It is by Paul Hsieh (c) 2004 and is analysed here | |
295 | * http://www.azillionmonkeys.com/qed/hash.html | |
296 | * license terms are here: | |
297 | * http://www.azillionmonkeys.com/qed/weblicense.html | |
298 | */ | |
299 | ||
3efdcc9c | 300 | |
4d3a042d YO |
301 | PERL_STATIC_INLINE U32 |
302 | S_perl_hash_superfast(const unsigned char * const seed, const unsigned char *str, STRLEN len) { | |
463ddf34 | 303 | U32 hash = *((const U32*)seed) + (U32)len; |
4d3a042d YO |
304 | U32 tmp; |
305 | int rem= len & 3; | |
306 | len >>= 2; | |
307 | ||
308 | for (;len > 0; len--) { | |
309 | hash += U8TO16_LE (str); | |
310 | tmp = (U8TO16_LE (str+2) << 11) ^ hash; | |
311 | hash = (hash << 16) ^ tmp; | |
312 | str += 2 * sizeof (U16); | |
313 | hash += hash >> 11; | |
314 | } | |
315 | ||
316 | /* Handle end cases */ | |
317 | switch (rem) { \ | |
318 | case 3: hash += U8TO16_LE (str); | |
319 | hash ^= hash << 16; | |
320 | hash ^= str[sizeof (U16)] << 18; | |
321 | hash += hash >> 11; | |
322 | break; | |
323 | case 2: hash += U8TO16_LE (str); | |
324 | hash ^= hash << 11; | |
325 | hash += hash >> 17; | |
326 | break; | |
327 | case 1: hash += *str; | |
328 | hash ^= hash << 10; | |
329 | hash += hash >> 1; | |
330 | } | |
331 | /* Force "avalanching" of final 127 bits */ | |
332 | hash ^= hash << 3; | |
333 | hash += hash >> 5; | |
334 | hash ^= hash << 4; | |
335 | hash += hash >> 17; | |
336 | hash ^= hash << 25; | |
337 | return (hash + (hash >> 6)); | |
338 | } | |
3efdcc9c | 339 | |
4d3a042d YO |
340 | |
341 | /*----------------------------------------------------------------------------- | |
342 | * MurmurHash3 was written by Austin Appleby, and is placed in the public | |
343 | * domain. | |
344 | * | |
345 | * This implementation was originally written by Shane Day, and is also public domain, | |
346 | * and was modified to function as a macro similar to other perl hash functions by | |
347 | * Yves Orton. | |
348 | * | |
349 | * This is a portable ANSI C implementation of MurmurHash3_x86_32 (Murmur3A) | |
350 | * with support for progressive processing. | |
351 | * | |
352 | * If you want to understand the MurmurHash algorithm you would be much better | |
353 | * off reading the original source. Just point your browser at: | |
354 | * http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp | |
355 | * | |
356 | * How does it work? | |
357 | * | |
358 | * We can only process entire 32 bit chunks of input, except for the very end | |
359 | * that may be shorter. | |
360 | * | |
361 | * To handle endianess I simply use a macro that reads a U32 and define | |
362 | * that macro to be a direct read on little endian machines, a read and swap | |
363 | * on big endian machines, or a byte-by-byte read if the endianess is unknown. | |
364 | */ | |
365 | ||
3efdcc9c | 366 | |
4d3a042d YO |
367 | /*----------------------------------------------------------------------------- |
368 | * Core murmurhash algorithm macros */ | |
369 | ||
370 | #define MURMUR_C1 (0xcc9e2d51) | |
371 | #define MURMUR_C2 (0x1b873593) | |
372 | #define MURMUR_C3 (0xe6546b64) | |
373 | #define MURMUR_C4 (0x85ebca6b) | |
374 | #define MURMUR_C5 (0xc2b2ae35) | |
375 | ||
376 | /* This is the main processing body of the algorithm. It operates | |
377 | * on each full 32-bits of input. */ | |
378 | #define MURMUR_DOBLOCK(h1, k1) STMT_START { \ | |
379 | k1 *= MURMUR_C1; \ | |
380 | k1 = ROTL32(k1,15); \ | |
381 | k1 *= MURMUR_C2; \ | |
382 | \ | |
383 | h1 ^= k1; \ | |
384 | h1 = ROTL32(h1,13); \ | |
385 | h1 = h1 * 5 + MURMUR_C3; \ | |
386 | } STMT_END | |
387 | ||
388 | ||
389 | /* Append unaligned bytes to carry, forcing hash churn if we have 4 bytes */ | |
390 | /* cnt=bytes to process, h1=name of h1 var, c=carry, n=bytes in c, ptr/len=payload */ | |
391 | #define MURMUR_DOBYTES(cnt, h1, c, n, ptr, len) STMT_START { \ | |
392 | int MURMUR_DOBYTES_i = cnt; \ | |
393 | while(MURMUR_DOBYTES_i--) { \ | |
394 | c = c>>8 | *ptr++<<24; \ | |
395 | n++; len--; \ | |
396 | if(n==4) { \ | |
397 | MURMUR_DOBLOCK(h1, c); \ | |
398 | n = 0; \ | |
399 | } \ | |
400 | } \ | |
401 | } STMT_END | |
402 | ||
403 | ||
404 | /* now we create the hash function */ | |
405 | PERL_STATIC_INLINE U32 | |
406 | S_perl_hash_murmur3(const unsigned char * const seed, const unsigned char *ptr, STRLEN len) { | |
463ddf34 | 407 | U32 h1 = *((const U32*)seed); |
4d3a042d YO |
408 | U32 k1; |
409 | U32 carry = 0; | |
410 | ||
411 | const unsigned char *end; | |
412 | int bytes_in_carry = 0; /* bytes in carry */ | |
b4045391 | 413 | I32 total_length= (I32)len; |
4d3a042d YO |
414 | |
415 | #if defined(UNALIGNED_SAFE) | |
416 | /* Handle carry: commented out as its only used in incremental mode - it never fires for us | |
417 | int i = (4-n) & 3; | |
418 | if(i && i <= len) { | |
419 | MURMUR_DOBYTES(i, h1, carry, bytes_in_carry, ptr, len); | |
420 | } | |
421 | */ | |
422 | ||
423 | /* This CPU handles unaligned word access */ | |
424 | /* Process 32-bit chunks */ | |
425 | end = ptr + len/4*4; | |
426 | for( ; ptr < end ; ptr+=4) { | |
427 | k1 = U8TO32_LE(ptr); | |
428 | MURMUR_DOBLOCK(h1, k1); | |
429 | } | |
430 | #else | |
431 | /* This CPU does not handle unaligned word access */ | |
432 | ||
433 | /* Consume enough so that the next data byte is word aligned */ | |
c8523631 RB |
434 | STRLEN i = -PTR2IV(ptr) & 3; |
435 | if(i && i <= len) { | |
b4045391 | 436 | MURMUR_DOBYTES((int)i, h1, carry, bytes_in_carry, ptr, len); |
4d3a042d YO |
437 | } |
438 | ||
439 | /* We're now aligned. Process in aligned blocks. Specialise for each possible carry count */ | |
440 | end = ptr + len/4*4; | |
441 | switch(bytes_in_carry) { /* how many bytes in carry */ | |
442 | case 0: /* c=[----] w=[3210] b=[3210]=w c'=[----] */ | |
443 | for( ; ptr < end ; ptr+=4) { | |
444 | k1 = U8TO32_LE(ptr); | |
445 | MURMUR_DOBLOCK(h1, k1); | |
446 | } | |
447 | break; | |
448 | case 1: /* c=[0---] w=[4321] b=[3210]=c>>24|w<<8 c'=[4---] */ | |
449 | for( ; ptr < end ; ptr+=4) { | |
450 | k1 = carry>>24; | |
451 | carry = U8TO32_LE(ptr); | |
452 | k1 |= carry<<8; | |
453 | MURMUR_DOBLOCK(h1, k1); | |
454 | } | |
455 | break; | |
456 | case 2: /* c=[10--] w=[5432] b=[3210]=c>>16|w<<16 c'=[54--] */ | |
457 | for( ; ptr < end ; ptr+=4) { | |
458 | k1 = carry>>16; | |
459 | carry = U8TO32_LE(ptr); | |
460 | k1 |= carry<<16; | |
461 | MURMUR_DOBLOCK(h1, k1); | |
462 | } | |
463 | break; | |
464 | case 3: /* c=[210-] w=[6543] b=[3210]=c>>8|w<<24 c'=[654-] */ | |
465 | for( ; ptr < end ; ptr+=4) { | |
466 | k1 = carry>>8; | |
467 | carry = U8TO32_LE(ptr); | |
468 | k1 |= carry<<24; | |
469 | MURMUR_DOBLOCK(h1, k1); | |
470 | } | |
471 | } | |
472 | #endif | |
473 | /* Advance over whole 32-bit chunks, possibly leaving 1..3 bytes */ | |
474 | len -= len/4*4; | |
475 | ||
476 | /* Append any remaining bytes into carry */ | |
b4045391 | 477 | MURMUR_DOBYTES((int)len, h1, carry, bytes_in_carry, ptr, len); |
4d3a042d YO |
478 | |
479 | if (bytes_in_carry) { | |
480 | k1 = carry >> ( 4 - bytes_in_carry ) * 8; | |
481 | k1 *= MURMUR_C1; | |
482 | k1 = ROTL32(k1,15); | |
483 | k1 *= MURMUR_C2; | |
484 | h1 ^= k1; | |
485 | } | |
486 | h1 ^= total_length; | |
487 | ||
488 | /* fmix */ | |
489 | h1 ^= h1 >> 16; | |
490 | h1 *= MURMUR_C4; | |
491 | h1 ^= h1 >> 13; | |
492 | h1 *= MURMUR_C5; | |
493 | h1 ^= h1 >> 16; | |
494 | return h1; | |
495 | } | |
496 | ||
3efdcc9c | 497 | |
4d3a042d YO |
498 | PERL_STATIC_INLINE U32 |
499 | S_perl_hash_djb2(const unsigned char * const seed, const unsigned char *str, const STRLEN len) { | |
500 | const unsigned char * const end = (const unsigned char *)str + len; | |
463ddf34 | 501 | U32 hash = *((const U32*)seed) + (U32)len; |
4d3a042d YO |
502 | while (str < end) { |
503 | hash = ((hash << 5) + hash) + *str++; | |
504 | } | |
505 | return hash; | |
506 | } | |
507 | ||
508 | PERL_STATIC_INLINE U32 | |
509 | S_perl_hash_sdbm(const unsigned char * const seed, const unsigned char *str, const STRLEN len) { | |
510 | const unsigned char * const end = (const unsigned char *)str + len; | |
463ddf34 | 511 | U32 hash = *((const U32*)seed) + (U32)len; |
4d3a042d YO |
512 | while (str < end) { |
513 | hash = (hash << 6) + (hash << 16) - hash + *str++; | |
514 | } | |
515 | return hash; | |
516 | } | |
517 | ||
fab3d107 DD |
518 | /* - ONE_AT_A_TIME_HARD is the 5.17+ recommend ONE_AT_A_TIME algorithm |
519 | * - ONE_AT_A_TIME_OLD is the unmodified 5.16 and older algorithm | |
520 | * - ONE_AT_A_TIME is a 5.17+ tweak of ONE_AT_A_TIME_OLD to | |
521 | * prevent strings of only \0 but different lengths from colliding | |
522 | * | |
523 | * Security-wise, from best to worst, | |
524 | * ONE_AT_A_TIME_HARD > ONE_AT_A_TIME > ONE_AT_A_TIME_OLD | |
525 | * There is a big drop-off in security between ONE_AT_A_TIME_HARD and | |
526 | * ONE_AT_A_TIME | |
527 | * */ | |
4d3a042d | 528 | |
b1300a73 | 529 | /* This is the "One-at-a-Time" algorithm by Bob Jenkins |
4d3a042d | 530 | * from requirements by Colin Plumb. |
b1300a73 YO |
531 | * (http://burtleburtle.net/bob/hash/doobs.html) |
532 | * With seed/len tweak. | |
533 | * */ | |
4d3a042d YO |
534 | PERL_STATIC_INLINE U32 |
535 | S_perl_hash_one_at_a_time(const unsigned char * const seed, const unsigned char *str, const STRLEN len) { | |
536 | const unsigned char * const end = (const unsigned char *)str + len; | |
463ddf34 | 537 | U32 hash = *((const U32*)seed) + (U32)len; |
4d3a042d YO |
538 | while (str < end) { |
539 | hash += *str++; | |
540 | hash += (hash << 10); | |
541 | hash ^= (hash >> 6); | |
542 | } | |
543 | hash += (hash << 3); | |
544 | hash ^= (hash >> 11); | |
545 | return (hash + (hash << 15)); | |
546 | } | |
547 | ||
b1300a73 YO |
548 | /* Derived from "One-at-a-Time" algorithm by Bob Jenkins */ |
549 | PERL_STATIC_INLINE U32 | |
550 | S_perl_hash_one_at_a_time_hard(const unsigned char * const seed, const unsigned char *str, const STRLEN len) { | |
551 | const unsigned char * const end = (const unsigned char *)str + len; | |
463ddf34 | 552 | U32 hash = *((const U32*)seed) + (U32)len; |
b1300a73 YO |
553 | |
554 | while (str < end) { | |
555 | hash += (hash << 10); | |
556 | hash ^= (hash >> 6); | |
557 | hash += *str++; | |
558 | } | |
559 | ||
560 | hash += (hash << 10); | |
561 | hash ^= (hash >> 6); | |
562 | hash += seed[4]; | |
563 | ||
564 | hash += (hash << 10); | |
565 | hash ^= (hash >> 6); | |
566 | hash += seed[5]; | |
567 | ||
568 | hash += (hash << 10); | |
569 | hash ^= (hash >> 6); | |
570 | hash += seed[6]; | |
571 | ||
572 | hash += (hash << 10); | |
573 | hash ^= (hash >> 6); | |
574 | hash += seed[7]; | |
575 | ||
576 | hash += (hash << 10); | |
577 | hash ^= (hash >> 6); | |
578 | ||
579 | hash += (hash << 3); | |
580 | hash ^= (hash >> 11); | |
581 | return (hash + (hash << 15)); | |
582 | } | |
583 | ||
6b026047 YO |
584 | #ifdef HAS_QUAD |
585 | /* For short strings, 16 bytes or shorter, we use an optimised variant | |
586 | * of One At A Time Hard, and for longer strings, we use siphash_1_3. | |
587 | */ | |
588 | PERL_STATIC_INLINE U32 | |
589 | S_perl_hash_oaathu_siphash_1_3(const unsigned char * const seed, const unsigned char *str, const STRLEN len) { | |
590 | U32 hash = *((const U32*)seed) + (U32)len; | |
591 | switch (len) { | |
592 | case 16: | |
593 | hash += (hash << 10); | |
594 | hash ^= (hash >> 6); | |
595 | hash += str[15]; | |
596 | case 15: | |
597 | hash += (hash << 10); | |
598 | hash ^= (hash >> 6); | |
599 | hash += str[14]; | |
600 | case 14: | |
601 | hash += (hash << 10); | |
602 | hash ^= (hash >> 6); | |
603 | hash += str[13]; | |
604 | case 13: | |
605 | hash += (hash << 10); | |
606 | hash ^= (hash >> 6); | |
607 | hash += str[12]; | |
608 | case 12: | |
609 | hash += (hash << 10); | |
610 | hash ^= (hash >> 6); | |
611 | hash += str[11]; | |
612 | case 11: | |
613 | hash += (hash << 10); | |
614 | hash ^= (hash >> 6); | |
615 | hash += str[10]; | |
616 | case 10: | |
617 | hash += (hash << 10); | |
618 | hash ^= (hash >> 6); | |
619 | hash += str[9]; | |
620 | case 9: | |
621 | hash += (hash << 10); | |
622 | hash ^= (hash >> 6); | |
623 | hash += str[8]; | |
624 | case 8: | |
625 | hash += (hash << 10); | |
626 | hash ^= (hash >> 6); | |
627 | hash += str[7]; | |
628 | case 7: | |
629 | hash += (hash << 10); | |
630 | hash ^= (hash >> 6); | |
631 | hash += str[6]; | |
632 | case 6: | |
633 | hash += (hash << 10); | |
634 | hash ^= (hash >> 6); | |
635 | hash += str[5]; | |
636 | case 5: | |
637 | hash += (hash << 10); | |
638 | hash ^= (hash >> 6); | |
639 | hash += str[4]; | |
640 | case 4: | |
641 | hash += (hash << 10); | |
642 | hash ^= (hash >> 6); | |
643 | hash += str[3]; | |
644 | case 3: | |
645 | hash += (hash << 10); | |
646 | hash ^= (hash >> 6); | |
647 | hash += str[2]; | |
648 | case 2: | |
649 | hash += (hash << 10); | |
650 | hash ^= (hash >> 6); | |
651 | hash += str[1]; | |
652 | case 1: | |
653 | hash += (hash << 10); | |
654 | hash ^= (hash >> 6); | |
655 | hash += str[0]; | |
656 | case 0: | |
657 | hash += (hash << 10); | |
658 | hash ^= (hash >> 6); | |
659 | hash += seed[4]; | |
660 | hash += (hash << 10); | |
661 | hash ^= (hash >> 6); | |
662 | hash += seed[5]; | |
663 | hash += (hash << 10); | |
664 | hash ^= (hash >> 6); | |
665 | hash += seed[6]; | |
666 | hash += (hash << 10); | |
667 | hash ^= (hash >> 6); | |
668 | hash += seed[7]; | |
669 | hash += (hash << 10); | |
670 | hash ^= (hash >> 6); | |
671 | ||
672 | hash += (hash << 3); | |
673 | hash ^= (hash >> 11); | |
674 | return (hash + (hash << 15)); | |
675 | } | |
676 | return S_perl_hash_siphash_1_3(seed+8, str, len); | |
677 | } | |
678 | #endif /* defined(HAS_QUAD) */ | |
679 | ||
4d3a042d YO |
680 | PERL_STATIC_INLINE U32 |
681 | S_perl_hash_old_one_at_a_time(const unsigned char * const seed, const unsigned char *str, const STRLEN len) { | |
682 | const unsigned char * const end = (const unsigned char *)str + len; | |
463ddf34 | 683 | U32 hash = *((const U32*)seed); |
4d3a042d YO |
684 | while (str < end) { |
685 | hash += *str++; | |
686 | hash += (hash << 10); | |
687 | hash ^= (hash >> 6); | |
688 | } | |
689 | hash += (hash << 3); | |
690 | hash ^= (hash >> 11); | |
691 | return (hash + (hash << 15)); | |
692 | } | |
693 | ||
7e0dd61b YO |
694 | #ifdef PERL_HASH_FUNC_MURMUR_HASH_64A |
695 | /* This code is from Austin Appleby and is in the public domain. | |
696 | Altered by Yves Orton to match Perl's hash interface, and to | |
697 | return a 32 bit hash. | |
698 | ||
699 | Note uses unaligned 64 bit loads - will NOT work on machines with | |
21ac16fe | 700 | strict alignment requirements. |
7e0dd61b YO |
701 | |
702 | Also this code may not be suitable for big-endian machines. | |
703 | */ | |
704 | ||
705 | /* a 64 bit hash where we only use the low 32 bits */ | |
706 | PERL_STATIC_INLINE U32 | |
707 | S_perl_hash_murmur_hash_64a (const unsigned char * const seed, const unsigned char *str, const STRLEN len) | |
708 | { | |
702c92eb | 709 | const U64 m = UINT64_C(0xc6a4a7935bd1e995); |
7e0dd61b | 710 | const int r = 47; |
463ddf34 | 711 | U64 h = *((const U64*)seed) ^ len; |
702c92eb JH |
712 | const U64 * data = (const U64 *)str; |
713 | const U64 * end = data + (len/8); | |
7e0dd61b YO |
714 | const unsigned char * data2; |
715 | ||
716 | while(data != end) | |
717 | { | |
702c92eb | 718 | U64 k = *data++; |
7e0dd61b YO |
719 | |
720 | k *= m; | |
721 | k ^= k >> r; | |
722 | k *= m; | |
723 | ||
724 | h ^= k; | |
725 | h *= m; | |
726 | } | |
727 | ||
728 | data2 = (const unsigned char *)data; | |
729 | ||
730 | switch(len & 7) | |
731 | { | |
702c92eb JH |
732 | case 7: h ^= (U64)(data2[6]) << 48; /* fallthrough */ |
733 | case 6: h ^= (U64)(data2[5]) << 40; /* fallthrough */ | |
734 | case 5: h ^= (U64)(data2[4]) << 32; /* fallthrough */ | |
735 | case 4: h ^= (U64)(data2[3]) << 24; /* fallthrough */ | |
736 | case 3: h ^= (U64)(data2[2]) << 16; /* fallthrough */ | |
737 | case 2: h ^= (U64)(data2[1]) << 8; /* fallthrough */ | |
738 | case 1: h ^= (U64)(data2[0]); /* fallthrough */ | |
7e0dd61b YO |
739 | h *= m; |
740 | }; | |
741 | ||
742 | h ^= h >> r; | |
743 | h *= m; | |
744 | h ^= h >> r; | |
745 | ||
746 | /* was: return h; */ | |
747 | return h & 0xFFFFFFFF; | |
748 | } | |
749 | ||
750 | #endif | |
751 | ||
752 | #ifdef PERL_HASH_FUNC_MURMUR_HASH_64B | |
753 | /* This code is from Austin Appleby and is in the public domain. | |
754 | Altered by Yves Orton to match Perl's hash interface and return | |
755 | a 32 bit value | |
756 | ||
757 | Note uses unaligned 32 bit loads - will NOT work on machines with | |
21ac16fe | 758 | strict alignment requirements. |
7e0dd61b YO |
759 | |
760 | Also this code may not be suitable for big-endian machines. | |
761 | */ | |
762 | ||
763 | /* a 64-bit hash for 32-bit platforms where we only use the low 32 bits */ | |
764 | PERL_STATIC_INLINE U32 | |
765 | S_perl_hash_murmur_hash_64b (const unsigned char * const seed, const unsigned char *str, STRLEN len) | |
766 | { | |
767 | const U32 m = 0x5bd1e995; | |
768 | const int r = 24; | |
769 | ||
770 | U32 h1 = ((U32 *)seed)[0] ^ len; | |
771 | U32 h2 = ((U32 *)seed)[1]; | |
772 | ||
773 | const U32 * data = (const U32 *)str; | |
774 | ||
775 | while(len >= 8) | |
776 | { | |
777 | U32 k1, k2; | |
778 | k1 = *data++; | |
779 | k1 *= m; k1 ^= k1 >> r; k1 *= m; | |
780 | h1 *= m; h1 ^= k1; | |
781 | len -= 4; | |
782 | ||
783 | k2 = *data++; | |
784 | k2 *= m; k2 ^= k2 >> r; k2 *= m; | |
785 | h2 *= m; h2 ^= k2; | |
786 | len -= 4; | |
787 | } | |
788 | ||
789 | if(len >= 4) | |
790 | { | |
791 | U32 k1 = *data++; | |
792 | k1 *= m; k1 ^= k1 >> r; k1 *= m; | |
793 | h1 *= m; h1 ^= k1; | |
794 | len -= 4; | |
795 | } | |
796 | ||
797 | switch(len) | |
798 | { | |
799 | case 3: h2 ^= ((unsigned char*)data)[2] << 16; /* fallthrough */ | |
800 | case 2: h2 ^= ((unsigned char*)data)[1] << 8; /* fallthrough */ | |
801 | case 1: h2 ^= ((unsigned char*)data)[0]; /* fallthrough */ | |
802 | h2 *= m; | |
803 | }; | |
804 | ||
805 | h1 ^= h2 >> 18; h1 *= m; | |
806 | h2 ^= h1 >> 22; h2 *= m; | |
807 | /* | |
808 | The following code has been removed as it is unused | |
809 | when only the low 32 bits are used. -- Yves | |
810 | ||
811 | h1 ^= h2 >> 17; h1 *= m; | |
812 | ||
702c92eb | 813 | U64 h = h1; |
7e0dd61b YO |
814 | |
815 | h = (h << 32) | h2; | |
816 | */ | |
817 | ||
818 | return h2; | |
819 | } | |
820 | #endif | |
821 | ||
6b026047 | 822 | |
4d3a042d YO |
823 | /* legacy - only mod_perl should be doing this. */ |
824 | #ifdef PERL_HASH_INTERNAL_ACCESS | |
825 | #define PERL_HASH_INTERNAL(hash,str,len) PERL_HASH(hash,str,len) | |
826 | #endif | |
827 | ||
828 | #endif /*compile once*/ | |
829 | ||
830 | /* | |
4d3a042d YO |
831 | * ex: set ts=8 sts=4 sw=4 et: |
832 | */ |