xref: /PHP-7.3/ext/standard/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    | Author: Rasmus Lerdorf <rasmus@php.net>                              |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "php.h"
20 #include "basic_functions.h"
21 #include "crc32.h"
22 
23 /* {{{ proto string crc32(string str)
24    Calculate the crc32 polynomial of a string */
PHP_NAMED_FUNCTION(php_if_crc32)25 PHP_NAMED_FUNCTION(php_if_crc32)
26 {
27 	char *p;
28 	size_t nr;
29 	uint32_t crcinit = 0;
30 	register uint32_t crc;
31 
32 	ZEND_PARSE_PARAMETERS_START(1, 1)
33 		Z_PARAM_STRING(p, nr)
34 	ZEND_PARSE_PARAMETERS_END();
35 
36 	crc = crcinit^0xFFFFFFFF;
37 
38 	for (; nr--; ++p) {
39 		crc = ((crc >> 8) & 0x00FFFFFF) ^ crc32tab[(crc ^ (*p)) & 0xFF ];
40 	}
41 	RETVAL_LONG(crc^0xFFFFFFFF);
42 }
43 /* }}} */
44 
45 /*
46  * Local variables:
47  * tab-width: 4
48  * c-basic-offset: 4
49  * End:
50  * vim600: sw=4 ts=4 fdm=marker
51  * vim<600: sw=4 ts=4
52  */
53