File:  [ELWIX - Embedded LightWeight unIX -] / libaitcrc / inc / aitcrc.h
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Thu Apr 28 20:28:20 2011 UTC (13 years, 2 months ago) by misho
Branches: MAIN
CVS tags: crc2_1, HEAD, CRC2_0
ver 2.0

    1: /*************************************************************************
    2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aitcrc.h,v 1.4 2011/04/28 20:28:20 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011
   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 __AITCRC_H
   47: #define __AITCRC_H
   48: 
   49: 
   50: #include <sys/types.h>
   51: 
   52: 
   53: #define REVOPTS_REVERTBYTE	1
   54: #define REVOPTS_REVERTCRC	2
   55: 
   56: 
   57: struct tagCRCPoly {
   58: 	u_char	poly_bits;
   59: 	u_int	poly_num;
   60: 	char	poly_name[19];
   61: };	// size 24bytes
   62: typedef struct tagCRCPoly crcPoly_t;
   63: 
   64: 
   65: // -------------------------------------------------------
   66: // crc_GetErrno() Get error code of last operation
   67: inline int crc_GetErrno();
   68: // crc_GetError() Get error text of last operation
   69: inline const char *crc_GetError();
   70: // -------------------------------------------------------
   71: 
   72: 
   73: /*
   74:  * crcReflect() Reflect all bits of number 
   75:  * @crcNum = Number for reflection
   76:  * @crcBits = Number width bits 
   77:  * return: -1 error, !=-1 reflecting number
   78:  */
   79: inline u_int crcReflect(u_int crcNum, u_char crcBits);
   80: /*
   81:  * crcCalc() Generic CRC calculation function for many sub variants of CRC algorithms
   82:  * @psBuf = Data for calculation
   83:  * @bufLen = Length of data
   84:  * @crcBits = CRC algorithm bits (1, 4, 5, 6, 7, 8, 10, 11, 12, 15, 16, 24, 30, 32)
   85:  * @RevOpts = Options for computation (REVOPTS_REVERTBYTE, REVOPTS_REVERTCRC)
   86:  * @initCRC = Initial CRC value
   87:  * @xorCRC = Last xor CRC value
   88:  * return: -1 error, !=-1 CRC checksum
   89:  */
   90: inline u_int crcCalc(u_char * __restrict psBuf, u_int bufLen, u_char crcBits, 
   91: 		u_char RevOpts, u_int initCRC, u_int xorCRC);
   92: 
   93: /*
   94:  * crcIP() Checksum in IP communication
   95:  * @nBuf = Data for calculation
   96:  * @bufLen = Length of data
   97:  * return: -1 error, !=-1 Checksum
   98:  */
   99: inline u_short crcIP(u_short * __restrict nBuf, int bufLen);
  100: /*
  101:  * crcFletcher16() Fletcher-16 Checksum computing
  102:  * @nBuf = Data for calculation
  103:  * @bufLen = Length of data
  104:  * return: -1 error, !=-1 Checksum
  105:  */
  106: inline u_short crcFletcher16(u_short * __restrict nBuf, int bufLen);
  107: /*
  108:  * crcFletcher() Fletcher-32 Checksum computing
  109:  * @nBuf = Data for calculation
  110:  * @bufLen = Length of data
  111:  * return: -1 error, !=-1 Checksum
  112:  */
  113: inline u_int crcFletcher(u_short * __restrict nBuf, int bufLen);
  114: /*
  115:  * crcAdler() crcAdler-32 Checksum computing
  116:  * @psBuf = Data for calculation
  117:  * @bufLen = Length of data
  118:  * return: -1 error, !=-1 Checksum
  119:  */
  120: inline u_int crcAdler(u_char * __restrict psBuf, int bufLen);
  121: 
  122: /*
  123:  * crcEther() Checksum in Ethernet communication
  124:  * @psBuf = Data for calculation
  125:  * @bufLen = Length of data
  126:  * return: -1 error, !=-1 Checksum
  127:  */
  128: #define crcEther(psBuf, bufLen) crcCalc((psBuf), (bufLen), 32, 3, 0xFFFFFFFF, 0xFFFFFFFF)
  129: 
  130: 
  131: /*
  132:  * crcPelco() Calculate Pelco D/P CRC
  133:  * @ver = Pelco protocol version (Dd | Pp)
  134:  * @pkt = Packet for calculate crc
  135:  * return: crc for packet, if is 0 check and crc_GetErrno() == 1 
  136:  	Pelco protocol not supported
  137:  */
  138: inline u_char crcPelco(u_char ver, u_char *pkt);
  139: 
  140: 
  141: /*
  142:  * hash_varchar() Compute index hash by variable length string
  143:  * @csStr = Input data buffer
  144:  * @nStrLen = Length of data buffer
  145:  * @nVer = Version of algorythm; 0 - original, 1 - AITNET variant
  146:  * return: Hash value
  147: */
  148: inline u_int hash_varchar(const char *csStr, int nStrLen, int nVer);
  149: /*
  150:  * hash_bernstein() Compute index hash by Bernstein
  151:  * @csStr = Input data buffer
  152:  * @nStrLen = Length of data buffer
  153:  * @nVer = Version of algorythm; 0 - Bernstein, 1 - DJBX33A variant
  154:  * return: Hash value
  155: */
  156: inline u_int hash_bernstein(const char *csStr, int nStrLen, int nVer);
  157: /*
  158:  * hash_jenkins() Compute index hash by Jenkins (one-at-a-time)
  159:  * @csStr = Input data buffer
  160:  * @nStrLen = Length of data buffer
  161:  * return: Hash value
  162: */
  163: inline u_int hash_jenkins(const char *csStr, int nStrLen);
  164: /*
  165:  * hash_reddragon() Compute index hash by Red Dragon
  166:  * @csStr = Input data buffer
  167:  * @nStrLen = Length of data buffer
  168:  * return: Hash value
  169: */
  170: inline u_int hash_reddragon(const char *csStr, int nStrLen);
  171: /*
  172:  * hash_fnv1() Compute index hash by FNV-1
  173:  * @csStr = Input data buffer
  174:  * @nStrLen = Length of data buffer
  175:  * @nVer = Version of algorythm; 0 - FNV-1, 1 - FNV-1a (best avalanche)
  176:  * return: Hash value
  177: */
  178: inline u_int hash_fnv1(const char *csStr, int nStrLen, int nVer);
  179: 
  180: /*
  181:  * hash_fnv() Compute index hash by FNV-1a
  182:  * @csStr = Input data buffer
  183:  * @nStrLen = Length of data buffer
  184:  * return: Hash value
  185: */
  186: #define hash_fnv(str, len)	hash_fnv1((str), (len), 1)
  187: 
  188: 
  189: #endif

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