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/. */
10 ** Play utility to parse up form get data into name value pairs.
20 static void unhexcape(char* inPlace
)
22 ** Real low tech unhexcaper....
24 ** inPlace string to decode, in place as it were.
31 int theLen
= strlen(inPlace
);
33 for(; index1
<= theLen
; index1
++)
35 if('%' == inPlace
[index1
] && '\0' != inPlace
[index1
+ 1] && '\0' != inPlace
[index1
+ 2])
39 if('9' >= inPlace
[index1
+ 1])
41 unhex
|= ((inPlace
[index1
+ 1] - '0') << 4);
45 unhex
|= ((toupper(inPlace
[index1
+ 1]) - 'A' + 10) << 4);
48 if('9' >= inPlace
[index1
+ 2])
50 unhex
|= (inPlace
[index1
+ 2] - '0');
54 unhex
|= (toupper(inPlace
[index1
+ 2]) - 'A' + 10);
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
));
82 ** Dup the incoming form data.
84 container
->mStorage
= strdup(inFormData
);
85 if(NULL
!= container
->mStorage
)
87 char* traverse
= NULL
;
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
++)
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
)
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
, '=');
129 if(NULL
!= equ
&& (NULL
== amp
|| equ
< amp
))
133 container
->mVArray
[container
->mNVCount
] = equ
;
137 container
->mVArray
[container
->mNVCount
] = (container
->mStorage
+ storeLen
);
147 unhexcape(container
->mNArray
[container
->mNVCount
]);
148 unhexcape(container
->mVArray
[container
->mNVCount
]);
157 ** If we failed, cleanup.
161 FormData_Destroy(container
);
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
;