
6 changed files with 183 additions and 70 deletions
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2015 Eistec AB |
||||
* |
||||
* This file is subject to the terms and conditions of the GNU Lesser |
||||
* General Public License v2.1. See the file LICENSE in the top level |
||||
* directory for more details. |
||||
*/ |
||||
|
||||
/**
|
||||
* @ingroup sys_checksum_fletcher16 |
||||
* @{ |
||||
* |
||||
* @file |
||||
* @brief Fletcher16 implementation |
||||
* |
||||
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se> |
||||
* |
||||
* @} |
||||
*/ |
||||
|
||||
#include "checksum/fletcher16.h" |
||||
|
||||
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; |
||||
} |
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2015 Eistec AB |
||||
* |
||||
* This file is subject to the terms and conditions of the GNU Lesser |
||||
* General Public License v2.1. See the file LICENSE in the top level |
||||
* directory for more details. |
||||
*/ |
||||
|
||||
/**
|
||||
* @ingroup sys_checksum_fletcher32 |
||||
* @{ |
||||
* |
||||
* @file |
||||
* @brief Fletcher32 implementation |
||||
* |
||||
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se> |
||||
* |
||||
* @} |
||||
*/ |
||||
|
||||
#include "checksum/fletcher32.h" |
||||
|
||||
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; |
||||
} |
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Eistec AB |
||||
* |
||||
* This file is subject to the terms and conditions of the GNU Lesser |
||||
* General Public License v2.1. See the file LICENSE in the top level |
||||
* directory for more details. |
||||
*/ |
||||
|
||||
/**
|
||||
* @defgroup sys_checksum_fletcher16 Fletcher16 |
||||
* @ingroup sys_checksum |
||||
* |
||||
* @brief Fletcher16 checksum algorithm |
||||
* @{ |
||||
* |
||||
* @file |
||||
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se> |
||||
*/ |
||||
|
||||
#ifndef FLETCHER16_H |
||||
#define FLETCHER16_H |
||||
|
||||
#include <stdlib.h> |
||||
#include <stdint.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/**
|
||||
* @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); |
||||
|
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* FLETCHER16_H */ |
||||
|
||||
/** @} */ |
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Eistec AB |
||||
* |
||||
* This file is subject to the terms and conditions of the GNU Lesser |
||||
* General Public License v2.1. See the file LICENSE in the top level |
||||
* directory for more details. |
||||
*/ |
||||
|
||||
/**
|
||||
* @defgroup sys_checksum_fletcher32 Fletcher32 |
||||
* @ingroup sys_checksum |
||||
* |
||||
* @brief Fletcher32 checksum algorithm |
||||
* @{ |
||||
* |
||||
* @file |
||||
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se> |
||||
*/ |
||||
|
||||
#ifndef FLETCHER32_H |
||||
#define FLETCHER32_H |
||||
|
||||
#include <stdlib.h> |
||||
#include <stdint.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/**
|
||||
* @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 |
||||
} |
||||
#endif |
||||
|
||||
#endif /* FLETCHER32_H */ |
||||
|
||||
/** @} */ |
Loading…
Reference in new issue