commit f7a8e6bb06903f11a6c0e1af709c3243edbcf672
parent e59ad0561de3a0df90e9445cc73ac14c5cbccdca
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Tue, 8 Jan 2019 20:55:33 +0100
xml: remove unnecesary checks
- reduce amount of data to check.
- remove unnecesary checks from (now) internal functions.
Diffstat:
M | xml.c | | | 45 | ++++++++++++++++----------------------------- |
1 file changed, 16 insertions(+), 29 deletions(-)
diff --git a/xml.c b/xml.c
@@ -247,19 +247,19 @@ static int
namedentitytostr(const char *e, char *buf, size_t bufsiz)
{
static const struct {
- char *entity;
+ const char *entity;
int c;
} entities[] = {
- { "&", '&' },
- { "<", '<' },
- { ">", '>' },
- { "'", '\'' },
- { """, '"' },
- { "&", '&' },
- { "<", '<' },
- { ">", '>' },
- { "&APOS;", '\'' },
- { """, '"' }
+ { "amp;", '&' },
+ { "lt;", '<' },
+ { "gt;", '>' },
+ { "apos;", '\'' },
+ { "quot;", '"' },
+ { "AMP;", '&' },
+ { "LT;", '<' },
+ { "GT;", '>' },
+ { "APOS;", '\'' },
+ { "QUOT;", '"' }
};
size_t i;
@@ -267,10 +267,6 @@ namedentitytostr(const char *e, char *buf, size_t bufsiz)
if (bufsiz < 2)
return -1;
- /* doesn't start with &: can't match */
- if (*e != '&')
- return 0;
-
for (i = 0; i < sizeof(entities) / sizeof(*entities); i++) {
if (!strcmp(e, entities[i].entity)) {
buf[0] = entities[i].c;
@@ -292,12 +288,6 @@ numericentitytostr(const char *e, char *buf, size_t bufsiz)
if (bufsiz < 5)
return -1;
- /* not a numeric entity */
- if (e[0] != '&' || e[1] != '#')
- return 0;
-
- /* e[1] == '#', numeric / hexadecimal entity */
- e += 2; /* skip "&#" */
errno = 0;
/* hex (16) or decimal (10) */
if (*e == 'x')
@@ -318,17 +308,14 @@ numericentitytostr(const char *e, char *buf, size_t bufsiz)
int
xml_entitytostr(const char *e, char *buf, size_t bufsiz)
{
- /* buffer is too small */
- if (bufsiz < 5)
- return -1;
/* doesn't start with & */
if (e[0] != '&')
return 0;
- /* named entity */
- if (e[1] != '#')
- return namedentitytostr(e, buf, bufsiz);
- else /* numeric entity */
- return numericentitytostr(e, buf, bufsiz);
+ /* numeric entity */
+ if (e[1] == '#')
+ return numericentitytostr(e + 2, buf, bufsiz);
+ else /* named entity */
+ return namedentitytostr(e + 1, buf, bufsiz);
}
void