--- libaitcrc/src/aitcrc.c 2010/06/13 16:13:51 1.3 +++ 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.3 2010/06/13 16:13:51 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" @@ -40,19 +77,22 @@ 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, ...) +inline void +crcSetErr(int eno, char *estr, ...) { va_list lst; @@ -192,23 +232,28 @@ crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha /* * 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) +crcIP(u_char * __restrict buf, int bufLen) { 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;