summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2019-01-08 20:55:33 +0100
committerHiltjo Posthuma <hiltjo@codemadness.org>2019-01-08 20:55:33 +0100
commitf7a8e6bb06903f11a6c0e1af709c3243edbcf672 (patch)
tree6bd086d168ede88590bf78f46cbe2257baf799b2
parente59ad0561de3a0df90e9445cc73ac14c5cbccdca (diff)
xml: remove unnecesary checks
- reduce amount of data to check. - remove unnecesary checks from (now) internal functions.
-rw-r--r--xml.c45
1 files changed, 16 insertions, 29 deletions
diff --git a/xml.c b/xml.c
index 6586a8c..0525c77 100644
--- 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[] = {
- { "&amp;", '&' },
- { "&lt;", '<' },
- { "&gt;", '>' },
- { "&apos;", '\'' },
- { "&quot;", '"' },
- { "&AMP;", '&' },
- { "&LT;", '<' },
- { "&GT;", '>' },
- { "&APOS;", '\'' },
- { "&QUOT;", '"' }
+ { "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