You are on page 1of 27

Program Number:1

//WAP a C program for character stuffing


#include<stdio.h> #include<conio.h> #include<string.h> #include<process.h> void main() { int i=0,j=0,n,pos; char a[20],b[50],ch; clrscr(); printf("enter string\n"); scanf("%s",&a); n=strlen(a); printf("enter position\n"); scanf("%d",&pos); if(pos>n) { printf("invalid position, Enter again :"); scanf("%d",&pos); } printf("enter the character\n"); ch=getche(); b[0]='d'; b[1]='l'; b[2]='e'; b[3]='s'; b[4]='t'; b[5]='x'; j=6; while(i<n) { if(i==pos-1) { b[j]='d'; b[j+1]='l'; b[j+2]='e'; b[j+3]=ch; b[j+4]='d'; b[j+5]='l'; b[j+6]='e'; j=j+7; } if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e') { b[j]='d'; b[j+1]='l'; b[j+2]='e'; j=j+3; }

b[j]=a[i]; i++; j++; } b[j]='d'; b[j+1]='l'; b[j+2]='e'; b[j+3]='e'; b[j+4]='t'; b[j+5]='x'; b[j+6]='\0'; printf("\nframe after stuffing:\n"); printf("%s",b); getch(); } INPUT: enter string: asdlefgh enter position: 8 invalid position,enter again: 3 enter the character: k OUTPUT: frame after stuffing: dlestx as dle k dle dle dlefgh dleetx

Program Number:2
//Bit stuffing
#include <stdio.h> #include <conio.h> void main() { char num[11], i, j, k, cnt=0; clrscr(); printf("Enter the sequence of 10 digit binary numbers: "); for(i=0;i<10;i++) scanf("%c", &num[i]); printf("The 10 digit binary number you have entered is: "); for(i=0;i<10;i++) printf("%c", num[i]); printf("\nAfter stuffing: "); i=0; while (i<10) { if(num[i]=='1') { i++; cnt++; if(cnt==5) { for(k=11;k>i;k--) num[k]=num[k-1]; num[i]=0; num[i]='0'; } } else { i++; cnt=0; } }

for(i=0;i<11;i++) printf("%c", num[i]); printf("\nAfter destuffing: "); i=0; while (i<10) { if(num[i]=='1') { i++; cnt++; if(cnt==5) { for(k=i;k<11;k++) num[k]=num[k+1]; } } else { i++; cnt=0; } } for(i=0;i<10;i++) printf("%d", num[i]-48); getch(); }

Program Number:3
// CRC Implementation
#include <stdio.h> #include <stdlib.h> main() { int i, j, n, g, a, ms[20], gen[20], b[20], q[20], s; printf("transmiter side"); printf("enter no of data bits"); scanf("%d", &n); printf("enter data"); for (i = 0; i < n; i++) scanf("%d", &ms[i]); printf("enter size of generator"); scanf("%d", &g); printf("enter generator"); for (j = 0; j < g; j++) scanf("%d", &gen[j]); printf("\n \t the generated matrix"); for (j = 0; j < g; j++) printf("%d", gen[j]); a = n + (g - 1); printf("\t \n the appended matrix is::"); for (i = 0; i < j; i++) ms[n + i] = 0; for (i = 0; i < a; i++) printf("%d", ms[i]); for (i = 0; i < n; i++) q[i] = ms[i]; for (i = 0; i < n; i++) { if (ms[i] == 0) { for (j = i; j < g + i; j++) { ms[j] = ms[j] ^ 0; } } else { ms[i] = ms[i] ^ gen[0]; ms[i + 1] = ms[i + 1] ^ gen[1]; ms[i + 2] = ms[i + 2] ^ gen[2]; ms[i + 3] = ms[i + 3] ^ gen[3]; } } printf("\n \t the crc is::"); for (i = n; i < a; i++)

printf("%d", ms[i]); s = n + a; for (i = n; i < s; i++) q[i] = ms[i]; printf("\n"); for (i = 0; i < a; i++) printf("%d", q[i]); }

