Sie sind auf Seite 1von 34

List of Practicals and Projects to be completed

for
Network Security Lab
Code No. :
L
P
C
0
2
1

Network Security

Practical based on Paper:


Code No.:

BACHELOR OF TECHNOLOGY
(Computer Science and Engineering)
Sem-6th

HMR INSTITUTE OF TECHNOLOGY & MANAGEMENT


Hamidpur, Delhi 110 036
Affiliated to
GURU GOBIND SINGH INDRAPRASTHA UNIVERSITY
Dwarka, New Delhi

List of Practicals and Projects to be completed


For
B.Tech (CSE) 6th Sem. (3rd Year)
AIM-1

Write a program to convert decimal number into binary number in c


language.

AIM-2

Write a program to find GCD of given numbers in C language.

AIM-3

Write a program to find general and particular solution.

AIM-4

Write a program to implement Euclidean algorithm in C language.

Aim -5

Write a program to implement Extended Euclidean algorithm in C


language.

Aim-6

Write a program to create password and find whether given


password is strong or weak

Aim-7

Write a program to encrypt a message in C language.

Aim-8

Write a program to implement Additive, multiplicative and affine


cipher in C language

Aim-9

Write a program to find multiplicative inverse in C language.

Aim-10

Write a program to Implement Rail fence encryption in C language.

Aim-11

Write a program to Implement RSA algorithm in C language.

Aim-12

Write a program in C to implement DES algorithm in C language.

Aim-13

Write a program to Implement Huffman coding in C language.

(DAYANAND)
Asst. Prof. (CSE)
HMRITM, Hamidpur, Delhi-36
HOD

OBJECTIVE of NETWORK SECURITY LAB

1. The purpose of this course is to be able to explain, configure,


verify, and troubleshoot networks security problems at an
introductory level.
2. To learn various methods of encryption as well as decryption
through practical programming in order to implement in
information and network security.
3. To create password and categorized in strong or weak
password.
4. To verify that a computer network has been properly secured

AimWrite a program to convert decimal number into binary


number in c language.
Step:#include<conio.h>
#include<stdio.h>
int dectobin(int,int*);
void arrrev(int*,int);
void main()
{
clrscr();
int num1,num2,arr1[30],arr2[30],i,temp1,temp2,s1,s2;
printf("\nENTER FIRST DECIMAL NO.:");
scanf("%d",&num1);
printf("\nENTER SECOND DECIMAL NO.:");
scanf("%d",&num2);
s1=dectobin(num1,arr1);
s2=dectobin(num2,arr2);
printf("\n\nTHE BINARY EQUIVALENT OF %d IS:",num1);
for(i=0;i<s1;i++)
printf("%d",arr1[i]);
printf("\n\nTHE BINARY EQUIVALENT OF %d IS:",num2);
for(i=0;i<s2;i++)
printf("%d",arr2[i]);
getch();
}
int dectobin(int temp,int *arr)
{
int i=0,j,k;
while(temp!=0)
{
arr[i++]=temp%2;
temp=temp/2;
}
arrrev(arr,i);
return i;
}
void arrrev(int *arr1,int n)
{
int j,k,temp=0;
for(j=0,k=n-1;j<k;j++,k--)
{
temp=arr1[j];

arr1[j]=arr1[k];
arr1[k]=temp;
}
}

Aim:- Write a program to find GCD of given numbers in C


language.
Steps:#include<stdio.h>
#include<conio.h>
int gcd(int,int);
int n,mat[100],i,r,r1,r2,q,a,b,m;
void main()
{
clrscr();
printf("\n How many number u want to input to find the GCD=");
scanf("%d",&n);
printf("\n enter the numbers");
for(i=0;i<n;i++)
{
printf("\n mat[%d]:",i);
scanf("%d",&mat[i]);
}
for(i=0;i<n;i++)
printf("\n display mat[%d]:%d",i,mat[i]);
b=gcd(mat[0],mat[1]) ;
for(i=2;i<n;i++)
b=gcd(b,mat[i]) ;
printf("\n GCD of %d numbers=%d",n,b);
getch();
}
int gcd(int a,int b)
{
r1=a;r2=b;
while(r2>0)
{
q=r1/r2;
r=r1-r2*q;
r1=r2;
r2=r;
}
return r1 ;
}

