xref: /PHP-8.1/ext/standard/crc32.c (revision 16072074)
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    | Author: Rasmus Lerdorf <rasmus@php.net>                              |
14    +----------------------------------------------------------------------+
15 */
16 
17 #include "php.h"
18 #include "basic_functions.h"
19 #include "crc32.h"
20 #include "crc32_x86.h"
21 
22 #if HAVE_AARCH64_CRC32
23 # include <arm_acle.h>
24 # if defined(__linux__)
25 #  include <sys/auxv.h>
26 #  include <asm/hwcap.h>
27 # elif defined(__APPLE__)
28 #  include <sys/sysctl.h>
29 # elif defined(__FreeBSD__)
30 #  include <sys/auxv.h>
31 
getauxval(unsigned long key)32 static unsigned long getauxval(unsigned long key) {
33 	unsigned long ret = 0;
34 	if (elf_aux_info(key, &ret, sizeof(ret)) != 0)
35 		return 0;
36 	return ret;
37 }
38 # endif
39 
has_crc32_insn()40 static inline int has_crc32_insn() {
41 	/* Only go through the runtime detection once. */
42 	static int res = -1;
43 	if (res != -1)
44 		return res;
45 # if defined(HWCAP_CRC32)
46 	res = getauxval(AT_HWCAP) & HWCAP_CRC32;
47 	return res;
48 # elif defined(HWCAP2_CRC32)
49 	res = getauxval(AT_HWCAP2) & HWCAP2_CRC32;
50 	return res;
51 # elif defined(__APPLE__)
52 	size_t reslen = sizeof(res);
53 	if (sysctlbyname("hw.optional.armv8_crc32", &res, &reslen, NULL, 0) < 0)
54 		res = 0;
55 	return res;
56 # else
57 	res = 0;
58 	return res;
59 # endif
60 }
61 
62 # if defined(__GNUC__) && !defined(__clang__)
63 #  pragma GCC push_options
64 #  pragma GCC target ("+nothing+crc")
65 # endif
crc32_aarch64(uint32_t crc,const char * p,size_t nr)66 static uint32_t crc32_aarch64(uint32_t crc, const char *p, size_t nr) {
67 	while (nr >= sizeof(uint64_t)) {
68 		crc = __crc32d(crc, *(uint64_t *)p);
69 		p += sizeof(uint64_t);
70 		nr -= sizeof(uint64_t);
71 	}
72 	if (nr >= sizeof(int32_t)) {
73 		crc = __crc32w(crc, *(uint32_t *)p);
74 		p += sizeof(uint32_t);
75 		nr -= sizeof(uint32_t);
76 	}
77 	if (nr >= sizeof(int16_t)) {
78 		crc = __crc32h(crc, *(uint16_t *)p);
79 		p += sizeof(uint16_t);
80 		nr -= sizeof(uint16_t);
81 	}
82 	if (nr) {
83 		crc = __crc32b(crc, *p);
84 	}
85 	return crc;
86 }
87 # if defined(__GNUC__) && !defined(__clang__)
88 #  pragma GCC pop_options
89 # endif
90 #endif
91 
php_crc32_bulk_update(uint32_t crc,const char * p,size_t nr)92 PHPAPI uint32_t php_crc32_bulk_update(uint32_t crc, const char *p, size_t nr)
93 {
94 #if HAVE_AARCH64_CRC32
95 	if (has_crc32_insn()) {
96 		crc = crc32_aarch64(crc, p, nr);
97 		return crc;
98 	}
99 #endif
100 
101 #if ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE || ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER
102 	size_t nr_simd = crc32_x86_simd_update(X86_CRC32B, &crc, (const unsigned char *)p, nr);
103 	nr -= nr_simd;
104 	p += nr_simd;
105 #endif
106 
107 	/* The trailing part */
108 	for (; nr--; ++p) {
109 		crc = ((crc >> 8) & 0x00FFFFFF) ^ crc32tab[(crc ^ (*p)) & 0xFF ];
110 	}
111 
112 	return crc;
113 }
114 
php_crc32_stream_bulk_update(uint32_t * crc,php_stream * fp,size_t nr)115 PHPAPI int php_crc32_stream_bulk_update(uint32_t *crc, php_stream *fp, size_t nr)
116 {
117 	size_t handled = 0, n;
118 	char buf[1024];
119 
120 	while (handled < nr) {
121 		n = nr - handled;
122 		n = (n < sizeof(buf)) ? n : sizeof(buf); /* tweak to buf size */
123 
124 		n = php_stream_read(fp, buf, n);
125 		if (n > 0) {
126 			*crc = php_crc32_bulk_update(*crc, buf, n);
127 			handled += n;
128 		} else { /* EOF */
129 			return FAILURE;
130 		}
131 	}
132 
133 	return SUCCESS;
134 }
135 
136 /* {{{ Calculate the crc32 polynomial of a string */
PHP_FUNCTION(crc32)137 PHP_FUNCTION(crc32)
138 {
139 	char *p;
140 	size_t nr;
141 	uint32_t crc = php_crc32_bulk_init();
142 
143 	ZEND_PARSE_PARAMETERS_START(1, 1)
144 		Z_PARAM_STRING(p, nr)
145 	ZEND_PARSE_PARAMETERS_END();
146 
147 	crc = php_crc32_bulk_update(crc, p, nr);
148 
149 	RETURN_LONG(php_crc32_bulk_end(crc));
150 }
151 /* }}} */
152