HAMMING CODE decoding:  

Posted in

#include"stdio.h"
#include"stdlib.h"
#include"math.h"

int hmatrix[3][7]={1,0,0,0,1,1,1,0,1,0,1,0,1,1,0,0,1,1,1,0,1};
int edata[7];
int syndrome[3];
int errdig;

int main()
{
int i,j;
system("clear");
printf("enter the encoded bits\n");
for(i=0;i<7;i++)
scanf("%d",&edata[i]);
for(i=0;i<3;i++)
for(j=0;j<7;j++)
syndrome[i]=syndrome[i]+edata[j]*hmatrix[i][j];
for(i=0;i<3;i++)
syndrome[i]=syndrome[i]%2;
errdig=4*syndrome[0]+2*syndrome[1]+1*syndrome[2];
if(0==errdig)
printf("error free data");
else
{
printf("error in bit number %d--%d\n",errdig,edata[errdig]);
errdig--;
if(1==edata[errdig])
edata[errdig]=0;
else
edata[errdig]=1;
}
for(i=3;i<7;i++)
printf("%d",edata[i]);
puts(" ");
}

Hamming Code-Encoding  

Posted in

#include"stdio.h"
#include"math.h"
#include"stdlib.h"

char d1[5],d2[5],d3[5],d4[5];
char p1[5],p2[5],p3[5],p4[5];
char data[5];
char gmatrix[4][8];
int encoded[8];
int con(char x);

int main()
{
int i,j;
system("clear");
printf("enter 4 data bits\n");
scanf("%s",data);
for(i=0;i<4;i++)
{
d1[i]=d2[i]=d3[i]=d4[i]='0';
p1[i]=p2[i]=p3[i]='1';
}
printf("--------------------------------------------------------\n");
d1[0]='1';
d2[1]='1';
d3[2]='1';
d4[3]='1';
p1[0]='0';
p2[1]='0';
p3[2]='0';
printf("%s\n",d1);
printf("%s\n",d2);
printf("%s\n",d3);
printf("%s\n",d4);
printf("%s\n",p1);
printf("%s\n",p2);
printf("%s\n",p3);

for(i=0;i<4;i++)
gmatrix[i][0]=p1[i];
for(i=0;i<4;i++)
gmatrix[i][1]=p2[i];
for(i=0;i<4;i++)
gmatrix[i][2]=p3[i];
for(i=0;i<4;i++)
gmatrix[i][3]=d1[i];
for(i=0;i<4;i++)
gmatrix[i][4]=d2[i];
for(i=0;i<4;i++)
gmatrix[i][5]=d3[i];
for(i=0;i<4;i++)
gmatrix[i][6]=d4[i];
printf("\n generator matrix\n");
for(i=0;i<4;i++)
printf("%s\n",gmatrix[i]);
/**ENCODING**/

for(i=0;i<7;i++)
for(j=0;j<4;j++)
encoded[i]=encoded[i]+con(data[j])*con(gmatrix[j][i]);
puts("encoded");

for(i=0;i<7;i++)
{
encoded[i]=encoded[i]%2;
printf("%i",encoded[i]);
}
puts(" ");
return 0;
}
int con(char x)
{
if(x=='1')
return 1;
else
return 0;
}

Leaky Bucket  

Posted in

#include"stdio.h"
#include"stdlib.h"
#include"math.h"

int t_rand(int a)
{
int m;
m=rand()%10;
m=m%a;
if(m==0)
m=1;
return m;
}

void main()
{
int o_rate,bufsize,i,maxtime,presentsize=0,rate;
clrscr();
printf("enter the buffer size,output rate and max time for transaction");
scanf("%d%d",&bufsize,&o_rate);
rate=o_rate;
maxtime=t_rand(4)*10;
for(i=0;ipresentsize)
o_rate=presentsize;
presentsize-=o_rate;
printf("the present size of bucket is %d after removing %d bytes\n\n",presentsize,o_rate);
}
getch();
}

RSA  

Posted in

#include"stdio.h"
#include"math.h"
#include"conio.h"

double min(double x,double y)
{
return(xy ? x:y);
}

double gcd(double x,double y)
{
if(x==y)
return x;
else
return(gcd(min(x,y),max(x,y)-min(x,y)));
}

/*long double dbin(long double n)
{
long double i=0,s=0,v,b;
do
{
s+=pow(10,i)*(fmod((int)n,2));
i++;
n=n/2;
}while(n>=1);
return(s);
} */

long double modexp(long double a,long double x,long double n)
{
long double r=1;
while(x>0)
{
if((int)(fmodl(x,2))==1)
r=fmodl((r*a),n);
a=fmodl((a*a),n);
x/=2;
}
return(r);

/* r=fmodl(pow(a,x),n);
return r;*/
}

