1 | // |
2 | // Checksum32.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Core |
6 | // Module: Checksum32 |
7 | // |
8 | // Definition of the Checksum32 class. |
9 | // |
10 | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Foundation_Checksum32_INCLUDED |
18 | #define Foundation_Checksum32_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | #include "Poco/ChecksumImpl.h" |
23 | |
24 | |
25 | namespace Poco { |
26 | |
27 | |
28 | class Foundation_API Checksum32 : public ChecksumImpl |
29 | /// This class calculates CRC-32 or Adler-32 checksums |
30 | /// for arbitrary data. |
31 | /// |
32 | /// A cyclic redundancy check (CRC) is a type of hash function, which is used to produce a |
33 | /// small, fixed-size checksum of a larger block of data, such as a packet of network |
34 | /// traffic or a computer file. CRC-32 is one of the most commonly used CRC algorithms. |
35 | /// |
36 | /// Adler-32 is a checksum algorithm which was invented by Mark Adler. |
37 | /// It is almost as reliable as a 32-bit cyclic redundancy check for protecting against |
38 | /// accidental modification of data, such as distortions occurring during a transmission, |
39 | /// but is significantly faster to calculate in software. |
40 | |
41 | { |
42 | public: |
43 | Checksum32(); |
44 | /// Creates a CRC-32 checksum initialized to 0. |
45 | |
46 | Checksum32(ChecksumImpl::Type t); |
47 | /// Creates the Checksum32, using the given type. |
48 | |
49 | ~Checksum32(); |
50 | /// Destroys the Checksum32. |
51 | |
52 | void update(const char* data, unsigned length); |
53 | /// Updates the checksum with the given data. |
54 | |
55 | void update(const std::string& data); |
56 | /// Updates the checksum with the given data. |
57 | |
58 | void update(char data); |
59 | /// Updates the checksum with the given data. |
60 | |
61 | Poco::UInt64 checksum() const; |
62 | /// Returns the calculated checksum. |
63 | |
64 | ChecksumImpl::Type type() const; |
65 | /// Which type of checksum are we calculating. |
66 | |
67 | private: |
68 | Type _type; |
69 | Poco::UInt32 _value; |
70 | }; |
71 | |
72 | |
73 | // |
74 | // inlines |
75 | // |
76 | inline void Checksum32::update(const std::string& data) |
77 | { |
78 | update(data.c_str(), static_cast<unsigned int>(data.size())); |
79 | } |
80 | |
81 | |
82 | inline void Checksum32::update(char c) |
83 | { |
84 | update(&c, 1); |
85 | } |
86 | |
87 | |
88 | inline Poco::UInt64 Checksum32::checksum() const |
89 | { |
90 | return _value; |
91 | } |
92 | |
93 | |
94 | inline ChecksumImpl::Type Checksum32::type() const |
95 | { |
96 | return _type; |
97 | } |
98 | |
99 | |
100 | } // namespace Poco |
101 | |
102 | |
103 | #endif // Foundation_Checksum32_INCLUDED |
104 | |