This source file includes following definitions.
- PHP_FNV132Init
- PHP_FNV132Update
- PHP_FNV1a32Update
- PHP_FNV132Final
- PHP_FNV164Init
- PHP_FNV164Update
- PHP_FNV1a64Update
- PHP_FNV164Final
- fnv_32_buf
- fnv_64_buf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #include "php_hash.h"
25 #include "php_hash_fnv.h"
26
27 const php_hash_ops php_hash_fnv132_ops = {
28 (php_hash_init_func_t) PHP_FNV132Init,
29 (php_hash_update_func_t) PHP_FNV132Update,
30 (php_hash_final_func_t) PHP_FNV132Final,
31 (php_hash_copy_func_t) php_hash_copy,
32 4,
33 4,
34 sizeof(PHP_FNV132_CTX)
35 };
36
37 const php_hash_ops php_hash_fnv1a32_ops = {
38 (php_hash_init_func_t) PHP_FNV132Init,
39 (php_hash_update_func_t) PHP_FNV1a32Update,
40 (php_hash_final_func_t) PHP_FNV132Final,
41 (php_hash_copy_func_t) php_hash_copy,
42 4,
43 4,
44 sizeof(PHP_FNV132_CTX)
45 };
46
47 const php_hash_ops php_hash_fnv164_ops = {
48 (php_hash_init_func_t) PHP_FNV164Init,
49 (php_hash_update_func_t) PHP_FNV164Update,
50 (php_hash_final_func_t) PHP_FNV164Final,
51 (php_hash_copy_func_t) php_hash_copy,
52 8,
53 4,
54 sizeof(PHP_FNV164_CTX)
55 };
56
57 const php_hash_ops php_hash_fnv1a64_ops = {
58 (php_hash_init_func_t) PHP_FNV164Init,
59 (php_hash_update_func_t) PHP_FNV1a64Update,
60 (php_hash_final_func_t) PHP_FNV164Final,
61 (php_hash_copy_func_t) php_hash_copy,
62 8,
63 4,
64 sizeof(PHP_FNV164_CTX)
65 };
66
67
68
69
70 PHP_HASH_API void PHP_FNV132Init(PHP_FNV132_CTX *context)
71 {
72 context->state = PHP_FNV1_32_INIT;
73 }
74
75
76 PHP_HASH_API void PHP_FNV132Update(PHP_FNV132_CTX *context, const unsigned char *input,
77 unsigned int inputLen)
78 {
79 context->state = fnv_32_buf((void *)input, inputLen, context->state, 0);
80 }
81
82 PHP_HASH_API void PHP_FNV1a32Update(PHP_FNV132_CTX *context, const unsigned char *input,
83 unsigned int inputLen)
84 {
85 context->state = fnv_32_buf((void *)input, inputLen, context->state, 1);
86 }
87
88 PHP_HASH_API void PHP_FNV132Final(unsigned char digest[4], PHP_FNV132_CTX * context)
89 {
90 #ifdef WORDS_BIGENDIAN
91 memcpy(digest, &context->state, 4);
92 #else
93 int i = 0;
94 unsigned char *c = (unsigned char *) &context->state;
95
96 for (i = 0; i < 4; i++) {
97 digest[i] = c[3 - i];
98 }
99 #endif
100 }
101
102
103
104
105 PHP_HASH_API void PHP_FNV164Init(PHP_FNV164_CTX *context)
106 {
107 context->state = PHP_FNV1_64_INIT;
108 }
109
110
111 PHP_HASH_API void PHP_FNV164Update(PHP_FNV164_CTX *context, const unsigned char *input,
112 unsigned int inputLen)
113 {
114 context->state = fnv_64_buf((void *)input, inputLen, context->state, 0);
115 }
116
117 PHP_HASH_API void PHP_FNV1a64Update(PHP_FNV164_CTX *context, const unsigned char *input,
118 unsigned int inputLen)
119 {
120 context->state = fnv_64_buf((void *)input, inputLen, context->state, 1);
121 }
122
123 PHP_HASH_API void PHP_FNV164Final(unsigned char digest[8], PHP_FNV164_CTX * context)
124 {
125 #ifdef WORDS_BIGENDIAN
126 memcpy(digest, &context->state, 8);
127 #else
128 int i = 0;
129 unsigned char *c = (unsigned char *) &context->state;
130
131 for (i = 0; i < 8; i++) {
132 digest[i] = c[7 - i];
133 }
134 #endif
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 static php_hash_uint32
151 fnv_32_buf(void *buf, size_t len, php_hash_uint32 hval, int alternate)
152 {
153 unsigned char *bp = (unsigned char *)buf;
154 unsigned char *be = bp + len;
155
156
157
158
159 while (bp < be) {
160
161 if (alternate == 0) {
162
163 hval *= PHP_FNV_32_PRIME;
164
165
166 hval ^= (php_hash_uint32)*bp++;
167 } else {
168
169 hval ^= (php_hash_uint32)*bp++;
170
171
172 hval *= PHP_FNV_32_PRIME;
173 }
174 }
175
176
177 return hval;
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192 static php_hash_uint64
193 fnv_64_buf(void *buf, size_t len, php_hash_uint64 hval, int alternate)
194 {
195 unsigned char *bp = (unsigned char *)buf;
196 unsigned char *be = bp + len;
197
198
199
200
201 while (bp < be) {
202
203 if (alternate == 0) {
204
205 hval *= PHP_FNV_64_PRIME;
206
207
208 hval ^= (php_hash_uint64)*bp++;
209 } else {
210
211 hval ^= (php_hash_uint64)*bp++;
212
213
214 hval *= PHP_FNV_64_PRIME;
215 }
216 }
217
218
219 return hval;
220 }
221
222
223
224
225
226
227
228
229