Coverage Report

Created: 2026-06-02 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/hash/hash_joaat.c
Line
Count
Source
1
/*
2
  +----------------------------------------------------------------------+
3
  | Copyright © The PHP Group and Contributors.                          |
4
  +----------------------------------------------------------------------+
5
  | This source file is subject to the Modified BSD License that is      |
6
  | bundled with this package in the file LICENSE, and is available      |
7
  | through the World Wide Web at <https://www.php.net/license/>.        |
8
  |                                                                      |
9
  | SPDX-License-Identifier: BSD-3-Clause                                |
10
  +----------------------------------------------------------------------+
11
  | Author: Martin Jansen <mj@php.net>                                   |
12
  +----------------------------------------------------------------------+
13
*/
14
15
/* Implements Jenkins's one-at-a-time hashing algorithm as presented on
16
 * http://www.burtleburtle.net/bob/hash/doobs.html.
17
 */
18
19
#include "php_hash.h"
20
#include "php_hash_joaat.h"
21
22
const php_hash_ops php_hash_joaat_ops = {
23
  "joaat",
24
  (php_hash_init_func_t) PHP_JOAATInit,
25
  (php_hash_update_func_t) PHP_JOAATUpdate,
26
  (php_hash_final_func_t) PHP_JOAATFinal,
27
  php_hash_copy,
28
  php_hash_serialize,
29
  php_hash_unserialize,
30
  PHP_JOAAT_SPEC,
31
  4,
32
  4,
33
  sizeof(PHP_JOAAT_CTX),
34
  0
35
};
36
37
PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
38
19
{
39
19
  context->state = 0;
40
19
}
41
42
PHP_HASH_API void PHP_JOAATUpdate(PHP_JOAAT_CTX *context, const unsigned char *input, size_t inputLen)
43
19
{
44
19
  context->state = joaat_buf((void *)input, inputLen, context->state);
45
19
}
46
47
PHP_HASH_API void PHP_JOAATFinal(unsigned char digest[4], PHP_JOAAT_CTX * context)
48
19
{
49
19
  uint32_t hval = context->state;
50
19
  hval += (hval << 3);
51
19
  hval ^= (hval >> 11);
52
19
  hval += (hval << 15);
53
54
#ifdef WORDS_BIGENDIAN
55
  memcpy(digest, &hval, 4);
56
#else
57
19
  int i = 0;
58
19
  unsigned char *c = (unsigned char *) &hval;
59
60
95
  for (i = 0; i < 4; i++) {
61
76
    digest[i] = c[3 - i];
62
76
  }
63
19
#endif
64
19
  context->state = 0;
65
19
}
66
67
/*
68
 * joaat_buf - perform a Jenkins's one-at-a-time hash on a buffer
69
 *
70
 * input:
71
 *  buf - start of buffer to hash
72
 *  len - length of buffer in octets
73
 *
74
 * returns:
75
 *  32 bit hash as a static hash type
76
 */
77
static uint32_t
78
joaat_buf(void *buf, size_t len, uint32_t hval)
79
19
{
80
19
  size_t i;
81
19
  unsigned char *input = (unsigned char *)buf;
82
83
74.1k
  for (i = 0; i < len; i++) {
84
74.1k
    hval += input[i];
85
74.1k
    hval += (hval << 10);
86
74.1k
    hval ^= (hval >> 6);
87
74.1k
  }
88
89
19
  return hval;
90
19
}