crc32c.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2022 The Abseil Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/crc/crc32c.h"
  15. #include <cstdint>
  16. #include "absl/crc/internal/crc.h"
  17. #include "absl/crc/internal/crc32c.h"
  18. #include "absl/crc/internal/crc_memcpy.h"
  19. #include "absl/strings/string_view.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. namespace {
  23. const crc_internal::CRC* CrcEngine() {
  24. static const crc_internal::CRC* engine = crc_internal::CRC::Crc32c();
  25. return engine;
  26. }
  27. constexpr uint32_t kCRC32Xor = 0xffffffffU;
  28. } // namespace
  29. namespace crc_internal {
  30. crc32c_t UnextendCrc32cByZeroes(crc32c_t initial_crc, size_t length) {
  31. uint32_t crc = static_cast<uint32_t>(initial_crc) ^ kCRC32Xor;
  32. CrcEngine()->UnextendByZeroes(&crc, length);
  33. return static_cast<crc32c_t>(crc ^ kCRC32Xor);
  34. }
  35. // Called by `absl::ExtendCrc32c()` on strings with size > 64 or when hardware
  36. // CRC32C support is missing.
  37. crc32c_t ExtendCrc32cInternal(crc32c_t initial_crc,
  38. absl::string_view buf_to_add) {
  39. uint32_t crc = static_cast<uint32_t>(initial_crc) ^ kCRC32Xor;
  40. CrcEngine()->Extend(&crc, buf_to_add.data(), buf_to_add.size());
  41. return static_cast<crc32c_t>(crc ^ kCRC32Xor);
  42. }
  43. } // namespace crc_internal
  44. crc32c_t ComputeCrc32c(absl::string_view buf) {
  45. return ExtendCrc32c(crc32c_t{0}, buf);
  46. }
  47. crc32c_t ExtendCrc32cByZeroes(crc32c_t initial_crc, size_t length) {
  48. uint32_t crc = static_cast<uint32_t>(initial_crc) ^ kCRC32Xor;
  49. CrcEngine()->ExtendByZeroes(&crc, length);
  50. return static_cast<crc32c_t>(crc ^ kCRC32Xor);
  51. }
  52. crc32c_t ConcatCrc32c(crc32c_t lhs_crc, crc32c_t rhs_crc, size_t rhs_len) {
  53. uint32_t result = static_cast<uint32_t>(lhs_crc);
  54. CrcEngine()->ExtendByZeroes(&result, rhs_len);
  55. return crc32c_t{result ^ static_cast<uint32_t>(rhs_crc)};
  56. }
  57. crc32c_t RemoveCrc32cPrefix(crc32c_t crc_a, crc32c_t crc_ab, size_t length_b) {
  58. return ConcatCrc32c(crc_a, crc_ab, length_b);
  59. }
  60. crc32c_t MemcpyCrc32c(void* dest, const void* src, size_t count,
  61. crc32c_t initial_crc) {
  62. return static_cast<crc32c_t>(
  63. crc_internal::Crc32CAndCopy(dest, src, count, initial_crc, false));
  64. }
  65. // Remove a Suffix of given size from a buffer
  66. //
  67. // Given a CRC32C of an existing buffer, `full_string_crc`; the CRC32C of a
  68. // suffix of that buffer to remove, `suffix_crc`; and suffix buffer's length,
  69. // `suffix_len` return the CRC32C of the buffer with suffix removed
  70. //
  71. // This operation has a runtime cost of O(log(`suffix_len`))
  72. crc32c_t RemoveCrc32cSuffix(crc32c_t full_string_crc, crc32c_t suffix_crc,
  73. size_t suffix_len) {
  74. uint32_t result = static_cast<uint32_t>(full_string_crc) ^
  75. static_cast<uint32_t>(suffix_crc);
  76. CrcEngine()->UnextendByZeroes(&result, suffix_len);
  77. return crc32c_t{result};
  78. }
  79. ABSL_NAMESPACE_END
  80. } // namespace absl