crc32.c 3.9 KB

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