Program Number:4
/* Program to find hamming(7,4) code*/
#include<stdio.h> #include<stdlib.h> char data[5]; int encoded[8],edata[7],syndrome[3]; 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 }; char gmatrix[4][8]={"0111000","1010100","1100010","1110001"}; int main(){ int i,j; system("clear"); printf("\nHamming code----- Encoding\n"); printf("Enter 4 bit data : "); scanf("%s",data); printf("\nGenerator matrix\n"); for(i=0;i<4;i++) printf("%s\n",gmatrix[i]); printf("\nEncoded data "); for(i=0;i<7;i++) { for(j=0;j<4;j++) encoded[i]+=((data[j]-'0')*(gmatrix[j][i]-'0')); encoded[i]=encoded[i]%2; printf("%d ",encoded[i]); } printf("\nHamming code----- Decoding\n"); printf("Enter encoded bits as recieved : "); for(i=0;i<7;i++) scanf("%d",&edata[i]); for(i=0;i<3;i++) { for(j=0;j<7;j++) syndrome[i]+=(edata[j]*hmatrix[i][j]); syndrome[i]=syndrome[i]%2; } for(j=0;j<7;j++) if((syndrome[0]==hmatrix[0][j]) && (syndrome[1]==hmatrix[1][j])&& (syndrome[2]==hmatrix[2][j])) break; if(j==7) printf("\nError free\n"); else

{ printf("\nError recieved at bit number %d of data\n",j+1); edata[j]=!edata[j]; printf("\nCorrect data should be : "); for(i=0;i<7;i++) printf("%d",edata[i]); } return 0; }

Program Number:5
//LZW compression algorithm
#include <stdio.h> #include <stdlib.h> #include <string.h> #define #define #define #define BITS 12 /* HASHING_SHIFT (BITS-8) /* MAX_VALUE (1 << BITS) - 1 /* MAX_CODE MAX_VALUE - 1 /* /* #if BITS == 16 #define TABLE_SIZE 99991 #endif #if BITS == 14 #define TABLE_SIZE 18041 /* #endif /* #if BITS == 13 /* #define TABLE_SIZE 9029 #endif #if BITS <= 12 #define TABLE_SIZE 5021 #endif void *malloc(); int *code_value; unsigned int *prefix_code; unsigned char *append_character; unsigned char decode_stack[4000]; /* /* /* /* This This This This is the code array holds array holds array holds value array the prefix codes the appended chars the decoded string */ */ */ */ Setting the number of bits to 12, 13*/ or 14 affects several constants. */ Note that MS-DOS machines need to */ compile their code in large model if*/ 14 bits are selected. */

The string table size needs to be a */ prime number that is somewhat larger*/ than 2**BITS. */

