Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,70 @@ node_creation:;
*/
struct xml_document* xml_parse_document(uint8_t* buffer, size_t length) {

/*
* Pre-processing: Remove XML comments and declarations.
*
* This loop iterates through the buffer and strips out any content between
* `<!--` and `-->` (comments) or `<?` and `?>` (declarations).
* The modification is performed in-place by shifting valid bytes.
*
* @warning This is a naive implementation. It does not check if the comment
* markers are inside strings or attributes. Patterns like <node attr="<!--" />
* will be incorrectly parsed as the start of a comment.
*/
uint32_t document_length = 0;
uint32_t buffer_index = 0;

uint8_t declare_flag = 0;
uint8_t comment_flag = 0;

while (buffer_index < length)
{
if (declare_flag == 0 && comment_flag == 0)
{
memcpy(&buffer[document_length], &buffer[buffer_index], 1);
document_length++;
}

if (buffer_index >= 1)
{
if (!strncmp(&buffer[buffer_index - 1], "<?", 2))
{
declare_flag = 1;
if (comment_flag == 0)
{
document_length -= 2;
}

}

if (!strncmp(&buffer[buffer_index - 1], "?>", 2))
{
declare_flag = 0;
}
}
if (buffer_index >= 3)
{
if (!strncmp(&buffer[buffer_index - 3], "<!--", 4))
{
comment_flag = 1;
if (declare_flag == 0)
{
document_length -= 4;
}
}

if (!strncmp(&buffer[buffer_index - 2], "-->", 3))
{
comment_flag = 0;
}
}

buffer_index++;
}

length = document_length;

/* Initialize parser
*/
struct xml_parser parser = {
Expand Down