[comp.sources.wanted] CRC-16 source code wanted

paul@frcs.UUCP (Paul Nash) (04/18/91)

I am looking for C source code to implement the CRC-16 algorithm.  I
have seen various from time to time, but cannot think of a place.  If
you have a suitable (short) routine, please post it or mail me a copy,
or tell me where I can find one.

Many thanks

 ---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---
Paul Nash				   Free Range Computer Systems cc
paul@frcs.UUCP				      ...!uunet!m2xenix!frcs!paul

oz@yunexus.yorku.ca (Ozan Yigit) (04/24/91)

In article <461@frcs.UUCP> paul@frcs.UUCP (Paul Nash) writes:

>I am looking for C source code to implement the CRC-16 algorithm.

I sent you something that may be of some use. In case you need
some literature refs for future use, here is something that is
easy to implement:

%A Georgia Griffiths
%A G. Carlyle Stones
%T The Tea-Leaf Reader Algorithm:
An Efficient Implementation of CRC-16 and CRC-32
%J Communications of the ACM
%V 30
%N 7
%D 1987
%P 617-620

hope this is useful.	oz
---
SunOS: There are many edible ways to serve | internet: oz@nexus.yorku.ca
up scrambled eggs, but one cannot get them | uucp: utzoo/utai!yunexus!oz
back as over-easy or sunny side up.  -- oz | phone: [416] 736 5257x33976

gwyn@smoke.brl.mil (Doug Gwyn) (04/24/91)

In article <461@frcs.UUCP> paul@frcs.UUCP (Paul Nash) writes:
>I am looking for C source code to implement the CRC-16 algorithm.  I
>have seen various from time to time, but cannot think of a place.  If
>you have a suitable (short) routine, please post it or mail me a copy,
>or tell me where I can find one.

In the past few days, R.Hyde and I have been using CRC-16 as an example
for comparison of programming in C vs. assembler, in the comp.sys.apple2
newsgroup.  If it hasn't yet expired on your site, grep the archives for
"crc".

glenn@ready.com (Glenn Kasten) (04/24/91)

In article <461@frcs.UUCP>, paul@frcs.UUCP (Paul Nash) writes:
|> 
|> I am looking for C source code to implement the CRC-16 algorithm.

/* CRC-16 cyclic redundancy check algorithm */

void crc16(data_buffer, nbytes, crc_buffer)
char *data_buffer, crc_buffer[2];
unsigned nbytes;
{
    unsigned crc = (crc_buffer[0] & 0xFF) | ((crc_buffer[1] & 0xFF) << 8);
    unsigned i, bit;

    for (i = 0; i < nbytes; ++i) {
	crc ^= data_buffer[i] & 0xFF;
	for (bit = 0; bit < 8; ++bit)
	    crc = crc & 1 ? (crc >> 1) ^ 0120001 : crc >> 1;
    }
    crc_buffer[1] = crc >> 8;
    crc_buffer[0] = crc;
}
-- 
Glenn Kasten
Ready Systems 470 Potrero Ave. Sunnyvale CA 94086 
glenn@ready.com (408) 522-7357 
-- 
Glenn Kasten
Ready Systems 470 Potrero Ave. Sunnyvale CA 94086 
glenn@ready.com (408) 522-7357 

ake@dayton.saic.com (Earle Ake) (04/24/91)

In article <461@frcs.UUCP>, paul@frcs.UUCP (Paul Nash) writes:
> 
> I am looking for C source code to implement the CRC-16 algorithm.  I
> have seen various from time to time, but cannot think of a place.  If
> you have a suitable (short) routine, please post it or mail me a copy,
> or tell me where I can find one.

int calcrc(prt, count)
char *ptr;
int count;
{
	int crc, i;

	crc = 0;
	while (--count >= 0) {
		crc = crc ^ (int)*ptr++ << 8;
		for (i = 0; i < 8; ++i)
			if (crc & 0x8000)
				crc = crc << 1 ^ 0x1021;
			else
				crc = crc << 1;
		}
	return (crc & 0xFFFF);
}


Earle
_____________________________________________________________________________
             ____ ____    ___
Earle Ake   /___ /___/ / /     Science Applications International Corporation
           ____//   / / /__                 Dayton, Ohio
-----------------------------------------------------------------------------
Internet: ake@dayton.saic.com        uucp: dayvb!ake         SPAN: 28284::ake