Coverage Report

Created: 2025-06-13 06:43

/src/php-src/ext/hash/hash_crc32.c
Line
Count
Source (jump to first uncovered line)
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
  | Authors: Michael Wallner <mike@php.net>                              |
14
  |          Sara Golemon <pollita@php.net>                              |
15
  +----------------------------------------------------------------------+
16
*/
17
18
#include "php_hash.h"
19
#include "php_hash_crc32.h"
20
#include "php_hash_crc32_tables.h"
21
#include "ext/standard/crc32_x86.h"
22
23
PHP_HASH_API void PHP_CRC32Init(PHP_CRC32_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
24
1.85k
{
25
1.85k
  context->state = ~0;
26
1.85k
}
27
28
PHP_HASH_API void PHP_CRC32Update(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
29
23
{
30
23
  size_t i = 0;
31
32
23
#if defined(ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE) || defined(ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER)
33
23
  i += crc32_x86_simd_update(X86_CRC32, &context->state, input, len);
34
23
#endif
35
36
82
  for (; i < len; ++i) {
37
59
    context->state = (context->state << 8) ^ crc32_table[(context->state >> 24) ^ (input[i] & 0xff)];
38
59
  }
39
23
}
40
41
PHP_HASH_API void PHP_CRC32BUpdate(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
42
7
{
43
7
  size_t i = 0;
44
45
7
#if defined(ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE) || defined(ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER)
46
7
  i += crc32_x86_simd_update(X86_CRC32B, &context->state, input, len);
47
7
#endif
48
49
31
  for (; i < len; ++i) {
50
24
    context->state = (context->state >> 8) ^ crc32b_table[(context->state ^ input[i]) & 0xff];
51
24
  }
52
7
}
53
54
PHP_HASH_API void PHP_CRC32CUpdate(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
55
20
{
56
20
  size_t i = 0;
57
58
20
#if defined(ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE) || defined(ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER)
59
20
  i += crc32_x86_simd_update(X86_CRC32C, &context->state, input, len);
60
20
#endif
61
62
43
  for (; i < len; ++i) {
63
23
    context->state = (context->state >> 8) ^ crc32c_table[(context->state ^ input[i]) & 0xff];
64
23
  }
65
20
}
66
67
PHP_HASH_API void PHP_CRC32LEFinal(unsigned char digest[4], PHP_CRC32_CTX *context)
68
23
{
69
23
  context->state=~context->state;
70
23
  digest[3] = (unsigned char) ((context->state >> 24) & 0xff);
71
23
  digest[2] = (unsigned char) ((context->state >> 16) & 0xff);
72
23
  digest[1] = (unsigned char) ((context->state >> 8) & 0xff);
73
23
  digest[0] = (unsigned char) (context->state & 0xff);
74
23
  context->state = 0;
75
23
}
76
77
PHP_HASH_API void PHP_CRC32BEFinal(unsigned char digest[4], PHP_CRC32_CTX *context)
78
27
{
79
27
  context->state=~context->state;
80
27
  digest[0] = (unsigned char) ((context->state >> 24) & 0xff);
81
27
  digest[1] = (unsigned char) ((context->state >> 16) & 0xff);
82
27
  digest[2] = (unsigned char) ((context->state >> 8) & 0xff);
83
27
  digest[3] = (unsigned char) (context->state & 0xff);
84
27
  context->state = 0;
85
27
}
86
87
PHP_HASH_API zend_result PHP_CRC32Copy(const php_hash_ops *ops, const PHP_CRC32_CTX *orig_context, PHP_CRC32_CTX *copy_context)
88
0
{
89
0
  copy_context->state = orig_context->state;
90
0
  return SUCCESS;
91
0
}
92
93
const php_hash_ops php_hash_crc32_ops = {
94
  "crc32",
95
  (php_hash_init_func_t) PHP_CRC32Init,
96
  (php_hash_update_func_t) PHP_CRC32Update,
97
  (php_hash_final_func_t) PHP_CRC32LEFinal,
98
  (php_hash_copy_func_t) PHP_CRC32Copy,
99
  php_hash_serialize,
100
  php_hash_unserialize,
101
  PHP_CRC32_SPEC,
102
  4, /* what to say here? */
103
  4,
104
  sizeof(PHP_CRC32_CTX),
105
  0
106
};
107
108
const php_hash_ops php_hash_crc32b_ops = {
109
  "crc32b",
110
  (php_hash_init_func_t) PHP_CRC32Init,
111
  (php_hash_update_func_t) PHP_CRC32BUpdate,
112
  (php_hash_final_func_t) PHP_CRC32BEFinal,
113
  (php_hash_copy_func_t) PHP_CRC32Copy,
114
  php_hash_serialize,
115
  php_hash_unserialize,
116
  PHP_CRC32_SPEC,
117
  4, /* what to say here? */
118
  4,
119
  sizeof(PHP_CRC32_CTX),
120
  0
121
};
122
123
const php_hash_ops php_hash_crc32c_ops = {
124
  "crc32c",
125
  (php_hash_init_func_t) PHP_CRC32Init,
126
  (php_hash_update_func_t) PHP_CRC32CUpdate,
127
  (php_hash_final_func_t) PHP_CRC32BEFinal,
128
  (php_hash_copy_func_t) PHP_CRC32Copy,
129
  php_hash_serialize,
130
  php_hash_unserialize,
131
  PHP_CRC32_SPEC,
132
  4, /* what to say here? */
133
  4,
134
  sizeof(PHP_CRC32_CTX),
135
  0
136
};