2016-02-13 09:46:41 +01:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2018-10-23 15:54:24 +02:00
|
|
|
static inline void _reduce(fletcher16_ctx_t *ctx)
|
2016-02-13 09:46:41 +01:00
|
|
|
{
|
2018-10-23 15:54:24 +02:00
|
|
|
ctx->sum1 = (ctx->sum1 & 0xff) + (ctx->sum1 >> 8);
|
|
|
|
ctx->sum2 = (ctx->sum2 & 0xff) + (ctx->sum2 >> 8);
|
|
|
|
}
|
2016-02-13 09:46:41 +01:00
|
|
|
|
2018-10-23 15:54:24 +02:00
|
|
|
void fletcher16_init(fletcher16_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
ctx->sum1 = 0xff;
|
|
|
|
ctx->sum2 = 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fletcher16_update(fletcher16_ctx_t *ctx, const uint8_t *data, size_t len)
|
|
|
|
{
|
|
|
|
while (len) {
|
|
|
|
size_t tlen = len > 20 ? 20 : len;
|
|
|
|
len -= tlen;
|
2016-02-13 09:46:41 +01:00
|
|
|
do {
|
2018-10-23 15:54:24 +02:00
|
|
|
ctx->sum2 += ctx->sum1 += *data++;
|
2016-02-13 09:46:41 +01:00
|
|
|
} while (--tlen);
|
2018-10-23 15:54:24 +02:00
|
|
|
_reduce(ctx);
|
2016-02-13 09:46:41 +01:00
|
|
|
}
|
2018-10-23 15:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t fletcher16_finish(fletcher16_ctx_t *ctx)
|
|
|
|
{
|
2016-02-13 09:46:41 +01:00
|
|
|
/* Second reduction step to reduce sums to 8 bits */
|
2018-10-23 15:54:24 +02:00
|
|
|
_reduce(ctx);
|
|
|
|
return (ctx->sum2 << 8) | ctx->sum1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t fletcher16(const uint8_t *data, size_t bytes)
|
|
|
|
{
|
|
|
|
fletcher16_ctx_t ctx;
|
|
|
|
fletcher16_init(&ctx);
|
|
|
|
fletcher16_update(&ctx, data, bytes);
|
|
|
|
return fletcher16_finish(&ctx);
|
2016-02-13 09:46:41 +01:00
|
|
|
}
|