78 lines
1.6 KiB
C
78 lines
1.6 KiB
C
|
/*
|
||
|
* Copyright (c) 2019 Shawn Nock <shawn@monadnock.ca>
|
||
|
* Copyright (c) 2018 Workaround GmbH.
|
||
|
* Copyright (c) 2017 Intel Corporation.
|
||
|
* Copyright (c) 2017 Nordic Semiconductor ASA
|
||
|
* Copyright (c) 2015 Runtime Inc
|
||
|
* Copyright (c) 2018 Google LLC.
|
||
|
*
|
||
|
* SPDX-License-Identifier: Apache-2.0
|
||
|
*/
|
||
|
/** @file
|
||
|
* @brief CRC computation function
|
||
|
*/
|
||
|
|
||
|
#ifndef ZEPHYR_INCLUDE_CRC_H_
|
||
|
#define ZEPHYR_INCLUDE_CRC_H_
|
||
|
|
||
|
#include <stdbool.h>
|
||
|
#include <stddef.h>
|
||
|
|
||
|
#ifdef __cplusplus
|
||
|
extern "C" {
|
||
|
#endif
|
||
|
|
||
|
/**
|
||
|
* @defgroup checksum Checksum
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* @defgroup crc CRC
|
||
|
* @ingroup checksum
|
||
|
* @{
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* @brief Generic function for computing CRC 16
|
||
|
*
|
||
|
* Compute CRC 16 by passing in the address of the input, the input length
|
||
|
* and polynomial used in addition to the initial value.
|
||
|
*
|
||
|
* @param src Input bytes for the computation
|
||
|
* @param len Length of the input in bytes
|
||
|
* @param polynomial The polynomial to use omitting the leading x^16
|
||
|
* coefficient
|
||
|
* @param initial_value Initial value for the CRC computation
|
||
|
* @param pad Adds padding with zeros at the end of input bytes
|
||
|
*
|
||
|
* @return The computed CRC16 value
|
||
|
*/
|
||
|
uint16_t crc16(const uint8_t *src, size_t len, uint16_t polynomial,
|
||
|
uint16_t initial_value, bool pad);
|
||
|
|
||
|
/**
|
||
|
* @brief Compute ANSI variant of CRC 16
|
||
|
*
|
||
|
* ANSI variant of CRC 16 is using 0x8005 as its polynomial with the initial
|
||
|
* value set to 0xffff.
|
||
|
*
|
||
|
* @param src Input bytes for the computation
|
||
|
* @param len Length of the input in bytes
|
||
|
*
|
||
|
* @return The computed CRC16 value
|
||
|
*/
|
||
|
static inline uint16_t crc16_ansi(const uint8_t *src, size_t len)
|
||
|
{
|
||
|
return crc16(src, len, 0x8005, 0xffff, true);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @}
|
||
|
*/
|
||
|
|
||
|
#ifdef __cplusplus
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
#endif
|