@lay:4386l3

Listing 3

/**************************************************
Function: GetFormData

Input:    NONE

Return:   A pointer to the start of as array of 
          pointers. Each element in this array 
          points to a NULL terminated string 
          containing a field name / field value 
          pair separated by an equal sign.

**************************************************/

char **GetFormData(flag LoadData) {
    char         *Buffer         = NULL;
    static char **Fields         = NULL;
    int           BufferLoop     = 0;
    int           NumberOfFields = 0;

    char         *AllocateString = 
                 getenv("CONTENT_LENGTH");
    
    long          AllocateLong   = 0;

    /* check if we already read in the form data 
       previously */

    if (Fields != NULL || LoadData == FALSE) 
        return(Fields);

    /* allocate space for the buffer and exit if 
       there's a problem */

    if (AllocateString != NULL)
        AllocateLong = atol(AllocateString);

    if (AllocateLong == 0)
        AllocateLong = 65536;

    if (AllocateLong > 0)
        AllocateLong += 100;

    Buffer = 
       (char *) malloc(sizeof(char) * AllocateLong);
      
    if (Buffer == NULL) return(NULL);

    /* if allocation was successful, put form data 
       in the buffer */

    bzero(Buffer, sizeof(char) * AllocateLong);
    fread(Buffer, sizeof(char) 
        * (AllocateLong - 1), 1, stdin);

    /* figure out how many fields we have and 
       allocate the array of fields */

    BufferLoop     = 0;
    NumberOfFields = 2;

    while(Buffer[BufferLoop] != 0x00) {
        if (Buffer[BufferLoop] == '&')
            NumberOfFields += 1;
        BufferLoop += 1;
    } /* end while */

    Fields = (char **) malloc(sizeof(char *) 
        * NumberOfFields);
    bzero(Fields, sizeof(char *) * NumberOfFields);

    if (Fields == NULL) {
        free(Buffer);
        return(NULL);
    } /* end if */

    /* load the fields array */

    BufferLoop     = 0;
    NumberOfFields = 1;
    Fields[0] = Buffer;

    while(Buffer[BufferLoop] != 0x00) {
        if (Buffer[BufferLoop] == '&') {
           Fields[NumberOfFields] = 
               &Buffer[BufferLoop + 1];
           Buffer[BufferLoop] = 0x00;
           ConvertToText(Fields[NumberOfFields-1]);
           NumberOfFields += 1;
        } /* end if */

        BufferLoop += 1;
    } /* end while */

    ConvertToText(Fields[NumberOfFields - 1]);

    /* get out successfully */
    return(Fields);
} /* end function GetFormData() */



/**************************************************
Function: ReleaseFormData
    
Input:    NONE
    
Return:   NONE
    
Purpose:  Release any structures which were 
          dynamically allocated when the form data 
          was read from stdin.
**************************************************/

void ReleaseFormData() {
    char **FormData = GetFormData(FALSE);
    if (FormData != NULL) {
        free(FormData[0]);
    free(FormData);
    } /* end if */
} /* end function ReleaseFormData() */

/**************************************************
Function: ConvertToText() 

Input:    Input - Pointer to a NULL terminated 
          string containing the data to be 
          converted.
    
Return:   NONE

**************************************************/

void ConvertToText(char *Input) {
    int   InputIndex = 0;
    int   TextIndex  = 0;
    unsigned char  HighDigit;
    unsigned char  LowDigit;
    char           Symbol;

    while (Input[InputIndex]) {
    
    /* '+' indicates that a space was passed in */
    if (Input[InputIndex] == '+') {
        Input[TextIndex++] = ' ';
        InputIndex += 1;
    } /* end if */

    /* a '%UV' indicates that character with ascii 
       value 0xUV was given */

    else if (Input[InputIndex] == '%') {
        if (Input[InputIndex + 1] > 0x2F 
            && Input[InputIndex + 1] <
            0x3A)
            HighDigit = Input[InputIndex + 1] 
            - 0x30;
        
        if (Input[InputIndex + 1] > 0x40 
            && Input[InputIndex + 1] < 0x47)
            HighDigit = Input[InputIndex + 1] 
                        - 0x37;

        if (Input[InputIndex + 2] > 0x2F 
            && Input[InputIndex + 2] < 0x3A)
            LowDigit = Input[InputIndex + 2] - 0x30;

        if (Input[InputIndex + 2] > 0x40 
            && Input[InputIndex + 2] < 0x47)
            LowDigit = Input[InputIndex + 2] - 0x37;


        Input[TextIndex++] = (HighDigit * 0x10) 
            + LowDigit;

        InputIndex += 3;

    } /* end else-if */

    /* any other character is not translated */

    else
        Input[TextIndex++] = Input[InputIndex++];
    } /* end while */

    Input[TextIndex] = 0x00;

    /* clean up the form data content */

    TextIndex = 0;

    while(Input[TextIndex] != 0x00) {
        if (Input[TextIndex] == '~')
            Input[TextIndex] = ' ';

        if (Input[TextIndex] <  0x20 
            && Input[TextIndex] != 0x0A 
            && Input[TextIndex] != 0x0D 
            && Input[TextIndex] != 0x09 )
            Input[TextIndex] = ' ';
        TextIndex += 1;
    } /* end while */

} /* end function ConvertToText() */           
