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
16 * The Original Code is formdata.c code, released
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.
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 ***** */
44 ** Play utility to parse up form get data into name value pairs.
54 static void unhexcape(char* inPlace
)
56 ** Real low tech unhexcaper....
58 ** inPlace string to decode, in place as it were.
65 int theLen
= strlen(inPlace
);
67 for(; index1
<= theLen
; index1
++)
69 if('%' == inPlace
[index1
] && '\0' != inPlace
[index1
+ 1] && '\0' != inPlace
[index1
+ 2])
73 if('9' >= inPlace
[index1
+ 1])
75 unhex
|= ((inPlace
[index1
+ 1] - '0') << 4);
79 unhex
|= ((toupper(inPlace
[index1
+ 1]) - 'A' + 10) << 4);
82 if('9' >= inPlace
[index1
+ 2])
84 unhex
|= (inPlace
[index1
+ 2] - '0');
88 unhex
|= (toupper(inPlace
[index1
+ 2]) - 'A' + 10);
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
++)
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
)
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
, '=');
163 if(NULL
!= equ
&& (NULL
== amp
|| equ
< amp
))
167 container
->mVArray
[container
->mNVCount
] = equ
;
171 container
->mVArray
[container
->mNVCount
] = (container
->mStorage
+ storeLen
);
181 unhexcape(container
->mNArray
[container
->mNVCount
]);
182 unhexcape(container
->mVArray
[container
->mNVCount
]);
191 ** If we failed, cleanup.
195 FormData_Destroy(container
);
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
;