php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
hash_fnv.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Michael Maclean <mgdm@php.net> |
14 +----------------------------------------------------------------------+
15*/
16
17/* Based on the public domain algorithm found at
18 http://www.isthe.com/chongo/tech/comp/fnv/index.html */
19
20#include "php_hash.h"
21#include "php_hash_fnv.h"
22
37
52
67
82
83/* {{{ PHP_FNV132Init
84 * 32-bit FNV-1 hash initialisation
85 */
90/* }}} */
91
92PHP_HASH_API void PHP_FNV132Update(PHP_FNV132_CTX *context, const unsigned char *input,
93 size_t inputLen)
94{
95 context->state = fnv_32_buf((void *)input, inputLen, context->state, 0);
96}
97
98PHP_HASH_API void PHP_FNV1a32Update(PHP_FNV132_CTX *context, const unsigned char *input,
99 size_t inputLen)
100{
101 context->state = fnv_32_buf((void *)input, inputLen, context->state, 1);
102}
103
104PHP_HASH_API void PHP_FNV132Final(unsigned char digest[4], PHP_FNV132_CTX * context)
105{
106#ifdef WORDS_BIGENDIAN
107 memcpy(digest, &context->state, 4);
108#else
109 int i = 0;
110 unsigned char *c = (unsigned char *) &context->state;
111
112 for (i = 0; i < 4; i++) {
113 digest[i] = c[3 - i];
114 }
115#endif
116}
117
118/* {{{ PHP_FNV164Init
119 * 64-bit FNV-1 hash initialisation
120 */
125/* }}} */
126
127PHP_HASH_API void PHP_FNV164Update(PHP_FNV164_CTX *context, const unsigned char *input,
128 size_t inputLen)
129{
130 context->state = fnv_64_buf((void *)input, inputLen, context->state, 0);
131}
132
133PHP_HASH_API void PHP_FNV1a64Update(PHP_FNV164_CTX *context, const unsigned char *input,
134 size_t inputLen)
135{
136 context->state = fnv_64_buf((void *)input, inputLen, context->state, 1);
137}
138
139PHP_HASH_API void PHP_FNV164Final(unsigned char digest[8], PHP_FNV164_CTX * context)
140{
141#ifdef WORDS_BIGENDIAN
142 memcpy(digest, &context->state, 8);
143#else
144 int i = 0;
145 unsigned char *c = (unsigned char *) &context->state;
146
147 for (i = 0; i < 8; i++) {
148 digest[i] = c[7 - i];
149 }
150#endif
151}
152
153
154/*
155 * fnv_32_buf - perform a 32 bit Fowler/Noll/Vo hash on a buffer
156 *
157 * input:
158 * buf - start of buffer to hash
159 * len - length of buffer in octets
160 * hval - previous hash value or 0 if first call
161 * alternate - if > 0 use the alternate version
162 *
163 * returns:
164 * 32-bit hash as a static hash type
165 */
166static uint32_t
167fnv_32_buf(void *buf, size_t len, uint32_t hval, int alternate)
168{
169 unsigned char *bp = (unsigned char *)buf; /* start of buffer */
170 unsigned char *be = bp + len; /* beyond end of buffer */
171
172 /*
173 * FNV-1 hash each octet in the buffer
174 */
175 if (alternate == 0) {
176 while (bp < be) {
177 /* multiply by the 32 bit FNV magic prime mod 2^32 */
178 hval *= PHP_FNV_32_PRIME;
179
180 /* xor the bottom with the current octet */
181 hval ^= (uint32_t)*bp++;
182 }
183 } else {
184 while (bp < be) {
185 /* xor the bottom with the current octet */
186 hval ^= (uint32_t)*bp++;
187
188 /* multiply by the 32 bit FNV magic prime mod 2^32 */
189 hval *= PHP_FNV_32_PRIME;
190 }
191 }
192
193 /* return our new hash value */
194 return hval;
195}
196
197/*
198 * fnv_64_buf - perform a 64 bit Fowler/Noll/Vo hash on a buffer
199 *
200 * input:
201 * buf - start of buffer to hash
202 * len - length of buffer in octets
203 * hval - previous hash value or 0 if first call
204 * alternate - if > 0 use the alternate version
205 *
206 * returns:
207 * 64-bit hash as a static hash type
208 */
209static uint64_t
210fnv_64_buf(void *buf, size_t len, uint64_t hval, int alternate)
211{
212 unsigned char *bp = (unsigned char *)buf; /* start of buffer */
213 unsigned char *be = bp + len; /* beyond end of buffer */
214
215 /*
216 * FNV-1 hash each octet of the buffer
217 */
218
219 if (alternate == 0) {
220 while (bp < be) {
221 /* multiply by the 64 bit FNV magic prime mod 2^64 */
222 hval *= PHP_FNV_64_PRIME;
223
224 /* xor the bottom with the current octet */
225 hval ^= (uint64_t)*bp++;
226 }
227 } else {
228 while (bp < be) {
229 /* xor the bottom with the current octet */
230 hval ^= (uint64_t)*bp++;
231
232 /* multiply by the 64 bit FNV magic prime mod 2^64 */
233 hval *= PHP_FNV_64_PRIME;
234 }
235 }
236
237 /* return our new hash value */
238 return hval;
239}
size_t len
Definition apprentice.c:174
memcpy(ptr1, ptr2, size)
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
PHP_HASH_API zend_result php_hash_serialize(const php_hashcontext_object *hash, zend_long *magic, zval *zv)
Definition hash.c:334
PHP_HASH_API zend_result php_hash_copy(const void *ops, const void *orig_context, void *dest_context)
Definition hash.c:124
PHP_HASH_API int php_hash_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv)
Definition hash.c:345
const php_hash_ops php_hash_fnv1a32_ops
Definition hash_fnv.c:38
PHP_HASH_API void PHP_FNV1a64Update(PHP_FNV164_CTX *context, const unsigned char *input, size_t inputLen)
Definition hash_fnv.c:133
PHP_HASH_API void PHP_FNV1a32Update(PHP_FNV132_CTX *context, const unsigned char *input, size_t inputLen)
Definition hash_fnv.c:98
const php_hash_ops php_hash_fnv1a64_ops
Definition hash_fnv.c:68
PHP_HASH_API void PHP_FNV132Init(PHP_FNV132_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
Definition hash_fnv.c:86
PHP_HASH_API void PHP_FNV164Init(PHP_FNV164_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
Definition hash_fnv.c:121
const php_hash_ops php_hash_fnv132_ops
Definition hash_fnv.c:23
PHP_HASH_API void PHP_FNV132Update(PHP_FNV132_CTX *context, const unsigned char *input, size_t inputLen)
Definition hash_fnv.c:92
PHP_HASH_API void PHP_FNV164Update(PHP_FNV164_CTX *context, const unsigned char *input, size_t inputLen)
Definition hash_fnv.c:127
PHP_HASH_API void PHP_FNV164Final(unsigned char digest[8], PHP_FNV164_CTX *context)
Definition hash_fnv.c:139
PHP_HASH_API void PHP_FNV132Final(unsigned char digest[4], PHP_FNV132_CTX *context)
Definition hash_fnv.c:104
const php_hash_ops php_hash_fnv164_ops
Definition hash_fnv.c:53
void(* php_hash_final_func_t)(unsigned char *digest, void *context)
Definition php_hash.h:36
void(* php_hash_init_func_t)(void *context, HashTable *args)
Definition php_hash.h:34
void(* php_hash_update_func_t)(void *context, const unsigned char *buf, size_t count)
Definition php_hash.h:35
#define PHP_HASH_API
Definition php_hash.h:144
struct _php_hash_ops php_hash_ops
#define PHP_FNV164_SPEC
#define PHP_FNV_32_PRIME
#define PHP_FNV1_32_INIT
#define PHP_FNV_64_PRIME
#define PHP_FNV132_SPEC
#define PHP_FNV1_64_INIT
HashTable bp[PHPDBG_BREAK_TABLES]
Definition phpdbg.h:231
Definition dce.c:49
zval * args
#define ZEND_ATTRIBUTE_UNUSED
struct _zend_array HashTable
Definition zend_types.h:386