1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Michael Maclean <mgdm@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* $Id$ */
20
21 /* Based on the public domain algorithm found at
22 http://www.isthe.com/chongo/tech/comp/fnv/index.html */
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 0
36 };
37
38 const php_hash_ops php_hash_fnv1a32_ops = {
39 (php_hash_init_func_t) PHP_FNV132Init,
40 (php_hash_update_func_t) PHP_FNV1a32Update,
41 (php_hash_final_func_t) PHP_FNV132Final,
42 (php_hash_copy_func_t) php_hash_copy,
43 4,
44 4,
45 sizeof(PHP_FNV132_CTX),
46 0
47 };
48
49 const php_hash_ops php_hash_fnv164_ops = {
50 (php_hash_init_func_t) PHP_FNV164Init,
51 (php_hash_update_func_t) PHP_FNV164Update,
52 (php_hash_final_func_t) PHP_FNV164Final,
53 (php_hash_copy_func_t) php_hash_copy,
54 8,
55 4,
56 sizeof(PHP_FNV164_CTX),
57 0
58 };
59
60 const php_hash_ops php_hash_fnv1a64_ops = {
61 (php_hash_init_func_t) PHP_FNV164Init,
62 (php_hash_update_func_t) PHP_FNV1a64Update,
63 (php_hash_final_func_t) PHP_FNV164Final,
64 (php_hash_copy_func_t) php_hash_copy,
65 8,
66 4,
67 sizeof(PHP_FNV164_CTX),
68 0
69 };
70
71 /* {{{ PHP_FNV132Init
72 * 32-bit FNV-1 hash initialisation
73 */
PHP_FNV132Init(PHP_FNV132_CTX * context)74 PHP_HASH_API void PHP_FNV132Init(PHP_FNV132_CTX *context)
75 {
76 context->state = PHP_FNV1_32_INIT;
77 }
78 /* }}} */
79
PHP_FNV132Update(PHP_FNV132_CTX * context,const unsigned char * input,unsigned int inputLen)80 PHP_HASH_API void PHP_FNV132Update(PHP_FNV132_CTX *context, const unsigned char *input,
81 unsigned int inputLen)
82 {
83 context->state = fnv_32_buf((void *)input, inputLen, context->state, 0);
84 }
85
PHP_FNV1a32Update(PHP_FNV132_CTX * context,const unsigned char * input,unsigned int inputLen)86 PHP_HASH_API void PHP_FNV1a32Update(PHP_FNV132_CTX *context, const unsigned char *input,
87 unsigned int inputLen)
88 {
89 context->state = fnv_32_buf((void *)input, inputLen, context->state, 1);
90 }
91
PHP_FNV132Final(unsigned char digest[4],PHP_FNV132_CTX * context)92 PHP_HASH_API void PHP_FNV132Final(unsigned char digest[4], PHP_FNV132_CTX * context)
93 {
94 #ifdef WORDS_BIGENDIAN
95 memcpy(digest, &context->state, 4);
96 #else
97 int i = 0;
98 unsigned char *c = (unsigned char *) &context->state;
99
100 for (i = 0; i < 4; i++) {
101 digest[i] = c[3 - i];
102 }
103 #endif
104 }
105
106 /* {{{ PHP_FNV164Init
107 * 64-bit FNV-1 hash initialisation
108 */
PHP_FNV164Init(PHP_FNV164_CTX * context)109 PHP_HASH_API void PHP_FNV164Init(PHP_FNV164_CTX *context)
110 {
111 context->state = PHP_FNV1_64_INIT;
112 }
113 /* }}} */
114
PHP_FNV164Update(PHP_FNV164_CTX * context,const unsigned char * input,unsigned int inputLen)115 PHP_HASH_API void PHP_FNV164Update(PHP_FNV164_CTX *context, const unsigned char *input,
116 unsigned int inputLen)
117 {
118 context->state = fnv_64_buf((void *)input, inputLen, context->state, 0);
119 }
120
PHP_FNV1a64Update(PHP_FNV164_CTX * context,const unsigned char * input,unsigned int inputLen)121 PHP_HASH_API void PHP_FNV1a64Update(PHP_FNV164_CTX *context, const unsigned char *input,
122 unsigned int inputLen)
123 {
124 context->state = fnv_64_buf((void *)input, inputLen, context->state, 1);
125 }
126
PHP_FNV164Final(unsigned char digest[8],PHP_FNV164_CTX * context)127 PHP_HASH_API void PHP_FNV164Final(unsigned char digest[8], PHP_FNV164_CTX * context)
128 {
129 #ifdef WORDS_BIGENDIAN
130 memcpy(digest, &context->state, 8);
131 #else
132 int i = 0;
133 unsigned char *c = (unsigned char *) &context->state;
134
135 for (i = 0; i < 8; i++) {
136 digest[i] = c[7 - i];
137 }
138 #endif
139 }
140
141
142 /*
143 * fnv_32_buf - perform a 32 bit Fowler/Noll/Vo hash on a buffer
144 *
145 * input:
146 * buf - start of buffer to hash
147 * len - length of buffer in octets
148 * hval - previous hash value or 0 if first call
149 * alternate - if > 0 use the alternate version
150 *
151 * returns:
152 * 32 bit hash as a static hash type
153 */
154 static uint32_t
fnv_32_buf(void * buf,size_t len,uint32_t hval,int alternate)155 fnv_32_buf(void *buf, size_t len, uint32_t hval, int alternate)
156 {
157 unsigned char *bp = (unsigned char *)buf; /* start of buffer */
158 unsigned char *be = bp + len; /* beyond end of buffer */
159
160 /*
161 * FNV-1 hash each octet in the buffer
162 */
163 while (bp < be) {
164
165 if (alternate == 0) {
166 /* multiply by the 32 bit FNV magic prime mod 2^32 */
167 hval *= PHP_FNV_32_PRIME;
168
169 /* xor the bottom with the current octet */
170 hval ^= (uint32_t)*bp++;
171 } else {
172 /* xor the bottom with the current octet */
173 hval ^= (uint32_t)*bp++;
174
175 /* multiply by the 32 bit FNV magic prime mod 2^32 */
176 hval *= PHP_FNV_32_PRIME;
177 }
178 }
179
180 /* return our new hash value */
181 return hval;
182 }
183
184 /*
185 * fnv_64_buf - perform a 64 bit Fowler/Noll/Vo hash on a buffer
186 *
187 * input:
188 * buf - start of buffer to hash
189 * len - length of buffer in octets
190 * hval - previous hash value or 0 if first call
191 * alternate - if > 0 use the alternate version
192 *
193 * returns:
194 * 64 bit hash as a static hash type
195 */
196 static uint64_t
fnv_64_buf(void * buf,size_t len,uint64_t hval,int alternate)197 fnv_64_buf(void *buf, size_t len, uint64_t hval, int alternate)
198 {
199 unsigned char *bp = (unsigned char *)buf; /* start of buffer */
200 unsigned char *be = bp + len; /* beyond end of buffer */
201
202 /*
203 * FNV-1 hash each octet of the buffer
204 */
205 while (bp < be) {
206
207 if (alternate == 0) {
208 /* multiply by the 64 bit FNV magic prime mod 2^64 */
209 hval *= PHP_FNV_64_PRIME;
210
211 /* xor the bottom with the current octet */
212 hval ^= (uint64_t)*bp++;
213 } else {
214 /* xor the bottom with the current octet */
215 hval ^= (uint64_t)*bp++;
216
217 /* multiply by the 64 bit FNV magic prime mod 2^64 */
218 hval *= PHP_FNV_64_PRIME;
219 }
220 }
221
222 /* return our new hash value */
223 return hval;
224 }
225
226 /*
227 * Local variables:
228 * tab-width: 4
229 * c-basic-offset: 4
230 * End:
231 * vim600: noet sw=4 ts=4 fdm=marker
232 * vim<600: noet sw=4 ts=4
233 */
234