#include "lib_crc.h"
/*******************************************************************\
* *
* Library : lib_crc *
* File : lib_crc.c *
* Author : Lammert Bies 1999-2005 *
* E-mail : [email protected] *
* Language : ANSI C *
* *
* *
* Description *
* =========== *
* *
* The file lib_crc.c contains the private and public func- *
* tions used for the calculation of CRC-16, CRC-CCITT and *
* CRC-32 cyclic redundancy values. *
* *
* *
* Dependencies *
* ============ *
* *
* lib_crc.h CRC definitions and prototypes *
* *
* *
* Modification history *
* ==================== *
* *
* Date Version Comment *
* *
* 2005-05-14 1.12 Added CRC-CCITT with start value 0 *
* *
* 2005-02-05 1.11 Fixed bug in CRC-DNP routine *
* *
* 2005-02-04 1.10 Added CRC-DNP routines *
* *
* 1999-02-21 1.01 Added FALSE and TRUE mnemonics *
* *
* 1999-01-22 1.00 Initial source *
* *
\*******************************************************************/
/*******************************************************************\
* *
* #define P_xxxx *
* *
* The CRC's are computed using polynomials. The coefficients *
* for the algorithms are defined by the following constants. *
* *
\*******************************************************************/
#define P_16 0xA001
#define P_32 0xEDB88320L
#define P_CCITT 0x1021
#define P_DNP 0xA6BC
/*******************************************************************\
* *
* static int crc_tab...init *
* static unsigned ... crc_tab...[] *
* *
* The algorithms use tables with precalculated values. This *
* speeds up the calculation dramaticaly. The first time the *
* CRC function is called, the table for that specific calcu- *
* lation is set up. The ...init variables are used to deter- *
* mine if the initialization has taken place. The calculated *
* values are stored in the crc_tab... arrays. *
* *
* The variables are declared static. This makes them invisi- *
* ble for other modules of the program. *
* *
\*******************************************************************/
static int crc_tab16_init = FALSE;
static int crc_tab32_init = FALSE;
static int crc_tabccitt_init = FALSE;
static int crc_tabdnp_init = FALSE;
static unsigned short crc_tab16[256];
static unsigned long crc_tab32[256];
static unsigned short crc_tabccitt[256];
static unsigned short crc_tabdnp[256];
/*******************************************************************\
* *
* static void init_crc...tab(); *
* *
* Three local functions are used to initialize the tables *
* with values for the algorithm. *
* *
\*******************************************************************/
static void init_crc16_tab( void );
static void init_crc32_tab( void );
static void init_crcccitt_tab( void );
static void init_crcdnp_tab( void );
/*******************************************************************\
* *
* unsigned short update_crc_ccitt( unsigned long crc, char c ); *
* *
* The function update_crc_ccitt calculates a new CRC-CCITT *
* value based on the previous value of the CRC and the next *
* byte of the data to be checked. *
* *
\*******************************************************************/
unsigned short update_crc_ccitt( unsigned short crc, char c ) {
unsigned short tmp, short_c;
short_c = 0x00ff & (unsigned short) c;
if ( ! crc_tabccitt_init ) init_crcccitt_tab();
tmp = (crc >> 8) ^ short_c;
crc = (crc << 8) ^ crc_tabccitt[tmp];
return crc;
} /* update_crc_ccitt */
/*******************************************************************\
* *
* unsigned short update_crc_16( unsigned long crc, char c ); *
* *
* The function update_crc_16 calculates a new CRC-16 value *
* based on the previous value of the CRC and the next byte *
* of the data to be checked. *
* *
\*******************************************************************/
unsigned short update_crc_16( unsigned short crc, char c ) {
unsigned short tmp, short_c;
short_c = 0x00ff & (unsigned short) c;