postheadericon Cyclic Redundancy Check - CRC


Sender / CRC Genretor



//CRC-Cyclic Redundancy Check Code

#include<stdio.h>
#include<string.h>
#include<fcntl.h>
void main()
{
int i,j,divisor_len,dividend_len,pid;
char dividend[100],dataword[100], divisor[30],temp[30],quotient[100],remainder[30],divisor1[30];

system("clear");

system(">pipe");
pid=open("pipe",O_WRONLY);

printf("Enter dividend: ");
scanf("%s",dataword);
strcpy(dividend,dataword);

printf("Enter divisor : ");
scanf("%s",divisor);
write(pid,&divisor,sizeof(divisor));
//calculate length of data  
divisor_len=strlen(divisor);

dividend_len=strlen(dividend);

//create backup of original divisor
strcpy(divisor1,divisor);

//left shift rawbits in dividend with divisor_len-1
for(i=0;i<divisor_len-1;i++)
{
dividend[dividend_len+i]='0';
}

//copy original dividend(without padding bits) into temp
for(i=0;i<divisor_len;i++)
{
temp[i]=dividend[i];
}

for(i=0;i<dividend_len;i++)
{
quotient[i]=temp[0];
//select quitient bit ZERO if first bit of current dividend is ZERO otherwise ONE
if(quotient[i]=='0')
{
//if ZERO selected Divisor became all ZERO bits
for(j=0;j<divisor_len;j++)
{
divisor[j]='0';
}
    }
else
{
//if ONE is selected divide with Original divisor
for(j=0;j<divisor_len;j++)
{
divisor[j]=divisor1[j];
}
}
for(j=divisor_len-1;j>0;j--)
{
//do the process of XOR and get Reminder
    if(temp[j]==divisor[j])
{
//temp[j]==divisor[j] : Reminder
// 0 == 0 : 0
// 1 == 1 : 1
    remainder[j-1]='0';
}
    else
{
//temp[j]==divisor[j] : Reminder
// 0 == 1 : 1
// 1 == 0 : 1

    remainder[j-1]='1';
}
}
remainder[divisor_len-1]=dividend[i+divisor_len];
strcpy(temp,remainder);
}
for(i=0;i<divisor_len-1;i++)
{
remainder[i]=temp[i];
}
remainder[i]='\0';

system("clear");
printf("\n[%d] - Dataword : %s",strlen(dataword),dataword);
printf("\n[%d] - Divisor  : %s",strlen(divisor1),divisor1);
printf("\n[%d] - CRC      : %s",strlen(remainder),remainder);
strcat(dataword,remainder);
printf("\n[%d] - Codeword : %s\n",strlen(dataword),dataword);
write(pid,&dataword,sizeof(dataword));
close(pid);

}






Receiver / CRC Receiver



//CRC-Cyclic Redundancy Check [reciver]
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
void main()
{
int i,j,divisor_len,dividend_len,pid,flag=0;
char dividend[100],dataword[100], divisor[30],temp[30],quotient[100],remainder[30],divisor1[30];

system("clear");
pid=open("pipe",O_RDONLY);

read(pid,&divisor,sizeof(divisor));
read(pid,&dataword,sizeof(dataword));


strcpy(dividend,dataword);

//calculate length of data  
divisor_len=strlen(divisor);

dividend_len=strlen(dividend);//here we got length of CODEWORD=Data+CRC

dividend_len=dividend_len-divisor_len; // (Length_of_Data = CODEWORD-CRC) i.e. dividend_len
 
// printf("\n[%d] - dividend: %s",dividend_len,dividend);
// printf("\n[%d] - divisor : %s",divisor_len,divisor);

//create backup of original divisor
strcpy(divisor1,divisor);
strcpy(temp,dividend);

//NO Need of left shift rawbits in dividend with divisor_len-1 @ reciver side

//copy original dividend(without padding bits) into temp
for(i=0;i<divisor_len;i++)
{
temp[i]=dividend[i];
}
temp[i]='\0';


for(i=0;i<dividend_len;i++)
{
quotient[i]=temp[0];
//select quitient bit ZERO if first bit of current dividend is ZERO otherwise ONE
if(quotient[i]=='0')
{
//if ZERO selected Divisor became all ZERO bits
for(j=0;j<divisor_len;j++)
{
divisor[j]='0';
}
    }
else
{
//if ONE is selected divide with Original divisor
for(j=0;j<divisor_len;j++)
{
divisor[j]=divisor1[j];
}
}
for(j=divisor_len-1;j>0;j--)
{
//do the process of XOR and get Reminder
    if(temp[j]==divisor[j])
{
//temp[j]==divisor[j] : Reminder
// 0 == 0 : 0
// 1 == 1 : 1
    remainder[j-1]='0';
}
    else
{
//temp[j]==divisor[j] : Reminder
// 0 == 1 : 1
// 1 == 0 : 1

    remainder[j-1]='1';
}
}
remainder[divisor_len-1]=dividend[i+divisor_len];
strcpy(temp,remainder);
}
for(i=0;i<divisor_len-1;i++)
{
remainder[i]=temp[i];
if(remainder[i]!='0')
{
flag=1;
}
}

//if all bits of CRC are ZERO then ACCEPT data otherwise REJECT data
if(flag==1)
{
printf("\n\tError in Data...!\tData Rejected\n");
}
else
{
printf("\n\tData Accepted\n");
}
remainder[i]='\0';


printf("\nDataword : %s",dataword);
printf("\nDivisor  : %s",divisor1);
printf("\nCRC      : %s\n",remainder);

close(pid);
 }




Note : 
1. Enter row bits
2. Divisor must be less than the dividend
    Example :
                  Dividend   : 1011
                  Divisor      : 1001
    Ans. :
                  CRC         : 110
                  Codeword : 1011110



0 comments:

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.