diff --git a/src/xml.c b/src/xml.c index 8b6b559..7540023 100644 --- a/src/xml.c +++ b/src/xml.c @@ -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 + * `` (comments) or `` (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 + * 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 = 0; + } + } + if (buffer_index >= 3) + { + if (!strncmp(&buffer[buffer_index - 3], "", 3)) + { + comment_flag = 0; + } + } + + buffer_index++; + } + + length = document_length; + /* Initialize parser */ struct xml_parser parser = {