81 #define MAX_STRING_SIZE 2048
85 void main(
int argc,
char *argv[] ) {
88 char *ptr, *dest, hex_val, prev_byte;
89 unsigned short crc_16, crc_16_modbus, crc_ccitt_ffff, crc_ccitt_0000, crc_ccitt_1d0f, crc_dnp, crc_sick, crc_kermit;
90 unsigned short low_byte, high_byte;
92 int a, ch, do_ascii, do_hex;
98 printf(
"\nCRC algorithm sample program\nLammert Bies, Version " CRC_VERSION "\n\n" );
102 printf(
"Usage: tst_crc [-a|-x] file1 ...\n\n" );
103 printf(
" -a Program asks for ASCII input. Following parameters ignored.\n" );
104 printf(
" -x Program asks for hexadecimal input. Following parameters ignored.\n" );
105 printf(
" All other parameters are treated like filenames. The CRC values\n" );
106 printf(
" for each separate file will be calculated.\n" );
111 if ( ! strcmp( argv[1],
"-a" ) || ! strcmp( argv[1],
"-A" ) ) do_ascii =
CRC_TRUE;
112 if ( ! strcmp( argv[1],
"-x" ) || ! strcmp( argv[1],
"-X" ) ) do_hex =
CRC_TRUE;
114 if ( do_ascii || do_hex ) {
123 while ( *ptr && *ptr !=
'\r' && *ptr !=
'\n' ) ptr++;
132 while( *ptr && *ptr !=
'\r' && *ptr !=
'\n' ) {
134 if ( *ptr >=
'0' && *ptr <=
'9' ) *dest++ = (char) ( (*ptr) -
'0' );
135 if ( *ptr >=
'A' && *ptr <=
'F' ) *dest++ = (char) ( (*ptr) -
'A' + 10 );
136 if ( *ptr >=
'a' && *ptr <=
'f' ) *dest++ = (char) ( (*ptr) -
'a' + 10 );
152 crc_16_modbus = 0xffff;
156 crc_ccitt_ffff = 0xffff;
157 crc_ccitt_1d0f = 0x1d0f;
159 crc_32 = 0xffffffffL;
192 while ( *ptr !=
'\x80' ) {
194 hex_val = (char) ( ( * ptr &
'\x0f' ) << 4 );
195 hex_val |= (char) ( ( *(ptr+1) &
'\x0f' ) );
219 fp = fopen( argv[a],
"rb" );
221 if ( fp !=
nullptr ) {
223 while( ( ch=fgetc( fp ) ) != EOF ) {
235 prev_byte = (char) ch;
241 else printf(
"%s : cannot open file\n", argv[a] );
246 crc_32 ^= 0xffffffffL;
249 low_byte = (crc_dnp & 0xff00) >> 8;
250 high_byte = (crc_dnp & 0x00ff) << 8;
251 crc_dnp = low_byte | high_byte;
253 low_byte = (crc_sick & 0xff00) >> 8;
254 high_byte = (crc_sick & 0x00ff) << 8;
255 crc_sick = low_byte | high_byte;
257 low_byte = (crc_kermit & 0xff00) >> 8;
258 high_byte = (crc_kermit & 0x00ff) << 8;
259 crc_kermit = low_byte | high_byte;
261 printf(
"%s%s%s :\nCRC16 = 0x%04X / %u\n"
262 "CRC16 (Modbus) = 0x%04X / %u\n"
263 "CRC16 (Sick) = 0x%04X / %u\n"
264 "CRC-CCITT (0x0000) = 0x%04X / %u\n"
265 "CRC-CCITT (0xffff) = 0x%04X / %u\n"
266 "CRC-CCITT (0x1d0f) = 0x%04X / %u\n"
267 "CRC-CCITT (Kermit) = 0x%04X / %u\n"
268 "CRC-DNP = 0x%04X / %u\n"
269 "CRC32 = 0x%08lX / %lu\n"
270 , ( do_ascii || do_hex ) ?
"\"" :
""
271 , ( ! do_ascii && ! do_hex ) ? argv[a] : input_string
272 , ( do_ascii || do_hex ) ?
"\"" :
""
274 , crc_16_modbus, crc_16_modbus
276 , crc_ccitt_0000, crc_ccitt_0000
277 , crc_ccitt_ffff, crc_ccitt_ffff
278 , crc_ccitt_1d0f, crc_ccitt_1d0f
279 , crc_kermit, crc_kermit
285 }
while ( a < argc );
unsigned short update_crc_ccitt(unsigned short crc, char c)
unsigned short update_crc_kermit(unsigned short crc, char c)
unsigned short update_crc_sick(unsigned short crc, char c, char prev_byte)
unsigned long update_crc_32(unsigned long crc, char c)
unsigned short update_crc_dnp(unsigned short crc, char c)
unsigned short update_crc_16(unsigned short crc, char c)
void main(int argc, char *argv[])