File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix / acrc.h
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Fri Dec 8 00:07:48 2017 UTC (6 years, 5 months ago) by misho
Branches: MAIN
CVS tags: elwix4_9, elwix4_8, elwix4_22, elwix4_21, elwix4_20, elwix4_19, elwix4_18, elwix4_17, elwix4_16, elwix4_15, elwix4_14, elwix4_13, elwix4_12, elwix4_11, elwix4_10, HEAD, ELWIX4_9, ELWIX4_8, ELWIX4_7, ELWIX4_21, ELWIX4_20, ELWIX4_19, ELWIX4_18, ELWIX4_17, ELWIX4_16, ELWIX4_15, ELWIX4_14, ELWIX4_13, ELWIX4_12, ELWIX4_11, ELWIX4_10
version 4.7

    1: /*************************************************************************
    2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: acrc.h,v 1.6 2017/12/08 00:07:48 misho Exp $
    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: 
   15: Copyright 2004 - 2017
   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 
   67:  * return: -1 error, !=-1 reflecting number
   68:  */
   69: unsigned int crcReflect(unsigned int crcNum, unsigned char crcBits);
   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
   79:  * return: -1 error, !=-1 CRC checksum
   80:  */
   81: unsigned int crcCalc(unsigned char * __restrict psBuf, unsigned int bufLen, 
   82: 		unsigned char crcBits, unsigned char RevOpts, 
   83: 		unsigned int initCRC, unsigned int xorCRC);
   84: 
   85: /*
   86:  * crcIP() - Checksum in IP communication
   87:  *
   88:  * @buf = Data for calculation
   89:  * @bufLen = Length of data
   90:  * return: -1 error, !=-1 Checksum
   91:  */
   92: unsigned short crcIP(unsigned char * __restrict buf, int bufLen);
   93: /*
   94:  * crcTCP() - Checksum for TCP v4 communication
   95:  *
   96:  * @buf = Data for calculation
   97:  * @bufLen = Length of data
   98:  * @th = TCP header
   99:  * return: -1 error, !=-1 Checksum
  100:  */
  101: unsigned short crcTCP(struct in_addr src, struct in_addr dst, 
  102: 		unsigned char * __restrict th);
  103: /*
  104:  * crcUDP() - Checksum for UDP v4 communication
  105:  *
  106:  * @buf = Data for calculation
  107:  * @bufLen = Length of data
  108:  * @uh = UDP header
  109:  * return: -1 error, !=-1 Checksum
  110:  */
  111: unsigned short crcUDP(struct in_addr src, struct in_addr dst, 
  112: 		unsigned char * __restrict uh);
  113: 
  114: /*
  115:  * crcFletcher16() - Fletcher-16 Checksum computing
  116:  *
  117:  * @nBuf = Data for calculation
  118:  * @bufLen = Length of data
  119:  * return: -1 error, !=-1 Checksum
  120:  */
  121: unsigned short crcFletcher16(unsigned short * __restrict nBuf, int bufLen);
  122: /*
  123:  * crcFletcher() - Fletcher-32 Checksum computing
  124:  *
  125:  * @nBuf = Data for calculation
  126:  * @bufLen = Length of data
  127:  * return: -1 error, !=-1 Checksum
  128:  */
  129: unsigned int crcFletcher(unsigned short * __restrict nBuf, int bufLen);
  130: /*
  131:  * crcAdler() - crcAdler-32 Checksum computing
  132:  *
  133:  * @psBuf = Data for calculation
  134:  * @bufLen = Length of data
  135:  * return: -1 error, !=-1 Checksum
  136:  */
  137: unsigned int crcAdler(unsigned char * __restrict psBuf, int bufLen);
  138: 
  139: /*
  140:  * crcEther() - Checksum in Ethernet communication
  141:  *
  142:  * @psBuf = Data for calculation
  143:  * @bufLen = Length of data
  144:  * return: -1 error, !=-1 Checksum
  145:  */
  146: #define crcEther(psBuf, bufLen) crcCalc((psBuf), (bufLen), 32, 3, 0xFFFFFFFF, 0xFFFFFFFF)
  147: 
  148: /*
  149:  * crc_32() - CRC32 calculation from table
  150:  *
  151:  * @crc = Initial crc value
  152:  * @buf = Data for calculation
  153:  * @len = Length of data
  154:  * return: calculated CRC32
  155:  */
  156: unsigned int crc_32(unsigned int crc, const unsigned char * __restrict buf, unsigned int len);
  157: 
  158: /*
  159:  * crcPelco() - Calculate Pelco D/P CRC
  160:  *
  161:  * @ver = Pelco protocol version (Dd | Pp)
  162:  * @pkt = Packet for calculate crc
  163:  * return: crc for packet, if is 0 check and crc_GetErrno() == 1 
  164:  	Pelco protocol not supported
  165:  */
  166: unsigned char crcPelco(unsigned char ver, unsigned char *pkt);
  167: 
  168: 
  169: /*
  170:  * hash_varchar() - Compute index hash by variable length string
  171:  *
  172:  * @csStr = Input data buffer
  173:  * @nStrLen = Length of data buffer
  174:  * @nVer = Version of algorythm; 0 - original, 1 - AITNET variant
  175:  * return: Hash value
  176: */
  177: unsigned int hash_varchar(const char *csStr, int nStrLen, int nVer);
  178: /*
  179:  * hash_bernstein() - Compute index hash by Bernstein
  180:  *
  181:  * @csStr = Input data buffer
  182:  * @nStrLen = Length of data buffer
  183:  * @nVer = Version of algorythm; 0 - Bernstein, 1 - DJBX33A variant
  184:  * return: Hash value
  185: */
  186: unsigned int hash_bernstein(const char *csStr, int nStrLen, int nVer);
  187: /*
  188:  * hash_jenkins() - Compute index hash by Jenkins (one-at-a-time)
  189:  *
  190:  * @csStr = Input data buffer
  191:  * @nStrLen = Length of data buffer
  192:  * return: Hash value
  193: */
  194: unsigned int hash_jenkins(const char *csStr, int nStrLen);
  195: /*
  196:  * hash_jenkins32() - Fast Jenkins hash function
  197:  *
  198:  * @buf = Input buffer
  199:  * @len = Length of buffer
  200:  * @prevhash = Previous hash, if participate in continuing hash
  201:  * return: Hash value
  202:  */
  203: unsigned int hash_jenkins32(const unsigned int *buf, int len, unsigned int prevhash);
  204: /*
  205:  * hash_reddragon() - Compute index hash by Red Dragon
  206:  *
  207:  * @csStr = Input data buffer
  208:  * @nStrLen = Length of data buffer
  209:  * return: Hash value
  210: */
  211: unsigned int hash_reddragon(const char *csStr, int nStrLen);
  212: /*
  213:  * hash_fnv1() - Compute index hash by FNV-1
  214:  *
  215:  * @csStr = Input data buffer
  216:  * @nStrLen = Length of data buffer
  217:  * @nVer = Version of algorythm; 0 - FNV-1, 1 - FNV-1a (best avalanche)
  218:  * return: Hash value
  219: */
  220: unsigned int hash_fnv1(const char *csStr, int nStrLen, int nVer);
  221: 
  222: /*
  223:  * hash_fnv() - Compute index hash by FNV-1a
  224:  *
  225:  * @csStr = Input data buffer
  226:  * @nStrLen = Length of data buffer
  227:  * return: Hash value
  228: */
  229: #define hash_fnv(str, len)	hash_fnv1((str), (len), 1)
  230: 
  231: 
  232: #endif

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