Aim:Steps:

Write a program to encrypt a message in C language.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void encrypt(char *msg,int k)
//void main()
{
int length,*arr,i;
length=strlen(msg);
arr=(int* )malloc(length*sizeof(int));
for(i=0;i<length;i++)
{
arr[i]=msg[i]-97;
msg[i]=(arr[i]+k)%26+97;
}
}
void decrypt(char *mmsg,int k)
{
int len,*arr,i,temp;
len=strlen(mmsg);
arr=(int *)malloc(len*sizeof(int));
for(i=0;i<len;i++)
{
arr[i]=mmsg[i]-97;
temp=(arr[i]-k)%26;
while(temp<0)
temp=temp+26;
mmsg[i]=temp+97;
}
}
void main()
{
char msg[40],k;
clrscr();
printf("\n Enter the message");
fflush(stdin);
gets(msg);
printf("Enter the key\n");
scanf("%d",&k);
encrypt(msg,k);
printf("Encrypted message is %s",msg);

decrypt(msg,k);
printf("\n\n THE DECRYPTED MESSAGE IS:%s",msg);
getch();
}

Aim:- Write a program to find general and particular solution.


Steps:#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,r1,r2,r,q,q1,d,r3,r4,r5,s=0,t=0,s1=1,s2=0,t1=0,t2=1,x0,y0,x,y;
clrscr();
printf("\n enter the value of a=");
scanf("%d",&a);
printf("\n enter the value of b=");
scanf("%d",&b);
printf("\n enter the value of c =");
scanf("%d",&c);
r1=a;r2=b;q=0;
while(r2>0)
{
q=r1/r2;
r=r1-q*r2;
r1=r2;
r2=r;
}
d=r1;
if(c%d==0)
{
printf("\n\n it has a solution");
r3=a/d;r4=b/d;
while(r4>0)
{
q1=r3/r4;
r5=r3-q1*r4;
r3=r4;r4=r5;
s=s1-q1*s2;
t=t1-q1*t2;
s1=s2;s2=s;t1=t2;t2=t;
}
printf("\n\n value of s=%d ,t=%d",s1,t1);
x0=(c/d)*s1;
y0=(c/d)*t1;
printf("\n\n\n particular solution is given by x0=%d , y0=%d",x0,y0);
printf("\n\n\ngeneral solution:\n");
printf("\n\n\n x = %d + %dk",x0,b/d);
printf("\n\n\n y = %d - %dk",y0,a/d);

printf("\t(where k is an integer)");
}
else
printf("\n \n it has no solution");
getch();
}

Aim:Write a program to implement Additive, multiplicative


and affine cipher in C language.
Steps:#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void addencrypt(char *mmsg,int k)
{
int len,*arr,i;
len=strlen(mmsg);
arr=(int *)malloc(len*sizeof(int));
for(i=0;i<len;i++)
{
arr[i]=mmsg[i]-97;
mmsg[i]=(arr[i]+k)%26+97;
}
}
void adddecrypt(char *mmsg,int k)
{
int len,*arr,i,temp;
len=strlen(mmsg);
arr=(int *)malloc(len*sizeof(int));
for(i=0;i<len;i++)
{
arr[i]=mmsg[i]-97;
temp=(arr[i]-k)%26;
while(temp<0)
temp=temp+26;
mmsg[i]=temp+97;
}
}
void mulencrypt(char *mmsg,int k)
{
int len,*arr,i;
len=strlen(mmsg);
arr=(int *)malloc(len*sizeof(int));
for(i=0;i<len;i++)
{
arr[i]=mmsg[i]-97;
mmsg[i]=(arr[i]*k)%26+97;
}
}
void muldecrypt(char *mmsg,int k)

