File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / src / crc.c
Revision 1.5.28.2: download - view: text, annotated - select for diffs - revision graph
Mon Dec 30 17:56:54 2019 UTC (4 years, 10 months ago) by misho
Branches: elwix4_21
Diff to: branchpoint 1.5: preferred, unified
adds new similar crc16 function

/*************************************************************************
* (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: crc.c,v 1.5.28.2 2019/12/30 17:56:54 misho Exp $
*
**************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:

All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>

Copyright 2004 - 2019
	by Michael Pounov <misho@elwix.org>.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "global.h"


/* CRC16 poly */
const u_short crc_16poly = 0x1021;

/* Adler module */
const u_int crc_modAdler = 0xFFF1L;

/* All known library CRC types ... */
const crcPoly_t crc_Poly[] = {
	{ 1, (u_int) 0x1, "CRC-1-Parity" }, 
	{ 4, (u_int) 0x3, "CRC-4-ITU" }, 
	{ 5, (u_int) 0x15, "CRC-5-ITU" }, 
	{ 6, (u_int) 0x3, "CRC-6-ITU" }, 
	{ 7, (u_int) 0x9, "CRC-7-MMC" }, 
	{ 8, (u_int) 0x8D, "CRC-8-CCITT" }, 
	{ 10, (u_int) 0x233, "CRC-10" }, 
	{ 11, (u_int) 0x385, "CRC-11-FlexRay" }, 
	{ 12, (u_int) 0x80F, "CRC-12-Telco" }, 
	{ 15, (u_int) 0x4599, "CRC-15-CAN" }, 
	{ 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" }
};


/*
 * crcReflect() - Reflect all bits of number 
 *
 * @crcNum = Number for reflection
 * @crcBits = Number width bits 
 * return: reflecting number
 */
u_int
crcReflect(u_int crcNum, u_char crcBits)
{
	register u_int i, j = 1, rev = 0;

	for (i = (u_int) 1 << (crcBits - 1); i; i >>= 1, j <<= 1)
		if (crcNum & i)
			rev |= j;
	return rev;
}

/*
 * crcCalc() - Generic CRC calculation function for many sub variants of CRC algorithms
 *
 * @psBuf = Data for calculation
 * @bufLen = Length of data
 * @crcBits = CRC algorithm bits (1, 4, 5, 6, 7, 8, 10, 11, 12, 15, 16, 24, 30, 32)
 * @RevOpts = Options for computation (REVOPTS_REVERTBYTE, REVOPTS_REVERTCRC)
 * @initCRC = Initial CRC value
 * @xorCRC = Last xor CRC value
 * return: CRC checksum
 */
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 poly, crchibit, crc;
	register u_int i, j, b, ch;

	assert(psBuf);

	switch (crcBits) {
		case 1:
			poly = crc_Poly[0].poly_num;
			break;
		case 4:
			poly = crc_Poly[1].poly_num;
			break;
		case 5:
			poly = crc_Poly[2].poly_num;
			break;
		case 6:
			poly = crc_Poly[3].poly_num;
			break;
		case 7:
			poly = crc_Poly[4].poly_num;
			break;
		case 8:
			poly = crc_Poly[5].poly_num;
			break;
		case 10:
			poly = crc_Poly[6].poly_num;
			break;
		case 11:
			poly = crc_Poly[7].poly_num;
			break;
		case 12:
			poly = crc_Poly[8].poly_num;
			break;
		case 15:
			poly = crc_Poly[9].poly_num;
			break;
		case 16:
			poly = crc_Poly[10].poly_num;
			break;
		case 24:
			poly = crc_Poly[11].poly_num;
			break;
		case 30:
			poly = crc_Poly[12].poly_num;
			break;
		case 32:
			poly = crc_Poly[13].poly_num;
			break;
		default:
			elwix_SetErr(EINVAL, "crcCalc(): Unsupported CRC method!!!");
			return -1;
	}
	poly <<= bits;

	crchibit = (u_int) 1 << (crcBits - 1);
	crchibit <<= bits;
	crc = initCRC << bits;

	for (i = 0; i < bufLen; i++) {
		ch = (u_int) *psBuf++;
		if (RevOpts & REVOPTS_REVERTBYTE)
			ch = crcReflect(ch, 8);

		for (j = 0x80; j; j >>= 1) {
			b = crc & crchibit;
			crc <<= 1;

			if (ch & j)
				b ^= crchibit;
			if (b)
				crc ^= poly;
		}
	}

	if (RevOpts & REVOPTS_REVERTCRC)
		crc = crcReflect(crc, sizeof(int) * 8);
	crc ^= xorCRC << bits;
	crc &= (((crchibit - 1) << 1) | 1);
	if (!(RevOpts & REVOPTS_REVERTCRC))
		crc >>= bits;

	return crc;
}


/*
 * crc16_ext() - Checksum ver.2 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)
{
	u_short crc, x;
	register u_short i;

	assert(buf);

	for (crc = 0; bufLen > 0; bufLen--, buf++)
		for (i = 0x80; i; i >>= 1) {
			x = crc >> 15;
			crc <<= 1;

			if (*buf & i)
				crc++;
			if (x)
				crc ^= crc_16poly;
		}

	for (i = 0; i < 16; i++) {
		x = crc >> 15;
		crc <<= 1;

		if (x)
			crc ^= crc_16poly;
	}

	return crc;
}

/*
 * crc16() - Checksum calculation in X/Y modem communication
 *
 * @buf = Data for calculation
 * @bufLen = Length of data
 * return: Checksum
 */
