获取crc32的程序
源程序稍作了修改(chjianash)
===============================================================================
crc32.c
===============================================================================
/*******************************************************************************
crc32.c by elektron
Compile:
# gcc -lz -o crc32 crc32.c
Usage:
# ./crc32 -h
crc32 by elektron
crc32 [-h] file1 [fileN...]
Notes:
This is a hack from someone elses code :
Find the most current version of this application at:
*******************************************************************************/
#define VERSION "v.0.1.1"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define _GNU_SOURCE
#include
int usage( char *progname )
{
printf( "\n%s %s by elektron\n", progname, VERSION);
printf( " %s [-h] file1 [fileN...]\n\n", progname );
}
int getFileSize( char *filename, unsigned long *size )
{
FILE *input_file;
unsigned long file_size;
input_file = fopen( filename, "r" );
if( input_file == NULL ) {
printf( "Could not open %s for reading.\n", filename );
return -1;
}
fseek( input_file, 0, SEEK_END );
file_size = ftell( input_file );
rewind( input_file);
fclose( input_file );
memcpy(size, &file_size, sizeof(file_size));
return 0;
}
int computeCRC32( char *filename, unsigned long *crcaddr )
{
int input_fd;
char *file_buffer;
unsigned long file_size;
unsigned long chunk_size=1024;
unsigned long bytes_read;
unsigned long crc=0L;
if ( getFileSize( filename, &file_size ) != 0) {
printf( "error determining file size\n" );
return -1;
}
if( file_size == 0 ) {
printf( "File size is calculated to be 0. Cannot calculate CRC\n" );
return -1;
}
//printf( "size:0x%08X\n", file_size );
file_buffer = (char *)malloc( chunk_size );
if( file_buffer == NULL ) {
printf( "Unable to reserve enough memory for file buffer.\n" );
return -2;
}
crc = crc32( 0L, Z_NULL, 0 );
input_fd = open( filename, O_RDONLY );
if( input_fd == -1 ) {
printf( "Could not open %s for reading.\n", filename );
free( file_buffer );
return -3;
}
while ( 1 ) {
bytes_read = read( input_fd, file_buffer, chunk_size );
if (bytes_read == 0 ) {
break;
}
if (bytes_read == -1 ) {
perror("read");
free( file_buffer );
return -4;
}
crc = crc32( crc, file_buffer, bytes_read );
}
free( file_buffer );
close( input_fd );
memcpy(crcaddr, &crc, sizeof(crc));
return 0;
}
int main( int argc, char *argv[] )
{
unsigned long crc=0L;
extern char *optarg;
extern int optind, opterr, optopt;
int i,e=0;
while ( getopt( argc, argv, "h" ) != -1 ) {
// printf("optarg = %s, optind = %d, opterr = %d, optopt = %d\n",
// optarg, optind, opterr, optopt);
if ( strncmp( argv[optind-1], "-h", 2 ) == 0 ) {
usage( basename(argv[0]) );
return 0;
}
}
//printf("optarg = %s, optind = %d, opterr = %d, optopt = %d, argc = %d\n",
// optarg, optind, opterr, optopt, argc);
if( argc < optind+1 ) {
usage( basename(argv[0]) );
return 2;
}
for ( i=optind; i if ( computeCRC32(argv[i], &crc ) != 0 ) {
e=1;
} else {
printf("0x%08X %s\n", crc, argv[i] );
}
}
return e;
}
===============================================================================
===============================================================================
Makefile
===============================================================================
####
# DYNAMIC
LIBS=-lz
STATIC=
#
####
# STATIC
#LIBS=/usr/lib/libz.a
#STATIC=-static
####
crc32: crc32.o
gcc ${STATIC} -o crc32 crc32.o ${LIBS}
all: crc32
install: crc32
strip crc32
install -d ${DESTDIR}/usr/local/bin
install crc32 ${DESTDIR}/usr/local/bin
clean:
rm -f crc32.o crc32
===============================================================================
其他程序:
CalcChecksum is a tool for calculating MD5, MD4, CRC32, SHA1 (SHA160), SHA256, RIPE-MD-160, TIGER, HAVAL (128, 160, 192, 224, 256 with 3, 4 or 5 passes) on files, text-strings or hash-lists generated by md5sum or sha1sum.
===============================================================================
===============================================================================
阅读(1180) | 评论(0) | 转发(0) |