1 // Copyright 2021 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 #ifndef ABSL_STRINGS_INTERNAL_CORD_REP_CRC_H_
16 #define ABSL_STRINGS_INTERNAL_CORD_REP_CRC_H_
17 
18 #include <cassert>
19 #include <cstdint>
20 
21 #include "absl/base/config.h"
22 #include "absl/base/optimization.h"
23 #include "absl/crc/internal/crc_cord_state.h"
24 #include "absl/strings/internal/cord_internal.h"
25 
26 namespace absl {
27 ABSL_NAMESPACE_BEGIN
28 namespace cord_internal {
29 
30 // CordRepCrc is a CordRep node intended only to appear at the top level of a
31 // cord tree.  It associates an "expected CRC" with the contained data, to allow
32 // for easy passage of checksum data in Cord data flows.
33 //
34 // From Cord's perspective, the crc value has no semantics; any validation of
35 // the contained checksum is the user's responsibility.
36 struct CordRepCrc : public CordRep {
37   CordRep* child;
38   absl::crc_internal::CrcCordState crc_cord_state;
39 
40   // Consumes `child` and returns a CordRepCrc prefixed tree containing `child`.
41   // If the specified `child` is itself a CordRepCrc node, then this method
42   // either replaces the existing node, or directly updates the crc state in it
43   // depending on the node being shared or not, i.e.: refcount.IsOne().
44   // `child` must only be null if the Cord is empty. Never returns null.
45   static CordRepCrc* New(CordRep* child, crc_internal::CrcCordState state);
46 
47   // Destroys (deletes) the provided node. `node` must not be null.
48   static void Destroy(CordRepCrc* node);
49 };
50 
51 // Consumes `rep` and returns a CordRep* with any outer CordRepCrc wrapper
52 // removed.  This is usually a no-op (returning `rep`), but this will remove and
53 // unref an outer CordRepCrc node.
RemoveCrcNode(CordRep * rep)54 inline CordRep* RemoveCrcNode(CordRep* rep) {
55   assert(rep != nullptr);
56   if (ABSL_PREDICT_FALSE(rep->IsCrc())) {
57     CordRep* child = rep->crc()->child;
58     if (rep->refcount.IsOne()) {
59       delete rep->crc();
60     } else {
61       CordRep::Ref(child);
62       CordRep::Unref(rep);
63     }
64     return child;
65   }
66   return rep;
67 }
68 
69 // Returns `rep` if it is not a CordRepCrc node, or its child if it is.
70 // Does not consume or create a reference on `rep` or the returned value.
SkipCrcNode(CordRep * rep)71 inline CordRep* SkipCrcNode(CordRep* rep) {
72   assert(rep != nullptr);
73   if (ABSL_PREDICT_FALSE(rep->IsCrc())) {
74     return rep->crc()->child;
75   } else {
76     return rep;
77   }
78 }
79 
SkipCrcNode(const CordRep * rep)80 inline const CordRep* SkipCrcNode(const CordRep* rep) {
81   assert(rep != nullptr);
82   if (ABSL_PREDICT_FALSE(rep->IsCrc())) {
83     return rep->crc()->child;
84   } else {
85     return rep;
86   }
87 }
88 
crc()89 inline CordRepCrc* CordRep::crc() {
90   assert(IsCrc());
91   return static_cast<CordRepCrc*>(this);
92 }
93 
crc()94 inline const CordRepCrc* CordRep::crc() const {
95   assert(IsCrc());
96   return static_cast<const CordRepCrc*>(this);
97 }
98 
99 }  // namespace cord_internal
100 ABSL_NAMESPACE_END
101 }  // namespace absl
102 
103 #endif  // ABSL_STRINGS_INTERNAL_CORD_REP_CRC_H_
104