Coverage Report

Created: 2023-06-07 07:09

/src/LPM/external.protobuf/include/absl/crc/crc32c.h
Line
Count
Source (jump to first uncovered line)
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
// -----------------------------------------------------------------------------
16
// File: crc32c.h
17
// -----------------------------------------------------------------------------
18
//
19
// This header file defines the API for computing CRC32C values as checksums
20
// for arbitrary sequences of bytes provided as a string buffer.
21
//
22
// The API includes the basic functions for computing such CRC32C values and
23
// some utility functions for performing more efficient mathematical
24
// computations using an existing checksum.
25
#ifndef ABSL_CRC_CRC32C_H_
26
#define ABSL_CRC_CRC32C_H_
27
28
#include <cstdint>
29
#include <ostream>
30
31
#include "absl/crc/internal/crc32c_inline.h"
32
#include "absl/strings/string_view.h"
33
34
namespace absl {
35
ABSL_NAMESPACE_BEGIN
36
37
//-----------------------------------------------------------------------------
38
// crc32c_t
39
//-----------------------------------------------------------------------------
40
41
// `crc32c_t` defines a strongly-typed integer for holding a CRC32C value.
42
//
43
// Some operators are intentionally omitted. Only equality operators are defined
44
// so that `crc32c_t` can be directly compared. Methods for putting `crc32c_t`
45
// directly into a set are omitted because this is bug-prone due to checksum
46
// collisions. Use an explicit conversion to the `uint32_t` space for operations
47
// that treat `crc32c_t` as an integer.
48
class crc32c_t final {
49
 public:
50
  crc32c_t() = default;
51
0
  constexpr explicit crc32c_t(uint32_t crc) : crc_(crc) {}
52
53
  crc32c_t(const crc32c_t&) = default;
54
  crc32c_t& operator=(const crc32c_t&) = default;
55
56
0
  explicit operator uint32_t() const { return crc_; }
57
58
0
  friend bool operator==(crc32c_t lhs, crc32c_t rhs) {
59
0
    return static_cast<uint32_t>(lhs) == static_cast<uint32_t>(rhs);
60
0
  }
61
62
0
  friend bool operator!=(crc32c_t lhs, crc32c_t rhs) { return !(lhs == rhs); }
63
64
 private:
65
  uint32_t crc_;
66
};
67
68
namespace crc_internal {
69
// Non-inline code path for `absl::ExtendCrc32c()`. Do not call directly.
70
// Call `absl::ExtendCrc32c()` (defined below) instead.
71
crc32c_t ExtendCrc32cInternal(crc32c_t initial_crc,
72
                              absl::string_view buf_to_add);
73
}  // namespace crc_internal
74
75
// -----------------------------------------------------------------------------
76
// CRC32C Computation Functions
77
// -----------------------------------------------------------------------------
78
79
// ComputeCrc32c()
80
//
81
// Returns the CRC32C value of the provided string.
82
crc32c_t ComputeCrc32c(absl::string_view buf);
83
84
// ExtendCrc32c()
85
//
86
// Computes a CRC32C value from an `initial_crc` CRC32C value including the
87
// `buf_to_add` bytes of an additional buffer. Using this function is more
88
// efficient than computing a CRC32C value for the combined buffer from
89
// scratch.
90
//
91
// Note: `ExtendCrc32c` with an initial_crc of 0 is equivalent to
92
// `ComputeCrc32c`.
93
//
94
// This operation has a runtime cost of O(`buf_to_add.size()`)
95
inline crc32c_t ExtendCrc32c(crc32c_t initial_crc,
96
0
                             absl::string_view buf_to_add) {
97
0
  // Approximately 75% of calls have size <= 64.
98
0
  if (buf_to_add.size() <= 64) {
99
0
    uint32_t crc = static_cast<uint32_t>(initial_crc);
100
0
    if (crc_internal::ExtendCrc32cInline(&crc, buf_to_add.data(),
101
0
                                         buf_to_add.size())) {
102
0
      return crc32c_t{crc};
103
0
    }
104
0
  }
105
0
  return crc_internal::ExtendCrc32cInternal(initial_crc, buf_to_add);
106
0
}
107
108
// ExtendCrc32cByZeroes()
109
//
110
// Computes a CRC32C value for a buffer with an `initial_crc` CRC32C value,
111
// where `length` bytes with a value of 0 are appended to the buffer. Using this
112
// function is more efficient than computing a CRC32C value for the combined
113
// buffer from scratch.
114
//
115
// This operation has a runtime cost of O(log(`length`))
116
crc32c_t ExtendCrc32cByZeroes(crc32c_t initial_crc, size_t length);
117
118
// MemcpyCrc32c()
119
//
120
// Copies `src` to `dest` using `memcpy()` semantics, returning the CRC32C
121
// value of the copied buffer.
122
//
123
// Using `MemcpyCrc32c()` is potentially faster than performing the `memcpy()`
124
// and `ComputeCrc32c()` operations separately.
125
crc32c_t MemcpyCrc32c(void* dest, const void* src, size_t count,
126
                      crc32c_t initial_crc = crc32c_t{0});
127
128
// -----------------------------------------------------------------------------
129
// CRC32C Arithmetic Functions
130
// -----------------------------------------------------------------------------
131
132
// The following functions perform arithmetic on CRC32C values, which are
133
// generally more efficient than recalculating any given result's CRC32C value.
134
135
// ConcatCrc32c()
136
//
137
// Calculates the CRC32C value of two buffers with known CRC32C values
138
// concatenated together.
139
//
140
// Given a buffer with CRC32C value `crc1` and a buffer with
141
// CRC32C value `crc2` and length, `crc2_length`, returns the CRC32C value of
142
// the concatenation of these two buffers.
143
//
144
// This operation has a runtime cost of O(log(`crc2_length`)).
145
crc32c_t ConcatCrc32c(crc32c_t crc1, crc32c_t crc2, size_t crc2_length);
146
147
// RemoveCrc32cPrefix()
148
//
149
// Calculates the CRC32C value of an existing buffer with a series of bytes
150
// (the prefix) removed from the beginning of that buffer.
151
//
152
// Given the CRC32C value of an existing buffer, `full_string_crc`; The CRC32C
153
// value of a prefix of that buffer, `prefix_crc`; and the length of the buffer
154
// with the prefix removed, `remaining_string_length` , return the CRC32C
155
// value of the buffer with the prefix removed.
156
//
157
// This operation has a runtime cost of O(log(`remaining_string_length`)).
158
crc32c_t RemoveCrc32cPrefix(crc32c_t prefix_crc, crc32c_t full_string_crc,
159
                            size_t remaining_string_length);
160
// RemoveCrc32cSuffix()
161
//
162
// Calculates the CRC32C value of an existing buffer with a series of bytes
163
// (the suffix) removed from the end of that buffer.
164
//
165
// Given a CRC32C value of an existing buffer `full_string_crc`, the CRC32C
166
// value of the suffix to remove `suffix_crc`, and the length of that suffix
167
// `suffix_len`, returns the CRC32C value of the buffer with suffix removed.
168
//
169
// This operation has a runtime cost of O(log(`suffix_len`))
170
crc32c_t RemoveCrc32cSuffix(crc32c_t full_string_crc, crc32c_t suffix_crc,
171
                            size_t suffix_length);
172
173
// operator<<
174
//
175
// Streams the CRC32C value `crc` to the stream `os`.
176
0
inline std::ostream& operator<<(std::ostream& os, crc32c_t crc) {
177
0
  return os << static_cast<uint32_t>(crc);
178
0
}
179
180
ABSL_NAMESPACE_END
181
}  // namespace absl
182
183
#endif  // ABSL_CRC_CRC32C_H_