1 Star 6 Fork 2

Honrun / CRC32-16-8

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
DevicesCRC.c 12.54 KB
一键复制 编辑 原始数据 按行查看 历史
Honrun 提交于 2023-08-19 16:51 . 优化CRC计算函数
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
#include "stdio.h"
#include "stdint.h"
#include "DevicesCRC.h"
uint8_t ucCRC7_MMC(uint8_t *pucInitCRC, void *pvDataBuff, int32_t iLength)
{
uint8_t ucPolynomial = 0x12, ucInputCRC = 0, *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pucInitCRC != NULL)
ucInputCRC = *pucInitCRC;
while((iLength--) > 0)
{
ucInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if (ucInputCRC & 0x80)
ucInputCRC = (ucInputCRC << 1) ^ ucPolynomial;
else
ucInputCRC <<= 1;
}
}
return ucInputCRC >> 1;
}
uint8_t ucCRC8(uint8_t *pucInitCRC, void *pvDataBuff, int32_t iLength)
{
uint8_t ucPolynomial = 0x07, ucInputCRC = 0, *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pucInitCRC != NULL)
ucInputCRC = *pucInitCRC;
while((iLength--) > 0)
{
ucInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if (ucInputCRC & 0x80)
ucInputCRC = (ucInputCRC << 1) ^ ucPolynomial;
else
ucInputCRC <<= 1;
}
}
return ucInputCRC;
}
uint8_t ucCRC8_ITU(uint8_t *pucInitCRC, void *pvDataBuff, int32_t iLength)
{
uint8_t ucPolynomial = 0x07, ucInputCRC = 0, *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pucInitCRC != NULL)
ucInputCRC = *pucInitCRC;
while((iLength--) > 0)
{
ucInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if (ucInputCRC & 0x80)
ucInputCRC = (ucInputCRC << 1) ^ ucPolynomial;
else
ucInputCRC <<= 1;
}
}
return ucInputCRC ^ 0x55;
}
uint8_t ucCRC8_ROHC(uint8_t *pucInitCRC, void *pvDataBuff, int32_t iLength)
{
uint8_t ucPolynomial = 0xE0, ucInputCRC = 0xFF, *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pucInitCRC != NULL)
ucInputCRC = *pucInitCRC;
while((iLength--) > 0)
{
ucInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if (ucInputCRC & 1)
ucInputCRC = (ucInputCRC >> 1) ^ ucPolynomial;
else
ucInputCRC >>= 1;
}
}
return ucInputCRC;
}
uint8_t ucCRC8_MAXIM(uint8_t *pucInitCRC, void *pvDataBuff, int32_t iLength)
{
uint8_t ucPolynomial = 0x8C, ucInputCRC = 0, *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pucInitCRC != NULL)
ucInputCRC = *pucInitCRC;
while((iLength--) > 0)
{
ucInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if (ucInputCRC & 1)
ucInputCRC = (ucInputCRC >> 1) ^ ucPolynomial;
else
ucInputCRC >>= 1;
}
}
return ucInputCRC;
}
uint16_t usCRC16_IBM(uint16_t *pusInitCRC, void *pvDataBuff, int32_t iLength)
{
uint16_t usPolynomial = 0xA001, usInputCRC = 0xFFFF;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pusInitCRC != NULL)
usInputCRC = *pusInitCRC;
while((iLength--) > 0)
{
usInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if (usInputCRC & 1)
usInputCRC = (usInputCRC >> 1) ^ usPolynomial;
else
usInputCRC >>= 1;
}
}
return usInputCRC;
}
uint16_t usCRC16_MAXIM(uint16_t *pusInitCRC, void *pvDataBuff, int32_t iLength)
{
uint16_t usPolynomial = 0xA001, usInputCRC = 0;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pusInitCRC != NULL)
usInputCRC = *pusInitCRC;
while((iLength--) > 0)
{
usInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if (usInputCRC & 1)
usInputCRC = (usInputCRC >> 1) ^ usPolynomial;
else
usInputCRC >>= 1;
}
}
return ~usInputCRC;
}
uint16_t usCRC16_USB(uint16_t *pusInitCRC, void *pvDataBuff, int32_t iLength)
{
uint16_t usPolynomial = 0xA001, usInputCRC = 0xFFFF;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pusInitCRC != NULL)
usInputCRC = *pusInitCRC;
while((iLength--) > 0)
{
usInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if (usInputCRC & 1)
usInputCRC = (usInputCRC >> 1) ^ usPolynomial;
else
usInputCRC >>= 1;
}
}
return ~usInputCRC;
}
uint16_t usCRC16_MODBUS(uint16_t *pusInitCRC, void *pvDataBuff, int32_t iLength)
{
uint16_t usPolynomial = 0xA001, usInputCRC = 0xFFFF;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pusInitCRC != NULL)
usInputCRC = *pusInitCRC;
while((iLength--) > 0)
{
usInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if (usInputCRC & 1)
usInputCRC = (usInputCRC >> 1) ^ usPolynomial;
else
usInputCRC >>= 1;
}
}
return usInputCRC;
}
uint16_t usCRC16_CCITT(uint16_t *pusInitCRC, void *pvDataBuff, int32_t iLength)
{
uint16_t usPolynomial = 0x8408, usInputCRC = 0;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pusInitCRC != NULL)
usInputCRC = *pusInitCRC;
while((iLength--) > 0)
{
usInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if (usInputCRC & 1)
usInputCRC = (usInputCRC >> 1) ^ usPolynomial;
else
usInputCRC >>= 1;
}
}
return usInputCRC;
}
uint16_t usCRC16_CCITT_FALSE(uint16_t *pusInitCRC, void *pvDataBuff, int32_t iLength)
{
uint16_t usPolynomial = 0x1021, usInputCRC = 0xFFFF;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pusInitCRC != NULL)
usInputCRC = *pusInitCRC;
while((iLength--) > 0)
{
usInputCRC ^= (uint16_t)(*pucDataBuff++) << 8;
for(i = 0; i < 8; ++i)
{
if (usInputCRC & 0x8000)
usInputCRC = (usInputCRC << 1) ^ usPolynomial;
else
usInputCRC <<= 1;
}
}
return usInputCRC;
}
uint16_t usCRC16_X25(uint16_t *pusInitCRC, void *pvDataBuff, int32_t iLength)
{
uint16_t usPolynomial = 0x8408, usInputCRC = 0xFFFF;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pusInitCRC != NULL)
usInputCRC = *pusInitCRC;
while((iLength--) > 0)
{
usInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if (usInputCRC & 1)
usInputCRC = (usInputCRC >> 1) ^ usPolynomial;
else
usInputCRC >>= 1;
}
}
return ~usInputCRC;
}
uint16_t usCRC16_XMODEM(uint16_t *pusInitCRC, void *pvDataBuff, int32_t iLength)
{
uint16_t usPolynomial = 0x1021, usInputCRC = 0;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pusInitCRC != NULL)
usInputCRC = *pusInitCRC;
while((iLength--) > 0)
{
usInputCRC ^= (uint16_t)(*pucDataBuff++) << 8;
for(i = 0; i < 8; ++i)
{
if (usInputCRC & 0x8000)
usInputCRC = (usInputCRC << 1) ^ usPolynomial;
else
usInputCRC <<= 1;
}
}
return usInputCRC;
}
uint16_t usCRC16_DNP(uint16_t *pusInitCRC, void *pvDataBuff, int32_t iLength)
{
uint16_t usPolynomial = 0xA6BC, usInputCRC = 0;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(pusInitCRC != NULL)
usInputCRC = *pusInitCRC;
while((iLength--) > 0)
{
usInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if (usInputCRC & 1)
usInputCRC = (usInputCRC >> 1) ^ usPolynomial;
else
usInputCRC >>= 1;
}
}
return ~usInputCRC;
}
uint32_t uiReflect(uint32_t uiData, uint8_t ucLength)
{
uint32_t uiMask = 1 << (ucLength - 1), uiMaskRef = 1, uiDataReturn = 0;
for(; uiMask; uiMask >>= 1)
{
if(uiData & uiMask)
uiDataReturn |= uiMaskRef;
uiMaskRef <<= 1;
}
return uiDataReturn;
}
uint32_t uiCRC32(uint32_t *puiInitCRC, void *pvDataBuff, int32_t iLength)
{
uint32_t uiPolynomial = 0x04C11DB7, uiInputCRC = 0xFFFFFFFF;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(puiInitCRC != NULL)
uiInputCRC = *puiInitCRC;
uiPolynomial = uiReflect(uiPolynomial, 32);
while((iLength--) > 0)
{
uiInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if(uiInputCRC & 1)
uiInputCRC = (uiInputCRC >> 1) ^ uiPolynomial;
else
uiInputCRC >>= 1;
}
}
return ~uiInputCRC;
}
uint32_t uiCRC32_BZIP2(uint32_t *puiInitCRC, void *pvDataBuff, int32_t iLength)
{
uint32_t uiPolynomial = 0x04C11DB7, uiInputCRC = 0xFFFFFFFF;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(puiInitCRC != NULL)
uiInputCRC = *puiInitCRC;
while((iLength--) > 0)
{
uiInputCRC ^= (uint32_t)(*pucDataBuff++) << 24;
for(i = 0; i < 8; ++i)
{
if(uiInputCRC & 0x80000000)
uiInputCRC = (uiInputCRC << 1) ^ uiPolynomial;
else
uiInputCRC <<= 1;
}
}
return ~uiInputCRC;
}
uint32_t uiCRC32_MPEG2(uint32_t *puiInitCRC, void *pvDataBuff, int32_t iLength)
{
uint32_t uiPolynomial = 0x04C11DB7, uiInputCRC = 0xFFFFFFFF;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(puiInitCRC != NULL)
uiInputCRC = *puiInitCRC;
while((iLength--) > 0)
{
uiInputCRC ^= (uint32_t)(*pucDataBuff++) << 24;
for(i = 0; i < 8; ++i)
{
if(uiInputCRC & 0x80000000)
uiInputCRC = (uiInputCRC << 1) ^ uiPolynomial;
else
uiInputCRC <<= 1;
}
}
return uiInputCRC;
}
uint32_t uiCRC32_POSIX(uint32_t *puiInitCRC, void *pvDataBuff, int32_t iLength)
{
uint32_t uiPolynomial = 0x04C11DB7, uiInputCRC = 0;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(puiInitCRC != NULL)
uiInputCRC = *puiInitCRC;
while((iLength--) > 0)
{
uiInputCRC ^= (uint32_t)(*pucDataBuff++) << 24;
for(i = 0; i < 8; ++i)
{
if(uiInputCRC & 0x80000000)
uiInputCRC = (uiInputCRC << 1) ^ uiPolynomial;
else
uiInputCRC <<= 1;
}
}
return ~uiInputCRC;
}
uint32_t uiCRC32_JAMCRC(uint32_t *puiInitCRC, void *pvDataBuff, int32_t iLength)
{
uint32_t uiPolynomial = 0x04C11DB7, uiInputCRC = 0xFFFFFFFF;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(puiInitCRC != NULL)
uiInputCRC = *puiInitCRC;
uiPolynomial = uiReflect(uiPolynomial, 32);
while((iLength--) > 0)
{
uiInputCRC ^= *pucDataBuff++;
for(i = 0; i < 8; ++i)
{
if(uiInputCRC & 1)
uiInputCRC = (uiInputCRC >> 1) ^ uiPolynomial;
else
uiInputCRC >>= 1;
}
}
return uiInputCRC;
}
uint32_t uiCRC32_STM32(uint32_t *puiInitCRC, void *pvDataBuff, int32_t iLength)
{
uint32_t uiPolynomial = 0x04C11DB7, uiInputCRC = 0xFFFFFFFF, xbit = 0x80000000, uiDataTemp = 0;
uint8_t *pucDataBuff = pvDataBuff;
int8_t i = 0;
if(pucDataBuff == NULL)
return 0;
if(puiInitCRC != NULL)
uiInputCRC = *puiInitCRC;
while((iLength--) > 0)
{
uiDataTemp = *pucDataBuff++;
xbit = 0x80000000;
for(i = 0; i < 8; ++i)
{
if(uiInputCRC & 0x80000000)
uiInputCRC = (uiInputCRC << 1) ^ uiPolynomial;
else
uiInputCRC <<= 1;
if(uiDataTemp & xbit)
uiInputCRC ^= uiPolynomial;
xbit >>= 1;
}
}
return uiInputCRC;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/honrun_he/crc32-16-8.git
git@gitee.com:honrun_he/crc32-16-8.git
honrun_he
crc32-16-8
CRC32-16-8
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891