1<?xml version="1.0" encoding="utf-8"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 5 <head> 6 <!-- 7 __ __ _ 8 ___\ \/ /_ __ __ _| |_ 9 / _ \\ /| '_ \ / _` | __| 10 | __// \| |_) | (_| | |_ 11 \___/_/\_\ .__/ \__,_|\__| 12 |_| XML parser 13 14 Copyright (c) 2000 Clark Cooper <coopercc@users.sourceforge.net> 15 Copyright (c) 2000-2004 Fred L. Drake, Jr. <fdrake@users.sourceforge.net> 16 Copyright (c) 2002-2012 Karl Waclawek <karl@waclawek.net> 17 Copyright (c) 2017-2026 Sebastian Pipping <sebastian@pipping.org> 18 Copyright (c) 2017 Jakub Wilk <jwilk@jwilk.net> 19 Copyright (c) 2021 Tomas Korbar <tkorbar@redhat.com> 20 Copyright (c) 2021 Nicolas Cavallari <nicolas.cavallari@green-communications.fr> 21 Copyright (c) 2022 Thijs Schreijer <thijs@thijsschreijer.nl> 22 Copyright (c) 2023-2025 Hanno Böck <hanno@gentoo.org> 23 Copyright (c) 2023 Sony Corporation / Snild Dolkow <snild@sony.com> 24 Licensed under the MIT license: 25 26 Permission is hereby granted, free of charge, to any person obtaining 27 a copy of this software and associated documentation files (the 28 "Software"), to deal in the Software without restriction, including 29 without limitation the rights to use, copy, modify, merge, publish, 30 distribute, sublicense, and/or sell copies of the Software, and to permit 31 persons to whom the Software is furnished to do so, subject to the 32 following conditions: 33 34 The above copyright notice and this permission notice shall be included 35 in all copies or substantial portions of the Software. 36 37 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 38 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 39 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 40 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 41 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 42 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 43 USE OR OTHER DEALINGS IN THE SOFTWARE. 44--> 45 46 <title> 47 Expat XML Parser 48 </title> 49 <meta name="author" content="Clark Cooper, coopercc@netheaven.com" /> 50 <link href="ok.min.css" rel="stylesheet" /> 51 <link href="style.css" rel="stylesheet" /> 52 </head> 53 <body> 54 <div> 55 <h1> 56 The Expat XML Parser <small>Release 2.9.0</small> 57 </h1> 58 </div> 59 60 <div class="content"> 61 <p> 62 Expat is a library, written in C, for parsing XML documents. It's the underlying 63 XML parser for the open source Mozilla project, Perl's <code>XML::Parser</code>, 64 Python's <code>xml.parsers.expat</code>, and other open-source XML parsers. 65 </p> 66 67 <p> 68 This library is the creation of James Clark, who's also given us groff (an nroff 69 look-alike), Jade (an implementation of ISO's DSSSL stylesheet language for 70 SGML), XP (a Java XML parser package), XT (a Java XSL engine). James was also the 71 technical lead on the XML Working Group at W3C that produced the XML 72 specification. 73 </p> 74 75 <p> 76 This is free software, licensed under the <a href="../COPYING">MIT/X Consortium 77 license</a>. You may download it from <a href="https://libexpat.github.io/">the 78 Expat home page</a>. 79 </p> 80 81 <p> 82 The bulk of this document was originally commissioned as an article by <a href= 83 "https://www.xml.com/">XML.com</a>. They graciously allowed Clark Cooper to 84 retain copyright and to distribute it with Expat. This version has been 85 substantially extended to include documentation on features which have been added 86 since the original article was published, and additional information on using the 87 original interface. 88 </p> 89 90 <hr /> 91 92 <h2> 93 Table of Contents 94 </h2> 95 96 <ul> 97 <li> 98 <a href="#overview">Overview</a> 99 </li> 100 101 <li> 102 <a href="#building">Building and Installing</a> 103 </li> 104 105 <li> 106 <a href="#using">Using Expat</a> 107 </li> 108 109 <li> 110 <a href="#reference">Reference</a> 111 <ul> 112 <li> 113 <a href="#creation">Parser Creation Functions</a> 114 <ul> 115 <li> 116 <a href="#XML_ParserCreate">XML_ParserCreate</a> 117 </li> 118 119 <li> 120 <a href="#XML_ParserCreateNS">XML_ParserCreateNS</a> 121 </li> 122 123 <li> 124 <a href="#XML_ParserCreate_MM">XML_ParserCreate_MM</a> 125 </li> 126 127 <li> 128 <a href= 129 "#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a> 130 </li> 131 132 <li> 133 <a href="#XML_ParserFree">XML_ParserFree</a> 134 </li> 135 136 <li> 137 <a href="#XML_ParserReset">XML_ParserReset</a> 138 </li> 139 </ul> 140 </li> 141 142 <li> 143 <a href="#parsing">Parsing Functions</a> 144 <ul> 145 <li> 146 <a href="#XML_Parse">XML_Parse</a> 147 </li> 148 149 <li> 150 <a href="#XML_ParseBuffer">XML_ParseBuffer</a> 151 </li> 152 153 <li> 154 <a href="#XML_GetBuffer">XML_GetBuffer</a> 155 </li> 156 157 <li> 158 <a href="#XML_StopParser">XML_StopParser</a> 159 </li> 160 161 <li> 162 <a href="#XML_ResumeParser">XML_ResumeParser</a> 163 </li> 164 165 <li> 166 <a href="#XML_GetParsingStatus">XML_GetParsingStatus</a> 167 </li> 168 </ul> 169 </li> 170 171 <li> 172 <a href="#setting">Handler Setting Functions</a> 173 <ul> 174 <li> 175 <a href="#XML_SetStartElementHandler">XML_SetStartElementHandler</a> 176 </li> 177 178 <li> 179 <a href="#XML_SetEndElementHandler">XML_SetEndElementHandler</a> 180 </li> 181 182 <li> 183 <a href="#XML_SetElementHandler">XML_SetElementHandler</a> 184 </li> 185 186 <li> 187 <a href="#XML_SetCharacterDataHandler">XML_SetCharacterDataHandler</a> 188 </li> 189 190 <li> 191 <a href= 192 "#XML_SetProcessingInstructionHandler">XML_SetProcessingInstructionHandler</a> 193 </li> 194 195 <li> 196 <a href="#XML_SetCommentHandler">XML_SetCommentHandler</a> 197 </li> 198 199 <li> 200 <a href= 201 "#XML_SetStartCdataSectionHandler">XML_SetStartCdataSectionHandler</a> 202 </li> 203 204 <li> 205 <a href= 206 "#XML_SetEndCdataSectionHandler">XML_SetEndCdataSectionHandler</a> 207 </li> 208 209 <li> 210 <a href="#XML_SetCdataSectionHandler">XML_SetCdataSectionHandler</a> 211 </li> 212 213 <li> 214 <a href="#XML_SetDefaultHandler">XML_SetDefaultHandler</a> 215 </li> 216 217 <li> 218 <a href="#XML_SetDefaultHandlerExpand">XML_SetDefaultHandlerExpand</a> 219 </li> 220 221 <li> 222 <a href= 223 "#XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler</a> 224 </li> 225 226 <li> 227 <a href= 228 "#XML_SetExternalEntityRefHandlerArg">XML_SetExternalEntityRefHandlerArg</a> 229 </li> 230 231 <li> 232 <a href="#XML_SetSkippedEntityHandler">XML_SetSkippedEntityHandler</a> 233 </li> 234 235 <li> 236 <a href= 237 "#XML_SetUnknownEncodingHandler">XML_SetUnknownEncodingHandler</a> 238 </li> 239 240 <li> 241 <a href= 242 "#XML_SetStartNamespaceDeclHandler">XML_SetStartNamespaceDeclHandler</a> 243 </li> 244 245 <li> 246 <a href= 247 "#XML_SetEndNamespaceDeclHandler">XML_SetEndNamespaceDeclHandler</a> 248 </li> 249 250 <li> 251 <a href="#XML_SetNamespaceDeclHandler">XML_SetNamespaceDeclHandler</a> 252 </li> 253 254 <li> 255 <a href="#XML_SetXmlDeclHandler">XML_SetXmlDeclHandler</a> 256 </li> 257 258 <li> 259 <a href= 260 "#XML_SetStartDoctypeDeclHandler">XML_SetStartDoctypeDeclHandler</a> 261 </li> 262 263 <li> 264 <a href= 265 "#XML_SetEndDoctypeDeclHandler">XML_SetEndDoctypeDeclHandler</a> 266 </li> 267 268 <li> 269 <a href="#XML_SetDoctypeDeclHandler">XML_SetDoctypeDeclHandler</a> 270 </li> 271 272 <li> 273 <a href="#XML_SetElementDeclHandler">XML_SetElementDeclHandler</a> 274 </li> 275 276 <li> 277 <a href="#XML_SetAttlistDeclHandler">XML_SetAttlistDeclHandler</a> 278 </li> 279 280 <li> 281 <a href="#XML_SetEntityDeclHandler">XML_SetEntityDeclHandler</a> 282 </li> 283 284 <li> 285 <a href= 286 "#XML_SetUnparsedEntityDeclHandler">XML_SetUnparsedEntityDeclHandler</a> 287 </li> 288 289 <li> 290 <a href="#XML_SetNotationDeclHandler">XML_SetNotationDeclHandler</a> 291 </li> 292 293 <li> 294 <a href="#XML_SetNotStandaloneHandler">XML_SetNotStandaloneHandler</a> 295 </li> 296 </ul> 297 </li> 298 299 <li> 300 <a href="#position">Parse Position and Error Reporting Functions</a> 301 <ul> 302 <li> 303 <a href="#XML_GetErrorCode">XML_GetErrorCode</a> 304 </li> 305 306 <li> 307 <a href="#XML_ErrorString">XML_ErrorString</a> 308 </li> 309 310 <li> 311 <a href="#XML_GetCurrentByteIndex">XML_GetCurrentByteIndex</a> 312 (deprecated) 313 </li> 314 315 <li> 316 <a href="#XML_GetCurrentByteIndex64">XML_GetCurrentByteIndex64</a> 317 </li> 318 319 <li> 320 <a href="#XML_GetCurrentLineNumber">XML_GetCurrentLineNumber</a> 321 (deprecated) 322 </li> 323 324 <li> 325 <a href="#XML_GetCurrentLineNumber64">XML_GetCurrentLineNumber64</a> 326 </li> 327 328 <li> 329 <a href="#XML_GetCurrentColumnNumber">XML_GetCurrentColumnNumber</a> 330 (deprecated) 331 </li> 332 333 <li> 334 <a href= 335 "#XML_GetCurrentColumnNumber64">XML_GetCurrentColumnNumber64</a> 336 </li> 337 338 <li> 339 <a href="#XML_GetCurrentByteCount">XML_GetCurrentByteCount</a> 340 (deprecated) 341 </li> 342 343 <li> 344 <a href="#XML_GetCurrentByteCount64">XML_GetCurrentByteCount64</a> 345 </li> 346 347 <li> 348 <a href="#XML_GetInputContext">XML_GetInputContext</a> (deprecated) 349 </li> 350 351 <li> 352 <a href="#XML_GetInputContext64">XML_GetInputContext64</a> 353 </li> 354 </ul> 355 </li> 356 357 <li> 358 <a href="#properties">Properties / Security Tuning</a> 359 <ul> 360 <li> 361 <a href="#XML_GetPropertyBool">XML_GetPropertyBool</a> 362 </li> 363 364 <li> 365 <a href="#XML_GetPropertyDouble">XML_GetPropertyDouble</a> 366 </li> 367 368 <li> 369 <a href="#XML_GetPropertyUInt64">XML_GetPropertyUInt64</a> 370 </li> 371 372 <li> 373 <a href="#XML_SetPropertyBool">XML_SetPropertyBool</a> 374 </li> 375 376 <li> 377 <a href="#XML_SetPropertyDouble">XML_SetPropertyDouble</a> 378 </li> 379 380 <li> 381 <a href="#XML_SetPropertyUInt64">XML_SetPropertyUInt64</a> 382 </li> 383 </ul> 384 </li> 385 386 <li> 387 <a href="#attack-protection">Attack Protection</a> 388 <ul> 389 <li> 390 <a href= 391 "#XML_SetBillionLaughsAttackProtectionMaximumAmplification">XML_SetBillionLaughsAttackProtectionMaximumAmplification</a> 392 </li> 393 394 <li> 395 <a href= 396 "#XML_SetBillionLaughsAttackProtectionActivationThreshold">XML_SetBillionLaughsAttackProtectionActivationThreshold</a> 397 </li> 398 399 <li> 400 <a href= 401 "#XML_SetAllocTrackerMaximumAmplification">XML_SetAllocTrackerMaximumAmplification</a> 402 </li> 403 404 <li> 405 <a href= 406 "#XML_SetAllocTrackerActivationThreshold">XML_SetAllocTrackerActivationThreshold</a> 407 </li> 408 409 <li> 410 <a href= 411 "#XML_SetReparseDeferralEnabled">XML_SetReparseDeferralEnabled</a> 412 </li> 413 </ul> 414 </li> 415 416 <li> 417 <a href="#miscellaneous">Miscellaneous Functions</a> 418 <ul> 419 <li> 420 <a href="#XML_SetUserData">XML_SetUserData</a> 421 </li> 422 423 <li> 424 <a href="#XML_GetUserData">XML_GetUserData</a> 425 </li> 426 427 <li> 428 <a href="#XML_UseParserAsHandlerArg">XML_UseParserAsHandlerArg</a> 429 </li> 430 431 <li> 432 <a href="#XML_SetBase">XML_SetBase</a> 433 </li> 434 435 <li> 436 <a href="#XML_GetBase">XML_GetBase</a> 437 </li> 438 439 <li> 440 <a href= 441 "#XML_GetSpecifiedAttributeCount">XML_GetSpecifiedAttributeCount</a> 442 </li> 443 444 <li> 445 <a href="#XML_GetIdAttributeIndex">XML_GetIdAttributeIndex</a> 446 </li> 447 448 <li> 449 <a href="#XML_SetEncoding">XML_SetEncoding</a> 450 </li> 451 452 <li> 453 <a href="#XML_SetParamEntityParsing">XML_SetParamEntityParsing</a> 454 </li> 455 456 <li> 457 <a href="#XML_SetHashSalt">XML_SetHashSalt</a> (deprecated) 458 </li> 459 460 <li> 461 <a href="#XML_SetHashSalt16Bytes">XML_SetHashSalt16Bytes</a> 462 </li> 463 464 <li> 465 <a href="#XML_UseForeignDTD">XML_UseForeignDTD</a> 466 </li> 467 468 <li> 469 <a href="#XML_SetReturnNSTriplet">XML_SetReturnNSTriplet</a> 470 </li> 471 472 <li> 473 <a href="#XML_DefaultCurrent">XML_DefaultCurrent</a> 474 </li> 475 476 <li> 477 <a href="#XML_ExpatVersion">XML_ExpatVersion</a> 478 </li> 479 480 <li> 481 <a href="#XML_ExpatVersionInfo">XML_ExpatVersionInfo</a> 482 </li> 483 484 <li> 485 <a href="#XML_GetFeatureList">XML_GetFeatureList</a> 486 </li> 487 488 <li> 489 <a href="#XML_FreeContentModel">XML_FreeContentModel</a> 490 </li> 491 492 <li> 493 <a href="#XML_MemMalloc">XML_MemMalloc</a> 494 </li> 495 496 <li> 497 <a href="#XML_MemRealloc">XML_MemRealloc</a> 498 </li> 499 500 <li> 501 <a href="#XML_MemFree">XML_MemFree</a> 502 </li> 503 </ul> 504 </li> 505 </ul> 506 </li> 507 </ul> 508 509 <hr /> 510 511 <h2> 512 <a id="overview" name="overview">Overview</a> 513 </h2> 514 515 <p> 516 Expat is a stream-oriented parser. You register callback (or handler) functions 517 with the parser and then start feeding it the document. As the parser recognizes 518 parts of the document, it will call the appropriate handler for that part (if 519 you've registered one.) The document is fed to the parser in pieces, so you can 520 start parsing before you have all the document. This also allows you to parse 521 really huge documents that won't fit into memory. 522 </p> 523 524 <p> 525 Expat can be intimidating due to the many kinds of handlers and options you can 526 set. But you only need to learn four functions in order to do 90% of what you'll 527 want to do with it: 528 </p> 529 530 <dl> 531 <dt> 532 <code><a href="#XML_ParserCreate">XML_ParserCreate</a></code> 533 </dt> 534 535 <dd> 536 Create a new parser object. 537 </dd> 538 539 <dt> 540 <code><a href="#XML_SetElementHandler">XML_SetElementHandler</a></code> 541 </dt> 542 543 <dd> 544 Set handlers for start and end tags. 545 </dd> 546 547 <dt> 548 <code><a href= 549 "#XML_SetCharacterDataHandler">XML_SetCharacterDataHandler</a></code> 550 </dt> 551 552 <dd> 553 Set handler for text. 554 </dd> 555 556 <dt> 557 <code><a href="#XML_Parse">XML_Parse</a></code> 558 </dt> 559 560 <dd> 561 Pass a buffer full of document to the parser 562 </dd> 563 </dl> 564 565 <p> 566 These functions and others are described in the <a href= 567 "#reference">reference</a> part of this document. The reference section also 568 describes in detail the parameters passed to the different types of handlers. 569 </p> 570 571 <p> 572 Let's look at a very simple example program that only uses 3 of the above 573 functions (it doesn't need to set a character handler.) The program <a href= 574 "../examples/outline.c">outline.c</a> prints an element outline, indenting child 575 elements to distinguish them from the parent element that contains them. The 576 start handler does all the work. It prints two indenting spaces for every level 577 of ancestor elements, then it prints the element and attribute information. 578 Finally it increments the global <code>Depth</code> variable. 579 </p> 580 581 <pre class="eg"> 582int Depth; 583 584void XMLCALL 585start(void *data, const char *el, const char **attr) { 586 int i; 587 588 for (i = 0; i < Depth; i++) 589 printf(" "); 590 591 printf("%s", el); 592 593 for (i = 0; attr[i]; i += 2) { 594 printf(" %s='%s'", attr[i], attr[i + 1]); 595 } 596 597 printf("\n"); 598 Depth++; 599} /* End of start handler */ 600</pre> 601 <p> 602 The end tag simply does the bookkeeping work of decrementing <code>Depth</code>. 603 </p> 604 605 <pre class="eg"> 606void XMLCALL 607end(void *data, const char *el) { 608 Depth--; 609} /* End of end handler */ 610</pre> 611 <p> 612 Note the <code>XMLCALL</code> annotation used for the callbacks. This is used to 613 ensure that the Expat and the callbacks are using the same calling convention in 614 case the compiler options used for Expat itself and the client code are 615 different. Expat tries not to care what the default calling convention is, though 616 it may require that it be compiled with a default convention of "cdecl" on some 617 platforms. For code which uses Expat, however, the calling convention is 618 specified by the <code>XMLCALL</code> annotation on most platforms; callbacks 619 should be defined using this annotation. 620 </p> 621 622 <p> 623 The <code>XMLCALL</code> annotation was added in Expat 1.95.7, but existing 624 working Expat applications don't need to add it (since they are already using the 625 "cdecl" calling convention, or they wouldn't be working). The annotation is only 626 needed if the default calling convention may be something other than "cdecl". To 627 use the annotation safely with older versions of Expat, you can conditionally 628 define it <em>after</em> including Expat's header file: 629 </p> 630 631 <pre class="eg"> 632#include <expat.h> 633 634#ifndef XMLCALL 635#if defined(_MSC_VER) && !defined(__BEOS__) && !defined(__CYGWIN__) 636#define XMLCALL __cdecl 637#elif defined(__GNUC__) 638#define XMLCALL __attribute__((cdecl)) 639#else 640#define XMLCALL 641#endif 642#endif 643</pre> 644 <p> 645 After creating the parser, the main program just has the job of shoveling the 646 document to the parser so that it can do its work. 647 </p> 648 649 <hr /> 650 651 <h2> 652 <a id="building" name="building">Building and Installing Expat</a> 653 </h2> 654 655 <p> 656 The Expat distribution comes as a compressed (with GNU gzip) tar file. You may 657 download the latest version from <a href= 658 "https://sourceforge.net/projects/expat/">Source Forge</a>. After unpacking this, 659 cd into the directory. Then follow either the Win32 directions or Unix directions 660 below. 661 </p> 662 663 <h3> 664 Building under Win32 665 </h3> 666 667 <p> 668 If you're using the GNU compiler under cygwin, follow the Unix directions in the 669 next section. Otherwise if you have Microsoft's Developer Studio installed, you 670 can use CMake to generate a <code>.sln</code> file, e.g. <code>cmake -G"Visual 671 Studio 17 2022" -DCMAKE_BUILD_TYPE=RelWithDebInfo .</code> , and build Expat 672 using <code>msbuild /m expat.sln</code> after. 673 </p> 674 675 <p> 676 Alternatively, you may download the Win32 binary package that contains the 677 "expat.h" include file and a pre-built DLL. 678 </p> 679 680 <h3> 681 Building under Unix (or GNU) 682 </h3> 683 684 <p> 685 First you'll need to run the configure shell script in order to configure the 686 Makefiles and headers for your system. 687 </p> 688 689 <p> 690 If you're happy with all the defaults that configure picks for you, and you have 691 permission on your system to install into /usr/local, you can install Expat with 692 this sequence of commands: 693 </p> 694 695 <pre class="eg"> 696./configure 697make 698make install 699</pre> 700 <p> 701 There are some options that you can provide to this script, but the only one 702 we'll mention here is the <code>--prefix</code> option. You can find out all the 703 options available by running configure with just the <code>--help</code> option. 704 </p> 705 706 <p> 707 By default, the configure script sets things up so that the library gets 708 installed in <code>/usr/local/lib</code> and the associated header file in 709 <code>/usr/local/include</code>. But if you were to give the option, 710 <code>--prefix=/home/me/mystuff</code>, then the library and header would get 711 installed in <code>/home/me/mystuff/lib</code> and 712 <code>/home/me/mystuff/include</code> respectively. 713 </p> 714 715 <h3> 716 Configuring Expat Using the Pre-Processor 717 </h3> 718 719 <p> 720 Expat's feature set can be configured using a small number of pre-processor 721 definitions. The symbols are: 722 </p> 723 724 <dl class="cpp-symbols"> 725 <dt> 726 <a id="XML_GE" name="XML_GE">XML_GE</a> 727 </dt> 728 729 <dd> 730 Added in Expat 2.6.0. Include support for <a href= 731 "https://www.w3.org/TR/2006/REC-xml-20060816/#sec-physical-struct">general 732 entities</a> (syntax <code>&e1;</code> to reference and syntax 733 <code><!ENTITY e1 'value1'></code> (an internal general entity) or 734 <code><!ENTITY e2 SYSTEM 'file2'></code> (an external general entity) to 735 declare). With <code>XML_GE</code> enabled, general entities will be replaced 736 by their declared replacement text; for this to work for <em>external</em> 737 general entities, in addition an <code><a href= 738 "#XML_SetExternalEntityRefHandler">XML_ExternalEntityRefHandler</a></code> must 739 be set using <code><a href= 740 "#XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler</a></code>. 741 Also, enabling <code>XML_GE</code> makes the functions <code><a href= 742 "#XML_SetBillionLaughsAttackProtectionMaximumAmplification">XML_SetBillionLaughsAttackProtectionMaximumAmplification</a></code> 743 and <code><a href= 744 "#XML_SetBillionLaughsAttackProtectionActivationThreshold">XML_SetBillionLaughsAttackProtectionActivationThreshold</a></code> 745 available.<br /> 746 With <code>XML_GE</code> disabled, Expat has a smaller memory footprint and can 747 be faster, but will not load external general entities and will replace all 748 general entities (except the <a href= 749 "https://www.w3.org/TR/2006/REC-xml-20060816/#sec-predefined-ent">predefined 750 five</a>: <code>amp</code>, <code>apos</code>, <code>gt</code>, 751 <code>lt</code>, <code>quot</code>) with a self-reference: for example, 752 referencing an entity <code>e1</code> via <code>&e1;</code> will be 753 replaced by text <code>&e1;</code>. 754 </dd> 755 756 <dt> 757 <a id="XML_DTD" name="XML_DTD">XML_DTD</a> 758 </dt> 759 760 <dd> 761 Include support for using and reporting DTD-based content. If this is defined, 762 default attribute values from an external DTD subset are reported and attribute 763 value normalization occurs based on the type of attributes defined in the 764 external subset. Without this, Expat has a smaller memory footprint and can be 765 faster, but will not load external parameter entities or process conditional 766 sections. If defined, makes the functions <code><a href= 767 "#XML_SetBillionLaughsAttackProtectionMaximumAmplification">XML_SetBillionLaughsAttackProtectionMaximumAmplification</a></code> 768 and <code><a href= 769 "#XML_SetBillionLaughsAttackProtectionActivationThreshold">XML_SetBillionLaughsAttackProtectionActivationThreshold</a></code> 770 available. 771 </dd> 772 773 <dt> 774 <a id="XML_NS" name="XML_NS">XML_NS</a> 775 </dt> 776 777 <dd> 778 When defined, support for the <cite><a href= 779 "https://www.w3.org/TR/REC-xml-names/">Namespaces in XML</a></cite> 780 specification is included. 781 </dd> 782 783 <dt> 784 <a id="XML_UNICODE" name="XML_UNICODE">XML_UNICODE</a> 785 </dt> 786 787 <dd> 788 When defined, character data reported to the application is encoded in UTF-16 789 using wide characters of the type <code>XML_Char</code>. This is implied if 790 <code>XML_UNICODE_WCHAR_T</code> is defined. 791 </dd> 792 793 <dt> 794 <a id="XML_UNICODE_WCHAR_T" name="XML_UNICODE_WCHAR_T">XML_UNICODE_WCHAR_T</a> 795 </dt> 796 797 <dd> 798 If defined, causes the <code>XML_Char</code> character type to be defined using 799 the <code>wchar_t</code> type; otherwise, <code>unsigned short</code> is used. 800 Defining this implies <code>XML_UNICODE</code>. 801 </dd> 802 803 <dt> 804 <a id="XML_LARGE_SIZE" name="XML_LARGE_SIZE">XML_LARGE_SIZE</a> (deprecated) 805 </dt> 806 807 <dd> 808 <p> 809 If defined, causes the <code>XML_Size</code> and <code>XML_Index</code> 810 integer types to be at least 64 bits in size. This is intended to support 811 processing of very large input streams, where the return values of 812 <code><a href="#XML_GetCurrentByteIndex">XML_GetCurrentByteIndex</a></code>, 813 <code><a href="#XML_GetCurrentLineNumber">XML_GetCurrentLineNumber</a></code> 814 and <code><a href= 815 "#XML_GetCurrentColumnNumber">XML_GetCurrentColumnNumber</a></code> could 816 overflow. It may not be supported by all compilers, and is turned off by 817 default. 818 </p> 819 820 <p> 821 Starting with Expat 2.9.0, there are 64bit versions of these functions, so 822 that relying on (non-default and uncommon) macro <code>XML_LARGE_SIZE</code> 823 is no longer needed. These functions are: 824 </p> 825 826 <ul> 827 <li> 828 <code><a href= 829 "#XML_GetCurrentByteIndex64">XML_GetCurrentByteIndex64</a></code> 830 </li> 831 832 <li> 833 <code><a href= 834 "#XML_GetCurrentColumnNumber64">XML_GetCurrentColumnNumber64</a></code> 835 </li> 836 837 <li> 838 <code><a href= 839 "#XML_GetCurrentLineNumber64">XML_GetCurrentLineNumber64</a></code> 840 </li> 841 </ul> 842 843 <p> 844 And there are two more that <code>XML_LARGE_SIZE</code> was not covering: 845 </p> 846 847 <ul> 848 <li> 849 <code><a href= 850 "#XML_GetCurrentByteCount64">XML_GetCurrentByteCount64</a></code> 851 </li> 852 853 <li> 854 <code><a href="#XML_GetInputContext64">XML_GetInputContext64</a></code> 855 </li> 856 </ul> 857 </dd> 858 859 <dt> 860 <a id="XML_CONTEXT_BYTES" name="XML_CONTEXT_BYTES">XML_CONTEXT_BYTES</a> 861 </dt> 862 863 <dd> 864 The number of input bytes of markup context which the parser will ensure are 865 available for reporting via <code><a href= 866 "#XML_GetInputContext">XML_GetInputContext</a></code>. This is normally set to 867 1024, and must be set to a positive integer to enable. If this is set to zero, 868 the input context will not be available and <code><a href= 869 "#XML_GetInputContext">XML_GetInputContext</a></code> will always report 870 <code>NULL</code>. Without this, Expat has a smaller memory footprint and can 871 be faster. 872 </dd> 873 874 <dt> 875 <a id="XML_STATIC" name="XML_STATIC">XML_STATIC</a> 876 </dt> 877 878 <dd> 879 On Windows, this should be set if Expat is going to be linked statically with 880 the code that calls it; this is required to get all the right MSVC magic 881 annotations correct. This is ignored on other platforms. 882 </dd> 883 </dl> 884 885 <hr /> 886 887 <h2> 888 <a id="using" name="using">Using Expat</a> 889 </h2> 890 891 <h3> 892 Compiling and Linking Against Expat 893 </h3> 894 895 <p> 896 Unless you installed Expat in a location not expected by your compiler and 897 linker, all you have to do to use Expat in your programs is to include the Expat 898 header (<code>#include <expat.h></code>) in your files that make calls to 899 it and to tell the linker that it needs to link against the Expat library. On 900 Unix systems, this would usually be done with the <code>-lexpat</code> argument. 901 Otherwise, you'll need to tell the compiler where to look for the Expat header 902 and the linker where to find the Expat library. You may also need to take steps 903 to tell the operating system where to find this library at run time. 904 </p> 905 906 <p> 907 On a Unix-based system, here's what a Makefile might look like when Expat is 908 installed in a standard location: 909 </p> 910 911 <pre class="eg"> 912CC=cc 913LDFLAGS= 914LIBS= -lexpat 915xmlapp: xmlapp.o 916 $(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS) 917</pre> 918 <p> 919 If you installed Expat in, say, <code>/home/me/mystuff</code>, then the Makefile 920 would look like this: 921 </p> 922 923 <pre class="eg"> 924CC=cc 925CFLAGS= -I/home/me/mystuff/include 926LDFLAGS= 927LIBS= -L/home/me/mystuff/lib -lexpat 928xmlapp: xmlapp.o 929 $(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS) 930</pre> 931 <p> 932 You'd also have to set the environment variable <code>LD_LIBRARY_PATH</code> to 933 <code>/home/me/mystuff/lib</code> (or to 934 <code>${LD_LIBRARY_PATH}:/home/me/mystuff/lib</code> if LD_LIBRARY_PATH already 935 has some directories in it) in order to run your application. 936 </p> 937 938 <h3> 939 Expat Basics 940 </h3> 941 942 <p> 943 As we saw in the example in the overview, the first step in parsing an XML 944 document with Expat is to create a parser object. There are <a href= 945 "#creation">three functions</a> in the Expat API for creating a parser object. 946 However, only two of these (<code><a href= 947 "#XML_ParserCreate">XML_ParserCreate</a></code> and <code><a href= 948 "#XML_ParserCreateNS">XML_ParserCreateNS</a></code>) can be used for constructing 949 a parser for a top-level document. The object returned by these functions is an 950 opaque pointer (i.e. "expat.h" declares it as void *) to data with further 951 internal structure. In order to free the memory associated with this object you 952 must call <code><a href="#XML_ParserFree">XML_ParserFree</a></code>. Note that if 953 you have provided any <a href="#userdata">user data</a> that gets stored in the 954 parser, then your application is responsible for freeing it prior to calling 955 <code>XML_ParserFree</code>. 956 </p> 957 958 <p> 959 The objects returned by the parser creation functions are good for parsing only 960 one XML document or external parsed entity. If your application needs to parse 961 many XML documents, then it needs to create a parser object for each one. The 962 best way to deal with this is to create a higher level object that contains all 963 the default initialization you want for your parser objects. 964 </p> 965 966 <p> 967 Walking through a document hierarchy with a stream oriented parser will require a 968 good stack mechanism in order to keep track of current context. For instance, to 969 answer the simple question, "What element does this text belong to?" requires a 970 stack, since the parser may have descended into other elements that are children 971 of the current one and has encountered this text on the way out. 972 </p> 973 974 <p> 975 The things you're likely to want to keep on a stack are the currently opened 976 element and it's attributes. You push this information onto the stack in the 977 start handler and you pop it off in the end handler. 978 </p> 979 980 <p> 981 For some tasks, it is sufficient to just keep information on what the depth of 982 the stack is (or would be if you had one.) The outline program shown above 983 presents one example. Another such task would be skipping over a complete 984 element. When you see the start tag for the element you want to skip, you set a 985 skip flag and record the depth at which the element started. When the end tag 986 handler encounters the same depth, the skipped element has ended and the flag may 987 be cleared. If you follow the convention that the root element starts at 1, then 988 you can use the same variable for skip flag and skip depth. 989 </p> 990 991 <pre class="eg"> 992void 993init_info(Parseinfo *info) { 994 info->skip = 0; 995 info->depth = 1; 996 /* Other initializations here */ 997} /* End of init_info */ 998 999void XMLCALL 1000rawstart(void *data, const char *el, const char **attr) { 1001 Parseinfo *inf = (Parseinfo *) data; 1002 1003 if (! inf->skip) { 1004 if (should_skip(inf, el, attr)) { 1005 inf->skip = inf->depth; 1006 } 1007 else 1008 start(inf, el, attr); /* This does rest of start handling */ 1009 } 1010 1011 inf->depth++; 1012} /* End of rawstart */ 1013 1014void XMLCALL 1015rawend(void *data, const char *el) { 1016 Parseinfo *inf = (Parseinfo *) data; 1017 1018 inf->depth--; 1019 1020 if (! inf->skip) 1021 end(inf, el); /* This does rest of end handling */ 1022 1023 if (inf->skip == inf->depth) 1024 inf->skip = 0; 1025} /* End rawend */ 1026</pre> 1027 <p> 1028 Notice in the above example the difference in how depth is manipulated in the 1029 start and end handlers. The end tag handler should be the mirror image of the 1030 start tag handler. This is necessary to properly model containment. Since, in the 1031 start tag handler, we incremented depth <em>after</em> the main body of start tag 1032 code, then in the end handler, we need to manipulate it <em>before</em> the main 1033 body. If we'd decided to increment it first thing in the start handler, then we'd 1034 have had to decrement it last thing in the end handler. 1035 </p> 1036 1037 <h3 id="userdata"> 1038 Communicating between handlers 1039 </h3> 1040 1041 <p> 1042 In order to be able to pass information between different handlers without using 1043 globals, you'll need to define a data structure to hold the shared variables. You 1044 can then tell Expat (with the <code><a href= 1045 "#XML_SetUserData">XML_SetUserData</a></code> function) to pass a pointer to this 1046 structure to the handlers. This is the first argument received by most handlers. 1047 In the <a href="#reference">reference section</a>, an argument to a callback 1048 function is named <code>userData</code> and have type <code>void *</code> if the 1049 user data is passed; it will have the type <code>XML_Parser</code> if the parser 1050 itself is passed. When the parser is passed, the user data may be retrieved using 1051 <code><a href="#XML_GetUserData">XML_GetUserData</a></code>. 1052 </p> 1053 1054 <p> 1055 One common case where multiple calls to a single handler may need to communicate 1056 using an application data structure is the case when content passed to the 1057 character data handler (set by <code><a href= 1058 "#XML_SetCharacterDataHandler">XML_SetCharacterDataHandler</a></code>) needs to 1059 be accumulated. A common first-time mistake with any of the event-oriented 1060 interfaces to an XML parser is to expect all the text contained in an element to 1061 be reported by a single call to the character data handler. Expat, like many 1062 other XML parsers, reports such data as a sequence of calls; there's no way to 1063 know when the end of the sequence is reached until a different callback is made. 1064 A buffer referenced by the user data structure proves both an effective and 1065 convenient place to accumulate character data. 1066 </p> 1067 <!-- XXX example needed here --> 1068 1069 <h3> 1070 XML Version 1071 </h3> 1072 1073 <p> 1074 Expat is an XML 1.0 parser, and as such never complains based on the value of the 1075 <code>version</code> pseudo-attribute in the XML declaration, if present. 1076 </p> 1077 1078 <p> 1079 If an application needs to check the version number (to support alternate 1080 processing), it should use the <code><a href= 1081 "#XML_SetXmlDeclHandler">XML_SetXmlDeclHandler</a></code> function to set a 1082 handler that uses the information in the XML declaration to determine what to do. 1083 This example shows how to check that only a version number of <code>"1.0"</code> 1084 is accepted: 1085 </p> 1086 1087 <pre class="eg"> 1088static int wrong_version; 1089static XML_Parser parser; 1090 1091static void XMLCALL 1092xmldecl_handler(void *userData, 1093 const XML_Char *version, 1094 const XML_Char *encoding, 1095 int standalone) 1096{ 1097 static const XML_Char Version_1_0[] = {'1', '.', '0', 0}; 1098 1099 int i; 1100 1101 for (i = 0; i < (sizeof(Version_1_0) / sizeof(Version_1_0[0])); ++i) { 1102 if (version[i] != Version_1_0[i]) { 1103 wrong_version = 1; 1104 /* also clear all other handlers: */ 1105 XML_SetCharacterDataHandler(parser, NULL); 1106 ... 1107 return; 1108 } 1109 } 1110 ... 1111} 1112</pre> 1113 <h3> 1114 Namespace Processing 1115 </h3> 1116 1117 <p> 1118 When the parser is created using the <code><a href= 1119 "#XML_ParserCreateNS">XML_ParserCreateNS</a></code>, function, Expat performs 1120 namespace processing. Under namespace processing, Expat consumes 1121 <code>xmlns</code> and <code>xmlns:...</code> attributes, which declare 1122 namespaces for the scope of the element in which they occur. This means that your 1123 start handler will not see these attributes. Your application can still be 1124 informed of these declarations by setting namespace declaration handlers with 1125 <a href= 1126 "#XML_SetNamespaceDeclHandler"><code>XML_SetNamespaceDeclHandler</code></a>. 1127 </p> 1128 1129 <p> 1130 Element type and attribute names that belong to a given namespace are passed to 1131 the appropriate handler in expanded form. By default this expanded form is a 1132 concatenation of the namespace URI, the separator character (which is the 2nd 1133 argument to <code><a href="#XML_ParserCreateNS">XML_ParserCreateNS</a></code>), 1134 and the local name (i.e. the part after the colon). Names with undeclared 1135 prefixes are not well-formed when namespace processing is enabled, and will 1136 trigger an error. Unprefixed attribute names are never expanded, and unprefixed 1137 element names are only expanded when they are in the scope of a default 1138 namespace. 1139 </p> 1140 1141 <p> 1142 However if <code><a href= 1143 "#XML_SetReturnNSTriplet">XML_SetReturnNSTriplet</a></code> has been called with 1144 a non-zero <code>do_nst</code> parameter, then the expanded form for names with 1145 an explicit prefix is a concatenation of: URI, separator, local name, separator, 1146 prefix. 1147 </p> 1148 1149 <p> 1150 You can set handlers for the start of a namespace declaration and for the end of 1151 a scope of a declaration with the <code><a href= 1152 "#XML_SetNamespaceDeclHandler">XML_SetNamespaceDeclHandler</a></code> function. 1153 The StartNamespaceDeclHandler is called prior to the start tag handler and the 1154 EndNamespaceDeclHandler is called after the corresponding end tag that ends the 1155 namespace's scope. The namespace start handler gets passed the prefix and URI for 1156 the namespace. For a default namespace declaration (xmlns='...'), the prefix will 1157 be <code>NULL</code>. The URI will be <code>NULL</code> for the case where the 1158 default namespace is being unset. The namespace end handler just gets the prefix 1159 for the closing scope. 1160 </p> 1161 1162 <p> 1163 These handlers are called for each declaration. So if, for instance, a start tag 1164 had three namespace declarations, then the StartNamespaceDeclHandler would be 1165 called three times before the start tag handler is called, once for each 1166 declaration. 1167 </p> 1168 1169 <h3> 1170 Character Encodings 1171 </h3> 1172 1173 <p> 1174 While XML is based on Unicode, and every XML processor is required to recognized 1175 UTF-8 and UTF-16 (1 and 2 byte encodings of Unicode), other encodings may be 1176 declared in XML documents or entities. For the main document, an XML declaration 1177 may contain an encoding declaration: 1178 </p> 1179 1180 <pre> 1181<?xml version="1.0" encoding="ISO-8859-2"?> 1182</pre> 1183 <p> 1184 External parsed entities may begin with a text declaration, which looks like an 1185 XML declaration with just an encoding declaration: 1186 </p> 1187 1188 <pre> 1189<?xml encoding="Big5"?> 1190</pre> 1191 <p> 1192 With Expat, you may also specify an encoding at the time of creating a parser. 1193 This is useful when the encoding information may come from a source outside the 1194 document itself (like a higher level protocol.) 1195 </p> 1196 1197 <p> 1198 <a id="builtin_encodings" name="builtin_encodings"></a>There are four built-in 1199 encodings in Expat: 1200 </p> 1201 1202 <ul> 1203 <li>UTF-8 1204 </li> 1205 1206 <li>UTF-16 1207 </li> 1208 1209 <li>ISO-8859-1 1210 </li> 1211 1212 <li>US-ASCII 1213 </li> 1214 </ul> 1215 1216 <p> 1217 Anything else discovered in an encoding declaration or in the protocol encoding 1218 specified in the parser constructor, triggers a call to the 1219 <code>UnknownEncodingHandler</code>. This handler gets passed the encoding name 1220 and a pointer to an <code>XML_Encoding</code> data structure. Your handler must 1221 fill in this structure and return <code>XML_STATUS_OK</code> if it knows how to 1222 deal with the encoding. Otherwise the handler should return 1223 <code>XML_STATUS_ERROR</code>. The handler also gets passed a pointer to an 1224 optional application data structure that you may indicate when you set the 1225 handler. 1226 </p> 1227 1228 <p> 1229 Expat places restrictions on character encodings that it can support by filling 1230 in the <code>XML_Encoding</code> structure. include file: 1231 </p> 1232 1233 <ol> 1234 <li>Every ASCII character that can appear in a well-formed XML document must be 1235 represented by a single byte, and that byte must correspond to it's ASCII 1236 encoding (except for the characters $@\^'{}~) 1237 </li> 1238 1239 <li>Characters must be encoded in 4 bytes or less. 1240 </li> 1241 1242 <li>All characters encoded must have Unicode scalar values less than or equal to 1243 65535 (0xFFFF)<em>This does not apply to the built-in support for UTF-16 and 1244 UTF-8</em> 1245 </li> 1246 1247 <li>No character may be encoded by more that one distinct sequence of bytes 1248 </li> 1249 </ol> 1250 1251 <p> 1252 <code>XML_Encoding</code> contains an array of integers that correspond to the 1253 1st byte of an encoding sequence. If the value in the array for a byte is zero or 1254 positive, then the byte is a single byte encoding that encodes the Unicode scalar 1255 value contained in the array. A -1 in this array indicates a malformed byte. If 1256 the value is -2, -3, or -4, then the byte is the beginning of a 2, 3, or 4 byte 1257 sequence respectively. Multi-byte sequences are sent to the convert function 1258 pointed at in the <code>XML_Encoding</code> structure. This function should 1259 return the Unicode scalar value for the sequence or -1 if the sequence is 1260 malformed. 1261 </p> 1262 1263 <p> 1264 One pitfall that novice Expat users are likely to fall into is that although 1265 Expat may accept input in various encodings, the strings that it passes to the 1266 handlers are always encoded in UTF-8 or UTF-16 (depending on how Expat was 1267 compiled). Your application is responsible for any translation of these strings 1268 into other encodings. 1269 </p> 1270 1271 <h3> 1272 Handling External Entity References 1273 </h3> 1274 1275 <p> 1276 Expat does not read or parse external entities directly. Note that any external 1277 DTD is a special case of an external entity. If you've set no 1278 <code>ExternalEntityRefHandler</code>, then external entity references are 1279 silently ignored. Otherwise, it calls your handler with the information needed to 1280 read and parse the external entity. 1281 </p> 1282 1283 <p> 1284 Your handler isn't actually responsible for parsing the entity, but it is 1285 responsible for creating a subsidiary parser with <code><a href= 1286 "#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a></code> that 1287 will do the job. This returns an instance of <code>XML_Parser</code> that has 1288 handlers and other data structures initialized from the parent parser. You may 1289 then use <code><a href="#XML_Parse">XML_Parse</a></code> or <code><a href= 1290 "#XML_ParseBuffer">XML_ParseBuffer</a></code> calls against this parser. Since 1291 external entities my refer to other external entities, your handler should be 1292 prepared to be called recursively. 1293 </p> 1294 1295 <h3> 1296 Parsing DTDs 1297 </h3> 1298 1299 <p> 1300 In order to parse parameter entities, before starting the parse, you must call 1301 <code><a href="#XML_SetParamEntityParsing">XML_SetParamEntityParsing</a></code> 1302 with one of the following arguments: 1303 </p> 1304 1305 <dl> 1306 <dt> 1307 <code>XML_PARAM_ENTITY_PARSING_NEVER</code> 1308 </dt> 1309 1310 <dd> 1311 Don't parse parameter entities or the external subset 1312 </dd> 1313 1314 <dt> 1315 <code>XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE</code> 1316 </dt> 1317 1318 <dd> 1319 Parse parameter entities and the external subset unless <code>standalone</code> 1320 was set to "yes" in the XML declaration. 1321 </dd> 1322 1323 <dt> 1324 <code>XML_PARAM_ENTITY_PARSING_ALWAYS</code> 1325 </dt> 1326 1327 <dd> 1328 Always parse parameter entities and the external subset 1329 </dd> 1330 </dl> 1331 1332 <p> 1333 In order to read an external DTD, you also have to set an external entity 1334 reference handler as described above. 1335 </p> 1336 1337 <h3 id="stop-resume"> 1338 Temporarily Stopping Parsing 1339 </h3> 1340 1341 <p> 1342 Expat 1.95.8 introduces a new feature: its now possible to stop parsing 1343 temporarily from within a handler function, even if more data has already been 1344 passed into the parser. Applications for this include 1345 </p> 1346 1347 <ul> 1348 <li>Supporting the <a href="https://www.w3.org/TR/xinclude/">XInclude</a> 1349 specification. 1350 </li> 1351 1352 <li>Delaying further processing until additional information is available from 1353 some other source. 1354 </li> 1355 1356 <li>Adjusting processor load as task priorities shift within an application. 1357 </li> 1358 1359 <li>Stopping parsing completely (simply free or reset the parser instead of 1360 resuming in the outer parsing loop). This can be useful if an application-domain 1361 error is found in the XML being parsed or if the result of the parse is 1362 determined not to be useful after all. 1363 </li> 1364 </ul> 1365 1366 <p> 1367 To take advantage of this feature, the main parsing loop of an application needs 1368 to support this specifically. It cannot be supported with a parsing loop 1369 compatible with Expat 1.95.7 or earlier (though existing loops will continue to 1370 work without supporting the stop/resume feature). 1371 </p> 1372 1373 <p> 1374 An application that uses this feature for a single parser will have the rough 1375 structure (in pseudo-code): 1376 </p> 1377 1378 <pre class="pseudocode"> 1379fd = open_input() 1380p = create_parser() 1381 1382if parse_xml(p, fd) { 1383 /* suspended */ 1384 1385 int suspended = 1; 1386 1387 while (suspended) { 1388 do_something_else() 1389 if ready_to_resume() { 1390 suspended = continue_parsing(p, fd); 1391 } 1392 } 1393} 1394</pre> 1395 <p> 1396 An application that may resume any of several parsers based on input (either from 1397 the XML being parsed or some other source) will certainly have more interesting 1398 control structures. 1399 </p> 1400 1401 <p> 1402 This C function could be used for the <code>parse_xml</code> function mentioned 1403 in the pseudo-code above: 1404 </p> 1405 1406 <pre class="eg"> 1407#define BUFF_SIZE 10240 1408 1409/* Parse a document from the open file descriptor 'fd' until the parse 1410 is complete (the document has been completely parsed, or there's 1411 been an error), or the parse is stopped. Return non-zero when 1412 the parse is merely suspended. 1413*/ 1414int 1415parse_xml(XML_Parser p, int fd) 1416{ 1417 for (;;) { 1418 int last_chunk; 1419 int bytes_read; 1420 enum XML_Status status; 1421 1422 void *buff = XML_GetBuffer(p, BUFF_SIZE); 1423 if (buff == NULL) { 1424 /* handle error... */ 1425 return 0; 1426 } 1427 bytes_read = read(fd, buff, BUFF_SIZE); 1428 if (bytes_read < 0) { 1429 /* handle error... */ 1430 return 0; 1431 } 1432 status = XML_ParseBuffer(p, bytes_read, bytes_read == 0); 1433 switch (status) { 1434 case XML_STATUS_ERROR: 1435 /* handle error... */ 1436 return 0; 1437 case XML_STATUS_SUSPENDED: 1438 return 1; 1439 } 1440 if (bytes_read == 0) 1441 return 0; 1442 } 1443} 1444</pre> 1445 <p> 1446 The corresponding <code>continue_parsing</code> function is somewhat simpler, 1447 since it only need deal with the return code from <code><a href= 1448 "#XML_ResumeParser">XML_ResumeParser</a></code>; it can delegate the input 1449 handling to the <code>parse_xml</code> function: 1450 </p> 1451 1452 <pre class="eg"> 1453/* Continue parsing a document which had been suspended. The 'p' and 1454 'fd' arguments are the same as passed to parse_xml(). Return 1455 non-zero when the parse is suspended. 1456*/ 1457int 1458continue_parsing(XML_Parser p, int fd) 1459{ 1460 enum XML_Status status = XML_ResumeParser(p); 1461 switch (status) { 1462 case XML_STATUS_ERROR: 1463 /* handle error... */ 1464 return 0; 1465 case XML_ERROR_NOT_SUSPENDED: 1466 /* handle error... */ 1467 return 0;. 1468 case XML_STATUS_SUSPENDED: 1469 return 1; 1470 } 1471 return parse_xml(p, fd); 1472} 1473</pre> 1474 <p> 1475 Now that we've seen what a mess the top-level parsing loop can become, what have 1476 we gained? Very simply, we can now use the <code><a href= 1477 "#XML_StopParser">XML_StopParser</a></code> function to stop parsing, without 1478 having to go to great lengths to avoid additional processing that we're expecting 1479 to ignore. As a bonus, we get to stop parsing <em>temporarily</em>, and come back 1480 to it when we're ready. 1481 </p> 1482 1483 <p> 1484 To stop parsing from a handler function, use the <code><a href= 1485 "#XML_StopParser">XML_StopParser</a></code> function. This function takes two 1486 arguments; the parser being stopped and a flag indicating whether the parse can 1487 be resumed in the future. 1488 </p> 1489 <!-- XXX really need more here --> 1490 1491 <hr /> 1492 <!-- ================================================================ --> 1493 1494 <h2> 1495 <a id="reference" name="reference">Expat Reference</a> 1496 </h2> 1497 1498 <h3> 1499 <a id="creation" name="creation">Parser Creation</a> 1500 </h3> 1501 1502 <h4 id="XML_ParserCreate"> 1503 XML_ParserCreate 1504 </h4> 1505 1506 <pre class="fcndec"> 1507XML_Parser XMLCALL 1508XML_ParserCreate(const XML_Char *encoding); 1509</pre> 1510 <div class="fcndef"> 1511 <p> 1512 Construct a new parser. If encoding is non-<code>NULL</code>, it specifies a 1513 character encoding to use for the document. This overrides the document 1514 encoding declaration. There are four built-in encodings: 1515 </p> 1516 1517 <ul> 1518 <li>US-ASCII 1519 </li> 1520 1521 <li>UTF-8 1522 </li> 1523 1524 <li>UTF-16 1525 </li> 1526 1527 <li>ISO-8859-1 1528 </li> 1529 </ul> 1530 1531 <p> 1532 Any other value will invoke a call to the UnknownEncodingHandler. 1533 </p> 1534 </div> 1535 1536 <h4 id="XML_ParserCreateNS"> 1537 XML_ParserCreateNS 1538 </h4> 1539 1540 <pre class="fcndec"> 1541XML_Parser XMLCALL 1542XML_ParserCreateNS(const XML_Char *encoding, 1543 XML_Char sep); 1544</pre> 1545 <div class="fcndef"> 1546 Constructs a new parser that has namespace processing in effect. Namespace 1547 expanded element names and attribute names are returned as a concatenation of the 1548 namespace URI, <em>sep</em>, and the local part of the name. This means that you 1549 should pick a character for <em>sep</em> that can't be part of an URI. Since 1550 Expat does not check namespace URIs for conformance, the only safe choice for a 1551 namespace separator is a character that is illegal in XML. For instance, 1552 <code>'\xFF'</code> is not legal in UTF-8, and <code>'\xFFFF'</code> is not legal 1553 in UTF-16. There is a special case when <em>sep</em> is the null character 1554 <code>'\0'</code>: the namespace URI and the local part will be concatenated 1555 without any separator - this is intended to support RDF processors. It is a 1556 programming error to use the null separator with <a href= 1557 "#XML_SetReturnNSTriplet">namespace triplets</a>. 1558 </div> 1559 1560 <p> 1561 <strong>Note:</strong> Expat does not validate namespace URIs (beyond encoding) 1562 against RFC 3986 today (and is not required to do so with regard to the XML 1.0 1563 namespaces specification) but it may start doing that in future releases. Before 1564 that, an application using Expat must be ready to receive namespace URIs 1565 containing non-URI characters. 1566 </p> 1567 1568 <h4 id="XML_ParserCreate_MM"> 1569 XML_ParserCreate_MM 1570 </h4> 1571 1572 <pre class="fcndec"> 1573XML_Parser XMLCALL 1574XML_ParserCreate_MM(const XML_Char *encoding, 1575 const XML_Memory_Handling_Suite *ms, 1576 const XML_Char *sep); 1577</pre> 1578 1579 <pre class="signature"> 1580typedef struct { 1581 void *(XMLCALL *malloc_fcn)(size_t size); 1582 void *(XMLCALL *realloc_fcn)(void *ptr, size_t size); 1583 void (XMLCALL *free_fcn)(void *ptr); 1584} XML_Memory_Handling_Suite; 1585</pre> 1586 <div class="fcndef"> 1587 <p> 1588 Construct a new parser using the suite of memory handling functions specified 1589 in <code>ms</code>. If <code>ms</code> is <code>NULL</code>, then use the 1590 standard set of memory management functions. If <code>sep</code> is 1591 non-<code>NULL</code>, then namespace processing is enabled in the created 1592 parser and the character pointed at by sep is used as the separator between the 1593 namespace URI and the local part of the name. 1594 </p> 1595 </div> 1596 1597 <h4 id="XML_ExternalEntityParserCreate"> 1598 XML_ExternalEntityParserCreate 1599 </h4> 1600 1601 <pre class="fcndec"> 1602XML_Parser XMLCALL 1603XML_ExternalEntityParserCreate(XML_Parser p, 1604 const XML_Char *context, 1605 const XML_Char *encoding); 1606</pre> 1607 <div class="fcndef"> 1608 <p> 1609 Construct a new <code>XML_Parser</code> object for parsing an external general 1610 entity. Context is the context argument passed in a call to a 1611 ExternalEntityRefHandler. Other state information such as handlers, user data, 1612 namespace processing is inherited from the parser passed as the 1st argument. 1613 So you shouldn't need to call any of the behavior changing functions on this 1614 parser (unless you want it to act differently than the parent parser). 1615 </p> 1616 1617 <p> 1618 <strong>Note:</strong> Please be sure to free subparsers created by 1619 <code><a href= 1620 "#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a></code> 1621 <em>prior to</em> freeing their related parent parser, as subparsers reference 1622 and use parts of their respective parent parser, internally. Parent parsers 1623 must outlive subparsers. 1624 </p> 1625 </div> 1626 1627 <h4 id="XML_ParserFree"> 1628 XML_ParserFree 1629 </h4> 1630 1631 <pre class="fcndec"> 1632void XMLCALL 1633XML_ParserFree(XML_Parser p); 1634</pre> 1635 <div class="fcndef"> 1636 <p> 1637 Free memory used by the parser. 1638 </p> 1639 1640 <p> 1641 <strong>Note:</strong> Your application is responsible for freeing any memory 1642 associated with <a href="#userdata">user data</a>. 1643 </p> 1644 1645 <p> 1646 <strong>Note:</strong> Please be sure to free subparsers created by 1647 <code><a href= 1648 "#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a></code> 1649 <em>prior to</em> freeing their related parent parser, as subparsers reference 1650 and use parts of their respective parent parser, internally. Parent parsers 1651 must outlive subparsers. 1652 </p> 1653 </div> 1654 1655 <h4 id="XML_ParserReset"> 1656 XML_ParserReset 1657 </h4> 1658 1659 <pre class="fcndec"> 1660XML_Bool XMLCALL 1661XML_ParserReset(XML_Parser p, 1662 const XML_Char *encoding); 1663</pre> 1664 <div class="fcndef"> 1665 Clean up the memory structures maintained by the parser so that it may be used 1666 again. After this has been called, <code>parser</code> is ready to start parsing 1667 a new document. All handlers are cleared from the parser, except for the 1668 unknownEncodingHandler. The parser's external state is re-initialized except for 1669 the values of ns and ns_triplets. This function may not be used on a parser 1670 created using <code><a href= 1671 "#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a></code>; it 1672 will return <code>XML_FALSE</code> in that case. Returns <code>XML_TRUE</code> on 1673 success. Your application is responsible for dealing with any memory associated 1674 with <a href="#userdata">user data</a>. 1675 </div> 1676 1677 <h3> 1678 <a id="parsing" name="parsing">Parsing</a> 1679 </h3> 1680 1681 <p> 1682 To state the obvious: the three parsing functions <code><a href= 1683 "#XML_Parse">XML_Parse</a></code>, <code><a href= 1684 "#XML_ParseBuffer">XML_ParseBuffer</a></code> and <code><a href= 1685 "#XML_GetBuffer">XML_GetBuffer</a></code> as well as the two cleanup functions 1686 <code><a href="#XML_ParserFree">XML_ParserFree</a></code> and <code><a href= 1687 "#XML_ParserReset">XML_ParserReset</a></code> must not be called from within a 1688 handler unless they operate on a separate parser instance, that is, one that did 1689 not call the handler. For example, it is OK to call the parsing functions from 1690 within an <code>XML_ExternalEntityRefHandler</code>, if they apply to the parser 1691 created by <code><a href= 1692 "#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a></code>. 1693 </p> 1694 1695 <p> 1696 Note: The <code>len</code> argument passed to these functions should be 1697 considerably less than the maximum value for an integer, as it could create an 1698 integer overflow situation if the added lengths of a buffer and the unprocessed 1699 portion of the previous buffer exceed the maximum integer value. Input data at 1700 the end of a buffer will remain unprocessed if it is part of an XML token for 1701 which the end is not part of that buffer. 1702 </p> 1703 1704 <p> 1705 <a id="isFinal" name="isFinal"></a>The application <em>must</em> make a 1706 concluding <code><a href="#XML_Parse">XML_Parse</a></code> or <code><a href= 1707 "#XML_ParseBuffer">XML_ParseBuffer</a></code> call with <code>isFinal</code> set 1708 to <code>XML_TRUE</code>. 1709 </p> 1710 1711 <h4 id="XML_Parse"> 1712 XML_Parse 1713 </h4> 1714 1715 <pre class="fcndec"> 1716enum XML_Status XMLCALL 1717XML_Parse(XML_Parser p, 1718 const char *s, 1719 int len, 1720 int isFinal); 1721</pre> 1722 1723 <pre class="signature"> 1724enum XML_Status { 1725 XML_STATUS_ERROR = 0, 1726 XML_STATUS_OK = 1 1727}; 1728</pre> 1729 <div class="fcndef"> 1730 <p> 1731 Parse some more of the document. The string <code>s</code> is a buffer 1732 containing part (or perhaps all) of the document. The number of bytes of s that 1733 are part of the document is indicated by <code>len</code>. This means that 1734 <code>s</code> doesn't have to be null-terminated. It also means that if 1735 <code>len</code> is larger than the number of bytes in the block of memory that 1736 <code>s</code> points at, then a memory fault is likely. Negative values for 1737 <code>len</code> are rejected since Expat 2.2.1. The <code>isFinal</code> 1738 parameter informs the parser that this is the last piece of the document. 1739 Frequently, the last piece is empty (i.e. <code>len</code> is zero.) 1740 </p> 1741 1742 <p> 1743 If a parse error occurred, it returns <code>XML_STATUS_ERROR</code>. Otherwise 1744 it returns <code>XML_STATUS_OK</code> value. Note that regardless of the return 1745 value, there is no guarantee that all provided input has been parsed; only 1746 after <a href="#isFinal">the concluding call</a> will all handler callbacks and 1747 parsing errors have happened. 1748 </p> 1749 1750 <p> 1751 Simplified, <code>XML_Parse</code> can be considered a convenience wrapper that 1752 is pairing calls to <code><a href="#XML_GetBuffer">XML_GetBuffer</a></code> and 1753 <code><a href="#XML_ParseBuffer">XML_ParseBuffer</a></code> (when Expat is 1754 built with macro <code>XML_CONTEXT_BYTES</code> defined to a positive value, 1755 which is both common and default). <code>XML_Parse</code> is then functionally 1756 equivalent to calling <code><a href="#XML_GetBuffer">XML_GetBuffer</a></code>, 1757 <code>memcpy</code>, and <code><a href= 1758 "#XML_ParseBuffer">XML_ParseBuffer</a></code>. 1759 </p> 1760 1761 <p> 1762 To avoid double copying of the input, direct use of functions <code><a href= 1763 "#XML_GetBuffer">XML_GetBuffer</a></code> and <code><a href= 1764 "#XML_ParseBuffer">XML_ParseBuffer</a></code> is advised for most production 1765 use, e.g. if you're using <code>read</code> or similar functionality to fill 1766 your buffers, fill directly into the buffer from <code><a href= 1767 "#XML_GetBuffer">XML_GetBuffer</a></code>, then parse with <code><a href= 1768 "#XML_ParseBuffer">XML_ParseBuffer</a></code>. 1769 </p> 1770 </div> 1771 1772 <h4 id="XML_ParseBuffer"> 1773 XML_ParseBuffer 1774 </h4> 1775 1776 <pre class="fcndec"> 1777enum XML_Status XMLCALL 1778XML_ParseBuffer(XML_Parser p, 1779 int len, 1780 int isFinal); 1781</pre> 1782 <div class="fcndef"> 1783 <p> 1784 This is just like <code><a href="#XML_Parse">XML_Parse</a></code>, except in 1785 this case Expat provides the buffer. By obtaining the buffer from Expat with 1786 the <code><a href="#XML_GetBuffer">XML_GetBuffer</a></code> function, the 1787 application can avoid double copying of the input. 1788 </p> 1789 1790 <p> 1791 <strong>Note:</strong> It is the responsibility of the application code to pass 1792 a value for <code>len</code> that does not exceed the length of the remaining 1793 unparsed XML content that was previously copied into the buffer obtained from 1794 <code><a href="#XML_GetBuffer">XML_GetBuffer</a></code>. The documentation of 1795 <code><a href="#XML_GetBuffer">XML_GetBuffer</a></code> below has an <a href= 1796 "#XML_GetBuffer">example of typical use</a> of functions <code><a href= 1797 "#XML_GetBuffer">XML_GetBuffer</a></code> and <code><a href= 1798 "#XML_ParseBuffer">XML_ParseBuffer</a></code> in tandem. 1799 </p> 1800 1801 <p> 1802 Negative values for <code>len</code> are rejected since Expat 2.6.3. 1803 </p> 1804 </div> 1805 1806 <h4 id="XML_GetBuffer"> 1807 XML_GetBuffer 1808 </h4> 1809 1810 <pre class="fcndec"> 1811void * XMLCALL 1812XML_GetBuffer(XML_Parser p, 1813 int len); 1814</pre> 1815 <div class="fcndef"> 1816 Obtain a buffer of size <code>len</code> to read a piece of the document into. A 1817 <code>NULL</code> value is returned if Expat can't allocate enough memory for 1818 this buffer. A <code>NULL</code> value may also be returned if <code>len</code> 1819 is zero. This has to be called prior to every call to <code><a href= 1820 "#XML_ParseBuffer">XML_ParseBuffer</a></code>. A typical use would look like 1821 this: 1822 1823 <pre class="eg"> 1824for (;;) { 1825 int bytes_read; 1826 void *buff = XML_GetBuffer(p, BUFF_SIZE); 1827 if (buff == NULL) { 1828 /* handle error */ 1829 } 1830 1831 bytes_read = read(docfd, buff, BUFF_SIZE); 1832 if (bytes_read < 0) { 1833 /* handle error */ 1834 } 1835 1836 if (! XML_ParseBuffer(p, bytes_read, bytes_read == 0)) { 1837 /* handle parse error */ 1838 } 1839 1840 if (bytes_read == 0) 1841 break; 1842} 1843</pre> 1844 </div> 1845 1846 <h4 id="XML_StopParser"> 1847 XML_StopParser 1848 </h4> 1849 1850 <pre class="fcndec"> 1851enum XML_Status XMLCALL 1852XML_StopParser(XML_Parser p, 1853 XML_Bool resumable); 1854</pre> 1855 <div class="fcndef"> 1856 <p> 1857 Stops parsing as soon as possible, causing <code><a href= 1858 "#XML_Parse">XML_Parse</a></code> or <code><a href= 1859 "#XML_ParseBuffer">XML_ParseBuffer</a></code> to return. Must be called from 1860 within a call-back handler, except when aborting (when <code>resumable</code> 1861 is <code>XML_FALSE</code>) an already suspended parser. 1862 </p> 1863 1864 <p> 1865 <strong>Note:</strong> Some call-backs may still follow because they would 1866 otherwise get lost, including 1867 </p> 1868 1869 <ul> 1870 <li>the end element handler for empty elements when stopped in the start 1871 element handler, 1872 </li> 1873 1874 <li>the end namespace declaration handler when stopped in the end element 1875 handler, 1876 </li> 1877 1878 <li>the character data handler when stopped in the character data handler while 1879 making multiple call-backs on a contiguous chunk of characters, 1880 </li> 1881 </ul> 1882 1883 <p> 1884 and possibly others. 1885 </p> 1886 1887 <p> 1888 This can be called from most handlers, including DTD related call-backs, except 1889 when parsing an external parameter entity and <code>resumable</code> is 1890 <code>XML_TRUE</code>. Returns <code>XML_STATUS_OK</code> when successful, 1891 <code>XML_STATUS_ERROR</code> otherwise. The possible error codes are: 1892 </p> 1893 1894 <dl> 1895 <dt> 1896 <code>XML_ERROR_NOT_STARTED</code> 1897 </dt> 1898 1899 <dd> 1900 when stopping or suspending a parser before it has started, added in Expat 1901 2.6.4. 1902 </dd> 1903 1904 <dt> 1905 <code>XML_ERROR_SUSPENDED</code> 1906 </dt> 1907 1908 <dd> 1909 when suspending an already suspended parser. 1910 </dd> 1911 1912 <dt> 1913 <code>XML_ERROR_FINISHED</code> 1914 </dt> 1915 1916 <dd> 1917 when the parser has already finished. 1918 </dd> 1919 1920 <dt> 1921 <code>XML_ERROR_SUSPEND_PE</code> 1922 </dt> 1923 1924 <dd> 1925 when suspending while parsing an external PE. 1926 </dd> 1927 </dl> 1928 1929 <p> 1930 Since the stop/resume feature requires application support in the outer parsing 1931 loop, it is an error to call this function for a parser not being handled 1932 appropriately; see <a href="#stop-resume">Temporarily Stopping Parsing</a> for 1933 more information. 1934 </p> 1935 1936 <p> 1937 When <code>resumable</code> is <code>XML_TRUE</code> then parsing is 1938 <em>suspended</em>, that is, <code><a href="#XML_Parse">XML_Parse</a></code> 1939 and <code><a href="#XML_ParseBuffer">XML_ParseBuffer</a></code> return 1940 <code>XML_STATUS_SUSPENDED</code>. Otherwise, parsing is <em>aborted</em>, that 1941 is, <code><a href="#XML_Parse">XML_Parse</a></code> and <code><a href= 1942 "#XML_ParseBuffer">XML_ParseBuffer</a></code> return 1943 <code>XML_STATUS_ERROR</code> with error code <code>XML_ERROR_ABORTED</code>. 1944 </p> 1945 1946 <p> 1947 <strong>Note:</strong> This will be applied to the current parser instance 1948 only, that is, if there is a parent parser then it will continue parsing when 1949 the external entity reference handler returns. It is up to the implementation 1950 of that handler to call <code><a href= 1951 "#XML_StopParser">XML_StopParser</a></code> on the parent parser (recursively), 1952 if one wants to stop parsing altogether. 1953 </p> 1954 1955 <p> 1956 When suspended, parsing can be resumed by calling <code><a href= 1957 "#XML_ResumeParser">XML_ResumeParser</a></code>. 1958 </p> 1959 1960 <p> 1961 New in Expat 1.95.8. 1962 </p> 1963 </div> 1964 1965 <h4 id="XML_ResumeParser"> 1966 XML_ResumeParser 1967 </h4> 1968 1969 <pre class="fcndec"> 1970enum XML_Status XMLCALL 1971XML_ResumeParser(XML_Parser p); 1972</pre> 1973 <div class="fcndef"> 1974 <p> 1975 Resumes parsing after it has been suspended with <code><a href= 1976 "#XML_StopParser">XML_StopParser</a></code>. Must not be called from within a 1977 handler call-back. Returns same status codes as <code><a href= 1978 "#XML_Parse">XML_Parse</a></code> or <code><a href= 1979 "#XML_ParseBuffer">XML_ParseBuffer</a></code>. An additional error code, 1980 <code>XML_ERROR_NOT_SUSPENDED</code>, will be returned if the parser was not 1981 currently suspended. 1982 </p> 1983 1984 <p> 1985 <strong>Note:</strong> This must be called on the most deeply nested child 1986 parser instance first, and on its parent parser only after the child parser has 1987 finished, to be applied recursively until the document entity's parser is 1988 restarted. That is, the parent parser will not resume by itself and it is up to 1989 the application to call <code><a href= 1990 "#XML_ResumeParser">XML_ResumeParser</a></code> on it at the appropriate 1991 moment. 1992 </p> 1993 1994 <p> 1995 New in Expat 1.95.8. 1996 </p> 1997 </div> 1998 1999 <h4 id="XML_GetParsingStatus"> 2000 XML_GetParsingStatus 2001 </h4> 2002 2003 <pre class="fcndec"> 2004void XMLCALL 2005XML_GetParsingStatus(XML_Parser p, 2006 XML_ParsingStatus *status); 2007</pre> 2008 2009 <pre class="signature"> 2010enum XML_Parsing { 2011 XML_INITIALIZED, 2012 XML_PARSING, 2013 XML_FINISHED, 2014 XML_SUSPENDED 2015}; 2016 2017typedef struct { 2018 enum XML_Parsing parsing; 2019 XML_Bool finalBuffer; 2020} XML_ParsingStatus; 2021</pre> 2022 <div class="fcndef"> 2023 <p> 2024 Returns status of parser with respect to being initialized, parsing, finished, 2025 or suspended, and whether the final buffer is being processed. The 2026 <code>status</code> parameter <em>must not</em> be <code>NULL</code>. 2027 </p> 2028 2029 <p> 2030 New in Expat 1.95.8. 2031 </p> 2032 </div> 2033 2034 <h3> 2035 <a id="setting" name="setting">Handler Setting</a> 2036 </h3> 2037 2038 <p> 2039 Although handlers are typically set prior to parsing and left alone, an 2040 application may choose to set or change the handler for a parsing event while the 2041 parse is in progress. For instance, your application may choose to ignore all 2042 text not descended from a <code>para</code> element. One way it could do this is 2043 to set the character handler when a para start tag is seen, and unset it for the 2044 corresponding end tag. 2045 </p> 2046 2047 <p> 2048 A handler may be <em>unset</em> by providing a <code>NULL</code> pointer to the 2049 appropriate handler setter. None of the handler setting functions have a return 2050 value. 2051 </p> 2052 2053 <p> 2054 Your handlers will be receiving strings in arrays of type <code>XML_Char</code>. 2055 This type is conditionally defined in expat.h as either <code>char</code>, 2056 <code>wchar_t</code> or <code>unsigned short</code>. The former implies UTF-8 2057 encoding, the latter two imply UTF-16 encoding. Note that you'll receive them in 2058 this form independent of the original encoding of the document. 2059 </p> 2060 2061 <div class="handler"> 2062 <h4 id="XML_SetStartElementHandler"> 2063 XML_SetStartElementHandler 2064 </h4> 2065 2066 <pre class="setter"> 2067void XMLCALL 2068XML_SetStartElementHandler(XML_Parser p, 2069 XML_StartElementHandler start); 2070</pre> 2071 2072 <pre class="signature"> 2073typedef void 2074(XMLCALL *XML_StartElementHandler)(void *userData, 2075 const XML_Char *name, 2076 const XML_Char **atts); 2077</pre> 2078 <p> 2079 Set handler for start (and empty) tags. Attributes are passed to the start 2080 handler as a pointer to a vector of char pointers. Each attribute seen in a 2081 start (or empty) tag occupies 2 consecutive places in this vector: the 2082 attribute name followed by the attribute value. These pairs are terminated by a 2083 <code>NULL</code> pointer. 2084 </p> 2085 2086 <p> 2087 Note that an empty tag generates a call to both start and end handlers (in that 2088 order). 2089 </p> 2090 </div> 2091 2092 <div class="handler"> 2093 <h4 id="XML_SetEndElementHandler"> 2094 XML_SetEndElementHandler 2095 </h4> 2096 2097 <pre class="setter"> 2098void XMLCALL 2099XML_SetEndElementHandler(XML_Parser p, 2100 XML_EndElementHandler); 2101</pre> 2102 2103 <pre class="signature"> 2104typedef void 2105(XMLCALL *XML_EndElementHandler)(void *userData, 2106 const XML_Char *name); 2107</pre> 2108 <p> 2109 Set handler for end (and empty) tags. As noted above, an empty tag generates a 2110 call to both start and end handlers. 2111 </p> 2112 </div> 2113 2114 <div class="handler"> 2115 <h4 id="XML_SetElementHandler"> 2116 XML_SetElementHandler 2117 </h4> 2118 2119 <pre class="setter"> 2120void XMLCALL 2121XML_SetElementHandler(XML_Parser p, 2122 XML_StartElementHandler start, 2123 XML_EndElementHandler end); 2124</pre> 2125 <p> 2126 Set handlers for start and end tags with one call. 2127 </p> 2128 </div> 2129 2130 <div class="handler"> 2131 <h4 id="XML_SetCharacterDataHandler"> 2132 XML_SetCharacterDataHandler 2133 </h4> 2134 2135 <pre class="setter"> 2136void XMLCALL 2137XML_SetCharacterDataHandler(XML_Parser p, 2138 XML_CharacterDataHandler charhndl) 2139</pre> 2140 2141 <pre class="signature"> 2142typedef void 2143(XMLCALL *XML_CharacterDataHandler)(void *userData, 2144 const XML_Char *s, 2145 int len); 2146</pre> 2147 <p> 2148 Set a text handler. The string your handler receives is <em>NOT 2149 null-terminated</em>. You have to use the length argument to deal with the end 2150 of the string. A single block of contiguous text free of markup may still 2151 result in a sequence of calls to this handler. In other words, if you're 2152 searching for a pattern in the text, it may be split across calls to this 2153 handler. Note: Setting this handler to <code>NULL</code> may <em>NOT 2154 immediately</em> terminate call-backs if the parser is currently processing 2155 such a single block of contiguous markup-free text, as the parser will continue 2156 calling back until the end of the block is reached. 2157 </p> 2158 </div> 2159 2160 <div class="handler"> 2161 <h4 id="XML_SetProcessingInstructionHandler"> 2162 XML_SetProcessingInstructionHandler 2163 </h4> 2164 2165 <pre class="setter"> 2166void XMLCALL 2167XML_SetProcessingInstructionHandler(XML_Parser p, 2168 XML_ProcessingInstructionHandler proc) 2169</pre> 2170 2171 <pre class="signature"> 2172typedef void 2173(XMLCALL *XML_ProcessingInstructionHandler)(void *userData, 2174 const XML_Char *target, 2175 const XML_Char *data); 2176 2177</pre> 2178 <p> 2179 Set a handler for processing instructions. The target is the first word in the 2180 processing instruction. The data is the rest of the characters in it after 2181 skipping all whitespace after the initial word. 2182 </p> 2183 </div> 2184 2185 <div class="handler"> 2186 <h4 id="XML_SetCommentHandler"> 2187 XML_SetCommentHandler 2188 </h4> 2189 2190 <pre class="setter"> 2191void XMLCALL 2192XML_SetCommentHandler(XML_Parser p, 2193 XML_CommentHandler cmnt) 2194</pre> 2195 2196 <pre class="signature"> 2197typedef void 2198(XMLCALL *XML_CommentHandler)(void *userData, 2199 const XML_Char *data); 2200</pre> 2201 <p> 2202 Set a handler for comments. The data is all text inside the comment delimiters. 2203 </p> 2204 </div> 2205 2206 <div class="handler"> 2207 <h4 id="XML_SetStartCdataSectionHandler"> 2208 XML_SetStartCdataSectionHandler 2209 </h4> 2210 2211 <pre class="setter"> 2212void XMLCALL 2213XML_SetStartCdataSectionHandler(XML_Parser p, 2214 XML_StartCdataSectionHandler start); 2215</pre> 2216 2217 <pre class="signature"> 2218typedef void 2219(XMLCALL *XML_StartCdataSectionHandler)(void *userData); 2220</pre> 2221 <p> 2222 Set a handler that gets called at the beginning of a CDATA section. 2223 </p> 2224 </div> 2225 2226 <div class="handler"> 2227 <h4 id="XML_SetEndCdataSectionHandler"> 2228 XML_SetEndCdataSectionHandler 2229 </h4> 2230 2231 <pre class="setter"> 2232void XMLCALL 2233XML_SetEndCdataSectionHandler(XML_Parser p, 2234 XML_EndCdataSectionHandler end); 2235</pre> 2236 2237 <pre class="signature"> 2238typedef void 2239(XMLCALL *XML_EndCdataSectionHandler)(void *userData); 2240</pre> 2241 <p> 2242 Set a handler that gets called at the end of a CDATA section. 2243 </p> 2244 </div> 2245 2246 <div class="handler"> 2247 <h4 id="XML_SetCdataSectionHandler"> 2248 XML_SetCdataSectionHandler 2249 </h4> 2250 2251 <pre class="setter"> 2252void XMLCALL 2253XML_SetCdataSectionHandler(XML_Parser p, 2254 XML_StartCdataSectionHandler start, 2255 XML_EndCdataSectionHandler end) 2256</pre> 2257 <p> 2258 Sets both CDATA section handlers with one call. 2259 </p> 2260 </div> 2261 2262 <div class="handler"> 2263 <h4 id="XML_SetDefaultHandler"> 2264 XML_SetDefaultHandler 2265 </h4> 2266 2267 <pre class="setter"> 2268void XMLCALL 2269XML_SetDefaultHandler(XML_Parser p, 2270 XML_DefaultHandler hndl) 2271</pre> 2272 2273 <pre class="signature"> 2274typedef void 2275(XMLCALL *XML_DefaultHandler)(void *userData, 2276 const XML_Char *s, 2277 int len); 2278</pre> 2279 <p> 2280 Sets a handler for any characters in the document which wouldn't otherwise be 2281 handled. This includes both data for which no handlers can be set (like some 2282 kinds of DTD declarations) and data which could be reported but which currently 2283 has no handler set. The characters are passed exactly as they were present in 2284 the XML document except that they will be encoded in UTF-8 or UTF-16. Line 2285 boundaries are not normalized. Note that a byte order mark character is not 2286 passed to the default handler. There are no guarantees about how characters are 2287 divided between calls to the default handler: for example, a comment might be 2288 split between multiple calls. Setting the handler with this call has the side 2289 effect of turning off expansion of references to internally defined general 2290 entities. Instead these references are passed to the default handler. 2291 </p> 2292 2293 <p> 2294 See also <code><a href="#XML_DefaultCurrent">XML_DefaultCurrent</a></code>. 2295 </p> 2296 </div> 2297 2298 <div class="handler"> 2299 <h4 id="XML_SetDefaultHandlerExpand"> 2300 XML_SetDefaultHandlerExpand 2301 </h4> 2302 2303 <pre class="setter"> 2304void XMLCALL 2305XML_SetDefaultHandlerExpand(XML_Parser p, 2306 XML_DefaultHandler hndl) 2307</pre> 2308 2309 <pre class="signature"> 2310typedef void 2311(XMLCALL *XML_DefaultHandler)(void *userData, 2312 const XML_Char *s, 2313 int len); 2314</pre> 2315 <p> 2316 This sets a default handler, but doesn't inhibit the expansion of internal 2317 entity references. The entity reference will not be passed to the default 2318 handler. 2319 </p> 2320 2321 <p> 2322 See also <code><a href="#XML_DefaultCurrent">XML_DefaultCurrent</a></code>. 2323 </p> 2324 </div> 2325 2326 <div class="handler"> 2327 <h4 id="XML_SetExternalEntityRefHandler"> 2328 XML_SetExternalEntityRefHandler 2329 </h4> 2330 2331 <pre class="setter"> 2332void XMLCALL 2333XML_SetExternalEntityRefHandler(XML_Parser p, 2334 XML_ExternalEntityRefHandler hndl) 2335</pre> 2336 2337 <pre class="signature"> 2338typedef int 2339(XMLCALL *XML_ExternalEntityRefHandler)(XML_Parser p, 2340 const XML_Char *context, 2341 const XML_Char *base, 2342 const XML_Char *systemId, 2343 const XML_Char *publicId); 2344</pre> 2345 <p> 2346 Set an external entity reference handler. This handler is also called for 2347 processing an external DTD subset if parameter entity parsing is in effect. 2348 (See <a href= 2349 "#XML_SetParamEntityParsing"><code>XML_SetParamEntityParsing</code></a>.) 2350 </p> 2351 2352 <p> 2353 <strong>Warning:</strong> Using an external entity reference handler can lead 2354 to <a href="https://libexpat.github.io/doc/xml-security/#external-entities">XXE 2355 vulnerabilities</a>. It should only be used in applications that do not parse 2356 untrusted XML input. 2357 </p> 2358 2359 <p> 2360 The <code>context</code> parameter specifies the parsing context in the format 2361 expected by the <code>context</code> argument to <code><a href= 2362 "#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a></code>. 2363 <code>code</code> is valid only until the handler returns, so if the referenced 2364 entity is to be parsed later, it must be copied. <code>context</code> is 2365 <code>NULL</code> only when the entity is a parameter entity, which is how one 2366 can differentiate between general and parameter entities. 2367 </p> 2368 2369 <p> 2370 The <code>base</code> parameter is the base to use for relative system 2371 identifiers. It is set by <code><a href="#XML_SetBase">XML_SetBase</a></code> 2372 and may be <code>NULL</code>. The <code>publicId</code> parameter is the public 2373 id given in the entity declaration and may be <code>NULL</code>. 2374 <code>systemId</code> is the system identifier specified in the entity 2375 declaration and is never <code>NULL</code>. 2376 </p> 2377 2378 <p> 2379 There are a couple of ways in which this handler differs from others. First, 2380 this handler returns a status indicator (an integer). 2381 <code>XML_STATUS_OK</code> should be returned for successful handling of the 2382 external entity reference. Returning <code>XML_STATUS_ERROR</code> indicates 2383 failure, and causes the calling parser to return an 2384 <code>XML_ERROR_EXTERNAL_ENTITY_HANDLING</code> error. 2385 </p> 2386 2387 <p> 2388 Second, instead of having the user data as its first argument, it receives the 2389 parser that encountered the entity reference. This, along with the context 2390 parameter, may be used as arguments to a call to <code><a href= 2391 "#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a></code>. 2392 Using the returned parser, the body of the external entity can be recursively 2393 parsed. 2394 </p> 2395 2396 <p> 2397 Since this handler may be called recursively, it should not be saving 2398 information into global or static variables. 2399 </p> 2400 </div> 2401 2402 <h4 id="XML_SetExternalEntityRefHandlerArg"> 2403 XML_SetExternalEntityRefHandlerArg 2404 </h4> 2405 2406 <pre class="fcndec"> 2407void XMLCALL 2408XML_SetExternalEntityRefHandlerArg(XML_Parser p, 2409 void *arg) 2410</pre> 2411 <div class="fcndef"> 2412 <p> 2413 Set the argument passed to the ExternalEntityRefHandler. If <code>arg</code> is 2414 not <code>NULL</code>, it is the new value passed to the handler set using 2415 <code><a href= 2416 "#XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler</a></code>; 2417 if <code>arg</code> is <code>NULL</code>, the argument passed to the handler 2418 function will be the parser object itself. 2419 </p> 2420 2421 <p> 2422 <strong>Note:</strong> The type of <code>arg</code> and the type of the first 2423 argument to the ExternalEntityRefHandler do not match. This function takes a 2424 <code>void *</code> to be passed to the handler, while the handler accepts an 2425 <code>XML_Parser</code>. This is a historical accident, but will not be 2426 corrected before Expat 2.0 (at the earliest) to avoid causing compiler warnings 2427 for code that's known to work with this API. It is the responsibility of the 2428 application code to know the actual type of the argument passed to the handler 2429 and to manage it properly. 2430 </p> 2431 </div> 2432 2433 <div class="handler"> 2434 <h4 id="XML_SetSkippedEntityHandler"> 2435 XML_SetSkippedEntityHandler 2436 </h4> 2437 2438 <pre class="setter"> 2439void XMLCALL 2440XML_SetSkippedEntityHandler(XML_Parser p, 2441 XML_SkippedEntityHandler handler) 2442</pre> 2443 2444 <pre class="signature"> 2445typedef void 2446(XMLCALL *XML_SkippedEntityHandler)(void *userData, 2447 const XML_Char *entityName, 2448 int is_parameter_entity); 2449</pre> 2450 <p> 2451 Set a skipped entity handler. This is called in two situations: 2452 </p> 2453 2454 <ol> 2455 <li>An entity reference is encountered for which no declaration has been read 2456 <em>and</em> this is not an error. 2457 </li> 2458 2459 <li>An internal entity reference is read, but not expanded, because <a href= 2460 "#XML_SetDefaultHandler"><code>XML_SetDefaultHandler</code></a> has been 2461 called. 2462 </li> 2463 </ol> 2464 2465 <p> 2466 The <code>is_parameter_entity</code> argument will be non-zero for a parameter 2467 entity and zero for a general entity. 2468 </p> 2469 2470 <p> 2471 Note: Skipped parameter entities in declarations and skipped general entities 2472 in attribute values cannot be reported, because the event would be out of sync 2473 with the reporting of the declarations or attribute values 2474 </p> 2475 </div> 2476 2477 <div class="handler"> 2478 <h4 id="XML_SetUnknownEncodingHandler"> 2479 XML_SetUnknownEncodingHandler 2480 </h4> 2481 2482 <pre class="setter"> 2483void XMLCALL 2484XML_SetUnknownEncodingHandler(XML_Parser p, 2485 XML_UnknownEncodingHandler enchandler, 2486 void *encodingHandlerData) 2487</pre> 2488 2489 <pre class="signature"> 2490typedef int 2491(XMLCALL *XML_UnknownEncodingHandler)(void *encodingHandlerData, 2492 const XML_Char *name, 2493 XML_Encoding *info); 2494 2495typedef struct { 2496 int map[256]; 2497 void *data; 2498 int (XMLCALL *convert)(void *data, const char *s); 2499 void (XMLCALL *release)(void *data); 2500} XML_Encoding; 2501</pre> 2502 <p> 2503 Set a handler to deal with encodings other than the <a href= 2504 "#builtin_encodings">built in set</a>. This should be done before 2505 <code><a href="#XML_Parse">XML_Parse</a></code> or <code><a href= 2506 "#XML_ParseBuffer">XML_ParseBuffer</a></code> have been called on the given 2507 parser. 2508 </p> 2509 2510 <p> 2511 If the handler knows how to deal with an encoding with the given name, it 2512 should fill in the <code>info</code> data structure and return 2513 <code>XML_STATUS_OK</code>. Otherwise it should return 2514 <code>XML_STATUS_ERROR</code>. The handler will be called at most once per 2515 parsed (external) entity. The optional application data pointer 2516 <code>encodingHandlerData</code> will be passed back to the handler. 2517 </p> 2518 2519 <p> 2520 The map array contains information for every possible leading byte in a byte 2521 sequence. If the corresponding value is >= 0, then it's a single byte 2522 sequence and the byte encodes that Unicode value. If the value is -1, then that 2523 byte is invalid as the initial byte in a sequence. If the value is -n, where n 2524 is an integer > 1, then n is the number of bytes in the sequence and the 2525 actual conversion is accomplished by a call to the function pointed at by 2526 convert. This function may return -1 if the sequence itself is invalid. The 2527 convert pointer may be <code>NULL</code> if there are only single byte codes. 2528 The data parameter passed to the convert function is the data pointer from 2529 <code>XML_Encoding</code>. The string s is <em>NOT</em> null-terminated and 2530 points at the sequence of bytes to be converted. 2531 </p> 2532 2533 <p> 2534 The function pointed at by <code>release</code> is called by the parser when it 2535 is finished with the encoding. It may be <code>NULL</code>. 2536 </p> 2537 </div> 2538 2539 <div class="handler"> 2540 <h4 id="XML_SetStartNamespaceDeclHandler"> 2541 XML_SetStartNamespaceDeclHandler 2542 </h4> 2543 2544 <pre class="setter"> 2545void XMLCALL 2546XML_SetStartNamespaceDeclHandler(XML_Parser p, 2547 XML_StartNamespaceDeclHandler start); 2548</pre> 2549 2550 <pre class="signature"> 2551typedef void 2552(XMLCALL *XML_StartNamespaceDeclHandler)(void *userData, 2553 const XML_Char *prefix, 2554 const XML_Char *uri); 2555</pre> 2556 <p> 2557 Set a handler to be called when a namespace is declared. Namespace declarations 2558 occur inside start tags. But the namespace declaration start handler is called 2559 before the start tag handler for each namespace declared in that start tag. 2560 </p> 2561 </div> 2562 2563 <div class="handler"> 2564 <h4 id="XML_SetEndNamespaceDeclHandler"> 2565 XML_SetEndNamespaceDeclHandler 2566 </h4> 2567 2568 <pre class="setter"> 2569void XMLCALL 2570XML_SetEndNamespaceDeclHandler(XML_Parser p, 2571 XML_EndNamespaceDeclHandler end); 2572</pre> 2573 2574 <pre class="signature"> 2575typedef void 2576(XMLCALL *XML_EndNamespaceDeclHandler)(void *userData, 2577 const XML_Char *prefix); 2578</pre> 2579 <p> 2580 Set a handler to be called when leaving the scope of a namespace declaration. 2581 This will be called, for each namespace declaration, after the handler for the 2582 end tag of the element in which the namespace was declared. 2583 </p> 2584 </div> 2585 2586 <div class="handler"> 2587 <h4 id="XML_SetNamespaceDeclHandler"> 2588 XML_SetNamespaceDeclHandler 2589 </h4> 2590 2591 <pre class="setter"> 2592void XMLCALL 2593XML_SetNamespaceDeclHandler(XML_Parser p, 2594 XML_StartNamespaceDeclHandler start, 2595 XML_EndNamespaceDeclHandler end) 2596</pre> 2597 <p> 2598 Sets both namespace declaration handlers with a single call. 2599 </p> 2600 </div> 2601 2602 <div class="handler"> 2603 <h4 id="XML_SetXmlDeclHandler"> 2604 XML_SetXmlDeclHandler 2605 </h4> 2606 2607 <pre class="setter"> 2608void XMLCALL 2609XML_SetXmlDeclHandler(XML_Parser p, 2610 XML_XmlDeclHandler xmldecl); 2611</pre> 2612 2613 <pre class="signature"> 2614typedef void 2615(XMLCALL *XML_XmlDeclHandler)(void *userData, 2616 const XML_Char *version, 2617 const XML_Char *encoding, 2618 int standalone); 2619</pre> 2620 <p> 2621 Sets a handler that is called for XML declarations and also for text 2622 declarations discovered in external entities. The way to distinguish is that 2623 the <code>version</code> parameter will be <code>NULL</code> for text 2624 declarations. The <code>encoding</code> parameter may be <code>NULL</code> for 2625 an XML declaration. The <code>standalone</code> argument will contain -1, 0, or 2626 1 indicating respectively that there was no standalone parameter in the 2627 declaration, that it was given as no, or that it was given as yes. 2628 </p> 2629 </div> 2630 2631 <div class="handler"> 2632 <h4 id="XML_SetStartDoctypeDeclHandler"> 2633 XML_SetStartDoctypeDeclHandler 2634 </h4> 2635 2636 <pre class="setter"> 2637void XMLCALL 2638XML_SetStartDoctypeDeclHandler(XML_Parser p, 2639 XML_StartDoctypeDeclHandler start); 2640</pre> 2641 2642 <pre class="signature"> 2643typedef void 2644(XMLCALL *XML_StartDoctypeDeclHandler)(void *userData, 2645 const XML_Char *doctypeName, 2646 const XML_Char *sysid, 2647 const XML_Char *pubid, 2648 int has_internal_subset); 2649</pre> 2650 <p> 2651 Set a handler that is called at the start of a DOCTYPE declaration, before any 2652 external or internal subset is parsed. Both <code>sysid</code> and 2653 <code>pubid</code> may be <code>NULL</code>. The 2654 <code>has_internal_subset</code> will be non-zero if the DOCTYPE declaration 2655 has an internal subset. 2656 </p> 2657 </div> 2658 2659 <div class="handler"> 2660 <h4 id="XML_SetEndDoctypeDeclHandler"> 2661 XML_SetEndDoctypeDeclHandler 2662 </h4> 2663 2664 <pre class="setter"> 2665void XMLCALL 2666XML_SetEndDoctypeDeclHandler(XML_Parser p, 2667 XML_EndDoctypeDeclHandler end); 2668</pre> 2669 2670 <pre class="signature"> 2671typedef void 2672(XMLCALL *XML_EndDoctypeDeclHandler)(void *userData); 2673</pre> 2674 <p> 2675 Set a handler that is called at the end of a DOCTYPE declaration, after parsing 2676 any external subset. 2677 </p> 2678 </div> 2679 2680 <div class="handler"> 2681 <h4 id="XML_SetDoctypeDeclHandler"> 2682 XML_SetDoctypeDeclHandler 2683 </h4> 2684 2685 <pre class="setter"> 2686void XMLCALL 2687XML_SetDoctypeDeclHandler(XML_Parser p, 2688 XML_StartDoctypeDeclHandler start, 2689 XML_EndDoctypeDeclHandler end); 2690</pre> 2691 <p> 2692 Set both doctype handlers with one call. 2693 </p> 2694 </div> 2695 2696 <div class="handler"> 2697 <h4 id="XML_SetElementDeclHandler"> 2698 XML_SetElementDeclHandler 2699 </h4> 2700 2701 <pre class="setter"> 2702void XMLCALL 2703XML_SetElementDeclHandler(XML_Parser p, 2704 XML_ElementDeclHandler eldecl); 2705</pre> 2706 2707 <pre class="signature"> 2708typedef void 2709(XMLCALL *XML_ElementDeclHandler)(void *userData, 2710 const XML_Char *name, 2711 XML_Content *model); 2712</pre> 2713 2714 <pre class="signature"> 2715enum XML_Content_Type { 2716 XML_CTYPE_EMPTY = 1, 2717 XML_CTYPE_ANY, 2718 XML_CTYPE_MIXED, 2719 XML_CTYPE_NAME, 2720 XML_CTYPE_CHOICE, 2721 XML_CTYPE_SEQ 2722}; 2723 2724enum XML_Content_Quant { 2725 XML_CQUANT_NONE, 2726 XML_CQUANT_OPT, 2727 XML_CQUANT_REP, 2728 XML_CQUANT_PLUS 2729}; 2730 2731typedef struct XML_cp XML_Content; 2732 2733struct XML_cp { 2734 enum XML_Content_Type type; 2735 enum XML_Content_Quant quant; 2736 const XML_Char * name; 2737 unsigned int numchildren; 2738 XML_Content * children; 2739}; 2740</pre> 2741 <p> 2742 Sets a handler for element declarations in a DTD. The handler gets called with 2743 the name of the element in the declaration and a pointer to a structure that 2744 contains the element model. It's the user code's responsibility to free model 2745 when finished with via a call to <code><a href= 2746 "#XML_FreeContentModel">XML_FreeContentModel</a></code>. There is no need to 2747 free the model from the handler, it can be kept around and freed at a later 2748 stage. 2749 </p> 2750 2751 <p> 2752 The <code>model</code> argument is the root of a tree of 2753 <code>XML_Content</code> nodes. If <code>type</code> equals 2754 <code>XML_CTYPE_EMPTY</code> or <code>XML_CTYPE_ANY</code>, then 2755 <code>quant</code> will be <code>XML_CQUANT_NONE</code>, and the other fields 2756 will be zero or <code>NULL</code>. If <code>type</code> is 2757 <code>XML_CTYPE_MIXED</code>, then <code>quant</code> will be 2758 <code>XML_CQUANT_NONE</code> or <code>XML_CQUANT_REP</code> and 2759 <code>numchildren</code> will contain the number of elements that are allowed 2760 to be mixed in and <code>children</code> points to an array of 2761 <code>XML_Content</code> structures that will all have type XML_CTYPE_NAME with 2762 no quantification. Only the root node can be type <code>XML_CTYPE_EMPTY</code>, 2763 <code>XML_CTYPE_ANY</code>, or <code>XML_CTYPE_MIXED</code>. 2764 </p> 2765 2766 <p> 2767 For type <code>XML_CTYPE_NAME</code>, the <code>name</code> field points to the 2768 name and the <code>numchildren</code> and <code>children</code> fields will be 2769 zero and <code>NULL</code>. The <code>quant</code> field will indicate any 2770 quantifiers placed on the name. 2771 </p> 2772 2773 <p> 2774 Types <code>XML_CTYPE_CHOICE</code> and <code>XML_CTYPE_SEQ</code> indicate a 2775 choice or sequence respectively. The <code>numchildren</code> field indicates 2776 how many nodes in the choice or sequence and <code>children</code> points to 2777 the nodes. 2778 </p> 2779 </div> 2780 2781 <div class="handler"> 2782 <h4 id="XML_SetAttlistDeclHandler"> 2783 XML_SetAttlistDeclHandler 2784 </h4> 2785 2786 <pre class="setter"> 2787void XMLCALL 2788XML_SetAttlistDeclHandler(XML_Parser p, 2789 XML_AttlistDeclHandler attdecl); 2790</pre> 2791 2792 <pre class="signature"> 2793typedef void 2794(XMLCALL *XML_AttlistDeclHandler)(void *userData, 2795 const XML_Char *elname, 2796 const XML_Char *attname, 2797 const XML_Char *att_type, 2798 const XML_Char *dflt, 2799 int isrequired); 2800</pre> 2801 <p> 2802 Set a handler for attlist declarations in the DTD. This handler is called for 2803 <em>each</em> attribute. So a single attlist declaration with multiple 2804 attributes declared will generate multiple calls to this handler. The 2805 <code>elname</code> parameter returns the name of the element for which the 2806 attribute is being declared. The attribute name is in the <code>attname</code> 2807 parameter. The attribute type is in the <code>att_type</code> parameter. It is 2808 the string representing the type in the declaration with whitespace removed. 2809 </p> 2810 2811 <p> 2812 The <code>dflt</code> parameter holds the default value. It will be 2813 <code>NULL</code> in the case of "#IMPLIED" or "#REQUIRED" attributes. You can 2814 distinguish these two cases by checking the <code>isrequired</code> parameter, 2815 which will be true in the case of "#REQUIRED" attributes. Attributes which are 2816 "#FIXED" will have also have a true <code>isrequired</code>, but they will have 2817 the non-<code>NULL</code> fixed value in the <code>dflt</code> parameter. 2818 </p> 2819 </div> 2820 2821 <div class="handler"> 2822 <h4 id="XML_SetEntityDeclHandler"> 2823 XML_SetEntityDeclHandler 2824 </h4> 2825 2826 <pre class="setter"> 2827void XMLCALL 2828XML_SetEntityDeclHandler(XML_Parser p, 2829 XML_EntityDeclHandler handler); 2830</pre> 2831 2832 <pre class="signature"> 2833typedef void 2834(XMLCALL *XML_EntityDeclHandler)(void *userData, 2835 const XML_Char *entityName, 2836 int is_parameter_entity, 2837 const XML_Char *value, 2838 int value_length, 2839 const XML_Char *base, 2840 const XML_Char *systemId, 2841 const XML_Char *publicId, 2842 const XML_Char *notationName); 2843</pre> 2844 <p> 2845 Sets a handler that will be called for all entity declarations. The 2846 <code>is_parameter_entity</code> argument will be non-zero in the case of 2847 parameter entities and zero otherwise. 2848 </p> 2849 2850 <p> 2851 For internal entities (<code><!ENTITY foo "bar"></code>), 2852 <code>value</code> will be non-<code>NULL</code> and <code>systemId</code>, 2853 <code>publicId</code>, and <code>notationName</code> will all be 2854 <code>NULL</code>. The value string is <em>not</em> null-terminated; the length 2855 is provided in the <code>value_length</code> parameter. Do not use 2856 <code>value_length</code> to test for internal entities, since it is legal to 2857 have zero-length values. Instead check for whether or not <code>value</code> is 2858 <code>NULL</code>. 2859 </p> 2860 2861 <p> 2862 The <code>notationName</code> argument will have a non-<code>NULL</code> value 2863 only for unparsed entity declarations. 2864 </p> 2865 </div> 2866 2867 <div class="handler"> 2868 <h4 id="XML_SetUnparsedEntityDeclHandler"> 2869 XML_SetUnparsedEntityDeclHandler 2870 </h4> 2871 2872 <pre class="setter"> 2873void XMLCALL 2874XML_SetUnparsedEntityDeclHandler(XML_Parser p, 2875 XML_UnparsedEntityDeclHandler h) 2876</pre> 2877 2878 <pre class="signature"> 2879typedef void 2880(XMLCALL *XML_UnparsedEntityDeclHandler)(void *userData, 2881 const XML_Char *entityName, 2882 const XML_Char *base, 2883 const XML_Char *systemId, 2884 const XML_Char *publicId, 2885 const XML_Char *notationName); 2886</pre> 2887 <p> 2888 Set a handler that receives declarations of unparsed entities. These are entity 2889 declarations that have a notation (NDATA) field: 2890 </p> 2891 2892 <div id="eg"> 2893 <pre> 2894<!ENTITY logo SYSTEM "images/logo.gif" NDATA gif> 2895</pre> 2896 </div> 2897 2898 <p> 2899 This handler is obsolete and is provided for backwards compatibility. Use 2900 instead <a href="#XML_SetEntityDeclHandler">XML_SetEntityDeclHandler</a>. 2901 </p> 2902 </div> 2903 2904 <div class="handler"> 2905 <h4 id="XML_SetNotationDeclHandler"> 2906 XML_SetNotationDeclHandler 2907 </h4> 2908 2909 <pre class="setter"> 2910void XMLCALL 2911XML_SetNotationDeclHandler(XML_Parser p, 2912 XML_NotationDeclHandler h) 2913</pre> 2914 2915 <pre class="signature"> 2916typedef void 2917(XMLCALL *XML_NotationDeclHandler)(void *userData, 2918 const XML_Char *notationName, 2919 const XML_Char *base, 2920 const XML_Char *systemId, 2921 const XML_Char *publicId); 2922</pre> 2923 <p> 2924 Set a handler that receives notation declarations. 2925 </p> 2926 </div> 2927 2928 <div class="handler"> 2929 <h4 id="XML_SetNotStandaloneHandler"> 2930 XML_SetNotStandaloneHandler 2931 </h4> 2932 2933 <pre class="setter"> 2934void XMLCALL 2935XML_SetNotStandaloneHandler(XML_Parser p, 2936 XML_NotStandaloneHandler h) 2937</pre> 2938 2939 <pre class="signature"> 2940typedef int 2941(XMLCALL *XML_NotStandaloneHandler)(void *userData); 2942</pre> 2943 <p> 2944 Set a handler that is called if the document is not "standalone". This happens 2945 when there is an external subset or a reference to a parameter entity, but does 2946 not have standalone set to "yes" in an XML declaration. If this handler returns 2947 <code>XML_STATUS_ERROR</code>, then the parser will throw an 2948 <code>XML_ERROR_NOT_STANDALONE</code> error. 2949 </p> 2950 </div> 2951 2952 <h3> 2953 <a id="position" name="position">Parse position and error reporting functions</a> 2954 </h3> 2955 2956 <p> 2957 These are the functions you'll want to call when the parse functions return 2958 <code>XML_STATUS_ERROR</code> (a parse error has occurred), although the position 2959 reporting functions are useful outside of errors. The position reported is the 2960 byte position (in the original document or entity encoding) of the first of the 2961 sequence of characters that generated the current event (or the error that caused 2962 the parse functions to return <code>XML_STATUS_ERROR</code>.) The exceptions are 2963 callbacks triggered by declarations in the document prologue, in which case they 2964 exact position reported is somewhere in the relevant markup, but not necessarily 2965 as meaningful as for other events. 2966 </p> 2967 2968 <p> 2969 The position reporting functions are accurate only outside of the DTD. In other 2970 words, they usually return bogus information when called from within a DTD 2971 declaration handler. 2972 </p> 2973 2974 <h4 id="XML_GetErrorCode"> 2975 XML_GetErrorCode 2976 </h4> 2977 2978 <pre class="fcndec"> 2979enum XML_Error XMLCALL 2980XML_GetErrorCode(XML_Parser p); 2981</pre> 2982 <div class="fcndef"> 2983 <p> 2984 Return what type of error has occurred inside the main Expat parsing machinery. 2985 </p> 2986 2987 <p> 2988 Only the following functions record the type of error for retrivial via 2989 <code>XML_GetErrorCode</code>: 2990 </p> 2991 2992 <ul> 2993 <li> 2994 <code><a href="#XML_GetBuffer">XML_GetBuffer</a></code> 2995 </li> 2996 2997 <li> 2998 <code><a href="#XML_Parse">XML_Parse</a></code> 2999 </li> 3000 3001 <li> 3002 <code><a href="#XML_ParseBuffer">XML_ParseBuffer</a></code> 3003 </li> 3004 3005 <li> 3006 <code><a href="#XML_ParserReset">XML_ParserReset</a></code> 3007 </li> 3008 3009 <li> 3010 <code><a href="#XML_ResumeParser">XML_ResumeParser</a></code> 3011 </li> 3012 3013 <li> 3014 <code><a href="#XML_StopParser">XML_StopParser</a></code> 3015 </li> 3016 </ul> 3017 </div> 3018 3019 <h4 id="XML_ErrorString"> 3020 XML_ErrorString 3021 </h4> 3022 3023 <pre class="fcndec"> 3024const XML_LChar * XMLCALL 3025XML_ErrorString(enum XML_Error code); 3026</pre> 3027 <div class="fcndef"> 3028 Return a string describing the error corresponding to code. The code should be 3029 one of the enums that can be returned from <code><a href= 3030 "#XML_GetErrorCode">XML_GetErrorCode</a></code>. 3031 </div> 3032 3033 <h4 id="XML_GetCurrentByteIndex"> 3034 XML_GetCurrentByteIndex (deprecated) 3035 </h4> 3036 3037 <pre class="fcndec"> 3038XML_Index XMLCALL 3039XML_GetCurrentByteIndex(XML_Parser p); 3040</pre> 3041 <p> 3042 <b>Note:</b> This function is deprecated, please use function <code><a href= 3043 "#XML_GetCurrentByteIndex64">XML_GetCurrentByteIndex64</a></code>, instead. 3044 </p> 3045 3046 <div class="fcndef"> 3047 Return the byte offset of the position. This always corresponds to the values 3048 returned by <code><a href= 3049 "#XML_GetCurrentLineNumber">XML_GetCurrentLineNumber</a></code> and 3050 <code><a href="#XML_GetCurrentColumnNumber">XML_GetCurrentColumnNumber</a></code>. 3051 </div> 3052 3053 <p> 3054 <strong>Note:</strong> Type <code>XML_Index</code> is known to wrap around on 3055 32bit platforms and 64bit Windows unless Expat has been compiled with 3056 (non-default and uncommon) macro <code><a href= 3057 "#XML_LARGE_SIZE">XML_LARGE_SIZE</a></code>. 3058 </p> 3059 3060 <h4 id="XML_GetCurrentByteIndex64"> 3061 XML_GetCurrentByteIndex64 3062 </h4> 3063 3064 <pre class="fcndec"> 3065/* Added in Expat 2.9.0. */ 3066int64_t XMLCALL 3067XML_GetCurrentByteIndex64(XML_Parser p); 3068</pre> 3069 <div class="fcndef"> 3070 Return the byte offset of the position. This always corresponds to the values 3071 returned by <code><a href= 3072 "#XML_GetCurrentLineNumber64">XML_GetCurrentLineNumber64</a></code> and 3073 <code><a href= 3074 "#XML_GetCurrentColumnNumber64">XML_GetCurrentColumnNumber64</a></code>. 3075 </div> 3076 3077 <h4 id="XML_GetCurrentLineNumber"> 3078 XML_GetCurrentLineNumber (deprecated) 3079 </h4> 3080 3081 <pre class="fcndec"> 3082XML_Size XMLCALL 3083XML_GetCurrentLineNumber(XML_Parser p); 3084</pre> 3085 <p> 3086 <b>Note:</b> This function is deprecated, please use function <code><a href= 3087 "#XML_GetCurrentLineNumber64">XML_GetCurrentLineNumber64</a></code>, instead. 3088 </p> 3089 3090 <div class="fcndef"> 3091 Return the line number of the position. The first line is reported as 3092 <code>1</code>. 3093 </div> 3094 3095 <p> 3096 <strong>Note:</strong> Type <code>XML_Size</code> is known to wrap around on 3097 32bit platforms and 64bit Windows unless Expat has been compiled with 3098 (non-default and uncommon) macro <code><a href= 3099 "#XML_LARGE_SIZE">XML_LARGE_SIZE</a></code>. 3100 </p> 3101 3102 <h4 id="XML_GetCurrentLineNumber64"> 3103 XML_GetCurrentLineNumber64 3104 </h4> 3105 3106 <pre class="fcndec"> 3107/* Added in Expat 2.9.0. */ 3108uint64_t XMLCALL 3109XML_GetCurrentLineNumber64(XML_Parser p); 3110</pre> 3111 <div class="fcndef"> 3112 Return the line number of the position. The first line is reported as 3113 <code>1</code>. 3114 </div> 3115 3116 <h4 id="XML_GetCurrentColumnNumber"> 3117 XML_GetCurrentColumnNumber (deprecated) 3118 </h4> 3119 3120 <pre class="fcndec"> 3121XML_Size XMLCALL 3122XML_GetCurrentColumnNumber(XML_Parser p); 3123</pre> 3124 <p> 3125 <b>Note:</b> This function is deprecated, please use function <code><a href= 3126 "#XML_GetCurrentColumnNumber64">XML_GetCurrentColumnNumber64</a></code>, instead. 3127 </p> 3128 3129 <div class="fcndef"> 3130 Return the <em>offset</em>, from the beginning of the current line, of the 3131 position. The first column is reported as <code>0</code>. 3132 </div> 3133 3134 <p> 3135 <strong>Note:</strong> Type <code>XML_Size</code> is known to wrap around on 3136 32bit platforms and 64bit Windows unless Expat has been compiled with 3137 (non-default and uncommon) macro <code><a href= 3138 "#XML_LARGE_SIZE">XML_LARGE_SIZE</a></code>. 3139 </p> 3140 3141 <h4 id="XML_GetCurrentColumnNumber64"> 3142 XML_GetCurrentColumnNumber64 3143 </h4> 3144 3145 <pre class="fcndec"> 3146/* Added in Expat 2.9.0. */ 3147uint64_t XMLCALL 3148XML_GetCurrentColumnNumber64(XML_Parser p); 3149</pre> 3150 <div class="fcndef"> 3151 Return the <em>offset</em>, from the beginning of the current line, of the 3152 position. The first column is reported as <code>0</code>. 3153 </div> 3154 3155 <h4 id="XML_GetCurrentByteCount"> 3156 XML_GetCurrentByteCount (deprecated) 3157 </h4> 3158 3159 <pre class="fcndec"> 3160int XMLCALL 3161XML_GetCurrentByteCount(XML_Parser p); 3162</pre> 3163 <p> 3164 <b>Note:</b> This function is deprecated, please use function <code><a href= 3165 "#XML_GetCurrentByteCount64">XML_GetCurrentByteCount64</a></code>, instead. 3166 </p> 3167 3168 <div class="fcndef"> 3169 Return the number of bytes in the current event. Returns <code>0</code> if the 3170 event is inside a reference to an internal entity and for the end-tag event for 3171 empty element tags (the later can be used to distinguish empty-element tags from 3172 empty elements using separate start and end tags). 3173 </div> 3174 3175 <p> 3176 <strong>Note:</strong> Type <code>int</code> is known to wrap around (on both 3177 32bit platforms and 64bit platforms). 3178 </p> 3179 3180 <h4 id="XML_GetCurrentByteCount64"> 3181 XML_GetCurrentByteCount64 3182 </h4> 3183 3184 <pre class="fcndec"> 3185/* Added in Expat 2.9.0. */ 3186uint64_t XMLCALL 3187XML_GetCurrentByteCount64(XML_Parser p); 3188</pre> 3189 <div class="fcndef"> 3190 Return the number of bytes in the current event. Returns <code>0</code> if the 3191 event is inside a reference to an internal entity and for the end-tag event for 3192 empty element tags (the later can be used to distinguish empty-element tags from 3193 empty elements using separate start and end tags). 3194 </div> 3195 3196 <h4 id="XML_GetInputContext"> 3197 XML_GetInputContext (deprecated) 3198 </h4> 3199 3200 <pre class="fcndec"> 3201const char * XMLCALL 3202XML_GetInputContext(XML_Parser p, 3203 int *offset, 3204 int *size); 3205</pre> 3206 <p> 3207 <b>Note:</b> This function is deprecated, please use function <code><a href= 3208 "#XML_GetInputContext64">XML_GetInputContext64</a></code>, instead. 3209 </p> 3210 3211 <div class="fcndef"> 3212 <p> 3213 Returns the parser's input buffer, sets the integer pointed at by 3214 <code>offset</code> to the offset within this buffer of the current parse 3215 position, and set the integer pointed at by <code>size</code> to the size of 3216 the returned buffer. 3217 </p> 3218 3219 <p> 3220 This should only be called from within a handler during an active parse and the 3221 returned buffer should only be referred to from within the handler that made 3222 the call. This input buffer contains the untranslated bytes of the input. 3223 </p> 3224 3225 <p> 3226 Only a limited amount of context is kept, so if the event triggering a call 3227 spans over a very large amount of input, the actual parse position may be 3228 before the beginning of the buffer. 3229 </p> 3230 3231 <p> 3232 If <code>XML_CONTEXT_BYTES</code> is zero, this will always return 3233 <code>NULL</code>. 3234 </p> 3235 3236 <p> 3237 <strong>Note:</strong> Type <code>int</code> is known to wrap around (on both 3238 32bit platforms and 64bit platforms). 3239 </p> 3240 </div> 3241 3242 <h4 id="XML_GetInputContext64"> 3243 XML_GetInputContext64 3244 </h4> 3245 3246 <pre class="fcndec"> 3247/* Added in Expat 2.9.0. */ 3248const char * XMLCALL 3249XML_GetInputContext64(XML_Parser p, 3250 int64_t *offset, 3251 zint64_t *size); 3252</pre> 3253 <div class="fcndef"> 3254 <p> 3255 Returns the parser's input buffer, sets the integer pointed at by 3256 <code>offset</code> to the offset within this buffer of the current parse 3257 position, and set the integer pointed at by <code>size</code> to the size of 3258 the returned buffer. 3259 </p> 3260 3261 <p> 3262 This should only be called from within a handler during an active parse and the 3263 returned buffer should only be referred to from within the handler that made 3264 the call. This input buffer contains the untranslated bytes of the input. 3265 </p> 3266 3267 <p> 3268 Only a limited amount of context is kept, so if the event triggering a call 3269 spans over a very large amount of input, the actual parse position may be 3270 before the beginning of the buffer. 3271 </p> 3272 3273 <p> 3274 If <code>XML_CONTEXT_BYTES</code> is zero, this will always return 3275 <code>NULL</code>. 3276 </p> 3277 </div> 3278 3279 <h3> 3280 <a id="properties" name="properties">Properties / Security Tuning</a> 3281 </h3> 3282 3283 <p> 3284 Expat 2.9.0 introduced a new Properties API. Think of properties as the control 3285 knobs of a parser. The key motivations behind its introduction are: 3286 </p> 3287 3288 <ul> 3289 <li>to allow Expat to add more properties — both security-related and non- 3290 security ones — in the future (without need to also add new related getter and 3291 setter functions for each of them as was the case in the past), 3292 </li> 3293 3294 <li>to allow users to read (not just to set) the current values, and 3295 </li> 3296 3297 <li>to allow users to distinuish the different sources of error beyond a mere 3298 "did not work". 3299 </li> 3300 </ul> 3301 3302 <p> 3303 There is one setter function and one getter function — a pair — per supported 3304 data type: 3305 </p> 3306 3307 <ul> 3308 <li>For <code>XML_Bool</code>: 3309 <ul> 3310 <li> 3311 <code><a href="#XML_GetPropertyBool">XML_GetPropertyBool</a></code> 3312 </li> 3313 3314 <li> 3315 <code><a href="#XML_SetPropertyBool">XML_SetPropertyBool</a></code> 3316 </li> 3317 </ul> 3318 </li> 3319 3320 <li>For <code>double</code>: 3321 <ul> 3322 <li> 3323 <code><a href="#XML_GetPropertyDouble">XML_GetPropertyDouble</a></code> 3324 </li> 3325 3326 <li> 3327 <code><a href="#XML_SetPropertyDouble">XML_SetPropertyDouble</a></code> 3328 </li> 3329 </ul> 3330 </li> 3331 3332 <li>For <code>uint64_t</code>: 3333 <ul> 3334 <li> 3335 <code><a href="#XML_GetPropertyUInt64">XML_GetPropertyUInt64</a></code> 3336 </li> 3337 3338 <li> 3339 <code><a href="#XML_SetPropertyUInt64">XML_SetPropertyUInt64</a></code> 3340 </li> 3341 </ul> 3342 </li> 3343 </ul> 3344 3345 <p> 3346 They all share these two key enums: 3347 </p> 3348 3349 <h4 id="XML_Parser_Property"> 3350 XML_Parser_Property 3351 </h4> 3352 3353 <pre> 3354/* Added in Expat 2.9.0. */ 3355enum XML_Parser_Property { 3356# if XML_GE == 1 3357 XML_PROP_ALLOC_TRACKER_ACTIVATION_THRESHOLD = 1, // of type `uint64_t` 3358 XML_PROP_ALLOC_TRACKER_MAXIMUM_AMPLIFICATION = 2, // of type `double` 3359 XML_PROP_BILLION_LAUGHS_ACTIVATION_THRESHOLD = 3, // of type `uint64_t` 3360 XML_PROP_BILLION_LAUGHS_MAXIMUM_AMPLIFICATION = 4, // of type `double` 3361# endif 3362 XML_PROP_REPARSE_DEFERRAL_ENABLED = 5, // of type `XML_Bool` 3363 /* more properties upcoming here */ 3364};</pre> 3365 <h4 id="XML_Prop_Error"> 3366 XML_Prop_Error 3367 </h4> 3368 3369 <pre> 3370/* Added in 2.9.0. */ 3371enum XML_Prop_Error { 3372 XML_PROP_ERROR_NONE = 0, // i.e. success 3373 3374 XML_PROP_ERROR_INVALID_KEY = 1, // i.e. the property is not known at all 3375 XML_PROP_ERROR_INVALID_TYPE = 2, // i.e. the property is known but expects a 3376 // different type (so a different 3377 // getter/setter would need to be called) 3378 XML_PROP_ERROR_INVALID_VALUE = 3, // i.e. the value is invalid (with a setter 3379 // function) or a NULL-pointer (with a 3380 // getter function) 3381 3382 XML_PROP_ERROR_PARSER_NULL = 4, // i.e. the parser argument is NULL 3383 XML_PROP_ERROR_PARSER_NOT_ROOT = 5, // i.e. the parser is not a root parser 3384 // but a subparser (and a root parser is 3385 // needed) 3386 3387 /* potentially more error codes upcoming here */ 3388};</pre> 3389 <p> 3390 Let us look at these functions in more detail: 3391 </p> 3392 3393 <h4 id="XML_GetPropertyBool"> 3394 XML_GetPropertyBool 3395 </h4> 3396 3397 <pre class="fcndec"> 3398/* Added in Expat 2.9.0. */ 3399enum XML_Prop_Error 3400XML_GetPropertyBool(XML_Parser parser, enum XML_Parser_Property property, 3401 XML_Bool *value); 3402</pre> 3403 <div class="fcndef"> 3404 <p> 3405 Writes the current value of boolean property <code>property</code> of parser 3406 <code>parser</code> to output variable <code>value</code>. Supported properties 3407 are: 3408 </p> 3409 3410 <ul> 3411 <li> 3412 <code>XML_PROP_REPARSE_DEFERRAL_ENABLED</code> — whether <a href= 3413 "#XML_SetReparseDeferralEnabled">reparse deferral</a> is active or not 3414 </li> 3415 </ul> 3416 3417 <p> 3418 Both parameters <code>parser</code> and <code>value</code> must be 3419 non-<code>NULL</code>. On success <code>XML_PROP_ERROR_NONE</code> is returned; 3420 on error one of the other variants of <a href= 3421 "#XML_Prop_Error">XML_Prop_Error</a> is returned. 3422 </p> 3423 3424 <p> 3425 <strong>Note:</strong> There may be more error codes introduced into <a href= 3426 "#XML_Prop_Error">XML_Prop_Error</a> in the future, so user code must be robust 3427 towards error codes from the future at runtime that were not known at compile 3428 time. 3429 </p> 3430 </div> 3431 3432 <h4 id="XML_GetPropertyDouble"> 3433 XML_GetPropertyDouble 3434 </h4> 3435 3436 <pre class="fcndec"> 3437/* Added in Expat 2.9.0. */ 3438enum XML_Prop_Error 3439XML_GetPropertyDouble(XML_Parser parser, enum XML_Parser_Property property, 3440 double *value); 3441</pre> 3442 <div class="fcndef"> 3443 <p> 3444 Writes the current value of floating point property <code>property</code> of 3445 parser <code>parser</code> to output variable <code>value</code>. Supported 3446 properties are: 3447 </p> 3448 3449 <ul> 3450 <li> 3451 <code>XML_PROP_ALLOC_TRACKER_MAXIMUM_AMPLIFICATION</code> — the maximum 3452 tolerated amplification with the <a href= 3453 "#XML_SetAllocTrackerMaximumAmplification">allocation tracker</a> 3454 </li> 3455 3456 <li> 3457 <code>XML_PROP_BILLION_LAUGHS_MAXIMUM_AMPLIFICATION</code> — the maximum 3458 tolerated amplification with <a href= 3459 "#XML_SetBillionLaughsAttackProtectionMaximumAmplification">billion laughs 3460 attack protection</a> 3461 </li> 3462 </ul> 3463 3464 <p> 3465 Both parameters <code>parser</code> and <code>value</code> must be 3466 non-<code>NULL</code>. On success <code>XML_PROP_ERROR_NONE</code> is returned; 3467 on error one of the other variants of <a href= 3468 "#XML_Prop_Error">XML_Prop_Error</a> is returned. 3469 </p> 3470 3471 <p> 3472 <strong>Note:</strong> There may be more error codes introduced into <a href= 3473 "#XML_Prop_Error">XML_Prop_Error</a> in the future, so user code must be robust 3474 towards error codes from the future at runtime that were not known at compile 3475 time. 3476 </p> 3477 </div> 3478 3479 <h4 id="XML_GetPropertyUInt64"> 3480 XML_GetPropertyUInt64 3481 </h4> 3482 3483 <pre class="fcndec"> 3484/* Added in Expat 2.9.0. */ 3485enum XML_Prop_Error 3486XML_GetPropertyUInt64(XML_Parser parser, enum XML_Parser_Property property, 3487 uint64_t *value); 3488</pre> 3489 <div class="fcndef"> 3490 <p> 3491 Writes the current value of integral property <code>property</code> of parser 3492 <code>parser</code> to output variable <code>value</code>. Supported properties 3493 are: 3494 </p> 3495 3496 <ul> 3497 <li> 3498 <code>XML_PROP_ALLOC_TRACKER_ACTIVATION_THRESHOLD</code> — the activation 3499 threshold in bytes with the <a href= 3500 "#XML_SetAllocTrackerActivationThreshold">allocation tracker</a> 3501 </li> 3502 3503 <li> 3504 <code>XML_PROP_BILLION_LAUGHS_ACTIVATION_THRESHOLD</code> — the activation 3505 threshold in bytes with <a href= 3506 "#XML_SetBillionLaughsAttackProtectionActivationThreshold">billion laughs 3507 attack protection</a> 3508 </li> 3509 </ul> 3510 3511 <p> 3512 Both parameters <code>parser</code> and <code>value</code> must be 3513 non-<code>NULL</code>. On success <code>XML_PROP_ERROR_NONE</code> is returned; 3514 on error one of the other variants of <a href= 3515 "#XML_Prop_Error">XML_Prop_Error</a> is returned. 3516 </p> 3517 3518 <p> 3519 <strong>Note:</strong> There may be more error codes introduced into <a href= 3520 "#XML_Prop_Error">XML_Prop_Error</a> in the future, so user code must be robust 3521 towards error codes from the future at runtime that were not known at compile 3522 time. 3523 </p> 3524 </div> 3525 3526 <h3> 3527 <a id="attack-protection" name="attack-protection">Attack Protection</a><a id= 3528 "billion-laughs" name="billion-laughs"></a> 3529 </h3> 3530 3531 <h4 id="XML_SetPropertyBool"> 3532 XML_SetPropertyBool 3533 </h4> 3534 3535 <pre class="fcndec"> 3536/* Added in Expat 2.9.0. */ 3537enum XML_Prop_Error 3538XML_SetPropertyBool(XML_Parser parser, enum XML_Parser_Property property, 3539 XML_Bool value); 3540</pre> 3541 <div class="fcndef"> 3542 <p> 3543 Applies the given value <code>value</code> to boolean property 3544 <code>property</code> of parser <code>parser</code>. Supported properties are: 3545 </p> 3546 3547 <ul> 3548 <li> 3549 <code>XML_PROP_REPARSE_DEFERRAL_ENABLED</code> — whether <a href= 3550 "#XML_SetReparseDeferralEnabled">reparse deferral</a> is active or not 3551 </li> 3552 </ul> 3553 3554 <p> 3555 Parameter <code>parser</code> must be non-<code>NULL</code>. On success 3556 <code>XML_PROP_ERROR_NONE</code> is returned; on error one of the other 3557 variants of <a href="#XML_Prop_Error">XML_Prop_Error</a> is returned. 3558 </p> 3559 3560 <p> 3561 <strong>Note:</strong> There may be more error codes introduced into <a href= 3562 "#XML_Prop_Error">XML_Prop_Error</a> in the future, so user code must be robust 3563 towards error codes from the future at runtime that were not known at compile 3564 time. 3565 </p> 3566 </div> 3567 3568 <h4 id="XML_SetPropertyDouble"> 3569 XML_SetPropertyDouble 3570 </h4> 3571 3572 <pre class="fcndec"> 3573/* Added in Expat 2.9.0. */ 3574enum XML_Prop_Error 3575XML_SetPropertyDouble(XML_Parser parser, enum XML_Parser_Property property, 3576 double value); 3577</pre> 3578 <div class="fcndef"> 3579 <p> 3580 Applies the given value <code>value</code> to floating point property 3581 <code>property</code> of parser <code>parser</code>. Supported properties are: 3582 </p> 3583 3584 <ul> 3585 <li> 3586 <code>XML_PROP_ALLOC_TRACKER_MAXIMUM_AMPLIFICATION</code> — the maximum 3587 tolerated amplification with the <a href= 3588 "#XML_SetAllocTrackerMaximumAmplification">allocation tracker</a> 3589 </li> 3590 3591 <li> 3592 <code>XML_PROP_BILLION_LAUGHS_MAXIMUM_AMPLIFICATION</code> — the maximum 3593 tolerated amplification with <a href= 3594 "#XML_SetBillionLaughsAttackProtectionMaximumAmplification">billion laughs 3595 attack protection</a> 3596 </li> 3597 </ul> 3598 3599 <p> 3600 Value <code>value</code> may or may not be cast to <code>float</code> 3601 internally. 3602 </p> 3603 3604 <p> 3605 Parameter <code>parser</code> must be non-<code>NULL</code>. On success 3606 <code>XML_PROP_ERROR_NONE</code> is returned; on error one of the other 3607 variants of <a href="#XML_Prop_Error">XML_Prop_Error</a> is returned. 3608 </p> 3609 3610 <p> 3611 <strong>Note:</strong> There may be more error codes introduced into <a href= 3612 "#XML_Prop_Error">XML_Prop_Error</a> in the future, so user code must be robust 3613 towards error codes from the future at runtime that were not known at compile 3614 time. 3615 </p> 3616 </div> 3617 3618 <h4 id="XML_SetPropertyUInt64"> 3619 XML_SetPropertyUInt64 3620 </h4> 3621 3622 <pre class="fcndec"> 3623/* Added in Expat 2.9.0. */ 3624enum XML_Prop_Error 3625XML_SetPropertyUInt64(XML_Parser parser, enum XML_Parser_Property property, 3626 uint64_t value); 3627</pre> 3628 <div class="fcndef"> 3629 <p> 3630 Applies the given value <code>value</code> to integral property 3631 <code>property</code> of parser <code>parser</code>. Supported properties are: 3632 </p> 3633 3634 <ul> 3635 <li> 3636 <code>XML_PROP_ALLOC_TRACKER_ACTIVATION_THRESHOLD</code> — the activation 3637 threshold in bytes with the <a href= 3638 "#XML_SetAllocTrackerActivationThreshold">allocation tracker</a> 3639 </li> 3640 3641 <li> 3642 <code>XML_PROP_BILLION_LAUGHS_ACTIVATION_THRESHOLD</code> — the activation 3643 threshold in bytes with <a href= 3644 "#XML_SetBillionLaughsAttackProtectionActivationThreshold">billion laughs 3645 attack protection</a> 3646 </li> 3647 </ul> 3648 3649 <p> 3650 Parameter <code>parser</code> must be non-<code>NULL</code>. On success 3651 <code>XML_PROP_ERROR_NONE</code> is returned; on error one of the other 3652 variants of <a href="#XML_Prop_Error">XML_Prop_Error</a> is returned. 3653 </p> 3654 3655 <p> 3656 <strong>Note:</strong> There may be more error codes introduced into <a href= 3657 "#XML_Prop_Error">XML_Prop_Error</a> in the future, so user code must be robust 3658 towards error codes from the future at runtime that were not known at compile 3659 time. 3660 </p> 3661 </div> 3662 3663 <h4 id="XML_SetBillionLaughsAttackProtectionMaximumAmplification"> 3664 XML_SetBillionLaughsAttackProtectionMaximumAmplification 3665 </h4> 3666 3667 <pre class="fcndec"> 3668/* Added in Expat 2.4.0. */ 3669XML_Bool XMLCALL 3670XML_SetBillionLaughsAttackProtectionMaximumAmplification(XML_Parser p, 3671 float maximumAmplificationFactor); 3672</pre> 3673 <div class="fcndef"> 3674 <p> 3675 Sets the maximum tolerated amplification factor for protection against <a href= 3676 "https://en.wikipedia.org/wiki/Billion_laughs_attack">billion laughs 3677 attacks</a> (default: <code>100.0</code>) of parser <code>p</code> to 3678 <code>maximumAmplificationFactor</code>, and returns <code>XML_TRUE</code> upon 3679 success and <code>XML_FALSE</code> upon error. 3680 </p> 3681 3682 <p> 3683 Once the <a href= 3684 "#XML_SetBillionLaughsAttackProtectionActivationThreshold">threshold for 3685 activation</a> is reached, the amplification factor is calculated as .. 3686 </p> 3687 3688 <pre>amplification := (direct + indirect) / direct</pre> 3689 <p> 3690 .. while parsing, whereas <code>direct</code> is the number of bytes read from 3691 the primary document in parsing and <code>indirect</code> is the number of 3692 bytes added by expanding entities and reading of external DTD files, combined. 3693 </p> 3694 3695 <p> 3696 For a call to 3697 <code>XML_SetBillionLaughsAttackProtectionMaximumAmplification</code> to 3698 succeed: 3699 </p> 3700 3701 <ul> 3702 <li>parser <code>p</code> must be a non-<code>NULL</code> root parser (without 3703 any parent parsers) and 3704 </li> 3705 3706 <li> 3707 <code>maximumAmplificationFactor</code> must be non-<code>NaN</code> and 3708 greater than or equal to <code>1.0</code>. 3709 </li> 3710 </ul> 3711 3712 <p> 3713 <strong>Note:</strong> If you ever need to increase this value for non-attack 3714 payload, please <a href="https://github.com/libexpat/libexpat/issues">file a 3715 bug report</a>. 3716 </p> 3717 3718 <p> 3719 <strong>Note:</strong> Peak amplifications of factor 15,000 for the entire 3720 payload and of factor 30,000 in the middle of parsing have been observed with 3721 small benign files in practice. So if you do reduce the maximum allowed 3722 amplification, please make sure that the activation threshold is still big 3723 enough to not end up with undesired false positives (i.e. benign files being 3724 rejected). 3725 </p> 3726 </div> 3727 3728 <h4 id="XML_SetBillionLaughsAttackProtectionActivationThreshold"> 3729 XML_SetBillionLaughsAttackProtectionActivationThreshold 3730 </h4> 3731 3732 <pre class="fcndec"> 3733/* Added in Expat 2.4.0. */ 3734XML_Bool XMLCALL 3735XML_SetBillionLaughsAttackProtectionActivationThreshold(XML_Parser p, 3736 unsigned long long activationThresholdBytes); 3737</pre> 3738 <div class="fcndef"> 3739 <p> 3740 Sets number of output bytes (including amplification from entity expansion and 3741 reading DTD files) needed to activate protection against <a href= 3742 "https://en.wikipedia.org/wiki/Billion_laughs_attack">billion laughs 3743 attacks</a> (default: <code>8 MiB</code>) of parser <code>p</code> to 3744 <code>activationThresholdBytes</code>, and returns <code>XML_TRUE</code> upon 3745 success and <code>XML_FALSE</code> upon error. 3746 </p> 3747 3748 <p> 3749 For a call to 3750 <code>XML_SetBillionLaughsAttackProtectionActivationThreshold</code> to 3751 succeed: 3752 </p> 3753 3754 <ul> 3755 <li>parser <code>p</code> must be a non-<code>NULL</code> root parser (without 3756 any parent parsers). 3757 </li> 3758 </ul> 3759 3760 <p> 3761 <strong>Note:</strong> If you ever need to increase this value for non-attack 3762 payload, please <a href="https://github.com/libexpat/libexpat/issues">file a 3763 bug report</a>. 3764 </p> 3765 3766 <p> 3767 <strong>Note:</strong> Activation thresholds below 4 MiB are known to break 3768 support for <a href= 3769 "https://en.wikipedia.org/wiki/Darwin_Information_Typing_Architecture">DITA</a> 3770 1.3 payload and are hence not recommended. 3771 </p> 3772 </div> 3773 3774 <h4 id="XML_SetAllocTrackerMaximumAmplification"> 3775 XML_SetAllocTrackerMaximumAmplification 3776 </h4> 3777 3778 <pre class="fcndec"> 3779/* Added in Expat 2.7.2. */ 3780XML_Bool 3781XML_SetAllocTrackerMaximumAmplification(XML_Parser p, 3782 float maximumAmplificationFactor); 3783</pre> 3784 <div class="fcndef"> 3785 <p> 3786 Sets the maximum tolerated amplification factor between direct input and bytes 3787 of dynamic memory allocated (default: <code>100.0</code>) of parser 3788 <code>p</code> to <code>maximumAmplificationFactor</code>, and returns 3789 <code>XML_TRUE</code> upon success and <code>XML_FALSE</code> upon error. 3790 </p> 3791 3792 <p> 3793 <strong>Note:</strong> There are three types of allocations that intentionally 3794 bypass tracking and limiting: 3795 </p> 3796 3797 <ul> 3798 <li>application calls to functions <code><a href= 3799 "#XML_MemMalloc">XML_MemMalloc</a></code> and <code><a href="#XML_MemRealloc"> 3800 XML_MemRealloc</a></code> — <em>healthy</em> use of these two functions 3801 continues to be a responsibility of the application using Expat —, 3802 </li> 3803 3804 <li>the main character buffer used by functions <code><a href="#XML_GetBuffer"> 3805 XML_GetBuffer</a></code> and <code><a href= 3806 "#XML_ParseBuffer">XML_ParseBuffer</a></code> (and thus also by plain 3807 <code><a href="#XML_Parse">XML_Parse</a></code>), and 3808 </li> 3809 3810 <li>the <a href="#XML_SetElementDeclHandler">content model memory</a> (that is 3811 passed to the <a href="#XML_SetElementDeclHandler">element declaration 3812 handler</a> and freed by a call to <code><a href= 3813 "#XML_FreeContentModel">XML_FreeContentModel</a></code>). 3814 </li> 3815 </ul> 3816 3817 <p> 3818 Once the <a href="#XML_SetAllocTrackerActivationThreshold">threshold for 3819 activation</a> is reached, the amplification factor is calculated as .. 3820 </p> 3821 3822 <pre>amplification := allocated / direct</pre> 3823 <p> 3824 .. while parsing, whereas <code>direct</code> is the number of bytes read from 3825 the primary document in parsing and <code>allocated</code> is the number of 3826 bytes of dynamic memory allocated in the parser hierarchy. 3827 </p> 3828 3829 <p> 3830 For a call to <code>XML_SetAllocTrackerMaximumAmplification</code> to succeed: 3831 </p> 3832 3833 <ul> 3834 <li>parser <code>p</code> must be a non-<code>NULL</code> root parser (without 3835 any parent parsers) and 3836 </li> 3837 3838 <li> 3839 <code>maximumAmplificationFactor</code> must be non-<code>NaN</code> and 3840 greater than or equal to <code>1.0</code>. 3841 </li> 3842 </ul> 3843 3844 <p> 3845 <strong>Note:</strong> If you ever need to increase this value for non-attack 3846 payload, please <a href="https://github.com/libexpat/libexpat/issues">file a 3847 bug report</a>. 3848 </p> 3849 3850 <p> 3851 <strong>Note:</strong> Amplifications factors greater than <code>100.0</code> 3852 can been observed near the start of parsing even with benign files in practice. 3853 So if you do reduce the maximum allowed amplification, please make sure that 3854 the activation threshold is still big enough to not end up with undesired false 3855 positives (i.e. benign files being rejected). 3856 </p> 3857 </div> 3858 3859 <h4 id="XML_SetAllocTrackerActivationThreshold"> 3860 XML_SetAllocTrackerActivationThreshold 3861 </h4> 3862 3863 <pre class="fcndec"> 3864/* Added in Expat 2.7.2. */ 3865XML_Bool 3866XML_SetAllocTrackerActivationThreshold(XML_Parser p, 3867 unsigned long long activationThresholdBytes); 3868</pre> 3869 <div class="fcndef"> 3870 <p> 3871 Sets number of allocated bytes of dynamic memory needed to activate protection 3872 against disproportionate use of RAM (default: <code>64 MiB</code>) of parser 3873 <code>p</code> to <code>activationThresholdBytes</code>, and returns 3874 <code>XML_TRUE</code> upon success and <code>XML_FALSE</code> upon error. 3875 </p> 3876 3877 <p> 3878 <strong>Note:</strong> For types of allocations that intentionally bypass 3879 tracking and limiting, please see <code><a href= 3880 "#XML_SetAllocTrackerMaximumAmplification">XML_SetAllocTrackerMaximumAmplification</a></code> 3881 above. 3882 </p> 3883 3884 <p> 3885 For a call to <code>XML_SetAllocTrackerActivationThreshold</code> to succeed: 3886 </p> 3887 3888 <ul> 3889 <li>parser <code>p</code> must be a non-<code>NULL</code> root parser (without 3890 any parent parsers). 3891 </li> 3892 </ul> 3893 3894 <p> 3895 <strong>Note:</strong> If you ever need to increase this value for non-attack 3896 payload, please <a href="https://github.com/libexpat/libexpat/issues">file a 3897 bug report</a>. 3898 </p> 3899 </div> 3900 3901 <h4 id="XML_SetReparseDeferralEnabled"> 3902 XML_SetReparseDeferralEnabled 3903 </h4> 3904 3905 <pre class="fcndec"> 3906/* Added in Expat 2.6.0. */ 3907XML_Bool XMLCALL 3908XML_SetReparseDeferralEnabled(XML_Parser parser, XML_Bool enabled); 3909</pre> 3910 <div class="fcndef"> 3911 <p> 3912 Large tokens may require many parse calls before enough data is available for 3913 Expat to parse it in full. If Expat retried parsing the token on every parse 3914 call, parsing could take quadratic time. To avoid this, Expat only retries once 3915 a significant amount of new data is available. This function allows disabling 3916 this behavior. 3917 </p> 3918 3919 <p> 3920 The <code>enabled</code> argument should be <code>XML_TRUE</code> or 3921 <code>XML_FALSE</code>. 3922 </p> 3923 3924 <p> 3925 Returns <code>XML_TRUE</code> on success, and <code>XML_FALSE</code> on error. 3926 </p> 3927 </div> 3928 3929 <h3> 3930 <a id="miscellaneous" name="miscellaneous">Miscellaneous functions</a> 3931 </h3> 3932 3933 <p> 3934 The functions in this section either obtain state information from the parser or 3935 can be used to dynamically set parser options. 3936 </p> 3937 3938 <h4 id="XML_SetUserData"> 3939 XML_SetUserData 3940 </h4> 3941 3942 <pre class="fcndec"> 3943void XMLCALL 3944XML_SetUserData(XML_Parser p, 3945 void *userData); 3946</pre> 3947 <div class="fcndef"> 3948 This sets the user data pointer that gets passed to handlers. It overwrites any 3949 previous value for this pointer. Note that the application is responsible for 3950 freeing the memory associated with <code>userData</code> when it is finished with 3951 the parser. So if you call this when there's already a pointer there, and you 3952 haven't freed the memory associated with it, then you've probably just leaked 3953 memory. 3954 </div> 3955 3956 <h4 id="XML_GetUserData"> 3957 XML_GetUserData 3958 </h4> 3959 3960 <pre class="fcndec"> 3961void * XMLCALL 3962XML_GetUserData(XML_Parser p); 3963</pre> 3964 <div class="fcndef"> 3965 This returns the user data pointer that gets passed to handlers. It is actually 3966 implemented as a macro. 3967 </div> 3968 3969 <h4 id="XML_UseParserAsHandlerArg"> 3970 XML_UseParserAsHandlerArg 3971 </h4> 3972 3973 <pre class="fcndec"> 3974void XMLCALL 3975XML_UseParserAsHandlerArg(XML_Parser p); 3976</pre> 3977 <div class="fcndef"> 3978 After this is called, handlers receive the parser in their <code>userData</code> 3979 arguments. The user data can still be obtained using the <code><a href= 3980 "#XML_GetUserData">XML_GetUserData</a></code> function. 3981 </div> 3982 3983 <h4 id="XML_SetBase"> 3984 XML_SetBase 3985 </h4> 3986 3987 <pre class="fcndec"> 3988enum XML_Status XMLCALL 3989XML_SetBase(XML_Parser p, 3990 const XML_Char *base); 3991</pre> 3992 <div class="fcndef"> 3993 Set the base to be used for resolving relative URIs in system identifiers. The 3994 return value is <code>XML_STATUS_ERROR</code> if there's no memory to store base, 3995 otherwise it's <code>XML_STATUS_OK</code>. 3996 </div> 3997 3998 <h4 id="XML_GetBase"> 3999 XML_GetBase 4000 </h4> 4001 4002 <pre class="fcndec"> 4003const XML_Char * XMLCALL 4004XML_GetBase(XML_Parser p); 4005</pre> 4006 <div class="fcndef"> 4007 Return the base for resolving relative URIs. 4008 </div> 4009 4010 <h4 id="XML_GetSpecifiedAttributeCount"> 4011 XML_GetSpecifiedAttributeCount 4012 </h4> 4013 4014 <pre class="fcndec"> 4015int XMLCALL 4016XML_GetSpecifiedAttributeCount(XML_Parser p); 4017</pre> 4018 <div class="fcndef"> 4019 When attributes are reported to the start handler in the atts vector, attributes 4020 that were explicitly set in the element occur before any attributes that receive 4021 their value from default information in an ATTLIST declaration. This function 4022 returns the number of attributes that were explicitly set times two, thus giving 4023 the offset in the <code>atts</code> array passed to the start tag handler of the 4024 first attribute set due to defaults. It supplies information for the last call to 4025 a start handler. If called inside a start handler, then that means the current 4026 call. 4027 </div> 4028 4029 <h4 id="XML_GetIdAttributeIndex"> 4030 XML_GetIdAttributeIndex 4031 </h4> 4032 4033 <pre class="fcndec"> 4034int XMLCALL 4035XML_GetIdAttributeIndex(XML_Parser p); 4036</pre> 4037 <div class="fcndef"> 4038 Returns the index of the ID attribute passed in the atts array in the last call 4039 to <code><a href="#XML_StartElementHandler">XML_StartElementHandler</a></code>, 4040 or -1 if there is no ID attribute. If called inside a start handler, then that 4041 means the current call. 4042 </div> 4043 4044 <h4 id="XML_SetEncoding"> 4045 XML_SetEncoding 4046 </h4> 4047 4048 <pre class="fcndec"> 4049enum XML_Status XMLCALL 4050XML_SetEncoding(XML_Parser p, 4051 const XML_Char *encoding); 4052</pre> 4053 <div class="fcndef"> 4054 Set the encoding to be used by the parser. It is equivalent to passing a 4055 non-<code>NULL</code> encoding argument to the parser creation functions. It must 4056 not be called after <code><a href="#XML_Parse">XML_Parse</a></code> or 4057 <code><a href="#XML_ParseBuffer">XML_ParseBuffer</a></code> have been called on 4058 the given parser. Returns <code>XML_STATUS_OK</code> on success or 4059 <code>XML_STATUS_ERROR</code> on error. 4060 </div> 4061 4062 <h4 id="XML_SetParamEntityParsing"> 4063 XML_SetParamEntityParsing 4064 </h4> 4065 4066 <pre class="fcndec"> 4067int XMLCALL 4068XML_SetParamEntityParsing(XML_Parser p, 4069 enum XML_ParamEntityParsing code); 4070</pre> 4071 <div class="fcndef"> 4072 This enables parsing of parameter entities, including the external parameter 4073 entity that is the external DTD subset, according to <code>code</code>. The 4074 choices for <code>code</code> are: 4075 <ul> 4076 <li> 4077 <code>XML_PARAM_ENTITY_PARSING_NEVER</code> 4078 </li> 4079 4080 <li> 4081 <code>XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE</code> 4082 </li> 4083 4084 <li> 4085 <code>XML_PARAM_ENTITY_PARSING_ALWAYS</code> 4086 </li> 4087 </ul> 4088 <b>Note:</b> If <code>XML_SetParamEntityParsing</code> is called after 4089 <code>XML_Parse</code> or <code>XML_ParseBuffer</code>, then it has no effect and 4090 will always return 0. 4091 </div> 4092 4093 <h4 id="XML_SetHashSalt"> 4094 XML_SetHashSalt (deprecated) 4095 </h4> 4096 4097 <pre class="fcndec"> 4098int XMLCALL 4099XML_SetHashSalt(XML_Parser parser, 4100 unsigned long hash_salt); 4101</pre> 4102 <p> 4103 <b>Note:</b> This function is deprecated, please use function <code><a href= 4104 "#XML_SetHashSalt16Bytes">XML_SetHashSalt16Bytes</a></code>, instead. 4105 </p> 4106 4107 <div class="fcndef"> 4108 Sets the hash salt to use for internal hash calculations. Helps in preventing DoS 4109 attacks based on predicting hash function behavior. In order to have an effect 4110 this must be called before parsing has started. Returns 1 if successful, 0 when 4111 called after <code>XML_Parse</code> or <code>XML_ParseBuffer</code> or when 4112 <code>parser</code> is <code>NULL</code>. 4113 <p> 4114 <b>Note:</b> Function <code>XML_SetHashSalt</code> only provides 4 to 8 bytes 4115 of entropy (depending on the size of type <code>unsigned long</code>) while the 4116 SipHash implementation used by Expat can leverage up to 16 bytes of entropy — 4117 at least twice as much. Function <code><a href= 4118 "#XML_SetHashSalt16Bytes">XML_SetHashSalt16Bytes</a></code> of Expat >=2.8.0 4119 (and where backported) with better security matches the amount of entropy 4120 supported by SipHash. 4121 </p> 4122 4123 <p> 4124 <b>Note:</b> This call is optional, as the parser will auto-generate a new 4125 random salt value internally if no value has been set by the start of parsing. 4126 </p> 4127 4128 <p> 4129 <b>Note:</b> One should not call <code>XML_SetHashSalt</code> with a hash salt 4130 value of 0, as this value is used as sentinel value to indicate that 4131 <code>XML_SetHashSalt</code> has <b>not</b> been called. Consequently such a 4132 call will have no effect, even if it returns 1. 4133 </p> 4134 </div> 4135 4136 <h4 id="XML_SetHashSalt16Bytes"> 4137 XML_SetHashSalt16Bytes 4138 </h4> 4139 4140 <pre class="fcndec"> 4141/* Added in Expat 2.8.0. */ 4142XML_Bool XMLCALL 4143XML_SetHashSalt16Bytes(XML_Parser parser, 4144 const uint8_t entropy[16]); 4145</pre> 4146 <div class="fcndef"> 4147 Sets the hash salt to use for internal hash calculations. Helps in preventing DoS 4148 attacks based on predicting hash function behavior. In order to have an effect 4149 this must be called before parsing has started. Returns <code>XML_TRUE</code> if 4150 successful, <code>XML_FALSE</code> when called after <code>XML_Parse</code> or 4151 <code>XML_ParseBuffer</code> or when <code>parser</code> is <code>NULL</code>. 4152 <p> 4153 <b>Note:</b> Setting a salt that is <em>not</em> from a source of high quality 4154 entropy (like <code>getentropy(3)</code>) will make the parser vulnerable to 4155 hash flooding attacks. 4156 </p> 4157 4158 <p> 4159 <b>Note:</b> This call is optional, as the parser will auto-generate a new 4160 random salt value internally if no value has been set by the start of parsing. 4161 </p> 4162 </div> 4163 4164 <h4 id="XML_UseForeignDTD"> 4165 XML_UseForeignDTD 4166 </h4> 4167 4168 <pre class="fcndec"> 4169enum XML_Error XMLCALL 4170XML_UseForeignDTD(XML_Parser parser, XML_Bool useDTD); 4171</pre> 4172 <div class="fcndef"> 4173 <p> 4174 This function allows an application to provide an external subset for the 4175 document type declaration for documents which do not specify an external subset 4176 of their own. For documents which specify an external subset in their DOCTYPE 4177 declaration, the application-provided subset will be ignored. If the document 4178 does not contain a DOCTYPE declaration at all and <code>useDTD</code> is true, 4179 the application-provided subset will be parsed, but the 4180 <code>startDoctypeDeclHandler</code> and <code>endDoctypeDeclHandler</code> 4181 functions, if set, will not be called. The setting of parameter entity parsing, 4182 controlled using <code><a href= 4183 "#XML_SetParamEntityParsing">XML_SetParamEntityParsing</a></code>, will be 4184 honored. 4185 </p> 4186 4187 <p> 4188 The application-provided external subset is read by calling the external entity 4189 reference handler set via <code><a href= 4190 "#XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler</a></code> 4191 with both <code>publicId</code> and <code>systemId</code> set to 4192 <code>NULL</code>. 4193 </p> 4194 4195 <p> 4196 If this function is called after parsing has begun, it returns 4197 <code>XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING</code> and ignores 4198 <code>useDTD</code>. If called when Expat has been compiled without DTD 4199 support, it returns <code>XML_ERROR_FEATURE_REQUIRES_XML_DTD</code>. Otherwise, 4200 it returns <code>XML_ERROR_NONE</code>. 4201 </p> 4202 4203 <p> 4204 <b>Note:</b> For the purpose of checking WFC: Entity Declared, passing 4205 <code>useDTD == XML_TRUE</code> will make the parser behave as if the document 4206 had a DTD with an external subset. This holds true even if the external entity 4207 reference handler returns without action. 4208 </p> 4209 </div> 4210 4211 <h4 id="XML_SetReturnNSTriplet"> 4212 XML_SetReturnNSTriplet 4213 </h4> 4214 4215 <pre class="fcndec"> 4216void XMLCALL 4217XML_SetReturnNSTriplet(XML_Parser parser, 4218 int do_nst); 4219</pre> 4220 <div class="fcndef"> 4221 <p> 4222 This function only has an effect when using a parser created with 4223 <code><a href="#XML_ParserCreateNS">XML_ParserCreateNS</a></code>, i.e. when 4224 namespace processing is in effect. The <code>do_nst</code> sets whether or not 4225 prefixes are returned with names qualified with a namespace prefix. If this 4226 function is called with <code>do_nst</code> non-zero, then afterwards namespace 4227 qualified names (that is qualified with a prefix as opposed to belonging to a 4228 default namespace) are returned as a triplet with the three parts separated by 4229 the namespace separator specified when the parser was created. The order of 4230 returned parts is URI, local name, and prefix. 4231 </p> 4232 4233 <p> 4234 If <code>do_nst</code> is zero, then namespaces are reported in the default 4235 manner, URI then local_name separated by the namespace separator. 4236 </p> 4237 </div> 4238 4239 <h4 id="XML_DefaultCurrent"> 4240 XML_DefaultCurrent 4241 </h4> 4242 4243 <pre class="fcndec"> 4244void XMLCALL 4245XML_DefaultCurrent(XML_Parser parser); 4246</pre> 4247 <div class="fcndef"> 4248 This can be called within a handler for a start element, end element, processing 4249 instruction or character data. It causes the corresponding markup to be passed to 4250 the default handler set by <code><a href= 4251 "#XML_SetDefaultHandler">XML_SetDefaultHandler</a></code> or <code><a href= 4252 "#XML_SetDefaultHandlerExpand">XML_SetDefaultHandlerExpand</a></code>. It does 4253 nothing if there is not a default handler. 4254 </div> 4255 4256 <h4 id="XML_ExpatVersion"> 4257 XML_ExpatVersion 4258 </h4> 4259 4260 <pre class="fcndec"> 4261XML_LChar * XMLCALL 4262XML_ExpatVersion(); 4263</pre> 4264 <div class="fcndef"> 4265 Return the library version as a string (e.g. <code>"expat_1.95.1"</code>). 4266 </div> 4267 4268 <h4 id="XML_ExpatVersionInfo"> 4269 XML_ExpatVersionInfo 4270 </h4> 4271 4272 <pre class="fcndec"> 4273struct XML_Expat_Version XMLCALL 4274XML_ExpatVersionInfo(); 4275</pre> 4276 4277 <pre class="signature"> 4278typedef struct { 4279 int major; 4280 int minor; 4281 int micro; 4282} XML_Expat_Version; 4283</pre> 4284 <div class="fcndef"> 4285 Return the library version information as a structure. Some macros are also 4286 defined that support compile-time tests of the library version: 4287 <ul> 4288 <li> 4289 <code>XML_MAJOR_VERSION</code> 4290 </li> 4291 4292 <li> 4293 <code>XML_MINOR_VERSION</code> 4294 </li> 4295 4296 <li> 4297 <code>XML_MICRO_VERSION</code> 4298 </li> 4299 </ul> 4300 Testing these constants is currently the best way to determine if particular 4301 parts of the Expat API are available. 4302 </div> 4303 4304 <h4 id="XML_GetFeatureList"> 4305 XML_GetFeatureList 4306 </h4> 4307 4308 <pre class="fcndec"> 4309const XML_Feature * XMLCALL 4310XML_GetFeatureList(); 4311</pre> 4312 4313 <pre class="signature"> 4314enum XML_FeatureEnum { 4315 XML_FEATURE_END = 0, 4316 XML_FEATURE_UNICODE, 4317 XML_FEATURE_UNICODE_WCHAR_T, 4318 XML_FEATURE_DTD, 4319 XML_FEATURE_CONTEXT_BYTES, 4320 /* Added in Expat 1.95.5, removed in Expat 2.9.0. */ 4321 XML_FEATURE_MIN_SIZE, 4322 XML_FEATURE_SIZEOF_XML_CHAR, 4323 XML_FEATURE_SIZEOF_XML_LCHAR, 4324 /* Added in Expat 2.0.0. */ 4325 XML_FEATURE_NS, 4326 /* Added in Expat 2.0.1. */ 4327 XML_FEATURE_LARGE_SIZE, 4328 /* Added in Expat 2.1.0, removed in Expat 2.9.0. */ 4329 XML_FEATURE_ATTR_INFO, 4330 /* Added in Expat 2.4.0. */ 4331 XML_FEATURE_BILLION_LAUGHS_ATTACK_PROTECTION_MAXIMUM_AMPLIFICATION_DEFAULT, 4332 XML_FEATURE_BILLION_LAUGHS_ATTACK_PROTECTION_ACTIVATION_THRESHOLD_DEFAULT, 4333 /* Added in Expat 2.6.0. */ 4334 XML_FEATURE_GE, 4335 /* Added in Expat 2.7.2. */ 4336 XML_FEATURE_ALLOC_TRACKER_MAXIMUM_AMPLIFICATION_DEFAULT, 4337 XML_FEATURE_ALLOC_TRACKER_ACTIVATION_THRESHOLD_DEFAULT, 4338}; 4339 4340typedef struct { 4341 enum XML_FeatureEnum feature; 4342 XML_LChar *name; 4343 long int value; 4344} XML_Feature; 4345</pre> 4346 <div class="fcndef"> 4347 <p> 4348 Returns a list of "feature" records, providing details on how Expat was 4349 configured at compile time. Most applications should not need to worry about 4350 this, but this information is otherwise not available from Expat. This function 4351 allows code that does need to check these features to do so at runtime. 4352 </p> 4353 4354 <p> 4355 The return value is an array of <code>XML_Feature</code>, terminated by a 4356 record with a <code>feature</code> of <code>XML_FEATURE_END</code> and 4357 <code>name</code> of <code>NULL</code>, identifying the feature-test macros 4358 Expat was compiled with. Since an application that requires this kind of 4359 information needs to determine the type of character the <code>name</code> 4360 points to, records for the <code>XML_FEATURE_SIZEOF_XML_CHAR</code> and 4361 <code>XML_FEATURE_SIZEOF_XML_LCHAR</code> will be located at the beginning of 4362 the list, followed by <code>XML_FEATURE_UNICODE</code> and 4363 <code>XML_FEATURE_UNICODE_WCHAR_T</code>, if they are present at all. 4364 </p> 4365 4366 <p> 4367 Some features have an associated value. If there isn't an associated value, the 4368 <code>value</code> field is set to 0. At this time, the following features have 4369 been defined to have values: 4370 </p> 4371 4372 <dl> 4373 <dt> 4374 <code>XML_FEATURE_SIZEOF_XML_CHAR</code> 4375 </dt> 4376 4377 <dd> 4378 The number of bytes occupied by one <code>XML_Char</code> character. 4379 </dd> 4380 4381 <dt> 4382 <code>XML_FEATURE_SIZEOF_XML_LCHAR</code> 4383 </dt> 4384 4385 <dd> 4386 The number of bytes occupied by one <code>XML_LChar</code> character. 4387 </dd> 4388 4389 <dt> 4390 <code>XML_FEATURE_CONTEXT_BYTES</code> 4391 </dt> 4392 4393 <dd> 4394 The maximum number of characters of context which can be reported by 4395 <code><a href="#XML_GetInputContext">XML_GetInputContext</a></code>. 4396 </dd> 4397 </dl> 4398 </div> 4399 4400 <h4 id="XML_FreeContentModel"> 4401 XML_FreeContentModel 4402 </h4> 4403 4404 <pre class="fcndec"> 4405void XMLCALL 4406XML_FreeContentModel(XML_Parser parser, XML_Content *model); 4407</pre> 4408 <div class="fcndef"> 4409 Function to deallocate the <code>model</code> argument passed to the 4410 <code>XML_ElementDeclHandler</code> callback set using <code><a href= 4411 "#XML_SetElementDeclHandler">XML_ElementDeclHandler</a></code>. This function 4412 should not be used for any other purpose. 4413 </div> 4414 4415 <p> 4416 The following functions allow external code to share the memory allocator an 4417 <code>XML_Parser</code> has been configured to use. This is especially useful for 4418 third-party libraries that interact with a parser object created by application 4419 code, or heavily layered applications. This can be essential when using 4420 dynamically loaded libraries which use different C standard libraries (this can 4421 happen on Windows, at least). 4422 </p> 4423 4424 <h4 id="XML_MemMalloc"> 4425 XML_MemMalloc 4426 </h4> 4427 4428 <pre class="fcndec"> 4429void * XMLCALL 4430XML_MemMalloc(XML_Parser parser, size_t size); 4431</pre> 4432 <div class="fcndef"> 4433 Allocate <code>size</code> bytes of memory using the allocator the 4434 <code>parser</code> object has been configured to use. Returns a pointer to the 4435 memory or <code>NULL</code> on failure. Memory allocated in this way must be 4436 freed using <code><a href="#XML_MemFree">XML_MemFree</a></code>. 4437 </div> 4438 4439 <h4 id="XML_MemRealloc"> 4440 XML_MemRealloc 4441 </h4> 4442 4443 <pre class="fcndec"> 4444void * XMLCALL 4445XML_MemRealloc(XML_Parser parser, void *ptr, size_t size); 4446</pre> 4447 <div class="fcndef"> 4448 Allocate <code>size</code> bytes of memory using the allocator the 4449 <code>parser</code> object has been configured to use. <code>ptr</code> must 4450 point to a block of memory allocated by <code><a href= 4451 "#XML_MemMalloc">XML_MemMalloc</a></code> or <code>XML_MemRealloc</code>, or be 4452 <code>NULL</code>. This function tries to expand the block pointed to by 4453 <code>ptr</code> if possible. Returns a pointer to the memory or 4454 <code>NULL</code> on failure. On success, the original block has either been 4455 expanded or freed. On failure, the original block has not been freed; the caller 4456 is responsible for freeing the original block. Memory allocated in this way must 4457 be freed using <code><a href="#XML_MemFree">XML_MemFree</a></code>. 4458 </div> 4459 4460 <h4 id="XML_MemFree"> 4461 XML_MemFree 4462 </h4> 4463 4464 <pre class="fcndec"> 4465void XMLCALL 4466XML_MemFree(XML_Parser parser, void *ptr); 4467</pre> 4468 <div class="fcndef"> 4469 Free a block of memory pointed to by <code>ptr</code>. The block must have been 4470 allocated by <code><a href="#XML_MemMalloc">XML_MemMalloc</a></code> or 4471 <code>XML_MemRealloc</code>, or be <code>NULL</code>. 4472 </div> 4473 4474 <hr /> 4475 4476 <div class="footer"> 4477 Found a bug in the documentation? <a href= 4478 "https://github.com/libexpat/libexpat/issues">Please file a bug report.</a> 4479 </div> 4480 </div> 4481 </body> 4482</html> 4483