/* * Forward declarations */ void compress(FILE *input,FILE *output); void expand(FILE *input,FILE *output); int find_match(int hash_prefix,unsigned int hash_character); void output_code(FILE *output,unsigned int code); unsigned int input_code(FILE *input); unsigned char *decode_string(unsigned char *buffer,unsigned int code); /******************************************************************** ** ** This program gets a file name from the command line. It compresses the ** file, placing its output in a file named test.lzw. It then expands ** test.lzw into test.out. Test.out should then be an exact duplicate of

** the input file. ** *************************************************************************/ main(int argc, char *argv[]) { FILE *input_file; FILE *output_file; FILE *lzw_file; char input_file_name[81]; char command; command=(argv==3); /* ** */ The three buffers are needed for the compression phase. code_value=(int*)malloc(TABLE_SIZE*sizeof(int)); prefix_code=(unsigned int *)malloc(TABLE_SIZE*sizeof(unsigned int)); append_character=(unsigned char *)malloc(TABLE_SIZE*sizeof(unsigned char)); if (code_value==NULL || prefix_code==NULL || append_character==NULL) { printf("Fatal error allocating table space!\n"); exit(-1); } /* ** Get the file name, open it up, and open up the lzw output file. */ if (argc>1) strcpy(input_file_name,argv[1]); else { printf("Input file name? "); scanf("%s",input_file_name); } input_file=fopen(input_file_name,"rb"); lzw_file=fopen("test.lzw","wb"); if (input_file==NULL || lzw_file==NULL) { printf("Fatal error opening files.\n"); exit(-1); }; /* ** Compress the file. */ if(command=='r') { compress(input_file,lzw_file); } fclose(input_file); fclose(lzw_file); free(c-ode_value); /* ** Now open the files for the expansion. */ lzw_file=fopen("test.lzw","rb"); output_file=fopen("test.out","wb");

if (lzw_file==NULL || output_file==NULL) { printf("Fatal error opening files.\n"); exit(-2); }; /* ** Expand the file. */ expand(lzw_file,output_file); fclose(lzw_file); fclose(output_file); free(prefix_code); free(append_character); } /* ** This is the compression routine. The code should be a fairly close ** match to the algorithm accompanying the article. ** */ void compress(FILE *input,FILE *output) { unsigned int next_code; unsigned int character; unsigned int string_code; unsigned int index; int i; next_code=256; code*/ for (i=0;i<TABLE_SIZE;i++) */ code_value[i]=-1; /* Next code is the next available string /* Clear out the string table before starting

i=0; printf("Compressing...\n"); string_code=getc(input); /* Get the first code */ /* ** This is the main loop where it all happens. This loop runs util all of ** the input has been exhausted. Note that it stops adding codes to the ** table after all of the possible codes have been defined. */ while ((character=getc(input)) != (unsigned)EOF) { if (++i==1000) /* Print a * every 1000 */ { /* input characters. This */ i=0; /* is just a pacifier. */ printf("*"); } index=find_match(string_code,character);/* See if the string is in */ if (code_value[index] != -1) /* the table. If it is, */ string_code=code_value[index]; /* get the code value. If */ else /* the string is not in the*/ { /* table, try to add it. */

if (next_code <= MAX_CODE) { code_value[index]=next_code++; prefix_code[index]=string_code; append_character[index]=character; } output_code(output,string_code); /* string_code=character; /* } /* } /*

When a string is found */ that is not in the table*/ I output the last string*/ after adding the new one*/

/* ** End of the main loop. */ output_code(output,string_code); /* Output the last code */ output_code(output,MAX_VALUE); /* Output the end of buffer code */ output_code(output,0); /* This code flushes the output buffer*/ printf("\n"); } /* ** ** ** ** */ This is the hashing routine. It tries to find a match for the prefix+char string in the string table. If it finds it, the index is returned. If the string is not found, the first available index in the string table is returned instead.

int find_match(int hash_prefix,unsigned int hash_character) { int index; int offset; index = (hash_character << HASHING_SHIFT) ^ hash_prefix; if (index == 0) offset = 1; else offset = TABLE_SIZE - index; while (1) { if (code_value[index] == -1) return(index); if (prefix_code[index] == hash_prefix && append_character[index] == hash_character) return(index); index -= offset; if (index < 0) index += TABLE_SIZE; } } /* ** ** ** */ This is the expansion routine. It takes an LZW format file, and expands it to an output file. The code here should be a fairly close match to the algorithm in the accompanying article.

void expand(FILE *input,FILE *output) {

unsigned int next_code; unsigned int new_code; unsigned int old_code; int character; int counter; unsigned char *string; next_code=256; /* This is the next available code to define */ counter=0; /* Counter is used as a pacifier. */ printf("Expanding...\n"); old_code=input_code(input); character=old_code; putc(old_code,output); /* Read in the first code, initialize the */ /* character variable, and send the first */ /* code to the output file */

/* ** This is the main expansion loop. It reads in characters from the LZW file ** until it sees the special code used to inidicate the end of the data. */ while ((new_code=input_code(input)) != (MAX_VALUE)) { if (++counter==1000) /* This section of code prints out */ { /* an asterisk every 1000 characters */ counter=0; /* It is just a pacifier. */ printf("*"); } /* ** This code checks for the special STRING+CHARACTER+STRING+CHARACTER+STRING ** case which generates an undefined code. It handles it by decoding ** the last code, and adding a single character to the end of the decode string. */ if (new_code>=next_code) { *decode_stack=character; string=decode_string(decode_stack+1,old_code); } /* ** Otherwise we do a straight decode of the new code. */ else string=decode_string(decode_stack,new_code); /* ** Now we output the decoded string in reverse order. */ character=*string; while (string >= decode_stack) putc(*string--,output); /* ** Finally, if possible, add a new code to the string table. */ if (next_code <= MAX_CODE) { prefix_code[next_code]=old_code; append_character[next_code]=character; next_code++; }

old_code=new_code; } printf("\n"); } /* ** This routine simply decodes a string from the string table, storing ** it in a buffer. The buffer can then be output in reverse order by ** the expansion program. */ unsigned char *decode_string(unsigned char *buffer,unsigned int code) { int i; i=0; while (code > 255) { *buffer++ = append_character/code/; code=prefix_code/code/; if (i++>=MAX_CODE) { printf("Fatal error during code expansion.\n"); exit(-3); } } *buffer=code; return(buffer); } /* ** The following two routines are used to output variable length ** codes. They are written strictly for clarity, and are not ** particularyl efficient. */ unsigned int input_code(FILE *input) { unsigned int return_value; static int input_bit_count=0; static unsigned long input_bit_buffer=0L; while (input_bit_count <= 24) { input_bit_buffer |= (unsigned long) getc(input) << (24-input_bit_count); input_bit_count += 8; } return_value=input_bit_buffer >> (32-BITS); input_bit_buffer <<= BITS; input_bit_count -= BITS; return(return_value); } void output_code(FILE *output,unsigned int code) { static int output_bit_count=0;

static unsigned long output_bit_buffer=0L; output_bit_buffer |= (unsigned long) code << (32-BITS-output_bit_count); output_bit_count += BITS; while (output_bit_count >= 8) { putc(output_bit_buffer >> 24,output); output_bit_buffer <<= 8; output_bit_count -= 8; } }

Program Number:6
//Socket programming in c for listener and talker

#include #include #include #include #include #include #include #include #include #include

<stdio.h> <stdlib.h> <unistd.h> <errno.h> <string.h> <sys/types.h> <sys/socket.h> <netinet/in.h> <arpa/inet.h> <netdb.h> // the port users will be connecting to

#define SERVERPORT "8080"

int main(int argc, char *argv[]) { int sockfd; struct addrinfo hints, *servinfo, *p; int rv; int numbytes; if (argc != 2) { fprintf(stderr,"usage: talker hostname\n"); exit(1); } memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } // loop through all the results and make a socket for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("talker: socket"); continue; } break; } if (p == NULL) {

fprintf(stderr, "talker: failed to bind socket\n"); return 2; } int num = 9090; num = htonl(num); if ((numbytes = sendto(sockfd, num, sizeof(num), 0, p->ai_addr, p->ai_addrlen)) == -1) { perror("talker: sendto"); exit(1); }

Program Number:7
/*** listener.c -- a datagram sockets "server" demo*/

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h>

#define MYPORT "4950" // the port users will be connecting to

#define MAXBUFLEN 100

// get sockaddr, IPv4 or IPv6: void *get_in_addr(struct sockaddr *sa) { if (sa->sa_family == AF_INET) {

return &(((struct sockaddr_in*)sa)->sin_addr); }

