From b7e14e7b79a73f69044d91c6af6cf8b62e82437c Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sun, 11 Mar 2018 16:32:37 +0100 Subject: xml: improve comment parsing note that ---> is officially invalid XML, but we allow it anyway. --- xml.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/xml.c b/xml.c index ec5d05c..bff793d 100644 --- a/xml.c +++ b/xml.c @@ -107,36 +107,41 @@ xml_parseattrs(XMLParser *x) static void xml_parsecomment(XMLParser *x) { - static const char *end = "-->"; size_t datalen = 0, i = 0; - char tmp[4]; int c; if (x->xmlcommentstart) x->xmlcommentstart(x); while ((c = x->getnext()) != EOF) { - if (c == end[i]) { - if (end[++i] == '\0') { /* end */ + if (c == '-' || c == '>') { + if (x->xmlcomment) { x->data[datalen] = '\0'; + x->xmlcomment(x, x->data, datalen); + datalen = 0; + } + } + + if (c == '-') { + if (++i > 2) { if (x->xmlcomment) - x->xmlcomment(x, x->data, datalen); - if (x->xmlcommentend) - x->xmlcommentend(x); - return; + for (; i > 2; i--) + x->xmlcomment(x, "-", 1); + i = 2; } + continue; + } else if (c == '>' && i == 2) { + if (x->xmlcommentend) + x->xmlcommentend(x); + return; } else if (i) { if (x->xmlcomment) { - x->data[datalen] = '\0'; - if (datalen) - x->xmlcomment(x, x->data, datalen); - memcpy(tmp, end, i); - tmp[i] = '\0'; - x->xmlcomment(x, tmp, i); + for (; i > 0; i--) + x->xmlcomment(x, "-", 1); } i = 0; - x->data[0] = c; - datalen = 1; - } else if (datalen < sizeof(x->data) - 1) { + } + + if (datalen < sizeof(x->data) - 1) { x->data[datalen++] = c; } else { x->data[datalen] = '\0'; -- cgit v1.2.3