{
int len,*arr,i,temp;
len=strlen(mmsg);
arr=(int *)malloc(len*sizeof(int));
for(i=0;i<len;i++)
{
arr[i]=mmsg[i]-97;
mmsg[i]=(arr[i]*k)%26+97;
}
}
int gcd(int x,int y)
{
int i,g,n;
if(x<=y)
n=x;
else
n=y;
g=0;
for(i=1;i<=n;i++)
{
if(x%i==0 && y%i==0)
{
if(g<i)
g=i;
}
}
return g;
}
void main()
{
clrscr();
char msg[50],k,ch,q,r1,r2,r,t,t1,t2;
printf(" ENTER MESSAGE:");
fflush(stdin);
gets(msg);
printf("\n\nENTER 1 FOR ADDITIVE CIPHER");
printf("\n\nENTER 2 FOR MULTIPLICATIVE CIPHER");
printf("\n\nENTER 3 FOR AFFINE CIPHER");
printf("\n\nENTER CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\n ADDITIVE CIPHER");

printf("\n\n ENTER KEY:");


scanf("%d",&k);
addencrypt(msg,k);
printf("\n\n THE ENCRYPTED MESSAGE IS:%s",msg);
adddecrypt(msg,k);
printf("\n\n THE DECRYPTED MESSAGE IS:%s",msg);
break;
case 2:printf("\n\n MUTIPLICATIVE CIPHER");
printf("\n\n ENTER KEY:");
scanf("%d",&k);
mulencrypt(msg,k);
printf("\n\n THE ENCRYPTED MESSAGE IS:%s",msg);
if(gcd(k,26)==1)
{
r1=26;
r2=k;
t1=0;
t2=1;
while(r2!=0)
{
q=r1/r2;
r=r1%r2;
t=t1-q*t2;
r1=r2;
r2=r;
t1=t2;
t2=t;
}
while(t1<0)
t1=t1+26;
k=t1%26;
}
muldecrypt(msg,k);
printf("\n\n THE DECRYPTED MESSAGE IS:%s",msg);
break;
//case 3:
default:printf("\nINVALID ENTRY TRY AGAIN");
}
getch();
}
2nd method#include<stdio.h>

