1 /* 2 __ __ _ 3 ___\ \/ /_ __ __ _| |_ 4 / _ \\ /| '_ \ / _` | __| 5 | __// \| |_) | (_| | |_ 6 \___/_/\_\ .__/ \__,_|\__| 7 |_| XML parser 8 9 Copyright (c) 1997-2000 Thai Open Source Software Center Ltd 10 Copyright (c) 2000 Clark Cooper <coopercc@users.sourceforge.net> 11 Copyright (c) 2000-2005 Fred L. Drake, Jr. <fdrake@users.sourceforge.net> 12 Copyright (c) 2001-2002 Greg Stein <gstein@users.sourceforge.net> 13 Copyright (c) 2002-2016 Karl Waclawek <karl@waclawek.net> 14 Copyright (c) 2016-2025 Sebastian Pipping <sebastian@pipping.org> 15 Copyright (c) 2016 Cristian Rodríguez <crrodriguez@opensuse.org> 16 Copyright (c) 2016 Thomas Beutlich <tc@tbeu.de> 17 Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk> 18 Copyright (c) 2022 Thijs Schreijer <thijs@thijsschreijer.nl> 19 Copyright (c) 2023 Hanno Böck <hanno@gentoo.org> 20 Copyright (c) 2023 Sony Corporation / Snild Dolkow <snild@sony.com> 21 Copyright (c) 2024 Taichi Haradaguchi <20001722@ymail.ne.jp> 22 Copyright (c) 2025 Matthew Fernandez <matthew.fernandez@gmail.com> 23 Licensed under the MIT license: 24 25 Permission is hereby granted, free of charge, to any person obtaining 26 a copy of this software and associated documentation files (the 27 "Software"), to deal in the Software without restriction, including 28 without limitation the rights to use, copy, modify, merge, publish, 29 distribute, sublicense, and/or sell copies of the Software, and to permit 30 persons to whom the Software is furnished to do so, subject to the 31 following conditions: 32 33 The above copyright notice and this permission notice shall be included 34 in all copies or substantial portions of the Software. 35 36 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 37 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 38 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 39 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 40 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 41 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 42 USE OR OTHER DEALINGS IN THE SOFTWARE. 43 */ 44 45 #ifndef Expat_INCLUDED 46 # define Expat_INCLUDED 1 47 48 # include <stdlib.h> 49 # include "expat_external.h" 50 51 # ifdef __cplusplus 52 extern "C" { 53 # endif 54 55 struct XML_ParserStruct; 56 typedef struct XML_ParserStruct *XML_Parser; 57 58 typedef unsigned char XML_Bool; 59 # define XML_TRUE ((XML_Bool)1) 60 # define XML_FALSE ((XML_Bool)0) 61 62 /* The XML_Status enum gives the possible return values for several 63 API functions. The preprocessor #defines are included so this 64 stanza can be added to code that still needs to support older 65 versions of Expat 1.95.x: 66 67 #ifndef XML_STATUS_OK 68 #define XML_STATUS_OK 1 69 #define XML_STATUS_ERROR 0 70 #endif 71 72 Otherwise, the #define hackery is quite ugly and would have been 73 dropped. 74 */ 75 enum XML_Status { 76 XML_STATUS_ERROR = 0, 77 # define XML_STATUS_ERROR XML_STATUS_ERROR 78 XML_STATUS_OK = 1, 79 # define XML_STATUS_OK XML_STATUS_OK 80 XML_STATUS_SUSPENDED = 2 81 # define XML_STATUS_SUSPENDED XML_STATUS_SUSPENDED 82 }; 83 84 enum XML_Error { 85 XML_ERROR_NONE, 86 XML_ERROR_NO_MEMORY, 87 XML_ERROR_SYNTAX, 88 XML_ERROR_NO_ELEMENTS, 89 XML_ERROR_INVALID_TOKEN, 90 XML_ERROR_UNCLOSED_TOKEN, 91 XML_ERROR_PARTIAL_CHAR, 92 XML_ERROR_TAG_MISMATCH, 93 XML_ERROR_DUPLICATE_ATTRIBUTE, 94 XML_ERROR_JUNK_AFTER_DOC_ELEMENT, 95 XML_ERROR_PARAM_ENTITY_REF, 96 XML_ERROR_UNDEFINED_ENTITY, 97 XML_ERROR_RECURSIVE_ENTITY_REF, 98 XML_ERROR_ASYNC_ENTITY, 99 XML_ERROR_BAD_CHAR_REF, 100 XML_ERROR_BINARY_ENTITY_REF, 101 XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF, 102 XML_ERROR_MISPLACED_XML_PI, 103 XML_ERROR_UNKNOWN_ENCODING, 104 XML_ERROR_INCORRECT_ENCODING, 105 XML_ERROR_UNCLOSED_CDATA_SECTION, 106 XML_ERROR_EXTERNAL_ENTITY_HANDLING, 107 XML_ERROR_NOT_STANDALONE, 108 XML_ERROR_UNEXPECTED_STATE, 109 XML_ERROR_ENTITY_DECLARED_IN_PE, 110 XML_ERROR_FEATURE_REQUIRES_XML_DTD, 111 XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING, 112 /* Added in 1.95.7. */ 113 XML_ERROR_UNBOUND_PREFIX, 114 /* Added in 1.95.8. */ 115 XML_ERROR_UNDECLARING_PREFIX, 116 XML_ERROR_INCOMPLETE_PE, 117 XML_ERROR_XML_DECL, 118 XML_ERROR_TEXT_DECL, 119 XML_ERROR_PUBLICID, 120 XML_ERROR_SUSPENDED, 121 XML_ERROR_NOT_SUSPENDED, 122 XML_ERROR_ABORTED, 123 XML_ERROR_FINISHED, 124 XML_ERROR_SUSPEND_PE, 125 /* Added in 2.0. */ 126 XML_ERROR_RESERVED_PREFIX_XML, 127 XML_ERROR_RESERVED_PREFIX_XMLNS, 128 XML_ERROR_RESERVED_NAMESPACE_URI, 129 /* Added in 2.2.1. */ 130 XML_ERROR_INVALID_ARGUMENT, 131 /* Added in 2.3.0. */ 132 XML_ERROR_NO_BUFFER, 133 /* Added in 2.4.0. */ 134 XML_ERROR_AMPLIFICATION_LIMIT_BREACH, 135 /* Added in 2.6.4. */ 136 XML_ERROR_NOT_STARTED, 137 }; 138 139 enum XML_Content_Type { 140 XML_CTYPE_EMPTY = 1, 141 XML_CTYPE_ANY, 142 XML_CTYPE_MIXED, 143 XML_CTYPE_NAME, 144 XML_CTYPE_CHOICE, 145 XML_CTYPE_SEQ 146 }; 147 148 enum XML_Content_Quant { 149 XML_CQUANT_NONE, 150 XML_CQUANT_OPT, 151 XML_CQUANT_REP, 152 XML_CQUANT_PLUS 153 }; 154 155 /* If type == XML_CTYPE_EMPTY or XML_CTYPE_ANY, then quant will be 156 XML_CQUANT_NONE, and the other fields will be zero or NULL. 157 If type == XML_CTYPE_MIXED, then quant will be NONE or REP and 158 numchildren will contain number of elements that may be mixed in 159 and children point to an array of XML_Content cells that will be 160 all of XML_CTYPE_NAME type with no quantification. 161 162 If type == XML_CTYPE_NAME, then the name points to the name, and 163 the numchildren field will be zero and children will be NULL. The 164 quant fields indicates any quantifiers placed on the name. 165 166 CHOICE and SEQ will have name NULL, the number of children in 167 numchildren and children will point, recursively, to an array 168 of XML_Content cells. 169 170 The EMPTY, ANY, and MIXED types will only occur at top level. 171 */ 172 173 typedef struct XML_cp XML_Content; 174 175 struct XML_cp { 176 enum XML_Content_Type type; 177 enum XML_Content_Quant quant; 178 XML_Char *name; 179 unsigned int numchildren; 180 XML_Content *children; 181 }; 182 183 /* This is called for an element declaration. See above for 184 description of the model argument. It's the user code's responsibility 185 to free model when finished with it. See XML_FreeContentModel. 186 There is no need to free the model from the handler, it can be kept 187 around and freed at a later stage. 188 */ 189 typedef void(XMLCALL *XML_ElementDeclHandler)(void *userData, 190 const XML_Char *name, 191 XML_Content *model); 192 193 XMLPARSEAPI(void) 194 XML_SetElementDeclHandler(XML_Parser parser, XML_ElementDeclHandler eldecl); 195 196 /* The Attlist declaration handler is called for *each* attribute. So 197 a single Attlist declaration with multiple attributes declared will 198 generate multiple calls to this handler. The "default" parameter 199 may be NULL in the case of the "#IMPLIED" or "#REQUIRED" 200 keyword. The "isrequired" parameter will be true and the default 201 value will be NULL in the case of "#REQUIRED". If "isrequired" is 202 true and default is non-NULL, then this is a "#FIXED" default. 203 */ 204 typedef void(XMLCALL *XML_AttlistDeclHandler)( 205 void *userData, const XML_Char *elname, const XML_Char *attname, 206 const XML_Char *att_type, const XML_Char *dflt, int isrequired); 207 208 XMLPARSEAPI(void) 209 XML_SetAttlistDeclHandler(XML_Parser parser, XML_AttlistDeclHandler attdecl); 210 211 /* The XML declaration handler is called for *both* XML declarations 212 and text declarations. The way to distinguish is that the version 213 parameter will be NULL for text declarations. The encoding 214 parameter may be NULL for XML declarations. The standalone 215 parameter will be -1, 0, or 1 indicating respectively that there 216 was no standalone parameter in the declaration, that it was given 217 as no, or that it was given as yes. 218 */ 219 typedef void(XMLCALL *XML_XmlDeclHandler)(void *userData, 220 const XML_Char *version, 221 const XML_Char *encoding, 222 int standalone); 223 224 XMLPARSEAPI(void) 225 XML_SetXmlDeclHandler(XML_Parser parser, XML_XmlDeclHandler xmldecl); 226 227 typedef struct { 228 void *(*malloc_fcn)(size_t size); 229 void *(*realloc_fcn)(void *ptr, size_t size); 230 void (*free_fcn)(void *ptr); 231 } XML_Memory_Handling_Suite; 232 233 /* Constructs a new parser; encoding is the encoding specified by the 234 external protocol or NULL if there is none specified. 235 */ 236 XMLPARSEAPI(XML_Parser) 237 XML_ParserCreate(const XML_Char *encoding); 238 239 /* Constructs a new parser and namespace processor. Element type 240 names and attribute names that belong to a namespace will be 241 expanded; unprefixed attribute names are never expanded; unprefixed 242 element type names are expanded only if there is a default 243 namespace. The expanded name is the concatenation of the namespace 244 URI, the namespace separator character, and the local part of the 245 name. If the namespace separator is '\0' then the namespace URI 246 and the local part will be concatenated without any separator. 247 It is a programming error to use the separator '\0' with namespace 248 triplets (see XML_SetReturnNSTriplet). 249 If a namespace separator is chosen that can be part of a URI or 250 part of an XML name, splitting an expanded name back into its 251 1, 2 or 3 original parts on application level in the element handler 252 may end up vulnerable, so these are advised against; sane choices for 253 a namespace separator are e.g. '\n' (line feed) and '|' (pipe). 254 255 Note that Expat does not validate namespace URIs (beyond encoding) 256 against RFC 3986 today (and is not required to do so with regard to 257 the XML 1.0 namespaces specification) but it may start doing that 258 in future releases. Before that, an application using Expat must 259 be ready to receive namespace URIs containing non-URI characters. 260 */ 261 XMLPARSEAPI(XML_Parser) 262 XML_ParserCreateNS(const XML_Char *encoding, XML_Char namespaceSeparator); 263 264 /* Constructs a new parser using the memory management suite referred to 265 by memsuite. If memsuite is NULL, then use the standard library memory 266 suite. If namespaceSeparator is non-NULL it creates a parser with 267 namespace processing as described above. The character pointed at 268 will serve as the namespace separator. 269 270 All further memory operations used for the created parser will come from 271 the given suite. 272 */ 273 XMLPARSEAPI(XML_Parser) 274 XML_ParserCreate_MM(const XML_Char *encoding, 275 const XML_Memory_Handling_Suite *memsuite, 276 const XML_Char *namespaceSeparator); 277 278 /* Prepare a parser object to be reused. This is particularly 279 valuable when memory allocation overhead is disproportionately high, 280 such as when a large number of small documents need to be parsed. 281 All handlers are cleared from the parser, except for the 282 unknownEncodingHandler. The parser's external state is re-initialized 283 except for the values of ns and ns_triplets. 284 285 Added in Expat 1.95.3. 286 */ 287 XMLPARSEAPI(XML_Bool) 288 XML_ParserReset(XML_Parser parser, const XML_Char *encoding); 289 290 /* atts is array of name/value pairs, terminated by 0; 291 names and values are 0 terminated. 292 */ 293 typedef void(XMLCALL *XML_StartElementHandler)(void *userData, 294 const XML_Char *name, 295 const XML_Char **atts); 296 297 typedef void(XMLCALL *XML_EndElementHandler)(void *userData, 298 const XML_Char *name); 299 300 /* s is not 0 terminated. */ 301 typedef void(XMLCALL *XML_CharacterDataHandler)(void *userData, 302 const XML_Char *s, int len); 303 304 /* target and data are 0 terminated */ 305 typedef void(XMLCALL *XML_ProcessingInstructionHandler)(void *userData, 306 const XML_Char *target, 307 const XML_Char *data); 308 309 /* data is 0 terminated */ 310 typedef void(XMLCALL *XML_CommentHandler)(void *userData, const XML_Char *data); 311 312 typedef void(XMLCALL *XML_StartCdataSectionHandler)(void *userData); 313 typedef void(XMLCALL *XML_EndCdataSectionHandler)(void *userData); 314 315 /* This is called for any characters in the XML document for which 316 there is no applicable handler. This includes both characters that 317 are part of markup which is of a kind that is not reported 318 (comments, markup declarations), or characters that are part of a 319 construct which could be reported but for which no handler has been 320 supplied. The characters are passed exactly as they were in the XML 321 document except that they will be encoded in UTF-8 or UTF-16. 322 Line boundaries are not normalized. Note that a byte order mark 323 character is not passed to the default handler. There are no 324 guarantees about how characters are divided between calls to the 325 default handler: for example, a comment might be split between 326 multiple calls. 327 */ 328 typedef void(XMLCALL *XML_DefaultHandler)(void *userData, const XML_Char *s, 329 int len); 330 331 /* This is called for the start of the DOCTYPE declaration, before 332 any DTD or internal subset is parsed. 333 */ 334 typedef void(XMLCALL *XML_StartDoctypeDeclHandler)(void *userData, 335 const XML_Char *doctypeName, 336 const XML_Char *sysid, 337 const XML_Char *pubid, 338 int has_internal_subset); 339 340 /* This is called for the end of the DOCTYPE declaration when the 341 closing > is encountered, but after processing any external 342 subset. 343 */ 344 typedef void(XMLCALL *XML_EndDoctypeDeclHandler)(void *userData); 345 346 /* This is called for entity declarations. The is_parameter_entity 347 argument will be non-zero if the entity is a parameter entity, zero 348 otherwise. 349 350 For internal entities (<!ENTITY foo "bar">), value will 351 be non-NULL and systemId, publicID, and notationName will be NULL. 352 The value string is NOT null-terminated; the length is provided in 353 the value_length argument. Since it is legal to have zero-length 354 values, do not use this argument to test for internal entities. 355 356 For external entities, value will be NULL and systemId will be 357 non-NULL. The publicId argument will be NULL unless a public 358 identifier was provided. The notationName argument will have a 359 non-NULL value only for unparsed entity declarations. 360 361 Note that is_parameter_entity can't be changed to XML_Bool, since 362 that would break binary compatibility. 363 */ 364 typedef void(XMLCALL *XML_EntityDeclHandler)( 365 void *userData, const XML_Char *entityName, int is_parameter_entity, 366 const XML_Char *value, int value_length, const XML_Char *base, 367 const XML_Char *systemId, const XML_Char *publicId, 368 const XML_Char *notationName); 369 370 XMLPARSEAPI(void) 371 XML_SetEntityDeclHandler(XML_Parser parser, XML_EntityDeclHandler handler); 372 373 /* OBSOLETE -- OBSOLETE -- OBSOLETE 374 This handler has been superseded by the EntityDeclHandler above. 375 It is provided here for backward compatibility. 376 377 This is called for a declaration of an unparsed (NDATA) entity. 378 The base argument is whatever was set by XML_SetBase. The 379 entityName, systemId and notationName arguments will never be 380 NULL. The other arguments may be. 381 */ 382 typedef void(XMLCALL *XML_UnparsedEntityDeclHandler)( 383 void *userData, const XML_Char *entityName, const XML_Char *base, 384 const XML_Char *systemId, const XML_Char *publicId, 385 const XML_Char *notationName); 386 387 /* This is called for a declaration of notation. The base argument is 388 whatever was set by XML_SetBase. The notationName will never be 389 NULL. The other arguments can be. 390 */ 391 typedef void(XMLCALL *XML_NotationDeclHandler)(void *userData, 392 const XML_Char *notationName, 393 const XML_Char *base, 394 const XML_Char *systemId, 395 const XML_Char *publicId); 396 397 /* When namespace processing is enabled, these are called once for 398 each namespace declaration. The call to the start and end element 399 handlers occur between the calls to the start and end namespace 400 declaration handlers. For an xmlns attribute, prefix will be 401 NULL. For an xmlns="" attribute, uri will be NULL. 402 */ 403 typedef void(XMLCALL *XML_StartNamespaceDeclHandler)(void *userData, 404 const XML_Char *prefix, 405 const XML_Char *uri); 406 407 typedef void(XMLCALL *XML_EndNamespaceDeclHandler)(void *userData, 408 const XML_Char *prefix); 409 410 /* This is called if the document is not standalone, that is, it has an 411 external subset or a reference to a parameter entity, but does not 412 have standalone="yes". If this handler returns XML_STATUS_ERROR, 413 then processing will not continue, and the parser will return a 414 XML_ERROR_NOT_STANDALONE error. 415 If parameter entity parsing is enabled, then in addition to the 416 conditions above this handler will only be called if the referenced 417 entity was actually read. 418 */ 419 typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData); 420 421 /* This is called for a reference to an external parsed general 422 entity. The referenced entity is not automatically parsed. The 423 application can parse it immediately or later using 424 XML_ExternalEntityParserCreate. 425 426 The parser argument is the parser parsing the entity containing the 427 reference; it can be passed as the parser argument to 428 XML_ExternalEntityParserCreate. The systemId argument is the 429 system identifier as specified in the entity declaration; it will 430 not be NULL. 431 432 The base argument is the system identifier that should be used as 433 the base for resolving systemId if systemId was relative; this is 434 set by XML_SetBase; it may be NULL. 435 436 The publicId argument is the public identifier as specified in the 437 entity declaration, or NULL if none was specified; the whitespace 438 in the public identifier will have been normalized as required by 439 the XML spec. 440 441 The context argument specifies the parsing context in the format 442 expected by the context argument to XML_ExternalEntityParserCreate; 443 context is valid only until the handler returns, so if the 444 referenced entity is to be parsed later, it must be copied. 445 context is NULL only when the entity is a parameter entity. 446 447 The handler should return XML_STATUS_ERROR if processing should not 448 continue because of a fatal error in the handling of the external 449 entity. In this case the calling parser will return an 450 XML_ERROR_EXTERNAL_ENTITY_HANDLING error. 451 452 Note that unlike other handlers the first argument is the parser, 453 not userData. 454 */ 455 typedef int(XMLCALL *XML_ExternalEntityRefHandler)(XML_Parser parser, 456 const XML_Char *context, 457 const XML_Char *base, 458 const XML_Char *systemId, 459 const XML_Char *publicId); 460 461 /* This is called in two situations: 462 1) An entity reference is encountered for which no declaration 463 has been read *and* this is not an error. 464 2) An internal entity reference is read, but not expanded, because 465 XML_SetDefaultHandler has been called. 466 Note: skipped parameter entities in declarations and skipped general 467 entities in attribute values cannot be reported, because 468 the event would be out of sync with the reporting of the 469 declarations or attribute values 470 */ 471 typedef void(XMLCALL *XML_SkippedEntityHandler)(void *userData, 472 const XML_Char *entityName, 473 int is_parameter_entity); 474 475 /* This structure is filled in by the XML_UnknownEncodingHandler to 476 provide information to the parser about encodings that are unknown 477 to the parser. 478 479 The map[b] member gives information about byte sequences whose 480 first byte is b. 481 482 If map[b] is c where c is >= 0, then b by itself encodes the 483 Unicode scalar value c. 484 485 If map[b] is -1, then the byte sequence is malformed. 486 487 If map[b] is -n, where n >= 2, then b is the first byte of an 488 n-byte sequence that encodes a single Unicode scalar value. 489 490 The data member will be passed as the first argument to the convert 491 function. 492 493 The convert function is used to convert multibyte sequences; s will 494 point to a n-byte sequence where map[(unsigned char)*s] == -n. The 495 convert function must return the Unicode scalar value represented 496 by this byte sequence or -1 if the byte sequence is malformed. 497 498 The convert function may be NULL if the encoding is a single-byte 499 encoding, that is if map[b] >= -1 for all bytes b. 500 501 When the parser is finished with the encoding, then if release is 502 not NULL, it will call release passing it the data member; once 503 release has been called, the convert function will not be called 504 again. 505 506 Expat places certain restrictions on the encodings that are supported 507 using this mechanism. 508 509 1. Every ASCII character that can appear in a well-formed XML document, 510 other than the characters 511 512 $@\^`{}~ 513 514 must be represented by a single byte, and that byte must be the 515 same byte that represents that character in ASCII. 516 517 2. No character may require more than 4 bytes to encode. 518 519 3. All characters encoded must have Unicode scalar values <= 520 0xFFFF, (i.e., characters that would be encoded by surrogates in 521 UTF-16 are not allowed). Note that this restriction doesn't 522 apply to the built-in support for UTF-8 and UTF-16. 523 524 4. No Unicode character may be encoded by more than one distinct 525 sequence of bytes. 526 */ 527 typedef struct { 528 int map[256]; 529 void *data; 530 int(XMLCALL *convert)(void *data, const char *s); 531 void(XMLCALL *release)(void *data); 532 } XML_Encoding; 533 534 /* This is called for an encoding that is unknown to the parser. 535 536 The encodingHandlerData argument is that which was passed as the 537 second argument to XML_SetUnknownEncodingHandler. 538 539 The name argument gives the name of the encoding as specified in 540 the encoding declaration. 541 542 If the callback can provide information about the encoding, it must 543 fill in the XML_Encoding structure, and return XML_STATUS_OK. 544 Otherwise it must return XML_STATUS_ERROR. 545 546 If info does not describe a suitable encoding, then the parser will 547 return an XML_ERROR_UNKNOWN_ENCODING error. 548 */ 549 typedef int(XMLCALL *XML_UnknownEncodingHandler)(void *encodingHandlerData, 550 const XML_Char *name, 551 XML_Encoding *info); 552 553 XMLPARSEAPI(void) 554 XML_SetElementHandler(XML_Parser parser, XML_StartElementHandler start, 555 XML_EndElementHandler end); 556 557 XMLPARSEAPI(void) 558 XML_SetStartElementHandler(XML_Parser parser, XML_StartElementHandler handler); 559 560 XMLPARSEAPI(void) 561 XML_SetEndElementHandler(XML_Parser parser, XML_EndElementHandler handler); 562 563 XMLPARSEAPI(void) 564 XML_SetCharacterDataHandler(XML_Parser parser, 565 XML_CharacterDataHandler handler); 566 567 XMLPARSEAPI(void) 568 XML_SetProcessingInstructionHandler(XML_Parser parser, 569 XML_ProcessingInstructionHandler handler); 570 XMLPARSEAPI(void) 571 XML_SetCommentHandler(XML_Parser parser, XML_CommentHandler handler); 572 573 XMLPARSEAPI(void) 574 XML_SetCdataSectionHandler(XML_Parser parser, 575 XML_StartCdataSectionHandler start, 576 XML_EndCdataSectionHandler end); 577 578 XMLPARSEAPI(void) 579 XML_SetStartCdataSectionHandler(XML_Parser parser, 580 XML_StartCdataSectionHandler start); 581 582 XMLPARSEAPI(void) 583 XML_SetEndCdataSectionHandler(XML_Parser parser, 584 XML_EndCdataSectionHandler end); 585 586 /* This sets the default handler and also inhibits expansion of 587 internal entities. These entity references will be passed to the 588 default handler, or to the skipped entity handler, if one is set. 589 */ 590 XMLPARSEAPI(void) 591 XML_SetDefaultHandler(XML_Parser parser, XML_DefaultHandler handler); 592 593 /* This sets the default handler but does not inhibit expansion of 594 internal entities. The entity reference will not be passed to the 595 default handler. 596 */ 597 XMLPARSEAPI(void) 598 XML_SetDefaultHandlerExpand(XML_Parser parser, XML_DefaultHandler handler); 599 600 XMLPARSEAPI(void) 601 XML_SetDoctypeDeclHandler(XML_Parser parser, XML_StartDoctypeDeclHandler start, 602 XML_EndDoctypeDeclHandler end); 603 604 XMLPARSEAPI(void) 605 XML_SetStartDoctypeDeclHandler(XML_Parser parser, 606 XML_StartDoctypeDeclHandler start); 607 608 XMLPARSEAPI(void) 609 XML_SetEndDoctypeDeclHandler(XML_Parser parser, XML_EndDoctypeDeclHandler end); 610 611 XMLPARSEAPI(void) 612 XML_SetUnparsedEntityDeclHandler(XML_Parser parser, 613 XML_UnparsedEntityDeclHandler handler); 614 615 XMLPARSEAPI(void) 616 XML_SetNotationDeclHandler(XML_Parser parser, XML_NotationDeclHandler handler); 617 618 XMLPARSEAPI(void) 619 XML_SetNamespaceDeclHandler(XML_Parser parser, 620 XML_StartNamespaceDeclHandler start, 621 XML_EndNamespaceDeclHandler end); 622 623 XMLPARSEAPI(void) 624 XML_SetStartNamespaceDeclHandler(XML_Parser parser, 625 XML_StartNamespaceDeclHandler start); 626 627 XMLPARSEAPI(void) 628 XML_SetEndNamespaceDeclHandler(XML_Parser parser, 629 XML_EndNamespaceDeclHandler end); 630 631 XMLPARSEAPI(void) 632 XML_SetNotStandaloneHandler(XML_Parser parser, 633 XML_NotStandaloneHandler handler); 634 635 XMLPARSEAPI(void) 636 XML_SetExternalEntityRefHandler(XML_Parser parser, 637 XML_ExternalEntityRefHandler handler); 638 639 /* If a non-NULL value for arg is specified here, then it will be 640 passed as the first argument to the external entity ref handler 641 instead of the parser object. 642 */ 643 XMLPARSEAPI(void) 644 XML_SetExternalEntityRefHandlerArg(XML_Parser parser, void *arg); 645 646 XMLPARSEAPI(void) 647 XML_SetSkippedEntityHandler(XML_Parser parser, 648 XML_SkippedEntityHandler handler); 649 650 XMLPARSEAPI(void) 651 XML_SetUnknownEncodingHandler(XML_Parser parser, 652 XML_UnknownEncodingHandler handler, 653 void *encodingHandlerData); 654 655 /* This can be called within a handler for a start element, end 656 element, processing instruction or character data. It causes the 657 corresponding markup to be passed to the default handler. 658 */ 659 XMLPARSEAPI(void) 660 XML_DefaultCurrent(XML_Parser parser); 661 662 /* If do_nst is non-zero, and namespace processing is in effect, and 663 a name has a prefix (i.e. an explicit namespace qualifier) then 664 that name is returned as a triplet in a single string separated by 665 the separator character specified when the parser was created: URI 666 + sep + local_name + sep + prefix. 667 668 If do_nst is zero, then namespace information is returned in the 669 default manner (URI + sep + local_name) whether or not the name 670 has a prefix. 671 672 Note: Calling XML_SetReturnNSTriplet after XML_Parse or 673 XML_ParseBuffer has no effect. 674 */ 675 676 XMLPARSEAPI(void) 677 XML_SetReturnNSTriplet(XML_Parser parser, int do_nst); 678 679 /* This value is passed as the userData argument to callbacks. */ 680 XMLPARSEAPI(void) 681 XML_SetUserData(XML_Parser parser, void *userData); 682 683 /* Returns the last value set by XML_SetUserData or NULL. */ 684 # define XML_GetUserData(parser) (*(void **)(parser)) 685 686 /* This is equivalent to supplying an encoding argument to 687 XML_ParserCreate. On success XML_SetEncoding returns non-zero, 688 zero otherwise. 689 Note: Calling XML_SetEncoding after XML_Parse or XML_ParseBuffer 690 has no effect and returns XML_STATUS_ERROR. 691 */ 692 XMLPARSEAPI(enum XML_Status) 693 XML_SetEncoding(XML_Parser parser, const XML_Char *encoding); 694 695 /* If this function is called, then the parser will be passed as the 696 first argument to callbacks instead of userData. The userData will 697 still be accessible using XML_GetUserData. 698 */ 699 XMLPARSEAPI(void) 700 XML_UseParserAsHandlerArg(XML_Parser parser); 701 702 /* If useDTD == XML_TRUE is passed to this function, then the parser 703 will assume that there is an external subset, even if none is 704 specified in the document. In such a case the parser will call the 705 externalEntityRefHandler with a value of NULL for the systemId 706 argument (the publicId and context arguments will be NULL as well). 707 Note: For the purpose of checking WFC: Entity Declared, passing 708 useDTD == XML_TRUE will make the parser behave as if the document 709 had a DTD with an external subset. 710 Note: If this function is called, then this must be done before 711 the first call to XML_Parse or XML_ParseBuffer, since it will 712 have no effect after that. Returns 713 XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING. 714 Note: If the document does not have a DOCTYPE declaration at all, 715 then startDoctypeDeclHandler and endDoctypeDeclHandler will not 716 be called, despite an external subset being parsed. 717 Note: If XML_DTD is not defined when Expat is compiled, returns 718 XML_ERROR_FEATURE_REQUIRES_XML_DTD. 719 Note: If parser == NULL, returns XML_ERROR_INVALID_ARGUMENT. 720 */ 721 XMLPARSEAPI(enum XML_Error) 722 XML_UseForeignDTD(XML_Parser parser, XML_Bool useDTD); 723 724 /* Sets the base to be used for resolving relative URIs in system 725 identifiers in declarations. Resolving relative identifiers is 726 left to the application: this value will be passed through as the 727 base argument to the XML_ExternalEntityRefHandler, 728 XML_NotationDeclHandler and XML_UnparsedEntityDeclHandler. The base 729 argument will be copied. Returns XML_STATUS_ERROR if out of memory, 730 XML_STATUS_OK otherwise. 731 */ 732 XMLPARSEAPI(enum XML_Status) 733 XML_SetBase(XML_Parser parser, const XML_Char *base); 734 735 XMLPARSEAPI(const XML_Char *) 736 XML_GetBase(XML_Parser parser); 737 738 /* Returns the number of the attribute/value pairs passed in last call 739 to the XML_StartElementHandler that were specified in the start-tag 740 rather than defaulted. Each attribute/value pair counts as 2; thus 741 this corresponds to an index into the atts array passed to the 742 XML_StartElementHandler. Returns -1 if parser == NULL. 743 */ 744 XMLPARSEAPI(int) 745 XML_GetSpecifiedAttributeCount(XML_Parser parser); 746 747 /* Returns the index of the ID attribute passed in the last call to 748 XML_StartElementHandler, or -1 if there is no ID attribute or 749 parser == NULL. Each attribute/value pair counts as 2; thus this 750 corresponds to an index into the atts array passed to the 751 XML_StartElementHandler. 752 */ 753 XMLPARSEAPI(int) 754 XML_GetIdAttributeIndex(XML_Parser parser); 755 756 # ifdef XML_ATTR_INFO 757 /* Source file byte offsets for the start and end of attribute names and values. 758 The value indices are exclusive of surrounding quotes; thus in a UTF-8 source 759 file an attribute value of "blah" will yield: 760 info->valueEnd - info->valueStart = 4 bytes. 761 */ 762 typedef struct { 763 XML_Index nameStart; /* Offset to beginning of the attribute name. */ 764 XML_Index nameEnd; /* Offset after the attribute name's last byte. */ 765 XML_Index valueStart; /* Offset to beginning of the attribute value. */ 766 XML_Index valueEnd; /* Offset after the attribute value's last byte. */ 767 } XML_AttrInfo; 768 769 /* Returns an array of XML_AttrInfo structures for the attribute/value pairs 770 passed in last call to the XML_StartElementHandler that were specified 771 in the start-tag rather than defaulted. Each attribute/value pair counts 772 as 1; thus the number of entries in the array is 773 XML_GetSpecifiedAttributeCount(parser) / 2. 774 */ 775 XMLPARSEAPI(const XML_AttrInfo *) 776 XML_GetAttributeInfo(XML_Parser parser); 777 # endif 778 779 /* Parses some input. Returns XML_STATUS_ERROR if a fatal error is 780 detected. The last call to XML_Parse must have isFinal true; len 781 may be zero for this call (or any other). 782 783 Though the return values for these functions has always been 784 described as a Boolean value, the implementation, at least for the 785 1.95.x series, has always returned exactly one of the XML_Status 786 values. 787 */ 788 XMLPARSEAPI(enum XML_Status) 789 XML_Parse(XML_Parser parser, const char *s, int len, int isFinal); 790 791 XMLPARSEAPI(void *) 792 XML_GetBuffer(XML_Parser parser, int len); 793 794 XMLPARSEAPI(enum XML_Status) 795 XML_ParseBuffer(XML_Parser parser, int len, int isFinal); 796 797 /* Stops parsing, causing XML_Parse() or XML_ParseBuffer() to return. 798 Must be called from within a call-back handler, except when aborting 799 (resumable = 0) an already suspended parser. Some call-backs may 800 still follow because they would otherwise get lost. Examples: 801 - endElementHandler() for empty elements when stopped in 802 startElementHandler(), 803 - endNameSpaceDeclHandler() when stopped in endElementHandler(), 804 and possibly others. 805 806 Can be called from most handlers, including DTD related call-backs, 807 except when parsing an external parameter entity and resumable != 0. 808 Returns XML_STATUS_OK when successful, XML_STATUS_ERROR otherwise. 809 Possible error codes: 810 - XML_ERROR_SUSPENDED: when suspending an already suspended parser. 811 - XML_ERROR_FINISHED: when the parser has already finished. 812 - XML_ERROR_SUSPEND_PE: when suspending while parsing an external PE. 813 814 When resumable != 0 (true) then parsing is suspended, that is, 815 XML_Parse() and XML_ParseBuffer() return XML_STATUS_SUSPENDED. 816 Otherwise, parsing is aborted, that is, XML_Parse() and XML_ParseBuffer() 817 return XML_STATUS_ERROR with error code XML_ERROR_ABORTED. 818 819 *Note*: 820 This will be applied to the current parser instance only, that is, if 821 there is a parent parser then it will continue parsing when the 822 externalEntityRefHandler() returns. It is up to the implementation of 823 the externalEntityRefHandler() to call XML_StopParser() on the parent 824 parser (recursively), if one wants to stop parsing altogether. 825 826 When suspended, parsing can be resumed by calling XML_ResumeParser(). 827 */ 828 XMLPARSEAPI(enum XML_Status) 829 XML_StopParser(XML_Parser parser, XML_Bool resumable); 830 831 /* Resumes parsing after it has been suspended with XML_StopParser(). 832 Must not be called from within a handler call-back. Returns same 833 status codes as XML_Parse() or XML_ParseBuffer(). 834 Additional error code XML_ERROR_NOT_SUSPENDED possible. 835 836 *Note*: 837 This must be called on the most deeply nested child parser instance 838 first, and on its parent parser only after the child parser has finished, 839 to be applied recursively until the document entity's parser is restarted. 840 That is, the parent parser will not resume by itself and it is up to the 841 application to call XML_ResumeParser() on it at the appropriate moment. 842 */ 843 XMLPARSEAPI(enum XML_Status) 844 XML_ResumeParser(XML_Parser parser); 845 846 enum XML_Parsing { XML_INITIALIZED, XML_PARSING, XML_FINISHED, XML_SUSPENDED }; 847 848 typedef struct { 849 enum XML_Parsing parsing; 850 XML_Bool finalBuffer; 851 } XML_ParsingStatus; 852 853 /* Returns status of parser with respect to being initialized, parsing, 854 finished, or suspended and processing the final buffer. 855 XXX XML_Parse() and XML_ParseBuffer() should return XML_ParsingStatus, 856 XXX with XML_FINISHED_OK or XML_FINISHED_ERROR replacing XML_FINISHED 857 */ 858 XMLPARSEAPI(void) 859 XML_GetParsingStatus(XML_Parser parser, XML_ParsingStatus *status); 860 861 /* Creates an XML_Parser object that can parse an external general 862 entity; context is a '\0'-terminated string specifying the parse 863 context; encoding is a '\0'-terminated string giving the name of 864 the externally specified encoding, or NULL if there is no 865 externally specified encoding. The context string consists of a 866 sequence of tokens separated by formfeeds (\f); a token consisting 867 of a name specifies that the general entity of the name is open; a 868 token of the form prefix=uri specifies the namespace for a 869 particular prefix; a token of the form =uri specifies the default 870 namespace. This can be called at any point after the first call to 871 an ExternalEntityRefHandler so longer as the parser has not yet 872 been freed. The new parser is completely independent and may 873 safely be used in a separate thread. The handlers and userData are 874 initialized from the parser argument. Returns NULL if out of memory. 875 Otherwise returns a new XML_Parser object. 876 */ 877 XMLPARSEAPI(XML_Parser) 878 XML_ExternalEntityParserCreate(XML_Parser parser, const XML_Char *context, 879 const XML_Char *encoding); 880 881 enum XML_ParamEntityParsing { 882 XML_PARAM_ENTITY_PARSING_NEVER, 883 XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE, 884 XML_PARAM_ENTITY_PARSING_ALWAYS 885 }; 886 887 /* Controls parsing of parameter entities (including the external DTD 888 subset). If parsing of parameter entities is enabled, then 889 references to external parameter entities (including the external 890 DTD subset) will be passed to the handler set with 891 XML_SetExternalEntityRefHandler. The context passed will be 0. 892 893 Unlike external general entities, external parameter entities can 894 only be parsed synchronously. If the external parameter entity is 895 to be parsed, it must be parsed during the call to the external 896 entity ref handler: the complete sequence of 897 XML_ExternalEntityParserCreate, XML_Parse/XML_ParseBuffer and 898 XML_ParserFree calls must be made during this call. After 899 XML_ExternalEntityParserCreate has been called to create the parser 900 for the external parameter entity (context must be 0 for this 901 call), it is illegal to make any calls on the old parser until 902 XML_ParserFree has been called on the newly created parser. 903 If the library has been compiled without support for parameter 904 entity parsing (ie without XML_DTD being defined), then 905 XML_SetParamEntityParsing will return 0 if parsing of parameter 906 entities is requested; otherwise it will return non-zero. 907 Note: If XML_SetParamEntityParsing is called after XML_Parse or 908 XML_ParseBuffer, then it has no effect and will always return 0. 909 Note: If parser == NULL, the function will do nothing and return 0. 910 */ 911 XMLPARSEAPI(int) 912 XML_SetParamEntityParsing(XML_Parser parser, 913 enum XML_ParamEntityParsing parsing); 914 915 /* Sets the hash salt to use for internal hash calculations. 916 Helps in preventing DoS attacks based on predicting hash 917 function behavior. This must be called before parsing is started. 918 Returns 1 if successful, 0 when called after parsing has started. 919 Note: If parser == NULL, the function will do nothing and return 0. 920 */ 921 XMLPARSEAPI(int) 922 XML_SetHashSalt(XML_Parser parser, unsigned long hash_salt); 923 924 /* If XML_Parse or XML_ParseBuffer have returned XML_STATUS_ERROR, then 925 XML_GetErrorCode returns information about the error. 926 */ 927 XMLPARSEAPI(enum XML_Error) 928 XML_GetErrorCode(XML_Parser parser); 929 930 /* These functions return information about the current parse 931 location. They may be called from any callback called to report 932 some parse event; in this case the location is the location of the 933 first of the sequence of characters that generated the event. When 934 called from callbacks generated by declarations in the document 935 prologue, the location identified isn't as neatly defined, but will 936 be within the relevant markup. When called outside of the callback 937 functions, the position indicated will be just past the last parse 938 event (regardless of whether there was an associated callback). 939 940 They may also be called after returning from a call to XML_Parse 941 or XML_ParseBuffer. If the return value is XML_STATUS_ERROR then 942 the location is the location of the character at which the error 943 was detected; otherwise the location is the location of the last 944 parse event, as described above. 945 946 Note: XML_GetCurrentLineNumber and XML_GetCurrentColumnNumber 947 return 0 to indicate an error. 948 Note: XML_GetCurrentByteIndex returns -1 to indicate an error. 949 */ 950 XMLPARSEAPI(XML_Size) XML_GetCurrentLineNumber(XML_Parser parser); 951 XMLPARSEAPI(XML_Size) XML_GetCurrentColumnNumber(XML_Parser parser); 952 XMLPARSEAPI(XML_Index) XML_GetCurrentByteIndex(XML_Parser parser); 953 954 /* Return the number of bytes in the current event. 955 Returns 0 if the event is in an internal entity. 956 */ 957 XMLPARSEAPI(int) 958 XML_GetCurrentByteCount(XML_Parser parser); 959 960 /* If XML_CONTEXT_BYTES is >=1, returns the input buffer, sets 961 the integer pointed to by offset to the offset within this buffer 962 of the current parse position, and sets the integer pointed to by size 963 to the size of this buffer (the number of input bytes). Otherwise 964 returns a NULL pointer. Also returns a NULL pointer if a parse isn't 965 active. 966 967 NOTE: The character pointer returned should not be used outside 968 the handler that makes the call. 969 */ 970 XMLPARSEAPI(const char *) 971 XML_GetInputContext(XML_Parser parser, int *offset, int *size); 972 973 /* For backwards compatibility with previous versions. */ 974 # define XML_GetErrorLineNumber XML_GetCurrentLineNumber 975 # define XML_GetErrorColumnNumber XML_GetCurrentColumnNumber 976 # define XML_GetErrorByteIndex XML_GetCurrentByteIndex 977 978 /* Frees the content model passed to the element declaration handler */ 979 XMLPARSEAPI(void) 980 XML_FreeContentModel(XML_Parser parser, XML_Content *model); 981 982 /* Exposing the memory handling functions used in Expat */ 983 XMLPARSEAPI(void *) 984 XML_ATTR_MALLOC 985 XML_ATTR_ALLOC_SIZE(2) 986 XML_MemMalloc(XML_Parser parser, size_t size); 987 988 XMLPARSEAPI(void *) 989 XML_ATTR_ALLOC_SIZE(3) 990 XML_MemRealloc(XML_Parser parser, void *ptr, size_t size); 991 992 XMLPARSEAPI(void) 993 XML_MemFree(XML_Parser parser, void *ptr); 994 995 /* Frees memory used by the parser. */ 996 XMLPARSEAPI(void) 997 XML_ParserFree(XML_Parser parser); 998 999 /* Returns a string describing the error. */ 1000 XMLPARSEAPI(const XML_LChar *) 1001 XML_ErrorString(enum XML_Error code); 1002 1003 /* Return a string containing the version number of this expat */ 1004 XMLPARSEAPI(const XML_LChar *) 1005 XML_ExpatVersion(void); 1006 1007 typedef struct { 1008 int major; 1009 int minor; 1010 int micro; 1011 } XML_Expat_Version; 1012 1013 /* Return an XML_Expat_Version structure containing numeric version 1014 number information for this version of expat. 1015 */ 1016 XMLPARSEAPI(XML_Expat_Version) 1017 XML_ExpatVersionInfo(void); 1018 1019 /* Added in Expat 1.95.5. */ 1020 enum XML_FeatureEnum { 1021 XML_FEATURE_END = 0, 1022 XML_FEATURE_UNICODE, 1023 XML_FEATURE_UNICODE_WCHAR_T, 1024 XML_FEATURE_DTD, 1025 XML_FEATURE_CONTEXT_BYTES, 1026 XML_FEATURE_MIN_SIZE, 1027 XML_FEATURE_SIZEOF_XML_CHAR, 1028 XML_FEATURE_SIZEOF_XML_LCHAR, 1029 XML_FEATURE_NS, 1030 XML_FEATURE_LARGE_SIZE, 1031 XML_FEATURE_ATTR_INFO, 1032 /* Added in Expat 2.4.0. */ 1033 XML_FEATURE_BILLION_LAUGHS_ATTACK_PROTECTION_MAXIMUM_AMPLIFICATION_DEFAULT, 1034 XML_FEATURE_BILLION_LAUGHS_ATTACK_PROTECTION_ACTIVATION_THRESHOLD_DEFAULT, 1035 /* Added in Expat 2.6.0. */ 1036 XML_FEATURE_GE, 1037 /* Added in Expat 2.7.2. */ 1038 XML_FEATURE_ALLOC_TRACKER_MAXIMUM_AMPLIFICATION_DEFAULT, 1039 XML_FEATURE_ALLOC_TRACKER_ACTIVATION_THRESHOLD_DEFAULT, 1040 /* Additional features must be added to the end of this enum. */ 1041 }; 1042 1043 typedef struct { 1044 enum XML_FeatureEnum feature; 1045 const XML_LChar *name; 1046 long int value; 1047 } XML_Feature; 1048 1049 XMLPARSEAPI(const XML_Feature *) 1050 XML_GetFeatureList(void); 1051 1052 # if defined(XML_DTD) || (defined(XML_GE) && XML_GE == 1) 1053 /* Added in Expat 2.4.0 for XML_DTD defined and 1054 * added in Expat 2.6.0 for XML_GE == 1. */ 1055 XMLPARSEAPI(XML_Bool) 1056 XML_SetBillionLaughsAttackProtectionMaximumAmplification( 1057 XML_Parser parser, float maximumAmplificationFactor); 1058 1059 /* Added in Expat 2.4.0 for XML_DTD defined and 1060 * added in Expat 2.6.0 for XML_GE == 1. */ 1061 XMLPARSEAPI(XML_Bool) 1062 XML_SetBillionLaughsAttackProtectionActivationThreshold( 1063 XML_Parser parser, unsigned long long activationThresholdBytes); 1064 1065 /* Added in Expat 2.7.2. */ 1066 XMLPARSEAPI(XML_Bool) 1067 XML_SetAllocTrackerMaximumAmplification(XML_Parser parser, 1068 float maximumAmplificationFactor); 1069 1070 /* Added in Expat 2.7.2. */ 1071 XMLPARSEAPI(XML_Bool) 1072 XML_SetAllocTrackerActivationThreshold( 1073 XML_Parser parser, unsigned long long activationThresholdBytes); 1074 # endif 1075 1076 /* Added in Expat 2.6.0. */ 1077 XMLPARSEAPI(XML_Bool) 1078 XML_SetReparseDeferralEnabled(XML_Parser parser, XML_Bool enabled); 1079 1080 /* Expat follows the semantic versioning convention. 1081 See https://semver.org 1082 */ 1083 # define XML_MAJOR_VERSION 2 1084 # define XML_MINOR_VERSION 7 1085 # define XML_MICRO_VERSION 3 1086 1087 # ifdef __cplusplus 1088 } 1089 # endif 1090 1091 #endif /* not Expat_INCLUDED */ 1092