eive-obsw/thirdparty/tas/tas/crc.h

117 lines
3.6 KiB
C

/***************************************************************************************
* \copyright: 2020-2022 Thales Alenia Space Deutschland GmbH
* \project: multiMIND
* \file: crc.c
* \date: 22.02.2022
* \author: David Woodward
* \brief: CRC algorithms
***************************************************************************************/
#ifndef TAS_D_C_CRC_H
#define TAS_D_C_CRC_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
// NOTE: These defines are in the header as some are needed for (initial) crc function calls
//CRC-32/BZIP2
#define CRC32_TOPBIT (1UL<<31)
#define CRC32_POLYNOMIAL 0x04C11DB7
#define CRC32_INITIAL_REMAINDER 0xFFFFFFFF
#define CRC32_FINAL_XOR_VALUE 0xFFFFFFFF
// CRC-16/CCITT-FALSE
#define CRC16_INITIAL_REMAINDER 0xFFFF
#define CRC16_FINAL_XOR_VALUE 0x0
extern const uint16_t crc16_0x1021_table[256];
extern const uint16_t crc16_0x1021_table_reverse[256];
/**
* \brief CRC-32/BZIP2 algorithm
*/
uint32_t Crc32(const uint8_t *msg, int numBytes, uint32_t remainder);
/**
* \brief CRC-16/CCITT-FALSE (alias CRC-16/AUTOSAR) algorithm,
// initial: 0xFFFF, xorOut: 0x0000, RefIn: false, RefOut: false, polynomial: 0x1021
* using a lookup table
* \param data Data
* \param len Data length
* \param remainder Remainder to be used,
* use initial remainder for non coherent/standalone calculations
* \param final_xor The value that the final result will be xored
* \return CRC result
*/
uint16_t calc_crc16_unreflected(const uint8_t *data, uint32_t len, uint16_t remainder, uint16_t final_xor);
/**
* generates a 16-bit CRC for the said data
*
* @param data input data for CRC
* @param len length of the data
* @return crc Generated 16-bit CRC
*/
void calc_crc16_byte_unreflected(uint16_t *crc16, uint8_t bt);
/**
* \brief CRC-16/CCITT-FALSE (alias CRC-16/AUTOSAR) algorithm,
* polynomial: 0x1021, initial: 0xFFFF, final xor: 0x0,
* using a lookup table
* \param data Data
* \param len Data length
* \param remainder Remainder to be used,
* use initial remainder for non coherent/standalone calculations
* \param final_xor The value that the final result will be xored
* \return CRC result
*/
uint16_t calc_crc16_buff_unreflected(uint8_t *data, uint16_t len);
/**
* \brief CRC-16/X25 algorithm,
* initial: 0xFFFF, xorOut: 0xFFFF, RefIn: true, RefOut: true, polynomial: 0x1021
* using a lookup table
* \param data Data
* \param len Data length
* \param remainder Remainder to be used,
* use initial remainder for non coherent/standalone calculations
* \param final_xor The value that the final result will be xored
* \return CRC result
*/
uint16_t calc_crc16_reflected(const uint8_t *data, uint32_t len, uint16_t remainder, uint16_t final_xor);
/**
* \brief CRC-16/X25 algorithm,
* calculates the crc16 for the next byte, given an already calculated crc16
*
* @param *crc16 : calculated crc16 - the value will be updated
* @param bt : next byte for crc16 calculation
* @return none
*/
void calc_crc16_byte_reflected(uint16_t *crc16, uint8_t bt);
/**
* \brief CRC-16/X25 algorithm,
* initial: 0xFFFF, xorOut: 0xFFFF, RefIn: true, RefOut: true, polynomial: 0x1021
* using a lookup table
* \param data Data
* \param len Data length
* \param remainder Remainder to be used,
* use initial remainder for non coherent/standalone calculations
* \param final_xor The value that the final result will be xored
* \return CRC result
*/
uint16_t calc_crc16_buff_reflected(const uint8_t *data, uint16_t len);
#ifdef __cplusplus
}
#endif
#endif