Commit e6225c94 authored by Markus Kauppila's avatar Markus Kauppila

Added escaping special characters to their corresponding

entities in XML output.
parent 2dd346fa
...@@ -31,7 +31,6 @@ LogGenericOutput(const char *message) ...@@ -31,7 +31,6 @@ LogGenericOutput(const char *message)
fflush(stderr); fflush(stderr);
} }
/*! /*!
* Test app for logging functionality * Test app for logging functionality
*/ */
...@@ -66,15 +65,12 @@ main(int argc, char *argv[]) ...@@ -66,15 +65,12 @@ main(int argc, char *argv[])
Log = PlainLog; Log = PlainLog;
} }
RunStarted(LogGenericOutput, "some_data_here", 0); RunStarted(LogGenericOutput, "some_<data_>here&here", 0);
SuiteStarted("Suite data here", 0); SuiteStarted("Suite data here", 0);
TestStarted("test1", "suite", "desc", 0); TestStarted("test1", "suite", "desc", 0);
TestEnded("test1", "suite", 0, 0, 0, 0); TestEnded("test1", "suite", 0, 0, 0, 0);
//XMLTestStarted("test2", "desc", 0);
//XMLTestEnded("test2", "desc", 0, 0, 0, 0);
SuiteEnded(0, 0, 0, 0.0f, 0); SuiteEnded(0, 0, 0, 0.0f, 0);
RunEnded(0, 0, 0, 0, 0, 0); RunEnded(0, 0, 0, 0, 0, 0);
......
...@@ -104,6 +104,57 @@ PrintOpenTags() ...@@ -104,6 +104,57 @@ PrintOpenTags()
} }
} }
/*!
* Converts the special characters ', ", <, >, and & to
* corresponding entities: &apos; &quot; &lt; &gt; and &amp;
*
* \param string String to be escaped
* \return Escaped string
*/
const char *EscapeString(const char *string) {
const int bufferSize = 4096;
char buffer[bufferSize];
memset(buffer, 0, bufferSize);
// prevents the code doing a 'bus error'
char stringBuffer[bufferSize];
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;
for(; counter < maxCount; ++counter) {
char *character = characters[counter];
char *entity = entities[counter];
if(strstr(stringBuffer, character) == NULL)
continue;
char *token = strtok(stringBuffer, character);
while(token) {
char *nextToken = strtok(NULL, character);
//! \todo use strncat and count the bytes left in the buffer
strcat(buffer, token);
if(nextToken)
strcat(buffer, entity);
token = nextToken;
}
memcpy(stringBuffer, buffer, bufferSize);
memset(buffer, 0, bufferSize);
}
return stringBuffer;
}
/* /*
=================== ===================
...@@ -131,7 +182,6 @@ XMLOpenDocument(const char *rootTag, LogOutputFp log) ...@@ -131,7 +182,6 @@ XMLOpenDocument(const char *rootTag, LogOutputFp log)
snprintf(buffer, bufferSize, "<%s>", rootTag); snprintf(buffer, bufferSize, "<%s>", rootTag);
logger(buffer); logger(buffer);
// add open tag
AddOpenTag(rootTag); AddOpenTag(rootTag);
root = rootTag; // it's fine, as long as rootTag points to static memory? root = rootTag; // it's fine, as long as rootTag points to static memory?
...@@ -167,8 +217,10 @@ XMLOpenElementWithAttribute(const char *tag, Attribute *attribute) ...@@ -167,8 +217,10 @@ XMLOpenElementWithAttribute(const char *tag, Attribute *attribute)
void void
XMLAddContent(const char *content) XMLAddContent(const char *content)
{ {
const char *escapedContent = EscapeString(content);
memset(buffer, 0, bufferSize); memset(buffer, 0, bufferSize);
snprintf(buffer, bufferSize, "%s", content); snprintf(buffer, bufferSize, "%s", escapedContent);
logger(buffer); logger(buffer);
} }
......
...@@ -26,6 +26,8 @@ ...@@ -26,6 +26,8 @@
void void
XMLRunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime) XMLRunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime)
{ {
//! \todo Giving outputFn to the function is awful, fix it
//! Make the outputting differently
XMLOpenDocument("testlog", outputFn); XMLOpenDocument("testlog", outputFn);
XMLOpenElement("parameters"); XMLOpenElement("parameters");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment