BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlde_crc32c.h
Go to the documentation of this file.
1/// @file bdlde_crc32c.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlde_crc32c.h -*-C++-*-
8#ifndef INCLUDED_BDLDE_CRC32C
9#define INCLUDED_BDLDE_CRC32C
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id$")
13
14/// @defgroup bdlde_crc32c bdlde_crc32c
15/// @brief Provide utilities to calculate the CRC32-C checksum of a dataset.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlde
19/// @{
20/// @addtogroup bdlde_crc32c
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlde_crc32c-purpose"> Purpose</a>
25/// * <a href="#bdlde_crc32c-classes"> Classes </a>
26/// * <a href="#bdlde_crc32c-description"> Description </a>
27/// * <a href="#bdlde_crc32c-thread-safety"> Thread Safety </a>
28/// * <a href="#bdlde_crc32c-support-for-hardware-acceleration"> Support for Hardware Acceleration </a>
29/// * <a href="#bdlde_crc32c-performance"> Performance </a>
30/// * <a href="#bdlde_crc32c-usage"> Usage </a>
31/// * <a href="#bdlde_crc32c-example-1-computing-and-updating-a-checksum"> Example 1: Computing and updating a checksum </a>
32///
33/// # Purpose {#bdlde_crc32c-purpose}
34/// Provide utilities to calculate the CRC32-C checksum of a dataset.
35///
36/// # Classes {#bdlde_crc32c-classes}
37///
38/// - bdlde::Crc32c : calculates CRC32-C checksum
39/// - bdlde::Crc32c_Impl: calculates CRC32-C checksum with alternative impl.
40///
41/// @see bdlde_crc32
42///
43/// # Description {#bdlde_crc32c-description}
44/// This component defines a struct, `bdlde::Crc32c` that
45/// dramatically accelerates (as opposite to the `bdlde::Crc32` component)
46/// calculation of a CRC32-C checksum (a cyclic redundancy check, comprised of
47/// 32 bits, that uses the Castagnoli polynomial), using a hardware-accelerated
48/// implementation if supported or a software implementation otherwise. It
49/// additionally defines the struct `bdlde::Crc32c_Impl` to expose alternative
50/// implementations that should not be used other than to test and benchmark.
51/// Note that a CRC32-C checksum is a strong and fast technique for determining
52/// whether or not a message was received without errors. Also note that a
53/// CRC-32 checksum does not aid in error correction and is not naively useful
54/// in any sort of cryptography application.
55///
56/// ## Thread Safety {#bdlde_crc32c-thread-safety}
57///
58///
59/// Thread safe.
60///
61/// ## Support for Hardware Acceleration {#bdlde_crc32c-support-for-hardware-acceleration}
62///
63///
64/// Hardware-accelerated implementation is enabled at compile time when building
65/// on a supported architecture with a compatible compiler. In addition,
66/// runtime checks are performed to detect whether the running platform has the
67/// required hardware support:
68/// * x86: SSE4.2 instructions are required
69/// * sparc: runtime check is detected by the `is_sparc_crc32c_avail` system
70/// call
71///
72/// ## Performance {#bdlde_crc32c-performance}
73///
74///
75/// See the test driver for this component in the `.t.cpp` to compare
76/// performance of the hardware-accelerated and software implementations against
77/// various alternative implementations that compute a 32-bit CRC checksum.
78///
79/// ## Usage {#bdlde_crc32c-usage}
80///
81///
82/// This section illustrates intended use of this component.
83///
84/// ### Example 1: Computing and updating a checksum {#bdlde_crc32c-example-1-computing-and-updating-a-checksum}
85///
86///
87/// The following code illustrates how to calculate and update a CRC32-C
88/// checksum for a message over the course of building the full message.
89///
90/// First, prepare a message.
91/// @code
92/// bsl::string message = "This is a test message.";
93/// @endcode
94/// Now, generate a checksum for `message`.
95/// @code
96/// unsigned int checksum = bdlde::Crc32c::calculate(message.c_str(),
97/// message.size());
98/// @endcode
99/// Finally, if we learn that our message has grown by another chunk and we want
100/// to compute the checksum of the original message plus the new chunk, let's
101/// update the checksum by using it as a starting point.
102/// @code
103/// // New chunk
104/// bsl::string newChunk = "This is a chunk appended to original message";
105/// message += newChunk;
106///
107/// // Update checksum using previous value as starting point
108/// checksum = bdlde::Crc32c::calculate(newChunk.c_str(),
109/// newChunk.size(),
110/// checksum);
111/// @endcode
112/// @}
113/** @} */
114/** @} */
115
116/** @addtogroup bdl
117 * @{
118 */
119/** @addtogroup bdlde
120 * @{
121 */
122/** @addtogroup bdlde_crc32c
123 * @{
124 */
125
126#include <bdlscm_version.h>
127
128#include <bsl_cstddef.h>
129
130
131namespace bdlde {
132
133 // =============
134 // struct Crc32c
135 // =============
136
137/// This class provides runtime-efficient utilities to calculate a CRC32-C
138/// checksum.
139struct Crc32c {
140
141 public:
142 // CLASS DATA
143
144 /// CRC32-C value for a 0 length input. Note that a buffer with this
145 /// CRC32-C value need not be a 0 length input.
146 static const unsigned int k_NULL_CRC32C = 0U;
147
148 // CLASS METHODS
149
150 /// Return the CRC32-C value calculated for the specified `data` over
151 /// the specified `length` number of bytes, using the optionally
152 /// specified `crc` value as the starting point for the calculation.
153 /// Note that if `data` is 0, then `length` also must be 0.
154 static unsigned int calculate(const void *data,
155 bsl::size_t length,
156 unsigned int crc = k_NULL_CRC32C);
157};
158
159 // ==================
160 // struct Crc32c_Impl
161 // ==================
162
163/// This class provides alternative implementations of utilities to
164/// calculate a CRC32-C checksum.
166
167 public:
168 // CLASS METHODS
169
170 /// Return the CRC32-C value calculated for the specified `data` over
171 /// the specified `length` number of bytes, using the optionally
172 /// specified `crc` value as the starting point for the calculation.
173 /// This utilizes a portable software-based implementation to perform
174 /// the calculation. Note that if `data` is 0, then `length` must also
175 /// be 0.
176 static
177 unsigned int calculateSoftware(const void *data,
178 bsl::size_t length,
179 unsigned int crc = Crc32c::k_NULL_CRC32C);
180
181 /// Return the CRC32-C value calculated for the specified `data` over
182 /// the specified `length` number of bytes, using the optionally
183 /// specified `crc` value as the starting point for the calculation.
184 /// This utilizes a hardware-based implementation that does not leverage
185 /// instruction level parallelism to perform the calculation (hence it
186 /// calculates the crc32c in "serial"). Note that this function will
187 /// fall back to the software version when running on unsupported
188 /// platforms. Also note that if `data` is 0, then `length` must also
189 /// be 0.
190 static
192 const void *data,
193 bsl::size_t length,
194 unsigned int crc = Crc32c::k_NULL_CRC32C);
195};
196
197} // close package namespace
198
199
200
201#endif
202
203// ----------------------------------------------------------------------------
204// Copyright 2018 Bloomberg Finance L.P.
205//
206// Licensed under the Apache License, Version 2.0 (the "License");
207// you may not use this file except in compliance with the License.
208// You may obtain a copy of the License at
209//
210// http://www.apache.org/licenses/LICENSE-2.0
211//
212// Unless required by applicable law or agreed to in writing, software
213// distributed under the License is distributed on an "AS IS" BASIS,
214// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
215// See the License for the specific language governing permissions and
216// limitations under the License.
217// ----------------------------- END-OF-FILE ----------------------------------
218
219/** @} */
220/** @} */
221/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlde_base64alphabet.h:118
Definition bdlde_crc32c.h:165
static unsigned int calculateSoftware(const void *data, bsl::size_t length, unsigned int crc=Crc32c::k_NULL_CRC32C)
static unsigned int calculateHardwareSerial(const void *data, bsl::size_t length, unsigned int crc=Crc32c::k_NULL_CRC32C)
Definition bdlde_crc32c.h:139
static const unsigned int k_NULL_CRC32C
Definition bdlde_crc32c.h:146
static unsigned int calculate(const void *data, bsl::size_t length, unsigned int crc=k_NULL_CRC32C)