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
PHP_CRC32Init(PHP_CRC32_CTX * context,ZEND_ATTRIBUTE_UNUSED HashTable * args)23 PHP_HASH_API void PHP_CRC32Init(PHP_CRC32_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
24 {
25 context->state = ~0;
26 }
27
PHP_CRC32Update(PHP_CRC32_CTX * context,const unsigned char * input,size_t len)28 PHP_HASH_API void PHP_CRC32Update(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
29 {
30 size_t i = 0;
31
32 #if ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE || ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER
33 i += crc32_x86_simd_update(X86_CRC32, &context->state, input, len);
34 #endif
35
36 for (; i < len; ++i) {
37 context->state = (context->state << 8) ^ crc32_table[(context->state >> 24) ^ (input[i] & 0xff)];
38 }
39 }
40
PHP_CRC32BUpdate(PHP_CRC32_CTX * context,const unsigned char * input,size_t len)41 PHP_HASH_API void PHP_CRC32BUpdate(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
42 {
43 size_t i = 0;
44
45 #if ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE || ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER
46 i += crc32_x86_simd_update(X86_CRC32B, &context->state, input, len);
47 #endif
48
49 for (; i < len; ++i) {
50 context->state = (context->state >> 8) ^ crc32b_table[(context->state ^ input[i]) & 0xff];
51 }
52 }
53
PHP_CRC32CUpdate(PHP_CRC32_CTX * context,const unsigned char * input,size_t len)54 PHP_HASH_API void PHP_CRC32CUpdate(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
55 {
56 size_t i = 0;
57
58 #if ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE || ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER
59 i += crc32_x86_simd_update(X86_CRC32C, &context->state, input, len);
60 #endif
61
62 for (; i < len; ++i) {
63 context->state = (context->state >> 8) ^ crc32c_table[(context->state ^ input[i]) & 0xff];
64 }
65 }
66
PHP_CRC32LEFinal(unsigned char digest[4],PHP_CRC32_CTX * context)67 PHP_HASH_API void PHP_CRC32LEFinal(unsigned char digest[4], PHP_CRC32_CTX *context)
68 {
69 context->state=~context->state;
70 digest[3] = (unsigned char) ((context->state >> 24) & 0xff);
71 digest[2] = (unsigned char) ((context->state >> 16) & 0xff);
72 digest[1] = (unsigned char) ((context->state >> 8) & 0xff);
73 digest[0] = (unsigned char) (context->state & 0xff);
74 context->state = 0;
75 }
76
PHP_CRC32BEFinal(unsigned char digest[4],PHP_CRC32_CTX * context)77 PHP_HASH_API void PHP_CRC32BEFinal(unsigned char digest[4], PHP_CRC32_CTX *context)
78 {
79 context->state=~context->state;
80 digest[0] = (unsigned char) ((context->state >> 24) & 0xff);
81 digest[1] = (unsigned char) ((context->state >> 16) & 0xff);
82 digest[2] = (unsigned char) ((context->state >> 8) & 0xff);
83 digest[3] = (unsigned char) (context->state & 0xff);
84 context->state = 0;
85 }
86
PHP_CRC32Copy(const php_hash_ops * ops,PHP_CRC32_CTX * orig_context,PHP_CRC32_CTX * copy_context)87 PHP_HASH_API int PHP_CRC32Copy(const php_hash_ops *ops, PHP_CRC32_CTX *orig_context, PHP_CRC32_CTX *copy_context)
88 {
89 copy_context->state = orig_context->state;
90 return SUCCESS;
91 }
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 };
137