Coverage Report

Created: 2026-07-25 06:39

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
  0
36
};
37
38
PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
39
26
{
40
26
  context->state = 0;
41
26
}
42
43
PHP_HASH_API void PHP_JOAATUpdate(PHP_JOAAT_CTX *context, const unsigned char *input, size_t inputLen)
44
26
{
45
26
  context->state = joaat_buf((void *)input, inputLen, context->state);
46
26
}
47
48
PHP_HASH_API void PHP_JOAATFinal(unsigned char digest[4], PHP_JOAAT_CTX * context)
49
26
{
50
26
  uint32_t hval = context->state;
51
26
  hval += (hval << 3);
52
26
  hval ^= (hval >> 11);
53
26
  hval += (hval << 15);
54
55
#ifdef WORDS_BIGENDIAN
56
  memcpy(digest, &hval, 4);
57
#else
58
26
  int i = 0;
59
26
  unsigned char *c = (unsigned char *) &hval;
60
61
130
  for (i = 0; i < 4; i++) {
62
104
    digest[i] = c[3 - i];
63
104
  }
64
26
#endif
65
26
  context->state = 0;
66
26
}
67
68
/*
69
 * joaat_buf - perform a Jenkins's one-at-a-time hash on a buffer
70
 *
71
 * input:
72
 *  buf - start of buffer to hash
73
 *  len - length of buffer in octets
74
 *
75
 * returns:
76
 *  32 bit hash as a static hash type
77
 */
78
static uint32_t
79
joaat_buf(void *buf, size_t len, uint32_t hval)
80
26
{
81
26
  size_t i;
82
26
  unsigned char *input = (unsigned char *)buf;
83
84
665k
  for (i = 0; i < len; i++) {
85
664k
    hval += input[i];
86
664k
    hval += (hval << 10);
87
664k
    hval ^= (hval >> 6);
88
664k
  }
89
90
26
  return hval;
91
26
}