C언어로 구현한 BIT 단위 연산을 통해 CRC-5, CRC-7, CRC-8, CRC-10, CRC-16, CRC-32 를 계산하는 함수.
삽질해 가면서 일단 만들어 본 CRC-5, CRC-7, CRC-8, CRC-10, CRC-16, CRC-32 계산 함수. 가장 기본적인 계산이며, 바이트 확장과 테이블 참조 방식은 더 머리를 써 보아야 겠다. 라이브러리를 가져다 쓰면 끝이겠지만, 일단은 내가 쓰고 있는지 뭔지는 알아야 할 것이다.
CRC-5 계산함수
// SAPJIL CRC Calcurator by eqmaker
unsigned char CRC5_Generator(unsigned char *datastream, unsigned int numberofbyte, unsigned char poly)
{
unsigned char rtn=0;
for (unsigned int i=0; i<numberofbyte+1; i++)
{
for (char j= ((i==numberofbyte) ? 4 : 7); j>=0; j--)
{
if (rtn & 1<<4)
{
rtn = rtn << 1;
rtn = (((i==numberofbyte) ? 0 : datastream[i]) & (1<<j)) ? (rtn | 1) : rtn;
rtn = rtn ^ poly;
}
else
{
rtn = rtn << 1;
rtn = (((i==numberofbyte) ? 0 : datastream[i]) & (1<<j)) ? (rtn | 1) : rtn;
}
}
}
return rtn & 0b00011111;
}
CRC-7 계산함수
// SAPJIL CRC Calcurator by eqmaker
unsigned char CRC7_Generator(unsigned char *datastream, unsigned int numberofbyte, unsigned char poly)
{
unsigned char rtn=0;
for (unsigned int i=0; i<numberofbyte+1; i++)
{
for (char j= ((i==numberofbyte) ? 6 : 7); j>=0; j--)
{
if (rtn & 1<<6)
{
rtn = rtn << 1;
rtn = (((i==numberofbyte) ? 0 : datastream[i]) & (1<<j)) ? (rtn | 1) : rtn;
rtn = rtn ^ poly;
}
else
{
rtn = rtn << 1;
rtn = (((i==numberofbyte) ? 0 : datastream[i]) & (1<<j)) ? (rtn | 1) : rtn;
}
}
}
return rtn & 0b01111111;
}
CRC-8 계산함수
// SAPJIL CRC Calcurator by eqmaker
unsigned char CRC8_Generator(unsigned char *datastream, unsigned int numberofbyte, unsigned char poly)
{
unsigned char rtn=0x00;
for (unsigned int i=0; i<numberofbyte+1; i++)
{
for (char j=7; j>=0; j--)
{
if (rtn & 0x80)
{
rtn = rtn << 1;
rtn = (((i==numberofbyte) ? 0 : datastream[i]) & (1<<j)) ? (rtn | 1) : rtn;
rtn = rtn ^ poly;
}
else
{
rtn = rtn << 1;
rtn = (((i==numberofbyte) ? 0 : datastream[i]) & (1<<j)) ? (rtn | 1) : rtn;
}
}
}
return (rtn);
}
CRC-10 계산함수
// SAPJIL CRC Calcurator by eqmaker
void CRC10_Generator(unsigned char *datastream, unsigned int numberofbyte, unsigned int poly)
{
unsigned int rtn=0;
for (unsigned int i=0; i<numberofbyte+2; i++) // 8bit x 2 = 16bit 나머지 (중 10bit 사용)
{
for (char j=((i==numberofbyte+1) ? 1 : 7); j>=0; j--) // 15-6 14-5 13-4 12-3 11-2 10-1 9-0
{
if (rtn & 1<<9)
{
rtn = rtn << 1;
rtn = (((i>=numberofbyte) ? 0 : datastream[i]) & (1<<j)) ? (rtn | 1) : rtn;
rtn = rtn ^ poly;
}
else
{
rtn = rtn << 1;
rtn = (((i>=numberofbyte) ? 0 : datastream[i]) & (1<<j)) ? (rtn | 1) : rtn;
}
}
}
rtn = rtn & 0x03ff;
printf("CRC10 : %04X\n", rtn);
}
CRC-16 계산함수
// SAPJIL CRC Calcurator by eqmaker
void CRC16_Generator(unsigned char *datastream, unsigned int numberofbyte, unsigned int poly)
{
unsigned int rtn=0;
for (unsigned int i=0; i<numberofbyte+2; i++) // 8bit x 2 = 16bit 나머지
{
for (char j=7; j>=0; j--)
{
if (rtn & 0x8000)
{
rtn = rtn << 1;
rtn = (((i>=numberofbyte) ? 0 : datastream[i]) & (1<<j)) ? (rtn | 1) : rtn;
rtn = rtn ^ poly;
}
else
{
rtn = rtn << 1;
rtn = (((i>=numberofbyte) ? 0 : datastream[i]) & (1<<j)) ? (rtn | 1) : rtn;
}
}
}
rtn = rtn & 0xffff;
printf("CRC16 : %04X\n", rtn);
}
CRC-32 계산함수
// SAPJIL CRC Calcurator by eqmaker
void CRC32_Generator(unsigned char *datastream, unsigned int numberofbyte, unsigned int poly)
{
unsigned int rtn=0;
for (unsigned int i=0; i<numberofbyte+4; i++) // 8bit x 4 = 32bit
{
for (char j=((i==numberofbyte+1) ? 7 : 7); j>=0; j--)
{
if (rtn & 1<<31)
{
rtn = rtn << 1;
rtn = (((i>=numberofbyte) ? 0 : datastream[i]) & (1<<j)) ? (rtn | 1) : rtn;
rtn = rtn ^ poly;
}
else
{
rtn = rtn << 1;
rtn = (((i>=numberofbyte) ? 0 : datastream[i]) & (1<<j)) ? (rtn | 1) : rtn;
}
}
}
printf("CRC32 : %08X\n", rtn);
}
최초 작성 2021. 12. 20. 16:30 / 수정 2024. 12. 06.