xref: /PHP-7.4/ext/hash/hash_crc32.c (revision c79ce48d)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 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_CRC32CUpdate(PHP_CRC32_CTX * context,const unsigned char * input,size_t len)47 PHP_HASH_API void PHP_CRC32CUpdate(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
48 {
49 	size_t i;
50 
51 	for (i = 0; i < len; ++i) {
52 		context->state = (context->state >> 8) ^ crc32c_table[(context->state ^ input[i]) & 0xff];
53 	}
54 }
55 
PHP_CRC32LEFinal(unsigned char digest[4],PHP_CRC32_CTX * context)56 PHP_HASH_API void PHP_CRC32LEFinal(unsigned char digest[4], PHP_CRC32_CTX *context)
57 {
58 	context->state=~context->state;
59 	digest[3] = (unsigned char) ((context->state >> 24) & 0xff);
60 	digest[2] = (unsigned char) ((context->state >> 16) & 0xff);
61 	digest[1] = (unsigned char) ((context->state >> 8) & 0xff);
62 	digest[0] = (unsigned char) (context->state & 0xff);
63 	context->state = 0;
64 }
65 
PHP_CRC32BEFinal(unsigned char digest[4],PHP_CRC32_CTX * context)66 PHP_HASH_API void PHP_CRC32BEFinal(unsigned char digest[4], PHP_CRC32_CTX *context)
67 {
68 	context->state=~context->state;
69 	digest[0] = (unsigned char) ((context->state >> 24) & 0xff);
70 	digest[1] = (unsigned char) ((context->state >> 16) & 0xff);
71 	digest[2] = (unsigned char) ((context->state >> 8) & 0xff);
72 	digest[3] = (unsigned char) (context->state & 0xff);
73 	context->state = 0;
74 }
75 
PHP_CRC32Copy(const php_hash_ops * ops,PHP_CRC32_CTX * orig_context,PHP_CRC32_CTX * copy_context)76 PHP_HASH_API int PHP_CRC32Copy(const php_hash_ops *ops, PHP_CRC32_CTX *orig_context, PHP_CRC32_CTX *copy_context)
77 {
78 	copy_context->state = orig_context->state;
79 	return SUCCESS;
80 }
81 
82 const php_hash_ops php_hash_crc32_ops = {
83 	(php_hash_init_func_t) PHP_CRC32Init,
84 	(php_hash_update_func_t) PHP_CRC32Update,
85 	(php_hash_final_func_t) PHP_CRC32LEFinal,
86 	(php_hash_copy_func_t) PHP_CRC32Copy,
87 	4, /* what to say here? */
88 	4,
89 	sizeof(PHP_CRC32_CTX),
90 	0
91 };
92 
93 const php_hash_ops php_hash_crc32b_ops = {
94 	(php_hash_init_func_t) PHP_CRC32Init,
95 	(php_hash_update_func_t) PHP_CRC32BUpdate,
96 	(php_hash_final_func_t) PHP_CRC32BEFinal,
97 	(php_hash_copy_func_t) PHP_CRC32Copy,
98 	4, /* what to say here? */
99 	4,
100 	sizeof(PHP_CRC32_CTX),
101 	0
102 };
103 
104 const php_hash_ops php_hash_crc32c_ops = {
105 	(php_hash_init_func_t) PHP_CRC32Init,
106 	(php_hash_update_func_t) PHP_CRC32CUpdate,
107 	(php_hash_final_func_t) PHP_CRC32BEFinal,
108 	(php_hash_copy_func_t) PHP_CRC32Copy,
109 	4, /* what to say here? */
110 	4,
111 	sizeof(PHP_CRC32_CTX),
112 	0
113 };
114