File : lib_crc.txt Date : 2005-02-05 Author : Lammert Bies E-mail : info@lammertbies.nl Version : 1.11 Contents : Documentation for the CRC functions in lib_crc.c The file lib_crc.c contains source code for functions to calculate four commonly used CRC values: CRC-16, CRC-32, CRC-DNP and CRC-CCITT. The functions can be freely used. To calculate a CRC, the following three steps must be followed: 1. Initialize the CRC value. For CRC-16 and CRC-DNP the initial value of the CRC is 0. For CRC-CCITT the value 0xffff is used. CRC-32 starts with an initial value of 0xffffffffL. 2. For each byte of the data starting with the first byte, call the function update_crc_16(), update_crc_32(), update_crc_dnp() or update_crc_ccitt() to recalculate the value of the CRC. 3. Only for CRC-32: When all bytes have been processed, take the one's complement of the obtained CRC value. 4. Only for CRC-DNP: After all input processing, the one's complement of the CRC is calcluated and the two bytes of the CRC are swapped. An example of this calculation process can be found in the tst_crc.c sample program. The program and other CRC implementations can be tested with the test string "123456789" without the quotes. The results should be: CRC16 : BB3D CRC-CCITT : 29B1 CRC-DNP : 82EA CRC32 : CBF43926 The example program tst_crc.exe can be invoked in three ways: tst_crc -a The program will prompt for an input string. All characters in the input string are used for the CRC calculation, based on their ASCII value. Example input string: ABC CRC16 = 0x4521 CRC-CCITT = 0xF508 CRC-DNP = 0x5AD3 CRC32 = 0xA3830348 tst_crc -x The program will prompt for an input string. All characters will be filtered out, except for 0..9, a..f and A..F. The remaining characters will be paired, and every pair of two characters represent the hexadecimal value to be used for one byte in the CRC calculation. The result if an od number of valud characters is provided is undefined. Example input string: 41 42 43 CRC16 = 0x4521 CRC-CCITT = 0xF508 CRC-DNP = 0x5AD3 CRC32 = 0xA3830348 You see, that the result is the same as for the ASCII input "ABC". This is, because A, B and C are represented in ASCII by the hexadecimal values 41, 42 and 43. So it is obvious that the result should be the same in both cases. tst_crc file1 file2 ... If neither the -a, nor the -x parameter is used, the test program assumes that the parameters are file names. Each file is opened and the CRC values are calculated. The newest version of these files can be found at: http://www.lammertbies.nl/download/lib_crc.zip On-line CRC calculations of strings can be performed at: http://www.lammertbies.nl/comm/info/crc-calculation.html