1 | // |
---|
2 | // Checksum.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/Checksum.h#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Core |
---|
8 | // Module: Checksum |
---|
9 | // |
---|
10 | // Definition of the Checksum class. |
---|
11 | // |
---|
12 | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_Checksum_INCLUDED |
---|
40 | #define Foundation_Checksum_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | |
---|
45 | |
---|
46 | namespace Poco { |
---|
47 | |
---|
48 | |
---|
49 | class Foundation_API Checksum |
---|
50 | /// This class calculates CRC-32 or Adler-32 checksums |
---|
51 | /// for arbitrary data. |
---|
52 | /// |
---|
53 | /// A cyclic redundancy check (CRC) is a type of hash function, which is used to produce a |
---|
54 | /// small, fixed-size checksum of a larger block of data, such as a packet of network |
---|
55 | /// traffic or a computer file. CRC-32 is one of the most commonly used CRC algorithms. |
---|
56 | /// |
---|
57 | /// Adler-32 is a checksum algorithm which was invented by Mark Adler. |
---|
58 | /// It is almost as reliable as a 32-bit cyclic redundancy check for protecting against |
---|
59 | /// accidental modification of data, such as distortions occurring during a transmission, |
---|
60 | /// but is significantly faster to calculate in software. |
---|
61 | |
---|
62 | { |
---|
63 | public: |
---|
64 | enum Type |
---|
65 | { |
---|
66 | TYPE_ADLER32 = 0, |
---|
67 | TYPE_CRC32 |
---|
68 | }; |
---|
69 | |
---|
70 | Checksum(); |
---|
71 | /// Creates a CRC-32 checksum initialized to 0. |
---|
72 | |
---|
73 | Checksum(Type t); |
---|
74 | /// Creates the Checksum, using the given type. |
---|
75 | |
---|
76 | ~Checksum(); |
---|
77 | /// Destroys the Checksum. |
---|
78 | |
---|
79 | void update(const char* data, unsigned length); |
---|
80 | /// Updates the checksum with the given data. |
---|
81 | |
---|
82 | void update(const std::string& data); |
---|
83 | /// Updates the checksum with the given data. |
---|
84 | |
---|
85 | void update(char data); |
---|
86 | /// Updates the checksum with the given data. |
---|
87 | |
---|
88 | Poco::UInt32 checksum() const; |
---|
89 | /// Returns the calculated checksum. |
---|
90 | |
---|
91 | Type type() const; |
---|
92 | /// Which type of checksum are we calulcating |
---|
93 | |
---|
94 | private: |
---|
95 | Type _type; |
---|
96 | Poco::UInt32 _value; |
---|
97 | }; |
---|
98 | |
---|
99 | |
---|
100 | // |
---|
101 | // inlines |
---|
102 | // |
---|
103 | inline void Checksum::update(const std::string& data) |
---|
104 | { |
---|
105 | update(data.c_str(), static_cast<unsigned int>(data.size())); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | inline void Checksum::update(char c) |
---|
110 | { |
---|
111 | update(&c, 1); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | inline Poco::UInt32 Checksum::checksum() const |
---|
116 | { |
---|
117 | return _value; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | inline Checksum::Type Checksum::type() const |
---|
122 | { |
---|
123 | return _type; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | } // namespace Poco |
---|
128 | |
---|
129 | |
---|
130 | #endif // Foundation_Checksum_INCLUDED |
---|