CRC Program  

Posted in

#include"stdio.h"
#include"string.h"
#define poly 0x1021
char original[1000];
char verified[1000];
unsigned short original_crc;
unsigned short verified_crc;
unsigned short original_text_length;
unsigned short verified_text_length;
int main()
{
void update_original_crc(unsigned short);
void update_verified_crc(unsigned short);
void augment_original_crc();
void augment_verified_crc();
unsigned short ch,i=0;
original_crc=0xffff;
verified_crc=0xffff;
original_text_length=0;
verified_text_length=0;
printf("enter original text:\n");
scanf("%s",original);
printf("enter verified text:\n");
scanf("%s",verified);
while((ch=original[i])!=0)
{
update_original_crc(ch);
i++;
original_text_length++;
}
i=0;
while((ch=verified[i])!=0)
{
update_verified_crc(ch);
i++;
verified_text_length++;
}
augment_original_crc();
augment_verified_crc();
printf("\n crc original text=%04x\n",original_crc);
printf("length of originaltext=%u\n",original_text_length);
printf("\n crc verified text=%04x\n",verified_crc);
printf("length of verified text=%u\n",verified_text_length);
if(original_crc!=verified_crc)
printf("data(crc)mismatched!\n");
else
printf("data(crc)matched!\n");
}
void update_original_crc(unsigned short ch)
{
unsigned short i,v,xor_flag;
v=0x80;
for(i=0;i<8;i++) { if(original_crc & 0x8000) { xor_flag=1; } else { xor_flag=0; } original_crc=original_crc<<1; if(ch & v) { original_crc=original_crc^poly; } if(xor_flag) { original_crc=original_crc^poly; } v=v>>1;
}
}

void update_verified_crc(unsigned short ch)
{
unsigned short i,v,xor_flag;
v=0x80;
for(i=0;i<8;i++) { if(verified_crc & 0x8000) { xor_flag=1; } else { xor_flag=0; } verified_crc=verified_crc<<1; if(ch & v) { verified_crc=verified_crc^poly; } if(xor_flag) { verified_crc=verified_crc^poly; } v=v>>1;
}
}



void augment_original_crc()
{
unsigned short i,xor_flag;
for(i=0;i<16;i++)
{
if(original_crc & 0x8000)
{
xor_flag=1;
}
else
{
xor_flag=0;
}
original_crc=original_crc<<1;
if(xor_flag)
{
original_crc=original_crc^poly;
}
}
}


void augment_verified_crc()
{
unsigned short i,xor_flag;
for(i=0;i<16;i++)
{
if(verified_crc & 0x8000)
{
xor_flag=1;
}
else
{
xor_flag=0;
}
verified_crc=verified_crc<<1;
if(xor_flag)
{
verified_crc=verified_crc^poly;
}
}
}

This entry was posted at 4:43 AM and is filed under . You can follow any responses to this entry through the comments feed .

0 comments

Post a Comment

Reverse Engineering :Subscribe Now

Zts - ZTS

DISCLAIMER

This Blog is ONLY for educational purposes,
and any such codes/snippets provided are to be executed on
your sole discretion. The author is not responsible for the codes.