return &(((struct sockaddr_in6*)sa)->sin6_addr); }

int main(void) { int sockfd; struct addrinfo hints, *servinfo, *p; int rv; int numbytes; struct sockaddr_storage their_addr; char buf[MAXBUFLEN]; socklen_t addr_len; char s[INET6_ADDRSTRLEN];

memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4 hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_PASSIVE; // use my IP

if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1;

// loop through all the results and bind to the first we can for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("listener: socket"); continue; }

if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("listener: bind"); continue; }

break; }

if (p == NULL) { fprintf(stderr, "listener: failed to bind socket\n"); return 2; }

freeaddrinfo(servinfo);

printf("listener: waiting to recvfrom...\n");

addr_len = sizeof their_addr; if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); }

printf("listener: got packet from %s\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s)); printf("listener: packet is %d bytes long\n", numbytes); buf[numbytes] = '\0'; printf("listener: packet contains \"%s\"\n", buf);

close(sockfd);

return 0; }

Program Number:8
/*** talker.c -- a datagram "client" demo*/

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h>

#define SERVERPORT "4950" // the port users will be connecting to

int main(int argc, char *argv[]) { int sockfd; struct addrinfo hints, *servinfo, *p; int rv; int numbytes;

if (argc != 3) { fprintf(stderr,"usage: talker hostname message\n"); exit(1); }

memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM;

if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; }

