diff --git a/checksum.h b/checksum.h index 44633267..61a6c24d 100644 --- a/checksum.h +++ b/checksum.h @@ -32,6 +32,7 @@ SOFTWARE. #include "static_assert.h" #include "type_traits.h" #include "binary.h" +#include "frame_check_sequence.h" ///\defgroup checksum Checksum calculation ///\ingroup maths @@ -44,6 +45,8 @@ namespace etl template struct checksum_policy_sum { + typedef T value_type; + inline T initial() const { return 0; @@ -66,6 +69,8 @@ namespace etl template struct checksum_policy_bsd { + typedef T value_type; + inline T initial() const { return 0; @@ -88,6 +93,8 @@ namespace etl template struct checksum_policy_xor { + typedef T value_type; + inline T initial() const { return 0; @@ -104,22 +111,14 @@ namespace etl } }; - //*************************************************************************** - /// Calculates a checksum according to the specified policy. - ///\tparam TSum The type used for the sum. - ///\tparam TPolicy The type used to enact the policy. Default = checksum_policy_sum - ///\ingroup checksum - //*************************************************************************** - template > - class checksum + //************************************************************************* + /// Standard Checksum. + //************************************************************************* + template + class checksum : public etl::frame_check_sequence > { public: - STATIC_ASSERT(etl::is_unsigned::value, "Signed TSum template parameter not supported"); - - typedef TSum value_type; - typedef TPolicy policy_type; - //************************************************************************* /// Default constructor. //************************************************************************* @@ -141,66 +140,13 @@ namespace etl reset(); add(begin, end); } - - //************************************************************************* - /// Resets the CRC to the initial state. - //************************************************************************* - void reset() - { - sum = policy.initial(); - } - - //************************************************************************* - /// Adds a range. - /// \param begin - /// \param end - //************************************************************************* - template - void add(TIterator begin, const TIterator end) - { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - - while (begin != end) - { - sum = policy.add(sum, *begin++); - } - } - - //************************************************************************* - /// \param value The uint8_t to add to the checksum. - //************************************************************************* - void add(uint8_t value) - { - sum = policy.add(sum, value); - } - - //************************************************************************* - /// Gets the checksum value. - //************************************************************************* - value_type value() const - { - return policy.final(sum); - } - - //************************************************************************* - /// Conversion operator to value_type. - //************************************************************************* - operator value_type () const - { - return policy.final(sum); - } - - private: - - value_type sum; - policy_type policy; }; //************************************************************************* /// BSD Checksum. //************************************************************************* template - class bsd_checksum : public etl::checksum > + class bsd_checksum : public etl::frame_check_sequence > { public: @@ -231,7 +177,7 @@ namespace etl /// XOR Checksum. //************************************************************************* template - class xor_checksum : public etl::checksum > + class xor_checksum : public etl::frame_check_sequence > { public: diff --git a/crc16.h b/crc16.h index 7261cf29..96688d97 100644 --- a/crc16.h +++ b/crc16.h @@ -32,9 +32,10 @@ SOFTWARE. #define __ETL_CRC16__ #include +#include #include "static_assert.h" -#include "type_traits.h" +#include "frame_check_sequence.h" #if defined(COMPILER_KEIL) #pragma diag_suppress 1300 @@ -52,16 +53,36 @@ namespace etl extern const uint16_t CRC16[]; //*************************************************************************** + /// CRC16 policy. /// Calculates CRC16 using polynomial 0x8005. - /// \ingroup crc16 //*************************************************************************** - class crc16 + struct crc_policy_16 + { + typedef uint16_t value_type; + + inline uint16_t initial() const + { + return 0; + } + + inline uint16_t add(uint16_t crc, uint8_t value) const + { + return (crc >> 8) ^ CRC16[(crc ^ value) & 0xFF]; + } + + inline uint16_t final(uint16_t crc) const + { + return crc; + } + }; + + //************************************************************************* + /// CRC16 + //************************************************************************* + class crc16 : public etl::frame_check_sequence { public: - typedef uint16_t value_type; - typedef uint16_t argument_type; - //************************************************************************* /// Default constructor. //************************************************************************* @@ -81,64 +102,8 @@ namespace etl STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); reset(); - while (begin != end) - { - crc = (crc >> 8) ^ CRC16[(crc ^ *begin++) & 0xFF]; - } + add(begin, end); } - - //************************************************************************* - /// Resets the CRC to the initial state. - //************************************************************************* - void reset() - { - crc = 0; - } - - //************************************************************************* - /// Adds a range. - /// \param begin - /// \param end - //************************************************************************* - template - void add(TIterator begin, const TIterator end) - { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - - while (begin != end) - { - crc = (crc >> 8) ^ CRC16[(crc ^ *begin++) & 0xFF]; - } - } - - //************************************************************************* - /// \param value The uint8_t to add to the CRC. - /// \return The CRC result. - //************************************************************************* - void add(uint8_t value) - { - crc = (crc >> 8) ^ CRC16[(crc ^ value) & 0xFF]; - } - - //************************************************************************* - /// Gets the CRC value. - //************************************************************************* - value_type value() const - { - return crc; - } - - //************************************************************************* - /// Conversion operator to value_type. - //************************************************************************* - operator value_type () const - { - return crc; - } - - private: - - value_type crc; }; } diff --git a/crc16_ccitt.h b/crc16_ccitt.h index ca0769bc..b8ab06a8 100644 --- a/crc16_ccitt.h +++ b/crc16_ccitt.h @@ -32,9 +32,10 @@ SOFTWARE. #define __ETL_CRC16_CCITT__ #include +#include #include "static_assert.h" -#include "type_traits.h" +#include "frame_check_sequence.h" #if defined(COMPILER_KEIL) #pragma diag_suppress 1300 @@ -50,18 +51,38 @@ namespace etl /// \ingroup crc16_ccitt //*************************************************************************** extern const uint16_t CRC_CCITT[]; + + //*************************************************************************** + /// CRC16 CCITT policy. + /// Calculates CRC16 CCITT using polynomial 0x1021 + //*************************************************************************** + struct crc_policy_16_ccitt + { + typedef uint16_t value_type; - //*************************************************************************** - /// Calculates CRC-CCITT using polynomial 0x1021 - /// \ingroup crc16_ccitt - //*************************************************************************** - class crc16_ccitt + inline uint16_t initial() const + { + return 0xFFFF; + } + + inline uint16_t add(uint16_t crc, uint8_t value) const + { + return (crc << 8) ^ CRC_CCITT[((crc >> 8) ^ value) & 0xFF]; + } + + inline uint16_t final(uint16_t crc) const + { + return crc; + } + }; + + //************************************************************************* + /// CRC16 CCITT + //************************************************************************* + class crc16_ccitt : public etl::frame_check_sequence { public: - typedef uint16_t value_type; - typedef uint16_t argument_type; - //************************************************************************* /// Default constructor. //************************************************************************* @@ -81,63 +102,8 @@ namespace etl STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); reset(); - while (begin != end) - { - crc = (crc << 8) ^ CRC_CCITT[((crc >> 8) ^ *begin++) & 0xFF]; - } + add(begin, end); } - - //************************************************************************* - /// Resets the CRC to the initial state. - //************************************************************************* - void reset() - { - crc = 0xFFFF; - } - - //************************************************************************* - /// Adds a range. - /// \param begin - /// \param end - //************************************************************************* - template - void add(TIterator begin, const TIterator end) - { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - - while (begin != end) - { - crc = (crc << 8) ^ CRC_CCITT[((crc >> 8) ^ *begin++) & 0xFF]; - } - } - - //************************************************************************* - /// \param value The uint8_t to add to the CRC. - //************************************************************************* - void add(uint8_t value) - { - crc = (crc << 8) ^ CRC_CCITT[((crc >> 8) ^ value) & 0xFF]; - } - - //************************************************************************* - /// Gets the CRC value. - //************************************************************************* - value_type value() const - { - return crc; - } - - //************************************************************************* - /// Conversion operator to value_type. - //************************************************************************* - operator value_type () const - { - return crc; - } - - private: - - value_type crc; }; } diff --git a/crc16_kermit.h b/crc16_kermit.h index 337843c6..94d94adb 100644 --- a/crc16_kermit.h +++ b/crc16_kermit.h @@ -28,13 +28,14 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ -#ifndef __etl_crc16_kermit__ -#define __etl_crc16_kermit__ +#ifndef __ETL_CRC16_KERMIT__ +#define __ETL_CRC16_KERMIT__ #include +#include #include "static_assert.h" -#include "type_traits.h" +#include "frame_check_sequence.h" #if defined(COMPILER_KEIL) #pragma diag_suppress 1300 @@ -50,18 +51,38 @@ namespace etl /// \ingroup crc //*************************************************************************** extern const uint16_t CRC_KERMIT[]; - + //*************************************************************************** - /// Calculates CRC-Kermit using polynomial 0x1021 - /// \ingroup crc16_kermit + /// CRC16 Kermit policy. + /// Calculates CRC16 Kermit using polynomial 0x1021 //*************************************************************************** - class crc16_kermit + struct crc_policy_16_kermit + { + typedef uint16_t value_type; + + inline uint16_t initial() const + { + return 0; + } + + inline uint16_t add(uint16_t crc, uint8_t value) const + { + return (crc >> 8) ^ CRC_KERMIT[(crc ^ value) & 0xFF]; + } + + inline uint16_t final(uint16_t crc) const + { + return crc; + } + }; + + //************************************************************************* + /// CRC16 Kermit + //************************************************************************* + class crc16_kermit : public etl::frame_check_sequence { public: - typedef uint16_t value_type; - typedef uint16_t argument_type; - //************************************************************************* /// Default constructor. //************************************************************************* @@ -81,63 +102,8 @@ namespace etl STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); reset(); - while (begin != end) - { - crc = (crc >> 8) ^ CRC_KERMIT[(crc ^ *begin++) & 0xFF]; - } + add(begin, end); } - - //************************************************************************* - /// Resets the CRC to the initial state. - //************************************************************************* - void reset() - { - crc = 0; - } - - //************************************************************************* - /// Adds a range. - /// \param begin - /// \param end - //************************************************************************* - template - void add(TIterator begin, const TIterator end) - { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - - while (begin != end) - { - crc = (crc >> 8) ^ CRC_KERMIT[(crc ^ *begin++) & 0xFF]; - } - } - - //************************************************************************* - /// \param value The uint8_t to add to the CRC. - //************************************************************************* - void add(uint8_t value) - { - crc = (crc >> 8) ^ CRC_KERMIT[(crc ^ value) & 0xFF]; - } - - //************************************************************************* - /// Gets the CRC value. - //************************************************************************* - value_type value() const - { - return crc; - } - - //************************************************************************* - /// Conversion operator to value_type - //************************************************************************* - operator value_type () const - { - return crc; - } - - private: - - value_type crc; }; } diff --git a/crc32.h b/crc32.h index ecf9a5d7..7359ac45 100644 --- a/crc32.h +++ b/crc32.h @@ -35,6 +35,7 @@ SOFTWARE. #include #include "static_assert.h" +#include "frame_check_sequence.h" #if defined(COMPILER_KEIL) #pragma diag_suppress 1300 @@ -52,16 +53,36 @@ namespace etl extern const uint32_t CRC32[]; //*************************************************************************** + /// CRC32 policy. /// Calculates CRC32 using polynomial 0x04C11DB7. - /// \ingroup crc32 //*************************************************************************** - class crc32 + struct crc_policy_32 + { + typedef uint32_t value_type; + + inline uint32_t initial() const + { + return 0xFFFFFFFF; + } + + inline uint32_t add(uint32_t crc, uint8_t value) const + { + return (crc >> 8) ^ CRC32[(crc ^ value) & 0xFF]; + } + + inline uint32_t final(uint32_t crc) const + { + return crc ^ 0xFFFFFFFF; + } + }; + + //************************************************************************* + /// CRC32 + //************************************************************************* + class crc32 : public etl::frame_check_sequence { public: - typedef uint32_t value_type; - typedef uint32_t argument_type; - //************************************************************************* /// Default constructor. //************************************************************************* @@ -79,64 +100,10 @@ namespace etl crc32(TIterator begin, const TIterator end) { STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); + reset(); - while (begin != end) - { - crc = (crc >> 8) ^ CRC32[(crc ^ *begin++) & 0xFF]; - } + add(begin, end); } - - //************************************************************************* - /// Resets the CRC to the initial state. - //************************************************************************* - void reset() - { - crc = 0xFFFFFFFF; - } - - //************************************************************************* - /// Adds a range. - /// \param begin - /// \param end - //************************************************************************* - template - void add(TIterator begin, const TIterator end) - { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - - while (begin != end) - { - crc = (crc >> 8) ^ CRC32[(crc ^ *begin++) & 0xFF]; - } - } - - //************************************************************************* - /// \param value The uint8_t to add to the CRC. - //************************************************************************* - void add(uint8_t value) - { - crc = (crc >> 8) ^ CRC32[(crc ^ value) & 0xFF]; - } - - //************************************************************************* - /// Gets the CRC value. - //************************************************************************* - value_type value() const - { - return crc ^ 0xFFFFFFFF; - } - - //************************************************************************* - /// Conversion operator to value_type. - //************************************************************************* - operator value_type () const - { - return crc ^ 0xFFFFFFFF; - } - - private: - - value_type crc; }; } diff --git a/crc64_ecma.h b/crc64_ecma.h index 442a57d5..19ae9900 100644 --- a/crc64_ecma.h +++ b/crc64_ecma.h @@ -32,9 +32,10 @@ SOFTWARE. #define __ETL_CRC64_ECMA__ #include +#include #include "static_assert.h" -#include "type_traits.h" +#include "frame_check_sequence.h" #if defined(COMPILER_KEIL) #pragma diag_suppress 1300 @@ -52,16 +53,36 @@ namespace etl extern const uint64_t CRC64_ECMA[]; //*************************************************************************** - /// Calculates CRC64-ECMA using polynomial 0x42F0E1EBA9EA3693. - /// \ingroup crc64_ecma + /// CRC64 policy. + /// Calculates CRC64 ECMA using polynomial 0x42F0E1EBA9EA3693. //*************************************************************************** - class crc64_ecma + struct crc_policy_64_ecma + { + typedef uint64_t value_type; + + inline uint64_t initial() const + { + return 0; + } + + inline uint64_t add(uint64_t crc, uint8_t value) const + { + return (crc << 8) ^ CRC64_ECMA[((crc >> 56) ^ value) & 0xFF]; + } + + inline uint64_t final(uint64_t crc) const + { + return crc; + } + }; + + //************************************************************************* + /// CRC64 ECMA + //************************************************************************* + class crc64_ecma : public etl::frame_check_sequence { public: - typedef uint64_t value_type; - typedef uint64_t argument_type; - //************************************************************************* /// Default constructor. //************************************************************************* @@ -81,63 +102,8 @@ namespace etl STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); reset(); - while (begin != end) - { - crc = (crc << 8) ^ CRC64_ECMA[((crc >> 56) ^ *begin++) & 0xFF]; - } + add(begin, end); } - - //************************************************************************* - /// Resets the CRC to the initial state. - //************************************************************************* - void reset() - { - crc = 0; - } - - //************************************************************************* - /// Adds a range. - /// \param begin - /// \param end - //************************************************************************* - template - void add(TIterator begin, const TIterator end) - { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - - while (begin != end) - { - crc = (crc << 8) ^ CRC64_ECMA[((crc >> 56) ^ *begin++) & 0xFF]; - } - } - - //************************************************************************* - /// \param value The value to add to the CRC. - //************************************************************************* - void add(uint8_t value) - { - crc = (crc << 8) ^ CRC64_ECMA[((crc >> 56) ^ value) & 0xFF]; - } - - //************************************************************************* - /// Gets the CRC value. - //************************************************************************* - value_type value() const - { - return crc; - } - - //************************************************************************* - /// Conversion operator to value_type. - //************************************************************************* - operator value_type () const - { - return crc; - } - - private: - - value_type crc; }; } diff --git a/crc8_ccitt.h b/crc8_ccitt.h index fb11accf..2500490a 100644 --- a/crc8_ccitt.h +++ b/crc8_ccitt.h @@ -32,9 +32,10 @@ SOFTWARE. #define __ETL_CRC8_CCITT__ #include +#include #include "static_assert.h" -#include "type_traits.h" +#include "frame_check_sequence.h" #if defined(COMPILER_KEIL) #pragma diag_suppress 1300 @@ -52,16 +53,36 @@ namespace etl extern const uint8_t CRC8_CCITT[]; //*************************************************************************** + /// CRC8 CCITT policy. /// Calculates CRC8 CCITT using polynomial 0x07. - /// \ingroup crc8_ccitt //*************************************************************************** - class crc8_ccitt + struct crc_policy_8_ccitt + { + typedef uint8_t value_type; + + inline uint8_t initial() const + { + return 0; + } + + inline uint8_t add(uint8_t crc, uint8_t value) const + { + return CRC8_CCITT[crc ^ value]; + } + + inline uint8_t final(uint8_t crc) const + { + return crc; + } + }; + + //************************************************************************* + /// CRC8 CCITT + //************************************************************************* + class crc8_ccitt : public etl::frame_check_sequence { public: - typedef uint8_t value_type; - typedef uint8_t argument_type; - //************************************************************************* /// Default constructor. //************************************************************************* @@ -79,63 +100,10 @@ namespace etl crc8_ccitt(TIterator begin, const TIterator end) { STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); + reset(); - while (begin != end) - { - crc = CRC8_CCITT[crc ^ *begin++]; - } + add(begin, end); } - - //************************************************************************* - /// Resets the CRC to the initial state. - //************************************************************************* - void reset() - { - crc = 0; - } - - //************************************************************************* - /// Adds a range. - /// \param begin - /// \param end - //************************************************************************* - template - void add(TIterator begin, const TIterator end) - { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - while (begin != end) - { - crc = CRC8_CCITT[crc ^ *begin++]; - } - } - - //************************************************************************* - /// \param value The char to add to the CRC. - //************************************************************************* - void add(uint8_t value) - { - crc = CRC8_CCITT[crc ^ value]; - } - - //************************************************************************* - /// Gets the CRC value. - //************************************************************************* - value_type value() const - { - return crc; - } - - //************************************************************************* - /// Conversion operator to value_type. - //************************************************************************* - operator value_type () const - { - return crc; - } - - private: - - value_type crc; }; } diff --git a/frame_check_sequence.h b/frame_check_sequence.h new file mode 100644 index 00000000..74dc7744 --- /dev/null +++ b/frame_check_sequence.h @@ -0,0 +1,133 @@ + +///\file + +/****************************************************************************** +The MIT License(MIT) +Embedded Template Library. +https://github.com/ETLCPP/etl +http://www.etlcpp.com +Copyright(c) 2014 jwellbelove +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef __ETL_FRAME_CHECK_SEQUENCE__ +#define __ETL_FRAME_CHECK_SEQUENCE__ + +#include + +#include "static_assert.h" +#include "type_traits.h" +#include "binary.h" + +///\defgroup frame_check_sequence Frame check sequence calculation +///\ingroup maths + +namespace etl +{ + //*************************************************************************** + /// Calculates a frame check sequence according to the specified policy. + ///\tparam TPolicy The type used to enact the policy. + ///\ingroup frame_check_sequence + //*************************************************************************** + template + class frame_check_sequence + { + public: + + typedef TPolicy policy_type; + typedef typename policy_type::value_type value_type; + + STATIC_ASSERT(etl::is_unsigned::value, "Signed frame check type not supported"); + + //************************************************************************* + /// Default constructor. + //************************************************************************* + frame_check_sequence() + { + reset(); + } + + //************************************************************************* + /// Constructor from range. + /// \param begin Start of the range. + /// \param end End of the range. + //************************************************************************* + template + frame_check_sequence(TIterator begin, const TIterator end) + { + STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); + + reset(); + add(begin, end); + } + + //************************************************************************* + /// Resets the FCS to the initial state. + //************************************************************************* + void reset() + { + frame_check = policy.initial(); + } + + //************************************************************************* + /// Adds a range. + /// \param begin + /// \param end + //************************************************************************* + template + void add(TIterator begin, const TIterator end) + { + STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); + + while (begin != end) + { + frame_check = policy.add(frame_check, *begin++); + } + } + + //************************************************************************* + /// \param value The uint8_t to add to the FCS. + //************************************************************************* + void add(uint8_t value) + { + frame_check = policy.add(frame_check, value); + } + + //************************************************************************* + /// Gets the FCS value. + //************************************************************************* + value_type value() const + { + return policy.final(frame_check); + } + + //************************************************************************* + /// Conversion operator to value_type. + //************************************************************************* + operator value_type () const + { + return policy.final(frame_check); + } + + private: + + value_type frame_check; + policy_type policy; + }; +} + +#endif