--- libelwix/src/crc.c 2019/12/30 18:11:16 1.6 +++ libelwix/src/crc.c 2024/10/10 23:55:48 1.7 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: crc.c,v 1.6 2019/12/30 18:11:16 misho Exp $ +* $Id: crc.c,v 1.7 2024/10/10 23:55:48 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 - 2019 +Copyright 2004 - 2024 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -67,7 +67,9 @@ const crcPoly_t crc_Poly[] = { { 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" } + { 32, (u_int) 0x04C11DB7, "CRC-32-802.3" }, + { 16, (u_int) 0x1021, "CRC-16-CCITT" }, + { 16, (u_int) 0x8408, "CRC-16-XMODEM" } }; @@ -103,7 +105,7 @@ crcReflect(u_int crcNum, u_char crcBits) u_int crcCalc(u_char * __restrict psBuf, u_int bufLen, u_char crcBits, u_char RevOpts, u_int initCRC, u_int xorCRC) { - const u_int bits = sizeof(int) * 8 - crcBits; + u_int bits = sizeof(int) * 8; u_int poly, crchibit, crc; register u_int i, j, b, ch; @@ -112,46 +114,70 @@ crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha switch (crcBits) { case 1: poly = crc_Poly[0].poly_num; + bits -= crc_Poly[0].poly_bits; break; case 4: poly = crc_Poly[1].poly_num; + bits -= crc_Poly[1].poly_bits; break; case 5: poly = crc_Poly[2].poly_num; + bits -= crc_Poly[2].poly_bits; break; case 6: poly = crc_Poly[3].poly_num; + bits -= crc_Poly[3].poly_bits; break; case 7: poly = crc_Poly[4].poly_num; + bits -= crc_Poly[4].poly_bits; break; case 8: poly = crc_Poly[5].poly_num; + bits -= crc_Poly[5].poly_bits; break; case 10: poly = crc_Poly[6].poly_num; + bits -= crc_Poly[6].poly_bits; break; case 11: poly = crc_Poly[7].poly_num; + bits -= crc_Poly[7].poly_bits; break; case 12: poly = crc_Poly[8].poly_num; + bits -= crc_Poly[8].poly_bits; break; case 15: poly = crc_Poly[9].poly_num; + bits -= crc_Poly[9].poly_bits; break; case 16: poly = crc_Poly[10].poly_num; + bits -= crc_Poly[10].poly_bits; break; case 24: poly = crc_Poly[11].poly_num; + bits -= crc_Poly[11].poly_bits; break; case 30: poly = crc_Poly[12].poly_num; + bits -= crc_Poly[12].poly_bits; break; case 32: poly = crc_Poly[13].poly_num; + bits -= crc_Poly[13].poly_bits; break; + case 161: + poly = crc_Poly[14].poly_num; + bits -= crc_Poly[14].poly_bits; + crcBits = crc_Poly[14].poly_bits; + break; + case 162: + poly = crc_Poly[15].poly_num; + bits -= crc_Poly[15].poly_bits; + crcBits = crc_Poly[15].poly_bits; + break; default: elwix_SetErr(EINVAL, "crcCalc(): Unsupported CRC method!!!"); return -1; @@ -190,14 +216,14 @@ crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha /* - * crc16_ext() - Checksum ver.2 calculation in X/Y modem communication + * crc16_xy() - Checksum calculation in X/Y modem communication * * @buf = Data for calculation * @bufLen = Length of data * return: Checksum */ u_short -crc16_ext(u_char * __restrict buf, int bufLen) +crc16_xy(u_char * __restrict buf, int bufLen) { u_short crc, x; register u_short i; @@ -227,14 +253,14 @@ crc16_ext(u_char * __restrict buf, int bufLen) } /* - * crc16() - Checksum calculation in X/Y modem communication + * crc16_ccitt() - Checksum calculation * * @buf = Data for calculation * @bufLen = Length of data * return: Checksum */ u_short -crc16(u_char * __restrict buf, int bufLen) +crc16_ccitt(u_char * __restrict buf, int bufLen) { u_short crc; register u_char i;