CSCI 3310
Summer 2006
Small Programming Assignment 1

Assigned: May 16
Due: May 18, 11:59pm


HTML and CGI

There are a variety of ways to make web pages that respond to requests. One is called the Common Gateway Interface, or CGI. The user fills in a form on a web page, and submits the form to a server. The server runs a program that receives information on the form and produces a new page for the browser to display.

This page does not explain how CGI works, except one aspect of that. Between the user input and the program that responds to the request, strings are converted into a form that avoids certain special characters. The following conversions are done.

  1. Each space is replaced by +.
  2. A special character (including, for example, +) is replaced by a three-character sequence %XX, where XX is the hexadecimal code for the character. For example, ':' is replaced by %3A.

The program that processes the request needs to undo those conversions. This assignment is to write a function that a CGI application can use for that purpose.


The assignment

Write a C++ function called unmangle that takes an array of characters (a null-terminated string) as a parameter, and returns a newly allocated array of characters that holds the string that you get by undoing the above conversions, with a null terminator. For example, unmangle("a+b%3A") should return null-terminated string "a b:".


Getting started

You will want to be able to convert a string of hexadecimal digits to an integer (a character code). Here is a function that does such a conversion. You can use this as a helper for unmangle.

/************************************************
 *              hex_to_int                      *
 ************************************************
 * digs is an array of two hexadecimal digits.  *
 * Return the number that they represent.  For  *
 * example, "2A" represents 2*16 + 10 = 42.     *
 * Array digs is not required to be             *
 * null-terminated.                             *
 ************************************************/

int hex_to_int(const char* digs)
{
  int result;
  char digcpy[3];

  digcpy[0] = digs[0];
  digcpy[1] = digs[1];
  digcpy[2] = '\0';
  sscanf(digcpy, "%x", &result);
  return result;
}

Submitting your work

Put your function in a file called unmangle.cpp. Be sure to include the hex_to_int function as well. If there is a main program, for testing, then make a copy of your program, remove the main program from the copy, and submit that.

Put your file somewhere in your home directory in the lab. Change to the directory that contains your file. For example, if it is in directory 3310, then do command

  cd 3310
Then submit using command
  ~karl/3310/bin/submit S1 unmangle.cpp
You should get a confirmation that the file was successfully submitted.