#include<conio.h>
#include<string.h>
#include<stdlib.h>
void addencrypt(char *msg,int k)
{
int length,*mat,j;
length=strlen(msg);
mat=(int* )malloc(length*sizeof(int));
for(j=0;j<length;j++)
{
mat[j]=msg[j]-97;
msg[j]=(mat[j]+k)%26+65;
}
}
void adddecrypt(char *msg,int k)
{
int length,*mat1,j;
length=strlen(msg);
mat1=(int* )malloc(length*sizeof(int));
for(j=0;j<length;j++)
{
mat1[j]=msg[j]-65;
msg[j]=(mat1[j]-k)%26+97;
if(((mat1[j]-k)%26)<0)
msg[j]=msg[j]+26;
}
}
void multencrypt(char *msg,int k)
{
int length,*mat,j;
length=strlen(msg);
mat=(int* )malloc(length*sizeof(int));
for(j=0;j<length;j++)
{
mat[j]=msg[j]-97;
msg[j]=(mat[j]*k)%26+65;
}
}
void multdecrypt(char *msg,int k1)
{
int length,*mat1,j;
length=strlen(msg);
mat1=(int* )malloc(length*sizeof(int));
for(j=0;j<length;j++)
{

mat1[j]=msg[j]-65;
msg[j]=(mat1[j]*k1)%26+97;
if(((mat1[j]*k1)%26)<0)
msg[j]=msg[j]+26;
}
}
void affineencrypt(char *msg,int k1,int k2)
{
int length,*mat,j;
length=strlen(msg);
mat=(int* )malloc(length*sizeof(int));
for(j=0;j<length;j++)
{
mat[j]=msg[j]-97;
msg[j]=((mat[j]*k1)+k2)%26+65;
}
}
void affinedecrypt(char *msg,int k2,int k1)
{
int length,*mat1,j;
length=strlen(msg);
mat1=(int* )malloc(length*sizeof(int));
for(j=0;j<length;j++)
{
mat1[j]=msg[j]-65;
msg[j]=((mat1[j]-k2)*k1)%26+97;
if((((mat1[j]-k2)*k1)%26)<0)
msg[j]=msg[j]+26;
}
}
void main()
{
int k2,k3;
char msg[40],ch;
int r1,r2,r,q,t1=0,t2=1,t,k1,k;
clrscr();
printf("\n\t Enter the message : ");
fflush(stdin);
gets(msg);
printf("\n\t select 1 for additive cipher");
printf("\n\t select 2 for mulplicative cipher");
printf("\n\t select 3 for affine cipher");
printf("\n\t select 4 for exit");
printf("\n\t enter ur choice =");

scanf("%c",&ch);
switch(ch)
{
case '1':printf("\n\t Enter the key : ");
scanf("%d",&k);
addencrypt(msg, k);
printf("\n THE ENCRYPTED MESSAGE IS:%s",msg);
adddecrypt(msg,k);
printf("\n THE DECRYPTED MESSAGE IS:%s",msg);
break;
case '2':printf("\n\t Enter the key : ");
scanf("%d",&k);
multencrypt(msg, k);
printf("\n THE ENCRYPTED MESSAGE IS:%s",msg);
r1=26,r2=k;
while(r2>0)
{
q=r1/r2;
r=r1-q*r2;
r1=r2,r2=r;
t=t1-q*t2;
t1=t2,t2=t;
}
if(r1==1)
{
while(t1<0)
{
t1=t1+26;
}
k1=t1;
multdecrypt(msg,k1);
printf("\n THE DECRYPTED MESSAGE IS:%s",msg);
}
else
printf("\n DECRYOPTION CAN'T POSSIBLE WITH THIS KEY k=
%d",k);
break;
case '3':printf("\n\t Enter the key k1: ");
scanf("%d",&k1);
printf("\n\t Enter the key k2:");
scanf("%d",&k2);
affineencrypt(msg, k1,k2);
printf("\n THE ENCRYPTED MESSAGE IS:%s",msg);

r1=26,r2=k1;
while(r2>0)
{
q=r1/r2;
r=r1-q*r2;
r1=r2,r2=r;
t=t1-q*t2;
t1=t2,t2=t;
}
if(r1==1)
{
while(t1<0)
{
t1=t1+26;
}
k1=t1;
affinedecrypt(msg,k2,k1);
printf("\n THE DECRYPTED MESSAGE IS:%s",msg);
}
else
printf("\n AFFINEDECRYOPTION CAN'T POSSIBLE WITH
THESE KEY k1=%d k2=%d",k1,k2);
break;
default:printf("wrong choice");
break;
}
getch(); }

Aim:Write a program to implement Euclidean algorithm in C


language.
Steps:#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int n1,n2,r1,r2,q,r;
printf("\nENTER FIRST NUMBER:");
scanf("%d",&n1);
printf("\nENTER SECOND NUMBER:");
scanf("%d",&n2);
if(n1>=0 && n2>=0)
{
r1=n1;r2=n2;
printf("\nQ\tR1\tR2\tR");
while(r2!=0)
{
r=r1%r2;
q=r1/r2;
printf("\n\n%d\t%d\t%d\t%d",q,r1,r2,r);
r1=r2;
r2=r;
}
printf("\n\n-\t%d\t%d\t-",r1,r2);
printf("\n\n GCD(%d,%d)=%d",n1,n2,r1);
}
else
printf("\nINVALID ENTRY TRY AGAIN");
getch();
}

Aim:- Write a program to implement Extended Euclidean algorithm


in C language.
Steps:#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n1,n2,r1,r2,r,q,s1=1,s2=0,s,t1=0,t2=1,t;
printf("\nENTER FIRST NUMBER:");
scanf("%d",&n1);
printf("\nENTER SECOND NUMBER:");
scanf("%d",&n2);
if(n1>=0 && n2>=0)
{
printf("\nQ\tR1\tR2\tR\tS1\tS2\tS\tT1\tT2\tT");
r1=n1;
r2=n2;
while(r2!=0)
{
r=r1%r2;
q=r1/r2;
s=s1-q*s2;
t=t1-q*t2;
printf("\n\n%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t
%d",q,r1,r2,r,s1,s2,s,t1,t2,t);
r1=r2;
r2=r;
s1=s2;
s2=s;
t1=t2;
t2=t;
}
printf("\n\n-\t%d\t%d\t-\t%d\t%d\t-\t%d\t%d\t-",r1,r2,s1,s2,t1,t2);
printf("\n\nGCD(%d,%d)=%d",n1,n2,r1);
}
else
printf("\nINVALID ENTRY TRY AGAIN");
getch(); }

Aim- Write a program to create password and find whether given


password is strong or weak.
Steps:#include<conio.h>
#include<stdio.h>
#include<string.h>
void veryweak(char*,char*,int);
void main()
{
char un[15],psw[15],psww[15];
int len;
clrscr();
a:printf("\nENTER USERNAME:");
fflush(stdin);
gets(un);
printf("\nENTER PASSWORD(4-15):");
fflush(stdin);
gets(psw);
len=strlen(psw);
if(len>15)
{
printf("\nLENGTH OVERSIZE PLEASE REENTER\n");
goto a;
}
printf("\nREENTER PASSWORD:");
fflush(stdin);
gets(psww);
if(strcmp(psw,psww)!=0)
{
printf("\nTYPE MISMATCH REENTER USERNAME AND
PASSWORD\n");
goto a;
}
printf("\nUSERNAME:");
puts(un);
printf("\nPASSWORD:");
puts(psw);
//printf("%d",len);
veryweak(psw,un,len);
getch();
}
void veryweak(char *ppsw,char *uun,int llen)
{
int flag=0;

if(strcmp(ppsw,uun)==0)
flag=1;
if(llen<5)
flag=1;
/*for(int i=0;i<llen;i++)
{
if(ppsw[i]>='0' && ppsw[i]<='9')
} */
if(flag==1)
printf("\nIT IS A WEAK PASSWORD");
}

AimWrite a program to find multiplicative inverse in C language.


Steps:#include<stdio.h>
#include<conio.h>
int gcd(int,int);
void main()
{
clrscr();
int n,a,r1,r2,q,r,t1,t2,t;
printf("\na * b CONTO 1(mod k)");
printf("\n\nEnter k:");
scanf("%d",&n);
printf("\n\nEnter a(0<=a<=%d):",n-1);
scanf("%d",&a);
if(gcd(n,a)==1)
{
r1=n;
r2=a;
t1=0;
t2=1;
printf("\n\nQ\tR1\tR2\tR\tT1\tT2\tT");
while(r2!=0)
{
q=r1/r2;
r=r1%r2;
t=t1-q*t2;
printf("\n\n%d\t%d\t%d\t%d\t%d\t%d\t%d",q,r1,r2,r,t1,t2,t);
r1=r2;
r2=r;
t1=t2;
t2=t;
}
printf("\n\n-\t%d\t%d\t-\t%d\t%d\t-",r1,r2,t1,t2);
while(t1<0)
t1=t1+n;
printf("\n\nMULTIPLICATIVE INVERSE OF a=%d IS b=
%d",a,t1%n);
}
else
printf("\n\nGCD(%d,%d)!=1,MULTIPLICATIVE INVERSE DOES NOT
EXIST",a,n);
getch();

}
int gcd(int x,int y)
{
int i,g,n;
if(x<=y)
n=x;
else
n=y;
g=1;
for(i=1;i<=n;i++)
{
if(x%i==0 && y%i==0)
{
if(g<i)
g=i;
}
}
printf("%d",g);
return g;
}

Aim:Write a program to Implement RSA algorithm in C


language.
Steps:/* C program for the Implementation Of RSA Algorithm */
#include<stdio.h>
#include<conio.h>
int phi,M,n,e,d,C,FLAG;
int check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
FLAG = 1;
return;
}
FLAG = 0;
}
void encrypt()
{
int i;
C = 1;
for(i=0;i< e;i++)
C=C*M%n;
C = C%n;
printf("\n\tEncrypted keyword : %d",C);
}
void decrypt()
{
int i;
M = 1;
for(i=0;i< d;i++)
M=M*C%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
}
void main()
{
int p,q,s;

clrscr();
printf("Enter Two Relatively Prime Numbers\t: ");
scanf("%d%d",&p,&q);
n = p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t= %d",phi);
do
{
printf("\n\nEnter e\t: ");
scanf("%d",&e);
check();
}while(FLAG==1);
d = 1;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
scanf("%d",&M);
encrypt();
printf("\n\nEnter the Cipher text\t: ");
scanf("%d",&C);
decrypt();
getch();
}

Aim:language.

Write a program in C to implement DES algorithm in C

Steps:
# include <stdio.h>
# include <fstream.h>
# include <string.h>
# include <conio.h>
# include <iostream.h>
int key[64]={
0,0,0,1,0,0,1,1,
0,0,1,1,0,1,0,0,
0,1,0,1,0,1,1,1,
0,1,1,1,1,0,0,1,
1,0,0,1,1,0,1,1,
1,0,1,1,1,1,0,0,
1,1,0,1,1,1,1,1,
1,1,1,1,0,0,0,1
};
class Des
{
public:
int keyi[16][48],
total[64],
left[32],
right[32],
ck[28],
dk[28],
expansion[48],
z[48],
xor1[48],
sub[32],
p[32],
xor2[32],
temp[64],
pc1[56],
ip[64],
inv[8][8];
char final[1000];
void IP();
void PermChoice1();
void PermChoice2();
void Expansion();

void inverse();
void xor_two();
void xor_oneE(int);
void xor_oneD(int);
void substitution();
void permutation();
void keygen();
char * Encrypt(char *);
char * Decrypt(char *);
};
void Des::IP() //Initial Permutation
{
int k=58,i;
for(i=0;i<32;i++)
{
ip[i]=total[k-1];
if(k-8>0) k=k-8;
else
k=k+58;
}
k=57;
for( i=32;i<64;i++)
{
ip[i]=total[k-1];
if(k-8>0) k=k-8;
else
k=k+58;
}
}
void Des::PermChoice1() //Permutation Choice-1
{
int k=57,i;
for(i=0;i<28;i++)
{
pc1[i]=key[k-1];
if(k-8>0) k=k-8;
else
k=k+57;
}
k=63;
for( i=28;i<52;i++)
{
pc1[i]=key[k-1];
if(k-8>0) k=k-8;
else
k=k+55;
}
k=28;
for(i=52;i<56;i++)

{
pc1[i]=key[k-1];
k=k-8;
}
}

AimWrite a program to Implement Rail fence encryption in C


language.
Steps:#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,k=0,j=0,l;
char a[20],b[10],c[20];
clrscr();
printf("\nenter plain text:\n ");
gets(a);
while(a[i]!='\0')
{
if(a[i++]!=' ')
k++;
}
i=0;
while(a[i]!='\0')
{
if(a[i]!=' ')
c[j++]=a[i];
i++;
}
c[j]='\0';
// puts(c);
j=0;l=0;
for(i=0;i<k;i++)
{ a[j++]=c[i++];
b[l++]=c[i];
}
a[j]=b[l]='\0';
strcat(a,b);
printf("\nthe encrypted text is:\n");
puts(a);
getch();
}
2nd method#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char text[40];

char up[20];
int len,i=0,j=0;
clrscr();
printf("\nenter the text for encryption:");
gets(text);
while(text[i]!='\0')
{
if(i%2==0)
{
up[j]=text[i];
j++;
}
i++;
}
up[j]='\0';
puts(up);
getch();
}

