--- libaitcrc/src/aitcrc.c 2011/04/28 20:28:20 1.4 +++ libaitcrc/src/aitcrc.c 2012/07/04 14:53:43 1.5 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcrc.c,v 1.4 2011/04/28 20:28:20 misho Exp $ +* $Id: aitcrc.c,v 1.5 2012/07/04 14:53:43 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -49,10 +49,10 @@ SUCH DAMAGE. static int crc_Errno; static char crc_Error[MAX_STR + 1]; -// Adler module +/* Adler module */ const u_int crc_modAdler = 0xFFF1L; -// All known library CRC types ... +/* All known library CRC types ... */ const crcPoly_t crc_Poly[] = { { 1, (u_int) 0x1, "CRC-1-Parity" }, { 4, (u_int) 0x3, "CRC-4-ITU" }, @@ -70,40 +70,42 @@ const crcPoly_t crc_Poly[] = { { 32, (u_int) 0x04C11DB7, "CRC-32-802.3" } }; -// ---------------------------------------------------------- -// -// Error maintenance functions ... -// +/* + * Error maintenance functions ... + */ -// crc_GetErrno() Get error code of last operation -inline int crc_GetErrno() +/* 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() +/* 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, ...) +/* crc_SetErr() Set error to variables for internal use!!! */ +inline void +crc_SetErr(int eno, char *estr, ...) { va_list lst; crc_Errno = eno; - memset(crc_Error, 0, MAX_STR + 1); + memset(crc_Error, 0, sizeof crc_Error); va_start(lst, estr); - vsnprintf(crc_Error, MAX_STR + 1, estr, lst); + vsnprintf(crc_Error, sizeof crc_Error, estr, lst); va_end(lst); } -// ---------------------------------------------------------- /* - * crcReflect() Reflect all bits of number + * crcReflect() - Reflect all bits of number + * * @crcNum = Number for reflection * @crcBits = Number width bits * return: -1 error, !=-1 reflecting number @@ -122,7 +124,8 @@ crcReflect(u_int crcNum, u_char crcBits) } /* - * crcCalc() Generic CRC calculation function for many sub variants of CRC algorithms + * 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) @@ -138,11 +141,7 @@ crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha u_int poly, crchibit, crc; register u_int i, j, b, ch; - if (!psBuf) { - crc_Errno = 1; - strlcpy(crc_Error, "crcCalc(): Invalid parameters!", MAX_STR + 1); - return -1; - } + assert(psBuf); switch (crcBits) { case 1: @@ -188,8 +187,7 @@ crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha poly = crc_Poly[13].poly_num; break; default: - crc_Errno = 2; - strlcpy(crc_Error, "crcCalc(): Unsupported CRC method!!!", MAX_STR + 1); + crc_SetErr(EINVAL, "crcCalc(): Unsupported CRC method!!!"); return -1; } poly <<= bits; @@ -225,27 +223,28 @@ crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha return crc; } -// ---------------------------------------------------------- /* - * crcIP() Checksum in IP communication - * @nBuf = Data for calculation + * crcIP() - Checksum in IP communication + * + * @buf = Data for calculation * @bufLen = Length of data * return: -1 error, !=-1 Checksum */ inline u_short -crcIP(u_short * __restrict nBuf, int bufLen) +crcIP(u_char * __restrict buf, int bufLen) { register u_int sum; + u_short last = 0, *nBuf = (u_short*) buf; - if (!nBuf) { - crc_Errno = 1; - strlcpy(crc_Error, "crcIP(): Invalid parameters!", MAX_STR + 1); - return -1; - } + assert(buf); - for (sum = 0; bufLen; bufLen--) + for (sum = 0; bufLen && bufLen > 1; bufLen -= 2) sum += *nBuf++; + if (bufLen == 1) { + *(u_char*)(&last) += *(u_char*) nBuf; + sum += last; + } sum = (sum >> 16) + (sum & 0xFFFF); sum += sum >> 16; @@ -255,7 +254,8 @@ crcIP(u_short * __restrict nBuf, int bufLen) } /* - * crcFletcher16() Fletcher-16 Checksum computing + * crcFletcher16() - Fletcher-16 Checksum computing + * * @nBuf = Data for calculation * @bufLen = Length of data * return: -1 error, !=-1 Checksum @@ -266,11 +266,7 @@ crcFletcher16(u_short * __restrict nBuf, int bufLen) register u_short s1, s2; register u_int clen; - if (!nBuf) { - crc_Errno = 1; - strlcpy(crc_Error, "crcFletcher16(): Invalid parameters!", MAX_STR + 1); - return -1; - } + assert(nBuf); s1 = s2 = 0xFF; while (bufLen) { @@ -291,7 +287,8 @@ crcFletcher16(u_short * __restrict nBuf, int bufLen) } /* - * crcFletcher() Fletcher-32 Checksum computing + * crcFletcher() - Fletcher-32 Checksum computing + * * @nBuf = Data for calculation * @bufLen = Length of data * return: -1 error, !=-1 Checksum @@ -301,11 +298,7 @@ crcFletcher(u_short * __restrict nBuf, int bufLen) { register u_int s1, s2, clen; - if (!nBuf) { - crc_Errno = 1; - strlcpy(crc_Error, "crcFletcher(): Invalid parameters!", MAX_STR + 1); - return -1; - } + assert(nBuf); s1 = s2 = 0xFFFF; while (bufLen) { @@ -326,7 +319,8 @@ crcFletcher(u_short * __restrict nBuf, int bufLen) } /* - * crcAdler() crcAdler-32 Checksum computing + * crcAdler() - crcAdler-32 Checksum computing + * * @psBuf = Data for calculation * @bufLen = Length of data * return: -1 error, !=-1 Checksum @@ -336,11 +330,7 @@ crcAdler(u_char * __restrict psBuf, int bufLen) { register u_int s1, s2, clen; - if (!psBuf) { - crc_Errno = 1; - strlcpy(crc_Error, "crcAdler(): Invalid parameters!", MAX_STR + 1); - return -1; - } + assert(psBuf); s1 = 1L; s2 ^= s2;