Code:
////////////////////////////////////////////////////////////////////////////////
//
// File Name : URLCoding.c
//
// Platform : IBM PC (Windows XP)
//
// Compiler : Microsoft Visual C++ 6.0
//
////////////////////////////////////////////////////////////////////////////////
#define _CRT_SECURE_NO_DEPRECATE
// Compiler Includes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Constants
#define MAX_STRING_LENGTH 100 // Maximum file name length
#define MAX_ARRAY_SIZE 100 // maximum array size
const char hexa[]={"0123456789ABCDEFabcdef"};
const char nonencode[]={"abcdefghijklmnopqrstuvwqyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_~"};
//User defined definitions struct { // PERSON_TYPE structure definition
typedef struct {
char name[MAX_ARRAY_SIZE];
char address[MAX_ARRAY_SIZE];
char phone[MAX_ARRAY_SIZE];
} PERSON_TYPE;
//function prototype
void strip_newline( char *string, int size );
char encode_string(char *string, size_t length); // function to encode information
PERSON_TYPE scanPersonByValue(void); // function to scan person info
void printPersonInfo (PERSON_TYPE personToPrint); //function to print information
void URLEncode(char* destination, int length, char* source);
void URLDecode(char* destination, int length, char* source);
int GetHex(char value);
int main (void) {
//local variables
PERSON_TYPE personOne = {"<JOHN PAUL Jones>","<124 Rainbow Way>","<1-888-555-4444>"};
PERSON_TYPE encodedPerson = {'\0', '\0', '\0'};
PERSON_TYPE unencodedPerson = {'\0', '\0', '\0'};
printPersonInfo(personOne); // prints out default array
personOne = scanPersonByValue(); //
URLEncode(encodedPerson.name, sizeof personOne.name, personOne.name);
URLEncode(encodedPerson.address, sizeof personOne.address, personOne.address);
URLEncode(encodedPerson.phone, sizeof personOne.phone, personOne.phone);
printPersonInfo(encodedPerson);
// this is to test to make sure personOne is just being sent to URLDecode
// it isn't so URLDecode is working properly
personOne= scanPersonByValue();
printPersonInfo(personOne);
URLDecode(unencodedPerson.name, sizeof encodedPerson.name, encodedPerson.name);
URLDecode(unencodedPerson.address, sizeof encodedPerson.address, encodedPerson.address);
URLDecode(unencodedPerson.phone, sizeof encodedPerson.phone, encodedPerson.phone);
printPersonInfo(unencodedPerson);
return 0;
}
PERSON_TYPE scanPersonByValue (void) {
PERSON_TYPE inputPerson;
printf("\nenter your name:");
fgets(inputPerson.name, sizeof inputPerson.name, stdin);
strip_newline(inputPerson.name, strlen(inputPerson.name));
printf("\nenter your address:");
fgets(inputPerson.address,sizeof inputPerson.address, stdin);
strip_newline(inputPerson.address, strlen(inputPerson.address));
printf("\nenter your phone number:");
fgets(inputPerson.phone, sizeof inputPerson.phone, stdin);
strip_newline(inputPerson.phone, strlen(inputPerson.phone));
return inputPerson;
} //end function scanPersonByValue
void printPersonInfo (PERSON_TYPE personToPrint) {
printf("\n==================================================================\n");
printf("NAME: %s \nADDRESS: %s \nPHONE: %s\n",personToPrint.name,
personToPrint.address, personToPrint.phone); // prints structures from PERSON_TYPE */
} // end function PrintpersonInfo
void strip_newline( char *string, int size )
{
int i;
/* remove the null terminator */
for ( i = 0; i < size; ++i )
{
if ( string[i] == '\n' )
{
string[i] = '\0';
/* we're done, so just exit the function by returning */
return;
}
}
/* if we get all the way to here, there must not have been a newline! */
}
int GetHex(char value){
int i;
for(i=0;hexa[i];i++){
if(hexa[i]==value){
if(i>15){ return i-6; }
return i;
}
}
return -1;
}
void URLDecode(char* destination,int length, char* source){
//local variables
int destc=0,i;
for(i=0;destc+1<length&&source[i];i++){
if(source[i]=='%'&&(GetHex(source[i+1])>=0&&GetHex(source[i+2])>=0)){
destination[destc]=GetHex(source[i+1])<<4|GetHex(source[i+2]);
i+=2;
}
else if(source[i]=='+'){
destination[destc]=' ';
}
else{
destination[destc]=source[i];
}
destc++;
}
destination[destc]='\0';
}
void URLEncode(char* destination,int length, char* source){
//local variables
int destc=0;
int i;
int a;
int clear=0;
for(i=0;destc+1<length&&source[i];i++){
clear=0;
for(a=0;nonencode[a];a++){
if(nonencode[a]==source[i]){
clear=1;
break;
}
}
if(!clear){
if(destc+3>=length){
break;
}
destination[destc]='%';
destination[destc+1]=hexa[source[i]/16];
destination[destc+2]=hexa[source[i]%16];
destc+=3;
}
else{
destination[destc]=source[i];
destc++;
}
}
destination[destc]='\0';
}
My next step is to take the encoded information and make a query string with it. Then take that URLDecode function and have the query string passed to it after it has been broken apart. Thanks for all the help