BDE 4.39.x 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` method.
165 ///
166 /// \note Note that the `versionSelector` is expected to be formatted
167 /// as `yyyymmdd`, a date representation. See the `bslx` package-level
168 /// documentation for more information on BDEX streaming of
169 /// value-semantic types and containers.
170 static int maxSupportedBdexVersion(int versionSelector);
171
172 // CREATORS
173
174 /// Construct a checksum having the value corresponding to no data
175 /// having been provided (i.e., having the value 0).
176 Crc32();
177
178 /// Construct a checksum corresponding to the specified `data` having the specified `length` (in bytes).
179 ///
180 /// \note Note that if `data` is 0, then
181 /// `length` also must be 0.
182 Crc32(const void *data, bsl::size_t length);
183
184 /// Construct a checksum having the value of the specified `original`
185 /// checksum.
186 Crc32(const Crc32& original);
187
188 /// Destroy this object.
189 ~Crc32() = default;
190
191 // MANIPULATORS
192
193 /// Assign to this checksum the value of the specified `rhs` checksum,
194 /// and return a reference to this modifiable checksum.
195 Crc32& operator=(const Crc32& rhs);
196
197 /// Assign to this object the value read from the specified input
198 /// `stream` using the specified `version` format, and return a
199 /// reference to `stream`. If `stream` is initially invalid, this
200 /// operation has no effect. If `version` is not supported, this object
201 /// is unaltered and `stream` is invalidated but otherwise unmodified.
202 /// If `version` is supported but `stream` becomes invalid during this
203 /// operation, this object has an undefined, but valid, state.
204 ///
205 /// \note Note that no version is read from `stream`. See the `bslx` package-level
206 /// documentation for more information on BDEX streaming of
207 /// value-semantic types and containers.
208 template <class STREAM>
209 STREAM& bdexStreamIn(STREAM& stream, int version);
210
211 /// Return the current value of this checksum and set the value of this
212 /// checksum to the value the default constructor provides.
213 unsigned int checksumAndReset();
214
215 /// Reset the value of this checksum to the value the default
216 /// constructor provides.
217 void reset();
218
219 /// Update the value of this checksum to incorporate the specified
220 /// `data` having the specified `length`. If the current state is the
221 /// default state, the resultant value of this checksum is the
222 /// application of the CRC-32 algorithm upon the currently given `data`
223 /// of the given `length`. If this checksum has been previously
224 /// provided data and has not been subsequently reset, the current state
225 /// is not the default state and the resultant value is equivalent to
226 /// applying the CRC-32 algorithm upon the concatenation of all the provided data.
227 ///
228 /// \note Note that if `data` is 0, then `length` also must be
229 /// 0.
230 void update(const void *data, bsl::size_t length);
231
232 // ACCESSORS
233
234 /// Write this value to the specified output `stream` using the
235 /// specified `version` format, and return a reference to `stream`.
236 /// If `stream` is initially invalid, this operation has no effect.
237 /// If `version` is not supported, `stream` is invalidated but otherwise unmodified.
238 ///
239 /// \note Note that `version` is not written to
240 /// `stream`. See the `bslx` package-level documentation for more
241 /// information on BDEX streaming of value-semantic types and
242 /// containers.
243 template <class STREAM>
244 STREAM& bdexStreamOut(STREAM& stream, int version) const;
245
246 /// Return the current value of this checksum.
247 unsigned int checksum() const;
248
249 /// Format this object to the specified output `stream` at the (absolute
250 /// value of) the optionally specified indentation `level` and return a
251 /// reference to `stream`. If `level` is specified, optionally specify
252 /// `spacesPerLevel`, the number of spaces per indentation level for
253 /// this and all of its nested objects. If `level` is negative,
254 /// suppress indentation of the first line. If `spacesPerLevel` is
255 /// negative, format the entire output on one line, suppressing all but
256 /// the initial indentation (as governed by `level`). If `stream` is
257 /// not valid on entry, this operation has no effect.
258 bsl::ostream& print(bsl::ostream& stream) const;
259
260#ifndef BDE_OMIT_INTERNAL_DEPRECATED
261 // CLASS METHOD
262
263 /// Return the most current `bdex` streaming version number supported by
264 /// this class. (See the package-group-level documentation for more
265 /// information on `bdex` streaming of container types.)
266 static int maxSupportedBdexVersion();
267
268 // ACCESSOR
269
270 /// Return the current value of this checksum.
271 ///
272 /// @deprecated use method `checksum` instead.
273 unsigned int view() const;
274#endif // BDE_OMIT_INTERNAL_DEPRECATED
275
276};
277
278// FREE OPERATORS
279
280/// Return `true` if the specified `lhs` and `rhs` checksums have the same
281/// value, and `false` otherwise. Two checksums have the same value if the
282/// values obtained from their `checksum` methods are identical.
283bool operator==(const Crc32& lhs, const Crc32& rhs);
284
285/// Return `true` if the specified `lhs` and `rhs` checksums do not have the
286/// same value, and `false` otherwise. Two checksums do not have the same
287/// value if the values obtained from their `checksum` methods differ.
288bool operator!=(const Crc32& lhs, const Crc32& rhs);
289
290/// Write to the specified output `stream` the specified `checksum` value
291/// and return a reference to the modifiable `stream`.
292bsl::ostream& operator<<(bsl::ostream& stream, const Crc32& checksum);
293
294// ============================================================================
295// INLINE FUNCTION DEFINITIONS
296// ============================================================================
297
298 // -----------
299 // class Crc32
300 // -----------
301
302// CLASS METHODS
303inline
305{
306 return 1;
307}
308
309// CREATORS
310inline
312: d_crc(0xffffffff)
313{
314}
315
316inline
317Crc32::Crc32(const void *data, bsl::size_t length)
318: d_crc(0xffffffff)
319{
320 update(data, length);
321}
322
323inline
324Crc32::Crc32(const Crc32& original)
325: d_crc(original.d_crc)
326{
327}
328
329// MANIPULATORS
330inline
332{
333 d_crc = rhs.d_crc;
334 return *this;
335}
336
337template <class STREAM>
338STREAM& Crc32::bdexStreamIn(STREAM& stream, int version)
339{
340 if (stream) {
341 switch (version) {
342 case 1: {
343 unsigned int crc;
344 stream.getUint32(crc);
345 if (!stream) {
346 return stream; // RETURN
347 }
348 d_crc = crc;
349 } break;
350 default: {
351 stream.invalidate();
352 } break;
353 }
354 }
355 return stream;
356}
357
358inline
360{
361 const unsigned int crc = d_crc;
362 d_crc = 0xffffffff;
363 return crc ^ 0xffffffff;
364}
365
366inline
368{
369 d_crc = 0xffffffff;
370}
371
372// ACCESSORS
373template <class STREAM>
374STREAM& Crc32::bdexStreamOut(STREAM& stream, int version) const
375{
376 switch (version) {
377 case 1: {
378 stream.putUint32(d_crc);
379 } break;
380 default: {
381 stream.invalidate();
382 } break;
383 }
384 return stream;
385}
386
387inline
388unsigned int Crc32::checksum() const
389{
390 return d_crc ^ 0xffffffff;
391}
392
393#ifndef BDE_OMIT_INTERNAL_DEPRECATED
394
395// CLASS METHODS
396inline
401
402// ACCESSORS
403inline
404unsigned int Crc32::view() const
405{
406 return d_crc ^ 0xffffffff;
407}
408
409#endif // BDE_OMIT_INTERNAL_DEPRECATED
410
411} // close package namespace
412
413// FREE OPERATORS
414inline
415bool bdlde::operator==(const Crc32& lhs, const Crc32& rhs)
416{
417 return lhs.d_crc == rhs.d_crc;
418}
419
420inline
421bool bdlde::operator!=(const Crc32& lhs, const Crc32& rhs)
422{
423 return !(lhs == rhs);
424}
425
426inline
427bsl::ostream& bdlde::operator<<(bsl::ostream& stream, const Crc32& checksum)
428{
429 return checksum.print(stream);
430}
431
432
433
434#endif
435
436// ----------------------------------------------------------------------------
437// Copyright 2018 Bloomberg Finance L.P.
438//
439// Licensed under the Apache License, Version 2.0 (the "License");
440// you may not use this file except in compliance with the License.
441// You may obtain a copy of the License at
442//
443// http://www.apache.org/licenses/LICENSE-2.0
444//
445// Unless required by applicable law or agreed to in writing, software
446// distributed under the License is distributed on an "AS IS" BASIS,
447// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
448// See the License for the specific language governing permissions and
449// limitations under the License.
450// ----------------------------- END-OF-FILE ----------------------------------
451
452/** @} */
453/** @} */
454/** @} */
Definition bdlde_crc32.h:152
~Crc32()=default
Destroy this object.
Crc32()
Definition bdlde_crc32.h:311
void reset()
Definition bdlde_crc32.h:367
STREAM & bdexStreamIn(STREAM &stream, int version)
Definition bdlde_crc32.h:338
static int maxSupportedBdexVersion()
Definition bdlde_crc32.h:397
friend bool operator==(const Crc32 &, const Crc32 &)
unsigned int view() const
Definition bdlde_crc32.h:404
unsigned int checksum() const
Return the current value of this checksum.
Definition bdlde_crc32.h:388
Crc32 & operator=(const Crc32 &rhs)
Definition bdlde_crc32.h:331
STREAM & bdexStreamOut(STREAM &stream, int version) const
Definition bdlde_crc32.h:374
void update(const void *data, bsl::size_t length)
unsigned int checksumAndReset()
Definition bdlde_crc32.h:359
bsl::ostream & print(bsl::ostream &stream) const
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
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)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917