

4 changed files with 161 additions and 0 deletions
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Freie Universität Berlin |
||||
* |
||||
* 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. |
||||
*/ |
||||
|
||||
/**
|
||||
* @{ |
||||
* |
||||
* @file |
||||
* @author Martine Lenders <m.lenders@fu-berlin.de> |
||||
*/ |
||||
|
||||
#include <assert.h> |
||||
#include <inttypes.h> |
||||
|
||||
#include "checksum/ucrc16.h" |
||||
|
||||
#define UINT8_BIT_SIZE (8U) |
||||
#define UINT16_BIT_SIZE (16U) |
||||
#define LEFTMOST_BIT_SET(value) ((value) & 0x8000U) |
||||
#define RIGHTMOST_BIT_SET(value) ((value) & 0x0001U) |
||||
|
||||
uint16_t ucrc16_calc_be(const uint8_t *buf, size_t len, uint16_t poly, |
||||
uint16_t seed) |
||||
{ |
||||
assert(buf != NULL); |
||||
for (unsigned c = 0; c < len; c++, buf++) { |
||||
uint32_t tmp = seed ^ (*buf << (UINT16_BIT_SIZE - UINT8_BIT_SIZE)); |
||||
for (unsigned i = 0; i < UINT8_BIT_SIZE; i++) { |
||||
tmp = LEFTMOST_BIT_SET(tmp) ? ((tmp << 1) ^ poly) : (tmp << 1); |
||||
} |
||||
seed = tmp & UINT16_MAX; |
||||
} |
||||
return seed; |
||||
} |
||||
|
||||
uint16_t ucrc16_calc_le(const uint8_t *buf, size_t len, uint16_t poly, |
||||
uint16_t seed) |
||||
{ |
||||
assert(buf != NULL); |
||||
for (unsigned c = 0; c < len; c++, buf++) { |
||||
seed ^= (*buf); |
||||
for (unsigned i = 0; i < UINT8_BIT_SIZE; i++) { |
||||
seed = RIGHTMOST_BIT_SET(seed) ? ((seed >> 1) ^ poly) : (seed >> 1); |
||||
} |
||||
} |
||||
return seed; |
||||
} |
||||
|
||||
/** @} */ |
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Freie Universität Berlin |
||||
* |
||||
* 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_ucrc16 CRC16 (lightweight) |
||||
* @ingroup sys_checksum |
||||
* @brief Lightweight CRC16 checksum algorithms |
||||
* |
||||
* This CRC16 implementation does not use pre-calculated lookup tables and is |
||||
* thus very lightweight (memory-wise), but as a caveat slower (about factor 8) |
||||
* than the version with pre-calculated lookup tables. |
||||
* |
||||
* Additionally compared to @ref sys_checksum_crc_ccitt this is a generalized |
||||
* implementation. One can easily exchange generator polynomials and starting |
||||
* seeds. |
||||
* |
||||
* @{ |
||||
* |
||||
* @file |
||||
* @brief ucrc16 definitions |
||||
* |
||||
* @author Martine Lenders <m.lenders@fu-berlin.de> |
||||
*/ |
||||
#ifndef UCRC16_H_ |
||||
#define UCRC16_H_ |
||||
|
||||
#include <stdint.h> |
||||
#include <stdlib.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/**
|
||||
* @{ |
||||
* @brief Various generator polynomials |
||||
*/ |
||||
#define UCRC16_CCITT_POLY_BE (0x1021) /**< CRC16-CCITT polynomial (big-endian) */ |
||||
#define UCRC16_CCITT_POLY_LE (0x8408) /**< CRC16-CCITT polynomial (little-endian) */ |
||||
/** *} */ |
||||
|
||||
/**
|
||||
* @brief Calculate CRC16 (big-endian version) |
||||
* |
||||
* @param[in] buf Start of memory are to checksum |
||||
* @param[in] len Number of bytes in @p buf to calculate checksum for |
||||
* @param[in] poly The generator polynomial for the checksum |
||||
* @param[in] seed The seed (starting value) for the checksum |
||||
* |
||||
* @return Checksum of the specified memory area based on @p seed and @p poly |
||||
* |
||||
* @note The return value is not the complement of the sum but the sum itself |
||||
*/ |
||||
uint16_t ucrc16_calc_be(const uint8_t *buf, size_t len, uint16_t poly, |
||||
uint16_t seed); |
||||
|
||||
/**
|
||||
* @brief Calculate CRC16 (little-endian version) |
||||
* |
||||
* @param[in] buf Start of memory are to checksum |
||||
* @param[in] len Number of bytes in @p buf to calculate checksum for |
||||
* @param[in] poly The generator polynomial for the checksum |
||||
* @param[in] seed The seed (starting value) for the checksum |
||||
* |
||||
* @return Checksum of the specified memory area based on @p seed and @p poly |
||||
* |
||||
* @note The return value is not the complement of the sum but the sum itself |
||||
*/ |
||||
uint16_t ucrc16_calc_le(const uint8_t *buf, size_t len, uint16_t poly, |
||||
uint16_t seed); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* UCRC16_H_ */ |
||||
/** @} */ |
Loading…
Reference in new issue