/*
=============== Note ================
* CRC란?
> 데이터 검증 시 사용.
> 유니한 숫자를 생성해 값을 비교하여 서로의 데이터를 검증할 때 사용한다.
> 현 코드는 16비트 CCITT
> 관련 : MD2 ( CRC의 확장 ), SHA-1 ( MD2의 확장 )
*/
// The CRC parameters. Currently configured for CCITT.
// Simply modify these to switch to another CRC standard.
#define POLYNOMIAL 0x1021
#define INITIAL_REMAINDER 0xFFFF
#define FINAL_XOR_VALUE 0x0000
// The width of the CRC calculation and result.
// Modify the typedef for an 8 or 32-bit CRC standard.
typedef unsigned short width;
#define WIDTH ( 8 *sizeof( width ) )
#define TOPBIT ( 1 <<( WIDTH -1 ) )
// An array containing the pre-computed intermediate result for each possible byte of input.
// This is used to speed up the computation.
width g_wCrcTable[256];
void crcInit();
width crcCompute( unsigned char *szMessage, unsigned int nBytes );
// ------------------------------- cpp -----------------------------------
#include <stdio.h>
/***************************************************************************
* Function : crcInit()
*
* Description : Initialize the CRC lookup table. This table is used by
crcCompute() to make CRC computation faster.
* Notes : The mod-2 binary long division is implemented here.
* Returns : None defined.
***************************************************************************/
void crcInit()
{
width wRemainder = 0;
width wDividend = 0;
long int nBit = 0;
// Perform binary long division, a bit at a time.
for( wDividend = 0; wDividend < 256; wDividend++ )
{
// Initialize the remainder.
wRemainder = wDividend <<( WIDTH -8 );
// Shift and XOR with the polynomial.
for( nBit = 0; nBit < 8; nBit++ )
{
// Try to divide the current data bit.
if( wRemainder & TOPBIT ) wRemainder = ( wRemainder <<1 ) ^POLYNOMIAL;
else wRemainder = wRemainder <<1;
}
// Save the result in the table.
g_wCrcTable[wDividend] = wRemainder;
}
return;
}
/***************************************************************************
* Function : crcCompute()
*
* Description : Compute the CRC checksum of a binary message block.
* Notes : This function expects that crcInit() has been called
* first to initialize the CRC lookup table.
* Returns : The CRC of the data.
***************************************************************************/
width crcCompute( unsigned char *szMessage, unsigned int nBytes )
{
unsigned int nOffSet = 0;
unsigned char cByte = 0;
width wRemainder = INITIAL_REMAINDER;
// Divide the message by the polynomial, a byte at time.
for( nOffSet = 0; nOffSet < nBytes; nOffSet++ )
{
cByte = ( wRemainder >>( WIDTH -8 ) ) ^szMessage[nOffSet];
wRemainder = g_wCrcTable[cByte] ^( wRemainder <<8 );
}
// The final remainder is the CRC result.
return ( wRemainder ^FINAL_XOR_VALUE );
}
void main()
{
// ============================= TEST START ===================================
unsigned char cData = 11;
width wData = 0;
long int nDataSize = sizeof( cData );
printf( "=============== TEST CRC ================ \n\n" );
printf( " TEST cData = %d \n wData = %d \n cDataSize = %d \n\n", cData, wData, nDataSize );
crcInit();
wData = crcCompute( &cData, nDataSize );
printf( "crcCompute : \n");
printf( " TEST cData = %d \n wData = %d \n cDataSize = %d \n\n", cData, wData, nDataSize );
// ============================= TEST END ===================================
// ============================= TEST2 START ==================== ===============
unsigned char cData2 = 10;
width wData2 = 0;
long int nDataSize2 = sizeof( cData2 );
printf( "=============== TEST2 CRC ================ \n\n" );
printf( " TEST cData2 = %d \n wData2 = %d \n cDataSize2 = %d \n\n", cData2, wData2, nDataSize2 );
crcInit();
wData2 = crcCompute( &cData2, nDataSize2 );
printf( "crcCompute : \n");
printf( " TEST2 cData2 = %d \n wData2 = %d \n cDataSize2 = %d \n\n", cData2, wData2, nDataSize2 );
// ============================= TEST2 END ===================================
}
'[ Programing ] > Algorithm' 카테고리의 다른 글
[ 흔들림 ] Shake (0) | 2010.02.01 |
---|---|
Singleton은 왠만하면 동적 할당으로 사용하자. (0) | 2010.01.31 |
Sington 정리. ( 동적 할당 + friend + atexit() ) (0) | 2010.01.12 |
Sington은 Friend로 사용하여 간단하게 만들자. (0) | 2009.12.28 |
[ 가속도 ] ( fAfter -fBefore ) /fTime; (0) | 2009.12.24 |