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/internal/crc_cord_state.h"
16
17 #include <cassert>
18
19 #include "absl/base/config.h"
20 #include "absl/numeric/bits.h"
21
22 namespace absl {
23 ABSL_NAMESPACE_BEGIN
24 namespace crc_internal {
25
RefSharedEmptyRep()26 CrcCordState::RefcountedRep* CrcCordState::RefSharedEmptyRep() {
27 static CrcCordState::RefcountedRep* empty = new CrcCordState::RefcountedRep;
28
29 assert(empty->count.load(std::memory_order_relaxed) >= 1);
30 assert(empty->rep.removed_prefix.length == 0);
31 assert(empty->rep.prefix_crc.empty());
32
33 Ref(empty);
34 return empty;
35 }
36
CrcCordState()37 CrcCordState::CrcCordState() : refcounted_rep_(new RefcountedRep) {}
38
CrcCordState(const CrcCordState & other)39 CrcCordState::CrcCordState(const CrcCordState& other)
40 : refcounted_rep_(other.refcounted_rep_) {
41 Ref(refcounted_rep_);
42 }
43
CrcCordState(CrcCordState && other)44 CrcCordState::CrcCordState(CrcCordState&& other)
45 : refcounted_rep_(other.refcounted_rep_) {
46 // Make `other` valid for use after move.
47 other.refcounted_rep_ = RefSharedEmptyRep();
48 }
49
operator =(const CrcCordState & other)50 CrcCordState& CrcCordState::operator=(const CrcCordState& other) {
51 if (this != &other) {
52 Unref(refcounted_rep_);
53 refcounted_rep_ = other.refcounted_rep_;
54 Ref(refcounted_rep_);
55 }
56 return *this;
57 }
58
operator =(CrcCordState && other)59 CrcCordState& CrcCordState::operator=(CrcCordState&& other) {
60 if (this != &other) {
61 Unref(refcounted_rep_);
62 refcounted_rep_ = other.refcounted_rep_;
63 // Make `other` valid for use after move.
64 other.refcounted_rep_ = RefSharedEmptyRep();
65 }
66 return *this;
67 }
68
~CrcCordState()69 CrcCordState::~CrcCordState() {
70 Unref(refcounted_rep_);
71 }
72
Checksum() const73 crc32c_t CrcCordState::Checksum() const {
74 if (rep().prefix_crc.empty()) {
75 return absl::crc32c_t{0};
76 }
77 if (IsNormalized()) {
78 return rep().prefix_crc.back().crc;
79 }
80 return absl::RemoveCrc32cPrefix(
81 rep().removed_prefix.crc, rep().prefix_crc.back().crc,
82 rep().prefix_crc.back().length - rep().removed_prefix.length);
83 }
84
NormalizedPrefixCrcAtNthChunk(size_t n) const85 CrcCordState::PrefixCrc CrcCordState::NormalizedPrefixCrcAtNthChunk(
86 size_t n) const {
87 assert(n < NumChunks());
88 if (IsNormalized()) {
89 return rep().prefix_crc[n];
90 }
91 size_t length = rep().prefix_crc[n].length - rep().removed_prefix.length;
92 return PrefixCrc(length,
93 absl::RemoveCrc32cPrefix(rep().removed_prefix.crc,
94 rep().prefix_crc[n].crc, length));
95 }
96
Normalize()97 void CrcCordState::Normalize() {
98 if (IsNormalized() || rep().prefix_crc.empty()) {
99 return;
100 }
101
102 Rep* r = mutable_rep();
103 for (auto& prefix_crc : r->prefix_crc) {
104 size_t remaining = prefix_crc.length - r->removed_prefix.length;
105 prefix_crc.crc = absl::RemoveCrc32cPrefix(r->removed_prefix.crc,
106 prefix_crc.crc, remaining);
107 prefix_crc.length = remaining;
108 }
109 r->removed_prefix = PrefixCrc();
110 }
111
Poison()112 void CrcCordState::Poison() {
113 Rep* rep = mutable_rep();
114 if (NumChunks() > 0) {
115 for (auto& prefix_crc : rep->prefix_crc) {
116 // This is basically CRC32::Scramble().
117 uint32_t crc = static_cast<uint32_t>(prefix_crc.crc);
118 crc += 0x2e76e41b;
119 crc = absl::rotr(crc, 17);
120 prefix_crc.crc = crc32c_t{crc};
121 }
122 } else {
123 // Add a fake corrupt chunk.
124 rep->prefix_crc.push_back(PrefixCrc(0, crc32c_t{1}));
125 }
126 }
127
128 } // namespace crc_internal
129 ABSL_NAMESPACE_END
130 } // namespace absl
131