--- libaitcrc/src/aitcrc.c 2008/11/05 17:35:20 1.1.1.1.2.1 +++ libaitcrc/src/aitcrc.c 2011/09/14 14:33:56 1.4.2.2 @@ -3,9 +3,46 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcrc.c,v 1.1.1.1.2.1 2008/11/05 17:35:20 misho Exp $ +* $Id: aitcrc.c,v 1.4.2.2 2011/09/14 14:33:56 misho Exp $ * -*************************************************************************/ +************************************************************************** +The ELWIX and AITNET software is distributed under the following +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 + by Michael Pounov . All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: +This product includes software developed by Michael Pounov +ELWIX - Embedded LightWeight unIX and its contributors. +4. Neither the name of AITNET nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. +*/ #include "global.h" @@ -13,24 +50,24 @@ static int crc_Errno; static char crc_Error[MAX_STR + 1]; // Adler module -const u_long crc_modAdler = 0xFFF1L; +const u_int 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" }, - { 5, (u_long) 0x15, "CRC-5-ITU" }, - { 6, (u_long) 0x3, "CRC-6-ITU" }, - { 7, (u_long) 0x9, "CRC-7-MMC" }, - { 8, (u_long) 0x8D, "CRC-8-CCITT" }, - { 10, (u_long) 0x233, "CRC-10" }, - { 11, (u_long) 0x385, "CRC-11-FlexRay" }, - { 12, (u_long) 0x80F, "CRC-12-Telco" }, - { 15, (u_long) 0x4599, "CRC-15-CAN" }, - { 16, (u_long) 0x8005, "CRC-16-IBM" }, - { 24, (u_long) 0x864CFB, "CRC-24-Radix64" }, - { 30, (u_long) 0x2030B9C7, "CRC-30-CDMA" }, - { 32, (u_long) 0x04C11DB7, "CRC-32-802.3" } + { 1, (u_int) 0x1, "CRC-1-Parity" }, + { 4, (u_int) 0x3, "CRC-4-ITU" }, + { 5, (u_int) 0x15, "CRC-5-ITU" }, + { 6, (u_int) 0x3, "CRC-6-ITU" }, + { 7, (u_int) 0x9, "CRC-7-MMC" }, + { 8, (u_int) 0x8D, "CRC-8-CCITT" }, + { 10, (u_int) 0x233, "CRC-10" }, + { 11, (u_int) 0x385, "CRC-11-FlexRay" }, + { 12, (u_int) 0x80F, "CRC-12-Telco" }, + { 15, (u_int) 0x4599, "CRC-15-CAN" }, + { 16, (u_int) 0x8005, "CRC-16-IBM" }, + { 24, (u_int) 0x864CFB, "CRC-24-Radix64" }, + { 30, (u_int) 0x2030B9C7, "CRC-30-CDMA" }, + { 32, (u_int) 0x04C11DB7, "CRC-32-802.3" } }; // ---------------------------------------------------------- @@ -40,17 +77,32 @@ const crcPoly_t crc_Poly[] = { // // crc_GetErrno() Get error code of last operation -inline int crc_GetErrno() +inline int +crc_GetErrno() { return crc_Errno; } // crc_GetError() Get error text of last operation -inline const char *crc_GetError() +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); +} + // ---------------------------------------------------------- /* @@ -59,11 +111,12 @@ inline const char *crc_GetError() * @crcBits = Number width bits * return: -1 error, !=-1 reflecting number */ -inline u_long crcReflect(u_long crcNum, u_char crcBits) +inline u_int +crcReflect(u_int crcNum, u_char crcBits) { - register u_long i, j, rev; + register u_int i, j, rev; - for (i = (u_long) 1 << (crcBits - 1), j = 1, rev ^= rev; i; i >>= 1, j <<= 1) + for (i = (u_int) 1 << (crcBits - 1), j = 1, rev ^= rev; i; i >>= 1, j <<= 1) if (crcNum & i) rev |= j; @@ -81,11 +134,12 @@ inline u_long crcReflect(u_long crcNum, u_char crcBits * @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) +inline u_int +crcCalc(u_char * __restrict psBuf, u_int bufLen, u_char crcBits, u_char RevOpts, u_int initCRC, u_int xorCRC) { - const u_long bits = sizeof(long) * 8 - crcBits; - u_long poly, crchibit, crc; - register u_long i, j, b, ch; + const u_int bits = sizeof(int) * 8 - crcBits; + u_int poly, crchibit, crc; + register u_int i, j, b, ch; if (!psBuf) { crc_Errno = 1; @@ -143,12 +197,12 @@ inline u_long crcCalc(u_char * __restrict psBuf, u_int } poly <<= bits; - crchibit = (u_long) 1 << (crcBits - 1); + crchibit = (u_int) 1 << (crcBits - 1); crchibit <<= bits; crc = initCRC << bits; for (i = 0; i < bufLen; i++) { - ch = (u_long) *psBuf++; + ch = (u_int) *psBuf++; if (RevOpts & REVOPTS_REVERTBYTE) ch = crcReflect(ch, 8); @@ -164,7 +218,7 @@ inline u_long crcCalc(u_char * __restrict psBuf, u_int } if (RevOpts & REVOPTS_REVERTCRC) - crc = crcReflect(crc, sizeof(long) * 8); + crc = crcReflect(crc, sizeof(int) * 8); crc ^= xorCRC << bits; crc &= (((crchibit - 1) << 1) | 1); if (!(RevOpts & REVOPTS_REVERTCRC)) @@ -178,22 +232,28 @@ inline u_long crcCalc(u_char * __restrict psBuf, u_int /* * crcIP() Checksum in IP communication - * @nBuf = Data for calculation + * @buf = Data for calculation * @bufLen = Length of data * return: -1 error, !=-1 Checksum */ -inline u_short crcIP(u_short * __restrict nBuf, int bufLen) +inline u_short +crcIP(u_char * __restrict buf, int bufLen) { - register u_long sum; + register u_int sum; + u_short last = 0, *nBuf = (u_short*) buf; - if (!nBuf) { + if (!buf) { crc_Errno = 1; strlcpy(crc_Error, "crcIP(): Invalid parameters!", MAX_STR + 1); return -1; } - 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; @@ -203,17 +263,54 @@ inline u_short crcIP(u_short * __restrict nBuf, int bu } /* - * crcFletcher() Fletcher-16 Checksum computing + * crcFletcher16() 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) +inline u_short +crcFletcher16(u_short * __restrict nBuf, int bufLen) { - register u_long s1, s2, clen; + 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; + } + + s1 = s2 = 0xFF; + while (bufLen) { + clen = bufLen > MAX_FLETCHER16_DIGEST ? MAX_FLETCHER16_DIGEST : bufLen; + bufLen -= clen; + + do { + s1 += (u_short) *nBuf++; + s2 += s1; + } while (--clen); + + s1 = (s1 >> 8) + (s1 & 0xFF); + s2 = (s2 >> 8) + (s2 & 0xFF); + } + + crc_Errno ^= crc_Errno; + return (s2 << 8) | s1; +} + +/* + * crcFletcher() Fletcher-32 Checksum computing + * @nBuf = Data for calculation + * @bufLen = Length of data + * return: -1 error, !=-1 Checksum + */ +inline u_int +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; } @@ -224,7 +321,7 @@ inline u_long crcFletcher(u_short * __restrict nBuf, i bufLen -= clen; do { - s1 += (u_long) *nBuf++; + s1 += (u_int) *nBuf++; s2 += s1; } while (--clen); @@ -242,9 +339,10 @@ inline u_long crcFletcher(u_short * __restrict nBuf, i * @bufLen = Length of data * return: -1 error, !=-1 Checksum */ -inline u_long crcAdler(u_char * __restrict psBuf, int bufLen) +inline u_int +crcAdler(u_char * __restrict psBuf, int bufLen) { - register u_long s1, s2, clen; + register u_int s1, s2, clen; if (!psBuf) { crc_Errno = 1; @@ -259,7 +357,7 @@ inline u_long crcAdler(u_char * __restrict psBuf, int bufLen -= clen; do { - s1 += (u_long) *psBuf++; + s1 += (u_int) *psBuf++; s2 += s1; } while (--clen);