Bumping manifests a=b2g-bump
[gecko.git] / tools / trace-malloc / formdata.c
blobf4660d0db18516f4a8bcb848bdb4d8f863d55397
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /*
8 ** formdata.c
9 **
10 ** Play utility to parse up form get data into name value pairs.
13 #include "formdata.h"
15 #include <stdlib.h>
16 #include <string.h>
17 #include <ctype.h>
20 static void unhexcape(char* inPlace)
22 ** Real low tech unhexcaper....
24 ** inPlace string to decode, in place as it were.
27 if(NULL != inPlace)
29 int index1 = 0;
30 int index2 = 0;
31 int theLen = strlen(inPlace);
33 for(; index1 <= theLen; index1++)
35 if('%' == inPlace[index1] && '\0' != inPlace[index1 + 1] && '\0' != inPlace[index1 + 2])
37 int unhex = 0;
39 if('9' >= inPlace[index1 + 1])
41 unhex |= ((inPlace[index1 + 1] - '0') << 4);
43 else
45 unhex |= ((toupper(inPlace[index1 + 1]) - 'A' + 10) << 4);
48 if('9' >= inPlace[index1 + 2])
50 unhex |= (inPlace[index1 + 2] - '0');
52 else
54 unhex |= (toupper(inPlace[index1 + 2]) - 'A' + 10);
57 index1 += 2;
58 inPlace[index1] = unhex;
61 inPlace[index2++] = inPlace[index1];
67 FormData* FormData_Create(const char* inFormData)
69 FormData* retval = NULL;
71 if(NULL != inFormData)
73 FormData* container = NULL;
76 ** Allocate form data container.
78 container = (FormData*)calloc(1, sizeof(FormData));
79 if(NULL != container)
82 ** Dup the incoming form data.
84 container->mStorage = strdup(inFormData);
85 if(NULL != container->mStorage)
87 char* traverse = NULL;
88 unsigned nvpairs = 1;
89 unsigned storeLen = 0;
92 ** Count the number of pairs we are going to have.
93 ** We do this by counting '&' + 1.
95 for(traverse = container->mStorage; '\0' != *traverse; traverse++)
97 if('&' == *traverse)
99 nvpairs++;
102 storeLen = (unsigned)(traverse - container->mStorage);
105 ** Allocate space for our names and values.
107 container->mNArray = (char**)calloc(nvpairs * 2, sizeof(char*));
108 if(NULL != container->mNArray)
110 char* amp = NULL;
111 char* equ = NULL;
113 container->mVArray = &container->mNArray[nvpairs];
116 ** Go back over the storage.
117 ** Fill in the names and values as we go.
118 ** Terminate on dividing '=' and '&' characters.
119 ** Increase the count of items as we go.
121 for(traverse = container->mStorage; NULL != traverse; container->mNVCount++)
123 container->mNArray[container->mNVCount] = traverse;
125 amp = strchr(traverse, '&');
126 equ = strchr(traverse, '=');
127 traverse = NULL;
129 if(NULL != equ && (NULL == amp || equ < amp))
131 *equ++ = '\0';
133 container->mVArray[container->mNVCount] = equ;
135 else
137 container->mVArray[container->mNVCount] = (container->mStorage + storeLen);
140 if(NULL != amp)
142 *amp++ = '\0';
144 traverse = amp;
147 unhexcape(container->mNArray[container->mNVCount]);
148 unhexcape(container->mVArray[container->mNVCount]);
151 retval = container;
157 ** If we failed, cleanup.
159 if(NULL == retval)
161 FormData_Destroy(container);
165 return retval;
169 void FormData_Destroy(FormData* inDestroy)
171 if(NULL != inDestroy)
173 unsigned traverse = 0;
175 for(traverse = 0; traverse < inDestroy->mNVCount; traverse++)
177 if(NULL != inDestroy->mNArray)
179 inDestroy->mNArray[traverse] = NULL;
181 if(NULL != inDestroy->mVArray)
183 inDestroy->mVArray[traverse] = NULL;
186 inDestroy->mNVCount = 0;
188 if(NULL != inDestroy->mStorage)
190 free(inDestroy->mStorage);
191 inDestroy->mStorage = NULL;
194 if(NULL != inDestroy->mNArray)
196 free(inDestroy->mNArray);
197 inDestroy->mNArray = NULL;
198 inDestroy->mVArray = NULL;
201 free(inDestroy);
202 inDestroy = NULL;