Annotation of libelwix/inc/elwix/acrc.h, revision 1.10

1.1       misho       1: /*************************************************************************
                      2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
                      3: *  by Michael Pounov <misho@elwix.org>
                      4: *
                      5: * $Author: misho $
1.10    ! misho       6: * $Id: acrc.h,v 1.9.2.1 2024/10/28 09:55:57 misho Exp $
1.1       misho       7: *
                      8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
1.8       misho      15: Copyright 2004 - 2024
1.1       misho      16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
                     46: #ifndef __ACRC_H
                     47: #define __ACRC_H
                     48: 
                     49: 
                     50: #define REVOPTS_REVERTBYTE     1
                     51: #define REVOPTS_REVERTCRC      2
                     52: 
                     53: 
                     54: struct tagCRCPoly {
                     55:        u_char  poly_bits;
                     56:        u_int   poly_num;
                     57:        char    poly_name[19];
                     58: };     /* size 24bytes */
                     59: typedef struct tagCRCPoly crcPoly_t;
                     60: 
                     61: 
                     62: /*
                     63:  * crcReflect() - Reflect all bits of number 
                     64:  *
                     65:  * @crcNum = Number for reflection
                     66:  * @crcBits = Number width bits 
1.7       misho      67:  * return: reflecting number
1.1       misho      68:  */
1.2       misho      69: unsigned int crcReflect(unsigned int crcNum, unsigned char crcBits);
1.1       misho      70: /*
                     71:  * crcCalc() - Generic CRC calculation function for many sub variants of CRC algorithms
                     72:  *
                     73:  * @psBuf = Data for calculation
                     74:  * @bufLen = Length of data
                     75:  * @crcBits = CRC algorithm bits (1, 4, 5, 6, 7, 8, 10, 11, 12, 15, 16, 24, 30, 32)
                     76:  * @RevOpts = Options for computation (REVOPTS_REVERTBYTE, REVOPTS_REVERTCRC)
                     77:  * @initCRC = Initial CRC value
                     78:  * @xorCRC = Last xor CRC value
1.7       misho      79:  * return: CRC checksum
1.1       misho      80:  */
1.2       misho      81: unsigned int crcCalc(unsigned char * __restrict psBuf, unsigned int bufLen, 
1.1       misho      82:                unsigned char crcBits, unsigned char RevOpts, 
                     83:                unsigned int initCRC, unsigned int xorCRC);
1.8       misho      84: #define crc_32(x, l)           crcCalc((x), (l), 32, REVOPTS_REVERTBYTE|REVOPTS_REVERTCRC, 0xFFFFFFFF, 0xFFFFFFFF)
                     85: #define crc_16(x, l)           crcCalc((x), (l), 16, REVOPTS_REVERTBYTE|REVOPTS_REVERTCRC, 0x0, 0x0)
                     86: #define crc_16_ccitt(x, l)     crcCalc((x), (l), 161, 0, 0x0, 0x0)
                     87: #define crc_16_xmodem(x, l)    crcCalc((x), (l), 162, REVOPTS_REVERTBYTE|REVOPTS_REVERTCRC, 0x0, 0x0)
1.9       misho      88: #define crc_8(x, l)            crcCalc((x), (l), 8, 0, 0x0, 0x0)
1.1       misho      89: 
                     90: /*
1.8       misho      91:  * crc16_ccitt() - Checksum calculation
1.7       misho      92:  *
                     93:  * @buf = Data for calculation
                     94:  * @bufLen = Length of data
                     95:  * return: Checksum
                     96:  */
1.8       misho      97: unsigned short crc16_ccitt(unsigned char * __restrict buf, int bufLen);
1.7       misho      98: /*
1.8       misho      99:  * crc16_xy() - Checksum calculation in X/Y modem communication
1.7       misho     100:  *
                    101:  * @buf = Data for calculation
                    102:  * @bufLen = Length of data
                    103:  * return: Checksum
                    104:  */
1.8       misho     105: unsigned short crc16_xy(unsigned char * __restrict buf, int bufLen);
1.7       misho     106: /*
1.1       misho     107:  * crcIP() - Checksum in IP communication
                    108:  *
                    109:  * @buf = Data for calculation
1.10    ! misho     110:  * @len = Length of data in bytes
1.7       misho     111:  * return: Checksum
1.1       misho     112:  */
1.10    ! misho     113: unsigned short crcIP(unsigned short* __restrict buf, int len);
1.6       misho     114: /*
                    115:  * crcTCP() - Checksum for TCP v4 communication
                    116:  *
                    117:  * @buf = Data for calculation
                    118:  * @bufLen = Length of data
                    119:  * @th = TCP header
1.7       misho     120:  * return: Checksum
1.6       misho     121:  */
                    122: unsigned short crcTCP(struct in_addr src, struct in_addr dst, 
                    123:                unsigned char * __restrict th);
                    124: /*
                    125:  * crcUDP() - Checksum for UDP v4 communication
                    126:  *
                    127:  * @buf = Data for calculation
                    128:  * @bufLen = Length of data
                    129:  * @uh = UDP header
1.7       misho     130:  * return: Checksum
1.6       misho     131:  */
                    132: unsigned short crcUDP(struct in_addr src, struct in_addr dst, 
                    133:                unsigned char * __restrict uh);
                    134: 
