/src/php-src/ext/hash/hash_joaat.c
Line | Count | Source |
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 | | |
39 | | PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) |
40 | 10 | { |
41 | 10 | context->state = 0; |
42 | 10 | } |
43 | | |
44 | | PHP_HASH_API void PHP_JOAATUpdate(PHP_JOAAT_CTX *context, const unsigned char *input, size_t inputLen) |
45 | 10 | { |
46 | 10 | context->state = joaat_buf((void *)input, inputLen, context->state); |
47 | 10 | } |
48 | | |
49 | | PHP_HASH_API void PHP_JOAATFinal(unsigned char digest[4], PHP_JOAAT_CTX * context) |
50 | 10 | { |
51 | 10 | uint32_t hval = context->state; |
52 | 10 | hval += (hval << 3); |
53 | 10 | hval ^= (hval >> 11); |
54 | 10 | hval += (hval << 15); |
55 | | |
56 | | #ifdef WORDS_BIGENDIAN |
57 | | memcpy(digest, &hval, 4); |
58 | | #else |
59 | 10 | int i = 0; |
60 | 10 | unsigned char *c = (unsigned char *) &hval; |
61 | | |
62 | 50 | for (i = 0; i < 4; i++) { |
63 | 40 | digest[i] = c[3 - i]; |
64 | 40 | } |
65 | 10 | #endif |
66 | 10 | context->state = 0; |
67 | 10 | } |
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 |
80 | | joaat_buf(void *buf, size_t len, uint32_t hval) |
81 | 10 | { |
82 | 10 | size_t i; |
83 | 10 | unsigned char *input = (unsigned char *)buf; |
84 | | |
85 | 979 | for (i = 0; i < len; i++) { |
86 | 969 | hval += input[i]; |
87 | 969 | hval += (hval << 10); |
88 | 969 | hval ^= (hval >> 6); |
89 | 969 | } |
90 | | |
91 | 10 | return hval; |
92 | 10 | } |