Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:40:50

0001 // Copyright 2022 The Abseil Authors
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //     https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // -----------------------------------------------------------------------------
0016 // File: crc32c.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // This header file defines the API for computing CRC32C values as checksums
0020 // for arbitrary sequences of bytes provided as a string buffer.
0021 //
0022 // The API includes the basic functions for computing such CRC32C values and
0023 // some utility functions for performing more efficient mathematical
0024 // computations using an existing checksum.
0025 #ifndef ABSL_CRC_CRC32C_H_
0026 #define ABSL_CRC_CRC32C_H_
0027 
0028 #include <cstdint>
0029 #include <ostream>
0030 
0031 #include "absl/crc/internal/crc32c_inline.h"
0032 #include "absl/strings/str_format.h"
0033 #include "absl/strings/string_view.h"
0034 
0035 namespace absl {
0036 ABSL_NAMESPACE_BEGIN
0037 
0038 //-----------------------------------------------------------------------------
0039 // crc32c_t
0040 //-----------------------------------------------------------------------------
0041 
0042 // `crc32c_t` defines a strongly-typed integer for holding a CRC32C value.
0043 //
0044 // Some operators are intentionally omitted. Only equality operators are defined
0045 // so that `crc32c_t` can be directly compared. Methods for putting `crc32c_t`
0046 // directly into a set are omitted because this is bug-prone due to checksum
0047 // collisions. Use an explicit conversion to the `uint32_t` space for operations
0048 // that treat `crc32c_t` as an integer.
0049 class crc32c_t final {
0050  public:
0051   crc32c_t() = default;
0052   constexpr explicit crc32c_t(uint32_t crc) : crc_(crc) {}
0053 
0054   crc32c_t(const crc32c_t&) = default;
0055   crc32c_t& operator=(const crc32c_t&) = default;
0056 
0057   explicit operator uint32_t() const { return crc_; }
0058 
0059   friend bool operator==(crc32c_t lhs, crc32c_t rhs) {
0060     return static_cast<uint32_t>(lhs) == static_cast<uint32_t>(rhs);
0061   }
0062 
0063   friend bool operator!=(crc32c_t lhs, crc32c_t rhs) { return !(lhs == rhs); }
0064 
0065   template <typename Sink>
0066   friend void AbslStringify(Sink& sink, crc32c_t crc) {
0067     absl::Format(&sink, "%08x", static_cast<uint32_t>(crc));
0068   }
0069 
0070  private:
0071   uint32_t crc_;
0072 };
0073 
0074 
0075 namespace crc_internal {
0076 // Non-inline code path for `absl::ExtendCrc32c()`. Do not call directly.
0077 // Call `absl::ExtendCrc32c()` (defined below) instead.
0078 crc32c_t ExtendCrc32cInternal(crc32c_t initial_crc,
0079                               absl::string_view buf_to_add);
0080 }  // namespace crc_internal
0081 
0082 // -----------------------------------------------------------------------------
0083 // CRC32C Computation Functions
0084 // -----------------------------------------------------------------------------
0085 
0086 // ComputeCrc32c()
0087 //
0088 // Returns the CRC32C value of the provided string.
0089 crc32c_t ComputeCrc32c(absl::string_view buf);
0090 
0091 // ExtendCrc32c()
0092 //
0093 // Computes a CRC32C value from an `initial_crc` CRC32C value including the
0094 // `buf_to_add` bytes of an additional buffer. Using this function is more
0095 // efficient than computing a CRC32C value for the combined buffer from
0096 // scratch.
0097 //
0098 // Note: `ExtendCrc32c` with an initial_crc of 0 is equivalent to
0099 // `ComputeCrc32c`.
0100 //
0101 // This operation has a runtime cost of O(`buf_to_add.size()`)
0102 inline crc32c_t ExtendCrc32c(crc32c_t initial_crc,
0103                              absl::string_view buf_to_add) {
0104   // Approximately 75% of calls have size <= 64.
0105   if (buf_to_add.size() <= 64) {
0106     uint32_t crc = static_cast<uint32_t>(initial_crc);
0107     if (crc_internal::ExtendCrc32cInline(&crc, buf_to_add.data(),
0108                                          buf_to_add.size())) {
0109       return crc32c_t{crc};
0110     }
0111   }
0112   return crc_internal::ExtendCrc32cInternal(initial_crc, buf_to_add);
0113 }
0114 
0115 // ExtendCrc32cByZeroes()
0116 //
0117 // Computes a CRC32C value for a buffer with an `initial_crc` CRC32C value,
0118 // where `length` bytes with a value of 0 are appended to the buffer. Using this
0119 // function is more efficient than computing a CRC32C value for the combined
0120 // buffer from scratch.
0121 //
0122 // This operation has a runtime cost of O(log(`length`))
0123 crc32c_t ExtendCrc32cByZeroes(crc32c_t initial_crc, size_t length);
0124 
0125 // MemcpyCrc32c()
0126 //
0127 // Copies `src` to `dest` using `memcpy()` semantics, returning the CRC32C
0128 // value of the copied buffer.
0129 //
0130 // Using `MemcpyCrc32c()` is potentially faster than performing the `memcpy()`
0131 // and `ComputeCrc32c()` operations separately.
0132 crc32c_t MemcpyCrc32c(void* dest, const void* src, size_t count,
0133                       crc32c_t initial_crc = crc32c_t{0});
0134 
0135 // -----------------------------------------------------------------------------
0136 // CRC32C Arithmetic Functions
0137 // -----------------------------------------------------------------------------
0138 
0139 // The following functions perform arithmetic on CRC32C values, which are
0140 // generally more efficient than recalculating any given result's CRC32C value.
0141 
0142 // ConcatCrc32c()
0143 //
0144 // Calculates the CRC32C value of two buffers with known CRC32C values
0145 // concatenated together.
0146 //
0147 // Given a buffer with CRC32C value `crc1` and a buffer with
0148 // CRC32C value `crc2` and length, `crc2_length`, returns the CRC32C value of
0149 // the concatenation of these two buffers.
0150 //
0151 // This operation has a runtime cost of O(log(`crc2_length`)).
0152 crc32c_t ConcatCrc32c(crc32c_t crc1, crc32c_t crc2, size_t crc2_length);
0153 
0154 // RemoveCrc32cPrefix()
0155 //
0156 // Calculates the CRC32C value of an existing buffer with a series of bytes
0157 // (the prefix) removed from the beginning of that buffer.
0158 //
0159 // Given the CRC32C value of an existing buffer, `full_string_crc`; The CRC32C
0160 // value of a prefix of that buffer, `prefix_crc`; and the length of the buffer
0161 // with the prefix removed, `remaining_string_length` , return the CRC32C
0162 // value of the buffer with the prefix removed.
0163 //
0164 // This operation has a runtime cost of O(log(`remaining_string_length`)).
0165 crc32c_t RemoveCrc32cPrefix(crc32c_t prefix_crc, crc32c_t full_string_crc,
0166                             size_t remaining_string_length);
0167 // RemoveCrc32cSuffix()
0168 //
0169 // Calculates the CRC32C value of an existing buffer with a series of bytes
0170 // (the suffix) removed from the end of that buffer.
0171 //
0172 // Given a CRC32C value of an existing buffer `full_string_crc`, the CRC32C
0173 // value of the suffix to remove `suffix_crc`, and the length of that suffix
0174 // `suffix_len`, returns the CRC32C value of the buffer with suffix removed.
0175 //
0176 // This operation has a runtime cost of O(log(`suffix_len`))
0177 crc32c_t RemoveCrc32cSuffix(crc32c_t full_string_crc, crc32c_t suffix_crc,
0178                             size_t suffix_length);
0179 
0180 // operator<<
0181 //
0182 // Streams the CRC32C value `crc` to the stream `os`.
0183 inline std::ostream& operator<<(std::ostream& os, crc32c_t crc) {
0184   return os << absl::StreamFormat("%08x", static_cast<uint32_t>(crc));
0185 }
0186 
0187 ABSL_NAMESPACE_END
0188 }  // namespace absl
0189 
0190 #endif  // ABSL_CRC_CRC32C_H_