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

Socket server program  

Posted in

#include "netinet/in.h"
#include "unistd.h"
#include "stdio.h"
#include "sys/types.h"
#include "sys/socket.h"
#include "stdlib.h"
#include "arpa/inet.h"
#define IP "127.0.0.1"
#define PORT 6010

int main()
{
int sd,ad,bd,i=0,j,ss;
char fn[256],buf[5000],c;
FILE *fp;
struct sockaddr_in server,client;
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
printf("\nSocket Error");
server.sin_addr.s_addr=inet_addr(IP);
server.sin_port=htons(PORT);
server.sin_family=AF_INET;
if((bd=bind(sd,(struct sockaddr *)&server,sizeof(server)))<0)
printf("\nBind Error");
listen(sd,5);
ss=sizeof(client);
if((ad=accept(sd,(struct sockaddr *)&client,&ss))<0)
printf("\nAccept Error");
read(ad,&fn,sizeof(fn));
fp=fopen(fn,"r");
if(fp==NULL)
{
printf("\nFile doesnot exist");
write(ad,&i,sizeof(i));
exit(0);
}
while((c=fgetc(fp))!=EOF)
{
buf[i]=c;
i++;
}
write(ad,&i,sizeof(i));
for(j=0;j write(ad,&buf[j],sizeof(buf[j]));
fclose(fp);
close(sd);
close(bd);
close(ad);
return 0;
}

Socket client program  

Posted in

#include "netinet/in.h"
#include "unistd.h"
#include "stdio.h"
#include "sys/types.h"
#include "sys/socket.h"
#include "stdlib.h"
#include "arpa/inet.h"
#define IP "127.0.0.1"
#define PORT 6010

int main()
{
int sd,cd,i=0,n;
char fn[256],buf[5000];
struct sockaddr_in server;
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
printf("\nSocket Error");
server.sin_addr.s_addr=inet_addr(IP);
server.sin_port=htons(PORT);
server.sin_family=AF_INET;
if((cd=connect(sd,(struct sockaddr *)&server,sizeof(server)))<0)
printf("\nConnect Error");
printf("\nEnter filename= ");
scanf("%s",fn);
write(sd,&fn,sizeof(fn));
read(sd,&n,sizeof(n));
// next two lines r newly added with else
if(n==0)
printf("file doesnt exist\n");
else{
printf("%d",n);
for(i=0;i {
read(sd,&buf[i],sizeof(char));
}
for(i=0;i {
write(1,&buf[i],sizeof(char));
// printf("%c",buf[i]);
}
}
close(sd);
close(cd);
return 0;
}

Distance Vector  

Posted in

#include"stdio.h"
#include"stdlib.h"
#define nul 1000
#define nodes 10
int no;

struct node
{
int a[nodes][4];
}router[nodes];

void init(int r)
{
int i;
for(i=1;i<=no;i++) { router[r].a[i][1]=i; router[r].a[i][2]=999; router[r].a[i][3]=nul; } router[r].a[r][2]=0; router[r].a[r][3]=r; } void inp(int r) { int i; printf("Enter distance from the node %d to other nodes\n",r); printf("Enter 999 if there is no direct route\n"); for(i=1;i<=no;i++) { if(i!=r) { printf("Enter distance to node %d\n",i); scanf("%d",&router[r].a[i][2]); router[r].a[i][3]=i; } } } void display(int r) { int i; printf("The routing table for node %d is as follows :\n",r); for(i=1;i<=no;i++) { if(router[r].a[i][2]>=999)
printf("\t\t %d \t no link \t no hop\n",router[r].a[i][1]);
else
printf("\t\t %d \t %d \t\t %d\n",router[r].a[i][1],router[r].a[i][2],router[r].a[i][3]);
}
}

void dv_algo(int r)
{
int i,j,z;
for(i=1;i<=no;i++) { if(router[r].a[i][2]!=999 && router[r].a[i][2]!=0) { for(j=1;j<=no;j++) { z=router[r].a[i][2]+router[i].a[j][2]; if(router[r].a[j][2]>z)
{
router[r].a[j][2]=z;
router[r].a[j][3]=i;
}
}
}
}
}

int main()
{
int i,j,x,y;
char choice;
printf("Enter the number of nodes required\n");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
init(i);
inp(i);
}
printf("The configuration of the nodes after initialization is as follows :\n");
for(i=1;i<=no;i++)
display(i);
for(i=1;i<=no;i++)
dv_algo(i);
printf("The configuration of the nodes after computation of path is as follows:\n");
for(i=1;i<=no;i++)
display(i);
while(1)
{
printf("Continue(y/n)\n");
scanf("%s",&choice);
if(choice=='n')
break;
printf("Enter the nodes between which shortest path is to be found\n");
scanf("%d%d",&x,&y);
printf("The length of the shortest path is %d\n",router[x].a[y][2]);
}
return 0;
}

Frame Sorting  

Posted in

#include "stdio.h"
struct frame
{
int fslno;
char finfo[20];
};
struct frame arr[10];
int n;
void sort()
{
int i,j,ex;
struct frame temp;
for(i=0;i<=n;i++) { ex=0; for(j=0;jarr[j+1].fslno)
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
ex++;
}
if(ex==0)break;
}
}
int main()
{
int i;

printf("Enter the no of frames");
scanf("%d",&n);
printf("Enter the frame sequence and contents\n");
for(i=0;i scanf("%d%s",&arr[i].fslno,&arr[i].finfo);
sort();
printf("The frame in sequence\n");
for(i=0;i printf("Sequence No:\t%d\t%s\t\n",arr[i].fslno,arr[i].finfo);
return 0;
}

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

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.