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