xref: /aosp_15_r20/external/cronet/third_party/abseil-cpp/absl/crc/crc32c.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #include "absl/crc/crc32c.h"
16 
17 #include <cstdint>
18 
19 #include "absl/crc/internal/crc.h"
20 #include "absl/crc/internal/crc32c.h"
21 #include "absl/crc/internal/crc_memcpy.h"
22 #include "absl/strings/string_view.h"
23 
24 namespace absl {
25 ABSL_NAMESPACE_BEGIN
26 
27 namespace {
28 
CrcEngine()29 const crc_internal::CRC* CrcEngine() {
30   static const crc_internal::CRC* engine = crc_internal::CRC::Crc32c();
31   return engine;
32 }
33 
34 constexpr uint32_t kCRC32Xor = 0xffffffffU;
35 
36 }  // namespace
37 
38 namespace crc_internal {
39 
UnextendCrc32cByZeroes(crc32c_t initial_crc,size_t length)40 crc32c_t UnextendCrc32cByZeroes(crc32c_t initial_crc, size_t length) {
41   uint32_t crc = static_cast<uint32_t>(initial_crc) ^ kCRC32Xor;
42   CrcEngine()->UnextendByZeroes(&crc, length);
43   return static_cast<crc32c_t>(crc ^ kCRC32Xor);
44 }
45 
46 // Called by `absl::ExtendCrc32c()` on strings with size > 64 or when hardware
47 // CRC32C support is missing.
ExtendCrc32cInternal(crc32c_t initial_crc,absl::string_view buf_to_add)48 crc32c_t ExtendCrc32cInternal(crc32c_t initial_crc,
49                               absl::string_view buf_to_add) {
50   uint32_t crc = static_cast<uint32_t>(initial_crc) ^ kCRC32Xor;
51   CrcEngine()->Extend(&crc, buf_to_add.data(), buf_to_add.size());
52   return static_cast<crc32c_t>(crc ^ kCRC32Xor);
53 }
54 
55 }  // namespace crc_internal
56 
ComputeCrc32c(absl::string_view buf)57 crc32c_t ComputeCrc32c(absl::string_view buf) {
58   return ExtendCrc32c(crc32c_t{0}, buf);
59 }
60 
ExtendCrc32cByZeroes(crc32c_t initial_crc,size_t length)61 crc32c_t ExtendCrc32cByZeroes(crc32c_t initial_crc, size_t length) {
62   uint32_t crc = static_cast<uint32_t>(initial_crc) ^ kCRC32Xor;
63   CrcEngine()->ExtendByZeroes(&crc, length);
64   return static_cast<crc32c_t>(crc ^ kCRC32Xor);
65 }
66 
ConcatCrc32c(crc32c_t lhs_crc,crc32c_t rhs_crc,size_t rhs_len)67 crc32c_t ConcatCrc32c(crc32c_t lhs_crc, crc32c_t rhs_crc, size_t rhs_len) {
68   uint32_t result = static_cast<uint32_t>(lhs_crc);
69   CrcEngine()->ExtendByZeroes(&result, rhs_len);
70   return crc32c_t{result ^ static_cast<uint32_t>(rhs_crc)};
71 }
72 
RemoveCrc32cPrefix(crc32c_t crc_a,crc32c_t crc_ab,size_t length_b)73 crc32c_t RemoveCrc32cPrefix(crc32c_t crc_a, crc32c_t crc_ab, size_t length_b) {
74   return ConcatCrc32c(crc_a, crc_ab, length_b);
75 }
76 
MemcpyCrc32c(void * dest,const void * src,size_t count,crc32c_t initial_crc)77 crc32c_t MemcpyCrc32c(void* dest, const void* src, size_t count,
78                       crc32c_t initial_crc) {
79   return static_cast<crc32c_t>(
80       crc_internal::Crc32CAndCopy(dest, src, count, initial_crc, false));
81 }
82 
83 // Remove a Suffix of given size from a buffer
84 //
85 // Given a CRC32C of an existing buffer, `full_string_crc`; the CRC32C of a
86 // suffix of that buffer to remove, `suffix_crc`; and suffix buffer's length,
87 // `suffix_len` return the CRC32C of the buffer with suffix removed
88 //
89 // This operation has a runtime cost of O(log(`suffix_len`))
RemoveCrc32cSuffix(crc32c_t full_string_crc,crc32c_t suffix_crc,size_t suffix_len)90 crc32c_t RemoveCrc32cSuffix(crc32c_t full_string_crc, crc32c_t suffix_crc,
91                             size_t suffix_len) {
92   uint32_t result = static_cast<uint32_t>(full_string_crc) ^
93                     static_cast<uint32_t>(suffix_crc);
94   CrcEngine()->UnextendByZeroes(&result, suffix_len);
95   return crc32c_t{result};
96 }
97 
98 ABSL_NAMESPACE_END
99 }  // namespace absl
100