1 | // |
2 | // Checksum.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Core |
6 | // Module: Checksum |
7 | // |
8 | // Definition of the Checksum 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_Checksum_INCLUDED |
18 | #define Foundation_Checksum_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | #include "Poco/ChecksumImpl.h" |
23 | |
24 | |
25 | namespace Poco { |
26 | |
27 | |
28 | class Foundation_API Checksum |
29 | /// This class calculates checksums for arbitrary data. |
30 | { |
31 | public: |
32 | enum Type |
33 | { |
34 | TYPE_ADLER32 = ChecksumImpl::TYPE_ADLER32_IMPL, |
35 | TYPE_CRC32 = ChecksumImpl::TYPE_CRC32_IMPL, |
36 | TYPE_CRC64 = ChecksumImpl::TYPE_CRC64_IMPL |
37 | }; |
38 | |
39 | Checksum(); |
40 | /// Creates a CRC-32 checksum initialized to 0. |
41 | |
42 | Checksum(Type t); |
43 | /// Creates the Checksum, using the given type. |
44 | |
45 | ~Checksum(); |
46 | /// Destroys the Checksum. |
47 | |
48 | void update(const char* data, unsigned length); |
49 | /// Updates the checksum with the given data. |
50 | |
51 | void update(const std::string& data); |
52 | /// Updates the checksum with the given data. |
53 | |
54 | void update(char data); |
55 | /// Updates the checksum with the given data. |
56 | |
57 | Poco::UInt64 checksum() const; |
58 | /// Returns the calculated checksum. |
59 | |
60 | Type type() const; |
61 | /// Which type of checksum are we calculating. |
62 | |
63 | private: |
64 | ChecksumImpl* _pImpl; |
65 | }; |
66 | |
67 | |
68 | // |
69 | // inlines |
70 | // |
71 | |
72 | inline void Checksum::update(const std::string& data) |
73 | { |
74 | _pImpl->update(data.c_str(), static_cast<unsigned int>(data.size())); |
75 | } |
76 | |
77 | |
78 | inline void Checksum::update(char c) |
79 | { |
80 | _pImpl->update(&c, 1); |
81 | } |
82 | |
83 | |
84 | inline void Checksum::update(const char* data, unsigned length) |
85 | { |
86 | _pImpl->update(data, length); |
87 | } |
88 | |
89 | |
90 | inline Poco::UInt64 Checksum::checksum() const |
91 | { |
92 | return _pImpl->checksum(); |
93 | } |
94 | |
95 | |
96 | inline Checksum::Type Checksum::type() const |
97 | { |
98 | return static_cast<Checksum::Type>(_pImpl->type()); |
99 | } |
100 | |
101 | |
102 | } // namespace Poco |
103 | |
104 | |
105 | #endif // Foundation_Checksum_INCLUDED |
106 | |