일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- RFID
- Book
- hbase
- Software Engineering
- management
- psychology
- Spain
- Python
- history
- Java
- hadoop
- France
- comic agile
- programming_book
- Italy
- agile
- Malaysia
- web
- django
- Linux
- Book review
- Programming
- ubuntu
- program
- essay
- erlang
- Kuala Lumpur
- leadership
- QT
- MySQL
- Today
- Total
string의 crc 계산 본문
gcc 3.4.5 (mingw), Windows XP
#include <stdio.h>
// define to get CRC32 value
#define POLYNOMIAL_FOR_CRC32 (unsigned long)0xedb88320 // use in function makeCRCTable()
#define SIZE_OF_CRC32_TABLE 256
static unsigned long l_ulCRCtable[SIZE_OF_CRC32_TABLE];
#include "crc.h"
/**
CRC32 를 얻기위한 CRC 테이블을 작성한다(Make CRC table to get CRC32 value).
getCRC32() 에 의하여 사용되어지고, 사용자가 직접 사용하지는 않는다
(Called by function getCRC32(), not be used by user directly).
@author Kooknam Hong
@version 0.1
*/
static void makeCRCTable(void)
{
unsigned int i, j;
unsigned long h = 1;
l_ulCRCtable[0] = 0;
for ( i = 128; i ; i >>= 1 )
{
h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL_FOR_CRC32 : 0);
for ( j = 0; j < SIZE_OF_CRC32_TABLE; j += 2 * i )
l_ulCRCtable[i+j] = l_ulCRCtable[j] ^ h;
}
} // end of function makeCRCTable()
/**
주어진 buffer 로부터 CRC32 값을 얻는다(Get CRC value from given buffer).
주어진 buffer 에 대해서만 CRC32 값을 얻을 때는 첫번째 인자를 0으로 세팅하고
이전에 구해진 CRC32 값과 연계하여 CRC32 값을 얻을 경우에는 첫번째 인자에
이전에 구한 CRC32 값을 세팅한다.
@param p_ulCRC (input) 0 when get CRC value only using the given buffer,
the former CRC value when get CRC value related to that
@param p_cpBuffer (input) the given buffer to get CRC value
@param p_uiLen (input) the size of buffer
@return computed CRC value
@author Kooknam Hong
@version 0.1
*/
unsigned long getCRC32(unsigned long p_ulCRC, char const *p_cpBuffer, unsigned int p_uiLen)
{
if (l_ulCRCtable[SIZE_OF_CRC32_TABLE - 1] == 0)
makeCRCTable();
p_ulCRC ^= 0xffffffff;
while (p_uiLen--)
{
p_ulCRC = (p_ulCRC >> 8) ^ l_ulCRCtable[(p_ulCRC ^ *p_cpBuffer++) & 0xff];
}
return p_ulCRC ^ 0xffffffff;
} // end of function getCRC32()
#include "crc.h"
int main()
{
char z_cArrBuff[] = "abcdefg";
printf("%s's CRC32 : %lu\n", z_cArrBuff, getCRC32(0, (char*)z_cArrBuff, sizeof(z_cArrBuff)));
return 0;
}