Thank you
- Tim
// My attempt at generating a CRC-16 checksum
// reflectByte() and reflectWord() are functions
// I wrote and tested to reverse the order of the bits
// because the description of the CRC-16 algorithm
// from:
// http://www.cubic.org/source/archive/coding/algorith/misc/crc.txt
//
// Name : "CRC-16"
// Width : 16
// Poly : 8005
// Init : 0000
// RefIn : True
// RefOut : True
// XorOut : 0000
// Check : BB3D
// Says that each byte is reflected before being
// processed (RefIn) and the final register (checksum)
// value is reflected (RefOut).
//
WORD crc16(BYTE buf[], UINT nSize)
{
WORD reg = 0; // Checksum register.
WORD POLY = 0x8005, // Divisor Polynomial.
wPop; // Used to test wether to XOR the register
UINT n;
BYTE bit, curByte;
for(n = 0; n < nSize; n++)
{
// Reflect the byte
curByte = reflectByte(buf[n]);
// start with bit 7 and loop to bit 0
for(bit = 0x80; bit; bit = (bit / 2))
{
wPop = reg & 0x8000;
reg <<= 1;
if(curByte & bit)
reg |= 0x0001;
// if the bit popped off the register was set
if(wPop)
reg ^= POLY; // XOR in the polynomial
}
}
// reflect register value
reg = reflectWord(reg);
// XOR in the XOROut value
reg ^= 0x0000;
return reg;
}
Go to the Usenet archives (such as DejaNews) and pull up a copy of Ward
Christensen's XMODEM source, and look how it is done in there.
ftp://ftp.cs.pdx.edu/pub/zmodem/rzsz.zip
Then check the page: "The Great CRC Mystery" by Terry Ritter
http://www.io.com/~ritter/ARTS/CRCMYST.HTM
And "CRC: Resources On The Net
This page contains links to other CRC resources on the Internet."
http://www.ross.net/crc/links.html
And "Fil's FAQ-Link-In Corner: CRC algorithms"
http://www.repairfaq.org/filipg/LINK/F_crc_v35.html
I noticed another page (in another language) with an implementation:
http://www.robustdc.com/crc_vb.txt
And "CRC.DYL: A DYL for calculating common CRCs"
http://ourworld.compuserve.com/homepages/cade/crc.htm
(and this is just out of my bookmarks...)
--
Scott G. Hall
GTE Government Systems
North Carolina Systems Center
email: Scott...@GSC.GTE.Com