crc아시는분 코드점 봐주세여
큰나래
질문 제목 : crc16비트 알고리즘crc16비트 주석 헬프미질문 내용 : crc16비트 코딩한거 자료를 구햇는데 이해가 잘안가네요 ㅜ
crc16 나눗셈 과정은 알겠는데 아래코드에서 어떻게 수행되는건지 주석좀 달아주세여ㅜ#include stdio.h
#include string.h
#define poly 0x11021 /* crc-ccitt mask */
/* global variables */
char text[1000];
unsigned short good_crc;
unsigned short bad_crc;
unsigned short text_length;
void go();
void repeat_character(unsigned char, unsigned short);
void update_good_crc(unsigned short);
void augment_message_for_good_crc();
void update_bad_crc(unsigned short);
int main(void)
{
sprintf(text, %s, );
go();
sprintf(text, %s, a);
go();
sprintf(text, %s, 123456789);
go();
repeat_character(65, 256);
go();
return 0;
}
void go(void)
{
unsigned short ch, i;
good_crc = 0xffff;
bad_crc = 0xffff;
i = 0;
text_length= 0;
while((ch=text[i])!=0)
{
update_good_crc(ch);
update_bad_crc(ch);
i++;
text_length++;
}
augment_message_for_good_crc();
printf(
\ngood_crc = %04x, bad_crc = %04x, length = %u, text = \%s\,
good_crc, bad_crc, text_length, text
);
}
void repeat_character(unsigned char ch, unsigned short n)
{
unsigned short i;
for (i=0; in; i++)
{
text[i] = ch;
}
text[n] = 0;
}
void update_good_crc(unsigned short ch)
{
unsigned short i, v, xor_flag;
/*
align test bit with leftmost bit of the message byte.
*/
v = 0x80;
for (i=0; i8; i++)
{
if (good_crc & 0x8000)
{
xor_flag= 1;
}
else
{
xor_flag= 0;
}
good_crc = good_crc 1;
if (ch & v)
{
/*
append next bit of message to end of crc if it is not zero.
the zero bit placed there by the shift above need not be
changed if the next bit of the message is zero.
*/
good_crc= good_crc + 1;
}
if (xor_flag)
{
good_crc = good_crc ^ poly;
}
/*
align test bit with next bit of the message byte.
*/
v = v 1;
}
}
void augment_message_for_good_crc()
{
unsigned short i, xor_flag;
for (i=0; i16; i++)
{
if (good_crc & 0x8000)
{
xor_flag= 1;
}
else
{
xor_flag= 0;
}
good_crc = good_crc 1;
if (xor_flag)
{
good_crc = good_crc ^ polrc ^ poly;
}
}
}
void update_bad_crc(unsigned short ch)
{
/* based on code found at
http://www.programmingparadise.com/utility/crc.html
*/
unsigned short i, xor_flag;
/*
why are they shifting this byte left by 8 bits??
how do the low bits of the poly ever see it?
*/
ch=8;
for(i=0; i8; i++)
{
if ((bad_crc ^ ch) & 0x8000)
{
xor_flag = 1;
}
else
{
xor_flag = 0;
}
bad_crc = bad_crc 1;
if (xor_flag)
{
bad_crc = bad_crc ^ poly;
}
ch = ch 1;
}
}