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