xref: /PHP-7.3/ext/hash/hash_crc32.c (revision 8d3f8ca1)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Michael Wallner <mike@php.net>                              |
16   |          Sara Golemon <pollita@php.net>                              |
17   +----------------------------------------------------------------------+
18 */
19 
20 #include "php_hash.h"
21 #include "php_hash_crc32.h"
22 #include "php_hash_crc32_tables.h"
23 
PHP_CRC32Init(PHP_CRC32_CTX * context)24 PHP_HASH_API void PHP_CRC32Init(PHP_CRC32_CTX *context)
25 {
26 	context->state = ~0;
27 }
28 
PHP_CRC32Update(PHP_CRC32_CTX * context,const unsigned char * input,size_t len)29 PHP_HASH_API void PHP_CRC32Update(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
30 {
31 	size_t i;
32 
33 	for (i = 0; i < len; ++i) {
34 		context->state = (context->state << 8) ^ crc32_table[(context->state >> 24) ^ (input[i] & 0xff)];
35 	}
36 }
37 
PHP_CRC32BUpdate(PHP_CRC32_CTX * context,const unsigned char * input,size_t len)38 PHP_HASH_API void PHP_CRC32BUpdate(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
39 {
40 	size_t i;
41 
42 	for (i = 0; i < len; ++i) {
43 		context->state = (context->state >> 8) ^ crc32b_table[(context->state ^ input[i]) & 0xff];
44 	}
45 }
46 
PHP_CRC32Final(unsigned char digest[4],PHP_CRC32_CTX * context)47 PHP_HASH_API void PHP_CRC32Final(unsigned char digest[4], PHP_CRC32_CTX *context)
48 {
49 	context->state=~context->state;
50 	digest[3] = (unsigned char) ((context->state >> 24) & 0xff);
51 	digest[2] = (unsigned char) ((context->state >> 16) & 0xff);
52 	digest[1] = (unsigned char) ((context->state >> 8) & 0xff);
53 	digest[0] = (unsigned char) (context->state & 0xff);
54 	context->state = 0;
55 }
56 
PHP_CRC32BFinal(unsigned char digest[4],PHP_CRC32_CTX * context)57 PHP_HASH_API void PHP_CRC32BFinal(unsigned char digest[4], PHP_CRC32_CTX *context)
58 {
59 	context->state=~context->state;
60 	digest[0] = (unsigned char) ((context->state >> 24) & 0xff);
61 	digest[1] = (unsigned char) ((context->state >> 16) & 0xff);
62 	digest[2] = (unsigned char) ((context->state >> 8) & 0xff);
63 	digest[3] = (unsigned char) (context->state & 0xff);
64 	context->state = 0;
65 }
66 
PHP_CRC32Copy(const php_hash_ops * ops,PHP_CRC32_CTX * orig_context,PHP_CRC32_CTX * copy_context)67 PHP_HASH_API int PHP_CRC32Copy(const php_hash_ops *ops, PHP_CRC32_CTX *orig_context, PHP_CRC32_CTX *copy_context)
68 {
69 	copy_context->state = orig_context->state;
70 	return SUCCESS;
71 }
72 
73 const php_hash_ops php_hash_crc32_ops = {
74 	(php_hash_init_func_t) PHP_CRC32Init,
75 	(php_hash_update_func_t) PHP_CRC32Update,
76 	(php_hash_final_func_t) PHP_CRC32Final,
77 	(php_hash_copy_func_t) PHP_CRC32Copy,
78 	4, /* what to say here? */
79 	4,
80 	sizeof(PHP_CRC32_CTX),
81 	0
82 };
83 
84 const php_hash_ops php_hash_crc32b_ops = {
85 	(php_hash_init_func_t) PHP_CRC32Init,
86 	(php_hash_update_func_t) PHP_CRC32BUpdate,
87 	(php_hash_final_func_t) PHP_CRC32BFinal,
88 	(php_hash_copy_func_t) PHP_CRC32Copy,
89 	4, /* what to say here? */
90 	4,
91 	sizeof(PHP_CRC32_CTX),
92 	0
93 };
94 
95 /*
96  * Local variables:
97  * tab-width: 4
98  * c-basic-offset: 4
99  * End:
100  * vim600: sw=4 ts=4 fdm=marker
101  * vim<600: sw=4 ts=4
102  */
103