📄 crc32.cpp
字号:
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Crc32.h"
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
// define static member
FastCrc::Table FastCrc::_table;
void FastCrc::Table::Init (Crc::Type key)
{
assert (key != 0);
if (key == _key)
return;
_key = key;
//Application->MessageBox("This should be on top.", "Look", MB_OK);
// for all possible byte values
for (unsigned i = 0; i < 256; ++i)
{
Crc::Type reg = i << 24;
// for all bits in a byte
for (int j = 0; j < 8; ++j)
{
bool topBit = (reg & 0x80000000) != 0;
reg <<= 1;
if (topBit)
reg ^= _key;
}
_table [i] = reg;
}
}
void FastCrc::PutByte (unsigned byte)
{
unsigned top = _register >> 24;
top ^= byte;
_register = (_register << 8) ^ _table [top];
}
/*
int main ()
{
std::string msg ("Harry had a little lamp");
std::string exMsg (msg);
exMsg.resize (msg.length () + 4);
Crc::Type const ethernetKey = 0x04c11db7;
FastCrc fastCrc (ethernetKey);
for (int i = 0; i < msg.length (); ++i)
{
fastCrc.PutByte (msg [i]);
}
Crc::Type newCrc = fastCrc.Done ();
std::cout << "\n0x" << std::hex << newCrc << std::endl;
return 0;
}
*/
void SlowCrc::PutBit (bool bit)
{
// std::cout << bit? "1": "0";
bool topBit = (_register & 0x80000000) != 0;
// shift bit into register from the right
_register <<= 1;
_register ^= (bit? 0x1: 0x0); // OR or XOR, same result
if (topBit)
{
// XOR the 32-bits of the key.
// The implicit high bit of the 33-bit key conceptually
// clears the topBit shifted out of the register
_register ^= _key;
}
}
unsigned long __fastcall FastCrc::CalcCRC32(unsigned char * Buf, int Size)
{
unsigned long crc32;
for(int i=0;i<(Size);i++)PutByte(Buf[i]);
crc32=Done();
return crc32;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -