DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (5b81998bb7ab)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
**  formdata.c
**
**  Play utility to parse up form get data into name value pairs.
*/

#include "formdata.h"

#include <stdlib.h>
#include <string.h>
#include <ctype.h>


static void unhexcape(char* inPlace)
/*
**  Real low tech unhexcaper....
**
**  inPlace     string to decode, in place as it were.
*/
{
    if(NULL != inPlace)
    {
        int index1 = 0;
        int index2 = 0;
        int theLen = strlen(inPlace);
        
        for(; index1 <= theLen; index1++)
        {
            if('%' == inPlace[index1] && '\0' != inPlace[index1 + 1] && '\0' != inPlace[index1 + 2])
            {
                int unhex = 0;
                
                if('9' >= inPlace[index1 + 1])
                {
                    unhex |= ((inPlace[index1 + 1] - '0') << 4);
                }
                else
                {
                    unhex |= ((toupper(inPlace[index1 + 1]) - 'A' + 10) << 4);
                }
                
                if('9' >= inPlace[index1 + 2])
                {
                    unhex |= (inPlace[index1 + 2] - '0');
                }
                else
                {
                    unhex |= (toupper(inPlace[index1 + 2]) - 'A' + 10);
                }
                
                index1 += 2;
                inPlace[index1] = unhex;
            }
            
            inPlace[index2++] = inPlace[index1];
        }
    }
}


FormData* FormData_Create(const char* inFormData)
{
    FormData* retval = NULL;

    if(NULL != inFormData)
    {
        FormData* container = NULL;

        /*
        **  Allocate form data container.
        */
        container = (FormData*)calloc(1, sizeof(FormData));
        if(NULL != container)
        {
            /*
            **  Dup the incoming form data.
            */
            container->mStorage = strdup(inFormData);
            if(NULL != container->mStorage)
            {
                char* traverse = NULL;
                unsigned nvpairs = 1;
                unsigned storeLen = 0;

                /*
                **  Count the number of pairs we are going to have.
                **  We do this by counting '&' + 1.
                */
                for(traverse = container->mStorage; '\0' != *traverse; traverse++)
                {
                    if('&' == *traverse)
                    {
                        nvpairs++;
                    }
                }
                storeLen = (unsigned)(traverse - container->mStorage);

                /*
                **  Allocate space for our names and values.
                */
                container->mNArray = (char**)calloc(nvpairs * 2, sizeof(char*));
                if(NULL != container->mNArray)
                {
                    char* amp = NULL;
                    char* equ = NULL;

                    container->mVArray = &container->mNArray[nvpairs];

                    /*
                    **  Go back over the storage.
                    **  Fill in the names and values as we go.
                    **  Terminate on dividing '=' and '&' characters.
                    **  Increase the count of items as we go.
                    */
                    for(traverse = container->mStorage; NULL != traverse; container->mNVCount++)
                    {
                        container->mNArray[container->mNVCount] = traverse;

                        amp = strchr(traverse, '&');
                        equ = strchr(traverse, '=');
                        traverse = NULL;

                        if(NULL != equ && (NULL == amp || equ < amp))
                        {
                            *equ++ = '\0';

                            container->mVArray[container->mNVCount] = equ;
                        }
                        else
                        {
                            container->mVArray[container->mNVCount] = (container->mStorage + storeLen);
                        }

                        if(NULL != amp)
                        {
                            *amp++ = '\0';

                            traverse = amp;
                        }

                        unhexcape(container->mNArray[container->mNVCount]);
                        unhexcape(container->mVArray[container->mNVCount]);
                    }

                    retval = container;
                }
            }
        }

        /*
        **  If we failed, cleanup.
        */
        if(NULL == retval)
        {
            FormData_Destroy(container);
        }
    }

    return retval;
}


void FormData_Destroy(FormData* inDestroy)
{
    if(NULL != inDestroy)
    {
        unsigned traverse = 0;

        for(traverse = 0; traverse < inDestroy->mNVCount; traverse++)
        {
            if(NULL != inDestroy->mNArray)
            {
                inDestroy->mNArray[traverse] = NULL;
            }
            if(NULL != inDestroy->mVArray)
            {
                inDestroy->mVArray[traverse] = NULL;
            }
        }
        inDestroy->mNVCount = 0;

        if(NULL != inDestroy->mStorage)
        {
            free(inDestroy->mStorage);
            inDestroy->mStorage = NULL;
        }

        if(NULL != inDestroy->mNArray)
        {
            free(inDestroy->mNArray);
            inDestroy->mNArray = NULL;
            inDestroy->mVArray = NULL;
        }

        free(inDestroy);
        inDestroy = NULL;
    }
}