mirror of
https://github.com/ETLCPP/etl.git
synced 2026-11-09 22:59:47 +08:00
Modified checksum class to be easily customised.
Added sum, bsd & xor policies. Deleted bsd_checksum.h
This commit is contained in:
parent
348a6333d2
commit
9a35980950
139
bsd_checksum.h
139
bsd_checksum.h
@ -1,139 +0,0 @@
|
||||
///\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_BSDCHECKSUM__
|
||||
#define __ETL_BSDCHECKSUM__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "static_assert.h"
|
||||
#include "type_traits.h"
|
||||
#include "binary.h"
|
||||
#include "ihash.h"
|
||||
|
||||
///\defgroup bsdchecksum BSD Checksum calculation
|
||||
///\ingroup maths
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Calculates the checksum.
|
||||
///\tparam TSum The type used for the sum.
|
||||
///\ingroup checksum
|
||||
//***************************************************************************
|
||||
template <typename TSum>
|
||||
class bsd_checksum
|
||||
{
|
||||
public:
|
||||
|
||||
STATIC_ASSERT(is_unsigned<TSum>::value, "Signed TSum template parameter not supported");
|
||||
|
||||
typedef TSum value_type;
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
bsd_checksum()
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
bsd_checksum(TIterator begin, const TIterator end)
|
||||
{
|
||||
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Only 8 bit types supported");
|
||||
reset();
|
||||
|
||||
while (begin != end)
|
||||
{
|
||||
sum = rotate_right(sum) + *begin++;
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Resets the CRC to the initial state.
|
||||
//*************************************************************************
|
||||
void reset()
|
||||
{
|
||||
sum = 0;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Adds a range.
|
||||
/// \param begin
|
||||
/// \param end
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
void add(TIterator begin, const TIterator end)
|
||||
{
|
||||
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Only 8 bit types supported");
|
||||
|
||||
while (begin != end)
|
||||
{
|
||||
sum = rotate_right(sum) + *begin++;
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// \param value The uint8_t to add to the checksum.
|
||||
//*************************************************************************
|
||||
void add(uint8_t value)
|
||||
{
|
||||
sum = rotate_right(sum) + value;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the checksum value.
|
||||
//*************************************************************************
|
||||
value_type value()
|
||||
{
|
||||
return sum;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Conversion operator to value_type.
|
||||
//*************************************************************************
|
||||
operator value_type ()
|
||||
{
|
||||
return sum;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
value_type sum;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
167
checksum.h
167
checksum.h
@ -1,24 +1,20 @@
|
||||
|
||||
///\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
|
||||
@ -35,7 +31,7 @@ SOFTWARE.
|
||||
|
||||
#include "static_assert.h"
|
||||
#include "type_traits.h"
|
||||
#include "ihash.h"
|
||||
#include "binary.h"
|
||||
|
||||
///\defgroup checksum Checksum calculation
|
||||
///\ingroup maths
|
||||
@ -43,18 +39,86 @@ SOFTWARE.
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Calculates the checksum.
|
||||
///\tparam TSum The type used for the sum.
|
||||
/// Standard addition checksum policy.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
struct checksum_policy_sum
|
||||
{
|
||||
inline T initial() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline T add(T sum, uint8_t value) const
|
||||
{
|
||||
return sum + value;
|
||||
}
|
||||
|
||||
inline T final(T sum) const
|
||||
{
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// BSD checksum policy.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
struct checksum_policy_bsd
|
||||
{
|
||||
inline T initial() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline T add(T sum, uint8_t value) const
|
||||
{
|
||||
return etl::rotate_right(sum) + value;
|
||||
}
|
||||
|
||||
inline T final(T sum) const
|
||||
{
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Standard XOR checksum policy.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
struct checksum_policy_xor
|
||||
{
|
||||
inline T initial() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline T add(T sum, uint8_t value) const
|
||||
{
|
||||
return sum ^ value;
|
||||
}
|
||||
|
||||
inline T final(T sum) const
|
||||
{
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// 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 <typename TSum>
|
||||
template <typename TSum, typename TPolicy = checksum_policy_sum<TSum> >
|
||||
class checksum
|
||||
{
|
||||
public:
|
||||
|
||||
STATIC_ASSERT(is_unsigned<TSum>::value, "Signed TSum template parameter not supported");
|
||||
STATIC_ASSERT(etl::is_unsigned<TSum>::value, "Signed TSum template parameter not supported");
|
||||
|
||||
typedef TSum value_type;
|
||||
typedef TSum value_type;
|
||||
typedef TPolicy policy_type;
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
@ -75,10 +139,7 @@ namespace etl
|
||||
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
|
||||
|
||||
reset();
|
||||
while (begin != end)
|
||||
{
|
||||
sum += *begin++;
|
||||
}
|
||||
add(begin, end);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -86,7 +147,7 @@ namespace etl
|
||||
//*************************************************************************
|
||||
void reset()
|
||||
{
|
||||
sum = 0;
|
||||
sum = policy.initial();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -101,7 +162,7 @@ namespace etl
|
||||
|
||||
while (begin != end)
|
||||
{
|
||||
sum += *begin++;
|
||||
sum = policy.add(sum, *begin++);
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,7 +171,7 @@ namespace etl
|
||||
//*************************************************************************
|
||||
void add(uint8_t value)
|
||||
{
|
||||
sum += value;
|
||||
sum = policy.add(sum, value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -118,7 +179,7 @@ namespace etl
|
||||
//*************************************************************************
|
||||
value_type value() const
|
||||
{
|
||||
return sum;
|
||||
return policy.final(sum);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -126,13 +187,77 @@ namespace etl
|
||||
//*************************************************************************
|
||||
operator value_type () const
|
||||
{
|
||||
return sum;
|
||||
return policy.final(sum);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
value_type sum;
|
||||
value_type sum;
|
||||
policy_type policy;
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// BSD Checksum.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
class bsd_checksum : public etl::checksum<T, etl::checksum_policy_bsd<T> >
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
bsd_checksum()
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
bsd_checksum(TIterator begin, const TIterator end)
|
||||
{
|
||||
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
|
||||
|
||||
reset();
|
||||
add(begin, end);
|
||||
}
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// XOR Checksum.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
class xor_checksum : public etl::checksum<T, etl::checksum_policy_xor<T> >
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
xor_checksum()
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
xor_checksum(TIterator begin, const TIterator end)
|
||||
{
|
||||
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
|
||||
|
||||
reset();
|
||||
add(begin, end);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
149
test/test_xor_checksum.cpp
Normal file
149
test/test_xor_checksum.cpp
Normal file
@ -0,0 +1,149 @@
|
||||
/******************************************************************************
|
||||
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.
|
||||
******************************************************************************/
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../checksum.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename TSum, typename TIterator>
|
||||
TSum reference_checksum(TIterator begin, TIterator end)
|
||||
{
|
||||
typedef typename std::iterator_traits<TIterator>::value_type value_type;
|
||||
TSum checksum = 0;
|
||||
|
||||
while (begin != end)
|
||||
{
|
||||
value_type value = *begin++;
|
||||
checksum ^= value;
|
||||
}
|
||||
|
||||
return checksum;
|
||||
}
|
||||
|
||||
SUITE(test_checksum)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(test_checksum_constructor)
|
||||
{
|
||||
std::string data("123456789");
|
||||
|
||||
uint8_t sum = etl::xor_checksum<uint8_t>(data.begin(), data.end());
|
||||
uint8_t compare = reference_checksum<uint8_t>(data.begin(), data.end());
|
||||
|
||||
CHECK_EQUAL(int(compare), int(sum));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_checksum_add_values8_add)
|
||||
{
|
||||
std::string data("123456789");
|
||||
|
||||
etl::xor_checksum<uint8_t> checksum_calculator;
|
||||
|
||||
for (size_t i = 0; i < data.size(); ++i)
|
||||
{
|
||||
checksum_calculator.add(data[i]);
|
||||
}
|
||||
|
||||
uint8_t sum = checksum_calculator;
|
||||
uint8_t compare = reference_checksum<uint8_t>(data.begin(), data.end());
|
||||
|
||||
CHECK_EQUAL(int(compare), int(sum));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_checksum_add_values8_operator_plus_equals)
|
||||
{
|
||||
std::string data("123456789");
|
||||
|
||||
etl::xor_checksum<uint8_t> checksum_calculator;
|
||||
|
||||
for (size_t i = 0; i < data.size(); ++i)
|
||||
{
|
||||
checksum_calculator.add(data[i]);
|
||||
}
|
||||
|
||||
uint8_t sum = checksum_calculator;
|
||||
uint8_t compare = reference_checksum<uint8_t>(data.begin(), data.end());
|
||||
|
||||
CHECK_EQUAL(int(compare), int(sum));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_checksum_add_range)
|
||||
{
|
||||
std::string data("123456789");
|
||||
|
||||
etl::xor_checksum<uint8_t> checksum_calculator;
|
||||
|
||||
checksum_calculator.add(data.begin(), data.end());
|
||||
|
||||
uint8_t sum = checksum_calculator.value();
|
||||
uint8_t compare = reference_checksum<uint8_t>(data.begin(), data.end());
|
||||
|
||||
CHECK_EQUAL(int(compare), int(sum));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_checksum_add_range_sum32)
|
||||
{
|
||||
std::string data("1");
|
||||
|
||||
etl::xor_checksum<uint32_t> checksum_calculator;
|
||||
|
||||
checksum_calculator.add(data.begin(), data.end());
|
||||
|
||||
uint32_t sum = checksum_calculator.value();
|
||||
uint32_t compare = reference_checksum<uint32_t>(data.begin(), data.end());
|
||||
|
||||
CHECK_EQUAL(compare, sum);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_checksum_add_range_endian)
|
||||
{
|
||||
std::vector<uint8_t> data1 = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
|
||||
std::vector<uint32_t> data2 = { 0x04030201, 0x08070605 };
|
||||
std::vector<uint8_t> data3 = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
|
||||
|
||||
uint64_t hash1 = etl::xor_checksum<uint8_t>(data1.begin(), data1.end());
|
||||
uint64_t hash2 = etl::xor_checksum<uint8_t>((uint8_t*)&data2[0], (uint8_t*)&data2[0] + (data2.size() * sizeof(uint32_t)));
|
||||
uint64_t hash3 = etl::xor_checksum<uint8_t>(data3.rbegin(), data3.rend());
|
||||
CHECK_EQUAL(hash1, hash2);
|
||||
CHECK_EQUAL(hash1, hash3);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user