crc32c.h 6.9 KB

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