diff options
author | Hiltjo Posthuma <hiltjo@codemadness.org> | 2018-03-11 16:32:37 +0100 |
---|---|---|
committer | Hiltjo Posthuma <hiltjo@codemadness.org> | 2018-03-11 16:32:37 +0100 |
commit | b7e14e7b79a73f69044d91c6af6cf8b62e82437c (patch) | |
tree | 3e7e2a451358d750ff94d093aad46752f3490418 | |
parent | 4f40df3788165a137bc60bb6c0fd96af0c248c35 (diff) |
xml: improve comment parsing
note that ---> is officially invalid XML, but we allow it anyway.
-rw-r--r-- | xml.c | 39 |
1 files changed, 22 insertions, 17 deletions
@@ -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'; |