hash_crc32.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. | Authors: Michael Wallner <mike@php.net> |
  14. | Sara Golemon <pollita@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include "php_hash.h"
  18. #include "php_hash_crc32.h"
  19. #include "php_hash_crc32_tables.h"
  20. #include "ext/standard/crc32_x86.h"
  21. PHP_HASH_API void PHP_CRC32Init(PHP_CRC32_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  22. {
  23. context->state = ~0;
  24. }
  25. PHP_HASH_API void PHP_CRC32Update(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
  26. {
  27. size_t i = 0;
  28. #if ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE || ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER
  29. i += crc32_x86_simd_update(X86_CRC32, &context->state, input, len);
  30. #endif
  31. for (; i < len; ++i) {
  32. context->state = (context->state << 8) ^ crc32_table[(context->state >> 24) ^ (input[i] & 0xff)];
  33. }
  34. }
  35. PHP_HASH_API void PHP_CRC32BUpdate(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
  36. {
  37. size_t i = 0;
  38. #if ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE || ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER
  39. i += crc32_x86_simd_update(X86_CRC32B, &context->state, input, len);
  40. #endif
  41. for (; i < len; ++i) {
  42. context->state = (context->state >> 8) ^ crc32b_table[(context->state ^ input[i]) & 0xff];
  43. }
  44. }
  45. PHP_HASH_API void PHP_CRC32CUpdate(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
  46. {
  47. size_t i = 0;
  48. #if ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE || ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER
  49. i += crc32_x86_simd_update(X86_CRC32C, &context->state, input, len);
  50. #endif
  51. for (; i < len; ++i) {
  52. context->state = (context->state >> 8) ^ crc32c_table[(context->state ^ input[i]) & 0xff];
  53. }
  54. }
  55. PHP_HASH_API void PHP_CRC32LEFinal(unsigned char digest[4], PHP_CRC32_CTX *context)
  56. {
  57. context->state=~context->state;
  58. digest[3] = (unsigned char) ((context->state >> 24) & 0xff);
  59. digest[2] = (unsigned char) ((context->state >> 16) & 0xff);
  60. digest[1] = (unsigned char) ((context->state >> 8) & 0xff);
  61. digest[0] = (unsigned char) (context->state & 0xff);
  62. context->state = 0;
  63. }
  64. PHP_HASH_API void PHP_CRC32BEFinal(unsigned char digest[4], PHP_CRC32_CTX *context)
  65. {
  66. context->state=~context->state;
  67. digest[0] = (unsigned char) ((context->state >> 24) & 0xff);
  68. digest[1] = (unsigned char) ((context->state >> 16) & 0xff);
  69. digest[2] = (unsigned char) ((context->state >> 8) & 0xff);
  70. digest[3] = (unsigned char) (context->state & 0xff);
  71. context->state = 0;
  72. }
  73. PHP_HASH_API int PHP_CRC32Copy(const php_hash_ops *ops, PHP_CRC32_CTX *orig_context, PHP_CRC32_CTX *copy_context)
  74. {
  75. copy_context->state = orig_context->state;
  76. return SUCCESS;
  77. }
  78. const php_hash_ops php_hash_crc32_ops = {
  79. "crc32",
  80. (php_hash_init_func_t) PHP_CRC32Init,
  81. (php_hash_update_func_t) PHP_CRC32Update,
  82. (php_hash_final_func_t) PHP_CRC32LEFinal,
  83. (php_hash_copy_func_t) PHP_CRC32Copy,
  84. php_hash_serialize,
  85. php_hash_unserialize,
  86. PHP_CRC32_SPEC,
  87. 4, /* what to say here? */
  88. 4,
  89. sizeof(PHP_CRC32_CTX),
  90. 0
  91. };
  92. const php_hash_ops php_hash_crc32b_ops = {
  93. "crc32b",
  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. php_hash_serialize,
  99. php_hash_unserialize,
  100. PHP_CRC32_SPEC,
  101. 4, /* what to say here? */
  102. 4,
  103. sizeof(PHP_CRC32_CTX),
  104. 0
  105. };
  106. const php_hash_ops php_hash_crc32c_ops = {
  107. "crc32c",
  108. (php_hash_init_func_t) PHP_CRC32Init,
  109. (php_hash_update_func_t) PHP_CRC32CUpdate,
  110. (php_hash_final_func_t) PHP_CRC32BEFinal,
  111. (php_hash_copy_func_t) PHP_CRC32Copy,
  112. php_hash_serialize,
  113. php_hash_unserialize,
  114. PHP_CRC32_SPEC,
  115. 4, /* what to say here? */
  116. 4,
  117. sizeof(PHP_CRC32_CTX),
  118. 0
  119. };