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