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