| 1 | // |
| 2 | // Checksum64.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Core |
| 6 | // Module: Checksum64 |
| 7 | // |
| 8 | // Definition of the Checksum64 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_Checksum64_INCLUDED |
| 18 | #define Foundation_Checksum64_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Foundation.h" |
| 22 | #include "Poco/ChecksumImpl.h" |
| 23 | |
| 24 | |
| 25 | namespace Poco { |
| 26 | |
| 27 | |
| 28 | class Foundation_API Checksum64 : public ChecksumImpl |
| 29 | /// This class calculates CRC-64 checksums for arbitrary data. |
| 30 | /// |
| 31 | /// The algorithm and table are based on the polynomial |
| 32 | /// |
| 33 | /// x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 + |
| 34 | /// x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 + |
| 35 | /// x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 + |
| 36 | /// x^7 + x^4 + x + 1 |
| 37 | /// |
| 38 | /// Implementation is adapted from the DLT1 spec: |
| 39 | /// ECMA - 182, http://www.ecma-international.org/publications/standards/Ecma-182.htm |
| 40 | /// |
| 41 | { |
| 42 | public: |
| 43 | Checksum64(); |
| 44 | /// Creates a CRC-64 checksum initialized to 0. |
| 45 | |
| 46 | Checksum64(Type t); |
| 47 | /// Creates the Checksum64, using the given type. |
| 48 | |
| 49 | ~Checksum64(); |
| 50 | /// Destroys the Checksum64. |
| 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 | static Poco::UInt64 _initCRC64Val; |
| 69 | static Poco::UInt64 _finXORVal; |
| 70 | static const unsigned _tableSize = 256; |
| 71 | static Poco::UInt64 _crcTable[_tableSize]; |
| 72 | Poco::UInt64 _value; |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | // |
| 77 | // inlines |
| 78 | // |
| 79 | inline void Checksum64::update(const std::string& data) |
| 80 | { |
| 81 | update(data.c_str(), static_cast<unsigned int>(data.size())); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | inline void Checksum64::update(char c) |
| 86 | { |
| 87 | update(&c, 1); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | inline Poco::UInt64 Checksum64::checksum() const |
| 92 | { |
| 93 | return _value; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | inline ChecksumImpl::Type Checksum64::type() const |
| 98 | { |
| 99 | return TYPE_CRC64_IMPL; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | } // namespace Poco |
| 104 | |
| 105 | |
| 106 | #endif // Foundation_Checksum64_INCLUDED |
| 107 | |