📄 lib_crc.c
字号:
/*******************************************************************\
* *
* unsigned short update_crc_dnp( unsigned long crc, char c ); *
* *
* The function update_crc_dnp calculates a new CRC-DNP value *
* based on the previous value of the CRC and the next byte *
* of the data to be checked. *
* *
\*******************************************************************/
unsigned short update_crc_dnp( unsigned short crc, char c ) {
unsigned short tmp, short_c;
short_c = 0x00ff & (unsigned short) c;
if ( ! crc_tabdnp_init ) init_crcdnp_tab();
tmp = crc ^ short_c;
crc = (crc >> 8) ^ crc_tabdnp[ tmp & 0xff ];
return crc;
} /* update_crc_dnp */
/*******************************************************************\
* *
* unsigned long update_crc_32( unsigned long crc, char c ); *
* *
* The function update_crc_32 calculates a new CRC-32 value *
* based on the previous value of the CRC and the next byte *
* of the data to be checked. *
* *
\*******************************************************************/
unsigned long update_crc_32( unsigned long crc, char c ) {
unsigned long tmp, long_c;
long_c = 0x000000ffL & (unsigned long) c;
if ( ! crc_tab32_init ) init_crc32_tab();
tmp = crc ^ long_c;
crc = (crc >> 8) ^ crc_tab32[ tmp & 0xff ];
return crc;
} /* update_crc_32 */
/*******************************************************************\
* *
* static void init_crc16_tab( void ); *
* *
* The function init_crc16_tab() is used to fill the array *
* for calculation of the CRC-16 with values. *
* *
\*******************************************************************/
static void init_crc16_tab( void ) {
int i, j;
unsigned short crc, c;
for (i=0; i<256; i++) {
crc = 0;
c = (unsigned short) i;
for (j=0; j<8; j++) {
if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ P_16;
else crc = crc >> 1;
c = c >> 1;
}
crc_tab16[i] = crc;
}
crc_tab16_init = TRUE;
} /* init_crc16_tab */
/*******************************************************************\
* *
* static void init_crcdnp_tab( void ); *
* *
* The function init_crcdnp_tab() is used to fill the array *
* for calculation of the CRC-DNP with values. *
* *
\*******************************************************************/
static void init_crcdnp_tab( void ) {
int i, j;
unsigned short crc, c;
for (i=0; i<256; i++) {
crc = 0;
c = (unsigned short) i;
for (j=0; j<8; j++) {
if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ P_DNP;
else crc = crc >> 1;
c = c >> 1;
}
crc_tabdnp[i] = crc;
}
crc_tabdnp_init = TRUE;
} /* init_crcdnp_tab */
/*******************************************************************\
* *
* static void init_crc32_tab( void ); *
* *
* The function init_crc32_tab() is used to fill the array *
* for calculation of the CRC-32 with values. *
* *
\*******************************************************************/
static void init_crc32_tab( void ) {
int i, j;
unsigned long crc;
for (i=0; i<256; i++) {
crc = (unsigned long) i;
for (j=0; j<8; j++) {
if ( crc & 0x00000001L ) crc = ( crc >> 1 ) ^ P_32;
else crc = crc >> 1;
}
crc_tab32[i] = crc;
}
crc_tab32_init = TRUE;
} /* init_crc32_tab */
/*******************************************************************\
* *
* static void init_crcccitt_tab( void ); *
* *
* The function init_crcccitt_tab() is used to fill the array *
* for calculation of the CRC-CCITT with values. *
* *
\*******************************************************************/
static void init_crcccitt_tab( void ) {
int i, j;
unsigned short crc, c;
for (i=0; i<256; i++) {
crc = 0;
c = ((unsigned short) i) << 8;
for (j=0; j<8; j++) {
if ( (crc ^ c) & 0x8000 ) crc = ( crc << 1 ) ^ P_CCITT;
else crc = crc << 1;
c = c << 1;
}
crc_tabccitt[i] = crc;
}
crc_tabccitt_init = TRUE;
} /* init_crcccitt_tab */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -