--- libelwix/src/crc.c 2024/10/10 23:53:00 1.6.36.1 +++ libelwix/src/crc.c 2024/10/28 09:55:57 1.8.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: crc.c,v 1.6.36.1 2024/10/10 23:53:00 misho Exp $ +* $Id: crc.c,v 1.8.2.1 2024/10/28 09:55:57 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -59,7 +59,7 @@ const crcPoly_t crc_Poly[] = { { 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" }, + { 8, (u_int) 0x7, "CRC-8-CCITT" }, { 10, (u_int) 0x233, "CRC-10" }, { 11, (u_int) 0x385, "CRC-11-FlexRay" }, { 12, (u_int) 0x80F, "CRC-12-Telco" }, @@ -281,28 +281,25 @@ crc16_ccitt(u_char * __restrict buf, int bufLen) * crcIP() - Checksum in IP communication * * @buf = Data for calculation - * @bufLen = Length of data + * @len = Length of data in bytes * return: Checksum */ u_short -crcIP(u_char * __restrict buf, int bufLen) +crcIP(u_short* __restrict buf, int len) { - register u_int sum; - u_short last = 0, *nBuf = (u_short*) buf; + register u_long sum = 0; assert(buf); - for (sum = 0; bufLen > 1; bufLen -= 2) - sum += *nBuf++; - if (bufLen == 1) { - *(u_char*)(&last) += *(u_char*) nBuf; - sum += last; - } + for (sum = 0; len > 1; len -= 2) + sum += *buf++; + if (len > 0) + sum += ((*buf) & htons(0xFF00)); sum = (sum >> 16) + (sum & 0xFFFF); sum += sum >> 16; - return (u_short) ~sum; + return (u_short) (~sum); } /* @@ -332,7 +329,7 @@ crcTCP(struct in_addr src, struct in_addr dst, u_char buf.tcp_len = htons(sizeof buf.tcp); memcpy(&buf.tcp, th, sizeof buf.tcp); - return crcIP((u_char*) &buf, sizeof buf); + return crcIP((u_short*) &buf, sizeof buf); } /* @@ -362,7 +359,7 @@ crcUDP(struct in_addr src, struct in_addr dst, u_char buf.udp_len = htons(sizeof buf.udp); memcpy(&buf.udp, uh, sizeof buf.udp); - return crcIP((u_char*) &buf, sizeof buf); + return crcIP((u_short*) &buf, sizeof buf); }