Bug 626361: Reserve space for call/equality ICs. (r=dmandelin)
[mozilla-central.git] / tools / trace-malloc / formdata.c
blobcc7f2ba36edbb4bb2e79ac48d16eead4286278bc
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is formdata.c code, released
17 * May 9, 2002.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 2002
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
25 * Garrett Arch Blythe, 09-May-2002
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 ** formdata.c
44 ** Play utility to parse up form get data into name value pairs.
47 #include "formdata.h"
49 #include <stdlib.h>
50 #include <string.h>
51 #include <ctype.h>
54 static void unhexcape(char* inPlace)
56 ** Real low tech unhexcaper....
58 ** inPlace string to decode, in place as it were.
61 if(NULL != inPlace)
63 int index1 = 0;
64 int index2 = 0;
65 int theLen = strlen(inPlace);
67 for(; index1 <= theLen; index1++)
69 if('%' == inPlace[index1] && '\0' != inPlace[index1 + 1] && '\0' != inPlace[index1 + 2])
71 int unhex = 0;
73 if('9' >= inPlace[index1 + 1])
75 unhex |= ((inPlace[index1 + 1] - '0') << 4);
77 else
79 unhex |= ((toupper(inPlace[index1 + 1]) - 'A' + 10) << 4);
82 if('9' >= inPlace[index1 + 2])
84 unhex |= (inPlace[index1 + 2] - '0');
86 else
88 unhex |= (toupper(inPlace[index1 + 2]) - 'A' + 10);
91 index1 += 2;
92 inPlace[index1] = unhex;
95 inPlace[index2++] = inPlace[index1];
101 FormData* FormData_Create(const char* inFormData)
103 FormData* retval = NULL;
105 if(NULL != inFormData)
107 FormData* container = NULL;
110 ** Allocate form data container.
112 container = (FormData*)calloc(1, sizeof(FormData));
113 if(NULL != container)
116 ** Dup the incoming form data.
118 container->mStorage = strdup(inFormData);
119 if(NULL != container->mStorage)
121 char* traverse = NULL;
122 unsigned nvpairs = 1;
123 unsigned storeLen = 0;
126 ** Count the number of pairs we are going to have.
127 ** We do this by counting '&' + 1.
129 for(traverse = container->mStorage; '\0' != *traverse; traverse++)
131 if('&' == *traverse)
133 nvpairs++;
136 storeLen = (unsigned)(traverse - container->mStorage);
139 ** Allocate space for our names and values.
141 container->mNArray = (char**)calloc(nvpairs * 2, sizeof(char*));
142 if(NULL != container->mNArray)
144 char* amp = NULL;
145 char* equ = NULL;
147 container->mVArray = &container->mNArray[nvpairs];
150 ** Go back over the storage.
151 ** Fill in the names and values as we go.
152 ** Terminate on dividing '=' and '&' characters.
153 ** Increase the count of items as we go.
155 for(traverse = container->mStorage; NULL != traverse; container->mNVCount++)
157 container->mNArray[container->mNVCount] = traverse;
159 amp = strchr(traverse, '&');
160 equ = strchr(traverse, '=');
161 traverse = NULL;
163 if(NULL != equ && (NULL == amp || equ < amp))
165 *equ++ = '\0';
167 container->mVArray[container->mNVCount] = equ;
169 else
171 container->mVArray[container->mNVCount] = (container->mStorage + storeLen);
174 if(NULL != amp)
176 *amp++ = '\0';
178 traverse = amp;
181 unhexcape(container->mNArray[container->mNVCount]);
182 unhexcape(container->mVArray[container->mNVCount]);
185 retval = container;
191 ** If we failed, cleanup.
193 if(NULL == retval)
195 FormData_Destroy(container);
199 return retval;
203 void FormData_Destroy(FormData* inDestroy)
205 if(NULL != inDestroy)
207 unsigned traverse = 0;
209 for(traverse = 0; traverse < inDestroy->mNVCount; traverse++)
211 if(NULL != inDestroy->mNArray)
213 inDestroy->mNArray[traverse] = NULL;
215 if(NULL != inDestroy->mVArray)
217 inDestroy->mVArray[traverse] = NULL;
220 inDestroy->mNVCount = 0;
222 if(NULL != inDestroy->mStorage)
224 free(inDestroy->mStorage);
225 inDestroy->mStorage = NULL;
228 if(NULL != inDestroy->mNArray)
230 free(inDestroy->mNArray);
231 inDestroy->mNArray = NULL;
232 inDestroy->mVArray = NULL;
235 free(inDestroy);
236 inDestroy = NULL;