11#ifndef Py_HASH_H
2-
32#define Py_HASH_H
43#ifdef __cplusplus
54extern "C" {
65#endif
76
8- /* Helpers for hash functions */
97#ifndef Py_LIMITED_API
10- PyAPI_FUNC (Py_hash_t ) _Py_HashDouble (PyObject * , double );
11- PyAPI_FUNC (Py_hash_t ) _Py_HashPointer (const void * );
12- // Similar to _Py_HashPointer(), but don't replace -1 with -2
13- PyAPI_FUNC (Py_hash_t ) _Py_HashPointerRaw (const void * );
14- PyAPI_FUNC (Py_hash_t ) _Py_HashBytes (const void * , Py_ssize_t );
15- #endif
16-
17- /* Prime multiplier used in string and various other hashes. */
18- #define _PyHASH_MULTIPLIER 1000003UL /* 0xf4243 */
19-
20- /* Parameters used for the numeric hash implementation. See notes for
21- _Py_HashDouble in Python/pyhash.c. Numeric hashes are based on
22- reduction modulo the prime 2**_PyHASH_BITS - 1. */
23-
24- #if SIZEOF_VOID_P >= 8
25- # define _PyHASH_BITS 61
26- #else
27- # define _PyHASH_BITS 31
28- #endif
29-
30- #define _PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
31- #define _PyHASH_INF 314159
32- #define _PyHASH_IMAG _PyHASH_MULTIPLIER
33-
34-
35- /* hash secret
36- *
37- * memory layout on 64 bit systems
38- * cccccccc cccccccc cccccccc uc -- unsigned char[24]
39- * pppppppp ssssssss ........ fnv -- two Py_hash_t
40- * k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t
41- * ........ ........ ssssssss djbx33a -- 16 bytes padding + one Py_hash_t
42- * ........ ........ eeeeeeee pyexpat XML hash salt
43- *
44- * memory layout on 32 bit systems
45- * cccccccc cccccccc cccccccc uc
46- * ppppssss ........ ........ fnv -- two Py_hash_t
47- * k0k0k0k0 k1k1k1k1 ........ siphash -- two uint64_t (*)
48- * ........ ........ ssss.... djbx33a -- 16 bytes padding + one Py_hash_t
49- * ........ ........ eeee.... pyexpat XML hash salt
50- *
51- * (*) The siphash member may not be available on 32 bit platforms without
52- * an unsigned int64 data type.
53- */
54- #ifndef Py_LIMITED_API
55- typedef union {
56- /* ensure 24 bytes */
57- unsigned char uc [24 ];
58- /* two Py_hash_t for FNV */
59- struct {
60- Py_hash_t prefix ;
61- Py_hash_t suffix ;
62- } fnv ;
63- /* two uint64 for SipHash24 */
64- struct {
65- uint64_t k0 ;
66- uint64_t k1 ;
67- } siphash ;
68- /* a different (!) Py_hash_t for small string optimization */
69- struct {
70- unsigned char padding [16 ];
71- Py_hash_t suffix ;
72- } djbx33a ;
73- struct {
74- unsigned char padding [16 ];
75- Py_hash_t hashsalt ;
76- } expat ;
77- } _Py_HashSecret_t ;
78- PyAPI_DATA (_Py_HashSecret_t ) _Py_HashSecret ;
79-
80- #ifdef Py_DEBUG
81- PyAPI_DATA (int ) _Py_HashSecret_Initialized ;
82- #endif
83-
84-
858/* hash function definition */
869typedef struct {
8710 Py_hash_t (* const hash )(const void * , Py_ssize_t );
@@ -94,7 +17,7 @@ PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);
9417#endif
9518
9619
97- /* cutoff for small string DJBX33A optimization in range [1, cutoff).
20+ /* Cutoff for small string DJBX33A optimization in range [1, cutoff).
9821 *
9922 * About 50% of the strings in a typical Python application are smaller than
10023 * 6 to 7 chars. However DJBX33A is vulnerable to hash collision attacks.
@@ -112,7 +35,7 @@ PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);
11235#endif /* Py_HASH_CUTOFF */
11336
11437
115- /* hash algorithm selection
38+ /* Hash algorithm selection
11639 *
11740 * The values for Py_HASH_* are hard-coded in the
11841 * configure script.
@@ -140,5 +63,4 @@ PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);
14063#ifdef __cplusplus
14164}
14265#endif
143-
144- #endif /* !Py_HASH_H */
66+ #endif // !Py_HASH_H
0 commit comments