[ b a c k ]
/********************************************************************
* bs.c is a program that demonstrates byte stuffing.
*
* Completed: 9:50 AM 2/9/01
* Author: William Martin boneshakerbike@hotmail.com
*******************************************************************/
#include <stdio.h>
/*********************************************************
VARIABLES
**********************************************************/
char string1[25] = {1, 'H', 'i', ' ', 27, 'w', 'o', 'r',
'l', 'd', ',', ' ', 1, 'I', ' ', 'a',
'm', ' ', 'h', 'e', 'r', 'e', 4, 4};
char string2[25];
char packet[28];
int index1 = 1, index2 = 0;
/*********************************************************
FUNCTIONS
**********************************************************/
void packetStuff(void *str);
void packetUnStuff(void *str);
/*********************************************************
MAIN
**********************************************************/
int main(void){
printf("\n"
"Hello and welcome to a byte stuffing exercise."
"\n\nSince you cannot type non printable characters"
"\nfrom the keyboard this exercise will use the"
"\ncharacter string "
"\n\n\"[soh]Hi [esc]world, [soh]I am "
" here[eot][eot]\" "
"\n\nFor soh you will see ==> %c"
"\nFor esc you will see ==> %c"
"\nFor eot you will see ==> %c"
"\n\n", 1, 27, 4);
printf("Before stuffing: %s\n", string1);
packetStuff(string1);
printf("After stuffing: %s\n", packet);
packetUnStuff(packet);
printf("After un-stuffing: %s\n", string2);
return 0;
}
/********************************************************************
* packetStuff is a function that takes a message in the form of a
* string and for every soh, eot, and esc another sec is added in
* front to provide packet stuffing.
*
* Completed: 7:41 AM 2/9/01
* Dependencies: (global) variable packet is modified
* Input: string of characters
* Output: void
* Sample Call: packetStuff(string);
*******************************************************************/
void packetStuff(void *str){
packet[0]= 1;
for(int i = 1; i < 23; i++){
if(string1[i] == 1 || string1[i] == 4 || string1[i] == 27){
packet[index1] = 27;
index1++;
packet[index1] = string1[i];
index1++;
}else{
packet[index1] = string1[i];
index1++;
}
}
packet[index1] = 4;
}
/********************************************************************
* packetUnStuff is a function that takes a message that has been
* stuffed and unstuffs it.
*
* Completed: 7:50 AM 2/9/01
* Dependencies: (global)variable string2 is modified
* Input: string of characters (stuffed)
* Output: void
* Sample Call: packetUnStuff(packet);
*******************************************************************/
void packetUnStuff(void *str){
for(int i = 0; i < 30; i++){
if(packet[i] != 27){
string2[index2] = packet[i];
index2++;
}
}
}
[ t o p ]