void main()
{
long double p,q,phi,n,e,d;
long double ms,es,ds,a[100];
int ch,i;
char s[100],c,m;
clrscr();
do
{
printf("\nEnter prime numbers\n");
scanf("%Lf%Lf",&p,&q);
printf("%Lf %Lf",p,q);
}while(p==q);
n=p*q;
phi=(p-1)*(q-1);
do
{
printf("\nEnter prime value of e\n");
scanf("%Lf",&e);
printf("%Lf",e);
}while((gcd(e,phi)!=1) && e>phi);
for(d=1;d if(fmod((e*d),phi)==1)
break;
printf("\nD within main=%Lf",d);
do
{
printf("\nEnter your choice\n1:Numbers\n2:Characters\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\nEnter the message[numbers]:\n");
scanf("%Lf",&ms);
printf("%Lf",ms);
es=modexp(ms,e,n);
ds=modexp(es,d,n);
printf("\n\nOriginal message:%Lf\n",ms);
printf("\nEncrypted message:%Lf\n",es);
printf("\nDecrypted message:%Lf\n",ds);
break;

case 2:printf("Enter message[characters]\n");
fflush(stdin);
gets(s);
i=0;
while((c=s[i])!='\0')
{
c=c-'a';
c++;
a[i]=c;
es=modexp(a[i],e,n);
ds=modexp(es,d,n);
printf("\nOriginal message=%c",c+'a'-1);//((int)a[i]+'a'-1));
printf("\nEncrypted message=%Lf",es);
printf("\nDecrypted message=%c",((int)ds+'a'-1));
i++;
}
break;
default:break;
}
}while(ch<=2);
getch();
}

Dijikrstra's Algo  

Posted in

#include"stdio.h"
#define INFINITY 999

void dijkstra(int wrt[10][10],int n,int source,int distance[10])
{
int visited[10],min,u,i,j;
for(i=1;i<=n;i++)
{
distance[i]=wrt[source][i];
visited[i]=0;
}
visited[source]=1;
for(i=1;i<=n;i++)
{
min=INFINITY;
u=-1;
for(j=1;j<=n;j++)
if(visited[j]==0 && distance[j] {
min=distance[j];
u=j;
}
visited[u]=1;
for(j=1;j<=n;j++)
if(visited[j]==0 && distance[u]+wrt[u][j] {
distance[j]=distance[u]+wrt[u][j];
}
}
}

int main()
{
int wrt[10][10],n,source,distance[10];
int i,j,sum;
printf("enter the nodes\n");
scanf("%d",&n);
printf("write matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&wrt[i][j]);
printf("enter the source\n");
scanf("%d\n",&source);
dijkstra(wrt,n,source,distance);
for(i=1;i<=n;i++)
if(distance[i]!=999)
printf("the shortest distance from %d to %d is %d\n",source,i,distance[i]);
else
printf("not reachable\n");
}

FIFO CLIENT  

Posted in

#include"sys/types.h"
#include"sys/stat.h"
#include"stdio.h"
#include"stdlib.h"
#include"fcntl.h"

#define FIFO1 "fifo1"
#define FIFO2 "fifo2"

void main()
{
FILE *fp;
char buff[2500],fn[100],c;
int rd,wd,i=0;
mknod(FIFO1,S_IFIFO|0666,0);
mknod(FIFO2,S_IFIFO|0666,0);
wd=open(FIFO1,O_WRONLY);
printf("enter filename");
scanf("%s",fn);
write(wd,fn,sizeof(fn));
rd=open(FIFO2,O_RDONLY);
read(rd,&i,sizeof(i));
//next two lines added
if(i==0)
printf("file doesnt exist\n");
else
{
read(rd,buff,i);
write(1,buff,i);
}
unlink(FIFO1);
unlink(FIFO2);
}

FIFO SERVER  

Posted in

FIFO
server:

#include"sys/types.h"
#include"sys/stat.h"
#include"stdio.h"
#include"stdlib.h"
#include"fcntl.h"

#define FIFO1 "fifo1"
#define FIFO2 "fifo2"

void main()
{
FILE *fp;
char buff[2500],fn[100],c;
int rd,wd,i=0;
mknod(FIFO1,S_IFIFO|0666,0);
mknod(FIFO2,S_IFIFO|0666,0);
rd=open(FIFO1,O_RDONLY);
read(rd,&fn,sizeof(fn));
wd=open(FIFO2,O_WRONLY);
fp=fopen(fn,"r");
if(fp==NULL)
{
printf("file cant be created\n");
//next two lines r newly added
i=0;
write(wd,&i,sizeof(i));
exit(0);
}
while((c=fgetc(fp))!=EOF)
{
buff[i]=c;
i++;
}
write(wd,&i,sizeof(i));
write(wd,buff,i);
unlink(FIFO1);
unlink(FIFO2);
}

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.