// loop through all the results and make a socket for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("talker: socket"); continue; }

break; }

if (p == NULL) { fprintf(stderr, "talker: failed to bind socket\n"); return 2; }

if ((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, p->ai_addr, p->ai_addrlen)) == -1) { perror("talker: sendto"); exit(1); }

freeaddrinfo(servinfo);

printf("talker: sent %d bytes to %s\n", numbytes, argv[1]); close(sockfd);

return 0; }

Program Number:9
// To encrypt a 64-bit plain text using DES algorithm.
#include <stdio.h> #include <stdlib.h> #include <conio.h>

int s00[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}},s0,s1; int s11[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}}; int f1[4],f2[4],k1[8]={1,0,1,0,0,1,0,0},k2[8]={0,1,0,0,0,0,1,1}; int ip[8],ipi[8]={1,5,2,0,3,7,4,6},cp[8],epi[8]={3,0,1,2,1,2,3,0},ep[8]; int p[4],r1,c1,r2,c2,p4[4],p44[4]={1,3,2,0},i,j,k[10],r[4],c=0,rs[8]; int p[8]={1,0,0,0,1,1,0,1},ipii[8]={3,0,2,4,6,1,7,5}; main() { clrscr(); printf(\n Enter the 8 bits of plain text:); for(i=0;i<8;i++) scanf(%d,&p1[i]); printf(\n enter the 10-bit key:); scanf(%d, &k[i]); for(i=0;i<8;i++) ip[i]=p1[ipi[i]]; fn(); c++; fn(); for(i=0;i<4;i++) ip[i]=r[i]; for(i=0,j=4;i<4;i++,j++) ip[j]=f2[i]; for(i=0;i<8;i++) cp[i]=ip[ipii[i]]; printf(\n The cipher text is \n); for(i=0;i<8;i++) printf(%d,\t,cp[i]); getch();

int fn() { for(i=0,j=0;i<4;i++,j++) f1[j]=ip[i]; for(i=4,j=0;i<8;i++,j++) f2[j]=ip[i]; for(i=0;i<8;i++) ep[i]=f2[epi[i]]; if(c==0) { for(i=0;i<8;i++) { if(k1[i]==ep[i]) rs[i]=0; else rs[i]=1; } } else { for(i=0;i<8;i++) { if(k2[i]==ep[i]) rs[i]=0; else rs[i]=1; } } r1=rs[0]*2+rs[3]*1; c1=rs[1]*2+rs[2]*1; r2=rs[4]*2+rs[7]*1; c2=rs[5]*2+rs[6]*1; s0=s00[r1][c1]; s1=s11[r2][c2]; i=0; p[i]=s0/2;i++; p[i]=s0%2;i++;

p[i]=s1/2;i++; p[i]=s1%2; for(i=0;i<4;i++) p4[i]=p[p44[i]]; for(i=0;i<4;i++) { if(p4[i]==f1[i]) r[i]=0; else r[i]=1; } for(i=0;i<4;i++) ip[i]=f2[i]; for(i=0,j=4;i<4;i++,j++) ip[j]=r[i]; }

You might also like