crc.h 2.68 KB
/***********************************************************************CHDR**
 * NAME:      crc.h
 *
 * PURPOSE:   Provides functions for calculating Cyclic Redundancy Checks
 *            (CRCs).
 *
 *****************************************************************************/

#ifndef	INCLUDED_CRC
#define	INCLUDED_CRC

/*****************************************************************************
 * Internal constants
 *****************************************************************************/
#define	CRC_BITS_BYTE	8
#define	CRC_BYTE_MASK	0xFF
#define	CRC_UCHAR_MAX	255

/***********************************************************************EXDT**
 * NAME:      CRC_VAL
 *
 * PURPOSE:   Holds the CRC 'register' during calculation and can be cast to
 *            an integer for output
 *
 *****************************************************************************/
typedef unsigned int	CRC_VAL;

/***********************************************************************EXDT**
 * NAME:      CRC_BOOL
 *
 * PURPOSE:   True / false boolean value
 *
 *****************************************************************************/
typedef int	CRC_BOOL;
#define	CRC_TRUE	1
#define	CRC_FALSE	0

/***********************************************************************EXDT**
 * NAME:      CRC_TBL
 *
 * PURPOSE:   Stores CRC the parameters and look-up table required for doing
 *            the CRC calculation
 *
 *****************************************************************************/
typedef struct crc_tbl
{
  CRC_VAL pre_calc[CRC_UCHAR_MAX + 1];
  int width;
  CRC_VAL poly;
  CRC_VAL init_val;
  CRC_VAL xor_out;
  CRC_BOOL is_rev;
} CRC_TBL;

/***********************************************************************EXCN**
 * NAME:      CRC_STD_*
 *
 * PURPOSE:   Constants to identify standard varieties of CRCs
 *
 *****************************************************************************/
#define CRC_STD_CRC16     0
#define CRC_DNP_CRC16     1
#define CRC_MOD_CRC16     2
#define CRC_STD_XMODEM    3
#define CRC_STD_ZMODEM16  4
#define CRC_STD_CRC32     5
#define CRC_STD_JAM       6

/*****************************************************************************
 * Function prototypes
 *****************************************************************************/
CRC_BOOL	crc_mk_tbl(CRC_TBL*, int, CRC_VAL, CRC_VAL, CRC_VAL, int);
CRC_BOOL	crc_mk_std_tbl(CRC_TBL*, int);
CRC_VAL		crc_final(CRC_VAL*, CRC_TBL*);

void		crc_init(CRC_VAL*, CRC_TBL*);
void		crc_add_chr(CRC_VAL*, CRC_TBL*, unsigned char, int);
void		crc_add_str(CRC_VAL*, CRC_TBL*, unsigned char*);
void		crc_add_data(CRC_VAL*, CRC_TBL*, unsigned char*, int);

#endif