Added better function for escaping strings.

This commit is contained in:
Markus Kauppila 2011-07-01 13:16:51 +03:00
parent 4fdb4d47e7
commit d7bab62d35

View file

@ -134,53 +134,85 @@ PrintOpenTags()
const char * const char *
EscapeString(const char *string) EscapeString(const char *string)
{ {
//const int bufferSize = 4096; // Calculate the size of the escaped string
char buffer[bufferSize]; int totalSize = 0;
memset(buffer, 0, bufferSize);
// prevents the code doing a 'bus error' const int maxCount = SDL_strlen(string);
char *stringBuffer = SDL_malloc(bufferSize);
if(stringBuffer == NULL) {
return NULL;
}
strncpy(stringBuffer, string, bufferSize);
// Ampersand (&) must be first, otherwise it'll mess up the other entities
char *characters[] = {"&", "'", "\"", "<", ">"};
char *entities[] = {"&amp;", "&apos;", "&quot;", "&lt;", "&gt;"};
int maxCount = 5;
int counter = 0; int counter = 0;
for(; counter < maxCount; ++counter) { for(; counter < maxCount; ++counter) {
char *character = characters[counter]; char character = string[counter];
char *entity = entities[counter];
if(strstr(stringBuffer, character) == NULL) switch(character) {
continue; case '&': totalSize += 5; //SDL_strlen("&amp;");
break;
char *token = strtok(stringBuffer, character); case '\'': totalSize += 6; //SDL_strlen("&apos;");
while(token) { break;
char *nextToken = strtok(NULL, character); case '"': totalSize += 6; //SDL_strlen("&quot;");
break;
int bytesLeft = bufferSize - SDL_strlen(buffer); case '<': totalSize += 4; //SDL_strlen("&lt;");
if(bytesLeft) { break;
strncat(buffer, token, bytesLeft); case '>': totalSize += 4; //SDL_strlen("&gt;");
} else { break;
// \! todo there's probably better way to report an error? default:
fprintf(stderr, "xml.c | EscapingString: Buffer is full"); totalSize += 1;
} break;
if(nextToken)
strcat(buffer, entity);
token = nextToken;
} }
memcpy(stringBuffer, buffer, bufferSize);
memset(buffer, 0, bufferSize);
} }
return stringBuffer; char *retBuffer = SDL_malloc(totalSize * sizeof(char));
if(retBuffer == NULL) {
return NULL;
}
// escape the string
int retBufferCounter = 0;
for(counter = 0; counter < maxCount; ++counter) {
char character = string[counter];
switch(character) {
case '&':
retBuffer[retBufferCounter++] = '&';
retBuffer[retBufferCounter++] = 'a';
retBuffer[retBufferCounter++] = 'm';
retBuffer[retBufferCounter++] = 'p';
retBuffer[retBufferCounter++] = ';';
break;
case '\'':
retBuffer[retBufferCounter++] = '&';
retBuffer[retBufferCounter++] = 'a';
retBuffer[retBufferCounter++] = 'p';
retBuffer[retBufferCounter++] = 'o';
retBuffer[retBufferCounter++] = 's';
retBuffer[retBufferCounter++] = ';';
break;
case '"':
retBuffer[retBufferCounter++] = '&';
retBuffer[retBufferCounter++] = 'q';
retBuffer[retBufferCounter++] = 'u';
retBuffer[retBufferCounter++] = 'o';
retBuffer[retBufferCounter++] = 't';
retBuffer[retBufferCounter++] = ';';
break;
case '<':
retBuffer[retBufferCounter++] = '&';
retBuffer[retBufferCounter++] = 'l';
retBuffer[retBufferCounter++] = 't';
retBuffer[retBufferCounter++] = ';';
break;
case '>': totalSize += SDL_strlen("&gt;");
retBuffer[retBufferCounter++] = '&';
retBuffer[retBufferCounter++] = 'g';
retBuffer[retBufferCounter++] = 't';
retBuffer[retBufferCounter++] = ';';
break;
default:
retBuffer[retBufferCounter++] = character;
break;
}
}
return retBuffer;
} }