crc32c.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. //
  15. // -----------------------------------------------------------------------------
  16. // File: crc32c.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header file defines the API for computing CRC32C values as checksums
  20. // for arbitrary sequences of bytes provided as a string buffer.
  21. //
  22. // The API includes the basic functions for computing such CRC32C values and
  23. // some utility functions for performing more efficient mathematical
  24. // computations using an existing checksum.
  25. #ifndef Y_ABSL_CRC_CRC32C_H_
  26. #define Y_ABSL_CRC_CRC32C_H_
  27. #include <cstdint>
  28. #include <ostream>
  29. #include "y_absl/crc/internal/crc32c_inline.h"
  30. #include "y_absl/strings/str_format.h"
  31. #include "y_absl/strings/string_view.h"
  32. namespace y_absl {
  33. Y_ABSL_NAMESPACE_BEGIN
  34. //-----------------------------------------------------------------------------
  35. // crc32c_t
  36. //-----------------------------------------------------------------------------
  37. // `crc32c_t` defines a strongly-typed integer for holding a CRC32C value.
  38. //
  39. // Some operators are intentionally omitted. Only equality operators are defined
  40. // so that `crc32c_t` can be directly compared. Methods for putting `crc32c_t`
  41. // directly into a set are omitted because this is bug-prone due to checksum
  42. // collisions. Use an explicit conversion to the `uint32_t` space for operations
  43. // that treat `crc32c_t` as an integer.
  44. class crc32c_t final {
  45. public:
  46. crc32c_t() = default;
  47. constexpr explicit crc32c_t(uint32_t crc) : crc_(crc) {}
  48. crc32c_t(const crc32c_t&) = default;
  49. crc32c_t& operator=(const crc32c_t&) = default;
  50. explicit operator uint32_t() const { return crc_; }
  51. friend bool operator==(crc32c_t lhs, crc32c_t rhs) {
  52. return static_cast<uint32_t>(lhs) == static_cast<uint32_t>(rhs);
  53. }
  54. friend bool operator!=(crc32c_t lhs, crc32c_t rhs) { return !(lhs == rhs); }
  55. template <typename Sink>
  56. friend void AbslStringify(Sink& sink, crc32c_t crc) {
  57. y_absl::Format(&sink, "%08x", static_cast<uint32_t>(crc));
  58. }
  59. private:
  60. uint32_t crc_;
  61. };
  62. namespace crc_internal {
  63. // Non-inline code path for `y_absl::ExtendCrc32c()`. Do not call directly.
  64. // Call `y_absl::ExtendCrc32c()` (defined below) instead.
  65. crc32c_t ExtendCrc32cInternal(crc32c_t initial_crc,
  66. y_absl::string_view buf_to_add);
  67. } // namespace crc_internal
  68. // -----------------------------------------------------------------------------
  69. // CRC32C Computation Functions
  70. // -----------------------------------------------------------------------------
  71. // ComputeCrc32c()
  72. //
  73. // Returns the CRC32C value of the provided string.
  74. crc32c_t ComputeCrc32c(y_absl::string_view buf);
  75. // ExtendCrc32c()
  76. //
  77. // Computes a CRC32C value from an `initial_crc` CRC32C value including the
  78. // `buf_to_add` bytes of an additional buffer. Using this function is more
  79. // efficient than computing a CRC32C value for the combined buffer from
  80. // scratch.
  81. //
  82. // Note: `ExtendCrc32c` with an initial_crc of 0 is equivalent to
  83. // `ComputeCrc32c`.
  84. //
  85. // This operation has a runtime cost of O(`buf_to_add.size()`)
  86. inline crc32c_t ExtendCrc32c(crc32c_t initial_crc,
  87. y_absl::string_view buf_to_add) {
  88. // Approximately 75% of calls have size <= 64.
  89. if (buf_to_add.size() <= 64) {
  90. uint32_t crc = static_cast<uint32_t>(initial_crc);
  91. if (crc_internal::ExtendCrc32cInline(&crc, buf_to_add.data(),
  92. buf_to_add.size())) {
  93. return crc32c_t{crc};
  94. }
  95. }
  96. return crc_internal::ExtendCrc32cInternal(initial_crc, buf_to_add);
  97. }
  98. // ExtendCrc32cByZeroes()
  99. //
  100. // Computes a CRC32C value for a buffer with an `initial_crc` CRC32C value,
  101. // where `length` bytes with a value of 0 are appended to the buffer. Using this
  102. // function is more efficient than computing a CRC32C value for the combined
  103. // buffer from scratch.
  104. //
  105. // This operation has a runtime cost of O(log(`length`))
  106. crc32c_t ExtendCrc32cByZeroes(crc32c_t initial_crc, size_t length);
  107. // MemcpyCrc32c()
  108. //
  109. // Copies `src` to `dest` using `memcpy()` semantics, returning the CRC32C
  110. // value of the copied buffer.
  111. //
  112. // Using `MemcpyCrc32c()` is potentially faster than performing the `memcpy()`
  113. // and `ComputeCrc32c()` operations separately.
  114. crc32c_t MemcpyCrc32c(void* dest, const void* src, size_t count,
  115. crc32c_t initial_crc = crc32c_t{0});
  116. // -----------------------------------------------------------------------------
  117. // CRC32C Arithmetic Functions
  118. // -----------------------------------------------------------------------------
  119. // The following functions perform arithmetic on CRC32C values, which are
  120. // generally more efficient than recalculating any given result's CRC32C value.
  121. // ConcatCrc32c()
  122. //
  123. // Calculates the CRC32C value of two buffers with known CRC32C values
  124. // concatenated together.
  125. //
  126. // Given a buffer with CRC32C value `crc1` and a buffer with
  127. // CRC32C value `crc2` and length, `crc2_length`, returns the CRC32C value of
  128. // the concatenation of these two buffers.
  129. //
  130. // This operation has a runtime cost of O(log(`crc2_length`)).
  131. crc32c_t ConcatCrc32c(crc32c_t crc1, crc32c_t crc2, size_t crc2_length);
  132. // RemoveCrc32cPrefix()
  133. //
  134. // Calculates the CRC32C value of an existing buffer with a series of bytes
  135. // (the prefix) removed from the beginning of that buffer.
  136. //
  137. // Given the CRC32C value of an existing buffer, `full_string_crc`; The CRC32C
  138. // value of a prefix of that buffer, `prefix_crc`; and the length of the buffer
  139. // with the prefix removed, `remaining_string_length` , return the CRC32C
  140. // value of the buffer with the prefix removed.
  141. //
  142. // This operation has a runtime cost of O(log(`remaining_string_length`)).
  143. crc32c_t RemoveCrc32cPrefix(crc32c_t prefix_crc, crc32c_t full_string_crc,
  144. size_t remaining_string_length);
  145. // RemoveCrc32cSuffix()
  146. //
  147. // Calculates the CRC32C value of an existing buffer with a series of bytes
  148. // (the suffix) removed from the end of that buffer.
  149. //
  150. // Given a CRC32C value of an existing buffer `full_string_crc`, the CRC32C
  151. // value of the suffix to remove `suffix_crc`, and the length of that suffix
  152. // `suffix_len`, returns the CRC32C value of the buffer with suffix removed.
  153. //
  154. // This operation has a runtime cost of O(log(`suffix_len`))
  155. crc32c_t RemoveCrc32cSuffix(crc32c_t full_string_crc, crc32c_t suffix_crc,
  156. size_t suffix_length);
  157. // operator<<
  158. //
  159. // Streams the CRC32C value `crc` to the stream `os`.
  160. inline std::ostream& operator<<(std::ostream& os, crc32c_t crc) {
  161. return os << y_absl::StreamFormat("%08x", static_cast<uint32_t>(crc));
  162. }
  163. Y_ABSL_NAMESPACE_END
  164. } // namespace y_absl
  165. #endif // Y_ABSL_CRC_CRC32C_H_