望那位大虾给出奇校验,偶校验, crc校验的原码
解决方案 »
- 怎麼可以讓delphi不報錯
- 在线等 解决放上结贴 怎么能让一个程序开始运行时不显示主窗口
- 运行存储过程时的错误?
- 求具有数据缓存的stringgrid控件,就相ms sql 的企业管理器里边表的显示
- 紧急请教:ELTREE控件的使用方法和例子?(在线等待)
- 年前升星,明天回家,买到黄牛票,鄙视中国铁道部!!!
- 图像截取的问题
- 在sql server 2000里怎样锁住一个数据库,也就是说没有授权密码看不到表,存储过程等,即使使用sa登陆并用企业管理器也看不了,有没有可能实
- 部署WebService时遇到一个让人蛋疼的问题,有没有前辈会疗此伤???
- 初学Delphi,请大家推荐书籍!谢谢
- 大家帮帮忙,请问怎样把以下Excel格式导到Access库里?谢谢!
- 窗体无法关闭的问题??
可计算8、16、32位的CRC校验
初始化时应指明CRC的位长,多项式、运算顺序、初始值和屏蔽值---------------------------------------------------------------------------*/
#include <vcl.h>
#pragma hdrstop#include "CRCClass.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
UBYTE4 Crc::bits [BITSPERBYTE * 4] =
{ 0x00000001UL, 0x00000002UL, 0x00000004UL, 0x00000008UL,
0x00000010UL, 0x00000020UL, 0x00000040UL, 0x00000080UL,
0x00000100UL, 0x00000200UL, 0x00000400UL, 0x00000800UL,
0x00001000UL, 0x00002000UL, 0x00004000UL, 0x00008000UL,
0x00010000UL, 0x00020000UL, 0x00040000UL, 0x00080000UL,
0x00100000UL, 0x00200000UL, 0x00400000UL, 0x00800000UL,
0x01000000UL, 0x02000000UL, 0x04000000UL, 0x08000000UL,
0x10000000UL, 0x20000000UL, 0x40000000UL, 0x80000000UL,
} ;
//---------------------------------------------------------------------------
__fastcall Crc::Crc (unsigned int BitCount, unsigned long Polynominal,
bool Reverse, unsigned long Initial, unsigned long FinalMask)
// BitCount : CRC Size
// Polynominal : CRC Polynomial
// Reverse : Reversed
// Initial : Initial CRC Register Value
// FinalMask : Final CRC XOR Value
{
BITCOUNT = BitCount;
POLYNOMINAL = Polynominal;
INITIAL = Initial;
FINALMASK = FinalMask;
// Mask needed to mask off redundent bits
mask = ((1UL << (BITCOUNT - 1)) - 1UL)
| (1UL << (BITCOUNT - 1)) ;
REVERSE = Reverse; // Before set this property
// mask must be calculated
}
//---------------------------------------------------------------------------
void __fastcall Crc::SetReverse(bool reverse)
{
FREVERSE = reverse;
if (reverse) {
for (unsigned int ii = 0 ; ii < MAXBYTEVALUES ; ++ ii)
values [ii] = ReverseTableEntry (ii) ;
}
else {
for (unsigned int ii = 0 ; ii < MAXBYTEVALUES ; ++ ii)
values [ii] = ForwardTableEntry (ii) ;
}}
//---------------------------------------------------------------------------
BYTE4 __fastcall Crc::Reverse (UBYTE4 value)
// This function returns the reversed bit patter from its input.
// For example, 1010 becomes 0101.
//
// Parameters:
//
// value: The value to reverse
{
unsigned long result = 0 ; for (unsigned int jj = 0 ; jj < BITCOUNT ; ++ jj)
{
if ((value & bits [jj]) != 0)
result |= bits [BITCOUNT - jj - 1] ;
}
return result ;
}
//---------------------------------------------------------------------------
UBYTE4 __fastcall Crc::ForwardTableEntry (unsigned int entryindex)
// This function creates a CRC table entry for a non-reversed
// CRC function.
//
// Parameters:
//
// entryindex: The index of the CRC table entry.
//
// Return Value:
//
// The value for the specified CRC table entry.
//
{ UBYTE4 result = entryindex << (BITCOUNT - BITSPERBYTE) ;
for (unsigned int ii = 0 ; ii < BITSPERBYTE ; ++ ii)
{
if ((result & bits [BITCOUNT - 1]) == 0)
result <<= 1 ;
else
result = (result << 1) ^ POLYNOMINAL ;
}
result &= mask ;
return result ;
}
//---------------------------------------------------------------------------
UBYTE4 __fastcall Crc::ReverseTableEntry (unsigned int entryindex)
// This function creates a CRC table entry for a reversed
// CRC function.
//
// Parameters:
//
// entryindex: The index of the CRC table entry.
//
// Return Value:
//
// The value for the specified CRC table entry.
//
{
UBYTE4 result = entryindex ;
for (unsigned int ii = 0 ; ii < BITSPERBYTE ; ++ ii)
{
if ((result & 1) == 0)
result >>= 1 ;
else
result = (result >> 1) ^ Reverse (POLYNOMINAL) ;
}
result &= mask ;
return result ;
}
//---------------------------------------------------------------------------
void __fastcall Crc::reset (void)
{
crc_register = INITIAL ;
}
//---------------------------------------------------------------------------
UBYTE4 __fastcall Crc::value (void) const
{
UBYTE4 result = crc_register ^ FINALMASK ;
result &= mask ;
return result ;
}
//---------------------------------------------------------------------------
void __fastcall Crc::update (const char *buffer, unsigned int length)
// This function updates the value of the CRC register based upon
// the contents of a buffer.
//
// Parameters:
//
// buffer: The input buffer
// length: The length of the input buffer.
//
{
// The process for updating depends upon whether or not we are using
// the reversed CRC form.
if (REVERSE)
{
for (unsigned int ii = 0 ; ii < length ; ++ ii)
{
crc_register = values [(crc_register ^ buffer [ii]) & 0xFF]
^ (crc_register >> 8) ;
}
}
else
{
for (unsigned int ii = 0 ; ii < length ; ++ ii)
{
UBYTE4 index = ((crc_register >> (BITCOUNT - BITSPERBYTE)) ^ buffer [ii]) ;
crc_register = values [index & 0xFF]
^ (crc_register << BITSPERBYTE) ;
}
}
return ;
}
//---------------------------------------------------------------------------
UBYTE4 __fastcall Crc::getcrc(const char *buffer, unsigned int length)
{
reset();
update(buffer,length);
return value();
}
//---------------------------------------------------------------------------