git-svn-id: http://moon:8086/svn/software/trunk/libsrc/crc@1 b431acfa-c32f-4a4a-93f1-934dc6c82436
47 lines
1.1 KiB
C
Executable File
47 lines
1.1 KiB
C
Executable File
/* CRC16.C */
|
|
|
|
/* Developed 1990-1997 by Rob Swindell; PO Box 501, Yorba Linda, CA 92885 */
|
|
|
|
/* 16-bit CRC routines */
|
|
|
|
#include <stdint.h>
|
|
|
|
/****************************************************************************/
|
|
/* Updates 16-bit "rcrc" with character 'ch' */
|
|
/****************************************************************************/
|
|
uint16_t Upd_crc16(uint8_t ch, uint16_t poly, uint16_t crc)
|
|
{
|
|
uint16_t i, cy;
|
|
uint8_t nch = ch;
|
|
|
|
for (i=0; i<8; i++)
|
|
{
|
|
cy = crc & 0x8000;
|
|
crc <<= 1;
|
|
if (nch & 0x80)
|
|
crc |= 1;
|
|
|
|
nch<<=1;
|
|
|
|
if (cy)
|
|
crc ^= poly;
|
|
}
|
|
return crc;
|
|
}
|
|
|
|
/****************************************************************************/
|
|
/* Returns 16-crc of string (not counting terminating NULL) */
|
|
/****************************************************************************/
|
|
uint16_t Crc16(uint16_t poly, uint16_t crc, uint8_t *pBuf, uint32_t len)
|
|
{
|
|
while(len)
|
|
{
|
|
crc = Upd_crc16(*pBuf, poly, crc);
|
|
pBuf++;
|
|
len--;
|
|
|
|
}
|
|
|
|
return crc;
|
|
}
|