2022-04-15 08:35:07 -04:00
|
|
|
//
|
|
|
|
// FILE: Adler.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-09-21 03:34:38 -04:00
|
|
|
// VERSION: 0.2.1
|
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
|
|
|
|
//
|
|
|
|
uint32_t adler32(uint8_t * array, uint16_t length)
|
2022-06-14 09:28:07 -04:00
|
|
|
{
|
2022-06-16 03:46:28 -04:00
|
|
|
uint32_t s1 = 1;
|
|
|
|
uint32_t s2 = 0;
|
2022-06-14 09:28:07 -04:00
|
|
|
for (uint16_t i = 0; i < length;)
|
|
|
|
{
|
|
|
|
// if S2 is halfway it is time to do modulo
|
|
|
|
while ((i < length) && (s2 < 2147483648ULL))
|
|
|
|
{
|
|
|
|
s1 += array[i++];
|
|
|
|
s2 += s1;
|
|
|
|
}
|
2022-06-16 03:46:28 -04:00
|
|
|
s1 %= ADLER32_MOD_PRIME;
|
|
|
|
s2 %= ADLER32_MOD_PRIME;
|
2022-06-14 09:28:07 -04:00
|
|
|
}
|
2022-06-16 03:46:28 -04:00
|
|
|
return (s2 << 16) | s1;
|
2022-06-14 09:28:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-16 03:46:28 -04:00
|
|
|
uint16_t adler16(uint8_t * array, uint16_t length)
|
2022-04-15 08:35:07 -04:00
|
|
|
{
|
2022-06-16 03:46:28 -04:00
|
|
|
uint16_t s1 = 1;
|
|
|
|
uint16_t s2 = 0;
|
2022-04-15 08:35:07 -04:00
|
|
|
for (uint16_t i = 0; i < length;)
|
|
|
|
{
|
|
|
|
// if S2 is halfway it is time to do modulo
|
2022-06-16 03:46:28 -04:00
|
|
|
while ((i < length) && (s2 < 32768))
|
2022-04-15 08:35:07 -04:00
|
|
|
{
|
2022-06-14 09:28:07 -04:00
|
|
|
s1 += array[i++];
|
2022-04-15 08:35:07 -04:00
|
|
|
s2 += s1;
|
|
|
|
}
|
2022-06-16 03:46:28 -04:00
|
|
|
s1 %= ADLER16_MOD_PRIME;
|
|
|
|
s2 %= ADLER16_MOD_PRIME;
|
2022-04-15 08:35:07 -04:00
|
|
|
}
|
2022-06-16 03:46:28 -04:00
|
|
|
return (s2 << 8) | s1;
|
2022-04-15 08:35:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|