--- libaitcrc/src/aitcrc.c 2008/11/05 17:02:55 1.1 +++ libaitcrc/src/aitcrc.c 2010/01/20 00:04:45 1.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcrc.c,v 1.1 2008/11/05 17:02:55 misho Exp $ +* $Id: aitcrc.c,v 1.2 2010/01/20 00:04:45 misho Exp $ * *************************************************************************/ #include "global.h" @@ -12,8 +12,10 @@ static int crc_Errno; static char crc_Error[MAX_STR + 1]; +// Adler module const u_long crc_modAdler = 0xFFF1L; +// All known library CRC types ... const crcPoly_t crc_Poly[] = { { 1, (u_long) 0x1, "CRC-1-Parity" }, { 4, (u_long) 0x3, "CRC-4-ITU" }, @@ -33,18 +35,42 @@ const crcPoly_t crc_Poly[] = { // ---------------------------------------------------------- +// +// Error maintenance functions ... +// + +// crc_GetErrno() Get error code of last operation inline int crc_GetErrno() { return crc_Errno; } +// crc_GetError() Get error text of last operation inline const char *crc_GetError() { return crc_Error; } +// crcSetErr() Set error to variables for internal use!!! +inline void crcSetErr(int eno, char *estr, ...) +{ + va_list lst; + + crc_Errno = eno; + memset(crc_Error, 0, MAX_STR + 1); + va_start(lst, estr); + vsnprintf(crc_Error, MAX_STR + 1, estr, lst); + va_end(lst); +} + // ---------------------------------------------------------- +/* + * crcReflect() Reflect all bits of number + * @crcNum = Number for reflection + * @crcBits = Number width bits + * return: -1 error, !=-1 reflecting number + */ inline u_long crcReflect(u_long crcNum, u_char crcBits) { register u_long i, j, rev; @@ -57,6 +83,16 @@ inline u_long crcReflect(u_long crcNum, u_char crcBits return rev; } +/* + * crcCalc() Generic CRC calculation function for many sub variants of CRC algorithms + * @psBuf = Data for calculation + * @bufLen = Length of data + * @crcBits = CRC algorithm bits (1, 4, 5, 6, 7, 8, 10, 11, 12, 15, 16, 24, 30, 32) + * @RevOpts = Options for computation (REVOPTS_REVERTBYTE, REVOPTS_REVERTCRC) + * @initCRC = Initial CRC value + * @xorCRC = Last xor CRC value + * return: -1 error, !=-1 CRC checksum + */ inline u_long crcCalc(u_char * __restrict psBuf, u_int bufLen, u_char crcBits, u_char RevOpts, u_long initCRC, u_long xorCRC) { const u_long bits = sizeof(long) * 8 - crcBits; @@ -152,6 +188,12 @@ inline u_long crcCalc(u_char * __restrict psBuf, u_int // ---------------------------------------------------------- +/* + * crcIP() Checksum in IP communication + * @nBuf = Data for calculation + * @bufLen = Length of data + * return: -1 error, !=-1 Checksum + */ inline u_short crcIP(u_short * __restrict nBuf, int bufLen) { register u_long sum; @@ -172,6 +214,12 @@ inline u_short crcIP(u_short * __restrict nBuf, int bu return (u_short) ~sum; } +/* + * crcFletcher() Fletcher-16 Checksum computing + * @nBuf = Data for calculation + * @bufLen = Length of data + * return: -1 error, !=-1 Checksum + */ inline u_long crcFletcher(u_short * __restrict nBuf, int bufLen) { register u_long s1, s2, clen; @@ -200,6 +248,12 @@ inline u_long crcFletcher(u_short * __restrict nBuf, i return (s2 << 16) | s1; } +/* + * crcAdler() crcAdler-32 Checksum computing + * @psBuf = Data for calculation + * @bufLen = Length of data + * return: -1 error, !=-1 Checksum + */ inline u_long crcAdler(u_char * __restrict psBuf, int bufLen) { register u_long s1, s2, clen;