GY-63_MS5611/libraries/Adler/Adler.cpp

59 lines
1.3 KiB
C++
Raw Normal View History

2022-04-15 08:35:07 -04:00
//
// FILE: Adler.cpp
// AUTHOR: Rob Tillaart
2023-01-16 10:33:37 -05:00
// VERSION: 0.2.3
2022-04-15 08:35:07 -04:00
// DATE: 2022-01-27
2022-06-16 03:46:28 -04:00
// PURPOSE: Arduino Library for calculating Adler checksum
2022-04-15 08:35:07 -04:00
// URL: https://github.com/RobTillaart/Adler
// https://en.wikipedia.org/wiki/Adler-32
2022-06-16 03:46:28 -04:00
// (no Adler16 reference, implementation is experimental)
2022-04-15 08:35:07 -04:00
#include "Adler.h"
2022-06-16 03:46:28 -04:00
//////////////////////////////////////////////////////////////
2022-06-14 09:28:07 -04:00
//
2022-06-16 03:46:28 -04:00
// STATIC FUNCTION
//
2023-01-16 10:33:37 -05:00
uint32_t adler32(uint8_t * array, uint16_t length, uint32_t s1, uint32_t s2)
2022-06-14 09:28:07 -04:00
{
2023-01-16 10:33:37 -05:00
uint32_t _s1 = s1;
uint32_t _s2 = s2;
2022-06-14 09:28:07 -04:00
for (uint16_t i = 0; i < length;)
{
2023-01-16 10:33:37 -05:00
// if _s2 is halfway it is time to do modulo
while ((i < length) && (_s2 < 2147483648ULL)) // MAGIC NR.
2022-06-14 09:28:07 -04:00
{
2023-01-16 10:33:37 -05:00
_s1 += array[i++];
_s2 += _s1;
2022-06-14 09:28:07 -04:00
}
2023-01-16 10:33:37 -05:00
_s1 %= ADLER32_MOD_PRIME;
_s2 %= ADLER32_MOD_PRIME;
2022-06-14 09:28:07 -04:00
}
2023-01-16 10:33:37 -05:00
return (_s2 << 16) | _s1;
2022-06-14 09:28:07 -04:00
}
2023-01-16 10:33:37 -05:00
uint16_t adler16(uint8_t * array, uint16_t length, uint16_t s1, uint16_t s2)
2022-04-15 08:35:07 -04:00
{
2023-01-16 10:33:37 -05:00
uint16_t _s1 = s1;
uint16_t _s2 = s2;
2022-04-15 08:35:07 -04:00
for (uint16_t i = 0; i < length;)
{
2023-01-16 10:33:37 -05:00
// if _s2 is halfway it is time to do modulo
while ((i < length) && (_s2 < 32768)) // MAGIC NR.
2022-04-15 08:35:07 -04:00
{
2023-01-16 10:33:37 -05:00
_s1 += array[i++];
_s2 += _s1;
2022-04-15 08:35:07 -04:00
}
2023-01-16 10:33:37 -05:00
_s1 %= ADLER16_MOD_PRIME;
_s2 %= ADLER16_MOD_PRIME;
2022-04-15 08:35:07 -04:00
}
2023-01-16 10:33:37 -05:00
return (_s2 << 8) | _s1;
2022-04-15 08:35:07 -04:00
}
2023-01-16 10:33:37 -05:00
// -- END OF FILE --
2022-04-15 08:35:07 -04:00