This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Various updates and fixes to some of the SysV IPC ops and their tests
[perl5.git] / hv_func.h
CommitLineData
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
4d3a042d
YO
7 * (see also perl.c:perl_parse() and S_init_tls_and_interp() and util.c:get_hash_seed())
8 */
b41deab9
KW
9#ifndef PERL_SEEN_HV_FUNC_H_ /* compile once */
10#define PERL_SEEN_HV_FUNC_H_
1c4b2386 11#include "hv_macro.h"
9d5e3f1a
YO
12
13#if !( 0 \
14 || defined(PERL_HASH_FUNC_SIPHASH) \
15 || defined(PERL_HASH_FUNC_SIPHASH13) \
16 || defined(PERL_HASH_FUNC_STADTX) \
17 || defined(PERL_HASH_FUNC_ZAPHOD32) \
18 )
19# ifdef CAN64BITHASH
20# define PERL_HASH_FUNC_STADTX
21# else
22# define PERL_HASH_FUNC_ZAPHOD32
23# endif
24#endif
25
26#ifndef PERL_HASH_USE_SBOX32_ALSO
27#define PERL_HASH_USE_SBOX32_ALSO 1
28#endif
29
30#ifndef SBOX32_MAX_LEN
31#define SBOX32_MAX_LEN 24
32#endif
33
34/* this must be after the SBOX32_MAX_LEN define */
35#include "sbox32_hash.h"
36
37#if defined(PERL_HASH_FUNC_SIPHASH)
38# define __PERL_HASH_FUNC "SIPHASH_2_4"
39# define __PERL_HASH_SEED_BYTES 16
40# define __PERL_HASH_STATE_BYTES 32
41# define __PERL_HASH_SEED_STATE(seed,state) S_perl_siphash_seed_state(seed,state)
42# define __PERL_HASH_WITH_STATE(state,str,len) S_perl_hash_siphash_2_4_with_state((state),(U8*)(str),(len))
43#elif defined(PERL_HASH_FUNC_SIPHASH13)
44# define __PERL_HASH_FUNC "SIPHASH_1_3"
45# define __PERL_HASH_SEED_BYTES 16
46# define __PERL_HASH_STATE_BYTES 32
47# define __PERL_HASH_SEED_STATE(seed,state) S_perl_siphash_seed_state(seed,state)
48# define __PERL_HASH_WITH_STATE(state,str,len) S_perl_hash_siphash_1_3_with_state((state),(U8*)(str),(len))
49#elif defined(PERL_HASH_FUNC_STADTX)
a19348e7 50# define __PERL_HASH_FUNC "STADTX"
9d5e3f1a
YO
51# define __PERL_HASH_SEED_BYTES 16
52# define __PERL_HASH_STATE_BYTES 32
53# define __PERL_HASH_SEED_STATE(seed,state) stadtx_seed_state(seed,state)
54# define __PERL_HASH_WITH_STATE(state,str,len) (U32)stadtx_hash_with_state((state),(U8*)(str),(len))
55# include "stadtx_hash.h"
56#elif defined(PERL_HASH_FUNC_ZAPHOD32)
57# define __PERL_HASH_FUNC "ZAPHOD32"
58# define __PERL_HASH_SEED_BYTES 12
59# define __PERL_HASH_STATE_BYTES 12
60# define __PERL_HASH_SEED_STATE(seed,state) zaphod32_seed_state(seed,state)
61# define __PERL_HASH_WITH_STATE(state,str,len) (U32)zaphod32_hash_with_state((state),(U8*)(str),(len))
62# include "zaphod32_hash.h"
63#endif
64
65#ifndef __PERL_HASH_WITH_STATE
66#error "No hash function defined!"
67#endif
68#ifndef __PERL_HASH_SEED_BYTES
69#error "__PERL_HASH_SEED_BYTES not defined"
70#endif
71#ifndef __PERL_HASH_FUNC
72#error "__PERL_HASH_FUNC not defined"
73#endif
74
1af90cb1
TC
75/* Some siphash static functions are needed by XS::APItest even when
76 siphash isn't the current hash. For SipHash builds this needs to
77 be before the S_perl_hash_with_seed() definition.
78*/
79#include "perl_siphash.h"
9d5e3f1a 80
f0df1bde 81#if PERL_HASH_USE_SBOX32_ALSO != 1
9d5e3f1a
YO
82# define _PERL_HASH_FUNC __PERL_HASH_FUNC
83# define _PERL_HASH_SEED_BYTES __PERL_HASH_SEED_BYTES
84# define _PERL_HASH_STATE_BYTES __PERL_HASH_STATE_BYTES
85# define _PERL_HASH_SEED_STATE(seed,state) __PERL_HASH_SEED_STATE(seed,state)
86# define _PERL_HASH_WITH_STATE(state,str,len) __PERL_HASH_WITH_STATE(state,str,len)
87#else
88
89#define _PERL_HASH_FUNC "SBOX32_WITH_" __PERL_HASH_FUNC
90
290f44ea 91#define _PERL_HASH_SEED_BYTES ( __PERL_HASH_SEED_BYTES + (int)( 3 * sizeof(U32) ) )
9d5e3f1a
YO
92
93#define _PERL_HASH_STATE_BYTES \
f0df1bde 94 ( __PERL_HASH_STATE_BYTES + ( ( 1 + ( 256 * SBOX32_MAX_LEN ) ) * sizeof(U32) ) )
9d5e3f1a
YO
95
96#define _PERL_HASH_SEED_STATE(seed,state) STMT_START { \
97 __PERL_HASH_SEED_STATE(seed,state); \
f0df1bde 98 sbox32_seed_state96(seed + __PERL_HASH_SEED_BYTES, state + __PERL_HASH_STATE_BYTES); \
9d5e3f1a
YO
99} STMT_END
100
b02f3645
AC
101#define _PERL_HASH_WITH_STATE(state,str,len) \
102 (LIKELY(len <= SBOX32_MAX_LEN) \
103 ? sbox32_hash_with_state((state + __PERL_HASH_STATE_BYTES),(U8*)(str),(len)) \
f92b6808 104 : __PERL_HASH_WITH_STATE((state),(str),(len)))
9d5e3f1a
YO
105
106#endif
107
108PERL_STATIC_INLINE
109U32 S_perl_hash_with_seed(const U8 * const seed, const U8 * const str, const STRLEN len)
110{
111 U8 state[_PERL_HASH_STATE_BYTES];
112 _PERL_HASH_SEED_STATE(seed,state);
113 return _PERL_HASH_WITH_STATE(state,str,len);
114}
115
116#define PERL_HASH_WITH_SEED(seed,hash,str,len) \
935d8e72 117 (hash) = S_perl_hash_with_seed((const U8 *) seed, (const U8 *) str,len)
9d5e3f1a
YO
118#define PERL_HASH_WITH_STATE(state,hash,str,len) \
119 (hash) = _PERL_HASH_WITH_STATE((state),(U8*)(str),(len))
120#define PERL_HASH_SEED_STATE(seed,state) _PERL_HASH_SEED_STATE(seed,state)
121#define PERL_HASH_SEED_BYTES _PERL_HASH_SEED_BYTES
122#define PERL_HASH_STATE_BYTES _PERL_HASH_STATE_BYTES
123#define PERL_HASH_FUNC _PERL_HASH_FUNC
124
125#ifdef PERL_USE_SINGLE_CHAR_HASH_CACHE
126#define PERL_HASH(state,str,len) \
127 (hash) = ((len) < 2 ? ( (len) == 0 ? PL_hash_chars[256] : PL_hash_chars[(U8)(str)[0]] ) \
128 : _PERL_HASH_WITH_STATE(PL_hash_state,(U8*)(str),(len)))
129#else
130#define PERL_HASH(hash,str,len) \
131 PERL_HASH_WITH_STATE(PL_hash_state,hash,(U8*)(str),(len))
132#endif
133
134/* Setup the hash seed, either we do things dynamically at start up,
135 * including reading from the environment, or we randomly setup the
136 * seed. The seed will be passed into the PERL_HASH_SEED_STATE() function
137 * defined for the configuration defined for this perl, which will then
9af31f94 138 * initialize whatever state it might need later in hashing. */
9d5e3f1a
YO
139
140#ifndef PERL_HASH_SEED
1a237f4f 141# if defined(USE_HASH_SEED)
9d5e3f1a
YO
142# define PERL_HASH_SEED PL_hash_seed
143# else
144 /* this is a 512 bit seed, which should be more than enough for the
145 * configuration of any of our hash functions (with or without sbox).
146 * If you actually use a hard coded seed, you are strongly encouraged
147 * to replace this with something else of the correct length
148 * for the hash function you are using (24-32 bytes depending on build
149 * options). Repeat, you are *STRONGLY* encouraged not to use the value
150 * provided here.
151 */
152# define PERL_HASH_SEED \
153 ((const U8 *)"A long string of pseudorandomly " \
154 "chosen bytes for hashing in Perl")
155# endif
156#endif
157
158/* legacy - only mod_perl should be doing this. */
159#ifdef PERL_HASH_INTERNAL_ACCESS
160#define PERL_HASH_INTERNAL(hash,str,len) PERL_HASH(hash,str,len)
b716320d
YO
161#endif
162
4d3a042d
YO
163#endif /*compile once*/
164
165/*
4d3a042d
YO
166 * ex: set ts=8 sts=4 sw=4 et:
167 */