semf
crcsoftware.h
Go to the documentation of this file.
1
10#ifndef SEMF_UTILS_PROCESSING_CRCSOFTWARE_H_
11#define SEMF_UTILS_PROCESSING_CRCSOFTWARE_H_
12
15#include <algorithm>
16#include <cstring>
17#include <cstdint>
18
19namespace semf
20{
28template <typename T, T DEFAULT_POLYNOMIAL, T DEFAULT_INITVALUE, T DEFAULT_FINALXOR>
29class CrcSoftware : public app::Crc
30{
31public:
38 explicit CrcSoftware(T polynomial = DEFAULT_POLYNOMIAL, T initValue = DEFAULT_INITVALUE, T finalXor = DEFAULT_FINALXOR);
39 virtual ~CrcSoftware() = default;
40
41 void reset();
42 const uint8_t* accumulate(const uint8_t data[], size_t dataSize) override;
43 const uint8_t* calculate(const uint8_t data[], size_t dataSize) override;
44 bool isEqual(const uint8_t data[]) override;
45 size_t bitSize() const override;
46 size_t byteSize() const override;
47
48private:
54 static T invertBitSequence(T value);
56 const T m_invertedPolynomial;
58 const T m_initValue;
60 const T m_finalXor;
62 T m_crc;
63};
64
65template <typename T, T DEFAULT_POLYNOMIAL, T DEFAULT_INITVALUE, T DEFAULT_FINALXOR>
67: m_invertedPolynomial(invertBitSequence(polynomial)),
68 m_initValue(initValue),
69 m_finalXor(finalXor),
70 m_crc(initValue ^ m_finalXor)
71{
72}
73
74template <typename T, T DEFAULT_POLYNOMIAL, T DEFAULT_INITVALUE, T DEFAULT_FINALXOR>
76{
77 SEMF_INFO("reset");
78 m_crc = m_initValue ^ m_finalXor;
79}
80
81template <typename T, T DEFAULT_POLYNOMIAL, T DEFAULT_INITVALUE, T DEFAULT_FINALXOR>
83{
84 m_crc ^= m_finalXor;
85 for (size_t i = 0; i < dataSize; i++)
86 {
87 m_crc ^= data[i];
88
89 for (int i = 0; i < 8; i++)
90 {
91 T sum = ~((m_crc & 1) - 1);
92 m_crc = (m_crc >> 1) ^ (m_invertedPolynomial & sum);
93 }
94 }
95 m_crc ^= m_finalXor;
96 SEMF_INFO("accumulated with data %p and size %u to crc value %u", data, dataSize, m_crc);
97 return reinterpret_cast<const uint8_t*>(&m_crc);
98}
99
100template <typename T, T DEFAULT_POLYNOMIAL, T DEFAULT_INITVALUE, T DEFAULT_FINALXOR>
102{
103 m_crc = m_initValue ^ m_finalXor;
104 accumulate(data, dataSize);
105 SEMF_INFO("calculated with data %p and size %u to crc value %u", data, dataSize, m_crc);
106 return reinterpret_cast<const uint8_t*>(&m_crc);
107}
108
109template <typename T, T DEFAULT_POLYNOMIAL, T DEFAULT_INITVALUE, T DEFAULT_FINALXOR>
111{
112 if (data == nullptr)
113 {
114 SEMF_ERROR("input is nullptr");
115 return false;
116 }
117 bool e = std::memcmp(reinterpret_cast<const uint8_t*>(&m_crc), data, sizeof(T)) == 0;
118 SEMF_INFO("crc equal: %d", e);
119 return e;
120}
121
122template <typename T, T DEFAULT_POLYNOMIAL, T DEFAULT_INITVALUE, T DEFAULT_FINALXOR>
124{
125 return sizeof(T) * 8;
126}
127
128template <typename T, T DEFAULT_POLYNOMIAL, T DEFAULT_INITVALUE, T DEFAULT_FINALXOR>
130{
131 size_t ret = bitSize() / 8;
132 return (bitSize() % 8 == 0) ? ret : ret + 1;
133}
134
135template <typename T, T DEFAULT_POLYNOMIAL, T DEFAULT_INITVALUE, T DEFAULT_FINALXOR>
137{
138 T retVal = 0;
139 for (size_t i = 0; i < sizeof(T) * 8; i++)
140 {
141 retVal |= (((1 << i) & value) ? 1u : 0u) << (sizeof(T) * 8 - 1 - i);
142 }
143
144 return retVal;
145}
146
153} /* namespace semf */
154#endif /* SEMF_UTILS_PROCESSING_CRCSOFTWARE_H_ */
Template class for CRC generation in software.
Definition: crcsoftware.h:30
size_t bitSize() const override
Returns the size of the CRC in bits.
Definition: crcsoftware.h:123
virtual ~CrcSoftware()=default
const uint8_t * accumulate(const uint8_t data[], size_t dataSize) override
Computes the CRC of the input data using a combination of the previous CRC value and the new one.
Definition: crcsoftware.h:82
void reset()
Resets internal values to their initial values.
Definition: crcsoftware.h:75
size_t byteSize() const override
Returns the size of the CRC in bytes.
Definition: crcsoftware.h:129
const uint8_t * calculate(const uint8_t data[], size_t dataSize) override
Calculates the CRC of the input data.
Definition: crcsoftware.h:101
bool isEqual(const uint8_t data[]) override
Checks if previously calculated CRC is equal to the input value.
Definition: crcsoftware.h:110
CrcSoftware(T polynomial=DEFAULT_POLYNOMIAL, T initValue=DEFAULT_INITVALUE, T finalXor=DEFAULT_FINALXOR)
Constructor.
Definition: crcsoftware.h:66
Interface for implementing cyclic redundancy check (CRC).
Definition: crc.h:24
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41
CrcSoftware< uint8_t, 0x07, 0xFF, 0xFF > Crc8Sofware
Definition: crcsoftware.h:152
CrcSoftware< uint32_t, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF > Crc32Software
Definition: crcsoftware.h:148
CrcSoftware< uint16_t, 0x1021, 0xFFFF, 0xFFFF > Crc16Software
Definition: crcsoftware.h:150