xref: /PHP-8.1/ext/hash/hash_joaat.c (revision aff36587)
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: Martin Jansen <mj@php.net>                                   |
14   +----------------------------------------------------------------------+
15 */
16 
17 /* Implements Jenkins's one-at-a-time hashing algorithm as presented on
18  * http://www.burtleburtle.net/bob/hash/doobs.html.
19  */
20 
21 #include "php_hash.h"
22 #include "php_hash_joaat.h"
23 
24 const php_hash_ops php_hash_joaat_ops = {
25 	"joaat",
26 	(php_hash_init_func_t) PHP_JOAATInit,
27 	(php_hash_update_func_t) PHP_JOAATUpdate,
28 	(php_hash_final_func_t) PHP_JOAATFinal,
29 	php_hash_copy,
30 	php_hash_serialize,
31 	php_hash_unserialize,
32 	PHP_JOAAT_SPEC,
33 	4,
34 	4,
35 	sizeof(PHP_JOAAT_CTX),
36 	0
37 };
38 
PHP_JOAATInit(PHP_JOAAT_CTX * context,ZEND_ATTRIBUTE_UNUSED HashTable * args)39 PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
40 {
41 	context->state = 0;
42 }
43 
PHP_JOAATUpdate(PHP_JOAAT_CTX * context,const unsigned char * input,size_t inputLen)44 PHP_HASH_API void PHP_JOAATUpdate(PHP_JOAAT_CTX *context, const unsigned char *input, size_t inputLen)
45 {
46 	context->state = joaat_buf((void *)input, inputLen, context->state);
47 }
48 
PHP_JOAATFinal(unsigned char digest[4],PHP_JOAAT_CTX * context)49 PHP_HASH_API void PHP_JOAATFinal(unsigned char digest[4], PHP_JOAAT_CTX * context)
50 {
51 	uint32_t hval = context->state;
52 	hval += (hval << 3);
53 	hval ^= (hval >> 11);
54 	hval += (hval << 15);
55 
56 #ifdef WORDS_BIGENDIAN
57 	memcpy(digest, &hval, 4);
58 #else
59 	int i = 0;
60 	unsigned char *c = (unsigned char *) &hval;
61 
62 	for (i = 0; i < 4; i++) {
63 		digest[i] = c[3 - i];
64 	}
65 #endif
66 	context->state = 0;
67 }
68 
69 /*
70  * joaat_buf - perform a Jenkins's one-at-a-time hash on a buffer
71  *
72  * input:
73  *  buf - start of buffer to hash
74  *  len - length of buffer in octets
75  *
76  * returns:
77  *  32 bit hash as a static hash type
78  */
79 static uint32_t
joaat_buf(void * buf,size_t len,uint32_t hval)80 joaat_buf(void *buf, size_t len, uint32_t hval)
81 {
82 	size_t i;
83 	unsigned char *input = (unsigned char *)buf;
84 
85 	for (i = 0; i < len; i++) {
86 		hval += input[i];
87 		hval += (hval << 10);
88 		hval ^= (hval >> 6);
89 	}
90 
91 	return hval;
92 }
93