Author: Colin Mahoney
This CRC web was created because of the popularity of a paper on
CRC algorithms written by Ross Williams and posted to some
Internet newsgroups on 19 August 1993. This paper is available by FTP as a
pure text file. It can also be browsed directly on the web:
Here is the title and abstract of the paper:
This document explains CRCs (Cyclic Redundancy Codes) and their table-driven implementations in full, precise detail. Much of the literature on CRCs, and in particular on their table-driven implementations, is a little obscure. This document is an attempt to provide a clear and simple no-nonsense explanation of CRCs and to absolutely nail down every detail of the operation of their high-speed implementations. In addition to this, this document presents a parameterized model CRC algorithm called the "Rocksoft^tm Model CRC Algorithm". The model algorithm can be parameterized to behave like most of the CRC implementations around, and so acts as a good reference for describing particular algorithms. A low-speed implementation of the model CRC algorithm is provided in the C programming language. Lastly there is a section giving two forms of high-speed table driven implementations, and providing a program that generates CRC lookup tables.
// Listing 2 ( from C Users Journal, June 1999 ) // // Author: Colin Mahoney (cmahoney@readysoft.es) // // /link setargv.obj req'd for wildcard handling in Microsoft VC++ (b.arnold) // #include <iostream> #include <fstream> #include <iterator> using namespace std; #include "crc.h" int main( int argc, char* argv[] ) { if( argc < 2 ) { cerr << "Usage: CRC16 file1 file2...\n"; return 1; } typedef cdm_crc::CRCGenerator<16, 0x8005, 0, 0, true> CRC16; CRC16 crc16; cout.setf( ios_base::hex, ios_base::basefield ); cout.setf( ios_base::showbase ); for( int i = 1; i < argc; i++ ) { ifstream infile( argv[i] ); infile.unsetf( ios_base::skipws ); if( !infile ) { cerr << "Error: unable to open " << argv[i] << '\n'; continue; } crc16.Process( istream_iterator<unsigned char>( infile ), istream_iterator<unsigned char>() ); unsigned long ncrcVal = crc16.GetNormalCRC(); ncrcVal >>= (cdm_crc::CRCMaxBits-crc16.GetWidth() ); cout << "File: " << argv[i] << '\t' << "CRC-16 checksum: " << ncrcVal << '\n'; } return 0; }