Coverage Report

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