msEncodeHTMLEntities() ** ** Return a copy of string after replacing some problematic chars with their ** HTML entity equivalents. ** ** The replacements performed are: ** '&' -> "&", '"' -> """, '<' -> "<" and '>' -> ">" **/
| 1155 | ** '&' -> "&", '"' -> """, '<' -> "<" and '>' -> ">" |
| 1156 | **/ |
| 1157 | char *msEncodeHTMLEntities(const char *string) |
| 1158 | { |
| 1159 | int buflen, i; |
| 1160 | char *newstring; |
| 1161 | const char *c; |
| 1162 | |
| 1163 | if(string == NULL) |
| 1164 | return NULL; |
| 1165 | |
| 1166 | /* Start with 100 extra chars for replacements... */ |
| 1167 | /* should be good enough for most cases */ |
| 1168 | buflen = strlen(string) + 100; |
| 1169 | newstring = (char*)malloc(buflen+1); |
| 1170 | MS_CHECK_ALLOC(newstring, buflen+1, NULL); |
| 1171 | |
| 1172 | for(i=0, c=string; *c != '\0'; c++) |
| 1173 | { |
| 1174 | /* Need to realloc buffer? */ |
| 1175 | if (i+6 > buflen) |
| 1176 | { |
| 1177 | /* If we had to realloc then this string must contain several */ |
| 1178 | /* entities... so let's go with twice the previous buffer size */ |
| 1179 | buflen *= 2; |
| 1180 | newstring = (char*)realloc(newstring, buflen+1); |
| 1181 | MS_CHECK_ALLOC(newstring, buflen+1, NULL); |
| 1182 | } |
| 1183 | |
| 1184 | switch(*c) |
| 1185 | { |
| 1186 | case '&': |
| 1187 | strcpy(newstring+i, "&"); |
| 1188 | i += 5; |
| 1189 | break; |
| 1190 | case '<': |
| 1191 | strcpy(newstring+i, "<"); |
| 1192 | i += 4; |
| 1193 | break; |
| 1194 | case '>': |
| 1195 | strcpy(newstring+i, ">"); |
| 1196 | i += 4; |
| 1197 | break; |
| 1198 | case '"': |
| 1199 | strcpy(newstring+i, """); |
| 1200 | i += 6; |
| 1201 | break; |
| 1202 | case '\'': |
| 1203 | strcpy(newstring+i, "'"); /* changed from ' and i += 6 (bug 1040) */ |
| 1204 | i += 5; |
| 1205 | break; |
| 1206 | default: |
| 1207 | newstring[i++] = *c; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | newstring[i++] = '\0'; |
| 1212 | |
| 1213 | return newstring; |
| 1214 | } |
no outgoing calls
no test coverage detected