1 |
|
---|
2 |
|
---|
3 |
|
---|
4 | File : lib_crc.txt
|
---|
5 | Date : 2005-02-05
|
---|
6 | Author : Lammert Bies
|
---|
7 | E-mail : info@lammertbies.nl
|
---|
8 | Version : 1.11
|
---|
9 |
|
---|
10 | Contents : Documentation for the CRC functions in lib_crc.c
|
---|
11 |
|
---|
12 |
|
---|
13 |
|
---|
14 | The file lib_crc.c contains source code for functions to calculate
|
---|
15 | four commonly used CRC values: CRC-16, CRC-32, CRC-DNP and CRC-CCITT.
|
---|
16 | The functions can be freely used.
|
---|
17 |
|
---|
18 | To calculate a CRC, the following three steps must be followed:
|
---|
19 |
|
---|
20 | 1. Initialize the CRC value. For CRC-16 and CRC-DNP the initial value
|
---|
21 | of the CRC is 0. For CRC-CCITT the value 0xffff is used. CRC-32
|
---|
22 | starts with an initial value of 0xffffffffL.
|
---|
23 |
|
---|
24 | 2. For each byte of the data starting with the first byte, call the
|
---|
25 | function update_crc_16(), update_crc_32(), update_crc_dnp() or
|
---|
26 | update_crc_ccitt() to recalculate the value of the CRC.
|
---|
27 |
|
---|
28 | 3. Only for CRC-32: When all bytes have been processed, take the
|
---|
29 | one's complement of the obtained CRC value.
|
---|
30 |
|
---|
31 | 4. Only for CRC-DNP: After all input processing, the one's complement
|
---|
32 | of the CRC is calcluated and the two bytes of the CRC are swapped.
|
---|
33 |
|
---|
34 |
|
---|
35 |
|
---|
36 | An example of this calculation process can be found in the tst_crc.c
|
---|
37 | sample program. The program and other CRC implementations can be
|
---|
38 | tested with the test string "123456789" without the quotes. The
|
---|
39 | results should be:
|
---|
40 |
|
---|
41 | CRC16 : BB3D
|
---|
42 | CRC-CCITT : 29B1
|
---|
43 | CRC-DNP : 82EA
|
---|
44 | CRC32 : CBF43926
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | The example program tst_crc.exe can be invoked in three ways:
|
---|
49 |
|
---|
50 | tst_crc -a
|
---|
51 |
|
---|
52 | The program will prompt for an input string. All characters in the
|
---|
53 | input string are used for the CRC calculation, based on their ASCII
|
---|
54 | value.
|
---|
55 |
|
---|
56 | Example input string: ABC
|
---|
57 | CRC16 = 0x4521
|
---|
58 | CRC-CCITT = 0xF508
|
---|
59 | CRC-DNP = 0x5AD3
|
---|
60 | CRC32 = 0xA3830348
|
---|
61 |
|
---|
62 | tst_crc -x
|
---|
63 |
|
---|
64 | The program will prompt for an input string. All characters will
|
---|
65 | be filtered out, except for 0..9, a..f and A..F. The remaining characters
|
---|
66 | will be paired, and every pair of two characters represent the hexadecimal
|
---|
67 | value to be used for one byte in the CRC calculation. The result if an
|
---|
68 | od number of valud characters is provided is undefined.
|
---|
69 |
|
---|
70 | Example input string: 41 42 43
|
---|
71 | CRC16 = 0x4521
|
---|
72 | CRC-CCITT = 0xF508
|
---|
73 | CRC-DNP = 0x5AD3
|
---|
74 | CRC32 = 0xA3830348
|
---|
75 |
|
---|
76 | You see, that the result is the same as for the ASCII input "ABC". This
|
---|
77 | is, because A, B and C are represented in ASCII by the hexadecimal
|
---|
78 | values 41, 42 and 43. So it is obvious that the result should be
|
---|
79 | the same in both cases.
|
---|
80 |
|
---|
81 | tst_crc file1 file2 ...
|
---|
82 |
|
---|
83 | If neither the -a, nor the -x parameter is used, the test program
|
---|
84 | assumes that the parameters are file names. Each file is opened and
|
---|
85 | the CRC values are calculated.
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 | The newest version of these files can be found at:
|
---|
90 |
|
---|
91 | http://www.lammertbies.nl/download/lib_crc.zip
|
---|
92 |
|
---|
93 | On-line CRC calculations of strings can be performed at:
|
---|
94 |
|
---|
95 | http://www.lammertbies.nl/comm/info/crc-calculation.html
|
---|