1.1       misho     135: /*
                    136:  * crcFletcher16() - Fletcher-16 Checksum computing
                    137:  *
                    138:  * @nBuf = Data for calculation
                    139:  * @bufLen = Length of data
1.7       misho     140:  * return: Checksum
1.1       misho     141:  */
1.2       misho     142: unsigned short crcFletcher16(unsigned short * __restrict nBuf, int bufLen);
1.1       misho     143: /*
                    144:  * crcFletcher() - Fletcher-32 Checksum computing
                    145:  *
                    146:  * @nBuf = Data for calculation
                    147:  * @bufLen = Length of data
1.7       misho     148:  * return: Checksum
1.1       misho     149:  */
1.2       misho     150: unsigned int crcFletcher(unsigned short * __restrict nBuf, int bufLen);
1.1       misho     151: /*
                    152:  * crcAdler() - crcAdler-32 Checksum computing
                    153:  *
                    154:  * @psBuf = Data for calculation
                    155:  * @bufLen = Length of data
1.7       misho     156:  * return: Checksum
1.1       misho     157:  */
1.2       misho     158: unsigned int crcAdler(unsigned char * __restrict psBuf, int bufLen);
1.1       misho     159: 
                    160: /*
                    161:  * crcEther() - Checksum in Ethernet communication
                    162:  *
                    163:  * @psBuf = Data for calculation
                    164:  * @bufLen = Length of data
1.7       misho     165:  * return: Checksum
1.1       misho     166:  */
                    167: #define crcEther(psBuf, bufLen) crcCalc((psBuf), (bufLen), 32, 3, 0xFFFFFFFF, 0xFFFFFFFF)
                    168: 
1.3       misho     169: /*
1.9       misho     170:  * crc8tbl() - CRC8 calculation from table
                    171:  *
                    172:  * @crc = Initial crc value
                    173:  * @buf = Data for calculation
                    174:  * @len = Length of data
                    175:  * return: calculated CRC8
                    176:  */
                    177: uint8_t crc8tbl(unsigned char crc, const unsigned char * __restrict buf, unsigned int len);
                    178: /*
1.8       misho     179:  * crc32tbl() - CRC32 calculation from table
1.3       misho     180:  *
                    181:  * @crc = Initial crc value
                    182:  * @buf = Data for calculation
                    183:  * @len = Length of data
                    184:  * return: calculated CRC32
                    185:  */
1.8       misho     186: unsigned int crc32tbl(unsigned int crc, const unsigned char * __restrict buf, unsigned int len);
1.1       misho     187: 
                    188: /*
                    189:  * crcPelco() - Calculate Pelco D/P CRC
                    190:  *
                    191:  * @ver = Pelco protocol version (Dd | Pp)
                    192:  * @pkt = Packet for calculate crc
                    193:  * return: crc for packet, if is 0 check and crc_GetErrno() == 1 
                    194:        Pelco protocol not supported
                    195:  */
1.2       misho     196: unsigned char crcPelco(unsigned char ver, unsigned char *pkt);
1.1       misho     197: 
                    198: 
                    199: /*
                    200:  * hash_varchar() - Compute index hash by variable length string
                    201:  *
                    202:  * @csStr = Input data buffer
                    203:  * @nStrLen = Length of data buffer
                    204:  * @nVer = Version of algorythm; 0 - original, 1 - AITNET variant
                    205:  * return: Hash value
                    206: */
1.2       misho     207: unsigned int hash_varchar(const char *csStr, int nStrLen, int nVer);
1.1       misho     208: /*
                    209:  * hash_bernstein() - Compute index hash by Bernstein
                    210:  *
                    211:  * @csStr = Input data buffer
                    212:  * @nStrLen = Length of data buffer
                    213:  * @nVer = Version of algorythm; 0 - Bernstein, 1 - DJBX33A variant
                    214:  * return: Hash value
                    215: */
1.2       misho     216: unsigned int hash_bernstein(const char *csStr, int nStrLen, int nVer);
1.1       misho     217: /*
                    218:  * hash_jenkins() - Compute index hash by Jenkins (one-at-a-time)
                    219:  *
                    220:  * @csStr = Input data buffer
                    221:  * @nStrLen = Length of data buffer
                    222:  * return: Hash value
                    223: */
1.2       misho     224: unsigned int hash_jenkins(const char *csStr, int nStrLen);
1.1       misho     225: /*
                    226:  * hash_jenkins32() - Fast Jenkins hash function
                    227:  *
                    228:  * @buf = Input buffer
                    229:  * @len = Length of buffer
                    230:  * @prevhash = Previous hash, if participate in continuing hash
                    231:  * return: Hash value
                    232:  */
                    233: unsigned int hash_jenkins32(const unsigned int *buf, int len, unsigned int prevhash);
                    234: /*
                    235:  * hash_reddragon() - Compute index hash by Red Dragon
                    236:  *
                    237:  * @csStr = Input data buffer
                    238:  * @nStrLen = Length of data buffer
                    239:  * return: Hash value
                    240: */
1.2       misho     241: unsigned int hash_reddragon(const char *csStr, int nStrLen);
1.1       misho     242: /*
                    243:  * hash_fnv1() - Compute index hash by FNV-1
                    244:  *
                    245:  * @csStr = Input data buffer
                    246:  * @nStrLen = Length of data buffer
                    247:  * @nVer = Version of algorythm; 0 - FNV-1, 1 - FNV-1a (best avalanche)
                    248:  * return: Hash value
                    249: */
1.2       misho     250: unsigned int hash_fnv1(const char *csStr, int nStrLen, int nVer);
1.1       misho     251: 
                    252: /*
                    253:  * hash_fnv() - Compute index hash by FNV-1a
                    254:  *
                    255:  * @csStr = Input data buffer
                    256:  * @nStrLen = Length of data buffer
                    257:  * return: Hash value
                    258: */
                    259: #define hash_fnv(str, len)     hash_fnv1((str), (len), 1)
                    260: 
                    261: 
                    262: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>