2022-04-15 17:36:01 +02:00
|
|
|
//
|
|
|
|
// FILE: Fletcher.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-01-17 19:28:45 +01:00
|
|
|
// VERSION: 0.1.8
|
2022-04-15 17:36:01 +02:00
|
|
|
// DATE: 2022-01-25
|
|
|
|
// PURPOSE: Arduino Library for calculating Fletcher's checksum
|
|
|
|
// URL: https://github.com/RobTillaart/Fletcher
|
|
|
|
// https://en.wikipedia.org/wiki/Fletcher%27s_checksum
|
|
|
|
|
|
|
|
|
2022-09-08 13:31:39 +02:00
|
|
|
#include "Fletcher.h"
|
2022-04-15 17:36:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
// straightforward implementation.
|
|
|
|
// max length buffer 65534.
|
|
|
|
// Wikipedia shows optimizations.
|
|
|
|
//
|
2023-01-17 19:28:45 +01:00
|
|
|
uint16_t fletcher16(uint8_t *data, uint16_t length, uint32_t s1, uint32_t s2)
|
2022-04-15 17:36:01 +02:00
|
|
|
{
|
2023-01-17 19:28:45 +01:00
|
|
|
uint32_t _s1 = s1;
|
|
|
|
uint32_t _s2 = s2;
|
2022-04-15 17:36:01 +02:00
|
|
|
for (uint16_t i = 0; i < length;)
|
|
|
|
{
|
2023-01-17 19:28:45 +01:00
|
|
|
// if _s2 is halfway it is time to do modulo
|
|
|
|
while ((i < length) && (_s2 < 2147483648ULL)) // MAGIC NR
|
2022-04-15 17:36:01 +02:00
|
|
|
{
|
2023-01-17 19:28:45 +01:00
|
|
|
_s1 += data[i++];
|
|
|
|
_s2 += _s1;
|
2022-04-15 17:36:01 +02:00
|
|
|
}
|
2022-09-08 17:33:44 +02:00
|
|
|
// this optimization does not work due to the above "32-bit" loop.
|
|
|
|
// for all three functions.
|
2023-01-17 19:28:45 +01:00
|
|
|
// _s1 = (_s1 & 255) + (_s1 >> 8);
|
|
|
|
_s1 %= FLETCHER_16;
|
|
|
|
_s2 %= FLETCHER_16;
|
2022-04-15 17:36:01 +02:00
|
|
|
}
|
2023-01-17 19:28:45 +01:00
|
|
|
return (_s2 << 8) | _s1;
|
2022-04-15 17:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-17 19:28:45 +01:00
|
|
|
uint32_t fletcher32(uint16_t *data, uint16_t length, uint32_t s1, uint32_t s2)
|
2022-04-15 17:36:01 +02:00
|
|
|
{
|
2023-01-17 19:28:45 +01:00
|
|
|
uint32_t _s1 = s1;
|
|
|
|
uint32_t _s2 = s2;
|
2022-04-15 17:36:01 +02:00
|
|
|
for (uint16_t i = 0; i < length;)
|
|
|
|
{
|
2023-01-17 19:28:45 +01:00
|
|
|
// if _s2 is halfway it is time to do modulo
|
|
|
|
while ((i < length) && (_s2 < 2147483648ULL)) // MAGIC NR
|
2022-04-15 17:36:01 +02:00
|
|
|
{
|
2023-01-17 19:28:45 +01:00
|
|
|
_s1 += data[i++];
|
|
|
|
_s2 += _s1;
|
2022-04-15 17:36:01 +02:00
|
|
|
}
|
2023-01-17 19:28:45 +01:00
|
|
|
_s1 %= FLETCHER_32;
|
|
|
|
_s2 %= FLETCHER_32;
|
2022-04-15 17:36:01 +02:00
|
|
|
}
|
2023-01-17 19:28:45 +01:00
|
|
|
return (_s2 << 16) | _s1;
|
2022-04-15 17:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-17 19:28:45 +01:00
|
|
|
uint64_t fletcher64(uint32_t *data, uint16_t length, uint64_t s1, uint64_t s2)
|
2022-04-15 17:36:01 +02:00
|
|
|
{
|
2023-01-17 19:28:45 +01:00
|
|
|
uint64_t _s1 = s1;
|
|
|
|
uint64_t _s2 = s2;
|
2022-04-15 17:36:01 +02:00
|
|
|
for (uint16_t i = 0; i < length;)
|
|
|
|
{
|
2023-01-17 19:28:45 +01:00
|
|
|
// if _s2 is halfway it is time to do modulo
|
|
|
|
while ((i < length) && (_s2 < 9223372036854775808ULL)) // MAGIC NR
|
2022-04-15 17:36:01 +02:00
|
|
|
{
|
2023-01-17 19:28:45 +01:00
|
|
|
_s1 += data[i++];
|
|
|
|
_s2 += _s1;
|
2022-04-15 17:36:01 +02:00
|
|
|
}
|
2023-01-17 19:28:45 +01:00
|
|
|
_s1 %= FLETCHER_64;
|
|
|
|
_s2 %= FLETCHER_64;
|
2022-04-15 17:36:01 +02:00
|
|
|
}
|
2023-01-17 19:28:45 +01:00
|
|
|
return (_s2 << 32) | _s1;
|
2022-04-15 17:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|
|
|
|
|