give XML parser an option to ignore junk at the end

Signed-off-by: Erik Boasson <eb@ilities.com>
This commit is contained in:
Erik Boasson 2019-03-23 13:03:27 +01:00
parent 431f70a2e9
commit f65d07fb14
2 changed files with 9 additions and 1 deletions

View file

@ -38,6 +38,7 @@ extern "C" {
DDS_EXPORT struct ut_xmlpState *ut_xmlpNewFile (FILE *fp, void *varg, const struct ut_xmlpCallbacks *cb); DDS_EXPORT struct ut_xmlpState *ut_xmlpNewFile (FILE *fp, void *varg, const struct ut_xmlpCallbacks *cb);
DDS_EXPORT struct ut_xmlpState *ut_xmlpNewString (const char *string, void *varg, const struct ut_xmlpCallbacks *cb); DDS_EXPORT struct ut_xmlpState *ut_xmlpNewString (const char *string, void *varg, const struct ut_xmlpCallbacks *cb);
DDS_EXPORT void ut_xmlpSetRequireEOF (struct ut_xmlpState *st, int require_eof);
DDS_EXPORT void ut_xmlpFree (struct ut_xmlpState *st); DDS_EXPORT void ut_xmlpFree (struct ut_xmlpState *st);
DDS_EXPORT int ut_xmlpParse (struct ut_xmlpState *st); DDS_EXPORT int ut_xmlpParse (struct ut_xmlpState *st);

View file

@ -49,6 +49,7 @@ struct ut_xmlpState {
size_t tpescp; /* still escape sequences in tpescp .. tpp */ size_t tpescp; /* still escape sequences in tpescp .. tpp */
int nest; /* current nesting level */ int nest; /* current nesting level */
void *varg; /* user argument to callback functions */ void *varg; /* user argument to callback functions */
int require_eof; /* if false, junk may follow top-level closing tag */
struct ut_xmlpCallbacks cb; /* user-supplied callbacks (or stubs) */ struct ut_xmlpCallbacks cb; /* user-supplied callbacks (or stubs) */
}; };
@ -107,6 +108,7 @@ static void ut_xmlpNewCommon (struct ut_xmlpState *st)
st->peekpayload = NULL; st->peekpayload = NULL;
st->nest = 0; st->nest = 0;
st->error = 0; st->error = 0;
st->require_eof = 1;
} }
static void ut_xmlpNewSetCB (struct ut_xmlpState *st, void *varg, const struct ut_xmlpCallbacks *cb) static void ut_xmlpNewSetCB (struct ut_xmlpState *st, void *varg, const struct ut_xmlpCallbacks *cb)
@ -146,6 +148,11 @@ struct ut_xmlpState *ut_xmlpNewString (const char *string, void *varg, const str
return st; return st;
} }
void ut_xmlpSetRequireEOF (struct ut_xmlpState *st, int require_eof)
{
st->require_eof = require_eof;
}
void ut_xmlpFree (struct ut_xmlpState *st) void ut_xmlpFree (struct ut_xmlpState *st)
{ {
if (st->fp != NULL) { if (st->fp != NULL) {
@ -697,7 +704,7 @@ int ut_xmlpParse (struct ut_xmlpState *st)
return 0; return 0;
} else { } else {
int ret = parse_element (st, 0); int ret = parse_element (st, 0);
if (ret < 0 || next_token (st, NULL) == TOK_EOF) { if (ret < 0|| !st->require_eof || next_token (st, NULL) == TOK_EOF ) {
return ret; return ret;
} else { } else {
return -1; return -1;