1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

sys/hashes: Add Fletcher's checksums

This commit is contained in:
Joakim Gebart 2015-05-24 07:31:43 +02:00
parent e0098d408a
commit 1f94a93fb8
2 changed files with 70 additions and 0 deletions

View File

@ -12,6 +12,7 @@
* @file * @file
* @author Jason Linehan <patientulysses@gmail.com> * @author Jason Linehan <patientulysses@gmail.com>
* @author Christian Mehlis <mehlis@inf.fu-berlin.de> * @author Christian Mehlis <mehlis@inf.fu-berlin.de>
* @author Joakim Gebart <joakim.gebart@eistec.se>
*/ */
#include "hashes.h" #include "hashes.h"
@ -109,3 +110,41 @@ uint32_t one_at_a_time_hash(const uint8_t *buf, size_t len)
hash += hash << 15; hash += hash << 15;
return hash; return hash;
} }
uint16_t fletcher16(const uint8_t *data, size_t bytes)
{
uint16_t sum1 = 0xff, sum2 = 0xff;
while (bytes) {
size_t tlen = bytes > 20 ? 20 : bytes;
bytes -= tlen;
do {
sum2 += sum1 += *data++;
} while (--tlen);
sum1 = (sum1 & 0xff) + (sum1 >> 8);
sum2 = (sum2 & 0xff) + (sum2 >> 8);
}
/* Second reduction step to reduce sums to 8 bits */
sum1 = (sum1 & 0xff) + (sum1 >> 8);
sum2 = (sum2 & 0xff) + (sum2 >> 8);
return (sum2 << 8) | sum1;
}
uint32_t fletcher32(const uint16_t *data, size_t words)
{
uint32_t sum1 = 0xffff, sum2 = 0xffff;
while (words) {
unsigned tlen = words > 359 ? 359 : words;
words -= tlen;
do {
sum2 += sum1 += *data++;
} while (--tlen);
sum1 = (sum1 & 0xffff) + (sum1 >> 16);
sum2 = (sum2 & 0xffff) + (sum2 >> 16);
}
/* Second reduction step to reduce sums to 16 bits */
sum1 = (sum1 & 0xffff) + (sum1 >> 16);
sum2 = (sum2 & 0xffff) + (sum2 >> 16);
return (sum2 << 16) | sum1;
}

View File

@ -17,6 +17,7 @@
* *
* @author Jason Linehan <patientulysses@gmail.com> * @author Jason Linehan <patientulysses@gmail.com>
* @author Christian Mehlis <mehlis@inf.fu-berlin.de> * @author Christian Mehlis <mehlis@inf.fu-berlin.de>
* @author Joakim Gebart <joakim.gebart@eistec.se>
*/ */
#ifndef HASHES_H_ #ifndef HASHES_H_
@ -159,6 +160,36 @@ uint32_t rotating_hash(const uint8_t *buf, size_t len);
*/ */
uint32_t one_at_a_time_hash(const uint8_t *buf, size_t len); uint32_t one_at_a_time_hash(const uint8_t *buf, size_t len);
/**
* @brief Fletcher's 16 bit checksum
*
* found on
* http://en.wikipedia.org/w/index.php?title=Fletcher%27s_checksum&oldid=661273016#Optimizations
*
* @note the returned sum is never 0
*
* @param buf input buffer to hash
* @param bytes length of buffer, in bytes
* @return 16 bit sized hash in the interval [1..65535]
*/
uint16_t fletcher16(const uint8_t *buf, size_t bytes);
/**
* @brief Fletcher's 32 bit checksum
*
* found on
* http://en.wikipedia.org/w/index.php?title=Fletcher%27s_checksum&oldid=661273016#Optimizations
*
* @note the returned sum is never 0
* @note pay attention to alignment issues since this operates on an input
* buffer containing 16 bit words, not bytes.
*
* @param buf input buffer to hash
* @param words length of buffer, in 16 bit words
* @return 32 bit sized hash in the interval [1..2^32]
*/
uint32_t fletcher32(const uint16_t *buf, size_t words);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif