php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
hash_joaat.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: 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
38
43
44PHP_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
49PHP_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 */
79static uint32_t
80joaat_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}
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
PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
Definition hash_joaat.c:39
PHP_HASH_API void PHP_JOAATUpdate(PHP_JOAAT_CTX *context, const unsigned char *input, size_t inputLen)
Definition hash_joaat.c:44
const php_hash_ops php_hash_joaat_ops
Definition hash_joaat.c:24
PHP_HASH_API void PHP_JOAATFinal(unsigned char digest[4], PHP_JOAAT_CTX *context)
Definition hash_joaat.c:49
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_JOAAT_SPEC
Definition dce.c:49
zval * args
#define ZEND_ATTRIBUTE_UNUSED
struct _zend_array HashTable
Definition zend_types.h:386