source: liacs/alg/solitaire/crc/Tst_crc.c@ 92

Last change on this file since 92 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

  • Property svn:executable set to *
File size: 9.6 KB
RevLine 
[2]1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "lib_crc.h"
6
7
8
9 /*******************************************************************\
10 * *
11 * Library : lib_crc *
12 * File : tst_crc.c *
13 * Author : Lammert Bies 1999-2005 *
14 * E-mail : info@lammertbies.nl *
15 * Language : ANSI C *
16 * *
17 * *
18 * Description *
19 * =========== *
20 * *
21 * The file tst_crc.c contains a small sample program which *
22 * demonstrates the use of the functions for calculating the *
23 * CRC-CCITT, CRC-16 and CRC-32 values of data. The file cal- *
24 * culates the three different CRC's for a file who's name is *
25 * either provided at the command line, or typed in right *
26 * after the program has started. *
27 * *
28 * *
29 * Dependencies *
30 * ============ *
31 * *
32 * lib_crc.h CRC definitions and prototypes *
33 * lib_crc.c CRC routines *
34 * *
35 * *
36 * Modification history *
37 * ==================== *
38 * *
39 * Date Version Comment *
40 * *
41 * 2005-02-14 1.12 Added CRC-CCITT with initial value 0. *
42 * *
43 * 2005-02-05 1.11 Fixed post processing bug in CRC-DNP. *
44 * *
45 * 2005-02-04 1.10 Added the CRC calculation for DNP 3.0, *
46 * a protocol used in the communication *
47 * between remote units and masters in the *
48 * electric utility industry. The method *
49 * of calculation is the same as CRC-16, *
50 * but with a different polynomial. *
51 * *
52 * 2005-01-07 1.02 Changed way program is used. When a *
53 * commandline parameter is present, the *
54 * program assumes it is a file, but when *
55 * invoked without a parameter the entered *
56 * string is used to calculate the CRC. *
57 * *
58 * CRC's are now printed in hexadecimal *
59 * decimal format. *
60 * *
61 * Let CRC-CCITT calculation start with *
62 * 0xffff as this is used in most imple- *
63 * mentations. *
64 * *
65 * 1999-02-21 1.01 none *
66 * *
67 * 1999-01-22 1.00 Initial source *
68 * *
69 \*******************************************************************/
70
71#define MAX_STRING_SIZE 2048
72
73
74
75int main( int argc, char *argv[] ) {
76
77 char input_string[MAX_STRING_SIZE];
78 char *ptr, *dest, hex_val;
79 unsigned short crc_16, crc_ccitt_ffff, crc_ccitt_0000, crc_dnp, low_byte, high_byte;
80 unsigned long crc_32;
81 int a, ch, do_ascii, do_hex;
82 FILE *fp;
83
84 do_ascii = FALSE;
85 do_hex = FALSE;
86
87 printf( "\nCRC algorithm sample program\nLammert Bies, Version " CRC_VERSION "\n\n" );
88
89 if ( argc < 2 ) {
90
91 printf( "Usage: tst_crc [-a|-x] file1 ...\n\n" );
92 printf( " -a Program asks for ASCII input. Following parameters ignored.\n" );
93 printf( " -x Program asks for hexadecimal input. Following parameters ignored.\n" );
94 printf( " All other parameters are treated like filenames. The CRC values\n" );
95 printf( " for each separate file will be calculated.\n" );
96
97 exit( 0 );
98 }
99
100 if ( ! strcmp( argv[1], "-a" ) || ! strcmp( argv[1], "-A" ) ) do_ascii = TRUE;
101 if ( ! strcmp( argv[1], "-x" ) || ! strcmp( argv[1], "-X" ) ) do_hex = TRUE;
102
103 if ( do_ascii || do_hex ) {
104
105 printf( "Input: " );
106 fgets( input_string, MAX_STRING_SIZE-1, stdin );
107 }
108
109 if ( do_ascii ) {
110
111 ptr = input_string;
112 while ( *ptr && *ptr != '\r' && *ptr != '\n' ) ptr++;
113 *ptr = 0;
114 }
115
116 if ( do_hex ) {
117
118 ptr = input_string;
119 dest = input_string;
120
121 while( *ptr && *ptr != '\r' && *ptr != '\n' ) {
122
123 if ( *ptr >= '0' && *ptr <= '9' ) *dest++ = (char) ( (*ptr) - '0' );
124 if ( *ptr >= 'A' && *ptr <= 'F' ) *dest++ = (char) ( (*ptr) - 'A' + 10 );
125 if ( *ptr >= 'a' && *ptr <= 'f' ) *dest++ = (char) ( (*ptr) - 'a' + 10 );
126
127 ptr++;
128 }
129
130 * dest = '\x80';
131 *(dest+1) = '\x80';
132 }
133
134
135
136 a = 1;
137
138 do {
139
140 crc_16 = 0;
141 crc_dnp = 0;
142 crc_ccitt_0000 = 0;
143 crc_ccitt_ffff = 0xffff;
144 crc_32 = 0xffffffffL;
145
146
147
148 if ( do_ascii ) {
149
150 ptr = input_string;
151
152 while ( *ptr ) {
153
154 crc_16 = update_crc_16( crc_16, *ptr );
155 crc_dnp = update_crc_dnp( crc_dnp, *ptr );
156 crc_ccitt_0000 = update_crc_ccitt( crc_ccitt_0000, *ptr );
157 crc_ccitt_ffff = update_crc_ccitt( crc_ccitt_ffff, *ptr );
158 crc_32 = update_crc_32( crc_32, *ptr );
159
160 ptr++;
161 }
162 }
163
164
165
166 else if ( do_hex ) {
167
168 ptr = input_string;
169
170 while ( *ptr != '\x80' ) {
171
172 hex_val = (char) ( ( * ptr & '\x0f' ) << 4 );
173 hex_val |= (char) ( ( *(ptr+1) & '\x0f' ) );
174
175 crc_16 = update_crc_16( crc_16, hex_val );
176 crc_dnp = update_crc_dnp( crc_dnp, hex_val );
177 crc_ccitt_0000 = update_crc_ccitt( crc_ccitt_0000, hex_val );
178 crc_ccitt_ffff = update_crc_ccitt( crc_ccitt_ffff, hex_val );
179 crc_32 = update_crc_32( crc_32, hex_val );
180
181 ptr += 2;
182 }
183
184 input_string[0] = 0;
185 }
186
187
188
189 else {
190
191 fp = fopen( argv[a], "rb" );
192
193 if ( fp != NULL ) {
194
195 while( ( ch=fgetc( fp ) ) != EOF ) {
196
197 crc_16 = update_crc_16( crc_16, (char) ch );
198 crc_dnp = update_crc_dnp( crc_dnp, (char) ch );
199 crc_ccitt_0000 = update_crc_ccitt( crc_ccitt_0000, (char) ch );
200 crc_ccitt_ffff = update_crc_ccitt( crc_ccitt_ffff, (char) ch );
201 crc_32 = update_crc_32( crc_32, (char) ch );
202 }
203
204 fclose( fp );
205 }
206
207 else printf( "%s : cannot open file\n", argv[a] );
208 }
209
210
211
212 crc_32 ^= 0xffffffffL;
213
214 crc_dnp = ~crc_dnp;
215 low_byte = (crc_dnp & 0xff00) >> 8;
216 high_byte = (crc_dnp & 0x00ff) << 8;
217 crc_dnp = low_byte | high_byte;
218
219
220 printf( "%s%s%s :\nCRC16 = 0x%04X / %u\n"
221 "CRC-CCITT (0x0000) = 0x%04X / %u\n"
222 "CRC-CCITT (0xffff) = 0x%04X / %u\n"
223 "CRC-DNP = 0x%04X / %u\n"
224 "CRC32 = 0x%08lX / %lu\n"
225 , ( do_ascii || do_hex ) ? "\"" : ""
226 , ( ! do_ascii && ! do_hex ) ? argv[a] : input_string
227 , ( do_ascii || do_hex ) ? "\"" : ""
228 , crc_16, crc_16
229 , crc_ccitt_0000, crc_ccitt_0000
230 , crc_ccitt_ffff, crc_ccitt_ffff
231 , crc_dnp, crc_dnp
232 , crc_32, crc_32 );
233
234 a++;
235
236 } while ( a < argc );
237
238} /* main (tst_crc.c) */
Note: See TracBrowser for help on using the repository browser.