BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlde_crc32.h
Go to the documentation of this file.
1/// @file bdlde_crc32.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlde_crc32.h -*-C++-*-
8#ifndef INCLUDED_BDLDE_CRC32
9#define INCLUDED_BDLDE_CRC32
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlde_crc32 bdlde_crc32
15/// @brief Provide a mechanism for computing the CRC-32 checksum of a dataset.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlde
19/// @{
20/// @addtogroup bdlde_crc32
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlde_crc32-purpose"> Purpose</a>
25/// * <a href="#bdlde_crc32-classes"> Classes </a>
26/// * <a href="#bdlde_crc32-description"> Description </a>
27/// * <a href="#bdlde_crc32-usage"> Usage </a>
28/// * <a href="#bdlde_crc32-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#bdlde_crc32-purpose}
31/// Provide a mechanism for computing the CRC-32 checksum of a dataset.
32///
33/// # Classes {#bdlde_crc32-classes}
34///
35/// - bdlde::Crc32: stores and updates a CRC-32 checksum
36///
37/// @see
38///
39/// # Description {#bdlde_crc32-description}
40/// This component implements a mechanism for computing, updating,
41/// and streaming a CRC-32 checksum (a cyclic redundancy check comprised of 32
42/// bits). This checksum is a strong and fast technique for determining whether
43/// or not a message was received without errors. Note that a CRC-32 checksum
44/// does not aid in error correction and is not naively useful in any sort of
45/// cryptographic application. Compared to other methods such as MD5 and
46/// SHA-256, it is relatively easy to find alternate texts with identical
47/// checksum.
48///
49/// ## Usage {#bdlde_crc32-usage}
50///
51///
52/// This section illustrates intended use of this component.
53///
54/// ### Example 1: Basic Usage {#bdlde_crc32-example-1-basic-usage}
55///
56///
57/// The following snippets of code illustrate a typical use of the
58/// `bdlde::Crc32` class. Each function would typically execute in separate
59/// processes or potentially on separate machines. The `senderExample` function
60/// below demonstrates how a message sender can write a message and its CRC-32
61/// checksum to a `bdex` output stream. Note that `Out` may be a `typedef` of
62/// any class that implements the `bslx::OutStream` protocol:
63/// @code
64/// /// Write a message and its CRC-32 checksum to the specified `output`
65/// /// stream.
66/// void senderExample(Out& output)
67/// {
68/// // prepare a message
69/// bsl::string message = "This is a test message.";
70///
71/// // generate a checksum for `message`
72/// bdlde::Crc32 crc(message.data(), message.length());
73///
74/// // write the message to `output`
75/// output << message;
76///
77/// // write the checksum to `output`
78/// const int VERSION = 1;
79/// crc.bdexStreamOut(output, VERSION);
80/// }
81/// @endcode
82/// The `receiverExample` function below illustrates how a message receiver can
83/// read a message and its CRC-32 checksum from a `bdex` input stream, then
84/// perform a local CRC-32 computation to verify that the message was received
85/// intact. Note that `In` may be a `typedef` of any class that implements the
86/// `bslx::InStream` protocol:
87/// @code
88/// /// Read a message and its CRC-32 checksum from the specified `input`
89/// /// stream, and verify the integrity of the message.
90/// void receiverExample(In& input)
91/// {
92/// // read the message from `input`
93/// bsl::string message;
94/// input >> message;
95///
96/// // read the checksum from `input`
97/// bdlde::Crc32 crc;
98/// const int VERSION = 1;
99/// crc.bdexStreamIn(input, VERSION);
100///
101/// // locally compute the checksum of the received `message`
102/// bdlde::Crc32 crcLocal;
103/// crcLocal.update(message.data(), message.length());
104///
105/// // verify that the received and locally-computed checksums match
106/// assert(crcLocal == crc);
107/// }
108/// @endcode
109/// @}
110/** @} */
111/** @} */
112
113/** @addtogroup bdl
114 * @{
115 */
116/** @addtogroup bdlde
117 * @{
118 */
119/** @addtogroup bdlde_crc32
120 * @{
121 */
122
123#include <bdlscm_version.h>
124
125#include <bsls_assert.h>
126
127#include <bsl_cstddef.h>
128#include <bsl_iosfwd.h>
129
130
131namespace bdlde {
132
133 // ===========
134 // class Crc32
135 // ===========
136
137/// This class represents a CRC-32 checksum value that can be updated as
138/// data is provided.
139///
140/// More generally, this class supports a complete set of *value semantic*
141/// operations, including copy construction, assignment, equality comparison,
142/// `ostream` printing, and `bdex` serialization. (A precise operational
143/// definition of when two objects have the same value can be found in the
144/// description of `operator==` for the class.) This class is *exception
145/// neutral* with no guarantee of rollback: if an exception is thrown during
146/// the invocation of a method on a pre-existing object, the class is left in a
147/// valid state, but its value is undefined. In no event is memory leaked.
148/// Finally, *aliasing* (e.g., using all or part of an object as both source
149/// and destination) is supported in all cases.
150///
151/// See @ref bdlde_crc32
152class Crc32 {
153
154 // DATA
155 unsigned int d_crc; // value of the checksum ^ 0xffffffff
156
157 // FRIENDS
158 friend bool operator==(const Crc32&, const Crc32&);
159
160 public:
161 // CLASS METHODS
162
163 /// Return the maximum valid BDEX format version, as indicated by the
164 /// specified `versionSelector`, to be passed to the `bdexStreamOut`
165 /// method. Note that the `versionSelector` is expected to be formatted
166 /// as `yyyymmdd`, a date representation. See the `bslx` package-level
167 /// documentation for more information on BDEX streaming of
168 /// value-semantic types and containers.
169 static int maxSupportedBdexVersion(int versionSelector);
170
171 // CREATORS
172
173 /// Construct a checksum having the value corresponding to no data
174 /// having been provided (i.e., having the value 0).
175 Crc32();
176
177 /// Construct a checksum corresponding to the specified `data` having
178 /// the specified `length` (in bytes). Note that if `data` is 0, then
179 /// `length` also must be 0.
180 Crc32(const void *data, bsl::size_t length);
181
182 /// Construct a checksum having the value of the specified `original`
183 /// checksum.
184 Crc32(const Crc32& original);
185
186 /// Destroy this object.
187 ~Crc32() = default;
188
189 // MANIPULATORS
190
191 /// Assign to this checksum the value of the specified `rhs` checksum,
192 /// and return a reference to this modifiable checksum.
193 Crc32& operator=(const Crc32& rhs);
194
195 /// Assign to this object the value read from the specified input
196 /// `stream` using the specified `version` format, and return a
197 /// reference to `stream`. If `stream` is initially invalid, this
198 /// operation has no effect. If `version` is not supported, this object
199 /// is unaltered and `stream` is invalidated but otherwise unmodified.
200 /// If `version` is supported but `stream` becomes invalid during this
201 /// operation, this object has an undefined, but valid, state. Note
202 /// that no version is read from `stream`. See the `bslx` package-level
203 /// documentation for more information on BDEX streaming of
204 /// value-semantic types and containers.
205 template <class STREAM>
206 STREAM& bdexStreamIn(STREAM& stream, int version);
207
208 /// Return the current value of this checksum and set the value of this
209 /// checksum to the value the default constructor provides.
210 unsigned int checksumAndReset();
211
212 /// Reset the value of this checksum to the value the default
213 /// constructor provides.
214 void reset();
215
216 /// Update the value of this checksum to incorporate the specified
217 /// `data` having the specified `length`. If the current state is the
218 /// default state, the resultant value of this checksum is the
219 /// application of the CRC-32 algorithm upon the currently given `data`
220 /// of the given `length`. If this checksum has been previously
221 /// provided data and has not been subsequently reset, the current state
222 /// is not the default state and the resultant value is equivalent to
223 /// applying the CRC-32 algorithm upon the concatenation of all the
224 /// provided data. Note that if `data` is 0, then `length` also must be
225 /// 0.
226 void update(const void *data, bsl::size_t length);
227
228 // ACCESSORS
229
230 /// Write this value to the specified output `stream` using the
231 /// specified `version` format, and return a reference to `stream`.
232 /// If `stream` is initially invalid, this operation has no effect.
233 /// If `version` is not supported, `stream` is invalidated but
234 /// otherwise unmodified. Note that `version` is not written to
235 /// `stream`. See the `bslx` package-level documentation for more
236 /// information on BDEX streaming of value-semantic types and
237 /// containers.
238 template <class STREAM>
239 STREAM& bdexStreamOut(STREAM& stream, int version) const;
240
241 /// Return the current value of this checksum.
242 unsigned int checksum() const;
243
244 /// Format this object to the specified output `stream` at the (absolute
245 /// value of) the optionally specified indentation `level` and return a
246 /// reference to `stream`. If `level` is specified, optionally specify
247 /// `spacesPerLevel`, the number of spaces per indentation level for
248 /// this and all of its nested objects. If `level` is negative,
249 /// suppress indentation of the first line. If `spacesPerLevel` is
250 /// negative, format the entire output on one line, suppressing all but
251 /// the initial indentation (as governed by `level`). If `stream` is
252 /// not valid on entry, this operation has no effect.
253 bsl::ostream& print(bsl::ostream& stream) const;
254
255#ifndef BDE_OMIT_INTERNAL_DEPRECATED
256 // CLASS METHOD
257
258 /// Return the most current `bdex` streaming version number supported by
259 /// this class. (See the package-group-level documentation for more
260 /// information on `bdex` streaming of container types.)
261 static int maxSupportedBdexVersion();
262
263 // ACCESSOR
264
265 /// Return the current value of this checksum.
266 ///
267 /// @deprecated use method `checksum` instead.
268 unsigned int view() const;
269#endif // BDE_OMIT_INTERNAL_DEPRECATED
270
271};
272
273// FREE OPERATORS
274
275/// Return `true` if the specified `lhs` and `rhs` checksums have the same
276/// value, and `false` otherwise. Two checksums have the same value if the
277/// values obtained from their `checksum` methods are identical.
278bool operator==(const Crc32& lhs, const Crc32& rhs);
279
280/// Return `true` if the specified `lhs` and `rhs` checksums do not have the
281/// same value, and `false` otherwise. Two checksums do not have the same
282/// value if the values obtained from their `checksum` methods differ.
283bool operator!=(const Crc32& lhs, const Crc32& rhs);
284
285/// Write to the specified output `stream` the specified `checksum` value
286/// and return a reference to the modifiable `stream`.
287bsl::ostream& operator<<(bsl::ostream& stream, const Crc32& checksum);
288
289// ============================================================================
290// INLINE FUNCTION DEFINITIONS
291// ============================================================================
292
293 // -----------
294 // class Crc32
295 // -----------
296
297// CLASS METHODS
298inline
300{
301 return 1;
302}
303
304// CREATORS
305inline
307: d_crc(0xffffffff)
308{
309}
310
311inline
312Crc32::Crc32(const void *data, bsl::size_t length)
313: d_crc(0xffffffff)
314{
315 update(data, length);
316}
317
318inline
319Crc32::Crc32(const Crc32& original)
320: d_crc(original.d_crc)
321{
322}
323
324// MANIPULATORS
325inline
327{
328 d_crc = rhs.d_crc;
329 return *this;
330}
331
332template <class STREAM>
333STREAM& Crc32::bdexStreamIn(STREAM& stream, int version)
334{
335 if (stream) {
336 switch (version) {
337 case 1: {
338 unsigned int crc;
339 stream.getUint32(crc);
340 if (!stream) {
341 return stream; // RETURN
342 }
343 d_crc = crc;
344 } break;
345 default: {
346 stream.invalidate();
347 } break;
348 }
349 }
350 return stream;
351}
352
353inline
355{
356 const unsigned int crc = d_crc;
357 d_crc = 0xffffffff;
358 return crc ^ 0xffffffff;
359}
360
361inline
363{
364 d_crc = 0xffffffff;
365}
366
367// ACCESSORS
368template <class STREAM>
369STREAM& Crc32::bdexStreamOut(STREAM& stream, int version) const
370{
371 switch (version) {
372 case 1: {
373 stream.putUint32(d_crc);
374 } break;
375 default: {
376 stream.invalidate();
377 } break;
378 }
379 return stream;
380}
381
382inline
383unsigned int Crc32::checksum() const
384{
385 return d_crc ^ 0xffffffff;
386}
387
388#ifndef BDE_OMIT_INTERNAL_DEPRECATED
389
390// CLASS METHODS
391inline
396
397// ACCESSORS
398inline
399unsigned int Crc32::view() const
400{
401 return d_crc ^ 0xffffffff;
402}
403
404#endif // BDE_OMIT_INTERNAL_DEPRECATED
405
406} // close package namespace
407
408// FREE OPERATORS
409inline
410bool bdlde::operator==(const Crc32& lhs, const Crc32& rhs)
411{
412 return lhs.d_crc == rhs.d_crc;
413}
414
415inline
416bool bdlde::operator!=(const Crc32& lhs, const Crc32& rhs)
417{
418 return !(lhs == rhs);
419}
420
421inline
422bsl::ostream& bdlde::operator<<(bsl::ostream& stream, const Crc32& checksum)
423{
424 return checksum.print(stream);
425}
426
427
428
429#endif
430
431// ----------------------------------------------------------------------------
432// Copyright 2018 Bloomberg Finance L.P.
433//
434// Licensed under the Apache License, Version 2.0 (the "License");
435// you may not use this file except in compliance with the License.
436// You may obtain a copy of the License at
437//
438// http://www.apache.org/licenses/LICENSE-2.0
439//
440// Unless required by applicable law or agreed to in writing, software
441// distributed under the License is distributed on an "AS IS" BASIS,
442// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
443// See the License for the specific language governing permissions and
444// limitations under the License.
445// ----------------------------- END-OF-FILE ----------------------------------
446
447/** @} */
448/** @} */
449/** @} */
Definition bdlde_crc32.h:152
~Crc32()=default
Destroy this object.
Crc32()
Definition bdlde_crc32.h:306
void reset()
Definition bdlde_crc32.h:362
STREAM & bdexStreamIn(STREAM &stream, int version)
Definition bdlde_crc32.h:333
static int maxSupportedBdexVersion()
Definition bdlde_crc32.h:392
friend bool operator==(const Crc32 &, const Crc32 &)
unsigned int view() const
Definition bdlde_crc32.h:399
unsigned int checksum() const
Return the current value of this checksum.
Definition bdlde_crc32.h:383
Crc32 & operator=(const Crc32 &rhs)
Definition bdlde_crc32.h:326
STREAM & bdexStreamOut(STREAM &stream, int version) const
Definition bdlde_crc32.h:369
void update(const void *data, bsl::size_t length)
unsigned int checksumAndReset()
Definition bdlde_crc32.h:354
bsl::ostream & print(bsl::ostream &stream) const
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlde_base64alphabet.h:118
bool operator!=(const Base64DecoderOptions &lhs, const Base64DecoderOptions &rhs)
bsl::ostream & operator<<(bsl::ostream &stream, Base64Alphabet::Enum value)
bool operator==(const Base64DecoderOptions &lhs, const Base64DecoderOptions &rhs)