Bitwise CRC 계산기

삽질해 가면서 일단 만들어 본 CRC5, CRC7, CRC8, CRC10, CRC16, CRC32 계산 함수. 바이트 확장과 테이블 참조는 아직 좀 더 머리를 싸 매야 할 것들...

//////////////////////////////////////////
// SAPJIL CRC Calcurator
// by eqmaker
//////////////////////////////////////////

#include <stdio.h>
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);
}

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;
}

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;
}

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);
}

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);
}

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);
}

CRC8 : 37
CRC7 : 4A
CRC5 : 1E
계속하려면 아무 키나 누르십시오 . . .

이 글은 2021. 12. 20. 16:30 작성 되었습니다.