📄 crc.cpp
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
unsigned long CRCTable[ 256 ];
#define SEPARATOR "/"
#define FILENAME_SIZE 260
#define MSDOS 1
#define Mob 512
#include <stdarg.h>
#include <dos.h>
#define FILE_INFO struct find_t
#define FIND_FIRST( n, i ) _dos_findfirst( (n), _A_SUBDIR, (i) )
#define FIND_NEXT( info ) _dos_findnext( ( info ) )
#define FILE_NAME( info ) ( ( info ).name )
unsigned long CalculateFileCRC( FILE *file );
void BuildCRCTable( void );
unsigned long CalculateBufferCRC( unsigned int count, unsigned long crc, void *buffer );
unsigned long CheckFiles( char *na);
#define CRC32_POLYNOMIAL 0xEDB88320L
#define defi 0x7c211d96L
unsigned long CheckFiles( char *na)
{
FILE *crc_file;
FILE *test_file;
unsigned long crc;
BuildCRCTable();
test_file = fopen( na, "rb" );
if ( test_file != NULL ) {
crc = CalculateFileCRC( test_file );
fclose( test_file );
return crc;
}
return NULL;
}
/*
* This routine is responsible for actually performing the
* calculation of the 32 bit CRC for the entire file. We
* precondition the CRC value with all 1's, then invert every bit
* after the entire file has been done. This gives us a CRC value
* that corresponds with the values calculated by PKZIP and ARJ.
* The actual calculation consists of reading in blocks from the
* file, then updating the CRC with the value for that block. The
* CRC work is done by another the CalculateBufferCRC routine.
*/
unsigned long CalculateFileCRC( FILE *file )
{
unsigned long crc;
int count;
unsigned char buffer[ Mob+1 ];
int i;
crc = 0xFFFFFFFFL;
i = 0;
for ( ; ; ) {
count = fread( buffer, 1, Mob, file );
//if ( ( i++ % 32 ) == 0 ) { } //putc( '.', stdout );
if ( count == 0 ) break;
crc = CalculateBufferCRC( count, crc, buffer );
}
// putc( ' ', stdout );
return( crc ^= 0xFFFFFFFFL );
}
void BuildCRCTable()
{
int i;
int j;
unsigned long crc;
for ( i = 0; i <= 255 ; i++ ) {
crc = i;
for ( j = 8 ; j > 0; j-- ) {
if ( crc & 1 )
crc = ( crc >> 1 ) ^ CRC32_POLYNOMIAL;
else
crc >>= 1;
}
CRCTable[ i ] = crc;
}
}
unsigned long CalculateBufferCRC( unsigned int count, unsigned long crc, void *buffer )
{
unsigned char *p;
unsigned long temp1;
unsigned long temp2;
p = (unsigned char*) buffer;
while ( count-- != 0 ) {
temp1 = ( crc >> 8 ) & 0x00FFFFFFL;
temp2 = CRCTable[ ( (int) crc ^ *p++ ) & 0xff ];
crc = temp1 ^ temp2;
}
return( crc );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -