Coverage Report

Created: 2026-09-14 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/hash/hash_adler32.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
  | Authors: Michael Wallner <mike@php.net>                              |
12
  |          Sara Golemon <pollita@php.net>                              |
13
  +----------------------------------------------------------------------+
14
*/
15
16
#include "php_hash.h"
17
#include "php_hash_adler32.h"
18
19
PHP_HASH_API void PHP_ADLER32Init(PHP_ADLER32_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
20
45
{
21
45
  context->state = 1;
22
45
}
23
24
PHP_HASH_API void PHP_ADLER32Update(PHP_ADLER32_CTX *context, const unsigned char *input, size_t len)
25
44
{
26
44
  uint32_t s[2];
27
28
44
  s[0] = context->state & 0xffff;
29
44
  s[1] = (context->state >> 16) & 0xffff;
30
820k
  for (size_t i = 0; i < len; ++i) {
31
820k
    s[0] += input[i];
32
820k
    s[1] += s[0];
33
820k
    if (s[1]>=0x7fffffff)
34
204
    {
35
204
      s[0] = s[0] % 65521;
36
204
      s[1] = s[1] % 65521;
37
204
    }
38
820k
  }
39
44
  s[0] = s[0] % 65521;
40
44
  s[1] = s[1] % 65521;
41
44
  context->state = s[0] + (s[1] << 16);
42
44
}
43
44
PHP_HASH_API void PHP_ADLER32Final(unsigned char digest[4], PHP_ADLER32_CTX *context)
45
44
{
46
44
  digest[0] = (unsigned char) ((context->state >> 24) & 0xff);
47
44
  digest[1] = (unsigned char) ((context->state >> 16) & 0xff);
48
44
  digest[2] = (unsigned char) ((context->state >> 8) & 0xff);
49
44
  digest[3] = (unsigned char) (context->state & 0xff);
50
44
  context->state = 0;
51
44
}
52
53
PHP_HASH_API zend_result PHP_ADLER32Copy(const php_hash_ops *ops, const PHP_ADLER32_CTX *orig_context, PHP_ADLER32_CTX *copy_context)
54
0
{
55
0
  copy_context->state = orig_context->state;
56
0
  return SUCCESS;
57
0
}
58
59
const php_hash_ops php_hash_adler32_ops = {
60
  "adler32",
61
  (php_hash_init_func_t) PHP_ADLER32Init,
62
  (php_hash_update_func_t) PHP_ADLER32Update,
63
  (php_hash_final_func_t) PHP_ADLER32Final,
64
  (php_hash_copy_func_t) PHP_ADLER32Copy,
65
  php_hash_serialize,
66
  php_hash_unserialize,
67
  PHP_ADLER32_SPEC,
68
  4, /* what to say here? */
69
  4,
70
  sizeof(PHP_ADLER32_CTX),
71
  0,
72
  0
73
};