📄 tst_crc.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib_crc.h"
/*******************************************************************\
* *
* Library : lib_crc *
* File : tst_crc.c *
* Author : Lammert Bies 1999 *
* E-mail : info@lammertbies.nl *
* Language : ANSI C *
* *
* *
* Description *
* =========== *
* *
* The file tst_crc.c contains a small sample program which *
* demonstrates the use of the functions for calculating the *
* CRC-CCITT, CRC-16 and CRC-32 values of data. The file cal- *
* culates the three different CRC's for a file who's name is *
* either provided at the command line, or typed in right *
* after the program has started. *
* *
* *
* Dependencies *
* ============ *
* *
* lib_crc.h CRC definitions and prototypes *
* lib_crc.c CRC routines *
* *
* *
* Modification history *
* ==================== *
* *
* Date Version Comment *
* *
* 1999-02-21 1.01 none *
* *
* 1999-01-22 1.00 Initial source *
* *
\*******************************************************************/
void main( int argc, char *argv[] ) {
char file[256];
char *ptr;
unsigned short crc_16, crc_ccitt;
unsigned long crc_32;
int a, ch;
FILE *fp;
printf( "\nCRC algorithm sample program\nLammert Bies, Version " CRC_VERSION "\n\n" );
if ( argc < 2 ) {
printf( "File: " );
fgets( file, 255, stdin );
}
ptr = file;
while ( *ptr && *ptr != '\r' && *ptr != '\n' ) ptr++;
*ptr = 0;
a = 1;
do {
if ( a < argc ) strcpy( file, argv[a] );
crc_16 = 0;
crc_ccitt = 0;
crc_32 = 0xffffffffL;
fp = fopen( file, "rb" );
if ( fp != NULL ) {
while( ( ch=fgetc( fp ) ) != EOF ) {
crc_16 = update_crc_16( crc_16, (char) ch );
crc_ccitt = update_crc_ccitt( crc_ccitt, (char) ch );
crc_32 = update_crc_32( crc_32, (char) ch );
}
crc_32 ^= 0xffffffffL;
fclose( fp );
printf( "%-20s : CRC16 = 0x%04X CRC-CCITT = 0x%04X CRC32 = 0x%08lX\n"
, file
, crc_16
, crc_ccitt
, crc_32 );
}
else printf( "%-20s : cannot open file\n", file );
a++;
} while ( a < argc );
} /* main (tst_crc.c) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -