W
crc.cpp
#include "crc.h"
#include <dirent.h>
#include <fstream.h>
#include <system.hpp>
#define FAIL 1
#define PASS 0
#define BUFFERLEN 8192
#define CRC32_DEFAULT_POLYNOMIAL 0x04c11db7L
MyCRC::MyCRC(){
}
void MyCRC::InitCrcCheckSum(unsigned long int *value)
{
*value = 0xFFFFFFFFL;
}
void MyCRC::GenCrcTable(unsigned long int value)
// generate the table of CRC remainders for all possible bytes
{
register int i, j;
register unsigned long CrcAccum;
if (0l==value)
value=CRC32_DEFAULT_POLYNOMIAL;
for ( i = 0; i < 256; i++ )
{
CrcAccum = ( (unsigned long) i << 24 );
for ( j = 0; j < 8; j++ )
{
CrcAccum = ( CrcAccum & 0x80000000L )
? ( ( CrcAccum << 1 ) ^ value)
: ( CrcAccum << 1 );
}
crc_table[i] = CrcAccum;
}
}
unsigned long int MyCRC::CrcCheckSumC(char *tx_message,unsigned long int length, unsigned long int *value)
{
register unsigned long int i, j;
register unsigned char count = 0;
// update the CRC on the data block one byte at a time
for ( j = 0; j < length; j++ )
{
i = ( (unsigned int) ( *value >> 24) ^ tx_message[j] ) & 0xff;
*value = ( *value << 8 ) ^ crc_table[i];
}
return *value;
} //end checksum
unsigned long int MyCRC::CrcCheckSum(char *tx_message,unsigned long int length)
{
unsigned long int value;
InitCrcCheckSum(&value);
return CrcCheckSumC(tx_message,length,&value);
} // end checksum
int MyCRC::FileCheckSum(char *file,unsigned long int *result)
{
unsigned long int i;
char Buffer[BUFFERLEN+1];
unsigned long int numread;
FILE *fp;
GenCrcTable(0);
InitCrcCheckSum(result);
if ((fp = fopen(file, "rb")) == NULL)
return FAIL;
while (!feof(fp))
{
numread = fread( Buffer, sizeof( char ), BUFFERLEN, fp);
if (0 != (numread % 4))
{
for (i=numread;i<BUFFERLEN;i++)
Buffer[i]=0x00;
numread=((numread % 4)+1)*4;
}
*result=CrcCheckSumC(Buffer,numread,result);
if (feof(fp))
break;
}
return PASS;
}
und noch die crc.h
class MyCRC{
public:
MyCRC();
int FileCheckSum(char *file,unsigned long int *result);
private:
unsigned long crc_table[256];
unsigned long int value;
void InitCrcCheckSum(unsigned long int *value);
void GenCrcTable(unsigned long int value);
unsigned long int CrcCheckSumC(char *tx_message,unsigned long int length, unsigned long int *value);
unsigned long int CrcCheckSum(char *tx_message,unsigned long int length);
};
ich hoffe du kannst mir jetzt weiterhelfen.
mfg
wdsl