Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* |
michael@0 | 8 | ** formdata.c |
michael@0 | 9 | ** |
michael@0 | 10 | ** Play utility to parse up form get data into name value pairs. |
michael@0 | 11 | */ |
michael@0 | 12 | |
michael@0 | 13 | #include "formdata.h" |
michael@0 | 14 | |
michael@0 | 15 | #include <stdlib.h> |
michael@0 | 16 | #include <string.h> |
michael@0 | 17 | #include <ctype.h> |
michael@0 | 18 | |
michael@0 | 19 | |
michael@0 | 20 | static void unhexcape(char* inPlace) |
michael@0 | 21 | /* |
michael@0 | 22 | ** Real low tech unhexcaper.... |
michael@0 | 23 | ** |
michael@0 | 24 | ** inPlace string to decode, in place as it were. |
michael@0 | 25 | */ |
michael@0 | 26 | { |
michael@0 | 27 | if(NULL != inPlace) |
michael@0 | 28 | { |
michael@0 | 29 | int index1 = 0; |
michael@0 | 30 | int index2 = 0; |
michael@0 | 31 | int theLen = strlen(inPlace); |
michael@0 | 32 | |
michael@0 | 33 | for(; index1 <= theLen; index1++) |
michael@0 | 34 | { |
michael@0 | 35 | if('%' == inPlace[index1] && '\0' != inPlace[index1 + 1] && '\0' != inPlace[index1 + 2]) |
michael@0 | 36 | { |
michael@0 | 37 | int unhex = 0; |
michael@0 | 38 | |
michael@0 | 39 | if('9' >= inPlace[index1 + 1]) |
michael@0 | 40 | { |
michael@0 | 41 | unhex |= ((inPlace[index1 + 1] - '0') << 4); |
michael@0 | 42 | } |
michael@0 | 43 | else |
michael@0 | 44 | { |
michael@0 | 45 | unhex |= ((toupper(inPlace[index1 + 1]) - 'A' + 10) << 4); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | if('9' >= inPlace[index1 + 2]) |
michael@0 | 49 | { |
michael@0 | 50 | unhex |= (inPlace[index1 + 2] - '0'); |
michael@0 | 51 | } |
michael@0 | 52 | else |
michael@0 | 53 | { |
michael@0 | 54 | unhex |= (toupper(inPlace[index1 + 2]) - 'A' + 10); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | index1 += 2; |
michael@0 | 58 | inPlace[index1] = unhex; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | inPlace[index2++] = inPlace[index1]; |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | |
michael@0 | 67 | FormData* FormData_Create(const char* inFormData) |
michael@0 | 68 | { |
michael@0 | 69 | FormData* retval = NULL; |
michael@0 | 70 | |
michael@0 | 71 | if(NULL != inFormData) |
michael@0 | 72 | { |
michael@0 | 73 | FormData* container = NULL; |
michael@0 | 74 | |
michael@0 | 75 | /* |
michael@0 | 76 | ** Allocate form data container. |
michael@0 | 77 | */ |
michael@0 | 78 | container = (FormData*)calloc(1, sizeof(FormData)); |
michael@0 | 79 | if(NULL != container) |
michael@0 | 80 | { |
michael@0 | 81 | /* |
michael@0 | 82 | ** Dup the incoming form data. |
michael@0 | 83 | */ |
michael@0 | 84 | container->mStorage = strdup(inFormData); |
michael@0 | 85 | if(NULL != container->mStorage) |
michael@0 | 86 | { |
michael@0 | 87 | char* traverse = NULL; |
michael@0 | 88 | unsigned nvpairs = 1; |
michael@0 | 89 | unsigned storeLen = 0; |
michael@0 | 90 | |
michael@0 | 91 | /* |
michael@0 | 92 | ** Count the number of pairs we are going to have. |
michael@0 | 93 | ** We do this by counting '&' + 1. |
michael@0 | 94 | */ |
michael@0 | 95 | for(traverse = container->mStorage; '\0' != *traverse; traverse++) |
michael@0 | 96 | { |
michael@0 | 97 | if('&' == *traverse) |
michael@0 | 98 | { |
michael@0 | 99 | nvpairs++; |
michael@0 | 100 | } |
michael@0 | 101 | } |
michael@0 | 102 | storeLen = (unsigned)(traverse - container->mStorage); |
michael@0 | 103 | |
michael@0 | 104 | /* |
michael@0 | 105 | ** Allocate space for our names and values. |
michael@0 | 106 | */ |
michael@0 | 107 | container->mNArray = (char**)calloc(nvpairs * 2, sizeof(char*)); |
michael@0 | 108 | if(NULL != container->mNArray) |
michael@0 | 109 | { |
michael@0 | 110 | char* amp = NULL; |
michael@0 | 111 | char* equ = NULL; |
michael@0 | 112 | |
michael@0 | 113 | container->mVArray = &container->mNArray[nvpairs]; |
michael@0 | 114 | |
michael@0 | 115 | /* |
michael@0 | 116 | ** Go back over the storage. |
michael@0 | 117 | ** Fill in the names and values as we go. |
michael@0 | 118 | ** Terminate on dividing '=' and '&' characters. |
michael@0 | 119 | ** Increase the count of items as we go. |
michael@0 | 120 | */ |
michael@0 | 121 | for(traverse = container->mStorage; NULL != traverse; container->mNVCount++) |
michael@0 | 122 | { |
michael@0 | 123 | container->mNArray[container->mNVCount] = traverse; |
michael@0 | 124 | |
michael@0 | 125 | amp = strchr(traverse, '&'); |
michael@0 | 126 | equ = strchr(traverse, '='); |
michael@0 | 127 | traverse = NULL; |
michael@0 | 128 | |
michael@0 | 129 | if(NULL != equ && (NULL == amp || equ < amp)) |
michael@0 | 130 | { |
michael@0 | 131 | *equ++ = '\0'; |
michael@0 | 132 | |
michael@0 | 133 | container->mVArray[container->mNVCount] = equ; |
michael@0 | 134 | } |
michael@0 | 135 | else |
michael@0 | 136 | { |
michael@0 | 137 | container->mVArray[container->mNVCount] = (container->mStorage + storeLen); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | if(NULL != amp) |
michael@0 | 141 | { |
michael@0 | 142 | *amp++ = '\0'; |
michael@0 | 143 | |
michael@0 | 144 | traverse = amp; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | unhexcape(container->mNArray[container->mNVCount]); |
michael@0 | 148 | unhexcape(container->mVArray[container->mNVCount]); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | retval = container; |
michael@0 | 152 | } |
michael@0 | 153 | } |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | /* |
michael@0 | 157 | ** If we failed, cleanup. |
michael@0 | 158 | */ |
michael@0 | 159 | if(NULL == retval) |
michael@0 | 160 | { |
michael@0 | 161 | FormData_Destroy(container); |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | return retval; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | |
michael@0 | 169 | void FormData_Destroy(FormData* inDestroy) |
michael@0 | 170 | { |
michael@0 | 171 | if(NULL != inDestroy) |
michael@0 | 172 | { |
michael@0 | 173 | unsigned traverse = 0; |
michael@0 | 174 | |
michael@0 | 175 | for(traverse = 0; traverse < inDestroy->mNVCount; traverse++) |
michael@0 | 176 | { |
michael@0 | 177 | if(NULL != inDestroy->mNArray) |
michael@0 | 178 | { |
michael@0 | 179 | inDestroy->mNArray[traverse] = NULL; |
michael@0 | 180 | } |
michael@0 | 181 | if(NULL != inDestroy->mVArray) |
michael@0 | 182 | { |
michael@0 | 183 | inDestroy->mVArray[traverse] = NULL; |
michael@0 | 184 | } |
michael@0 | 185 | } |
michael@0 | 186 | inDestroy->mNVCount = 0; |
michael@0 | 187 | |
michael@0 | 188 | if(NULL != inDestroy->mStorage) |
michael@0 | 189 | { |
michael@0 | 190 | free(inDestroy->mStorage); |
michael@0 | 191 | inDestroy->mStorage = NULL; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | if(NULL != inDestroy->mNArray) |
michael@0 | 195 | { |
michael@0 | 196 | free(inDestroy->mNArray); |
michael@0 | 197 | inDestroy->mNArray = NULL; |
michael@0 | 198 | inDestroy->mVArray = NULL; |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | free(inDestroy); |
michael@0 | 202 | inDestroy = NULL; |
michael@0 | 203 | } |
michael@0 | 204 | } |