AimWrite a program to Implement Huffman coding in C


language.
Steps:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BYTES 256
struct huffcode {
int nbits;
int code;
};
typedef struct huffcode huffcode_t;
struct huffheap {
int *h;
int n, s, cs;
long *f;
};
typedef struct huffheap heap_t;
/* heap handling funcs */
static heap_t *_heap_create(int s, long *f)
{
heap_t *h;
h = malloc(sizeof(heap_t));
h->h = malloc(sizeof(int)*s);
h->s = h->cs = s;
h->n = 0;
h->f = f;
return h;
}
static void _heap_destroy(heap_t *heap)
{
free(heap->h);
free(heap);
}
#define swap_(I,J) do { int t_; t_ = a[(I)]; \
a[(I)] = a[(J)]; a[(J)] = t_; } while(0)
static void _heap_sort(heap_t *heap)
{

int i=1, j=2; /* gnome sort */


int *a = heap->h;
while(i < heap->n) { /* smaller values are kept at the end */
if ( heap->f[a[i-1]] >= heap->f[a[i]] ) {
i = j; j++;
} else {
swap_(i-1, i);
i--;
i = (i==0) ? j++ : i;
}
}
}
#undef swap_
static void _heap_add(heap_t *heap, int c)
{
if ( (heap->n + 1) > heap->s ) {
heap->h = realloc(heap->h, heap->s + heap->cs);
heap->s += heap->cs;
}
heap->h[heap->n] = c;
heap->n++;
_heap_sort(heap);
}
static int _heap_remove(heap_t *heap)
{
if ( heap->n > 0 ) {
heap->n--;
return heap->h[heap->n];
}
return -1;
}
/* huffmann code generator */
huffcode_t **create_huffman_codes(long *freqs)
{
huffcode_t **codes;
heap_t *heap;
long efreqs[BYTES*2];
int preds[BYTES*2];
int i, extf=BYTES;
int r1, r2;

memcpy(efreqs, freqs, sizeof(long)*BYTES);


memset(&efreqs[BYTES], 0, sizeof(long)*BYTES);
heap = _heap_create(BYTES*2, efreqs);
if ( heap == NULL ) return NULL;
for(i=0; i < BYTES; i++) if ( efreqs[i] > 0 ) _heap_add(heap, i);
while( heap->n > 1 )
{
r1 = _heap_remove(heap);
r2 = _heap_remove(heap);
efreqs[extf] = efreqs[r1] + efreqs[r2];
_heap_add(heap, extf);
preds[r1] = extf;
preds[r2] = -extf;
extf++;
}
r1 = _heap_remove(heap);
preds[r1] = r1;
_heap_destroy(heap);
codes = malloc(sizeof(huffcode_t *)*BYTES);
int bc, bn, ix;
for(i=0; i < BYTES; i++) {
bc=0; bn=0;
if ( efreqs[i] == 0 ) { codes[i] = NULL; continue; }
ix = i;
while( abs(preds[ix]) != ix ) {
bc |= ((preds[ix] >= 0) ? 1 : 0 ) << bn;
ix = abs(preds[ix]);
bn++;
}
codes[i] = malloc(sizeof(huffcode_t));
codes[i]->nbits = bn;
codes[i]->code = bc;
}
return codes;
}
void free_huffman_codes(huffcode_t **c)
{
int i;

for(i=0; i < BYTES; i++) free(c[i]);


free(c);
}
#define MAXBITSPERCODE 100
void inttobits(int c, int n, char *s)
{
s[n] = 0;
while(n > 0) {
s[n-1] = (c%2) + '0';
c >>= 1; n--;
}
}
const char *test = "this is an example for huffman encoding";
int main()
{
huffcode_t **r;
int i;
char strbit[MAXBITSPERCODE];
const char *p;
long freqs[BYTES];
memset(freqs, 0, sizeof freqs);
p = test;
while(*p != '\0') freqs[*p++]++;
r = create_huffman_codes(freqs);
for(i=0; i < BYTES; i++) {
if ( r[i] != NULL ) {
inttobits(r[i]->code, r[i]->nbits, strbit);
printf("%c (%d) %s\n", i, r[i]->code, strbit);
}
}
free_huffman_codes(r);
return 0;
}

Das könnte Ihnen auch gefallen