u_short
crc16(u_char * __restrict buf, int bufLen)
{
	u_short crc;
	register u_char i;

	for (crc = 0; bufLen > 0; bufLen--, buf++) {
		crc ^= (u_short) *buf << 8;
		for (i = 0x80; i; i >>= 1)
			if (crc & 0x8000)
				crc = crc << 1 ^ crc_16poly;
			else
				crc <<= 1;
	}

	return crc;
}

/*
 * crcIP() - Checksum in IP communication
 *
 * @buf = Data for calculation
 * @bufLen = Length of data
 * return: Checksum
 */
u_short
crcIP(u_char * __restrict buf, int bufLen)
{
	register u_int sum;
	u_short last = 0, *nBuf = (u_short*) buf;

	assert(buf);

	for (sum = 0; bufLen > 1; bufLen -= 2)
		sum += *nBuf++;
	if (bufLen == 1) {
		*(u_char*)(&last) += *(u_char*) nBuf;
		sum += last;
	}

	sum = (sum >> 16) + (sum & 0xFFFF);
	sum += sum >> 16;

	return (u_short) ~sum;
}

/*
 * crcTCP() - Checksum for TCP v4 communication
 *
 * @buf = Data for calculation
 * @bufLen = Length of data
 * @th = TCP header
 * return: Checksum
 */
u_short
crcTCP(struct in_addr src, struct in_addr dst, u_char * __restrict th)
{
	struct psd_tcp {
		struct in_addr src;
		struct in_addr dst;
		u_char pad;
		u_char proto;
		u_short tcp_len;
		u_char tcp[20];
	} buf;

	buf.src = src;
	buf.dst = dst;
	buf.pad = 0;
	buf.proto = IPPROTO_TCP;
	buf.tcp_len = htons(sizeof buf.tcp);
	memcpy(&buf.tcp, th, sizeof buf.tcp);

	return crcIP((u_char*) &buf, sizeof buf);
}

/*
 * crcUDP() - Checksum for UDP v4 communication
 *
 * @buf = Data for calculation
 * @bufLen = Length of data
 * @uh = UDP header
 * return: Checksum
 */
u_short
crcUDP(struct in_addr src, struct in_addr dst, u_char * __restrict uh)
{
	struct psd_udp {
		struct in_addr src;
		struct in_addr dst;
		u_char pad;
		u_char proto;
		u_short udp_len;
		u_char udp[8];
	} buf;

	buf.src = src;
	buf.dst = dst;
	buf.pad = 0;
	buf.proto = IPPROTO_UDP;
	buf.udp_len = htons(sizeof buf.udp);
	memcpy(&buf.udp, uh, sizeof buf.udp);

	return crcIP((u_char*) &buf, sizeof buf);
}


/*
 * crcFletcher16() - Fletcher-16 Checksum computing
 *
 * @nBuf = Data for calculation
 * @bufLen = Length of data
 * return: Checksum
 */
u_short
crcFletcher16(u_short * __restrict nBuf, int bufLen)
{
	register u_short s1, s2;
	register u_int clen;

	assert(nBuf);

	s1 = s2 = 0xFF;
	while (bufLen) {
		clen = bufLen > MAX_FLETCHER16_DIGEST ? MAX_FLETCHER16_DIGEST : bufLen;
		bufLen -= clen;

		do {
			s1 += (u_short) *nBuf++;
			s2 += s1;
		} while (--clen);

		s1 = (s1 >> 8) + (s1 & 0xFF);
		s2 = (s2 >> 8) + (s2 & 0xFF);
	}

	return (s2 << 8) | s1;
}

/*
 * crcFletcher() - Fletcher-32 Checksum computing
 *
 * @nBuf = Data for calculation
 * @bufLen = Length of data
 * return: Checksum
 */
u_int
crcFletcher(u_short * __restrict nBuf, int bufLen)
{
	register u_int s1, s2, clen;

	assert(nBuf);

	s1 = s2 = 0xFFFF;
	while (bufLen) {
		clen = bufLen > MAX_FLETCHER_DIGEST ? MAX_FLETCHER_DIGEST : bufLen;
		bufLen -= clen;

		do {
			s1 += (u_int) *nBuf++;
			s2 += s1;
		} while (--clen);

		s1 = (s1 >> 16) + (s1 & 0xFFFF);
		s2 = (s2 >> 16) + (s2 & 0xFFFF);
	}

	return (s2 << 16) | s1;
}

/*
 * crcAdler() - crcAdler-32 Checksum computing
 *
 * @psBuf = Data for calculation
 * @bufLen = Length of data
 * return: Checksum
 */
u_int
crcAdler(u_char * __restrict psBuf, int bufLen)
{
	register u_int s1 = 1, s2 = 0, clen;

	assert(psBuf);

	while (bufLen) {
		clen = bufLen > MAX_ADLER_DIGEST ? MAX_ADLER_DIGEST : bufLen;
		bufLen -= clen;

		do {
			s1 += (u_int) *psBuf++;
			s2 += s1;
		} while (--clen);

		s1 %= crc_modAdler;
		s2 %= crc_modAdler;
	}

	return (s2 << 16) | s1;
}

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