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