#ifndef Crc32H
#define Crc32H
#endif
#include
class CCrc32
{
public:
CCrc32();
virtual ~CCrc32();
void Init(void);
void Free(void);
unsigned long int FileCrc32(AnsiString FileName);
AnsiString FileCrc32Hex(AnsiString FileName);
private:
unsigned long int *crc32_tab;
};
Implementacja: crc32.cpp
#pragma hdrstop
#include "Crc32.h"
#include
#include
#define BUFSIZE 8192
#pragma package(smart_init)
CCrc32::CCrc32()
{
}
CCrc32::~CCrc32()
{
Free();
}
void CCrc32::Free(void)
{
delete[] crc32_tab;
}
void CCrc32::Init(void)
{
// Often times the polynomial shown reversed as 0x04C11DB7.
unsigned long int dwPolynomial = 0xEDB88320;
int i = 0, j = 0;
crc32_tab = new unsigned long int[256];
unsigned long int dwCrc;
for(i = 0; i < 256; i++)
{
dwCrc = i;
for(j = 8; j > 0; j--)
{
if(dwCrc & 1)
dwCrc = (dwCrc >> 1) ^ dwPolynomial;
else
dwCrc >>= 1;
}
crc32_tab[i] = dwCrc;
}
}
unsigned long int CCrc32::FileCrc32(AnsiString FileName)
{
FILE *file = NULL;
unsigned char buf[BUFSIZE];
unsigned long int crc32 = 0;
size_t i = 0, n = 0;
if(!(file = fopen(FileName.c_str(), "rb")))
{
return -1;
}
fseek(file, 0, SEEK_SET);
crc32 = 0xffffffff;
while((n = fread(buf, 1, BUFSIZE, file)) > 0)
{
for(i = 0; i < n; i++)
crc32 = (crc32 >> 8) ^ crc32_tab[(crc32 & 0xff) ^ buf[i]];
}
crc32 ^= 0xffffffff;
fclose(file);
return crc32;
}
AnsiString CCrc32::FileCrc32Hex(AnsiString FileName)
{
unsigned long int wynik;
AnsiString szHex;
wynik = FileCrc32(FileName);
TVarRec vr[] = { wynik };
szHex = Format("%X",vr,1);
if (szHex.Length()<8) {
for (int i = szHex.Length(); i < 8; i++) szHex ="0" +szHex;
}
szHex ="0x"+szHex;
return szHex;
}
Brak komentarzy:
Prześlij komentarz