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