Putting the Cyclic back into CRC

added a parameter to the crc function to supply it with a starting value
for the crc, so one can calculate a crc over mutiple separate parts.
This commit is contained in:
Ulrich Mohr 2020-04-06 12:50:57 +02:00
parent 9d5a30a1f6
commit 90cba58ded
2 changed files with 5 additions and 6 deletions

View File

@ -38,19 +38,18 @@ static const uint16_t crc_table[256] = {
// CRC implementation // CRC implementation
uint16_t Calculate_CRC(uint8_t const input[], uint32_t length) uint16_t Calculate_CRC(uint8_t const input[], uint32_t length, uint16_t startingCrc)
{ {
uint16_t crc = 0xFFFF;
uint8_t *data = (uint8_t *)input; uint8_t *data = (uint8_t *)input;
unsigned int tbl_idx; unsigned int tbl_idx;
while (length--) { while (length--) {
tbl_idx = ((crc >> 8) ^ *data) & 0xff; tbl_idx = ((startingCrc >> 8) ^ *data) & 0xff;
crc = (crc_table[tbl_idx] ^ (crc << 8)) & 0xffff; startingCrc = (crc_table[tbl_idx] ^ (startingCrc << 8)) & 0xffff;
data++; data++;
} }
return crc & 0xffff; return startingCrc & 0xffff;
//The part below is not used! //The part below is not used!
// bool temr[16]; // bool temr[16];

View File

@ -3,7 +3,7 @@
#include <stdint.h> #include <stdint.h>
uint16_t Calculate_CRC(uint8_t const input[], uint32_t length); uint16_t Calculate_CRC(uint8_t const input[], uint32_t length, uint16_t startingCrc = 0xffff);
#endif /* CRC_H_ */ #endif /* CRC_H_ */