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