| 1 |
/* |
| 2 |
Wrapper interface to XML parser |
| 3 |
Copyright (C) 1999-2007, 2009, Joe Orton <[email protected]> |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Library General Public |
| 7 |
License as published by the Free Software Foundation; either |
| 8 |
version 2 of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Library General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Library General Public |
| 16 |
License along with this library; if not, write to the Free |
| 17 |
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 18 |
MA 02111-1307, USA |
| 19 |
|
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#pragma GCC diagnostic ignored "-Wpointer-sign" |
| 25 |
|
| 26 |
#ifdef HAVE_STDLIB_H |
| 27 |
#include <stdlib.h> |
| 28 |
#endif |
| 29 |
#ifdef HAVE_STRING_H |
| 30 |
#include <string.h> |
| 31 |
#endif |
| 32 |
#ifdef HAVE_STRINGS_H |
| 33 |
#include <strings.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
#include "ne_internal.h" |
| 37 |
|
| 38 |
#include "ne_alloc.h" |
| 39 |
#include "ne_xml.h" |
| 40 |
#include "ne_utils.h" |
| 41 |
#include "ne_string.h" |
| 42 |
|
| 43 |
#if defined(HAVE_EXPAT) |
| 44 |
/* expat support: */ |
| 45 |
#ifdef HAVE_XMLPARSE_H |
| 46 |
#include "xmlparse.h" |
| 47 |
#else |
| 48 |
#include <expat.h> |
| 49 |
#endif |
| 50 |
typedef XML_Char ne_xml_char; |
| 51 |
|
| 52 |
#if !defined(XML_MAJOR_VERSION) |
| 53 |
#define NEED_BOM_HANDLING |
| 54 |
#elif XML_MAJOR_VERSION < 2 && XML_MINOR_VERSION == 95 && XML_MICRO_VERSION < 2 |
| 55 |
#define NEED_BOM_HANDLING |
| 56 |
#endif |
| 57 |
|
| 58 |
#elif defined(HAVE_LIBXML) |
| 59 |
/* libxml2 support: */ |
| 60 |
#include <libxml/xmlversion.h> |
| 61 |
#include <libxml/parser.h> |
| 62 |
#include <pthread.h> |
| 63 |
typedef xmlChar ne_xml_char; |
| 64 |
|
| 65 |
#if LIBXML_VERSION < 20619 |
| 66 |
/* 2.6.19 and earlier have broken BOM handling */ |
| 67 |
#define NEED_BOM_HANDLING |
| 68 |
#endif |
| 69 |
#else /* not HAVE_LIBXML */ |
| 70 |
# error need an XML parser |
| 71 |
#endif /* not HAVE_EXPAT */ |
| 72 |
|
| 73 |
/* Approx. one screen of text: */ |
| 74 |
#define ERR_SIZE (2048) |
| 75 |
|
| 76 |
struct handler { |
| 77 |
ne_xml_startelm_cb *startelm_cb; /* start-element callback */ |
| 78 |
ne_xml_endelm_cb *endelm_cb; /* end-element callback */ |
| 79 |
ne_xml_cdata_cb *cdata_cb; /* character-data callback. */ |
| 80 |
void *userdata; /* userdata for the above. */ |
| 81 |
struct handler *next; /* next handler in stack. */ |
| 82 |
}; |
| 83 |
|
| 84 |
#ifdef HAVE_LIBXML |
| 85 |
static void sax_error(void *ctx, const char *msg, ...); |
| 86 |
#endif |
| 87 |
|
| 88 |
struct element { |
| 89 |
const ne_xml_char *nspace; |
| 90 |
ne_xml_char *name; |
| 91 |
|
| 92 |
int state; /* opaque state integer */ |
| 93 |
|
| 94 |
/* Namespaces declared in this element */ |
| 95 |
ne_xml_char *default_ns; /* A default namespace */ |
| 96 |
struct namespace *nspaces; /* List of other namespace scopes */ |
| 97 |
|
| 98 |
struct handler *handler; /* Handler for this element */ |
| 99 |
|
| 100 |
struct element *parent; /* parent element, or NULL */ |
| 101 |
}; |
| 102 |
|
| 103 |
/* We pass around a ne_xml_parser as the userdata in the parsing |
| 104 |
* library. This maintains the current state of the parse and various |
| 105 |
* other bits and bobs. Within the parse, we store the current branch |
| 106 |
* of the tree, i.e., the current element and all its parents, up to |
| 107 |
* the root, but nothing other than that. */ |
| 108 |
struct ne_xml_parser_s { |
| 109 |
struct element *root; /* the root of the document */ |
| 110 |
struct element *current; /* current element in the branch */ |
| 111 |
struct handler *top_handlers; /* always points at the |
| 112 |
* handler on top of the stack. */ |
| 113 |
int failure; /* zero whilst parse should continue */ |
| 114 |
int prune; /* if non-zero, depth within a dead branch */ |
| 115 |
|
| 116 |
#ifdef NEED_BOM_HANDLING |
| 117 |
int bom_pos; |
| 118 |
#endif |
| 119 |
|
| 120 |
#ifdef HAVE_EXPAT |
| 121 |
XML_Parser parser; |
| 122 |
char *encoding; |
| 123 |
#else |
| 124 |
xmlParserCtxtPtr parser; |
| 125 |
#endif |
| 126 |
char error[ERR_SIZE]; |
| 127 |
}; |
| 128 |
|
| 129 |
/* The callback handlers */ |
| 130 |
static void start_element(void *userdata, const ne_xml_char *name, const ne_xml_char **atts); |
| 131 |
static void end_element(void *userdata, const ne_xml_char *name); |
| 132 |
static void char_data(void *userdata, const ne_xml_char *cdata, int len); |
| 133 |
static const char *resolve_nspace(const struct element *elm, |
| 134 |
const char *prefix, size_t pfxlen); |
| 135 |
|
| 136 |
/* Linked list of namespace scopes */ |
| 137 |
struct namespace { |
| 138 |
ne_xml_char *name; |
| 139 |
ne_xml_char *uri; |
| 140 |
struct namespace *next; |
| 141 |
}; |
| 142 |
|
| 143 |
#ifdef HAVE_LIBXML |
| 144 |
|
| 145 |
/* Could be const as far as we care, but libxml doesn't want that */ |
| 146 |
static xmlSAXHandler sax_handler = { |
| 147 |
NULL, /* internalSubset */ |
| 148 |
NULL, /* isStandalone */ |
| 149 |
NULL, /* hasInternalSubset */ |
| 150 |
NULL, /* hasExternalSubset */ |
| 151 |
NULL, /* resolveEntity */ |
| 152 |
NULL, /* getEntity */ |
| 153 |
NULL, /* entityDecl */ |
| 154 |
NULL, /* notationDecl */ |
| 155 |
NULL, /* attributeDecl */ |
| 156 |
NULL, /* elementDecl */ |
| 157 |
NULL, /* unparsedEntityDecl */ |
| 158 |
NULL, /* setDocumentLocator */ |
| 159 |
NULL, /* startDocument */ |
| 160 |
NULL, /* endDocument */ |
| 161 |
start_element, /* startElement */ |
| 162 |
end_element, /* endElement */ |
| 163 |
NULL, /* reference */ |
| 164 |
char_data, /* characters */ |
| 165 |
NULL, /* ignorableWhitespace */ |
| 166 |
NULL, /* processingInstruction */ |
| 167 |
NULL, /* comment */ |
| 168 |
NULL, /* xmlParserWarning */ |
| 169 |
sax_error, /* xmlParserError */ |
| 170 |
sax_error, /* fatal error (never called by libxml2?) */ |
| 171 |
NULL, /* getParameterEntity */ |
| 172 |
char_data /* cdataBlock */ |
| 173 |
}; |
| 174 |
|
| 175 |
/* empty attributes array to mimic expat behaviour */ |
| 176 |
static const char *const empty_atts[] = {NULL, NULL}; |
| 177 |
|
| 178 |
/* macro for determining the attributes array to pass */ |
| 179 |
#define PASS_ATTS(atts) (atts ? (const char **)(atts) : empty_atts) |
| 180 |
|
| 181 |
/* locker for libxml2 thread safety */ |
| 182 |
static pthread_once_t libxml_is_initialized = PTHREAD_ONCE_INIT; |
| 183 |
|
| 184 |
void initialize_libxml() |
| 185 |
{ |
| 186 |
xmlInitParser(); |
| 187 |
} |
| 188 |
|
| 189 |
#else |
| 190 |
|
| 191 |
#define PASS_ATTS(atts) ((const char **)(atts)) |
| 192 |
|
| 193 |
/* XML declaration callback for expat. */ |
| 194 |
static void decl_handler(void *userdata, |
| 195 |
const XML_Char *version, const XML_Char *encoding, |
| 196 |
int standalone) |
| 197 |
{ |
| 198 |
ne_xml_parser *p = userdata; |
| 199 |
if (encoding) p->encoding = ne_strdup(encoding); |
| 200 |
} |
| 201 |
|
| 202 |
#endif /* HAVE_LIBXML */ |
| 203 |
|
| 204 |
int ne_xml_currentline(ne_xml_parser *p) |
| 205 |
{ |
| 206 |
#ifdef HAVE_EXPAT |
| 207 |
return XML_GetCurrentLineNumber(p->parser); |
| 208 |
#else |
| 209 |
return p->parser->input->line; |
| 210 |
#endif |
| 211 |
} |
| 212 |
|
| 213 |
const char *ne_xml_doc_encoding(const ne_xml_parser *p) |
| 214 |
{ |
| 215 |
#ifdef HAVE_LIBXML |
| 216 |
return p->parser->encoding; |
| 217 |
#else |
| 218 |
return p->encoding; |
| 219 |
#endif |
| 220 |
} |
| 221 |
|
| 222 |
/* The first character of the REC-xml-names "NCName" rule excludes |
| 223 |
* "Digit | '.' | '-' | '_' | CombiningChar | Extender"; the XML |
| 224 |
* parser will not enforce this rule in a namespace declaration since |
| 225 |
* it treats the entire attribute name as a REC-xml "Name" rule. It's |
| 226 |
* too hard to check for all of CombiningChar | Digit | Extender here, |
| 227 |
* but the valid_ncname_ch1 macro catches some of the rest. */ |
| 228 |
|
| 229 |
/* Return non-zero if 'ch' is an invalid start character for an NCName: */ |
| 230 |
#define invalid_ncname_ch1(ch) ((ch) == '\0' || strchr("-.0123456789", (ch)) != NULL) |
| 231 |
|
| 232 |
/* Subversion repositories have been deployed which use property names |
| 233 |
* marshalled as NCNames including a colon character; these should |
| 234 |
* also be rejected but will be allowed for the time being. */ |
| 235 |
#define invalid_ncname(xn) (invalid_ncname_ch1((xn)[0])) |
| 236 |
|
| 237 |
/* Extract the namespace prefix declarations from 'atts'. */ |
| 238 |
static int declare_nspaces(ne_xml_parser *p, struct element *elm, |
| 239 |
const ne_xml_char **atts) |
| 240 |
{ |
| 241 |
int n; |
| 242 |
|
| 243 |
for (n = 0; atts && atts[n]; n += 2) { |
| 244 |
if (strcmp(atts[n], "xmlns") == 0) { |
| 245 |
/* New default namespace */ |
| 246 |
elm->default_ns = ne_strdup(atts[n+1]); |
| 247 |
} else if (strncmp(atts[n], "xmlns:", 6) == 0) { |
| 248 |
struct namespace *ns; |
| 249 |
|
| 250 |
/* Reject some invalid NCNames as namespace prefix, and an |
| 251 |
* empty URI as the namespace URI */ |
| 252 |
if (invalid_ncname(atts[n] + 6) || atts[n+1][0] == '\0') { |
| 253 |
ne_snprintf(p->error, ERR_SIZE, |
| 254 |
("XML parse error at line %d: invalid namespace " |
| 255 |
"declaration"), ne_xml_currentline(p)); |
| 256 |
return -1; |
| 257 |
} |
| 258 |
|
| 259 |
/* New namespace scope */ |
| 260 |
ns = ne_calloc(sizeof(*ns)); |
| 261 |
ns->next = elm->nspaces; |
| 262 |
elm->nspaces = ns; |
| 263 |
ns->name = ne_strdup(atts[n]+6); /* skip the xmlns= */ |
| 264 |
ns->uri = ne_strdup(atts[n+1]); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
return 0; |
| 269 |
} |
| 270 |
|
| 271 |
/* Expand an XML qualified name, which may include a namespace prefix |
| 272 |
* as well as the local part. */ |
| 273 |
static int expand_qname(ne_xml_parser *p, struct element *elm, |
| 274 |
const ne_xml_char *qname) |
| 275 |
{ |
| 276 |
const ne_xml_char *pfx; |
| 277 |
|
| 278 |
pfx = strchr(qname, ':'); |
| 279 |
if (pfx == NULL) { |
| 280 |
struct element *e = elm; |
| 281 |
|
| 282 |
/* Find default namespace; guaranteed to terminate as the root |
| 283 |
* element always has default_ns="". */ |
| 284 |
while (e->default_ns == NULL) |
| 285 |
e = e->parent; |
| 286 |
|
| 287 |
elm->name = ne_strdup(qname); |
| 288 |
elm->nspace = e->default_ns; |
| 289 |
} else if (invalid_ncname(pfx + 1) || qname == pfx) { |
| 290 |
ne_snprintf(p->error, ERR_SIZE, |
| 291 |
_("XML parse error at line %d: invalid element name"), |
| 292 |
ne_xml_currentline(p)); |
| 293 |
return -1; |
| 294 |
} else { |
| 295 |
const char *uri = resolve_nspace(elm, qname, pfx-qname); |
| 296 |
|
| 297 |
if (uri) { |
| 298 |
elm->name = ne_strdup(pfx+1); |
| 299 |
elm->nspace = uri; |
| 300 |
} else { |
| 301 |
ne_snprintf(p->error, ERR_SIZE, |
| 302 |
("XML parse error at line %d: undeclared namespace prefix"), |
| 303 |
ne_xml_currentline(p)); |
| 304 |
return -1; |
| 305 |
} |
| 306 |
} |
| 307 |
return 0; |
| 308 |
} |
| 309 |
|
| 310 |
/* Called with the start of a new element. */ |
| 311 |
static void start_element(void *userdata, const ne_xml_char *name, |
| 312 |
const ne_xml_char **atts) |
| 313 |
{ |
| 314 |
ne_xml_parser *p = userdata; |
| 315 |
struct element *elm; |
| 316 |
struct handler *hand; |
| 317 |
int state = NE_XML_DECLINE; |
| 318 |
|
| 319 |
if (p->failure) return; |
| 320 |
|
| 321 |
if (p->prune) { |
| 322 |
p->prune++; |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
/* Create a new element */ |
| 327 |
elm = ne_calloc(sizeof *elm); |
| 328 |
elm->parent = p->current; |
| 329 |
p->current = elm; |
| 330 |
|
| 331 |
if (declare_nspaces(p, elm, atts) || expand_qname(p, elm, name)) { |
| 332 |
p->failure = 1; |
| 333 |
return; |
| 334 |
} |
| 335 |
|
| 336 |
/* Find a handler which will accept this element (or abort the parse) */ |
| 337 |
for (hand = elm->parent->handler; hand && state == NE_XML_DECLINE; |
| 338 |
hand = hand->next) { |
| 339 |
elm->handler = hand; |
| 340 |
state = hand->startelm_cb(hand->userdata, elm->parent->state, |
| 341 |
elm->nspace, elm->name, PASS_ATTS(atts)); |
| 342 |
} |
| 343 |
|
| 344 |
NE_DEBUG(NE_DBG_XML, "XML: start-element (%d, {%s, %s}) => %d", |
| 345 |
elm->parent->state, elm->nspace, elm->name, state); |
| 346 |
|
| 347 |
if (state > 0) |
| 348 |
elm->state = state; |
| 349 |
else if (state == NE_XML_DECLINE) |
| 350 |
/* prune this branch. */ |
| 351 |
p->prune++; |
| 352 |
else /* state < 0 => abort parse */ |
| 353 |
p->failure = state; |
| 354 |
} |
| 355 |
|
| 356 |
/* Destroys an element structure. */ |
| 357 |
static void destroy_element(struct element *elm) |
| 358 |
{ |
| 359 |
struct namespace *this_ns, *next_ns; |
| 360 |
ne_free(elm->name); |
| 361 |
/* Free the namespaces */ |
| 362 |
this_ns = elm->nspaces; |
| 363 |
while (this_ns != NULL) { |
| 364 |
next_ns = this_ns->next; |
| 365 |
ne_free(this_ns->name); |
| 366 |
ne_free(this_ns->uri); |
| 367 |
ne_free(this_ns); |
| 368 |
this_ns = next_ns; |
| 369 |
} |
| 370 |
if (elm->default_ns) |
| 371 |
ne_free(elm->default_ns); |
| 372 |
ne_free(elm); |
| 373 |
} |
| 374 |
|
| 375 |
/* cdata SAX callback */ |
| 376 |
static void char_data(void *userdata, const ne_xml_char *data, int len) |
| 377 |
{ |
| 378 |
ne_xml_parser *p = userdata; |
| 379 |
struct element *elm = p->current; |
| 380 |
|
| 381 |
if (p->failure || p->prune) return; |
| 382 |
|
| 383 |
if (elm->handler->cdata_cb) { |
| 384 |
p->failure = elm->handler->cdata_cb(elm->handler->userdata, elm->state, data, len); |
| 385 |
NE_DEBUG(NE_DBG_XML, "XML: char-data (%d) returns %d", |
| 386 |
elm->state, p->failure); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
/* Called with the end of an element */ |
| 391 |
static void end_element(void *userdata, const ne_xml_char *name) |
| 392 |
{ |
| 393 |
ne_xml_parser *p = userdata; |
| 394 |
struct element *elm = p->current; |
| 395 |
|
| 396 |
if (p->failure) return; |
| 397 |
|
| 398 |
if (p->prune) { |
| 399 |
if (p->prune-- > 1) return; |
| 400 |
} else if (elm->handler->endelm_cb) { |
| 401 |
p->failure = elm->handler->endelm_cb(elm->handler->userdata, elm->state, |
| 402 |
elm->nspace, elm->name); |
| 403 |
if (p->failure) { |
| 404 |
NE_DEBUG(NE_DBG_XML, "XML: end-element for %d failed with %d.", |
| 405 |
elm->state, p->failure); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
NE_DEBUG(NE_DBG_XML, "XML: end-element (%d, {%s, %s})", |
| 410 |
elm->state, elm->nspace, elm->name); |
| 411 |
|
| 412 |
/* move back up the tree */ |
| 413 |
p->current = elm->parent; |
| 414 |
p->prune = 0; |
| 415 |
|
| 416 |
destroy_element(elm); |
| 417 |
} |
| 418 |
|
| 419 |
#if defined(HAVE_EXPAT) && XML_MAJOR_VERSION > 1 |
| 420 |
/* Stop the parser if an entity declaration is hit. */ |
| 421 |
static void entity_declaration(void *userData, const XML_Char *entityName, |
| 422 |
int is_parameter_entity, const XML_Char *value, |
| 423 |
int value_length, const XML_Char *base, |
| 424 |
const XML_Char *systemId, const XML_Char *publicId, |
| 425 |
const XML_Char *notationName) |
| 426 |
{ |
| 427 |
ne_xml_parser *parser = userData; |
| 428 |
|
| 429 |
NE_DEBUG(NE_DBG_XMLPARSE, "XML: entity declaration [%s]. Failing.", |
| 430 |
entityName); |
| 431 |
|
| 432 |
XML_StopParser(parser->parser, XML_FALSE); |
| 433 |
} |
| 434 |
#elif defined(HAVE_EXPAT) |
| 435 |
/* A noop default_handler. */ |
| 436 |
static void default_handler(void *userData, const XML_Char *s, int len) |
| 437 |
{ |
| 438 |
} |
| 439 |
#endif |
| 440 |
|
| 441 |
/* Find a namespace definition for 'prefix' in given element, where |
| 442 |
* length of prefix is 'pfxlen'. Returns the URI or NULL. */ |
| 443 |
static const char *resolve_nspace(const struct element *elm, |
| 444 |
const char *prefix, size_t pfxlen) |
| 445 |
{ |
| 446 |
const struct element *s; |
| 447 |
|
| 448 |
/* Search up the tree. */ |
| 449 |
for (s = elm; s != NULL; s = s->parent) { |
| 450 |
const struct namespace *ns; |
| 451 |
/* Iterate over defined spaces on this node. */ |
| 452 |
for (ns = s->nspaces; ns != NULL; ns = ns->next) { |
| 453 |
if (strlen(ns->name) == pfxlen && |
| 454 |
memcmp(ns->name, prefix, pfxlen) == 0) |
| 455 |
return ns->uri; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
return NULL; |
| 460 |
} |
| 461 |
|
| 462 |
const char *ne_xml_resolve_nspace(ne_xml_parser *parser, |
| 463 |
const char *prefix, size_t length) |
| 464 |
{ |
| 465 |
if (prefix) { |
| 466 |
return resolve_nspace(parser->current, prefix, length); |
| 467 |
} |
| 468 |
else { |
| 469 |
struct element *e = parser->current; |
| 470 |
|
| 471 |
while (e->default_ns == NULL) |
| 472 |
e = e->parent; |
| 473 |
|
| 474 |
return e->default_ns; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
ne_xml_parser *ne_xml_create(void) |
| 479 |
{ |
| 480 |
ne_xml_parser *p = ne_calloc(sizeof *p); |
| 481 |
/* Placeholder for the root element */ |
| 482 |
p->current = p->root = ne_calloc(sizeof *p->root); |
| 483 |
p->root->default_ns = ""; |
| 484 |
p->root->state = 0; |
| 485 |
strcpy(p->error, _("Unknown error")); |
| 486 |
#ifdef HAVE_EXPAT |
| 487 |
p->parser = XML_ParserCreate(NULL); |
| 488 |
if (p->parser == NULL) { |
| 489 |
abort(); |
| 490 |
} |
| 491 |
XML_SetElementHandler(p->parser, start_element, end_element); |
| 492 |
XML_SetCharacterDataHandler(p->parser, char_data); |
| 493 |
XML_SetUserData(p->parser, (void *) p); |
| 494 |
XML_SetXmlDeclHandler(p->parser, decl_handler); |
| 495 |
|
| 496 |
/* Prevent the "billion laughs" attack against expat by disabling |
| 497 |
* internal entity expansion. With 2.x, forcibly stop the parser |
| 498 |
* if an entity is declared - this is safer and a more obvious |
| 499 |
* failure mode. With older versions, installing a noop |
| 500 |
* DefaultHandler means that internal entities will be expanded as |
| 501 |
* the empty string, which is also sufficient to prevent the |
| 502 |
* attack. */ |
| 503 |
#if XML_MAJOR_VERSION > 1 |
| 504 |
XML_SetEntityDeclHandler(p->parser, entity_declaration); |
| 505 |
#else |
| 506 |
XML_SetDefaultHandler(p->parser, default_handler); |
| 507 |
#endif |
| 508 |
|
| 509 |
#else /* HAVE_LIBXML */ |
| 510 |
pthread_once(&libxml_is_initialized, initialize_libxml); |
| 511 |
p->parser = xmlCreatePushParserCtxt(&sax_handler, |
| 512 |
(void *)p, NULL, 0, NULL); |
| 513 |
if (p->parser == NULL) { |
| 514 |
abort(); |
| 515 |
} |
| 516 |
#if LIBXML_VERSION < 20602 |
| 517 |
p->parser->replaceEntities = 1; |
| 518 |
#else |
| 519 |
/* Enable expansion of entities, and disable network access. */ |
| 520 |
xmlCtxtUseOptions(p->parser, XML_PARSE_NOENT | XML_PARSE_NONET); |
| 521 |
#endif |
| 522 |
|
| 523 |
#endif /* HAVE_LIBXML || HAVE_EXPAT */ |
| 524 |
return p; |
| 525 |
} |
| 526 |
|
| 527 |
void ne_xml_push_handler(ne_xml_parser *p, |
| 528 |
ne_xml_startelm_cb *startelm_cb, |
| 529 |
ne_xml_cdata_cb *cdata_cb, |
| 530 |
ne_xml_endelm_cb *endelm_cb, |
| 531 |
void *userdata) |
| 532 |
{ |
| 533 |
struct handler *hand = ne_calloc(sizeof(struct handler)); |
| 534 |
|
| 535 |
hand->startelm_cb = startelm_cb; |
| 536 |
hand->cdata_cb = cdata_cb; |
| 537 |
hand->endelm_cb = endelm_cb; |
| 538 |
hand->userdata = userdata; |
| 539 |
|
| 540 |
/* If this is the first handler registered, update the |
| 541 |
* base pointer too. */ |
| 542 |
if (p->top_handlers == NULL) { |
| 543 |
p->root->handler = hand; |
| 544 |
p->top_handlers = hand; |
| 545 |
} else { |
| 546 |
p->top_handlers->next = hand; |
| 547 |
p->top_handlers = hand; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
int ne_xml_parse_v(void *userdata, const char *block, size_t len) |
| 552 |
{ |
| 553 |
ne_xml_parser *p = userdata; |
| 554 |
return ne_xml_parse(p, (const ne_xml_char *)block, len); |
| 555 |
} |
| 556 |
|
| 557 |
#define BOM_UTF8 "\xEF\xBB\xBF" /* UTF-8 BOM */ |
| 558 |
|
| 559 |
int ne_xml_parse(ne_xml_parser *p, const char *block, size_t len) |
| 560 |
{ |
| 561 |
int ret, flag; |
| 562 |
/* duck out if it's broken */ |
| 563 |
if (p->failure) { |
| 564 |
NE_DEBUG(NE_DBG_XMLPARSE, "XML: Failed; ignoring %" NE_FMT_SIZE_T |
| 565 |
" bytes.\n", len); |
| 566 |
return p->failure; |
| 567 |
} |
| 568 |
if (len == 0) { |
| 569 |
flag = -1; |
| 570 |
block = ""; |
| 571 |
NE_DEBUG(NE_DBG_XMLPARSE, "XML: End of document."); |
| 572 |
} else { |
| 573 |
NE_DEBUG(NE_DBG_XMLPARSE, "XML: Parsing %" NE_FMT_SIZE_T " bytes.", len); |
| 574 |
flag = 0; |
| 575 |
} |
| 576 |
|
| 577 |
#ifdef NEED_BOM_HANDLING |
| 578 |
if (p->bom_pos < 3) { |
| 579 |
NE_DEBUG(NE_DBG_XMLPARSE, "Checking for UTF-8 BOM."); |
| 580 |
while (len > 0 && p->bom_pos < 3 && |
| 581 |
block[0] == BOM_UTF8[p->bom_pos]) { |
| 582 |
block++; |
| 583 |
len--; |
| 584 |
p->bom_pos++; |
| 585 |
} |
| 586 |
if (len == 0) |
| 587 |
return 0; |
| 588 |
if (p->bom_pos == 0) { |
| 589 |
p->bom_pos = 3; /* no BOM */ |
| 590 |
} else if (p->bom_pos > 0 && p->bom_pos < 3) { |
| 591 |
strcpy(p->error, _("Invalid Byte Order Mark")); |
| 592 |
return p->failure = 1; |
| 593 |
} |
| 594 |
} |
| 595 |
#endif |
| 596 |
|
| 597 |
/* Note, don't write a parser error if p->failure, since an error |
| 598 |
* will already have been written in that case. */ |
| 599 |
#ifdef HAVE_EXPAT |
| 600 |
ret = XML_Parse(p->parser, block, len, flag); |
| 601 |
NE_DEBUG(NE_DBG_XMLPARSE, "XML: XML_Parse returned %d", ret); |
| 602 |
if (ret == 0 && p->failure == 0) { |
| 603 |
ne_snprintf(p->error, ERR_SIZE, |
| 604 |
"XML parse error at line %" NE_FMT_XML_SIZE ": %s", |
| 605 |
XML_GetCurrentLineNumber(p->parser), |
| 606 |
XML_ErrorString(XML_GetErrorCode(p->parser))); |
| 607 |
p->failure = 1; |
| 608 |
NE_DEBUG(NE_DBG_XMLPARSE, "XML: Parse error: %s", p->error); |
| 609 |
} |
| 610 |
#else |
| 611 |
ret = xmlParseChunk(p->parser, block, len, flag); |
| 612 |
NE_DEBUG(NE_DBG_XMLPARSE, "XML: xmlParseChunk returned %d", ret); |
| 613 |
/* Parse errors are normally caught by the sax_error() callback, |
| 614 |
* which clears p->valid. */ |
| 615 |
if (p->parser->errNo && p->failure == 0) { |
| 616 |
ne_snprintf(p->error, ERR_SIZE, "XML parse error at line %d", |
| 617 |
ne_xml_currentline(p)); |
| 618 |
p->failure = 1; |
| 619 |
NE_DEBUG(NE_DBG_XMLPARSE, "XML: Parse error: %s", p->error); |
| 620 |
} |
| 621 |
#endif |
| 622 |
return p->failure; |
| 623 |
} |
| 624 |
|
| 625 |
int ne_xml_failed(ne_xml_parser *p) |
| 626 |
{ |
| 627 |
return p->failure; |
| 628 |
} |
| 629 |
|
| 630 |
void ne_xml_destroy(ne_xml_parser *p) |
| 631 |
{ |
| 632 |
struct element *elm, *parent; |
| 633 |
struct handler *hand, *next; |
| 634 |
|
| 635 |
/* Free up the handlers on the stack: the root element has the |
| 636 |
* pointer to the base of the handler stack. */ |
| 637 |
for (hand = p->root->handler; hand!=NULL; hand=next) { |
| 638 |
next = hand->next; |
| 639 |
ne_free(hand); |
| 640 |
} |
| 641 |
|
| 642 |
/* Clean up remaining elements */ |
| 643 |
for (elm = p->current; elm != p->root; elm = parent) { |
| 644 |
parent = elm->parent; |
| 645 |
destroy_element(elm); |
| 646 |
} |
| 647 |
|
| 648 |
/* free root element */ |
| 649 |
ne_free(p->root); |
| 650 |
|
| 651 |
#ifdef HAVE_EXPAT |
| 652 |
XML_ParserFree(p->parser); |
| 653 |
if (p->encoding) ne_free(p->encoding); |
| 654 |
#else |
| 655 |
xmlFreeParserCtxt(p->parser); |
| 656 |
#endif |
| 657 |
|
| 658 |
ne_free(p); |
| 659 |
} |
| 660 |
|
| 661 |
void ne_xml_set_error(ne_xml_parser *p, const char *msg) |
| 662 |
{ |
| 663 |
ne_snprintf(p->error, ERR_SIZE, "%s", msg); |
| 664 |
} |
| 665 |
|
| 666 |
#ifdef HAVE_LIBXML |
| 667 |
static void sax_error(void *ctx, const char *msg, ...) |
| 668 |
{ |
| 669 |
ne_xml_parser *p = ctx; |
| 670 |
va_list ap; |
| 671 |
char buf[1024]; |
| 672 |
|
| 673 |
va_start(ap, msg); |
| 674 |
ne_vsnprintf(buf, 1024, msg, ap); |
| 675 |
va_end(ap); |
| 676 |
|
| 677 |
if (p->failure == 0) { |
| 678 |
ne_snprintf(p->error, ERR_SIZE, |
| 679 |
_("XML parse error at line %d: %s"), |
| 680 |
p->parser->input->line, buf); |
| 681 |
p->failure = 1; |
| 682 |
} |
| 683 |
} |
| 684 |
#endif |
| 685 |
|
| 686 |
const char *ne_xml_get_error(ne_xml_parser *p) |
| 687 |
{ |
| 688 |
return p->error; |
| 689 |
} |
| 690 |
|
| 691 |
const char * |
| 692 |
ne_xml_get_attr(ne_xml_parser *p, const char **attrs, |
| 693 |
const char *nspace, const char *name) |
| 694 |
{ |
| 695 |
int n; |
| 696 |
|
| 697 |
for (n = 0; attrs[n] != NULL; n += 2) { |
| 698 |
char *pnt = strchr(attrs[n], ':'); |
| 699 |
|
| 700 |
if (!nspace && !pnt && strcmp(attrs[n], name) == 0) { |
| 701 |
return attrs[n+1]; |
| 702 |
} else if (nspace && pnt) { |
| 703 |
/* If a namespace is given, and the local part matches, |
| 704 |
* then resolve the namespace and compare that too. */ |
| 705 |
if (strcmp(pnt + 1, name) == 0) { |
| 706 |
const char *uri = resolve_nspace(p->current, |
| 707 |
attrs[n], pnt - attrs[n]); |
| 708 |
if (uri && strcmp(uri, nspace) == 0) |
| 709 |
return attrs[n+1]; |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
return NULL; |
| 715 |
} |
| 716 |
|
| 717 |
int ne_xml_mapid(const struct ne_xml_idmap map[], size_t maplen, |
| 718 |
const char *nspace, const char *name) |
| 719 |
{ |
| 720 |
size_t n; |
| 721 |
|
| 722 |
for (n = 0; n < maplen; n++) |
| 723 |
if (strcmp(name, map[n].name) == 0 && |
| 724 |
strcmp(nspace, map[n].nspace) == 0) |
| 725 |
return map[n].id; |
| 726 |
|
| 727 |
return 0; |
| 728 |
} |