| 1 |
#ifndef Py_INTERNAL_PYHASH_H |
| 2 |
#define Py_INTERNAL_PYHASH_H |
| 3 |
|
| 4 |
#ifndef Py_BUILD_CORE |
| 5 |
# error "this header requires Py_BUILD_CORE define" |
| 6 |
#endif |
| 7 |
|
| 8 |
// Similar to Py_HashPointer(), but don't replace -1 with -2. |
| 9 |
static inline Py_hash_t |
| 10 |
_Py_HashPointerRaw(const void *ptr) |
| 11 |
{ |
| 12 |
uintptr_t x = (uintptr_t)ptr; |
| 13 |
Py_BUILD_ASSERT(sizeof(x) == sizeof(ptr)); |
| 14 |
|
| 15 |
// Bottom 3 or 4 bits are likely to be 0; rotate x by 4 to the right |
| 16 |
// to avoid excessive hash collisions for dicts and sets. |
| 17 |
x = (x >> 4) | (x << (8 * sizeof(uintptr_t) - 4)); |
| 18 |
|
| 19 |
Py_BUILD_ASSERT(sizeof(x) == sizeof(Py_hash_t)); |
| 20 |
return (Py_hash_t)x; |
| 21 |
} |
| 22 |
|
| 23 |
/* Hash secret |
| 24 |
* |
| 25 |
* memory layout on 64 bit systems |
| 26 |
* cccccccc cccccccc cccccccc uc -- unsigned char[24] |
| 27 |
* pppppppp ssssssss ........ fnv -- two Py_hash_t |
| 28 |
* k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t |
| 29 |
* ........ ........ ssssssss djbx33a -- 16 bytes padding + one Py_hash_t |
| 30 |
* eeeeeeee eeeeeeee eeeeeeee pyexpat XML hash salt |
| 31 |
* |
| 32 |
* memory layout on 32 bit systems |
| 33 |
* cccccccc cccccccc cccccccc uc |
| 34 |
* ppppssss ........ ........ fnv -- two Py_hash_t |
| 35 |
* k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t (*) |
| 36 |
* ........ ........ ssss.... djbx33a -- 16 bytes padding + one Py_hash_t |
| 37 |
* eeeeeeee eeeeeeee eeee.... pyexpat XML hash salt |
| 38 |
* |
| 39 |
* (*) The siphash member may not be available on 32 bit platforms without |
| 40 |
* an unsigned int64 data type. |
| 41 |
*/ |
| 42 |
typedef union { |
| 43 |
/* ensure 24 bytes */ |
| 44 |
unsigned char uc[24]; |
| 45 |
/* two Py_hash_t for FNV */ |
| 46 |
struct { |
| 47 |
Py_hash_t prefix; |
| 48 |
Py_hash_t suffix; |
| 49 |
} fnv; |
| 50 |
/* two uint64 for SipHash24 */ |
| 51 |
struct { |
| 52 |
uint64_t k0; |
| 53 |
uint64_t k1; |
| 54 |
} siphash; |
| 55 |
/* a different (!) Py_hash_t for small string optimization */ |
| 56 |
struct { |
| 57 |
unsigned char padding[16]; |
| 58 |
Py_hash_t suffix; |
| 59 |
} djbx33a; |
| 60 |
struct { |
| 61 |
/* 16 bytes for XML_SetHashSalt16Bytes */ |
| 62 |
uint8_t hashsalt16[16]; |
| 63 |
/* 4/8 bytes for legacy XML_SetHashSalt */ |
| 64 |
Py_hash_t hashsalt; |
| 65 |
} expat; |
| 66 |
} _Py_HashSecret_t; |
| 67 |
|
| 68 |
// Export for '_elementtree' shared extension |
| 69 |
PyAPI_DATA(_Py_HashSecret_t) _Py_HashSecret; |
| 70 |
|
| 71 |
#ifdef Py_DEBUG |
| 72 |
extern int _Py_HashSecret_Initialized; |
| 73 |
#endif |
| 74 |
|
| 75 |
|
| 76 |
#ifndef MS_WINDOWS |
| 77 |
# define _py_urandom_cache_INIT \ |
| 78 |
{ \ |
| 79 |
.fd = -1, \ |
| 80 |
} |
| 81 |
#else |
| 82 |
# define _py_urandom_cache_INIT {0} |
| 83 |
#endif |
| 84 |
|
| 85 |
#define pyhash_state_INIT \ |
| 86 |
{ \ |
| 87 |
.urandom_cache = _py_urandom_cache_INIT, \ |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
extern uint64_t _Py_KeyedHash(uint64_t key, const void *src, Py_ssize_t src_sz); |
| 92 |
|
| 93 |
#endif // !Py_INTERNAL_PYHASH_H |