|
| 1 | +// Copyright 2025 Google LLC |
| 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 | +#include "google/cloud/bigtable/internal/crc32c.h" |
| 16 | +#include "absl/base/config.h" |
| 17 | +#if defined(ABSL_LTS_RELEASE_VERSION) && ABSL_LTS_RELEASE_VERSION >= 20230125 |
| 18 | +#include "absl/crc/crc32c.h" |
| 19 | +#define GOOGLE_CLOUD_CPP_USE_ABSL_CRC32C 1 |
| 20 | +#else |
| 21 | +#define GOOGLE_CLOUD_CPP_USE_ABSL_CRC32C 0 |
| 22 | +#endif // ABSL_LTS_RELEASE_VERSION |
| 23 | +#include <crc32c/crc32c.h> |
| 24 | + |
| 25 | +namespace google { |
| 26 | +namespace cloud { |
| 27 | +namespace bigtable_internal { |
| 28 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 29 | + |
| 30 | +std::uint32_t ExtendCrc32c(std::uint32_t crc, absl::string_view data) { |
| 31 | + return crc32c::Extend(crc, reinterpret_cast<uint8_t const*>(data.data()), |
| 32 | + data.size()); |
| 33 | +} |
| 34 | + |
| 35 | +std::uint32_t ExtendCrc32c(std::uint32_t crc, |
| 36 | + bigtable_internal::ConstBufferSequence const& data) { |
| 37 | + for (auto const& b : data) { |
| 38 | + crc = ExtendCrc32c(crc, absl::string_view{b.data(), b.size()}); |
| 39 | + } |
| 40 | + return crc; |
| 41 | +} |
| 42 | + |
| 43 | +std::uint32_t ExtendCrc32c(std::uint32_t crc, absl::Cord const& data) { |
| 44 | + for (auto i = data.chunk_begin(); i != data.chunk_end(); ++i) { |
| 45 | + crc = ExtendCrc32c(crc, *i); |
| 46 | + } |
| 47 | + return crc; |
| 48 | +} |
| 49 | + |
| 50 | +#if GOOGLE_CLOUD_CPP_USE_ABSL_CRC32C |
| 51 | + |
| 52 | +std::uint32_t ExtendCrc32c(std::uint32_t crc, absl::string_view data, |
| 53 | + std::uint32_t data_crc) { |
| 54 | + return static_cast<std::uint32_t>(absl::ConcatCrc32c( |
| 55 | + absl::crc32c_t{crc}, absl::crc32c_t{data_crc}, data.size())); |
| 56 | +} |
| 57 | + |
| 58 | +std::uint32_t ExtendCrc32c(std::uint32_t crc, |
| 59 | + bigtable_internal::ConstBufferSequence const& data, |
| 60 | + std::uint32_t data_crc) { |
| 61 | + auto const size = bigtable_internal::TotalBytes(data); |
| 62 | + return static_cast<std::uint32_t>( |
| 63 | + absl::ConcatCrc32c(absl::crc32c_t{crc}, absl::crc32c_t{data_crc}, size)); |
| 64 | +} |
| 65 | + |
| 66 | +std::uint32_t ExtendCrc32c(std::uint32_t crc, absl::Cord const& data, |
| 67 | + std::uint32_t data_crc) { |
| 68 | + return static_cast<std::uint32_t>(absl::ConcatCrc32c( |
| 69 | + absl::crc32c_t{crc}, absl::crc32c_t{data_crc}, data.size())); |
| 70 | +} |
| 71 | + |
| 72 | +#else |
| 73 | + |
| 74 | +std::uint32_t ExtendCrc32c(std::uint32_t crc, absl::string_view data, |
| 75 | + std::uint32_t /*data_crc*/) { |
| 76 | + return ExtendCrc32c(crc, data); |
| 77 | +} |
| 78 | + |
| 79 | +std::uint32_t ExtendCrc32c(std::uint32_t crc, |
| 80 | + bigtable_internal::ConstBufferSequence const& data, |
| 81 | + std::uint32_t /*data_crc*/) { |
| 82 | + return ExtendCrc32c(crc, data); |
| 83 | +} |
| 84 | + |
| 85 | +std::uint32_t ExtendCrc32c(std::uint32_t crc, absl::Cord const& data, |
| 86 | + std::uint32_t /*data_crc*/) { |
| 87 | + return ExtendCrc32c(crc, data); |
| 88 | +} |
| 89 | + |
| 90 | +#endif // GOOGLE_CLOUD_CPP_USE_ABSL_CRC32C |
| 91 | + |
| 92 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 93 | +} // namespace bigtable_internal |
| 94 | +} // namespace cloud |
| 95 | +} // namespace google |
0 commit comments