--- libelwix/inc/elwix/acrc.h 2013/01/17 10:05:35 1.1 +++ libelwix/inc/elwix/acrc.h 2024/10/10 23:55:48 1.8 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: acrc.h,v 1.1 2013/01/17 10:05:35 misho Exp $ +* $Id: acrc.h,v 1.8 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 +Copyright 2004 - 2024 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -64,9 +64,9 @@ typedef struct tagCRCPoly crcPoly_t; * * @crcNum = Number for reflection * @crcBits = Number width bits - * return: -1 error, !=-1 reflecting number + * return: reflecting number */ -inline unsigned int crcReflect(unsigned int crcNum, unsigned char crcBits); +unsigned int crcReflect(unsigned int crcNum, unsigned char crcBits); /* * crcCalc() - Generic CRC calculation function for many sub variants of CRC algorithms * @@ -76,54 +76,104 @@ inline unsigned int crcReflect(unsigned int crcNum, un * @RevOpts = Options for computation (REVOPTS_REVERTBYTE, REVOPTS_REVERTCRC) * @initCRC = Initial CRC value * @xorCRC = Last xor CRC value - * return: -1 error, !=-1 CRC checksum + * return: CRC checksum */ -inline unsigned int crcCalc(unsigned char * __restrict psBuf, unsigned int bufLen, +unsigned int crcCalc(unsigned char * __restrict psBuf, unsigned int bufLen, unsigned char crcBits, unsigned char RevOpts, unsigned int initCRC, unsigned int xorCRC); +#define crc_32(x, l) crcCalc((x), (l), 32, REVOPTS_REVERTBYTE|REVOPTS_REVERTCRC, 0xFFFFFFFF, 0xFFFFFFFF) +#define crc_16(x, l) crcCalc((x), (l), 16, REVOPTS_REVERTBYTE|REVOPTS_REVERTCRC, 0x0, 0x0) +#define crc_16_ccitt(x, l) crcCalc((x), (l), 161, 0, 0x0, 0x0) +#define crc_16_xmodem(x, l) crcCalc((x), (l), 162, REVOPTS_REVERTBYTE|REVOPTS_REVERTCRC, 0x0, 0x0) /* + * crc16_ccitt() - Checksum calculation + * + * @buf = Data for calculation + * @bufLen = Length of data + * return: Checksum + */ +unsigned short crc16_ccitt(unsigned char * __restrict buf, int bufLen); +/* + * crc16_xy() - Checksum calculation in X/Y modem communication + * + * @buf = Data for calculation + * @bufLen = Length of data + * return: Checksum + */ +unsigned short crc16_xy(unsigned char * __restrict buf, int bufLen); +/* * crcIP() - Checksum in IP communication * * @buf = Data for calculation * @bufLen = Length of data - * return: -1 error, !=-1 Checksum + * return: Checksum */ -inline unsigned short crcIP(unsigned char * __restrict buf, int bufLen); +unsigned short crcIP(unsigned char * __restrict buf, int bufLen); /* + * crcTCP() - Checksum for TCP v4 communication + * + * @buf = Data for calculation + * @bufLen = Length of data + * @th = TCP header + * return: Checksum + */ +unsigned short crcTCP(struct in_addr src, struct in_addr dst, + unsigned char * __restrict th); +/* + * crcUDP() - Checksum for UDP v4 communication + * + * @buf = Data for calculation + * @bufLen = Length of data + * @uh = UDP header + * return: Checksum + */ +unsigned short crcUDP(struct in_addr src, struct in_addr dst, + unsigned char * __restrict uh); + +/* * crcFletcher16() - Fletcher-16 Checksum computing * * @nBuf = Data for calculation * @bufLen = Length of data - * return: -1 error, !=-1 Checksum + * return: Checksum */ -inline unsigned short crcFletcher16(unsigned short * __restrict nBuf, int bufLen); +unsigned short crcFletcher16(unsigned short * __restrict nBuf, int bufLen); /* * crcFletcher() - Fletcher-32 Checksum computing * * @nBuf = Data for calculation * @bufLen = Length of data - * return: -1 error, !=-1 Checksum + * return: Checksum */ -inline unsigned int crcFletcher(unsigned short * __restrict nBuf, int bufLen); +unsigned int crcFletcher(unsigned short * __restrict nBuf, int bufLen); /* * crcAdler() - crcAdler-32 Checksum computing * * @psBuf = Data for calculation * @bufLen = Length of data - * return: -1 error, !=-1 Checksum + * return: Checksum */ -inline unsigned int crcAdler(unsigned char * __restrict psBuf, int bufLen); +unsigned int crcAdler(unsigned char * __restrict psBuf, int bufLen); /* * crcEther() - Checksum in Ethernet communication * * @psBuf = Data for calculation * @bufLen = Length of data - * return: -1 error, !=-1 Checksum + * return: Checksum */ #define crcEther(psBuf, bufLen) crcCalc((psBuf), (bufLen), 32, 3, 0xFFFFFFFF, 0xFFFFFFFF) +/* + * crc32tbl() - CRC32 calculation from table + * + * @crc = Initial crc value + * @buf = Data for calculation + * @len = Length of data + * return: calculated CRC32 + */ +unsigned int crc32tbl(unsigned int crc, const unsigned char * __restrict buf, unsigned int len); /* * crcPelco() - Calculate Pelco D/P CRC @@ -133,7 +183,7 @@ inline unsigned int crcAdler(unsigned char * __restric * return: crc for packet, if is 0 check and crc_GetErrno() == 1 Pelco protocol not supported */ -inline unsigned char crcPelco(unsigned char ver, unsigned char *pkt); +unsigned char crcPelco(unsigned char ver, unsigned char *pkt); /* @@ -144,7 +194,7 @@ inline unsigned char crcPelco(unsigned char ver, unsig * @nVer = Version of algorythm; 0 - original, 1 - AITNET variant * return: Hash value */ -inline unsigned int hash_varchar(const char *csStr, int nStrLen, int nVer); +unsigned int hash_varchar(const char *csStr, int nStrLen, int nVer); /* * hash_bernstein() - Compute index hash by Bernstein * @@ -153,7 +203,7 @@ inline unsigned int hash_varchar(const char *csStr, in * @nVer = Version of algorythm; 0 - Bernstein, 1 - DJBX33A variant * return: Hash value */ -inline unsigned int hash_bernstein(const char *csStr, int nStrLen, int nVer); +unsigned int hash_bernstein(const char *csStr, int nStrLen, int nVer); /* * hash_jenkins() - Compute index hash by Jenkins (one-at-a-time) * @@ -161,7 +211,7 @@ inline unsigned int hash_bernstein(const char *csStr, * @nStrLen = Length of data buffer * return: Hash value */ -inline unsigned int hash_jenkins(const char *csStr, int nStrLen); +unsigned int hash_jenkins(const char *csStr, int nStrLen); /* * hash_jenkins32() - Fast Jenkins hash function * @@ -178,7 +228,7 @@ unsigned int hash_jenkins32(const unsigned int *buf, i * @nStrLen = Length of data buffer * return: Hash value */ -inline unsigned int hash_reddragon(const char *csStr, int nStrLen); +unsigned int hash_reddragon(const char *csStr, int nStrLen); /* * hash_fnv1() - Compute index hash by FNV-1 * @@ -187,7 +237,7 @@ inline unsigned int hash_reddragon(const char *csStr, * @nVer = Version of algorythm; 0 - FNV-1, 1 - FNV-1a (best avalanche) * return: Hash value */ -inline unsigned int hash_fnv1(const char *csStr, int nStrLen, int nVer); +unsigned int hash_fnv1(const char *csStr, int nStrLen, int nVer); /* * hash_fnv() - Compute index hash by FNV-1a