Ok, now you need a brief rundown of how URL encoding works. All of the variables
provided in the form (named Var1, name, and address) are set equal to the data they will
return, each variable separated by an & (ampersand). All spaces, as I have pointed out
before, have been removed and replaced with +'s (plus symbols). Any extraneous ASCII data
is converted to hex, as is the ' (apostrophe) in "Calypso's island". Here is a
small chunk of code that will decode all of the hex and put the URL string, with the hex
and spaces decoded into a variable called buff2[]. The string with the original URL
encoded is called buff[]. Please take note of the comment "/* Prevent user from
altering URL delimiter sequence */". This means that if a user types an &
(ampersand) or an = (equals sign) into your program, it won't mess up the natural
delimiting structure of the URL encoding scheme. This also means, on the downside, that
you'll have to pass each separate variable string through another loop similar to this one
to change the &'s and ='s back into their respective characters.
for (x = 0, y = 0; x < strlen(buff); x++, y++)
{
switch (buff[x])
{
/* Convert all + chars to space chars */
case '+':
buff2[y] = ' ';
break;
/* Convert all %xy hex codes into ASCII chars */
case '%':
/* Copy the two bytes following the % */
strncpy(hexstr, &buff[x + 1], 2);
/* Skip over the hex */
x = x + 2;
/* Convert the hex to ASCII */
/* Prevent user from altering URL delimiter sequence */
if( ((strcmp(hexstr,"26")==0)) || ((strcmp(hexstr,"3D")==0)) )
{
buff2[y]='%';
y++;
strcpy(buff2,hexstr);
y=y+2;
break;
}
buff2[y] = (char)strtol(hexstr, NULL, 16);
break;
/* Make an exact copy of anything else */
default:
buff2[y] = buff[x];
break;
}
}
|